summaryrefslogtreecommitdiffhomepage
path: root/src/core/chunks/reducer.ts
blob: ee5858ff1482c07402a6221656389d166c48c95b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import type { AgentEvent, Chunk, ImageInput, Role, StoredChunk } from "@dispatch/wire";
import { assertChunkExhaustive } from "../wire/conformance";
import { historyBackedProvisional } from "./reconcile";
import type { AccumulatingChunk, ProvisionalChunk, TranscriptState } from "./types";

/** The initial empty transcript state. */
export function initialState(): TranscriptState {
  return {
    committed: [],
    provisional: [],
    accumulating: null,
    currentTurnId: null,
    latestUsage: null,
    sealedTurnId: null,
    hiddenBeforeSeq: 0,
    hiddenThinkingCount: 0,
    generating: false,
    providerRetry: null,
  };
}

/**
 * Clear the `generating` flag without touching anything else. Used on a WS
 * (re)connect: a turn may have sealed while we were disconnected, so the live
 * `turn-sealed`/`done` that would have cleared `generating` was missed. The
 * caller resets here, then re-subscribes — if the turn is still running the
 * server's replay re-asserts `generating` via the replayed `turn-start`.
 */
export function clearGenerating(state: TranscriptState): TranscriptState {
  if (!state.generating) return state;
  // Also drop a stale `provider-retry` banner — a retry pending at disconnect
  // is stale once we re-subscribe (provider-retry events are not replayed), so
  // a finished turn must not keep showing a "retrying…" banner forever.
  return { ...state, generating: false, providerRetry: null };
}

function flushAccumulating(
  provisional: readonly ProvisionalChunk[],
  acc: AccumulatingChunk | null,
): readonly ProvisionalChunk[] {
  if (acc === null) return provisional;
  const chunk: Chunk =
    acc.kind === "text" ? { type: "text", text: acc.text } : { type: "thinking", text: acc.text };
  return [
    ...provisional,
    withTurn(
      { role: "assistant", chunk, ...(acc.stepId !== undefined ? { stepId: acc.stepId } : {}) },
      acc.turnId,
    ),
  ];
}

/** Attach a `turnId` to a provisional chunk (omitted when unknown). */
function withTurn(p: ProvisionalChunk, turnId: string | undefined): ProvisionalChunk {
  return turnId !== undefined ? { ...p, turnId } : p;
}

/**
 * Tag the trailing UNTAGGED user run (the sender's optimistic echo, appended
 * before the server assigned a turn id) with the turn that just started.
 */
function tagTrailingUserEcho(
  provisional: readonly ProvisionalChunk[],
  turnId: string,
): readonly ProvisionalChunk[] {
  let start = provisional.length;
  while (start > 0) {
    const p = provisional[start - 1];
    if (p === undefined || p.role !== "user" || p.turnId !== undefined) break;
    start--;
  }
  if (start === provisional.length) return provisional;
  return [
    ...provisional.slice(0, start),
    ...provisional.slice(start).map((p) => ({ ...p, turnId })),
  ];
}

/**
 * Content equality for two chunks of the SAME role (used to de-dup an
 * optimistic echo against the authoritative committed version). Compares the
 * discriminating payload only — `stepId` (generation provenance) is ignored
 * since it is absent on provisional echoes but present on committed tool chunks.
 */
function chunkContentEquals(a: Chunk, b: Chunk): boolean {
  if (a.type !== b.type) return false;
  switch (a.type) {
    case "text": {
      const o = b as Extract<Chunk, { type: "text" }>;
      return a.text === o.text;
    }
    case "thinking": {
      const o = b as Extract<Chunk, { type: "thinking" }>;
      return a.text === o.text;
    }
    case "image": {
      const o = b as Extract<Chunk, { type: "image" }>;
      return a.url === o.url;
    }
    case "error": {
      const o = b as Extract<Chunk, { type: "error" }>;
      return a.message === o.message && a.code === o.code;
    }
    case "system": {
      const o = b as Extract<Chunk, { type: "system" }>;
      return a.text === o.text;
    }
    case "tool-call": {
      const o = b as Extract<Chunk, { type: "tool-call" }>;
      return a.toolCallId === o.toolCallId && a.toolName === o.toolName;
    }
    case "tool-result": {
      const o = b as Extract<Chunk, { type: "tool-result" }>;
      return a.toolCallId === o.toolCallId && a.toolName === o.toolName && a.isError === o.isError;
    }
    default:
      return assertChunkExhaustive(a) === assertChunkExhaustive(b);
  }
}

/**
 * The trailing run of consecutive same-role provisional chunks at the end of
 * `provisional` (the optimistic echo of one message). Returns the start/end
 * indices `[start, end)` into `provisional` (empty if none).
 */
function trailingRun(
  provisional: readonly ProvisionalChunk[],
  role: Role,
): readonly ProvisionalChunk[] {
  const end = provisional.length;
  let start = end;
  while (start > 0 && provisional[start - 1]?.role === role) start--;
  return provisional.slice(start, end);
}

/**
 * Merge authoritative seq-keyed chunks into the committed history.
 * Dedupes by seq (new wins), keeps seq-monotonic order, idempotent.
 * A sealed/finally-synced turn supersedes only its own provisional chunks;
 * unrelated turns and an already-running queued carry remain live.
 *
 * Chunks below the chat-limit unload watermark (`hiddenBeforeSeq`) are
 * REJECTED: a full-cache or tail merge must not resurrect what the trim
 * unloaded. Restoring earlier history goes through `restoreEarlier` instead.
 */
export function applyHistory(
  state: TranscriptState,
  chunks: readonly StoredChunk[],
  finishedTurnIds: readonly string[] = [],
): TranscriptState {
  const seqMap = new Map<number, StoredChunk>();
  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);
  const identityRows = new Map(seqMap);
  for (const c of chunks) identityRows.set(c.seq, c);
  const backed = historyBackedProvisional(
    state,
    [...identityRows.values()].sort((a, b) => a.seq - b.seq),
  );
  const finished = new Set(finishedTurnIds);
  if (state.sealedTurnId !== null) finished.add(state.sealedTurnId);
  state = {
    ...state,
    provisional: state.provisional.filter((p) => !backed.has(p) && !finished.has(p.turnId ?? "")),
    accumulating:
      state.accumulating?.turnId !== undefined && finished.has(state.accumulating.turnId)
        ? null
        : state.accumulating,
  };

  if (state.sealedTurnId !== null) {
    // Legacy (non-recovery) histories may carry no turn provenance at all.
    // Retain their seal cleanup, but never erase an already-live queued carry.
    const legacySeal =
      (state.historyRecoveryTurnIds?.length ?? 0) === 0 && finishedTurnIds.length === 0;
    return {
      ...state,
      committed,
      provisional: legacySeal
        ? state.provisional.filter(
            (p) =>
              state.generating &&
              p.turnId === state.currentTurnId &&
              p.turnId !== state.sealedTurnId,
          )
        : state.provisional,
      accumulating:
        legacySeal && state.accumulating?.turnId === state.sealedTurnId ? null : state.accumulating,
      sealedTurnId: null,
      ...(state.historyRecoveryTurnIds !== undefined
        ? {
            historyRecoveryTurnIds: state.historyRecoveryTurnIds.filter(
              (id) => !finishedTurnIds.includes(id),
            ),
          }
        : {}),
    };
  }

  // During generation: if new committed chunks arrived, the provisional
  // array may contain duplicates — the optimistic echo from `appendUserMessage`
  // is now backed by committed chunks (CR-6: user message persisted at turn
  // start). A user message may be multi-chunk (`[text, image, image, …]`), so
  // match the trailing provisional user-run against the trailing committed
  // user-run by content equality and drop the whole echo when it is fully
  // backed. Leaves the accumulating (streaming) chunk untouched.
  if (
    addedNew &&
    state.generating &&
    state.provisional.length > 0 &&
    !state.provisional.some(
      (p) => p.turnId !== undefined && state.historyRecoveryTurnIds?.includes(p.turnId),
    )
  ) {
    const provRun = trailingRun(state.provisional, "user");
    if (provRun.length > 0) {
      const commRun = trailingRun(committed, "user");
      // Drop the provisional user echo iff every echoed chunk is content-equal
      // to the corresponding committed chunk (the server persisted the same
      // message). A partial match (echo longer than committed) keeps the echo
      // — the not-yet-committed tail stays until the turn seals.
      const fullyBacked =
        commRun.length >= provRun.length &&
        provRun.every((p, i) => {
          const c = commRun[i];
          return (
            c !== undefined &&
            (c.turnId === undefined || p.turnId === undefined || c.turnId === p.turnId) &&
            chunkContentEquals(p.chunk, c.chunk)
          );
        });
      if (fullyBacked) {
        const dropStart = state.provisional.length - provRun.length;
        const provisional = state.provisional.slice(0, dropStart);
        return { ...state, committed, provisional, accumulating: state.accumulating };
      }
    }
  }

  return {
    ...state,
    committed,
    ...(state.historyRecoveryTurnIds !== undefined
      ? {
          historyRecoveryTurnIds: state.historyRecoveryTurnIds.filter(
            (id) => !finishedTurnIds.includes(id),
          ),
        }
      : {}),
  };
}

/**
 * Fold one live AgentEvent into the provisional state.
 *
 * - `turn-start` records the turnId.
 * - `user-message` appends the turn's user prompt (de-duped vs the sender's
 *   optimistic echo) so a watcher renders it mid-turn.
 * - `text-delta` extends the current accumulating TextChunk (or starts one).
 * - `reasoning-delta` extends the current accumulating ThinkingChunk (or starts one).
 * - `tool-call` / `tool-result` / `error` finalize any accumulating chunk and
 *   add a new provisional chunk.
 * - `steering` appends a user bubble mid-turn (drained from the message queue
 *   at a tool-result boundary; the queue surface separately clears on drain).
 * - `usage` stores the latest Usage.
 * - `done` finalizes any accumulating chunk (turn still provisional).
 * - `turn-sealed` finalizes any accumulating chunk and sets sealedTurnId.
 * - `status` and `tool-output` are ignored (best-effort no-ops).
 *
 * `generating` is folded structurally: a `turn-start` or any content delta sets
 * it true; `done` / `turn-sealed` / `error` clear it. This is what a watching
 * (or reconnected) client renders as "generating…", with no dependence on the
 * free-form `status` event string.
 *
 * NOTE: this is the inner reducer. The transient `provider-retry` banner is SET
 * here (on a `provider-retry` event) but CLEARED by the `foldEvent` wrapper
 * below, so the clearing logic stays centralized in one place.
 */
