Skip to content

fix(agents): stop the wake sweep from forking sessions that live SSH panes still own - #12702

Closed
robertnisipeanu wants to merge 3 commits into
stablyai:mainfrom
robertnisipeanu:fix/sleeping-agent-live-ssh-dedupe
Closed

robertnisipeanu wants to merge 3 commits into
stablyai:mainfrom
robertnisipeanu:fix/sleeping-agent-live-ssh-dedupe

Conversation

@robertnisipeanu

@robertnisipeanu robertnisipeanu commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When Orca wakes up a sleeping agent, it first checks whether a terminal pane is already running that agent's conversation. In three situations that check got it wrong and said "nobody has it", so Orca opened a second copy of the same conversation in a new tab. Two agents then wrote to one transcript. This PR fixes those three checks.

What Changed

All changes are in the wake sweep (resumeSleepingAgentSessionsForWorktree) and its pane-ownership check (recordPaneIsOwnedByPreservedPane).

  1. SSH pane while its host is unreachable. When an SSH connection drops, Orca clears its live-terminal map and tab.ptyId for that host (clearDirectSshTargetPtyBindings), but the agent keeps running on the remote machine. The pane's saved layout still names its SSH pty. Before: the sweep saw no live terminal and launched a duplicate claude --resume. After: a pane bound to an SSH pty whose host is not connected counts as still owning its session. The tab-level pty is only used as a fallback for a single-pane tab, so in a split one pane is never credited with another pane's terminal. When the host is connected, the live-terminal map decides, so a workspace the user put to sleep (terminal killed, id kept) still resumes normally.
  2. Record pointing at the wrong workspace. A sleeping record stores the workspace it saw when it was captured, which can be stale (for example when the agent works in a checkout it manages itself). Before: the sweep looked for the tab only under that workspace, missed it, and relaunched. After: tab ids are globally unique, so a miss falls back to the workspace that actually holds the tab (findPreservedTabOwner).
  3. Same conversation tracked under two workspaces. Duplicate detection was scoped to one workspace. Before: two records for one conversation in different workspaces never matched. After: a record is dropped instead of launched when an active record in another workspace shows a pane already owns that conversation (providerSessionOwnedElsewhere). Finished and invalid records never count as owners, matching the existing same-workspace rule.

Why

