Repository navigation
fix(terminal): detect foreground from the physical PTY owner - #36342
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review.
|
There was a problem hiding this comment.
Actionable comments posted: 1

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
58fa6438-25dd-4ed9-9648-113546cbf9d0
Files ignored due to path filters: 1-
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
Files selected for processing: 63-
config/patches/[email protected]src/main/daemon/daemon-foreground-process-protocol.tssrc/main/daemon/daemon-protocol-version.tssrc/main/daemon/daemon-pty-adapter-session-adoption.test.tssrc/main/daemon/daemon-pty-adapter-steady-state-compat.test.tssrc/main/daemon/daemon-pty-process-inspection.tssrc/main/daemon/daemon-pty-router-inventory.test.tssrc/main/daemon/daemon-pty-router-test-fixture.tssrc/main/daemon/daemon-pty-router.tssrc/main/daemon/daemon-pty-session-inventory.tssrc/main/daemon/daemon-pty-target-inspection.tssrc/main/daemon/daemon-request-router.tssrc/main/daemon/daemon-session-owner-resolution.tssrc/main/daemon/pty-subprocess-foreground-identity.test.tssrc/main/daemon/pty-subprocess/spawn-file-foreground-process.tssrc/main/daemon/pty-subprocess/subprocess-handle.tssrc/main/daemon/session-process-inspection.tssrc/main/daemon/session-subprocess-handle.tssrc/main/daemon/session.tssrc/main/daemon/terminal-host-process-inspection.test.tssrc/main/daemon/terminal-host-process-inspection.tssrc/main/daemon/terminal-host-session-inspection-operations.tssrc/main/daemon/terminal-host.tssrc/main/ipc/pty/ipc/inspect.tssrc/main/ipc/pty/runtime/controller.tssrc/main/ipc/pty/runtime/inventory-operations.tssrc/main/ipc/pty/runtime/operations.tssrc/main/providers/local-pty-provider-foreground-process.test.tssrc/main/providers/local-pty-provider.tssrc/main/providers/local-pty-session-operations.tssrc/main/providers/pty-foreground-confirmation.tssrc/main/providers/pty-process-inspection.tssrc/main/providers/pty-provider-contract.tssrc/main/pty/posix-pty-foreground-group.test.tssrc/main/pty/posix-pty-foreground-group.tssrc/main/pty/pty-master-foreground-native.test.tssrc/main/pty/pty-master-foreground-observation.test.tssrc/main/pty/pty-master-foreground-observation.tssrc/main/pty/terminal-process-rows.tssrc/main/runtime/launched-agent-foreground.test.tssrc/main/runtime/launched-agent-foreground.tssrc/main/runtime/launched-agent-write-guard.test.tssrc/main/runtime/launched-agent-write-guard.tssrc/main/runtime/rpc/methods/terminal/terminal-inspect-process-params.test.tssrc/main/runtime/rpc/methods/terminal/terminal-query-methods.tssrc/main/runtime/runtime-pty-controller-contract.tssrc/main/runtime/session-tabs/runtime-terminal-process-inspection.test.tssrc/main/runtime/session-tabs/runtime-terminal-process-inspection.tssrc/main/runtime/session-tabs/structured-session-tab-restoration.tssrc/main/runtime/terminal-foreground-capture.test.tssrc/main/runtime/terminal-foreground-group.tssrc/main/runtime/workspaces/workspace-activation.tssrc/main/runtime/workspaces/workspace-foreground-inspection.tssrc/preload/api/pty-api.tssrc/preload/api/pty-bridge-stream-and-serialization.tssrc/renderer/src/runtime/runtime-terminal-inspection.tssrc/shared/libproc-argv-completion.tssrc/shared/libproc-process-table.test.tssrc/shared/libproc-process-table.tssrc/shared/process-table-snapshot.tssrc/shared/rpc-contract/terminal-unary-params.tssrc/shared/terminal-foreground-observation.tssrc/shared/terminal-process-inspection.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
-
…ics around master-group foreground
Review round 1 fixes for the physical-owner foreground observation:
- Stop and liveness verification keep complete-inventory proof: the router's
scoped listProcesses({ptyIds}) is owner resolution again (absent = []).
Only the launch guard's old-owner fallback asks for an established
target's owner via an internal targetOwnerOnly option.
- Degraded daemon mode routes foreground observations and target reads to
the established owner, so an unreachable unrelated daemon cannot refuse a
known terminal there either.
- Display and name refreshes read the master's foreground group but take
member rows from the shared single-flight process-table snapshot; no
per-terminal ps per refresh. Recognized agent names skip the confirm scan
again, and builds without the native accessor keep the existing reader.
- Ordinary inspections keep their first-match routing; strict owner and
incarnation checks apply only to foreground decisions. The unused
foregroundOnly preload/IPC/RPC surface is removed.
- Local inventory forwards only the target scope, never caller deadlines.
- An interpreter running an agent script counts as that agent, as before;
a blank argv row no longer voids a terminal capture outside the
foreground group.
- Bump the packaged compatibility contract to daemon protocol 55 and move
the node-pty accessor above the process getter's doc comment.
There was a problem hiding this comment.
Actionable comments posted: 2

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
e16843f2-3187-470c-af08-fe0991779cb4
Files ignored due to path filters: 1-
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
Files selected for processing: 24-
config/patches/[email protected]src/main/daemon/daemon-pty-adapter-session-adoption.test.tssrc/main/daemon/daemon-pty-router-inventory.test.tssrc/main/daemon/daemon-pty-router.tssrc/main/daemon/daemon-pty-session-inventory.tssrc/main/daemon/daemon-pty-target-inspection.tssrc/main/daemon/degraded-daemon-owner-recovery.tssrc/main/daemon/degraded-daemon-pty-provider.test.tssrc/main/daemon/degraded-daemon-pty-provider.tssrc/main/daemon/pty-foreground-display-scan-scaling.test.tssrc/main/daemon/pty-subprocess/spawn-file-foreground-process.tssrc/main/daemon/terminal-host-process-inspection.test.tssrc/main/ipc/pty/runtime/inventory-operations.tssrc/main/providers/pty-provider-contract.tssrc/main/pty/pty-master-foreground-observation.test.tssrc/main/pty/pty-master-foreground-observation.tssrc/main/runtime/launched-agent-foreground.tssrc/main/runtime/runtime-pty-controller-contract.tssrc/main/runtime/terminal-foreground-capture.test.tssrc/main/runtime/terminal-foreground-group.tssrc/main/runtime/workspaces/workspace-foreground-inspection.tssrc/shared/local-build-compatibility-contract.jsonsrc/shared/local-build-compatibility-contract.tssrc/shared/process-table-snapshot.ts
Files skipped from review as they are similar to previous changes: 1-
- src/main/daemon/terminal-host-process-inspection.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.
-
…ndant foreground tests Remove two stray context lines from the src/unixTerminal.ts hunk so its body matches its header (strict patch parsers reject the mismatch), and update the lockfile patch hash. Prune foreground tests whose rows repeat branches the parity table already covers.
…tion age nonnegative Display refreshes filtered the whole shared process table per pane, so work grew with panes times processes. Group each snapshot by normalized TTY once, memoized by array identity, and look each pane up directly; the scaling contract now counts row TTY reads at N and 10N panes. Both foreground observation producers clamp capturedAgeMs at zero, so a wall-clock step back during the read no longer yields an envelope the validator rejects and the launch guard refuses.
…atibility suite Main removed daemon-pty-adapter-steady-state-compat.test.ts; the merge restored it with only this branch's block. Reuse the suite's existing inspection adapter helper so no new type assertion is added.
Review loop status — readyThe problem. Starting a worker on an agent could fail with The change users get. The process that owns the terminal now asks the terminal itself which process group is in the foreground (a new native accessor on the PTY), then resolves that group's live members. A healthy terminal no longer depends on unrelated services or a stale cache. A real shell in the foreground is still refused, and user-typed agents, aliases and shell functions are still accepted. No settings, saved data or copy changed. What review fixed (4 review rounds, 1 decider ruling; ledger has the full list):
Deferred (P3, none user-visible): a re-export shim; Windows reporting a capability it never delivers (no caller relies on it); two test-assertion nits; and a bot review note about a fresh display read whose group can move while its snapshot is captured. That one was checked: decision reads use the existing resolver ( Verified
Not verified: the historical reports' exact trigger. They carry no process or terminal traces, so this fixes the confirmed failure class without claiming those incidents were reproduced. The check-then-write race (an agent exiting between the check and the write) is unchanged and stated in the PR body. For Brennan's review
Spots where human judgment matters:
|
…2025/lu-foreground-detect # Conflicts: # src/main/daemon/degraded-daemon-pty-provider.test.ts # src/main/daemon/terminal-host-process-inspection.test.ts # src/main/runtime/workspaces/workspace-activation.ts











ELI5
A running terminal agent can currently refuse its first task because another retained terminal daemon cannot be inspected, or because Orca remembers an outdated terminal for a reused process ID. This change reads the foreground process group from the terminal itself, at the process that owns it. A healthy target no longer depends on unrelated terminal inventory or a stale PID-to-terminal cache.
What Changed
The existing shell/unknown admission rules and refusal text remain. Local non-shell admission still accepts user wrappers without requiring provider recognition. User-entered commands, aliases, shell functions, and launch commands are unchanged. No settings or saved fields are added.
Why
The common pattern starts with the physical terminal owner's master descriptor. Reconstructing that authority from a global application inventory lets one unrelated failed owner block a healthy terminal; caching terminal names by numeric PID also confuses PID reuse. The owner-held descriptor removes those dependencies. Strict capture validation prevents partial output from becoming positive evidence.
This repairs the confirmed failure class. The historical macOS coordinator-rebind and Linux daemon-update reports do not contain the PID/TTY traces needed to isolate their exact trigger, so this PR does not claim those incidents were faithfully reproduced. Foreground detection and a subsequent terminal write remain non-atomic: an agent can exit between them. The existing shell-return watcher and guarded input policy remain; this does not claim recipient-bound delivery.
Differences from the common pattern:
Linked Issue
Tracked in STA-9961.
Visual Proof
N/A — no visual layout or copy changes. This is the detector underneath existing terminal actions. Real terminal/agent and cross-platform QA remains for the review loop; no app run is claimed here.
Testing
CI is green on the head, including the patched native module built for Linux glibc/musl (x64 and arm64), macOS and Windows. The native accessor test runs on the rebuilt Linux module. Contracts cover:
Real-app QA used real Claude and Codex:
claude, acx() { command codex; }function and an alias were each recognized and delivered to. Redraw after a split was clean.cxfunction was delivered to.Review
Full path:
Agent skill upstream boundary
Notes
Review/QA must exercise macOS and Linux fresh/rebound terminals with Claude and Codex, manual commands and an argument-dropping shell function, shell return before dispatch, follow-up/status consumers, retained owners and mixed versions, and the affected Windows/managed WSL/SSH routes. Existing folder and git workspaces and new workspaces share the detector. There is no mobile layout change; mobile reads the existing host projection.
Checklist