Skip to content

調査: Claude Code custom SubAgent の spawn observability 非決定性を診断する (#2013) - #2022

Merged
squne121 merged 3 commits into
mainfrom
worktree-issue-2013-spawn-observability-research
Aug 8, 2026
Merged

squne121 merged 3 commits into
mainfrom
worktree-issue-2013-spawn-observability-research

Conversation

@squne121

@squne121 squne121 commented Aug 8, 2026 •

Copy link
Copy Markdown
Owner

概要

Issue #2013(research-only)の成果物を artifacts/claude-code-spawn-observability-research/ に追加する。
PR #2005 / Issue #1886 の impl-review-loop で観測された claude_code runtime の非決定的
spawn_not_observed / validation_failed について、lifecycle checkpoint 単位で原因を切り分けた。

production code(scripts/agent-ops/run_worktree_agent_runtime_smoke.py、
scripts/agent-ops/run_agent_provider_route_smoke.py、repo-tracked .claude/settings.json、
.claude/hooks/**)は一切変更していない。

差分は原則として Allowed Paths(artifacts/claude-code-spawn-observability-research/)内だが、
Scope Delta として .github/ci/python-test-plan.json に 1 行だけ target を追加している。
AC が要求する新規 pytest ディレクトリを required check である python-test が拾えるようにするための
最小限の登録であり、これを行わないと ci_test_selection/v1 の
uncovered_changed_test_files gate が required CI を fail させる。
判断の詳細は本 PR の CI_TEST_PERFORMANCE_DECISION_V1 コメントに記録した。

本 PR のマージをもって対象 Issue を完了として閉じる。Closes #2013

調査方法(research method)

事前に trial-plan.json として試行条件を凍結し、その SHA256 digest を全 record に刻んだ上で
固定試行数を実行した。結果を見てから route 比率・prompt・timeout・max-turns・agent 定義を
変更していない。

  • control lane(15 trial 固定): Claude Code の --agents で inline 定義した最小 custom
    subagent spawn-probe を spawn する。外部 provider・Serena MCP・GitHub retrieval を一切必要としない。
    research artifact 内に生成した session-local --settings で SubagentStart / SubagentStop
    hook を no-op logger として構成し、公式 hook payload を stream-json から回収した。
  • production lane(15 trial 固定): current main の実 codebase-investigator /
    web-researcher を、実 build_route_prompt() と実 gemini sentinel stub で駆動した。
    3 route を宣言順にラウンドロビンして各 5 trial。

分類は production コードを read-only import して算出しており、failure_class は
_run_route_once() の評価順序を忠実に複製している。diagnostic_cause はそれとは独立に
raw lifecycle checkpoint のみから算出する。全 trial の raw stdout/stderr を raw/ に保存したので、
派生値はすべて独立に再計算できる。

実行条件

項目 値
Claude Code version 2.1.225 (Claude Code)
actual tested SHA 9eca2f0074552a3e0687b0c81ee94b62122890a0
historical baseline SHA(PR #2005 merge commit) 28394e226533cd59cdfc0f55602ac65e389a6600
control lane trial 数 15(有効 15 / 除外 0)
production lane trial 数 15(有効 15 / 除外 0)

baseline から tested SHA までの差分は #2010(Graphify CLI advisory pilot)の 1 commit のみで、
調査対象の 2 スクリプトは変更されていない。provenance は code-analysis.md に記録した。

分布サマリ(distribution summary)

lane 別の status 分布は次のとおりである。

lane pass fail total
control 4 11 15
production 2 13 15

lane 別 diagnostic_cause(none は pass):

lane diagnostic_cause count
control none 4
control tool_result_identity_not_observed 11
production none 2
production tool_result_identity_not_observed 9
production request_validation_failed 3
production delegation_wrapper_failed 1

identity evidence の channel 比較:

lane tool_result channel の agentType hook channel の agent_type agentId 一致 total
control 4 15 15 15
production 6 15 15 15

wall-clock timeout は 0 件、system/api_retry 観測も 0 件だった。

診断結論(diagnostic conclusion)

結論カテゴリは repo_observability_defect である。

Claude Code 2.1.225 の Agent tool が返す tool_use_result には 2 形状がある。

  • 同期完了型: status: "completed"、agentType あり
  • 非同期起動型: isAsync: true, status: "async_launched"、agentId はあるが agentType が無い

extract_claude_child_agent_type() は tool_use_result.agentType とテキスト断片のみを読むため、
非同期起動型では常に None を返す。その結果 agent_type_identity_verified が False となり、
spawn が実際に成立している run でも native_spawn_event_observed が False に落ちる。

一方、同じ run の hook channel には runtime 自身が返した identity evidence が存在する。
SubagentStart / SubagentStop hook payload は公式スキーマどおり agent_id / agent_type を持ち、
SubagentStart の hook_name は "SubagentStart:<agent_type>" 形式で agent type を含む。
agentType が欠落した 20 trial すべてで hook channel は要求どおりの agent_type を返し、
agentId は全 30 trial で両 channel 完全一致した。

すなわち runtime は identity evidence を提供しており、欠けているのは repo 側の抽出経路のみである。
upstream runtime の契約違反でも、infrastructure の transient failure でも、
model orchestration の問題でも、downstream route の失敗でもない。

spawn_not_observed と validation_failed に分かれるのは spawn の有無ではなく、
_run_route_once() の順 4(harness 非ゼロ終了)が順 5(spawn evidence)より先に評価されるかで決まる。
外側の failure_class から spawn の有無を推測してはならない。
なお Issue 本文が 8 段階として記載していた評価順序は current main では 9 段階
(route_evidence_schema_mismatch を含む)であり、この drift も code-analysis.md に記録した。

retry policy 結論

retry_policy_verdict: keep_excluded

_is_transient_infrastructure_candidate() が claude_code + spawn_not_observed を
bounded single retry の対象外としている現行設計は、観測データと整合するため 維持する。

観測された spawn_not_observed はすべて決定論的な抽出経路の欠落であり、
timeout も system/api_retry も 1 件も観測されていない。
再実行するとエンベロープ形状の分岐に応じて成功することはあるが、それは根本原因の解消ではない。
「再実行したら通った」だけを transient 判定の根拠にはしていない。

ただし現行コードのコメントが述べる理由(Claude の spawn evidence は in-memory stdout にあるため
miss は transient ではない)は、実際に観測した機序とは異なる。結論は正しいが根拠は更新されるべきである。

既存契約テスト test_claude_spawn_not_observed_is_not_transient_candidate は変更不要である。
silent retry・2 回以上の追加 retry・成功するまでの反復 retry はいずれも提案していない。

追従実装 Issue(follow-up implementation issue)

#2021 を起票した。本 PR は research-only のため、#2021 を閉じる keyword は本文に含めていない。

run_worktree_agent_runtime_smoke.py の identity evidence 抽出経路を hook channel 対応にすること、
および extract_claude_child_session_id() が parent_session_id 欠落時に stdout 探索自体を
スキップする短絡を解消することを扱う。#2013 は research-only のため本 PR では実装しない。

非同期起動時に親セッションが子の完了を待たず marker / delegation evidence が materialize しない
事象は、完了待ち semantics の別課題として conclusion.md に観測事実のみ記録し、
#2021 のスコープには含めていない。

スコープ境界

検証結果(全 verification)

Issue #2013 の Verification Commands はすべて baseline-expect: fail である。
origin/main には artifacts/claude-code-spawn-observability-research/ が存在せず
(git ls-tree origin/main -- artifacts/claude-code-spawn-observability-research/ が空)、
5 コマンドすべてが baseline で fail することを確認した上で成果物を完成させた。
false-green のために assertion を弱めていない。

AC Verification Command 結果
AC1 uv run pytest artifacts/claude-code-spawn-observability-research/tests/test_code_analysis_contract.py PASS(46 passed)
AC2 uv run pytest artifacts/claude-code-spawn-observability-research/tests/test_reproduction_log_contract.py PASS(32 passed)
AC3 uv run pytest artifacts/claude-code-spawn-observability-research/tests/test_reproduction_summary_contract.py PASS(16 passed)
AC4 uv run pytest artifacts/claude-code-spawn-observability-research/tests/test_retry_policy_assessment_contract.py PASS(10 passed)
AC5 uv run pytest artifacts/claude-code-spawn-observability-research/tests/test_conclusion_contract.py PASS(10 passed)
全体 uv run pytest artifacts/claude-code-spawn-observability-research/tests/ PASS(114 passed)

リポジトリ必須検証:

コマンド 結果
pnpm typecheck PASS
pnpm lint PASS(ESLint: No issues found)
pnpm test PASS(108 files / 1864 tests)
pnpm build PASS
uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_model_routing.py PASS(21 passed)
uv run ruff check artifacts/claude-code-spawn-observability-research/ PASS
uv run python3 scripts/ci/check_python_invocation_policy.py PASS(0 violations)
pnpm lint:docs PASS(markdownlint 0 errors / textlint / roadmap refs)
uv run pytest scripts/ci/tests/test_python_test_plan.py PASS(55 passed)
変更した 4 つの Markdown への validate_japanese_content.py PASS(4 ファイルとも exit 0)

GitHub Actions の required check(typecheck / lint / test / build / python-test /
validate-generated-artifact)はすべて green である。
component-vrt-report は required check ではなく、branch 分岐元の main(9eca2f00)および
その前 2 commit でも同じ job が failure であることを確認した既存の failure であるため、
本 PR では対応しない(VRT を通すためのスコープ外変更を行わない)。

contract test は artifact の散文を読むだけでなく、引用した file:line が実際の production source の
該当条件を指しているか、既存テスト名が実在するか、summary の各数値が raw JSONL から再計算した値と
一致するかまで相互検証している。

本 PR は Claude Code により生成された。
🤖 Generated with Claude Code

control lane 15 trial / production lane 15 trial の固定試行で、
claude_code runtime の spawn_not_observed / validation_failed を
lifecycle 12 checkpoint 単位で切り分けた research artifact を追加する。

結論カテゴリは repo_observability_defect。
Claude Code 2.1.225 の Agent tool は status: "async_launched" の
非同期起動エンベロープを返すことがあり、その形状には agentType が無い。
extract_claude_child_agent_type() は tool_use_result.agentType のみを読むため
None を返し、native_spawn_event_observed が spawn 成立時にも False に落ちる。
同じ run の hook channel には runtime が返した agent_type が存在し、
agentId は両 channel で 30/30 trial 完全一致する。

timeout と system/api_retry は 0 件であり transient failure ではないため、
_is_transient_infrastructure_candidate() が claude_code + spawn_not_observed を
bounded retry 対象外とする現行設計は維持する。

production code は一切変更していない。追従する修正は #2021 で扱う。

Closes #2013

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

github-actions Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

Preview removed (PR #2022 closed).

squne121 and others added 2 commits August 8, 2026 14:46
markdownlint MD040 が retry-policy-assessment.md / conclusion.md の
言語指定なし fenced code block を検出したため、build_assessments.py が
yaml 言語付きの fence を生成するようにする。

Co-Authored-By: Claude Sonnet 5 <[email protected]>
ci_test_selection/v1 の uncovered_changed_test_files gate が、新規追加した
5 本の contract test を「plan に未登録」として検出し、required check の
python-test(python-test-core 経由)を fail させていた。

Scope Delta: Issue #2013 の Allowed Paths は
artifacts/claude-code-spawn-observability-research/ のみだが、
新規 pytest ディレクトリを .github/ci/python-test-plan.json の targets に
登録しないと required CI が通らないため、1 行の target 追加のみを行う。
Issue #2013 が変更を禁じている production code
(run_worktree_agent_runtime_smoke.py / run_agent_provider_route_smoke.py /
.claude/settings.json / .claude/hooks/**)には一切触れていない。

あわせて日本語比率ゲート(PR Body Japanese Check の changed Markdown 検査)で
不足していた prose block に日本語を追加する。

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

squne121 commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

CI / テストレーン判断の証跡

.github/ci/python-test-plan.json を 1 行変更したため、ci-test-performance skill の
要求に従って判断内容を記録する。

CI_TEST_PERFORMANCE_DECISION_V1:
  schema: CI_TEST_PERFORMANCE_DECISION_V1
  issue_number: 2013
  pr_number: 2022
  decision_scope: ci_change
  changed_paths:
    - .github/ci/python-test-plan.json
    - artifacts/claude-code-spawn-observability-research/
  lane_classification:
    fast_static:
      applicable: true
      evidence:
        - "markdownlint MD040 が生成 Markdown の fence 言語欠落を検出したため修正した"
      required_commands:
        - pnpm lint
        - pnpm lint:docs
        - uv run --locked ruff check .claude/scripts scripts schemas .claude/skills
    python_unit:
      applicable: true
      evidence:
        - "新規 contract test 5 本を artifacts/claude-code-spawn-observability-research/tests/ に追加した"
        - "ci_test_selection/v1 の uncovered_changed_test_files gate が 5 件を未登録として検出した"
      required_commands:
        - uv run pytest artifacts/claude-code-spawn-observability-research/tests/
        - uv run pytest scripts/ci/tests/test_python_test_plan.py
    contract_artifact:
      applicable: true
      evidence:
        - "python-test-plan SSOT に target を追加し、pytest 実行と ci_test_selection/v1 の対象を一致させた"
      required_commands:
        - uv run python3 scripts/ci/python_test_plan.py --emit scope-argv --check-paths --format json
    integration:
      applicable: false
      evidence:
        - "runtime 実行は research harness 内で完結し、CI integration lane を変更していない"
      required_commands: []
  baseline_inputs:
    ci_runtime_baseline_v1_available: false
    run_count: 0
    p50_p95_ready: false
  artifact_consistency:
    ci_test_selection_v1_checked: true
    missing_pytest_args: []
  risk_flags:
    - "追加した contract test は artifacts/ 配下にあるが、CI の pytest lane は plan の targets を正本とするため登録が必要である"
    - "テストは decision/ledger の静的検証のみで、live runtime 起動を伴わないため lane 実行時間への影響は小さい"
  reviewer_gate:
    approve_allowed: true
    required_evidence:
      - CI_CHECK_RUN_SCOPED
  follow_up_required: []

Scope Delta の説明

Issue #2013 の Allowed Paths は artifacts/claude-code-spawn-observability-research/ のみである。
.github/ci/python-test-plan.json への 1 行追加は、AC が要求する新規 pytest ディレクトリを
required check である python-test が拾えるようにするための最小限の登録であり、
Issue #2013 が変更を禁じている production code
(run_worktree_agent_runtime_smoke.py / run_agent_provider_route_smoke.py /
repo-tracked .claude/settings.json / .claude/hooks/**)には一切触れていない。

component-vrt-report について

component-vrt-report job は required status check ではなく、
失敗内容は .vitest-attachments の hidden-file upload round trip
(negative-control-download が生成されない)であり、本 PR の差分と無関係である。
本 PR は VRT を通すためのスコープ外変更を行わない。

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.

調査: Claude Code custom SubAgent の spawn observability 非決定性を診断する

1 participant