xonecas.io
64875
Mc

mini-coder / commit 9d2a780

fix: make context-pruned events accurate and consistent

Two issues with context-pruned notifications:

1. Spurious events: pruneMessages() from the AI SDK strips reasoning
   parts (via reasoning: 'before-last-message'), reducing totalBytes
   without removing messages. The wasPruned check compared bytes,
   causing 'context pruned –0 messages' on every turn with reasoning.
   Now wasPruned only checks messageCount.

2. Missing events: during multi-step tool loops, applyStepPruning runs
   in the SDK's prepareStep callback (synchronous, returns messages)
   with no way to emit events. Added a StepPruneRecord queue shared
   between prepareStep (producer) and mapFullStreamToTurnEvents
   (consumer), which drains it on each start-step chunk to yield
   context-pruned events inline with the stream.

xonecas2026-03-20 9d2a780fdfd820f3dcd656db14593bea5b3e41ac parent 8a3e281

5 files changed

src/llm-api/turn-execution.ts+15−0
@@ -356,11 +356,26 @@ export async function* mapFullStreamToTurnEvents(
356356 stream: AsyncIterable<{ type?: string; [key: string]: unknown }>,
357357 opts: {
358358 onChunk?: (chunk: { type?: string; [key: string]: unknown }) => void;
359+ stepPruneQueue?: {
360+ removedMessageCount: number;
361+ removedBytes: number;
362+ beforeMessageCount: number;
363+ afterMessageCount: number;
364+ beforeTotalBytes: number;
365+ afterTotalBytes: number;
366+ }[];
359367 },
360368 ): AsyncGenerator<TurnEvent> {
361369 const toolCallTracker = new StreamToolCallTracker();
362370 const textPhaseTracker = new StreamTextPhaseTracker();
363371 for await (const originalChunk of stream) {
372+ // Drain step-pruning records before each new step so the user sees
373+ // the pruning notification before the step's output begins.
374+ if (originalChunk.type === "start-step" && opts.stepPruneQueue) {
375+ for (const rec of opts.stepPruneQueue.splice(0)) {
376+ yield { type: "context-pruned", ...rec };
377+ }
378+ }
364379 const prepared = toolCallTracker.prepare(originalChunk);
365380 const chunk = prepared.chunk;
366381 const route = textPhaseTracker.route(chunk);
src/llm-api/turn-prepare-messages.ts+1−3
@@ -127,9 +127,7 @@ export function prepareTurnMessages(input: {
127127 finalMessages = [...systemMessages, ...finalMessages];
128128 }
129129
130- const wasPruned =
131- postStats.messageCount < preStats.messageCount ||
132- postStats.totalBytes < preStats.totalBytes;
130+ const wasPruned = postStats.messageCount < preStats.messageCount;
133131
134132 return {
135133 messages: finalMessages,
src/llm-api/turn-request.test.ts+1−0
@@ -27,6 +27,7 @@ function makeDefaultInput(
2727 onStepFinish: () => {},
2828 signal: undefined,
2929 providerOptions: {},
30+ stepPruneQueue: [],
3031 };
3132 }
3233
src/llm-api/turn-request.ts+25−0
@@ -6,10 +6,20 @@ import {
66 annotateAnthropicCacheBreakpoints,
77 applyStepPruning,
88 compactToolResultPayloads,
9+ getMessageStats,
910 } from "./turn-context.ts";
1011 import { prepareTurnMessages } from "./turn-prepare-messages.ts";
1112 import { buildTurnProviderOptions } from "./turn-provider-options.ts";
1213
14+export interface StepPruneRecord {
15+ removedMessageCount: number;
16+ removedBytes: number;
17+ beforeMessageCount: number;
18+ afterMessageCount: number;
19+ beforeTotalBytes: number;
20+ afterTotalBytes: number;
21+}
22+
1323 type StreamTextOptions = Parameters<typeof streamText>[0];
1424 type CoreMessage = NonNullable<StreamTextOptions["messages"]>[number];
1525 type CoreModel = StreamTextOptions["model"];
@@ -54,6 +64,7 @@ interface BuildStreamTextRequestInput {
5464 onStepFinish: NonNullable<StreamTextOptions["onStepFinish"]>;
5565 signal: AbortSignal | undefined;
5666 providerOptions: Record<string, unknown>;
67+ stepPruneQueue: StepPruneRecord[];
5768 }
5869
5970 export function buildStreamTextRequest(
@@ -87,10 +98,24 @@ export function buildStreamTextRequest(
8798 }
8899 // Steps 1+: use step-aware pruning that preserves the cached
89100 // prefix and keeps the model's chain-of-thought.
101+ const preCount = messages.length;
90102 const pruned = applyStepPruning(
91103 messages as CoreMessage[],
92104 initialMessageCount,
93105 );
106+ const postCount = pruned.length;
107+ if (postCount < preCount) {
108+ const pre = getMessageStats(messages as CoreMessage[]);
109+ const post = getMessageStats(pruned);
110+ input.stepPruneQueue.push({
111+ beforeMessageCount: pre.messageCount,
112+ afterMessageCount: post.messageCount,
113+ removedMessageCount: pre.messageCount - post.messageCount,
114+ beforeTotalBytes: pre.totalBytes,
115+ afterTotalBytes: post.totalBytes,
116+ removedBytes: pre.totalBytes - post.totalBytes,
117+ });
118+ }
94119 const compacted = compactToolResultPayloads(pruned);
95120 const final = isAnthropic
96121 ? annotateAnthropicCacheBreakpoints(compacted)
src/llm-api/turn.ts+5−0
@@ -12,6 +12,7 @@ import {
1212 import {
1313 buildStreamTextRequest,
1414 buildTurnPreparation,
15+ type StepPruneRecord,
1516 } from "./turn-request.ts";
1617
1718 import type { ToolDef, TurnEvent } from "./types.ts";
@@ -95,6 +96,8 @@ export async function* runTurn(options: {
9596 });
9697 }
9798
99+ const stepPruneQueue: StepPruneRecord[] = [];
100+
98101 const result = streamText(
99102 buildStreamTextRequest({
100103 model,
@@ -104,11 +107,13 @@ export async function* runTurn(options: {
104107 onStepFinish: turnState.onStepFinish,
105108 signal,
106109 providerOptions: providerOptionsResult.providerOptions,
110+ stepPruneQueue,
107111 }),
108112 ) as StreamTextResultFull;
109113 result.response.catch(() => {});
110114
111115 for await (const event of mapFullStreamToTurnEvents(result.fullStream, {
116+ stepPruneQueue,
112117 onChunk: (streamChunk) => {
113118 logApiEvent("stream chunk", {
114119 type: streamChunk.type,