@flamingo-stack/openframe-frontend-core 0.0.607 → 0.0.608-snapshot.20260908153450

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/dist/{chunk-7ZAPPBFT.cjs → chunk-3N4FXBVJ.cjs} +3 -3
  2. package/dist/{chunk-7ZAPPBFT.cjs.map → chunk-3N4FXBVJ.cjs.map} +1 -1
  3. package/dist/{chunk-PS4WZ7YL.cjs → chunk-3XNMDLGB.cjs} +9 -9
  4. package/dist/{chunk-PS4WZ7YL.cjs.map → chunk-3XNMDLGB.cjs.map} +1 -1
  5. package/dist/{chunk-UJ64PL7A.cjs → chunk-45XXK2VU.cjs} +10 -10
  6. package/dist/{chunk-UJ64PL7A.cjs.map → chunk-45XXK2VU.cjs.map} +1 -1
  7. package/dist/{chunk-ANHHTB32.js → chunk-6MLRFM3T.js} +5 -5
  8. package/dist/{chunk-ZYEAIBWA.js → chunk-B3UXNGVM.js} +3 -3
  9. package/dist/{chunk-2V3PJ5X5.js → chunk-B7LYKBCD.js} +5 -5
  10. package/dist/{chunk-JGSEBZYO.js → chunk-BSMPCPOZ.js} +155 -38
  11. package/dist/{chunk-JGSEBZYO.js.map → chunk-BSMPCPOZ.js.map} +1 -1
  12. package/dist/{chunk-3J73ZG3V.js → chunk-BUXGWBTF.js} +2 -2
  13. package/dist/{chunk-63AGD45L.js → chunk-C7CTEUU7.js} +2 -2
  14. package/dist/{chunk-XOQI3AH7.cjs → chunk-CZTOSG7Q.cjs} +22 -22
  15. package/dist/{chunk-XOQI3AH7.cjs.map → chunk-CZTOSG7Q.cjs.map} +1 -1
  16. package/dist/{chunk-Y6MYIJTK.cjs → chunk-D3DTC7SZ.cjs} +15 -15
  17. package/dist/{chunk-Y6MYIJTK.cjs.map → chunk-D3DTC7SZ.cjs.map} +1 -1
  18. package/dist/{chunk-YUDYFA6H.cjs → chunk-DKNXAJMP.cjs} +183 -163
  19. package/dist/chunk-DKNXAJMP.cjs.map +1 -0
  20. package/dist/{chunk-XRNILWKL.cjs → chunk-FYG7QNAO.cjs} +5 -5
  21. package/dist/{chunk-XRNILWKL.cjs.map → chunk-FYG7QNAO.cjs.map} +1 -1
  22. package/dist/{chunk-IXXMNATN.cjs → chunk-GMOBT5NP.cjs} +70 -70
  23. package/dist/{chunk-IXXMNATN.cjs.map → chunk-GMOBT5NP.cjs.map} +1 -1
  24. package/dist/{chunk-CCY47QKL.js → chunk-HGVW7OHI.js} +2 -2
  25. package/dist/{chunk-7CQA7DAB.js → chunk-JKM5FH63.js} +3 -3
  26. package/dist/{chunk-RVCLDXCR.js → chunk-JOCVQZGT.js} +5 -5
  27. package/dist/{chunk-GPIPJOOV.js → chunk-JR7I2AGL.js} +3 -3
  28. package/dist/{chunk-SWC5LOGS.cjs → chunk-L2JWC5VS.cjs} +43 -43
  29. package/dist/{chunk-SWC5LOGS.cjs.map → chunk-L2JWC5VS.cjs.map} +1 -1
  30. package/dist/{chunk-TF6FCNKU.js → chunk-LA2W5L6K.js} +2 -2
  31. package/dist/{chunk-XJKNJJYO.js → chunk-MVBRMQGU.js} +26 -6
  32. package/dist/{chunk-XJKNJJYO.js.map → chunk-MVBRMQGU.js.map} +1 -1
  33. package/dist/{chunk-G7ZK3Q5B.cjs → chunk-NJUSDREV.cjs} +22 -22
  34. package/dist/{chunk-G7ZK3Q5B.cjs.map → chunk-NJUSDREV.cjs.map} +1 -1
  35. package/dist/{chunk-Q6ZFTDRR.cjs → chunk-O2M4T34Q.cjs} +4 -4
  36. package/dist/{chunk-Q6ZFTDRR.cjs.map → chunk-O2M4T34Q.cjs.map} +1 -1
  37. package/dist/{chunk-PSGHIQXU.js → chunk-OTYV6SDN.js} +3 -3
  38. package/dist/{chunk-YCOXI5CH.js → chunk-QX4ANESW.js} +6 -6
  39. package/dist/{chunk-JLVIGGUF.cjs → chunk-RZM6FPOT.cjs} +25 -25
  40. package/dist/{chunk-JLVIGGUF.cjs.map → chunk-RZM6FPOT.cjs.map} +1 -1
  41. package/dist/{chunk-WR23DZLT.cjs → chunk-SEW25Y2W.cjs} +10 -10
  42. package/dist/{chunk-WR23DZLT.cjs.map → chunk-SEW25Y2W.cjs.map} +1 -1
  43. package/dist/{chunk-25XSOCZK.cjs → chunk-TCEALWE3.cjs} +33 -33
  44. package/dist/{chunk-25XSOCZK.cjs.map → chunk-TCEALWE3.cjs.map} +1 -1
  45. package/dist/{chunk-Z2NXXA27.js → chunk-TJ3GFULR.js} +3 -3
  46. package/dist/{chunk-3FC4DPRX.js → chunk-VCYRJY7J.js} +4 -4
  47. package/dist/{chunk-VK743MVL.cjs → chunk-XNDTYTVT.cjs} +188 -71
  48. package/dist/chunk-XNDTYTVT.cjs.map +1 -0
  49. package/dist/{chunk-BEDMOEAM.cjs → chunk-ZRLFBLME.cjs} +3 -3
  50. package/dist/{chunk-BEDMOEAM.cjs.map → chunk-ZRLFBLME.cjs.map} +1 -1
  51. package/dist/components/case-studies/index.cjs +9 -9
  52. package/dist/components/case-studies/index.js +3 -3
  53. package/dist/components/chat/index.cjs +2 -2
  54. package/dist/components/chat/index.js +1 -1
  55. package/dist/components/chat/utils/history-merge.d.ts +4 -2
  56. package/dist/components/chat/utils/history-merge.d.ts.map +1 -1
  57. package/dist/components/contact/index.cjs +4 -4
  58. package/dist/components/contact/index.js +3 -3
  59. package/dist/components/docs/index.cjs +7 -7
  60. package/dist/components/docs/index.js +6 -6
  61. package/dist/components/embeds/index.cjs +4 -4
  62. package/dist/components/embeds/index.js +3 -3
  63. package/dist/components/faq/index.cjs +4 -4
  64. package/dist/components/faq/index.js +3 -3
  65. package/dist/components/features/board/index.d.ts +1 -1
  66. package/dist/components/features/board/index.d.ts.map +1 -1
  67. package/dist/components/features/board/ticket-card.d.ts.map +1 -1
  68. package/dist/components/features/board/types.d.ts +18 -0
  69. package/dist/components/features/board/types.d.ts.map +1 -1
  70. package/dist/components/features/index.cjs +3 -3
  71. package/dist/components/features/index.js +2 -2
  72. package/dist/components/help-center-pages/index.cjs +23 -23
  73. package/dist/components/help-center-pages/index.js +14 -14
  74. package/dist/components/index.cjs +38 -38
  75. package/dist/components/index.js +13 -13
  76. package/dist/components/meeting-scheduler/index.cjs +24 -24
  77. package/dist/components/meeting-scheduler/index.js +2 -2
  78. package/dist/components/navigation/index.cjs +6 -6
  79. package/dist/components/navigation/index.js +5 -5
  80. package/dist/components/onboarding-guides/index.cjs +6 -6
  81. package/dist/components/onboarding-guides/index.js +5 -5
  82. package/dist/components/tickets/index.cjs +7 -7
  83. package/dist/components/tickets/index.js +6 -6
  84. package/dist/components/ui/index.cjs +3 -3
  85. package/dist/components/ui/index.js +2 -2
  86. package/dist/index.cjs +7 -7
  87. package/dist/index.js +6 -6
  88. package/package.json +1 -1
  89. package/src/components/chat/utils/__tests__/history-merge.test.ts +292 -0
  90. package/src/components/chat/utils/history-merge.ts +276 -56
  91. package/src/components/features/board/index.ts +2 -0
  92. package/src/components/features/board/ticket-card.tsx +31 -2
  93. package/src/components/features/board/types.ts +20 -0
  94. package/src/stories/TicketCard.stories.tsx +40 -1
  95. package/dist/chunk-VK743MVL.cjs.map +0 -1
  96. package/dist/chunk-YUDYFA6H.cjs.map +0 -1
  97. /package/dist/{chunk-ANHHTB32.js.map → chunk-6MLRFM3T.js.map} +0 -0
  98. /package/dist/{chunk-ZYEAIBWA.js.map → chunk-B3UXNGVM.js.map} +0 -0
  99. /package/dist/{chunk-2V3PJ5X5.js.map → chunk-B7LYKBCD.js.map} +0 -0
  100. /package/dist/{chunk-3J73ZG3V.js.map → chunk-BUXGWBTF.js.map} +0 -0
  101. /package/dist/{chunk-63AGD45L.js.map → chunk-C7CTEUU7.js.map} +0 -0
  102. /package/dist/{chunk-CCY47QKL.js.map → chunk-HGVW7OHI.js.map} +0 -0
  103. /package/dist/{chunk-7CQA7DAB.js.map → chunk-JKM5FH63.js.map} +0 -0
  104. /package/dist/{chunk-RVCLDXCR.js.map → chunk-JOCVQZGT.js.map} +0 -0
  105. /package/dist/{chunk-GPIPJOOV.js.map → chunk-JR7I2AGL.js.map} +0 -0
  106. /package/dist/{chunk-TF6FCNKU.js.map → chunk-LA2W5L6K.js.map} +0 -0
  107. /package/dist/{chunk-PSGHIQXU.js.map → chunk-OTYV6SDN.js.map} +0 -0
  108. /package/dist/{chunk-YCOXI5CH.js.map → chunk-QX4ANESW.js.map} +0 -0
  109. /package/dist/{chunk-Z2NXXA27.js.map → chunk-TJ3GFULR.js.map} +0 -0
  110. /package/dist/{chunk-3FC4DPRX.js.map → chunk-VCYRJY7J.js.map} +0 -0
