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: name the creating test file in temp-leak warnings and sweep stray song-revision browser roots (#10404)

Adam Eivy0b758efbc964
diff --git a/scripts/vitestTempRootSetup.js b/scripts/vitestTempRootSetup.js
index 3e8de601b2..3b7e53908a 100644
--- a/scripts/vitestTempRootSetup.js
+++ b/scripts/vitestTempRootSetup.js
@@ -3,7 +3,7 @@
* redirects temp APIs before Vitest creates its module-transform cache.
* The server's exclusive capture projects share this single global owner.
*/
-import { readdirSync, rmSync, statSync } from 'node:fs';
+import { readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { OWNER_FILE } from './lib/vitestStaleRunRoots.js';
@@ -30,6 +30,23 @@ const TOOLCHAIN_CACHE_PREFIXES = new Set([
'.org.chromium.Chromium.',
]);
+// Written by server/lib/mockPathsDataRoot.js (`<dir>\t<test file>` per root it
+// minted). Kept as a literal here: this setup file imports no server code.
+const OWNERS_FILE = '.leak-owners';
+
+function readLeakOwners(root) {
+ const owners = new Map();
+ let text = '';
+ try {
+ text = readFileSync(join(root, OWNERS_FILE), 'utf8');
+ } catch { /* no PATHS-mocking suite ran */ }
+ for (const line of text.split('\n')) {
+ const [dir, file] = line.split('\t');
+ if (dir && file) owners.set(dir, file);
+ }
+ return owners;
+}
+
export function groupLeakPrefix(name) {
return name.replace(/[0-9a-zA-Z]{6,}$/, '') || name;
}
@@ -63,18 +80,23 @@ export function teardown() {
return;
}
entries = entries
- .filter((name) => name !== OWNER_FILE)
+ .filter((name) => name !== OWNER_FILE && name !== OWNERS_FILE)
.filter((name) => !VITEST_INTERNAL_SCRATCH_DIR.test(name))
.filter((name) => !TOOLCHAIN_CACHE_PREFIXES.has(groupLeakPrefix(name)))
.filter((name) => !isEffectivelyEmpty(join(root, name)));
+ const owners = readLeakOwners(root);
const byPrefix = new Map();
for (const name of entries) {
const prefix = groupLeakPrefix(name);
- byPrefix.set(prefix, (byPrefix.get(prefix) || 0) + 1);
+ const group = byPrefix.get(prefix) || { count: 0, files: new Set() };
+ group.count++;
+ if (owners.has(name)) group.files.add(owners.get(name));
+ byPrefix.set(prefix, group);
}
- for (const [prefix, count] of byPrefix) {
- console.warn(`⚠️ test temp leak: ${prefix} ×${count}`);
+ for (const [prefix, { count, files }] of byPrefix) {
+ const by = files.size ? ` (created by ${[...files].join(', ')})` : '';
+ console.warn(`⚠️ test temp leak: ${prefix} ×${count}${by}`);
}
if (entries.length) process.exitCode = 1;
diff --git a/scripts/vitestTempRootSetup.test.js b/scripts/vitestTempRootSetup.test.js
index 0aeaff4e1f..820a220e98 100644
--- a/scripts/vitestTempRootSetup.test.js
+++ b/scripts/vitestTempRootSetup.test.js
@@ -60,4 +60,19 @@ describe('vitestTempRootSetup teardown', () => {
expect(leaked.warnings).toEqual(['⚠️ test temp leak: fixture- ×1']);
expect(leaked.exitCode).toBe(1);
});
+
+ it('names the test file that minted a leaked root, and never reports the owner sidecar itself', () => {
+ const leaked = runTeardown((root) => {
+ mkdirSync(join(root, 'mv-song-browser-Abc123'));
+ writeFileSync(join(root, 'mv-song-browser-Abc123', 'chrome-profile'), 'x');
+ mkdirSync(join(root, 'unowned-Xyz789'));
+ writeFileSync(join(root, 'unowned-Xyz789', 'data.json'), '{}');
+ writeFileSync(join(root, '.leak-owners'), 'mv-song-browser-Abc123\tsongRevision.browser.test.js\n');
+ });
+ expect(leaked.warnings.sort()).toEqual([
+ '⚠️ test temp leak: mv-song-browser- ×1 (created by songRevision.browser.test.js)',
+ '⚠️ test temp leak: unowned- ×1',
+ ]);
+ expect(leaked.exitCode).toBe(1);
+ });
});
diff --git a/server/lib/mockPathsDataRoot.js b/server/lib/mockPathsDataRoot.js
index b48c6fff19..983d82b3ca 100644
--- a/server/lib/mockPathsDataRoot.js
+++ b/server/lib/mockPathsDataRoot.js
@@ -45,16 +45,35 @@
* target the default re-rooting wouldn't produce — it merges last.
*/
-import { mkdtempSync, readdirSync, rmSync } from 'fs';
+import { appendFileSync, mkdtempSync, readdirSync, rmSync } from 'fs';
import { tmpdir } from 'os';
-import { isAbsolute, join, relative, sep } from 'path';
+import { basename, isAbsolute, join, relative, sep } from 'path';
/**
* Allocate a unique temp dir suitable for use as `PATHS.data` in a test file.
* Caller is responsible for cleanup (`rmSync` in afterAll) when needed.
*/
export function createTempDataRoot(prefix = 'portos-test-') {
- return mkdtempSync(join(tmpdir(), prefix));
+ const dir = mkdtempSync(join(tmpdir(), prefix));
+ recordTempRootOwner(dir);
+ return dir;
+}
+
+/**
+ * Sidecar the run-wide leak guard (`scripts/vitestTempRootSetup.js`) reads so a
+ * leaked `<prefix>XXXXXX` directory is reported with the test file that minted
+ * it. A random-suffixed name alone cannot identify its creator (#10404).
+ * Best-effort and basename-only: diagnostics must never fail a fixture or put a
+ * checkout path in a log.
+ */
+const TEMP_ROOT_OWNERS_FILE = '.leak-owners';
+function recordTempRootOwner(dir) {
+ try {
+ const runRoot = process.env.PORTOS_TEST_TEMP_ROOT;
+ const owner = globalThis.__vitest_worker__?.filepath;
+ if (!runRoot || !owner) return;
+ appendFileSync(join(runRoot, TEMP_ROOT_OWNERS_FILE), `${basename(dir)}\t${basename(owner)}\n`);
+ } catch { /* diagnostics only */ }
}
/**
diff --git a/server/routes/musicVideoSongRevision.browser.test.js b/server/routes/musicVideoSongRevision.browser.test.js
index 2d003838ce..ab1c7903e5 100644
--- a/server/routes/musicVideoSongRevision.browser.test.js
+++ b/server/routes/musicVideoSongRevision.browser.test.js
@@ -8,7 +8,7 @@ import { join, resolve } from 'node:path';
import { spawn } from 'node:child_process';
import { createRequire } from 'node:module';
import { chromium } from 'playwright-core';
-import { makePathsProxy, lazyTempDataRoot, cleanupTempDataRoots } from '../lib/mockPathsDataRoot.js';
+import { makePathsProxy, lazyTempDataRoot, cleanupTempDataRoots, sweepStrayTempRoots } from '../lib/mockPathsDataRoot.js';
import { errorMiddleware } from '../lib/errorHandler.js';
import { browserSuiteCanRun } from '../lib/browserSuiteGate.js';
vi.mock('../lib/paths.js', async original => makePathsProxy(await original(), { dataRoot: () => lazyTempDataRoot('mv-song-browser-') }));
@@ -27,14 +27,20 @@ const broadcast = event => io.emit('music-video:song-revision', event);
afterAll(async () => {
musicVideoEvents?.off('song-revision', broadcast);
await songs?.__testing.settle(); songs?.__setSongRevisionDepsForTests();
- await _cleanupTestBrowser({ browser, proc, cleanup: async () => {
- try {
- if (io) await new Promise(resolve => io.close(resolve));
- else if (server) await new Promise(resolve => server.close(resolve));
- } finally {
- cleanupTempDataRoots();
- }
- } });
+ try {
+ await _cleanupTestBrowser({ browser, proc, cleanup: async () => {
+ try {
+ if (io) await new Promise(resolve => io.close(resolve));
+ else if (server) await new Promise(resolve => server.close(resolve));
+ } finally {
+ cleanupTempDataRoots();
+ }
+ } });
+ } finally {
+ // A SIGKILLed Chrome leaves helper processes that can still flush into the
+ // profile (<root>/chrome) after the root was removed, recreating it (#10404).
+ await sweepStrayTempRoots('mv-song-browser-');
+ }
});
describe.skipIf(!canRun)('song revision in Chrome (client dependencies required)', () => {