function reduceEvent(state: TranscriptState, event: AgentEvent): TranscriptState {
  switch (event.type) {
    case "status":
    case "tool-output":
      return state;

    case "turn-start":
      return {
        ...state,
        provisional: tagTrailingUserEcho(
          flushAccumulating(state.provisional, state.accumulating),
          event.turnId,
        ),
        accumulating: null,
        currentTurnId: event.turnId,
        generating: true,
      };

    case "user-message": {
      // The turn's USER prompt, surfaced on the event stream (backend CR-3) so a
      // WATCHER/late-joiner renders it mid-turn instead of waiting for seal. The
      // SENDER already echoed its own prompt optimistically (`appendUserMessage`),
      // so DE-DUP: skip if the trailing provisional USER run already contains an
      // identical user text chunk. The echo may be multi-chunk (`[text, image,
      // image, …]` — `appendUserMessage` appends the text first, then images), so
      // we scan the whole trailing user run, not just the last chunk (which would
      // be an image when images were pasted). A pure watcher has no such echo →
      // it appends and renders. The `user-message` event carries ONLY text (never
      // images — images arrive via history/loadSince); an images-only send (empty
      // text) emits no `user-message` and is not de-duped here.
      if (event.text.length === 0) return state;
      const run = trailingRun(state.provisional, "user");
      const echoed = run.find(
        (p) =>
          (p.turnId === undefined || p.turnId === event.turnId) &&
          p.chunk.type === "text" &&
          p.chunk.text === event.text,
      );
      if (echoed !== undefined) {
        return {
          ...state,
          // Preserve the live text independently of the local send. A delayed
          // queued ack may remove its images, but cannot erase this server event.
          provisional: tagTrailingUserEcho(state.provisional, event.turnId).map((p) => {
            if (p.chunk !== echoed.chunk) return p;
            const { localRequestId: _local, ...confirmed } = p;
            return { ...confirmed, opening: true };
          }),
          generating: true,
        };
      }
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      return {
        ...state,
        provisional: [
          ...provisional,
          {
            role: "user",
            chunk: { type: "text", text: event.text },
            turnId: event.turnId,
            opening: true,
          },
        ],
        accumulating: null,
        generating: true,
      };
    }

    case "text-delta": {
      const acc = state.accumulating;
      if (acc !== null && acc.kind === "text" && acc.turnId === event.turnId) {
        return {
          ...state,
          accumulating: { ...acc, text: acc.text + event.delta },
          generating: true,
        };
      }
      const provisional = flushAccumulating(state.provisional, acc);
      return {
        ...state,
        provisional,
        accumulating: { kind: "text", text: event.delta, turnId: event.turnId },
        generating: true,
      };
    }

    case "reasoning-delta": {
      const acc = state.accumulating;
      if (acc !== null && acc.kind === "thinking" && acc.turnId === event.turnId) {
        return {
          ...state,
          accumulating: { ...acc, text: acc.text + event.delta },
          generating: true,
        };
      }
      const provisional = flushAccumulating(state.provisional, acc);
      return {
        ...state,
        provisional,
        accumulating: { kind: "thinking", text: event.delta, turnId: event.turnId },
        generating: true,
      };
    }

    case "tool-call": {
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      const chunk: Chunk = {
        type: "tool-call",
        toolCallId: event.toolCallId,
        toolName: event.toolName,
        input: event.input,
        stepId: event.stepId,
      };
      return {
        ...state,
        provisional: [...provisional, { role: "assistant", chunk, turnId: event.turnId }],
        accumulating: null,
        generating: true,
      };
    }

    case "tool-result": {
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      const chunk: Chunk = {
        type: "tool-result",
        toolCallId: event.toolCallId,
        toolName: event.toolName,
        content: event.content,
        isError: event.isError,
        stepId: event.stepId,
      };
      return {
        ...state,
        provisional: [...provisional, { role: "tool", chunk, turnId: event.turnId }],
        accumulating: null,
        generating: true,
      };
    }

    case "error": {
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      const chunk: Chunk =
        event.code !== undefined
          ? { type: "error", message: event.message, code: event.code }
          : { type: "error", message: event.message };
      return {
        ...state,
        provisional: [...provisional, { role: "assistant", chunk, turnId: event.turnId }],
        accumulating: null,
        generating: false,
      };
    }

    case "usage":
      return { ...state, latestUsage: event.usage };

    case "step-complete":
      // A persistence boundary may lag this notification. Stamp provenance and
      // finish the generation chunk, but keep it until history really backs it.
      return {
        ...state,
        provisional: flushAccumulating(state.provisional, state.accumulating),
        accumulating: null,
      };

    case "turn-ended":
      return {
        ...state,
        provisional:
          state.accumulating?.turnId === event.turnId
            ? flushAccumulating(state.provisional, state.accumulating)
            : state.provisional,
        accumulating: state.accumulating?.turnId === event.turnId ? null : state.accumulating,
        generating: state.currentTurnId === event.turnId ? false : state.generating,
      };

    case "done": {
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      return {
        ...state,
        provisional,
        accumulating: null,
        generating: false,
      };
    }

    case "turn-sealed": {
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      return {
        ...state,
        provisional,
        accumulating: null,
        sealedTurnId: event.turnId,
        generating: false,
      };
    }

    case "steering": {
      // A steering message drained from the queue at a tool-result boundary
      // (the model sees it alongside the tool results). Append a user bubble
      // to the provisional transcript; the turn is still in flight. The queue
      // surface clears separately on drain (a different channel) — no de-dup
      // here (unlike `user-message`, steering is never optimistically echoed
      // into the transcript by the sender).
      if (event.text.length === 0) return state;
      const provisional = flushAccumulating(state.provisional, state.accumulating);
      const preceding = [...state.committed, ...provisional]
        .reverse()
        .find((p) => p.turnId === event.turnId && p.chunk.type === "tool-result");
      const afterToolCallId =
        preceding?.chunk.type === "tool-result" ? preceding.chunk.toolCallId : undefined;
      return {
        ...state,
        provisional: [
          ...provisional,
          {
            role: "user",
            chunk: { type: "text", text: event.text },
            turnId: event.turnId,
            ...(afterToolCallId !== undefined ? { afterToolCallId } : {}),
          },
        ],
        accumulating: null,
        generating: true,
      };
    }

    case "provider-retry": {
      // TRANSIENT: a retryable provider error is being retried with backoff.
      // Coalesce — the latest attempt + delay replaces any previous, so a
      // single updating "retrying…" banner shows the newest. NOT a chunk: it
      // never enters provisional/committed, so it can never pollute the prompt
      // or be replayed on a reload. The turn is still in flight, so `generating`
      // (already true from `turn-start`) is left untouched.
      return { ...state, providerRetry: event };
    }

    default:
      return state; // Forward-compatible with additive wire events.
  }
}

