Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

test: fakePlatform's temp dirs no longer outlive the run

reviewe3e7ad37c276
diff --git a/CLAUDE.md b/CLAUDE.md
index 4e5fad56f..b292206f4 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -7270,6 +7270,18 @@ points at tmux's `server_accept()` calling `fatal()` under the suite's process/f
memory-starved machine, and two identical runs finished clean. Sharing a server with the user's live
sessions is a hazard whatever kills it; this removes the hazard, not a proven cause.
+**`fakePlatform()`'s directories live exactly as long as the run** (`test/setup/fake-platform-root.ts`,
+the same per-RUN `globalSetup` shape as the tmux sandbox). Each call used to `mkdtemp` in the system
+temp dir at construction and nothing removed it: a development server running the suite repeatedly
+collected ~395,000 `nodeterm-fake-*` directories, `/tmp` ran out of inodes while still showing GBs
+free, and full runs failed in 1,201 of 1,207 files with ENOSPC. Measured on 40 suites that use it:
+271 directories left behind before, 0 after. Two halves, both needed: the directory is made on first
+READ of `userDataDir` (a test that passes its own, or never reads it, makes none), and it is made
+under a run root that teardown removes once every worker is gone. **Per run, not per file**: a
+per-file `afterAll` would sweep a directory while a store's debounced write could still land in it,
+turning a leak into an ENOENT thrown from a timer. A run killed before teardown leaves ONE directory.
+`platform-fake.test.ts` fails if the root is not in effect, so dropping the `globalSetup` entry is loud.
+
## Conventions
- **Two docs, two audiences — keep both.** This file holds the deep invariants with their
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index acd28ce6d..e7ac446bc 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1043,6 +1043,12 @@ tmux without carrying `TMUX_TMPDIR` into it, which is the one way left to escape
`src/core/tmux-socket-isolation.guard.test.ts` holds the short allowlist of suites that name a
production socket on purpose; adding a third is a review conversation, not a checkbox.
+**A test's temp directory must go away when the run does.** `fakePlatform()`'s `userDataDir` is made
+on first read under one per-run root (`test/setup/fake-platform-root.ts`), and that root is removed
+after the last worker exits. It used to be one `mkdtemp` in the system temp dir per call, never
+removed, and a development server collected ~395,000 of them until `/tmp` ran out of inodes and whole
+runs failed with ENOSPC. If you `mkdtemp` in a test yourself, remove it in `afterEach`/`afterAll`.
+
**An `infinite` CSS animation is a frame loop, and it runs whether or not anyone is looking.** A
running animation makes the compositor produce a frame every vsync — 120/s on a ProMotion display —
and re-raster the window each time; measured on a 40-terminal canvas, ONE visible pulsing node took
diff --git a/src/core/platform-fake.test.ts b/src/core/platform-fake.test.ts
new file mode 100644
index 000000000..933924587
--- /dev/null
+++ b/src/core/platform-fake.test.ts
@@ -0,0 +1,91 @@
+import fs from 'fs'
+import os from 'os'
+import path from 'path'
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { FAKE_PLATFORM_ROOT_ENV, enterFakePlatformRoot, fakePlatform, leaveFakePlatformRoot } from './platform-fake'
+
+// Read before any test below re-points it: what the vitest run itself set up.
+const runRootAtLoad = process.env[FAKE_PLATFORM_ROOT_ENV]
+
+// fakePlatform() used to mkdtemp a directory in the system temp dir on EVERY call and never remove
+// it — ~395,000 of them filled a development server's /tmp inodes. These pin the three halves of the
+// fix: made only when read, made under the run's root, and the root removed at the end of the run.
+describe('fakePlatform userDataDir', () => {
+ let saved: string | undefined
+ let root: string
+
+ beforeEach(() => {
+ saved = process.env[FAKE_PLATFORM_ROOT_ENV]
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-fake-root-test-'))
+ process.env[FAKE_PLATFORM_ROOT_ENV] = root
+ })
+ afterEach(() => {
+ if (saved === undefined) delete process.env[FAKE_PLATFORM_ROOT_ENV]
+ else process.env[FAKE_PLATFORM_ROOT_ENV] = saved
+ fs.rmSync(root, { recursive: true, force: true })
+ })
+
+ it('makes no directory until userDataDir is read', () => {
+ fakePlatform()
+ fakePlatform()
+ expect(fs.readdirSync(root)).toEqual([])
+ })
+
+ it('makes one fresh directory under the run root on first read, and keeps it', () => {
+ const p = fakePlatform()
+ const dir = p.userDataDir
+ expect(path.dirname(dir)).toBe(root)
+ expect(path.basename(dir)).toMatch(/^nodeterm-fake-/)
+ expect(fs.statSync(dir).isDirectory()).toBe(true)
+ expect(p.userDataDir).toBe(dir)
+ expect(fakePlatform().userDataDir).not.toBe(dir)
+ expect(fs.readdirSync(root)).toHaveLength(2)
+ })
+
+ it('makes nothing when the test passes its own userDataDir', () => {
+ const p = fakePlatform({ userDataDir: '/nonexistent/own-dir' })
+ expect(p.userDataDir).toBe('/nonexistent/own-dir')
+ expect(fs.readdirSync(root)).toEqual([])
+ })
+
+ it('carries the directory through a spread copy', () => {
+ const p = { ...fakePlatform() }
+ expect(path.dirname(p.userDataDir)).toBe(root)
+ expect(fs.readdirSync(root)).toHaveLength(1)
+ })
+
+ it('falls back to the system temp dir when the run root is gone', () => {
+ fs.rmSync(root, { recursive: true, force: true })
+ const dir = fakePlatform().userDataDir
+ try {
+ expect(path.dirname(dir)).toBe(os.tmpdir())
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+ })
+})
+
+describe('the run root (test/setup/fake-platform-root.ts)', () => {
+ it('is in effect for this run — vitest.config.ts keeps it in globalSetup', () => {
+ // Without it every fakePlatform() directory lands in the system temp dir and is never removed.
+ expect(runRootAtLoad, 'fake-platform root not set — see test/setup/fake-platform-root.ts').toBeTruthy()
+ expect(fs.statSync(runRootAtLoad!).isDirectory()).toBe(true)
+ })
+
+ it('removes every directory the run made, and the variable with it', () => {
+ const saved = process.env[FAKE_PLATFORM_ROOT_ENV]
+ try {
+ const runRoot = enterFakePlatformRoot()
+ expect(process.env[FAKE_PLATFORM_ROOT_ENV]).toBe(runRoot)
+ const dir = fakePlatform().userDataDir
+ fs.writeFileSync(path.join(dir, 'state.json'), '{}')
+ expect(dir.startsWith(runRoot + path.sep)).toBe(true)
+ leaveFakePlatformRoot(runRoot)
+ expect(fs.existsSync(runRoot)).toBe(false)
+ expect(process.env[FAKE_PLATFORM_ROOT_ENV]).toBeUndefined()
+ } finally {
+ if (saved === undefined) delete process.env[FAKE_PLATFORM_ROOT_ENV]
+ else process.env[FAKE_PLATFORM_ROOT_ENV] = saved
+ }
+ })
+})
diff --git a/src/core/platform-fake.ts b/src/core/platform-fake.ts
index f9435c7db..9a972ae4b 100644
--- a/src/core/platform-fake.ts
+++ b/src/core/platform-fake.ts
@@ -1,8 +1,35 @@
-import { mkdtempSync } from 'fs'
+import { existsSync, mkdtempSync, rmSync } from 'fs'
import os from 'os'
import path from 'path'
import type { CorePlatform } from './platform'
+/**
+ * The per-RUN parent of every directory `fakePlatform` makes. `test/setup/fake-platform-root.ts`
+ * (vitest `globalSetup`) creates it in the main process, so the workers inherit the variable, and
+ * removes it once every worker is gone — at that point no store's debounced write can still land
+ * in a directory that was swept from under it, which a per-file `afterAll` could not promise.
+ */
+export const FAKE_PLATFORM_ROOT_ENV = 'NODETERM_FAKE_PLATFORM_ROOT'
+
+/** Make the run's root and point this process (and so every worker it starts) at it. */
+export function enterFakePlatformRoot(): string {
+ const dir = mkdtempSync(path.join(os.tmpdir(), 'nodeterm-fake-run-'))
+ process.env[FAKE_PLATFORM_ROOT_ENV] = dir
+ return dir
+}
+
+/** Remove the run's root with everything made under it. */
+export function leaveFakePlatformRoot(dir: string): void {
+ rmSync(dir, { recursive: true, force: true })
+ if (process.env[FAKE_PLATFORM_ROOT_ENV] === dir) delete process.env[FAKE_PLATFORM_ROOT_ENV]
+}
+
+function fakeUserDataParent(): string {
+ const root = process.env[FAKE_PLATFORM_ROOT_ENV]
+ // Outside the vitest config (no globalSetup), fall back to the system temp dir as before.
+ return root && existsSync(root) ? root : os.tmpdir()
+}
+
export interface FakePlatform extends CorePlatform {
handlers: Record<string, (...args: any[]) => unknown>
listeners: Record<string, (...args: any[]) => void>
@@ -23,10 +50,19 @@ export interface FakePlatform extends CorePlatform {
* store, context-link, the token files — statically reads as a write to a PREDICTABLE temp path,
* which is a real symlink-attack shape and which CodeQL flags as `js/insecure-temporary-file`.
* Tests that want their own directory still pass one in; this only fixes what they inherit.
+ *
+ * The directory is made on first READ, not at construction, and under the run's own root
+ * (`FAKE_PLATFORM_ROOT_ENV`). Made eagerly in the system temp dir and never removed, it leaked one
+ * directory per call — including every call whose test passes its own `userDataDir` or never reads
+ * it — and a development server running the suite repeatedly collected ~395,000 of them until
+ * `/tmp` ran out of inodes and whole runs failed with ENOSPC.
*/
export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatform {
+ let userDataDir: string | undefined
const f: FakePlatform = {
- userDataDir: mkdtempSync(path.join(os.tmpdir(), 'nodeterm-fake-')),
+ get userDataDir(): string {
+ return (userDataDir ??= mkdtempSync(path.join(fakeUserDataParent(), 'nodeterm-fake-')))
+ },
appVersion: '0.0.0-test',
isPackaged: false,
handlers: {},
diff --git a/test/setup/fake-platform-root.ts b/test/setup/fake-platform-root.ts
new file mode 100644
index 000000000..36f28f12b
--- /dev/null
+++ b/test/setup/fake-platform-root.ts
@@ -0,0 +1,25 @@
+// TEST-ONLY. The run-scoped parent directory for `fakePlatform()`'s `userDataDir`s.
+//
+// `fakePlatform()` (src/core/platform-fake.ts) gives each call a fresh `mkdtemp` directory, and
+// nothing ever removed them: a development server running the suite over and over collected
+// ~395,000 `nodeterm-fake-*` directories until `/tmp` ran out of inodes and whole runs failed with
+// ENOSPC. Every one of them is now made under this directory, which lives exactly as long as the run.
+//
+// Per RUN, not per file, for the same reason as the tmux sandbox beside it: `setup` runs in the main
+// process, so the workers inherit the variable, and `teardown` runs after every worker is gone. A
+// per-file `afterAll` would sweep a directory while a store's debounced write could still be on its
+// way into it, and turn a leak into an ENOENT thrown from a timer. A run that is killed before its
+// teardown leaves ONE directory behind, not thousands.
+import { enterFakePlatformRoot, leaveFakePlatformRoot } from '../../src/core/platform-fake'
+
+let dir: string | null = null
+
+export async function setup(): Promise<void> {
+ dir = enterFakePlatformRoot()
+}
+
+export async function teardown(): Promise<void> {
+ if (!dir) return
+ leaveFakePlatformRoot(dir)
+ dir = null
+}
diff --git a/vitest.config.ts b/vitest.config.ts
index 2bb2415a9..41f698165 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -29,7 +29,9 @@ export default defineConfig({
// Issue #629: every run gets a private `TMUX_TMPDIR`, so no test can reach the tmux servers
// this machine runs nodeterm on. `globalSetup` creates and removes it, the setup file re-asserts
// it inside each worker (and refuses if it is missing). See test/setup/tmux-sandbox.ts.
- globalSetup: ['test/setup/tmux-sandbox.ts'],
+ // `fake-platform-root.ts` gives the run one parent directory for every `fakePlatform()`
+ // userDataDir and removes it at the end — before it, each call leaked a directory in /tmp.
+ globalSetup: ['test/setup/tmux-sandbox.ts', 'test/setup/fake-platform-root.ts'],
setupFiles: ['test/setup/tmux-worker-env.ts'],
// Issue #160: with the default (one worker per core), a 10-core Mac runs ~10 fs-heavy suites
// at once and transient fd exhaustion (EMFILE) turns into silent test flakiness — probes like