Repository navigation
fix: fence 前後の地の文でも structured_output を抽出できるようにする - #2353
Conversation
`_structured_output_from_result_compat()` の fence 抽出が `result` テキスト 全体に対する厳格な `^...$` anchor 一致を要求していたため、実際の `--agent` 呼び出し応答(fenced JSON block の前後に地の文を伴う shape)で decode failure し `missing_structured_output` に fail-closed していた。 `result` 内の fenced code block を non-greedy に列挙し、各候補を `json.loads` した上で対象 JSON Schema に対して独立に厳密検証し、 schema-valid な候補が一意に定まる場合にのみ採用するよう修正した。候補が 0件、または schema-valid な候補が複数(曖昧)の場合は従来どおり `None` (missing_structured_output 扱い)を返す。fence が全く無い `result` テキストは従来どおり全体を単一候補として扱う(後方互換)。 新規リグレッションテスト `test_run_retrospective_structured_output_prose_fence.py` を追加し、 実測 shape 1/2(fence+trailing prose / leading+trailing prose)の recovery、schema 不適合 fence のネガティブケース、複数 schema-valid 候補の曖昧性拒否を検証する。既存 hermetic suite (test_run_retrospective.py, 135 tests)は無回帰で PASS。 Co-Authored-By: Claude Sonnet 5 <[email protected]>
Preview URLPreview removed (PR #2353 closed). |
PR Review Verdict (impl-review-loop, iteration 1)確認内容(独立再検証、live head
|
結論判定: 修正方向は正しく、compatibility recovery を残す判断にも実用上の根拠があります。Claude Code の現行ドキュメントは PR #2353 はその実害を局所的に救う小さな adapter fix であり、CI も全緑、既存 impl-review-loop も APPROVE しています。 ただし、新しい fence 列挙正規表現に実運用で起こり得る false negative が残っています。 マージ前にここだけ直す価値があります。 Findings
良い点
また、回収した candidate を元の JSON Schema に再度通す設計も正しいです。Claude の構造化出力は本来スキーマ準拠 JSON を得るための機能なので、抽出だけ緩め、意味上の acceptance criterion は schema validation に残すのが妥当です。([Claude Platform]3) CI は required な マージ前に求める変更私は P1 の1件だけを必須にします。
なお、#2353 が直っても agent-retrospective 全体の完走保証にはなりません。 最終判定は |
…2348) OWNER P1 REQUEST_CHANGES fix_delta on PR #2353 (#2353 (comment)). `_FENCED_JSON_BLOCK_RE` matched only ```json / bare ``` *opener* fences. A foreign-language fenced block (e.g. ```text) appearing before the real ```json block had its own *closing* fence misread by `finditer()` as a new bare *opening* fence, corrupting subsequent pairing so the real JSON block's closing fence -- and thus the real JSON candidate itself -- never appeared in the results. This reproduced `missing_structured_output` for the common "prose with a text/code example, then the final JSON answer" LLM response shape. Replace the opener-only regex with a delimiter-line scanner (`_FENCE_DELIMITER_RE`) that finds ALL fence delimiter lines in encounter order and pairs them sequentially (opener, closer), filtering to pairs whose opener info string is "" or "json" (case-insensitive). Non-JSON fences are still correctly paired and skipped, not misread as new fence boundaries. Add regression test test_prose_fence_foreign_language_fence_before_json_fence_recovered, confirmed to FAIL against the pre-fix code (malformed_output / missing_structured_output) and PASS after the fix. Incidentally satisfies two optional review suggestions for free: case-insensitive JSON info string, and closing-fence indent tolerance (up to 3 spaces, GFM-compliant). Distinct-value dedup for duplicate schema-valid candidates was explicitly NOT implemented, per review guidance to keep this fix_delta minimal. Co-Authored-By: Claude Sonnet 5 <[email protected]>
fix_delta: OWNER P1 fence-pairing blocker resolvedFix_delta for the P1 REQUEST_CHANGES blocker raised in 変更内容 (what changed)
根本原因 (root cause)旧実装は テスト結果 (test results)新規回帰テスト
フルテストファイル(既存4件+新規1件): 5 passed 追加検証 (project-wide required verification)
スコープメモ (scope notes)レビューコメントのOPTIONAL項目のうち以下2件は今回の修正で副次的に満たした:
以下のOPTIONAL項目は意図的に未実装:
Markdown ASTライブラリ・json-repair・新規CIハーネスの追加は行っていない(scope外)。 コミット / push
|
LOOP_VERDICT (impl-review-loop, iteration 2)verdict: APPROVE
reviewed_head_sha: "a45b2c196c593a342b0d5d234b29b2e9e25c1b16"
blockers: []
warnings:
- "PR本文(Safety Claim Matrix/検証コマンド結果)がfix_delta前の状態のまま(AC1/AC2を「4 passed」と記載、実際は5 passed)。AC3のPR本文evidenceもiteration-1 head時点のもの。ただしiteration-1→fix_delta間の差分はフェンス抽出アルゴリズムのみに限定されており、単一fenceケースの挙動は変更前後で同一であることをdiffトレースで確認済みのためblockerとしない。"確認内容(独立再検証、live head
|
Summary
_structured_output_from_result_compat()(run_retrospective.py)の fence 抽出がresultテキスト全体への厳格な^...$anchor 一致を要求していたため、fenced JSON block の前後に地の文を伴う実際の--agent応答で decode failure しreason_code=missing_structured_outputに不必要に fail-closed していた問題を解消する(Issue 実装: agent-retrospective の structured output compatibility recovery が fence 前後の地の文で decode failure する missing_structured_output を解消する #2348)。result内の fenced code block を non-greedy に列挙し、各候補を独立にjson.loads+ 対象 JSON Schema で厳密検証し、schema-valid な候補が一意に定まる場合にのみ採用する方式に変更した。schema validation は一切緩めていない(malformed / partial output は引き続き fail-closed)。test_run_retrospective_structured_output_prose_fence.pyを追加し、実測 shape 1/2(fence 直後に地の文 / fence 前後に地の文)の recovery、schema 不適合 fence のネガティブケース、複数 schema-valid 候補の曖昧性拒否(fail-closed)を検証する。Checks
pnpm typecheckpnpm lintpnpm testpnpm build.claude/skills/agent-retrospective/scripts/配下の Python ファイルのみを変更しており、TypeScript/JavaScript ファイルの変更はゼロ(git diff --statで確認済み)。上記 4 コマンドは frontend ビルドパイプライン向けであり本変更のスコープと無関係のため、ローカルでは実行していない。代わりに Issue 実装: agent-retrospective の structured output compatibility recovery が fence 前後の地の文で decode failure する missing_structured_output を解消する #2348 本文の## Verification Commands(AC1/AC2/AC3/AC4)を実行し、全て結果を確認した(下記「検証コマンド結果」参照)。Schema Change Applicability
not_schema_changeobserver_result_v1.schema.jsonを含む producer-consumer 境界の machine-readable contract 自体は変更していない。変更対象はrun_retrospective.py内部のresultテキストから既存スキーマへ照合する compat recovery の抽出ロジックのみで、schema の形状・フィールド・型定義は不変。Schema Consumer Inventory
N/A(
not_schema_changeのため)。Compatibility Decision(互換性判定)
N/AN/AN/AN/ASafety Claim Matrix
test_prose_fence_schema_invalid_fence_stays_missing_structured_outputPASS(uv run --locked pytest .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective_structured_output_prose_fence.py -q4 passed)test_prose_fence_multiple_schema_valid_candidates_ambiguous_rejectedPASS(同上ファイル)claudeCLI 経由でretrospective-runtime-observerを非空・実質的な分析 prompt で呼び出した際にreason_code=missing_structured_outputとならずstructured_outputが抽出される(AC3)verify_run_retrospective_live_cli.sh --select test_real_claude_cli_analytical_prompt_structured_output_shape→1 passed。sanitized bounded classification:adapter_status=ok adapter_reason_code=None child_exit_code=0test_run_retrospective.py)に回帰なし(AC4)uv run --locked pytest .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective.py→135 passedVisual Impact Declaration(視覚影響申告)
N/A(UI / DOM / CSS 変更なし)。
検証コマンド結果
uv run --locked pytest .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective_structured_output_prose_fence.py -q→ 修正前(fix をgit stashで一時退避し baseline 検証):2 failed, 2 passed(shape1/shape2 が期待どおり fail)。修正適用後:4 passed。.claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh --select test_real_claude_cli_analytical_prompt_structured_output_shape→PASS: live CLI verification succeeded(claudeCLI 2.1.245、claude auth statusログイン済み環境)。sanitized bounded classification:adapter_status=ok adapter_reason_code=None child_exit_code=0(missing_structured_outputは発生せず)。uv run --locked pytest .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective.py→135 passed(既存 hermetic suite に無回帰)。uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_model_routing.py→24 passed(project constitution の標準チェック)。Notes
git stashで一時退避し新規テストが実際に fail することを確認してから復元・commit した(TDD baseline-expect: fail の確認)。