agen 0.2.1

Provider-neutral orchestration 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
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use thiserror::Error;

use crate::llm_client::ToolDefinition as LlmToolDefinition;
use crate::tool::{
    Tool, ToolDefinition as EngineToolDefinition, ToolExecutionContext, ToolMeta, ToolOutput,
};

type ToolMap = HashMap<String, (ToolMeta, Arc<dyn Tool>)>;

/// Errors produced by ToolServer operations.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum ToolServerError {
    /// A tool with the same name already exists.
    #[error("Tool with name '{0}' already registered")]
    DuplicateName(String),
    /// Requested tool was not found.
    #[error("Tool '{0}' not found")]
    ToolNotFound(String),
    /// Tool execution failed.
    #[error("Tool execution failed: {0}")]
    ToolExecution(String),
}

/// In-memory tool server.
#[derive(Clone, Default)]
pub struct ToolServer {
    tools: Arc<Mutex<ToolMap>>,
    pending: Arc<Mutex<Vec<EngineToolDefinition>>>,
}

impl ToolServer {
    /// Create a new empty tool server.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a handle for shared access.
    pub fn handle(&self) -> ToolServerHandle {
        ToolServerHandle {
            tools: Arc::clone(&self.tools),
            pending: Arc::clone(&self.pending),
        }
    }
}

/// Shareable handle to a tool server.
#[derive(Clone, Default)]
pub struct ToolServerHandle {
    tools: Arc<Mutex<ToolMap>>,
    pending: Arc<Mutex<Vec<EngineToolDefinition>>>,
}

impl ToolServerHandle {
    /// Queue a tool factory for deferred initialization.
    ///
    /// The factory is **not** called here; it is stored and executed
    /// when [`flush_pending`](Self::flush_pending) is called (typically
    /// at the start of `Engine::run()`).
    pub(crate) fn register_tool(&self, factory: EngineToolDefinition) {
        self.pending
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push(factory);
    }

    /// Queue many tool factories for deferred initialization.
    pub(crate) fn register_tools(&self, factories: impl IntoIterator<Item = EngineToolDefinition>) {
        let mut guard = self.pending.lock().unwrap_or_else(|e| e.into_inner());
        guard.extend(factories);
    }

    /// Execute all pending factories and register the resulting tools.
    ///
    /// Called implicitly by `Engine::lock()` before the first turn.
    /// Exposed as `pub` so callers can force-materialise tools earlier —
    /// for example when building a system-prompt template context that needs
    /// the list of registered tool names. Redundant calls are no-ops.
    ///
    /// # Panics
    ///
    /// Panics if any factory produces a tool whose name collides with
    /// an already-registered tool. Duplicate names are a programming
    /// error and should be caught during development.
    pub fn flush_pending(&self) {
        let pending: Vec<_> = {
            let mut guard = self.pending.lock().unwrap_or_else(|e| e.into_inner());
            std::mem::take(&mut *guard)
        };
        if pending.is_empty() {
            return;
        }
        // Execute all factories first, then validate and insert atomically.
        let materialized: Vec<_> = pending.into_iter().map(|f| f()).collect();
        let mut tools = self.tools.lock().unwrap_or_else(|e| e.into_inner());
        for (meta, instance) in materialized {
            assert!(
                !tools.contains_key(&meta.name),
                "duplicate tool name: '{}'",
                meta.name,
            );
            tools.insert(meta.name.clone(), (meta, instance));
        }
    }

    /// Get a tool by name for hook contexts.
    pub fn get_tool(&self, name: &str) -> Option<(ToolMeta, Arc<dyn Tool>)> {
        let guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
        guard
            .get(name)
            .map(|(meta, tool)| (meta.clone(), Arc::clone(tool)))
    }

