Skip to content

fix(chat): skip sub-agent synthesis when one turn reported the batch - #17307

Merged
iamwhatever merged 1 commit into
mainfrom
fix/synthesis-single-subagent
Oct 6, 2026
Merged

iamwhatever merged 1 commit into
mainfrom
fix/synthesis-single-subagent

Conversation

@patrigao

@patrigao patrigao commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A lone sub-agent, or a batch whose results arrive in one turn, ends with that turn's reply instead of a second turn that restates it.

When a session spawns one background sub-agent, its result arrives as its own [Subagent completion event] turn, and the agent reports there. That sub-agent was also the last one running, so the gateway arms the post-fan-out synthesis, that is, it schedules one extra turn to run once the session's queued turns are used up. When the queue drains (each queued message is taken off and run in order) and nothing is left, that second turn runs with [SYSTEM] Sub-agent synthesis: ... (1) restate the original goal .... Nothing new arrived between the two turns, so the reply typically opens with "This restates my last message; nothing new has come in since." and repeats the report the user just read.

Why it matters

Long-running monitor and loop sessions spawn one work session at a time and report each result on its completion turn. Every result then costs an extra model turn and posts a duplicate message. A spawn_run wave whose results land in one digest turn hits the same duplicate.

Not a goal

What changed (motivation → approach → change)

Batch Before After
One sub-agent completion turn + synthesis turn completion turn only
One wave digest (several members, one turn) digest turn + synthesis turn digest turn only
Results in two or more completion turns one synthesis turn unchanged

The gateway arms the synthesis when the last outstanding sub-agent completes and fires it once the queue drains, but nothing records how many turns carried the batch. The synthesis only adds something when the results were spread over two or more turns.

The gateway now counts each completion it queues or launches into the session's chat slot (_ChatSlot._synthesis_completion_turns, set in slack/gateway.py). The queue drain never merges completions, so each counted completion is one turn. A wave digest is a single queued completion, so it counts once. In dashboard/chat_runner.py, the turn-end fire decision (_finish_queue_cycle) drops an arm that is otherwise ready to fire when the count is exactly 1. Any other count fires as before, including 0, and the store-outage re-check fires exactly as on main. Dropping happens only at fire time, so every disarm path still disarms exactly when it does today.

The count resets when the synthesis runs or is dropped, and when a user message drained at a turn's end disarms an armed synthesis while no sub-agent of the batch is running, delivering, pending in memory or queued. The turn-end hand-off (_hand_off_queue) asks the same synthesis_fire_verdict the fire decision uses, while the ending turn still holds the slot, so no turn can start during the read. It passes the answer to the drain (_start_next_queued_turn), which reads no store and dequeues exactly as on main. Every other drain, such as Run now on a queued message, keeps the count: at worst that costs one synthesis main also runs, never one a multi-turn batch should get. The subagent.md and injected-messages.md specs describe the rule.

Backwards compatibility

Compatible: nothing main accepts is rejected or renamed; the only change is one fewer automatic turn when exactly one completion turn carried the batch, and every other arm fires as today.

Removes nothing: no key, record, validator or public function goes away, and the synthesis turn still runs for every batch reported across two or more turns.

Tests

  • test/test_slack_gateway.py TestSynthesisAfterAOneTurnBatch routes completions through the real on_done into a real slot and ends each turn through _finish_queue_cycle. A lone completion gets no synthesis, both when it launches a turn on an idle chat and when it is queued behind a busy one, and so does a two-member wave digest. A user message drained at the end of the report turn disarms it, and the next lone completion gets none either. These four fail on main. Two completions in separate turns still get one synthesis, and so does a batch that a user message ran beside, both before the arm and while a lone completion's follow-up sub-agents were still running; these pass on main too, as unchanged behaviour should.
  • test/test_chat_runner_coverage.py: a count of 1 drops the arm at turn end, while the store-outage re-check launches the synthesis whatever the count. A user message drained at a turn's end clears the count only when no child is running, delivering, pending in memory or queued in the store, and keeps it when that cannot be read. The hand-off reads the fire gate once, while the ending turn holds the slot; a direct drain and an unarmed hand-off read no store. Running the synthesis, or a user message that runs in its place, resets the count.
  • The existing arm, fire, re-check and disarm tests pass unchanged.

Manual verification

N/A — unit coverage sufficient: the change is a counter on the delivery path, one check at the turn-end fire decision and one fire-gate read at the turn-end hand-off while a synthesis is armed, and the tests drive each through the real code paths.

Related Issues

Related: #13012 (the synthesis prompt presumes success).

