@@ -1,5 +1,6 @@
11 const std = @import("std");
22 const platform = @import("platform.zig");
3+const time = @import("time.zig");
34 const build_options = @import("build_options");
45
56 const user_agent = "mini-coding-agent/" ++ build_options.version;
@@ -19,25 +20,61 @@ const HttpError = error{
1920 RequestFailed,
2021 HttpStatus,
2122 ReadFailed,
23+ IdleTimeout,
2224 Aborted,
2325 } || std.mem.Allocator.Error;
2426
25-// Blocking socket reads cannot observe the cancel token, so a watcher thread
26-// shuts the connection down when a cancel is requested, unblocking the read.
27-const CancelWatch = struct {
27+/// How a request fails over: the retries after the first attempt, the first
28+/// backoff step (each retry doubles it), and how long a stream may stay silent
29+/// before the connection is treated as dead.
30+pub const Retry = struct {
31+ attempts: usize = 3,
32+ base_ms: u64 = 1000,
33+ idle_ms: u64 = 120_000,
34+};
35+
36+const max_backoff_ms: u64 = 30_000;
37+
38+/// Told about every retry before it happens, so both front ends can say why the
39+/// request is being repeated.
40+pub const RetryNotice = struct {
41+ ctx: *anyopaque,
42+ on_retry: *const fn (ctx: *anyopaque, attempt: usize, total: usize, delay_ms: u64, reason: []const u8) void,
43+
44+ fn call(self: RetryNotice, attempt: usize, total: usize, delay_ms: u64, reason: []const u8) void {
45+ self.on_retry(self.ctx, attempt, total, delay_ms, reason);
46+ }
47+};
48+
49+/// Blocking socket reads cannot observe the cancel token or the idle window, so
50+/// a watcher thread shuts the connection down when either is spent, unblocking
51+/// the read that cannot see them.
52+const Watch = struct {
2853 cancel: *const std.atomic.Value(bool),
2954 connection: *std.http.Client.Connection,
55+ idle_ms: u64,
56+ last: std.atomic.Value(i64),
57+ idle: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
3058 done: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
3159
32- fn run(self: *CancelWatch) void {
60+ fn run(self: *Watch) void {
3361 while (!self.done.load(.acquire)) {
3462 if (self.cancel.load(.acquire)) {
35- self.connection.stream_reader.stream.shutdown(platform.io, .both) catch {};
63+ self.hangUp();
64+ return;
65+ }
66+ if (self.idle_ms > 0 and time.nowMs() - self.last.load(.acquire) >= self.idle_ms) {
67+ self.idle.store(true, .release);
68+ self.hangUp();
3669 return;
3770 }
3871 std.Io.sleep(platform.io, .{ .nanoseconds = 25 * std.time.ns_per_ms }, .boot) catch {};
3972 }
4073 }
74+
75+ fn hangUp(self: *Watch) void {
76+ self.connection.stream_reader.stream.shutdown(platform.io, .both) catch {};
77+ }
4178 };
4279
4380 /// One request and its response, with the buffers the response reader borrows.
@@ -97,17 +134,44 @@ const Exchange = struct {
97134 }
98135 };
99136
137+/// What one attempt achieved: the status it saw, whether the sink already
138+/// received events (a partial stream cannot be repeated), and how it ended.
139+const Attempt = struct {
140+ status: u16 = 0,
141+ sent: bool = false,
142+ err: ?HttpError = null,
143+};
144+
100145 pub fn get(
101146 a: std.mem.Allocator,
102147 url: []const u8,
103148 headers: []const Header,
104149 cancel: *const std.atomic.Value(bool),
150+ retry: Retry,
151+) HttpError![]u8 {
152+ var attempt: usize = 0;
153+ while (true) : (attempt += 1) {
154+ var out = Attempt{};
155+ if (getOnce(a, url, headers, cancel, &out)) |body| return body else |e| out.err = e;
156+ const e = out.err orelse error.ReadFailed;
157+ if (attempt + 1 >= retry.attempts + 1 or !retryable(e, out.status)) return e;
158+ try pause(cancel, backoff(retry.base_ms, attempt));
159+ }
160+}
161+
162+fn getOnce(
163+ a: std.mem.Allocator,
164+ url: []const u8,
165+ headers: []const Header,
166+ cancel: *const std.atomic.Value(bool),
167+ out: *Attempt,
105168 ) HttpError![]u8 {
106169 if (cancel.load(.acquire)) return error.Aborted;
107170 var ex: Exchange = undefined;
108171 try ex.open(a, .GET, url, headers, null);
109172 defer ex.close();
110173 try ex.receive();
174+ out.status = ex.status;
111175 if (!ex.ok()) return error.HttpStatus;
112176 return ex.reader.allocRemaining(a, .limited(1 << 22)) catch return error.ReadFailed;
113177 }
@@ -120,23 +184,62 @@ pub fn postSse(
120184 handler: SseHandler,
121185 cancel: *const std.atomic.Value(bool),
122186 err_body: *?[]const u8,
187+ retry: Retry,
188+ notice: ?RetryNotice,
123189 ) HttpError!void {
124- if (cancel.load(.acquire)) return error.Aborted;
190+ var attempt: usize = 0;
191+ while (true) : (attempt += 1) {
192+ err_body.* = null;
193+ const out = postOnce(a, url, headers, body, handler, cancel, err_body, retry.idle_ms);
194+ const e = out.err orelse return;
195+ if (out.sent or attempt + 1 >= retry.attempts + 1 or !retryable(e, out.status)) return e;
196+ const delay_ms = backoff(retry.base_ms, attempt);
197+ if (notice) |n| {
198+ var buf: [16]u8 = undefined;
199+ n.call(attempt + 2, retry.attempts + 1, delay_ms, describe(e, out.status, &buf));
200+ }
201+ try pause(cancel, delay_ms);
202+ }
203+}
204+
205+fn postOnce(
206+ a: std.mem.Allocator,
207+ url: []const u8,
208+ headers: []const Header,
209+ body: []const u8,
210+ handler: SseHandler,
211+ cancel: *const std.atomic.Value(bool),
212+ err_body: *?[]const u8,
213+ idle_ms: u64,
214+) Attempt {
215+ var out = Attempt{};
216+ if (cancel.load(.acquire)) {
217+ out.err = error.Aborted;
218+ return out;
219+ }
125220 var ex: Exchange = undefined;
126- try ex.open(a, .POST, url, headers, body);
221+ ex.open(a, .POST, url, headers, body) catch |e| {
222+ out.err = e;
223+ return out;
224+ };
127225 defer ex.close();
128226
129- var watch = CancelWatch{ .cancel = cancel, .connection = ex.req.connection.? };
130- const watcher = std.Thread.spawn(.{}, CancelWatch.run, .{&watch}) catch null;
227+ var watch = Watch{ .cancel = cancel, .connection = ex.req.connection.?, .idle_ms = idle_ms, .last = std.atomic.Value(i64).init(time.nowMs()) };
228+ const watcher = std.Thread.spawn(.{}, Watch.run, .{&watch}) catch null;
131229 defer {
132230 watch.done.store(true, .release);
133231 if (watcher) |t| t.join();
134232 }
135233
136- try ex.receive();
234+ ex.receive() catch {
235+ out.err = if (watch.idle.load(.acquire)) error.IdleTimeout else error.RequestFailed;
236+ return out;
237+ };
238+ out.status = ex.status;
137239 if (!ex.ok()) {
138240 err_body.* = ex.reader.allocRemaining(a, .limited(1 << 20)) catch null;
139- return error.HttpStatus;
241+ out.err = error.HttpStatus;
242+ return out;
140243 }
141244
142245 var data: std.ArrayList(u8) = .empty;
@@ -151,28 +254,97 @@ pub fn postSse(
151254 // reader's buffer sends the delimiter search down a failure path that
152255 // desynchronizes chunked transfer decoding.
153256 while (true) {
154- if (cancel.load(.acquire)) return error.Aborted;
155- const n = ex.reader.readSliceShort(&buf) catch return error.ReadFailed;
257+ if (cancel.load(.acquire)) {
258+ out.err = error.Aborted;
259+ return out;
260+ }
261+ const n = ex.reader.readSliceShort(&buf) catch {
262+ out.err = if (watch.idle.load(.acquire)) error.IdleTimeout else error.ReadFailed;
263+ return out;
264+ };
156265 if (n == 0) break;
266+ watch.last.store(time.nowMs(), .release);
157267 var rest: []const u8 = buf[0..n];
158268 while (std.mem.indexOfScalar(u8, rest, '\n')) |nl| {
159- try line.appendSlice(a, rest[0..nl]);
269+ line.appendSlice(a, rest[0..nl]) catch {
270+ out.err = error.OutOfMemory;
271+ return out;
272+ };
160273 rest = rest[nl + 1 ..];
161- if (try feedLine(a, line.items, &data, &have_data)) {
274+ const complete = feedLine(a, line.items, &data, &have_data) catch {
275+ out.err = error.OutOfMemory;
276+ return out;
277+ };
278+ if (complete) {
279+ out.sent = true;
162280 handler.call(data.items);
163281 data.clearRetainingCapacity();
164282 have_data = false;
165283 }
166284 line.clearRetainingCapacity();
167285 }
168- try line.appendSlice(a, rest);
286+ line.appendSlice(a, rest) catch {
287+ out.err = error.OutOfMemory;
288+ return out;
289+ };
290+ }
291+ // The watcher shuts a dead stream down, which can end the read cleanly; the
292+ // flag is what tells a finished stream from a silent one.
293+ if (watch.idle.load(.acquire)) {
294+ out.err = error.IdleTimeout;
295+ return out;
296+ }
297+ if (line.items.len > 0) {
298+ const complete = feedLine(a, line.items, &data, &have_data) catch {
299+ out.err = error.OutOfMemory;
300+ return out;
301+ };
302+ if (complete) {
303+ out.sent = true;
304+ handler.call(data.items);
305+ data.clearRetainingCapacity();
306+ have_data = false;
307+ }
169308 }
170- if (line.items.len > 0 and try feedLine(a, line.items, &data, &have_data)) {
309+ if (have_data) {
310+ out.sent = true;
171311 handler.call(data.items);
172- data.clearRetainingCapacity();
173- have_data = false;
174312 }
175- if (have_data) handler.call(data.items);
313+ return out;
314+}
315+
316+fn retryable(e: HttpError, status: u16) bool {
317+ return switch (e) {
318+ error.RequestFailed, error.ReadFailed, error.IdleTimeout => true,
319+ error.HttpStatus => status == 429 or status >= 500,
320+ error.Aborted, error.OutOfMemory => false,
321+ };
322+}
323+
324+fn backoff(base_ms: u64, attempt: usize) u64 {
325+ const shift: u6 = @intCast(@min(attempt, 6));
326+ return @min(base_ms << shift, max_backoff_ms);
327+}
328+
329+fn describe(e: HttpError, status: u16, buf: []u8) []const u8 {
330+ return switch (e) {
331+ error.HttpStatus => std.fmt.bufPrint(buf, "http {d}", .{status}) catch "http error",
332+ error.IdleTimeout => "idle timeout",
333+ error.ReadFailed => "stream interrupted",
334+ else => "connection failed",
335+ };
336+}
337+
338+/// Waits out the backoff in small steps, so a cancel lands promptly.
339+fn pause(cancel: *const std.atomic.Value(bool), delay_ms: u64) HttpError!void {
340+ var left = delay_ms;
341+ while (left > 0) {
342+ if (cancel.load(.acquire)) return error.Aborted;
343+ const step: u64 = @min(left, 25);
344+ std.Io.sleep(platform.io, .{ .nanoseconds = step * std.time.ns_per_ms }, .boot) catch {};
345+ left -= step;
346+ }
347+ if (cancel.load(.acquire)) return error.Aborted;
176348 }
177349
178350 /// Applies one SSE line to the pending event payload, returning whether the