Skip to content

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

Python
28.2 KB
Raw Blame
//! Provider-specific HTTP identity and authorization headers.

const std = @import("std");
const Io = std.Io;
const Provider = @import("provider.zig").Provider;
const root = @import("main.zig");
const kimi_catalog = @import("kimi_catalog.zig");
const cache_affinity = @import("cache_affinity.zig"); // ADR 0069: git-root / scratch seed
const client_tags = @import("client_tags.zig");
const anthropic_oauth = @import("anthropic_oauth_wire.zig");

var session_id_buf: [36]u8 = undefined;
var session_id_len: usize = 0;
/// Spin-lock, not an Io.Mutex: providerHeaders has no Io handle to reach one
/// with, and the critical section is a single one-time 36-byte format.
var session_id_lock: std.atomic.Value(bool) = .init(false);

/// Process UUIDv4 persisted with the session; distinct from prompt-cache routing.
pub fn sessionId(io: Io) []const u8 {
    while (session_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
    defer session_id_lock.store(false, .release);
    if (session_id_len == 0) {
        var raw: [16]u8 = undefined;
        io.random(&raw);
        raw[6] = (raw[6] & 0x0f) | 0x40; // version 4
        raw[8] = (raw[8] & 0x3f) | 0x80; // variant 1
        const hex = std.fmt.bytesToHex(raw, .lower);
        @memcpy(session_id_buf[0..8], hex[0..8]);
        session_id_buf[8] = '-';
        @memcpy(session_id_buf[9..13], hex[8..12]);
        session_id_buf[13] = '-';
        @memcpy(session_id_buf[14..18], hex[12..16]);
        session_id_buf[18] = '-';
        @memcpy(session_id_buf[19..23], hex[16..20]);
        session_id_buf[23] = '-';
        @memcpy(session_id_buf[24..36], hex[20..32]);
        session_id_len = 36;
    }
    return session_id_buf[0..session_id_len];
}

/// A clone-on-write session is a new durable conversation, not another name
/// for the parent's cache identity. Mint its UUID before the first child save.
pub fn renewSessionId(io: Io) []const u8 {
    while (session_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
    session_id_len = 0;
    session_id_lock.store(false, .release);
    return sessionId(io);
}

/// Conversation affinity for one request. xAI routes `x-grok-conv-id` (Chat
/// Completions) and `prompt_cache_key` (Responses) to the same server — cache
/// entries are per-server, so a sticky id is how prefix hits stay reliable
/// (docs.x.ai prompt-caching / maximizing-cache-hits). Root is the durable
/// project id. Children share four role lanes so sibling scouts with the
/// same system+tools land together; they do **not** reuse the root id
/// (different prefix). `agent` is unused: a pointer suffix would split
/// siblings onto different servers and miss on purpose.
pub fn promptCacheKey(io: Io, label: []const u8, agent: *const anyopaque, buf: []u8) []const u8 {
    _ = agent;
    return promptPrefixCacheKey(io, label, buf);
}

/// OpenAI/Codex cache affinity for requests sharing a stable prompt prefix.
/// Root keeps the durable project id. Children use four deterministic lanes:
/// enough sharing for repeated workflow roles, without pushing one key past
/// OpenAI's documented ~15 requests/minute cache-routing guidance during fanout.
pub fn promptPrefixCacheKey(io: Io, label: []const u8, buf: []u8) []const u8 {
    return partitionKey(projectRootId(io), label, buf);
}

/// The root key or a child's role lane on one partition base.
fn partitionKey(base: []const u8, label: []const u8, buf: []u8) []const u8 {
    if (std.mem.eql(u8, label, "main") or sharesParentCache(label)) return std.fmt.bufPrint(buf, "{s}", .{base}) catch base;
    return std.fmt.bufPrint(buf, "{s}-child-{d}", .{ base, std.hash.Wyhash.hash(0, label) & 3 }) catch base;
}

/// Partition for one request. Header (`x-grok-conv-id` / codex `session_id`)
/// and body (`prompt_cache_key`) must be the same value. Role lanes for every
/// provider; codex with an account id partitions by account (ADR 0223).
pub fn requestCacheKey(io: Io, label: []const u8, agent: *const anyopaque, provider: Provider, buf: []u8) []const u8 {
    // 64 bytes hold the base plus a lane suffix, so partitionKey never falls
    // back to returning the stack-local `account`.
    var account: [36]u8 = undefined;
    if (buf.len >= 64) if (cache_affinity.accountRootId(provider.id, provider.account, &account)) |base| return partitionKey(base, label, buf);
    return promptCacheKey(io, label, agent, buf);
}

/// Write the body's `prompt_cache_key`, unless this provider rejects it.
/// Google validates the payload strictly and fails the WHOLE request on an
/// unknown field ("Unknown name \"prompt_cache_key\": Cannot find field."),
/// and it caches implicitly, so it is the one openai-wire provider that
/// carries no partition key. Everything else must still pin one.
pub fn writeRequestCacheKey(s: anytype, io: Io, label: []const u8, agent: *const anyopaque, provider: Provider) !void {
    if (std.mem.eql(u8, provider.id, "google")) return;
    var buf: [96]u8 = undefined;
    try s.objectField("prompt_cache_key");
    try s.write(requestCacheKey(io, label, agent, provider, &buf));
}

var project_id_buf: [36]u8 = undefined;
var project_id_len: usize = 0;
var project_id_lock: std.atomic.Value(bool) = .init(false);

/// Durable per-project id (affinity-seed UUIDv5). A new session in the same
/// repo reuses the bucket the last session wrote, so turn 1 can hit the warm
/// system+tools prefix if the provider still has it. `/resume` restores a
/// saved `prompt_cache_key` (see `restoreProjectRootId`) when cwd would hash
/// differently — idle-park, worktree move. Different models do not share a
/// cache (the server keys by model); they each get this same routing id so
/// *their* later sessions can find *their* prefix.
pub fn projectRootId(io: Io) []const u8 {
    while (project_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
    defer project_id_lock.store(false, .release);
    if (project_id_len == 0) {
        var cwd_buf: [4096]u8 = undefined;
        const n = Io.Dir.cwd().realPathFile(io, ".", &cwd_buf) catch blk: {
            @memcpy(cwd_buf[0..1], ".");
            break :blk 1;
        };
        _ = cache_affinity.projectIdForCwd(io, cwd_buf[0..n], &project_id_buf);
        project_id_len = 36;
    }
    return project_id_buf[0..project_id_len];
}

/// Side-calls that replay the parent history (`/btw`) share the root
/// partition. Workers use a role lane, not the root id. grok-build's recap
/// does the same as `/btw`: same `prompt_cache_key` as the parent.
pub fn sharesParentCache(label: []const u8) bool {
    return std.mem.eql(u8, label, "btw");
}

pub fn projectCacheKey(io: Io, label: []const u8, agent: *const anyopaque, buf: []u8) []const u8 {
    const base = projectRootId(io);
    if (std.mem.eql(u8, label, "main") or sharesParentCache(label)) {
        if (buf.len >= base.len) {
            @memcpy(buf[0..base.len], base);
            return buf[0..base.len];
        }
        return base;
    }
    return std.fmt.bufPrint(buf, "{s}-{x}", .{ base, @intFromPtr(agent) }) catch base;
}

/// /resume adopts the persisted Codex `session_id`. Prompt-cache routing
/// uses `projectRootId` and does not need this. No-op once minted.
pub fn adoptSessionId(id: []const u8) void {
    if (id.len != 36) return;
    while (session_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
    defer session_id_lock.store(false, .release);
    if (session_id_len != 0) return;
    // copyForwards, not @memcpy: a caller may hand back a slice of the
    // static buffer itself (round-trip restore), which aliases dest.
    std.mem.copyForwards(u8, session_id_buf[0..36], id[0..36]);
    session_id_len = 36;
}

/// A replacement worker loading a saved session must take that conversation
/// id even if startup already minted one (idle-park respawn, `graff acp --resume`).
pub fn restoreSessionId(id: []const u8) void {
    if (id.len != 36) return;
    while (session_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
    defer session_id_lock.store(false, .release);
    std.mem.copyForwards(u8, session_id_buf[0..36], id[0..36]);
    session_id_len = 36;
}

/// `/resume` / idle-park: keep the persisted `prompt_cache_key` even if this
/// process already hashed cwd. Length-checked like `restoreSessionId`.
pub fn restoreProjectRootId(id: []const u8) void {
    if (id.len != 36) return;
    while (project_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();
    defer project_id_lock.store(false, .release);
    std.mem.copyForwards(u8, project_id_buf[0..36], id[0..36]);
    project_id_len = 36;
}

pub fn userAgent(provider: Provider) std.http.Client.Request.Headers.Value {
    if (anthropic_oauth.active(provider)) return .omit; // explicit header also reaches HTTP/2
    if (std.mem.eql(u8, provider.id, "kimi")) {
        return .{ .override = root.kimi_user_agent };
    }
    if (std.mem.eql(u8, provider.id, "codegraff")) return .{ .override = client_tags.user_agent }; // the gateway records the build
    return .default;
}

/// xAI Chat Completions: `x-grok-conv-id` (Responses: `prompt_cache_key`).
/// Same value on both — xAI routes that id to one server so the prefix hits.
pub fn wantsGrokConvId(provider_id: []const u8) bool {
    return std.mem.eql(u8, provider_id, "xai");
}

/// ChatGPT/Codex cache routing needs session_id equal to prompt_cache_key;
/// direct API-key OpenAI requests omit the backend-only header.
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);
}

/// Same as `providerHeaders`, with an explicit conversation id for xAI.
/// Null falls back to `projectRootId` (one-off posts: title, compact).
pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, buf: *[12]std.http.Header, conv_id: ?[]const u8) []const std.http.Header {
    var count: usize = 0;
    switch (provider.auth) {
        .x_api_key => {
            buf[count] = .{ .name = "x-api-key", .value = provider.api_key };
            count += 1;
        },
        .bearer => {
            buf[count] = .{ .name = "authorization", .value = bearer };
            count += 1;
        },
        // generativelanguage rejects an API key sent as `Authorization: Bearer`
        // with a 401 asking for an OAuth token — it wants its own header.
        .goog_api_key => {
            buf[count] = .{ .name = "x-goog-api-key", .value = provider.api_key };
            count += 1;
        },
    }
    // SuperGrok user tokens need the grok-build routing header.
    if (std.mem.eql(u8, provider.id, "xai") and provider.source == .login) {
        buf[count] = .{ .name = "X-XAI-Token-Auth", .value = "xai-grok-cli" };
        count += 1;
    }
    if (provider.kind == .anthropic) {
        buf[count] = .{ .name = "anthropic-version", .value = root.anthropic_version };
        count += 1;
        // ADR 0219: the beta that goes with the body's thinking.block_binding,
        // on the Anthropic API and the models that check it (claude_wire).
        const claude = @import("claude_wire.zig");
        const beta: ?[]const u8 = if (anthropic_oauth.active(provider)) anthropic_oauth.beta(provider) else if (claude.isClaudeApi(provider.id, provider.model) and claude.bindsThinking(provider.model)) claude.binding_beta else null;
        if (beta) |value| {
            buf[count] = .{ .name = "anthropic-beta", .value = value };
            count += 1;
        }
    }
    if (anthropic_oauth.active(provider)) count += anthropic_oauth.identityHeaders(buf[count..]);
    if (std.mem.eql(u8, provider.id, "kimi")) {
        const identity = kimi_catalog.identityHeaders(buf[count..]);
        count += identity.len;
    }
    // The Codegraff gateway records each request's device and launching app.
    if (std.mem.eql(u8, provider.id, "codegraff")) count += client_tags.headers(buf[count..]).len;
    // Z.AI docs default the response language this way; omit and some
    // accounts still answer, but the cache examples all send it.
    if (std.mem.eql(u8, provider.id, "zai")) {
        buf[count] = .{ .name = "Accept-Language", .value = "en-US,en" };
        count += 1;
    }
    // Optional app attribution: Vercel AI Gateway and OpenRouter rankings
    // (vercel.com/docs/ai-gateway/ecosystem/app-attribution, openrouter.ai/docs).
    if (std.mem.eql(u8, provider.id, "vercel") or std.mem.eql(u8, provider.id, "openrouter")) {
        buf[count] = .{ .name = "http-referer", .value = "https://codegraff.com" };
        count += 1;
        buf[count] = .{ .name = "x-title", .value = "graff" };
        count += 1;
    }
    // Vercel pins a session to the provider credential that holds its cache
    // and forwards this header upstream. `prompt_cache_key` alone is treated
    // as a shared label that pins the provider but not the credential
    // (vercel.com/docs/ai-gateway/models-and-providers/session-affinity).
    if (std.mem.eql(u8, provider.id, "vercel")) {
        buf[count] = .{ .name = "x-session-affinity", .value = conv_id orelse projectRootId(io) };
        count += 1;
    }
    // OpenRouter marketplace categories. Max 2 per request; only recognized
    // slugs count — unrecognized values are dropped server-side, silently.
    if (std.mem.eql(u8, provider.id, "openrouter")) {
        buf[count] = .{ .name = "x-openrouter-categories", .value = "cli-agent,programming-app" };
        count += 1;
    }
    // These identify the ChatGPT/Codex backend. The official Platform Responses
    // endpoint needs only normal bearer auth and rejects backend-only identity.
    if (std.mem.eql(u8, provider.id, "codex")) {
        buf[count] = .{ .name = "chatgpt-account-id", .value = provider.account };
        count += 1;
        buf[count] = .{ .name = "OpenAI-Beta", .value = "responses=experimental" };
        count += 1;
        buf[count] = .{ .name = "originator", .value = "codex_cli_rs" };
        count += 1;
    }
    // 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;
    }
    if (wantsGrokConvId(provider.id)) {
        const root_id = projectRootId(io);
        buf[count] = .{ .name = "x-grok-conv-id", .value = conv_id orelse root_id };
        count += 1;
        // grok-build always sends session + conv. Session stays the durable
        // project id so a child partition still routes onto the same server
        // family; conv-id / prompt_cache_key isolate the prefix itself.
        buf[count] = .{ .name = "x-grok-session-id", .value = root_id };
        count += 1;
    }
    return buf[0..count];
}

test "session_id is a stable per-process UUIDv4, not a shared constant" {
    const io = std.testing.io;
    const a = sessionId(io);
    try std.testing.expectEqual(@as(usize, 36), a.len);
    // Codex session_id must not collapse to the old shared constant.
    try std.testing.expect(!std.mem.eql(u8, a, "00000000-0000-0000-0000-000000000001"));
    for ([_]usize{ 8, 13, 18, 23 }) |i| try std.testing.expectEqual(@as(u8, '-'), a[i]);
    try std.testing.expectEqual(@as(u8, '4'), a[14]); // version nibble
    try std.testing.expect(a[19] == '8' or a[19] == '9' or a[19] == 'a' or a[19] == 'b'); // variant
    for (a, 0..) |c, i| {
        if (i == 8 or i == 13 or i == 18 or i == 23) continue;
        try std.testing.expect((c >= '0' and c <= '9') or (c >= 'a' and c <= 'f'));
    }
    try std.testing.expectEqualStrings(a, sessionId(io));

    var buf: [12]std.http.Header = undefined;
    const p: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-5.6", .context = 272_000, .account = "acct" };
    const headers = providerHeaders(io, p, "Bearer k", &buf);
    try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "session_id") orelse return error.SessionIdHeaderMissing);
    try std.testing.expect(!std.mem.eql(u8, a, headerValue(headers, "session_id").?));
}

test "official OpenAI Responses uses Platform headers, not ChatGPT backend identity" {
    const io = std.testing.io;
    var buf: [12]std.http.Header = undefined;
    const p: Provider = .{ .id = "openai", .kind = .responses, .auth = .bearer, .url = "https://api.openai.com/v1/responses", .api_key = "k", .model = "gpt-5.6", .context = 272_000 };
    const headers = providerHeaders(io, p, "Bearer k", &buf);
    try std.testing.expectEqual(@as(usize, 1), headers.len);
    try std.testing.expectEqualStrings("authorization", headers[0].name);
    try std.testing.expectEqualStrings("Bearer k", headers[0].value);
}

test "Z.AI chat sends Accept-Language and no grok conv headers" {
    const io = std.testing.io;
    var buf: [12]std.http.Header = undefined;
    const p: Provider = .{ .id = "zai", .kind = .openai, .auth = .bearer, .url = "https://api.z.ai/api/paas/v4/chat/completions", .api_key = "k", .model = "glm-5.3", .context = 1_000_000 };
    const headers = providerHeaders(io, p, "Bearer k", &buf);
    var saw_lang = false;
    for (headers) |h| {
        try std.testing.expect(!std.mem.eql(u8, h.name, "x-grok-conv-id"));
        if (std.mem.eql(u8, h.name, "Accept-Language")) {
            saw_lang = true;
            try std.testing.expectEqualStrings("en-US,en", h.value);
        }
    }
    try std.testing.expect(saw_lang);
}

test "projectRootId is minted from the affinity seed, not a second hash" {
    // Locks the wire: a future change that hashes cwd again inside
    // projectRootId would still pass the seed-string tests in
    // cache_affinity.zig. Offline — no provider.
    const io = std.testing.io;
    var cwd_buf: [4096]u8 = undefined;
    const n = Io.Dir.cwd().realPathFile(io, ".", &cwd_buf) catch blk: {
        cwd_buf[0] = '.';
        break :blk 1;
    };
    var want: [36]u8 = undefined;
    try std.testing.expectEqualStrings(projectRootId(io), cache_affinity.projectIdForCwd(io, cwd_buf[0..n], &want));
}

test "x-grok-conv-id with no explicit conv still uses the project root id" {
    const io = std.testing.io;
    const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };
    var buf: [12]std.http.Header = undefined;
    const headers = providerHeadersWithConv(io, xai, "Bearer k", &buf, null);
    try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "x-grok-conv-id") orelse return error.MissingGrokConvId);
    try std.testing.expectEqualStrings(projectRootId(io), headerValue(headers, "x-grok-session-id") orelse return error.MissingGrokSessionId);
}

test "adoptSessionId validates length and never overwrites a minted id" {
    const io = std.testing.io;
    const before = sessionId(io); // minted by whichever test ran first
    adoptSessionId("too-short");
    adoptSessionId("00000000-0000-4000-8000-000000000000");
    try std.testing.expectEqualStrings(before, sessionId(io));
}

test "restoreSessionId overwrites a minted id so a parked worker keeps cache affinity" {
    const io = std.testing.io;
    _ = sessionId(io);
    restoreSessionId("00000000-0000-4000-8000-000000000001");
    try std.testing.expectEqualStrings("00000000-0000-4000-8000-000000000001", sessionId(io));
    restoreSessionId("short");
    try std.testing.expectEqualStrings("00000000-0000-4000-8000-000000000001", sessionId(io));
}

test "restoreProjectRootId overwrites a minted id so resume keeps the prompt-cache partition" {
    const io = std.testing.io;
    // A copy: projectRootId is a view of the buffer the restore overwrites.
    var before: [36]u8 = undefined;
    @memcpy(&before, projectRootId(io));
    restoreProjectRootId("22222222-2222-4222-8222-222222222222");
    try std.testing.expectEqualStrings("22222222-2222-4222-8222-222222222222", projectRootId(io));
    restoreProjectRootId("short");
    try std.testing.expectEqualStrings("22222222-2222-4222-8222-222222222222", projectRootId(io));
    restoreProjectRootId(&before);
    try std.testing.expectEqualStrings(&before, projectRootId(io));
}

fn headerValue(headers: []const std.http.Header, name: []const u8) ?[]const u8 {
    for (headers) |h| if (std.mem.eql(u8, h.name, name)) return h.value;
    return null;
}

test "xAI Chat Completions headers carry a stable per-conversation x-grok-conv-id" {
    const io = std.testing.io;
    var root_ptr: usize = 1;
    var child_ptr: usize = 2;
    const root_agent: *const anyopaque = @ptrCast(&root_ptr);
    const child_agent: *const anyopaque = @ptrCast(&child_ptr);
    var root_buf: [96]u8 = undefined;
    var child_buf: [96]u8 = undefined;
    const root_id = promptCacheKey(io, "main", root_agent, &root_buf);
    const again_buf = promptCacheKey(io, "main", root_agent, root_buf[0..]);
    _ = again_buf;
    var root_buf2: [96]u8 = undefined;
    try std.testing.expectEqualStrings(root_id, promptCacheKey(io, "main", root_agent, &root_buf2));
    const child_id = promptCacheKey(io, "sub", child_agent, &child_buf);
    try std.testing.expect(!std.mem.eql(u8, root_id, child_id));

    const xai: Provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 500_000 };
    var buf: [12]std.http.Header = undefined;
    const first = providerHeadersWithConv(io, xai, "Bearer k", &buf, root_id);
    try std.testing.expectEqualStrings(root_id, headerValue(first, "x-grok-conv-id") orelse return error.MissingGrokConvId);
    try std.testing.expectEqualStrings(root_id, headerValue(first, "x-grok-session-id") orelse return error.MissingGrokSessionId);
    var buf2: [12]std.http.Header = undefined;
    const second = providerHeadersWithConv(io, xai, "Bearer k", &buf2, root_id);
    try std.testing.expectEqualStrings(root_id, headerValue(second, "x-grok-conv-id").?);
    try std.testing.expectEqualStrings(root_id, headerValue(second, "x-grok-session-id").?);
    var buf3: [12]std.http.Header = undefined;
    const child_headers = providerHeadersWithConv(io, xai, "Bearer k", &buf3, child_id);
    try std.testing.expectEqualStrings(child_id, headerValue(child_headers, "x-grok-conv-id").?);
    try std.testing.expectEqualStrings(root_id, headerValue(child_headers, "x-grok-session-id").?);

    const anth: Provider = .{ .id = "anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "", .api_key = "k", .model = "claude", .context = 200_000 };
    var abuf: [12]std.http.Header = undefined;
    const ah = providerHeadersWithConv(io, anth, "", &abuf, root_id);
    try std.testing.expect(headerValue(ah, "x-grok-conv-id") == null);
    try std.testing.expect(headerValue(ah, "x-grok-session-id") == null);
}

