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): preserve first-wins dedup when splitting duplicate ids

[._.]/ Adam Eivy1aa54767c31e
diff --git a/scripts/migrations/059-split-media-collections.js b/scripts/migrations/059-split-media-collections.js
index 46a1b53ba8..9ab04fb004 100644
--- a/scripts/migrations/059-split-media-collections.js
+++ b/scripts/migrations/059-split-media-collections.js
@@ -160,15 +160,19 @@ export default {
continue;
}
if (existingIds.has(id)) {
- // Already split in a prior partial run — leave it alone. The
- // monolithic record may have stale state (in-flight writes after the
- // crash), so trusting the already-split per-record file is safer.
+ // Already split — either in a prior partial run (trust the on-disk
+ // per-record file, which may hold fresher post-crash state) OR earlier
+ // in THIS loop (a duplicate id within the legacy array). For the
+ // duplicate case this preserves the old monolithic `listCollections`
+ // first-wins dedup (`seen.has(id)` → skip), so the upgrade can't flip
+ // which record survives (live vs tombstone, or item membership).
skipped += 1;
continue;
}
const recordDir = join(typeDir, id);
await mkdir(recordDir, { recursive: true });
await writeJson(join(recordDir, 'index.json'), record);
+ existingIds.add(id); // first-wins: later duplicates of this id are skipped above
written += 1;
}
diff --git a/scripts/migrations/059-split-media-collections.test.js b/scripts/migrations/059-split-media-collections.test.js
index e5032e8c84..d51104a02c 100644
--- a/scripts/migrations/059-split-media-collections.test.js
+++ b/scripts/migrations/059-split-media-collections.test.js
@@ -139,6 +139,22 @@ describe('migration 059 — split media-collections.json to per-record files', (
expect(existsSync(join(typeDir, '11111111-1111-1111-1111-111111111111', 'index.json'))).toBe(true);
});
+ it('duplicate ids: first record wins (matches the old monolithic dedup)', async () => {
+ // The legacy reader (listCollections) kept the FIRST occurrence of a
+ // duplicate id; the split must preserve that so an upgrade can't flip which
+ // record survives (e.g. a live record shadowing a later tombstone).
+ const id = '11111111-1111-1111-1111-111111111111';
+ writeJson(legacyPath, {
+ collections: [
+ collection(id, { name: 'First (wins)' }),
+ collection(id, { name: 'Second (dropped)', deleted: true, deletedAt: '2026-05-31T00:00:00.000Z' }),
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result).toEqual({ ok: true, reason: 'split', written: 1, skipped: 1, invalid: 0 });
+ expect(readJson(join(typeDir, id, 'index.json')).name).toBe('First (wins)');
+ });
+
it('reports unreadable when the legacy file is corrupted', async () => {
writeFileSync(legacyPath, 'not json');
const result = await migration.up({ rootDir });