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(migrations): adopt a pre-May-2026 ledger on every migration run

Adam Eivyf229a1a1c819
diff --git a/scripts/lib/migrationLedger.js b/scripts/lib/migrationLedger.js
index 60948eec52..b70453441f 100644
--- a/scripts/lib/migrationLedger.js
+++ b/scripts/lib/migrationLedger.js
@@ -1,4 +1,4 @@
-import { readdir, readFile, rename } from 'fs/promises';
+import { readdir, readFile, rename, stat } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { resolveInstallRoot } from '../../server/lib/dataRoot.js';
@@ -20,6 +20,32 @@ export const DEFAULT_MIGRATIONS_DIR = join(__dirname, '..', 'migrations');
export const appliedFilePath = (rootDir) => join(rootDir, 'data', 'migrations.applied.json');
+// Installs from before the 2026-05-16 move of migrations out of data/ keep the
+// ledger here. The booting server runs migrations on a plain pull-and-restart
+// without setup-data.js, so every runner adopts it: an unread legacy ledger
+// reads as empty, re-runs every applied migration and disarms the purge class.
+export const legacyAppliedFilePath = (rootDir) => join(rootDir, 'data', 'migrations', '.applied.json');
+
+const pathExists = (path) => stat(path).then(() => true, (err) => {
+ if (err.code === 'ENOENT') return false;
+ throw err;
+});
+
+/**
+ * Move a legacy ledger to `appliedFilePath`. A current ledger is authoritative
+ * when both exist, so the legacy copy is then left untouched. Returns whether
+ * the file moved.
+ */
+export async function adoptLegacyAppliedList(rootDir) {
+ const legacy = legacyAppliedFilePath(rootDir);
+ if (!(await pathExists(legacy)) || await pathExists(appliedFilePath(rootDir))) return false;
+ return rename(legacy, appliedFilePath(rootDir)).then(() => true, (err) => {
+ // Another runner (setup-data has no lock) adopted it first.
+ if (err.code === 'ENOENT') return false;
+ throw err;
+ });
+}
+
/**
* Scan for numbered migration files (NNN-name.js, sorted by filename). Co-located vitest
* files (*.test.js) are excluded — they don't export `up()` and would
@@ -81,7 +107,12 @@ export async function listPendingMigrations({
rootDir = DEFAULT_ROOT_DIR,
migrationsDir = DEFAULT_MIGRATIONS_DIR,
} = {}) {
- const applied = await readAppliedList(appliedFilePath(rootDir), { repair: false });
+ // Read-only: report what the next run sees once it adopts a legacy ledger.
+ const useLegacy = await pathExists(appliedFilePath(rootDir))
+ .then((exists) => !exists && pathExists(legacyAppliedFilePath(rootDir)))
+ .catch(() => false);
+ const ledger = useLegacy ? legacyAppliedFilePath(rootDir) : appliedFilePath(rootDir);
+ const applied = await readAppliedList(ledger, { repair: false });
const appliedSet = new Set(applied);
const files = await scanMigrationFiles(migrationsDir).catch(() => []);
return files.filter(f => !appliedSet.has(f));
diff --git a/scripts/run-migrations.js b/scripts/run-migrations.js
index 61032002ef..722d0d435c 100644
--- a/scripts/run-migrations.js
+++ b/scripts/run-migrations.js
@@ -7,6 +7,7 @@ import { isDirectlyInvoked } from './lib/directInvocation.js';
import {
DEFAULT_ROOT_DIR,
DEFAULT_MIGRATIONS_DIR,
+ adoptLegacyAppliedList,
appliedFilePath,
listPendingMigrations,
readAppliedList,
@@ -168,6 +169,10 @@ export async function runMigrations({
}
async function runPendingMigrations({ rootDir, migrationsDir, appliedFile }) {
+ if (await adoptLegacyAppliedList(rootDir)) {
+ console.log('🧹 Moved data/migrations/.applied.json → data/migrations.applied.json');
+ }
+
// A runner killed between writing the ledger's temp file and renaming it
// leaves `migrations.applied.json.tmp-<pid>` behind, and no later run (a new
// pid) would ever reuse it. Remove only a dead writer's file: a lock reclaimed
diff --git a/scripts/run-migrations.test.js b/scripts/run-migrations.test.js
index 593cc44815..46aa585af3 100644
--- a/scripts/run-migrations.test.js
+++ b/scripts/run-migrations.test.js
@@ -113,6 +113,31 @@ export default {
expect(warnSpy).not.toHaveBeenCalled();
});
+ // A pre-2026-05-16 install upgraded by pull-and-restart reaches the boot
+ // runner without setup-data.js ever moving its ledger.
+ it('adopts a legacy data/migrations/.applied.json instead of re-running what it records', async () => {
+ const legacyFile = join(dataDir, 'migrations', '.applied.json');
+ mkdirSync(join(dataDir, 'migrations'));
+ writeFileSync(legacyFile, JSON.stringify(['001-fixture.js']));
+
+ const ran = await runMigrations({ rootDir, migrationsDir });
+
+ expect(ran).toBe(0);
+ expect(existsSync(join(dataDir, 'fixture-marker.txt'))).toBe(false);
+ expect(existsSync(legacyFile)).toBe(false);
+ expect(JSON.parse(readFileSync(appliedFile, 'utf-8'))).toEqual(['001-fixture.js']);
+ });
+
+ it('keeps the current ledger authoritative when a legacy one also exists', async () => {
+ const legacyFile = join(dataDir, 'migrations', '.applied.json');
+ mkdirSync(join(dataDir, 'migrations'));
+ writeFileSync(legacyFile, '[]');
+ writeFileSync(appliedFile, JSON.stringify(['001-fixture.js']));
+
+ expect(await runMigrations({ rootDir, migrationsDir })).toBe(0);
+ expect(readFileSync(legacyFile, 'utf-8')).toBe('[]');
+ });
+
it('rejects unreadable ledgers without running migrations or changing their bytes', async () => {
const original = '["001-fixture.js"]\n';
writeFileSync(appliedFile, original);
@@ -618,6 +643,16 @@ describe('listPendingMigrations', () => {
expect(pending).toEqual(['001-a.js', '002-b.js', '003-c.js']);
});
+ it('reads a legacy ledger the next run will adopt, without moving it', async () => {
+ const legacyFile = join(dataDir, 'migrations', '.applied.json');
+ mkdirSync(join(dataDir, 'migrations'));
+ writeFileSync(legacyFile, JSON.stringify(['001-a.js']));
+ const pending = await listPendingMigrations({ rootDir, migrationsDir });
+ expect(pending).toEqual(['002-b.js', '003-c.js']);
+ expect(existsSync(legacyFile)).toBe(true);
+ expect(existsSync(appliedFile)).toBe(false);
+ });
+
it('treats a corrupt applied-list as empty WITHOUT mutating it (read-only)', async () => {
writeFileSync(appliedFile, '{ not valid json');
const pending = await listPendingMigrations({ rootDir, migrationsDir });
diff --git a/scripts/setup-data.js b/scripts/setup-data.js
index d4c57405d0..7fe205fda6 100644
--- a/scripts/setup-data.js
+++ b/scripts/setup-data.js
@@ -1,5 +1,5 @@
#!/usr/bin/env node
-import { existsSync, readdirSync, readFileSync, renameSync, rmdirSync } from 'fs';
+import { existsSync, readdirSync, readFileSync, rmdirSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
@@ -7,6 +7,7 @@ import { md5, buildPromptDriftTables } from './migrations/_lib.js';
import { seedDataDir } from './lib/seedDataDir.js';
import { mergeJsonStarterTargets } from './lib/mergeJsonStarter.js';
import { rewriteAppsPortosRoot } from './lib/rewriteAppsPortosRoot.js';
+import { adoptLegacyAppliedList } from './lib/migrationLedger.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, '..');
@@ -16,16 +17,13 @@ const referenceDir = join(rootDir, 'data.reference');
console.log('📁 Setting up data directory...');
// One-shot cleanup for installs that predate the migrations-out-of-data move:
-// (1) relocate data/migrations/.applied.json → data/migrations.applied.json so
-// run-migrations.js finds the prior applied-list and doesn't re-run anything;
+// (1) relocate data/migrations/.applied.json → data/migrations.applied.json
+// (the migration runner adopts it too, for a boot that skips setup-data);
// (2) drop the now-orphan data/migrations/ directory if empty. Guarded so we
// never clobber a fresh-layout applied file and never blow away a non-empty
// dir (in case a user has uncommitted local migration files).
const legacyMigrationsDir = join(dataDir, 'migrations');
-const legacyAppliedFile = join(legacyMigrationsDir, '.applied.json');
-const newAppliedFile = join(dataDir, 'migrations.applied.json');
-if (existsSync(legacyAppliedFile) && !existsSync(newAppliedFile)) {
- renameSync(legacyAppliedFile, newAppliedFile);
+if (await adoptLegacyAppliedList(rootDir)) {
console.log('🧹 Moved data/migrations/.applied.json → data/migrations.applied.json');
}
if (existsSync(legacyMigrationsDir) && readdirSync(legacyMigrationsDir).length === 0) {