Files
claude-code/packages/builtin-tools/src/tools/ScheduleCronTool/CronCreateTool.ts
claude-code-best 99b9c6a400 fix: ExecuteExtraTool 加 schema 预校验防止 deferred 工具崩溃
模型通过 ExecuteExtraTool 调 CronCreate 时把字段名拼成 schedule(而非
cron),raw params 透传到 validateInput,input.cron 为 undefined,触
发 parseCronExpression 的 expr.trim() 抛 TypeError。所有参数组合同
样崩溃,与具体参数无关。

三层修复:
- ExecuteTool.call 在调 validateInput 前先跑 targetTool.inputSchema.
  safeParse(鸭子类型跳过 MCP),失败时返回 formatZodValidationError
  友好消息,成功时透传 parsed data 让 .default() 生效、strictObject
  拦截多余字段。这是架构根治,覆盖所有 deferred 工具。
- CronCreateTool.validateInput 顶部加 typeof string 守卫,给模型精确
  字段错误消息。
- parseCronExpression 顶部加 typeof string 守卫,覆盖 cronToHuman 等
  所有调用者。

新增 4 个测试覆盖:cron undefined 输入返回 null、ExecuteTool schema
验证拒绝错字段名且 validateInput 不被触达、.default() 透传、MCP-like
工具跳过 schema 校验。

Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
2026-06-18 13:59:27 +08:00

171 lines
6.2 KiB
TypeScript

import { z } from 'zod/v4'
import { setScheduledTasksEnabled } from 'src/bootstrap/state.js'
import type { ValidationResult } from 'src/Tool.js'
import { buildTool, type ToolDef } from 'src/Tool.js'
import { cronToHuman, parseCronExpression } from 'src/utils/cron.js'
import {
addCronTask,
getCronFilePath,
listAllCronTasks,
nextCronRunMs,
} from 'src/utils/cronTasks.js'
import { lazySchema } from 'src/utils/lazySchema.js'
import { semanticBoolean } from 'src/utils/semanticBoolean.js'
import { getTeammateContext } from 'src/utils/teammateContext.js'
import {
buildCronCreateDescription,
buildCronCreatePrompt,
CRON_CREATE_TOOL_NAME,
DEFAULT_MAX_AGE_DAYS,
isDurableCronEnabled,
isKairosCronEnabled,
} from './prompt.js'
import { renderCreateResultMessage, renderCreateToolUseMessage } from './UI.js'
const MAX_JOBS = 50
const inputSchema = lazySchema(() =>
z.strictObject({
cron: z
.string()
.describe(
'Standard 5-field cron expression in local time: "M H DoM Mon DoW" (e.g. "*/5 * * * *" = every 5 minutes, "30 14 28 2 *" = Feb 28 at 2:30pm local once).',
),
prompt: z.string().describe('The prompt to enqueue at each fire time.'),
recurring: semanticBoolean(z.boolean().optional()).describe(
`true (default) = fire on every cron match until deleted or auto-expired after ${DEFAULT_MAX_AGE_DAYS} days. false = fire once at the next match, then auto-delete. Use false for "remind me at X" one-shot requests with pinned minute/hour/dom/month.`,
),
durable: semanticBoolean(z.boolean().optional()).describe(
'true = persist to .claude/scheduled_tasks.json and survive restarts. false (default) = in-memory only, dies when this Claude session ends. Use true only when the user asks the task to survive across sessions.',
),
}),
)
type InputSchema = ReturnType<typeof inputSchema>
const outputSchema = lazySchema(() =>
z.object({
id: z.string(),
humanSchedule: z.string(),
recurring: z.boolean(),
durable: z.boolean().optional(),
}),
)
type OutputSchema = ReturnType<typeof outputSchema>
export type CreateOutput = z.infer<OutputSchema>
export const CronCreateTool = buildTool({
name: CRON_CREATE_TOOL_NAME,
searchHint: 'schedule a recurring or one-shot prompt',
maxResultSizeChars: 100_000,
shouldDefer: true,
get inputSchema(): InputSchema {
return inputSchema()
},
get outputSchema(): OutputSchema {
return outputSchema()
},
isEnabled() {
return isKairosCronEnabled()
},
toAutoClassifierInput(input) {
return `${input.cron}: ${input.prompt}`
},
async description() {
return buildCronCreateDescription(isDurableCronEnabled())
},
async prompt() {
return buildCronCreatePrompt(isDurableCronEnabled())
},
getPath() {
return getCronFilePath()
},
async validateInput(input): Promise<ValidationResult> {
// ExecuteExtraTool passes raw params through without re-running this
// tool's inputSchema, so when the model uses a wrong field name (e.g.
// 'schedule' instead of 'cron'), input.cron is undefined. parseCronExpression
// would throw on .trim(undefined); catch here with a message that tells
// the model which field is actually required.
if (typeof input.cron !== 'string' || input.cron.length === 0) {
return {
result: false,
message:
"Missing required parameter 'cron' (5-field cron expression, e.g. '*/5 * * * *'). Check parameter names against the schema.",
errorCode: 1,
}
}
if (!parseCronExpression(input.cron)) {
return {
result: false,
message: `Invalid cron expression '${input.cron}'. Expected 5 fields: M H DoM Mon DoW.`,
errorCode: 1,
}
}
if (nextCronRunMs(input.cron, Date.now()) === null) {
return {
result: false,
message: `Cron expression '${input.cron}' does not match any calendar date in the next year.`,
errorCode: 2,
}
}
const tasks = await listAllCronTasks()
if (tasks.length >= MAX_JOBS) {
return {
result: false,
message: `Too many scheduled jobs (max ${MAX_JOBS}). Cancel one first.`,
errorCode: 3,
}
}
// Teammates don't persist across sessions, so a durable teammate cron
// would orphan on restart (agentId would point to a nonexistent teammate).
if (input.durable && getTeammateContext()) {
return {
result: false,
message:
'durable crons are not supported for teammates (teammates do not persist across sessions)',
errorCode: 4,
}
}
return { result: true }
},
async call({ cron, prompt, recurring = true, durable = false }) {
// Kill switch forces session-only; schema stays stable so the model sees
// no validation errors when the gate flips mid-session.
const effectiveDurable = durable && isDurableCronEnabled()
const id = await addCronTask(
cron,
prompt,
recurring,
effectiveDurable,
getTeammateContext()?.agentId,
)
// Enable the scheduler so the task fires in this session. The
// useScheduledTasks hook polls this flag and will start watching
// on the next tick. For durable: false tasks the file never changes
// — check() reads the session store directly — but the enable flag
// is still what starts the tick loop.
setScheduledTasksEnabled(true)
return {
data: {
id,
humanSchedule: cronToHuman(cron),
recurring,
durable: effectiveDurable,
},
}
},
mapToolResultToToolResultBlockParam(output, toolUseID) {
const where = output.durable
? 'Persisted to .claude/scheduled_tasks.json'
: 'Session-only (not written to disk, dies when Claude exits)'
return {
tool_use_id: toolUseID,
type: 'tool_result',
content: output.recurring
? `Scheduled recurring job ${output.id} (${output.humanSchedule}). ${where}. Auto-expires after ${DEFAULT_MAX_AGE_DAYS} days. Use CronDelete to cancel sooner.`
: `Scheduled one-shot task ${output.id} (${output.humanSchedule}). ${where}. It will fire once then auto-delete.`,
}
},
renderToolUseMessage: renderCreateToolUseMessage,
renderToolResultMessage: renderCreateResultMessage,
} satisfies ToolDef<InputSchema, CreateOutput>)