Skip to content

fix(terminal): prevent automatic replacement of host-mirrored agents - #29289

Merged
nwparker merged 2 commits into
mainfrom
nwparker/P0-bugs-scream
Oct 16, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/P0-bugs-scream

Conversation

@nwparker

@nwparker nwparker commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​201 $\color{#cf222e}{\Huge{\mathbf{−}}}$​141 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​60
Prod 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​34 $\color{#cf222e}{\Huge{\mathbf{−}}}$​38 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4

ELI5

A paired desktop could restart an agent after another client closed its tab, creating another writer for the same conversation. The desktop now leaves that automatic restart decision to the machine running the agent.

What Changed

The resume sweep declines a replacement launch for a web-terminal-* recovery record. The guard sits immediately before launch, after existing completed-session cleanup, duplicate cleanup and preserved-pane ownership checks. This keeps those behaviors unchanged while preventing an automatic second writer.

The launch guard covers retracted rows, inventory settling before the old row disappears, a split pane whose sibling has the only handle, and an expired handle wait. Stable and legacy pane keys are recognized. Existing native and direct SSH recovery paths remain covered.

Why

A missing mirrored row or expired handle wait cannot distinguish an explicit close, reconnect re-key, pending handle, or actual process exit. Retirement proofs also do not distinguish user close from natural exit. The host's existing automatic-session API explicitly rejects these legacy sleeping records as insufficient authority. Treating the sweep as an explicit user resume would bypass that boundary.

Blocking only the replacement launch is narrower than bypassing all cleanup for mirrored records. Completed history is still cleaned up, and an older mirrored duplicate does not survive when a newer native record recovers the conversation.

Linked Issue

Refs #21235. Addresses the reproduced paired-client retraction path and related mirror ordering cases. The latest iOS host-side live-pane omission is not verified or claimed fixed; #24386 addresses related live-PTY census work. Keep #21235 open until those remaining triggers are verified.

Visual Proof

No live paired-client recording captured. Tests drive production resume/store logic and stream-frame reconciliation, including phone-triggered background wake, with mocked runtime boundaries. Live paired sleep/wake remains a validation gap.

Testing

macOS arm64; ORCA_BACKGROUND_LAUNCH=1 on every test command.

  • 241 tests passed across 24 focused suites: resume, mirror handle gaps, stream ordering, background wake, activation, execution-host scope, direct SSH, and git/folder workspace controls.
  • Added controls for completed mirror cleanup and exactly one native recovery when its older mirrored duplicate is present, for both git and folder workspaces.
  • Updated the two activation tests that failed the previous CI run: hydration/retraction no longer authorizes a replacement agent. Activation may still seed an ordinary shell, without a queued agent resume.
  • pnpm tc:web, pnpm run check:code-quality:changed, formatting, and git diff --check passed.
  • E2E coverage: no e2e spec covers these renderer recovery files, so pr.yml's e2e routing skips them. The changed recovery path is covered by src/renderer/src/lib/retracted-host-pane-resume.test.ts and src/renderer/src/lib/host-mirror-handle-gap-resume.test.ts. Both fail on main's production code (the sweep launches a replacement agent) and pass on this branch.
  • I manually tested these changes locally in paired apps
  • Automated tests added/updated

Review

Merge risk: medium, with a smaller regression surface than the first revision. Completed-session and duplicate cleanup retain their prior behavior. A lone recoverable mirror record survives without launching a replacement.

Remaining behavior tradeoff: even a genuinely exited or deliberately slept mirrored remote agent can require host recovery or an explicit resume. In particular, automatic client recovery of remote worktree-sleep records is suppressed. Restoring it safely requires a host-authorized recovery checkpoint and an atomic ownership claim; no such protocol change is included here. Headless hosts cannot rely on renderer-based wake.

Independent read-only review found no safe existing host exit/close predicate that could authorize a narrower automatic launch. Full PR CI must pass for the updated commit before merge; the previous commit's aggregate failed on the two activation expectations updated here.

Agent skill upstream boundary

  • Not applicable; no upstream skill source copied.

Notes

No wire, persisted-format, platform command, or provider-specific changes. Native host-owned mobile wake retains its existing path. No live Windows/Linux/SSH/paired iOS validation; full lint/test/build are left to CI.

Checklist

  • This PR is small and focused
  • I explained the user-facing before/after, mechanism, and approach
  • Before/after recording attached
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and compatibility impact considered
  • Full lint, typecheck, test, and build pass (CI pending)

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The resume sweep now skips sleeping-session records identified as host-mirrored. Handle-gap documentation states that expiry does not authorize a client-side resume. Tests now expect host-owned records to remain unresumed across handle waits, mirror hydration, and frame-settlement cases. Tests also verify that ordinary terminal records can still be recovered.

Priority: ⬇️ Low

Merge Risk: 🟡 Moderate · up to a4a43

This change stops clients from launching replacement agents for tabs owned by another host. One path remains: when a newer local record shares a host-owned agent session, the client can still resume it, which could create a second writer for that conversation. Confirm the intended ownership rule for that case before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 10 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly and concisely describes the main change: preventing automatic replacement of host-mirrored agents.
Description check Passed The description is detailed and covers the user impact, implementation, rationale, linked issues, testing, risks, and checklist status. It also clearly discloses missing manual paired-app testing, vis…
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@nwparker nwparker changed the title fix(terminal): leave mirrored agent recovery to the execution host fix(terminal): prevent automatic replacement of host-mirrored agents Oct 16, 2026

@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: Advanced
  • Run ID: 5514efe8-9542-4923-bc9e-1e1586c65f0d
📥 Commits

Reviewing files that changed from the base of the PR and between 102b267 and a4a4320.

📒 Files selected for processing: 10
  • src/renderer/src/lib/host-mirror-handle-gap-resume.test.ts
  • src/renderer/src/lib/host-mirror-handle-gap-wait.ts
  • src/renderer/src/lib/host-mirrored-pane-liveness.ts
  • src/renderer/src/lib/host-mirrored-pane-resume-replay.test.ts
  • src/renderer/src/lib/resume-sleeping-agent-session.ts
  • src/renderer/src/lib/retracted-host-pane-resume.test.ts
  • src/renderer/src/lib/worktree-reactivation-preserved-pane-replacement.test.ts
  • src/renderer/src/runtime/host-session-mirror-frame-ordering-harness.ts
  • src/renderer/src/runtime/host-session-mirror-hydration-frame-ordering.test.tsx
  • src/renderer/src/runtime/host-session-mirror-settle-receipt-frames.test.tsx

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

Comment on lines +323 to +324
if (isHostMirroredSleepingRecord(record)) {
continue

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.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Block native resumes that share a host-owned provider session.

If a newer native record shares a provider session with an older mirrored record, the sweep clears the mirrored record and launches the native one. The new duplicate-record test exercises that path. A retracted mirror row does not establish that the host stopped writing the session. Check for a mirrored record across the claim key before launching any record for that session. Keep the mirrored record until host ownership is resolved.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant