Skip to content

実装: dedicated worktree 内の git add が local_main_branch_guard に誤 block される publish blocker を解消する #1215

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "1209"
goal_ref: "implementation-worker の dedicated worktree 内で canonical な `git add -- <allowed-path>` が `local_main_branch_guard` に `unknown_command_requires_review` と誤判定されず、#1209 の publish が完了できる状態にする"
change_kind: workflow

Parent Issue

#1209

Parent Goal Ref

  • Goal: implementation-worker が安全な exact command class で Issue worktree を作成でき、impl-review-loop が実装前に停止しない状態にする
  • Desired Destination: dedicated worktree での実装・検証完了後、Allowed Paths 内の変更を通常の git add / git commit / Draft PR 起票まで進められ、publish 手前で local hook false positive に止められない状態

Current Validated Scope

  • 実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 の専用 worktree .claude/worktrees/issue-1209-worktree-bootstrap-codex / branch worktree-issue-1209-worktree-bootstrap-codex では、実装と検証は完了している
  • uv run pytest tests/agent_ops/test_worktree_bootstrap_exec.py -v
  • uv run pytest tests/agent_ops/test_implement_issue_worktree_bootstrap_docs.py -v
  • uv run pytest tests/codex/test_worktree_bootstrap_command_policy.py -v
  • uv run pytest .claude/hooks/tests/test_worktree_scope_guard_worktree_bootstrap.py -v
  • pnpm typecheck
  • pnpm lint
  • pnpm test
  • pnpm build
  • ただし dedicated worktree 内の canonical な git add -- <allowed-path> が local_main_branch_guard に reason_code: unknown_command_requires_review で fail-closed block され、commit / push / Draft PR 起票へ進めない
  • 2026-06-27 の owner comment では、local_main_branch_guard は linked issue worktree context を早期 allow し、git add の pathspec / staging 判定は worktree_scope_guard 側に分離するよう REQUEST_CHANGES が出ている

Remaining Parent Gaps

Outcome

dedicated worktree 上の implementation issue で、local_main_branch_guard は linked issue worktree context を root checkout と誤認せず context ベースで早期 allow し、git add の pathspec / staging 判定は worktree_scope_guard のみに集約されたまま、Allowed Paths 内ファイルに対する canonical な git add -- <path> が commit / push / Draft PR 起票まで進められる状態。

In Scope

  • local_main_branch_guard に linked issue worktree context の早期 allow を追加し、root default branch drift 防御とは責務を分離する
  • local_main_branch_guard は git add pathspec / Allowed Paths 判定を持たず、command 例外ではなく context 判定だけを担当する形に修正する
  • worktree_scope_guard に dedicated worktree publish 向け git add pathspec classifier を追加し、git add -- <explicit-file> [<explicit-file>...] のみを明示ファイル staging として許可する
  • PreToolUse と PermissionRequest の両イベントで、実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 相当の dedicated worktree publish blocker を再現する regression fixture / test を追加する
  • fixture は実際に block された hook payload を最小化した JSON とし、少なくとも cwd、tool_name、tool_input.command、hook_event_name または同等のイベント識別情報を保持する
  • 必要最小限の shared guard helper / catalog / diagnostics を更新し、判定経路と reason_code を追跡可能にする
  • 実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 publish blocker の再現と解消確認

Out of Scope

  • root checkout での branch mutation 保護の緩和
  • git add .、git add -A、git add --all、git add -u、git add --update、glob / pathspec magic / directory-wide staging の許可
  • git add --pathspec-from-file ...、git add --pathspec-from-file=...、git add :/、git add ':(glob)**' などの広域 pathspec staging の許可
  • git checkout、git switch、git reset --hard など危険コマンドの緩和
  • 実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 の executor 実装そのものの再設計
  • GitHub remote write policy 全体の再設計
  • rtk proxy / arbitrary shell passthrough の許可
  • owner comment 内の仮説群を、この issue 単独で広域原因調査に拡張すること

Acceptance Criteria

  • AC1: dedicated worktree context では local_main_branch_guard が linked issue worktree を root checkout と誤認せず、branch drift 判定より前に context ベースで allow して unknown_command_requires_review を返さない
  • AC2: local_main_branch_guard は git add pathspec / Allowed Paths 判定を持たず、git add の command-specific 例外追加ではなく linked issue worktree context 判定だけで publish 経路を通す
  • AC3: dedicated worktree 内の git add pathspec 判定は worktree_scope_guard が担い、git add -- <explicit-file> [<explicit-file>...] のみ publish staging として継続可能になる
  • AC4: dedicated worktree 内でも Allowed Paths 外の git add、git add .、git add -A、git add --all、git add -u、git add --update、git add --pathspec-from-file ...、git add --pathspec-from-file=...、git add :/、git add ':(glob)**'、directory-wide staging、git -C <outside> add、bash -lc 'git add ...'、rtk proxy ...、危険な branch mutation コマンドは引き続き block または review される
  • AC5: tests/fixtures/hooks/issue1215-pretooluse-dedicated-worktree-git-add.json と tests/fixtures/hooks/issue1215-permissionrequest-dedicated-worktree-git-add.json が追加され、実 blocker を最小化した payload で cwd、tool_name、tool_input.command、イベント識別情報を保持する
  • AC6: dedicated worktree publish path の判定理由が diagnostics またはテストで追跡可能になり、linked_issue_worktree_context 相当の context-based reason と unknown_command_requires_review からの脱出が確認できる
  • AC7: 実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 worktree で commit / push / Draft PR 起票まで再開できることが確認できる

Verification Commands

# AC1
# baseline-expect: fail
$ rg -n "issue1215_worktree_context" tests/codex/test_local_main_branch_guard.py
# AC2
# baseline-expect: fail
$ rg -n "issue1215_no_git_add_exception" tests/codex/test_local_main_branch_guard.py
# AC3
# baseline-expect: fail
$ test -f .claude/hooks/tests/test_issue1215_git_add_allow.py
# AC4
# baseline-expect: fail
$ test -f .claude/hooks/tests/test_issue1215_git_add_block.py
# AC5
# baseline-expect: fail
$ test -f tests/fixtures/hooks/issue1215-permissionrequest-dedicated-worktree-git-add.json
# AC6
# baseline-expect: fail
$ rg -n "linked_issue_worktree_context" scripts/agent-guards/local_main_branch_guard.py tests/codex/test_local_main_branch_guard.py
# AC7
# baseline-expect: fail
$ test -f .claude/hooks/tests/test_issue1215_publish_resume.py

