Skip to content

fix(ralph-wiggum): make stop hook's jq error handling reachable under set -e#77443

Open
Yigtwxx wants to merge 1 commit into
anthropics:mainfrom
Yigtwxx:fix/ralph-wiggum-stop-hook-set-e
Open

fix(ralph-wiggum): make stop hook's jq error handling reachable under set -e#77443
Yigtwxx wants to merge 1 commit into
anthropics:mainfrom
Yigtwxx:fix/ralph-wiggum-stop-hook-set-e

Conversation

@Yigtwxx

@Yigtwxx Yigtwxx commented Jul 14, 2026

Copy link
Copy Markdown

Problem

plugins/ralph-wiggum/hooks/stop-hook.sh runs under set -euo pipefail and parses the last assistant transcript line with:

LAST_OUTPUT=$(echo "$LAST_LINE" | jq -r '...' 2>&1)

# Check if jq succeeded
if [[ $? -ne 0 ]]; then
  # friendly message, rm state file, exit 0

A plain VAR=$(cmd) assignment takes cmd's exit status, so if jq fails to parse (malformed/truncated transcript line), set -e aborts the script at the assignment — the $? check and the whole recovery branch below it are unreachable. Instead of the designed graceful stop (message to the user, .claude/ralph-loop.local.md cleaned up, exit 0), the hook dies with an unhandled nonzero exit and leaves the loop state file behind.

Fix

Guard the assignment so the failure is captured instead of fatal:

JQ_EXIT=0
LAST_OUTPUT=$(echo "$LAST_LINE" | jq -r '...' 2>&1) || JQ_EXIT=$?

if [[ $JQ_EXIT -ne 0 ]]; then

Behavior is otherwise identical: 2>&1 still captures jq's error text into LAST_OUTPUT for the diagnostic message.

Verification

  • bash -n passes.
  • Reproduced the pattern under set -euo pipefail: with the guard, a failing command substitution reaches the error branch with the right exit code; without it, the shell exits at the assignment.

The stop hook runs with set -euo pipefail. When jq failed to parse
the last transcript line, the LAST_OUTPUT assignment itself exited
nonzero and set -e aborted the script right there, so the recovery
branch below (friendly message, state-file cleanup, exit 0) never
ran and the loop state file was left behind. Guard the assignment
with '|| JQ_EXIT=$?' so the existing error branch executes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant