xonecas.io
64875
Mc

mini-coder / commit 166bb23

fix(pruning): increase toolCalls window to preserve multi-turn continuity

balanced: before-last-3 → before-last-40-messages
aggressive: before-last-2 → before-last-20-messages

The old thresholds guaranteed exactly 1 tool result survived (balanced)
or zero (aggressive) on every pruned turn, regardless of session size.
Sessions under 40 messages are now never pruned by balanced mode.

xonecas2026-03-13 166bb2315e599f60475960c2a13be9840e930cd2 parent 01f2bea

2 files changed

src/llm-api/turn.test.ts+36−1
@@ -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");
src/llm-api/turn.ts+2−2
@@ -150,7 +150,7 @@ export function applyContextPruning(
150150 return pruneMessages({
151151 messages,
152152 reasoning: "before-last-message",
153- toolCalls: "before-last-2-messages",
153+ toolCalls: "before-last-20-messages",
154154 emptyMessages: "remove",
155155 }) as CoreMessage[];
156156 }
@@ -158,7 +158,7 @@ export function applyContextPruning(
158158 return pruneMessages({
159159 messages,
160160 reasoning: "before-last-message",
161- toolCalls: "before-last-3-messages",
161+ toolCalls: "before-last-40-messages",
162162 emptyMessages: "remove",
163163 }) as CoreMessage[];
164164 }