Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions server/services/brainSyncLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export async function compactLog(minSeq = 0) {
: 0;

const preservedTail = [];
const tailKeys = new Set();
const tailEntriesByKey = new Map();
const olderEntriesByKey = new Map();
const unindexedOrUntypedOlder = [];

Expand All @@ -312,7 +312,9 @@ export async function compactLog(minSeq = 0) {
if (seq !== null && floor > 0 && seq >= floor) {
preservedTail.push(item);
if (entry?.type && entry?.id) {
tailKeys.add(`${entry.type}/${entry.id}`);
const key = `${entry.type}/${entry.id}`;
if (!tailEntriesByKey.has(key)) tailEntriesByKey.set(key, []);
tailEntriesByKey.get(key).push(item);
}
} else if (entry?.type && entry?.id) {
const key = `${entry.type}/${entry.id}`;
Expand All @@ -338,8 +340,8 @@ export async function compactLog(minSeq = 0) {
// If the tail carries ONLY stale/losing operations (e.g. olderWinner is a Jan-02 delete
// and tail has an echoed Jan-01 create), we MUST retain olderWinner before the verbatim
// tail so fresh / delta-only peers do not accept the stale create and resurrect the record.
if (tailKeys.has(key)) {
const tailItems = preservedTail.filter(i => i.entry && `${i.entry.type}/${i.entry.id}` === key);
const tailItems = tailEntriesByKey.get(key);
if (tailItems) {
const supersededByTail = tailItems.some(i => {
const tailTs = i.entry?.record?.updatedAt;
return tailTs != null && olderWinner.record?.updatedAt != null && tailTs > olderWinner.record.updatedAt;
Expand Down
20 changes: 20 additions & 0 deletions server/services/brainSyncLog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,5 +567,25 @@ describe('brainSyncLog', () => {
const fromZero = await getChangesSince(0);
expect(fromZero.changes.map(c => c.seq)).toEqual([2, 99]);
});

it('applies mixed LWW outcomes across indexed tail entries', async () => {
writeLog(
'{"seq":1,"op":"create","type":"links","id":"x","record":{"updatedAt":"2026-01-01T00:00:00.000Z"}}\n' +
'{"seq":2,"op":"delete","type":"links","id":"x","record":{"updatedAt":"2026-01-02T00:00:00.000Z"}}\n' +
'{"seq":3,"op":"create","type":"links","id":"y","record":{"updatedAt":"2026-01-01T00:00:00.000Z"}}\n' +
'{"seq":4,"op":"delete","type":"links","id":"y","record":{"updatedAt":"2026-01-03T00:00:00.000Z"}}\n' +
'{"seq":50,"op":"update","type":"links","id":"x","record":{"updatedAt":"2026-01-01T12:00:00.000Z"}}\n' +
'{"seq":51,"op":"create","type":"links","id":"y","record":{"updatedAt":"2026-01-04T00:00:00.000Z"}}\n' +
'{"seq":52,"op":"update","type":"links","id":"x","record":{"updatedAt":"2026-01-01T18:00:00.000Z"}}\n'
);
await initSyncLog();

expect(await compactLog(50)).toBe(3);

const lines = readFileSync(syncLogPath(), 'utf8').trim().split('\n').map(l => JSON.parse(l));
expect(lines.map(line => line.seq)).toEqual([2, 50, 51, 52]);
expect(lines[0].id).toBe('x');
expect(lines[0].op).toBe('delete');
});
});
});