test "xai login tokens send X-XAI-Token-Auth; API keys do not" {
    const io = std.testing.io;
    var buf: [12]std.http.Header = undefined;
    const login: Provider = .{
        .id = "xai",
        .kind = .openai,
        .auth = .bearer,
        .url = "",
        .api_key = "oauth-tok",
        .model = "grok-4.3",
        .context = 256_000,
        .source = .login,
    };
    const login_headers = providerHeaders(io, login, "Bearer oauth-tok", &buf);
    var saw_token_auth = false;
    for (login_headers) |h| {
        if (std.mem.eql(u8, h.name, "X-XAI-Token-Auth")) {
            try std.testing.expectEqualStrings("xai-grok-cli", h.value);
            saw_token_auth = true;
        }
    }
    try std.testing.expect(saw_token_auth);

    const env_key: Provider = .{
        .id = "xai",
        .kind = .openai,
        .auth = .bearer,
        .url = "",
        .api_key = "xai-api-key",
        .model = "grok-4.3",
        .context = 256_000,
        .source = .environment,
    };
    const env_headers = providerHeaders(io, env_key, "Bearer xai-api-key", &buf);
    for (env_headers) |h| {
        try std.testing.expect(!std.mem.eql(u8, h.name, "X-XAI-Token-Auth"));
    }
}

test "Kimi chat sends graff identity and kimi-code device headers (#617)" {
    const io = std.testing.io;
    var buf: [12]std.http.Header = undefined;
    const p: Provider = .{ .id = "kimi", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "k3", .context = 256_000 };
    const headers = providerHeaders(io, p, "Bearer k", &buf);
    try std.testing.expectEqualStrings("graff", headerValue(headers, "X-Msh-Platform").?);
    for (headers) |h| try std.testing.expect(std.mem.indexOf(u8, h.value, "kimi_code_cli") == null and std.mem.indexOf(u8, h.value, "kimi-code-cli") == null);
    try std.testing.expect(headerValue(headers, "X-Msh-Device-Name").?.len > 0);
    try std.testing.expect(!std.mem.eql(u8, headerValue(headers, "X-Msh-Device-Name").?, "unknown"));
    const model = headerValue(headers, "X-Msh-Device-Model").?;
    const prefix = switch (@import("builtin").os.tag) {
        .windows => "Windows ",
        .macos => "macOS ",
        .linux => "Linux ",
        else => "",
    };
    if (prefix.len != 0) try std.testing.expect(std.mem.startsWith(u8, model, prefix));
    try std.testing.expect(!std.mem.eql(u8, headerValue(headers, "X-Msh-Os-Version").?, @tagName(@import("builtin").os.tag)));
}

test "the thinking-binding beta rides only 5.5-era Claude models on the Anthropic API (ADR 0219)" {
    const io = std.testing.io;
    var buf: [12]std.http.Header = undefined;
    const beta = struct {
        fn of(headers: []const std.http.Header) ?[]const u8 {
            for (headers) |h| if (std.mem.eql(u8, h.name, "anthropic-beta")) return h.value;
            return null;
        }
    }.of;
    var p: Provider = .{ .id = "anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "", .api_key = "k", .model = "claude-opus-5-5", .context = 1_000_000 };
    try std.testing.expectEqualStrings("thinking-binding-controls-2026-08-01", beta(providerHeaders(io, p, "", &buf)).?);
    p.model = "claude-opus-5";
    try std.testing.expect(beta(providerHeaders(io, p, "", &buf)) == null);
    // An Anthropic-format provider that is not Anthropic never sees it.
    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
}