Repository navigation
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: none
goal_ref: "check_issue_contract.py --json の caller が stdout のみを JSON parse し stderr は diagnostics として扱う契約を regression test で固定し、stdout が JSON parse 不能なときは fail-open ではなく fail-closed にする"
change_kind: codeParent Issue
none(PR #596 の follow-up 単独バグ修正)
Parent Goal Ref
- Goal: なし(単独 follow-up)
- Desired Destination: なし
Current Validated Scope
issue_contract_hygiene_autofix.pyのcheck_non_c4_c9_blockersで、json.loads(proc.stdout)がJSONDecodeErrorを出した場合の挙動を、現状の fail-open(return False, [])から fail-closed(return True, ["check_error"])へ変更する- 同関数に caller contract コメント(stdout = JSON only、stderr = diagnostics、stderr を stdout に merge しない)を追加する
test_issue_contract_hygiene_autofix.pyにsubprocess.runを monkeypatch する regression test 群(stream 分離 kwargs 検査・stderr 診断混入の正例・stdout 結合の fail-closed 負例)を追加するcheck_issue_contract.pyの--jsonモード docstring または argparse--jsonhelp に caller contract を明記する
Remaining Parent Gaps
なし
Outcome
check_issue_contract.py --json の主要 consumer(issue_contract_hygiene_autofix.py の check_non_c4_c9_blockers)が、
subprocess.run(..., capture_output=True, text=True)で stdout/stderr を分離してキャプチャし(stderr=subprocess.STDOUTを使わない)、- JSON parse 対象を stdout のみに限定し、stderr に診断出力が流れても parse が成功し、
- stdout が valid JSON として parse できない場合は fail-open(
return False, [])せず fail-closed(return True, ["check_error"])で non-C4/C9 blocker check を有効化したまま安全側へ倒す、
ことを monkeypatch ベースの regression test が保証している状態。
Background
PR #596(Issue #574)で check_issue_contract.py --json 単体の stdout purity を修正した。script 側は contextlib.redirect_stdout(sys.stderr) で最終 JSON 以外の標準出力を封じ、datetime.utcnow() の DeprecationWarning も除去した。
しかし、caller 側の契約(stdout のみを JSON parse し、stderr は diagnostics として扱う)は regression test で固定されていない。将来 consumer が stderr=subprocess.STDOUT 相当でストリームを結合した場合、将来追加された warning や diagnostics によって JSON parse error が再発する可能性がある。
さらに、現行 check_non_c4_c9_blockers は json.loads(proc.stdout) が JSONDecodeError を出した場合に return False, [](fail-open)しており、これは「契約違反を検出したら non-C4/C9 blocker の検出をスキップして autofix を続行しうる」挙動である。本 Issue では caller 契約の固定に合わせて、この parse 失敗を fail-closed(check_error)へ変更し、退行を regression test で固定する。
現在の check_non_c4_c9_blockers は capture_output=True(stdout/stderr 分離)を使っているが、この分離を保証する regression test がなく、将来 stderr=subprocess.STDOUT が追加されても検出できない。
In Scope
check_non_c4_c9_blockersのJSONDecodeError分岐を fail-open(return False, [])から fail-closed(return True, ["check_error"])へ変更する- 同関数に caller contract コメント(stdout のみを JSON parse、stderr は diagnostics、stderr を stdout に merge しない)を追加する
test_issue_contract_hygiene_autofix.pyにsubprocess.runを monkeypatch して以下を固定する regression test を追加する:- 関数名に
stream_separationを含むテスト:check_non_c4_c9_blockersがcapture_output=True, text=Trueを使いstderr=subprocess.STDOUTを使わないことを kwargs 検査で固定する - 関数名に
stderr_diagnostic_ignoredを含むテスト: stdout が valid JSON・stderr に診断 warning がある場合に stdout のみ parse して成功する(正例) - 関数名に
fail_closed_on_non_jsonを含むテスト: stdout が「診断テキスト + JSON」結合の場合に fail-open せずreturn True, ["check_error"]で fail-closed する(負例)
- 関数名に
check_issue_contract.pyの--jsonモード docstring または argparse--jsonhelp に caller contract を明記する("diagnostics" を含む文言で stdout=JSON only / stderr=diagnostics / stderr を stdout に merge しない、を記述)
Out of Scope
check_issue_contract.pyの判定ロジック変更- issue-refinement-loop / issue-contract-review 等のシェル呼び出し側の修正
- PR fix(#574): check_issue_contract.py --json の stdout を JSON のみに分離し DeprecationWarning を除去 #596 で修正済みの script-local stdout purity
Acceptance Criteria
- AC1:
test_issue_contract_hygiene_autofix.pyに、check_non_c4_c9_blockersがsubprocess.run(..., capture_output=True, text=True)を使いstderr=subprocess.STDOUTを使わないことをmonkeypatchで kwargs 検査する regression test(関数名にstream_separationを含む)が追加され、PASS する - AC2:
subprocess.runの戻り値がstdout=<valid JSON>,stderr=<診断 warning>の場合にcheck_non_c4_c9_blockersが stdout のみを JSON parse し stderr に影響されず成功する regression test(関数名にstderr_diagnostic_ignoredを含む)が追加され、PASS する - AC3:
subprocess.runの戻り値がstdout=<診断テキスト + JSON>の場合にcheck_non_c4_c9_blockersが fail-open せずreturn True, ["check_error"]で fail-closed する regression test(関数名にfail_closed_on_non_jsonを含む)が追加され、PASS する - AC4:
check_issue_contract.pyに caller contract(stdout = JSON only、stderr = diagnostics、caller は stderr を stdout に merge しない)が module docstring または--jsonargparse help に "diagnostics" を含む文言で明記されている - AC5:
pnpm typecheckが PASS する - AC6:
pnpm lintが PASS する - AC7:
pnpm testが PASS する - AC8:
pnpm buildが PASS する
Verification Commands
# AC1
$ rg -n "stream_separation" .claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py
# AC2
$ rg -n "stderr_diagnostic_ignored" .claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py
# AC3
$ rg -n "fail_closed_on_non_json" .claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py
# AC4
$ rg -n "diagnostics" .claude/skills/review-issue/scripts/check_issue_contract.py
# AC5
# preflight-scope: runtime_only
$ pnpm typecheck
# AC6
# preflight-scope: runtime_only
$ pnpm lint
# AC7
# preflight-scope: runtime_only
$ pnpm test
# AC8
# preflight-scope: runtime_only
$ pnpm buildAllowed Paths
.claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py.claude/skills/edit-issue/scripts/issue_contract_hygiene_autofix.py.claude/skills/review-issue/scripts/check_issue_contract.py
Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
Required Skills
なし
Scope Delta(任意)
PR #596 の follow-up として起票後、anchor comment(#598 の human review, 2026-06-06)の NEEDS FIX 判定を反映して In Scope / Outcome / AC を更新した。主な変更は (1) JSONDecodeError 分岐の fail-open → fail-closed 化を In Scope に追加、(2) 弱い grep AC を subprocess.run monkeypatch ベースの kwargs 検査 + 正例/負例 regression test に差し替え、(3) caller contract の明記要件を docstring または argparse help に拡張、の 3 点。Allowed Paths は変更なし。
Runtime Verification Applicability
本 Issue は Python スクリプトの挙動変更(fail-closed 化)とテスト追加・docstring 追加のみです。ゲームの実行時動作には影響しません。
decision: not_applicable
reason: "Python script consumer behavior change (fail-open to fail-closed) plus regression test and docstring addition only. No runtime game behavior changed."Delivery Rule
1 Issue = 1 PR、worktree 作業、Draft PR 既定。
Activity
squne121 commentedon Jun 6, 2026
判定: NEEDS FIX
#598 の方向性は妥当です。ただし、このまま実装すると「stdout/stderr 分離契約を固定したつもり」になり、肝心の退行条件を殺しきれません。特に、現在の consumer 側は
json.loads(proc.stdout)に失敗した場合に fail-open でreturn False, []しており、これは「非 C4/C9 blocker の検出をスキップして autofix を続行しうる」挙動です。ここはブロッカーです。([GitHub]1)確認した関連関係
#598 は #596 の follow-up 単独バグ修正として作られており、目的は
check_issue_contract.py --jsonの caller が stdout のみを JSON parse し、stderr を diagnostics として扱う契約を regression test で固定することです。Scope にはissue_contract_hygiene_autofix.pyの caller contract コメント追加、test_issue_contract_hygiene_autofix.pyへの stderr 混入 regression test 追加、check_issue_contract.pyの--jsondocstring 明記が入っています。([GitHub]2)#574 は前段の問題で、
check_issue_contract.py --json実行時にdatetime.utcnow()のDeprecationWarningや diagnostics が stdout/JSON parse を壊す可能性を扱っていました。#574 自体も「Python warnings は通常 stderr」「caller が stdout/stderr を結合している可能性」を明示しています。([GitHub]3)#596 は #574 対応 PR で、
datetime.datetime.utcnow()をdatetime.datetime.now(datetime.timezone.utc)に置換し、--json処理中の incidental stdout をcontextlib.redirect_stdout(sys.stderr)で stderr に逃がし、stdout に最終 JSON だけを出す変更を入れています。([GitHub]4)#598 自体は Open で、GitHub 上の Development 欄には現時点で branch / PR は紐づいていません。([GitHub]2)
技術調査からの前提
Python の
subprocess.run(capture_output=True)は内部的に stdout/stderr をそれぞれPIPEにし、分離して捕捉します。逆に stdout/stderr を結合したい場合はstdout=PIPE, stderr=STDOUTを使う、というのが公式仕様です。したがって、この Issue で固定すべき契約は「capture_output=Trueまたはstdout=PIPE, stderr=PIPEで分離し、JSON parse 対象は stdout のみ」です。([Python documentation]5)Python warnings は通常
sys.stderrに書かれます。つまり stderr に warning が出ること自体は正常であり、consumer が stderr を stdout に混ぜて JSON parse するのが壊れ方の本質です。([Python documentation]6)contextlib.redirect_stdoutは utility script では有用ですが、sys.stdoutへの global side effect があるため library code や threaded application には不向きで、subprocess の出力には効きません。#596 の script-local 対応は utility script としては許容できますが、caller 側では subprocess の stream 分離を別途固定する必要があります。([Python documentation]7)ブロッカー
B1. JSON parse failure が fail-open のまま残っている
現行
issue_contract_hygiene_autofix.pyはproc.stdoutをjson.loadsし、JSONDecodeErrorなら「non-JSON output; cannot parse — skip guard」としてreturn False, []します。これは「契約違反を検出したら安全側に倒す」ではなく、「契約違反時に blocker check を無効化する」です。([GitHub]1)#598 の主題は「caller 契約の固定」です。ならば、stdout が JSON として parse できない場合は
return True, ["check_error"]相当で fail-closed にすべきです。最低でも regression test で以下を固定してください。B2. AC2 が
rg "capture_output"だけなのは弱い#598 の AC2 は
capture_output=Trueの grep 一致です。これはコメントや無関係箇所でも通りますし、将来stderr=subprocess.STDOUTが追加されても検出できません。現行実装はcapture_output=Trueで subprocess を呼び、stdout のみを parse していますが、これを grep ではなくテストで固定すべきです。([GitHub]1)推奨テストは、
subprocess.runを monkeypatch して kwargs を検査する形です。pytest のmonkeypatch.setattrはテスト中だけ関数・属性を差し替え、テスト終了後に戻す用途として公式に想定されています。([pytest]8)B3. 「stderr に疑似 warning が流れても stdout parse 成功」だけでは、本件の退行を十分に再現していない
stderr が別 stream にある限り、stdout JSON parse が成功するのは当然です。Python 公式でも
capture_output=Trueは stdout/stderr を別々に捕捉します。([Python documentation]5)本当に殺すべき退行は、将来誰かが
stderr=subprocess.STDOUT相当で結合した場合です。したがって、テストは少なくとも次の 2 本が必要です。stdout='{"blocking_issues":[]}',stderr='DeprecationWarning...'なら parse 成功。stdout='DeprecationWarning...\n{"blocking_issues":[]}',stderr=Noneなら fail-closed。これを入れないと、#574 で特定した壊れ方を #598 が固定できません。
ノンブロッカー
N1.
check_issue_contract.pyの docstring だけでなく CLI help にも契約を出した方がよい#598 は
--jsonモード docstring に caller contract を明記するとしています。これは最低限として妥当ですが、caller 実装者が見るのは module docstring より--helpや周辺コメントの可能性が高いです。argparseの--jsonhelp にも次の趣旨を入れるとよいです。N2.
contextlib.redirect_stdoutへの過信は避ける#596 の
redirect_stdout(sys.stderr)は utility script では許容できますが、公式 docs の通り global stdout を差し替えるため library / threaded 用途には不向きです。([Python documentation]7)#598 側の説明では「script 側で stdout purity を頑張る」ではなく、「caller は stream contract を守る」と明確に分離してください。
N3. AC4〜AC7 は過剰気味
#598 の変更対象は
.claude/skills/**の Python script / test / docstring であり、ゲーム runtime には影響しません。Issue 本文も runtime verification は not_applicable としています。([GitHub]2)pnpm typecheck/lint/test/buildを残すのはプロジェクト運用上はよいですが、実質的な品質ゲートはuv run pytest .claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py -vと、必要なら review-issue 側の pytest です。起票文への修正提案
#598 はこのまま実装に回すより、AC を次のように差し替えるのが安全です。
結論
#598 は必要です。ただし、現状の AC だと「現在たまたま分離している」ことを弱く確認するだけで、実際の退行条件である
stderr=STDOUT/ JSONDecodeError / fail-open を固定できません。実装に回す前に、JSONDecodeError を fail-closed に変更すること、grep ではなく monkeypatch による kwargs 検査を入れること、stderr 分離の正例と stderr 結合の負例を両方 regression test 化することをブロッカーとして要求するのが妥当です。
squne121 commentedon Jun 6, 2026
Refinement Loop: Approved
The issue has been approved and is ready for implementation.
approvedsqune121 commentedon Jun 6, 2026
squne121 commentedon Jun 6, 2026
Contract Snapshot (2026-06-06T23:48:05Z)
check_issue_contract.py --jsonの consumer (check_non_c4_c9_blockers) が stdout/stderr を分離キャプチャし、stdout のみ JSON parse、parse 不能時は fail-closed (return True, ["check_error"]) になることを regression test で固定した状態。.claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py,.claude/skills/edit-issue/scripts/issue_contract_hygiene_autofix.py,.claude/skills/review-issue/scripts/check_issue_contract.py.claude/worktrees/issue-598-check-issue-contract-py-json-consumer-stworktree-issue-598-check-issue-contract-py-json-consumer-stsqune121 commentedon Jun 6, 2026
implement-issue: 実装完了 (2026-06-06T23:54:42Z)
.claude/worktrees/issue-598-check-issue-contract-py-json-consumer-stworktree-issue-598-check-issue-contract-py-json-consumer-sttest(#598): strengthen consumer contract test oracles
fix: check_issue_contract.py --json consumer の stdout/stderr 契約を regr…