Skip to content

fix(runtime): reject leaf terminal sends only on controller-proven PTY absence - #12578

Merged
Jinwoo-H merged 4 commits into
mainfrom
stale-write-proven-absence-guard
Aug 5, 2026
Merged

Jinwoo-H merged 4 commits into
mainfrom
stale-write-proven-absence-guard

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Stacked on #12574 (contains its two commits).

Summary

orca terminal send to a leaf handle whose ptyId no provider in this process owns was a silent no-op reported as success: the graph mirror answers writable: true, every provider write to an unknown id is fire-and-forget, and bytesWritten is computed from the payload rather than delivery. Automation could "successfully" type into terminals that no longer exist (confirmed chain in the run6 incident investigation; the same fabricated-receipt behavior was observed live on a remote host in the v1.4.169-rc.0 blank-terminals handoff).

  • The pty controller now exposes probePtyLiveness, routed like write: daemon router/degraded/adapter probes delegate (2s-bounded, false only when every owner refuses); a raw LocalPtyProvider is its own sole owner so its hasPty === false is authoritative; SSH-without-probe and remote: scopes answer null with zero I/O.
  • sendTerminal / sendTerminalAgentPrompt leaf branches reject only on an exact false — unknown liveness, probe errors, and probe-less providers always proceed (fix(pty): require proven absence before retiring a stable pane owner #12393's rule: null is not absence), so a restored daemon session still accepts writes before its pane remounts. Existing terminal_not_writable token; CLI handling unchanged.
  • Hot path is zero-cost: a provider-known id (sync hasPty) never probes and supersedes any cached verdict; proven-absent verdicts cache for 15s with in-flight dedupe.
  • Push-on-idle orchestration delivery gains the same gate: a proven-dead leaf keeps its messages queued instead of marking them delivered into a void.

Test plan

  • New terminal-send-stale-leaf-liveness.test.ts (11): proven-absent rejects with write never called; null/throwing probe proceeds; true (restored-before-remount) proceeds; provider-known id never probes; repeated sends probe once; verdict dropped when the provider re-learns the id; orchestration messages stay queued on proven absence.
  • 6 new controller-routing tests in ipc/pty.test.ts (local sole-owner absence, probe delegation, probe-less/SSH/remote/throwing → null); full file 440 ✅.
  • Full orca-runtime.test.ts 1022 ✅; tsc node ✅; oxlint ✅. Independently re-verified by the coordinating session (15/15).

Review notes

  • "Live pty record" is deliberately defined as the provider's sync hasPty, not ptysById — graph sync fabricates a connected: true record for every leaf ptyId, so runtime records cannot distinguish live from stale.
  • Strongest inference: raw local hasPty === false = proven absence (in-process PTYs cannot outlive the process).
  • The orchestration-delivery site becomes conditionally async only when a probe exists and the provider doesn't know the id; the sync hot path is untouched.

OrcaWin added 4 commits August 4, 2026 20:21
…Y absence

orca terminal send to a leaf whose ptyId no provider in this process owns
was a silent no-op reported as success: the graph mirror answers
writable=true, every provider write to an unknown id is accepted
fire-and-forget, and bytesWritten is computed from the payload rather than
delivery. sendTerminal and sendTerminalAgentPrompt now consult a controller
liveness probe when the provider does not synchronously know the id
(hasPty), and throw terminal_not_writable only on an exact false — unknown
liveness, probe errors, SSH/remote scopes, and probe-less providers never
reject (#12393's rule: null is not absence), so a restored daemon session
still accepts writes before its pane remounts. Push-on-idle orchestration
delivery gains the same gate so a proven-dead leaf keeps its messages
queued instead of marking them delivered into a void.

The pty controller now exposes probePtyLiveness, routed like write: a
provider probe is preferred, the in-process local provider's refusal is
authoritative (sole owner), and remote-scoped or SSH ids without a probe
answer null after awaiting the cold-start daemon swap. Proven-absent
verdicts cache 15s per ptyId with in-flight dedupe, superseded the moment
the provider re-learns the id.
Review (GPT verifier) confirmed: triggers arriving during one in-flight
absence probe each attached a continuation to the deduped probe promise, and
since Claude-target delivered_at stamps only after the delayed Enter, every
continuation re-read the same unread rows — double payload injection and two
armed Enters. Single-flight the deferred continuation per pty; the one armed
continuation re-reads fresh rows when it fires, so nothing is lost, and the
guard clears on settle so later triggers defer again. The narrower
pre-existing 500ms sync-path window is unchanged and out of scope.
…er pty

The probe-continuation guard cleared at probe settle, but Claude-target
delivered_at stamps only in the delayed-Enter callback ~500ms later — a
trigger landing in that gap armed a fresh probe cycle, re-read the same
un-stamped rows, and re-injected the payload. The identical window existed
on the pure sync path pre-PR (two triggers within 500ms double-deliver).

Hold a per-pty delivery-in-flight flag from before the payload write until
delivery settles: entry-checked before reading unread rows, cleared through
one settle point covering the failed write, the sync-stamped coordinator and
Cursor branches, any sync throw, and the delayed-Enter callback on submit,
refusal, and throw alike. A trigger arriving mid-flight is not dropped — it
parks the latest leaf per ptyId and re-runs delivery once on settle, so rows
inserted mid-flight deliver without waiting for the next idle event. The
probe single-flight stays; the new guard subsumes its post-settle gap, and
no trigger site bypasses it.

Both strengthened tests are red on the previous commit (first subject
injected twice) and green here: in-window re-trigger on the probe path and
sync-path double-trigger each deliver the first batch exactly once, with the
parked second row delivering alone after settle.
…-time on current state

Two variants of one root cause — the delayed-Enter callback outliving the
session it was armed for:

1. Cold restore respawns under the same session id. onPtyExit never
   cancelled the armed Enter or the in-flight delivery state, and
   onPtySpawned flips the same leaf writable again — so an exit + same-id
   respawn inside the 500ms window let the stale callback inject \r into
   the replacement session and stamp rows it never received, then settle
   against a newer same-id flight.
2. Graph resync replaces leaf objects, so onPtyExit flips writable=false
   only on the current replacement; a callback trusting its closed-over
   snapshot still read writable=true and fired after exit with no respawn.

The flight record now carries its armed Enter timer and serves as settle
identity: onPtyExit clears the timer and drops the flight and any parked
re-delivery without stamping (rows stay unstamped and re-deliver on the
replacement's next idle — the existing contract), and settle no-ops unless
its own flight is still current, so a stale settle can never clear a newer
same-id flight or flush its parked trigger. At fire time the callback
re-resolves the leaf by key and requires the same ptyId binding and current
writability instead of reading the closure snapshot.

All three regressions are red on the previous commit: same-id respawn saw
\r plus a false delivered_at stamp, exit leaked the flight and parked
state, and the orphaned-snapshot resync variant fired Enter after exit.
@Jinwoo-H
Jinwoo-H force-pushed the stale-write-proven-absence-guard branch from 751d51c to 5dfca16 Compare August 5, 2026 00:21
@Jinwoo-H
Jinwoo-H merged commit 3d8131d into main Aug 5, 2026
40 checks passed
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 845edb30-db94-4dc1-8c18-145b01db472d

📥 Commits

Reviewing files that changed from the base of the PR and between 27da04d and 5dfca16.

📒 Files selected for processing (4)
  • src/main/ipc/pty.test.ts
  • src/main/ipc/pty.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/runtime/terminal-send-stale-leaf-liveness.test.ts

📝 Walkthrough

Walkthrough

The PTY controller now exposes asynchronous liveness probing with provider routing and fail-closed unknown results. The runtime caches proven-absent leaf PTYs, gates terminal writes on confirmed absence, and preserves writes for unknown states. Pending-message delivery now uses per-PTY flights, parked retriggers, probe deferral, cleanup retirement, and identity checks before delayed writes. Tests cover provider routing, liveness caching, terminal-send behavior, delivery deduplication, PTY replacement, and stale graph snapshots.

✨ Finishing Touches
📝 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.

@Jinwoo-H
Jinwoo-H deleted the stale-write-proven-absence-guard branch August 5, 2026 00:25
@AmethystLiang

Copy link
Copy Markdown
Contributor

thanks for reporting. looking into it

enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…Y absence (stablyai#12578)

* fix(runtime): reject leaf terminal sends only on controller-proven PTY absence

orca terminal send to a leaf whose ptyId no provider in this process owns
was a silent no-op reported as success: the graph mirror answers
writable=true, every provider write to an unknown id is accepted
fire-and-forget, and bytesWritten is computed from the payload rather than
delivery. sendTerminal and sendTerminalAgentPrompt now consult a controller
liveness probe when the provider does not synchronously know the id
(hasPty), and throw terminal_not_writable only on an exact false — unknown
liveness, probe errors, SSH/remote scopes, and probe-less providers never
reject (stablyai#12393's rule: null is not absence), so a restored daemon session
still accepts writes before its pane remounts. Push-on-idle orchestration
delivery gains the same gate so a proven-dead leaf keeps its messages
queued instead of marking them delivered into a void.

The pty controller now exposes probePtyLiveness, routed like write: a
provider probe is preferred, the in-process local provider's refusal is
authoritative (sole owner), and remote-scoped or SSH ids without a probe
answer null after awaiting the cold-start daemon swap. Proven-absent
verdicts cache 15s per ptyId with in-flight dedupe, superseded the moment
the provider re-learns the id.

* fix(runtime): arm one probe-deferred delivery continuation per pty

Review (GPT verifier) confirmed: triggers arriving during one in-flight
absence probe each attached a continuation to the deduped probe promise, and
since Claude-target delivered_at stamps only after the delayed Enter, every
continuation re-read the same unread rows — double payload injection and two
armed Enters. Single-flight the deferred continuation per pty; the one armed
continuation re-reads fresh rows when it fires, so nothing is lost, and the
guard clears on settle so later triggers defer again. The narrower
pre-existing 500ms sync-path window is unchanged and out of scope.

* fix(runtime): single-flight the whole orchestration delivery window per pty

The probe-continuation guard cleared at probe settle, but Claude-target
delivered_at stamps only in the delayed-Enter callback ~500ms later — a
trigger landing in that gap armed a fresh probe cycle, re-read the same
un-stamped rows, and re-injected the payload. The identical window existed
on the pure sync path pre-PR (two triggers within 500ms double-deliver).

Hold a per-pty delivery-in-flight flag from before the payload write until
delivery settles: entry-checked before reading unread rows, cleared through
one settle point covering the failed write, the sync-stamped coordinator and
Cursor branches, any sync throw, and the delayed-Enter callback on submit,
refusal, and throw alike. A trigger arriving mid-flight is not dropped — it
parks the latest leaf per ptyId and re-runs delivery once on settle, so rows
inserted mid-flight deliver without waiting for the next idle event. The
probe single-flight stays; the new guard subsumes its post-settle gap, and
no trigger site bypasses it.

Both strengthened tests are red on the previous commit (first subject
injected twice) and green here: in-window re-trigger on the probe path and
sync-path double-trigger each deliver the first batch exactly once, with the
parked second row delivering alone after settle.

* fix(runtime): retire the armed delivery Enter on pty exit; guard fire-time on current state

Two variants of one root cause — the delayed-Enter callback outliving the
session it was armed for:

1. Cold restore respawns under the same session id. onPtyExit never
   cancelled the armed Enter or the in-flight delivery state, and
   onPtySpawned flips the same leaf writable again — so an exit + same-id
   respawn inside the 500ms window let the stale callback inject \r into
   the replacement session and stamp rows it never received, then settle
   against a newer same-id flight.
2. Graph resync replaces leaf objects, so onPtyExit flips writable=false
   only on the current replacement; a callback trusting its closed-over
   snapshot still read writable=true and fired after exit with no respawn.

The flight record now carries its armed Enter timer and serves as settle
identity: onPtyExit clears the timer and drops the flight and any parked
re-delivery without stamping (rows stay unstamped and re-deliver on the
replacement's next idle — the existing contract), and settle no-ops unless
its own flight is still current, so a stale settle can never clear a newer
same-id flight or flush its parked trigger. At fire time the callback
re-resolves the leaf by key and requires the same ptyId binding and current
writability instead of reading the closure snapshot.

All three regressions are red on the previous commit: same-id respawn saw
\r plus a false delivered_at stamp, exit leaked the flight and parked
state, and the orphaned-snapshot resync variant fired Enter after exit.

---------

Co-authored-by: OrcaWin <[email protected]>
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.

3 participants