Address review feedback: fix event handler leak, error handling, model alignment

- Move session.On handler outside loop to prevent handler accumulation (C#)
- Use TrySetResult instead of SetResult to avoid duplicate-set exceptions (C#)
- Wrap CreateSessionAsync in broader try/finally so client always stops (C#)
- Fix PersistentRalphLoop to use maxIterations parameter instead of hardcoded 10
- Align model name to gpt-5.1-codex-mini across all doc snippets
- Fix completion promise DONE -> COMPLETE in usage snippet
- Replace Claude references with generic model terminology
This commit is contained in:
Anthony Shaw
2026-02-11 06:35:14 -08:00
parent bb9f63a899
commit ab82accc08
3 changed files with 119 additions and 88 deletions

View File

@@ -62,54 +62,63 @@ public class RalphLoop
public async Task<string> RunAsync(string initialPrompt)
{
await _client.StartAsync();
var session = await _client.CreateSessionAsync(new SessionConfig
{
Model = "gpt-5.1-codex-mini"
});
try
{
while (_iteration < _maxIterations)
{
_iteration++;
Console.WriteLine($"\n=== Iteration {_iteration}/{_maxIterations} ===");
var session = await _client.CreateSessionAsync(new SessionConfig
{
Model = "gpt-5.1-codex-mini"
});
try
{
var done = new TaskCompletionSource<string>();
session.On(evt =>
{
if (evt is AssistantMessageEvent msg)
{
_lastResponse = msg.Data.Content;
done.SetResult(msg.Data.Content);
done.TrySetResult(msg.Data.Content);
}
});
var currentPrompt = BuildIterationPrompt(initialPrompt);
Console.WriteLine($"Sending prompt (length: {currentPrompt.Length})...");
await session.SendAsync(new MessageOptions { Prompt = currentPrompt });
var response = await done.Task;
var summary = response.Length > 200
? response.Substring(0, 200) + "..."
: response;
Console.WriteLine($"Response: {summary}");
if (response.Contains(_completionPromise))
while (_iteration < _maxIterations)
{
Console.WriteLine($"\n✓ Completion promise detected: '{_completionPromise}'");
return response;
_iteration++;
Console.WriteLine($"\n=== Iteration {_iteration}/{_maxIterations} ===");
done = new TaskCompletionSource<string>();
var currentPrompt = BuildIterationPrompt(initialPrompt);
Console.WriteLine($"Sending prompt (length: {currentPrompt.Length})...");
await session.SendAsync(new MessageOptions { Prompt = currentPrompt });
var response = await done.Task;
var summary = response.Length > 200
? response.Substring(0, 200) + "..."
: response;
Console.WriteLine($"Response: {summary}");
if (response.Contains(_completionPromise))
{
Console.WriteLine($"\n✓ Completion promise detected: '{_completionPromise}'");
return response;
}
Console.WriteLine($"Iteration {_iteration} complete. Continuing...");
}
Console.WriteLine($"Iteration {_iteration} complete. Continuing...");
throw new InvalidOperationException(
$"Max iterations ({_maxIterations}) reached without completion promise: '{_completionPromise}'");
}
finally
{
await session.DisposeAsync();
}
throw new InvalidOperationException(
$"Max iterations ({_maxIterations}) reached without completion promise: '{_completionPromise}'");
}
finally
{
await session.DisposeAsync();
await _client.StopAsync();
}
}