-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Summary
Upstream PR anomalyco#8497 fixes the orphaned `tool_use`/`tool_result` bug that causes Anthropic API errors like:
`tool_use` ids were found without `tool_result` blocks immediately after: toolu_01Bt3roC3qGySjcx1xgjdYSk
Upstream has 6+ open issues on this (dating to Aug 2025: anomalyco#2214, anomalyco#1662, anomalyco#2720, anomalyco#5750, anomalyco#8377, anomalyco#10616) and PR anomalyco#8497 has been sitting unmerged since Jan 14, 2026. We should cherry-pick it into our fork now rather than wait.
Root Cause
When a session is aborted/cancelled while a tool is mid-execution:
- The tool part remains in `running`/`pending` state in the DB (the abort races the DB write)
- On session resume, `toModelMessages()` emits a `tool_use` block from the stale DB state
- No corresponding `tool_result` block exists → Anthropic API rejects the message array
The existing in-memory workaround at `message-v2.ts:660–670` (introduced upstream Jan 16, 2026) only covers in-memory state — it misses cases where the DB didn't get the write before the crash.
Fix (upstream PR anomalyco#8497)
Cherry-pick anomalyco#8497 into our fork:
- Converts `pending`/`running` tool calls to synthetic error `tool_result` blocks inside `toModelMessages()`
- This ensures the fix applies at API-call time regardless of whether the DB was written before the abort
Tasks
- Read upstream PR fix: handle dangling tool_use blocks for LiteLLM proxy compatibility anomalyco/opencode#8497 diff to understand exact changes
- Cherry-pick or manually apply the patch to `packages/opencode/src/session/message-v2.ts`
- Verify typecheck passes: `bun run typecheck`
- Verify tests pass: `bun test`
- Add entry to `.fork-features/manifest.json` (see schema below)
- Run `bun test .fork-features/verify.ts` to baseline
- Create PR targeting `randomm/opencode` dev branch
Manifest Entry (to add to .fork-features/manifest.json)
```json
"tool-use-abort-fix": {
"status": "active",
"description": "Fixes orphaned tool_use blocks when session is aborted mid-tool. Converts pending/running tool calls to synthetic error tool_result blocks in toModelMessages() at API-call time, ensuring the fix applies even when the DB write did not complete before the abort. Cherry-picked from upstream PR anomalyco#8497 (open since Jan 14 2026, unmerged).",
"issue": "https://github.com/randomm/opencode/issues/THISNUM",
"newFiles": [],
"modifiedFiles": ["packages/opencode/src/session/message-v2.ts"],
"deletedFiles": [],
"criticalCode": [
"pending.*tool.*error.*result",
"running.*tool.*error.*result",
"dangling tool_use",
"synthetic.*tool_result"
],
"tests": [],
"upstreamTracking": {
"relatedPRs": ["anomalyco#8497"],
"relatedIssues": [
"anomalyco#2214",
"anomalyco#2720",
"anomalyco#5750",
"anomalyco#8377",
"anomalyco#10616"
],
"absorptionSignals": [
"dangling.*tool_use",
"pending.*tool.*tool_result",
"running.*tool.*synthetic.*error"
]
}
}
```
Quality Gates
- Tests written/updated as needed
- `bun run typecheck` passes (0 errors)
- `bun test` passes (0 failures)
- `.fork-features/verify.ts` passes baseline
- Local verification complete before push
References
- Upstream PR: fix: handle dangling tool_use blocks for LiteLLM proxy compatibility anomalyco/opencode#8497
- Upstream issues: AI_APICallError: messages.3:
tool_useids were found withouttool_resultblocks immediately after: toolu_01G5ipgNZWmDHuV6cvzyQpfH. Eachtool_useblock must have a correspondingtool_resultblock in the next message. anomalyco/opencode#2214, AI_APICallError: tool_use blocks found without corresponding tool_result blocks anomalyco/opencode#2720, Tool use id bug anomalyco/opencode#5750, Most sessions eventually gets antool_useerror anomalyco/opencode#8377, messages.87:tool_useids were found withouttool_resultblocks immediately anomalyco/opencode#10616 - Related upstream fix (partial): commit `9b57db30d` — in-memory workaround at `message-v2.ts:660-670`