Something went wrong. Try again.
Cursor plugin specification and official plugins
Something went wrong. Try again.
2.9 kB · 86 lines
Shell
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687#!/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 0fi
# Parse state file frontmatterFRONTMATTER=$(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 numericif [[ ! "$ITERATION" =~ ^[0-9]+$ ]]; then echo "Ralph loop: state file corrupted (iteration: '$ITERATION'). Stopping." >&2 rm -f "$STATE_FILE" "$DONE_FLAG" exit 0fi
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 0fi
# Check if completion promise was detected by the afterAgentResponse hookif [[ -f "$DONE_FLAG" ]]; then echo "Ralph loop: completion promise fulfilled at iteration $ITERATION." >&2 rm -f "$STATE_FILE" "$DONE_FLAG" exit 0fi
# Check max iterationsif [[ $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 0fi
# 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 0fi
# Increment iterationNEXT_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 promptif [[ "$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 loopjq -n --arg msg "$FOLLOWUP" '{"followup_message": $msg}'
exit 0