Skip to content

fix(daemon): preserve agent context when injecting messages#72

Open
xnoto wants to merge 1 commit intomainfrom
fix/agent-context-in-message-injection
Open

fix(daemon): preserve agent context when injecting messages#72
xnoto wants to merge 1 commit intomainfrom
fix/agent-context-in-message-injection

Conversation

@xnoto
Copy link
Copy Markdown
Owner

@xnoto xnoto commented Mar 27, 2026

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

  1. Agent Detection: get_agent_from_session_messages() queries the SQLite database for the most recent message in each session and extracts the agent field
  2. Agent Assignment: get_or_create_agent_for_session() uses the detected agent for session registration, falling back to session slug/ID for new sessions
  3. Agent Injection: All inject_message_sync() calls include the target agent ID in the /prompt_async payload

Key Features

  • Automatic Detection: Detects agent from actual message history, not just config
  • Dynamic Updates: Handles agent switching mid-session (uses most recent message)
  • Per-Session Tracking: Different sessions can use different agents
  • Graceful Fallback: Falls back to session slug/ID for new sessions without messages
  • Comprehensive Logging: Reports coordinator agent and injection agent in logs

Changes

  • Added get_agent_from_session_messages() to query agent from SQLite
  • Added optional agent parameter to inject_message_sync() and inject_message()
  • Updated InjectionTask dataclass with agent field
  • Updated get_or_create_agent_for_session() to detect and use running agent
  • Added logging for coordinator agent detection and message injection agent
  • Added 27 comprehensive tests covering detection, injection, and integration

Files Changed

  • src/opencode_agent_hub/daemon.py - Core agent detection and injection logic
  • tests/test_agent_detection.py - 13 tests for agent detection from SQLite
  • tests/test_message_injection.py - 9 tests for agent parameter in injections
  • tests/test_integration.py - 5 integration tests for full flow
  • tests/test_orientation_retry.py - Updated existing tests for new parameter

Testing

  • 177 tests pass (150 existing + 27 new)
  • Ruff linter passes with no issues

@xnoto xnoto force-pushed the fix/agent-context-in-message-injection branch 3 times, most recently from a93f422 to 60c582b Compare March 27, 2026 21:38
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.
@xnoto xnoto force-pushed the fix/agent-context-in-message-injection branch from 60c582b to 2b58bb2 Compare March 27, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant