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([med-migration-scripts-migrations-014-attribute]): address codex review iter 1

[._.]/ Adam Eivyae3933573415
diff --git a/scripts/migrations/014-attribute-existing-annotations.js b/scripts/migrations/014-attribute-existing-annotations.js
index 830bf5887f..4c856cb922 100644
--- a/scripts/migrations/014-attribute-existing-annotations.js
+++ b/scripts/migrations/014-attribute-existing-annotations.js
@@ -25,11 +25,21 @@ import { join } from 'path';
import os from 'os';
import crypto from 'crypto';
+// Mirrors `safeJSONParse` in server/lib/fileUtils.js — a syntactically corrupt
+// instances.json must NOT crash the migration runner, otherwise this fix would
+// itself be a new boot-blocker. Treat parse failure as "no usable file" and let
+// the caller fall back to the default shape (which triggers fresh-identity
+// creation, the same recovery the in-server reader provides).
async function readJsonOr(path, fallback) {
if (!existsSync(path)) return fallback;
const raw = await readFile(path, 'utf8').catch(() => null);
if (!raw) return fallback;
- return JSON.parse(raw);
+ try {
+ return JSON.parse(raw);
+ } catch (err) {
+ console.warn(`⚠️ migration 014: ${path} is not valid JSON (${err.message}); falling back to defaults`);
+ return fallback;
+ }
}
// Self-contained ensureSelf: matches `server/services/instances.js`
@@ -39,7 +49,14 @@ async function readJsonOr(path, fallback) {
// CLI with no server lifecycle).
async function ensureSelfIdentity(instancesPath) {
const data = await readJsonOr(instancesPath, { self: null, peers: [] });
- if (data?.self?.instanceId) return data.self;
+ // Strict: only reuse a real-looking identity. Reject the `'unknown'` sentinel
+ // (this is exactly the value we're trying to never persist), reject non-string
+ // shapes, and reject empty strings — any of those would re-introduce the
+ // phantom-author bug we're fixing. Fall through to fresh-identity creation.
+ const existingId = data?.self?.instanceId;
+ if (typeof existingId === 'string' && existingId && existingId !== 'unknown') {
+ return data.self;
+ }
const self = { instanceId: crypto.randomUUID(), name: os.hostname() };
const next = {
self,
diff --git a/scripts/migrations/014-attribute-existing-annotations.test.js b/scripts/migrations/014-attribute-existing-annotations.test.js
index 3ca87d366f..915a5f2d42 100644
--- a/scripts/migrations/014-attribute-existing-annotations.test.js
+++ b/scripts/migrations/014-attribute-existing-annotations.test.js
@@ -143,6 +143,58 @@ describe('migration 014 — attribute existing annotations', () => {
expect(out['image:keep.png']).toBeDefined();
});
+ it('rejects existing self.instanceId === "unknown" and creates a fresh identity', async () => {
+ // The whole point of this migration fix is to never persist the literal
+ // 'unknown' string as an author key. If a previous run (or hand-edit) left
+ // self.instanceId === 'unknown', the migration must replace it with a real
+ // uuid — otherwise we'd re-attribute legacy entries to the phantom again.
+ writeInstances({ self: { instanceId: 'unknown', name: 'old-host' }, peers: [] });
+ writeAnnotations({
+ 'image:foo.png': { starred: true, updatedAt: '2025-01-01T00:00:00.000Z' },
+ });
+ await migration.up({ rootDir });
+ const instances = readInstances();
+ expect(instances.self.instanceId).not.toBe('unknown');
+ expect(instances.self.instanceId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
+ const out = readAnnotations();
+ expect(Object.keys(out['image:foo.png'].authors)).toEqual([instances.self.instanceId]);
+ });
+
+ it('rejects non-string self.instanceId (numeric) and creates a fresh identity', async () => {
+ writeInstances({ self: { instanceId: 42, name: 'host' }, peers: [] });
+ writeAnnotations({
+ 'image:foo.png': { starred: true, updatedAt: '2025-01-01T00:00:00.000Z' },
+ });
+ await migration.up({ rootDir });
+ const instances = readInstances();
+ expect(typeof instances.self.instanceId).toBe('string');
+ expect(instances.self.instanceId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-/);
+ });
+
+ it('rejects empty-string self.instanceId and creates a fresh identity', async () => {
+ writeInstances({ self: { instanceId: '', name: 'host' }, peers: [] });
+ writeAnnotations({
+ 'image:foo.png': { starred: true, updatedAt: '2025-01-01T00:00:00.000Z' },
+ });
+ await migration.up({ rootDir });
+ const instances = readInstances();
+ expect(instances.self.instanceId.length).toBeGreaterThan(0);
+ expect(instances.self.instanceId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-/);
+ });
+
+ it('tolerates corrupt instances.json — treats as missing and creates a fresh identity', async () => {
+ // The in-server reader (safeJSONParse) tolerates malformed JSON; the
+ // migration must match that behavior, otherwise a hand-edited or
+ // half-written instances.json would brick boot.
+ writeFileSync(instancesPath, '{ this is not valid JSON');
+ writeAnnotations({
+ 'image:foo.png': { starred: true, updatedAt: '2025-01-01T00:00:00.000Z' },
+ });
+ await expect(migration.up({ rootDir })).resolves.not.toThrow();
+ const instances = readInstances();
+ expect(instances.self.instanceId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-/);
+ });
+
it('is idempotent — re-running over already-migrated data is a no-op', async () => {
writeInstances({ self: { instanceId: 'real-uuid', name: 'host' }, peers: [] });
writeAnnotations({