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
6 changes: 3 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ tmux attach -t <session-name>

### Running Tests
```bash
# Run all tests (566 tests)
# Run all tests (568 tests)
npm test

# Run specific test suites
Expand Down Expand Up @@ -519,13 +519,13 @@ Ralph uses a multi-layered strategy to prevent Claude from accidentally deleting

## Test Suite

### Test Files (566 tests total)
### Test Files (568 tests total)

| File | Tests | Description |
|------|-------|-------------|
| `test_circuit_breaker_recovery.bats` | 19 | Cooldown timer, auto-reset, parse_iso_to_epoch, CLI flag (Issue #160) |
| `test_cli_parsing.bats` | 35 | CLI argument parsing for all flags + monitor parameter forwarding |
| `test_cli_modern.bats` | 66 | Modern CLI commands (Phase 1.1) + build_claude_command fix + live mode text format fix (#164) + errexit pipeline guard (#175) + ALLOWED_TOOLS tightening (#149) + API limit false positive detection (#183) + Claude CLI command validation (#97) |
| `test_cli_modern.bats` | 68 | Modern CLI commands (Phase 1.1) + build_claude_command fix + live mode text format fix (#164) + errexit pipeline guard (#175) + ALLOWED_TOOLS tightening (#149) + API limit false positive detection (#183) + Claude CLI command validation (#97) + stale call counter fix (#196) |
| `test_json_parsing.bats` | 52 | JSON output format parsing + Claude CLI format + session management + array format |
| `test_session_continuity.bats` | 44 | Session lifecycle management + expiration + circuit breaker integration + issue #91 fix |
| `test_exit_detection.bats` | 53 | Exit signal detection + EXIT_SIGNAL-based completion indicators + progress detection |
Expand Down
7 changes: 2 additions & 5 deletions ralph_loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,8 @@ execute_claude_code() {
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
local output_file="$LOG_DIR/claude_output_${timestamp}.log"
local loop_count=$1
local calls_made=$(cat "$CALL_COUNT_FILE" 2>/dev/null || echo "0")
calls_made=$((calls_made + 1))
local calls_made
calls_made=$(increment_call_counter)

# Fix #141: Capture git HEAD SHA at loop start to detect commits as progress
# Store in file for access by progress detection after Claude execution
Expand Down Expand Up @@ -1392,9 +1392,6 @@ EOF
fi

if [ $exit_code -eq 0 ]; then
# Only increment counter on successful execution
echo "$calls_made" > "$CALL_COUNT_FILE"

# Clear progress file
echo '{"status": "completed", "timestamp": "'$(date '+%Y-%m-%d %H:%M:%S')'"}' > "$PROGRESS_FILE"

Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_cli_modern.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1187,3 +1187,29 @@ EOF
run grep 'CLAUDE_CODE_CMD' "$script"
assert_success
}

# --- Issue #196: Call counter must persist immediately, not only on success ---

@test "execute_claude_code uses increment_call_counter instead of manual read+increment" {
# Issue #196: The bug was execute_claude_code manually doing calls_made=$((calls_made + 1))
# instead of using increment_call_counter() which writes to disk immediately.
local script="${BATS_TEST_DIRNAME}/../../ralph_loop.sh"

# Verify increment_call_counter is called in execute_claude_code
run grep 'calls_made=\$(increment_call_counter)' "$script"
assert_success

# Verify the old manual increment pattern is gone (this was unique to the bug)
run grep 'calls_made=\$((calls_made + 1))' "$script"
assert_failure
}

@test "execute_claude_code does not conditionally write call count on success" {
# Issue #196: The comment "Only increment counter on successful execution" was
# the marker for the conditional write that caused stale counters on failure.
local script="${BATS_TEST_DIRNAME}/../../ralph_loop.sh"

# This comment+write pair was removed — counter is now persisted before execution
run grep 'Only increment counter on successful execution' "$script"
assert_failure
}