@@ -628,7 +628,7 @@ describe("stripOpenAIItemIdsFromHistory", () => {
628628 });
629629
630630 describe("applyContextPruning", () => {
631- test("balanced mode prunes stale tool history", () => {
631+ test("balanced mode does not prune sessions shorter than 40 messages", () => {
632632 const messages: CoreMessage[] = [
633633 { role: "user", content: "u1" },
634634 {
@@ -655,6 +655,41 @@ describe("applyContextPruning", () => {
655655 { role: "user", content: "u4" },
656656 ];
657657
658+ const pruned = applyContextPruning(messages, "balanced");
659+ // 8 messages < 40 threshold — nothing should be pruned
660+ expect(pruned.length).toBe(messages.length);
661+ });
662+
663+ test("balanced mode prunes stale tool history in large sessions", () => {
664+ // Build a history of 50 messages (25 user+assistant pairs with tool calls)
665+ const messages: CoreMessage[] = [];
666+ for (let i = 0; i < 25; i++) {
667+ messages.push({ role: "user", content: `u${i}` });
668+ messages.push({
669+ role: "assistant",
670+ content: [
671+ {
672+ type: "tool-call",
673+ toolCallId: `tc${i}`,
674+ toolName: "read",
675+ input: {},
676+ },
677+ ],
678+ } as unknown as CoreMessage);
679+ messages.push({
680+ role: "tool",
681+ content: [
682+ {
683+ type: "tool-result",
684+ toolCallId: `tc${i}`,
685+ toolName: "read",
686+ output: { text: `result ${i}` },
687+ },
688+ ],
689+ } as unknown as CoreMessage);
690+ }
691+ messages.push({ role: "user", content: "final" });
692+
658693 const pruned = applyContextPruning(messages, "balanced");
659694 expect(pruned.length).toBeLessThan(messages.length);
660695 expect(pruned[pruned.length - 1]?.role).toBe("user");