@@ -801,18 +801,34 @@ export async function* runTurn(options: {
801801 logApiEvent("openai item ids stripped from history", { modelString });
802802 }
803803
804- logApiEvent(
805- "turn context pre-prune",
806- getMessageDiagnostics(openAIItemIdsStrippedMessages),
804+ const prePruneDiagnostics = getMessageDiagnostics(
805+ openAIItemIdsStrippedMessages,
807806 );
807+ logApiEvent("turn context pre-prune", prePruneDiagnostics);
808808 const prunedMessages = applyContextPruning(
809809 openAIItemIdsStrippedMessages,
810810 pruningMode,
811811 );
812- logApiEvent(
813- "turn context post-prune",
814- getMessageDiagnostics(prunedMessages),
815- );
812+ const postPruneDiagnostics = getMessageDiagnostics(prunedMessages);
813+ logApiEvent("turn context post-prune", postPruneDiagnostics);
814+ if (
815+ (pruningMode === "balanced" || pruningMode === "aggressive") &&
816+ (postPruneDiagnostics.messageCount < prePruneDiagnostics.messageCount ||
817+ postPruneDiagnostics.totalBytes < prePruneDiagnostics.totalBytes)
818+ ) {
819+ yield {
820+ type: "context-pruned",
821+ mode: pruningMode,
822+ beforeMessageCount: prePruneDiagnostics.messageCount,
823+ afterMessageCount: postPruneDiagnostics.messageCount,
824+ removedMessageCount:
825+ prePruneDiagnostics.messageCount - postPruneDiagnostics.messageCount,
826+ beforeTotalBytes: prePruneDiagnostics.totalBytes,
827+ afterTotalBytes: postPruneDiagnostics.totalBytes,
828+ removedBytes:
829+ prePruneDiagnostics.totalBytes - postPruneDiagnostics.totalBytes,
830+ };
831+ }
816832
817833 const turnMessages = compactToolResultPayloads(
818834 prunedMessages,