Skip to content

fix: debounce session manifest hook noise - #1074

Merged
squne121 merged 6 commits into
mainfrom
worktree-issue-1057-session-manifest-debounce
Jun 21, 2026
Merged

squne121 merged 6 commits into
mainfrom
worktree-issue-1057-session-manifest-debounce

Conversation

@squne121

@squne121 squne121 commented Jun 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

PostToolUse の session manifest hook を Claude Code native async の debounce front gate に差し替えたうえで、iteration 3 では live worker lock・mutation Bash・producer hang・event spool redaction・scope-rollup capture・hook-boundaries narrative drift まで補強しました。Stop/SubagentStop は pending debounce state の flush を優先し、完了できない場合も machine-readable timeout reason を返したまま fail-open を維持します。

Checks

  • AC1 .claude/hooks/session_manifest_debounce.mjs を追加
  • AC2 .claude/settings.json の PostToolUse を native async + debounce entrypoint に更新
  • AC3 連続 Bash 10 payload で producer invocation が 1 回以下
  • AC4 read-only Bash 10 payload で producer invocation が 0 回
  • AC5 mutation Bash / Edit / Write の delta と event spool を sanitized relative path / mutation type のみに限定
  • AC6 PostToolUse path の stdout は空
  • AC7 stderr 10 行以内・path redaction・scope-rollup capture noisy/hang を deterministic test で固定
  • AC8 coordinator は guard timeout だけでなく debounce flush timeout も machine-readable に返す
  • AC9 docs/dev/hook-boundaries.md と .claude/settings.json の topology / timeout / stderr contract / narrative を同期
  • AC10 対象 pytest 群 PASS

Schema Change Applicability

  • decision: not_schema_change
  • 理由: session manifest schema 自体は変更していません。

Schema Consumer Inventory

  • 変更なし。既存 schema consumer の更新は不要です。

Safety Claim Matrix

安全性に関わる変更点と、今回の検証で担保した範囲・未統制の残りを表で整理します。

Claim Implemented? Not controlled Evidence Follow-up
read-only Bash は producer を起動しない yes shell feature を多用する曖昧コマンドは conservative に mutation_bash_unknown へ倒す uv run pytest .claude/hooks/tests/test_session_manifest_debounce.py -q -k "readonly_bash_skips_producer or mutating_bash_patterns_are_queued" #1047
Stop/SubagentStop は pending debounce state を flush し、できない場合も machine-readable timeout reason を返す yes truly live worker が長時間 lock を保持する場合は telemetry warn のまま fail-open uv run pytest .claude/hooks/tests/test_session_manifest_coordinator.py -q -k "flushes_pending_debounce_before_running_producer or reports_debounce_flush_timeout_when_worker_lock_stays_live" #1047
debounced worker の producer hang は timeout し、worker.lock を回復できる yes OS レベルの abrupt kill は stale TTL 経由でのみ回復 uv run pytest .claude/hooks/tests/test_session_manifest_debounce.py -q -k "worker_times_out_producer_and_releases_lock or flush_recovers_stale_lock_and_forces_pending_events" #1047
debounce spool / coordinator stderr に raw cwd・transcript_path・absolute path を残さない yes producer downstream が追加メタデータを将来広げる場合は再監視が必要 uv run pytest .claude/hooks/tests/test_session_manifest_debounce.py -q -k "event_spool_is_sanitized_without_raw_paths or stderr_redaction" と uv run pytest .claude/hooks/tests/test_session_manifest_coordinator.py -q -k "scope_rollup_capture_is_redacted_and_bounded" #1047
hook-boundaries 正本は YAML だけでなく本文 stale も検出する yes topology 変更時は checker の required narrative snippet 更新が必要 uv run pytest .claude/hooks/tests/test_generate_session_manifest_from_hook.py -q -k "hook_boundaries_sync or narrative_checker_detects_stale_posttooluse_text" と uv run python3 scripts/check_hook_boundaries.py #1047

Notes

@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.

Verdict: REQUEST_CHANGES

Mergeability

  • mergeable=MERGEABLE, merge_state_status=BLOCKED

