@@ -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 });