| 15 | */ |
| 16 | public class RalphLoop { |
| 17 | public static void main(String[] args) throws Exception { |
| 18 | String promptFile = args.length > 0 ? args[0] : "PROMPT.md"; |
| 19 | int maxIterations = args.length > 1 ? Integer.parseInt(args[1]) : 50; |
| 20 | |
| 21 | System.out.printf("Ralph Loop — prompt: %s, max iterations: %d%n", promptFile, maxIterations); |
| 22 | |
| 23 | try (var client = new CopilotClient()) { |
| 24 | client.start().get(); |
| 25 | |
| 26 | String prompt = Files.readString(Path.of(promptFile)); |
| 27 | |
| 28 | for (int i = 1; i <= maxIterations; i++) { |
| 29 | System.out.printf("%n=== Iteration %d/%d ===%n", i, maxIterations); |
| 30 | |
| 31 | // Fresh session each iteration — context isolation is the point |
| 32 | var session = client.createSession( |
| 33 | new SessionConfig() |
| 34 | .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 35 | .setModel("gpt-5.1-codex-mini") |
| 36 | .setWorkingDirectory(System.getProperty("user.dir")) |
| 37 | ).get(); |
| 38 | |
| 39 | // Log tool usage for visibility |
| 40 | session.on(ToolExecutionStartEvent.class, |
| 41 | ev -> System.out.printf(" ⚙ %s%n", ev.getData().toolName())); |
| 42 | |
| 43 | try { |
| 44 | session.sendAndWait(new MessageOptions().setPrompt(prompt)).get(); |
| 45 | } finally { |
| 46 | session.close(); |
| 47 | } |
| 48 | |
| 49 | System.out.printf("Iteration %d complete.%n", i); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | System.out.println("\nAll iterations complete."); |
| 54 | } |
| 55 | } |