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

perf: key the recorded test memo on the setup modules each test loaded (#12298)

[._.]/ Adam Eivyd895e624a10b
diff --git a/docs/GITHUB_ACTIONS.md b/docs/GITHUB_ACTIONS.md
index b9694bb9c3..1c44306a86 100644
--- a/docs/GITHUB_ACTIONS.md
+++ b/docs/GITHUB_ACTIONS.md
@@ -938,6 +938,18 @@ version, `process.platform`, `process.arch`; and `TEST_MEMO_ENV` (`CI`,
`PORTOS_TEST_QUIET`, `PORTOS_TEST_PYTHON`, `CHROME_PATH`, `TZ`, `LANG`,
`LC_ALL`, `CI_EXPECT_SLASHDO_SUBMODULE`, …).
+The recorded key takes the setup phase from the record too. Vitest's depfile
+lists, in every test file's record, the modules its custom environment and
+`setupFiles` loaded, so for the setup entries the config reaches
+(`WORKSPACE_SETUP_ENTRIES`: `server/vitest.setup.js` and
+`server/test/mediaRegistryEnvironment.js`, `client/src/test/setup.js`) the
+recorded key's workspace base stops the static walk at them: a module the
+setup files only mock or never load (`services/instances.js` and its
+closure) no longer invalidates every recorded key. A record that does not
+name every setup entry (a per-file environment pragma) keeps the full static
+base, and a setup-phase problem the static walk finds still fails both keys
+closed. The floor key always walks the setup closure.
+
**Never memoized (fails closed).** A file in the planner's `ALWAYS_RUN_TESTS`
or in `TEST_MEMO_NEVER` (the tree-wide convention and catalog scanners); a
file whose closure contains the `// test-memo: never` marker; a test file or
diff --git a/scripts/lib/testMemo.js b/scripts/lib/testMemo.js
index 33ba26f81e..3ef283ce8d 100644
--- a/scripts/lib/testMemo.js
+++ b/scripts/lib/testMemo.js
@@ -26,8 +26,10 @@
* `DIRECTORY_INPUT_LIMIT`); the installed version of every package the
* members reach; the sibling imports of a Python sidecar; the version of an
* external program a member reaches (`TEST_MEMO_TOOLS`); the workspace's
- * Vitest configuration closure and manifest; the full Node version, platform
- * and arch; and `TEST_MEMO_ENV`.
+ * Vitest configuration closure and manifest (for a recorded key, the setup
+ * files and custom environment come from the record instead of the static
+ * walk: `WORKSPACE_SETUP_ENTRIES`); the full Node version, platform and
+ * arch; and `TEST_MEMO_ENV`.
*
* Fail closed. A test is not memoizable when a member reads a path the
* analyzer cannot resolve, when a test or `scripts/` member spawns `git`
@@ -131,6 +133,21 @@ export const WORKSPACE_BASE_INPUTS = Object.freeze({
'server/scripts/init-db.sql', 'server/scripts/setup-db-test.mjs'],
});
+/**
+ * Setup-phase entries: each config's `setupFiles` and its repository-local
+ * custom `environment`. Vitest 5's depfile already lists what they load in
+ * every test file's record (`runBaseTests` seeds a file's record with the
+ * environment loader's modules; setup files run inside the file's own
+ * recording), so the recorded key takes them from the record and its
+ * workspace base stops the static walk at them. The floor key walks them.
+ * An entry the config no longer reaches is ignored rather than trusted.
+ */
+export const WORKSPACE_SETUP_ENTRIES = Object.freeze({
+ server: ['server/vitest.setup.js', 'server/test/mediaRegistryEnvironment.js'],
+ client: ['client/src/test/setup.js'],
+ db: ['server/vitest.setup.js', 'server/test/mediaRegistryEnvironment.js'],
+});
+
const JS_EXT = /\.(?:[cm]?js|jsx|tsx?)$/;
const RESOLVE_EXTENSIONS = ['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.json'];
const RESOLVE_INDEXES = ['/index.js', '/index.jsx', '/index.ts', '/index.tsx'];
@@ -1080,7 +1097,7 @@ export function createTestMemoAnalyzer({ fs, workspaceRel, env = {}, platform =
};
/** Fold the facts of `members` (and, when walking, of everything they reach) into one verdict. */
- const gather = (roots, { walk, testRel = null }) => {
+ const gather = (roots, { walk, testRel = null, stop = null }) => {
const members = new Set();
// A side module (read as text, spawned, loaded natively or as a worker)
// is never narrowed by the recording: it and its own closure are walked
@@ -1096,7 +1113,7 @@ export function createTestMemoAnalyzer({ fs, workspaceRel, env = {}, platform =
let git = false;
while (stack.length) {
const { rel, walkEdges } = stack.pop();
- if (members.has(rel)) continue;
+ if (members.has(rel) || stop?.has(rel)) continue;
members.add(rel);
const f = factsFor(rel);
if (walkEdges) for (const dep of f.modules) if (!members.has(dep)) stack.push({ rel: dep, walkEdges: true });
@@ -1124,7 +1141,7 @@ export function createTestMemoAnalyzer({ fs, workspaceRel, env = {}, platform =
return { members, inputs, absent, packages: packageVersions, tools, unresolvedEdges, unresolvedInputs, never, git };
};
- const closureOf = (entry, testRel = entry) => gather([entry], { walk: true, testRel });
+ const closureOf = (entry, testRel = entry, stop = null) => gather([entry], { walk: true, testRel, stop });
const snapshotInputs = (testRel) => {
const base = posix.basename(testRel);
@@ -1150,21 +1167,35 @@ export function createTestMemoAnalyzer({ fs, workspaceRel, env = {}, platform =
...TEST_MEMO_ENV.map((name) => `env ${name} ${env[name] === undefined ? '<unset>' : env[name]}`),
];
- /** The workspace-level inputs every key shares: the Vitest config closure and manifest. */
- const workspaceBase = (roots) => {
- const files = new Set();
- const packageVersions = new Map();
- const unresolved = [];
- for (const root of roots) {
- if (fs.kind(root) !== 'file') { unresolved.push(`missing workspace input ${root}`); continue; }
- if (!JS_EXT.test(root)) { files.add(root); continue; }
- const closure = closureOf(root, null);
- for (const rel of closure.members) files.add(rel);
- for (const rel of closure.inputs) files.add(rel);
- for (const [name, version] of closure.packages) packageVersions.set(name, version);
- unresolved.push(...closure.unresolvedEdges, ...closure.unresolvedInputs);
- }
- return { files, packages: packageVersions, unresolved };
+ /**
+ * The workspace-level inputs every key shares: the Vitest config closure and
+ * manifest. `recorded` is the same walk stopped at the setup entries the
+ * config reaches (`WORKSPACE_SETUP_ENTRIES`), for a recorded key whose
+ * module list names every one of `recorded.entries`. It keeps the full
+ * walk's `unresolved`, so a setup-phase problem fails both keys closed.
+ */
+ const workspaceBase = (roots, setupEntries = []) => {
+ const walkBase = (stop) => {
+ const files = new Set();
+ const packageVersions = new Map();
+ const unresolved = [];
+ for (const root of roots) {
+ if (stop?.has(root)) continue;
+ if (fs.kind(root) !== 'file') { unresolved.push(`missing workspace input ${root}`); continue; }
+ if (!JS_EXT.test(root)) { files.add(root); continue; }
+ const closure = closureOf(root, null, stop);
+ for (const rel of closure.members) files.add(rel);
+ for (const rel of closure.inputs) files.add(rel);
+ for (const [name, version] of closure.packages) packageVersions.set(name, version);
+ unresolved.push(...closure.unresolvedEdges, ...closure.unresolvedInputs);
+ }
+ return { files, packages: packageVersions, unresolved };
+ };
+ const full = walkBase(null);
+ const entries = setupEntries.filter((rel) => full.files.has(rel));
+ if (!entries.length) return { ...full, recorded: null };
+ const narrowed = walkBase(new Set(entries));
+ return { ...full, recorded: { ...narrowed, unresolved: full.unresolved, entries } };
};
const verdict = (testRel, gathered, extra, baseClosure, { requireEdges }) => {
@@ -1208,7 +1239,10 @@ export function createTestMemoAnalyzer({ fs, workspaceRel, env = {}, platform =
recordedVerdict = { key: null, memoizable: false, reason: `loaded a module outside the repository: ${outside}`, reasons: [] };
} else {
const gathered = gather(new Set([testRel, ...recorded]), { walk: false, testRel });
- recordedVerdict = verdict(testRel, gathered, fixtures, baseClosure, { requireEdges: false });
+ // A record missing a setup entry (a per-file environment pragma, a
+ // Vitest that stopped recording them) keeps the full static base.
+ const base = baseClosure.recorded?.entries.every((rel) => gathered.members.has(rel)) ? baseClosure.recorded : baseClosure;
+ recordedVerdict = verdict(testRel, gathered, fixtures, base, { requireEdges: false });
}
}
return { floor, recorded: recordedVerdict, closure: closure.members, inputs: closure.inputs, edgesResolved: closure.unresolvedEdges.length === 0 };
@@ -1225,7 +1259,7 @@ export function createTestMemoAnalyzer({ fs, workspaceRel, env = {}, platform =
*/
export function computeTestMemoKeys(testFiles, { fs, workspace, env, platform, arch, nodeVersion, toolVersion, records = new Map() }) {
const analyzer = createTestMemoAnalyzer({ fs, workspaceRel: workspace, env, platform, arch, nodeVersion, toolVersion });
- const baseClosure = analyzer.workspaceBase(WORKSPACE_BASE_INPUTS[workspace] ?? []);
+ const baseClosure = analyzer.workspaceBase(WORKSPACE_BASE_INPUTS[workspace] ?? [], WORKSPACE_SETUP_ENTRIES[workspace] ?? []);
const out = new Map();
for (const test of testFiles) out.set(test, analyzer.analyzeTest(test, { baseClosure, recorded: records.get(test) ?? null }));
return out;
diff --git a/scripts/lib/testMemo.test.js b/scripts/lib/testMemo.test.js
index af5cb3604c..97396c9965 100644
--- a/scripts/lib/testMemo.test.js
+++ b/scripts/lib/testMemo.test.js
@@ -201,6 +201,36 @@ describe('recorded key', () => {
expect(withRecord(unresolvable, 'server/lib/side.test.js', ['server/lib/side.test.js']).recorded).toMatchObject({ memoizable: false, reason: expect.stringContaining('readServerSource') });
});
+ it('takes the setup files and custom environment from the record, not their static closure', () => {
+ const files = {
+ ...TREE,
+ 'server/vitest.config.js': [
+ "import { vitestCiPool } from '../scripts/vitestCiPool.js';",
+ "export default { test: { environment: fileURLToPath(new URL('./test/mediaRegistryEnvironment.js', import.meta.url)), setupFiles: ['./vitest.setup.js'] } };",
+ ].join('\n'),
+ 'server/vitest.setup.js': "import { helper } from './lib/setupHelper.js';\nvi.mock('./services/instances.js', async (importOriginal) => importOriginal());\n",
+ 'server/lib/setupHelper.js': 'export const helper = 1;\n',
+ 'server/services/instances.js': "import { peer } from '../lib/peerLeaf.js';\nexport const getPeers = () => [peer];\n",
+ 'server/lib/peerLeaf.js': 'export const peer = 1;\n',
+ 'server/test/mediaRegistryEnvironment.js': "import { observe } from './envHelper.js';\nexport default { name: 'env', setup: observe };\n",
+ 'server/test/envHelper.js': 'export const observe = () => ({ teardown() {} });\n',
+ };
+ const loaded = [...recorded, 'server/vitest.setup.js', 'server/lib/setupHelper.js', 'server/test/mediaRegistryEnvironment.js', 'server/test/envHelper.js'];
+ const change = (rel) => ({ ...files, [rel]: `${files[rel]}// changed\n` });
+ const baseline = withRecord(files, 'server/lib/a.test.js', loaded);
+ expect(baseline.recorded.memoizable).toBe(true);
+ // The setup phase never loaded instances.js (no suite imported it): its closure is outside the recorded key, inside the floor key.
+ expect(withRecord(change('server/lib/peerLeaf.js'), 'server/lib/a.test.js', loaded).recorded.key).toBe(baseline.recorded.key);
+ expect(withRecord(change('server/lib/peerLeaf.js'), 'server/lib/a.test.js', loaded).floor.key).not.toBe(baseline.floor.key);
+ for (const rel of ['server/lib/setupHelper.js', 'server/test/envHelper.js', 'server/vitest.setup.js', 'scripts/vitestCiPool.js']) {
+ expect(withRecord(change(rel), 'server/lib/a.test.js', loaded).recorded.key, rel).not.toBe(baseline.recorded.key);
+ }
+ // A record without the environment (a per-file pragma) keeps the full static base.
+ const withoutEnv = loaded.filter((rel) => !rel.startsWith('server/test/'));
+ expect(withRecord(change('server/lib/peerLeaf.js'), 'server/lib/a.test.js', withoutEnv).recorded.key)
+ .not.toBe(withRecord(files, 'server/lib/a.test.js', withoutEnv).recorded.key);
+ });
+
it('is not memoizable when the loaded set reaches outside the repository', () => {
const info = keyOf(TREE, 'server/lib/a.test.js', { records: new Map([['server/lib/a.test.js', [...recorded, '../outside/x.js']]]) });
expect(info.recorded).toMatchObject({ memoizable: false, reason: expect.stringContaining('outside the repository') });
diff --git a/scripts/lib/testMemoRunner.js b/scripts/lib/testMemoRunner.js
index 47a499031a..827b20b2b4 100644
--- a/scripts/lib/testMemoRunner.js
+++ b/scripts/lib/testMemoRunner.js
@@ -32,7 +32,7 @@ import { tmpdir } from 'node:os';
import * as nodePath from 'node:path';
import { join, relative, sep } from 'node:path';
import { resolveTestChrome } from '../../server/lib/browserSuiteGate.js';
-import { WORKSPACE_BASE_INPUTS, createDiskFs, createTestMemoAnalyzer, relatedTests } from './testMemo.js';
+import { WORKSPACE_BASE_INPUTS, WORKSPACE_SETUP_ENTRIES, createDiskFs, createTestMemoAnalyzer, relatedTests } from './testMemo.js';
import { readMemoEntry, readMemoRecord, writeMemoEntry, writeMemoRecord } from './testMemoStore.js';
import { TEST_SCOPES, listWorkspaceTests, toRepoPath, toWorkspacePath, workspaceOf } from './workspaceTests.js';
@@ -56,7 +56,7 @@ export function planTestMemo({ scope, mode, repoFiles, repoSources, memoDir, rep
const candidates = mode === 'files' ? uniqueSorted(repoFiles) : uniqueSorted([...workspaceTests, ...repoFiles]);
const options = { fs: createDiskFs(repoRoot, nodeFs, nodePath), workspaceRel: workspaceOf(scope), env, platform, arch, nodeVersion, toolVersion };
const analyzer = createTestMemoAnalyzer(options);
- const baseClosure = analyzer.workspaceBase(WORKSPACE_BASE_INPUTS[scope]);
+ const baseClosure = analyzer.workspaceBase(WORKSPACE_BASE_INPUTS[scope], WORKSPACE_SETUP_ENTRIES[scope]);
const analyze = (test, recorded) => analyzer.analyzeTest(test, { baseClosure, recorded });
// A second analyzer with empty caches re-reads every input after the run,
// so an entry is written only for inputs that did not change meanwhile.
@@ -64,7 +64,7 @@ export function planTestMemo({ scope, mode, repoFiles, repoSources, memoDir, rep
const freshAnalyze = (test, recorded) => {
if (!fresh) {
const again = createTestMemoAnalyzer(options);
- fresh = { analyzer: again, baseClosure: again.workspaceBase(WORKSPACE_BASE_INPUTS[scope]) };
+ fresh = { analyzer: again, baseClosure: again.workspaceBase(WORKSPACE_BASE_INPUTS[scope], WORKSPACE_SETUP_ENTRIES[scope]) };
}
return fresh.analyzer.analyzeTest(test, { baseClosure: fresh.baseClosure, recorded });
};