Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(setup): unset claudish session env vars in a bun test preload

Jack Rudenkoa1c71ad5d55c
diff --git a/bunfig.toml b/bunfig.toml
new file mode 100644
index 00000000..68dbb77c
--- /dev/null
+++ b/bunfig.toml
@@ -0,0 +1,5 @@
+# The preload removes the claudish session-identity variables before any test file loads
+# (scripts/test-preload.ts). bun reads bunfig.toml from the working directory only, so
+# packages/cli and packages/macos-bridge carry their own copy of this entry.
+[test]
+preload = ["./scripts/test-preload.ts"]
diff --git a/packages/cli/bunfig.toml b/packages/cli/bunfig.toml
new file mode 100644
index 00000000..87a49120
--- /dev/null
+++ b/packages/cli/bunfig.toml
@@ -0,0 +1,3 @@
+# See the repo-root bunfig.toml: bun does not walk up for it, so this directory needs its own.
+[test]
+preload = ["../../scripts/test-preload.ts"]
diff --git a/packages/macos-bridge/bunfig.toml b/packages/macos-bridge/bunfig.toml
new file mode 100644
index 00000000..87a49120
--- /dev/null
+++ b/packages/macos-bridge/bunfig.toml
@@ -0,0 +1,3 @@
+# See the repo-root bunfig.toml: bun does not walk up for it, so this directory needs its own.
+[test]
+preload = ["../../scripts/test-preload.ts"]
diff --git a/scripts/session-env-vars.ts b/scripts/session-env-vars.ts
new file mode 100644
index 00000000..b1fbbd84
--- /dev/null
+++ b/scripts/session-env-vars.ts
@@ -0,0 +1,34 @@
+/**
+ * The environment variables that point a process at ONE claudish session's records.
+ *
+ * A pane child (an MCP `create_session` slot, a `team` slot) inherits them, and so does
+ * a test run started inside that pane. Inherited, they make the suite write its test
+ * doubles into the real session: `captureUpstreamError` appends to
+ * `~/.claudish/sessions/<id>/upstream-errors.jsonl` whenever the first one is set, and
+ * `get_diagnostics` then lists `stub` and `fixture-upstream-error` as that session's
+ * upstream failures (model-failures-2026-10-21.md, D5).
+ *
+ * Each name is the literal of a production constant (the writer is in the comment).
+ * They are literals, with no imports, on purpose: this file is loaded by the bun test
+ * preload before every test file, and importing the production modules would load
+ * `hooks/`, `config.ts` and the token tracker ahead of the module-scope snapshots tests
+ * rely on. Importing this file has NO side effect; `test-preload.ts` does the deleting.
+ *
+ * Read by `scripts/test-preload.ts` (in-process, first layer) and
+ * `scripts/guard-real-config.ts` (the child's OS environment, second layer).
+ */
+export const SESSION_ENV_VARS = [
+ // UPSTREAM_ERROR_LOG_ENV, handlers/shared/upstream-error-capture.ts → upstream-errors.jsonl
+ "CLAUDISH_UPSTREAM_ERROR_LOG",
+ // HOOK_INBOX_ENV, hooks/injection/sinks.ts → the session's hook inbox
+ "CLAUDISH_HOOK_INBOX",
+ // HOOK_SESSION_ENV, hooks/request-stage.ts → the identity hook records are attributed to
+ "CLAUDISH_HOOK_SESSION",
+ // ENV.CLAUDISH_TOKEN_FILE, config.ts → sessions/<id>/tokens.json (TokenTracker.writeFile)
+ "CLAUDISH_TOKEN_FILE",
+ // ENV.CLAUDISH_PUBLISHED_TOKEN_FILE, config.ts → the marker resolveTokenFilePath compares
+ // with the one above; clearing only one of the pair writes the session's tokens.json
+ "CLAUDISH_PUBLISHED_TOKEN_FILE",
+ // read by isPaneChild (pane/child-env.ts): gates the inbox sink and the nested refusal
+ "CLAUDISH_PANE_CHILD",
+] as const;
diff --git a/scripts/test-preload.ts b/scripts/test-preload.ts
new file mode 100644
index 00000000..4a173ee2
--- /dev/null
+++ b/scripts/test-preload.ts
@@ -0,0 +1,24 @@
+/**
+ * bun test preload: runs once per `bun test` process, before the first test file loads.
+ *
+ * It removes the variables that point a process at a real claudish session
+ * (`SESSION_ENV_VARS`), so a suite run inside a claudish pane cannot append its test
+ * doubles to that session's records (D5). Registered by three bunfig.toml files: the
+ * repo root, packages/cli and packages/macos-bridge, because bun reads bunfig.toml
+ * from the working directory only and never walks up (measured on 1.3.10 and 1.4.0).
+ *
+ * What this covers, measured: in-process writers, and children spawned with an
+ * explicit `env: { ...process.env }`. What it does NOT cover: a child spawned with no
+ * `env` option. `Bun.spawn`/`Bun.spawnSync` (1.3.10 and 1.4.0) and `node:child_process`
+ * (1.3.10, the CI pin) hand such a child the ORIGINAL environment, deletions undone.
+ * `scripts/spawn-env.test.ts` requires the `env` option in test code for that reason,
+ * and `scripts/guard-real-config.ts` deletes the same names from the OS environment
+ * of a `test:safe` run.
+ *
+ * Keep this file import-light: it must not load production code (see session-env-vars.ts).
+ */
+import { SESSION_ENV_VARS } from "./session-env-vars.ts";
+
+for (const name of SESSION_ENV_VARS) {
+ delete process.env[name];
+}