-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstrategy.rs
More file actions
341 lines (297 loc) · 10.9 KB
/
Copy pathstrategy.rs
File metadata and controls
341 lines (297 loc) · 10.9 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
//! Compaction strategy - decides what to evict
//!
//! Implements smart eviction that:
//! - Preserves a retention window of recent messages
//! - Avoids splitting tool call from its result
//! - Handles droppable messages appropriately
use serde::{Deserialize, Serialize};
/// Role of a message in conversation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageRole {
System,
User,
Assistant,
Tool,
}
/// Metadata about a message for eviction decisions
#[derive(Debug, Clone)]
pub struct MessageMeta {
/// Index in the message list
pub index: usize,
/// Role of the message
pub role: MessageRole,
/// Whether this message can be dropped entirely (ephemeral)
pub droppable: bool,
/// Whether this message contains a tool call
pub has_tool_call: bool,
/// Whether this is a tool result message
pub is_tool_result: bool,
/// Associated tool call ID (for matching call to result)
pub tool_id: Option<String>,
/// Estimated token count
pub token_count: usize,
}
/// Range of messages to evict
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EvictionRange {
/// Start index (inclusive)
pub start: usize,
/// End index (exclusive)
pub end: usize,
}
impl EvictionRange {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub fn len(&self) -> usize {
self.end.saturating_sub(self.start)
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
/// Strategy for choosing what to evict
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompactionStrategy {
/// Evict a percentage of messages
Evict(f64),
/// Retain the last N messages
Retain(usize),
/// Take minimum of two strategies (more conservative)
Min(Box<CompactionStrategy>, Box<CompactionStrategy>),
/// Take maximum of two strategies (more aggressive)
Max(Box<CompactionStrategy>, Box<CompactionStrategy>),
}
impl Default for CompactionStrategy {
fn default() -> Self {
// Default: evict 60% or retain last 10, whichever is more conservative
Self::Min(Box::new(Self::Evict(0.6)), Box::new(Self::Retain(10)))
}
}
impl CompactionStrategy {
/// Calculate eviction range based on strategy
///
/// # Arguments
/// * `messages` - Metadata about all messages
/// * `retention_window` - Minimum messages to always keep
///
/// # Returns
/// The range of messages to evict, adjusted for safety
pub fn calculate_eviction_range(
&self,
messages: &[MessageMeta],
retention_window: usize,
) -> Option<EvictionRange> {
if messages.len() <= retention_window {
return None; // Nothing to evict
}
let raw_end = self.calculate_raw_end(messages.len(), retention_window);
// Find safe start: first assistant message (skip initial system/user)
let start = Self::find_safe_start(messages);
if start >= raw_end {
return None; // Nothing to evict
}
// Adjust end to avoid splitting tool call/result pairs
let end = Self::adjust_end_for_tool_safety(messages, raw_end, retention_window);
if start >= end {
return None;
}
Some(EvictionRange::new(start, end))
}
/// Calculate raw end index based on strategy type
fn calculate_raw_end(&self, total: usize, retention_window: usize) -> usize {
match self {
Self::Evict(fraction) => {
let evict_count = (total as f64 * fraction).floor() as usize;
total.saturating_sub(retention_window).min(evict_count)
}
Self::Retain(keep) => total.saturating_sub(*keep.max(&retention_window)),
Self::Min(a, b) => {
let end_a = a.calculate_raw_end(total, retention_window);
let end_b = b.calculate_raw_end(total, retention_window);
end_a.min(end_b)
}
Self::Max(a, b) => {
let end_a = a.calculate_raw_end(total, retention_window);
let end_b = b.calculate_raw_end(total, retention_window);
end_a.max(end_b)
}
}
}
/// Find safe start index (first assistant message)
fn find_safe_start(messages: &[MessageMeta]) -> usize {
messages
.iter()
.position(|m| m.role == MessageRole::Assistant)
.unwrap_or(0)
}
/// Adjust end index to avoid splitting tool call from result
fn adjust_end_for_tool_safety(
messages: &[MessageMeta],
mut end: usize,
retention_window: usize,
) -> usize {
let min_end = messages.len().saturating_sub(retention_window);
// Don't go past minimum retention
if end > min_end {
end = min_end;
}
if end == 0 || end >= messages.len() {
return end;
}
// Check if we're splitting a tool call from its result
// Look at message at end-1 (last message to evict)
let last_evicted = &messages[end - 1];
if last_evicted.has_tool_call {
// We're evicting a tool call - need to also evict its result
// Find the tool result with matching ID
if let Some(tool_id) = &last_evicted.tool_id {
for i in end..messages.len().min(end + 5) {
if messages[i].is_tool_result && messages[i].tool_id.as_ref() == Some(tool_id) {
// Found matching result - extend eviction to include it
end = i + 1;
break;
}
}
}
}
// Check if we're about to evict a tool result without its call
let msg_at_end = messages.get(end);
if let Some(msg) = msg_at_end
&& msg.is_tool_result
{
// We're keeping a tool result - make sure we also keep its call
// Move end back to before this tool result group
while end > 0 {
let prev = &messages[end - 1];
if prev.is_tool_result || prev.has_tool_call {
end -= 1;
} else {
break;
}
}
}
// Final safety: don't end in the middle of a tool result sequence
while end > 0 && end < messages.len() {
if messages[end].is_tool_result {
end -= 1;
} else {
break;
}
}
end
}
/// Filter out droppable messages from a range
/// Returns indices of non-droppable messages to summarize
pub fn filter_droppable(messages: &[MessageMeta], range: &EvictionRange) -> Vec<usize> {
(range.start..range.end)
.filter(|&i| !messages[i].droppable)
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_messages(roles: &[(MessageRole, bool, bool)]) -> Vec<MessageMeta> {
roles
.iter()
.enumerate()
.map(|(i, (role, has_tool_call, is_tool_result))| MessageMeta {
index: i,
role: *role,
droppable: false,
has_tool_call: *has_tool_call,
is_tool_result: *is_tool_result,
tool_id: if *has_tool_call || *is_tool_result {
Some(format!("tool_{}", i))
} else {
None
},
token_count: 100,
})
.collect()
}
#[test]
fn test_eviction_range_empty() {
let strategy = CompactionStrategy::Retain(10);
let messages = make_messages(&[
(MessageRole::System, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, false, false),
]);
let range = strategy.calculate_eviction_range(&messages, 5);
assert!(range.is_none());
}
#[test]
fn test_eviction_starts_at_assistant() {
let strategy = CompactionStrategy::Evict(0.5);
let messages = make_messages(&[
(MessageRole::System, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, false, false),
]);
let range = strategy.calculate_eviction_range(&messages, 2);
assert!(range.is_some());
let range = range.unwrap();
// Should start at index 2 (first assistant)
assert_eq!(range.start, 2);
}
#[test]
fn test_tool_call_result_adjacency() {
let mut messages = make_messages(&[
(MessageRole::System, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, true, false), // has tool call
(MessageRole::Tool, false, true), // tool result
(MessageRole::Assistant, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, false, false),
]);
// Set matching tool IDs
messages[2].tool_id = Some("call_1".to_string());
messages[3].tool_id = Some("call_1".to_string());
let strategy = CompactionStrategy::Retain(2);
let range = strategy.calculate_eviction_range(&messages, 2);
// Should either evict both tool call and result, or neither
if let Some(range) = range {
// If evicting, should include both call and result
if range.end > 2 && range.end <= 3 {
panic!("Eviction split tool call from result!");
}
}
}
#[test]
fn test_filter_droppable() {
let mut messages = make_messages(&[
(MessageRole::System, false, false),
(MessageRole::User, false, false),
(MessageRole::Assistant, false, false),
(MessageRole::User, false, false), // droppable
(MessageRole::Assistant, false, false),
]);
messages[3].droppable = true;
let range = EvictionRange::new(0, 5);
let non_droppable = CompactionStrategy::filter_droppable(&messages, &range);
assert_eq!(non_droppable.len(), 4);
assert!(!non_droppable.contains(&3));
}
#[test]
fn test_min_strategy() {
let strategy = CompactionStrategy::Min(
Box::new(CompactionStrategy::Evict(0.8)),
Box::new(CompactionStrategy::Retain(5)),
);
// With 10 messages:
// Evict(0.8) would evict 8, keeping 2
// Retain(5) would evict 5, keeping 5
// Min should be more conservative = evict less = end at 5
let messages = make_messages(&vec![(MessageRole::Assistant, false, false); 10]);
let range = strategy.calculate_eviction_range(&messages, 3);
assert!(range.is_some());
// Min strategy should be more conservative
}
}