/**
 * Fold one live AgentEvent into the transcript state. Wraps `reduceEvent` to
 * centralize the TRANSIENT `provider-retry` banner's clearing: the banner is
 * SET by `reduceEvent` on a `provider-retry` event (coalescing), and CLEARED
 * here when the model's content resumes (the retry succeeded) or the turn ends
 * (done/sealed/error) or a new turn starts. Metadata/no-op events
 * (`status`/`tool-output`/`usage`/`step-complete`) leave a showing banner
 * untouched. The `state.providerRetry !== null` guard keeps the common path
 * (no banner pending) identity-stable — no needless new object.
 */
// Events that clear a showing provider-retry banner (content resumed or turn ended).
const RETRY_CLEARING_EVENTS: ReadonlySet<AgentEvent["type"]> = new Set([
  "turn-start",
  "text-delta",
  "reasoning-delta",
  "tool-call",
  "tool-result",
  "error",
  "done",
  "turn-sealed",
  "turn-ended",
]);

export function foldEvent(state: TranscriptState, event: AgentEvent): TranscriptState {
  if (
    event.type === "tool-call" ||
    event.type === "tool-result" ||
    event.type === "step-complete"
  ) {
    state = {
      ...state,
      provisional: state.provisional.map((p) =>
        p.turnId === event.turnId &&
        p.role === "assistant" &&
        p.stepId === undefined &&
        (p.chunk.type === "text" || p.chunk.type === "thinking")
          ? { ...p, stepId: event.stepId }
          : p,
      ),
      accumulating:
        state.accumulating?.turnId === event.turnId
          ? { ...state.accumulating, stepId: event.stepId }
          : state.accumulating,
    };
  }
  const next = reduceEvent(state, event);
  if (event.type === "provider-retry") return next; // set by reduceEvent; not a clearing event
  if (RETRY_CLEARING_EVENTS.has(event.type) && state.providerRetry !== null) {
    return { ...next, providerRetry: null };
  }
  return next;
}

/**
 * Optimistically append a user message to the provisional list.
 * Flushes any in-progress accumulating chunk first (defensively).
 * The provisional user chunks are superseded when applyHistory receives
 * the authoritative committed chunks after a turn seals.
 *
 * When `images` are provided, they are appended AFTER the text chunk (in
 * order) as `image` chunks — matching the server's persisted layout
 * (`[text, image, image, …]` in one user message). A text chunk is only
 * appended when `text` is non-empty (an images-only send echoes just the
 * images). The `user-message` event carries only text (never images), so its
 * de-dup scans the trailing user run for the echoed text rather than just the
 * last chunk.
 */
export function appendUserMessage(
  state: TranscriptState,
  text: string,
  images?: readonly ImageInput[],
  localRequestId?: string,
): TranscriptState {
  const provisional = flushAccumulating(state.provisional, state.accumulating);
  const userChunks: Chunk[] = [];
  if (text.length > 0) userChunks.push({ type: "text", text });
  if (images !== undefined) {
    for (const img of images) {
      if (img.url.length === 0) continue;
      const chunk: Chunk =
        img.mimeType !== undefined
          ? { type: "image", url: img.url, mimeType: img.mimeType }
          : { type: "image", url: img.url };
      userChunks.push(chunk);
    }
  }
  if (userChunks.length === 0) {
    // Nothing to echo (empty text + no images) — leave state unchanged but
    // still flush any accumulating chunk defensively.
    return { ...state, provisional, accumulating: null };
  }
  return {
    ...state,
    provisional: [
      ...provisional,
      ...userChunks.map(
        (chunk) =>
          ({
            role: "user",
            chunk,
            ...(localRequestId !== undefined ? { localRequestId } : {}),
          }) as const,
      ),
    ],
    accumulating: null,
  };
}

/** Remove only still-local artifacts of one accepted queued send; never history/live text. */
export function removeLocalEcho(state: TranscriptState, requestId: string): TranscriptState {
  const provisional = state.provisional.filter((p) => p.localRequestId !== requestId);
  return provisional.length === state.provisional.length ? state : { ...state, provisional };
}

/** A resync notice is lifecycle evidence, never fabricated transcript content. */
export function markHistoryRecovering(state: TranscriptState, turnId: string): TranscriptState {
  return {
    ...state,
    currentTurnId: turnId,
    generating: true,
    historyRecoveryTurnIds: state.historyRecoveryTurnIds?.includes(turnId)
      ? state.historyRecoveryTurnIds
      : [...(state.historyRecoveryTurnIds ?? []), turnId],
  };
}