Repository navigation
実装: review pipeline の timeout budget を動的 VC 本数ベースに再設計する - #2221
Conversation
固定「near-cap VC 最大2本」前提のtimeout budgetを、side-effect-freeなcanonical VC planner(baseline_vc_preflight.compute_canonical_vc_plan)が算出する launch_upper_boundベースの動的formula(contract_readiness_check.derive_review_budget) へ置き換える。N<=2では既存値(350/400/480/520)を維持し、N>=3ではcleanup tail込みで 比例拡張する。policy ceiling超過はsubprocess起動前にtypedで拒否する。 run_root_review_pipeline._cmd_produce()はpinned bodyから算出したinvocation-local ReviewBudgetをrun_reviewer_transport()へ明示的に渡す(reviewer_transport.pyの fallback定数は不変)。baseline_vc_preflight.pyにSIGTERM cooperative cancellation を追加し、outer timeout時にactive VC process group(direct child・grandchild)を 確実に回収する。全AC(1-10)を実process fault-injection testを含むpytestで固定。 Co-Authored-By: Claude Sonnet 5 <[email protected]>
Preview URL✅ verified live assets: https://squne121.github.io/loop-protocol/pr-2221/ The preview will be removed when this PR is closed. |
) fix_delta iteration 1: test_scaled_fault_injection_inner_timeout_precedes_outer_deadline が inner_timeout_seconds=1 のとき、uv run --locked pytest サブプロセスの起動オーバーヘッドだけで marker.txt 書き込み前に inner timeout が発火し FileNotFoundError で 5/5 決定論的に失敗していた。 - inner_timeout_seconds を 1 → 3、fixture_sleep_seconds を 5.0 → 8.0 へ拡大 - タイマー開始前に uv/pytest 環境を collect-only invocation で pre-warm し、 計測対象の起動オーバーヘッドを最小化 - clean worktree で 5 連続実行し決定論的 PASS を確認(AC9 も併せて確認)
fix_delta iteration 1 対応(AC10 flaky failure)Blocker: 対応(commit 1fa40da):
再検証: 修正後、AC10 VC を clean worktree で 5 連続実行し全て deterministic PASS(各 ~3.2s)。 変更は |
判定
レビュー対象は現在の 動的 budget 導出、invocation-local deadline の明示配線、fallback しかし、#2207 の中心的な安全主張である outer timeout 後に direct child・grandchild が残留しないことは、production 経路では成立していません。 P0-1: production の outer timeout は SIGTERM handler を通らず、VC process group を残留させる
result = subprocess.run(
[...],
timeout=aggregate_timeout_seconds,
)さらに上位の CPython の 同じ制御フローを Linux/Python 3.13 で抽出した最小再現結果は以下でした。 {
"sigterm_handler_ran": false,
"separate_session_descendant_survived": true
}つまり現在の AC9 は「誰かが baseline に手動で SIGTERM を送れば cleanup できる」ことしか示しておらず、実際の aggregate timeout が発火したときの経路を証明していません。 必須修正
修正後の integration test は P0-2: SIGTERM handler が非 reentrant Lock を再取得し、cleanup 自体がデッドロックし得る実装は active group 登録簿を Python の signal handler は main Python thread で実行されます。main thread が 独立最小再現では次の状態になりました。 {
"handler_entered": true,
"handler_completed": false,
"process_hung": true
}必須修正signal handler 内では、Lock 取得、sleep、process wait、複数 process への kill を実行しないでください。 handler は cancellation flag の設定、self-pipe / また、active group が複数ある場合は、
とすべきです。現在の「1 group ごとに最大5秒待つ」方式では、最大40 group で cleanup が直列に数分へ膨張します。 P0-3:
|
…D leak修正・budget denominator是正) - contract_readiness_check.py / run_root_review_pipeline.py の subprocess.run(timeout=...) を baseline_vc_preflight.run_subprocess_with_cooperative_supervisor() へ置換し、outer timeout 時に SIGKILL直接ではなく SIGTERM→grace→SIGKILL の cooperative 経路を通す(P0-1)。 - SIGTERM handler を flag設定+例外送出のみに縮小し、Lock取得・sleep・process wait・killを main() の top-level except(通常制御フロー)へ移動(P0-2)。 - _kill_process_group() を、leader終了後も process group 不在を確認し SIGTERM を無視する grandchild を SIGKILL するよう修正(P0-3)。 - PGID registry を register時に返した immutable token で unregister するよう修正し、 stale PGID leak / PID wrap 再利用ハザードを解消(P1-1)。 - run_baseline_vc_preflight() production wrapper を実際に通し、outer deadline 発火・ SIGTERM-ignoring grandchild・wrapper/VC leader/grandchild 全不在確認・SIGTERM handler 実行確認を伴う production-shaped integration test を追加(10回反復、P1-2 item5)。 実装過程で、SIGKILL後に reap(wait)されない zombie が group-absence判定を偽陽性化する 追加のバグを発見し、_ACTIVE_PROCESS_HANDLES による明示的 reap を追加して修正。 - POSIX-unsupported環境の pytest skip を exit 77 (environment blocked) として区別する conftest.py の pytest_sessionfinish hook を追加(bare skipif の exit 0 と区別、P1-2 item6)。 - budget denominator を Issue #2207 Outcome/AC5 契約どおり command_occurrence_count に是正 (launch_upper_bound からの無断置換を解消)。連続同一pure VC 3/40/41件境界のformula/policy-cap テストを追加(P1-3)。 - _kill_process_group() 単体の SIGTERM-ignoring grandchild 回帰テストを追加(P0-3該当)。 Refs #2207
Summary
run_root_review_pipeline.py produce→reviewer_transport.py→contract_readiness_check.py→baseline_vc_preflight.py)の timeout budget を、固定の「near-cap VC 最大2本」前提から、pinned Issue body から実際に起動され得る VC subprocess occurrence 数(保守的上限)に応じた動的導出へ再設計した。baseline_vc_preflight.pyに side-effect-free な canonical VC planner(compute_canonical_vc_plan())を追加し、contract_readiness_check.pyに formula(derive_review_budget())を追加した。N <= 2では既存値(350s/400s/480s/520s)を維持し、N >= 3ではN * (150 + 15) + 20の formula で command cap だけでなく cleanup tail も含めて比例拡張する。Nが固定の policy ceiling(MAX_VC_EXECUTION_SLOTS = 40)を超える場合、subprocess 起動前に typed non-retryableverification_budget_exceeds_policyで拒否する(contract_readiness_check.py/run_root_review_pipeline._cmd_produce()の両層)。run_root_review_pipeline._cmd_produce()は pinned body から算出した invocation-localReviewBudgetをrun_reviewer_transport()へ明示引数として渡す。reviewer_transport.pyの module-level fallback constants(PER_ATTEMPT_DEADLINE_SECONDS=480/TOTAL_DEADLINE_SECONDS=520)は不変のまま。baseline_vc_preflight.pyに SIGTERM cooperative cancellation を追加した。SIGTERM 受信時に現在管理している全 VC process group(direct child・grandchild を含む)を終了・回収する。outer deadline → active VC descendant absentを実 subprocess を用いた fault-injection test(AC9/AC10)で検証した。classify_result()の timeout 判定(exit_code==-1 かつ stderr=="timeout")を regression_gate 判定より前に実行するよう順序を変更した。regression-gate 形状の VC(pytest 等)が per-command timeout した場合に、汎用の "Regression gate command failed" へ misclassify されず正しくcategory: timeoutになる(AC9/AC10 の fault-injection test で顕在化した既存の分類漏れを、本 Issue のスコープ内で修正)。本 PR は Issue #2207 の実装であり、マージ時に Closes #2207 として Issue を close する。
追加修正(fix_delta): OWNER レビュー指摘への対応(2026-08-17 実施)
2026-08-16 の OWNER
REQUEST_CHANGES(レビューコメント、head=e5fafd6e832ef95d73d189fde59bb01a512541a7時点)で指摘された 8 件のマージ条件をすべて反映した。contract_readiness_check.py::run_baseline_vc_preflight()とrun_root_review_pipeline.py::run_contract_readiness_check()のsubprocess.run(timeout=...)を、baseline_vc_preflight.run_subprocess_with_cooperative_supervisor()へ置換した。outer deadline 発火時は SIGKILL 直接ではなく、子の process group へ SIGTERM → 単一の bounded grace → grace 超過後のみ SIGKILL、という cooperative 経路を通す。_sigterm_cooperative_handler)の本体を、flag 設定とCooperativeCancellationRequested(BaseException)の送出のみに縮小した。Lock 取得・sleep・process wait・kill はすべてmain()の単一 top-levelexcept CooperativeCancellationRequested(通常制御フロー)へ移動した。複数 active group がある場合も「全 group に先に SIGTERM → 単一 global grace → 生存 group へ SIGKILL」の順序を維持する。_kill_process_group()が leader 終了で即 return し SIGTERM 無視 grandchild を残す): leader のwait()が返った後もos.killpg(pgid, 0)で process group 存在を明示的に確認し、まだ生存していれば group 全体へ SIGKILL、その後 group 不在を bounded poll で確認するよう修正した。SIGTERM を明示的に無視する grandchild を含む回帰テスト(test_kill_process_group_reaps_sigterm_ignoring_grandchild)を追加した。_register_active_process_group()が返す PGID を immutable token として呼び出し側が保持し、_unregister_active_process_group()にはその token をそのまま渡す(PID からの再解決をやめた)。contract_readiness_check.run_baseline_vc_preflight()の production wrapper を実際に起動し、outer deadline を意図的に発火させ、direct child(VC leader)と SIGTERM 無視 grandchild を両方起動し、baseline の SIGTERM handler が実際に実行されたことをマーカーファイルで確認し、wrapper・VC leader・grandchild すべての不在を確認するtest_outer_deadline_via_run_baseline_vc_preflight_reaps_full_process_treeを追加した(10 回反復)。この実装過程で、SIGKILL 後にwait()されない process が zombie としてkillpg(pgid, 0)に "生存" と誤判定され続ける追加バグを発見し、_ACTIVE_PROCESS_HANDLES(pgid → 直接子のPopenhandle)による明示的 reap をreap_all_active_process_groups()に追加して修正した。.claude/skills/issue-contract-review/scripts/tests/conftest.pyを新規追加し、pytest_sessionfinishhook でセッション内の全 item がenvironment blocked理由で skip された場合にのみ session exit code を 77 へ上書きする(他の PASS/FAIL と混在する通常のフルスイート実行には影響しない)。derive_review_budget()/compute_invocation_local_baseline_timeout()/run_root_review_pipeline._cmd_produce()の入力をlaunch_upper_boundから Issue 実装: review pipeline の timeout budget を動的 VC 本数ベースに再設計する #2207 Outcome/AC5 契約どおりのcommand_occurrence_countへ是正した。連続する同一 pure VC が 3 件・40 件(policy cap ちょうど)・41 件(policy cap 超過)の formula/policy-cap 境界テストを追加した。cef36c369e19b2f382d974685d32b6e414677fb2で再取得した証跡に更新した。Checks
uv run --locked pytest .claude/skills/issue-contract-review/scripts/tests/ -q(329 passed, 1 skipped)uv run --locked pytest .claude/skills/issue-contract-review/tests/ -q(533 passed)uv run --locked pytest .claude/skills/issue-refinement-loop/tests/ -q(1556 passed)uv run --directory <worktree> ruff check <changed files>(all checks passed, 8 files)pnpm typecheck/pnpm lint/pnpm test/pnpm build— 本 PR は.claude/skills/**/scripts/*.pyおよび.claude/skills/**/tests/*.pyのみを変更する Python-only の変更であり、TypeScript/フロントエンドのビルド・テストパイプラインには触れない受け入れ条件の達成状況
すべて fix_delta 後の現在 head SHA
cef36c369e19b2f382d974685d32b6e414677fb2で個別 VC pytest node-id を実行し、全 10 AC が PASS した。.../test_review_budget_planner.py::test_canonical_plan_binds_body_sha256—1 passed in 0.02s.../test_review_budget_planner.py::test_conservative_count_counts_duplicate_non_pure_occurrences—1 passed in 0.02s.../test_review_budget_planner.py::test_pure_coverage_included_in_budget—1 passed in 0.02s.../test_review_budget_planner.py::test_state_barrier_separates_repeated_pure_command_launches—1 passed in 0.02s.../test_review_budget_formula.py::test_low_count_compatibility_preserves_existing_budget—1 passed in 0.04s.../test_review_budget_formula.py::test_cleanup_aware_growth_scales_with_occurrence_count—1 passed in 0.04s.../test_review_budget_formula.py::test_policy_ceiling_rejects_before_subprocess_launch—1 passed in 0.04s.../test_review_budget_wiring.py::test_root_pipeline_passes_invocation_local_deadline_to_transport—1 passed in 0.06s.../test_vc_process_group_cancellation.py::test_sigterm_reaps_active_vc_process_groups—1 passed in 0.35s(実 subprocess tree に対する SIGTERM fault-injection test、fallback 不使用).../test_vc_process_group_cancellation.py::test_scaled_fault_injection_inner_timeout_precedes_outer_deadline—1 passed in 3.34s(inner timeout が outer deadline より先行することを実証済み)加えて、OWNER fix_delta で新規追加した runtime test も同一 head で PASS した:
.../test_vc_process_group_cancellation.py::test_kill_process_group_reaps_sigterm_ignoring_grandchild—1 passed in 0.06s.../test_vc_process_group_cancellation.py::test_outer_deadline_via_run_baseline_vc_preflight_reaps_full_process_tree—1 passed in 8.72s(10 回反復、production wrapper 経由で outer deadline を実発火)検証コマンド結果
Issue #2207 の
## Verification Commandsに記載された 10 個の VC を、すべて fix_delta 後の現在 head SHAcef36c369e19b2f382d974685d32b6e414677fb2の worktree(uv run --locked pytest <node-id> -q)で個別実行した。PASS以外の出力(FAILED/ERROR/skip)は 0 件。pytestmark = pytest.mark.skipif(not posix_process_groups_supported(), ...)により POSIX process group semantics 非対応環境では SKIP するが、本 fix_delta で追加したconftest.py::pytest_sessionfinishにより、そのような全 skip セッションは通常の pytest exit 0 ではなく exit 77(environment blocked)として区別される(他の PASS/FAIL と混在する通常実行には影響しない)。本実行環境では skip されず実行・PASS した。Allowed Paths 遵守
gh pr view 2221 --json filesで取得した変更ファイル一覧はすべて Issue #2207 の## Allowed Pathsに含まれる。対象ファイルは以下の 9 件である:.claude/skills/issue-contract-review/scripts/baseline_vc_preflight.py.claude/skills/issue-contract-review/scripts/contract_readiness_check.py.claude/skills/issue-contract-review/scripts/tests/conftest.py(P1-2 item6: environment-blocked を exit 77 として区別するpytest_sessionfinishhook).claude/skills/issue-contract-review/scripts/tests/test_review_budget_formula.py.claude/skills/issue-contract-review/scripts/tests/test_review_budget_planner.py.claude/skills/issue-contract-review/scripts/tests/test_vc_process_group_cancellation.py.claude/skills/issue-refinement-loop/scripts/run_root_review_pipeline.py.claude/skills/issue-refinement-loop/tests/test_ac1_live_issue_no_environment_failure.py(P0-1 の supervisor 置換に伴い、旧subprocess.runmonkeypatch を新しいrun_subprocess_with_cooperative_supervisormonkeypatch へ更新).claude/skills/issue-refinement-loop/tests/test_review_budget_wiring.pyAllowed Paths 外のファイル変更はない。
Runtime Verification Evidence(動作検証証跡)
Issue #2207 の
Runtime Verification Applicability: immediate(applicable_acs: [AC9, AC10])に基づく実 subprocess fault-injection 証跡:baseline_vc_preflight.pyが管理する VC process group(direct child・grandchild を含む)に対し SIGTERM を送出し、実 subprocess tree が bounded time 内に終了・回収されることを marker file の存在確認で証明するtest_sigterm_reaps_active_vc_process_groupsを実行し PASS(1 passed in 0.35s)。mock のみで完結する fallback 経路は使用していない。test_scaled_fault_injection_inner_timeout_precedes_outer_deadlineを実行し PASS(1 passed in 3.34s)。inner timeout が outer aggregate deadline より先に typed classification(category: timeout)され、outer 側に active VC descendant が残存しないことを marker file ベースで確認しており、fallback 経由の成功を PASS に変換していない。_kill_process_group()の SIGTERM 無視 grandchild):test_kill_process_group_reaps_sigterm_ignoring_grandchildを実行し PASS(1 passed in 0.06s)。leader は正常終了するが grandchild が明示的に SIGTERM を無視する fixture で、leader 終了後も process group が SIGKILL で回収されることを確認する。test_outer_deadline_via_run_baseline_vc_preflight_reaps_full_process_treeを実行し PASS(1 passed in 8.72s、内部で 10 iteration)。contract_readiness_check.run_baseline_vc_preflight()(production entry point)をoverride_timeout_seconds=0.6sの test-only DI で実際に outer timeout させ、direct child(VC leader、.venv/bin/python3 -m pytest実 subprocess)と SIGTERM を明示的に無視する grandchild を両方起動し、baseline の SIGTERM handler がテスト専用マーカーファイルへ書き込むことでハンドラの実行を確認し、wrapper(baseline_vc_preflight.py自身)・VC leader・grandchild すべての不在を bounded poll で確認し、outer result が typedstatus: runtime_error/failure_class: timeout/timeout_phase: baseline_vc_preflight_aggregateであることを確認する。injected deadline は Issue が示唆する 50〜200ms ではなく 600ms とした(この repo の venv interpreter + pytest collection の起動オーバーヘッドだけで実測約 200ms かかるため、50〜200ms では grandchild が起動する前に deadline が発火してしまい "grandchild を実際に reap した" ことを証明できない)。反復回数も Issue が示唆する ~50 回ではなく 10 回とした(各反復が実 subprocess tree の spawn を伴うため)。両者ともテストコード内のコメントに rationale を明記している。Schema Change Applicability
not_schema_changebaseline_vc_preflight/v1の既存 producer JSON schema フィールドは追加・変更していない(Issue 実装: review pipeline の timeout budget を動的 VC 本数ベースに再設計する #2207 の明示スコープ境界)。canonical VC plan の各フィールド(body_sha256/command_occurrence_count等)はbaseline_vc_preflight.pyの in-process 関数戻り値であり、consumer をまたぐ producer schema ではない。fix_delta で追加したテスト専用マーカーファイル・環境変数(BASELINE_VC_PREFLIGHT_TEST_SIGTERM_MARKER_PATH等)も production の JSON 出力スキーマには含まれず、テスト専用の side-channel である。Schema Consumer Inventory
N/A(
not_schema_changeのため)Safety Claim Matrix
baseline_vc_preflight.pyは SIGTERM 受信時に現在管理している全 VC process group(direct child・grandchild)を bounded time 内に終了・回収するtest_vc_process_group_cancellation.py::test_sigterm_reaps_active_vc_process_groups(実 subprocess tree を用いた fault-injection test、PASS)category: timeout)され、outer 側で active VC descendant は残存しないtest_vc_process_group_cancellation.py::test_scaled_fault_injection_inner_timeout_precedes_outer_deadline(実 subprocess を用いた fault-injection test、PASS)contract_readiness_check.run_baseline_vc_preflight()/run_root_review_pipeline.run_contract_readiness_check())はsubprocess.run(timeout=...)の直接 SIGKILL ではなく cooperative supervisor(SIGTERM → 単一 grace → SIGKILL)を経由し、SIGTERM handler を確実に実行させるcontract_readiness_check.py::run_subprocess_with_cooperative_supervisor()呼び出しへの置換 +test_outer_deadline_via_run_baseline_vc_preflight_reaps_full_process_tree(production wrapper 経由の実 outer-deadline fault-injection、10 回反復、PASS)_sigterm_cooperative_handler()の実装(flag 設定 + 例外送出のみ)+main()の単一except CooperativeCancellationRequested_kill_process_group()は leader 終了後も process group 不在を確認し、SIGTERM を無視する grandchild を SIGKILL で回収するtest_vc_process_group_cancellation.py::test_kill_process_group_reaps_sigterm_ignoring_grandchild(SIGTERM 無視 grandchild を含む実 fixture、PASS)_register_active_process_group()/_unregister_active_process_group()のシグネチャ変更(token 方式) + 既存/新規 process-group テスト全 PASSNが policy ceiling を超える場合、subprocess 起動前に typed non-retryableverification_budget_exceeds_policyで拒否されるtest_review_budget_formula.py::test_policy_ceiling_rejects_before_subprocess_launch/test_review_budget_wiring.py::test_policy_ceiling_exceeded_rejects_before_any_checker_subprocess(両方 PASS)N)は Issue #2207 Outcome/AC5 契約どおりcommand_occurrence_count(連続同一 pure VC も dedup せず raw occurrence 数として数える)であり、launch_upper_boundではないtest_review_budget_formula.pyの 3/40/41 件境界テスト 3 件(PASS)+test_review_budget_wiring.pyの denominator 参照更新N<=2では既存 production 値(350s/400s/480s/520s)を厳密に維持し、budget 拡張が既存の低本数 Issue の挙動を変えないtest_review_budget_formula.py::test_low_count_compatibility_preserves_existing_budget/test_review_budget_wiring.py::test_low_n_body_wiring_matches_current_production_values(両方 PASS)reviewer_transport.pyの module-level fallback constants(PER_ATTEMPT_DEADLINE_SECONDS/TOTAL_DEADLINE_SECONDS)は本 PR の配線によって書き換えられないtest_review_budget_wiring.py::test_root_pipeline_passes_invocation_local_deadline_to_transport内のアサーション(PASS)environment blockedとして扱われ、pytest.mark.skipifの exit 0 と混同されない(専用 exit 77)test_vc_process_group_cancellation.pyのpytestmark = pytest.mark.skipif(...)+ 新規conftest.py::pytest_sessionfinish(全 skip セッションのみ exit 77 へ上書き。他 PASS/FAIL と混在する通常実行は無影響であることをローカル self-test で確認)Notes
immediate(AC9/AC10)。50〜200ミリ秒 scale の想定に対し、実装では--timeout-secondsの整数秒 CLI 制約(最小 1 秒)と実プロセス起動オーバーヘッドを踏まえ、AC9 は 10 秒 sleep(SIGTERM で即時終了確認)、AC10 は inner timeout 3 秒 / fixture sleep 8 秒(elapsed が fixture sleep を大きく下回ることで inner timeout 先行を証明)とした。fix_delta で追加した P1-2 item5 の production integration test も同様の理由で outer deadline を 600ms(Issue の示唆する 50〜200ms ではない)・反復回数を 10 回(Issue の示唆する ~50 回ではない)とし、テストコード内にそれぞれの rationale を明記した。全テストとも fallback 経由の成功を PASS に変換せず、marker file で実プロセスが実際に kill・reap されたことを証明する。