summaryrefslogtreecommitdiff
path: root/plugins/ralph-loop/hooks/stop-hook.sh
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/ralph-loop/hooks/stop-hook.sh')
-rwxr-xr-xplugins/ralph-loop/hooks/stop-hook.sh14
1 files changed, 10 insertions, 4 deletions
diff --git a/plugins/ralph-loop/hooks/stop-hook.sh b/plugins/ralph-loop/hooks/stop-hook.sh
index 7edf13b7..c6f73ce6 100755
--- a/plugins/ralph-loop/hooks/stop-hook.sh
+++ b/plugins/ralph-loop/hooks/stop-hook.sh
@@ -21,8 +21,10 @@ fi
FRONTMATTER=$(sed -n '/^---$/,/^---$/{ /^---$/d; p; }' "$RALPH_STATE_FILE")
ITERATION=$(echo "$FRONTMATTER" | grep '^iteration:' | sed 's/iteration: *//')
MAX_ITERATIONS=$(echo "$FRONTMATTER" | grep '^max_iterations:' | sed 's/max_iterations: *//')
-# Extract completion_promise and strip surrounding quotes if present
-COMPLETION_PROMISE=$(echo "$FRONTMATTER" | grep '^completion_promise:' | sed 's/completion_promise: *//' | sed 's/^"\(.*\)"$/\1/')
+# Extract completion_promise, strip surrounding quotes if present, then
+# normalize whitespace (trim + collapse runs to a single space) — the observed
+# <promise> text is normalized the same way below, so both sides must match.
+COMPLETION_PROMISE=$(echo "$FRONTMATTER" | grep '^completion_promise:' | sed 's/completion_promise: *//' | sed 's/^"\(.*\)"$/\1/' | perl -0777 -pe 's/^\s+|\s+$//g; s/\s+/ /g')
# Session isolation: the state file is project-scoped, but the Stop hook
# fires in every Claude Code session in that project. If another session
@@ -130,7 +132,9 @@ if [[ "$COMPLETION_PROMISE" != "null" ]] && [[ -n "$COMPLETION_PROMISE" ]]; then
# Extract text from <promise> tags using Perl for multiline support
# -0777 slurps entire input, s flag makes . match newlines
# .*? is non-greedy (takes FIRST tag), whitespace normalized
- PROMISE_TEXT=$(echo "$LAST_OUTPUT" | perl -0777 -pe 's/.*?<promise>(.*?)<\/promise>.*/$1/s; s/^\s+|\s+$//g; s/\s+/ /g' 2>/dev/null || echo "")
+ # Prints ONLY when a <promise> tag is present — output without the tag
+ # (e.g. a bare "DONE") must never count as the promise.
+ PROMISE_TEXT=$(echo "$LAST_OUTPUT" | perl -0777 -ne 'if (/<promise>(.*?)<\/promise>/s) { my $t = $1; $t =~ s/^\s+|\s+$//g; $t =~ s/\s+/ /g; print $t; }' 2>/dev/null || echo "")
# Use = for literal string comparison (not pattern matching)
# == in [[ ]] does glob pattern matching which breaks with *, ?, [ characters
@@ -166,7 +170,9 @@ fi
# Update iteration in frontmatter (portable across macOS and Linux)
# Create temp file, then atomically replace
TEMP_FILE="${RALPH_STATE_FILE}.tmp.$$"
-sed "s/^iteration: .*/iteration: $NEXT_ITERATION/" "$RALPH_STATE_FILE" > "$TEMP_FILE"
+# Match "iteration:" with or without spaces after the colon, like the reader
+# above — a stricter pattern here would silently no-op and freeze the counter.
+sed "s/^iteration:.*/iteration: $NEXT_ITERATION/" "$RALPH_STATE_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$RALPH_STATE_FILE"
# Build system message with iteration count and completion promise info