agent-message-window 0.1.0

Sliding window of recent LLM conversation turns with paired-protection: never drop a tool_use without its tool_result sibling. Zero deps (serde_json only).
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
/*!
agent-message-window: sliding window of LLM conversation turns with
paired tool_use / tool_result protection.

The Anthropic API rejects conversations that contain a `tool_result`
whose `tool_use` is no longer in the history. A naive "drop the oldest"
window silently corrupts the conversation. This crate's eviction logic
never drops a `tool_use` message without also evicting all `tool_result`
messages that reference it.

```rust
use agent_message_window::MessageWindow;
use serde_json::json;

let mut win = MessageWindow::new(4, true);
win.add(json!({"role": "user", "content": "hi"})).unwrap();
win.add(json!({"role": "assistant", "content": [
    {"type": "tool_use", "id": "u1", "name": "search", "input": {}}
]})).unwrap();
win.add(json!({"role": "user", "content": [
    {"type": "tool_result", "tool_use_id": "u1", "content": "ok"}
]})).unwrap();

// Even with max_turns=4, paired protection applies.
assert_eq!(win.len(), 3);
```
*/

use serde_json::Value;
use std::collections::{HashSet, VecDeque};

// ---- error ----------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WindowError {
    NotAnObject,
    MaxTurnsMustBePositive,
}

impl std::fmt::Display for WindowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WindowError::NotAnObject => write!(f, "message must be a JSON object"),
            WindowError::MaxTurnsMustBePositive => write!(f, "max_turns must be >= 1"),
        }
    }
}

impl std::error::Error for WindowError {}

// ---- helpers --------------------------------------------------------------

/// Collect `tool_use` IDs from an assistant message's `content` array.
fn tool_use_ids(message: &Value) -> HashSet<String> {
    let mut out = HashSet::new();
    if let Some(content) = message.get("content") {
        if let Some(blocks) = content.as_array() {
            for block in blocks {
                if block.get("type").and_then(|t| t.as_str()) == Some("tool_use") {
                    if let Some(id) = block.get("id").and_then(|i| i.as_str()) {
                        out.insert(id.to_owned());
                    }
                }
            }
        }
    }
    out
}

/// Collect `tool_use_id` references from a user message's `content` array.
fn tool_result_refs(message: &Value) -> HashSet<String> {
    let mut out = HashSet::new();
    if let Some(content) = message.get("content") {
        if let Some(blocks) = content.as_array() {
            for block in blocks {
                if block.get("type").and_then(|t| t.as_str()) == Some("tool_result") {
                    if let Some(id) = block.get("tool_use_id").and_then(|i| i.as_str()) {
                        out.insert(id.to_owned());
                    }
                }
            }
        }
    }
    out
}

// ---- MessageWindow --------------------------------------------------------

/// Sliding window over LLM conversation messages.
///
/// `max_turns` controls how many messages are kept. When adding a new
/// message causes overflow, the oldest is evicted first. With
/// `paired_protect = true` (the default), evicting a `tool_use` message
/// also evicts any `tool_result` messages that reference its IDs.
pub struct MessageWindow {
    max_turns: usize,
    paired_protect: bool,
    buf: VecDeque<Value>,
    evicted_count: usize,
}

impl MessageWindow {
    /// Create a new window with `max_turns` capacity.
    ///
    /// `paired_protect = true` guards Anthropic tool-use / tool-result pairs.
    pub fn new(max_turns: usize, paired_protect: bool) -> Self {
        assert!(max_turns >= 1, "max_turns must be >= 1");
        Self {
            max_turns,
            paired_protect,
            buf: VecDeque::new(),
            evicted_count: 0,
        }
    }

    /// Try to create; returns Err if max_turns < 1.
    pub fn try_new(max_turns: usize, paired_protect: bool) -> Result<Self, WindowError> {
        if max_turns < 1 {
            return Err(WindowError::MaxTurnsMustBePositive);
        }
        Ok(Self::new(max_turns, paired_protect))
    }

    pub fn max_turns(&self) -> usize {
        self.max_turns
    }

    pub fn len(&self) -> usize {
        self.buf.len()
    }

    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    pub fn evicted_count(&self) -> usize {
        self.evicted_count
    }

    /// Ordered slice of current messages (oldest → newest).
    pub fn messages(&self) -> Vec<&Value> {
        self.buf.iter().collect()
    }

    /// Cloned owned snapshot.
    pub fn messages_owned(&self) -> Vec<Value> {
        self.buf.iter().cloned().collect()
    }

    pub fn clear(&mut self) {
        self.buf.clear();
        self.evicted_count = 0;
    }

    /// Append a message and evict-to-fit.
    pub fn add(&mut self, message: Value) -> Result<(), WindowError> {
        if !message.is_object() {
            return Err(WindowError::NotAnObject);
        }
        self.buf.push_back(message);
        self.evict_to_fit();
        Ok(())
    }

    /// Add multiple messages in order.
    pub fn extend(&mut self, messages: impl IntoIterator<Item = Value>) -> Result<(), WindowError> {
        for m in messages {
            self.add(m)?;
        }
        Ok(())
    }

    // ---- eviction ---------------------------------------------------

    fn evict_to_fit(&mut self) {
        while self.buf.len() > self.max_turns {
            let oldest = self.buf.pop_front().expect("buf non-empty");
            self.evicted_count += 1;
            if self.paired_protect {
                let tu_ids = tool_use_ids(&oldest);
                if !tu_ids.is_empty() {
                    self.drop_orphan_tool_results(&tu_ids);
                }
            }
        }
    }

    /// Remove any buffered messages whose tool_result references are in `tu_ids`.
    fn drop_orphan_tool_results(&mut self, tu_ids: &HashSet<String>) {
        let mut kept = VecDeque::with_capacity(self.buf.len());
        for msg in self.buf.drain(..) {
            let refs = tool_result_refs(&msg);
            if refs.iter().any(|id| tu_ids.contains(id)) {
                self.evicted_count += 1;
            } else {
                kept.push_back(msg);
            }
        }
        self.buf = kept;
    }
}

