Skip to content

Commit 7e39d55

Browse files
tonybaloneyCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot <[email protected]>
1 parent d8fc473 commit 7e39d55

File tree

7 files changed

+101
-93
lines changed

7 files changed

+101
-93
lines changed

cookbook/copilot-sdk/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ go run <filename>.go
8787

8888
## Status
8989

90-
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.
90+
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.

cookbook/copilot-sdk/dotnet/ralph-loop.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Implement self-referential feedback loops where an AI agent iteratively improves
55
> **Runnable example:** [recipe/ralph-loop.cs](recipe/ralph-loop.cs)
66
>
77
> ```bash
8-
> cd dotnet/recipe
9-
> dotnet run ralph-loop.cs
8+
> cd dotnet
9+
> dotnet run recipe/ralph-loop.cs
1010
> ```
1111
1212
## What is RALPH-loop?

cookbook/copilot-sdk/go/ralph-loop.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Implement self-referential feedback loops where an AI agent iteratively improves
55
> **Runnable example:** [recipe/ralph-loop.go](recipe/ralph-loop.go)
66
>
77
> ```bash
8-
> cd go/recipe
9-
> go run ralph-loop.go
8+
> cd go
9+
> go run recipe/ralph-loop.go
1010
> ```
1111
1212
## What is RALPH-loop?
@@ -20,13 +20,13 @@ RALPH-loop is a development methodology for iterative AI-powered task completion
2020
2121
## Example Scenario
2222
23-
You need to iteratively improve code until all tests pass. Instead of asking Claude to "write perfect code," you use RALPH-loop to:
23+
You need to iteratively improve code until all tests pass. Instead of asking Copilot to "write perfect code," you use RALPH-loop to:
2424
2525
1. Send the initial prompt with clear success criteria
26-
2. Claude writes code and tests
27-
3. Claude runs tests and sees failures
26+
2. Copilot writes code and tests
27+
3. Copilot runs tests and sees failures
2828
4. Loop automatically re-sends the prompt
29-
5. Claude reads test output and previous code, fixes issues
29+
5. Copilot reads test output and previous code, fixes issues
3030
6. Repeat until all tests pass and completion promise is output
3131
3232
## Basic Implementation

cookbook/copilot-sdk/nodejs/ralph-loop.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ Implement self-referential feedback loops where an AI agent iteratively improves
55
> **Runnable example:** [recipe/ralph-loop.ts](recipe/ralph-loop.ts)
66
>
77
> ```bash
8-
> cd nodejs/recipe
9-
> npm install
8+
> cd recipe && npm install
109
> npx tsx ralph-loop.ts
1110
> ```
1211
@@ -21,13 +20,13 @@ RALPH-loop is a development methodology for iterative AI-powered task completion
2120
2221
## Example Scenario
2322
24-
You need to iteratively improve code until all tests pass. Instead of asking Claude to "write perfect code," you use RALPH-loop to:
23+
You need to iteratively improve code until all tests pass. Instead of asking Copilot to "write perfect code," you use RALPH-loop to:
2524
2625
1. Send the initial prompt with clear success criteria
27-
2. Claude writes code and tests
28-
3. Claude runs tests and sees failures
26+
2. Copilot writes code and tests
27+
3. Copilot runs tests and sees failures
2928
4. Loop automatically re-sends the prompt
30-
5. Claude reads test output and previous code, fixes issues
29+
5. Copilot reads test output and previous code, fixes issues
3130
6. Repeat until all tests pass and completion promise is output
3231
3332
## Basic Implementation

