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

address review (codex): skip migrations on a mixed-root run (worktree code + foreign data tree)

[._.]/ Adam Eivy66a69a7aed70
diff --git a/scripts/run-migrations.js b/scripts/run-migrations.js
index 64b25647fa..692cc9471e 100644
--- a/scripts/run-migrations.js
+++ b/scripts/run-migrations.js
@@ -5,10 +5,13 @@ import { fileURLToPath, pathToFileURL } from 'url';
import { resolveInstallRoot, isWorktreeRoot } from '../server/lib/dataRoot.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
+// The install this migration CODE ships with (the checkout that loaded this
+// file), independent of any env override — used to detect a mixed-root run.
+const CHECKOUT_ROOT = join(__dirname, '..');
// Prefer an explicit PORTOS_DATA_ROOT env var over the executing-file location
// so a process booted from inside a CoS agent git worktree still resolves to
// the real install (#1947). Falls back to the derived path when unset.
-const DEFAULT_ROOT_DIR = resolveInstallRoot(join(__dirname, '..'));
+const DEFAULT_ROOT_DIR = resolveInstallRoot(CHECKOUT_ROOT);
const DEFAULT_MIGRATIONS_DIR = join(__dirname, 'migrations');
const appliedFilePath = (rootDir) => join(rootDir, 'data', 'migrations.applied.json');
@@ -96,6 +99,20 @@ export async function runMigrations({
return 0;
}
+ // Mixed-root backstop (#1947): the migrations being scanned ship with THIS
+ // checkout (DEFAULT_MIGRATIONS_DIR), but rootDir points at a DIFFERENT
+ // install's data tree — e.g. a worktree process with PORTOS_DATA_ROOT pinned
+ // to the real install. Applying this checkout's migrations to a foreign data
+ // tree can mark a worktree-only migration applied without ever copying its
+ // shipped data (seed migrations only warn when their data.reference sample is
+ // absent), corrupting the real install's applied-list. Only enforce when the
+ // default migrations dir is in use — an explicit migrationsDir (tests, a
+ // deliberate cross-install run) means the caller owns the code/data pairing.
+ if (migrationsDir === DEFAULT_MIGRATIONS_DIR && resolve(rootDir) !== resolve(CHECKOUT_ROOT)) {
+ console.warn(`⚠️ Skipping migrations: data root (${rootDir}) differs from the executing checkout (${CHECKOUT_ROOT}) — refusing to apply this checkout's migrations to a foreign install (mixed-root)`);
+ return 0;
+ }
+
const appliedFile = appliedFilePath(rootDir);
// Ensure data/ exists so we can persist applied state (migrationsDir
diff --git a/scripts/run-migrations.test.js b/scripts/run-migrations.test.js
index 3b2e4230eb..fa4f5983c1 100644
--- a/scripts/run-migrations.test.js
+++ b/scripts/run-migrations.test.js
@@ -165,6 +165,21 @@ export default {
expect(ran).toBe(1);
expect(existsSync(join(realRoot, 'data', 'fixture-marker.txt'))).toBe(true);
});
+
+ it('skips when the data root differs from the executing checkout (mixed-root, default migrationsDir)', async () => {
+ // No migrationsDir override → the runner uses its OWN checkout's
+ // DEFAULT_MIGRATIONS_DIR. A rootDir pointing at a different install must not
+ // get this checkout's migrations applied to it (returns before any FS write
+ // to the foreign root, so this is safe to run against the real repo).
+ const foreignRoot = join(baseDir, 'some-other-install');
+ mkdirSync(join(foreignRoot, 'data'), { recursive: true });
+
+ const ran = await runMigrations({ rootDir: foreignRoot });
+
+ expect(ran).toBe(0);
+ expect(existsSync(join(foreignRoot, 'data', 'migrations.applied.json'))).toBe(false);
+ expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('mixed-root'));
+ });
});
describe('listPendingMigrations', () => {