-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathralph.sh
More file actions
executable file
·408 lines (338 loc) · 10.8 KB
/
Copy pathralph.sh
File metadata and controls
executable file
·408 lines (338 loc) · 10.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/bin/bash
#
# ralph.sh
#
# Orquestrador que le docs/project-phases.md, quebra em fases,
# e alimenta cada uma ao Codex CLI ou Claude Code para implementacao automatica.
#
# Uso:
# chmod +x ralph.sh
# ./ralph.sh [--engine codex|claude] [caminho-do-arquivo]
#
# Exemplos:
# ./ralph.sh # default: codex
# ./ralph.sh --engine claude # usa Claude Code
# ./ralph.sh --engine codex docs/project-phases.md
#
# Pre-requisitos:
# - Codex: npm install -g @openai/codex + OPENAI_API_KEY
# - Claude: npm install -g @anthropic-ai/claude-code + ANTHROPIC_API_KEY
# - Estar na raiz do projeto Laravel (dentro de um repo git)
set -euo pipefail
ENGINE="codex"
INPUT_FILE=""
while [[ $# -gt 0 ]]; do
case "$1" in
--engine)
ENGINE="$2"
shift 2
;;
--engine=*)
ENGINE="${1#*=}"
shift
;;
*)
INPUT_FILE="$1"
shift
;;
esac
done
INPUT_FILE="${INPUT_FILE:-docs/project-phases.md}"
if [[ "$ENGINE" != "codex" && "$ENGINE" != "claude" ]]; then
echo "Engine invalida: $ENGINE. Use 'codex' ou 'claude'."
exit 1
fi
PHASES_DIR=".phases"
LOG_DIR=".phases/logs"
PROMPT_DIR=".phases/prompts"
MANIFEST="$PHASES_DIR/manifest.txt"
PROGRESS_FILE="$PHASES_DIR/.progress"
MAX_RETRIES=2
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"; }
success() { echo -e "${GREEN}[$(date '+%H:%M:%S')] $1${NC}"; }
warn() { echo -e "${YELLOW}[$(date '+%H:%M:%S')] $1${NC}"; }
fail() { echo -e "${RED}[$(date '+%H:%M:%S')] $1${NC}"; }
format_duration() {
local total_seconds=$1
local hours=$((total_seconds / 3600))
local minutes=$(( (total_seconds % 3600) / 60 ))
local seconds=$((total_seconds % 60))
if [ $hours -gt 0 ]; then
printf "%dh %dm %ds" $hours $minutes $seconds
elif [ $minutes -gt 0 ]; then
printf "%dm %ds" $minutes $seconds
else
printf "%ds" $seconds
fi
}
preflight_checks() {
if [[ "$ENGINE" == "codex" ]]; then
if ! command -v codex &> /dev/null; then
fail "codex CLI nao encontrado. Instale com: npm install -g @openai/codex"
exit 1
fi
elif [[ "$ENGINE" == "claude" ]]; then
if ! command -v claude &> /dev/null; then
fail "Claude Code CLI nao encontrado. Instale com: npm install -g @anthropic-ai/claude-code"
exit 1
fi
fi
if [ ! -f "$INPUT_FILE" ]; then
fail "Arquivo nao encontrado: $INPUT_FILE"
exit 1
fi
if [ ! -f "artisan" ]; then
warn "Nao parece ser a raiz de um projeto Laravel (artisan nao encontrado)"
read -p "Continuar mesmo assim? (y/N) " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] || exit 1
fi
if ! git rev-parse --is-inside-work-tree &> /dev/null 2>&1; then
fail "Requer um repositorio git."
exit 1
fi
success "Pre-checks OK (engine: $ENGINE)"
}
split_phases() {
log "Quebrando $INPUT_FILE em fases..."
rm -rf "$PHASES_DIR"
mkdir -p "$PHASES_DIR" "$LOG_DIR" "$PROMPT_DIR"
> "$MANIFEST"
local current_file=""
local phase_count=0
while IFS= read -r line || [ -n "$line" ]; do
if [[ "$line" =~ ^##[[:space:]]+(Phase[[:space:]]+[0-9]+[^#]*) ]]; then
phase_count=$((phase_count + 1))
local raw_title="${BASH_REMATCH[1]}"
raw_title="$(echo "$raw_title" | sed 's/[[:space:]]*$//')"
local slug
slug=$(echo "$raw_title" \
| tr '[:upper:]' '[:lower:]' \
| sed 's/phase[[:space:]]*/phase-/' \
| sed 's/[^a-z0-9-]/-/g' \
| sed 's/--*/-/g' \
| sed 's/-$//' \
| sed 's/^-//')
slug=$(echo "$slug" | sed -E 's/phase-([0-9])$/phase-0\1/' | sed -E 's/phase-([0-9])-/phase-0\1-/')
current_file="$PHASES_DIR/${slug}.md"
echo "$line" > "$current_file"
echo "${slug}.md|${raw_title}" >> "$MANIFEST"
continue
fi
if [ -n "$current_file" ]; then
echo "$line" >> "$current_file"
fi
done < "$INPUT_FILE"
success "$phase_count fases extraidas"
}
build_prompt_file() {
local phase_file="$1"
local prompt_file="$PROMPT_DIR/${phase_file%.md}.txt"
cat > "$prompt_file" <<PROMPT
Voce e um desenvolvedor Laravel senior.
## Stack do projeto
- Laravel 13, PHP 8.5
- Pest PHP 4 (testes)
- postgresql (via Laravel Sail)
## Arquivos de referencia importantes
- Voce tem acesso ao mem0 para entender o contexto do projeto
- docs/project-phases.md — plano completo de fases
- docs/user-stories.md — user stories
- docs/project-description.md — descricao geral
## Sua tarefa agora
Implemente COMPLETAMENTE a fase descrita abaixo.
Para cada item:
1. Implemente o codigo completo (nao deixe TODOs ou placeholders)
2. Crie os testes listados
3. Rode os testes com o subagent
4. Se um teste falhar, corrija o codigo e rode novamente
5. So passe pro proximo item quando os testes passarem
## Regras obrigatorias
- LEIA o CLAUDE.md antes de comecar — ele contem as convencoes do projeto
- Todos os comandos devem usar ./vendor/bin/sail (Docker/Sail)
- Factories devem criar todas as dependencias (role, user, product, etc.)
- Nomes de classes, arquivos e metodos devem seguir EXATAMENTE o que esta descrito
- Nao pule nenhum item marcado com [ ]
- Ao final utilize o subagent de testes para validar se esta tudo correto
## Fase a implementar
$(cat "$PHASES_DIR/$phase_file")
PROMPT
echo "$prompt_file"
}
build_retry_prompt_file() {
local phase_file="$1"
local test_output="$2"
local prompt_file="$PROMPT_DIR/${phase_file%.md}-retry.txt"
cat > "$prompt_file" <<PROMPT
Os testes falharam apos a implementacao anterior. Corrija os erros.
Saida dos testes:
\`\`\`
$test_output
\`\`\`
Corrija o codigo para que todos os testes passem. Rode os testes novamente apos cada correcao.
PROMPT
echo "$prompt_file"
}
run_engine() {
local prompt_file="$1"
local log_file="$2"
# Exporta contexto da fase atual para os hooks (notify-n8n.sh usa quando .message vem vazio)
export RALPH_ENGINE="$ENGINE"
export RALPH_PHASE_TITLE="${RALPH_PHASE_TITLE:-}"
export RALPH_PHASE_NUM="${RALPH_PHASE_NUM:-}"
export RALPH_PHASE_TOTAL="${RALPH_PHASE_TOTAL:-}"
export RALPH_PHASE_ATTEMPT="${RALPH_PHASE_ATTEMPT:-1}"
export RALPH_PHASE_MAX_ATTEMPTS="$((MAX_RETRIES + 1))"
if [[ "$ENGINE" == "codex" ]]; then
cat "$prompt_file" | codex exec --sandbox danger-full-access - 2>&1 | tee "$log_file"
elif [[ "$ENGINE" == "claude" ]]; then
env -u CLAUDECODE claude --dangerously-skip-permissions -p "$(cat "$prompt_file")" --output-format text --verbose 2>&1 | tee "$log_file"
fi
}
run_phase() {
local phase_file="$1"
local phase_title="$2"
local phase_num="$3"
local total_phases="$4"
local log_file="$LOG_DIR/${phase_file%.md}.log"
local phase_start
phase_start=$(date +%s)
export RALPH_PHASE_TITLE="$phase_title"
export RALPH_PHASE_NUM="$phase_num"
export RALPH_PHASE_TOTAL="$total_phases"
echo ""
log "[$phase_num/$total_phases] $phase_title"
local attempt=0
local phase_success=false
while [ $attempt -le $MAX_RETRIES ]; do
attempt=$((attempt + 1))
export RALPH_PHASE_ATTEMPT="$attempt"
if [ $attempt -gt 1 ]; then
warn "Tentativa $attempt/$((MAX_RETRIES + 1))..."
fi
local prompt_file
if [ $attempt -eq 1 ]; then
prompt_file=$(build_prompt_file "$phase_file")
fi
if run_engine "$prompt_file" "$log_file"; then
phase_success=true
break
else
fail "$ENGINE retornou erro"
if [ $attempt -le $MAX_RETRIES ]; then
local test_output
test_output=$(tail -30 "$log_file" 2>/dev/null || echo "Sem output disponivel")
prompt_file=$(build_retry_prompt_file "$phase_file" "$test_output")
fi
fi
done
local phase_end
phase_end=$(date +%s)
local phase_duration=$((phase_end - phase_start))
if $phase_success; then
success "$phase_title — COMPLETA ($(format_duration $phase_duration))"
if git rev-parse --is-inside-work-tree &> /dev/null 2>&1; then
git add -A
git commit -m "feat: $phase_title" --allow-empty
log "Commit criado no git"
fi
echo "$phase_file" >> "$PROGRESS_FILE"
return 0
else
fail "$phase_title — FALHOU apos $((MAX_RETRIES + 1)) tentativas ($(format_duration $phase_duration))"
fail "Log disponivel em: $log_file"
return 1
fi
}
is_phase_done() {
local phase_file="$1"
[ -f "$PROGRESS_FILE" ] && grep -qF "$phase_file" "$PROGRESS_FILE"
}
main() {
preflight_checks
split_phases
local total_phases
total_phases=$(wc -l < "$MANIFEST")
echo ""
log "$total_phases fases para implementar"
echo ""
local num=0
while IFS="|" read -r file title; do
num=$((num + 1))
if is_phase_done "$file"; then
echo -e " ${GREEN}[$num] $title (ja completada)${NC}"
else
echo -e " ${YELLOW}[$num] $title${NC}"
fi
done < "$MANIFEST"
echo ""
read -p "Iniciar implementacao? (Y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Nn]$ ]] && exit 0
local start_time
start_time=$(date +%s)
log "Inicio: $(date '+%d/%m/%Y %H:%M:%S')"
local current=0
local failed_phases=()
local skipped_phases=()
local completed_phases=()
while IFS="|" read -r file title; do
current=$((current + 1))
if is_phase_done "$file"; then
log "Pulando $title (ja completada)"
skipped_phases+=("$title")
continue
fi
if run_phase "$file" "$title" "$current" "$total_phases"; then
completed_phases+=("$title")
else
failed_phases+=("$title")
echo ""
warn "Fase falhou: $title"
read -p "Continuar para a proxima fase? (Y/n) " -n 1 -r
echo
[[ $REPLY =~ ^[Nn]$ ]] && break
fi
done < "$MANIFEST"
local end_time
end_time=$(date +%s)
local total_duration=$((end_time - start_time))
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log "RELATORIO FINAL (engine: $ENGINE)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ ${#completed_phases[@]} -gt 0 ]; then
echo ""
success "Completadas (${#completed_phases[@]}):"
for phase in "${completed_phases[@]}"; do
echo -e " ${GREEN}$phase${NC}"
done
fi
if [ ${#skipped_phases[@]} -gt 0 ]; then
echo ""
log "Puladas (${#skipped_phases[@]}):"
for phase in "${skipped_phases[@]}"; do
echo -e " $phase"
done
fi
if [ ${#failed_phases[@]} -gt 0 ]; then
echo ""
fail "Falharam (${#failed_phases[@]}):"
for phase in "${failed_phases[@]}"; do
echo -e " ${RED}$phase${NC}"
done
echo ""
fail "Verifique os logs em $LOG_DIR/"
fi
echo ""
log "Inicio: $(date -d @$start_time '+%d/%m/%Y %H:%M:%S')"
log "Fim: $(date -d @$end_time '+%d/%m/%Y %H:%M:%S')"
log "Duracao total: $(format_duration $total_duration)"
echo ""
}
main