Skip to content

feat(agent-core-v2): deliver queued prompts to the model in one batched turn - #4052

Open
mike-lmctl wants to merge 1 commit into
MoonshotAI:mainfrom
mike-lmctl:kimifix/batch-queued-prompts-upstream
Open

mike-lmctl wants to merge 1 commit into
MoonshotAI:mainfrom
mike-lmctl:kimifix/batch-queued-prompts-upstream

Conversation

@mike-lmctl

Copy link
Copy Markdown

Requirement or Bug

Related: #4051 — prompts submitted while a turn is running are delivered strictly one turn per prompt (FIFO); this change delivers all prompts queued at a turn boundary together in a single turn.

Note: this is a behavior enhancement, filed alongside issue #4051 per the contribution policy (external feature PRs need maintainer approval; offered here as a tested reference implementation).

Bug Reproduction Steps

N/A (enhancement; current vs proposed behavior is described in #4051).

Root Cause

N/A. Mechanically: at a turn boundary the agent machine drained only the queue head into the next turn (drainPendingPatch in packages/agent-core-v2/src/human/agent/machine.ts), so each queued prompt got its own turn.

Code Changes

All changes are in @moonshot-ai/agent-core-v2:

  • src/human/agent/machine.ts: at a turn boundary the machine now drains the whole queue into the next turn (notifications first, then all queued entries, FIFO). The prompt gate evaluates the entire queue snapshot sequentially with per-item outcomes: blocked items emit prompt.blocked and drop, gate errors emit prompt.gate_failed and drop, rewrites apply per item, and items arriving during gating are kept for the next batch (the stale-gate guard re-gates when a gated item vanished). An actor-level gate failure now fails every queued item and clears the queue instead of potentially re-entering gating forever. The emitted turn.started event gains entries (all drained queue entries); queueItemId/entry still carry the first entry, and the store journal schema is unchanged.
  • src/agent/loop/machine/engine.ts: passes entries through on turnStarted.
  • src/agent/loop/loopService.ts: secondary (batched) prompts bind to the shared turn — their handles launch with it, each tracked prompt gets its own started/completed events, and completions resolve from the shared turn result. If the batch head was cancelled before binding, the first entry with a live waiter becomes the primary. Cancelling any batched prompt cancels the shared turn. Single-prompt, idle-submit, and pure-notification paths are unchanged.

Impact Scope

  • Prompt delivery when multiple prompts queue during a running turn (TUI typing ahead, API/ACP clients submitting follow-ups): one turn instead of n.
  • No changes to the store journal schema, the klient/kap-server contracts, or public APIs.
  • Tests: new/updated coverage in the existing machine, loop, prompt-service, compaction, and usage test files (multi-prompt drain order, per-item gate block/fail/rewrite, arrivals during gating, shared-turn launch/completion, cancel-by-batched-prompt, cancelled-head promotion). Full @moonshot-ai/agent-core-v2 suite passes: 6,878 tests.

Checklist

…ed turn

When a turn ends with several prompts queued, the harness now drains the
whole queue into the next turn instead of starting one turn per prompt:
the model receives all queued user messages at once, in FIFO order, each
still a separate user message. Single-prompt and idle-submit behavior is
unchanged.

The prompt gate now evaluates the whole queue snapshot sequentially with
per-item block/fail/rewrite; blocked or failed items are dropped
individually and arrivals during gating are kept for the next batch.
Secondary prompts bind to the shared turn: their handles launch with it,
each tracked prompt gets its own started/completed events, and
cancellation of any batched prompt cancels the shared turn.
@changeset-bot

changeset-bot Bot commented Sep 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7aaef76

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

This branch has not been deployed

No deployments
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