// ---- tests ----------------------------------------------------------------

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

    fn user(text: &str) -> Value {
        json!({"role": "user", "content": text})
    }

    fn assistant(text: &str) -> Value {
        json!({"role": "assistant", "content": text})
    }

    fn tool_use_msg(id: &str, name: &str) -> Value {
        json!({
            "role": "assistant",
            "content": [{"type": "tool_use", "id": id, "name": name, "input": {}}]
        })
    }

    fn tool_result_msg(tool_use_id: &str) -> Value {
        json!({
            "role": "user",
            "content": [{"type": "tool_result", "tool_use_id": tool_use_id, "content": "ok"}]
        })
    }

    #[test]
    fn basic_sliding_window() {
        let mut win = MessageWindow::new(3, false);
        win.add(user("a")).unwrap();
        win.add(user("b")).unwrap();
        win.add(user("c")).unwrap();
        win.add(user("d")).unwrap();
        assert_eq!(win.len(), 3);
        assert_eq!(win.evicted_count(), 1);
        assert_eq!(win.messages()[0]["content"], json!("b"));
    }

    #[test]
    fn no_overflow_within_cap() {
        let mut win = MessageWindow::new(5, true);
        for i in 0..5 {
            win.add(user(&i.to_string())).unwrap();
        }
        assert_eq!(win.len(), 5);
        assert_eq!(win.evicted_count(), 0);
    }

    #[test]
    fn paired_protect_drops_orphan_tool_result() {
        let mut win = MessageWindow::new(2, true);
        win.add(tool_use_msg("u1", "search")).unwrap();
        win.add(tool_result_msg("u1")).unwrap();
        // Now add two more; the tool_use will be evicted, which cascades to result
        win.add(user("follow-up")).unwrap();
        win.add(assistant("done")).unwrap();
        // tool_use AND tool_result both gone; only last 2 remain
        assert_eq!(win.len(), 2);
        assert!(win.messages().iter().all(|m| {
            m.get("content")
                .and_then(|c| c.as_array())
                .map(|arr| arr.iter().all(|b| b.get("type") != Some(&json!("tool_result"))))
                .unwrap_or(true)
        }));
    }

    #[test]
    fn paired_protect_off_leaves_orphan() {
        let mut win = MessageWindow::new(2, false);
        win.add(tool_use_msg("u1", "search")).unwrap();
        win.add(tool_result_msg("u1")).unwrap();
        win.add(user("next")).unwrap();
        win.add(assistant("done")).unwrap();
        // Without protection, raw oldest is dropped; tool_result may still be present
        assert_eq!(win.len(), 2);
    }

    #[test]
    fn clear_resets_all() {
        let mut win = MessageWindow::new(3, true);
        win.add(user("a")).unwrap();
        win.add(user("b")).unwrap();
        win.clear();
        assert_eq!(win.len(), 0);
        assert_eq!(win.evicted_count(), 0);
    }

    #[test]
    fn extend_adds_in_order() {
        let mut win = MessageWindow::new(5, true);
        win.extend(vec![user("a"), user("b"), user("c")]).unwrap();
        assert_eq!(win.len(), 3);
        assert_eq!(win.messages()[1]["content"], json!("b"));
    }

    #[test]
    fn not_object_error() {
        let mut win = MessageWindow::new(3, true);
        let err = win.add(json!([1, 2, 3])).unwrap_err();
        assert_eq!(err, WindowError::NotAnObject);
    }

    #[test]
    fn try_new_zero_cap_errors() {
        assert!(matches!(
            MessageWindow::try_new(0, true),
            Err(WindowError::MaxTurnsMustBePositive)
        ));
    }

    #[test]
    fn try_new_valid() {
        assert!(MessageWindow::try_new(1, true).is_ok());
    }

    #[test]
    fn evicted_count_increments_correctly() {
        let mut win = MessageWindow::new(2, false);
        for i in 0..5u32 {
            win.add(user(&i.to_string())).unwrap();
        }
        assert_eq!(win.evicted_count(), 3);
    }

    #[test]
    fn multiple_tool_use_ids_in_one_message() {
        let mut win = MessageWindow::new(2, true);
        // one assistant message with two tool_use blocks
        win.add(json!({
            "role": "assistant",
            "content": [
                {"type": "tool_use", "id": "a1", "name": "x", "input": {}},
                {"type": "tool_use", "id": "a2", "name": "y", "input": {}}
            ]
        }))
        .unwrap();
        win.add(tool_result_msg("a1")).unwrap();
        win.add(tool_result_msg("a2")).unwrap();
        // window fills to 2, starts evicting
        win.add(user("next")).unwrap();
        // tool_use evicted → both results should be purged
        for m in win.messages() {
            let content = m.get("content");
            if let Some(Value::Array(blocks)) = content {
                for b in blocks {
                    assert_ne!(b.get("type"), Some(&json!("tool_result")));
                }
            }
        }
    }

    #[test]
    fn messages_owned_clones() {
        let mut win = MessageWindow::new(3, true);
        win.add(user("hello")).unwrap();
        let owned = win.messages_owned();
        assert_eq!(owned[0]["content"], json!("hello"));
    }

    #[test]
    fn single_capacity_window() {
        let mut win = MessageWindow::new(1, true);
        win.add(user("a")).unwrap();
        win.add(user("b")).unwrap();
        assert_eq!(win.len(), 1);
        assert_eq!(win.messages()[0]["content"], json!("b"));
        assert_eq!(win.evicted_count(), 1);
    }

    #[test]
    fn tool_use_no_result_in_buffer_just_evicts_use() {
        let mut win = MessageWindow::new(1, true);
        win.add(tool_use_msg("u1", "search")).unwrap();
        // no tool_result in buffer; evicting tool_use alone is fine
        win.add(user("follow")).unwrap();
        assert_eq!(win.len(), 1);
        assert_eq!(win.messages()[0]["content"], json!("follow"));
    }

    #[test]
    fn add_then_clear_then_add() {
        let mut win = MessageWindow::new(3, true);
        win.add(user("a")).unwrap();
        win.clear();
        win.add(user("b")).unwrap();
        assert_eq!(win.len(), 1);
        assert_eq!(win.messages()[0]["content"], json!("b"));
    }

    #[test]
    fn is_empty_and_len() {
        let mut win = MessageWindow::new(3, true);
        assert!(win.is_empty());
        win.add(user("x")).unwrap();
        assert!(!win.is_empty());
        assert_eq!(win.len(), 1);
    }
}