1use 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
27fn 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
41pub struct FilteredToolExecutor {
51 inner: Arc<dyn ErasedToolExecutor>,
52 policy: ToolPolicy,
53 disallowed: Vec<String>,
54 fenced_tags: Vec<&'static str>,
57}
58
59impl FilteredToolExecutor {
60 #[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 #[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 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 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 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 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 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 fn requires_confirmation_erased(&self, call: &ToolCall) -> bool {
194 self.inner.requires_confirmation_erased(call)
195 }
196}
197
198pub struct PlanModeExecutor {
207 inner: Arc<dyn ErasedToolExecutor>,
208}
209
210impl PlanModeExecutor {
211 #[must_use]
213 pub fn new(inner: Arc<dyn ErasedToolExecutor>) -> Self {
214 Self { inner }
215 }
216}
217
218impl ErasedToolExecutor for PlanModeExecutor {
219 fn execute_erased<'a>(
220 &'a self,
221 _response: &'a str,
222 ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
223 {
224 Box::pin(std::future::ready(Err(ToolError::Blocked {
225 command: "plan_mode".into(),
226 })))
227 }
228
229 fn execute_confirmed_erased<'a>(
230 &'a self,
231 _response: &'a str,
232 ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
233 {
234 Box::pin(std::future::ready(Err(ToolError::Blocked {
235 command: "plan_mode".into(),
236 })))
237 }
238
239 fn tool_definitions_erased(&self) -> Vec<ToolDef> {
240 self.inner.tool_definitions_erased()
241 }
242
243 fn execute_tool_call_erased<'a>(
244 &'a self,
245 call: &'a ToolCall,
246 ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a>>
247 {
248 tracing::debug!(
249 tool_id = %call.tool_id,
250 "tool execution blocked in plan mode"
251 );
252 Box::pin(std::future::ready(Err(ToolError::Blocked {
253 command: call.tool_id.to_string(),
254 })))
255 }
256
257 fn set_skill_env(&self, env: Option<std::collections::HashMap<String, String>>) {
258 self.inner.set_skill_env(env);
259 }
260
261 fn is_tool_retryable_erased(&self, _tool_id: &str) -> bool {
262 false
263 }
264
265 fn requires_confirmation_erased(&self, _call: &ToolCall) -> bool {
266 false
267 }
268}
269
270pub fn filter_skills(
299 registry: &SkillRegistry,
300 filter: &SkillFilter,
301) -> Result<Vec<Skill>, SubAgentError> {
302 let compiled_include = compile_globs(&filter.include)?;
303 let compiled_exclude = compile_globs(&filter.exclude)?;
304
305 let all: Vec<Skill> = registry
306 .all_meta()
307 .into_iter()
308 .filter(|meta| {
309 let name = &meta.name;
310 let included =
311 compiled_include.is_empty() || compiled_include.iter().any(|p| glob_match(p, name));
312 let excluded = compiled_exclude.iter().any(|p| glob_match(p, name));
313 included && !excluded
314 })
315 .filter_map(|meta| registry.skill(&meta.name).ok())
316 .collect();
317
318 Ok(all)
319}
320
321struct GlobPattern {
323 raw: String,
324 prefix: String,
325 suffix: Option<String>,
326 is_star: bool,
327}
328
329fn compile_globs(patterns: &[String]) -> Result<Vec<GlobPattern>, SubAgentError> {
330 patterns.iter().map(|p| compile_glob(p)).collect()
331}
332
333fn compile_glob(pattern: &str) -> Result<GlobPattern, SubAgentError> {
334 if pattern.contains("**") {
337 return Err(SubAgentError::Invalid(format!(
338 "glob pattern '{pattern}' uses '**' which is not supported"
339 )));
340 }
341
342 let is_star = pattern == "*";
343
344 let (prefix, suffix) = if let Some(pos) = pattern.find('*') {
345 let before = pattern[..pos].to_owned();
346 let after = pattern[pos + 1..].to_owned();
347 (before, Some(after))
348 } else {
349 (pattern.to_owned(), None)
350 };
351
352 Ok(GlobPattern {
353 raw: pattern.to_owned(),
354 prefix,
355 suffix,
356 is_star,
357 })
358}
359
360fn glob_match(pattern: &GlobPattern, name: &str) -> bool {
361 if pattern.is_star {
362 return true;
363 }
364
365 match &pattern.suffix {
366 None => name == pattern.raw,
367 Some(suf) => {
368 name.starts_with(&pattern.prefix) && name.ends_with(suf.as_str()) && {
369 name.len() >= pattern.prefix.len() + suf.len()
371 }
372 }
373 }
374}
375
376#[cfg(test)]
379mod tests {
380 #![allow(clippy::default_trait_access)]
381
382 use super::*;
383 use crate::def::ToolPolicy;
384
385 struct StubExecutor {
388 tools: Vec<&'static str>,
389 }
390
391 struct StubFencedExecutor {
393 tag: &'static str,
394 }
395
396 impl ErasedToolExecutor for StubFencedExecutor {
397 fn execute_erased<'a>(
398 &'a self,
399 _response: &'a str,
400 ) -> Pin<
401 Box<
402 dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
403 >,
404 > {
405 Box::pin(std::future::ready(Ok(None)))
406 }
407
408 fn execute_confirmed_erased<'a>(
409 &'a self,
410 _response: &'a str,
411 ) -> Pin<
412 Box<
413 dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
414 >,
415 > {
416 Box::pin(std::future::ready(Ok(None)))
417 }
418
419 fn tool_definitions_erased(&self) -> Vec<ToolDef> {
420 use zeph_tools::registry::InvocationHint;
421 vec![ToolDef {
422 id: self.tag.into(),
423 description: "fenced stub".into(),
424 schema: schemars::Schema::default(),
425 invocation: InvocationHint::FencedBlock(self.tag),
426 output_schema: None,
427 }]
428 }
429
430 fn execute_tool_call_erased<'a>(
431 &'a self,
432 call: &'a ToolCall,
433 ) -> Pin<
434 Box<
435 dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
436 >,
437 > {
438 let result = Ok(Some(ToolOutput {
439 tool_name: call.tool_id.clone(),
440 summary: "ok".into(),
441 blocks_executed: 1,
442 filter_stats: None,
443 diff: None,
444 streamed: false,
445 terminal_id: None,
446 locations: None,
447 raw_response: None,
448 claim_source: None,
449 }));
450 Box::pin(std::future::ready(result))
451 }
452
453 fn is_tool_retryable_erased(&self, _tool_id: &str) -> bool {
454 false
455 }
456
457 fn requires_confirmation_erased(&self, _call: &ToolCall) -> bool {
458 false
459 }
460 }
461
462 fn fenced_stub_box(tag: &'static str) -> Arc<dyn ErasedToolExecutor> {
463 Arc::new(StubFencedExecutor { tag })
464 }
465
466 impl ErasedToolExecutor for StubExecutor {
467 fn execute_erased<'a>(
468 &'a self,
469 _response: &'a str,
470 ) -> Pin<
471 Box<
472 dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
473 >,
474 > {
475 Box::pin(std::future::ready(Ok(None)))
476 }
477
478 fn execute_confirmed_erased<'a>(
479 &'a self,
480 _response: &'a str,
481 ) -> Pin<
482 Box<
483 dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
484 >,
485 > {
486 Box::pin(std::future::ready(Ok(None)))
487 }
488
489 fn tool_definitions_erased(&self) -> Vec<ToolDef> {
490 use zeph_tools::registry::InvocationHint;
492 self.tools
493 .iter()
494 .map(|id| ToolDef {
495 id: (*id).into(),
496 description: "stub".into(),
497 schema: schemars::Schema::default(),
498 invocation: InvocationHint::ToolCall,
499 output_schema: None,
500 })
501 .collect()
502 }
503
504 fn execute_tool_call_erased<'a>(
505 &'a self,
506 call: &'a ToolCall,
507 ) -> Pin<
508 Box<
509 dyn std::future::Future<Output = Result<Option<ToolOutput>, ToolError>> + Send + 'a,
510 >,
511 > {
512 let result = Ok(Some(ToolOutput {
513 tool_name: call.tool_id.clone(),
514 summary: "ok".into(),
515 blocks_executed: 1,
516 filter_stats: None,
517 diff: None,
518 streamed: false,
519 terminal_id: None,
520 locations: None,
521 raw_response: None,
522 claim_source: None,
523 }));
524 Box::pin(std::future::ready(result))
525 }
526
527 fn is_tool_retryable_erased(&self, _tool_id: &str) -> bool {
528 false
529 }
530
531 fn requires_confirmation_erased(&self, _call: &ToolCall) -> bool {
532 false
533 }
534 }
535
536 fn stub_box(tools: &[&'static str]) -> Arc<dyn ErasedToolExecutor> {
537 Arc::new(StubExecutor {
538 tools: tools.to_vec(),
539 })
540 }
541
542 #[tokio::test]
543 async fn allow_list_permits_listed_tool() {
544 let exec = FilteredToolExecutor::new(
545 stub_box(&["shell", "web"]),
546 ToolPolicy::AllowList(vec!["shell".into()]),
547 );
548 let call = ToolCall {
549 tool_id: "shell".into(),
550 params: serde_json::Map::default(),
551 caller_id: None,
552 context: None,
553 };
554 let res = exec.execute_tool_call_erased(&call).await.unwrap();
555 assert!(res.is_some());
556 }
557
558 #[tokio::test]
559 async fn allow_list_blocks_unlisted_tool() {
560 let exec = FilteredToolExecutor::new(
561 stub_box(&["shell", "web"]),
562 ToolPolicy::AllowList(vec!["shell".into()]),
563 );
564 let call = ToolCall {
565 tool_id: "web".into(),
566 params: serde_json::Map::default(),
567 caller_id: None,
568 context: None,
569 };
570 let res = exec.execute_tool_call_erased(&call).await;
571 assert!(res.is_err());
572 }
573
574 #[tokio::test]
575 async fn deny_list_blocks_listed_tool() {
576 let exec = FilteredToolExecutor::new(
577 stub_box(&["shell", "web"]),
578 ToolPolicy::DenyList(vec!["shell".into()]),
579 );
580 let call = ToolCall {
581 tool_id: "shell".into(),
582 params: serde_json::Map::default(),
583 caller_id: None,
584 context: None,
585 };
586 let res = exec.execute_tool_call_erased(&call).await;
587 assert!(res.is_err());
588 }
589
590 #[tokio::test]
591 async fn inherit_all_permits_any_tool() {
592 let exec = FilteredToolExecutor::new(stub_box(&["shell"]), ToolPolicy::InheritAll);
593 let call = ToolCall {
594 tool_id: "shell".into(),
595 params: serde_json::Map::default(),
596 caller_id: None,
597 context: None,
598 };
599 let res = exec.execute_tool_call_erased(&call).await.unwrap();
600 assert!(res.is_some());
601 }
602
603 #[test]
604 fn tool_definitions_filtered_by_allow_list() {
605 let exec = FilteredToolExecutor::new(
606 stub_box(&["shell", "web"]),
607 ToolPolicy::AllowList(vec!["shell".into()]),
608 );
609 let defs = exec.tool_definitions_erased();
610 assert_eq!(defs.len(), 1);
611 assert_eq!(defs[0].id, "shell");
612 }
613
614 fn matches(pattern: &str, name: &str) -> bool {
617 let p = compile_glob(pattern).unwrap();
618 glob_match(&p, name)
619 }
620
621 #[test]
622 fn glob_star_matches_all() {
623 assert!(matches("*", "anything"));
624 assert!(matches("*", ""));
625 }
626
627 #[test]
628 fn glob_prefix_star() {
629 assert!(matches("git-*", "git-commit"));
630 assert!(matches("git-*", "git-status"));
631 assert!(!matches("git-*", "rust-fmt"));
632 }
633
634 #[test]
635 fn glob_literal_exact_match() {
636 assert!(matches("shell", "shell"));
637 assert!(!matches("shell", "shell-extra"));
638 }
639
640 #[test]
641 fn glob_star_suffix() {
642 assert!(matches("*-review", "code-review"));
643 assert!(!matches("*-review", "code-reviewer"));
644 }
645
646 #[test]
647 fn glob_double_star_is_error() {
648 assert!(compile_glob("**").is_err());
649 }
650
651 #[test]
652 fn glob_mid_string_wildcard() {
653 assert!(matches("a*b", "axb"));
655 assert!(matches("a*b", "aXYZb"));
656 assert!(!matches("a*b", "ab-extra"));
657 assert!(!matches("a*b", "xab"));
658 }
659
660 #[tokio::test]
663 async fn deny_list_permits_unlisted_tool() {
664 let exec = FilteredToolExecutor::new(
665 stub_box(&["shell", "web"]),
666 ToolPolicy::DenyList(vec!["shell".into()]),
667 );
668 let call = ToolCall {
669 tool_id: "web".into(), params: serde_json::Map::default(),
671 caller_id: None,
672 context: None,
673 };
674 let res = exec.execute_tool_call_erased(&call).await.unwrap();
675 assert!(res.is_some());
676 }
677
678 #[test]
679 fn tool_definitions_filtered_by_deny_list() {
680 let exec = FilteredToolExecutor::new(
681 stub_box(&["shell", "web"]),
682 ToolPolicy::DenyList(vec!["shell".into()]),
683 );
684 let defs = exec.tool_definitions_erased();
685 assert_eq!(defs.len(), 1);
686 assert_eq!(defs[0].id, "web");
687 }
688
689 #[test]
690 fn tool_definitions_inherit_all_returns_all() {
691 let exec = FilteredToolExecutor::new(stub_box(&["shell", "web"]), ToolPolicy::InheritAll);
692 let defs = exec.tool_definitions_erased();
693 assert_eq!(defs.len(), 2);
694 }
695
696 #[tokio::test]
699 async fn fenced_block_matching_tag_is_blocked() {
700 let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
702 let res = exec.execute_erased("```bash\nls\n```").await;
703 assert!(
704 res.is_err(),
705 "actual fenced-block invocation must be blocked"
706 );
707 }
708
709 #[tokio::test]
710 async fn fenced_block_matching_tag_confirmed_is_blocked() {
711 let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
712 let res = exec.execute_confirmed_erased("```bash\nls\n```").await;
713 assert!(
714 res.is_err(),
715 "actual fenced-block invocation (confirmed) must be blocked"
716 );
717 }
718
719 #[tokio::test]
720 async fn no_fenced_tools_plain_text_returns_ok_none() {
721 let exec = FilteredToolExecutor::new(stub_box(&["shell"]), ToolPolicy::InheritAll);
723 let res = exec.execute_erased("This is a plain text response.").await;
724 assert!(
725 res.unwrap().is_none(),
726 "plain text must not be treated as a tool call"
727 );
728 }
729
730 #[tokio::test]
731 async fn markdown_non_tool_fence_returns_ok_none() {
732 let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
734 let res = exec
735 .execute_erased("Here is some code:\n```rust\nfn main() {}\n```")
736 .await;
737 assert!(
738 res.unwrap().is_none(),
739 "non-tool code fence must not trigger blocking"
740 );
741 }
742
743 #[tokio::test]
744 async fn no_fenced_tools_plain_text_confirmed_returns_ok_none() {
745 let exec = FilteredToolExecutor::new(stub_box(&["shell"]), ToolPolicy::InheritAll);
746 let res = exec
747 .execute_confirmed_erased("Plain response without any fences.")
748 .await;
749 assert!(res.unwrap().is_none());
750 }
751
752 #[tokio::test]
756 async fn fenced_executor_plain_text_returns_ok_none() {
757 let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
758 let res = exec
759 .execute_erased("Here is my analysis of the code. No shell commands needed.")
760 .await;
761 assert!(
762 res.unwrap().is_none(),
763 "plain text with fenced executor must not be treated as a tool call"
764 );
765 }
766
767 #[tokio::test]
770 async fn unclosed_fenced_block_returns_ok_none() {
771 let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
772 let res = exec.execute_erased("```bash\nls -la\n").await;
773 assert!(
774 res.unwrap().is_none(),
775 "unclosed fenced block must not be treated as a tool invocation"
776 );
777 }
778
779 #[tokio::test]
781 async fn multiple_fences_one_matching_tag_is_blocked() {
782 let exec = FilteredToolExecutor::new(fenced_stub_box("bash"), ToolPolicy::InheritAll);
783 let response = "Here is an example:\n```python\nprint('hello')\n```\nAnd the fix:\n```bash\nrm -rf /tmp/old\n```";
784 let res = exec.execute_erased(response).await;
785 assert!(
786 res.is_err(),
787 "response containing a matching fenced block must be blocked"
788 );
789 }
790
791 #[tokio::test]
794 async fn disallowed_blocks_tool_from_allow_list() {
795 let exec = FilteredToolExecutor::with_disallowed(
796 stub_box(&["shell", "web"]),
797 ToolPolicy::AllowList(vec!["shell".into(), "web".into()]),
798 vec!["shell".into()],
799 );
800 let call = ToolCall {
801 tool_id: "shell".into(),
802 params: serde_json::Map::default(),
803 caller_id: None,
804 context: None,
805 };
806 let res = exec.execute_tool_call_erased(&call).await;
807 assert!(
808 res.is_err(),
809 "disallowed tool must be blocked even if in allow list"
810 );
811 }
812
813 #[tokio::test]
814 async fn disallowed_allows_non_disallowed_tool() {
815 let exec = FilteredToolExecutor::with_disallowed(
816 stub_box(&["shell", "web"]),
817 ToolPolicy::AllowList(vec!["shell".into(), "web".into()]),
818 vec!["shell".into()],
819 );
820 let call = ToolCall {
821 tool_id: "web".into(),
822 params: serde_json::Map::default(),
823 caller_id: None,
824 context: None,
825 };
826 let res = exec.execute_tool_call_erased(&call).await;
827 assert!(res.is_ok(), "non-disallowed tool must be allowed");
828 }
829
830 #[test]
831 fn disallowed_empty_list_no_change() {
832 let exec = FilteredToolExecutor::with_disallowed(
833 stub_box(&["shell", "web"]),
834 ToolPolicy::InheritAll,
835 vec![],
836 );
837 let defs = exec.tool_definitions_erased();
838 assert_eq!(defs.len(), 2);
839 }
840
841 #[test]
842 fn tool_definitions_filters_disallowed_tools() {
843 let exec = FilteredToolExecutor::with_disallowed(
844 stub_box(&["shell", "web", "dangerous"]),
845 ToolPolicy::InheritAll,
846 vec!["dangerous".into()],
847 );
848 let defs = exec.tool_definitions_erased();
849 assert_eq!(defs.len(), 2);
850 assert!(!defs.iter().any(|d| d.id == "dangerous"));
851 }
852
853 #[test]
856 fn plan_mode_with_disallowed_excludes_from_catalog() {
857 let inner = Arc::new(PlanModeExecutor::new(stub_box(&["shell", "web"])));
860 let exec = FilteredToolExecutor::with_disallowed(
861 inner,
862 ToolPolicy::InheritAll,
863 vec!["shell".into()],
864 );
865 let defs = exec.tool_definitions_erased();
866 assert!(
867 !defs.iter().any(|d| d.id == "shell"),
868 "shell must be excluded from catalog"
869 );
870 assert!(
871 defs.iter().any(|d| d.id == "web"),
872 "web must remain in catalog"
873 );
874 }
875
876 #[tokio::test]
879 async fn plan_mode_blocks_execute_erased() {
880 let exec = PlanModeExecutor::new(stub_box(&["shell"]));
881 let res = exec.execute_erased("response").await;
882 assert!(res.is_err());
883 }
884
885 #[tokio::test]
886 async fn plan_mode_blocks_execute_confirmed_erased() {
887 let exec = PlanModeExecutor::new(stub_box(&["shell"]));
888 let res = exec.execute_confirmed_erased("response").await;
889 assert!(res.is_err());
890 }
891
892 #[tokio::test]
893 async fn plan_mode_blocks_tool_call() {
894 let exec = PlanModeExecutor::new(stub_box(&["shell"]));
895 let call = ToolCall {
896 tool_id: "shell".into(),
897 params: serde_json::Map::default(),
898 caller_id: None,
899 context: None,
900 };
901 let res = exec.execute_tool_call_erased(&call).await;
902 assert!(res.is_err(), "plan mode must block all tool execution");
903 }
904
905 #[test]
906 fn plan_mode_exposes_real_tool_definitions() {
907 let exec = PlanModeExecutor::new(stub_box(&["shell", "web"]));
908 let defs = exec.tool_definitions_erased();
909 assert_eq!(defs.len(), 2);
911 assert!(defs.iter().any(|d| d.id == "shell"));
912 assert!(defs.iter().any(|d| d.id == "web"));
913 }
914
915 #[test]
918 fn filter_skills_empty_registry_returns_empty() {
919 let registry = zeph_skills::registry::SkillRegistry::load(&[] as &[&str]);
920 let filter = SkillFilter::default();
921 let result = filter_skills(®istry, &filter).unwrap();
922 assert!(result.is_empty());
923 }
924
925 #[test]
926 fn filter_skills_empty_include_passes_all() {
927 let registry = zeph_skills::registry::SkillRegistry::load(&[] as &[&str]);
930 let filter = SkillFilter {
931 include: vec![],
932 exclude: vec![],
933 };
934 let result = filter_skills(®istry, &filter).unwrap();
935 assert!(result.is_empty());
936 }
937
938 #[test]
939 fn filter_skills_double_star_pattern_is_error() {
940 let registry = zeph_skills::registry::SkillRegistry::load(&[] as &[&str]);
941 let filter = SkillFilter {
942 include: vec!["**".into()],
943 exclude: vec![],
944 };
945 let err = filter_skills(®istry, &filter).unwrap_err();
946 assert!(matches!(err, SubAgentError::Invalid(_)));
947 }
948
949 mod proptest_glob {
950 use proptest::prelude::*;
951
952 use super::{compile_glob, glob_match};
953
954 proptest! {
955 #![proptest_config(proptest::test_runner::Config::with_cases(500))]
956
957 #[test]
959 fn glob_match_never_panics(
960 pattern in "[a-z*-]{1,10}",
961 name in "[a-z-]{0,15}",
962 ) {
963 if !pattern.contains("**")
965 && let Ok(p) = compile_glob(&pattern)
966 {
967 let _ = glob_match(&p, &name);
968 }
969 }
970
971 #[test]
973 fn glob_literal_matches_only_exact(
974 name in "[a-z-]{1,10}",
975 ) {
976 let p = compile_glob(&name).unwrap();
978 prop_assert!(glob_match(&p, &name));
979
980 let other = format!("{name}-x");
982 prop_assert!(!glob_match(&p, &other));
983 }
984
985 #[test]
987 fn glob_star_matches_everything(name in ".*") {
988 let p = compile_glob("*").unwrap();
989 prop_assert!(glob_match(&p, &name));
990 }
991 }
992 }
993}