dirge-agent 0.7.3

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
//! Multi-tier auto-compaction decision engine — and the **canonical reference
//! for the whole context-budget ladder** (dirge-w5iy). The budget policy is
//! split across two cohesive modules by concern, and this is the one place
//! that documents the complete picture:
//!
//!   - **decision** (when/whether/how-hard to fold) lives here.
//!   - **mechanism** (the token estimator, per-result caps, the summarizer,
//!     and the snip override) lives in [`crate::agent::compression`].
//!
//! Faithful port of `DeepSeek-Reasonix/src/context-manager.ts` (345 lines).
//!
//! # The budget ladder
//!
//! Every threshold is a **fraction of the model's context window** (`ctx_max`),
//! compared against the current token count. In ascending order of pressure:
//!
//! | Fraction | Tier | Owner | Action |
//! |----------|------|-------|--------|
//! | 0.60 | Aggressive per-result cap | compression: [`AGGRESSIVE_CAP_THRESHOLD`] | tighten each tool-result cap (3000→1000 tok) to head off overflow *before* a fold is needed |
//! | 0.75 | Post-response fold | [`HISTORY_FOLD_THRESHOLD`] | fold older history into a summary, keep a 20% tail. Also gates the summarizer LLM call ([`should_compress`]) |
//! | 0.78 | Aggressive fold | [`HISTORY_FOLD_AGGRESSIVE_THRESHOLD`] | the normal fold didn't buy enough headroom → halve the tail budget (10%) |
//! | 0.80 | Exit-with-summary | [`FORCE_SUMMARY_THRESHOLD`] | defense in depth: force a final summary and end the turn |
//! | 0.90 | Turn-start fold | [`TURN_START_FOLD_THRESHOLD`] | before the first API call — catches a terminal prior turn, session restore, or a huge user paste |
//!
//! Plus a guard (not a pressure tier): the **min-savings check** (0.30,
//! `HISTORY_FOLD_MIN_SAVINGS_FRACTION`) skips a fold whose head wouldn't
//! shrink the log by at least that fraction.
//!
//! # One estimator, two measurement points
//!
//! There is a **single** token estimator —
//! [`compression::estimate_messages_tokens`] (`chars / CHARS_PER_TOKEN`). What
//! differs is *when* the count is taken, not *how*:
//!
//!   - **pre-send** (turn-start fold, the per-result cap tier): the local
//!     estimate, since the API hasn't been called yet;
//!   - **post-response** ([`decide_after_usage`]): the API's exact
//!     `prompt_tokens` from the usage response.
//!
//! These two numbers can legitimately disagree (the estimate is approximate);
//! that's inherent to measuring before vs. after the call, not a duplicated
//! estimator.
//!
//! # The snip override
//!
//! A pre-send "snip" ([`compression::cap_oversized_tool_results`]) can free
//! enough tokens that a *normal* post-response fold is unnecessary; the
//! suppression lives in `run.rs` via [`compression::snip_bought_enough`]
//! (a snip freeing ≥10% of the window skips a normal fold; aggressive /
//! force-summary folds always proceed). It is intentionally *not* baked into
//! [`decide_after_usage`] so the decision stays a pure function of the token
//! ratio; run.rs composes the two.
//!
//! # Tail protection: two strategies
//!
//! Recent messages are protected by **message count**
//! ([`compression::PROTECT_TAIL_DEFAULT`]) at the pruning layer, while the
//! fold tiers above express the tail as a **token fraction** of the window
//! (20% / 10%). They are not equivalent (5 messages may be 100 or 50 000
//! tokens); run.rs picks the `protect_tail` count per fold kind.
//!
//! [`AGGRESSIVE_CAP_THRESHOLD`]: crate::agent::compression::AGGRESSIVE_CAP_THRESHOLD
//! [`should_compress`]: crate::agent::compression::should_compress
//! [`compression::estimate_messages_tokens`]: crate::agent::compression::estimate_messages_tokens
//! [`compression::cap_oversized_tool_results`]: crate::agent::compression::cap_oversized_tool_results
//! [`compression::snip_bought_enough`]: crate::agent::compression::snip_bought_enough
//! [`compression::PROTECT_TAIL_DEFAULT`]: crate::agent::compression::PROTECT_TAIL_DEFAULT

