Repository navigation
Description
背景
#960 / PR #971 で worktree_scope_guard が PreToolUse で active issue worktree 外の Write/Edit/MultiEdit と mutating Bash を fail-closed に block する。再レビューで判明した fail-open(外部 absolute path への shell file-write 等)を #971 で塞ぐと guard はより厳格になる。
その結果、LOOP_ISSUE_NUMBER 等で active issue context が解決されたまま local main checkout 上で作業すると、.gitignore 対象の生成物(artifacts/, playwright-report/, test-results/, coverage/, .cache/ 等)への書込も block され、正当な作業が止まる懸念がある。
Outcome
worktree_scope_guard.py に LOCAL_MAIN_SCRATCH_ALLOW_V1 判定関数が実装され、local main context(cwd_realpath == project_root_realpath かつ current_branch == main/default かつ cwd が linked issue worktree 内でない)からの Write/Edit/MultiEdit が repo-local .gitignore 由来の gitignored & untracked な safe_scratch_prefix 配下のみ allow される。tracked / global-only ignore / sensitive path / symlink component を含む target への書込は block されたままになる。
目的
main 汚染防止を維持したまま、明示的に安全な scratch 出力先への local main 書込のみを許可する例外 LOCAL_MAIN_SCRATCH_ALLOW_V1 を追加し、作業継続性と guard の安全性を両立する。
設計方針
.gitignore 単独での blanket allow は危険(.gitignore は tracked file に効かず、.env/local config/secret cache を含み得る。git add -f で強制 stage も可能)。そのため以下の複合条件をすべて満たす direct Write/Edit/MultiEdit のみ allow する(Phase 1)。
local_main_context 判定(3 条件すべて必須)
cwd_realpath == project_root_realpath
current_branch in [main, repo_default_branch]
cwd is not inside any linked issue worktree (git worktree list --porcelain -z で検証)
target 判定(すべて必須)
- target_realpath が project_root_realpath 内
- target_realpath が expected_issue_worktree /
.git/**/.claude/worktrees/**外 - normalized relpath が explicit safe_scratch_prefix 配下(component boundary 照合、文字列 prefix 不可)
- safe_scratch_prefix は実装時に repo-local
.gitignoreへ追加する git ls-files --error-unmatch -- <rel>が nonzero(tracked/index 入りを reject)git check-ignore -v -- <rel>が repo-local.gitignore由来(global excludes /.git/info/excludeのみでは不十分)- sensitive path denylist に非該当(
.env*,*.pem/key/p12/pfx,*token*/*secret*/*credential*,.npmrc/.pypirc/.netrc,.ssh/**,.config/gh/**) - path component に既存 symlink を含まない(safe root が存在かつ非 symlink、parent が非 symlink、target が存在する場合は非 symlink)
Phase 分割
- Phase 1(本 Issue): Write/Edit/MultiEdit のみ対象。Bash 例外は入れない。
- Phase 2(follow-up): PR impl(#960): worktree 外 mutation を遮断する worktree_scope_guard を追加 #971 write-target extraction を利用し、静的に抽出できた file-write のみ許可。dynamic destination は fail-closed 維持。
git/gh/package manager mutation
scratch 例外を適用しない(常に block)。
非ゴール
.gitignore単独での blanket allow- git/gh/package manager mutation の許可
- staged diff /
git add -Aの Allowed Paths 混入判定(実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963 の責務) - Phase 1 での Bash write exception(Phase 2 で対応)
- global
.gitignoreexcludes や.git/info/excludeのみ一致による allow tmp/全体の safe-prefix 化(tmp/scratch/等の専用 subtree に限定)- 実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963 未導入時の
git add -fルート問題の解決(実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963 blocking dependency として明記)
関連
- depends on 実装: active issue worktree 外への Write/Edit/Bash mutation を hook で遮断する #960 / PR impl(#960): worktree 外 mutation を遮断する worktree_scope_guard を追加 #971(worktree_scope_guard 本体・write-target extraction)
- 実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963(
git add -Ablock / staged diff Allowed Paths 照合): 実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963 未実装の間はgit add -fによる index 混入ルートが残る - 設計詳細は PR impl(#960): worktree 外 mutation を遮断する worktree_scope_guard を追加 #971 のレビュー議論(LOCAL_MAIN_SCRATCH_ALLOW_V1)を参照
- Anchor レビュー: worktree_scope_guard に local main scratch 出力先の安全な例外 (LOCAL_MAIN_SCRATCH_ALLOW_V1) を追加する #974 (comment)
Acceptance Criteria
- AC1:
cwd_realpath == project_root_realpathかつcurrent_branch == main/default_branchかつcwd が linked issue worktree 内でない三条件をすべて満たすときのみ local main scratch context と認める - AC2: safe_scratch_prefix 配下の repo-local
.gitignore由来 gitignored & untracked path への local main からの Write/Edit/MultiEdit を allow する(prefix は実装時に.gitignoreへ追加される) - AC3: safe-prefix 外 / tracked / global-only ignore / sensitive path / symlink component を含む target への local main 書込は引き続き block する
- AC4: prefix containment は
realpath/commonpathを使い path component boundary で照合する(artifacts_evil/やartifacts/../README.mdを block) - AC5: symlink policy: safe_root が存在かつ非 symlink、target への path component に既存 symlink を含む場合は fail-closed
- AC6: sensitive path denylist(
.env*,*.pem/key/p12/pfx,*token*/*secret*/*credential*,.npmrc/.pypirc/.netrc,.ssh/**,.config/gh/**)を含む target は block - AC7:
git ls-files --error-unmatch -- <rel>が 0 を返す(tracked/index 入り)場合は block - AC8:
git check-ignore -v -- <rel>で source が repo-local.gitignore由来でない(global excludes /.git/info/excludeのみ)場合は block - AC9: git/gh/package manager mutation には scratch 例外を適用しない
- AC10: Phase 1 として Write/Edit/MultiEdit のみを対象とし、Bash write exception は Phase 2 follow-up issue に分離する
- AC11: pytest behavioral harness(実 git repo + real issue worktree 作成、subprocess 経由で guard wrapper 呼出)で以下を検証する:
allow_local_main_write_to_repo_ignored_untracked_playwright_reportallow_local_main_write_to_repo_ignored_untracked_test_resultsblock_safe_prefix_not_in_repo_gitignoreblock_global_gitignore_only_matchblock_tracked_file_under_safe_prefixblock_artifacts_prefix_sibling_artifacts_evilblock_artifacts_dotdot_readmeblock_symlink_safe_root_to_outsideblock_symlink_parent_componentblock_existing_target_symlinkblock_sensitive_dotenv_under_artifactsblock_sensitive_npmrc_under_cacheblock_write_when_cwd_inside_issue_worktreeblock_write_when_branch_is_issue_branch_not_mainblock_write_inside_dot_git_even_if_ignoredblock_write_inside_claude_worktrees_even_if_ignoredblock_git_add_force_ignored_scratch_file
Allowed Paths
.claude/hooks/worktree_scope_guard.py.claude/hooks/tests/test_local_main_scratch_allow.py.gitignore
Verification Commands
# AC1
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "local_main_context or cwd_inside_issue_worktree or branch_is_issue_branch"
# AC2
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "allow_local_main"
# AC3
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "block_safe_prefix_not or block_tracked or block_global or block_sensitive or block_symlink or block_write"
# AC4
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "artifacts_evil or dotdot"
# AC5
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "symlink"
# AC6
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "sensitive"
# AC7
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "tracked"
# AC8
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "global_gitignore"
# AC9
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "git_add_force or git_mutation"
# AC10
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k "write_exception or bash_exception"
# AC11
$ uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v
# preflight-scope: runtime_only
$ test -f .claude/hooks/worktree_scope_guard.py
# preflight-scope: runtime_only
$ rg -n "LOCAL_MAIN_SCRATCH_ALLOW_V1|local_main_scratch" .claude/hooks/worktree_scope_guard.py
# preflight-scope: runtime_only
$ rg -n "playwright-report|test-results|artifacts" .gitignore
# preflight-scope: runtime_only
$ uv run pytest .claude/hooks/tests/ -v --ignore=.claude/hooks/tests/test_local_main_scratch_allow.pyRuntime Verification Applicability
- decision: deferred
- reason: worktree_scope_guard.py への LOCAL_MAIN_SCRATCH_ALLOW_V1 実装が未完了のため、behavioral harness(pytest)は現時点で実行不可。実装完了後に uv run pytest で検証する。
- deferred_destination:
- destination_type: issue
- destination_ref: worktree_scope_guard に local main scratch 出力先の安全な例外 (LOCAL_MAIN_SCRATCH_ALLOW_V1) を追加する #974(本 Issue の実装完了・Stop Conditions 達成後)
- deferred_verification_condition: worktree_scope_guard.py に LOCAL_MAIN_SCRATCH_ALLOW_V1 判定関数が実装され、test_local_main_scratch_allow.py が存在すること
Stop Conditions
- worktree_scope_guard.py に
LOCAL_MAIN_SCRATCH_ALLOW_V1判定関数が実装され、AC1..AC11 の pytest が全 pass .gitignoreに Phase 1 safe_scratch_prefix エントリが追加済み- 既存 worktree_scope_guard tests が退行しない
- Phase 2(Bash exception)は本 Issue の scope 外
依存 / ブロック
- depends on: 実装: active issue worktree 外への Write/Edit/Bash mutation を hook で遮断する #960, PR impl(#960): worktree 外 mutation を遮断する worktree_scope_guard を追加 #971(マージ済み)
- blocked by: 実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963(staging guard)が入るまで
git add -fルートは別 blocker として残存
Activity
squne121 commentedon Jun 17, 2026
判定
#974 は方向性として妥当。ただし、現行ドラフトのまま実装すると “safe scratch” 例外が権限穴になる可能性が高い。
LOCAL_MAIN_SCRATCH_ALLOW_V1は、単なる safe-prefix allowlist ではなく、local main checkoutの厳密判定、repo-local ignore 判定、tracked/index 判定、symlink/TOCTOU 対策、secret denylist、#963 との依存順序まで含めた contract に格上げすべきです。#974 の問題意識は正しいです。#960 / PR #971 で
worktree_scope_guardが導入され、active issue worktree 外の Write/Edit/MultiEdit と mutating Bash を fail-closed に遮断する設計になったため、main checkout 側の一時生成物まで巻き添えで止まる余地があります。#974 はその例外としてLOCAL_MAIN_SCRATCH_ALLOW_V1を提案しています。一方で、#974 自体にコメントはまだありません。PR #971 側のコメントには、レビューで AC1..AC15、75 pytest、mutation test、
realpath+commonpath、bounded stderr、secret guard 先行などが確認された旨が残っています。根拠整理
#960 の本体 contract は、「active issue worktree が存在するとき、worktree 外への Write/Edit/MultiEdit と mutating Bash を PreToolUse hook で block する」ことを目的にしています。expected worktree 解決は
CLAUDE_PROJECT_DIRと settings.json 親解決を優先し、git rev-parse --show-toplevelを使わない、worktree catalog はgit worktree list --porcelain -z、path containment はrealpath/commonpath、malformed payload や git binary 不在は fail-closed、というかなり強い契約です。PR #971 はこれを実装済み・merge 済みで、変更ファイルは
.claude/hooks/worktree_scope_guard.sh、.py、tests、settings の 4 ファイル、75 tests pass、pnpm test/typecheck/lint/buildpass とされています。Claude Code の hook 仕様上、PreToolUse で exit 2 を返すと tool call はブロックされ、stderr がフィードバックとして渡されます。したがって、この guard は「注意喚起」ではなく実際の enforcement point です。ここに例外を足す場合、例外条件が policy の実体になります。([Claude Code]1)
現行コードもこの前提に沿っています。Write/Edit/MultiEdit は active issue が解決され、対応 worktree が 0 件・複数件・不明の場合に block し、target が expected worktree 内にある場合だけ allow します。Bash も read-only は allow しますが、mutating/unknown は expected worktree 外 cwd、外部
cd/git -C、外部 write target、解析不能 write mutation を block します。Bash の write target 抽出は redirection、
tee、sed -i/perl -i、Python/Node/Ruby one-liner などを対象にしており、変数・glob・command substitution 等の動的 destination は抽出しないため、caller 側で fail-closed になります。#963 は別 issue として、
git add -A/git add .の常時 block と、git commit前の cached diff の Allowed Paths 照合を扱います。#974 が「git/gh/package-manager mutation は例外にしない」「staged diff は #963 の責務」とするのは設計分割としては妥当ですが、#963 未実装のまま safe scratch 例外を入れると、ignored scratch artifact がgit add -fで index に入るルートが残ります。 Git 公式 docs でも、ignored file は通常git addされませんが--force/-fで追加できます。([Git]2)致命的な指摘
1.
local mainの定義が足りない#974 は「local main scratch」と言っていますが、draft 条件は実質的に「target が project_root 内、expected worktree 外、safe-prefix 配下、ignored/untracked」までしか明示していません。これでは、どの checkout からの mutation なら “local main” と認めるのかが曖昧です。
必要なのは以下のような判定です。
Git worktree は 1 repo に複数 working tree を関連付ける仕組みで、
git worktree list --porcelain -zは機械処理用に NUL 区切りを提供します。#960 / PR #971 がこの形式に寄せたのは正しいですが、#974 の例外では「main checkout 自体」を catalog 上でどう特定するかを contract 化する必要があります。([Git]3)2. 現行
.gitignoreと候補 safe-prefix が一致していない#974 の候補には
artifacts/,playwright-report/,test-results/,coverage/,.cache/,.tmp/,tmp/が挙がっていますが、現行.gitignoreで確認できるのは主にtmp/,.claude/worktrees/,.claude/plans/,.guard_shadow_log.jsonl,.venv/,__pycache__/,.pytest_cache/などです。artifacts/,playwright-report/,test-results/,coverage/,.cache/,.tmp/は現行.gitignoreには見えません。これは設計上かなり大きいです。#974 は
git check-ignore -q -- <rel>を条件にしていますが、この条件を厳密に実装すると、候補 prefix の多くは allow されません。逆に.gitignore条件を緩めると、「gitignored generated artifacts のみ」という #974 の安全根拠が崩れます。Git 公式 docs では、
.gitignoreは「意図的に untracked にするファイル」を指定するもので、すでに tracked なファイルには効きません。また ignore 判定は.gitignoreだけでなく.git/info/excludeや global excludes など複数ソースを見ます。([git.github.io]4)したがって、単に
git check-ignore -qが true なら allow では弱いです。git check-ignore -vで source を確認し、少なくとも repo 内の.gitignore由来であることを要求する方が安全です。git check-ignoreの公式 docs でも-vは matching pattern の source を出せるとされています。([Git]5)3. 「safe-prefix」だけでは secret を守れない
artifacts/や.cache/は名前だけでは安全ではありません。以下はすべて safe-prefix 配下に置けてしまいます。現行 settings では secret boundary guard が worktree guard より前に配置されています。これは良い順序です。
ただし #974 の例外は worktree guard 側に追加されるため、worktree guard 自身も
safe-prefixだけでなく sensitive path denylist を持つか、既存 secret boundary の path 判定ロジックを共有すべきです。settings には.ssh,.config/gh,.env,printenv,gh secretなどの deny が見えますが、scratch 配下の secret filename 全般を網羅しているとは読めません。4. symlink / TOCTOU が未解決
PR #971 の
realpath+commonpath方針はstartswithより正しいです。Python docs でもcommonprefix()は文字単位で無効な path を返し得る一方、commonpath()は有効な common sub-path を返すと説明されています。([Python documentation]6)しかし、
realpath()は存在しない component 以降をそのまま付ける挙動があり、存在しない target file をこれから作る場合、parent directory の symlink 差し替えや hook 判定後から write 実行までの TOCTOU が残ります。Python docs も、strict でないrealpath()は最初に評価不能になった component 以降を unchanged で付けると説明しています。([Python documentation]6)最低限、scratch allow では以下を要求すべきです。
PreToolUse hook は atomic open を代行できないため完全な TOCTOU 排除はできません。ただし、既存 symlink component を reject するだけで、実用上の bypass はかなり減ります。
5.
tmp/は広すぎる現行
.gitignoreはtmp/全体を ignore しています。しかし
tmp/にはissue_*_backup_*.md,issue_*_new_*.md,reviewer_feedback.md,edit_issue_*.mdなど、プロトコル運用上の中間ファイルが想定されています。これをLOCAL_MAIN_SCRATCH_ALLOW_V1の broad safe-prefix にすると、生成物だけでなくレビュー本文、issue 本文バックアップ、作業指示系ファイルまで「safe scratch」として扱うことになります。tmp/全体ではなく、例えば以下のように用途別に狭めるべきです。tmp/を残すならtmp/scratch/のように専用 subtree に限定するべきです。技術・配布物ごとの実装品質レビュー
Playwright の HTML reporter は、公式 docs 上もデフォルトで
playwright-reportに report を出力します。これは #974 の候補 prefix と整合します。(Playwright)Playwright Test の
outputDirはデフォルトで<package.json-directory>/test-resultsで、テスト実行中に作られる file の出力先として使われます。これも候補 prefix と整合します。([Playwright]8)一方、
artifacts/,coverage/,.cache/,.tmp/はツール標準というより repo convention です。coverage.py の HTML report は source を annotated HTML として出すため、coverage 出力にはソース断片が含まれる可能性があります。([Coverage.py]9) したがってcoverage/を safe とするなら「commit されない」「PR artifact として配布しない」「secret scan 対象外にしない」などの運用条件も明記すべきです。また、#960 の review comment には、main working tree 上の untracked artifact、具体的には
playwright-report/やartifacts/が lint/build の baseline 検証を汚染した、という実害が記録されています。これは #974 の動機として強い根拠ですが、同時に「artifact が eslint 等に拾われる」という事実でもあります。safe scratch にするだけでなく、toolchain 側でも ignore 対象にする必要があります。推奨する修正版 contract
#974 の AC は次の水準まで具体化してください。
git ls-files --error-unmatchは、path が index に存在しない場合に error を返すため、tracked/index 入り file の reject に使えます。([Git]10)git check-ignoreは tracked files をデフォルトでは表示しないため、ls-filesとcheck-ignoreの両方が必要です。([Git]5)追加すべき adversarial tests
最低限、以下の pytest を #974 の AC に含めるべきです。
PR #971 の tests は、実 git repo と real issue worktree を作る behavioral harness で、
__file__anchor を使い、subprocess 経由で guard wrapper を叩く構成でした。この形式を #974 でも踏襲するべきです。実装方針
おすすめは phase 分割です。
Phase 1:
Write/Edit/MultiEditだけを対象にする。Bash 例外は入れない。Phase 2: PR #971 の write-target extraction を利用し、redirection/tee/sed/perl/interpreter one-liner のうち destination が静的に抽出できた file-write のみ許可する。dynamic destination は fail-closed のままにする。
Phase 3: #963 が入った後、staged diff /
git add -fルートとの結合テストを追加する。実装は概ねこの形です。
最終レビュー結論
#974 は 実装してよいが、現状の draft AC では不足です。特に、現行
.gitignoreと候補 prefix の不一致、global ignore 依存、local main判定の曖昧さ、secret denylist 未定義、symlink/TOCTOU 未対策、#963 未導入時のgit add -fルートが問題です。修正後の merge 条件は次で固定してください。
.gitignoreに許可する scratch prefix を repo-local に追加する、または現行.gitignoreにある prefix だけを許可する。git check-ignore -vの source が repo-local.gitignoreであることを検証する。git ls-files --error-unmatchで tracked/index 入りを reject する。cwd == project_rootかつ branch がmain/defaultのときだけ local main scratch と認める。artifacts_evilやartifacts/../README.mdを block する。git add -f/ staged diff 混入を別 blocker として明記する。squne121 commentedon Jun 17, 2026
Refinement Loop: Approved
The issue has been approved and is ready for implementation.
approvedsqune121 commentedon Jun 17, 2026
CONTRACT_REVIEW_RESULT_V1
status: go
Contract Snapshot
worktree_scope_guard.pyにLOCAL_MAIN_SCRATCH_ALLOW_V1判定関数を実装し、local main context からの safe_scratch_prefix 配下への gitignored & untracked path への Write/Edit/MultiEdit のみ allow する.claude/worktrees/issue-974-local-main-scratch-allowworktree-issue-974-local-main-scratch-allowsha256:f02fb5a89fd34567398cfec708f1dd0ca13ed71ba51dd47fea234f38780f8f1fAllowed Paths
.claude/hooks/worktree_scope_guard.py.claude/hooks/tests/test_local_main_scratch_allow.py.gitignoreAcceptance Criteria
AC1〜AC11(チェックボックス形式・決定論的 VC 付き)
Verification Commands
AC1〜AC11:
uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -v -k <filter>形式checks
state/needs-humanなし)expected_baseline_fail/go, exit4)VC preflight summary
Handoff
.claude/worktrees/issue-974-local-main-scratch-allowworktree-issue-974-local-main-scratch-allowsqune121 commentedon Jun 17, 2026
実装完了
PR #979(Draft)を作成しました。
変更ファイル
.claude/hooks/worktree_scope_guard.py—LOCAL_MAIN_SCRATCH_ALLOW_V1実装.claude/hooks/tests/test_local_main_scratch_allow.py— AC11 behavioral harness(14 テスト).gitignore— safe_scratch_prefix 5 エントリ追加検証結果
uv run pytest .claude/hooks/tests/test_local_main_scratch_allow.py -vuv run pytest .claude/hooks/tests/ --ignore=...test_local_main_scratch_allow.pypnpm typecheckpnpm lintpnpm testpnpm build設計上の主要な実装詳細
_is_repo_local_gitignore():git check-ignore -vの出力ソースが相対パス(.gitignore)で返される場合はproject_rootを基準に解決(os.path.realpath(os.path.join(project_root, source_part)))_decide_write()内でis_inside(expected, target)が False になった後に実行(worktree 内へのアクセスは従来通り allow、worktree 外への書き込みのみ scratch check を適用)Allowed Paths 準拠
allowed_paths_compliance: truefix(#974): ブロッカー3件を解消する
impl(#974): worktree_scope_guard に LOCAL_MAIN_SCRATCH_ALLOW_V1 を追加 (#979
squne121 commentedon Jun 18, 2026
post-merge-cleanup: 完了 (2026-06-18)
実行内容:
worktree-issue-974-local-main-scratch-allow削除済み.claude/worktrees/issue-974-local-main-scratch-allow削除済みissue-919,issue-936) も併せて削除済み