everruns-engine 0.18.4

Shared Input/Reason/Act execution and sans-IO turn planning for Everruns hosts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use uuid::Uuid;

use crate::compact::{CompactRequest, messages_to_compact_input};
use crate::driver_registry::{LlmMessage, LlmMessageContent, LlmMessageRole};
use crate::error::{AgentLoopError, Result};
use crate::event_emitter::EventEmitter;
use crate::events::{
    CompactionReason, CompactionStepData, ContextCompactedData, ContextCompactingData,
    EventContext, EventRequest, LlmCompactionInfo, TokenUsage,
};
use crate::typed_id::SessionId;

pub(super) const CHECKPOINT_REARM_MIN_SUFFIX_MESSAGES: usize = 4;
pub(super) const PROACTIVE_RETRY_MIN_TOKEN_GROWTH: u64 = 4_096;
pub(super) const PROACTIVE_RETRY_GROWTH_DIVISOR: u64 = 20;

pub(super) fn proactive_source_fingerprint(
    provider_opaque_context: Option<&crate::ProviderOpaqueContext>,
    messages: &[LlmMessage],
) -> [u8; 32] {
    let mut input = match provider_opaque_context {
        Some(crate::ProviderOpaqueContext::OpenResponsesCompact { output, .. }) => {
            output.iter().map(crate::CompactInputItem::from).collect()
        }
        None => Vec::new(),
    };
    input.extend(messages_to_compact_input(messages));
    let bytes = serde_json::to_vec(&input).unwrap_or_default();
    Sha256::digest(bytes).into()
}

#[derive(Debug)]
pub(super) struct AppliedNativeCompaction {
    pub(super) checkpoint_id: Option<String>,
    pub(super) output_items_after: usize,
    pub(super) tokens_before: Option<u64>,
    pub(super) tokens_after: Option<u64>,
    pub(super) bytes_before: Option<u64>,
    pub(super) bytes_after: Option<u64>,
    pub(super) duration_ms: u64,
    /// Provider-reported cost of the compaction call, when reported (EVE-895).
    pub(super) cost_usd: Option<f64>,
}

pub(super) fn materially_reduced(before: u64, after: u64) -> bool {
    const MIN_REDUCTION_UNITS: u64 = 32;
    let five_percent = before.div_ceil(20);
    let required_reduction = five_percent.max(MIN_REDUCTION_UNITS).min(before);
    after < before && before - after >= required_reduction
}

