chore(claude): add repo conventions and agent guardrails
This commit is contained in:
1 parent
b56ea412de
commit
6d404e2b21
9 files changed
+411
No files matched your search
Executable
+26
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env bash
|
||||
# Stop hook: makes Claude fix lint errors before it can finish a reply.
|
||||
#
|
||||
# When YAML files have uncommitted changes, run yamllint and ansible-lint from
|
||||
# .venv. On failure, exit 2 sends the errors back to Claude. It never contacts a
|
||||
# host. If .venv doesn't exist it does nothing, and CI still runs both linters.
|
||||
input=$(cat)
|
||||
# Set when Claude is already continuing because of this hook; stopping here
|
||||
# prevents an endless loop if it can't fix the error.
|
||||
[[ "$(jq -r '.stop_hook_active // false' <<<"$input")" == "true" ]] && exit 0
|
||||
cd "$CLAUDE_PROJECT_DIR" || exit 0
|
||||
|
||||
[[ -x .venv/bin/yamllint && -x .venv/bin/ansible-lint ]] || exit 0
|
||||
# -uall lists files inside new folders, not just the folder.
|
||||
git status --porcelain -uall | grep -qE '\.ya?ml$|\.j2$|\.ansible-lint$' || exit 0
|
||||
|
||||
failed=0
|
||||
for check in ".venv/bin/yamllint --strict ." ".venv/bin/ansible-lint"; do
|
||||
if ! out=$($check 2>&1); then
|
||||
echo "'$check' failed, fix before finishing:" >&2
|
||||
tail -n 30 <<<"$out" >&2
|
||||
failed=1
|
||||
fi
|
||||
done
|
||||
[[ $failed -eq 1 ]] && exit 2
|
||||
exit 0
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env bash
|
||||
# PreToolUse hook for Bash: forces a permission prompt before any command that
|
||||
# can reach or change a live host. The ask rules in .claude/settings.json match
|
||||
# the command text as Claude usually writes it; this catches the same commands
|
||||
# when they are wrapped (sh -c '...', env VAR=x ..., absolute paths).
|
||||
set -euo pipefail
|
||||
|
||||
input=$(cat)
|
||||
cmd=$(jq -r '.tool_input.command // empty' <<<"$input")
|
||||
|
||||
[ -z "$cmd" ] && exit 0
|
||||
[[ $cmd =~ ^ansible[[:space:]]+--version[[:space:]]*$ ]] && exit 0
|
||||
|
||||
# A tool name must start a word. "/" is allowed before it to catch absolute
|
||||
# paths, "-" is not, so the folder name homelab-ansible doesn't match.
|
||||
start='(^|[^[:alnum:]_.-])'
|
||||
end="([[:space:]\"')]|\$)"
|
||||
ansible_re="${start}(ansible-playbook|ansible-pull|ansible-console|ansible-vault|ansible)[[:space:]]"
|
||||
remote_re="${start}(ssh|scp|sftp|rsync)[[:space:]]"
|
||||
proxmox_re="${start}(pct|qm|pvesh|pvesm|vzdump)[[:space:]]"
|
||||
docker_re="${start}docker([[:space:]]+compose)?[[:space:]]+([^|;&]*[[:space:]])?(up|down|start|stop|restart|kill|rm|rmi|run|exec|pull|push|create|build|prune|cp|update|rename|pause|unpause|volume|network|system)$end"
|
||||
|
||||
what=""
|
||||
if [[ $cmd =~ $ansible_re ]]; then
|
||||
what="${BASH_REMATCH[2]}"
|
||||
elif [[ $cmd =~ $remote_re ]]; then
|
||||
what="${BASH_REMATCH[2]} to a host"
|
||||
elif [[ $cmd =~ $proxmox_re ]]; then
|
||||
what="${BASH_REMATCH[2]} on Proxmox"
|
||||
elif [[ $cmd =~ $docker_re ]]; then
|
||||
what="docker ${BASH_REMATCH[4]}"
|
||||
fi
|
||||
|
||||
[ -z "$what" ] && exit 0
|
||||
|
||||
reason="Live infrastructure: '$what'. Approve only if you want Claude to run this instead of running it yourself."
|
||||
|
||||
jq -n --arg reason "$reason" '{
|
||||
hookSpecificOutput: {
|
||||
hookEventName: "PreToolUse",
|
||||
permissionDecision: "ask",
|
||||
permissionDecisionReason: $reason
|
||||
}
|
||||
}'
|
||||
Reference in new issue
Block a user