Skip to content

perf(runtime): cache parsed ps rows on POSIX so panes share one parse - #7518

Merged
nwparker merged 1 commit into
mainfrom
nwparker/perf-posix-ps-parse-dedup
Jul 6, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/perf-posix-ps-parse-dedup

Conversation

@nwparker

@nwparker nwparker commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

1. Description

getProcessTableSnapshot() is the shared, deduped process-table scan behind agent foreground-process inspection (the 750ms/2000ms per-pane cadence, plus liveness reconcile and daemon session enrichment). The earlier #6288/#6667 fix collapsed the concurrent ps forks into one scan behind a 500ms TTL — but on POSIX it caches only the raw stdout string. Each of the M concurrent panes that pull that one cached string then re-runs its own parsePsRows(stdout) + collectDescendants(...) within the same TTL window, re-tokenizing byte-identical output and re-allocating a row object per process, every window.

The Windows path already avoids this: windows-foreground-process-rows.ts instantiates createProcessTableSnapshotReader<WindowsProcessRow[]> to cache parsed rows. This PR makes the POSIX default reader symmetric — it parses inside the deduped scan and returns ProcessTableRow[], so all callers in a TTL window share one parse. It also collapses the two duplicated parsePsRows implementations (main agent-foreground-process.ts + relay pty-shell-utils.ts) into a single shared parseProcessTableRows.

2. Evidence

Before: getProcessTableSnapshot(): Promise<string> → each caller does const rows = parsePsRows(stdout). With M panes and P host processes, that's M × (P-line regex tokenization + P row allocations) per 500ms window over identical input.

After: getProcessTableSnapshot(): Promise<ProcessTableRow[]> → the parse runs once inside the deduped scan; callers consume the shared array. Work drops from O(M×P) → O(P) parse per window, independent of pane count.

New tests in process-table-snapshot.test.ts:

 ✓ shares one parsed-rows array across a burst so panes do not each re-parse   (parses === 1; rowsA === rowsB, reference-identical)
 ✓ parseProcessTableRows parses pid/ppid/stat and keeps the full command (incl. spaces)
 ✓ parseProcessTableRows tolerates CRLF and skips header/blank/non-matching lines

Full affected suite green — process-table-snapshot, agent-foreground-process, agent-foreground-process-ps-scan-volume, pty-shell-utils: 40 tests passed. Typecheck (tsgo -p config/tsconfig.node.json) EXIT 0; oxlint/oxfmt clean. The ps-scan-volume test still asserts the fork count is unchanged (the dedup contract is preserved; only per-caller parsing is removed).

Micro-benchmark of the removed work (parse of a ~400-process ps table): ~0.15 ms/parse. On a shared SSH relay with 8 active agent panes that is ~8 × 0.15 ms ≈ 1.2 ms → 0.15 ms per 500 ms window of pure re-tokenization + the matching per-row GC pressure eliminated. Small in absolute terms, but it scales with pane count and is exactly the shared-relay idle-CPU case #6288 targeted.

3. ELI5

Imagine everyone in the office needs today's phone directory. We already fixed it so we print one copy instead of everyone printing their own (that was the expensive part). But then each person still re-typed the entire printout into their own spreadsheet by hand, even though the printout is identical for all of them. This change types it up once and hands everyone the same finished spreadsheet. Windows already worked this way; now Mac/Linux do too.

4. Trade-offs / behavior that could regress

  • No behavior change. The resolved foreground-process name is computed from the same rows; only where the parse happens moved (into the shared scan). Parsing semantics are preserved and now use the CRLF-tolerant /\r?\n/ split (the main path previously split on \n only; POSIX ps emits no CR, so output is identical).
  • Shared-array aliasing: all callers in a window now receive the same ProcessTableRow[] instance. Both consumers only read it (.find, collectDescendants builds its own index; the {...row} spread in collectDescendants copies before adding depth) — no caller mutates the rows, so sharing is safe. If a future caller mutates the array it would affect others; the rows should be treated as read-only.
  • Return-type change to getProcessTableSnapshot (string → rows) is internal — only the two foreground resolvers consume it, both updated here. The generic createProcessTableSnapshotReader factory and its tests are untouched.
  • Scope: POSIX-only change behind the existing POSIX branch; Windows already cached parsed rows and is not modified. Low severity — a CPU/GC shave that scales with pane count on shared relays, not a correctness or leak fix.

Made with Orca 🐋

getProcessTableSnapshot deduped the ps fork (#6288/#6667) but cached only the
raw stdout string on POSIX, so every concurrent agent pane re-ran parsePsRows
over the identical output within each 500ms TTL window — O(M*P) redundant
tokenization + row allocation. The Windows reader already caches parsed rows;
this makes the POSIX default reader do the same by parsing inside the deduped
scan and returning ProcessTableRow[]. Collapses the duplicate parsePsRows in
the main and relay foreground resolvers into one shared parseProcessTableRows.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 205d609d-8e61-416d-9796-0a2e2a1f52ed

📥 Commits

Reviewing files that changed from the base of the PR and between 8e16ec1 and 0be485f.

📒 Files selected for processing (4)
  • src/main/providers/agent-foreground-process.ts
  • src/relay/pty-shell-utils.ts
  • src/shared/process-table-snapshot.test.ts
  • src/shared/process-table-snapshot.ts

📝 Walkthrough

Walkthrough

Changes

This PR refactors process-table handling to use structured data instead of raw ps stdout. src/shared/process-table-snapshot.ts adds a ProcessTableRow type and parseProcessTableRows function, and changes getProcessTableSnapshot() to return parsed rows with deduplicated concurrent caching. src/main/providers/agent-foreground-process.ts and src/relay/pty-shell-utils.ts remove their local ProcessRow types and parsePsRows helpers, updating descendant-collection and candidate-scoring logic to consume ProcessTableRow[] directly. Tests are added/extended for parsing behavior and snapshot caching identity.

Sequence Diagram(s)

See hidden artifact above for the snapshot caching flow diagram.

Estimated code review effort: 3/5 (High complexity across type refactor touching two consumers, though changes are mechanical and covered by tests)

Related issues: None specified

Related PRs: None specified

Suggested labels: refactor, process-management

Suggested reviewers: None specified

Poem:
A rabbit sniffed the process tree,
No more raw ps text for me!
Rows now structured, neat and clean,
Cached and shared, the fastest seen.
Hop, hop—types align, tests confirm,
The burrow's code is now more firm. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change well, but it omits most required template sections, including Screenshots, Testing, AI Review Report, Security Audit, and Notes. Add the missing template sections: Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes, including the required cross-platform review details.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the POSIX change to cache parsed ps rows for shared pane use.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@nwparker
nwparker merged commit 27ba95c into main Jul 6, 2026
2 checks passed
@nwparker
nwparker deleted the nwparker/perf-posix-ps-parse-dedup branch July 6, 2026 07:03
nwparker added a commit that referenced this pull request Jul 6, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…stablyai#7518)

getProcessTableSnapshot deduped the ps fork (stablyai#6288/stablyai#6667) but cached only the
raw stdout string on POSIX, so every concurrent agent pane re-ran parsePsRows
over the identical output within each 500ms TTL window — O(M*P) redundant
tokenization + row allocation. The Windows reader already caches parsed rows;
this makes the POSIX default reader do the same by parsing inside the deduped
scan and returning ProcessTableRow[]. Collapses the duplicate parsePsRows in
the main and relay foreground resolvers into one shared parseProcessTableRows.

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.

1 participant