fix(server): tool calls cut off by a lost provider stream no longer wait forever #16749
main fromrefs/pull/16749/head·opened View on GitHubProblem#
When a run's provider event stream is lost mid tool call, the run is marked failed but its open tool call stays running. t3_worktree_handoff does this every time, because it detaches the session that is running it. Because the run failed, later runs on the same provider thread inherit the item as background work, so the thread shows "Waiting on background task …" after every later run until a server restart. Because the item counts as pending work, the sidebar keeps the thread at Waiting and auto-settle skips it. Fixes #15993.
Change#
RunExecutionService already tracks the ids of open background items to decide when to stop ingesting. It now keeps each item's latest update as well. When the provider's own stream fails, as when its session is released, the lost-stream fallback cancels the run's own open command_execution and dynamic_tool items in the same write that records the run as failed. This matches what restart recovery does when a provider process is gone. The cancellation is built from the adapter's item, so it gets the same unserved image stripping the ingestor applies to stored updates. A failure to store an event goes through the same fallback, but there the provider may still be running the tool, so its items are left as before. Subagent rows already end through the existing cascade.
Terminals reported by the provider are unchanged. Their process is still alive, so open items still pass to the next run through selectInheritedBackgroundTurnItems.
The cleanup is in run finalization rather than in a change to session shutdown order, because a crashed provider can't emit its own cancellations.
The fake adapter that was inline in BackgroundWorkStop.integration.test.ts moved into a scriptedAdapter helper, so the new integration test reuses it instead of copying it.
Scope and approval#
- Triaged bug issue: #15993 (triage). This takes the first direction listed there, limited to runs whose provider stream was lost, so inheritance still works for live processes.
- Not covered, and still cleared by restart recovery as today:
- A stream lost after the run's terminal, while it is still waiting on background items.
- Items inherited from earlier runs when a later run's stream is lost.
- #15203 finalizes a planned workspace detach as an
interruptedterminal, which bypasses this fallback. If both land, that detached terminal should pass the sameorphanedTurnItems.
Verification#
- New integration test,
BackgroundWorkStop.integration.test.ts› "cancels a tool call cut off when a workspace change detaches its session". It uses the real orchestrator, session manager, and run execution with a scripted adapter that has Claude's single-thread session capabilities. A dynamic tool is running whenProviderSessionManager.detachreleases the session, as a workspace change does.- Before: the item stays
running(expected 'running' to equal 'cancelled'). This matches the persisted state of the affected thread: the handoff call from a failed run was stillrunningafter 63 later runs. - After: the item is
cancelled, andderivePendingBackgroundWorkreturns nothing.
- Before: the item stays
- New unit tests in
RunExecutionService.test.ts:- A lost stream cancels the open tool call and leaves a finished one alone. This fails before the change.
- A provider-reported
interruptedterminal still leaves an open command for the next run. - A failure to store an event, while the provider stream stays up, leaves the open tool call for the next run.
- A cancelled tool call keeps only the image metadata of an unserved image, as normal ingestion stores it.
vp test runonRunExecutionService,BackgroundWorkStop.integration,ProviderSessionManager,ProviderTurnStartService,ProviderFailure,SelectionRestart.integration,SteeringCompletion.integration, andtestkit/OrchestratorReplayFixtures.integration: 290 passed.tsc --noEmitforapps/server: no errors.vp lintandvp fmt --checkon the changed files: clean.- Not checked: a live Claude session in a real client. The server logs from the original incident had rotated, so the diagnosis came from the thread's persisted projection.
Fixed by Claude Opus 5.5 in Claude Code, running inside T3 Code.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
coderabbitai[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge added labelsmacroscope-review
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved at
2f44ec9Macroscope's review found this PR approvable — This is a scoped server bug fix that cancels only run-owned tool and command items when their provider stream is actually lost, while preserving existing background-work behavior for live streams. The production change is localized and supported by targeted unit and integration coverage, with no schema, default, deployment, or static-analysis configuration impact.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Note
Written by
gpt-6.1-solon behalf of MariaThanks for tracing the tool calls left running after their provider stream is lost. Right now we are only taking focused bug fixes from outside contributors, and our limited review capacity means we are declining unvouched submissions over 500 changed lines. This PR changes 555 lines across run finalization and integration test restructuring. Please keep the reproduction documented in issue #15993 and request reconsideration with a smaller fix or an agreed maintainer takeover.
maria-rcks closed this
Sign in to join this conversation.
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
ceae154a-6617-479c-8033-6d3cb0153e10📥 Commits
Reviewing files that changed from the base of the PR and between 3cd7adf006db50ebe596b9271c7158d0af1d2247 and 2f44ec9cff6372bd1e1bf03f69ef194319bb0d16.
📒 Files selected for processing (3)
apps/server/src/orchestration-v2/BackgroundWorkStop.integration.test.tsapps/server/src/orchestration-v2/RunExecutionService.test.tsapps/server/src/orchestration-v2/RunExecutionService.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
RunExecutionService now tracks active background-item snapshots. When the provider event stream fails, finalization cancels eligible current-run command and dynamic-tool items. Tests cover stream loss, other termination paths, and Claude session detachment.
Changes#
Background work cleanup
apps/server/src/orchestration-v2/RunExecutionService.tscommand_executionanddynamic_toolitems.apps/server/src/orchestration-v2/BackgroundWorkStop.integration.test.ts,apps/server/src/orchestration-v2/RunExecutionService.test.tsscriptedAdapterhelper.Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix · Severity of issue fixed: Low
Suggested reviewers:
juliusmarmingeMerge Risk
Merge Risk: ⚪ Minimal · up to
2f44eThe change addresses pending tool calls after provider-stream loss, and no actionable merge-blocking risk remains after normal checks.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
2f44eThe change is narrowly scoped to abandoned work and retains existing ownership checks. No expanded tool permissions or new externally exposed interface was identified. Remaining uncertainty concerns interruption timing and completion of provider teardown.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
#15993] requires the orphaned handoff tool item to stop appearing as pending work after its provider session is released. The PR cancels run-owneddynamic_toolandcommand_executionitems w…scriptedAdapterextraction supports the new session-detachment integration test. The added unit tests verify stream-loss cancellation, image sanitization, and the required exception paths. The r…✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.