Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions plugins/ralph-loop/CLAUDE.md

This file was deleted.

51 changes: 0 additions & 51 deletions plugins/ralph-loop/hooks/CLAUDE.md

This file was deleted.

4 changes: 2 additions & 2 deletions plugins/ralph-loop/hooks/hooks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"hooks": {
"SessionStart": [
{
"matcher": "ralph-loop-active",
"matcher": "*",
"hooks": [
{
"type": "command",
Expand All @@ -15,7 +15,7 @@
],
"Stop": [
{
"matcher": "ralph-loop-active",
"matcher": "*",
"hooks": [
{
"type": "command",
Expand Down
11 changes: 9 additions & 2 deletions plugins/ralph-loop/hooks/session-start-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../scripts/lib/state.sh"

# Extract session_id from stdin (remove useless use of cat)
SESSION_ID=$(jq -r '.session_id')
# Extract session_id from stdin
SESSION_ID=$(jq -r '.session_id' </dev/stdin)

# Validate session_id exists
if [[ -z "$SESSION_ID" ]] || [[ "$SESSION_ID" == "null" ]]; then
Expand Down Expand Up @@ -57,9 +57,16 @@ if [[ -z "$ENV_FILE" ]]; then
ENV_FILE="$HOME/.claude/ralph-loop/session-env.sh"
fi

# Write SESSION_ID to ENV_FILE
# If file exists and is writable, append to it
# Otherwise (including just-created file), write to it directly
if [[ -f "$ENV_FILE" ]] && [[ -w "$ENV_FILE" ]]; then
# Safe to use unquoted since we validated SESSION_ID contains only safe characters
echo "export RALPH_SESSION_ID=$SESSION_ID" >> "$ENV_FILE"
elif [[ ! -f "$ENV_FILE" ]]; then
# File doesn't exist (just created above), create it with content
# Safe to use unquoted since we validated SESSION_ID contains only safe characters
echo "export RALPH_SESSION_ID=$SESSION_ID" > "$ENV_FILE"
fi

exit 0
125 changes: 125 additions & 0 deletions plugins/ralph-loop/tests/ralph-loop-specific.bats
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,128 @@ EOF
# This prevents "unbound variable" errors when set -u is enabled
grep -q 'PROMPT_PARTS\[\*\]:-' "$script"
}

# ============================================================================
# Bug Fix Tests - These tests should FAIL before the fix
# ============================================================================

@test "ralph-loop: hooks.json uses wildcard matcher for SessionStart hook" {
local hooks_json="${PLUGIN_DIR}/hooks/hooks.json"
[ -f "$hooks_json" ]
ensure_jq

# The matcher should be "*" to run on all sessions
# The hook script itself checks if state file exists
local matcher
matcher=$($JQ_BIN -r '.hooks.SessionStart[0].matcher' "$hooks_json")

# This test will FAIL until the bug is fixed
[ "$matcher" = "*" ]
}

@test "ralph-loop: hooks.json uses wildcard matcher for Stop hook" {
local hooks_json="${PLUGIN_DIR}/hooks/hooks.json"
[ -f "$hooks_json" ]
ensure_jq

# The matcher should be "*" to run on all sessions
local matcher
matcher=$($JQ_BIN -r '.hooks.Stop[0].matcher' "$hooks_json")

# This test will FAIL until the bug is fixed
[ "$matcher" = "*" ]
}

@test "ralph-loop: session-start-hook.sh reads SESSION_ID from stdin" {
local hook="${PLUGIN_DIR}/hooks/session-start-hook.sh"
[ -f "$hook" ]

# The hook must read from stdin (</dev/stdin or similar)
# to get the JSON input from Claude Code
grep -q 'jq.*session_id.*</dev/stdin' "$hook"
}

@test "ralph-loop: session-start-hook.sh writes RALPH_SESSION_ID to ENV_FILE" {
local hook="${PLUGIN_DIR}/hooks/session-start-hook.sh"
[ -f "$hook" ]

# The hook should write RALPH_SESSION_ID to ENV_FILE
# The bug was that it checked if file exists BEFORE writing, but creates it right before
# The fix handles both cases: existing file and newly-created file
grep -q 'echo.*RALPH_SESSION_ID.*>>.*ENV_FILE' "$hook"
grep -q 'echo.*RALPH_SESSION_ID.*>.*ENV_FILE' "$hook"
}

# ============================================================================
# E2E Integration Tests
# These tests verify the core integration scenarios work correctly
# ============================================================================

@test "ralph-loop: session-start-hook.sh reads session_id from stdin" {
local hook="${PLUGIN_DIR}/hooks/session-start-hook.sh"
[ -f "$hook" ]

# Verify the hook reads from stdin using </dev/stdin or similar
grep -q 'jq.*session_id.*</dev/stdin' "$hook"
}

@test "ralph-loop: stop-hook.sh reads hook input from stdin" {
local hook="${PLUGIN_DIR}/hooks/stop-hook.sh"
[ -f "$hook" ]

# Verify the hook reads from stdin
grep -q 'HOOK_INPUT=.*/dev/stdin' "$hook"
}

@test "ralph-loop: stop-hook.sh outputs block decision JSON when loop active" {
local hook="${PLUGIN_DIR}/hooks/stop-hook.sh"
[ -f "$hook" ]

# Verify the hook outputs JSON with block decision
grep -q '"decision": "block"' "$hook"
grep -q 'jq -n' "$hook"
}

@test "ralph-loop: stop-hook.sh increments iteration counter in state file" {
local hook="${PLUGIN_DIR}/hooks/stop-hook.sh"
[ -f "$hook" ]

# Verify iteration increment logic
grep -q 'NEXT_ITERATION=.*ITERATION.*+.*1' "$hook"
grep -q 'sed.*iteration.*NEXT_ITERATION' "$hook"
}

@test "ralph-loop: stop-hook.sh detects completion promise in assistant output" {
local hook="${PLUGIN_DIR}/hooks/stop-hook.sh"
[ -f "$hook" ]

# Verify promise detection logic
grep -q '<promise>' "$hook"
grep -q 'PROMISE_TEXT=.*perl' "$hook"
}

@test "ralph-loop: stop-hook.sh validates numeric iteration fields" {
local hook="${PLUGIN_DIR}/hooks/stop-hook.sh"
[ -f "$hook" ]

# Verify numeric validation before arithmetic
grep -q 'ITERATION.*0-9' "$hook"
grep -q 'MAX_ITERATIONS.*0-9' "$hook"
}

@test "ralph-loop: stop-hook.sh handles max iterations reached" {
local hook="${PLUGIN_DIR}/hooks/stop-hook.sh"
[ -f "$hook" ]

# Verify max iteration check
grep -q 'MAX_ITERATIONS.*-gt.*0.*ITERATION.*-ge.*MAX_ITERATIONS' "$hook"
}

@test "ralph-loop: cancel-ralph.sh removes state file when cancelled" {
local script="${PLUGIN_DIR}/scripts/cancel-ralph.sh"
[ -f "$script" ]
[ -x "$script" ]

# Verify state file removal logic
grep -q 'rm.*STATE_FILE' "$script"
}
Loading