Skip to content

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

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

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

@AlphaComposite

Copy link
Copy Markdown

I'm using the newest version of OpenCode, and I got this error, too, using Anthropic models.

@TeddyEngel

Copy link
Copy Markdown
Author

I'm using the newest version of OpenCode, and I got this error, too, using Anthropic models.

@AlphaComposite Feel free to cherrypick my commit into your local forked repo and run it locally as I don't know if they want to merge this or not - you won't get the issue and it should resume the compacted sessions properly. Been running it myself for 5 days and not getting any issue anymore. I believe they are working on a fix for the root cause

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

3 participants