Skip to content

fix(runtime): a terminal start on an SSH twin no longer wakes or is refused by this computer's sleep - #30860

Merged
nwparker merged 5 commits into
mainfrom
nwparker/sleep-state-host-scoped
Oct 19, 2026
Merged

nwparker merged 5 commits into
mainfrom
nwparker/sleep-state-host-scoped

Conversation

@nwparker

@nwparker nwparker commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​140 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​140
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​115 $\color{#cf222e}{\Huge{\mathbf{−}}}$​31 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​84

ELI5

When a project is registered both on this computer and on an SSH host, its workspaces on the two machines share one id. Putting this computer's copy to sleep recorded the sleep under that shared id, so a terminal starting on the SSH copy was treated as if it were this computer's copy: a background start there was refused, and any other start there "woke" this computer's sleeping copy.

What Changed

  • Before: after sleeping a workspace on this computer, a background terminal start on its SSH twin failed with "worktree terminals sleeping", and a user-started terminal on the twin cleared this computer's sleep (clients were told it had woken).
  • Before, too: sleeping this computer's copy also stopped the SSH twin's running terminals and filed them under this computer's sleep, where nothing on the twin could wake them.
  • After: each host's copy has its own sleep record and stops only its own terminals. A start on the SSH twin neither is refused by nor wakes this computer's sleep, and the twin's terminals keep running. A start on the same host still refuses (background) or wakes (user) exactly as before.

Mechanism: each sleep resolves one scope (a host-plus-workspace key and a terminal host fence) once, from the launch-host resolver, and uses it everywhere. The runtime's two sleep maps (in-flight sleeps and committed sleep state) are keyed by that HostScopedWorktreeKey instead of the bare id, typed so a bare id cannot be passed, and renamed ...ByHostWorktree. The sleep's live-terminal inventory is filtered by the same fence, so only that host's terminals are stopped and recorded. When rival project rows disagree about the host, the sleep is refused, as the launch already is, rather than guessing this computer. A spawn already names its host through spawnWorktreeTarget (local or ssh:<target>); the sleep side derives its host from the same launch-host resolver (resolveWorktreeLaunchHost) the terminal launch scope uses to pick that connection, so a sleep and the spawns of the same workspace always name one key. That includes a runtime:-stamped project with a nested SSH target, which this process launches with no connection. Readers that only know the bare workspace id still match any host, as before: the "is this held asleep" check for automatic tab materialization, the sidebar retention gap, and the structured-session "sleeping" probe. They are conservative (a twin's sleep can only make them more cautious), and narrowing them needs host context their callers don't carry.

Unchanged on the wire: the worktreeTerminalSleepState events and the status snapshot keep their shape and still carry the bare worktreeId, so older clients see no difference.

Why

