mentra 0.14.0

An agent runtime for tool-using LLM applications
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
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
//! End-to-end coverage for automatic tool-result paging: what the model sees,
//! what the event stream keeps, and how `read_tool_result` walks a retained
//! result window by window.

use async_trait::async_trait;
use serde_json::{Value, json};

use crate::{
    AgentConfig, BuiltinProvider, ContentBlock, Message, Role, ToolResultPagingConfig,
    agent::AgentEvent,
    provider::{ContentBlockDelta, ContentBlockStart, ProviderEvent},
    runtime::{Runtime, RuntimePolicy},
    tool::{
        ParallelToolContext, ToolDefinition, ToolDurability, ToolExecutionCategory, ToolExecutor,
        ToolResult, ToolSideEffectLevel, ToolSpec,
    },
};

use super::support::{ScriptedProvider, StaticTool, StreamScript, model_info, ok_stream};

/// Builds `count` lines of exactly 20 bytes each (`{tag}-{n:03}` padded), so
/// every window boundary asserted below is exact arithmetic on line counts
/// rather than an approximation.
fn numbered_lines(tag: &str, count: usize) -> String {
    assert_eq!(
        tag.len(),
        4,
        "the fixed 20-byte line layout assumes a 4-byte tag"
    );
    (1..=count)
        .map(|line| format!("{tag}-{line:03}{}\n", "x".repeat(11)))
        .collect()
}

/// Removes the runtime's own tool-result caps from the picture. Paging runs
/// downstream of that limiter, so a threshold above `max_tool_result_bytes`
/// would never be reached — every paging test has to raise the caps first,
/// exactly as a real consumer enabling paging must.
fn unlimited_results() -> RuntimePolicy {
    RuntimePolicy::default()
        .with_max_tool_result_bytes(usize::MAX)
        .with_max_tool_result_lines(usize::MAX)
        .spill_full_tool_output(false)
}

fn paged_config(threshold_bytes: usize, page_bytes: usize) -> AgentConfig {
    AgentConfig {
        tool_result_paging: Some(ToolResultPagingConfig {
            threshold_bytes,
            page_bytes,
        }),
        ..Default::default()
    }
}

fn tool_results(messages: &[Message]) -> Vec<(String, String, bool)> {
    messages
        .iter()
        .filter(|message| message.role == Role::User)
        .flat_map(|message| message.content.iter())
        .filter_map(|block| match block {
            ContentBlock::ToolResult {
                tool_use_id,
                content,
                is_error,
            } => Some((tool_use_id.clone(), content.to_display_string(), *is_error)),
            _ => None,
        })
        .collect()
}

fn multi_tool_use_stream(model: &str, calls: &[(&str, &str, &str)]) -> StreamScript {
    let mut events = vec![ProviderEvent::MessageStarted {
        id: "msg-multi-tool".to_string(),
        model: model.to_string(),
        role: Role::Assistant,
    }];
    for (index, (id, name, input_json)) in calls.iter().enumerate() {
        events.push(ProviderEvent::ContentBlockStarted {
            index,
            kind: ContentBlockStart::ToolUse {
                id: (*id).to_string(),
                name: (*name).to_string(),
            },
        });
        events.push(ProviderEvent::ContentBlockDelta {
            index,
            delta: ContentBlockDelta::ToolUseInputJson((*input_json).to_string()),
        });
        events.push(ProviderEvent::ContentBlockStopped { index });
    }
    events.push(ProviderEvent::MessageStopped);
    ok_stream(events)
}

fn read_window_input(tool_use_id: &str, start_line: usize) -> String {
    json!({ "tool_use_id": tool_use_id, "start_line": start_line }).to_string()
}

/// A parallel-lane tool returning a caller-supplied oversized result.
struct ParallelPagedTool {
    name: &'static str,
    output: String,
}

impl ToolDefinition for ParallelPagedTool {
    fn descriptor(&self) -> ToolSpec {
        ToolSpec::builder(self.name)
            .description("test tool: returns an oversized parallel result")
            .input_schema(json!({ "type": "object", "properties": {} }))
            .side_effect_level(ToolSideEffectLevel::None)
            .durability(ToolDurability::ReplaySafe)
            .execution_category(ToolExecutionCategory::ReadOnlyParallel)
            .build()
    }
}

#[async_trait]
impl ToolExecutor for ParallelPagedTool {
    async fn execute(&self, _ctx: ParallelToolContext, _input: Value) -> ToolResult {
        Ok(self.output.clone())
    }
}

// (a) With paging unconfigured, an oversized result is inserted whole and the
// reader is neither registered nor offered to the model.
#[tokio::test]
async fn unpaged_agents_receive_oversized_results_whole_without_the_reader() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let full = numbered_lines("line", 40);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    agent
        .send(vec![ContentBlock::text("run the big tool")])
        .await
        .expect("send");

    let results = tool_results(agent.history());
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].1, full, "the result must be byte-identical");
    assert!(
        agent
            .tools()
            .iter()
            .all(|tool| tool.name != "read_tool_result"),
        "read_tool_result must not be offered to an unpaged agent"
    );
    assert!(
        runtime.tool_descriptor("read_tool_result").is_none(),
        "read_tool_result must not be registered for an unpaged agent"
    );
}

// (e) With paging enabled, a result at or below the threshold is still
// byte-identical — only the roster changes.
#[tokio::test]
async fn sub_threshold_results_stay_byte_identical_under_paging() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let full = numbered_lines("line", 40);
    assert_eq!(full.len(), 800);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime
        .spawn_with_config("agent", model, paged_config(800, 100))
        .expect("spawn agent");

    agent
        .send(vec![ContentBlock::text("run the big tool")])
        .await
        .expect("send");

    let results = tool_results(agent.history());
    assert_eq!(results[0].1, full, "a result at the threshold is not paged");
    assert!(!results[0].1.contains("[paged:"));
    assert!(
        agent
            .tools()
            .iter()
            .any(|tool| tool.name == "read_tool_result"),
        "the reader is offered whenever paging is enabled, not only once it fires"
    );
}

// (b) An oversized result reaches the model as page 1 plus a trailer, while
// the event stream still carries the complete block.
#[tokio::test]
async fn oversized_results_reach_the_model_paged_and_the_event_stream_whole() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let full = numbered_lines("line", 40);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime
        .spawn_with_config("agent", model, paged_config(100, 100))
        .expect("spawn agent");
    let mut events = agent.subscribe_events();

    agent
        .send(vec![ContentBlock::text("run the big tool")])
        .await
        .expect("send");

    let results = tool_results(agent.history());
    assert_eq!(results.len(), 1);
    let page = &results[0].1;
    assert!(page.starts_with(&numbered_lines("line", 5)));
    assert!(!page.contains("line-006"));
    assert!(
        page.contains(
            "…[paged: lines 1–5 of 40 (0.1 KB of 0.8 KB). \
             Call read_tool_result(tool_use_id=\"call-1\", start_line=6) for the next window.]"
        ),
        "unexpected trailer: {page}"
    );

    let finished = collect_events(&mut events)
        .into_iter()
        .find_map(|event| match event {
            AgentEvent::ToolExecutionFinished { result } => Some(result),
            _ => None,
        })
        .expect("a ToolExecutionFinished event");
    let ContentBlock::ToolResult { content, .. } = finished else {
        panic!("expected a tool result block");
    };
    assert_eq!(
        content.to_display_string(),
        full,
        "the event stream must keep carrying the unpaged result"
    );
}

// (c) Successive windows tile the result with absolute line numbers, the last
// one is marked as the end, and a start_line past the end is empty.
#[tokio::test]
async fn read_tool_result_windows_tile_the_result_and_mark_the_end() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let full = numbered_lines("line", 40);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::tool_use_stream(
                &model.id,
                "call-2",
                "read_tool_result",
                &read_window_input("call-1", 6),
            ),
            super::support::tool_use_stream(
                &model.id,
                "call-3",
                "read_tool_result",
                &read_window_input("call-1", 36),
            ),
            super::support::tool_use_stream(
                &model.id,
                "call-4",
                "read_tool_result",
                &read_window_input("call-1", 41),
            ),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime
        .spawn_with_config("agent", model, paged_config(100, 100))
        .expect("spawn agent");

    let message = agent
        .send(vec![ContentBlock::text("read the whole result")])
        .await
        .expect("send");
    assert_eq!(message.text(), "done");

    let results = tool_results(agent.history());
    assert_eq!(results.len(), 4);

    assert!(results[1].1.starts_with("line-006"));
    assert!(results[1].1.contains("lines 6–10 of 40"));
    assert!(results[1].1.contains("start_line=11"));

    assert!(results[2].1.starts_with("line-036"));
    assert!(results[2].1.contains("line-040"));
    assert!(
        results[2].1.ends_with("…[end of result]"),
        "the window reaching the last line ends the result: {}",
        results[2].1
    );
    assert!(!results[2].1.contains("[paged:"));

    assert_eq!(
        results[3].1, "…[end of result]",
        "a start_line past the end is an empty window, not an error"
    );
    assert!(!results[3].2, "reading past the end is not a tool error");
}

// The reader's own windows are never paged again, even when `page_bytes`
// exceeds `threshold_bytes` and a window is therefore itself "oversized".
#[tokio::test]
async fn read_tool_result_windows_are_never_paged_recursively() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let full = numbered_lines("line", 40);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::tool_use_stream(
                &model.id,
                "call-2",
                "read_tool_result",
                &read_window_input("call-1", 16),
            ),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime
        .spawn_with_config("agent", model, paged_config(100, 300))
        .expect("spawn agent");

    agent
        .send(vec![ContentBlock::text("read a large window")])
        .await
        .expect("send");

    let results = tool_results(agent.history());
    let window = &results[1].1;
    assert!(window.starts_with("line-016"));
    assert!(window.contains("lines 16–30 of 40"));
    assert_eq!(
        window.matches("[paged:").count(),
        1,
        "a window must carry exactly one trailer, never a trailer nested in a page: {window}"
    );
    assert!(
        !window.contains("call-2"),
        "a window must never be re-paged under its own tool_use_id: {window}"
    );
}

// (d) An unknown tool_use_id is an ordinary tool error and the run continues.
#[tokio::test]
async fn an_unknown_tool_use_id_is_a_tool_error_and_the_run_continues() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let full = numbered_lines("line", 40);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::tool_use_stream(
                &model.id,
                "call-2",
                "read_tool_result",
                &read_window_input("call-does-not-exist", 1),
            ),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime
        .spawn_with_config("agent", model, paged_config(100, 100))
        .expect("spawn agent");

    let message = agent
        .send(vec![ContentBlock::text(
            "read a result that was never paged",
        )])
        .await
        .expect("an unknown id must not fail the run");

    assert_eq!(message.text(), "done");
    let results = tool_results(agent.history());
    assert!(results[1].2, "the failed read is an is_error result");
    assert!(results[1].1.contains("no retained result for tool_use_id"));
    assert!(results[1].1.contains("call-does-not-exist"));
}

// (f) A single line longer than a page is the one case that cuts mid-line:
// it cuts on a character boundary and says so.
#[tokio::test]
async fn a_line_longer_than_a_page_hard_cuts_on_a_character_boundary() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    // 50 four-byte characters: a 200-byte line, plus a short second line.
    let full = format!("{}\ntail line\n", "𝄞".repeat(50));
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            super::support::tool_use_stream(&model.id, "call-1", "big_tool", r#"{}"#),
            super::support::tool_use_stream(
                &model.id,
                "call-2",
                "read_tool_result",
                &read_window_input("call-1", 2),
            ),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(StaticTool::success("big_tool", &full))
        .build()
        .expect("build runtime");
    let mut agent = runtime
        // 102 is not a multiple of the 4-byte character width, so a correct
        // cut must round down to 100.
        .spawn_with_config("agent", model, paged_config(100, 102))
        .expect("spawn agent");

    agent
        .send(vec![ContentBlock::text("run the long-line tool")])
        .await
        .expect("send");

    let results = tool_results(agent.history());
    let page = &results[0].1;
    assert!(page.starts_with(&"𝄞".repeat(25)));
    assert!(!page.starts_with(&"𝄞".repeat(26)));
    assert!(
        page.contains("…[line 1 hard-cut at 100 of 201 bytes"),
        "unexpected hard-cut marker: {page}"
    );
    assert!(page.contains("start_line=2"));

    assert!(
        results[1].1.starts_with("tail line\n"),
        "the next window resumes at the following whole line: {}",
        results[1].1
    );
    assert!(results[1].1.ends_with("…[end of result]"));
}

// (g) Parallel oversized results page independently, and each one's full text
// is retained under its own tool_use_id.
#[tokio::test]
async fn parallel_oversized_results_page_independently_per_tool_use_id() {
    let model = model_info("model", BuiltinProvider::Anthropic);
    let first = numbered_lines("aaaa", 40);
    let second = numbered_lines("bbbb", 40);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            multi_tool_use_stream(
                &model.id,
                &[
                    ("call-a", "parallel_a", r#"{}"#),
                    ("call-b", "parallel_b", r#"{}"#),
                ],
            ),
            super::support::tool_use_stream(
                &model.id,
                "call-c",
                "read_tool_result",
                &read_window_input("call-b", 6),
            ),
            super::support::text_stream(&model.id, "done"),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_policy(unlimited_results())
        .with_tool(ParallelPagedTool {
            name: "parallel_a",
            output: first,
        })
        .with_tool(ParallelPagedTool {
            name: "parallel_b",
            output: second,
        })
        .build()
        .expect("build runtime");
    let mut agent = runtime
        .spawn_with_config("agent", model, paged_config(100, 100))
        .expect("spawn agent");

    agent
        .send(vec![ContentBlock::text("run both parallel tools")])
        .await
        .expect("send");

    let results = tool_results(agent.history());
    assert_eq!(results.len(), 3);

    assert_eq!(results[0].0, "call-a");
    assert!(results[0].1.starts_with("aaaa-001"));
    assert!(results[0].1.contains("tool_use_id=\"call-a\""));
    assert!(!results[0].1.contains("bbbb"));

    assert_eq!(results[1].0, "call-b");
    assert!(results[1].1.starts_with("bbbb-001"));
    assert!(results[1].1.contains("tool_use_id=\"call-b\""));
    assert!(!results[1].1.contains("aaaa"));

    assert!(
        results[2].1.starts_with("bbbb-006"),
        "each result is retained under its own id: {}",
        results[2].1
    );
    assert!(results[2].1.contains("lines 6–10 of 40"));
}

fn collect_events(receiver: &mut tokio::sync::broadcast::Receiver<AgentEvent>) -> Vec<AgentEvent> {
    let mut events = Vec::new();
    while let Ok(event) = receiver.try_recv() {
        events.push(event);
    }
    events
}