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

test: broaden the data/-isolation guard to parent-climb paths, and record the probe recipe

[._.]/ Adam Eivy2eaa362b97e9
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index a738854a45..687c63a6fd 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -81,4 +81,4 @@
- Internal: the series-autopilot unlock-pass test suite no longer reads the checkout's real `data/` directory. It partial-mocked `fileUtils.js` with an in-memory store but left `PATHS.data` pointing at the repo, and the series collection store enumerates that directory with a real `readdir` the mock never intercepts — so on any checkout with real series on disk, two sole-universe-ownership tests failed with what looked like a regression on whatever branch happened to be checked out. CI never saw it because a fresh checkout has no `data/`. `PATHS.data` now resolves to a per-run temp dir via the shared `makePathsProxy` helper, and three added assertions pin the isolation so it cannot quietly come back. The three sibling pipeline suites named as suspects were audited and are already isolated — they replace `series.js` outright and never reach the store. Test-only change; no production code touched.
-- Internal: audited the whole server test suite for the "test reads the checkout's real `data/`" class that #3683 fixed one instance of, and closed the one other case it found. The audit was empirical rather than static — a full-suite baseline against an empty `data/`, then a second run with obviously-fake probe records planted in every `data/` subdirectory a service enumerates (prefix-aware, so the stores whose id allowlists require `iss-`/`ser-`/`stb-`/`game-` were actually reached) — and exactly one of 1301 test files failed only in the second run: the missions suite, which addressed `<repo>/data/cos/missions` directly with no mock at all, so it read the developer's live missions *and* wrote its own fixtures into them. It now runs against a per-run temp root, with two assertions pinning the isolation. The 67 suites that partial-mock `fileUtils.js` without redirecting `PATHS` were cleared by the same two-run diff — they replace the service graph wholesale and never reach a real `readdir`. A new repo-wide guard keys on the thing that is never legitimate in a test, naming the checkout's own `data/` directory, so it needs no allowlist. Test-only change; no production code touched.
+- Internal: audited the whole server test suite for the "test reads the checkout's real `data/`" class that #3683 fixed one instance of, and closed the one other case it found. The audit was empirical rather than static — a full-suite baseline against an empty `data/`, then a second run with obviously-fake probe records planted in every `data/` subdirectory a service enumerates (prefix-aware, so the stores whose id allowlists require `iss-`/`ser-`/`stb-`/`game-` were actually reached) — and exactly one of 1301 test files failed only in the second run: the missions suite, which addressed `<repo>/data/cos/missions` directly with no mock at all, so it read the developer's live missions *and* wrote its own fixtures into them. It now runs against a per-run temp root, with two assertions pinning the isolation. The 67 suites that partial-mock `fileUtils.js` without redirecting `PATHS` were cleared by the same two-run diff — they replace the service graph wholesale and never reach a real `readdir`. A new repo-wide guard now fails the suite when a test names the checkout's own `data/` directory — either through an install-root anchor or a parent-directory climb — with the one deliberate exception documented in place (the creative-director prompt suite reads the installed prompt on purpose, to catch drift against a local edit). It deliberately does not police the third spelling, importing the real `PATHS` and reading a data-rooted member: 50 files do that, nearly all safely, so that one stays covered by the two-run probe, whose recipe is now written down beside the guard so it can be re-run. Test-only change; no production code touched.
diff --git a/server/lib/testDataIsolation.guards.test.js b/server/lib/testDataIsolation.guards.test.js
index 17d1d5fb12..4e773bab46 100644
--- a/server/lib/testDataIsolation.guards.test.js
+++ b/server/lib/testDataIsolation.guards.test.js
@@ -14,17 +14,33 @@
* partial mock that left `PATHS` alone) and #3687 (`missions.test.js`, via
* `join(process.cwd(), 'data', …)` with no mock at all).
*
- * ## Why this rule and not "every fileUtils partial mock must redirect PATHS"
+ * ## What this rule covers, and what covers the rest
*
- * #3687 audited the whole server suite empirically — two full runs, the second
- * with probe records planted in every `data/` subdirectory a service enumerates
- * — and found exactly ONE leaker in 1301 test files. The 67 suites that
- * partial-mock `fileUtils.js` without a `PATHS` redirect are overwhelmingly safe
- * (they replace the service graph wholesale, so nothing ever reaches a real
- * `readdir`), and a guard over that shape would need a 66-entry allowlist to
- * flag one file. So the rule keys on the thing that is *never* legitimate in a
- * test — naming the checkout's own `data/` directory — which needs no allowlist
- * at all.
+ * The rule matches the two spellings that name the checkout's `data/` directory
+ * *textually*: an install-root anchor (`process.cwd()`, `PATHS.root`,
+ * `PATHS.installRoot`) or a parent-directory climb (`'..'`), joined to a `data`
+ * segment. Both are illegitimate in a test with no exceptions to carve out, so
+ * the rule needs no allowlist beyond the one deliberate case documented below.
+ *
+ * It does NOT cover the third spelling — importing the real `PATHS` and reading
+ * a data-rooted member (`PATHS.missions`, `PATHS.brain`, …) with no redirect.
+ * 50 of the ~1300 scoped test files do that, nearly all of them safely, because
+ * they replace the service graph wholesale and never reach a real `readdir`; a
+ * static rule there would be a 50-entry allowlist. That spelling is covered
+ * **empirically** instead, by the two-run probe #3687 introduced:
+ *
+ * 1. Run the server suite on a checkout with an empty `data/`; record failures.
+ * 2. Plant probe records in every `data/` subdirectory a service enumerates —
+ * every `PATHS.*` member under `data/`, every `createCollectionStore` root,
+ * and every directory `data.reference/` seeds. Use prefix-aware ids
+ * (`iss-`/`ser-`/`stb-`/`game-`/UUID): several stores pass a prefixed
+ * `idPattern` and silently ignore a probe dir that doesn't match it.
+ * 3. Re-run. Any file that fails only in run 2 is leaking.
+ *
+ * That audit found 2 leakers across 1301 files — `missions.test.js` (fixed in
+ * #3687) and nothing the static rule below doesn't already catch. Re-run it when
+ * touching this area; it is the ground truth, and this rule is the cheap
+ * always-on approximation of it.
*
* A test that genuinely needs a data root uses `createTempDataRoot()` +
* `makePathsProxy()` from `lib/mockPathsDataRoot.js`.
@@ -33,42 +49,50 @@
import { describe, it, expect } from 'vitest';
import { execFileSync } from 'child_process';
import { readFileSync } from 'fs';
-import { join, dirname } from 'path';
+import { join, dirname, relative } from 'path';
import { fileURLToPath } from 'url';
const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
-// This file quotes the forbidden spellings in its own pattern assertions below,
-// so it must exempt itself or it reports itself as the sole offender.
-const SELF = 'server/lib/testDataIsolation.guards.test.js';
+// Derived, not hardcoded: this file quotes the forbidden spellings in its own
+// pattern assertions below, so a stale literal would let it report itself as the
+// sole offender after a rename.
+const SELF = relative(REPO_ROOT, fileURLToPath(import.meta.url));
+
+// Deliberate exemption. `creativeDirectorPrompts.test.js` reads the INSTALLED
+// stage prompt in preference to the committed `data.reference/` seed on purpose:
+// the point is to catch drift between a locally-edited prompt and the code that
+// renders it. It falls back to the seed when `data/` is absent, so CI is stable,
+// and it only ever reads — it writes nothing into the user's data.
+const ALLOWED = new Set(['server/lib/creativeDirectorPrompts.test.js']);
-// Matches an install-root anchor (`process.cwd()`, `PATHS.root`,
-// `PATHS.installRoot`) joined to a `data` segment — as a `join()` argument
-// (`, 'data'`) or inside a template literal (`}/data`). Deliberately does NOT
-// match `'test-data'` or any other name that merely ends in `data`.
+// An install-root anchor or a parent climb, joined to a `data` path segment.
+// The trailing lookahead spares `data.reference` / `data-foo` / `dataDir`, and
+// the leading quote-or-slash class spares `'test-data'`.
const REAL_DATA_ROOT_RE =
- /(process\.cwd\(\)|PATHS\.(?:root|installRoot))(?:[^\n;]*?,\s*['"]data['"]|\}?\/data['"/`])/;
+ /(?:process\.cwd\(\)|PATHS\.(?:root|installRoot)|['"]\.\.['"])[^\n;]*?['"`/]data(?![\w.-])/;
// Scope to the test files the server runner globs (`server/vitest.config.js`).
-// Client tests run in jsdom and have no filesystem to leak into.
-const scopedTests = () => execFileSync('git', ['ls-files', '*.test.js'], {
+// Client tests run in jsdom and have no filesystem to leak into. Computed once —
+// the inputs are immutable for the run.
+const SCOPED_TESTS = execFileSync('git', ['ls-files', '*.test.js'], {
cwd: REPO_ROOT,
encoding: 'utf8',
})
.split('\n')
.filter((p) => /^(server|scripts|lib|autofixer)\//.test(p))
- .filter((p) => !p.startsWith('lib/slashdo/') && p !== SELF);
+ .filter((p) => !p.startsWith('lib/slashdo/') && p !== SELF && !ALLOWED.has(p));
+
+const scan = (source) => REAL_DATA_ROOT_RE.test(source);
describe('test-data isolation guard', () => {
it('finds test files to scan', () => {
// Fails loudly if the glob or the path filter ever stops matching, rather
// than reporting a vacuous pass over zero files.
- expect(scopedTests().length).toBeGreaterThan(100);
+ expect(SCOPED_TESTS.length).toBeGreaterThan(100);
});
it('no test resolves a path into the checkout\'s real data/ directory', () => {
- const offenders = scopedTests().filter((rel) =>
- REAL_DATA_ROOT_RE.test(readFileSync(join(REPO_ROOT, rel), 'utf8'))
- );
+ const offenders = SCOPED_TESTS.filter((rel) => scan(readFileSync(join(REPO_ROOT, rel), 'utf8')));
expect(offenders, [
'These tests address the checkout\'s own data/ directory, so they read (and may',
'overwrite) the developer\'s live records. Allocate a temp root instead:',
@@ -81,14 +105,21 @@ describe('test-data isolation guard', () => {
].join('\n')).toEqual([]);
});
- it('the pattern matches the shapes that shipped, and spares look-alikes', () => {
- // #3687's actual line, plus the PATHS-anchored and template-literal spellings.
- expect(REAL_DATA_ROOT_RE.test("path.join(process.cwd(), 'data', 'cos', 'missions')")).toBe(true);
- expect(REAL_DATA_ROOT_RE.test("join(PATHS.installRoot, 'data', 'runs')")).toBe(true);
- expect(REAL_DATA_ROOT_RE.test('`${process.cwd()}/data/cos`')).toBe(true);
- // Look-alikes that must stay legal.
- expect(REAL_DATA_ROOT_RE.test("join(process.cwd(), 'test-data')")).toBe(false);
- expect(REAL_DATA_ROOT_RE.test('workspacePath: process.cwd()')).toBe(false);
- expect(REAL_DATA_ROOT_RE.test("join(tempRoot, 'data', 'cos')")).toBe(false);
+ it('would have caught both spellings that shipped', () => {
+ // A bypass probe, not a pattern unit test: these are the exact lines the two
+ // known instances carried, so a rule that stops matching them fails here
+ // instead of silently passing over a re-introduced leak.
+ expect(scan("const DATA_DIR = path.join(process.cwd(), 'data', 'cos', 'missions');")).toBe(true);
+ expect(scan("const D = join(__HERE, '..', '..', 'data', 'prompts', 'stages');")).toBe(true);
+ expect(scan("join(PATHS.installRoot, 'data', 'runs')")).toBe(true);
+ expect(scan('`${process.cwd()}/data/cos`')).toBe(true);
+ });
+
+ it('spares the look-alikes that must stay legal', () => {
+ expect(scan("join(process.cwd(), 'test-data')")).toBe(false);
+ expect(scan('workspacePath: process.cwd()')).toBe(false);
+ expect(scan("join(tempRoot, 'data', 'cos')")).toBe(false);
+ expect(scan("join(__HERE, '..', '..', 'data.reference', 'prompts')")).toBe(false);
+ expect(scan("import { PATHS } from '../lib/fileUtils.js'")).toBe(false);
});
});
diff --git a/server/services/missions.test.js b/server/services/missions.test.js
index 5517cd7b13..c93e07b95c 100644
--- a/server/services/missions.test.js
+++ b/server/services/missions.test.js
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from 'vitest';
import { rmSync } from 'fs';
import path from 'path';
-import { createTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
+import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';
// Mock cosEvents before import
vi.mock('./cos.js', () => ({
@@ -13,20 +13,17 @@ vi.mock('./cos.js', () => ({
// This suite exercises the REAL file-backed mission store — createMission writes
// `PATHS.missions/<id>.json` and loadMissions enumerates that dir with a real
// readdir. Without a PATHS redirect that dir is the checkout's own
-// `data/cos/missions`, so the suite both read the developer's live missions (a
-// hand-edited or legacy record with no `subTasks` crashed getStats) and wrote its
-// own fixtures into it. Same leak class as #3683; see the isolation probe below.
-//
-// `PATHS.missions` is anchored on the install root rather than derived from
-// `PATHS.data`, so redirecting `data` alone is not enough — it needs its own
-// override, which is what `extraOverrides` is for.
-const tempRoot = createTempDataRoot('portos-missions-');
-vi.mock('../lib/fileUtils.js', async (importOriginal) => makePathsProxy(await importOriginal(), {
- dataRoot: tempRoot,
+// `data/cos/missions`, so the suite both read the developer's live missions (one
+// with no `subTasks` crashed getStats) and wrote its own fixtures into it — the
+// same leak class as #3683. `PATHS.missions` is anchored on the install root
+// rather than derived from `PATHS.data`, so it needs an override of its own.
+const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({
+ prefix: 'portos-missions-',
extraOverrides: (root) => ({ missions: path.join(root, 'cos', 'missions') }),
-}));
+});
+vi.mock('../lib/fileUtils.js', async (importOriginal) => makeProxy(await importOriginal()));
-afterAll(() => rmSync(tempRoot, { recursive: true, force: true }));
+afterAll(cleanup);
// Dynamic import, not a static one: `missions.js` reads `PATHS.missions` into a
// module-level const at load time, and a static import would run that load
@@ -394,20 +391,14 @@ describe('Missions Service', () => {
});
});
-// Isolation probe (#3687). The suite above drives the real file-backed store, so
-// without the PATHS redirect at the top of this file it reads AND writes the
-// checkout's own `data/cos/missions`. These assertions fail the same way if the
-// redirect is ever dropped, so the leak can't come back silently.
+// Isolation probe for #3687 — these fail the same way if the PATHS redirect at
+// the top of the file is ever dropped, so the leak can't come back silently.
describe('Missions Service — the suite is isolated from the checkout\'s real data/', () => {
it('resolves PATHS.missions to a temp dir outside the repo', async () => {
const { PATHS } = await import('../lib/fileUtils.js');
expect(PATHS.missions).toBe(DATA_DIR);
- expect(PATHS.missions).not.toContain('PortOS');
});
- // The behavioral half: loadMissions enumerates the dir with a real readdir, so
- // a PATHS.missions pointing at the checkout would surface the developer's own
- // missions alongside this one.
it('counts only the missions this suite created', async () => {
await createMission({ id: 'test-isolation-probe', appId: 'test-app', name: 'Isolation Probe' });
const stats = await getStats();