Repository navigation
perf(terminal): stop forking ps on every poll of an anchored agent pane - #35257
thefrikidude wants to merge 1 commit into
Conversation
A visible agent pane asks the terminal host every 750 ms whether its agent is still in front. For an anchored agent the host answered from a cheap tier that still forked `ps -axo ...` to rebuild the pane's process fingerprint, and the 500 ms snapshot cache meant a new fork for almost every poll window. While the agent ran tools, the fingerprint changed on every poll and each poll escalated to the full `ps ... command=` capture. On macOS every one of those forks goes through syspolicyd, which is the load reported in stablyai#31783. Between captures the host now trusts two kernel facts that need no fork: node-pty still names the same process in the foreground (the cheap tier already checked this), and that process, the leader of the anchored foreground group, is still alive (`kill(pid, 0)`). Exit, Ctrl-Z, a tool taking the terminal, and a restart each break one of them. The fingerprint itself is still rebuilt once per shared 10 s window, so anything subtler is caught at the next window, and all panes share that one capture. The shortcut only applies when the foreground leader is a recognized agent; a wrapper such as `node` or `bash` can outlive the agent inside it, so those panes keep reading the table on every poll. The local (non-daemon) provider uses the same helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Could a maintainer approve the CI workflow runs for this PR? They're waiting on approval. Thanks! |
|
No actionable comments were generated in the recent review.
|
|
Thanks @thefrikidude — your measurements here directly shaped the fix. #31783 is now resolved by #35987, which reads the macOS process table in-process via libproc (the native route you described), so the daemon no longer forks |
ELI5
When an agent runs in a terminal pane that you can see, Orca checks about every second whether the agent is still there. To check, it started the
psprogram each time. On a Mac, eachpsstart makes the system security service (syspolicyd) do work, so with a few agent panes open that service burned CPU all day. Now Orca asks the kernel two cheap questions instead, and runspsonly once every 10 seconds. With 4 agent panes,psruns go from 81 to 9 per minute when the agents wait, and from 149 to 24 per minute when they are busy.Author: @thefrikidude
What Changed
inspectProcessevery 750 ms. For an anchored agent the terminal host used the "cheap tier". That tier still forkedps -axo pid=,ppid=,pgid=,tpgid=,stat=,lstart=to rebuild the pane fingerprint. The snapshot cache lasts 500 ms, so almost every poll window forked. While the agent ran tools, its subprocesses changed the fingerprint on each poll. Each poll then escalated to the fullps … command=capture.tcgetpgrp+sysctl). The cheap tier already checked this before it forked.process.kill(pid, 0)).EPERMcounts as alive.claude,codex, …). A wrapper (node,bash -c …) can outlive the agent inside it, so those panes still read the table on every poll, as before.pane-fingerprint-revalidation.tsholds the rule.paneFingerprintForegroundGroup()reads the group from the fingerprint thatbuildPaneProcessFingerprint()already writes.terminal-host-process-inspection.ts) and the local provider (local-pty-foreground-inspection.ts) both use the helper.{ foregroundProcess, hasChildProcesses: true }, no evidence). Full captures, evidence, unanchored panes, Windows and remote panes are untouched.Why
The issue measured about 10 forks per second per terminal host, mostly
ps. The commenters showed why that is expensive on macOS:/bin/psis setuid, sosyspolicydassesses every exec and never caches the result.I reproduced the cause on a dev build. Idle shells and hidden panes fork little. The rate comes from visible agent panes, which poll every 750 ms.
Options I considered:
libproc/sysctl), as the issue suggests. This removespsentirely, but it needs a new native addon in the signed terminal host bundle. It would also need exactpsparity forcommand=/tdev=; for example, only setuidpscan read the arguments of asudopane. That is a much larger and riskier change. This PR does not rule it out, and it removes most of the forks now.Linked Issue
Fixes #31783
I saw that @Jinwoo-H said on the issue they are looking into it. If this overlaps with work in progress, I am happy to close or reduce this PR.
Visual Proof
N/A: there is no UI change. The proof is the measurements below.psforks by one terminal host in 60 s (dev build on macOS). A loggingpsshim sits first on the daemon'sPATH, and I replay the visible-pane poll for 4 panes every 750 ms. The panes run a compiled fake agent namedclaude:command=+ 71 cheap)The 2 remaining
ps -axo pid=,ppid=,pgid=,lstart=calls per minute come from a separate periodic check and are the same before and after.Detection in the real app after the change (one poll every 150 ms):
claudefgclaudeagain, then back to the no-fork pathclaudeas before.Testing
Real app (macOS, dev build): the tables above, both measured with the same script against main and against this branch.
New tests:
pane-fingerprint-revalidation.test.ts(10):node,bash, unknown) read on every pollEPERMis alive,ESRCHis gonenodewrapper, so they also pin the "wrappers keep the old behavior" rule.Mutation checks: I broke each of 9 safeguards in turn, and a test failed each time:
EPERMhandlingSuites: the daemon, providers and pty suites pass: 4,256 tests.
Gate:
pnpm check:changed: 20 of 20 checks pass (format, lint, casts, flaky-test ratchet, typecheck, affected tests).Not tested: Linux and Windows hosts on real hardware. Windows does not reach this path. Linux runs the same code with
/proc-backedps, and the daemon tests cover it.I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
Written with Claude (Opus 5.5) via Claude Code. I reviewed the diff, ran the measurements, and checked detection in the real app.
Author: X @thefrikidude
Review
AI review summary:
kill(pid, 0)works the same on macOS and Linux.psforks for agent panes. Each spared fork is onesyspolicydassessment of the terminal host bundle on macOS.killuses signal 0, which only checks that the PID exists.Agent skill upstream boundary
Notes
nodewrapper (npm-installed Claude Code) keep the per-poll capture. That is deliberate, see above. The native installer'sclaudeandcodexget the full benefit.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred) —check:changedand the daemon, providers and pty suites pass locally; the full test run and build are left to CI🤖 Generated with Claude Code