Skip to content

fix(agent-resume): skip sleeping-session resume for runtime-owned worktrees - #9098

Closed
nwparker wants to merge 3 commits into
mainfrom
nwparker/fix-8878-wave3
Closed

nwparker wants to merge 3 commits into
mainfrom
nwparker/fix-8878-wave3

Conversation

@nwparker

Copy link
Copy Markdown
Contributor

Description

Fixes #8878. A paired remote client (web client or a second Orca desktop) could launch codex resume <id> / claude --resume for agent sessions that are still alive and daemon-backed on the host, spawning a second TUI on the same provider session. The client-side resume pass runs before the async session-tabs mirror arrives, and every guard reads client-local state, so a live host session is invisible to it.

resumeSleepingAgentSessionsForWorktree now short-circuits when the target worktree is runtime-owned (host-authoritative) — the same host-authority predicate the plain-terminal auto-create gate already uses in Terminal.tsx (isWebRuntimeSessionActive(getRuntimeEnvironmentIdForWorktree(...))), applied at the single choke point that covers activation, startup hydration, and background wake. The host renderer keeps wake authority (its worktrees are not runtime-owned there); local, SSH, and WSL worktrees are unaffected.

Credit: Core fix and original runtime-owner matrix by @bbingz in #8887 (cherry-picked and rebased onto current main). This PR takes over that approach with adversarial coverage (folder workspaces, active-runtime global fallback, re-entrant activation/startup races) and the handoff-branch repro flipped into a positive regression guard.

Evaluated and not used: #9074 (@brennanb2025) — unrelated AskUserQuestion amber-status linger work; no overlap with the resume ownership gate.

Deliberate follow-ups tracked in #8878 (out of scope here): host-side provider-session dedup in terminal.create, and retiring resume records on tab close. Interaction with #8871: mirror-echo kills currently convert live sessions into pending duplicate resumes — this PR stops the duplicate-resume half on clients.

Evidence

  • Root cause: resume runs client-side with no runtime-ownership gate; guards only consult client-local tabsByWorktree / claim maps; host tabs arrive async via session-tabs mirror.
  • Fix site: src/renderer/src/lib/resume-sleeping-agent-session.ts — early return when isWebRuntimeSessionActive(getRuntimeEnvironmentIdForWorktree(...)).
  • Automated:
    pnpm exec vitest run --config config/vitest.config.ts \
      src/renderer/src/lib/resume-sleeping-agent-session-runtime-owner.test.ts \
      src/renderer/src/lib/repro-8878-remote-resume-no-runtime-gate.test.ts \
      src/renderer/src/lib/resume-sleeping-agent-session.test.ts \
      src/renderer/src/lib/resume-sleeping-agent-session-replay.test.ts \
      src/renderer/src/lib/resume-sleeping-agent-session-suppress-navigation.test.ts \
      src/renderer/src/lib/wake-sleeping-agents-in-background.test.ts \
      src/renderer/src/lib/worktree-activation
    → 16 files / 110 tests green (65 resume + 45 activation).
  • Failing-first: without the gate, runtime-owned matrix cases fail with expected 1 to be +0 (client resumes a live host session); with the gate, all pass.
  • Adversarial matrix:
    • explicit runtime:* worktree / repo host → skip
    • explicit local / ssh:* / WSL-local → still resume
    • runtime sibling isolation under different global focus
    • non-destructive record preservation + re-resume after ownership moves local
    • folder:* workspace keys via runtime project group
    • unmarked worktrees via active-runtime global fallback (paired client)
    • re-entrant activation + Terminal startup double-call stays 0

ELI5

When you open a worktree from another computer that's connected to your main Orca machine, that remote computer used to try to "wake up" agent sessions it thought were asleep. But those sessions were still running fine on the main machine — so you got two chat windows fighting over the same agent. Now the remote computer knows "this worktree belongs to the host machine" and leaves wake-up to the host, same rule we already use for not auto-creating extra empty terminals.

User-regression-tradeoffs

Change Who feels it Tradeoff
Skip client-side sleeping-session resume for runtime-owned worktrees Paired web / second-desktop clients Clients no longer auto-resume host sessions they only mirrored; host remains the sole wake authority. Correct — clients never had enough state to decide safely.
Local / SSH / WSL resume Unchanged Explicit non-runtime owners still resume.
Sleeping records left on disk when gated Clients with stale origin:live/quit records for host sessions Records are preserved (non-destructive) so a later ownership move back to this client can still resume. Retirement-on-close is a separate follow-up.
No host-side terminal.create provider-session dedup Defense-in-depth still missing if some other path spawns a resume Intentional scope cut; tracked in #8878. This gate covers all four production call sites of the resume choke point.

bbingz and others added 3 commits July 16, 2026 17:30
Extend @bbingz's runtime-owner matrix with folder workspaces, active-runtime
global fallback (paired client), and re-entrant activation/startup races.
Convert the handoff repro into a source-level regression guard that asserts
the choke-point gate remains present.
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a0e5a4f1-d946-4356-ad72-736d89ac319a

📥 Commits

Reviewing files that changed from the base of the PR and between 1cdc8e8 and 9f3abd7.

📒 Files selected for processing (3)
  • src/renderer/src/lib/repro-8878-remote-resume-no-runtime-gate.test.ts
  • src/renderer/src/lib/resume-sleeping-agent-session-runtime-owner.test.ts
  • src/renderer/src/lib/resume-sleeping-agent-session.ts

📝 Walkthrough

Walkthrough

Adds a runtime-ownership gate to resumeSleepingAgentSessionsForWorktree, preventing client-side resumption when an active web-runtime session owns the worktree. Tests cover local and remote ownership, shared stores, folder workspaces, fallback runtime detection, state preservation, re-entrant calls, and activation-path wiring.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the fix and tests, but it omits most required template sections. Add the missing template sections, including Summary, Screenshots, Testing checklist, AI Review Report, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: skipping resume for runtime-owned worktrees.
Linked Issues check ✅ Passed The PR implements the requested runtime-ownership gate for resume and adds regression tests covering #8878.
Out of Scope Changes check ✅ Passed The changes are limited to the fix and regression tests; no unrelated code appears introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

@AmethystLiang AmethystLiang added the orca-remote-server Remote Orca Server / orca serve / paired remote runtime label Jul 22, 2026
@AmethystLiang

Copy link
Copy Markdown
Contributor

Associating this PR with related issue(s): #8871

@AmethystLiang

Copy link
Copy Markdown
Contributor

Related to #9952 (sessions vanishing over remote Orca link).

@AmethystLiang

Copy link
Copy Markdown
Contributor

Associating this PR with related issue(s): #8871

@nwparker

nwparker commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded — the linked issue(s) for this PR were fixed by another change that has already been merged.

Superseding merge: #9687

Thank you for the contribution! If anything remains unaddressed, please open a new issue or PR against current main.

@nwparker nwparker closed this Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

orca-remote-server Remote Orca Server / orca serve / paired remote runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Remote client resumes live host agent sessions, spawning duplicate TUIs on the same provider session

3 participants