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
use std::{
    collections::{BTreeMap, BTreeSet},
    path::PathBuf,
    time::Duration,
};

#[cfg(test)]
use std::sync::atomic::{AtomicU64, Ordering};

use serde::{Deserialize, Serialize};

use crate::compaction::CompactionMode;
#[cfg(test)]
use crate::provider::ToolSearchMode;
use crate::provider::{ProviderRequestOptions, ToolChoice};

#[cfg(test)]
static NEXT_TEST_TRANSCRIPT_DIR_ID: AtomicU64 = AtomicU64::new(1);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskConfig {
    pub tasks_dir: PathBuf,
    pub reminder_threshold: usize,
}

impl Default for TaskConfig {
    fn default() -> Self {
        Self {
            tasks_dir: default_tasks_dir(),
            reminder_threshold: 3,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TeamAutonomyConfig {
    pub enabled: bool,
    pub poll_interval: Duration,
    pub idle_timeout: Duration,
}

impl Default for TeamAutonomyConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            poll_interval: Duration::from_secs(5),
            idle_timeout: Duration::from_secs(60),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TeamConfig {
    pub team_dir: PathBuf,
    pub autonomy: TeamAutonomyConfig,
}

impl Default for TeamConfig {
    fn default() -> Self {
        Self {
            team_dir: default_team_dir(),
            autonomy: TeamAutonomyConfig::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompactionConfig {
    pub keep_recent_tool_results: usize,
    pub auto_compact_threshold_tokens: Option<usize>,
    pub transcript_dir: PathBuf,
    pub summary_max_input_chars: usize,
    pub summary_max_output_tokens: u32,
    #[serde(default)]
    pub mode: CompactionMode,
    pub preserve_recent_user_tokens: usize,
    pub preserve_recent_delegation_results: usize,
    pub max_persisted_transcripts: Option<usize>,
}

impl Default for CompactionConfig {
    fn default() -> Self {
        Self {
            keep_recent_tool_results: 3,
            auto_compact_threshold_tokens: Some(50_000),
            transcript_dir: default_transcript_dir(),
            summary_max_input_chars: 80_000,
            summary_max_output_tokens: 2_000,
            mode: CompactionMode::LocalOnly,
            preserve_recent_user_tokens: 20_000,
            preserve_recent_delegation_results: 8,
            max_persisted_transcripts: Some(10),
        }
    }
}

pub type ContextCompactionConfig = CompactionConfig;

/// Bounds how much of an oversized tool result enters the model's view.
///
/// A result at or below `threshold_bytes` is inserted byte-identically to a
/// run without paging. Above it, the transcript receives the first window
/// (at most `page_bytes`, cut on a line boundary) plus a trailer naming the
/// `read_tool_result` call that returns the next window; the full result is
/// retained in memory for the life of the agent so nothing is lost.
///
/// Paging is applied *after* the runtime's own tool-result limiter
/// (`RuntimePolicy::with_max_tool_result_bytes` /
/// `with_max_tool_result_lines`), so a `threshold_bytes` above those caps
/// never triggers — the limiter clamps the result first. Enabling paging
/// therefore means raising the policy caps to whatever a tool may legitimately
/// return and leaving them as the anti-abuse backstop.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolResultPagingConfig {
    /// Results at or below this size are inserted whole. Default 64 KiB.
    pub threshold_bytes: usize,
    /// Maximum bytes per inserted page/window. Default 32 KiB.
    pub page_bytes: usize,
}

impl Default for ToolResultPagingConfig {
    fn default() -> Self {
        Self {
            threshold_bytes: 64 * 1024,
            page_bytes: 32 * 1024,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspaceConfig {
    pub base_dir: PathBuf,
    pub auto_route_shell: bool,
}

impl Default for WorkspaceConfig {
    fn default() -> Self {
        let base_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
        Self {
            base_dir,
            auto_route_shell: true,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MemoryConfig {
    pub auto_recall_enabled: bool,
    pub auto_recall_limit: usize,
    pub auto_recall_char_budget: usize,
    pub tool_search_limit: usize,
    pub write_tools_enabled: bool,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            auto_recall_enabled: true,
            auto_recall_limit: 3,
            auto_recall_char_budget: 2_000,
            tool_search_limit: 10,
            write_tools_enabled: true,
        }
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolProfile {
    #[serde(default)]
    pub allowed_tools: Option<BTreeSet<String>>,
    #[serde(default)]
    pub hidden_tools: BTreeSet<String>,
}

impl ToolProfile {
    pub fn all() -> Self {
        Self::default()
    }

    pub fn only<I, S>(tools: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            allowed_tools: Some(tools.into_iter().map(Into::into).collect()),
            hidden_tools: BTreeSet::new(),
        }
    }

    pub fn hide<I, S>(tools: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            allowed_tools: None,
            hidden_tools: tools.into_iter().map(Into::into).collect(),
        }
    }

    pub fn allows(&self, tool_name: &str) -> bool {
        if let Some(allowed_tools) = &self.allowed_tools {
            if !allowed_tools.contains(tool_name) {
                return false;
            }
        }

        !self.hidden_tools.contains(tool_name)
    }
}

#[cfg(not(test))]
fn default_team_dir() -> PathBuf {
    crate::default_paths::workspace_default_paths().team_dir
}

#[cfg(test)]
fn default_team_dir() -> PathBuf {
    let suffix = NEXT_TEST_TRANSCRIPT_DIR_ID.fetch_add(1, Ordering::Relaxed);
    std::env::temp_dir()
        .join("mentra-test-team")
        .join(format!("process-{}-{suffix}", std::process::id()))
}

#[cfg(not(test))]
fn default_transcript_dir() -> PathBuf {
    crate::default_paths::workspace_default_paths().transcripts_dir
}

#[cfg(not(test))]
fn default_tasks_dir() -> PathBuf {
    crate::default_paths::workspace_default_paths().tasks_dir
}

#[cfg(test)]
fn default_tasks_dir() -> PathBuf {
    let suffix = NEXT_TEST_TRANSCRIPT_DIR_ID.fetch_add(1, Ordering::Relaxed);
    std::env::temp_dir()
        .join("mentra-test-tasks")
        .join(format!("process-{}-{suffix}", std::process::id()))
}

#[cfg(test)]
fn default_transcript_dir() -> PathBuf {
    let suffix = NEXT_TEST_TRANSCRIPT_DIR_ID.fetch_add(1, Ordering::Relaxed);
    std::env::temp_dir()
        .join("mentra-test-transcripts")
        .join(format!("process-{}-{suffix}", std::process::id()))
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    pub system: Option<String>,
    pub tool_choice: Option<ToolChoice>,
    #[serde(default)]
    pub tool_profile: ToolProfile,
    pub temperature: Option<f32>,
    pub max_output_tokens: Option<u32>,
    pub metadata: BTreeMap<String, String>,
    #[serde(default)]
    pub provider_request_options: ProviderRequestOptions,
    pub team: TeamConfig,
    pub task: TaskConfig,
    pub workspace: WorkspaceConfig,
    #[serde(default)]
    pub memory: MemoryConfig,
    #[serde(alias = "context_compaction")]
    pub compaction: CompactionConfig,
    /// `None` (the default) preserves the unpaged behaviour exactly: every
    /// tool result enters the transcript as produced, and `read_tool_result`
    /// is absent from the agent's tool roster.
    #[serde(default)]
    pub tool_result_paging: Option<ToolResultPagingConfig>,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            system: None,
            tool_choice: Some(ToolChoice::default()),
            tool_profile: ToolProfile::default(),
            temperature: None,
            max_output_tokens: Some(8192),
            metadata: BTreeMap::new(),
            provider_request_options: ProviderRequestOptions::default(),
            team: TeamConfig::default(),
            task: TaskConfig::default(),
            workspace: WorkspaceConfig::default(),
            memory: MemoryConfig::default(),
            compaction: CompactionConfig::default(),
            tool_result_paging: None,
        }
    }
}

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

    use crate::provider::{ReasoningEffort, ReasoningOptions};

    fn test_path(label: &str) -> PathBuf {
        std::env::temp_dir()
            .join("mentra-agent-config-tests")
            .join(label)
    }

    #[test]
    fn explicit_paths_override_defaults() {
        let tasks_dir = test_path("custom-tasks");
        let team_dir = test_path("custom-team");
        let transcript_dir = test_path("custom-transcripts");

        let config = AgentConfig {
            task: TaskConfig {
                tasks_dir: tasks_dir.clone(),
                ..Default::default()
            },
            team: TeamConfig {
                team_dir: team_dir.clone(),
                ..Default::default()
            },
            compaction: ContextCompactionConfig {
                transcript_dir: transcript_dir.clone(),
                ..Default::default()
            },
            ..Default::default()
        };

        assert_eq!(config.task.tasks_dir, tasks_dir);
        assert_eq!(config.team.team_dir, team_dir);
        assert_eq!(config.compaction.transcript_dir, transcript_dir);
    }

    #[test]
    fn tool_profile_defaults_to_allowing_everything() {
        let profile = ToolProfile::default();

        assert!(profile.allows("shell"));
        assert!(profile.allows("files"));
    }

    #[test]
    fn tool_profile_only_restricts_to_allowlist() {
        let profile = ToolProfile::only(["shell", "files"]);

        assert!(profile.allows("shell"));
        assert!(profile.allows("files"));
        assert!(!profile.allows("task"));
    }

    #[test]
    fn tool_profile_hide_blocks_named_tools() {
        let profile = ToolProfile::hide(["shell", "background_run"]);

        assert!(!profile.allows("shell"));
        assert!(!profile.allows("background_run"));
        assert!(profile.allows("files"));
    }

    #[test]
    fn tool_profile_respects_allowlist_and_hidden_overrides() {
        let profile = ToolProfile {
            allowed_tools: Some(["shell", "files"].into_iter().map(str::to_string).collect()),
            hidden_tools: ["shell"].into_iter().map(str::to_string).collect(),
        };

        assert!(!profile.allows("shell"));
        assert!(profile.allows("files"));
        assert!(!profile.allows("task"));
    }

    #[test]
    fn agent_config_deserializes_without_tool_profile_field() {
        let config: AgentConfig = serde_json::from_value(json!({
            "system": null,
            "tool_choice": serde_json::to_value(ToolChoice::Auto).expect("serialize tool choice"),
            "temperature": null,
            "max_output_tokens": 8192,
            "metadata": {},
            "provider_request_options": {},
            "team": TeamConfig::default(),
            "task": TaskConfig::default(),
            "workspace": WorkspaceConfig::default(),
            "memory": MemoryConfig::default(),
            "context_compaction": ContextCompactionConfig::default()
        }))
        .expect("deserialize config without tool profile");

        assert_eq!(config.tool_profile, ToolProfile::default());
    }

    #[test]
    fn provider_request_options_default_to_disabled_tool_search() {
        let options = ProviderRequestOptions::default();

        assert_eq!(options.tool_search_mode, ToolSearchMode::Disabled);
        assert_eq!(options.reasoning, None);
    }

    #[test]
    fn agent_config_deserializes_without_tool_search_mode() {
        let config: AgentConfig = serde_json::from_value(json!({
            "system": null,
            "tool_choice": serde_json::to_value(ToolChoice::Auto).expect("serialize tool choice"),
            "temperature": null,
            "max_output_tokens": 8192,
            "metadata": {},
            "provider_request_options": {
                "responses": {
                    "parallel_tool_calls": true
                }
            },
            "team": TeamConfig::default(),
            "task": TaskConfig::default(),
            "workspace": WorkspaceConfig::default(),
            "memory": MemoryConfig::default(),
            "context_compaction": ContextCompactionConfig::default()
        }))
        .expect("deserialize config without tool search mode");

        assert_eq!(
            config.provider_request_options.tool_search_mode,
            ToolSearchMode::Disabled
        );
        assert_eq!(
            config
                .provider_request_options
                .responses
                .parallel_tool_calls,
            Some(true)
        );
    }

    #[test]
    fn tool_result_paging_is_disabled_by_default() {
        assert_eq!(AgentConfig::default().tool_result_paging, None);
    }

    #[test]
    fn tool_result_paging_defaults_to_64_kib_threshold_and_32_kib_pages() {
        let paging = ToolResultPagingConfig::default();

        assert_eq!(paging.threshold_bytes, 64 * 1024);
        assert_eq!(paging.page_bytes, 32 * 1024);
    }

    #[test]
    fn agent_config_deserializes_without_tool_result_paging_field() {
        let config: AgentConfig = serde_json::from_value(json!({
            "system": null,
            "tool_choice": serde_json::to_value(ToolChoice::Auto).expect("serialize tool choice"),
            "temperature": null,
            "max_output_tokens": 8192,
            "metadata": {},
            "provider_request_options": {},
            "team": TeamConfig::default(),
            "task": TaskConfig::default(),
            "workspace": WorkspaceConfig::default(),
            "memory": MemoryConfig::default(),
            "context_compaction": ContextCompactionConfig::default()
        }))
        .expect("deserialize config persisted before paging existed");

        assert_eq!(config.tool_result_paging, None);
    }

    #[test]
    fn agent_config_round_trips_tool_result_paging() {
        let config = AgentConfig {
            tool_result_paging: Some(ToolResultPagingConfig {
                threshold_bytes: 4_096,
                page_bytes: 1_024,
            }),
            ..Default::default()
        };

        let restored: AgentConfig =
            serde_json::from_value(serde_json::to_value(&config).expect("serialize config"))
                .expect("deserialize config");

        assert_eq!(restored.tool_result_paging, config.tool_result_paging);
    }

    #[test]
    fn agent_config_deserializes_reasoning_options() {
        let config: AgentConfig = serde_json::from_value(json!({
            "system": null,
            "tool_choice": serde_json::to_value(ToolChoice::Auto).expect("serialize tool choice"),
            "temperature": null,
            "max_output_tokens": 8192,
            "metadata": {},
            "provider_request_options": {
                "reasoning": {
                    "effort": "high"
                }
            },
            "team": TeamConfig::default(),
            "task": TaskConfig::default(),
            "workspace": WorkspaceConfig::default(),
            "memory": MemoryConfig::default(),
            "context_compaction": ContextCompactionConfig::default()
        }))
        .expect("deserialize config with reasoning options");

        assert_eq!(
            config.provider_request_options.reasoning,
            Some(ReasoningOptions {
                effort: Some(ReasoningEffort::High),
                summary: None,
            })
        );
    }
}