Skip to content

perf(windows): relax whole-process-table scans for terminals with no agent evidence - #7607

Merged
brennanb2025 merged 5 commits into
mainfrom
brennanb2025/perf-win-process-scan
Jul 8, 2026
Merged

brennanb2025 merged 5 commits into
mainfrom
brennanb2025/perf-win-process-scan

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On Windows, agent-completion tracking (default-on via notifications) inspects the terminal's process tree by forking powershell.exe -NoProfile ... Get-CimInstance Win32_Process — a whole-process-table scan costing a ~250–700ms PowerShell cold-start per fork (plus a Defender scan of every launch), ~10–40× heavier than the POSIX ps path.

A single visible, idle, plain shell with no agent paid that scan every 2s, forever (~30 cold-starts/min): the renderer cadence (IDLE_POLL_INTERVAL_MS = 2000) is gated only on tracking-enabled + visibility, and the shared 500ms-TTL snapshot (src/shared/process-table-snapshot.ts) dedupes concurrent panes within a tick but does not slow the tick. Windows carries 64% of Orca crash reports plus an open hang cluster (STA-1073, STA-1248, STA-1169), so idle overhead there is expensive.

Design

Renderer (shared scheduling, platform-clean). The coordinator's cadence tiers (active 750ms / idle 2s / hidden 3s from #6667) gain a fourth tier: no-evidence = 15s, applied only when the pane has no agent evidence and the new injected isProcessInspectionCostly() option returns true. pty-connection supplies the policy with runtime checks: Windows renderer and local PTY (SSH ssh:/remote remote: PTYs run their scans on the remote host, so they keep full cadence). The coordinator itself stays platform-free.

Event-driven escalation back to the hot cadence, so agent-start detection does not wait out the 15s interval:

  • PTY output — new observeOutputActivity() wired into the pane data callback (a starting agent always prints); opens a 10s hot window at the full 2s idle cadence, re-arming an armed slow timer immediately.
  • Title change / hook status — same activity bump; an explicit agent title or recognized hook additionally establishes evidence, which switches to the standard hot tiers outright.
  • Inspection hit — a recognized foreground agent re-arms at the 750ms active cadence (generalizes the existing hidden→visible re-arm: any tier change toward a faster cadence replaces the armed timer).

Daemon (persisted sessions). pty-subprocess.ts runs its own scan when readers call getForegroundProcess (throttled at 5s for no-identity shells). That throttle now has the same shape: 15s on win32 for an idle shell with no cached agent identity and no output in the last 10s; output re-arms the 5s retry. This bounds daemon scans regardless of how many readers poll. POSIX keeps 5s; cached agent identities keep the 1s refresh TTL.

Latency tradeoff (stated bound)

  • Agent-START on an idle local Windows shell: worst case ≤15s + jitter at the renderer layer, but only for an agent that produces no output, no title change, and no hook before detection — in practice output escalates detection to ~2s (renderer) / ~5s (daemon identity), matching pre-fix behavior. One measured nuance on daemon-persisted sessions: the renderer's 15s polls and the daemon's 15s refresh floor can beat against each other (a poll landing at 15s−jitter is skipped by the daemon throttle), so a fully silent agent's degenerate worst case there is ~2 intervals (~30s). Any real agent prints (TUI repaint alone), which the on-device run confirmed at 1.8s attribution; if we ever want a hard ≤16s bound instead, drop the daemon floor to ~12s so renderer polls always clear it (costs +1 scan/min).
  • Agent-FINISH: unchanged. Completion detection requires agent evidence, and evidenced panes keep the 750ms/2s cadences; daemon sessions with a cached identity keep the 1s refresh TTL. Verified by test.
  • macOS/Linux/SSH/remote: unchanged (cheap-host panes keep the 2s idle cadence; verified by test).
  • Multi-pane dedupe (#6288 guards): untouched; both existing scan-volume suites pass.

Cost effect

Scenario (local Windows) Before After
Visible idle shell, no agent ~30 CIM scans/min 4/min
Same, daemon-persisted session ~12/min (5s retry) 4/min read-driven, 15s floor
Active agent (finish detection) 750ms cadence unchanged

Tests

Deterministic scan-count regression tests mirroring the #6288 guard style (pinned jitter, fake timers, exact counts):

  • agent-completion-no-evidence-cadence.test.ts (renderer): idle no-evidence pane → exactly 4 inspections/60s on a costly host (pre-fix 30); cheap host and no-cost-source coordinators stay at 30; output/title escalation re-arms within 2s; hot window decays back to 15s (exact 5-then-3 poll counts); recognized agent keeps ≥70 inspections/60s; unannounced agent exit after escalation still dispatches completion.
  • pty-subprocess-foreground-scan-cadence.test.ts (daemon): idle win32 shell polled every 2s for 60s → exactly 4 scans (pre-fix 11); output re-arms the fast retry and the resolved identity serves from cache; cached-identity sessions refresh every read past the 1s TTL; POSIX idle shell keeps the 5s retry (exact 6 scans/30s).

All 161 tests across the touched suites pass; pnpm typecheck clean; oxfmt/oxlint clean. (Pre-existing on main and unrelated: pty-connection.test.ts + 4 sibling suites fail to resolve a path alias in vitest — identical failure set with and without this change.)

Real-Windows validation

Measured on a Windows machine running this branch (dev build, daemon-persisted session), using a branch-owned execFile('powershell.exe') probe to avoid cross-instance noise:

  • Idle scan rate: 6 foreground scans over 125s; steady-state tail 2.85 scans/min (target ≤4/min, pre-fix ~30/min). Gap pattern alternates ~15–16s and ~29–30s — the renderer×daemon throttle beat described above.
  • Agent-start attribution: Codex icon/attribution appeared ~1.8s after launch output (output-escalation path working as designed).
  • Agent-finish: completion state and in-app unread markers fired promptly (agent pane +5ms, terminal tab +320ms after done).

Adversarial review outcome

Two independent review passes (correctness/lifecycle lens and environment-matrix lens) plus a perf audit over the final diff. No Critical/High findings. Two items worth recording:

  • Intentional behavior delta (was Medium): hook-only coordinators (agent-hook-completion-notifications.ts — panes on worktrees without a mounted pane) never call startProcessTracking, so the new gate stops their stub cadence polls. On main those polls' null inspections cleared workingStatusObserved ~2s after each hook, silently bypassing the designed 1.5s done quiet window; with the gate the quiet window applies there as designed (≤1.5s added notification latency on that path, in exchange for suppressing premature milestone-done notifications, and the perpetual 2s stub-poll loop is gone). Pinned as intentional by the "keeps cadence disarmed and the done quiet window intact" test. Mounted panes are unaffected (primary coordinator unchanged; notification IDs dedupe).
  • Known residual (Low, pre-existing cadence): isProcessInspectionCostly keys on the renderer's UA, so a paired web/mobile client driving a Windows host keeps 2s terminal.inspectProcess RPCs. The daemon-side 15s throttle still bounds actual CIM scans host-side regardless of caller, so the residual is RPC chatter, not scans — except in degraded-daemon mode (unthrottled LocalPtyProvider scans), which is rare and matches pre-PR cadence. Follow-up idea: derive "costly" from a host capability flag over runtime RPC instead of the client UA.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR adds tiered polling cadence controls for foreground and process-inspection checks in the daemon and renderer. The daemon now tracks recent PTY output to slow idle Windows foreground scans. The renderer coordinator adds a no-evidence polling tier, activity tracking, and a new output-activity signal. PTY connection code now reports output activity and narrows the costly-inspection check to local Windows PTYs. Regression tests cover the new cadence behavior and timer handling.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 concise and accurately summarizes the main change: relaxing Windows terminal scans when no agent evidence is present.
Description check ✅ Passed The description is detailed and covers the change, testing, validation, and risks, but it doesn't use the template's Screenshots/Security Audit/Notes headings.
✨ 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/daemon/pty-subprocess.ts (1)

71-72: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Keep the 15s retry floor in sync with the renderer. This matches NO_EVIDENCE_POLL_INTERVAL_MS now, but the duplicated constant can drift; source both from one shared value.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e652e267-460f-4752-b793-0271059ac9d3

📥 Commits

Reviewing files that changed from the base of the PR and between aa3936c and b1a1d61.

📒 Files selected for processing (6)
  • src/main/daemon/pty-subprocess-foreground-scan-cadence.test.ts
  • src/main/daemon/pty-subprocess.ts
  • src/renderer/src/components/terminal-pane/agent-completion-coordinator-types.ts
  • src/renderer/src/components/terminal-pane/agent-completion-coordinator.ts
  • src/renderer/src/components/terminal-pane/agent-completion-no-evidence-cadence.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Priority: P0 — from the 2026-07-06 perf audit.

Issue: on Windows, any visible terminal pane — even an idle shell with no agent — triggered a whole-process-table Get-CimInstance Win32_Process scan via a fresh powershell.exe cold-start (~250–700ms, Defender-scanned) every 2 seconds, forever (~30 spawns/min per machine). Biggest identified idle CPU/battery drain on Windows, adjacent to the AppHang/Not-Responding cluster (STA-1073, STA-1248).

This PR: adds a 15s no-evidence tier (local Windows only) with event-driven escalation back to the hot cadence on PTY output/title/hook/inspection evidence; agent-finish detection and macOS/Linux/SSH cadences unchanged; daemon-owned sessions covered.

brennanb2025 and others added 2 commits July 6, 2026 19:10
…agent evidence

A visible, idle terminal with no agent evidence previously drove a
whole-process-table powershell.exe/CIM scan every 2s forever on Windows
(~30 cold-starts/min). The agent-completion coordinator now relaxes such
panes to a 15s no-evidence cadence on costly hosts (local Windows only),
with event-driven escalation back to the hot cadence on PTY output,
title changes, hook status, or an inspection hit. The daemon's
foreground refresh for persisted sessions gains the same tier: idle
no-evidence Windows shells retry at 15s instead of 5s unless output
arrived within the last 10s.

Agent-finish detection is unchanged: panes with agent evidence keep the
existing 750ms/2s cadences, cached daemon identities keep the 1s refresh
TTL, and macOS/Linux/SSH/remote panes keep full cadence everywhere.

Co-authored-by: Orca <help@stably.ai>
The new activity bumps (output/title/hook) could arm cadence poll timers
before the bind path called startProcessTracking, which made
pty-connection tests' timer-drain helpers spin forever on CI (4 failures
in verify). scheduleNextPoll now no-ops until startProcessTracking has
run once, matching the production lifecycle where cadence only begins at
PTY bind; coordinator tests that relied on observeTitle alone starting
polls now start tracking explicitly.