@@ -1229,3 +1229,295 @@ describe('ordering of surviving realtime messages', () => {
1229
1229
  expect(ids(merged)).toEqual([H_U0.id, outOfOrder.id, AWAY_U.id, AWAY_A.id]);
1230
1230
  });
1231
1231
  });
1232
+
1233
+ describe('sliding history window (refetch after the thread grew)', () => {
1234
+ // Pages are fetched newest-first with a fixed count, so a refetch after the
1235
+ // thread has grown returns the newest rows and the head of what is on screen
1236
+ // is no longer in it. Everything older than that window must survive, in
1237
+ // place: it cannot have a twin the window could dedupe it against.
1238
+ const persisted = (n: number, role: 'user' | 'assistant', seq?: number): TestMessage => ({
1239
+ id: `dddd${String(n).padStart(4, '0')}`,
1240
+ role,
1241
+ content: role === 'user' ? `q${n}` : txt(`a${n}`),
1242
+ timestamp: t(10_000 + n),
1243
+ ...(seq !== undefined ? { streamSeq: seq } : {}),
1244
+ });
1245
+ const head = [persisted(1, 'user'), persisted(2, 'assistant', 2)];
1246
+ const refetched = [
1247
+ persisted(5, 'user'),
1248
+ persisted(6, 'assistant', 6),
1249
+ persisted(7, 'user'),
1250
+ persisted(8, 'assistant', 8),
1251
+ ];
1252
+ const windowWith = (sixth: TestMessage) => [refetched[0], sixth, refetched[2], refetched[3]];
1253
+ const merge = (existingMessages: TestMessage[], processedHistory: TestMessage[] = refetched, fetchedAt = 99_000) =>
1254
+ mergeHistoryWithRealtime({
1255
+ processedHistory,
1256
+ rawHistoryIds: new Set(ids(processedHistory)),
1257
+ existingMessages,
1258
+ streamingMessageId: null,
1259
+ historyFetchedAt: fetchedAt,
1260
+ historyMaxStreamSeq: maxPersistedStreamSeq([
1261
+ { messages: processedHistory.map(m => ({ lastChunkStreamSeq: m.streamSeq })) },
1262
+ ]),
1263
+ realtimeSeenStreamSeq: 8,
1264
+ });
1265
+
1266
+ it('keeps persisted rows that fell out of the refetched window IN FRONT of it, in order', () => {
1267
+ // These used to be appended after the window — the start of the
1268
+ // conversation showed up below the newest reply on every refetch.
1269
+ const onScreen = Array.from({ length: 12 }, (_, i) => {
1270
+ const n = i + 1;
1271
+ return n % 2 === 0 ? persisted(n, 'assistant', n) : persisted(n, 'user');
1272
+ });
1273
+ expect(ids(merge(onScreen, onScreen.slice(4)))).toEqual(ids(onScreen));
1274
+ });
1275
+
1276
+ it('keeps a live turn before the first window row even when its text repeats a window row', () => {
1277
+ // The thread's own window rows fix the boundary, so a row above them is
1278
+ // older whatever its content says — no clock, seq or text heuristics.
1279
+ const live: TestMessage[] = [
1280
+ { id: 'optimistic-3', role: 'user', content: 'q5', timestamp: t(10_003) },
1281
+ { id: 'assistant-4', role: 'assistant', content: txt('a6'), timestamp: t(10_004), streamSeq: 4 },
1282
+ ];
1283
+ expect(ids(merge([...head, ...live, ...refetched]))).toEqual([...ids(head), ...ids(live), ...ids(refetched)]);
1284
+ });
1285
+
1286
+ it('judges rows on their own when the thread holds no window row — a live turn older than the window is kept', () => {
1287
+ // Turns streamed live and never refetched are still synthetics. Once the
1288
+ // window slid past them, same-role coverage read "a persisted row reached
1289
+ // this seq" and DROPPED the assistant reply — with no twin on screen to
1290
+ // stand in for it — while the optimistic prompt was kept out of order.
1291
+ const live: TestMessage[] = [
1292
+ { id: 'optimistic-3', role: 'user', content: 'q3', timestamp: t(10_003) },
1293
+ { id: 'assistant-4', role: 'assistant', content: txt('a4'), timestamp: t(10_004), streamSeq: 4 },
1294
+ ];
1295
+ expect(ids(merge([...head, ...live]))).toEqual([...ids(head), ...ids(live), ...ids(refetched)]);
1296
+ });
1297
+
1298
+ it('pins the twin guard: a cut-short copy of a window turn is left to coverage even when its seq reads older', () => {
1299
+ // The window's first assistant row is unstamped (async stamping lag), so by
1300
+ // seq alone the live copy looks older than the window. Its answer text is
1301
+ // a prefix of that row's: it is the row's twin, and coverage drops it.
1302
+ const unstamped: TestMessage = {
1303
+ id: 'dddd0006',
1304
+ role: 'assistant',
1305
+ content: txt('a6 and then'),
1306
+ timestamp: t(10_006),
1307
+ };
1308
+ const snapshot = windowWith(unstamped);
1309
+ const cutShort: TestMessage = {
1310
+ id: 'assistant-6',
1311
+ role: 'assistant',
1312
+ content: txt('a6'),
1313
+ timestamp: t(10_006),
1314
+ streamSeq: 6,
1315
+ };
1316
+ expect(ids(merge([cutShort], snapshot))).toEqual(ids(snapshot));
1317
+ });
1318
+
1319
+ it('pins the twin guard the other way: a persisted row still being written is the twin of the fuller live copy', () => {
1320
+ const lagging: TestMessage = { id: 'dddd0006', role: 'assistant', content: txt('a6'), timestamp: t(10_006) };
1321
+ const snapshot = windowWith(lagging);
1322
+ const fuller: TestMessage = {
1323
+ id: 'assistant-6',
1324
+ role: 'assistant',
1325
+ content: txt('a6 and then'),
1326
+ timestamp: t(10_006),
1327
+ streamSeq: 6,
1328
+ };
1329
+ expect(ids(merge([fuller], snapshot))).toEqual(ids(snapshot));
1330
+ });
1331
+
1332
+ it('keeps a trailing live turn AFTER the window — the boundary only bounds what is older', () => {
1333
+ const live: TestMessage[] = [
1334
+ { id: 'optimistic-9', role: 'user', content: 'q9', timestamp: t(99_500) },
1335
+ { id: 'assistant-10', role: 'assistant', content: txt('a10'), timestamp: t(99_600), streamSeq: 10 },
1336
+ ];
1337
+ expect(ids(merge([...head, ...refetched, ...live]))).toEqual([...ids(head), ...ids(refetched), ...ids(live)]);
1338
+ });
1339
+
1340
+ it('never pulls a host-minted tail row above the window it sits behind', () => {
1341
+ // A non-synthetic, non-optimistic id is read as persisted only when the
1342
+ // thread holds no window row; behind the window it is the live tail.
1343
+ const preview: TestMessage = {
1344
+ id: 'ticket-preview-1',
1345
+ role: 'assistant',
1346
+ content: txt('preview'),
1347
+ timestamp: t(99_500),
1348
+ };
1349
+ expect(ids(merge([...refetched, preview]))).toEqual([...ids(refetched), preview.id]);
1350
+ });
1351
+
1352
+ it('never reads a host-minted row newer than the fetch as persisted history, even with no window row in the thread', () => {
1353
+ // openframe-chat's call shape: the thread is the live tail only. A ticket
1354
+ // preview it minted after the fetch must neither anchor the live rows in
1355
+ // front nor be dropped as a disjoint persisted row.
1356
+ const live: TestMessage[] = [
1357
+ { id: 'user-5', role: 'user', content: 'q5', timestamp: t(99_500), streamSeq: 5 },
1358
+ { id: 'assistant-6', role: 'assistant', content: txt('a6'), timestamp: t(99_600), streamSeq: 6 },
1359
+ ];
1360
+ const preview: TestMessage = {
1361
+ id: 'ticket-preview-1',
1362
+ role: 'assistant',
1363
+ content: txt('preview'),
1364
+ timestamp: t(99_900),
1365
+ };
1366
+ const snapshot = [persisted(5, 'user'), persisted(6, 'assistant', 6)];
1367
+ expect(ids(merge([...live, preview], snapshot))).toEqual([...ids(snapshot), preview.id]);
1368
+ expect(ids(merge([preview], snapshot))).toEqual([...ids(snapshot), preview.id]);
1369
+ });
1370
+
1371
+ it('does not read a message sent AFTER the fetch as older than the window on a clock behind the server', () => {
1372
+ // Client clock 9_000, window rows stamped 10_005+ by the server, fetch at
1373
+ // client 8_000: the send happened after the fetch, so it cannot predate a
1374
+ // window that fetch returned.
1375
+ const sent: TestMessage = { id: 'optimistic-x', role: 'user', content: 'a new question', timestamp: t(9_000) };
1376
+ expect(ids(merge([sent], refetched, 8_000))).toEqual([...ids(refetched), sent.id]);
1377
+ });
1378
+
1379
+ it('anchors on the last persisted row when the thread holds no window row: everything before it is older', () => {
1380
+ // Text collisions with the window ("continue"-style prompts, short
1381
+ // answers) cannot demote a row that precedes a persisted row the window
1382
+ // itself lacks.
1383
+ const live: TestMessage[] = [
1384
+ { id: 'optimistic-1', role: 'user', content: 'q5', timestamp: t(10_001) },
1385
+ { id: 'assistant-2', role: 'assistant', content: txt('a6'), timestamp: t(10_002), streamSeq: 2 },
1386
+ ];
1387
+ const older = [persisted(3, 'user'), persisted(4, 'assistant', 4)];
1388
+ expect(ids(merge([...live, ...older]))).toEqual([...ids(live), ...ids(older), ...ids(refetched)]);
1389
+ });
1390
+
1391
+ it('heals a copy a previous merge placed in front once a window row proves it by request id', () => {
1392
+ // A tool turn whose persisted row was unstamped when first refetched read
1393
+ // as older and went in front. Now stamped and in the window, the shared
1394
+ // tool id says which turn the live copy is: back to coverage, gone.
1395
+ const toolTurn = (id: string, seq?: number): TestMessage => ({
1396
+ id,
1397
+ role: 'assistant',
1398
+ content: [tool('call-6', 'EXECUTED_TOOL')],
1399
+ timestamp: t(10_006),
1400
+ ...(seq !== undefined ? { streamSeq: seq } : {}),
1401
+ });
1402
+ const stale = toolTurn('assistant-6', 6);
1403
+ const snapshot = windowWith(toolTurn('dddd0006', 6));
1404
+ expect(ids(merge([stale, ...snapshot], snapshot))).toEqual(ids(snapshot));
1405
+ });
1406
+
1407
+ it('keeps an older system notice on seq alone — its persisted twin is stamped, and empty text is no identity', () => {
1408
+ const notice: TestMessage = { id: 'system-3', role: 'user', content: '', timestamp: t(10_003), streamSeq: 3 };
1409
+ const windowNotice: TestMessage = { id: 'dddd0006', role: 'user', content: '', timestamp: t(10_006), streamSeq: 6 };
1410
+ const snapshot = windowWith(windowNotice);
1411
+ expect(ids(merge([notice], snapshot))).toEqual([notice.id, ...ids(snapshot)]);
1412
+ });
1413
+
1414
+ it("drops a cut-short copy of the window's oldest stamped turn even though its seq reads older", () => {
1415
+ // A copy carries its LAST CONSUMED chunk's seq, which is below its twin's
1416
+ // stamp whenever the client missed the tail — the reconnect race. Every
1417
+ // window assistant is stamped, so seq alone would promote it; its text is a
1418
+ // prefix of the twin's, and the verdict wins.
1419
+ const full = { ...persisted(6, 'assistant', 6), content: txt('a6 and then more') };
1420
+ const snapshot = windowWith(full);
1421
+ const cutShort: TestMessage = {
1422
+ id: 'assistant-2',
1423
+ role: 'assistant',
1424
+ content: txt('a6'),
1425
+ timestamp: t(10_006),
1426
+ streamSeq: 4,
1427
+ };
1428
+ const prompt: TestMessage = { id: 'optimistic-1', role: 'user', content: 'q5', timestamp: t(10_005) };
1429
+ expect(ids(merge([prompt, cutShort], snapshot))).toEqual(ids(snapshot));
1430
+ });
1431
+
1432
+ it('drops persisted rows disjoint from the window when nothing live bridges them', () => {
1433
+ // Rows added while this client was unsubscribed for a page or more: the
1434
+ // thread's persisted rows and the window share nothing, and the next older
1435
+ // page would land above the thread's rows. The window alone is the thread.
1436
+ expect(ids(merge(head))).toEqual(ids(refetched));
1437
+ });
1438
+
1439
+ it('trusts seq for an assistant copy when every assistant row in the window is stamped, even a text-less one', () => {
1440
+ const thinkingOnly: TestMessage = {
1441
+ id: 'assistant-3',
1442
+ role: 'assistant',
1443
+ content: [{ type: 'thinking', text: 'hmm' }],
1444
+ timestamp: t(10_003),
1445
+ streamSeq: 3,
1446
+ };
1447
+ expect(ids(merge([thinkingOnly]))).toEqual([thinkingOnly.id, ...ids(refetched)]);
1448
+ });
1449
+ });
1450
+
1451
+ describe('computeHistoryPrepend after a window slide', () => {
1452
+ const persisted = (
1453
+ n: number,
1454
+ role: 'user' | 'assistant',
1455
+ text = role === 'user' ? `q${n}` : `a${n}`,
1456
+ ): TestMessage => ({
1457
+ id: `cccc${String(n).padStart(4, '0')}`,
1458
+ role,
1459
+ content: role === 'user' ? text : txt(text),
1460
+ timestamp: t(20_000 + n),
1461
+ ...(role === 'assistant' ? { streamSeq: n } : {}),
1462
+ });
1463
+ const snapshot = [persisted(5, 'user'), persisted(6, 'assistant')];
1464
+
1465
+ it('leaves out the persisted twins of live rows the thread still shows above the boundary', () => {
1466
+ // The thread kept turn 3/4 live (see the window-boundary tests); the older
1467
+ // page now carries their persisted rows. Prepending them would render the
1468
+ // turn twice; the persisted copy waits for the next full merge.
1469
+ const live: TestMessage[] = [
1470
+ { id: 'optimistic-3', role: 'user', content: 'q3', timestamp: t(20_003) },
1471
+ { id: 'assistant-4', role: 'assistant', content: txt('a4'), timestamp: t(20_004), streamSeq: 4 },
1472
+ ];
1473
+ const olderPages = [
1474
+ persisted(1, 'user'),
1475
+ persisted(2, 'assistant'),
1476
+ persisted(3, 'user'),
1477
+ persisted(4, 'assistant'),
1478
+ ...snapshot,
1479
+ ];
1480
+ const result = computeHistoryPrepend(olderPages, [...live, ...snapshot]);
1481
+ if (!result) throw new Error('computeHistoryPrepend returned null for a page with older messages');
1482
+ expect(ids(result.newMessages)).toEqual(['cccc0001', 'cccc0002']);
1483
+ });
1484
+
1485
+ it('matches a repeated prompt positionally — the live row twins the NEWEST same-text row before the boundary', () => {
1486
+ const live: TestMessage[] = [{ id: 'optimistic-3', role: 'user', content: 'continue', timestamp: t(20_003) }];
1487
+ const olderPages = [
1488
+ persisted(1, 'user', 'continue'),
1489
+ persisted(2, 'assistant'),
1490
+ persisted(3, 'user', 'continue'),
1491
+ persisted(4, 'assistant'),
1492
+ ...snapshot,
1493
+ ];
1494
+ const result = computeHistoryPrepend(olderPages, [...live, ...snapshot]);
1495
+ if (!result) throw new Error('computeHistoryPrepend returned null for a page with older messages');
1496
+ expect(ids(result.newMessages)).toEqual(['cccc0001', 'cccc0002', 'cccc0004']);
1497
+ });
1498
+
1499
+ it('prepends everything when nothing live sits above the boundary', () => {
1500
+ const olderPages = [persisted(3, 'user'), persisted(4, 'assistant'), ...snapshot];
1501
+ const result = computeHistoryPrepend(olderPages, snapshot);
1502
+ if (!result) throw new Error('computeHistoryPrepend returned null for a page with older messages');
1503
+ expect(ids(result.newMessages)).toEqual(['cccc0003', 'cccc0004']);
1504
+ });
1505
+
1506
+ it('claims one persisted answer per live answer, newest first, and only in the cut-short direction', () => {
1507
+ // A live "Done." above the boundary twins the newest persisted "Done." on
1508
+ // the page — not an older turn that merely opens with it.
1509
+ const live: TestMessage[] = [
1510
+ { id: 'assistant-4', role: 'assistant', content: txt('Done.'), timestamp: t(20_004), streamSeq: 4 },
1511
+ ];
1512
+ const olderPages = [
1513
+ persisted(1, 'user'),
1514
+ persisted(2, 'assistant', 'Done. Anything else?'),
1515
+ persisted(3, 'user'),
1516
+ persisted(4, 'assistant', 'Done.'),
1517
+ ...snapshot,
1518
+ ];
1519
+ const result = computeHistoryPrepend(olderPages, [...live, ...snapshot]);
1520
+ if (!result) throw new Error('computeHistoryPrepend returned null for a page with older messages');
1521
+ expect(ids(result.newMessages)).toEqual(['cccc0001', 'cccc0002', 'cccc0003']);
1522
+ });
1523
+ });