graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Bound native judgment transport over HTTP/2
Rach Pradhandf1806c7b252
diff --git a/build.zig.zon b/build.zig.zonindex 8f91de04..5ee33cdd 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#459e8eb2a6227cce937549a6d656ca5049d4fe63",- .hash = "http_zig-0.1.0-2CB7ExwFAwBn4Hk9aRpSLLkhMyIAruz65-IpVlCmHtn8",+ .url = "git+https://github.com/justrach/http-zig#b875fed4741b947e731b86f430f532895f13c1de",+ .hash = "http_zig-0.1.0-2CB7E-ERAwBIvemDRdHHwUIk68jZJY0rlfQgNoi8rnqX",},},.paths = .{""},diff --git a/scripts/eval/jev-http2-stall.cjs b/scripts/eval/jev-http2-stall.cjsnew file mode 100644index 00000000..d16bd4bb--- /dev/null+++ b/scripts/eval/jev-http2-stall.cjs@@ -0,0 +1,24 @@+// Local TLS fixture: acknowledge one POST, then hold its JSON body open.+const fs = require('node:fs');+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 }));+server.on('stream', (stream, headers) => {+ let body = '';+ stream.on('data', chunk => { body += chunk.toString(); });+ stream.on('end', () => {+ record({ event: 'request', path: headers[':path'], method: headers[':method'], auth: headers.authorization, body });+ if (headers[':path'] === '/drop') {+ stream.close(http2.constants.NGHTTP2_INTERNAL_ERROR);+ record({ event: 'dropped' });+ return;+ }+ stream.respond({ ':status': 200, 'content-type': 'application/json' });+ stream.write('{"answers":');+ record({ event: 'stalled' });+ });+ stream.on('close', () => record({ event: 'closed' }));+});+server.listen(0, () => process.stdout.write(String(server.address().port) + '\n'));diff --git a/scripts/test-jev-http2.py b/scripts/test-jev-http2.pynew file mode 100644index 00000000..d9cfb042--- /dev/null+++ b/scripts/test-jev-http2.py@@ -0,0 +1,62 @@+#!/usr/bin/env python3+"""Offline loopback check of the bounded HTTPS cancellation path."""+import importlib.util+import json+from pathlib import Path+import queue+import subprocess+import tempfile+import threading+import time++REPO = Path(__file__).resolve().parents[1]+spec = importlib.util.spec_from_file_location('h2_fixture', REPO/'scripts/test-acp-http2.py')+fixture = importlib.util.module_from_spec(spec)+spec.loader.exec_module(fixture)+++def exercise(root):+ fixture.certificates(root)+ fixture.build(root) # verifies the exact pinned package and injects only the local CA+ log = root/'server.jsonl'+ log.touch()+ with (root/'server-stderr').open('w') as stderr:+ server = subprocess.Popen(['node', str(REPO/'scripts/eval/jev-http2-stall.cjs'),+ str(root/'server.key'), str(root/'server.pem'), str(log)],+ stdout=subprocess.PIPE, stderr=stderr, text=True, start_new_session=True)+ try:+ ports = queue.Queue()+ threading.Thread(target=lambda: ports.put(server.stdout.readline()), daemon=True).start()+ 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')+ 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)+ elapsed = time.monotonic() - start+ assert '1 skipped' not in result.stderr, result.stderr+ deadline = time.monotonic() + 5+ while time.monotonic() < deadline:+ rows = [json.loads(line) for line in log.read_text().splitlines()]+ if any(row['event'] == 'closed' for row in rows):+ break+ time.sleep(.02)+ assert any(row['event'] == 'session' and row['alpn'] == 'h2' for row in rows), rows+ requests = [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 all(row['method'] == 'POST' and row['auth'] == 'Bearer fixture' and row['body'] == '{}' for row in requests), requests+ assert len([row for row in rows if row['event'] == 'dropped']) == 1, rows+ assert any(row['event'] == 'stalled' for row in rows), rows+ assert 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)+ finally:+ fixture.terminate(server)+++if __name__ == '__main__':+ with tempfile.TemporaryDirectory(prefix='jev-http2-') as name:+ exercise(Path(name))diff --git a/src/http2_buffered.zig b/src/http2_buffered.zignew file mode 100644index 00000000..36f6ec93--- /dev/null+++ b/src/http2_buffered.zig@@ -0,0 +1,300 @@+//! Bounded HTTPS JSON POST. HTTP/2 streams hold an exclusive pool lease;+//! HTTP/1.1 is used only before an HTTP/2 request was sent (dial/ALPN).+const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;+const h2 = @import("http_zig");+const pool = @import("http2_pool.zig");++pub const max_body = 64 * 1024;+pub const Response = struct { status: u16, body: []u8 };++fn authority(url: []const u8) []const u8 {+ const rest = url["https://".len..]; // caller already passed parseHttpsUrl+ return rest[0 .. std.mem.indexOfScalar(u8, rest, '/') orelse rest.len];+}++const Exchange = struct {+ gpa: Allocator,+ io: Io,+ client: *std.http.Client,+ url: []const u8,+ bearer: []const u8,+ body: []const u8,+ limit: usize,+ status: u16 = 0,+ len: usize = 0,+ bytes: [max_body]u8 = undefined,+ test_task: ?*const fn (*Exchange) anyerror!void = null,++ fn append(self: *Exchange, chunk: []const u8) !void {+ if (chunk.len > self.limit - self.len) return error.ResponseTooLarge;+ @memcpy(self.bytes[self.len..][0..chunk.len], chunk);+ self.len += chunk.len;+ }++ fn run(self: *Exchange) !void {+ if (self.test_task) |task| return task(self);+ if (pool.want(self.url)) {+ const origin = try pool.parseOrigin(self.url);+ if (!pool.knownH1(self.io, origin.host, origin.port)) {+ if (try self.tryH2(origin)) return;+ }+ }+ try self.runH1();+ }++ /// Returns false only before startLines has been called. Errors after it+ /// may follow a complete upload and must never silently resend on H1.+ fn tryH2(self: *Exchange, origin: pool.Origin) !bool {+ const lease = pool.acquire(self.gpa, self.io, origin.host, origin.port) catch |err| {+ if (h2.session.handshakeFallback(err)) return false; // pre-send TLS failure+ return err;+ };+ var keep = false;+ defer lease.release(keep);+ if (lease.session.h1_only) {+ pool.noteH1(self.io, origin.host, origin.port);+ return false; // ALPN did not select h2+ }+ const headers = [_]h2.Header{+ .{ .name = "content-type", .value = "application/json" },+ .{ .name = "accept", .value = "application/json" },+ .{ .name = "authorization", .value = self.bearer },+ };+ var stream = try lease.session.startLines(.{+ .method = "POST",+ .scheme = "https",+ .authority = authority(self.url),+ .path = origin.path,+ .extra = &headers,+ .body = self.body,+ });+ defer stream.deinit();+ try self.consume(&stream);+ keep = stream.ended and lease.session.reusable();+ return true;+ }++ fn consume(self: *Exchange, stream: *h2.LineStream) !void {+ self.status = try stream.waitStatus();+ if (self.status != 200) return; // no error body crosses this boundary+ var chunk: [4096]u8 = undefined;+ while (true) {+ const n = try stream.readChunk(&chunk);+ if (n == 0) break;+ try self.append(chunk[0..n]);+ }+ }++ fn runH1(self: *Exchange) !void {+ var req = try self.client.request(.POST, try std.Uri.parse(self.url), .{+ .redirect_behavior = .unhandled,+ .headers = .{+ .content_type = .{ .override = "application/json" },+ .authorization = .{ .override = self.bearer },+ },+ });+ defer req.deinit();+ errdefer {+ if (req.connection) |conn| conn.closing = true;+ }+ var response: std.http.Client.Response = undefined;+ try @import("http.zig").sendHeadTask(&req, self.body, &response);+ self.status = @intFromEnum(response.head.status);+ if (self.status != 200) {+ // Error bodies may contain private details. Close without reading.+ if (req.connection) |conn| conn.closing = true;+ return;+ }+ const dbuf: []u8 = switch (response.head.content_encoding) {+ .identity => &.{},+ .zstd => try self.gpa.alloc(u8, std.compress.zstd.default_window_len),+ .deflate, .gzip => try self.gpa.alloc(u8, std.compress.flate.max_window_len),+ .compress => return error.UnsupportedCompressionMethod,+ };+ defer if (dbuf.len != 0) self.gpa.free(dbuf);+ var tbuf: [64]u8 = undefined;+ var dec: std.http.Decompress = undefined;+ const reader = response.readerDecompressing(&tbuf, &dec, dbuf);+ var writer = Io.Writer.fixed(self.bytes[0..self.limit]);+ _ = try reader.streamRemaining(&writer);+ self.len = writer.buffered().len;+ }+};++fn deadline(io: Io, ms: u64) void {+ io.sleep(.fromMilliseconds(@intCast(@min(ms, std.math.maxInt(i64)))), .awake) catch {};+}++/// Both select arms are cancelled and joined before this returns, so the+/// exchange's credential and buffers cannot outlive their owner.+fn watched(ex: *Exchange, deadline_ms: u64) !void {+ const Done = union(enum) { task: anyerror!void, timeout: void };+ var done: [2]Done = undefined;+ var select: Io.Select(Done) = .init(ex.io, &done);+ select.concurrent(.timeout, deadline, .{ ex.io, deadline_ms }) catch return error.DeadlineUnavailable;+ select.concurrent(.task, Exchange.run, .{ex}) catch {+ select.cancelDiscard();+ return error.TransportUnavailable;+ };+ const first = select.await() catch |err| {+ select.cancelDiscard();+ return err;+ };+ select.cancelDiscard();+ switch (first) {+ .task => |result| try result,+ .timeout => return error.DeadlineExceeded,+ }+}++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 };+ try watched(&ex, deadline_ms);+ return .{ .status = ex.status, .body = try gpa.dupe(u8, ex.bytes[0..ex.len]) };+}++test "buffered response rejects growth past limit before copying" {+ var client: std.http.Client = undefined;+ var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = 4 };+ try ex.append("abcd");+ try std.testing.expectError(error.ResponseTooLarge, ex.append("e"));+ try std.testing.expectEqualStrings("abcd", ex.bytes[0..ex.len]);+}++test "HTTP2 authority retains nondefault port" {+ try std.testing.expectEqualStrings("localhost:8443", authority("https://localhost:8443/v1/systemone"));+ try std.testing.expectEqualStrings("gateway.example", authority("https://gateway.example/v1/systemone"));+}++test "buffered exchange deadline cancels and joins its worker" {+ const Fake = struct {+ fn run(ex: *Exchange) anyerror!void {+ ex.io.sleep(.fromSeconds(10), .awake) catch return;+ ex.status = 200;+ }+ };+ var client: std.http.Client = undefined;+ var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = max_body, .test_task = Fake.run };+ try std.testing.expectError(error.DeadlineExceeded, watched(&ex, 1));+ try std.testing.expectEqual(@as(u16, 0), ex.status);+}++test "ambiguous send failure returns once without replay" {+ const Fake = struct {+ var calls: usize = 0;+ fn run(_: *Exchange) anyerror!void {+ calls += 1;+ return error.WriteFailed;+ }+ };+ Fake.calls = 0;+ var client: std.http.Client = undefined;+ var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = max_body, .test_task = Fake.run };+ try std.testing.expectError(error.WriteFailed, watched(&ex, 100));+ try std.testing.expectEqual(@as(usize, 1), Fake.calls);+}++test "buffered HTTP2 JSON succeeds across frames without newlines" {+ const frame = h2.frame;+ var srv: Io.Writer.Allocating = .init(std.testing.allocator);+ defer srv.deinit();+ try frame.write(&srv.writer, .{ .typ = .settings, .flags = 0, .stream_id = 0, .payload = &.{} });+ const ok = [_]u8{0x88};+ try frame.write(&srv.writer, .{ .typ = .headers, .flags = frame.flags.end_headers, .stream_id = 1, .payload = &ok });+ try frame.write(&srv.writer, .{ .typ = .data, .flags = 0, .stream_id = 1, .payload = "{\"ok\":" });+ try frame.write(&srv.writer, .{ .typ = .data, .flags = frame.flags.end_stream, .stream_id = 1, .payload = "true}" });+ var reader: Io.Reader = .fixed(srv.written());+ var writer: Io.Writer.Allocating = .init(std.testing.allocator);+ defer writer.deinit();+ var conn = h2.Conn.init(std.testing.allocator, &reader, &writer.writer);+ defer conn.deinit();+ var stream = try conn.startLines(.{ .method = "GET", .scheme = "https", .authority = "localhost", .path = "/" });+ defer stream.deinit();+ var client: std.http.Client = undefined;+ var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = max_body };+ try ex.consume(&stream);+ try std.testing.expectEqual(@as(u16, 200), ex.status);+ try std.testing.expectEqualStrings("{\"ok\":true}", ex.bytes[0..ex.len]);+ try std.testing.expect(stream.ended);+}++test "buffered HTTP2 rejects newline-free multi-frame oversize before output growth" {+ const frame = h2.frame;+ var srv: Io.Writer.Allocating = .init(std.testing.allocator);+ defer srv.deinit();+ try frame.write(&srv.writer, .{ .typ = .settings, .flags = 0, .stream_id = 0, .payload = &.{} });+ const ok = [_]u8{0x88};+ try frame.write(&srv.writer, .{ .typ = .headers, .flags = frame.flags.end_headers, .stream_id = 1, .payload = &ok });+ const part: [16_384]u8 = @splat('x');+ for (0..5) |i| try frame.write(&srv.writer, .{ .typ = .data, .flags = if (i == 4) frame.flags.end_stream else 0, .stream_id = 1, .payload = &part });+ var reader: Io.Reader = .fixed(srv.written());+ var writer: Io.Writer.Allocating = .init(std.testing.allocator);+ defer writer.deinit();+ var conn = h2.Conn.init(std.testing.allocator, &reader, &writer.writer);+ defer conn.deinit();+ var stream = try conn.startLines(.{ .method = "GET", .scheme = "https", .authority = "localhost", .path = "/" });+ defer stream.deinit();+ var client: std.http.Client = undefined;+ var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = max_body };+ try std.testing.expectError(error.ResponseTooLarge, ex.consume(&stream));+ try std.testing.expectEqual(@as(usize, max_body), ex.len);+ try std.testing.expect(stream.pending.items.len <= 16_384);+}++test "buffered HTTP2 preserves non-success status without reading its body" {+ const frame = h2.frame;+ var srv: Io.Writer.Allocating = .init(std.testing.allocator);+ defer srv.deinit();+ try frame.write(&srv.writer, .{ .typ = .settings, .flags = 0, .stream_id = 0, .payload = &.{} });+ // Literal :status 401, HPACK name index 8 and a three-byte value.+ const unauthorized = [_]u8{ 0x08, 0x03, '4', '0', '1' };+ try frame.write(&srv.writer, .{ .typ = .headers, .flags = frame.flags.end_headers, .stream_id = 1, .payload = &unauthorized });+ try frame.write(&srv.writer, .{ .typ = .data, .flags = frame.flags.end_stream, .stream_id = 1, .payload = "private error body" });+ var reader: Io.Reader = .fixed(srv.written());+ var writer: Io.Writer.Allocating = .init(std.testing.allocator);+ defer writer.deinit();+ var conn = h2.Conn.init(std.testing.allocator, &reader, &writer.writer);+ defer conn.deinit();+ var stream = try conn.startLines(.{ .method = "GET", .scheme = "https", .authority = "localhost", .path = "/" });+ defer stream.deinit();+ var client: std.http.Client = undefined;+ var ex: Exchange = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .url = "", .bearer = "", .body = "", .limit = max_body };+ try ex.consume(&stream);+ try std.testing.expectEqual(@as(u16, 401), ex.status);+ try std.testing.expectEqual(@as(usize, 0), ex.len);+}++test "loopback HTTPS stalled body cancels and releases the exchange" {+ const value = std.c.getenv("GRAFF_JEV_HTTP2_STALL_URL") orelse return error.SkipZigTest;+ const url = std.mem.span(value);+ 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();+ const started = Io.Timestamp.now(io, .awake).nanoseconds;+ try std.testing.expectError(error.DeadlineExceeded, post(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);+}++test "loopback HTTP2 ambiguous post-send drop is never replayed" {+ const value = std.c.getenv("GRAFF_JEV_HTTP2_DROP_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();+ const response = post(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;+}diff --git a/src/jev_tool.zig b/src/jev_tool.zigindex 8448fb6f..3351fcfa 100644--- a/src/jev_tool.zig+++ b/src/jev_tool.zig@@ -11,6 +11,7 @@ const ToolOutput = @import("tools.zig").ToolOutput;const ToolSpec = @import("schema.zig").ToolSpec;const scope = @import("jev_model_scope.zig");const pricing = @import("pricing.zig");+const buffered_https = @import("http2_buffered.zig");pub const name = "jev_judge";pub const description = "Ask Jev one closed-form judgment about a SHORT, NON-SENSITIVE state. Requires a Codegraff login and an eligible GPT-6 or MiMo model. Use for yes/no (noul), a choice, or an ordered score, not writing or open-ended reasoning. Never send source code, secrets, customer data, paths, or unrelated context. A low-confidence verdict escalates to you. If Jev fails once, this tool skips all later Jev calls for this session; decide yourself instead.";@@ -202,30 +203,19 @@ fn noteGatewayUsage(io: Io, tally: *pricing.CostTally, arena: Allocator, raw: []fn fetch(ctx: ToolCtx, arena: Allocator, body: []const u8) ![]const u8 {const bearer = try gatewayBearer(arena, state.key);- var aw = Io.Writer.fixed(try arena.alloc(u8, 64 * 1024));var completed = false;defer if (!completed) pricing.g_cost.failedWithoutUsage(ctx.io, 1);- const res = try ctx.client.fetch(.{- .location = .{ .url = endpoint },- .method = .POST,- .payload = body,- .response_writer = &aw,- .redirect_behavior = .unhandled,- .headers = .{- .content_type = .{ .override = "application/json" },- .authorization = .{ .override = bearer },- },- });- switch (@intFromEnum(res.status)) {+ const res = try buffered_https.post(arena, ctx.io, ctx.client, endpoint, bearer, body, 10_000);+ switch (res.status) {200 => {},401, 403 => return error.JevUnauthorized,402 => return error.JevNoCredits,429 => return error.JevRateLimited,else => return error.JevUnavailable,}- noteGatewayUsage(ctx.io, &pricing.g_cost, arena, aw.buffered());+ noteGatewayUsage(ctx.io, &pricing.g_cost, arena, res.body);completed = true;- return aw.buffered();+ return res.body;}fn verdict(arena: Allocator, input: Value, raw: []const u8) ![]const u8 {diff --git a/src/main.zig b/src/main.zigindex b0b24003..9f43a840 100644--- a/src/main.zig+++ b/src/main.zig@@ -50,7 +50,7 @@ const learn_store = @import("learn_store.zig");const learn_eval = @import("learn_eval.zig");const learn_cli = @import("learn_cli.zig");test {- _ = .{ @import("clipboard_native.zig"), @import("clipboard_edge_tests.zig"), @import("clipboard_failure_tests.zig"), @import("process_runner_clipboard_tests.zig") };+ _ = .{ @import("http2_buffered.zig"), @import("clipboard_native.zig"), @import("clipboard_edge_tests.zig"), @import("clipboard_failure_tests.zig"), @import("process_runner_clipboard_tests.zig") };_ = @import("server_orphan.zig");_ = @import("repo_transaction.zig");_ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("pr_verify.zig"), @import("pr_acceptance.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig") };