Repository navigation
Closed
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phasetriage-requiredNeeds owner triage for priority decision
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: none
goal_ref: "root からの preflight.run が stale worktree 由来の tmp/check_live.json を参照して fail-closed する問題を検出・隔離する"
change_kind: guardrailParent Issue
none
Parent Goal Ref
- Goal: preflight.run の fail-closed 方針は維持したまま、target issue と無関係な stale worktree runtime state が混入して valid な refinement 作業を止める問題を解消する。
- Desired Destination: root checkout から
preflight.runを実行しても、過去 issue の.claude/worktrees/issue-*/tmp/check_live.jsonを出力先として選ばない。stale state が検出された場合は、原因と復旧手順が分かる専用 reason_code で fail-closed する。
Current Validated Scope
- 観測事象: 実装: CI の changed-markdown 日本語チェックが shallow checkout の no-merge-base で実質 no-op になる問題を修正する #1235 の issue-refinement-loop 中、root からの
preflight.runが stale worktree 由来の.claude/worktrees/issue-1220-refinement/tmp/check_live.jsonで fail-closed した。 - 実装: CI の changed-markdown 日本語チェックが shallow checkout の no-merge-base で実質 no-op になる問題を修正する #1235 の本文改善自体は別経路で完了済み。
- canonical artifact writer(
.claude/artifacts/issue-refinement-loop/<issue_number>/...)はすでに issue-scoped であるため、混入点は主にTMPDIR/TEMP/TMP/XDG_*/ child stdout のARTIFACT:projection / target issue authority binding にある。 - 現行
skill_runtime_execの主防御は postcondition のunauthorized write path検出であり、write attempt 自体を防ぐ AC を満たすには child 実行前または stdout 公開前の fail-closed が必要である。
Remaining Parent Gaps
- なし(単独改善)。follow-up issue へ分割せず、Allowed Paths 内の runtime guard / docs / regression test 更新で収束させる。
Background
対象 issue は #1235 だったが、preflight が参照した path は .claude/worktrees/issue-1220-refinement/tmp/check_live.json だった。これは target issue number / active runtime / scratch output path の対応関係が崩れた状態を示す。
この問題は guardrail を緩めて通すのではなく、stale state を target issue 実行の前段で検出し、出力先を target issue 由来の安全な scratch path に正規化するか、正規化不能なら専用エラーで止めるべきである。
Outcome
root checkout から preflight.run を実行しても、過去 issue の .claude/worktrees/issue-*/tmp/check_live.json が target issue の出力先として使われない。
stale worktree/runtime state が混入している場合は、child 実行前または child stdout 公開前に fail-closed し、target issue と stale issue/path の不一致が分かる専用 reason_code と復旧メッセージを返す。
In Scope
preflight.run/skill_runtime_exec(root no-worktree 実行含む)で、CLI の--issue-numberを唯一の target issue authority として扱う。- child 実行前に
TMPDIR/TEMP/TMP/XDG_CACHE_HOME/XDG_STATE_HOME/XDG_DATA_HOME/XDG_CONFIG_HOMEを検査し、repo 内.claude/worktrees/**/tmp配下を指す stale runtime state を検出する。 - child stdout に含まれる
ARTIFACT:projection が.claude/artifacts/issue-refinement-loop/<target_issue>/配下以外を指す場合、stdout をそのまま公開せず fail-closed する。 - path containment 判定に
realpath+commonpath系の実体パス比較を使い、symlink /../ 絶対パス混在を文字列 prefix 判定で見逃さない。 - mismatch または stale path が検出された場合に、
reason_code: stale_worktree_runtime_state相当の専用分類で fail-closed する。 - fail-closed メッセージに、target issue、検出された stale path、推奨復旧手順を public-safe に含める。
- stale env / stale stdout artifact projection を再現する regression test を追加する。
docs/dev/agent-skill-boundaries.mdに、rootpreflight.runの scratch/artifact root は.claude/artifacts/issue-refinement-loop/<target_issue>/のみであり、.claude/worktrees/**/tmpを使用しないことを明記する。
Out of Scope
- root/worktree guard の無効化または緩和。
.claude/worktrees/**への broad write allow-list 追加。- stale worktree の自動 cleanup。
- 実装: CI の changed-markdown 日本語チェックが shallow checkout の no-merge-base で実質 no-op になる問題を修正する #1235 の本文契約、AC/VC、workflow 修正方針の再変更。
- Issue/PR 本文更新フロー全体の再設計。
Acceptance Criteria
- AC1:
preflight.run/skill_runtime_execは child 実行前の env/path precheck で stale worktree runtime state を検出し、stale_worktree_runtime_stateで fail-closed できる。 - AC2: child stdout の
ARTIFACT:projection が.claude/artifacts/issue-refinement-loop/<target_issue>/配下以外を指す場合、raw stdout を公開せず fail-closed できる。 - AC3: fail-closed 出力には public-safe な範囲で
reason_code=stale_worktree_runtime_state、target_issue、stale_path、recoveryが含まれる。 - AC4: 既存の
unauthorized write pathpostcondition は維持され、主防御ではなく二次防波堤として継続機能する。 - AC5: regression test が stale worktree state を seed した後に別 issue の preflight を実行するケースを再現し、別 issue tmp path への write attempt が発生しないことを検証する。
- AC6: 既存の worktree scope guard / local main branch guard の安全性は維持され、許可範囲の拡大で問題を隠していない。
Verification Commands
# AC1
$ rg -n "test_stale_worktree_runtime_state_precheck" scripts/agent-guards/tests/test_skill_runtime_preflight_no_worktree.py# AC2
$ rg -n "test_stale_worktree_artifact_projection_fail_closed" scripts/agent-guards/tests/test_skill_runtime_preflight_no_worktree.py# AC3
$ rg -n "stale_worktree_runtime_state" scripts/agent-guards/skill_runtime_exec.py# AC4
# baseline-expect: pass
$ rg -n "unauthorized write path" scripts/agent-guards/tests/test_skill_runtime_preflight_no_worktree.py# AC5
$ rg -n "test_stale_worktree_preflight_does_not_write_other_issue_tmp" scripts/agent-guards/tests/test_skill_runtime_preflight_no_worktree.py# AC6
# baseline-expect: pass
$ rg -n "REASON_SKILL_RUNTIME_EXECUTOR" tests/codex/test_local_main_branch_guard.pyRuntime Verification Applicability
decision: immediate
reason: stale worktree state はローカルで seed 可能な runtime state / path resolution 問題であり、runtime guard precheck と controlled local invocation、unit/regression test で検証できる。
applicable_acs: [AC1, AC2, AC5]
execution_environment:
tools:
- uv
- pytest
- rg
auth: none
network: none
notes: root checkout からの controlled local invocation と `scripts/agent-guards/tests/test_skill_runtime_preflight_no_worktree.py` fixture で stale env / stale artifact projection を seed する。
skip_conditions: |
`uv` または `pytest` が実行不能で local regression test を起動できない場合。
runtime fixture が stale path seed を再現できず、AC1 / AC2 / AC5 の実動検証が成立しない場合。
fallback_policy: |
fallback や事後検出のみを PASS としない。
runtime precheck を実行できない場合は SKIP または fail-closed として扱う。
artifact_requirements: |
`stale_worktree_runtime_state` を含む pytest / stderr 証跡。
stale env、stdout artifact mismatch、normal path regression、postcondition preservation を示すテスト結果。Allowed Paths
scripts/agent-guards/skill_runtime_exec.pyscripts/agent-guards/skill_runtime_command_policy.pyscripts/agent-guards/tests/test_skill_runtime_preflight_no_worktree.py.claude/skills/issue-refinement-loop/scripts/run_refinement_preflight.py.claude/skills/issue-refinement-loop/scripts/command_registry.py.claude/skills/issue-contract-review/tests/codex/test_local_main_branch_guard.pydocs/dev/agent-skill-boundaries.md
Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
- root/worktree guard の許可範囲拡大が必要と判明した場合。
--issue-number以外(例:LOOP_ISSUE_NUMBER依存)を target issue authority として採用する必要が出た場合。local_main_branch_guardまたはworktree_scope_guardを緩和する方向の変更が必要になった場合。- stale worktree の自動 cleanup が必要と判明した場合。
- Issue/PR 本文更新フロー全体の再設計が必要になった場合。
Required Skills
なし
Source
- related_issue: 実装: CI の changed-markdown 日本語チェックが shallow checkout の no-merge-base で実質 no-op になる問題を修正する #1235
- owner anchor comment: 実装: preflight.run の stale worktree state を検出・隔離する #1236 (comment)
- observed note: root からの preflight が stale worktree 由来の
.claude/worktrees/issue-1220-refinement/tmp/check_live.jsonで fail-closed した。 - key findings:
- Blocker 1: canonical artifact writer は issue-scoped であり、混入点は env / stdout artifact projection / authority binding 側にある。
- Blocker 2: 現行
skill_runtime_execは事後検出型であり、AC5 を満たすには pre-exec または pre-publish fail-closed が必要である。 - Blocker 3: root no-worktree 例外では
--issue-numberを唯一の authority として再束縛する必要がある。 - Blocker 4: hook 緩和ではなく runtime 境界検査で解決する。
- Blocker 5: regression test は stale env / stale artifact projection / normal path / guard preservation を区別して追加する。
- dedupe search:
"unauthorized write path" "stale worktree" preflight: open issue 0 件"check_live.json" "issue-1220-refinement": issue 0 件
Delivery Rule
1 Issue = 1 PR。- fail-closed 方針を緩める変更は禁止。
--issue-number以外の issue 決定源に依存する変更は禁止。- 実装 PR では stale env / stdout artifact mismatch / normal path regression / unauthorized write path preservation の evidence を残す。
Scope Delta(任意)
事後検出だけでは別 issue tmp path への write attempt 自体を防げないため、本 issue では child 実行前と stdout 公開前の fail-closed を contract の中心に引き上げる.
Metadata
Metadata
Assignees
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phasetriage-requiredNeeds owner triage for priority decision
Activity
squne121 commentedon Jun 29, 2026
判定
REQUEST_CHANGES 相当。#1236 は有効な Issue だが、現状のまま実装すると「stale path を検出する」ではなく「実行後の unauthorized write で偶然止まる」だけになりやすい。
#1236 の要求は、#1235 実行中に target issue
1235とは無関係な.claude/worktrees/issue-1220-refinement/tmp/check_live.jsonが混入した問題を、preflight.run/skill_runtime_exec周辺で検出・隔離し、stale_worktree_runtime_stateで fail-closed することです。自動 cleanup や.claude/worktrees/**broad allow-list は明示的に禁止されています。主要な敵対的レビュー
Blocker 1: 既存実装の canonical artifact 出力先はすでに issue-scoped であり、真の混入点がまだ特定されていない。
run_refinement_preflight.pyの正規 artifact 出力先は.claude/artifacts/issue-refinement-loop/<issue_number>/で、raw_issue_snapshot.json/planner_input.json/refinement_preflight_result_v1.jsonを issue number から生成しています。つまり、単純にこの writer を直しても.claude/worktrees/issue-1220-refinement/tmp/check_live.json混入の説明になりません。混入点はむしろTMPDIR/XDG_*/ child stdout のARTIFACT:projection / 旧 scratch path consumer のいずれかです。Blocker 2: 現在の
skill_runtime_execは「実行後検出」であり、AC5 の「write attempt が発生しない」を満たせない。skill_runtime_exec.pyは child command を実行した後に_find_unauthorized_repo_changes()で repo drift を検査し、違反時にskill_runtime_exec: unauthorized write path ...で exit 2 します。これは安全な後段防御としては有効ですが、stale path への write attempt 自体は child 実行後まで発生し得ます。#1236 の AC5 は「stale state を seed した後に別 issue preflight を実行し、別 issue tmp path への write attempt が発生しない」ことなので、pre-exec validation が必要です。Blocker 3: root no-worktree 例外が target issue / active issue の結合を弱めている。
preflight.runは root checkout から実行可能な特権 runtime として allowlist されています。skill_runtime_command_policy.pyは exact executor command を検査し、preflight.runは root no-worktree policy によって active issue worktree 解決を省略できます。これは設計上必要ですが、同時に「target issue = parsed--issue-number」を scratch/artifact scope の唯一の authority として再束縛しないと、古い worktree state を検出できません。Blocker 4: hook 側を緩めて直す案は不可。
local_main_branch_guardとworktree_scope_guardは exactskill_runtime_exec.pyを早期 allow しています。したがって修正点は hook の broad allow ではなく、skill_runtime_exec.py/skill_runtime_command_policy.pyの runtime 境界検査です。#1236 自体も root/worktree guard の無効化・緩和、.claude/worktrees/**への broad write allow-list を Out of Scope にしています。Blocker 5: regression test がまだ一般的な unauthorized write 後検出しか見ていない。
既存テストは root no-worktree preflight が artifact root へ書けること、direct preflight が block されること、外部 write が postcondition で
unauthorized write pathになることを確認しています。しかし、.claude/worktrees/issue-1220-refinement/tmp/check_live.jsonを seed し、target1235の実行前にstale_worktree_runtime_stateで止まるケースは未カバーです。外部実装観点での補強
Python
tempfileはTMPDIR、TEMP、TMPの順にデフォルト temporary directory を探索し、結果を cache します。したがって stale worktree tmp path が環境変数経由で残っている場合、child 側がtempfile/mkstemp/mkdtempを使うだけで旧 issue path に書く可能性があります。skill_runtime_execは child 実行前にこれらの scratch 系 env を検査し、少なくとも.claude/worktrees/**配下は target issue と一致しても root preflight では拒否するのが安全です。([Python documentation]1)Git worktree は 1 repo に main worktree と複数 linked worktree を持てる仕組みで、linked worktree は branch/path/metadata を持ちます。stale worktree 問題では「path basename に issue number があるから安全」ではなく、repo root からの realpath containment と issue-number binding を同時に見る必要があります。([Git]2)
Python
PurePath.is_relative_to()は string-based で、filesystem へアクセスせず..も特別扱いしません。symlink /../ absolute path 混在を扱う guardrail では、既存コードと同じくrealpath+commonpath系の実体パス containment を使うべきです。([Python documentation]3)#1235 / PR #1238 のレビューで出た「process substitution 内の失敗を no-op に落とす」問題も教訓になります。Bash の process substitution は非同期に実行され、filename として展開されるため、fail-closed guard では内部 command の exit status を別に検査する必要があります。#1236 でも「stale path が見えたら後段でたまたま落ちる」ではなく、明示分類して止める設計に寄せるべきです。([GNU]4)
推奨修正方針
最小で堅い実装は次です。
scripts/agent-guards/skill_runtime_command_policy.pyにSTALE_WORKTREE_RUNTIME_STATE = "stale_worktree_runtime_state"を追加する。.claude/worktrees/(?:worktree-)?issue-<N>-*/tmp/...または.claude/worktrees/**を検出する helper を追加する。判定は regex だけにせず、project_rootrealpath 配下であることを先に確認する。scripts/agent-guards/skill_runtime_exec.pyで child 実行前にTMPDIR/TEMP/TMP/XDG_CACHE_HOME/XDG_STATE_HOME/XDG_DATA_HOME/XDG_CONFIG_HOMEを検査する。.claude/worktrees/**配下ならstale_worktree_runtime_stateで fail-closed。ARTIFACT:projection の path が.claude/artifacts/issue-refinement-loop/<target_issue>/配下に収まるか検査する。違反時は raw stdout を出さず、public-safe な stderr にreason_code,target_issue, repo-relative stale path, recovery を出す。docs/dev/agent-skill-boundaries.mdに、rootpreflight.runの scratch/artifact root は target issue 由来の.claude/artifacts/issue-refinement-loop/<issue>/のみで、.claude/worktrees/**/tmpは使用禁止と明記する。追加すべきテスト
.claude/worktrees/issue-1220-refinement/tmpを作り、TMPDIRをそこへ向け、--issue-number 1235でskill_runtime_exec.py --command-id preflight.runを起動する。child が実行される前に exit 2、stderr にstale_worktree_runtime_state、target issue、stale path、recovery が出ること。stale file が作られていないこと。ARTIFACT: check_live: .claude/worktrees/issue-1220-refinement/tmp/check_live.jsonを出す。executor が stdout をそのまま表示せず fail-closed すること。1235で.claude/artifacts/issue-refinement-loop/1235/...のみを書き、既存 root no-worktree preflight は pass すること。unauthorized write path/worktree_scope_guard/local_main_branch_guard検索 VC が継続 pass すること。Codex / Claude Code 実行プロンプト
PR は Draft で作成し、本文に
Closes #1236を含めてください。PR 本文には、stale env / stdout artifact mismatch / normal path regression / unauthorized write path preservation の test evidence を記載してください。[-]改善: preflight.run の stale worktree state を検出・隔離する[/-][+]実装: preflight.run の stale worktree state を検出・隔離する[/+]squne121 commentedon Jun 29, 2026
Contract Review Result
test: align stale worktree VC markers (#1236)
fix: narrow stale worktree env detection (#1236)
fix: fail closed stale worktree preflight paths (#1236)
docs: localize runtime guardrail headings (#1236)
fix: guard stale worktree runtime state (#1236) (#1242)