1433
return [...turns];
1434
}
1436
>
const byAnchor = new Map<string, Turn[]>();
1437
>
const head: Turn[] = [];
1438
>
for (const record of records) {
1439
>
let turn: Turn;
1440
>
try {
1441
>
turn = JSON.parse(record.payload) as Turn;
1442
>
} catch {
1443
continue;
1444
}
1446
head.push(turn);
1448
>
const list = byAnchor.get(record.anchorTurnId) ?? [];
1449
>
list.push(turn);
1450
>
byAnchor.set(record.anchorTurnId, list);
1451
>
}
1452
>
// else: orphaned (anchor truncated away) → drop.
1453
>
}
1454
>
const merged: Turn[] = [...head];
1455
>
for (const turn of turns) {
1456
>
merged.push(turn);
1457
>
const locals = byAnchor.get(turn.id);
1458
>
if (locals) {
1459
>
merged.push(...locals);
1460
>
}
1461
>
}
1462
>
return merged;
1463
}
1464