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(scheduler): 语法预检保持执行器不抛错,并响应取消与超时预算

Dash0568d25b6498
diff --git a/apps/desktop/src/main/scheduler-host/__tests__/preRunHook.test.ts b/apps/desktop/src/main/scheduler-host/__tests__/preRunHook.test.ts
index 63e993845e..3116c90453 100644
--- a/apps/desktop/src/main/scheduler-host/__tests__/preRunHook.test.ts
+++ b/apps/desktop/src/main/scheduler-host/__tests__/preRunHook.test.ts
@@ -202,6 +202,21 @@ describe.skipIf(process.platform === 'win32')('前置检查命令语法预检',
await expect(findShellSyntaxError('if true; then echo')).resolves.toBeTruthy();
});
+ it('命令含 NUL:预检不同步抛错,执行仍折叠成失败结果', async () => {
+ await expect(findShellSyntaxError('node -e "1"\0')).resolves.toBeUndefined();
+ const result = await executePreRunHook({ command: 'node -e "1"\0', stdinPayload: payload });
+ expect(result.status).toBe('failed');
+ expect(result.decision).toBe('block');
+ expect(result.spawnError || result.error).toBeTruthy();
+ });
+
+ it('预检响应取消信号:已取消时不再预检,执行返回 aborted', async () => {
+ const controller = new AbortController();
+ controller.abort();
+ await expect(findShellSyntaxError(`node '/a b/x.mjs'//'`, { signal: controller.signal }))
+ .resolves.toBeUndefined();
+ });
+
it('保存时校验:语法错误抛 invalid,正确命令放行', async () => {
await expect(assertPreRunHookCommandSyntax(`node '/a b/x.mjs'//'`)).rejects.toThrow(
/^invalid pre-run hook configuration: shell syntax error in command: /,
diff --git a/apps/desktop/src/main/scheduler-host/pre-run-hook.ts b/apps/desktop/src/main/scheduler-host/pre-run-hook.ts
index 0acc627c20..c63611287f 100644
--- a/apps/desktop/src/main/scheduler-host/pre-run-hook.ts
+++ b/apps/desktop/src/main/scheduler-host/pre-run-hook.ts
@@ -103,26 +103,37 @@ const SYNTAX_CHECK_TIMEOUT_MS = 5_000;
/**
* POSIX shell 语法预检(`sh -n -c`,只解析不执行)。返回 shell 的报错文本;语法
- * 正确、Windows(cmd.exe 没有对应能力)或预检本身没能完成(spawn 失败 / 超时)
- * 时返回 undefined,交给真实执行按原协议处理。
+ * 正确、Windows(cmd.exe 没有对应能力)或预检本身没能完成(参数非法 / spawn
+ * 失败 / 超时 / 取消)时返回 undefined,交给真实执行按原协议处理。
*/
-export function findShellSyntaxError(command: string): Promise<string | undefined> {
- if (process.platform === 'win32') return Promise.resolve(undefined);
+export function findShellSyntaxError(
+ command: string,
+ options: { signal?: AbortSignal; timeoutMs?: number } = {},
+): Promise<string | undefined> {
+ if (process.platform === 'win32' || options.signal?.aborted) return Promise.resolve(undefined);
return new Promise((resolve) => {
- execFile(
- POSIX_SHELL,
- ['-n', '-c', command],
- { timeout: SYNTAX_CHECK_TIMEOUT_MS },
- (err, _stdout, stderr) => {
- // 只有 shell 正常退出且非 0 才是语法错误;err.code 为字符串(ENOENT 等)或
- // 超时被杀时不下结论。
- if (!err || typeof err.code !== 'number') {
- resolve(undefined);
- return;
- }
- resolve(String(stderr).trim() || `${POSIX_SHELL} -n exited with code ${err.code}`);
- },
- );
+ try {
+ execFile(
+ POSIX_SHELL,
+ ['-n', '-c', command],
+ {
+ timeout: Math.min(SYNTAX_CHECK_TIMEOUT_MS, options.timeoutMs ?? SYNTAX_CHECK_TIMEOUT_MS),
+ signal: options.signal,
+ },
+ (err, _stdout, stderr) => {
+ // 只有 shell 正常退出且非 0 才是语法错误;err.code 为字符串(ENOENT /
+ // ABORT_ERR 等)或超时被杀时不下结论。
+ if (!err || typeof err.code !== 'number') {
+ resolve(undefined);
+ return;
+ }
+ resolve(String(stderr).trim() || `${POSIX_SHELL} -n exited with code ${err.code}`);
+ },
+ );
+ } catch {
+ // 参数非法(如命令含 NUL)时 Node 同步抛错:不下结论,交给真实执行折叠成失败结果。
+ resolve(undefined);
+ }
});
}
@@ -167,8 +178,14 @@ export async function executePreRunHook(input: PreRunHookInput): Promise<PreRunH
if (input.signal?.aborted) return abortedResult();
const resolved = resolveHookCommand(input.command);
- const syntaxError = await findShellSyntaxError(resolved.command);
+ // 预检与真实执行共用同一个取消信号和超时预算。
+ const syntaxError = await findShellSyntaxError(resolved.command, {
+ signal: input.signal,
+ timeoutMs,
+ });
if (input.signal?.aborted) return abortedResult();
+ const remainingTimeoutMs =
+ timeoutMs === undefined ? undefined : Math.max(1, timeoutMs - (Date.now() - startedAt));
if (syntaxError) {
return {
status: 'failed',
@@ -245,14 +262,14 @@ export async function executePreRunHook(input: PreRunHookInput): Promise<PreRunH
setTimeout(() => settle({ exitCode: null }), 1_000).unref?.();
};
- // 未配置 timeoutMs 时不武装定时器 —— 不限时。
+ // 未配置 timeoutMs 时不武装定时器 —— 不限时;配置了则只用预检后剩余的预算。
const timer =
- timeoutMs === undefined
+ remainingTimeoutMs === undefined
? undefined
: setTimeout(() => {
timedOut = true;
killProcessTree(child?.pid, child, armForceSettle);
- }, timeoutMs);
+ }, remainingTimeoutMs);
timer?.unref?.();
// 任务 pause/delete → 与超时同款树杀 + 1s 强制 settle:abortInflightAndWait