Repository navigation
実装: Claude Code 非同期起動 SubAgent の identity evidence を hook channel からも抽出する (#2021) - #2025
Merged
Merged
Conversation
…抽出する (#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]>
Contributor
Preview URL✅ verified live assets: https://squne121.github.io/loop-protocol/pr-2025/ The preview will be removed when this PR is closed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
Claude Code の
Agenttool が返す非同期起動エンベロープでも 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)で、Agenttool のtool_use_resultに 2 形状があることが決定論的に確認された。statusagentIdagentTypecompletedasync_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/SubagentStophook の公式 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 をそこへ移した。
child_agent_type_sourceとchild_spawn_launch_modeを記録する。evidence 水準を下げていないこと
Noneに fail-close する。抽出関数は
stdout以外の引数を取らず、構造的に期待値を反映できない。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 として扱わない。実測 raw evidence による検証
Issue #2013 が記録した 30 trial の raw stdout(
artifacts/.../raw/)に対して修正後の抽出経路を適用した結果は次のとおりである。
native_spawn_event_observed内訳は
async_launched20 件(hook_payloadから復元)、completed10 件(従来どおりtool_use_resultから取得)である。検証結果
uv run pytest scripts/agent-ops/tests/test_claude_spawn_identity_evidence.pyuv run pytest scripts/agent-ops/tests/リポジトリ必須検証:
pnpm typecheckpnpm lintpnpm testpnpm builduv run --locked ruff check .claude/scripts scripts schemas .claude/skillsuv run pytest artifacts/claude-code-spawn-observability-research/tests/pnpm testでtests/retro-live-verification.test.tsの 1 件が 5000ms のtimeout で落ちたが、同ファイル単体では PASS する負荷起因の flake であり、
本 PR は Python のみの変更のため因果関係はない。
スコープ境界
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は現行設計の維持を結論している。materialize しない事象は完了待ち semantics の別課題であり、本 PR では扱わない。
.claude/settings.json/.claude/hooks/**を変更していない。本変更は既に stream-json に現れる hook event を読むだけで、hook 登録を要求しない。
(provenance ラベルの記録のみ追加している)。
本 PR は Claude Code により生成された。
🤖 Generated with Claude Code