forked from FlorianBruniaux/claude-code-ultimate-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfresh-context-loop.sh
More file actions
executable file
·96 lines (80 loc) · 2.97 KB
/
Copy pathfresh-context-loop.sh
File metadata and controls
executable file
·96 lines (80 loc) · 2.97 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
#!/usr/bin/env bash
# Fresh Context Loop - Ralph Wiggum pattern for Claude Code
#
# Solves context rot by spawning fresh Claude instances per task iteration.
# State persists via TASK.md, PROGRESS.md, and git commits.
#
# Usage: ./fresh-context-loop.sh [max_iterations] [task_file] [progress_file]
#
# Sources:
# - Pattern: https://block.github.io/goose/docs/tutorials/ralph-loop/
# - Research: https://research.trychroma.com/context-rot
set -euo pipefail
MAX_ITERATIONS=${1:-10}
TASK_FILE=${2:-"TASK.md"}
PROGRESS_FILE=${3:-"PROGRESS.md"}
# Validate prerequisites
if [[ ! -f "$TASK_FILE" ]]; then
echo "❌ Missing $TASK_FILE"
echo ""
echo "Create a TASK.md with:"
echo " ## Current Focus"
echo " [Single atomic task]"
echo ""
echo " ## Acceptance Criteria"
echo " - [ ] Tests pass"
echo " - [ ] Build succeeds"
exit 1
fi
# Create progress file if missing
[[ ! -f "$PROGRESS_FILE" ]] && touch "$PROGRESS_FILE"
echo "🔄 Fresh Context Loop"
echo " Max iterations: $MAX_ITERATIONS"
echo " Task file: $TASK_FILE"
echo " Progress file: $PROGRESS_FILE"
echo ""
for i in $(seq 1 "$MAX_ITERATIONS"); do
echo "═══════════════════════════════════════"
echo " Iteration $i/$MAX_ITERATIONS"
echo "═══════════════════════════════════════"
echo ""
# Fresh context each iteration (no -c flag = new session)
claude -p "$(cat "$TASK_FILE" "$PROGRESS_FILE")
---
INSTRUCTIONS FOR THIS ITERATION:
1. Read the task and progress above carefully
2. Complete the NEXT incomplete task only (one at a time)
3. Run tests/build to verify your changes work
4. Update $PROGRESS_FILE with:
- What you completed
- Any blockers encountered
- What should be done next
5. If ALL tasks are complete and verified, add this exact line to $PROGRESS_FILE:
LOOP_COMPLETE
IMPORTANT:
- Focus on ONE task per iteration
- Always verify before marking complete
- Commit your changes before the iteration ends"
echo ""
# Check completion marker
if grep -q "LOOP_COMPLETE" "$PROGRESS_FILE" 2>/dev/null; then
echo ""
echo "✅ All tasks completed in $i iterations"
exit 0
fi
# Auto-commit progress if changes exist
if ! git diff --quiet "$PROGRESS_FILE" 2>/dev/null; then
git add "$PROGRESS_FILE"
git commit -m "chore: update progress (iteration $i)" --no-verify 2>/dev/null || true
fi
# Auto-commit any other staged changes
if ! git diff --cached --quiet 2>/dev/null; then
git commit -m "feat: iteration $i progress" --no-verify 2>/dev/null || true
fi
echo ""
echo "─────────────────────────────────────────"
echo ""
done
echo "⚠️ Max iterations ($MAX_ITERATIONS) reached without completion"
echo " Check $PROGRESS_FILE for status"
exit 1