From 313df9f7c244ae7c55ea2b15affc5b2ce70fb19b Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Tue, 17 Nov 2026 16:12:05 -0500 Subject: [PATCH] Keep streamed replies visible once while history and notifications catch up --- robot-docs/web.md | 5 ++++ web/scripts/transcript.test.mjs | 40 ++++++++++++++++++++++++++++++-- web/src/lib/transcript.svelte.ts | 33 ++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/robot-docs/web.md b/robot-docs/web.md index 9b5ca86fd..ba6518231 100644 --- a/robot-docs/web.md +++ b/robot-docs/web.md @@ -118,6 +118,11 @@ bytes can expire while the history remains. See [saved pastes and uploads](kernel-state.md#saved-pastes-and-uploads) and the [uploaded-file rules](contributing.md#handle-uploaded-files). +While a reply streams, `Transcript` keeps the live text or thinking visible +until a successful history read takes over. History can arrive ahead of the +stream notification. The first remaining live segment’s history boundary +determines which durable replies the chat can already show. + [`QueuePanel.svelte`](../web/src/lib/chat/QueuePanel.svelte) sits above the composer while anything waits in the session's [inbox](inbox.md). It lists what waits, in the order the session will read it, and drops, reorders, or clears it diff --git a/web/scripts/transcript.test.mjs b/web/scripts/transcript.test.mjs index 5c0963eea..f0b4ac0d9 100644 --- a/web/scripts/transcript.test.mjs +++ b/web/scripts/transcript.test.mjs @@ -627,7 +627,10 @@ test('a reply committed while an earlier read is in flight stays live until a la stream.emit({ events: [ { type: 'text', data: { message_id: 'm1', text: 'first' } }, - { type: 'committed', data: { replaces_live_ids: ['m1'] } }, + { + type: 'committed', + data: { high_water: 3, replaces_live_ids: ['m1'] }, + }, ], }); await settle(); @@ -635,7 +638,10 @@ test('a reply committed while an earlier read is in flight stays live until a la stream.emit({ events: [ { type: 'text', data: { message_id: 'm2', text: 'second' } }, - { type: 'committed', data: { replaces_live_ids: ['m2'] } }, + { + type: 'committed', + data: { high_water: 4, replaces_live_ids: ['m2'] }, + }, ], }); await settle(); @@ -1021,3 +1027,33 @@ test('thumbnailSource requests 256/512 edge variants only when the image exceeds }, ); }); + +test('history and commit notifications can arrive independently for identical replies', async () => { + api.reset([]); + const transcript = new Transcript('session'); + transcript.entries = conversation(); + transcript.attach(); + const event = (type, data) => stream.emit({ events: [{ type, data }] }); + const keys = () => + transcript.groups + .filter((row) => row.kind === 'assistant' && row.text === 'yes') + .map((row) => row.key); + try { + event('text', { message_id: 'a', text: 'yes' }); + const first = { ...message('assistant', 3, 3, 'yes'), id: '3' }; + const second = { ...message('assistant', 4, 4, 'yes'), id: '4' }; + api.answerHistory(async () => ({ items: [first], newer: null })); + await transcript.poll(); + assert.deepEqual(keys(), ['live:a']); + event('committed', { high_water: 3, replaces_live_ids: ['a'] }); + event('text', { message_id: 'b', text: 'yes' }); + api.answerHistory(async () => ({ items: [second], newer: null })); + await transcript.poll(); + assert.deepEqual(keys(), ['3', 'live:b']); + event('committed', { high_water: 4, replaces_live_ids: ['b'] }); + await transcript.poll(); + assert.deepEqual(keys(), ['3', '4']); + } finally { + transcript.detach(); + } +}); diff --git a/web/src/lib/transcript.svelte.ts b/web/src/lib/transcript.svelte.ts index 199a542fd..1baba209a 100644 --- a/web/src/lib/transcript.svelte.ts +++ b/web/src/lib/transcript.svelte.ts @@ -156,6 +156,8 @@ export function displayedIn( /** Output the session is writing now, before it commits: one message of thinking or reply. */ interface Live { id: string; + /** History already durable when the stream began this segment. */ + after: number; kind: 'text' | 'thinking'; text: string; } @@ -608,6 +610,8 @@ export class Transcript { #lease: ReturnType | undefined; #stop: (() => void) | null = null; #settle: ReturnType | undefined; + #streamPosition: number | null = null; + #visibleThrough = $derived(this.live[0]?.after ?? Infinity); /** Live output ids history has committed, kept until a read that began after their commit succeeds. */ #replacing = new Set(); more = $state(false); @@ -789,7 +793,13 @@ export class Transcript { } if (!page.newer || !fresh.length) break; } - this.#dropLive(committed); + // Without a stream boundary we cannot divide durable output between + // retired segments and newer ones. Hand the whole burst over together. + if ( + this.#streamPosition !== null || + this.live.every((item) => committed.has(item.id)) + ) + this.#dropLive(committed); } while (this.#again); } @@ -852,11 +862,16 @@ export class Transcript { snapshot.usage.observed_at || snapshot.usage.spent ? snapshot.usage : null; + this.#streamPosition = snapshot.history?.high_water ?? null; this.live = (snapshot.active_output ?? []).flatMap((output: any) => output.kind === 'text' || output.kind === 'thinking' ? [ { id: output.message_id, + after: + this.#streamPosition ?? + this.entries.at(-1)?.position ?? + 0, kind: output.kind, text: output.text ?? '…', }, @@ -901,7 +916,13 @@ export class Transcript { if (at < 0) this.live = [ ...this.live, - { id: data.message_id, kind: event.type, text: data.text }, + { + id: data.message_id, + kind: event.type, + text: data.text, + after: + this.#streamPosition ?? this.entries.at(-1)?.position ?? 0, + }, ]; else this.live = this.live.map((item, index) => @@ -932,6 +953,7 @@ export class Transcript { this.#commit(null); break; case 'committed': + this.#streamPosition = data.high_water ?? null; this.#commit( data.replaces_all_live ? 'all' : (data.replaces_live_ids ?? []), ); @@ -1408,6 +1430,13 @@ export class Transcript { } }; for (const entry of this.entries) { + // HTTP can see durable output before its stream notification. Keep the + // live rendition until the read that hands it over to history succeeds. + if ( + (entry.kind === 'assistant' || entry.kind === 'thinking') && + entry.position > this.#visibleThrough + ) + continue; // Mid-turn receipts belong to the surrounding turn. Loose idle receipts // still sign it off before shell time starts. if (entry.kind === 'shell_command') { -- knotmirror 0.0.1