Skip to content

fix(kimi-code): keep kimi -p alive while background tasks are pending - #2675

Merged
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:fix/print-wait-timer-overflow
Aug 6, 2026
Merged

fix(kimi-code): keep kimi -p alive while background tasks are pending#2675
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:fix/print-wait-timer-overflow

Conversation

@sailist

@sailist sailist commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

In print mode (kimi -p), the default background-policy wait ceiling is 10 years (315360000s). Converted to milliseconds it exceeds Node's setTimeout limit (2^31-1 ms), and Node silently re-arms such a timer at ~1ms instead of rejecting it (the only trace is a TimeoutOverflowWarning). Every wait in the print background policy — both steer (the default) and drain — therefore returned immediately: a kimi -p run whose main turn fanned out background Bash tasks or subagents exited right after the main turn, shutting down all pending work and losing its output.

This is a v1→v2 porting regression: v1 clamps the delay to 2^31-1 ms (MAX_TIMER_DELAY_MS in packages/agent-core/src/utils/promise.ts), but the v2 port dropped the clamp, and kimi -p runs on the v2 engine by default.

What changed

  • Shared clamped timer: setClampedTimeout in agent-core-v2's _base/utils/timer.ts clamps any delay to MAX_TIMER_DELAY_MS; every config-driven timer routes through it (timeoutOutcome, task wait / manager timeout, swarm attempt timeout), so no call site handles the constant itself.
  • Chunked print wait: the steer loop's turn-endings wait re-arms against the real deadline when a clamped chunk expires, instead of treating the first fire as the timeout (a bare clamp would misreport an early chunk as "deadline reached").
  • Default ceiling: print_wait_ceiling_s now defaults to 2147483s (~24.8 days, the timer maximum) — still effectively unbounded, but the default itself can no longer overflow. Explicitly configured larger ceilings remain safe via the clamp + chunking. User docs (en/zh) updated.
  • v1 parity fix found along the way: the v2 swarm batch dropped v1's timeout <= 0 guard, so print mode's unbounded subagent timeout (0) would have aborted swarm subagents immediately; restored.

Regression tests cover: the default-ceiling steer wait staying alive with pending tasks, oversized turn-ending budgets resolving only at the real deadline, taskService.wait clamping, and swarm timeout: 0 meaning unbounded.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

The 10-year default print wait ceiling (315360000s) overflowed Node's
setTimeout limit (2^31-1 ms) into a 1ms fire, so the steer/drain wait
returned instantly and kimi -p exited right after the main turn, killing
pending background tasks and subagents.

- add setClampedTimeout in agent-core-v2 _base, clamping delays to
  MAX_TIMER_DELAY_MS, and route every config-driven timer through it
  (timeoutOutcome, task wait/manager timeout, swarm attempt timeout)
- chunk the print turn-endings wait against the real deadline instead of
  returning null on the first clamped timer fire
- restore v1 semantics: a non-positive swarm subagent timeout is unbounded
- default print_wait_ceiling_s to 2147483s (~24.8 days, the timer maximum)
@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c403b82

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

@pkg-pr-new

pkg-pr-new Bot commented Aug 6, 2026

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

commit: c403b82

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c403b821e9

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

Comment on lines +875 to +877
// A clamped early return just makes callers (e.g. the print drain
// loop) re-poll — the task may still be running, which the caller
// observes from the returned info.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move the wait note out of the method body

In packages/agent-core-v2 implementation files, the local guide requires comments to live only in the top-of-file block; this new explanatory note is inside wait() beside a statement, so it violates the package convention and should be removed or folded into the file header if still needed.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L17-L17

Useful? React with 👍 / 👎.

@sailist
sailist merged commit 34c4181 into MoonshotAI:main Aug 6, 2026
15 checks passed
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Aug 15, 2026
…MoonshotAI#2675)

The 10-year default print wait ceiling (315360000s) overflowed Node's
setTimeout limit (2^31-1 ms) into a 1ms fire, so the steer/drain wait
returned instantly and kimi -p exited right after the main turn, killing
pending background tasks and subagents.

- add setClampedTimeout in agent-core-v2 _base, clamping delays to
  MAX_TIMER_DELAY_MS, and route every config-driven timer through it
  (timeoutOutcome, task wait/manager timeout, swarm attempt timeout)
- chunk the print turn-endings wait against the real deadline instead of
  returning null on the first clamped timer fire
- restore v1 semantics: a non-positive swarm subagent timeout is unbounded
- default print_wait_ceiling_s to 2147483s (~24.8 days, the timer maximum)
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.

1 participant