Skip to content

実装: Normalize branch-safe classifier telemetry for git fetch and git worktree prune #1075

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "PR #1072 の post-merge follow-up として、branch-safe maintenance commands を readonly と混同しない telemetry に分離し、pipeline/read-only classifier と consumer compatibility を fail-closed に固定する"
change_kind: code

Parent Issue

none

Parent Goal Ref

  • Goal: PR fix(codex): allow readonly bash pipelines (#1069) #1072 で導入した local root branch guard の allow/block 行動を維持しつつ、git fetch / git worktree prune を true read-only command とは別 reason_code で観測できるようにする
  • Desired Destination: git fetch と git worktree prune が guard telemetry では branch-safe maintenance として報告され、readonly pipeline classifier は display read-only command のみに限定され、既存 consumer が unknown reason_code 追加で壊れないことをテストで確認できる状態

Background

  • PR fix(codex): allow readonly bash pipelines (#1069) #1072 では local root branch guard に限定的な read-only pipeline classifier が入った。
  • その結果、git fetch や git worktree prune のような branch-safe maintenance command まで readonly_command と見える語彙が残っている。
  • owner comment #issuecomment-4760842373 では、単に文字列を追加するのではなく、reason_code 名・pipeline semantics・consumer compatibility まで contract で固定するよう求めている。
  • ワークアラウンドとして issue コメントだけで注記する案は採らない。guard telemetry の誤解を deterministic に解消する。

Source

Current Validated Scope

  • scripts/agent-guards/local_main_branch_guard.py の reason_code / classifier 実装を、display read-only と branch-safe maintenance で分離する
  • .claude/hooks/tests/test_local_main_branch_guard.py に git fetch / git worktree prune の reason_code 回帰、readonly parity 回帰、pipeline fail-closed 回帰を追加する
  • allow/block 行動は変えず、telemetry vocabulary・pipeline routing・consumer compatibility だけを tighten する

Remaining Parent Gaps

なし

Outcome

scripts/agent-guards/local_main_branch_guard.py と .claude/hooks/tests/test_local_main_branch_guard.py が更新され、git fetch と git worktree prune は readonly_command ではない branch-safe maintenance reason_code で allow される。display read-only command の既存表示は維持され、git fetch | head -n 1 と git worktree prune | head -n 1 は readonly pipeline に分類されず fail-closed のままで、追加した guard regression test が PASS している状態。

In Scope

  • scripts/agent-guards/local_main_branch_guard.py で display read-only classifier と branch-safe maintenance classifier を分離する
  • scripts/agent-guards/local_main_branch_guard.py で git fetch / git worktree prune を pipeline readonly allowlist へ再注入しないことを固定する
  • .claude/hooks/tests/test_local_main_branch_guard.py に branch-safe maintenance reason_code 回帰、readonly command 回帰、pipeline fail-closed 回帰、hook_flavor parity 回帰を追加する
  • LOCAL_MAIN_BRANCH_GUARD_RESULT_V1.reason_code の enum 追加が key set / value type 変更を伴わないことを、この 2 file 内のテストで確認する

Out of Scope

  • git fetch / git worktree prune の allow/block 判定そのものの変更
  • 他の guard / wrapper / validator の telemetry 定義の横断変更
  • read-only 以外の git / gh / pnpm command の再分類
  • tests/codex/** や docs への波及修正
  • pipeline allowlist の一般化や新しい maintenance pipeline 分類の導入

Implementation Constraints

  • git fetch / git worktree prune は display read-only command として扱わない
  • READONLY_PATTERNS = DISPLAY_READONLY_PATTERNS + ... のような再結合は禁止する
  • readonly pipeline classifier の先頭 segment は display read-only command のみ許可し、branch-safe maintenance command を混ぜない
  • branch-safe maintenance は通常の単独 command としてのみ allow し、pipeline では fail-closed を維持する
  • LOCAL_MAIN_BRANCH_GUARD_RESULT_V1.reason_code には新 enum value を追加してよいが、JSON key set / value type は変えない
  • 追加する pytest 名は verification から機械的に見つけられるように、branch_safe_maintenance / readonly_parity / pipeline_fail_closed / hook_flavor_parity / result_schema_compatibility のいずれかを test 名に含める

Schema / Consumer Compatibility

  • LOCAL_MAIN_BRANCH_GUARD_RESULT_V1.reason_code に branch-safe maintenance 用 enum value を追加する
  • 既存 readonly_command consumer が git fetch / git worktree prune を true read-only と仮定していないことを、少なくとも .claude/hooks/tests/test_local_main_branch_guard.py で fail-closed に確認する
  • hook_flavor="claude" と hook_flavor="codex" の両方で同じ reason_code を返すことを regression で固定する
  • unknown reason_code を扱えない別 consumer や追加 Allowed Paths が必要と判明した場合は stop する

Acceptance Criteria

  • AC1: scripts/agent-guards/local_main_branch_guard.py に display read-only と branch-safe maintenance を分離した distinct reason_code が定義されている
  • AC2: scripts/agent-guards/local_main_branch_guard.py で git fetch と git worktree prune は allow されるが readonly_command ではなく branch-safe maintenance reason_code を返す
  • AC3: scripts/agent-guards/local_main_branch_guard.py で git status、git diff --stat、git log、git worktree list は引き続き readonly_command を返す
  • AC4: readonly pipeline classifier は display read-only command のみに限定され、git fetch | head -n 1 と git worktree prune | head -n 1 は readonly pipeline に分類されない
  • AC5: .claude/hooks/tests/test_local_main_branch_guard.py に branch-safe maintenance 回帰、readonly parity 回帰、pipeline fail-closed 回帰、hook_flavor parity 回帰を表す named test が追加され、guard test file 全体が PASS する
  • AC6: LOCAL_MAIN_BRANCH_GUARD_RESULT_V1 の key set / value type は変えず、reason_code enum 追加だけであることを確認する named test が追加されている

Verification Commands

# AC1
$ rg -n 'branch_safe_maintenance' scripts/agent-guards/local_main_branch_guard.py
# AC2
$ rg -n 'branch_safe_maintenance' .claude/hooks/tests/test_local_main_branch_guard.py
# AC3
$ rg -n 'readonly_parity' .claude/hooks/tests/test_local_main_branch_guard.py
# AC4
$ rg -n 'pipeline_fail_closed' .claude/hooks/tests/test_local_main_branch_guard.py
# AC5
$ uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q -k "branch_safe_maintenance or readonly_parity or pipeline_fail_closed or hook_flavor_parity or result_schema_compatibility"
# AC6
$ rg -n 'result_schema_compatibility' .claude/hooks/tests/test_local_main_branch_guard.py

Allowed Paths

  • /home/squne/projects/LOOP_PROTOCOL/scripts/agent-guards/local_main_branch_guard.py
  • scripts/agent-guards/local_main_branch_guard.py
  • /home/squne/projects/LOOP_PROTOCOL/.claude/hooks/tests/test_local_main_branch_guard.py
  • .claude/hooks/tests/test_local_main_branch_guard.py

Runtime Verification Applicability

decision: not_applicable
reason: "本 Issue は deterministic な command classifier / reason_code / pytest regression の修正であり、画面操作や browser runtime 証跡を要求しない"

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • allow/block 行動の変更が必要になった場合
  • branch-safe maintenance の判定をこの 2 file だけでは consumer compatibility まで安全に固定できないと判明した場合
  • readonly pipeline classifier に branch-safe maintenance command を混ぜないと要件を満たせないと判明した場合

Required Skills

  • Python
  • pytest

Delivery Rule

  • 1 Issue = 1 PR
  • issue 専用 worktree で実装する
  • Draft PR を既定とする

Scope Delta

N/A

Activity

  1. added
    phase/implementation
    agent/implementer
    enhancement
    triage-required
    on Jun 21, 2026
  2. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    判定: #1075 は必要。ただし、このまま実装すると「理由コードだけ増やしたが分類境界は曖昧なまま」になるリスクが高いです。

    #1075 の出自は妥当です。Issue 本文は PR #1072 の post-merge follow-up として、git fetch / git worktree prune を readonly と誤読される telemetry から分離する目的を明示しています。#1075 自体には現時点でコメントはありません。

    PR #1072 側の議論を見ると、直接の発火点は comment #4760766472 です。そこでは、PR #1072 の pipeline classifier 方針自体は概ね妥当としつつ、git fetch / git worktree prune が REASON_READONLY に畳み込まれている点を「telemetry / policy naming として不正確」と指摘し、follow-up issue に分離することを求めています。

    重大ブロッカー

    1. branch_safe_command という名前がまだ広すぎる

    #1075 は branch_safe_command の追加を AC1 にしていますが、この語は広すぎます。git fetch と git worktree prune は「branch checkout を動かさない maintenance」ではありますが、「安全なコマンド」一般ではありません。将来 git restore、git checkout -- <path>、gh pr view などまで同じ語彙に吸い込まれると、また分類負債になります。

    より正確には、reason code は次のどちらかに寄せるべきです。

    REASON_BRANCH_SAFE_MAINTENANCE = "branch_safe_maintenance_command"
    # または
    REASON_NON_BRANCH_MUTATING_GIT = "non_branch_mutating_git_command"

    公式 Git docs 上でも、git fetch は remote-tracking branch や .git/FETCH_HEAD を更新し得るため pure read-only ではありません。さらにデフォルトで自動メンテナンスを走らせる挙動もあります。([Git]1) git worktree prune も $GIT_DIR/worktrees 配下の管理情報を削除するコマンドです。([Git]2)

    2. 現行コードは「分けたように見えて、最後に再結合している」

    現行 local_main_branch_guard.py は DISPLAY_READONLY_PATTERNS と NON_BRANCH_MUTATING_PATTERNS を分けていますが、直後に READONLY_PATTERNS = DISPLAY_READONLY_PATTERNS + NON_BRANCH_MUTATING_PATTERNS として再結合しています。つまり git fetch / git worktree prune は引き続き is_readonly_command() 経由で readonly_command になります。

    さらに evaluate() では、readonly pipeline classifier でも通常 readonly 判定でも reason_code=REASON_READONLY を返しています。#1075 の狙いはここに直撃しているので、単に constant を足すだけでは不足です。

    必要な修正は最低限これです。

    DISPLAY_READONLY_PATTERNS = [...]
    BRANCH_SAFE_MAINTENANCE_PATTERNS = [
        r"^git\s+fetch(\s|$)",
        r"^git\s+worktree\s+prune(\s|$)",
    ]
    
    def is_readonly_command(cmd: str) -> bool:
        return any(re.match(p, cmd.strip()) for p in DISPLAY_READONLY_PATTERNS)
    
    def is_branch_safe_maintenance_command(cmd: str) -> bool:
        return any(re.match(p, cmd.strip()) for p in BRANCH_SAFE_MAINTENANCE_PATTERNS)

    そのうえで evaluate() の通常コマンド分類は、is_readonly_command() と is_branch_safe_maintenance_command() を別々に評価して、別 reason_code を返すべきです。

    3. pipeline 内の git fetch | head をどう扱うかが未定義

    現行 classify_readonly_pipeline() は、先頭 segment に is_readonly_command(first_segment) を要求します。今の is_readonly_command() には git fetch が含まれているため、理屈上は git fetch | head -n 1 が readonly pipeline として許可され得ます。

    #1075 は git fetch / git worktree prune の allow/block 行動は変えない、としていますが、pipeline 経由の挙動を明記していません。ここは危険です。Git docs 上 fetch は副作用を持つため、出力を head に絞っただけで readonly と見なすべきではありません。(Git)

    推奨は次です。

    git fetch
      -> allow, reason_code=branch_safe_maintenance_command
    
    git worktree prune
      -> allow, reason_code=branch_safe_maintenance_command
    
    git fetch | head -n 1
    git worktree prune | head -n 1
      -> block, reason_code=unparseable_branch_mutation
    

    どうしても pipeline を許可するなら、readonly pipeline ではなく branch_safe_maintenance_pipeline のように別分類すべきです。ただし #1075 の範囲では、そこまで広げない方が安全です。

    4. 出力 schema / consumer inventory がない

    local_main_branch_guard.py の header では LOCAL_MAIN_BRANCH_GUARD_RESULT_V1 の reason_code が明示的な出力項目です。現在の reason code 一覧には readonly_command はありますが、branch-safe 系はありません。

    #1075 の Stop Conditions には「固定契約(キー集合・スキーマ・型定義)の変更が必要なら停止」とあります。しかし reason_code の enum 値追加は、キー集合ではなくても downstream consumer にとっては contract change です。最低限、Issue に次を追加してください。

    ### Schema / Consumer Compatibility
    
    - `LOCAL_MAIN_BRANCH_GUARD_RESULT_V1.reason_code` に新 enum value を追加する。
    - JSON key set / value type は変更しない。
    - 既存 consumer が unknown reason_code を fail-close する場合は Stop Condition とする。
    - 既存 `readonly_command` consumer が `git fetch` / `git worktree prune` を readonly と仮定していないことを確認する。

    5. .claude 側だけのテスト指定では Codex parity が弱い

    #1075 の Allowed Paths は scripts/agent-guards/local_main_branch_guard.py と .claude/hooks/tests/test_local_main_branch_guard.py に限定されています。 しかしこの shared core は Claude / Codex の両方で使われます。PR #1072 の Codex 側テストには readonly pipeline classifier の positive / negative matrix が既にあり、そこでは REASON_READONLY を前提にしています。

    #1075 の実装が通常コマンドの git fetch / git worktree prune だけを分離するなら、Codex test を変えなくても成立する可能性はあります。ただし hook_flavor="codex" でも同じ reason_code になることは、少なくとも .claude 側テスト内で hook_flavor parameterize して確認すべきです。

    修正後の Acceptance Criteria 案

    現在の AC は弱いです。rg -n "branch_safe_command" と test name の存在だけでは、誤実装でも通ります。

    差し替えるなら、次の AC が必要です。

    - [ ] AC1: `readonly_command` と `branch_safe_maintenance_command` が別 reason_code として定義されている。
    - [ ] AC2: `DISPLAY_READONLY_PATTERNS` と `BRANCH_SAFE_MAINTENANCE_PATTERNS` が再結合されていない。
    - [ ] AC3: `git fetch` は allow されるが `reason_code=branch_safe_maintenance_command` になる。
    - [ ] AC4: `git worktree prune` は allow されるが `reason_code=branch_safe_maintenance_command` になる。
    - [ ] AC5: `git status`, `git diff --stat`, `git log`, `git worktree list`, `rg -n ...` は引き続き `readonly_command` になる。
    - [ ] AC6: drifted root 上でも `git fetch` / `git worktree prune` は allow される場合、`readonly_command` ではなく branch-safe maintenance reason を返す。
    - [ ] AC7: `git -c ... fetch`, `git --git-dir=.git fetch`, `git --work-tree=. fetch` は従来通り fail-closed になる。
    - [ ] AC8: `git fetch | head -n 1` と `git worktree prune | head -n 1` の扱いが明示され、少なくとも readonly pipeline には分類されない。
    - [ ] AC9: `LOCAL_MAIN_BRANCH_GUARD_RESULT_V1` の key set / value type は変更せず、reason_code enum 追加だけであることを確認する。

    実装方針

    evaluate() の分類順は以下が安全です。

    # after compound/wrapped fail-closed and git global option normalization
    
    if is_readonly_command(normalized_cmd):
        return allow(REASON_READONLY)
    
    if is_branch_safe_maintenance_command(normalized_cmd):
        return allow(REASON_BRANCH_SAFE_MAINTENANCE)

    ただし pipeline classifier は DISPLAY_READONLY_PATTERNS のみを参照させるべきです。Python shlex は shell 風の字句解析には使えますが、公式 docs でも「完全な shell parser ではないので独自 error checks が必要」とされています。現行実装の fail-closed 方向は正しいため、maintenance command を pipeline allow に混ぜない方がよいです。([Python documentation]3)

    Codex hook については、OpenAI docs 上でも PreToolUse は guardrail であり完全な enforcement boundary ではなく、単純な shell call 以外の経路は捕捉し切れません。また複数 matching hooks は同時に走るため、1 hook の分類名変更で全体の安全性が上がるわけではありません。今回の修正は「安全境界の強化」ではなく「telemetry の誤語彙修正」と位置づけるべきです。([OpenAI Developers]4) ([OpenAI Developers]4)

    結論

    #1075 は実装すべきです。ただし、現 Issue のままだと「branch_safe_command という文字列を足しただけ」で終わる危険があります。

    実装前に、Issue 本文へ次を追記するのが妥当です。

    Implementation Constraints:
    - `git fetch` / `git worktree prune` は display readonly ではない。
    - `READONLY_PATTERNS = DISPLAY_READONLY_PATTERNS + ...` の再結合は禁止。
    - pipeline classifier の先頭 segment は display readonly のみ許可する。
    - branch-safe maintenance は通常単独コマンドとしてのみ allow する。
    - reason_code enum 追加による consumer 影響を確認する。

    この条件を満たすなら、#1075 は PR #1072 の設計負債を小さく閉じる良い follow-up です。逆にここを曖昧にしたまま実装すると、次は「branch-safe とは何か」をめぐる再分類 Issue が必要になります。

  3. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-21T04:14:07Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1075
      body_sha256: "sha256:2536dc1e713913bd6da0c18f0a28ee1ebb0474b01175f84efcf3ae84d9e64f19"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  4. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    Issue 本文更新

    • AC5 の Verification Commands を、追加予定のテスト名に絞った pytest -k へ変更しました
    • 全体 pytest だと現状ベースラインで通ってしまうため、未実装なら落ちる形に調整しています

    確認結果

    • guard-issue-body.py: pass
    • issue_contract_hygiene_autofix.py: no change
    • contract_readiness_check.py: go
    • ready tuple: pass
  5. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    PR #1079 が 2026-06-21 に main へマージされ、issue contract の Outcome/AC を満たしたため close します。post-merge-cleanup として linked issue を手動で整合させます。

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/implementationtriage-required

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions