diff --git a/.changeset/print-drain-bash.md b/.changeset/print-drain-bash.md new file mode 100644 index 000000000..abdbd98e7 --- /dev/null +++ b/.changeset/print-drain-bash.md @@ -0,0 +1,5 @@ +--- +"@moonshot-ai/kimi-code": patch +--- + +In `kimi -p`, wait for background bash commands to finish and respond to their results before the turn ends, instead of tearing them down with the process. diff --git a/apps/kimi-code/src/cli/run-prompt.ts b/apps/kimi-code/src/cli/run-prompt.ts index 66315ff3e..6412d79ba 100644 --- a/apps/kimi-code/src/cli/run-prompt.ts +++ b/apps/kimi-code/src/cli/run-prompt.ts @@ -339,6 +339,7 @@ async function resolvePromptSession( permission: 'auto', additionalDirs: opts.addDirs?.length ? opts.addDirs : undefined, drainAgentTasksOnStop: true, + drainProcessTasksOnStop: true, }); installHeadlessHandlers(session); return { diff --git a/apps/kimi-code/test/cli/run-prompt.test.ts b/apps/kimi-code/test/cli/run-prompt.test.ts index d75a6b3a5..59d4705c4 100644 --- a/apps/kimi-code/test/cli/run-prompt.test.ts +++ b/apps/kimi-code/test/cli/run-prompt.test.ts @@ -210,6 +210,7 @@ describe('runPrompt', () => { permission: 'auto', additionalDirs: undefined, drainAgentTasksOnStop: true, + drainProcessTasksOnStop: true, }); expect(mocks.session.setPermission).not.toHaveBeenCalled(); expect(mocks.session.setApprovalHandler).toHaveBeenCalledWith(expect.any(Function)); @@ -336,6 +337,7 @@ describe('runPrompt', () => { permission: 'auto', additionalDirs: undefined, drainAgentTasksOnStop: true, + drainProcessTasksOnStop: true, }); expect(mocks.initializeTelemetry).toHaveBeenCalledWith( expect.objectContaining({ model: 'kimi-code/k2.5' }), @@ -354,6 +356,7 @@ describe('runPrompt', () => { permission: 'auto', additionalDirs: ['../shared', '/tmp/extra'], drainAgentTasksOnStop: true, + drainProcessTasksOnStop: true, }); }); diff --git a/docs/en/configuration/config-files.md b/docs/en/configuration/config-files.md index 0d4e57a18..40e3438d1 100644 --- a/docs/en/configuration/config-files.md +++ b/docs/en/configuration/config-files.md @@ -193,12 +193,12 @@ You can also switch models temporarily without touching the config file — by s | Field | Type | Default | Description | | --- | --- | --- | --- | | `max_running_tasks` | `integer` | — | Maximum number of background tasks running concurrently | -| `keep_alive_on_exit` | `boolean` | `false` | Whether to keep still-running background tasks when the session closes. By default, Kimi Code requests that all background tasks stop before the process exits; set this to `true` only when you want tasks to outlive the session. In print mode (`kimi -p`), setting this to `true` also makes the process wait for all background tasks to finish before exiting, so background subagents can complete their work | -| `print_wait_ceiling_s` | `integer` | `3600` | In print mode (`kimi -p`) with `keep_alive_on_exit = true`, the maximum number of seconds the process waits for background tasks to finish after the main agent's turn ends. Has no effect outside print mode or when `keep_alive_on_exit` is `false` | +| `keep_alive_on_exit` | `boolean` | `false` | Whether to keep still-running background tasks when the session closes. By default, Kimi Code requests that all background tasks stop before the process exits; set this to `true` only when you want tasks to outlive the session. In print mode (`kimi -p`), setting this to `true` also makes the process wait for all background tasks to finish before exiting, so background subagents and bash can complete their work | +| `print_wait_ceiling_s` | `integer` | `3600` | In print mode (`kimi -p`), the maximum number of seconds Kimi Code waits for background tasks to finish. It bounds both the end-of-turn drain of background subagents and bash (so the agent can react to their results) and, when `keep_alive_on_exit = true`, the additional wait before the process exits. Has no effect outside print mode | `keep_alive_on_exit` can be overridden by the `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` environment variable, which takes higher priority than `config.toml`. -In print mode (`kimi -p ""`), Kimi Code runs a single non-interactive turn and exits as soon as the main agent finishes. If you launch background tasks (for example, concurrent subagents via `Agent(run_in_background=true)`) and need them to run to completion, set `keep_alive_on_exit = true`: the process then waits for every background task to reach a terminal state before exiting, bounded by `print_wait_ceiling_s`. Without it, the single turn ending tears background tasks down with the process. +In print mode (`kimi -p ""`), Kimi Code runs a single non-interactive turn. Before the turn ends, it holds the turn open to drain any still-running background subagents and bash, delivering their completions so the agent can react to the results; this drain is bounded by `print_wait_ceiling_s`. If you also need tasks to outlive the turn (for example, long-running subagents launched via `Agent(run_in_background=true)`), set `keep_alive_on_exit = true`: the process then waits for every background task to reach a terminal state before exiting, again bounded by `print_wait_ceiling_s`. Otherwise, any background task still running when the run exits is torn down with the process.