A duplicate --resume cannot be undone: both processes append to the same transcript. Each fix answers "who owns this conversation?" from evidence the app already has, instead of adding new state or IPC.

  • For SSH, losing contact is not evidence the process died, so an unreachable host is treated as "can't tell" and the sweep holds back. Treating every saved SSH pty id as alive would have stopped slept workspaces from ever resuming, so the check only applies while the host is unreachable.
  • These checks run after the host-authority gate on main (fix(terminal): don't seed or resume while the execution host hasn't answered #16750). That gate already stops the sweep for SSH repo workspaces whose host is disconnected; SSH folder workspaces pass that gate and rely on fix 1.

Linked Issue

Fixes #12699

Visual Proof

N/A. The fix decides whether a background resume happens; there is no new UI. The visible effect is the absence of a duplicate agent tab.

Testing

New tests in src/renderer/src/lib/resume-sleeping-agent-session-live-ssh-dedupe.test.ts:

  • a pane bound to an SSH pty is not resumed while its host is disconnected
  • a dead SSH pane on a connected host is resumed
  • in a split tab, a pane is not credited with another pane's SSH pty
  • a pane with no terminal binding still resumes
  • a record with a stale workspace id finds its tab's real workspace
  • a record is dropped when another workspace's pane owns the same conversation
  • a finished record in another workspace does not block an active one

Each fix's test fails without the fix.

pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/resume-sleeping-agent-session src/renderer/src/lib/sleeping-agent src/renderer/src/lib/wake-sleeping
pnpm tc:web
oxlint / oxfmt --check on changed files

Platforms: unit tests on macOS. Not manually reproduced against a live SSH host.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

AI Disclosure

Written with Claude Code (Claude Fable 5 and Claude Opus 5.5); reviewed with independent Claude review agents.

Review

Self-reviewed plus an independent code review of the full diff and a review of each follow-up fix.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Security: no new inputs, IPC or permissions.
  • Cross-platform: renderer-only logic, no paths or shortcuts.
  • Remote SSH: the main subject of the change; see fix 1.
  • Mobile: phone wake uses the same ownership check and gets the same fixes.
  • Backwards compatibility: no wire or persisted-format changes.
  • Performance: one extra pass over the sleeping records per record that would otherwise launch; the list is small.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c1e9bd28-a0d6-4f93-ba41-9ad7676efb5c


📥 Commits

Reviewing files that changed from the base of the PR and between 67271e9 and 9cafd1f.



📒 Files selected for processing (2)
  • src/renderer/src/lib/resume-sleeping-agent-session-live-ssh-dedupe.test.ts
  • src/renderer/src/lib/sleeping-agent-pane-ownership.ts


🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/lib/sleeping-agent-pane-ownership.ts
  • src/renderer/src/lib/resume-sleeping-agent-session-live-ssh-dedupe.test.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
📝 Walkthrough

Walkthrough

The change updates sleeping agent session recovery. Preserved pane ownership checks search across worktrees and recognize SSH PTYs whose connection is not connected. Resume processing clears a record and skips launch when another worktree has a preserved pane for the same provider session. Tests cover SSH ownership, missing pane bindings, stale worktree IDs, duplicate records, and finished records.



Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: 🔵 Low · up to 9cafd

Recovery may skip an older session after its pane is reused. The affected sequence is narrow, so the change is mergeable with owner awareness.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Issue #12699 is open and directly linked. The PR implements its three coding requirements. recordPaneIsOwnedByPreservedPane recognizes a persisted SSH binding when the target is disconnected. `findP…
Out of Scope Changes check Passed The production changes implement the ownership checks and cross-worktree deduplication required by issue #12699. The added tests verify those changes. No unrelated change is established by the reviewe…
Title check Passed The title clearly identifies the main change: preventing the wake sweep from launching duplicate sessions that are still owned by live SSH panes. It is concise and specific.
Description check Passed The description includes all required sections, explains the user impact and implementation, links issue #12699, documents testing and platform coverage, and addresses security, SSH, compatibility, an…

  • Fix all pre-merge checks with AI

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.

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

🧹 Nitpick comments (1)
src/renderer/src/lib/sleeping-agent-pane-ownership.ts (1)

124-128: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Shorten the new explanatory comments.

Keep one line for each non-obvious rule.

  • src/renderer/src/lib/sleeping-agent-pane-ownership.ts#L124-L128: state that stale worktree IDs require global tab lookup.
  • src/renderer/src/lib/sleeping-agent-pane-ownership.ts#L183-L188: state that a detached SSH relay remains owned by its bound pane.
  • src/renderer/src/lib/resume-sleeping-agent-session.ts#L232-L234: state that provider sessions can be owned across worktrees.

As per coding guidelines, comments must be concise, non-obvious, and brief; prefer one line.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0c6eb61c-37d4-4f33-882d-ecd17fefefcf

📥 Commits

Reviewing files that changed from the base of the PR and between eea0bb6 and 989016c21f3df0b8cf35961b09dbd8b392cd45b5.

📒 Files selected for processing (3)
  • src/renderer/src/lib/resume-sleeping-agent-session-live-ssh-dedupe.test.ts
  • src/renderer/src/lib/resume-sleeping-agent-session.ts
  • src/renderer/src/lib/sleeping-agent-pane-ownership.ts

@AmethystLiang
AmethystLiang requested a review from OrcaWin August 6, 2026 16:48
@robertnisipeanu
robertnisipeanu force-pushed the fix/sleeping-agent-live-ssh-dedupe branch from 989016c to 6ff633b Compare August 14, 2026 23:16
@coderabbitai

coderabbitai Bot commented Aug 14, 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.

@nwparker nwparker removed the P1 label Aug 19, 2026
@OrcaWin

OrcaWin commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Status update: the SSH relay is gone (#27214/#27215), but the disconnect path still clears direct-SSH PTY bindings (clearDirectSshTargetPtyBindings) and the wake sweep still judges ownership from ptyIdsByTabId, so #12699 still applies on the managed remote server. Keeping this open; it needs a rebase, and main now also gates resume on host authority (#16750).

robertnisipeanu and others added 2 commits October 11, 2026 11:50
…panes still own

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…s unreachable

A connected target's live-PTY map is authoritative: sleep kills the PTY but keeps
its id. Cross-worktree session dedupe ignores finished and invalid records, the
same records claim-key dedupe skips.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@robertnisipeanu
robertnisipeanu force-pushed the fix/sleeping-agent-live-ssh-dedupe branch from 6ff633b to 67271e9 Compare October 11, 2026 08:59
@robertnisipeanu

Copy link
Copy Markdown
Contributor Author

Rebased onto main. The SSH-binding check now only claims ownership while the leaf's target is unreachable (sshConnectionStates not connected), so it covers the clearDirectSshTargetPtyBindings path without blocking resume of a slept workspace on a connected target. It runs after the #16750 host-authority gate, which already stops the sweep for disconnected SSH repo workspaces; SSH folder workspaces still reach it. Cross-worktree dedupe also ignores finished and invalid records, so a finished note can't clear an active one.

@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: f67d4346-d3ad-4293-83e9-46c72b5032c3
📥 Commits

Reviewing files that changed from the base of the PR and between 6ff633b3045acc6b531d1772bca1f77648a6c73a and 67271e9.

📒 Files selected for processing (3)
  • src/renderer/src/lib/resume-sleeping-agent-session-live-ssh-dedupe.test.ts
  • src/renderer/src/lib/resume-sleeping-agent-session.ts
  • src/renderer/src/lib/sleeping-agent-pane-ownership.ts

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

…ng ssh ownership

In a split tab the tab-level pty can belong to another leaf.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@nwparker

Copy link
Copy Markdown
Contributor

Thanks, @robertnisipeanu! This was rebuilt on current main and merged in #29454 (your approach was used). Closing as superseded.

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

Development

Successfully merging this pull request may close these issues.

SSH: sleeping-agent wake sweep forks live remote sessions (disconnected panes judged dead; stale worktreeId; per-worktree dedupe)

4 participants