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: disarm purges when migration ledger entries are invalid

[._.]/ Adam Eivye4c93064c95b
diff --git a/scripts/lib/migrationLedger.js b/scripts/lib/migrationLedger.js
index 441896bb21..bcf4fc6317 100644
--- a/scripts/lib/migrationLedger.js
+++ b/scripts/lib/migrationLedger.js
@@ -68,8 +68,8 @@ export async function scanMigrationFiles(migrationsDir) {
* Read the applied-migrations list. A missing file defaults to [].
*
* When `repair` is true and the file is corrupt (mid-write truncation, bad
- * JSON, wrong shape), rename it aside and rebuild the ledger from scratch so
- * corruption does not brick future boots. Non-ENOENT read errors
+ * JSON, wrong shape or invalid migration filenames), rename it aside and
+ * rebuild the ledger from scratch so corruption does not brick future boots. Non-ENOENT read errors
* propagate in repair mode: treating an unreadable ledger as empty could rerun
* migrations against live data. When `repair` is false (read-only callers like
* listPendingMigrations), read errors and corrupt content are treated as `[]`
@@ -90,6 +90,12 @@ export async function readAppliedList(appliedFile, { repair = false } = {}) {
if (corruptReason === null && !Array.isArray(parsed)) {
corruptReason = `expected array, got ${typeof parsed}`;
}
+ if (corruptReason === null && parsed.some(file => typeof file !== 'string'
+ || !/^\d+-[^/\\]+\.js$/.test(file) || file.endsWith('.test.js'))) {
+ // A nonempty array is the runner's evidence that purge migrations may run.
+ // Invalid entries cannot supply that evidence, even beside valid filenames.
+ corruptReason = 'expected numbered migration filenames';
+ }
if (corruptReason !== null) {
if (repair) {
const aside = `${appliedFile}.corrupt-${new Date().toISOString().replace(/[:.]/g, '-')}`;
diff --git a/scripts/run-migrations.test.js b/scripts/run-migrations.test.js
index 6e8e029dfd..142a8ddeed 100644
--- a/scripts/run-migrations.test.js
+++ b/scripts/run-migrations.test.js
@@ -355,6 +355,26 @@ export default {
expect(JSON.parse(readFileSync(appliedFile, 'utf-8'))).toEqual(['197-purge.js']);
});
+ it.each([
+ [null],
+ ['000-earlier.js', null],
+ [''],
+ ])('preserves post-upgrade data when the ledger contains invalid entries: %j', async (...entries) => {
+ const original = JSON.stringify(entries);
+ writeFileSync(appliedFile, original);
+ const learning = { bucket: { legitPostFixRuns: 5 }, keep: 2 };
+ seedLearning(learning);
+
+ expect(await runMigrations({ rootDir, migrationsDir })).toBe(0);
+ expect(readLearning()).toEqual(learning);
+ const parked = readdirSync(dataDir).find(name => name.startsWith('migrations.applied.json.corrupt-'));
+ expect(parked).toBeDefined();
+ expect(readFileSync(join(dataDir, parked), 'utf8')).toBe(original);
+ expect(JSON.parse(readFileSync(appliedFile, 'utf8'))).toEqual(['197-purge.js']);
+ expect(await runMigrations({ rootDir, migrationsDir })).toBe(0);
+ expect(readLearning()).toEqual(learning);
+ });
+
it('records a purge migration as applied on a fresh install (nothing to purge)', async () => {
// No learning.json at all — the empty-ledger skip is harmless and still marks
// it applied so it never fires destructively later.