Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(brain): preserve pre-floor LWW winner when tail operations are stale (#5439)
prateek18-ai76775fe91fc8
diff --git a/server/services/brainSyncLog.js b/server/services/brainSyncLog.jsindex 3cfa5efd8b..ff384654c9 100644--- a/server/services/brainSyncLog.js+++ b/server/services/brainSyncLog.js@@ -325,22 +325,34 @@ export async function compactLog(minSeq = 0) {}}- // Replay terminal winning state for older keys not already superseded in tail+ // Replay terminal winning state for older keysconst keptOlder = [];const olderWinnersByKey = new Map();for (const [key, items] of olderEntriesByKey) {- if (tailKeys.has(key)) {- // Already superseded by newer operations in the preserved tail- continue;- }const entries = items.map(i => i.entry);- const terminalEntry = replayTerminal(entries);- if (terminalEntry) {- const matchingItem = items.find(i => i.entry === terminalEntry)- || { rawLine: JSON.stringify(terminalEntry), entry: terminalEntry, seq: terminalEntry.seq };- keptOlder.push(matchingItem);- olderWinnersByKey.set(key, matchingItem);+ const olderWinner = replayTerminal(entries);+ if (!olderWinner) continue;++ // If this key also appears in the preserved tail, check whether any tail+ // operation strictly supersedes the pre-floor LWW winner (updatedAt > olderWinner.updatedAt).+ // 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 supersededByTail = tailItems.some(i => {+ const tailTs = i.entry?.record?.updatedAt;+ return tailTs != null && olderWinner.record?.updatedAt != null && tailTs > olderWinner.record.updatedAt;+ });+ if (supersededByTail) {+ continue;+ }}++ const matchingItem = items.find(i => i.entry === olderWinner)+ || { rawLine: JSON.stringify(olderWinner), entry: olderWinner, seq: olderWinner.seq };+ keptOlder.push(matchingItem);+ olderWinnersByKey.set(key, matchingItem);}const kept = [...unindexedOrUntypedOlder, ...keptOlder, ...preservedTail];diff --git a/server/services/brainSyncLog.test.js b/server/services/brainSyncLog.test.jsindex a5de0f1d12..8a65d9c8a8 100644--- a/server/services/brainSyncLog.test.js+++ b/server/services/brainSyncLog.test.js@@ -537,5 +537,35 @@ describe('brainSyncLog', () => {expect(lines[0].record.updatedAt).toBe('2026-01-02T00:00:00.000Z');expect(lines[0].seq).toBe(99); // max seq preserved});++ it('preserves pre-floor LWW winner before verbatim tail when tail is stale under positive floor', async () => {+ // seq 1: create links x (Jan-01)+ // seq 2: delete links x (Jan-02) -> winning delete+ // seq 99: stale create links x (Jan-01) -> stale echoed create in tail+ 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":99,"op":"create","type":"links","id":"x","record":{"updatedAt":"2026-01-01T00:00:00.000Z"}}\n'+ );+ await initSyncLog();++ // Compact with positive floor (50). Tail (seq >= 50) is kept verbatim,+ // and seq 2 (delete Jan-02) is preserved before the tail so a fresh peer+ // replaying from since=0 rejects the stale create and avoids resurrecting the record.+ const dropped = await compactLog(50);+ expect(dropped).toBe(1); // seq 1 is dropped, seq 2 and seq 99 are kept++ const lines = readFileSync(syncLogPath(), 'utf8').trim().split('\n').map(l => JSON.parse(l));+ expect(lines).toHaveLength(2);+ expect(lines[0].seq).toBe(2);+ expect(lines[0].op).toBe('delete');+ expect(lines[0].record.updatedAt).toBe('2026-01-02T00:00:00.000Z');+ expect(lines[1].seq).toBe(99);+ expect(lines[1].op).toBe('create');++ // A fresh peer pulling from since=0 gets seq 2 (delete) then seq 99 (stale create)+ const fromZero = await getChangesSince(0);+ expect(fromZero.changes.map(c => c.seq)).toEqual([2, 99]);+ });});});