Skip to content

fix(server): a thread's later turns keep control of the preview tabs it opened - #42219

Open
juliusmarminge wants to merge 2 commits into
mainfrom
factory/issue-42209
Open

juliusmarminge wants to merge 2 commits into
mainfrom
factory/issue-42209

Conversation

@juliusmarminge

@juliusmarminge juliusmarminge commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

When a delegated thread got a follow-up message, it could no longer use or close the preview tab it had opened earlier. Evaluate, navigate and close failed with agentMismatch, so agents opened a second tab and the first stayed open until the idle sweep. Tabs were owned by the MCP credential of the session that opened them, and a thread gets a new credential whenever its provider session is released. A thread only ever holds one credential at a time, so a tab's owner is now the thread (environment and thread id). Other threads still cannot reach the tab, and tabs a person opened or controls stay protected.

Fixes #42209

Reproduction on main

Commit ae45e00 (origin/main). Fresh dev server (T3CODE_DEV_SEED=0 vp run dev), scratch git project added with t3 project add, provider claudeAgent / claude-haiku-5-5, server-side preview browser (Playwright Chromium). 1) t3 thread start on a parent thread that called delegate_task (claudeAgent, claude-haiku-5-5) with a task to preview_open({url:'https://example.com/?owner-repro=1', open:false, reuseExistingTab:false}) then preview_evaluate('navigator.userAgent') and end its turn. Both succeeded; preview_open returned control {owner:'agent', ownedByCaller:true, generation:0}. 2) t3 thread send --wait to the child thread asking for preview_evaluate('location.href'), preview_navigate to ?owner-repro=2 and preview_status on the same tabId. preview_evaluate was rejected with 'This browser tab belongs to another agent session or a human. Open your own tab.'; the agent then skipped navigate because the tab was no longer its own; preview_status returned url ?owner-repro=1 and control {owner:'agent', ownedByCaller:false, generation:0}. 3) A second follow-up asking for t3_preview_close on that tab returned PreviewAutomationControlInterruptedError reason agentMismatch ('This tab belongs to another agent session. You can read it, but open your own tab to act.'). The server trace showed McpSessionRegistry.revokeProviderSession, then revokeThread, then issue between turns, as the reporter described. Failing unit test pushed: ServerBrowser.test.ts 'the same thread's next turn can keep using and close the tab it opened' (a tab opened as providerSessionId agent-a, then evaluate/navigate/close as agent-a-next-turn on the same thread) fails on main with agentMismatch.

Evidence

Create a parent and delegated child; the child opens a preview tab and finishes. Send a follow-up to that child and evaluate, navigate, inspect ownership, and close the original tab. Real web client and server-side Chromium; fresh synthetic state, 1280×800, light theme; deterministic ACP agent invokes the real MCP tools.

Before After
ae45e00923 b513929723
Evaluate, navigate and close rejected; ownedByCaller:false Evaluate, navigate and close succeed; ownedByCaller:true
Before: follow-up rejected After: follow-up succeeds
Before recording After recording

Scenario and deterministic agent driver. scenarios:prove exited 0: failsOnBase=true, passesOnHead=true.

Not shown:

  • The original Claude Haiku adapter was not exercised; the deterministic ACP agent reproduced the ownership failure through real delegation, session release, follow-up and preview MCP calls.
  • Click operations were not separately exercised; this comparison covered evaluate, navigate, status and close.

Tests

  • vp test run src/preview/ServerBrowser.test.ts src/mcp/PreviewAutomationBroker.test.ts (apps/server): 143 passed
  • vp test run src/preview/ServerBrowser.test.ts -t "next turn can keep using" without the source change: fails with reason agentMismatch, as on main
  • vp run typecheck (apps/server): no errors
  • vp run check:changed: format, lint, knip, graphql, typecheck and tests (3 files, 145 tests) all pass
  • PR fix(server): a thread's later turns keep control of the preview tabs it opened #42219 CI: all 8 required checks passed on b513929

Not in this change

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

