Skip to content

fix(relay): guard all three delivery release paths, and explain a restoring source - #18038

Merged
nwparker merged 1 commit into
mainfrom
port/restore-required-guards
Sep 2, 2026
Merged

nwparker merged 1 commit into
mainfrom
port/restore-required-guards

Conversation

@nwparker

@nwparker nwparker commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​182 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​182
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​28 $\color{#cf222e}{\Huge{\mathbf{−}}}$​13 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​15

Replaces #18016. Ports only what the #17951 lease stack does not already have, expressed in the stack's existing vocabulary.

Why this exists: #18016 and the #17951 stack independently fixed the same defect with parallel vocabularies (SSH_SOURCE_RESTORE_REQUIRED vs SSH_PTY_SOURCE_RESTORE_REQUIRED_ERROR, retry 3 vs 2, isProvenSshSessionGoneError vs isSshSessionGoneError), conflicting on six files. #17951 is an ancestor of four PRs, so its vocabulary wins and #18016 should close in favour of this.

Item 1 — the three-site ownership guard. Zero sites were guarded, not one.

I reported that the stack guarded one of three release/cancel sites in activate(). Checked: src/relay/relay-pty-source-publication.ts at the stack tip is byte-identical to main for this file. None of the three were guarded.

All three now derive ownership once and act only on a record the caller still owns:

const owned = current?.clientId === context?.clientId ? current : undefined

Three configurations of the same new test, verbatim:

configuration result
unguarded (stack tip) FAIL — AssertionError: expected true to be false at relay-pty-source-superseded-activation.test.ts:151:81
first site only FAIL — identical assertion, same line
all three guarded PASS

So the one-site fix #18016 shipped would not have closed the bug it targeted. The live path is the unadmitted/subscriber branch: a superseded transport released the rotation fence of the delivery its own replacement had opened, so a publish landed on a delivery still rotating.

Two deliberate non-ports, both flagged rather than silently dropped:

  • clientTransportGeneration / sameClientTransport — that is fix(ssh): stop treating a live PTY's retired source delivery as proof of death #18016's second root cause (delivery record keyed on client id alone) and needs a new RequestContext.transportGeneration field the stack lacks. Ownership here is expressed with the stack's existing record.clientId === context.clientId, the same comparison activate() already makes twice below.
  • context.isStale() in the first bail-out — a separate behavior change; out of scope.

One forced adjustment: the guard pushed the file to 301 counted lines against the 300 max-lines cap. Rather than a disable (forbidden), the one-line private wrapper deliveryClosedUnderRecord was inlined at its two call sites.

Residual behavior note, stated: an unowned record whose calling client is a legacy-owner is no longer cancelled by that caller. It stays owned by its actual source-owner client and is retired by that client's own settlement/exit path — which is the point.

Item 2 — toast handling. Was missing, ported.

SSH_PTY_SOURCE_RESTORE_REQUIRED was matched nowhere in TerminalErrorToast.tsx, so the raw marker reached the user. It now has its own pattern, an isExplainedTerminalError entry, its own humanizeTerminalError replacement, and a sourceRestoring key.

Kept deliberately out of UNREATTACHABLE_SESSION_SOURCES — that copy says "Open a new terminal", which here abandons a running agent.

The pattern is widened over #18016's, because the stack's message carries a trailing reason word that #18016's did not:

'SSH_PTY_SOURCE_RESTORE_REQUIRED(?::[ \t]*\S*(?:[ \t]+\S+)?)?'

Both new tests fail before / pass after:

  • describes a retired output source as reconnecting, not as a lost session — before: expected 'SSH_PTY_SOURCE_RESTORE_REQUIRED: remo…' not to contain 'SSH_PTY_SOURCE_RESTORE_REQUIRED'
  • suppresses the issue link while a live session restores its output source — before: expected false to be true

Item 3 — render-time humanization. Already present, skipped, no code written.

grep -rn "humanizeTerminalError" src/ returns exactly two hits, both in TerminalErrorToast.tsx: the definition and const displayError = humanizeTerminalError(error) at render. Nothing humanizes before storing, remote-runtime-pty-transport.ts stores the raw message, and markRecoveryHealthy replays those raw strings — so clearPaneTerminalError's match still lines up. #18016's change to that file was an import swap only.

User-facing regressions

None expected. The stack's token, retry count and predicate are untouched. Nothing certifies death from a pty.attach not-found (see #18029, which reverts exactly that).

Testing

Both new/changed suites: 36 passed. Full relay pty-source set (12 files) + dispatcher + ssh-pty-consumer-session-adapter: 148 passed. All 421 terminal-pane suites: 4120 passed. tc:node and tc:web clean; check:code-quality:changed 0 new findings across 47 files; check:max-lines-ratchet OK with no new bypasses.

Not ported

src/shared/ssh-pty-failure-tokens.ts, isProvenSshSessionGoneError (a no-op over the stack's predicate once tokens are disjoint), the = 3 retry constant, and the duplicate reattach/absence-verdict tests.

@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

  • Relay ownership guard — activate() in src/relay/relay-pty-source-publication.ts now derives owned once and scopes all three bail-outs (the !context?.onResponseSettled and unadmitted/subscriber fence releases, and the legacy-owner cancel) to the record the caller actually owns, so a superseded transport can no longer release or cancel the delivery its own replacement opened.
  • Toast handling — SSH_PTY_SOURCE_RESTORE_REQUIRED gets a widened match pattern, an isExplainedTerminalError entry, a humanizeTerminalError replacement, and an i18n sourceRestoring key, deliberately kept out of UNREATTACHABLE_SESSION_SOURCES.
  • Tests — a new superseded-activation integration test plus two toast tests; I confirmed the relay test genuinely fails (expected true to be false) with the guard reverted, so the coverage is real.
  • Line budget — the deliveryClosedUnderRecord wrapper was inlined to stay under the 300-line max-lines cap (file sits at 298 counted lines, no new bypass).

The guard is complete (the two remaining current?.clientId === context.clientId paths were already owner-checked), the deliberately non-ported items are clearly documented, and the residual legacy-owner behavior change is intentional and matches the lease stack's ownership model.

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

…r owns

Two of the three release/cancel sites in RelayPtySourcePublication.activate()
acted on `current` unconditionally. A superseded transport re-entering activate()
therefore released — or cancelled and deleted — the delivery its own replacement
had just opened: releasing the fence resumes a send the replacement is still
rotating, and retiring it blanks the pane that owns it. The live path is the
unadmitted/subscriber branch, so guarding only the first site leaves the defect
exactly as it was; all three now act only on a record the caller still owns.

Also give the restore-required token its own toast copy. It must not join
UNREATTACHABLE_SESSION_SOURCES: that copy says "Open a new terminal", which here
abandons a running agent on a PTY the relay has just proven alive
(docs/reference/ssh-execution-boundary.md).
@nwparker
nwparker force-pushed the port/restore-required-guards branch from 2f53970 to 1fc250a Compare September 2, 2026 05:52
@nwparker
nwparker changed the base branch from fix/ssh-lease-liveness-gate to main September 2, 2026 05:52
@nwparker

nwparker commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Retargeted to main carrying only this PR's own commit (rebased off the #17951 stack via --onto, --force-with-lease; git diff origin/main is byte-identical to the original 2f539706ca8).

Note for readers: on main alone, nothing yet emits SSH_PTY_SOURCE_RESTORE_REQUIRED — the producer (SSH_PTY_SOURCE_RESTORE_REQUIRED_ERROR in ssh-pty-session-reattach.ts) lives in #17951. The TerminalErrorToast branch here is inert until that stack lands, at which point it activates with no further change (regex verified against the exact thrown format). The relay ownership guard in relay-pty-source-publication.ts is independent and live immediately.

@coderabbitai

coderabbitai Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: de2935cb-93bc-487d-9f8c-ad747369f240

📥 Commits

Reviewing files that changed from the base of the PR and between 7f4a17d and 1fc250a.

📒 Files selected for processing (5)
  • src/relay/relay-pty-source-publication.ts
  • src/relay/relay-pty-source-superseded-activation.test.ts
  • src/renderer/src/components/terminal-pane/TerminalErrorToast.test.ts
  • src/renderer/src/components/terminal-pane/TerminalErrorToast.tsx
  • src/renderer/src/i18n/locales/en.json

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The relay now limits rotation-fence release, cancellation, and wake operations to the delivery owner. Superseded activation coverage verifies that replacement deliveries remain active. The renderer now recognizes SSH_PTY_SOURCE_RESTORE_REQUIRED, displays localized reconnecting-session text, classifies the error as explained, and omits new-terminal guidance. Tests cover the new relay and renderer behavior.

Merge Risk: 🔵 Low · up to 1fc25

The PR prevents superseded clients from releasing or cancelling another client's PTY delivery and replaces a raw restoration marker with clear user messaging. A bounded follow-up risk remains for reconnects that reuse the same client identity, where stale cleanup could briefly disrupt that client's delivery; merge is reasonable with owner awareness.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements the three-site delivery ownership guard and restore-required toast handling from [#18016]. However, [#18016] also requires distinct failure tokens, bounded retry changes, and transpo… Implement the remaining [#18016] requirements, or split them into explicitly linked follow-up issues and revise the linked issue scope. At minimum, address transport-generation ownership and verify the required failure-token and retry behav…
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes both primary changes: guarding all three delivery release paths and explaining restore-required PTY source errors.
Description check ✅ Passed The description provides detailed context, rationale, linked issue information, implementation scope, exclusions, and test results. It does not include the template's explicit Visual Proof, AI Disclos…
Out of Scope Changes check ✅ Passed The changed production code, locale string, and tests directly support PTY delivery ownership protection and restore-required error handling. No unrelated code changes are evident.
Full details: Description check

Explanation

The description provides detailed context, rationale, linked issue information, implementation scope, exclusions, and test results. It does not include the template's explicit Visual Proof, AI Disclosure, Review, Notes, or Checklist sections, but the core description is substantially complete.

Full details: Linked Issues check

Explanation

The PR implements the three-site delivery ownership guard and restore-required toast handling from [#18016]. However, [#18016] also requires distinct failure tokens, bounded retry changes, and transport-generation ownership. The PR explicitly omits those changes, including the transport-generation fix for the second ownership root cause.

Resolution

Implement the remaining [#18016] requirements, or split them into explicitly linked follow-up issues and revise the linked issue scope. At minimum, address transport-generation ownership and verify the required failure-token and retry behavior.

Full details: Docstring Coverage

Explanation

Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

@nwparker
nwparker merged commit 3d3b4f9 into main Sep 2, 2026
41 checks passed
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Sep 3, 2026
…r owns (stablyai#18038)

Two of the three release/cancel sites in RelayPtySourcePublication.activate()
acted on `current` unconditionally. A superseded transport re-entering activate()
therefore released — or cancelled and deleted — the delivery its own replacement
had just opened: releasing the fence resumes a send the replacement is still
rotating, and retiring it blanks the pane that owns it. The live path is the
unadmitted/subscriber branch, so guarding only the first site leaves the defect
exactly as it was; all three now act only on a record the caller still owns.

Also give the restore-required token its own toast copy. It must not join
UNREATTACHABLE_SESSION_SOURCES: that copy says "Open a new terminal", which here
abandons a running agent on a PTY the relay has just proven alive
(docs/reference/ssh-execution-boundary.md).

Mirror-Of: 3d3b4f9
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…r owns (stablyai#18038)

Two of the three release/cancel sites in RelayPtySourcePublication.activate()
acted on `current` unconditionally. A superseded transport re-entering activate()
therefore released — or cancelled and deleted — the delivery its own replacement
had just opened: releasing the fence resumes a send the replacement is still
rotating, and retiring it blanks the pane that owns it. The live path is the
unadmitted/subscriber branch, so guarding only the first site leaves the defect
exactly as it was; all three now act only on a record the caller still owns.

Also give the restore-required token its own toast copy. It must not join
UNREATTACHABLE_SESSION_SOURCES: that copy says "Open a new terminal", which here
abandons a running agent on a PTY the relay has just proven alive
(docs/reference/ssh-execution-boundary.md).
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