Skip to main content

causa_runtime/executor/
catalog.rs

1//! Acquire the definitions and concrete targets for one invocation.
2
3use super::{ToolCatalogError, registry::ToolExecutor};
4use crate::composition::ToolBridge;
5use causa_kernel::{CallControl, InvocationId, Tool, ToolSurface};
6use std::collections::HashMap;
7use std::sync::Arc;
8
9/// One invocation's fixed descriptions, concrete targets, and parent control.
10///
11/// The binding borrows the executor configuration without holding locks. It
12/// cannot be cloned, and processing consumes it. It does not prevent a caller
13/// from creating a new binding or independently replaying a declaration.
14pub struct BoundTools<'a> {
15    pub(super) executor: &'a ToolExecutor,
16    pub(super) surface: ToolSurface,
17    pub(super) targets: HashMap<String, Arc<dyn Tool>>,
18    pub(super) invocation_id: InvocationId,
19    pub(super) control: CallControl,
20}
21
22impl ToolExecutor {
23    /// Fixes an ordered catalog and its concrete dispatch targets for one
24    /// invocation. Listing failures abort acquisition rather than using stale
25    /// definitions or silently omitting a source.
26    pub async fn bind(
27        &self,
28        invocation_id: InvocationId,
29        control: CallControl,
30    ) -> Result<BoundTools<'_>, ToolCatalogError> {
31        control.check().map_err(ToolCatalogError::Control)?;
32        let registrations = self.dynamic.read().expect("dynamic registry lock").clone();
33        let mut definitions = Vec::new();
34        let mut targets = HashMap::new();
35        for entry in &self.tools {
36            control.check().map_err(ToolCatalogError::Control)?;
37            let definition = entry.tool.definition();
38            if definition.name != entry.name {
39                return Err(ToolCatalogError::StaticNameChanged {
40                    registered: entry.name.clone(),
41                    actual: definition.name,
42                });
43            }
44            targets.insert(entry.name.clone(), entry.tool.clone());
45            definitions.push(definition);
46        }
47        for entry in registrations {
48            control.check().map_err(ToolCatalogError::Control)?;
49            for definition in entry.list(&control).await? {
50                let name = definition.name.clone();
51                if targets.contains_key(&name) {
52                    return Err(ToolCatalogError::DuplicateTool { name });
53                }
54                targets.insert(
55                    name,
56                    Arc::new(ToolBridge::new(entry.source.clone(), definition.clone()))
57                        as Arc<dyn Tool>,
58                );
59                definitions.push(definition);
60            }
61        }
62        control.check().map_err(ToolCatalogError::Control)?;
63        Ok(BoundTools {
64            executor: self,
65            surface: ToolSurface::from_definitions(definitions),
66            targets,
67            invocation_id,
68            control,
69        })
70    }
71}
72
73impl BoundTools<'_> {
74    /// Borrows the ordered, immutable model-facing catalog fixed by this bind.
75    pub fn surface(&self) -> &ToolSurface {
76        &self.surface
77    }
78}