Skip to content

[Bug]: bare bun test prunes the real ~/.claude/agents/ocx-*.md (Bun homedir() ignores the preload's HOME sandbox) #6775

Closed

Description

@thisisjun786

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:

  1. tests/preload.ts sandboxes the run by reassigning process.env.HOME. On Bun 1.4.0, os.homedir() keeps returning the home the process started with and ignores a reassigned HOME. src/claude/auth-detect.ts:92-98 already documents this and works around it.
  2. claudeConfigDir() (src/claude/gateway-cache.ts:47-50) falls back to join(homedir(), ".claude") when CLAUDE_CONFIG_DIR is unset, and the preload does not set CLAUDE_CONFIG_DIR.
  3. tests/claude-integration/claude-messages-endpoint.test.ts:1618 ("compatibility survives management toggles and rejects Desktop source features") sends PUT /api/native-integrations/claude {"enabled":false} and PUT /api/claude-code {"injectAgents":false} to a startServer(0) server. Those routes call syncClaudeAgentDefsBestEffort() (src/server/management-api.ts:300-321), which runs injectClaudeAgentDefs(config, {}, deps.claudeAgentConfigDir) with claudeAgentConfigDir undefined. syncClaudeAgentDefs([]) then prunes every generated ocx-*.md in 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 pruned

In 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.0

Observed 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:

  1. Resolve the user home through one helper that reads HOME (USERPROFILE on Windows) at call time and falls back to os.homedir(). Route claudeConfigDir() and the other home-derived defaults through it, and add a source check that rejects direct homedir() calls in src/ outside the helper. The preload's existing HOME sandbox then covers bare runs. Production behaviour is unchanged, because a process's HOME is what homedir() returns at startup.
  2. Until then, have createIsolatedTestEnvironment() also set CLAUDE_CONFIG_DIR (and the other client-home overrides) to sandbox paths.
  3. 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.
  4. Have the management test pass deps.claudeAgentConfigDir explicitly.

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

  1. added
    bug
    cli
    on Oct 8, 2026
  2. lidge-jun commented on Oct 9, 2026

    @lidge-jun
    Owner

    Fixed on dev by #6835 (merge commit 7f642e3).

    A bare bun test no longer reaches the real Claude config directory:

    • The test sandbox (scripts/test.ts, used by both the wrapper and the preload) pins CLAUDE_CONFIG_DIR and GROK_HOME inside its root instead of inheriting them.
    • claudeConfigDir() reads HOME (USERPROFILE on 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.
    • The armed test-home guard refuses Claude agent sync, gateway-cache writes, served-catalog invalidation and intercept settings.json writes that resolve into the real Claude directory (default and your own CLAUDE_CONFIG_DIR), judged by where each path resolves, and the directory is a protected removal tree.
    • The management toggle test passes an explicit agents directory.

    Your fake-HOME probe now survives claude-messages-endpoint.test.ts. Other client homes that still default to os.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 Bash HOME reads its own config. Thanks for the precise root-cause report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcli

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions