Repository navigation
Description
Client or integration
Claude Code
Area
Tests (home isolation), management API, Claude agent definition sync
Summary
A bare bun test <file> run, which bunfig.toml describes as sandboxed by tests/preload.ts, deletes the developer's real ~/.claude/agents/ocx-*.md. Running Claude Code sessions then report every ocx-* subagent type as unavailable until the next ocx claude launch or proxy restart re-syncs the files.
Expected: a bare bun test <file> never writes to or deletes from the real home.
Cause:
tests/preload.tssandboxes the run by reassigningprocess.env.HOME. On Bun 1.4.0,os.homedir()keeps returning the home the process started with and ignores a reassignedHOME.src/claude/auth-detect.ts:92-98already documents this and works around it.claudeConfigDir()(src/claude/gateway-cache.ts:47-50) falls back tojoin(homedir(), ".claude")whenCLAUDE_CONFIG_DIRis unset, and the preload does not setCLAUDE_CONFIG_DIR.tests/claude-integration/claude-messages-endpoint.test.ts:1618("compatibility survives management toggles and rejects Desktop source features") sendsPUT /api/native-integrations/claude {"enabled":false}andPUT /api/claude-code {"injectAgents":false}to astartServer(0)server. Those routes callsyncClaudeAgentDefsBestEffort()(src/server/management-api.ts:300-321), which runsinjectClaudeAgentDefs(config, {}, deps.claudeAgentConfigDir)withclaudeAgentConfigDirundefined.syncClaudeAgentDefs([])then prunes every generatedocx-*.mdin the real~/.claude/agents.
bun run test and bun run test:changed are not affected: scripts/test.ts spawns its children with HOME already set to the sandbox, so homedir() is correct there. Only bare runs leak.
The test home guard (src/lib/test-home-guard.ts) protects only the real OpenCodex home, so nothing fails closed here. src/ has 150 homedir() calls in 49 files. Any of them that writes during a bare run escapes the sandbox the same way; the Claude agents directory is the one observed.
Reproduction
Safe reproduction. HOME is set at process start, so homedir() returns the fake home and nothing real is touched:
FH=$(mktemp -d); mkdir -p "$FH/.claude/agents"
printf -- '---\nname: "ocx-probe"\nmodel: "x"\n---\n\n<!-- generated-by: opencodex -->\n' > "$FH/.claude/agents/ocx-probe.md"
env -u CLAUDE_CONFIG_DIR HOME="$FH" bun test tests/claude-integration/claude-messages-endpoint.test.ts
ls "$FH/.claude/agents" # empty: ocx-probe.md was prunedIn a normal bare run homedir() is the real home, so the real files are pruned instead. With the same probe, claude-native-passthrough.test.ts, messages-native.test.ts, claude-intercept-client-class.test.ts and tests/server/server-auth.test.ts leave the file in place.
Bun behaviour:
import { homedir } from "node:os";
console.log(homedir()); // /home/user
process.env.HOME = "/tmp/sandbox";
console.log(homedir()); // still /home/user on Bun 1.4.0Observed on a real machine: all six ~/.claude/agents/ocx-*.md files disappeared during a bare run of the file above, and a running Claude Code session then reported every ocx-* agent type unavailable. ocx claude --version restored them.
Suggested fix, root cause first:
- Resolve the user home through one helper that reads
HOME(USERPROFILEon Windows) at call time and falls back toos.homedir(). RouteclaudeConfigDir()and the other home-derived defaults through it, and add a source check that rejects directhomedir()calls insrc/outside the helper. The preload's existingHOMEsandbox then covers bare runs. Production behaviour is unchanged, because a process'sHOMEis whathomedir()returns at startup. - Until then, have
createIsolatedTestEnvironment()also setCLAUDE_CONFIG_DIR(and the other client-home overrides) to sandbox paths. - Extend the test home guard's deny-list to the real Claude config directory for destructive writers such as
syncClaudeAgentDefs, so a missed call site fails closed. - Have the management test pass
deps.claudeAgentConfigDirexplicitly.
Version
dev e6c6e13a2 (opens 2.82.0). The installed 2.80.0 has the same syncClaudeAgentDefsBestEffort path. Bun 1.4.0.
Operating system
Ubuntu Linux (kernel 7.0)
Checks
- I searched existing issues and documentation.
Activity
fix(test): keep test processes out of the real Claude config directory (
lidge-jun commentedon Oct 9, 2026
Fixed on
devby #6835 (merge commit 7f642e3).A bare
bun testno longer reaches the real Claude config directory:scripts/test.ts, used by both the wrapper and the preload) pinsCLAUDE_CONFIG_DIRandGROK_HOMEinside its root instead of inheriting them.claudeConfigDir()readsHOME(USERPROFILEon Windows) at call time, as Claude Code's Node runtime does, so a post-start HOME rewrite is followed. Production resolves the same path as before.settings.jsonwrites that resolve into the real Claude directory (default and your ownCLAUDE_CONFIG_DIR), judged by where each path resolves, and the directory is a protected removal tree.Your fake-HOME probe now survives
claude-messages-endpoint.test.ts. Other client homes that still default toos.homedir()(Claude Desktop config library, several integration writers, Kiro, XDG clients) were left as follow-ups, because moving their production resolution would change where a Windows host with a Git BashHOMEreads its own config. Thanks for the precise root-cause report.docs(devlog): close the N3 test-infra unit (#6840)