Skip to content

fix(ai): repair interrupted tool result ordering - #1102

Closed
junhoyeo wants to merge 1 commit into
PrimeIntellect-ai:mainfrom
junhoyeo:fix/interrupted-tool-result-ordering-upstream
Closed

fix(ai): repair interrupted tool result ordering#1102
junhoyeo wants to merge 1 commit into
PrimeIntellect-ai:mainfrom
junhoyeo:fix/interrupted-tool-result-ordering-upstream

Conversation

@junhoyeo

@junhoyeo junhoyeo commented Aug 9, 2026

Copy link
Copy Markdown

Summary

  • Repair interrupted tool turns when a persisted user/custom message separates a tool call from its delayed real result.
  • Hoist matching real results before the interposed message, synthesize only genuinely missing results, and discard invalid orphan or duplicate results.
  • Heal already-persisted affected sessions in memory without rewriting append-only session evidence.

Root cause

An update restart can persist this temporal order while aborting an in-flight tool:

assistant(tool call T)
custom update-restart marker
real tool result T: Request was aborted

The marker becomes a user message in model context. The existing transform synthesized a replacement result before that user boundary but later emitted the real result too. Anthropic then rejected the late result because its immediately preceding message had no matching tool_use, permanently blocking every subsequent request that replayed the same history.

The provider transform is the shared repair boundary for this invariant. Matching is bounded at the next assistant message, occurs after tool-call ID normalization, preserves real results over synthetic ones, and is a no-op for well-formed turns.

Validation

  • cd packages/ai && npx tsx ../../node_modules/vitest/dist/cli.js --run test/transform-messages-copilot-openai-to-anthropic.test.ts — 10 passed
  • npm run check — passed on the upstream main base; Biome checked 900 files with no fixes, type checking and installer/browser smoke checks passed
  • Replayed the original affected local session through buildSessionContextconvertToLlm → patched transformMessages: 1,904 persisted entries, 377 transformed messages, the real interrupted result preserved, and zero tool-pairing violations
  • GPT-5.6 Terra adversarial review — PASS after reducing the patch to the provider-boundary repair and avoiding update-daemon lifecycle changes

Note

Fix tool result ordering when an assistant turn is interrupted by a user message

  • Reworks the second pass in transformMessages to correctly handle tool results that arrive after an interposed user message during an interrupted assistant turn.
  • Hoists valid tool results across an intervening user message to match pending tool calls, marks them consumed to avoid double-processing, and synthesizes error results for any unresolved tool calls at user boundaries and end-of-conversation.
  • Drops orphaned tool results (no matching pending call) and duplicate tool results.
  • Behavioral Change: messages with roles other than assistant, toolResult, and user are no longer forwarded in the second pass.

Macroscope summarized 0e0452c.

@sethkarten

Copy link
Copy Markdown
Contributor

Thank you for the report and proposed work. This root cause is now covered by maintainer-owned stacked PR #1165, authored independently from upstream/main.

We did not inspect or reuse this PR's diff, branch, commits, implementation code, or tests; its public description/comments were used only as a bug report. To keep one review surface, this PR is superseded by #1165 and is being closed.

The complete review stack is #1158#1165. It is being left unmerged for human review after CI and review-bot findings are cleared.

@sethkarten sethkarten closed this Aug 10, 2026
sethkarten added a commit that referenced this pull request Aug 10, 2026
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.

2 participants