Skip to content

fix(runtime): reject stale inventory after PTY lifecycle changes - #21014

Merged
nwparker merged 2 commits into
mainfrom
np-oom-scan-stale-pty-inventory
Sep 20, 2026
Merged

nwparker merged 2 commits into
mainfrom
np-oom-scan-stale-pty-inventory

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​252 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​252
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​447 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​446

ELI5

A terminal inventory can arrive after the terminal has already closed or been replaced. Orca could then use that old list to reconnect the wrong terminal. This change rejects an inventory that became outdated while it was being fetched.

What Changed

Invalidate an in-flight inventory when an accepted spawn, registration, or exit changes ownership. The existing stale-response check rejects the whole result before it updates owners or processes missing entries.

Why

An old “this terminal exists” or “this terminal is missing” answer is unsafe after ownership changes. Reusing the existing generation check avoids another registry or wire field.

Tradeoff

A lifecycle race can require fetching inventory again. Targeted reads keep one retry within the original deadline; other hosts remain independent.

Linked Issue

Related to #19018. This is primarily an ownership-correctness fix, not a measured incident-scale OOM fix.

Visual Proof

No before/after UI capture is attached; this is a lifecycle behavior change. Automated behavior evidence is linked under Testing.

Testing

The recorded proof covers stale positive and absent rows, reused IDs, replacement terminals, host isolation, fresh discovery, and bounded retries. 22 before/after proof cases plus 16 adjacent regression cases are recorded in docs/audits/stale-pty-inventory/.

Review

The stale-result guard is host-scoped: it covers local and WSL PTYs, direct SSH providers, folder workspaces, and paired sessions on the runtime that owns them. A paired client does not substitute its local inventory for a remote host's inventory. The targeted proof covers 22 before/after cases plus 16 adjacent regressions, and current typecheck, static analysis, and full CI pass. A lifecycle race can still require the documented bounded retry.

@coderabbitai

coderabbitai Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

📝 Walkthrough

Walkthrough

The runtime now invalidates pending PTY controller inventory when a PTY spawns, exits, or registers. It records invalidation by provider or aggregate execution host and uses canonical provider keys. New fixtures and tests cover stale responses, lifecycle replacements, host routing, concurrent generations, retries, and deadlines. Audit artifacts record 16 failing baseline cases and 22 passing fixed cases.

Priority: ➖ Normal

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 8 files. (3 skipped: 3 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly identifies the primary change: rejecting stale inventory after PTY lifecycle changes. It is concise and directly related to the pull request.
Description check ✅ Passed The description is mostly complete. It includes the ELI5 summary, changed behavior, rationale, tradeoff, linked issue reference, visual-proof explanation, and testing summary. It does not include the …
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 8 files. (3 skipped: 3 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.

@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: 286d57ba-bce6-4e28-8bd8-c573f59105ef

📥 Commits

Reviewing files that changed from the base of the PR and between 291b4dd and 099f339.

📒 Files selected for processing (10)
  • docs/audits/stale-pty-inventory/README.md
  • docs/audits/stale-pty-inventory/fix.patch
  • docs/audits/stale-pty-inventory/reproduce.mjs
  • docs/audits/stale-pty-inventory/results.json
  • src/main/runtime/orca-runtime-invalidate-all-handles-for-pty.ts
  • src/main/runtime/orca-runtime-on-pty-exit.ts
  • src/main/runtime/orca-runtime-register-pty.ts
  • src/main/runtime/pty-inventory-lifecycle-admission.test.ts
  • src/main/runtime/pty-inventory-lifecycle-fixture.ts
  • src/main/runtime/pty-inventory-lifecycle-scope.test.ts

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

@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 lifecycle fence is keyed differently from the reader it must trip, so it silently no-ops for SSH targets whose id needs percent-encoding (e.g. [email protected]:2222) — the stale census this PR is meant to reject is still accepted there.

Reviewed changes

  • Lifecycle inventory fence — invalidatePtyControllerInventoryForLifecycle bumps the provider inventory generation from onPtySpawned, onPtyExit, and registerPty, so the existing isCurrentInventory check rejects the entire pending census before handle adoption, record mutation, or absence processing.
  • Scoping — foreign/runtime hosts bump the aggregate generation and return; SSH/local hosts bump a per-provider entry; targeted reads keep their single retry and original deadline; no wire fields or per-PTY registry.
  • Tests — 18 new cases (admission + scope/retry bounds) over a shared fixture, plus the before/after audit harness under docs/audits/stale-pty-inventory/. I reran the two new suites and the adjacent inventory suites (51 passed).

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

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

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Use the aggregate generation when PTY ownership is unknown. · fix.patch:35

docs/audits/stale-pty-inventory/fix.patch:35
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Use the aggregate generation when PTY ownership is unknown.

SSH relay restoration calls onPtySpawned without a connectionId or registerPty record. For a legacy unqualified appPtyId, getPtyExecutionHost returns null, so invalidation updates LOCAL_EXECUTION_HOST_ID. A targeted SSH inventory can then still satisfy isCurrentInventory because its SSH provider generation and the aggregate generation are unchanged. When both hostId and connection are absent, update ptyControllerAggregateInventoryGeneration instead.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: defc04e5-8853-4b15-8c76-aec8269e5386

📥 Commits

Reviewing files that changed from the base of the PR and between 099f339 and f940d76.

📒 Files selected for processing (6)
  • docs/audits/stale-pty-inventory/README.md
  • docs/audits/stale-pty-inventory/fix.patch
  • docs/audits/stale-pty-inventory/reproduce.mjs
  • docs/audits/stale-pty-inventory/results.json
  • src/main/runtime/orca-runtime-refresh-pty-worktree-records-with-controller-inventory.ts
  • src/main/runtime/pty-inventory-lifecycle-scope.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/runtime/pty-inventory-lifecycle-scope.test.ts
  • docs/audits/stale-pty-inventory/README.md

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

@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

The delta since the prior pullfrog review (099f339) canonicalizes the provider inventory generation key on the reader side and adds encoding-sensitive regression coverage for the previously flagged mismatch.

  • Canonicalized the reader's provider key — refreshPtyWorktreeRecordsWithControllerInventory now derives providerKey via toSshExecutionHostId(connectionId) / LOCAL_EXECUTION_HOST_ID instead of the raw `ssh:${connectionId}` / 'local', matching what invalidatePtyControllerInventoryForLifecycle writes.
  • Added encoding-sensitive regression tests — the scope suite now runs the legacy-unqualified and ssh:<id>@@child stale-inventory cases over [email protected]:2222 and ssh target, the ids the fence previously missed.
  • Updated the audit record — README.md/results.json now report 16 failing / 6 passing before and 22 passing after, and reproduce.mjs asserts process exit codes in addition to pass/fail counts.

I independently confirmed the new tests are not theatre: swapping the pre-fix reader back in makes both encoding-sensitive cases fail with the stale row adopted (pending non-null), and they pass on the head. Head CI is green (16 success, 12 skipped, 1 in progress, 0 failures).

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

@nwparker
nwparker merged commit 4d82149 into main Sep 20, 2026
29 checks passed
@nwparker
nwparker deleted the np-oom-scan-stale-pty-inventory branch September 20, 2026 00:24
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.

2 participants