Repository navigation
fix(agent-events): deduplicate tool call events after watchdog force resends - #156
Open
alienfacepalm wants to merge 1 commit into
Open
alienfacepalm wants to merge 1 commit into
alienfacepalm wants to merge 1 commit into
Conversation
…resends
When the stream watchdog detects a stall and forces a resend with
`local: { force: true }`, the Cursor SDK may replay tool-call-started
events with the same callId. OpenCode v2 tracks which tool call IDs
have been received and rejects duplicates, throwing:
`Error: Duplicate tool call: {id}`
This was observed in long agentic sessions (15+ minutes) with OpenCode
v2.2.6, always right after bash/PowerShell tool calls when the watchdog
triggered a force resend.
Root cause: OpenCode v2's publish-llm-event handler
(packages/core/src/session/runner/publish-llm-event.ts:479) maintains
a per-turn registry of tool calls and explicitly rejects duplicates:
```typescript
if (tool.called) return yield* Effect.die(new Error(`Duplicate tool call: ${event.id}`))
```
The plugin had no deduplication logic, so replayed events from force
resends reached OpenCode and were rejected.
Fix: Track emitted tool call IDs in a Set and skip duplicate
tool-call-started events. The Set is scoped per turn (local to
streamAgentTurn), persists across force resends, and only filters
tool-call events (tool-result events pass through unchanged).
Testing:
- Added test/duplicate-tool-call.test.ts with 3 test cases
- All new tests pass (3/3)
- All existing tests pass (659/659)
- Typecheck and build successful
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.
Problem
In long agentic sessions on OpenCode v2.2.6, runs die with
Error: Duplicate tool call: <id>, usually right after a bash/PowerShell tool call.Root cause
When the stream watchdog detects a stall, it cancels and resends with
local: { force: true }. The Cursor SDK can then replaytool-call-startedevents with the samecallId. OpenCode v2 rejects repeated tool call IDs on purpose (packages/core/src/session/runner/publish-llm-event.ts,if (tool.called) ... Duplicate tool call), and the plugin forwarded the replays.Fix
streamAgentTurnnow tracks emitted tool call IDs in a per-turn Set that survives force resends, and skips repeatedtool-call-startedevents (logged in debug mode). Tool results pass through unchanged.Testing
test/duplicate-tool-call.test.ts(3 cases: single duplicate, distinct IDs, repeated duplicates)