@kood/claude-code 0.7.9 → 0.7.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,161 @@
1
+ #!/bin/bash
2
+
3
+ # Ralph Loop Setup Script
4
+ # Creates state file + ensures Stop hook is registered in User settings
5
+
6
+ set -euo pipefail
7
+
8
+ # --- Hook auto-registration (User settings only, never Project) ---
9
+ ensure_stop_hook() {
10
+ local USER_SETTINGS="$HOME/.claude/settings.json"
11
+ local HOOK_CMD=".claude/hooks/ralph-stop-hook.sh"
12
+
13
+ # Create ~/.claude/ if needed
14
+ mkdir -p "$HOME/.claude"
15
+
16
+ # If no settings file, create with hook
17
+ if [[ ! -f "$USER_SETTINGS" ]]; then
18
+ cat > "$USER_SETTINGS" <<'HOOKEOF'
19
+ {
20
+ "hooks": {
21
+ "Stop": [
22
+ {
23
+ "hooks": [
24
+ {
25
+ "type": "command",
26
+ "command": ".claude/hooks/ralph-stop-hook.sh"
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ }
32
+ }
33
+ HOOKEOF
34
+ echo "Created $USER_SETTINGS with Ralph stop hook"
35
+ return
36
+ fi
37
+
38
+ # Check if ralph hook already registered (any path containing 'ralph')
39
+ if jq -e '.hooks.Stop // [] | .. | .command? // empty | test("ralph")' "$USER_SETTINGS" >/dev/null 2>&1; then
40
+ return # Already registered
41
+ fi
42
+
43
+ # Add hook to existing settings (preserve all existing values)
44
+ local TEMP_FILE="${USER_SETTINGS}.tmp.$$"
45
+
46
+ # If Stop array exists, append to it; otherwise create it
47
+ if jq -e '.hooks.Stop' "$USER_SETTINGS" >/dev/null 2>&1; then
48
+ jq --arg cmd "$HOOK_CMD" '.hooks.Stop += [{"hooks": [{"type": "command", "command": $cmd}]}]' "$USER_SETTINGS" > "$TEMP_FILE"
49
+ elif jq -e '.hooks' "$USER_SETTINGS" >/dev/null 2>&1; then
50
+ jq --arg cmd "$HOOK_CMD" '.hooks.Stop = [{"hooks": [{"type": "command", "command": $cmd}]}]' "$USER_SETTINGS" > "$TEMP_FILE"
51
+ else
52
+ jq --arg cmd "$HOOK_CMD" '.hooks = {"Stop": [{"hooks": [{"type": "command", "command": $cmd}]}]}' "$USER_SETTINGS" > "$TEMP_FILE"
53
+ fi
54
+
55
+ mv "$TEMP_FILE" "$USER_SETTINGS"
56
+ echo "Registered Ralph stop hook in $USER_SETTINGS"
57
+ }
58
+
59
+ ensure_stop_hook
60
+
61
+ # --- Argument parsing ---
62
+ PROMPT_PARTS=()
63
+ MAX_ITERATIONS=0
64
+ COMPLETION_PROMISE="null"
65
+
66
+ while [[ $# -gt 0 ]]; do
67
+ case $1 in
68
+ -h|--help)
69
+ cat << 'HELP_EOF'
70
+ Ralph Loop - Interactive self-referential development loop
71
+
72
+ USAGE:
73
+ /ralph-loop [PROMPT...] [OPTIONS]
74
+
75
+ OPTIONS:
76
+ --max-iterations <n> Maximum iterations (default: unlimited)
77
+ --completion-promise '<text>' Promise phrase (USE QUOTES for multi-word)
78
+ -h, --help Show this help
79
+
80
+ EXAMPLES:
81
+ /ralph-loop Build a todo API --completion-promise 'DONE' --max-iterations 20
82
+ /ralph-loop --max-iterations 10 Fix the auth bug
83
+ /ralph-loop Refactor cache layer (runs forever)
84
+
85
+ STOPPING:
86
+ Only by reaching --max-iterations or detecting --completion-promise
87
+
88
+ MONITORING:
89
+ grep '^iteration:' .claude/ralph-loop.local.md
90
+ HELP_EOF
91
+ exit 0
92
+ ;;
93
+ --max-iterations)
94
+ if [[ -z "${2:-}" ]] || ! [[ "$2" =~ ^[0-9]+$ ]]; then
95
+ echo "Error: --max-iterations requires a positive integer" >&2
96
+ exit 1
97
+ fi
98
+ MAX_ITERATIONS="$2"
99
+ shift 2
100
+ ;;
101
+ --completion-promise)
102
+ if [[ -z "${2:-}" ]]; then
103
+ echo "Error: --completion-promise requires a text argument" >&2
104
+ exit 1
105
+ fi
106
+ COMPLETION_PROMISE="$2"
107
+ shift 2
108
+ ;;
109
+ *)
110
+ PROMPT_PARTS+=("$1")
111
+ shift
112
+ ;;
113
+ esac
114
+ done
115
+
116
+ PROMPT="${PROMPT_PARTS[*]}"
117
+
118
+ if [[ -z "$PROMPT" ]]; then
119
+ echo "Error: No prompt provided" >&2
120
+ echo "Usage: /ralph-loop <PROMPT> [--max-iterations N] [--completion-promise TEXT]" >&2
121
+ exit 1
122
+ fi
123
+
124
+ # --- Create state file ---
125
+ mkdir -p .claude
126
+
127
+ if [[ -n "$COMPLETION_PROMISE" ]] && [[ "$COMPLETION_PROMISE" != "null" ]]; then
128
+ COMPLETION_PROMISE_YAML="\"$COMPLETION_PROMISE\""
129
+ else
130
+ COMPLETION_PROMISE_YAML="null"
131
+ fi
132
+
133
+ cat > .claude/ralph-loop.local.md <<EOF
134
+ ---
135
+ active: true
136
+ iteration: 1
137
+ max_iterations: $MAX_ITERATIONS
138
+ completion_promise: $COMPLETION_PROMISE_YAML
139
+ started_at: "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
140
+ ---
141
+
142
+ $PROMPT
143
+ EOF
144
+
145
+ cat <<EOF
146
+ Ralph loop activated!
147
+
148
+ Iteration: 1
149
+ Max iterations: $(if [[ $MAX_ITERATIONS -gt 0 ]]; then echo $MAX_ITERATIONS; else echo "unlimited"; fi)
150
+ Completion promise: $(if [[ "$COMPLETION_PROMISE" != "null" ]]; then echo "$COMPLETION_PROMISE"; else echo "none (runs forever)"; fi)
151
+
152
+ The stop hook blocks exit and re-feeds the same prompt each iteration.
153
+
154
+ EOF
155
+
156
+ echo "$PROMPT"
157
+
158
+ if [[ "$COMPLETION_PROMISE" != "null" ]]; then
159
+ echo ""
160
+ echo "To complete: output <promise>$COMPLETION_PROMISE</promise> (ONLY when TRUE)"
161
+ fi