Repository navigation
fix(agent-core-v2): wait for queued Stop hooks before print mode exits - #5276
nitishagar wants to merge 1 commit into
Conversation
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 detectedLatest commit: 2f0670b 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 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. |
There was a problem hiding this comment.
💡 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?.(); |
There was a problem hiding this comment.
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 👍 / 👎.
Fixes #5032.
Summary
kimi -preturned as soon as the first turn completed, soclient.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).IAgentExternalHooksService.drainStopHooks()that settles the post-turn Stop hook queue.applyPrintBackgroundPolicydrains queued Stop hooks at the top of each iteration, before deciding the run is quiescent.turnActivenow 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 afterdrainStopHooks().tsctypecheck: agent-core-v2 and kimi-code app pass.