juliusmarminge and others added 2 commits November 24, 2026 14:41
…tab (#42209)

Failing repro: a finished delegate's provider session is released, so its
next turn gets a new MCP credential and the tab it opened rejects
evaluate, navigate and close with agentMismatch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…it opened

A preview tab's agent owner was the MCP credential of the session that
opened it. A thread gets a new credential whenever its provider session is
released (a finished delegate, the idle stop), so its next turn was refused
with agentMismatch on its own tabs and could not even close them.

A thread holds only one MCP credential at a time, so the owner is now the
thread (environment and thread id). Tests that stood in for another agent
with a second credential on the same thread now use a separate thread.

Fixes #42209

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:XS 0-9 changed lines (additions + deletions). labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

ℹ️ The exact PR base did not have a successful artifact. Baseline uses the latest successful main measurement shown below.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 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 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅
Codex Live turn messages 1 1 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 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 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅
Claude Live turn messages 1 1 0 (0.0%) 8 ✅

Other scenarios

All 64 scenario measurements match main.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded 10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅
Codex short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn running, cold open HTTP decoded 281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅
Codex long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Codex long turn running, history page HTTP decoded 92.4 KiB 92.4 KiB 0 B (0.0%) 107.4 KiB ✅
Codex long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Codex long turn finished, cold open HTTP decoded 381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, socket cold open WebSocket decoded 381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, work page HTTP decoded 186.2 KiB 186.2 KiB 0 B (0.0%) 200.2 KiB ✅
Codex long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Codex idle thread resume WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell snapshot HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Codex fleet shell subscribe WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Codex fleet shell resume check rows 0 0 0 0 ✅
Codex stale shell reload HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex stale shell reload rows 27 27 0 (0.0%) 27 ✅
Codex stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Codex stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex stale todo list cold open HTTP decoded 72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅
Codex stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Codex stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅
Claude short turn resume WebSocket decoded 10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅
Claude short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn running, cold open HTTP decoded 283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅
Claude long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Claude long turn running, history page HTTP decoded 93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅
Claude long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Claude long turn finished, cold open HTTP decoded 384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, socket cold open WebSocket decoded 384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, work page HTTP decoded 188.1 KiB 188.1 KiB 0 B (0.0%) 200.2 KiB ✅
Claude long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Claude idle thread resume WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell snapshot HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Claude fleet shell subscribe WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Claude fleet shell resume check rows 0 0 0 0 ✅
Claude stale shell reload HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude stale shell reload rows 27 27 0 (0.0%) 27 ✅
Claude stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Claude stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude stale todo list cold open HTTP decoded 73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅
Claude stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Claude stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅

Baseline: f8d0951 · PR result: b513929 · Source CI: success

Scenario 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.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The broker now identifies preview tab ownership by environment and thread, while host assignment remains scoped to provider session and thread. Tests cover same-thread access across turns, different-thread ownership, and capacity and reclamation across threads.

Changes: Preview tab ownership across agent turns

Layer / File(s) Summary
Derive tab ownership from thread identity
apps/server/src/mcp/PreviewAutomationBroker.ts
The broker derives agentSessionId from the environment and thread IDs. The host assignment key continues to include the provider session ID.
Verify ownership across agent turns
apps/server/src/preview/ServerBrowser.test.ts
Tests cover reuse and closure of a tab by a later turn in the same thread, rejection of actions from a different thread, and ownership reporting for tabs opened in the same thread.
Track capacity and reclamation across threads
apps/server/src/preview/ServerBrowser.test.ts
Capacity and idle-tab reclamation tests gather sessions across tracked threads and attach viewers using each session’s thread ID.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • pingdotgg/t3code#16956: Defines preview tab access rules around ownership, including ownership across provider sessions.
  • pingdotgg/t3code#26258: Copies an opener tab’s recorded automationOwner to a newly opened preview tab.

Suggested labels: bugfix

Suggested reviewers: macroscopeapp

Merge Risk: Minimal · up to b5139

This change lets a thread keep using and closing its own preview tabs across later turns, while other threads stay blocked from them. No merge-blocking risk was identified.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Linked issue #42209 requires that a delegated child thread keep using and closing its preview tab after a follow-up message. The root cause is the owner key: the provider session ID changes between tu… Provide a readable diff of apps/server/src/mcp/PreviewAutomationBroker.ts (and SessionControl.ts or ServerBrowser.ts if changed), or confirm that the agentSessionId assignment uses tabOwnerKey at the reviewed head. Confirm that the 'nex…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title names the main change: a thread's later turns keep control of the preview tabs it opened. It uses the conventional fix(server) prefix and matches the ownership change in PreviewAutomationBro…
Out of Scope Changes check Passed The AI-generated summary lists two changed files. apps/server/src/mcp/PreviewAutomationBroker.ts changes the tab-owner identity from the provider session to the thread. The change adds tabOwnerKey a…
Description check Passed The description covers the required sections. The Problem section explains that a delegated thread lost the ability to evaluate, navigate, and close its preview tab after a follow-up message, because …
Full details: Title check

Explanation

The title names the main change: a thread's later turns keep control of the preview tabs it opened. It uses the conventional fix(server) prefix and matches the ownership change in PreviewAutomationBroker and ServerBrowser tests. It is specific and not vague.

Full details: Out of Scope Changes check

Explanation

The AI-generated summary lists two changed files. apps/server/src/mcp/PreviewAutomationBroker.ts changes the tab-owner identity from the provider session to the thread. The change adds tabOwnerKey and keeps hostAssignmentKey scoped by provider session and thread, so host routing does not change. apps/server/src/preview/ServerBrowser.test.ts updates ownership, capacity, and idle-reclamation tests to use separate threads and to cover same-thread reuse. Both changes relate directly to issue #42209. The PR does not change the tabLimit or agentMismatch message text, and it states this scope limit. The PR does not include unrelated work, based on the supplied summary. The full diff could not be read, so this verdict depends on the summary being complete.

Full details: Description check

Explanation

The description covers the required sections. The Problem section explains that a delegated thread lost the ability to evaluate, navigate, and close its preview tab after a follow-up message, because tab ownership was tied to a provider-session credential that gets replaced. The Change section states that ownership moves to the thread (environment and thread ID) and that other threads and human-controlled tabs stay protected. The Scope section links issue #42209 as the bug. The Verification section gives specific test commands with results, a before/after reproduction on main, screenshots, recordings, and a list of what was not checked. It also names the models and harnesses used, as the template asks. Minor gaps: the Change section does not explain why the web client or other components are affected, and the Problem section is longer than the template's one-or-two-sentence guidance. The description also contains some internal material (factory run notes, scope notes on related PRs) that is not in the template. These gaps are non-critical, so the check passes.

Full details: Linked Issues check

Explanation

Linked issue #42209 requires that a delegated child thread keep using and closing its preview tab after a follow-up message. The root cause is the owner key: the provider session ID changes between turns. The reviewed head defines tabOwnerKey in apps/server/src/mcp/PreviewAutomationBroker.ts as ${environmentId}\u0000${threadId}, and its comment states that a thread holds one credential at a time. This supports the fix. The diff could not be read because the changed blobs are missing from the checkout. The head file read was truncated before the place where agentSessionId is built, so I could not confirm that tabOwnerKey replaces the provider-session key there. I also did not inspect SessionControl.agentMayAct or ServerBrowser.ts. The PR reports that the new ServerBrowser.test.ts case ('the same thread's next turn can keep using and close the tab it opened') passes with the change and fails without it. I did not run these tests. The 'Evidence' section of the PR description is still marked as pending, so no before and after proof exists in the PR.

Resolution

Provide a readable diff of apps/server/src/mcp/PreviewAutomationBroker.ts (and SessionControl.ts or ServerBrowser.ts if changed), or confirm that the agentSessionId assignment uses tabOwnerKey at the reviewed head. Confirm that the 'next turn can keep using' test passes at head. The reviewer may then assess PASS. Also note that the hostAssignmentKey comment still describes a provider-session key; that comment should be checked for accuracy.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fbe5033 and b513929.

Files selected for processing: 2

  • apps/server/src/mcp/PreviewAutomationBroker.ts
  • apps/server/src/preview/ServerBrowser.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS 0-9 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A follow-up message to a delegated thread locks it out of the preview tab it opened

1 participant