Skip to main content

deepstrike_core/context/
config.rs

1/// All compression and context management parameters expressed as fractions of
2/// `max_tokens`. This is the single control surface for the compression pipeline:
3/// changing `max_tokens` (e.g. switching model) rescales every derived limit
4/// automatically with no other configuration change required.
5///
6/// Invariant: snip < micro < collapse < auto < renewal (strictly increasing).
7#[derive(Debug, Clone)]
8pub struct ContextConfig {
9    // ── Pressure thresholds ─────────────────────────────────────────────────
10    pub snip_threshold: f64,
11    pub micro_threshold: f64,
12    pub collapse_threshold: f64,
13    pub auto_threshold: f64,
14    pub renewal_threshold: f64,
15
16    // ── Post-compression target ──────────────────────────────────────────────
17    /// Target rho after any compression pass. Must be < snip_threshold.
18    pub target_after_compress: f64,
19
20    // ── Per-compactor ratios ─────────────────────────────────────────────────
21    /// Fraction of max_tokens any single message may occupy after SnipCompact.
22    /// Messages smaller than this are never touched.
23    pub snip_per_msg_ratio: f64,
24
25    // ── Renewal ──────────────────────────────────────────────────────────────
26    /// Fraction of max_tokens worth of history tokens to carry across renewal.
27    /// Renewal stops carrying messages once this token budget is exhausted.
28    pub carryover_ratio: f64,
29
30    // ── Recovery / repair ────────────────────────────────────────────────────
31    /// Maximum fraction of max_tokens a recovery/replay payload may occupy.
32    pub recovery_content_ratio: f64,
33
34    /// Recent history messages always kept during render.
35    pub preserve_recent_msgs: usize,
36
37    /// Number of most-recent turns (user+assistant pairs) preserved by
38    /// CollapseCompactor and AutoCompactor. Each turn = 2 messages, so
39    /// the actual message count kept is `preserve_recent_turns * 2`.
40    /// Must be ≥ 1. Default: 2 (= 4 messages).
41    pub preserve_recent_turns: usize,
42
43    // ── Noise reduction ──────────────────────────────────────────────────────
44    /// Include the dashboard block in the rendered system context.
45    /// Defaults to false; enable only in explicit agent-os mode.
46    pub render_dashboard: bool,
47
48    /// Use verbose internal control notes (e.g. "[SYSTEM] Transaction rollback: …").
49    /// Defaults to false; uses concise natural-language notes instead.
50    pub verbose_control_notes: bool,
51
52    // ── Layer 3: Time-based decay ───────────────────────────────────────
53
54    /// Minutes of inactivity before triggering Micro-Compact (Layer 3).
55    /// Defaults to 60 minutes — assumes Prompt Cache has expired by then.
56    pub micro_compact_idle_minutes: u32,
57
58    /// Number of recent tool results to preserve during Micro-Compact.
59    pub preserved_tool_results: usize,
60
61    // ── Layer 5: Auto-Compact buffer ─────────────────────────────────────
62
63    /// Buffer size for Auto-Compact trigger (Layer 5).
64    /// Trigger threshold = max_tokens - autocompact_buffer.
65    /// Defaults to 13K tokens (p99.99 of summarizer output length + safety margin).
66    pub autocompact_buffer: u32,
67
68    // ── Layer 1: Large-result spool ──────────────────────────────────────
69
70    /// Byte size above which a single tool result is spooled (Layer 1): the kernel
71    /// keeps only a preview in context and emits `LargeResultSpooled` for the SDK to
72    /// persist the full content to disk. Default: 50 KiB. `0` disables spooling.
73    pub spool_threshold_bytes: u32,
74
75    /// Preview byte budget kept in context when a tool result is spooled. Default: 2 KiB.
76    pub spool_preview_bytes: u32,
77}
78
79fn default_micro_compact_idle_minutes() -> u32 {
80    60
81}
82
83fn default_preserved_tool_results() -> usize {
84    5
85}
86
87fn default_autocompact_buffer() -> u32 {
88    13_000
89}
90
91impl Default for ContextConfig {
92    fn default() -> Self {
93        Self {
94            snip_threshold: 0.70,
95            micro_threshold: 0.80,
96            collapse_threshold: 0.90,
97            auto_threshold: 0.95,
98            renewal_threshold: 0.98,
99            target_after_compress: 0.65,
100            snip_per_msg_ratio: 0.05,
101            carryover_ratio: 0.05,
102            recovery_content_ratio: 0.25,
103            preserve_recent_msgs: 4,
104            preserve_recent_turns: 2,
105            render_dashboard: false,
106            verbose_control_notes: false,
107            micro_compact_idle_minutes: 60,
108            preserved_tool_results: 5,
109            autocompact_buffer: 13_000,
110            spool_threshold_bytes: 50 * 1024,
111            spool_preview_bytes: 2 * 1024,
112        }
113    }
114}
115
116impl ContextConfig {
117    /// Token budget to target after a compression pass.
118    pub fn target_tokens(&self, max_tokens: u32) -> u32 {
119        (max_tokens as f64 * self.target_after_compress) as u32
120    }
121
122    /// Per-message token cap used by SnipCompact.
123    /// Floor of 50 ensures very small context windows still get useful output.
124    pub fn snip_per_msg_tokens(&self, max_tokens: u32) -> u32 {
125        ((max_tokens as f64 * self.snip_per_msg_ratio) as u32).max(50)
126    }
127
128    /// Token budget for history carryover across renewal.
129    pub fn carryover_tokens(&self, max_tokens: u32) -> u32 {
130        ((max_tokens as f64 * self.carryover_ratio) as u32).max(100)
131    }
132
133    /// Token cap for a single recovery/replay payload.
134    pub fn recovery_content_tokens(&self, max_tokens: u32) -> u32 {
135        (max_tokens as f64 * self.recovery_content_ratio) as u32
136    }
137
138    /// Auto-Compact trigger threshold (Layer 5).
139    /// Returns `max_tokens - autocompact_buffer` (absolute value).
140    pub fn autocompact_threshold(&self, max_tokens: u32) -> u32 {
141        max_tokens.saturating_sub(self.autocompact_buffer)
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn noise_reduction_defaults_to_quiet() {
151        let c = ContextConfig::default();
152        assert!(!c.render_dashboard, "dashboard should be off by default");
153        assert!(!c.verbose_control_notes, "verbose notes should be off by default");
154    }
155
156    #[test]
157    fn default_thresholds_strictly_increasing() {
158        let c = ContextConfig::default();
159        assert!(c.snip_threshold < c.micro_threshold);
160        assert!(c.micro_threshold < c.collapse_threshold);
161        assert!(c.collapse_threshold < c.auto_threshold);
162        assert!(c.auto_threshold < c.renewal_threshold);
163    }
164
165    #[test]
166    fn target_after_compress_below_snip_threshold() {
167        let c = ContextConfig::default();
168        assert!(c.target_after_compress < c.snip_threshold);
169    }
170
171    #[test]
172    fn derived_limits_scale_with_max_tokens() {
173        let c = ContextConfig::default();
174        let small = 8_000u32;
175        let large = 200_000u32;
176        let ratio = c.snip_per_msg_tokens(large) as f64 / c.snip_per_msg_tokens(small) as f64;
177        assert!((ratio - 25.0).abs() < 1.0, "expected ~25×, got {ratio}");
178    }
179
180    #[test]
181    fn small_context_window_has_floor() {
182        let c = ContextConfig::default();
183        assert!(c.snip_per_msg_tokens(100) >= 50);
184        assert!(c.carryover_tokens(100) >= 100);
185    }
186}