Skip to content

fix(runtime): keep Codex sandbox from creating Git metadata files - #232

Merged
datacas merged 2 commits into
mainfrom
task-229
Oct 14, 2026
Merged

datacas merged 2 commits into
mainfrom
task-229

Conversation

@datacas

@datacas datacas commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Closes #229

Work item: datacas/code-cycle-toolkit#229 (GitHub Issues)

Summary

Codex writing publish dispatches no longer leave empty files in .git. The publish profile now keeps the common Git directory read-only, grants writes only to objects, refs, logs, and the worktree's private directory, and names only paths that already exist. Before dispatch, a stage routes away from Codex when a dangerous path is absent. A stopped dispatch's process group now gets SIGTERM and a bounded grace period before SIGKILL.

Diagnosis

  • Symptom. After a Codex dispatch from a linked worktree, zero-byte files appeared in the common .git: commondir, state markers, lock files, and refs//logs/refs/{bisect,worktree,rewritten}. Git then failed with failed to read .git/commondir.
  • Hypothesis in the issue. Filesystem rules for absent paths turn into synthetic mount destinations. Confirmed.
  • Reproduction.
    • This repository's own .git still had the incident's leftovers: empty logs/HEAD.lock and logs/refs/{bisect,rewritten,worktree}, dated 2026-10-13 10:21. The routing step that started this run hit the same leftovers: "Codex linked-worktree Git metadata probe failed: protected Git metadata directory has an unexpected file: bisect".
    • In a fresh linked worktree, the old _publish_filesystem_rules() emits 70 rules, and 55 of them name absent paths. The incident reported 54 files. The new profile emits 9 rules and names 0 absent paths.
    • test_the_emulator_reproduces_the_incident uses a fake codex that creates missing rule destinations, as the Linux sandbox does. It reproduces the broken git status.
    • The real Codex CLI could not be run here: invoking codex needs an approval this stage does not have.
  • Cause and invariant. Codex's Linux sandbox mounts every rule on its destination and creates the destination when it is missing. The invariant: no filesystem rule may name a path that does not exist when the profile is built. The old profile broke it in two ways. It needed a writable common root, because packed-refs cannot be granted as a file path. So it carved out every protected name as an individual rule, whether or not the path existed.
  • Fix shape. Restore the invariant where the rules are built. It follows the decisions recorded on the issue:
    • The common root is read-only. Absent protected files are then denied without being named.
    • Write grants go only to objects/refs/logs and the private worktree directory. These are also the only --add-dir roots now.
    • Read-only carve-outs are emitted only for paths that exist: the private commondir/gitdir, config.worktree and its lock, the workspace .git, and the main worktree's logs/HEAD and refs/logs/refs bisect/worktree/rewritten.
    • The stage routes away (decisions 5 and 6) when:
      • extensions.worktreeConfig is on and config.worktree or its lock is missing;
      • the effective core.hooksPath (local, worktree-specific, or relative) resolves inside a writable Git directory;
      • the stage would publish from the main worktree.
    • gc.auto=0 and maintenance.auto=false are set through GIT_CONFIG_PARAMETERS.
    • Staged termination covers dispatches and the probe (decision 1).
    • Nothing sweeps .git after the dispatch.
  • Related work. Basic search: the item's links fix(worktree): isolate sandbox tests and Git metadata #196 and fix(runtime): Codex cannot write Git metadata of a linked worktree, so implementation stops at publication #203, plus a search for "sandbox git metadata". fix(runtime): Codex cannot write Git metadata of a linked worktree, so implementation stops at publication #203 (open) shares the linked-worktree publishing path. This change keeps its behaviour: commit, fetch, push, and loose ref updates are tested under a read-only root. It does not resolve fix(runtime): Codex cannot write Git metadata of a linked worktree, so implementation stops at publication #203. No duplicate was found.
  • Classification. isolated_defect, decision implement.

Work units

Unit Behaviour Files Tests Verification Rollback
WU-1 Read-only common root, rules only for existing paths, route-away for dangerous absent paths, probe that never names absent paths and cleans its markers on timeout and interrupt, Git maintenance disabled in the sandbox scripts/executors.py, tests/test_codex_git_sandbox.py, tests/test_executors.py, tests/fakes/fake_agent.py, docs/verification.md, CHANGELOG.md test_codex_git_sandbox (Profile, RouteAway, ProbeMarker, SandboxSideEffect, PublishingUnderReadOnlyRoot), updated test_executors, test_installed_cycle python3 -m unittest discover -s tests: 1387 tests OK; python3 scripts/validate-package.py: VALID independent
WU-2 Process group gets SIGTERM, then a bounded grace (10 s), then SIGKILL; bounded output drain scripts/executors.py, tests/test_codex_git_sandbox.py, docs/routing.md, CHANGELOG.md StagedTerminationTests (teardown runs on timeout; TERM-ignoring group killed after grace; finished dispatch leaves no descendants) python3 -m unittest discover -s tests: 1390 tests OK; validator VALID independent

Verification (agent-reported)

