Skip to content

fix: append diagram-state context after tool_result blocks, not before - #79

Merged
tomasz-zajac-oss merged 1 commit into
mainfrom
fix/forge-tool-result-ordering
Sep 11, 2026
Merged

tomasz-zajac-oss merged 1 commit into
mainfrom
fix/forge-tool-result-ordering

Conversation

@tomasz-zajac-oss

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes a real bug hit in production right after feat: 5-point token/latency optimization for the AI integration #78 merged: multi-round Radical Forge runs against the real Anthropic API failed with HTTP 400: tool_use ids were found without tool_result blocks immediately after.
  • feat: 5-point token/latency optimization for the AI integration #78 moved the rolling diagram-state snapshot into each round's outgoing turn as a leading text block (withLeadingText), ahead of the tool_result blocks that answer the previous round's tool_use calls. Anthropic requires tool_result to be the first content in that turn — a block in front of it breaks the pairing and the request is rejected outright.
  • Fix: append the snapshot after the turn's existing content instead (withTrailingText). Round 1's plain prompt is unaffected either way; round 2+'s tool_result blocks now stay first, as required.

Test plan

  • npm run typecheck clean
  • npm run test — 370 passing, including updated aiRunner.test.ts assertions that now explicitly check content[0].type === 'tool_result' on a tool-result turn (this is exactly the check that would have caught the original bug) and that the context snapshot lands at content.at(-1) instead

🤖 Generated with Claude Code

Anthropic rejects a tool-result turn with a 400 ("tool_use ids were found
without tool_result blocks immediately after") when anything precedes the
tool_result block(s) that answer the prior round's tool_use. The rolling
diagram-state snapshot (PR #78) was being prepended to each round's
outgoing turn, which broke every multi-round Forge run against the real
API. Append it after the turn's own content instead — round 1's plain
prompt is unaffected either way, and round 2+'s tool_result blocks now
stay first.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tomasz-zajac-oss
tomasz-zajac-oss merged commit ef374e0 into main Sep 11, 2026
2 checks passed
@tomasz-zajac-oss
tomasz-zajac-oss deleted the fix/forge-tool-result-ordering branch September 11, 2026 14:28
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.

1 participant