Skip to content

feat: hold print-mode turn until background subagents drain#1371

Merged
sailist merged 5 commits into
MoonshotAI:mainfrom
sailist:feat/print-drain-subagents
Jul 4, 2026
Merged

feat: hold print-mode turn until background subagents drain#1371
sailist merged 5 commits into
MoonshotAI:mainfrom
sailist:feat/print-drain-subagents

Conversation

@sailist

@sailist sailist commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No tracking issue. This fixes a class of failures where an agent that coordinates many background subagents in kimi -p ended without producing a final result.

Problem

In kimi -p (print mode), the main agent can end its turn while background subagents (run_in_background=true) are still running. The print flow then drained those tasks with their completion notifications suppressed, so the main agent never received the results and the run exited with the subagents' work abandoned. The "completion arrives in a later turn" contract only holds for interactive sessions; in print mode there was no later turn, so the main agent could not react to the subagents' results and the run produced nothing.

What changed

  • Hold the main turn open when the main agent ends a turn while kind === 'agent' background tasks are still running. The turn idle-waits (no extra model calls) until they finish, then flushes their completions into the turn so the model gets one wrap-up step to react before the run exits.
  • Gated on a new drainAgentTasksOnStop session option set by the print flow, bounded by background.printWaitCeilingS, and re-enumerates active tasks so backfill / fan-out is handled.
  • Only kind === 'agent' background tasks trigger the hold; process / question tasks and non-print modes are unaffected, and the existing post-turn drain still handles those after the turn as before.
  • Added tests for the drain primitive, the turn-level hold (including the agent-only scoping), and the session wiring.

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.

sailist added 2 commits July 4, 2026 18:09
In `kimi -p` (print mode), when the main agent ends a turn while background
subagents (`kind === 'agent'`) are still running, hold the turn open and
idle-wait until they finish, flushing their completions into the turn so the
model can react before the run exits.

Previously, the main agent could end its turn after launching background
subagents; the print flow then drained them with their completion
notifications suppressed, so the main agent never saw the results and the run
exited with the work abandoned (e.g. no nomination). This was the root cause
of the swarm-alpha-mining eval failures.

The hold is gated on a new `drainAgentTasksOnStop` session option (set by the
print flow), only affects `kind === 'agent'` background tasks, and is bounded
by `background.printWaitCeilingS`. Backfill / fan-out is handled by
re-enumerating active tasks. Other background task kinds and non-print modes
are unaffected.
@changeset-bot

changeset-bot Bot commented Jul 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 54bb19d

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 Minor

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 4, 2026

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

commit: 54bb19d

@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: 5d565f3fee

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

model,
permission: 'auto',
additionalDirs: opts.addDirs?.length ? opts.addDirs : undefined,
drainAgentTasksOnStop: true,

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 Enable print drain for resumed sessions

In kimi -p -r <id> or kimi -p --continue, the branches above call resumeSession and never set this new option (there is no resume-side equivalent), so resumed main agents keep printDrainAgentTasksOnStop false. A resumed print turn can still launch Agent(run_in_background=true) and then exit before those completion notifications are folded into the turn, leaving the original print-mode failure unfixed for resumed sessions. Please thread the drain flag through the resume/continue path or enable it on the resumed main agent for print mode.

Useful? React with 👍 / 👎.

{ timeoutMs: remaining, signal },
);
this.flushSteerBuffer();
return { continue: true };

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 Respect the max-step cap before wrap-up

When loopControl.maxStepsPerTurn is already exhausted by the step that stops while background agents are still running, this unconditional continuation asks runTurn for another step; the loop checks maxSteps at the top of the next iteration and throws max_steps, so a bounded print run that should complete after waiting for subagents fails instead. The goal continuation below already guards with hasStepBudgetRemaining; the drain continuation needs the same check or should skip the wrap-up when no step budget remains.

Useful? React with 👍 / 👎.

@sailist sailist merged commit 5394fea into MoonshotAI:main Jul 4, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 4, 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