Skip to content

feat: cap WaitFor at 90s and let new input end the wait - #4061

Open
chengluyu wants to merge 17 commits into
mainfrom
claude/waitfor-agent-core-2f238a
Open

chengluyu wants to merge 17 commits into
mainfrom
claude/waitfor-agent-core-2f238a

Conversation

@chengluyu

@chengluyu chengluyu commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Anonymous usage data shows the agent asks WaitFor for long timeouts (5–10 min) while real waits are short, and users waiting on it cannot get a message in until the wait ends.

Bug Reproduction Steps

  1. Have the agent start a background task and call WaitFor.
  2. Type a message in the TUI and press Enter.
  3. The message is queued and only reaches the agent after the wait returns (up to 10 min).

Root Cause

agent-core already ends a wait on a steer (wait_status: interrupted), but the TUI queues plain Enter input locally during a running turn and never steers it (only Ctrl-S / Tower mode do). Fundamental fix: steer while a main-agent WaitFor is running.

Code Changes

 WaitFor (agent-core-v2)
-  timeout cap 600s (tied to the Bash background timeout)
+  timeout cap 90s
+  description: call only when there is no other work; finished tasks notify you
+  2nd+ call in the same turn → [wait_warning] with calls + total waited time
+    main agent: strict ("only when the user explicitly wants you to wait")
+    active goal: softened (waiting beats Bash sleep polling / re-continuation)
+    subagent:    turn end is the hand-off, keep waiting for results you need
+  subagents get an extra description paragraph and timeout text saying the same

 TUI (kimi-code)
+  a main-agent WaitFor counts as running once its first progress update arrives
+    (never for rejected / skipped / no-task calls)
   sendMessage
-    steer only in Tower mode
+    also steer while a WaitFor is running (input is appended to the queue and the queue is steered)
   on WaitFor running
+    steer the queued backlog into the turn (prompt-only backlog)
   queue steer in flight
+    items stay at the queue front and the queue holds; success removes them,
+    failure leaves them in place (transcript entries undone, media handed back);
+    a queue left by an ended turn drains once the steer settles; stale-session steers are dropped
   staging leases
+    a lease whose dispatch is in flight survives turn end until the RPC settles
   WaitFor renderer
+    `wait_status: interrupted` → "Wait interrupted by new input"

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
WaitFor timeout max 600 90; larger values fail validation models used to long waits call again
WaitFor tool description "use when your next step depends on a task" "only when you have no other work"; tasks notify you every session with wait_for on (default), main/coder/Tower workers KIMI_CODE_EXPERIMENTAL_WAIT_FOR=0
Timeout result text "Call WaitFor again to keep waiting" main: prefer other work; subagent: call again if you need it same same
Repeated WaitFor in one turn no warning [wait_warning] (main / goal / subagent wording) same same
TUI Enter during a main-agent WaitFor queued until the turn ends steers; wait ends immediately TUI users Ctrl-S semantics unchanged
TUI queue when a WaitFor starts stays queued prompt-only backlog is steered in; bash/skill backlog stays queued TUI users none
Failed steer (Tower / WaitFor paths) input lost behind an error input stays queued in order, error shown TUI users none
Interrupted WaitFor result in the TUI rendered as "Waited for background task" "Wait interrupted by new input" TUI users none

Not changed: subagent WaitFor calls never trigger TUI steering; web UI (code-app) still queues and needs the same change separately. User docs (en/zh) updated.

Tests: task-tools.test.ts (cap, schema, subagent/main wiring), kimi-tui-message-flow.test.ts (Enter steers during WaitFor, queued backlog steered on WaitFor start, bash backlog kept, subagent WaitFor ignored, failed steer requeues), tool-call.test.ts (interrupted render). Prompt-wording substring tests for WaitFor were removed.

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.

Copilot AI balanced review requested due to automatic review settings September 28, 2026 07:27
@changeset-bot

changeset-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7d19195

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-28T10:57:47.085947Z 7d19195 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.

@pkg-pr-new

pkg-pr-new Bot commented Sep 28, 2026 •

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

commit: 7d19195

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Repeat warnings miss common outcomes, shared guidance contradicts web behavior, and required documentation and changeset updates are absent.

Review effort: Balanced
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Caps WaitFor at 90 seconds, adds repeat-wait guidance, and lets TUI input interrupt active waits.

Changes:

  • Adds main-agent, goal, and subagent wait guidance.
  • Steers fresh and queued TUI input during WaitFor.
  • Updates related tests and snapshots.