Level Command Exit Executed
static python3 scripts/validate-package.py 0 —
test python3 -m unittest discover -s tests (final head) 0 1390
test python3 -m unittest test_codex_git_sandbox 0 23
boundary old vs new _publish_filesystem_rules() in a real linked worktree: rules naming absent paths 0 55 → 0

The tests check the repository itself, before, during, and after both the probe and the dispatch, rather than only the serialized rules. In the side-effect tests, .git is identical at all three points, for absent and for existing protected paths. A real-Git test makes the common root and worktrees read-only with directory permissions. Under that restriction, commit, update-ref, fetch, and push succeed, the main worktree's HEAD, index, logs/HEAD, and refs/heads/main are unchanged, and pack-refs fails, which is the documented limitation.

Environment assumptions. Git-using tests set their own identity and GIT_CONFIG_GLOBAL=/dev/null / GIT_CONFIG_NOSYSTEM=1, so they do not depend on ~/.gitconfig. The read-only emulation and signal tests are skipped on Windows and when running as root. Local run: Linux (WSL2), Python 3.10, Git 2.34.1.

Residual risk

  • Not verified against the real Codex CLI. The fake reproduces only the mount-materialisation behaviour seen in the incident. The manual Linux probe is described in docs/verification.md.
  • Codex's own carve-outs. Codex creates .git, .codex, .agents, and .aws below every writable root, and they were observed as empty directories in objects, refs, and logs. These come from the CLI, not from these rules. Git ignores the dot-prefixed names, but they are still artifacts. Avoiding them needs a Codex-side setting, so a follow-up may be worth opening.
  • Main-worktree publishing. It now routes away from Codex. The cycle grants Git metadata only to linked worktrees, so this affects direct calls only.
  • GIT_CONFIG_PARAMETERS. It reaches the agent's commands only if Codex's shell_environment_policy inherits it, which is the default.
  • Leftover files. Files already left by earlier dispatches, such as this repository's logs/refs/*, are not removed. They no longer block the probe, which now protects them as existing files.

Checks of the pushed head

Head d81f290af0d8ba1673c0204525f0111601175575: 4 passed, 0 failed, 0 pending.

Check Event State
Skills, manifests, and Bash installer push pass
Skills, manifests, and Bash installer pull_request pass
PowerShell installer push pass
PowerShell installer pull_request pass

run: stage=implement profile=deep_coder requested_model=anthropic/claude-opus-5-5 resolved_model=? effort=high executor=claude schema:2

