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

disarm pending purge migrations up front so an aborted rebuilt-ledger run can't re-arm them

[._.]/ Adam Eivyd58e50b2bc17
diff --git a/scripts/run-migrations.js b/scripts/run-migrations.js
index 97a474cae7..e5f6f87ac4 100644
--- a/scripts/run-migrations.js
+++ b/scripts/run-migrations.js
@@ -126,6 +126,25 @@ export async function runMigrations({
const files = await scanMigrationFiles(migrationsDir);
+ // Disarm EVERY pending purge migration up front, not as each is reached in
+ // the run loop: if this rebuilt-from-empty run aborted on an earlier
+ // throwing migration (the documented repair-and-reboot flow), the partial
+ // ledger would no longer "start empty" on the next boot — and the purge
+ // would then execute destructively against the rebuilt install.
+ if (ledgerStartedEmpty) {
+ let disarmed = 0;
+ for (const file of files) {
+ if (applied.includes(file)) continue;
+ const mod = await import(pathToFileURL(join(migrationsDir, file)).href);
+ const migration = (mod?.default && typeof mod.default.up === 'function') ? mod.default : mod;
+ if (!migration?.purge) continue;
+ console.warn(`⏭️ Skipping purge migration ${file}: applied-list started empty/rebuilt — recorded as applied without running to avoid a destructive rerun (#2770)`);
+ applied.push(file);
+ disarmed++;
+ }
+ if (disarmed > 0) await writeFile(appliedFile, JSON.stringify(applied, null, 2) + '\n');
+ }
+
let ran = 0;
for (const file of files) {
if (applied.includes(file)) continue;
@@ -136,13 +155,6 @@ export async function runMigrations({
throw new Error(`Migration "${file}" does not export an up() function`);
}
- if (migration.purge && ledgerStartedEmpty) {
- console.warn(`⏭️ Skipping purge migration ${file}: applied-list started empty/rebuilt — recorded as applied without running to avoid a destructive rerun (#2770)`);
- applied.push(file);
- await writeFile(appliedFile, JSON.stringify(applied, null, 2) + '\n');
- continue;
- }
-
console.log(`🔄 Running migration: ${file}`);
await migration.up({ rootDir, migrationsDir });
applied.push(file);
diff --git a/scripts/run-migrations.test.js b/scripts/run-migrations.test.js
index 7653bcac1f..cad83d64f4 100644
--- a/scripts/run-migrations.test.js
+++ b/scripts/run-migrations.test.js
@@ -230,6 +230,36 @@ export default {
expect(JSON.parse(readFileSync(appliedFile, 'utf-8')).sort())
.toEqual(['001-normal.js', '197-purge.js']);
});
+
+ it('disarms purge migrations even when an earlier migration aborts the rebuilt-empty run', async () => {
+ // The re-arm hole: a rebuilt-from-empty run that throws BEFORE reaching the
+ // purge migration persists a partial ledger. The next boot's ledger no
+ // longer "starts empty" — so unless the purge was disarmed up front, it
+ // would then execute destructively against the rebuilt install.
+ // Throws on its first execution only (marker-gated, since the module cache
+ // would defeat rewriting the file between runs in-process).
+ writeFileSync(join(migrationsDir, '001-throws.js'), `
+import { existsSync, writeFileSync } from 'fs';
+import { join } from 'path';
+export default {
+ async up({ rootDir }) {
+ const marker = join(rootDir, 'data', 'throw-once.txt');
+ if (!existsSync(marker)) { writeFileSync(marker, '1'); throw new Error('repair me and reboot'); }
+ }
+};
+`);
+ seedLearning({ bucket: { legitPostFixRuns: 7 }, keep: 3 });
+
+ // Run 1: ledger starts empty, 001 throws and aborts the run.
+ await expect(runMigrations({ rootDir, migrationsDir })).rejects.toThrow('repair me');
+ // The purge was already skip-recorded before the loop reached 001.
+ expect(JSON.parse(readFileSync(appliedFile, 'utf-8'))).toContain('197-purge.js');
+
+ // "Reboot": the ledger is now non-empty, but the purge must NOT fire — it
+ // was recorded as applied during the aborted run.
+ await runMigrations({ rootDir, migrationsDir });
+ expect(readLearning()).toEqual({ bucket: { legitPostFixRuns: 7 }, keep: 3 });
+ });
});
describe('runMigrations worktree backstop (#1947)', () => {