Skip to content

fix(agent-core-v2): disable subagent deadline when timeoutMs is 0 - #4868

Merged
sailist merged 1 commit into
mainfrom
fix/subagent-zero-timeout
Nov 12, 2026
Merged

sailist merged 1 commit into
mainfrom
fix/subagent-zero-timeout

Conversation

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator

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

  1. 不在 config.toml 中设置 [subagent] timeout_ms(或显式设置 [subagent] timeout_ms = 0),也未设置 KIMI_SUBAGENT_TIMEOUT_MS。
  2. 运行 kimi -p "<触发调用 Agent 工具的 prompt>" --output-format stream-json(分别测试前台 run_in_background = false 与后台 run_in_background = true,子 Agent 模拟耗时 1.5s)。
  3. 观察到前台子 Agent 在数十毫秒内返回 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 门控超时定时器与 deadline Promise。
  • 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)的前后输出对比。

  1. 前台 Agent 调用:修复前立即报 Agent timed out after 0 hours.,修复后正常等待 1.5s 返回子 Agent 结果。
Before After
agent_id: agent-0
actual_subagent_type: explore
status: failed
stop_reason: timed_out

subagent error: Agent timed out after 0 hours.

[partial output] none — the subagent wrote no text in this run before it ended.

agent_id: agent-0
actual_subagent_type: explore
status: completed
stop_reason: completed

[summary]
Subagent finished after 1.5s.

  1. 后台 Agent 调用(run_in_background: true):修复前约 103ms 即写入 status: "timed_out", timeoutMs: 0,修复后在 1.5s 后正常 completed 并保留 timeoutMs: 0 元数据。
Before After
{
  "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

Behavior Before After Who relies on the old behavior Escape hatch
kimi -p 未显式设置 [subagent] timeout_ms 或 KIMI_SUBAGENT_TIMEOUT_MS 时的前台/后台 Agent 执行 applyPrintModeConfigDefaults 注入 timeoutMs = 0 后立即触发 setTimeout(..., 0),前台报错 Agent timed out after 0 hours.,后台立即变为 timed_out 不挂载超时定时器,子 Agent 运行至自然结束或被取消,任务元数据保留 timeoutMs: 0 无真实用户依赖该故障(文档与 printDefaults.ts 原意即为无超时) 如需极短超时上限,可显式设置 [subagent] timeout_ms = 1(或任意正整数)或环境变量 KIMI_SUBAGENT_TIMEOUT_MS
显式配置 [subagent] timeout_ms = 0 时的 Agent 与 TowerSpawn 执行 立即触发 0ms 超时中止子 Agent / Tower worker 按配置文档语义不设超时上限,任务元数据保留 timeoutMs: 0 无(docs/*/configuration/config-files.md 已公开声明 0 表示无超时) 设置 [subagent] timeout_ms 为正整数毫秒值
正整数超时(默认交互模式 7200000、显式 [subagent] timeout_ms > 0、KIMI_SUBAGENT_TIMEOUT_MS) 到期触发超时中止与 grace period 部分输出回收 保持不变 全体 TUI、desktop 与 web、VS Code、ACP、SDK 用户 N/A
  • 受影响模块:@moonshot-ai/agent-core-v2(SubagentTool、TowerSpawnTool),随构建进入 @moonshot-ai/kimi-code CLI、@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 passed
  • pnpm 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 passed
  • pnpm 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 errors
  • pnpm run lint(oxlint --type-aware 全仓 3286 文件):0 errors;pnpm run sherif:No issues found

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T08:22:43.983386Z 7767b3f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@changeset-bot

changeset-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7767b3f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Nov 12, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@7767b3f
npx https://pkg.pr.new/@moonshot-ai/kimi-code@7767b3f

commit: 7767b3f

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 7767b3f891

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@sailist
sailist merged commit 68b849c into main Nov 12, 2026
17 checks passed
@sailist
sailist deleted the fix/subagent-zero-timeout branch November 12, 2026 08:32
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 15, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants