tirea-agentos 0.5.0

Agent runtime with streaming LLM integration, sub-agent orchestration, and context window management
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
use super::sorted_registry_ids;
use super::traits::{ToolRegistry, ToolRegistryError};
use crate::contracts::runtime::tool_call::Tool;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

#[derive(Clone, Default)]
pub struct InMemoryToolRegistry {
    tools: HashMap<String, Arc<dyn Tool>>,
}

impl std::fmt::Debug for InMemoryToolRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InMemoryToolRegistry")
            .field("len", &self.tools.len())
            .finish()
    }
}

impl InMemoryToolRegistry {
    pub fn new() -> Self {
        Self::default()
    }

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

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

    pub fn get(&self, id: &str) -> Option<Arc<dyn Tool>> {
        self.tools.get(id).cloned()
    }

    pub fn ids(&self) -> impl Iterator<Item = &String> {
        self.tools.keys()
    }

    pub fn register(&mut self, tool: Arc<dyn Tool>) -> Result<(), ToolRegistryError> {
        let id = tool.descriptor().id;
        if self.tools.contains_key(&id) {
            return Err(ToolRegistryError::ToolIdConflict(id));
        }
        self.tools.insert(id, tool);
        Ok(())
    }

    pub fn register_named(
        &mut self,
        id: impl Into<String>,
        tool: Arc<dyn Tool>,
    ) -> Result<(), ToolRegistryError> {
        let key = id.into();
        let descriptor_id = tool.descriptor().id;
        if key != descriptor_id {
            return Err(ToolRegistryError::ToolIdMismatch { key, descriptor_id });
        }
        if self.tools.contains_key(&key) {
            return Err(ToolRegistryError::ToolIdConflict(key));
        }
        self.tools.insert(key, tool);
        Ok(())
    }

    pub fn extend_named(
        &mut self,
        tools: HashMap<String, Arc<dyn Tool>>,
    ) -> Result<(), ToolRegistryError> {
        for (key, tool) in tools {
            self.register_named(key, tool)?;
        }
        Ok(())
    }

    pub fn extend_registry(&mut self, other: &dyn ToolRegistry) -> Result<(), ToolRegistryError> {
        self.extend_named(other.snapshot())
    }

    pub fn merge_many(
        regs: impl IntoIterator<Item = InMemoryToolRegistry>,
    ) -> Result<InMemoryToolRegistry, ToolRegistryError> {
        let mut out = InMemoryToolRegistry::new();
        for r in regs {
            out.extend_named(r.into_map())?;
        }
        Ok(out)
    }

    pub fn into_map(self) -> HashMap<String, Arc<dyn Tool>> {
        self.tools
    }

    pub fn to_map(&self) -> HashMap<String, Arc<dyn Tool>> {
        self.tools.clone()
    }
}

impl ToolRegistry for InMemoryToolRegistry {
    fn len(&self) -> usize {
        self.len()
    }

    fn get(&self, id: &str) -> Option<Arc<dyn Tool>> {
        self.get(id)
    }

    fn ids(&self) -> Vec<String> {
        sorted_registry_ids(&self.tools)
    }

    fn snapshot(&self) -> HashMap<String, Arc<dyn Tool>> {
        self.tools.clone()
    }
}

#[derive(Clone, Default)]
pub struct CompositeToolRegistry {
    registries: Vec<Arc<dyn ToolRegistry>>,
    cached_snapshot: Arc<RwLock<HashMap<String, Arc<dyn Tool>>>>,
}

impl std::fmt::Debug for CompositeToolRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let snapshot = match self.cached_snapshot.read() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        f.debug_struct("CompositeToolRegistry")
            .field("registries", &self.registries.len())
            .field("len", &snapshot.len())
            .finish()
    }
}

impl CompositeToolRegistry {
    pub fn try_new(
        regs: impl IntoIterator<Item = Arc<dyn ToolRegistry>>,
    ) -> Result<Self, ToolRegistryError> {
        let registries: Vec<Arc<dyn ToolRegistry>> = regs.into_iter().collect();
        let merged = Self::merge_snapshots(&registries)?;
        Ok(Self {
            registries,
            cached_snapshot: Arc::new(RwLock::new(merged)),
        })
    }

    fn merge_snapshots(
        registries: &[Arc<dyn ToolRegistry>],
    ) -> Result<HashMap<String, Arc<dyn Tool>>, ToolRegistryError> {
        let mut merged = InMemoryToolRegistry::new();
        for reg in registries {
            merged.extend_registry(reg.as_ref())?;
        }
        Ok(merged.into_map())
    }

    fn refresh_snapshot(&self) -> Result<HashMap<String, Arc<dyn Tool>>, ToolRegistryError> {
        Self::merge_snapshots(&self.registries)
    }

    fn read_cached_snapshot(&self) -> HashMap<String, Arc<dyn Tool>> {
        match self.cached_snapshot.read() {
            Ok(guard) => guard.clone(),
            Err(poisoned) => poisoned.into_inner().clone(),
        }
    }

    fn write_cached_snapshot(&self, snapshot: HashMap<String, Arc<dyn Tool>>) {
        match self.cached_snapshot.write() {
            Ok(mut guard) => *guard = snapshot,
            Err(poisoned) => *poisoned.into_inner() = snapshot,
        };
    }
}

impl ToolRegistry for CompositeToolRegistry {
    fn len(&self) -> usize {
        self.snapshot().len()
    }

    fn get(&self, id: &str) -> Option<Arc<dyn Tool>> {
        self.snapshot().get(id).cloned()
    }

    fn ids(&self) -> Vec<String> {
        let snapshot = self.snapshot();
        sorted_registry_ids(&snapshot)
    }

    fn snapshot(&self) -> HashMap<String, Arc<dyn Tool>> {
        match self.refresh_snapshot() {
            Ok(snapshot) => {
                self.write_cached_snapshot(snapshot.clone());
                snapshot
            }
            Err(_) => self.read_cached_snapshot(),
        }
    }
}

// ---------------------------------------------------------------------------
// MCP ToolRegistry bridge
// ---------------------------------------------------------------------------

#[cfg(feature = "mcp")]
impl ToolRegistry for tirea_extension_mcp::McpToolRegistry {
    fn len(&self) -> usize {
        tirea_extension_mcp::McpToolRegistry::len(self)
    }

    fn get(&self, id: &str) -> Option<Arc<dyn Tool>> {
        tirea_extension_mcp::McpToolRegistry::get(self, id)
    }

    fn ids(&self) -> Vec<String> {
        tirea_extension_mcp::McpToolRegistry::ids(self)
    }

    fn snapshot(&self) -> HashMap<String, Arc<dyn Tool>> {
        tirea_extension_mcp::McpToolRegistry::snapshot(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::contracts::runtime::tool_call::{ToolDescriptor, ToolError, ToolResult};
    use crate::contracts::ToolCallContext;
    use serde_json::json;

    struct StaticTool {
        descriptor: ToolDescriptor,
    }

    impl StaticTool {
        fn new(id: &str) -> Self {
            Self {
                descriptor: ToolDescriptor::new(id, id, "test tool"),
            }
        }
    }

    #[async_trait::async_trait]
    impl Tool for StaticTool {
        fn descriptor(&self) -> ToolDescriptor {
            self.descriptor.clone()
        }

        async fn execute(
            &self,
            _args: serde_json::Value,
            _ctx: &ToolCallContext<'_>,
        ) -> Result<ToolResult, ToolError> {
            Ok(ToolResult::success(
                self.descriptor.id.clone(),
                json!({"ok": true}),
            ))
        }
    }

    #[derive(Default)]
    struct MutableToolRegistry {
        tools: RwLock<HashMap<String, Arc<dyn Tool>>>,
    }

    impl MutableToolRegistry {
        fn replace_ids(&self, ids: &[&str]) {
            let mut map = HashMap::new();
            for id in ids {
                map.insert(
                    (*id).to_string(),
                    Arc::new(StaticTool::new(id)) as Arc<dyn Tool>,
                );
            }
            match self.tools.write() {
                Ok(mut guard) => *guard = map,
                Err(poisoned) => *poisoned.into_inner() = map,
            }
        }
    }

    impl ToolRegistry for MutableToolRegistry {
        fn len(&self) -> usize {
            self.snapshot().len()
        }

        fn get(&self, id: &str) -> Option<Arc<dyn Tool>> {
            self.snapshot().get(id).cloned()
        }

        fn ids(&self) -> Vec<String> {
            let mut ids: Vec<String> = self.snapshot().keys().cloned().collect();
            ids.sort();
            ids
        }

        fn snapshot(&self) -> HashMap<String, Arc<dyn Tool>> {
            match self.tools.read() {
                Ok(guard) => guard.clone(),
                Err(poisoned) => poisoned.into_inner().clone(),
            }
        }
    }

    #[test]
    fn composite_tool_registry_reads_live_updates_from_source_registries() {
        let dynamic = Arc::new(MutableToolRegistry::default());
        dynamic.replace_ids(&["dynamic_a"]);

        let mut static_registry = InMemoryToolRegistry::new();
        static_registry
            .register_named("static_tool", Arc::new(StaticTool::new("static_tool")))
            .expect("register static tool");

        let composite = CompositeToolRegistry::try_new(vec![
            dynamic.clone() as Arc<dyn ToolRegistry>,
            Arc::new(static_registry) as Arc<dyn ToolRegistry>,
        ])
        .expect("compose registries");

        assert!(composite.ids().contains(&"dynamic_a".to_string()));
        assert!(composite.ids().contains(&"static_tool".to_string()));

        dynamic.replace_ids(&["dynamic_a", "dynamic_b"]);

        let ids = composite.ids();
        assert!(ids.contains(&"dynamic_a".to_string()));
        assert!(ids.contains(&"dynamic_b".to_string()));
        assert!(ids.contains(&"static_tool".to_string()));
    }

    #[test]
    fn composite_tool_registry_keeps_last_good_snapshot_on_runtime_conflict() {
        let reg_a = Arc::new(MutableToolRegistry::default());
        reg_a.replace_ids(&["tool_a"]);

        let reg_b = Arc::new(MutableToolRegistry::default());
        reg_b.replace_ids(&["tool_b"]);

        let composite = CompositeToolRegistry::try_new(vec![
            reg_a.clone() as Arc<dyn ToolRegistry>,
            reg_b.clone() as Arc<dyn ToolRegistry>,
        ])
        .expect("compose registries");

        let initial_ids = composite.ids();
        assert_eq!(
            initial_ids,
            vec!["tool_a".to_string(), "tool_b".to_string()]
        );

        // Introduce a conflict at runtime. Composite should fall back to last good snapshot.
        reg_b.replace_ids(&["tool_a"]);

        assert_eq!(composite.ids(), initial_ids);
        assert!(composite.get("tool_b").is_some());
    }
}

#[cfg(all(test, feature = "mcp"))]
mod mcp_tests {
    use super::*;
    use async_trait::async_trait;
    use mcp::transport::{McpServerConnectionConfig, McpTransportError, TransportTypeId};
    use mcp::McpToolDefinition;
    use serde_json::Value;
    use std::sync::{Arc, Mutex};
    use tokio::sync::mpsc;

    use crate::composition::{AgentDefinition, AgentDefinitionSpec};
    use crate::runtime::AgentOs;
    use tirea_extension_mcp::{McpProgressUpdate, McpToolRegistryManager, McpToolTransport};

    #[derive(Debug, Clone)]
    struct MutableTransport {
        tools: Arc<Mutex<Vec<McpToolDefinition>>>,
    }

    impl MutableTransport {
        fn new(tools: Vec<McpToolDefinition>) -> Self {
            Self {
                tools: Arc::new(Mutex::new(tools)),
            }
        }

        fn replace(&self, tools: Vec<McpToolDefinition>) {
            match self.tools.lock() {
                Ok(mut guard) => *guard = tools,
                Err(poisoned) => *poisoned.into_inner() = tools,
            }
        }
    }

    #[async_trait]
    impl McpToolTransport for MutableTransport {
        async fn list_tools(&self) -> Result<Vec<McpToolDefinition>, McpTransportError> {
            let tools = match self.tools.lock() {
                Ok(guard) => guard.clone(),
                Err(poisoned) => poisoned.into_inner().clone(),
            };
            Ok(tools)
        }

        async fn call_tool(
            &self,
            _name: &str,
            _args: Value,
            _progress_tx: Option<mpsc::UnboundedSender<McpProgressUpdate>>,
        ) -> Result<mcp::CallToolResult, McpTransportError> {
            Ok(mcp::CallToolResult {
                content: vec![mcp::ToolContent::text("ok")],
                structured_content: None,
                is_error: None,
            })
        }

        fn transport_type(&self) -> TransportTypeId {
            TransportTypeId::Stdio
        }
    }

    fn cfg(name: &str) -> McpServerConnectionConfig {
        McpServerConnectionConfig::stdio(name, "unused", vec![])
    }

    #[tokio::test]
    async fn mcp_registry_implements_dynamic_tool_registry() {
        let transport = Arc::new(MutableTransport::new(vec![McpToolDefinition::new("echo")]));
        let manager = McpToolRegistryManager::from_transports([(
            cfg("mcp_s1"),
            transport.clone() as Arc<dyn McpToolTransport>,
        )])
        .await
        .expect("build manager");
        let registry = Arc::new(manager.registry()) as Arc<dyn ToolRegistry>;

        let os = AgentOs::builder()
            .with_agent_spec(AgentDefinitionSpec::local_with_id(
                "assistant",
                AgentDefinition::new("gpt-4o-mini"),
            ))
            .with_tool_registry(registry)
            .build()
            .expect("build agent os");

        let resolved1 = os.resolve("assistant").expect("resolve first snapshot");
        assert!(resolved1.tools.contains_key("mcp__mcp_s1__echo"));
        assert!(!resolved1.tools.contains_key("mcp__mcp_s1__sum"));

        transport.replace(vec![McpToolDefinition::new("sum")]);
        manager.refresh().await.expect("refresh registry");

        let resolved2 = os.resolve("assistant").expect("resolve refreshed snapshot");
        assert!(!resolved2.tools.contains_key("mcp__mcp_s1__echo"));
        assert!(resolved2.tools.contains_key("mcp__mcp_s1__sum"));
    }
}