Codex's Linux sandbox creates every missing filesystem-rule destination
as an empty mount point, which outlived the dispatch and left files such
as commondir and MERGE_HEAD in .git (#229).

The publish profile now keeps the common Git directory read-only, grants
writes only to objects, refs, logs and the worktree's private directory,
and names only paths that exist. Dangerous absent paths (worktree config
under extensions.worktreeConfig, a core.hooksPath inside a writable Git
directory, main-worktree publishing) route the stage away before
dispatch. Automatic Git maintenance is disabled inside the sandbox.
A timed-out, interrupted or finished dispatch's process group now gets
SIGTERM and a bounded grace period, so a sandbox can run its own
teardown, before SIGKILL. Output pipes held by a stray descendant are
waited on for a bounded time, so cleanup never blocks the cycle (#229).
@datacas

datacas commented Oct 14, 2026

Copy link
Copy Markdown
Owner Author

Initial review — PR #232 (work item #229)

[CCR-20261014-001] · senior_reviewer · anthropic/claude-opus-5-5→? · high · schema:2

Verdict: APPROVED for head d81f290af0d8ba1673c0204525f0111601175575. There are no blocking findings, and all 4 checks passed. Two low, non-blocking findings are below.

Summary and scope

I reviewed the whole accumulated diff origin/main...d81f290, from merge base 42fa7bc7c6282d36ab6e4fb76f77b71bfe78ce0e. It is 1900 lines, SHA-256 42b5bb2d…41ce85, and covers 7 files: CHANGELOG.md, docs/routing.md, docs/verification.md, scripts/executors.py, tests/fakes/fake_agent.py, tests/test_codex_git_sandbox.py (new), and tests/test_executors.py. I read all of it, plus the surrounding code in scripts/executors.py (_reap_process_group, probe_git_metadata_write, cleanup) and the probe call site in scripts/cycle.py.

The change does what it says. The Codex publish profile now keeps the common Git root read-only and grants writes only to objects, refs, logs, and the private worktree directory. Every rule is filtered by path.exists(). --add-dir no longer includes the common root. Three conditions now route the stage away from Codex before dispatch: extensions.worktreeConfig without its files, a core.hooksPath inside a writable Git directory, and publishing from the main worktree. Process groups now get SIGTERM, then a grace period, then SIGKILL. The new invariant (no rule names an absent path) holds in the code and is pinned by tests that snapshot .git before, during, and after the probe and the dispatch.

Conventions: I applied AGENTS.md and .code-cycle.yml. .code-cycle.yml has no security_review.always_when rule, so I used the skill's default triage list.

Findings

[REV-001] · low · open · - · blocks:no — Output drain can wait twice OUTPUT_DRAIN_SECONDS, contrary to the docs

scripts/executors.py:482-486, docs/routing.md:116

join_output() joins the stdout thread and then the stderr thread, each with OUTPUT_DRAIN_SECONDS. A stray descendant that holds both pipes therefore delays the return by 2 × 5 s. docs/routing.md says these pipes "are waited on for at most OUTPUT_DRAIN_SECONDS". I observed it by running ex._run(["sh","-c","setsid sleep 20 & echo hi"], timeout=10), which returned rc 0 after 10.0 s, not 5 s. The bound still holds, so nothing hangs. The fix is either a shared deadline for both joins or a doc wording of "per pipe".

[REV-002] · low · open · - · blocks:no — No test still asserts that a failed probe's detail keeps the sandbox's stderr

tests/test_codex_git_sandbox.py:377, tests/test_executors.py

The PR removed test_git_metadata_probe_preserves_sandbox_error_when_markers_are_absent. The probe-failure tests that remain (test_probe_keeps_a_marker_it_does_not_own and test_failing_probe_leaves_git_metadata_unchanged) assert only "sandbox probe exited N", never the stderr text. That text is what the cycle records as the route-away reason. The behavior itself still works: I ran a fake codex that writes sandbox: mount failed to stderr and exits 3 through the real _run_sandbox_probe path, and it returned "Codex Git metadata sandbox probe exited 3: sandbox: mount failed". Only the regression guard is missing. Adding assertIn("simulated failure", detail) to the existing test would restore it.

Security triage and audit

The rule half did not fire: .code-cycle.yml declares no security_review.always_when, the PR has no labels, and the default path list does not match. The reviewer half fired: the change modifies a sandbox filesystem permission boundary and the environment passed to dispatched agents, which counts as security tooling and environment configuration. I ran the audit inline as part of this review (degraded delegation: no separate cc-security-review invocation).

Result: no confirmed vulnerabilities.

  • The boundary is now tighter than on main. config, hooks, packed-refs, commondir, gitdir, modules, worktrees/*, and the main worktree's root state are no longer writable at all, instead of being protected by enumerated carve-outs.
  • In the writable private worktree directory, Git reads no configuration or hooks except config.worktree. That file is read-only when it exists, and the stage routes away when extensions.worktreeConfig is on and the file is absent. The agent cannot turn the extension on, because the common config is read-only.
  • The core.hooksPath route-away resolves relative paths and symlinks. I checked this by execution: a hooks path that is a symlink into .git/refs was detected ("resolves inside the writable Git directory refs").
  • GIT_CONFIG_PARAMETERS is appended to, not replaced. Otherwise the environment is inherited exactly as before.
  • Potential risks, all pre-existing and documented, none introduced here: writable refs allow any local branch ref to be moved; objects/info/alternates is writable; a hooks path inside the writable source workspace is outside these rules; an include.path that points into a writable Git directory is not checked.

Executed verification

Command / flow Result
wc -l + sha256sum on the evidence diff 1900 lines, hash matches; PR head equals d81f290…
python3 -m unittest discover -s tests exit 0, 1390 tests OK (120 s)
python3 scripts/validate-package.py exit 0, VALID: 15 skills …
Real Git: empty .git/.codex/.agents/.aws dirs in objects, refs, logs, worktrees/w, then fsck, for-each-ref, count-objects -v, reflog expire --all, pack-refs --all, gc, worktree list, status in the worktree all exit 0, no warnings, garbage: 0. Confirms the docs' claim that Git ignores Codex's own carve-outs
_run with a setsid descendant holding both pipes returns rc 0 after 10.0 s (REV-001)
Probe through the real runner with a failing fake codex stderr kept in the detail (REV-002 is coverage only)
unprotectable_git_paths with core.hooksPath as a symlink into refs routes away
git status --porcelain before and after the suite clean

Unverified

  • Real Codex CLI behavior (whether a nested write under a read root takes effect, mount materialisation, and whether shell_environment_policy passes GIT_CONFIG_PARAMETERS through). No Codex CLI was run in this stage. This fails closed: if Codex does not honor the nested grants, the probe's write markers fail and the stage routes away before dispatch.

CI (policy: strict, the default)

Head d81f290af0d8ba1673c0204525f0111601175575: 4 passed, 0 failed, 0 pending.

Check State
Skills, manifests, and Bash installer (push) pass
Skills, manifests, and Bash installer (pull_request) pass
PowerShell installer (push) pass
PowerShell installer (pull_request) pass

No check that the repository normally runs is missing.

Residual risks

  • The change has not been run against a live Codex CLI (see Unverified). The manual check in docs/verification.md is the remaining evidence.
  • Inside the sandbox, packed-ref deletion (including fetch --prune and push --delete of packed remote-tracking refs), pack-refs, gc, shallow fetch, and rerere (when rr-cache exists) now fail, because each needs a write to the read-only root. The PR documents the first four. rerere is an additional edge case.
  • Codex still creates empty .git, .codex, .agents, and .aws directories below the writable roots. Git ignores them (verified above), but they remain artifacts. A follow-up issue is suggested in the PR.

@datacas
datacas merged commit 42a66b0 into main Oct 14, 2026
4 checks passed
@datacas
datacas deleted the task-229 branch October 14, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant