Skip to content

feat(agent): coalesce queued follow-ups into one run - #871

Merged
huichen merged 1 commit into
mainfrom
feat/coalesce-followups
Sep 26, 2026
Merged

huichen merged 1 commit into
mainfrom
feat/coalesce-followups

Conversation

@huichen

@huichen huichen commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Restores the follow-up coalescing that #637 removed, behind an opt-in enqueue_coalescing busy policy so only callers that can absorb a folded run pay for it.

Why

A submission sent while a turn is running is usually a supplement to the one before it. Answering each fragment in its own run makes the model reply to a half-stated request. The same is true of the loop's supervisor outbox: those batches are state syncs, not questions, so a busy orchestrator turning each into its own run (and having most of them superseded) is pure waste — one measured goal ran 26 queued orchestrator runs back-to-back after a 2h turn, 25 of them cancelled.

What

Agent

  • BusyPolicy::EnqueueCoalescing: queues like enqueue_if_busy, but the run boundary may fold a consecutive run of such requests into ONE run.
  • InMemoryRunQueue::drain_coalescing_after_first takes that prefix atomically. Folding stops at the first request that did not opt in (so a client that streams its own run keeps its own run, in order), and at the merged-payload byte cap — folding happens after admission, so this is the only bound on the combined size.
  • Folded requests never run: they leave the queue, release their quota, and are recorded terminal with reason merged.
  • start_next_scheduled merges message + model-context sidecar + images + attachments in queue order (nothing dropped, nothing reordered), keeps the front request's run identity, and takes the execution configuration (model, thinking level, cwd, permissions, sandbox) from the LAST folded request — the user's latest instruction decides how the combined turn runs. That is the behaviour fix: harden agent, desktop, and mobile reliability #637 objected to (it reused the front's settings for later requests), and it no longer happens. The accepted user message records coalesced_run_ids.

TUI — submits with the coalescing policy and renders a folded submission as merged into the previous run (RunState::Merged) instead of leaving it stuck at queued (#n); a merged reason is mapped before the plain terminal state on refresh.

Loop — supervision::flush delivers its batches with the coalescing policy, so batches that queue behind a busy orchestrator fold into one reconciliation turn. A batch arriving while the orchestrator is idle still runs immediately.

Deliberately unchanged — CLI --follow-up, the IM bridges (Feishu/DingTalk) and Desktop/Mobile keep enqueue_if_busy: they stream their own run and would wait forever for an agent_end that a folded run never produces. Desktop and Mobile additionally block submission while a reply streams (stop first, then send), so they have nothing to coalesce anyway.

Verification

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test for future-agent, future-tui, future-loop, future-rpc.
  • New tests: queue-level fold boundaries (opt-in front, non-opt-in stop, byte cap, quota release), end-to-end fold (one user message "second question\n\nthird question", merged receipts, coalesced_run_ids metadata), last-settings-wins (falsified: it fails when the front's snapshot is used), TUI merged rendering + refresh mapping, and the outbox's wire policy.
  • Proto regenerated (REGENERATE_PROTO=1); only the field-144 comment changed.

Known limitation

The fold drains before the run is activated, so if activation fails (inaccessible workspace, lost lease race) the folded requests are gone with the front request that is cancelled in the same arm. This matches the pre-#637 shape and the window is narrow (persistence is checked earlier); it is documented at the call site.

Follow-up (not in this PR)

skills/builtin/future-loop/SKILL.md (separate submodule) can mention that a burst of reports may arrive as one prompt.

Restores the follow-up coalescing removed in #637, this time behind an
opt-in `enqueue_coalescing` busy policy so only callers that can absorb a
folded run opt in.

Agent:
- `BusyPolicy::EnqueueCoalescing` queues like `enqueue_if_busy` but lets
  the run boundary fold a consecutive run of such requests into one.
- `InMemoryRunQueue::drain_coalescing_after_first` takes that prefix
  atomically: the front must itself have opted in, folding stops at the
  first request that did not (order and identity preserved) and at the
  merged-payload byte cap; folded requests leave the queue, release their
  quota and are recorded terminal with reason `merged`.
- `start_next_scheduled` merges text, model-context sidecar, images and
  attachments in queue order (nothing dropped, nothing reordered), keeps
  the front request's run identity, and takes the EXECUTION configuration
  (model, thinking, cwd, permissions, sandbox) from the LAST folded
  request, so #637's "front config silently reused for later requests"
  cannot happen. The accepted message carries `coalesced_run_ids`.

TUI: submits with the coalescing policy, and renders a folded submission
as `merged into the previous run` (`RunState::Merged`) instead of leaving
it at `queued (#n)`.

Loop: the supervisor outbox delivers batches with the coalescing policy —
batches are fire-and-forget state syncs, so the ones that queue behind a
busy orchestrator fold into one reconciliation run instead of each
starting and being superseded on its own.

CLI `--follow-up`, the IM bridges and Desktop/Mobile keep plain
`enqueue_if_busy`: they stream their own run and would stall on a merged
ack they never receive.
@huichen
huichen enabled auto-merge (squash) September 26, 2026 08:24
@huichen
huichen merged commit 4c97937 into main Sep 26, 2026
12 checks passed
@huichen
huichen deleted the feat/coalesce-followups branch September 26, 2026 08:33
huichen added a commit that referenced this pull request Sep 26, 2026
Picks up future-skills#60, which documents the coalesced supervisor
delivery from #871 (busy orchestrator folds queued batches into one
turn) and the per-batch note cap. The bump also carries the three
commits main already had (future-slides research decks, builtin lookup
helper fixes, the code-development skill), so the pin is current.

skills offline checks pass on the bumped content (builtin structure,
27 unit tests, experimental-design, slides, orchestration, deep-research).
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