|
| 1 | +// agent_loop.zig — Sleep-wake cycle for Ralph autonomous agent |
| 2 | +const std = @import("std"); |
| 3 | +const identity_mod = @import("identity.zig"); |
| 4 | +const handover = @import("handover.zig"); |
| 5 | +const github_poller = @import("github_poller.zig"); |
| 6 | +const context_builder = @import("context_builder.zig"); |
| 7 | +const claude_runner = @import("claude_runner.zig"); |
| 8 | +const state_mod = @import("state.zig"); |
| 9 | + |
| 10 | +pub const Config = struct { |
| 11 | + project_root: []const u8, |
| 12 | + gh_token: []const u8, |
| 13 | + owner: []const u8, |
| 14 | + repo: []const u8, |
| 15 | + sleep_interval_s: u64 = 1800, // 30 minutes |
| 16 | + max_turns: u32 = 50, |
| 17 | + max_wakes: u32 = 0, // 0 = infinite |
| 18 | + single_shot: bool = false, // true = run once and exit |
| 19 | +}; |
| 20 | + |
| 21 | +fn log(comptime fmt: []const u8, args: anytype) void { |
| 22 | + std.debug.print("[ralph-agent] " ++ fmt ++ "\n", args); |
| 23 | +} |
| 24 | + |
| 25 | +/// Run the sleep-wake loop. |
| 26 | +pub fn run(allocator: std.mem.Allocator, config: Config) !void { |
| 27 | + var state = try state_mod.State.init(allocator, config.project_root); |
| 28 | + defer state.deinit(); |
| 29 | + |
| 30 | + log("Starting sleep-wake loop", .{}); |
| 31 | + log(" project: {s}", .{config.project_root}); |
| 32 | + log(" repo: {s}/{s}", .{ config.owner, config.repo }); |
| 33 | + log(" sleep interval: {d}s", .{config.sleep_interval_s}); |
| 34 | + log(" max turns: {d}", .{config.max_turns}); |
| 35 | + |
| 36 | + while (true) { |
| 37 | + // === WAKE === |
| 38 | + const wake_count = state.incrementWakeCount() catch 0; |
| 39 | + log("=== WAKE #{d} ===", .{wake_count}); |
| 40 | + |
| 41 | + if (config.max_wakes > 0 and wake_count > config.max_wakes) { |
| 42 | + log("Max wakes ({d}) reached. Exiting.", .{config.max_wakes}); |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + // Load identity |
| 47 | + var id = identity_mod.load(allocator, config.project_root); |
| 48 | + defer id.deinit(); |
| 49 | + |
| 50 | + // Read previous handover |
| 51 | + const handover_content = handover.read(allocator, config.project_root); |
| 52 | + defer if (handover_content) |h| allocator.free(h); |
| 53 | + |
| 54 | + // Poll GitHub for pending issues |
| 55 | + log("Polling GitHub issues...", .{}); |
| 56 | + const issues_json = github_poller.fetchPending( |
| 57 | + allocator, |
| 58 | + config.owner, |
| 59 | + config.repo, |
| 60 | + config.gh_token, |
| 61 | + ); |
| 62 | + defer if (issues_json) |j| allocator.free(j); |
| 63 | + |
| 64 | + if (issues_json == null) { |
| 65 | + log("No pending issues or GitHub API unavailable. Sleeping.", .{}); |
| 66 | + if (config.single_shot) break; |
| 67 | + std.Thread.sleep(config.sleep_interval_s * std.time.ns_per_s); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // Read current state |
| 72 | + const current_issue = state.read("current_issue"); |
| 73 | + defer if (current_issue) |v| allocator.free(v); |
| 74 | + const current_branch = state.read("current_branch"); |
| 75 | + defer if (current_branch) |v| allocator.free(v); |
| 76 | + |
| 77 | + // === BUILD CONTEXT === |
| 78 | + const prompt = try context_builder.build(allocator, .{ |
| 79 | + .identity = id.content, |
| 80 | + .handover_content = handover_content, |
| 81 | + .issues_json = issues_json, |
| 82 | + .current_issue = current_issue, |
| 83 | + .current_branch = current_branch, |
| 84 | + .wake_count = wake_count, |
| 85 | + }); |
| 86 | + defer allocator.free(prompt); |
| 87 | + |
| 88 | + log("Context built ({d} bytes). Spawning Claude CLI...", .{prompt.len}); |
| 89 | + |
| 90 | + // === WORK === |
| 91 | + var result = claude_runner.spawn( |
| 92 | + allocator, |
| 93 | + prompt, |
| 94 | + config.project_root, |
| 95 | + config.max_turns, |
| 96 | + ) catch |err| { |
| 97 | + log("Claude spawn error: {s}", .{@errorName(err)}); |
| 98 | + if (config.single_shot) break; |
| 99 | + std.Thread.sleep(config.sleep_interval_s * std.time.ns_per_s); |
| 100 | + continue; |
| 101 | + }; |
| 102 | + defer result.deinit(); |
| 103 | + |
| 104 | + log("Claude exited with code {d} ({d} bytes output)", .{ result.exit_code, result.stdout.len }); |
| 105 | + |
| 106 | + // Save session log |
| 107 | + claude_runner.saveLog(allocator, config.project_root, result.stdout); |
| 108 | + |
| 109 | + // === SLEEP === |
| 110 | + // Check if handover was written by the session |
| 111 | + const new_handover = handover.read(allocator, config.project_root); |
| 112 | + if (new_handover) |nh| { |
| 113 | + allocator.free(nh); |
| 114 | + } else { |
| 115 | + // Emergency handover — session didn't write one |
| 116 | + log("WARNING: No handover written. Creating emergency handover.", .{}); |
| 117 | + handover.writeEmergency(allocator, config.project_root, wake_count, current_issue) catch { |
| 118 | + log("Failed to write emergency handover!", .{}); |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + // Update state |
| 123 | + var count_buf: [16]u8 = undefined; |
| 124 | + const count_str = std.fmt.bufPrint(&count_buf, "{d}", .{wake_count}) catch "0"; |
| 125 | + state.write("last_wake", count_str) catch {}; |
| 126 | + |
| 127 | + if (config.single_shot) { |
| 128 | + log("Single-shot mode. Exiting.", .{}); |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + log("Sleeping for {d}s...", .{config.sleep_interval_s}); |
| 133 | + std.Thread.sleep(config.sleep_interval_s * std.time.ns_per_s); |
| 134 | + } |
| 135 | + |
| 136 | + log("Agent loop terminated.", .{}); |
| 137 | +} |
0 commit comments