File Description
packages/​agent-core-v2/​test/​features/​goal/​stubs.ts Adds a goal-service stub.
packages/​agent-core-v2/​test/​features/​goal/​goal.test.ts Removes obsolete prompt assertions.
packages/​agent-core-v2/​test/​agent/​task/​tools/​task-tools.test.ts Updates timeout and constructor tests.
packages/​agent-core-v2/​test/​agent/​task/​taskService.test.ts Supplies new tool dependencies.
packages/​agent-core-v2/​test/​agent/​loop/​loop.test.ts Updates tool-description snapshots.
packages/​agent-core-v2/​src/​agent/​tools/​task/​task-wait/​taskWaitTool.ts Adds contextual repeat-wait guidance.
packages/​agent-core-v2/​src/​agent/​tools/​task/​task-wait/​task-wait.ts Caps waits at 90 seconds.
packages/​agent-core-v2/​src/​agent/​tools/​task/​task-wait/​task-wait.md Revises main wait guidance.
packages/​agent-core-v2/​src/​agent/​tools/​task/​task-wait/​task-wait-subagent.md Adds subagent-specific guidance.
apps/​kimi-code/​test/​tui/​kimi-tui-message-flow.test.ts Tests WaitFor steering behavior.
apps/​kimi-code/​src/​tui/​kimi-tui.ts Steers eligible input and backlog.
apps/​kimi-code/​src/​tui/​controllers/​streaming-ui.ts Detects active tools by name.
apps/​kimi-code/​src/​tui/​controllers/​session-event-handler.ts Steers queued input when WaitFor starts.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/agent-core-v2/src/agent/tools/task/task-wait/task-wait.ts
Comment thread packages/agent-core-v2/src/agent/tools/task/task-wait/task-wait.md

@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

waitStatus: 'interrupted',
reason: 'steer',

P2 Badge Render interrupted waits as interrupted

Whenever new input steers a running wait, this newly emitted wait_status: interrupted is not recognized by the TUI's parseWaitForOutput, whose status union only accepts completed, timed_out, and no_tasks. The result therefore falls through to the normal completed header (Waited for background task) and generic body rather than indicating that user input ended the wait; extend the WaitFor renderer and its tests to handle this new status.

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/tools/task/task-wait/task-wait.ts
Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
Comment on lines +209 to +211
} finally {
progress.stop();
tally.waitedMs += Date.now() - startedAt;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid double-counting concurrent wait duration

When one model response emits multiple WaitFor calls, the tool executor runs them concurrently but every call adds its full elapsed duration to the shared turn tally here. Two parallel 90-second waits therefore report up to three minutes of total waiting, and even the two results can show different totals depending on completion order; since this warning is intended to tell the model how long the user has actually been kept waiting, track wall-clock intervals or serialize the accounting rather than summing overlapping calls.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not applicable: WaitFor declares no accesses, so the executor gives it ToolAccesses.all() (toolExecutorService.ts:438), and all conflicts with all in ToolScheduler — two WaitFor calls from one response run one after the other, never concurrently. Their elapsed times therefore don't overlap and the per-turn sum is the real wall-clock waiting time.

@chengluyu

Copy link
Copy Markdown
Collaborator Author

Codex review-body finding (render interrupted waits as interrupted): fixed in 5ceb8c5. parseWaitForOutput now accepts wait_status: interrupted; the header reads "Wait interrupted by new input" and the body shows the still-running tasks like a timeout. Covered by renders a wait ended by new input as interrupted in tool-call.test.ts.

@codex review

@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: 5ceb8c52e3

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
Comment thread docs/en/reference/tools.md Outdated
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 750501cf55

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
Comment on lines +221 to +223
return {
output: this.withRepeatWarning(this.formatTimeout(args, startedAt, timeoutMs), tally),
isError: false,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Count the current wait before formatting the warning

For serialized repeated waits, withRepeatWarning is evaluated before the surrounding finally adds the current call's elapsed time to tally.waitedMs. Consequently, after two 90-second waits the second result reports only about 90 seconds total rather than three minutes, systematically understating the user-visible delay that this warning is intended to discourage; add the current duration before constructing the warning.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not applicable: withRepeatWarning is not evaluated inside the try. The timed-out and completed branches that call it come after the whole try/catch/finally statement, and the finally (which adds this call's Date.now() - startedAt to tally.waitedMs) has already run by then. The only early return inside the try statement is the interrupted one, which intentionally carries no warning. So after two 90-second waits the second result reports about 3m.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Still holds after 1f40d5e: the accumulation moved from the finally to right after the try statement (and into the interrupted branch), in both cases before withRepeatWarning builds the total.

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: b0fded275e

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 517ba3b451

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 242e5a7847

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
Comment thread apps/kimi-code/src/tui/controllers/session-event-handler.ts Outdated
Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 3f68738a63

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: b37c1eec3c

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/controllers/editor-keyboard.ts Outdated
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 4840cc0e80

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Outdated
Comment thread apps/kimi-code/src/tui/controllers/session-event-handler.ts
Comment thread apps/kimi-code/src/tui/controllers/editor-keyboard.ts
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 31a5e68b9a

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 7d1919551d

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chengluyu chengluyu closed this Sep 28, 2026
@chengluyu
chengluyu deleted the claude/waitfor-agent-core-2f238a branch September 28, 2026 17:53
@chengluyu
chengluyu restored the claude/waitfor-agent-core-2f238a branch September 28, 2026 17:55
@chengluyu chengluyu reopened this Sep 28, 2026

This branch has not been deployed

No deployments
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