diff --git a/cookbook/copilot-sdk/README.md b/cookbook/copilot-sdk/README.md index c4892687..5a20ba0a 100644 --- a/cookbook/copilot-sdk/README.md +++ b/cookbook/copilot-sdk/README.md @@ -87,4 +87,4 @@ go run .go ## Status -Cookbook structure is complete with 5 recipes across all 4 supported languages. Each recipe includes both markdown documentation and runnable examples. The RALPH-loop recipe demonstrates iterative self-referential AI loops for autonomous task completion. +Cookbook structure is complete with 6 recipes across all 4 supported languages. Each recipe includes both markdown documentation and runnable examples. The RALPH-loop recipe demonstrates iterative self-referential AI loops for autonomous task completion. diff --git a/cookbook/copilot-sdk/dotnet/ralph-loop.md b/cookbook/copilot-sdk/dotnet/ralph-loop.md index 43d22e72..5580e0f4 100644 --- a/cookbook/copilot-sdk/dotnet/ralph-loop.md +++ b/cookbook/copilot-sdk/dotnet/ralph-loop.md @@ -5,8 +5,8 @@ Implement self-referential feedback loops where an AI agent iteratively improves > **Runnable example:** [recipe/ralph-loop.cs](recipe/ralph-loop.cs) > > ```bash -> cd dotnet/recipe -> dotnet run ralph-loop.cs +> cd dotnet +> dotnet run recipe/ralph-loop.cs > ``` ## What is RALPH-loop? diff --git a/cookbook/copilot-sdk/go/ralph-loop.md b/cookbook/copilot-sdk/go/ralph-loop.md index 37879c0b..2469c181 100644 --- a/cookbook/copilot-sdk/go/ralph-loop.md +++ b/cookbook/copilot-sdk/go/ralph-loop.md @@ -5,8 +5,8 @@ Implement self-referential feedback loops where an AI agent iteratively improves > **Runnable example:** [recipe/ralph-loop.go](recipe/ralph-loop.go) > > ```bash -> cd go/recipe -> go run ralph-loop.go +> cd go +> go run recipe/ralph-loop.go > ``` ## What is RALPH-loop? @@ -20,13 +20,13 @@ RALPH-loop is a development methodology for iterative AI-powered task completion ## Example Scenario -You need to iteratively improve code until all tests pass. Instead of asking Claude to "write perfect code," you use RALPH-loop to: +You need to iteratively improve code until all tests pass. Instead of asking Copilot to "write perfect code," you use RALPH-loop to: 1. Send the initial prompt with clear success criteria -2. Claude writes code and tests -3. Claude runs tests and sees failures +2. Copilot writes code and tests +3. Copilot runs tests and sees failures 4. Loop automatically re-sends the prompt -5. Claude reads test output and previous code, fixes issues +5. Copilot reads test output and previous code, fixes issues 6. Repeat until all tests pass and completion promise is output ## Basic Implementation diff --git a/cookbook/copilot-sdk/nodejs/ralph-loop.md b/cookbook/copilot-sdk/nodejs/ralph-loop.md index 1ad70708..bc4d1998 100644 --- a/cookbook/copilot-sdk/nodejs/ralph-loop.md +++ b/cookbook/copilot-sdk/nodejs/ralph-loop.md @@ -5,8 +5,7 @@ Implement self-referential feedback loops where an AI agent iteratively improves > **Runnable example:** [recipe/ralph-loop.ts](recipe/ralph-loop.ts) > > ```bash -> cd nodejs/recipe -> npm install +> cd recipe && npm install > npx tsx ralph-loop.ts > ``` @@ -21,13 +20,13 @@ RALPH-loop is a development methodology for iterative AI-powered task completion ## Example Scenario -You need to iteratively improve code until all tests pass. Instead of asking Claude to "write perfect code," you use RALPH-loop to: +You need to iteratively improve code until all tests pass. Instead of asking Copilot to "write perfect code," you use RALPH-loop to: 1. Send the initial prompt with clear success criteria -2. Claude writes code and tests -3. Claude runs tests and sees failures +2. Copilot writes code and tests +3. Copilot runs tests and sees failures 4. Loop automatically re-sends the prompt -5. Claude reads test output and previous code, fixes issues +5. Copilot reads test output and previous code, fixes issues 6. Repeat until all tests pass and completion promise is output ## Basic Implementation diff --git a/cookbook/copilot-sdk/nodejs/recipe/ralph-loop.ts b/cookbook/copilot-sdk/nodejs/recipe/ralph-loop.ts index 19b16b06..93a7ebb2 100644 --- a/cookbook/copilot-sdk/nodejs/recipe/ralph-loop.ts +++ b/cookbook/copilot-sdk/nodejs/recipe/ralph-loop.ts @@ -22,47 +22,54 @@ class RalphLoop { * Run the RALPH-loop until completion promise is detected or max iterations reached. */ async run(initialPrompt: string): Promise { + let session: Awaited> | null = null; + await this.client.start(); - const session = await this.client.createSession({ - model: "gpt-5.1-codex-mini" - }); - try { - while (this.iteration < this.maxIterations) { - this.iteration++; - console.log(`\n=== Iteration ${this.iteration}/${this.maxIterations} ===`); + session = await this.client.createSession({ + model: "gpt-5.1-codex-mini" + }); - // Build the prompt for this iteration - const currentPrompt = this.buildIterationPrompt(initialPrompt); - console.log(`Sending prompt (length: ${currentPrompt.length})...`); + try { + while (this.iteration < this.maxIterations) { + this.iteration++; + console.log(`\n=== Iteration ${this.iteration}/${this.maxIterations} ===`); - const response = await session.sendAndWait({ prompt: currentPrompt }, 300_000); - this.lastResponse = response?.data.content || ""; + // Build the prompt for this iteration + const currentPrompt = this.buildIterationPrompt(initialPrompt); + console.log(`Sending prompt (length: ${currentPrompt.length})...`); - // Display response summary - const summary = this.lastResponse.length > 200 - ? this.lastResponse.substring(0, 200) + "..." - : this.lastResponse; - console.log(`Response: ${summary}`); + const response = await session.sendAndWait({ prompt: currentPrompt }, 300_000); + this.lastResponse = response?.data.content || ""; - // Check for completion promise - if (this.lastResponse.includes(this.completionPromise)) { - console.log(`\n✓ Success! Completion promise detected: '${this.completionPromise}'`); - return this.lastResponse; + // Display response summary + const summary = this.lastResponse.length > 200 + ? this.lastResponse.substring(0, 200) + "..." + : this.lastResponse; + console.log(`Response: ${summary}`); + + // Check for completion promise + if (this.lastResponse.includes(this.completionPromise)) { + console.log(`\n✓ Success! Completion promise detected: '${this.completionPromise}'`); + return this.lastResponse; + } + + console.log(`Iteration ${this.iteration} complete. Checking for next iteration...`); } - console.log(`Iteration ${this.iteration} complete. Checking for next iteration...`); + // Max iterations reached without completion + throw new Error( + `Maximum iterations (${this.maxIterations}) reached without detecting completion promise: '${this.completionPromise}'` + ); + } catch (error) { + console.error(`\nError during RALPH-loop: ${error instanceof Error ? error.message : String(error)}`); + throw error; + } finally { + if (session) { + await session.destroy(); + } } - - // Max iterations reached without completion - throw new Error( - `Maximum iterations (${this.maxIterations}) reached without detecting completion promise: '${this.completionPromise}'` - ); - } catch (error) { - console.error(`\nError during RALPH-loop: ${error instanceof Error ? error.message : String(error)}`); - throw error; } finally { - await session.destroy(); await this.client.stop(); } } diff --git a/cookbook/copilot-sdk/python/ralph-loop.md b/cookbook/copilot-sdk/python/ralph-loop.md index 5b208200..9a969ce6 100644 --- a/cookbook/copilot-sdk/python/ralph-loop.md +++ b/cookbook/copilot-sdk/python/ralph-loop.md @@ -5,11 +5,9 @@ Implement self-referential feedback loops where an AI agent iteratively improves > **Runnable example:** [recipe/ralph_loop.py](recipe/ralph_loop.py) > > ```bash -> cd python/recipe -> pip install -r requirements.txt +> cd recipe && pip install -r requirements.txt > python ralph_loop.py > ``` - ## What is RALPH-loop? RALPH-loop is a development methodology for iterative AI-powered task completion. Named after the Ralph Wiggum technique, it embodies the philosophy of persistent iteration: @@ -21,13 +19,13 @@ RALPH-loop is a development methodology for iterative AI-powered task completion ## Example Scenario -You need to iteratively improve code until all tests pass. Instead of asking Claude to "write perfect code," you use RALPH-loop to: +You need to iteratively improve code until all tests pass. Instead of asking Copilot to "write perfect code," you use RALPH-loop to: 1. Send the initial prompt with clear success criteria -2. Claude writes code and tests -3. Claude runs tests and sees failures +2. Copilot writes code and tests +3. Copilot runs tests and sees failures 4. Loop automatically re-sends the prompt -5. Claude reads test output and previous code, fixes issues +5. Copilot reads test output and previous code, fixes issues 6. Repeat until all tests pass and completion promise is output ## Basic Implementation diff --git a/cookbook/copilot-sdk/python/recipe/ralph_loop.py b/cookbook/copilot-sdk/python/recipe/ralph_loop.py index a8c228a2..00ecadcc 100644 --- a/cookbook/copilot-sdk/python/recipe/ralph_loop.py +++ b/cookbook/copilot-sdk/python/recipe/ralph_loop.py @@ -25,55 +25,59 @@ class RalphLoop: """ Run the RALPH-loop until completion promise is detected or max iterations reached. """ + session = None await self.client.start() - session = await self.client.create_session( - SessionConfig(model="gpt-5.1-codex-mini") - ) - try: - while self.iteration < self.max_iterations: - self.iteration += 1 - print(f"\n=== Iteration {self.iteration}/{self.max_iterations} ===") - - current_prompt = self._build_iteration_prompt(initial_prompt) - print(f"Sending prompt (length: {len(current_prompt)})...") - - result = await session.send_and_wait( - MessageOptions(prompt=current_prompt), - timeout=300, - ) - - self.last_response = result.data.content if result else "" - - # Display response summary - summary = ( - self.last_response[:200] + "..." - if len(self.last_response) > 200 - else self.last_response - ) - print(f"Response: {summary}") - - # Check for completion promise - if self.completion_promise in self.last_response: - print( - f"\n✓ Success! Completion promise detected: '{self.completion_promise}'" - ) - return self.last_response - - print( - f"Iteration {self.iteration} complete. Checking for next iteration..." - ) - - raise RuntimeError( - f"Maximum iterations ({self.max_iterations}) reached without " - f"detecting completion promise: '{self.completion_promise}'" + session = await self.client.create_session( + SessionConfig(model="gpt-5.1-codex-mini") ) - except Exception as e: - print(f"\nError during RALPH-loop: {e}") - raise + try: + while self.iteration < self.max_iterations: + self.iteration += 1 + print(f"\n=== Iteration {self.iteration}/{self.max_iterations} ===") + + current_prompt = self._build_iteration_prompt(initial_prompt) + print(f"Sending prompt (length: {len(current_prompt)})...") + + result = await session.send_and_wait( + MessageOptions(prompt=current_prompt), + timeout=300, + ) + + self.last_response = result.data.content if result else "" + + # Display response summary + summary = ( + self.last_response[:200] + "..." + if len(self.last_response) > 200 + else self.last_response + ) + print(f"Response: {summary}") + + # Check for completion promise + if self.completion_promise in self.last_response: + print( + f"\n✓ Success! Completion promise detected: '{self.completion_promise}'" + ) + return self.last_response + + print( + f"Iteration {self.iteration} complete. Checking for next iteration..." + ) + + raise RuntimeError( + f"Maximum iterations ({self.max_iterations}) reached without " + f"detecting completion promise: '{self.completion_promise}'" + ) + + except Exception as e: + print(f"\nError during RALPH-loop: {e}") + raise + finally: + if session is not None: + await session.destroy() finally: - await session.destroy() await self.client.stop() def _build_iteration_prompt(self, initial_prompt):