Skip to content

fix(ssh): compare lease and runtime pty ids in one form so pane recovery runs - #17969

Merged
nwparker merged 1 commit into
mainfrom
fix/ssh-lease-id-form
Sep 3, 2026
Merged

nwparker merged 1 commit into
mainfrom
fix/ssh-lease-id-form

Conversation

@nwparker

@nwparker nwparker commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​393 $\color{#cf222e}{\Huge{\mathbf{−}}}$​25 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​368
Prod 11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​141 $\color{#cf222e}{\Huge{\mathbf{−}}}$​41 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​100

Closes #17958. Stacked on #17966 — review that first.

ELI5

SSH pane recovery has never run. Leases are stored in relay form (pty-3); the reader compared them raw against the runtime's app form (ssh:target@@pty-3), so the equality never held and recoverTerminalPane always threw terminal_not_recoverable. toComparablePtyId existed right beside it, unused.

#17958 said this could not be fixed alone: switching the path on would have introduced duplicate spawns, since every expired writer was audited as non-attesting. That ordering constraint is satisfied by the stack below this PR — verified by reading the code on the branch, not just the log:

It reattaches; it does not respawn

Traced end to end and pinned with tests rather than read:

recoverTerminalPane → createTerminal(..., {focus: false}) → no rendererBacked, so shouldCreateInBackground is true → the first act is adoptStablePane, before any spawn → resolveStablePaneOwner finds the persisted binding (retained because #17965 stopped expired withdrawing it) → attachStablePaneOwner calls provider.spawn({ sessionId: owner.ptyId, attachOnly: true }).

That is a reattach. A fresh shell happens only when the provider raises isPtyAlreadyGoneError — the relay's own "PTY not found" — which retires the binding and lets createTerminal spawn. The attach attempt is itself the arbiter, so #17958's warned-about duplicate-spawn cannot occur.

The common orphan case never reaches createTerminal at all: a failed relay reattach writes expired and a synthetic pty:exit -1, which grades unverifiable, and #17957's gate refuses. This path fires for an expired lease whose PTY has no live/unverifiable verdict — a host-confirmed exit, or a pane with no verdict recorded.

A second inert test found along the way

#17957's still recreates a shell for an SSH pane whose host confirmed the exit was also inert in production. It only passed because the fixture createRuntimeWithSshLease stored the lease in app form, which upsertSshRemotePtyLease never writes. Making the fixture apply the same relay-form conversion production does is what exposed it — and it then failed, correctly.

Changes

  • src/shared/ssh-pty-id.ts — extracted the tolerant normalizer as toComparableRelaySshPtyId (the try/catch wrapper previously inline in getRelayPtyIdForSshLeaseComparison, which now delegates to it). One implementation, not two.
  • orca-runtime-reconcile-headless-mobile-session-browser-tabs.ts:95 — lease.ptyId === toComparableRelaySshPtyId(lease.targetId, ptyId).
  • orca-runtime-resolve-terminal-pane.ts — recoverTerminalPane also refuses a lease that sshRemotePtyLeaseAllowsReattach disqualifies (superseded / recycled).

Why the primitive and not a RuntimeStore member: getRelayPtyIdForSshLeaseComparison is a method on a persistence class reachable only through instance wiring; neither RuntimeStore nor Store exposes it. Plumbing it as an optional member would have re-created this exact bug — every test mock store lacks it, so it would silently fall back to the raw compare.

The other two callers of getRecentExpiredSshLease (:90, :122) pass no ptyId, so the normalized branch short-circuits and their behavior is unchanged. The supersession check went in recoverTerminalPane rather than the shared getter deliberately, to keep their pane-coordinate retention exactly as-is.

User-facing regressions

None expected. Negative controls, all passing:

  • operator-closed terminated still refuses — and that test was upgraded to a real SSH pane, so it now exercises the live path instead of a non-SSH id that never reached the comparison;
  • superseded and relay-id-recycled leases both refuse;
  • a genuinely dead shell still gets a working terminal via the fresh-spawn fallthrough.

Testing

Before (production change reverted, tests in place):

 ❯ src/main/runtime/orca-runtime.test.ts (1226 tests | 2 failed | 1 skipped)
     × matches an SSH pane against its own lease across the relay/app id forms
     × still recreates a shell for an SSH pane whose host confirmed the exit
AssertionError: promise rejected "Error: terminal_not_recoverable" instead of resolving
 ❯ OrcaRuntimeService.recoverTerminalPane orca-runtime-resolve-terminal-pane.ts:84:13
      Tests  2 failed | 1223 passed | 1 skipped (1226)

After:

 ✓ matches an SSH pane against its own lease across the relay/app id forms
 ✓ does not recover a pane whose expired lease was superseded by a newer one
 ✓ does not recover a pane whose expired lease had its relay id recycled
 ✓ does not recover a pane whose authoritative SSH lease was terminated
 ✓ does not recreate a shell for an expired lease whose PTY liveness is unverifiable
 ✓ still recreates a shell for an SSH pane whose host confirmed the exit
 ✓ reattaches the surviving orphan instead of spawning a second shell
 ✓ falls through to a fresh spawn once the host answers that the PTY is absent

Suites: orca-runtime.test.ts 1225 passed | 1 skipped; +5 lease/binding suites 1283 passed; +7 further suites 68 passed. tc:node, tc:web clean. check:code-quality:changed 0 new findings across 35 files.

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

✅ No new issues found.

Reviewed changes

  • Id-form normalization at the comparison site — getRecentExpiredSshLease now compares lease.ptyId against toComparableRelaySshPtyId(lease.targetId, ptyId), so the relay-form stored lease (pty-3) finally matches the runtime's app-form id (ssh:target@@pty-3). Non-SSH ids pass through unchanged, and a mismatched target returns false rather than throwing.
  • Tolerant normalizer extraction — toComparableRelaySshPtyId in src/shared/ssh-pty-id.ts absorbs the previous inline try/catch from getRelayPtyIdForSshLeaseComparison into one shared implementation, re-exported through the provider barrel.
  • Reattach-eligibility gate — recoverTerminalPane now refuses superseded and relay-id-recycled expired leases via sshRemotePtyLeaseAllowsReattach, placed at the recovery site (not the shared getter) so the two coordinate-retention callers keep their existing behavior.
  • Fixture and test hardening — createRuntimeWithSshLease now stores the lease in relay form exactly as upsertSshRemotePtyLease does, which exposed that #17957's "host confirmed the exit" test was also inert in production; new tests pin the cross-form match plus the superseded/recycled refusals, and two adoptStablePane tests pin reattach-before-respawn.

I traced the reattach claim end to end: recoverTerminalPane → createTerminal({focus: false}) → shouldCreateInBackground → adoptStablePane → resolveStablePaneOwner → provider.spawn({sessionId, attachOnly: true}), with a fresh spawn only after the relay's own "PTY not found" answer retires the binding. The comparison semantics check out for matching targets, mismatched targets, host-form vs bare target ids, and non-SSH passthrough. Test suites pass (orca-runtime.test.ts, ssh-expired-lease-pane-readoption.test.ts: 1229 passed, 1 skipped).

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

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

✅ No new issues found.

Reviewed changes

  • Base lint-fix propagation — the new commit merges the base branch's import-consolidation change into deferred-session-reattach-connect.ts (two ./pty-connect-limits imports collapsed into one). Purely mechanical; no behavioral surface.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker

nwparker commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Resolved: this does not arm any dead positive gate

Review raised the sharpest risk on this PR — that fixing the id-form comparison flips getRecentExpiredSshLease from never matching to matching repo-wide, for every caller at once, potentially arming a positive gate that had been dead code since it was written. That is exactly how a latent respawn path goes live, so it needed settling before merge.

Checked, and it does not happen. There are exactly three non-test callers on this branch, and only one passes a ptyId:

Caller ptyId Polarity Effect of this PR
orca-runtime-resolve-terminal-pane.ts:81 yes negative — if (!lease) throw 'terminal_not_recoverable' recovery goes from never-running to running
…browser-tabs.ts:90 undefined (explicit) positive none
…browser-tabs.ts:124 (hasRecentExpiredSshLeasePane) omitted → undefined positive none

The predicate short-circuits on the id entirely when none is supplied:

(ptyId === undefined || lease.ptyId === toComparableRelaySshPtyId(lease.targetId, ptyId)) &&

So both positive gates were already matching before this PR and are byte-for-byte unaffected by it — they never reached the broken comparison. The only caller whose behaviour changes is the negative one, and arming it restores pane recovery rather than authorising anything new.

Two related points from the same review, recorded

1. The fails-closed direction means #17965 is not blocked on this. Because the resolve-terminal-pane gate always threw pre-fix, landing #17965 without this PR cannot match the wrong pty or strand an orphan at that site — SSH pane recovery simply stays inert and the pane reports terminal_not_recoverable. Conservative, not harmful. #17965 should not be held on account of this.

2. This PR does depend on #17966, which the stated chain order already guarantees: the sshRemotePtyLeaseAllowsReattach predicate ships in #17966 (src/shared/ssh-types.ts) while its only pane-recovery consumer ships here.

Acknowledged limitation

Review also makes a fair architectural point: the underlying defect is that an SSH pty id exists in two forms that are both string, so relay-form and app-form compare with === without complaint, and this PR fixes one comparison site. Every other site comparing a runtime ptyId against a stored lease/binding ptyId carries the same latent bug, invisibly.

The root fix is a branded type — type RelaySshPtyId = string & { __relay: true } — so the storage boundary cannot accept an app-form id and the compiler enumerates the remaining sites. That converts an invisible runtime no-op into a build error. Worth doing, and deliberately out of scope here; filing separately rather than widening a fix that is already switching a dormant path on.

`getRecentExpiredSshLease` compared the stored lease ptyId (relay form,
written through `toStoredPtyId` -> `toRelaySshPtyId`) raw against the
runtime's app-form `pty.ptyId`, so `'pty-3' === 'ssh:target@@pty-3'` never
held and `recoverTerminalPane` refused every real SSH pane. Normalize with
the same tolerant helper the binding reader already uses, now shared as
`toComparableRelaySshPtyId`.

Switching the path on is only safe on top of #17957 (respawn gated on the
runtime liveness verdict), #17965 (`expired` no longer withdraws bindings)
and #17966 (supersession and id recycling carry their own marks).
`recoverTerminalPane` additionally refuses a lease those marks disqualify,
so it acts only on an `expired` lease that means "reattach gave up".

The path's outcome is a reattach, not a respawn: `createTerminal` calls
`adoptStablePane` first, which attaches attach-only to the retained
binding and only falls through to a fresh shell once the host itself
answers that the PTY is absent.
@nwparker
nwparker force-pushed the fix/ssh-lease-id-form branch from d3623d8 to c571773 Compare September 3, 2026 04:41
@nwparker
nwparker changed the base branch from fix/superseded-lease-marker to main September 3, 2026 04:42
@nwparker
nwparker merged commit 08c7152 into main Sep 3, 2026
30 of 31 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#17969)

`getRecentExpiredSshLease` compared the stored lease ptyId (relay form,
written through `toStoredPtyId` -> `toRelaySshPtyId`) raw against the
runtime's app-form `pty.ptyId`, so `'pty-3' === 'ssh:target@@pty-3'` never
held and `recoverTerminalPane` refused every real SSH pane. Normalize with
the same tolerant helper the binding reader already uses, now shared as
`toComparableRelaySshPtyId`.

Switching the path on is only safe on top of stablyai#17957 (respawn gated on the
runtime liveness verdict), stablyai#17965 (`expired` no longer withdraws bindings)
and stablyai#17966 (supersession and id recycling carry their own marks).
`recoverTerminalPane` additionally refuses a lease those marks disqualify,
so it acts only on an `expired` lease that means "reattach gave up".

The path's outcome is a reattach, not a respawn: `createTerminal` calls
`adoptStablePane` first, which attaches attach-only to the retained
binding and only falls through to a fresh shell once the host itself
answers that the PTY is absent.
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 pane recovery is inert: getRecentExpiredSshLease compares relay-form and app-form pty ids

1 participant