    /// Execute a tool by name.
    pub async fn call_tool(
        &self,
        name: &str,
        input_json: &str,
        ctx: ToolExecutionContext,
    ) -> Result<ToolOutput, ToolServerError> {
        let tool = {
            let guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
            let (_, tool) = guard
                .get(name)
                .ok_or_else(|| ToolServerError::ToolNotFound(name.to_string()))?;
            Arc::clone(tool)
        };
        tool.execute(input_json, ctx)
            .await
            .map_err(|e| ToolServerError::ToolExecution(e.to_string()))
    }

    /// Remove a registered tool by name.
    ///
    /// In-flight calls that already obtained an `Arc<dyn Tool>` clone are
    /// unaffected and will run to completion.
    pub fn unregister(&self, name: &str) -> Result<(), ToolServerError> {
        let mut guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
        guard
            .remove(name)
            .map(|_| ())
            .ok_or_else(|| ToolServerError::ToolNotFound(name.to_string()))
    }

    /// Replace an existing tool with a new implementation.
    ///
    /// The factory is called immediately and the resulting tool overwrites
    /// the entry with the same name. Returns `ToolNotFound` if the name
    /// produced by the factory does not match any registered tool.
    pub fn replace(&self, factory: EngineToolDefinition) -> Result<(), ToolServerError> {
        let (meta, instance) = factory();
        let mut guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
        if !guard.contains_key(&meta.name) {
            return Err(ToolServerError::ToolNotFound(meta.name));
        }
        guard.insert(meta.name.clone(), (meta, instance));
        Ok(())
    }

    /// Build deterministic tool definitions sorted by tool name.
    pub fn tool_definitions_sorted(&self) -> Vec<LlmToolDefinition> {
        let guard = self.tools.lock().unwrap_or_else(|e| e.into_inner());
        let mut defs: Vec<_> = guard
            .values()
            .map(|(meta, _)| {
                LlmToolDefinition::new(&meta.name)
                    .description(&meta.description)
                    .input_schema(meta.input_schema.clone())
            })
            .collect();
        defs.sort_by(|a, b| a.name.cmp(&b.name));
        defs
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use async_trait::async_trait;
    use serde_json::json;

    use super::*;
    use crate::tool::{Tool, ToolDefinition, ToolError, ToolMeta};

    struct EchoTool;

    #[async_trait]
    impl Tool for EchoTool {
        async fn execute(
            &self,
            input_json: &str,
            _ctx: crate::tool::ToolExecutionContext,
        ) -> Result<ToolOutput, ToolError> {
            Ok(input_json.to_string().into())
        }
    }

    fn def(name: &'static str) -> ToolDefinition {
        Arc::new(move || {
            (
                ToolMeta::new(name)
                    .description(format!("desc-{name}"))
                    .input_schema(json!({"type":"object"})),
                Arc::new(EchoTool) as Arc<dyn Tool>,
            )
        })
    }

    #[test]
    fn flush_pending_registers_tools() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("alpha"));
        handle.register_tool(def("beta"));

        // Before flush, no tools are available
        assert!(handle.get_tool("alpha").is_none());

        handle.flush_pending();

        // After flush, tools are available
        assert!(handle.get_tool("alpha").is_some());
        assert!(handle.get_tool("beta").is_some());
    }

    #[test]
    #[should_panic(expected = "duplicate tool name: 'alpha'")]
    fn flush_pending_duplicate_name_panics() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("alpha"));
        handle.flush_pending();

        handle.register_tool(def("alpha"));
        handle.flush_pending(); // panics
    }

    #[tokio::test]
    async fn call_tool_success_and_not_found() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("echo"));
        handle.flush_pending();

        let out = handle
            .call_tool("echo", r#"{"x":1}"#, Default::default())
            .await
            .expect("call");
        assert_eq!(out.summary, r#"{"x":1}"#);
        assert!(out.content.is_none());

        let err = handle
            .call_tool("missing", "{}", Default::default())
            .await
            .expect_err("missing tool");
        assert_eq!(err, ToolServerError::ToolNotFound("missing".to_string()));
    }

    #[test]
    fn tool_definitions_are_sorted() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("zeta"));
        handle.register_tool(def("alpha"));
        handle.register_tool(def("beta"));
        handle.flush_pending();

        let names: Vec<_> = handle
            .tool_definitions_sorted()
            .into_iter()
            .map(|d| d.name)
            .collect();
        assert_eq!(names, vec!["alpha", "beta", "zeta"]);
    }

    #[test]
    fn flush_pending_is_noop_when_empty() {
        let handle = ToolServer::new().handle();
        handle.flush_pending();
        handle.flush_pending();
    }

    #[test]
    fn unregister_removes_tool() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("alpha"));
        handle.flush_pending();

        handle.unregister("alpha").expect("unregister");
        assert!(handle.get_tool("alpha").is_none());
    }

    #[test]
    fn unregister_not_found() {
        let handle = ToolServer::new().handle();
        let err = handle.unregister("ghost").expect_err("should fail");
        assert_eq!(err, ToolServerError::ToolNotFound("ghost".to_string()));
    }

    #[test]
    fn replace_swaps_implementation() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("alpha"));
        handle.flush_pending();

        // Replace with a tool that returns a fixed string.
        struct FixedTool;

        #[async_trait]
        impl Tool for FixedTool {
            async fn execute(
                &self,
                _input_json: &str,
                _ctx: crate::tool::ToolExecutionContext,
            ) -> Result<ToolOutput, ToolError> {
                Ok("replaced".to_string().into())
            }
        }

        let replacement: ToolDefinition = Arc::new(|| {
            (
                ToolMeta::new("alpha")
                    .description("replaced-desc")
                    .input_schema(json!({"type":"object"})),
                Arc::new(FixedTool) as Arc<dyn Tool>,
            )
        });
        handle.replace(replacement).expect("replace");

        let (meta, _) = handle.get_tool("alpha").expect("exists");
        assert_eq!(meta.description, "replaced-desc");
    }

    #[tokio::test]
    async fn replace_updates_call_result() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("echo"));
        handle.flush_pending();

        struct ConstTool;

        #[async_trait]
        impl Tool for ConstTool {
            async fn execute(
                &self,
                _input_json: &str,
                _ctx: crate::tool::ToolExecutionContext,
            ) -> Result<ToolOutput, ToolError> {
                Ok("const".to_string().into())
            }
        }

        let replacement: ToolDefinition = Arc::new(|| {
            (
                ToolMeta::new("echo")
                    .description("const")
                    .input_schema(json!({"type":"object"})),
                Arc::new(ConstTool) as Arc<dyn Tool>,
            )
        });
        handle.replace(replacement).expect("replace");

        let out = handle
            .call_tool("echo", "{}", Default::default())
            .await
            .expect("call");
        assert_eq!(out.summary, "const");
    }

    #[tokio::test]
    async fn unregister_during_execution_does_not_affect_inflight() {
        use tokio::sync::Notify;

        let started = Arc::new(Notify::new());
        let finish = Arc::new(Notify::new());

        struct GatedTool {
            started: Arc<Notify>,
            finish: Arc<Notify>,
        }

        #[async_trait]
        impl Tool for GatedTool {
            async fn execute(
                &self,
                _input_json: &str,
                _ctx: crate::tool::ToolExecutionContext,
            ) -> Result<ToolOutput, ToolError> {
                self.started.notify_one();
                self.finish.notified().await;
                Ok("done".to_string().into())
            }
        }

        let handle = ToolServer::new().handle();
        let s = Arc::clone(&started);
        let f = Arc::clone(&finish);
        handle.register_tool(Arc::new(move || {
            (
                ToolMeta::new("slow")
                    .description("slow")
                    .input_schema(json!({"type":"object"})),
                Arc::new(GatedTool {
                    started: Arc::clone(&s),
                    finish: Arc::clone(&f),
                }) as Arc<dyn Tool>,
            )
        }));
        handle.flush_pending();

        let h = handle.clone();
        let call = tokio::spawn(async move { h.call_tool("slow", "{}", Default::default()).await });

        // Wait until the tool is actually executing.
        started.notified().await;

        // Unregister while the tool is mid-execution.
        handle.unregister("slow").expect("unregister");
        assert!(handle.get_tool("slow").is_none());

        // Let the in-flight call finish.
        finish.notify_one();
        let result = call.await.expect("join");
        assert_eq!(result.expect("call").summary, "done");
    }

    #[tokio::test]
    async fn replace_during_execution_inflight_uses_old_impl() {
        use tokio::sync::Notify;

        let started = Arc::new(Notify::new());
        let finish = Arc::new(Notify::new());

        struct OldTool {
            started: Arc<Notify>,
            finish: Arc<Notify>,
        }

        #[async_trait]
        impl Tool for OldTool {
            async fn execute(
                &self,
                _input_json: &str,
                _ctx: crate::tool::ToolExecutionContext,
            ) -> Result<ToolOutput, ToolError> {
                self.started.notify_one();
                self.finish.notified().await;
                Ok("old".to_string().into())
            }
        }

        let handle = ToolServer::new().handle();
        let s = Arc::clone(&started);
        let f = Arc::clone(&finish);
        handle.register_tool(Arc::new(move || {
            (
                ToolMeta::new("t")
                    .description("d")
                    .input_schema(json!({"type":"object"})),
                Arc::new(OldTool {
                    started: Arc::clone(&s),
                    finish: Arc::clone(&f),
                }) as Arc<dyn Tool>,
            )
        }));
        handle.flush_pending();

        let h = handle.clone();
        let call = tokio::spawn(async move { h.call_tool("t", "{}", Default::default()).await });

        // Wait until the old tool is mid-execution.
        started.notified().await;

        // Replace while the old tool is executing.
        struct NewTool;

        #[async_trait]
        impl Tool for NewTool {
            async fn execute(
                &self,
                _input_json: &str,
                _ctx: crate::tool::ToolExecutionContext,
            ) -> Result<ToolOutput, ToolError> {
                Ok("new".to_string().into())
            }
        }

        handle
            .replace(Arc::new(|| {
                (
                    ToolMeta::new("t")
                        .description("d")
                        .input_schema(json!({"type":"object"})),
                    Arc::new(NewTool) as Arc<dyn Tool>,
                )
            }))
            .expect("replace");

        // Let the old in-flight call finish — it should return "old".
        finish.notify_one();
        let result = call.await.expect("join");
        assert_eq!(result.expect("call").summary, "old");

        // New calls use the replacement.
        let out = handle
            .call_tool("t", "{}", Default::default())
            .await
            .expect("call");
        assert_eq!(out.summary, "new");
    }

    #[test]
    fn unregister_reflects_in_tool_definitions() {
        let handle = ToolServer::new().handle();
        handle.register_tool(def("alpha"));
        handle.register_tool(def("beta"));
        handle.flush_pending();

        handle.unregister("alpha").expect("unregister");
        let names: Vec<_> = handle
            .tool_definitions_sorted()
            .into_iter()
            .map(|d| d.name)
            .collect();
        assert_eq!(names, vec!["beta"]);
    }

    #[test]
    fn replace_not_found() {
        let handle = ToolServer::new().handle();
        let factory: ToolDefinition = Arc::new(|| {
            (
                ToolMeta::new("ghost")
                    .description("x")
                    .input_schema(json!({"type":"object"})),
                Arc::new(EchoTool) as Arc<dyn Tool>,
            )
        });
        let err = handle.replace(factory).expect_err("should fail");
        assert_eq!(err, ToolServerError::ToolNotFound("ghost".to_string()));
    }
}