Co-authored-by: Orca <help@stably.ai>
@brennanb2025
brennanb2025 force-pushed the brennanb2025/perf-win-process-scan branch from b1a1d61 to 915592f Compare July 7, 2026 05:00
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Real-Windows validation results

Validated on a physical Windows machine running this branch (dev build, daemon-persisted session). Idle scan counting used a branch-owned execFile('powershell.exe') probe rather than a global spawn watcher, to avoid noise from other Orca dev instances on the machine.

Idle scan rate (headline fix): 6 foreground scans over 125s; steady-state tail 2.85 scans/min vs ~30/min pre-fix and the ≤4/min target. Scan timestamps (s): 4.66, 23.38, 52.21, 82.00, 97.85, 114.11 — gaps alternate ~15–16s and ~29–30s, the renderer×daemon throttle beat described in the PR body's latency section.

Agent-start attribution: Codex foreground attribution/icon appeared ~1.8s after launch output — the output-escalation path working as designed (the theoretical 15s bound only applies to an agent that never prints).

Agent-finish: Codex done was reached normally; in-app completion/unread markers fired promptly (agent pane +5ms, terminal tab +320ms). Completion-notification latency is unchanged, as designed.

Caveats from the run: the harness shell wasn't elevated, so the OS-native notification banner itself wasn't observed — but the coordinator's dispatch (which is all this PR touches) is proven by the in-app markers above; the native-banner path is untouched downstream code. Evidence JSON/screenshots are on the validation machine under test-results/pr7607-*.

brennanb2025 and others added 2 commits July 8, 2026 12:43
The 10s error-backoff ceiling would undercut the 15s no-evidence tier
(erroring scans polling more often than healthy ones); the Math.max cap
prevents that but had no regression guard.

Co-authored-by: Orca <help@stably.ai>
…indow intact

Adversarial review of the pollTrackingStarted gate surfaced the one real
behavioral delta: hook-only coordinators (which never start process
tracking) previously armed stub cadence polls whose null inspections
cleared workingStatusObserved ~2s after each hook, silently bypassing
the designed 1.5s done quiet window on panes only the hook coordinator
observes. The gate makes them purely push-driven; this pins that as
intentional.

Co-authored-by: Orca <help@stably.ai>
@brennanb2025
brennanb2025 merged commit c8c5201 into main Jul 8, 2026
2 checks passed
Jinwoo-H added a commit that referenced this pull request Jul 9, 2026
Clean auto-merge, no conflicts. Brought in from main:
- daemon boot hardening: fail builds/CI when terminal daemon can't start;
  plain-node-entry-guard + verify-packaged-daemon-entry (electron require
  leaked into daemon-entry chunk, #7844/#7849)
- perf(windows): relax whole-process-table scans for terminals with no
  agent evidence (#7607) — foreground-scan-cadence
- fix(agents): route WSL OMP hooks to the Windows listener (#7565/#7641)
- fix: recover fresh local terminal spawns from a deleted saved cwd (#7847)
- native-chat: upload composer attachments over SSH (#7832)
- mobile: keyboard dismiss control; agent-tab launch fix
- new GitHub issue draft persistence (#7778)

Perf-branch structure preserved (clean auto-merge; no restructured terminal
files conflicted). Gates: typecheck 0 errors; terminal-pane + ipc/pty +
daemon + pane-manager suites 4497 passed / 14 skipped / 0 failed.

Co-authored-by: Orca <help@stably.ai>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…agent evidence (stablyai#7607)

Co-authored-by: Orca <help@stably.ai>
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