返回 CodeWhale
activation.rs
根目录 / crates / plugins / src / activation.rs
1 //! Versioned plugin activation policy.
2 //!
3 //! This is the single source of truth for which reviewed component adapters
4 //! this Codewhale build will execute. Compatibility, `active()` decisions,
5 //! consumption-boundary checks, and the capability hash all read this policy.
6 //! Enabling a new adapter later must change the policy (version and/or mask)
7 //! so existing trust receipts fail closed as `CapabilitiesChanged`.
8
9 use sha2::Digest;
10
11 /// Capability-hash domain for the current activation-policy binding.
12 pub const CAPABILITY_HASH_DOMAIN_V3: &[u8] = b"codewhale-plugin-capabilities-v3\0";
13
14 /// Historical policy domain kept so persisted v2 receipts are intentionally
15 /// invalidated when the declarative Commands, Agents, and Hooks adapters ship.
16 pub const CAPABILITY_HASH_DOMAIN_V2: &[u8] = b"codewhale-plugin-capabilities-v2\0";
17
18 /// Historical domain used before the activation policy was bound into the
19 /// receipt. Kept so discovery can prove a v1 receipt no longer matches.
20 pub const CAPABILITY_HASH_DOMAIN_V1: &[u8] = b"codewhale-plugin-capabilities-v1\0";
21
22 pub const ACTIVATION_POLICY_VERSION: u32 = 5;
23
24 /// Policy version selected when `[features] extension_host` is on: `Native`
25 /// (host code run by the TypeScript extension host) moves from inactive to
26 /// supported. Every receipt reviewed under v5 fails closed as
27 /// `CapabilitiesChanged` under v9 and the reverse, so toggling the flag in
28 /// either direction re-reviews every plugin. That is intended.
29 pub const EXTENSION_HOST_POLICY_VERSION: u32 = 9;
30
31 pub const DSH_COMPATIBILITY: &[u8] = include_bytes!("../assets/dsh-compatibility.json");
32
33 pub fn dsh_row_bridge(name: &str) -> bool {
34 static TABLE: std::sync::OnceLock<Option<serde_json::Value>> = std::sync::OnceLock::new();
35 TABLE
36 .get_or_init(|| serde_json::from_slice(DSH_COMPATIBILITY).ok())
37 .as_ref()
38 .and_then(|table| table.get("row_bridges"))
39 .and_then(|bridges| bridges.get(name))
40 .is_some_and(serde_json::Value::is_string)
41 }
42
43 /// Process-wide policy selection, set once at boot from config
44 /// (`install_extension_host_policy`). A config reload never flips it
45 /// mid-process; unset means the shipping v5 policy, which also covers tests.
46 static EXTENSION_HOST_POLICY: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
47
48 #[cfg(any(test, feature = "test-support"))]
49 thread_local! {
50 /// Test-only per-thread override so one test can exercise v9 without
51 /// changing the policy every other (parallel) test observes.
52 static TEST_POLICY_OVERRIDE: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
53 }
54
55 /// Select the activation policy for this process. The first call wins.
56 pub fn install_extension_host_policy(extension_host_enabled: bool) {
57 let _ = EXTENSION_HOST_POLICY.set(extension_host_enabled);
58 }
59
60 /// Whether this process activates `Native` (extension host) components.
61 #[must_use]
62 pub fn extension_host_policy_enabled() -> bool {
63 #[cfg(any(test, feature = "test-support"))]
64 if let Some(value) = TEST_POLICY_OVERRIDE.with(std::cell::Cell::get) {
65 return value;
66 }
67 EXTENSION_HOST_POLICY.get().copied().unwrap_or(false)
68 }
69
70 /// Carries the calling thread's policy into a `spawn_blocking` closure. In
71 /// production the policy is process-wide, so this is a no-op; under test it
72 /// re-applies the per-thread override on the blocking thread.
73 pub struct PolicyScope {
74 #[cfg(any(test, feature = "test-support"))]
75 _guard: TestPolicyGuard,
76 }
77
78 impl PolicyScope {
79 #[must_use]
80 pub fn propagate(extension_host_enabled: bool) -> Self {
81 #[cfg(any(test, feature = "test-support"))]
82 {
83 Self {
84 _guard: TestPolicyGuard::extension_host(extension_host_enabled),
85 }
86 }
87 #[cfg(not(any(test, feature = "test-support")))]
88 {
89 let _ = extension_host_enabled;
90 Self {}
91 }
92 }
93 }
94
95 /// Test guard selecting the v9 (or v5) policy on the current thread only.
96 #[cfg(any(test, feature = "test-support"))]
97 pub struct TestPolicyGuard {
98 previous: Option<bool>,
99 }
100
101 #[cfg(any(test, feature = "test-support"))]
102 impl TestPolicyGuard {
103 pub fn extension_host(enabled: bool) -> Self {
104 let previous = TEST_POLICY_OVERRIDE.with(|cell| cell.replace(Some(enabled)));
105 Self { previous }
106 }
107 }
108
109 #[cfg(any(test, feature = "test-support"))]
110 impl Drop for TestPolicyGuard {
111 fn drop(&mut self) {
112 TEST_POLICY_OVERRIDE.with(|cell| cell.set(self.previous));
113 }
114 }
115
116 /// A runtime adapter or inventoried capability that a bundle may declare.
117 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
118 pub enum PluginActivationCapability {
119 Skills,
120 McpStdio,
121 McpRemote,
122 Commands,
123 Agents,
124 Hooks,
125 Lsp,
126 Native,
127 Providers,
128 FilesystemRoots,
129 LifecycleMutation,
130 }
131
132 impl PluginActivationCapability {
133 pub const ALL: &'static [Self] = &[
134 Self::Skills,
135 Self::McpStdio,
136 Self::McpRemote,
137 Self::Commands,
138 Self::Agents,
139 Self::Hooks,
140 Self::Lsp,
141 Self::Native,
142 Self::Providers,
143 Self::FilesystemRoots,
144 Self::LifecycleMutation,
145 ];
146
147 #[must_use]
148 pub fn as_str(self) -> &'static str {
149 match self {
150 Self::Skills => "skills",
151 Self::McpStdio => "mcp-stdio",
152 Self::McpRemote => "mcp-remote",
153 Self::Commands => "commands",
154 Self::Agents => "agents",
155 Self::Hooks => "hooks",
156 Self::Lsp => "lsp",
157 Self::Native => "native",
158 Self::Providers => "providers",
159 Self::FilesystemRoots => "filesystem-roots",
160 Self::LifecycleMutation => "lifecycle-mutation",
161 }
162 }
163 }
164
165 /// The adapters this exact Codewhale build will activate, plus the inventoried
166 /// surfaces that stay inactive. Fields are public so tests can construct a
167 /// mutated policy and prove the capability hash moves.
168 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
169 pub struct PluginActivationPolicy {
170 pub version: u32,
171 pub supported: &'static [PluginActivationCapability],
172 pub inactive: &'static [PluginActivationCapability],
173 pub dsh_compatibility: &'static [u8],
174 }
175
176 impl PluginActivationPolicy {
177 /// The policy this process runs under: v5, or v9 when the experimental
178 /// extension host is enabled (see [`install_extension_host_policy`]).
179 #[must_use]
180 pub fn current() -> Self {
181 if extension_host_policy_enabled() {
182 Self::extension_host()
183 } else {
184 Self::v3()
185 }
186 }
187
188 /// v9: v5 capabilities plus `Native`, namespaced commands, brokered Git metadata and admitted client maps.
189 #[must_use]
190 pub const fn extension_host() -> Self {
191 Self {
192 version: EXTENSION_HOST_POLICY_VERSION,
193 dsh_compatibility: DSH_COMPATIBILITY,
194 supported: &[
195 PluginActivationCapability::Skills,
196 PluginActivationCapability::McpStdio,
197 PluginActivationCapability::McpRemote,
198 PluginActivationCapability::Commands,
199 PluginActivationCapability::Agents,
200 PluginActivationCapability::Hooks,
201 PluginActivationCapability::Providers,
202 PluginActivationCapability::Native,
203 ],
204 inactive: &[
205 PluginActivationCapability::Lsp,
206 PluginActivationCapability::FilesystemRoots,
207 PluginActivationCapability::LifecycleMutation,
208 ],
209 }
210 }
211
212 /// Shipping declarative policy (v5). The historical `v3` method name is
213 /// retained; providers intentionally invalidate older review receipts.
214 #[must_use]
215 pub const fn v3() -> Self {
216 Self {
217 version: ACTIVATION_POLICY_VERSION,
218 dsh_compatibility: &[],
219 supported: &[
220 PluginActivationCapability::Skills,
221 PluginActivationCapability::McpStdio,
222 PluginActivationCapability::McpRemote,
223 PluginActivationCapability::Commands,
224 PluginActivationCapability::Agents,
225 PluginActivationCapability::Hooks,
226 PluginActivationCapability::Providers,
227 ],
228 inactive: &[
229 PluginActivationCapability::Lsp,
230 PluginActivationCapability::Native,
231 PluginActivationCapability::FilesystemRoots,
232 PluginActivationCapability::LifecycleMutation,
233 ],
234 }
235 }
236
237 #[must_use]
238 pub fn is_supported(self, capability: PluginActivationCapability) -> bool {
239 self.supported.contains(&capability)
240 }
241
242 pub fn write_hash_material(self, hasher: &mut impl Digest) {
243 hasher.update(CAPABILITY_HASH_DOMAIN_V3);
244 hasher.update(b"policy-version\0");
245 hasher.update(self.version.to_string().as_bytes());
246 hasher.update(b"\0");
247 for capability in self.supported {
248 hasher.update(b"supported\0");
249 hasher.update(capability.as_str().as_bytes());
250 hasher.update(b"\0");
251 }
252 for capability in self.inactive {
253 hasher.update(b"inactive\0");
254 hasher.update(capability.as_str().as_bytes());
255 hasher.update(b"\0");
256 }
257 if !self.dsh_compatibility.is_empty() {
258 hasher.update(b"dsh-compatibility\0");
259 hasher.update((self.dsh_compatibility.len() as u64).to_le_bytes());
260 hasher.update(self.dsh_compatibility);
261 }
262 }
263 }
264
265 #[cfg(test)]
266 mod tests {
267 use super::*;
268
269 #[test]
270 fn current_activation_policy_partitions_known_capabilities() {
271 let policy = PluginActivationPolicy::current();
272 for capability in PluginActivationCapability::ALL {
273 let supported = policy.supported.contains(capability);
274 let inactive = policy.inactive.contains(capability);
275 assert_ne!(
276 supported, inactive,
277 "{capability:?} must be supported or inactive, not both or neither"
278 );
279 }
280 }
281
282 fn policy_digest(policy: PluginActivationPolicy) -> String {
283 let mut hasher = sha2::Sha256::new();
284 policy.write_hash_material(&mut hasher);
285 hasher
286 .finalize()
287 .iter()
288 .map(|byte| format!("{byte:02x}"))
289 .collect()
290 }
291
292 /// Pin the shipping provider-aware policy independently of the optional
293 /// native extension host; changes require deliberate receipt migration.
294 #[test]
295 fn flag_off_policy_hashes_exactly_as_shipping_provider_policy() {
296 let _guard = TestPolicyGuard::extension_host(false);
297 let current = PluginActivationPolicy::current();
298 assert_eq!(current, PluginActivationPolicy::v3());
299 assert_eq!(
300 policy_digest(current),
301 "f0584fdb007b7987b0934517c8723b96190137771795ed33b38499b9353aa7cf"
302 );
303 assert!(!current.is_supported(PluginActivationCapability::Native));
304 }
305
306 #[test]
307 fn flag_on_policy_supports_native_and_moves_the_hash() {
308 let _guard = TestPolicyGuard::extension_host(true);
309 let current = PluginActivationPolicy::current();
310 assert_eq!(current.version, EXTENSION_HOST_POLICY_VERSION);
311 assert!(current.is_supported(PluginActivationCapability::Native));
312 assert_ne!(
313 policy_digest(current),
314 policy_digest(PluginActivationPolicy::v3())
315 );
316 let previous_native = PluginActivationPolicy {
317 version: 8,
318 ..current
319 };
320 assert_ne!(policy_digest(current), policy_digest(previous_native));
321 let changed = PluginActivationPolicy {
322 dsh_compatibility: b"{\"provided_services\":[\"new-service\"]}",
323 ..current
324 };
325 assert_ne!(policy_digest(current), policy_digest(changed));
326 let table: serde_json::Value = serde_json::from_slice(DSH_COMPATIBILITY).unwrap();
327 assert_eq!(table["version"], 3);
328 assert!(dsh_row_bridge("@deepseek-ai/dsh-persona"));
329 assert!(!dsh_row_bridge("@deepseek-ai/dsh-agent-loop"));
330 for capability in PluginActivationCapability::ALL {
331 assert_ne!(
332 current.supported.contains(capability),
333 current.inactive.contains(capability),
334 "{capability:?}"
335 );
336 }
337 }
338 }
339
339 lines RUST