Pattern harvest

Not generalizable: the arm counted outstanding sub-agents but not the turns that carried their results, a gap specific to this one post-fan-out turn.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Fixes #17274

@patrigao
patrigao requested a review from a team as a code owner October 6, 2026 09:33
@patrigao
patrigao requested a review from iamwhatever October 6, 2026 09:33
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Intent: When one turn already reported a sub-agent batch (a lone sub-agent, or a wave whose results arrive in one digest turn), end there instead of running a follow-up synthesis turn that only restates that report. Batches whose results arrive over two or more turns keep their single synthesis.
Not a goal: Changing the synthesis prompt or what a synthesis says, changing when the gateway arms the synthesis, or changing any existing disarm path (a drained user message, results collected inline by spawn_sub_agents, stop/close, the spec-builder stop). An occasional extra synthesis where today's code also runs one is accepted; skipping one that a multi-turn batch should get is not.

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of d2b6c98257c06ef287a89c2ca65622bc2428d2ad — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that _synthesis_completion_turns can't go stale across batches when a disarm path other than the user-message drain keeps the count (for example, results collected inline by spawn_sub_agents). The stale value should only ever cost one extra synthesis, never suppress a needed one.

[DESIGN-REVIEWED] d2b6c98

Description read

[DESCRIPTION-READ] e738902c125c6e163b6e095296f30d886cdd8ec5dcc1aaf8e4912e4caf41cc7b

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🟡 CONCERNS

Premise-level review of d2b6c98257c06ef287a89c2ca65622bc2428d2ad — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The user-message end_batch count-reset path (a new store read in _hand_off_queue, a new end_batch parameter and a three-way caller split) is oversized for a corner case where the zero option is main's behaviour.

Not justified as shipped

  • Item 4 — oversized: the description says "Any other count fires as before", and the diff's own comment says "A stale count costs at most one extra synthesis". Yet the diff adds end_batch to _start_next_queued_turn and a new synthesis_fire_verdict await in _hand_off_queue on every armed turn-end that drains a queue.
    Subtraction: delete the end_batch parameter and the _hand_off_queue read. Zero _synthesis_completion_turns only where the synthesis runs or is dropped, and drop the "user message ends the batch" tests and spec paragraph. A stale count then yields the extra synthesis that main already runs.
    Clears when: the end_batch path is removed, or a failing-on-main case shows the stale count produces something worse than main's single extra synthesis.
Inventory (5 items) — 4 justified

Intent: a lone sub-agent, or a batch reported in one turn, ends with that turn's reply instead of a restating synthesis turn. This is a FIX (#17274).

  1. A lone sub-agent completion or a one-turn wave digest no longer triggers a synthesis turn — justified
  2. The gateway counts completion turns per chat slot (_synthesis_completion_turns) — justified
  3. The count resets when the synthesis runs or is dropped — justified
  4. A user message drained at turn end zeroes the count after an extra store read — oversized
  5. The subagent.md and injected-messages.md specs describe the rule — justified

[FIRST-PRINCIPLES-REVIEWED] d2b6c98

Description read

[DESCRIPTION-READ] 2751ef8d9d738b690127c7cc6ef6d2b1d1e8fa0d2b68a030bbcf0f93dd84a24a

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed d2b6c98257c06ef287a89c2ca65622bc2428d2ad — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] d2b6c98

Verdict parsed from the review's SHA-scoped output markers for commit d2b6c98257c06ef287a89c2ca65622bc2428d2ad.

False positive or not applicable? A repository writer can comment:
/ai-review override fable d2b6c98257c06ef287a89c2ca65622bc2428d2ad: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of d2b6c98257c06ef287a89c2ca65622bc2428d2ad and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] d2b6c98

False positive or not applicable? A repository writer can comment:
/ai-review override gpt d2b6c98257c06ef287a89c2ca65622bc2428d2ad: <one-sentence reason>

@patrigao
patrigao force-pushed the fix/synthesis-single-subagent branch from 213dc67 to 368cc15 Compare October 6, 2026 09:53
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in test/test_session_control_owner_dm.py since this head's CI started (5edf2fe -> cc7e798). That commit (#17309) fixes the test_source_pin_budget failure on Backend Tests (Windows) (3): the over-budget module was test/test_session_control_owner_dm.py, which this PR does not touch, and the same test fails on 5edf2fe alone. CI re-running on 368cc15; the code diff is unchanged.

@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author
  • span=4d163aa29d6d: reset _synthesis_completion_turns at batch start instead of at each end path — rebutted (disproportional; no regression)

