-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathstop-hook.sh
More file actions
executable file
·86 lines (68 loc) · 2.79 KB
/
stop-hook.sh
File metadata and controls
executable file
·86 lines (68 loc) · 2.79 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
#!/bin/bash
# Ralph Loop stop hook.
# When the agent finishes a turn, this hook decides whether to feed the
# same prompt back for another iteration or let the session end.
#
# Cursor stop hook API:
# Input: { "status": "completed"|"aborted"|"error", "loop_count": N, ...common }
# Output: { "followup_message": "<text>" } to continue, or exit 0 with no output to stop
set -euo pipefail
HOOK_INPUT=$(cat)
PROJECT_DIR="${CURSOR_PROJECT_DIR:-.}"
STATE_FILE="$PROJECT_DIR/.cursor/ralph/scratchpad.md"
DONE_FLAG="$PROJECT_DIR/.cursor/ralph/done"
# No active loop. Let the session end.
if [[ ! -f "$STATE_FILE" ]]; then
exit 0
fi
# Parse state file frontmatter
FRONTMATTER=$(sed -n '/^---$/,/^---$/{ /^---$/d; p; }' "$STATE_FILE")
ITERATION=$(echo "$FRONTMATTER" | grep '^iteration:' | sed 's/iteration: *//')
MAX_ITERATIONS=$(echo "$FRONTMATTER" | grep '^max_iterations:' | sed 's/max_iterations: *//')
COMPLETION_PROMISE=$(echo "$FRONTMATTER" | grep '^completion_promise:' | sed 's/completion_promise: *//' | sed 's/^"\(.*\)"$/\1/')
# Validate iteration is numeric
if [[ ! "$ITERATION" =~ ^[0-9]+$ ]]; then
echo "Ralph loop: state file corrupted (iteration: '$ITERATION'). Stopping." >&2
rm -f "$STATE_FILE" "$DONE_FLAG"
exit 0
fi
if [[ ! "$MAX_ITERATIONS" =~ ^[0-9]+$ ]]; then
echo "Ralph loop: state file corrupted (max_iterations: '$MAX_ITERATIONS'). Stopping." >&2
rm -f "$STATE_FILE" "$DONE_FLAG"
exit 0
fi
# Check if completion promise was detected by the afterAgentResponse hook
if [[ -f "$DONE_FLAG" ]]; then
echo "Ralph loop: completion promise fulfilled at iteration $ITERATION." >&2
rm -f "$STATE_FILE" "$DONE_FLAG"
exit 0
fi
# Check max iterations
if [[ $MAX_ITERATIONS -gt 0 ]] && [[ $ITERATION -ge $MAX_ITERATIONS ]]; then
echo "Ralph loop: max iterations ($MAX_ITERATIONS) reached." >&2
rm -f "$STATE_FILE" "$DONE_FLAG"
exit 0
fi
# Extract prompt text (everything after the closing --- in frontmatter)
PROMPT_TEXT=$(awk '/^---$/{i++; next} i>=2' "$STATE_FILE")
if [[ -z "$PROMPT_TEXT" ]]; then
echo "Ralph loop: no prompt text found in state file. Stopping." >&2
rm -f "$STATE_FILE" "$DONE_FLAG"
exit 0
fi
# Increment iteration
NEXT_ITERATION=$((ITERATION + 1))
TEMP_FILE="${STATE_FILE}.tmp.$$"
sed "s/^iteration: .*/iteration: $NEXT_ITERATION/" "$STATE_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$STATE_FILE"
# Build the followup message: iteration context + original prompt
if [[ "$COMPLETION_PROMISE" != "null" ]] && [[ -n "$COMPLETION_PROMISE" ]]; then
HEADER="[Ralph loop iteration $NEXT_ITERATION. To complete: output <promise>$COMPLETION_PROMISE</promise> ONLY when genuinely true.]"
else
HEADER="[Ralph loop iteration $NEXT_ITERATION.]"
fi
FOLLOWUP="$HEADER
$PROMPT_TEXT"
# Output followup_message to continue the loop
jq -n --arg msg "$FOLLOWUP" '{"followup_message": $msg}'
exit 0