Skip to content

fix(agent-teams): remint stale leader terminal handles via pane key in tmuxCompat - #11742

Closed
bluehansl wants to merge 8 commits into
stablyai:mainfrom
bluehansl:fix/agent-teams-stale-handle-remint
Closed

bluehansl wants to merge 8 commits into
stablyai:mainfrom
bluehansl:fix/agent-teams-stale-handle-remint

Conversation

@bluehansl

@bluehansl bluehansl commented Jul 31, 2026 •

Copy link
Copy Markdown

Summary

Fixes #11739.

  • Claude Code agent-teams becomes entirely unusable in long-lived orca claude-teams sessions: the team snapshots the leader terminal handle at launch into server-side team state, and once that handle is invalidated (handles are remintable — see the Validate ORCA_TERMINAL_HANDLE and remint stale orchestration terminals via pane key #7514 rationale), every shim call that touches the leader pane fails with terminal_handle_stale. Teammate spawning is the first casualty, and since the first Agent spawn is what materializes the team, the whole feature is lost until the session is restarted.
  • Apply the same validate-then-remint treatment that PR Validate ORCA_TERMINAL_HANDLE and remint stale orchestration terminals via pane key #7514 gave the general orchestration path, on the team side: keep the leader's remint-stable pane key in team pane state (agentTeams.prepareLaunch already receives it and then discarded it), and when an operation fails with terminal_handle_stale/terminal_gone, re-resolve the current handle via getTerminalHandleForPaneKey and retry once, persisting the fresh handle in team state (pane.handle, and team.leaderHandle for the leader).
  • The recovery lives in a dedicated withFreshPaneHandle wrapper (claude-agent-teams-pane-handle-remint.ts) applied to split/send/read/focus paths in the tmux dispatcher. The leader pane key is threaded through all three launch paths that already know it (agentTeams.prepareLaunch, the captured native Agent Teams env refresh for renderer PTY spawns, and the adopted-pane-identity headless path); panes without a pane key (teammate panes, and headless launches whose random identity may be replaced by the canonical surface after spawn) and callers without the resolver keep the exact previous behavior — the API addition is optional, so no other AgentTeamsTerminalApi consumer changes.

Screenshots

No visual change.

Testing

  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed
    • claude-agent-teams-service.test.ts: stale leader handle is reminted via pane key and the split retried; the fresh handle is persisted for later operations; concurrent operations racing on the same stale handle both recover; stale errors still propagate when no pane key is known or when the pane key resolves to the same attempted handle; send-keys recovery covered too.
  • vitest run src/main/runtime/claude-agent-teams-service.test.ts — 12/12 passed (includes a concurrent same-stale-handle race regression test)
  • vitest run src/main/ipc/pty.test.ts — 401/401 passed (prepareClaudeAgentTeamsLeaderForHandle consumers updated for the pane key)
  • oxlint / oxfmt --check clean on all changed files
  • pnpm exec tsc --noEmit -p config/tsconfig.node.json — passes

Reproduced live before the fix: in an aged claude-teams session, tmux split-window -h through the shim fails with terminal_handle_stale even while ORCA_TERMINAL_HANDLE is verifiably live — full trace and CLI-source analysis in #11739.

…n tmuxCompat

Teams snapshot the leader terminal handle at launch, but handles are
remintable while teams are long-lived — once the stored handle goes stale,
every teammate operation (starting with the pane split that materializes
the team) fails with terminal_handle_stale and Claude Code agent-teams
becomes unusable for the rest of the session.

Apply the same validate-then-remint treatment PR stablyai#7514 gave the general
orchestration path: keep the leader's remint-stable pane key in team state,
and on a stale-handle failure re-resolve the current handle through the
pane key and retry once, persisting the fresh handle.

Fixes stablyai#11739

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 80ef46ac-3318-4aeb-b240-5abcee40bab9

📥 Commits

Reviewing files that changed from the base of the PR and between e2f326c and 8c4fe77.

📒 Files selected for processing (11)
  • src/main/ipc/pty-spawn-runtime-handle-binding.test.ts
  • src/main/ipc/pty/ipc/spawn-env.ts
  • src/main/runtime/claude-agent-teams-pane-handle-remint.ts
  • src/main/runtime/claude-agent-teams-pane-resolution.ts
  • src/main/runtime/claude-agent-teams-service.test.ts
  • src/main/runtime/claude-agent-teams-service.ts
  • src/main/runtime/claude-agent-teams-tmux-dispatcher.ts
  • src/main/runtime/claude-agent-teams-types.ts
  • src/main/runtime/orca-runtime-create-terminal.ts
  • src/main/runtime/orca-runtime-resolve-terminal-split-source-authority.ts
  • src/renderer/src/store/slices/runtime-status-recheck.ts
🚧 Files skipped from review as they are similar to previous changes (7)
  • src/main/runtime/claude-agent-teams-pane-resolution.ts
  • src/main/ipc/pty-spawn-runtime-handle-binding.test.ts
  • src/main/runtime/claude-agent-teams-service.test.ts
  • src/main/runtime/claude-agent-teams-pane-handle-remint.ts
  • src/main/runtime/claude-agent-teams-service.ts
  • src/main/runtime/claude-agent-teams-tmux-dispatcher.ts
  • src/main/runtime/claude-agent-teams-types.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The change adds stable pane keys and terminal-handle resolution to Claude Agent Teams. Stale or gone handles are resolved and retried once across terminal operations. Shared pane and window resolution utilities replace dispatcher-local logic. Launch and environment preparation now propagate pane keys. Tests cover reminting, persistence, concurrency, unchanged-handle resolution, unavailable resolution, and retry failures.

Merge Risk: 🔵 Low · up to 8c4fe

The change restores long-lived team sessions by reminting stale terminal handles and retrying operations once. A bounded merge-readiness risk remains because edge-case pane resolution could direct input, output, focus, or split operations to the wrong terminal; the PR is mergeable with explicit owner awareness and follow-up to enforce exact pane identity.

🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (3 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug, solution, linked issue, testing, and lack of visual changes, but it omits several required template sections and does not use the required Visual Proof wording. Add the missing ELI5, What Changed, Why, Linked Issue, Visual Proof, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist sections. Use N/A with a brief reason under Visual Proof, and complete or explicitly mark each…
Out of Scope Changes check ⚠️ Warning Most changes support issue #11739, but the formatting-only change in src/renderer/src/store/slices/runtime-status-recheck.ts is unrelated to stale leader handle recovery. Revert the unrelated formatting-only change, or document a specific repository-level reason for including it.
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 12 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: reminting stale Claude Agent Teams leader terminal handles through pane keys.
Linked Issues check ✅ Passed The changes satisfy issue #11739 by storing the leader pane key, resolving fresh handles after terminal_handle_stale or terminal_gone, retrying operations once, and persisting refreshed handles. T…
Full details: Description check

Resolution

Add the missing ELI5, What Changed, Why, Linked Issue, Visual Proof, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist sections. Use N/A with a brief reason under Visual Proof, and complete or explicitly mark each applicable checklist item.

Full details: Linked Issues check

Explanation

The changes satisfy issue #11739 by storing the leader pane key, resolving fresh handles after terminal_handle_stale or terminal_gone, retrying operations once, and persisting refreshed handles. Tests cover the required recovery and failure cases.


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.

@greptile-apps

greptile-apps Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Adds stale terminal handle recovery for agent teams.

The PR appears safe to merge based on the reviewed changes.

Summary

The PR retains the leader’s pane key and uses it to retry team operations after a terminal handle becomes stale. The latest changes also forward the required input classification for send-keys. The previously reported same-handle retry gap is fixed.

Reviews (5) · Last reviewed commit: "Merge upstream/main (31012aeb09) to reso..."

…r launch paths

Review follow-ups: compare the remint result against the handle actually
attempted (not the shared mutable pane.handle) so the loser of a concurrent
stale race still retries; pass the already-known leader pane key through the
captured native Agent Teams env refresh (renderer PTY spawns) and the adopted
pane identity headless path, so those leaders can recover too.

Co-Authored-By: Claude Fable 5 <[email protected]>
…eals epoch staleness

Review follow-up (greptile): resolving by pane key re-registers the attempted
handle against the current renderer epoch as a side effect, so epoch-only
staleness is already healed for the retry — only skip the retry when the
resolver returns no handle at all.

Co-Authored-By: Claude Fable 5 <[email protected]>
@bluehansl

Copy link
Copy Markdown
Author

@OrcaWin gentle ping — you are the requested reviewer on this one. @AmethystLiang tagging you as well, since this PR implements the fix for #11739 (P1, assigned to you) and addresses the same staleness family you have been investigating in #11191.

Current status: all bot feedback is addressed — greptile's concurrent-remint race and the epoch-refresh same-handle retry case are both fixed with regression tests. Targeted tests pass (claude-agent-teams-service 14/14, ipc/pty 401/401), and changed files are clean on oxlint / oxfmt / tsc -p config/tsconfig.node.json.

Happy to adjust scope or approach if the team prefers a different direction for the agentTeams.tmuxCompat path.

- Re-apply the pane-key remint wiring over three weeks of upstream drift:
  tmux dispatcher call sites, runtime resolver injection, leader paneKey
  plumbing in pty.ts / orca-runtime.ts
- Adopt upstream's pty.test.ts split (stablyai#14834); paneKey expectations now
  live in pty-spawn-runtime-handle-binding.test.ts
- Extract resolvePane/resolvePaneOrWindow into
  claude-agent-teams-pane-resolution.ts to keep the dispatcher under the
  max-lines budget

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gHoNRQpffLLRjf6aSDDWc
@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@bluehansl

Copy link
Copy Markdown
Author

Synced with current main (59892a2) — the three weeks of drift are merged and the PR should be mergeable again.

What the sync involved

  • Re-applied the pane-key remint wiring over the drift: tmux dispatcher call sites, runtime resolver injection, and the leader paneKey plumbing in pty.ts / orca-runtime.ts.
  • Adopted the upstream pty.test.ts split (refactor(daemon,runtime): split daemon, pty and rpc modules under the max-lines budget #14834); the paneKey expectations now live in pty-spawn-runtime-handle-binding.test.ts.
  • Extracted resolvePane/resolvePaneOrWindow into claude-agent-teams-pane-resolution.ts to keep the dispatcher under the max-lines budget.

Verification on the merged tree

  • claude-agent-teams-service.test.ts 17/17, pty-spawn-runtime-handle-binding.test.ts 14/14.
  • Full src/main/ipc and src/main/runtime suites green except three failures that reproduce identically on a pristine checkout of main @ 59892a2 in the same local environment (macOS): pty-pane-claim-arbitration.test.ts ("shuts down a split PTY…") and quarter-circle-title-send-authorization.test.ts (×2) — pre-existing on main, unrelated to this PR.
  • oxlint chain and tsc --noEmit -p config/tsconfig.node.json clean.

Relation to #14590 (“make close and handles incarnation-stable”, merged Aug 15): it stabilizes handle incarnation on the close/reload paths (and resolved #14524), but it does not touch the agent-teams tmuxCompat path — the team still resolves teammate panes via the leader-handle snapshot taken at team creation, so #11739 still reproduces whenever the leader handle changes (runtime restart, epoch refresh). This PR remains the fix for that path.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 036ef32f-31b0-4b67-a1d3-3565c6f6bc19

📥 Commits

Reviewing files that changed from the base of the PR and between 59892a2 and bbedd4d.

📒 Files selected for processing (9)
  • src/main/ipc/pty-spawn-runtime-handle-binding.test.ts
  • src/main/ipc/pty.ts
  • src/main/runtime/claude-agent-teams-pane-handle-remint.ts
  • src/main/runtime/claude-agent-teams-pane-resolution.ts
  • src/main/runtime/claude-agent-teams-service.test.ts
  • src/main/runtime/claude-agent-teams-service.ts
  • src/main/runtime/claude-agent-teams-tmux-dispatcher.ts
  • src/main/runtime/claude-agent-teams-types.ts
  • src/main/runtime/orca-runtime.ts
🚧 Files skipped from review as they are similar to previous changes (7)
  • src/main/ipc/pty.ts
  • src/main/runtime/claude-agent-teams-service.ts
  • src/main/runtime/claude-agent-teams-types.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/runtime/claude-agent-teams-service.test.ts
  • src/main/runtime/claude-agent-teams-pane-handle-remint.ts
  • src/main/runtime/claude-agent-teams-tmux-dispatcher.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Upstream split the monolithic PTY IPC module (stablyai#15172); the leader-prepare
call site moved from pty.ts to pty/ipc/spawn-env.ts, so the paneKey
pass-through is re-applied there (ctx.stablePaneKey). Everything else
auto-merged.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gHoNRQpffLLRjf6aSDDWc
@bluehansl

bluehansl commented Aug 25, 2026 •

Copy link
Copy Markdown
Author

Re-synced with main @ 09048c6. The only conflict came from the PTY IPC module split (#15172): the leader-prepare call site moved from pty.ts to pty/ipc/spawn-env.ts, so the one-line paneKey pass-through now lives there (ctx.stablePaneKey). Everything else auto-merged; the tree differs from main by exactly this PR's files. Verified locally: typecheck clean, claude-agent-teams suites green, changed-file lint clean.

For clarity, since this push landed right after the review thread on claude-agent-teams-pane-resolution.ts was resolved: the two are unrelated. That finding was withdrawn as pre-existing behavior and no code changed in response to it — the new commit is purely the upstream merge, carrying the same one-line pass-through to the file it moved to.

@bluehansl

Copy link
Copy Markdown
Author

@OrcaWin Gentle follow-up on this one. Since the first ping on Aug 7 the PR has been kept mergeable through two upstream re-syncs (currently on main @ 09048c6), and the only bot review thread was withdrawn and resolved by CodeRabbit as pre-existing behavior. #11739 still reproduces whenever the leader handle changes, so agent-teams sessions remain exposed until this lands. If there is anything you'd like changed — or a rough idea of when it could get a look — I'd appreciate it.

Upstream split OrcaRuntimeService into modules (stablyai#17605); the two call
sites this PR touches moved out of orca-runtime.ts, so the same lines are
re-applied in their new homes: the leaderPaneKey pass-through in
orca-runtime-create-terminal.ts and the resolver injection plus paneKey
plumbing in orca-runtime-resolve-terminal-split-source-authority.ts.
Everything else auto-merged.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gHoNRQpffLLRjf6aSDDWc
@bluehansl

Copy link
Copy Markdown
Author

Re-synced with main @ e2f326c. The only conflict came from the OrcaRuntimeService module split (#17605): the two call sites this PR touches moved out of orca-runtime.ts, so the same lines now live in their new homes — the leaderPaneKey pass-through in orca-runtime-create-terminal.ts, and the resolver injection plus paneKey plumbing in orca-runtime-resolve-terminal-split-source-authority.ts. Everything else auto-merged; the tree still differs from main by exactly this PR's files. Verified locally: typecheck clean, claude-agent-teams suites green, changed-file lint clean.

@coderabbitai

coderabbitai Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@bluehansl

Copy link
Copy Markdown
Author

@Jinwoo-H @AmethystLiang — sorry for the direct ping; reaching out to you two specifically because you worked on this exact area recently (#14590 / #14524, the incarnation-stable handle work).

This PR is the missing counterpart on the agent-teams side. The tmuxCompat path still resolves teammate panes via the leader-handle snapshot taken at team creation, so terminal_handle_stale still breaks team spawn whenever the leader handle changes — the survival mechanisms from #14590 deliberately exclude renderer leaf panes, which is what team panes are. The fix re-resolves through the stable pane key instead of guessing from a stale handle, consistent with the fail-closed design (full analysis in #11739).

It's a small review in practice: the logic is one 40-line wrapper (claude-agent-teams-pane-handle-remint.ts); everything else is call-site wiring, paneKey plumbing, and tests. The review request has been open since early August, and I've kept the branch mergeable through three upstream re-syncs. If either of you could take a look — or route it to whoever owns this area now — I'd really appreciate it.

The only conflict was incidental: the previous merge's pre-commit oxfmt
pass had reformatted runtime-status-recheck.ts, which upstream has since
deleted — the deletion is accepted. All PR changes carried over cleanly.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gHoNRQpffLLRjf6aSDDWc
@bluehansl

Copy link
Copy Markdown
Author

Re-synced with main @ 20794ee (fourth time). No real conflict this round — the only collision was an incidental formatting change the previous merge's pre-commit hook had left on a file upstream has since deleted; the deletion is accepted and the tree again differs from main by exactly this PR's files. Typecheck and the claude-agent-teams suites pass locally.

Two real hunks, both combining upstream features with the remint wiring:
send-keys keeps the withFreshPaneHandle wrapper and adopts upstream's
inputKind 'driving' option; prepareClaudeAgentTeamsLeader forwards both
paneKey and upstream's new prepareAuth.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016gHoNRQpffLLRjf6aSDDWc
@bluehansl

Copy link
Copy Markdown
Author

Re-synced with main @ 31012ae (fifth time). Both conflicts were genuine three-way merges this round, and both resolve by combining upstream's new behavior with this PR's wiring: send-keys keeps the withFreshPaneHandle wrapper while adopting the new inputKind: 'driving' option (#22989), and prepareClaudeAgentTeamsLeader forwards both paneKey and the new prepareAuth (#21356). Typecheck and the claude-agent-teams suites pass; the tree still differs from main by exactly this PR's files.

@nwparker

Copy link
Copy Markdown
Contributor

Thank you for this, and for tracing the asymmetry with #7514. The main trigger was fixed separately in #14590 (handles now survive renderer reloads), and this branch now conflicts heavily with the refactored runtime. Closing in favor of that fix.

@nwparker nwparker closed this Oct 25, 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

4 participants