fix(daemon): preserve agent context when injecting messages#72
Open
fix(daemon): preserve agent context when injecting messages#72
Conversation
a93f422 to
60c582b
Compare
The daemon was injecting messages without knowing which agent the TUI client was actually using. This caused OpenCode to potentially use a different agent than expected. Changes: - Add get_agent_from_session_messages() to query the actual agent from the OpenCode SQLite database (from the most recent message) - Update get_or_create_agent_for_session() to detect and use the running agent, falling back to session slug/ID if not found - Add optional agent parameter to inject_message_sync and inject_message - Update InjectionTask dataclass to include agent field - Pass agent ID when injecting messages to ensure correct agent handles it - Update existing tests to expect new agent parameter - Add comprehensive test suite for agent detection (13 tests) - Add test suite for message injection with agent (9 tests) - Add integration tests for full agent detection flow (5 tests) Logging improvements: - Log coordinator's agent when detected at startup - Log agent being used for each prompt_async injection - Log coordinator agent periodically in cost polling - Log agent detection capability at daemon startup The daemon now detects the actual agent by: 1. Querying the SQLite database for the most recent message in the session 2. Extracting the 'agent' field from the message JSON data 3. Using that agent for all future injections into that session All 177 tests pass.
60c582b to
2b58bb2
Compare
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.
Summary
Detects and uses the actual agent that TUI clients are running from the OpenCode SQLite database, ensuring injected messages are handled by the correct agent.
How It Works
get_agent_from_session_messages()queries the SQLite database for the most recent message in each session and extracts theagentfieldget_or_create_agent_for_session()uses the detected agent for session registration, falling back to session slug/ID for new sessionsinject_message_sync()calls include the target agent ID in the/prompt_asyncpayloadKey Features
Changes
get_agent_from_session_messages()to query agent from SQLiteagentparameter toinject_message_sync()andinject_message()InjectionTaskdataclass withagentfieldget_or_create_agent_for_session()to detect and use running agentFiles Changed
src/opencode_agent_hub/daemon.py- Core agent detection and injection logictests/test_agent_detection.py- 13 tests for agent detection from SQLitetests/test_message_injection.py- 9 tests for agent parameter in injectionstests/test_integration.py- 5 integration tests for full flowtests/test_orientation_retry.py- Updated existing tests for new parameterTesting