Skip to content

fix(terminal): don't report agent exit on a degraded foreground scan - #9244

Merged
brennanb2025 merged 2 commits into
mainfrom
brennanb2025/fix-agent-done-false-completion
Jul 18, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennanb2025/fix-agent-done-false-completion

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Problem

Windows users running many Claude agents across ~10+ worktrees report too many "agent done" notifications, and notifications that fire while the agent is still working — clicking one navigates to the worktree and the agent is mid-task.

Root cause (false "agent done")

The completion coordinator runs three parallel completion sources (hook / title / process-exit). The process-exit backstop declares "done" when the foreground process is no longer a recognized agent for two consecutive samples.

On Windows/ConPTY there is no foreground process group, so foreground detection forks powershell.exe … Get-CimInstance Win32_Process … ConvertTo-Json with a hard 3s timeout (windows-foreground-process-rows.ts). Measured on a real Win11 24H2 box under load (concurrent scans + CPU pressure), that fork+scan took 2.94–4.57s — repeatedly over the 3s budget. When it times out it's killed → returns null → and the wmic fallback is removed on Win11 24H2+, so getForegroundProcess drops to node-pty's .process, which transiently reads the shell name. The coordinator reads that shell as "the agent exited" and fires a false completion while the agent is still working.

Notably this does not require dead hooks — the process-exit suppressor is same-agent-blind (it only vetoes a different agent), and a "working" hook status stays fresh for 30 minutes, so neither protects a working agent from a flaky inspection. macOS is unaffected because its ps-based inspection is fast and never times out.

Fix

Make a degraded (available: false) foreground scan report the last positively-recognized agent instead of the shell fallback, so a transient scan failure no longer looks like an exit:

  • New pure helper src/main/providers/stable-foreground-process.ts (resolveStableForegroundProcess).
  • Wired into LocalPtyProvider.getForegroundProcess via a per-PTY ptyLastRecognizedForeground cache (cleared on teardown / when the PTY is gone).

An authoritative (available: true) scan still refreshes or clears the memory, so a genuine exit or crash is still detected. This is a no-op on macOS, where the scan is always available — the change only engages on the Windows timeout path.

Trade-off: under sustained scan timeouts, a real exit registers slightly later (once a scan succeeds) — the safe direction vs. a false completion, and the hook Stop still fires the real completion on a Git-for-Windows box.

Testing

  • stable-foreground-process.test.ts — 5/5, including a regression guard that a real exit is still detected (authoritative scan clears the memory).
  • 527 tests pass across the completion-coordinator suite + the full src/main/providers dir; typecheck:node clean; oxlint/oxfmt clean.

Scope

This fixes only the false completion. The flood half — no global notification rate cap (cooldown is per-worktree), suppressWhenFocused covering only the active worktree, and the parked-watcher second completion path — is platform-independent and a separate follow-up PR.

Live validation on real Windows (the CIM-timeout load rig) is in progress against this branch.

On Windows/ConPTY the foreground scan (a Get-CimInstance Win32_Process
PowerShell fork) can exceed its 3s budget under load, and there is no
wmic fallback on Win11 24H2+. A timed-out scan returned the shell name,
which the completion coordinator reads as "agent exited" and fires a
false "agent done" notification while the agent is still working.

Prefer the last positively-recognized agent across a degraded
(available:false) scan so a transient failure no longer looks like an
exit. An authoritative (available:true) scan still refreshes/clears the
memory, so a genuine exit or crash is still detected. No-op on macOS,
where the scan is always available.
@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@brennanb2025, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 4 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: eb4c8433-175e-4195-ae50-d2d7bc2a416f

📥 Commits

Reviewing files that changed from the base of the PR and between c96b4c3 and 213d936.

📒 Files selected for processing (2)
  • src/main/providers/local-pty-provider.test.ts
  • src/main/providers/local-pty-provider.ts
📝 Walkthrough

Walkthrough

Adds resolveStableForegroundProcess and the StableForegroundProcess type to preserve recognized agent foregrounds across unavailable inspections. New tests cover authoritative and degraded scan behavior, remembered agents, and cache clearing. LocalPtyProvider now maintains per-PTY recognized foreground state, uses the resolver during foreground detection, returns cached values on inspection errors, and removes cached state when PTYs are missing or torn down.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the problem, fix, and testing, but it misses the required template sections like Summary, Screenshots, AI Review Report, Security Audit, and Notes. Rewrite the description to follow the repository template and add the missing sections, including Summary, Screenshots, AI Review Report, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: preventing false agent-exit reports on degraded foreground scans.
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.

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/local-pty-provider.ts (1)

1210-1214: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use an explicit null check.

Since lastRecognizedAgent is typed as string | null, it's generally safer and more idiomatic to explicitly check for !== null rather than relying on truthiness, even though agent process names are not expected to be empty strings.

♻️ Proposed refactor
-      if (stable.lastRecognizedAgent) {
+      if (stable.lastRecognizedAgent !== null) {
         ptyLastRecognizedForeground.set(id, stable.lastRecognizedAgent)
       } else {
         ptyLastRecognizedForeground.delete(id)
       }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9d661d41-6bf2-42f9-9e1d-a6e8919ff452

📥 Commits

Reviewing files that changed from the base of the PR and between 7c0b84f and c96b4c3.

📒 Files selected for processing (3)
  • src/main/providers/local-pty-provider.ts
  • src/main/providers/stable-foreground-process.test.ts
  • src/main/providers/stable-foreground-process.ts

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Validation on real Windows 11 24H2 (branch head c96b4c3)

Validated by an agent on a real Windows box with the CIM-timeout load rig.

PASS (authoritative):

  • Unit test stable-foreground-process.test.ts → 5/5 on Windows (degraded available:false → returns last recognized agent; available:true → refreshes/clears).
  • Confirmed the scan genuinely times out under load (raw CIM Get-CimInstance Win32_Process measured 5.41s vs the 3s budget).
  • No regression: the hook Stop still fires real completions — agentStatusByPaneKey transitioned to done at the true end of every task. Legitimate completion is preserved.
  • Symptom did not reproduce with the fix: across working tick-loops, idle, and sustained CIM-timeout load (getForegroundProcess returning powershell/kids:false for 14–16 consecutive samples), the pane never entered unreadAgentCompletionPanes.

Caveat (honest): the pre-fix false completion could not be reproduced in that exact rig — the process-exit backstop didn't engage as the completion driver there (hooks dominated), so live end-to-end causation isn't fully proven. The fix's correctness rests on the (authoritative) unit test plus the confirmed timeout behavior.

Known limitation surfaced (follow-up candidate, not addressed here): under pathological self-load (~12 claude.exe), ~20/30 idle probes returned the shell, consistent with an available:true-but-incomplete CIM snapshot (scan succeeds, shell root present, but the agent descendant row missing) → resolves to the shell and clears the cache. This fix guards only the available:false (timeout) path; it deliberately does not cache-fallback on available:true, because that is indistinguishable from a real exit and would regress genuine exit/crash detection. Likely amplified by the pathological process count; mitigated by working hooks. A safe follow-up would cache the recognized agent's PID and gate exit-confirmation on PID liveness, which distinguishes an incomplete snapshot from a real exit without the regression risk.

@brennanb2025
brennanb2025 merged commit 2f5fc65 into main Jul 18, 2026
2 checks passed
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Cross-reference for the record: this fix lives in the local PTY provider, which on Windows is only the daemon-unavailable fallback path — Windows PTYs are hosted by the terminal daemon, so this change is inert on the default Windows configuration (it's a correct, no-regression improvement to the fallback path, just not the primary one). #9258 carries the equivalent fix on the daemon foreground path, which is what actually lands the Windows false-"agent done" fix.

enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#9244)

* fix(terminal): don't report agent exit on a degraded foreground scan

On Windows/ConPTY the foreground scan (a Get-CimInstance Win32_Process
PowerShell fork) can exceed its 3s budget under load, and there is no
wmic fallback on Win11 24H2+. A timed-out scan returned the shell name,
which the completion coordinator reads as "agent exited" and fires a
false "agent done" notification while the agent is still working.

Prefer the last positively-recognized agent across a degraded
(available:false) scan so a transient failure no longer looks like an
exit. An authoritative (available:true) scan still refreshes/clears the
memory, so a genuine exit or crash is still detected. No-op on macOS,
where the scan is always available.

* fix(terminal): drop stale foreground scan results
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