Skip to content

fix(ssh): a briefly held fence is retried, not a failed update; terminate keeps unverifiable leases; idle-wake fence race - #25420

Merged
OrcaWin merged 1 commit into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-r3-ssh
Oct 5, 2026
Merged

OrcaWin merged 1 commit into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-r3-ssh

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​96 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​89
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​132 $\color{#cf222e}{\Huge{\mathbf{−}}}$​42 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​90

ELI5

This fixes two problems the final review found, plus a race in the idle-server end-to-end test:

  • Busy hosts: a host that was only busy for a moment could block managed-server updates for the rest of the app version.
  • Terminate: "terminate sessions" could record a terminal as dead when Orca could not actually tell.

What Changed

A briefly held activation fence no longer blocks updates for the app version. This is a regression from round 2.

  • Before: a fence held by another client for a few seconds answered at once with orcad_activation_recovery_required. So did a single failed lock command, which was counted as "busy". Auto-update treated that code as a failed update and skipped every later connect for this app version.
  • After:
    • The fence lock is retried for 5 seconds.
    • A fence still held answers the new orcad_activation_fence_busy. That code is a waiting deferral, so it is never recorded as a failed update, and the update is retried on a later connect.
    • "Recovery required" is kept for an interrupted run: a fence with a journal, or a lock older than the stale age (20 minutes).
    • acquireInstallLock reports Busy only when a holder actually answered (the BUSY marker, or a GC claim). A lock command that only ever failed surfaces its own error.

Terminate keeps leases it could not verify.

  • Before: a PTY that an older relay may still run was correctly counted as unverifiable. The final teardown then disposed the session, which bulk-marked every remaining lease, that one included, as terminated.
  • After: if any PTY came back unverifiable, the teardown detaches instead of disposing, so those leases stay detached. Only the leases this run proved terminated are marked terminated.

Idle-exit e2e (ssh-orcad-idle-exit.spec.ts:50), which also failed on other PRs

  • Likely second race: after a relaunch, the launch-time tunnel ensure wakes the idled server while holding the fence. The test's disconnect then cuts that connection. The fence stays on the host, because a lost exec is never assumed finished and the release can't reach the host. The connect that follows sees a held fence and refuses.
  • Fix: a wake now remembers that its own connection dropped while it held the fence. On this client's next wake, if there is no journal and the slot was just proven exited, it releases that fence and starts the slot. A fence another client might own is never touched.
  • Test helper: the e2e reconnect helper now throws with the connect's error text and main's SSH state, instead of a JSON parse error that hid it.

Why

  • The fence: a held fence that a live run will release soon should be retried, not reported as a failure. Only real evidence of an interrupted run (a journal, or a lock past its stale age) should ask for Recover.
  • Terminate: marking only what this run proved keeps "unknown" from being stored as "exited".

Linked Issue

N/A (Phase 3 round-3 review)

Visual Proof

N/A. There is no UI change.

Testing

  • pnpm tc:node passes.

  • pnpm test src/main/ssh src/main/ipc passes: 793 files, 8875 tests.

  • The changed-code gate passes.

  • New tests:

    • busy vs. recovery classification in deploy (fresh fence, stale fence, journal);
    • the short fence wait;
    • a lock command that only failed surfaces its own error;
    • auto-update treats fence-busy as waiting;
    • terminate's bulk mark stays detached (this test fails without the fix);
    • an interrupted wake releases its own fence, and never a fence from another target.
  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH execution boundary: unverifiable leases stay detached.
  • Wire: no wire change.

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)

…a failed update; terminate keeps held leases

- The activation fence is retried for a few seconds. A fence still held answers
  orcad_activation_fence_busy (a waiting deferral, never an update failure) unless a journal or
  a lock past its stale age shows an interrupted run, which stays recovery-required.
- acquireInstallLock reports Busy only when a holder answered; a lock command that only ever
  failed surfaces its own error.
- Terminate detaches instead of disposing when any PTY was unverifiable, so the final teardown
  never bulk-marks a lease an older relay may hold as terminated.
- A wake whose connection dropped while holding the fence releases that fence on this client's
  next wake (no journal, slot proven exited), so a relaunch-then-connect is not left fenced.
