Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions plugins/setkernel/hooks/session-context.sh
Original file line number Diff line number Diff line change
@@ -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."

Expand Down
18 changes: 18 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading