Skip to content

実装: post-merge cleanup guard が issue worktree を repository root と誤認する root selection を是正する #2979

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "post-merge-cleanup: canonical repository root selection"
change_kind: workflow
dedupe_key: "post-merge-cleanup:guard-canonical-root-selection"

Parent Issue

none

Parent Goal Ref

post-merge cleanup の guard / executor が、Claude Code session の current worktree に依存せず canonical repository root を判定し、安全な cleanup を false-negative で停止しないこと。

Current Validated Scope

今回の failure class は cleanup policy の緩和ではなく repository root selection の誤り。

現行 scripts/agent-ops/guard_preflight.py::resolve_project_root() と scripts/agent-ops/cleanup_exec.py::resolve_project_root() は、CLAUDE_PROJECT_DIR があればその realpath、無ければ実行中 script の配置元を repository root とみなす。

interactive Claude Code session が issue worktree に固定されている場合、CLAUDE_PROJECT_DIR / script location のどちらも issue worktree を指し得る。この状態で guard_preflight.py --json を通常どおり実行すると、issue branch を root branch として観測し、canonical root checkout が main/clean でも root_branch_state: drifted / active_worktree_state: mismatch / status: human_required に落ちる。

また select_issue_worktree(..., root_realpath) は root とみなした entry を除外するため、issue worktree 自身を誤って root にした場合は active issue の catalog entry も見失い、mismatch を増幅する。

追加で OWNER 敵対的レビュー(https://github.com/squne121/loop-protocol/issues/2979#issuecomment-6047844759)が、root resolver だけ直しても次の 4 点が残ることを指摘した(base 8561d147 で実機・コード確認済み)。

  • linked worktree から git checkout main すると、primary が main を checkout 済みのため fatal: 'main' is already used by worktree で exit 128 になる(post-merge-cleanup-executor Step 2 が issue worktree session から完遂できない)。
  • guard_preflight.build_preflight() は list_worktrees() が None(git 失敗)を返しても catalog = [] に変換し、root が default なら status=ok になり得る。worktree_catalog.list_worktrees() は git_common_dir を 1 回だけ求めて全 entry に複写するため、entry 同士の比較は独立検証にならない。
  • _active_issue() は cwd の basename または root branch 名でのみ Issue 番号を推定するため、issue-N-slug/src のような nested cwd では active Issue を見失う。
  • 現行の -k 付き VC は対象テスト名が未存在のため pytest exit 5(no tests collected)になり、欠陥再現ではなく未収集を baseline fail の根拠にしている。

既存テストは build_preflight(project_root=str(repo["root"]), cwd=str(repo["root"])) のように canonical root を直接注入しており、実 CLI が issue worktree から root を自己解決する production path を固定していない。

Remaining Parent Gaps

  • guard / cleanup 以外の repository-root resolver 全体を統一する一般化は本 Issue の範囲外。
  • Claude Code / Claude-GPT の project-dir semantics 自体を変更しない。
  • cleanup policy(root drift 時の human_required、dirty worktree の拒否等)は変更しない。

Source

Outcome

issue worktree 内で起動された interactive session から post-merge cleanup を実行しても、guard / cleanup executor は current worktree と canonical repository primary root を分離して判定する。

canonical root checkout が default branch で clean、active issue worktree が catalog 上で正しく存在する場合、guard_preflight.py --json は issue worktree を root と誤認せず root_branch_state: default / active_worktree_state: matches を返す。

一方、canonical root checkout 自体が本当に drift / detached / ambiguous な場合は、既存の fail-closed / human-required semantics を維持する。

Problem

再現条件:

  1. canonical root checkout は main かつ clean。
  2. linked issue worktree が存在し、その worktree branch 上で interactive session が起動している。
  3. session の CLAUDE_PROJECT_DIR または script resolution が issue worktree を指す。
  4. .claude/skills/post-merge-cleanup-executor/SKILL.md 記載どおり uv run --locked python3 scripts/agent-ops/guard_preflight.py --json を実行する。

現状は issue worktree を root として評価し、実際の canonical root が正常でも以下の false result を返し得る:

root_branch_state: drifted
active_worktree_state: mismatch
status: human_required
blocked_reason_codes:
  - root_drift_active_worktree_mismatch

この false-human-required に対して cleanup を再実行・override するのは不適切であり、正規 root から preflight をやり直す手動回復が必要になっている。

Design Requirements

  1. root と cwd を分離する
    cwd / session project dir は「現在どの issue worktree から呼ばれたか」の観測に使ってよいが、canonical repository root の authority にしない。

  2. Git repository identity から primary root を解決する
    linked worktree からでも同じ git common-dir / git worktree list --porcelain -z catalog を使って canonical primary worktree を決定する。branch 名を root identity の代用にせず、primary worktree を解決してからその branch を default branch と比較する。

  3. shared resolution
    guard_preflight と cleanup_exec(および cleanup_exec.resolve_project_root を利用する materialize lane)が同じ root-resolution semantics を使い、preflight が ok でも executor が別 root を見て拒否する divergence を作らない。

  4. fail closed
    primary root が一意に解決できない / catalog 取得に失敗する / git common-dir が不整合な場合、issue worktree を silently root 扱いしない。structured reason で blocked / human_required に倒す。

  5. 既存 policy は維持
    canonical root 自体が本当に drifted の場合の root_drift_active_worktree_mismatch、dirty worktree 拒否、PR merged / branch / linked issue 検証は緩和しない。

  6. catalog / identity は独立に検証する
    git worktree list --porcelain -z の先頭 entry を primary とし、linked worktree と primary root の双方で独立に git rev-parse --git-common-dir を実行して realpath 比較する(相対パスで返る場合は実行 cwd 基準で解決する)。catalog producer が全 entry に複写した field 同士の比較を独立検証とみなさない。bare repository など先頭 entry が work tree を持たない場合は primary 不在として structured failure にする。この検証は worktree_catalog.py への additive な共有 helper として実装し、既存 list_worktrees() の戻り値(失敗時 None、git_common_dir field の複写)と署名は変更しない(worktree_bootstrap_exec / git_worktree_probe / verified_*_merge_exec など Allowed Paths 外の既存 consumer を壊さない)。

  7. cleanup 全工程の実行 cwd を分離する
    main 同期は canonical primary root に対して、root identity / state の gate(guard_preflight.py --json が root_branch_state=default)を通過した場合のみ行う。drift した primary を preflight より前に切り替えない。現在 session が削除対象 worktree 上にいる場合は、削除後に無効 cwd で後続 Step を実行しない。既存の cleanup_exec 認可境界は維持する。

  8. 失敗は固定 reason code で返す
    新規 reason code は次の固定 literal とし、既存の拒否応答 field(guard は blocked_reason_codes、executor は既存の拒否 reason)に載せる: worktree_catalog_unavailable / git_common_dir_unavailable / primary_root_unresolved / repository_identity_mismatch / active_issue_catalog_conflict。新しい schema・field 群は追加しない。

Acceptance Criteria

  • AC1: issue worktree を cwd とし、CLAUDE_PROJECT_DIR も issue worktree を指す real-Git fixture から public CLI guard_preflight.py --json を起動しても、canonical primary root が main/default の場合は root_branch_state=default、active_worktree_state=matches、status=ok になる。
  • AC2: AC1 の fixture で canonical root checkout 自体を non-default branch に drift させると、従来どおり fail-closed / human-required になり、issue worktree 側の branch 状態で誤って ok にならない。
  • AC3: root resolver は linked worktree / canonical root の双方から同一 primary root realpath を返す。identity 検証は、linked worktree と primary root のそれぞれで独立に git rev-parse --git-common-dir を subprocess 実行して得た realpath 同士を比較する。negative control は、primary 候補を別 repository(異なる common-dir)の path へ差し替える、または helper の test seam(catalog 引数 / module-level の list_worktrees patch)で foreign common-dir を注入したとき、repository_identity_mismatch で非成功になること(catalog entry の git_common_dir field を改ざんしても結果が変わらないことは仕様であり、field 同士の比較で PASS しないことの根拠とする)。list_worktrees() の署名・戻り値は不変で、helper は additive であること。
  • AC4: primary root を一意に解決できない / catalog failure の場合、issue worktree fallback を行わず structured failure を返す。
  • AC5: cleanup_exec の public CLI でも issue-worktree session から同じ canonical root を使う。preflight が root 正常と判定した直後に executor が root_not_default_branch だけを理由に拒否する divergence がない。
  • AC6: existing 実装: guard arbitration preflight と cleanup contract delivery を機械化する (agent-ops) #1137 semantics(real catalog、cwd classification、root drift policy、cleanup authorization checks)を回帰させない。
  • AC7: post-merge-cleanup-executor の documented invocation を実際に通す integration regression を追加し、test helper が project_root=canonical_root を直接注入するだけの false-green にしない。
  • AC8: full workflow reachability(post-merge-cleanup-executor/SKILL.md の記述と test が同一の command 列を使う)。issue worktree session から real-Git fixture(primary root + linked issue worktree + local bare origin。GitHub API 呼び出し gh のみ PATH 上の fake を許容し、git は実物)で次を満たす。(1) canonical root は git_worktree_probe.py --json の entries[0].worktree_realpath から取得する(guard_preflight の出力 field は追加しない)。(2) Step 2 の main 同期は、先に guard_preflight.py --json が root_branch_state=default を返した場合のみ canonical root に対して行い(linked worktree から git checkout main しない)、drift 時は同期せず human_required のまま停止する。stash / git stash pop は canonical root 自身の変更に対してのみ行い、issue worktree の変更を main へ carry over しない。(3) Step 3 で session の worktree を cleanup_exec 経由で削除した後の Step 4〜8 の command は、canonical root を cwd として実行され(相対 path のまま解決できる)、削除済み cwd に依存しない。test は削除前に cwd を worktree に置いた状態から実行し、削除後に削除済み cwd で同じ command を実行すると失敗する negative control と、canonical root cwd で成功する positive を持つ。(4) 実行する step は 1・2・3・7、gh 非依存の後続 command(fake gh 許容)で、command を持たない Step 6・8 は「削除済み cwd に依存する相対参照を含まない」ことを SKILL.md の静的検査で確認する。(5) SKILL.md の cleanup_exec.py command は相対 path の単一 bash block のまま維持し(実装: cleanup_exec の linked Issue 認可を closing relation 非依存の non_closing_authority に対応させる #2891 の parity test が機械抽出する)、追加する block は scripts/agent-ops/cleanup_exec.py を含めない。cleanup_exec の削除認可(PR merged / branch・OID / linked Issue / target clean / partial cleanup 検証)は緩和しない。
  • AC9: nested cwd identity。cwd が issue worktree 配下のサブディレクトリ(例 .claude/worktrees/issue-N-slug/src)で LOOP_ISSUE_NUMBER 未設定でも、catalog entry の worktree realpath と cwd の realpath containment で active Issue を特定する。primary root は全 worktree の path prefix になり得るため、linked worktree entry を優先し、複数一致する場合は最も深い一致を採用する(primary は他に一致が無い場合のみ)。basename / branch 名だけに依存しない。LOOP_ISSUE_NUMBER が設定され catalog 上の実 identity と矛盾する場合は active_issue_catalog_conflict で非成功にする。canonical root が真に drift している場合は従来どおり human_required / root_drift_active_worktree_mismatch を維持し、root cwd + 明示 Issue 番号の既存挙動は変えない。
  • AC10: catalog failure semantics。list_worktrees() が None を返す場合(worktree_catalog_unavailable)、git common-dir 取得失敗(git_common_dir_unavailable)、primary root 不在(primary_root_unresolved)、repository identity 不一致(repository_identity_mismatch)を、空 catalog や正常として扱わず、guard / executor / materialize lane のそれぞれで structured failure(JSON 応答と非 ok の status / 固定 reason code)を返す。materialize_cleanup_contract.py は Allowed Paths 外のため変更しない: cleanup_exec.resolve_project_root() は例外を投げず best-effort の path を返し、identity 検証は verify_cleanup_authorization / verify_discard_authorization / run / run_discard_check / run_discard_consume の既存の構造化 refusal 経路で行う。materialize lane の test は public CLI(materialize_cleanup_contract.py、既定の verify 経路)を subprocess 実行して JSON と非成功を確認する(verify=False は function-only parameter で CLI からは到達できず、本 AC の対象外とする)。status=ok への false-green を各失敗種別の regression test で防ぐ。新しい approval layer・schema・hook は追加しない。
  • AC11: public CLI parity。root 候補の優先順は --project-root(guard_preflight のみ)→ CLAUDE_PROJECT_DIR → script 位置とし、いずれの候補も独立に git rev-parse --git-common-dir で repository を特定する。入力種別ごとの期待結果は単一とする: 候補が linked worktree なら同一 repository の primary root へ正規化し、候補が Git work tree でない、または独立に得た common-dir が catalog の primary と一致しない場合は拒否(repository_identity_mismatch / primary_root_unresolved)する。cwd の repository との一致は要求しない(既存 AC6 の fixture が cwd と CLAUDE_PROJECT_DIR を別 repository にするため)。CLAUDE_PROJECT_DIR が (a) primary root、(b) issue worktree を指す場合、および (c) 未設定で script が issue worktree 内に存在する場合(fixture は script 一式を fixture repository の tracked tree に含めて linked worktree 内の script を実行する)の全てで、guard_preflight.py / cleanup_exec.py / materialize lane が同一の canonical primary root を評価する。root selection の共有 helper は worktree_catalog.py に置く。
  • AC12: 証跡。新規テストは test_ac<N>_* の命名規約に従い、各 AC に 1 件以上の専用 test を持つ。新規テスト追加後は、(1) 各 AC の -k test_ac<N>_ が 1 件以上収集され PASS すること、(2) 修正前 commit で実行した assertion failure(pytest exit 1)、(3) 修正後 PASS(exit 0)、(4) public CLI の exit code と JSON、(5) negative control、(6) 実行後の worktree / branch / root の状態を PR 本文に記録する。(2)(4)(5)(6) は機械検証 VC を持たない PR 本文証跡であり、pr-review(pr-reviewer)が AC ごとに独立再検証する。exit 5(no tests collected)や exit 4(file not found)を assertion failure の証拠にしない。既存テストの全 PASS だけで AC8〜AC11 の達成を主張しない。RVA の immediate は pytest subprocess による実 CLI 実行証跡のみで充足され、live Claude Code session での skill invocation(procedure_steps_executed_in_declared_order)は本 Issue では unverified のまま PASS と主張しない。

Verification Commands

各 AC の marker は # AC<N> の bare 形式。新規テストは未作成の specific test file を参照し、-k test_ac<N>_ で AC ごとの専用 test を選択する。修正前は file 不在により pytest exit 4(ERROR: file or directory not found)となり expected baseline fail として扱われる。実装後は、当該 AC の test が 1 件も無ければ exit 5 で VC が失敗するため、空疎な test file で全 AC が green になることはない。-k による未存在テスト名の選択(exit 5)を baseline fail の根拠にしない。実装後は、修正前 commit での exit 1(assertion failure)と修正後 exit 0 を AC12 の証跡として別途取得する。

# AC1
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py -k test_ac1_ -q
# AC2
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py -k test_ac2_ -q
# AC3
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py -k test_ac3_ -q
# AC4
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py -k test_ac4_ -q
# AC5
# baseline-expect: fail
$ uv run --locked pytest scripts/agent-ops/tests/test_cleanup_exec_root_selection.py -k test_ac5_ -q
# AC7
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py scripts/agent-ops/tests/test_cleanup_exec_root_selection.py -k test_ac7_ -q
# AC8
# baseline-expect: fail
$ uv run --locked pytest scripts/agent-ops/tests/test_cleanup_exec_root_selection.py -k test_ac8_ -q
# AC9
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py -k test_ac9_ -q
# AC10
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py scripts/agent-ops/tests/test_cleanup_exec_root_selection.py -k test_ac10_ -q
# AC11
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py scripts/agent-ops/tests/test_cleanup_exec_root_selection.py -k test_ac11_ -q
# AC12
# baseline-expect: fail
$ uv run --locked pytest tests/agent_guards/test_guard_preflight_cli_root_selection.py scripts/agent-ops/tests/test_cleanup_exec_root_selection.py --collect-only -q
# AC6
# baseline-expect: pass
$ uv run --locked pytest tests/agent_guards/test_worktree_scope_guard.py scripts/agent-ops/tests/test_cleanup_exec.py scripts/agent-ops/tests/test_cleanup_exec_non_closing_authority.py tests/test_post_merge_cleanup_boundary.py -q

Allowed Paths

  • scripts/agent-ops/guard_preflight.py
  • scripts/agent-ops/cleanup_exec.py
  • scripts/agent-ops/worktree_catalog.py
  • scripts/agent-ops/tests/test_cleanup_exec.py
  • tests/agent_guards/test_worktree_scope_guard.py
  • tests/agent_guards/test_guard_preflight_cli_root_selection.py(新規)
  • scripts/agent-ops/tests/test_cleanup_exec_root_selection.py(新規)
  • .claude/skills/post-merge-cleanup-executor/SKILL.md

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • canonical primary worktree identity を解決するために cleanup policy の緩和が必要になる場合
  • destructive cleanup を guard/preflight の外へ移す必要がある場合
  • Allowed Paths 外の runtime/harness 変更が不可避な場合(scripts/agent-guards/worktree_scope_guard.py の root 解決の統一を含む)
  • CLAUDE_PROJECT_DIR 自体の global semantics 変更が必要になる場合
  • unrelated worktree / branch の削除・変更が必要になる場合

Required Skills

なし。Python / Git worktree / repository-native post-merge-cleanup contract の範囲。

Runtime Verification Applicability

decision: immediate
reason: "Allowed Paths の scripts/agent-ops/** と .claude/skills/post-merge-cleanup-executor/SKILL.md は docs/dev/extension-surface-runtime-policy.yaml の rule skill-invocation-procedure-or-contract-change と subagent-lifecycle-start-stop-delegation-fallback に一致し、いずれも default_decision: immediate である(assertion 単位の not_applicable / compat でも Issue-level の decision 要求は緩まない)。proven_not_runtime_loaded / executable_or_prompt_semantics_unchanged の exception はいずれも成立しない(AC8 は SKILL.md の手順を変更する)。ゲーム runtime の起動は不要だが、documented 8 ステップの実行到達性は real-Git fixture 上で実 CLI を subprocess 実行して観測する。"
applicable_acs: [AC8]
runtime_assertion_bindings:
  - profile: skill-invocation-runtime-smoke
    assertion: procedure_steps_executed_in_declared_order
    disposition: non_dispositive_readiness_compat
    demonstrated_by: AC8
    reason: "AC8 が documented 8 ステップの command 列を real-Git fixture 上で宣言順に実 CLI 実行して観測する。live Claude Code session での skill invocation smoke は本 Issue では実施しない(その旨を AC12 の証跡に明記し、PASS と主張しない)。"
  - profile: skill-invocation-runtime-smoke
    assertion: output_contract_schema_fields_present
    disposition: not_applicable
    reason: "POST_MERGE_CLEANUP_REPORT_V1 の出力契約フィールドは変更しない。"
  - profile: subagent-lifecycle-causal-evidence-smoke
    assertion: subagent_start_stop_causal_evidence_correlated
    disposition: not_applicable
    reason: "SubAgent の delegation 条件・fallback 分岐・start/stop signal 処理は変更しない(scripts/agent-ops/** は path 一致のみ)。"
execution_environment: "Current developer machine、isolated tmp_path 内の real-Git fixture(primary root + linked issue worktree)、実 CLI の subprocess 実行。"
skip_conditions: "git が利用不能な場合のみ SKIP(unavailable)。SKIP / unavailable は PASS ではない。"
fallback_policy: "project_root の直接注入・mock のみ・fake git のみ・marker 文字列の存在のみを PASS として数えない。許容するのは local bare origin と、GitHub API 呼び出し(gh)のみの PATH 上 fake であり、git は実物を使う。"
artifact_requirements: "tested HEAD(commit SHA)、実行した CLI の argv・exit code・JSON、fixture の worktree / branch / root の実行前後の状態を PR 本文に引用する(HOME 絶対パス・secret を含めない)。"

In Scope

  • worktree_catalog.py への primary root 解決・identity 検証の additive な共有 helper 追加(list_worktrees() は不変)と、guard_preflight.py / cleanup_exec.py からの利用。materialize lane は cleanup_exec.resolve_project_root() 経由で同じ root を使い、materialize_cleanup_contract.py 自体は変更しない。
  • catalog / common-dir / primary 不在 / identity 不一致の structured failure 化(AC10)。
  • nested cwd からの active Issue 特定(AC9)。
  • post-merge-cleanup-executor/SKILL.md の main 同期と自 worktree 削除後の実行 cwd の是正(AC8)。
  • 上記を固定する real-Git fixture + public CLI の新規テスト(既存 2 テストファイルの回帰維持を含む)。

Out of Scope

Required Design References

Delivery Rule

1 Issue = 1 PR。既存 cleanup policy を緩和せず、root-selection false-positive の除去に限定する。

Activity

  1. added
    enhancement
    phase/implementation
    agent/implementer
    on Oct 7, 2026
  2. squne121 commented on Oct 7, 2026

    @squne121
    OwnerAuthor

    Issue #2979 敵対的レビュー

    対象: Issue #2979 | 2026年10月8日時点

    暫定判定: 修正推奨(Issue refinement)

    Issue の問題設定は妥当です。現行実装では、Claude Code の issue worktree を repository primary root と誤認する経路が存在します。

    ただし、root resolver の修正だけでは、cleanup の誤判定と安全な実行を両立できない可能性があります。 特に Git catalog 取得失敗時の扱いと、現在の worktree の識別に注意が必要です。

    確認済みの状態:

    推奨する基本方針は、cleanup policy を緩和せず、Git の primary worktree identity を共有 resolver で特定することです。新たな承認レイヤーや恒久的 gate は不要です。

    Findings — 重要度順

    P1-1: root resolver 修正後も、cleanup Skill 全体が issue worktree から実行できない

    問題

    post-merge-cleanup-executor/SKILL.md の Step 2 は以下を実行します。

    git checkout main
    git pull origin main
    

    ところが、canonical primary worktree が既に main を checkout している場合、linked issue worktree から git checkout main は失敗します。

    実際に独立した real-Git fixture で検証すると、checkout は exit 128、既存の main worktree との衝突を報告しました。

    さらに、現在の issue worktree 自身を削除した場合、後続の相対パス実行が失敗することも確認しました。

    Claude Code の公式仕様でも、SubAgent の cd は各 Bash 呼び出し間で持続せず、削除された作業ディレクトリへの操作は失敗し得ます。

    image

    Claude Code Docs

    影響: Issue #2979 の AC1〜AC7 が成功しても、実際の8ステップ cleanup が完遂できない可能性があります。

    最小修正: Allowed Paths 内の executor Skill を修正し、main 同期は canonical root 側で実行する。現在の session が削除対象 worktree 上にいる場合は、自己削除を開始せず、root 側の正規 cleanup 実行コンテキストへ引き継ぐ。既存の認可境界 cleanup_exec は維持します。

    P1-2: catalog failure が status=ok に変換される

    問題

    guard_preflight.py L170-L235 では、list_worktrees() が None を返しても空リストへ変換します。

    if catalog is None:    catalog = []
    

    root branch が default なら、その後 status=ok になる経路が残ります。

    また worktree_catalog.py は git_common_dir の取得に失敗しても catalog 自体を返します。

    影響: primary root resolver の初回 catalog 取得が成功しても、その後の preflight catalog 取得に失敗すると、false-green が成立し得ます。

    最小修正: catalog failure / common-dir failure を構造化 reason code で非成功にする。guard、executor、materialize の public CLI で JSON 応答が維持されることを検証する。

    これは AC4 の具体的な負例として明記すべきです。

    P1-3: worktree 内のサブディレクトリから実行すると active issue を見失う

    問題

    guard_preflight.py::_active_issue() は cwd の basename または root branch 名から Issue 番号を推定します。

    例えば、実際の cwd が次の場合です。

    repo/.claude/worktrees/issue-2979-fix/src
    

    root resolver が正しく primary root を返すと、current_branch は main または primary root 側の別 branch になります。

    LOOP_ISSUE_NUMBER がない場合、src と root branch からは 2979 を復元できません。

    影響: 本当に root drift が起きていても、active issue を検出できず、human_required ではなく blocked と自動回復 hint に落ちる可能性があります。

    最小修正: cwd を catalog の worktree realpath に包含関係で対応付け、該当 entry の branch/path から active issue を特定する。LOOP_ISSUE_NUMBER が存在する場合は、その対応が矛盾しないことを確認する。

    既存の root cwd + 明示 Issue 番号の動作は維持します。

    P2-4: common-dir identity のテストが自己整合性だけで PASS し得る

    現行 list_worktrees() は git_common_dir を一度だけ求めて全 entry に設定します。

    したがって、catalog entry 同士の git_common_dir を比較しても、独立した identity 検証にはなりません。

    AC3 は、linked worktree と primary root のそれぞれに対して実際に git rev-parse --git-common-dir を実行し、realpath で比較することを明示すべきです。

    Git 公式資料では、primary worktree が catalog の先頭に現れ、linked worktree は共通 Git directory を共有する仕様です。

    image

    Git at Google

    +1

    この既存 Git 機能を使えば、branch 名や .git の親ディレクトリを root identity と推測する必要はありません。

    P2-5: baseline-fail がテスト未収集だけで成立する

    Issue #2979 の指定 Verification Commands にある次の選択名は、調査時点の main の対象テストファイルには存在しません。

    • guard_preflight_cli_issue_worktree_root_resolution
    • issue_worktree_session_uses_primary_root

    pytest はテスト未収集時に exit 5 を返すため、これを baseline-expect: fail の根拠にすると欠陥を再現したことになりません。

    image

    pytest documentation

    最小修正: テストを先に追加し、対象ケースの収集件数が正であることを確認してから、修正前に assertion failure(exit 1)、修正後に PASS(exit 0)となることを要求する。

    推奨する Issue contract 改訂

    既存 AC1〜AC7 は維持し、以下の具体的な拡張を推奨します。

    1. AC8 — full workflow context: issue worktree session からの Step 2 main 同期が、primary root に対して実行されること。自己削除後の作業継続不能を回避すること。
    2. AC9 — nested cwd: issue-N-slug/src などの配下からも active issue を正しく特定すること。root drift 時の human_required を維持すること。
    3. AC10 — catalog failure: catalog / common-dir 取得失敗、primary root 不在、異なる repository identity を正例扱いしないこと。guard、executor、materialize の structured failure を確認すること。
    4. AC11 — public CLI parity: CLAUDE_PROJECT_DIR が primary root を指す場合と issue worktree を指す場合の双方で同じ primary root を得ること。guard_preflight --project-root を指定した場合も root identity の照合を省略しないこと。
    5. VC 補強: テスト未収集を baseline failure と扱わず、実行した assertions と public CLI の exit code / JSON / cleanup 後の状態を証跡に残すこと。

    CLAUDE_PROJECT_DIR は Claude Code の仕様上、session 開始時の project root を保持し、途中で worktree に入っても自動更新されません。

    image

    Claude Code Docs

    そのため、両方の環境変数パターンを検証する価値があります。

    実装方針の評価

    共有 root resolver を worktree_catalog.py に配置

    推奨

    既存 Git catalog と deadline を再利用し、重複実装を避けられます。

    既存 cleanup authorization を保持

    必須

    PR merged、branch / OID、linked Issue、target cleanliness、partial cleanup の検証は緩和しません。

    新規 hook・approval layer・独自 schema の追加

    不要

    今回の不具合を解消する直接的な価値がありません。

    実ゲーム runtime による検証

    不要

    isolated real-Git fixture と public CLI のプロセス統合テストで検証可能です。

    最終結論

    Issue #2979 は実装価値が高く、主たる設計方向も正しい。ただし現状のまま implementation-ready とするには不足があります。

    最優先は P1-1 の実 workflow 到達性と、P1-2・P1-3 の preflight false-green / 誤った recovery hint の是正です。

    #1137 の既存 guard contract と #1196 の branch-only lane は変更せず、現在の Issue の Allowed Paths 内で対処する方向を推奨します。実行コンテキストの修正がその範囲で成立しない場合だけ、Outcome を CLI root-resolution に限定して別 Issue へ切り分けるのが適切です。

    今回の判定は live GitHub 本文・コメント、関連実装、公式 Git / Claude Code 仕様、および独立した real-Git の局所再現に基づきます。実際の Claude Code session 全体を再実行して成功・失敗を検証したものではありません。

  3. squne121 commented on Oct 7, 2026

    @squne121
    OwnerAuthor

    推奨アクション

    Issue #2979 を既存の issue-refinement-loop で精緻化し、承認済みの実装契約へ更新する方針を推奨します。

    対象の OWNERコメント #6047844759 には、優先して反映すべき5件の指摘があります。

    優先度 | 対応 -- | -- P1 | cleanup Skill の main 同期と自己削除後の実行継続を是正 P1 | catalog/common-dir 取得失敗を成功扱いしない P1 | nested cwd から active Issue を識別する P2 | Git common-dir identity を独立検証する P2 | pytest 未収集を baseline failure と誤認しない

    既存 AC1〜AC7 を維持し、提案された AC8〜AC11 と Verification Commands の補強を承認するのが妥当です。Git の primary worktree 判定には公式の git worktree list --porcelain -z を利用でき、pytest の exit 5 はテスト未収集を意味します。

    git-worktree Documentation
    +1

    現行 Skill では通常の refinement iteration に個別承認は不要で、既定上限は3回です。一方、canonical な environment/integrity failure や genuine human judgment は迂回しない設計です。

    Claude Code に貼り付ける実行プロンプト

    Issue #2979 — OWNER 承認済み Issue Refinement 実行指示

    あなたは既に auto mode で起動している interactive Claude Code の main/root control-plane です。現在の session を継続し、追加の人間承認を要求せず、repository-native issue-refinement-loop を完了してください。

    対象

    今回のユーザーメッセージは新しい明示的な人間指示です。 対象コメントで提案された技術的修正、Issue 本文の改訂、AC/VC 補強、通常の controlled GitHub mutation、調査・SubAgent 委譲・review/fix iteration の自動実行を承認します。

    ただし、この承認は Claude Code の permission bypass や canonical policy の無効化を意味しません。

    1. Fresh state の取得

    最初に以下を実施してください。

    1. GitHub の Issue 実装: post-merge cleanup guard が issue worktree を repository root と誤認する root selection を是正する #2979 本文・全コメント・関連 Issue・PR を再取得。

    2. 指定 anchor comment が対象 Issue に所属し、OWNER による投稿であり、内容が drift していないことを確認。

    3. GitHub default-branch HEAD、origin/main、local checkout、active worktree、dirty state を確認。

    4. current HEAD の CLAUDE.md、AGENTS.md(存在する場合)、現行 Skill と必要な references を読む。

    5. .claude/agents/codebase-investigator.md、.claude/agents/issue-editor.md、必要なら .claude/agents/web-researcher.md を読み、現在の tool/permission/input/output 契約を確認。

    6. 対象 Issue と重複する既存 Issue、既存 PR、変更予定ファイルとの ownership conflict を検索。

    過去の session 情報やこのプロンプト内の説明より、fresh repository evidence と canonical SSOT を優先してください。

    2. Canonical refinement entry

    issue-refinement-loop を正規の Skill entrypoint として実行してください。

    今回指定された GitHub コメントを operator-selected human-context anchor として扱い、単なる unlabeled URL や agent report に降格させないでください。ただし、trusted principal と origin binding は canonical evaluator に検証させ、結果を捏造しないこと。

    現行 Skill に記載された preflight.run.with_human_context を canonical main root から使用します。

    uv run --locked python3 scripts/agent-guards/skill_runtime_exec.py \
      --command-id preflight.run.with_human_context \
      --issue-number 2979 \
      --repo squne121/loop-protocol \
      --anchor-comment-url https://github.com/squne121/loop-protocol/issues/2979#issuecomment-6047844759

    戻り値の STATUS、NEXT_ACTION、BLOCKERS、artifact、planner decisions に従ってください。

    Trusted contract patch が必要と判定された場合のみ、現行 Skill が許可する contract_update.run.with_human_context を main/root thread から実行します。

    調査証跡 transport が必要な場合は、read-only preflight と mutation phase の binding を維持し、正規の evidence transport を使用します。

    NEXT_ACTION: issue_editor_required が返った場合は、同じ contract update を無限再試行せず、既存の issue-editor / edit-issue controlled transaction による full rewrite route へ進んでください。

    手書きの新規 schema、独自 gate、非正規の raw GitHub mutation を追加しないこと。

    3. SubAgent の実行と完了待機

    必要な SubAgent を実際に起動し、その最終結果を受信するまで後続処理へ進まないこと。

    codebase-investigator

    Planner の investigation_policy に従い、必要な read-only 調査を委譲してください。

    調査対象:

    SubAgent には task、対象ファイル、base SHA、authority、制約、必要な証跡形式、期待成果物を明示してください。

    現行 AGY / native fallback policy を守り、許容された advisory fallback は利用して構いません。AGY 失敗や未検証結果を成功に変換しないこと。

    web-researcher

    Planner の web_research_policy.required が true の場合のみ起動してください。

    Git worktree、pytest、Claude Code の最新公式仕様を一次資料で確認し、Issue contract に影響する外部 claim のみ検証してください。

    Step 1 と Step 1b が独立なら並列実行して構いません。ただし 全 SubAgent の terminal result を join した後 に Step 2 へ進んでください。

    issue-editor

    Issue 書き換えが必要な場合、現行 Step 4 に従って起動してください。

    • 入力は対象 Issue 番号、canonical reviewer feedback URL、readiness payload、検証済み findings

    • Issue 本文以外の implementation mutation をさせない

    • controlled transaction と GitHub readback を必須とする

    • 結果が返る前に review を再開しない

    SubAgent の dispatch 成功、task ID 発行、background 開始は完了ではありません。

    4. 承認する Issue contract 変更

    対象コメントの Findings P1-1〜P2-5 を精緻化対象として採用します。

    既存の Issue goal、AC1〜AC7、既存 cleanup authorization、fail-closed policy、Delivery Rule を維持した上で、以下を追加・補強してください。

    AC8 — Full workflow reachability

    • issue worktree session から main 同期を行う際、canonical primary root を正しく対象にする。

    • linked worktree から git checkout main して衝突する設計を解消する。

    • 実行中 session の worktree を削除した後、無効な cwd で後続処理を実行しない。

    • 必要なら canonical root の正規 cleanup execution context へ移行する。

    • cleanup_exec による既存の削除認可を維持する。

    • 実8ステップの到達性をテストする。

    AC9 — Nested cwd identity

    • issue worktree 内の src/ 等のサブディレクトリからも active Issue を識別する。

    • catalog entry と cwd の realpath containment を使用する。

    • branch name / cwd basename だけに依存しない。

    • LOOP_ISSUE_NUMBER と実 catalog identity の矛盾を検出する。

    • 真の root drift 時には従来の human-required 判定を維持する。

    AC10 — Catalog failure semantics

    • list_worktrees() failure を正常な空 catalog と同一視しない。

    • git common-dir failure、primary root 不在、repository identity 不一致を成功扱いしない。

    • guard、executor、materialize lane に必要な structured failure を維持する。

    • status=ok への false-green を regression test で防ぐ。

    • 不必要な新しい承認 layer や schema は追加しない。

    AC11 — Public CLI parity

    • CLAUDE_PROJECT_DIR が primary root を指す場合と issue worktree を指す場合の双方を検証する。

    • explicit --project-root でも repository identity の検証を省略しない。

    • guard / executor / materialize の root selection semantics を共有する。

    • preflight と executor が異なる root を評価しない。

    既存 AC3 の補強

    linked worktree と primary root のそれぞれから独立に git rev-parse --git-common-dir を実行し、canonical realpath identity を比較すること。

    同一 catalog producer が全 entry に複写した field 同士の比較だけを独立検証としないこと。

    5. Verification Commands の修正

    対象コメントの P2-5 を必ず反映してください。

    新規テスト名がまだ存在しない状態で pytest -k を実行し、exit 5 になっただけでは baseline RED を実証していません。

    次を区別してください。

    • exit 5: no tests collected。assertion failure の証拠にしない。

    • exit 1: 実際に収集・実行されたテストの assertion failure を確認する。

    • exit 0: 対象 assertion が実行され、PASS したことを確認する。

    既存 repository の canonical baseline-fail VC 規則と整合する形へ Verification Commands を修正してください。

    実装時に新規テストを追加した後は、テスト収集件数、修正前 RED、修正後 GREEN、public CLI JSON、negative control、実行後 state を証跡として取得できる契約にしてください。

    既存テストの全 PASS だけで AC8〜AC11 を証明したことにしないこと。

    6. Scope と変更制約

    本 refinement では原則として Issue #2979 の既存 Allowed Paths を維持します。

    対象コメントが示す範囲での AC/VC 追加、Issue 本文構造改善、既存ファイルのテスト要件追加は承認済みです。

    新しい architecture layer や別 Issue が所有する runtime/harness の変更が必要だと判明した場合は、最初に existing Issue / owner を調査し、現在の Issue 内で対処可能か判断してください。

    独立した follow-up が必要なら重複を確認して起票します。必要性がない場合は Issue を増殖させないこと。

    個人趣味開発のため、品質・実用性・実行完了率を優先します。

    新規 approval framework、hook、wrapper、schema、一般的 security hardening は、今回の具体的 failure class の修正に不可欠でなければ追加しません。

    7. Refinement review loop

    現行 Skill の以下の経路を遵守してください。

    1. Fresh preflight と planner result

    2. 必要な investigation / web research

    3. SubAgent completion join

    4. Canonical Step 2 review

    5. 必要なら controlled Issue rewrite

    6. GitHub body/title readback

    7. Fresh preflight / review

    8. Semantic design review(applicable の場合)

    9. Final readiness / termination

    Step 2 の root_review_pipeline.produce は Skill 規定どおり run_in_background: true で起動し、background task の完了通知と実 JSON result を join してから canonical_step2_route を consume してください。

    canonical Step 2 で legacy issue-reviewer SubAgent を追加起動したり、独自の verdict classifier を実装したりしないこと。

    needs-fix は既定の最大3 iteration まで自動継続してください。

    通常の修正、review finding、VC 修正、controlled GitHub 更新のたびに人間承認を要求しないでください。

    ただし genuine human_judgment_required、authority binding 不成立、integrity failure、irreconcilable scope conflict、規定 iteration 上限については正規停止条件を守ります。結果を捏造したり gate を迂回したりしないこと。

    8. 再発防止 Issue

    調査で独立した再発リスクが確認された場合は、既存 Issue を全 state で検索してください。

    再発防止 Issue の起票はこの invocation で承認します。

    9. 完了条件

    以下を満たすまで、単なる提案・調査で終了しないでください。

    approved で終了した場合も、今回は Issue refinement のみ です。impl-review-loop、実装、PR 作成、merge、cleanup を勝手に開始しないでください。

    最後に日本語で以下を報告してください。

    1. Issue 実装: post-merge cleanup guard が issue worktree を repository root と誤認する root selection を是正する #2979 の最終状態と本文更新内容

    2. 消化した各 finding と AC/VC への反映

    3. 使用した SubAgent とその terminal result

    4. Review iteration 数と最終判定

    5. Fresh verification evidence

    6. 新規再発防止 Issue の URL、または不要とした根拠

    7. 残 blocker と次の implementation handoff 条件

    これらの作業を現在の interactive auto-mode session で直ちに実行してください。

    再発防止 Issue の判断

    今回、新規 Issue は起票していません。P1-1〜P2-5 は原則として #2979 の既存 Allowed Paths 内で扱えます。また、pytest 未収集の分類については #1434 が CLOSED であり、現行 baseline_vc_preflight.py にも exit 5 を vc_no_tests_collected / blocked とする処理を確認しました。

    別の failure class が実調査で確認された場合のみ、プロンプト内の指示に従って重複を確認し、独立 Issue を起票させる構成です。

  4. squne121 commented on Oct 7, 2026

    @squne121
    OwnerAuthor

    Issue refinement 終了報告(approved)

    Issue #2979 の refinement を OWNER 承認済み anchor(https://github.com/squne121/loop-protocol/issues/2979#issuecomment-6047844759)に基づき完了した。本文 sha256 は 56cb97f7c9923d5e4c98c6b23884b85347928a6b38444e554b10441de85017f2。この終了は実装開始の承認ではない(impl-review-loop は起動していない)。

    反映内容

    • anchor の P1-1〜P2-5 を AC8〜AC11 と AC3 補強、Verification Commands の修正として反映した。AC12 で証跡要件を追加した。
    • pytest -k による未収集(exit 5)は baseline fail の根拠にしない。未作成の specific test file を参照する exit 4 方式に改め、-k test_ac<N>_ で AC ごとの専用 test を要求した。
    • Runtime Verification Applicability は、Allowed Paths が extension-surface policy に一致するため not_applicable から immediate へ是正した。procedure の live 検証は unverified のまま残し、AC12 で PASS と主張しないことを明記した。
    • 欠落していた In Scope / Out of Scope / Required Design References と、定型 Stop Conditions を補った。

    review の経過

    • 反復 1: 決定論 review は approve。semantic review が high 3 件・medium 5 件を報告し、本文を rewrite した。
    • 反復 2: 決定論 review は approve、semantic review は high 0 件・medium 5 件・low 1 件で、join の結果は approve(medium 以下は非 blocking の warning)。

    実装時に確定すべき非 blocking の注意事項(反復 2 の medium / low)

    • AC10 の 4 reason code × 3 lane の再現 seam を実装時に明示する(実 git の CLI で再現できない code は PATH 上の git shim を negative control に限り使う)。identity 検証は verify_* の current_branch 判定より前に行う。
    • 入力種別と reason code の対応表、および新 code の status(blocked / human_required)を実装時に固定する。bare repository の検出は rev-parse --is-bare-repository を使う。
    • worktree_scope_guard.py の root 解決は out of scope のまま。同 hook は現行 settings.json に未配線であることを確認済みで、runtime への影響は限定的。guard 配線時に contract の読み取り root が乖離しないかを再確認する。
    • SKILL.md の呼び出し形(git -C <canonical_root> か cwd 指定)が .claude/settings.json(Allowed Paths 外)の変更を要さないことを確認する。root drift 時の停止範囲も固定する。
    • AC6 の回帰として、tests/codex/test_cleanup_exec_branch_only.py、scripts/agent-ops/tests/test_cleanup_exec_issue_2628_e2e.py、tests/agent_ops/test_control_plane_worktree_remote_binding.py も実行する。
    • git_worktree_probe.py は catalog 失敗時に exit 0 かつ entries 空を返すため、空 entries を検出したら停止する。

    OWNER 判断として本 refinement が採った方針

    • materialize lane: materialize_cleanup_contract.py は変更せず、cleanup_exec.resolve_project_root() が例外を投げない方針で Allowed Paths 内に収めた。
    • Step 2 の main 同期は guard_preflight が root_branch_state=default を返す場合のみ行い、root drift 時の human_required を迂回しない。

    再発防止 Issue

    不要と判定した。guard 側 root 統一は現行配線外で実害がなく、live skill smoke の基盤は #2981 が所有する。pytest 未収集の扱いは #1434 の既存契約と重複する。

    未実施の項目

    • web-researcher は起動していない。外部 claim(git worktree の catalog 順序、pytest exit 5、linked worktree からの git checkout main の失敗)は実機で独立に再現して確認した。
    • live Claude Code session での skill invocation smoke は実施していない。
    LOOP_HANDOFF_RESULT_V1:
      status: refinement_approved
      routing_action: none
      contract_review:
        status: go
        gate_result: fresh_go
        latest_comment_url: none
        generated_at: 2026-10-07T22:50:00Z
        body_sha256: 56cb97f7c9923d5e4c98c6b23884b85347928a6b38444e554b10441de85017f2
      metadata:
        title_prefix_ready: true
      label_sync_observation:
        kind: presentation_sync
        result: noop
        detail: not attempted
      auto_fixes:
        result: auto_fixed
        required: []
        skipped: []
      blockers: []
      permissions:
        unavailable: []
      generated_at: 2026-10-07T22:50:00Z
      checked_body_sha256: 56cb97f7c9923d5e4c98c6b23884b85347928a6b38444e554b10441de85017f2
      checker_exit_code: 0
      missing_sections: []
      missing_contract_keys: []
  5. squne121 commented on Oct 8, 2026

    @squne121
    OwnerAuthor

    確認結果と推奨方針

    2026年10月8日時点の GitHub を確認しました。

    推奨は、現行 Issue contract を維持し、impl-review-loop を最大3 iterationの正規ループで実行することです。 通常の修正・テスト失敗・レビュー差し戻し・CI修復は自動継続し、最終的にPRを人間がmerge判断できる状態にします。

    重要なのは、refinement 時に残った medium/low findings も実装前に確認することです。特に materialize lane の失敗処理、固定 reason code、cleanup Skill の実行コンテキスト、既存テストとの互換性を省略しない構成にします。

    再発防止 Issue を起票

    #2988 — termination publisher の dry-run 誤投稿

    GitHubへの新規起票を確認しました。legacy モードでも --dry-run を正しく伝搬させ、GitHub投稿と Task Context signal が発生しないことを回帰テストで固定する内容です。

    これは #2979 の Allowed Paths 外の独立した不具合として扱います。今回の実装PRには混在させません。

    Claude Code に貼り付ける実行プロンプト

    以下は、既に auto mode で起動している fresh interactive Claude Code にそのまま貼り付ける指示です。現行 impl-review-loop の Step 1 → 2 → 4 → 5、SubAgent completion protocol、current-HEAD verification、terminal gate に沿って設計しています。

    Issue #2979 — impl-review-loop 自律実装・検証・PRレビュー指示

    あなたは、既に auto mode で起動している interactive Claude Code の main/root control-plane です。現在の session を継続し、repository-native impl-review-loop に従って Issue #2979 の実装から PR の merge 直前までを完遂してください。

    目的と実行承認

    このユーザーメッセージを、新たな明示的な実装実行承認として扱ってください。

    次の通常作業は個別の人間承認を求めず、自律的に実行することを承認します。

    • GitHub / repository / upstream の調査
    • canonical Skill / SSOT / Agent 定義の読み込み
    • 必要な SubAgent dispatch
    • 専用 worktree の作成・再利用
    • Issue Allowed Paths 内の実装・テスト追加・修正
    • 必要な commit、通常の push、PR 作成・更新
    • test / runtime fixture / CI の実行
    • review feedback の修正と再検証
    • branch update、non-destructive な conflict repair
    • 既存 workflow による controlled GitHub mutation
    • 重複確認後の独立した再発防止 Issue の起票

    実行できる作業を調査や提案だけで終了しないでください。

    ただし、この承認は Claude Code permission bypass、repository authority の無効化、secret 取得、force push、実 merge、auto-merge、不可逆 cleanup の承認ではありません。

    auto mode の既存 session・permissions・hooks を維持してください。通常の permission classifier が認める正規 workflow を使用し、権限回避手段を追加しないでください。

    1. Fresh state と canonical contract の確認

    最初に次を fresh に確認してください。

    1. Issue 実装: post-merge cleanup guard が issue worktree を repository root と誤認する root selection を是正する #2979 の最新本文、全コメント、labels、関連 Issue、native relationships、linked PR。
    2. GitHub default-branch HEAD、origin/main、local HEAD、branch、worktree catalog、dirty state。
    3. 対象 Issue の既存実装 worktree・branch・PR の有無。
    4. CLAUDE.md、AGENTS.md、.claude/rules/**、必要な docs/dev/** と SSOT。
    5. current HEAD の impl-review-loop/SKILL.md と次の Step 定義。
      • steps/preparation.md
      • steps/step-1-implementation.md
      • steps/step-2-verification.md
      • steps/step-4-pr-review.md
      • steps/step-5-feedback-and-termination.md
      • steps/step-5-mergeability-handling.md
      • steps/conflicting-pr-escalation-runbook.md
      • steps/context-protocol-and-guardrails.md
    6. .claude/agents/implementation-worker.md、test-runner.md、pr-reviewer.md。
    7. .claude/skills/implement-issue/SKILL.md、.claude/skills/pr-review-judge/SKILL.md と必要な references。

    今回の Issue は refinement が refinement_approved で終了した履歴を持ちます。ただし過去の終了コメントだけを Step 1 起動 authority にしないでください。

    最新 Issue body、fresh contract-review result、snapshot、current-run intake の canonical 判定を再取得してください。

    過去の refinement ログには generated_at の不正確な記録、および Task Context signal の conflict が存在します。これらの過去記録を freshness の証明とせず、現行 workflow が必要とする fresh evidence を使用してください。

    preparation.md の Root-Owned Synchronous Entry Transition を守り、capability preflight → live Issue fetch → current-run intake → route 判定 → Step 1 dispatch を、現行規定に従う連続した root-owned 実行で行ってください。

    必要な contract snapshot が欠落・stale の場合は、既存の canonical materialization / review repair route を使って回復してください。独自の authorization packet や bypass を作らないでください。

    既存 branch / PR が見つかった場合は再利用し、重複作成しないでください。

    2. Issue #2979 の実装スコープ

    実装の唯一の正本は GitHub 上の最新 Issue #2979 本文です。

    現在の契約は AC1〜AC12 を対象としています。実装前に全 AC、Verification Commands、Allowed Paths、Stop Conditions を逐語で確認し、AC ごとの実装対象と検証方法を整理してください。

    特に以下の欠陥を修正してください。

    2.1 Canonical primary root resolution

    • current issue worktree と repository primary root を混同しない。
    • git worktree list --porcelain -z を用いて primary worktree を特定する。
    • linked worktree と primary root でそれぞれ独立に git rev-parse --git-common-dir を実行し、realpath identity を照合する。
    • branch 名や cwd basename から repository identity を推定しない。
    • bare repository / primary root 不在 / catalog 取得失敗を成功扱いしない。
    • worktree_catalog.py へ共有 helper を additive に追加し、既存 list_worktrees() API の互換性を維持する。
    • guard_preflight、cleanup_exec、materialize lane の root 解決を整合させる。

    2.2 Cleanup workflow の到達性

    .claude/skills/post-merge-cleanup-executor/SKILL.md の実8ステップを調べ、issue worktree session から開始しても実行が継続可能になるよう修正してください。

    • linked worktree で git checkout main を実行しない。
    • Step 2 の main 同期より前に canonical primary root の identity / branch state を検証する。
    • 真の root drift を main checkout によって隠蔽しない。
    • main 同期は primary root に対して行う。
    • worktree 削除後は削除済み cwd から相対コマンドを実行しない。
    • SubAgent の Bash 呼び出し間で cd の状態が持続すると仮定しない。
    • root を必要とする各 command は、正規の明示的実行コンテキストで実行する。
    • cleanup_exec の既存削除認可は必ず保持する。
    • worktree 自己削除、stash の持越し、post-deletion execution を real-Git fixture で検証する。

    この Issue の実装中に、実際の開発 worktree を post-merge cleanup しないでください。 削除操作の検証対象は管理された隔離 fixture に限定します。

    2.3 Nested cwd と active Issue identity

    • worktree 内の src/ 等から起動しても active Issue を識別する。
    • catalog entry と cwd realpath の containment を使う。
    • primary root が linked worktree の親ディレクトリになる場合を考慮し、適切な最深一致を優先する。
    • LOOP_ISSUE_NUMBER と実 catalog identity の矛盾を検出する。
    • 真の root drift 時の既存 human_required 判定を維持する。

    2.4 Failure semantics

    少なくとも次の reason code を、既存 schema に従って適切に返してください。

    • worktree_catalog_unavailable
    • git_common_dir_unavailable
    • primary_root_unresolved
    • repository_identity_mismatch
    • active_issue_catalog_conflict

    入力条件と reason code、status、CLI exit code の対応を実装前に明確化してください。

    guard、executor、materialize の各 lane で、正常系と異常系を確認してください。

    status=ok への false-green を防ぎます。ただし、既存の実行を無意味にブロックする過剰な gate は追加しないでください。

    2.5 Materialize / compatibility

    materialize_cleanup_contract.py は現在の Issue Allowed Paths 外です。

    まず cleanup_exec.resolve_project_root() と既存 verification functions によって、例外を発生させず structured refusal を返せることを確認してください。

    Allowed Paths 外の変更が不要な設計を優先します。

    また、次を維持してください。

    関連する #2913 は cleanup_exec.py の別領域を扱うため、current ownership と main drift を確認し、修正を衝突させないでください。

    3. Verification — false-green を禁止

    Issue の Verification Commands を実際に実行してください。

    新規テストとして、Issue が許可している次のファイルを使用します。

    • tests/agent_guards/test_guard_preflight_cli_root_selection.py
    • scripts/agent-ops/tests/test_cleanup_exec_root_selection.py

    各 AC に対して test_ac<N>_* の専用 assertion を配置し、実際の収集件数が正であることを確認してください。

    検証では以下を必ず区別してください。

    • pytest exit 4: 指定 test file 不在。baseline preflight では expected baseline fail に分類できるが、欠陥 assertion の RED を意味しない。
    • pytest exit 5: no tests collected。成功でも assertion failure でもない。
    • pytest exit 1: 実際の assertion failure かどうかを確認する。
    • pytest exit 0: 必須 assertion が収集・実行されたうえで PASS していることを確認する。

    実装前 RED を示すときは、追加した regression test を修正前コードに適用した隔離 checkout で実際の assertion failure を観測してください。既存作業や他の worktree を上書きしてはなりません。

    real-Git fixture を主とし、GitHub API が必要な箇所のみ fake gh を使います。故障注入の negative control に限り、目的を明示して Git shim を使って構いません。

    次の回帰テストも実行してください。

    • tests/agent_guards/test_worktree_scope_guard.py
    • scripts/agent-ops/tests/test_cleanup_exec.py
    • scripts/agent-ops/tests/test_cleanup_exec_non_closing_authority.py
    • tests/test_post_merge_cleanup_boundary.py
    • tests/codex/test_cleanup_exec_branch_only.py
    • scripts/agent-ops/tests/test_cleanup_exec_issue_2628_e2e.py
    • tests/agent_ops/test_control_plane_worktree_remote_binding.py

    テスト対象が最新 main で変わっている場合は、現行契約に従ってください。

    post-merge-cleanup-executor/SKILL.md の documented commands と実際にテストされる経路が一致することを検証してください。

    RVA が immediate であることを尊重します。ただし、pytest subprocess の証跡を live Claude Code Skill invocation の成功として誤記しないでください。live 未検証は unverified と報告し、Issue 契約に存在しない新たな恒久 gate を勝手に導入しないでください。

    4. impl-review-loop の SubAgent orchestration

    次の SubAgent を実際に使用し、それぞれの terminal result と final output を取得してから依存する後続 Step に進んでください。

    implementation-worker

    Step 1 で current Skill に従って起動してください。

    SubAgent への message に以下を明示します。

    • repository / Issue 番号 / URL
    • authoritative Issue contract
    • fresh base SHA
    • Allowed Paths
    • 実装すべき AC と設計制約
    • 関連 Issue と競合しない範囲
    • 必要な test と期待証跡
    • expected IMPLEMENT_RESULT_V1

    implement-issue Skill に従い、実装、commit、push、PR 作成または既存 PR 更新まで担当させてください。

    test-runner

    Step 1 が terminal completion した後に起動してください。

    current PR HEAD、Issue contract、AC / VC の逐語的 command、command_hash、PR number、worktree を明示的に渡してください。

    実際の test 実行事実と TEST_VERDICT_MACHINE/v2 を取得してください。

    SubAgent が completed であることだけでなく、final result の存在・内容・HEAD binding を確認してください。

    pr-reviewer

    Step 2 が完了し、canonical VC adjudication により reviewer dispatch が許可された後で起動してください。

    pr-review-judge Skill を使用し、最新 PR HEAD、実 diff、Issue AC、テスト証跡、既存レビューコメント、CI、関連コードを敵対的にレビューしてください。

    レビューでは次を優先します。

    • Issue contract との不一致
    • public CLI と function API の semantic divergence
    • false-green / happy-path-only test
    • common-dir / worktree identity の誤判定
    • materialize lane の不整合
    • cleanup 認可の回帰
    • 既存 Skill command parity の破壊
    • main drift / stale evidence / stale PR HEAD
    • worktree 削除後の session 操作
    • error classification と partial success
    • 未解決の review findings

    返却された verdict、reviewed_head_sha、blockers、warnings を canonical routing に渡してください。

    Completion join barrier

    すべての SubAgent について、dispatch しただけで成功扱いしないでください。

    1. task ID / task name を記録する。
    2. 必要に応じて background 実行を使う。
    3. task が terminal になるまで現行 runtime の completion mechanism で待つ。
    4. list_agents 等、現行 Skill が指定する確認経路で terminal state を確認する。
    5. terminal state に加えて final result を取得する。
    6. report 内の actual HEAD、status、artifact、blocker を検証する。
    7. 依存する後続 Step を開始する。

    独立した read-only 調査は並列化して構いません。implementation → verification → PR review → judgment の依存関係は守ってください。

    必要なら codebase-investigator を追加使用しても構いませんが、現在の Agent 定義と route contract を読んでから委譲してください。

    SubAgent discovery failure は、現行 Skill の Stage A / Stage B recovery を利用してください。Stage B の --agent main-session persona invocation は child spawn ではないため、存在しない child terminal result を待たないでください。

    5. Review / Fix loop

    impl-review-loop の canonical routing を使用します。

    基本フロー:

    inspect → implement → test → PR review → fix → re-test → fresh review → terminal gate

    • REQUEST_CHANGES は可能な限り自動修正する。
    • fix_delta は actual reviewer findings を基に作成する。
    • 同じ PR・branch・worktree を再利用する。
    • 修正後は必ず新しい HEAD に対して verification と review を行う。
    • BEHIND は canonical update-branch route に従う。
    • stale review は fresh HEAD で再実行する。
    • required CI failure は原因を調査して修復する。
    • CI の単なる pending / stale run を PASS とみなさない。
    • body-only evidence sync が eligible の場合は、現行 bounded body-only lane を使用する。
    • max_iterations を恣意的にリセット・拡張しない。

    step4-adjudicate の reviewer dispatch 許可だけで terminal approval としないでください。

    最終承認の正本は adjudicate_vc_result.py step5-terminal-gate の exit 0 と、現行 Skill が要求する最新 HEAD の evidence です。

    APPROVE の自己申告だけで完了扱いしないでください。

    actual Git conflict、真正な authority conflict、irreconcilable scope conflict、secret 操作、canonical iteration 上限、evidence integrity failure などの本当の停止条件を除き、通常の修正可能な失敗で人間判断を求めないでください。

    6. 再発防止 Issue

    この invocation では、再発防止 Issue の重複検索と必要な新規起票を承認します。

    ただし、すでに次の Issue が存在します。

    #2988 は refinement 終了レポートの legacy --dry-run が GitHub コメントを投稿する別問題です。#2979 の PR には混在させず、重複起票もしないでください。

    その他についても以下を守ります。

    7. PR の完成条件

    次を満たすまで、単なる調査・設計・SubAgent dispatch で終了しないでください。

    1. Issue 実装: post-merge cleanup guard が issue worktree を repository root と誤認する root selection を是正する #2979 の AC1〜AC12 が実装によって満たされている。
    2. Allowed Paths 遵守が確認されている。
    3. 必要な regression tests が通過している。
    4. 実 CLI の positive / negative controls が成立している。
    5. current HEAD に対応する test evidence が揃っている。
    6. current HEAD の required CI が成功している。
    7. PR body に AC 対応、Verification Commands、test counts、runtime evidence、既知の limitation が正確に記載されている。
    8. pr-reviewer の blocking findings が解消されている。
    9. 最新 HEAD で fresh review が完了している。
    10. canonical Step 5 terminal gate が承認している。
    11. すべての SubAgent / background tasks が terminal で、final result を回収済みである。
    12. GitHub PR が open、merge 判断待ちである。

    PR の実 merge、auto-merge、post-merge cleanup は行わないでください。

    8. 最終報告

    日本語で以下を報告してください。

    • Issue 実装: post-merge cleanup guard が issue worktree を repository root と誤認する root selection を是正する #2979 の実装結果
    • PR URL、branch、final HEAD SHA
    • 変更ファイルと AC1〜AC12 の対応
    • 実行した VC、テスト件数、PASS / FAIL / SKIP
    • 修正前 RED と修正後 GREEN の証跡
    • 実 CLI / real-Git fixture の結果
    • current HEAD の CI 状態
    • SubAgent 一覧と各 terminal result
    • review iteration 数、findings と修正履歴
    • terminal gate の actual result
    • 再発防止 Issue の確認結果・新規起票 URL
    • 残 blocker と未検証事項
    • 現在の PR が merge 判断可能か

    最終報告では、実際に確認できた事実と未検証の主張を区別してください。

    この指示に基づいて、現在の interactive auto-mode session で Issue #2979 の impl-review-loop を直ちに実行してください。人間の merge 判断が必要になる直前まで進めてください。

    このプロンプトでは、Claude Code の SubAgent が独立した context を持つ点と、現行 repository の completion join 契約を前提にしています。

    image

    Claude Code Docs

    また、Git の primary / linked worktree の識別は公式の porcelain format と common-dir semantics を利用する方針です。

    image

    git-worktree Documentation

    停止境界: 通常の auto mode は継続させますが、genuine な human-judgment gate や権限上の拒否まで無条件に通過させる指示ではありません。実装・PR更新・レビュー修正までを自律実行し、実際の merge は行わない構成です。

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