cookbook/copilot-sdk/nodejs/recipe/ralph-loop.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,54 @@ class RalphLoop {
2222
* Run the RALPH-loop until completion promise is detected or max iterations reached.
2323
*/
2424
async run(initialPrompt: string): Promise<string> {
25-
await this.client.start();
26-
const session = await this.client.createSession({
27-
model: "gpt-5.1-codex-mini"
28-
});
25+
let session: Awaited<ReturnType<CopilotClient["createSession"]>> | null = null;
2926

27+
await this.client.start();
3028
try {
31-
while (this.iteration < this.maxIterations) {
32-
this.iteration++;
33-
console.log(`\n=== Iteration ${this.iteration}/${this.maxIterations} ===`);
34-
35-
// Build the prompt for this iteration
36-
const currentPrompt = this.buildIterationPrompt(initialPrompt);
37-
console.log(`Sending prompt (length: ${currentPrompt.length})...`);
38-
39-
const response = await session.sendAndWait({ prompt: currentPrompt }, 300_000);
40-
this.lastResponse = response?.data.content || "";
41-
42-
// Display response summary
43-
const summary = this.lastResponse.length > 200
44-
? this.lastResponse.substring(0, 200) + "..."
45-
: this.lastResponse;
46-
console.log(`Response: ${summary}`);
47-
48-
// Check for completion promise
49-
if (this.lastResponse.includes(this.completionPromise)) {
50-
console.log(`\n✓ Success! Completion promise detected: '${this.completionPromise}'`);
51-
return this.lastResponse;
29+
session = await this.client.createSession({
30+
model: "gpt-5.1-codex-mini"
31+
});
32+
33+
try {
34+
while (this.iteration < this.maxIterations) {
35+
this.iteration++;
36+
console.log(`\n=== Iteration ${this.iteration}/${this.maxIterations} ===`);
37+
38+
// Build the prompt for this iteration
39+
const currentPrompt = this.buildIterationPrompt(initialPrompt);
40+
console.log(`Sending prompt (length: ${currentPrompt.length})...`);
41+
42+
const response = await session.sendAndWait({ prompt: currentPrompt }, 300_000);
43+
this.lastResponse = response?.data.content || "";
44+
45+
// Display response summary
46+
const summary = this.lastResponse.length > 200
47+
? this.lastResponse.substring(0, 200) + "..."
48+
: this.lastResponse;
49+
console.log(`Response: ${summary}`);
50+
51+
// Check for completion promise
52+
if (this.lastResponse.includes(this.completionPromise)) {
53+
console.log(`\n✓ Success! Completion promise detected: '${this.completionPromise}'`);
54+
return this.lastResponse;
55+
}
56+
57+
console.log(`Iteration ${this.iteration} complete. Checking for next iteration...`);
5258
}
5359

54-
console.log(`Iteration ${this.iteration} complete. Checking for next iteration...`);
60+
// Max iterations reached without completion
61+
throw new Error(
62+
`Maximum iterations (${this.maxIterations}) reached without detecting completion promise: '${this.completionPromise}'`
63+
);
64+
} catch (error) {
65+
console.error(`\nError during RALPH-loop: ${error instanceof Error ? error.message : String(error)}`);
66+
throw error;
67+
} finally {
68+
if (session) {
69+
await session.destroy();
70+
}
5571
}
56-
57-
// Max iterations reached without completion
58-
throw new Error(
59-
`Maximum iterations (${this.maxIterations}) reached without detecting completion promise: '${this.completionPromise}'`
60-
);
61-
} catch (error) {
62-
console.error(`\nError during RALPH-loop: ${error instanceof Error ? error.message : String(error)}`);
63-
throw error;
6472
} finally {
65-
await session.destroy();
6673
await this.client.stop();
6774
}
6875
}

cookbook/copilot-sdk/python/ralph-loop.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ Implement self-referential feedback loops where an AI agent iteratively improves
55
> **Runnable example:** [recipe/ralph_loop.py](recipe/ralph_loop.py)
66
>
77
> ```bash
8-
> cd python/recipe
9-
> pip install -r requirements.txt
8+
> cd recipe && pip install -r requirements.txt
109
> python ralph_loop.py
1110
> ```
12-
1311
## What is RALPH-loop?
1412
1513
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
2119
2220
## Example Scenario
2321
24-
You need to iteratively improve code until all tests pass. Instead of asking Claude to "write perfect code," you use RALPH-loop to:
22+
You need to iteratively improve code until all tests pass. Instead of asking Copilot to "write perfect code," you use RALPH-loop to:
2523
2624
1. Send the initial prompt with clear success criteria
27-
2. Claude writes code and tests
28-
3. Claude runs tests and sees failures
25+
2. Copilot writes code and tests
26+
3. Copilot runs tests and sees failures
2927
4. Loop automatically re-sends the prompt
30-
5. Claude reads test output and previous code, fixes issues
28+
5. Copilot reads test output and previous code, fixes issues
3129
6. Repeat until all tests pass and completion promise is output
3230
3331
## Basic Implementation

cookbook/copilot-sdk/python/recipe/ralph_loop.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,59 @@ async def run(self, initial_prompt):
2525
"""
2626
Run the RALPH-loop until completion promise is detected or max iterations reached.
2727
"""
28+
session = None
2829
await self.client.start()
29-
session = await self.client.create_session(
30-
SessionConfig(model="gpt-5.1-codex-mini")
31-
)
32-
3330
try:
34-
while self.iteration < self.max_iterations:
35-
self.iteration += 1
36-
print(f"\n=== Iteration {self.iteration}/{self.max_iterations} ===")
31+
session = await self.client.create_session(
32+
SessionConfig(model="gpt-5.1-codex-mini")
33+
)
3734

38-
current_prompt = self._build_iteration_prompt(initial_prompt)
39-
print(f"Sending prompt (length: {len(current_prompt)})...")
35+
try:
36+
while self.iteration < self.max_iterations:
37+
self.iteration += 1
38+
print(f"\n=== Iteration {self.iteration}/{self.max_iterations} ===")
4039

41-
result = await session.send_and_wait(
42-
MessageOptions(prompt=current_prompt),
43-
timeout=300,
44-
)
40+
current_prompt = self._build_iteration_prompt(initial_prompt)
41+
print(f"Sending prompt (length: {len(current_prompt)})...")
4542

46-
self.last_response = result.data.content if result else ""
43+
result = await session.send_and_wait(
44+
MessageOptions(prompt=current_prompt),
45+
timeout=300,
46+
)
4747

48-
# Display response summary
49-
summary = (
50-
self.last_response[:200] + "..."
51-
if len(self.last_response) > 200
52-
else self.last_response
53-
)
54-
print(f"Response: {summary}")
48+
self.last_response = result.data.content if result else ""
49+
50+
# Display response summary
51+
summary = (
52+
self.last_response[:200] + "..."
53+
if len(self.last_response) > 200
54+
else self.last_response
55+
)
56+
print(f"Response: {summary}")
57+
58+
# Check for completion promise
59+
if self.completion_promise in self.last_response:
60+
print(
61+
f"\n✓ Success! Completion promise detected: '{self.completion_promise}'"
62+
)
63+
return self.last_response
5564

56-
# Check for completion promise
57-
if self.completion_promise in self.last_response:
5865
print(
59-
f"\n✓ Success! Completion promise detected: '{self.completion_promise}'"
66+
f"Iteration {self.iteration} complete. Checking for next iteration..."
6067
)
61-
return self.last_response
6268

63-
print(
64-
f"Iteration {self.iteration} complete. Checking for next iteration..."
69+
raise RuntimeError(
70+
f"Maximum iterations ({self.max_iterations}) reached without "
71+
f"detecting completion promise: '{self.completion_promise}'"
6572
)
6673

67-
raise RuntimeError(
68-
f"Maximum iterations ({self.max_iterations}) reached without "
69-
f"detecting completion promise: '{self.completion_promise}'"
70-
)
71-
72-
except Exception as e:
73-
print(f"\nError during RALPH-loop: {e}")
74-
raise
74+
except Exception as e:
75+
print(f"\nError during RALPH-loop: {e}")
76+
raise
77+
finally:
78+
if session is not None:
79+
await session.destroy()
7580
finally:
76-
await session.destroy()
7781
await self.client.stop()
7882

7983
def _build_iteration_prompt(self, initial_prompt):

0 commit comments

Comments
 (0)