Skip to content

fix: restore bounded rtk git publish path (#1241) - #1244

Merged
squne121 merged 14 commits into
mainfrom
worktree-issue-1241-codex-linked-issue-worktree-rtk-git-muta
Jun 30, 2026
Merged

squne121 merged 14 commits into
mainfrom
worktree-issue-1241-codex-linked-issue-worktree-rtk-git-muta

Conversation

@squne121

@squne121 squne121 commented Jun 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

この PR は、issue 用 worktree での rtk git add/commit/push を安全な手順に戻し、review で指摘された抜け道を塞ぐ修正です。

  • .codex/rules/default.rules の rtk git 変更許可を、add / commit -m / push origin の 3 つの形に分けました。
  • git_mutation_command_policy.py で wrapper 経由の解釈をやめ、Allowed Paths と staged diff の範囲確認を fail-closed にしました。
  • worktree_scope_guard.py の issue 未解決 allow をやめ、issue context が取れない変更操作を deny にしました。
  • HOOK_COMMAND_REPAIR_HINT_V1 の必須項目を root/worktree の両 guard、SSOT、validator、test に揃えました。

受け入れ条件の達成状況

  • AC1: linked issue worktree 内の rtk git add <allowed-path-file> を維持しつつ、root、issue 未解決、broad pathspec を block。
  • AC2: rtk git commit -m の前に、staged diff が Allowed Paths の範囲内かを検証。
  • AC3: rtk git push origin HEAD:refs/heads/<active_branch> 以外の push を block。
  • AC4-AC5: direct git 変更、破壊的な git、wrapper 経由の変更解釈は引き続き fail-closed。
  • AC6-AC9: shared policy、repair hint schema、docs、tests のずれを縮小。
  • AC10-AC12: validator と regression test を更新し、Issue 実装: Codex linked issue worktree 内の rtk git mutation path と hook/rules split-brain を解消する #1241 の指摘を再現できる形にした。

検証コマンド結果

  • uv run pytest scripts/agent-guards/tests/test_git_mutation_command_policy.py -q : 9 passed
  • uv run pytest .claude/hooks/tests/test_issue1241_hook_repair_hint.py -q : 5 passed
  • uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q : 268 passed
  • uv run pytest .claude/hooks/tests/test_worktree_scope_guard.py -q : 206 passed
  • node scripts/check-codex-agents.mjs --self-test : all assertions passed

Allowed Paths 遵守

@github-actions

github-actions Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

Preview removed (PR #1244 closed).

@squne121 squne121 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: APPROVE

Mergeability

  • mergeable=MERGEABLE, merge_state_status=CLEAN

Evidence Check

  • AC coverage: PR 本文の ## 受け入れ条件の達成状況 で AC1-AC12 の達成と根拠が記録されていることを確認。
  • Allowed Paths: PR diff は issue #1241 の Allowed Paths 12 件に限定されていることを確認。
  • CI Verification: ci_verdict_summary.py が expected head 0f0123393f0eeefe24a225b27ba58a626d0a7977 に対して status: all_pass を返したことを確認。
  • 検証コマンド結果: issue contract の Verification Commands と追加の codex execpolicy 回帰確認が green。

Blockers

  • なし

Non-blockers

  • Draft PR のままだが、review gate 上は blocker ではない。

LOOP_VERDICT_V2

verdict: APPROVE
reviewed_head_sha: 0f0123393f0eeefe24a225b27ba58a626d0a7977
merge_ready: true
mergeability:
  mergeable: MERGEABLE
  merge_state_status: CLEAN
blockers: []
required_auto_actions: []
auto_fix_applied: []
follow_up_issue_requests: []

@squne121

Copy link
Copy Markdown
Owner Author

判定

REQUEST_CHANGES 相当。まだ undraft / merge しない方がよいです。

PR #1244 は open / draft / mergeable で、head は 0f0123393f0eeefe24a225b27ba58a626d0a7977、変更は 12 files / +639 / -99 です。PR 本文上は AC1–AC12 達成、CI・pytest・validator green を主張していますが、その主張の一部が実装境界と合っていません。

CI は head SHA に対して ci、Japanese check、hook integrity、agent manifest、deploy-pages が success です。ただし CI green は今回の policy 設計リスクを十分に覆っていません。

P0: merge blocker

1. .codex/rules/default.rules が rtk git add/commit/push を prefix allow しており、hook を authorization 前提にしている

PR は .codex/rules/default.rules で ["rtk", "git", ["add", "commit", "push"]] を allow にしています。justification では「runtime hooks validate exact bounded shape」と書いていますが、これは repo 内の既存原則と衝突します。

AGENTS.md と CLAUDE.md は、hook は diagnostic/prevention layer であり canonical gate ではなく、post-run verifier が canonical gate だと明記しています。

Codex rules は prefix rule で、複数 rule が一致した場合は forbidden > prompt > allow の最も restrictive な decision が勝ちます。つまり、rtk git add/commit/push を allow にするなら、危険形状を rules 側でより restrictive に落とす設計が必要です。現 PR は「hook が止めるから allow」という設計に見えます。([OpenAI Developers]1)

さらに Codex hooks 公式 docs は、現行の hook interception は simple shell calls に限定され、完全な shell interception boundary ではないと説明しています。これは repo docs の「hook は canonical gate ではない」という方針と整合します。([OpenAI Developers]2)

修正方針:
.codex/rules/default.rules では rtk git add/commit/push を blanket allow しない。少なくとも invalid shape を forbidden / prompt に落とす rules と match / not_match を追加し、codex execpolicy check で最終 decision を固定してください。runtime hook は二次防御に戻すべきです。


2. rtk git add / commit が Allowed Paths と staged index を検証していない

新規 git_mutation_command_policy.py は rtk git add について、-A, -u, --all, broad pathspec, directory, wildcard などを拒否しますが、issue contract の Allowed Paths matcher とは接続していません。許可判定は最終的に status="allow" を返すだけです。

rtk git commit -m も len(args) == 2 と -m の確認だけで allow され、staged index が Allowed Paths subset かどうかを検査していません。

Git の仕様上、git commit は index の現在内容を commit します。したがって、out-of-scope file が既に stage 済みなら、commit -m の allow は Allowed Paths gate の迂回になります。([Git]3)

git add も pathspec、directory、glob、--pathspec-from-file などで index を操作できます。今回の実装は一部を拒否していますが、「repo-relative POSIX path が Issue #1241 の Allowed Paths 内か」という本質的な検証がありません。([Git]4)

修正方針:
classify_rtk_git_mutation() だけで allow を返さず、次を必須にしてください。

add:
- pathspec は literal file 1件ずつ
- cwd-relative → realpath → repo-relative POSIX path に正規化
- expected issue contract の Allowed Paths matcher で allow された file のみ許可
- assets/ / LICENSES/ / .env* / .git/** など protected path は fail-closed

commit:
- 実行前に git diff --cached --name-status -z を読む
- staged set が Allowed Paths subset であることを確認
- rename/copy/delete/typechange を policy として明示
- out-of-scope staged file が1件でもあれば deny

3. bash -lc / env / command wrapper を unwrap して allow する経路がある

_extract_git_argv() は bash / sh / zsh -c|-lc、env VAR=...、command ... を再帰的に unwrap して rtk git として扱います。

しかも worktree_scope_guard.py では、この classifier が classify_bash(command) より前に呼ばれ、bounded_rtk_git.status == allow なら worktree 条件確認後に _allow() します。つまり、bash -lc 'rtk git add src/file.txt' のような shell wrapper が「正規 rtk git」として通る設計です。

これは #1241 の「partial-shell workaround や policy bypass を前提にしない」方向と逆です。Codex rules 側は compound command を別々に評価して危険部位を防ぐ設計ですが、repo hook 側で shell wrapper を先に unwrap すると、その安全前提を弱めます。([OpenAI Developers]1)

修正方針:
bash|sh|zsh -c|-lc、env ...、command ... は rtk_git_wrapped_command_denied のような reason_code で deny。少なくとも mutation 系では unwrap allow 禁止にしてください。


4. issue context が無い場合に rtk git mutation を allow している

worktree_scope_guard.py は bounded_rtk_git.status == allow の後、if not issue: _allow() しています。pure decision 側でも not issue なら allow / no_issue_no_scope です。

#1241 の目的は linked issue worktree 内 の publish path 復旧です。issue context が無い場合の mutation allow は、スコープ外の許可拡大です。

修正方針:
not issue は allow ではなく deny/prompt にしてください。reason は issue_context_required_for_rtk_git_mutation などでよいです。


5. HOOK_COMMAND_REPAIR_HINT_V1 が実際には schema を満たさない経路がある

docs では HOOK_COMMAND_REPAIR_HINT_V1 に blocked_command_class, reason_code, safe_action, suggested_command, forbidden_alternatives, verification_command, stop_condition の全フィールドが必要と定義されています。

しかし worktree_scope_guard._block_with_reason() は header 3 行 + formatter 8 行 = 11 行を作り、lines[:10] で出力するため、最後の stop_condition が落ちます。

local_main_branch_guard._emit_block_stderr() は HOOK_COMMAND_REPAIR_HINT_V1: の後に blocked_command_class, reason_code, verification_command, stop_condition だけを1行に押し込み、safe_action, suggested_command, forbidden_alternatives を schema field として出していません。

Claude Code は exit 2 の stderr を model に返し、PreToolUse を block します。だから stderr に修復ヒントを出す方向自体は妥当です。ただし machine-readable contract を名乗るなら、全 consumer で parse 可能な同一 schema にする必要があります。([Claude Platform Docs]5) Codex も PreToolUse で JSON stdout または exit code 2 + stderr による block reason を扱えます。([OpenAI Developers]2)

修正方針:
header を削るか、hint を1行 compact JSON/YAML にして、常に全フィールドを出す。テストは単なる substring ではなく、stderr から schema を parse して全 field presence を検証してください。


P1: 品質・回帰テスト不足

6. validator が存在検査・substring 検査に寄りすぎている

check-codex-agents.mjs は default.rules に allow pattern があること、policy/formatter ファイルが存在すること、docs に文字列があることを確認しています。実際の allow/deny matrix、schema parse、Allowed Paths subset、no-issue deny、wrapper deny は検証していません。

test_git_mutation_command_policy.py も positive 1件、broad add deny 1件、commit amend deny 1件、push main deny 1件だけです。重要な negative matrix が不足しています。

追加すべきテスト例:

deny:
- rtk git add ../x
- rtk git add .git/config
- rtk git add assets/foo.png
- rtk git add LICENSES/foo
- rtk git add -- path/outside/allowed
- bash -lc 'rtk git add docs/dev/hook-boundaries.md'
- env X=1 rtk git add docs/dev/hook-boundaries.md
- command rtk git add docs/dev/hook-boundaries.md
- rtk git commit -m msg when staged file is outside Allowed Paths
- rtk git commit -m msg when staged set is empty
- rtk git push origin HEAD:refs/heads/main
- rtk git push origin +HEAD:refs/heads/<branch>
- rtk git push origin HEAD:refs/tags/x
- no issue context + rtk git add/commit/push

allow:
- issue worktree + allowed file path + active issue branch + exact push refspec

関連 Issue / PR との整合

#1233 は Allowed Paths matcher v2 の実装で、.claude/skills/**/SKILL.md や docs/**/README.md のような mid-path ** を deterministic に扱うための Issue です。#1240 はその実装 PR で、allowed_paths_gate の false indeterminate 解消が目的でした。

#1244 は #1233/#1240 の publish 停止を再発防止する位置づけですが、現実装は Allowed Paths matcher そのものを rtk git add/commit の許可判定に接続していません。ここが最大の契約不一致です。

修正プロンプト案: Claude Code / Codex CLI 向け

PR #1244 / Issue #1241 を REQUEST_CHANGES として修正してください。目的は linked issue worktree 内の bounded `rtk git add/commit/push` publish path 復旧ですが、hook を authorization boundary と見なす実装は禁止です。

必須修正:

1. `.codex/rules/default.rules`
   - `rtk git add/commit/push` の blanket prefix allow を撤回する。
   - allow/prompt/forbidden の最終 decision を `codex execpolicy check` で固定する。
   - 危険形状の `match` / `not_match` を追加する。
   - rules は hook 依存の authorization claim を書かない。

2. `scripts/agent-guards/git_mutation_command_policy.py`
   - `bash|sh|zsh -c|-lc`, `env ...`, `command ...` wrapper は mutation では deny。
   - `rtk git add` は literal file pathspec のみ許可し、repo-relative POSIX path に正規化した上で Issue Allowed Paths matcher を通す。
   - `rtk git commit -m` は実行前に `git diff --cached --name-status -z` を検査し、staged set が Allowed Paths subset である場合だけ allow。
   - staged set empty / out-of-scope / protected path / rename-copy-delete policy 未定義は fail-closed。
   - `rtk git push` は `origin HEAD:refs/heads/<active_issue_branch>` だけ許可し、active issue id / branch / expected worktree の一致を検証する。

3. `.claude/hooks/worktree_scope_guard.py`
   - `not issue` で mutation allow しない。
   - classifier allow を `_allow()` する前に Allowed Paths / staged index / active branch / expected worktree をすべて検証する。
   - shell wrapper は `classify_bash` より前に allow しない。

4. `HOOK_COMMAND_REPAIR_HINT_V1`
   - local_main_branch_guard / worktree_scope_guard の全 block 経路で同一 schema を出す。
   - 必須 fields: blocked_command_class, reason_code, safe_action, suggested_command, forbidden_alternatives, verification_command, stop_condition。
   - 10行制限で field が落ちないようにする。
   - stderr を parse する regression test を追加する。

5. validator / tests
   - `node scripts/check-codex-agents.mjs --self-test` は存在確認ではなく policy matrix を検証する。
   - pytest に no-issue deny、wrapper deny、Allowed Paths subset、staged index contamination、push refspec matrix、schema parse を追加する。
   - #1233/#1240 の allowed paths matcher v2 を fixture として再利用し、publish path と allowed paths gate が分離しないことを確認する。

検証コマンド:
- uv run pytest scripts/agent-guards/tests/test_git_mutation_command_policy.py -q
- uv run pytest scripts/agent-guards/tests/test_local_main_branch_guard.py -q
- uv run pytest .claude/hooks/tests/test_worktree_scope_guard.py -q
- uv run pytest .claude/hooks/tests/test_issue1241_hook_repair_hint.py -q
- node scripts/check-codex-agents.mjs --self-test
- codex execpolicy check --rules .codex/rules/default.rules -- rtk git add docs/dev/hook-boundaries.md
- codex execpolicy check --rules .codex/rules/default.rules -- rtk git add .
- codex execpolicy check --rules .codex/rules/default.rules -- bash -lc 'rtk git add docs/dev/hook-boundaries.md'
- codex execpolicy check --rules .codex/rules/default.rules -- rtk git commit -m issue-1241-update
- codex execpolicy check --rules .codex/rules/default.rules -- rtk git push origin HEAD:refs/heads/worktree-issue-1241-codex-linked-issue-worktree-rtk-git-muta
- codex execpolicy check --rules .codex/rules/default.rules -- rtk git push origin main

停止条件:
- Allowed Paths 外の変更が必要
- root checkout mutating git を許可しないと解けない
- force push / reset / clean / checkout / switch の許可が必要
- hook を canonical authorization gate にしないと成立しない
- secret / token / 外部課金 / 権限昇格が必要

最小結論

PR #1244 は方向性は正しいですが、現状は “bounded publish path 復旧” ではなく “rtk git add/commit/push の広めの allow を hook で補う設計” になっています。特に rules blanket allow、Allowed Paths 未接続、staged index 未検査、shell wrapper unwrap、hint schema 欠落は merge 前に直すべきです。

@squne121
squne121 marked this pull request as ready for review June 30, 2026 10:38
@squne121
squne121 merged commit 1683e7b into main Jun 30, 2026
18 checks passed
@squne121
squne121 deleted the worktree-issue-1241-codex-linked-issue-worktree-rtk-git-muta branch June 30, 2026 10: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

Development

Successfully merging this pull request may close these issues.

1 participant