Skip to content

feat(background): add print_background_mode steer for multi-turn -p runs - #1497

Merged
sailist merged 2 commits into
mainfrom
fix/background-bash
Jul 12, 2026
Merged

feat(background): add print_background_mode steer for multi-turn -p runs#1497
sailist merged 2 commits into
mainfrom
fix/background-bash

Conversation

@sailist

@sailist sailist commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

Problem

In kimi -p (print) mode, the main agent's run finished after a single turn.
The only knob for background tasks was background.keep_alive_on_exit: when
true, the driver suppressed terminal notifications and drained every
background task before exiting. That gives the main agent no chance to react
to a background result — a completion could not turn.steer the main agent
into a follow-up turn the way it does for background subagents. So multi-turn
-p runs driven by background-task completions were not possible.

What changed

Added an explicit background.print_background_mode policy and taught the
print driver to follow the main agent across turns.

1. New print-mode background policy (agent-core)

What was done:

  • Added [background].print_background_mode ('exit' | 'drain' | 'steer') and
    print_max_turns to BackgroundConfigSchema.
  • Added Session.resolvePrintBackgroundMode(): print_background_mode is
    authoritative; when unset it falls back to the legacy keep_alive_on_exit
    mapping (true ⇒ 'drain', otherwise 'exit'), so existing configs keep
    their behavior.
  • waitForBackgroundTasksOnPrint() now only runs under the 'drain' mode.
  • Added Session.handlePrintMainTurnCompleted() returning 'finish' | 'continue':
    • 'exit' → finish immediately (default);
    • 'drain' → suppress + drain, then finish;
    • 'steer' → return 'continue' while background tasks are still pending so a
      completion can turn.steer the main agent into a new turn; finish once
      quiescent, or when print_wait_ceiling_s / print_max_turns is hit.

2. Print driver follows every main turn (apps/kimi-code)

What was done:

  • runPromptTurn no longer maps only the first turn.started; it tracks the
    main agent across subsequent turns so steered turns render too.
  • On turn.ended with reason === 'completed', it flushes the buffered
    assistant message, then calls handlePrintMainTurnCompleted() and defers
    finish() when the action is 'continue'.

3. SDK plumbing, docs, and tests

What was done:

  • Plumbed handlePrintMainTurnCompleted through node-sdk RPC and Session.
  • Updated docs/en and docs/zh config references and release notes.
  • Added coverage in apps/kimi-code/test/cli/run-prompt.test.ts and
    packages/agent-core/test/session/lifecycle-hooks.test.ts.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a5d9a6b

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 8, 2026

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

commit: a5d9a6b

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

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +575 to +576
if (this.countActiveBackgroundTasks() > 0) {
return 'continue';

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 Enforce the steer wait ceiling while tasks are pending

When print_background_mode = "steer" and a background task is still running after the main turn, this returns continue immediately. The print driver only calls handlePrintMainTurnCompleted() again after another main turn.ended, so a hung or long-running background task produces no wake-up and the deadline checked above is never re-evaluated; kimi -p can therefore hang past print_wait_ceiling_s instead of being bounded as documented.

Useful? React with 👍 / 👎.

sailist added 2 commits July 11, 2026 23:20
… -p runs

- add `[background].print_background_mode` (`exit`/`drain`/`steer`) and
  `print_max_turns`; when unset, falls back to `keep_alive_on_exit = true`
  mapping to `drain`, preserving existing behavior
- core: `Session.handlePrintMainTurnCompleted()` returns `finish`/`continue`;
  in `steer` mode the run stays alive so a background-task completion
  `turn.steer`s the main agent into a new turn (matching background
  subagents), bounded by `print_wait_ceiling_s` and `print_max_turns`
- cli: print driver follows every main turn instead of only the first and
  defers `finish()` until the run quiesces or a limit is hit
- plumb `handlePrintMainTurnCompleted` through node-sdk RPC; docs + tests
@sailist
sailist force-pushed the fix/background-bash branch from 6721c05 to a5d9a6b Compare July 11, 2026 15:21
@sailist
sailist merged commit c6e02da into main Jul 12, 2026
10 checks passed
@sailist
sailist deleted the fix/background-bash branch July 12, 2026 02:47
@github-actions github-actions Bot mentioned this pull request Jul 12, 2026
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.

1 participant