Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(claude): write hook markers without re-executing wt (#4619)
Worktrunk Bot4a3ac94b1a19
diff --git a/src/commands/config/plugins.rs b/src/commands/config/plugins.rsindex ad671c34b..c9d7aebb5 100644--- a/src/commands/config/plugins.rs+++ b/src/commands/config/plugins.rs@@ -162,8 +162,8 @@ struct PermissionRequest {/// (#3754). It never force-deletes, since Claude Code fires it on session/// exit for any clean worktree (#2939).///-/// Each action runs this `wt` binary again with the command a user would type,-/// so the hook behaves exactly as that command does.+/// Worktree actions run this `wt` binary again with the command a user would+/// type. Marker actions write through the discovered repository directly.pub fn handle_claude_hook() -> anyhow::Result<()> {let hook: ClaudeHook = serde_json::from_reader(std::io::stdin().lock()).context("Failed to parse Claude Code hook payload")?;@@ -173,18 +173,18 @@ pub fn handle_claude_hook() -> anyhow::Result<()> {};match hook {- ClaudeHook::UserPromptSubmit {} => set_marker(&project_dir, &["set", "π€"]),+ ClaudeHook::UserPromptSubmit {} => set_marker(&project_dir, Some("π€")),ClaudeHook::Notification {} | ClaudeHook::PreToolUse {} | ClaudeHook::Stop {} => {- set_marker(&project_dir, &["set", "π¬"])+ set_marker(&project_dir, Some("π¬"))}- ClaudeHook::SessionEnd {} => set_marker(&project_dir, &["clear"]),+ ClaudeHook::SessionEnd {} => set_marker(&project_dir, None),ClaudeHook::PermissionRequest(request) => {// A payload `wt` can't resolve, such as a `cwd` in no repository,// declines, leaving the dialog.if approves_enter_worktree(&request).unwrap_or(false) {return print_allow_decision();}- set_marker(&project_dir, &["set", "π¬"])+ set_marker(&project_dir, Some("π¬"))}ClaudeHook::WorktreeCreate { name } => {// A directory no repository resolves from falls through to@@ -248,15 +248,26 @@ fn print_allow_decision() -> anyhow::Result<()> {}))}-/// Sets or clears the activity marker of the worktree at `dir`, ignoring-/// failure.-fn set_marker(dir: &Path, args: &[&str]) -> anyhow::Result<()> {- let dir = dir.to_string_lossy();- let _ = run_wt(- Cmd::new(wt_binary()?)- .args(["-C", dir.as_ref(), "config", "state", "marker"])- .args(args.iter().copied()),- );+/// Sets or clears the activity marker in the session's launch worktree.+/// Marker failures never fail a Claude hook.+fn set_marker(dir: &Path, marker: Option<&str>) -> anyhow::Result<()> {+ let _ = (|| -> anyhow::Result<()> {+ let repo = Repository::at(dir)?;+ let Some(branch) = repo.current_worktree().branch()? else {+ return Ok(());+ };+ let key = format!("worktrunk.state.{branch}.marker");+ if let Some(marker) = marker {+ let value = serde_json::json!({+ "marker": marker,+ "set_at": worktrunk::utils::epoch_now(),+ });+ repo.set_config(&key, &value.to_string())?;+ } else {+ repo.unset_config(&key)?;+ }+ Ok(())+ })();Ok(())}diff --git a/tests/integration_tests/claude_hook.rs b/tests/integration_tests/claude_hook.rsindex c87817bc6..9e895e9e6 100644--- a/tests/integration_tests/claude_hook.rs+++ b/tests/integration_tests/claude_hook.rs@@ -71,6 +71,10 @@ fn test_claude_hook_markers_follow_project_dir(mut repo: TestRepo) {let stop = run_hook(&repo, &feature, &root, r#"{"hook_event_name":"Stop"}"#);assert!(stop.status.success());+ assert!(+ stop.stderr.is_empty(),+ "marker hook wrote to stderr: {stop:?}"+ );assert_eq!(marker(&repo, &root), "π¬");let end = run_hook(@@ -97,6 +101,29 @@ fn test_claude_hook_marker_failure_succeeds(repo: TestRepo) {assert_eq!(stdout(&output), "");}+/// A detached worktree has no branch to attach a marker to.+#[rstest]+fn test_claude_hook_marker_detached_worktree(repo: TestRepo) {+ let root = repo.root_path();+ let prompt = run_hook(+ &repo,+ root,+ root,+ r#"{"hook_event_name":"UserPromptSubmit"}"#,+ );+ assert!(prompt.status.success(), "{prompt:?}");+ assert_eq!(marker(&repo, root), "π€");++ repo.run_git(&["checkout", "--detach"]);+ let output = run_hook(&repo, root, root, r#"{"hook_event_name":"Stop"}"#);+ assert!(output.status.success(), "{output:?}");+ assert!(output.stderr.is_empty(), "{output:?}");+ assert_eq!(stdout(&output), "");++ repo.run_git(&["checkout", "main"]);+ assert_eq!(marker(&repo, root), "π€");+}+/// `PermissionRequest` answers an `EnterWorktree` into a managed worktree with/// the allow decision and leaves the marker unset; any other request prints/// nothing, so the dialog stays, and marks the session π¬ while it waits.