-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpost-skill-auto-loop.sh
More file actions
executable file
·101 lines (90 loc) · 3.74 KB
/
post-skill-auto-loop.sh
File metadata and controls
executable file
·101 lines (90 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
# PostToolUse (Skill) Hook: Inject auto-loop directive after Skill completion
# When a skill like /deep-analyze creates docs, this hook reads the state file
# and outputs a directive to stdout (presented to model via PostToolUse output).
# This prevents the model from asking "要執行嗎?" after doc-producing skills.
#
# Defense-in-depth: Primary fixes are behavioral (SKILL.md + auto-loop rule).
# This hook reinforces the directive via hook infrastructure.
#
# Only outputs when there are pending review steps. Silent otherwise.
set -euo pipefail
# === Plugin-defers-to-local arbitration ===
_SELF_NAME="$(basename "$0")"
if [[ -n "${CLAUDE_PROJECT_DIR:-}" ]] \
&& [[ ! -f "${CLAUDE_PROJECT_DIR}/hooks/hooks.json" ]] \
&& [[ -x "${CLAUDE_PROJECT_DIR}/.claude/hooks/${_SELF_NAME}" ]]; then
_SETTINGS_MATCH=false
for _sf in "${CLAUDE_PROJECT_DIR}/.claude/settings.json" \
"${CLAUDE_PROJECT_DIR}/.claude/settings.local.json"; do
if [[ -f "$_sf" ]]; then
if command -v jq &>/dev/null; then
jq -e '.hooks // {} | .. | strings | select(contains(".claude/hooks/'"${_SELF_NAME}"'"))' "$_sf" >/dev/null 2>&1 \
&& _SETTINGS_MATCH=true && break
else
grep -q "\.claude/hooks/${_SELF_NAME}" "$_sf" 2>/dev/null \
&& _SETTINGS_MATCH=true && break
fi
fi
done
if [[ "$_SETTINGS_MATCH" == "true" ]]; then
exit 0 # Defer to local hook
fi
fi
STATE_FILE=".claude_review_state.json"
# Consume stdin (required by hook protocol)
cat > /dev/null
# Graceful degradation
if ! command -v jq &>/dev/null; then exit 0; fi
if [[ ! -f "$STATE_FILE" ]]; then exit 0; fi
# Read state
HAS_CODE=$(jq -r '.has_code_change // false' "$STATE_FILE" 2>/dev/null || echo "false")
HAS_DOC=$(jq -r '.has_doc_change // false' "$STATE_FILE" 2>/dev/null || echo "false")
CODE_PASSED=$(jq -r '.code_review.passed // false' "$STATE_FILE" 2>/dev/null || echo "false")
DOC_PASSED=$(jq -r '.doc_review.passed // false' "$STATE_FILE" 2>/dev/null || echo "false")
PRE_PASSED=$(jq -r '.precommit.passed // false' "$STATE_FILE" 2>/dev/null || echo "false")
# === Sidecar fail-closed marker ===
if [[ -f "${STATE_FILE}.blocked" ]]; then
CODE_PASSED="false"
DOC_PASSED="false"
PRE_PASSED="false"
# Fail-closed: if no change flags set, sidecar means state write failed — assume changes exist
[[ "$HAS_CODE" != "true" && "$HAS_DOC" != "true" ]] && { HAS_CODE="true"; HAS_DOC="true"; }
fi
# Stale-state reconciliation (one-way: true→false only, same as stop-guard/post-compact)
# Skip when sidecar present — would undo fail-closed HAS_* forcing
if [[ -f "${STATE_FILE}.blocked" ]]; then
GIT_PORCELAIN="__GIT_UNAVAILABLE__"
else
GIT_PORCELAIN=$(git status --porcelain -uno 2>/dev/null || echo "__GIT_UNAVAILABLE__")
fi
if [[ "$GIT_PORCELAIN" != "__GIT_UNAVAILABLE__" ]]; then
if [[ "$HAS_CODE" == "true" ]]; then
if ! echo "$GIT_PORCELAIN" | grep -qE '\.(ts|tsx|js|jsx|mjs|cjs|py|pyw|go|rs|java|kt|kts|rb|php|swift|c|cpp|cc|h|hpp|cs|scala|ex|exs)($|\s|")'; then
HAS_CODE="false"
fi
fi
if [[ "$HAS_DOC" == "true" ]]; then
if ! echo "$GIT_PORCELAIN" | grep -qE '\.(md|mdx)($|\s|")'; then
HAS_DOC="false"
fi
fi
fi
# Determine next required step
NEXT=""
if [[ "$HAS_CODE" == "true" && "$CODE_PASSED" != "true" ]]; then
NEXT="/codex-review-fast"
elif [[ "$HAS_CODE" == "true" && "$CODE_PASSED" == "true" && "$PRE_PASSED" != "true" ]]; then
NEXT="/precommit"
elif [[ "$HAS_DOC" == "true" && "$DOC_PASSED" != "true" ]]; then
NEXT="/codex-review-doc"
fi
# Only output when there is a pending step
if [[ -n "$NEXT" ]]; then
cat <<EOF
[AUTO_LOOP] Review state has pending step after skill completion.
Required: ${NEXT}
Do not ask "要執行嗎?" — execute ${NEXT} now per auto-loop rules.
EOF
fi
exit 0