xonecas.io
64875
Mc

mini-coder / commit fb341f7

fix: unified per-step prompt caching via middleware

Replace the broken per-turn cache breakpoint annotation with per-step
caching using wrapLanguageModel middleware. Cache breakpoints now track
the conversation tail on every API call, not just the first one.

Key changes:
- Move annotateAnthropicCacheBreakpoints into transformParams middleware
  so it runs on every step (matching opencode's approach)
- Annotate first system message + last 2 non-system messages (3 of 4
  max breakpoints), with 1 reserved for tool-level caching
- Switch OAuth from authToken to apiKey:"" + custom fetch wrapper
- Remove /cache command — caching is always on
- Remove promptCachingEnabled, openaiPromptCacheRetention, and
  googleCachedContent plumbing throughout the codebase
- Remove sanitizeOAuthBody hack
- Fix OAuth system message ordering (identity must be first)

Verified with both zen and oauth providers showing cache reads across
multi-step turns (14+ steps, 1 uncached token per step once active).

xonecas2026-03-18 fb341f72ee3a1d52dccdf6e0410a9d88ab1c359d parent 2cb77b5

23 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

TODO.md+1−1
@@ -1,7 +1,7 @@
11 # TODO
22
3-- Reaching max tool calls causes a 400 error
43 - CTRL+d hangs sometimes and doesn't exit
4+- Codebase is hurting from fast iteration and adding/removing features as well as core fundamental changes.
55
66 ## Deferred fixes
77
mini-coder-idea.md+0−1
@@ -44,7 +44,6 @@ conventions and not introduce more specs. (`.agents` / `AGENTS.md`, while also s
4444 - `/reasoning` toggles display of model reasoning output.
4545 - `/verbose` toggles output truncation. Tuncates large outputs keeping start and end sections and truncating the rest when verbose is off. This controls truncation for the user output, it's a visibility UI concern
4646 - `/context` configures context pruning and tool-result caps.
47- - `/cache` toggles prompt caching globally; sub-commands configure provider-specific caching (`/cache openai <in_memory|24h>`, `/cache gemini <off|cachedContents/...>`).
4847 - `/review` reviews recent changes via a global custom command installed at app start (`~/.agents/commands/review.md`), and can be customized or shadowed locally.
4948 - `/agent` sets or clears the active agent.
5049 - `/mcp` list/add/remove mcp servers. servers are stored in sqlite
scripts/ui-oneshot.ts+0−514
@@ -1,514 +0,0 @@
1-#!/usr/bin/env bun
2-/**
3- * Manual one-shot UI test: renders example scenarios to stdout
4- * so you can visually inspect the console output.
5- *
6- * Usage: bun scripts/ui-oneshot.ts
7- */
8-import type { TurnEvent } from "../src/llm-api/types.ts";
9-import { renderBanner, renderError, renderUserMessage, writeln } from "../src/cli/output.ts";
10-import { Spinner } from "../src/cli/spinner.ts";
11-import { renderTurn } from "../src/cli/stream-render.ts";
12-import { renderToolCall, renderToolResult } from "../src/cli/tool-render.ts";
13-
14-function eventsFrom(events: TurnEvent[]): AsyncIterable<TurnEvent> {
15- return (async function* () {
16- for (const event of events) {
17- yield event;
18- }
19- })();
20-}
21-
22-function done(): TurnEvent {
23- return {
24- type: "turn-complete",
25- inputTokens: 1234,
26- outputTokens: 567,
27- contextTokens: 50000,
28- messages: [],
29- };
30-}
31-
32-function shell(opts: {
33- stdout?: string;
34- stderr?: string;
35- exitCode?: number;
36- success?: boolean;
37-}) {
38- return {
39- stdout: opts.stdout ?? "",
40- stderr: opts.stderr ?? "",
41- exitCode: opts.exitCode ?? 0,
42- success: opts.success ?? true,
43- timedOut: false,
44- };
45-}
46-
47-function separator(title: string) {
48- writeln(`\n${"─".repeat(60)}`);
49- writeln(` ${title}`);
50- writeln(`${"─".repeat(60)}\n`);
51-}
52-
53-async function main() {
54- // ── Banner ──────────────────────────────────────────────
55- separator("1. Banner");
56- renderBanner("claude-sonnet-4-20250514", "~/projects/my-app");
57-
58- // ── User message ───────────────────────────────────────
59- separator("2. User message (single line)");
60- renderUserMessage("Fix the failing tests in src/utils.ts");
61-
62- separator("3. User message (multi-line)");
63- renderUserMessage(
64- "Please review these files:\n- src/index.ts\n- src/utils.ts\n- src/config.ts",
65- );
66-
67- // ── Simple text reply ──────────────────────────────────
68- separator("4. Simple text reply (no reasoning)");
69- await renderTurn(
70- eventsFrom([
71- { type: "text-delta", delta: "I'll take a look at those files for you." },
72- done(),
73- ]),
74- new Spinner(),
75- );
76-
77- // ── Reasoning + text reply ─────────────────────────────
78- separator("5. Reasoning → text reply");
79- await renderTurn(
80- eventsFrom([
81- {
82- type: "reasoning-delta",
83- delta:
84- "The user wants me to fix failing tests. Let me check the test file first to understand the failures.",
85- },
86- {
87- type: "text-delta",
88- delta:
89- "I'll start by examining the test file to understand what's failing.",
90- },
91- done(),
92- ]),
93- new Spinner(),
94- );
95-
96- // ── Single tool call ───────────────────────────────────
97- separator("6. Single tool call (shell)");
98- await renderTurn(
99- eventsFrom([
100- {
101- type: "tool-call-start",
102- toolName: "shell",
103- toolCallId: "t1",
104- args: { command: "bun test src/utils.test.ts 2>&1 | tail -20" },
105- },
106- {
107- type: "tool-result",
108- toolName: "shell",
109- toolCallId: "t1",
110- isError: false,
111- result: shell({
112- stdout:
113- "src/utils.test.ts:\n(pass) parseConfig > handles defaults\n(fail) parseConfig > validates required fields\n(pass) parseConfig > merges overrides\n\n 2 pass\n 1 fail\n 5 expect() calls",
114- exitCode: 1,
115- success: false,
116- }),
117- },
118- {
119- type: "text-delta",
120- delta:
121- "I can see one test is failing. Let me look at the test to understand the expected behavior.",
122- },
123- done(),
124- ]),
125- new Spinner(),
126- );
127-
128- // ── Parallel tool calls ────────────────────────────────
129- separator("7. Parallel tool calls (2 shells)");
130- await renderTurn(
131- eventsFrom([
132- {
133- type: "reasoning-delta",
134- delta: "I need to read both the source and test file to understand the issue.",
135- },
136- {
137- type: "tool-call-start",
138- toolName: "shell",
139- toolCallId: "p1",
140- args: { command: "cat src/utils.ts" },
141- },
142- {
143- type: "tool-call-start",
144- toolName: "shell",
145- toolCallId: "p2",
146- args: { command: "cat src/utils.test.ts" },
147- },
148- {
149- type: "tool-result",
150- toolName: "shell",
151- toolCallId: "p1",
152- isError: false,
153- result: shell({
154- stdout:
155- 'export function parseConfig(raw: unknown): Config {\n if (typeof raw !== "object" || raw === null) {\n throw new Error("invalid config");\n }\n return { ...defaults, ...raw };\n}',
156- }),
157- },
158- {
159- type: "tool-result",
160- toolName: "shell",
161- toolCallId: "p2",
162- isError: false,
163- result: shell({
164- stdout:
165- 'test("validates required fields", () => {\n expect(() => parseConfig(null)).toThrow("invalid config");\n expect(() => parseConfig({})).toThrow("missing required");\n});',
166- }),
167- },
168- {
169- type: "text-delta",
170- delta:
171- "Found the issue! The test expects `parseConfig({})` to throw `\"missing required\"`, but the function only throws for non-objects.",
172- },
173- done(),
174- ]),
175- new Spinner(),
176- );
177-
178- // ── Parallel mixed tools ───────────────────────────────
179- separator("8. Parallel mixed tools (shell + readSkill)");
180- await renderTurn(
181- eventsFrom([
182- {
183- type: "tool-call-start",
184- toolName: "shell",
185- toolCallId: "m1",
186- args: { command: "wc -l src/**/*.ts" },
187- },
188- {
189- type: "tool-call-start",
190- toolName: "readSkill",
191- toolCallId: "m3",
192- args: { name: "testing" },
193- },
194- {
195- type: "tool-result",
196- toolName: "shell",
197- toolCallId: "m1",
198- isError: false,
199- result: shell({
200- stdout: " 142 src/index.ts\n 87 src/utils.ts\n 45 src/config.ts\n 274 total",
201- }),
202- },
203- {
204- type: "tool-result",
205- toolName: "readSkill",
206- toolCallId: "m3",
207- isError: false,
208- result: {
209- skill: { name: "testing", description: "Testing best practices", source: "local" },
210- },
211- },
212- {
213- type: "text-delta",
214- delta: "Based on the code review and testing guidelines, here are my recommendations.",
215- },
216- done(),
217- ]),
218- new Spinner(),
219- );
220-
221- // ── Tool error ─────────────────────────────────────────
222- separator("9. Tool call with error");
223- await renderTurn(
224- eventsFrom([
225- {
226- type: "tool-call-start",
227- toolName: "shell",
228- toolCallId: "e1",
229- args: { command: "rm -rf /protected" },
230- },
231- {
232- type: "tool-result",
233- toolName: "shell",
234- toolCallId: "e1",
235- isError: true,
236- result: "permission denied: /protected",
237- },
238- {
239- type: "text-delta",
240- delta: "I don't have permission to modify that directory.",
241- },
242- done(),
243- ]),
244- new Spinner(),
245- );
246-
247- // ── Parallel with mixed success/error ──────────────────
248- separator("10. Parallel calls: one success, one error");
249- await renderTurn(
250- eventsFrom([
251- {
252- type: "tool-call-start",
253- toolName: "shell",
254- toolCallId: "x1",
255- args: { command: "cat README.md" },
256- },
257- {
258- type: "tool-call-start",
259- toolName: "shell",
260- toolCallId: "x2",
261- args: { command: "cat MISSING.md" },
262- },
263- {
264- type: "tool-result",
265- toolName: "shell",
266- toolCallId: "x1",
267- isError: false,
268- result: shell({ stdout: "# My Project\n\nA cool project." }),
269- },
270- {
271- type: "tool-result",
272- toolName: "shell",
273- toolCallId: "x2",
274- isError: false,
275- result: shell({
276- stderr: "cat: MISSING.md: No such file or directory",
277- exitCode: 1,
278- success: false,
279- }),
280- },
281- {
282- type: "text-delta",
283- delta: "README exists but MISSING.md doesn't. Let me create it.",
284- },
285- done(),
286- ]),
287- new Spinner(),
288- );
289-
290- // ── Context pruned ─────────────────────────────────────
291- separator("11. Context pruned event");
292- await renderTurn(
293- eventsFrom([
294- {
295- type: "context-pruned",
296- mode: "balanced",
297- beforeMessageCount: 120,
298- afterMessageCount: 80,
299- removedMessageCount: 40,
300- beforeTotalBytes: 65536,
301- afterTotalBytes: 40960,
302- removedBytes: 24576,
303- },
304- {
305- type: "text-delta",
306- delta: "I've been working on this for a while. Let me continue where I left off.",
307- },
308- done(),
309- ]),
310- new Spinner(),
311- );
312-
313- // ── Error rendering ────────────────────────────────────
314- separator("12. Error messages");
315- renderError("API rate limit exceeded. Retrying in 30s...");
316- writeln("");
317- renderError("Connection timed out after 60s");
318-
319- // ── Full realistic session ─────────────────────────────
320- separator("13. Realistic multi-turn snippet");
321- renderUserMessage("Fix the type error in src/config.ts");
322- writeln("");
323- await renderTurn(
324- eventsFrom([
325- {
326- type: "reasoning-delta",
327- delta: "Let me look at the file to find the type error.",
328- },
329- {
330- type: "tool-call-start",
331- toolName: "shell",
332- toolCallId: "r1",
333- args: { command: "bun run typecheck 2>&1 | grep 'config.ts'" },
334- },
335- {
336- type: "tool-result",
337- toolName: "shell",
338- toolCallId: "r1",
339- isError: false,
340- result: shell({
341- stdout:
342- "src/config.ts(12,5): error TS2322: Type 'string' is not assignable to type 'number'.",
343- exitCode: 1,
344- success: false,
345- }),
346- },
347- {
348- type: "reasoning-delta",
349- delta:
350- "Line 12 has a type mismatch. The port should be a number but is being set as a string.",
351- },
352- {
353- type: "tool-call-start",
354- toolName: "shell",
355- toolCallId: "r2",
356- args: { command: "sed -n '10,15p' src/config.ts" },
357- },
358- {
359- type: "tool-result",
360- toolName: "shell",
361- toolCallId: "r2",
362- isError: false,
363- result: shell({
364- stdout:
365- ' const config = {\n host: env.HOST ?? "localhost",\n port: env.PORT ?? "3000",\n debug: env.DEBUG === "true",\n };',
366- }),
367- },
368- {
369- type: "tool-call-start",
370- toolName: "shell",
371- toolCallId: "r3",
372- args: {
373- command:
374- 'mc-edit src/config.ts --old \'port: env.PORT ?? "3000"\' --new \'port: Number(env.PORT ?? "3000")\'',
375- },
376- },
377- {
378- type: "tool-result",
379- toolName: "shell",
380- toolCallId: "r3",
381- isError: false,
382- result: shell({ stdout: "ok: true\npath: src/config.ts\nchanged: true" }),
383- },
384- {
385- type: "tool-call-start",
386- toolName: "shell",
387- toolCallId: "r4",
388- args: { command: "bun run typecheck 2>&1" },
389- },
390- {
391- type: "tool-result",
392- toolName: "shell",
393- toolCallId: "r4",
394- isError: false,
395- result: shell({ stdout: "" }),
396- },
397- {
398- type: "text-delta",
399- delta:
400- 'Fixed the type error on line 12 of `src/config.ts`. The `port` field was being assigned a string from `env.PORT ?? "3000"` — wrapped it in `Number()` to coerce to the expected `number` type. Typecheck passes now.',
401- },
402- done(),
403- ]),
404- new Spinner(),
405- );
406-
407- // ── Web search ──────────────────────────────────────────
408- separator("14. Web search results");
409- await renderTurn(
410- eventsFrom([
411- {
412- type: "tool-call-start",
413- toolName: "webSearch",
414- toolCallId: "ws1",
415- args: { query: "bun test runner docs" },
416- },
417- {
418- type: "tool-result",
419- toolName: "webSearch",
420- toolCallId: "ws1",
421- isError: false,
422- result: {
423- results: [
424- {
425- title: "Bun Test Runner Documentation",
426- url: "https://bun.sh/docs/cli/test",
427- score: 0.95,
428- },
429- {
430- title: "Testing with Bun - Getting Started",
431- url: "https://bun.sh/guides/test/getting-started",
432- score: 0.87,
433- },
434- ],
435- },
436- },
437- {
438- type: "text-delta",
439- delta: "Found the relevant documentation.",
440- },
441- done(),
442- ]),
443- new Spinner(),
444- );
445-
446- // ── Web content ─────────────────────────────────────────
447- separator("16. Web content fetch");
448- await renderTurn(
449- eventsFrom([
450- {
451- type: "tool-call-start",
452- toolName: "webContent",
453- toolCallId: "wc1",
454- args: { urls: ["https://bun.sh/docs/cli/test"] },
455- },
456- {
457- type: "tool-result",
458- toolName: "webContent",
459- toolCallId: "wc1",
460- isError: false,
461- result: {
462- results: [
463- {
464- title: "Bun Test Runner Documentation",
465- url: "https://bun.sh/docs/cli/test",
466- text: "Bun ships with a fast built-in test runner. Tests are written in TypeScript or JavaScript using a familiar Jest-like API.",
467- },
468- ],
469- },
470- },
471- {
472- type: "text-delta",
473- delta: "The docs confirm Bun uses a Jest-like API.",
474- },
475- done(),
476- ]),
477- new Spinner(),
478- );
479-
480- // ── List skills ─────────────────────────────────────────
481- separator("17. List skills");
482- await renderTurn(
483- eventsFrom([
484- {
485- type: "tool-call-start",
486- toolName: "listSkills",
487- toolCallId: "ls1",
488- args: {},
489- },
490- {
491- type: "tool-result",
492- toolName: "listSkills",
493- toolCallId: "ls1",
494- isError: false,
495- result: {
496- skills: [
497- { name: "mini-coder", description: "How to use the mc CLI coding agent", source: "local" },
498- { name: "deploy", description: "Deployment guidelines", source: "global" },
499- ],
500- },
501- },
502- {
503- type: "text-delta",
504- delta: "Found 2 available skills.",
505- },
506- done(),
507- ]),
508- new Spinner(),
509- );
510-
511- writeln("");
512-}
513-
514-main();
src/agent/agent.ts+0−30
@@ -10,10 +10,7 @@ import {
1010 listMcpServers,
1111 setPreferredActiveAgent,
1212 setPreferredContextPruningMode,
13- setPreferredGoogleCachedContent,
1413 setPreferredModel,
15- setPreferredOpenAIPromptCacheRetention,
16- setPreferredPromptCachingEnabled,
1714 setPreferredShowReasoning,
1815 setPreferredThinkingEffort,
1916 setPreferredToolResultPayloadCapBytes,
@@ -31,9 +28,6 @@ interface AgentOptions {
3128 initialVerboseOutput: boolean;
3229 initialPruningMode: ContextPruningMode;
3330 initialToolResultPayloadCapBytes: number;
34- initialPromptCachingEnabled: boolean;
35- initialOpenAIPromptCacheRetention: "in_memory" | "24h";
36- initialGoogleCachedContent: string | null;
3731 sessionId?: string;
3832 reporter: AgentReporter;
3933 agentSystemPrompt?: string;
@@ -87,9 +81,6 @@ export async function initAgent(opts: AgentOptions): Promise<{
8781 initialVerboseOutput: opts.initialVerboseOutput,
8882 initialPruningMode: opts.initialPruningMode,
8983 initialToolResultPayloadCapBytes: opts.initialToolResultPayloadCapBytes,
90- initialPromptCachingEnabled: opts.initialPromptCachingEnabled,
91- initialOpenAIPromptCacheRetention: opts.initialOpenAIPromptCacheRetention,
92- initialGoogleCachedContent: opts.initialGoogleCachedContent,
9384 sessionId: opts.sessionId,
9485 extraSystemPrompt: opts.agentSystemPrompt,
9586 });
@@ -152,27 +143,6 @@ export async function initAgent(opts: AgentOptions): Promise<{
152143 runner.toolResultPayloadCapBytes = Math.max(0, bytes);
153144 setPreferredToolResultPayloadCapBytes(runner.toolResultPayloadCapBytes);
154145 },
155- get promptCachingEnabled() {
156- return runner.promptCachingEnabled;
157- },
158- setPromptCachingEnabled: (enabled) => {
159- runner.promptCachingEnabled = enabled;
160- setPreferredPromptCachingEnabled(enabled);
161- },
162- get openaiPromptCacheRetention() {
163- return runner.openaiPromptCacheRetention;
164- },
165- setOpenAIPromptCacheRetention: (retention) => {
166- runner.openaiPromptCacheRetention = retention;
167- setPreferredOpenAIPromptCacheRetention(retention);
168- },
169- get googleCachedContent() {
170- return runner.googleCachedContent;
171- },
172- setGoogleCachedContent: (contentId) => {
173- runner.googleCachedContent = contentId;
174- setPreferredGoogleCachedContent(contentId);
175- },
176146 cwd,
177147
178148 get activeAgent() {
src/agent/session-runner.ts+0−12
@@ -33,9 +33,6 @@ interface SessionRunnerOptions {
3333 initialVerboseOutput: boolean;
3434 initialPruningMode: ContextPruningMode;
3535 initialToolResultPayloadCapBytes: number;
36- initialPromptCachingEnabled: boolean;
37- initialOpenAIPromptCacheRetention: "in_memory" | "24h";
38- initialGoogleCachedContent: string | null;
3936 sessionId?: string | undefined;
4037 extraSystemPrompt?: string | undefined;
4138 }
@@ -62,9 +59,6 @@ export class SessionRunner {
6259 public verboseOutput: boolean;
6360 public pruningMode: ContextPruningMode;
6461 public toolResultPayloadCapBytes: number;
65- public promptCachingEnabled: boolean;
66- public openaiPromptCacheRetention: "in_memory" | "24h";
67- public googleCachedContent: string | null;
6862
6963 public session!: ActiveSession;
7064
@@ -88,9 +82,6 @@ export class SessionRunner {
8882 this.verboseOutput = opts.initialVerboseOutput;
8983 this.pruningMode = opts.initialPruningMode;
9084 this.toolResultPayloadCapBytes = opts.initialToolResultPayloadCapBytes;
91- this.promptCachingEnabled = opts.initialPromptCachingEnabled;
92- this.openaiPromptCacheRetention = opts.initialOpenAIPromptCacheRetention;
93- this.googleCachedContent = opts.initialGoogleCachedContent;
9485 this.extraSystemPrompt = opts.extraSystemPrompt;
9586 this.initSession(opts.sessionId);
9687 }
@@ -215,9 +206,6 @@ export class SessionRunner {
215206 signal: abortController.signal,
216207 pruningMode: this.pruningMode,
217208 toolResultPayloadCapBytes: this.toolResultPayloadCapBytes,
218- promptCachingEnabled: this.promptCachingEnabled,
219- openaiPromptCacheRetention: this.openaiPromptCacheRetention,
220- googleCachedContent: this.googleCachedContent,
221209 ...(this.currentThinkingEffort
222210 ? { thinkingEffort: this.currentThinkingEffort }
223211 : {}),
src/cli/commands-config.test.ts+0−18
@@ -1,6 +1,5 @@
11 import { describe, expect, test } from "bun:test";
22 import {
3- handleCacheCommand,
43 handleContextCommand,
54 handleReasoningCommand,
65 handleVerboseCommand,
@@ -21,12 +20,6 @@ function createContext(overrides?: Partial<CommandContext>): CommandContext {
2120 setPruningMode: () => {},
2221 toolResultPayloadCapBytes: 0,
2322 setToolResultPayloadCapBytes: () => {},
24- promptCachingEnabled: true,
25- setPromptCachingEnabled: () => {},
26- openaiPromptCacheRetention: "in_memory",
27- setOpenAIPromptCacheRetention: () => {},
28- googleCachedContent: null,
29- setGoogleCachedContent: () => {},
3023 undoLastTurn: async () => false,
3124 startNewSession: () => {},
3225 connectMcpServer: async () => {},
@@ -51,17 +44,6 @@ describe("commands-config", () => {
5144 expect(captured).toBe(12 * 1024);
5245 });
5346
54- test("/cache gemini off clears cached content", () => {
55- let cachedContent: string | null = "cachedContents/foo";
56- const ctx = createContext({
57- setGoogleCachedContent: (value) => {
58- cachedContent = value;
59- },
60- });
61- handleCacheCommand(ctx, "gemini off");
62- expect(cachedContent).toBeNull();
63- });
64-
6547 test("/reasoning toggles when no arg is provided", () => {
6648 let reasoningVisible = true;
6749 const ctx = createContext({
src/cli/commands-config.ts+0−56
@@ -117,59 +117,3 @@ export function handleContextCommand(ctx: CommandContext, args: string): void {
117117 `${PREFIX.success} tool-result payload cap → ${c.cyan(`${capBytes} bytes`)}`,
118118 );
119119 }
120-
121-export function handleCacheCommand(ctx: CommandContext, args: string): void {
122- const [subcommand, value] = args.trim().split(/\s+/, 2);
123-
124- if (!subcommand) {
125- const geminiCache = ctx.googleCachedContent
126- ? c.cyan(ctx.googleCachedContent)
127- : "off";
128- writeln(
129- `${PREFIX.info} prompt-caching=${ctx.promptCachingEnabled ? c.green("on") : c.dim("off")} openai-retention=${c.cyan(ctx.openaiPromptCacheRetention)} gemini-cache=${geminiCache}`,
130- );
131- writeln(c.dim(" usage: /cache <on|off>"));
132- writeln(c.dim(" /cache openai <in_memory|24h>"));
133- writeln(c.dim(" /cache gemini <off|cachedContents/...>"));
134- return;
135- }
136-
137- if (subcommand === "on" || subcommand === "off") {
138- ctx.setPromptCachingEnabled(subcommand === "on");
139- writeln(
140- `${PREFIX.success} prompt caching → ${subcommand === "on" ? c.green("on") : c.dim("off")}`,
141- );
142- return;
143- }
144-
145- if (subcommand === "openai") {
146- if (value === "in_memory" || value === "24h") {
147- ctx.setOpenAIPromptCacheRetention(value);
148- writeln(
149- `${PREFIX.success} openai prompt cache retention → ${c.cyan(value)}`,
150- );
151- return;
152- }
153- writeln(`${PREFIX.error} usage: /cache openai <in_memory|24h>`);
154- return;
155- }
156-
157- if (subcommand !== "gemini") {
158- writeln(`${PREFIX.error} usage: /cache <on|off|openai|gemini> ...`);
159- return;
160- }
161-
162- if (!value) {
163- writeln(`${PREFIX.error} usage: /cache gemini <off|cachedContents/...>`);
164- return;
165- }
166-
167- if (value === "off") {
168- ctx.setGoogleCachedContent(null);
169- writeln(`${PREFIX.success} gemini cached content → ${c.dim("off")}`);
170- return;
171- }
172-
173- ctx.setGoogleCachedContent(value);
174- writeln(`${PREFIX.success} gemini cached content → ${c.cyan(value)}`);
175-}
src/cli/commands-help.ts+0−1
@@ -32,7 +32,6 @@ export function renderHelpCommand(
3232 ["/reasoning [on|off]", "toggle reasoning display"],
3333 ["/verbose [on|off]", "toggle output truncation"],
3434 ["/context [prune|cap]", "configure pruning and tool-result caps"],
35- ["/cache [...]", "configure provider prompt caching"],
3635 ["/agent [name]", "set or clear the active agent"],
3736 ["/mcp list", "list MCP servers"],
3837 ["/mcp add <n> <t> [u]", "add an MCP server"],
src/cli/commands.ts+0−5
@@ -1,7 +1,6 @@
11 import * as c from "yoctocolors";
22 import { handleAgentCommand } from "./commands-agent.ts";
33 import {
4- handleCacheCommand,
54 handleContextCommand,
65 handleReasoningCommand,
76 handleVerboseCommand,
@@ -105,10 +104,6 @@ export async function handleCommand(
105104 case "context":
106105 handleContextCommand(ctx, args);
107106 return { type: "handled" };
108- case "cache":
109- handleCacheCommand(ctx, args);
110- return { type: "handled" };
111-
112107 case "agent":
113108 handleAgentCommand(ctx, args);
114109 return { type: "handled" };
src/cli/types.ts+0−6
@@ -14,12 +14,6 @@ export interface CommandContext {
1414 setPruningMode: (mode: ContextPruningMode) => void;
1515 toolResultPayloadCapBytes: number;
1616 setToolResultPayloadCapBytes: (bytes: number) => void;
17- promptCachingEnabled: boolean;
18- setPromptCachingEnabled: (enabled: boolean) => void;
19- openaiPromptCacheRetention: "in_memory" | "24h";
20- setOpenAIPromptCacheRetention: (retention: "in_memory" | "24h") => void;
21- googleCachedContent: string | null;
22- setGoogleCachedContent: (contentId: string | null) => void;
2317 undoLastTurn: () => Promise<boolean>;
2418 startNewSession: () => void;
2519
src/index.ts+0−6
@@ -28,10 +28,7 @@ import { autoDiscoverModel } from "./llm-api/providers.ts";
2828
2929 import {
3030 getPreferredContextPruningMode,
31- getPreferredGoogleCachedContent,
3231 getPreferredModel,
33- getPreferredOpenAIPromptCacheRetention,
34- getPreferredPromptCachingEnabled,
3532 getPreferredShowReasoning,
3633 getPreferredThinkingEffort,
3734 getPreferredToolResultPayloadCapBytes,
@@ -66,9 +63,6 @@ function buildAgentOptions(opts: {
6663 initialVerboseOutput: getPreferredVerboseOutput(),
6764 initialPruningMode: getPreferredContextPruningMode(),
6865 initialToolResultPayloadCapBytes: getPreferredToolResultPayloadCapBytes(),
69- initialPromptCachingEnabled: getPreferredPromptCachingEnabled(),
70- initialOpenAIPromptCacheRetention: getPreferredOpenAIPromptCacheRetention(),
71- initialGoogleCachedContent: getPreferredGoogleCachedContent(),
7266 reporter: opts.reporter,
7367 ...(opts.sessionId !== undefined && { sessionId: opts.sessionId }),
7468 };
src/llm-api/provider-options.ts+0−25
@@ -8,13 +8,6 @@ import {
88
99 export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
1010
11-interface CachingOptions {
12- enabled: boolean;
13- openaiRetention?: "in_memory" | "24h";
14- googleCachedContent?: string | null;
15- googleExplicitCachingCompatible?: boolean;
16-}
17-
1811 type CacheFamily = "google" | "anthropic" | "none";
1912
2013 const ANTHROPIC_BUDGET: Record<ThinkingEffort, number> = {
@@ -145,21 +138,3 @@ export function getCacheFamily(modelString: string): CacheFamily {
145138 }
146139 return "none";
147140 }
148-
149-export function getCachingProviderOptions(
150- modelString: string,
151- opts: CachingOptions,
152-): Record<string, unknown> | null {
153- if (!opts.enabled) return null;
154-
155- const family = getCacheFamily(modelString);
156- if (
157- family === "google" &&
158- opts.googleCachedContent &&
159- opts.googleExplicitCachingCompatible !== false
160- ) {
161- return { google: { cachedContent: opts.googleCachedContent } };
162- }
163-
164- return null;
165-}
src/llm-api/providers.test.ts+0−57
@@ -2,7 +2,6 @@ import { describe, expect, test } from "bun:test";
22
33 import {
44 getCacheFamily,
5- getCachingProviderOptions,
65 getThinkingProviderOptions,
76 } from "./provider-options.ts";
87
@@ -63,59 +62,3 @@ describe("getCacheFamily", () => {
6362 expect(getCacheFamily("ollama/llama3")).toBe("none");
6463 });
6564 });
66-
67-describe("getCachingProviderOptions", () => {
68- test("returns null if disabled", () => {
69- expect(
70- getCachingProviderOptions("openai/gpt-4", { enabled: false }),
71- ).toBeNull();
72- });
73-
74- test("returns null for openai (caching is automatic, no hint sent)", () => {
75- expect(
76- getCachingProviderOptions("openai/gpt-4", {
77- enabled: true,
78- openaiRetention: "24h",
79- }),
80- ).toBeNull();
81- });
82-
83- test("returns google cached content when explicit caching is compatible", () => {
84- expect(
85- getCachingProviderOptions("google/gemini-1.5-pro", {
86- enabled: true,
87- googleCachedContent: "cache-123",
88- googleExplicitCachingCompatible: true,
89- }),
90- ).toEqual({
91- google: { cachedContent: "cache-123" },
92- });
93- });
94-
95- test("returns null for google when explicit caching is incompatible", () => {
96- expect(
97- getCachingProviderOptions("google/gemini-1.5-pro", {
98- enabled: true,
99- googleCachedContent: "cache-123",
100- googleExplicitCachingCompatible: false,
101- }),
102- ).toBeNull();
103- });
104-
105- test("returns null for google if no content id", () => {
106- expect(
107- getCachingProviderOptions("google/gemini-1.5-pro", {
108- enabled: true,
109- googleCachedContent: null,
110- }),
111- ).toBeNull();
112- });
113-
114- test("returns null for anthropic (handled via messages)", () => {
115- expect(
116- getCachingProviderOptions("anthropic/claude-3-opus", {
117- enabled: true,
118- }),
119- ).toBeNull();
120- });
121-});
src/llm-api/providers.ts+19−50
@@ -79,45 +79,10 @@ function createFetchWithLogging(): ProviderFetch {
7979
8080 const fetchWithLogging = createFetchWithLogging();
8181
82-/** Betas the AI SDK adds that are not compatible with OAuth / Claude Code identity. */
83-const OAUTH_STRIP_BETAS = new Set(["structured-outputs-2025-11-13"]);
84-
85-/** Strip non-standard fields from the request body that the AI SDK adds but
86- * the Anthropic API doesn't expect (e.g. `caller` on tool_use blocks,
87- * `$schema` on tool input_schema). */
88-function sanitizeOAuthBody(body: string): string {
89- try {
90- const json = JSON.parse(body);
91- // Normalize tool input schemas to only type/properties/required
92- if (Array.isArray(json.tools)) {
93- for (const tool of json.tools) {
94- if (tool.input_schema) {
95- tool.input_schema = {
96- type: "object",
97- properties: tool.input_schema.properties ?? {},
98- required: tool.input_schema.required ?? [],
99- };
100- }
101- }
102- }
103- // Strip non-standard fields from tool_use blocks in messages
104- if (Array.isArray(json.messages)) {
105- for (const msg of json.messages) {
106- if (!Array.isArray(msg.content)) continue;
107- for (const block of msg.content) {
108- if (block.type === "tool_use") {
109- delete block.caller;
110- }
111- }
112- }
113- }
114- return JSON.stringify(json);
115- } catch {
116- return body;
117- }
118-}
82+/** Betas the AI SDK adds that are not compatible with the OAuth endpoint. */
83+const OAUTH_STRIP_BETAS = new Set<string>();
11984
120-function createOAuthFetch(): ProviderFetch {
85+function createOAuthFetch(accessToken: string): ProviderFetch {
12186 const baseFetch = createFetchWithLogging();
12287 const oauthFetch = async (
12388 input: Parameters<ProviderFetch>[0],
@@ -136,15 +101,12 @@ function createOAuthFetch(): ProviderFetch {
136101 .join(",");
137102 h.set("anthropic-beta", filtered);
138103 }
139- // OAuth tokens require Claude Code client identity
104+ h.delete("x-api-key");
105+ h.set("authorization", `Bearer ${accessToken}`);
140106 h.set("user-agent", "claude-cli/2.1.75");
141107 h.set("x-app", "cli");
142108 opts = { ...opts, headers: Object.fromEntries(h.entries()) };
143109 }
144- // Sanitize body to remove non-standard fields
145- if (opts?.body) {
146- opts = { ...opts, body: sanitizeOAuthBody(opts.body.toString()) };
147- }
148110 return baseFetch(input, opts);
149111 };
150112 return oauthFetch as ProviderFetch;
@@ -263,6 +225,19 @@ type AsyncModelResolver = (
263225 let oauthAnthropicCache: { token: string; provider: AnthropicProvider } | null =
264226 null;
265227
228+function createOAuthAnthropicProvider(token: string): AnthropicProvider {
229+ return createAnthropic({
230+ // Use empty apiKey + custom fetch (like opencode) instead of authToken.
231+ // The SDK sends x-api-key for apiKey, which we override to Authorization
232+ // Bearer in the OAuth fetch wrapper.
233+ apiKey: "",
234+ fetch: createOAuthFetch(token),
235+ headers: {
236+ "anthropic-beta": "claude-code-20250219,oauth-2025-04-20",
237+ },
238+ });
239+}
240+
266241 async function resolveAnthropicModel(modelId: string): Promise<LanguageModel> {
267242 // OAuth token takes priority over env var
268243 if (isLoggedIn("anthropic")) {
@@ -271,13 +246,7 @@ async function resolveAnthropicModel(modelId: string): Promise<LanguageModel> {
271246 if (!oauthAnthropicCache || oauthAnthropicCache.token !== token) {
272247 oauthAnthropicCache = {
273248 token,
274- provider: createAnthropic({
275- fetch: createOAuthFetch(),
276- authToken: token,
277- headers: {
278- "anthropic-beta": "claude-code-20250219,oauth-2025-04-20",
279- },
280- }),
249+ provider: createOAuthAnthropicProvider(token),
281250 };
282251 }
283252 return oauthAnthropicCache.provider(modelId);
src/llm-api/turn-context.ts+41−43
@@ -241,52 +241,50 @@ export function compactToolResultPayloads(
241241 return mutated ? compacted : messages;
242242 }
243243
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn-execution.ts+33−0
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn-prepare-messages.ts+13−44
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn-provider-options.ts+2−42
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn-request.test.ts+1−0
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn-request.ts+23−11
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn.test.ts+84−59
Cut short: this commit's diff is over 1000 lines.
src/llm-api/turn.ts+4−12
Cut short: this commit's diff is over 1000 lines.
src/session/db/settings-repo.ts+0−38
Cut short: this commit's diff is over 1000 lines.