From 755bf3aa0d4202b60080f4d12a0d4288c4c15aca Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 4 Apr 2026 20:40:03 +0000 Subject: [PATCH] Fix Windows process tree traversal, remove dead code, deduplicate normalizeRunningPorts - win32.ts: Replace direct-child-only query with full BFS traversal (collectWindowsProcessFamilyPids) to match POSIX behavior for detecting grandchild processes and their listening ports - index.ts: Remove unused subprocessCheckerToInspector export and clean up unused type imports - Deduplicate normalizeRunningPorts by exporting from terminalStateStore and importing in Sidebar.tsx and ThreadTerminalDrawer.tsx --- .../src/terminalProcessInspector/index.ts | 30 +++++-------- .../src/terminalProcessInspector/win32.ts | 45 +++++++++++++++---- apps/web/src/components/Sidebar.tsx | 13 +++--- .../src/components/ThreadTerminalDrawer.tsx | 13 +++--- apps/web/src/terminalStateStore.ts | 2 +- 5 files changed, 58 insertions(+), 45 deletions(-) diff --git a/apps/server/src/terminalProcessInspector/index.ts b/apps/server/src/terminalProcessInspector/index.ts index 21871d5b8a..cfb71ac979 100644 --- a/apps/server/src/terminalProcessInspector/index.ts +++ b/apps/server/src/terminalProcessInspector/index.ts @@ -1,11 +1,6 @@ import { collectPosixProcessFamilyPids, checkPosixListeningPorts } from "./posix"; -import { - type TerminalSubprocessActivity, - type TerminalSubprocessChecker, - type TerminalSubprocessInspector, - type TerminalWebPortInspector, -} from "./types"; -import { checkWindowsListeningPorts, collectWindowsChildPids } from "./win32"; +import { type TerminalSubprocessActivity } from "./types"; +import { checkWindowsListeningPorts, collectWindowsProcessFamilyPids } from "./win32"; export { arePortListsEqual, normalizeRunningPorts } from "./utils"; @@ -24,11 +19,15 @@ export async function defaultSubprocessInspector( } if (process.platform === "win32") { - const childPids = await collectWindowsChildPids(terminalPid); - const processPidsForPortScan = [terminalPid, ...childPids]; - const runningPorts = await checkWindowsListeningPorts(processPidsForPortScan); + const processFamilyPids = await collectWindowsProcessFamilyPids(terminalPid); + if (processFamilyPids.length === 0) { + return { hasRunningSubprocess: false, runningPorts: [] }; + } + + const subprocessPids = processFamilyPids.filter((pid) => pid !== terminalPid); + const runningPorts = await checkWindowsListeningPorts(processFamilyPids); return { - hasRunningSubprocess: childPids.length > 0 || runningPorts.length > 0, + hasRunningSubprocess: subprocessPids.length > 0 || runningPorts.length > 0, runningPorts, }; } @@ -45,12 +44,3 @@ export async function defaultSubprocessInspector( runningPorts, }; } - -export function subprocessCheckerToInspector( - subprocessChecker: TerminalSubprocessChecker, -): TerminalSubprocessInspector { - return async (terminalPid: number) => ({ - hasRunningSubprocess: await subprocessChecker(terminalPid), - runningPorts: [], - }); -} diff --git a/apps/server/src/terminalProcessInspector/win32.ts b/apps/server/src/terminalProcessInspector/win32.ts index a0f5b99110..84925ab86d 100644 --- a/apps/server/src/terminalProcessInspector/win32.ts +++ b/apps/server/src/terminalProcessInspector/win32.ts @@ -1,27 +1,56 @@ import { runProcess } from "../processRunner"; -import { parsePidList, parsePortList } from "./utils"; +import { parsePortList } from "./utils"; -export async function collectWindowsChildPids(terminalPid: number): Promise { +export async function collectWindowsProcessFamilyPids(terminalPid: number): Promise { const command = [ - `$children = Get-CimInstance Win32_Process -Filter "ParentProcessId = ${terminalPid}" -ErrorAction SilentlyContinue`, - "if (-not $children) { exit 0 }", - "$children | Select-Object -ExpandProperty ProcessId", + "$procs = Get-CimInstance Win32_Process -Property ProcessId,ParentProcessId -ErrorAction SilentlyContinue", + "if (-not $procs) { exit 0 }", + '$procs | ForEach-Object { "$($_.ProcessId) $($_.ParentProcessId)" }', ].join("; "); try { const result = await runProcess( "powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", command], { - timeoutMs: 1_500, + timeoutMs: 2_000, allowNonZeroExit: true, - maxBufferBytes: 32_768, + maxBufferBytes: 262_144, outputMode: "truncate", }, ); if (result.code !== 0) { return []; } - return parsePidList(result.stdout); + + const childrenByParentPid = new Map(); + for (const line of result.stdout.split(/\r?\n/g)) { + const [pidRaw, ppidRaw] = line.trim().split(/\s+/g); + const pid = Number(pidRaw); + const ppid = Number(ppidRaw); + if (!Number.isInteger(pid) || !Number.isInteger(ppid)) continue; + const children = childrenByParentPid.get(ppid); + if (children) { + children.push(pid); + } else { + childrenByParentPid.set(ppid, [pid]); + } + } + + const processFamily = new Set([terminalPid]); + const pendingParents = [terminalPid]; + while (pendingParents.length > 0) { + const parentPid = pendingParents.shift(); + if (!parentPid) continue; + const childPids = childrenByParentPid.get(parentPid); + if (!childPids || childPids.length === 0) continue; + for (const childPid of childPids) { + if (processFamily.has(childPid)) continue; + processFamily.add(childPid); + pendingParents.push(childPid); + } + } + + return [...processFamily]; } catch { return []; } diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 8f7ddb19d0..280d87bedb 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -61,7 +61,11 @@ import { APP_STAGE_LABEL, APP_VERSION } from "../branding"; import { isTerminalFocused } from "../lib/terminalFocus"; import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils"; import { useStore } from "../store"; -import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore"; +import { + normalizeRunningPorts, + selectThreadTerminalState, + useTerminalStateStore, +} from "../terminalStateStore"; import { useUiStateStore } from "../uiStateStore"; import { resolveShortcutCommand, @@ -204,13 +208,6 @@ function ThreadStatusLabel({ ); } -function normalizeRunningPorts(rawPorts: number[] | undefined): number[] { - if (!rawPorts) return []; - return [...new Set(rawPorts)] - .filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535) - .toSorted((left, right) => left - right); -} - function terminalStatusFromTerminalState( runningTerminalIds: string[], runningTerminalPorts: Record, diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index 71243fa1b3..98de82b0af 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -33,7 +33,11 @@ import { type ThreadTerminalGroup, } from "../types"; import { readNativeApi } from "~/nativeApi"; -import { selectTerminalEventEntries, useTerminalStateStore } from "../terminalStateStore"; +import { + normalizeRunningPorts, + selectTerminalEventEntries, + useTerminalStateStore, +} from "../terminalStateStore"; const MIN_DRAWER_HEIGHT = 180; const MAX_DRAWER_HEIGHT_RATIO = 0.75; @@ -81,13 +85,6 @@ interface TerminalRuntimeStatus { extraWebPortCount: number; } -function normalizeRunningPorts(rawPorts: number[] | undefined): number[] { - if (!rawPorts) return []; - return [...new Set(rawPorts)] - .filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535) - .toSorted((left, right) => left - right); -} - function terminalRuntimeStatus( terminalId: string, runningTerminalIds: Set, diff --git a/apps/web/src/terminalStateStore.ts b/apps/web/src/terminalStateStore.ts index 545160074f..703aa89331 100644 --- a/apps/web/src/terminalStateStore.ts +++ b/apps/web/src/terminalStateStore.ts @@ -62,7 +62,7 @@ function normalizeRunningTerminalIds( .filter((id) => id.length > 0 && validTerminalIdSet.has(id)); } -function normalizeRunningPorts(ports: readonly number[] | undefined): number[] { +export function normalizeRunningPorts(ports: readonly number[] | undefined): number[] { if (!ports || ports.length === 0) return []; return [...new Set(ports)] .filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535)