Skip to content

test: run each package's tests in a temp dir that is removed afterwards - #98

Merged
avifenesh merged 1 commit into
mainfrom
test/clean-temp-dirs
Nov 4, 2026
Merged

avifenesh merged 1 commit into
mainfrom
test/clean-temp-dirs

Conversation

@avifenesh

@avifenesh avifenesh commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A full turbo run test leaves about 220 directories in the temp dir. The write, read, glob, grep, bash and lsp test helpers and the skill tests create a directory per test and never remove it. The tools package's ledger test leaves two more. On top of that, turbo's strict env mode does not pass TMPDIR to tasks, so the tests write to /tmp even when TMPDIR points somewhere else.

Fix

  • scripts/vitest-temp-dir.ts is a vitest global setup. It creates a fresh directory under the temp dir the run inherited (TMPDIR, or the OS default), points TMPDIR at it while the tests run, and removes it when the run ends. Workers see it through os.tmpdir(). No temp path is hardcoded.
  • Every package's vitest.config.ts lists it as globalSetup. core and tools had no vitest config and get one with only that setting, so their test discovery is unchanged.
  • turbo.json passes TMPDIR through to tasks (globalPassThroughEnv, which does not affect cache hashes) and lists the setup script in globalDependencies, so editing it invalidates the cache.

One setup per run, rather than cleanup in each helper, also catches what the code under test writes through os.tmpdir() (for example the bash runtime dirs) and what dependencies cache there (the ripgrep wasm file). Those land in the run dir and go with it. packages/lsp/test/spawnClient.test.ts is unchanged; it already removes its own dirs, and #95 changes it.

Proof

Full turbo run test --force without harness-e2e (as in CI), with TMPDIR set to a fresh empty directory, and /tmp listed before and after:

Entries in TMPDIR after the run New entries in /tmp
main 0 221: write-test 43, skill-test 38, read-test 36, glob-test 34, grep-test 28, bash-test 21, lsp-test 18, pi-ledger 2, the ripgrep wasm cache 1
this branch 0 0

On main the TMPDIR count is 0 only because turbo drops the variable and everything goes to /tmp. To show the variable now reaches the tests, a throwaway test in core asserted on os.tmpdir() under turbo run test. It reported <TMPDIR>/agent-sh-test-XXXXXX, and that directory was gone after the run.

Checks

pnpm test:dependencies, pnpm lint:harness and turbo run build typecheck test without harness-e2e pass, 39 of 39 tasks. No changeset: only test configuration changes, nothing published.


Note

Low Risk
Test and Turbo configuration only; no production or published package behavior changes.

Overview
Adds shared Vitest global setup (scripts/vitest-temp-dir.ts) that creates an agent-sh-test-* directory under the inherited temp root, sets TMPDIR for the run, and recursively deletes it on teardown so leaked test artifacts (helpers, os.tmpdir(), dependency caches) do not accumulate.

Every package vitest.config.ts wires in that setup; core and tools gain minimal configs for the same. turbo.json passes TMPDIR into test tasks via globalPassThroughEnv and treats the setup script as a globalDependency so cache invalidates when it changes.

Reviewed by Cursor Bugbot for commit bc23e07. Configure here.

A full test run left about 220 directories in the system temp dir:
write, skill, read, glob, grep, bash and lsp test helpers create one per
test and never remove it, and the tools package's ledger test leaves two.
Turbo's strict env mode also dropped TMPDIR, so the tests wrote to /tmp
even when TMPDIR pointed elsewhere.

Every package's vitest config now runs scripts/vitest-temp-dir.ts as a
global setup. It creates a fresh directory under the inherited temp dir,
points TMPDIR at it for the run, and removes it when the run ends. That
covers what the helpers create, what source code under test writes
through os.tmpdir(), and caches dependencies keep there. core and tools
get a vitest config for it. turbo.json passes TMPDIR through to tasks
and lists the setup script as a global dependency.
@cursor

cursor Bot commented Nov 4, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: eabecb49-b19a-4d40-9c72-3556f8139c4d)

