Skip to content

実装: post-merge-cleanup の branch-only resume lane を cleanup_exec に追加する #1196

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "partial cleanup 状態でも cleanup_exec 経由で branch-only cleanup を決定論的に完了できるようにする"
change_kind: workflow

Parent Issue

none

Parent Goal Ref

  • Goal: partial cleanup 状態でも cleanup_exec 経由で branch-only cleanup を決定論的に完了できるようにする
  • Desired Destination: post-merge-cleanup が worktree 削除済み・local branch 残存の partial 状態からでも、正本 executor 経由で安全に cleanup を完了できる状態

Current Validated Scope

  • scripts/agent-ops/cleanup_exec.py に branch-only resume lane を追加する
  • scripts/agent-ops/cleanup_contract_v3.py と scripts/agent-ops/guard_preflight.py に branch-only 判定・reason code・verified field を追加する
  • post-merge-cleanup skill と hook / guard テストを branch-only lane に追従させる
  • verify_cleanup_authorization() は通常 cleanup 専用のまま維持する(allow_branch_only=False がデフォルト)。branch-only verifier は cleanup_exec.run() の WORKTREE_NOT_IN_CATALOG 分岐からのみ到達させ、materialize_cleanup_contract.py からは branch-only verifier に到達させない

Remaining Parent Gaps

  • hook failure transcript noise compaction は別 Issue で扱う
  • resume capsule / SubAgent output contract v2 は別 Issue で扱う

Outcome

cleanup_exec が worktree 削除済み・branch のみ残存する partial cleanup 状態を判定し、必要条件を満たす場合に branch-only cleanup を status: ok で完了でき、対応テストと help/contract が更新されている状態。

In Scope

  • cleanup_exec の branch-only candidate 判定と verified field 拡張
  • branch-only lane に限定した git branch -D 昇格条件の実装
  • actions_taken / reason_code / report field の更新
  • post-merge-cleanup skill と guard テストの更新

Out of Scope

  • bare git branch -D を agent へ直接許可する変更
  • worktree_scope_guard の fail-closed 方針緩和
  • PR head OID mismatch の自動補正
  • hook transcript noise compaction や resume capsule の実装

Acceptance Criteria

  • AC1: worktree が存在する通常 cleanup は従来どおり worktree_remove → branch_delete の順で動作する
  • AC2: worktree が既に存在しないが branch tip と PR head OID が一致する場合、branch-only cleanup が status: ok で完了する。branch-only candidate 判定は以下 5 条件を全て満たす場合のみ:
    • (A) requested worktree realpath が <repo>/.claude/worktrees/ 配下である
    • (B) requested worktree path が filesystem 上に存在しない
    • (C) git worktree list --porcelain -z に同じ path のエントリが残っていない
    • (D) git worktree list --porcelain -z に同じ branch を使う別 worktree が存在しない
    • (E) local refs/heads/<branch_name> が存在する
  • AC3: branch tip と PR head OID が不一致の場合は pr_head_oid_mismatch で拒否する
  • AC4: requested worktree path の realpath が .claude/worktrees/ 配下でない場合は branch-only lane を拒否する
  • AC5: cross-repo PR / base branch mismatch / linked issue mismatch は branch-only lane でも拒否する
  • AC6: cleanup report に worktree_absent_after_removal: true を記録できる
  • AC7: branch-only force delete は cleanup_exec 内部 subprocess のみが実行でき、以下を全て満たす場合のみ git branch -D に昇格する: PR merged / same repo / base default / head branch match / head OID == local branch tip / linked issue match / branch not checked out anywhere。branch_only_force_delete_denied reason code と actions_taken: ["branch_delete"] が定義される
  • AC8: agent-facing の bare git branch -D は引き続き hook で deny される(既存 AC のまま)
  • AC9: cleanup_exec の CLI 形状を変更しない(新 flag を追加しない)。既存 --pr-number / --linked-issue-number / --worktree-path / --branch-name / --json のみで branch-only lane を自動判定する

Verification Commands

# AC1
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k normal_cleanup

# AC2
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k branch_only_candidate

# AC3
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k oid_mismatch

# AC4
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k path_constraint

# AC5
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k cross_repo

# AC6
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k absent_after_removal

# AC7
$ uv run pytest tests/codex/test_cleanup_exec_branch_only.py -k force_delete

# AC8
$ uv run pytest tests/codex/test_local_main_branch_guard.py -k cleanup

# AC9
# baseline-expect: pass
$ test -f scripts/agent-ops/cleanup_exec.py

Branch-Only Candidate Definition

branch-only cleanup は以下の条件を全て満たす場合のみ許可される:

  • requested worktree realpath は <repo>/.claude/worktrees/ 配下
  • requested worktree path は filesystem 上に存在しない
  • git worktree list --porcelain -z に同 path のエントリが残っていない
  • git worktree list --porcelain -z に同 branch を使う別 worktree が存在しない
  • local refs/heads/<branch_name> が存在する
  • root checkout がデフォルト branch にある
  • gh pr view --repo <trusted repo> で state == MERGED かつ mergedAt が存在する
  • PR headRefName == branch_name
  • PR headRefOid == local refs/heads/<branch_name>
  • PR baseRefName == default_branch
  • PR は cross-repository でなく head owner が trusted repo owner と一致する
  • linked_issue_number(指定時)が closingIssuesReferences に含まれる

Additional Reason Codes

  • worktree_still_in_catalog
  • branch_checked_out_in_worktree
  • local_branch_missing
  • pr_head_oid_mismatch
  • branch_only_force_delete_denied
  • branch_only_materialize_denied

Verified Fields(cleanup report に含む)

  • branch_only_candidate
  • worktree_path_under_worktrees_dir
  • worktree_absent_on_disk
  • worktree_absent_from_catalog
  • branch_absent_from_worktree_catalog
  • local_branch_exists
  • local_branch_tip_oid
  • pr_head_oid
  • head_oid_match
  • branch_only_force_delete_used

Allowed Paths

  • scripts/agent-ops/cleanup_exec.py
  • scripts/agent-ops/cleanup_contract_v3.py
  • scripts/agent-ops/guard_preflight.py
  • .claude/skills/post-merge-cleanup/SKILL.md
  • .claude/hooks/tests/test_worktree_scope_guard.py
  • tests/codex/test_cleanup_exec_branch_only.py
  • tests/codex/test_local_main_branch_guard.py
  • docs/dev/hook-boundaries.md

Stop Conditions

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

Required Skills

  • Python 3
  • Git / GitHub CLI

Scope Delta(任意)

N/A

Runtime Verification Applicability

  • decision: not_applicable
  • reason: cleanup executor・guard・skill・テスト更新で完結し、静的検証と pytest / CLI help で受け入れ可能
  • if immediate: N/A
  • if deferred: N/A

Delivery Rule

1 Issue = 1 PR。cleanup_exec を正本 executor とし、branch-only lane でも bare git cleanup を agent に直接実行させない。

