fix(server): background subagents keep T3 tools after the parent turn ends #19751
main fromrefs/pull/19751/head·merged by maria-rcks View on GitHubReplaces #19237, moved from a fork head to this repo.
Problem#
A Claude background subagent (Agent tool with run_in_background: true) keeps running after the parent turn ends, and it calls T3 tools with the parent thread's MCP credential. Once the parent run settles, assertLiveCaller sees activeRunId === null and every tool that acts as the caller (html_render, preview_open, preview_click, t3_thread_send, ...) fails with parent_not_active: The calling provider no longer owns an active thread run. Agents then fall back to other browsers or hold the parent turn open with blocking waits.
Seen on 0.0.46-nightly.20261013.3042: 10 preview tool failures from 7 Claude background subagents, and one t3_thread_send from a Codex native subagent 6 s after its parent run completed. Each came after the parent run had completed and before the next run started.
Related but separate: #17096 (credential slot left on another provider instance) and #17885 (wake turns stuck behind a held queue).
Change#
A thread caller is also live while its shell lists a running background subagent owned by the caller's provider instance (pendingBackgroundTasks with kind: "subagent" and a matching providerInstanceId). Ownership comes from the item's provider thread, including child provider threads, so background work from an earlier provider cannot keep a new provider's credential live. This list is the one that drives the sidebar's Waiting state, and it is already gated on the latest run having settled. The same check backs the cross-thread/cross-project check in OrchestratorMcpService. When the projection has no active run, it decides from one fresh shell snapshot, accepting a matching run that started in between.
Everything else still applies: archived threads, a different provider instance, and credentials revoked when the provider process goes away are all refused. After Stop ends the background work, the thread has no running subagent, so calls are refused again. delegate_task, create_threads and request_secret still need an active run, because they attach to its node.
Verification#
Same prompt on a dev server before and after: a Claude parent starts a background subagent, then ends its turn. The subagent runs ping -c 30 and then calls html_render.
- Before:
html_renderfails withparent_not_active33 s after the parent run completed. - After:
html_renderreturnshtmlRender33 s after the parent run completed, with no run active.


McpToolAccess.test.ts adds rows to the access table. A caller whose run ended but whose subagent still runs can act and write (ran). It is refused (parent_not_active) when the subagent belongs to an earlier provider, when the caller's provider instance differs, when the thread is archived, and when the subagent has no known owner. The existing "ended" rows still return parent_not_active.
Not covered: a subagent that finished and is then resumed by its own background task. T3 marks it completed and does not see it running again until the next wake run starts, so its calls in that gap still fail. That fix belongs in the Claude adapter's wake handling.
Re-verified on 30869a2#
A server built from 30869a2, with Claude Sonnet 5.5. Times are UTC.
- Claude: the parent run completed at 04:05:17.788. The background subagent's
html_renderran at 04:05:24.219 and returnedhtmlRender. The wake run was requested at 04:05:29.008, so no run was active during the call.ping -c 30failed at once in this sandbox (nocap_net_raw), so the gap is 6 s instead of 30 s. - Refused once the subagent is not running: a Claude subagent ended its own turn (its item completed at 04:07:29.94) while its background
sleepran. When the sleep finished it resumed and calledhtml_renderbetween 04:12:25 and 04:12:32, and gotparent_not_active. This is the "Not covered" case below. Pressing Stop on a running subagent ends it, so no call follows the Stop.


Codex native subagent, main vs 08a7a19#
The 30869a2 Codex check called t3_thread_list, which is a read tool and skips the live-caller check, so it did not test this fix. This run uses html_render, which acts as the caller. Same prompt on both servers with Codex GPT-6-Astra: the parent spawns a native subagent and ends its turn, and the subagent runs sleep 40 and then calls html_render. Times are UTC, read from statev2.sqlite, and each thread had only its one completed run.
- main (
86534db): the parent run completed at 07:20:49.363. The subagent'shtml_renderat 07:21:59.454 failed withparent_not_active. - This PR (
08a7a19): the parent run completed at 07:20:34.023. The subagent'shtml_renderat 07:21:24.309 returnedhtmlRender.


Written by Claude Opus 5.5 (claude-opus-5-5) in Claude Code.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge added labelsmacroscope-review
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 4.9 KiB 5.0 KiB +23 B (+0.5%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB +23 B (+1.9%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 20.8 KiB 20.9 KiB +41 B (+0.2%) 29.3 KiB ✅ Codex Live turn messages 1 2 +1 (+100.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB +7 B (+0.1%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB +7 B (+0.6%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.2 KiB 21.2 KiB 0 B (0.0%) 29.3 KiB ✅ Claude Live turn messages 2 2 0 (0.0%) 8 ✅ Baseline:
86534db· PR result:08a7a19· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 108.5 KiB
- Claude decoded thread snapshot: 108.8 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Advanced
- Run ID:
231b153b-9114-4c21-83e0-6288182c65c8
📥 Commits
-
Reviewing files that changed from the base of the PR and between 30869a252713d793f90b1f11c1e1022f0629bac6 and 08a7a19bbd53f3bc6a030c9955fbd8ae6fba57e6.
📒 Files selected for processing: 9
-
apps/server/src/mcp/McpToolAccess.test.tsapps/server/src/mcp/OrchestratorMcpService.activity.test.tsapps/server/src/mcp/OrchestratorMcpService.tsapps/server/src/mcp/threadAccess.tsapps/server/src/orchestration-v2/ProjectionStore.tspackages/contracts/src/orchestrationV2.test.tspackages/contracts/src/orchestrationV2.tspackages/shared/src/orchestrationV2PendingBackgroundWork.test.tspackages/shared/src/orchestrationV2PendingBackgroundWork.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
-
📝 Walkthrough
-
The change records provider-instance ownership on pending subagent tasks. Projection readers attach that ownership to pending work. MCP caller-liveness checks use ownership to accept matching background tasks and reject mismatched, archived, or otherwise inactive callers.
Changes: Provider-scoped background caller access
Layer / File(s) Summary Provider ownership in pending work
packages/contracts/src/orchestrationV2.ts,packages/contracts/src/orchestrationV2.test.ts,packages/shared/src/orchestrationV2PendingBackgroundWork.ts,packages/shared/src/orchestrationV2PendingBackgroundWork.test.tsThe pending subagent task contract accepts an optional provider instance ID. Derived tasks use the owning turn item or provider thread instance. Tests cover ownership and omission cases. Provider ownership in projections
apps/server/src/orchestration-v2/ProjectionStore.ts,apps/server/src/orchestration-v2/ProjectionStore.test.tsProjection replay tracks provider-thread ownership. SQL and memory shell readers attach that ownership to pending items. Tests cover ownership changes, cancellation, and rollback. MCP caller liveness
apps/server/src/mcp/OrchestratorMcpService.ts,apps/server/src/mcp/threadAccess.ts,apps/server/src/mcp/OrchestratorMcpService.activity.test.ts,apps/server/src/mcp/McpToolAccess.test.tsCaller checks use shell liveness and provider-instance ownership. Tests cover matching and mismatched providers, pending tasks, active runs, archived shells, and unavailable operations. Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Sequence Diagram(s)#
sequenceDiagram participant MCPCaller participant OrchestratorMcpService participant ProjectionStore MCPCaller->>OrchestratorMcpService: Request caller-scoped operation OrchestratorMcpService->>ProjectionStore: Read caller thread shell ProjectionStore-->>OrchestratorMcpService: Shell with pending task provider ownership OrchestratorMcpService->>OrchestratorMcpService: Check provider instance and shell liveness OrchestratorMcpService-->>MCPCaller: Allow or return parent_not_activeSuggested reviewers:
juliusmarminge
Merge Risk: ⚪ Minimal · up to
08a7a-
Background subagents can keep using their tools after the parent turn ends, as long as the same provider instance owns them. Archived threads, other provider instances, and ownerless tasks are still refused. No merge-blocking issue was found.
Security architecture risk: 🔵 Low · up to
08a7a-
Background work intentionally retains tool access after its parent turn ends. The reviewed checks preserve provider ownership and archive restrictions, with no demonstrated new authorization bypass. Cleanup and credential revocation remain important safeguards.
Retained concerns No architecture-level concerns identified.
Security review details
-
Security Blast Radius
- inferred — The expansion is temporal rather than a new credential class: a holder of the parent thread's credential can continue caller-acting and writing operations while matching background subagent work remains pending. Reach can include other permitted threads and projects, not only the subagent's child thread; existing capability and target-mode limits still bound those operations.
Trust Boundaries and Controls
- observed — Background authorization requires an unarchived shell, a shell provider matching the credential, and a subagent task owned by that same provider. Commands, monitors, and generic background tasks do not qualify. Caller wrappers retain read-only restrictions, orchestration capability checks, and target-mode enforcement.
Resilience and Maintainability Implications
- observed — Recovery constructs cancellation events for stale background items and clears provider-thread task rosters. Tests cover latest-owner attribution across app threads, cancellation removing pending work, and rollback excluding work in both projection implementations. These safeguards do not establish every adapter's behavior under lost or partially applied lifecycle events.
Hardening Proposals
- proposed — Validate authorization revocation with fault-injection scenarios spanning terminal-event write failure, concurrent provider replacement, interruption, and restart. Check both pending-work removal and old-credential rejection, since pending-work state now participates in authorization.
-
Pre-merge checks:
3 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Description check The description clearly documents the problem, implementation, verification steps, observed results, limitations, and test coverage. It does not provide the required Scope and approval information, su… Add the Scope and approval section. Link the triaged issue or discussion with explicit maintainer approval, or explain why this focused fix qualifies as an obvious-bug exemption without prior approval. Passed checks: 3 passed-
Check name Status Explanation Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Title check The title is concise, specific, and accurately describes the main change: preserving T3 tool access for background subagents after the parent turn ends.
Full details: Description check
-
Explanation
The description clearly documents the problem, implementation, verification steps, observed results, limitations, and test coverage. It does not provide the required Scope and approval information, such as maintainer approval or an explanation for why this focused fix qualifies for an exemption.
Resolution
Add the Scope and approval section. Link the triaged issue or discussion with explicit maintainer approval, or explain why this focused fix qualifies as an obvious-bug exemption without prior approval.
-
- Fix all pre-merge checks with AI
✨ Finishing Touches
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
maria-rcks merged this11ce406
maria-rcks closed this
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — This PR changes the MCP caller authorization boundary by allowing provider-owned background subagents to continue invoking tools after the parent run ends, with SQL and in-memory projection changes supplying ownership. Because it extends the permitted execution window for credentials, the change warrants focused human review despite its narrow bug-fix scope.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).