Skip to content

fix(test): keep test processes out of the real Claude config directory - #6835

Merged
lidge-jun merged 5 commits into
devfrom
codex/n3-test-infra
Oct 9, 2026
Merged

lidge-jun merged 5 commits into
devfrom
codex/n3-test-infra

Conversation

@lidge-jun

Copy link
Copy Markdown
Owner

Summary

Fixes #6775. A bare bun test <file>, which bunfig.toml describes as sandboxed by tests/preload.ts, deleted the developer's real ~/.claude/agents/ocx-*.md.

The preload rewrites HOME after Bun has started, and Bun's os.homedir() keeps the home it read at startup. claudeConfigDir() fell back to homedir() when CLAUDE_CONFIG_DIR was unset, so it named the real directory, and the management toggles in claude-messages-endpoint.test.ts ran Claude agent sync there with an empty roster, pruning every generated definition. bun run test was not affected for unset defaults because its children start with HOME already sandboxed, but it passed an inherited CLAUDE_CONFIG_DIR straight through.

The fix is layered so one gap does not reopen the hole:

  • Sandbox (scripts/test.ts, shared by the wrapper and the preload): pins CLAUDE_CONFIG_DIR and GROK_HOME inside the sandbox root, never inheriting a live override, and hands the developer's own Claude directory to the guard as OCX_REAL_CLAUDE_CONFIG_DIR.
  • Resolver (src/claude/gateway-cache.ts): claudeConfigDir() reads HOME (USERPROFILE on Windows) at call time, which is what Claude Code's Node runtime does. In production the process environment is the startup environment, so the resolved path is unchanged; only a post-start rewrite now follows.
  • Guard (src/lib/test-home-guard.ts): assertNotRealClaudeConfigUnderTest refuses, in an armed test process only, any write that resolves into the real Claude directory — default and developer override. Writers pass every path they touch, so a link at agents/, cache/, cache/gateway-models.json or a definition's temp file is judged by its target. Roots resolve at check time and compare case-insensitively on macOS and Windows; containment rejects only a leading .. component. The Claude directory is also a protected removal tree, with the same checkout-content lift the other trees have.
  • Writers guarded, each outside its best-effort catch: Claude agent sync, the gateway-model cache, served-catalog invalidation, and the intercept settings.json writer.
  • The toggle test passes an explicit agents directory, and the existing resolver test now expects the current home variable instead of the cached one.

The guard is inert unless OCX_TEST_HOME_GUARD=1, which only the test preload sets.

Other client homes still default to os.homedir() (Claude Desktop config library, several client-integration writers, Kiro, XDG-based clients). Moving production resolution for those would change where a Windows host with a Git Bash HOME reads its own config, so this PR leaves them; the sandbox and guard cover the observed path. They are follow-up candidates.

Verification

Every local run started Bun with a fresh temporary HOME, USERPROFILE and CLAUDE_CONFIG_DIR; the real ~/.claude/agents listing hash was identical before and after.

  • The issue's probe: a generated ocx-probe.md planted in a fake home with CLAUDE_CONFIG_DIR unset is pruned on dev by claude-messages-endpoint.test.ts; with this change it survives (54/54 pass).
  • New tests/claude-integration/claude-config-home-isolation.test.ts (8 cases): all fail on dev; the guard case covers direct writes, linked agents/cache, a linked cache file, a case alias of an override that does not exist yet, ..fixture and POSIX ..\\fixture children, catalog and settings writers, removal refusal, and an unrefused write to an ordinary fixture.
  • Focused set (13 files incl. test-home-guard, agents-inject, intercept settings, CLI picker, first-party agent sync, catalog prewarm, gateway cache, layout, structure, file-size ratchet): 249 pass, 0 fail. bun run typecheck, bun run structure:check, bun run privacy:scan: pass.
  • Structure: structure/ops/test-sandbox-cleanup.md documents the pinned client homes and the Claude guard. Other docs mapped for scripts/, src/claude/, src/lib/, tests/ were reviewed and describe nothing this changes.
  • Independent review and a separate security review (filesystem-deletion boundary) went three repair rounds; both PASS at this head. Native Windows was not executed locally.
  • The full suite was not run locally; it runs in hosted CI on this head.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. (structure/ updated; no user-facing change.)
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults. (Deletion-boundary security review PASS; guard inert in production.)

#6775)

A bare `bun test` pruned the developer's real ~/.claude/agents/ocx-*.md. The
preload rewrites HOME after Bun has started, Bun's os.homedir() keeps the
start-time home, so claudeConfigDir() resolved the real directory and the
management toggles' agent sync deleted the generated files there.

- scripts/test.ts pins CLAUDE_CONFIG_DIR and GROK_HOME inside the sandbox and
  hands the developer's own Claude directory to the guard.
- claudeConfigDir() reads HOME (USERPROFILE on Windows) at call time, as
  Claude Code's Node runtime does; production resolves the same path.
- The armed test-home guard refuses Claude agent sync and gateway-cache writes
  into the real Claude config directory and protects it from removal.
- The toggle test passes an explicit agents directory.
…cept writers (#6775)

Review found three ways past the first guard: an agents/cache child linked into
the real directory, a case alias of a protected root that did not exist when
the guard loaded, and the intercept settings.json and served-catalog writers,
which never called it. Writers now pass every directory they touch, roots are
resolved at check time and compared case-insensitively on macOS and Windows,
and both intercept writers call the guard outside their best-effort catches.
…ty (#6775)

Second review round: writeFileSync followed a link at gateway-models.json, a
child named "..fixture" was read as outside the root, and removal protection
still compared the import-time Claude roots case-sensitively. The cache file and
each agent definition path are now judged where they resolve, containment
rejects only a leading ".." component, and protectedRemovalReason checks the
Claude directory with the same current-root, case-folding comparison.
…p checkout content removable (#6775)

Third review round: splitting relative() on both separators read a POSIX child
named "..\fixture" as outside the root, and the new Claude removal check
dropped the checkout-content lift the other protected trees keep. Containment
now rejects only ".." or "..<sep>", and content of a checkout that lives inside
the Claude directory stays removable while the root and its ancestors do not.
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner October 9, 2026 11:54
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T11:58:04.174813Z 9ac1a95 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 23 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: lidge-jun/opencodex/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 9cc403aa-6461-4bfc-b04e-2c1486cf115d

📥 Commits

Reviewing files that changed from the base of the PR and between 37e9294 and 9ac1a95.


📒 Files selected for processing (16)
  • devlog/_plan/261009_n3_test_infra/000_plan.md
  • devlog/_plan/261009_n3_test_infra/010_6775_client_home_isolation.md
  • devlog/_plan/261009_n3_test_infra/020_6777_combo_fixture.md
  • devlog/_plan/261009_n3_test_infra/030_6776_toggle_ownership_fixture.md
  • scripts/test-layout/layout.json
  • scripts/test.ts
  • src/claude/agents-inject.ts
  • src/claude/gateway-cache.ts
  • src/claude/intercept/cli-catalog.ts
  • src/claude/intercept/settings.ts
  • src/lib/test-home-guard.ts
  • structure/ops/test-sandbox-cleanup.md
  • tests/claude-integration/claude-config-home-isolation.test.ts
  • tests/claude-integration/claude-gateway-cache.test.ts
  • tests/claude-integration/claude-messages-endpoint.test.ts
  • tests/fixtures/test-layout-expected.json


  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the bug Something isn't working label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@lidge-jun
lidge-jun merged commit 7f642e3 into dev Oct 9, 2026
39 checks passed
@lidge-jun
lidge-jun deleted the codex/n3-test-infra branch October 9, 2026 12:59
lidge-jun added a commit that referenced this pull request Oct 9, 2026
* docs(devlog): record the #6777 fixture implementation

* docs(devlog): record the #6776 fixture implementation

* docs(devlog): close the N3 test-infra unit (#6775, #6777, #6776)

Moves devlog/_plan/261009_n3_test_infra to devlog/_fin with an Outcome section recording #6835 (7f642e3), #6833 (6d7e5f4) and #6834 (c4baa30), their exact-head CI, the union check, and the scope left as follow-ups.

* docs(devlog): say which N3 CI jobs were skipped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant