@@ -9,6 +9,10 @@ import {
99 getCompactionProvider ,
1010 type CompactionProvider ,
1111} from "../../plugins/compaction-provider.js" ;
12+ import {
13+ buildHistoryPrunePlanWithWorker ,
14+ computeAdaptiveChunkRatioWithWorker ,
15+ } from "../compaction-planning-worker.js" ;
1216import {
1317 hasMeaningfulConversationContent ,
1418 isRealConversationMessage ,
@@ -19,9 +23,7 @@ import {
1923 SAFETY_MARGIN ,
2024 SUMMARIZATION_OVERHEAD_TOKENS ,
2125 computeAdaptiveChunkRatio ,
22- estimateMessagesTokens ,
2326 isOversizedForSummary ,
24- pruneHistoryForContextShare ,
2527 resolveContextWindowTokens ,
2628 summarizeInStages ,
2729} from "../compaction.js" ;
@@ -1071,19 +1073,18 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
10711073 let droppedSummary : string | undefined ;
10721074
10731075 if ( tokensBefore !== undefined ) {
1074- const summarizableTokens =
1075- estimateMessagesTokens ( messagesToSummarize ) + estimateMessagesTokens ( turnPrefixMessages ) ;
1076- const newContentTokens = Math . max ( 0 , Math . floor ( tokensBefore - summarizableTokens ) ) ;
1077- // Apply SAFETY_MARGIN so token underestimates don't trigger unnecessary pruning
1078- const maxHistoryTokens = Math . floor ( contextWindowTokens * maxHistoryShare * SAFETY_MARGIN ) ;
1079-
1080- if ( newContentTokens > maxHistoryTokens ) {
1081- const pruned = pruneHistoryForContextShare ( {
1082- messages : messagesToSummarize ,
1083- maxContextTokens : contextWindowTokens ,
1084- maxHistoryShare,
1085- parts : 2 ,
1086- } ) ;
1076+ const prunePlan = await buildHistoryPrunePlanWithWorker ( {
1077+ messagesToSummarize,
1078+ turnPrefixMessages,
1079+ tokensBefore,
1080+ contextWindowTokens,
1081+ maxHistoryShare,
1082+ parts : 2 ,
1083+ signal,
1084+ } ) ;
1085+ const { newContentTokens, maxHistoryTokens, pruned } = prunePlan ;
1086+
1087+ if ( newContentTokens > maxHistoryTokens && pruned ) {
10871088 if ( pruned . droppedChunks > 0 ) {
10881089 const newContentRatio = ( newContentTokens / contextWindowTokens ) * 100 ;
10891090 log . warn (
@@ -1097,10 +1098,11 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
10971098 // Summarize dropped messages so context isn't lost
10981099 if ( pruned . droppedMessagesList . length > 0 ) {
10991100 try {
1100- const droppedChunkRatio = computeAdaptiveChunkRatio (
1101- pruned . droppedMessagesList ,
1102- contextWindowTokens ,
1103- ) ;
1101+ const droppedChunkRatio = await computeAdaptiveChunkRatioWithWorker ( {
1102+ messages : pruned . droppedMessagesList ,
1103+ contextWindow : contextWindowTokens ,
1104+ signal,
1105+ } ) ;
11041106 const droppedMaxChunkTokens = Math . max (
11051107 1 ,
11061108 Math . floor ( contextWindowTokens * droppedChunkRatio ) -
@@ -1155,7 +1157,11 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
11551157 // the summarization prompt, system prompt, previous summary, and reasoning budget
11561158 // that generateSummary adds on top of the serialized conversation chunk.
11571159 const allMessages = [ ...messagesToSummarize , ...turnPrefixMessages ] ;
1158- const adaptiveRatio = computeAdaptiveChunkRatio ( allMessages , contextWindowTokens ) ;
1160+ const adaptiveRatio = await computeAdaptiveChunkRatioWithWorker ( {
1161+ messages : allMessages ,
1162+ contextWindow : contextWindowTokens ,
1163+ signal,
1164+ } ) ;
11591165 const maxChunkTokens = Math . max (
11601166 1 ,
11611167 Math . floor ( contextWindowTokens * adaptiveRatio ) - SUMMARIZATION_OVERHEAD_TOKENS ,
0 commit comments