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 (self): re-key journal memory-bridge entries uuid->date in migration 081
Adam Eivy4a495d41eac1
diff --git a/scripts/migrations/081-brain-daily-log-inbox-sync.js b/scripts/migrations/081-brain-daily-log-inbox-sync.jsindex b9bbafbf37..2b2313ac7e 100644--- a/scripts/migrations/081-brain-daily-log-inbox-sync.js+++ b/scripts/migrations/081-brain-daily-log-inbox-sync.js@@ -23,6 +23,13 @@* fields (createdAt/updatedAt seeded from capturedAt so the LWW clock is* meaningful). The old .jsonl is renamed aside (.migrated) as a recovery* copy rather than deleted.+ * 3. memory-bridge-map.json — the brain→memory vector bridge keys each+ * vectorized journal as `journals:<innerUuid>`. Now that the journal record+ * id is the DATE, re-key those entries to `journals:<date>` using the inner+ * uuid we strip in step 1. Without this, the next edit to an existing day+ * computes the new date-key, finds no match, and mints a DUPLICATE memory+ * entry — orphaning the uuid-keyed one. (Inbox is not vectorized, so it has+ * no bridge entries to remap.)** Idempotent: re-running finds the inner `id` already stripped and the sync* fields already present, and skips a missing/renamed inbox_log.jsonl. The wire@@ -76,12 +83,15 @@ function normalizeRecord(record, { instanceId, seedTime, nowIso }) {* Pure transform over parsed inputs, exported for unit tests.* - journalsStore: parsed journals.json (or null if absent).* - inboxRows: array of parsed inbox_log.jsonl rows (empty if absent).- * Returns { journalsStore, inboxStore, journalsChanged, inboxCount }.+ * - bridgeMap: parsed memory-bridge-map.json (or null if absent).+ * Returns { journalsStore, inboxStore, bridgeMap, bridgeRemapped, journalsChanged, inboxCount }.*/-export function computeDailyLogInboxMigration(journalsStore, inboxRows, { instanceId, nowIso }) {+export function computeDailyLogInboxMigration(journalsStore, inboxRows, { instanceId, nowIso, bridgeMap = null } = {}) {// 1. Journals: normalize each entry in place (strip inner id, stamp sync fields).+ // Capture oldUuid→date so step 3 can re-key the memory bridge.let journalsChanged = false;let outJournals = null;+ const uuidToDate = new Map();if (journalsStore && typeof journalsStore === 'object') {const records = journalsStore.records && typeof journalsStore.records === 'object'? journalsStore.records@@ -89,6 +99,7 @@ export function computeDailyLogInboxMigration(journalsStore, inboxRows, { instanconst nextRecords = {};for (const [date, entry] of Object.entries(records)) {if (!entry || typeof entry !== 'object') continue;+ if (entry.id) uuidToDate.set(entry.id, date);const before = JSON.stringify(entry);// Seed from the entry's own updatedAt/createdAt so we don't bump the LWW// clock of days that already have one.@@ -117,7 +128,26 @@ export function computeDailyLogInboxMigration(journalsStore, inboxRows, { instan}const inboxStore = { records: inboxRecords };- return { journalsStore: outJournals, inboxStore, journalsChanged, inboxCount };+ // 3. Memory bridge: re-key `journals:<oldUuid>` → `journals:<date>`. Only+ // touches journal entries whose uuid we just mapped; leaves every other+ // bridge entry (people/projects/…) and any already-date-keyed journal+ // entry untouched (idempotent).+ let outBridge = null;+ let bridgeRemapped = 0;+ if (bridgeMap && typeof bridgeMap === 'object') {+ outBridge = { ...bridgeMap };+ for (const [oldUuid, date] of uuidToDate) {+ const oldKey = `journals:${oldUuid}`;+ const newKey = `journals:${date}`;+ if (oldKey in outBridge && !(newKey in outBridge)) {+ outBridge[newKey] = outBridge[oldKey];+ delete outBridge[oldKey];+ bridgeRemapped++;+ }+ }+ }++ return { journalsStore: outJournals, inboxStore, bridgeMap: outBridge, bridgeRemapped, journalsChanged, inboxCount };}function parseJsonl(content) {@@ -139,6 +169,7 @@ export async function up({ rootDir }) {const journalsPath = join(brainDir, 'journals.json');const legacyInboxPath = join(brainDir, 'inbox_log.jsonl');const inboxPath = join(brainDir, 'inbox.json');+ const bridgeMapPath = join(brainDir, 'memory-bridge-map.json');const instanceId = await readInstanceId(rootDir);const nowIso = new Date().toISOString();@@ -152,6 +183,15 @@ export async function up({ rootDir }) {}}+ // Load the memory-bridge map if present (for journal uuid→date re-keying).+ let bridgeMap = null;+ if (existsSync(bridgeMapPath)) {+ const raw = await readFile(bridgeMapPath, 'utf-8').catch(() => null);+ if (raw != null) {+ try { bridgeMap = JSON.parse(raw); } catch { bridgeMap = null; }+ }+ }+// Load legacy inbox JSONL if present AND we haven't already produced inbox.json// (idempotency: once migrated, the .jsonl is renamed aside so this is skipped).let inboxRows = [];@@ -161,13 +201,17 @@ export async function up({ rootDir }) {if (raw != null) inboxRows = parseJsonl(raw);}- const { journalsStore: outJournals, inboxStore, journalsChanged, inboxCount } =- computeDailyLogInboxMigration(journalsStore, inboxRows, { instanceId, nowIso });+ const { journalsStore: outJournals, inboxStore, bridgeMap: outBridge, bridgeRemapped, journalsChanged, inboxCount } =+ computeDailyLogInboxMigration(journalsStore, inboxRows, { instanceId, nowIso, bridgeMap });if (outJournals && journalsChanged) {await writeFile(journalsPath, JSON.stringify(outJournals, null, 2));}+ if (outBridge && bridgeRemapped > 0) {+ await writeFile(bridgeMapPath, JSON.stringify(outBridge, null, 2));+ }+// Only write inbox.json + retire the legacy file when there was a legacy file// to convert. If inbox.json already exists and no .jsonl remains, this is a// re-run — leave the authoritative inbox.json untouched.@@ -189,7 +233,8 @@ export async function up({ rootDir }) {console.log(`🧠 daily-log/inbox-sync: journals ${journalsChanged ? 'normalized' : 'already current'}, ` +- `inbox ${hasLegacyInbox ? `migrated ${inboxCount} entr${inboxCount === 1 ? 'y' : 'ies'} → inbox.json` : 'already current'}`+ `inbox ${hasLegacyInbox ? `migrated ${inboxCount} entr${inboxCount === 1 ? 'y' : 'ies'} → inbox.json` : 'already current'}` ++ `${bridgeRemapped > 0 ? `, re-keyed ${bridgeRemapped} journal memory-bridge entr${bridgeRemapped === 1 ? 'y' : 'ies'}` : ''}`);}diff --git a/scripts/migrations/081-brain-daily-log-inbox-sync.test.js b/scripts/migrations/081-brain-daily-log-inbox-sync.test.jsindex f08e6c82ae..1e7457ef39 100644--- a/scripts/migrations/081-brain-daily-log-inbox-sync.test.js+++ b/scripts/migrations/081-brain-daily-log-inbox-sync.test.js@@ -87,6 +87,32 @@ describe('computeDailyLogInboxMigration (pure)', () => {expect(journalsStore).toBeNull();expect(journalsChanged).toBe(false);});++ it('re-keys journal memory-bridge entries from uuid to date, leaving others alone', () => {+ const journals = { records: { '2026-04-18': { id: 'uuid-1', date: '2026-04-18', content: 'hi', segments: [] } } };+ const bridgeMap = {+ 'journals:uuid-1': 'mem-journal',+ 'people:p1': 'mem-person', // untouched+ };+ const { bridgeMap: out, bridgeRemapped } = computeDailyLogInboxMigration(journals, [], { ...opts, bridgeMap });+ expect(bridgeRemapped).toBe(1);+ expect(out['journals:2026-04-18']).toBe('mem-journal');+ expect(out).not.toHaveProperty('journals:uuid-1');+ expect(out['people:p1']).toBe('mem-person');+ });++ it('does not clobber an existing date-keyed bridge entry (idempotent)', () => {+ const journals = { records: { '2026-04-18': { id: 'uuid-1', date: '2026-04-18', content: 'hi', segments: [] } } };+ const bridgeMap = { 'journals:uuid-1': 'old', 'journals:2026-04-18': 'already' };+ const { bridgeMap: out, bridgeRemapped } = computeDailyLogInboxMigration(journals, [], { ...opts, bridgeMap });+ expect(bridgeRemapped).toBe(0);+ expect(out['journals:2026-04-18']).toBe('already');+ });++ it('returns null bridge map when none provided', () => {+ const { bridgeMap } = computeDailyLogInboxMigration(null, [], opts);+ expect(bridgeMap).toBeNull();+ });});describe('migration 081 up()', () => {@@ -138,6 +164,21 @@ describe('migration 081 up()', () => {expect(readJson('journals.json')).toEqual(journalsAfter1);});+ it('re-keys the on-disk memory-bridge map for journal entries', async () => {+ writeInstanceId('inst-x');+ writeJson('journals.json', {+ records: { '2026-04-18': { id: 'uuid-1', date: '2026-04-18', content: 'hi', segments: [] } },+ });+ writeJson('memory-bridge-map.json', { 'journals:uuid-1': 'mem-1', 'people:p1': 'mem-2' });++ await migration.up({ rootDir });++ const bridge = readJson('memory-bridge-map.json');+ expect(bridge['journals:2026-04-18']).toBe('mem-1');+ expect(bridge).not.toHaveProperty('journals:uuid-1');+ expect(bridge['people:p1']).toBe('mem-2');+ });+it('falls back to the unknown sentinel when instances.json is absent', async () => {writeJsonl('inbox_log.jsonl', [{ id: 'a', capturedText: 'x', status: 'done', capturedAt: '2026-03-01T00:00:00.000Z' },