1use std::collections::{HashMap, HashSet};
49use std::sync::Arc;
50
51use arc_swap::ArcSwap;
52use globset::{Glob, GlobSet, GlobSetBuilder};
53use tracing::warn;
54
55use crate::audit::{AuditEntry, AuditLogger, AuditResult, chrono_now};
56use crate::executor::{ToolCall, ToolError, ToolExecutor, ToolOutput};
57use crate::registry::ToolDef;
58use zeph_config::{CapabilityScopesConfig, PatternStrictness};
59
60#[non_exhaustive]
63#[derive(Debug, thiserror::Error)]
65pub enum ScopeError {
66 #[error("scope '{scope}': pattern '{pattern}' matched zero registered tools (dead pattern)")]
68 DeadPattern { scope: String, pattern: String },
69
70 #[error(
72 "scope '{scope}': pattern '{pattern}' matches the entire registry; use default_scope=\"general\" to opt in"
73 )]
74 AccidentallyFull { scope: String, pattern: String },
75
76 #[error("tool id '{id}' has no namespace prefix (expected '<namespace>:<id>')")]
78 UnqualifiedId { id: String },
79
80 #[error("scope '{scope}': invalid glob pattern '{pattern}': {source}")]
82 InvalidPattern {
83 scope: String,
84 pattern: String,
85 #[source]
86 source: globset::Error,
87 },
88}
89
90#[derive(Debug)]
92pub struct ScopeWarning {
93 pub scope: String,
95 pub pattern: String,
97}
98
99#[derive(Debug, Clone)]
106pub struct ToolScope {
107 pub task_type: Option<String>,
109 admitted: HashSet<String>,
111 is_full: bool,
113 patterns: Vec<String>,
115}
116
117impl ToolScope {
118 #[must_use]
130 pub fn full() -> Self {
131 Self {
132 task_type: None,
133 admitted: HashSet::new(),
134 is_full: true,
135 patterns: vec!["*".to_owned()],
136 }
137 }
138
139 pub fn try_compile<S: std::hash::BuildHasher>(
147 task_type: impl Into<String>,
148 patterns: &[String],
149 registry_ids: &HashSet<String, S>,
150 strictness: PatternStrictness,
151 is_general_scope: bool,
152 ) -> Result<(Self, Vec<ScopeWarning>), ScopeError> {
153 let task_type_str = task_type.into();
154 let mut admitted = HashSet::new();
155 let mut warnings = Vec::new();
156
157 for pattern in patterns {
158 let glob = Glob::new(pattern).map_err(|e| ScopeError::InvalidPattern {
160 scope: task_type_str.clone(),
161 pattern: pattern.clone(),
162 source: e,
163 })?;
164
165 let mut builder = GlobSetBuilder::new();
166 builder.add(glob);
167 let glob_set: GlobSet = builder.build().map_err(|e| ScopeError::InvalidPattern {
168 scope: task_type_str.clone(),
169 pattern: pattern.clone(),
170 source: e,
171 })?;
172
173 let matched: HashSet<String> = registry_ids
174 .iter()
175 .filter(|id| glob_set.is_match(id.as_str()))
176 .cloned()
177 .collect();
178
179 if !is_general_scope && matched.len() == registry_ids.len() && !registry_ids.is_empty()
181 {
182 return Err(ScopeError::AccidentallyFull {
183 scope: task_type_str,
184 pattern: pattern.clone(),
185 });
186 }
187
188 if matched.is_empty() {
189 let is_strict = is_strict_pattern(pattern, strictness);
190 if is_strict {
191 return Err(ScopeError::DeadPattern {
192 scope: task_type_str,
193 pattern: pattern.clone(),
194 });
195 }
196 warnings.push(ScopeWarning {
197 scope: task_type_str.clone(),
198 pattern: pattern.clone(),
199 });
200 }
201
202 admitted.extend(matched);
203 }
204
205 Ok((
206 Self {
207 task_type: Some(task_type_str),
208 admitted,
209 is_full: false,
210 patterns: patterns.to_vec(),
211 },
212 warnings,
213 ))
214 }
215
216 #[must_use]
227 pub fn admits(&self, qualified_tool_id: &str) -> bool {
228 self.is_full || self.admitted.contains(qualified_tool_id)
229 }
230
231 #[must_use]
235 pub fn admitted_ids(&self) -> Vec<&str> {
236 self.admitted.iter().map(String::as_str).collect()
237 }
238
239 #[must_use]
241 pub fn patterns(&self) -> &[String] {
242 &self.patterns
243 }
244
245 #[must_use]
250 pub fn re_resolve<S: std::hash::BuildHasher>(&self, registry_ids: &HashSet<String, S>) -> Self {
251 let task_type_str = self
252 .task_type
253 .clone()
254 .unwrap_or_else(|| "<unknown>".to_owned());
255 let mut admitted = HashSet::new();
256 for pattern in &self.patterns {
257 let Ok(glob) = Glob::new(pattern) else {
258 warn!(scope = %task_type_str, pattern, "re-resolve: invalid glob, skipping");
259 continue;
260 };
261 let mut builder = GlobSetBuilder::new();
262 builder.add(glob);
263 let Ok(glob_set) = builder.build() else {
264 continue;
265 };
266 let matched: HashSet<String> = registry_ids
267 .iter()
268 .filter(|id| glob_set.is_match(id.as_str()))
269 .cloned()
270 .collect();
271 admitted.extend(matched);
272 }
273 Self {
274 task_type: self.task_type.clone(),
275 admitted,
276 is_full: false,
277 patterns: self.patterns.clone(),
278 }
279 }
280}
281
282fn is_strict_pattern(pattern: &str, strictness: PatternStrictness) -> bool {
284 match strictness {
285 PatternStrictness::Strict => true,
286 PatternStrictness::ProvisionalForDynamicNamespaces => {
287 pattern.starts_with("builtin:") || pattern.starts_with("skill:")
289 }
290 _ => false,
291 }
292}
293
294pub struct ScopedToolExecutor<E: ToolExecutor> {
321 inner: E,
322 scope: ArcSwap<ToolScope>,
324 scopes: HashMap<String, Arc<ToolScope>>,
326 scope_at_definition: parking_lot::Mutex<Option<String>>,
328 signal_queue: Option<crate::policy_gate::RiskSignalQueue>,
330 audit: Option<Arc<AuditLogger>>,
332}
333
334impl<E: ToolExecutor> ScopedToolExecutor<E> {
335 #[must_use]
349 pub fn new(inner: E, initial_scope: ToolScope) -> Self {
350 Self {
351 inner,
352 scope: ArcSwap::from_pointee(initial_scope),
353 scopes: HashMap::new(),
354 scope_at_definition: parking_lot::Mutex::new(None),
355 signal_queue: None,
356 audit: None,
357 }
358 }
359
360 #[must_use]
362 pub fn with_audit(mut self, audit: Arc<AuditLogger>) -> Self {
363 self.audit = Some(audit);
364 self
365 }
366
367 #[must_use]
369 pub fn with_signal_queue(mut self, queue: crate::policy_gate::RiskSignalQueue) -> Self {
370 self.signal_queue = Some(queue);
371 self
372 }
373
374 pub fn register_scope(&mut self, name: impl Into<String>, scope: ToolScope) {
376 self.scopes.insert(name.into(), Arc::new(scope));
377 }
378
379 pub fn set_scope_for_task(&self, task_type: &str) -> bool {
381 if let Some(scope) = self.scopes.get(task_type) {
382 self.scope.store(Arc::clone(scope));
383 true
384 } else {
385 false
386 }
387 }
388
389 pub fn set_scope(&self, scope: ToolScope) {
391 self.scope.store(Arc::new(scope));
392 }
393
394 #[must_use]
412 pub fn scope_for_task(&self, task_type: &str) -> Option<Vec<String>> {
413 self.scopes.get(task_type).map(|s| {
414 if s.is_full {
415 vec!["*".to_owned()]
416 } else {
417 s.admitted_ids().iter().map(|s| (*s).to_owned()).collect()
418 }
419 })
420 }
421
422 #[must_use]
424 pub fn scope_at_definition_name(&self) -> Option<String> {
425 self.scope_at_definition.lock().clone()
426 }
427
428 #[must_use]
430 pub fn active_scope_name(&self) -> Option<String> {
431 self.scope.load().task_type.clone()
432 }
433}
434
435impl<E: ToolExecutor> ToolExecutor for ScopedToolExecutor<E> {
436 async fn execute(&self, response: &str) -> Result<Option<ToolOutput>, ToolError> {
438 self.inner.execute(response).await
439 }
440
441 async fn execute_confirmed(&self, response: &str) -> Result<Option<ToolOutput>, ToolError> {
442 self.inner.execute_confirmed(response).await
443 }
444
445 fn tool_definitions(&self) -> Vec<ToolDef> {
449 let scope = self.scope.load();
450 self.scope_at_definition.lock().clone_from(&scope.task_type);
451 self.inner
452 .tool_definitions()
453 .into_iter()
454 .filter(|d| {
455 let id = d.id.as_ref();
456 let scope_id: String;
457 let qualified = if id.contains(':') {
458 id
459 } else {
460 scope_id = format!("builtin:{id}");
461 scope_id.as_str()
462 };
463 scope.admits(qualified)
464 })
465 .collect()
466 }
467
468 async fn execute_tool_call(&self, call: &ToolCall) -> Result<Option<ToolOutput>, ToolError> {
473 let scope = self.scope.load();
474 let tool_id = call.tool_id.as_str();
475 let qualified_id: String;
479 let scope_id = if tool_id.contains(':') {
480 tool_id
481 } else {
482 qualified_id = format!("builtin:{tool_id}");
483 qualified_id.as_str()
484 };
485
486 if !scope.admits(scope_id) {
487 let scope_name = scope.task_type.clone();
488 let scope_def = self.scope_at_definition.lock().clone();
489 tracing::debug!(
490 tool_id,
491 scope = ?scope_name,
492 "ScopedToolExecutor: out-of-scope rejection"
493 );
494 if let Some(ref q) = self.signal_queue {
496 q.lock().push(3);
497 }
498 if let Some(ref audit) = self.audit {
500 let entry = AuditEntry {
501 timestamp: chrono_now(),
502 tool: call.tool_id.clone(),
503 command: String::new(),
504 result: AuditResult::Blocked {
505 reason: "out_of_scope".to_owned(),
506 },
507 duration_ms: 0,
508 error_category: Some("out_of_scope".to_owned()),
509 error_domain: Some("security".to_owned()),
510 error_phase: None,
511 claim_source: None,
512 mcp_server_id: None,
513 injection_flagged: false,
514 embedding_anomalous: false,
515 cross_boundary_mcp_to_acp: false,
516 adversarial_policy_decision: None,
517 exit_code: None,
518 truncated: false,
519 caller_id: call.caller_id.clone(),
520 skill_name: call.skill_name.clone(),
521 policy_match: None,
522 correlation_id: None,
523 vigil_risk: None,
524 execution_env: None,
525 resolved_cwd: None,
526 scope_at_definition: scope_def,
527 scope_at_dispatch: scope_name,
528 };
529 audit.log(&entry).await;
530 }
531 return Err(ToolError::OutOfScope {
532 tool_id: tool_id.to_owned(),
533 task_type: scope.task_type.clone(),
534 });
535 }
536
537 self.inner.execute_tool_call(call).await
538 }
539
540 async fn execute_tool_call_confirmed(
541 &self,
542 call: &ToolCall,
543 ) -> Result<Option<ToolOutput>, ToolError> {
544 let scope = self.scope.load();
545 let tool_id = call.tool_id.as_str();
546 let qualified_id: String;
547 let scope_id = if tool_id.contains(':') {
548 tool_id
549 } else {
550 qualified_id = format!("builtin:{tool_id}");
551 qualified_id.as_str()
552 };
553 if !scope.admits(scope_id) {
554 let scope_name = scope.task_type.clone();
555 let scope_def = self.scope_at_definition.lock().clone();
556 if let Some(ref q) = self.signal_queue {
557 q.lock().push(3);
558 }
559 if let Some(ref audit) = self.audit {
560 let entry = AuditEntry {
561 timestamp: chrono_now(),
562 tool: call.tool_id.clone(),
563 command: String::new(),
564 result: AuditResult::Blocked {
565 reason: "out_of_scope".to_owned(),
566 },
567 duration_ms: 0,
568 error_category: Some("out_of_scope".to_owned()),
569 error_domain: Some("security".to_owned()),
570 error_phase: None,
571 claim_source: None,
572 mcp_server_id: None,
573 injection_flagged: false,
574 embedding_anomalous: false,
575 cross_boundary_mcp_to_acp: false,
576 adversarial_policy_decision: None,
577 exit_code: None,
578 truncated: false,
579 caller_id: call.caller_id.clone(),
580 skill_name: call.skill_name.clone(),
581 policy_match: None,
582 correlation_id: None,
583 vigil_risk: None,
584 execution_env: None,
585 resolved_cwd: None,
586 scope_at_definition: scope_def,
587 scope_at_dispatch: scope_name,
588 };
589 audit.log(&entry).await;
590 }
591 return Err(ToolError::OutOfScope {
592 tool_id: tool_id.to_owned(),
593 task_type: scope.task_type.clone(),
594 });
595 }
596 self.inner.execute_tool_call_confirmed(call).await
597 }
598
599 fn set_skill_env(&self, env: Option<std::collections::HashMap<String, String>>) {
600 self.inner.set_skill_env(env);
601 }
602
603 fn set_effective_trust(&self, level: crate::SkillTrustLevel) {
604 self.inner.set_effective_trust(level);
605 }
606
607 fn is_tool_retryable(&self, tool_id: &str) -> bool {
608 self.inner.is_tool_retryable(tool_id)
609 }
610
611 fn is_tool_speculatable(&self, tool_id: &str) -> bool {
612 self.inner.is_tool_speculatable(tool_id)
613 }
614}
615
616pub fn build_scoped_executor<E: ToolExecutor, S: std::hash::BuildHasher>(
644 inner: E,
645 cfg: &CapabilityScopesConfig,
646 registry_ids: &HashSet<String, S>,
647) -> Result<ScopedToolExecutor<E>, ScopeError> {
648 let default_scope_name = &cfg.default_scope;
649 let strictness = cfg.pattern_strictness;
650
651 let initial_scope = ToolScope::full();
653 let mut executor = ScopedToolExecutor::new(inner, initial_scope);
654
655 for (task_type, scope_cfg) in &cfg.scopes {
656 let is_general = task_type == default_scope_name;
657 let (scope, warnings) = ToolScope::try_compile(
658 task_type.clone(),
659 &scope_cfg.patterns,
660 registry_ids,
661 strictness,
662 is_general,
663 )?;
664 for w in &warnings {
665 warn!(
666 scope = %w.scope,
667 pattern = %w.pattern,
668 "capability scope: provisional zero-match pattern (will re-resolve on dynamic registration)"
669 );
670 }
671 executor.register_scope(task_type.clone(), scope);
672 }
673
674 if cfg.scopes.contains_key(default_scope_name.as_str()) {
676 executor.set_scope_for_task(default_scope_name);
677 }
678
679 Ok(executor)
680}
681
682#[cfg(test)]
683mod tests {
684 use super::*;
685 use crate::executor::ToolCall;
686 use crate::registry::{InvocationHint, ToolDef};
687 use zeph_common::ToolName;
688 use zeph_config::{CapabilityScopesConfig, PatternStrictness, ScopeConfig};
689
690 fn make_registry(ids: &[&str]) -> HashSet<String> {
691 ids.iter().map(|s| (*s).to_owned()).collect()
692 }
693
694 struct NullExecutor {
695 defs: Vec<ToolDef>,
696 }
697
698 impl ToolExecutor for NullExecutor {
699 async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
700 Ok(None)
701 }
702
703 fn tool_definitions(&self) -> Vec<ToolDef> {
704 self.defs.clone()
705 }
706
707 async fn execute_tool_call(
708 &self,
709 call: &ToolCall,
710 ) -> Result<Option<ToolOutput>, ToolError> {
711 Ok(Some(ToolOutput {
712 tool_name: call.tool_id.clone(),
713 summary: "ok".to_owned(),
714 blocks_executed: 1,
715 filter_stats: None,
716 diff: None,
717 streamed: false,
718 terminal_id: None,
719 locations: None,
720 raw_response: None,
721 claim_source: None,
722 }))
723 }
724 }
725
726 fn null_def(id: &str) -> ToolDef {
727 ToolDef {
728 id: id.to_owned().into(),
729 description: "test tool".into(),
730 schema: schemars::schema_for!(String),
731 invocation: InvocationHint::ToolCall,
732 output_schema: None,
733 }
734 }
735
736 fn make_call(tool_id: &str) -> ToolCall {
737 ToolCall {
738 tool_id: ToolName::new(tool_id),
739 params: serde_json::Map::new(),
740 caller_id: None,
741 context: None,
742
743 tool_call_id: String::new(),
744 skill_name: None,
745 }
746 }
747
748 #[test]
749 fn full_scope_admits_everything() {
750 let scope = ToolScope::full();
751 assert!(scope.admits("builtin:shell"));
752 assert!(scope.admits("mcp:server/tool"));
753 assert!(scope.admits("builtin:read"));
754 }
755
756 #[test]
757 fn compiled_scope_admits_only_matched() {
758 let registry = make_registry(&["builtin:shell", "builtin:read", "builtin:write"]);
759 let patterns = vec!["builtin:read".to_owned()];
760 let (scope, warnings) = ToolScope::try_compile(
761 "narrow",
762 &patterns,
763 ®istry,
764 PatternStrictness::Strict,
765 false,
766 )
767 .unwrap();
768 assert!(warnings.is_empty());
769 assert!(scope.admits("builtin:read"));
770 assert!(!scope.admits("builtin:shell"));
771 assert!(!scope.admits("builtin:write"));
772 }
773
774 #[test]
775 fn dead_pattern_strict_returns_error() {
776 let registry = make_registry(&["builtin:shell"]);
777 let patterns = vec!["builtin:nonexistent".to_owned()];
778 let result = ToolScope::try_compile(
779 "test",
780 &patterns,
781 ®istry,
782 PatternStrictness::Strict,
783 false,
784 );
785 assert!(
786 matches!(result, Err(ScopeError::DeadPattern { .. })),
787 "expected DeadPattern, got {result:?}"
788 );
789 }
790
791 #[test]
792 fn dead_pattern_provisional_returns_warning() {
793 let registry = make_registry(&["builtin:shell"]);
794 let patterns = vec!["mcp:server/nonexistent".to_owned()];
795 let result = ToolScope::try_compile(
796 "test",
797 &patterns,
798 ®istry,
799 PatternStrictness::ProvisionalForDynamicNamespaces,
800 false,
801 );
802 assert!(result.is_ok());
803 let (_, warnings) = result.unwrap();
804 assert_eq!(warnings.len(), 1);
805 }
806
807 #[test]
808 fn accidentally_full_pattern_returns_error() {
809 let registry = make_registry(&["builtin:shell", "builtin:read"]);
810 let patterns = vec!["*".to_owned()];
811 let result = ToolScope::try_compile(
812 "test",
813 &patterns,
814 ®istry,
815 PatternStrictness::Strict,
816 false, );
818 assert!(
819 matches!(result, Err(ScopeError::AccidentallyFull { .. })),
820 "expected AccidentallyFull for non-general scope with '*'"
821 );
822 }
823
824 #[test]
825 fn general_scope_allows_wildcard() {
826 let registry = make_registry(&["builtin:shell", "builtin:read"]);
827 let patterns = vec!["*".to_owned()];
828 let result = ToolScope::try_compile(
829 "general",
830 &patterns,
831 ®istry,
832 PatternStrictness::Strict,
833 true, );
835 assert!(result.is_ok());
836 }
837
838 #[tokio::test]
839 async fn executor_rejects_out_of_scope_call() {
840 let registry = make_registry(&["builtin:shell", "builtin:read"]);
841 let (scope, _) = ToolScope::try_compile(
842 "narrow",
843 &["builtin:read".to_owned()],
844 ®istry,
845 PatternStrictness::Strict,
846 false,
847 )
848 .unwrap();
849 let inner = NullExecutor {
850 defs: vec![null_def("builtin:shell"), null_def("builtin:read")],
851 };
852 let executor = ScopedToolExecutor::new(inner, scope);
853 let call = make_call("builtin:shell");
854 let result = executor.execute_tool_call(&call).await;
855 assert!(matches!(result, Err(ToolError::OutOfScope { .. })));
856 }
857
858 #[tokio::test]
859 async fn executor_allows_in_scope_call() {
860 let registry = make_registry(&["builtin:shell", "builtin:read"]);
861 let (scope, _) = ToolScope::try_compile(
862 "narrow",
863 &["builtin:read".to_owned()],
864 ®istry,
865 PatternStrictness::Strict,
866 false,
867 )
868 .unwrap();
869 let inner = NullExecutor {
870 defs: vec![null_def("builtin:shell"), null_def("builtin:read")],
871 };
872 let executor = ScopedToolExecutor::new(inner, scope);
873 let call = make_call("builtin:read");
874 let result = executor.execute_tool_call(&call).await;
875 assert!(result.is_ok());
876 }
877
878 #[test]
879 fn tool_definitions_filtered_by_scope() {
880 let registry = make_registry(&["builtin:shell", "builtin:read"]);
881 let (scope, _) = ToolScope::try_compile(
882 "narrow",
883 &["builtin:read".to_owned()],
884 ®istry,
885 PatternStrictness::Strict,
886 false,
887 )
888 .unwrap();
889 let inner = NullExecutor {
890 defs: vec![null_def("builtin:shell"), null_def("builtin:read")],
891 };
892 let executor = ScopedToolExecutor::new(inner, scope);
893 let defs = executor.tool_definitions();
894 assert_eq!(defs.len(), 1);
895 assert_eq!(defs[0].id.as_ref(), "builtin:read");
896 }
897
898 #[tokio::test]
899 async fn unnamespaced_tool_id_admitted_via_builtin_prefix() {
900 let registry = make_registry(&["builtin:bash", "builtin:read"]);
903 let (scope, _) = ToolScope::try_compile(
904 "narrow",
905 &["builtin:bash".to_owned()],
906 ®istry,
907 PatternStrictness::Strict,
908 false,
909 )
910 .unwrap();
911 let inner = NullExecutor {
912 defs: vec![null_def("bash"), null_def("read")],
913 };
914 let executor = ScopedToolExecutor::new(inner, scope);
915 let call = make_call("bash");
917 let result = executor.execute_tool_call(&call).await;
918 assert!(
919 result.is_ok(),
920 "builtin tool with unqualified id must be admitted"
921 );
922 let call_read = make_call("read");
924 let result_read = executor.execute_tool_call(&call_read).await;
925 assert!(
926 matches!(result_read, Err(ToolError::OutOfScope { .. })),
927 "out-of-scope built-in tool must be rejected"
928 );
929 }
930
931 #[test]
932 fn build_scoped_executor_accepts_unqualified_registry_id() {
933 let cfg = CapabilityScopesConfig::default();
936 let registry = make_registry(&["shell"]); let inner = NullExecutor { defs: vec![] };
938 let result = build_scoped_executor(inner, &cfg, ®istry);
939 assert!(
940 result.is_ok(),
941 "build_scoped_executor must accept unqualified registry ids"
942 );
943 }
944
945 #[test]
946 fn build_scoped_executor_with_builtin_prefix_and_glob() {
947 let mut cfg = CapabilityScopesConfig::default();
948 cfg.scopes.insert(
949 "general".to_owned(),
950 ScopeConfig {
951 patterns: vec!["builtin:*".to_owned()],
952 },
953 );
954 cfg.default_scope = "general".to_owned();
955 let registry = make_registry(&["builtin:bash", "builtin:read", "builtin:fetch"]);
956 let inner = NullExecutor { defs: vec![] };
957 let result = build_scoped_executor(inner, &cfg, ®istry);
958 assert!(
959 result.is_ok(),
960 "builtin:* glob must match all builtin tools"
961 );
962 }
963
964 #[tokio::test]
965 async fn unqualified_tool_out_of_scope_rejected() {
966 let registry = make_registry(&["builtin:bash", "builtin:read"]);
967 let (scope, _) = ToolScope::try_compile(
968 "narrow",
969 &["builtin:read".to_owned()],
970 ®istry,
971 PatternStrictness::Strict,
972 false,
973 )
974 .unwrap();
975 let inner = NullExecutor {
976 defs: vec![null_def("bash"), null_def("read")],
977 };
978 let executor = ScopedToolExecutor::new(inner, scope);
979 let call = make_call("bash"); let result = executor.execute_tool_call(&call).await;
981 assert!(
982 matches!(result, Err(ToolError::OutOfScope { .. })),
983 "unqualified id not in scope must be rejected after normalization"
984 );
985 }
986
987 #[test]
988 fn tool_definitions_filtered_by_scope_with_unqualified_ids() {
989 let registry = make_registry(&["builtin:bash", "builtin:read"]);
991 let (scope, _) = ToolScope::try_compile(
992 "narrow",
993 &["builtin:read".to_owned()],
994 ®istry,
995 PatternStrictness::Strict,
996 false,
997 )
998 .unwrap();
999 let inner = NullExecutor {
1000 defs: vec![null_def("bash"), null_def("read")],
1001 };
1002 let executor = ScopedToolExecutor::new(inner, scope);
1003 let defs = executor.tool_definitions();
1004 assert_eq!(defs.len(), 1);
1005 assert_eq!(defs[0].id.as_ref(), "read");
1006 }
1007
1008 #[test]
1009 fn scope_for_task_returns_ids() {
1010 let registry = make_registry(&["builtin:shell", "builtin:read"]);
1011 let (scope, _) = ToolScope::try_compile(
1012 "narrow",
1013 &["builtin:read".to_owned()],
1014 ®istry,
1015 PatternStrictness::Strict,
1016 false,
1017 )
1018 .unwrap();
1019 let inner = NullExecutor { defs: vec![] };
1020 let mut executor = ScopedToolExecutor::new(inner, ToolScope::full());
1021 executor.register_scope("narrow", scope);
1022 let ids = executor.scope_for_task("narrow");
1023 assert!(ids.is_some());
1024 let ids = ids.unwrap();
1025 assert!(ids.contains(&"builtin:read".to_owned()));
1026 assert!(!ids.contains(&"builtin:shell".to_owned()));
1027 }
1028
1029 #[test]
1030 fn scope_for_task_returns_none_for_unknown() {
1031 let inner = NullExecutor { defs: vec![] };
1032 let executor = ScopedToolExecutor::new(inner, ToolScope::full());
1033 assert!(executor.scope_for_task("does_not_exist").is_none());
1034 }
1035
1036 #[test]
1037 fn re_resolve_updates_admitted_set() {
1038 let registry = make_registry(&["builtin:read", "mcp:server/tool"]);
1041 let (scope, _) = ToolScope::try_compile(
1042 "narrow",
1043 &["builtin:read".to_owned()],
1044 ®istry,
1045 PatternStrictness::Strict,
1046 false,
1047 )
1048 .unwrap();
1049 assert!(scope.admits("builtin:read"));
1050 assert!(!scope.admits("builtin:write"));
1051
1052 let mut new_registry = registry.clone();
1054 new_registry.insert("builtin:write".to_owned());
1055 let updated = scope.re_resolve(&new_registry);
1056 assert!(updated.admits("builtin:read"));
1057 assert!(!updated.admits("builtin:write"));
1059 }
1060
1061 #[test]
1062 fn build_from_config_with_scopes() {
1063 let mut scopes = std::collections::HashMap::new();
1064 scopes.insert(
1065 "general".to_owned(),
1066 ScopeConfig {
1067 patterns: vec!["*".to_owned()],
1068 },
1069 );
1070 scopes.insert(
1071 "narrow".to_owned(),
1072 ScopeConfig {
1073 patterns: vec!["builtin:read".to_owned()],
1074 },
1075 );
1076 let cfg = CapabilityScopesConfig {
1077 default_scope: "general".to_owned(),
1078 strict: false,
1079 pattern_strictness: PatternStrictness::Strict,
1080 scopes,
1081 };
1082 let registry = make_registry(&["builtin:shell", "builtin:read"]);
1083 let inner = NullExecutor { defs: vec![] };
1084 let executor = build_scoped_executor(inner, &cfg, ®istry).unwrap();
1085 let narrow_ids = executor.scope_for_task("narrow");
1087 assert!(narrow_ids.is_some());
1088 let ids = narrow_ids.unwrap();
1089 assert!(ids.contains(&"builtin:read".to_owned()));
1090 }
1091}