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
address review (claude): symmetric LWW key for body-less exercise merge
[._.]/ Adam Eivy9fd5f99260ed
diff --git a/server/services/writersRoom/local.js b/server/services/writersRoom/local.jsindex 6640137972..ca3c996cc2 100644--- a/server/services/writersRoom/local.js+++ b/server/services/writersRoom/local.js@@ -601,6 +601,7 @@ export async function createExercise({ workId = null, prompt = '', durationSeconstatus: 'running',startedAt: nowIso(),finishedAt: null,+ updatedAt: nowIso(),};await store().writeExercise(exercise);// Auto-subscribe every writersRoomExercises-enabled peer so brand-new sprints@@ -620,17 +621,22 @@ export async function finishExercise(id, { endingWords, appendedText = null } =const startingWords = existing.startingWords || 0;const resolvedEnding = endingWords ?? startingWords;const wordsAdded = Math.max(0, resolvedEnding - startingWords);+ const now = nowIso();const finished = {...existing,endingWords: resolvedEnding,wordsAdded,appendedText: appendedText ?? null,status: 'finished',- finishedAt: nowIso(),+ finishedAt: now,+ updatedAt: now,};await store().writeExercise(finished);- // The settle transition bumps finishedAt (the derived LWW key) — push it so- // subscribed peers converge on the finished state (#1645).+ // The settle transition stamps a fresh updatedAt (the LWW key) — push it so+ // subscribed peers converge on the finished state. updatedAt must be bumped+ // explicitly (not left to the finishedAt-derived fallback): a sync-seeded+ // exercise already carries a stored updatedAt the sanitizer would otherwise+ // prefer over the new finishedAt, so the finish wouldn't advance the key (#1645).emitRecordUpdated(WRITERS_ROOM_EXERCISE_KIND, id);return finished;}@@ -640,7 +646,8 @@ export async function discardExercise(id) {const existing = all.find((e) => e.id === id);if (!existing) throw notFound('Exercise');if (settled(existing)) throw badRequest('Exercise is already settled');- const discarded = { ...existing, status: 'discarded', finishedAt: nowIso() };+ const now = nowIso();+ const discarded = { ...existing, status: 'discarded', finishedAt: now, updatedAt: now };await store().writeExercise(discarded);emitRecordUpdated(WRITERS_ROOM_EXERCISE_KIND, id);return discarded;diff --git a/server/services/writersRoom/syncLogic.js b/server/services/writersRoom/syncLogic.jsindex 2a38bc737f..6b2f9ae396 100644--- a/server/services/writersRoom/syncLogic.js+++ b/server/services/writersRoom/syncLogic.js@@ -141,7 +141,16 @@ function mergeBodylessRecord(local, remoteRaw, sanitize) {const remote = sanitize(remoteRaw);if (!remote) return { next: null, inserted: false, remoteWins: false, changed: false };if (!local) return { next: remote, inserted: true, remoteWins: true, changed: true };- const remoteWins = compareNewerWins(remote.updatedAt, local.updatedAt);+ // Derive the LOCAL lww key through the SAME sanitizer as the remote so the+ // compare is symmetric. Exercises predate federation and may carry no stored+ // `updatedAt` (the sanitizer derives it from `finishedAt ?? startedAt`); the+ // stored local record is raw (unsanitized), so an `undefined` local.updatedAt+ // would lose to ANY parseable remote — `compareNewerWins` treats an+ // unparseable incumbent as always-loses — letting a STALE push clobber a+ // newer local. `sanitize(local)` can't be null here (local came from our own+ // store with a valid id); fall back to the raw key defensively.+ const localKey = sanitize(local)?.updatedAt ?? local.updatedAt;+ const remoteWins = compareNewerWins(remote.updatedAt, localKey);const next = remoteWins ? remote : local;const changed = JSON.stringify(next) !== JSON.stringify(local);return { next, inserted: false, remoteWins, changed };diff --git a/server/services/writersRoom/syncLogic.test.js b/server/services/writersRoom/syncLogic.test.jsindex 32e963b0e0..feae343008 100644--- a/server/services/writersRoom/syncLogic.test.js+++ b/server/services/writersRoom/syncLogic.test.js@@ -299,6 +299,20 @@ describe('mergeExerciseRecord', () => {expect(mergeExerciseRecord(null, exercise()).inserted).toBe(true);expect(mergeExerciseRecord(exercise(), { id: 'bogus' }).next).toBeNull();});++ it('a legacy local with NO stored updatedAt is not clobbered by a stale remote (symmetric LWW key)', () => {+ // Regression: the local key must be derived through the same sanitizer as the+ // remote. A finished local (no updatedAt, finishedAt=00:10) must beat a stale+ // running remote (no updatedAt, derived key = startedAt=00:00) — without the+ // symmetric derivation the raw `local.updatedAt` is undefined and the stale+ // remote would win unconditionally.+ const localFinished = { ...exercise(), status: 'finished', finishedAt: '2026-01-01T00:10:00.000Z' };+ delete localFinished.updatedAt;+ const staleRunning = { ...exercise(), status: 'running', finishedAt: null };+ const { remoteWins, next } = mergeExerciseRecord(localFinished, staleRunning);+ expect(remoteWins).toBe(false);+ expect(next.status).toBe('finished');+ });});describe('body-less wire form', () => {