Allowed Paths

  • scripts/agent-guards/local_main_branch_guard.py
  • scripts/agent-guards/worktree_scope_guard.py
  • scripts/agent-guards/worktree_catalog.py
  • scripts/agent-guards/utils/**
  • .claude/hooks/**
  • tests/codex/test_local_main_branch_guard.py
  • tests/codex/test_worktree_scope_guard.py
  • tests/fixtures/hooks/issue1215-pretooluse-dedicated-worktree-git-add.json
  • tests/fixtures/hooks/issue1215-permissionrequest-dedicated-worktree-git-add.json
  • docs/dev/hook-boundaries.md

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
  • local root default branch 保護を弱めないと修正できない場合
  • local_main_branch_guard の責務を壊さずに dedicated worktree allow を導入できないと判明した場合
  • wrapper / hook event ordering の安全性要件が 実装: local hook の unparseable_branch_mutation 誤判定で issue-refinement-loop が停止する問題を解消する #1198 や別仕様と広く衝突すると判明した場合
  • repo 外 hook や Codex/Claude 本体側 bug で、repo 側修正だけでは解決不能と判明した場合

Runtime Verification Applicability

decision: not_applicable
reason: static checks only

Required Skills

  • Python 3
  • Git

Delivery Rule

1 Issue = 1 PR。dedicated worktree で実装し、#1209 の未公開差分を壊さず publish blocker だけを解消する。

Scope Delta(任意)

N/A

Activity

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

    @squne121
    OwnerAuthor

    判定: REQUEST_CHANGES

    #1215 は必要です。ただし、現契約のまま実装すると local_main_branch_guard に git add の例外を足して、root checkout の index mutation まで緩める事故が起きやすいです。正しい分離は、local_main_branch_guard は「linked worktree なら早期 allow」、worktree_scope_guard は「その worktree 内の staging だけを pathspec まで検査」です。

    #1215 は #1209 の publish blocker で、#1209 専用 worktree では実装・検証は完了しているが、dedicated worktree 内の単純な git add <allowed-path> が unknown_command_requires_review で止まり、commit/push/Draft PR に進めない、という問題です。AC も git add <allowed-path> の通過、git add . / git add -A / Allowed Paths 外 staging の継続 block、#1209 相当 fixture、診断追跡を要求しています。 #1215 自体にはコメントはありません。

    P0 ブロッカー

    1. 根因 fixture が足りない

    現行 local_main_branch_guard.py の目的は「local root checkout が default branch から drift する Bash command を止める」ことです。linked issue worktree 内の通常作業を裁く guard ではありません。 さらに現行実装は is_local_root_context(cwd) が false なら not_local_root_context で allow する構造です。

    したがって、dedicated worktree 内の git add が本当にこの guard で止まるなら、少なくとも次のどれかです。

    1. Codex hook payload の cwd が worktree ではなく main root になっている。
    2. CLAUDE_PROJECT_DIR / wrapper 経由の project root 解決が実 runtime と test fixture でズレている。
    3. 実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 未publish差分が unknown_command_requires_review を fail-closed に変更している。
    4. rtk git add など envelope 化された command が raw git add と異なる経路を踏んでいる。

    #1215 は「実際に block された raw Codex/Claude hook JSON」を fixture 化する AC を追加するべきです。evaluate("git add ...", cwd=worktree) だけでは再現として弱いです。run_guard_script(payload, cwd=...) で PreToolUse と PermissionRequest の両方を固定してください。

    2. git add の pathspec policy が未定義

    git add は branch mutation ではありませんが、index mutation です。Git 公式仕様でも git add は index/staging area に内容を追加する command で、-A/--all は working tree に一致するよう追加・変更・削除を index に反映し、pathspec 無しなら working tree 全体が対象です。(Git)

    現行 worktree_scope_guard.py は _GIT_MUTATING_SUBCMDS に add を含め、git add を mutating と分類します。 その後、cwd が expected worktree 内なら mutating command を allow します。 つまり 今の構造では git add . / git add -A も worktree 内なら通る可能性が高い。これは #1215 AC2 と衝突します。

    #1215 は local_main_branch_guard だけでなく、worktree_scope_guard 側に git add 専用 classifier を要求すべきです。少なくとも block/review 対象は以下です。

    git add .
    git add -A
    git add --all
    git add -u
    git add --update
    git add :/
    git add ':/**'
    git add ':(glob)**'
    git add --pathspec-from-file ...
    git add --pathspec-from-file=...
    git add --intent-to-add
    git add --refresh
    git -C <outside> add ...
    bash -lc 'git add ...'
    rtk proxy git add ...
    

    許可するなら、形はこの程度まで絞るべきです。

    git add -- <explicit-file-1> <explicit-file-2> ...

    Git 公式にも -- は option と file list の区切りとして定義されています。(Git) また <pathspec> は glob や directory 指定で広範囲に展開され得るので、「Allowed Paths 内の明示ファイル staging」を主張するなら、directory path / glob / pathspec magic をそのまま許可してはいけません。([Git]1)

    3. hook ordering 上、PreToolUse だけ直しても足りない

    Codex の PreToolUse では local_main_branch_guard が先に走り、その後に worktree_scope_guard が走ります。 PermissionRequest では local_main_branch_guard と session recording だけで、worktree_scope_guard は入っていません。

    したがって #1215 の fixture は最低限この 2 経路が必要です。

    PreToolUse + Bash + cwd=dedicated worktree + git add -- <allowed-path>
    PermissionRequest + Bash + cwd=dedicated worktree + git add -- <allowed-path>
    

    片方だけ通しても publish blocker は再発します。

    4. local_main_branch_guard に Allowed Paths 判定を持ち込むべきではない

    local_main_branch_guard は branch drift guard です。Allowed Paths / worktree containment / mutation target は worktree_scope_guard の責務です。現行 worktree_scope_guard は expected worktree 解決で shared worktree_catalog を使い、branch short-name と path basename の両方が issue に一致する worktree を選ぶ設計です。 worktree_catalog.py も git worktree list --porcelain -z の SSOT として導入され、newline/quoting ambiguity を避ける前提です。 Git 公式でも porcelain -z は NUL 区切りで、worktree 属性が record の先頭になる形式です。(Git)

    よって修正方針は次です。

    local_main_branch_guard:
      cwd が linked issue worktree と確認できるなら allow
      reason_code = linked_issue_worktree_context などにする
      git add pathspec の妥当性は判定しない
    
    worktree_scope_guard:
      git add 専用 classifier で pathspec / cwd / -C / wrapper / rtk envelope を判定する
    

    5. #1209 / #1198 との整合がまだ弱い

    #1209 は worktree bootstrap を controlled executor 化し、raw git worktree add と wrapper を deny-by-default に保つ契約です。 #1198 は rtk <inner> を wrapper として unwrap し、inner classifier の結果を踏襲する方向でした。 rtk 本体も hook-based agents で git status 等を rtk git status に rewrite し、rtk git add/commit/push と rtk proxy <command> を扱います。([GitHub]3)

    したがって #1215 は rtk git add -- <path> をどうするかを明示する必要があります。安全側なら初回は raw git add -- <path> のみ許可し、rtk git add は #1198 の wrapper policy に従って inner git add classifier と完全同値になる場合だけ許可、rtk proxy は block です。

    契約修正案

    #1215 に以下を追記するのが妥当です。

    ## Contract Fixes
    
    ### AC追加: raw hook fixture
    
    - tests/fixtures/hooks/issue1215-pretooluse-dedicated-worktree-git-add.json
    - tests/fixtures/hooks/issue1215-permissionrequest-dedicated-worktree-git-add.json
    
    上記は実際に block された Codex hook payload を最小化したものにする。
    `cwd`, `tool_name`, `tool_input.command`, `event` / `hook_event_name` を含める。
    secret-like path / user home は redaction する。
    
    ### AC追加: guard responsibility split
    
    - `local_main_branch_guard` は linked issue worktree context を検出した場合、branch mutation classifier に入る前に allow する。
    - reason_code は `not_local_root_context` のままでもよいが、再発防止のため `linked_issue_worktree_context` または `worktree_context_not_local_root` を追加する。
    - `local_main_branch_guard` は `git add` pathspec の Allowed Paths 判定を持たない。
    
    ### AC追加: git add pathspec policy
    
    `worktree_scope_guard` は dedicated worktree 内の publish staging として以下のみ allow する。
    
    allow:
    - `git add -- <explicit-file> [<explicit-file>...]`
    - optional: `rtk git add -- <explicit-file>...` は inner classifier が raw `git add` と同一判定になる場合のみ
    
    block/review:
    - `git add .`
    - `git add -A` / `git add --all`
    - `git add -u` / `git add --update`
    - `git add :/` / glob / pathspec magic / directory-wide pathspec
    - `git add --pathspec-from-file ...`
    - `git -C <outside> add ...`
    - `bash -lc 'git add ...'`
    - `rtk proxy ...`
    - Allowed Paths 外または expected worktree 外に解決される path
    
    ### AC追加: hook ordering regression
    
    - Codex PreToolUse では `local_main_branch_guard` → `worktree_scope_guard` の順で、dedicated worktree の explicit `git add -- <path>` が通る。
    - Codex PermissionRequest でも `local_main_branch_guard` が dedicated worktree context を誤 block しない。

    実装上の推奨

    local_main_branch_guard.py には、command 例外ではなく context 例外を追加してください。

    # pseudo
    context = classify_worktree_context(cwd, project_root)
    if context.kind == "linked_issue_worktree":
        return allow(reason_code="linked_issue_worktree_context")

    classify_worktree_context は可能なら worktree_catalog.py の list_worktrees() / find_by_realpath() / select_issue_worktrees() を再利用してください。git worktree list --porcelain -z を各 guard が独自 parse し始めると、#1197/#1137 で潰した quoting drift が再発します。

    git add の実行や検証に subprocess を使う場合は、Python 公式どおり subprocess.run([...], shell=False, capture_output=True, timeout=...) の argv list 形式を維持してください。capture_output は stdout/stderr を PIPE にし、timeout 超過時は child process を kill/wait して例外化します。([Python documentation]4) shlex.split(..., comments=False) は # をコメント扱いしないため、URL fragment や pathspec を扱う parser ではこの前提も固定した方がよいです。([Python documentation]5)

    最終コメント

    #1215 は「git add を許可する Issue」ではなく、dedicated worktree context を local_main_branch_guard が root checkout と誤認しないようにし、その後の git add は worktree_scope_guard が明示 pathspec だけ許す Issue として書き直すべきです。

    この分離を入れない場合、#1209 の publish は通るかもしれませんが、次に git add . / git add -A / rtk git add / PermissionRequest 経路で同じ種類の穴か詰まりが再発します。

  3. squne121 commented on Jun 27, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-27T09:53:18Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1215
      body_sha256: "sha256:fdd02dbff43a8e724c3aa336b2e564db168e02c9edb57edd6ea663a4ae6910af"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  4. added 2 commits that reference this issue on Jun 28, 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