Repository navigation
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
goal_ref: "issue-refinement-loop の各 phase で許可される router 呼び出しを deterministic に制限し、schema probing と premature termination を防ぐ"
change_kind: mixed
risk_class: agent_loop_control
affected_skill:
- .claude/skills/issue-refinement-loop
failure_modes:
- premature_decide_next_loop_action
- invalid_loop_state_schema_probe
- premature_scope_signal_guard_escalation
- subagent_churn_after_invalid_stateParent Issue
N/A — このIssueは standalone implementation として起票された(parent tracker なし)。
Parent Goal Ref
issue-refinement-loop の実行品質改善 — preflight 直後の誤った scope_signal_guard hard stop による human_escalation を防ぐ。
Current Validated Scope
本 Issue の AC1〜AC6 と Allowed Paths に記載の範囲に限定する。phase gate と ISSUE_REFINEMENT_PHASE_STATE_V1 契約の導入、references 同期、regression fixture 追加まで。
Remaining Parent Gaps
N/A — standalone implementation のため parent gap は存在しない。
Outcome
issue-refinement-loop の各 phase で呼び出せる router script を deterministic に制限し、preflight 直後の誤った decide_next_loop_action.py 呼び出しと human_escalation 誤終了を防ぐ。scope_signal_guard.triggered の意味を phase-sensitive に分離し、pre-rewrite では continue_investigation、post-rewrite では hard_stop_candidate として扱う。phase は既存の LOOP_STATE_V1 に混ぜず、別契約 ISSUE_REFINEMENT_PHASE_STATE_V1 として build_refinement_phase_state.py が生成する。
In Scope
.claude/skills/issue-refinement-loop/scripts/decide_next_loop_action.pyへの phase gate 追加build_refinement_phase_state.pyとvalidate_refinement_phase_transition.pyの新規作成test_refinement_phase_gate.pyの新規作成(regression fixture 含む)test_decide_next_loop_action.pyの既存 scope_signal_guard テストへの phase 条件追加SKILL.mdの procedure 更新references/loop-state.md、scope-signal-guard.md、termination-policy.mdの phase semantics 同期
Out of Scope
LOOP_STATE_V1スキーマ (loop_state.schema.json) へのphaseフィールド追加- Allowed Paths 外のファイル変更
decide_next_loop_action.pyの既存 CLI インターフェース変更planner判定ロジックの変更
背景
issue-refinement-loop 実行中、preflight pass の直後に decide_next_loop_action.py が呼ばれた。しかし scope_signal_guard の停止判定は rewrite / review 後に新規 scope が追加された場合の判定であり、preflight 直後に呼ぶものではない。
その結果、agent は以下のような schema probing を繰り返した。
- unexpected property
- required property missing
termination_reasonmissingdelivery_rollupmissingreason_codeunexpected- 最終的に
human_escalationと誤終了
この挙動は token 消費・context 汚染・人間への誤報告を増やす。
方針
issue-refinement-loop に phase gate を追加し、各 phase で呼び出せる script / router を明示する。phase は LOOP_STATE_V1 に追加せず、別契約 ISSUE_REFINEMENT_PHASE_STATE_V1 として扱う。禁止 phase への router 呼び出しは human_escalation ではなく ISSUE_REFINEMENT_ROUTER_ERROR_V1 を返し NEXT_ACTION: rebuild_phase_state を設定する。human_escalation は phase gate 通過後のみ許可する。
Phase Model
ISSUE_REFINEMENT_PHASE_V1:
phases:
- preflight
- investigation
- review
- rewrite
- post_rewrite_check
- decide_next_action
- publish
- terminatePhase State Contract
LOOP_STATE_V1 に phase フィールドを追加しない。代わりに build_refinement_phase_state.py が ISSUE_REFINEMENT_PHASE_STATE_V1 を生成し、router scripts はこの phase state を先に consume する。
ISSUE_REFINEMENT_PHASE_STATE_V1:
phase: preflight | investigation | review | rewrite | post_rewrite_check | decide_next_action | publish | terminate
source_artifact:
kind: refinement_preflight_result_v1 | issue_review_result_compact_v1 | issue_author_result_compact_v1 | loop_state_v1
path: string
loop_state_path: string | null
planner_result_path: string | null
review_result_path: string | null
allowed_routers:
- string
forbidden_routers:
- string
scope_signal_semantics:
triggered_meaning: continue_investigation | hard_stop_candidate | ignored
hard_stop_eligible: booldecide_next_loop_action.py は --phase-state-file を先に読んで、phase gate を LOOP_STATE_V1 schema validation より前に評価する。
Router Rule
decide_next_loop_action.py を呼べるのは以下の場合だけにする。
allowed_decide_next_loop_action:
- phase: post_rewrite_check
- phase: decide_next_action以下では呼び出し禁止。
forbidden_decide_next_loop_action:
- preflight
- investigation
- reviewpreflight 直後に scope_signal_guard.triggered == true が見えても、それは「調査・レビュー対象 signal」であり、即 human escalation ではない。planner の investigation_policy / web_research_policy に従って investigation | web_research | review のいずれかへ進む。
Router Error Contract
decide_next_loop_action.py が禁止 phase または不完全 input を受け取った場合、human_escalation ではなく ISSUE_REFINEMENT_ROUTER_ERROR_V1 を返す。
ISSUE_REFINEMENT_ROUTER_ERROR_V1:
schema_version: ISSUE_REFINEMENT_ROUTER_ERROR_V1
status: router_error
reason_code: phase_not_allowed | loop_state_invalid | missing_phase_state
phase: string
attempted_router: string
allowed_routers:
- string
forbidden_reason: string
missing_fields:
- string
unexpected_fields:
- string
next_action: rebuild_phase_state追加・変更ファイル
.claude/skills/issue-refinement-loop/scripts/build_refinement_phase_state.py.claude/skills/issue-refinement-loop/scripts/validate_refinement_phase_transition.py.claude/skills/issue-refinement-loop/scripts/decide_next_loop_action.py.claude/skills/issue-refinement-loop/SKILL.md.claude/skills/issue-refinement-loop/tests/test_refinement_phase_gate.py.claude/skills/issue-refinement-loop/references/loop-state.md.claude/skills/issue-refinement-loop/references/scope-signal-guard.md.claude/skills/issue-refinement-loop/references/termination-policy.md
Acceptance Criteria
- AC1: preflight phase では
decide_next_loop_action.pyを通常 routing として実行できない。scope_signal_guard.triggered: trueを含む preflight planner output でもhuman_escalationは返さず、planner のinvestigation_policy/web_research_policyに従ってinvestigation | web_research | reviewのいずれかへ進む。 - AC2:
decide_next_loop_action.pyは phase gate をLOOP_STATE_V1schema validation より先に評価する。禁止 phase の場合、ISSUE_REFINEMENT_ROUTER_ERROR_V1を一回だけ出力し、NEXT_ACTION: rebuild_phase_stateを返す。 - AC3:
scope_signal_guard.triggeredは phase-sensitive に扱う。build_refinement_phase_state.pyが生成するISSUE_REFINEMENT_PHASE_STATE_V1は preflight phase ではhard_stop_eligible: false、post_rewrite_check / decide_next_action phase ではhard_stop_eligible: trueを設定する。LOOP_STATE_V1にはphaseフィールドを追加しない。 - AC4: fix: issue-refinement-loop の scope_signal_guard 停止を valid termination_cause に正規化する #919 回帰を維持する。post_rewrite_check / decide_next_action での
scope_signal_guard.triggered=trueかつexcluded_by_anchor_reframe=falseは引き続きTERMINATION_CAUSE: human_judgment_requiredを出し、reason_codeは BLOCKERS に保持する。test_decide_next_loop_action.pyの既存 scope_signal_guard テストに phase 条件を追加する形で更新し、破壊しない。 - AC5: 今回の失敗パターン(preflight pass →
scope_signal_guard.triggered: true→ 誤ったhuman_escalation)を fixture としてtest_refinement_phase_gate.pyに追加し、再発しないことを検証する。 - AC6:
references/loop-state.md/references/scope-signal-guard.md/references/termination-policy.mdの phase semantics を同期し、SKILL.md だけに新規ルールを閉じ込めない。各参照ドキュメントに phase-sensitive なscope_signal_guardの意味とhard_stop_eligibleの判定条件を追記する。
Verification Commands
# AC1
$ uv run pytest .claude/skills/issue-refinement-loop/tests/test_refinement_phase_gate.py -q
# AC2
$ uv run pytest .claude/skills/issue-refinement-loop/tests/test_decide_next_loop_action.py -q
# AC3
$ uv run pytest .claude/skills/issue-refinement-loop/tests/test_refinement_phase_gate.py -k "phase_state or build_phase_state" -q
# AC4
$ uv run pytest .claude/skills/issue-refinement-loop/tests/test_decide_next_loop_action.py -k "scope_signal_guard" -q
# AC5
$ uv run pytest .claude/skills/issue-refinement-loop/tests/test_refinement_phase_gate.py -k "preflight" -q
# AC6
$ rg -qn "hard_stop_eligible" .claude/skills/issue-refinement-loop/references/loop-state.md
$ rg -qn "hard_stop_eligible" .claude/skills/issue-refinement-loop/references/scope-signal-guard.md
$ rg -qn "hard_stop_eligible" .claude/skills/issue-refinement-loop/references/termination-policy.mdAllowed Paths
.claude/skills/issue-refinement-loop/scripts/build_refinement_phase_state.py.claude/skills/issue-refinement-loop/scripts/validate_refinement_phase_transition.py.claude/skills/issue-refinement-loop/scripts/decide_next_loop_action.py.claude/skills/issue-refinement-loop/SKILL.md.claude/skills/issue-refinement-loop/tests/test_refinement_phase_gate.py.claude/skills/issue-refinement-loop/references/loop-state.md.claude/skills/issue-refinement-loop/references/scope-signal-guard.md.claude/skills/issue-refinement-loop/references/termination-policy.md
Runtime Verification Applicability
decision: not_applicable
reason: "skill script / test 追加の範囲で完結し、interactive runtime 証跡を成功条件に含まない"Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
decide_next_loop_action.pyの既存 CLI インターフェースを破壊する変更が必要になった場合LOOP_STATE_V1スキーマへのphaseフィールド追加が必要になった場合(ISSUE_REFINEMENT_PHASE_STATE_V1の別契約で対処)ISSUE_REFINEMENT_PHASE_STATE_V1がLOOP_STATE_V1スキーマと直接統合しなければ機能しないと判明した場合(別契約として維持不可な場合)test_decide_next_loop_action.pyの既存テストを全面削除・再作成しなければ phase gate と両立できないと判明した場合(phase 条件追加ではなく全面破壊が必要な場合)references/*.mdへの追記が Allowed Paths 外のスクリプト(run_refinement_preflight.py、plan_refinement_loop.py等)への修正を必要とすると判明した場合
Required Skills
- Python
- issue-refinement-loop skill の理解
Delivery Rule
1 Issue = 1 PRで完了する。- Draft PR を既定とする。
Activity
squne121 commentedon Jun 19, 2026
判定
#1013 の問題意識は正しいです。ただし、このIssue本文のまま実装に入ると、phase gate を追加するはずが、既存の
LOOP_STATE/scope_signal_guard/ #919 回帰テスト / termination policy の不整合を増やす可能性が高いです。実装着手前に仕様を少し締めるべきです。確認できた範囲では、#1013 自体にコメントはありません。 また、Issue本文は
preflight pass → scope_signal_guard.triggered → immediate human_escalationを失敗パターンとして定義し、build_refinement_phase_state.py/validate_refinement_phase_transition.py/decide_next_loop_action.pyなどの追加・変更を要求しています。Blocker 1: Allowed Paths が足りない
#1013 は
SKILL.mdと script/test だけを Allowed Paths にしていますが、実際のSSOTはそこだけではありません。現行のloop-state.mdはscope_signal_guard.triggered == trueかつexcluded_by_anchor_reframe == falseならhuman_escalationと明記しています。 一方、scope-signal-guard.mdでは停止条件を「iteration 中に新規追加された場合」としており、#1013 の主張と整合します。つまり、#1013 の本質は script 修正ではなく、「scope signal はいつ hard stop になるのか」というSSOTの再定義です。最低でも以下を Allowed Paths に追加すべきです。
さらに
phaseをLOOP_STATE_V1に入れるなら、schemas/loop_state.schema.jsonも必要です。現在の schema はadditionalProperties: falseで、必須フィールドも固定されています。 JSON Schema ではadditionalProperties: falseは未知プロパティを拒否し、requiredは欠落プロパティを不正にします。これは今回の schema probing エラーが自然に出る構造です。([JSON Schema]1)Blocker 2:
phaseをLOOP_STATEに入れる設計は危険現行
LOOP_STATE_V1にはphaseがありません。 ここに安易にphaseを足すと schema 変更が必要になり、Allowed Paths 外になります。逆に schema を変更せずにphaseを渡すと、additionalProperties: falseにより reject されます。推奨は、
LOOP_STATE_V1を拡張せず、別契約として以下を作ることです。build_refinement_phase_state.pyはこれを生成し、decide_next_loop_action.pyは--phase-state-fileを先に読んで、phase gate を通過した場合だけLOOP_STATE_V1を validate する構造にするのが安全です。Blocker 3: 現行
decide_next_loop_action.pyは phase gate 前に落ちる現行 script は、入力JSONを読み込んだ後、すぐ
validate_loop_state()でloop_state.schema.jsonに照合します。 そのため、preflight直後の不完全 state を渡すと、phase gate 判定に到達する前にinconsistent_stateとして落ちます。さらに、現在の不正入力出力は単なる line-oriented text です。
これは #1013 AC2 の
ISSUE_REFINEMENT_ROUTER_ERROR_V1と一致しません。修正方針は次の順番が必要です。
[jsonschema](https://python-jsonschema.readthedocs.io/en/stable/validate/).validate()は不正時にValidationErrorを投げますが、複数エラーを一括で扱うならiter_errors()が使えます。(jsonschema) AC2 の「schema probing を止める」目的なら、missing_fieldsだけでなくunexpected_fields、schema_version_seen、diagnostic_codeまで一回で出すべきです。Blocker 4: #919 回帰テストと衝突する
#919 の回帰テストは、
scope_signal_guard.triggered=trueのときdecide_next_loop_action.pyがTERMINATION_CAUSE: human_judgment_requiredを出すことを検証しています。 また、reason_codeを blockers に残し、termination_cause に直接渡さないことも検証しています。一方で #1013 は「preflight直後の
scope_signal_guard.triggeredは human escalation ではない」と言っています。これは正しいですが、既存テストをそのままにすると矛盾します。必要な分割はこれです。
つまり #919 を壊すのではなく、#919 の期待値に phase 条件を追加するのが正解です。
Major: AC1 の “next action は investigation” が狭すぎる
#1013 AC1 は「preflight pass +
scope_signal_guard.triggered: trueでも next action はinvestigation」としています。これは危険です。現行SKILLでは Step 1 Investigation と Step 1b Web Research は条件付きで、planner のinvestigation_policy.required/web_research_policy.requiredに従います。したがって、AC1 はこう直すべきです。
investigation固定にすると、web research のみ必要なIssueや、調査不要で review に進めるIssueを壊します。Major: planner は “新規追加された scope” を判定できていない
現行 planner の
_detect_scope_signals()は、現在の Issue body だけを見てnew_unverifiable_ac/new_allowed_path_layer/new_in_scope_areaを検出しています。 その出力はREFINEMENT_LOOP_PLAN_V1.decisions.scope_signal_guardに入ります。しかし、#1013 の主張に必要なのは「現在の本文に scope signal があるか」ではなく、rewrite / review 後に新しく追加されたかです。差分情報なしでは判定不能です。
build_refinement_phase_state.pyには最低限これを持たせるべきです。hard_stop_eligibleはsource_phase in [post_rewrite_check, decide_next_action]かつsignal_origin in [rewrite_delta, review_delta]の場合だけ true にするのが妥当です。Major: exit code の意味を混ぜるべきではない
現行
decide_next_loop_action.pyは exit 2 をhuman_escalation、exit 3 をinconsistent_stateに割り当てています。 Python のsys.exit()は 0 が正常、非0が異常終了という扱いで、Unix系では 2 がコマンドライン構文エラーによく使われます。([Python documentation]3)phase gate で「この phase では router 呼び出し禁止」と返すのは、human escalation ではありません。推奨は以下です。
または既存互換を優先するなら
STATUS: inconsistent_stateのままにしつつ、NEXT_ACTION: rebuild_phase_stateを出す。ただし、NEXT_ACTION: human_escalationは出してはいけません。これが今回の根本原因です。仕様修正案
#1013 は以下のように修正すると実装可能性が上がります。
改訂 Acceptance Criteria 案
最終評価
このIssueは 実装価値あり です。ただし、現状のままでは “phase gate” という名前の薄いラッパーを足すだけになり、既存の
scope_signal_guardhard stop と schema validation の順序問題を解決しません。実装PRに進める前の最低修正は次の3点です。
phaseはLOOP_STATE_V1に混ぜず、別のISSUE_REFINEMENT_PHASE_STATE_V1にする。LOOP_STATEschema validation より前に置く。squne121 commentedon Jun 20, 2026
Refinement Loop: Approved
The issue has been approved and is ready for implementation.
approvedsqune121 commentedon Jun 20, 2026
CONTRACT_REVIEW_RESULT_V1
squne121 commentedon Jun 20, 2026
実装完了
PR #1030 (Draft) を作成しました。
実装内容
新規ファイル:
scripts/build_refinement_phase_state.py—ISSUE_REFINEMENT_PHASE_STATE_V1を生成する。phase ごとにallowed_routers/forbidden_routers/scope_signal_semantics.hard_stop_eligibleを定義scripts/validate_refinement_phase_transition.py— phase transition と router gate を検証するtests/test_refinement_phase_gate.py— AC1/AC2/AC3/AC5 の 18 テスト変更ファイル:
scripts/decide_next_loop_action.py—--phase-state-file引数を追加。phase gate を schema validation より先に評価し、禁止 phase ではISSUE_REFINEMENT_ROUTER_ERROR_V1を出力してNEXT_ACTION: rebuild_phase_stateを返す (exit 3)tests/test_decide_next_loop_action.py— AC4 (fix: issue-refinement-loop の scope_signal_guard 停止を valid termination_cause に正規化する #919 回帰) の phase 条件テスト 4 件追加(既存 26 件維持で 30 件に)references/loop-state.md— phase-sensitive scope_signal_guard semantics 表、phase gate 呼び出し手順references/scope-signal-guard.md—hard_stop_eligible判定条件表、preflight phase での誤 routing 防止ルールreferences/termination-policy.md— phase-sensitivehard_stop_eligible前提条件セクション追加SKILL.md— Step 0f と Step 2 に phase state 生成・phase gate 手順追記AC 検証結果
test_refinement_phase_gate.py18 件test_decide_next_loop_action.py30 件-k "phase_state or build_phase_state"6 件-k "scope_signal_guard"6 件-k "preflight"5 件rg -qn "hard_stop_eligible"3 docs設計の核心
decide_next_loop_action.pyはforbidden_routersに含まれる。scope_signal_guard.triggered: trueがあってもcontinue_investigationとして扱い、human_escalationを返さないhard_stop_eligible: true— 従来通り scope signal guard がhuman_escalationを引き起こす (fix: issue-refinement-loop の scope_signal_guard 停止を valid termination_cause に正規化する #919 回帰維持)LOOP_STATE_V1へのphaseフィールド追加なし(Stop Condition 回避)IMPLEMENT_RESULT_V1:
status: ok
pr_url: #1030
branch: worktree-issue-1013-issue-refinement-loop-phase-gate
issue_number: 1013
allowed_paths_compliance: true
vc_all_pass: true
stop_condition_triggered: false
fix(#1013): Priority 2b 条件に review_verdict == VERDICT_NEEDS_FIX を復元し …
squne121 commentedon Jun 20, 2026
Fix Delta: B1 修正完了
Blocker B1 を修正しました。
修正内容:
VC 結果:
commit: b951143
fix(#1013): B2/B3/B4/M1/M3 phase gate修正 — review forbidden / allowlis…
feat(#1013): issue-refinement-loop に phase gate を追加し preflight 直後の誤った…
squne121 commentedon Jun 20, 2026
post-merge-cleanup: 完了 (2026-06-20T02:15:00Z)
cleanup サマリ
issue-1013-*worktree-issue-1013-*fix(#1013-followup): loop-state.md 散文誤記修正 — review phase を hard_stop_…