forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompaction.rs
More file actions
1779 lines (1559 loc) · 68.4 KB
/
compaction.rs
File metadata and controls
1779 lines (1559 loc) · 68.4 KB
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Background compaction for conversation context management
//!
//! When context reaches 80% of the limit, kicks off background summarization.
//! User continues chatting while summary is generated. When ready, seamlessly
//! swaps in the compacted context.
//!
//! The CompactionManager does NOT store its own copy of messages. Instead,
//! callers pass `&[Message]` references when needed. The manager tracks how
//! many messages from the front have been compacted via `compacted_count`.
//!
//! ## Compaction Modes
//!
//! - **Reactive** (default): compact when context hits a fixed threshold (80%).
//! - **Proactive**: compact early based on predicted EWMA token growth rate.
//! - **Semantic**: compact based on embedding-detected topic shifts and
//! relevance scoring. Falls back to proactive if embeddings are unavailable.
use crate::message::{ContentBlock, Message, Role};
use crate::provider::Provider;
use anyhow::Result;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::Instant;
use tokio::task::JoinHandle;
/// Default token budget (200k tokens - matches Claude's actual context limit)
const DEFAULT_TOKEN_BUDGET: usize = 200_000;
/// Trigger compaction at this percentage of budget
const COMPACTION_THRESHOLD: f32 = 0.80;
/// If context is above this threshold when compaction starts, do a synchronous
/// hard-compact (drop old messages) so the API call doesn't fail.
const CRITICAL_THRESHOLD: f32 = 0.95;
/// Minimum threshold for manual compaction (can compact at any time above this)
const MANUAL_COMPACT_MIN_THRESHOLD: f32 = 0.10;
/// Keep this many recent turns verbatim (not summarized)
const RECENT_TURNS_TO_KEEP: usize = 10;
/// Absolute minimum turns to keep during emergency compaction
const MIN_TURNS_TO_KEEP: usize = 2;
/// Max chars for a single tool result during emergency truncation
const EMERGENCY_TOOL_RESULT_MAX_CHARS: usize = 4000;
/// Approximate chars per token for estimation
const CHARS_PER_TOKEN: usize = 4;
/// Fixed token overhead for system prompt + tool definitions.
/// These are not counted in message content but do count toward the context limit.
/// Estimated conservatively: ~8k tokens for system prompt + ~10k for 50+ tools.
const SYSTEM_OVERHEAD_TOKENS: usize = 18_000;
// ── Proactive mode constants ────────────────────────────────────────────────
/// Rolling window size for token history (proactive/semantic modes)
const TOKEN_HISTORY_WINDOW: usize = 20;
// ── Semantic mode constants ─────────────────────────────────────────────────
/// Maximum characters to embed per message (first N chars capture semantic content)
const EMBED_MAX_CHARS_PER_MSG: usize = 512;
/// Rolling window of per-turn embeddings used for topic-shift detection
const EMBEDDING_HISTORY_WINDOW: usize = 10;
/// Per-manager semantic embedding cache capacity.
///
/// This avoids repeated embedding lookups for the same truncated message and
/// goal texts across successive semantic compaction checks.
const SEMANTIC_EMBED_CACHE_CAPACITY: usize = 256;
const SUMMARY_PROMPT: &str = r#"Summarize our conversation so you can continue this work later.
Write in natural language with these sections:
- **Context:** What we're working on and why (1-2 sentences)
- **What we did:** Key actions taken, files changed, problems solved
- **Current state:** What works, what's broken, what's next
- **User preferences:** Specific requirements or decisions they made
Be concise but preserve important details. You can search the full conversation later if you need exact error messages or code snippets."#;
/// A completed summary covering turns up to a certain point
#[derive(Debug, Clone)]
pub struct Summary {
pub text: String,
pub openai_encrypted_content: Option<String>,
pub covers_up_to_turn: usize,
pub original_turn_count: usize,
}
/// Event emitted when compaction is applied
#[derive(Debug, Clone)]
pub struct CompactionEvent {
pub trigger: String,
pub pre_tokens: Option<u64>,
pub post_tokens: Option<u64>,
pub tokens_saved: Option<u64>,
pub duration_ms: Option<u64>,
pub messages_dropped: Option<usize>,
pub messages_compacted: Option<usize>,
pub summary_chars: Option<usize>,
pub active_messages: Option<usize>,
}
/// What happened when ensure_context_fits was called
#[derive(Debug, Clone, PartialEq)]
pub enum CompactionAction {
/// Nothing needed — context is fine
None,
/// Background summarization started (context 80-95%)
BackgroundStarted { trigger: String },
/// Emergency hard compact performed (context >= 95%)
/// Contains number of messages dropped
HardCompacted(usize),
}
/// Result from background compaction task
struct CompactionResult {
summary_text: String,
openai_encrypted_content: Option<String>,
covers_up_to_turn: usize,
duration_ms: u64,
summarized_messages: usize,
}
/// Manages background compaction of conversation context.
///
/// Does NOT own message data. The caller owns the messages and passes
/// references into methods that need them. After compaction, the manager
/// records `compacted_count` — the number of leading messages that have
/// been summarized and should be skipped when building API payloads.
pub struct CompactionManager {
/// Number of leading messages that have been compacted into the summary.
/// When building API messages, skip the first `compacted_count` messages.
compacted_count: usize,
/// Active summary (if we've compacted before)
active_summary: Option<Summary>,
/// Rolling char estimate for the active (non-compacted) message suffix.
///
/// In the common append-only case this is maintained incrementally, so token
/// estimation does not need to rescan the entire active history every time.
active_message_chars: usize,
/// When true, the incremental char estimate must be recomputed from the
/// caller's full message list before it can be trusted.
active_message_chars_dirty: bool,
/// Background compaction task handle
pending_task: Option<JoinHandle<Result<CompactionResult>>>,
/// User-facing trigger label for the currently running background compaction.
pending_trigger: Option<String>,
/// Turn index (relative to uncompacted messages) where pending compaction will cut off
pending_cutoff: usize,
/// Total turns seen (for tracking)
total_turns: usize,
/// When true, session restore/reseed has just loaded old history and
/// compaction must stay disabled until a genuinely new message is added.
suppress_compaction_until_new_message: bool,
/// Token budget
token_budget: usize,
/// Provider-reported input token usage from the latest request.
/// Used to trigger compaction with real token counts instead of only heuristics.
observed_input_tokens: Option<u64>,
/// Last compaction event (if any)
last_compaction: Option<CompactionEvent>,
// ── Mode & strategy ────────────────────────────────────────────────────
/// Active compaction mode (set from config at construction)
mode: crate::config::CompactionMode,
/// Config snapshot for mode-specific parameters
compaction_config: crate::config::CompactionConfig,
// ── Proactive mode state ───────────────────────────────────────────────
/// Rolling window of observed token counts, one entry per turn snapshot.
/// Used to compute EWMA growth rate for proactive compaction.
token_history: VecDeque<u64>,
/// Total turns elapsed since the last successful compaction.
/// Used as a cooldown anti-signal.
turns_since_last_compact: usize,
// ── Semantic mode state ────────────────────────────────────────────────
/// Per-turn embedding snapshots for topic-shift detection.
/// Each entry is the L2-normalized embedding of the last assistant message
/// of that turn (truncated to EMBED_MAX_CHARS_PER_MSG for speed).
embedding_history: VecDeque<Vec<f32>>,
/// Local cache for semantic compaction embeddings keyed by truncated-text hash.
/// Stores both successful embeddings and failed lookups (`None`) so repeated
/// semantic scans do not redo the same work.
semantic_embed_cache: HashMap<u64, (Option<Vec<f32>>, u64)>,
/// Monotonic recency counter for the semantic embedding cache LRU.
semantic_embed_cache_counter: u64,
}
impl CompactionManager {
pub fn new() -> Self {
let cfg = crate::config::config().compaction.clone();
let mode = cfg.mode.clone();
Self {
compacted_count: 0,
active_summary: None,
active_message_chars: 0,
active_message_chars_dirty: false,
pending_task: None,
pending_trigger: None,
pending_cutoff: 0,
total_turns: 0,
suppress_compaction_until_new_message: false,
token_budget: DEFAULT_TOKEN_BUDGET,
observed_input_tokens: None,
last_compaction: None,
mode,
compaction_config: cfg,
token_history: VecDeque::with_capacity(TOKEN_HISTORY_WINDOW + 1),
turns_since_last_compact: 0,
embedding_history: VecDeque::with_capacity(EMBEDDING_HISTORY_WINDOW + 1),
semantic_embed_cache: HashMap::with_capacity(SEMANTIC_EMBED_CACHE_CAPACITY),
semantic_embed_cache_counter: 0,
}
}
/// Reset all compaction state
pub fn reset(&mut self) {
*self = Self::new();
}
pub fn with_budget(mut self, budget: usize) -> Self {
self.token_budget = budget;
self
}
/// Update the token budget (e.g., when model changes)
pub fn set_budget(&mut self, budget: usize) {
self.token_budget = budget;
}
/// Get current token budget
pub fn token_budget(&self) -> usize {
self.token_budget
}
/// Notify the manager that a message was added.
///
/// Legacy callers that do not provide the message content keep turn counts
/// correct, but mark the rolling char estimate dirty so the next token
/// estimate will resync from the provided history slice.
pub fn notify_message_added(&mut self) {
self.total_turns += 1;
self.suppress_compaction_until_new_message = false;
self.active_message_chars_dirty = true;
}
/// Notify the manager that a message was added and update the rolling char
/// estimate incrementally.
pub fn notify_message_added_with(&mut self, message: &Message) {
self.notify_message_added_blocks(&message.content);
}
pub fn notify_message_added_blocks(&mut self, content: &[ContentBlock]) {
self.total_turns += 1;
self.suppress_compaction_until_new_message = false;
self.active_message_chars = self
.active_message_chars
.saturating_add(Self::content_char_count(content));
self.active_message_chars_dirty = false;
}
/// Backward-compatible alias for `notify_message_added`.
/// Accepts (and ignores) the message — callers that haven't been
/// updated yet can still call `add_message(msg)`.
pub fn add_message(&mut self, message: Message) {
self.notify_message_added_with(&message);
}
/// Seed the manager from already-existing history that was restored from
/// disk or otherwise replayed into memory.
///
/// This updates turn counts but deliberately suppresses compaction until a
/// genuinely new message is added after the restore. Restoring history must
/// not itself trigger compaction.
pub fn seed_restored_messages(&mut self, count: usize) {
self.total_turns = count;
self.suppress_compaction_until_new_message = count > 0;
self.active_message_chars = 0;
self.active_message_chars_dirty = count > 0;
}
/// Seed the manager from already-existing history with an exact rolling char
/// estimate for the active suffix.
pub fn seed_restored_messages_with(&mut self, all_messages: &[Message]) {
self.total_turns = all_messages.len();
self.suppress_compaction_until_new_message = !all_messages.is_empty();
self.active_message_chars = all_messages.iter().map(Self::message_char_count).sum();
self.active_message_chars_dirty = false;
}
pub fn seed_restored_stored_messages_with(
&mut self,
all_messages: &[crate::session::StoredMessage],
) {
self.total_turns = all_messages.len();
self.suppress_compaction_until_new_message = !all_messages.is_empty();
self.active_message_chars = all_messages
.iter()
.map(|message| Self::content_char_count(&message.content))
.sum();
self.active_message_chars_dirty = false;
}
/// Restore a previously persisted compacted view.
pub fn restore_persisted_state(
&mut self,
state: &crate::session::StoredCompactionState,
total_messages: usize,
) {
self.pending_task = None;
self.pending_trigger = None;
self.pending_cutoff = 0;
self.observed_input_tokens = None;
self.last_compaction = None;
self.token_history.clear();
self.turns_since_last_compact = 0;
self.embedding_history.clear();
self.semantic_embed_cache.clear();
self.semantic_embed_cache_counter = 0;
self.total_turns = total_messages;
self.compacted_count = state.compacted_count.min(total_messages);
self.active_message_chars = 0;
self.active_message_chars_dirty = total_messages > self.compacted_count;
self.active_summary = Some(Summary {
text: state.summary_text.clone(),
openai_encrypted_content: state.openai_encrypted_content.clone(),
covers_up_to_turn: state.covers_up_to_turn,
original_turn_count: state.original_turn_count,
});
self.suppress_compaction_until_new_message = total_messages > 0;
}
/// Restore persisted compaction state and compute the active-suffix char
/// estimate from the provided full message list.
pub fn restore_persisted_state_with(
&mut self,
state: &crate::session::StoredCompactionState,
all_messages: &[Message],
) {
self.restore_persisted_state(state, all_messages.len());
self.active_message_chars = self
.active_messages(all_messages)
.iter()
.map(Self::message_char_count)
.sum();
self.active_message_chars_dirty = false;
}
pub fn restore_persisted_stored_state_with(
&mut self,
state: &crate::session::StoredCompactionState,
all_messages: &[crate::session::StoredMessage],
) {
self.restore_persisted_state(state, all_messages.len());
let start = self.compacted_count.min(all_messages.len());
self.active_message_chars = all_messages[start..]
.iter()
.map(|message| Self::content_char_count(&message.content))
.sum();
self.active_message_chars_dirty = false;
}
/// Export the currently active compacted view for persistence.
pub fn persisted_state(&self) -> Option<crate::session::StoredCompactionState> {
self.active_summary
.as_ref()
.map(|summary| crate::session::StoredCompactionState {
summary_text: summary.text.clone(),
openai_encrypted_content: summary.openai_encrypted_content.clone(),
covers_up_to_turn: summary.covers_up_to_turn,
original_turn_count: summary.original_turn_count,
compacted_count: self.compacted_count,
})
}
// ── Token snapshot (proactive mode) ────────────────────────────────────
/// Record the observed token count after a completed turn.
///
/// Called by the agent after `update_compaction_usage_from_stream`.
/// Pushes the value into the rolling history window used by the proactive
/// and semantic modes. Also increments the cooldown counter.
pub fn push_token_snapshot(&mut self, tokens: u64) {
self.token_history.push_back(tokens);
if self.token_history.len() > TOKEN_HISTORY_WINDOW {
self.token_history.pop_front();
}
self.turns_since_last_compact += 1;
}
/// Record an embedding snapshot for the current turn (semantic mode).
///
/// `text` should be a short representation of the turn's assistant output
/// (first EMBED_MAX_CHARS_PER_MSG chars). Silently skipped if the
/// embedding model is unavailable.
pub fn push_embedding_snapshot(&mut self, text: &str) {
let snippet: String = text.chars().take(EMBED_MAX_CHARS_PER_MSG).collect();
if let Some(emb) = self.cached_semantic_embedding(&snippet) {
self.embedding_history.push_back(emb);
if self.embedding_history.len() > EMBEDDING_HISTORY_WINDOW {
self.embedding_history.pop_front();
}
}
}
// ── Anti-signal guard (shared by proactive + semantic) ──────────────────
/// Returns `true` when any anti-signal fires and we should NOT compact
/// proactively right now.
///
/// Anti-signals are universal guards applied before the mode-specific
/// trigger logic. They prevent wasted work and respect user intent.
fn anti_signals_block(&self, all_messages: &[Message]) -> bool {
let cfg = &self.compaction_config;
// 1. Already compacting — never double-trigger.
if self.pending_task.is_some() {
return true;
}
// 2. Context below the proactive floor — too early regardless of trend.
let usage = self.context_usage_with(all_messages);
if usage < cfg.proactive_floor {
return true;
}
// 3. Not enough token history to project from.
if self.token_history.len() < cfg.min_samples {
return true;
}
// 4. Growth has stalled: last stall_window snapshots show no increase.
// If tokens haven't grown, there's no urgency.
if self.token_history.len() >= cfg.stall_window {
let recent: Vec<u64> = self
.token_history
.iter()
.rev()
.take(cfg.stall_window)
.cloned()
.collect();
let oldest = recent[recent.len() - 1];
let newest = recent[0];
if newest <= oldest {
return true;
}
}
// 5. Cooldown: too soon after the last compaction.
if self.turns_since_last_compact < cfg.min_turns_between_compactions {
return true;
}
false
}
// ── Proactive mode trigger ──────────────────────────────────────────────
/// Returns `true` if the proactive strategy thinks we should compact now.
///
/// Uses an EWMA over the token history to project forward `lookahead_turns`
/// turns. If the projected token count would exceed the 80% threshold,
/// it's time to compact before we get there.
fn should_compact_proactively(&self, all_messages: &[Message]) -> bool {
if self.anti_signals_block(all_messages) {
return false;
}
let cfg = &self.compaction_config;
let budget = self.token_budget as f64;
let threshold = COMPACTION_THRESHOLD as f64 * budget;
// Compute EWMA of per-turn token deltas.
// We need at least 2 snapshots to get a delta.
let snapshots: Vec<u64> = self.token_history.iter().cloned().collect();
if snapshots.len() < 2 {
return false;
}
let alpha = cfg.ewma_alpha as f64;
let mut ewma_delta: f64 = (snapshots[1] as f64) - (snapshots[0] as f64);
ewma_delta = ewma_delta.max(0.0);
for i in 2..snapshots.len() {
let delta = ((snapshots[i] as f64) - (snapshots[i - 1] as f64)).max(0.0);
ewma_delta = alpha * delta + (1.0 - alpha) * ewma_delta;
}
let Some(current) = snapshots.last().copied().map(|value| value as f64) else {
return false;
};
let projected = current + ewma_delta * cfg.lookahead_turns as f64;
crate::logging::info(&format!(
"[compaction/proactive] current={:.0} ewma_delta={:.1}/turn projected@{}turns={:.0} threshold={:.0}",
current, ewma_delta, cfg.lookahead_turns, projected, threshold
));
projected >= threshold
}
// ── Semantic mode trigger ───────────────────────────────────────────────
/// Returns `true` if the semantic strategy detects a topic shift or
/// predicts we should compact now.
///
/// Topic-shift detection: compares the mean embedding of the oldest half
/// of the history window against the newest half. A low cosine similarity
/// between the two clusters indicates a topic boundary was crossed —
/// the previous topic is complete and safe to summarize.
///
/// Falls back to proactive logic if embeddings are unavailable.
fn should_compact_semantic(&self, all_messages: &[Message]) -> bool {
if self.anti_signals_block(all_messages) {
return false;
}
// Need enough embedding history to split into two halves.
let history_len = self.embedding_history.len();
if history_len < 4 {
// Fall back to proactive trigger.
return self.should_compact_proactively(all_messages);
}
let cfg = &self.compaction_config;
let half = history_len / 2;
let old_embeddings: Vec<&Vec<f32>> = self.embedding_history.iter().take(half).collect();
let new_embeddings: Vec<&Vec<f32>> = self.embedding_history.iter().skip(half).collect();
let dim = old_embeddings[0].len();
// Compute mean embedding for each half.
let mean_old = mean_embedding(&old_embeddings, dim);
let mean_new = mean_embedding(&new_embeddings, dim);
let similarity = crate::embedding::cosine_similarity(&mean_old, &mean_new);
crate::logging::info(&format!(
"[compaction/semantic] topic similarity (old vs new half) = {:.3} (threshold={:.2})",
similarity, cfg.topic_shift_threshold
));
if similarity < cfg.topic_shift_threshold {
crate::logging::info(
"[compaction/semantic] Topic shift detected — triggering proactive compaction",
);
return true;
}
// No topic shift — still fall back to proactive growth check.
self.should_compact_proactively(all_messages)
}
/// Build a relevance-scored keep set for semantic compaction.
///
/// Embeds the last `goal_window_turns` messages to represent the current
/// goal, then scores all active messages by cosine similarity. Returns the
/// cutoff index: messages before the cutoff will be summarized, messages at
/// or after are kept verbatim.
///
/// Messages above `relevance_keep_threshold` anywhere in the history are
/// pulled out of the summarize set. Falls back to the standard recency
/// cutoff if embeddings fail.
fn semantic_cutoff(&mut self, active: &[Message]) -> usize {
let goal_window_turns = self.compaction_config.goal_window_turns;
let relevance_keep_threshold = self.compaction_config.relevance_keep_threshold;
let standard_cutoff = active.len().saturating_sub(RECENT_TURNS_TO_KEEP);
if standard_cutoff == 0 {
return 0;
}
// Build goal text from recent turns.
let goal_turns = goal_window_turns.min(active.len());
let goal_text = Self::semantic_goal_text(&active[active.len() - goal_turns..]);
if goal_text.is_empty() {
return standard_cutoff;
}
let goal_emb = match self.cached_semantic_embedding(&goal_text) {
Some(embedding) => embedding,
None => return standard_cutoff,
};
// Score each candidate message (those before standard_cutoff).
let mut high_relevance_count = 0usize;
let mut earliest_high_relevance = standard_cutoff;
for (idx, msg) in active[..standard_cutoff].iter().enumerate() {
let text = Self::semantic_message_text(msg);
if text.is_empty() {
continue;
}
if let Some(embedding) = self.cached_semantic_embedding(&text) {
let sim = crate::embedding::cosine_similarity(&goal_emb, &embedding);
if sim >= relevance_keep_threshold {
high_relevance_count += 1;
earliest_high_relevance = earliest_high_relevance.min(idx);
}
}
}
if high_relevance_count == 0 {
return standard_cutoff;
}
// Find the latest high-relevance message before standard_cutoff.
// We can't have gaps in the summarized range (tool call integrity),
// so we move the cutoff up to just before the earliest high-relevance
// message in the tail of the compaction range.
let adjusted_cutoff = earliest_high_relevance;
// Ensure we actually compact something meaningful.
if adjusted_cutoff < 2 {
return standard_cutoff;
}
crate::logging::info(&format!(
"[compaction/semantic] relevance scoring: {} high-relevance msgs kept, cutoff {} -> {}",
high_relevance_count, standard_cutoff, adjusted_cutoff
));
adjusted_cutoff
}
/// Get the active (uncompacted) messages from a full message list.
/// Skips the first `compacted_count` messages.
fn active_messages<'a>(&self, all_messages: &'a [Message]) -> &'a [Message] {
if self.compacted_count <= all_messages.len() {
&all_messages[self.compacted_count..]
} else {
// Edge case: messages were cleared/replaced with fewer items
all_messages
}
}
fn active_message_chars_with(&self, all_messages: &[Message]) -> usize {
if self.active_message_chars_dirty
|| self.active_messages_count() != self.active_messages(all_messages).len()
{
self.active_messages(all_messages)
.iter()
.map(Self::message_char_count)
.sum()
} else {
self.active_message_chars
}
}
/// Get current token estimate using the caller's message list
pub fn token_estimate_with(&self, all_messages: &[Message]) -> usize {
let mut total_chars = 0;
if let Some(ref summary) = self.active_summary {
total_chars += summary
.openai_encrypted_content
.as_ref()
.map(|s| s.len())
.unwrap_or_else(|| summary.text.len());
}
total_chars += self.active_message_chars_with(all_messages);
let msg_tokens = total_chars / CHARS_PER_TOKEN;
// Add overhead for system prompt + tool definitions, which are not in the message list
// but do count toward the context limit. Scale the overhead to the budget so
// tests with tiny budgets aren't affected.
let overhead = if self.token_budget >= DEFAULT_TOKEN_BUDGET / 2 {
SYSTEM_OVERHEAD_TOKENS
} else {
0
};
msg_tokens + overhead
}
/// Get current token estimate (backward compat — uses 0 messages, only summary + observed)
pub fn token_estimate(&self) -> usize {
let mut total_chars = 0;
if let Some(ref summary) = self.active_summary {
total_chars += summary
.openai_encrypted_content
.as_ref()
.map(|s| s.len())
.unwrap_or_else(|| summary.text.len());
}
let msg_tokens = total_chars / CHARS_PER_TOKEN;
let overhead = if self.token_budget >= DEFAULT_TOKEN_BUDGET / 2 {
SYSTEM_OVERHEAD_TOKENS
} else {
0
};
msg_tokens + overhead
}
/// Store provider-reported input token usage for compaction decisions.
pub fn update_observed_input_tokens(&mut self, tokens: u64) {
self.observed_input_tokens = Some(tokens);
}
/// Best-effort current token count using the caller's messages.
pub fn effective_token_count_with(&self, all_messages: &[Message]) -> usize {
let estimate = self.token_estimate_with(all_messages);
let observed = self
.observed_input_tokens
.and_then(|tokens| usize::try_from(tokens).ok())
.unwrap_or(0);
estimate.max(observed)
}
/// Best-effort token count without message data (uses only observed tokens)
pub fn effective_token_count(&self) -> usize {
let estimate = self.token_estimate();
let observed = self
.observed_input_tokens
.and_then(|tokens| usize::try_from(tokens).ok())
.unwrap_or(0);
estimate.max(observed)
}
/// Get current context usage as percentage (using caller's messages)
pub fn context_usage_with(&self, all_messages: &[Message]) -> f32 {
self.effective_token_count_with(all_messages) as f32 / self.token_budget as f32
}
/// Get current context usage (without messages, uses observed tokens only)
pub fn context_usage(&self) -> f32 {
self.effective_token_count() as f32 / self.token_budget as f32
}
/// Check if we should start compaction
pub fn should_compact_with(&self, all_messages: &[Message]) -> bool {
use crate::config::CompactionMode;
if self.suppress_compaction_until_new_message {
return false;
}
let active = self.active_messages(all_messages);
match self.mode {
CompactionMode::Reactive => {
self.pending_task.is_none()
&& self.context_usage_with(all_messages) >= COMPACTION_THRESHOLD
&& active.len() > RECENT_TURNS_TO_KEEP
}
CompactionMode::Proactive => {
active.len() > RECENT_TURNS_TO_KEEP && self.should_compact_proactively(all_messages)
}
CompactionMode::Semantic => {
active.len() > RECENT_TURNS_TO_KEEP && self.should_compact_semantic(all_messages)
}
}
}
/// Start background compaction if needed
pub fn maybe_start_compaction_with(
&mut self,
all_messages: &[Message],
provider: Arc<dyn Provider>,
) {
if !self.should_compact_with(all_messages) {
return;
}
let active = self.active_messages(all_messages);
// Calculate cutoff within active messages.
// Semantic mode uses relevance scoring; other modes use recency.
let mut cutoff = match self.mode {
crate::config::CompactionMode::Semantic => self.semantic_cutoff(active),
_ => active.len().saturating_sub(RECENT_TURNS_TO_KEEP),
};
if cutoff == 0 {
return;
}
// Adjust cutoff to not split tool call/result pairs
cutoff = Self::safe_cutoff_static(active, cutoff);
if cutoff == 0 {
return;
}
// Snapshot messages to summarize (must clone for the async task)
let messages_to_summarize: Vec<Message> = active[..cutoff].to_vec();
let msg_count = messages_to_summarize.len();
let existing_summary = self.active_summary.clone();
let mode_label = self.mode_trigger_label().to_string();
let estimated_tokens = self.effective_token_count_with(all_messages);
crate::logging::info(&format!(
"[TIMING] compaction_start: trigger={}, active_messages={}, cutoff={}, estimated_tokens={}, has_existing_summary={}",
mode_label,
active.len(),
cutoff,
estimated_tokens,
existing_summary.is_some(),
));
self.pending_cutoff = cutoff;
self.pending_trigger = Some(mode_label.clone());
// Spawn background task that notifies via Bus when done
self.pending_task = Some(tokio::spawn(async move {
let start = std::time::Instant::now();
let result =
generate_compaction_artifact(provider, messages_to_summarize, existing_summary)
.await;
let duration_ms = start.elapsed().as_millis() as u64;
crate::logging::info(&format!(
"Compaction ({}) finished in {:.2}s ({} messages summarized)",
mode_label,
duration_ms as f64 / 1000.0,
msg_count,
));
crate::bus::Bus::global().publish(crate::bus::BusEvent::CompactionFinished);
result.map(|mut result| {
result.duration_ms = duration_ms;
result.summarized_messages = msg_count;
result
})
}));
}
/// Ensure context fits before an API call.
///
/// Starts background compaction if above 80%. If context is critically full
/// (>=95%), also performs an immediate hard-compact (drops old messages) so
/// the next API call doesn't fail with "prompt too long".
pub fn ensure_context_fits(
&mut self,
all_messages: &[Message],
provider: Arc<dyn Provider>,
) -> CompactionAction {
let was_compacting = self.is_compacting();
self.maybe_start_compaction_with(all_messages, provider);
let bg_started = !was_compacting && self.is_compacting();
let usage = self.context_usage_with(all_messages);
if usage >= CRITICAL_THRESHOLD {
crate::logging::warn(&format!(
"[compaction] Context at {:.1}% (critical threshold {:.0}%) — performing synchronous hard compact",
usage * 100.0,
CRITICAL_THRESHOLD * 100.0,
));
match self.hard_compact_with(all_messages) {
Ok(dropped) => {
let post_usage = self.context_usage_with(all_messages);
crate::logging::info(&format!(
"[compaction] Hard compact dropped {} messages, context now at {:.1}%",
dropped,
post_usage * 100.0,
));
return CompactionAction::HardCompacted(dropped);
}
Err(reason) => {
crate::logging::error(&format!(
"[compaction] Hard compact failed at critical threshold: {}",
reason
));
}
}
}
if bg_started {
CompactionAction::BackgroundStarted {
trigger: self
.pending_trigger
.clone()
.unwrap_or_else(|| self.mode_trigger_label().to_string()),
}
} else {
CompactionAction::None
}
}
/// Backward-compatible wrapper
pub fn maybe_start_compaction(&mut self, _provider: Arc<dyn Provider>) {
// Without messages, we can only check observed tokens
// This is a no-op if no messages are provided
// Callers should migrate to maybe_start_compaction_with
}
/// Force immediate compaction (for manual /compact command).
pub fn force_compact_with(
&mut self,
all_messages: &[Message],
provider: Arc<dyn Provider>,
) -> Result<(), String> {
if self.pending_task.is_some() {
return Err("Compaction already in progress".to_string());
}
let active = self.active_messages(all_messages);
if active.len() <= RECENT_TURNS_TO_KEEP {
return Err(format!(
"Not enough messages to compact (need more than {}, have {})",
RECENT_TURNS_TO_KEEP,
active.len()
));
}
if self.context_usage_with(all_messages) < MANUAL_COMPACT_MIN_THRESHOLD {
return Err(format!(
"Context usage too low ({:.1}%) - nothing to compact",
self.context_usage_with(all_messages) * 100.0
));
}
let mut cutoff = active.len().saturating_sub(RECENT_TURNS_TO_KEEP);
if cutoff == 0 {
return Err("No messages available to compact after keeping recent turns".to_string());
}
cutoff = Self::safe_cutoff_static(active, cutoff);
if cutoff == 0 {
return Err("Cannot compact - would split tool call/result pairs".to_string());
}
let messages_to_summarize: Vec<Message> = active[..cutoff].to_vec();
let msg_count = messages_to_summarize.len();
let existing_summary = self.active_summary.clone();
self.pending_cutoff = cutoff;
self.pending_trigger = Some("manual".to_string());
self.pending_task = Some(tokio::spawn(async move {
let start = std::time::Instant::now();
let result =
generate_compaction_artifact(provider, messages_to_summarize, existing_summary)
.await;
let duration_ms = start.elapsed().as_millis() as u64;
crate::logging::info(&format!(
"Compaction finished in {:.2}s ({} messages summarized)",
duration_ms as f64 / 1000.0,
msg_count,
));
crate::bus::Bus::global().publish(crate::bus::BusEvent::CompactionFinished);
result.map(|mut result| {
result.duration_ms = duration_ms;
result.summarized_messages = msg_count;
result
})
}));
Ok(())
}
/// Backward-compatible force_compact (for callers that still have their own message vec).
/// This variant works with the old API where CompactionManager had its own messages.
/// Callers should migrate to force_compact_with.
pub fn force_compact(&mut self, _provider: Arc<dyn Provider>) -> Result<(), String> {
Err(
"force_compact requires messages — use force_compact_with(messages, provider)"
.to_string(),
)
}
/// Find a safe cutoff point that doesn't split tool call/result pairs.
/// Static version that works on a message slice.
fn safe_cutoff_static(messages: &[Message], initial_cutoff: usize) -> usize {
let mut cutoff = initial_cutoff;
// Track tool call/result ids in the kept portion.
let mut available_tool_ids = std::collections::HashSet::new();
let mut missing_tool_ids = std::collections::HashSet::new();
for msg in &messages[cutoff..] {
for block in &msg.content {
match block {
ContentBlock::ToolUse { id, .. } => {
available_tool_ids.insert(id.clone());
missing_tool_ids.remove(id);
}
ContentBlock::ToolResult { tool_use_id, .. } => {
if !available_tool_ids.contains(tool_use_id) {
missing_tool_ids.insert(tool_use_id.clone());
}
}
_ => {}
}
}