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): 预检耗尽超时预算时不再启动 hook,例行任务保存也做语法预检

Dashda9c3cec98ba
diff --git a/apps/desktop/src/main/routines/__tests__/service.test.ts b/apps/desktop/src/main/routines/__tests__/service.test.ts
index 61e1f0593c..b34173585a 100644
--- a/apps/desktop/src/main/routines/__tests__/service.test.ts
+++ b/apps/desktop/src/main/routines/__tests__/service.test.ts
@@ -101,6 +101,19 @@ it('dispatches into the current canonical task through the existing silent runne
expect((await routineTools.history('bot', routine.id))[0].resultText).toBe('Reviewed PR');
});
+it.skipIf(process.platform === 'win32')('rejects saving a pre-run hook command with a shell syntax error', async () => {
+ const input = {
+ name: 'Broken check',
+ prompt: 'Check',
+ enabled: false,
+ triggers: [{ id: 'tick', kind: 'interval' as const, intervalMs: 60000 }],
+ preRunHook: { command: `node '/a b/x.mjs'//'` },
+ };
+ await expect(routineTools.save('bot', input)).rejects.toThrow(/shell syntax error/);
+ await expect(routineTools.createOnce('bot', input, 'broken-hook-12345')).rejects.toThrow(/shell syntax error/);
+ expect(await routineTools.list('bot')).toEqual([]);
+});
+
it('blocks enabling and manual runs until imported handover is ready while retaining disabled edits', async () => {
let ready = false;
configureRoutineHost({
diff --git a/apps/desktop/src/main/routines/service.ts b/apps/desktop/src/main/routines/service.ts
index 7e1d3a7da8..38cda29512 100644
--- a/apps/desktop/src/main/routines/service.ts
+++ b/apps/desktop/src/main/routines/service.ts
@@ -22,6 +22,7 @@ import { assertTrustedAppRendererEvent } from '../security/trustedAppRenderer.js
import { throwIpcError } from '../utils/ipcValidate.js';
import { createLogger } from '../logger.js';
import { RoutineFileStore } from './store.js';
+import { assertPreRunHookCommandSyntax } from '../scheduler-host/pre-run-hook.js';
import { untrustedJsonBlock } from '../../shared/untrustedPrompt.js';
import { getCurrentDbClientSnapshot } from '../localDb/client/current.js';
import { botProfiles } from '../localDb/schema.js';
@@ -412,16 +413,23 @@ async function withBot<T>(
}
/** Single management boundary used by desktop, remote resources and all MCP harnesses. */
+/** routine_save、伙伴设置与远程资源共用的保存边界:与调度器保存同一套前置检查语法预检。 */
+async function assertRoutineHookSyntax(input: RoutineInput): Promise<void> {
+ if (input.preRunHook?.command?.trim()) await assertPreRunHookCommandSyntax(input.preRunHook.command);
+}
+
export const routineTools = {
list: (botId: string) => withBot(botId, (engine) => engine.list(botId)),
createOnce: (botId: string, input: RoutineInput, creationId: string) =>
withBot(botId, async (engine, scope) => {
+ await assertRoutineHookSyntax(input);
if (input.enabled) await getRoutineHost().assertImportedAutomationReady?.(ownerScopedUserDataPath(), botId, creationId, () => assertScope(scope), { input });
assertScope(scope);
return engine.createOnce(botId, input, creationId);
}),
save: (botId: string, input: RoutineInput, id?: string, expectedRevision?: number) =>
withBot(botId, async (engine, scope) => {
+ await assertRoutineHookSyntax(input);
if (id && input.enabled) {
const recoveredRevision = await getRoutineHost().assertImportedAutomationReady?.(ownerScopedUserDataPath(), botId, id, () => assertScope(scope), { input, expectedRevision });
if (recoveredRevision !== undefined) expectedRevision = recoveredRevision;
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 3116c90453..ab8938f82d 100644
--- a/apps/desktop/src/main/scheduler-host/__tests__/preRunHook.test.ts
+++ b/apps/desktop/src/main/scheduler-host/__tests__/preRunHook.test.ts
@@ -7,7 +7,20 @@
* @vitest-environment node
*/
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
+
+// 透传真实 spawn,只计数:用来断言某些路径根本没有启动真实命令。
+const spawnCalls = vi.hoisted(() => ({ count: 0 }));
+vi.mock('node:child_process', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('node:child_process')>();
+ return {
+ ...actual,
+ spawn: ((...args: Parameters<typeof actual.spawn>) => {
+ spawnCalls.count += 1;
+ return actual.spawn(...args);
+ }) as typeof actual.spawn,
+ };
+});
import {
assertPreRunHookCommandSyntax,
@@ -210,6 +223,19 @@ describe.skipIf(process.platform === 'win32')('前置检查命令语法预检',
expect(result.spawnError || result.error).toBeTruthy();
});
+ it('预检耗尽超时预算 → 直接 timed_out,不再启动真实命令', async () => {
+ spawnCalls.count = 0;
+ const result = await executePreRunHook({
+ command: 'node -e "process.exit(0)"',
+ timeoutMs: 1,
+ stdinPayload: payload,
+ });
+ expect(result.status).toBe('timed_out');
+ expect(result.decision).toBe('block');
+ expect(result.timedOut).toBe(true);
+ expect(spawnCalls.count).toBe(0);
+ });
+
it('预检响应取消信号:已取消时不再预检,执行返回 aborted', async () => {
const controller = new AbortController();
controller.abort();
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 c63611287f..539f2a7577 100644
--- a/apps/desktop/src/main/scheduler-host/pre-run-hook.ts
+++ b/apps/desktop/src/main/scheduler-host/pre-run-hook.ts
@@ -185,7 +185,23 @@ export async function executePreRunHook(input: PreRunHookInput): Promise<PreRunH
});
if (input.signal?.aborted) return abortedResult();
const remainingTimeoutMs =
- timeoutMs === undefined ? undefined : Math.max(1, timeoutMs - (Date.now() - startedAt));
+ timeoutMs === undefined ? undefined : timeoutMs - (Date.now() - startedAt);
+ // 预检已耗尽配置的超时预算:不再启动真实命令,直接按超时阻止本轮。
+ if (remainingTimeoutMs !== undefined && remainingTimeoutMs <= 0) {
+ return {
+ status: 'timed_out',
+ decision: 'block',
+ exitCode: null,
+ durationMs: Date.now() - startedAt,
+ stdout: '',
+ stderr: '',
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ timedOut: true,
+ aborted: false,
+ error: `pre-run hook timed out after ${timeoutMs}ms`,
+ };
+ }
if (syntaxError) {
return {
status: 'failed',