Skip to content

Mark Claude child-session assistant output as commentary at ingress - #401

Merged
j-asefa merged 17 commits into
mainfrom
loop/child-commentary
Nov 20, 2026
Merged

j-asefa merged 17 commits into
mainfrom
loop/child-commentary

Conversation

@j-asefa

@j-asefa j-asefa commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Defect

In src/adapters/claude.ts, the adapter only filtered child-session output at the turn-ending result message (foreignResult rejects a result carrying parent_tool_use_id). The assistant branch emitted { type: "item", item: { id, kind: "assistant", text } } for any message with text, including Task tool (subagent) session messages; the stream_event branch emitted delta events for child streams, using parent_tool_use_id only to keep per-stream message ids apart. The Claude adapter never set phase on an item, so on a completed status projectOutput (src/server/native/projector.ts) picks the last assistant item with non-empty text whose phase is not "commentary", writes it as the run's answer, and publishCompletedRunResult (src/server/result-publication.ts) seals it as an immutable Result. A subagent assistant message that is the last text before the parent's result (subagent speaks after the parent's final text, or a parent final message with no text) was therefore sealed as the Result body.

Fix

In the Claude adapter only:

  • Assistant messages whose envelope carries parent_tool_use_id are emitted with phase: "commentary", so the projector's existing selection rule (i.phase !== "commentary") excludes them. Codex and Muse already use this field the same way.
  • Delta path choice: I establish the commentary item before the child's first delta rather than dropping child deltas. A delta event carries no phase, and the projector creates a phase-less assistant item from an unknown delta id (projector.ts, the event.type === "delta" block); if the turn ends before the full child assistant message arrives, that phase-less item could be selected. Emitting the commentary item first (keyed by item id, because a stream's id can switch before message_start) makes the projector merge the deltas into a commentary item. This keeps the child's text visible as progress on the run exactly as today — dropping child deltas would hide the child's partial text precisely in the turn-ending case this fix targets.

foreignResult, the projector's selection rule and the other adapters are unchanged.

Review follow-up (revision 2, commit 62cb94efe)

The review team found that the first revision could overwrite saved child text: the projector replaces an item by id, so when a delta's establishing empty commentary item was emitted for an id the projector already held (full child assistant message arrived first, or the id was evicted after 128 others and the stream resumed), the earlier text was lost. Fix:

  • The assistant branch for child messages remembers the emitted id in childItems, so a later delta merges into the existing item instead of re-establishing an empty one over it.
  • The establishing emit happens only while a user turn is open (this.active), so a child message_start/delta arriving after the parent result cannot emit an empty item that clobbers saved native history (the projector drops post-result deltas).
  • The 128-entry eviction cap on childItems is removed, so a resumed stream cannot hit the overwrite through eviction. The set is bounded per turn and cleared at the turn's result.

Added regression merges a child delta into an earlier child assistant message instead of replacing its text (test/native-activity-ingress.test.ts). Against revision 1 it fails with the finding's symptom — retained text " and more" instead of "Full child text and more" — and passes on revision 2.

Review follow-up (revision 3, commit 400ca71ae)

The second review found two more concerns:

  1. Evicted child commentary in the stored-history fallback (P2). projectOutput's fallback (projector.ts) reads assistantHistory, whose SQL already excludes phase = 'commentary' (json_extract(item,'$.phase') IS NOT 'commentary'), and the in-memory pass already excluded anything still in the window, so an evicted child item cannot be selected. The JS fallback itself only re-checks "not in memory and nonblank" because the SQL carries the commentary exclusion; I added a comment in the projector recording that invariant. No behavior change.
  2. Child tracking can lose its phase across turns (P2, confirmed). A child assistant message received between turns adds its id to the adapter's child-id set; the next turn clears the projector's item window, so a delayed delta for that id skipped the establishing emit and the projector created a phase-less assistant fragment that could be selected as the new run's answer. Fixed by clearing the child-id set at the start of a new user turn (prompt() when no turn is active; mid-turn steering follow-ups keep the set), so a delayed delta re-establishes its commentary item in the fresh window.

