xonecas.io
64875
Mc

mini-coder / commit 1ab7f7e

fix: improve step pruning and cache breakpoints for Anthropic

- Add applyStepPruning that preserves reasoning and anchors tool-call
  window to initial boundary, keeping prefix byte-identical across steps
- Strip stale cache breakpoints before re-annotating to prevent
  accumulation across multi-step tool loops
- Anchor cache breakpoint at last user message for stable cache hits
- Fall back to full pruning past 200 messages
- Update tests and TODO

xonecas2026-03-20 1ab7f7e751310a6cfec5f1db47f8404a86221a11 parent 6643abe

6 files changed

.agents/skills/audit/SKILL.md+1−0
@@ -48,4 +48,5 @@ Write `AUDIT-REPORT.md` in the repo root with these sections:
4848 - **UI/UX Alignment** — comparison of actual output vs mini-coder-idea.md expectations across providers.
4949 - **Recommendations** — split into immediate bugs, code changes, and polish items.
5050
51+Don't just recite the todo items back to the user, report on the audit itself.
5152 Be concise. The report is analysis only — review findings with the user before making any changes.
TODO.md+7−11
@@ -1,22 +1,18 @@
11 # TODO
22
3-- Several truncation helpers accross the cli module files, consolidate in a cli/truncate.ts for rendering truncation.
3+- ~~mini coders uses 10x more tokens than claude using the anthropic subscription; this is likely an issue with caching not working correctly or our pruning resetting the cache.~~ Fixed: step pruning now anchors the tool-call window to the initial boundary (no prefix modification), preserves reasoning between steps, stabilizes cache breakpoints at last-user-message, and strips stale breakpoints. Falls back to full pruning past 200 messages.
44 - CTRL+d hangs sometimes and doesn't exit.
5-- When a shell tool call hits max timeout, the turn ends and the parent terminal is left in a broken state.
6-- **MiniMax empty lines in one-shot** — Investigate empty text deltas. May need to skip rendering deltas that are only whitespace at stream start.
7-- **Double prompt glyph after ESC** — The `turn-complete` handler writes a newline when `!renderedVisibleOutput`, then the input loop also writes its prompt. May need coordination to avoid the double prompt.
8-- **No conversation summary on max context** — The idea says "Stops with a conversation summary if max context is reached." Currently shows an error and suggests `/new` without generating a summary of the conversation.
9-
10----
11-
12-## Polish
13-
14-- **Truncated tool commands** — Long `mc-edit` invocations truncate with `…` mid-argument (e.g. `--new '{…`). Could truncate at argument boundaries for clarity.
155
166 ---
177
188 # Deferred fixes
199
10+- Several truncation helpers accross the cli module files, consolidate in a cli/truncate.ts for rendering truncation.
11+- When a shell tool call hits max timeout, the turn ends and the parent terminal is left in a broken state.
12+- **MiniMax empty lines in one-shot** — Investigate empty text deltas. May need to skip rendering deltas that are only whitespace at stream start.
13+- **Double prompt glyph after ESC** — The `turn-complete` handler writes a newline when `!renderedVisibleOutput`, then the input loop also writes its prompt. May need coordination to avoid the double prompt.
14+- **No conversation summary on max context** — The idea says "Stops with a conversation summary if max context is reached." Currently shows an error and suggests `/new` without generating a summary of the conversation.
15+- **Truncated tool commands** — Long `mc-edit` invocations truncate with `…` mid-argument (e.g. `--new '{…`). Could truncate at argument boundaries for clarity.
2016 - `maxOutputTokens` is hardcoded at 16384 in `turn-request.ts`. Some models support higher output (e.g. Claude Opus 32k). Consider deriving from model info or making it configurable.
2117 - `/_debug` hidden command that snapshots recent logs/db and creates a report in the cwd. For dev mostly but available to all. Do not list it anywhere, only documented here and in the code itself.
2218 - Check this skill to learn about tmux and how we can use it to run manual tests. https://raw.githubusercontent.com/obra/superpowers-lab/refs/heads/main/skills/using-tmux-for-interactive-commands/SKILL.md
src/llm-api/turn-context.ts+103−12
@@ -138,6 +138,46 @@ export function applyContextPruning(messages: CoreMessage[]): CoreMessage[] {
138138 }) as CoreMessage[];
139139 }
140140
141+/**
142+ * Step-level pruning designed to preserve the Anthropic prompt cache.
143+ *
144+ * Two differences from applyContextPruning:
145+ *
146+ * 1. reasoning: "none" — keeps the model's chain-of-thought intact during
147+ * the turn. Without this, reasoning from every previous step is stripped
148+ * (the last message is always a tool result, so "before-last-message"
149+ * exempts nothing), causing the model to lose its plan and loop.
150+ *
151+ * 2. The tool-call window is anchored to the boundary that prepareTurnMessages
152+ * already established. prepareTurnMessages pruned with before-last-40 at
153+ * the initial message count, stripping tool calls from messages 0 through
154+ * (initialCount - 40). By using a window of 40 + newMessageCount, we keep
155+ * that boundary fixed: the initial prefix stays byte-identical across
156+ * steps, so the Anthropic cache hit covers the full conversation history
157+ * up to the user's message.
158+ *
159+ * Falls back to full context pruning when the conversation grows past
160+ * STEP_PRUNE_FALLBACK_THRESHOLD to prevent context-window overflow on
161+ * very long turns.
162+ */
163+const STEP_PRUNE_FALLBACK_THRESHOLD = 200;
164+
165+export function applyStepPruning(
166+ messages: CoreMessage[],
167+ initialMessageCount: number,
168+): CoreMessage[] {
169+ if (messages.length > STEP_PRUNE_FALLBACK_THRESHOLD) {
170+ return applyContextPruning(messages);
171+ }
172+ const newMessageCount = Math.max(0, messages.length - initialMessageCount);
173+ return pruneMessages({
174+ messages,
175+ reasoning: "none",
176+ toolCalls: `before-last-${40 + newMessageCount}-messages`,
177+ emptyMessages: "remove",
178+ }) as CoreMessage[];
179+}
180+
141181 function compactHeadTail(
142182 serialized: string,
143183 maxChars = 4096,
@@ -223,6 +263,33 @@ export function compactToolResultPayloads(
223263 return mutated ? compacted : messages;
224264 }
225265
266+/**
267+ * Strip any existing Anthropic cache breakpoints from a message so we can
268+ * re-annotate cleanly without accumulating stale markers across steps.
269+ */
270+function stripCacheBreakpoint(msg: CoreMessage): CoreMessage {
271+ const anthropic = msg?.providerOptions?.anthropic as
272+ | Record<string, unknown>
273+ | undefined;
274+ if (!anthropic?.cacheControl) return msg;
275+
276+ const { cacheControl: _, ...rest } = anthropic;
277+ const hasOtherKeys = Object.keys(rest).length > 0;
278+ const { anthropic: __, ...otherProviders } = msg.providerOptions as Record<
279+ string,
280+ unknown
281+ >;
282+ const hasOtherProviders = Object.keys(otherProviders).length > 0;
283+
284+ return {
285+ ...msg,
286+ providerOptions: {
287+ ...(hasOtherProviders ? otherProviders : {}),
288+ ...(hasOtherKeys ? { anthropic: rest } : {}),
289+ },
290+ } as CoreMessage;
291+}
292+
226293 function withCacheBreakpoint(msg: CoreMessage): CoreMessage {
227294 return {
228295 ...msg,
@@ -243,28 +310,52 @@ function withCacheBreakpoint(msg: CoreMessage): CoreMessage {
243310 * Anthropic allows max 4 breakpoints per request. We reserve 1 for tool
244311 * caching (see annotateToolCaching), leaving 3 for messages:
245312 * - First system message (stable prefix)
246- * - Last 2 non-system messages (moving tail)
313+ * - Last user message (stable within a turn — anchors the cached prefix)
314+ * - Last non-system message (moving tail)
315+ *
316+ * By anchoring a breakpoint at the last user message, the full conversation
317+ * prefix up to the user's input is cached and reused across multi-step tool
318+ * loops. Only the new assistant/tool messages after it need re-processing.
247319 */
248320 export function annotateAnthropicCacheBreakpoints(
249321 prompt: CoreMessage[],
250322 ): CoreMessage[] {
251- const result = [...prompt];
252- const systemIdxs: number[] = [];
253- const nonSystemIdxs: number[] = [];
323+ // Strip existing breakpoints to prevent accumulation across steps
324+ const result = prompt.map(stripCacheBreakpoint);
325+
326+ let firstSystemIdx = -1;
327+ let lastUserIdx = -1;
328+ let lastNonSystemIdx = -1;
254329
255330 for (let i = 0; i < result.length; i++) {
256- if (result[i]?.role === "system") systemIdxs.push(i);
257- else nonSystemIdxs.push(i);
331+ const role = result[i]?.role;
332+ if (role === "system") {
333+ if (firstSystemIdx === -1) firstSystemIdx = i;
334+ } else {
335+ lastNonSystemIdx = i;
336+ if (role === "user") lastUserIdx = i;
337+ }
338+ }
339+
340+ // Annotate first system message (stable prefix)
341+ if (firstSystemIdx >= 0) {
342+ result[firstSystemIdx] = withCacheBreakpoint(
343+ result[firstSystemIdx] as CoreMessage,
344+ );
258345 }
259346
260- // Annotate first system message
261- for (const idx of systemIdxs.slice(0, 1)) {
262- result[idx] = withCacheBreakpoint(result[idx] as CoreMessage);
347+ // Annotate last user message (stable within a turn)
348+ if (lastUserIdx >= 0) {
349+ result[lastUserIdx] = withCacheBreakpoint(
350+ result[lastUserIdx] as CoreMessage,
351+ );
263352 }
264353
265- // Annotate last 2 non-system messages
266- for (const idx of nonSystemIdxs.slice(-2)) {
267- result[idx] = withCacheBreakpoint(result[idx] as CoreMessage);
354+ // Annotate last non-system message (moving tail) — skip if same as lastUserIdx
355+ if (lastNonSystemIdx >= 0 && lastNonSystemIdx !== lastUserIdx) {
356+ result[lastNonSystemIdx] = withCacheBreakpoint(
357+ result[lastNonSystemIdx] as CoreMessage,
358+ );
268359 }
269360
270361 return result;
src/llm-api/turn-request.test.ts+74−42
@@ -1,5 +1,6 @@
11 import { describe, expect, test } from "bun:test";
22 import type { CoreMessage } from "./turn.ts";
3+import { applyContextPruning } from "./turn-context.ts";
34 import { buildStreamTextRequest } from "./turn-request.ts";
45
56 function makePrepared(messages: CoreMessage[]) {
@@ -108,29 +109,52 @@ describe("buildStreamTextRequest", () => {
108109 expect(result).toEqual({});
109110 });
110111
111- test("prepareStep prunes tool history at step 1+", () => {
112- // 30 tool-call/result pairs = 91 messages (30*3 + 1 final user)
113- const messages = buildToolHistory(30);
114- const request = buildStreamTextRequest(makeDefaultInput(messages));
115-
116- const result = callPrepareStep(request, 1, messages);
117-
118- // pruneMessages with "before-last-40-messages" should remove old tool
119- // history, resulting in fewer messages than the original 91
120- expect(result.messages).toBeDefined();
121- expect(result.messages?.length).toBeLessThan(messages.length);
122- });
112+ test("prepareStep preserves initial prefix at step 1+ (cache-friendly)", () => {
113+ // Simulate the real flow: prepareTurnMessages prunes first, then
114+ // prepareStep runs on the pruned messages + new step responses.
115+ const raw = buildToolHistory(30); // 91 messages
116+ const prePruned = applyContextPruning(raw); // simulates prepareTurnMessages
117+ const request = buildStreamTextRequest(makeDefaultInput(prePruned));
123118
124- test("prepareStep does not prune short conversations at step 1+", () => {
125- // 5 pairs = 16 messages, well under the 40-message threshold
126- const messages = buildToolHistory(5);
127- const request = buildStreamTextRequest(makeDefaultInput(messages));
119+ // Simulate step 1: pre-pruned messages + new tool interaction
120+ const withNewMessages = [
121+ ...prePruned,
122+ {
123+ role: "assistant",
124+ content: [
125+ {
126+ type: "tool-call",
127+ toolCallId: "new1",
128+ toolName: "shell",
129+ input: {},
130+ },
131+ ],
132+ } as unknown as CoreMessage,
133+ {
134+ role: "tool",
135+ content: [
136+ {
137+ type: "tool-result",
138+ toolCallId: "new1",
139+ toolName: "shell",
140+ output: { text: "new-result" },
141+ },
142+ ],
143+ } as unknown as CoreMessage,
144+ ];
128145
129- const result = callPrepareStep(request, 1, messages);
146+ const result = callPrepareStep(request, 1, withNewMessages);
130147
131- // Short conversations should keep all messages
148+ // Adjusted window covers everything — no messages removed
132149 expect(result.messages).toBeDefined();
133- expect(result.messages?.length).toBe(messages.length);
150+ expect(result.messages?.length).toBe(withNewMessages.length);
151+
152+ // Initial prefix is byte-identical (cache-friendly)
153+ for (let i = 0; i < prePruned.length; i++) {
154+ expect(JSON.stringify(result.messages?.[i])).toBe(
155+ JSON.stringify(prePruned[i]),
156+ );
157+ }
134158 });
135159
136160 test("prepareStep adds cache breakpoints for Anthropic models", () => {
@@ -155,49 +179,57 @@ describe("buildStreamTextRequest", () => {
155179 expect(withCache.length).toBeGreaterThan(0);
156180 });
157181
158- test("prepareStep prunes consistently across multiple steps", () => {
159- // Simulate growing context across steps
160- const base = buildToolHistory(30); // 91 messages
182+ test("prepareStep falls back to full pruning on very long turns", () => {
183+ // Build a history that exceeds the step prune fallback threshold (200)
184+ const huge = buildToolHistory(70); // 70*3 + 1 = 211 messages
185+ const request = buildStreamTextRequest(makeDefaultInput(huge));
186+
187+ const result = callPrepareStep(request, 1, huge);
188+
189+ // Full context pruning kicks in — old tool calls removed
190+ expect(result.messages).toBeDefined();
191+ expect(result.messages?.length).toBeLessThan(huge.length);
192+ });
193+
194+ test("prepareStep preserves messages across growing steps", () => {
195+ // Pre-prune to simulate real prepareTurnMessages flow
196+ const raw = buildToolHistory(20); // 61 messages
197+ const base = applyContextPruning(raw);
161198 const request = buildStreamTextRequest(makeDefaultInput(base));
162199
163- // Step 1: prune from 91 messages
200+ // Step 1: same as initial
164201 const result1 = callPrepareStep(request, 1, base);
165202
166- // Step 5: add more messages (simulating accumulated tool calls)
167- const grown = [
168- ...base,
169- {
203+ // Step 5: 10 new messages appended
204+ const grown = [...base];
205+ for (let i = 0; i < 5; i++) {
206+ grown.push({
170207 role: "assistant",
171208 content: [
172209 {
173210 type: "tool-call",
174- toolCallId: "extra1",
211+ toolCallId: `extra${i}`,
175212 toolName: "shell",
176213 input: {},
177214 },
178215 ],
179- } as unknown as CoreMessage,
180- {
216+ } as unknown as CoreMessage);
217+ grown.push({
181218 role: "tool",
182219 content: [
183220 {
184221 type: "tool-result",
185- toolCallId: "extra1",
222+ toolCallId: `extra${i}`,
186223 toolName: "shell",
187- output: { text: "extra" },
224+ output: { text: `extra-${i}` },
188225 },
189226 ],
190- } as unknown as CoreMessage,
191- ];
227+ } as unknown as CoreMessage);
228+ }
192229 const result5 = callPrepareStep(request, 5, grown);
193230
194- // Both should prune, and the larger input should not produce a much
195- // larger output — pruning keeps the tail window roughly constant
196- const len1 = result1.messages?.length ?? 0;
197- const len5 = result5.messages?.length ?? 0;
198- expect(len1).toBeLessThan(base.length);
199- expect(len5).toBeLessThan(grown.length);
200- // The pruned sizes should be similar (within a small window)
201- expect(Math.abs(len1 - len5)).toBeLessThanOrEqual(4);
231+ // Adjusted window preserves everything
232+ expect(result1.messages?.length).toBe(base.length);
233+ expect(result5.messages?.length).toBe(grown.length);
202234 });
203235 });
src/llm-api/turn-request.ts+10−4
@@ -4,7 +4,7 @@ import { isAnthropicModelFamily } from "./model-routing.ts";
44 import type { ThinkingEffort } from "./provider-options.ts";
55 import {
66 annotateAnthropicCacheBreakpoints,
7- applyContextPruning,
7+ applyStepPruning,
88 compactToolResultPayloads,
99 } from "./turn-context.ts";
1010 import { prepareTurnMessages } from "./turn-prepare-messages.ts";
@@ -64,6 +64,7 @@ export function buildStreamTextRequest(
6464 // Runs on CoreMessages before SDK conversion, so pruneMessages operates
6565 // on the correct types and message boundaries.
6666 const isAnthropic = isAnthropicModelFamily(input.modelString);
67+ const initialMessageCount = input.prepared.messages.length;
6768
6869 return {
6970 model: input.model,
@@ -73,8 +74,8 @@ export function buildStreamTextRequest(
7374 stopWhen: continueUntilModelStops,
7475 onStepFinish: input.onStepFinish,
7576 prepareStep: ({ stepNumber, messages }) => {
76- // Step 0 messages are already pruned by prepareTurnMessages;
77- // only apply cache breakpoints.
77+ // Step 0: already pruned by prepareTurnMessages, only annotate
78+ // cache breakpoints.
7879 if (stepNumber === 0) {
7980 return isAnthropic
8081 ? {
@@ -84,7 +85,12 @@ export function buildStreamTextRequest(
8485 }
8586 : {};
8687 }
87- const pruned = applyContextPruning(messages as CoreMessage[]);
88+ // Steps 1+: use step-aware pruning that preserves the cached
89+ // prefix and keeps the model's chain-of-thought.
90+ const pruned = applyStepPruning(
91+ messages as CoreMessage[],
92+ initialMessageCount,
93+ );
8894 const compacted = compactToolResultPayloads(pruned);
8995 const final = isAnthropic
9096 ? annotateAnthropicCacheBreakpoints(compacted)
src/llm-api/turn.test.ts+167−6
@@ -12,6 +12,7 @@ import type { CoreMessage } from "./turn.ts";
1212 import {
1313 annotateAnthropicCacheBreakpoints,
1414 applyContextPruning,
15+ applyStepPruning,
1516 compactToolResultPayloads,
1617 getMessageDiagnostics,
1718 } from "./turn-context.ts";
@@ -726,6 +727,161 @@ describe("applyContextPruning", () => {
726727 });
727728 });
728729
730+describe("applyStepPruning", () => {
731+ /** Build N tool-call/result triplets + a final user message. */
732+ function buildHistory(pairCount: number): CoreMessage[] {
733+ const messages: CoreMessage[] = [];
734+ for (let i = 0; i < pairCount; i++) {
735+ messages.push({ role: "user", content: `u${i}` });
736+ messages.push({
737+ role: "assistant",
738+ content: [
739+ {
740+ type: "tool-call",
741+ toolCallId: `tc${i}`,
742+ toolName: "shell",
743+ input: {},
744+ },
745+ ],
746+ } as unknown as CoreMessage);
747+ messages.push({
748+ role: "tool",
749+ content: [
750+ {
751+ type: "tool-result",
752+ toolCallId: `tc${i}`,
753+ toolName: "shell",
754+ output: { text: `r${i}` },
755+ },
756+ ],
757+ } as unknown as CoreMessage);
758+ }
759+ messages.push({ role: "user", content: "final" });
760+ return messages;
761+ }
762+
763+ test("preserves all messages when within threshold", () => {
764+ // Pre-prune to simulate real prepareTurnMessages flow
765+ const raw = buildHistory(20); // 61 messages
766+ const initial = applyContextPruning(raw);
767+ // Simulate 2 new messages from a tool call
768+ const step2 = [
769+ ...initial,
770+ {
771+ role: "assistant",
772+ content: [
773+ {
774+ type: "tool-call",
775+ toolCallId: "new1",
776+ toolName: "shell",
777+ input: {},
778+ },
779+ ],
780+ } as unknown as CoreMessage,
781+ {
782+ role: "tool",
783+ content: [
784+ {
785+ type: "tool-result",
786+ toolCallId: "new1",
787+ toolName: "shell",
788+ output: { text: "new" },
789+ },
790+ ],
791+ } as unknown as CoreMessage,
792+ ];
793+
794+ const pruned = applyStepPruning(step2, initial.length);
795+ // Adjusted window covers everything — no messages removed
796+ expect(pruned.length).toBe(step2.length);
797+ });
798+
799+ test("preserves reasoning (does not strip chain-of-thought)", () => {
800+ const messages: CoreMessage[] = [
801+ { role: "user", content: "plan" },
802+ {
803+ role: "assistant",
804+ content: [
805+ { type: "reasoning", text: "I should read the file first" },
806+ {
807+ type: "tool-call",
808+ toolCallId: "tc1",
809+ toolName: "shell",
810+ input: {},
811+ },
812+ ],
813+ } as unknown as CoreMessage,
814+ {
815+ role: "tool",
816+ content: [
817+ {
818+ type: "tool-result",
819+ toolCallId: "tc1",
820+ toolName: "shell",
821+ output: { text: "file contents" },
822+ },
823+ ],
824+ } as unknown as CoreMessage,
825+ ];
826+
827+ const pruned = applyStepPruning(messages, messages.length);
828+ const assistantContent = pruned[1]?.content as Array<{
829+ type: string;
830+ text?: string;
831+ }>;
832+ const reasoning = assistantContent.find((p) => p.type === "reasoning");
833+ expect(reasoning).toBeDefined();
834+ expect(reasoning?.text).toBe("I should read the file first");
835+ });
836+
837+ test("falls back to full pruning past threshold", () => {
838+ const huge = buildHistory(70); // 211 messages > 200 threshold
839+ const pruned = applyStepPruning(huge, huge.length);
840+ // Full context pruning removes old tool calls
841+ expect(pruned.length).toBeLessThan(huge.length);
842+ });
843+
844+ test("initial prefix is byte-identical after step pruning", () => {
845+ const initial = buildHistory(25); // 76 messages
846+ // Apply turn-boundary pruning (simulates prepareTurnMessages)
847+ const turnPruned = applyContextPruning(initial);
848+
849+ // Simulate step 1: append new messages
850+ const step1 = [
851+ ...turnPruned,
852+ {
853+ role: "assistant",
854+ content: [
855+ {
856+ type: "tool-call",
857+ toolCallId: "new1",
858+ toolName: "shell",
859+ input: {},
860+ },
861+ ],
862+ } as unknown as CoreMessage,
863+ {
864+ role: "tool",
865+ content: [
866+ {
867+ type: "tool-result",
868+ toolCallId: "new1",
869+ toolName: "shell",
870+ output: { text: "new" },
871+ },
872+ ],
873+ } as unknown as CoreMessage,
874+ ];
875+
876+ const stepPruned = applyStepPruning(step1, turnPruned.length);
877+
878+ // The initial prefix (turnPruned messages) must be identical
879+ for (let i = 0; i < turnPruned.length; i++) {
880+ expect(JSON.stringify(stepPruned[i])).toBe(JSON.stringify(turnPruned[i]));
881+ }
882+ });
883+});
884+
729885 describe("getMessageDiagnostics", () => {
730886 test("aggregates role and tool-result byte stats", () => {
731887 const messages: CoreMessage[] = [
@@ -813,7 +969,7 @@ describe("annotateAnthropicCacheBreakpoints", () => {
813969 expect(annotated[1]?.providerOptions).toEqual(cacheOpts);
814970 });
815971
816- test("annotates last 2 non-system messages", () => {
972+ test("annotates last user message and last non-system message", () => {
817973 const messages: CoreMessage[] = [
818974 { role: "user", content: "m1" },
819975 { role: "assistant", content: "m2" },
@@ -822,13 +978,17 @@ describe("annotateAnthropicCacheBreakpoints", () => {
822978 ];
823979 const annotated = annotateAnthropicCacheBreakpoints(messages);
824980
981+ // m1: not last user, not last non-system
825982 expect(annotated[0]?.providerOptions).toBeUndefined();
983+ // m2: assistant, not last
826984 expect(annotated[1]?.providerOptions).toBeUndefined();
985+ // m3: last user message — breakpoint
827986 expect(annotated[2]?.providerOptions).toEqual(cacheOpts);
987+ // m4: last non-system message — breakpoint
828988 expect(annotated[3]?.providerOptions).toEqual(cacheOpts);
829989 });
830990
831- test("annotates both system and last 2 non-system messages", () => {
991+ test("annotates system, last user, and last non-system messages", () => {
832992 const messages: CoreMessage[] = [
833993 { role: "system", content: "System rules" },
834994 { role: "user", content: "m1" },
@@ -837,12 +997,13 @@ describe("annotateAnthropicCacheBreakpoints", () => {
837997 ];
838998 const annotated = annotateAnthropicCacheBreakpoints(messages);
839999
840- // system
1000+ // system — breakpoint
8411001 expect(annotated[0]?.providerOptions).toEqual(cacheOpts);
842- // first non-system — not in last 2
1002+ // m1: not last user
8431003 expect(annotated[1]?.providerOptions).toBeUndefined();
844- // last 2 non-system
845- expect(annotated[2]?.providerOptions).toEqual(cacheOpts);
1004+ // m2: not last non-system
1005+ expect(annotated[2]?.providerOptions).toBeUndefined();
1006+ // m3: last user AND last non-system — single breakpoint
8461007 expect(annotated[3]?.providerOptions).toEqual(cacheOpts);
8471008 });