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

fix: scope the mkdtemp guard to the true repo root, fix 6 more leaks (#9000)

[._.]/ Adam Eivy45f12e700e89
diff --git a/scripts/jev_head_kit.test.js b/scripts/jev_head_kit.test.js
index 3f71b5ee70..ae901c3f00 100644
--- a/scripts/jev_head_kit.test.js
+++ b/scripts/jev_head_kit.test.js
@@ -14,11 +14,11 @@
*/
import { execFileSync } from 'node:child_process';
-import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
-import { describe, expect, it } from 'vitest';
+import { afterEach, describe, expect, it } from 'vitest';
import { resolveTestPython } from '../server/lib/testHelper.js';
const python = resolveTestPython();
@@ -57,9 +57,18 @@ const head = (overrides = {}) => ({
...overrides,
});
+const tempRoots = [];
+
+afterEach(() => {
+ for (const root of tempRoots.splice(0)) {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
/** A heads directory holding `scope-adherence.json`, plus a file outside it. */
function headsDir(contents = head()) {
const root = mkdtempSync(join(tmpdir(), 'portos-jev-kit-'));
+ tempRoots.push(root);
const dir = join(root, 'heads');
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, 'scope-adherence.json'), JSON.stringify(contents));
diff --git a/scripts/upscale_ltx25.test.js b/scripts/upscale_ltx25.test.js
index 4023cd2c63..162a4ef11c 100644
--- a/scripts/upscale_ltx25.test.js
+++ b/scripts/upscale_ltx25.test.js
@@ -1,9 +1,9 @@
import { execFileSync } from 'node:child_process';
-import { mkdtempSync, writeFileSync } from 'node:fs';
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
-import { describe, expect, it } from 'vitest';
+import { afterAll, describe, expect, it } from 'vitest';
import { resolveTestPython, PY_TEST_TIMEOUT_MS, PY_SUBPROCESS_TIMEOUT_MS } from '../server/lib/testHelper.js';
const script = join(dirname(fileURLToPath(import.meta.url)), 'upscale_ltx25.py');
@@ -42,6 +42,21 @@ const REFERENCE = join(scratch, 'source.mp4');
const ADAPTER = join(scratch, 'adapter.safetensors');
writeFileSync(REFERENCE, 'not really a video');
+// Every mkdtempSync root this file creates (the module-level scratch plus one
+// per test below) so a single afterAll sweeps them all.
+const tempRoots = [scratch];
+const makeTempRoot = (prefix) => {
+ const root = mkdtempSync(join(tmpdir(), prefix));
+ tempRoots.push(root);
+ return root;
+};
+
+afterAll(() => {
+ for (const root of tempRoots.splice(0)) {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
// A real safetensors file: 8-byte little-endian header length, then the JSON
// header. Written rather than mocked because the header parse IS the thing
// under test — the runner reads `reference_downscale_factor` off this exact
@@ -164,7 +179,7 @@ describe.skipIf(!pyBin)('upscale_ltx25.py — capability gate (#6512)', () => {
// LTX-2.3 Gemma 3 id — the wrong conditioner for these weights AND an
// unannounced multi-GB download. That fallback is why this is a refusal.
it('refuses a model pack missing its own text encoder', () => {
- const pack = mkdtempSync(join(tmpdir(), 'portos-ltx25-pack-'));
+ const pack = makeTempRoot('portos-ltx25-pack-');
expect(call(`runner.validate_model_dir(${JSON.stringify(pack)})`))
.toMatch(/^REJECTED:.*text_encoder\/config\.json/);
}, PY_TEST_TIMEOUT_MS);
@@ -292,7 +307,7 @@ describe.skipIf(!pyBin)('upscale_ltx25.py — transformer header (#6512)', () =>
// Mirrors BasePipeline._resolve_safetensors: plain name wins, else the
// lexicographically last versioned file, else None so the caller can say so.
it('resolves the same transformer file the pipeline would', () => {
- const pack = mkdtempSync(join(tmpdir(), 'portos-ltx25-dit-'));
+ const pack = makeTempRoot('portos-ltx25-dit-');
const resolve = (dir) => call(`repr(runner.resolve_transformer_path(__import__("pathlib").Path(${JSON.stringify(dir)})) and runner.resolve_transformer_path(__import__("pathlib").Path(${JSON.stringify(dir)})).name)`);
expect(resolve(pack)).toBe('None');
@@ -310,7 +325,7 @@ describe.skipIf(!pyBin)('upscale_ltx25.py — transformer header (#6512)', () =>
// layout itself — and refuses dev WITHOUT the LoRA, because the distilled
// schedule on an un-distilled model is not a fallback, it is a wrong render.
it('resolves the dev + distilled-LoRA layout, preferring a pre-fused distilled file', () => {
- const pack = mkdtempSync(join(tmpdir(), 'portos-ltx25-layout-'));
+ const pack = makeTempRoot('portos-ltx25-layout-');
const layout = (dir) => call(`(lambda t, loras: [t.name, [(__import__("pathlib").Path(p).name, s) for p, s in loras]])(*runner.resolve_transformer_layout(__import__("pathlib").Path(${JSON.stringify(dir)})))`);
expect(layout(pack)).toMatch(/^REJECTED:.*no transformer weight file/);
diff --git a/scripts/upscale_ltx25_cuda.test.js b/scripts/upscale_ltx25_cuda.test.js
index d37ab98abe..0510aef20d 100644
--- a/scripts/upscale_ltx25_cuda.test.js
+++ b/scripts/upscale_ltx25_cuda.test.js
@@ -1,9 +1,9 @@
import { execFileSync } from 'node:child_process';
-import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
-import { describe, expect, it } from 'vitest';
+import { afterAll, describe, expect, it } from 'vitest';
import { resolveTestPython, PY_TEST_TIMEOUT_MS, PY_SUBPROCESS_TIMEOUT_MS } from '../server/lib/testHelper.js';
const scriptDir = dirname(fileURLToPath(import.meta.url));
@@ -49,6 +49,16 @@ const REFERENCE = join(scratch, 'source.mp4');
const ADAPTER = join(scratch, 'adapter.safetensors');
writeFileSync(REFERENCE, 'not really a video');
+// Every mkdtempSync root this file creates (the module-level scratch plus one
+// per writePack() call) so a single afterAll sweeps them all.
+const tempRoots = [scratch];
+
+afterAll(() => {
+ for (const root of tempRoots.splice(0)) {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
// A real safetensors file: 8-byte little-endian header length, then the JSON
// header. Written rather than mocked because the header parse IS the thing
// under test — the runner reads both the declared downscale factor and every
@@ -71,6 +81,7 @@ writeSafetensors(ADAPTER, { __metadata__: { reference_downscale_factor: '2' } })
const MODEL_FILES = JSON.parse(call('__import__("json").dumps(runner.MODEL_FILES)'));
const writePack = (files = Object.values(MODEL_FILES)) => {
const pack = mkdtempSync(join(tmpdir(), 'portos-ltx25-cuda-pack-'));
+ tempRoots.push(pack);
for (const relative of files) {
const target = join(pack, ...relative.split('/'));
mkdirSync(dirname(target), { recursive: true });
diff --git a/server/lib/mkdtempCleanup.guards.test.js b/server/lib/mkdtempCleanup.guards.test.js
index 13ee060528..c009fdf2d3 100644
--- a/server/lib/mkdtempCleanup.guards.test.js
+++ b/server/lib/mkdtempCleanup.guards.test.js
@@ -48,7 +48,14 @@ import { execFileSync } from 'child_process';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
-const SERVER_ROOT = dirname(fileURLToPath(import.meta.url));
+// This file lives at server/lib/, so REPO_ROOT needs two levels up. Getting
+// this wrong silently drops the repo-root scripts/, lib/, and autofixer/
+// trees from `git ls-files` (it would run with cwd=server/, so a top-level
+// scripts/foo.test.js is never returned) — those trees are part of the same
+// `server` vitest run (see AGENTS.md: "ALSO globs ../scripts, ../lib,
+// ../autofixer") and must be scanned too.
+const LIB_DIR = dirname(fileURLToPath(import.meta.url));
+const SERVER_ROOT = dirname(LIB_DIR);
const REPO_ROOT = dirname(SERVER_ROOT);
// Test-bearing trees where mkdtemp cleanup is required
diff --git a/server/services/agentWorkspacePrep.test.js b/server/services/agentWorkspacePrep.test.js
index 9a173f757e..cc275e67d0 100644
--- a/server/services/agentWorkspacePrep.test.js
+++ b/server/services/agentWorkspacePrep.test.js
@@ -289,16 +289,21 @@ describe('prepareAgentWorkspace — workspace validation (#3180)', () => {
});
it('blocks when the resolved workspace is a file, not a directory', async () => {
- const { mkdtempSync, writeFileSync } = await import('fs');
+ const { mkdtempSync, rmSync, writeFileSync } = await import('fs');
const { tmpdir } = await import('os');
const { join } = await import('path');
- const file = join(mkdtempSync(join(tmpdir(), 'prep-')), 'a-file.txt');
- writeFileSync(file, 'x');
- getAppWorkspace.mockResolvedValue(file);
- const task = { id: 't-file', taskType: 'user', metadata: { app: 'primes' } };
- const r = await prepareAgentWorkspace({ agentId: 'agent-f', task });
- expect(r.outcome).toBe('blocked');
- expect(r.reason).toContain('not a directory');
+ const root = mkdtempSync(join(tmpdir(), 'prep-'));
+ try {
+ const file = join(root, 'a-file.txt');
+ writeFileSync(file, 'x');
+ getAppWorkspace.mockResolvedValue(file);
+ const task = { id: 't-file', taskType: 'user', metadata: { app: 'primes' } };
+ const r = await prepareAgentWorkspace({ agentId: 'agent-f', task });
+ expect(r.outcome).toBe('blocked');
+ expect(r.reason).toContain('not a directory');
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
});
// The repoPath field accepts a literal '~/...' — the CoS path must expand it
diff --git a/server/services/jevCorpusBuilder.test.js b/server/services/jevCorpusBuilder.test.js
index a33979eae0..57cfd0acb1 100644
--- a/server/services/jevCorpusBuilder.test.js
+++ b/server/services/jevCorpusBuilder.test.js
@@ -7,13 +7,23 @@
* to disk despite a refusal.
*/
-import { mkdtemp, mkdir, writeFile, readdir } from 'fs/promises';
+import { mkdtemp, mkdir, writeFile, readdir, rm } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
-const repoPath = await mkdtemp(join(tmpdir(), 'portos-jev-repo-'));
+// Real filesystem mkdtemp roots this file creates outside the lazyTempDataRoot
+// mock (which cleanupTempDataRoots() below already handles) — tracked here so
+// the final afterAll can remove them too.
+const repoTempRoots = [];
+const makeRepoTempRoot = async (prefix) => {
+ const root = await mkdtemp(join(tmpdir(), prefix));
+ repoTempRoots.push(root);
+ return root;
+};
+
+const repoPath = await makeRepoTempRoot('portos-jev-repo-');
// The shared helper re-roots every `PATHS` member under `data/`, not just
// `PATHS.data` — a bare spread leaves the rest pointing at the live install.
@@ -31,7 +41,10 @@ vi.mock('../lib/childProcess.js', () => ({
const { buildScopeAdherenceCorpus } = await import('./jevCorpusBuilder.js');
-afterAll(() => cleanupTempDataRoots());
+afterAll(async () => {
+ cleanupTempDataRoots();
+ await Promise.all(repoTempRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
+});
// Enough distinct goals that the retriever has something to rank, and enough
// prose per clause to clear the parser's minimum.
@@ -112,7 +125,7 @@ describe('buildScopeAdherenceCorpus', () => {
});
it('refuses a repository that states no product intent', async () => {
- const bare = await mkdtemp(join(tmpdir(), 'portos-jev-bare-'));
+ const bare = await makeRepoTempRoot('portos-jev-bare-');
mockForge({ merged: changes(60) });
expect(await buildScopeAdherenceCorpus({ repoPath: bare })).toEqual({ ok: false, code: 'jev-corpus-no-clauses' });
// A missing repoPath must not silently grade against this install's own.
diff --git a/server/services/sprites/localAnimationRender.test.js b/server/services/sprites/localAnimationRender.test.js
index 0706292784..ed6083d7e3 100644
--- a/server/services/sprites/localAnimationRender.test.js
+++ b/server/services/sprites/localAnimationRender.test.js
@@ -9,14 +9,18 @@
* without being mistaken for a dead one.
*/
-import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { mkdtempSync } from 'fs';
+import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';
+import { mkdtempSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { mkdir, writeFile, readFile } from 'fs/promises';
const TEST_ROOT = mkdtempSync(join(tmpdir(), 'sprite-local-render-test-'));
+afterAll(() => {
+ rmSync(TEST_ROOT, { recursive: true, force: true });
+});
+
vi.mock('../../lib/fileUtils.js', async (importOriginal) => {
const actual = await importOriginal();
Object.assign(actual.PATHS, {