A count left behind by the inline-collected disarm (gateway.py ~9571/9614/9663), stop/close or the spec-builder stop (runtime.py:256) can only push the next lone completion's count to >= 2, which fires a synthesis exactly as origin/main does today. It never drops a synthesis a multi-turn batch should get, the one outcome the intent comment rules out.
A batch-start reset is new behaviour on the spawn path: no spawn-time code touches the slot's synthesis state today, so it would have to hang off the manager's on_event frames (_subagent_event) and decide "no child out" at spawn time. That decision has the same visibility limits as the drain-time check (a store-held, not-yet-published child), now on a second path.
Resetting at those disarm sites instead would reopen the lost-synthesis case the drain-time check guards (async children can still be out when inline collection or a stop clears the arm), so they stay unchanged in this PR.

@patrigao
patrigao force-pushed the fix/synthesis-single-subagent branch from 368cc15 to d0296c3 Compare October 6, 2026 10:32
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in src/kiro_crew/dashboard/chat_runner.py (plus same-dir specs and an importing test) since this head went green (cc7e798 -> b13cebd, #16788 _expand_dollar_skills skill-card hunk, no overlap with the synthesis code); CI re-running. Clean rebase, 1242 overlapping tests pass locally. New head d0296c3.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 6, 2026
@patrigao
patrigao force-pushed the fix/synthesis-single-subagent branch from d0296c3 to e843a7c Compare October 6, 2026 11:06
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased: main moved near this PR's specs and in tests importing its modules (docs/system-specs/modules/{computer-use,security}.md, test/test_mcp_computer.py, test/test_security*.py) since the last head (b13cebd -> 4bb0bdf, #15386); CI re-running. Clean rebase, 1796 overlapping tests pass locally. New head e843a7c. The previous head's GPT/Opus/UX reds were a GitHub App installation API rate limit (HTTP 403 reading PR comments), not review verdicts.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026
@patrigao
patrigao force-pushed the fix/synthesis-single-subagent branch from e843a7c to 9733ead Compare October 6, 2026 12:11
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: drain reads synthesis_fire_verdict once before dequeue; _batch_children_settled removed

  • span=f13bf245e1af: fixed in 9733ead — _batch_children_settled deleted; the drain reset uses the shared synthesis_fire_verdict gate

_start_next_queued_turn now reads synthesis_fire_verdict once, only when _pending_synthesis and a queued entry exist, and zeroes _synthesis_completion_turns only on SYNTHESIS_CLEAR; the duplicate settledness helper is gone, so the drain and the fire gate judge the batch by one rule.
The read sits before any dequeue decision, not between dequeue and dispatch as the Subtraction sketched: an await there lets an idle-slot completion take the floor and both turns dispatch. The pre-dequeue purges (_purge_superseded_continuations, _drop_revoked_replays) never await, so they stay atomic after the read.
The drain captures owner = slot.task before the await and returns False with the entry still queued if another turn took the slot meanwhile (same idiom as _finish_queue_cycle).
Using the shared gate also counts store-held queued children (queued_count_or_none_async) that the deleted helper missed, and an unreadable store reads as not-clear, so the count is kept.
Tests: verdict read before the dequeue, a turn taking the floor during the read keeps the entry queued, unarmed drains read no store, plus queued-in-store and store-unreadable cases; 1246 scoped tests pass.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026
@patrigao
patrigao force-pushed the fix/synthesis-single-subagent branch from 9733ead to f9057de Compare October 6, 2026 13:45
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: drain fire-gate read moved from _start_next_queued_turn to _hand_off_queue (end_batch keyword)

  • span=93adabc37a34: fixed in f9057de — the drain no longer awaits

_start_next_queued_turn is back to origin/main's await-free shape: the pre-dequeue synthesis_fire_verdict read, the owner capture and its return False are deleted. It takes a keyword-only end_batch and zeroes _synthesis_completion_turns only when that is set.
The one read moved to _hand_off_queue, taken only while a synthesis is armed and a message is queued, before it calls the drain. The ending turn still holds slot.task there (released only by _finish_queue_cycle), so no turn can start during the await.
Run now, Continue, rewind, regenerate and the parked drain call the drain with the default, so they read no store and cannot 409 queue_item_unavailable from this code. They keep the count, which costs at most a synthesis origin/main also runs.
_run_pending_synthesis passes end_batch=True: both of its launchers fire only on a CLEAR verdict.
Tests: an armed direct drain reads no store and keeps the count; the hand-off reads once while the ending turn holds the floor; three tests fail with that read removed; 1246 scoped tests pass.

@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: drain await, owner guard and early return removed; outage re-check drop and its extra chat_done removed (retrospective subtraction)

  • span=b1e26121cf65: fixed in f9057de — the drain's await and early return are gone

_start_next_queued_turn no longer reads the store, captures owner or returns False early; its dequeue is origin/main's again. The single synthesis_fire_verdict read sits in _hand_off_queue, armed only, where the ending turn holds slot.task, so no owner guard is needed.
The read stays the shared fire gate rather than the arm's in-memory checks. A child held only in the store is invisible in memory: a1 runs, a2 waits queued in the store, the user types during a1's report, the count resets, and a2's lone completion is dropped, losing a synthesis origin/main runs.
A retrospective over the PR's rounds subtracted the outage re-check drop and its second chat_done: _arm_synthesis_recheck is byte-identical to origin/main, so the re-check (reached only after an UNKNOWN verdict) fires as main does.
Tests: a direct drain reads no store and keeps the count; the hand-off reads once with the floor held; the re-check launches the synthesis whatever the count; 1246 scoped tests pass.

@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author
  • span=d2f04d0d8797: reset the count at batch start — rebutted (it can drop a synthesis origin/main runs)

A batch-start reset re-cuts one batch into two. Example: a lone child a1 completes, and its report turn spawns b1 into a slot that now has no running children. The count is zeroed, so b1's completion counts 1 and is dropped. origin/main keeps the arm set through b1 (the verdict is HELD at a1's turn end) and fires one synthesis covering a1 and b1. The intent rules this outcome out.
It also adds a new writer of slot synthesis state on the spawn path. Today every writer is in gateway on_done, chat_runner or the spec-builder stop. Its "no outstanding children" check either reads the store on the spawn path or misses children queued only in the store.
The drain becomes synchronous anyway: the one store read moves to _hand_off_queue, where the ending turn still holds slot.task (fix for span 93adabc37a34). This suggestion is therefore not needed to clear the Watch item. Disposition 6014121495 already rebutted the same idea under span 4d163aa29d6d.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026
After the last sub-agent of a parent completes, the gateway arms a one-shot
synthesis turn. When the whole batch reached the parent in a single
completion turn (one spawn_run, or one wave digest), that synthesis only
restates the reply the user just read.