use serde::Serialize;

// ================================================================
// Threshold constants — port of context-manager.ts:27-43
// ================================================================

/// Auto-fold when a turn's response shows promptTokens above
/// this fraction of ctxMax.
pub const HISTORY_FOLD_THRESHOLD: f64 = 0.75;

/// Tail budget after a normal fold, as a fraction of ctxMax.
pub const HISTORY_FOLD_TAIL_FRACTION: f64 = 0.2;

/// Above this fraction the normal fold's tail budget didn't
/// buy enough headroom — fold harder.
pub const HISTORY_FOLD_AGGRESSIVE_THRESHOLD: f64 = 0.78;

/// Tail budget after an aggressive fold — half the normal one,
/// sacrifices recent context for headroom.
pub const HISTORY_FOLD_AGGRESSIVE_TAIL_FRACTION: f64 = 0.1;

/// Skip the fold if the head wouldn't shrink the log by at
/// least this fraction.
#[cfg(test)]
pub const HISTORY_FOLD_MIN_SAVINGS_FRACTION: f64 = 0.3;

/// Above this fraction we exit the turn with a summary instead
/// of folding (defense in depth).
pub const FORCE_SUMMARY_THRESHOLD: f64 = 0.8;

/// Turn-start local estimate above this fraction triggers a
/// pre-iter fold. Covers cases the post-response fold can't
/// (terminal prior turn, fresh session restore, huge user
/// paste).
pub const TURN_START_FOLD_THRESHOLD: f64 = 0.9;

// ================================================================
// Data types — port of context-manager.ts:67-85
// ================================================================

/// What action the context manager recommends.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum PostUsageDecisionKind {
    /// Context is within healthy limits — carry on.
    None,
    /// Fold older messages into a summary; keep the tail.
    Fold,
    /// Exceeded even the exit-with-summary threshold — force
    /// a final summary before ending the turn.
    ExitWithSummary,
}

/// Decision after a turn's response.
#[derive(Debug, Clone, Copy)]
pub struct PostUsageDecision {
    pub kind: PostUsageDecisionKind,
    #[allow(dead_code)]
    pub prompt_tokens: u64,
    #[allow(dead_code)]
    pub ctx_max: u64,
    pub ratio: f64,
    /// Token budget for the recent tail when kind is Fold.
    /// Smaller in the aggressive band.
    pub tail_budget: Option<u64>,
    /// True when this fold is in the aggressive band (78%-80%).
    pub aggressive: bool,
}

/// Turn-start estimate result.
#[derive(Debug, Clone, Copy)]
pub struct TurnStartEstimate {
    pub estimate_tokens: u64,
    pub ctx_max: u64,
    pub ratio: f64,
}

// ================================================================
// Decision logic — port of context-manager.ts:134-177
// ================================================================

/// Decide what to do after a turn's response — fold, exit with
/// summary, or carry on. Port of `ContextManager.decideAfterUsage`
/// (context-manager.ts:134-165).
///
/// `prompt_tokens`: the prompt_tokens value from the API usage
///   response. If `None`, the decision is `None` (no usage data).
/// `ctx_max`: the model's context window size in tokens.
/// `already_folded_this_turn`: true if we already folded earlier
///   in this turn (prevents double-fold).
pub fn decide_after_usage(
    prompt_tokens: Option<u64>,
    ctx_max: u64,
    already_folded_this_turn: bool,
) -> PostUsageDecision {
    decide_after_usage_with_threshold(prompt_tokens, ctx_max, already_folded_this_turn, None)
}

/// MiMo-style incremental checkpoint cadence by context-window size
/// (port of opencode `defaultThresholdsFor`). These are NON-destructive
/// background checkpoint *writes* — they keep the full context and just
/// refresh the durable checkpoint often, so a later overflow/fold almost
/// always has a fresh checkpoint to recover from. They are independent of
/// the destructive fold thresholds above. `< 25K` windows disable the
/// subsystem (too little headroom to be worth it).
pub fn checkpoint_thresholds_for(ctx_max: u64) -> Vec<f64> {
    if ctx_max < 25_000 {
        return Vec::new();
    }
    if ctx_max <= 200_000 {
        return vec![0.2, 0.4, 0.6, 0.8];
    }
    if ctx_max <= 500_000 {
        return (1..=9).map(|i| i as f64 * 0.1).collect();
    }
    (1..=18).map(|i| i as f64 * 0.05).collect()
}

/// Tracks which incremental-checkpoint thresholds a run has crossed so each
/// fires its background writer exactly once. A destructive fold rebuilds
/// the context, so [`reset`](Self::reset) clears the crossed state and the
/// next growth re-checkpoints (mirrors opencode `resetThresholds`).
#[derive(Debug, Clone)]
pub struct CheckpointSchedule {
    thresholds: Vec<f64>,
    crossed: Vec<bool>,
}

impl CheckpointSchedule {
    pub fn new(ctx_max: u64) -> Self {
        let thresholds = checkpoint_thresholds_for(ctx_max);
        let crossed = vec![false; thresholds.len()];
        Self {
            thresholds,
            crossed,
        }
    }

    /// Whether the subsystem is active for this window size.
    pub fn is_enabled(&self) -> bool {
        !self.thresholds.is_empty()
    }

    /// Record the current usage `ratio` (prompt_tokens / ctx_max). Returns
    /// `true` when it has just crossed one or more not-yet-crossed
    /// thresholds — the caller fires a single background checkpoint for
    /// the crossing (multiple thresholds crossed at once → one write).
    pub fn note_usage(&mut self, ratio: f64) -> bool {
        let mut newly = false;
        for (i, &t) in self.thresholds.iter().enumerate() {
            if !self.crossed[i] && ratio >= t {
                self.crossed[i] = true;
                newly = true;
            }
        }
        newly
    }

    /// Clear crossed state after a destructive fold rebuilt the context.
    pub fn reset(&mut self) {
        for c in &mut self.crossed {
            *c = false;
        }
    }
}

/// Process-wide early-fold threshold, installed once at startup from
/// `Config::compaction_fold_threshold` via [`init_fold_threshold`]. Lets a
/// user opt into MiMo-style earlier checkpointing without threading the
/// value through the whole loop config — same OnceLock-set-once convention
/// as `timeout::Timeouts::init`. Unset → the [`HISTORY_FOLD_THRESHOLD`]
/// default.
static FOLD_THRESHOLD_OVERRIDE: std::sync::OnceLock<Option<f64>> = std::sync::OnceLock::new();

/// Install the configured early-fold threshold process-wide. Idempotent —
/// the first call wins; later calls are ignored. Called once at startup
/// after config load. `None` (or never calling this) keeps the default.
pub fn init_fold_threshold(override_fraction: Option<f64>) {
    let _ = FOLD_THRESHOLD_OVERRIDE.set(override_fraction);
}

/// Default working-context budget in tokens. Effective context is a
/// fraction of a model's advertised window — quality degrades well before
/// the limit, with the "smart zone" running out around 100k regardless of
/// the advertised size (RULER / Chroma context-rot research; see
/// garrit.xyz/posts/2026-05-06-dont-trust-large-context-windows). So dirge
/// caps the budget it actually works within, rather than trusting a large
/// window, and folds/forms memory to stay inside it.
pub const DEFAULT_CONTEXT_TARGET: u64 = 100_000;

/// Floor for a configured target — below this the agent can't get useful
/// work done between folds.
const MIN_CONTEXT_TARGET: u64 = 16_000;

/// Process-wide working-context budget, installed once at startup from
/// `Config::context_target`. The compaction decision treats the effective
/// window as `min(model_window, this)` so a 200k/1M model still folds
/// within the budget instead of drifting into the degradation zone.
static CONTEXT_TARGET: std::sync::OnceLock<u64> = std::sync::OnceLock::new();

/// Resolve a configured target to the value to install: `None` →
/// [`DEFAULT_CONTEXT_TARGET`]; a set value is floored at
/// [`MIN_CONTEXT_TARGET`]. Pure, so the floor/default logic is testable
/// without touching the process global.
pub fn resolve_context_target(configured: Option<u64>) -> u64 {
    configured
        .map(|t| t.max(MIN_CONTEXT_TARGET))
        .unwrap_or(DEFAULT_CONTEXT_TARGET)
}

/// Install the working-context budget process-wide. Idempotent (first call
/// wins).
pub fn init_context_target(target: Option<u64>) {
    let _ = CONTEXT_TARGET.set(resolve_context_target(target));
}

