Fix OpenCode adapter timeout and event handling#88
Conversation
33f4095 to
6392606
Compare
| if (idleSessionId && idleSessionId !== this.agentSessionId) { | ||
| continue; | ||
| } |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
- Use async endpoint (prompt_async) instead of sync endpoint The sync /message endpoint blocks until AI completes, causing curl to timeout on long-running operations (>30s) - Increase SSE timeout from 2 minutes to 10 minutes for long ops - Filter SSE events by sessionID to prevent cross-session interference Events from other sessions could cause premature session.idle detection - Remove duplicate error emission (was sending error twice to clients) - Add sessionExists() check before using saved agentSessionId Prevents "Connection failed" when restored session doesn't exist on server Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
6392606 to
daf2fa9
Compare
| const idleSessionId = event.properties?.sessionID; | ||
| if (!idleSessionId || idleSessionId !== this.agentSessionId) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Bug: The new filtering logic discards SSE events if the optional sessionID field is missing, which could lead to timeouts or silently dropped messages.
Severity: CRITICAL
🔍 Detailed Analysis
The filtering logic for session.idle and message.part.updated events assumes the sessionID field will always be present. However, the OpenCodeServerEvent interface defines sessionID as optional. If the OpenCode service sends a valid event for the current session but omits the sessionID, this code will incorrectly discard it. This can lead to two failure modes: a 10-minute stream timeout if a session.idle event is dropped, or silent loss of AI responses if a message.part.updated event is dropped.
💡 Suggested Fix
Modify the filtering logic to correctly handle events where sessionID is undefined. The logic should only filter out events that have a sessionID that does not match the current agentSessionId. Events without a sessionID should be processed, assuming they are relevant to the current session.
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/session-manager/adapters/opencode.ts#L521-L524
Potential issue: The filtering logic for `session.idle` and `message.part.updated`
events assumes the `sessionID` field will always be present. However, the
`OpenCodeServerEvent` interface defines `sessionID` as optional. If the OpenCode service
sends a valid event for the current session but omits the `sessionID`, this code will
incorrectly discard it. This can lead to two failure modes: a 10-minute stream timeout
if a `session.idle` event is dropped, or silent loss of AI responses if a
`message.part.updated` event is dropped.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8438808
Summary
prompt_async) instead of sync - prevents timeout on long-running operationsRoot Cause
The sync
/session/:id/messageendpoint blocks until the AI completes processing. Our curl had a 30-second timeout, so any operation taking longer would fail with "Connection failed". The async endpoint returns 204 immediately and we rely on SSE for the response.Test plan
🤖 Generated with Claude Code