worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Claude Code hook: set the marker in-process, and allow async for marker-only events

Open max-sixty opened this issue 1 month ago • 4 comments

wt config plugins claude hook runs synchronously on UserPromptSubmit, Stop, Notification, PreToolUse (AskUserQuestion) and PermissionRequest, so Claude Code waits for it each time. In a repository with 88 worktrees on a Linux host, one Stop run takes about 140 ms. Over a week of sessions alongside other plugins' hooks, it was the slowest synchronous Stop hook at about half of turn ends.

Most of that time is process and git overhead, not the marker write. Averages of 10 untraced runs:

Step Time
Whole hook, Stop event 141 ms
The wt -C <repo> config state marker set 💬 it re-execs 94 ms
git worktree list alone 31 ms
The git config marker write alone 21 ms

An strace -f -e trace=execve of one Stop run shows the sequence. The hook process discovers the repository with git --version, git rev-parse ... and git config --list -z. It then re-execs wt -C <repo> config state marker set, which runs the same three git calls again. The child also runs git worktree list --porcelain -z and git rev-parse --git-dir twice before the git config write.

Suggestions:

  1. Set the marker in the hook process instead of re-execing wt. Reusing the discovery the process has already done removes about 47 ms per run. The child's git worktree list and duplicate rev-parse --git-dir look unneeded for writing one branch's marker. Skipping them as well should bring a run close to one discovery plus the 21 ms write. That second part is inferred, not measured.

  2. Mark the marker-only registrations "async": true in the plugin's hooks.json: UserPromptSubmit, Notification, PreToolUse (AskUserQuestion) and Stop. Claude Code 2.1.310 backgrounds an async command hook about 17 ms after spawning it. It drops the hook's decisions and plain-text output. These events print nothing on stdout, so nothing is lost. PermissionRequest has to stay synchronous, because it answers the EnterWorktree permission prompt. WorktreeCreate and WorktreeRemove also stay synchronous, because Claude Code uses their output and exit status.

    Async needs ordered marker writes first. Today Claude Code waits for the Stop write before it starts a queued prompt's hooks. With async, a prompt queued at turn end can start its 🤖 write while the Stop's 💬 write is still running, and whichever finishes last wins. That leaves the wrong emoji until the next event. Writes that start together also contend on config.lock: in a scratch probe, 40 simultaneous starts gave 30 lock failures. One way to make the end state independent of completion order:

    • stamp set_at in milliseconds when the hook starts;
    • skip the write when the stored set_at is newer;
    • retry briefly when config.lock is held.

Seen on worktrunk wt v0.81.0.

This was written by Claude Code on behalf of max-sixty

max-sixty avatar Oct 28 '26 03:10 max-sixty

I opened #4619 for the in-process marker write. It removes the nested wt invocation from Claude Code marker events, and the five focused hook integration tests pass. The review run pushed a formatting correction; checks on that new head are still pending, so the PR remains for maintainer review.

The async registrations remain open in this issue. They need a tested ordering rule for overlapping marker events and handling for Git config lock contention before backgrounding writes is safe.

worktrunk-bot avatar Oct 28 '26 04:10 worktrunk-bot

#4619 merged on Nov 1, so marker events now write through the repository the hook process already discovered, with no nested wt. What remains here is the "async": true registrations and the ordered writes they depend on. Before I build that, two choices in the proposed ordering rule change stored state or locking, so they need your call:

  • Millisecond set_at. set_at is stored in seconds and read as seconds: wt config state renders it as a relative age, and the docs show it in hand-written values. Switching it to milliseconds changes a stored format. A separate field used only for ordering, such as seq_ms, would leave set_at as it is.
  • "Skip when the stored value is newer." In set_marker, the check and the write are separate git calls. config.lock serializes only the write, so two overlapping hooks can both pass the check, and the one that finishes last still wins. To make the result independent of completion order, the read and write need their own lock, such as an fs2 file lock under .git/wt/ (fs2 is already a dependency). Retrying on config.lock alone fixes the lock failures but not the ordering.

The hooks.json change itself can't be checked from CI because there's no Claude Code session to drive. A PR would cover the ordering and locking with tests and leave the async behavior for you to confirm locally. If the field-plus-lock approach works for you, I'll open that PR.

worktrunk-bot avatar Nov 22 '26 09:11 worktrunk-bot

@worktrunk-bot please register all five marker events (UserPromptSubmit, Notification, PreToolUse for AskUserQuestion, PermissionRequest, Stop) as "async": true, and retry a marker write that fails on config.lock. Skip the seq_ms field and the file lock: within one session these events arrive a turn or a minute apart while a run takes about 40 ms, so overlapping runs are rare, and an out-of-order write corrects itself at the next event. Sessions in different worktrees write different branches' keys.

This was written by Claude Code on behalf of max-sixty

max-sixty avatar Nov 28 '26 03:11 max-sixty

Opened #4932. It registers UserPromptSubmit, Notification, PreToolUse (AskUserQuestion) and Stop as "async": true, and a marker write that hits a held config.lock now retries for up to 1 s. There's no ordering field and no file lock.

I left PermissionRequest synchronous, which differs from what you asked for. That hook also prints the allow decision that skips the EnterWorktree dialog for worktrunk-managed worktrees, and Claude Code drops an async hook's decisions. Making it async would bring that dialog back. If you'd rather have it async anyway, it's a one-line change on the PR.

Git 2.56 now retries config.lock by itself for up to 1 s (core.configLockTimeout), so the new retry only adds time on older Gits.

worktrunk-bot avatar Nov 28 '26 04:11 worktrunk-bot