graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Keep pooled HTTPS transport outside request arenas
Rach Pradhan2e2616ed59c2
diff --git a/scripts/eval/jev-http2-stall.cjs b/scripts/eval/jev-http2-stall.cjsindex d16bd4bb..33e490de 100644--- a/scripts/eval/jev-http2-stall.cjs+++ b/scripts/eval/jev-http2-stall.cjs@@ -4,12 +4,24 @@ const http2 = require('node:http2');const [key, cert, log] = process.argv.slice(2);const server = http2.createSecureServer({ key: fs.readFileSync(key), cert: fs.readFileSync(cert), allowHTTP1: true });function record(row) { fs.appendFileSync(log, JSON.stringify(row) + '\n'); }-server.on('session', session => record({ event: 'session', alpn: session.socket.alpnProtocol }));+const sessions = new WeakMap(); let nextSession = 1;+server.on('session', session => {+ const id = nextSession++;+ sessions.set(session, id);+ record({ event: 'session', id, alpn: session.socket.alpnProtocol });+});server.on('stream', (stream, headers) => {let body = '';+ stream.on('error', () => {}); // expected for the deliberate reset pathstream.on('data', chunk => { body += chunk.toString(); });stream.on('end', () => {- record({ event: 'request', path: headers[':path'], method: headers[':method'], auth: headers.authorization, body });+ record({ event: 'request', path: headers[':path'], method: headers[':method'], auth: headers.authorization, body,+ session: sessions.get(stream.session), stream: stream.id });+ if (headers[':path'] === '/ok') {+ stream.respond({ ':status': 200, 'content-type': 'application/json' });+ stream.end('{"ok":true}');+ return;+ }if (headers[':path'] === '/drop') {stream.close(http2.constants.NGHTTP2_INTERNAL_ERROR);record({ event: 'dropped' });diff --git a/scripts/test-jev-http2.py b/scripts/test-jev-http2.pyindex d9cfb042..56359ea7 100644--- a/scripts/test-jev-http2.py+++ b/scripts/test-jev-http2.py@@ -30,12 +30,18 @@ def exercise(root):port = int(ports.get(timeout=10))env = dict(__import__('os').environ,GRAFF_JEV_HTTP2_STALL_URL=f'https://localhost:{port}/v1/systemone',- GRAFF_JEV_HTTP2_DROP_URL=f'https://localhost:{port}/drop')+ GRAFF_JEV_HTTP2_DROP_URL=f'https://localhost:{port}/drop',+ GRAFF_JEV_HTTP2_REUSE_URL=f'https://localhost:{port}/ok')start = time.monotonic()- result = fixture.run(['zig', 'build', 'test', '--system', str(root/'packages'),- '--cache-dir', str(root/'test-cache'),- '-Dtest-filter=loopback HTTP', '--summary', 'all'],- cwd=REPO, env=env, timeout=240)+ try:+ result = fixture.run(['zig', 'build', 'test', '--system', str(root/'packages'),+ '--cache-dir', str(root/'test-cache'),+ '-Dtest-filter=loopback HTTP', '--summary', 'all'],+ cwd=REPO, env=env, timeout=240)+ except RuntimeError:+ print('server log:', log.read_text(), flush=True)+ print('server stderr:', (root/'server-stderr').read_text(), flush=True)+ raiseelapsed = time.monotonic() - startassert '1 skipped' not in result.stderr, result.stderrdeadline = time.monotonic() + 5@@ -46,13 +52,16 @@ def exercise(root):time.sleep(.02)assert any(row['event'] == 'session' and row['alpn'] == 'h2' for row in rows), rowsrequests = [row for row in rows if row['event'] == 'request']- assert len(requests) == 2, rows- assert {row['path'] for row in requests} == {'/v1/systemone', '/drop'}, requests+ assert len(requests) == 4, rows+ assert {row['path'] for row in requests} == {'/v1/systemone', '/drop', '/ok'}, requestsassert all(row['method'] == 'POST' and row['auth'] == 'Bearer fixture' and row['body'] == '{}' for row in requests), requests+ reused = [row for row in requests if row['path'] == '/ok']+ assert len(reused) == 2 and reused[0]['session'] == reused[1]['session'], reused+ assert [row['stream'] for row in reused] == [1, 3], reusedassert len([row for row in rows if row['event'] == 'dropped']) == 1, rowsassert any(row['event'] == 'stalled' for row in rows), rowsassert any(row['event'] == 'closed' for row in rows), rows- print(f'PASS Jev HTTPS: stalled h2 canceled/closed, ambiguous drop had one POST; test command {elapsed:.1f}s', flush=True)+ print(f'PASS Jev HTTPS: cancel/close, one ambiguous POST, pooled reuse after arena teardown; test command {elapsed:.1f}s', flush=True)finally:fixture.terminate(server)diff --git a/src/http2_buffered.zig b/src/http2_buffered.zigindex 36f6ec93..8c53f7c9 100644--- a/src/http2_buffered.zig+++ b/src/http2_buffered.zig@@ -149,10 +149,13 @@ fn watched(ex: *Exchange, deadline_ms: u64) !void {}}-pub fn post(gpa: Allocator, io: Io, client: *std.http.Client, url: []const u8, bearer: []const u8, body: []const u8, deadline_ms: u64) !Response {- var ex: Exchange = .{ .gpa = gpa, .io = io, .client = client, .url = url, .bearer = bearer, .body = body, .limit = max_body };+/// `transport_gpa` must outlive pooled sessions (until pool.shutdown).+/// `result_allocator` may be a request-scoped arena; no pooled allocation+/// or live I/O retains its output after this function returns.+pub fn post(transport_gpa: Allocator, result_allocator: Allocator, io: Io, client: *std.http.Client, url: []const u8, bearer: []const u8, body: []const u8, deadline_ms: u64) !Response {+ var ex: Exchange = .{ .gpa = transport_gpa, .io = io, .client = client, .url = url, .bearer = bearer, .body = body, .limit = max_body };try watched(&ex, deadline_ms);- return .{ .status = ex.status, .body = try gpa.dupe(u8, ex.bytes[0..ex.len]) };+ return .{ .status = ex.status, .body = try result_allocator.dupe(u8, ex.bytes[0..ex.len]) };}test "buffered response rejects growth past limit before copying" {@@ -277,7 +280,7 @@ test "loopback HTTPS stalled body cancels and releases the exchange" {var client: std.http.Client = .{ .allocator = std.testing.allocator, .io = io };defer client.deinit();const started = Io.Timestamp.now(io, .awake).nanoseconds;- try std.testing.expectError(error.DeadlineExceeded, post(std.testing.allocator, io, &client, url, "Bearer fixture", "{}", 100));+ try std.testing.expectError(error.DeadlineExceeded, post(std.testing.allocator, std.testing.allocator, io, &client, url, "Bearer fixture", "{}", 100));const elapsed = Io.Timestamp.now(io, .awake).nanoseconds - started;try std.testing.expect(elapsed < 2 * std.time.ns_per_s);}@@ -291,10 +294,31 @@ test "loopback HTTP2 ambiguous post-send drop is never replayed" {defer pool.shutdown(io);var client: std.http.Client = .{ .allocator = std.testing.allocator, .io = io };defer client.deinit();- const response = post(std.testing.allocator, io, &client, std.mem.span(value), "Bearer fixture", "{}", 1000) catch |err| {+ const response = post(std.testing.allocator, std.testing.allocator, io, &client, std.mem.span(value), "Bearer fixture", "{}", 1000) catch |err| {try std.testing.expect(err == error.RstStream or err == error.ReadFailed or err == error.EndOfStream);return;};std.testing.allocator.free(response.body);return error.ExpectedAmbiguousFailure;}++test "loopback HTTP2 reuses a pooled session after result arena destruction" {+ const value = std.c.getenv("GRAFF_JEV_HTTP2_REUSE_URL") orelse return error.SkipZigTest;+ const io = std.testing.io;+ const saved = @import("main.zig").g_http2;+ defer @import("main.zig").g_http2 = saved;+ @import("main.zig").g_http2 = true;+ defer pool.shutdown(io);+ var client: std.http.Client = .{ .allocator = std.testing.allocator, .io = io };+ defer client.deinit();+ for (0..2) |_| {+ var temp = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer temp.deinit(); // response, bearer and body die before pool reuse+ const a = temp.allocator();+ const bearer = try a.dupe(u8, "Bearer fixture");+ const body = try a.dupe(u8, "{}");+ const response = try post(std.testing.allocator, a, io, &client, std.mem.span(value), bearer, body, 1000);+ try std.testing.expectEqual(@as(u16, 200), response.status);+ try std.testing.expectEqualStrings("{\"ok\":true}", response.body);+ }+}diff --git a/src/jev_tool.zig b/src/jev_tool.zigindex 3351fcfa..0abd0e79 100644--- a/src/jev_tool.zig+++ b/src/jev_tool.zig@@ -205,7 +205,7 @@ fn fetch(ctx: ToolCtx, arena: Allocator, body: []const u8) ![]const u8 {const bearer = try gatewayBearer(arena, state.key);var completed = false;defer if (!completed) pricing.g_cost.failedWithoutUsage(ctx.io, 1);- const res = try buffered_https.post(arena, ctx.io, ctx.client, endpoint, bearer, body, 10_000);+ const res = try buffered_https.post(ctx.gpa, arena, ctx.io, ctx.client, endpoint, bearer, body, 10_000);switch (res.status) {200 => {},401, 403 => return error.JevUnauthorized,