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): 换 agentKind 时丢弃上一引擎的模型路由,schedule_update 支持 null 清空 (#4165)

fico-hubd2f5a9c13d83
diff --git a/packages/lizi-mcps/src/__tests__/cindy_schedulerMcpServer.test.ts b/packages/lizi-mcps/src/__tests__/cindy_schedulerMcpServer.test.ts
index dd4006f6e1..e0ee9e3b90 100644
--- a/packages/lizi-mcps/src/__tests__/cindy_schedulerMcpServer.test.ts
+++ b/packages/lizi-mcps/src/__tests__/cindy_schedulerMcpServer.test.ts
@@ -1109,6 +1109,63 @@ describe('schedule_update — partial 语义的 JSON 边界翻译', () => {
expect(updated.patch?.intervalMs).toBeUndefined();
});
+ it('model / providerId / effort / workingDir: null → 带 key 的 undefined(显式清空路由)', async () => {
+ // 此前四个字段 schema 只收 string,agent 想把伙伴接管期钉上的 Codex 路由清掉
+ // 只能拿到 INVALID_ARGS,陈旧 model 永远摘不下来。
+ const { updated, registry } = setup({
+ agentKind: 'codex',
+ model: 'gpt-6-astra',
+ providerId: 'openai',
+ effort: 'medium',
+ workingDir: '/repo',
+ });
+ const env = await callUpdate(registry, {
+ agentKind: 'claude-code',
+ model: null,
+ providerId: null,
+ effort: null,
+ workingDir: null,
+ });
+ expect(env.ok).toBe(true);
+ for (const key of ['model', 'providerId', 'effort', 'workingDir']) {
+ expect(Object.prototype.hasOwnProperty.call(updated.patch ?? {}, key)).toBe(true);
+ expect(updated.patch?.[key]).toBeUndefined();
+ }
+ expect(updated.patch?.agentKind).toBe('claude-code');
+ });
+
+ it('workingDir: null 且任务仍开 useWorktree → 拒绝;同时关 useWorktree → 清目录并回到 dialogue', async () => {
+ // worktree 需要 workingDir 作为基仓,只清目录会留下一条每次 fire 都失败的任务(codex review)
+ const { updated, registry } = setup({ workingDir: '/repo', useWorktree: true });
+ const rejected = await callUpdate(registry, { workingDir: null });
+ expect(rejected.ok).toBe(false);
+ expect(updated.patch).toBeUndefined();
+
+ const ok = await callUpdate(registry, { workingDir: null, useWorktree: false });
+ expect(ok.ok).toBe(true);
+ expect(Object.prototype.hasOwnProperty.call(updated.patch ?? {}, 'workingDir')).toBe(true);
+ expect(updated.patch?.workingDir).toBeUndefined();
+ expect(updated.patch?.useWorktree).toBe(false);
+ expect(updated.patch?.workspaceKind).toBe('dialogue');
+
+ // 任务本来没开 worktree → 直接清
+ const plain = setup({ workingDir: '/repo', useWorktree: false });
+ const env = await callUpdate(plain.registry, { workingDir: null });
+ expect(env.ok).toBe(true);
+ expect(plain.updated.patch?.workingDir).toBeUndefined();
+ expect(plain.updated.patch?.workspaceKind).toBe('dialogue');
+ });
+
+ it('省略 model / providerId → patch 不带 key(真 partial);非法 effort 仍被拒', async () => {
+ const { updated, registry } = setup({ model: 'gpt-6-astra', providerId: 'openai' });
+ const env = await callUpdate(registry, { prompt: 'only prompt' });
+ expect(env.ok).toBe(true);
+ expect(Object.prototype.hasOwnProperty.call(updated.patch ?? {}, 'model')).toBe(false);
+ expect(Object.prototype.hasOwnProperty.call(updated.patch ?? {}, 'providerId')).toBe(false);
+ const bad = await callUpdate(registry, { effort: 'turbo' });
+ expect(bad.ok).toBe(false);
+ });
+
it('preRunHook 只改 command → 沿用任务现有 timeoutMs,不再静默清成不限时', async () => {
const { updated, registry } = setup({
preRunHook: { command: 'node /abs/old.mjs', timeoutMs: 180_000 },
diff --git a/packages/lizi-mcps/src/scheduler/update.ts b/packages/lizi-mcps/src/scheduler/update.ts
index faa14e755d..f506f8779b 100644
--- a/packages/lizi-mcps/src/scheduler/update.ts
+++ b/packages/lizi-mcps/src/scheduler/update.ts
@@ -40,10 +40,26 @@ export function registerScheduleUpdateTool(
recurring: z.boolean().optional(),
agentKind: z.enum(AGENT_KIND).optional(),
kind: z.literal('cron').optional(),
- model: z.string().optional(),
- providerId: z.string().optional(),
- effort: z.enum(EFFORT).optional(),
- workingDir: z.string().optional(),
+ model: z
+ .string()
+ .nullable()
+ .optional()
+ .describe('传 null = 清空,回到所选 agentKind 的默认模型路由;省略 = 不修改。换 agentKind 时上一引擎的 model 会被引擎自动丢弃,无需手动清。'),
+ providerId: z
+ .string()
+ .nullable()
+ .optional()
+ .describe('传 null = 清空(同 model 一起回到默认路由);省略 = 不修改。'),
+ effort: z
+ .enum(EFFORT)
+ .nullable()
+ .optional()
+ .describe('传 null = 清空(按模型默认档);省略 = 不修改。'),
+ workingDir: z
+ .string()
+ .nullable()
+ .optional()
+ .describe('传 null = 清空(回到绑定会话 / 对话工作区语义);省略 = 不修改。'),
useWorktree: z.boolean().optional(),
targetSessionId: z
.string()
@@ -111,6 +127,21 @@ export function registerScheduleUpdateTool(
// 这是 JSON 边界唯一的清空表达。
input = { ...input, intervalMs: undefined };
}
+ // 同上:model / providerId / effort / workingDir 的 null 也翻成带 key 的
+ // undefined(storage 按 hasOwnProperty 判定 → 清列 NULL)。此前 schema 只收
+ // string,agent 想把任务从一个引擎的模型路由上摘下来(如伙伴接管期用 Codex
+ // 模型跑过、再改回 Claude Code)只能报 INVALID_ARGS,陈旧路由永远清不掉。
+ for (const key of ['model', 'providerId', 'effort', 'workingDir'] as const) {
+ if ((patch as Record<string, unknown>)[key] === null) {
+ input = { ...input, [key]: undefined };
+ }
+ }
+ const clearsWorkingDir = (patch as { workingDir?: string | null }).workingDir === null;
+ if (clearsWorkingDir) {
+ // 清目录 = 回到对话工作区语义(与 create 缺省目录时的推断、引擎「给了真实
+ // 目录翻成 project」对称),否则 workspaceKind 仍是 project 却无目录。
+ input = { ...input, workspaceKind: 'dialogue' };
+ }
// 无条件校验本次 patch 显式带的 cronExpr / timezone(函数对缺省字段是
// no-op)。不能只在 patch 带 intervalMs 数值时校验:任务已有 intervalMs、
// patch 只改 cronExpr 时,真 partial 语义保留原 interval,引擎按
@@ -129,6 +160,17 @@ export function registerScheduleUpdateTool(
input = { ...input, targetSessionId: sessionId };
}
return scheduler.updateFromCurrent(id, async (existing) => {
+ // worktree 模式以 workingDir 为基仓(workdir-resolver 会拒绝无目录的 worktree
+ // 任务,每次 fire 都失败)。清目录时若任务仍开着 useWorktree 且本次没关,
+ // 明确拒绝而不是写出一条跑不动的任务(codex review)。
+ const nextUseWorktree = Object.prototype.hasOwnProperty.call(input, 'useWorktree')
+ ? input.useWorktree
+ : existing.useWorktree;
+ if (clearsWorkingDir && nextUseWorktree) {
+ throw new Error(
+ 'invalid request: workingDir 传 null 清空时任务仍开启 useWorktree(worktree 需要 workingDir 作为基仓);请同时传 useWorktree:false,或改传新的 workingDir',
+ );
+ }
// preRunHook.timeoutMs 的真 partial 表达:只改 command 时沿用任务现有超时
// (此前整对象替换会把省略的 timeoutMs 静默清成"不限时");传 null 才显式清除。
// null 在进入引擎前剥掉 —— 引擎/storage 只认 number | undefined。
diff --git a/packages/maker-scheduler/src/__tests__/scheduler.test.ts b/packages/maker-scheduler/src/__tests__/scheduler.test.ts
index 348fa0652c..5af1470310 100644
--- a/packages/maker-scheduler/src/__tests__/scheduler.test.ts
+++ b/packages/maker-scheduler/src/__tests__/scheduler.test.ts
@@ -411,6 +411,63 @@ describe('Scheduler', () => {
expect(plain.workspaceKind).toBe('project');
});
+ it('update() 换 agentKind 时丢弃上一引擎的 model / providerId / effort(回到默认路由)', async () => {
+ // 伙伴接管期把任务钉在 Codex 的 gpt-6-astra@openai 上,之后改回 Claude Code 却没法
+ // 清模型 → 任务带着 Claude Code 跑不了的路由,每轮 fire 都被上游拒绝。
+ const sch = await h.scheduler.create({
+ ...baseInput,
+ agentKind: 'codex',
+ model: 'gpt-6-astra',
+ providerId: 'openai',
+ effort: 'medium',
+ fastMode: true,
+ });
+ const switched = await h.scheduler.update(sch.id, { agentKind: 'claude-code' });
+ expect(switched.agentKind).toBe('claude-code');
+ expect(switched.model).toBeUndefined();
+ expect(switched.providerId).toBeUndefined();
+ expect(switched.effort).toBeUndefined();
+ // 旧引擎的 Fast 开关不能潜伏到下次切回 Codex/Pi 时复活
+ expect(switched.fastMode).toBe(false);
+ const back = await h.scheduler.update(sch.id, { agentKind: 'codex' });
+ expect(back.fastMode).toBe(false);
+ expect(back.model).toBeUndefined();
+ // 落库 patch 必须带 key(storage 按 hasOwnProperty 清列),不能只是省略
+ const stored = h.storage.schedules.get(sch.id)!;
+ expect(Object.prototype.hasOwnProperty.call(stored, 'model')).toBe(true);
+ expect(stored.model).toBeUndefined();
+ expect(stored.providerId).toBeUndefined();
+
+ // 反证 1:agentKind 没变 → 路由原样保留
+ const sch2 = await h.scheduler.create({
+ ...baseInput,
+ agentKind: 'codex',
+ model: 'gpt-6-astra',
+ providerId: 'openai',
+ fastMode: true,
+ });
+ const same = await h.scheduler.update(sch2.id, { agentKind: 'codex', prompt: 'p2' });
+ expect(same.model).toBe('gpt-6-astra');
+ expect(same.providerId).toBe('openai');
+ expect(same.fastMode).toBe(true);
+ const untouched = await h.scheduler.update(sch2.id, { prompt: 'p3' });
+ expect(untouched.model).toBe('gpt-6-astra');
+
+ // 反证 2:换引擎同时显式给了新路由 → 按调用方意图
+ const explicit = await h.scheduler.update(sch2.id, {
+ agentKind: 'claude-code',
+ model: 'claude-fable-5-1',
+ providerId: 'anthropic',
+ });
+ expect(explicit.model).toBe('claude-fable-5-1');
+ expect(explicit.providerId).toBe('anthropic');
+ expect(explicit.effort).toBeUndefined();
+ expect(explicit.fastMode).toBe(false);
+ // 显式带 fastMode 按调用方意图
+ const keepFast = await h.scheduler.update(sch2.id, { agentKind: 'codex', fastMode: true });
+ expect(keepFast.fastMode).toBe(true);
+ });
+
it('update() 给了真实 workingDir 时翻成 project(与 create 推断对称)', async () => {
// 对话任务后来被显式改了项目目录 → 不能仍留 dialogue,否则 fire 时
// 新目录会被管理工作区分配覆盖,显式设置被静默丢弃
diff --git a/packages/maker-scheduler/src/engine/scheduler.ts b/packages/maker-scheduler/src/engine/scheduler.ts
index 501b77a239..c5b2d1df2f 100644
--- a/packages/maker-scheduler/src/engine/scheduler.ts
+++ b/packages/maker-scheduler/src/engine/scheduler.ts
@@ -1382,6 +1382,21 @@ export class Scheduler extends EventEmitter {
}
const existing = await this.get(id);
if (!existing) throw new Error(`Schedule not found: ${id}`);
+ // 换引擎(agentKind 变了)时,上一引擎的 model / providerId / effort 是另一套模型
+ // 目录里的路由,对新引擎没有意义,patch 没显式给就丢弃(带 key 的 undefined →
+ // storage 清列 NULL),让任务回到新引擎的默认路由。否则陈旧路由会被原样带过去:
+ // fire 时路由守卫对「没有任何来源提供该模型」的组合放行,runner 再把绑定会话的
+ // 凭证/模型切到这条跑不通的路由上,每轮都以上游拒绝失败,会话 meta 也被写坏
+ // (伙伴接管期用 Codex 模型钉过的任务改回 Claude Code 后即复现)。
+ // 显式给了(含带 key 的 undefined)按调用方意图,不覆盖。fastMode 同属完整运行路由
+ // (harness + provider + model + effort + fastMode),旧引擎的 Fast 开关同样不该
+ // 潜伏到下一次切回 Codex/Pi 时复活(codex review),未显式给则关掉。
+ if (patch.agentKind !== undefined && patch.agentKind !== existing.agentKind) {
+ for (const key of ['model', 'providerId', 'effort'] as const) {
+ if (!Object.prototype.hasOwnProperty.call(patch, key)) updates[key] = undefined;
+ }
+ if (!Object.prototype.hasOwnProperty.call(patch, 'fastMode')) updates.fastMode = false;
+ }
const candidate: Schedule = { ...existing, ...updates };
validateScheduleExecutionShape(
candidate,