The spawn side was already host-qualified (#30798 gave every spawn a HostWorktreeTarget); only the sleep side dropped the host. Keying the record by host is the smallest change that makes both sides compare the same thing. Alternatives considered: checking each sleeping PTY's host at wake time (more moving parts, and the record would still collide for twins sleeping at once).

The per-worktree mutation lock stays keyed by the bare id on purpose. The same lock serializes adoption, project removal stops and terminal closes, which still run host-blind. A host-scoped sleep lock would stop excluding those. Over-serializing a twin's spawn behind a sleep is only a short wait, so it is safe.

Known limit: the scope is read when the sleep starts. If a project row is re-pointed to another host while its workspace sleeps, the record stays under the host it was slept on until that host's next spawn or sleep.

Design check (past three review rounds): earlier rounds each found a place where the sleep's host, or the terminals it touched, differed from the launch's. The redesign gives each sleep one scope object from the launch resolver, which every key and terminal filter reads, so there is a single choke point. Comparable remote-terminal designs also pick the host first and key saved terminal state by workspace inside it, so the approach holds.

Linked Issue

Follow-up from the SSH/managed-server deficiency scan (no issue).

Visual Proof

N/A: no UI change; the behavior is in the host runtime's sleep bookkeeping.

Testing

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

src/main/runtime/worktree-terminal-sleep-host-twin.test.ts:

  • an SSH twin's spawn is neither refused nor wakes this computer's sleep (fails on main with worktree_terminals_sleeping);
  • this computer's own spawn still refuses and wakes its sleep;
  • a runtime:-stamped project's spawn, with the connection the launch resolver hands it, still refuses and wakes its sleep;
  • sleeping this computer's copy while the SSH twin has a live terminal stops and records only this computer's terminal (fails without the fence);
  • rival rows that disagree about the host refuse the sleep.

Also ran src/main/runtime/terminal/stop, automatic-activation-host-sleep, project-removal-terminal-stop, sidebar-inputs, orca-runtime.test.ts and the PTY launch parity tests single-worker, pnpm tc:node, and every step of the static-analysis job locally. Verified only in tests, not live on a real SSH host. The managed-orcad Docker lane is dispatched against this head because path routing selects no e2e spec for this diff.

Pre-existing local failures, identical with this change reverted: orca-runtime-workspace-unread.test.ts, session-tabs/headless-browser-group-persistence.test.ts and two existing-folder import tests in orca-runtime.test.ts, which pass with a canonical TMPDIR. The VM rollback repro step can't build its baseline checkout locally (no zod in that temp tree), so CI covers it.

AI Disclosure

Review

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

Ensure no issues in: Security, Cross-platoform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

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)

@coderabbitai

coderabbitai Bot commented Oct 19, 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: 8b9fafb4-36c3-4b1f-bf7e-c03cbd2a91f0
📥 Commits

Reviewing files that changed from the base of the PR and between eb3af5dcf6ec9d66a01e0153c5732dce814d8ab6 and 9312f64.

📒 Files selected for processing: 10
  • src/main/runtime/construction/runtime-identity-state.ts
  • src/main/runtime/orca-runtime-get-status.ts
  • src/main/runtime/session-tabs/explicit-terminal-close-stops.ts
  • src/main/runtime/session-tabs/structured-session-cli-writer-host.ts
  • src/main/runtime/sidebar-inputs/orca-runtime-sidebar-temporal.ts
  • src/main/runtime/terminal/stop/worktree-terminal-sleep-key.ts
  • src/main/runtime/terminal/stop/worktree-terminal-sleep.ts
  • src/main/runtime/terminal/stop/worktree-terminal-stop-targets.ts
  • src/main/runtime/terminal/stop/worktree-terminal-stop.ts
  • src/main/runtime/worktree-terminal-sleep-host-twin.test.ts

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

📝 Walkthrough

Terminal sleep state and in-flight sleep tracking now use host-qualified worktree keys. Sleep scope resolution uses launch-host information to derive the key and PTY host fence. Sleep operations filter live PTYs by that fence. Sidebar retention and sleep-held checks can match a worktree ID across host-qualified keys. Tests cover local and SSH-twin spawn behavior, runtime-stamped launch behavior, and ambiguous host resolution.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 9312f

The previously reported typecheck issue is fixed, and the checked sleep path preserves host isolation. No identified issue remains to hold the merge.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Description check Warning The description is detailed and covers the user impact, implementation, rationale, testing, known limitations, and compatibility considerations. However, the required Linked Issue section states that … Add the actual linked issue under Fixes #, or document the applicable maintainer exception. Also update the checklist if the full command suite is covered by CI rather than local execution.
Docstring Coverage Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 10 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly states the primary fix: terminal starts on an SSH twin no longer interact incorrectly with local sleep state.
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.
Full details: Description check

Explanation

The description is detailed and covers the user impact, implementation, rationale, testing, known limitations, and compatibility considerations. However, the required Linked Issue section states that no issue exists and does not provide an issue link.

Resolution

Add the actual linked issue under Fixes #, or document the applicable maintainer exception. Also update the checklist if the full command suite is covered by CI rather than local execution.

  • 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: c12432f6-478a-4814-a1a8-b468d829d764
