Skip to content

fix(opencode): ensure tool_use/tool_result integrity and Anthropic user-first ordering#31547

Open
TeddyEngel wants to merge 1 commit into
anomalyco:devfrom
TeddyEngel:fix/ensure-tool-integrity
Open

fix(opencode): ensure tool_use/tool_result integrity and Anthropic user-first ordering#31547
TeddyEngel wants to merge 1 commit into
anomalyco:devfrom
TeddyEngel:fix/ensure-tool-integrity

Conversation

@TeddyEngel

@TeddyEngel TeddyEngel commented Jun 9, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #27594

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

(The PR implements fix 4 from the linked issue: "A defensive pass that pairs every tool_use with the matching tool_result before dispatch.")

Sessions that had undergone nested compaction would fail to resume. The specific error encountered was from Anthropic:

messages.2: `tool_use` ids were found without `tool_result` blocks immediately after

Two issues were identified:

Orphan Tool-Calls (All Providers)

After nested compaction, tool-calls could become orphaned (no corresponding tool-result). Most LLM APIs reject this.

User-First Ordering (Anthropic Only)

After nested compaction, the message array could start with system messages followed directly by an assistant message. Anthropic requires the first non-system message to be user:

How did you verify your code works?

  • All 276 tests pass
  • New tests cover the edge cases identified.
  • I tested resuming a session that had crashed with that dead-end case - the session was succesfully restored with the fix.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

@rekram1-node

Copy link
Copy Markdown
Collaborator

believe this has previously been fixed elsewhere actually, unless u can send me a session where u hit it

@TeddyEngel

Copy link
Copy Markdown
Author

@rekram1-node it's unfortunately still an issue. Here is a redacted session that crashes on latest dev, resumes ok with my fix: crash_minimal.json

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.

Session permanently stuck after auto-compaction: post-compaction auto-trigger fires tool_use without tool_result error and is non-recoverable

2 participants