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): read migration ledger from the data install root; document why agent shells don't inherit PORTOS_DATA_ROOT

[._.]/ Adam Eivybf662f93c9cc
diff --git a/server/lib/dataRoot.js b/server/lib/dataRoot.js
index d60bb86f47..74f4f34ef8 100644
--- a/server/lib/dataRoot.js
+++ b/server/lib/dataRoot.js
@@ -29,6 +29,16 @@ export const DATA_ROOT_ENV = 'PORTOS_DATA_ROOT';
* `fallbackRoot` unchanged when the env var is unset or blank — preserving the
* existing behavior for installs that never set it (backward compatible).
*
+ * By design this is pinned only at a REAL launch (`ecosystem.config.cjs`). It is
+ * intentionally NOT propagated into CoS agent worktree shells (`shellService`
+ * rebuilds a scrubbed env): a worktree process must NOT silently run migrations
+ * or data writes against the real `data/` tree using worktree-version code — a
+ * version-skew data-corruption hazard, and a `:5555` collision with the real
+ * server. There the env var stays unset, so resolution falls back to the
+ * worktree path and `isWorktreeRoot()` makes the boot-migration pass skip
+ * (no crash). An agent that genuinely wants the real data tree opts in by
+ * setting `PORTOS_DATA_ROOT` explicitly.
+ *
* @param {string} fallbackRoot absolute path derived from the caller's location
* @returns {string} the install root to use
*/
diff --git a/server/lib/fileUtils.js b/server/lib/fileUtils.js
index a7f6e2e02a..f394a9f6a2 100644
--- a/server/lib/fileUtils.js
+++ b/server/lib/fileUtils.js
@@ -47,6 +47,12 @@ export const RISKY_MIME_TYPES = new Set(['text/html', 'image/svg+xml', 'applicat
*/
export const PATHS = {
root: CODE_ROOT,
+ // The data-bearing install root (parent of `data/`). Equals `root` for a
+ // normal install; diverges only when PORTOS_DATA_ROOT pins a different tree
+ // (worktree boot, #1947). Use this — not `root` — for anything anchored to
+ // the `data/` tree but living one level above it (e.g. the migration ledger
+ // `data/migrations.applied.json`), so its reader agrees with where boot wrote it.
+ installRoot: INSTALL_ROOT,
data: join(INSTALL_ROOT, 'data'),
cos: join(INSTALL_ROOT, 'data/cos'),
brain: join(INSTALL_ROOT, 'data/brain'),
diff --git a/server/services/installState.js b/server/services/installState.js
index da9e262722..5e2f06dcac 100644
--- a/server/services/installState.js
+++ b/server/services/installState.js
@@ -187,12 +187,17 @@ async function detectStaleDeps(rootDir, { statMtime = statMtimeMs } = {}) {
*/
export async function getInstallState({
rootDir = PATHS.root,
+ // The migration ledger lives under the DATA install root (data/migrations.applied.json),
+ // which boot writes via the PORTOS_DATA_ROOT-resolved root — not the code checkout.
+ // Read it from the same place so a pinned-data-root worktree boot doesn't report every
+ // already-applied migration as pending (#1947). Equals rootDir for a normal install.
+ migrationRootDir = PATHS.installRoot,
boot = bootCommit,
getCurrentCommit = () => gitRevParseHead(rootDir),
isAncestor = (a, b) => gitIsAncestor(a, b, rootDir),
statMtime = statMtimeMs,
clientSourceNewer = (buildMs) => isClientSourceNewer(rootDir, buildMs, { statMtime }),
- listPending = () => listPendingMigrations({ rootDir }),
+ listPending = () => listPendingMigrations({ rootDir: migrationRootDir }),
} = {}) {
const currentCommit = await getCurrentCommit().catch(() => null);
diff --git a/server/services/installState.test.js b/server/services/installState.test.js
index e912488b5c..c2c74abccb 100644
--- a/server/services/installState.test.js
+++ b/server/services/installState.test.js
@@ -1,7 +1,15 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, utimesSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
+
+// Mock the migration-status reader so the migration-ledger-root test below can
+// assert which rootDir the DEFAULT wiring passes (the rest of the suite injects
+// its own `listPending`, so the mock is inert for them).
+vi.mock('../../scripts/run-migrations.js', () => ({
+ listPendingMigrations: vi.fn(async () => [])
+}));
+
import {
getInstallState,
captureBootCommit,
@@ -9,6 +17,8 @@ import {
__setBootCommitForTest,
__internal
} from './installState.js';
+import { listPendingMigrations } from '../../scripts/run-migrations.js';
+import { PATHS } from '../lib/fileUtils.js';
// All external effects are injected, so these tests never touch real git/fs.
@@ -187,6 +197,23 @@ describe('getInstallState — pending migrations', () => {
expect(state.pendingMigrations.count).toBe(0);
expect(state.outOfSync).toBe(false);
});
+
+ // #1947: the migration ledger lives under the DATA install root (where boot
+ // wrote it via the PORTOS_DATA_ROOT-resolved root), not the code checkout —
+ // so the status reader must not use PATHS.root when the two diverge.
+ it('reads the ledger from the data install root by default, not the code checkout', async () => {
+ listPendingMigrations.mockClear();
+ const { listPending, ...optsWithoutInjectedPending } = syncedOpts();
+ await getInstallState(optsWithoutInjectedPending);
+ expect(listPendingMigrations).toHaveBeenCalledWith({ rootDir: PATHS.installRoot });
+ });
+
+ it('honors an explicit migrationRootDir override', async () => {
+ listPendingMigrations.mockClear();
+ const { listPending, ...optsWithoutInjectedPending } = syncedOpts();
+ await getInstallState({ ...optsWithoutInjectedPending, migrationRootDir: '/pinned/install' });
+ expect(listPendingMigrations).toHaveBeenCalledWith({ rootDir: '/pinned/install' });
+ });
});
describe('getInstallState — resilience', () => {