1use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum Surface {
11 AgentProfile,
13 Package,
15 Manifest,
17 HookToml,
19 Settings,
21 AgentOverlay,
23 ModelAlias,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
29pub struct Release(pub u64, pub u64, pub u64);
30
31impl fmt::Display for Release {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(f, "{}.{}.{}", self.0, self.1, self.2)
34 }
35}
36
37impl Release {
38 pub fn version(self) -> semver::Version {
39 semver::Version::new(self.0, self.1, self.2)
40 }
41}
42
43#[derive(Debug, Clone, Copy)]
45pub enum Gate {
46 Present,
48 Rules(fn(&str) -> bool),
51 Value(fn(&str) -> bool),
54}
55
56#[derive(Debug, Clone, Copy)]
58pub struct Gated {
59 pub key: &'static str,
60 pub surfaces: &'static [Surface],
61 pub since: Release,
64 pub gate: Gate,
65}
66
67use crate::agent::EffortRequest;
68use Surface::*;
69
70fn denies_or_asks(action: &str) -> bool {
71 matches!(action, "deny" | "ask")
72}
73
74fn new_effort_request(value: &str) -> bool {
76 matches!(
77 value.to_ascii_lowercase().as_str(),
78 "minimal" | "auto" | "null"
79 )
80}
81
82fn new_default_effort(value: &str) -> bool {
85 !matches!(value, "low" | "medium" | "high" | "xhigh" | "max" | "auto")
86 && EffortRequest::parse(value).is_some()
87}
88
89pub const MIN_MARS_VERSION_SINCE: Release = Release(0, 30, 0);
92
93pub static GATED: &[Gated] = &[
95 Gated {
97 key: "schema",
98 surfaces: &[Package],
99 since: Release(0, 20, 0),
100 gate: Gate::Present,
101 },
102 Gated {
104 key: "uses-providers",
105 surfaces: &[Package],
106 since: Release(0, 20, 0),
107 gate: Gate::Present,
108 },
109 Gated {
111 key: "plugin",
112 surfaces: &[Manifest],
113 since: Release(0, 19, 0),
114 gate: Gate::Present,
115 },
116 Gated {
118 key: "hooks",
119 surfaces: &[HookToml],
120 since: Release(0, 25, 0),
121 gate: Gate::Present,
122 },
123 Gated {
128 key: "tools",
129 surfaces: &[Package, Settings, AgentOverlay, AgentProfile],
130 since: Release(0, 30, 0),
131 gate: Gate::Rules(denies_or_asks),
132 },
133 Gated {
136 key: "effort",
137 surfaces: &[AgentProfile, AgentOverlay, Settings],
138 since: Release(0, 35, 0),
139 gate: Gate::Value(new_effort_request),
140 },
141 Gated {
143 key: "default_effort",
144 surfaces: &[ModelAlias],
145 since: Release(0, 35, 0),
146 gate: Gate::Value(new_default_effort),
147 },
148];
149
150pub struct Used {
152 pub surface: Surface,
153 pub key: String,
154 pub value: Option<String>,
156 pub at: String,
158}
159
160impl Used {
161 pub fn hook(name: &str) -> Self {
163 Self {
164 surface: HookToml,
165 key: "hooks".into(),
166 value: None,
167 at: format!("hook `{name}` using `[hooks]`"),
168 }
169 }
170
171 pub fn profile_effort(agent: &str, label: &str, written: Option<&str>) -> Self {
174 let (value, shown) = match written {
175 Some(text) => (text, format!("\"{text}\"")),
176 None => ("null", "null".to_string()),
177 };
178 Self {
179 surface: AgentProfile,
180 key: "effort".into(),
181 value: Some(value.into()),
182 at: format!("agent `{agent}` {label} = {shown}"),
183 }
184 }
185
186 pub fn profile_rule(agent: &str, rule: &str, action: &str) -> Self {
188 Self {
189 surface: AgentProfile,
190 key: "tools".into(),
191 value: Some(action.into()),
192 at: format!("agent `{agent}` `tools` `{rule}` = \"{action}\""),
193 }
194 }
195}
196
197#[derive(Debug, Clone, Copy, PartialEq, Eq)]
199pub enum Document {
200 Package,
202 Consumer,
204}
205
206pub fn used(document: &toml::Table, kind: Document) -> Vec<Used> {
209 let mut used = Vec::new();
210 let mut scan = |surface: Surface, label: String, table: &toml::Table| {
211 for row in GATED.iter().filter(|row| on(row.surfaces, surface)) {
212 let Some(value) = table.get(row.key) else {
213 continue;
214 };
215 match (row.gate, value) {
216 (Gate::Rules(_), toml::Value::Table(rules)) => {
217 let table = if label.is_empty() {
218 row.key.to_string()
219 } else {
220 format!("{label}.{}", row.key)
221 };
222 for (name, action) in rules {
223 let Some(action) = action.as_str() else {
224 continue;
225 };
226 used.push(Used {
227 surface,
228 key: row.key.into(),
229 value: Some(action.into()),
230 at: format!("`[{table}]` `{name}` = \"{action}\""),
231 });
232 }
233 }
234 (Gate::Rules(_), _) => {}
235 (Gate::Value(_), toml::Value::String(text)) => used.push(Used {
236 surface,
237 key: row.key.into(),
238 value: Some(text.clone()),
239 at: if label.is_empty() {
240 format!("`{} = \"{text}\"`", row.key)
241 } else {
242 format!("`[{label}]` `{} = \"{text}\"`", row.key)
243 },
244 }),
245 (Gate::Value(_), _) => {}
246 (Gate::Present, value) => used.push(Used {
247 surface,
248 key: row.key.into(),
249 value: None,
250 at: match value {
251 toml::Value::Table(_) => format!("`[{}]`", row.key),
252 toml::Value::Integer(_) | toml::Value::Boolean(_) => {
253 format!("`{} = {value}`", row.key)
254 }
255 _ => format!("`{}`", row.key),
256 },
257 }),
258 }
259 }
260 };
261 match kind {
262 Document::Package => {
263 scan(Manifest, String::new(), document);
264 if let Some(package) = document.get("package").and_then(toml::Value::as_table) {
265 scan(Package, "package".into(), package);
266 }
267 scan_aliases(document, &mut scan);
268 }
269 Document::Consumer => {
270 if let Some(settings) = document.get("settings").and_then(toml::Value::as_table) {
271 scan(Settings, "settings".into(), settings);
272 }
273 let agents = document.get("agents").and_then(toml::Value::as_table);
274 for (name, agent) in agents.into_iter().flatten() {
275 if let Some(agent) = agent.as_table() {
276 scan(AgentOverlay, format!("agents.{name}"), agent);
277 scan_policies(AgentOverlay, &format!("agents.{name}"), agent, &mut scan);
278 }
279 }
280 if let Some(settings) = document.get("settings").and_then(toml::Value::as_table) {
281 scan_policies(Settings, "settings", settings, &mut scan);
282 }
283 scan_aliases(document, &mut scan);
284 }
285 }
286 used
287}
288
289#[derive(Debug, Clone, PartialEq, Eq)]
291pub struct Floor {
292 pub since: Release,
293 pub at: String,
294}
295
296impl Floor {
297 pub fn requirement(&self) -> String {
299 format!(">={}", self.since)
300 }
301}
302
303pub fn floor(used: impl IntoIterator<Item = Used>) -> Option<Floor> {
306 let mut floor: Option<Floor> = None;
307 for used in used {
308 let Some(row) = GATED
309 .iter()
310 .find(|row| row.key == used.key && on(row.surfaces, used.surface))
311 else {
312 continue;
313 };
314 let holds = match row.gate {
315 Gate::Present => true,
316 Gate::Rules(holds) | Gate::Value(holds) => used.value.as_deref().is_some_and(holds),
317 };
318 if holds && floor.as_ref().is_none_or(|floor| row.since > floor.since) {
319 floor = Some(Floor {
320 since: row.since,
321 at: used.at,
322 });
323 }
324 }
325 floor
326}
327
328fn scan_aliases(document: &toml::Table, scan: &mut impl FnMut(Surface, String, &toml::Table)) {
330 let aliases = document.get("models").and_then(toml::Value::as_table);
331 for (name, alias) in aliases.into_iter().flatten() {
332 if let Some(alias) = alias.as_table() {
333 scan(ModelAlias, format!("models.{name}"), alias);
334 }
335 }
336}
337
338fn scan_policies(
340 surface: Surface,
341 label: &str,
342 table: &toml::Table,
343 scan: &mut impl FnMut(Surface, String, &toml::Table),
344) {
345 let rules = table.get("model-policies").and_then(toml::Value::as_array);
346 for (index, rule) in rules.into_iter().flatten().enumerate() {
347 if let Some(overrides) = rule.get("override").and_then(toml::Value::as_table) {
348 scan(
349 surface,
350 format!("{label}.model-policies[{index}].override"),
351 overrides,
352 );
353 }
354 }
355}
356
357fn on(surfaces: &[Surface], surface: Surface) -> bool {
358 surfaces.contains(&surface)
359}