From 4e1d041c0ee34fa1b74b0d5ecbd432cdacf696e9 Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Mon, 22 Jun 2026 14:07:30 +0900 Subject: feat: trim chunks during generation via step-complete syncTail (CR-6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The backend now persists chunks at step boundaries during generation (CR-6). The FE calls syncTail on each step-complete event to fetch the newly committed chunks. applyHistory clears the provisional array when new committed chunks arrive mid-generation (they're duplicates of what was folded from live events). The accumulating chunk (current in-progress step) is kept. This means trimTranscript can now drop oldest committed chunks uniformly during a long turn — no unbounded provisional growth. The browser never holds more than chatLimit chunks, even mid-generation. 3 new tests: clears provisional on new committed during generation, keeps provisional when no new chunks, keeps accumulating when clearing. 689 tests green. --- src/core/chunks/reducer.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/core/chunks/reducer.ts') diff --git a/src/core/chunks/reducer.ts b/src/core/chunks/reducer.ts index 035846c..37f0164 100644 --- a/src/core/chunks/reducer.ts +++ b/src/core/chunks/reducer.ts @@ -54,8 +54,10 @@ export function applyHistory( ): TranscriptState { const seqMap = new Map(); for (const c of state.committed) seqMap.set(c.seq, c); + let addedNew = false; for (const c of chunks) { if (c.seq < state.hiddenBeforeSeq) continue; + if (!seqMap.has(c.seq)) addedNew = true; seqMap.set(c.seq, c); } const committed = Array.from(seqMap.values()).sort((a, b) => a.seq - b.seq); @@ -70,6 +72,15 @@ export function applyHistory( }; } + // During generation: if new committed chunks arrived (CR-6 — backend + // persists at step boundaries), clear provisional chunks. They're + // duplicates — the same content was folded from live events but is now + // persisted with seq. Keep the accumulating chunk (current in-progress + // step, not yet persisted). + if (addedNew && state.generating) { + return { ...state, committed, provisional: [], accumulating: state.accumulating }; + } + return { ...state, committed }; } -- cgit v1.2.3