📥 Commits

Reviewing files that changed from the base of the PR and between bd7633f and eb3af5dcf6ec9d66a01e0153c5732dce814d8ab6.

📒 Files selected for processing: 6
  • src/main/runtime/construction/runtime-identity-state.ts
  • src/main/runtime/sidebar-inputs/orca-runtime-sidebar-temporal.ts
  • src/main/runtime/terminal/stop/worktree-terminal-sleep-key.ts
  • src/main/runtime/terminal/stop/worktree-terminal-sleep.ts
  • src/main/runtime/terminal/stop/worktree-terminal-stop.ts
  • src/main/runtime/worktree-terminal-sleep-host-twin.test.ts

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

Comment on lines +15 to +19
const nestedSshRepo = {
...store.getRepos()[0],
executionHostId: 'runtime:env-a',
connectionId: 'ssh-nested'
}

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.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Fix the typecheck failure: executionHostId is inferred as string.

The object literal at Line 15 widens executionHostId: 'runtime:env-a' to string. resolveWorktreeLaunchHost at Line 85 requires LaunchHostRepo, whose executionHostId is "local" | runtime:${string}|ssh:${string} | null | undefined. This causes TS2345 and fails pnpm run typecheck:ci. Declare the literal with as const on that property.

🐛 Proposed fix
--- "a/src/main/runtime/worktree-terminal-sleep-host-twin.test.ts"
+++ "b/src/main/runtime/worktree-terminal-sleep-host-twin.test.ts"
@@ -12,11 +12,11 @@
   await import('./orca-runtime-test-fixtures.spec')
 
 // A `runtime:` stamp with a nested SSH target: this process launches it with no connection.
 const nestedSshRepo = {
   ...store.getRepos()[0],
-  executionHostId: 'runtime:env-a',
+  executionHostId: 'runtime:env-a' as const,
   connectionId: 'ssh-nested'
 }
 const nestedSshStore = {
   ...store,
   getRepo: () => nestedSshRepo,
📝 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
const nestedSshRepo = {
...store.getRepos()[0],
executionHostId: 'runtime:env-a',
connectionId: 'ssh-nested'
}
const nestedSshRepo = {
...store.getRepos()[0],
executionHostId: 'runtime:env-a' as const,
connectionId: 'ssh-nested'
}

Source: Pipeline failures

nwparker and others added 4 commits October 19, 2026 13:50
…efused by this computer's sleep

The host's sleep state was keyed by the bare worktree id, which a repo
registered on two hosts publishes twice. A spawn on the SSH copy found the
local copy's sleep, so an automatic probe was refused and any other spawn
woke the local sleep. Key both sleep maps by host plus workspace, with the
sleep side deriving its host the way a spawn does.

Co-authored-by: Claude <noreply@anthropic.com>
…H target, as its spawns do

Co-authored-by: Claude <noreply@anthropic.com>
…launch scope uses

Predicting the host from one repo row disagreed with the launch path for a
runtime-stamped project with a nested SSH target. Reuse resolveWorktreeLaunchHost
so a sleep and the spawns of the same workspace always name one key.

Co-authored-by: Claude <noreply@anthropic.com>
…and refuses an unresolvable host

Sleeping this computer's copy also stopped the SSH twin's live PTYs and filed
them under this host's key, where the twin's spawn could never wake them.
Resolve one sleep scope (key and PTY fence) per sleep from the launch-host
resolver, fence the live sets with it, and refuse the sleep when rival rows
disagree about the host instead of guessing local. The maps are typed and
named by host-scoped key.

Co-authored-by: Claude <noreply@anthropic.com>
@nwparker
nwparker force-pushed the nwparker/sleep-state-host-scoped branch from eb3af5d to 8ead454 Compare October 19, 2026 20:56
@nwparker
nwparker merged commit acdf066 into main Oct 19, 2026
56 checks passed
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