Skip to main content

vtcode_config/
consent.rs

1//! Shared ask-first consent model for home-directory installs.
2//!
3//! Single source of truth used by the CLI first-run wizard and the Desktop
4//! consent card so both surfaces ask the same question, persist the same
5//! answer (`skills.bundled.enabled` in the user layer), and enforce the same
6//! gate. Install sites must consult [`bundled_skills_install_permitted`]:
7//! nothing under the user's home is created for bundled skills until the
8//! answer is [`ConsentAnswer::Granted`] — or the cache already exists from a
9//! previous release (grandfathered), in which case only a refresh of VT Code's
10//! own cache directory is allowed.
11//!
12//! [`ConsentAnswer`] deliberately has three states: an explicit grant,
13//! an explicit decline (never ask again, actively remove our cache), and
14//! unasked (fail closed: no writes, surfaces may prompt once).
15
16use crate::loader::ConfigManager;
17
18/// Dotted setting path that carries the bundled-skills consent answer.
19///
20/// Persisted to the user layer by both the CLI wizard and the Desktop card.
21/// Workspace and project layers may still override the effective value for
22/// their own scope through the normal layer merge.
23pub const BUNDLED_SKILLS_SETTING_PATH: &str = "skills.bundled.enabled";
24
25/// Consent answer for installing bundled skills into the user's home.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum ConsentAnswer {
28    /// The user explicitly allowed the install.
29    Granted,
30    /// The user explicitly declined. Never install, never prompt again for
31    /// the same scope; our own cache directory is removed when present.
32    Declined,
33    /// No answer recorded in any enabled layer. Installs are forbidden
34    /// unless grandfathered (a cache from a previous release exists).
35    Unasked,
36}
37
38/// Resolve the bundled-skills consent answer from a loaded manager.
39///
40/// Presence is checked against every enabled layer (system, user, project,
41/// workspace); the effective value decides between grant and decline. A
42/// missing key is [`ConsentAnswer::Unasked`] even though the effective
43/// default is `false` — absence means "never asked", not "declined".
44#[must_use]
45pub fn bundled_skills_answer(manager: &ConfigManager) -> ConsentAnswer {
46    if !manager.has_explicit_dotted_key(BUNDLED_SKILLS_SETTING_PATH) {
47        return ConsentAnswer::Unasked;
48    }
49    if manager.config().skills.bundled.enabled {
50        ConsentAnswer::Granted
51    } else {
52        ConsentAnswer::Declined
53    }
54}
55
56/// Decide whether bundled skills may be installed or refreshed.
57///
58/// `grandfathered` must be true only when a marker written by a previous
59/// VT Code release proves the cache predates ask-first consent. The
60/// predicate is intentionally pure so unit tests pin the same truth table
61/// the install sites enforce.
62#[must_use]
63pub const fn bundled_skills_install_permitted(answer: ConsentAnswer, grandfathered: bool) -> bool {
64    match answer {
65        ConsentAnswer::Granted => true,
66        ConsentAnswer::Declined => false,
67        ConsentAnswer::Unasked => grandfathered,
68    }
69}
70
71/// Title shared by the CLI wizard step and the Desktop consent card.
72pub const CONSENT_TITLE: &str = "Bundled skills";
73
74/// Question shared by the CLI wizard step and the Desktop consent card.
75pub const CONSENT_QUESTION: &str = "Install VT Code's bundled skills (cmd-review, skill-creator, skill-installer)?";
76
77/// What the install contains. Sizes stay small and no network is involved.
78pub const CONSENT_BODY_WHAT: &str = "Three built-in skills are extracted from the VT Code binary: cmd-review (powers /review), skill-creator, and skill-installer.";
79
80pub const CONSENT_BODY_REVERSIBLE: &str = "Fully reversible: declining (or disabling later) removes the cache and VT Code keeps working; skill features report how to enable them.";
81
82pub const CONSENT_ALLOW_LABEL: &str = "Allow (recommended)";
83
84pub const CONSENT_ALLOW_DESCRIPTION: &str = "Install the three skills to the VT Code data directory.";
85
86pub const CONSENT_DECLINE_LABEL: &str = "Decline";
87
88pub const CONSENT_DECLINE_DESCRIPTION: &str = "Skip the install. VT Code works without these skills.";
89
90pub const CONSENT_MCP_DISCLOSURE: &str =
91    "MCP servers are never installed automatically. Each one is configured explicitly by you.";
92
93pub const CONSENT_REOPEN_HINT: &str =
94    "You can change this anytime in /config (skills.bundled.enabled) or your user vtcode.toml.";
95
96/// Describe where the install lands, for wizard and card copy.
97#[must_use]
98pub fn install_location_description(data_dir: &std::path::Path) -> String {
99    format!(
100        "Installs to {} (offline, from the running binary).",
101        data_dir.join("skills").join(".system").display()
102    )
103}
104
105/// Actionable hint appended when a bundled skill is requested but unavailable.
106#[must_use]
107pub fn bundled_skill_missing_hint(name: &str) -> String {
108    format!(
109        "Skill '{name}' is a bundled skill that is not installed (declined or never consented). Allow it with /config (skills.bundled.enabled) or your user vtcode.toml, then start a new session."
110    )
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use crate::defaults::{self, WorkspacePathsDefaults};
117    use serial_test::serial;
118    use std::fs;
119    use std::sync::Arc;
120    use vtcode_commons::reference::StaticWorkspacePaths;
121
122    fn with_hermetic_manager<R>(
123        user_toml: Option<&str>,
124        workspace_toml: Option<&str>,
125        action: impl FnOnce(&ConfigManager) -> R,
126    ) -> R {
127        let workspace = assert_fs::TempDir::new().expect("workspace tempdir");
128        let home_paths = match user_toml {
129            Some(content) => {
130                let home_dir = workspace.path().join("home");
131                fs::create_dir_all(&home_dir).expect("home dir");
132                let user_config = home_dir.join("vtcode.toml");
133                fs::write(&user_config, content).expect("user config");
134                vec![user_config]
135            }
136            None => Vec::new(),
137        };
138        if let Some(content) = workspace_toml {
139            fs::write(workspace.path().join("vtcode.toml"), content).expect("workspace config");
140        }
141        let paths = StaticWorkspacePaths::new(workspace.path(), workspace.path().join(".vtcode"));
142        let provider = WorkspacePathsDefaults::new(Arc::new(paths)).with_home_paths(home_paths);
143        defaults::provider::with_config_defaults_provider_for_test(Arc::new(provider), || {
144            let manager = ConfigManager::load_from_workspace(workspace.path()).expect("load hermetic config");
145            action(&manager)
146        })
147    }
148
149    fn answer_for_configs(user_toml: Option<&str>, workspace_toml: Option<&str>) -> ConsentAnswer {
150        with_hermetic_manager(user_toml, workspace_toml, bundled_skills_answer)
151    }
152
153    #[test]
154    #[serial]
155    fn answer_distinguishes_grant_decline_and_unasked() {
156        assert_eq!(answer_for_configs(None, None), ConsentAnswer::Unasked);
157        assert_eq!(answer_for_configs(Some("[skills.bundled]\nenabled = true\n"), None), ConsentAnswer::Granted);
158        assert_eq!(answer_for_configs(Some("[skills.bundled]\nenabled = false\n"), None), ConsentAnswer::Declined);
159        // Workspace layers answer too: an explicit workspace grant counts even
160        // when the user layer is silent.
161        assert_eq!(answer_for_configs(None, Some("[skills.bundled]\nenabled = true\n")), ConsentAnswer::Granted);
162        // A neighboring skills key does not answer the consent question.
163        assert_eq!(answer_for_configs(Some("[skills]\nenable-auto-trigger = false\n"), None), ConsentAnswer::Unasked);
164    }
165
166    #[test]
167    #[serial]
168    fn dotted_presence_handles_prefixes_and_malformed_paths() {
169        with_hermetic_manager(Some("[skills.bundled]\nenabled = true\n"), None, |manager| {
170            assert!(manager.has_explicit_dotted_key(BUNDLED_SKILLS_SETTING_PATH));
171            assert!(manager.has_explicit_dotted_key("skills.bundled"));
172            assert!(manager.has_explicit_dotted_key("skills"));
173            assert!(!manager.has_explicit_dotted_key("skills.bundled.missing"));
174            assert!(!manager.has_explicit_dotted_key("skills.bundled.enabled.extra"));
175            for malformed in ["", ".", "skills..enabled", ".skills", "skills."] {
176                assert!(!manager.has_explicit_dotted_key(malformed), "malformed path must not match: {malformed:?}");
177            }
178        });
179    }
180
181    #[test]
182    fn install_permitted_truth_table() {
183        use ConsentAnswer::{Declined, Granted, Unasked};
184        assert!(bundled_skills_install_permitted(Granted, false));
185        assert!(bundled_skills_install_permitted(Granted, true));
186        assert!(!bundled_skills_install_permitted(Declined, false));
187        assert!(!bundled_skills_install_permitted(Declined, true));
188        assert!(!bundled_skills_install_permitted(Unasked, false));
189        assert!(bundled_skills_install_permitted(Unasked, true));
190    }
191
192    #[test]
193    fn missing_hint_names_skill_and_recovery() {
194        let hint = bundled_skill_missing_hint("cmd-review");
195        assert!(hint.contains("cmd-review"), "hint: {hint}");
196        assert!(hint.contains("skills.bundled.enabled"), "hint: {hint}");
197        assert!(hint.contains("new session"), "hint: {hint}");
198    }
199
200    #[test]
201    fn install_location_points_at_system_cache() {
202        let description = install_location_description(std::path::Path::new("/data"));
203        assert!(description.contains("skills"), "description: {description}");
204        assert!(description.contains(".system"), "description: {description}");
205    }
206}