#[allow(clippy::too_many_arguments)]
pub(super) async fn try_apply_native_compaction(
    chat_driver: &dyn crate::ChatDriver,
    compaction_policy: &dyn crate::compaction_policy::CompactionPolicy,
    checkpoint_store: Option<&Arc<dyn crate::CompactionCheckpointStore>>,
    session_id: SessionId,
    source_sequence: Option<i64>,
    provider_type: &str,
    model: &str,
    system_prompt: Option<&str>,
    stateful_response_continuation: bool,
    llm_messages: &mut Vec<LlmMessage>,
    llm_config: &mut crate::driver_registry::LlmCallConfig,
) -> Result<Option<AppliedNativeCompaction>> {
    if !chat_driver.supports_compact() {
        return Ok(None);
    }

    let started = Instant::now();
    let has_system_prompt = system_prompt.is_some();
    let messages_to_compact = if has_system_prompt {
        &llm_messages[1..]
    } else {
        &llm_messages[..]
    };
    let (mut standalone_input, has_prior_opaque_context) =
        match llm_config.provider_opaque_context.as_ref() {
            Some(crate::ProviderOpaqueContext::OpenResponsesCompact { output, .. }) => (
                output.iter().map(crate::CompactInputItem::from).collect(),
                true,
            ),
            None => (Vec::new(), false),
        };
    if llm_config.reasoning_state.is_some()
        && let Some(crate::ProviderOpaqueContext::OpenResponsesCompact {
            reasoning_state, ..
        }) = &llm_config.provider_opaque_context
        && let Some(effort) = reasoning_state.as_ref().and_then(|state| state.effective)
    {
        standalone_input.push(crate::CompactInputItem::ConfigurationUpdate {
            reasoning: everruns_provider::compact::ConfigurationReasoning { effort },
        });
    }
    let prefix_len = standalone_input.len();
    standalone_input.extend(messages_to_compact_input(messages_to_compact));
    if let Some(effort) = llm_config
        .reasoning_state
        .as_ref()
        .and_then(|state| state.pending)
    {
        // The pending update belongs before fresh input, just as in normal
        // generation. Historical updates already travel with their messages.
        let boundary = prefix_len
            + standalone_input[prefix_len..]
                .iter()
                .rposition(crate::CompactInputItem::is_assistant_item)
                .map_or(0, |index| index + 1);
        standalone_input.insert(
            boundary,
            crate::CompactInputItem::ConfigurationUpdate {
                reasoning: everruns_provider::compact::ConfigurationReasoning { effort },
            },
        );
    }
    // Coalesce only adjacent updates (possible at an empty checkpoint suffix).
    // Never move updates across a semantic item.
    let mut input: Vec<crate::CompactInputItem> = Vec::new();
    for item in standalone_input {
        if matches!(item, crate::CompactInputItem::ConfigurationUpdate { .. })
            && matches!(
                input.last(),
                Some(crate::CompactInputItem::ConfigurationUpdate { .. })
            )
        {
            input.pop();
        }
        input.push(item);
    }
    let standalone_input = input;
    let local_tokens_before = (!stateful_response_continuation && !has_prior_opaque_context)
        .then(|| compaction_policy.estimate_total_tokens(messages_to_compact) as u64);
    let bytes_before = (!stateful_response_continuation
        || has_prior_opaque_context
        || llm_config.reasoning_state.is_some())
    .then(|| {
        serde_json::to_vec(&standalone_input)
            .ok()
            .map(|value| value.len() as u64)
    })
    .flatten();
    // Reconstruct standalone input even for a stateful continuation. Compacting
    // only the previous response handle would omit the fresh request delta, then
    // clearing that handle for the retry would make the omission permanent.
    let (input, compact_previous_response_id) = (standalone_input, None);

    let compact_response = match chat_driver
        .compact(
            &crate::ProviderEndpoint::default(),
            CompactRequest {
                reasoning_state: llm_config.reasoning_state.clone(),
                model: model.to_string(),
                input,
                previous_response_id: compact_previous_response_id,
                instructions: system_prompt.map(str::to_string),
            },
        )
        .await
    {
        Ok(Some(response)) => response,
        Ok(None) => return Ok(None),
        Err(error) => {
            tracing::warn!(
                session_id = %session_id,
                error = %error,
                "ReasonAtom: native compaction failed"
            );
            return Ok(None);
        }
    };

    let tokens_before = compact_response
        .usage
        .as_ref()
        .and_then(|usage| usage.input_tokens)
        .map(u64::from)
        .or(local_tokens_before);
    let tokens_after = compact_response
        .usage
        .as_ref()
        .and_then(|usage| usage.output_tokens)
        .map(u64::from);
    let cost_usd = compact_response.usage.as_ref().and_then(|usage| usage.cost);
    let bytes_after = serde_json::to_vec(&compact_response.output)
        .ok()
        .map(|value| value.len() as u64);
    let effective = match (tokens_before, tokens_after) {
        (Some(before), Some(after)) => materially_reduced(before, after),
        _ => match (bytes_before, bytes_after) {
            (Some(before), Some(after)) => materially_reduced(before, after),
            _ => false,
        },
    };
    if !effective {
        tracing::info!(
            session_id = %session_id,
            ?tokens_before,
            ?tokens_after,
            ?bytes_before,
            ?bytes_after,
            "ReasonAtom: native compaction produced no material reduction"
        );
        return Ok(None);
    }

    let output_items_after = compact_response.output.len();
    let opaque_context = crate::driver_registry::ProviderOpaqueContext::OpenResponsesCompact {
        output: compact_response.output,
        reasoning_state: llm_config.reasoning_state.clone(),
    };
    let checkpoint_id =
        if let (Some(store), Some(source_sequence)) = (checkpoint_store, source_sequence) {
            let id = Uuid::now_v7();
            let installed = store
                .install(crate::CompactionCheckpoint {
                    id,
                    session_id,
                    source_sequence,
                    provider_type: provider_type.to_string(),
                    model: model.to_string(),
                    format_version: crate::COMPACTION_CHECKPOINT_FORMAT_VERSION,
                    payload: crate::CompactionCheckpointPayload::ProviderOpaque {
                        context: opaque_context.clone(),
                    },
                })
                .await?;
            if !installed {
                return Err(AgentLoopError::store(
                    "a newer compaction checkpoint was installed concurrently",
                ));
            }
            Some(id.to_string())
        } else {
            None
        };

    // Apply only after the durable install succeeds, so checkpoint failures do
    // not partially mutate the retry request.
    llm_config.previous_response_id = None;
    llm_config.provider_opaque_context = Some(opaque_context);
    llm_messages.retain(|message| message.role == LlmMessageRole::System);
    if let Some(state) = llm_config.reasoning_state.as_mut() {
        // Explicit compaction resets configuration updates. Reassert the
        // effective effort even when it equals the original baseline.
        state.pending = state.effective;
    }

    Ok(Some(AppliedNativeCompaction {
        checkpoint_id,
        output_items_after,
        tokens_before,
        tokens_after,
        bytes_before,
        bytes_after,
        duration_ms: started.elapsed().as_millis() as u64,
        cost_usd,
    }))
}

pub(super) struct ProactiveCompactionContext<'a> {
    pub(super) chat_driver: &'a dyn crate::ChatDriver,
    pub(super) policy: &'a dyn crate::compaction_policy::CompactionPolicy,
    pub(super) checkpoint_store: Option<&'a Arc<dyn crate::CompactionCheckpointStore>>,
    pub(super) event_emitter: &'a dyn EventEmitter,
    pub(super) event_context: &'a EventContext,
    pub(super) session_id: SessionId,
    pub(super) message_source_sequence: Option<i64>,
    pub(super) provider_type: &'a str,
    pub(super) model: &'a str,
    pub(super) system_prompt: Option<&'a str>,
    pub(super) stateful_response_continuation: bool,
    pub(super) checkpoint_restored: bool,
    pub(super) checkpoint_suffix_message_count: usize,
    pub(super) raw_tool_result_bytes: usize,
    pub(super) prior_usage: Option<&'a TokenUsage>,
}

pub(super) async fn apply_proactive_compaction(
    context: ProactiveCompactionContext<'_>,
    messages: &mut Vec<LlmMessage>,
    config: &mut crate::driver_registry::LlmCallConfig,
) -> Result<Option<LlmCompactionInfo>> {
    use crate::compaction_policy::CompactionStrategy;

    let settings = context.policy.settings();
    let context_window = context
        .chat_driver
        .effective_context_window(context.model)
        .or_else(|| {
            crate::model_profiles::get_model_profile(
                &everruns_provider::DriverId::external(context.provider_type),
                context.model,
            )
            .and_then(|profile| profile.limits.map(|limits| limits.context as usize))
        })
        .unwrap_or(128_000);
    let checkpoint_rearmed = !context.checkpoint_restored
        || context.checkpoint_suffix_message_count >= CHECKPOINT_REARM_MIN_SUFFIX_MESSAGES;
    let native_strategy = matches!(
        settings.strategy,
        CompactionStrategy::Auto | CompactionStrategy::Native
    );
    let durable_source = context
        .checkpoint_store
        .zip(context.message_source_sequence);
    let estimated_tokens_before = context.policy.estimate_total_tokens(messages) as u64;
    let native_attempt_rearmed = if let Some((store, source_sequence)) = durable_source {
        match store
            .get_proactive_attempt(context.session_id, context.provider_type, context.model)
            .await
        {
            Ok(attempt) => attempt.is_none_or(|attempt| {
                if source_sequence < attempt.source_sequence
                    || messages.len() < attempt.input_message_count
                {
                    return true;
                }
                let same_source_lineage = proactive_source_fingerprint(
                    config.provider_opaque_context.as_ref(),
                    &messages[..attempt.input_message_count],
                ) == attempt.source_fingerprint;
                if !same_source_lineage {
                    return true;
                }
                if source_sequence == attempt.source_sequence {
                    return false;
                }
                let required_growth = PROACTIVE_RETRY_MIN_TOKEN_GROWTH
                    .max(attempt.estimated_input_tokens / PROACTIVE_RETRY_GROWTH_DIVISOR);
                estimated_tokens_before.saturating_sub(attempt.estimated_input_tokens)
                    >= required_growth
            }),
            Err(error) => {
                tracing::warn!(
                    session_id = %context.session_id,
                    error = %error,
                    "ReasonAtom: proactive compaction attempt watermark lookup failed"
                );
                true
            }
        }
    } else {
        true
    };
    let window_pressure = context
        .policy
        .should_compact_proactively(messages, context_window);
    let cost_pressure = context.policy.should_compact_for_cost(
        estimated_tokens_before as usize,
        context.raw_tool_result_bytes,
        context.prior_usage,
    );
    // Astra explicit compaction reconstructs the complete durable input even
    // when generation uses a response handle; it must still compact proactively.
    let local_pressure = (!context.stateful_response_continuation
        || config.reasoning_state.is_some())
        && checkpoint_rearmed
        && (window_pressure || cost_pressure);
    let should_attempt = native_strategy
        && context.chat_driver.supports_compact()
        && durable_source.is_some()
        && native_attempt_rearmed
        && local_pressure;

    let applied = if let (true, Some((store, source_sequence))) = (should_attempt, durable_source) {
        let messages_before = messages.len();
        let input_message_count = messages.len();
        let source_fingerprint =
            proactive_source_fingerprint(config.provider_opaque_context.as_ref(), messages);
        if let Err(error) = store
            .record_proactive_attempt(
                context.session_id,
                context.provider_type,
                context.model,
                crate::ProactiveCompactionAttempt {
                    source_sequence,
                    estimated_input_tokens: estimated_tokens_before,
                    input_message_count,
                    source_fingerprint,
                },
            )
            .await
        {
            tracing::warn!(
                session_id = %context.session_id,
                error = %error,
                "ReasonAtom: proactive compaction attempt watermark write failed"
            );
        }
        let _ = context
            .event_emitter
            .emit(EventRequest::new(
                context.session_id,
                context.event_context.clone(),
                ContextCompactingData {
                    reason: CompactionReason::ProactiveBudget,
                    strategy: settings.strategy.to_string(),
                    messages_before,
                    tokens_before: Some(estimated_tokens_before),
                    bytes_before: None,
                },
            ))
            .await;

        let applied = try_apply_native_compaction(
            context.chat_driver,
            context.policy,
            context.checkpoint_store,
            context.session_id,
            context.message_source_sequence,
            context.provider_type,
            context.model,
            context.system_prompt,
            false,
            messages,
            config,
        )
        .await?;

        if let Some(applied) = applied.as_ref() {
            let steps = vec![CompactionStepData {
                strategy: "native".to_string(),
                messages_after: applied.output_items_after,
                duration_ms: applied.duration_ms,
            }];
            let _ = context
                .event_emitter
                .emit(EventRequest::new(
                    context.session_id,
                    context.event_context.clone(),
                    ContextCompactedData {
                        checkpoint_id: applied.checkpoint_id.clone(),
                        strategy_used: "native".to_string(),
                        messages_before,
                        messages_after: applied.output_items_after,
                        tokens_before: applied.tokens_before,
                        tokens_after: applied.tokens_after,
                        bytes_before: applied.bytes_before,
                        bytes_after: applied.bytes_after,
                        duration_ms: applied.duration_ms,
                        steps,
                    },
                ))
                .await;
        }
        applied
    } else {
        None
    };

    if local_pressure && applied.is_none() {
        if matches!(
            settings.strategy,
            CompactionStrategy::Auto | CompactionStrategy::ObservationMasking
        ) {
            let conversation = if context.system_prompt.is_some() {
                &messages[1..]
            } else {
                &messages[..]
            };
            let masked = context.policy.apply_observation_masking(conversation);
            if masked.masked_count > 0 {
                let mut model_view = Vec::new();
                if context.system_prompt.is_some() {
                    model_view.push(messages[0].clone());
                }
                model_view.extend(masked.messages);
                *messages = model_view;
            }
        }
        let budget_tokens = (context_window as f32 * settings.budget_percent) as usize;
        if context.policy.estimate_total_tokens(messages) > budget_tokens {
            *messages = context.policy.aggressive_trim(
                messages,
                budget_tokens,
                context.system_prompt.is_some(),
            );
        }
    }

    Ok(applied.map(|applied| {
        LlmCompactionInfo::new(
            applied
                .tokens_before
                .and_then(|value| u32::try_from(value).ok()),
            applied
                .tokens_after
                .and_then(|value| u32::try_from(value).ok()),
            Some(applied.duration_ms),
            applied.cost_usd,
        )
    }))
}

