Skip to content

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

address review (codex): compact sync log to LWW winner, not seq-max

Adam Eivy1ab58582e730
diff --git a/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.js b/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.js
index f395a08bf4..a72bf5f057 100644
--- a/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.js
+++ b/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.js
@@ -50,6 +50,22 @@ function parseLog(content) {
return entries;
}
+// Does log entry `candidate` win last-writer-wins over the current winner
+// `cur` for the same (type, id)? Mirrors applyRemoteRecord's runtime guard:
+// strictly newer updatedAt wins; on an EQUAL timestamp a delete beats a
+// create/update (the runtime delete path applies on `>=`, i.e. a delete is
+// accepted when not strictly older). A missing updatedAt never wins.
+function lwwReplaces(candidate, cur) {
+ const a = candidate?.record?.updatedAt;
+ const b = cur?.record?.updatedAt;
+ if (a == null) return false;
+ if (b == null) return true;
+ if (a > b) return true;
+ if (a < b) return false;
+ // Equal timestamps: prefer the delete (tie-break matches runtime LWW).
+ return candidate.op === 'delete' && cur.op !== 'delete';
+}
+
/**
* Core transform, exported for unit tests. Pure over its inputs:
* - logEntries: array of parsed sync-log entries (in seq order).
@@ -89,22 +105,40 @@ export function computeBrainCleanup(logEntries, stores, { nowIso }) {
}
}
- // 2. Compact the log to the terminal entry per (type, id), preserving seq.
- // Keep insertion order by seq so the file stays monotonic for peers.
- const terminal = new Map(); // key -> entry (last wins)
+ // 2. Compact the log to the LAST-WRITER-WINS winner per (type, id) — NOT the
+ // highest-seq entry. The ping-pong appends create→delete repeatedly with
+ // FIXED updatedAt values (create's is always older than delete's), each
+ // cycle getting a fresh seq. So the highest-SEQ entry for a deleted record
+ // can be a stale `create` whose updatedAt LOSES to an earlier `delete`. If
+ // that stale create became the sole surviving log entry (the delete dropped
+ // by compaction), a fresh/lagging peer would pull a create with no delete
+ // to supersede it and resurrect the record — restarting the loop. Choosing
+ // the LWW winner makes the compacted log's net replay identical to the full
+ // log's.
+ const winners = new Map(); // key -> LWW-winning entry
let maxSeqEntry = null; // the single highest-seq entry, type/id or not
for (const e of logEntries) {
if (e?.seq == null) continue;
if (!maxSeqEntry || e.seq > maxSeqEntry.seq) maxSeqEntry = e;
if (!e.type || !e.id) continue;
- terminal.set(`${e.type}/${e.id}`, e);
+ const key = `${e.type}/${e.id}`;
+ const cur = winners.get(key);
+ if (!cur || lwwReplaces(e, cur)) winners.set(key, e);
}
- const kept = [...terminal.values()];
- // Guarantee the max seq survives even if the highest-seq entry lacks type/id
- // (every real appendChange sets both today, but the peer-cursor-validity
- // invariant must not depend on that). Append it only if not already kept.
+ const kept = [...winners.values()];
+ // Preserve the global max seq so peer brainSeq cursors stay valid (dropping it
+ // makes our currentSeq fall below a peer's cursor and forces a full re-sync) —
+ // but NEVER by re-introducing an LWW-losing entry. If the max-seq entry is
+ // inert (no type/id) keep it verbatim; if it belongs to a kept key, stamp that
+ // key's winner with the max seq instead of resurrecting the raw stale line.
if (maxSeqEntry && !kept.some((e) => e.seq === maxSeqEntry.seq)) {
- kept.push(maxSeqEntry);
+ if (!maxSeqEntry.type || !maxSeqEntry.id) {
+ kept.push(maxSeqEntry);
+ } else {
+ const winner = winners.get(`${maxSeqEntry.type}/${maxSeqEntry.id}`);
+ if (winner) winner.seq = maxSeqEntry.seq;
+ else kept.push(maxSeqEntry);
+ }
}
const compactedLines = kept
.sort((a, b) => a.seq - b.seq)
diff --git a/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.test.js b/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.test.js
index c57ed5fbc8..5d7fb17e3f 100644
--- a/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.test.js
+++ b/scripts/migrations/080-brain-tombstone-and-synclog-cleanup.test.js
@@ -90,6 +90,27 @@ describe('migration 080 — brain tombstone + sync-log cleanup', () => {
expect(lines.find((l) => l.id === 'b').seq).toBe(5);
});
+ it('keeps the DELETE as the terminal entry when a stale create has the highest seq (LWW, not seq-max)', async () => {
+ // The ping-pong: original create (older updatedAt), the winning delete
+ // (newest updatedAt), then a stale echoed create that re-uses the OLD
+ // updatedAt but lands at the highest SEQ. A fresh peer must pull the delete,
+ // not the stale create, or the record resurrects.
+ writeLog([
+ { seq: 1, op: 'create', type: 'links', id: 'x', record: { updatedAt: '2026-01-01T00:00:00.000Z' } },
+ { seq: 2, op: 'delete', type: 'links', id: 'x', record: { updatedAt: '2026-01-02T00:00:00.000Z' } },
+ { seq: 99, op: 'create', type: 'links', id: 'x', record: { updatedAt: '2026-01-01T00:00:00.000Z' } },
+ ]);
+
+ await migration.up({ rootDir });
+
+ const lines = readLogLines();
+ expect(lines).toHaveLength(1);
+ const terminal = lines[0];
+ expect(terminal.op).toBe('delete'); // LWW winner, NOT the seq-99 stale create
+ expect(terminal.record.updatedAt).toBe('2026-01-02T00:00:00.000Z');
+ expect(terminal.seq).toBe(99); // max seq preserved by stamping the winner
+ });
+
it('is idempotent — a second run is a no-op', async () => {
writeStore('links', {
'ghost-1': { url: 'https://x', updatedAt: '2026-01-01T00:00:00.000Z', originInstanceId: 'peer-a' },