From 45323c7b9f2af99ab2b4517fe0c8f72cdcf5351c Mon Sep 17 00:00:00 2001 From: orion-setkernel <341049454+orion-setkernel@users.noreply.github.com> Date: Wed, 11 Nov 2026 08:04:45 +0000 Subject: [PATCH] fix(session-hook): print the version holds outside a git work tree (#80) Fleet runs start in a run folder that is not a repository, and the hook exited before printing anything there. It now prints the heading, the org-rules line and the holds wherever it starts, and adds the branch and working-tree lines only inside a work tree. Output inside a work tree is unchanged. Co-Authored-By: Claude Opus 5.5 --- plugins/setkernel/hooks/session-context.sh | 37 +++++++++++----------- tests/run.sh | 18 +++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/plugins/setkernel/hooks/session-context.sh b/plugins/setkernel/hooks/session-context.sh index 4045da2..649239a 100755 --- a/plugins/setkernel/hooks/session-context.sh +++ b/plugins/setkernel/hooks/session-context.sh @@ -1,25 +1,26 @@ #!/bin/sh -# SessionStart: print the git state an agent would otherwise gather with several commands. -# Claude Code adds a SessionStart hook's stdout to the session context. Local git only, no -# network, POSIX sh (the agent VMs have no Node). Fails open: always exits 0. +# SessionStart: print the git state an agent would otherwise gather with several commands, and the +# org version holds. Claude Code adds a SessionStart hook's stdout to the session context. Local git +# only, no network, POSIX sh (the agent VMs have no Node). Fails open: always exits 0. +# Outside a git work tree (a fleet run folder) it still prints the org-rules line and the holds. holds="$(cd "$(dirname "$0")/.." 2>/dev/null && pwd)/policy/holds.md" -cd "${CLAUDE_PROJECT_DIR:-.}" 2>/dev/null || exit 0 -git rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0 - -branch=$(git branch --show-current 2>/dev/null) -head=$(git log -1 --format='%h %s (%cr)' 2>/dev/null) echo "# Session context (setkernel plugin)" -echo "- Branch \`${branch:-(detached)}\` at ${head:-(no commits)}" -# --no-optional-locks keeps this from taking the index lock another agent's git command may need. -changed=$(git --no-optional-locks status --short 2>/dev/null) -if [ -n "$changed" ]; then - n=$(printf '%s\n' "$changed" | wc -l | tr -d ' ') - echo "- Working tree: $n changed path(s); treat changes you did not make as someone else's work:" - printf '%s\n' "$changed" | head -n 15 | sed 's/^/ - /' - [ "$n" -gt 15 ] && echo " - … $((n - 15)) more (git status --short)" -else - echo "- Working tree: clean" +if cd "${CLAUDE_PROJECT_DIR:-.}" 2>/dev/null && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + branch=$(git branch --show-current 2>/dev/null) + head=$(git log -1 --format='%h %s (%cr)' 2>/dev/null) + echo "- Branch \`${branch:-(detached)}\` at ${head:-(no commits)}" + + # --no-optional-locks keeps this from taking the index lock another agent's git command may need. + changed=$(git --no-optional-locks status --short 2>/dev/null) + if [ -n "$changed" ]; then + n=$(printf '%s\n' "$changed" | wc -l | tr -d ' ') + echo "- Working tree: $n changed path(s); treat changes you did not make as someone else's work:" + printf '%s\n' "$changed" | head -n 15 | sed 's/^/ - /' + [ "$n" -gt 15 ] && echo " - … $((n - 15)) more (git status --short)" + else + echo "- Working tree: clean" + fi fi echo "- Follow the setkernel org rules and this repo's AGENTS.md; end with Blocked on owner / Changed / Found." diff --git a/tests/run.sh b/tests/run.sh index 9be3951..a79115e 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -289,6 +289,24 @@ expect 0 "Version holds (setkernel org policy)" "the session hook prints the hol env CLAUDE_PROJECT_DIR="$hookrepo" sh "$root/plugins/setkernel/hooks/session-context.sh" expect 0 "typescript-6.0" "the printed holds name each hold" -- \ env CLAUDE_PROJECT_DIR="$hookrepo" sh "$root/plugins/setkernel/hooks/session-context.sh" +expect 0 "^- Working tree: clean" "inside a work tree the hook prints the branch and working tree" -- \ + env CLAUDE_PROJECT_DIR="$hookrepo" sh "$root/plugins/setkernel/hooks/session-context.sh" +# Outside a work tree (a fleet run folder, #80): the heading, the org-rules line and the holds, no git lines. +# GIT_CEILING_DIRECTORIES keeps git from finding a repository above $tmp. +hookdir=$tmp/hooknotrepo +mkdir -p "$hookdir" +hook_out() { + env GIT_CEILING_DIRECTORIES="$tmp" CLAUDE_PROJECT_DIR="$hookdir" sh "$1/plugins/setkernel/hooks/session-context.sh" +} +for pattern in "^# Session context (setkernel plugin)" "^- Follow the setkernel org rules" "typescript-6.0"; do + expect 0 "$pattern" "outside a work tree the hook prints: $pattern" -- hook_out "$root" +done +expect 1 "" "outside a work tree the hook leaves out the branch and working-tree lines" -- \ + sh -c "env GIT_CEILING_DIRECTORIES='$tmp' CLAUDE_PROJECT_DIR='$hookdir' sh '$root/plugins/setkernel/hooks/session-context.sh' | grep -E '^- (Branch|Working tree)'" +rm "$tmp/copy/plugins/setkernel/policy/holds.md" +expect 0 "Follow the setkernel org rules" "without holds.md the hook still exits 0" -- hook_out "$tmp/copy" +expect 1 "" "without holds.md the hook prints nothing after the org-rules line" -- \ + sh -c "env GIT_CEILING_DIRECTORIES='$tmp' CLAUDE_PROJECT_DIR='$hookdir' sh '$tmp/copy/plugins/setkernel/hooks/session-context.sh' | sed '1,/Follow the setkernel org rules/d' | grep ." # The git guard (PreToolUse on Bash): blocks the forms a deny rule misses, lets ordinary work through. hooks=$root/plugins/setkernel/hooks