From: Jakub Czajka Date: Sun, 22 Nov 2026 21:03:32 +0000 (+0000) Subject: [ai] Fix block-worktrees responses and path checks. X-Git-Url: https://git.ekhem.eu.org/?a=commitdiff_plain;h=4b3f88098fe6c4dfdc6db4a909ca846786ed0f1d;p=guix.git [ai] Fix block-worktrees responses and path checks. Resolve git from PATH and return early outside a worktree, so a missing worktree cannot block unrelated tools. Derive the project root from git-common-dir, add hookEventName to hook responses, use updatedInput for ExitWorktree, and match project-root writes only at a path boundary. --- diff --git a/.claude/hooks/block-worktrees b/.claude/hooks/block-worktrees index 6d75e05..94f53ca 100755 --- a/.claude/hooks/block-worktrees +++ b/.claude/hooks/block-worktrees @@ -10,41 +10,53 @@ # Silently converts ExitWorktree remove→keep so worktrees are # never deleted. +# Only active inside a worktree. +case "$PWD" in + */worktrees/*) ;; + *) exit 0 ;; +esac + # Pre-flight: critical environment variables. if [ -z "$GUIX_BIN" ]; then echo '{"systemMessage":"❌ block-worktrees: GUIX_BIN not set"}' >&2 exit 0 fi if [ -z "$CLAUDE_CODE_SESSION_ID" ]; then - echo '{"systemMessage":"❌ block-worktrees: CLAUDE_CODE_SESSION_ID not set"}' >&2 + echo '{"systemMessage":' \ + '"❌ block-worktrees: CLAUDE_CODE_SESSION_ID not set"}' >&2 exit 0 fi # Pre-flight: commands referenced by this hook. if [ ! -x "$GUIX_BIN/sed" ]; then - echo '{"hookSpecificOutput":{"permissionDecision":"deny",' \ - '"permissionDecisionReason":"block-worktrees: GUIX_BIN/sed missing",' \ - '"systemMessage":"❌ block-worktrees broken: GUIX_BIN/sed not found"}}' + echo '{"systemMessage":' \ + '"❌ block-worktrees broken: GUIX_BIN/sed not found",' \ + '"hookSpecificOutput":{' \ + '"hookEventName":"PreToolUse",' \ + '"permissionDecision":"deny",' \ + '"permissionDecisionReason":' \ + '"block-worktrees: GUIX_BIN/sed missing"}}' exit 0 fi if [ ! -x "$GUIX_BIN/echo" ]; then - echo '{"hookSpecificOutput":{"permissionDecision":"deny",' \ - '"permissionDecisionReason":"block-worktrees: GUIX_BIN/echo missing",' \ - '"systemMessage":"❌ block-worktrees broken: GUIX_BIN/echo not found"}}' + echo '{"systemMessage":' \ + '"❌ block-worktrees broken: GUIX_BIN/echo not found",' \ + '"hookSpecificOutput":{' \ + '"hookEventName":"PreToolUse",' \ + '"permissionDecision":"deny",' \ + '"permissionDecisionReason":' \ + '"block-worktrees: GUIX_BIN/echo missing"}}' exit 0 fi -GIT_BIN="$HOME/.guix-profile/bin/git" -if [ ! -x "$GIT_BIN" ]; then - echo '{"hookSpecificOutput":{"permissionDecision":"deny",' \ - '"permissionDecisionReason":"block-worktrees: git missing",' \ - '"systemMessage":"❌ block-worktrees broken: git not found"}}' - exit 0 -fi - -# Only active inside a worktree. -if ! echo "$PWD" \ - | $GUIX_BIN/sed --quiet '\|/worktrees/|q0;$q1' -then +GIT_BIN=$(command -v git) +if [ -z "$GIT_BIN" ] || [ ! -x "$GIT_BIN" ]; then + echo '{"systemMessage":' \ + '"❌ block-worktrees broken: git not found",' \ + '"hookSpecificOutput":{' \ + '"hookEventName":"PreToolUse",' \ + '"permissionDecision":"deny",' \ + '"permissionDecisionReason":' \ + '"block-worktrees: git missing"}}' exit 0 fi @@ -52,13 +64,17 @@ stdin=$(cat) tool=$(echo "$stdin" \ | $GUIX_BIN/sed -n \ 's/.*"tool_name"[[:space:]]*:[[:space:]]*"\([^"]\+\)".*/\1/p') -proj_root=$($GIT_BIN -C "$PWD" rev-parse --show-toplevel 2>/dev/null) -[ -z "$proj_root" ] && exit 0 +common_dir=$($GIT_BIN -C "$PWD" rev-parse --path-format=absolute \ + --git-common-dir 2>/dev/null) +[ -z "$common_dir" ] && exit 0 +proj_root=${common_dir%/.git} +[ "$proj_root" = "$common_dir" ] && exit 0 worktree="$proj_root/.claude/worktrees/$CLAUDE_CODE_SESSION_ID" die() { - echo \ - '{"hookSpecificOutput":{"permissionDecision":"deny",' \ + echo '{"hookSpecificOutput":' \ + '{"hookEventName":"PreToolUse",' \ + '"permissionDecision":"deny",' \ '"permissionDecisionReason":"'"$1"'"}}' exit 0 } @@ -72,10 +88,10 @@ ExitWorktree) | $GUIX_BIN/sed -n \ 's/.*"action"[[:space:]]*:[[:space:]]*"\([^"]\+\)".*/\1/p') if [ "$action" = "remove" ]; then - echo \ - '{"hookSpecificOutput":{' \ + echo '{"hookSpecificOutput":' \ + '{"hookEventName":"PreToolUse",' \ '"permissionDecision":"allow",' \ - '"modifiedInput":{"action":"keep"}}}' + '"updatedInput":{"action":"keep"}}}' exit 0 fi ;; @@ -84,7 +100,7 @@ Write|Edit) | $GUIX_BIN/sed -n \ 's/.*"file_path"[[:space:]]*:[[:space:]]*"\([^"]\+\)".*/\1/p') case "$path" in - "$proj_root"*) + "$proj_root"|"$proj_root"/*) if ! echo "$path" \ | $GUIX_BIN/sed --quiet "\|$worktree|q0;\$q1" then