Skip to content

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

Open

Description

@smmoroz

On Kimi Code 2.6.3, kimi -p exits with status 0 after the first model response, before a configured Stop hook finishes. The same hook completes on 2.5.3 and its exit status 2 triggers a second model request.

Reproduced on macOS arm64 with a local OpenAI-compatible test endpoint.

Minimal reproduction

Save this as stop.sh in a writable directory. The one-second delay makes it easy to see whether the CLI waits. The marker makes the hook request continuation only once.

#!/bin/sh
marker="$(dirname "$0")/stop-ran"
if [ -f "$marker" ]; then
    exit 0
fi
sleep 1
: > "$marker"
printf '%s\n' 'Reply with SECOND and no tools.' >&2
exit 2

Add the hook to ~/.kimi-code/config.toml alongside a working model configuration.

[[hooks]]
event = "Stop"
command = "/bin/sh /absolute/path/to/stop.sh"

Remove stop-ran before each run. Run the same command with 2.5.3 and 2.6.3.

kimi --version
kimi -p 'Reply with FIRST and no tools.'

Observed results

Version Output stop-ran at CLI exit Model requests Exit status
2.5.3 FIRST, then SECOND Present 2 0
2.6.3 FIRST only Absent 1 0

Expected behavior

Print mode should wait for the Stop hook to finish and run the continuation requested by exit status 2 before exiting.

This looks like the print-mode shutdown race raised during review of the change that queues Stop hooks after turn completion. PR #4995

Activity

  1. IvanWng97 commented on Nov 22, 2026

    @IvanWng97

    Still reproduces on 2.7.2, and main has the same code. A Stop hook that only observes, without blocking, loses the event the same way. In our CI's headless kimi -p runs on macOS runners, the Stop hook never ran in 5 of 29 runs since 2.7.2. On 2.5.3 it was 0 of 45.

    The race, at @moonshot-ai/[email protected]:

    • notifyTurnEnded queues runStop on stopHookQueue after the turn has ended (packages/agent-core-v2/src/features/externalHooks/agent/agentExternalHooksService.ts:429-433).
    • Print mode's cleanup waits only for the agent loops to settle (quiesceSessionAgents). It then flushes and calls client.close() (apps/kimi-code/src/cli/v2/run-v2-print.ts:177-189), which disposes the agent.
    • The service's dispose aborts stopHookAbortController (agentExternalHooksService.ts:92-99), and runHook kills the hook process on that abort (packages/agent-core-v2/src/features/externalHooks/internal/runHook.ts:120-123).

    So whether Stop runs depends on whether the hook finishes before client.close(). One possible fix is for print mode to await the queued Stop hooks (stopHookQueue) before it disposes the agent, for example as part of quiescing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions