graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Claude prompt cache: compaction forks, turn-boundary TTL, 1-hour pricing, effort as shown
justrachf1e37f51cfc2
diff --git a/docs/adr/0219-claude-5-5-era-request-contract.md b/docs/adr/0219-claude-5-5-era-request-contract.mdindex cdf2698e..4b1fd4a9 100644--- a/docs/adr/0219-claude-5-5-era-request-contract.md+++ b/docs/adr/0219-claude-5-5-era-request-contract.md@@ -44,7 +44,8 @@ OpenAI-shaped errors, so every Claude error failed the request.later, Sonnet 4.6 and later, Fable, and Mythos. It shares one `output_config`object with a structured-output `format` when there is one. graff's default(`medium`) is left off, so each model keeps its own default. `ultra` maps to- `max`.+ `max`. (Amended by ADR 0220: 5.5-era models get `medium` as shown, and+ titles and recaps run at `low`.)- **max_tokens.** 64K on models with a 128K ceiling, and the full 128K at`xhigh`, `max` and `ultra`, as Anthropic advises for agentic work. Othermodels keep 16K. graff always streams, which responses this size need.@@ -68,7 +69,8 @@ OpenAI-shaped errors, so every Claude error failed the request.- Keeping thinking instead of dropping it, by freezing `system` and `tools`and using inline tool additions and mid-conversation system messages.- Showing text written between tool calls via the `updates` display.- - Pricing 1-hour cache writes at 2× (they are counted at 1.25×).+ - Pricing 1-hour cache writes at 2× (they are counted at 1.25×). Done in+ ADR 0220.- Tool search with deferred loading.- Task budgets.- An elapsed-time clock.diff --git a/docs/adr/0220-claude-prompt-cache-forks-and-pricing.md b/docs/adr/0220-claude-prompt-cache-forks-and-pricing.mdnew file mode 100644index 00000000..51c85c7b--- /dev/null+++ b/docs/adr/0220-claude-prompt-cache-forks-and-pricing.md@@ -0,0 +1,84 @@+# 0220. Claude prompt cache: compaction forks, turn-boundary TTL, 1-hour pricing, effort as shown++Status: accepted 2026-09-30++## Context++On the Anthropic API, graff marks two cache breakpoints: the system block+(which caches tools and system together) and the last block of the last+message. Hot context arrives as appended messages, and nothing edits history+between compactions. An audit against the prompt-caching contract found+these gaps:++- **Compaction paid full price for history the cache already held.** The+ note to self (#391) and the handoff summary each re-send the whole history.+ Neither kept the conversation's prefix: both dropped the tools, the note+ swapped in its own system prompt, and the summary cut old tool outputs to+ stubs. Each request re-read the history at the input price and wrote it+ back at 1.25x, and no later request ever read that entry. With a 1M window+ and compaction at 80%, that is two full-price passes over roughly 800K+ tokens. The note's system-prompt refresh also ran before the summary, so a+ fork could not have matched the prefix anyway.+- **An idle turn boundary bought the 1-hour TTL.** #1320 asks for the hour+ when the previous request began 3+ minutes ago. After a user idles past 5+ minutes the prefix has already expired, so the turn's first request+ rewrites all of it, and at 1 hour it paid 2x instead of 1.25x. The model is+ usually fast within the turn, so no later request needs that entry.+- **1-hour writes were priced at 1.25x.** Anthropic bills them at 2x.+ `usage.cache_creation` gives the split.+- **The effort graff showed was not the one that ran.** graff left effort off+ at its default (`medium`). On Sonnet 5.5 and Fable 5.1 the API's default is+ `high`. The Sonnet rung also runs titles and recaps, so one-line tasks ran+ at `high`.+- **One breakpoint on the messages.** A breakpoint finds an earlier entry by+ walking back at most 20 blocks. On 5.5-era models, one assistant turn that+ interleaves thinking with many parallel tool calls can add more than that.++## Decision++- **Compaction forks the conversation when the cache holds it**+ (`cache_fork.zig`). A fork is used when all of these hold:+ - The provider is the Anthropic API.+ - This agent's last request began inside the TTL it asked for.+ - No overflow is being recovered.+ - The history does not end on an unanswered tool call.+ - The previous summary attempt was usable.+ - The whole history, plus its reply, fits the window.++ In a fork, the note and the summary send the conversation's own tools,+ system prompt, thinking, effort and speed. Every message goes verbatim, with+ the instruction appended as the last user message. The note's persona leads+ its instruction. The summary sees the whole history, and nothing is trimmed.+ The note's system-prompt refresh runs after the summary.+- **Otherwise the old shape runs without breakpoints.** A compaction request+ that shares no prefix gets no `cache_control`. This covers Anthropic and+ Anthropic models through OpenRouter. It is sent uncached (1x) instead of+ written (1.25x).+- **A turn-opening request keeps the TTL of the last gap inside a turn.**+ The #1320 rule is unchanged within a turn.+- **1-hour writes are billed at 2x** on metered seats. A price row that+ states its own write multiplier is billed as the row says.+- **5.5-era models get graff's effort as shown**, `medium` included.+ Titles and recaps run at `low`. Older models keep their own default at+ graff's default, so their sessions do not move.+- **A second breakpoint** marks the user message before the last one. That is+ where the previous request wrote its entry. Breakpoints cost nothing, and+ a write is billed only for the tokens past the hit.++## Consequences++- A compaction on a warm cache reads the history instead of rewriting it:+ roughly 0.05x of input on Opus 5.5 and 0.1x on Sonnet 5.5, instead of+ 2.25x for the two passes combined. A fork that fails to produce a summary+ falls back to the old shape on the next attempt.+- The summarizer now sees full tool outputs and the recent suffix. That+ suffix is also kept verbatim after compaction.+- Sessions on Sonnet 5.5 and Fable 5.1 at graff's default now run at+ `medium`, which is lower than before.+- Not done yet:+ - Keeping the cache across idle gaps and long waits, by writing the hour at+ a turn boundary, or with `max_tokens: 0` refreshes.+ - Mid-session system-prompt changes (goal, recorded constraints, `/strict`,+ `/ultracode`) as mid-conversation system messages.+ - Tool changes as inline tool additions.+ - A lower compaction point for 1M windows.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex 272f4e1a..15b3eca8 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -246,6 +246,7 @@ record only when you need the evidence or the edge cases.| [0217](0217-follow-ups-supersede-the-streaming-reply.md) | A plain follow-up typed while the root reply streams cuts that stream, drops the uncommitted partial and rebuilds the request with the follow-up (not for compaction, children, started async tools, or GPT-6 WebSocket steer). TUI Enter hands plain text to the running turn; `/btw`, commands and images still wait; ACP is unchanged. || [0218](0218-background-jobs-survive-a-crash.md) | A finite root job started in the background writes its output to `.graff/job-output/<id>/` with an exit receipt from its own shell, so it outlives a crashed graff. Loading the session re-attaches this session's unreported jobs under their old ids, reports what ended while graff was down, and sweeps other sessions' stale captures. Foreground commands, servers, subagent jobs and Windows keep pipes. || [0219](0219-claude-5-5-era-request-contract.md) | Claude Opus 5.5, Sonnet 5.5 and Fable 5.1 are never forced to a tool, get `drop_block` thinking binding (with its beta header), take `/effort` as `output_config.effort` (left off at graff's default), and get 64K/128K `max_tokens`. Claude errors take the same retry-without ladder as other providers; a refusal ends the turn outside history. Default model `claude-opus-5-5`, ladder opus-5-5 → sonnet-5-5. |+| [0220](0220-claude-prompt-cache-forks-and-pricing.md) | On the Anthropic API, compaction's note and summary fork the conversation while its cache is warm. They use the same tools, system prompt and history, with the instruction appended, so they read the history instead of rewriting it. Otherwise the old shape runs without breakpoints. A turn-opening request keeps the TTL of the last gap inside a turn. 1-hour writes bill at 2x. 5.5-era models get graff's effort as shown; titles and recaps run at low. A second breakpoint marks where the previous request wrote. |## When to write onediff --git a/src/agent_compact.zig b/src/agent_compact.zigindex bf855efb..3cf4bea3 100644--- a/src/agent_compact.zig+++ b/src/agent_compact.zig@@ -117,6 +117,8 @@ pub fn compactPrelude(self: *Agent) ?usize {pub fn compact(self: *Agent) anyerror!usize {// An opaque item anywhere in history needs the server, never a local summary.if (@import("compaction_window.zig").latestBlob(self.messages.items) != null) return error.ServerCompactionRequired;+ const fork = @import("cache_fork.zig").begin(self); // ADR 0220: read the cached history, not rewrite it+ defer @import("cache_fork.zig").end();const pending_tokens = compactPrelude(self) orelse {if (!main_mod.json_mode) try self.say("nothing to compact\n", .{});return 0;@@ -127,7 +129,8 @@ pub fn compact(self: *Agent) anyerror!usize {// #391: the agent writes its own handoff BEFORE the summarizer rewrites the// history it describes. Gated, budgeted, best-effort: every refusal is a// named skip, so everything below runs unconditionally.- _ = compact_note_glue.maybeWrite(self);+ const note = compact_note_glue.maybeWrite(self);+ defer if (note == .fire) compact_note_glue.publish(self); // after the summary, which forks the old system prompt// #163: reclaim room BEFORE the summarization request so it fits under the// model's input cap. On codex/gpt-5.x an over-cap request fails to WRITE// (WriteFailed) rather than returning a clean overflow, so compaction could@@ -170,7 +173,7 @@ pub fn compact(self: *Agent) anyerror!usize {const recent_messages = live_messages.items[recent_start..];var summary_messages = std.json.Array.init(compact_arena);try summary_messages.ensureTotalCapacity(recent_start);- for (live_messages.items[0..recent_start]) |item| {+ for (live_messages.items[0..if (fork) live_messages.items.len else recent_start]) |item| {if (peer_context.isPeerInject(item)) continue; // ADR 0004: do not ask the summarizer to hoard the roomtry summary_messages.append(try cloneJsonValue(compact_arena, item));}@@ -195,7 +198,7 @@ pub fn compact(self: *Agent) anyerror!usize {// turn context once this synthetic user request is appended. Pruning first// left precisely that large encrypted blob in the full summary resend._ = dropPriorTurnReasoning(self);- _ = trimOldestToolOutputsAlloc(self, compact_arena);+ if (!fork) _ = trimOldestToolOutputsAlloc(self, compact_arena);var progress = @import("compact_status.zig").begin(self);defer progress.end(self);@@ -211,7 +214,7 @@ pub fn compact(self: *Agent) anyerror!usize {defer self.stream_quiet = was_quiet;defer self.compaction_request = was_compaction_request;defer self.message_mutation_arena = was_message_mutation_arena;- const root = try self.request(null);+ const root = try self.request(@import("cache_fork.zig").tools(self, fork));progress.end(self);// Any complete transport response proves the previous opaque failure was// transient/non-wedging, even when its summary text is empty or truncated.diff --git a/src/agent_context.zig b/src/agent_context.zigindex dbed42f4..10403081 100644--- a/src/agent_context.zig+++ b/src/agent_context.zig@@ -136,7 +136,7 @@ pub fn recordUsage(self: *Agent, root: std.json.ObjectMap, req_body_len: usize)const cache = usageInt(u, "cache_read_input_tokens");const cache_write = usageInt(u, "cache_creation_input_tokens");if (cache > 0) self.last_cache_read = @intCast(cache);- self.recordCost(usageInt(u, "input_tokens"), cache, cache_write, usageInt(u, "output_tokens"));+ @import("cache_billing.zig").record(self, u, usageInt(u, "input_tokens"), cache, cache_write, usageInt(u, "output_tokens"));},.openai => {const total = usageInt(u, "total_tokens");diff --git a/src/agent_request_body.zig b/src/agent_request_body.zigindex 08b9a253..92ffab49 100644--- a/src/agent_request_body.zig+++ b/src/agent_request_body.zig@@ -79,6 +79,7 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:// Prompt caching (Anthropic): a block-level cache_control breakpoint// on the system block caches the whole stable prefix (system+tools);// other anthropic-format providers (minimax) get a plain string.+ // No breakpoint on a compaction request no later request shares.try s.objectField("system");const sys = try @import("agent_request_body_responses.zig").schemaAwarePrompt(self);const cc = @import("cache_ttl.zig").control(self); // #1320: 1h when calls outlive 5m@@ -89,8 +90,10 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:try s.write("text");try s.objectField("text");try s.write(sys);- try s.objectField("cache_control");- try s.print("{s}", .{cc});+ if (cc) |c| {+ try s.objectField("cache_control");+ try s.print("{s}", .{c});+ }try s.endObject();try s.endArray();} else {@@ -98,7 +101,7 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:}// Effort rides output_config, in one object with a structured-output// format when there is one.- const effort: ?[]const u8 = if (is_claude and !self.effort_rejected and claude.takesEffort(self.provider.model)) claude.effortWire(self.reasoning) else null;+ const effort: ?[]const u8 = if (is_claude and !self.effort_rejected and claude.takesEffort(self.provider.model)) claude.effortFor(self.provider.model, self.reasoning, self.call_kind) else null;var effort_written = false;if (tools) |t| {try s.objectField("tools");diff --git a/src/agent_request_body_claude_tests.zig b/src/agent_request_body_claude_tests.zigindex 1ba5447c..d6ca4fbf 100644--- a/src/agent_request_body_claude_tests.zig+++ b/src/agent_request_body_claude_tests.zig@@ -53,10 +53,22 @@ test "effort rides output_config on Claude, in one object with a structured-outpdefer arena_state.deinit();var agent = try claudeAgent(arena_state.allocator(), "claude-sonnet-5-5");- // graff's default leaves the model's own effort in place.+ // ADR 0220: graff's default is sent as shown; Sonnet 5.5's own is high.const plain = try agent.buildBody(bash_tools, false, true, true);defer std.testing.allocator.free(plain);- try std.testing.expect(!has(plain, "\"output_config\""));+ try std.testing.expect(has(plain, "\"output_config\":{\"effort\":\"medium\"}"));+ // A title is one line: low effort, whatever the session's level.+ agent.call_kind = .title;+ const title = try agent.buildBody(null, false, true, true);+ defer std.testing.allocator.free(title);+ try std.testing.expect(has(title, "\"output_config\":{\"effort\":\"low\"}"));+ agent.call_kind = .root;+ // An older model keeps its own default at graff's default.+ agent.provider.model = "claude-opus-4-8";+ const older = try agent.buildBody(bash_tools, false, true, true);+ defer std.testing.allocator.free(older);+ try std.testing.expect(!has(older, "\"output_config\""));+ agent.provider.model = "claude-sonnet-5-5";agent.reasoning = .high;const high = try agent.buildBody(bash_tools, false, true, true);diff --git a/src/cache_billing.zig b/src/cache_billing.zignew file mode 100644index 00000000..50a799a4--- /dev/null+++ b/src/cache_billing.zig@@ -0,0 +1,48 @@+//! ADR 0220: on the Anthropic API a 1-hour cache write costs 2x input and a+//! 5-minute one 1.25x. The tally prices every write at 1.25x+//! (pricing_cache.usdForUsage), so a session that asked for the 1-hour TTL+//! (cache_ttl.zig, #1320) reported less than it was billed. The response's+//! usage.cache_creation splits the written tokens by TTL; the 1-hour part's+//! difference is added to the session's cost here.++const std = @import("std");+const Agent = @import("agent.zig").Agent;+const pricing = @import("pricing.zig");+const pricing_cache = @import("pricing_cache.zig");+const billing = @import("billing.zig");++/// Tokens this response wrote to the 1-hour cache.+pub fn hourTokens(usage: std.json.ObjectMap) i64 {+ const split = usage.get("cache_creation") orelse return 0;+ if (split != .object) return 0;+ const v = split.object.get("ephemeral_1h_input_tokens") orelse return 0;+ return if (v == .integer and v.integer > 0) v.integer else 0;+}++/// One Anthropic response's usage into the session tally, 1-hour writes at 2x.+pub fn record(self: *Agent, usage: std.json.ObjectMap, ordinary: i64, cache_read: i64, cache_write: i64, out: i64) void {+ self.recordCost(ordinary, cache_read, cache_write, out);+ const hour = hourTokens(usage);+ if (hour == 0 or billing.forProvider(self.provider) != .priced) return;+ const p = pricing.priceForProvider(self.provider.id, self.provider.model) orelse return;+ const prompt: u64 = @intCast(@max(ordinary, 0) +| @max(cache_read, 0) +| @max(cache_write, 0));+ const extra = pricing_cache.hourWriteExtraUsd(p, self.provider.model, prompt, hour);+ if (extra == 0) return;+ pricing.g_cost.mutex.lockUncancelable(self.io);+ defer pricing.g_cost.mutex.unlock(self.io);+ pricing.g_cost.usd += extra;+}++test "ADR 0220: usage.cache_creation names the 1-hour writes" {+ const gpa = std.testing.allocator;+ const body =+ \\{"input_tokens":10,"cache_creation_input_tokens":300,"cache_read_input_tokens":5000,+ \\ "cache_creation":{"ephemeral_5m_input_tokens":100,"ephemeral_1h_input_tokens":200}}+ ;+ var parsed = try std.json.parseFromSlice(std.json.Value, gpa, body, .{});+ defer parsed.deinit();+ try std.testing.expectEqual(@as(i64, 200), hourTokens(parsed.value.object));+ var bare = try std.json.parseFromSlice(std.json.Value, gpa, "{\"cache_creation_input_tokens\":300}", .{});+ defer bare.deinit();+ try std.testing.expectEqual(@as(i64, 0), hourTokens(bare.value.object));+}diff --git a/src/cache_fork.zig b/src/cache_fork.zignew file mode 100644index 00000000..066db74f--- /dev/null+++ b/src/cache_fork.zig@@ -0,0 +1,182 @@+//! Compaction forks that keep the conversation's cached prefix (ADR 0220).+//!+//! compact() makes up to two model calls over the whole history: the note to+//! self (#391) and the handoff summary. Both used to leave the tools off, the+//! note swapped in its own system prompt, and the summary cut old tool+//! outputs to stubs. On the Anthropic API neither shared a byte of prefix with+//! the conversation the cache held, so each re-read the history at the full+//! input price and wrote it back at the 1.25x cache-write price, for a prefix+//! no later request would read.+//!+//! A fork that sends the same tools, the same system prompt and every message+//! verbatim, its instruction appended as the last user message, reads that+//! history from the cache instead (0.05x of input on Claude Opus 5.5, 0.1x on+//! Sonnet 5.5). It runs when the cache can still hold the prefix and the+//! request fits: this agent's last request began inside the TTL it asked for+//! (5 minutes, or the hour cache_ttl.zig picks for slow calls), no overflow is+//! being recovered, the history does not end on an unanswered tool call, and+//! the last summary attempt did not come back unusable.+//! Otherwise the old shape runs without breakpoints (cache_ttl.control): a+//! prefix nothing will read again is cheaper sent uncached (1x) than written+//! (1.25x).++const std = @import("std");+const Value = std.json.Value;+const Agent = @import("agent.zig").Agent;+const claude = @import("claude_wire.zig");++/// A 5-minute entry, less the time the fork's own request takes to arrive.+const warm_ms: i64 = 4 * 60 * 1000 + 30 * 1000;+/// The same margin under a 1-hour entry (cache_ttl.zig, #1320).+const warm_long_ms: i64 = 55 * 60 * 1000;+/// Room for the instruction the fork appends.+const instruction_tokens: u64 = 8_000;++/// This thread's compaction requests fork the conversation. compact() and the+/// note it writes run on the thread that made the conversation's requests.+threadlocal var active: bool = false;++pub fn shares() bool {+ return active;+}++/// Decide for this compaction and mark the thread; pair with end().+pub fn begin(self: *Agent) bool {+ active = warm(self);+ return active;+}++pub fn end() void {+ active = false;+}++/// Whether the cache still holds this conversation's prefix and a fork of the+/// whole history fits the window.+pub fn warm(self: *Agent) bool {+ if (self.provider.kind != .anthropic or !std.mem.eql(u8, self.provider.id, "anthropic")) return false;+ if (self.last_request_context_overflow or self.compact_summary_failures > 0) return false;+ const started = self.request_started orelse return false;+ const window = if (@import("cache_ttl.zig").asksLong(self.provider.id, self.provider.model)) warm_long_ms else warm_ms;+ if (started.untilNow(self.io, .awake).toMilliseconds() >= window) return false;+ if (endsOnToolCall(self.messages.items)) return false;+ const out = claude.maxTokens(self.provider.model, self.reasoning, @import("main.zig").max_tokens);+ return self.effectiveContextTokens() +| out +| instruction_tokens <= self.provider.context;+}++/// The tools a compaction request sends: the ones the conversation's own+/// requests carry when it forks, none otherwise.+pub fn tools(self: *const Agent, fork: bool) ?[]const u8 {+ if (!fork or self.text_only) return null;+ return self.toolsJson();+}++/// An assistant turn whose tool calls have no results yet: a user message+/// appended after it is a 400.+fn endsOnToolCall(items: []const Value) bool {+ if (items.len == 0) return true;+ const m = items[items.len - 1];+ if (m != .object) return true;+ const role = m.object.get("role") orelse return true;+ if (role != .string or !std.mem.eql(u8, role.string, "assistant")) return false;+ const content = m.object.get("content") orelse return false;+ if (content != .array) return false;+ for (content.array.items) |block| {+ if (block != .object) continue;+ const kind = block.object.get("type") orelse continue;+ if (kind == .string and std.mem.eql(u8, kind.string, "tool_use")) return true;+ }+ return false;+}++fn message(arena: std.mem.Allocator, role: []const u8, block_type: ?[]const u8) !Value {+ var m: std.json.ObjectMap = .empty;+ try m.put(arena, "role", .{ .string = role });+ if (block_type) |kind| {+ var block: std.json.ObjectMap = .empty;+ try block.put(arena, "type", .{ .string = kind });+ var blocks = std.json.Array.init(arena);+ try blocks.append(.{ .object = block });+ try m.put(arena, "content", .{ .array = blocks });+ } else try m.put(arena, "content", .{ .string = "done" });+ return .{ .object = m };+}++fn testAgent(arena: std.mem.Allocator, model: []const u8, messages: std.json.Array) Agent {+ return .{+ .gpa = std.testing.allocator,+ .arena = arena,+ .io = std.testing.io,+ .client = undefined,+ .provider = .{ .id = "anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "", .api_key = "k", .model = model, .context = 1_000_000 },+ .messages = messages,+ .sub = false,+ .label = "",+ .out = null,+ .sys_normal = "system",+ };+}++test "ADR 0220: a compaction forks the conversation only while the cache holds it" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var messages = std.json.Array.init(arena);+ try messages.append(try message(arena, "user", null));+ try messages.append(try message(arena, "assistant", null));+ var agent = testAgent(arena, "claude-opus-5-5", messages);+ try std.testing.expect(!warm(&agent)); // no request yet: nothing cached+ agent.request_started = std.Io.Timestamp.now(std.testing.io, .awake);+ try std.testing.expect(warm(&agent));+ try std.testing.expect(begin(&agent) and shares());+ end();+ try std.testing.expect(!shares());++ // A history that ends on an unanswered tool call cannot take a user turn.+ try agent.messages.append(try message(arena, "assistant", "tool_use"));+ try std.testing.expect(!warm(&agent));+ _ = agent.messages.pop();+ // Over the window, recovering an overflow, or after an unusable summary.+ agent.last_context_tokens = 990_000;+ try std.testing.expect(!warm(&agent));+ agent.last_context_tokens = 0;+ agent.compact_summary_failures = 1;+ try std.testing.expect(!warm(&agent));+ agent.compact_summary_failures = 0;+ agent.last_request_context_overflow = true;+ try std.testing.expect(!warm(&agent));+ agent.last_request_context_overflow = false;+ // Only the Anthropic API: other providers keep the old shape.+ agent.provider.id = "kimi";+ try std.testing.expect(!warm(&agent));+ agent.provider.id = "anthropic";+ try std.testing.expect(warm(&agent));++ // The fork carries the conversation's own tools; the old shape none.+ try std.testing.expectEqualStrings(agent.toolsJson(), tools(&agent, true).?);+ try std.testing.expect(tools(&agent, false) == null);+ agent.text_only = true;+ try std.testing.expect(tools(&agent, true) == null);+}++test "ADR 0220: a forked compaction request keeps the conversation's prefix byte for byte" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var messages = std.json.Array.init(arena);+ try messages.append(try message(arena, "user", null));+ var agent = testAgent(arena, "claude-sonnet-5-5", messages);+ const catalog = "[{\"name\":\"bash\",\"description\":\"\",\"input_schema\":{\"type\":\"object\"}}]";+ const live = try agent.buildBody(catalog, false, true, true);+ defer std.testing.allocator.free(live);++ agent.compaction_request = true;+ active = true;+ defer active = false;+ try agent.messages.append(try message(arena, "user", null));+ const fork = try agent.buildBody(catalog, false, true, true);+ defer std.testing.allocator.free(fork);+ // Tools, system prompt, thinking and effort match; the breakpoints stay.+ const cut = std.mem.indexOf(u8, live, "\"messages\"").?;+ try std.testing.expectEqualStrings(live[0..cut], fork[0..cut]);+ try std.testing.expect(std.mem.indexOf(u8, fork, "\"cache_control\"") != null);+}diff --git a/src/cache_ttl.zig b/src/cache_ttl.zigindex d49d6d0e..f461b057 100644--- a/src/cache_ttl.zig+++ b/src/cache_ttl.zig@@ -6,6 +6,12 @@//! writes) so the entry outlives the next slow call. The gap is taken once//! per request, before any retry, and usage_trace.zig reports it with the//! reason a request read nothing.+//!+//! ADR 0220: the request that opens a user turn waited on the user, not on+//! the model. Its gap says nothing about how long the next call takes, and+//! after an idle past 5 minutes it rewrites the whole prefix: at 1 hour that+//! rewrite cost 2x instead of 1.25x. It follows the last gap measured inside+//! a turn instead.const std = @import("std");const Agent = @import("agent.zig").Agent;@@ -19,16 +25,26 @@ pub const default_ttl_ms: i64 = 5 * 60 * 1000;/// Requests and their usage lines run on one thread (see usage_trace.zig).threadlocal var gap_ms: ?i64 = null;+/// The gap the TTL follows: the last one measured inside a turn.+threadlocal var loop_gap_ms: ?i64 = null;/// Before a request's retry loop: time since this agent's previous request began.pub fn begin(self: *const Agent) void {gap_ms = if (self.request_started) |t| t.untilNow(self.io, .awake).toMilliseconds() else null;+ if (self.request_started == null) {+ loop_gap_ms = null; // a fresh agent on this thread+ } else if (!opensTurn(self.messages.items)) loop_gap_ms = gap_ms;}pub fn gap() ?i64 {return gap_ms;}+/// The request answers a new user message (or a wake), not a tool result.+fn opensTurn(items: []const std.json.Value) bool {+ return items.len > 0 and @import("compact_cut.zig").cleanUserTurn(items[items.len - 1]);+}+/// The Anthropic API, and Anthropic models through OpenRouter (#1284), take a/// TTL; other Anthropic-format providers keep the default.pub fn takesTtl(provider_id: []const u8, model: []const u8) bool {@@ -41,9 +57,17 @@ pub fn longTtl(provider_id: []const u8, model: []const u8, since_previous: ?i64)return takesTtl(provider_id, model) and g >= slow_gap_ms;}-/// The cache_control object for this request's breakpoints.-pub fn control(self: *const Agent) []const u8 {- return if (longTtl(self.provider.id, self.provider.model, gap_ms)) ephemeral_1h else ephemeral;+/// Whether this thread's current request asks for the 1-hour TTL.+pub fn asksLong(provider_id: []const u8, model: []const u8) bool {+ return longTtl(provider_id, model, loop_gap_ms);+}++/// The cache_control object for this request's breakpoints. Null on a+/// compaction request that does not fork the conversation (ADR 0220): no+/// later request starts with its bytes, so a write would never be read.+pub fn control(self: *const Agent) ?[]const u8 {+ if (self.compaction_request and !@import("cache_fork.zig").shares()) return null;+ return if (asksLong(self.provider.id, self.provider.model)) ephemeral_1h else ephemeral;}/// Why a request with real input read nothing from the cache. Content-free.@@ -86,24 +110,74 @@ test "#1320: an Anthropic body after a slow call marks every breakpoint 1h; Kimi.out = null,.sys_normal = "system",};- defer gap_ms = null;+ defer loop_gap_ms = null;const long = "\"cache_control\":" ++ ephemeral_1h;- gap_ms = 307_000;+ loop_gap_ms = 307_000;const slow = try agent.buildBody(null, false, true, true);defer std.testing.allocator.free(slow);try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, slow, long)); // system + last messagetry std.testing.expect(std.mem.indexOf(u8, slow, "\"cache_control\":" ++ ephemeral ++ "") == null);- gap_ms = 20_000;+ loop_gap_ms = 20_000;const fast = try agent.buildBody(null, false, true, true);defer std.testing.allocator.free(fast);try std.testing.expect(std.mem.indexOf(u8, fast, "\"ttl\"") == null);+ // ADR 0220: a compaction request outside a fork carries no breakpoint.+ agent.compaction_request = true;+ const summary = try agent.buildBody(null, false, true, true);+ defer std.testing.allocator.free(summary);+ try std.testing.expect(std.mem.indexOf(u8, summary, "cache_control") == null);+ try std.testing.expect(std.mem.indexOf(u8, summary, "\"system\":[{\"type\":\"text\",\"text\":\"system\"}]") != null);+ agent.compaction_request = false;agent.provider.id = "kimi";- gap_ms = 307_000;+ loop_gap_ms = 307_000;const kimi = try agent.buildBody(null, false, true, true);defer std.testing.allocator.free(kimi);try std.testing.expect(std.mem.indexOf(u8, kimi, "\"ttl\"") == null);}+test "ADR 0220: the request that opens a turn keeps the TTL of the last gap inside one" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var prompt: std.json.ObjectMap = .empty;+ try prompt.put(arena, "role", .{ .string = "user" });+ try prompt.put(arena, "content", .{ .string = "next task" });+ var result: std.json.ObjectMap = .empty;+ try result.put(arena, "type", .{ .string = "tool_result" });+ var blocks = std.json.Array.init(arena);+ try blocks.append(.{ .object = result });+ var results: std.json.ObjectMap = .empty;+ try results.put(arena, "role", .{ .string = "user" });+ try results.put(arena, "content", .{ .array = blocks });+ var in_turn = std.json.Array.init(arena);+ try in_turn.append(.{ .object = results });+ var opening = std.json.Array.init(arena);+ try opening.append(.{ .object = prompt });+ defer loop_gap_ms = null;++ var agent: Agent = undefined;+ agent.io = std.testing.io;+ agent.request_started = null;+ loop_gap_ms = 307_000;+ begin(&agent); // a fresh agent on this thread starts clean+ try std.testing.expect(loop_gap_ms == null);++ // Inside a turn the TTL follows the measured gap.+ agent.request_started = std.Io.Timestamp.now(std.testing.io, .awake);+ agent.messages = in_turn;+ begin(&agent);+ try std.testing.expect(loop_gap_ms.? < 60_000);+ try std.testing.expect(!asksLong("anthropic", "claude-opus-5-5"));++ // A turn-opening request keeps the last in-turn gap, not its own: a slow+ // model stays on the hour, and an idle user never buys one.+ loop_gap_ms = 307_000;+ agent.messages = opening;+ begin(&agent);+ try std.testing.expectEqual(@as(i64, 307_000), loop_gap_ms.?);+ try std.testing.expect(asksLong("anthropic", "claude-opus-5-5"));+}+test "#1320: a zero-read request names its likely cause" {try std.testing.expect(missReason(900, 1000, true, 1) == null);try std.testing.expect(missReason(0, 200, false, null) == null); // below any cacheable sizediff --git a/src/claude_wire.zig b/src/claude_wire.zigindex 42c72023..ea10dcaa 100644--- a/src/claude_wire.zig+++ b/src/claude_wire.zig@@ -9,13 +9,15 @@//! under. graff edits that prefix mid-session (tools change when an MCP//! server joins; compaction rewrites history), which new accounts see as a//! 400. `drop_block` has the API drop stale blocks instead.-//! - Effort: `output_config.effort`, on models that take it. graff's default-//! (medium) is left off so each model keeps its own default.+//! - Effort: `output_config.effort`, on models that take it. A 5.5-era model+//! gets graff's level as shown, medium included (ADR 0220); an older one+//! keeps its own default at graff's default. Titles and recaps run at low.//! - max_tokens covers thinking plus text on always-thinking models: 64K, and//! the 128K maximum at the top efforts, as Anthropic measured and advises.const std = @import("std");const ReasoningEffort = @import("main.zig").ReasoningEffort;+const CallKind = @import("run_budget.zig").CallKind;pub const Family = enum { opus, sonnet, haiku, fable, mythos };pub const Version = struct { family: Family, major: u16, minor: u16 };@@ -74,12 +76,16 @@ pub fn takesEffort(model: []const u8) bool {};}-/// graff's effort on the wire. Null at graff's default: the model keeps its-/// own (medium on Opus 5.5, high on Opus 5 and Sonnet 5.5), so a session that-/// never ran /effort does not move. `ultra` asks for the most there is.-pub fn effortWire(effort: ReasoningEffort) ?[]const u8 {+/// graff's effort on the wire. A 5.5-era model gets the level graff shows,+/// medium included: Sonnet 5.5 and Fable 5.1 default to high, so leaving it+/// off ran them a level above the one on screen (ADR 0220). On Opus 5.5+/// medium is the default, which the API treats as unset. An older model keeps+/// its own default at graff's default, so a session that never ran /effort+/// does not move. `ultra` asks for the most there is.+pub fn effortWire(model: []const u8, effort: ReasoningEffort) ?[]const u8 {return switch (effort) {- .medium, .none => null,+ .none => null,+ .medium => if (fiveFiveEra(model)) "medium" else null,.low => "low",.high => "high",.xhigh => "xhigh",@@ -87,6 +93,14 @@ pub fn effortWire(effort: ReasoningEffort) ?[]const u8 {};}+/// A title or a recap is one line: it runs at low effort whatever the+/// session's level. Every other call keeps the session's effort, which the+/// prompt cache is keyed on (a compaction fork or /btw shares its prefix).+pub fn effortFor(model: []const u8, effort: ReasoningEffort, kind: CallKind) ?[]const u8 {+ if (kind == .title or kind == .recap) return "low";+ return effortWire(model, effort);+}+/// Thinking is on by default (Opus and Sonnet 5 and later, Fable, Mythos):/// max_tokens is spent on thinking before the answer.pub fn thinksByDefault(model: []const u8) bool {@@ -147,15 +161,24 @@ test "5.5-era models are never forced to a tool and bind their thinking" {}}-test "effort goes on the wire only where it is taken, and never at graff's default" {+test "effort goes on the wire only where it is taken; 5.5-era models get graff's level as shown" {const t = std.testing;try t.expect(takesEffort("claude-opus-5-5") and takesEffort("claude-sonnet-5-5") and takesEffort("claude-fable-5-1"));try t.expect(takesEffort("claude-opus-4-5") and takesEffort("claude-sonnet-4-6"));try t.expect(!takesEffort("claude-haiku-4-5") and !takesEffort("claude-sonnet-4-5") and !takesEffort("claude-opus-4-1"));- try t.expect(effortWire(.medium) == null);- try t.expectEqualStrings("high", effortWire(.high).?);- try t.expectEqualStrings("xhigh", effortWire(.xhigh).?);- try t.expectEqualStrings("max", effortWire(.ultra).?);+ // ADR 0220: medium is sent to the models whose default is not medium.+ try t.expectEqualStrings("medium", effortWire("claude-sonnet-5-5", .medium).?);+ try t.expectEqualStrings("medium", effortWire("claude-opus-5-5", .medium).?);+ try t.expect(effortWire("claude-opus-5", .medium) == null);+ try t.expect(effortWire("claude-opus-4-8", .medium) == null);+ try t.expectEqualStrings("high", effortWire("claude-opus-4-8", .high).?);+ try t.expectEqualStrings("xhigh", effortWire("claude-sonnet-5-5", .xhigh).?);+ try t.expectEqualStrings("max", effortWire("claude-opus-5-5", .ultra).?);+ // Titles and recaps run at low; every other call keeps the session's level.+ try t.expectEqualStrings("low", effortFor("claude-sonnet-5-5", .xhigh, .title).?);+ try t.expectEqualStrings("low", effortFor("claude-opus-4-8", .medium, .recap).?);+ try t.expectEqualStrings("xhigh", effortFor("claude-sonnet-5-5", .xhigh, .judge).?);+ try t.expectEqualStrings("medium", effortFor("claude-sonnet-5-5", .medium, .compaction).?);}test "max_tokens leaves room for thinking on 128K-output models" {diff --git a/src/compact_note_glue.zig b/src/compact_note_glue.zigindex cfe5411f..5968a43c 100644--- a/src/compact_note_glue.zig+++ b/src/compact_note_glue.zig@@ -8,12 +8,16 @@//! the Agent type into the prompt funnel.//!//! THE CALL IS THE COMPACTION'S OWN. It runs on the root's provider (the-//! history it reads is in that wire format), carries NO tools so it cannot-//! fan out, and sets `compaction_request` — which bounds the reply, drops-//! reasoning effort to low, charges the call to `CallKind.compaction`, and,-//! load-bearing here, disables the in-request overflow recovery that would-//! otherwise let a note turn recurse back into emergencyTrim mid-compaction-//! (agent_overflow.applyOverflowRecovery's `compaction_request` early-out).+//! history it reads is in that wire format), is never run as a turn so it+//! cannot fan out, and sets `compaction_request` — which bounds the reply,+//! drops reasoning effort to low on the Responses wire, charges the call to+//! `CallKind.compaction`, and, load-bearing here, disables the in-request+//! overflow recovery that would otherwise let a note turn recurse back into+//! emergencyTrim mid-compaction (agent_overflow.applyOverflowRecovery's+//! `compaction_request` early-out).+//! When compaction forks the conversation (ADR 0220, cache_fork.zig) it keeps+//! the root's tools and system prompt, so it reads the history from the+//! prompt cache; the persona then leads the instruction instead.//!//! IT IS TRANSACTIONAL. The note request runs against a container-deep clone//! of history in a throwaway arena, exactly as compact() builds its summary@@ -27,6 +31,7 @@ const Allocator = std.mem.Allocator;const Agent = @import("agent.zig").Agent;const agent_compact = @import("agent_compact.zig");+const cache_fork = @import("cache_fork.zig");const compact_note = @import("compact_note.zig");const main_mod = @import("main.zig");const phase_budget = @import("phase_budget.zig");@@ -87,17 +92,22 @@ pub fn maybeWrite(self: *Agent) Decision {const reply = askModel(self) orelse return .skip_failed;if (!compact_note.record(self.io, self.arena, self.session_name, self.history_rewrites, reply))return .skip_failed;- // The note is in the SYSTEM prompt, beside HARD CONSTRAINTS, so it has to- // be re-composed to reach the next request — the same refresh a mid-- // session `/never` performs, and for the same reason.- prompts.armCompactNotes(self.session_name);- @import("prompt_cache_hud.zig").noteBust(.compact);- playbook_glue.refreshRoot(self, self.arena);if (!main_mod.json_mode) self.say(" 📝 wrote a pre-compaction note to self ({d} chars)\n", .{reply.len}) catch {};if (self.tracer) |tr| tr.note("compact", "wrote a pre-compaction note to self (#391)");return .fire;}+/// After a `.fire`: the note is in the SYSTEM prompt, beside HARD+/// CONSTRAINTS, so it has to be re-composed to reach the next request — the+/// same refresh a mid-session `/never` performs, and for the same reason.+/// compact() calls this once its summary request is done: that request forks+/// the prompt the conversation was cached under (ADR 0220).+pub fn publish(self: *Agent) void {+ prompts.armCompactNotes(self.session_name);+ @import("prompt_cache_hud.zig").noteBust(.compact);+ playbook_glue.refreshRoot(self, self.arena);+}+/// The bounded turn. Mirrors playbook_reflect.askModel — a throwaway agent in/// its own arena — but on the ROOT's provider and over a clone of the ROOT's/// history, because a note about work in flight can only be written by@@ -106,8 +116,10 @@ fn askModel(self: *Agent) ?[]const u8 {var arena_state = std.heap.ArenaAllocator.init(self.gpa);defer arena_state.deinit();const arena = arena_state.allocator();+ const fork = cache_fork.shares(); // ADR 0220: the root's exact prefix, persona in the askvar messages = agent_compact.cloneJsonArray(arena, self.messages) catch return null;- messages.append(textMessage(arena, "user", compact_note.instruction) catch return null) catch return null;+ const ask = if (fork) std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ compact_note.persona, compact_note.instruction }) catch return null else compact_note.instruction;+ messages.append(textMessage(arena, "user", ask) catch return null) catch return null;var agent: Agent = .{.gpa = self.gpa,.arena = arena,@@ -121,13 +133,14 @@ fn askModel(self: *Agent) ?[]const u8 {.tracer = self.tracer,.run_budget = self.run_budget,.reasoning = self.reasoning,+ .fast = self.fast, // a speed change would miss the cached prefix.stream_quiet = true,.compaction_request = true, // bounded reply, low effort, no recursive recovery.message_mutation_arena = arena,- .sys_override = compact_note.persona,+ .sys_override = if (fork) self.systemPrompt() else compact_note.persona,};defer agent.tools_used.deinit(self.gpa);- const root = agent.request(null) catch return null;+ const root = agent.request(cache_fork.tools(self, fork)) catch return null;const text = std.mem.trim(u8, title.assistantText(self.provider.kind, root), " \t\r\n");if (compact_note.isEmptyReply(text)) return null;return self.arena.dupe(u8, text) catch null;diff --git a/src/openrouter_cache.zig b/src/openrouter_cache.zigindex 68125c8b..365aa8d2 100644--- a/src/openrouter_cache.zig+++ b/src/openrouter_cache.zig@@ -10,13 +10,15 @@const std = @import("std");const Agent = @import("agent.zig").Agent;-pub fn write(s: anytype, provider_id: []const u8, model: []const u8, session_key: []const u8, cache_control: []const u8) !void {+/// `cache_control` is null on a request no later one shares (ADR 0220).+pub fn write(s: anytype, provider_id: []const u8, model: []const u8, session_key: []const u8, cache_control: ?[]const u8) !void {if (!std.mem.eql(u8, provider_id, "openrouter")) return;try s.objectField("session_id");try s.write(session_key);if (!std.mem.startsWith(u8, model, "anthropic/")) return;+ const cc = cache_control orelse return;try s.objectField("cache_control");- try s.print("{s}", .{cache_control});+ try s.print("{s}", .{cc});}fn body(arena: std.mem.Allocator, provider_id: []const u8, model: []const u8) ![]u8 {diff --git a/src/pricing_cache.zig b/src/pricing_cache.zigindex 3fa50f28..accb4baf 100644--- a/src/pricing_cache.zig+++ b/src/pricing_cache.zig@@ -2,6 +2,11 @@const std = @import("std");+/// The 5-minute cache write's price as a multiple of input (Anthropic, GPT-5.6).+const five_minute_write: f64 = 1.25;+/// Anthropic's 1-hour cache write (ADR 0220).+const one_hour_write: f64 = 2.0;+/// USD for one request (negative token counts clamp to zero). Cache writes are/// separate from ordinary input: GPT-5.6 and Anthropic ephemeral caches bill/// writes at 1.25× the ordinary input rate.@@ -12,10 +17,31 @@ pub fn usdForUsage(p: anytype, model: []const u8, ordinary_in: i64, cache_in: i6const prompt: u64 = @as(u64, @intCast(ui)) +| @as(u64, @intCast(ci)) +| @as(u64, @intCast(wi));const high = p.high_at > 0 and prompt >= p.high_at;const input_rate = if (high) p.high_in else p.in;- const write_multiplier: f64 = p.cache_write_multiplier orelse if (std.mem.startsWith(u8, model, "gpt-5.6") or std.mem.startsWith(u8, model, "gpt-6-") or std.mem.startsWith(u8, model, "claude-")) 1.25 else 1.0;+ const write_multiplier: f64 = p.cache_write_multiplier orelse if (std.mem.startsWith(u8, model, "gpt-5.6") or std.mem.startsWith(u8, model, "gpt-6-") or std.mem.startsWith(u8, model, "claude-")) five_minute_write else 1.0;const fi: f64 = @floatFromInt(ui);const fc: f64 = @floatFromInt(ci);const fw: f64 = @floatFromInt(wi);const fo: f64 = @floatFromInt(@max(out, 0));return (fi * input_rate + fc * (if (high) p.high_cache else p.cache) + fw * input_rate * write_multiplier + fo * (if (high) p.high_out else p.out)) / 1_000_000.0;}++/// What usdForUsage leaves out of a Claude request that wrote to the 1-hour+/// cache: those tokens cost 2x input, not the 1.25x it charges every write.+/// A row that states its own write price is billed as it says.+pub fn hourWriteExtraUsd(p: anytype, model: []const u8, prompt_tokens: u64, hour_tokens: i64) f64 {+ if (hour_tokens <= 0 or p.cache_write_multiplier != null or !std.mem.startsWith(u8, model, "claude-")) return 0;+ const high = p.high_at > 0 and prompt_tokens >= p.high_at;+ const input_rate = if (high) p.high_in else p.in;+ return @as(f64, @floatFromInt(hour_tokens)) * input_rate * (one_hour_write - five_minute_write) / 1_000_000.0;+}++test "ADR 0220: a 1-hour cache write costs 2x input on Claude" {+ const opus = @import("pricing.zig").priceFor("claude-opus-5-5").?;+ // 100k tokens written: 1.25x of $4/MTok in usdForUsage, 0.75x more at 1 hour.+ try std.testing.expectApproxEqAbs(@as(f64, 0.5), usdForUsage(opus, "claude-opus-5-5", 0, 0, 100_000, 0), 1e-9);+ try std.testing.expectApproxEqAbs(@as(f64, 0.3), hourWriteExtraUsd(opus, "claude-opus-5-5", 100_000, 100_000), 1e-9);+ try std.testing.expectEqual(@as(f64, 0), hourWriteExtraUsd(opus, "claude-opus-5-5", 100_000, 0));+ // A row with its own write price (the hosted gateway's flat 1x) is not repriced.+ const flat = @import("pricing_gateway.zig").rows[0];+ try std.testing.expectEqual(@as(f64, 0), hourWriteExtraUsd(flat, flat.name, 100_000, 100_000));+}diff --git a/src/serde.zig b/src/serde.zigindex 00134b5d..ac86d6bb 100644--- a/src/serde.zig+++ b/src/serde.zig@@ -105,12 +105,33 @@ fn writeObjectWithCache(s: *std.json.Stringify, obj: std.json.ObjectMap, cache_ctry s.endObject();}+/// The last user message before the final one: the previous request's last.+fn previousUser(items: []const Value) ?usize {+ if (items.len < 2) return null;+ var i = items.len - 1;+ while (i > 0) {+ i -= 1;+ const m = items[i];+ if (m != .object) continue;+ const role = m.object.get("role") orelse continue;+ if (role == .string and std.mem.eql(u8, role.string, "user")) return i;+ }+ return null;+}+/// Serialize Anthropic messages. `normalize_blocks` matches the official Kimi/// adapter by turning every plain string into a `{type:text,text}` content/// array. `cache` (the cache_control object, #1320) marks the final cacheable/// block (including tool_result), not just the plain-string happy path.+///+/// ADR 0220: it also marks the user message before the last one, where the+/// previous request wrote its entry. A breakpoint finds an earlier entry only+/// by walking back at most 20 blocks, and one assistant turn that interleaves+/// thinking with many tool calls can add more; the second breakpoint reads+/// that entry exactly. Breakpoints are free: writes bill once, past the hit.pub fn writeAnthropicMessages(s: *std.json.Stringify, messages: std.json.Array, cache: ?[]const u8, normalize_blocks: bool) !void {const items = messages.items;+ const previous = if (cache != null) previousUser(items) else null;try s.beginArray();for (items, 0..) |m, i| {if (m != .object or m.object.get("content") == null) {@@ -118,7 +139,7 @@ pub fn writeAnthropicMessages(s: *std.json.Stringify, messages: std.json.Array,continue;}const content = m.object.get("content").?;- const cache_this = cache != null and i + 1 == items.len;+ const cache_this = cache != null and (i + 1 == items.len or (previous != null and i == previous.?));const string_content = content == .string;const array_cache = cache_this and content == .array and content.array.items.len > 0 and anthropicCacheableBlock(content.array.items[content.array.items.len - 1]);if (!normalize_blocks and !cache_this) {@@ -416,6 +437,66 @@ fn renderAnthropicTools(arena: Allocator, raw: []const u8, cache: bool) ![]constreturn aw.writer.buffered();}+test "ADR 0220: the previous request's last message keeps a breakpoint too" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var prompt: std.json.ObjectMap = .empty;+ try prompt.put(a, "role", .{ .string = "user" });+ try prompt.put(a, "content", .{ .string = "fix the build" });+ var thinking: std.json.ObjectMap = .empty;+ try thinking.put(a, "type", .{ .string = "thinking" });+ try thinking.put(a, "thinking", .{ .string = "look" });+ try thinking.put(a, "signature", .{ .string = "sig" });+ var call: std.json.ObjectMap = .empty;+ try call.put(a, "type", .{ .string = "tool_use" });+ try call.put(a, "id", .{ .string = "t1" });+ var turn = std.json.Array.init(a);+ try turn.append(.{ .object = thinking });+ try turn.append(.{ .object = call });+ var assistant: std.json.ObjectMap = .empty;+ try assistant.put(a, "role", .{ .string = "assistant" });+ try assistant.put(a, "content", .{ .array = turn });+ var result: std.json.ObjectMap = .empty;+ try result.put(a, "type", .{ .string = "tool_result" });+ try result.put(a, "tool_use_id", .{ .string = "t1" });+ try result.put(a, "content", .{ .string = "ok" });+ var results = std.json.Array.init(a);+ try results.append(.{ .object = result });+ var reply: std.json.ObjectMap = .empty;+ try reply.put(a, "role", .{ .string = "user" });+ try reply.put(a, "content", .{ .array = results });+ var messages = std.json.Array.init(a);+ try messages.append(.{ .object = prompt });++ const cc = "{\"type\":\"ephemeral\"}";+ var one: Io.Writer.Allocating = .init(std.testing.allocator);+ defer one.deinit();+ var s1: std.json.Stringify = .{ .writer = &one.writer };+ try writeAnthropicMessages(&s1, messages, cc, false);+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, one.written(), "cache_control"));++ try messages.append(.{ .object = assistant });+ try messages.append(.{ .object = reply });+ var three: Io.Writer.Allocating = .init(std.testing.allocator);+ defer three.deinit();+ var s3: std.json.Stringify = .{ .writer = &three.writer };+ try writeAnthropicMessages(&s3, messages, cc, false);+ const out = three.written();+ // The prompt (where the previous request wrote) and the tool result; never the thinking block.+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, out, "cache_control"));+ try std.testing.expect(std.mem.indexOf(u8, out, "\"text\":\"fix the build\",\"cache_control\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "\"content\":\"ok\",\"cache_control\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "\"signature\":\"sig\",\"cache_control\"") == null);++ // Without a cache object nothing is marked.+ var none: Io.Writer.Allocating = .init(std.testing.allocator);+ defer none.deinit();+ var s0: std.json.Stringify = .{ .writer = &none.writer };+ try writeAnthropicMessages(&s0, messages, null, false);+ try std.testing.expect(std.mem.indexOf(u8, none.written(), "cache_control") == null);+}+test "kimi tools give a typeless MCP root schema type object (#261)" {var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);defer arena_state.deinit();diff --git a/src/usage_trace.zig b/src/usage_trace.zigindex 6597f9ce..1b11db1b 100644--- a/src/usage_trace.zig+++ b/src/usage_trace.zig@@ -54,7 +54,7 @@ pub fn note(self: *const Agent, ordinary: i64, cache_read: i64, cache_write: i64// Sent as previous_response_id + the new items on a held socket..chained = self.codex_prev_id != null,.gap_ms = gap,- .cache_ttl = if (cache_ttl.longTtl(self.provider.id, self.provider.model, gap)) "1h" else null,+ .cache_ttl = if (cache_ttl.control(self)) |c| (if (std.mem.eql(u8, c, cache_ttl.ephemeral_1h)) "1h" else null) else null,.prefix_changed = !snap.same,.prefix_bust = if (snap.same) null else hud.bustLabel(snap.last_bust),.cache_miss = cache_ttl.missReason(cache_read, input, !snap.same, gap),@@ -104,8 +104,10 @@ test "usage waits for its api line and is written once" {return 0;}};- var agent: Agent = undefined; // only provider, sub and codex_prev_id are read+ var agent: Agent = undefined; // only provider, sub, codex_prev_id and compaction_request are readagent.provider.id = "codex";+ agent.provider.model = "gpt-6-sol";+ agent.compaction_request = false;agent.codex_prev_id = null;agent.sub = true;agent.request_started = null;