Feat: Implement Kill Switch and Structured Logging#16
Open
google-labs-jules[bot] wants to merge 8 commits intomainfrom
Open
Feat: Implement Kill Switch and Structured Logging#16google-labs-jules[bot] wants to merge 8 commits intomainfrom
google-labs-jules[bot] wants to merge 8 commits intomainfrom
Conversation
This commit introduces the new UI handler structure, moving logic from listener_bot.py to handlers.py, main.py, and telegram_logic_adapter.py. Key changes include: - Implemented new UI handlers in handlers.py for better modularity. - Fixed a critical bug in mode change logic (LIVE/PAPER_TRADING) in handlers.py, addressing contradictory messages and ensuring correct transitions. - Resolved a SyntaxError in handlers.py related to async function definitions. - Improved test stability by fixing ImportError in tests/conftest.py and adding a dedicated test file for handlers.py. - Updated README.md to reflect recent advancements and ongoing work on risk management.
… to be unresponsive. The root cause was an outdated startup script (`start_all.sh`) that attempted to run a non-existent `listener_bot.py`. This meant the interactive part of the bot never started. A secondary cause was a conflict between two different bot implementations (`aiogram` in `run_bot.py` and `python-telegram-bot` in `main.py`), which would have caused issues even if the script was correct. The fixes are as follows: 1. **Modified `run_bot.py`:** Disabled the `aiogram` polling mechanism to prevent conflicts. This script now acts solely as a background analysis engine. 2. **Updated `start_all.sh`:** The script now launches the correct `main.py` for the interactive bot, ensuring that user commands are handled. 3. **Fixed Markdown in `handlers.py`:** Corrected a formatting issue that caused the bot to crash when sending the `/start` message due to unescaped characters in MarkdownV2 text. 4. **Added Global Error Handler:** Implemented a global error handler in `main.py` to make the bot more robust and provide detailed logs for future errors. 5. **Fixed Database Query:** Resolved a subtle bug in `telegram_logic_adapter.py` where a database query was not using the required SQLAlchemy `text()` construct, which was causing a sporadic `NoneType` error. These changes restore the bot's functionality and improve its stability and reliability.
This commit fixes the unresponsive UI buttons in the Telegram bot by implementing the required handlers and backend logic. The following features have been added: - A new 'Panel de Control' accessible from the main menu. - Handlers to show open positions and shield status in the control panel. - Handlers to manage risk settings, allowing users to switch between automatic (ML-based) and manual risk modes. - A conversation handler to guide the user through setting a manual risk percentage. The changes include: - `handlers.py`: Added new `CallbackQueryHandler`s and a `ConversationHandler` to manage the new UI interactions. - `keyboards.py`: Updated with new buttons for the main menu and control panel. - `telegram_logic_adapter.py`: Extended to bridge the new UI handlers with the backend managers (`risk_manager`, `shield_manager`, `position_manager`).
This commit addresses a critical bug where the 'Control Operativo' button was not functioning correctly. The issue was multifaceted, involving a faulty state management implementation, incorrect business logic in the UI handlers, and case-sensitivity issues in the order executor. The fix includes: - Refactoring the mode-switching logic in `handlers.py` into a single, robust `ConversationHandler` to simplify the flow and remove bugs. - Consolidating multiple keyboards in `keyboards.py` into a single dynamic keyboard that correctly reflects the current bot state. - Fixing a critical race condition in `utils/state_manager.py` by ensuring the state is loaded from disk before any modification, preventing stale data from being written. - Correcting a case-sensitivity bug in `utils/order_executor.py` to ensure it properly recognizes the 'LIVE' trading mode. These changes restore the functionality of the 'Control Operativo' button, making the process of switching between LIVE and PAPER modes reliable and robust.
This commit addresses a critical bug where the 'Control Operativo' button was not functioning correctly. The issue was multifaceted, involving a faulty state management implementation, overly complex UI handlers, and case-sensitivity issues in the order executor. The fix includes: - Refactoring the mode-switching logic in `handlers.py` into a single, robust `ConversationHandler` to simplify the flow, improve user feedback, and remove bugs. - Consolidating multiple keyboards in `keyboards.py` into a single dynamic keyboard that correctly reflects the current bot state. - Fixing a critical race condition in `utils/state_manager.py` by ensuring the state is loaded from disk before any modification, preventing stale data from being written. - Correcting a case-sensitivity bug in `utils/order_executor.py` to ensure it properly recognizes the 'LIVE' trading mode. - Simplifying the logic for returning to the main menu after a state change, which resolves a potential `telegram.error.TimedOut` error. These changes restore the functionality of the 'Control Operativo' button, making the process of switching between LIVE and PAPER modes reliable and robust.
Implements a robust kill switch and a comprehensive structured logging system. Kill Switch: - Adds a `/kill_switch` command restricted to an admin user ID. - The command liquidates all open positions using market orders and then puts the bot into a persistent "paused" state. - Includes a retry mechanism (one retry) for failed liquidation orders. - Adds a `/resume` command to exit the paused state. - The main trading loop in `run_bot.py` now respects the paused state. Structured Logging: - Refactors the entire logging system to be structured. - Logs are now written to file as JSON objects, containing detailed context (event_id, event_type, details). - Console logs remain in a human-readable text format for easy monitoring. - A new `StructuredLogger` class is introduced to centralize and standardize log creation. - Key modules (`run_bot.py`, `execution_worker.py`, `telegram_logic_adapter.py`) are updated to use the new logger. Testing: - Adds a new test suite for the kill switch feature, mocking the Telegram API to test the conversation flow, authorization, and logic. - Adds a new test suite for the logging system to verify the dual-format output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch introduces two major features as requested:
Kill Switch Command (
/kill_switch): A new admin-only emergency command that reliably liquidates all open positions and pauses all further trading activity. A corresponding/resumecommand is also included.Structured JSON Logging: The logging system has been overhauled to produce structured JSON logs to a file. This enables better programmatic analysis and monitoring while maintaining readable console output.