Skip to main content

zeph_tools/
composite.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Composite executor that chains two [`ToolExecutor`] implementations.
5
6use crate::executor::{ToolCall, ToolError, ToolExecutor, ToolOutput};
7use crate::registry::ToolDef;
8
9/// Chains two [`ToolExecutor`] implementations with first-match-wins dispatch.
10///
11/// For each method, `first` is tried first. If it returns `Ok(None)` (i.e. it does not
12/// handle the input), `second` is tried. If `first` returns an `Err`, the error propagates
13/// immediately without consulting `second`.
14///
15/// Use this to compose a chain of specialized executors at startup instead of a dynamic
16/// `Vec<Box<dyn ...>>`. Nest multiple `CompositeExecutor`s to handle more than two backends.
17///
18/// Tool definitions from both executors are merged, with `first` taking precedence when
19/// both define a tool with the same ID.
20///
21/// # Example
22///
23/// ```rust
24/// use zeph_tools::{
25///     CompositeExecutor, ShellExecutor, WebScrapeExecutor, ShellConfig, ScrapeConfig,
26/// };
27///
28/// let shell = ShellExecutor::new(&ShellConfig::default());
29/// let scrape = WebScrapeExecutor::new(&ScrapeConfig::default());
30/// let executor = CompositeExecutor::new(shell, scrape);
31/// // executor handles both bash blocks and scrape/fetch tool calls.
32/// ```
33#[derive(Debug)]
34pub struct CompositeExecutor<A: ToolExecutor, B: ToolExecutor> {
35    first: A,
36    second: B,
37}
38
39impl<A: ToolExecutor, B: ToolExecutor> CompositeExecutor<A, B> {
40    /// Create a new `CompositeExecutor` wrapping `first` and `second`.
41    #[must_use]
42    pub fn new(first: A, second: B) -> Self {
43        Self { first, second }
44    }
45}
46
47impl<A: ToolExecutor, B: ToolExecutor> ToolExecutor for CompositeExecutor<A, B> {
48    async fn execute(&self, response: &str) -> Result<Option<ToolOutput>, ToolError> {
49        if let Some(output) = self.first.execute(response).await? {
50            return Ok(Some(output));
51        }
52        self.second.execute(response).await
53    }
54
55    async fn execute_confirmed(&self, response: &str) -> Result<Option<ToolOutput>, ToolError> {
56        if let Some(output) = self.first.execute_confirmed(response).await? {
57            return Ok(Some(output));
58        }
59        self.second.execute_confirmed(response).await
60    }
61
62    fn tool_definitions(&self) -> Vec<ToolDef> {
63        let mut defs = self.first.tool_definitions();
64        let seen: std::collections::HashSet<String> =
65            defs.iter().map(|d| d.id.to_string()).collect();
66        for def in self.second.tool_definitions() {
67            if !seen.contains(def.id.as_ref()) {
68                defs.push(def);
69            }
70        }
71        defs
72    }
73
74    async fn execute_tool_call(&self, call: &ToolCall) -> Result<Option<ToolOutput>, ToolError> {
75        if let Some(output) = self.first.execute_tool_call(call).await? {
76            return Ok(Some(output));
77        }
78        self.second.execute_tool_call(call).await
79    }
80
81    fn is_tool_retryable(&self, tool_id: &str) -> bool {
82        self.first.is_tool_retryable(tool_id) || self.second.is_tool_retryable(tool_id)
83    }
84
85    fn is_tool_speculatable(&self, tool_id: &str) -> bool {
86        self.first.is_tool_speculatable(tool_id) || self.second.is_tool_speculatable(tool_id)
87    }
88
89    /// Forward the active skill's env injection to BOTH inner executors.
90    ///
91    /// The base [`ToolExecutor::set_skill_env`] is a no-op, so without this override the
92    /// composition tree built in `agent_setup` silently swallows env injection — the
93    /// underlying `ShellExecutor` never sees `GITHUB_TOKEN` etc. Each layer ignores the call
94    /// if it does not own a `skill_env` slot; layers that do (e.g. `ShellExecutor`) update
95    /// their state. See #3869.
96    fn set_skill_env(&self, env: Option<std::collections::HashMap<String, String>>) {
97        self.first.set_skill_env(env.clone());
98        self.second.set_skill_env(env);
99    }
100
101    /// Forward the active skill's trust level to BOTH inner executors.
102    ///
103    /// Mirrors [`Self::set_skill_env`]: without this override, `TrustGateExecutor` never
104    /// observes a non-`Trusted` level when composed under `CompositeExecutor`, leaving
105    /// quarantine enforcement effectively bypassed. See #3869.
106    fn set_effective_trust(&self, level: crate::SkillTrustLevel) {
107        self.first.set_effective_trust(level);
108        self.second.set_effective_trust(level);
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115    use crate::ToolName;
116
117    #[derive(Debug)]
118    struct MatchingExecutor;
119    impl ToolExecutor for MatchingExecutor {
120        async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
121            Ok(Some(ToolOutput {
122                tool_name: ToolName::new("test"),
123                summary: "matched".to_owned(),
124                blocks_executed: 1,
125                filter_stats: None,
126                diff: None,
127                streamed: false,
128                terminal_id: None,
129                locations: None,
130                raw_response: None,
131                claim_source: None,
132            }))
133        }
134    }
135
136    #[derive(Debug)]
137    struct NoMatchExecutor;
138    impl ToolExecutor for NoMatchExecutor {
139        async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
140            Ok(None)
141        }
142    }
143
144    #[derive(Debug)]
145    struct ErrorExecutor;
146    impl ToolExecutor for ErrorExecutor {
147        async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
148            Err(ToolError::Blocked {
149                command: "test".to_owned(),
150            })
151        }
152    }
153
154    #[derive(Debug)]
155    struct SecondExecutor;
156    impl ToolExecutor for SecondExecutor {
157        async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
158            Ok(Some(ToolOutput {
159                tool_name: ToolName::new("test"),
160                summary: "second".to_owned(),
161                blocks_executed: 1,
162                filter_stats: None,
163                diff: None,
164                streamed: false,
165                terminal_id: None,
166                locations: None,
167                raw_response: None,
168                claim_source: None,
169            }))
170        }
171    }
172
173    #[tokio::test]
174    async fn first_matches_returns_first() {
175        let composite = CompositeExecutor::new(MatchingExecutor, SecondExecutor);
176        let result = composite.execute("anything").await.unwrap();
177        assert_eq!(result.unwrap().summary, "matched");
178    }
179
180    #[tokio::test]
181    async fn first_none_falls_through_to_second() {
182        let composite = CompositeExecutor::new(NoMatchExecutor, SecondExecutor);
183        let result = composite.execute("anything").await.unwrap();
184        assert_eq!(result.unwrap().summary, "second");
185    }
186
187    #[tokio::test]
188    async fn both_none_returns_none() {
189        let composite = CompositeExecutor::new(NoMatchExecutor, NoMatchExecutor);
190        let result = composite.execute("anything").await.unwrap();
191        assert!(result.is_none());
192    }
193
194    #[tokio::test]
195    async fn first_error_propagates_without_trying_second() {
196        let composite = CompositeExecutor::new(ErrorExecutor, SecondExecutor);
197        let result = composite.execute("anything").await;
198        assert!(matches!(result, Err(ToolError::Blocked { .. })));
199    }
200
201    #[tokio::test]
202    async fn second_error_propagates_when_first_none() {
203        let composite = CompositeExecutor::new(NoMatchExecutor, ErrorExecutor);
204        let result = composite.execute("anything").await;
205        assert!(matches!(result, Err(ToolError::Blocked { .. })));
206    }
207
208    #[tokio::test]
209    async fn execute_confirmed_first_matches() {
210        let composite = CompositeExecutor::new(MatchingExecutor, SecondExecutor);
211        let result = composite.execute_confirmed("anything").await.unwrap();
212        assert_eq!(result.unwrap().summary, "matched");
213    }
214
215    #[tokio::test]
216    async fn execute_confirmed_falls_through() {
217        let composite = CompositeExecutor::new(NoMatchExecutor, SecondExecutor);
218        let result = composite.execute_confirmed("anything").await.unwrap();
219        assert_eq!(result.unwrap().summary, "second");
220    }
221
222    #[test]
223    fn composite_debug() {
224        let composite = CompositeExecutor::new(MatchingExecutor, SecondExecutor);
225        let debug = format!("{composite:?}");
226        assert!(debug.contains("CompositeExecutor"));
227    }
228
229    #[derive(Debug)]
230    struct FileToolExecutor;
231    impl ToolExecutor for FileToolExecutor {
232        async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
233            Ok(None)
234        }
235        async fn execute_tool_call(
236            &self,
237            call: &ToolCall,
238        ) -> Result<Option<ToolOutput>, ToolError> {
239            if call.tool_id == "read" || call.tool_id == "write" {
240                Ok(Some(ToolOutput {
241                    tool_name: call.tool_id.clone(),
242                    summary: "file_handler".to_owned(),
243                    blocks_executed: 1,
244                    filter_stats: None,
245                    diff: None,
246                    streamed: false,
247                    terminal_id: None,
248                    locations: None,
249                    raw_response: None,
250                    claim_source: None,
251                }))
252            } else {
253                Ok(None)
254            }
255        }
256    }
257
258    #[derive(Debug)]
259    struct ShellToolExecutor;
260    impl ToolExecutor for ShellToolExecutor {
261        async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
262            Ok(None)
263        }
264        async fn execute_tool_call(
265            &self,
266            call: &ToolCall,
267        ) -> Result<Option<ToolOutput>, ToolError> {
268            if call.tool_id == "bash" {
269                Ok(Some(ToolOutput {
270                    tool_name: ToolName::new("bash"),
271                    summary: "shell_handler".to_owned(),
272                    blocks_executed: 1,
273                    filter_stats: None,
274                    diff: None,
275                    streamed: false,
276                    terminal_id: None,
277                    locations: None,
278                    raw_response: None,
279                    claim_source: None,
280                }))
281            } else {
282                Ok(None)
283            }
284        }
285    }
286
287    #[tokio::test]
288    async fn tool_call_routes_to_file_executor() {
289        let composite = CompositeExecutor::new(FileToolExecutor, ShellToolExecutor);
290        let call = ToolCall {
291            tool_id: ToolName::new("read"),
292            params: serde_json::Map::new(),
293            caller_id: None,
294            context: None,
295
296            tool_call_id: String::new(),
297        };
298        let result = composite.execute_tool_call(&call).await.unwrap().unwrap();
299        assert_eq!(result.summary, "file_handler");
300    }
301
302    #[tokio::test]
303    async fn tool_call_routes_to_shell_executor() {
304        let composite = CompositeExecutor::new(FileToolExecutor, ShellToolExecutor);
305        let call = ToolCall {
306            tool_id: ToolName::new("bash"),
307            params: serde_json::Map::new(),
308            caller_id: None,
309            context: None,
310
311            tool_call_id: String::new(),
312        };
313        let result = composite.execute_tool_call(&call).await.unwrap().unwrap();
314        assert_eq!(result.summary, "shell_handler");
315    }
316
317    #[tokio::test]
318    async fn tool_call_unhandled_returns_none() {
319        let composite = CompositeExecutor::new(FileToolExecutor, ShellToolExecutor);
320        let call = ToolCall {
321            tool_id: ToolName::new("unknown"),
322            params: serde_json::Map::new(),
323            caller_id: None,
324            context: None,
325
326            tool_call_id: String::new(),
327        };
328        let result = composite.execute_tool_call(&call).await.unwrap();
329        assert!(result.is_none());
330    }
331
332    /// Regression test for #3869: state-mutating setters MUST reach both inner executors,
333    /// even across nested compositions. Prior to the fix, `set_skill_env` and
334    /// `set_effective_trust` fell through to the default no-op `ToolExecutor` impls and
335    /// were silently dropped at the `CompositeExecutor` boundary — breaking skill secret
336    /// env injection (`x-requires-secrets`) and quarantine trust enforcement.
337    mod state_forwarding {
338        use super::*;
339        use crate::SkillTrustLevel;
340        use std::sync::Mutex;
341
342        #[derive(Debug, Default)]
343        struct SpyExecutor {
344            last_env: Mutex<Option<std::collections::HashMap<String, String>>>,
345            last_trust: Mutex<Option<SkillTrustLevel>>,
346        }
347        impl ToolExecutor for SpyExecutor {
348            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
349                Ok(None)
350            }
351            fn set_skill_env(&self, env: Option<std::collections::HashMap<String, String>>) {
352                *self.last_env.lock().unwrap() = env;
353            }
354            fn set_effective_trust(&self, level: SkillTrustLevel) {
355                *self.last_trust.lock().unwrap() = Some(level);
356            }
357        }
358
359        #[test]
360        fn set_skill_env_reaches_both_inner_executors_in_nested_composition() {
361            // Mirrors the production wiring shape: a tree of CompositeExecutor with
362            // multiple leaves. All leaves must observe the call.
363            let leaf_a = SpyExecutor::default();
364            let leaf_b = SpyExecutor::default();
365            let leaf_c = SpyExecutor::default();
366            let nested = CompositeExecutor::new(leaf_a, leaf_b);
367            let outer = CompositeExecutor::new(nested, leaf_c);
368
369            let mut env = std::collections::HashMap::new();
370            env.insert("GITHUB_TOKEN".to_owned(), "tok".to_owned());
371            outer.set_skill_env(Some(env.clone()));
372
373            // first.first (leaf_a)
374            assert_eq!(
375                outer.first.first.last_env.lock().unwrap().as_ref(),
376                Some(&env)
377            );
378            // first.second (leaf_b)
379            assert_eq!(
380                outer.first.second.last_env.lock().unwrap().as_ref(),
381                Some(&env)
382            );
383            // second (leaf_c)
384            assert_eq!(outer.second.last_env.lock().unwrap().as_ref(), Some(&env));
385        }
386
387        #[test]
388        fn set_effective_trust_reaches_both_inner_executors_in_nested_composition() {
389            let leaf_a = SpyExecutor::default();
390            let leaf_b = SpyExecutor::default();
391            let outer = CompositeExecutor::new(leaf_a, leaf_b);
392
393            outer.set_effective_trust(SkillTrustLevel::Quarantined);
394
395            assert_eq!(
396                *outer.first.last_trust.lock().unwrap(),
397                Some(SkillTrustLevel::Quarantined)
398            );
399            assert_eq!(
400                *outer.second.last_trust.lock().unwrap(),
401                Some(SkillTrustLevel::Quarantined)
402            );
403        }
404    }
405}