Skip to content

fix(guards): split branch-safe maintenance telemetry (#1075) - #1079

Merged
squne121 merged 5 commits into
mainfrom
worktree-issue-1075-normalize-branch-safe-classifier-telemet
Jun 21, 2026
Merged

squne121 merged 5 commits into
mainfrom
worktree-issue-1075-normalize-branch-safe-classifier-telemet

Conversation

@squne121

@squne121 squne121 commented Jun 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • local_main_branch_guard.py で display read-only と branch-safe maintenance を別 classifier に分離し、git fetch / git worktree prune が readonly_command ではなく branch_safe_maintenance_command を返すようにしました。
  • readonly pipeline classifier は display read-only のみを対象に維持し、git fetch | head -n 1 と git worktree prune | head -n 1 が fail-closed のままになる回帰を追加しました。
  • .claude/hooks 側に hook flavor parity、readonly parity、schema compatibility に加えて fetch variants 意図固定と drifted root telemetry の named regression test を追加し、既存 Codex parity test も追加変更なしで通ることを確認しました。

受け入れ条件の達成状況

  • AC1: scripts/agent-guards/local_main_branch_guard.py に distinct reason code として branch_safe_maintenance_command を追加しました。
  • AC2: git fetch と git worktree prune は allow のまま branch_safe_maintenance_command を返します。
  • AC3: git status、git diff --stat、git log、git worktree list は引き続き readonly_command を返します。
  • AC4: git fetch | head -n 1 と git worktree prune | head -n 1 は readonly pipeline に分類されず fail-closed のままです。
  • AC5: branch-safe maintenance / readonly parity / pipeline fail-closed / hook flavor parity に加え、fetch variants 意図固定と drifted root telemetry の named regression test を追加し、.claude 側 full pytest も PASS しました。
  • AC6: result schema key set / value type が不変で、reason code enum 追加のみであることを回帰で固定しました。

検証コマンド結果

以下のコマンドで issue contract の VC と追加回帰確認を実施し、すべて成功しました。

  • rg -n 'branch_safe_maintenance' scripts/agent-guards/local_main_branch_guard.py
  • rg -n 'branch_safe_maintenance' .claude/hooks/tests/test_local_main_branch_guard.py
  • rg -n 'readonly_parity' .claude/hooks/tests/test_local_main_branch_guard.py
  • rg -n 'pipeline_fail_closed' .claude/hooks/tests/test_local_main_branch_guard.py
  • 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"
    結果: 24 passed, 113 deselected
  • rg -n 'result_schema_compatibility' .claude/hooks/tests/test_local_main_branch_guard.py
  • uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
    結果: 137 passed
  • uv run pytest tests/codex/test_local_main_branch_guard.py -q
    結果: 22 passed in 0.44s
  • python3 -m py_compile scripts/agent-guards/local_main_branch_guard.py

Allowed Paths 遵守

  • 変更ファイルは scripts/agent-guards/local_main_branch_guard.py と .claude/hooks/tests/test_local_main_branch_guard.py の 2 件のみです。
  • tests/codex/test_local_main_branch_guard.py は read-only 確認対象として参照しましたが、変更していません。
  • Issue contract の Allowed Paths 逸脱はありません。

Checks

以下は PR テンプレ必須の checks 要約で、実行済みコマンドをそのまま再掲しています。

  • rg -n 'branch_safe_maintenance' scripts/agent-guards/local_main_branch_guard.py
  • rg -n 'branch_safe_maintenance' .claude/hooks/tests/test_local_main_branch_guard.py
  • rg -n 'readonly_parity' .claude/hooks/tests/test_local_main_branch_guard.py
  • rg -n 'pipeline_fail_closed' .claude/hooks/tests/test_local_main_branch_guard.py
  • 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"
  • rg -n 'result_schema_compatibility' .claude/hooks/tests/test_local_main_branch_guard.py
  • uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
  • uv run pytest tests/codex/test_local_main_branch_guard.py -q
  • python3 -m py_compile scripts/agent-guards/local_main_branch_guard.py

Schema Change Applicability

この PR は shared guard の telemetry enum を additive に拡張するため、schema change として扱います。

  • decision: schema_change
  • reason: LOCAL_MAIN_BRANCH_GUARD_RESULT_V1.reason_code に branch_safe_maintenance_command を追加し、shared guard の machine-readable telemetry enum を拡張したためです。

Schema Consumer Inventory

この PR は key set や value type を変えず、reason_code enum に additive な値を 1 つ追加します。既存 consumer が readonly_command と混同しないことを regression で固定し、未変更 consumer については影響有無を確認しました。

変更対象 schema: LOCAL_MAIN_BRANCH_GUARD_RESULT_V1.reason_code

before/after 差分:

# before
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
    - detached_or_unknown_root
    - unparseable_branch_mutation
    - inline_env_override_not_allowed

# after
LOCAL_MAIN_BRANCH_GUARD_RESULT_V1:
  status: allow | block
  reason_code:
    - not_local_root_context
    - readonly_command
    - branch_safe_maintenance_command
    - recovery_to_default_branch
    - local_root_branch_drift
    - already_drifted_root
    - detached_or_unknown_root
    - unparseable_branch_mutation
    - inline_env_override_not_allowed

Consumer 列挙(hidden path を含む rg コマンド例と結果):

rg -uu -n 'LOCAL_MAIN_BRANCH_GUARD_RESULT_V1|readonly_command|branch_safe_maintenance_command' scripts tests .claude .codex
scripts/agent-guards/local_main_branch_guard.py:17:Output schema (LOCAL_MAIN_BRANCH_GUARD_RESULT_V1, self-test / CLI mode):
scripts/agent-guards/local_main_branch_guard.py:41:REASON_READONLY = "readonly_command"
scripts/agent-guards/local_main_branch_guard.py:42:REASON_BRANCH_SAFE_MAINTENANCE = "branch_safe_maintenance_command"
scripts/agent-guards/local_main_branch_guard.py:410:def is_readonly_command(cmd: str) -> bool:
scripts/agent-guards/local_main_branch_guard.py:419:def is_branch_safe_maintenance_command(cmd: str) -> bool:
.claude/hooks/tests/test_local_main_branch_guard.py:159:        assert is_readonly_command(command) is False
.claude/hooks/tests/test_local_main_branch_guard.py:174:        assert is_readonly_command(command) is False
tests/codex/test_local_main_branch_guard.py:181:        """GIVEN readonly pipeline WHEN rg is piped to head THEN allow readonly_command."""

Consumer 更新状況:

Consumer ファイル 更新有無 備考
scripts/agent-guards/local_main_branch_guard.py 更新済み enum 追加、classifier 分離、readonly pipeline 非再注入を実装
.claude/hooks/tests/test_local_main_branch_guard.py 更新済み branch-safe telemetry / parity / schema compatibility 回帰に加え、fetch variants 意図固定と drifted root telemetry 回帰を追加
tests/codex/test_local_main_branch_guard.py 不要(理由) git fetch / git worktree prune の reason_code を固定しておらず、既存 parity test は変更なしで PASS

Compatibility Decision / 互換性判断

互換性判断は次のとおりで、既存 consumer を壊さない additive change に限定しています。

  • change_type: additive (enum 値の追加のみ)
  • compatibility: backward_compatible (既存 key set / value type は不変)
  • migration_required: no (移行作業は不要)
  • migration_or_followup: N/A (追加の migration issue は不要)
  • reason: 変更は reason_code enum の追加のみで、LOCAL_MAIN_BRANCH_GUARD_RESULT_V1 の key set / value type は不変です。既存 readonly_command consumer を誤って壊さないことは .claude/hooks/tests/test_local_main_branch_guard.py の schema compatibility 回帰で確認しています。

Safety Claim Matrix

この PR は安全境界の設定変更ではなく telemetry 分類の修正に限定されるため、主張は 1 行に絞って記載します。

Claim Implemented? Not controlled Evidence Follow-up
permission / sandbox 設定は変更せず、branch-safe telemetry の分類だけを更新した yes uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q、uv run pytest tests/codex/test_local_main_branch_guard.py -q N/A

Notes

  • Related issue: Closes #1075 として、この follow-up を自動 close 対象にします。
  • MVP scope: workflow / guard telemetry follow-up です。ゲーム本体の MVP scope 変更はありません。
  • AI / NotebookLM usage: Codex を使用しました。issue-reviewer / issue-author / implementation-worker のサブエージェントを補助的に使い、最終 diff・検証・PR 本文は親スレッドで統合しました。

local_main_branch_guard の readonly classifier と branch-safe maintenance telemetry を分離し、git fetch / git worktree prune が readonly_command に畳み込まれないようにする。\n\n合わせて hook_flavor parity、pipeline fail-closed、result schema compatibility の targeted regression を追加する。
@github-actions

github-actions Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

✅ verified live assets: https://squne121.github.io/loop-protocol/pr-1079/

The preview will be removed when this PR is closed.

@squne121

Copy link
Copy Markdown
Owner Author

判定

結論: Issue #1075 の契約上は APPROVE 寄り。ただし、merge 前に 2 点だけ追補すると品質が上がります。
REQUEST_CHANGES 相当の決定的ブロッカーは見つけていません。#1079 は Draft で、#1075 を閉じる目的の PR です。#1075 は「git fetch / git worktree prune を readonly_command から分離し、pipeline は fail-closed、schema key/value type は不変」という狭い契約です。

CI は head SHA 191bdcb... に対して ci、check-hook-integrity、agent-session-manifest、deploy-pages、Check Japanese Content が success です。
PR コメントは現時点では preview URL の bot コメントのみで、実質的なレビュー議論はまだありません。

良い点

PR は anchor comment の主要求をかなり正確に満たしています。DISPLAY_READONLY_PATTERNS と BRANCH_SAFE_MAINTENANCE_PATTERNS を分離し、READONLY_PATTERNS = DISPLAY_READONLY_PATTERNS + ... の再結合を削除しています。

実装後の is_readonly_command() は display read-only のみを参照し、is_branch_safe_maintenance_command() は git fetch / git worktree prune 用の別 classifier になっています。
pipeline classifier も先頭 segment に is_readonly_command(first_segment) を要求するため、branch-safe maintenance は readonly pipeline に混ざりません。

評価順も妥当です。readonly allow の後に branch-safe maintenance allow を置き、REASON_BRANCH_SAFE_MAINTENANCE を返すため、readonly_command への畳み込みは解消されています。

テストも契約の中心は押さえています。git fetch / git worktree prune が Claude/Codex 両 hook flavor で同じ reason code になること、display readonly が従来どおり readonly_command になること、git fetch | head -n 1 と git worktree prune | head -n 1 が fail-closed になること、schema key/value type が不変であることを固定しています。

非ブロッカーだが直したい点

1. git fetch の分類名は、オプション付き fetch まで含むにはやや強すぎる

現在の pattern は ^git\s+fetch(\s|$) なので、git fetch だけでなく、git fetch --prune-tags、git fetch --refmap=...、明示 refspec 付き fetch も同じ branch_safe_maintenance_command になります。Git 公式 docs では git fetch は remote-tracking branch を更新し、.git/FETCH_HEAD も書きます。さらに refspec は「どの local refs を update するか」を指定でき、<dst> が空でなければ local ref の update を試みます。([Git]1) ([Git]1)

この PR の目的が「allow/block は変えず、readonly という誤語彙を消す」なら現状でも許容できます。ただし、branch_safe_maintenance_command という名前は「安全な maintenance 操作」に見えます。実態は「root checkout branch を直接 switch/check out しない Git 操作」です。厳密にするなら、追加テストかコメントで次のどちらかを明示してください。

# Option A: 現状維持する場合
# git fetch variants may update refs/FETCH_HEAD, but this guard only protects
# local root checkout branch drift. Therefore they are classified as
# branch-safe maintenance, not read-only.

# Option B: telemetry をより中立化する場合
REASON_NON_CHECKOUT_GIT_MAINTENANCE = "non_checkout_git_maintenance_command"

git worktree prune についても公式 docs は $GIT_DIR/worktrees の stale administrative files を削除すると説明しており、read-only ではありません。今回の分離方針自体は正しいです。([Git]2)

2. Consumer inventory の rg 例は hidden path を落とす可能性がある

PR 本文の inventory は rg -l readonly_command . 形式ですが、通常の rg は hidden directory を検索対象から落としやすいため、.claude/hooks/tests/... のような hidden path を見落とします。実際、GitHub code search では readonly_command は scripts/agent-guards/local_main_branch_guard.py、tests/codex/test_local_main_branch_guard.py、.claude/hooks/tests/test_local_main_branch_guard.py に存在します。

修正するなら PR 本文と検証コマンドを次に差し替えるのがよいです。

rg -uu -n 'LOCAL_MAIN_BRANCH_GUARD_RESULT_V1|readonly_command|branch_safe_maintenance_command' \
  scripts tests .claude .codex

今回、runtime consumer としての LOCAL_MAIN_BRANCH_GUARD_RESULT_V1 は core file だけに見えます。
ただし「hidden path も見た」と明示しないと、今後の schema change review で同じ弱点が残ります。

追加すると強いテスト

ブロッカーではありませんが、次の 2 件は入れる価値があります。

@pytest.mark.parametrize("command", ["git fetch", "git worktree prune"])
def test_branch_safe_maintenance_reason_code_when_root_already_drifted(
    self,
    tmp_git_repo_drifted: Path,
    command: str,
):
    result = eval_in_local_root(command, str(tmp_git_repo_drifted))
    assert result["status"] == "allow"
    assert result["reason_code"] == REASON_BRANCH_SAFE_MAINTENANCE
@pytest.mark.parametrize(
    "command",
    [
        "git fetch origin +seen:seen",
        "git fetch --prune-tags origin",
        "git fetch --refmap= refs/heads/main",
    ],
)
def test_fetch_variants_are_intentionally_branch_safe_not_readonly(
    self,
    tmp_git_repo: Path,
    command: str,
):
    assert is_readonly_command(command) is False
    assert is_branch_safe_maintenance_command(command) is True

後者は「許可するべき」というより、広い git fetch pattern を意図的に採用していることを固定するテストです。Git fetch は ref 更新や pruning を伴えるため、ここを暗黙仕様にしない方がよいです。(Git)

WEB調査からの品質観点

git fetch は read-only ではありません。remote-tracking branch、FETCH_HEAD、local ref、tag、maintenance に副作用を持ち得ます。したがって readonly_command から分離する今回の方向は妥当です。([Git]1)

git worktree prune も $GIT_DIR/worktrees の管理情報を削除するため、display read-only ではありません。branch_safe_maintenance_command 側に移す判断は妥当です。([Git]2)

pipeline parsing については、Python shlex は Unix shell 向けであり、完全な shell enforcement boundary ではありません。今回のように narrow allowlist + fail-closed に寄せる設計は正しいです。([Python documentation]3)

Codex hooks の PreToolUse は Bash / apply_patch / MCP を intercept できますが、公式 docs 上も complete enforcement boundary ではなく guardrail と説明されています。したがって、この PR は「安全境界の強化」ではなく「telemetry vocabulary の正規化」と位置づけるのが正確です。([OpenAI Developers]4)

推奨レビューコメント

Verdict: COMMENT / Approve-leaning

#1075 の中心契約は満たしています。`git fetch` / `git worktree prune` は `readonly_command` から分離され、display readonly classifier と branch-safe maintenance classifier の再結合も解消されています。readonly pipeline も display readonly のみを起点にしており、`git fetch | head -n 1` / `git worktree prune | head -n 1` が fail-closed になるテストも入っています。

Merge blocker はありません。

ただし merge 前に次のどちらかを入れると、後続レビューでの再燃を防げます。

1. PR 本文の consumer inventory を `rg -uu` ベースに修正してください。現状の `rg -l ... .` は hidden path を落とし得るため、`.claude/**` を確認した証跡として弱いです。

2. `git fetch` pattern は全 fetch variants を `branch_safe_maintenance_command` に分類します。Git fetch は refspec / pruning / FETCH_HEAD / auto maintenance に副作用を持ち得るため、「この guard は root checkout branch drift を守るものであり、ref mutation 全般を read-only 扱いしない」というコメントまたはテストを追加してください。

この 2 点は品質上の補強であり、#1075 の current AC 未達ではありません。

実務判断としては、このままでも #1075 は close 可能。ただし Draft のままなら上記 2 点を入れてから Ready for review にするのが最も安全です。

@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.

LOOP_VERDICT_V2

LOOP_VERDICT_V2:
  verdict: APPROVE
  reviewed_head_sha: "92be8a3b2a8a14ab9119ce58b16a3f87673ae934"
  merge_ready: true
  mergeability:
    mergeable: "MERGEABLE"
    merge_state_status: "CLEAN"
  blockers: []
  required_auto_actions: []
  auto_fix_applied: []
  follow_up_issue_requests: []
  allowed_paths_gate:
    status: ok
    expected_contract_fingerprint: "sha256:2536dc1e713913bd6da0c18f0a28ee1ebb0474b01175f84efcf3ae84d9e64f19"
    contract_source_kind: issue_comment_snapshot
    contract_source_id: "issuecomment-4761002290"
    producer_role: review_subagent
    worker_report_used_as_canonical: false
    changed_files:
      - .claude/hooks/tests/test_local_main_branch_guard.py
      - scripts/agent-guards/local_main_branch_guard.py
  allowed_paths_gate_source: review_subagent

判定根拠:

  • Closes #1075 があり、linked issue contract の AC/Allowed Paths/VC と整合しています。
  • changed files は issue contract の Allowed Paths 2件に一致し、Allowed Paths gate は ok です。
  • DISPLAY_READONLY_PATTERNS と BRANCH_SAFE_MAINTENANCE_PATTERNS の分離、readonly_command からの telemetry 分離、readonly pipeline fail-closed 回帰、schema key/value type 不変回帰が diff と PR本文で確認できました。
  • ci_verdict_summary.py --pr 1079 --expected-head-sha 92be8a3b2a8a14ab9119ce58b16a3f87673ae934 は status: all_pass、stale/pending/failed checks はありません。
  • PR本文には Schema Change Applicability / Schema Consumer Inventory / Safety Claim Matrix があり、schema additive change と safety claim の記述も contract と矛盾しません。

@squne121
squne121 marked this pull request as ready for review June 21, 2026 08:03
@squne121
squne121 merged commit f66edd5 into main Jun 21, 2026
21 checks passed
@squne121
squne121 deleted the worktree-issue-1075-normalize-branch-safe-classifier-telemet branch June 21, 2026 08:04
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