Skip to content

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

Python

fix(cache): send session_id on chatgpt-new HTTP and WebSocket requests

justrach40ecf1a3c156
diff --git a/src/agent_ws.zig b/src/agent_ws.zig
index fca9eb36..fb7984f1 100644
--- a/src/agent_ws.zig
+++ b/src/agent_ws.zig
@@ -347,27 +347,14 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {
const frame = @import("agent_ws_prewarm.zig").stripTransportFields(frame_full);
const bearer = try std.fmt.allocPrint(arena, "Bearer {s}", .{provider.api_key});
- // ChatGPT tail is codex-only (#502); xAI takes grok-build's affinity
- // pair: x-grok-session-id (durable project) + x-grok-conv-id (root/sub
+ // ChatGPT tail is codex-only (#502); other session_id routes send
+ // Authorization + session_id, xAI takes grok-build's affinity pair:
+ // x-grok-session-id (durable project) + x-grok-conv-id (root/sub
// partition, same value as the Responses body's prompt_cache_key).
var conv_buf: [96]u8 = undefined;
const conv = http_headers.requestCacheKey(self.io, self.label, self, provider, &conv_buf);
var hdrs: [7]ws.Header = undefined;
- var hn: usize = 1;
- hdrs[0] = .{ .name = "Authorization", .value = bearer };
- if (std.mem.eql(u8, provider.id, "codex")) {
- hdrs[1] = .{ .name = "session_id", .value = conv };
- hdrs[2] = .{ .name = "chatgpt-account-id", .value = provider.account };
- hdrs[3] = .{ .name = "OpenAI-Beta", .value = "responses_websockets=2026-02-06" };
- hdrs[4] = .{ .name = "originator", .value = "codex_cli_rs" };
- hdrs[5] = .{ .name = "User-Agent", .value = "codex_cli_rs/0.1 (graff)" };
- hn = 6;
- } else if (http_headers.wantsGrokConvId(provider.id)) {
- hdrs[1] = .{ .name = "x-grok-session-id", .value = http_headers.projectRootId(self.io) };
- hdrs[2] = .{ .name = "x-grok-conv-id", .value = conv };
- hn = 3;
- }
- const headers: []const ws.Header = hdrs[0..hn];
+ const headers: []const ws.Header = @import("agent_ws_headers.zig").handshakeHeaders(self.io, provider, bearer, conv, &hdrs);
const url = try wssUrl(arena, provider.url);
// Esc watching (root TTY), same gate as postStream.
diff --git a/src/agent_ws_headers.zig b/src/agent_ws_headers.zig
new file mode 100644
index 00000000..6a354b8d
--- /dev/null
+++ b/src/agent_ws_headers.zig
@@ -0,0 +1,70 @@
+//! WebSocket handshake headers per provider. The WS path cannot reuse
+//! providerHeaders (std.http.Header vs ws.Header, and codex's socket adds a
+//! websockets beta + User-Agent), so the selection lives in one place.
+
+const std = @import("std");
+const Io = std.Io;
+const ws = @import("ws.zig");
+const http_headers = @import("http_headers.zig");
+const Provider = @import("provider.zig").Provider;
+
+/// Authorization plus the provider's affinity/identity headers. Codex sends
+/// the full ChatGPT-backend set; other session_id routes (chatgpt-new) get
+/// Authorization + session_id only — the Platform endpoint rejects backend
+/// identity. xAI sends its grok-build affinity pair.
+pub fn handshakeHeaders(io: Io, provider: Provider, bearer: []const u8, conv: []const u8, hdrs: *[7]ws.Header) []const ws.Header {
+ var hn: usize = 1;
+ hdrs[0] = .{ .name = "Authorization", .value = bearer };
+ if (std.mem.eql(u8, provider.id, "codex")) {
+ hdrs[1] = .{ .name = "session_id", .value = conv };
+ hdrs[2] = .{ .name = "chatgpt-account-id", .value = provider.account };
+ hdrs[3] = .{ .name = "OpenAI-Beta", .value = "responses_websockets=2026-02-06" };
+ hdrs[4] = .{ .name = "originator", .value = "codex_cli_rs" };
+ hdrs[5] = .{ .name = "User-Agent", .value = "codex_cli_rs/0.1 (graff)" };
+ hn = 6;
+ } else if (http_headers.wantsSessionIdHeader(provider.id)) {
+ hdrs[1] = .{ .name = "session_id", .value = conv };
+ hn = 2;
+ } else if (http_headers.wantsGrokConvId(provider.id)) {
+ hdrs[1] = .{ .name = "x-grok-session-id", .value = http_headers.projectRootId(io) };
+ hdrs[2] = .{ .name = "x-grok-conv-id", .value = conv };
+ hn = 3;
+ }
+ return hdrs[0..hn];
+}
+
+test "chatgpt-new WS handshake sends session_id only; codex keeps the full set" {
+ const io = std.testing.io;
+ var hdrs: [7]ws.Header = undefined;
+ const chatgpt: Provider = .{ .id = "chatgpt-new", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-6.1-sol", .context = 272_000 };
+ const h = handshakeHeaders(io, chatgpt, "Bearer k", "conv-1", &hdrs);
+ try std.testing.expectEqual(@as(usize, 2), h.len);
+ try std.testing.expectEqualStrings("Authorization", h[0].name);
+ try std.testing.expectEqualStrings("session_id", h[1].name);
+ try std.testing.expectEqualStrings("conv-1", h[1].value);
+
+ const codex: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-5.6", .context = 272_000, .account = "acct" };
+ const c = handshakeHeaders(io, codex, "Bearer k", "conv-1", &hdrs);
+ try std.testing.expectEqual(@as(usize, 6), c.len);
+ const names = [_][]const u8{ "Authorization", "session_id", "chatgpt-account-id", "OpenAI-Beta", "originator", "User-Agent" };
+ for (names, c) |want, got| try std.testing.expectEqualStrings(want, got.name);
+ try std.testing.expectEqualStrings("conv-1", c[1].value);
+ try std.testing.expectEqualStrings("acct", c[2].value);
+}
+
+test "xAI WS handshake keeps the grok pair; openai sends Authorization only" {
+ const io = std.testing.io;
+ var hdrs: [7]ws.Header = undefined;
+ const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };
+ const x = handshakeHeaders(io, xai, "Bearer k", "conv-1", &hdrs);
+ try std.testing.expectEqual(@as(usize, 3), x.len);
+ try std.testing.expectEqualStrings("x-grok-session-id", x[1].name);
+ try std.testing.expectEqualStrings(http_headers.projectRootId(io), x[1].value);
+ try std.testing.expectEqualStrings("x-grok-conv-id", x[2].name);
+ try std.testing.expectEqualStrings("conv-1", x[2].value);
+
+ const openai: Provider = .{ .id = "openai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-5.6", .context = 272_000 };
+ const o = handshakeHeaders(io, openai, "Bearer k", "conv-1", &hdrs);
+ try std.testing.expectEqual(@as(usize, 1), o.len);
+ try std.testing.expectEqualStrings("Authorization", o[0].name);
+}
diff --git a/src/http_headers.zig b/src/http_headers.zig
index 4a125691..27e86398 100644
--- a/src/http_headers.zig
+++ b/src/http_headers.zig
@@ -189,6 +189,14 @@ pub fn wantsGrokConvId(provider_id: []const u8) bool {
return std.mem.eql(u8, provider_id, "xai");
}
+/// ChatGPT sign-in Responses routes send `session_id` equal to the body's
+/// `prompt_cache_key` — the endpoint keys prompt caching on it, and a body
+/// key alone returns zero cached tokens. The API-key `openai` route is
+/// untested and stays without it.
+pub fn wantsSessionIdHeader(provider_id: []const u8) bool {
+ return std.mem.eql(u8, provider_id, "codex") or std.mem.eql(u8, provider_id, "chatgpt-new");
+}
+
pub fn providerHeaders(io: Io, provider: Provider, bearer: []const u8, buf: *[12]std.http.Header) []const std.http.Header {
return providerHeadersWithConv(io, provider, bearer, buf, null);
}
@@ -262,7 +270,11 @@ pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, b
count += 1;
buf[count] = .{ .name = "originator", .value = "codex_cli_rs" };
count += 1;
- // openai/codex ModelClient: prompt_cache_key defaults to session_id.
+ }
+ // openai/codex ModelClient: prompt_cache_key defaults to session_id.
+ // chatgpt-new's Platform endpoint also caches only with the header; the
+ // account/beta/originator backend identity stays codex-only.
+ if (wantsSessionIdHeader(provider.id)) {
buf[count] = .{ .name = "session_id", .value = conv_id orelse projectRootId(io) };
count += 1;
}
@@ -548,3 +560,37 @@ test "the thinking-binding beta rides only 5.5-era Claude models on the Anthropi
p = .{ .id = "minimax", .kind = .anthropic, .auth = .x_api_key, .url = "", .api_key = "k", .model = "claude-opus-5-5", .context = 1_000_000 };
try std.testing.expect(beta(providerHeaders(io, p, "", &buf)) == null);
}
+
+test "chatgpt-new sends session_id equal to the body cache key, without backend identity" {
+ const io = std.testing.io;
+ var buf: [12]std.http.Header = undefined;
+ const p: Provider = .{ .id = "chatgpt-new", .kind = .responses, .auth = .bearer, .url = "https://api.openai.com/v1/responses", .api_key = "k", .model = "gpt-6.1-sol", .context = 272_000 };
+ var ckbuf: [96]u8 = undefined;
+ var agent_ptr: usize = 1;
+ const key = requestCacheKey(io, "main", @ptrCast(&agent_ptr), p, &ckbuf);
+ const headers = providerHeadersWithConv(io, p, "Bearer k", &buf, key);
+ try std.testing.expectEqualStrings(key, headerValue(headers, "session_id") orelse return error.SessionIdHeaderMissing);
+ for (headers) |h| {
+ try std.testing.expect(!std.mem.eql(u8, h.name, "chatgpt-account-id"));
+ try std.testing.expect(!std.mem.eql(u8, h.name, "OpenAI-Beta"));
+ try std.testing.expect(!std.mem.eql(u8, h.name, "originator"));
+ }
+ // One-off posts with no explicit conv still pin the project root id,
+ // the same value a main-label body's prompt_cache_key carries.
+ const headers2 = providerHeaders(io, p, "Bearer k", &buf);
+ try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers2, "session_id") orelse return error.SessionIdHeaderMissing);
+}
+
+test "every subscription-login Responses provider sends session_id" {
+ const io = std.testing.io;
+ var buf: [12]std.http.Header = undefined;
+ var checked: usize = 0;
+ for (@import("provider.zig").provider_specs) |spec| {
+ if (spec.kind != .responses or !spec.sub_login) continue;
+ checked += 1;
+ const p: Provider = .{ .id = spec.id, .kind = spec.kind, .auth = spec.auth, .url = "", .api_key = "k", .model = "m", .context = 1, .account = "acct" };
+ const headers = providerHeaders(io, p, "Bearer k", &buf);
+ try std.testing.expect(headerValue(headers, "session_id") != null);
+ }
+ try std.testing.expect(checked >= 2); // codex + chatgpt-new today
+}
diff --git a/src/main.zig b/src/main.zig
index 00fb2ef9..029616d4 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -576,7 +576,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu
_ = @import("agent_request_search_tests.zig");
_ = @import("agent_ws_steer.zig");
_ = @import("agent_ws_signal.zig");
- _ = @import("agent_ws_prewarm.zig");
+ _ = .{ @import("agent_ws_prewarm.zig"), @import("agent_ws_headers.zig") };
_ = .{ @import("http2_pool.zig"), @import("agent_stream_h2.zig"), @import("agent_stream_h2_transport_test.zig") }; // h2 stream path, and graff's request/response contract over the pinned http-zig
_ = @import("acp_preauth.zig"); // credential-free ACP loop must stay in the test root
_ = .{ @import("acp_protocol.zig"), @import("acp_v2.zig"), @import("acp_v2_prompt.zig"), @import("acp_elicit.zig"), @import("hot_context.zig"), @import("history_translate.zig"), @import("mcp_notify.zig"), @import("mcp_wait.zig"), @import("mcp_pages.zig"), @import("mcp_mrtr.zig"), @import("ask_user_args.zig"), @import("mcp_add.zig"), @import("mcp_lazy.zig"), @import("mcp_share.zig"), @import("acp_room.zig"), @import("mcp_catalog.zig"), @import("mcp_watch.zig"), @import("acp_engine_test.zig"), @import("subagent_mimo_tests.zig"), @import("mimo_effort_tests.zig"), @import("gateway_picker_catalog.zig"), @import("ask_user_answers.zig"), @import("job_class.zig"), @import("startup_claim.zig") };