From e5ca420b1d954fa7647f0fc59129f50918598b8d Mon Sep 17 00:00:00 2001 From: SK Akram Date: Tue, 23 Dec 2025 06:41:21 +0000 Subject: [PATCH] fix: add background PID tracking support for Windows --- packages/core/src/tools/shell.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/core/src/tools/shell.ts b/packages/core/src/tools/shell.ts index 5ee0b44..c72af62 100644 --- a/packages/core/src/tools/shell.ts +++ b/packages/core/src/tools/shell.ts @@ -35,9 +35,30 @@ import { isCommandNeedsPermission, stripShellWrapper, } from '../utils/shell-utils.js'; +import { execSync } from 'node:child_process'; export const OUTPUT_UPDATE_INTERVAL_MS = 1000; +function getWindowsChildPids(parentPid: number): number[] { + try { + const output = execSync( + `powershell -NoProfile -Command "Get-CimInstance Win32_Process | Where-Object {$_.ParentProcessId -eq ${parentPid}} | Select-Object -ExpandProperty ProcessId"`, + { encoding: 'utf8', timeout: 5000, windowsHide: true }, + ); + const pids: number[] = []; + for (const line of output.split(/\r?\n/)) { + const trimmed = line.trim(); + if (/^\d+$/.test(trimmed)) { + pids.push(Number(trimmed)); + } + } + return pids; + } catch (error) { + console.warn('Failed to get Windows child PIDs:', error); + return []; + } +} + export interface ShellToolParams { command: string; is_background: boolean; @@ -232,7 +253,12 @@ class ShellToolInvocation extends BaseToolInvocation< const result = await resultPromise; const backgroundPIDs: number[] = []; - if (os.platform() !== 'win32') { + if (isWindows) { + if (result.pid) { + const childPids = getWindowsChildPids(result.pid); + backgroundPIDs.push(...childPids); + } + } else { if (fs.existsSync(tempFilePath)) { const pgrepLines = fs .readFileSync(tempFilePath, 'utf8')