Skip to content

fix(agent-core-v2): wait for queued Stop hooks before print mode exits - #5276

Open
nitishagar wants to merge 1 commit into
MoonshotAI:mainfrom
nitishagar:fix/5032-stop-hook-exit
Open

nitishagar wants to merge 1 commit into
MoonshotAI:mainfrom
nitishagar:fix/5032-stop-hook-exit

Conversation

@nitishagar

Copy link
Copy Markdown

Fixes #5032.

Summary

kimi -p returned as soon as the first turn completed, so client.close() disposed the agent and aborted queued Stop hooks before they finished; exit-2 continuations were silently dropped (2.5.3 waited for the hook and ran the continuation).

  • Add IAgentExternalHooksService.drainStopHooks() that settles the post-turn Stop hook queue.
  • applyPrintBackgroundPolicy drains queued Stop hooks at the top of each iteration, before deciding the run is quiescent.
  • Print-mode turnActive now also treats queued and notified loop work as active, so a Stop-hook continuation sitting in the loop inbox keeps the run alive until its turn ends.

No behavior change when no Stop hooks are configured (drain is a no-op on a settled queue).

Test evidence

  • apps/kimi-code/test/cli/run-v2-print.test.ts: 28/28 pass, including a new policy test proving a Stop-hook continuation turn is consumed before exit.
  • packages/agent-core-v2/test/features/externalHooks/integration.test.ts: 27/27 pass, including a new testkit test with a slow blocking Stop hook proving the continuation is observable after drainStopHooks().
  • tsc typecheck: agent-core-v2 and kimi-code app pass.
  • Lint: check-comments, import-boundaries, guards, test-doubles, quarantine, instructions pass; oxlint clean on all touched files (full repo-wide type-aware oxlint and the Desktop renderer build are killed by the sandbox resource limits on base too).

Print mode returned as soon as the first turn completed, so client.close()
disposed the agent and aborted queued Stop hooks before they finished, and
exit-2 continuations were dropped. Drain the Stop hook queue in the print
background policy loop and treat queued or notified loop work as active so
Stop-hook continuation turns run to completion.

Fixes MoonshotAI#5032
@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2f0670b

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 Nov 23, 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-23T08:46:38.877021Z 2f0670b PR opened
ℹ️ 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.

@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: 2f0670bd16

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

let lastPastFireAt: number | undefined;
let cronWedged = false;
for (;;) {
await input.drainStopHooks?.();

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 Drain Stop hooks after goal-wait turns

When goalActive() remains true, this drain runs only once before the inner goal-wait loop. That loop consumes later turn.ended events without returning to the outer loop; if the final goal turn enqueues a slow or blocking Stop hook, the goal can become inactive and mode: 'exit' returns while it is pending, so cleanup may quiesce the agent before the hook's continuation is handled. Recheck or drain after goal-wait endings and cover this route.

AGENTS.md reference: AGENTS.md:L60-L60

Useful? React with 👍 / 👎.

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.

kimi -p exits before a blocking Stop hook finishes in 2.6.3

1 participant