fix(agent-isolation): make the gitignore safety check hook-safe - #1093
Open
potiuk wants to merge 1 commit into
Open
fix(agent-isolation): make the gitignore safety check hook-safe#1093potiuk wants to merge 1 commit into
potiuk wants to merge 1 commit into
Conversation
sandbox-add-project-root.sh refused to write settings.local.json on every invocation from a git hook, reporting "is not gitignored" for a file that was correctly ignored. Because the post-checkout hook is exactly how the helper reaches a fresh worktree, the per-worktree sandbox-allowlist entry that issue apache#197 asks for was never written there -- reads under a new worktree kept hitting sandbox denials. The check ran `cd "$(dirname "$file")" && git check-ignore "$file"`. Git hooks export GIT_DIR, and with GIT_DIR set but no GIT_WORK_TREE git treats the current directory as the work-tree root -- so after the cd into .claude/, the root-anchored pattern `/.claude/settings.local.json` was evaluated against .claude/ as the root, where it cannot match. Discovery now runs with GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE stripped, and check-ignore runs from the resolved toplevel instead of from a subdirectory. Fixing that surfaced a second, older bug in the same block: the fallback guard tested `[ -d "$(dirname "$file")/.." ]`, which is false when .claude/ does not exist yet. On a first run the check therefore matched neither branch and fell through to writing, so the safety check protected nothing in precisely the case it was written for. The replacement walks up to the nearest existing ancestor before asking git anything, so it holds whether or not .claude/ is present. Five regression tests cover both bugs; all five fail against the previous script. They neutralise global and system git config, including an explicit core.excludesFile override -- pointing GIT_CONFIG_GLOBAL at an empty file is not enough, because git still falls back to ~/.config/git/ignore, which commonly already ignores settings.local.json and would mask the "refuses" cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sandbox-add-project-root.shrefused to writesettings.local.jsonon every invocation from a git hook, warningis not gitignoredabout a file that was correctly ignored. Since the post-checkout hook is exactly how the helper reaches a fresh worktree, the per-worktree allowlist entry that setup-isolated-setup-install: "." in allowRead does not cover CWD; need defensive abs-path resolution at install time #197 asks for was never written there — reads under a new worktree kept hitting sandbox denials.cd "$(dirname "$file")" && git check-ignore "$file". Git hooks exportGIT_DIR, and withGIT_DIRset but noGIT_WORK_TREEgit treats the current directory as the work-tree root — so after thecdinto.claude/, the root-anchored/.claude/settings.local.jsonpattern was evaluated against.claude/as the root, where it cannot match.Type of change
.claude/skills/<name>/) — eval fixtures updated belowtools/<system>/*.md)tools/*/withpyproject.toml) — tests onlydocs/,README.md,CONTRIBUTING.md)projects/_template/)prek, workflows, validators)tools/agent-isolation/The second bug
The fallback guard tested
[ -d "$(dirname "$file")/.." ], which is false when.claude/does not exist yet. On a first run the check therefore matched neither branch and fell through to writing — so the safety check protected nothing in precisely the case it was written for. The replacement walks up to the nearest existing ancestor before asking git anything, so it holds whether or not.claude/is present.Test plan
Five regression tests in
TestGitignoreSafetyCheck, covering both bugs:test_refuses_when_not_gitignored.claude/fall-through (bug 2)test_writes_when_git_dir_is_exportedGIT_DIRmisresolution (bug 1)test_git_dir_exported_still_records_repo_root.claude/test_git_dir_exported_still_refuses_when_not_gitignoredtest_works_in_linked_worktreegit worktreeAll five fail against the previous script — verified by reverting the helper and re-running.
Two things worth knowing about how the tests are written:
.claude/for theGIT_DIRcases. Without that they pass against the buggy script too, because the old code'scdfailed and it fell through to writing anyway — theGIT_DIRbug only bites when.claude/already exists, as it does in a real adopter repo. My first draft got this wrong and proved nothing.core.excludesFileoverride. PointingGIT_CONFIG_GLOBALat an empty file is not enough: withcore.excludesFileunset git still falls back to$XDG_CONFIG_HOME/git/ignore(~/.config/git/ignore), which commonly already contains**/.claude/settings.local.jsonand would mask the "refuses" cases — passing on a clean CI box and failing on a developer machine, or vice versa.Checks run:
uv run pytestfortools/agent-isolation— 88 passedruff check/ruff format/mypy(workspace) — passed via pre-commit hooks on the changed filesprek run --all-files— not run; hooks ran on the changed files at commit timeRFC-AI-0004 compliance
Linked issues
Refs #197 — the per-worktree allowlist defence that this helper implements, and which was inert inside hooks.
Notes for reviewers (optional)
env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILEis held in a variable and word-split at the call sites. That is deliberate and safe here (fixed, space-free tokens), but say the word if you would rather see the two calls spelled out in full.GIT_INDEX_FILEas well as the two that matter today. It does not affectcheck-ignore, but hooks export it and leaving it set invites the same class of surprise later.apache/airflow, where the hook fired the false warning on everygit checkout/rebasein a worktree.