Repository navigation
fix(ssh): give supersession and id-recycling their own lease marks - #17966
Conversation
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the full diff (10 files, 1 commit) that separates the three meanings of the expired SSH lease state so genuine orphans can reattach while superseded and recycled-id leases stay excluded.
- New lease marks and a shared predicate —
supersededByandrelayIdRecycledfields added toSshRemotePtyLease, withsshRemotePtyLeaseAllowsReattachas the single decision point (src/shared/ssh-types.ts). - Supersession stamps already-
expiredpredecessors —supersedeSiblingLeasesForPanenow marks themsupersededBywhile leavingstate/updatedAtuntouched, bounding the reattach set to one orphan per pane. - Recycled-id retirement records its own mark — the pending-stop replay passes
{ relayIdRecycled: true }so the reattach fence no longer relies onexpiredalone. - Live re-upsert clears both marks — a restarted relay claiming
pty-1for a new shell drops the predecessor's marks before supersession re-derives anything. - Persistence round trip —
normalizeSshRemotePtyLeasewhitelists both fields with strict type checks; both reattach call sites inssh-relay-session.tsswitch to the shared predicate.
I traced the state machine against the readers of expired (getRecentExpiredSshLease, terminateSessions, recoverTerminalPane) and the pre-existing markSshRemotePtyLeasesAttachedAsync behavior that leaves expired leases expired after a successful reattach — the design is internally consistent and the new tests use exact, fail-able assertions rather than snapshots.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
Verified correction:
|
7bd3d08 to
037e6b1
Compare
57c88a9 to
db53b3e
Compare
037e6b1 to
b2ecc7f
Compare
…tell them apart `expired` was one word doing two unrelated jobs — "a newer lease won this pane" and "reattach lost contact" — so `reattachKnownPtys` had to exclude all of them. That kept the 2 -> 19 -> 20 fan-out fixed at the cost of never bulk-reattaching a genuine orphan; those recovered only through the slower `adoptStablePane` path. The blocker cited in #17965 does not apply. The STA-3077 note guards `upsertSshRemotePtyLease`'s match against a RECYCLED `pty-N` after a relay restart. `supersedeSiblingLeasesForPane` is a different path and already holds `winner.ptyId` when it expires a predecessor, so recording which lease won needs no relay-start identity. `SshRemotePtyLease` gains two optional marks, each meaning exactly one thing: - `supersededBy` — the winner's stored-form ptyId, written only by supersession. - `relayIdRecycled` — written only by the pending-stop replay's `relay-id-recycled` retirement. That retirement wrote `expired` *purely* to keep the lease out of the reattach that runs one step later ("hands the user's old pane to whatever process now holds the recycled id"), and the reattach fences on paneKey/tabId, never on incarnation. Relaxing the filter without this would have silently reopened that hole. Bulk reattach now skips a lease carrying either mark and re-adopts the rest, via one shared `sshRemotePtyLeaseAllowsReattach`. `terminated` is untouched. Recycled-id safety: both marks are dropped whenever the id is re-upserted `attached`/`detached`, so a relay that renumbered onto a new shell cannot inherit its predecessor's mark. Supersession also stamps an ALREADY-expired predecessor for the same pane — same evidence, and it is what bounds the reattach set, since otherwise every past orphan for that pane would stay reattachable forever. Its `updatedAt` deliberately stays put: bumping it would make a stale lease look recent to `getRecentExpiredSshLease`. Persistence: the lease loader is a strict whitelist, so both fields are named in `normalizeSshRemotePtyLease` or they would be stripped on every launch. Absence reads as "orphan", which is the only thing an older build could have meant, and an older build ignores keys it has never heard of (remote-wire Rule 1).
db53b3e to
a913860
Compare
`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.
…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 #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.
…tell them apart (stablyai#17966) `expired` was one word doing two unrelated jobs — "a newer lease won this pane" and "reattach lost contact" — so `reattachKnownPtys` had to exclude all of them. That kept the 2 -> 19 -> 20 fan-out fixed at the cost of never bulk-reattaching a genuine orphan; those recovered only through the slower `adoptStablePane` path. The blocker cited in stablyai#17965 does not apply. The STA-3077 note guards `upsertSshRemotePtyLease`'s match against a RECYCLED `pty-N` after a relay restart. `supersedeSiblingLeasesForPane` is a different path and already holds `winner.ptyId` when it expires a predecessor, so recording which lease won needs no relay-start identity. `SshRemotePtyLease` gains two optional marks, each meaning exactly one thing: - `supersededBy` — the winner's stored-form ptyId, written only by supersession. - `relayIdRecycled` — written only by the pending-stop replay's `relay-id-recycled` retirement. That retirement wrote `expired` *purely* to keep the lease out of the reattach that runs one step later ("hands the user's old pane to whatever process now holds the recycled id"), and the reattach fences on paneKey/tabId, never on incarnation. Relaxing the filter without this would have silently reopened that hole. Bulk reattach now skips a lease carrying either mark and re-adopts the rest, via one shared `sshRemotePtyLeaseAllowsReattach`. `terminated` is untouched. Recycled-id safety: both marks are dropped whenever the id is re-upserted `attached`/`detached`, so a relay that renumbered onto a new shell cannot inherit its predecessor's mark. Supersession also stamps an ALREADY-expired predecessor for the same pane — same evidence, and it is what bounds the reattach set, since otherwise every past orphan for that pane would stay reattachable forever. Its `updatedAt` deliberately stays put: bumping it would make a stale lease look recent to `getRecentExpiredSshLease`. Persistence: the lease loader is a strict whitelist, so both fields are named in `normalizeSshRemotePtyLease` or they would be stripped on every launch. Absence reads as "orphan", which is the only thing an older build could have meant, and an older build ignores keys it has never heard of (remote-wire Rule 1).
…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.

Stacked on #17965 — review that first. Closes the last conflation in the SSH lease vocabulary, and unblocks the two readers #17965 had to leave alone.
ELI5
The lease state
'expired'was one word doing three unrelated jobs:pty-1.Because those are indistinguishable,
ssh-relay-session.ts:1985/:2272had to exclude allexpiredleases from bulk reattach — so genuine orphans were never bulk-reattached either. This gives jobs 2 and 3 their own marks, so job 1 can finally be reattached.supersededBy?: stringsupersedeSiblingLeasesForPanerelayIdRecycled?: truerelay-id-recycledOne predicate,
sshRemotePtyLeaseAllowsReattach(src/shared/ssh-types.ts), is now used by both call sites.terminatedstill returns false unconditionally.The third job was not in the brief and would have been a silent regression
I briefed this as a two-way split. It isn't.
ssh-pending-pty-kill-replay.ts:50-58writesexpiredfor a third reason, and its comment says the write exists solely to be filtered out by the reattach one step later:A
supersededBy-only change would have reopened that hole, and the paneKey fence cannot catch it — which is exactly why the state was used. Hence two marks, not one.STA-3077 was not the blocker
#17965 concluded this needed relay-start identity per the STA-3077 note. That note (
ssh-pty-lease-operations.ts:118-122) guardsupsertSshRemotePtyLease's(targetId, ptyId)match against a recycledpty-N— a different path.supersedeSiblingLeasesForPanealready holdswinner.ptyIdat line 63. Recording the winner needed nothing new.Recycled ids cannot inherit a stale mark
Both marks are deleted in
upsertSshRemotePtyLeasewhen the merged lease landsattached/detached. A restarted relay handingpty-1to a new shell produces a live upsert, which clears the predecessor's mark before supersession re-derives anything. If a mark ever did leak, the failure direction is exclusion — today's behavior — never fan-out.Supersession also stamps already-
expiredpredecessors for the same pane (supersededByonly; state andupdatedAtuntouched, since bumpingupdatedAtwould make a stale row look fresh togetRecentExpiredSshLease, which #17958 owns). Without that, every past orphan for a pane stays in the reattach set forever and each reconnect adds anotherpty.attach. The invariant is now at most one non-superseded expired lease per pane — the newest, i.e. the actual orphan.Persistence and wire
normalizeSshRemotePtyLeaseis a strict whitelist, so both fields are named there or they'd be stripped on every launch (ssh-pending-pty-kill.ts:25-27records that failure having shipped once). Rule 1 holds both directions: an older build's row carries neither mark and reads as orphan — the only thing that build could have meant — and an older build ignores keys it never heard of. Mistyped values are dropped rather than trusted.cross-version-terminal-wire.unit.test.ts: 10 passed.User-facing regressions
One accepted cost, stated plainly: leases expired by Reset Relay now enter the bulk set. A reset relay is fresh, so
pty.attachanswers not-found andssh-relay-session.ts:2838re-expires it — the same convergenceadoptStablePanealready reaches after #17965, just earlier. The stamping above bounds it to one stale row per pane until that pane next spawns.Otherwise none. Negative controls pin every exclusion: superseded siblings, recycled ids, and
terminatedleases are all still excluded from bulk reattach, and the cardinality suite stays flat.Testing
Before (source reverted, new tests in place):
After:
Test Files 4 passed (4) / Tests 90 passed (90).Regression sweep across
src/main/ssh,persistence,ipc,providers,runtime,src/relay: 14602 passed | 44 skipped.tc:node/tc:web/tc:cliclean.check:code-quality:changed: 0 new findings across 31 files.Toward consolidation
This is the vocabulary prerequisite for merging SSH into the orca remote: every lease state now means exactly one thing, so the two subsystems can share one predicate instead of each re-deriving intent from an overloaded enum. Several bugs in this sweep (#17957, #17962, #17965) had the same root cause — one path guarding a hazard its twin did not.