Skip to content

perf(terminal): read the macOS process table in process instead of forking setuid ps - #35987

Merged
nwparker merged 13 commits into
mainfrom
fix/daemon-native-process-table-31783
Oct 29, 2026
Merged

nwparker merged 13 commits into
mainfrom
fix/daemon-native-process-table-31783

Conversation

@nwparker

@nwparker nwparker commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​933 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​933
Prod 29 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1510 $\color{#cf222e}{\Huge{\mathbf{−}}}$​153 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1357

ELI5

On macOS, Orca's terminal daemon launched /bin/ps (and lsof) about 1,000 times a minute to see what was running in each pane. Every launch of the setuid ps goes through macOS's security checks (syspolicyd), and through any endpoint-security agent such as Defender or Sophos. That kept syspolicyd at 70–110% CPU around the clock. The daemon now reads the process table directly from the OS, inside its own process, and ps is only a fallback.

What Changed

  • Native reads: the existing node-pty patch (config/patches/node-pty@1.1.0.patch) exports readProcessTable, processCwd and processPath on darwin. They use libproc and sysctl: proc_listallpids, PROC_PIDTBSDINFO, KERN_PROCARGS2, PROC_PIDVNODEPATHINFO and KERN_PROC_TTY. The table read runs on a worker thread. Build, signing and bundling are unchanged.
  • ps-identical output: src/shared/libproc-process-table.ts and libproc-ps-format.ts format rows exactly as /bin/ps prints them for every argument list Orca uses, so the existing parsers don't change. This covers lstart, tty names, stat flags, <defunct>, row order and -p dedupe. Arguments are escaped with Apple ps's strnvis in the caller's locale. A darwin-only test diffs the real addon against /bin/ps.
  • Freshness for signalling: each native read gets a JS-side sequence number when it's created. A caller only joins a read that was submitted after it registered, so rows from a read that started earlier can never drive a signal. There are no wall-clock comparisons, and every deadline uses performance.now(). A regression test steps the clock both ways and asserts that only a fresh descendant is signalled.
  • Bounded and resilient:
    • In-flight cap: at most 8 native reads run at once, each with one shared settle notifier, so nothing is retained after a timeout.
    • Circuit breaker: if the pool stalls, the native reader is marked unhealthy for 30 s and callers use ps.
    • Terminal naming: naming is single-flight. Canonical and strict captures go straight to /bin/ps while a naming pass is stuck, and readers fall back once a pass passes 500 ms.
    • Signalling paths: descendant teardown, foreground-group delivery and the daemon shutdown sweep give the native reader a slice of at most 150 ms. On failure they give ps its full original timeout, with each reader declaring a deadlineAllowanceMs, so a slow ps gets exactly the window it has on main.
  • Other users' processes: their argv can't be read without setuid ps. Those rows are marked incomplete. A targeted ps -p runs only for processes relevant to a user's pane (including sudo use_pty chains), cached for 30 s. Root daemons and GUI helpers never trigger it.
  • Fallback everywhere else: Linux, Windows, SSH hosts, older pty.node builds (the export was renamed, so they fall back), unsupported argument lists, and any error all use ps/lsof exactly as before.
  • Daemon protocol v54: new tabs start a daemon that has the fix. v54 keeps v53 attach compatibility, following the same pattern as earlier bumps (02a8ebe).

Why

Spawns from the daemon on macOS:

Scenario main this PR
4 agent panes, 60 s, steady state ~440 ps/lsof 0
24 panes polled at once at startup ~440 ps + 106 lsof 0
Per-tty foreground lookup 3.7 ms (/bin/ps) 0.29 ms p50

Per #27143 this is a substantial win. Every behaviour stays the same or improves, and ps remains the fallback on every error path.

Where this came from

The daemon's polling paths are:

  • process-table snapshots, with a 500 ms cache
  • the cheap fingerprint
  • shell foreground
  • the cwd lsof, with a 1.5 s cache
  • the growth monitor
  • agent presence
  • tmux attachment
  • the prompt-ready probe

Each of these forked setuid ps/lsof. The steady rate grew with the pane count, and stale daemons left over after updates (#23372) multiplied it. Reported in #31783 with syspolicyd and EDR measurements.

Linked Issue

Fixes #31783

Supersedes the macOS part of #35257, whose fingerprint shortcut becomes unnecessary when reads happen in process. #35257 still helps Linux.

Visual Proof

N/A: daemon-side change with no UI difference.

Testing

  • Focused tests: libproc unit tests, the real-addon /bin/ps parity test, and terminal-naming single-flight tests.

    • Regression tests: stale-read → wrong-pid with clock steps, a slow ps still reaching descendants and the foreground group, and the shutdown sweep with a stalled native read.
  • Wider suites: the affected suite (105 files / 1,164 tests) passes, and pnpm tc:node passes.

  • pnpm check:changed: 38/39 checks pass. The unit-stage failures (orcad-forked-child-paths, source-control-wsl-prompt, persisted-profile-state.unit) fail the same way on main.

  • Adversarial review: seven rounds by Opus and Codex (gpt-6.1-sol, xhigh). Every finding was reproduced and fixed, including:

    • other users' argv
    • escaping parity
    • missing timeouts
    • a stale shared read that could SIGTERM a reused pid
    • wall-clock rollback reopening that bug
    • callback retention under a stalled pool
    • a stuck naming pass blocking the ps fallback
    • the half-budget fallback regression
    • the shutdown-wrapper deadline gap

    The final verdict from both is MERGE. Codex re-ran all earlier repros, and in 90 simulated polls plus 50 real-addon reads spawned zero ps.

  • Not covered:

    • The sudo use_pty path is covered by synthetic tables only.
    • x64 macOS was built by CI, not tested locally.
  • I manually tested these changes locally

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

🤖 Generated with Claude Code

…rking setuid ps (#31783)

/bin/ps is setuid root, so every exec costs an uncached syspolicyd
assessment and an EDR inspection. The terminal host forked ~7-10 ps/lsof
per second with 4 agent panes, and each stale daemon multiplied that.

node-pty's patched pty.node now exports processTable (sysctl
KERN_PROC_ALL/KERN_PROCARGS2 on a worker thread), processCwd
(PROC_PIDVNODEPATHINFO) and processPath (proc_pidpath). runPsInProcess
renders the same ps column text for the invocations Orca issues, so the
existing parsers and callers are unchanged; anything it cannot reproduce,
non-macOS hosts, and builds without the exports keep running ps/lsof.

Measured with 4 anchored panes polled every 750 ms for 60 s: ~440
ps/lsof children before, 0 after.
…s table like ps (#31783)

Review follow-ups for the libproc reader:

- Timeouts: every caller passes its ps timeout; a read stalled behind a
  saturated libuv pool resolves timed out (code null, timedOut) at that
  budget, exactly as the killed ps would. Identical native reads share one
  worker, each caller racing its own deadline; past 8 distinct reads in
  flight callers run ps. execFile-shaped callers reject like a failed child.
- tty naming shares one slot with captureDarwinProcessTable's guard, so a
  draining naming pass sends both paths to ps instead of naming again.
- Commands: the addon renders argv with adv_cmds' getproclline and strnvis
  (VIS_TAB|VIS_NL|VIS_NOSLASH) under the caller's LC_CTYPE via a
  thread-scoped uselocale, so newlines print as \012 instead of splitting
  tokens; zombies print <defunct>.
- Other users' processes: unreadable argv is marked commandIncomplete. Only
  -p selections and processes on a terminal under the user's own readable
  process there (sudo in a pane) get a targeted `ps -p ... -o pid=,command=`,
  cached by (locale, pid, start time); root daemons and a pane's root login
  never spawn ps.
- -t selects with KERN_PROC_TTY after ps's addelem_tty path resolution, so
  per-terminal reads skip argv for every other process (~0.3 ms vs 4 ms ps).
- -p deduplicates; rows sort by terminal then pid as pscomp(); stat adds
  ps's < N X V L modifiers. The export is renamed readProcessTable so an
  older pty.node falls back to ps rather than misreading the new query.
…31783)

Spawns a pty holder whose argv carries a newline, tab, invalid UTF-8 and é,
then compares Orca's ps invocations column by column: pid/ppid/pgid/tpgid/
lstart/tdev/tty exactly, commands for the user's own processes, and stat
modulo running/idle (thread state needs setuid ps). Rows are compared only
when two in-process reads around the ps run agree, so churn cannot flake it.
…he request (#31783)

A timed-out read stayed shareable, so a later descendant capture could join
an earlier poll's abandoned worker and take its rows as fresh; a descendant
that exited meanwhile could have its reused pid SIGTERMed. The addon now
reports when its worker actually began reading (startedAtMs), and
runPsInProcess refuses a shared read that began before its own request and
reads again, so every consumer that signals from rows (descendant
teardown, foreground-group signalling, identity probes) only sees rows
scanned after its stamp. Regression: pty-descendant-termination-libproc.

Also from review:
- argv recovered with setuid ps is keyed by pid, start time to the
  microsecond, comm and executable path (exec changes the latter two) with
  a 10 min TTL and 512-entry bound.
- Recovery covers a sudo use_pty tree: the unreadable chain must start on
  the terminal of the user's own readable process, so the root shell and
  agent on sudo's own pty qualify while root daemons, GUI helpers and a
  pane's root login (own pty, under a terminal-less or tty-inheriting host)
  do not.
- Concurrent tty= readers wait out the single naming pass, and readers past
  the 8-read cap queue behind one, within their deadlines, instead of
  bursting /bin/ps at startup restore (24 panes polled at once: 0 ps).
- darwin-terminal-naming-slot.ts is now darwin-terminal-naming-single-flight.ts.
…31783)

Parsers that take the first matching row depend on ps's terminal-then-pid
order, so compare the order of the rows both captures share, not just their
contents.
…ce, not wall clock (#31783)

Freshness compared the worker's gettimeofday with Date.now(), so a clock
step back let an abandoned read's rows through (wrong-pid SIGTERM) and a
step forward could reject fresh reads until the deadline. Each read now
takes a JS-side sequence number when created and is submitted a microtask
later; a caller joins only a read not yet submitted or created after it
registered, so callers in one turn share a worker and nothing submitted
before a request serves it. The addon drops startedAtMs again. Deadlines,
timeouts and the argv cache use performance.now().

Callers past the 8-read cap wait on one slot notifier and each joined read
has one settle notifier; both unsubscribe on timeout, so a stalled pool
retains no per-caller reactions. If the pool is still at the cap when an
overflow caller's deadline passes, the reader is marked stalled for 30s
and callers run ps, as before this branch.

Recovered argv is trusted for 30s: exec can keep pid, start time, comm and
executable (node -> node with another script) and no cheap field shows it,
so a sudo pane costs one targeted ps per process per 30s.
…1783)

- captureDarwinProcessTable's canonical fallback, taken while the naming
  slot is busy, now runs /bin/ps directly instead of re-entering the
  in-process reader and waiting on that same slot (a held lstat timed out
  every strict capture without one subprocess fallback).
- In-process tty= readers wait out a short pass (startup restore) but hand
  a pass older than 500ms to ps, which names terminals out of process.
- The naming single flight keeps one settle notifier per pass; waiters sit
  in a Set and drop off on timeout, so timed-out callers leave no reactions
  on a stuck pass (was one per call). The notifier moves to
  deadline-settle-notifier.ts, shared with the native read scheduler.
- Once the native read breaker has tripped, a caller finding the cap still
  held after the cool-down returns to ps at once instead of waiting out its
  timeout.
- Descendant teardown and foreground-group signalling give the native read
  half their budget and fall back to a fresh ps on a native timeout, so a
  partly stalled pool cannot cost a kill.
…ut (#31783)

Splitting the budget in half shrank ps's window behind a stalled native
read: a 750ms ps lost descendant SIGTERMs and an 80ms foreground ps lost
group SIGWINCH. Descendant teardown and foreground-group signalling now try
the native reader for min(150ms, timeout/4) (inProcessSliceMs), then give
ps the whole original timeout; readProcessTableBeforeDeadline extends the
production reader's outer deadline by that slice, so ps succeeds exactly
when it did on main. The stale-read regression test now also asserts the
fresh rows' real descendant is signalled.
…descendant reader (#31783)

readProcessTableBeforeDeadline granted the native slice only when the
reader was readProcessTable by identity, so the daemon shutdown sweep's
sharing wrapper kept a bare 250ms deadline: a stalled native read plus a
200ms ps missed it on every retry and descendants survived shutdown.
ProcessTableReader now declares deadlineAllowanceMs; the fresh reader sets
it (0 without the addon or with a stalled pool, so Linux and ps-only hosts
keep their exact deadlines), createProcessTableSnapshotReader and the
shutdown wrapper inherit it, and injected readers default to none. The
fresh reader moves to pty-descendant-fresh-table.ts. The real-ps parity
test gets a 30s per-case timeout, since whole-table /bin/ps runs slow
under suite load.
@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge, with the existing non-blocking delay when the first terminal-naming call gets stuck.

Findings

  1. P2 First caller misses fallback ▶

Summary

The PR replaces frequent macOS ps calls with native process reads and keeps subprocess fallbacks.

  • Mac process checks read process details in the daemon instead of launching ps each time.
  • Terminal teardown and foreground checks try native process reads before using ps.
  • Terminal names stay available when another process-table read is naming devices.

Reviews (2) · Last reviewed commit: "test(terminal): pin the libproc test loc..." · Reviewed by Greptile

Comment on lines +73 to +78
if (naming) {
const named = await beforeDeadline(naming, deadline)
if (named === DEADLINE_PASSED) {
controller.abort()
}
return named

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 First caller misses fallback

The caller that starts terminal naming waits until its full deadline. Only later callers use the 500 ms stall check. If lstat or opendir hangs, the first shell-readiness check waits up to 3 seconds and returns a timeout instead of trying ps.

Limit the owner's wait to the stall threshold too, then return 'stalled' so the caller can use ps.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The changes add macOS native process-table, process-CWD, and process-path queries, plus a shared in-process ps reader with subprocess fallbacks in existing inspection paths. They add deadline handling, shared native reads, command completion, and coordinated terminal naming. The daemon protocol version changes to 54. The vendored node-pty patch also changes PTY lifecycle handling, native build configuration, and Windows ConPTY behavior.

Priority: Normal

Merge Risk: Low · up to 7ae71

The new native paths have localized fallback and cleanup gaps that can reduce process-inspection reliability or leak resources; they should be fixed or explicitly accepted before merge.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Out of Scope Changes check Warning The patch also changes Windows ConPTY and winpty behavior, including job-tree operations, process listing, teardown handling, and Windows target definitions. These changes do not support the macOS dae… Remove the unrelated Windows ConPTY, winpty, and Windows lifecycle changes, or move them to a separate pull request with a relevant issue.
Docstring Coverage Warning Docstring coverage is 51.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: replacing macOS setuid ps process-table forks with in-process reads.
Description check Passed The description includes all required sections, links issue #31783, explains the user impact and implementation, documents testing and limitations, and states that the change has no UI impact.
Linked Issues check Passed Issue #31783 requires reducing repeated macOS daemon full-table ps scans. The PR adds in-process macOS libproc/sysctl reads and routes supported process-table, foreground, cwd, and related queries t…
Full details: Linked Issues check

Explanation

Issue #31783 requires reducing repeated macOS daemon full-table ps scans. The PR adds in-process macOS libproc/sysctl reads and routes supported process-table, foreground, cwd, and related queries through them. It retains ps and lsof fallbacks. The reported parity, timeout, freshness, and fallback tests cover the implementation. No concrete coding requirement from #31783 remains unmet.

Full details: Out of Scope Changes check

Explanation

The patch also changes Windows ConPTY and winpty behavior, including job-tree operations, process listing, teardown handling, and Windows target definitions. These changes do not support the macOS daemon scan objective in #31783. The patch also includes Windows-specific generated and lifecycle changes without a demonstrated connection to that objective.

Resolution

Remove the unrelated Windows ConPTY, winpty, and Windows lifecycle changes, or move them to a separate pull request with a relevant issue.

Full details: Docstring Coverage

Explanation

Docstring coverage is 51.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 45 functions across 31 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Caution

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

⚠️ Outside diff range comments (3)

Minor · Preserve the /bin/ps fallback after a native timeout. · libproc-process-table.ts:131-137

src/shared/libproc-process-table.ts:131-137
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Preserve the /bin/ps fallback after a native timeout.

When a shared native read remains unsettled until its deadline, runPsInProcess returns timedOut: true, not null. Null-only consumers therefore skip their existing /bin/ps fallback. Process-table snapshots and shell-readiness checks can report unavailable results even when /bin/ps succeeds.

Handle timedOut as a fallback condition in each affected caller. Keep TIMED_OUT at this shared boundary because other callers intentionally use it to report timeout or indeterminate results.

Suggested fix
-    const result =
-      (await runPsInProcess(CHEAP_PS_ARGS, options)) ??
-      (await runProcess({ program: 'ps', args: CHEAP_PS_ARGS, ...options }))
+    const inProcess = await runPsInProcess(CHEAP_PS_ARGS, options)
+    const result =
+      inProcess && !inProcess.timedOut
+        ? inProcess
+        : await runProcess({ program: 'ps', args: CHEAP_PS_ARGS, ...options })

Apply the equivalent timeout handling to the other null-only consumers. For readPsStdoutInProcess consumers, handle its timeout rejection before the existing execFile('ps', ...) fallback.

Minor · Map native null reads to the undefined fallback sentinel. · libproc-addon.ts:96-119

src/shared/libproc-addon.ts:96-119
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Map native null reads to the undefined fallback sentinel.

The native C++ wrappers return null when a CWD or executable-path read fails. The TypeScript callers interpret undefined as the signal to run lsof. As a result, recoverable libproc failures can return an empty CWD and unavailable shell readiness without attempting the existing fallback.

Suggested fix
-    return addon.processCwd(pid)
+    return addon.processCwd(pid) ?? undefined
...
-    return addon.processPath(pid)
+    return addon.processPath(pid) ?? undefined
Minor · Clean up the child and master before throwing. · node-pty@1.1.0.patch:411-430

config/patches/node-pty@1.1.0.patch:411-430
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Clean up the child and master before throwing.

When pty_cloexec(master) fails, PtyFork throws before it creates the PTY object or registers SetupExitCallback. The child already exists, and pty_posix_spawn leaves master open on success. This leaks the master descriptor and leaves the child unmanaged.

Suggested fix
   if (pty_cloexec(master) == -1) {
+    close(master);
+    kill(pid, SIGKILL);
+    while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) {}
     throw Napi::Error::New(napiEnv, "Could not set master fd to close-on-exec.");
   }

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: b1639df1-5490-424f-8736-92d15d263e0d
Commits

Reviewing files that changed from the base of the PR and between 816cd15 and 7ae71b3.

Files selected for processing: 1
  • src/shared/libproc-process-table.test.ts

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

@nwparker
nwparker merged commit 5257197 into main Oct 29, 2026
95 of 97 checks passed
@nwparker

Copy link
Copy Markdown
Contributor Author

Credit note: this fix builds on @thefrikidude's measurements and diagnosis in #35257, which pointed to reading the process table natively instead of forking setuid ps. The squash commit should have carried Co-authored-by: Manan Chataut <mananchataut@gmail.com>. We can't amend main, so we're recording it here. Thank you!

nwparker added a commit that referenced this pull request Oct 30, 2026
…alls and ps fallbacks (#31783) (#36282)

* fix(terminal): bound the in-process macOS process table's blocking calls and ps fallbacks (#31783)

Follow-up to #35987 from its post-merge reviews.

- cwd and executable path: node-pty's readProcessCwd/readProcessPath now run
  PROC_PIDVNODEPATHINFO and proc_pidpath on a libuv worker (the synchronous
  exports are gone, so an older pty.node falls back to ps/lsof). JS caps them at
  two outstanding lookups and 250ms; a wedged mount or a full cap answers from
  lsof -b, which never stats, so a dead NFS/SMB/FUSE mount cannot freeze the
  daemon's event loop or take the whole pool.
- Unreadable argv prints "(comm)", as ps does, instead of a proc_pidpath lookup
  under Orca's credentials on the table worker.
- The table exports each row's euid. Argv recovery runs setuid ps only for
  another user's processes, never for our own mid-exec or exiting ones (or
  SIDL/exiting rows), which ps cannot read either: a pane looping short
  commands forked ps on ~15% of full reads, now none.
- Recovery is coalesced per process across concurrent readers and a failed
  recovery waits 5s before costing another ps. The cache keys on comm and start
  time.
- Full-table and shell-foreground captures, which feed strict evidence with
  the capture's age, take only argv ps read after their request; display
  readers keep the 30s cache.
- While the reader is down (stalled-pool breaker open, or a rejecting addon,
  which now backs off 1s..30s), runPsInProcess runs /bin/ps itself through one
  shared budget: identical invocations share a run, a burst of 4 forks then one
  a second, and a run spawned before a request never answers it. Descendant
  teardown and foreground-group signalling keep their own full-timeout ps
  (callerRunsPs). 13 pollers at 750ms: 1,040 ps/min before, 63 after.

No daemon protocol bump: the wire format is unchanged, and each daemon loads
JS and pty.node from the same runtime copy.

* fix(terminal): make the libproc reader survive worker teardown, fail closed and share one fair fork budget (#31783)

Review follow-ups to the previous commit.

- Native: every libproc AsyncWorker derives from OrcaPromiseWorker, which skips
  JS completion once an env cleanup hook marks the env closing and swallows a
  Napi::Error thrown mid-teardown. Terminating a worker_thread with cwd or table
  reads in flight aborted the process; 200 terminates each now survive.
- (comm) in a terminal's foreground group (argv neither reader recovered, e.g.
  a root `su`) judges 'unknown', never 'agent', so the launch line is not typed
  into it.
- Fresh argv only for getFreshSnapshot captures; the 500ms TTL readers keep the
  30s cache. Argv recovery runs through its own fork budget (2, then one per
  2s). Root `su` pane, 13 pollers at 750ms: TTL 79 -> 2 ps/min, fresh 158 -> 31.
- The fork budget serves invocations in FIFO turns, grouping each one's waiters
  onto one run. 13 streams: served 80,42,1,1,2,0x8 -> 10-12 each.
- Forced subprocess captures (a held naming pass) go through the fallback
  budget: 5 readers, 400 direct ps/min -> 63, 395 of 400 captures served.
- A failed recovery is retried 5s after ps gave up, not after it started.
- Tests pin Orca's euid instead of reading the runner's and assuming root is 0.
- process-cwd keeps one lsof per pid until it exits, so a dead mount no longer
  leaks one per poll; shell readiness reads the exe path through runProcess,
  which gives up on an lsof that ignores SIGTERM.
- A #35987 pty.node (table reader, no async path reads) keeps the in-process
  table; cwd/path use lsof, with a one-time warning.
- captureProcessTable moves to process-table-ps-capture.ts.

* fix(terminal): make withheld argv unverifiable and reuse recovered argv for at most 2s (#31783)

Round-2 review follow-ups.

- Worker teardown: drop OrcaPromiseWorker and its env flag (Node drains the
  pool before cleanup hooks, so it never fired). node-pty's mac pty target now
  builds with NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS, which covers both the
  call-in and completion paths. The smoke test now calls the reads from a
  timer while terminating: the previous build aborted 2 of 3 runs, this one
  survives 3/3 x 100. pty.cc is back to the first follow-up's exact content.
- A foreground process whose argv ps withheld (`(comm)`: another user's,
  exiting, or a recovery the budget skipped) makes the batch resolver, and so
  host-stamped evidence, `argv_unreadable`/unverifiable rather than live with no
  agent; agent-foreground resolution reports it unavailable.
- judgeTerminalForeground fails closed only for an unreadable shell (`(bash)`
  from a root `su`), not our own exiting `(git)`.
- Recovered argv is reused for at most 2s per identity (pid, start, comm) for
  every reader (the next commit ages evidence by it), and
  fresh reads no longer queue behind the recovery budget. Root `su` pane, 13
  fresh pollers: p50 2,989ms -> 53ms, 31 -> 26 ps/min (TTL reader 1 -> 26).
- The fork budgets serve interactive and fresh readers before display polls
  (cheap and TTL snapshot captures), FIFO within each. 20 display streams plus
  a 3s launch-paste read every 5s: 0/12 -> 12/12 served, polls 4-7 each.
- On a selected terminal, the foreground group's argv is recovered even with no
  process of ours left (`exec su`); the root login is not.
- Budgeted captures keep the 32 MiB output ceiling.

* fix(terminal): age strict evidence by its oldest recovered argv (#31783)

Round-3 review follow-ups.

- Recovered argv carried no read time, so a capture reusing it (cached, or a
  recovery still in flight) stamped its own capture time over argv up to 2s
  older, and the remote write guard could take a same-comm re-exec for the
  agent. Rows now carry when ps read their recovered argv; runPsInProcess
  reports the oldest through onArgvReadAt, and the snapshot reader stamps
  capturedAtMs at that input, so capturedAgeMs is the age of the oldest input
  and consumers' own freshness checks (the write guard's 500ms) decide. A
  recovery begun more than 2s before a reader asked no longer answers it, and
  an older recovery finishing late cannot replace a newer one.
- resolveSpawnFileForegroundFromRows reports a withheld `(node)` foreground
  unavailable.
- One budget queue entry per invocation: an interactive request promotes a
  queued poll's, a poll joins an interactive one.
- parseShellForegroundRows gets its JSDoc back.

* fix(terminal): keep a cached name from authorizing a refused write; integer, under-2s argv ages (#31783)

Round-4 review follow-ups.

- When the relay's evidence declines a write (stale, unverifiable, another
  agent), its foreground name may still prove the shell but no longer proves
  the agent: that name can be the tracker's 1s display cache, e.g. `claude`
  cached just before Claude exited to a root shell under su. Relays that send
  no evidence keep the name as before.
- Snapshot ages round up to whole milliseconds; the wire rejects fractions
  from the monotonic-to-wall conversion.
- Recovered argv is reused for at most 1.5s, so reuse plus the 500ms snapshot
  cache stays under the 2s freshness gate; reuse ages from when that ps
  spawned, not from the wait for its fork token, and only an answer is
  remembered (a timed-out or withheld run proves nothing). Fork budget
  unchanged. Root su pane, 13 pollers: 30 ps/min; fresh p50 51ms; strict
  evidence age p50 752ms, max 1,508ms.

* fix(terminal): keep the relay's agent name behind structurally unverifiable evidence (#31783)

The previous commit refused the name fallback whenever the relay sent any
evidence, so `unverifiable` evidence for structural reasons also blocked
writes: npm Codex (node codex.js spawning native codex) and Gemini (relaunching
itself) put two agent rows in the foreground group (ambiguous_foreground_group),
and remote tmux reads multiplexer_boundary. On SSH POSIX hosts that dropped
launch prompts and failed worker briefs, conversation send/cancel and
automations with agent_not_in_foreground.

The name now cannot authorize only when the evidence saw the group (`live`:
stale, or no agent in it, as in the cached-`claude`-over-root-shell repro) or
withheld a foreground argv (`argv_unreadable`, possibly a root `su` shell).
Other unverifiable reasons keep the name fallback, as before this branch.

* fix(terminal): let a relay name complete only fresh agent-positive evidence (#31783)

The SSH name fallback still let a stale or cached name authorize a write under
any unverifiable reason but argv_unreadable: ps EAGAIN (process_table_unreadable)
after the tracker cached `claude` over a root bash, anchor_missing,
tty_boundary (`sudo -i` with use_pty, a root shell on a new pty), or a 1.5s-old
ambiguous capture.

- The name now completes evidence only when it is fresh (within the guard's
  500ms bound), its reason is one that saw an agent row
  (ambiguous_foreground_group, multiplexer_boundary,
  candidate_start_time_missing), and the name is the launched agent. Every other
  reason, including ones this build does not know, reads unknown. Relays that
  send no evidence keep the name, for mixed versions.
- The relay's evidence names one agent that holds two rows of the group (npm
  Codex: node codex.js spawning native codex; Gemini relaunching itself), so a
  new relay proves them live even when its name is `node`. Two different agents
  stay ambiguous. No wire change.

* fix(terminal): judge a relay name by the host's capture age; keep old relays' npm Codex (#31783)

- Name completion compared the capture's start with the read's start, so a
  slow response made old evidence look fresh: a capture 1,500ms old arriving
  after 1,100ms passed. It now uses the host-reported capturedAgeMs against
  the 500ms bound, which latency cannot shrink. Live evidence keeps its
  capture-start rule.
- Relays before same-agent dedupe report npm Codex (node wrapper plus native
  binary) as ambiguous_foreground_group and name it `node`. For that reason
  only, `node` now completes the evidence when the launched agent is
  npm-installed per the node entrypoint tables; other agents and reasons stay
  fail-closed.

* fix(terminal): bound relay-name completion by twice the snapshot TTL (#31783)

The 500ms bound refused ~9.5% of tmux name completions on a host with 13
pollers (cached snapshot up to one 500ms TTL old plus ~30ms capture, max
528ms), and a refusal drops the launch prompt or throws with no retry. The
bound is now 2 x PROCESS_TABLE_SNAPSHOT_MAX_STALENESS_MS (1s), still on the
host-reported age: 0 of 767 reads exceed it, the delayed 1,500ms repro still
refuses, and the cached-name hazard (the tracker's ~1s display cache) is no
wider.

* fix(terminal): complete relay evidence with a name read now, not the display cache (#31783)

With the 1s bound, a capture at 0-528ms, `codex` cached at 100ms, Codex
exiting at 101ms and the guard at 999ms still let the cached name authorize a
write over the bash that replaced it.

- Name completion (structural reasons, host age within 2x the snapshot TTL)
  now asks confirmForegroundProcess, which reads the foreground now (a fresh
  scan, or node-pty's raw process name) rather than the tracker's 1s display
  cache; a null answer reads unknown. No new wire surface: the local daemon answers
  confirmForegroundProcess, paired runtimes evaluate guards locally, and the
  production controller returns null (read as unknown, failing safe) when a
  provider lacks it. Controllers without the method, and relays sending no
  evidence, keep the cached name as the legacy path.
- Live evidence also accepts a host-reported age within 2x the TTL, so a real
  agent behind a 500-528ms cached snapshot on a fast link is no longer
  refused; a capture begun after the read was asked still counts, and
  5,000ms-old evidence still refuses.

* fix(terminal): confirm live evidence older than one snapshot TTL with a fresh read (#31783)

Accepting live evidence up to 2x the TTL reopened the window the previous
commit closed for name completion: a capture at 0-528ms, the agent exiting at
~529ms, and at 999ms `live claude` still read as agent over a bash.

Live evidence begun no more than one TTL before the read was asked (so any
host age within one TTL, however long ps took) still answers alone. Between
that and 2x the TTL, confirmForegroundProcess reads the foreground now: the
launched agent proves it, a shell reads shell, anything else unknown.
Controllers without that read fall through and refuse, as before.

Reachability: the remote (host.remote) write-guard path has no production
caller since the SSH provider's removal (#27499); the registry dispatches only
local providers and paired runtimes evaluate guards locally. This is defence
in depth for any future remote provider.
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.

[Bug]: Terminal daemon spawns full-table ps scans ~1,000/min on macOS, keeping syspolicyd/EDR busy around the clock

1 participant