@@ -28,7 +28,7 @@ impl Drop for PrefetchObservation {
2828}
2929
3030pub ( super ) struct PrefetchedCompression {
31- task : CompressionPrefetch < CompressionCandidate , OpenBitFunError > ,
31+ task : CompressionPrefetch < Option < CompressionCandidate > , OpenBitFunError > ,
3232 source_message_ids : Vec < String > ,
3333 request_identity : serde_json:: Value ,
3434 observation : PrefetchObservation ,
@@ -38,7 +38,7 @@ impl PrefetchedCompression {
3838 pub ( super ) fn claim (
3939 mut self ,
4040 input : & CompressionModelSummaryInput < ' _ > ,
41- ) -> PrefetchClaim < CompressionCandidate , OpenBitFunError > {
41+ ) -> PrefetchClaim < Option < CompressionCandidate > , OpenBitFunError > {
4242 let latest = ContextCompressor :: canonical_conversation ( input. runtime_messages ) ;
4343 self . observation . claimed = true ;
4444 let request_matches = self . request_identity == CompressionJob :: request_identity ( input) ;
@@ -119,8 +119,14 @@ pub(super) struct CompressionJob {
119119}
120120
121121impl CompressionJob {
122- pub ( super ) fn spawn_prefetch ( self , parent : & CancellationToken ) -> PrefetchedCompression {
123- self . spawn_prefetch_with ( parent, |job| job. run ( ) )
122+ pub ( super ) fn spawn_prefetch (
123+ self ,
124+ parent : & CancellationToken ,
125+ contract : Option < crate :: agentic:: core:: CompressionContract > ,
126+ ) -> PrefetchedCompression {
127+ self . spawn_prefetch_with ( parent, move |job| async move {
128+ job. run_automatic ( contract. as_ref ( ) ) . await
129+ } )
124130 }
125131
126132 pub ( super ) fn spawn_prefetch_with < F , Fut > (
@@ -130,7 +136,9 @@ impl CompressionJob {
130136 ) -> PrefetchedCompression
131137 where
132138 F : FnOnce ( Self ) -> Fut + Send + ' static ,
133- Fut : std:: future:: Future < Output = OpenBitFunResult < CompressionCandidate > > + Send + ' static ,
139+ Fut : std:: future:: Future < Output = OpenBitFunResult < Option < CompressionCandidate > > >
140+ + Send
141+ + ' static ,
134142 {
135143 let source_message_ids = ContextCompressor :: canonical_conversation ( & self . messages )
136144 . map ( |message| message. id . clone ( ) )
@@ -162,7 +170,7 @@ impl CompressionJob {
162170 info ! (
163171 "Compression prefetch finished: prefetch_id={}, session_id={}, turn_id={}, outcome={}, duration_ms={}" ,
164172 id, session_id, turn_id,
165- if result. is_ok ( ) { "success" } else { "failed" } ,
173+ match & result { Ok ( Some ( _ ) ) => "ready" , Ok ( None ) => "no_gain" , Err ( _ ) => "failed" } ,
166174 elapsed_ms_u64( started_at)
167175 ) ;
168176 result
@@ -246,7 +254,24 @@ impl CompressionJob {
246254 . await
247255 }
248256
249- pub ( super ) async fn run ( self ) -> OpenBitFunResult < CompressionCandidate > {
257+ pub ( super ) async fn run_manual ( self ) -> OpenBitFunResult < CompressionCandidate > {
258+ self . prepare ( false , None ) . await ?. ok_or_else ( || {
259+ OpenBitFunError :: AIClient ( "Context compression has no eligible plan" . into ( ) )
260+ } )
261+ }
262+
263+ pub ( super ) async fn run_automatic (
264+ self ,
265+ contract : Option < & crate :: agentic:: core:: CompressionContract > ,
266+ ) -> OpenBitFunResult < Option < CompressionCandidate > > {
267+ self . prepare ( true , contract) . await
268+ }
269+
270+ async fn prepare (
271+ self ,
272+ require_savings : bool ,
273+ contract : Option < & crate :: agentic:: core:: CompressionContract > ,
274+ ) -> OpenBitFunResult < Option < CompressionCandidate > > {
250275 let session_id = self . session_id . as_str ( ) ;
251276 let dialog_turn_id = self . turn_id . as_str ( ) ;
252277 let runtime_messages = self . messages . as_slice ( ) ;
@@ -265,9 +290,7 @@ impl CompressionJob {
265290 recent_target,
266291 ) ?
267292 else {
268- return Err ( OpenBitFunError :: AIClient (
269- "Context compression has no eligible plan" . to_string ( ) ,
270- ) ) ;
293+ return Ok ( None ) ;
271294 } ;
272295 if previous_cutoff. is_some_and ( |cutoff| plan. cutoff_message_index >= cutoff) {
273296 return Err ( OpenBitFunError :: AIClient (
@@ -291,15 +314,37 @@ impl CompressionJob {
291314 plan. recent_tail_messages. len( )
292315 ) ;
293316
317+ if require_savings {
318+ // The leading system prompt, tools and prepended reminders are
319+ // common to both requests. Compare only the replaceable history.
320+ let system_count = runtime_messages
321+ . iter ( )
322+ . take_while ( |message| message. role == MessageRole :: System )
323+ . count ( ) ;
324+ let original = ExecutionEngine :: estimate_request_tokens_internal (
325+ & runtime_messages[ system_count..] ,
326+ None ,
327+ ) ;
328+ let minimum = self
329+ . compressor
330+ . minimum_replacement ( & plan, contract. cloned ( ) ) ;
331+ let floor =
332+ ExecutionEngine :: estimate_request_tokens_internal ( & minimum. messages , None ) ;
333+ if floor >= original {
334+ debug ! ( "Skipping compression summary: session_id={}, history_tokens={}, minimum_replacement_tokens={}" , session_id, original, floor) ;
335+ return Ok ( None ) ;
336+ }
337+ }
338+
294339 let summary_result = self . request_summary ( & plan. summary_request_messages ) . await ;
295340
296341 match summary_result {
297342 Ok ( summary) => {
298- return Ok ( CompressionCandidate {
343+ return Ok ( Some ( CompressionCandidate {
299344 plan,
300345 summary,
301346 request_identity : self . request_identity . clone ( ) ,
302- } ) ;
347+ } ) ) ;
303348 }
304349 Err ( err) if err. is_recoverable_context_overflow ( ) => {
305350 warn ! (
0 commit comments