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 failed legacy record splits pending after source repair (#9557)

Adam Eivy84a6ee434229
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 24b6913b6d..625c516baf 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -442,6 +442,31 @@ pg_isready -h localhost -p 5432 || pg_isready -h localhost -p 5561
Universes, series, catalog ingredients, memories, and other relational records live in PostgreSQL, not `data/` files. Inspect them via the Database settings tab or `psql`. To recover, restore a snapshot's `portos-db.sql` from the Backup tab (see [BACKUP.md](./BACKUP.md)).
+### Legacy Record Split Stamped Applied With a Corrupt Source
+
+**Symptom**: On an install that upgraded before #9557, `data/universe-builder.json` (migration 034), `data/pipeline-issues.json` (035) or `data/pipeline-series.json` (036) was truncated when migrations ran. The migration was stamped applied in `data/migrations.applied.json`, but `data/universes/`, `data/pipeline-issues/` or `data/pipeline-series/` lacks the records. Current builds throw and keep the migration pending instead, so only already-stamped installs need this.
+
+**Solution** (touch only the affected split):
+```bash
+# 1. Stop PortOS, then back up the source, the target layout and the ledger
+pm2 stop all
+cp -R data/pipeline-issues data/pipeline-issues.bak-manual 2>/dev/null
+mkdir -p data/manual-backup && cp data/pipeline-issues.json* data/manual-backup/ 2>/dev/null
+cp data/migrations.applied.json data/migrations.applied.json.bak-manual
+
+# 2. Repair the source JSON (restore from a backup or fix the truncation).
+# If the migration already renamed it, repair the `.bak-035` file instead.
+
+# 3. Remove ONLY the affected migration's entry (e.g. 035-split-pipeline-issues-to-per-id.js)
+# from data/migrations.applied.json — never clear the whole list.
+
+# 4. Re-run migrations; records already split are kept, only missing ones are added
+node scripts/run-migrations.js
+pm2 start all
+```
+
+The split never overwrites a record directory that already exists. If a later Postgres import (`server/scripts/migrateUniversesToDB.js` and similar) already ran, re-run that explicit legacy-import tool after the split rather than resetting import markers or schema versions.
+
### Lost App Registrations
**Symptom**: Apps disappear after restart.
diff --git a/scripts/migrations/034-split-universe-builder-to-per-uuid.js b/scripts/migrations/034-split-universe-builder-to-per-uuid.js
index 02a1080696..e33be208dd 100644
--- a/scripts/migrations/034-split-universe-builder-to-per-uuid.js
+++ b/scripts/migrations/034-split-universe-builder-to-per-uuid.js
@@ -58,6 +58,8 @@ export default makeSplitMigration({
recordsKey: 'universes',
idPattern: VALID_UNIVERSE_ID,
recordNoun: 'universe',
+ // Corrupt source must stay pending (not stamped applied) so a repaired file re-splits.
+ onUnreadable: 'throw',
// Cross-record `runs[]` moves into `config.runs` so it travels with the
// type-level index (fresh install → empty).
buildConfig: (doc) => ({ runs: Array.isArray(doc?.runs) ? doc.runs : [] }),
diff --git a/scripts/migrations/034-split-universe-builder-to-per-uuid.test.js b/scripts/migrations/034-split-universe-builder-to-per-uuid.test.js
index 7fbed45629..a5a31efa64 100644
--- a/scripts/migrations/034-split-universe-builder-to-per-uuid.test.js
+++ b/scripts/migrations/034-split-universe-builder-to-per-uuid.test.js
@@ -149,13 +149,9 @@ describe('migration 034 — split universe-builder.json to per-UUID files', () =
expect(existsSync(join(typeDir, 'aaaaaaaa-1111-1111-1111-aaaaaaaaaaaa', 'index.json'))).toBe(true);
});
- it('reports unreadable when the legacy file is corrupted', async () => {
+ it('rejects when the legacy file is corrupted so the runner keeps it pending', async () => {
writeFileSync(legacyPath, 'not json');
- const result = await migration.up({ rootDir });
- // JSON.parse throws — the migration catches outside our code? Actually
- // readJson throws on parse error. Let's check the behavior matches.
- // (If parse throws, the migration propagates — test that explicitly.)
- expect(result.ok).toBe(false);
+ await expect(migration.up({ rootDir })).rejects.toThrow(/unreadable/);
});
it('recovers from the .bak-034 file if the legacy was already renamed', async () => {
diff --git a/scripts/migrations/035-036-split-pipeline-collections.test.js b/scripts/migrations/035-036-split-pipeline-collections.test.js
index 90ea4c355d..d36cb42456 100644
--- a/scripts/migrations/035-036-split-pipeline-collections.test.js
+++ b/scripts/migrations/035-036-split-pipeline-collections.test.js
@@ -101,7 +101,7 @@ describe.each(cases)('migration split pipeline $name', (cfg) => {
expect(existsSync(join(typeDir, cfg.otherId, 'index.json'))).toBe(true);
});
- it('skips invalid ids and reports unreadable legacy files', async () => {
+ it('skips invalid ids and rejects unreadable legacy files', async () => {
writeJson(legacyPath, { [cfg.key]: [{ id: cfg.validId }, { id: cfg.invalidId }, null] });
const split = await cfg.migration.up({ rootDir });
expect(split.written).toBe(1);
@@ -110,7 +110,6 @@ describe.each(cases)('migration split pipeline $name', (cfg) => {
rmSync(rootDir, { recursive: true, force: true });
mkdirSync(dataDir, { recursive: true });
writeFileSync(legacyPath, 'not json');
- const unreadable = await cfg.migration.up({ rootDir });
- expect(unreadable).toEqual({ ok: false, reason: 'unreadable' });
+ await expect(cfg.migration.up({ rootDir })).rejects.toThrow(/unreadable/);
});
});
diff --git a/scripts/migrations/035-split-pipeline-issues-to-per-id.js b/scripts/migrations/035-split-pipeline-issues-to-per-id.js
index cfaae2f51a..89ccbe0813 100644
--- a/scripts/migrations/035-split-pipeline-issues-to-per-id.js
+++ b/scripts/migrations/035-split-pipeline-issues-to-per-id.js
@@ -24,4 +24,6 @@ export default makeSplitMigration({
recordsKey: 'issues',
idPattern: VALID_ID,
recordNoun: 'issue',
+ // Corrupt source must stay pending (not stamped applied) so a repaired file re-splits.
+ onUnreadable: 'throw',
});
diff --git a/scripts/migrations/036-split-pipeline-series-to-per-id.js b/scripts/migrations/036-split-pipeline-series-to-per-id.js
index 831093faeb..3222eff53d 100644
--- a/scripts/migrations/036-split-pipeline-series-to-per-id.js
+++ b/scripts/migrations/036-split-pipeline-series-to-per-id.js
@@ -20,4 +20,6 @@ export default makeSplitMigration({
recordsKey: 'series',
idPattern: VALID_ID,
recordNoun: 'series record',
+ // Corrupt source must stay pending (not stamped applied) so a repaired file re-splits.
+ onUnreadable: 'throw',
});
diff --git a/scripts/run-migrations.test.js b/scripts/run-migrations.test.js
index 0d309fd33e..e2db407beb 100644
--- a/scripts/run-migrations.test.js
+++ b/scripts/run-migrations.test.js
@@ -454,3 +454,45 @@ describe('listPendingMigrations', () => {
expect(existsSync(appliedFile)).toBe(true);
});
});
+
+describe('runMigrations record-split retry after source repair (#9557)', () => {
+ let rootDir;
+ let migrationsDir;
+ let appliedFile;
+ let sourcePath;
+ const realMigration = new URL('./migrations/035-split-pipeline-issues-to-per-id.js', import.meta.url).href;
+
+ beforeEach(() => {
+ rootDir = mkdtempSync(join(tmpdir(), 'run-migrations-split-'));
+ migrationsDir = join(rootDir, 'migrations');
+ mkdirSync(join(rootDir, 'data'), { recursive: true });
+ mkdirSync(migrationsDir, { recursive: true });
+ appliedFile = join(rootDir, 'data', 'migrations.applied.json');
+ sourcePath = join(rootDir, 'data', 'pipeline-issues.json');
+ // A non-empty ledger so the empty-ledger purge guard stays out of the way.
+ writeFileSync(appliedFile, JSON.stringify(['000-earlier.js']) + '\n');
+ writeFileSync(join(migrationsDir, '035-wrapper.js'), `export { default } from ${JSON.stringify(realMigration)};\n`);
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ rmSync(rootDir, { recursive: true, force: true });
+ vi.restoreAllMocks();
+ });
+
+ it('leaves a corrupt-source split pending, then splits and stamps it after repair', async () => {
+ const recordIndex = join(rootDir, 'data', 'pipeline-issues', 'iss-example', 'index.json');
+ writeFileSync(sourcePath, '{"issues": [');
+
+ await expect(runMigrations({ rootDir, migrationsDir })).rejects.toThrow(/unreadable/);
+ expect(JSON.parse(readFileSync(appliedFile, 'utf-8'))).toEqual(['000-earlier.js']);
+
+ writeFileSync(sourcePath, JSON.stringify({ issues: [{ id: 'iss-example', title: 'Example' }] }));
+ expect(await runMigrations({ rootDir, migrationsDir })).toBe(1);
+ expect(existsSync(recordIndex)).toBe(true);
+ expect(JSON.parse(readFileSync(appliedFile, 'utf-8'))).toEqual(['000-earlier.js', '035-wrapper.js']);
+
+ expect(await runMigrations({ rootDir, migrationsDir })).toBe(0);
+ });
+});