feat: hold print-mode turn until background subagents drain#1371
Conversation
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 detectedLatest commit: 54bb19d 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 |
commit: |
There was a problem hiding this comment.
💡 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, |
There was a problem hiding this comment.
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 }; |
There was a problem hiding this comment.
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 👍 / 👎.
Related Issue
No tracking issue. This fixes a class of failures where an agent that coordinates many background subagents in
kimi -pended 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
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.drainAgentTasksOnStopsession option set by the print flow, bounded bybackground.printWaitCeilingS, and re-enumerates active tasks so backfill / fan-out is handled.kind === 'agent'background tasks trigger the hold;process/questiontasks and non-print modes are unaffected, and the existing post-turn drain still handles those after the turn as before.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.