pro-workflow icon indicating copy to clipboard operation
pro-workflow copied to clipboard

WorktreeCreate hook fails every isolated agent spawn: echoes stdin JSON as the worktree path

Open gepenniman opened this issue 4 months ago • 0 comments

Summary

With pro-workflow installed, every Agent tool spawn that uses worktree isolation fails immediately:

WorktreeCreate hook returned a path that is not a directory: {"session_id":"...","transcript_path":"...","cwd":"...","prompt_id":"...","hook_event_name":"WorktreeCreate","name":"agent-..."}. The hook must create the directory before echoing its path.

The plugin registers a WorktreeCreate hook (hooks/hooks.json) that runs scripts/worktree-create.js. That script is meant to log worktree creation, but it ends with console.log(data), echoing the entire stdin JSON back on stdout. Claude Code treats WorktreeCreate stdout as the path of the worktree the hook created, so the echoed JSON blob is validated as a path and every spawn dies. This blocks all isolated subagent work for anyone running the plugin.

Environment

  • Claude Code 2.1.201, macOS (darwin 25.5)
  • pro-workflow at 9fc35f5

The WorktreeCreate contract (found empirically)

  1. If a WorktreeCreate hook is registered, it owns worktree creation. Stdout must be exactly the path of a directory that already exists.
  2. Empty stdout is also an error: WorktreeCreate hook failed: hook succeeded but returned no worktree path (command: echo the path to stdout; http/callback: return hookSpecificOutput.worktreePath). So the hook cannot simply stay quiet and let the harness do it.
  3. The harness does no git setup in the returned directory. Returning a plain mkdir -p directory makes spawns "succeed" but agents land in an empty non-git folder.
  4. The input payload has no worktree path or branch. It provides session_id, transcript_path, cwd, prompt_id, hook_event_name, and name. The current script reads input.worktree_path and input.branch, which never exist, so the log records "unknown" for both.

Repro

echo '{"session_id":"x","hook_event_name":"WorktreeCreate","name":"agent-test","cwd":"/path/to/any/git/repo"}' \
  | node scripts/worktree-create.js

Observe: stdout is the input JSON echoed back, and no directory is created. Any Agent spawn with isolation: "worktree" then fails with the error above.

Two ways to fix it (maintainer's call)

Option A: make the hooks actually own creation and cleanup. The create script runs git -C <cwd> worktree add -b agents/<name>-<timestamp> into a temp location, falls back to a plain directory when the session is not in a git repo, keeps the logging on stderr and in worktrees.json, and echoes only the created path. The remove script tears the worktree, branch, and ledger entry back down. I have this working and verified end to end (spawned agent lands in a real checkout: pwd equals git rev-parse --show-toplevel, files present). PR attached covering both scripts.

Option B: drop the WorktreeCreate and WorktreeRemove entries from hooks/hooks.json. With no hook registered, the harness creates worktrees itself, exactly as it does without the plugin. The SubagentStart and SubagentStop hooks already log spawn lifecycle, so the tracking loss is small. This is the smaller change if you would rather not own worktree creation in a logging plugin.

Happy to rework the PR into Option B if you prefer.

Related: WorktreeRemove never cleans up

scripts/worktree-remove.js has the same shape: it logs and echoes stdin, and never removes anything. Once WorktreeCreate hands out real worktrees (Option A), each spawn would leave a registered worktree and an agents/* branch behind until someone runs git worktree prune. The attached PR fixes this side too: best-effort git worktree remove --force on the payload's worktree_path, delete the matching agents/* branch, prune, always exit 0. Option B makes all of this moot.

gepenniman avatar Jul 06 '26 00:07 gepenniman