Skip to content

feat(tool-use-fix): cherry-pick upstream PR #8497 — fix dangling tool_use blocks on abort/crash #218

@randomm

Description

@randomm

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:

  1. The tool part remains in `running`/`pending` state in the DB (the abort races the DB write)
  2. On session resume, `toModelMessages()` emits a `tool_use` block from the stale DB state
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions