Skip to content

Instantly share code, notes, and snippets.

@fredflint
Created January 24, 2026 17:22
Show Gist options
  • Select an option

  • Save fredflint/588d865f98f3f81ff8d1dc8f1c7c47de to your computer and use it in GitHub Desktop.

Select an option

Save fredflint/588d865f98f3f81ff8d1dc8f1c7c47de to your computer and use it in GitHub Desktop.
Ralph loop - native Tasks version with sub-agents
#!/bin/bash
# ralph-native.sh - Execute PRD using Claude's native Tasks (sub-agents)
# Usage: ./ralph-native.sh <project-name> [model]
# Example: ./ralph-native.sh finance_calc opus 2>&1 | tee session.log
#
# Use for PRDs with <20 tasks (coordinator overhead acceptable)
# For >20 tasks, use ralph.sh (bash loop, no coordinator overhead)
#
# Progress file written to Google Drive for remote monitoring, copied back when done.
set -e
PROJECT="${1:?Usage: ralph-native.sh <project-name> [model]}"
MODEL="${2:-sonnet}"
PROJECT_UPPER=$(echo "$PROJECT" | tr '[:lower:]' '[:upper:]')
PRD_FILE="PRD_${PROJECT_UPPER}.md"
LOCAL_PROGRESS="progress_${PROJECT}.txt"
# Google Drive path for remote monitoring (customize for your setup)
GDRIVE_PATH="/your/googledrive/path"
PROGRESS_FILE="${GDRIVE_PATH}/progress_${PROJECT}.txt"
# Validate PRD exists
if [[ ! -f "$PRD_FILE" ]]; then
echo "Error: $PRD_FILE not found"
exit 1
fi
# Ensure Google Drive logs directory exists
mkdir -p "$GDRIVE_PATH"
# Initialize progress file - copy from local if exists, else create new
if [[ -s "$LOCAL_PROGRESS" ]]; then
cp "$LOCAL_PROGRESS" "$PROGRESS_FILE"
elif [[ ! -s "$PROGRESS_FILE" ]]; then
cat > "$PROGRESS_FILE" << 'EOF'
# Progress Log
## Learnings
(Patterns discovered during implementation)
---
EOF
fi
echo "==========================================="
echo " Ralph Native - Native Tasks Mode"
echo " Project: $PROJECT"
echo " PRD: $PRD_FILE"
echo " Progress (GDrive): $PROGRESS_FILE"
echo " Progress (Local): $LOCAL_PROGRESS"
echo " Model: $MODEL"
echo "==========================================="
echo ""
echo "Monitor progress remotely via Google Drive or:"
echo " watch -n10 'grep \"^### US-\" $PRD_FILE'"
echo " tail -f \"$PROGRESS_FILE\""
echo ""
# Launch Claude with the execution prompt (autonomous mode)
result=$(claude --model "$MODEL" --dangerously-skip-permissions -p "Read $PRD_FILE and $PROGRESS_FILE.
## CRITICAL: Read Learnings FIRST
Before starting ANY task, read the Learnings section in $PROGRESS_FILE for patterns from previous work.
## CRITICAL: Fresh Implementation
- Implement ALL tasks from scratch using TDD methodology
- Do NOT restore files from git history
- Do NOT skip tasks because they were previously done
## Task Type Detection
Check each task header to determine type:
- **Regular task**: \`### US-001: Create database [ ]\`
- **Review task**: \`### US-REVIEW-XXX: Review tasks 1-3 [ ]\` (any task with 'REVIEW' in title)
Route to appropriate process below based on task type.
## Task Creation
For each US-XXX story (in order from PRD):
1. TaskCreate with subject, description (full acceptance criteria), activeForm
2. TaskUpdate to add blockedBy=[previous_task_id] (sequential dependencies)
## Execution Rules
- Execute tasks ONE AT A TIME via sub-agent (Task tool, subagent_type=general-purpose)
- After each sub-agent completes, PRINT a progress summary to stdout
---
## Regular Task Process (Sub-Agent Prompt)
You are implementing [US-XXX: Task Name].
### Read Context First
1. Read $PROGRESS_FILE Learnings section for patterns
2. Read relevant existing code files
### TDD Methodology (CRITICAL)
1. Write failing test FIRST (see RED)
2. Implement minimum code to pass (see GREEN)
3. Refactor if needed
4. Verify all tests pass
### Test Structure
- Unit tests: Test each pure function in isolation
- Integration tests: Test combined functions together
- E2E tests: Test CLI commands end-to-end
### Critical: Only Complete If Tests Pass
As you work, check off acceptance criteria:
- After completing each criterion, edit $PRD_FILE to mark it [x]
When ALL acceptance criteria are [x]:
- Run: pytest -v (all tests must pass)
- Run: mypy (typecheck must pass)
- Mark the task header complete: \`### US-XXX: Title [x]\`
- Append learnings to $PROGRESS_FILE
- Git commit: \`git add <files> && git commit -m \"feat: US-XXX description\"\`
If tests FAIL:
- Do NOT mark any acceptance criteria [x]
- Do NOT mark the task header complete
- Do NOT commit broken code
- Append what went wrong to $PROGRESS_FILE (so next iteration can learn)
### Progress Notes Format
Append to bottom of $PROGRESS_FILE:
\`\`\`
## Iteration N - [Task Name] [x]
- What was implemented
- Files changed
- Learnings for future iterations:
- Patterns discovered
- Gotchas encountered
---
\`\`\`
NOTE: Use [x] to mark completed tasks (not [COMPLETE]). Match ralph.sh format.
---
## Review Task Process (For Tasks With 'REVIEW' In Title)
When you encounter a review task (e.g., US-REVIEW-PHASE1, US-FINAL-REVIEW):
### Steps
1. Read the review task acceptance criteria - it defines which tasks to review
2. Identify review scope: Note which US-XXX tasks are in scope
3. Gather commits: Run git log to find all commits for those tasks
4. Review comprehensively: Read ALL code files from the scope together
5. Apply Linus's criteria:
- Good taste: Is the code simple and elegant?
- No special cases: Edge cases handled through design, not if/else patches?
- Data structures: Appropriate for the problem?
- Complexity: Can anything be simplified?
- Duplication: Any copy-pasted code that should be extracted?
- Integration: Do components work together cleanly?
6. Cross-task analysis:
- Check for duplicated patterns between tasks
- Verify consistent naming/style across tasks
- Validate data flows between components
### If Issues Found
Insert fix tasks into $PRD_FILE:
- Add AFTER the original task that has the issue
- Add BEFORE the review task you're working on
- Use format: \`### US-XXXa: [Fix description] [ ]\`
Example:
\`\`\`
### US-002: Create API [x]
### US-002a: Extract duplicated validation [ ] <-- INSERT HERE
### US-003: Add tests [x]
### US-REVIEW-PHASE1: Review tasks 1-3 [ ] <-- Current task
\`\`\`
After inserting:
- Append review findings to $PROGRESS_FILE
- Output: <review-issues-found/>
- Do NOT mark the review task [x]
### If No Issues Found
- Append '## Review PASSED - [review task name]' to $PROGRESS_FILE
- Mark the review task [x] in $PRD_FILE
- Commit with: 'docs: [review task name] complete'
- Output: <review-passed/>
---
## Update AGENTS.md (If Applicable)
If you discover a reusable pattern that future work should know about:
- Check if AGENTS.md exists in the project root
- Add patterns like: 'This codebase uses X for Y'
- Only add genuinely reusable knowledge, not task-specific details
---
## Progress Output (CRITICAL FOR MONITORING)
After EACH task completes, output to console:
\`\`\`
=== TASK COMPLETE: [US-XXX] ===
Status: [PASS/FAIL]
Files: [list changed files]
Tests: [count passed/failed]
Next: [next task name]
================================
\`\`\`
---
## End Condition
CRITICAL: Before outputting <promise>COMPLETE</promise>:
1. Read $PRD_FILE from top to bottom
2. Search for ANY remaining [ ] in task headers (### US-XXX: Title [ ])
3. Ignore [ ] in descriptions or acceptance criteria - only count task headers
4. Only output COMPLETE if EVERY task header is marked [x]
5. If even ONE task header has [ ], do NOT output COMPLETE
After completing your task:
- If ALL task headers are [x]: output <promise>COMPLETE</promise>
- If any task headers remain [ ]: continue to next task")
echo "$result"
echo ""
# Bash-level validation: verify COMPLETE signal is accurate
if [[ "$result" == *"<promise>COMPLETE</promise>"* ]]; then
incomplete=$(grep -c "^### US-.*\[ \]" "$PRD_FILE" 2>/dev/null || true)
incomplete=${incomplete:-0}
if [[ "$incomplete" -gt 0 ]]; then
echo ""
echo "==========================================="
echo " WARNING: COMPLETE signal rejected"
echo " Found $incomplete incomplete task header(s)"
echo " Re-run to continue..."
echo "==========================================="
exit 1
fi
# Copy progress back to project folder
cp "$PROGRESS_FILE" "$LOCAL_PROGRESS"
echo "Progress copied to: $LOCAL_PROGRESS"
echo "==========================================="
echo " All tasks complete!"
echo "==========================================="
exit 0
fi
# Copy progress back to project folder
cp "$PROGRESS_FILE" "$LOCAL_PROGRESS"
echo "Progress copied to: $LOCAL_PROGRESS"
echo "==========================================="
echo " Ralph Native Complete (may need re-run)"
echo "==========================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment