Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): persist late Claude tool results after turn finalize #7192

Closedmaslinedwin(GitHub profile, opens in a new tab)wants to merge 6 commits intomain fromrefs/pull/7192/headopened View on GitHub

Fixes #6389

ClaudeAdapter.completeTurn force-completes any still-in-flight tools without a result, then clears inFlightTools. If the matching SDK tool_result arrives afterwards — the SDK result landing first, or finalize from interrupt, stream exit, session stop, or a non-steering turn handoff — handleUserMessage misses the lookup and drops the output with no warning. The persisted item.completed then has only toolName and input.

This keeps a bounded per-session map of those finalized tools, keyed by toolUseId. A late tool_result now emits the same item.updated + item.completed events as the happy path, including data.result, attributed to the original turn. It does not start a new turn or mark a completed turn as running.

Unknown toolUseId values after finalize do not throw. They log claude.tool_result.unknown-tool-use and are ignored. Map entries are removed when the late result is applied, evicted after 64 pending tools, and cleared on session teardown.

Coverage in ClaudeAdapter.test.ts:

  • late tool_result after completeTurn still emits completed with data.result
  • unknown toolUseId after finalize is a no-op
  • happy-path tool_result before completeTurn still works

Grok / T3 Code


Note

Medium Risk Touches Claude adapter turn/tool lifecycle and activity persistence; behavior is well covered by tests but ordering and duplicate-event edge cases matter for the thread timeline.

Overview Fixes a race where Claude’s turn can finish before a tool_result arrives, leaving force-completed tools without output in storage and the UI.

Server: ClaudeAdapter keeps force-completed tools in a bounded finalizedToolsAwaitingResult map (keyed by tool use id). Late tool_result messages emit a terminal item.updated (with full data.result) on the original turn, plus content.delta when needed, and skip a second item.completed. Tool event payloads now include toolCallId. ProviderRuntimeIngestion persists the full payload for terminal item.updated events (not only the trimmed projection used for in-progress streaming).

Clients: Web and mobile work-log collapse merges tool.updated / tool.completed rows by toolCallId even when they are not adjacent, preserving the first row’s createdAt so ordering stays correct.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 16ba7a6eff17048c75d0dd20fe3c877c453e0fca. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Persist late Claude tool results after turn finalization and merge them in the thread feed

  • Claude tool calls force-completed by completeTurn are now tracked in a bounded finalizedToolsAwaitingResult map so that tool_result messages arriving after turn finalization are matched to the correct turn.
  • Late terminal tool_result events emit an item.updated with status completed/failed (attributed to the finalized turn) without re-emitting item.completed; unknown tool_use_id values are ignored with a warning.
  • All tool lifecycle events now include toolCallId in their payload; terminal item.updated events persist the full payload (including result) instead of a size-limited projection.
  • The thread feed (collapseDerivedWorkLogEntries in both web and mobile) merges non-adjacent late tool.updated entries into their corresponding completed rows by toolCallId, preserving the original createdAt.
  • Behavioral Change: previously, all item.updated payloads were projected/truncated; terminal updates now bypass projection to retain the full result.
Macroscope summarized 16ba7a6.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 7cdc2096-ed25-4de8-a55b-41a5374492f4

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium

      https://github.com/pingdotgg/t3code/blob/b9f0a6b0449f82340162d415becc61d9dd3d932e/apps/server/src/provider/Layers/ClaudeAdapter.ts#L2797

      Late finalized tool results emit content.delta against context.turnState.turnId, so output is assigned to a newer turn or dropped when no turn is active even though the item events use the original eventTurnId. emitClaudeTaskPlanUpdated has the same problem for late TaskCreate/TaskUpdate/TaskList results. Pass eventTurnId through both paths so recovered output and plan updates remain attributed to the finalized tool's turn.

      ✅ Resolved in bd84c281b16133c9032fc048e385921d4340f1a5

    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR modifies runtime event handling for tool results across server, web, and mobile, introducing new state tracking for late tool completions. While well-tested, the coordinated behavioral changes across multiple surfaces warrant human verification.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. Passed the finalized eventTurnId through both content.delta and emitClaudeTaskPlanUpdated. Recovered command output and task plan updates stay on the original turn after finalize.

    6. Sorry, I'm unable to act on this request because you do not have permissions within this repository.

    7. This is fixed in bd84c281. Late content.delta and emitClaudeTaskPlanUpdated both take the finalized tool's eventTurnId, so recovered command output and TaskCreate/TaskUpdate/TaskList plan updates stay on the original turn after finalize.

    8. Sorry, I'm unable to act on this request because you do not have permissions within this repository.

    9. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 07f8c1412eaafcc544a8544c513069af8a618ce3. Configure [here](https://www.cursor.com/dashboard/bugbot).
    10. Reproduced the race deterministically against stable 0.0.40 artifacts: completeTurn force-completes entries still in inFlightTools and clears the map, so a tool_result arriving afterwards hits the no-entry branch and is dropped with no event.

      A local reference implementation converged on nearly the same shape as this PR, with one difference worth noting for the design: instead of retaining full entries, it stashes only the tool identity plus its original turn in a small bounded map (cap 20, oldest evicted) and emits a one-shot corrective item.updated carrying the real data.result pinned to the original turn. Results for tools that were never tracked are still dropped exactly as before.

      We have an offline lifecycle simulation covering force-complete → late result → one-shot correction vs. never-tracked drop — happy to share cases if useful for review.

    11. Note: GPT-6 on behalf of shivam (@shivamhwp).

      The new toolRowIndex in both clients is keyed only by toolCallId and merges a match before checking the turn. If two turns reuse an ID, the second tool replaces the first row and inherits its timestamp. Both exact client files at 16ba7a6e collapse those two calls into one.

      Keep the lookup scoped by turn and tool ID. On current main, extend the existing toolLifecycleRowIndex to accept a matching completed-to-updated correction instead of adding a second unscoped map. Preserve the original timestamp for that late correction. Mobile's existing stable row ID can remain while its displayed command output updates.

      The rebase also needs to retain ingestion's current top-level tool ID and presentation metadata when allowing terminal updates to persist their full result. The older turn-attribution and duplicate-completion findings are addressed in this head.

    12. Note

      This comment is posted by Julius' dot

      Closing for missing verification results. The PR lists added coverage but gives no executed checks or observed results for the current adapter, persistence, and web/mobile row changes. The separate reference implementation described in the discussion does not verify this branch. Run the focused tests and report that a late result persists on its original turn and updates one existing work log row without reordering it, then request reconsideration.

    13. juliusmarminge closed this

Sign in to join this conversation.