Added regression keeps a delayed child stream delta commentary across turn boundaries (test/native-activity-ingress.test.ts). Against revision 2 it fails with the finding's symptom — the retained child-cross item has no phase — and passes on revision 3.

Review follow-up (revision 4, commit ead8891c4)

The third review found one confirmed P2 finding:

Child progress can be overwritten across turns. The supervisor attaches the next run before prompt(); a delayed full child message arriving in that recall window is retained by the projector and its id recorded by the adapter, then prompt() clears the id set (the revision-3 fix), so a later delta re-established an empty commentary item that replaced the retained text before appending the fragment — progress showed only the trailing fragment. Fixed by keeping the last-known text per child id across prompt() (cleared together with the id set at the turn's result, so it stays bounded per turn) and seeding the re-established item with it: the replacement becomes content-identical and the delta appends to the full text.

Updated keeps a delayed child stream delta commentary across turn boundaries to assert the carried-over between-turns text ("Child text two late" instead of " late"), and added does not overwrite a recall-window child message with a later delta's empty item. Both fail on revision 3 with the finding's symptom (retained text " and more" / " late") and pass on revision 4.

The recall-window delta (as opposed to full message) while the adapter is still inactive remains an unresolved concern (a phase-less item if the new run has no saved item for that id); the review classified it as a pre-existing gap rather than a regression, and whether a live Claude CLI emits that sequence is unverified. childItems/childText growth is bounded per turn (cleared at the turn's result).

Review follow-up (revision 5, commit 0d9149b5b)

The fourth review found two findings:

  1. Medium — A child delta can be lost across prompt dispatch. The recall seed cached only the last full assistant message, so a full message plus a delta both arriving before dispatch left the seed stale at the full text; the post-dispatch re-established item then replaced the projector's merged text and the first delta was lost (Full text + A + B became Full text B). Fixed by accumulating no-turn (recall/between-turns) deltas into the seed, so a later delta appends to everything seen so far (Full recall text A B).
  2. Low — Child output retained without a memory bound. The seed saved full text for every distinct child item until the result. Now the seed is written only while no turn is open (mid-turn entries are never read back, since those ids are in childItems and their deltas merge), and its total size is byte-capped at 4 MiB (evicting oldest; the projector truncates its own display at a similar scale). Beyond the cap an evicted id's re-establishment degrades to the fragment.

The recall-window test now also sends a delta before the prompt; against revision 4 it fails with the finding's symptom (retained text "Full recall text B") and passes on revision 5. The pre-existing unresolved concerns (recall-window delta while inactive creating a phase-less item — not an introduced regression; attribution of between-runs text to the next run) are unchanged and unverified against a live Claude CLI.

Review follow-up (revision 6, commit 85e93651d)

The fifth review found two P2 findings:

  1. A single child stream can grow the recall cache without bound. The eviction loop stopped with one entry left, so a long-lived single stream's accumulated seed entry could exceed the budget (and every delta recalculated its byte length). Each seed entry is now capped at the projector's display-preview limit (64,000 bytes, keeping the tail), so one stream is bounded regardless of total output; the total cap then always terminates.
  2. Cache eviction can erase retained child progress. After prompt() clears the id set, a delta for an evicted id re-established with an empty seed, replacing the projector's retained text. A never-evicting childSeen set now records ids whose commentary item was emitted while no turn was open; a post-prompt delta for an evicted-but-seen id emits only the delta, so the projector merges it into the retained commentary item instead of replacing it.

Added caps a single recall stream's cached seed at the display limit and merges a post-prompt delta for an evicted recall id instead of re-establishing over it; both fail on revision 5 (200,000-byte cached entry; an establishing item emitted for the evicted id) and pass on revision 6. The unresolved concerns (recall-window delta while inactive — a pre-existing path; between-runs text attribution) are unchanged and unverified against a live Claude CLI.

Review follow-up (revision 7, commit 17a21579e)

The sixth review found one P2 finding:

Evicted child-stream seeds can be published as the next run's answer. Skipping the establishing item for an id whose recall seed was evicted let a between-turns delta reach the projector alone: the next turn clears the item window, the projector's fallback looks up only the current run's saved item and otherwise creates a phase-less assistant item, and completion could select that child fragment as the answer.

The skip and the seed eviction are mutually incompatible (skip → phase-less selectable; eviction → empty seed erases retained text). Resolved by removing both: the commentary item is now established unconditionally for any active post-prompt child delta (phase is always safe), and the recall seed is never evicted (its text never degrades to empty). Each seed entry stays capped at the projector's display-preview limit (64,000 bytes, tail-kept), so a single long-lived stream cannot grow the cache without bound; memory is bounded per entry and only grows with the distinct child ids seen between turns (the same class the review previously listed as an unresolved concern).

Rewrote the eviction regression as still establishes a commentary item when a post-prompt delta's recall seed was lost; against revision 6 it fails with the finding's symptom (only the delta emitted — no commentary item, so the fresh window's item would be phase-less) and passes on revision 7. The unresolved concerns (recall-window first delta while the adapter is inactive — a pre-existing path; between-runs text attribution) are unchanged and unverified against a live Claude CLI.

Review follow-up (revision 8, commit d166196ae)

The seventh review found one P2 finding:

A delayed child delta can overwrite previously retained child text. Full child messages received during an active turn were not cached, and the result handler cleared the cache, so a delayed delta for that id in the next turn re-established with an empty seed and the projector's run-item upsert replaced the stored child text with the fragment. Now every child assistant message's text is cached (whether a turn is open or not) and kept across the result and prompt, so a re-established item carries the earlier text and the delta appends to it. Entries stay capped at the projector's display-preview limit (64,000 bytes, tail-kept); the entry count tracks the session's distinct child message ids, which the review team already listed as an unresolved concern (live volume unverified).

Added keeps an active-turn child message's text when a delayed delta arrives in the next turn (no between-turn message, unlike the earlier cross-turn test); against revision 7 it fails with the finding's symptom (retained text " more" — the earlier "Full active text" overwritten) and passes on revision 8. The unresolved concerns (recall-window first delta while the adapter is inactive — a pre-existing path; between-runs text attribution; aggregate cache count) are unchanged and unverified against a live Claude CLI.

Review follow-up (revision 9, commit 9c7a961e9)

The eighth review found two P2 findings:

  1. Delayed child deltas can erase earlier progress. Child deltas updated the recall seed only while no turn was open, so an active turn's delta-only stream was projected but never cached: a delayed delta for that id in the next turn re-established from an incomplete seed and the earlier streamed text was dropped (A plus B became A plus C). The seed is now kept current with every child delta, open turn or not, so a later re-established item carries everything the stream has said so far. Added keeps an active-turn child stream's text when a delayed delta arrives in the next turn; against revision 8 it fails with the finding's symptom (retained text "B" — the streamed "A" dropped) and passes on revision 9.
  2. A delayed child fragment moves earlier progress into the next run. A re-established item carries the cached earlier text, which the projector records as the current run's progress and whose native_items upsert re-points the row to the current run. This is the direct counterpart of the confirmed revision-7 requirement: preserving the earlier streamed text across a turn boundary requires the next run's item to carry it (reverting to an empty seed re-introduces the review-7 text loss and finding 1 above). Two facts keep the earlier attribution intact: conversation history stores per-run progress rows keyed by (owner_id, run_id, update_key) (messages), so run N's earlier child text remains attributed to run N, and native_items is by design the single latest-state projection per native id. Routing a post-prompt delta back to a prior run's projection would require changing the projector's cross-run storage behavior, which is out of scope per the task constraint.

The unresolved concerns (recall-window first delta while the adapter is inactive — a pre-existing path; the session-lifetime aggregate cache count) are unchanged and unverified against a live Claude CLI.

Review follow-up (revision 10, commit 4ed1f5333)

The ninth review confirmed one P2 finding:

Child output accumulates for the lifetime of a Claude adapter. The child text cache was never cleared, so a long session that repeatedly uses subagents retained an entry per distinct child message id and could grow server memory until the adapter was disposed. The cache is now byte-capped at the projector's own display-window scale (4 MiB), evicting the oldest ids — which also bounds the entry count (~65 entries at the per-entry cap). Eviction only ever drops the oldest ids, the least likely to still receive a delayed delta; a re-established item for an evicted id starts from the fragment, while its earlier text stays in that id's own run's progress rows (the same reasoning as the revision-9 note on finding 2). Added bounds the child text cache so a long session cannot accumulate child output; against revision 9 it fails with the finding's symptom (the oldest id still cached, cache unbound) and passes on revision 10.

The unresolved concern (recall-window first delta while the adapter is inactive — a pre-existing path, unverified live ordering) is unchanged.

Review follow-up (revision 11, commit 5dfdbdeb9)

The tenth review confirmed one P2 finding:

Child text cache can grow without a bound on entries. Eviction relied only on the text-byte counter, so distinct child ids carrying empty-text deltas added entries without moving that counter and a long-lived session could grow the map's entry overhead without eviction. Empty deltas now contribute nothing and do not add a cache entry, and the map is also evicted by entry count (bound at 512 entries, oldest first) alongside the 4 MiB byte bound. Added bounds the child text cache by entry count even for empty-text streams; against revision 10 it fails with the finding's symptom (600 entries retained, unbounded) and passes on revision 11.

The unresolved concerns (recall-window first delta while the adapter is inactive — a pre-existing path; cross-run native-item run_id reassignment by the baseline delta path — unverified as an introduced regression; live ordering) are unchanged.

Review follow-up (revision 12, commit afafd3e43)

The eleventh review surfaced a P1:

A child delta during recall can become the parent run's answer. The adapter established the commentary item only while a user turn was open, but a child delta arriving during recall — the next run already attached, active still false — was emitted alone, and the projector created a phase-less assistant item that could be selected as the parent run's answer. The commentary item is now established for every child delta whose id has not been seen this window, active or not: during recall it reaches the run path as commentary, and when no run is attached the projector drops the delta and only upserts the seeded commentary item, which cannot be selected. Added marks a delta-only child stream commentary when it arrives during recall; against revision 11 it fails with the finding's symptom (the retained item has no phase) and passes on revision 12.

Remaining concerns are documented as unresolved (cross-run native-item run_id reassignment, seed eviction before a late delta, live ordering), not confirmed findings.

Review follow-up (revision 13, commits b6b0a0e23, 5c216b8a7)

The twelfth review surfaced a P1 and a P2:

  1. P1 — A child fragment can become the next run's answer during recall. A between-run child delta recorded its id and emitted a commentary seed; a second delta arriving pre-prompt then skipped the establishment (the id was already known) and reached the projector alone after the window reset — a phase-less, answer-eligible item. While no turn is open, each child delta is now folded into a single seeded commentary item (no bare delta is emitted), so an inactive delta can never produce a phase-less item — in the no-run window the projector upserts the seeded item and drops nothing harmful, and during recall the item is commentary. Root-stream deltas and the active establish-and-merge path are unchanged. Added keeps between-run child deltas commentary even across a window reset (and the existing marks a delta-only child stream commentary when it arrives during recall still passes); the new test fails on the prior revision with the finding's symptom (phase-less "B" — the first fragment also lost) and passes on revision 13.
  2. P2 — A delayed idle delta can erase a saved child preview after cache eviction. With the fold, the item carries the fragment text (or the accumulated seed) instead of an empty seed, so an evicted id's between-run delta no longer overwrites the saved preview with empty. The earlier full text remains in that id's own run's progress rows; the preview reflects the latest fragment.

5c216b8a7 closes the native sessions in the new ingress regressions so their one-second output-flush timers are cancelled before the file's database shut down (an intermittent unhandled teardown error — exit 1 despite all tests passing — is gone).

Remaining concerns are documented as unresolved (cross-run native-item run_id reassignment, live ordering), not confirmed findings.

Review follow-up (revision 14, commit e1558818f)

The thirteenth review confirmed two P2 findings:

  1. Delayed child output can move an earlier run's native item to a later run. A delayed delta for a completed child stream was re-established in the next run with the cached text; the projector recorded it as the next run's progress and its native_items upsert changed the row's run_id, misattributing the output and removing it from the earlier run's stored history.
  2. A delayed idle delta can overwrite saved child text after cache eviction. An idle delta for an evicted id emitted an item with only the new fragment; the unbound upsert replaced the saved item, losing the earlier native-history text.

Both are resolved by the same model: a child stream whose turn has completed is frozen. Every child id seen in a turn is harvested into a bounded prior-ids set at that turn's result, and any later delta or assistant message for a prior id is ignored entirely — nothing is emitted, so nothing can be re-attributed to a later run, moved, or overwritten. New ids in the recall or between-turn window still fold or establish commentary items, so a child fragment can never become a phase-less, answer-eligible item.

The three cross-turn regressions now assert the frozen behavior (ignores late child events for a stream whose turn already completed, plus the two active-turn tests) — each fails on the prior revision (the old adapter re-emitted the items) and passes on revision 14. The remaining recall/new-id tests are unchanged and green.

Remaining concerns are documented as unresolved (an unverified no-message_start delta resolving to a fresh id, live ordering), not confirmed findings.

Review follow-up (revisions 15 and 16, commits 69781245d, c18012904, rebased on main 87525aed0)

The fourteenth review (head e1558818f) found three P2s. Revision 15 (69781245d) addressed them: completed child ids are kept for the adapter's lifetime (the 4,096 cap let an evicted id's late event overwrite completed history); a frozen child's mixed assistant frame still emits its tool_use blocks and suppresses only the text; and child ids were kept across prompt().

The fifteenth review (head 69781245d) found that keeping child ids across prompt() was a High regression of this PR's own defect. A child stream folded between runs has no run, the next run's attach resets the projector window, and a post-dispatch delta then skipped its commentary item, so the projector created a phase-less fragment that could be selected as the answer. Revision 16 (c18012904) restores clearing the ids at a new user turn. Added re-establishes a between-run child stream as commentary after the next prompt; against 69781245d it fails with the finding's symptom (retained item { text: "B" } with no phase) and passes on c18012904.

The sixteenth review (head c18012904) had no P1 and no security finding. Its findings are documented, not fixed, because each one reverses an earlier fix:

  • P2, eviction shortens progress text. If a recall-window child's seed is evicted from the text cache (over 512 streams or 4 MiB in one turn cycle), its re-established commentary item carries only the new fragment. This is a deliberate trade-off, recorded in the adapter comment and asserted by keeps a post-prompt delta commentary even when its recall seed was evicted. Keeping ids across prompt() to avoid it is the High regression above. Making the cache unbounded was rejected in review 5. The item is never phase-less, so the answer is unaffected.
  • P2, a late delta without a fresh message_start. The result clears the stream-to-message mapping, so such a delta falls back to the envelope id and bypasses the completed-child guard. This was already listed as unresolved. Whether the Claude CLI emits that ordering is unverified.
  • P3, completed child ids grow for the adapter's lifetime. These are ids only. Bounding them is what review 14 flagged, because an evicted id's late event overwrote completed history.

Tests (fixture coverage)

Added regressions at the changed boundary:

  • test/native-activity-ingress.test.ts (adapter→projector ingress, following the existing Claude usage case): a child assistant message is retained as phase: "commentary" while parent items keep their exact shape; a child stream's first delta is preceded by a commentary item with the same id, so a turn ending on the child stream selects nothing; and the delta-after-full-message merge (above).
  • test/native-turn-recovery.test.ts (full worker + fake CLI, following the existing Claude recovery case). test/fixtures/harness.mjs now emits child assistant output between the parent's final assistant message and the parent result, driven by LOOP_TEST_CLAUDE_CHILD_TEXT, LOOP_TEST_CLAUDE_CHILD_DELTA_ONLY and LOOP_TEST_CLAUDE_PARENT_BLANK:
    1. Child assistant message with text after the parent's final text, before the parent result: the run's answer and the sealed Result body are the parent's text ("Fixture done").
    2. Child stream deltas with no following child assistant message before the parent result: the child text is not the answer; the run's answer is the parent's text.
    3. Parent's final message has no text and a child message does: the run's answer is empty ("") and no Result is sealed (publishCompletedRunResult returns null) — nothing is eligible.
    4. The child's text is still recorded as a progress update on the run (messages.update_key = 'native-message:child-1', metadata.progress = true).
    5. A turn with no child sessions produces the same answer as before ("Fixture done").

Confirmed cases 1–3 (plus the ingress phase tests) fail on main before this change. I reverted src/adapters/claude.ts and re-ran the new tests; the failures were the defect itself:

  • case 1: run result: "Subagent result text" instead of "Fixture done" (child assistant text sealed)
  • case 2: run result: "Subagent stream text" instead of "Fixture done" (child deltas sealed)
  • case 3: run result: "Only the subagent speaks" instead of ""
  • ingress: child items retained without phase: "commentary".

Verification

Submitted tips: abe4e1218 (initial fix), 62cb94efe (review 1), 400ca71ae (review 2), ead8891c4 (review 3), 0d9149b5b (review 4), 85e93651d (review 5), 17a21579e (review 6), d166196ae (review 7), 9c7a961e9 (review 8), 4ed1f5333 (review 9), 5dfdbdeb9 (review 10), afafd3e43 (review 11), b6b0a0e23/5c216b8a7 (review 12), e1558818f (review 13), 69781245d (review 14, rebased on main 87525aed0), c18012904 (review 15). The table below was run on the e1558818f tree; the runs on 69781245d and c18012904 are listed after it.

Command Exit Result
npm run check 0 check: all passed (prettier/format, typecheck, openapi, sql-index, contracts; latest run 149.9s)
npm test -- --shard=1/3 --maxWorkers=1 --testTimeout=30000 0 109 files, 1042 passed
npm test -- --shard=2/3 --maxWorkers=1 --testTimeout=30000 0 108 files, 1038 passed
npm test -- --shard=3/3 --maxWorkers=1 --testTimeout=30000 0 108 files, 1051 passed
npm run test:native 0 512 passed, 0 failed (includes test/adapters.node.test.ts exercising the changed fixture)

Runs on the rebased revisions:

Tree Command Exit Result
69781245d npm run check 0 check: all passed
69781245d npm test -- --shard=1/3 --maxWorkers=2 --testTimeout=30000 0 110 files, 1277 passed
69781245d npx vitest run test/review-snapshot-load.test.ts 0 16 passed (CI shard 1 on e1558818f had timed out on this test at 5 s; this PR does not touch it)
69781245d shards 2 and 3 locally 124 stopped by my 590 s limit at host load ~11, no failures logged; CI shards 2 and 3 passed
c18012904 npm run check 0 check: all passed
c18012904 npx vitest run on the 15 test/native*.test.ts vitest files 0 15 files, 177 passed
c18012904 GitHub CI (check, tests (1-3)) pass all four jobs passed (run 44823872524)

npm run test:native was not rerun on c18012904; it was stopped by my time limit under host load. The fixture harness is unchanged since e1558818f, where it passed.

Host note: the shared host periodically creates a stray empty /tmp/.git that trips Git-walk tests (guarded), and test/library-route-contracts.test.ts has a pre-existing exact-ended_at timing flake; both are unrelated to this change. The shards above ran with the guard and exited 0 with no errors.

Each shard also passed on the revision-1 tree earlier; the only failed shard attempts on this host were caused by a shared-host artifact (see below), not by the change.

These are fixture results; I did not run a live Claude Code session, so real CLI orderings of child-session messages remain unverified.

Host note (pre-existing, unrelated to this change): a live Loop workspace running on this host periodically creates an empty /tmp/.git directory, which makes the Git ancestor-walk in agent-worktree/harness-routing/native-workspaces/fleet-enrollment tests throw "Could not verify the project's Git status" while it exists. I removed it before each clean run (all shards green above); CI will not have this artifact.

Known limitation

Already-sealed Results that may contain subagent text (sealed before this fix) are not rewritten, re-sealed or backfilled; they stay as they are, per the constraint. No prompt or turn was replayed.

Other adapters

Reviewed for the same defect: Codex passes through the harness-provided phase (phase: item.phase); Muse maps messagePhase to commentary/final_answer and emits subagent items as kind: "tool"; Pi, OpenCode, Antigravity (including headless) and the ACP/Grok adapters have no child-session marker comparable to Claude's parent_tool_use_id — Antigravity headless marks the successful response final_answer. No same defect found; no other adapter changed.

docs/harnesses.md

No note added: that guide does not describe how the answer is chosen per harness (it only states that a final reply is published after the harness confirms completion), so there is nothing to update.

Canonical example followed: none: this is a native-adapter ingress fix with fixture regressions; the AGENTS.md index maps state-machine, route, SQL and external-effect recipes, none of which this change touches.

@j-asefa
j-asefa force-pushed the loop/child-commentary branch 2 times, most recently from 5c554e9 to abe4e12 Compare November 19, 2026 21:31
Task tool (subagent) assistant messages and stream deltas arrived with no
item phase, so when one was the last assistant text before the parent's
result the projector selected it as the run's answer and it was sealed as
an immutable Result. Mark such items phase=commentary so the projector
keeps excluding them from answer selection while their text stays visible
as progress on the run.
The first delta of a child stream establishes an empty commentary item so
the projector merges deltas into it, but the projector replaces an item by
id. If the full child assistant message arrived first (or the id was known
from an earlier emit), re-establishing an empty item discarded the child's
saved text. Remember every emitted child item id so a later delta merges
into the existing item, establish only while a user turn is open so
post-result deltas cannot clobber saved native history, and drop the
eviction cap that could recreate the overwrite for resumed streams.

Fixes the review finding on the first revision.
A child message received between turns added its id to the adapter's child
id set, and the next turn cleared the projector's item window, so a delayed
child delta for that id skipped the establishing emit and left a phase-less
assistant fragment that could be selected as the new run's answer. Clear
the child id set at the start of a new user turn (mid-turn steering prompt
keeps it), so a delayed delta re-establishes its commentary item.

The stored-history fallback in projectOutput already excludes commentary
through assistantHistory's SQL; add a comment recording that invariant.
The supervisor attaches the next run before prompt(); a delayed full child
message arriving in that recall window records its id, and prompt() then
clears the id set so a later delta re-establishes the commentary item. The
empty re-established item replaced the retained text before the delta
appended, so progress showed only the trailing fragment. Keep the last-known
text per child id across prompt() (cleared with the id set at the result)
and seed the re-established item with it, so the replacement is
content-identical and the delta appends to the full text.
A full child message and a delta both arriving before the next prompt left
the cached seed stale at the full text, so the post-dispatch re-established
item replaced the projector's merged text and the first delta was lost.
Accumulate no-turn (recall/between-turns) deltas into the seed so a later
delta appends to everything seen so far.

Write the seed only while no turn is open (mid-turn entries are never read
back) and byte-cap it, so a long turn with many child messages cannot grow
adapter memory with copies of full child output; the projector truncates
its own display at a similar scale.
A single long-lived child stream could grow its recall cache entry without
bound (the eviction loop keeps one entry even past the budget), and eviction
could erase retained progress: a post-prompt delta for an evicted id
re-established an empty item over the projector's retained text. Cap each
entry at the projector's display-preview limit (keeping the tail), and keep
a never-evicting set of ids whose commentary item was emitted while no turn
was open, so a delta for an evicted id merges into the retained item instead
of replacing it.
Skipping the establishing item for an id whose recall seed was evicted let
a between-turns delta reach the projector alone: the next turn clears the
item window, the fallback finds nothing for the current run, and a
phase-less assistant item was created that completion could select as the
answer. Establish the commentary item unconditionally (phase is always
safe) and never evict the recall seed (its text then never degrades to
empty); each entry stays capped at the projector's display-preview limit,
so one long-lived stream cannot grow it without bound.
A full child message received during an active turn was not cached and the
result handler cleared the cache, so a delayed delta for that id in the next
turn re-established with an empty seed and the projector's upsert replaced
the stored child text with the fragment. Cache every child assistant
message's text (whether a turn is open or not) and keep it across the
result and prompt, so a re-established item carries the earlier text and
the delta appends to it. Entries stay capped at the projector's
display-preview limit; the entry count tracks the session's distinct child
message ids.
Child deltas updated the recall seed only while no turn was open, so an
active turn's delta-only stream was projected but never cached: a delayed
delta for that id in the next turn re-established from an incomplete seed
and the earlier streamed text was dropped (A plus B became A plus C). Keep
the seed current with every child delta, open turn or not, so a later
re-established item carries everything the stream has said so far.
The child text cache was never cleared, so a long session that repeatedly
uses subagents retained an entry per distinct child message id and could
grow server memory until the adapter was disposed. Byte-cap the cache at
the projector's own display-window scale (4 MiB), evicting the oldest ids.
Eviction only ever drops the oldest ids, the least likely to still receive
a delayed delta; a re-established item for an evicted id starts from the
fragment while its earlier text stays in its own run's progress rows.
Distinct child ids carrying empty-text deltas added cache entries without
moving the byte counter, so a long-lived session could grow the map's entry
overhead without triggering byte-based eviction. Skip empty deltas (they
contribute nothing to the seed) and evict by entry count too (bound at 512),
so both byte volume and entry count stay bounded.
The adapter established the commentary item only while a user turn was
open, but a child delta arriving during recall (the next run already
attached, active still false) was emitted alone, and the projector created
a phase-less assistant item that could be selected as the parent run's
answer. Establish the commentary item for every child delta when its id
has not been seen, active or not: during recall it reaches the run path as
commentary, and when no run is attached the projector drops the delta and
only upserts the seeded commentary item, which cannot be selected.
Two between-run deltas could still leave a phase-less, answer-eligible item:
the first established the id (recording it in childItems), the next window
reset cleared the projector items, and a pre-prompt delta skipped the
establishment and was emitted alone. While no turn is open, fold each child
delta into a single commentary item carrying the accumulated seed instead of
emitting a bare delta: the item is never phase-less (so it cannot be
selected as the answer) and it carries the stream text (so an evicted id's
item holds the fragment, not an empty seed). Root-stream deltas and the
active-turn establish-and-merge path are unchanged.
The tests emit items and deltas, which schedule a one-second output flush
timer; when the file's afterAll closed the database first, a pending timer
fired on the closed connection and surfaced as an intermittent unhandled
error (exit 1 despite all tests passing). Close each session so its flush
timer is cancelled before the database shuts down.
A delayed delta or assistant message for a child stream whose turn already
completed was re-established in the next run with the cached text. The
projector recorded it as the next run's progress and its native_items upsert
moved the row from the earlier run (misattributing the output and removing
it from that run's stored history), and an idle delta could overwrite the
saved preview after cache eviction. Ids are harvested at each turn's result
into a bounded prior-ids set; any later child event for them is ignored
entirely, so a completed stream's native history stays as it was and no
child output is ever re-attributed to a later run. New ids in the recall or
between-turn window still fold or establish commentary items, so a child
fragment can never become a phase-less, answer-eligible item.
…ates

Three review findings: the prior-ids set's 4,096 cap let an old id's late
event be treated as new and overwrite its completed history (the set is now
ids-only and never evicted); a post-prompt delta after recall-window cache
eviction re-established an empty item over the retained text (childItems is
now kept across prompt so the delta merges into the retained commentary
item instead); and a frozen mixed assistant frame dropped its tool update
alongside the intended text suppression (only the text item is suppressed;
tool_use blocks still emit).
@j-asefa
j-asefa force-pushed the loop/child-commentary branch from e155881 to 6978124 Compare November 20, 2026 21:13
Review of 6978124 found that keeping childItems across prompt() let a
child stream folded between runs skip its commentary item after the next
dispatch: attaching the run resets the projector window and the folded item
has no run, so the post-dispatch delta created a phase-less fragment that
was eligible as the answer. prompt() again clears the ids when no turn is
open, so the delta re-establishes its seeded commentary item.

Trade-off, documented in the adapter: when a stream's seed was evicted
(over 512 streams or 4 MiB in one turn cycle) the re-established item
carries only the new fragment, so that progress preview loses earlier
text. It is never phase-less.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@j-asefa
j-asefa merged commit 65f894e into main Nov 20, 2026
4 checks passed
@j-asefa
j-asefa deleted the loop/child-commentary branch November 20, 2026 22:41
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