Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Loopback test servers answer only the agent's own requests

justrach84ce8deed1b3
diff --git a/src/agent_stream_stall_test.zig b/src/agent_stream_stall_test.zig
index 0e782cf6..5f74b7d8 100644
--- a/src/agent_stream_stall_test.zig
+++ b/src/agent_stream_stall_test.zig
@@ -32,19 +32,15 @@ const Srv = struct {
/// Drain the request head, send the SSE head plus one prose delta (plus
/// the terminal event when `finish`). The socket then stays open with no
/// further byte, so a client that gives up sees silence (a stall), never
- /// EOF (a drop).
- fn answer(io: Io, c: std.Io.net.Stream, finish: bool) void {
- var rbuf: [8192]u8 = undefined;
- var sr = std.Io.net.Stream.Reader.init(c, io, &rbuf);
- while (true) {
- const l = (sr.interface.takeDelimiter('\n') catch return) orelse return;
- if (l.len == 0 or (l.len == 1 and l[0] == '\r')) break; // end of headers
- }
+ /// EOF (a drop). False, with nothing sent, for anything but a POST.
+ fn answer(io: Io, c: std.Io.net.Stream, finish: bool) bool {
+ if (!takePostHead(io, c)) return false;
var wbuf: [1024]u8 = undefined;
var sw = std.Io.net.Stream.Writer.init(c, io, &wbuf);
- sw.interface.writeAll("HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\nconnection: close\r\n\r\n" ++ prose_line) catch return;
- if (finish) sw.interface.writeAll(done_line) catch return;
- sw.interface.flush() catch return;
+ sw.interface.writeAll("HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\nconnection: close\r\n\r\n" ++ prose_line) catch return true;
+ if (finish) sw.interface.writeAll(done_line) catch return true;
+ sw.interface.flush() catch return true;
+ return true;
}
/// `conns` streams that go quiet, in turn (the #680 ladder). Every
@@ -53,10 +49,14 @@ const Srv = struct {
var held: [3]?std.Io.net.Stream = @splat(null);
defer for (held) |h| if (h) |c| c.close(io);
var i: usize = 0;
- while (i < conns) : (i += 1) {
+ while (i < conns) {
const c = server.accept(io) catch return;
+ if (!answer(io, c, false)) {
+ c.close(io);
+ continue;
+ }
held[i] = c;
- answer(io, c, false);
+ i += 1;
}
while (!done.load(.acquire)) io.sleep(.fromMilliseconds(20), .awake) catch return;
}
@@ -65,15 +65,35 @@ const Srv = struct {
fn runTwo(io: Io, server: *std.Io.net.Server, done: *std.atomic.Value(bool)) void {
var held: [2]?std.Io.net.Stream = @splat(null);
defer for (held) |h| if (h) |c| c.close(io);
- for (0..2) |i| {
+ var i: usize = 0;
+ while (i < 2) {
const c = server.accept(io) catch return;
+ if (!answer(io, c, i == 0)) {
+ c.close(io);
+ continue;
+ }
held[i] = c;
- answer(io, c, i == 0);
+ i += 1;
}
while (!done.load(.acquire)) io.sleep(.fromMilliseconds(20), .awake) catch return;
}
};
+/// Drain one request head; true when it is the agent's POST. Another local
+/// process can probe any fresh loopback listener (#1303: `HEAD /` for
+/// `localhost`), and serving it would spend a connection the test counts on.
+fn takePostHead(io: Io, c: std.Io.net.Stream) bool {
+ var rbuf: [8192]u8 = undefined;
+ var sr = std.Io.net.Stream.Reader.init(c, io, &rbuf);
+ var post: ?bool = null;
+ while (true) {
+ const l = (sr.interface.takeDelimiter('\n') catch return false) orelse return false;
+ if (post == null) post = std.mem.startsWith(u8, l, "POST ");
+ if (l.len == 0 or (l.len == 1 and l[0] == '\r')) break; // end of headers
+ }
+ return post orelse false;
+}
+
fn testProvider(url: []const u8) @import("provider.zig").Provider {
return .{ .id = "test", .kind = .openai, .auth = .bearer, .url = url, .api_key = "k", .model = "m", .context = 0 };
}
@@ -216,14 +236,12 @@ const TrailerSrv = struct {
const finish_line = "data: {\"choices\": [{\"index\":0, \"delta\": {}, \"finish_reason\": \"stop\"}]}\n\n";
const usage_line = "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":20,\"completion_tokens\":2,\"total_tokens\":22}}\n\n";
fn run(io: Io, server: *std.Io.net.Server, mode: Mode, done: *std.atomic.Value(bool)) void {
- const c = server.accept(io) catch return;
+ const c = while (true) {
+ const conn = server.accept(io) catch return;
+ if (takePostHead(io, conn)) break conn;
+ conn.close(io);
+ };
defer c.close(io);
- var rbuf: [8192]u8 = undefined;
- var sr = std.Io.net.Stream.Reader.init(c, io, &rbuf);
- while (true) {
- const line = (sr.interface.takeDelimiter('\n') catch return) orelse return;
- if (line.len == 0 or (line.len == 1 and line[0] == '\r')) break;
- }
var wbuf: [1024]u8 = undefined;
var sw = std.Io.net.Stream.Writer.init(c, io, &wbuf);
sw.interface.writeAll("HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\nconnection: close\r\n\r\n") catch return;
diff --git a/src/http_client_integration_tests.zig b/src/http_client_integration_tests.zig
index 8d46098b..aff58f88 100644
--- a/src/http_client_integration_tests.zig
+++ b/src/http_client_integration_tests.zig
@@ -31,39 +31,51 @@ const chat_sse_body =
"data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" ++
"data: [DONE]\n\n";
-fn readRequest(reader: *std.Io.net.Stream.Reader) !void {
+/// Reads one request head and body; true when it is a POST.
+fn readRequest(reader: *std.Io.net.Stream.Reader) !bool {
var content_length: usize = 0;
+ var post: ?bool = null;
while (true) {
const line = (try reader.interface.takeDelimiter('\n')) orelse return error.EndOfStream;
+ if (post == null) post = std.mem.startsWith(u8, line, "POST ");
if (line.len == 0 or (line.len == 1 and line[0] == '\r')) break;
if (std.ascii.startsWithIgnoreCase(line, "content-length:")) {
content_length = try std.fmt.parseInt(usize, std.mem.trim(u8, line[15..], " \t\r"), 10);
}
}
try reader.interface.discardAll(content_length);
+ return post orelse false;
}
pub fn serveReplies(io: Io, server: *std.Io.net.Server, replies: []const Reply, accepted: *std.atomic.Value(usize)) void {
- for (replies) |reply| {
+ var served: usize = 0;
+ while (served < replies.len) {
const conn = server.accept(io) catch return;
- {
- defer conn.close(io);
+ defer conn.close(io);
+ var read_buf: [16 * 1024]u8 = undefined;
+ var reader = std.Io.net.Stream.Reader.init(conn, io, &read_buf);
+ const post = readRequest(&reader) catch {
_ = accepted.fetchAdd(1, .acq_rel);
- var read_buf: [16 * 1024]u8 = undefined;
- var reader = std.Io.net.Stream.Reader.init(conn, io, &read_buf);
- readRequest(&reader) catch return;
- var head_buf: [256]u8 = undefined;
- const head = std.fmt.bufPrint(
- &head_buf,
- "HTTP/1.1 {s}\r\ncontent-type: {s}\r\ncontent-length: {d}\r\nconnection: close\r\n\r\n",
- .{ reply.status, reply.content_type, reply.body.len },
- ) catch return;
- var write_buf: [4096]u8 = undefined;
- var writer = std.Io.net.Stream.Writer.init(conn, io, &write_buf);
- writer.interface.writeAll(head) catch return;
- writer.interface.writeAll(reply.body) catch return;
- writer.interface.flush() catch return;
- }
+ return;
+ };
+ // #1303: every agent request is a POST. Another local process can
+ // probe any fresh loopback listener (seen: `HEAD /` for `localhost`
+ // with no User-Agent); counting that broke "no request was made".
+ if (!post) continue;
+ _ = accepted.fetchAdd(1, .acq_rel);
+ const reply = replies[served];
+ served += 1;
+ var head_buf: [256]u8 = undefined;
+ const head = std.fmt.bufPrint(
+ &head_buf,
+ "HTTP/1.1 {s}\r\ncontent-type: {s}\r\ncontent-length: {d}\r\nconnection: close\r\n\r\n",
+ .{ reply.status, reply.content_type, reply.body.len },
+ ) catch return;
+ var write_buf: [4096]u8 = undefined;
+ var writer = std.Io.net.Stream.Writer.init(conn, io, &write_buf);
+ writer.interface.writeAll(head) catch return;
+ writer.interface.writeAll(reply.body) catch return;
+ writer.interface.flush() catch return;
}
}
diff --git a/src/router_catalog_tests.zig b/src/router_catalog_tests.zig
index 163e6113..cd5c8bf9 100644
--- a/src/router_catalog_tests.zig
+++ b/src/router_catalog_tests.zig
@@ -366,14 +366,18 @@ test "catalog HTTP/1.1 keeps auth, cursor pagination, and a valid prefix after a
fn serveCatalogPages(io: std.Io, server: *std.Io.net.Server) void {
const first = "{\"data\":[{\"id\":\"first\"}],\"has_more\":true,\"last_id\":\"first\"}";
const second = "{\"data\":[{\"id\":\"second\"}],\"has_more\":false}";
- for (0..6) |i| {
+ var i: usize = 0;
+ while (i < 6) {
const stream = server.accept(io) catch return;
defer stream.close(io);
var read_buffer: [4096]u8 = undefined;
var reader = std.Io.net.Stream.Reader.init(stream, io, &read_buffer);
- const line = reader.interface.takeDelimiter('\n') catch return;
+ const line = (reader.interface.takeDelimiter('\n') catch return) orelse return;
+ // #1303: another local process can probe a fresh loopback listener;
+ // only the catalog's own GETs are pages.
+ if (!std.mem.startsWith(u8, line, "GET /v1/models")) continue;
const expected = if (i % 2 == 0) "GET /v1/models?limit=1000 HTTP/1.1\r" else "GET /v1/models?limit=1000&after_id=first HTTP/1.1\r";
- if (!std.mem.eql(u8, line orelse return, expected)) return;
+ if (!std.mem.eql(u8, line, expected)) return;
var key = false;
var version = false;
while (reader.interface.takeDelimiter('\n') catch null) |header| {
@@ -387,6 +391,7 @@ fn serveCatalogPages(io: std.Io, server: *std.Io.net.Server) void {
var writer = std.Io.net.Stream.Writer.init(stream, io, &write_buffer);
writer.interface.print("HTTP/1.1 {s}\r\nContent-Length: {d}\r\nConnection: close\r\n\r\n{s}", .{ if (i == 3) "500 Error" else "200 OK", body.len, body }) catch return;
writer.interface.flush() catch return;
+ i += 1;
}
}