Skip to content

test: agent-retrospective の実 claude CLI 統合検証を追加し adapter を最小修正する (#2301) - #2324

Merged
squne121 merged 4 commits into
mainfrom
worktree-issue-2301-agent-invocation-live-cli
Aug 24, 2026
Merged

squne121 merged 4 commits into
mainfrom
worktree-issue-2301-agent-invocation-live-cli

Conversation

@squne121

@squne121 squne121 commented Aug 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Closes #2301(この PR で Issue #2301 を close します)。

.claude/skills/agent-retrospective/scripts/run_retrospective.py の production Agent invocation adapter(build_agent_invocation_argv() / invoke_agent())を実 claude CLI(2.1.241)で統合検証しました。実行環境(claude バイナリ + 認証済み subscription login)が利用可能だったため、AC5〜AC7 の動作検証は SKIP せず実際に実行し、いずれも PASS しました。

検証の過程で、--agent <custom-subagent> --json-schema ... 形式の invocation は schema-less(--agent なし)invocation と異なり、応答 wrapper に structured_output フィールドが含まれないという adapter 境界内の不整合を発見しました(AC9)。invoke_agent() に、渡した schema ファイルへ独立に再検証してから受理する compat 回収経路を追加し、この不整合を最小修正しています。

fix_delta(OWNER REQUEST_CHANGES 対応、head 194ac42e → ee0fb59b)

OWNER の REQUEST_CHANGES(https://github.com/squne121/loop-protocol/pull/2324#issuecomment-5394190352)を受け、以下を適用しました。

  • P0-1(Blocker): invoke_agent() が subtype を一度も検証していなかった adapter bug を修正。payload.get("subtype") != "success" を、structured_output/result の回収より前に検証し、非成功 subtype(例: error_max_structured_output_retries)を status="partial_result", reason_code=f"result_subtype_not_success:{subtype or 'missing'}" として拒否するようにしました。また compat 回収(旧 _structured_output_from_result_fallback、P1-1 でリネーム)を許すのは structured_output が absent または明示的に None の場合のみに限定し、string/list/number/bool など present だが非 dict の値は回収せず status="malformed_output", reason_code="missing_structured_output" として拒否するようにしました。
  • P0-2(Blocker): verify_run_retrospective_live_cli.sh が SKIP/preflight 判定より前に、常に tested HEAD: <sha>(git -C "$REPO_ROOT" rev-parse HEAD による live 値)を出力するようにしました。再発防止のための恒久対応です。
  • P1-1: _structured_output_from_result_fallback を _structured_output_from_result_compat へリネームし、docstring/コメントを観測事実ベースの記述に縮小しました(custom-subagent が structured_output を「never populate する」という断定を削除)。「fallback」という語は 検証・修正: agent-retrospective の production Agent invocation adapter を実 Claude CLI で確認する #2301/検証: agent-retrospective のsecurity boundary・evaluation・dual-runtime smokeを統合検証する #2239 の別文脈と衝突するため、この構造物の命名からは避けています。
  • P1-2: test_run_retrospective.py に P0-1 の regression 回帰テストを含む 6 件の hermetic テストを追加(absent/explicit-null 回収、非成功 subtype は必ず非 ok、present だが非 dict は回収しない、dict は result より優先、schema 不適合な result は malformed_output)。
  • P1-3: test_run_retrospective_live_cli.py の invalid-agent-name / invalid-schema live テストに status not in ("timeout", "terminated") 等の operational-failure 除外アサーションと adapter status/reason_code/exit_code の証跡出力を追加。invalid-agent-name のプロンプトも、default agent への silent fallback が誤って schema 適合してしまわないよう強化しました。
  • P1-4: verify_run_retrospective_live_cli.sh の --select オプションで値省略時(末尾 --select 等)に shift 2 が失敗して無限ループしうる不具合を修正。値が空またはフラグ様の場合は即座に exit 2 します。
  • P1-5: build_agent_invocation_argv() の --disallowedTools を、カンマ区切り単一文字列から個別 argv 要素(--disallowedTools tool1 tool2 ...)へ変更し、公式 CLI reference の argv 形式に整合させました。mock test の期待 argv も更新しています。

適用しなかった項目(Allowed Paths 外の follow-up として維持):

  • P2: SKILL.md の --schema-dir <placeholder> コマンド例は本 PR の Allowed Paths 外のため対応していません。

fix_delta 適用後、branch を main から gh pr update-branch で更新しています(merge commit、conflict なし。main 側の差分は docs/dev/agent-skill-boundaries.md のみで agent-retrospective スクリプトとの重複なし)。

Checks

  • .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective_live_cli.py を新規追加(claude_live マーカー付き実 CLI 統合テスト、AC1)
  • .claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh を新規追加・実行可能属性を付与(SKIP(exit 77)/FAIL(exit 1) 区別ラッパー、AC2)
  • pyproject.toml の claude_live marker 登録(default addopts に and not claude_live を追加、AC3)
  • .github/ci/python-test-plan.json の runtime_verification_only_markers に claude_live を追加(AC3)
  • .claude/skills/agent-retrospective/scripts/schemas/observer_result_v1.schema.json / evaluation_result_v1.schema.json を新規追加し、既存 mock harness に schema-dataclass parity テストを追加(AC4)
  • uv run --locked pytest .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective.py -q → 135 passed(fix_delta の P1-2 新規 6 件を含む、AC10)
  • .claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh を最終 HEAD で実行し AC5/AC6/AC7 の live case 3 件全て PASS を確認(3 passed in 12.80s)
  • 実行証跡ログを artifacts/2301/live-cli-verification-log.txt(ローカル、gitignore 対象)に記録し、下記「実行証跡」節に転記(AC8)

Schema Change Applicability

  • decision: schema_change
  • reason: .claude/skills/agent-retrospective/scripts/schemas/observer_result_v1.schema.json と evaluation_result_v1.schema.json を新規追加します。これらは run_retrospective.py の --schema-dir 既定値(_SCRIPTS_DIR / "schemas")が指す先であり、build_observer_requests() / run_cli() が実 claude CLI の --json-schema フラグへ渡す wire-contract schema そのものです。

Schema Consumer Inventory

# before
(該当ファイルは存在しなかった。scripts/schemas/ ディレクトリ自体が本 PR で新規作成される。
run_retrospective.py の --schema-dir 既定値はこの Issue 以前から `_SCRIPTS_DIR / "schemas"` を
指していたが、実ファイルが未配置だったため、--schema-dir を省略した実行は
FileNotFoundError で失敗していた)

# after
observer_result_v1.schema.json:
  {schema_version, run_id, base_sha, source_set_digest, observer_id, evidence_ref, findings[]}
  -- EvidenceBundle dataclass (WIRE_SCHEMA_EVIDENCE_BUNDLE = "observer_result/v1") と
  1:1 でフィールド一致(parity テストで固定)
evaluation_result_v1.schema.json:
  {schema_version, run_id, base_sha, source_set_digest, candidate_records[], evidence_ref}
  -- Evaluation dataclass (WIRE_SCHEMA_EVALUATION = "evaluation_result/v1") と
  1:1 でフィールド一致(parity テストで固定)
Consumer ファイル 更新有無 備考
.claude/skills/agent-retrospective/scripts/run_retrospective.py(build_observer_requests() / run_cli() の --schema-dir 既定値、main() の --json-schema-dir 相当引数) 更新なし(消費先の期待フィールド集合は変更していない。新規追加したのはこのパスが指す先の実ファイルのみ) 既存の json_schema_path=str(schema_dir / "observer_result_v1.schema.json") / .../"evaluation_result_v1.schema.json" 参照コードは変更なし
.claude/skills/agent-retrospective/scripts/tests/test_run_retrospective.py(AC4 parity テスト、fix_delta P1-2/P1-5) 更新あり schema の required/properties キー集合が EvidenceBundle/Evaluation dataclass のフィールド集合と一致することを固定検証。加えて P0-1 subtype 回帰テスト 6 件と --disallowedTools argv 形状テストを追加更新
.claude/skills/agent-retrospective/scripts/tests/test_run_retrospective_live_cli.py(新規、fix_delta P1-3 で強化) 更新あり 実 claude CLI が --json-schema にこのファイル内容を渡された時に返す structured_output を検証。operational-failure 除外アサーションと証跡出力を追加

Safety Claim Matrix

Claim Implemented? Not controlled Evidence Follow-up
invoke_agent() は type == "result" かつ subtype == "success" を、structured_output/result の回収より前に検証し、非成功レスポンスを ok へ昇格しない yes N/A run_retrospective.py の invoke_agent()(subtype != "success" ガード)。P1-2 の regression テストで固定 N/A
invoke_agent() の compat 回収(_structured_output_from_result_compat())は、渡された schema ファイルへ独立に jsonschema.validate() で再検証してから受理し、structured_output が absent/None の場合にのみ発動する。present だが非 dict の値は回収しない yes N/A .claude/skills/agent-retrospective/scripts/run_retrospective.py の _structured_output_from_result_compat()(jsonschema.validate() 呼び出し、例外時 None 返却)。実 CLI での AC5 round-trip(exact-equality アサーション)と P1-2 regression テストで確認 N/A
verify_run_retrospective_live_cli.sh は claude バイナリ不在 / 未認証時のみ SKIP(exit 77) を返し、invocation 開始後の失敗は SKIP に変換しない。--select の値省略は exit 2 のエラーとして扱う yes N/A 本 PR で PATH を /usr/bin:/bin に制限した状態で wrapper を実行し exit 77 を確認(下記「実行証跡」節)。SKIP は default pytest フィルタ(claude_live marker)で常時除外され CI の必須ゲートには含まれない。--select 未指定値は exit 2 で拒否することを手動確認 N/A
DelegatedAgentPermissionPolicy の --disallowedTools argv は、拒否 tool を個別 argv 要素として実 CLI 呼び出しへ渡す(policy が有効であることの証明ではなく、deny values を CLI へ渡したことの記録) yes policy 自体の enforcement は本 Issue のスコープ外(実行前 argv 構築のみを検証) build_agent_invocation_argv() の argv += ["--disallowedTools", *sorted(policy.denied_tools)]。mock test で argv 個別要素形状を固定検証 N/A

実行証跡(AC8: artifacts/2301/live-cli-verification-log.txt)

tested_head: ee0fb59b6a0662b2b476a7a3451e12cc33f1833d
resolved_claude_binary_path: /home/squne/.local/bin/claude
claude_version: 2.1.241 (Claude Code)

Case 1/3 real_claude_cli_production_policy_round_trip (AC5): PASS, exit_code 0
Case 2/3 real_claude_cli_invalid_agent_name_error_handling (AC6): PASS, exit_code 0
Case 3/3 real_claude_cli_invalid_schema_fail_closed (AC7): PASS, exit_code 0

Combined run: "3 passed in 12.80s", wrapper exit_code 0
wrapper stdout: "verify_run_retrospective_live_cli.sh: tested HEAD: ee0fb59b6a0662b2b476a7a3451e12cc33f1833d"

SKIP-path manual verification (PATH restricted to /usr/bin:/bin):
  "SKIP: claude binary not found in PATH (skip_condition: claude binary not in PATH)"
  exit_code 77

AC10 regression: "135 passed in 1.78s" (test_run_retrospective.py, fix_delta P1-2 の新規 6 件を含む)

artifacts/ はリポジトリ全体で .gitignore 対象のため、上記ログファイルは commit に含めていません(この PR body に全文を転記しています)。

Notes

build_agent_invocation_argv()/invoke_agent() を実 claude CLI (2.1.241) で
統合検証する claude_live マーカー付き pytest を追加した (AC1/AC3)。SKIP(exit
77)/FAIL(exit 1) を区別する verify_run_retrospective_live_cli.sh ラッパーを
追加し、claude バイナリ不在・未認証時のみ SKIP とする (AC2)。

--schema-dir の既定値が指す scripts/schemas/ に observer_result_v1 /
evaluation_result_v1 の canonical JSON Schema を追加し、EvidenceBundle /
Evaluation dataclass とのフィールド一致を既存 mock harness で検証する (AC4)。

実 CLI での動作検証により、`--agent <name> --json-schema ...` invocation は
schema-less invocation と異なり応答 wrapper に structured_output フィールド
を含めないという adapter 境界内の不整合を発見した。invoke_agent() に、
result テキスト(markdown fence を含みうる)から JSON を抽出し、渡した schema
ファイルに対して独立に再検証してから受理するフォールバックを追加した
(structured_output が存在する場合は従来どおりそちらを優先し、フォール
バックは値が欠落している場合のみ発動する。P0-1 の「wrapper 自体を business
schema として再解釈しない」という方針は維持する) (AC9)。

正常系 (production policy round-trip)・agent 名不正・schema 不正の 3 ケース
を実 claude CLI に対して実行し全て PASS したことを確認した (AC5/AC6/AC7)。
実行証跡は artifacts/2301/live-cli-verification-log.txt に記録した (AC8)。
既存 mock harness ベーステスト test_run_retrospective.py は全 129 件 PASS
(AC10)。

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@github-actions

github-actions Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

Preview removed (PR #2324 closed).

@squne121

Copy link
Copy Markdown
Owner Author

判定

REQUEST_CHANGES

実 Claude CLI を使って mock-only 検証の穴を発見し、result テキストを JSON Schema で再検証して回収する方向自体は妥当です。実際、Claude Code では structured_output が欠落または null になり、schema 適合 JSON が result に入る事例が報告されています。したがって、互換回収経路を削除して旧実装へ戻すべきではありません。 (GitHub)

ただし現状は、非成功レスポンスを成功へ反転し得る adapter bugと、現HEADに束縛されていないlive証跡が残っています。この2点はマージ前に修正が必要です。


Blocker

P0-1 — subtype != success のレスポンスを fallback が ok へ昇格できる

invoke_agent() は現在、次の順序でレスポンスを処理しています。

  1. subprocess exit code

  2. JSON objectか

  3. is_error がtruthyか

  4. type == "result" か

  5. structured_output がdictか

  6. dictでなければ result を再parse・schema検証

  7. schemaに通れば status="ok"

しかし、subtype を一度も検証していません。そのため、例えば次のwrapperを返された場合、

{
  "type": "result",
  "subtype": "error_max_structured_output_retries",
  "is_error": false,
  "result": "{\"schema_version\":\"observer_result/v1\", ...}"
}

result 内のJSONがschemaに適合すれば、現実装は ok を返せます。現在の実装は、type と is_error だけを見た後、非dictの structured_output を無条件で fallback に流しています。

公式のStructured Outputs契約では、成功判定は少なくとも type == "result"、subtype == "success"、構造化値が存在することに依存します。error_max_structured_output_retries は失敗として扱う必要があります。 (Claude)

必須修正

fallbackへ進む前に subtype を検証してください。

if payload.get("type") != "result":
    return AgentInvocationResult(
        status="malformed_output",
        structured_output=None,
        raw_stdout_excerpt=_stdout_excerpt(completed.stdout),
        exit_code=completed.returncode,
        reason_code="unexpected_wrapper_shape",
    )

subtype = payload.get("subtype")
if subtype != "success":
return AgentInvocationResult(
status="partial_result",
structured_output=None,
raw_stdout_excerpt=None,
exit_code=completed.returncode,
reason_code=f"result_subtype_not_success:{subtype or 'missing'}",
)

そのうえで、互換回収を許す条件を次に限定するのが適切です。

_MISSING = object()
raw_structured_output = payload.get("structured_output", _MISSING)

if isinstance(raw_structured_output, dict):
structured_output = raw_structured_output
elif raw_structured_output is _MISSING or raw_structured_output is None:
structured_output = _structured_output_from_result_compat(
payload,
json_schema_path=request.json_schema_path,
)
else:
structured_output = None

つまり、

  • absent:互換回収可

  • null:互換回収可

  • dict:正規値として採用

  • string / list / number / bool:wrapper契約違反として拒否

としてください。


P0-2 — live証跡の tested_head がPR現HEADではない

PR本文のlive証跡は次のコミットに対するものです。

tested_head: 194ac42e25df08fb21d07e8b60abc794acb811cd

一方、現在のPR HEADは次です。

c180a5dff121affe74c142a9d17b9c8733edc3ef

PRはその後mainをマージした2コミット構成になっています。Issue #2301のAC8は、証跡にtested HEADを記録することを明示的に要求しています。したがって、現在の証跡では現HEADがlive検証済みとは判定できません。

required CIが現HEADで成功していても、claude_live は意図的に通常CIから除外されているため代替証明にはなりません。

必須修正

最終修正後のHEADで以下を再実行し、PR本文の証跡を更新してください。

git rev-parse HEAD

.claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh

また、手作業でHEADを書き写す運用は再発しやすいため、thin wrapper自身に次だけ追加するのが合理的です。

TESTED_HEAD="$(git -C "$REPO_ROOT" rev-parse HEAD)"
echo "verify_run_retrospective_live_cli.sh: tested HEAD: $TESTED_HEAD"

新しいreceipt schema、artifact collector、GitHub Actions jobは不要です。


P1 findings

P1-1 — fallbackの説明と実装条件が一致していない

helperのdocstringとPR本文は「structured_output が欠落した場合のみ fallback」と説明しています。ところが呼び出し側は、

if not isinstance(structured_output, dict):

なので、欠落だけでなく次の全てを回収対象にします。

null
string
array
number
boolean

さらにコメントは「custom-subagent invocation は structured_output をnever populateする」と断定しています。しかし公式契約では本来 structured_output が正規経路であり、実際の不具合報告でも「欠落」と「null」の双方が存在します。「常に存在しない」という恒久的CLI契約として固定するのは不正確です。 (GitHub)

推奨修正

コメントを次のような観測事実へ縮小してください。

Claude Code 2.1.241 の project custom-agent invocation で、subtype: success にもかかわらず structured_output が欠落し、schema適合JSONが result に入る挙動を観測した。正規のdict値を優先し、欠落またはnullの場合に限って互換回収する。

また、fallback という名前は #2301 の「fallback経由の成功は禁止」および後続 #2239 の「fallbackなし」と衝突します。後者はruntime/profile fallbackを指していますが、同じ語を使うと後続エージェントが誤判定します。

例えば次へ改名する方が明確です。

_structured_output_from_result_compat()

あるいは、

_recover_structured_output_from_success_result()

P1-2 — 新しいproduction fallbackに対するhermetic回帰テストが足りない

既存mock testには、

  • 正常な structured_output の抽出

  • result が単なる文章の場合の拒否

  • wrapper shape不正の拒否

はあります。しかし、今回追加された主要productionロジックについて、次が固定されていません。

最低限、以下の6ケースを追加すべきです。

Case 期待値
subtype=success、structured_output 欠落、result が生JSON ok
subtype=success、structured_output=null、fenced JSON ok
subtype!=success、result がschema適合 必ず非ok
structured_output がstring/list、result がschema適合 必ず非ok
dictの structured_output と異なる result が共存 dictを優先
result がJSONだがschema不適合 malformed_output

特に3番目はP0-1の再発防止になります。

これは既存test fileへの数件のfixture追加で足り、live harnessの拡張は不要です。


P1-3 — 異常系live testが「目的のエラー」を証明していない

invalid-agentとinvalid-schemaのテストはどちらも、実質的に次しか確認していません。

assert result.status != "ok"
assert result.structured_output is None

したがって、次のどれでもPASSします。

  • timeout

  • 一時的なネットワーク障害

  • rate limit

  • model/backend障害

  • CLIの起動エラー

  • 本当にinvalid agent/schemaを拒否した場合

つまり、何らかの理由で失敗したことは分かりますが、対象のCLI接触面を検証したことにはなっていません。

推奨修正

少なくともoperational failureを除外してください。

assert result.status == "api_error"
assert result.reason_code == "nonzero_exit"
assert result.exit_code not in (None, 0)
assert result.structured_output is None

CLI version間で分類差を許容するなら、次のような意味分類でも構いません。

assert result.status in {"api_error", "partial_result"}
assert result.status not in {"timeout", "terminated"}
assert result.reason_code not in {"timeout", "sigterm"}

さらにinvalid-agent側のpromptは、default agentへ誤fallbackされた場合に成功してしまうよう、schema適合するexact payloadを返す指示にしてください。そうすれば、CLIが存在しないagent名を無視した場合は ok となり、テストが正しく失敗します。

証跡もpytestプロセスのexit codeだけでなく、adapterの次の値を記録すべきです。

adapter_status
adapter_reason_code
child_exit_code

新しい証跡形式は不要で、現在のテキストログへ3項目追加するだけで十分です。


P1-4 — --select の値を省略するとwrapperが終了しない

wrapperは現在、次の実装です。

--select)
  SELECT="${2:-}"
  shift 2
  ;;

引数が --select だけの場合、shift 2 は失敗しますが、set -e も明示的なエラー処理もありません。位置引数が残ったまま while [ $# -gt 0 ] が再開されるため、同じ分岐を繰り返します。

これは外部入力攻撃ではなく、手入力ミスで作業が止まる純粋なワークフロー障害です。

修正例

--select)
  if [ "$#" -lt 2 ] || [ -z "$2" ] || [[ "$2" == --* ]]; then
    echo "verify_run_retrospective_live_cli.sh: --select requires a non-empty expression" >&2
    exit 2
  fi
  SELECT="$2"
  shift 2
  ;;

併せて、存在しないselectorでpytestが「0 tests selected」を成功扱いしないことも確認してください。pytestの現行挙動では通常exit 5になりますが、wrapper testとして1ケース固定しておく方が安全です。


P1-5 — --disallowedTools のargv形状が公式例と異なり、live testでも意味論を検証していない

現在はdeny rule集合を一つのカンマ区切り文字列へ変換しています。

argv += [
    "--disallowedTools",
    ",".join(sorted(policy.denied_tools)),
]

一方、公式CLI referenceでは複数ruleを個別の値として渡す形式が示されています。現live testが証明しているのは「このargvを付けてもround-tripが完了した」ことだけで、各deny ruleが実際に解釈されたことではありません。 (Claude)

現時点でカンマ形式が必ず無効だと断定はできませんが、少なくとも公式のdocumented shapeへ合わせる方が安全です。

if policy is not None:
    argv += ["--disallowedTools", *sorted(policy.denied_tools)]

このPRで新しいsecurity live testを追加する必要はありません。実際のtool denial意味論は、すでに後続 #2239 が担当しています。ここでは、

  • documented argv shapeへ修正

  • mock testの期待argvを更新

  • PRのSafety Claimを「policyが有効と証明した」ではなく「deny valuesをCLIへ渡した」に縮小

までで十分です。


P2 — canonical schemaを追加したのに利用手順がまだplaceholderを要求する

run_retrospective.py は既にcanonical schema directoryを既定値として持っていますが、利用者向け SKILL.md のコマンド例は引き続き、

--schema-dir <observer/evaluator 用 JSON Schema ファイルの配置ディレクトリ>

を要求しています。今回のPRの価値は「既定production pathがschema不足で壊れていた状態を直すこと」でもあるため、このplaceholderを残すと利用者が再び不要なpath指定を行うことになります。

SKILL.md は現在のAllowed Paths外なので、これはマージblockerにはしません。ただし、最小のfollow-upとしてコマンド例から --schema-dir を削除するか、次のように実pathを示すべきです。

# 通常は省略。別schema assetを試す場合だけ指定する
--schema-dir .claude/skills/agent-retrospective/scripts/schemas

現設計で維持すべき点

以下は変更しない方がよいです。

  • claude_live を通常のrequired CIから除外し、operator-run smokeに限定する。

  • binary/authが利用不可の場合だけexit 77にする。

  • 正常系をnonce-boundな完全一致で判定する。

  • promptをstdinで渡す。

  • --no-session-persistence を使う。

  • result 回収時に、実際に渡したschemaへ再検証する。

  • 新しいgeneric runtime harnessやrequired GitHub Actions jobを作らない。

また、script用途だからという理由で --bare を追加してはいけません。公式仕様上、bare modeはprojectのskills・agents等を読み込まず、subscription OAuthも利用しないため、今回のproject-local custom agentとHOME-based subscription loginを壊します。現在の非bare構成が正しいです。 (Claude)

evaluation_result_v1.schema.json の candidate_records[] が単なるobjectまでしか制約せず、完全なcandidate検証を後段のPython validatorへ委ねている点も、現PRでは許容できます。完全schemaを二重埋め込みして保守負荷を増やすより、後続 #2239 のevaluator live smokeで実運用を確認する方が趣味開発として合理的です。


最小修正セット

マージ条件は次の範囲で十分です。

  1. subtype == "success" をfallback前の必須条件にする。

  2. fallback対象を「欠落またはnull」に限定し、string/list等は拒否する。

  3. fallback matrixのhermetic testを追加する。

  4. invalid-agent/schema testからtimeout等の無関係な失敗を除外する。

  5. wrapperの --select 引数不足を即時エラーにする。

  6. wrapperに git rev-parse HEAD 出力を追加する。

  7. 最終HEADでmock suiteとlive suiteを再実行する。

  8. PR本文の tested_head とcase別adapter結果を更新する。

  9. 可能なら --disallowedTools を公式の複数argv要素形式へ合わせる。

実行コマンドは既存の2本で足ります。

uv run --locked pytest \
  .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective.py \
  -q

.claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh

verdict: REQUEST_CHANGES
merge_ready: false

p0_blockers:

  • result subtypeを検証せず、schema適合resultテキストを非成功wrapperからokへ昇格できる
  • live evidenceのtested_headが現在のPR HEADと一致しない

bounded_p1_fixes:

  • fallback対象をmissing/nullへ限定
  • compatibility recoveryのhermetic matrix追加
  • invalid-agent/schema oracleの原因特定力を強化
  • --select欠損時の無限loop防止
  • --disallowedToolsをdocumented argv shapeへ整合

new_required_ci: false
new_generic_harness: false
new_security_framework: false
new_issue_required: false
recommended_disposition: fix_in_place

# 判定

REQUEST_CHANGES

実 Claude CLI を使って mock-only 検証の穴を発見し、result テキストを JSON Schema で再検証して回収する方向自体は妥当です。実際、Claude Code では structured_output が欠落または null になり、schema 適合 JSON が result に入る事例が報告されています。したがって、互換回収経路を削除して旧実装へ戻すべきではありません。 ([GitHub]1)

ただし現状は、非成功レスポンスを成功へ反転し得る adapter bugと、現HEADに束縛されていないlive証跡が残っています。この2点はマージ前に修正が必要です。


Blocker

P0-1 — subtype != success のレスポンスを fallback が ok へ昇格できる

invoke_agent() は現在、次の順序でレスポンスを処理しています。

  1. subprocess exit code
  2. JSON objectか
  3. is_error がtruthyか
  4. type == "result" か
  5. structured_output がdictか
  6. dictでなければ result を再parse・schema検証
  7. schemaに通れば status="ok"

しかし、subtype を一度も検証していません。そのため、例えば次のwrapperを返された場合、

{
  "type": "result",
  "subtype": "error_max_structured_output_retries",
  "is_error": false,
  "result": "{\"schema_version\":\"observer_result/v1\", ...}"
}

result 内のJSONがschemaに適合すれば、現実装は ok を返せます。現在の実装は、type と is_error だけを見た後、非dictの structured_output を無条件で fallback に流しています。

公式のStructured Outputs契約では、成功判定は少なくとも type == "result"、subtype == "success"、構造化値が存在することに依存します。error_max_structured_output_retries は失敗として扱う必要があります。 ([Claude]2)

必須修正

fallbackへ進む前に subtype を検証してください。

if payload.get("type") != "result":
    return AgentInvocationResult(
        status="malformed_output",
        structured_output=None,
        raw_stdout_excerpt=_stdout_excerpt(completed.stdout),
        exit_code=completed.returncode,
        reason_code="unexpected_wrapper_shape",
    )

subtype = payload.get("subtype")
if subtype != "success":
    return AgentInvocationResult(
        status="partial_result",
        structured_output=None,
        raw_stdout_excerpt=None,
        exit_code=completed.returncode,
        reason_code=f"result_subtype_not_success:{subtype or 'missing'}",
    )

そのうえで、互換回収を許す条件を次に限定するのが適切です。

_MISSING = object()
raw_structured_output = payload.get("structured_output", _MISSING)

if isinstance(raw_structured_output, dict):
    structured_output = raw_structured_output
elif raw_structured_output is _MISSING or raw_structured_output is None:
    structured_output = _structured_output_from_result_compat(
        payload,
        json_schema_path=request.json_schema_path,
    )
else:
    structured_output = None

つまり、

  • absent:互換回収可
  • null:互換回収可
  • dict:正規値として採用
  • string / list / number / bool:wrapper契約違反として拒否

としてください。


P0-2 — live証跡の tested_head がPR現HEADではない

PR本文のlive証跡は次のコミットに対するものです。

tested_head: 194ac42e25df08fb21d07e8b60abc794acb811cd

一方、現在のPR HEADは次です。

c180a5dff121affe74c142a9d17b9c8733edc3ef

PRはその後mainをマージした2コミット構成になっています。Issue #2301のAC8は、証跡にtested HEADを記録することを明示的に要求しています。したがって、現在の証跡では現HEADがlive検証済みとは判定できません。

required CIが現HEADで成功していても、claude_live は意図的に通常CIから除外されているため代替証明にはなりません。

必須修正

最終修正後のHEADで以下を再実行し、PR本文の証跡を更新してください。

git rev-parse HEAD

.claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh

また、手作業でHEADを書き写す運用は再発しやすいため、thin wrapper自身に次だけ追加するのが合理的です。

TESTED_HEAD="$(git -C "$REPO_ROOT" rev-parse HEAD)"
echo "verify_run_retrospective_live_cli.sh: tested HEAD: $TESTED_HEAD"

新しいreceipt schema、artifact collector、GitHub Actions jobは不要です。


P1 findings

P1-1 — fallbackの説明と実装条件が一致していない

helperのdocstringとPR本文は「structured_output が欠落した場合のみ fallback」と説明しています。ところが呼び出し側は、

if not isinstance(structured_output, dict):

なので、欠落だけでなく次の全てを回収対象にします。

null
string
array
number
boolean

さらにコメントは「custom-subagent invocation は structured_output をnever populateする」と断定しています。しかし公式契約では本来 structured_output が正規経路であり、実際の不具合報告でも「欠落」と「null」の双方が存在します。「常に存在しない」という恒久的CLI契約として固定するのは不正確です。 ([GitHub]1)

推奨修正

コメントを次のような観測事実へ縮小してください。

Claude Code 2.1.241 の project custom-agent invocation で、subtype: success にもかかわらず structured_output が欠落し、schema適合JSONが result に入る挙動を観測した。正規のdict値を優先し、欠落またはnullの場合に限って互換回収する。

また、fallback という名前は #2301 の「fallback経由の成功は禁止」および後続 #2239 の「fallbackなし」と衝突します。後者はruntime/profile fallbackを指していますが、同じ語を使うと後続エージェントが誤判定します。

例えば次へ改名する方が明確です。

_structured_output_from_result_compat()

あるいは、

_recover_structured_output_from_success_result()

P1-2 — 新しいproduction fallbackに対するhermetic回帰テストが足りない

既存mock testには、

  • 正常な structured_output の抽出
  • result が単なる文章の場合の拒否
  • wrapper shape不正の拒否

はあります。しかし、今回追加された主要productionロジックについて、次が固定されていません。

最低限、以下の6ケースを追加すべきです。

Case 期待値
subtype=success、structured_output 欠落、result が生JSON ok
subtype=success、structured_output=null、fenced JSON ok
subtype!=success、result がschema適合 必ず非ok
structured_output がstring/list、result がschema適合 必ず非ok
dictの structured_output と異なる result が共存 dictを優先
result がJSONだがschema不適合 malformed_output

特に3番目はP0-1の再発防止になります。

これは既存test fileへの数件のfixture追加で足り、live harnessの拡張は不要です。


P1-3 — 異常系live testが「目的のエラー」を証明していない

invalid-agentとinvalid-schemaのテストはどちらも、実質的に次しか確認していません。

assert result.status != "ok"
assert result.structured_output is None

したがって、次のどれでもPASSします。

  • timeout
  • 一時的なネットワーク障害
  • rate limit
  • model/backend障害
  • CLIの起動エラー
  • 本当にinvalid agent/schemaを拒否した場合

つまり、何らかの理由で失敗したことは分かりますが、対象のCLI接触面を検証したことにはなっていません。

推奨修正

少なくともoperational failureを除外してください。

assert result.status == "api_error"
assert result.reason_code == "nonzero_exit"
assert result.exit_code not in (None, 0)
assert result.structured_output is None

CLI version間で分類差を許容するなら、次のような意味分類でも構いません。

assert result.status in {"api_error", "partial_result"}
assert result.status not in {"timeout", "terminated"}
assert result.reason_code not in {"timeout", "sigterm"}

さらにinvalid-agent側のpromptは、default agentへ誤fallbackされた場合に成功してしまうよう、schema適合するexact payloadを返す指示にしてください。そうすれば、CLIが存在しないagent名を無視した場合は ok となり、テストが正しく失敗します。

証跡もpytestプロセスのexit codeだけでなく、adapterの次の値を記録すべきです。

adapter_status
adapter_reason_code
child_exit_code

新しい証跡形式は不要で、現在のテキストログへ3項目追加するだけで十分です。


P1-4 — --select の値を省略するとwrapperが終了しない

wrapperは現在、次の実装です。

--select)
  SELECT="${2:-}"
  shift 2
  ;;

引数が --select だけの場合、shift 2 は失敗しますが、set -e も明示的なエラー処理もありません。位置引数が残ったまま while [ $# -gt 0 ] が再開されるため、同じ分岐を繰り返します。

これは外部入力攻撃ではなく、手入力ミスで作業が止まる純粋なワークフロー障害です。

修正例

--select)
  if [ "$#" -lt 2 ] || [ -z "$2" ] || [[ "$2" == --* ]]; then
    echo "verify_run_retrospective_live_cli.sh: --select requires a non-empty expression" >&2
    exit 2
  fi
  SELECT="$2"
  shift 2
  ;;

併せて、存在しないselectorでpytestが「0 tests selected」を成功扱いしないことも確認してください。pytestの現行挙動では通常exit 5になりますが、wrapper testとして1ケース固定しておく方が安全です。


P1-5 — --disallowedTools のargv形状が公式例と異なり、live testでも意味論を検証していない

現在はdeny rule集合を一つのカンマ区切り文字列へ変換しています。

argv += [
    "--disallowedTools",
    ",".join(sorted(policy.denied_tools)),
]

一方、公式CLI referenceでは複数ruleを個別の値として渡す形式が示されています。現live testが証明しているのは「このargvを付けてもround-tripが完了した」ことだけで、各deny ruleが実際に解釈されたことではありません。 ([Claude]3)

現時点でカンマ形式が必ず無効だと断定はできませんが、少なくとも公式のdocumented shapeへ合わせる方が安全です。

if policy is not None:
    argv += ["--disallowedTools", *sorted(policy.denied_tools)]

このPRで新しいsecurity live testを追加する必要はありません。実際のtool denial意味論は、すでに後続 #2239 が担当しています。ここでは、

  • documented argv shapeへ修正
  • mock testの期待argvを更新
  • PRのSafety Claimを「policyが有効と証明した」ではなく「deny valuesをCLIへ渡した」に縮小

までで十分です。


P2 — canonical schemaを追加したのに利用手順がまだplaceholderを要求する

run_retrospective.py は既にcanonical schema directoryを既定値として持っていますが、利用者向け SKILL.md のコマンド例は引き続き、

--schema-dir <observer/evaluator 用 JSON Schema ファイルの配置ディレクトリ>

を要求しています。今回のPRの価値は「既定production pathがschema不足で壊れていた状態を直すこと」でもあるため、このplaceholderを残すと利用者が再び不要なpath指定を行うことになります。

SKILL.md は現在のAllowed Paths外なので、これはマージblockerにはしません。ただし、最小のfollow-upとしてコマンド例から --schema-dir を削除するか、次のように実pathを示すべきです。

# 通常は省略。別schema assetを試す場合だけ指定する
--schema-dir .claude/skills/agent-retrospective/scripts/schemas

現設計で維持すべき点

以下は変更しない方がよいです。

  • claude_live を通常のrequired CIから除外し、operator-run smokeに限定する。
  • binary/authが利用不可の場合だけexit 77にする。
  • 正常系をnonce-boundな完全一致で判定する。
  • promptをstdinで渡す。
  • --no-session-persistence を使う。
  • result 回収時に、実際に渡したschemaへ再検証する。
  • 新しいgeneric runtime harnessやrequired GitHub Actions jobを作らない。

また、script用途だからという理由で --bare を追加してはいけません。公式仕様上、bare modeはprojectのskills・agents等を読み込まず、subscription OAuthも利用しないため、今回のproject-local custom agentとHOME-based subscription loginを壊します。現在の非bare構成が正しいです。 ([Claude]4)

evaluation_result_v1.schema.json の candidate_records[] が単なるobjectまでしか制約せず、完全なcandidate検証を後段のPython validatorへ委ねている点も、現PRでは許容できます。完全schemaを二重埋め込みして保守負荷を増やすより、後続 #2239 のevaluator live smokeで実運用を確認する方が趣味開発として合理的です。


最小修正セット

マージ条件は次の範囲で十分です。

  1. subtype == "success" をfallback前の必須条件にする。
  2. fallback対象を「欠落またはnull」に限定し、string/list等は拒否する。
  3. fallback matrixのhermetic testを追加する。
  4. invalid-agent/schema testからtimeout等の無関係な失敗を除外する。
  5. wrapperの --select 引数不足を即時エラーにする。
  6. wrapperに git rev-parse HEAD 出力を追加する。
  7. 最終HEADでmock suiteとlive suiteを再実行する。
  8. PR本文の tested_head とcase別adapter結果を更新する。
  9. 可能なら --disallowedTools を公式の複数argv要素形式へ合わせる。

実行コマンドは既存の2本で足ります。

uv run --locked pytest \
  .claude/skills/agent-retrospective/scripts/tests/test_run_retrospective.py \
  -q

.claude/skills/agent-retrospective/scripts/tests/verify_run_retrospective_live_cli.sh
verdict: REQUEST_CHANGES
merge_ready: false

p0_blockers:
  - result subtypeを検証せず、schema適合resultテキストを非成功wrapperからokへ昇格できる
  - live evidenceのtested_headが現在のPR HEADと一致しない

bounded_p1_fixes:
  - fallback対象をmissing/nullへ限定
  - compatibility recoveryのhermetic matrix追加
  - invalid-agent/schema oracleの原因特定力を強化
  - --select欠損時の無限loop防止
  - --disallowedToolsをdocumented argv shapeへ整合

new_required_ci: false
new_generic_harness: false
new_security_framework: false
new_issue_required: false
recommended_disposition: fix_in_place

Test and others added 2 commits August 24, 2026 19:58
- invoke_agent(): subtype!="success" を result/structured_output 復元より前に
  gate し、error_max_structured_output_retries 等の非成功応答が status="ok" へ
  昇格するのを防ぐ (P0-1)
- structured_output の compat 復元は absent/None のみで発火するよう限定し、
  present-but-wrong-type (string/list等) は missing_structured_output のまま
  にする。_structured_output_from_result_fallback を
  _structured_output_from_result_compat へ改名しdocstringを観測事実ベースに
  修正 (P1-1)
- build_agent_invocation_argv(): --disallowedTools を comma-joined 文字列でな
  く個別 argv 要素として渡す (P1-5)
- verify_run_retrospective_live_cli.sh: 常に tested HEAD を出力し (P0-2)、
  --select の値欠落による無限ループ/未検証を防止 (P1-4)
- test_run_retrospective.py: P0-1 回帰防止の6ケースを追加、--disallowedTools
  argv 形状アサーションを更新
- test_run_retrospective_live_cli.py: invalid-agent-name テストの prompt を
  fallback-default-agent 誤PASS対策で強化、両live testにtimeout/terminated
  除外assertionとadapter_status/reason_code/exit_code の evidence print を追加
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

検証・修正: agent-retrospective の production Agent invocation adapter を実 Claude CLI で確認する

1 participant