Skip to content

perf(windows): dedupe per-pane process-table scans in agent inspection - #7384

Merged
nwparker merged 2 commits into
mainfrom
nwparker/perf-windows-process-snapshot
Jul 5, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/perf-windows-process-snapshot

Conversation

@nwparker

@nwparker nwparker commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

Windows agent foreground-process inspection (queryWindowsProcessDescendants) forks a whole-process-table PowerShell/CIM scan per pane, on the same 750ms/2000ms cadence the POSIX path uses. The POSIX side routes through getProcessTableSnapshot (500ms TTL + single in-flight promise, #6288/#6667), which collapses N concurrent panes to ~2 scans/sec. Windows had no equivalent, so K concurrent agent panes forked K powershell.exe cold-starts, each enumerating the entire Win32_Process table and filtering per-pid in JS.

Fix: generalize the existing createProcessTableSnapshotReader factory to be generic over its scan result (<T = string>, so the POSIX path stays byte-identical), and add a Windows singleton reader that caches parsed WindowsProcessRow[].

ELI5

To show which AI agent is running in each terminal, Orca on Windows ran a heavy "list every process on the machine" PowerShell command — separately for each terminal, a few times a second. With several agent terminals open, that spun up lots of PowerShell processes (each ~10–40× heavier than the Mac/Linux ps equivalent) and chewed idle CPU. Now they all share one recent scan, refreshed twice a second, instead of each running their own.

Evidence

New windows-agent-foreground-process-scan-volume.test.ts mirrors the POSIX #6288 guard: mocks execFile, drives PANE_COUNT panes concurrently across the cadence window, and asserts powershell.exe spawns are bounded by poll ticks, not pane count, while every pane still resolves its foreground descendant. A second case asserts a concurrent burst collapses to a single scan. Reverting the dedup fails both.

POSIX snapshot + ps-scan volume tests are unchanged and green (the generic change defaults to the old string type); node/web/cli typecheck + oxlint clean.

npx vitest run --config config/vitest.config.ts \
  src/main/providers/windows-agent-foreground-process-scan-volume.test.ts \
  src/shared/process-table-snapshot.test.ts \
  src/main/providers/agent-foreground-process-ps-scan-volume.test.ts

This is Windows-only main-process code and can't be exercised end-to-end on a macOS dev machine, so the scan-volume unit test (with a red→green check) is the reproduction harness; a Windows CI run is the real confirmation.

The null-fallback contract is preserved: runWindowsProcessRows throws on total enumeration failure so the miss isn't cached, and queryWindowsProcessDescendants catches it and returns null (callers then fall through to node-pty's process name, exactly as before).

Trade-offs

  • Detection can be up to 500ms staler. A pane may read a scan up to half a second old instead of forking a fresh one. The foreground agent rarely changes within 500ms, and this is exactly the trade the POSIX path already ships — but it is "slightly less fresh, for much less CPU."
  • Mostly helps multi-pane users. A single agent pane is already throttled to ~1 scan/sec by the daemon call site, so the big win is for people with several agent terminals open — plus the degraded/local PTY path, which had no throttle at all. Honest calibration: a 1-agent user won't notice much.
  • Touches a shared file (process-table-snapshot.ts, also used by the SSH relay). The change is purely additive (generic with T = string default → POSIX/relay runtime is byte-identical and re-tested), but the blast radius technically includes that proven path — the reason it was kept type-parametric rather than rewritten.

Scope

src/shared/process-table-snapshot.ts (additive generic) + src/main/providers/windows-foreground-process-rows.ts + test. No behavior change for POSIX/relay.

Windows agent foreground-process inspection forks a whole-process-table
PowerShell/CIM scan per pane on the same 750ms/2000ms cadence the POSIX path
uses. The POSIX side routes through getProcessTableSnapshot (500ms TTL + single
in-flight, #6288/#6667), collapsing N concurrent panes to ~2 scans/sec. The
Windows path (queryWindowsProcessDescendants) had no such dedup: K concurrent
agent panes forked K powershell.exe cold-starts, each enumerating the ENTIRE
process table then filtering per-pid in JS — ~10-40x heavier than `ps` (a
powershell cold start is ~150-400ms CPU + tens of MB RSS). The degraded/local
PTY provider path calls it with no per-pane throttle at all. This is the
Windows analogue of the idle-CPU churn #6288 fixed for POSIX.

Generalize the existing createProcessTableSnapshotReader factory to be generic
over its scan result (default T = string, so the POSIX path and its test are
byte-identical) and add a Windows singleton reader that caches parsed
WindowsProcessRow[]. queryWindowsProcessDescendants now reads the shared
snapshot and runs its own descendant walk; runWindowsProcessRows throws on total
enumeration failure so the miss is not cached and the prior null-fallback
contract (callers fall through to node-pty's name) is preserved.

Windows scan-volume regression test (mirrors the POSIX #6288 guard) drives
PANE_COUNT concurrent panes over the cadence window and asserts powershell.exe
spawns are bounded by ticks, not pane count, while every pane still resolves its
descendant. Reverting the dedup fails both cases. POSIX snapshot + volume tests
unchanged and green; node/web/cli typecheck clean.

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

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ff053053-f4c8-4050-be1b-97392c561ae2

📥 Commits

Reviewing files that changed from the base of the PR and between c3f7900 and 7b4c0c3.

📒 Files selected for processing (1)
  • src/main/providers/agent-foreground-process.test.ts

📝 Walkthrough

Walkthrough

This PR makes the shared process-table snapshot reader generic, uses it to cache Windows process-row enumeration across concurrent descendant queries, and adds a reset helper for tests. queryWindowsProcessDescendants now reads from the shared snapshot reader instead of probing per call, with PowerShell and WMIC enumeration handled in one path. A new Vitest suite verifies scan counts stay bounded across polling ticks and that concurrent pane queries collapse into a single scan.

Changes

Related issues: None identified

Related PRs: None identified

Suggested labels: windows, tests, performance

Suggested reviewers: None identified

Poem:
A shared snapshot trims the repeated scan,
Across the panes, one read is all it can.
Ticks and bursts now fold into one view,
With cached rows returning what they knew.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is helpful but does not follow the required template and omits the Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes sections. Rewrite the PR description using the repository template and fill in each required section, including the testing checklist and security/AI review summaries.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title is specific and matches the main change: deduplicating Windows process-table scans.
✨ 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.

@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.

🧹 Nitpick comments (1)
src/main/providers/windows-foreground-process-rows.ts (1)

26-32: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Trim the comment to 1-2 lines per guideline.

This 7-line block explaining the dedup rationale is much longer than the repo's comment-brevity rule for non-obvious-reason comments.

As per coding guidelines, "Keep comments short — one or two lines, capturing only the non-obvious reason."

✏️ Suggested trim
-// Why: agent foreground inspection forks a whole-process-table PowerShell/CIM
-// scan per pane on the same 750ms/2000ms cadence as the POSIX `ps` path. Without
-// dedup, K concurrent agent panes fork K powershell.exe cold-starts, each ~10-40x
-// heavier than `ps` — the Windows analogue of the idle-CPU churn `#6288/`#6667 fixed
-// for POSIX. Reuse the same TTL + single-in-flight reader, caching parsed rows so
-// a burst of panes collapses to ~2 scans/sec; every caller runs its own descendant
-// walk over the shared snapshot.
+// Why: dedupes per-pane PowerShell/CIM scans (same idle-CPU issue as POSIX `ps`,
+// see `#6288/`#6667) via the shared TTL + single-in-flight reader.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1a448b13-2cb9-47f1-817f-abf4433c9f96

📥 Commits

Reviewing files that changed from the base of the PR and between 85adb2d and c3f7900.

📒 Files selected for processing (3)
  • src/main/providers/windows-agent-foreground-process-scan-volume.test.ts
  • src/main/providers/windows-foreground-process-rows.ts
  • src/shared/process-table-snapshot.ts

The new module-level Windows rows reader caches for 500ms with real
Date.now(), so one case's mocked process table was served to the next
case's assertions (7 CI failures in agent-foreground-process.test.ts).
Mirror the suite's existing POSIX resetProcessTableSnapshotForTests()
with the Windows reset in beforeEach.

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

nwparker commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Took ownership; re-verified and fixed the CI failure before merge.

The verify CI failure was real: the new module-level Windows rows reader caches parsed rows for 500ms (real Date.now()), so in the pre-existing agent-foreground-process.test.ts one case's mocked process table was served to the next case within the TTL — 7 win32 cases got codex instead of their expected resolution. Fixed in 7b4c0c3 by adding resetWindowsProcessRowsSnapshotForTests() to that suite's beforeEach, mirroring its existing POSIX reset. Production code needed no change.

Independent verification (adversarial review agent + local re-runs):

  • Red→green re-confirmed: bypassing the dedup fails both scan-volume cases (240 scans vs the 41 bound; 6 vs 1 in the burst case).
  • Caller equivalence confirmed: null-on-total-failure contract preserved; wmic fallback still happens inside each scan; empty-table and concurrent-rejection behavior identical; no mutation hazard (rows are spread into fresh objects per call).
  • POSIX/relay path: generic change is type-level only; both getProcessTableSnapshot consumers runtime-identical.
  • Staleness: all call cadences (750ms/2000ms/3s) exceed the 500ms TTL, so a single pane's sequential polls always scan fresh; only same-tick concurrent panes share — the intended win.
  • Full local unit suite: 2334 files / 24,241 tests passed, 0 failed; typecheck (node/cli/web) and lint clean.

No further improvements taken without tradeoffs: sharing the snapshot with the other Win32_Process sites (memory collector, port scanner, daemon health) would change query shapes/freshness semantics, and caching the descendant map is negligible next to the PowerShell fork cost.

@nwparker
nwparker merged commit 91323cf into main Jul 5, 2026
1 check passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#7384)

* perf(windows): dedupe per-pane process-table scans in agent inspection

Windows agent foreground-process inspection forks a whole-process-table
PowerShell/CIM scan per pane on the same 750ms/2000ms cadence the POSIX path
uses. The POSIX side routes through getProcessTableSnapshot (500ms TTL + single
in-flight, stablyai#6288/stablyai#6667), collapsing N concurrent panes to ~2 scans/sec. The
Windows path (queryWindowsProcessDescendants) had no such dedup: K concurrent
agent panes forked K powershell.exe cold-starts, each enumerating the ENTIRE
process table then filtering per-pid in JS — ~10-40x heavier than `ps` (a
powershell cold start is ~150-400ms CPU + tens of MB RSS). The degraded/local
PTY provider path calls it with no per-pane throttle at all. This is the
Windows analogue of the idle-CPU churn stablyai#6288 fixed for POSIX.

Generalize the existing createProcessTableSnapshotReader factory to be generic
over its scan result (default T = string, so the POSIX path and its test are
byte-identical) and add a Windows singleton reader that caches parsed
WindowsProcessRow[]. queryWindowsProcessDescendants now reads the shared
snapshot and runs its own descendant walk; runWindowsProcessRows throws on total
enumeration failure so the miss is not cached and the prior null-fallback
contract (callers fall through to node-pty's name) is preserved.

Windows scan-volume regression test (mirrors the POSIX stablyai#6288 guard) drives
PANE_COUNT concurrent panes over the cadence window and asserts powershell.exe
spawns are bounded by ticks, not pane count, while every pane still resolves its
descendant. Reverting the dedup fails both cases. POSIX snapshot + volume tests
unchanged and green; node/web/cli typecheck clean.

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

* test: reset windows process-rows snapshot between agent-foreground cases

The new module-level Windows rows reader caches for 500ms with real
Date.now(), so one case's mocked process table was served to the next
case's assertions (7 CI failures in agent-foreground-process.test.ts).
Mirror the suite's existing POSIX resetProcessTableSnapshotForTests()
with the Windows reset in beforeEach.

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

---------

Co-authored-by: Orca <[email protected]>
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