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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
gate the data-root write guard on VITEST, not NODE_ENV=test alone
[._.]/ Adam Eivy77bb588957cc
diff --git a/server/lib/collectionStore.js b/server/lib/collectionStore.jsindex a2c9ad671d..291a9f4b0e 100644--- a/server/lib/collectionStore.js+++ b/server/lib/collectionStore.js@@ -85,7 +85,7 @@ import { join } from 'path';import { readdir, lstat, rm } from 'fs/promises';import { atomicWrite, readJSONFile, ensureDir } from './fileUtils.js';import { createFileWriteQueue, createRecordWriteQueue } from './fileWriteQueue.js';-import { isTestRunner } from './runtimeEnv.js';+import { isVitestRunner } from './runtimeEnv.js';const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);@@ -378,7 +378,7 @@ export function createCollectionStore({// #6176 — a recursive delete is the most destructive form of a data-root// leak, so it takes the same guard the write primitives do. Loaded lazily// for the same import-budget reason as fileCore.js.- if (isTestRunner()) {+ if (isVitestRunner()) {const { assertNotRealDataWrite } = await import('./testDataIsolation.js');assertNotRealDataWrite(recordDir(id), 'collectionStore delete');}diff --git a/server/lib/fileCore.js b/server/lib/fileCore.jsindex f835a4df2f..bc4639b59a 100644--- a/server/lib/fileCore.js+++ b/server/lib/fileCore.js@@ -5,14 +5,18 @@ import { createHash, randomUUID } from 'crypto';import { basename, dirname, extname, join } from 'path';import { promisify } from 'util';import { execFile } from './childProcess.js';-import { isTestRunner } from './runtimeEnv.js';+import { isVitestRunner } from './runtimeEnv.js';-// The #6176 data-root guard is loaded ONLY under the test runner. `fileCore` is+// The #6176 data-root guard is loaded ONLY under an actual Vitest worker —+// `isVitestRunner()`, narrower than `isTestRunner()`, because `scripts/+// smoke-boot.js` sets NODE_ENV=test on the real server boot to select the+// file-backend escape hatch, and that boot's writes into the real data/ tree+// are correct, not a leak (see `isVitestRunner()`'s doc comment). `fileCore` is// reached by essentially every server suite, so a static import of the guard// (and its `dataRoot`/`pathContainment` closure) landed on every one of them and// pushed `importScoping.test.js`'s tree-wide budget over its ceiling. Deferring-// it behind `isTestRunner()` — the shape server/AGENTS.md prescribes — also means-// production never loads the guard at all, rather than loading it to no-op.+// it — the shape server/AGENTS.md prescribes — also means production never+// loads the guard at all, rather than loading it to no-op.// The module promise is memoized, so the dynamic import resolves once per worker.let guardModule = null;const loadGuard = () => (guardModule ??= import('./testDataIsolation.js'));@@ -100,7 +104,7 @@ export function watchForFile(filePath, onDetected, { settleMs = 50, pollMs = FILexport async function ensureDir(dir) {// #6176 — refuse a NEW dir in the real data/ tree under the runner. Only the// CREATE path: `mkdir -p` on an existing directory mutates nothing.- if (isTestRunner()) (await loadGuard()).assertNotNewRealDataDir(dir);+ if (isVitestRunner()) (await loadGuard()).assertNotNewRealDataDir(dir);// mkdir with recursive: true is idempotent - it succeeds if dir exists.// BUT on Windows it can intermittently throw UNKNOWN/EPERM/EEXIST even when// the directory already exists or is created concurrently — antivirus locks,@@ -176,7 +180,7 @@ export async function atomicWrite(filePath, data) {// #6176 — the filesystem analogue of db.js's row-write backstop. Refuses// before any bytes are produced, so a leaking suite cannot even leave a temp// file behind in the real tree.- if (isTestRunner()) (await loadGuard()).assertNotRealDataWrite(filePath, 'atomicWrite');+ if (isVitestRunner()) (await loadGuard()).assertNotRealDataWrite(filePath, 'atomicWrite');// Buffer must pass through unchanged — JSON.stringify on a Buffer produces// `{"type":"Buffer","data":[...]}` which corrupts binary writes (PNG, etc.).const payload = typeof data === 'string' || Buffer.isBuffer(data) ? data : JSON.stringify(data, null, 2);@@ -259,19 +263,19 @@ export async function atomicWrite(filePath, data) {/** Guarded `fs/promises.writeFile`. Same signature. */export async function writeFileGuarded(filePath, data, options) {- if (isTestRunner()) (await loadGuard()).assertNotRealDataWrite(filePath, 'writeFile');+ if (isVitestRunner()) (await loadGuard()).assertNotRealDataWrite(filePath, 'writeFile');return writeFile(filePath, data, options);}/** Guarded `fs/promises.appendFile`. Same signature. */export async function appendFileGuarded(filePath, data, options) {- if (isTestRunner()) (await loadGuard()).assertNotRealDataWrite(filePath, 'appendFile');+ if (isVitestRunner()) (await loadGuard()).assertNotRealDataWrite(filePath, 'appendFile');return appendFile(filePath, data, options);}/** Guarded `fs/promises.copyFile` — the DESTINATION is what gets written. */export async function copyFileGuarded(src, dest, mode) {- if (isTestRunner()) (await loadGuard()).assertNotRealDataWrite(dest, 'copyFile');+ if (isVitestRunner()) (await loadGuard()).assertNotRealDataWrite(dest, 'copyFile');return copyFile(src, dest, mode);}diff --git a/server/lib/runtimeEnv.js b/server/lib/runtimeEnv.jsindex cdb53f725d..9e9624b5b8 100644--- a/server/lib/runtimeEnv.js+++ b/server/lib/runtimeEnv.js@@ -29,3 +29,24 @@export function isTestRunner() {return process.env.NODE_ENV === 'test' || process.env.VITEST != null;}++/**+ * Are we executing inside an actual Vitest worker — narrower than+ * `isTestRunner()`.+ *+ * `NODE_ENV=test` alone is not "running under Vitest": it is also the+ * documented signal a plain `node` process uses to select the file-backend+ * escape hatch (see AGENTS.md "Storage backend policy"), and `scripts/+ * smoke-boot.js` sets it on the real `server/index.js` boot specifically so+ * CI can smoke-test the server without Postgres. That smoke-booted server+ * legitimately writes its own runtime files into the real `data/` tree — the+ * exact thing `lib/testDataIsolation.js`'s write guard exists to catch a+ * *test suite* doing. Only `process.env.VITEST` (set by every Vitest worker,+ * never by a bare `node` invocation) tells the two apart, so the write guard+ * gates on this, not `isTestRunner()`.+ *+ * @returns {boolean}+ */+export function isVitestRunner() {+ return process.env.VITEST != null;+}diff --git a/server/lib/testDataIsolation.js b/server/lib/testDataIsolation.jsindex 8458d71671..ef21a92627 100644--- a/server/lib/testDataIsolation.js+++ b/server/lib/testDataIsolation.js@@ -31,9 +31,13 @@** ## Scope*- * Inert outside the test runner — production writes are never touched, and the- * `isTestRunner()` check comes first everywhere so a production call costs two- * `process.env` reads and no syscall.+ * Inert outside an actual Vitest worker — production writes are never+ * touched, and the `isVitestRunner()` check (narrower than `isTestRunner()`:+ * only `process.env.VITEST`, not `NODE_ENV=test` alone — see its doc comment)+ * comes first everywhere so a non-Vitest call costs one `process.env` read and+ * no syscall. `NODE_ENV=test` alone is not enough here: `scripts/smoke-boot.js`+ * sets it on the real server boot to select the file-backend escape hatch, and+ * that boot's writes into the real `data/` tree are correct, not a leak.** Under the runner it fires from the shared primitives in `fileCore.js` /* `jsonIo.js` (`atomicWrite`, `ensureDir`'s create path, `writeFileGuarded`,@@ -60,7 +64,7 @@ import { existsSync, lstatSync, readlinkSync } from 'fs';import { basename, dirname, isAbsolute, join, resolve } from 'path';import { resolveCodeRootForModule, resolveInstallRoot } from './dataRoot.js';import { canonicalizePath, isPathAtOrInsideDir } from './pathContainment.js';-import { isTestRunner } from './runtimeEnv.js';+import { isVitestRunner } from './runtimeEnv.js';// Resolved lazily, not at module load, so the realpath below sees a tree that// exists. Derived through `resolveCodeRootForModule`/`resolveInstallRoot` — the@@ -175,7 +179,7 @@ export function isInsideRealDataRoot(target) {* @throws {Error} when the runner is active and `target` lands in the real root*/export function assertNotRealDataWrite(target, operation) {- if (!isTestRunner() || !isInsideRealDataRoot(target)) return;+ if (!isVitestRunner() || !isInsideRealDataRoot(target)) return;throw new Error(`${operation} refused: this test tried to write ${target} inside the install's real data/ tree ` +`(${realDataRoot()}). A test must never write into the developer's live data — see ` +@@ -203,6 +207,6 @@ export function assertNotRealDataWrite(target, operation) {* @param {string} dir - the directory `ensureDir` is about to create*/export function assertNotNewRealDataDir(dir) {- if (!isTestRunner() || existsSync(dir)) return;+ if (!isVitestRunner() || existsSync(dir)) return;assertNotRealDataWrite(dir, 'ensureDir');}diff --git a/server/services/userActions.js b/server/services/userActions.jsindex 7381274e68..eb52e6c4d1 100644--- a/server/services/userActions.js+++ b/server/services/userActions.js@@ -46,7 +46,7 @@ import { atomicWrite, ensureDir, PATHS, readJSONFile } from '../lib/fileUtils.jsimport { createFileWriteQueue } from '../lib/fileWriteQueue.js';import { isPlainObject } from '../lib/objects.js';import { createPgFileFacade, resolvePgBackend } from '../lib/pgFileFacade.js';-import { isTestRunner } from '../lib/runtimeEnv.js';+import { isTestRunner, isVitestRunner } from '../lib/runtimeEnv.js';import { isUserActionActor, isUserActionType } from '../lib/userActionTypes.js';import { insertUserActionEvent, listUserActionEvents, pruneUserActionEvents } from './userActionsDb.js';@@ -85,7 +85,7 @@ const loadRealDataRootGuard = () => (realDataRootGuard ??= import('../lib/testDa* `'recordUserAction attempted a write of'`*/async function assertTestDataRootRedirected(attempted) {- if (!isTestRunner()) return;+ if (!isVitestRunner()) return;const { isInsideRealDataRoot } = await loadRealDataRootGuard();if (!isInsideRealDataRoot(eventsFile())) return;throw new Error(