Skip to content

実装: CodexCLI read-only Bash hook noise guard を追加する #1069

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "#1047"
goal_ref: "CodexCLI の read-only Bash に対する false block と hook inventory drift を抑制し、root guard の fail-closed 性を維持したまま read-only 調査ノイズを deterministic に減らす"
change_kind: workflow

Parent Issue

#1047

Parent Goal Ref

  • Goal: impl-review-loop / post-merge-cleanup の周辺制御で発生するトークン浪費とノイズを deterministic script に集約して根絶する
  • Desired Destination: hook / preflight / worktree 周辺制御が必要なときだけ発火し、read-only 調査では main context に不要ノイズを混入させない状態

Current Validated Scope

  • .codex/hooks.json の CodexCLI hook matcher / event 配線を、validator と整合する inventory 前提で維持・調整する
  • scripts/agent-guards/local_main_branch_guard.py に、fail-closed を維持した限定的 read-only pipeline classifier を追加する
  • scripts/session-recording/validate-codex-hooks.mjs の Bash hook inventory 検証を現行 .codex/hooks.json と整合させる
  • scripts/session-recording/codex-hook-adapter.mjs の現行 no-decision/no-manifest 挙動を回帰テストで固定する
  • tests/codex/test_local_main_branch_guard.py と tests/session_recording/codex/test_hook_adapter.py に allow/deny matrix と validator 回帰テストを追加する
  • PR body publish preflight で英語固定 required heading と日本語比率 checker が衝突する blocker を、.claude/skills/create-issue/scripts/validate_japanese_content.py と .claude/skills/create-issue/scripts/prose_boundary_policy.py の整合で解消する
  • create-issue script 側の Python test 期待値も更新し、heading policy 追加分が CI の python-test によって fail-close されないようにする

Remaining Parent Gaps

Outcome

CodexCLI の read-only Bash に対して、local_main_branch_guard.py が blanket allow ではなく限定的な read-only pipeline classifier で rg ... | head ... などの代表調査コマンドを許可しつつ、混合 command / redirection / wrapper command には fail-closed を維持する。加えて .codex/hooks.json と scripts/session-recording/validate-codex-hooks.mjs の inventory drift が解消され、codex-hook-adapter.mjs の read-only PreToolUse / PermissionRequest が stdout 空かつ manifest 非生成であることを回帰テストで固定し、uv run pytest tests/codex/test_local_main_branch_guard.py tests/session_recording/codex/test_hook_adapter.py -q が PASS している状態。

同時に、open-pr が要求する英語固定 required heading(Summary / Checks / Schema Change Applicability / Schema Consumer Inventory / Safety Claim Matrix / Notes)を日本語比率 checker が prose block として誤検出しないようにし、同 branch から Draft PR を fail-closed ではなく正常起票できる状態にする。

In Scope

  • .codex/hooks.json の PreToolUse ^Bash$ / PermissionRequest ^Bash$ matcher inventory の整合維持
  • scripts/agent-guards/local_main_branch_guard.py に限定的 read-only pipeline classifier を追加すること
  • scripts/session-recording/validate-codex-hooks.mjs を現行 .codex/hooks.json inventory に合わせて更新すること
  • scripts/session-recording/codex-hook-adapter.mjs の read-only PreToolUse / PermissionRequest no-decision 挙動を regression で固定すること
  • tests/codex/test_local_main_branch_guard.py に allow/deny matrix を追加すること
  • tests/session_recording/codex/test_hook_adapter.py に hook validator 整合確認と read-only no-output/no-manifest 回帰を追加すること
  • .claude/skills/create-issue/scripts/validate_japanese_content.py と .claude/skills/create-issue/scripts/prose_boundary_policy.py を更新し、open-pr required heading を canonical heading として扱えるようにすること
  • .claude/skills/create-issue/scripts/tests/test_heading_policy.py を更新し、許可 heading 集合の near-equivalence test を新 heading 群に追従させること

