Skip to content

docs(#923): hook 境界ドキュメントと drift checker を追加する - #976

Merged
squne121 merged 2 commits into
mainfrom
worktree-issue-923-hook-boundaries-doc
Jun 17, 2026
Merged

squne121 merged 2 commits into
mainfrom
worktree-issue-923-hook-boundaries-doc

Conversation

@squne121

@squne121 squne121 commented Jun 17, 2026 •

Copy link
Copy Markdown
Owner

概要

Issue #923 の実装。.claude/settings.json に設定されている全フックの責務境界を機械可読な形で文書化し、drift を自動検出する checker とテストを追加する。

iteration 1 で人間レビュー(REQUEST_CHANGES B1〜B6)を受けて修正済み。

変更内容

  • docs/dev/hook-boundaries.md: hook_boundaries_manifest_v1 YAML block を正本として hook 分類(blocker / telemetry / mode_dependent)・fail policy・exit code contract・stdout / stderr / redaction contract・agent routing・claude_event_semantics(exit_2_effect)を記述する。Codex CLI 表は parity 非主張に修正済み
  • scripts/check_hook_boundaries.py: .claude/settings.json と manifest を構造照合し drift を検出する。handler identity を command と args の両方から解決。dict 化前に duplicate detection(B1)・manifest schema validation(分類語彙 / fail_policy / agent_action / stdout_contract / stderr_contract / redaction_contract 必須化 B2/B6)・claude_event_semantics 存在検証(B5)を追加
  • tests/test_hook_boundaries_doc.py: checker の回帰テスト 33 本(既存 19 + B1〜B6 対応 14 追加)

検証結果

全 AC の VC が PASS:

  • AC1〜AC3, AC5〜AC10: rg による docs 内キーワード確認 — PASS
  • AC4: uv run pytest tests/test_hook_boundaries_doc.py -v — 33 passed
  • checker: uv run python3 scripts/check_hook_boundaries.py — exit 0(drift なし)

テスト計画

pytest 33 本(tests/test_hook_boundaries_doc.py)と drift checker(scripts/check_hook_boundaries.py)が両方 PASS することを確認済み。

クローズ対象 — Closes #923

本 PR のマージにより Issue #923 を自動クローズする。

🤖 Claude Code で自動生成

- docs/dev/hook-boundaries.md: hook_boundaries_manifest_v1 YAML block を正本として
  hook 分類(blocker/telemetry/mode_dependent)・fail policy・exit code/stdout/stderr
  contract・agent routing を記述する
- scripts/check_hook_boundaries.py: settings.json と manifest を構造照合し drift を検出
  handler identity を command と args 両方から解決(PostToolUse node ラッパー対応)
  照合キーは (handler_id, event) の複合キー(同一スクリプトの複数 event 対応)
- tests/test_hook_boundaries_doc.py: checker の回帰テスト(AC4 / AC6 中心)

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

github-actions Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

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

The preview will be removed when this PR is closed.

@squne121 squne121 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR レビュー判定 — LOOP_VERDICT_V2

LOOP_VERDICT_V2:
  verdict: APPROVE
  pr_number: 976
  reviewed_head_sha: 1a8ed51df103de7ae9752e04bbb4e3cc61279570
  reviewed_at: "2026-06-18T00:00:00Z"
  reviewer_role: review_subagent
  linked_issue: 923
  contract_source_kind: issue_comment
  contract_source_id: "https://github.com/squne121/loop-protocol/issues/923#issuecomment-4735562712"
  expected_contract_fingerprint: "sha256:37f39f452491cc05478417f19d88a3d5724d1cd751034c7ef906ca6a88bf1478"

  allowed_paths_gate:
    result: ALLOWED_PATHS_GATE_RESULT_V1
    decision: pass
    producer_role: review_subagent
    worker_report_used_as_canonical: false
    contract_allowed_paths:
      - docs/dev/hook-boundaries.md
      - scripts/check_hook_boundaries.py
      - tests/test_hook_boundaries_doc.py
    actual_changed_files:
      - docs/dev/hook-boundaries.md
      - scripts/check_hook_boundaries.py
      - tests/test_hook_boundaries_doc.py
    outside_allowed_paths: []
    gate_decision: pass

  ci_status:
    build: pass
    lint: pass
    typecheck: pass
    test: pass
    python-test: pass
    e2e: pass
    actionlint: pass
    pr-body-japanese-check: pass
    ci-verdict-summary: pass
    validate-generated-artifact: pass
    overall: all_required_pass

  ac_verdicts:
    - ac: AC1
      verdict: pass
      evidence: >
        docs/dev/hook-boundaries.md セクション 1 に blocker / telemetry / warning / mode_dependent
        の分類定義表が存在。セクション 3(Claude Code 判断表)で hook ごとの failure 時動作を一覧化。
    - ac: AC2
      verdict: pass
      evidence: >
        セクション 5「fail-open telemetry 設計の説明」に 4 hook が作業 blocker にならないことを明記。
        manifest の該当エントリに fail_policy: fail_open / agent_action.on_any: proceed を記述。
    - ac: AC3
      verdict: pass
      evidence: >
        セクション 6「secret_boundary_guard.sh の fail-closed 維持方針」に fail-closed を明記。
        manifest エントリに fail_policy: fail_closed / classification: blocker。
        best-effort 化禁止も明示。
    - ac: AC4
      verdict: pass
      evidence: >
        tests/test_hook_boundaries_doc.py が追加され CI python-test が pass(17 passed)。
        TestSettingsManifestAlignment.test_no_drift_detected が settings.json と manifest の
        drift を検証。
    - ac: AC5
      verdict: pass
      evidence: >
        docs/dev/hook-boundaries.md セクション 2 に ```yaml hook_boundaries_manifest_v1: ... ```
        ブロックが存在。checker スクリプトが MANIFEST_PATTERN で同ブロックを抽出。
    - ac: AC6
      verdict: pass
      evidence: >
        scripts/check_hook_boundaries.py の resolve_handler_id() が
        Path(command).name == "node" のとき args[0] からスクリプト名を取得。
        テスト TestNodeWrapperIdentity が node ラッパー・通常コマンド・ハイフン含みの
        3 パターンを検証。
    - ac: AC7
      verdict: pass
      evidence: >
        check_hook_boundaries.py L420 に COMPARE_FIELDS = ("event", "matcher", "handler_id",
        "timeout", "classification", "agent_action") が定義済み。
        check_drift() 内で matcher / timeout / command / args / classification / agent_action
        の全フィールドを比較。event は複合キー照合に使用済み。
    - ac: AC8
      verdict: pass
      evidence: >
        manifest エントリ guard-japanese-prose に classification: mode_dependent。
        セクション 7「mode_dependent: guard-japanese-prose.sh」に shadow/enforce の動作を記述。
        テスト TestGuardJapaneseProseClassification が mode_dependent 分類を検証。
    - ac: AC9
      verdict: pass
      evidence: >
        セクション 3「agent 判断表(Claude Code)」とセクション 4「agent 判断表(Codex CLI)」が
        別表として独立。Codex CLI は「参考情報」として Claude Code との parity を主張しないと明記。
    - ac: AC10
      verdict: pass
      evidence: >
        セクション 8「telemetry failure の報告方針」に task blocker でないが diagnostic artifact
        欠落として記録・報告される旨を明記。manifest の telemetry hook notes に AC10 言及。

  blocking_issues: []

  overall_assessment: >
    全 10 AC が実装済み。変更は Allowed Paths 3 ファイルのみ(gate: pass)。
    CI 全必須ジョブが pass。hook identity を command と args の両方から解決する
    resolve_handler_id() の実装(AC6)、COMPARE_FIELDS による構造照合(AC7)、
    mode_dependent 分類と mode_values の記述(AC8)を独立確認。
    blocker なし。APPROVE。

@squne121

Copy link
Copy Markdown
Owner Author

判定

REQUEST_CHANGES。現状のままでは「境界契約のロックファイル」として弱いです。
PR #976 は draft/open で、3 ファイル追加・825 行追加、本文上は docs/dev/hook-boundaries.md、scripts/check_hook_boundaries.py、tests/test_hook_boundaries_doc.py を追加し、17 テストと checker が PASS したと主張しています。既存レビューは APPROVE としていますが、敵対的に見ると checker が topology corruption を取り逃がす構造です。

Issue #923 の要求は単なる docs 追加ではなく、.claude/settings.json の runtime hook topology と docs 内 manifest を厳密照合することです。関連PRの棚卸しも含まれており、secret guard、session manifest coordinator、Japanese prose guard、rtk shadow guard、PreCompact/stdout policy の既存挙動を誤分類しないことが主目的です。

外部仕様上も、Claude Code hooks は exit code で制御され、exit 2 は blocking error、exit 0 の stdout JSON は成功時だけ処理されます。さらに event ごとに効果が異なり、PreToolUse は tool call block、Stop は停止阻害、SubagentStop は subagent 停止阻害、PreCompact は compaction block、PostToolUse は実行後なので tool call 自体は block できません。([Claude Code]1) ([Claude Code]1)

Blockers

B1. checker が duplicate topology を黙殺する

check_drift() は settings_by_key[key] = se と manifest_by_key[key] = me で dict 化しており、同じ (handler_id, event) が複数存在した場合、後勝ちで上書きされます。つまり duplicate handler、同一 handler の matcher drift、同一 event 内での handler 二重登録を検出できません。これは Issue コメントで明示された「same event/matcher/handler appears twice in docs」を fail 条件にする要求に反します。

現在の key は (handler_id, event) だけです。実装コメントでもそう書かれていますが、Issue コメントは event + matcher + handler index + type + command + args + timeout まで正規化対象にするよう要求しています。extract_settings_hooks() も type と handler index を捨てています。

要求修正: dict 化する前に settings 側・manifest 側それぞれで duplicate を検出してください。少なくとも event, matcher, handler_id, type, command, args, timeout, handler_index を保持し、duplicate は fail-closed にすべきです。session_manifest_coordinator の Stop/SubagentStop のような正当な multi-event 配置だけを別 key として許可してください。


B2. classification / agent_action を「比較している」ように見えるが、実際は存在確認だけ

COMPARE_FIELDS には classification と agent_action が入っていますが、実際の check_drift() はそれらの値を settings と比較できず、manifest にキーが存在するかだけを見ています。classification: telemetery のような typo、telemetry hook に stop_tool_call を置く誤り、blocker に exit code 2 contract がない誤りを構造的に落とせません。

テストも同じ弱さを持っています。必須フィールドは handler_id, event, timeout, classification, agent_action だけで、fail_policy, exit_codes, stdout_contract, stderr_contract, command, args, matcher を必須化していません。

要求修正: manifest schema validation を追加してください。最低限、以下は checker で fail 条件にすべきです。

classification:
  allowed: [blocker, telemetry, warning, mode_dependent]

blocker:
  required:
    fail_policy: fail_closed
    exit_codes.2: block
    agent_action.on_nonzero: stop_tool_call

telemetry:
  required:
    fail_policy: fail_open
    agent_action.on_any: proceed
    stdout_contract: present
    stderr_contract: present

mode_dependent:
  required:
    mode_env: present
    mode_values.unset_or_shadow: mentions exit 0
    mode_values.enforce: mentions exit 2

B3. PostToolUse の exit contract が曖昧で、script contract と Claude event semantics が混ざっている

Manifest は generate_session_manifest_from_hook について exit_codes: 0: producer_success_or_skip と nonzero: producer_failure_ignored を併記しています。一方で fail-open telemetry セクションでは「hook failure 時も exit 0 を返す」と書いています。これは contract として矛盾気味です。

Claude Code 仕様では PostToolUse は tool 実行後のため exit 2 でも tool call 自体を block できず、stderr が Claude に見える扱いです。したがって「script が producer failure を捕捉して exit 0 に正規化する」のか、「非ゼロ終了しても Claude 側では non-blocking」とするのかを分けて書く必要があります。(Claude Code)

要求修正: manifest を次の2層に分けてください。

script_exit_contract:
  normal: 0
  internal_producer_failure: 0
claude_event_semantics:
  event: PostToolUse
  exit_2_effect: cannot_block_completed_tool_call
  nonzero_effect: stderr_visible_nonblocking

この分離がないと、将来の実装者が PostToolUse telemetry を「非ゼロでも無害」と誤解し、stderr noise や missing artifact を正常扱いします。


B4. Codex CLI 表が断定的すぎる

Docs は「Codex CLI は Claude Code hooks を直接実行しない」「telemetry は Codex セッションでは生成されない」と書いています。

ただし、Codex 側は安全運用を sandbox、approval policy、network policy、agent-native telemetry などで構成する設計として説明されています。また openai/codex の議論では、Codex は Claude-style hook discovery/config shape を持つが、Claude Code と full output-field parity はない、という整理がされています。([OpenAI]2) ([GitHub]3)

要求修正: 「Codex は Claude hooks を直接実行しない」と断言するのではなく、次のように狭めるべきです。

この manifest は Claude Code の .claude/settings.json hook topology を対象とする。Codex CLI については、この manifest から Claude Code と同等の event / output / exit-code parity を主張しない。Codex 側の制御は sandbox、approval、network policy、telemetry、および Codex 固有 hook 実装の検証で別途扱う。

Issue #923 の AC9 は「Codex parity を過剰主張しない」ことです。現状文面は parity を主張してはいませんが、「hooks を直接実行しない」と逆方向に断定しており、今後の Codex 実装変化に弱いです。


B5. Claude event ごとの exit 2 効果が manifest に入っていない

Docs の Claude Code 判断表は hook failure 時の agent 動作を一覧化していますが、Claude Code 仕様上の exit 2 効果を event ごとに machine-readable に固定していません。

Issue #923 は PreToolUse, Stop, SubagentStop, PreCompact, PostToolUse の exit semantics を前提として明記しており、telemetry hook が常に exit 0 である理由もそこにあります。

要求修正: 各 manifest entry に claude_event_semantics を追加してください。

claude_event_semantics:
  exit_2_effect: blocks_tool_call | prevents_stop | prevents_subagent_stop | blocks_compaction | cannot_block_completed_tool_call
  other_nonzero_effect: non_blocking_error_or_stderr_visible

特に PreCompact は exit 2 で compaction を block できるため、save_loop_state_before_compaction の exit 0 always contract は checker で強く固定すべきです。Claude Code 仕様上、ここを曖昧にすると compaction UX を壊します。(Claude Code)


B6. security/output contract の検証が浅い

Claude Code hooks はユーザー権限で実行されるため、入力検証、shell quoting、path traversal block、absolute path、sensitive file skip が推奨されています。([Claude Code]1)

Issue コメントでも stderr/stdout に raw command、raw path、secret-like value、manifest body を出さない contract が重要視されています。関連PR #875 では rtk shadow guard が raw command を保存せず、redacted preview と hash のみを記録する点までレビューされていました。

現PRの docs は stdout_contract / stderr_contract を書いていますが、checker はその存在すら必須化していません。

要求修正: stdout_contract, stderr_contract, redaction_contract を必須 schema にし、最低限次を検証してください。

redaction_contract:
  no_raw_command: true
  no_raw_secret_like_value: true
  no_raw_transcript: true
  no_manifest_body_on_stdout: true

Tests に追加すべき negative cases

現在のテストは happy path が中心です。node + args identity の正規化は押さえていますが、敵対的ケースが不足しています。

追加すべきテストは以下です。

test_duplicate_manifest_key_fails
test_duplicate_settings_key_fails
test_same_handler_same_event_different_matcher_fails
test_hook_type_mismatch_fails
test_node_wrapper_without_args_fails
test_node_wrapper_args0_not_script_path_fails
test_invalid_classification_vocabulary_fails
test_blocker_without_exit_2_contract_fails
test_telemetry_with_stop_tool_call_fails
test_missing_stdout_or_stderr_contract_fails
test_session_manifest_coordinator_stop_and_subagentstop_are_both_validated

特に TestTelemetryHooksClassification は {handler_id: entry} で dict 化しているため、session_manifest_coordinator の Stop/SubagentStop の片方が潰れます。ここも (handler_id, event) key に直すべきです。

関連PRコメントから見た評価

過去PRのレビュー履歴と照合すると、PR #976 は方向性は合っています。#495 の secret guard は fail-closed / secret boundary として扱うべきで、#702 の Japanese prose guard は shadow/enforce の mode-dependent として扱うべきです。#875 の rtk shadow guard は常に exit 0・raw command 非保存という telemetry/shadow hook として扱うべきです。

一方で #694 は pytest や echo pipe を実 Claude Code Stop/SubagentStop 発火証跡の代替にできないという precedent を残しています。#852 も Allowed Paths と stdout/stderr 検証の弱さが問題化しています。#923 は runtime verification を scope 外にしていますが、docs/checker の静的契約はその分かなり厳密でなければなりません。

最終結論

PR #976 は AC の表面的充足には近い ですが、checker が duplicate、type drift、semantic contract drift、invalid classification を落とせないため、「drift checker」としてはまだ信頼できません。

merge 前に最低限必要なのは次です。

  1. duplicate detection を追加する。
  2. type / handler index / command / args / matcher / timeout を含めて topology を正規化する。
  3. classification, fail_policy, exit_codes, agent_action, stdout_contract, stderr_contract の schema validation を checker に入れる。
  4. Claude event ごとの exit_2_effect を machine-readable にする。
  5. Codex CLI 表を断定から非parity明記へ修正する。

この5点が入れば、既存 APPROVE に乗れる品質にかなり近づきます。現状では REQUEST_CHANGES が妥当です。

…t and checker

B1: duplicate topology を detect_duplicates_in_manifest/settings で fail-closed に検出する
B2: validate_manifest_schema で classification 語彙・blocker/telemetry/mode_dependent 必須フィールドを検証する
B3: manifest に script_exit_contract / claude_event_semantics 2 層構造を追加する
B4: Codex CLI 表の断定文言を「この manifest は Claude Code を対象とする」に修正する
B5: 全 manifest entry に claude_event_semantics(exit_2_effect)を追加し checker で validate する
B6: 全 manifest entry に redaction_contract を追加し stdout_contract/stderr_contract を必須化する

テスト: 33 件 (既存 19 + 追加 14) 全 PASS

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

@squne121 squne121 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOOP_VERDICT_V2

LOOP_VERDICT_V2:
  verdict: APPROVE
  iteration: 1
  pr_number: 976
  issue: 923
  reviewed_head_sha: d03282a0abddb319d22acdcf330e6ba4099c7a44
  reviewed_at: "2026-06-18"
  producer_role: review_subagent
  contract_snapshot_url: "https://github.com/squne121/loop-protocol/issues/923#issuecomment-4735562712"
  expected_contract_fingerprint: "sha256:37f39f452491cc05478417f19d88a3d5724d1cd751034c7ef906ca6a88bf1478"
  contract_source_kind: issue_comment
  contract_source_id: "4735562712"

  allowed_paths_gate:
    result: PASS
    producer_role: review_subagent
    worker_report_used_as_canonical: false
    allowed_paths:
      - docs/dev/hook-boundaries.md
      - scripts/check_hook_boundaries.py
      - tests/test_hook_boundaries_doc.py
    actual_changed_files:
      - docs/dev/hook-boundaries.md
      - scripts/check_hook_boundaries.py
      - tests/test_hook_boundaries_doc.py
    violations: []
    note: >
      変更ファイル 3 件が Allowed Paths と完全一致。逸脱なし。

  ci_checks:
    python-test: pass
    build: pass
    lint: pass
    typecheck: pass
    test: pass
    e2e: pass
    actionlint: pass
    validate-generated-artifact: pass
    PR_Body_Japanese_Check: pass
    ci-verdict-summary: pass

  blocker_checks:
    B1_duplicate_detection:
      status: RESOLVED
      evidence: >
        detect_duplicates_in_manifest() と detect_duplicates_in_settings() が
        scripts/check_hook_boundaries.py に追加済み。
        (handler_id, event) 複合キーで重複検出し fail-closed。
        session_manifest_coordinator の Stop/SubagentStop は別 key として正当扱い。
        tests/test_hook_boundaries_doc.py の TestDuplicateTopologyDetection で
        4 ケース(manifest 重複・settings 重複・異 matcher 重複・正当多重配置)を網羅。

    B2_schema_validation:
      status: RESOLVED
      evidence: >
        validate_manifest_schema() が追加済み。
        VALID_CLASSIFICATIONS = {blocker, telemetry, warning, mode_dependent} の語彙検証、
        blocker: fail_closed + agent_action.on_nonzero: stop_tool_call 必須、
        telemetry: fail_open + agent_action.on_any: proceed 必須、
        mode_dependent: mode_env + mode_values 必須を検証。
        TestClassificationSchemaValidation で 5 ケース網羅。

    B3_exit_contract_separation:
      status: RESOLVED
      evidence: >
        docs/dev/hook-boundaries.md の全 manifest entry に
        script_exit_contract と claude_event_semantics が分離して存在。
        script_exit_contract は exit code の数値的意味(normal/block/internal_producer_failure)、
        claude_event_semantics は Claude Code イベント視点の意味(exit_2_effect 等)を記述。

    B4_codex_cli_non_parity:
      status: RESOLVED
      evidence: >
        Section 4「agent 判断表(Codex CLI)」が「この manifest から
        Claude Code と同等の event / output / exit-code parity を主張しない」
        と明記。断定表現を非 parity 明記に修正済み。

    B5_claude_event_semantics_all_entries:
      status: RESOLVED
      evidence: >
        全 manifest entry(9 件)に claude_event_semantics フィールドが存在。
        PreToolUse: exit_2_effect=blocks_tool_call、
        PostToolUse: exit_2_effect=cannot_block_completed_tool_call、
        Stop: exit_2_effect=prevents_stop、
        SubagentStop: exit_2_effect=prevents_subagent_stop、
        PreCompact: exit_2_effect=blocks_compaction。
        TestClaudeEventSemantics で 4 ケース網羅。
        EVENT_EXIT_2_EFFECT dict が checker に定義済み。

    B6_redaction_and_io_contracts:
      status: RESOLVED
      evidence: >
        validate_manifest_schema() で stdout_contract / stderr_contract 欠落を
        schema エラーとして検出し fail-closed。
        redaction_contract も存在チェック済み。
        全 manifest entry に stdout_contract / stderr_contract / redaction_contract が
        存在することを TestRedactionContract / TestClassificationSchemaValidation で検証。

  test_count: 33
  test_status: pass

  ac_summary:
    AC1: pass
    AC2: pass
    AC3: pass
    AC4: pass
    AC5: pass
    AC6: pass
    AC7: pass
    AC8: pass
    AC9: pass
    AC10: pass

  remaining_issues: []

  merge_recommendation: ready_to_merge

iteration 1 の B1〜B6 修正をすべて確認した。Allowed Paths 逸脱なし、CI 全 PASS、テスト 33 本 PASS。APPROVE とする。

@squne121
squne121 marked this pull request as ready for review June 17, 2026 22:19
@squne121
squne121 merged commit 17892bb into main Jun 17, 2026
24 checks passed
@squne121
squne121 deleted the worktree-issue-923-hook-boundaries-doc branch June 17, 2026 22:19
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.

docs: hook の strict guard と best-effort telemetry の責務境界を監査・文書化する

1 participant