Skip to content

fix(windows): stop foreground ConPTY helper fanout - #16074

Closed
OrcaWin wants to merge 5 commits into
mainfrom
OrcaWin/aug23-triage-sta2715
Closed

OrcaWin wants to merge 5 commits into
mainfrom
OrcaWin/aug23-triage-sta2715

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Aug 23, 2026 •

Copy link
Copy Markdown
Collaborator

ELI5

To work out which program is currently in the foreground of a Windows terminal pane — is Claude still running, or has it exited back to the shell? — Orca asked Windows for the list of processes attached to that pane's console.

node-pty answers that question by forking a little helper process each time you ask.

Orca asks on a poll, per pane. So with panes open and agents running, that is one short-lived conpty_console_list_agent process per read, per pane, forever. On the reported machines it is hundreds of hidden processes; killing them does nothing because the next poll spawns more. RAM goes, and the machine gets unusable within minutes.

The fix is to stop asking. That probe was always optional: it only ever confirmed a cached answer. So instead of forking a helper, we now return "unverifiable", and every caller already has a conservative path for that.

The measurement, from a fixture that issues 32 concurrent reads for distinct PTY roots:

forks live helper processes
main today 32 32
this PR 0 0

User-facing trade-offs

Superseded — see the correction comment below. This section was written from one call site; there are three, and the third makes the cost materially larger than described here. Kept for history rather than silently edited.


Summary

  • stop local/daemon foreground polling from spawning one conpty_console_list_agent subprocess per read
  • return null (unverifiable) at that boundary, so callers use their existing conservative degraded scan instead of publishing stale console membership
  • leave node-pty teardown, hidden PTYs, SSH relay patching, native jobs, and every cross-platform path unchanged
  • add a physical-Windows red/green fault injection and run it in Windows PR CI

Relates to #10857 and STA-2715.

Invariant and owner

Falsifiable invariant: any number of foreground membership reads creates exactly zero console-list subprocesses and returns unverifiable.

The authoritative owner is src/main/providers/windows-conpty-process-membership.ts. It previously forked a one-shot helper without owning the foreground poll/cancellation topology. Local and daemon providers consume its answer and already treat null conservatively.

The other consumer is node-pty's system-ConPTY teardown path. node-pty owns that helper, its deadline, and the PTY teardown. Hidden PTYs and the Windows SSH relay reach that existing owner. This PR does not pool or admission-limit it: physical review proved that global load shedding skipped concurrent console-member cleanup, while detached descendants are outside GetConsoleProcessList entirely. The final diff therefore does not change node-pty or SSH behavior.

Reproduction and topology

Current origin/main is affected. The identical Windows fixture issues 32 concurrent reads for distinct PTY roots and stalls every helper:

  • current main / fix disabled: 32 forks, 32 simultaneously live node.exe children running conpty-console-list-stall.cjs, then zero after deadline cleanup; the zero-helper assertion fails
  • candidate: 0 forks, 0 live helpers, pass
  • candidate teardown: no timers, listeners, child handles, mutable admission state, or membership cache exists; every call settles immediately to null

This distinguishes the defect as unbounded active foreground fanout, not a persistent post-settlement leak. The two independent signals are injected fork count and OS PID liveness. The disabled arm uses the same assertion and confirms cleanup returns to zero.

Design decision

Compared before coding/rework:

  1. Caller-local deadlines or kill sweeps: rejected because they retain N helpers and duplicate ownership.
  2. A process-wide pool or one-slot node-pty admission owner: rejected after physical concurrency review showed it regressed system-ConPTY descendant cleanup; serializing teardown also cannot recover exact membership from a stalled helper.
  3. Native job ownership: already owns explicit local/daemon PTY tree teardown, but it is not authoritative for foreground application identity and stock SSH node-pty cannot assume the patched native API.
  4. Simplest viable alternative (selected): remove the optional foreground probe and fail closed at its owner.

Downside: production local/daemon Windows foreground confirmation no longer gets exact console membership from this path. Recognized agent routing may remain conservative or use existing process-table/hook evidence. This is intentional: unavailable is safer than stale identity or subprocess fanout.

Evidence

  • focused final slice: 6 files, 53/53 tests passed
  • direct-import ratchet: 4/4 passed after removing the obsolete child_process allowlist entry
  • Node typecheck passed
  • oxlint and oxfmt passed
  • reliability manifest and max-lines ratchet passed
  • Electron/Playwright CDP golden: rendered daemon-backed ConPTY accepted typed input/output and passed in 41.6s
  • final diff check passed
  • three Codex reviewers (architecture/ownership, concurrency/failure, topology/tests) are clean after the boundary reset

The earlier pushed design produced expected CI failures in its now-deleted admission test and stale child_process allowlist. Commit 8cc16c6 removes both causes. Fresh CI run 32643375247 is green, including Windows packaging, static analysis, compatibility checks, all 32 Node test shards, and the aggregate verify job.

External review state

Draft. OpenCode review remains blocked because opencode is not installed on Physical Windows 2; no installation was attempted. Linear remains unavailable as linear_not_connected, so the full Linear issue/comment/attachment read could not be completed. GitHub #10857 and linked PRs #15749/#15755 were read as evidence. No Linear/GitHub comments were posted.

@OrcaWin OrcaWin changed the title fix(windows): bound ConPTY console-list helpers fix(windows): remove unverifiable ConPTY membership probes Aug 23, 2026
@OrcaWin OrcaWin changed the title fix(windows): remove unverifiable ConPTY membership probes fix(windows): bound ConPTY console-list helpers at their owner Aug 23, 2026
@OrcaWin OrcaWin changed the title fix(windows): bound ConPTY console-list helpers at their owner fix(windows): stop foreground ConPTY helper fanout Aug 23, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Correcting the trade-off section I added to this PR body — it was wrong, and understated the cost.

I wrote that there was "one, small" trade-off in the safe direction. I had read one call site. There are three, and the third changes the conclusion.

src/main/providers/windows-agent-foreground-process.ts:74-79:

if (hasRecognizedCandidate && options.readWindowsConptyProcessIds) {
  const conptyProcessIds = await options.readWindowsConptyProcessIds()
  if (!conptyProcessIds) {
    return { available: false, processName: null }
  }

With a permanent-null stub, this returns available: false whenever a recognized agent candidate exists — that is, precisely while the agent is running. It is reached from confirmForegroundProcess in both the local provider (local-pty-provider.ts:1483) and the daemon (foreground-process-tracker.ts:250).

And the retire path (foreground-process-tracker.ts:152-155) early-returns on null, so retireStaleForegroundIdentity() is never called on that branch.

So the honest cost is not "agent-finished detected slightly later". It is:

  • poll-driven agent-finished detection disabled, not delayed — exit detection survives only through event-driven OSC-133/visibility confirms, which cmd.exe and Git Bash do not emit;
  • foreground resolution reporting unavailable while an agent runs, which gates Windows key-routing trust and lets the renderer's command-finished ladder drop a live agent's pane identity after an OSC 133;D leak — the exact leak that tracker exists to refute.

That is a real regression, not a conservative degradation, and my summary hid it. Apologies — the ELI5 and the measurement stand, the trade-off list did not.

There is a better design, and it is already in the tree

listPtyJobProcessIds (src/main/windows/windows-pty-job.ts:117) has zero production callers. QueryInformationJobObject works from any process holding the job handle — no console attachment, no fork, one syscall, kernel-authoritative. That is the reason node-pty forks in the first place: GetConsoleProcessList must run from a process attached to the console. The job has no such constraint.

All three call sites keep their semantics: size > 1 keeps the cache, root-only retires, membership filters candidates. The only semantic delta is that descendants detached from the console remain in the job — which errs conservative in every one of those callers.

This PR rejected the job object as "not authoritative for foreground application identity". That is true but beside the point: the probe was never used for identity, only for membership and liveness, where the job is authoritative. Coverage matches too — patched node-pty covers local and daemon, and the SSH relay never called this module.

Recommended: reimplement windows-conpty-process-membership over listPtyJobProcessIds(proc) (every caller already holds the IPty), keep null meaning unverifiable, and fix site 3 so a null no longer fails the whole resolution.

Floor, if pure deletion is preferred: delete at the callers rather than leaving a permanent-null stub — remove the readWindowsConptyProcessIds option and the available: false branch, plus the two now-inert branches at local-pty-provider.ts:1413-1440 and foreground-process-tracker.ts:150-158. As it stands the stub leaves dead branches and comments that no longer describe the code.

Happy to implement the job-object version if you want it.

@OrcaWin

OrcaWin commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator Author

Closing because the polling fix is already implemented in current main, and this PR's remaining permanent-null implementation would discard useful correctness behavior.

At main 07b5c083bd995457a503353cb059685622719759, readWindowsPtyJobProcessIds reads native job membership without console-helper forks. Both local polling and daemon polling use it to retain live anchors and retire identities with exit evidence. Selective console confirmation remains for decisions that require exact attached membership; it is distinct from the repeated polling this PR targets.

The PR head instead always returns null. That prevents poll-driven retirement and makes fresh foreground resolution unavailable when a recognized candidate exists, as the correction comment also documents. The zero-helper fixture therefore proves reduced work by removing functionality, while main already supplies the useful polling change. This does not meet the performance bar in discussion #27143.

This disposition is based on the intended parent-to-head delta and current main's callers, not merge conflicts. Native Windows execution was not repeated in this audit. Branch preserved.

@OrcaWin OrcaWin closed this Oct 27, 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.

2 participants