Out of Scope

  • .claude/hooks/** の Claude Code hook 変更
  • root direct mutation deny の再設計全体
  • session manifest schema の変更
  • Codex CLI / OpenAI product 側の statusMessage 表示仕様変更
  • hook trust / review UI の変更
  • open-pr の required section 名自体の変更
  • create-issue Python test suite 全体の再設計

Acceptance Criteria

  • AC1: scripts/agent-guards/local_main_branch_guard.py が | を blanket allow せず、限定的な read-only pipeline classifier を経由して許可/拒否を分岐する
  • AC2: tests/codex/test_local_main_branch_guard.py に、rg -n "TODO" README.md | head -n 20、git status --short | head -n 20、git diff --stat | head -n 20 を許可する positive fixture が追加される
  • AC3: tests/codex/test_local_main_branch_guard.py に、git status | git switch issue-123、rg TODO . | xargs rm -f、rg TODO . && git switch issue-123、rg TODO . > out.txt、bash -lc 'git switch issue-123'、$(git switch issue-123) を拒否する negative fixture が追加される
  • AC4: fail-closed を維持し、許可対象外の compound / wrapped / redirected command は引き続き unparseable_branch_mutation もしくは同等の deny lane で遮断されることをテストで確認する
  • AC5: scripts/session-recording/codex-hook-adapter.mjs の read-only PreToolUse / PermissionRequest は、deny JSON を返さず stdout 空かつ tmp/session-manifests/codex/pretooluse / permissionrequest を生成しないことを回帰テストで確認する
  • AC6: scripts/session-recording/codex-hook-adapter.mjs の secret / forbidden_path / remote_write deny shape は既存通り維持される
  • AC7: scripts/session-recording/validate-codex-hooks.mjs が .codex/hooks.json の PreToolUse ^Bash$ で local_main_branch_guard.sh、check-codex-agents.mjs --hook-pretool、session-recording-composite.mjs --event PreToolUse を各 1 回、PermissionRequest ^Bash$ で local_main_branch_guard.sh と session-recording-composite.mjs --event PermissionRequest を各 1 回要求する
  • AC8: tests/session_recording/codex/test_hook_adapter.py が hook validator を直接実行して returncode 0 を確認し、validator と .codex/hooks.json の inventory drift 回帰を防ぐ
  • AC9: uv run pytest tests/codex/test_local_main_branch_guard.py tests/session_recording/codex/test_hook_adapter.py -q が PASS する
  • AC10: open-pr が要求する英語固定 required heading を含む PR body でも、.claude/skills/create-issue/scripts/validate_japanese_content.py --threshold 0.1 が heading だけを理由に fail しない
  • AC11: .claude/skills/create-issue/scripts/tests/test_heading_policy.py が追加した canonical heading 群を許可集合として扱い、CI python-test が near-equivalence で fail しない
  • AC12: worktree-issue-1069-readonly-bash-hook-noise-guard から Draft PR を正常起票でき、issue #1069 に対する impl-review-loop の Step 2 / Step 4 へ進める

Verification Commands

# AC1
$ rg -q "readonly pipeline" tests/codex/test_local_main_branch_guard.py
# AC2
$ rg -q "test_readonly_pipeline_rg_head" tests/codex/test_local_main_branch_guard.py
# AC3
$ rg -q "test_readonly_pipeline_rejects_xargs_rm" tests/codex/test_local_main_branch_guard.py
# AC4
$ rg -q "unparseable_branch_mutation" tests/codex/test_local_main_branch_guard.py
# AC5
$ rg -q "stdout empty" tests/session_recording/codex/test_hook_adapter.py
# AC6
# baseline-expect: pass
$ rg -q "secret_boundary_violation" tests/session_recording/codex/test_hook_adapter.py
# AC7
$ rg -q "local_main_branch_guard.sh" scripts/session-recording/validate-codex-hooks.mjs
# AC8
# baseline-expect: pass
$ rg -q "test_hook_config_positive_fixture" tests/session_recording/codex/test_hook_adapter.py
# AC9
# preflight-scope: pr_review_only
$ uv run pytest tests/codex/test_local_main_branch_guard.py tests/session_recording/codex/test_hook_adapter.py -q
# AC10
$ rg -q '"Summary"' .claude/skills/create-issue/scripts/prose_boundary_policy.py
# AC11
$ rg -q 'Schema Consumer Inventory' .claude/skills/create-issue/scripts/tests/test_heading_policy.py
# AC12
# preflight-scope: pr_review_only
$ gh pr view 1072 --json url

Allowed Paths

  • .codex/hooks.json
  • .codex/hooks/local_main_branch_guard.sh
  • .codex/hooks/session-recording-composite.mjs
  • scripts/agent-guards/local_main_branch_guard.py
  • scripts/session-recording/codex-hook-adapter.mjs
  • scripts/session-recording/validate-codex-hooks.mjs
  • tests/codex/test_local_main_branch_guard.py
  • tests/session_recording/codex/test_hook_adapter.py
  • .claude/skills/create-issue/scripts/validate_japanese_content.py
  • .claude/skills/create-issue/scripts/prose_boundary_policy.py
  • .claude/skills/create-issue/scripts/tests/test_heading_policy.py

Runtime Verification Applicability

decision: not_applicable

理由: 本 Issue は deterministic な command classification / hook inventory validation / unit test 回帰の修正であり、画面操作や browser runtime 証跡を要求しない。

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • fail-closed を維持したまま read-only pipeline classifier を安全に定義できないと判明した場合

Required Skills

  • Python 3(command classification / pytest fixture)
  • Node.js / ESM(Codex hook adapter / hook validator)

Scope Delta(任意)

  • scripts/session-recording/validate-codex-hooks.mjs を明示的に scope へ追加
  • codex-hook-adapter.mjs は新規挙動追加よりも現行 no-decision/no-manifest 挙動の regression 固定へ焦点を修正
  • PR body publish preflight blocker が validate_japanese_content.py と heading policy の不整合にあると判明したため、同 issue 内で canonical heading inventory の整合まで扱う

Activity

  1. added
    phase/implementation
    agent/implementer
    enhancement
    on Jun 20, 2026
  2. squne121 commented on Jun 20, 2026

    @squne121
    OwnerAuthor

    判定

    現状の #1069 は「必要だが、このまま実装開始は危険」です。
    狙いは妥当です。rg ... | head ... のような読取調査コマンドが unparseable_branch_mutation で止まるのは、Codex 運用上かなり邪魔です。ただし、現在の Issue 定義は受け入れ条件が粗く、実装者が単純な正規表現 allow に逃げると、git status | git switch issue-* や rg ... | xargs rm のような混合コマンドを通す退行が起きます。

    重大ブロッカー

    1. 現行 guard は pipe を必ず fail-closed するので、#1069 の代表ケースは今の構造では通らない

    local_main_branch_guard.py の read-only allowlist は現状ほぼ Git コマンドだけで、rg や head は入っていません。さらに |, &&, ;, backtick, $() を含むと is_compound_or_wrapped() が true になり、read-only 判定より前に REASON_UNPARSEABLE で block されます。

    したがって、#1069 の実装は単に allowlist に rg を足すだけでは不十分です。pipeline 全体を「各 segment が安全な読取専用か」で分類する必要があります。

    2. .codex/hooks.json と validate-codex-hooks.mjs がすでに矛盾している

    現行 .codex/hooks.json の PreToolUse ^Bash$ には local_main_branch_guard.sh、check-codex-agents.mjs、session-recording-composite.mjs の 3 hook が登録されています。PermissionRequest ^Bash$ にも local_main_branch_guard.sh と session-recording hook が登録されています。

    一方で scripts/session-recording/validate-codex-hooks.mjs は PreToolUse ^Bash$ を 2 hook 固定、PermissionRequest ^Bash$ を 1 hook 固定として検証しており、そこに local_main_branch_guard.sh が入る余地がありません。

    さらに tests/session_recording/codex/test_hook_adapter.py はその validator を直接実行して returncode 0 を要求しています。
    つまり、#1069 の Allowed Paths に scripts/session-recording/validate-codex-hooks.mjs を含めないと、AC6/AC7 は false green か CI failure のどちらかになります。

    3. codex-hook-adapter.mjs の read-only skip は、Issue の焦点がずれている可能性が高い

    codex-hook-adapter.mjs には isReadonlyInvestigation() があるものの、現状は placeholder で常に false です。
    ただし実際には、deny 条件に該当しない PreToolUse / PermissionRequest は最後に return null となり、main() 側でもそのまま stdout なしで return しています。manifest 生成は Stop / SubagentStop のときだけ実行されます。

    したがって、AC4/AC5 は「read-only を skip する実装を入れる」ではなく、代表 read-only payload で stdout が空、deny JSON が出ない、tmp/session-manifests/codex/pretooluse や permissionrequest が作られないことを固定する regression test にすべきです。

    4. Codex hook の実行モデル上、「片方の hook が no-op すれば他の hook が黙る」は成り立たない

    OpenAI 公式 docs では、複数の matching hook はすべて実行され、同一 event の複数 command hook は並行起動されます。さらに repo-local hook は信頼済み project layer のときだけロードされます。([OpenAI Developers]1)
    現在の PreToolUse ^Bash$ には statusMessage 付き hook が 3 本あり、session-recording hook にも Checking Codex session-recording PreToolUse guard が設定されています。

    つまり「noise」の発生源が local_main_branch_guard の stderr なのか、Codex の statusMessage 表示なのか、session-recording hook の起動自体なのかを Issue に明記しないと、実装しても体感ノイズが残ります。

    5. READONLY_PATTERNS という名前がすでに不正確

    現行 READONLY_PATTERNS には git fetch と git worktree prune が含まれています。これらは「branch drift を直接起こさない」可能性はありますが、純粋な read-only ではありません。
    #1069 では「read-only Bash hook noise guard」と呼んでいるため、ここを曖昧にしたまま拡張すると、将来 fetch/prune まで read-only として扱う設計負債になります。

    分類名は少なくとも次の 2 段に分けるべきです。

    • display_readonly: rg, grep, cat, head, git status, git diff, git log など副作用なし前提
    • non_branch_mutating_git: git fetch, git worktree prune など branch drift ではないが副作用はある操作

    WEB調査による設計補正

    Codex hooks は公式 docs 上でも「guardrail」であり、完全な enforcement boundary ではありません。PreToolUse は Bash / apply_patch / MCP tool を intercept できますが、すべての shell call を捕捉するわけではなく、非 shell・非 MCP の経路も対象外です。([OpenAI Developers]1)

    また、PreToolUse では deny する場合に hook-specific JSON を返すか、exit code 2 と stderr を使えます。一方で continue, stopReason, suppressOutput は PreToolUse / PermissionRequest では未サポートで、返すと hook run が failed 扱いになります。([OpenAI Developers]1) ([OpenAI Developers]1)
    PermissionRequest は approval が必要なときだけ動き、複数 hook の decision がある場合は deny が勝ち、何も decision しなければ通常の approval flow に戻ります。([OpenAI Developers]1)

    このため、#1069 の品質基準は「read-only を allow する」だけでなく、unsupported output shape を返さない、deny/no-decision の event-specific shape を崩さない、hook が信頼・並行実行される前提でテストすることが必要です。

    推奨する実装方針

    local_main_branch_guard.py

    is_compound_or_wrapped() の前に、限定的な classify_readonly_pipeline(cmd) を入れるのが安全です。単純に _SHELL_METACHAR_RE から | を外すのは危険です。

    許可してよい形は、例えば次だけです。

    <display-readonly-command> | head -n <N>
    <display-readonly-command> | tail -n <N>
    <display-readonly-command> | wc -l
    

    display-readonly-command の候補は、最初は狭くしてください。

    rg, grep, cat, head, tail, wc
    git status, git diff, git log, git show, git branch --show-current, git rev-parse, git ls-files
    

    逆に以下は明示 deny fixture に入れるべきです。

    rg foo . | xargs rm -f
    git status | git switch issue-123
    rg foo . && git switch issue-123
    rg foo . > out.txt
    bash -lc 'git switch issue-123'
    sh -c 'git switch issue-123'
    $(git switch issue-123)
    `git switch issue-123`
    FOO=bar git switch issue-123
    

    Python の shlex は shell 風の字句解析には使えますが、公式 docs でも「full parser for shells ではない」「返った token が shell として有効とは限らず、自前の error checks が必要」と説明されています。pipeline allow を実装するなら、shlex.shlex(posix=True, punctuation_chars=True) で token 化し、その後に独自の構文チェックを必ず入れるべきです。([Python documentation]2)

    codex-hook-adapter.mjs

    ここは大改修不要です。むしろ、現行の deny 優先順位を崩さないことが重要です。forbidden_path、env dump、secret、remote write は read-only 判定より前に評価されています。

    追加するなら、isReadonlyInvestigation() を実装してもよいですが、機能的には return null と同じなので、テストでは次を固定してください。

    • read-only PreToolUse は stdout 空
    • read-only PermissionRequest は stdout 空
    • tmp/session-manifests/codex/pretooluse と permissionrequest は作られない
    • printenv, gh secret list, cat .env, git push は従来通り deny

    validate-codex-hooks.mjs

    ここは #1069 の scope に追加必須です。

    修正方針は、exact shape 固定ではなく、次の不変条件にするべきです。

    PreToolUse ^Bash$:
      local_main_branch_guard.sh が exactly once
      check-codex-agents.mjs --hook-pretool が exactly once
      session-recording-composite.mjs --event PreToolUse が exactly once
    
    PermissionRequest ^Bash$:
      local_main_branch_guard.sh が exactly once
      session-recording-composite.mjs --event PermissionRequest が exactly once
    

    現行の tests/codex/test_local_main_branch_guard.py は git switch / git checkout / gh pr checkout の deny と、hooks.json に local guard が存在することしか見ていません。read-only compound の positive/negative matrix が不足しています。

    Issue 本文への追記案

    ### Scope追加
    
    - scripts/session-recording/validate-codex-hooks.mjs
      - 現在の .codex/hooks.json は PreToolUse/PermissionRequest ^Bash$ に local_main_branch_guard.sh を含むが、validator 側は旧 inventory を exact 固定しているため、AC6/AC7 の信頼性維持に必要。
    
    ### 実装制約
    
    - `|` を blanket allow しない。
    - `;`, `&&`, `||`, backtick, `$()`, redirection, `xargs`, `tee`, `bash -c`, `sh -c`, `python -c`, `node -e` は fail-closed。
    - 許可する pipeline は、全 segment が display-readonly allowlist に入る場合だけ。
    - `git fetch` / `git worktree prune` は display-readonly ではなく non_branch_mutating_git として分類名を分ける。
    
    ### 追加テスト
    
    Allow:
    - `rg -n "TODO" README.md | head -n 20`
    - `git status --short | head -n 20`
    - `git diff --stat | head -n 20`
    
    Deny:
    - `git status | git switch issue-123`
    - `rg TODO . | xargs rm -f`
    - `rg TODO . && git switch issue-123`
    - `rg TODO . > out.txt`
    - `bash -lc 'git switch issue-123'`
    - `$(git switch issue-123)`
    
    Adapter regression:
    - read-only PreToolUse / PermissionRequest は stdout 空
    - pretooluse / permissionrequest manifest directory が作られない
    - secret / forbidden_path / remote_write deny は既存通り

    結論

    #1069 は実施すべきですが、現 Issue のままだと「読取コマンドを通す」ために guard を緩めすぎる可能性が高いです。最小修正ではなく、pipeline classifier、validator inventory 更新、adapter no-output regression、deny matrix まで入れて初めて安全です。特に validate-codex-hooks.mjs を scope に入れない実装は、CI と現行 hook 構成の整合性を壊します。

  3. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-21T01:45:06Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1069
      body_sha256: "sha256:d5d297069968e4dbdb98ac762687e968a4e49ed5458ba57644bf44402b19010c"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent/implementerenhancementphase/implementation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions