graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Keep HTTP/2 requests from hanging, double-sending, or ignoring Esc.
justrachcc040b7398cf
diff --git a/build.zig.zon b/build.zig.zonindex ea990b69..c52458b8 100644--- a/build.zig.zon+++ b/build.zig.zon@@ -8,8 +8,8 @@.hash = "accord-0.0.0-aQy1J3iBAgADDHWM0hIAXhISKtQ0zasFN7TyLhI_tkI8",},.http_zig = .{- .url = "git+https://github.com/justrach/http-zig#a9d55d9a12257a4ff0e7adedbdd62d914a5142b0",- .hash = "http_zig-0.1.0-2CB7E97hAgAw1_SDpmoE2fevdDvfA4u6zVOs0K0KD9Mo",+ .url = "git+https://github.com/justrach/http-zig#785afb52ee5c7633577c50ffd57463b484810fb1",+ .hash = "http_zig-0.1.0-2CB7E2r-AgC2AJp4zQIzrr1qJRqne4EKdbgHl7oAo2Tt",},},.paths = .{""},diff --git a/src/agent_stream.zig b/src/agent_stream.zigindex 6bcaf580..221c38c2 100644--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@ -103,8 +103,6 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consself.md_table.clearRetainingCapacity();self.partial_text.clearRetainingCapacity(); // fresh Esc-interrupt capture- if (try @import("agent_stream_h2.zig").postStream(self, body)) |full| return full;-// Esc-interrupt: while the root's request is on a TTY, stdin sits in raw// non-blocking no-echo mode — from *before* the connect, so Esc pressed// during a slow time-to-first-token wait neither echoes ^[ nor leaks into@@ -120,6 +118,9 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []cons_ = drainSteerStdin(true);restoreStdin(o);};+ // HTTP/2 first, under the same raw-stdin window so Esc and steering work+ // there too; null means "not sent, use HTTP/1.1".+ if (try @import("agent_stream_h2.zig").postStream(self, body, orig_tio != null)) |full| return full;var req = transport.request(.POST, try std.Uri.parse(provider.url), .{.redirect_behavior = .unhandled,.headers = .{diff --git a/src/agent_stream_h2.zig b/src/agent_stream_h2.zigindex 8977c71e..3236ab09 100644--- a/src/agent_stream_h2.zig+++ b/src/agent_stream_h2.zig@@ -1,4 +1,9 @@-//! HTTPS SSE over http-zig. Returns null to keep the std.http/1.1 path.+//! HTTPS SSE over http-zig. Returns null to keep the std.http/1.1 path, but+//! only while the request provably never reached the server: the dial or the+//! send failed, the server did not pick h2 via ALPN, or it answered GOAWAY /+//! REFUSED_STREAM for our stream (RFC 9113 §8.7). Once the request is on the+//! wire any other failure is an error for request()'s retry loop -- falling+//! back would POST the same prompt twice and bill two generations.const std = @import("std");const Io = std.Io;@@ -12,15 +17,26 @@ const engine_sink = @import("engine_sink.zig");const http_zig = @import("http_zig");const WatchdogFired = http.WatchdogFired;const watchdogError = http.watchdogError;-const deadlineStallTask = @import("http.zig").deadlineStallTask;+const deadlineStallTask = http.deadlineStallTask;+const headStallTask = http.headStallTask;const ssePayload = Agent.ssePayload;+const escPressed = Agent.escPressed;const openaiComplete = @import("agent_stream.zig").openaiComplete;const isStreamEnd = @import("agent_stream.zig").isStreamEnd;-pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {+/// After the provider's terminal event, how long to wait for a trailing+/// END_STREAM (servers often send it as its own empty DATA frame) before+/// giving up on reusing the connection.+const trailing_end_ms: u64 = 500;++/// `poll_stdin`: the root REPL has stdin in raw non-blocking mode (set up by+/// agent_stream.postStream before either transport runs), so Esc and steering+/// keystrokes are polled here exactly as on the HTTP/1.1 path.+pub fn postStream(self: *Agent, body: []const u8, poll_stdin: bool) !?[]u8 {if (!http2_pool.want(self.provider.url)) return null;const origin = http2_pool.parseOrigin(self.provider.url) catch return null;+ if (http2_pool.knownH1(self.io, origin.host, origin.port)) return null;const gpa = self.gpa;const provider = self.provider;const bearer = switch (provider.auth) {@@ -48,26 +64,39 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {n += 1;}- const lease = http2_pool.acquire(gpa, self.io, origin.host, origin.port) catch return null;- const sess = lease.session;+ var head: Head = .{+ .gpa = gpa,+ .io = self.io,+ .origin = origin,+ .openai = std.mem.startsWith(u8, provider.id, "openai"),+ .req = .{+ .method = "POST",+ .scheme = "https",+ .authority = origin.host,+ .path = origin.path,+ .extra = extras[0..n],+ .body = body,+ },+ };var keep = false;- defer lease.release(keep);- var lines = sess.startLines(.{- .method = "POST",- .scheme = "https",- .authority = origin.host,- .path = origin.path,- .extra = extras[0..n],- .body = body,- }) catch return null;- defer lines.deinit();-- const status = lines.waitStatus() catch return null;- if (status == 429) return error.RateLimited;- if (status >= 500) return error.ServerError;- if (status == 404 and std.mem.startsWith(u8, provider.id, "openai")) return error.OpenAiFlaky404;- if (status < 200 or status >= 300) return null;+ defer head.deinit(keep);++ // Dial, send and time-to-first-HEADERS race the head-stall watchdog, as+ // the HTTP/1.1 send + receiveHead do (issue #54): a half-open socket or a+ // server that accepts the stream and goes silent must not hang the turn.+ switch (try raceHead(self.io, &head, poll_stdin)) {+ .ok => {},+ .fallback => return null,+ }+ if (head.status == 429) return error.RateLimited;+ if (head.status >= 500) return error.ServerError;+ if (head.status == 404 and head.openai) return error.OpenAiFlaky404;+ // Esc pressed while connecting / waiting for headers? Stop before the body.+ if (poll_stdin and escPressed(true)) return error.Interrupted;+ const lines = &head.lines.?;+ // Any other status streams its body like the HTTP/1.1 path does: a 4xx+ // error envelope is what request()'s parser classifies.const sink = engine_sink.forAgent(self);var full: Io.Writer.Allocating = .init(gpa);errdefer full.deinit();@@ -77,25 +106,19 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {var got_body = false;var saw_done = false;- stream: while (true) {+ while (true) {const stall_budget = http_stall.interFrameBudgetMs(http.stream_stall_ms, got_body, self.partial_text.items.len != 0, self.stall.widen);- read: {- const ReadDone = union(enum) { line: anyerror!bool, stall: WatchdogFired };- var rd_buf: [2]ReadDone = undefined;- var rsel: Io.Select(ReadDone) = .init(self.io, &rd_buf);- rsel.concurrent(.line, lineTask, .{ &lines, &line, gpa }) catch {- if (saw_done) break;- self.stall.tripped_ms = stall_budget;- sink.emit(self.io, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = false } });- return error.StreamStalled;- };- rsel.concurrent(.stall, deadlineStallTask, .{ self.io, false, stall_budget }) catch {- const r = rsel.await() catch |e| {- rsel.cancelDiscard();- return e;- };- rsel.cancelDiscard();- const more = r.line catch |e| {+ const got = raceLine(self.io, lines, &line, gpa, poll_stdin, stall_budget) orelse {+ // #56 Fix-B: no spare concurrency for the read. Fail safe.+ if (saw_done) break;+ self.stall.tripped_ms = stall_budget;+ sink.emit(self.io, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = false } });+ return error.StreamStalled;+ };+ switch (got) {+ .line => |r| {+ const more = r catch |e| {+ if (e == error.Canceled) return e;if (saw_done) break;if (got_body) {sink.emit(self.io, .{ .stream_aborted = .dropped });@@ -104,38 +127,16 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {return e;};if (!more) {- keep = http2_pool.keepAfter(lines.ended);+ keep = lines.ended;break;}- break :read;- };- const first = rsel.await() catch |e| {- rsel.cancelDiscard();- return e;- };- rsel.cancelDiscard();- switch (first) {- .line => |r| {- const more = r catch |e| {- if (saw_done) break;- if (got_body) {- sink.emit(self.io, .{ .stream_aborted = .dropped });- return error.StreamDropped;- }- return e;- };- if (!more) {- keep = http2_pool.keepAfter(lines.ended);- break;- }- },- .stall => |w| {- if (w == .deadline and saw_done) break :stream;- if (w == .deadline) self.stall.tripped_ms = stall_budget;- if (w == .deadline) sink.emit(self.io, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = false } }) else sink.emit(self.io, .{ .stream_aborted = .interrupted });- return watchdogError(w, error.StreamStalled);- },- }+ },+ .stall => |w| {+ if (w == .deadline and saw_done) break;+ if (w == .deadline) self.stall.tripped_ms = stall_budget;+ if (w == .deadline) sink.emit(self.io, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = false } }) else sink.emit(self.io, .{ .stream_aborted = .interrupted });+ return watchdogError(w, error.StreamStalled);+ },}if (ssePayload(line.items)) |payload|try loop_guard.event(self, payload, true);@@ -150,24 +151,165 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {if (self.provider.kind == .openai and openaiComplete(line.items)) saw_done = true;if (isStreamEnd(self.scratchAlloc(), self.provider.kind, line.items)) {saw_done = true;- if (lines.ended) {- var junk: std.ArrayList(u8) = .empty;- defer junk.deinit(gpa);- while (lines.readLine(&junk) catch false) {}- keep = true;- }+ keep = settleAfterEnd(self.io, lines, gpa);break;}- if (self.sub and Agent.esc_cancel.load(.acquire)) {+ if ((poll_stdin and escPressed(true)) or (self.sub and Agent.esc_cancel.load(.acquire))) {sink.emit(self.io, .{ .stream_aborted = .interrupted });return error.Interrupted;}}+ // A connection the peer is draining (GOAWAY) takes no new streams.+ keep = keep and head.lease.?.session.reusable();sink.emit(self.io, .{ .stream_complete = .{ .streamed_text = self.streamed_text } });return try full.toOwnedSlice();}-fn lineTask(ls: *http_zig.conn.LineStream, dest: *std.ArrayList(u8), gpa: std.mem.Allocator) !bool {+/// Where the head task got to. Only failures before the request is complete+/// on the wire (or that the server proves it never processed) may fall back.+const Phase = enum { dial, send, wait };++/// State the head task fills and postStream owns. Select.cancelDiscard waits+/// for the task, so deinit always sees a settled lease/lines pair, whether the+/// task succeeded, failed or was cancelled by the watchdog.+const Head = struct {+ gpa: std.mem.Allocator,+ io: Io,+ origin: http2_pool.Origin,+ openai: bool,+ req: http_zig.Request,+ phase: Phase = .dial,+ lease: ?http2_pool.Lease = null,+ lines: ?http_zig.LineStream = null,+ status: u16 = 0,++ fn deinit(self: *Head, keep: bool) void {+ if (self.lines) |*l| l.deinit();+ if (self.lease) |l| l.release(keep);+ }+};++const HeadResult = enum { ok, fallback };++fn raceHead(io: Io, head: *Head, poll_stdin: bool) !HeadResult {+ const HeadDone = union(enum) { done: anyerror!void, stall: WatchdogFired };+ var hd_buf: [2]HeadDone = undefined;+ var hsel: Io.Select(HeadDone) = .init(io, &hd_buf);+ // #56 Fix-B: no slot for the task means no watchdog either. Nothing has+ // been sent, so fail retryable instead of blocking the turn.+ hsel.concurrent(.done, headTask, .{head}) catch return error.HungRequest;+ hsel.concurrent(.stall, headStallTask, .{ io, poll_stdin }) catch {};+ const first = hsel.await() catch |e| {+ hsel.cancelDiscard();+ return e;+ };+ hsel.cancelDiscard();+ switch (first) {+ .stall => |w| return watchdogError(w, error.HungRequest),+ .done => |r| {+ r catch |e| {+ if (e == error.Canceled) return e;+ if (head.lease) |l| if (l.session.h1_only) http2_pool.noteH1(io, head.origin.host, head.origin.port);+ if (mayResendOnH1(head.phase, e)) return .fallback;+ return e;+ };+ return .ok;+ },+ }+}++/// Falling back to HTTP/1.1 re-sends the POST, so it is allowed only when+/// the server cannot have acted on it: the body never fully left (dial or+/// send failed), or it said so -- GOAWAY above our stream id, REFUSED_STREAM.+fn mayResendOnH1(phase: Phase, err: anyerror) bool {+ if (err == error.Canceled) return false;+ if (phase != .wait) return true;+ return err == error.GoAway or err == error.StreamRefused;+}++test "HTTP/1.1 fallback only when the request provably never reached the server" {+ try std.testing.expect(mayResendOnH1(.dial, error.ConnectionRefused));+ try std.testing.expect(mayResendOnH1(.dial, error.H1NoStream));+ try std.testing.expect(mayResendOnH1(.send, error.WriteFailed));+ try std.testing.expect(mayResendOnH1(.wait, error.GoAway));+ try std.testing.expect(mayResendOnH1(.wait, error.StreamRefused));+ // Sent and possibly generating: retry via request(), never a silent re-POST.+ try std.testing.expect(!mayResendOnH1(.wait, error.EndOfStream));+ try std.testing.expect(!mayResendOnH1(.wait, error.RstStream));+ try std.testing.expect(!mayResendOnH1(.wait, error.ReadFailed));+ // A cancel is never turned into a replacement request.+ try std.testing.expect(!mayResendOnH1(.dial, error.Canceled));+ try std.testing.expect(!mayResendOnH1(.send, error.Canceled));+}++fn headTask(head: *Head) anyerror!void {+ head.phase = .dial;+ head.lease = try http2_pool.acquire(head.gpa, head.io, head.origin.host, head.origin.port);+ const sess = head.lease.?.session;+ if (sess.h1_only) return error.H1NoStream;+ head.phase = .send;+ head.lines = try sess.startLines(head.req);+ head.phase = .wait;+ const lines = &head.lines.?;+ head.status = try lines.waitStatus();+ // Error bodies are read here, still under the head watchdog: request()+ // classifies quota/overflow from the snippet and honours Retry-After.+ const throttled = head.status == 429 or head.status >= 500;+ if (throttled) main_mod.g_retry_after_ms = http.retryAfterMs(lines.header("retry-after"), lines.header("retry-after-ms"));+ if (throttled or (head.status == 404 and head.openai)) captureErrorBody(head.gpa, lines);+}++/// Same contract as http.capture5xxBodyStream: up to g_5xx_body_buf.len bytes+/// of the body, lines joined without their newlines; best effort.+fn captureErrorBody(gpa: std.mem.Allocator, lines: *http_zig.LineStream) void {+ main_mod.g_5xx_body_len = 0;+ var line: std.ArrayList(u8) = .empty;+ defer line.deinit(gpa);+ while (main_mod.g_5xx_body_len < main_mod.g_5xx_body_buf.len) {+ const more = lines.readLine(&line) catch break;+ if (!more) break;+ const n = @min(line.items.len, main_mod.g_5xx_body_buf.len - main_mod.g_5xx_body_len);+ @memcpy(main_mod.g_5xx_body_buf[main_mod.g_5xx_body_len..][0..n], line.items[0..n]);+ main_mod.g_5xx_body_len += n;+ }+}++const ReadDone = union(enum) { line: anyerror!bool, stall: WatchdogFired };++/// One readLine raced against the between-lines watchdog. Null when there is+/// no concurrency slot for the read at all.+fn raceLine(io: Io, lines: *http_zig.LineStream, line: *std.ArrayList(u8), gpa: std.mem.Allocator, poll_stdin: bool, budget_ms: u64) ?ReadDone {+ var rd_buf: [2]ReadDone = undefined;+ var rsel: Io.Select(ReadDone) = .init(io, &rd_buf);+ rsel.concurrent(.line, lineTask, .{ lines, line, gpa }) catch return null;+ rsel.concurrent(.stall, deadlineStallTask, .{ io, poll_stdin, budget_ms }) catch {};+ const first = rsel.await() catch |e| {+ rsel.cancelDiscard();+ return .{ .line = e };+ };+ rsel.cancelDiscard();+ return first;+}++/// The terminal event landed. Reuse the connection only if END_STREAM+/// follows promptly; a server that holds the stream open costs one dial.+fn settleAfterEnd(io: Io, lines: *http_zig.LineStream, gpa: std.mem.Allocator) bool {+ var junk: std.ArrayList(u8) = .empty;+ defer junk.deinit(gpa);+ while (true) {+ if (lines.ended) {+ while (lines.readLine(&junk) catch return false) {}+ return true;+ }+ const got = raceLine(io, lines, &junk, gpa, false, trailing_end_ms) orelse return false;+ switch (got) {+ .line => |r| if (!(r catch return false)) return lines.ended,+ .stall => return false,+ }+ }+}++fn lineTask(ls: *http_zig.LineStream, dest: *std.ArrayList(u8), gpa: std.mem.Allocator) !bool {_ = gpa;return ls.readLine(dest);}diff --git a/src/http2_pool.zig b/src/http2_pool.zigindex 0f53bfa1..e26bc034 100644--- a/src/http2_pool.zig+++ b/src/http2_pool.zig@@ -10,6 +10,35 @@ const main_mod = @import("main.zig");var mu: Io.Mutex = .init;var session: ?*http_zig.Session = null;+/// Origins that answered without h2 (ALPN picked http/1.1 or none). Skipping+/// them saves a throwaway TCP + TLS dial on every request to that origin.+const H1Origin = struct { host: [255]u8 = undefined, len: u8 = 0, port: u16 = 0 };+var h1_origins: [8]H1Origin = @splat(.{});+var h1_next: usize = 0;++fn isOrigin(o: *const H1Origin, host: []const u8, port: u16) bool {+ return o.len != 0 and o.port == port and std.ascii.eqlIgnoreCase(o.host[0..o.len], host);+}++pub fn knownH1(io: Io, host: []const u8, port: u16) bool {+ mu.lockUncancelable(io);+ defer mu.unlock(io);+ for (&h1_origins) |*o| if (isOrigin(o, host, port)) return true;+ return false;+}++pub fn noteH1(io: Io, host: []const u8, port: u16) void {+ if (host.len == 0 or host.len > 255) return;+ mu.lockUncancelable(io);+ defer mu.unlock(io);+ for (&h1_origins) |*o| if (isOrigin(o, host, port)) return;+ const slot = &h1_origins[h1_next % h1_origins.len];+ h1_next += 1;+ @memcpy(slot.host[0..host.len], host);+ slot.len = @intCast(host.len);+ slot.port = port;+}+pub fn enabled() bool {return main_mod.g_http2;}@@ -64,14 +93,25 @@ pub fn acquire(gpa: std.mem.Allocator, io: Io, host: []const u8, p: u16) !Leasereturn .{ .session = takeIdle(io, host, p) orelse try http_zig.Session.open(gpa, io, host, p) };}+/// Close the idle session. Called when the HTTP runtime shuts down so the+/// kept-alive connection (socket, TLS buffers, CA bundle, HPACK table) is not+/// left for the leak checker at exit.+pub fn shutdown(io: Io) void {+ mu.lockUncancelable(io);+ const idle = session;+ session = null;+ mu.unlock(io);+ if (idle) |s| s.close();+}+/// Tests release every active lease before clearing the idle pool.pub fn resetForTest() void {if (!builtin.is_test) return;+ shutdown(std.testing.io);mu.lockUncancelable(std.testing.io);- const idle = session;- session = null;+ h1_origins = @splat(.{});+ h1_next = 0;mu.unlock(std.testing.io);- if (idle) |s| s.close();}pub fn keepAfter(ended: bool) bool {@@ -119,6 +159,20 @@ fn countWindowUpdates(bytes: []const u8) usize {return n;}+test "an origin that answered without h2 is remembered, case-insensitively and per port" {+ defer resetForTest();+ const io = std.testing.io;+ try std.testing.expect(!knownH1(io, "old.example", 443));+ noteH1(io, "old.example", 443);+ noteH1(io, "old.example", 443);+ try std.testing.expect(knownH1(io, "OLD.example", 443));+ try std.testing.expect(!knownH1(io, "old.example", 8443));+ // The table is bounded: the oldest origin is evicted, never overflowed.+ var buf: [16]u8 = undefined;+ for (0..h1_origins.len) |i| noteH1(io, try std.fmt.bufPrint(&buf, "h{d}.example", .{i}), 443);+ try std.testing.expect(!knownH1(io, "old.example", 443));+}+test "want is https-only" {const saved = main_mod.g_http2;defer main_mod.g_http2 = saved;diff --git a/src/http_client.zig b/src/http_client.zigindex 490dcc1c..654ac350 100644--- a/src/http_client.zig+++ b/src/http_client.zig@@ -393,6 +393,7 @@ pub const Runtime = struct {self.recovery.shutdown();_ = self.warm_future.await(await_io);+ @import("http2_pool.zig").shutdown(self.io);g_lifecycle_mutex.lockUncancelable(self.io);g_client_ready = null;