Repository navigation
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
goal_ref: "ローカルリポジトリ root checkout は default branch に固定し、issue 作業は linked issue worktree のみで行う"
change_kind: mixed
risk_class: agent_ops_guard
affected_runtimes:
- claude_code
- codex_cli
primary_failure_mode:
- local_root_branch_drift
- no_matching_worktree_block_churn
- agent_context_pollution
- repeated_permission_failure
non_goals:
- remote branch protection の変更
- worktree_scope_guard の置換
- local root での通常実装作業許可
- hooks を唯一の security boundary とみなすこと
- Codex PreToolUse / PermissionRequest hook を完全な enforcement boundary とみなすこと
- Codex hook の実行順序に依存すること
- branch drift guard を general mutation guard に拡張することParent Issue
なし(standalone 実装 Issue)
Parent Goal Ref
ローカルリポジトリ root checkout は default branch に固定し、issue 作業は linked issue worktree のみで行う
Current Validated Scope
scripts/agent-guards/local_main_branch_guard.py.claude/hooks/local_main_branch_guard.sh.claude/hooks/tests/test_local_main_branch_guard.py.claude/settings.jsondocs/dev/hook-boundaries.mdscripts/check_hook_boundaries.pyscripts/check_local_main_branch_state.pyscripts/check_codex_agent_config.py.codex/hooks/local_main_branch_guard.sh.codex/hooks.jsontests/codex/test_local_main_branch_guard.py.codex/rules/default.rules
Remaining Parent Gaps
なし(standalone 実装 Issue)
Outcome
ローカルリポジトリ root checkout が常に main / default branch に固定される。Claude Code・Codex CLI の双方で local_main_branch_guard が機能し、worktree_scope_guard より前段で root checkout の branch drift を即時ブロックする。drift 後に繰り返されていた無駄な token 消費・原因誤認・人間手戻りが防止される。
In Scope
scripts/agent-guards/local_main_branch_guard.pyの新規実装(共有ロジック).claude/hooks/local_main_branch_guard.shの新規実装(Claude Code wrapper).codex/hooks/local_main_branch_guard.shの新規実装(Codex CLI wrapper).claude/settings.jsonの PreToolUse hook 順序更新(secret → local_main → worktree).codex/hooks.jsonへの hook entry 追加(event → matcher → hooks array 形式)scripts/check_hook_boundaries.pyによる hook 順序・manifest drift 検証scripts/check_codex_agent_config.pyによる Codex config 整合性検証scripts/check_local_main_branch_state.pyによる startup preflight の新規実装.claude/hooks/tests/test_local_main_branch_guard.pyの新規テストtests/codex/test_local_main_branch_guard.py.codex/rules/default.rulesの新規テストdocs/dev/hook-boundaries.mdへのlocal_main_branch_guard追記
Out of Scope
- remote branch protection の変更
worktree_scope_guardの既存ロジック変更- local root での通常実装作業の許可
- Codex PreToolUse / PermissionRequest hook を完全な enforcement boundary とみなす設計
- Codex hook の実行順序への依存
- branch drift guard の general mutation guard への拡張
背景
AI エージェント作業中に、ローカルリポジトリ root checkout が main / default branch から issue-* ブランチへ変わった状態で作業が継続された。
その結果、既存の worktree_scope_guard が current branch から active issue を推定し、対応する .claude/worktrees/issue-<N>-* が存在しないため expected_worktree: <no-matching-worktree> として fail-closed した。
このブロック自体は worktree guard の責務として妥当だが、発火タイミングが遅いため、エージェントはその後も gh issue view / uv run / gh issue edit 等を試し、無駄な token 消費・SubAgent 起動・原因誤認・人間への手戻りを発生させた。
問題
現在の guard 層は「active issue worktree 外で mutation しようとした瞬間」は止めるが、次を事前に防げない。
- local root checkout が
main/ default branch からissue-*/worktree-issue-*/ PR checkout branch へ drift する - drift 後に
worktree_scope_guardが<no-matching-worktree>で広範囲に block する - agent が block 原因を workflow 誤りではなく hook 設計不良・scope_signal_guard 問題として誤読する
- Claude Code と Codex CLI の hook / config 境界が非対称になる
方針
worktree_scope_guard の前段に、local root checkout の branch drift を検出・ブロックする guard を追加する。
追加する guard
- 共有本体:
scripts/agent-guards/local_main_branch_guard.py - Claude Code wrapper:
.claude/hooks/local_main_branch_guard.sh - Codex CLI wrapper:
.codex/hooks/local_main_branch_guard.sh - テスト:
.claude/hooks/tests/test_local_main_branch_guard.py/tests/codex/test_local_main_branch_guard.py - config / docs:
.claude/settings.json/.codex/config.toml/docs/dev/hook-boundaries.md/scripts/check_hook_boundaries.py/scripts/check_codex_agent_config.py
Guard 判定仕様
Local root context
以下をすべて満たす場合だけ、この guard の enforcement 対象とする。
local_root_context:
cwd_realpath_equals_project_root: true
current_worktree_is_primary_repo_root: true
cwd_not_under_dot_claude_worktrees: trueproject_root 解決は git worktree list --porcelain -z で primary worktree を catalog から特定する。git rev-parse --show-toplevel 単体は linked worktree 内で main root を返すため使用しない。linked worktree 内では本 guard は allow し、既存の worktree_scope_guard に任せる。解決優先順位:
CLAUDE_PROJECT_DIR- Codex hook input の
cwdから上位探索 git worktree list --porcelain -zの primary worktree path- script path anchor
Default branch 解決
default branch は以下の順で解決する。
LOOP_DEFAULT_BRANCHgit symbolic-ref --short refs/remotes/origin/HEADmainmastertrunk
Block 対象コマンド
local root context で、以下のように root checkout の branch を default branch 以外へ変更しうる Bash を block する。
blocked_patterns:
- git switch <non_default_branch>
- git switch -c <branch>
- git switch -C <branch>
- git switch --detach [<start-point>]
- git switch --orphan <branch>
- git switch --guess <remote-branch-like>
- git checkout <non_default_branch>
- git checkout -b <branch>
- git checkout -B <branch>
- git checkout --orphan <branch>
- git checkout --detach <commit-or-branch>
- git checkout --ignore-other-worktrees <branch>
- git branch -m <branch>
- git branch -M <branch>
- gh pr checkout <number_or_branch>
- hub pr checkout <number_or_branch>
- bash -lc "git switch ..."
- sh -c "git checkout ..."local root context で branch mutation grammar を完全に証明できない compound command は fail-closed にする(unparseable_branch_mutation)。
以下は allow する。
allowed_patterns:
- git status
- git branch --show-current
- git rev-parse --abbrev-ref HEAD
- git worktree list
- git fetch
- git switch <default_branch>
- git checkout <default_branch>
- git checkout -- <path>
- git checkout HEAD -- <path>
- git checkout --pathspec-from-file=<file>
- git checkout -p -- <path>
- git restore <path>注意: git checkout -- <path> / git checkout HEAD -- <path> / git checkout --pathspec-from-file / git checkout -p は branch switch ではないため、この guard では block しない。ファイル mutation の可否は既存の worktree_scope_guard に任せる。
既に drift 済みの場合
local root checkout がすでに default branch 以外の場合は、以下の allowlist のみ許可する。
allowed_when_already_drifted:
git:
- git status
- git branch --show-current
- git rev-parse --abbrev-ref HEAD
- git worktree list
- git switch <default_branch>
- git checkout <default_branch>
gh:
- gh issue view <number>
- gh issue list
- gh pr view <number>
- gh pr list
- gh pr status
blocked_when_already_drifted:
- gh issue edit|comment|close|reopen|delete|lock|unlock
- gh pr checkout|edit|comment|merge|close|reopen|ready|review|update-branch
- hub pr checkout
- uv run / pnpm / npm / test / build commands from drifted rootそれ以外の branch mutation は block する。
Manual override
緊急時のみ、CLI 起動前に人間が明示した環境変数で override できる。
LOOP_ALLOW_LOCAL_ROOT_BRANCH_CHANGE=1
LOOP_LOCAL_ROOT_BRANCH_CHANGE_REASON="manual recovery: ..."hook 対象 Bash コマンド内の inline env 指定は override として扱わない。
例: LOOP_ALLOW_LOCAL_ROOT_BRANCH_CHANGE=1 git switch issue-123-x は block する(hook process env のみ有効)。
block stderr には override reason の全文を出さず、manual_override: accepted か manual_override: missing_reason 程度にとどめる。branch 名は raw 出力せず target_branch_kind に寄せる。
Claude Code integration
.claude/settings.json の PreToolUse に local_main_branch_guard.sh を追加する。
順序は以下とする。
secret_boundary_guardlocal_main_branch_guardworktree_scope_guard- その他 guard
理由: branch drift は worktree scope violation より前に、より明確な reason で止めるべきである。
block 時は exit 2。stderr は最大 10 行、raw command / raw env / secret-like value を出さない。
例:
[local_main_branch_guard] blocked: local root checkout must stay on default branch
reason_code: local_root_branch_drift
current_branch: main
target_branch_kind: issue_branch
recovery: create/enter linked issue worktree, or switch only to default branch
Codex CLI integration
Codex 側にも同一判定を入れる。このリポジトリの Codex hooks は .codex/hooks.json(JSON)に定義する。.codex/config.toml には hooks を含めない。.codex/hooks.json に以下を追加する。
{
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "bash \"$(git rev-parse --show-toplevel)/.codex/hooks/local_main_branch_guard.sh\"",
"timeout": 10,
"statusMessage": "Checking local root branch policy"
}
]
}
],
"PermissionRequest": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "bash \"$(git rev-parse --show-toplevel)/.codex/hooks/local_main_branch_guard.sh\"",
"timeout": 10,
"statusMessage": "Checking local root branch policy"
}
]
}
]
}Codex の PreToolUse は全 shell 経路を完全捕捉する security boundary ではないため、追加で起動前 preflight を用意する。
scripts/check_local_main_branch_state.pyscripts/check_codex_agent_config.pyに hook 有効化確認を追加- Codex セッション開始プロンプト / rules に「preflight 失敗時は実装作業を開始しない」を明記
check_codex_agent_config.py は以下の条件で fail する。
- command handler が
.codex/hooks.jsonのhooks.<Event>[]→hooks[]ネストの外側に定義されている .codex/hooks.jsonに startup preflight が記載されていない- startup preflight が Codex rules / session prompt に記載されていない
共有スクリプトの出力仕様
hook 実行時:
allow:
exit_code: 0
stdout: ""
stderr: ""
block:
exit_code: 2
stdout: ""
stderr: bounded_structural_messageself-test / CLI 実行時:
LOCAL_MAIN_BRANCH_GUARD_RESULT_V1:
status: allow | block
reason_code:
- not_local_root_context
- readonly_command
- recovery_to_default_branch
- local_root_branch_drift
- already_drifted_root
- unparseable_branch_mutation
- inline_env_override_not_allowed
current_branch: string | null
target_branch: string | null
target_branch_kind: default | issue | worktree_issue | pr | unknown | null
hook_flavor: claude | codex | cliAcceptance Criteria
- AC1: Claude Code blocks root branch drift。一時 repo で cwd=repo root、current branch=main の状態で
git switch issue-981-*/git checkout issue-981-*/git checkout -b issue-981-*/gh pr checkout 988/bash -lc 'git switch issue-981-*'を block する。 - AC2: Claude Code allows recovery to default branch。local root が
issue-981-*になっていてもgit switch main/git checkout main/git status/git branch --show-currentは allow する。 - AC3: issue worktree 内ではこの guard は block しない。cwd が
.claude/worktrees/issue-981-*の場合、branch command を原則 allow し既存のworktree_scope_guardに委譲する。 - AC4:
git checkout -- <path>を branch switch と誤判定しない。git checkout -- README.md/git checkout HEAD -- README.md/git restore README.mdは local branch drift としては block しない。 - AC5: bounded stderr / no leak。block stderr は最大 10 行。raw command 全文 / raw tool input JSON / env dump / token-secret / worktree catalog 全体を含めない。
- AC6: hook order is enforced。
secret_boundary_guardがlocal_main_branch_guardより前、local_main_branch_guardがworktree_scope_guardより前であることを test で検証する。 - AC7: hook-boundaries manifest drift check。
docs/dev/hook-boundaries.mdにlocal_main_branch_guardを追加し、scripts/check_hook_boundaries.pyが drift なしで pass する。 - AC8: Codex parity。Codex hook input fixture で
git switch issue-*が deny、git switch mainが allow、PermissionRequest 経由でも同判定になる。 - AC9: startup preflight。
uv run python3 scripts/check_local_main_branch_state.py --jsonが local root が default branch 以外の場合 non-zero で終了する。 - AC10: branch mutation grammar is complete。Block:
git switch -C <branch>/git switch --detach/git switch --orphan <branch>/git switch --guess <remote>/git checkout -B <branch>/git checkout --detach/git checkout --orphan <branch>/git checkout --ignore-other-worktrees <branch>/git branch -m <branch>/git branch -M <branch>。 - AC11:
git checkoutpath restore forms are not overblocked。git checkout HEAD -- README.md/git checkout --pathspec-from-file=<file>/git checkout -p -- README.mdは local branch drift として block しない。 - AC12: already-drifted state uses explicit allowlist。mutating
gh issue edit/comment/closeおよびgh pr checkout/edit/comment/merge/update-branchは already-drifted root から block される。allowlist 外の gh コマンドもデフォルト block。 - AC13: project root / primary worktree detection uses
git worktree list --porcelain -zcatalog。git rev-parse --show-toplevel単体に依存せず、linked worktree 内で primary root を誤判定しない。 - AC14: default branch resolution is tested against shared fixtures。
LOOP_DEFAULT_BRANCH優先 /origin/HEADfallback /mainfallback の順序が既存 local main scratch guard と同一 fixtures で検証される。 - AC15: inline env override in Bash command is blocked。
LOOP_ALLOW_LOCAL_ROOT_BRANCH_CHANGE=1 git switch issue-*は block。hook process env に両変数が設定されている場合のみ allow。 - AC16: unparseable compound shell commands are fail-closed。
cd repo && git checkout issue-*/command git switch issue-*/env FOO=bar git switch issue-*等、branch mutation が証明できない compound command はunparseable_branch_mutationで block する。 - AC17: Codex startup preflight is mandatory。
scripts/check_codex_agent_config.pyが startup preflight 未記載・handler 形式不正・hooks.json 二重定義のいずれかで fail する。
Verification Commands
# AC1
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC2
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC3
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC4
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC5
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC6
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC7
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC8
uv run pytest tests/codex/test_local_main_branch_guard.py -q
# AC9
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC10
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC11
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC12
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC13
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC14
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC15
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC16
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
# AC17
uv run pytest tests/codex/test_local_main_branch_guard.py -qAllowed Paths
scripts/agent-guards/local_main_branch_guard.py.claude/hooks/local_main_branch_guard.sh.claude/hooks/tests/test_local_main_branch_guard.py.claude/settings.jsondocs/dev/hook-boundaries.mdscripts/check_hook_boundaries.pyscripts/check_local_main_branch_state.pyscripts/check_codex_agent_config.py.codex/hooks/local_main_branch_guard.sh.codex/hooks.jsontests/codex/test_local_main_branch_guard.py.codex/rules/default.rules
Runtime Verification Applicability
decision: not_applicable
reason: "hook / script 追加の範囲で完結し、interactive runtime 証跡を成功条件に含まない。ただし temp repo を使った integration fixture(一時 git repo 作成・root に main ブランチ設定・guard 実行・block/allow 判定)は AC1–AC16 テストの一部として実装する"Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
worktree_scope_guardの既存ロジック変更が必要になった場合- Codex CLI の hook 仕様(
.codex/hooks.json形式)が公式ドキュメントと異なることが判明した場合 git worktree list --porcelain -zが primary worktree を期待形式で返さない環境が判明した場合- Claude Code の hook 実行順序が
.claude/settings.jsonとdocs/dev/hook-boundaries.mdで整合不可能な場合 check_hook_boundaries.pyまたはcheck_codex_agent_config.pyの既存テスト前提を破壊する変更が必要になった場合
Required Skills
- Python / shell scripting
- Claude Code hooks / PreToolUse
- git worktree
Delivery Rule
1 Issue = 1 PRで完了する。- Draft PR を既定とする。
Activity
squne121 commentedon Jun 19, 2026
判定
#1014 の問題設定は妥当です。local root checkout が
mainからissue-*系へ drift した結果、既存worktree_scope_guardが<no-matching-worktree>で fail-closed し、エージェントが原因を誤読して無駄なgh issue view/uv run/gh issue editを繰り返した、という failure mode は Issue 本文と整合しています。#1014 自体にはコメントはありません。ただし、現状の #1014 は実装着手前に修正すべき blocker があります。特に Codex hook 設定例が公式仕様とずれており、このまま実装すると「Claude 側は効くが Codex 側は効かない/信頼されず実行されない/順序を誤認する」危険があります。
関連Issue / PRから見た位置づけ
#1014 は #960 / PR #971 の
worktree_scope_guardを置換するものではなく、前段で root branch drift を早期に止める追加 guard です。PR #971 は active issue worktree 外のWrite/Edit/MultiEditと mutating Bash を fail-closed する hook で、project root 解決にgit rev-parse --show-toplevelを使わずCLAUDE_PROJECT_DIRと script anchor を使う設計でした。ここは #1014 の root 解決仕様と衝突し得ます。#974 / PR #979 は local main checkout 上の
.gitignore対象 scratch 出力だけを限定許可する例外です。#1014 はこの例外と競合しない必要があります。特に「local root が default branch であること」は #979 の local main context 判定と同じ中核条件なので、default branch resolver を二重実装せず共通化すべきです。#970 / PR #989 は hook 順序と
hook-boundaries.mdの manifest drift 検出に関係します。PR #989 ではsecret_boundary_guardがworktree_scope_guardより前であること、manifest と settings の matcher が一致することを検証対象にしています。#1014 の「secret → local_main → worktree」の順序も、同じ manifest 検査に載せるべきです。gh pr checkout 988が AC1 に入っている点も妥当です。PR #988 は head branch がissue-981-unit-taxonomy-typesで、まさに local root が issue branch へ drift し得る形です。GitHub CLI 公式 manual でもgh pr checkoutは PR を git checkout し、既定では head branch 名の local branch を使い、--detachや--forceも持ちます。 ([GitHub CLI]1)Blocker
B1. Codex hook 設定例が公式仕様と一致していない
#1014 の Codex 例は次の形です。
これは Codex hooks の三層構造に合っていません。公式 docs では event → matcher group →
hookshandler array の構造で、commandは matcher group 直下ではなく handler 直下に置きます。また、複数 hook は同時起動されるため、Claude のような逐次順序を Codex 側に期待できません。project-local hooks は.codex/layer が trust 済みでないと実行されず、同一 layer にhooks.jsonとconfig.tomlの両方があると merge + warning になります。([OpenAI Developers]2)修正案はこうです。
さらに AC8 は「Codex parity」だけでは弱いです。
scripts/check_codex_agent_config.pyで、config.tomlの nested handler 形式、features.hooks != false、hooks.jsonとの二重定義なし、project-local hook trust 前提の明記、command path が git root 基準であることまで検査対象にしてください。B2. Codex PreToolUse を enforcement boundary と誤認しない条件が弱い
#1014 は「hooks を唯一の security boundary とみなさない」と書いていますが、AC 側ではまだ弱いです。Codex 公式 docs は、PreToolUse は Bash / apply_patch / MCP などを intercept できるが、完全な enforcement boundary ではなく、すべての shell call を捕捉するわけではない、と明記しています。PermissionRequest も「承認が必要なコマンド」の前にだけ走ります。([OpenAI Developers]2) ([OpenAI Developers]2)
したがって、Codex 側は hook だけでなく
scripts/check_local_main_branch_state.py --jsonを session startup preflight の必須 gate に格上げしてください。AC9 は「non-zero で終了する」だけでなく、「Codex セッション開始プロンプト / rules / check_codex_agent_config が preflight 実行を要求し、失敗時は実装作業を開始しない」まで含めるべきです。B3.
git switch/git checkoutの command grammar が不足しているgit switchは単なる branch switch だけではありません。-c/-Cで作成して switch、--orphanで unborn branch、--detachで detached HEAD、--guessで remote branch から暗黙作成できます。#1014 の blocked patterns はgit switch -cと--orphanまでは入っていますが、-C、--detach、--guess、--discard-changes、--merge、--force-create相当の扱いが曖昧です。([Git]3)git checkoutはさらに危険です。公式 docs 上、branch switch と file restore の二つの mode があり、git checkout <tree-ish> -- <pathspec>や--pathspec-from-fileは path restore、git checkout -b/-B/--orphanは branch 作成、--detachは detached HEAD です。#1014 はgit checkout -- <path>を許可に入れていますが、git checkout HEAD -- README.md以外に--pathspec-from-file、-p/--patch、-B、--detach、--ignore-other-worktreesの扱いをテスト化する必要があります。([Git]4) ([Git]4)追加すべき AC:
B4. root 解決仕様が既存 guard と衝突し得る
#1014 は project root 解決の優先順位に
git rev-parse --show-toplevelを入れています。一方、PR #971 のworktree_scope_guardは、worktree isolation ではgit rev-parse --show-toplevelが main root を返すため使わない、という設計でした。ここを雑に実装すると、issue worktree 内なのに local root context と誤判定するか、逆に primary root を見逃す可能性があります。修正方針は、
git rev-parse単体ではなくgit worktree list --porcelain -zを使って「primary worktree path」と「linked issue worktree path」を catalog から決めることです。Git の porcelain-zは machine parsing 用に NUL terminated 形式を提供しているため、既存worktree_scope_guardと同じ catalog parser を共有するのが安全です。([Git]4)B5. 「既に drift 済みの場合の read-only Git / gh allow」が広すぎる
#1014 の背景では、drift 後に
gh issue view/uv run/gh issue edit等を試して churn したことが問題です。ところが仕様では「read-only Git / gh command」を許可するとしか書いていません。gh issue editは明らかに read-only ではありません。ここは allowlist にしてください。推奨:
B6. manual override の検証条件が不足している
「CLI 起動前に人間が明示した env だけ許可、inline env は不許可」という方針は正しいです。ただし実装上は、hook process の環境にある env と Bash command 文字列中の inline env を分けて扱う必要があります。Claude hook は stdin の
tool_input.commandと process env を両方見られます。exit 2 は PreToolUse / PermissionRequest を block できますが、exit 2 の場合 stdout JSON は無視されるため、構造化出力と stderr 方式を混ぜない方がよいです。([Claude API Docs]5)追加テスト:
stderr には override reason の全文を出さず、
manual_override: acceptedかmanual_override: missing_reason程度にしてください。branch 名も raw 出力せず、target_branch_kind: issue | worktree_issue | pr | unknownに寄せる方が安全です。B7. Runtime Verification Applicability が弱い
#1014 は
runtime verification: not_applicableとしていますが、これはアプリ runtime には正しい一方、hook 自体は operational runtime です。hook / script 追加は「実行時挙動が本体」なので、temp repo integration test と self-test は必須です。Issue 本文の verification commands は unit test 寄りなので、次を足してください。B8.
bash -lc/ compound command の fail-closed 方針が必要#1014 は
bash -lc "git switch ..."とsh -c "git checkout ..."を block 対象にしていますが、現実には次のような形も出ます。local root context で branch mutation grammar を完全に証明できない compound command は、branch guard では fail-closed にしてください。これは
worktree_scope_guardの「unparseable Bash that may mutate is blocked」という既存思想とも整合します。PR #971 の設計は shell wrapper の再帰 unwrap と unparseable mutating Bash の fail-closed を採っています。Issue本文に入れるべき修正差分
最小修正は以下です。
check_codex_agent_config.pymust fail if:[features].hooks = false[[hooks.<Event>.hooks]].codex/config.tomland.codex/hooks.jsondefine hooks in the same layer最終結論
#1014 は必要です。特に #960/PR #971 の
worktree_scope_guardは「mutation outside active issue worktree」を止める層であり、local root が issue branch に drift する前段事故を説明する UI / reason code ではありません。#1014 のlocal_main_branch_guardは追加する価値があります。ただし、このまま実装に投げると Codex 側の hook 設定・信頼・不完全 interception・command grammar の穴で fail-open します。先に #1014 本文を上記 blocker で補強し、
Codex config shape、startup preflight、git checkout/switch grammar、gh allowlist、root/worktree catalog resolverを AC に昇格してください。squne121 commentedon Jun 20, 2026
Refinement Loop: Human Escalation Required
The loop has terminated and requires human review. Cause: human judgment required
human_escalationhuman_judgment_requiredBlockers
squne121 commentedon Jun 20, 2026
CONTRACT_REVIEW_RESULT_V1
VC Preflight 詳細
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -quv run pytest tests/codex/test_local_main_branch_guard.py -qContract Snapshot
Outcome: ローカルリポジトリ root checkout が常に
main/ default branch に固定される。Claude Code・Codex CLI の双方でlocal_main_branch_guardが機能し、worktree_scope_guardより前段で root checkout の branch drift を即時ブロックする。Worktree / Branch:
.claude/worktrees/issue-1014-local-main-branch-guardworktree-issue-1014-local-main-branch-guardAllowed Paths:
scripts/agent-guards/local_main_branch_guard.py.claude/hooks/local_main_branch_guard.sh.claude/hooks/tests/test_local_main_branch_guard.py.claude/settings.jsondocs/dev/hook-boundaries.mdscripts/check_hook_boundaries.pyscripts/check_local_main_branch_state.pyscripts/check_codex_agent_config.py.codex/hooks/local_main_branch_guard.sh.codex/hooks.jsontests/codex/test_local_main_branch_guard.pyRuntime Verification Applicability:
not_applicableRequired Skills: Python / shell scripting, Claude Code hooks / PreToolUse, git worktree
squne121 commentedon Jun 20, 2026
実装完了 — Draft PR #1041
PR
#1041
検証結果
Allowed Paths 準拠
全変更ファイルが contract Allowed Paths 内に収まっている(self-report: true)。
fix(#1014): B1-B6 blocker fixes — subdirectory context, git global op…
squne121 commentedon Jun 20, 2026
CONTRACT_REVIEW_RESULT_V1
Scope Delta 詳細
前回 go コメント(#issuecomment-4756083657)から以下を変更:
.codex/rules/default.rules.codex/rules/default.rulesに記述しcheck_codex_agent_config.pyが実検証するためContract Snapshot
Worktree / Branch:
.claude/worktrees/issue-1014-local-main-branch-guardworktree-issue-1014-local-main-branch-guardAllowed Paths(更新後):
scripts/agent-guards/local_main_branch_guard.py.claude/hooks/local_main_branch_guard.sh.claude/hooks/tests/test_local_main_branch_guard.py.claude/settings.jsondocs/dev/hook-boundaries.mdscripts/check_hook_boundaries.pyscripts/check_local_main_branch_state.pyscripts/check_codex_agent_config.py.codex/hooks/local_main_branch_guard.sh.codex/hooks.jsontests/codex/test_local_main_branch_guard.py.codex/rules/default.rules← Scope Delta 追加VC preflight: 全 AC1–AC17 が expected_baseline_fail / go(exit4)
fix(#1014): B1-B5 blocker fixes — detached HEAD, git -c bypass, drift…
feat(#1014): local_main_branch_guard — ローカル root checkout のブランチ drift…