Skip to main content

zeph_subagent/
filter.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Tool and skill filtering for sub-agents.
5//!
6//! [`FilteredToolExecutor`] wraps any [`ErasedToolExecutor`] and enforces a [`ToolPolicy`]
7//! plus an optional extra denylist on every tool invocation.
8//!
9//! [`PlanModeExecutor`] wraps any executor to allow catalog inspection while blocking all
10//! execution — implementing the read-only planning permission mode.
11//!
12//! [`filter_skills`] applies glob-based include/exclude patterns against a skill registry.
13
14use std::collections::HashMap;
15use std::pin::Pin;
16use std::sync::Arc;
17
18use zeph_skills::loader::Skill;
19use zeph_skills::registry::SkillRegistry;
20use zeph_tools::ToolCall;
21use zeph_tools::executor::{ErasedToolExecutor, ToolError, ToolOutput, extract_fenced_blocks};
22use zeph_tools::registry::{InvocationHint, ToolDef};
23
24use super::def::{SkillFilter, ToolPolicy};
25use super::error::SubAgentError;
26
27// ── Helpers ───────────────────────────────────────────────────────────────────
28
29/// Collect all fenced-block language tags from an executor's tool definitions.
30fn collect_fenced_tags(executor: &dyn ErasedToolExecutor) -> Vec<&'static str> {
31    executor
32        .tool_definitions_erased()
33        .into_iter()
34        .filter_map(|def| match def.invocation {
35            InvocationHint::FencedBlock(tag) => Some(tag),
36            InvocationHint::ToolCall => None,
37        })
38        .collect()
39}
40
41// ── Tool filtering ────────────────────────────────────────────────────────────
42
43/// Wraps an [`ErasedToolExecutor`] and enforces a [`ToolPolicy`] plus an optional
44/// additional denylist (`disallowed`).
45///
46/// All calls are checked against the policy and the denylist before being forwarded
47/// to the inner executor. The denylist is evaluated first — a tool in `disallowed`
48/// is blocked even if `policy` would allow it (deny wins). Rejected calls return a
49/// descriptive [`ToolError`].
50pub struct FilteredToolExecutor {
51    inner: Arc<dyn ErasedToolExecutor>,
52    policy: ToolPolicy,
53    disallowed: Vec<String>,
54    /// Fenced-block language tags collected from `inner` at construction time.
55    /// Used to detect actual fenced-block tool invocations in LLM responses.
56    fenced_tags: Vec<&'static str>,
57}
58
59impl FilteredToolExecutor {
60    /// Create a new filtered executor with the given policy and no additional denylist.
61    ///
62    /// Use [`with_disallowed`][Self::with_disallowed] when the agent definition also
63    /// specifies `tools.except` entries.
64    #[must_use]
65    pub fn new(inner: Arc<dyn ErasedToolExecutor>, policy: ToolPolicy) -> Self {
66        let fenced_tags = collect_fenced_tags(&*inner);
67        Self {
68            inner,
69            policy,
70            disallowed: Vec::new(),
71            fenced_tags,
72        }
73    }
74
75    /// Create a new filtered executor with an additional denylist.
76    ///
77    /// Tools in `disallowed` are blocked regardless of the base `policy`
78    /// (deny wins over allow).
79    #[must_use]
80    pub fn with_disallowed(
81        inner: Arc<dyn ErasedToolExecutor>,
82        policy: ToolPolicy,
83        disallowed: Vec<String>,
84    ) -> Self {
85        let fenced_tags = collect_fenced_tags(&*inner);
86        Self {
87            inner,
88            policy,
89            disallowed,
90            fenced_tags,
91        }
92    }
93
94    /// Return `true` if `response` contains at least one fenced block matching a registered tool.
95    fn has_fenced_tool_invocation(&self, response: &str) -> bool {
96        self.fenced_tags
97            .iter()
98            .any(|tag| !extract_fenced_blocks(response, tag).is_empty())
99    }
100
101    /// Check whether `tool_id` is allowed under the current policy and denylist.
102    ///
103    /// Matching is exact string equality. MCP compound tool IDs (e.g. `mcp__server__tool`)
104    /// must be listed in full in `tools.except` — partial names or prefixes are not matched.
105    fn is_allowed(&self, tool_id: &str) -> bool {
106        if self.disallowed.iter().any(|t| t == tool_id) {
107            return false;
108        }
109        match &self.policy {
110            ToolPolicy::InheritAll => true,
111            ToolPolicy::AllowList(list) => list.iter().any(|t| t == tool_id),
112            ToolPolicy::DenyList(list) => !list.iter().any(|t| t == tool_id),
113        }
114    }
115}
116
117impl ErasedToolExecutor for FilteredToolExecutor {
118    fn execute_erased<'a>(
119        &'a self,
120        response: &'a str,
121    ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
122    {
123        // Sub-agents must use structured tool calls (execute_tool_call_erased).
124        // Fenced-block execution is disabled to prevent policy bypass (SEC-03).
125        //
126        // However, this method is also called for plain-text LLM responses that
127        // contain markdown code fences unrelated to tool invocations. Returning
128        // Err unconditionally causes the agent loop to treat every text response
129        // as a failed tool call and exhaust all turns without producing output.
130        //
131        // Only block when the response actually contains a fenced block that
132        // matches a registered fenced-block tool language tag.
133        if self.has_fenced_tool_invocation(response) {
134            tracing::warn!("sub-agent attempted fenced-block tool invocation — blocked by policy");
135            return Box::pin(std::future::ready(Err(ToolError::Blocked {
136                command: "fenced-block".into(),
137            })));
138        }
139        Box::pin(std::future::ready(Ok(None)))
140    }
141
142    fn execute_confirmed_erased<'a>(
143        &'a self,
144        response: &'a str,
145    ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
146    {
147        // Same policy as execute_erased: only block actual fenced-block invocations.
148        if self.has_fenced_tool_invocation(response) {
149            tracing::warn!(
150                "sub-agent attempted confirmed fenced-block tool invocation — blocked by policy"
151            );
152            return Box::pin(std::future::ready(Err(ToolError::Blocked {
153                command: "fenced-block".into(),
154            })));
155        }
156        Box::pin(std::future::ready(Ok(None)))
157    }
158
159    fn tool_definitions_erased(&self) -> Vec<ToolDef> {
160        // Filter the visible tool definitions according to the policy.
161        self.inner
162            .tool_definitions_erased()
163            .into_iter()
164            .filter(|def| self.is_allowed(&def.id))
165            .collect()
166    }
167
168    fn execute_tool_call_erased<'a>(
169        &'a self,
170        call: &'a ToolCall,
171    ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
172    {
173        if !self.is_allowed(call.tool_id.as_str()) {
174            tracing::warn!(
175                tool_id = %call.tool_id,
176                "sub-agent tool call rejected by policy"
177            );
178            return Box::pin(std::future::ready(Err(ToolError::Blocked {
179                command: call.tool_id.to_string(),
180            })));
181        }
182        Box::pin(self.inner.execute_tool_call_erased(call))
183    }
184
185    fn set_skill_env(&self, env: Option<HashMap<String, String>>) {
186        self.inner.set_skill_env(env);
187    }
188
189    fn is_tool_retryable_erased(&self, tool_id: &str) -> bool {
190        self.inner.is_tool_retryable_erased(tool_id)
191    }
192}
193
194// ── Plan mode executor ────────────────────────────────────────────────────────
195
196/// Wraps an [`ErasedToolExecutor`] for `Plan` permission mode.
197///
198/// Exposes the real tool catalog via `tool_definitions_erased()` so the LLM can
199/// reference existing tools in its plan, but blocks all execution methods with
200/// [`ToolError::Blocked`]. This implements read-only planning: the agent sees what
201/// tools exist but cannot invoke them.
202pub struct PlanModeExecutor {
203    inner: Arc<dyn ErasedToolExecutor>,
204}
205
206impl PlanModeExecutor {
207    /// Wrap `inner` with plan-mode restrictions.
208    #[must_use]
209    pub fn new(inner: Arc<dyn ErasedToolExecutor>) -> Self {
210        Self { inner }
211    }
212}
213
214impl ErasedToolExecutor for PlanModeExecutor {
215    fn execute_erased<'a>(
216        &'a self,
217        _response: &'a str,
218    ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
219    {
220        Box::pin(std::future::ready(Err(ToolError::Blocked {
221            command: "plan_mode".into(),
222        })))
223    }
224
225    fn execute_confirmed_erased<'a>(
226        &'a self,
227        _response: &'a str,
228    ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
229    {
230        Box::pin(std::future::ready(Err(ToolError::Blocked {
231            command: "plan_mode".into(),
232        })))
233    }
234
235    fn tool_definitions_erased(&self) -> Vec<ToolDef> {
236        self.inner.tool_definitions_erased()
237    }
238
239    fn execute_tool_call_erased<'a>(
240        &'a self,
241        call: &'a ToolCall,
242    ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
243    {
244        tracing::debug!(
245            tool_id = %call.tool_id,
246            "tool execution blocked in plan mode"
247        );
248        Box::pin(std::future::ready(Err(ToolError::Blocked {
249            command: call.tool_id.to_string(),
250        })))
251    }
252
253    fn set_skill_env(&self, env: Option<std::collections::HashMap<String, String>>) {
254        self.inner.set_skill_env(env);
255    }
256
257    fn is_tool_retryable_erased(&self, _tool_id: &str) -> bool {
258        false
259    }
260}
261
262// ── Skill filtering ───────────────────────────────────────────────────────────
263
264/// Filter skills from a registry according to a [`SkillFilter`].
265///
266/// Include patterns are glob-matched against skill names. If `include` is empty,
267/// all skills pass (unless excluded). Exclude patterns always take precedence.
268///
269/// Supported glob syntax:
270/// - `*` — wildcard matching any substring (e.g., `"git-*"`)
271/// - Literal strings — exact match only
272/// - `**` is **not** supported and returns [`SubAgentError::Invalid`]
273///
274/// # Errors
275///
276/// Returns [`SubAgentError::Invalid`] if any glob pattern is syntactically invalid.
277///
278/// # Examples
279///
280/// ```rust,no_run
281/// use zeph_skills::registry::SkillRegistry;
282/// use zeph_subagent::filter_skills;
283/// use zeph_subagent::SkillFilter;
284///
285/// let registry = SkillRegistry::load(&[] as &[&str]);
286/// let filter = SkillFilter { include: vec![], exclude: vec![] };
287/// let skills = filter_skills(&registry, &filter).unwrap();
288/// assert!(skills.is_empty());
289/// ```
290pub fn filter_skills(
291    registry: &SkillRegistry,
292    filter: &SkillFilter,
293) -> Result<Vec<Skill>, SubAgentError> {
294    let compiled_include = compile_globs(&filter.include)?;
295    let compiled_exclude = compile_globs(&filter.exclude)?;
296
297    let all: Vec<Skill> = registry
298        .all_meta()
299        .into_iter()
300        .filter(|meta| {
301            let name = &meta.name;
302            let included =
303                compiled_include.is_empty() || compiled_include.iter().any(|p| glob_match(p, name));
304            let excluded = compiled_exclude.iter().any(|p| glob_match(p, name));
305            included && !excluded
306        })
307        .filter_map(|meta| registry.get_skill(&meta.name).ok())
308        .collect();
309
310    Ok(all)
311}
312
313/// Compiled glob pattern: literal prefix + optional `*` wildcard suffix.
314struct GlobPattern {
315    raw: String,
316    prefix: String,
317    suffix: Option<String>,
318    is_star: bool,
319}
320
321fn compile_globs(patterns: &[String]) -> Result<Vec<GlobPattern>, SubAgentError> {
322    patterns.iter().map(|p| compile_glob(p)).collect()
323}
324
325fn compile_glob(pattern: &str) -> Result<GlobPattern, SubAgentError> {
326    // Simple glob: supports `*` as a wildcard anywhere in the string.
327    // For MVP we only need prefix-star patterns like "git-*" or "*".
328    if pattern.contains("**") {
329        return Err(SubAgentError::Invalid(format!(
330            "glob pattern '{pattern}' uses '**' which is not supported"
331        )));
332    }
333
334    let is_star = pattern == "*";
335
336    let (prefix, suffix) = if let Some(pos) = pattern.find('*') {
337        let before = pattern[..pos].to_owned();
338        let after = pattern[pos + 1..].to_owned();
339        (before, Some(after))
340    } else {
341        (pattern.to_owned(), None)
342    };
343
344    Ok(GlobPattern {
345        raw: pattern.to_owned(),
346        prefix,
347        suffix,
348        is_star,
349    })
350}
351
352fn glob_match(pattern: &GlobPattern, name: &str) -> bool {
353    if pattern.is_star {
354        return true;
355    }
356
357    match &pattern.suffix {
358        None => name == pattern.raw,
359        Some(suf) => {
360            name.starts_with(&pattern.prefix) && name.ends_with(suf.as_str()) && {
361                // Ensure the wildcard section isn't negative-length.
362                name.len() >= pattern.prefix.len() + suf.len()
363            }
364        }
365    }
366}
367
368// ── Tests ─────────────────────────────────────────────────────────────────────
369
370#[cfg(test)]
371mod tests {
372    #![allow(clippy::default_trait_access)]
373
374    use super::*;
375    use crate::def::ToolPolicy;
376
377    // ── FilteredToolExecutor tests ─────────────────────────────────────────
378
379    struct StubExecutor {
380        tools: Vec<&'static str>,
381    }
382
383    /// Stub executor that exposes tools with `InvocationHint::FencedBlock(tag)`.
384    struct StubFencedExecutor {
385        tag: &'static str,
386    }
387
388    impl ErasedToolExecutor for StubFencedExecutor {
389        fn execute_erased<'a>(
390            &'a self,
391            _response: &'a str,
392        ) -> Pin<
393            Box<
394                dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
395            >,
396        > {
397            Box::pin(std::future::ready(Ok(None)))
398        }
399
400        fn execute_confirmed_erased<'a>(
401            &'a self,
402            _response: &'a str,
403        ) -> Pin<
404            Box<
405                dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
406            >,
407        > {
408            Box::pin(std::future::ready(Ok(None)))
409        }
410
411        fn tool_definitions_erased(&self) -> Vec<ToolDef> {
412            use zeph_tools::registry::InvocationHint;
413            vec![ToolDef {
414                id: self.tag.into(),
415                description: "fenced stub".into(),
416                schema: schemars::Schema::default(),
417                invocation: InvocationHint::FencedBlock(self.tag),
418            }]
419        }
420
421        fn execute_tool_call_erased<'a>(
422            &'a self,
423            call: &'a ToolCall,
424        ) -> Pin<
425            Box<
426                dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
427            >,
428        > {
429            let result = Ok(Some(ToolOutput {
430                tool_name: call.tool_id.clone(),
431                summary: "ok".into(),
432                blocks_executed: 1,
433                filter_stats: None,
434                diff: None,
435                streamed: false,
436                terminal_id: None,
437                locations: None,
438                raw_response: None,
439                claim_source: None,
440            }));
441            Box::pin(std::future::ready(result))
442        }
443
444        fn is_tool_retryable_erased(&self, _tool_id: &str) -> bool {
445            false
446        }
447    }
448
449    fn fenced_stub_box(tag: &'static str) -> Arc<dyn ErasedToolExecutor> {
450        Arc::new(StubFencedExecutor { tag })
451    }
452
453    impl ErasedToolExecutor for StubExecutor {
454        fn execute_erased<'a>(
455            &'a self,
456            _response: &'a str,
457        ) -> Pin<
458            Box<
459                dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
460            >,
461        > {
462            Box::pin(std::future::ready(Ok(None)))
463        }
464
465        fn execute_confirmed_erased<'a>(
466            &'a self,
467            _response: &'a str,
468        ) -> Pin<
469            Box<
470                dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
471            >,
472        > {
473            Box::pin(std::future::ready(Ok(None)))
474        }
475
476        fn tool_definitions_erased(&self) -> Vec<ToolDef> {
477            // Return stub definitions for each tool name.
478            use zeph_tools::registry::InvocationHint;
479            self.tools
480                .iter()
481                .map(|id| ToolDef {
482                    id: (*id).into(),
483                    description: "stub".into(),
484                    schema: schemars::Schema::default(),
485                    invocation: InvocationHint::ToolCall,
486                })
487                .collect()
488        }
489
490        fn execute_tool_call_erased<'a>(
491            &'a self,
492            call: &'a ToolCall,
493        ) -> Pin<
494            Box<
495                dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
496            >,
497        > {
498            let result = Ok(Some(ToolOutput {
499                tool_name: call.tool_id.clone(),
500                summary: "ok".into(),
501                blocks_executed: 1,
502                filter_stats: None,
503                diff: None,
504                streamed: false,
505                terminal_id: None,
506                locations: None,
507                raw_response: None,
508                claim_source: None,
509            }));
510            Box::pin(std::future::ready(result))
511        }
512
513        fn is_tool_retryable_erased(&self, _tool_id: &str) -> bool {
514            false
515        }
516    }
517
518    fn stub_box(tools: &[&'static str]) -> Arc<dyn ErasedToolExecutor> {
519        Arc::new(StubExecutor {
520            tools: tools.to_vec(),
521        })
522    }
523
524    #[tokio::test]
525    async fn allow_list_permits_listed_tool() {
526        let exec = FilteredToolExecutor::new(
527            stub_box(&["shell", "web"]),
528            ToolPolicy::AllowList(vec!["shell".into()]),
529        );
530        let call = ToolCall {
531            tool_id: "shell".into(),
532            params: serde_json::Map::default(),
533            caller_id: None,
534        };
535        let res = exec.execute_tool_call_erased(&call).await.unwrap();
536        assert!(res.is_some());
537    }
538
539    #[tokio::test]
540    async fn allow_list_blocks_unlisted_tool() {
541        let exec = FilteredToolExecutor::new(
542            stub_box(&["shell", "web"]),
543            ToolPolicy::AllowList(vec!["shell".into()]),
544        );
545        let call = ToolCall {
546            tool_id: "web".into(),
547            params: serde_json::Map::default(),
548            caller_id: None,
549        };
550        let res = exec.execute_tool_call_erased(&call).await;
551        assert!(res.is_err());
552    }
553
554    #[tokio::test]
555    async fn deny_list_blocks_listed_tool() {
556        let exec = FilteredToolExecutor::new(
557            stub_box(&["shell", "web"]),
558            ToolPolicy::DenyList(vec!["shell".into()]),
559        );
560        let call = ToolCall {
561            tool_id: "shell".into(),
562            params: serde_json::Map::default(),
563            caller_id: None,
564        };
565        let res = exec.execute_tool_call_erased(&call).await;
566        assert!(res.is_err());
567    }
568
569    #[tokio::test]
570    async fn inherit_all_permits_any_tool() {
571        let exec = FilteredToolExecutor::new(stub_box(&["shell"]), ToolPolicy::InheritAll);
572        let call = ToolCall {
573            tool_id: "shell".into(),
574            params: serde_json::Map::default(),
575            caller_id: None,
576        };
577        let res = exec.execute_tool_call_erased(&call).await.unwrap();
578        assert!(res.is_some());
579    }
580
581    #[test]
582    fn tool_definitions_filtered_by_allow_list() {
583        let exec = FilteredToolExecutor::new(
584            stub_box(&["shell", "web"]),
585            ToolPolicy::AllowList(vec!["shell".into()]),
586        );
587        let defs = exec.tool_definitions_erased();
588        assert_eq!(defs.len(), 1);
589        assert_eq!(defs[0].id, "shell");
590    }
591
592    // ── glob_match tests ───────────────────────────────────────────────────
593
594    fn matches(pattern: &str, name: &str) -> bool {
595        let p = compile_glob(pattern).unwrap();
596        glob_match(&p, name)
597    }
598
599    #[test]
600    fn glob_star_matches_all() {
601        assert!(matches("*", "anything"));
602        assert!(matches("*", ""));
603    }
604
605    #[test]
606    fn glob_prefix_star() {
607        assert!(matches("git-*", "git-commit"));
608        assert!(matches("git-*", "git-status"));
609        assert!(!matches("git-*", "rust-fmt"));
610    }
611
612    #[test]
613    fn glob_literal_exact_match() {
614        assert!(matches("shell", "shell"));
615        assert!(!matches("shell", "shell-extra"));
616    }
617
618    #[test]
619    fn glob_star_suffix() {
620        assert!(matches("*-review", "code-review"));
621        assert!(!matches("*-review", "code-reviewer"));
622    }
623
624    #[test]
625    fn glob_double_star_is_error() {
626        assert!(compile_glob("**").is_err());
627    }
628
629    #[test]
630    fn glob_mid_string_wildcard() {
631        // "a*b" — prefix="a", suffix=Some("b")
632        assert!(matches("a*b", "axb"));
633        assert!(matches("a*b", "aXYZb"));
634        assert!(!matches("a*b", "ab-extra"));
635        assert!(!matches("a*b", "xab"));
636    }
637
638    // ── FilteredToolExecutor additional tests ──────────────────────────────
639
640    #[tokio::test]
641    async fn deny_list_permits_unlisted_tool() {
642        let exec = FilteredToolExecutor::new(
643            stub_box(&["shell", "web"]),
644            ToolPolicy::DenyList(vec!["shell".into()]),
645        );
646        let call = ToolCall {
647            tool_id: "web".into(), // not in deny list → allowed
648            params: serde_json::Map::default(),
649            caller_id: None,
650        };
651        let res = exec.execute_tool_call_erased(&call).await.unwrap();
652        assert!(res.is_some());
653    }
654
655    #[test]
656    fn tool_definitions_filtered_by_deny_list() {
657        let exec = FilteredToolExecutor::new(
658            stub_box(&["shell", "web"]),
659            ToolPolicy::DenyList(vec!["shell".into()]),
660        );
661        let defs = exec.tool_definitions_erased();
662        assert_eq!(defs.len(), 1);
663        assert_eq!(defs[0].id, "web");
664    }
665
666    #[test]
667    fn tool_definitions_inherit_all_returns_all() {
668        let exec = FilteredToolExecutor::new(stub_box(&["shell", "web"]), ToolPolicy::InheritAll);
669        let defs = exec.tool_definitions_erased();
670        assert_eq!(defs.len(), 2);
671    }
672
673    // ── fenced-block detection tests (fix for #1432) ──────────────────────
674
675    #[tokio::test]
676    async fn fenced_block_matching_tag_is_blocked() {
677        // Executor has a FencedBlock("bash") tool; response contains ```bash block.
678        let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
679        let res = exec.execute_erased("```bash\nls\n```").await;
680        assert!(
681            res.is_err(),
682            "actual fenced-block invocation must be blocked"
683        );
684    }
685
686    #[tokio::test]
687    async fn fenced_block_matching_tag_confirmed_is_blocked() {
688        let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
689        let res = exec.execute_confirmed_erased("```bash\nls\n```").await;
690        assert!(
691            res.is_err(),
692            "actual fenced-block invocation (confirmed) must be blocked"
693        );
694    }
695
696    #[tokio::test]
697    async fn no_fenced_tools_plain_text_returns_ok_none() {
698        // No fenced-block tools registered → plain text must return Ok(None).
699        let exec = FilteredToolExecutor::new(stub_box(&["shell"]), ToolPolicy::InheritAll);
700        let res = exec.execute_erased("This is a plain text response.").await;
701        assert!(
702            res.unwrap().is_none(),
703            "plain text must not be treated as a tool call"
704        );
705    }
706
707    #[tokio::test]
708    async fn markdown_non_tool_fence_returns_ok_none() {
709        // Response has a ```rust fence but no FencedBlock tool with tag "rust" is registered.
710        let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
711        let res = exec
712            .execute_erased("Here is some code:\n```rust\nfn main() {}\n```")
713            .await;
714        assert!(
715            res.unwrap().is_none(),
716            "non-tool code fence must not trigger blocking"
717        );
718    }
719
720    #[tokio::test]
721    async fn no_fenced_tools_plain_text_confirmed_returns_ok_none() {
722        let exec = FilteredToolExecutor::new(stub_box(&["shell"]), ToolPolicy::InheritAll);
723        let res = exec
724            .execute_confirmed_erased("Plain response without any fences.")
725            .await;
726        assert!(res.unwrap().is_none());
727    }
728
729    /// Regression test for #1432: fenced executor + plain text (no fences at all) must return
730    /// Ok(None) so the agent loop can break. Previously this returned Err(Blocked)
731    /// unconditionally, exhausting all sub-agent turns.
732    #[tokio::test]
733    async fn fenced_executor_plain_text_returns_ok_none() {
734        let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
735        let res = exec
736            .execute_erased("Here is my analysis of the code. No shell commands needed.")
737            .await;
738        assert!(
739            res.unwrap().is_none(),
740            "plain text with fenced executor must not be treated as a tool call"
741        );
742    }
743
744    /// Unclosed fence (no closing ```) must not trigger blocking — it is not an executable
745    /// tool invocation. Verified by debugger as an intentional false-negative.
746    #[tokio::test]
747    async fn unclosed_fenced_block_returns_ok_none() {
748        let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
749        let res = exec.execute_erased("```bash\nls -la\n").await;
750        assert!(
751            res.unwrap().is_none(),
752            "unclosed fenced block must not be treated as a tool invocation"
753        );
754    }
755
756    /// Multiple fenced blocks where one matches a registered tag — must block.
757    #[tokio::test]
758    async fn multiple_fences_one_matching_tag_is_blocked() {
759        let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
760        let response = "Here is an example:\n```python\nprint('hello')\n```\nAnd the fix:\n```bash\nrm -rf /tmp/old\n```";
761        let res = exec.execute_erased(response).await;
762        assert!(
763            res.is_err(),
764            "response containing a matching fenced block must be blocked"
765        );
766    }
767
768    // ── disallowed_tools (tools.except) tests ─────────────────────────────
769
770    #[tokio::test]
771    async fn disallowed_blocks_tool_from_allow_list() {
772        let exec = FilteredToolExecutor::with_disallowed(
773            stub_box(&["shell", "web"]),
774            ToolPolicy::AllowList(vec!["shell".into(), "web".into()]),
775            vec!["shell".into()],
776        );
777        let call = ToolCall {
778            tool_id: "shell".into(),
779            params: serde_json::Map::default(),
780            caller_id: None,
781        };
782        let res = exec.execute_tool_call_erased(&call).await;
783        assert!(
784            res.is_err(),
785            "disallowed tool must be blocked even if in allow list"
786        );
787    }
788
789    #[tokio::test]
790    async fn disallowed_allows_non_disallowed_tool() {
791        let exec = FilteredToolExecutor::with_disallowed(
792            stub_box(&["shell", "web"]),
793            ToolPolicy::AllowList(vec!["shell".into(), "web".into()]),
794            vec!["shell".into()],
795        );
796        let call = ToolCall {
797            tool_id: "web".into(),
798            params: serde_json::Map::default(),
799            caller_id: None,
800        };
801        let res = exec.execute_tool_call_erased(&call).await;
802        assert!(res.is_ok(), "non-disallowed tool must be allowed");
803    }
804
805    #[test]
806    fn disallowed_empty_list_no_change() {
807        let exec = FilteredToolExecutor::with_disallowed(
808            stub_box(&["shell", "web"]),
809            ToolPolicy::InheritAll,
810            vec![],
811        );
812        let defs = exec.tool_definitions_erased();
813        assert_eq!(defs.len(), 2);
814    }
815
816    #[test]
817    fn tool_definitions_filters_disallowed_tools() {
818        let exec = FilteredToolExecutor::with_disallowed(
819            stub_box(&["shell", "web", "dangerous"]),
820            ToolPolicy::InheritAll,
821            vec!["dangerous".into()],
822        );
823        let defs = exec.tool_definitions_erased();
824        assert_eq!(defs.len(), 2);
825        assert!(!defs.iter().any(|d| d.id == "dangerous"));
826    }
827
828    // ── #1184: PlanModeExecutor + disallowed_tools catalog test ───────────
829
830    #[test]
831    fn plan_mode_with_disallowed_excludes_from_catalog() {
832        // FilteredToolExecutor wrapping PlanModeExecutor must exclude disallowed tools from
833        // tool_definitions_erased(), verifying that deny-list is enforced in plan mode catalog.
834        let inner = Arc::new(PlanModeExecutor::new(stub_box(&["shell", "web"])));
835        let exec = FilteredToolExecutor::with_disallowed(
836            inner,
837            ToolPolicy::InheritAll,
838            vec!["shell".into()],
839        );
840        let defs = exec.tool_definitions_erased();
841        assert!(
842            !defs.iter().any(|d| d.id == "shell"),
843            "shell must be excluded from catalog"
844        );
845        assert!(
846            defs.iter().any(|d| d.id == "web"),
847            "web must remain in catalog"
848        );
849    }
850
851    // ── PlanModeExecutor tests ─────────────────────────────────────────────
852
853    #[tokio::test]
854    async fn plan_mode_blocks_execute_erased() {
855        let exec = PlanModeExecutor::new(stub_box(&["shell"]));
856        let res = exec.execute_erased("response").await;
857        assert!(res.is_err());
858    }
859
860    #[tokio::test]
861    async fn plan_mode_blocks_execute_confirmed_erased() {
862        let exec = PlanModeExecutor::new(stub_box(&["shell"]));
863        let res = exec.execute_confirmed_erased("response").await;
864        assert!(res.is_err());
865    }
866
867    #[tokio::test]
868    async fn plan_mode_blocks_tool_call() {
869        let exec = PlanModeExecutor::new(stub_box(&["shell"]));
870        let call = ToolCall {
871            tool_id: "shell".into(),
872            params: serde_json::Map::default(),
873            caller_id: None,
874        };
875        let res = exec.execute_tool_call_erased(&call).await;
876        assert!(res.is_err(), "plan mode must block all tool execution");
877    }
878
879    #[test]
880    fn plan_mode_exposes_real_tool_definitions() {
881        let exec = PlanModeExecutor::new(stub_box(&["shell", "web"]));
882        let defs = exec.tool_definitions_erased();
883        // Real tool catalog exposed — LLM can reference tools in its plan.
884        assert_eq!(defs.len(), 2);
885        assert!(defs.iter().any(|d| d.id == "shell"));
886        assert!(defs.iter().any(|d| d.id == "web"));
887    }
888
889    // ── filter_skills tests ────────────────────────────────────────────────
890
891    #[test]
892    fn filter_skills_empty_registry_returns_empty() {
893        let registry = zeph_skills::registry::SkillRegistry::load(&[] as &[&str]);
894        let filter = SkillFilter::default();
895        let result = filter_skills(&registry, &filter).unwrap();
896        assert!(result.is_empty());
897    }
898
899    #[test]
900    fn filter_skills_empty_include_passes_all() {
901        // Empty include list means "include everything".
902        // With an empty registry, result is still empty — logic is correct.
903        let registry = zeph_skills::registry::SkillRegistry::load(&[] as &[&str]);
904        let filter = SkillFilter {
905            include: vec![],
906            exclude: vec![],
907        };
908        let result = filter_skills(&registry, &filter).unwrap();
909        assert!(result.is_empty());
910    }
911
912    #[test]
913    fn filter_skills_double_star_pattern_is_error() {
914        let registry = zeph_skills::registry::SkillRegistry::load(&[] as &[&str]);
915        let filter = SkillFilter {
916            include: vec!["**".into()],
917            exclude: vec![],
918        };
919        let err = filter_skills(&registry, &filter).unwrap_err();
920        assert!(matches!(err, SubAgentError::Invalid(_)));
921    }
922
923    mod proptest_glob {
924        use proptest::prelude::*;
925
926        use super::{compile_glob, glob_match};
927
928        proptest! {
929            #![proptest_config(proptest::test_runner::Config::with_cases(500))]
930
931            /// glob_match must never panic for any valid (non-**) pattern and any name string.
932            #[test]
933            fn glob_match_never_panics(
934                pattern in "[a-z*-]{1,10}",
935                name in "[a-z-]{0,15}",
936            ) {
937                // Skip patterns with ** (those are compile errors by design).
938                if !pattern.contains("**")
939                    && let Ok(p) = compile_glob(&pattern)
940                {
941                    let _ = glob_match(&p, &name);
942                }
943            }
944
945            /// A literal pattern (no `*`) must match only exact strings.
946            #[test]
947            fn glob_literal_matches_only_exact(
948                name in "[a-z-]{1,10}",
949            ) {
950                // A literal pattern equal to `name` must match.
951                let p = compile_glob(&name).unwrap();
952                prop_assert!(glob_match(&p, &name));
953
954                // A different name must not match.
955                let other = format!("{name}-x");
956                prop_assert!(!glob_match(&p, &other));
957            }
958
959            /// The `*` pattern must match every input.
960            #[test]
961            fn glob_star_matches_everything(name in ".*") {
962                let p = compile_glob("*").unwrap();
963                prop_assert!(glob_match(&p, &name));
964            }
965        }
966    }
967}