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): 修正 Windows 分组路径测试的子进程预算 (#4206)

Dash50025e3c3a07
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 ef52a54c13..36290d3919 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
@@ -11,6 +11,7 @@ import {
buildWindowsRegistryProbeScript,
buildWindowsDescendantCleanupScript,
countWindowsPowerShellDiagnostics,
+ maxWindowsPathKindProbeBatchCount,
terminateWindowsPowerShellDescendants,
warnWindowsGitPathProbeDiagnostics,
warnWindowsGitPathProbeFailure,
@@ -68,7 +69,9 @@ describe('Windows Git PATH PowerShell probes', () => {
expect(script).toContain('Write-ProbeOutput $operation.Process');
expect(script).toContain('$budgetMs = 2750');
expect(script).toContain('if ($nextGroupIndex -lt $groups.Count -or $operations.Count -gt 0) {');
- expect(script).toContain('WriteLine("__CINDY_WINDOWS_GIT_PATH_DIAGNOSTIC__`tpath-process")');
+ for (const phase of ['output', 'start', 'exit', 'complete', 'timeout', 'budget', 'coordinator', 'cleanup']) {
+ expect(script).toContain(`WriteLine("__CINDY_WINDOWS_GIT_PATH_DIAGNOSTIC__\`tpath-process-${phase}")`);
+ }
expect(script).not.toContain('foreach ($candidate in $paths)');
expect(script).not.toContain('[RunspaceFactory]');
expect(script.indexOf('$clock = [Diagnostics.Stopwatch]::StartNew()'))
@@ -108,9 +111,19 @@ describe('Windows Git PATH PowerShell probes', () => {
expect(() => buildWindowsDescendantCleanupScript(0)).toThrow(RangeError);
});
- it.runIf(process.platform === 'win32')(
- 'executes grouped path probes in Windows PowerShell',
- ({ skip }) => {
+ it('bounds an explicit child budget and queue admission by the coordinator budget', () => {
+ expect(maxWindowsPathKindProbeBatchCount(3_000)).toBe(8);
+ expect(maxWindowsPathKindProbeBatchCount(10_000, 10_000)).toBe(4);
+ const script = buildWindowsPathKindProbeScript(25, 10_000, 25, { operationTimeoutMs: 10_000 });
+ expect(script).toContain('$operationTimeoutMs = 9500');
+ expect(script).toContain('$budgetMs = 9750');
+ expect(script).toContain('$groups = @($allGroups | Select-Object -First 4)');
+ expect(script).toContain('$maxConcurrency = 4');
+ });
+
+ it.runIf(process.platform === 'win32').each([0, 2_000])(
+ 'executes grouped path probes in Windows PowerShell with %i ms child delay',
+ (delayMs) => {
const systemRoot = process.env.SystemRoot ?? process.env.WINDIR;
if (!systemRoot) throw new Error('Windows system root is unavailable');
const tempRoot = mkdtempSync(path.join(tmpdir(), 'cindy-git-path-'));
@@ -125,13 +138,18 @@ describe('Windows Git PATH PowerShell probes', () => {
{ paths: [path.join(staleRoot, 'cmd'), path.join(staleRoot, 'cmd', 'git.exe')] },
{ paths: [validCmd, validGit] },
];
- let output: string;
- try {
- output = execFileSync(
+ const runProbe = (script: string, childDelayMs = delayMs) => {
+ // Inject latency in the real nested command, without adding a production
+ // delay hook. This deterministically exercises the old 1250ms cutoff.
+ const encoded = script.match(/\$probeCommand = '([^']+)'/)?.[1];
+ if (!encoded) throw new Error('Missing nested probe command');
+ const delayedCommand = Buffer.from(
+ `Start-Sleep -Milliseconds ${childDelayMs}\n${Buffer.from(encoded, 'base64').toString('utf16le')}`,
+ 'utf16le',
+ ).toString('base64');
+ return execFileSync(
path.join(systemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
- // Hosted Windows runners can spend several seconds starting the
- // nested PowerShell probes before any candidate is inspected.
- ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', buildWindowsPathKindProbeScript(4, 10_000, 2)],
+ ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', script.replace(encoded, delayedCommand)],
{
encoding: 'utf8',
input: Buffer.from(JSON.stringify(groups), 'utf8'),
@@ -140,21 +158,28 @@ describe('Windows Git PATH PowerShell probes', () => {
windowsHide: true,
},
);
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code === 'ETIMEDOUT') {
- skip();
- return;
- }
- throw error;
+ };
+ if (delayMs > 0) {
+ // Outlive even the outer deadline, so scheduler pauses cannot make the
+ // negative case complete before the coordinator observes expiry.
+ const expired = runProbe(buildWindowsPathKindProbeScript(4, 10_000, 2), 30_000);
+ expect(expired).toMatch(/__CINDY_WINDOWS_GIT_PATH_DIAGNOSTIC__\tpath-process-(timeout|budget)/);
+ expect(expired).not.toContain(`F\t${Buffer.from(validGit, 'utf16le').toString('base64')}`);
}
+ // Correctness needs startup headroom at BOTH timeout layers. Production
+ // callers retain their default 1250ms child budget and 3000ms outer budget.
+ const output = runProbe(buildWindowsPathKindProbeScript(4, 10_000, 2, { operationTimeoutMs: 10_000 }));
+
expect(output).toContain(`D\t${Buffer.from(validCmd, 'utf16le').toString('base64')}`);
expect(output).toContain(`F\t${Buffer.from(validGit, 'utf16le').toString('base64')}`);
+ expect(output).not.toContain(Buffer.from(path.join(staleRoot, 'cmd'), 'utf16le').toString('base64'));
+ expect(countWindowsPowerShellDiagnostics(output)).toBe(0);
} finally {
rmSync(tempRoot, { recursive: true, force: true });
}
},
- 30_000,
+ 40_000,
);
it.runIf(process.platform === 'win32')(
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 1daaa4b861..79b7e7b853 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
@@ -104,10 +104,10 @@ function windowsPathKindProbeLines(outputLine: string): string[] {
];
}
-export function maxWindowsPathKindProbeBatchCount(timeoutMs: number): number {
+export function maxWindowsPathKindProbeBatchCount(timeoutMs: number, requestedOperationTimeoutMs = 1_250): number {
const budgetMs = Math.max(timeoutMs - 250, 1);
const availableOperationBudgetMs = Math.max(budgetMs - 250, 1);
- const operationTimeoutMs = Math.min(availableOperationBudgetMs, 1_250);
+ const operationTimeoutMs = Math.min(availableOperationBudgetMs, Math.max(requestedOperationTimeoutMs, 1));
const maxOperationWaves = Math.max(Math.floor(availableOperationBudgetMs / operationTimeoutMs), 1);
return 4 * maxOperationWaves;
}
@@ -116,6 +116,7 @@ export function buildWindowsPathKindProbeScript(
candidateCount: number,
timeoutMs: number,
batchCount = candidateCount,
+ options: { operationTimeoutMs?: number } = {},
): string {
const inputPrelude = [
'$stdin = [Console]::OpenStandardInput()',
@@ -134,8 +135,8 @@ export function buildWindowsPathKindProbeScript(
const operationCount = Math.min(Math.max(batchCount, 1), Math.max(candidateCount, 1));
const maxConcurrency = Math.min(operationCount, 4);
const availableOperationBudgetMs = Math.max(budgetMs - 250, 1);
- const operationTimeoutMs = Math.min(availableOperationBudgetMs, 1_250);
- const maxOperationCount = Math.min(operationCount, maxWindowsPathKindProbeBatchCount(timeoutMs));
+ const operationTimeoutMs = Math.min(availableOperationBudgetMs, Math.max(options.operationTimeoutMs ?? 1_250, 1));
+ const maxOperationCount = Math.min(operationCount, maxWindowsPathKindProbeBatchCount(timeoutMs, operationTimeoutMs));
const encodedProbeCommand = Buffer.from([
'$paths = @(([string]$env:CINDY_WINDOWS_GIT_PATH_CANDIDATES | ConvertFrom-Json))',
'foreach ($pathValue in $paths) {',
@@ -165,7 +166,7 @@ export function buildWindowsPathKindProbeScript(
' }',
' }',
' } catch {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-output")`,
' }',
'}',
'try {',
@@ -187,7 +188,7 @@ export function buildWindowsPathKindProbeScript(
' [void]$process.Start()',
' [void]$operations.Add([PSCustomObject]@{ Process = $process; StartedAt = $clock.ElapsedMilliseconds })',
' } catch {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-start")`,
' if ($null -ne $process) {',
' $process.Dispose()',
' }',
@@ -204,22 +205,22 @@ export function buildWindowsPathKindProbeScript(
' try {',
' Write-ProbeOutput $operation.Process',
' if ($operation.Process.ExitCode -ne 0) {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-exit")`,
' }',
' } catch {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-complete")`,
' } finally {',
' $operation.Process.Dispose()',
' [void]$operations.Remove($operation)',
' }',
' }',
' foreach ($operation in $expired) {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-timeout")`,
' try {',
' $operation.Process.Kill()',
' $operation.Process.WaitForExit()',
' } catch {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-cleanup")`,
' } finally {',
' if ($operation.Process.HasExited) {',
' Write-ProbeOutput $operation.Process',
@@ -230,10 +231,10 @@ export function buildWindowsPathKindProbeScript(
' }',
' }',
' if ($nextGroupIndex -lt $groups.Count -or $operations.Count -gt 0) {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-budget")`,
' }',
'} catch {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-coordinator")`,
'} finally {',
' foreach ($operation in @($operations)) {',
' try {',
@@ -242,7 +243,7 @@ export function buildWindowsPathKindProbeScript(
' $operation.Process.WaitForExit()',
' }',
' } catch {',
- ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process")`,
+ ` [Console]::Out.WriteLine("${DIAGNOSTIC_PREFIX}\`tpath-process-cleanup")`,
' } finally {',
' if ($operation.Process.HasExited) {',
' Write-ProbeOutput $operation.Process',