Skip to content

実装: update_branch.py を canonical branch-update executor に統一し、入力・race・postcondition を自己検証する #1429

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "implement-issue の update_branch 実行契約を、Claude Code/Codex CLI Hook の block/allow に依存せず、update_branch.py 自体の入力検証・race guard・postcondition readback で完結する canonical branch-update executor へ統一する"
change_kind: code
preflight_scope: implementation
runtime_verification:
  required: true
  mode: immediate
  evidence_type: executor-contract-subprocess-tests

Parent Issue

なし(単独改善)

Depends On

なし。旧版は #1403(branch force delete lane、CLOSED)を local_main_branch_guard.py 共有編集の前提として記載していたが、本改訂は local_main_branch_guard.py を Allowed Paths から除外するため、この依存は解消する。

Parent Goal Ref

  • Goal: implementation-worker の update_branch mode が、決定論的な .claude/skills/implement-issue/scripts/update_branch.py を production code/Skill/Agent が使用する canonical supported path として使用する(導入: AIエージェント運用をminimal harnessへ縮退し、過剰なcontrol-planeを廃止する #1860 Owner Decision: 「唯一の実行経路」は canonical supported path の意味に限定し、raw command が技術的に不可能であるとは主張しない)
  • Desired Destination:
    • update_branch.py が repository/PR番号/full SHA/caller/update method を API 呼び出し前に自己検証する
    • .claude/skills/implement-issue/SKILL.md と .claude/agents/implementation-worker.md が、raw gh api のインライン例を持たず、update_branch.py の canonical invocation のみを production caller として記載する
    • raw update-branch endpoint(gh api -X PUT .../update-branch 系)が wrapper 内部実装以外の production caller に存在しないことが static inventory で示される
    • update_branch.py の exact invocation は root checkout の default branch から実行できる(Hook block/allow の有無に関わらず)

Current Validated Scope

current main(.claude/skills/implement-issue/scripts/update_branch.py、control-plane が直接 git show HEAD: で読取り確認済み)は既に次を実装している。

  • expected_head_sha の事前照合(_get_current_head_sha() で PR head を取得し current_head_sha != expected_head_sha なら status: blocked / reason_code: expected_head_sha_mismatch を返し API を呼ばない — race guard)
  • REST PUT /repos/{owner}/{repo}/pulls/{pull_number}/update-branch(gh api -i -X PUT を list argv で起動、shell 文字列を経由しない)
  • 202 Accepted 後の bounded poll(poll_max / poll_interval 引数、既定 12 回・5 秒間隔)
  • 403(permission_blocked + _get_permission_diagnostics())/422(expected_head_sha mismatch 再分類 or validation_failed)/429・secondary rate limit(_is_secondary_rate_limit() + _rate_limit_diagnostics())/transport error(returncode 非 0 かつ HTTP status 不明)の分類
  • 更新成功後の rerun_required: {verification: true, pr_review: true, reason: pr_head_changed_by_update_branch} postcondition

一方で、次は未実装または契約と不整合であることを確認済み。

  • --pr-number は argparse の type=int のみで正数チェックがない
  • --repo に対する format/canonical repository 一致の検証がない
  • --expected-head-sha に対する完全長 hexadecimal 文字列の検証がない
  • --caller は任意文字列を許容し allowlist がない
  • .claude/skills/implement-issue/SKILL.md:319 に raw gh api -i -X PUT repos/$REPO/pulls/$PR_NUMBER/update-branch を直接実行するインライン bash 例が存在し、update_branch.py を経由しない production caller になっている(static inventory の反例)
  • .claude/agents/implementation-worker.md:14 の Bash 許可例外一覧に raw gh api -X PUT .../update-branch がそのまま記載されている
  • .claude/hooks/local_main_branch_guard.sh / .claude/hooks/worktree_scope_guard.sh は current main の .claude/settings.json の hooks.PreToolUse から削除済み(commit f971a95d, PR chore: local_main_branch_guard / worktree_scope_guard の2 hook を緊急停止する #1691)であり、.codex/hooks.json にも branch/worktree 系の PreToolUse blocking hook は一切登録されていない(SessionEnd/SubagentStop の advisory hook のみ)。Hook source file 自体(.sh/scripts/agent-guards/local_main_branch_guard.py 等)は repo に現存するが runtime wiring から切断されている

Remaining Parent Gaps

なし

Outcome

implement-issue の update_branch Contract が、raw gh api の直接実行やインライン bash 例ではなく、次の canonical wrapper invocation のみを production caller として記載する契約へ変更されている。

uv run --locked python3 \
  .claude/skills/implement-issue/scripts/update_branch.py \
  --pr-number <pull-request-number> \
  --repo squne121/loop-protocol \
  --expected-head-sha <reviewed-head-sha> \
  --caller impl-review-loop.step-5 \
  --update-method merge_only

update_branch.py は、次の入力検証を API 呼び出し前に自己完結で行う。

  • pr_number > 0
  • repo == squne121/loop-protocol
  • expected_head_sha が完全長 hexadecimal commit SHA
  • caller が既知の production caller allowlist(impl-review-loop.step-5 等)に含まれる
  • update_method == merge_only
  • 上記いずれかに違反する場合、GitHub API を呼ばず validation_failed(または既存の対応する reason code)を返す

.claude/skills/implement-issue/SKILL.md と .claude/agents/implementation-worker.md のどちらにも raw gh api -X PUT .../update-branch のインライン実行例・許可例外が残っておらず、両ファイルは update_branch.py の canonical invocation のみを参照する。static inventory(repository 全体 grep)により、raw update-branch endpoint 文字列が update_branch.py 内部実装以外に存在しないことを示す。

safety は Hook block/allow ではなく、次の decision で構成する(#1860 Owner Decision)。

  • update_branch.py 自身の入力検証(上記)
  • repository binding(--repo の厳密一致)
  • race guard(expected_head_sha 事前照合、mismatch 時は API 呼び出しをスキップ)
  • postcondition readback(更新後の実際の head SHA 取得と rerun_required の設定)
  • FakeGhRunner を使った executor contract test(202/403/422/429/transport の分岐を決定論的に検証)
  • 実際の diff・実テスト・current-head CI・independent review(PR review)・human stop

In Scope

update_branch.py の executor 契約強化、production caller(SKILL.md/implementation-worker.md/Step 5 routing)の統一、raw endpoint の static inventory、executor contract test を対象とする。Claude/Codex Hook、settings、Codex rules は対象外(次節参照)。

1. implement-issue 契約を update_branch.py に統一する

  • .claude/skills/implement-issue/SKILL.md の ## update_branch Contract を修正する
  • worker向け呼び出し形式を raw gh api インライン bash 例(現 319 行目)から update_branch.py invocation のみへ変更する
  • raw gh api は wrapper 内部実装としてのみ使用可能と明記する
  • gh pr update-branch は引き続き使用しない
  • UPDATE_BRANCH_REQUEST_V1 / UPDATE_BRANCH_RESULT_V1 の schema は維持する
  • wrapper が request schema を CLI引数へマッピングする規則を明記する

2. implementation-worker の許可コマンドを修正する

  • .claude/agents/implementation-worker.md の Bash許可例外(現 14 行目)を次の形式へ変更する
uv run --locked python3 .claude/skills/implement-issue/scripts/update_branch.py ...
  • raw gh api -X PUT .../update-branch を許可例外として記載しない
  • update_branch mode は wrapper(production supported path)を経由することを明記する。raw command が技術的に実行不可能であるとは主張しない

3. Step 5 routing の実行経路を修正する

  • .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md の BEHIND 分岐を修正する
  • implementation-worker が update_branch.py を実行することを明記する
  • REST呼び出し、202 poll、403 / 422分類は wrapper内部の責務とする
  • orchestrator は UPDATE_BRANCH_RESULT_V1 のみを消費する

4. update_branch.py の入力境界を強化する

.claude/skills/implement-issue/scripts/update_branch.py に、次の入力検証を追加する(execute_update_branch 内、GitHub API 呼び出し前)。

  • pr_number > 0
  • repo == squne121/loop-protocol
  • expected_head_sha が完全長 hexadecimal SHA(正規表現で検証)
  • caller が明示的な production caller allowlist に含まれる
  • update_method == merge_only(既存実装済み)
  • 不正入力時は GitHub APIを呼ばず validation_failed または既存の対応 reason code を返す(既に update_method については実装済み。他の入力にも同一パターンを適用する)
  • GitHub CLI は既存どおり list argv で起動する(shell文字列を経由しない)
  • 全ての gh pr view / gh api に明示的な repository bindingを使用する(既存実装済み、regression として維持)
  • production invocation では poll回数・poll間隔を callerから弱められない(productionから呼ぶ際の既定値を固定し、callerが緩和できないことをテストで固定する)

5. raw update-branch endpoint の static inventory を追加する

update_branch.py 以外の production path(.claude/skills/**/*.md、.claude/agents/*.md、.claude/skills/**/scripts/*.py、scripts/**/*.py)に、raw gh api ... update-branch または gh pr update-branch の実行例・呼び出しが存在しないことを検証する static inventory test を追加する。テスト fixture・Hook fixture・adversarial test 内の文字列(block/deny を検証する既存 Hook test 等)は inventory の対象から明示的に除外する(それらは実行例ではなく、無効化された Hook のための historical fixture である)。

Out of Scope

  • .claude/settings.json の変更
  • .codex/rules/default.rules の変更
  • Claude Code/Codex CLI Hook(local_main_branch_guard、worktree_scope_guard 等)の新規追加・復旧・修正
  • Hook repair hint(HOOK_COMMAND_REPAIR_HINT_V1 等)の新規追加
  • Hook parity/Hook subprocess テストの新規追加
  • raw gh api PUT .../update-branch の allowlist 追加
  • raw gh api の実行を技術的に不可能にするという主張
  • gh pr update-branch の使用
  • GraphQL updatePullRequestBranch
  • rebase update
  • merge以外のupdate method
  • Claude / Codexで別々のparserを実装すること
  • GitHub repository setting / branch protectionの変更
  • UPDATE_BRANCH_RESULT_V1 の不要なschema変更
  • branch_force_delete_denied の分類・repair hint・force delete allow/deny matrix・post-merge-cleanup guidance(実装: post-merge-cleanup の dedicated branch force delete lane を許可する #1403 の責務、変更しない)

Acceptance Criteria

  • AC1: implement-issue/SKILL.md の update_branch Contract が raw gh api のインライン bash 例ではなく update_branch.py の canonical invocation のみを production caller として記載している
  • AC2: implementation-worker.md の Bash許可例外が update_branch.py invocationへ変更され、raw gh api update-branch が許可例外から削除されている
  • AC3: Step 5 BEHIND routing が update_branch.py をworker実行経路として参照している
  • AC4: update_branch.py が PR番号(正数)、repo(canonical 一致)、SHA(完全長 hex)、caller(allowlist)、update methodをAPI呼び出し前に検証し、違反時は GitHub API を呼ばず reason_code を返す
  • AC5: production path(.claude/skills/**、.claude/agents/*.md、scripts/**)に raw gh api .../update-branch または gh pr update-branch の実行例・呼び出しが update_branch.py 内部実装以外に存在しないことが static inventory test で確認できる(Hook fixture/adversarial test 内の文字列は明示的 allowlist で除外する)
  • AC6: update_branch.py の既存 202 / 403 / 422 / 429 / transport error contract testsが回帰なくPASSする
  • AC7: 既存の FakeGhRunner executor contract test(test_given_preflight_mismatch_when_execute_then_block_before_rest_call)が、expected_head_sha mismatch 時に API 呼び出しをスキップすること(race guard)を回帰なく検証し続けている
  • AC8: production invocation の poll 回数・poll 間隔が caller から緩和できないことがテストで固定されている
  • AC9: .claude/settings.json/.codex/rules/default.rules/.claude/hooks/**/scripts/agent-guards/local_main_branch_guard.py/.codex/hooks.json のいずれも本 Issue の diff に含まれない

Runtime Verification Applicability

runtime_verification_applicability:
  decision: immediate
  reason: "update_branch.py の入力検証・race guard・HTTP分類・postconditionは FakeGhRunner を使った subprocess/関数呼び出しレベルの決定論的テストでローカル検証できる。Hook wrapper のsubprocess起動は本 Issue のスコープ外であり証明対象にしない。"
  applicable_acs:
    - AC4
    - AC5
    - AC6
    - AC7
    - AC8
  execution_environment: "WSL2 Ubuntu / repository pytest harness"
  skip_conditions:
    - "python3、uv のいずれかが存在せず、pytest を起動できない場合"
  fallback_policy: "実 subprocess/関数呼び出しテストを実行できない場合、静的 grep のみでPASS扱いにせずruntime verificationをdeferredまたはblockedとして人間判断へ送る"
  artifact_requirements:
    - "update_branch REST contract test output"
    - "raw update-branch static inventory test output"

Verification Commands

# AC1
$ rg -n "update_branch.py" .claude/skills/implement-issue/SKILL.md
# baseline-expect: pass
$ rg -n "gh api -i -X PUT" .claude/skills/implement-issue/SKILL.md
# AC2
$ rg -n "update_branch.py" .claude/agents/implementation-worker.md
# baseline-expect: pass
$ rg -n "update-branch" .claude/agents/implementation-worker.md
# AC3
$ rg -n "update_branch.py" .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md
# AC4
$ uv run --locked pytest tests/agent_ops/test_update_branch_input_validation.py -v
# AC5
$ uv run --locked pytest tests/agent_ops/test_update_branch_static_inventory.py -v
# AC6
# baseline-expect: pass
$ uv run --locked pytest .claude/skills/implement-issue/tests/test_update_branch_rest_contract.py -v
# AC7
# baseline-expect: pass
$ uv run --locked pytest .claude/skills/implement-issue/tests/test_update_branch_rest_contract.py -k "preflight_mismatch" -v
# AC8
$ uv run --locked pytest tests/agent_ops/test_update_branch_poll_bounds.py -v
# AC9
# baseline-expect: pass
$ git diff --name-only origin/main... -- .claude/settings.json .codex/rules/default.rules .claude/hooks scripts/agent-guards/local_main_branch_guard.py .codex/hooks.json

Allowed Paths

  • .claude/skills/implement-issue/SKILL.md
  • .claude/skills/implement-issue/scripts/update_branch.py
  • .claude/skills/implement-issue/tests/test_update_branch_rest_contract.py
  • .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md
  • .claude/agents/implementation-worker.md
  • tests/agent_ops/test_update_branch_static_inventory.py
  • tests/agent_ops/test_update_branch_input_validation.py
  • tests/agent_ops/test_update_branch_poll_bounds.py

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • Hook、settings、Codex rules の変更が実装に必要と判明した場合(本 Issue のスコープ外であり着手しない)
  • UPDATE_BRANCH_REQUEST_V1 / UPDATE_BRANCH_RESULT_V1 の互換性を破壊する変更が必要な場合
  • repository permission / branch protection変更が必要な場合
  • 外部サービス利用、権限昇格、既存テストの大規模改変が必要になった場合

Required Skills

  • Python
  • GitHub CLI / REST API
  • pytest / FakeGhRunner パターン

Required Design References

  • .claude/skills/implement-issue/SKILL.md
  • .claude/skills/implement-issue/scripts/update_branch.py
  • .claude/agents/implementation-worker.md
  • .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md

Delivery Rule

  • 1 Issue = 1 PR
  • 実装開始前に issue-contract-review で fresh CONTRACT_REVIEW_RESULT_V1 status: go を取得する
  • Dedicated worktreeを作成する
  • Draft PRを既定とする
  • PR本文に executor contract test の evidence を記載する
  • live GitHub update-branchはruntime verificationとして実行しない

Scope Delta

旧Issue #1429の「raw gh api update-branch を Claude/Codex Hook で拒否し、正規 wrapper へ誘導する」設計を撤回する(#1860 Owner Decision: Hook は Child C/本系列の authority・security boundary・dependency・AC にしない)。

新しい設計では以下を採用する。

  • safety は update_branch.py 自身の入力検証・race guard・postcondition readback・executor contract test・実 diff・実テスト・current-head CI・independent review・human stop で構成する
  • Hook block/allow、repair hint、Codex rules、settings permission による raw command の技術的拒否は実装しない
  • raw update-branch endpoint が wrapper 内部以外に存在しないことは、Hook による拒否ではなく static inventory test で示す
  • production caller(SKILL.md/implementation-worker.md)から raw gh api のインライン例を削除し、update_branch.py の canonical invocation のみを記載する

Notes for Reviewer

  • 本改訂は issue-refinement-loop(実装: Agent/Skill の重複を削減し progressive disclosure へ移行する #1732 Child C 改訂作業の一部)により、旧版(2026-07-XX 起票、Hook block/allow・Codex rules・settings.json 変更・Hook parity subprocess test を In Scope としていた版)を全面差し替えしたものである。旧版の historical comment は削除・改変していない。
  • 撤回した理由: #1860 Owner Decision により、Claude Code/Codex CLI Hook はこの系列の authority・security boundary・dependency・AC にしないことが確定した。Hook は「local workflow guardrail であり security boundary ではない」(Claude Code 公式 Hooks reference、disableAllHooks で無効化可能、exit code 1 は non-blocking)ことが web research で確認されている。また現在 local_main_branch_guard / worktree_scope_guard は .claude/settings.json の hooks.PreToolUse から既に削除済み(commit f971a95d, PR chore: local_main_branch_guard / worktree_scope_guard の2 hook を緊急停止する #1691)であり、Hook を dependency にすると現状の runtime と矛盾する。
  • 維持した理由: update_branch.py の入力検証・race guard(expected_head_sha)・202 bounded poll・HTTP分類・postcondition readback は、Hook の有無に関わらず GitHub API 呼び出しの安全性を担保する実体であり、そのまま維持する。
  • update_branch.py の内部 gh api 呼び出しはPythonのlist argv経由であり、shell command ではない
  • GitHub API response handlingは既存 FakeGhRunner contract testsで継続検証する

Activity

  1. added
    enhancement
    phase/implementation
    triage-required
    agent/implementer
    on Jul 10, 2026
  2. squne121 commented on Jul 11, 2026

    @squne121
    OwnerAuthor

    you asked

    message time: 2026-07-11 08:46:56

    #1429
    内容について関連Issue、PR、リポジトリ資産、コメントを参照した上で敵対的レビューして。
    取り扱う技術や配布物の実装をWEB検索し高品質化してほしい。


    chatgpt response

    Issue #1429の本文・コメント・リンクされたIssue/PR・関連実装・リポジトリ規約を追い、外部仕様と照合して欠陥、曖昧さ、検証不足を優先度付きで整理します。変更操作は行わず、レビュー結果のみ返します。

    現時点で、Issue本文のテスト配置指定と実際の専用テスト構成に不整合の兆候があります。また、リポジトリ設定では allow_update_branch が無効であり、hook例外追加だけで運用目的を達成できるかは別途検証が必要です。ここから実装境界とGitHub API仕様を突き合わせます。

    設計上の核心は、既存の決定論的 update_branch.py があるにもかかわらず、root から生の gh api を広く許可しようとしている点です。加えて、Issue の Allowed Paths は実際の guard テスト資産と一致せず、着手前 contract gate も未充足です。

    結論

    判定: REQUEST_CHANGES。現状の Issue #1429 は、そのまま実装しても目的を達成できない可能性が高く、セキュリティ境界も過度に拡張します。

    問題認識自体は正しいです。implement-issue は PUT /pulls/{number}/update-branch の実行を契約している一方、local_main_branch_guard は既存の GET allowlist に一致しない gh api を gh_api_not_allowed で遮断します。

    しかし、生の gh api を local_main_branch_guard.py だけで許可する設計は不十分です。


    重大な指摘

    P0-1: local_main_branch_guard だけ直しても、次の worktree_scope_guard に止められる

    Hook の実行順序は secret → local_main → worktree です。Issue #1429 が提案する変更で最初の guard を通過しても、worktree_scope_guard は gh api -X PUT ... を明示的に mutating と分類します。

    active Issue と対応 worktree が解決されている場合、root から許可される例外は次のような exact executor に限定されています。

    • agent-ops executor
    • skill-runtime executor
    • worktree-bootstrap executor
    • controlled-skill-mutation executor

    生の gh api はこの例外に該当しません。root の cwd が expected worktree 外であれば、最終的に block されます。

    PR #1421 は worktree-issue-1420-... ブランチ上にあるため、Issue #1429 の発端となったフローでは、この条件に入る可能性が高いです。

    必要な修正: local_main_branch_guard と worktree_scope_guard の双方が利用する shared policy / exact executor lane として設計する必要があります。


    P0-2: 生の gh api 許可は、既存の決定論的 wrapper を迂回している

    既に .claude/skills/implement-issue/scripts/update_branch.py が存在し、以下を実装しています。

    • 実行前の headRefOid 照合
    • expected_head_sha 不一致時の API 呼び出し前停止
    • list argv による shell 非依存実行
    • 202 後の head poll
    • 403、422、secondary rate limit、transport error の分類
    • 更新後の verification / review 再実行要求

    Issue #1429 の raw-command classifier が確認できるのは、コマンドの外形だけです。次は確認できません。

    • この操作が IMPLEMENTATION_WORKER_REQUEST_V2 由来か
    • PR review の reviewed_head_sha と一致しているか
    • caller が impl-review-loop.step-5 か
    • 対象 PR が現在処理中の PR か
    • fork PR に対する操作を許可すべきか

    したがって、条件に合う文字列を作れる caller は、同一リポジトリ内の任意の数値 PR に対して update を試行できます。

    リポジトリには既に「raw mutation ではなく、共有 policy と exact executor を両 guard が消費して split-brain を防ぐ」という設計があります。

    推奨: raw gh api は引き続き block し、既存 update_branch.py の canonical invocation だけを両 guard で許可してください。


    P0-3: 「default branch 上のみ」という条件が実装位置と一致していない

    Issue は root checkout の default branch 上でのみ許可する意図ですが、指定された挿入位置は gh_api_not_allowed より前です。この位置では、まだ root_state が計算されていません。root_state の判定は gh API 判定より後です。

    そのため、classifier が repo・PR番号・SHAしか検査しなければ、次の状態でも allow されます。

    • root が非 default branch に drift している
    • detached HEAD
    • branch 解決不能

    必要条件:

    • cwd == canonical main root
    • current_branch == resolved default branch
    • origin slug が trusted repo と一致
    • detached / unknown は fail-closed

    これらを shared policy 内で検証すべきです。


    P1-1: Allowed Paths とテスト対象が誤っている

    Issue が Allowed Paths に含めている scripts/agent-guards/tests/test_git_mutation_command_policy.py は、local_main_branch_guard をテストしていません。git_mutation_command_policy の rtk git add/commit/push を対象とする別のテストです。

    実際の専用テストは次です。

    • .claude/hooks/tests/test_local_main_branch_guard.py
    • tests/codex/test_local_main_branch_guard.py
    • .claude/hooks/tests/test_worktree_scope_guard.py

    Claude / Codex の両テストには、既に raw gh api mutation の回帰ケースがあります。

    Issue 本文は「専用テストがあればそちらを使用」としながら、その専用ファイルを Allowed Paths に含めていません。これは AC2 と Stop Conditions の自己矛盾です。

    また、Verification Command が rg "update-branch" だけでは、過剰許可、wrong repo、duplicate flags、drifted root、Codex parity、二段目 guard の block を一切検出できません。


    P1-2: Issue は implementation-ready ではない

    Issue #1429 にはコメントがなく、fresh な Contract Snapshot / CONTRACT_REVIEW_RESULT_V1 status: go が存在しません。

    implement-issue 自身は次を開始条件にしています。

    • Contract Snapshot URL
    • 最新の contract review が go
    • Runtime Verification Applicability の存在
    • consumer readiness の確認

    未充足なら停止する契約です。

    Issue #1429 には Runtime Verification Applicability もありません。これは hook の allow/block を変更する runtime-sensitive Issue なので、decision: immediate が妥当です。

    さらに、open な Issue #1403 が同じ local_main_branch_guard.py、Claude/Codex parity tests、hook-boundaries docs を変更対象にしています。並行実装による競合と policy split を避けるため、少なくとも依存関係または統合順序を明記すべきです。


    P1-3: 許可するコマンド文法が固定されていない

    実際の implement-issue 契約は、単なる

    gh api -X PUT endpoint -f expected_head_sha=...
    

    ではありません。-i、Accept header、API version header を含む canonical command を定義しています。

    一方、GitHub CLI には次の挙動があります。

    • -f / -F を付けると、明示しなければ method が POST に変わる
    • -F value=@file はファイルを読み込める
    • --input で任意 body を渡せる
    • {owner} / {repo} は cwd または GH_REPO から展開される
    • --hostname、--preview、--paginate など追加の意味を持つ flag がある

    (GitHub CLI)

    raw command を許可するなら、少なくとも以下を拒否する必要があります。

    • unknown / duplicate flags
    • -F、--field、--input
    • --hostname
    • endpoint placeholder
    • 追加の -f
    • 複数 endpoint
    • --method=PUT など canonical 以外の alias
    • PR番号 0、負数、巨大値
    • empty / abbreviated / non-hex SHA
    • 任意の追加 header
    • shell wrapper、leading env、compound command

    現在の Issue はこの negative matrix を定義していません。


    P1-4: fork・権限・repository setting の成立条件が未検証

    GitHub の update-branch endpoint は単なる PR metadata 更新ではなく、base branch の HEAD を PR head branch に merge する操作です。GitHub App の場合は Pull Requests write に加え、head repository の Contents write 権限も必要です。fork PR では endpoint の owner/repo が base repo と一致していても、実際の書き込み先は別の head repository になり得ます。(GitHub Docs)

    したがって、endpoint slug を現在の checkout に固定するだけでは十分ではありません。少なくとも次を preflight する必要があります。

    • headRepository.nameWithOwner
    • baseRepository.nameWithOwner
    • isCrossRepository
    • maintainerCanModify
    • auth actor / token permission

    また、レビュー時点の GitHub connector metadata では、このリポジトリの allow_update_branch は false でした。この設定が true の場合、branch freshness が merge 条件でなくても behind branch update を常時許可します。false ではその保証がないため、branch rules と実 PR を使った runtime verification が必要です。(GitHub Docs)


    P2: reason code とドキュメントの監査性が不足している

    REASON_GITHUB_REMOTE_OPS を再利用すべきではありません。既存の分類では、これは主に Issue / PR comment や edit などの metadata operation 用です。update-branch は PR head の commit graph を変更する操作なので、同じ監査分類に混ぜるとログ上の意味が失われます。 (GitHub Docs)

    推奨値は例えば次です。

    reason_code: github_update_branch_command
    rule_id: gh_api_update_branch_exact_executor
    command_kind: github_pr_branch_mutation
    

    また、docs/dev/hook-boundaries.md は hook 責務の正本で、現在は allowlist 外 gh api をすべて block すると明記しています。Issue の Allowed Paths にこの正本が含まれていないため、実装後に policy/document drift が残ります。


    推奨する設計

    推奨案: exact executor lane

    生の gh api は許可せず、以下の canonical command だけを許可します。

    uv run --locked python3 \
      .claude/skills/implement-issue/scripts/update_branch.py \
      --pr-number <positive-int> \
      --repo squne121/loop-protocol \
      --expected-head-sha <full-hex-sha> \
      --caller impl-review-loop.step-5 \
      --update-method merge_only

    実装方針:

    1. update_branch_command_policy.py のような shared policy を作る。
    2. local_main_branch_guard と worktree_scope_guard が同じ判定関数を使用する。
    3. canonical realpath、root/default branch、trusted origin、exact argv を検査する。
    4. update_branch.py 内で reviewed SHA / current PR / fork policy を検証する。
    5. raw gh api は従来どおり gh_api_not_allowed にする。
    6. dedicated reason code を出す。

    Issue #1429 に最低限必要な Scope Delta

    Allowed Paths には少なくとも以下が必要です。

    • scripts/agent-guards/local_main_branch_guard.py
    • .claude/hooks/worktree_scope_guard.py
    • shared update-branch command policy
    • .claude/skills/implement-issue/scripts/update_branch.py
    • .claude/skills/implement-issue/tests/test_update_branch_rest_contract.py
    • .claude/hooks/tests/test_local_main_branch_guard.py
    • .claude/hooks/tests/test_worktree_scope_guard.py
    • tests/codex/test_local_main_branch_guard.py
    • .claude/skills/implement-issue/SKILL.md
    • .claude/agents/implementation-worker.md
    • docs/dev/hook-boundaries.md
    • 必要なら .claude/settings.json

    Acceptance Criteria には、次を追加すべきです。

    • exact executor は root/default branch で両 guard を通過する
    • raw gh api update-branch は引き続き拒否される
    • drifted / detached root は拒否される
    • wrong repo / PR / SHA / caller / duplicate flag は拒否される
    • active issue worktree が存在しても exact executor だけは通過する
    • fork PR policy が決定論的に検証される
    • Claude / Codex parity が成立する
    • 202 後に head SHA が更新され、Step 2 → Step 4 → Step 5 の再実行要求が出る
    • 403 / 422 / 410 / rate limit が fail-closed に分類される

    Issue #1429 は「raw API の narrow allowlist追加」ではなく、「既存 update_branch wrapper を両 guard 共通の exact executor lane に接続する Issue」へ再 refinement するのが妥当です。


    you asked

    message time: 2026-07-11 09:10:48

    implement-issue の PUT /pulls/{number}/update-branch 実行契約を update_branch.pyに修正する
    生の gh api PUT /pulls/{number}/update-branch を実行しようとした際にブロックしながらupdate_branch.pyの実行へ誘導するClaudeCodeとCodexCLIのhooksを作成する。
    実装時はClaudeCodeとCodexCLIのhooksの動作検証をする。


    chatgpt response

    Issue #1429を「raw gh api の許可」から「update_branch.py を唯一の実行経路とし、raw API は拒否しつつ修復コマンドへ誘導する」契約へ組み替えます。Claude Code/Codex CLI双方の実hook検証までACとVCに固定します。

    既存構成では Claude Code と Codex CLI の各 wrapper が同じ local_main_branch_guard.py を呼ぶため、独立hookを重複追加するより、共通policyと既存hookを拡張する方が整合的です。update_branch.py の exact invocation は両guardで許可し、raw APIは最初のguardで専用理由コードと修復ヒントを返す構成にします。

    方針は妥当です。既存の Claude Code/Codex CLI wrapper は同じ local_main_branch_guard.py を呼ぶため、新しい独立hookを増やさず、共通policy・両guard・既存hook入口を拡張します。

    また、update_branch.py には既に race guard、REST呼び出し、202後のpoll、エラー分類が実装されています。これを唯一の実行経路として契約化します。

    **Document Title: **

    実装: update_branch.py を唯一の branch update 経路にし raw gh api を Claude/Codex hooks で拒否・誘導する

    Machine-Readable Contract

    contract_schema_version: v1
    issue_kind: implementation
    parent_issue: "none"
    goal_ref: "implement-issue の update_branch 実行契約を raw gh api 直接実行から update_branch.py exact executor 実行へ統一し、Claude Code / Codex CLI hooks が raw API 呼び出しを拒否しながら正規 wrapper へ誘導する"
    change_kind: code
    preflight_scope: implementation
    runtime_verification:
      required: true
      mode: immediate
      evidence_type: claude-codex-hook-subprocess-tests

    Parent Issue

    なし(単独改善)

    Parent Goal Ref

    • Goal: implementation-worker の update_branch mode が、決定論的な .claude/skills/implement-issue/scripts/update_branch.py を唯一の実行経路として使用する
    • Desired Destination:
      • 生の gh api -X PUT .../update-branch は引き続き hook で拒否される
      • 拒否時に update_branch.py の正規コマンドが machine-readable repair hint として提示される
      • update_branch.py の exact invocation は root checkout の default branch から実行できる
      • Claude Code と Codex CLI の実hook wrapperで allow/block動作が検証されている

    Current Validated Scope

    • .claude/skills/implement-issue/scripts/update_branch.py は既に次を実装している
      • expected_head_sha の事前照合
      • REST PUT /repos/{owner}/{repo}/pulls/{pull_number}/update-branch
      • 202 Accepted 後の bounded poll
      • 403 / 422 / 429 / transport error の分類
      • 更新成功後の verification / PR review 再実行要求
    • .claude/skills/implement-issue/SKILL.md は現在、worker が raw gh api を直接実行する形式を契約しており、実装済み wrapper と契約が一致していない
    • Claude Code の .claude/hooks/local_main_branch_guard.sh と Codex CLI の .codex/hooks/local_main_branch_guard.sh は、共通の scripts/agent-guards/local_main_branch_guard.py を呼び出している
    • raw gh api は local_main_branch_guard.py の gh_api_not_allowed で拒否されるが、update_branch.py への専用誘導がない
    • active issue worktree が存在する場合、root checkout からの update_branch.py 実行は worktree_scope_guard.py の exact executor 例外にも登録する必要がある

    Remaining Parent Gaps

    なし

    Outcome

    implement-issue の update_branch Contract が、raw gh api の直接実行ではなく、次の canonical wrapper invocation を使用する契約へ変更されている。

    uv run --locked python3 \
      .claude/skills/implement-issue/scripts/update_branch.py \
      --pr-number <pull-request-number> \
      --repo squne121/loop-protocol \
      --expected-head-sha <reviewed-head-sha> \
      --caller impl-review-loop.step-5 \
      --update-method merge_only

    Claude Code / Codex CLI から次のような raw API を実行しようとした場合は、既存の local_main_branch_guard hook が exit 2 で拒否する。

    gh api -i -X PUT \
      repos/squne121/loop-protocol/pulls/<number>/update-branch \
      -f expected_head_sha=<sha>

    拒否時の stderr には、専用の reason code と HOOK_COMMAND_REPAIR_HINT_V1 が出力され、update_branch.py の canonical invocation へ誘導される。

    一方、canonical update_branch.py invocation は、以下の条件をすべて満たす場合に限り、local_main_branch_guard と worktree_scope_guard の双方で許可される。

    • cwd が canonical root checkout
    • root checkout が default branch 上
    • script path が repository 内の正規 update_branch.py と realpath 一致
    • origin remote が squne121/loop-protocol と一致
    • argv が許可された exact shape と一致
    • --repo が squne121/loop-protocol
    • --pr-number が正の整数
    • --expected-head-sha が完全長の hexadecimal commit SHA
    • --caller が impl-review-loop.step-5
    • --update-method が merge_only
    • duplicate flag、unknown flag、--flag=value、shell wrapper、compound command、leading env assignmentが存在しない

    Claude Code と Codex CLI の実hook wrapperを subprocess で起動するテストにより、raw API の拒否、repair hint、canonical wrapper の許可、stdout/stderr contract が検証されている。

    In Scope

    1. implement-issue 契約を update_branch.py に統一する

    • .claude/skills/implement-issue/SKILL.md の ## update_branch Contract を修正する
    • worker向け呼び出し形式を raw gh api から update_branch.py invocation へ変更する
    • raw gh api は wrapper 内部実装としてのみ使用可能と明記する
    • gh pr update-branch は引き続き使用しない
    • UPDATE_BRANCH_REQUEST_V1 / UPDATE_BRANCH_RESULT_V1 の schema は維持する
    • wrapper が request schema を CLI引数へマッピングする規則を明記する

    2. implementation-worker の許可コマンドを修正する

    • .claude/agents/implementation-worker.md の Bash許可例外を次の形式へ変更する
    uv run --locked python3 .claude/skills/implement-issue/scripts/update_branch.py ...
    
    • raw gh api -X PUT .../update-branch を許可例外として記載しない
    • update_branch mode は wrapper以外を使用しないことを明記する

    3. Step 5 routing の実行経路を修正する

    • .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md の BEHIND 分岐を修正する
    • implementation-worker が update_branch.py を実行することを明記する
    • REST呼び出し、202 poll、403 / 422分類は wrapper内部の責務とする
    • orchestrator は UPDATE_BRANCH_RESULT_V1 のみを消費する

    4. update_branch.py の入力境界を強化する

    .claude/skills/implement-issue/scripts/update_branch.py に、少なくとも次の入力検証を追加または固定する。

    • pr_number > 0
    • repo == squne121/loop-protocol
    • expected_head_sha が完全長 hexadecimal SHA
    • caller == impl-review-loop.step-5
    • update_method == merge_only
    • 不正入力時は GitHub APIを呼ばず validation_failed または既存の対応 reason code を返す
    • GitHub CLI は shell文字列ではなく list argv で起動する
    • 全ての gh pr view / gh api に明示的な repository bindingを使用する
    • production invocation では poll回数・poll間隔を callerから弱められない

    5. shared update-branch command policy を追加する

    scripts/agent-guards/update_branch_command_policy.py を新設し、両guardから使用する。

    提供する判定は少なくとも次の2種類とする。

    parse_raw_update_branch_api_attempt(command)
    is_exact_update_branch_executor_command(command, cwd, project_root)
    looks_like_update_branch_executor_command(command)

    raw API attempt detector

    次の endpoint を対象とする raw gh api 呼び出しを識別する。

    repos/<owner>/<repo>/pulls/<positive-number>/update-branch
    

    識別結果は allow に使用せず、専用の block reason と repair hint の選択にのみ使用する。

    exact executor classifier

    次の script invocation のみを認識する。

    uv run --locked python3 .claude/skills/implement-issue/scripts/update_branch.py ...
    

    以下を拒否する。

    • script の absolute-path差し替え
    • symlinkによる script差し替え
    • python -c
    • /tmp 配下の wrapper
    • bash -c / bash -lc / sh -c
    • env VAR=...
    • --flag=value
    • duplicate flag
    • unknown flag
    • 不要な positional引数
    • shell metacharacter
    • wrong cwd
    • non-default branch
    • wrong origin
    • wrong repo
    • wrong caller
    • unsupported update method
    • invalid PR番号
    • invalid SHA

    6. local_main_branch_guard の block / allow を追加する

    scripts/agent-guards/local_main_branch_guard.py に以下を追加する。

    raw API block

    raw gh api .../update-branch を検出した場合:

    status: block
    reason_code: raw_update_branch_api_requires_wrapper
    rule_id: gh_api_update_branch_raw_denied
    command_kind: github_pr_branch_mutation

    一般的な gh_api_not_allowed より前に専用分類するが、allowにはしない。

    canonical wrapper allow

    exact update_branch.py invocation の場合:

    status: allow
    reason_code: update_branch_executor_command
    rule_id: update_branch_executor_allow
    command_kind: controlled_github_pr_branch_mutation

    malformed な wrapper-like command は generic default allowへ落とさず fail-closed にする。

    7. worktree_scope_guard に exact executor 例外を追加する

    .claude/hooks/worktree_scope_guard.py が shared policy を使用し、active issue worktree が存在する場合でも、canonical root/default branchからの exact update_branch.py invocation を許可する。

    • raw gh api は例外対象にしない
    • malformed wrapper-like command は blockする
    • local_main_branch_guard と独立した判定実装を複製しない
    • 両guardが同一の shared policy を消費し、split-brainを防止する

    8. repair hint を追加する

    scripts/agent-guards/hook_repair_hints.py に raw_update_branch_api_requires_wrapper 用の専用hintを追加する。

    HOOK_COMMAND_REPAIR_HINT_V1:
      blocked_command_class: github_pr_branch_mutation
      reason_code: raw_update_branch_api_requires_wrapper
      safe_action: 生の gh api を使用せず implement-issue の update_branch wrapper を実行する
      suggested_command: >-
        uv run --locked python3
        .claude/skills/implement-issue/scripts/update_branch.py
        --pr-number <pull-request-number>
        --repo squne121/loop-protocol
        --expected-head-sha <reviewed-head-sha>
        --caller impl-review-loop.step-5
        --update-method merge_only
      forbidden_alternatives:
        - gh api -X PUT .../update-branch
        - gh pr update-branch
      verification_command: gh pr view <pull-request-number> --repo squne121/loop-protocol --json headRefOid
      stop_condition: reviewed head SHAを確定できない場合は実行せず人間判断

    repair hintには raw command全文、認証情報、環境変数、header値を出力しない。

    9. Claude Code / Codex CLI command permission を整合させる

    • .claude/settings.json に canonical update_branch.py invocation の許可を追加する
    • .codex/rules/default.rules に canonical update_branch.py invocation の exact allow ruleを追加する
    • raw gh api update-branch を静的allow ruleとして追加しない
    • Codex rules は codex execpolicy check で検証する

    10. hook boundary document を更新する

    docs/dev/hook-boundaries.md に以下を追加する。

    • raw gh api update-branch は raw_update_branch_api_requires_wrapper でblock
    • canonical update_branch.py は exact executorとしてallow
    • Claude / Codex wrapperが同一policyを消費する
    • hookはsecurity boundaryではなくworkflow guardrailである
    • GitHub mutationの実際のrace / permission検証は update_branch.py が担当する

    11. Claude Code / Codex CLI の実hook動作を検証する

    mock関数だけでなく、実際の shell wrapperを subprocess で起動する。

    Claude Code

    対象:

    .claude/hooks/local_main_branch_guard.sh
    .claude/hooks/worktree_scope_guard.sh
    

    検証:

    • raw API payload → exit 2
    • stdout空
    • stderrに専用reason code
    • stderrに update_branch.py の修復コマンド
    • raw command全文をstderrへ出力しない
    • canonical wrapper payload → local_main / worktree 両guardでexit 0

    Codex CLI

    対象:

    .codex/hooks/local_main_branch_guard.sh
    .codex/hooks/worktree_scope_guard.sh
    

    検証イベント:

    • PreToolUse
    • PermissionRequest

    検証:

    • raw API payload → exit 2
    • stdout空
    • stderrに専用reason codeと修復コマンド
    • canonical wrapper payload → local_main / worktree 両guardでexit 0
    • ClaudeとCodexで reason code / rule id / command kind が一致する

    hook動作検証では実際のGitHub branch更新を実行しない。raw APIはhookで実行前に拒否し、canonical wrapperについてはhook classifierのallow判定までを実wrapperで検証する。wrapper内部のREST処理は FakeGhRunner を使用した既存contract testで検証する。

    Out of Scope

    • raw gh api PUT .../update-branch のallowlist追加
    • raw gh api の実行許可
    • gh pr update-branch の使用
    • GraphQL updatePullRequestBranch
    • rebase update
    • merge以外のupdate method
    • 新しい独立PreToolUse hook entryの追加
    • Claude / Codexで別々のparserを実装すること
    • hook subprocess testから実際のGitHub PRを更新すること
    • GitHub repository setting / branch protectionの変更
    • update-branch以外のraw gh api に対するrepair hint拡張
    • UPDATE_BRANCH_RESULT_V1 の不要なschema変更

    Acceptance Criteria

    • AC1: implement-issue/SKILL.md の update_branch Contract が raw gh api ではなく update_branch.py の canonical invocation を実行経路としている
    • AC2: implementation-worker.md の Bash許可例外が update_branch.py invocationへ変更され、raw gh api update-branch が許可例外から削除されている
    • AC3: Step 5 BEHIND routing が update_branch.py をworker実行経路として参照している
    • AC4: update_branch.py が PR番号、repo、SHA、caller、update methodをAPI呼び出し前に検証する
    • AC5: shared update_branch_command_policy.py が canonical wrapperのexact argvとcontextを判定する
    • AC6: raw gh api .../update-branch は raw_update_branch_api_requires_wrapper / gh_api_update_branch_raw_denied でblockされる
    • AC7: raw API block時の HOOK_COMMAND_REPAIR_HINT_V1 が canonical update_branch.py commandを提示する
    • AC8: canonical wrapper invocationはroot/default branchで local_main_branch_guard を通過する
    • AC9: canonical wrapper invocationはactive issue worktreeが存在してもroot/default branchから worktree_scope_guard を通過する
    • AC10: malformed wrapper、wrong repo、wrong caller、invalid SHA、invalid PR番号、duplicate / unknown flag、shell wrapperはfail-closedになる
    • AC11: update-branch以外のallowlist外 gh api は従来どおり gh_api_not_allowed でblockされる
    • AC12: Claude Codeの実hook wrapperでraw APIがexit 2、canonical wrapperがexit 0になる
    • AC13: Codex CLIの実hook wrapperで、PreToolUse / PermissionRequestのraw APIがexit 2、canonical wrapperがexit 0になる
    • AC14: Claude / Codex hooksのstdoutはallow/blockとも既存contractに従い、block stderrはboundedかつraw commandやsecret-like値を含まない
    • AC15: .claude/settings.json と .codex/rules/default.rules がcanonical wrapperを許可し、raw APIを新規allowしていない
    • AC16: docs/dev/hook-boundaries.md が専用reason code、wrapper-only policy、Claude/Codex parityを記述している
    • AC17: update_branch.py の既存202 / 403 / 422 / 429 / transport error contract testsが回帰なくPASSする
    • AC18: hook subprocess testsはGitHub APIを実行せず、実際のPR branchを変更しない

    Runtime Verification Applicability

    runtime_verification_applicability:
      decision: immediate
      reason: "Claude Code / Codex CLI の実hook wrapperをsubprocess起動し、raw APIのblockとcanonical wrapperのallowをローカルで決定論的に検証できる"
      applicable_acs:
        - AC6
        - AC7
        - AC8
        - AC9
        - AC10
        - AC11
        - AC12
        - AC13
        - AC14
        - AC15
        - AC17
        - AC18
      execution_environment: "WSL2 Ubuntu / repository hook test harness"
      skip_conditions:
        - "python3、uv、gitのいずれかが存在せず、hook wrapperを起動できない場合"
        - "Codex execpolicy checkerがインストールされていない場合"
      fallback_policy: "実hook wrapperを起動できない場合、unit testのみでPASS扱いにせずruntime verificationをdeferredまたはblockedとして人間判断へ送る"
      artifact_requirements:
        - "Claude hook subprocess test output"
        - "Codex PreToolUse hook subprocess test output"
        - "Codex PermissionRequest hook subprocess test output"
        - "update_branch REST contract test output"
        - "Codex execpolicy check output"

    Verification Commands

    # AC1 / AC2 / AC3
    rg -n "update_branch.py|gh api.*update-branch" \
      .claude/skills/implement-issue/SKILL.md \
      .claude/agents/implementation-worker.md \
      .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md
    
    # AC4 / AC17: update_branch wrapper contract
    uv run --locked pytest \
      .claude/skills/implement-issue/tests/test_update_branch_rest_contract.py
    
    # AC5 / AC10: shared exact-command policy
    uv run --locked pytest \
      scripts/agent-guards/tests/test_update_branch_command_policy.py
    
    # AC6 / AC7 / AC8 / AC11 / AC12 / AC14: Claude Code hook
    uv run --locked pytest \
      .claude/hooks/tests/test_local_main_branch_guard.py \
      -k "update_branch"
    
    # AC9: worktree scope exact executor
    uv run --locked pytest \
      .claude/hooks/tests/test_worktree_scope_guard.py \
      -k "update_branch"
    
    # AC6 / AC7 / AC8 / AC11 / AC13 / AC14: Codex CLI hook parity
    uv run --locked pytest \
      tests/codex/test_local_main_branch_guard.py \
      -k "update_branch"
    
    # AC15: Codex static command policy
    codex execpolicy check \
      --rules .codex/rules/default.rules \
      uv run --locked python3 \
      .claude/skills/implement-issue/scripts/update_branch.py \
      --pr-number 1421 \
      --repo squne121/loop-protocol \
      --expected-head-sha 0000000000000000000000000000000000000000 \
      --caller impl-review-loop.step-5 \
      --update-method merge_only
    
    # AC16: hook boundary manifest
    uv run --locked python3 scripts/check_hook_boundaries.py
    
    # Config / agent contract validation
    node scripts/check-codex-agents.mjs
    
    # Standard regression
    pnpm typecheck
    pnpm lint
    pnpm test
    pnpm build

    Allowed Paths

    • .claude/skills/implement-issue/SKILL.md
    • .claude/skills/implement-issue/scripts/update_branch.py
    • .claude/skills/implement-issue/tests/test_update_branch_rest_contract.py
    • .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md
    • .claude/agents/implementation-worker.md
    • scripts/agent-guards/update_branch_command_policy.py
    • scripts/agent-guards/tests/test_update_branch_command_policy.py
    • scripts/agent-guards/local_main_branch_guard.py
    • scripts/agent-guards/hook_repair_hints.py
    • .claude/hooks/worktree_scope_guard.py
    • .claude/hooks/tests/test_local_main_branch_guard.py
    • .claude/hooks/tests/test_worktree_scope_guard.py
    • tests/codex/test_local_main_branch_guard.py
    • .claude/settings.json
    • .codex/rules/default.rules
    • docs/dev/hook-boundaries.md

    Verification-Only Paths

    以下は実装時に編集せず、実hook entrypointとしてsubprocess実行する。

    • .claude/hooks/local_main_branch_guard.sh
    • .claude/hooks/worktree_scope_guard.sh
    • .codex/hooks/local_main_branch_guard.sh
    • .codex/hooks/worktree_scope_guard.sh
    • .codex/hooks.json

    Stop Conditions

    • raw gh api update-branch をallowしなければ実現できないと判明した場合
    • canonical wrapper invocationを両guard共通policyで表現できない場合
    • Claude / Codexで別々の判定実装が必要と判明した場合
    • 実hook検証にlive GitHub PR mutationが必要と判明した場合
    • UPDATE_BRANCH_REQUEST_V1 / UPDATE_BRANCH_RESULT_V1 の互換性を破壊する変更が必要な場合
    • Allowed Paths外の変更が必要と判明した場合
    • Issue #1403等のopen implementation Issueと同一policyファイルの競合が解消できない場合
    • repository permission / branch protection変更が必要な場合
    • nested SubAgent delegationが必要になった場合
    • 外部サービス利用、権限昇格、既存テストの大規模改変が必要になった場合

    Required Skills

    • Python
    • Claude Code PreToolUse hook
    • Codex CLI PreToolUse / PermissionRequest hook
    • GitHub CLI / REST API
    • command tokenization / exact argv policy
    • subprocess-based hook testing

    Required Design References

    • .claude/skills/implement-issue/SKILL.md
    • .claude/skills/implement-issue/scripts/update_branch.py
    • .claude/agents/implementation-worker.md
    • .claude/skills/impl-review-loop/steps/step-5-mergeability-handling.md
    • scripts/agent-guards/local_main_branch_guard.py
    • scripts/agent-guards/hook_repair_hints.py
    • scripts/agent-guards/controlled_skill_mutation_policy.py
    • .claude/hooks/worktree_scope_guard.py
    • .claude/hooks/local_main_branch_guard.sh
    • .codex/hooks/local_main_branch_guard.sh
    • docs/dev/hook-boundaries.md

    Delivery Rule

    • 1 Issue = 1 PR
    • 実装開始前に issue-contract-review で fresh CONTRACT_REVIEW_RESULT_V1 status: go を取得する
    • Dedicated worktreeを作成する
    • Draft PRを既定とする
    • PR本文にClaude / Codexの実hook subprocess evidenceを記載する
    • live GitHub update-branchはruntime verificationとして実行しない
    • raw API block、wrapper allow、REST wrapper logicをそれぞれ独立したテスト層で証明する

    Scope Delta

    旧Issue #1429の「raw gh api update-branch をallowlistへ追加する」設計を撤回する。

    新しい設計では以下を採用する。

    • raw API: blockを維持
    • raw API block時: wrapperへのrepair hintを返す
    • remote mutation: update_branch.py 内部だけで実行
    • root execution: exact wrapper invocationだけを両guardでallow
    • Claude / Codex: 既存hook wrapperを実subprocessで検証
    • shared policy: 両guardが同一判定を使用する

    Notes for Reviewer

    • 新しい独立hook entryは追加しない。Claude Code / Codex CLIには既に local_main_branch_guard wrapperが存在するため、共通guardとshared policyを拡張する
    • update_branch.py の内部 gh api 呼び出しはPythonのlist argv経由であり、hookが受け取る生のBash commandではない
    • hookはraw API呼び出しを拒否するが、wrapper内部のsubprocess呼び出しはPreToolUse対象外である
    • repair hintは操作を自動実行せず、正規経路のみを提示する
    • hook testはネットワークを使用せず、実際のPR headを変更しない
    • GitHub API response handlingは既存 FakeGhRunner contract testsで継続検証する

    この内容なら、旧 #1429 の raw allowlist 方針を完全に撤回し、wrapper-only設計と実hook検証を一つのIssueに固定できます。GitHub上のIssue本文はまだ変更していません。

  3. changed the title [-]実装: local_main_branch_guard に gh api update-branch の allowlist 例外を追加する[/-] [+]実装: update_branch.py を唯一の branch update 経路にし raw gh api を Claude/Codex hooks で拒否・誘導する[/+] on Jul 11, 2026
  4. squne121 commented on Jul 11, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  5. squne121 commented on Jul 11, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-07-11T00:45:33Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1429
      body_sha256: "sha256:472b1eef6d1d7f99d4c48e897c13c60685092eadd983ccfc800908ed6cbf21e1"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  6. squne121 commented on Jul 20, 2026

    @squne121
    OwnerAuthor

    Sequencing 決定(OWNER 判断、#1657 側 overlap preflight にて記録)

    #1657(実装: worktree mutation guard を AI エージェント非依存の shared core へ移行する)を最優先とし、先にマージする。本 Issue(#1429)の Allowed Paths には .claude/hooks/worktree_scope_guard.py と scripts/agent-guards/local_main_branch_guard.py が含まれるが、#1657 は worktree_scope_guard.py を .claude/hooks/ から scripts/agent-guards/worktree_scope_guard.py へ移設する計画である。

    本 Issue の実装着手前に、以下を確認すること:

    blocked_by: #1657 として本 Issue の着手順序を記録する(#1657 側 overlap preflight の HUMAN_C1_DECISION_V1 記録: #1657 (comment) )。

  7. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    #1732 系列 issue-refinement-loop による本文改訂(2026-08-05)

    #1860(Minimal harness decision)の Owner Decision を反映し、本 Issue の本文を全面改訂しました。

    撤回した契約:

    • Claude Code/Codex CLI Hook(local_main_branch_guard/worktree_scope_guard)による raw gh api update-branch の block/allow
    • Hook 拒否時の HOOK_COMMAND_REPAIR_HINT_V1 repair hint
    • .claude/settings.json/.codex/rules/default.rules の変更
    • Hook parity/Hook subprocess テストの追加
    • 「raw command は Hook で技術的に拒否される」という設計そのもの

    理由: #1860 Owner Decision により、Claude Code/Codex CLI Hook はこの系列の authority・security boundary・dependency・AC にしないことが確定しました。Hook は「local workflow guardrail であり security boundary ではない」(Claude Code 公式 Hooks reference: disableAllHooks で無効化可能、exit code 1 は non-blocking)ことを web research で確認しています。また現在 local_main_branch_guard/worktree_scope_guard は .claude/settings.json の hooks.PreToolUse から既に削除済み(commit f971a95d, PR #1691)であり、Hook を dependency にすると現状の runtime と矛盾していました。

    維持した契約(executor safety):

    • update_branch.py 自身の入力検証(PR番号・repo・SHA形式・caller・update method)
    • expected_head_sha による race guard(GitHub API 呼び出し前の事前照合)
    • 202 Accepted 後の bounded poll
    • 403/422/429/transport error の分類
    • 更新成功後の verification/PR review 再実行 postcondition
    • raw update-branch endpoint が wrapper 内部以外の production caller に存在しないことの static inventory
    • FakeGhRunner を使った executor contract test

    update_branch.py の race guard・202 poll・HTTP分類・postcondition は既に current main に実装済みであることを control-plane が git show HEAD: で直接確認済みです(本 Issue は主に入力検証の強化と、SKILL.md/implementation-worker.md からの raw API インライン例の除去を扱います)。

    本改訂は #1732 系列の issue-refinement-loop の一部です。旧本文は削除せず、上記コメントの下に新本文として反映します。

  8. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    issue-refinement-loop iteration 2(review needs-fix 対応)

    前回改訂に対する issue-reviewer レビューで、Verification Commands の VC パターンに関する blocker が指摘されました。以下を修正しました。

    issue-contract-review の pre-edit static readiness check で status: go を再確認済みです。

  9. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    issue-refinement-loop iteration 3(review needs-fix 対応、最終)

    前回(iteration 2)の再レビューで、AC1/AC2 の VC アノテーションに誤りがあることが判明しました。control-plane が git grep -n "update_branch.py" HEAD -- .claude/skills/implement-issue/SKILL.md .claude/agents/implementation-worker.md を直接実行して確認したところ、現時点でこの文字列はどちらのファイルにも存在しません(exit 1)。一方、gh api -i -X PUT(SKILL.md)と update-branch(implementation-worker.md)は現時点で実在します(exit 0)。

    このため、各 AC の VC 2行のうち 1行目(update_branch.py の存在確認)は通常の baseline-fail 想定のまま(# baseline-expect: pass 注記を削除)とし、2行目(raw API 例の存在確認)にのみ # baseline-expect: pass を残しました。

    修正後、contract_readiness_check.py --mode preflight-static で status: go を再確認済みです。

  10. changed the title [-]実装: update_branch.py を唯一の branch update 経路にし raw gh api を Claude/Codex hooks で拒否・誘導する[/-] [+]実装: update_branch.py を canonical branch-update executor に統一し、入力・race・postcondition を自己検証する[/+] on Aug 5, 2026
  11. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    タイトル更新(issue-refinement-loop 追記)

    旧タイトル「実装: update_branch.py を唯一の branch update 経路にし raw gh api を Claude/Codex hooks で拒否・誘導する」は、本文改訂(Hook block/allow の撤回)と矛盾していたため、Owner Decision の推奨タイトルへ変更しました。

    新タイトル: 実装: update_branch.py を canonical branch-update executor に統一し、入力・race・postcondition を自己検証する

  12. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    impl-review-loop: 完了 (2026-08-05T12:21:39Z)

    • iteration: 0(初回で APPROVE)
    • verdict: APPROVE
    • route: fail_closed(reason_code: merge_state_status_unstable_not_conflict_defer_to_ci_evaluator)→ required CI 確認済み PASS のため実質 draft_pr_ready
    • PR: 実装: update_branch.py を canonical branch-update executor に統一する #1999
    • 補足: mergeStateStatus は UNSTABLE だが required checks(wait_ci_checks.py --required)は PASS 済み。non-required の e2e/ci-verdict-summary job 失敗は本 PR の変更(update_branch.py 等)と無関係な既存 VRT(title-menu / combat HUD / pause dialog スクリーンショット)フレークであり blocker としない。mergeable: MERGEABLE、conflict なし。
    • 次アクション: 人間レビュー → マージ → post-merge-cleanup
    IMPL_REVIEW_LOOP_RESULT_V1:
      schema_version: 1
      status: draft_pr_ready
      pr_url: "https://github.com/squne121/loop-protocol/pull/1999"
      head_sha: "2334d5b240be820e57e17657ba73df4ca3269ebb"
      issue_number: 1429
      termination_reason: approved
      merge_ready: false
      iteration: 0
    
    FOLLOW_UP_MATERIALIZATION_RESULT_V1:
      schema_version: 1
      materialized_by: impl-review-loop
      follow_up_issues: []
      note_only_observations: []
  13. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    impl-review-loop: 完了 (2026-08-05T13:54:23Z)

    経緯サマリ

    1. 初回実装(head 2334d5b)→ pr-reviewer APPROVE。ただし OWNER が直接 PR にコメントで REQUEST_CHANGES(static inventory の bypass、postcondition の ancestry 未検証、subprocess runtime verification 欠如、CI 未 triage 等 P1 4件 + P2 2件)。
    2. fix_delta iteration 1(head dbd929b)で全 P1/P2 を修正。pr-reviewer が敵対的に再検証したが、PR 本文の Schema Change Applicability 記述の誤り(実際は additive schema change なのに not_schema_change と記載)を新規 blocker として検出、REQUEST_CHANGES。
    3. body-only fix(head 変更なし)で PR 本文の Schema Change Applicability / Consumer Inventory を訂正。pr-reviewer 再レビューで APPROVE。ただし live mergeability が BEHIND のため route_to_update_branch。
    4. update_branch.py(merge_only)で head を main 最新へ更新(f649d7d4)。verification 再実行(全AC・全検証 PASS)、pr-reviewer 再レビューで APPROVE。live mergeability CLEAN で終了。
    IMPL_REVIEW_LOOP_RESULT_V1:
      schema_version: 1
      status: draft_pr_ready
      pr_url: "https://github.com/squne121/loop-protocol/pull/1999"
      head_sha: "f649d7d4436fb53e1a6bad00cd0dc1e157710600"
      issue_number: 1429
      termination_reason: approved
      merge_ready: true
      iteration: 2
    
    FOLLOW_UP_MATERIALIZATION_RESULT_V1:
      schema_version: 1
      materialized_by: impl-review-loop
      follow_up_issues: []
      note_only_observations: []
  14. squne121 commented on Aug 5, 2026

    @squne121
    OwnerAuthor

    post-merge-cleanup: 完了 (2026-08-05T14:10:57Z)

    • status: ok(cleanup_exec の自動拒否分は人間承認後に手動対応)
    • 次アクション: なし(cleanup 完了)

    補足

    worker(post-merge-cleanup-worker)の cleanup_exec は worktree が detached HEAD 状態だったため worktree_branch_mismatch で自動削除を拒否した(status: partial, human_review_required: true)。人間承認を得た上で、PR #1999 が MERGED・Issue #1429 が CLOSED・worktree に未コミット変更なし・remote branch が既に削除済みであることを確認してから、git worktree remove .claude/worktrees/issue-1429-update-branch-canonical-executor と git branch -D worktree-issue-1429-update-branch-canonical-executor を手動実行しクリーンアップを完了した。

    • parent issue: なし(単独改善)
    • superseded PR: なし
    • follow-up候補: なし
    FOLLOW_UP_MATERIALIZATION_RESULT_V1:
      schema_version: 1
      materialized_by: post-merge-cleanup
      follow_up_issues: []
      note_only_observations: []
  15. added 2 commits that reference this issue on Aug 11, 2026
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