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): 子代理 spawn 前校验 argv 长度,超限按可读状态 fail-closed (#5968)

Yunjin Zhangc69e8b918265
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
index e2e352d873..b3eaba1273 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
@@ -265,6 +265,8 @@ async function makeFixture(options: {
* permissions or mocking fs.
*/
poisonResultPath?: boolean;
+ /** Override the per-task profile prompt, e.g. to blow the spawn argv budget. */
+ profilePrompt?: string;
} = {}) {
const root = await tempRoot();
const runId = randomUUID();
@@ -495,7 +497,7 @@ process.stdin.on('end', () => {
title: `scout ${index + 1}`,
task: `task ${index + 1}`,
tools: 'read,grep,find,ls',
- profilePrompt: 'fixture prompt',
+ profilePrompt: options.profilePrompt ?? 'fixture prompt',
provider: 'fixture',
model: 'fixture-model',
thinking: 'high',
@@ -598,6 +600,49 @@ describe('Cindy durable PI Subagent runner', () => {
});
});
+ // A profile prompt long enough to blow the platform spawn command line budget
+ // must fail closed as a readable task error. Without the guard the spawn
+ // itself throws (ENAMETOOLONG / EINVAL on Windows) and the run ends with an
+ // opaque error instead of saying which argument was too long.
+ it('fails a task closed when its profile prompt exceeds the spawn argv budget', async () => {
+ // Over the budget on both platforms: 30k on Windows, 128k on POSIX.
+ const fixture = await makeFixture({
+ profilePrompt: 'profile prompt. '.repeat(process.platform === 'win32' ? 3000 : 9000),
+ });
+ const failed = await waitFor(async () => {
+ const [run] = await listPiSubagentRuns(fixture.root);
+ return run?.state === 'failed' ? run : null;
+ });
+ expect(failed.tasks[0]?.status).toBe('failed');
+ expect(failed.tasks[0]?.error).toMatch(/spawn budget/i);
+ expect(await readCommandsIfPresent(fixture.commandsFile)).toBeNull();
+ });
+
+ // 两种口径:Windows CreateProcess 上限是 32767 个 UTF-16 字符(按字节算会把中文
+ // 放大三倍、误拒),POSIX 的 ARG_MAX 按字节算(按字符算会漏检中文长提示词)。
+ it('measures argv in characters on Windows and in UTF-8 bytes on POSIX', async () => {
+ // ~4.5 万字符 ≈ 13.5 万 UTF-8 字节:字符数超 Windows 的 30k、字节数超 POSIX
+ // 的 128k,两边都该被拒。
+ const oversized = await makeFixture({
+ profilePrompt: '中文提示词'.repeat(9000),
+ });
+ const failed = await waitFor(async () => {
+ const [run] = await listPiSubagentRuns(oversized.root);
+ return run?.state === 'failed' ? run : null;
+ });
+ expect(failed.tasks[0]?.error).toMatch(/spawn budget/i);
+ expect(await readCommandsIfPresent(oversized.commandsFile)).toBeNull();
+
+ // ~1.8 万字符 ≈ 5.4 万 UTF-8 字节:两边都没超,必须照常跑完 —— 这条在
+ // Windows 上按字节估时会被误拒。
+ const fits = await makeFixture({ profilePrompt: '中文'.repeat(9000) });
+ const completed = await waitFor(async () => {
+ const [run] = await listPiSubagentRuns(fits.root);
+ return run?.state === 'completed' ? run : null;
+ });
+ expect(completed.tasks[0]?.status).toBe('completed');
+ });
+
it('uses unique exact child session ids and persists terminal output/usage', async () => {
const fixture = await makeFixture({ tasks: 2 });
const completed = await waitFor(async () => {
diff --git a/packages/maker-core/src/agents/pi/cindy-subagent-runner-source.ts b/packages/maker-core/src/agents/pi/cindy-subagent-runner-source.ts
index fcee9accb6..c737457530 100644
--- a/packages/maker-core/src/agents/pi/cindy-subagent-runner-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-subagent-runner-source.ts
@@ -37,6 +37,12 @@ const runnerScriptPath = typeof process.argv[1] === 'string' ? process.argv[1] :
const CONTROL_VERSION = 1;
const MAX_TRANSCRIPT_BYTES = 50 * 1024 * 1024;
const MAX_RESULT_BYTES = 256 * 1024;
+// Spawn command line budget, same values the Host enforces for its own Pi
+// launch (PI_WINDOWS_SPAWN_ARGV_BUDGET / PI_POSIX_SPAWN_ARGV_BUDGET). The
+// child's profile prompt is a hardcoded short string today; this only exists so
+// a future custom profile fails as a readable status instead of an opaque
+// spawn error.
+const SPAWN_ARGV_BUDGET = process.platform === 'win32' ? 30000 : 128000;
const CONTROL_POLL_MS = 200;
const STATUS_FLUSH_MS = 50;
const HEARTBEAT_MS = 2000;
@@ -824,6 +830,24 @@ function main() {
const childArgs = Array.isArray(config.binaryPrefixArgs)
? config.binaryPrefixArgs.concat(args)
: args;
+ // 两种口径不一样,不能统一:Windows CreateProcess 的命令行上限是 32767 个
+ // UTF-16 字符(按 UTF-8 字节算会把中文路径放大三倍、误拒);POSIX 的
+ // ARG_MAX 按字节算(用字符数会漏检中文长提示词)。
+ const argvUnits = childArgs.reduce((total, arg) => {
+ const value = String(arg);
+ return total
+ + (process.platform === 'win32' ? value.length : Buffer.byteLength(value, 'utf8'))
+ + 3;
+ }, 0);
+ if (argvUnits > SPAWN_ARGV_BUDGET) {
+ task.status = 'failed';
+ task.error = 'Subagent command line exceeds the platform spawn budget ('
+ + argvUnits + ' > ' + SPAWN_ARGV_BUDGET + ').';
+ task.endedAt = Date.now();
+ scheduleStatus();
+ resolve();
+ return;
+ }
const child = spawn(config.binary, childArgs, {
cwd: task.cwd,
env: childEnv,