-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathralph.sh
More file actions
executable file
·235 lines (197 loc) · 7.76 KB
/
ralph.sh
File metadata and controls
executable file
·235 lines (197 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
set -euo pipefail
# Ralph Wiggum CLI
# Main entrypoint that dispatches to subcommands
RALPH_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$RALPH_HOME/lib/utils.sh"
# Parse global flags
DOCKER_MODE=false
while [[ $# -gt 0 ]]; do
case $1 in
--docker) DOCKER_MODE=true; shift ;;
*) break ;;
esac
done
# Handle Docker mode
if [[ "$DOCKER_MODE" == "true" ]]; then
# Build and run in Docker
docker compose -f "$RALPH_HOME/docker/docker-compose.yml" run --rm \
-e PROJECT_DIR="$(pwd)" \
ralph "$@"
exit $?
fi
COMMAND="${1:-run}"
shift || true
ralph_status() {
if [[ ! -f "ralph/prd.json" ]]; then
log_error "No prd.json found. Run 'ralph init' and 'ralph plan' first."
exit 1
fi
# Check if jq is available for pretty output
if command -v jq &> /dev/null; then
echo ""
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ PRD Status ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
local goal iteration
goal=$(jq -r '.meta.goal' ralph/prd.json)
iteration=$(jq -r '.meta.iteration' ralph/prd.json)
echo "Goal: $goal"
echo "Iteration: $iteration"
echo ""
echo "=== Tasks ==="
echo ""
# In Progress
local in_progress
in_progress=$(jq -r '.tasks[] | select(.status == "in_progress") | " ▶ [\(.id)] \(.description)"' ralph/prd.json)
if [[ -n "$in_progress" ]]; then
echo -e "${YELLOW}In Progress:${NC}"
echo "$in_progress"
echo ""
fi
# Pending
local pending
pending=$(jq -r '.tasks[] | select(.status == "pending") | " ○ [\(.id)] (p\(.priority)) \(.description)"' ralph/prd.json)
if [[ -n "$pending" ]]; then
echo "Pending:"
echo "$pending"
echo ""
fi
# Blocked
local blocked
blocked=$(jq -r '.tasks[] | select(.status == "blocked") | " ✗ [\(.id)] \(.description) - \(.notes // "no notes")"' ralph/prd.json)
if [[ -n "$blocked" ]]; then
echo -e "${RED}Blocked:${NC}"
echo "$blocked"
echo ""
fi
# Completed count
local completed_count
completed_count=$(jq '[.tasks[] | select(.status == "completed")] | length' ralph/prd.json)
echo -e "${GREEN}Completed: $completed_count tasks${NC}"
echo ""
# Issues
local issues
issues=$(jq -r '.issues[] | select(.resolved == false) | " ! [\(.severity)] \(.description)"' ralph/prd.json 2>/dev/null)
if [[ -n "$issues" ]]; then
echo -e "${RED}=== Open Issues ===${NC}"
echo "$issues"
echo ""
fi
# Summary
local total pending_count in_progress_count
total=$(jq '.tasks | length' ralph/prd.json)
pending_count=$(jq '[.tasks[] | select(.status == "pending")] | length' ralph/prd.json)
in_progress_count=$(jq '[.tasks[] | select(.status == "in_progress")] | length' ralph/prd.json)
echo "─────────────────────────────────────────"
echo "Total: $total | Pending: $pending_count | In Progress: $in_progress_count | Completed: $completed_count"
else
# Fallback without jq
log_warn "Install jq for formatted output. Showing raw JSON:"
cat ralph/prd.json
fi
}
ralph_clean() {
if [[ ! -f "ralph/prd.json" ]]; then
log_error "No prd.json found."
exit 1
fi
if ! command -v jq &> /dev/null; then
log_error "jq is required for cleanup. Install with: brew install jq"
exit 1
fi
log_info "Cleaning completed tasks from prd.json..."
# Keep only non-completed tasks, or recent completions
local temp_file
temp_file=$(mktemp)
jq '.tasks = [.tasks[] | select(.status != "completed")]' ralph/prd.json > "$temp_file"
mv "$temp_file" ralph/prd.json
log_info "Removed completed tasks. Run 'ralph status' to see current state."
}
ralph_import() {
local input_file="${1:-}"
if [[ -z "$input_file" ]]; then
log_error "Usage: ralph import <plan.md>"
log_info "Import a markdown plan and initialize JSON tracking."
exit 1
fi
if [[ ! -f "$input_file" ]]; then
log_error "File not found: $input_file"
exit 1
fi
# Ensure ralph directory exists
mkdir -p ralph
# Copy markdown plan
cp "$input_file" ralph/plan.md
log_info "Copied $input_file to ralph/plan.md"
# Create initial prd.json if it doesn't exist
local timestamp
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [[ ! -f "ralph/prd.json" ]]; then
cat > ralph/prd.json << EOF
{
"meta": {
"goal": "Imported from $input_file",
"created_at": "$timestamp",
"updated_at": "$timestamp",
"iteration": 0
},
"tasks": [],
"issues": []
}
EOF
log_info "Created ralph/prd.json"
fi
echo ""
log_info "Plan imported successfully!"
echo ""
echo "Next steps:"
echo " 1. Run 'ralph plan' to parse the markdown and create JSON tasks"
echo " 2. Or run 'ralph' to start building (Ralph will sync on first run)"
echo ""
echo "Ralph will read requirements from plan.md and track state in prd.json."
}
ralph_help() {
cat << 'EOF'
Ralph Wiggum - Autonomous Agent Loop Toolkit
Usage:
ralph Build mode, unlimited iterations
ralph <n> Build mode, max n iterations
ralph plan Planning mode
ralph plan <n> Planning mode, max n iterations
ralph plan-work "<scope>" Scoped planning for specific work
ralph import <plan.md> Import existing markdown plan
ralph status Show current plan status
ralph clean Clean completed items from plan
ralph init Initialize Ralph in current project
ralph help Show this help
Options:
--docker Run in Docker sandbox (recommended for autonomous runs)
Examples:
ralph init # Set up Ralph in your project
ralph import ~/my-plan.md # Import existing markdown plan
ralph plan # Generate implementation plan from specs
ralph # Start building (unlimited iterations)
ralph 20 # Build for max 20 iterations
ralph --docker # Build in Docker sandbox
ralph plan-work "user auth" # Create scoped plan for user auth feature
File Structure:
ralph/plan.md - Human-readable implementation plan (you write this)
ralph/prd.json - Machine state tracking (Ralph manages this)
ralph/specs/ - Feature specifications
ralph/AGENTS.md - Build/test/lint commands
Documentation: https://github.com/yourorg/ralph-wiggum
EOF
}
case "$COMMAND" in
init) source "$RALPH_HOME/lib/init.sh"; ralph_init "$@" ;;
import) ralph_import "$@" ;;
plan) source "$RALPH_HOME/lib/loop.sh"; ralph_loop "plan" "$@" ;;
plan-work) source "$RALPH_HOME/lib/loop.sh"; WORK_SCOPE="${1:-}"; shift || true; ralph_loop "plan-work" "$@" ;;
status) ralph_status "$@" ;;
clean) ralph_clean "$@" ;;
help|--help|-h) ralph_help ;;
run) source "$RALPH_HOME/lib/loop.sh"; ralph_loop "build" "$@" ;;
*) source "$RALPH_HOME/lib/loop.sh"; ralph_loop "build" "$COMMAND" "$@" ;;
esac