ailoy 0.3.3

Agents that drive a language model through tool-augmented turns, in a virtx console.
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
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
use serde::{Deserialize, Serialize};

use crate::message::{Message, Part, Role};

#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(default)]
pub struct ContextManager {
    /// Triggers truncation, as a turn begins, when input_tokens from the previous API call
    /// exceeds this value. `0`, the default, turns truncation off.
    pub max_input_tokens: u64,
    /// Number of recent user turns to preserve after truncation (system message is always
    /// preserved separately). The turn just begun is one of them, so `0` keeps it too.
    pub preserve_recent_turns: usize,
    /// Images in tool results the history may hold at the start of a turn; over it, the
    /// oldest become a placeholder. `0` turns the cap off.
    pub max_images: usize,
    /// Images in tool results left once the cap is passed.
    pub preserve_recent_images: usize,
}

impl Default for ContextManager {
    fn default() -> Self {
        Self {
            max_input_tokens: 0,
            preserve_recent_turns: 3,
            max_images: 30,
            preserve_recent_images: 10,
        }
    }
}

/// What stands in for an image [`cap_images`](ContextManager::cap_images) replaced.
pub(super) const IMAGE_PLACEHOLDER: &str = "[image from an earlier turn removed]";

impl ContextManager {
    /// Truncate the conversation history to reduce context size.
    ///
    /// A leading system message is always kept, as is everything from the
    /// [preserve boundary](find_preserve_boundary) on. Each earlier `Role::Tool` message
    /// has its contents replaced by `"[context truncated]"` but keeps its `id`: Anthropic
    /// returns HTTP 400 for a tool-use id with no matching tool result.
    ///
    /// Whole turns are never dropped; that would need a post-truncation token estimate.
    pub(super) fn truncate_history(&self, history: &mut [Message]) {
        if history.is_empty() {
            return;
        }

        // ── Locate preserve boundary ───────────────────────────────────────────────
        let preserve_from = find_preserve_boundary(history, self.preserve_recent_turns);

        let start_idx = if history[0].role == Role::System {
            1
        } else {
            0
        };

        // `preserve_from == 0` truncates nothing; otherwise the oldest preserved User turn
        // is at index >= 1, so start_idx <= preserve_from.
        debug_assert!(
            preserve_from == 0 || start_idx <= preserve_from,
            "start_idx ({start_idx}) > preserve_from ({preserve_from}): \
             truncation window would overlap the system message"
        );

        // ── Replace Tool messages outside the preserve window with placeholders ────
        for msg in history.iter_mut().take(preserve_from).skip(start_idx) {
            if msg.role == Role::Tool {
                let original_id = msg.id.clone();
                let placeholder =
                    Message::new(Role::Tool).with_contents([Part::text("[context truncated]")]);
                *msg = if let Some(id) = original_id {
                    placeholder.with_id(id)
                } else {
                    placeholder
                };
            }
        }
    }

    /// Over [`max_images`](Self::max_images) images in the tool results of `history`, replace
    /// the oldest by [`IMAGE_PLACEHOLDER`] until
    /// [`preserve_recent_images`](Self::preserve_recent_images) remain: many at once, so the prompt
    /// cache breaks once in several turns rather than every turn. Images a person attached are
    /// left alone.
    ///
    /// Only for the start of a turn: Claude rejects a history altered before a thinking block
    /// it is handed back, and at turn start none follows, since `marshal_messages` (Claude)
    /// and `conversation` (Bedrock) replay thinking only after the last user message.
    pub(super) fn cap_images(&self, history: &mut [Message]) {
        if self.max_images == 0 {
            return;
        }
        let images: Vec<(usize, usize)> = history
            .iter()
            .enumerate()
            .filter(|(_, m)| m.role == Role::Tool)
            .flat_map(|(i, m)| {
                let parts = m.contents.iter().enumerate();
                parts
                    .filter(|(_, p)| p.is_image())
                    .map(move |(j, _)| (i, j))
            })
            .collect();
        if images.len() <= self.max_images {
            return;
        }
        let replaced = images.len().saturating_sub(self.preserve_recent_images);
        for (i, j) in images.into_iter().take(replaced) {
            history[i].contents[j] = Part::text(IMAGE_PLACEHOLDER);
        }
    }
}

/// Find the index from which messages should be preserved.
///
/// Returns the index of the `preserve_recent_turns`-th `User` message from the end, or
/// `0` (preserve everything) when there are fewer turns.
///
/// Counts `User` rather than `Assistant` messages because one user input may produce
/// several assistant messages (`asst(tool_call) → tool → asst(text)`).
fn find_preserve_boundary(history: &[Message], preserve_recent_turns: usize) -> usize {
    if preserve_recent_turns == 0 {
        return history.len();
    }

    let mut turns_found = 0usize;
    let mut i = history.len();

    while i > 0 {
        i -= 1;
        if history[i].role == Role::System {
            continue;
        }
        if history[i].role == Role::User {
            turns_found += 1;
            if turns_found >= preserve_recent_turns {
                return i;
            }
        }
    }

    // Fewer turns than requested — preserve everything.
    0
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        datatype::Value,
        message::{Message, Part, Role},
    };

    fn sys() -> Message {
        Message::new(Role::System).with_contents([Part::text("system")])
    }

    fn user(text: &str) -> Message {
        Message::new(Role::User).with_contents([Part::text(text)])
    }

    fn asst(text: &str) -> Message {
        Message::new(Role::Assistant).with_contents([Part::text(text)])
    }

    fn tool_result(id: &str) -> Message {
        Message::new(Role::Tool)
            .with_id(id)
            .with_contents([Part::value(Value::string("ok".to_string()))])
    }

    fn tool_call_asst(call_id: &str, tool_name: &str) -> Message {
        Message::new(Role::Assistant).with_tool_calls([Part::function(
            call_id,
            tool_name,
            Value::null(),
        )])
    }

    fn image() -> Part {
        Part::image_embedded("image/png", vec![0u8; 4].into()).unwrap()
    }

    fn shot(id: &str) -> Message {
        Message::new(Role::Tool)
            .with_id(id)
            .with_contents([Part::text("the page"), image()])
    }

    fn user_with_image(text: &str) -> Message {
        Message::new(Role::User).with_contents([Part::text(text), image()])
    }

    fn images_in(history: &[Message]) -> usize {
        history
            .iter()
            .flat_map(|m| &m.contents)
            .filter(|p| p.is_image())
            .count()
    }

    fn with_images(max_images: usize, preserve_recent_images: usize) -> ContextManager {
        ContextManager {
            max_images,
            preserve_recent_images,
            ..Default::default()
        }
    }

    #[test]
    fn test_preserve_recent_turns_boundary() {
        // history: sys, u1, a1, u2, a2, u3, a3
        // preserve_recent_turns = 2 → preserve from u2 (index 3) onwards
        let history = vec![
            sys(),
            user("u1"),
            asst("a1"),
            user("u2"),
            asst("a2"),
            user("u3"),
            asst("a3"),
        ];
        let boundary = find_preserve_boundary(&history, 2);
        assert_eq!(boundary, 3, "preserve boundary should be at u2 (index 3)");
    }

    #[test]
    fn test_no_change_when_all_within_preserve_window() {
        let mut history = vec![sys(), user("u1"), asst("a1"), user("u2"), asst("a2")];
        let cm = ContextManager {
            max_input_tokens: 30_000,
            preserve_recent_turns: 10,
            ..Default::default()
        };
        let original_len = history.len();
        cm.truncate_history(&mut history);
        assert_eq!(history.len(), original_len, "nothing should change");
    }

    #[test]
    fn test_tool_placeholder_replacement() {
        // history: sys, u1, tool_call_asst(call_1), tool_result(call_1), u2, a2
        // preserve_recent_turns = 1 → preserve (u2, a2) from index 4 onwards.
        // tool_result at index 3 is outside the preserve window → becomes placeholder.
        let mut history = vec![
            sys(),
            user("u1"),
            tool_call_asst("call_1", "my_tool"),
            tool_result("call_1"),
            user("u2"),
            asst("a2"),
        ];
        let cm = ContextManager {
            max_input_tokens: 30_000,
            preserve_recent_turns: 1,
            ..Default::default()
        };
        cm.truncate_history(&mut history);

        // The Tool message must still be present (as a placeholder, not removed).
        let tool_msg = history.iter().find(|m| m.role == Role::Tool);
        assert!(
            tool_msg.is_some(),
            "Tool message must still exist as placeholder"
        );
        let tool_msg = tool_msg.unwrap();
        assert_eq!(
            tool_msg.id.as_deref(),
            Some("call_1"),
            "tool_use_id must be preserved to avoid Anthropic 400 errors"
        );
        let content = tool_msg
            .contents
            .first()
            .expect("placeholder must have content");
        let val = content
            .as_text()
            .expect("placeholder content must be a Value part");
        assert_eq!(
            val, "[context truncated]",
            "placeholder content must be '[context truncated]'"
        );
    }

    #[test]
    fn test_system_message_never_dropped() {
        let mut history = vec![
            sys(),
            user("u1"),
            asst("a1"),
            user("u2"),
            asst("a2"),
            user("u3"),
            asst("a3"),
        ];
        let cm = ContextManager {
            max_input_tokens: 30_000,
            preserve_recent_turns: 1,
            ..Default::default()
        };
        cm.truncate_history(&mut history);
        assert_eq!(
            history[0].role,
            Role::System,
            "System message must always remain at index 0"
        );
        assert_eq!(history.len(), 7, "no messages should be dropped");
    }

    #[test]
    fn test_preserve_counts_user_messages_not_assistant() {
        // u2's turn spans asst(tool_call) → tool → asst("a2"); counting assistants would put
        // the boundary inside it.
        // history: sys(0), u1(1), asst("a1")(2), u2(3), asst(tool_call)(4), tool(5), asst("a2")(6)
        let history = vec![
            sys(),
            user("u1"),
            asst("a1"),
            user("u2"),
            tool_call_asst("call_1", "my_tool"),
            tool_result("call_1"),
            asst("a2"),
        ];
        let boundary = find_preserve_boundary(&history, 2);
        assert_eq!(
            boundary, 1,
            "preserve_recent_turns=2 must keep 2 user turns, landing at u1 (index 1)"
        );
    }

    #[test]
    fn test_preserved_messages_untouched() {
        let mut history = vec![
            sys(),
            user("u1"),
            asst("a1"),
            user("u2"),
            tool_call_asst("call_2", "tool_b"),
            tool_result("call_2"),
            asst("a2"),
        ];
        // preserve_recent_turns = 1 → boundary is at u2 (index 3).
        // tool_result("call_2") is at index 5 which is >= 3 → preserved.
        let cm = ContextManager {
            max_input_tokens: 30_000,
            preserve_recent_turns: 1,
            ..Default::default()
        };
        cm.truncate_history(&mut history);
        let tool_msg = history
            .iter()
            .find(|m| m.role == Role::Tool)
            .expect("tool result must still be present");
        let val = tool_msg
            .contents
            .first()
            .and_then(|p| p.as_value())
            .expect("tool result content must be a Value part");
        assert_eq!(
            val.as_str(),
            Some("ok"),
            "tool result inside preserve window must not be replaced"
        );
    }

    #[test]
    fn test_over_the_cap_the_oldest_screenshots_go_until_the_preserved_remain() {
        // Five screenshots over a cap of four: the last two stay.
        let mut history = vec![sys(), user("u1")];
        history.extend((1..=5).map(|n| shot(&format!("c{n}"))));
        history.push(asst("a1"));
        with_images(4, 2).cap_images(&mut history);

        assert_eq!(history[2].contents[1].as_text(), Some(IMAGE_PLACEHOLDER));
        assert!(history[2..=4].iter().all(|m| m.contents[1].is_text()));
        assert!(history[5..=6].iter().all(|m| m.contents[1].is_image()));
        // The note beside each screenshot stays.
        assert_eq!(history[2].contents[0].as_text(), Some("the page"));
        assert_eq!(history[2].id.as_deref(), Some("c1"));
    }

    #[test]
    fn test_at_the_cap_nothing_goes() {
        let mut history = vec![sys(), user("u1")];
        history.extend((1..=4).map(|n| shot(&format!("c{n}"))));
        with_images(4, 2).cap_images(&mut history);
        assert_eq!(images_in(&history), 4);
    }

    #[test]
    fn test_a_persons_images_are_neither_counted_nor_replaced() {
        // Three of a person's images and two screenshots are under a cap of two.
        let mut history = vec![
            sys(),
            user_with_image("u1"),
            user_with_image("u2"),
            shot("c1"),
            user_with_image("u3"),
            shot("c2"),
            asst("a1"),
        ];
        with_images(2, 1).cap_images(&mut history);
        assert_eq!(images_in(&history), 5);

        // A third screenshot passes it: the two oldest go, the person's three stay.
        history.push(shot("c3"));
        with_images(2, 1).cap_images(&mut history);
        assert_eq!(images_in(&history), 4);
        assert!(history[3].contents[1].is_text());
        assert!(history[5].contents[1].is_text());
        assert!(history[7].contents[1].is_image());
        assert!(
            history
                .iter()
                .filter(|m| m.role == Role::User)
                .all(|m| m.contents[1].is_image())
        );
    }

    #[test]
    fn test_a_cap_of_zero_is_off_and_keeping_none_clears_them() {
        let mut history = vec![sys(), user("u1")];
        history.extend((1..=12).map(|n| shot(&format!("c{n}"))));

        with_images(0, 0).cap_images(&mut history);
        assert_eq!(images_in(&history), 12);

        with_images(3, 0).cap_images(&mut history);
        assert_eq!(images_in(&history), 0);
    }
}