Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(pi): reap Windows path probes with an owned job

xuzini-xzn96d7ba19b58a
diff --git a/packages/maker-core/src/agents/pi/windows-git-path-powershell.test.ts b/packages/maker-core/src/agents/pi/windows-git-path-powershell.test.ts
index 03824e2510..4328645202 100644
--- a/packages/maker-core/src/agents/pi/windows-git-path-powershell.test.ts
+++ b/packages/maker-core/src/agents/pi/windows-git-path-powershell.test.ts
@@ -9,10 +9,8 @@ import {
buildWindowsNetworkDriveProbeScript,
buildWindowsPathKindProbeScript,
buildWindowsRegistryProbeScript,
- buildWindowsDescendantCleanupScript,
countWindowsPowerShellDiagnostics,
maxWindowsPathKindProbeBatchCount,
- terminateWindowsPowerShellDescendants,
warnWindowsGitPathProbeDiagnostics,
warnWindowsGitPathProbeFailure,
} from './windows-git-path-powershell.js';
@@ -102,18 +100,16 @@ describe('Windows Git PATH PowerShell probes', () => {
expect(script).toContain('$nextGroupIndex += 1');
});
- it('builds a recursive descendant cleanup script without candidate path data', () => {
- const script = buildWindowsDescendantCleanupScript(4321);
+ it('attaches the coordinator to its owned job before starting any nested probe', () => {
+ const script = buildWindowsPathKindProbeScript(2, 3_000);
- expect(script).toContain('[void]$pending.Add(4321)');
- expect(script).toContain('Get-CimInstance Win32_Process');
- expect(script).toContain('ParentProcessId = ');
- expect(script).toContain('-ErrorAction SilentlyContinue');
- expect(script).toContain('$descendants.Count - 1');
- expect(script).toContain('Stop-Process -Id $childProcessId -Force');
- expect(script).toContain('taskkill.exe /PID $childProcessId /F /T');
- expect(script).not.toContain('C:\\');
- expect(() => buildWindowsDescendantCleanupScript(0)).toThrow(RangeError);
+ expect(script).toContain('limits.Basic.Flags = 0x2000; // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE');
+ expect(script).toContain('AssignProcessToJobObject(job, GetCurrentProcess())');
+ expect(script.indexOf('[CindyWindowsPathProbeJob]::Attach()')).toBeLessThan(
+ script.indexOf('[void]$process.Start()'),
+ );
+ expect(script).toContain('__CINDY_WINDOWS_GIT_PATH_DIAGNOSTIC__`tpath-process-job');
+ expect(script).not.toContain('Get-CimInstance');
});
it('bounds an explicit child budget and queue admission by the coordinator budget', () => {
@@ -202,130 +198,79 @@ describe('Windows Git PATH PowerShell probes', () => {
PATH_PROBE_TEST_PROCESS_TIMEOUT_MS * 2 + 10_000,
);
- it.runIf(process.platform === 'win32')(
- 'kills probe descendants after the coordinator is terminated',
- async () => {
+ // Regression: #6117 CI left a real child alive when the separate CIM cleanup
+ // timed out. Exercise the production coordinator and keep the OS liveness
+ // assertion; source-contract tests above cannot validate a Windows Job Object.
+ // Tier: existing native Windows unit probe; macOS/Linux explicitly skip it.
+ it.runIf(process.platform === 'win32').each(['terminated', 'exited'] as const)(
+ 'reaps live probe descendants when the coordinator is %s',
+ async (exitMode) => {
const systemRoot = process.env.SystemRoot ?? process.env.WINDIR;
if (!systemRoot) throw new Error('Windows system root is unavailable');
- const powershell = path.join(
- systemRoot,
- 'System32',
- 'WindowsPowerShell',
- 'v1.0',
- 'powershell.exe',
- );
- const encodedSleepCommand = Buffer.from('Start-Sleep -Seconds 30', 'utf16le').toString(
- 'base64',
- );
- const script = [
- '$startInfo = New-Object System.Diagnostics.ProcessStartInfo',
- `$startInfo.FileName = '${powershell.replaceAll("'", "''")}'`,
- `$startInfo.Arguments = '-NoLogo -NoProfile -NonInteractive -EncodedCommand ${encodedSleepCommand}'`,
- '$startInfo.UseShellExecute = $false',
- '$startInfo.CreateNoWindow = $true',
- '$process = New-Object System.Diagnostics.Process',
- '$process.StartInfo = $startInfo',
+ const powershell = path.join(systemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
+ const productionScript = buildWindowsPathKindProbeScript(2, 30_000, 1, { operationTimeoutMs: 30_000 });
+ const encoded = productionScript.match(/\$probeCommand = '([^']+)'/)?.[1];
+ if (!encoded) throw new Error('Missing nested probe command');
+ const sleepingCommand = Buffer.from('Start-Sleep -Seconds 120', 'utf16le').toString('base64');
+ // Instrument the existing spawn boundary, never a production delay hook.
+ // Publish the real child PID before terminating/exiting the coordinator.
+ const script = productionScript.replace(encoded, sleepingCommand).replace(
'[void]$process.Start()',
- '[Console]::Out.WriteLine($process.Id)',
- '[Console]::Out.Flush()',
- 'Start-Sleep -Seconds 30',
- ].join('\n');
+ [
+ '[void]$process.Start()',
+ '[Console]::Out.WriteLine("child=" + $process.Id)',
+ '[Console]::Out.Flush()',
+ ...(exitMode === 'exited' ? ['exit 0'] : []),
+ ].join('\n'),
+ );
let childPid: number | undefined;
- let coordinatorPid: number | undefined;
+ let childGone = false;
let coordinator: ReturnType<typeof spawn> | undefined;
try {
- coordinator = spawn(
- powershell,
- ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', script],
- {
- stdio: ['ignore', 'pipe', 'pipe'],
- windowsHide: true,
- },
- );
- coordinatorPid = coordinator.pid;
- expect(Number.isInteger(coordinatorPid) && (coordinatorPid ?? 0) > 0).toBe(true);
-
- let output = '';
- coordinator.stdout?.setEncoding('utf8');
- coordinator.stdout?.on('data', (chunk: string) => {
- output += chunk;
+ coordinator = spawn(powershell, ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', script], {
+ stdio: ['pipe', 'pipe', 'pipe'],
+ windowsHide: true,
});
- await vi.waitFor(
- () => {
- childPid = Number(output.trim().split(/\r?\n/).at(-1));
- expect(Number.isInteger(childPid) && childPid > 0).toBe(true);
- },
- { timeout: 5_000, interval: 50 },
- );
-
const coordinatorExit = new Promise<void>((resolve) => {
coordinator?.once('exit', () => resolve());
});
- expect(coordinator.kill()).toBe(true);
+ coordinator.stdin?.end(JSON.stringify([{ paths: ['unused-one', 'unused-two'] }]));
+ let output = '';
+ let errors = '';
+ coordinator.stdout?.setEncoding('utf8');
+ coordinator.stdout?.on('data', (chunk: string) => { output += chunk; });
+ coordinator.stderr?.setEncoding('utf8');
+ coordinator.stderr?.on('data', (chunk: string) => { errors += chunk; });
+ await vi.waitFor(() => {
+ childPid = Number(output.match(/child=(\d+)/)?.[1]);
+ expect(Number.isInteger(childPid) && childPid > 0, JSON.stringify({ output, errors })).toBe(true);
+ }, { timeout: PATH_PROBE_TEST_PROCESS_TIMEOUT_MS, interval: 50 });
+ if (exitMode === 'terminated') expect(coordinator.kill()).toBe(true);
await coordinatorExit;
- const cleanupWarnings: unknown[] = [];
- const cleanupStarted = performance.now();
- terminateWindowsPowerShellDescendants(powershell, coordinatorPid, {
- warn: (...args: unknown[]) => {
- cleanupWarnings.push(args);
- },
- });
- const cleanupMs = Math.round(performance.now() - cleanupStarted);
-
- // EncodedCommand avoids Windows argv splitting of a multi-line -Command.
- // Poll until the orphaned child is gone; a live child after cleanup is a real leak.
- const livenessCommand = Buffer.from(
- [
- `$child = Get-Process -Id ${childPid} -ErrorAction SilentlyContinue`,
- 'if ($child) {',
- ' $child | Select-Object Id, ProcessName, StartTime, HasExited | ConvertTo-Json -Compress',
- ' exit 1',
- '}',
- 'exit 0',
- ].join('\n'),
- 'utf16le',
- ).toString('base64');
- await vi.waitFor(
- () => {
- const liveness = spawnSync(
- powershell,
- ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', livenessCommand],
- { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 10_000, windowsHide: true },
- );
- // Keep the real exit assertion, but report whether cleanup timed out,
- // failed, or left a process alive instead of only "expected 1 to be 0".
- expect(liveness.status, JSON.stringify({
- coordinatorPid,
- childPid,
- cleanupMs,
- cleanupWarnings,
- livenessError: liveness.error?.message,
- livenessSignal: liveness.signal,
- livenessStdout: liveness.stdout,
- livenessStderr: liveness.stderr,
- })).toBe(0);
- },
- { timeout: 8_000, interval: 50 },
- );
+ // Probe the actual OS process directly, without another cold PowerShell
+ // startup. Only ESRCH proves absence; permission or other errors fail.
+ await vi.waitFor(() => {
+ let exists = true;
+ try {
+ process.kill(childPid!, 0);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== 'ESRCH') throw error;
+ exists = false;
+ }
+ expect(exists, JSON.stringify({ coordinatorPid: coordinator?.pid, childPid, output, errors })).toBe(false);
+ }, { timeout: 8_000, interval: 50 });
+ childGone = true;
} finally {
- if (coordinatorPid) {
- spawnSync('taskkill.exe', ['/PID', String(coordinatorPid), '/F'], {
- stdio: 'ignore',
- windowsHide: true,
- });
- }
- if (childPid) {
- spawnSync('taskkill.exe', ['/PID', String(childPid), '/F'], {
- stdio: 'ignore',
- windowsHide: true,
- });
+ // Only this fixture's live coordinator is owned. A completed PID can
+ // already be reused; do not send taskkill to it after a successful exit.
+ if (coordinator && coordinator.exitCode === null && coordinator.signalCode === null) coordinator.kill();
+ if (childPid && !childGone) {
+ spawnSync('taskkill.exe', ['/PID', String(childPid), '/F'], { stdio: 'ignore', windowsHide: true, timeout: 5_000 });
}
}
},
- // waitFor(5s) + descendant cleanup exec(10s) + liveness probe exec(15s) already
- // sum to 30s; leave headroom for coordinator exit, taskkill, and scheduling.
- 45_000,
+ PATH_PROBE_TEST_PROCESS_TIMEOUT_MS + 15_000,
);
it('reports recoverable script failures only when a logger is supplied', () => {
diff --git a/packages/maker-core/src/agents/pi/windows-git-path-powershell.ts b/packages/maker-core/src/agents/pi/windows-git-path-powershell.ts
index 74b0a89409..29638b74ea 100644
--- a/packages/maker-core/src/agents/pi/windows-git-path-powershell.ts
+++ b/packages/maker-core/src/agents/pi/windows-git-path-powershell.ts
@@ -1,17 +1,9 @@
-import { execFileSync } from 'node:child_process';
-
import type { Logger } from '../../interfaces/logger.js';
export type WindowsGitPathLogger = Pick<Logger, 'warn'>;
const DIAGNOSTIC_PREFIX = '__CINDY_WINDOWS_GIT_PATH_DIAGNOSTIC__';
-// 忙碌的 Windows 宿主(如 CI hosted runner)上 PowerShell 冷启动就可能超过
-// 5 秒:2 秒预算会让清理进程在 Stop-Process 执行前就被 execFileSync 超时
-// 杀掉,后代进程泄漏(#3574 的偶发失败正是这个竞态)。清理只在探测已经
-// 失败的收尾路径运行,放宽预算不影响任何成功路径的耗时。
-const WINDOWS_DESCENDANT_CLEANUP_TIMEOUT_MS = 10_000;
-
-type WindowsPowerShellProbe = 'registry' | 'network-drives' | 'path-kinds' | 'path-cleanup';
+type WindowsPowerShellProbe = 'registry' | 'network-drives' | 'path-kinds';
export function buildWindowsRegistryProbeScript(registryPaths: readonly string[]): string {
const quotedPaths = registryPaths.map((registryPath) => `'${registryPath.replaceAll("'", "''")}'`).join(', ');
@@ -44,56 +36,63 @@ export function buildWindowsNetworkDriveProbeScript(): string {
].join('\n');
}
-export function buildWindowsDescendantCleanupScript(rootProcessId: number): string {
- if (!Number.isInteger(rootProcessId) || rootProcessId <= 0) {
- throw new RangeError('rootProcessId must be a positive integer');
+// Put the coordinator itself in a private, non-inheritable kill-on-close job
+// BEFORE it creates a child. Children inherit membership, not the job handle:
+// even TerminateProcess / execFileSync's outer timeout closes the last handle
+// and Windows reaps the whole probe tree. No second PowerShell/CIM cleanup
+// process (which can itself time out before killing anything) is required.
+// Nested jobs are supported by the Windows versions supported by Electron.
+const WINDOWS_PROBE_JOB_SOURCE = `
+using System;
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+public static class CindyWindowsPathProbeJob {
+ [StructLayout(LayoutKind.Sequential)]
+ struct BasicLimits {
+ public long ProcessTime, JobTime;
+ public uint Flags;
+ public UIntPtr MinWorkingSet, MaxWorkingSet;
+ public uint ActiveProcesses;
+ public UIntPtr Affinity;
+ public uint Priority, Scheduling;
}
- return [
- '$pending = New-Object System.Collections.ArrayList',
- '$descendants = New-Object System.Collections.ArrayList',
- `[void]$pending.Add(${rootProcessId})`,
- 'while ($pending.Count -gt 0) {',
- ' $parentProcessId = [int]$pending[0]',
- ' $pending.RemoveAt(0)',
- ' $children = @(Get-CimInstance Win32_Process -Filter ("ParentProcessId = " + $parentProcessId) -ErrorAction SilentlyContinue)',
- ' foreach ($child in $children) {',
- ' $childProcessId = [int]$child.ProcessId',
- ' [void]$descendants.Add($childProcessId)',
- ' [void]$pending.Add($childProcessId)',
- ' }',
- '}',
- 'for ($index = $descendants.Count - 1; $index -ge 0; $index -= 1) {',
- ' $childProcessId = [int]$descendants[$index]',
- ' Stop-Process -Id $childProcessId -Force -ErrorAction SilentlyContinue',
- " & taskkill.exe /PID $childProcessId /F /T 2>$null | Out-Null",
- '}',
- ].join('\n');
-}
-
-export function terminateWindowsPowerShellDescendants(
- powershell: string,
- rootProcessId: number | undefined,
- logger?: WindowsGitPathLogger,
-): void {
- if (typeof rootProcessId !== 'number' || !Number.isInteger(rootProcessId) || rootProcessId <= 0) return;
- try {
- // EncodedCommand keeps the cleanup script as one argv on Windows. A
- // multiline -Command can be split or fail to parse, which leaves probe
- // descendants running after the coordinator has already exited.
- const encoded = Buffer.from(buildWindowsDescendantCleanupScript(rootProcessId), 'utf16le').toString('base64');
- execFileSync(
- powershell,
- ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', encoded],
- {
- stdio: 'ignore',
- timeout: WINDOWS_DESCENDANT_CLEANUP_TIMEOUT_MS,
- windowsHide: true,
- },
- );
- } catch (error) {
- warnWindowsGitPathProbeFailure(logger, 'path-cleanup', error);
+ [StructLayout(LayoutKind.Sequential)]
+ struct IoCounters {
+ public ulong ReadOps, WriteOps, OtherOps, ReadBytes, WriteBytes, OtherBytes;
}
-}
+ [StructLayout(LayoutKind.Sequential)]
+ struct ExtendedLimits {
+ public BasicLimits Basic;
+ public IoCounters Io;
+ public UIntPtr ProcessMemory, JobMemory, PeakProcessMemory, PeakJobMemory;
+ }
+ [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
+ static extern IntPtr CreateJobObject(IntPtr attributes, string name);
+ [DllImport("kernel32.dll", SetLastError = true)]
+ static extern bool SetInformationJobObject(IntPtr job, int infoClass, ref ExtendedLimits limits, uint length);
+ [DllImport("kernel32.dll", SetLastError = true)]
+ static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
+ [DllImport("kernel32.dll")]
+ static extern IntPtr GetCurrentProcess();
+ [DllImport("kernel32.dll")]
+ static extern bool CloseHandle(IntPtr handle);
+ // Intentionally kept open until OS process teardown, never inherited by a child.
+ static IntPtr ownedJob;
+ public static void Attach() {
+ if (ownedJob != IntPtr.Zero) return;
+ IntPtr job = CreateJobObject(IntPtr.Zero, null);
+ if (job == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
+ ExtendedLimits limits = new ExtendedLimits();
+ limits.Basic.Flags = 0x2000; // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
+ if (!SetInformationJobObject(job, 9, ref limits, (uint)Marshal.SizeOf(typeof(ExtendedLimits)))
+ || !AssignProcessToJobObject(job, GetCurrentProcess())) {
+ int error = Marshal.GetLastWin32Error();
+ CloseHandle(job);
+ throw new Win32Exception(error);
+ }
+ ownedJob = job;
+ }
+}`;
function windowsPathKindProbeLines(outputLine: string): string[] {
return [
@@ -152,6 +151,15 @@ export function buildWindowsPathKindProbeScript(
].join('\n'), 'utf16le').toString('base64');
return [
...inputPrelude,
+ 'try {',
+ " Add-Type -TypeDefinition @'",
+ WINDOWS_PROBE_JOB_SOURCE,
+ "'@ -ErrorAction Stop",
+ ' [CindyWindowsPathProbeJob]::Attach()',
+ '} catch {',
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-job")`,
+ ' exit 1',
+ '}',
'$allGroups = @($json | ConvertFrom-Json)',
`$groups = @($allGroups | Select-Object -First ${maxOperationCount})`,
'if ($allGroups.Count -gt $groups.Count) {',
diff --git a/packages/maker-core/src/agents/pi/windows-git-path.ts b/packages/maker-core/src/agents/pi/windows-git-path.ts
index b0432a20a2..2f2700a55c 100644
--- a/packages/maker-core/src/agents/pi/windows-git-path.ts
+++ b/packages/maker-core/src/agents/pi/windows-git-path.ts
@@ -6,7 +6,6 @@ import {
buildWindowsPathKindProbeScript,
buildWindowsRegistryProbeScript,
maxWindowsPathKindProbeBatchCount,
- terminateWindowsPowerShellDescendants,
type WindowsGitPathLogger,
warnWindowsGitPathProbeDiagnostics,
warnWindowsGitPathProbeFailure,
@@ -267,11 +266,8 @@ function defaultProbeWindowsPathKindBatches(
warnWindowsGitPathProbeDiagnostics(logger, 'path-kinds', output);
return decodeWindowsPathKindLines(output);
} catch (error) {
- terminateWindowsPowerShellDescendants(
- powershell,
- (error as { pid?: number } | undefined)?.pid,
- logger,
- );
+ // The coordinator's private kill-on-close job owns any nested probes,
+ // including when execFileSync terminates the coordinator on timeout.
warnWindowsGitPathProbeFailure(logger, 'path-kinds', error);
return decodeWindowsPathKindsFromProbeError(error);
}