Skip to content

fix(ssh): reopening a workspace-slept agent tab on a managed host resumes it, without widening its permissions - #29672

Merged
nwparker merged 8 commits into
mainfrom
nwparker/desktop-wake-proven-exit
Oct 16, 2026
Merged

nwparker merged 8 commits into
mainfrom
nwparker/desktop-wake-proven-exit

Conversation

@nwparker

@nwparker nwparker commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

You put a workspace to sleep while an agent was in one of its tabs. On a managed SSH host, opening that tab again gave you a plain shell, and the conversation never came back. Now the host remembers which conversation each slept tab held and resumes it when you open the tab. Resuming also no longer adds permission-skipping flags you never launched the agent with.

What Changed

Before: on a managed SSH host, with one or two paired desktops, opening a workspace-slept agent tab always gave a plain shell. That was true even when no other tab held the conversation (the soak's control runs from desktop 1 and desktop 2 both failed).

After: reopening the tab runs claude --resume <id> (or that agent's equivalent).

Mechanism. When a paired desktop sleeps a workspace, it asks the host (terminal.sleep) to stop the terminals. The desktop wrote its "resume this conversation" record only into its own state. For host-owned tabs, the desktop defers the tab open to the host (session.tabs.activate), which reads only its own records. So no --resume was ever issued, from either desktop. The #29588 holder check was never reached.

The host's sleep now captures those records itself, before it stops each terminal (worktree-sleep-resume-records.ts, worktree-sleep-resume-recording.ts):

  • It reads each pane's conversation id from its own hook store. The host already owns agent status, so this needs no new protocol.
  • It skips an agent the hook store has proven exited. This is the same holder rule fix(remote): reopening a slept tab resumes when the other holder's agent has proven exited #29588 uses: proven exited means not a holder, unverifiable means still a holder.
  • It writes records only for terminals it proved stopped, or whose stop was acknowledged when the post-stop listing failed.
  • It keeps the PTY's launch recipe, the SSH host stamp (a WSL relay tag counts as local), and an earlier record's recipe when a reattached PTY has forgotten it.
  • An older record is retired only on hook evidence: a proven exit, or another agent now reporting in that pane. A missing hook row keeps it, because a just-resumed agent may simply not have reported yet.

At open, the existing #29588 path applies the holder rule against other panes.

Permission rule (security). A resume used to fill a missing launch recipe from the settings defaults, and those defaults carry the permission bypass (for example --dangerously-skip-permissions). That silently widened a session the user had started by hand as a bare claude. Three paths now share one helper, resolveResumeLaunchRecipe: the host tab-open resume (mobile-terminal-startup.ts), the window's cold restore (cold-restore-resume-startup.ts), and worktree wake (sleeping-agent-session-launch.ts). They use the recorded recipe, or no args and no env. Orca-launched sessions keep their recorded flags.

The headless "can't wake slept agents" warning on mobile activation now ignores records that resume on tab open.

Why

The host answers the tab open, so it has to hold the record. Alternatives considered:

  • Have the desktop send its record to the host. That needs a new field on the sleep RPC, and it still fails for a second desktop or a phone that never had the record.
  • Keep the hook row through the stop. That changes hook-store retention, which other readers depend on.

Capturing on the host from its own hook store needs no wire change. Older clients get the fix as soon as the host updates, and an older host behaves exactly as before.

The record write goes through the existing durable session-edit path, now a shared commitWorkspaceSessionEdit, so the main-process layout-writer ratchet stays flat.

Known follow-ups, not in this PR:

  • A desktop-renderer agent launch does not hand its recipe to the host PTY. Its slept resume now runs with no args, which is narrower and safe, rather than with its recipe.
  • On a host that also has its own desktop window open, that window's next full session save can overwrite the host-written records. Before this PR there were no records at all.

Linked Issue

Follow-up to #29588 (soak finding: the desktop path on managed SSH hosts).

Visual Proof

N/A. Terminal behaviour only; the live soak results are below.

Testing

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

Live, on real hardware. Soak harness, managed SSH host on omarchy (Linux) under an isolated HOME and a private sshd, with two paired macOS desktops. The fake claude posts the real hook process identity and the SessionEnd reason. The managed server and both desktops were built from 4e3f509d1b11:

  • (A) sleep, then reopen from desktop 1: claude ... --resume <S>, resumed=yes.
  • (B) sleep from desktop 1, then reopen from desktop 2: resumed=yes.
  • (C) sleep, then claude --resume S plus a prompt plus exit in the main worktree, then reopen: resumed=yes. This is the original soak scenario.
  • (D) as C, with the main agent still running: plain shell, no second writer.

That build still added --dangerously-skip-permissions from the settings defaults, which led to the permission fix. Re-run on 50d4042:

  • (A) and (C) with a hand-typed claude: claude '--resume' '<S>', no permission flag, resumed=yes.
  • (E) an agent launched from Orca's New tab → Claude (which ran claude '--dangerously-skip-permissions'): the recorded recipe is kept, giving claude '--dangerously-skip-permissions' '--resume' '<S>', resumed=yes.

Tests. src/main/runtime/host-sleep-resume-record.test.ts is new. Its sleep-and-reopen cases fail on main.

  • Agent verdict null, unverifiable or exited at sleep.
  • Another pane holds the session, proven exited or unverifiable.
  • Re-slept before the first hook; retire on another agent.
  • Launch recipe kept or dropped; SSH, WSL and local stamps.
  • No permission bypass for a hand-typed session; recorded flags kept for an Orca launch.

Updated renderer resume specs pin the no-defaults rule. orca-runtime.test.ts (1388 tests) passes. The full local replay of the pr.yml "static analysis and typecheck" job passes, including the focus-read and layout-writer ratchets.

Review

Codex review (gpt-6.1-sol, high) ran across 8 rounds. Fixed:

  • Persist stops that were acknowledged before the inventory failure.
  • Keep the launch recipe.
  • Don't drop a record over a missing hook row.
  • Keep the SSH stamp, and treat a WSL relay tag as local.
  • Keep a saved recipe a reattached PTY forgot.
  • Read the conversation id after the presence probe.
  • Retire a record when another agent reports in the pane.

The remaining P2s are the two follow-ups above.

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: resume can no longer widen agent permissions beyond the original launch.
  • SSH: records carry the SSH host stamp.
  • Wire compatibility: no wire change; old clients benefit as soon as the host updates.
  • Folder workspaces: captures are keyed by pane and worktree id, so folder workspaces are covered.
  • Windows/WSL: a WSL relay tag counts as local.

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)

…nt on the managed host

A paired client's worktree sleep stops the host's PTYs, but the resume
record its renderer writes never reaches the host, and the host is what
answers the tab open. Capture the record on the host from its own hook
store before the stop, applying the shared holder rule (an agent the
hook store proved exited leaves no record).
…ermission-widening defaults

Resuming a session started as a hand-typed `claude` filled the missing
launch recipe from settings defaults, which carry the permission bypass.
Host tab-open resume, window cold restore and worktree wake now share
one rule: the recorded recipe, or none.
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The change adds capture, persistence, application, and cleanup helpers for worktree sleep-resume records. Worktree terminal sleep now captures records before stopping PTYs and persists them for acknowledged or verified stops. Headless activation excludes records marked tab-open-only. Resume startup paths use a shared launch recipe resolver, which uses saved arguments and environment when available and otherwise returns empty values.

Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 50d40

A hook snapshot failure could prevent a workspace from sleeping. This is a bounded risk, but capture errors should not block sleep.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 31.25% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 16 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 identifies the SSH workspace-sleep resume fix and the permission-safety behavior. It is concise enough for a pull request title.
Description check Passed The description is complete and aligned with the template. It explains the user impact, mechanism, rationale, linked issue, testing, security, compatibility, follow-ups, and checklist status. The issu…
Full details: Description check

Explanation

The description is complete and aligned with the template. It explains the user impact, mechanism, rationale, linked issue, testing, security, compatibility, follow-ups, and checklist status. The issue is referenced as a follow-up rather than with a “Fixes #” line, but the required issue context is present.

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

@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: bf8fd9e7-46e4-4ff7-9479-ed35a44306e2
📥 Commits

Reviewing files that changed from the base of the PR and between 19cf2e5 and 50d4042.

📒 Files selected for processing: 16
  • src/main/runtime/host-sleep-resume-record.test.ts
  • src/main/runtime/terminal/launch/mobile-terminal-startup.ts
  • src/main/runtime/terminal/stop/worktree-sleep-resume-recording.ts
  • src/main/runtime/terminal/stop/worktree-sleep-resume-records.ts
  • src/main/runtime/terminal/stop/worktree-terminal-sleep.ts
  • src/main/runtime/terminal/stop/worktree-terminal-stop.ts
  • src/main/runtime/workspaces/workspace-activation.ts
  • src/renderer/src/components/terminal-pane/pty-connection-agent-session-resume.spec.ts
  • src/renderer/src/components/terminal-pane/pty-connection-cold-restore-agent-resume.spec.ts
  • src/renderer/src/components/terminal-pane/pty-connection-cold-restore-resume-command.spec.ts
  • src/renderer/src/components/terminal-pane/pty-connection-remote-runtime-attach.spec.ts
  • src/renderer/src/components/terminal-pane/pty-connection-sleeping-resume-banner.spec.ts
  • src/renderer/src/components/terminal-pane/pty-connection/cold-restore-resume-startup.ts
  • src/renderer/src/lib/sleeping-agent-session-launch-windows-quoting.test.ts
  • src/renderer/src/lib/sleeping-agent-session-launch.ts
  • src/shared/tui-agent-launch-defaults.ts

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

Comment on lines +143 to +144
// Why before the stop: a PTY's exit clears its hook rows, and with them the session ids.
const resumeCapture = await this.sleepResumeRecords.capture(worktree.id, livePtyIds)

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.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Capture errors now abort the sleep before any PTY stops.

sleepResumeRecords.capture runs inside the main try. It has no catch. persist swallows its errors, so a lost record "costs the resume, not the sleep". capture does not follow that rule. captureWorktreeSleepResumeRecords catches errors from checkAgentPresence. It does not catch a throw from readRows, which calls getAgentProviderSessionSnapshotFn. If that call throws, the sleep fails with no PTYs stopped. Wrap the capture so that a failure produces an empty capture.

Proposed fix
-      const resumeCapture = await this.sleepResumeRecords.capture(worktree.id, livePtyIds)
+      const resumeCapture = await this.sleepResumeRecords
+        .capture(worktree.id, livePtyIds)
+        .catch((error) => {
+          console.error('[runtime] could not capture slept agent sessions', { worktreeId: worktree.id, error })
+          return new Map() as Awaited<ReturnType<typeof this.sleepResumeRecords.capture>>
+        })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Why before the stop: a PTY's exit clears its hook rows, and with them the session ids.
const resumeCapture = await this.sleepResumeRecords.capture(worktree.id, livePtyIds)
// Why before the stop: a PTY's exit clears its hook rows, and with them the session ids.
const resumeCapture = await this.sleepResumeRecords
.capture(worktree.id, livePtyIds)
.catch((error) => {
console.error('[runtime] could not capture slept agent sessions', { worktreeId: worktree.id, error })
return new Map() as Awaited<ReturnType<typeof this.sleepResumeRecords.capture>>
})

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