- The orcad e2e reconnect helper surfaces a connect's error text instead of a JSON parse error.

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

Important

The idle-wake fence auto-release can drop an activation fence it does not own. One inline finding; the rest of the PR reads sound.

Reviewed changes

  • Fence-hold classification split — new orcad-activation-fence-hold.ts resolves a held fence to orcad_activation_fence_busy (retry later) or orcad_activation_recovery_required (journal present, or lock past its stale age); deploy, rollback, and stop all route their held() through it, replacing the single orcad_activation_recovery_required code.
  • Short fence wait — withOrcadActivationLock now waits 5s (ORCAD_ACTIVATION_FENCE_WAIT_MS) before reporting held(), and exports releaseOrcadActivationFence.
  • Auto-update waits on a held fence — orcad_activation_fence_busy joins WAITING_CODES, so a briefly held fence defers instead of being recorded as a failed update for the app version.
  • Idle-wake fence recovery — a wake whose slot start lost its connection now releases the journal-less fence it left, tracked in a module-global interruptedWakes set.
  • Terminate keeps unverifiable leases — the final teardown detaches instead of disposing when any PTY was unverifiable, so only leases this run proved terminated are marked terminated.
  • Install-lock failure classification — a lock command that only ever failed now surfaces its own error rather than RemoteInstallLockBusyError, and new tests plus an e2e helper that reports the connect error text round it out.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Comment on lines +84 to +94
async function releaseOwnInterruptedWakeFence(
options: OrcadSlotOptions,
host: string
): Promise<boolean> {
if (!interruptedWakes.has(host) || (await readOrcadActivationTransaction(options))) {
return false
}
await releaseOrcadActivationFence(options)
interruptedWakes.delete(host)
return true
}

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.

This guard treats "key present + no journal" as proof the fence is this client's own, but neither half proves ownership. interruptedWakes is mutated only here and never cleared by any other fence-removal path, so a key survives a fence removed elsewhere (operator Recover, stop-cancel); the next wake on that target then deletes a journal-less fence a different run or client currently holds — another client's in-progress wake, or a deploy in the window between orcad-remote-deploy.ts:106 and the first journal write at orcad-installed-activation.ts:144. Consider proving ownership (a token written alongside the fence, verified before release) or clearing the key whenever the fence is absent, and add a test with a stale key rather than a fresh target id.

Technical details
# Wake fence release is not ownership-scoped

## Affected sites
- `src/main/ssh/orcad-managed-wake.ts:64` — `interruptedWakes.add(host)`, keyed by local `${conn.getTarget().id}\0${remoteHome}`.
- `src/main/ssh/orcad-managed-wake.ts:88` — the only check is `interruptedWakes.has(host) || readOrcadActivationTransaction(options)`.
- `src/main/ssh/orcad-managed-wake.ts:92` — the key is deleted only on a successful own-release, so it leaks process-lifetimes.
- Fence-removal paths that never clear the key: `orcad-activation-lock.ts:106`, `:112`, `:138`, `:162` (reached via `orcad-activation-recovery.ts:53` and `orcad-runtime-decommission.ts:165`).
- `src/main/ssh/orcad-remote-deploy.ts:106` → `src/main/ssh/orcad-installed-activation.ts:144` — a deploy holds a journal-less fence across preflight and admission before writing its journal.
- `src/main/ssh/orcad-managed-wake.test.ts:130` — the "never releases a fence another client may hold" test only passes a fresh target id, so it never exercises a stale key.

## Required outcome
- A wake may release only a fence it can prove it left. Either persist an owner token with the fence and verify it on release, or drop the auto-release and route the interrupted wake through the existing Recover path.
- On the shorter path: clear `interruptedWakes` for the host whenever `orcadActivationFenceExists` is false, and wrap the `readOrcadActivationTransaction(options)` call in `.catch(() => null)` (or treat an unreadable journal as `fenced`) so an unreadable journal does not reject the whole wake.

## Open questions for the human
- Is the auto-release the intended mechanism, or should an interrupted wake leave the fence for operator Recover?

@OrcaWin
OrcaWin merged commit 70d7db3 into OrcaWin/node-rt-phase3 Oct 5, 2026
94 of 95 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