Skip to content

feat(#1209): worktree bootstrap controlled executor - #1216

Merged
squne121 merged 3 commits into
mainfrom
worktree-issue-1209-worktree-bootstrap
Jun 28, 2026
Merged

squne121 merged 3 commits into
mainfrom
worktree-issue-1209-worktree-bootstrap

Conversation

@squne121

@squne121 squne121 commented Jun 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

implement-issue の worktree 作成ステップを scripts/agent-ops/worktree_bootstrap_exec.py による controlled executor 経由に置換する。これにより、raw git worktree add シェル呼び出しと rtk/raw git policy 衝突を解消し、hook allowlist での確実な allow を実現する。

主な変更内容:

  • scripts/agent-ops/worktree_bootstrap_exec.py: WORKTREE_BOOTSTRAP_RESULT_V1 を stdout JSON-only で返す controlled executor。引数検証・repo root 検証・default branch 検証・idempotent readback(ok_existing)・git subprocess (shell=False, capture_output=True, timeout付き) を実装。
  • scripts/agent-guards/worktree_bootstrap_command_policy.py: canonical command と rtk 1段 envelope の同値判定ポリシー。
  • scripts/agent-guards/local_main_branch_guard.py: executor コマンドを allow する step 13.7 を追加。
  • .claude/hooks/worktree_scope_guard.py: bootstrap executor をアクティブ issue worktree 有り状態でも allow。
  • .claude/skills/implement-issue/SKILL.md: Step 3 の raw git worktree add を executor 呼び出しに置換。
  • .claude/agents/implementation-worker.md: V1 dispatch セクションに executor 結果フィールドの説明を追加。

Checks

チェック 結果
pnpm typecheck PASS
pnpm lint PASS
pnpm test PASS
pnpm build PASS
uv run pytest tests/agent_ops/test_worktree_bootstrap_exec.py -v PASS(7/7)
uv run pytest tests/agent_ops/test_implement_issue_worktree_bootstrap_docs.py -v PASS(2/2)
uv run pytest tests/codex/test_worktree_bootstrap_command_policy.py -v PASS(5/5)
uv run pytest .claude/hooks/tests/test_worktree_scope_guard_worktree_bootstrap.py -v PASS(3/3)

Schema Change Applicability

  • decision: not_schema_change
    Python スクリプト・hook・SKILL.md の変更のみ。外部 machine-readable contract(JSON schema 等)への変更なし。python-test-plan.json は CI テストターゲットの追加であり、schema 変更には該当しない。

Schema Consumer Inventory

schema 変更なし(not_schema_change)のため、Consumer Inventory は対象外。

Safety Claim Matrix

Claim Implemented? Not controlled Evidence Follow-up
raw git worktree add は引き続きブロック Yes N/A test_given_active_issue_worktree_when_raw_git_worktree_add_runs_then_guard_blocks N/A
executor コマンドはアクティブ worktree 有り状態で allow Yes N/A test_given_active_issue_worktree_when_canonical_bootstrap_command_runs_then_guard_allows N/A
rtk 1段ラップも allow Yes N/A test_given_active_issue_worktree_when_rtk_wrapped_bootstrap_command_runs_then_guard_allows N/A
local_main_branch_guard が executor を allow Yes N/A test_given_non_default_base_ref_when_bootstrap_runs_then_base_ref_is_rejected N/A
相対パスエスケープをブロック Yes N/A test_given_invalid_relative_escape_when_bootstrap_runs_then_path_is_rejected N/A
root が default branch でない場合はブロック Yes N/A test_given_root_not_on_default_branch_when_bootstrap_runs_then_executor_blocks N/A

Notes

4 テストファイルを追加し全 AC をカバー:

  • tests/agent_ops/test_worktree_bootstrap_exec.py: AC1-AC7(7 テスト)
  • tests/agent_ops/test_implement_issue_worktree_bootstrap_docs.py: AC8, AC11(2 テスト)
  • tests/codex/test_worktree_bootstrap_command_policy.py: AC9(5 テスト)
  • .claude/hooks/tests/test_worktree_scope_guard_worktree_bootstrap.py: AC10(3 テスト)

Closes #1209(本 PR が merge された際に Issue #1209 を close します)

@github-actions

github-actions Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

Preview removed (PR #1216 closed).

@squne121

Copy link
Copy Markdown
Owner Author

test comment

@squne121

Copy link
Copy Markdown
Owner Author

PR レビュー判定: feat(#1209) worktree bootstrap controlled executor

reviewed_head_sha: b805770
verdict: REQUEST_CHANGES


Allowed Paths Gate

ALLOWED_PATHS_GATE_RESULT_V1:
status: ok
producer_role: review_subagent
worker_report_used_as_canonical: false

変更ファイル 10 件すべてが Allowed Paths 内に収まっている。


CI 状態

PASS: typecheck, lint, build, test(pnpm), actionlint, check-hook-integrity, validate-generated-artifact

FAILURE:

IN_PROGRESS(CI スナップショット時点): python-test, e2e


AC カバレッジ

AC1 (WORKTREE_BOOTSTRAP_RESULT_V1 stdout JSON): PASS
AC2 (repo root / default branch / slug 検証): PASS
AC3 (path validation): PASS
AC4 (branch_name format + git check-ref-format): PASS
AC5 (base_ref: default branch only): PASS
AC6 (idempotent readback: ok_existing): PASS
AC7 (git subprocess: shell=False, capture_output=True, timeout): PASS
AC8 (SKILL.md Step 3: executor 呼び出しに置換): PASS
AC9 (guard: canonical + rtk 1段 allow, 異形 block): PASS
AC10 (raw git worktree add / rtk proxy は引き続き block): PASS
AC11 (implementation-worker.md: executor 結果フィールド反映): PASS


ブロッカー

B1: node-backed-hook-tests CI FAILURE

3 つの新規テストファイルが .github/ci/python-test-plan.json に未登録。

  • tests/agent_ops/test_worktree_bootstrap_exec.py
  • tests/agent_ops/test_implement_issue_worktree_bootstrap_docs.py
  • tests/codex/test_worktree_bootstrap_command_policy.py

.claude/hooks/tests/test_worktree_scope_guard_worktree_bootstrap.py は既存の .claude/hooks/tests/ target でカバー済み。

必要な修正: .github/ci/python-test-plan.json の targets に tests/agent_ops/ ディレクトリと tests/codex/test_worktree_bootstrap_command_policy.py を追加する。
注意: このファイルは現行 Allowed Paths 外なので contract amendment または ANCHOR_SCOPE_REFRAME_V1 が必要。

B2: PR Body Japanese Check CI FAILURE

Closes #1209 行(11文字、日本語 0)が独立 prose block として判定されており per-block 閾値 0.1 を満たさない。
PR body 末尾に日本語 prose を追加するか、Closes #1209 を日本語 prose と統合する。


LOOP_VERDICT_V2

schema: LOOP_VERDICT_V2
verdict: REQUEST_CHANGES
merge_ready: false
reviewed_head_sha: b805770
contract_fingerprint_source: issue_contract_comment_4816031398
required_auto_actions: []
allowed_paths_gate:
status: ok
producer_role: review_subagent
worker_report_used_as_canonical: false
blockers:

  • id: B1
    kind: ci_failure
    ci_job: node-backed-hook-tests
    description: 3 新規テストファイルが python-test-plan.json に未登録。tests/agent_ops/ および tests/codex/test_worktree_bootstrap_command_policy.py を追加すること。Allowed Paths 拡張が必要。
  • id: B2
    kind: ci_failure
    ci_job: PR Body Japanese Check
    description: Closes 実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する #1209 行(11文字、日本語 0)が per-block 閾値 0.1 を満たさない。PR body を更新すること。
    fix_delta:
    description: (1) .github/ci/python-test-plan.json に tests/agent_ops/ と tests/codex/test_worktree_bootstrap_command_policy.py を追加する。contract amendment を先行させること。(2) PR body 末尾の Closes 行を日本語 prose と統合する。
    summary: 実装品質・AC カバレッジ・Allowed Paths はすべて問題なし。CI に 2 件ブロッカーあり(python-test-plan.json 未更新、PR body Japanese check)。修正後に再レビューを依頼すること。

squne121 and others added 2 commits June 28, 2026 09:52
worktree_bootstrap_exec.py と worktree_bootstrap_command_policy.py を追加し、
implement-issue の Step 3 の raw git worktree add を executor 経由に置換する。

- scripts/agent-ops/worktree_bootstrap_exec.py: WORKTREE_BOOTSTRAP_RESULT_V1
  を stdout JSON-only で返す controlled executor。AC1-AC7 対応。
- scripts/agent-guards/worktree_bootstrap_command_policy.py: canonical command
  と rtk 1段 envelope の同値判定ポリシー。AC9 対応。
- local_main_branch_guard.py: REASON_WORKTREE_BOOTSTRAP_EXECUTOR 定数追加 +
  step 13.7 で executor コマンドを allow。AC9 対応。
- worktree_scope_guard.py: _wbe_exec_command で bootstrap executor を allow。
  AC10 対応。
- implement-issue/SKILL.md Step 3 を executor 呼び出しに置換。AC8 対応。
- implementation-worker.md に worktree_bootstrap_exec.py 参照を追加。AC11 対応。
- 4 テストファイル追加: AC1-AC11 全 AC を pytest で検証。

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
tests/agent_ops/test_worktree_bootstrap_exec.py、
tests/agent_ops/test_implement_issue_worktree_bootstrap_docs.py、
tests/codex/test_worktree_bootstrap_command_policy.py を
python-test-plan.json の targets に登録し CI 自動検証を確立する。

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@squne121
squne121 force-pushed the worktree-issue-1209-worktree-bootstrap branch from 55f9e05 to 4a74228 Compare June 28, 2026 00:53

@squne121 squne121 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request Changes 解消確認(orchestrator 代理投稿)

iteration 1 レビュー判定: B1 (python-test-plan.json) / B2 (PR body Japanese) は解消済み。
B3 (CONFLICTING/DIRTY) 修正のため rebase 実施中。

@squne121

Copy link
Copy Markdown
Owner Author

判定: REQUEST_CHANGES

PR #1216 の方向性は正しいです。controlled_skill_mutation_* に混ぜず、worktree_bootstrap_command_policy.py を分離し、local_main_branch_guard / worktree_scope_guard が同じ exact command policy を参照する設計は、#1209 の契約と過去コメントの指摘に沿っています。rtk も blanket allow ではなく 1 段 envelope として扱う方針で、#1198/#1204 の「unwrap して inner command を分類する」設計と整合しています。

ただし、現状は merge ready ではありません。CI は現 head 4a742289... で主要 workflow が成功しており、ci の typecheck / lint / test / build / python-test / node-backed-hook-tests / e2e も success ですが、CI で拾えていない仕様不整合と isolation 破壊があります。

P0 ブロッカー

1. WORKTREE_BOOTSTRAP_RESULT_V1 と consumer 文書が不一致

executor の payload は worktree_path を返します。worktree フィールドは返していません。

一方、implementation-worker.md は「executor が返した worktree と branch フィールドを IMPLEMENT_RESULT_V1.worktree / IMPLEMENT_RESULT_V1.branch に反映する」と書いています。

これは実装 worker / orchestrator 側で worktree を読もうとして null になる型の不具合です。#1209 の契約上は worktree_path が正なので、推奨修正は docs 側を worktree_path -> IMPLEMENT_RESULT_V1.worktree に明記することです。あわせてテストは単なる文字列存在確認ではなく、executor の JSON key と consumer 文書の key mapping を固定してください。

2. cd "$WORKTREE" 削除により、以降の実装・verify・commit が main root で走り得る

implement-issue Step 3 は raw git worktree add と一緒に cd "$WORKTREE" も削除し、executor 実行だけに置換しています。

その後の Step 4〜7 は pnpm ...、git add、git commit、git push を通常手順として続けますが、手順上 cwd を worktree に移す確定ステップがありません。

これは #1209 の目的である「root checkout を default branch のまま維持しつつ、専用 issue worktree で実装開始」と衝突します。.claude/worktrees 配下に worktree を作っても、実作業が main root で続くなら controlled executor 化の価値が半減します。

修正は Step 3 を以下の意味にしてください。

BOOTSTRAP_JSON="$(uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ... --json)"
STATUS="$(printf '%s\n' "$BOOTSTRAP_JSON" | jq -r '.status')"
WORKTREE="$(printf '%s\n' "$BOOTSTRAP_JSON" | jq -r '.worktree_path')"

test "$STATUS" = "ok_created" -o "$STATUS" = "ok_existing"
cd "$WORKTREE"
test "$(git branch --show-current)" = "$BRANCH"

jq 依存を避けるなら Python one-liner でもよいですが、**「executor 成功後に worktree cwd へ遷移し、branch を検証する」**ことを contract test に入れるべきです。

3. AC3 の symlink escape 拒否が実装されていない

executor は normalized_worktree_path == expected_path、absolute path、backslash を見ていますが、worktree_realpath が repo root 配下に留まるかを検証していません。

#1209 の AC3 は symlink escape を明示的に拒否対象にしています。 既存の worktree_scope_guard 自体も path containment では realpath + commonpath を使う設計方針を明記しています。

攻撃例は単純です。.claude/worktrees が外部ディレクトリへの symlink になっている状態で、文字列上は .claude/worktrees/issue-1209-x を渡せます。executor はこれを通し、git worktree add は repo 外 realpath に worktree を作成し得ます。

修正案:

def _is_under(base: str, target: str) -> bool:
    try:
        return os.path.commonpath([os.path.realpath(base), os.path.realpath(target)]) == os.path.realpath(base)
    except ValueError:
        return False

worktrees_dir = os.path.join(project_root, ".claude", "worktrees")
if os.path.islink(worktrees_dir):
    block invalid_path

if not _is_under(project_root, worktree_realpath):
    block invalid_path

加えて、.claude/worktrees -> /tmp/outside の symlink fixture を必ず追加してください。

P1: 設計・テスト強化

4. 作成後 readback が既存 state より弱い

既存 worktree の ok_existing 判定は catalog、branch、detached、directory exists、basename を確認しています。

しかし git worktree add 成功後の verification は、catalog entry が存在して branch short-name が一致するかだけです。

git worktree add -b <branch> <path> <base-ref> 自体は妥当です。Git 公式でも worktree add は <commit-ish> を checkout し、-b は新 branch 作成、-B は既存 branch reset、--force は safeguard 上書きなので、この PR が --no-guess-remote -b に限定している点は正しいです。 (Git)

ただし、postcondition は _validate_existing_state() を再利用して同じ条件に揃えるべきです。成功直後だけ検証条件が弱いのは、将来の変更で regression を生みます。

5. exact command policy は概ね良いが、negative fixture が不足

policy は uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ... --json の exact argv と rtk <same> の 1 段 envelope を許可し、flag order、--flag=value、path、branch、default branch、cwd、repo slug を固定しています。

この方向性は RTK の実態とも合っています。RTK は hook-based agent の Bash command を rtk git status のように rewrite し、git add/commit/push/pull などの mutation や rtk proxy <command> raw passthrough も扱うため、rtk 自体を信頼して blanket allow してはいけません。([GitHub]2)

不足している negative fixture は少なくとも以下です。

bash -lc 'uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ...'
rtk bash -lc 'uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ...'
env FOO=bar uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ...
uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ... --json --json
uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ... EXTRA
rtk uv run python3 scripts/agent-ops/worktree_bootstrap_exec.py ... --unknown

#1209 のコメントでも rtk proxy / bash -lc / env prefix / duplicate flag / extra positional を deny matrix に含める方針が示されています。

6. --json は parser で受けるだけで、executor 本体では必須化されていない

executor は argparse で --json を action="store_true" として定義していますが、args.json を検査していません。

guard policy 側は exact command class として --json ありを要求するので通常経路では問題化しにくいです。ただし executor 単体の AC1 は「--json を受け取り stdout JSON-only」と読めるため、直接実行時も --json なしは blocked invalid_args にする方が契約として堅いです。

Python の subprocess.run(..., capture_output=True, timeout=...) 利用は外部仕様上も正しいです。capture_output は stdout/stderr を PIPE に取り、timeout は child timeout 時に例外化するため、PR の subprocess 設計は良いです。 ([Python documentation]3)

良い点

  • worktree_catalog.py の SSOT 再利用は正しいです。git worktree list --porcelain -z を parse し、worktree_realpath / branch_ref / detached / exists_on_disk を扱えるため、実装: git ref/worktree probe を script 化し、shell quoting と compound command 起因の迷走を削減する #1197 の「shell quoting と compound command を減らす」方向と整合します。
  • branch 名は expected string と git check-ref-format --branch の二段で見ており、Git ref 名の危険文字制約とも整合します。 (Git)
  • --base-ref を default branch 正規化可能な ref に限定し、任意 SHA / HEAD~1 を拒否する設計は正しいです。
  • 過去レビューで出ていた python-test-plan.json 未登録と PR body Japanese check は、現 head では解消済みと見てよいです。

PR に投げるべきレビュー本文

REQUEST_CHANGES

現 head の CI は成功しているが、worktree bootstrap contract と実作業 isolation に P0 が残っています。

1. `WORKTREE_BOOTSTRAP_RESULT_V1` は `worktree_path` を返す一方、`implementation-worker.md` は executor が `worktree` を返す前提になっています。`worktree_path -> IMPLEMENT_RESULT_V1.worktree` の mapping に修正し、contract test を追加してください。

2. `implement-issue` Step 3 で旧 `cd "$WORKTREE"` が削除され、executor 成功後に cwd を issue worktree へ移す確定手順がありません。このままだと Step 4〜7 の `pnpm` / `git add` / `git commit` / `git push` が main root で走り得ます。executor JSON を読み、`status` 確認後に `cd "$WORKTREE"` し、`git branch --show-current == "$BRANCH"` を検証する手順を追加してください。

3. AC3 の symlink escape 拒否が未実装です。文字列上の `.claude/worktrees/issue-<N>-<slug>` は検証されていますが、`realpath` が repo root 配下に留まることを `commonpath` で確認していません。`.claude/worktrees` symlink fixture を追加し、repo 外 escape を `invalid_path` で block してください。

4. `git worktree add` 成功後の readback が既存 state の `ok_existing` 判定より弱いです。作成後も `_validate_existing_state()` 相当で non-detached / exists_on_disk / basename / branch_ref を再検証してください。

以上が直るまでは merge 不可です。

結論として、PR #1216 は 方向性は採用、現実装は差し戻しです。特に cd "$WORKTREE" 欠落と symlink escape は、controlled executor 化の目的そのものを壊すため、CI green でも通すべきではありません。

…ing, cd step, negative fixtures

B1: implementation-worker.md を修正して executor が返す `worktree_path` を
    `IMPLEMENT_RESULT_V1.worktree` にマップすると明記(旧: `worktree` フィールドと誤記)。
    test_implement_issue_worktree_bootstrap_docs.py に JSON key / consumer mapping 検証テストを追加。

B2: implement-issue SKILL.md Step 3 に executor 成功後の cwd 移行フローを追加:
    BOOTSTRAP_JSON 取得 → status 確認 → worktree_path 取得 → cd "$WORKTREE" → branch 検証。
    docs テストに worktree_path と cd の存在確認テストを追加。

B3: worktree_bootstrap_exec.py に symlink escape guard を追加:
    _is_under() で realpath が project_root 配下かを確認。
    .claude/worktrees/ 自体が symlink の場合も blocked/invalid_path を返す。
    test_worktree_bootstrap_exec.py にシンボリックリンク escape fixture を追加。

B4: 作成後 readback を _validate_existing_state() 相当に強化:
    non-detached / exists_on_disk / basename / branch_ref を再検証。

B5: 否定 fixture を追加:
    test_worktree_bootstrap_command_policy.py: bash -lc / rtk bash -lc / env prefix /
      duplicate --json / extra positional / rtk + unknown flag を block 検証。
    test_worktree_scope_guard_worktree_bootstrap.py: 同様の否定 fixture を追加。

B6: worktree_bootstrap_exec.py で --json フラグ未指定時に blocked/invalid_args を返す。
    test_worktree_bootstrap_exec.py に --json 省略テストを追加。

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@squne121
squne121 marked this pull request as ready for review June 28, 2026 01:36
@squne121
squne121 merged commit d5d6743 into main Jun 28, 2026
18 checks passed
@squne121
squne121 deleted the worktree-issue-1209-worktree-bootstrap branch June 28, 2026 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

実装: implement-issue の worktree bootstrap を controlled executor 化し、rtk/raw git policy 衝突を解消する

1 participant