Skip to content

perf(mac): resolve terminal names once per full process capture - #27146

Merged
nwparker merged 10 commits into
stablyai:mainfrom
nwparker:nwparker/perf-darwin-tdev-fresh-index
Oct 10, 2026
Merged

nwparker merged 10 commits into
stablyai:mainfrom
nwparker:nwparker/perf-darwin-tdev-fresh-index

Conversation

@nwparker

@nwparker nwparker commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

On a busy Mac, Orca regularly lists every running process so it can tell which terminal each process belongs to. Today ps does the job of turning each terminal's device number into a name like ttys012, and with hundreds of terminals that alone takes about a second of CPU per refresh. This PR has Orca look up those names itself, checking each distinct terminal once per refresh. On the measured busy Mac, a refresh drops from about 950 ms to about 110 ms and uses about 90% less CPU overall.

What Changed

Before: on macOS, a full process-table capture asked ps to print terminal names. ps looks up each name separately, which is slow when there are many terminal processes.

After: the full macOS capture asks ps for raw terminal device numbers instead, and Orca names them itself (darwin-terminal-names.ts, darwin-process-table-capture.ts). The rows it produces are the same named rows the existing readers already consume, so nothing downstream changes.

  • Direct lookup first: macOS names every terminal ttys plus its minor device number, padded to three digits (minor 12 is ttys012), and gives each one a single /dev entry. For each distinct terminal, Orca checks only that one path. It accepts the name only if the entry is a character device with exactly the same device number.
  • Exact fallback: a device that doesn't verify (the console, serial ports, unusual setups) is found by scanning /dev the way Apple's own lookup does: the first matching entry in directory order wins. The scan stops once every remaining device is named.
  • Nothing is cached across captures. Every capture looks names up again, so terminals that were created, closed, or renamed are never misreported.
  • Bounded work: only one device lookup runs at a time, so this never fills Node's shared filesystem thread pool. Capture, naming, and validation all share one 15-second deadline. If the deadline expires, the capture fails as "unverifiable" and is not retried slowly. If an earlier lookup is still running, later captures fall back to plain ps instead of piling up more work.
  • Fallback: if anything goes wrong reading /dev, Orca uses the existing ps capture with whatever time is left.
  • Small cleanup: non-Linux hosts no longer parse the full table a second time and then throw the result away.

Unchanged: Linux and Windows capture, the cheap and targeted captures, the 500 ms result cache, and strict/lenient parsing. The host that runs the processes still reads its own /dev, so SSH works the same way. Remote clients still receive named rows, so there is no wire change. There are no new native modules, workers, processes, or protocol fields.

Why

This builds on Giorgio Zamparelli's #24890 and out-of-place's #26985. Both contributors are credited with Co-authored-by trailers; keep those when squashing.

Alternatives considered:

  • A long-lived device-name cache: rejected. It would go stale when terminals are created, closed, or renamed.
  • Synchronous /dev reads: used a bit less CPU, but one stuck system call would freeze the host process and every terminal it serves. Rejected.
  • Scanning all of /dev on every capture: this was the PR's first version. It cost a fixed ~10 ms of Orca CPU per capture even with one terminal. The direct lookup replaced it, and the scan now runs only for devices the direct lookup can't verify.
  • Caching names until /dev changes: rejected. Opening a terminal doesn't update /dev's modification time, and terminal numbers are reused, so a cache would show wrong names.
  • Several lookups in parallel instead of one: slightly faster, but more Orca CPU, and it occupies the whole filesystem thread pool. Rejected.
  • Batching, numeric stats, directory-type filtering: none of these improved enough to be worth the extra code.

Linked Issue

Fixes #24889

Supersedes #24890. Related: #26985 (a separate repaint-path change).

Visual Proof

N/A. This changes how the host captures processes, and nothing visible in the UI changes. Terminal names were checked against the existing output (see Testing).

Performance

Measured on an Apple M4 Max running macOS (Darwin 25.3) and Node 24, with about 2,240 processes, 1,007 of them attached to a terminal, across 389 terminals. Numbers are medians of warm captures using the real snapshot reader. This measures capture cost only; it is not a whole-app or battery test.

Per capture Wall time Orca host CPU ps CPU Combined CPU
Base (ps prints names) ~950 ms ~6 ms ~930 ms ~936 ms
This PR (one lookup at a time) ~112 ms ~15 ms ~80 ms ~95 ms

That table was measured with the earlier full-/dev-scan version. The direct lookup then cut Orca's own naming time on the same Mac from about 12 ms to about 6.5 ms of CPU per capture. With a single terminal, naming dropped from about 10 ms to well under 1 ms.

In a load test (100 requests over 10 seconds through the existing cache), captures finished faster, so more of them ran: 15 per window versus 6–7. Even so, combined CPU per window fell from about 7,000 ms to about 1,900 ms.

Testing

  • Focused unit tests: 150 across the 12 process-table and terminal-naming suites. They cover:
    • the direct lookup, and every way it can fail (wrong device number, symlink or regular file, failed stat)
    • the fallback scan: first-match ordering and stopping early
    • cancellation that waits for an in-progress lookup to finish, and one lookup at a time
    • signed device numbers and UTF-8 names
    • deadline, fallback, and cache behavior
  • A macOS-only test compares the result with real ps output. It starts a real terminal and proves that terminal is named without scanning /dev. It also fails when the name format is deliberately broken.
  • Names match ps exactly on two Macs. On macOS 26.3, 2,237 processes and 390 terminals. On macOS 26.6, 798 processes and 85 terminals. Every terminal on both was named by the direct lookup.
  • Earlier versions were also checked against real captures under C, English, German, Japanese, and Russian locales.
  • The affected suites pass on a Linux host (Ubuntu 24.04).
  • Updated the process-inspection test fixtures so they return the columns the real code requests. The Darwin and Linux tests run through the real translator, cache, and tier code.
  • Node typecheck, relay build, lint, and the changed-code quality check pass.
  • Two rounds of independent adversarial review found no blocking or major issues. Their minor findings are fixed.
  • Native Linux, Windows, and SSH runtime were not measured again. Their code paths did not change.

All runs used ORCA_BACKGROUND_LAUNCH=1.

Review

Risk: low to medium. The production change is about 230 net lines across three modules, and most of the diff is tests. Two rounds of adversarial review found no blocking or major issues.

Trade-offs a reviewer should accept knowingly:

  • More CPU in Orca's own process. Orca now does the naming that ps used to do. On a busy Mac that's a few milliseconds per capture, against roughly 850 ms less CPU in ps. With few terminals, the added cost is negligible.
  • One deliberate difference from Apple's lookup: if root creates a second /dev entry for a terminal that sorts before the real one, Orca still shows the real ttys name, where ps would show the extra entry. A normal system never has these.
  • More frequent captures. Because captures finish faster, more of them run under the unchanged 500 ms cache.
  • Slow /dev is not handled specially. On a host where device lookups are persistently slow, the capture can hit its deadline sooner and fall back to plain ps. There is no backoff that remembers this between captures.

Agent skill upstream boundary

  • Not applicable. No upstream skill-installer source is copied.

Checklist

  • This PR is small and focused (about 200 production lines; the rest is tests)
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Use the host's fresh /dev directory order to translate tdev into the same
tty names without retaining positive or negative device-name caches.
Preserve localized start markers, argv, targeted ps columns, and failure
verdicts. Fall back to canonical ps when translation is unavailable.

Co-authored-by: Giorgio Zamparelli <[email protected]>
Co-authored-by: out-of-place <[email protected]>
nwparker and others added 9 commits October 9, 2026 22:08
Resolve the Darwin filesystem API when used so platform-independent imports
work with existing filesystem mocks. Remove the discarded non-Linux parse
and keep fresh asynchronous inventory work bounded through deadline and drain.

Co-authored-by: Giorgio Zamparelli <[email protected]>
Co-authored-by: out-of-place <[email protected]>
Render numeric Darwin terminal devices and Linux column sets from one
synthetic process table while keeping the production translator and caches.
Exercise both hosts with stable filesystem identity and preserve scan,
agent recognition, evidence, escalation, and exit/restart assertions.

Co-authored-by: Giorgio Zamparelli <[email protected]>
Co-authored-by: out-of-place <[email protected]>
Measured one asynchronous stat at a time lowers host CPU and leaves more
filesystem pool capacity for other work. Preserve fresh native-order lookup
and keep timed-out work held until its admitted request actually drains.

Co-authored-by: Giorgio Zamparelli <[email protected]>
Co-authored-by: out-of-place <[email protected]>
Use the host no-terminal marker in evidence-budget fixtures so Linux does not read proc start times after the timed ps response. Observe rejection before advancing fake timers while retaining the exact 1199ms capture-age assertion and 1201ms rejection case.

Co-authored-by: Giorgio Zamparelli <[email protected]>

Co-authored-by: out-of-place <[email protected]>
Record proc fixture reads outside production error handling and use deterministic numeric start ticks for Linux PID identity.

Co-authored-by: Giorgio Zamparelli <[email protected]>

Co-authored-by: out-of-place <[email protected]>
macOS names every pseudo-terminal ttys%03d from its minor number, with one
/dev entry each. Check that one path per distinct terminal and accept it only
when it is a character device with the same device number; scan /dev only for
devices still unnamed, stopping once all are found.

Removes the fixed per-capture /dev scan, so few-terminal hosts pay one stat per
terminal and busy hosts stat each terminal once instead of every /dev entry.
- Key terminal lookups by numeric major/minor so padded tokens still resolve.
- Clear the in-flight naming slot in the test reset.
- Prove cancellation waits for the active stat, and compare naming with real
  ps output on macOS.
Give the parity check a real terminal on headless Macs and prove that pty is
named by the direct lookup without scanning /dev.
@nwparker
nwparker marked this pull request as ready for review October 10, 2026 23:15
@coderabbitai

coderabbitai Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f7ebed51-220e-4f59-a022-a280eea399a7

📥 Commits

Reviewing files that changed from the base of the PR and between 8452fc3 and df4d953.


📒 Files selected for processing (15)
  • src/main/daemon/terminal-host-cheap-tier-ps-scan-volume.test.ts
  • src/main/daemon/terminal-host-process-inspection-cheap-tier.test.ts
  • src/main/providers/agent-foreground-process-ps-scan-volume.test.ts
  • src/shared/darwin-process-table-capture.test.ts
  • src/shared/darwin-process-table-capture.ts
  • src/shared/darwin-process-table-inventory-drain.test.ts
  • src/shared/darwin-terminal-device-stats.test.ts
  • src/shared/darwin-terminal-names-real-ps.test.ts
  • src/shared/darwin-terminal-names.test.ts
  • src/shared/darwin-terminal-names.ts
  • src/shared/process-table-capture-parsing.test.ts
  • src/shared/process-table-ps-test-fixture.ts
  • src/shared/process-table-snapshot-reader.ts
  • src/shared/process-table-snapshot.test.ts
  • src/shared/shell-foreground-snapshot.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.



📝 Walkthrough

Walkthrough

On macOS, process-table capture now requests terminal device identifiers and translates them to terminal names. The capture uses a time budget and falls back to canonical process-table arguments in specified cases. Shared snapshots use this path on macOS and confirm process start ticks on Linux. Tests cover terminal translation, capture deadlines, fallback behavior, and process inspection on Darwin and Linux.


Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to df4d9

No identified issue needs resolution before merging; the reported checks support proceeding with normal validation.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 24.24% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 33 functions across 15 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly summarizes the primary macOS performance change: resolving terminal names once per full process capture.
Description check Passed The description is detailed and covers the required change summary, rationale, linked issue, visual-proof exemption, testing, review risks, cross-platform considerations, and checklist. The missing No…
Linked Issues check Passed The PR meets the coding requirements for directly linked issue #24889. src/shared/process-table-snapshot-reader.ts uses the Darwin capture path. src/shared/darwin-process-table-capture.ts requests…
Out of Scope Changes check Passed The changes stay within the scope of #24889. Production changes implement Darwin terminal-name resolution, bounded capture behavior, fallback handling, and the related removal of redundant non-Linux p…

  • Fix all pre-merge checks with AI
  • 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.

@nwparker
nwparker merged commit db2ed7c into stablyai:main Oct 10, 2026
49 of 61 checks passed
@nwparker
nwparker deleted the nwparker/perf-darwin-tdev-fresh-index branch October 10, 2026 23:45
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.

macOS: the daemon's full ps capture spends most of its time naming terminals (tty=), about a core on a busy Mac

1 participant