Skip to content

実装: preflight.run の stale worktree state を検出・隔離する #1236

Closed

Description

@squne121

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: guardrail

Parent 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

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 に、root preflight.run の scratch/artifact root は .claude/artifacts/issue-refinement-loop/<target_issue>/ のみであり、.claude/worktrees/**/tmp を使用しないことを明記する。

Out of Scope

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 path postcondition は維持され、主防御ではなく二次防波堤として継続機能する。
  • 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.py

Runtime 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.py
  • scripts/agent-guards/skill_runtime_command_policy.py
  • scripts/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.py
  • docs/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 の中心に引き上げる.

Activity

  1. squne121 commented on Jun 29, 2026

    @squne121
    OwnerAuthor

    判定

    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 は exact skill_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 し、target 1235 の実行前に 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)

    推奨修正方針

    最小で堅い実装は次です。

    1. scripts/agent-guards/skill_runtime_command_policy.py に STALE_WORKTREE_RUNTIME_STATE = "stale_worktree_runtime_state" を追加する。
    2. repo-relative / absolute path を受け取り、.claude/worktrees/(?:worktree-)?issue-<N>-*/tmp/... または .claude/worktrees/** を検出する helper を追加する。判定は regex だけにせず、project_root realpath 配下であることを先に確認する。
    3. 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。
    4. child stdout を user に流す前に、ARTIFACT: projection の path が .claude/artifacts/issue-refinement-loop/<target_issue>/ 配下に収まるか検査する。違反時は raw stdout を出さず、public-safe な stderr に reason_code, target_issue, repo-relative stale path, recovery を出す。
    5. 既存の unauthorized write postcondition は残す。これは最後の防波堤であり、実装: preflight.run の stale worktree state を検出・隔離する #1236 の主防御ではない。
    6. docs/dev/agent-skill-boundaries.md に、root preflight.run の scratch/artifact root は target issue 由来の .claude/artifacts/issue-refinement-loop/<issue>/ のみで、.claude/worktrees/**/tmp は使用禁止と明記する。

    追加すべきテスト

    • stale env test: .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 が作られていないこと。
    • stdout artifact mismatch test: child fixture が ARTIFACT: check_live: .claude/worktrees/issue-1220-refinement/tmp/check_live.json を出す。executor が stdout をそのまま表示せず fail-closed すること。
    • normal path test: target 1235 で .claude/artifacts/issue-refinement-loop/1235/... のみを書き、既存 root no-worktree preflight は pass すること。
    • guard preservation test: 既存の unauthorized write path / worktree_scope_guard / local_main_branch_guard 検索 VC が継続 pass すること。

    Codex / Claude Code 実行プロンプト

    https://github.com/squne121/loop-protocol/issues/1236 を実装してください。
    
    目的は、root checkout からの `preflight.run` / `skill_runtime_exec` 実行時に、target issue と無関係な stale worktree runtime state、特に `.claude/worktrees/issue-1220-refinement/tmp/check_live.json` のような旧 issue scratch path が混入する問題を、実行前または stdout 公開前に検出して fail-closed することです。
    
    必ず守ること:
    
    1. Allowed Paths は Issue #1236 の範囲内に限定する。
       - `scripts/agent-guards/**`
       - `.claude/skills/issue-refinement-loop/**`
       - `.claude/skills/issue-contract-review/**`
       - `tests/**`
       - `docs/dev/**`
    
    2. root/worktree guard の無効化・緩和は禁止。
    3. `.claude/worktrees/**` への broad write allow-list 追加は禁止。
    4. stale worktree の自動 cleanup はしない。
    5. 既存の unauthorized write path postcondition は削除しない。
    6. `preflight.run` の target issue authority は CLI の `--issue-number` として扱い、`LOOP_ISSUE_NUMBER` や古い worktree state に依存しない。
    7. `stale_worktree_runtime_state` reason_code を導入する。
    8. child 実行前に `TMPDIR` / `TEMP` / `TMP` / `XDG_CACHE_HOME` / `XDG_STATE_HOME` / `XDG_DATA_HOME` / `XDG_CONFIG_HOME` を検査し、repo 内 `.claude/worktrees/**` 配下を指す場合は `stale_worktree_runtime_state` で fail-closed する。
    9. child stdout を user に流す前に、`ARTIFACT:` projection の path を検査し、`.claude/artifacts/issue-refinement-loop/<target_issue>/` 配下以外を指す場合は fail-closed する。
    10. fail-closed message は public-safe にし、少なくとも `reason_code`, `target_issue`, `stale_path`, `recovery` を含める。絶対パスや secret/env 値は出さず、repo-relative path に正規化する。
    11. path containment は symlink / `..` / absolute path 混在を考慮し、`realpath` + `commonpath` 系で実装する。string prefix 判定だけは禁止。
    12. `docs/dev/agent-skill-boundaries.md` に、root `preflight.run` の scratch/artifact root は `.claude/artifacts/issue-refinement-loop/<target_issue>/` のみであり、`.claude/worktrees/**/tmp` を使わないことを明記する。
    
    追加テスト:
    
    - stale env を seed する regression test:
      - `.claude/worktrees/issue-1220-refinement/tmp` を作る。
      - `TMPDIR` など scratch env をその path に向ける。
      - `--issue-number 1235` で `skill_runtime_exec.py --command-id preflight.run` を実行する。
      - child の stale write attempt が発生せず、exit 2、stderr に `stale_worktree_runtime_state` / target issue / stale path / recovery が出ることを確認する。
    
    - stdout artifact mismatch test:
      - child fixture が `.claude/worktrees/issue-1220-refinement/tmp/check_live.json` を `ARTIFACT:` として出す。
      - executor が raw stdout を流さず、`stale_worktree_runtime_state` で fail-closed することを確認する。
    
    - normal path regression:
      - target issue の `.claude/artifacts/issue-refinement-loop/<issue>/` だけを使う root preflight は pass すること。
    
    検証コマンド:
    
    ```bash
    uv run --locked pytest tests -k 'stale_worktree or skill_runtime_exec or preflight'
    rg -n "stale_worktree_runtime_state|target_issue|stale_path|recovery" scripts .claude tests docs/dev
    rg -n "unauthorized write path|worktree_scope_guard|local_main_branch_guard" scripts .claude tests

    PR は Draft で作成し、本文に Closes #1236 を含めてください。PR 本文には、stale env / stdout artifact mismatch / normal path regression / unauthorized write path preservation の test evidence を記載してください。

  2. changed the title [-]改善: preflight.run の stale worktree state を検出・隔離する[/-] [+]実装: preflight.run の stale worktree state を検出・隔離する[/+] on Jun 29, 2026
  3. added
    enhancement
    phase/implementation
    agent/implementer
    triage-required
    on Jun 29, 2026
  4. squne121 commented on Jun 29, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-29T12:33:17Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1236
      body_sha256: "sha256:917384238e7ec973e04bf24bac85682b01cb1790c2a3b3519ee4e7f9ac68d10c"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
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/implementationtriage-required

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions