Skip to content

feat(hooks): SessionStart hook でセッション ID 伝播 + CronCreate セッション問題解決 - #17

Closed
aloekun wants to merge 1 commit into
masterfrom
feat/session-start-hook
Closed

feat(hooks): SessionStart hook でセッション ID 伝播 + CronCreate セッション問題解決#17
aloekun wants to merge 1 commit into
masterfrom
feat/session-start-hook

Conversation

@aloekun

@aloekun aloekun commented Apr 3, 2026

Copy link
Copy Markdown
Owner

SessionStart hook 新規作成 + hooks-post-pr-monitor を --resume 方式に変更

Summary by CodeRabbit

  • 新機能

    • セッション開始フックを追加し、セッションIDを保存・出力して後続処理で再利用できるようにしました。
  • 改善

    • モニタリングが既存セッションIDを優先して再開し、再開失敗時のフォールバック動作を導入しました。
    • プッシュパイプラインの一部ステップを一時的に無効化しました。
  • Chores

    • フックのビルドスクリプト、設定テンプレート、除外設定、関連ドキュメントを更新しました。

@coderabbitai

coderabbitai Bot commented Apr 3, 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: 6b661002-0d9e-45cc-9957-0e84457ba29f

📥 Commits

Reviewing files that changed from the base of the PR and between 37f418f and d895815.

⛔ Files ignored due to path filters (1)
  • .claude/hooks-session-start/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • .claude/hooks-config.toml
  • .claude/hooks-post-pr-monitor/src/main.rs
  • .claude/hooks-session-start/Cargo.toml
  • .claude/hooks-session-start/src/main.rs
  • .claude/settings.local.json.template
  • .gitignore
  • docs/todo.md
  • package.json
✅ Files skipped from review due to trivial changes (3)
  • .gitignore
  • .claude/hooks-session-start/Cargo.toml
  • docs/todo.md
🚧 Files skipped from review as they are similar to previous changes (5)
  • .claude/settings.local.json.template
  • package.json
  • .claude/hooks-config.toml
  • .claude/hooks-post-pr-monitor/src/main.rs
  • .claude/hooks-session-start/src/main.rs

📝 Walkthrough

Walkthrough

セッション開始時の SessionStart フックを追加し、セッション ID を環境ファイルと実行ファイル隣の .session-id に記録。post-PR モニターをその ID を使って claude -p --resume <id> で再開するよう変更。push パイプラインのステップをコメントアウト。

Changes

Cohort / File(s) Summary
SessionStart フック実装
.claude/hooks-session-start/Cargo.toml, .claude/hooks-session-start/src/main.rs
新規 Rust バイナリを追加。stdin の JSON から session_id を取得し、CLAUDE_ENV_FILE(あれば)に export 行を追記(重複回避)、実行ファイル隣の .session-id に first‑win で書き込み、hook 出力に additionalContext を返す。ユニットテストを含む。
post-PR モニター更新
.claude/hooks-post-pr-monitor/src/main.rs
fn get_main_session_id() -> Option<String> を追加。run_claude_with_stdin() のシグネチャを session_id: Option<&str> を受け取るよう変更し、存在時は claude -p --resume <id>、未指定時は claude -p --continue を使用。start_monitoring() にフェールバック(--resume 失敗時に --continue 再試行)とログ強化を追加。
設定・ビルド・パイプライン変更
.claude/hooks-config.toml, .claude/settings.local.json.template, package.json
push_pipeline.stepsintegration_test, ai_review, commit_refactor)をコメントアウトして無効化。hooks.SessionStart 設定を settings.local.json.template に追加。package.jsonbuild:hooks-session-start スクリプトを追加し、build:hooks の先頭に組み込むよう更新。
雑多なファイル更新
.gitignore, docs/todo.md
ビルド成果物 .claude/hooks-session-start.exe、ビルド出力ディレクトリ、ランタイム .claude/.session-id.gitignore に追加。docs/todo.md に Cron/セッション問題の既知問題を追記。

Sequence Diagram(s)

sequenceDiagram
    participant Claude as Claude (起動)
    participant Hook as SessionStart Hook
    participant Env as CLAUDE_ENV_FILE
    participant FS as File System (.session-id)
    participant Monitor as Post-PR Monitor

    Claude->>Hook: stdin: { "session_id": "xyz..." }
    Hook->>Env: append export CLAUDE_CODE_SESSION_ID='xyz...' (if set)
    Hook->>FS: write "xyz..." to .session-id (first-win)
    Hook->>Claude: stdout hookSpecificOutput { additionalContext: "CLAUDE_CODE_SESSION_ID=xyz..." }

    Note over Monitor: 後続のモニター起動時
    Monitor->>Monitor: get_main_session_id() reads env or .session-id
    alt session_id present
        Monitor->>Claude: run claude -p --resume <xyz...>
    else no session_id
        Monitor->>Claude: run claude -p --continue
    end
    Claude->>Monitor: response (success/failure)
    alt resume failed
        Monitor->>Claude: run claude -p --continue (fallback)
        Claude->>Monitor: response
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

🚥 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 プルリクエストのタイトルは、SessionStart フックの作成とセッション ID 伝播、CronCreate セッション問題の解決という主要な変更を正確に反映しており、プルリクエストの目的と一致しています。
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-config.toml:
- Around line 101-115: Revert the committed mass-commenting of
push_pipeline.steps in .claude/hooks-config.toml (restore the integration_test,
ai_review, commit_refactor entries) and instead implement a local-only bypass:
add a config flag or respect an environment variable (e.g.,
CLAUDE_LOCAL_SKIP_STEPS) that the push pipeline code checks; update the push
pipeline runner in main.rs (the code paths that interpret push_pipeline.steps /
execute_pipeline / run_steps) to skip executing steps only when that local
env/config flag is present, leaving committed config intact so CI/quality gates
run for others.

In @.claude/hooks-session-start/src/main.rs:
- Around line 57-61: The current format!-based interpolation of session_id into
output corrupts JSON and shell env lines; instead build the hookSpecificOutput
object using serde_json (e.g. construct a Map or use serde_json::json! and call
serde_json::to_string) to produce a safe JSON string for the variable output and
avoid manual string formatting of hookEventName/additionalContext; separately,
when producing the CLAUDE_ENV_FILE export lines (the code around the same block
and the lines flagged 66–84), shell-quote the session_id (use a proper
sh-quoting utility such as shlex::quote or wrap in single quotes and escape
internal single quotes) before embedding into the env export so the shell export
cannot be injected or corrupted. Ensure references: replace the format!-based
creation of output, protect session_id, and update the code that writes
CLAUDE_CODE_SESSION_ID / CLAUDE_ENV_FILE accordingly.
🪄 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: 9c7ec37f-dc23-41cf-acdd-0c8dcefa16cb

📥 Commits

Reviewing files that changed from the base of the PR and between 5f2aa95 and c4e797b.

⛔ Files ignored due to path filters (1)
  • .claude/hooks-session-start/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • .claude/hooks-config.toml
  • .claude/hooks-post-pr-monitor/src/main.rs
  • .claude/hooks-session-start/Cargo.toml
  • .claude/hooks-session-start/src/main.rs
  • .claude/settings.local.json.template
  • .gitignore
  • docs/todo.md
  • package.json

Comment thread .claude/hooks-config.toml Outdated
Comment on lines +101 to +115
# >>> TEMPORARY: 初回 push 用に steps 無効化 (push 後に復元) <<<
# [[push_pipeline.steps]]
# name = "integration_test"
# type = "command"
# cmd = "pnpm test"
#
# [[push_pipeline.steps]]
# name = "ai_review"
# type = "command"
# cmd = "pnpm review:ai"
#
# [[push_pipeline.steps]]
# name = "commit_refactor"
# type = "ai"
# prompt = "optimize_commit_structure"

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 | 🟠 Major

このコミット状態だと pnpm push の品質ゲートが完全に外れます。

Line 101-115 を全部コメントアウトすると、.claude/hooks-push-pipeline/src/main.rs:206-223 / 225-278 の実装上 steps=[] 扱いになり、テストも review:ai も実行せずそのまま jj git push --allow-new まで進みます。初回 push だけの一時回避なら、コミット済み設定ではなくローカル専用の切り替えにしたいです。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.claude/hooks-config.toml around lines 101 - 115, Revert the committed
mass-commenting of push_pipeline.steps in .claude/hooks-config.toml (restore the
integration_test, ai_review, commit_refactor entries) and instead implement a
local-only bypass: add a config flag or respect an environment variable (e.g.,
CLAUDE_LOCAL_SKIP_STEPS) that the push pipeline code checks; update the push
pipeline runner in main.rs (the code paths that interpret push_pipeline.steps /
execute_pipeline / run_steps) to skip executing steps only when that local
env/config flag is present, leaving committed config intact so CI/quality gates
run for others.

Comment thread .claude/hooks-session-start/src/main.rs
@aloekun
aloekun force-pushed the feat/session-start-hook branch from c4e797b to 37f418f Compare April 3, 2026 15:09

@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-session-start/src/main.rs:
- Around line 43-46: hook_input.session_id currently treats whitespace-only
strings as valid; change the match so you trim the incoming id and reject if
trimmed is empty (e.g., match Some(id) if !id.trim().is_empty() => use
id.trim().to_string(), _ => std::process::exit(0)). Ensure the stored session_id
is the trimmed value so CLAUDE_CODE_SESSION_ID and downstream
get_main_session_id() don't receive whitespace-only values that break --resume.
- Around line 121-137: The test write_to_env_file_creates_and_writes uses a
fixed tmp file name which causes collisions in parallel runs; change the test to
create a unique temp file (e.g., use tempfile::NamedTempFile or append a
uuid/pid/timestamp to std::env::temp_dir() path) and pass its path to
write_to_env_file so each test run uses an isolated file; ensure you still
remove/cleanup the file and keep the same assertions against the file contents.
🪄 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: f13b1187-e95b-483d-becb-b27598317f9e

📥 Commits

Reviewing files that changed from the base of the PR and between c4e797b and 37f418f.

⛔ Files ignored due to path filters (1)
  • .claude/hooks-session-start/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • .claude/hooks-config.toml
  • .claude/hooks-post-pr-monitor/src/main.rs
  • .claude/hooks-session-start/Cargo.toml
  • .claude/hooks-session-start/src/main.rs
  • .claude/settings.local.json.template
  • .gitignore
  • docs/todo.md
  • package.json
✅ Files skipped from review due to trivial changes (3)
  • .gitignore
  • .claude/hooks-session-start/Cargo.toml
  • docs/todo.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • .claude/settings.local.json.template
  • package.json
  • .claude/hooks-config.toml
  • .claude/hooks-post-pr-monitor/src/main.rs

Comment thread .claude/hooks-session-start/src/main.rs
Comment thread .claude/hooks-session-start/src/main.rs
@aloekun
aloekun force-pushed the feat/session-start-hook branch from 37f418f to d895815 Compare April 3, 2026 15:48
@aloekun

aloekun commented Apr 3, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 3, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@aloekun

aloekun commented Apr 3, 2026

Copy link
Copy Markdown
Owner Author

CodeRabbit レビュー上限到達のため新 PR に移行

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