1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! The keys a Mars release gates: a file that uses one needs a floor at or above the release that
//! first read it. This lists keys Mars reads today and when they arrived, nothing about keys it
//! no longer reads. A file's version floor is [`floor`] over the keys [`used`] finds in it, plus
//! the uses a caller derives from other files (a hook, a profile's tool rule).
use std::fmt;
/// Where a gated key appears.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Surface {
/// Agent frontmatter.
AgentProfile,
/// `[package]` in `mars.toml`.
Package,
/// A top-level table of a package's `mars.toml`, such as `[plugin]`.
Manifest,
/// `hook.toml`.
HookToml,
/// `[settings]` in `mars.toml`, `mars.local.toml` and the user config.
Settings,
/// `[settings]` in `mars.local.toml` alone, where a key can mean more than in `mars.toml`.
LocalSettings,
/// `[agents.<name>]` in the consumer config files.
AgentOverlay,
/// `[models.<alias>]` in `mars.toml`.
ModelAlias,
}
/// A Mars release.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Release(pub u64, pub u64, pub u64);
impl fmt::Display for Release {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}.{}", self.0, self.1, self.2)
}
}
impl Release {
pub fn version(self) -> semver::Version {
semver::Version::new(self.0, self.1, self.2)
}
}
/// When a file that writes a gated key needs the row's `since`.
#[derive(Debug, Clone, Copy)]
pub enum Gate {
/// The key's presence: an older Mars misreads how the package reads (`schema`, `plugin`).
Present,
/// The key holds tool rules, and the rule's action decides: an older Mars drops the rule,
/// so a restricting one (`deny`, `ask`) must not meet one, and an `allow` loses only a grant.
Rules(fn(&str) -> bool),
/// The key's value decides: an older Mars refuses a value it doesn't know and fails the
/// whole sync, so a file that writes a new value needs the row's `since`.
Value(fn(&str) -> bool),
}
/// A key a Mars release gates: a file that uses it needs a floor at or above `since`.
#[derive(Debug, Clone, Copy)]
pub struct Gated {
pub key: &'static str,
pub surfaces: &'static [Surface],
/// The first Mars release that reads the key. Mars before it ignores the key or misreads
/// it; the row's comment says which.
pub since: Release,
pub gate: Gate,
}
use crate::agent::EffortRequest;
use Surface::*;
fn denies_or_asks(action: &str) -> bool {
matches!(action, "deny" | "ask")
}
/// Effort requests Mars before 0.35.0 refuses. `null` is YAML's spelling of `auto`.
fn new_effort_request(value: &str) -> bool {
matches!(
value.to_ascii_lowercase().as_str(),
"minimal" | "auto" | "null"
)
}
/// `default_effort` spellings Mars before 0.35.0 refuses: it matched `low`, `medium`, `high`,
/// `xhigh`, `max` and `auto` exactly, in lowercase. Mars now reads any case and the new levels.
fn new_default_effort(value: &str) -> bool {
!matches!(value, "low" | "medium" | "high" | "xhigh" | "max" | "auto")
&& EffortRequest::parse(value).is_some()
}
/// The first Mars that honours `min_mars_version`. A floor at or below it protects nothing a
/// `min_mars_version` line could: Mars before it ignores the line.
pub const MIN_MARS_VERSION_SINCE: Release = Release(0, 30, 0);
/// Keys a Mars release gates; the comment on each says what an older Mars does with it.
pub static GATED: &[Gated] = &[
// Mars before 0.20.0 reads a `schema = 1` package as pre-v1 and drops what it cannot place.
Gated {
key: "schema",
surfaces: &[Package],
since: Release(0, 20, 0),
gate: Gate::Present,
},
// Mars before 0.20.0 fails `skill__provider` refs with `skill not found`.
Gated {
key: "uses-providers",
surfaces: &[Package],
since: Release(0, 20, 0),
gate: Gate::Present,
},
// Mars before 0.19.0 drops `[plugin]` when `mars version` rewrites the manifest.
Gated {
key: "plugin",
surfaces: &[Manifest],
since: Release(0, 19, 0),
gate: Gate::Present,
},
// Mars before 0.25.0 rejects `[hooks]` and fails the whole sync.
Gated {
key: "hooks",
surfaces: &[HookToml],
since: Release(0, 25, 0),
gate: Gate::Present,
},
// Tool rule tables. Mars before 0.29.0 ignores them without a word; 0.29.x reads `deny`
// under the older precedence, where a narrower allow can beat it, and fails on `allow` or
// `ask`; 0.30.0 is the first where the strictest rule wins. `allow` is not a restriction:
// a Mars that drops one loses a grant only.
Gated {
key: "tools",
surfaces: &[Package, Settings, AgentOverlay, AgentProfile],
since: Release(0, 30, 0),
gate: Gate::Rules(denies_or_asks),
},
// Mars before 0.39.0 reads a local `targets` as the whole list, not an addition to the
// project's, and under its advised `--force` rewrites the shared lock to match.
Gated {
key: "targets",
surfaces: &[LocalSettings],
since: Release(0, 39, 0),
gate: Gate::Present,
},
// An Agent's `effort`, an overlay's `effort` and a model policy's `override.effort`. Mars
// before 0.35.0 refuses `minimal`, `auto` and `null` and fails the whole sync.
Gated {
key: "effort",
surfaces: &[AgentProfile, AgentOverlay, Settings],
since: Release(0, 35, 0),
gate: Gate::Value(new_effort_request),
},
// A model alias's `default_effort`. Mars before 0.35.0 refuses `none` and `minimal`.
Gated {
key: "default_effort",
surfaces: &[ModelAlias],
since: Release(0, 35, 0),
gate: Gate::Value(new_default_effort),
},
];
/// A key a file uses, as [`floor`] reads it.
pub struct Used {
pub surface: Surface,
pub key: String,
/// The rule's action, for a key whose gate depends on it.
pub value: Option<String>,
/// Where the file uses it, for the message: "`[package.tools]` `bash` = \"deny\"".
pub at: String,
}
impl Used {
/// A portable hook in a `hook.toml`, which an older Mars rejects.
pub fn hook(name: &str) -> Self {
Self {
surface: HookToml,
key: "hooks".into(),
value: None,
at: format!("hook `{name}` using `[hooks]`"),
}
}
/// An effort an Agent's frontmatter writes, as written, named by `label` (`effort`, or a
/// model policy's override): `null` is YAML null, anything else the text of a string.
pub fn profile_effort(agent: &str, label: &str, written: Option<&str>) -> Self {
let (value, shown) = match written {
Some(text) => (text, format!("\"{text}\"")),
None => ("null", "null".to_string()),
};
Self {
surface: AgentProfile,
key: "effort".into(),
value: Some(value.into()),
at: format!("agent `{agent}` {label} = {shown}"),
}
}
/// A tool rule in an agent's frontmatter `tools`.
pub fn profile_rule(agent: &str, rule: &str, action: &str) -> Self {
Self {
surface: AgentProfile,
key: "tools".into(),
value: Some(action.into()),
at: format!("agent `{agent}` `tools` `{rule}` = \"{action}\""),
}
}
}
/// Which file [`used`] reads.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Document {
/// A package's `mars.toml`.
Package,
/// `mars.toml` or the user `config.toml` as a project's configuration.
Consumer,
/// `mars.local.toml`: a consumer file with the keys only it gates.
Local,
}
/// The gated keys `document` uses: every key of a table that has a gated row on that table's
/// surface, and for a key that holds rules, each rule.
pub fn used(document: &toml::Table, kind: Document) -> Vec<Used> {
let mut used = Vec::new();
let mut scan = |surface: Surface, label: String, table: &toml::Table| {
for row in GATED.iter().filter(|row| on(row.surfaces, surface)) {
let Some(value) = table.get(row.key) else {
continue;
};
match (row.gate, value) {
(Gate::Rules(_), toml::Value::Table(rules)) => {
let table = if label.is_empty() {
row.key.to_string()
} else {
format!("{label}.{}", row.key)
};
for (name, action) in rules {
let Some(action) = action.as_str() else {
continue;
};
used.push(Used {
surface,
key: row.key.into(),
value: Some(action.into()),
at: format!("`[{table}]` `{name}` = \"{action}\""),
});
}
}
(Gate::Rules(_), _) => {}
(Gate::Value(_), toml::Value::String(text)) => used.push(Used {
surface,
key: row.key.into(),
value: Some(text.clone()),
at: if label.is_empty() {
format!("`{} = \"{text}\"`", row.key)
} else {
format!("`[{label}]` `{} = \"{text}\"`", row.key)
},
}),
(Gate::Value(_), _) => {}
(Gate::Present, value) => used.push(Used {
surface,
key: row.key.into(),
value: None,
at: match value {
toml::Value::Table(_) => format!("`[{}]`", row.key),
toml::Value::Integer(_) | toml::Value::Boolean(_) => {
format!("`{} = {value}`", row.key)
}
_ => format!("`{}`", row.key),
},
}),
}
}
};
match kind {
Document::Package => {
scan(Manifest, String::new(), document);
if let Some(package) = document.get("package").and_then(toml::Value::as_table) {
scan(Package, "package".into(), package);
}
scan_aliases(document, &mut scan);
}
Document::Consumer | Document::Local => {
if let Some(settings) = document.get("settings").and_then(toml::Value::as_table) {
scan(Settings, "settings".into(), settings);
if kind == Document::Local {
scan(LocalSettings, "settings".into(), settings);
}
}
let agents = document.get("agents").and_then(toml::Value::as_table);
for (name, agent) in agents.into_iter().flatten() {
if let Some(agent) = agent.as_table() {
scan(AgentOverlay, format!("agents.{name}"), agent);
scan_policies(AgentOverlay, &format!("agents.{name}"), agent, &mut scan);
}
}
if let Some(settings) = document.get("settings").and_then(toml::Value::as_table) {
scan_policies(Settings, "settings", settings, &mut scan);
}
scan_aliases(document, &mut scan);
}
}
used
}
/// The Mars a file needs, and what in it asks for that.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Floor {
pub since: Release,
pub at: String,
}
impl Floor {
/// The requirement that states it: `>=0.30.0`.
pub fn requirement(&self) -> String {
format!(">={}", self.since)
}
}
/// The highest `since` among the keys used whose row says an older Mars would misread them.
/// The first of equal ones names the floor.
pub fn floor(used: impl IntoIterator<Item = Used>) -> Option<Floor> {
let mut floor: Option<Floor> = None;
for used in used {
let Some(row) = GATED
.iter()
.find(|row| row.key == used.key && on(row.surfaces, used.surface))
else {
continue;
};
let holds = match row.gate {
Gate::Present => true,
Gate::Rules(holds) | Gate::Value(holds) => used.value.as_deref().is_some_and(holds),
};
if holds && floor.as_ref().is_none_or(|floor| row.since > floor.since) {
floor = Some(Floor {
since: row.since,
at: used.at,
});
}
}
floor
}
/// Each `[models.<alias>]` table.
fn scan_aliases(document: &toml::Table, scan: &mut impl FnMut(Surface, String, &toml::Table)) {
let aliases = document.get("models").and_then(toml::Value::as_table);
for (name, alias) in aliases.into_iter().flatten() {
if let Some(alias) = alias.as_table() {
scan(ModelAlias, format!("models.{name}"), alias);
}
}
}
/// The `override` table of each `model-policies` rule in `table`.
fn scan_policies(
surface: Surface,
label: &str,
table: &toml::Table,
scan: &mut impl FnMut(Surface, String, &toml::Table),
) {
let rules = table.get("model-policies").and_then(toml::Value::as_array);
for (index, rule) in rules.into_iter().flatten().enumerate() {
if let Some(overrides) = rule.get("override").and_then(toml::Value::as_table) {
scan(
surface,
format!("{label}.model-policies[{index}].override"),
overrides,
);
}
}
}
fn on(surfaces: &[Surface], surface: Surface) -> bool {
surfaces.contains(&surface)
}