Skip to content

実装: Claude Code 非同期起動 SubAgent の identity evidence を hook channel からも抽出する (#2021) - #2025

Merged
squne121 merged 1 commit into
mainfrom
worktree-issue-2021-spawn-identity-hook-channel
Aug 8, 2026
Merged

squne121 merged 1 commit into
mainfrom
worktree-issue-2021-spawn-identity-hook-channel

Conversation

@squne121

@squne121 squne121 commented Aug 8, 2026

Copy link
Copy Markdown
Owner

概要

Claude Code の Agent tool が返す非同期起動エンベロープでも child identity evidence を
取りこぼさないように、scripts/agent-ops/run_worktree_agent_runtime_smoke.py の抽出経路を
hook channel 対応にする。

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

背景

Issue #2013 の research(artifacts/claude-code-spawn-observability-research/、
Claude Code 2.1.225、control 15 trial + production 15 trial)で、
Agent tool の tool_use_result に 2 形状があることが決定論的に確認された。

形状 status agentId agentType
同期完了型 completed あり あり
非同期起動型 async_launched(isAsync: true) あり 無し

extract_claude_child_agent_type() は tool_use_result.agentType とテキスト断片のみを読むため、
非同期起動型では常に None を返していた。その結果 agent_type_identity_verified が False になり、
spawn が完全に観測できている run でも native_spawn_event_observed が False に落ち、
spawn_not_observed / validation_failed として非決定的に失敗していた。

一方、同じ stream の hook channel には runtime 自身が返した identity evidence が存在する。
SubagentStart / SubagentStop hook の公式 payload(agent_id / agent_type)と、
SubagentStart の hook_name 接尾辞 "<HookEvent>:<agent_type>" である。
research の 30 trial すべてで hook channel の agent_id は tool_use_result.agentId と完全一致し、
agent type も要求値と一致していた。timeout と system/api_retry は 0 件で、transient failure ではない。

変更内容

  • extract_claude_hook_agent_identity() を追加。hook lifecycle event から
    agent_id / agent_type と、その provenance(hook_payload / hook_name)を抽出する。
  • extract_claude_child_agent_type_with_source() を追加。agent type と観測 channel を組で返す。
  • extract_claude_child_agent_type() は tool_use_result を優先し、
    そこで得られない場合のみ hook channel に fallback する。
  • classify_claude_spawn_launch_mode() を追加。async_launched / completed / None を分類する。
  • extract_claude_child_session_id() の parent_session_id 欠落時の短絡を解消する。
    従来は parent id が無いと stdout に child agentId があっても探索せず即 None を返していた。
    parent id が本当に必要なのは file-based fallback だけなので、guard をそこへ移した。
  • schema summary に child_agent_type_source と child_spawn_launch_mode を記録する。

evidence 水準を下げていないこと

  • 両 channel とも証跡が無ければ従来どおり None に fail-close する。
  • 要求 agent type の代入や caller の self-report 採用は行わない。
    抽出関数は stdout 以外の引数を取らず、構造的に期待値を反映できない。
  • hook を identity evidence の唯一の正本にしない。tool_use_result が常に優先される。
    upstream の [BUG] SubagentStart/SubagentStop unreliable for settings.json hooks — cascade failures break agent lifecycle management anthropics/claude-code#27755 は
    hook 未発火を報告する community bug report("Closed as not planned")であり公式契約ではないため、
    hook 単独依存を避ける設計を維持している。
  • SubagentStop のような接尾辞なし hook_name を identity evidence と誤認しない。
  • PreToolUse 等の lifecycle 外 hook event は identity evidence として扱わない。
  • agent type 不一致は従来どおり fail-close する(回帰テストあり)。

実測 raw evidence による検証

Issue #2013 が記録した 30 trial の raw stdout(artifacts/.../raw/)に対して
修正後の抽出経路を適用した結果は次のとおりである。

指標 修正前 修正後
native_spawn_event_observed 10 / 30 30 / 30

内訳は async_launched 20 件(hook_payload から復元)、
completed 10 件(従来どおり tool_use_result から取得)である。

検証結果

AC Verification Command 結果
AC1-AC7 uv run pytest scripts/agent-ops/tests/test_claude_spawn_identity_evidence.py PASS(33 passed)
AC8 uv run pytest scripts/agent-ops/tests/ PASS(194 passed)

リポジトリ必須検証:

コマンド 結果
pnpm typecheck PASS
pnpm lint PASS(ESLint: No issues found)
pnpm test PASS(1864 tests。1 件の timeout flake を単体再実行で PASS 確認済み)
pnpm build PASS
uv run --locked ruff check .claude/scripts scripts schemas .claude/skills PASS
uv run pytest artifacts/claude-code-spawn-observability-research/tests/ PASS(114 passed。#2013 の contract test に回帰なし)

pnpm test で tests/retro-live-verification.test.ts の 1 件が 5000ms の
timeout で落ちたが、同ファイル単体では PASS する負荷起因の flake であり、
本 PR は Python のみの変更のため因果関係はない。

スコープ境界

  • Allowed Paths(scripts/agent-ops/run_worktree_agent_runtime_smoke.py と
    scripts/agent-ops/tests/)の外を変更していない。
  • run_agent_provider_route_smoke.py の failure ladder と
    _is_transient_infrastructure_candidate() は変更していない。
    Issue 調査: Claude Code custom SubAgent の spawn observability 非決定性を診断する #2013 の retry-policy-assessment.md は現行設計の維持を結論している。
  • 非同期起動時に親セッションが子の完了を待たず marker / delegation evidence が
    materialize しない事象は完了待ち semantics の別課題であり、本 PR では扱わない。
  • repo-tracked .claude/settings.json / .claude/hooks/** を変更していない。
    本変更は既に stream-json に現れる hook event を読むだけで、hook 登録を要求しない。
  • Codex CLI lane の identity 抽出経路は変更していない
    (provenance ラベルの記録のみ追加している)。

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

…抽出する (#2021)

Claude Code の 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 を返し、spawn が完全に観測できている run でも
native_spawn_event_observed が False に落ちていた。

runtime は同じ stream に identity evidence を出している。
SubagentStart / SubagentStop hook event の公式 payload(agent_id / agent_type)と、
SubagentStart の hook_name 接尾辞 "<HookEvent>:<agent_type>" である。

- extract_claude_hook_agent_identity() を追加し、hook channel から identity を抽出する
- extract_claude_child_agent_type_with_source() を追加し、evidence の provenance を返す
- extract_claude_child_agent_type() は tool_use_result を優先し、hook channel を fallback にする
- classify_claude_spawn_launch_mode() を追加し、launch mode を記録する
- extract_claude_child_session_id() の parent_session_id 欠落による短絡を解消する
- schema summary に child_agent_type_source / child_spawn_launch_mode を記録する

evidence 水準は下げていない。両 channel とも証跡が無ければ従来どおり None に fail-close し、
要求 agent type の代入や self-report の採用は行わない。
hook を唯一の正本にもしない(tool_use_result が常に優先される)。

Issue #2013 の実測 raw evidence 30 trial に対して検証したところ、
native_spawn_event_observed は 10/30 から 30/30 になった。
内訳は async_launched 20 件(hook_payload から復元)、completed 10 件(tool_use_result から取得)。

Closes #2021

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

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Preview URL

✅ verified live assets: https://squne121.github.io/loop-protocol/pr-2025/

The preview will be removed when this PR is closed.

@squne121
squne121 merged commit 941d5eb into main Aug 8, 2026
21 of 22 checks passed
@squne121
squne121 deleted the worktree-issue-2021-spawn-identity-hook-channel branch August 8, 2026 06:21
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 非同期起動 SubAgent の identity evidence を hook channel からも抽出する

1 participant