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, ProcessorData, Tool, ToolDescription, 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) tools: Vec<ToolDescription>,
18    pub(super) targets: HashMap<String, Arc<dyn Tool>>,
19    pub(super) invocation_id: InvocationId,
20    pub(super) control: CallControl,
21    pub(super) data: Option<ProcessorData>,
22}
23
24impl ToolExecutor {
25    /// Fixes an ordered catalog, the tools' declared hints and their concrete
26    /// dispatch targets for one invocation. Static tools give
27    /// [`Tool::definition`] and [`Tool::hints`]; dynamic sources give
28    /// [`causa_kernel::DynamicToolSource::describe`]. Listing failures abort
29    /// acquisition rather than using stale definitions or silently omitting a
30    /// source.
31    pub async fn bind(
32        &self,
33        invocation_id: InvocationId,
34        control: CallControl,
35    ) -> Result<BoundTools<'_>, ToolCatalogError> {
36        control.check().map_err(ToolCatalogError::Control)?;
37        let registrations = self.dynamic.read().expect("dynamic registry lock").clone();
38        let mut tools = Vec::new();
39        let mut targets = HashMap::new();
40        for entry in &self.tools {
41            control.check().map_err(ToolCatalogError::Control)?;
42            let definition = entry.tool.definition();
43            if definition.name != entry.name {
44                return Err(ToolCatalogError::StaticNameChanged {
45                    registered: entry.name.clone(),
46                    actual: definition.name,
47                });
48            }
49            targets.insert(entry.name.clone(), entry.tool.clone());
50            tools.push(ToolDescription::new(definition).with_hints(entry.tool.hints()));
51        }
52        for entry in registrations {
53            control.check().map_err(ToolCatalogError::Control)?;
54            for description in entry.describe(&control).await? {
55                let name = description.definition.name.clone();
56                if targets.contains_key(&name) {
57                    return Err(ToolCatalogError::DuplicateTool { name });
58                }
59                let bridge = ToolBridge::new(entry.source.clone(), description.definition.clone())
60                    .with_hints(description.hints.clone());
61                targets.insert(name, Arc::new(bridge) as Arc<dyn Tool>);
62                tools.push(description);
63            }
64        }
65        control.check().map_err(ToolCatalogError::Control)?;
66        let surface = ToolSurface::from_definitions(
67            tools.iter().map(|tool| tool.definition.clone()).collect(),
68        );
69        Ok(BoundTools {
70            executor: self,
71            surface,
72            tools,
73            targets,
74            invocation_id,
75            control,
76            data: None,
77        })
78    }
79}
80
81impl BoundTools<'_> {
82    /// Borrows the ordered, immutable model-facing catalog fixed by this bind.
83    pub fn surface(&self) -> &ToolSurface {
84        &self.surface
85    }
86
87    /// Hands `data` to every processor of this binding's batch through
88    /// [`causa_kernel::ProcessorContext::data`]. The runner sets it from
89    /// [`crate::TurnRunOptions::processor_data`]; a caller that binds and
90    /// processes on its own sets it here.
91    pub fn with_processor_data(mut self, data: ProcessorData) -> Self {
92        self.data = Some(data);
93        self
94    }
95}