The gateway now counts each completion it queues or launches into the slot
(_ChatSlot._synthesis_completion_turns). The turn-end fire decision in
_finish_queue_cycle drops a cleared arm when the count is exactly 1. Any
other count fires as before, and so does the store-outage re-check. The
count resets when the synthesis runs, when it is dropped, and when a user
message drained at a turn's end disarms an armed synthesis while no child
of the batch is running, delivering, pending in memory or queued:
_hand_off_queue reads synthesis_fire_verdict while the ending turn still
holds the slot and passes the answer to the drain as end_batch. The drain
itself reads no store, and every other drain keeps the count. The subagent
and injected-messages specs describe the rule.

Fixes #17274
@patrigao
patrigao force-pushed the fix/synthesis-single-subagent branch from f9057de to d2b6c98 Compare October 6, 2026 14:47
@patrigao

patrigao commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased: main moved to pick up the source-pin-budget fix (#17339) that turned test/test_source_pin_budget.py::test_a_budget_falls_with_the_reads_it_covers red on Backend Tests shard 3 (ea7e91c -> 3343351); the diff is unchanged and CI is re-running on d2b6c98.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The red 'Cross-Platform Portability' check is not a real defect. Job 112332176612 never got a runner: its runner name is empty, it has no steps, it sat queued on its run-scoped CodeBuild label from 14:47 to 15:38, and its only annotation is "The run was canceled by @patrigao". Its log blob returns BlobNotFound because no step ever ran, so nothing failed. The fix itself is small and safe: a count of 1 drops the arm only at fire time, and every other count, including 0 and a stale leftover, fires exactly as main does, so a multi-turn batch never loses its synthesis. Reading the fire gate in _hand_off_queue while the ending turn still holds slot.task keeps the drain free of awaits. Please rerun only that one job before merge so this head has a real portability result, even though readiness already shows passed.

@iamwhatever
iamwhatever enabled auto-merge (squash) October 6, 2026 16:42
@iamwhatever
iamwhatever merged commit 4a67cdc into main Oct 6, 2026
101 of 102 checks passed
@iamwhatever
iamwhatever deleted the fix/synthesis-single-subagent branch October 6, 2026 16:43
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 6, 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.

subagents: synthesis turn fires after a single sub-agent and restates the reply it already gave

2 participants