/// The configured working-context budget (tokens). Default
/// [`DEFAULT_CONTEXT_TARGET`].
pub fn context_target() -> u64 {
    *CONTEXT_TARGET.get().unwrap_or(&DEFAULT_CONTEXT_TARGET)
}

/// The effective context window for all compaction math: the smaller of
/// the model's advertised window and the configured budget. Capping here
/// means every existing tier (fold / aggressive / force / turn-start /
/// incremental checkpoint) operates within the budget, so the live context
/// stays in the model's smart zone no matter how large the window claims
/// to be.
pub fn effective_ctx_max(model_window: u64) -> u64 {
    model_window.min(context_target())
}

/// Process-wide explicit context-window override from
/// `Config::context_window`, installed at startup. When set it replaces the
/// built-in model-table lookup for the loop's window (before the
/// [`context_target`] cap), so a user can correct it or supply one for a
/// model the table doesn't know. Previously the loop read the table
/// directly and silently ignored this config; mirroring the other budget
/// knobs as a process global lets the loop honor it without threading the
/// full `Config` through. `None` → use the model table.
static CONTEXT_WINDOW_OVERRIDE: std::sync::OnceLock<Option<u64>> = std::sync::OnceLock::new();

/// Install the explicit context-window override. Idempotent.
pub fn init_context_window_override(window: Option<u64>) {
    let _ = CONTEXT_WINDOW_OVERRIDE.set(window);
}

/// The configured context-window override, if any.
pub fn context_window_override() -> Option<u64> {
    CONTEXT_WINDOW_OVERRIDE.get().copied().flatten()
}

/// Process-wide toggle for the incremental background checkpoint. Default
/// ON (mirrors MiMo) — installed once at startup from
/// `Config::incremental_checkpoint`. Only an explicit `Some(false)`
/// disables it.
static INCREMENTAL_CHECKPOINT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();

/// Install the incremental-checkpoint toggle. `None` keeps the default-on
/// behavior; `Some(false)` turns it off process-wide.
pub fn init_incremental_checkpoint(enabled: Option<bool>) {
    let _ = INCREMENTAL_CHECKPOINT.set(enabled.unwrap_or(true));
}

/// Whether the incremental background checkpoint is active. Default true.
pub fn incremental_checkpoint_enabled() -> bool {
    *INCREMENTAL_CHECKPOINT.get().unwrap_or(&true)
}

/// Round 2 (memory-awareness feedback): set when background learning
/// (post-session consolidation) has written new memories. The system-prompt
/// memory block is baked at agent-build time, so mid-session writes wouldn't
/// otherwise reach the running agent until restart. The loop consumes this
/// at the next turn boundary and re-injects the refreshed memory block.
static MEMORIES_DIRTY: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Mark consolidated memories as changed; the running loop re-injects the
/// refreshed block at its next turn boundary. Called by the post-session
/// orchestrator after its learning passes complete.
pub fn mark_memories_dirty() {
    MEMORIES_DIRTY.store(true, std::sync::atomic::Ordering::Release);
}

/// Consume the memory-dirty flag — returns `true` exactly once per `mark`,
/// resetting it. Cheap to poll every turn (a single atomic swap).
pub fn take_memories_dirty() -> bool {
    MEMORIES_DIRTY.swap(false, std::sync::atomic::Ordering::AcqRel)
}

/// Clamp a configured early-fold threshold into a safe range. An override
/// may only make the NORMAL fold fire *earlier* — never later than the
/// default and never below a floor that would fold almost immediately —
/// so the aggressive/force bands above it keep their ordering. An explicit
/// `override_fraction` wins (used by callers and tests); otherwise the
/// startup-installed process global is consulted; out-of-range or absent
/// values fall back to [`HISTORY_FOLD_THRESHOLD`].
pub fn effective_fold_threshold(override_fraction: Option<f64>) -> f64 {
    let candidate = override_fraction.or_else(|| FOLD_THRESHOLD_OVERRIDE.get().copied().flatten());
    match candidate {
        Some(f) if f.is_finite() && (0.3..=HISTORY_FOLD_THRESHOLD).contains(&f) => f,
        _ => HISTORY_FOLD_THRESHOLD,
    }
}

/// As [`decide_after_usage`], but with a configurable early-fold threshold
/// (MiMo's "checkpoint/compress earlier" knob). A lower threshold folds —
/// and therefore writes the durable checkpoint — sooner and from more
/// coherent context, at the cost of more frequent folds. The aggressive
/// and force-summary bands are unchanged; the override is clamped by
/// [`effective_fold_threshold`] so it can only lower the normal band.
pub fn decide_after_usage_with_threshold(
    prompt_tokens: Option<u64>,
    ctx_max: u64,
    already_folded_this_turn: bool,
    fold_threshold_override: Option<f64>,
) -> PostUsageDecision {
    let Some(prompt_tokens) = prompt_tokens else {
        return PostUsageDecision {
            kind: PostUsageDecisionKind::None,
            prompt_tokens: 0,
            ctx_max,
            ratio: 0.0,
            tail_budget: None,
            aggressive: false,
        };
    };
    if ctx_max == 0 {
        return PostUsageDecision {
            kind: PostUsageDecisionKind::None,
            prompt_tokens,
            ctx_max,
            ratio: 0.0,
            tail_budget: None,
            aggressive: false,
        };
    }
    let ratio = prompt_tokens as f64 / ctx_max as f64;

    if ratio > FORCE_SUMMARY_THRESHOLD {
        return PostUsageDecision {
            kind: PostUsageDecisionKind::ExitWithSummary,
            prompt_tokens,
            ctx_max,
            ratio,
            tail_budget: None,
            aggressive: false,
        };
    }

    if already_folded_this_turn {
        return PostUsageDecision {
            kind: PostUsageDecisionKind::None,
            prompt_tokens,
            ctx_max,
            ratio,
            tail_budget: None,
            aggressive: false,
        };
    }

    if ratio > HISTORY_FOLD_AGGRESSIVE_THRESHOLD {
        return PostUsageDecision {
            kind: PostUsageDecisionKind::Fold,
            prompt_tokens,
            ctx_max,
            ratio,
            tail_budget: Some((ctx_max as f64 * HISTORY_FOLD_AGGRESSIVE_TAIL_FRACTION) as u64),
            aggressive: true,
        };
    }

    if ratio > effective_fold_threshold(fold_threshold_override) {
        return PostUsageDecision {
            kind: PostUsageDecisionKind::Fold,
            prompt_tokens,
            ctx_max,
            ratio,
            tail_budget: Some((ctx_max as f64 * HISTORY_FOLD_TAIL_FRACTION) as u64),
            aggressive: false,
        };
    }

    PostUsageDecision {
        kind: PostUsageDecisionKind::None,
        prompt_tokens,
        ctx_max,
        ratio,
        tail_budget: None,
        aggressive: false,
    }
}

/// Turn-start estimate vs ctxMax. Caller folds if the ratio
/// crosses TURN_START_FOLD_THRESHOLD. Port of
/// `ContextManager.estimateTurnStart`
/// (context-manager.ts:167-177).
///
/// `estimate_tokens`: a local estimate of total request tokens
///   (messages + tools + system prompt).
/// `ctx_max`: the model's context window size in tokens.
pub fn estimate_turn_start(estimate_tokens: u64, ctx_max: u64) -> TurnStartEstimate {
    let ratio = if ctx_max == 0 {
        f64::INFINITY
    } else {
        estimate_tokens as f64 / ctx_max as f64
    };
    TurnStartEstimate {
        estimate_tokens,
        ctx_max,
        ratio,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================================
    // working-context budget
    // ============================================================

    #[test]
    fn resolve_context_target_defaults_and_floors() {
        assert_eq!(resolve_context_target(None), DEFAULT_CONTEXT_TARGET);
        assert_eq!(resolve_context_target(Some(50_000)), 50_000);
        // A tiny configured target is floored so the agent can still work.
        assert_eq!(resolve_context_target(Some(1_000)), MIN_CONTEXT_TARGET);
        // A target above any model window is kept; min(window, target)
        // applies the real cap later.
        assert_eq!(resolve_context_target(Some(250_000)), 250_000);
    }

    #[test]
    fn capped_budget_folds_within_the_target() {
        // With a 100k working budget — what `effective_ctx_max` yields for a
        // big model — the normal fold fires at 0.75 * 100k = 75k, keeping
        // the live context in the smart zone no matter how large the
        // advertised window is.
        let budget = DEFAULT_CONTEXT_TARGET;
        assert_eq!(
            decide_after_usage(Some(74_000), budget, false).kind,
            PostUsageDecisionKind::None
        );
        assert_eq!(
            decide_after_usage(Some(76_000), budget, false).kind,
            PostUsageDecisionKind::Fold
        );
    }

    // ============================================================
    // incremental checkpoint schedule
    // ============================================================

    #[test]
    fn checkpoint_cadence_matches_mimo_by_window() {
        assert!(
            checkpoint_thresholds_for(8_000).is_empty(),
            "tiny window disabled"
        );
        assert_eq!(checkpoint_thresholds_for(128_000), vec![0.2, 0.4, 0.6, 0.8]);
        assert_eq!(checkpoint_thresholds_for(200_000), vec![0.2, 0.4, 0.6, 0.8]);
        assert_eq!(checkpoint_thresholds_for(400_000).len(), 9, "10% cadence");
        assert_eq!(checkpoint_thresholds_for(1_000_000).len(), 18, "5% cadence");
    }

    #[test]
    fn schedule_fires_each_threshold_once_until_reset() {
        let mut s = CheckpointSchedule::new(128_000); // [.2,.4,.6,.8]
        assert!(s.is_enabled());
        assert!(!s.note_usage(0.1), "below first threshold");
        assert!(s.note_usage(0.25), "crossed 20%");
        assert!(!s.note_usage(0.30), "no new threshold");
        assert!(s.note_usage(0.45), "crossed 40%");
        // Jumping past several at once is a single firing.
        assert!(s.note_usage(0.85), "crossed 60% and 80% together");
        assert!(!s.note_usage(0.9), "all crossed");
        // A destructive fold rebuilds context → re-arm.
        s.reset();
        assert!(s.note_usage(0.25), "fires again after reset");
    }

    #[test]
    fn disabled_schedule_never_fires() {
        let mut s = CheckpointSchedule::new(10_000);
        assert!(!s.is_enabled());
        assert!(!s.note_usage(0.99));
    }

    // ============================================================
    // early-fold threshold override
    // ============================================================

    /// The override is clamped: a value in `0.3..=0.75` is honored; out of
    /// range or absent falls back to the default. Uses explicit params so
    /// it doesn't depend on the process-global install.
    #[test]
    fn effective_fold_threshold_clamps_override() {
        assert_eq!(effective_fold_threshold(Some(0.5)), 0.5);
        assert_eq!(effective_fold_threshold(Some(0.3)), 0.3);
        assert_eq!(
            effective_fold_threshold(Some(0.9)),
            HISTORY_FOLD_THRESHOLD,
            "above the default is rejected (can't fold later)"
        );
        assert_eq!(
            effective_fold_threshold(Some(0.05)),
            HISTORY_FOLD_THRESHOLD,
            "below the floor is rejected"
        );
        assert_eq!(
            effective_fold_threshold(Some(f64::NAN)),
            HISTORY_FOLD_THRESHOLD
        );
        assert_eq!(effective_fold_threshold(None), HISTORY_FOLD_THRESHOLD);
    }

    /// A lower override folds sooner: a ratio that is healthy at the
    /// default (0.75) becomes a Fold under an earlier threshold, while the
    /// aggressive/force bands above it are unchanged.
    #[test]
    fn lower_override_folds_earlier() {
        // 60% of the window: no fold at the default threshold…
        let d = decide_after_usage_with_threshold(Some(76_800), 128_000, false, None);
        assert_eq!(d.kind, PostUsageDecisionKind::None);
        // …but folds with an early 0.5 threshold.
        let d = decide_after_usage_with_threshold(Some(76_800), 128_000, false, Some(0.5));
        assert_eq!(d.kind, PostUsageDecisionKind::Fold);
        assert!(!d.aggressive, "still the normal band, not aggressive");
        // The force-summary band is independent of the override.
        let d = decide_after_usage_with_threshold(Some(110_000), 128_000, false, Some(0.5));
        assert_eq!(d.kind, PostUsageDecisionKind::ExitWithSummary);
    }

    // ============================================================
    // decide_after_usage
    // ============================================================

    #[test]
    fn no_usage_data_returns_none() {
        let d = decide_after_usage(None, 128_000, false);
        assert_eq!(d.kind, PostUsageDecisionKind::None);
        assert_eq!(d.ratio, 0.0);
    }

    #[test]
    fn below_threshold_returns_none() {
        // 50K out of 128K = ~39% → below 75% threshold
        let d = decide_after_usage(Some(50_000), 128_000, false);
        assert_eq!(d.kind, PostUsageDecisionKind::None);
    }

    #[test]
    fn above_75pct_triggers_fold() {
        // 98K out of 128K = ~76.5% → above 75%, below 78%
        let d = decide_after_usage(Some(98_000), 128_000, false);
        assert_eq!(d.kind, PostUsageDecisionKind::Fold);
        assert!(!d.aggressive);
        // Tail budget: 20% of 128K = 25600
        assert_eq!(d.tail_budget, Some(25600));
    }

    #[test]
    fn above_78pct_triggers_aggressive_fold() {
        // 101K out of 128K = ~78.9% → above 78%
        let d = decide_after_usage(Some(101_000), 128_000, false);
        assert_eq!(d.kind, PostUsageDecisionKind::Fold);
        assert!(d.aggressive);
        // Aggressive tail budget: 10% of 128K = 12800
        assert_eq!(d.tail_budget, Some(12800));
    }

    #[test]
    fn above_80pct_triggers_exit_with_summary() {
        // 105K out of 128K = ~82% → above 80%
        let d = decide_after_usage(Some(105_000), 128_000, false);
        assert_eq!(d.kind, PostUsageDecisionKind::ExitWithSummary);
    }

    #[test]
    fn already_folded_prevents_double_fold() {
        // Even though ratio is above 75%, we don't fold again
        let d = decide_after_usage(Some(100_000), 128_000, true);
        assert_eq!(d.kind, PostUsageDecisionKind::None);
    }

    #[test]
    fn already_folded_does_not_prevent_exit_with_summary() {
        // Above 80% still triggers exit even if already folded
        let d = decide_after_usage(Some(105_000), 128_000, true);
        assert_eq!(d.kind, PostUsageDecisionKind::ExitWithSummary);
    }

    #[test]
    fn zero_ctx_max_handled_gracefully() {
        // ctx_max == 0 is degenerate (unknown model, config error).
        // Guard returns None rather than computing inf/NaN ratio.
        let d = decide_after_usage(Some(1000), 0, false);
        assert_eq!(d.kind, PostUsageDecisionKind::None);
    }

    // ============================================================
    // estimate_turn_start
    // ============================================================

    #[test]
    fn estimate_below_threshold() {
        let e = estimate_turn_start(50_000, 128_000);
        assert!(e.ratio < TURN_START_FOLD_THRESHOLD);
        assert_eq!(e.ctx_max, 128_000);
    }

    #[test]
    fn estimate_above_threshold() {
        let e = estimate_turn_start(120_000, 128_000);
        assert!(e.ratio > TURN_START_FOLD_THRESHOLD);
    }

    #[test]
    fn estimate_at_boundary() {
        let boundary = (128_000.0 * TURN_START_FOLD_THRESHOLD) as u64;
        let e = estimate_turn_start(boundary, 128_000);
        // At exactly the threshold — caller decides whether to fold
        assert!((e.ratio - TURN_START_FOLD_THRESHOLD).abs() < 0.001);
    }

    // ============================================================
    // Threshold constant sanity
    // ============================================================

    #[test]
    fn thresholds_are_strictly_ordered() {
        assert!(FORCE_SUMMARY_THRESHOLD > HISTORY_FOLD_AGGRESSIVE_THRESHOLD);
        assert!(HISTORY_FOLD_AGGRESSIVE_THRESHOLD > HISTORY_FOLD_THRESHOLD);
        assert!(HISTORY_FOLD_THRESHOLD > HISTORY_FOLD_MIN_SAVINGS_FRACTION);
    }

    #[test]
    fn aggressive_tail_is_smaller_than_normal_tail() {
        assert!(HISTORY_FOLD_AGGRESSIVE_TAIL_FRACTION < HISTORY_FOLD_TAIL_FRACTION);
    }
}