Activity

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

    @squne121
    OwnerAuthor

    判定: REQUEST_CHANGES

    Issue #1196 の方向性は正しいです。現行 cleanup_exec は、git worktree remove 成功後に git branch -d が失敗すると actions_taken=["worktree_remove"] を保持したまま error を返すため、「worktree は消えたが branch だけ残る」partial cleanup が実際に起きます。 ただし、現Issueのまま実装すると force delete 境界、V3 materialize 経路、preflight 表示、検証コマンド が曖昧で、#1139/#1143 で潰した認可境界の事故を再導入するリスクがあります。

    Issue #1196 自体にはコメントはありません。

    前提として確認した関連経緯

    #1147 では、post-merge-cleanup-worker が root checkout 上で直接 git checkout main / git pull / git branch -d / git worktree remove を実行しようとして local_main_branch_guard に止められる問題が整理されています。根本原因は、cleanup 手順が root checkout からの直接 mutation を前提にしていた一方、guard はそれを意図どおり拒否していたことです。

    #1137 はこの反省から、cleanup の認可境界を単一 cleanup_exec に集約し、PR merged / head branch / linked issue / worktree catalog / branch / root default / clean state を検証してから内部 subprocess で削除する設計に寄せています。 #1139 はその前身PRでしたが、実hook経路を通らないテスト、invalid V3→V2 downgrade、preflight の catalog 未使用、Codex route 不成立などで OWNER REQUEST_CHANGES を受けています。 その後 #1143 が、単一 cleanup_exec、one-shot V3、三値ローダ、実hook統合、Codex guarded route を実装して merged されています。

    現行 cleanup_exec は、まず root default、worktree catalog entry、branch match、.claude/worktrees/ 配下チェック、worktree clean を検証し、その後に PR merged / head branch / cross-repo / base branch / head OID / linked issue を検証します。 つまり worktree が既に消えて catalog に無い状態では、現状は WORKTREE_NOT_IN_CATALOG で止まり、PR head OID の照合まで到達しません。#1196 が解くべき穴はここです。

    P0 ブロッカー

    1. 「worktree が存在しない」の定義が甘い

    #1196 の AC2 は「worktree が既に存在しないが branch tip と PR head OID が一致する場合」としていますが、これは不足です。

    Git 公式仕様では、作業ツリーを git worktree remove ではなく手動削除した場合、管理情報は残り、後で自動または git worktree prune で除去されます。また git worktree remove は clean な worktree だけを削除し、dirty/submodule 付きなら force が必要です。(Git)

    したがって branch-only lane は最低限、次を別々に判定すべきです。

    A. requested worktree realpath が <repo>/.claude/worktrees/ 配下か
    B. requested path が filesystem 上に無いか
    C. git worktree list --porcelain -z に同じ path が残っていないか
    D. git worktree list --porcelain -z に同じ branch を使う別 worktree が無いか
    E. local refs/heads/<branch_name> が存在するか
    

    path が無い だけで git branch -D に進むのは危険です。catalog に stale/prunable entry が残っているなら worktree_still_in_catalog などで拒否し、git worktree prune をこのIssueに入れるなら別途 AC と guard テストが必要です。

    2. git branch -D 昇格条件が安全境界そのもの

    現行 _perform() は git branch -d を使っています。 しかし Git 公式仕様上、-d は fully merged な branch の削除で、-D は --delete --force のショートカットです。--force 付き delete は merged 判定や valid commit 判定を無視します。(Git)

    つまり #1196 が squash merge 後の branch 残存を解きたいなら、内部的に git branch -D へ昇格する可能性があります。これは妥当になり得ますが、agent に bare git branch -D を許すこととは別物です。現行 hook は git branch -D / --force / -f / multi-target -d を force delete として検出し、cleanup block に倒す実装を持っています。 また _decide_bash でも force branch delete は branch_force_delete_denied で拒否されます。

    必要な仕様は次です。

    branch-only force delete は cleanup_exec 内部 subprocess だけが実行できる。
    agent-facing Bash の bare git branch -D は引き続き deny。
    -D 昇格前に、PR merged / same repo / base default / head branch match / head OID == local branch tip / linked issue match / branch not checked out anywhere を全て満たす。
    

    branch_only_force_delete_denied を AC に入れるなら、「どの入力が force を要求するのか」も定義してください。CLI に force flag を追加しないなら、この reason は cleanup_exec 内部の defensive reason であり、hook 側は既存 branch_force_delete_denied のままでよいです。

    3. verify_cleanup_authorization() を緩めると materialize_cleanup_contract.py まで広がる

    これは最も見落としやすいです。

    materialize_cleanup_contract.py は V3 contract を発行する前に、cleanup_exec.verify_cleanup_authorization() と同じ検証を呼びます。 したがって #1196 で verify_cleanup_authorization() を「worktree absent でも branch-only OK」に緩めると、意図せず materialize --operation branch_delete の V3 bare-git route まで branch-only を許可する可能性があります。

    #1196 の Allowed Paths には materialize_cleanup_contract.py が含まれていません。つまり今回の正本は cleanup_exec の branch-only resume であり、V3 bare-git contract 発行まで広げるべきではありません。

    推奨修正:

    verify_cleanup_authorization(req, *, allow_branch_only: bool = False)
    

    または通常 cleanup と branch-only cleanup を別関数に分けるべきです。materialize_cleanup_contract.py は allow_branch_only=False のまま維持し、#1196 の branch-only は cleanup_exec.run() からだけ到達させるのが安全です。

    4. CLI flag を増やすなら hook allowlist が壊れる

    現行 worktree_scope_guard.py は cleanup_exec.py の agent-facing argv を厳密に allowlist しています。許可される value flags は --pr-number / --linked-issue-number / --worktree-path / --branch-name、bool は --json です。 さらに unknown flag、重複、--flag=value、余剰 positional を拒否します。

    #1196 の Allowed Paths には .claude/hooks/worktree_scope_guard.py がありません。したがって cleanup_exec.py --branch-only や --resume のような新flagを追加すると、実hook経路で deny されます。

    推奨は 既存CLI形状のまま自動判定 です。

    uv run python3 scripts/agent-ops/cleanup_exec.py \
      --pr-number N \
      --linked-issue-number M \
      --worktree-path <old path> \
      --branch-name <branch> \
      --json
    

    この同じ呼び出しで、通常 lane と branch-only lane を cleanup_exec 内部が決定する形にしてください。新flagが必要なら、Allowed Paths に .claude/hooks/worktree_scope_guard.py を追加し、実hook process test も必須です。

    5. Verification Commands が hollow

    #1196 の AC2 / AC6 / AC7 が cleanup_exec.py --help で検証されるのは不十分です。help 出力は branch-only の安全性、status、reason、actions、verified field を何も保証しません。

    #1139 が失敗した主要因の一つは、実hook経路や実worktree catalog を通さないテストで「通ったように見えた」ことです。#1143 では実hook stdin JSON、実git worktree、one-shot replay、Codex route まで検証して修正されています。

    #1196 も --help ではなく、最低限以下を pytest 化してください。

    ケース | 期待 -- | -- 通常 cleanup | actions_taken == ["worktree_remove", "branch_delete"] worktree remove 成功後 branch delete 失敗 | status: error, actions_taken == ["worktree_remove"] rerun: worktree absent + branch tip == PR head OID | status: ok, actions_taken == ["branch_delete"] branch tip != PR head OID | status: refused, reason_code: pr_head_oid_mismatch, branch remains requested path outside .claude/worktrees/ | GH call 前に拒否 cross-repo PR | pr_head_repo_mismatch base branch mismatch | pr_base_branch_mismatch linked issue mismatch | linked_issue_mismatch catalog に同branchの別worktreeあり | branch_checked_out_in_worktree などで拒否 bare git branch -D | Claude hook / Codex hook とも deny

    P1 改善

    guard_preflight.py は現在、active worktree が実 catalog に存在することを前提に matches を返します。 branch-only resume は「worktree が無いが cleanup_exec で安全に branch だけ消せる」状態なので、active_worktree_state: mismatch に混ぜると誤誘導します。

    追加するなら、次のような別フィールドがよいです。

    {
      "cleanup_resume_state": "branch_only_candidate",
      "allowed_next_commands": [
        {
          "action": "run_cleanup_exec",
          "detail": "worktree is absent but local branch can be verified against merged PR head"
        }
      ]
    }
    

    run_gated_cleanup は使わない方がよいです。現行の V3 bare-git route は contract が catalog entry に bind する前提なので、branch-only とは性質が違います。

    AC 追記案

    Issue #1196 には以下を追記すると実装品質が上がります。

    ### Branch-only candidate definition
    

    branch-only cleanup is allowed only when all conditions below are true:

    • requested worktree realpath is under &lt;repo&gt;/.claude/worktrees/

    • requested worktree path is absent on disk

    • git worktree list --porcelain -z has no entry for the requested worktree realpath

    • git worktree list --porcelain -z has no entry whose branch is refs/heads/&lt;branch_name&gt;

    • local refs/heads/&lt;branch_name&gt; exists

    • root checkout is on the default branch

    • gh pr view --repo &lt;trusted repo&gt; reports state == MERGED and mergedAt is present

    • PR headRefName == branch_name

    • PR headRefOid == local refs/heads/&lt;branch_name&gt;

    • PR baseRefName == default_branch

    • PR is not cross-repository and head owner matches the trusted repo owner

    • supplied linked_issue_number, if any, is included in closingIssuesReferences

    gh pr view --json は headRefName、headRefOid、baseRefName、headRepositoryOwner、isCrossRepository、mergedAt、state、closingIssuesReferences などを取得できます。(GitHub CLI) 現行 cleanup_exec もこれらのフィールドを使っています。

    ### Additional reason codes

  3. worktree_still_in_catalog

  4. branch_checked_out_in_worktree

  5. local_branch_missing

  6. pr_head_oid_mismatch

  7. branch_only_force_delete_denied

  8. branch_only_materialize_denied

  9. ### Verified fields

    The cleanup report must include:

    • branch_only_candidate
    • worktree_path_under_worktrees_dir
    • worktree_absent_on_disk
    • worktree_absent_from_catalog
    • branch_absent_from_worktree_catalog
    • local_branch_exists
    • local_branch_tip_oid
    • pr_head_oid
    • head_oid_match
    • branch_only_force_delete_used

    実装方針

    最も安全な実装は次です。

    1. cleanup_exec の既存 CLI は変えない。

    2. verify_cleanup_authorization() は通常 cleanup 用のまま維持する。

    3. run() 内で通常 verifier が WORKTREE_NOT_IN_CATALOG になった場合だけ、branch-only verifier に分岐する。

    4. branch-only verifier は PR/head/local branch/catalog/path を全検証する。

    5. branch-only 実行は git -C <root> branch -D <branch_name> を内部 subprocess 配列でのみ実行する。

    6. 結果に actions_taken: ["branch_delete"] と branch-only verified fields を出す。

    7. materialize_cleanup_contract.py からは branch-only verifier に到達させない。

    結論として、#1196 は必要なIssueですが、現状のACでは「branchだけ残った partial cleanup を安全に終わらせる」よりも「force delete の抜け道を増やす」方向へ倒れやすいです。上記の state definition、materialize 分離、実hook/pytest 検証を入れてから実装に進めるべきです。

    ## 判定: REQUEST_CHANGES

    Issue #1196 の方向性は正しいです。現行 cleanup_exec は、git worktree remove 成功後に git branch -d が失敗すると actions_taken=["worktree_remove"] を保持したまま error を返すため、「worktree は消えたが branch だけ残る」partial cleanup が実際に起きます。 ただし、現Issueのまま実装すると force delete 境界、V3 materialize 経路、preflight 表示、検証コマンド が曖昧で、#1139/#1143 で潰した認可境界の事故を再導入するリスクがあります。

    Issue #1196 自体にはコメントはありません。

    前提として確認した関連経緯

    #1147 では、post-merge-cleanup-worker が root checkout 上で直接 git checkout main / git pull / git branch -d / git worktree remove を実行しようとして local_main_branch_guard に止められる問題が整理されています。根本原因は、cleanup 手順が root checkout からの直接 mutation を前提にしていた一方、guard はそれを意図どおり拒否していたことです。

    #1137 はこの反省から、cleanup の認可境界を単一 cleanup_exec に集約し、PR merged / head branch / linked issue / worktree catalog / branch / root default / clean state を検証してから内部 subprocess で削除する設計に寄せています。 #1139 はその前身PRでしたが、実hook経路を通らないテスト、invalid V3→V2 downgrade、preflight の catalog 未使用、Codex route 不成立などで OWNER REQUEST_CHANGES を受けています。 その後 #1143 が、単一 cleanup_exec、one-shot V3、三値ローダ、実hook統合、Codex guarded route を実装して merged されています。

    現行 cleanup_exec は、まず root default、worktree catalog entry、branch match、.claude/worktrees/ 配下チェック、worktree clean を検証し、その後に PR merged / head branch / cross-repo / base branch / head OID / linked issue を検証します。 つまり worktree が既に消えて catalog に無い状態では、現状は WORKTREE_NOT_IN_CATALOG で止まり、PR head OID の照合まで到達しません。#1196 が解くべき穴はここです。

    P0 ブロッカー

    1. 「worktree が存在しない」の定義が甘い

    #1196 の AC2 は「worktree が既に存在しないが branch tip と PR head OID が一致する場合」としていますが、これは不足です。

    Git 公式仕様では、作業ツリーを git worktree remove ではなく手動削除した場合、管理情報は残り、後で自動または git worktree prune で除去されます。また git worktree remove は clean な worktree だけを削除し、dirty/submodule 付きなら force が必要です。([Git]1)

    したがって branch-only lane は最低限、次を別々に判定すべきです。

    A. requested worktree realpath が <repo>/.claude/worktrees/ 配下か
    B. requested path が filesystem 上に無いか
    C. git worktree list --porcelain -z に同じ path が残っていないか
    D. git worktree list --porcelain -z に同じ branch を使う別 worktree が無いか
    E. local refs/heads/<branch_name> が存在するか
    

    path が無い だけで git branch -D に進むのは危険です。catalog に stale/prunable entry が残っているなら worktree_still_in_catalog などで拒否し、git worktree prune をこのIssueに入れるなら別途 AC と guard テストが必要です。

    2. git branch -D 昇格条件が安全境界そのもの

    現行 _perform() は git branch -d を使っています。 しかし Git 公式仕様上、-d は fully merged な branch の削除で、-D は --delete --force のショートカットです。--force 付き delete は merged 判定や valid commit 判定を無視します。([Git]2)

    つまり #1196 が squash merge 後の branch 残存を解きたいなら、内部的に git branch -D へ昇格する可能性があります。これは妥当になり得ますが、agent に bare git branch -D を許すこととは別物です。現行 hook は git branch -D / --force / -f / multi-target -d を force delete として検出し、cleanup block に倒す実装を持っています。 また _decide_bash でも force branch delete は branch_force_delete_denied で拒否されます。

    必要な仕様は次です。

    branch-only force delete は cleanup_exec 内部 subprocess だけが実行できる。
    agent-facing Bash の bare git branch -D は引き続き deny。
    -D 昇格前に、PR merged / same repo / base default / head branch match / head OID == local branch tip / linked issue match / branch not checked out anywhere を全て満たす。
    

    branch_only_force_delete_denied を AC に入れるなら、「どの入力が force を要求するのか」も定義してください。CLI に force flag を追加しないなら、この reason は cleanup_exec 内部の defensive reason であり、hook 側は既存 branch_force_delete_denied のままでよいです。

    3. verify_cleanup_authorization() を緩めると materialize_cleanup_contract.py まで広がる

    これは最も見落としやすいです。

    materialize_cleanup_contract.py は V3 contract を発行する前に、cleanup_exec.verify_cleanup_authorization() と同じ検証を呼びます。 したがって #1196 で verify_cleanup_authorization() を「worktree absent でも branch-only OK」に緩めると、意図せず materialize --operation branch_delete の V3 bare-git route まで branch-only を許可する可能性があります。

    #1196 の Allowed Paths には materialize_cleanup_contract.py が含まれていません。つまり今回の正本は cleanup_exec の branch-only resume であり、V3 bare-git contract 発行まで広げるべきではありません。

    推奨修正:

    verify_cleanup_authorization(req, *, allow_branch_only: bool = False)

    または通常 cleanup と branch-only cleanup を別関数に分けるべきです。materialize_cleanup_contract.py は allow_branch_only=False のまま維持し、#1196 の branch-only は cleanup_exec.run() からだけ到達させるのが安全です。

    4. CLI flag を増やすなら hook allowlist が壊れる

    現行 worktree_scope_guard.py は cleanup_exec.py の agent-facing argv を厳密に allowlist しています。許可される value flags は --pr-number / --linked-issue-number / --worktree-path / --branch-name、bool は --json です。 さらに unknown flag、重複、--flag=value、余剰 positional を拒否します。

    #1196 の Allowed Paths には .claude/hooks/worktree_scope_guard.py がありません。したがって cleanup_exec.py --branch-only や --resume のような新flagを追加すると、実hook経路で deny されます。

    推奨は 既存CLI形状のまま自動判定 です。

    uv run python3 scripts/agent-ops/cleanup_exec.py \
      --pr-number N \
      --linked-issue-number M \
      --worktree-path <old path> \
      --branch-name <branch> \
      --json

    この同じ呼び出しで、通常 lane と branch-only lane を cleanup_exec 内部が決定する形にしてください。新flagが必要なら、Allowed Paths に .claude/hooks/worktree_scope_guard.py を追加し、実hook process test も必須です。

    5. Verification Commands が hollow

    #1196 の AC2 / AC6 / AC7 が cleanup_exec.py --help で検証されるのは不十分です。help 出力は branch-only の安全性、status、reason、actions、verified field を何も保証しません。

    #1139 が失敗した主要因の一つは、実hook経路や実worktree catalog を通さないテストで「通ったように見えた」ことです。#1143 では実hook stdin JSON、実git worktree、one-shot replay、Codex route まで検証して修正されています。

    #1196 も --help ではなく、最低限以下を pytest 化してください。

    ケース 期待
    通常 cleanup actions_taken == ["worktree_remove", "branch_delete"]
    worktree remove 成功後 branch delete 失敗 status: error, actions_taken == ["worktree_remove"]
    rerun: worktree absent + branch tip == PR head OID status: ok, actions_taken == ["branch_delete"]
    branch tip != PR head OID status: refused, reason_code: pr_head_oid_mismatch, branch remains
    requested path outside .claude/worktrees/ GH call 前に拒否
    cross-repo PR pr_head_repo_mismatch
    base branch mismatch pr_base_branch_mismatch
    linked issue mismatch linked_issue_mismatch
    catalog に同branchの別worktreeあり branch_checked_out_in_worktree などで拒否
    bare git branch -D Claude hook / Codex hook とも deny

    P1 改善

    guard_preflight.py は現在、active worktree が実 catalog に存在することを前提に matches を返します。 branch-only resume は「worktree が無いが cleanup_exec で安全に branch だけ消せる」状態なので、active_worktree_state: mismatch に混ぜると誤誘導します。

    追加するなら、次のような別フィールドがよいです。

    {
      "cleanup_resume_state": "branch_only_candidate",
      "allowed_next_commands": [
        {
          "action": "run_cleanup_exec",
          "detail": "worktree is absent but local branch can be verified against merged PR head"
        }
      ]
    }

    run_gated_cleanup は使わない方がよいです。現行の V3 bare-git route は contract が catalog entry に bind する前提なので、branch-only とは性質が違います。

    AC 追記案

    Issue #1196 には以下を追記すると実装品質が上がります。

    ### Branch-only candidate definition
    
    branch-only cleanup is allowed only when all conditions below are true:
    
    - requested worktree realpath is under `<repo>/.claude/worktrees/`
    - requested worktree path is absent on disk
    - `git worktree list --porcelain -z` has no entry for the requested worktree realpath
    - `git worktree list --porcelain -z` has no entry whose branch is `refs/heads/<branch_name>`
    - local `refs/heads/<branch_name>` exists
    - root checkout is on the default branch
    - `gh pr view --repo <trusted repo>` reports `state == MERGED` and `mergedAt` is present
    - PR `headRefName == branch_name`
    - PR `headRefOid == local refs/heads/<branch_name>`
    - PR `baseRefName == default_branch`
    - PR is not cross-repository and head owner matches the trusted repo owner
    - supplied `linked_issue_number`, if any, is included in `closingIssuesReferences`

    gh pr view --json は headRefName、headRefOid、baseRefName、headRepositoryOwner、isCrossRepository、mergedAt、state、closingIssuesReferences などを取得できます。([GitHub CLI]3) 現行 cleanup_exec もこれらのフィールドを使っています。

    ### Additional reason codes
    
    - `worktree_still_in_catalog`
    - `branch_checked_out_in_worktree`
    - `local_branch_missing`
    - `pr_head_oid_mismatch`
    - `branch_only_force_delete_denied`
    - `branch_only_materialize_denied`
    ### Verified fields
    
    The cleanup report must include:
    
    - `branch_only_candidate`
    - `worktree_path_under_worktrees_dir`
    - `worktree_absent_on_disk`
    - `worktree_absent_from_catalog`
    - `branch_absent_from_worktree_catalog`
    - `local_branch_exists`
    - `local_branch_tip_oid`
    - `pr_head_oid`
    - `head_oid_match`
    - `branch_only_force_delete_used`

    実装方針

    最も安全な実装は次です。

    1. cleanup_exec の既存 CLI は変えない。
    2. verify_cleanup_authorization() は通常 cleanup 用のまま維持する。
    3. run() 内で通常 verifier が WORKTREE_NOT_IN_CATALOG になった場合だけ、branch-only verifier に分岐する。
    4. branch-only verifier は PR/head/local branch/catalog/path を全検証する。
    5. branch-only 実行は git -C <root> branch -D <branch_name> を内部 subprocess 配列でのみ実行する。
    6. 結果に actions_taken: ["branch_delete"] と branch-only verified fields を出す。
    7. materialize_cleanup_contract.py からは branch-only verifier に到達させない。

    結論として、#1196 は必要なIssueですが、現状のACでは「branchだけ残った partial cleanup を安全に終わらせる」よりも「force delete の抜け道を増やす」方向へ倒れやすいです。上記の state definition、materialize 分離、実hook/pytest 検証を入れてから実装に進めるべきです。

  10. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Human Escalation Required

    The loop has terminated and requires human review. Cause: human judgment required

    Blockers

    "scope_signal_guard: new_allowed_path_layer (Allowed Paths に .claude/ 配下のパスを含む)"
    
    "ANCHOR_SCOPE_REFRAME_V1 payload が anchor comment に存在しないため scope を承認できない"
    
    "Issue body は reviewer に APPROVE 済み (C1-C13 全 PASS、P0-1〜P0-5 解決済み)"
    
  11. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor
    schema_version: ANCHOR_SCOPE_REFRAME_V1
    target:
      repo: squne121/loop-protocol
      issue_number: 1196
    decision: approve_scope_delta
    allowed_path_deltas:
      - ".claude/hooks/tests/test_worktree_scope_guard.py"
      - ".claude/skills/post-merge-cleanup/SKILL.md"
    rationale: "OWNER による .claude/ 配下 Allowed Paths の明示的承認"
    required_rerun:
      - contract_review
      - refinement_preflight
      - allowed_paths_gate
  12. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  13. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-27T06:19:18Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1196
      body_sha256: "sha256:d2ad5d103ca2af5c49849823929e851eb95f912e1de6e15a6ae0a187985ca00c"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  14. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    実装完了

    PR #1206 (Draft) を作成しました: #1206

    VC 結果 (全 AC pass)

    • AC1 normal_cleanup: 4 passed
    • AC2 branch_only_candidate: 4 passed
    • AC3 oid_mismatch: 2 passed
    • AC4 path_constraint: 2 passed
    • AC5 cross_repo: 4 passed
    • AC6 absent_after_removal: 3 passed
    • AC7 force_delete: 5 passed
    • AC8 regression gate: 6 passed
    • AC9 CLI shape: 3 passed

    合計 30 new tests + 6 existing regression tests = all pass

  15. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    test review posted via issue comment

  16. added a commit that references this issue on Jun 27, 2026
  17. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    test

  18. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor
    schema_version: ANCHOR_SCOPE_REFRAME_V1
    target:
      repo: squne121/loop-protocol
      issue_number: 1196
    decision: approve_scope_delta
    allowed_path_deltas:
      - ".github/ci/python-test-plan.json"
    rationale: "tests/codex/test_cleanup_exec_branch_only.py の CI test plan 登録に必要。generate_ci_test_selection_artifact.py が changed but uncovered として B1/B2 CI 失敗を引き起こすため。"
    required_rerun:
      - allowed_paths_gate
  19. added 3 commits that reference this issue on Jun 27, 2026
  20. added a commit that references this issue on Jun 27, 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/implementation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions