Repository navigation
fix(agent-core-v2): disable subagent deadline when timeoutMs is 0 - #4868
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🦋 Changeset detectedLatest commit: 7767b3f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@codex review |
commit: |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
Port MoonshotAI#4844: CronCreate/CronDelete and MCP authenticate calls never fold, the authorization link stays visible (and click-inert) while the call is running, the reason stays visible after a failure, and compact density unfolds to rows instead of cards. Disable the external-subagent deadline when subagent.timeoutMs is 0 (MoonshotAI#4868). The in-process path already guards on a positive timeout inside the task service, so only executionBackend armed an immediate deadline. Record MoonshotAI#4886 as equivalent: task output tails are already read as raw bytes from the byte store, so the UTF-8 split that upstream fixed cannot occur here.
Requirement or Bug
在
kimi -p(print 模式)下未显式配置[subagent] timeout_ms正整数,或在config.toml中显式设置[subagent] timeout_ms = 0(文档定义为「无超时」)时,前台与后台子 Agent 都会在启动后被立即超时取消:前台直接返回Agent timed out after 0 hours.,后台任务立即终止为status = "timed_out"(timeoutMs = 0)。同属resolveSubagentTimeoutMs配置消费者的TowerSpawn也存在同样的0ms定时器问题。本 PR 使timeoutMs = 0按配置契约作为「无超时」运行,同时保留任务元数据中的timeoutMs: 0。Bug Reproduction Steps
config.toml中设置[subagent] timeout_ms(或显式设置[subagent] timeout_ms = 0),也未设置KIMI_SUBAGENT_TIMEOUT_MS。kimi -p "<触发调用 Agent 工具的 prompt>" --output-format stream-json(分别测试前台run_in_background = false与后台run_in_background = true,子 Agent 模拟耗时 1.5s)。subagent error: Agent timed out after 0 hours.,后台子 Agent 在约 100ms 内写入task.terminated(status: "timed_out", timeoutMs: 0)。Root Cause
这是根本修复。
kimi -p启动时,applyPrintModeConfigDefaults(packages/agent-core-v2/src/agent/task/printDefaults.ts)会将未显式配置的subagent.timeoutMs注入为0,与config.toml文档中[subagent] timeout_ms = 0表示「无超时」的约定一致(bashTool.ts与agentRunBatch.ts均已对<= 0跳过定时器)。但resolveSubagentTimeoutMs(this.config)的两个消费点——SubagentTool.execution(agentTool.ts)与TowerSpawnTool(spawnTool.ts)——都无条件执行了setTimeout(..., timeoutMs),在 Node.js 中0会作为最快到期定时器(约 1ms)立即触发controller.abort('Timed out')。此外,packages/agent-core-v2/test/tool/tool.test.ts中原有单测treats timeoutMs=0 as an immediate deadline and preserves the metadata将这一错误行为写成了预期。Code Changes
仅当
timeoutMs > 0时创建超时 deadline 与setTimeout定时器;当timeoutMs <= 0时不挂载超时定时器,直接等待子 Agent 完成(外部signal中止与TaskStop停止路径保持不变),同时继续在任务状态字段中写入timeoutMs(保留timeoutMs: 0元数据)。SubagentTool.execution / TowerSpawnTool.execute timeoutMs = resolveSubagentTimeoutMs(config) setFields({ ..., timeoutMs }) - timeoutHandle = setTimeout(abortAsTimedOut, timeoutMs) - outcome = await Promise.race([handle.completion, deadline]) + if timeoutMs > 0: + timeoutHandle = setTimeout(abortAsTimedOut, timeoutMs) + outcome = await Promise.race([handle.completion, deadline]) + else: + outcome = await handle.completion阅读顺序:
packages/agent-core-v2/src/agent/tools/agent/agentTool.ts:Agent工具在前台与后台共用的execution中按timeoutMs > 0门控超时定时器与deadlinePromise。packages/agent-core-v2/src/features/tower/tools/spawn/spawnTool.ts:TowerSpawn同步按timeoutMs > 0门控超时定时器并在finally中安全清理。packages/agent-core-v2/test/tool/tool.test.ts、packages/agent-core-v2/test/features/tower/tools/spawnTool.test.ts:修正旧预期单测,补充timeoutMs = 0与applyPrintModeConfigDefaults下的前台/后台回归用例。Screenshots / Recordings
以下为未配置
[subagent]时真实运行kimi -p "Launch subagent" --output-format stream-json(子 Agent 模拟响应延迟 1.5s)的前后输出对比。Agent调用:修复前立即报Agent timed out after 0 hours.,修复后正常等待 1.5s 返回子 Agent 结果。Agent调用(run_in_background: true):修复前约 103ms 即写入status: "timed_out", timeoutMs: 0,修复后在 1.5s 后正常completed并保留timeoutMs: 0元数据。{ "type": "task.terminated", "info": { "taskId": "agent-c1l0d91w", "status": "timed_out", "detached": true, "stopReason": "Timed out", "timeoutMs": 0, "kind": "agent", "subagentType": "explore" } }{ "type": "task.terminated", "info": { "taskId": "agent-vgsthz6f", "status": "completed", "detached": true, "timeoutMs": 0, "kind": "agent", "subagentType": "explore" } }Behavior Changes and Affected Users
kimi -p未显式设置[subagent] timeout_ms或KIMI_SUBAGENT_TIMEOUT_MS时的前台/后台Agent执行applyPrintModeConfigDefaults注入timeoutMs = 0后立即触发setTimeout(..., 0),前台报错Agent timed out after 0 hours.,后台立即变为timed_outtimeoutMs: 0printDefaults.ts原意即为无超时)[subagent] timeout_ms = 1(或任意正整数)或环境变量KIMI_SUBAGENT_TIMEOUT_MS[subagent] timeout_ms = 0时的Agent与TowerSpawn执行0ms超时中止子 Agent / Tower workertimeoutMs: 0docs/*/configuration/config-files.md已公开声明0表示无超时)[subagent] timeout_ms为正整数毫秒值7200000、显式[subagent] timeout_ms > 0、KIMI_SUBAGENT_TIMEOUT_MS)@moonshot-ai/agent-core-v2(SubagentTool、TowerSpawnTool),随构建进入@moonshot-ai/kimi-codeCLI、@moonshot-ai/kap-server与@moonshot-ai/kimi-code-sdk。契约清单(config-manifest.toml、wire-manifest.d.ts、state-manifest.d.ts、apiSurface.snapshot.test.ts.snap)与公开文档均无需变更(文档原本即声明0表示无超时)。[subagent] timeout_ms = 0后台子 Agent、applyPrintModeConfigDefaults前台与后台子 Agent、TowerSpawn在[subagent] timeout_ms = 0下的无超时执行及timeoutMs: 0元数据保留,并在真实kimi -p构建产物上通过 mock OpenAI 服务完成端到端验证。测试与校验记录
pnpm vitest run packages/agent-core-v2/test/tool/tool.test.ts packages/agent-core-v2/test/features/tower/tools/spawnTool.test.ts:2 files, 223 passed(新增/更新的 4 条用例先在未修复代码上验证全部失败,修复后全部通过)pnpm vitest run packages/agent-core-v2/test/app/config/config.test.ts packages/agent-core-v2/test/agent/task/ apps/kimi-code/test/cli/v2-run-print.test.ts:12 files, 393 passedpnpm vitest run packages/agent-core-v2/test/session/subagent/ packages/agent-core-v2/test/features/swarm/ packages/agent-core-v2/test/features/tower/ packages/agent-core-v2/test/app/config/configManifest.test.ts packages/kap-server/test/apiSurface.snapshot.test.ts:17 files, 784 passedpnpm run build:packages && pnpm --filter @moonshot-ai/kimi-code run build && pnpm -C apps/kimi-code run smoke:通过pnpm --filter @moonshot-ai/agent-core-v2 run typecheck && pnpm --filter @moonshot-ai/kimi-code-sdk run typecheck && pnpm --filter @moonshot-ai/kap-server run typecheck && pnpm --filter @moonshot-ai/kimi-code run typecheck:0 errorspnpm run lint(oxlint --type-aware全仓 3286 文件):0 errors;pnpm run sherif:No issues foundChecklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.