Skip to content

refactor(hooks): post-pr-monitor を PostToolUse hook からスタンドアロン CLI に移行 - #13

Merged
aloekun merged 2 commits into
masterfrom
refactor/post-pr-monitor-standalone
Apr 3, 2026
Merged

refactor(hooks): post-pr-monitor を PostToolUse hook からスタンドアロン CLI に移行#13
aloekun merged 2 commits into
masterfrom
refactor/post-pr-monitor-standalone

Conversation

@aloekun

@aloekun aloekun commented Apr 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • hooks-post-pr-monitor.exe を PostToolUse hook からスタンドアロン CLI に全面書き換え
    • デフォルト: pnpm pr-creategh pr create + claude -p で CronCreate 監視開始
    • --monitor-only: pnpm push 完了後に PR 存在確認 → あれば監視開始
  • hooks-pre-tool-validate.exegh-pr-create-guard プリセット追加(直接の gh pr create をブロック)
  • settings.local.json.template から PostToolUse Bash matcher 削除
  • ADR-009 アーキテクチャ図・変更履歴を更新

Why

元の PostToolUse hook + additionalContext 方式は Claude が CronCreate を実行しないケースがあり信頼性が低かった。push-pipeline と同じ「ガード + 専用コマンド + claude -p」パターンに統一。

Test plan

  • cargo test — hooks-pre-tool-validate 99テスト全通過(gh-pr-create-guard 6件含む)
  • pnpm build:hooks — 全 exe ビルド成功
  • pnpm push — pipeline + --monitor-only チェイン動作確認(PR なし時は正常スキップ)
  • pnpm pr-create — PR 作成成功

Known Issues (次 PR で対応)

  • pnpm pr-create 経由で改行を含む --body が切り詰められる(pnpm のシェル引数分割)
  • PR 番号パースが gh pr create 出力の URL から取得できていない(pr=None)
  • claude -p の監視ジョブ起動がタイムアウト

🤖 Generated with Claude Code

Summary by CodeRabbit

リリースノート

  • 新機能

    • PR作成フローを保護するガード機能を追加し、直接的なPR作成コマンドをブロックし、指定されたワークフローを通じるよう誘導します。
    • 新しいpnpm pr-createスクリプトでPR作成と監視を実行できるようになりました。
  • 改善

    • PR監視メカニズムをスタンドアロンツールに移行し、より柔軟な実行制御を実現しました。
    • PR監視の確認間隔を120秒に更新しました。

- hooks-post-pr-monitor.exe: stdin JSON パース → CLI 引数方式に全面書き換え
  - デフォルトモード: gh pr create 実行後に claude -p で CronCreate 監視開始
  - --monitor-only モード: PR 存在確認のみ、あれば監視開始
- hooks-pre-tool-validate.exe: gh-pr-create-guard プリセット追加 (テスト6件)
- hooks-config.toml: blocked_patterns に gh-pr-create-guard 追加
- package.json: pnpm pr-create / pnpm push に --monitor-only チェイン追加
- settings.local.json.template: PostToolUse Bash matcher 削除、pnpm pr-create 許可追加
- ADR-009: アーキテクチャ図・変更履歴を更新

Why: 元の PostToolUse hook + additionalContext 方式は Claude が CronCreate を
実行しないケースがあり信頼性が低かった。push-pipeline と同じ
「ガード + 専用コマンド + claude -p」パターンに統一。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 2, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 798c9ddc-c5d2-4464-b29c-da663b44f4a6

📥 Commits

Reviewing files that changed from the base of the PR and between 3f676f8 and 3cca0ac.

📒 Files selected for processing (1)
  • .claude/hooks-pre-tool-validate/src/main.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • .claude/hooks-pre-tool-validate/src/main.rs

📝 Walkthrough

Walkthrough

このPRは、gh pr createをブロックする新しいgh-pr-create-guardプリセットを追加し、PostToolUseフックベースのPR監視をスタンドアロンCLI(hooks-post-pr-monitor)へリファクタリングします。設定、フック実装、スクリプト、ドキュメント、および関連テストが更新されます。

Changes

Cohort / File(s) Summary
設定・依存関係の更新
​.claude/hooks-config.toml, ​.claude/hooks-post-pr-monitor/Cargo.toml, ​.claude/settings.local.json.template
pre_tool_validate.blocked_patterns"gh-pr-create-guard"を追加。hooks-post-pr-monitorからserde_jsonregexを削除。settings.local.json.templateからPostToolUseフック行を削除し、pnpm pr-create許可ルールを追加。
Pre-Tool-Validateフック拡張
​.claude/hooks-pre-tool-validate/src/main.rs
preset_gh_pr_create_guard()を追加し、gh pr create(チェーン・環境変数プレフィックス・-R指定含む)をブロックする正規表現パターンを導入。ビルドロジックのプリセット識別と対応テストを追加。
Post-PR監視ツールのリファクタリング
​.claude/hooks-post-pr-monitor/src/main.rs
従来のJSON stdinトリガー検出を削除し、独立CLIへ移行。gh pr createを実行してから監視を開始する通常モードと、--monitor-onlyで既存PRを監視するモードを追加。サブプロセス実行ユーティリティ(並行出力ドレイン、タイムアウト、出力結合)を導入。テストを更新。
スクリプト・ドキュメント更新
package.json, docs/adr/adr-009-post-pr-monitor.md
package.jsonpushスクリプトに--monitor-only呼び出しを追加し、新しいpr-createスクリプトを追加。ADRをPostToolUse→(PreToolUseガード + スタンドアロンCLI)の2段階フローへ更新し、ポーリング間隔を30s→120sへ変更。

Sequence Diagram(s)

sequenceDiagram
participant Dev as 開発者
participant Shell as ローカルシェル
participant PreGuard as PreToolValidate (gh-pr-create-guard)
participant CLI as hooks-post-pr-monitor.exe
participant GH as GitHub CLI (gh)
participant Claude as Claude (claude -p)
Shell->>PreGuard: コマンド入力 (例: gh pr create ...)
alt マッチする場合
PreGuard-->>Shell: ブロック指示(pnpm pr-create推奨)
Shell->>CLI: `pnpm pr-create` 実行
CLI->>GH: `gh pr create` 実行
GH-->>CLI: PR作成結果
CLI->>Claude: `claude -p` による CronCreate 開始(監視プロンプト送信)
Claude-->>CLI: 監視応答/アクション
else マッチしない場合
PreGuard-->>Shell: 通常実行許可
end
Loading

推定レビュー工数

🎯 4 (Complex) | ⏱️ ~50 分

関連する可能性のあるPR

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PRタイトルは、PostToolUse hookからスタンドアロンCLIへの移行という主要な変更を明確に説明しており、実際の変更内容と完全に一致しています。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.claude/hooks-post-pr-monitor/src/main.rs:
- Around line 125-128: The code in run_cmd_direct is directly concatenating
stdout and stderr into combined (using stdout_handle.join/ stderr_handle.join
and format!("{}{}", ...)), which misses the newline behavior used elsewhere;
replace that manual concatenation with a call to the existing combine_output
helper (the function defined around lines 187-195) so you produce the same
newline-separated output consistently—fetch stdout_text and stderr_text as you
already do, then pass them into combine_output (using the exact parameter
names/signature from combine_output) and assign its return to combined.

In @.claude/hooks-pre-tool-validate/src/main.rs:
- Around line 267-285: In the preset_gh_pr_create_guard() function update the
user-facing message inside the BlockedPattern so it refers to the actual script
name `pnpm pr-create` instead of `pnpm pr`; specifically replace the occurrences
in the message text (the "PR 作成は ..." line and the "代わりに以下を実行してください:" code
example) so the guidance and the code block use `pnpm pr-create -- --title
"タイトル" --body "本文"`; keep the rest of the BlockedPattern (Regex, structure)
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 423daabd-8cfa-44a7-bde8-3fde5819cd60

📥 Commits

Reviewing files that changed from the base of the PR and between a1a464c and 3f676f8.

⛔ Files ignored due to path filters (1)
  • .claude/hooks-post-pr-monitor/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • .claude/hooks-config.toml
  • .claude/hooks-post-pr-monitor/Cargo.toml
  • .claude/hooks-post-pr-monitor/src/main.rs
  • .claude/hooks-pre-tool-validate/src/main.rs
  • .claude/settings.local.json.template
  • docs/adr/adr-009-post-pr-monitor.md
  • package.json
💤 Files with no reviewable changes (1)
  • .claude/hooks-post-pr-monitor/Cargo.toml

Comment on lines +125 to +128
let stdout_text = stdout_handle.join().unwrap_or_default();
let stderr_text = stderr_handle.join().unwrap_or_default();
let combined = format!("{}{}", stdout_text, stderr_text).trim().to_string();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

stdout と stderr の結合に改行が欠けています。

run_cmd_direct では stdout と stderr を直接結合していますが、combine_output 関数(Line 187-195)では改行を挿入しています。一貫性のため combine_output を使用してください。

📝 修正案
     let stdout_text = stdout_handle.join().unwrap_or_default();
     let stderr_text = stderr_handle.join().unwrap_or_default();
-    let combined = format!("{}{}", stdout_text, stderr_text).trim().to_string();
+    let combined = combine_output(&stdout_text, &stderr_text);

     if timed_out {
         return (false, format!("{}\n(timeout after {}s)", combined, timeout_secs));
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.claude/hooks-post-pr-monitor/src/main.rs around lines 125 - 128, The code
in run_cmd_direct is directly concatenating stdout and stderr into combined
(using stdout_handle.join/ stderr_handle.join and format!("{}{}", ...)), which
misses the newline behavior used elsewhere; replace that manual concatenation
with a call to the existing combine_output helper (the function defined around
lines 187-195) so you produce the same newline-separated output
consistently—fetch stdout_text and stderr_text as you already do, then pass them
into combine_output (using the exact parameter names/signature from
combine_output) and assign its return to combined.

Comment thread .claude/hooks-pre-tool-validate/src/main.rs Outdated
CodeRabbit レビュー指摘対応。ガードメッセージ内の誘導先コマンド名が
実際の package.json スクリプト名と不一致だった。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@aloekun
aloekun merged commit 60dbe5e into master Apr 3, 2026
1 check passed
@aloekun
aloekun deleted the refactor/post-pr-monitor-standalone branch April 3, 2026 05:06
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