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

fix: sort an unparseable clock below every real one when collapsing duplicates

[._.]/ Adam Eivyd6d618c1ecdf
diff --git a/server/services/memorySync.db.test.js b/server/services/memorySync.db.test.js
index b11f6cf342..8e34b1cfeb 100644
--- a/server/services/memorySync.db.test.js
+++ b/server/services/memorySync.db.test.js
@@ -120,6 +120,19 @@ describe.skipIf(!runDb)('memorySync.applyRemoteChanges', () => {
expect(await contentOf(ID_A)).toBe('content @ 2026-06-09T00:00:00.000Z');
});
+ it('keeps the parseable copy when a duplicate carries a malformed clock', async () => {
+ // A malformed `updatedAt` must not win the collapse: it would be handed to a
+ // timestamptz column and fail the statement, losing a row we could have
+ // applied intact. NaN compares false against everything, so the comparator
+ // has to sort it below a real clock explicitly rather than by accident.
+ const result = await memorySync.applyRemoteChanges([
+ remoteMemory(ID_A, '2026-09-01T00:00:00.000Z'),
+ remoteMemory(ID_A, 'not-a-timestamp'),
+ ]);
+ expect(await contentOf(ID_A)).toBe('content @ 2026-09-01T00:00:00.000Z');
+ expect(result).toMatchObject({ inserted: 1, skipped: 1 });
+ });
+
it('still refuses a remote row older than the local one (last-writer-wins)', async () => {
await memorySync.applyRemoteChanges([remoteMemory(ID_A, '2026-07-10T00:00:00.000Z')]);
diff --git a/server/services/memorySync.js b/server/services/memorySync.js
index 9c45f67b66..62b8412eca 100644
--- a/server/services/memorySync.js
+++ b/server/services/memorySync.js
@@ -102,12 +102,21 @@ export async function applyRemoteChanges(incomingMemories) {
// The survivor is the newest copy by `updated_at`, not simply the last one:
// that is the winner the ON CONFLICT clause's last-writer-wins rule picks when
// the same duplicates arrive in separate batches, so how a peer happened to
- // order its payload can't change the outcome. An unparseable clock compares
- // false and yields to the later occurrence.
+ // order its payload can't change the outcome. Ties keep the first copy, matching
+ // the SQL's strict `>` (an equal clock is not a newer write).
+ //
+ // An unparseable clock sorts BELOW every real one rather than NaN-comparing
+ // false and thereby winning: a peer that sends one good and one malformed copy
+ // of a row must keep the good one, or the batch carries a timestamp Postgres
+ // rejects and the apply fails on a row we already had intact.
+ const lwwClock = (mem) => {
+ const at = Date.parse(mem?.updatedAt);
+ return Number.isNaN(at) ? -Infinity : at;
+ };
const deduped = dedupeByKey(
incomingMemories,
(mem) => mem.id,
- (held, next) => (Date.parse(held.updatedAt) > Date.parse(next.updatedAt) ? held : next),
+ (held, next) => (lwwClock(held) >= lwwClock(next) ? held : next),
);
// A collapsed duplicate lost last-writer-wins, which is exactly what `skipped`
// counts — so the three tallies still sum to what the peer sent.