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: keep pre-#12432 eye exam ids and match unknown-provenance copies by content (#12432)

[._.]/ Adam Eivy7bbcb2fc1f5e
diff --git a/scripts/migrations/429-eye-exam-legacy-row-ids.js b/scripts/migrations/429-eye-exam-legacy-row-ids.js
index fd8a185cde..5e5491c551 100644
--- a/scripts/migrations/429-eye-exam-legacy-row-ids.js
+++ b/scripts/migrations/429-eye-exam-legacy-row-ids.js
@@ -1,10 +1,12 @@
-/** Re-key legacy eye exams to content-derived ids and collapse their duplicates
+/** Collapse duplicated legacy eye exams and give id-less ones their content id
* (#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. Every row without `createdAt` predates minted ids, so its random stamp
- * is replaced by the `legacy-` id every peer derives from the row's content, and
- * rows that land on the same id are one exam. Rows created since keep their id.
- * Input-gated on eyes.json; ships no seed. Idempotent: re-keyed rows keep their id. */
+ * exam. Rows without `createdAt` whose content is identical are copies of one
+ * exam: they collapse to one row carrying the `legacy-` id every peer derives
+ * from that content. 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.
+ * Idempotent: the output has no id-less rows and no duplicates left. */
import { join } from 'node:path';
import { readJsonOrNull, writeJsonAtomic } from './_lib.js';
import { normalizeLegacyRows } from '../../server/lib/meatspaceRowIdentity.js';
diff --git a/scripts/migrations/429-eye-exam-legacy-row-ids.test.js b/scripts/migrations/429-eye-exam-legacy-row-ids.test.js
index d38b39e458..79679d045f 100644
--- a/scripts/migrations/429-eye-exam-legacy-row-ids.test.js
+++ b/scripts/migrations/429-eye-exam-legacy-row-ids.test.js
@@ -16,14 +16,17 @@ const seed = async (doc) => {
};
const read = async () => JSON.parse(await readFile(eyesPath(), 'utf8'));
-it('collapses random-stamped copies of one legacy exam, re-keys singles, keeps minted rows, and is idempotent', async () => {
+it('collapses copies of one legacy exam to its content id, stamps id-less rows, keeps every other id, and is idempotent', async () => {
const legacy = { date: '2024-05-01', leftSphere: -1, rightSphere: -1.25 };
- const single = { date: '2023-02-01', leftSphere: -0.5 };
+ const idless = { date: '2023-02-01', leftSphere: -0.5 };
+ // A pre-#12432 addEyeExam row: random id, no createdAt, edited since. Its id is kept.
+ const preFixEdited = { id: '22222222-0000-4000-8000-000000000002', date: '2023-09-01', leftSphere: -0.75 };
const minted = { id: 'exam-minted', createdAt: '2026-01-01T00:00:00.000Z', date: '2026-01-01', leftSphere: -2 };
await seed({
exams: [
{ id: '11111111-0000-4000-8000-000000000001', ...legacy }, // stamped on this machine
- { id: '22222222-0000-4000-8000-000000000002', ...single },
+ { ...idless },
+ preFixEdited,
{ id: '33333333-0000-4000-8000-000000000003', ...legacy }, // the peer's stamp, appended by a sync
minted,
],
@@ -34,7 +37,8 @@ it('collapses random-stamped copies of one legacy exam, re-keys singles, keeps m
const doc = await read();
expect(doc.note).toBe('kept');
expect(doc.exams).toEqual([
- { id: legacyRowId(single), ...single },
+ { id: legacyRowId(idless), ...idless },
+ preFixEdited,
{ id: legacyRowId(legacy), ...legacy },
minted,
]);
diff --git a/server/lib/README.md b/server/lib/README.md
index baefe57814..7b5f773f13 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -250,7 +250,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `modelInventory.js` | Identity AND durable shape of a downloaded-model inventory row. `modelInventoryId(backend, key)` mints the `hf:` / `lora:` / `ollama:` / `lmstudio:` id and `parseModelInventoryId` splits it back on the FIRST colon (an Ollama tag like `qwen3:8b` carries one of its own). `hfInventoryRow` / `loraInventoryRow` / `localModelInventoryRow` build the row a backend contributes, and `presentInventoryRow` re-derives the live fields (residency, inventory-verified) that a STORED row can never answer. Shared by the scan that mints rows from disk (`services/systemResources.js`) and the manifest that records them at the install chokepoints (`services/modelManifest.js`) — a drifted copy of a row's risk, manage path or delete action is a delete button pointed at the wrong place. Pure and dependency-free, so the install services reach the builders without pulling the scan's module graph. |
| `videoFailure.js` | `normalizeVideoFailure` classifies and scrubs local video causes; `createVideoDiagnosticTail` retains bounded stdout/stderr diagnostics without grouping bare exit codes. |
| `maintenanceSequence.js` | The maintenance ladder shared by both runners: `MAINTENANCE_TASK_ORDER` / `MAINTENANCE_ORDER_GUIDANCE`, the interleaved `MAINTENANCE_SEQUENCE_TYPES` (a perpetual `claim-issue` drain between every pair of audits), `maintenanceStepParams` (audits file issues, documentation does the work), and `buildMaintenanceSteps` (fix mode runs audits directly with one final drain; issue filing can omit claim jobs) which emits quota-burn-shaped run-once steps so `quotaBurnInvoke.js` can run a step from the Schedule tab's manual run or a Quota Burn family plan alike. Pure. |
-| `meatspaceRowIdentity.js` | Cross-machine identity for MeatSpace health rows (#12432): `legacyRowId` derives a `legacy-<sha256>` id from a legacy row's content, `legacyRowIdentity` / `contentRowIdentity` key rows for the `row-union` snapshot merge, `preferRowCopy` is the total per-row `updatedAt` LWW order, `normalizeLegacyRows` re-keys and collapses legacy eye exams (also migration 429), and `ROW_TOMBSTONES_KEY` names the per-file deletion markers. Pure. |
+| `meatspaceRowIdentity.js` | Cross-machine identity for MeatSpace health rows (#12432): `legacyRowId` derives a `legacy-<sha256>` key from a row's content, `rowIdentityKeys` lists the keys a row answers to (its id, plus the content key when it has no `createdAt`), `mergeRowCopies` collapses copies of one row for the `row-union` snapshot merge, `preferRowCopy` is the total per-row `updatedAt` LWW order, `normalizeLegacyRows` stamps and dedupes legacy eye exams (also migration 429), and `ROW_TOMBSTONES_KEY` names the per-file deletion markers. Pure. |
| `meetingUrl.js` | Selects the one cacheable join URL (`selectMeetingUrl`) out of a raw Google Calendar event — first http(s) `video` entry point, else `hangoutLink` — and owns the shared `MEETING_URL_MAX` bound. Three-state: `undefined` = the producer never described conferencing, `null` = described with none usable. |
| `minimaxH3Memory.js` | The declared weight-placement table for the three MiniMax H3 entries (#5420) — H3 is the one video model family whose components fit nowhere unassisted, so "does this box have enough" is a render gate, not a UI fact. `MINIMAX_H3_MEMORY_PROFILES` maps entry id → `{ shippedRepo, shippedRevision, profiles }`, each profile carrying an honest `minMemoryGb` host floor and (CUDA only) a `minVramGb` device floor, ordered best-first. Every capacity number is HOISTED from what already existed — the CUDA tiers are `resolve_offload_profile()`'s own thresholds in `scripts/generate_minimax_h3_cuda.py`, the host floors are the entries' `memoryGb` — the sole new number being `MINIMAX_H3_HOST_RESERVE_GB`, a policy reserve held back for the OS. `applyMiniMaxH3MemoryProfiles(list)` is the load-time backfill (twin of migration 317) and guards BOTH `repo` and `revision`, like the speed-profile decorator. `selectMiniMaxH3MemoryProfile({ model, totalMemoryGb })` picks the best profile the HOST can hold (VRAM is the runner's call — the server has no synchronous device view); `miniMaxH3MemoryDeclineReason()` RETURNS the fail-closed reason so the render path can 400 it and a status surface can show it, and returns `null` on an UNMEASURED host — "not measured" is a deferral to the runner, never the same as zero. `validateMiniMaxH3MemoryProfileTable` / `sanitizeMiniMaxH3MemoryProfiles` warn + strip a hand-edited table (NaN floor, duplicate/reserved id, mis-ordered tiers) at load. |
| `videoContinuity.js` | How chunk N+1 of a chained video render is conditioned on chunk N. `resolveContextFrames(requested)` normalizes the tail-window size (absent → `DEFAULT_CONTEXT_FRAMES` = 22 ≈ 1s @ 24fps; an explicit `0` is preserved as "last frame only", NOT collapsed into the default) and clamps to `MIN_CONTEXT_FRAMES`..`MAX_CONTEXT_FRAMES`. `resolveContinuityStrategy({model, contextFrames})` picks `'window'` (LTX-2 `extend_from_video` conditioned on the prior chunk's last N frames — motion, not just a pose) or `'frame'` (extract the last frame, run i2v), degrading to `'frame'` on any runtime outside `CONTEXT_WINDOW_RUNTIMES` rather than rejecting. `extendLatentFrames` / `extendedPixelFrames` convert across the VAE's `LATENT_FRAME_STRIDE` (8 pixel frames per latent), `contextPrefixFrames({totalFrames, extendLatents})` measures how much of an extend render is echoed context to trim back off before stitching (0 = leave it alone), `tailWindowStartFrame` gives the cut index for the window itself, and `planExtendSplice({sourceFrames, outputFrames, extendLatents})` decides where a plain native Extend joins the ORIGINAL source to the render's new frames (`null` = layout doesn't verify, keep the model output). Pure — importable from `prepareParams.js` without dragging in `local.js`. Mirrored for the picker in `client/src/lib/videoGenParams.js`, pinned by `videoContinuity.parity.test.js`. |
diff --git a/server/lib/meatspaceRowIdentity.js b/server/lib/meatspaceRowIdentity.js
index 7242d60138..a6771e4f79 100644
--- a/server/lib/meatspaceRowIdentity.js
+++ b/server/lib/meatspaceRowIdentity.js
@@ -2,16 +2,17 @@
* Cross-machine identity for MeatSpace health rows (blood / epigenetic / eye
* records) that federate through the `row-union` snapshot merge (#12432).
*
- * A row created before rows carried identity has no `id`, or an `id` a machine
- * stamped with `randomUUID()` on first read — two machines holding the same
- * legacy row then gave it two different ids, and every sync duplicated it. A
- * legacy row's identity is therefore derived from its content
- * (`legacy-<sha256>`), so every machine names the same legacy row the same way.
- * Rows created since carry a minted id plus `createdAt`; the id is their
- * identity and two identical rows with different ids stay two records.
+ * Before #12432 an id-less eye exam was stamped with `randomUUID()` on each
+ * machine's first read, so two machines gave the same exam two ids and every
+ * sync appended the peer's copy. Rows created since carry `createdAt`, and
+ * their `id` alone is their identity. A row without `createdAt` has unknown
+ * 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. Two copies that meet only through the
+ * content key converge on that key as their id.
*
- * Pure. Migration 429 imports `normalizeLegacyRows`, so the derivation here IS
- * the on-disk id: changing it would re-key every peer's rows differently.
+ * Pure. Migration 429 calls `normalizeLegacyRows`, so `legacyRowId` IS an
+ * on-disk id: changing it would key every peer's rows differently.
*/
import { createHash } from 'crypto';
@@ -26,8 +27,6 @@ export const ROW_TOMBSTONES_KEY = 'rowTombstones';
const hasStringId = (row) => typeof row?.id === 'string' && row.id !== '';
-export const isLegacyRowId = (id) => typeof id === 'string' && id.startsWith(LEGACY_ROW_ID_PREFIX);
-
/** Deterministic id for a legacy row: a hash of its content without identity or clock fields. */
export function legacyRowId(row) {
const { id: _id, createdAt: _createdAt, updatedAt: _updatedAt, ...content } = row;
@@ -35,20 +34,14 @@ export function legacyRowId(row) {
return `${LEGACY_ROW_ID_PREFIX}${digest.slice(0, 24)}`;
}
-/**
- * The identity of a row whose file once stamped random ids (eyes.json). A row
- * without `createdAt` predates minted identity, so a non-legacy id on it is a
- * per-machine random stamp and is replaced by the content-derived id. An
- * edited legacy row keeps its `legacy-` id even though its content moved on.
- */
-export function legacyRowIdentity(row) {
- if (hasStringId(row) && (row.createdAt !== undefined || isLegacyRowId(row.id))) return row.id;
- return legacyRowId(row);
-}
-
-/** Identity for rows that never had random stamps: their own id, else their content. */
-export function contentRowIdentity(row) {
- return hasStringId(row) ? row.id : legacyRowId(row);
+/** Every key a row answers to; the content key last. A delete tombstones all of them. */
+export function rowIdentityKeys(row) {
+ const keys = hasStringId(row) ? [row.id] : [];
+ if (row.createdAt === undefined) {
+ const contentKey = legacyRowId(row);
+ if (!keys.includes(contentKey)) keys.push(contentKey);
+ }
+ return keys;
}
/** A row's newest clock: `updatedAt`, else `createdAt` (string, or undefined). */
@@ -73,27 +66,56 @@ export function preferRowCopy(a, b) {
}
/**
- * Re-key every row of an id-stamping file to `legacyRowIdentity` and collapse
- * rows that share an identity to their preferred copy (content-identical legacy
- * rows are one record). Order follows each identity's first occurrence.
- * Non-object rows are kept as they are.
+ * Collapse copies of one record across `localRows` then `remoteRows`. Each
+ * entry of the result is `{ row, keys }` (`keys` is null for a non-object row,
+ * which is kept only from the local side). Copies resolve to `preferRowCopy`;
+ * copies with different ids that met through the content key take that key as
+ * their id.
+ *
+ * `stampIds` gives an id-less row its content key as its id (eyes.json, whose
+ * rows are addressed by id). `keepLocalDuplicates` keeps rows of `localRows`
+ * that share a key as separate rows (blood/epigenetic files, whose id-less rows
+ * were never deduped locally); only a remote copy is matched against them.
*/
-export function normalizeLegacyRows(rows) {
- const out = [];
- const indexById = new Map();
- for (const row of Array.isArray(rows) ? rows : []) {
- if (!isPlainObject(row)) { out.push(row); continue; }
- const id = legacyRowIdentity(row);
- const keyed = row.id === id ? row : { ...row, id };
- const index = indexById.get(id);
- if (index === undefined) {
- indexById.set(id, out.length);
- out.push(keyed);
- } else {
- out[index] = preferRowCopy(out[index], keyed);
+export function mergeRowCopies(localRows, remoteRows = [], { stampIds = false, keepLocalDuplicates = false } = {}) {
+ const entries = [];
+ const indexByKey = new Map();
+ const register = (keys, index) => {
+ for (const key of keys) if (!indexByKey.has(key)) indexByKey.set(key, index);
+ };
+ const add = (row, fromLocal) => {
+ if (!isPlainObject(row)) {
+ if (fromLocal) entries.push({ row, keys: null });
+ return;
}
- }
- const changed = out.length !== (Array.isArray(rows) ? rows.length : 0)
- || out.some((row, i) => row !== rows[i]);
+ const keys = rowIdentityKeys(row);
+ const keyed = stampIds && !hasStringId(row) ? { ...row, id: keys[keys.length - 1] } : row;
+ const matchKey = keys.find((key) => indexByKey.has(key));
+ if (matchKey === undefined || (keepLocalDuplicates && fromLocal)) {
+ register(keys, entries.length);
+ entries.push({ row: keyed, keys: new Set(keys) });
+ return;
+ }
+ const entry = entries[indexByKey.get(matchKey)];
+ const preferred = preferRowCopy(entry.row, keyed);
+ const id = entry.row.id === keyed.id ? preferred.id : matchKey;
+ entry.row = preferred.id === id ? preferred : { ...preferred, id };
+ for (const key of [...keys, ...rowIdentityKeys(entry.row)]) entry.keys.add(key);
+ register(entry.keys, indexByKey.get(matchKey));
+ };
+ for (const row of Array.isArray(localRows) ? localRows : []) add(row, true);
+ for (const row of Array.isArray(remoteRows) ? remoteRows : []) add(row, false);
+ return entries;
+}
+
+/**
+ * Give id-less eye exams their content id and collapse copies of one exam (the
+ * duplicates pre-#12432 syncs created). Returns `{ rows, changed }`; rows keep
+ * the order of each record's first occurrence.
+ */
+export function normalizeLegacyRows(rows) {
+ const list = Array.isArray(rows) ? rows : [];
+ const out = mergeRowCopies(list, [], { stampIds: true }).map((entry) => entry.row);
+ const changed = out.length !== list.length || out.some((row, i) => row !== list[i]);
return { rows: out, changed };
}
diff --git a/server/services/dataSync.js b/server/services/dataSync.js
index e4073ee5d8..1e93876079 100644
--- a/server/services/dataSync.js
+++ b/server/services/dataSync.js
@@ -17,13 +17,7 @@ import { snapshotChecksum } from '../lib/snapshotChecksum.js';
import { parseTsMs } from '../lib/lwwTimestamp.js';
import { isTombstoned, mergeTombstones, pruneTombstones } from '../lib/tombstones.js';
import { GOAL_TOMBSTONES_KEY, reconcileGoalTombstones } from './identity/goalTombstones.js';
-import {
- ROW_TOMBSTONES_KEY,
- contentRowIdentity,
- legacyRowIdentity,
- preferRowCopy,
- rowLiveStamp,
-} from '../lib/meatspaceRowIdentity.js';
+import { ROW_TOMBSTONES_KEY, mergeRowCopies, rowLiveStamp } from '../lib/meatspaceRowIdentity.js';
import {
DAILY_LOG_TOMBSTONES_KEY,
dailyLogEventLiveStamp,
@@ -135,12 +129,12 @@ const STORY_BUILDER_EPOCH_KEY = '__storyBuilderEpoch';
// Blood/epigenetic/eye records merge by row identity (`mergeRowUnionFile`), not
// by `date`: two labs on one day, a retest, or left/right exams entered
-// separately share a date (#10910). Identity is the row's minted id, or for a
-// legacy row a content-derived `legacy-` id every machine computes alike
-// (`lib/meatspaceRowIdentity.js`, #12432). Copies of one row resolve by per-row
+// separately share a date (#10910). Identity is the row's id, plus for a row of
+// unknown provenance (no `createdAt`) a content-derived `legacy-` key every
+// machine computes alike (`lib/meatspaceRowIdentity.js`, #12432). Copies of one row resolve by per-row
// `updatedAt` LWW, and a delete travels as a `rowTombstones` entry, so edits and
// deletes reach peers at `PORTOS_SCHEMA_VERSIONS.meatspace` 2. `legacyIds` marks
-// the file whose pre-#12432 readers stamped random ids on legacy rows.
+// the file whose rows are addressed by id (id-less rows get their content key).
const MEATSPACE_FILES = {
'daily-log.json': { type: 'daily-log' }, // mergeDailyLogFile
'blood-tests.json': { type: 'row-union', arrayKey: 'tests' },
@@ -416,40 +410,19 @@ function mergeArraysByKey(localArr, remoteArr, idField, timestampField, mergeRec
* Merge a peer's row-union file (blood / epigenetic / eye records) into the
* local one. Pure: returns the file to persist.
*
- * Rows match by identity (`legacyRowIdentity` for an id-stamping file, else
- * `contentRowIdentity`); copies of one row resolve to `preferRowCopy`, so an
- * edit stamped with `updatedAt` wins on every machine. Local rows that share an
- * identity in a non-stamping file are kept as they are — only a peer's copy is
- * deduped against them — so the merge never drops a local row it cannot tell
- * apart. Tombstones union both ways and drop a row unless it was edited after
- * the delete; a row that outlives its tombstone prunes it.
+ * Copies of one row (`mergeRowCopies`: same id, or same content key for a row
+ * without `createdAt`) resolve to the newer `updatedAt`, so an edit wins on
+ * every machine. Tombstones union both ways and drop a row when any of its keys
+ * is tombstoned, unless it was edited after the delete; a row that outlives its
+ * tombstone prunes it.
*/
function mergeRowUnionFile(localFile, remoteFile, { arrayKey, legacyIds = false }) {
const local = isPlainObject(localFile) ? localFile : {};
const remote = isPlainObject(remoteFile) ? remoteFile : {};
- const identity = legacyIds ? legacyRowIdentity : contentRowIdentity;
- const rows = [];
- const keys = [];
- const indexByKey = new Map();
- const add = (row, fromLocal) => {
- if (!isPlainObject(row)) {
- if (fromLocal) { rows.push(row); keys.push(null); }
- return;
- }
- const key = identity(row);
- const keyed = legacyIds && row.id !== key ? { ...row, id: key } : row;
- const index = indexByKey.get(key);
- if (index !== undefined && (legacyIds || !fromLocal)) {
- rows[index] = preferRowCopy(rows[index], keyed);
- return;
- }
- if (index === undefined) indexByKey.set(key, rows.length);
- rows.push(keyed);
- keys.push(key);
- };
- for (const row of Array.isArray(local[arrayKey]) ? local[arrayKey] : []) add(row, true);
- for (const row of Array.isArray(remote[arrayKey]) ? remote[arrayKey] : []) add(row, false);
-
+ const entries = mergeRowCopies(local[arrayKey], remote[arrayKey], {
+ stampIds: legacyIds,
+ keepLocalDuplicates: !legacyIds,
+ });
const { merged: unionTombstones } = mergeTombstones(
local[ROW_TOMBSTONES_KEY],
remote[ROW_TOMBSTONES_KEY],
@@ -457,14 +430,14 @@ function mergeRowUnionFile(localFile, remoteFile, { arrayKey, legacyIds = false
);
const kept = [];
const liveStamps = [];
- rows.forEach((row, i) => {
- if (keys[i] !== null) {
+ for (const { row, keys } of entries) {
+ if (keys) {
const stamp = rowLiveStamp(row);
- if (isTombstoned(unionTombstones, keys[i], stamp, 'id')) return;
- liveStamps.push({ id: keys[i], stamp });
+ if ([...keys].some((key) => isTombstoned(unionTombstones, key, stamp, 'id'))) continue;
+ for (const key of keys) liveStamps.push({ id: key, stamp });
}
kept.push(row);
- });
+ }
const tombstones = pruneTombstones(unionTombstones, liveStamps, { keyField: 'id', timestampField: 'stamp' });
// Stable by date; same-date rows keep local-then-remote order.
kept.sort((a, b) => (a?.date || '').localeCompare(b?.date || ''));
diff --git a/server/services/dataSync.meatspace.test.js b/server/services/dataSync.meatspace.test.js
index 5587f9c91e..291962b4b8 100644
--- a/server/services/dataSync.meatspace.test.js
+++ b/server/services/dataSync.meatspace.test.js
@@ -130,6 +130,27 @@ describe('applyRemote meatspace eyes.json identity', () => {
expect(exams).toEqual([{ id: legacyRowId(legacy), ...legacy }]);
});
+ // An exam minted before #12432 has an id but no createdAt. Two machines that
+ // each edited it keep one row (its id), not one per edit.
+ it('keeps one row for a pre-#12432 exam edited differently on two machines', async () => {
+ const mintedBeforeFix = { id: 'exam-old', date: '2025-03-01', leftSphere: -1 };
+ const editedHere = { ...mintedBeforeFix, leftSphere: -1.25 };
+ const editedThere = { ...mintedBeforeFix, leftSphere: -1.5 };
+ await persist('eyes.json', { exams: [editedHere] });
+ await apply('eyes.json', { exams: [editedThere] });
+ const exams = (await readDoc('eyes.json')).exams;
+ expect(exams).toHaveLength(1);
+ expect(exams[0].id).toBe('exam-old');
+ });
+
+ it('keeps a legacy exam deleted before the copies converged, though the peer stamped it differently', async () => {
+ const legacy = { date: '2025-04-01', rightSphere: -2 };
+ await persist('eyes.json', { exams: [{ id: 'stamp-here', ...legacy }] });
+ await health.removeEyeExam('stamp-here');
+ expect((await apply('eyes.json', { exams: [{ id: 'stamp-there', ...legacy }] })).applied).toBe(false);
+ expect((await readDoc('eyes.json')).exams).toEqual([]);
+ });
+
it('resolves two copies of one exam to the newer edit on either side', async () => {
const original = { id: 'exam-x', createdAt: minted, date: '2026-05-01', leftSphere: -1 };
const edited = { ...original, leftSphere: -1.5, updatedAt: '2026-06-01T00:00:00.000Z' };
diff --git a/server/services/meatspaceHealth.js b/server/services/meatspaceHealth.js
index 4c9dff0fa7..8130c56c5a 100644
--- a/server/services/meatspaceHealth.js
+++ b/server/services/meatspaceHealth.js
@@ -12,7 +12,7 @@ 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, ROW_TOMBSTONES_KEY } from '../lib/meatspaceRowIdentity.js';
+import { normalizeLegacyRows, rowIdentityKeys, ROW_TOMBSTONES_KEY } from '../lib/meatspaceRowIdentity.js';
import { recordTombstone } from '../lib/tombstones.js';
import {
isMortalLoomEnabled,
@@ -149,10 +149,10 @@ const readLocalEyes = async () => {
};
/**
- * Local eyes.json with every row carrying its identity. MUST run inside
- * `queueHealthWrite`: a legacy row (no id, or a pre-#12432 random stamp) is
- * re-keyed to its content-derived id and persisted once, so the ids the client
- * reads back match the ones every peer computes for the same row.
+ * Local eyes.json with every row id-stamped. MUST run inside `queueHealthWrite`:
+ * an id-less legacy row gets its content-derived id (the one every peer computes
+ * for the same row) and the file is persisted once, so the ids the client reads
+ * back are the ones later edits/deletes resolve.
*/
async function loadLocalEyeExamsQueued() {
const { data, exams } = await readLocalEyes();
@@ -171,7 +171,7 @@ export async function getEyeExams() {
const ml = await mlArrayIfEnabled('eyeExams');
if (ml) return { exams: [...ml].sort(byDate) };
const { data, exams } = await readLocalEyes();
- // Only a legacy file needs the (queued) re-keying write; the common read stays lock-free.
+ // Only a legacy file needs the (queued) stamping write; the common read stays lock-free.
if (normalizeLegacyRows(exams).changed) return withoutTombstones(await queueHealthWrite(loadLocalEyeExamsQueued));
return withoutTombstones({ ...data, exams });
}
@@ -191,7 +191,7 @@ export async function addEyeExam(exam) {
console.log(`👁️ Eye exam added for ${stored.date} (MortalLoom)`);
return stored;
}
- // `createdAt` marks a minted id: without it the row reads as legacy and is re-keyed by content.
+ // `createdAt` marks a minted id: without it the row also matches peers by content.
const stored = { ...exam, id: exam.id || randomUUID(), createdAt: new Date().toISOString() };
await queueHealthWrite(async () => {
const data = await loadLocalEyeExamsQueued();
@@ -244,8 +244,12 @@ export async function removeEyeExam(id) {
const found = data.exams.find(e => e.id === id);
if (!found) return null;
data.exams = data.exams.filter(e => e.id !== id);
- // The marker keeps a peer that still holds the row from syncing it back.
- data[ROW_TOMBSTONES_KEY] = recordTombstone(data[ROW_TOMBSTONES_KEY], id, { keyField: 'id' });
+ // 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();
+ for (const key of rowIdentityKeys(found)) {
+ data[ROW_TOMBSTONES_KEY] = recordTombstone(data[ROW_TOMBSTONES_KEY], key, { keyField: 'id', deletedAt });
+ }
await writeLocal(EYES_FILE, data);
return found;
});
diff --git a/server/services/meatspaceHealth.test.js b/server/services/meatspaceHealth.test.js
index 1ffcc2ba52..bddac42da8 100644
--- a/server/services/meatspaceHealth.test.js
+++ b/server/services/meatspaceHealth.test.js
@@ -291,14 +291,21 @@ describe('eye exams by id', () => {
const legacy = { date: '2025-01-01', leftSphere: -1.5 };
disk = { exams: [{ ...legacy }] };
const first = (await getEyeExams()).exams[0].id;
- // A second machine holding the same row, or one this machine stamped with a
- // random id before #12432 (no createdAt), lands on the same id.
- disk = { exams: [{ ...legacy, id: '4f1c2a9e-0000-4000-8000-000000000000' }] };
+ // A second machine stamping its own copy of the same row lands on the same id.
+ disk = { exams: [{ ...legacy }] };
const second = (await getEyeExams()).exams[0].id;
expect(first).toMatch(/^legacy-/);
expect(second).toBe(first);
});
+ it('tombstones a legacy exam under its content key too, so a peer copy with another id stays deleted', async () => {
+ disk = { exams: [{ id: 'random-stamp-a', date: '2025-01-01', leftSphere: -1 }] };
+ await removeEyeExam('random-stamp-a');
+ expect(disk.rowTombstones.map(t => t.id).sort()).toEqual(
+ ['random-stamp-a', expect.stringMatching(/^legacy-/)].sort(),
+ );
+ });
+
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));