Repository navigation
fix: run_retrospective.py に identity-binding helper を追加し caller-supplied prompt の observer_run_id_mismatch を解消 - #2358
Conversation
…ied prompt の observer_run_id_mismatch を解消 (#2350) prepare() 完了後の ctx/plan から run_id/base_sha/source_set_digest/observer_id を 取得し、default prompt 経路と caller-supplied prompt(--prompts-file)経路の両方を 同一の bind_observer_prompt() へ結合する。caller-supplied prompt が identity 情報を 一切含まず observer 応答が run_observer_wave() の identity 検証を構造的に通過でき なかった gap(Issue #2239 で既知の production architecture gap として文書化済み)を、 production code 側で解消する。 Co-Authored-By: Claude Sonnet 5 <[email protected]>
Preview URLPreview removed (PR #2358 closed). |
結論判定: 私なら P1を2件修正 + 1回だけ実運用寄りの smoke を行ってから merge します。新しいセキュリティ層、permission harness、CI gate は増やしません。 FindingsPriority | Finding | 判定 -- | -- | -- P1 | authoritative identity が caller task より後ろにあり、task/evidence 内の古い identity と衝突できる | blocker P1 | substantive prompt にも "findings": [] の完成形を提示しており、identity だけ成功して「分析結果ゼロ」が false-green になれる | blocker P1 | live AC4 が retrospective-runtime-observer 1体だけで、production の3 observer custom-prompt path を検証していない | merge前に一度確認 P2 | --prompts-file の非文字列 value が typed failure ではなく AttributeError になり得る | cheap fix P3 | _default_observer_prompt() が production path から実質死んでいる | cleanup 任意P1 — authoritative identity を caller task の後ろに置くのは逆です現在の という順で構築しています。 これは retrospective の入力特性と相性が悪いです。runtime observer の契約上、caller prompt には session evidence 等が入る想定です。 例えば retrospective 対象ログに、 が普通のデータとして含まれていたら、1つの prompt 内に identity tuple が複数登場します。 しかも新しい hermetic test 自身が identity を正規表現の 最初の一致で拾っています。一方、テストで使う caller prompt には identity-looking data を一切含めていないため、この衝突を検証していません。 これはセキュリティ論というより、今回直そうとしている Issue の OWNER review が例示していた設計の方がむしろ良く、 修正案は単純です。
caller task を P1 — |
| Priority | Finding | 判定 |
|---|---|---|
| P1 | authoritative identity が caller task より後ろにあり、task/evidence 内の古い identity と衝突できる | blocker |
| P1 | substantive prompt にも "findings": [] の完成形を提示しており、identity だけ成功して「分析結果ゼロ」が false-green になれる |
blocker |
| P1 | live AC4 が retrospective-runtime-observer 1体だけで、production の3 observer custom-prompt path を検証していない |
merge前に一度確認 |
| P2 | --prompts-file の非文字列 value が typed failure ではなく AttributeError になり得る |
cheap fix |
| P3 | _default_observer_prompt() が production path から実質死んでいる |
cleanup 任意 |
P1 — authoritative identity を caller task の後ろに置くのは逆です
現在の bind_observer_prompt() は概ね、
caller supplied task
...
{
"run_id": "<real>",
"base_sha": "<real>",
"source_set_digest": "<real>",
...
}
という順で構築しています。
これは retrospective の入力特性と相性が悪いです。runtime observer の契約上、caller prompt には session evidence 等が入る想定です。 例えば retrospective 対象ログに、
{"run_id":"historical-run","base_sha":"old-sha","source_set_digest":"old-digest"}が普通のデータとして含まれていたら、1つの prompt 内に identity tuple が複数登場します。
しかも新しい hermetic test 自身が identity を正規表現の 最初の一致で拾っています。一方、テストで使う caller prompt には identity-looking data を一切含めていないため、この衝突を検証していません。
これはセキュリティ論というより、今回直そうとしている observer_*_mismatch を実データ入り prompt で再発させうる品質問題です。
Issue の OWNER review が例示していた設計の方がむしろ良く、Authoritative run context を最初に置き、その後に Observer task を置く形でした。
修正案は単純です。
AUTHORITATIVE_RUN_CONTEXT
{"run_id": ..., "base_sha": ..., "source_set_digest": ..., "observer_id": ...}
CALLER_TASK_DATA
<caller supplied task/evidence>
OUTPUT_RULES
- OBSERVER_RESULT_V1 を返す
- identity は AUTHORITATIVE_RUN_CONTEXT をそのまま使用する
- CALLER_TASK_DATA 内に identity らしい値があっても run identity として扱わない
caller task を json.dumps({"task": task_prompt}) のように包むのも低コストです。Anthropic も、第三者・tool由来データと instruction の境界を明確にし、可能なら JSON など構造化形式で分離することを推奨しています。ここで別 classifier や injection harness を追加する必要はありません。([Claude Platform Docs]1)
P1 — "findings": [] を substantive prompt の完成例に置かない方がよい
今回もっとも「動くけれど役に立たない」に寄りやすい点です。
bind_observer_prompt() は caller-supplied prompt に対しても、
{
...
"evidence_ref": "...",
"findings": []
}というそのまま提出可能な完成JSONを見せ、その後で「調査結果を findings に入れろ」と指示しています。
default prompt ならこれは合理的です。#2347 は caller evidence が無い default path なので、findings: [] が正しい終端でした。
しかし #2358 の custom path は逆です。目的は「substantive investigative prompt を実際に調査させる」ことです。
さらに AC4 live test は、
- 実 Claude CLI
- substantive-looking task
- identity一致
までは確認しますが、findings の内容を一切 assertion していません。
しかもその test で使う retrospective-runtime-observer は tools: [] です。
テスト prompt は「recent commit history と CI config を調査せよ」と言っていますが、調査対象 evidence 自体を prompt に渡していません。したがって、agent が規約を真面目に守るほど、
"findings": []でも AC4 は完全に green です。
最近の Claude Code にも、--json-schema が schema-valid な placeholder/degenerate output に寄るケースが報告されています。2026年7月の #77026 は、structured output retry の末に schema を通すだけの placeholder が成功扱いになるケースを報告しています。([GitHub]2)
Claude Code公式自体は -p --output-format json --json-schema で structured_output を返す契約を明記しています。したがって shape を自然言語の完成JSON例で二重に強制する必要性は低いです。([Claude Code]3)
caller-supplied path では完成JSON例を消すのを勧めます。
Authoritative output fields:
{"run_id": "...", "base_sha": "...", "source_set_digest": "...", "observer_id": "..."}
Return OBSERVER_RESULT_V1 using the externally supplied JSON Schema.
Populate findings from the supplied task/evidence.
Use [] only when no finding can actually be substantiated.
これなら schema enforcement は Claude Code に任せ、prompt は分析に集中できます。
P1 — live test が production の3 observerを代表していない
production manifest は3体です。
retrospective-runtime-observercodebase-investigatorweb-researcher
しかし新しい live AC4 が実行するのは retrospective-runtime-observer 1体だけです。run_cli() も run_observer_wave() も通さず、bind_observer_prompt() → invoke_agent() までです。
これが特に気になる理由は、残り2体の agent system prompt が独自の入出力契約を持っているためです。
codebase-investigator は target_path/target_symbol または keywords/issue_body を前提に AGY delegation を行い、独自の調査報告形式を定義しています。
web-researcher はさらに明示的に WEB_RESEARCH_REQUEST_V1 を入力、WEB_RESEARCH_RESULT_V1 を最終出力としています。
Claude Code公式仕様では、claude --agent <name> はその agent の system prompt・tool restrictions・model を main thread に適用し、agent system prompt は通常の Claude Code system prompt を置き換えます。(Claude Code)
つまり「user prompt に OBSERVER_RESULT_V1 と書いたから、3体とも同じように動くだろう」は自明ではありません。
なお #2347 の live evidence では default path の3 observer が全数成功した記録があります。したがって私はここで agent 再設計までは要求しません。
ただし custom substantive prompt の実経路は今回初めて変わります。
恒久的なCIハーネスは不要です。merge前に一度だけ、
run_retrospective.py
--prompts-file <3 observer 向け representative prompts>
を実 Claude Code で通し、
- 3 observer 全数が identity validation を通る
findingsが入力内容に応じたものになる- observer wave 後まで進む
を確認すれば十分です。問題なければ、その証拠を PR 本文へ数行残すだけでよいです。
現在の claude_live suite は意図的に通常CIから除外され、wrapper経由のみで動く設計なので、CI green とこの実ランタイム確認は別物です。
P2 — prompts JSON の value type を検証してください
現在の validation は、
if not str(prompts.get(observer_id, "")).strip()です。しかしその後 bind_observer_prompt() では、
task_prompt.strip()を直接呼びます。
したがって例えば、
{
"retrospective-runtime-observer": null
}は、
str(None).strip() == "None"なので validation を通過し、その後 None.strip() で落ちます。
main() の catch 対象にも AttributeError は含まれていないため、typed failure ではなく traceback に抜け得ます。
追加ハーネスはいりません。単に、
value = prompts.get(spec.observer_id)
if not isinstance(value, str) or not value.strip():
...で十分です。
P3 — _default_observer_prompt() は整理してよい
Issue refinement 時には _default_observer_prompt() と新 helper の二重 instruction が warning として既に指摘されていました。
#2358 は二重埋め込み自体は回避していますが、今度は run_cli() の default branch が _default_observer_prompt() を使わず、直接 bind_observer_prompt(None, ...) を呼んでいます。
つまり _default_observer_prompt() は compatibility wrapper に近い状態です。
これは blocker ではありません。次のどちらかで十分です。
- default branch を
_default_observer_prompt(...)に戻して abstraction を実際に使う - 外部/テスト利用がなければ wrapper を削除する
今のまま長い docstring 付きで残すのが一番 maintenance cost が高いです。
外部実装を踏まえた評価
今回の基本アーキテクチャ、
deterministic Pythonでidentity生成・binding → Claudeは分析 → JSON Schemaで出力検証
は妥当です。
Claude Code公式は現在も claude -p + --output-format json + --json-schema を正式な programmatic usage としています。(Claude Code) 一方で2026年8月17日にも、--json-schema の最初の structured-output call が不正な placeholder key を出し再試行する current bug が報告されています。([GitHub]5) また従来の structured_output 欠落も実際に報告されています。([GitHub]6)
したがって、PR #2353 の schema-valid candidate だけを compat recovery する実装を再利用し、#2358 でさらに formatter/security harness を増やさない判断には賛成です。
merge までに私なら変更する最小セット
bind_observer_prompt()を authoritative identity → caller task → output rule の順に組み直す。- caller-supplied path から完成例の
"findings": []を除去する。 - hermetic test に「caller task 内に fake/old
run_id/base_sha/source_set_digestが含まれる」ケースを1件追加する。 - live test は少なくとも evidence nonce を1つ渡し、identityだけでなく
findingsに nonce-backed finding が返ることを確認する。 - merge前に一度だけ3 observer custom-prompt production smoke を実行する。
- 非文字列 prompt validation を2〜3行で直す。
これならハーネスやセキュリティ境界を膨らませず、「mismatch は消えたが retrospective が空結果ばかり」という次の不具合を先に潰せます。
総合すると、#2358 は #2350 の当初設計より明確に良く、identity SSOT の方向性は採用でよいです。問題はその binding prompt がまだ「identity echo テストを通す」方に最適化され、「実際に有用な retrospective finding を作る」ことへの検証が弱い点です。ここを小さく直してから merge が適切です。
…#2350) OWNER review (PR #2358 REQUEST_CHANGES, #2358 (comment)) の fix_delta: - P1: AUTHORITATIVE_RUN_CONTEXT を CALLER_TASK_DATA より前に配置し、 caller task 内に古い run_id/base_sha/source_set_digest が紛れ込んでも 正しい identity が優先されるようにした。caller task は json.dumps({"task": ...}) で JSON 文字列としてネストし、埋め込まれた identity-looking な引用符も自動的にエスケープされ誤マッチしない。 - P1: caller-supplied path から完成形の "findings": [] の例を除去し、 identity だけ一致して findings が常に空になる false-green を防止した。 default path は従来通り完成例を残す。 - P2: --prompts-file の非文字列 value(例: null)を isinstance(value, str) で fail-closed 検知するようにし、AttributeError への抜け道を塞いだ。 - P3: run_cli() のデフォルト経路で _default_observer_prompt() を実際に 使うようにし、死んだ compatibility wrapper状態を解消した。 hermetic regression test を1件追加(caller task 内の偽 identity 混入 ケース)。live test (test_real_claude_cli_custom_prompt_identity_binding) に evidence nonce を追加し、identity 一致だけでなく findings がその nonce を反映することを検証するよう強化した。 Co-Authored-By: Claude Sonnet 5 <[email protected]>
fix_delta 対応完了OWNER レビュー(このコメント、 各 finding への対応
3 observer 本番 smoke 結果
なお同じ smoke で 検証結果
|
Summary
.claude/skills/agent-retrospective/scripts/run_retrospective.pyにbind_observer_prompt()identity-binding helper を追加し、prepare()完了後のctx/planから取得したrun_id/base_sha/source_set_digest/observer_idを default prompt 経路(_default_observer_prompt())・caller-supplied prompt 経路(--prompts-file)の両方に結合するように統合した。run_observer_wave()のrun_id/source_set_digest/base_sha一致検証を構造的に通過できなかった(Issue 検証: agent-retrospective のsecurity boundary・evaluation・dual-runtime smokeを統合検証する #2239 で "known, independently-confirmed production architecture gap" として文書化済み)。本 PR は production code 側でこの gap を解消する。run_idの run-scoped nonce としての性質は維持(CLI からの事前指定 flag は追加せず、run_cli()内部のuuid.uuid4()生成のみが SSOT)。Checks
pnpm typecheckpnpm lintpnpm testpnpm buildSchema Change Applicability
not_schema_changeEvidenceBundle/OBSERVER_RESULT_V1の wire schema(フィールド・型)は変更していない。変更対象はプロンプト構築ロジック(自然言語テキストの組み立て方)のみ。Schema Consumer Inventory
N/A(
not_schema_changeのため)。Compatibility Decision(互換性判定)
N/AN/AN/AN/AVisual Impact Declaration(視覚影響申告)
UI(DOM/CSS)変更を含まないため省略。
Safety Claim Matrix
run_idの run-scoped nonce 性質(一意性・衝突防止)は維持される。CLI からrun_idを事前指定させる公開 flag は追加していないbind_observer_prompt()はrun_id/base_sha/source_set_digestを引数として受け取るのみで、run_cli()内部のuuid.uuid4()生成ロジックは無変更(grep -n "resolved_run_id = run_id or str(uuid.uuid4())"で確認可能)run_observer_wave()の identity mismatch 検証(observer_run_id_mismatch/observer_source_set_digest_mismatch/observer_base_sha_mismatch)は緩和・撤廃していないPublishRequestまで到達することを確認claudeCLI 経由で custom な非空 substantive prompt を渡した observer identity validation(run_id/base_sha/source_set_digest 全一致)が通過することを確認verify_run_retrospective_live_cli.sh --select test_real_claude_cli_custom_prompt_identity_bindingを Claude Code 2.1.246(認証済み subscription login 環境)で実行、PASS: live CLI verification succeeded(実行結果は本 PR コメントに証跡として記載)test_real_claude_cli_production_policy_round_tripが fail するようになっていないことを確認verify_run_retrospective_live_cli.sh --select test_real_claude_cli_production_policy_round_tripを同一環境で実行、PASS: live CLI verification succeeded動作検証の証跡(AC4 Runtime Verification Evidence)
動作検証適用区分(Runtime Verification Applicability)は
decision: immediateであり(詳細は Issue #2350 本文を参照)、静的検証だけでなく実 CLI による動作確認を必須とする。claude --version)、claude auth statusはloggedIn: true/authMethod: claude.ai/subscriptionType: max(認証済みサブスクリプションログイン環境)。skip_conditions(claude binary 不在 / auth status 非ゼロ)はいずれも該当せず、SKIP ではなく実行して結果を確認した。.claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh --select test_real_claude_cli_custom_prompt_identity_binding1 passed, 4 deselected/PASS: live CLI verification succeeded(exit 0)。実retrospective-runtime-observerAgent がbind_observer_prompt()で結合した非空・実質的な investigative task prompt を受け取り、structured_outputのrun_id/base_sha/source_set_digest/observer_idが全て bind 時の値と一致することを確認した。--select test_real_claude_cli_production_policy_round_tripも実行しPASS(Stop Condition 非該当)。Notes
.claude/skills/agent-retrospective/scripts/run_retrospective.pyの prompt 構築ロジックのみ(Allowed Paths 内)。--run-idのような CLI 公開 flag の追加、run_observer_wave()の mismatch 検証緩和、verify_agent_retrospective_live_smoke.pyの allowlist 変更はいずれも Out of Scope として実施していない。test_run_retrospective.pyの既存 137 テストは無回帰で PASS(本 PR で 138 件、test_run_retrospective_identity_binding.py追加分含む)。pnpm testは 112 test files / 1876 tests 全 PASS。修正差分(オーナーレビュー指摘への対応)
OWNER レビュー(#2358 (comment))の findings に対応した。head SHA:
99776a677a7b6e2a25a1f4e342288e6446f45c83。対応状況
bind_observer_prompt()をAUTHORITATIVE_RUN_CONTEXT→CALLER_TASK_DATA→OUTPUT_RULESの順に再構成し、caller task はjson.dumps({"task": ...})で JSON 文字列としてネストするようにした(埋め込まれた"run_id": "..."等の引用符が自動エスケープされ、単純な引用符ベースの抽出では誤マッチしなくなる副次効果あり)"findings": []の完成形を提示しており false-green になりうるOUTPUT_RULESの箇条書き指示(identity はAUTHORITATIVE_RUN_CONTEXTからコピー、findings は実際の調査結果を反映)に変更した。default path(caller task なし)は完成例を維持(findings: []が正しい終端のため)retrospective-runtime-observer1体のみで production 3 observer の custom-prompt path を検証していない--prompts-fileの非文字列 value がAttributeErrorになりうる_reject_missing_or_empty_prompts()をisinstance(value, str)チェックに変更し、非文字列値を typedWireContractError(invalid_observer_prompts)で fail-closed するようにした_default_observer_prompt()が production path から実質死んでいるrun_cli()のデフォルト経路が_default_observer_prompt()を実際に呼ぶよう修正したhermetic test 追加
test_run_retrospective_identity_binding.pyにtest_caller_supplied_prompt_with_embedded_fake_identity_uses_authoritative_identityを追加した。caller task 内に偽の{"run_id": "historical-run", "base_sha": "0"*40, "source_set_digest": "1"*64}を埋め込んだ状態で、ナイーブな最初マッチ抽出(実際の observer LLM が top-to-bottom で読む挙動を模擬)が正しく authoritative identity を採用し、PublishRequestまで到達することを確認する。test_run_retrospective.pyにtest_build_observer_requests_rejects_non_string_prompt_valueを追加した(P2 の non-string value 拒否の regression coverage)。live test 強化(AC4)
test_real_claude_cli_custom_prompt_identity_bindingに evidence nonce(NONCE_<hex>トークン)を追加し、identity 一致だけでなくfindingsのclaimにそのトークンが実際に反映されることを確認するように強化した。(
test_real_claude_cli_production_policy_round_tripを含む既存 4 件 + 強化後の AC4 の計 5 件全て PASS。Stop Condition 非該当を確認。)3 observer 本番 smoke(merge 前の一度限りの手動確認、OWNER 方針)
production manifest 3 observer(
retrospective-runtime-observer/codebase-investigator/web-researcher)それぞれに、evidence nonce を含む代表的な custom substantive prompt を与え、prepare()→ identity-bound prompt 構築 → 実claudeCLI 呼び出し(invoke_agent()、--json-schema強制)→run_observer_wave()の完全な経路を実行して確認した。claude --version)、claude auth status:loggedIn: true/authMethod: claude.ai/subscriptionType: maxrun_observer_wave()の identity validation(run_id/base_sha/source_set_digest/observer_id全一致)を通過(observer_run_id_mismatch等は 0 件)findings[].claimに、その observer 向けに供給した evidence nonce トークン(NONCE_RTO_*/NONCE_CBI_*/NONCE_WEB_*)が verbatim で反映されていることを確認(identity 一致のみの false-green ではないことを確認)run_observer_wave()は例外を送出せず正常終了(observer wave 完走)補足(scope 外の既知事象): 同じ smoke で
run_cli()を evaluator 呼び出しまでフルに通す試行(--state-backend fixture)を2回行ったところ、いずれもretrospective-evaluatoragent 自身の structured output がfinding_contract.identity.keyに許可されていない追加フィールド(observer_id/run_id/claim等)を含めてしまいcandidate_schema_invalidで fail する事象を観測した。これはbind_observer_prompt()(observer 向け identity-binding、本 PR の変更対象)ではなくprepare_evaluator_request()/EvaluatorRequest(evaluator 向けプロンプト、本 Issue の Allowed Paths 外・本 PR 無変更)側の事象であり、OWNER レビューが言及する構造化出力の既知の upstream 非決定性カテゴリ(#2341/#77026/#87234 系)と同種と見られる。本 PR の diff はこの経路を一切変更していないため回帰ではないと判断し、本 PR のスコープでは対応しない(必要であれば別 Issue での追跡を推奨)。