Evidence Check

  • AC coverage: linked issue #1057 と fresh contract snapshot issuecomment-4760715964 は確認済み
  • Allowed Paths: issue #1057 の Allowed Paths 8件と PR changed files 8件は一致
  • CI Verification: ci_verdict_summary.py は head cc6c9a7e1252ddefdd8249434fbed8643e48b2e2 に対して failed を返し、deploy-pr / lint / python-test が failure
  • 検証コマンド結果: test-runner の 11/11 VC PASS self-report は確認したが、単独では APPROVE 根拠に使わない

Blockers

  • High: .claude/hooks/session_manifest_coordinator.sh:95-107 は guard timeout (guard_exit=124) のあとも producer を続行します。guard failure 時は producer を skip する従来契約と session_recording_policy_guard.sh の保護意図を破り、guard が hang しただけで unguarded な manifest 生成が走ります。test_timeout_reason も timeout reason しか見ておらず、この回帰を検出していません。 Evidence: .claude/worktrees/issue-1057-session-manifest-debounce/.claude/hooks/session_manifest_coordinator.sh:95, .claude/worktrees/issue-1057-session-manifest-debounce/.claude/hooks/tests/test_session_manifest_coordinator.py:35
  • High: .claude/hooks/session_manifest_debounce.mjs:268-273 の --flush 経路は lock を取得していないのに release(WORKER_LOCK) を無条件実行します。Stop/SubagentStop の flush が実行中 worker の lock を剥がすため、直後の PostToolUse が二重 worker を spawn でき、debounce の 1-worker 前提が壊れます。 Evidence: .claude/worktrees/issue-1057-session-manifest-debounce/.claude/hooks/session_manifest_debounce.mjs:268
  • Medium: required CI checks are red on reviewed head cc6c9a7e1252ddefdd8249434fbed8643e48b2e2 (deploy-pr, lint, python-test) なので、PR_REVIEW_JUDGE_VC_EVIDENCE_POLICY 上も APPROVE 不可です。 Evidence: ci_verdict_summary.py --pr 1074 --repo squne121/loop-protocol --expected-head-sha cc6c9a7e1252ddefdd8249434fbed8643e48b2e2

Non-blockers

  • なし

LOOP_VERDICT_V2

verdict: REQUEST_CHANGES
reviewed_head_sha: "cc6c9a7e1252ddefdd8249434fbed8643e48b2e2"
merge_ready: false
mergeability:
  mergeable: "MERGEABLE"
  merge_state_status: "BLOCKED"
blockers:
  - "high: guard timeout in session_manifest_coordinator.sh still falls through to producer; skip producer when guard exits non-zero, including timeout, and extend the timeout test to assert producer is not called"
  - "high: session_manifest_debounce.mjs --flush releases worker.lock without owning it; acquire/hand off the lock correctly or avoid releasing it from forced flush"
  - "medium: ci_verdict_summary.py reports failed required checks on reviewed head (deploy-pr, lint, python-test)"
required_auto_actions: []
auto_fix_applied: []
follow_up_issue_requests: []
allowed_paths_gate:
  status: ok
allowed_paths_gate_source: review_subagent

@github-actions

github-actions Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

Preview removed (PR #1074 closed).

@squne121

Copy link
Copy Markdown
Owner Author

TEST VERDICT POST ATTEMPT

@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.

Verdict: APPROVE

Mergeability

  • mergeable=MERGEABLE, merge_state_status=BEHIND

Evidence Check

  • AC coverage: linked issue #1057 と fresh contract snapshot issuecomment-4760715964 は確認済み
  • Allowed Paths: issue #1057 の Allowed Paths 8件と PR changed files 8件は一致
  • CI Verification: ci_verdict_summary.py は head d8ed7ea6cb11cff18fd4e81dfa50a24bac48896f に対して all_pass を返し、required checks は green
  • 検証コマンド結果: test-runner の再実行で 11/11 VC PASS / TEST_VERDICT PASS / merge_state_status BEHIND を確認済みという共有と、live CI 状態は整合しています

Blockers

  • なし

Non-blockers

  • mergeStateStatus=BEHIND のため merge-ready ではありません。branch update 後に同等証跡を維持してください。

LOOP_VERDICT_V2

verdict: APPROVE
reviewed_head_sha: "d8ed7ea6cb11cff18fd4e81dfa50a24bac48896f"
merge_ready: false
mergeability:
  mergeable: "MERGEABLE"
  merge_state_status: "BEHIND"
blockers: []
required_auto_actions:
  - kind: update_branch
    executor: implementation-worker
    skill: implement-issue.update_branch
    blocking_merge_ready: true
    mechanical: true
    expected_head_sha: "d8ed7ea6cb11cff18fd4e81dfa50a24bac48896f"
auto_fix_applied: []
follow_up_issue_requests: []
allowed_paths_gate:
  status: ok
allowed_paths_gate_source: review_subagent

@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.

Verdict: APPROVE

Mergeability

  • mergeable=MERGEABLE, merge_state_status=BEHIND
  • draft_pr=true

Evidence Check

  • AC coverage: linked issue #1057 と fresh contract snapshot issuecomment-4760715964 は確認済み
  • Allowed Paths: issue #1057 の Allowed Paths 8件と PR changed files 8件は一致
  • CI Verification: ci_verdict_summary.py は head f0c7678865aea09f86f6c3111be4aa347d76a619 に対して all_pass を返し、required checks は green
  • 検証コマンド結果: shared test-runner result(11/11 VC PASS / TEST_VERDICT PASS)と live head-scoped CI 状態は整合

Blockers

  • なし

Non-blockers

  • Draft PR のままです
  • mergeStateStatus=BEHIND のため merge-ready ではありません。branch update 後に同等証跡を維持してください

LOOP_VERDICT_V2

verdict: APPROVE
reviewed_head_sha: "f0c7678865aea09f86f6c3111be4aa347d76a619"
is_draft: true
merge_ready: false
mergeability:
  mergeable: "MERGEABLE"
  merge_state_status: "BEHIND"
blockers: []
required_auto_actions:
  - kind: update_branch
    executor: implementation-worker
    skill: implement-issue.update_branch
    blocking_merge_ready: true
    mechanical: true
    expected_head_sha: "f0c7678865aea09f86f6c3111be4aa347d76a619"
auto_fix_applied: []
follow_up_issue_requests: []
allowed_paths_gate:
  status: ok
allowed_paths_gate_source: review_subagent

@squne121
squne121 marked this pull request as ready for review June 21, 2026 03:48
@squne121

Copy link
Copy Markdown
Owner Author

結論

判定: REQUEST_CHANGES。まだマージ不可。

現行PRは open / draft=false / head a2766826e35798a23a532e147e0c977c56648df2、8 files changed、+869/-1397 です。CI自体は current head で主要workflowが success になっていますが、これは「要求契約を満たす」ことの十分条件ではありません。

#1057 の目的は、Claude Code native async + debounce front gate により PostToolUse ごとの manifest 過剰実行と main context ノイズを抑えることです。ACには「read-only Bash は producer 0」「mutation Bash/Edit/Write は sanitized relative path / mutation type のみ」「Stop/SubagentStop は60秒以内・machine-readable timeout reason・stderr 10行以内」などが含まれます。 親 #1047 も hook/preflight/context 肥大化によるトークン浪費・ノイズ根絶が目的です。

既存レビューで指摘されていた guard timeout後にproducerへ落ちる問題 と flushが未所有lockをreleaseする問題 は、現行headではおおむね修正済みです。guard non-zero では producer に進まず exit 0 し、--flush は lock を取得できない場合に skip する構造になっています。

ただし、より厳しく見ると別のブロッカーが残っています。

Blocker 1: Stop/SubagentStop の「flushしてからproducer」契約が lock held 時に破れる

session_manifest_coordinator.sh は debounce_flush を実行してから guard / producer に進む設計ですが、session_manifest_debounce.mjs --flush は worker lock があると flush_skipped_lock_held を出して即returnします。そのまま coordinator は guard / producer に進みます。つまり、Stop/SubagentStop 時に pending debounce state が存在しても、既存workerが debounce window 待機中なら flush されないまま Stop manifest が生成されます。

これは docs に書かれている「pending debounce state を flush したうえで session manifest を生成する」という説明とも矛盾します。 現在のテストは「既存lockを壊さない」ことだけを確認しており、lock held でも Stop 側が bounded wait / handoff / forced flush を達成することは確認していません。

修正方針: --flush は lock held を単なる成功扱いにせず、最大数秒だけ bounded wait するか、worker に forced flush signal を渡すか、lock owner heartbeat/stale判定で回復するべきです。少なくとも coordinator test に「pending event + worker.lock held + workerがquiet window中 → Stop producer前にpending batchがflushされる / または明示的に timeout_reason を出す」を追加してください。

Blocker 2: Bash read-only classifier が危険に楽観的

classifyBash() は sed / find をコマンド先頭だけで read-only 扱いしています。 しかし GNU sed の -i/--in-place は入力ファイルを一時ファイル経由で置換し、拡張子なしなら元ファイルを上書きします。GNU find の -delete はファイル削除アクションであり、公式マニュアルも find dirname -delete -name quux が意図せず大量削除しうる例を示しています。([GNU]1) (GNU)

この実装だと、例えば以下が read-only と誤分類され、producer invocation 0 になります。

sed -i 's/foo/bar/' docs/dev/hook-boundaries.md
find . -name '*.tmp' -delete
git diff --output=patch.diff

#1057 のAC4は「read-only Bash で producer 0」ですが、それは mutation Bash を落としてよいという意味ではありません。mutation Bash の取りこぼしは telemetry 欠落で、今回のIssue目的に直撃します。

修正方針: allowlistを「コマンド名」ではなく「サブコマンド + 禁止flag不在」まで見る保守的分類にしてください。迷ったら mutation_bash_unknown として queue するべきです。追加テストは最低限 sed -i, find -delete, find -exec rm, git diff --output, python -c 'open(...,"w")' です。

Blocker 3: detached worker 内 producer に timeout がなく、lock永久保持の経路がある

PostToolUse front gate は detached worker を stdio: 'ignore' + unref() で起動しており、Node公式の長時間バックグラウンド化パターン自体には沿っています。 ([Node.js]3) しかし worker内の runProducer() は spawnSync(PRODUCER_CMD, PRODUCER_ARGS, ...) に timeout を指定していません。 Node の spawnSync には timeout option があり、指定しない場合の default は undefined です。([Node.js]3)

このため producer が hang すると worker は戻らず、finally { release(WORKER_LOCK) } に到達しません。以後の PostToolUse は queue されても新workerを起動できず、Stop/SubagentStop の --flush も lock held で skip するため、debounce queue が永続的に詰まります。

修正方針: SESSION_MANIFEST_DEBOUNCE_PRODUCER_TIMEOUT_MS を追加し、spawnSync に timeout / killSignal / maxBuffer を設定してください。さらに worker lock には owner pid / started_at / heartbeat を書き、TTL超過時に stale recovery するテストを追加してください。

Blocker 4: raw cwd を debounce state file に保存している

STATE_DIR はデフォルトで artifacts/session-manifest-debounce 配下です。 queueEvent() は event JSON に cwd: hookCtx?.cwd ?? null をそのまま書いています。

#1057 のOutcome/ACは、raw cwd / transcript / absolute path を残さず、sanitized relative path / mutation type のみを delta に残す契約です。 現在の最終producer payloadからは消えていても、worker crash・lock詰まり・debug時に artifacts/session-manifest-debounce/events/*.json へ raw absolute cwd が残ります。これは「main contextに出ない」だけでなく「artifact/stateにも残さない」方向に寄せるべきです。

修正方針: event spool に raw cwd を保存しない。必要なら cwd_relative_root: "." か repo_root_marker のみ、または sanitizeRelativePath(cwd, cwd) の結果だけにしてください。テストは flush後payloadだけでなく、queued event fileそのものに REPO_ROOT, transcript_path, absolute path が含まれないことを確認するべきです。

Blocker 5: coordinator の scope-rollup capture が timeout/redaction/10行制限の外にある

session_manifest_coordinator.sh は guard / producer には run_step() の timeout と stderr summary を通していますが、scope-rollup capture はその前段で直接実行され、stderr を cat でそのまま流しています。 一方、Issue契約は Stop/SubagentStop coordinator の stderr 10行以内・machine-readable・redacted を要求しています。

capture script が大量stderr・absolute path・hang を起こすと、guard/producer が正しくても契約を破ります。現テストは fake guard/producer だけで、capture failure/hang/noisy stderr を検証していません。

修正方針: capture も run_step "scope_rollup_capture" に統合し、timeout / redaction / max 2 detail lines の対象にしてください。Allowed Paths 外の capture script自体は触らず、coordinator側の呼び出しラップだけで対応可能です。

Non-blocker: docs の説明表が一部 stale

YAML manifest は session_manifest_debounce を追加していますが、後段の判断表にはまだ generate_session_manifest_from_hook.mjs が telemetry として残っています。 また「fail-open telemetry 設計」でも PostToolUse 対象として generate_session_manifest_from_hook.mjs を挙げており、現行 settings の PostToolUse が session_manifest_debounce.mjs + async: true を向く事実とズレています。

check_hook_boundaries.py は YAML block と settings topology を比較するだけなので、本文表の stale は検出できません。 これはマージ前に直すべきですが、上記5点よりは軽いです。

技術調査からの判断

Claude Code の async hook は、発火ごとに別のbackground processを作り、複数発火間のdedupeは行いません。したがって今回の「native async + debounce front gate」という方向性自体は妥当ですが、front gate側の lock / forced flush / stale recovery が仕様上の中核になります。([Claude API Docs]4) また PostToolUse は実行後hookなので、tool call自体の副作用は既に発生済みです。よって「mutation Bashをread-onlyと誤判定してproducerを起動しない」設計は、あとから補正できない telemetry 欠落になります。([Claude API Docs]4)

投稿用レビューコメント案

## Verdict: REQUEST_CHANGES

CI は current head `a2766826e35798a23a532e147e0c977c56648df2` で green だが、#1057 の contract を満たすにはまだ不足があります。

### Blockers

1. High: Stop/SubagentStop の forced flush contract が lock held 時に破れます。
   `session_manifest_debounce.mjs --flush` は worker.lock があると `flush_skipped_lock_held` で即returnし、`session_manifest_coordinator.sh` はそのまま guard / producer に進みます。pending debounce state を flush してから Stop manifest を生成する契約を満たしていません。bounded wait / worker handoff / stale lock recovery のいずれかを入れ、lock held + pending event のテストを追加してください。

2. High: Bash read-only classifier が危険に楽観的です。
   `sed` と `find` を先頭コマンドだけで read-only 扱いしているため、`sed -i ...` や `find . -delete` が producer invocation 0 になります。read-only allowlist は subcommand/flag まで見て、迷うものは `mutation_bash_unknown` として queue してください。

3. High: detached worker 内 producer に timeout がなく、hang 時に worker.lock を永久保持できます。
   `spawnSync(PRODUCER_CMD, PRODUCER_ARGS, ...)` に timeout がありません。producer hang → worker lock 未解放 → future PostToolUse queue 詰まり → Stop flush skip の経路があります。producer timeout / maxBuffer / stale lock TTL / heartbeat を追加してください。

4. Medium/High: debounce state file に raw cwd を保存しています。
   `artifacts/session-manifest-debounce/events/*.json` に `cwd` がそのまま残ります。#1057 は raw cwd / absolute path を残さない契約なので、event spool 自体も sanitized relative path / mutation type だけにしてください。

5. Medium: scope-rollup capture が coordinator の timeout/redaction/10行制限の外です。
   capture stderr を直接 `cat` しており、noisy/hanging capture で coordinator contract を破れます。capture も `run_step` に統合してください。

### Required tests

- pending event + worker.lock held + Stop/SubagentStop flush の順序保証
- `sed -i`, `find -delete`, `git diff --output` が mutation扱いになること
- debounced producer hang で lock TTL/recovery すること
- queued event file に raw cwd / transcript_path / absolute path が残らないこと
- scope-rollup capture の noisy stderr/hang が 10行以内・redacted・exit 0 になること

現状は REQUEST_CHANGES です。

Claude Code / Codex CLI に投げる修正プロンプト

PR #1074 / Issue #1057 を修正してください。人間承認不要で、実装修正・テスト追加・既存CI green まで進めてください。破壊的操作、秘密情報、外部課金、権限不足だけ停止条件です。

必ず確認:
- #1057 本文と parent #1047
- PR #1074 の current head
- .claude/hooks/session_manifest_debounce.mjs
- .claude/hooks/session_manifest_coordinator.sh
- .claude/hooks/generate_session_manifest_from_hook.mjs
- .claude/settings.json
- docs/dev/hook-boundaries.md
- .claude/hooks/tests/test_session_manifest_debounce.py
- .claude/hooks/tests/test_session_manifest_coordinator.py
- .claude/hooks/tests/test_generate_session_manifest_from_hook.py

修正要件:
1. Stop/SubagentStop の `--flush` が worker.lock held 時に単なる success skip にならないようにする。
   - pending event がある場合、bounded wait / worker handoff / stale lock recovery のどれかで producer 前に flush を保証する。
   - どうしても flush できない場合は machine-readable timeout/warn reason を出し、silent success にしない。
2. Bash classifier を保守的にする。
   - `sed -i`, `sed --in-place`, `find -delete`, `find -exec`, `find -ok`, `git diff --output`, `git show --output` は mutation扱い。
   - read-only と断定できない Bash は `mutation_bash_unknown` として queue。
3. debounced worker producer に timeout / maxBuffer / killSignal を設定する。
   - `SESSION_MANIFEST_DEBOUNCE_PRODUCER_TIMEOUT_MS` を追加。
   - worker.lock に owner pid / started_at を保存し、TTL超過時に stale lock recovery する。
4. debounce event spool から raw cwd / transcript_path / absolute path を排除する。
   - spool file 自体も sanitized relative path / mutation type / session_id / issue_number 程度に限定。
5. scope-rollup capture を coordinator の timeout/redaction/10行制限に含める。
   - capture stderr を直接 cat しない。
6. docs/dev/hook-boundaries.md の本文表・fail-open説明を current topology に合わせる。
   - PostToolUse は `session_manifest_debounce.mjs` が front gate。
   - `generate_session_manifest_from_hook.mjs` は Stop/SubagentStop producer / debounce worker downstream として説明する。

追加テスト:
- worker.lock held + pending event + Stop/SubagentStop で flush保証または明示warnになる
- `sed -i`, `find -delete`, `git diff --output` が producer queue 対象になる
- producer hang で timeoutし、lockが回復する
- queued event file に raw cwd / transcript_path / absolute path が含まれない
- scope-rollup capture の 100行stderr・POSIX/Windows/WSL path・hang が 10行以内/redacted/exit 0 になる
- hook-boundaries の YAMLだけでなく本文表の stale も最低限 grep/regression test で検出する

検証:
- uv run pytest .claude/hooks/tests/test_generate_session_manifest_from_hook.py .claude/hooks/tests/test_session_manifest_coordinator.py .claude/hooks/tests/test_session_manifest_debounce.py -q
- uv run python3 scripts/check_hook_boundaries.py
- 既存CI相当の lint/python-test が通ることを確認

PR body の Safety Claim Matrix と Verification を更新し、Closes #1057 は維持してください。

@squne121
squne121 merged commit dd92b69 into main Jun 21, 2026
17 checks passed
@squne121
squne121 deleted the worktree-issue-1057-session-manifest-debounce branch June 21, 2026 04:26
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.

実装: Session Manifest hook を debounce / async split し PostToolUse ごとの過剰実行を抑制する

1 participant