pub(super) struct ReactiveCompactionContext<'a> {
    pub(super) chat_driver: &'a dyn crate::ChatDriver,
    pub(super) policy: &'a dyn crate::compaction_policy::CompactionPolicy,
    pub(super) checkpoint_store: Option<&'a Arc<dyn crate::CompactionCheckpointStore>>,
    pub(super) event_emitter: &'a dyn EventEmitter,
    pub(super) event_context: &'a EventContext,
    pub(super) session_id: SessionId,
    pub(super) message_source_sequence: Option<i64>,
    pub(super) provider_type: &'a str,
    pub(super) model: &'a str,
    pub(super) summarization_model_fallback: &'a str,
    pub(super) system_prompt: Option<&'a str>,
    pub(super) stateful_response_continuation: bool,
}

pub(super) struct ReactiveCompactionResult {
    pub(super) generation_info: Option<LlmCompactionInfo>,
}

/// Apply the request-too-large recovery cascade as one state transition.
///
/// `Ok(None)` means the cascade could not materially reduce the request and
/// the original provider error must remain authoritative.
pub(super) async fn apply_reactive_compaction(
    context: ReactiveCompactionContext<'_>,
    messages: &mut Vec<LlmMessage>,
    config: &mut crate::driver_registry::LlmCallConfig,
) -> Result<Option<ReactiveCompactionResult>> {
    use crate::compaction_policy::CompactionStrategy;

    let settings = context.policy.settings();
    let messages_before = messages.len();
    tracing::info!(
        session_id = %context.session_id,
        strategy = %settings.strategy,
        messages = messages_before,
        "ReasonAtom: context too large, attempting compaction"
    );

    let tokens_before = Some(context.policy.estimate_total_tokens(messages) as u64);
    let _ = context
        .event_emitter
        .emit(EventRequest::new(
            context.session_id,
            context.event_context.clone(),
            ContextCompactingData {
                reason: CompactionReason::RequestTooLarge,
                strategy: settings.strategy.to_string(),
                messages_before,
                tokens_before,
                bytes_before: None,
            },
        ))
        .await;

    let cascade_start = Instant::now();
    let mut steps = Vec::new();
    let mut strategies_used = Vec::new();
    let mut checkpoint_id = None;
    let mut generation_info = None;
    let mut measured_tokens_before = tokens_before;
    let mut tokens_after = None;
    let mut bytes_before = None;
    let mut bytes_after = None;
    let has_system_prompt = context.system_prompt.is_some();

    let run_masking = matches!(
        settings.strategy,
        CompactionStrategy::Auto | CompactionStrategy::ObservationMasking
    );
    let run_native = matches!(
        settings.strategy,
        CompactionStrategy::Auto | CompactionStrategy::Native
    ) && context.chat_driver.supports_compact();
    let run_summarization = matches!(
        settings.strategy,
        CompactionStrategy::Auto | CompactionStrategy::Summarization
    );

    if run_masking {
        let step_start = Instant::now();
        let conversation = if has_system_prompt {
            &messages[1..]
        } else {
            &messages[..]
        };
        let masked = context.policy.apply_observation_masking(conversation);
        if masked.masked_count > 0 {
            let mut model_view = Vec::new();
            if has_system_prompt {
                model_view.push(messages[0].clone());
            }
            model_view.extend(masked.messages);
            *messages = model_view;

            let duration_ms = step_start.elapsed().as_millis() as u64;
            strategies_used.push("observation_masking".to_string());
            steps.push(CompactionStepData {
                strategy: "observation_masking".to_string(),
                messages_after: messages.len(),
                duration_ms,
            });
            tracing::info!(
                session_id = %context.session_id,
                masked_count = masked.masked_count,
                duration_ms,
                "ReasonAtom: observation masking applied"
            );
        }
    }

    if run_native
        && let Some(applied) = try_apply_native_compaction(
            context.chat_driver,
            context.policy,
            context.checkpoint_store,
            context.session_id,
            context.message_source_sequence,
            context.provider_type,
            context.model,
            context.system_prompt,
            context.stateful_response_continuation,
            messages,
            config,
        )
        .await?
    {
        generation_info = Some(LlmCompactionInfo::new(
            applied
                .tokens_before
                .and_then(|value| u32::try_from(value).ok()),
            applied
                .tokens_after
                .and_then(|value| u32::try_from(value).ok()),
            Some(applied.duration_ms),
            applied.cost_usd,
        ));
        checkpoint_id = applied.checkpoint_id;
        measured_tokens_before = applied.tokens_before;
        tokens_after = applied.tokens_after;
        bytes_before = applied.bytes_before;
        bytes_after = applied.bytes_after;
        strategies_used.push("native".to_string());
        steps.push(CompactionStepData {
            strategy: "native".to_string(),
            messages_after: applied.output_items_after,
            duration_ms: applied.duration_ms,
        });
    }

    if config.reasoning_state.is_some()
        && !strategies_used.iter().any(|strategy| strategy == "native")
    {
        // Summary/trim would lose native configuration and opaque context. An
        // unsuccessful explicit compaction must leave the canonical history
        // intact and surface the context error, not silently discard it.
        return Ok(None);
    }
    if run_summarization && !strategies_used.iter().any(|strategy| strategy == "native") {
        let step_start = Instant::now();
        let conversation = if has_system_prompt {
            &messages[1..]
        } else {
            &messages[..]
        };
        let keep_recent = 10.min(conversation.len());
        let to_summarize = &conversation[..conversation.len() - keep_recent];
        let recent = &conversation[conversation.len() - keep_recent..];

        if !to_summarize.is_empty() {
            let summary_messages = vec![
                LlmMessage {
                    role: LlmMessageRole::System,
                    content: LlmMessageContent::Text(context.policy.summarization_prompt()),
                    tool_calls: None,
                    tool_call_id: None,
                    phase: None,
                    reasoning: Vec::new(),
                    configuration_update: None,
                },
                LlmMessage {
                    role: LlmMessageRole::User,
                    content: LlmMessageContent::Text(
                        context
                            .policy
                            .format_messages_for_summarization(to_summarize),
                    ),
                    tool_calls: None,
                    tool_call_id: None,
                    phase: None,
                    reasoning: Vec::new(),
                    configuration_update: None,
                },
            ];
            let summary_config = crate::driver_registry::LlmCallConfig {
                speed: None,
                verbosity: None,
                model: settings
                    .summarization_model
                    .clone()
                    .unwrap_or_else(|| context.summarization_model_fallback.to_string()),
                temperature: Some(0.0),
                max_tokens: Some(2000),
                tools: vec![],
                reasoning_effort: None,
                metadata: HashMap::new(),
                previous_response_id: None,
                provider_opaque_context: None,
                tool_search: None,
                prompt_cache: None,
                openrouter_routing: None,
                parallel_tool_calls: None,
                volatile_suffix_len: 0,
                extra_headers: Vec::new(),
                cache_diagnostics: None,
                reasoning_state: None,
            };

            match context
                .chat_driver
                .chat_completion(
                    &crate::ProviderEndpoint::default(),
                    summary_messages,
                    &summary_config,
                )
                .await
            {
                Ok(response) => {
                    let system_message = has_system_prompt.then(|| messages[0].clone());
                    *messages = context.policy.compose_summary_with_recent(
                        system_message,
                        &response.text,
                        recent,
                    );
                    let duration_ms = step_start.elapsed().as_millis() as u64;
                    strategies_used.push("summarization".to_string());
                    steps.push(CompactionStepData {
                        strategy: "summarization".to_string(),
                        messages_after: messages.len(),
                        duration_ms,
                    });
                    tracing::info!(
                        session_id = %context.session_id,
                        duration_ms,
                        messages_after = messages.len(),
                        "ReasonAtom: summarization applied"
                    );
                }
                Err(error) => tracing::warn!(
                    session_id = %context.session_id,
                    error = %error,
                    "ReasonAtom: summarization failed, continuing"
                ),
            }
        }
    }

    if strategies_used.is_empty() || messages.len() > messages_before / 2 {
        let step_start = Instant::now();
        let target = context.policy.estimate_total_tokens(messages) / 2;
        let trimmed = context
            .policy
            .aggressive_trim(messages, target, has_system_prompt);
        if trimmed.len() < messages.len() {
            *messages = trimmed;
            let duration_ms = step_start.elapsed().as_millis() as u64;
            strategies_used.push("aggressive_trim".to_string());
            steps.push(CompactionStepData {
                strategy: "aggressive_trim".to_string(),
                messages_after: messages.len(),
                duration_ms,
            });
            tracing::info!(
                session_id = %context.session_id,
                messages_after = messages.len(),
                "ReasonAtom: aggressive trim applied (last resort)"
            );
        }
    }

    let duration_ms = cascade_start.elapsed().as_millis() as u64;
    let messages_after = messages.len();
    if tokens_after.is_none() {
        tokens_after = Some(context.policy.estimate_total_tokens(messages) as u64);
    }
    let effective = match (measured_tokens_before, tokens_after) {
        (Some(before), Some(after)) => materially_reduced(before, after),
        _ => match (bytes_before, bytes_after) {
            (Some(before), Some(after)) => materially_reduced(before, after),
            _ => false,
        },
    };
    if !effective {
        tracing::warn!(
            session_id = %context.session_id,
            ?measured_tokens_before,
            ?tokens_after,
            "ReasonAtom: compaction cascade made no material reduction"
        );
        return Ok(None);
    }

    let strategy_used = strategies_used.join("+");
    if strategies_used
        .iter()
        .any(|strategy| strategy != "observation_masking")
    {
        let _ = context
            .event_emitter
            .emit(EventRequest::new(
                context.session_id,
                context.event_context.clone(),
                ContextCompactedData {
                    checkpoint_id,
                    strategy_used: strategy_used.clone(),
                    messages_before,
                    messages_after,
                    tokens_before: measured_tokens_before,
                    tokens_after,
                    bytes_before,
                    bytes_after,
                    duration_ms,
                    steps,
                },
            ))
            .await;
    }

    tracing::info!(
        session_id = %context.session_id,
        strategy = %strategy_used,
        messages_before,
        messages_after,
        duration_ms,
        "ReasonAtom: compaction cascade completed, retrying LLM call"
    );
    Ok(Some(ReactiveCompactionResult { generation_info }))
}