@revuto-review revuto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an auto review done by revuto.


Revuto completed the review and found no evidence-backed concerns.

@avifenesh

Copy link
Copy Markdown
Collaborator Author

Verdict: ready to land. The counts reproduce exactly, the setup only ever removes its own run dir, and nothing blocking came up.

Non-blocking

  1. An interrupted run leaves its run dir. I sent SIGINT, SIGTERM and SIGKILL to vitest run in packages/lsp 5 s in. Each left one agent-sh-test-* dir under the inherited TMPDIR: vitest does not run the global teardown on a signal. Before this PR the same interrupted run left its per-test dirs loose in the temp dir, so this is better, not worse. The header comment in scripts/vitest-temp-dir.ts:1-8 could say so.
  2. Unrelated, found while checking test order. packages/webfetch/test/webfetch.test.ts:22-30 keeps handler at module level and never resets it. So "allows public IP" (:167) fails whenever a test that installs a 404 handler runs before it. --sequence.shuffle --sequence.seed=11 fails it on main and on this branch alike. It is not a file dependence and this PR does not cause it. A reset in beforeEach would fix it in a follow-up.

Checked and fine

  • Counts. Full turbo run test --force --filter='!@agent-sh/harness-e2e', TMPDIR set to a fresh dir under my cache, my own temp-dir entries listed before and after.

    New entries in /tmp Left in TMPDIR
    main (e68a4ab) 221: write-test 43, skill-test 38, read-test 36, glob-test 34, grep-test 28, bash-test 21, lsp-test 18, pi-ledger 2, ripgrep wasm cache 1 0
    this PR (bc23e07) 0 0

    One more new entry on main was a lock file from an unrelated process, so I left it out. Both runs report the same test files and test counts in all 13 packages, so the new core and tools configs do not change discovery.

  • TMPDIR reaches the tests under turbo. A throwaway test in core recorded os.tmpdir() under turbo run test. On this PR it was <TMPDIR>/agent-sh-test-XXXXXX. On main it was /tmp, with TMPDIR unset in the test.

  • The setup deletes only its own dir. Teardown removes the path mkdtempSync returned, captured at setup, not read back from the environment. Probe in core with two test files in two workers: both saw the same run dir. One test planted a symlink from the run dir to a dir outside it. It then pointed TMPDIR at a second outside dir and wrote a file there. After the run the run dir was gone, the symlink target and its file were intact, and the test's own dir was untouched. Each package gets its own mkdtemp name, so parallel turbo tasks never share or remove each other's dirs.

  • TMPDIR unset. core and write with env -u TMPDIR: the tests saw /tmp/agent-sh-test-XXXXXX, and nothing was left in /tmp. On main, write alone leaves 43. CI on ubuntu-latest takes the same path, and its TypeScript job passes on bc23e07.

  • No hardcoded temp path. The setup uses os.tmpdir(). The remaining "/tmp" strings in tests are schema inputs and mocks, and none of them writes there.

  • No test needs a file another test left behind. Every run now starts from an empty dir, and the full run passes. I also ran write, skill, read, glob, grep, bash, lsp, tools, batch and webfetch with --sequence.shuffle, seeds 11 and 4242. All pass except the webfetch case above, which fails on main too.

  • harness-e2e. lsp-transport, vcr and vcr-security pass with the setup and leave nothing behind.

  • No changeset. Only test config changes, and none of it is in any package's files.

What I ran

  • The two counted test runs above, on main and on this PR.
  • turbo run build typecheck --force --filter='!@agent-sh/harness-e2e' on this PR: 26 of 26 tasks. The test run: 24 of 24.
  • pnpm test:dependencies and pnpm lint:harness pass.
  • CI: TypeScript, Rust and CodeQL pass on bc23e07. revuto approved bc23e07. Bugbot did not run (usage limit).

@avifenesh
avifenesh merged commit bc23e07 into main Nov 4, 2026
8 checks passed
@avifenesh
avifenesh deleted the test/clean-temp-dirs branch November 4, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant