Skip to content

Comments

feat: SubagentStopフックのJSON出力対応#62

Merged
syou6162 merged 9 commits intomainfrom
subagent_stop
Feb 9, 2026
Merged

feat: SubagentStopフックのJSON出力対応#62
syou6162 merged 9 commits intomainfrom
subagent_stop

Conversation

@syou6162
Copy link
Owner

@syou6162 syou6162 commented Feb 9, 2026

概要

SubagentStopフックをレガシーなexit codeベースの実装からJSON出力形式に移行し、Claude Code公式仕様に準拠させました。

背景

cchookは各フックイベントのJSON出力対応を段階的に進めています。SessionStart、UserPromptSubmit、PreToolUse、Stop、PostToolUseのJSON出力対応は完了済みでしたが、SubagentStopはレガシーなexit codeベースの実装が残っていました。Claude Code公式仕様ではSubagentStopもStopと同じJSON出力形式が必要とされています。

変更内容

  • types.go: SubagentStopOutput型を追加(StopOutputと同一スキーマ、公式仕様準拠)
  • executor.go: ExecuteSubagentStopActionをJSON出力対応に変更((*ActionOutput, error)を返すシグネチャに変更)
    • command/outputアクション両対応
    • fail-safe: エラー時はdecision="block"
    • checkUnsupportedFieldsSubagentStopを追加
  • hooks.go: executeSubagentStopHooksをJSON出力対応に変更
    • 複数アクションのdecision/reason/systemMessageマージロジック実装
    • decision="block"時のearly return
    • fail-safe: アクションエラー時にdecision="block"
    • RunSubagentStopHooks公開ラッパー関数を追加
    • runHooksのSubagentStopブランチを更新
  • main.go: SubagentStop JSON出力ハンドリングを追加(RunSubagentStopHooks → JSON Marshal → validate → stdout出力 → exit 0)
  • utils.go: validateSubagentStopOutputを追加(スキーマ検証 + セマンティック検証)
  • テスト: SubagentStop関連27件のテストを追加・修正
    • TestExecuteSubagentStopAction_TypeOutput(9件)
    • TestExecuteSubagentStopAction_TypeCommand(9件)
    • TestExecuteSubagentStopHooksJSON(8件)
    • TestValidateSubagentStopOutput(5件)
    • 既存テスト2件を修正
  • CLAUDE.md: SubagentStop JSON Outputセクションを追加
  • README.md: Exit Status ControlセクションにSubagentStopをJSON Output Eventsとして追加

関連情報

SubagentStopフックのJSON出力対応のため、SubagentStopOutput型をtypes.goに追加。
StopOutputと同じスキーマを使用(公式仕様に準拠)。
ExecuteSubagentStopAction_TypeOutputテストケースを追加。
9つのシナリオ(decision未指定/block+reason/fail-safe等)をカバー。
テストをコンパイル可能にするため、ExecuteSubagentStopActionのシグネチャを
(*ActionOutput, error)に変更し、スタブ実装を追加。hooks.goの呼び出し元も修正。
ExecuteSubagentStopAction_TypeCommandテストケースを追加。
9つのシナリオ(有効JSON/コマンド失敗/無効JSON/fail-safe等)をカバー。
Stopと同じスキーマのため、同様のテストパターンを使用。
ExecuteStopActionと同じロジックでSubagentStop JSON出力を実装。
- command/outputアクション両対応
- fail-safe: エラー時はdecision="block"
- checkUnsupportedFieldsSubagentStopを追加
- テストケースのwantSystemMessage期待値を修正

全18テストケースがパス(TypeOutput 9件、TypeCommand 9件)
executeStopHooksと同じマージロジックでSubagentStop hooks実装。
- 複数アクションのdecision/reason/systemMessageマージ
- decision="block"時のearly return
- fail-safe: アクションエラー時にdecision="block"
- runHooksのSubagentStopブランチ更新(戻り値を破棄)
- 既存テスト2件 + 新規テスト8件を修正/追加

全テストパス(SubagentStop関連27件)
main.goから呼び出す公開ラッパー関数を実装。
- stdin入力のパース
- executeSubagentStopHooksへの委譲
- (*SubagentStopOutput, error)を返してJSON化対応

RunStopHooksと同じパターン。Step 5完了。
Step 3 (validate)とStep 6 (main.go)を実装完了。
- utils.go: validateSubagentStopOutputを追加(StopとStopOutput同じスキーマ)
- utils_test.go: TestValidateSubagentStopOutputを追加(5テストケース)
- main.go: SubagentStop JSON出力ハンドリング追加(Stopと同じパターン)
  - RunSubagentStopHooks呼び出し
  - fail-safe: エラー時decision="block"
  - JSON Marshal + validate + stdout出力
  - 常にexit 0

全テストパス。ビルド成功。
CLAUDE.mdとREADME.mdにSubagentStop JSON出力の説明を追加。
- CLAUDE.md: SubagentStop JSON Outputセクション追加(Stopと同じパターン)
  - Output/Command action形式
  - Field merging、Exit code behavior、Exampleを記載
- README.md: Exit Status Controlセクション更新
  - JSON Output EventsにSubagentStopを追加
  - Legacy Exit Code EventsからSubagentStopを除外
  - exit_statusデフォルト一覧を更新

Step 1-6完了。ドキュメント整備完了。
@syou6162 syou6162 self-assigned this Feb 9, 2026
@syou6162 syou6162 marked this pull request as ready for review February 9, 2026 06:42
@syou6162 syou6162 merged commit 7350f2b into main Feb 9, 2026
2 checks passed
@syou6162 syou6162 deleted the subagent_stop branch February 9, 2026 06:42
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.

1 participant