graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(zai): preserve thinking so implicit prefix cache survives tool turns
Cursor Agenteb3e9b0339a5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c56276e3..3313c48c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,7 +16,10 @@ current is part of cutting a release.`/api/paas/v4/models`, `thinking.type=enabled`, and `/effort` remappedonto `low`/`high`/`max` (graff `medium` → `high`). Pay-go stays on`api.z.ai/api/paas/v4`; GLM Coding Plan keys use `ZAI_CODING=1` or- `GRAFF_ZAI_URL`. This is the GLM chat API, not image/video.+ `GRAFF_ZAI_URL`. Prompt-cache max: implicit prefix reuse+ (`cached_tokens`) plus `thinking.clear_thinking=false` so replayed+ `reasoning_content` stays in that prefix. This is the GLM chat API,+ not image/video.- Cerebras Inference is a built-in OpenAI-chat provider (`CEREBRAS_API_KEY`,`graff key set cerebras …`, default `gpt-oss-120b`). Live `/v1/models`overlay; this is `api.cerebras.ai`, not the WSE CSL SDK.diff --git a/docs/releases/v0.0.268.md b/docs/releases/v0.0.268.mdindex 410ccda2..0cb48198 100644--- a/docs/releases/v0.0.268.md+++ b/docs/releases/v0.0.268.md@@ -57,8 +57,11 @@ ADR 0010 (wait-for-exit already in 0.0.267), the model should not poll`https://api.z.ai/api/paas/v4/models`. GLM-5.3 always reasons: the wiresends `thinking.type=enabled` and maps `/effort` onto `low` / `high` /`max` (graff's default `medium` becomes `high` so the request is not-rejected). Pay-as-you-go stays on `/api/paas/v4/`. A GLM Coding Plan key-needs `ZAI_CODING=1` or `GRAFF_ZAI_URL` pointing at+rejected). Cache is implicit on an identical prefix+(`usage.prompt_tokens_details.cached_tokens`); the wire also sets+`thinking.clear_thinking=false` so replayed `reasoning_content` stays+in that prefix across tool turns. Pay-as-you-go stays on `/api/paas/v4/`.+A GLM Coding Plan key needs `ZAI_CODING=1` or `GRAFF_ZAI_URL` pointing at`https://api.z.ai/api/coding/paas/v4/chat/completions`. Image and videomodels are not chat seats.diff --git a/src/http_headers.zig b/src/http_headers.zigindex 9443358f..97bc086e 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -179,6 +179,12 @@ pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, bconst identity = kimi_catalog.identityHeaders(buf[count..]);count += identity.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;+ }// 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")) {@@ -241,6 +247,22 @@ test "official OpenAI Responses uses Platform headers, not ChatGPT backend identtry 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 "project and prefix cache keys preserve conversation and sharing boundaries" {var fake: usize = 0;var sibling_fake: usize = 1;diff --git a/src/prompt_cache_hud.zig b/src/prompt_cache_hud.zigindex d8413628..b154abf3 100644--- a/src/prompt_cache_hud.zig+++ b/src/prompt_cache_hud.zig@@ -216,6 +216,7 @@ pub fn render(w: *Io.Writer) !void {\\ leave skill bodies and file: paths out of the prefix (already the default)\\ append-only messages official xAI prefix; edit/remove/reorder is a miss\\ replay reasoning Chat reasoning_content · Responses encrypted_content+ \\ Z.AI implicit prefix (cached_tokens); thinking.clear_thinking=false\\ /btw parent tools + system + cache key; note is the user message\\);@@ -300,6 +301,7 @@ test "render stays content-free and names remaining levers" {try std.testing.expect(contains(text, "prompt cache"));try std.testing.expect(contains(text, "prefix"));try std.testing.expect(contains(text, "GRAFF_STABLE_CATALOG"));+ try std.testing.expect(contains(text, "clear_thinking"));try std.testing.expect(contains(text, "/goal"));try std.testing.expect(contains(text, "x-grok-conv-id"));try std.testing.expect(contains(text, "append-only"));diff --git a/src/zai_wire.zig b/src/zai_wire.zigindex 1b705d06..5c1a1f9f 100644--- a/src/zai_wire.zig+++ b/src/zai_wire.zig@@ -3,6 +3,11 @@//! GLM-5.3 always reasons: `thinking.type` is only `enabled` (disabled fails)//! and `reasoning_effort` is `low` | `high` | `max` (docs default `max`).//! Graff's default is `medium`, which the API rejects — map it here.+//!+//! Context cache is implicit on identical prefixes+//! (`usage.prompt_tokens_details.cached_tokens`). Preserved thinking+//! (`clear_thinking: false`) keeps replayed `reasoning_content` in that+//! prefix so a tool turn does not drop the hit (docs.z.ai thinking-mode).const std = @import("std");@@ -22,7 +27,7 @@ pub fn writeChatExtras(s: *std.json.Stringify, provider_id: []const u8, send_effconst is_zai = std.mem.eql(u8, provider_id, "zai");if (is_zai) {try s.objectField("thinking");- try s.print("{s}", .{"{\"type\":\"enabled\"}"});+ try s.print("{s}", .{"{\"type\":\"enabled\",\"clear_thinking\":false}"});}if (!std.mem.eql(u8, provider_id, "kimi") and send_effort) {try s.objectField("reasoning_effort");@@ -72,7 +77,8 @@ test "Z.AI chat body sends thinking.enabled and remaps medium to high" {};const body = try agent.buildBody(null, false, true, true);defer std.testing.allocator.free(body);- try std.testing.expect(std.mem.indexOf(u8, body, "\"thinking\":{\"type\":\"enabled\"}") != null);+ try std.testing.expect(std.mem.indexOf(u8, body, "\"thinking\":{\"type\":\"enabled\",\"clear_thinking\":false}") != null);+ try std.testing.expect(std.mem.indexOf(u8, body, "\"prompt_cache_key\":\"") != null);try std.testing.expect(std.mem.indexOf(u8, body, "\"reasoning_effort\":\"high\"") != null);try std.testing.expect(std.mem.indexOf(u8, body, "\"reasoning_effort\":\"medium\"") == null);