Skip to content

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

Python

WebSocket accepts the same event size as SSE; chatgpt-new takes a mock URL

Rach Pradhan703af5d772e2
diff --git a/src/provider.zig b/src/provider.zig
index 35600c16..cdf954c5 100644
--- a/src/provider.zig
+++ b/src/provider.zig
@@ -156,10 +156,11 @@ pub fn specFor(id: []const u8) ?ProviderSpec {
pub const kimi_native_url = "https://api.kimi.com/coding/v1/chat/completions";
pub const kimi_anthropic_url = "https://api.kimi.com/coding/v1/messages?beta=true";
-/// Endpoint overrides (GRAFF_CODEX_URL / GRAFF_XAI_URL / GRAFF_ZAI_URL /
-/// GRAFF_VERCEL_URL). Parsed before Keys.build so startup, /model, and
-/// session restore share one URL.
+/// Endpoint overrides (GRAFF_CODEX_URL / GRAFF_CHATGPT_URL / GRAFF_XAI_URL /
+/// GRAFF_ZAI_URL / GRAFF_VERCEL_URL). Parsed before Keys.build so startup,
+/// /model, and session restore share one URL.
pub var g_codex_url_override: ?[]const u8 = null;
+pub var g_chatgpt_url_override: ?[]const u8 = null;
pub var g_xai_url_override: ?[]const u8 = null;
pub var g_zai_url_override: ?[]const u8 = null;
pub var g_vercel_url_override: ?[]const u8 = null;
@@ -170,6 +171,7 @@ pub const vercel_v1_url = "https://ai-gateway.vercel.sh/v1/chat/completions";
fn resolvedUrl(spec: ProviderSpec, model: []const u8) []const u8 {
if (std.mem.eql(u8, spec.id, "kimi") and pricing.kimiProtocol(model) == .anthropic) return kimi_anthropic_url;
if (std.mem.eql(u8, spec.id, "codex")) return g_codex_url_override orelse spec.url;
+ if (std.mem.eql(u8, spec.id, "chatgpt-new")) return g_chatgpt_url_override orelse spec.url;
if (std.mem.eql(u8, spec.id, "xai")) return g_xai_url_override orelse if (g_xai_responses) xai_responses_url else spec.url;
if (std.mem.eql(u8, spec.id, "zai")) return g_zai_url_override orelse spec.url;
if (std.mem.eql(u8, spec.id, "vercel")) return g_vercel_url_override orelse spec.url;
diff --git a/src/router_catalog.zig b/src/router_catalog.zig
index 37f71f81..12246ea2 100644
--- a/src/router_catalog.zig
+++ b/src/router_catalog.zig
@@ -86,7 +86,19 @@ pub fn dynamic(spec: provider.ProviderSpec) bool {
}
pub fn modelsUrl(spec: provider.ProviderSpec) []const u8 {
- return if (dynamic(spec)) spec.models_url else "";
+ if (!dynamic(spec)) return "";
+ // GRAFF_CHATGPT_URL keeps discovery on the mock's origin, never the live API.
+ if (std.mem.eql(u8, spec.id, "chatgpt-new")) if (provider.g_chatgpt_url_override) |url| return overrideModelsUrl(url);
+ return spec.models_url;
+}
+
+var override_models_buf: [256]u8 = undefined;
+
+/// scheme://host[:port] of `url` + /v1/models; `url` itself when unparsable.
+fn overrideModelsUrl(url: []const u8) []const u8 {
+ const scheme_end = std.mem.indexOf(u8, url, "://") orelse return url;
+ const path_start = std.mem.indexOfScalarPos(u8, url, scheme_end + 3, '/') orelse url.len;
+ return std.fmt.bufPrint(&override_models_buf, "{s}/v1/models", .{url[0..path_start]}) catch url;
}
fn validModelId(id: []const u8) bool {
diff --git a/src/router_catalog_tests.zig b/src/router_catalog_tests.zig
index c36ae260..00132898 100644
--- a/src/router_catalog_tests.zig
+++ b/src/router_catalog_tests.zig
@@ -538,3 +538,14 @@ test "xAI defaults to the Responses wire; GRAFF_XAI_WIRE=chat opts out (#502)" {
try std.testing.expectEqual(Provider.Kind.responses, oai.kind);
try std.testing.expect(std.mem.indexOf(u8, oai.url, "api.openai.com") != null);
}
+
+test "GRAFF_CHATGPT_URL keeps chatgpt-new discovery on the override origin" {
+ const saved = provider.g_chatgpt_url_override;
+ defer provider.g_chatgpt_url_override = saved;
+ const spec = provider.specFor("chatgpt-new").?;
+ provider.g_chatgpt_url_override = null;
+ try std.testing.expectEqualStrings(spec.models_url, modelsUrl(spec));
+ provider.g_chatgpt_url_override = "http://127.0.0.1:4321/v1/responses";
+ try std.testing.expectEqualStrings("http://127.0.0.1:4321/v1/models", modelsUrl(spec));
+ try std.testing.expectEqualStrings(provider.specFor("codex").?.models_url, modelsUrl(provider.specFor("codex").?));
+}
diff --git a/src/startup_keys.zig b/src/startup_keys.zig
index f1e756be..90115bbd 100644
--- a/src/startup_keys.zig
+++ b/src/startup_keys.zig
@@ -265,6 +265,11 @@ fn missingCredentials() noreturn {
/// failures without manufacturing a provider. All successful resolution and
/// model-selection behavior is shared with resolveKeys above.
pub fn resolveKeysOptional(io: Io, gpa: Allocator, arena: Allocator, environ_map: anytype, model_flag: ?[]const u8, worker_provider_hint: ?[]const u8) !?ResolvedKeys {
+ // GRAFF_CHATGPT_URL: chatgpt-new's Responses endpoint for localhost mocks,
+ // read before Keys.build and the startup catalog load so neither goes live.
+ if (environ_map.get("GRAFF_CHATGPT_URL")) |v| {
+ if (v.len > 0) provider_mod.g_chatgpt_url_override = v;
+ }
var keys: provider_mod.Keys = .{ .values = undefined };
for (provider_mod.provider_specs, &keys.values, &keys.sources) |spec, *value, *source| {
value.* = environ_map.get(spec.env_key);
diff --git a/src/stream_read_progress.zig b/src/stream_read_progress.zig
index 925883df..76efba7c 100644
--- a/src/stream_read_progress.zig
+++ b/src/stream_read_progress.zig
@@ -6,11 +6,15 @@ const http = @import("http.zig");
const Agent = @import("agent.zig").Agent;
const cancel_source = @import("cancel_source.zig");
+/// Largest single streamed event either transport assembles. A Responses
+/// compaction item carries its opaque state whole; real ones exceed 60 MB.
+pub const max_event_bytes: usize = 128 * 1024 * 1024;
+
pub const Limits = struct {
idle_ms: u64,
// A peer dripping bytes cannot hold an unfinished event open forever.
total_ms: u64 = 15 * 60 * 1000,
- max_bytes: usize = 128 * 1024 * 1024,
+ max_bytes: usize = max_event_bytes,
};
pub fn expired(start_ms: i64, last_ms: i64, now_ms: i64, limits: Limits) bool {
diff --git a/src/ws.zig b/src/ws.zig
index 265726b3..0eb9108b 100644
--- a/src/ws.zig
+++ b/src/ws.zig
@@ -62,7 +62,9 @@ const sock_buf_cap = 64 * 1024;
// Plaintext TLS buffers.
const tls_buf_cap = 64 * 1024;
/// Hard cap on one reassembled inbound message (a single Responses event).
-const message_cap = 4 * 1024 * 1024;
+/// The SSE cap too: 4 MiB refused a compaction item's opaque state, so the
+/// turn was retried on a fresh socket and then SSE, each re-running it.
+const message_cap = @import("stream_read_progress.zig").max_event_bytes;
comptime {
if (sock_buf_cap < tls.max_ciphertext_record_len) @compileError("sock_buf_cap < tls min_buffer_len");
@@ -362,3 +364,30 @@ test "counted forced WS failures exhaust exactly" {
));
try std.testing.expectEqual(@as(u8, 0), g_force_connect_failure_count);
}
+
+test "an inbound message may carry a compaction item's multi-megabyte state" {
+ const gpa = std.testing.allocator;
+ const len: usize = 5 * 1024 * 1024; // over the old 4 MiB cap
+ const frame = try gpa.alloc(u8, 10 + len);
+ defer gpa.free(frame);
+ frame[0] = 0x81; // FIN | text
+ frame[1] = 127;
+ std.mem.writeInt(u64, frame[2..10], len, .big);
+ @memset(frame[10..], 'x');
+ var reader: Io.Reader = .fixed(frame);
+ var client: WsClient = undefined;
+ client.r = &reader;
+ var out: std.ArrayList(u8) = .empty;
+ defer out.deinit(gpa);
+ try std.testing.expectEqual(Opcode.text, try client.readMessage(gpa, &out));
+ try std.testing.expectEqual(len, out.items.len);
+
+ // A header claiming more than the cap is refused before any allocation.
+ var oversized: [10]u8 = undefined;
+ oversized[0] = 0x81;
+ oversized[1] = 127;
+ std.mem.writeInt(u64, oversized[2..10], message_cap + 1, .big);
+ var over_reader: Io.Reader = .fixed(&oversized);
+ client.r = &over_reader;
+ try std.testing.expectError(error.MessageTooLong, client.readMessage(gpa, &out));
+}