mirror of
https://github.com/github/awesome-copilot.git
synced 2026-02-24 04:15:14 +00:00
Rewrite Ralph loop recipes: split into simple vs ideal versions
Align all 4 language recipes (Node.js, Python, .NET, Go) with the Ralph Playbook architecture: - Simple version: minimal outer loop with fresh session per iteration - Ideal version: planning/building modes, backpressure, git integration - Fresh context isolation instead of in-session context accumulation - Disk-based shared state via IMPLEMENTATION_PLAN.md - Example prompt templates (PROMPT_plan.md, PROMPT_build.md, AGENTS.md) - Updated cookbook README descriptions
This commit is contained in:
@@ -4,127 +4,101 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
copilot "github.com/github/copilot-sdk/go"
|
||||
)
|
||||
|
||||
// RalphLoop implements iterative self-referential feedback loops.
|
||||
// The same prompt is sent repeatedly, with AI reading its own previous output.
|
||||
// Loop continues until completion promise is detected in the response.
|
||||
type RalphLoop struct {
|
||||
client *copilot.Client
|
||||
iteration int
|
||||
maxIterations int
|
||||
completionPromise string
|
||||
LastResponse string
|
||||
}
|
||||
// Ralph loop: autonomous AI task loop with fresh context per iteration.
|
||||
//
|
||||
// Two modes:
|
||||
// - "plan": reads PROMPT_plan.md, generates/updates IMPLEMENTATION_PLAN.md
|
||||
// - "build": reads PROMPT_build.md, implements tasks, runs tests, commits
|
||||
//
|
||||
// Each iteration creates a fresh session so the agent always operates in
|
||||
// the "smart zone" of its context window. State is shared between
|
||||
// iterations via files on disk (IMPLEMENTATION_PLAN.md, AGENTS.md, specs/*).
|
||||
//
|
||||
// Usage:
|
||||
// go run ralph-loop.go # build mode, 50 iterations
|
||||
// go run ralph-loop.go plan # planning mode
|
||||
// go run ralph-loop.go 20 # build mode, 20 iterations
|
||||
// go run ralph-loop.go plan 5 # planning mode, 5 iterations
|
||||
|
||||
// NewRalphLoop creates a new RALPH-loop instance.
|
||||
func NewRalphLoop(maxIterations int, completionPromise string) *RalphLoop {
|
||||
return &RalphLoop{
|
||||
client: copilot.NewClient(nil),
|
||||
maxIterations: maxIterations,
|
||||
completionPromise: completionPromise,
|
||||
func ralphLoop(ctx context.Context, mode string, maxIterations int) error {
|
||||
promptFile := "PROMPT_build.md"
|
||||
if mode == "plan" {
|
||||
promptFile = "PROMPT_plan.md"
|
||||
}
|
||||
}
|
||||
|
||||
// Run executes the RALPH-loop until completion promise is detected or max iterations reached.
|
||||
func (r *RalphLoop) Run(ctx context.Context, initialPrompt string) (string, error) {
|
||||
if err := r.client.Start(ctx); err != nil {
|
||||
return "", fmt.Errorf("failed to start client: %w", err)
|
||||
client := copilot.NewClient(nil)
|
||||
if err := client.Start(ctx); err != nil {
|
||||
return fmt.Errorf("failed to start client: %w", err)
|
||||
}
|
||||
defer r.client.Stop()
|
||||
defer client.Stop()
|
||||
|
||||
session, err := r.client.CreateSession(ctx, &copilot.SessionConfig{
|
||||
Model: "gpt-5.1-codex-mini",
|
||||
})
|
||||
branchOut, _ := exec.Command("git", "branch", "--show-current").Output()
|
||||
branch := strings.TrimSpace(string(branchOut))
|
||||
|
||||
fmt.Println(strings.Repeat("━", 40))
|
||||
fmt.Printf("Mode: %s\n", mode)
|
||||
fmt.Printf("Prompt: %s\n", promptFile)
|
||||
fmt.Printf("Branch: %s\n", branch)
|
||||
fmt.Printf("Max: %d iterations\n", maxIterations)
|
||||
fmt.Println(strings.Repeat("━", 40))
|
||||
|
||||
prompt, err := os.ReadFile(promptFile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create session: %w", err)
|
||||
return fmt.Errorf("failed to read %s: %w", promptFile, err)
|
||||
}
|
||||
defer session.Destroy()
|
||||
|
||||
for r.iteration < r.maxIterations {
|
||||
r.iteration++
|
||||
fmt.Printf("\n=== Iteration %d/%d ===\n", r.iteration, r.maxIterations)
|
||||
for i := 1; i <= maxIterations; i++ {
|
||||
fmt.Printf("\n=== Iteration %d/%d ===\n", i, maxIterations)
|
||||
|
||||
currentPrompt := r.buildIterationPrompt(initialPrompt)
|
||||
fmt.Printf("Sending prompt (length: %d)...\n", len(currentPrompt))
|
||||
|
||||
result, err := session.SendAndWait(ctx, copilot.MessageOptions{
|
||||
Prompt: currentPrompt,
|
||||
// Fresh session — each task gets full context budget
|
||||
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
|
||||
Model: "claude-sonnet-4.5",
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("send failed on iteration %d: %w", r.iteration, err)
|
||||
return fmt.Errorf("failed to create session: %w", err)
|
||||
}
|
||||
|
||||
if result != nil && result.Data.Content != nil {
|
||||
r.LastResponse = *result.Data.Content
|
||||
} else {
|
||||
r.LastResponse = ""
|
||||
_, err = session.SendAndWait(ctx, copilot.MessageOptions{
|
||||
Prompt: string(prompt),
|
||||
})
|
||||
session.Destroy()
|
||||
if err != nil {
|
||||
return fmt.Errorf("send failed on iteration %d: %w", i, err)
|
||||
}
|
||||
|
||||
// Display response summary
|
||||
summary := r.LastResponse
|
||||
if len(summary) > 200 {
|
||||
summary = summary[:200] + "..."
|
||||
}
|
||||
fmt.Printf("Response: %s\n", summary)
|
||||
|
||||
// Check for completion promise
|
||||
if strings.Contains(r.LastResponse, r.completionPromise) {
|
||||
fmt.Printf("\n✓ Success! Completion promise detected: '%s'\n", r.completionPromise)
|
||||
return r.LastResponse, nil
|
||||
// Push changes after each iteration
|
||||
if err := exec.Command("git", "push", "origin", branch).Run(); err != nil {
|
||||
exec.Command("git", "push", "-u", "origin", branch).Run()
|
||||
}
|
||||
|
||||
fmt.Printf("Iteration %d complete. Continuing...\n", r.iteration)
|
||||
fmt.Printf("\nIteration %d complete.\n", i)
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("maximum iterations (%d) reached without detecting completion promise: '%s'",
|
||||
r.maxIterations, r.completionPromise)
|
||||
}
|
||||
|
||||
func (r *RalphLoop) buildIterationPrompt(initialPrompt string) string {
|
||||
if r.iteration == 1 {
|
||||
return initialPrompt
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`%s
|
||||
|
||||
=== CONTEXT FROM PREVIOUS ITERATION ===
|
||||
%s
|
||||
=== END CONTEXT ===
|
||||
|
||||
Continue working on this task. Review the previous attempt and improve upon it.`,
|
||||
initialPrompt, r.LastResponse)
|
||||
fmt.Printf("\nReached max iterations: %d\n", maxIterations)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
prompt := `You are iteratively building a small library. Follow these phases IN ORDER.
|
||||
Do NOT skip ahead — only do the current phase, then stop and wait for the next iteration.
|
||||
mode := "build"
|
||||
maxIterations := 50
|
||||
|
||||
Phase 1: Design a DataValidator struct that validates records against a schema.
|
||||
- Schema defines field names, types (string, int, float, bool), and whether required.
|
||||
- Return a slice of validation errors per record.
|
||||
- Show the struct and method code only. Do NOT output COMPLETE.
|
||||
|
||||
Phase 2: Write at least 4 unit tests covering: missing required field, wrong type,
|
||||
valid record, and empty input. Show test code only. Do NOT output COMPLETE.
|
||||
|
||||
Phase 3: Review the code from phases 1 and 2. Fix any bugs, add doc comments, and add
|
||||
an extra edge-case test. Show the final consolidated code with all fixes.
|
||||
When this phase is fully done, output the exact text: COMPLETE`
|
||||
|
||||
ctx := context.Background()
|
||||
loop := NewRalphLoop(5, "COMPLETE")
|
||||
|
||||
result, err := loop.Run(ctx, prompt)
|
||||
if err != nil {
|
||||
log.Printf("Task did not complete: %v", err)
|
||||
log.Printf("Last attempt: %s", loop.LastResponse)
|
||||
return
|
||||
for _, arg := range os.Args[1:] {
|
||||
if arg == "plan" {
|
||||
mode = "plan"
|
||||
} else if n, err := strconv.Atoi(arg); err == nil {
|
||||
maxIterations = n
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("\n=== FINAL RESULT ===")
|
||||
fmt.Println(result)
|
||||
if err := ralphLoop(context.Background(), mode, maxIterations); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user