Skip to content

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

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "なし(単独改善)"
goal_ref: "Claude Code の非同期起動 SubAgent でも identity evidence を失わないように、run_worktree_agent_runtime_smoke.py の child identity 抽出経路を hook channel 対応にする"
change_kind: implementation

Parent Issue

なし(単独改善)

Outcome

scripts/agent-ops/run_worktree_agent_runtime_smoke.py の child identity 抽出経路が、
Claude Code の Agent tool が返す 非同期起動エンベロープ(status: "async_launched"、
agentType フィールドを含まない)でも runtime 提供の identity evidence を取りこぼさない状態。
native_spawn_event_observed が、spawn が実際に成立している run で False に落ちなくなる。
evidence の出所(provenance)が記録され、evidence が皆無の場合は従来どおり fail-closed で None を返す。

背景(Issue #2013 の evidence)

Issue #2013 の research(artifacts/claude-code-spawn-observability-research/、
Claude Code 2.1.225、tested SHA 9eca2f0074552a3e0687b0c81ee94b62122890a0、control 15 trial + production 15 trial)で
次が決定論的に確認された。

  • 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 になり、
    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 を含む。
  • tool_use_result.agentType が欠落した 17 trial すべてで、hook channel は
    正しい agent_type を返し、agentId は両 channel で完全一致した。
    wall-clock timeout と system/api_retry は 0 件であり、transient failure ではない。

すなわち runtime は identity evidence を提供しており、欠落しているのは repo 側の抽出経路のみである。
Issue #2013 の結論カテゴリは repo_observability_defect である。

In Scope

  • extract_claude_child_agent_type() を、tool_use_result.agentType に加えて
    hook channel(SubagentStart / SubagentStop hook event の payload、および
    hook_name の <HookEvent>:<agent_type> 接尾辞)からも agent type を抽出できるようにする。
    優先順位は既存の tool_use_result 経路を先に評価し、そこで得られない場合のみ hook channel を見る。
  • identity evidence の provenance を child_agent_type_source として
    summary.md の schema summary に記録する。
  • extract_claude_child_session_id() の parent_session_id 欠落時の短絡
    (run_worktree_agent_runtime_smoke.py 内 if not parent_session_id: return None)を解消し、
    stdout に child agentId が存在すればそれを返す。
  • Agent tool の launch mode(同期完了 / 非同期起動 / 不明)を
    child_spawn_launch_mode として記録する。
  • 上記すべてに対する fixture ベースの回帰テストを追加する。

Out of Scope

  • native_spawn_event_observed の合成条件そのものの緩和(禁止)。
    evidence が皆無なら従来どおり False に fail-close する。
  • expected agent type の代入、caller self-report の identity evidence 化(禁止)。
  • scripts/agent-ops/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 の課題であり、別途 scope 判断が必要)。
  • repo-tracked .claude/settings.json / .claude/hooks/** の変更。
    本 Issue は既に stream-json に現れる hook event を読むだけで、hook 登録を要求しない。
  • Codex CLI lane の identity 抽出経路の変更。

Acceptance Criteria

  • AC1: 非同期起動エンベロープ(tool_use_result に agentId はあるが agentType が無い)と
    SubagentStart hook payload(agent_type を含む)を含む stdout に対して、
    extract_claude_child_agent_type() が hook payload の agent_type を返す。
  • AC2: tool_use_result.agentType が存在する stdout では、hook channel より
    tool_use_result の値が優先される(既存挙動の非退行)。
  • AC3: hook payload が無く hook_name が "SubagentStart:<agent_type>" 形式の場合、
    その接尾辞から agent type を抽出する。
  • AC4: identity evidence が一切存在しない stdout では extract_claude_child_agent_type() が
    None を返す(fail-closed の非退行)。
  • AC5: extract_claude_child_session_id() が parent_session_id を None で呼ばれても、
    stdout に child agentId があればそれを返す。
  • AC6: schema summary に child_agent_type_source が記録され、値が
    tool_use_result / hook_payload / hook_name / None のいずれかである。
  • AC7: schema summary に child_spawn_launch_mode が記録され、
    非同期起動エンベロープでは async_launched になる。
  • AC8: 既存の scripts/agent-ops/tests/test_worktree_agent_runtime_smoke.py と
    scripts/agent-ops/tests/test_agent_provider_route_smoke.py が引き続き全 pass する。

Verification Commands

# AC1-AC7
# baseline-expect: fail
$ uv run pytest scripts/agent-ops/tests/test_claude_spawn_identity_evidence.py
# AC8
# baseline-expect: pass
$ uv run pytest scripts/agent-ops/tests/

Allowed Paths

  • scripts/agent-ops/run_worktree_agent_runtime_smoke.py
  • scripts/agent-ops/tests/

Stop Conditions

  • Outcome / Scope / AC が検索可能な形で記載されていない場合は即停止
  • Allowed Paths 外への書き込みを試みた場合は即停止
  • 権限不足(permission denied)により操作が完了できない場合は即停止
  • native_spawn_event_observed の evidence 水準を下げる変更が必要になった場合は即停止

Handoff Contract

  • Current Objective
  • Bounded Current Context
  • Open Questions
  • Next Action
  • Artifact Refs

Notes for Reviewer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions