Skip to main content

vtcode_core/skills/
consent.rs

1//! Workspace-level composition of ask-first consent for bundled skills.
2//!
3//! Combines the shared [`vtcode_config::consent`] answer with the pre-consent
4//! cache probe. Every install site funnels through
5//! [`bundled_skills_install_permitted_for_workspace`]; the function is
6//! fail-closed, so any configuration-load or path-resolution error means no
7//! install and no system discovery root.
8
9use std::path::Path;
10
11use crate::config::ConfigManager;
12use crate::config::consent::{bundled_skills_answer, bundled_skills_install_permitted};
13use crate::skills::system::bundled_skills_grandfathered;
14
15/// Whether bundled system skills may be installed or refreshed for `workspace`.
16///
17/// True only for an explicit grant, or while unasked when a pre-consent
18/// cache grandfathers the refresh. Never true for an explicit decline —
19/// even when a stale cache is still on disk.
20#[must_use]
21pub fn bundled_skills_install_permitted_for_workspace(workspace: &Path) -> bool {
22    let Ok(manager) = ConfigManager::load_from_workspace(workspace) else {
23        return false;
24    };
25    bundled_skills_install_permitted(bundled_skills_answer(&manager), bundled_skills_grandfathered())
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use crate::skills::system::{install_system_skills, system_cache_root_dir};
32    use serial_test::serial;
33    use std::fs;
34    use std::sync::Arc;
35    use tempfile::TempDir;
36
37    struct ConsentHarness {
38        _root: TempDir,
39        workspace: std::path::PathBuf,
40        data: std::path::PathBuf,
41        codex: std::path::PathBuf,
42        previous_provider: Arc<dyn crate::config::ConfigDefaultsProvider>,
43        previous_data: Option<std::ffi::OsString>,
44        previous_codex: Option<std::ffi::OsString>,
45    }
46
47    impl ConsentHarness {
48        fn new(user_toml: Option<&str>) -> Self {
49            let root = TempDir::new().expect("hermetic root");
50            let data = root.path().join("data");
51            let codex = root.path().join("codex");
52            let workspace = root.path().join("workspace");
53            for dir in [&data, &codex, &workspace] {
54                fs::create_dir_all(dir).expect("hermetic dir");
55            }
56            let home_paths = match user_toml {
57                Some(content) => {
58                    let home_dir = root.path().join("home");
59                    fs::create_dir_all(&home_dir).expect("home dir");
60                    let user_config = home_dir.join("vtcode.toml");
61                    fs::write(&user_config, content).expect("user config");
62                    vec![user_config]
63                }
64                None => Vec::new(),
65            };
66            let static_paths =
67                vtcode_commons::reference::StaticWorkspacePaths::new(&workspace, workspace.join(".vtcode"));
68            let provider = crate::config::defaults::WorkspacePathsDefaults::new(Arc::new(static_paths))
69                .with_home_paths(home_paths);
70            let previous_provider = crate::config::install_config_defaults_provider(Arc::new(provider));
71            let previous_data = std::env::var_os("VTCODE_DATA");
72            let previous_codex = std::env::var_os("CODEX_HOME");
73            let guard = vtcode_commons::env_lock::lock();
74            guard.set_var("VTCODE_DATA", &data);
75            guard.set_var("CODEX_HOME", &codex);
76            Self {
77                _root: root,
78                workspace,
79                data,
80                codex,
81                previous_provider,
82                previous_data,
83                previous_codex,
84            }
85        }
86
87        fn permitted(&self) -> bool {
88            bundled_skills_install_permitted_for_workspace(&self.workspace)
89        }
90
91        fn install_unified_cache(&self) {
92            install_system_skills(&self.data).expect("unified cache install");
93        }
94
95        fn install_legacy_cache(&self) {
96            install_system_skills(&self.codex).expect("legacy cache install");
97        }
98    }
99
100    impl Drop for ConsentHarness {
101        fn drop(&mut self) {
102            // Restore globals even on assertion failure: the provider and env
103            // outlive the temp dirs, and a leaked override would poison every
104            // later `load_from_workspace` in this test binary. Neither call
105            // panics (poisoned locks recover), so teardown is abort-safe.
106            let _ = crate::config::install_config_defaults_provider(self.previous_provider.clone());
107            let guard = vtcode_commons::env_lock::lock();
108            guard.restore_var("VTCODE_DATA", self.previous_data.as_deref());
109            guard.restore_var("CODEX_HOME", self.previous_codex.as_deref());
110        }
111    }
112
113    #[test]
114    #[serial]
115    fn grant_permits_without_cache() {
116        let harness = ConsentHarness::new(Some("[skills.bundled]\nenabled = true\n"));
117        assert!(!system_cache_root_dir(&harness.data).exists());
118        assert!(harness.permitted());
119    }
120
121    #[test]
122    #[serial]
123    fn decline_forbids_despite_stale_cache() {
124        let harness = ConsentHarness::new(Some("[skills.bundled]\nenabled = false\n"));
125        harness.install_unified_cache();
126        assert!(!harness.permitted());
127    }
128
129    #[test]
130    #[serial]
131    fn unasked_without_cache_forbids() {
132        let harness = ConsentHarness::new(None);
133        assert!(!harness.permitted());
134    }
135
136    #[test]
137    #[serial]
138    fn unasked_with_unified_cache_permits() {
139        let harness = ConsentHarness::new(None);
140        harness.install_unified_cache();
141        assert!(harness.permitted());
142    }
143
144    #[test]
145    #[serial]
146    fn unasked_with_legacy_only_cache_permits() {
147        let harness = ConsentHarness::new(None);
148        assert!(!system_cache_root_dir(&harness.data).exists());
149        harness.install_legacy_cache();
150        assert!(harness.permitted());
151    }
152}