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: let eye exam edits and deletes find rows by retired ids and content keys (#12432)
[._.]/ Adam Eivy4ae3937f1dad
diff --git a/scripts/migrations/429-eye-exam-legacy-row-ids.js b/scripts/migrations/429-eye-exam-legacy-row-ids.jsindex 67eff3f0ed..768a6dc4bf 100644--- a/scripts/migrations/429-eye-exam-legacy-row-ids.js+++ b/scripts/migrations/429-eye-exam-legacy-row-ids.js@@ -2,7 +2,7 @@* (#12432). Before this, each machine stamped an id-less eyes.json row with its* own `randomUUID()`, so syncing peers appended each other's copy of the same* exam. Rows without `createdAt` whose content is identical are copies of one- * exam: they collapse to one row under the smallest of their ids, the others+ * exam: they collapse to one row under the smallest minted id, the others* kept as `aliasIds`. A single row keeps its id — it may have been minted before* #12432 and edited — and still meets a peer's differently-stamped copy through* the sync merge's content key. Input-gated on eyes.json; ships no seed.diff --git a/server/lib/meatspaceRowIdentity.js b/server/lib/meatspaceRowIdentity.jsindex c506db3b7e..962a531a2a 100644--- a/server/lib/meatspaceRowIdentity.js+++ b/server/lib/meatspaceRowIdentity.js@@ -9,8 +9,9 @@* provenance — an id-less legacy row, a random stamp, or an id minted before* #12432 — so it answers to its own id AND to `legacy-<sha256>` of its content,* which every machine computes alike. Copies of one record that carry different- * ids converge on the smallest of them and keep the rest as `aliasIds`, so an- * edit or delete still addressed by a retired id finds the row.+ * ids converge on the smallest non-content id and keep the rest (plus content+ * keys an edit replaced) as `aliasIds`, so an edit, delete or stale copy still+ * addressed by a retired key finds the row.** Pure. Migration 429 calls `normalizeLegacyRows`, so `legacyRowId` IS an* on-disk id: changing it would key every peer's rows differently.@@ -40,6 +41,21 @@ const rowIds = (row) => [...(Array.isArray(row.aliasIds) ? row.aliasIds.filter((id) => typeof id === 'string' && id !== '') : []),];+/** True when `id` is the row's id or one of its aliases (a mutation addressed by a retired id). */+export const rowAnswersTo = (row, id) => isPlainObject(row) && rowIds(row).includes(id);++/**+ * `after` (an edit of `before`) keeping `before`'s content key as an alias when+ * the edit changed it, so a peer's unedited copy of a row of unknown provenance+ * still finds the edited row.+ */+export function retainContentKey(before, after) {+ if (after.createdAt !== undefined) return after;+ const previous = legacyRowId(before);+ if (previous === legacyRowId(after) || rowIds(after).includes(previous)) return after;+ return { ...after, aliasIds: [...(Array.isArray(after.aliasIds) ? after.aliasIds : []), previous] };+}+/** Every key a row answers to: id, aliases, then the content key. A delete tombstones all of them. */export function rowIdentityKeys(row) {const keys = [...new Set(rowIds(row))];@@ -72,16 +88,25 @@ export function preferRowCopy(a, b) {}// One record from a group of its copies: the preferred copy's fields, the-// smallest id as its id and every other id as an alias. Both choices are-// order-free, so every machine reduces the same copies to the same row.+// smallest minted or stamped id as its id, and every other id — plus the content key of any copy+// of unknown provenance the winner's content replaced — as an alias. Every+// choice is order-free, so every machine reduces the same copies to the same row.function reduceCopies(members) {if (members.length === 1) return members[0];const winner = members.map((m) => m.row).reduce(preferRowCopy);- const ids = [...new Set(members.flatMap((m) => rowIds(m.row)))].sort();+ const winnerContentKey = legacyRowId(winner);+ const replacedContentKeys = members+ .filter((m) => m.row.createdAt === undefined)+ .map((m) => legacyRowId(m.row))+ .filter((key) => key !== winnerContentKey);+ const ids = [...new Set([...members.flatMap((m) => rowIds(m.row)), ...replacedContentKeys])].sort();let row = winner;if (ids.length > 0) {+ // A minted or stamped id outranks a content key, which only names a version.+ const id = ids.find((key) => !key.startsWith(LEGACY_ROW_ID_PREFIX)) ?? ids[0];+ const aliasIds = ids.filter((key) => key !== id);const { aliasIds: _aliasIds, ...fields } = winner;- row = ids.length > 1 ? { ...fields, id: ids[0], aliasIds: ids.slice(1) } : { ...fields, id: ids[0] };+ row = aliasIds.length > 0 ? { ...fields, id, aliasIds } : { ...fields, id };}const keys = new Set([...members.flatMap((m) => [...m.keys]), ...rowIdentityKeys(row)]);return { row, keys };@@ -95,9 +120,10 @@ function reduceCopies(members) {* local side), in first-occurrence order.** `stampIds` gives an id-less row its content key as its id (eyes.json, whose- * rows are addressed by id). `keepLocalDuplicates` keeps a group's local rows- * as they are and drops its remote copies (blood/epigenetic files: id-less rows- * keyed by content alone, which were never deduped locally).+ * rows are addressed by id). `keepLocalDuplicates` keeps the local rows of a+ * group of id-less rows as they are and drops its remote copies (blood and+ * epigenetic rows, keyed by content alone, were never deduped locally; such a+ * group's rows are identical, so nothing is lost). A group with an id reduces.*/export function mergeRowCopies(localRows, remoteRows = [], { stampIds = false, keepLocalDuplicates = false } = {}) {const items = [];@@ -138,7 +164,8 @@ export function mergeRowCopies(localRows, remoteRows = [], { stampIds = false, kfor (const members of groups.values()) {if (members[0].keys === null) { entries.push({ row: members[0].row, keys: null }); continue; }const locals = members.filter((m) => m.local);- if (keepLocalDuplicates && locals.length > 0) entries.push(...locals.map(({ row, keys }) => ({ row, keys })));+ const contentOnly = members.every((m) => rowIds(m.row).length === 0);+ if (keepLocalDuplicates && contentOnly && locals.length > 0) entries.push(...locals.map(({ row, keys }) => ({ row, keys })));else entries.push(reduceCopies(members.map(({ row, keys }) => ({ row, keys }))));}return entries;diff --git a/server/services/dataSync.meatspace.test.js b/server/services/dataSync.meatspace.test.jsindex aa5437f351..d1b7b383c8 100644--- a/server/services/dataSync.meatspace.test.js+++ b/server/services/dataSync.meatspace.test.js@@ -111,7 +111,9 @@ describe('applyRemote meatspace eyes.json identity', () => {expect(result.applied).toBe(true);const exams = (await readDoc('eyes.json')).exams;- expect(exams.map((e) => e.id)).toEqual([legacyRowId(values), 'twin-1', 'twin-2']);+ // The peer's stamp becomes the id; the content id this machine stamped stays an alias.+ expect(exams.map((e) => e.id)).toEqual(['stamped-copy', 'twin-1', 'twin-2']);+ expect(exams[0].aliasIds).toEqual([legacyRowId(values)]);});// #12432: each machine used to stamp a legacy row with its own random id, so the@@ -139,7 +141,24 @@ describe('applyRemote meatspace eyes.json identity', () => {const edited = { id: 'stamp-z', ...legacy, leftSphere: -1.75, updatedAt: '2026-07-01T00:00:00.000Z' };await persist('eyes.json', { exams: [edited] });await apply('eyes.json', { exams: [{ id: 'stamp-b', aliasIds: ['stamp-z'], ...legacy }] });- expect((await readDoc('eyes.json')).exams).toEqual([{ ...edited, id: 'stamp-b', aliasIds: ['stamp-z'] }]);+ // The unedited copy's content key stays reachable too, for any other peer still holding it.+ expect((await readDoc('eyes.json')).exams).toEqual([+ { ...edited, id: 'stamp-b', aliasIds: [legacyRowId(legacy), 'stamp-z'] },+ ]);+ });++ // An edit replaces a legacy exam's content, but a third peer's untouched copy,+ // stamped with yet another id, still finds it through the retained content key.+ it('matches a third peer copy to a legacy exam edited after its copies converged', async () => {+ const legacy = { date: '2025-05-05', leftSphere: -1 };+ await persist('eyes.json', { exams: [{ id: 'stamp-a', aliasIds: ['stamp-b'], ...legacy }] });+ await health.updateEyeExam('stamp-a', { leftSphere: -1.5 });++ await apply('eyes.json', { exams: [{ id: 'stamp-c', ...legacy }] });+ const exams = (await readDoc('eyes.json')).exams;+ expect(exams).toHaveLength(1);+ expect(exams[0]).toMatchObject({ id: 'stamp-a', leftSphere: -1.5 });+ expect(exams[0].aliasIds).toEqual(expect.arrayContaining(['stamp-b', 'stamp-c', legacyRowId(legacy)]));});// A row whose id matches one exam and whose content matches another joins all@@ -157,7 +176,7 @@ describe('applyRemote meatspace eyes.json identity', () => {await apply('eyes.json', { exams: [a, b] });const backward = (await readDoc('eyes.json')).exams;- expect(forward).toEqual([{ ...bridge, aliasIds: ['stamp-b'] }]);+ expect(forward).toEqual([{ ...bridge, aliasIds: [legacyRowId(a), 'stamp-b'] }]);expect(backward).toEqual(forward);});diff --git a/server/services/meatspaceHealth.js b/server/services/meatspaceHealth.jsindex 8130c56c5a..470eaed27f 100644--- a/server/services/meatspaceHealth.js+++ b/server/services/meatspaceHealth.js@@ -12,7 +12,13 @@ import { randomUUID } from 'crypto';import { atomicWrite, PATHS, ensureDir, readJSONFile, getDateString } from '../lib/fileUtils.js';import { readLocalDailyLog, mutateDailyLog } from './meatspaceDailyLog.js';import { queueHealthWrite } from './meatspaceWriteQueues.js';-import { normalizeLegacyRows, rowIdentityKeys, ROW_TOMBSTONES_KEY } from '../lib/meatspaceRowIdentity.js';+import {+ normalizeLegacyRows,+ retainContentKey,+ rowAnswersTo,+ rowIdentityKeys,+ ROW_TOMBSTONES_KEY,+} from '../lib/meatspaceRowIdentity.js';import { recordTombstone } from '../lib/tombstones.js';import {isMortalLoomEnabled,@@ -218,10 +224,13 @@ export async function updateEyeExam(id, updates) {const exam = await queueHealthWrite(async () => {const data = await loadLocalEyeExamsQueued();- const target = data.exams.find(e => e.id === id);- if (!target) return null;+ // A sync may have retired the id the client holds into `aliasIds`.+ const index = data.exams.findIndex(e => rowAnswersTo(e, id));+ if (index === -1) return null;+ const before = data.exams[index];// `updatedAt` is the per-row clock the federation merge resolves copies by.- Object.assign(target, patch, { updatedAt: new Date().toISOString() });+ const target = retainContentKey(before, { ...before, ...patch, updatedAt: new Date().toISOString() });+ data.exams[index] = target;data.exams.sort(byDate);await writeLocal(EYES_FILE, data);return target;@@ -241,9 +250,9 @@ export async function removeEyeExam(id) {const target = await queueHealthWrite(async () => {const data = await loadLocalEyeExamsQueued();- const found = data.exams.find(e => e.id === id);+ const found = data.exams.find(e => rowAnswersTo(e, id));if (!found) return null;- data.exams = data.exams.filter(e => e.id !== id);+ data.exams = data.exams.filter(e => e !== found);// The markers keep a peer that still holds the row from syncing it back —// under its content key too, for a copy the peer stamped with another id.const deletedAt = new Date().toISOString();diff --git a/server/services/meatspaceHealth.test.js b/server/services/meatspaceHealth.test.jsindex bddac42da8..be361ed748 100644--- a/server/services/meatspaceHealth.test.js+++ b/server/services/meatspaceHealth.test.js@@ -306,6 +306,14 @@ describe('eye exams by id', () => {);});+ it('edits and deletes an exam addressed by an id a sync retired into aliasIds', async () => {+ disk = { exams: [{ id: 'stamp-b', aliasIds: ['stamp-z'], createdAt: '2025-01-01T00:00:00.000Z', date: '2025-01-01', leftSphere: -1 }] };+ expect((await updateEyeExam('stamp-z', { leftSphere: -2 })).id).toBe('stamp-b');+ expect(disk.exams[0].leftSphere).toBe(-2);+ expect((await removeEyeExam('stamp-z')).id).toBe('stamp-b');+ expect(disk.exams).toEqual([]);+ });+it('stamps createdAt on a new exam and updatedAt on an edit, so peers resolve copies by clock', async () => {const exam = await addEyeExam({ date: '2025-01-01' });expect(exam.createdAt).toEqual(expect.any(String));