Skip to content

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

Python

Keep HTTP/2 sessions usable across turns.

justrach6a568763b476
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 272cd842..8e31446d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,7 +25,7 @@ current is part of cutting a release.
- Task workspace housekeeping: `graff worktree update` merges the remote base; `gc` drops dead session trees and clean trees whose GitHub PR is MERGED (dirty trees stay). Desktop: Update from main, Clear unused trees. Asking to free space unfolds `workspace` so `action=gc` is callable this turn.
- xAI Responses WS chains `previous_response_id` on the held socket (store:false in-memory cache) and `generate:false` prewarms a fresh socket. A stalled first handshake or 426 latches HTTPS SSE for the session; chrome shows a dim `using HTTPS SSE` notice. `GRAFF_XAI_WS_CHAIN=0` opts out.
-- HTTPS SSE prefers HTTP/2 (`justrach/http-zig`): connection reuse, ALPN `h2`, first-failure HTTP/1.1 latch. `GRAFF_HTTP2=0` forces 1.1.
+- HTTPS SSE prefers HTTP/2 (`justrach/http-zig`): connection reuse, ALPN `h2`, first-failure HTTP/1.1 latch. `GRAFF_HTTP2=0` forces 1.1. A finished turn keeps that connection; the socket is flushed and DATA windows are credited so the next stream is not stuck. A stream that does not end is dropped instead of reused.
- grok-4.7 is the xAI default (500k, same dual-band $2/$6 and 200k high band as 4.6; efforts still low|medium|high|xhigh).
## v0.0.302.3
diff --git a/build.zig.zon b/build.zig.zon
index 8792aba6..850fc3fa 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#1d734eee07d3add6638b171adcc0da9f933d7902",
- .hash = "http_zig-0.1.0-2CB7E-I9AgAXrxfMjTCoG9Q9RRwh3QhfEY4OXfhHWCiX",
+ .url = "git+https://github.com/justrach/http-zig#3228257a00fd186ba48b65768c22cac0969beeb1",
+ .hash = "http_zig-0.1.0-2CB7EztzAgAf1-X3QYsdpwL7pzWcfHuEzBOCRSL3c5t6",
},
},
.paths = .{""},
diff --git a/lean-proofs/Graff/Provider.lean b/lean-proofs/Graff/Provider.lean
index 85939dff..642c6b48 100644
--- a/lean-proofs/Graff/Provider.lean
+++ b/lean-proofs/Graff/Provider.lean
@@ -51,7 +51,7 @@ def specs : List Row :=
, { id := "openai", kind := .responses, auth := .bearer }
, { id := "google", kind := .interactions, auth := .googApiKey, catalog := .openai, takesEffort := true }
, { id := "minimax", kind := .anthropic, auth := .bearer }
- , { id := "xiaomi", kind := .openai, auth := .bearer }
+ , { id := "xiaomi", kind := .openai, auth := .bearer, catalog := .openai }
, { id := "kilo", kind := .openai, auth := .bearer }
, { id := "groq", kind := .openai, auth := .bearer }
, { id := "cerebras", kind := .openai, auth := .bearer, catalog := .openai, takesEffort := true }
diff --git a/spec/kernels/providers.json b/spec/kernels/providers.json
index 32efb0c0..3260b9a4 100644
--- a/spec/kernels/providers.json
+++ b/spec/kernels/providers.json
@@ -61,7 +61,7 @@
"kind": "openai",
"auth": "bearer",
"login": "api_key",
- "catalog": "baked",
+ "catalog": "openai",
"sub_login": false,
"takes_effort": false
},
diff --git a/spec/ref/provider.py b/spec/ref/provider.py
index a73753a0..363d43dc 100644
--- a/spec/ref/provider.py
+++ b/spec/ref/provider.py
@@ -37,7 +37,7 @@ SPECS: list[dict] = [
row("openai", "responses", "bearer"),
row("google", "interactions", "goog_api_key", catalog="openai", takes_effort=True),
row("minimax", "anthropic", "bearer"),
- row("xiaomi", "openai", "bearer"),
+ row("xiaomi", "openai", "bearer", catalog="openai"),
row("kilo", "openai", "bearer"),
row("groq", "openai", "bearer"),
row("cerebras", "openai", "bearer", catalog="openai", takes_effort=True),
diff --git a/src/agent_stream_h2.zig b/src/agent_stream_h2.zig
index 398c632a..1ae27495 100644
--- a/src/agent_stream_h2.zig
+++ b/src/agent_stream_h2.zig
@@ -48,6 +48,8 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {
}
const sess = http2_pool.sessionFor(gpa, self.io, origin.host, origin.port) catch return null;
+ var keep = false;
+ defer if (!keep) http2_pool.invalidate(self.io);
var lines = sess.startLines(.{
.method = "POST",
.scheme = "https",
@@ -99,7 +101,10 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {
}
return e;
};
- if (!more) break;
+ if (!more) {
+ keep = http2_pool.keepAfter(lines.ended);
+ break;
+ }
break :read;
};
const first = rsel.await() catch |e| {
@@ -117,7 +122,10 @@ pub fn postStream(self: *Agent, body: []const u8) !?[]u8 {
}
return e;
};
- if (!more) break;
+ if (!more) {
+ keep = http2_pool.keepAfter(lines.ended);
+ break;
+ }
},
.stall => |w| {
if (w == .deadline) self.stall.tripped_ms = stall_budget;
@@ -139,6 +147,12 @@ 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;
+ }
break;
}
if (self.sub and Agent.esc_cancel.load(.acquire)) {
diff --git a/src/http2_pool.zig b/src/http2_pool.zig
index 09b146d5..f3af7ff3 100644
--- a/src/http2_pool.zig
+++ b/src/http2_pool.zig
@@ -49,6 +49,61 @@ pub fn sessionFor(gpa: std.mem.Allocator, io: Io, host: []const u8, p: u16) !*ht
return s;
}
+/// Drop the pooled session. A stream that did not reach END_STREAM must not
+/// be reused: the next turn would read the previous frame.
+pub fn invalidate(io: Io) void {
+ mu.lockUncancelable(io);
+ defer mu.unlock(io);
+ if (session) |s| s.close();
+ session = null;
+ host_len = 0;
+}
+
+pub fn keepAfter(ended: bool) bool {
+ return ended;
+}
+
+test "keep the pooled session only after END_STREAM" {
+ try std.testing.expect(keepAfter(true));
+ try std.testing.expect(!keepAfter(false));
+}
+
+test "http-zig credits DATA on the connection and the stream" {
+ const gpa = std.testing.allocator;
+ const frame = http_zig.frame;
+ var srv_aw: std.Io.Writer.Allocating = .init(gpa);
+ defer srv_aw.deinit();
+ try frame.write(&srv_aw.writer, .{ .typ = .settings, .flags = 0, .stream_id = 0, .payload = &.{} });
+ const status_hpack = [_]u8{0x88};
+ try frame.write(&srv_aw.writer, .{ .typ = .headers, .flags = frame.flags.end_headers, .stream_id = 1, .payload = &status_hpack });
+ try frame.write(&srv_aw.writer, .{ .typ = .data, .flags = frame.flags.end_stream, .stream_id = 1, .payload = "ok" });
+ const server_bytes = try gpa.dupe(u8, srv_aw.written());
+ defer gpa.free(server_bytes);
+ var reader: std.Io.Reader = .fixed(server_bytes);
+ var client_aw: std.Io.Writer.Allocating = .init(gpa);
+ defer client_aw.deinit();
+ var c = http_zig.Conn.init(gpa, &reader, &client_aw.writer);
+ defer c.deinit();
+ var res = try c.request(.{ .method = "GET", .scheme = "https", .authority = "api.x.ai", .path = "/v1/models" });
+ defer res.deinit();
+ try std.testing.expectEqual(@as(u16, 200), res.status);
+ try std.testing.expectEqual(@as(usize, 2), countWindowUpdates(client_aw.written()));
+}
+
+fn countWindowUpdates(bytes: []const u8) usize {
+ const frame = http_zig.frame;
+ var i: usize = 0;
+ if (std.mem.startsWith(u8, bytes, frame.preface)) i = frame.preface.len;
+ var n: usize = 0;
+ while (i + 9 <= bytes.len) {
+ const len = (@as(usize, bytes[i]) << 16) | (@as(usize, bytes[i + 1]) << 8) | bytes[i + 2];
+ if (i + 9 + len > bytes.len) break;
+ if (bytes[i + 3] == @intFromEnum(frame.Type.window_update)) n += 1;
+ i += 9 + len;
+ }
+ return n;
+}
+
test "want is https-only" {
const saved = main_mod.g_http2;
defer main_mod.g_http2 = saved;