leviath_cli/lint/mod.rs
1//! Blueprint lint: the checks [`Blueprint::validate`] deliberately does not make.
2//!
3//! `Blueprint::validate` answers "is this manifest structurally coherent" - the
4//! layout fits, the graph resolves, fan-out wiring points at real stages. It
5//! says nothing about the fields whose *absence* quietly changes what a run
6//! does, and those are what actually bite:
7//!
8//! - a stage with no `[stages.<name>.model]` table parses fine, because the
9//! parser substitutes a default, and then runs on whatever the user's default
10//! provider happens to be
11//! - an agent-level `[model]` block is never read at all, so the author's model
12//! choice is discarded silently
13//! - a typo in `available_tools` matches nothing, and the stage just advertises
14//! one tool fewer - the model is told the tool does not exist
15//! - an autonomous stage granting `ask_user_text` parks in `WaitingInput` the
16//! first time it asks, with nobody there to answer
17//!
18//! Each of those is invisible on inspection and shows up hours later as a stuck
19//! run. This module names them at author time instead.
20//!
21//! Questions about what the author *declared* ("is there a `mode` key?") are
22//! answered from the manifest text, not from the parsed [`Blueprint`]: by then
23//! the parser has already filled in its defaults, and asking the struct cannot
24//! tell "wrote `autonomous`" apart from "wrote nothing".
25//!
26//! [`Blueprint::validate`]: leviath_core::Blueprint::validate
27
28use std::collections::{HashMap, HashSet};
29use std::path::Path;
30
31use leviath_core::Blueprint;
32use leviath_core::blueprint::StageMode;
33use leviath_runtime::dynamic_interaction::BLOCKING_INTERACTION_TOOLS;
34use leviath_tools::canonical_tool_name;
35use serde::{Deserialize, Serialize};
36
37/// How much a finding matters. Only [`LintSeverity::Error`] fails
38/// `lev validate`; warnings are printed and the command still exits zero
39/// (unless `--deny-warnings` is passed); notes never fail anything.
40///
41/// Declared worst-first so sorting by it groups the report.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
43#[serde(rename_all = "lowercase")]
44pub enum LintSeverity {
45 /// The manifest says something that cannot be what the author meant - a
46 /// tool name matching nothing, a permission for a tool the stage never
47 /// granted.
48 Error,
49 /// The manifest leaves a decision to a default the author may not know
50 /// about.
51 Warning,
52 /// Nothing is wrong; the blueprint is doing something worth knowing before
53 /// you run it, like reaching outside its workdir or running a shell command
54 /// at spawn. A note must never fail a build, so `--deny-warnings` skips it.
55 Note,
56}
57
58impl LintSeverity {
59 /// Fixed-width label for the report, so the messages line up.
60 pub fn label(self) -> &'static str {
61 match self {
62 Self::Error => "ERR ",
63 Self::Warning => "WARN",
64 Self::Note => "NOTE",
65 }
66 }
67}
68
69/// One thing worth telling the author about.
70///
71/// Serialize only: `code` is a `&'static str` pointing at a literal in this
72/// file, which no deserializer can produce.
73#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
74pub struct LintFinding {
75 /// How much this matters, and therefore whether it fails the check.
76 pub severity: LintSeverity,
77 /// Stable slug (`"unknown-tool"`), so a finding can be referenced in an
78 /// issue or grepped for in daemon logs without quoting prose.
79 pub code: &'static str,
80 /// The stage it belongs to, when it belongs to one.
81 pub stage: Option<String>,
82 /// What is wrong.
83 pub message: String,
84 /// What to do about it. Rendered on its own indented line.
85 pub fix: Option<String>,
86}
87
88impl LintFinding {
89 fn new(severity: LintSeverity, code: &'static str, message: String) -> Self {
90 Self {
91 severity,
92 code,
93 stage: None,
94 message,
95 fix: None,
96 }
97 }
98
99 fn in_stage(mut self, stage: &str) -> Self {
100 self.stage = Some(stage.to_string());
101 self
102 }
103
104 fn with_fix(mut self, fix: impl Into<String>) -> Self {
105 self.fix = Some(fix.into());
106 self
107 }
108
109 /// Whether this finding should fail the command.
110 pub fn is_error(&self) -> bool {
111 self.severity == LintSeverity::Error
112 }
113
114 /// One-line rendering for a log record: `stage 'x': message`.
115 pub fn one_line(&self) -> String {
116 match &self.stage {
117 Some(stage) => format!("stage '{stage}': {}", self.message),
118 None => self.message.clone(),
119 }
120 }
121}
122
123/// Facts about the machine the blueprint will run on, which the manifest alone
124/// cannot supply.
125///
126/// Every field is "unknown" when empty/`None`, and an unknown field skips its
127/// check entirely rather than guessing. A linter that cannot see the installed
128/// MCP servers must not claim their tools do not exist.
129#[derive(Debug, Default, Clone)]
130pub struct LintEnv {
131 /// Every tool name a manifest may legally write: canonical built-ins, their
132 /// aliases, the sub-agent tools, this agent's own `tools/*.rhai`, and any
133 /// MCP tools already resolved. Empty skips the unknown-tool check.
134 pub known_tools: HashSet<String>,
135
136 /// `(provider, model)` rows for providers whose catalog is closed enough to
137 /// check against. A provider with no row here is not checked at all, which
138 /// is what keeps open catalogs (Ollama, OpenRouter, script providers) from
139 /// producing noise.
140 pub known_models: Vec<(String, String)>,
141
142 /// The providers the blueprint names that this install can actually reach,
143 /// as answered by `ProviderRegistry::has`. `None` means nobody asked, so
144 /// the check is skipped. Resolution lives with the caller because script
145 /// providers are loaded on demand and cannot be enumerated up front.
146 pub available_providers: Option<HashSet<String>>,
147
148 /// Which of the blueprint's `[read_paths]` this install's config grants.
149 /// `None` means nobody asked (the daemon's offline lint), in which case the
150 /// check only says that a declaration needs granting. `Some(Err(..))` is a
151 /// grant list of the user's own that will not compile.
152 pub read_paths: Option<Result<crate::read_path_report::GrantReport, String>>,
153
154 /// Whether this install's config honours the blueprint's own
155 /// `[safe_commands]`. `None` means nobody asked (the daemon's offline
156 /// lint), in which case the check only says the declaration needs granting.
157 ///
158 /// A bool rather than a report: unlike read paths, where *which* entries are
159 /// granted is the interesting part, a safe-commands block is honoured whole
160 /// or not at all.
161 pub safe_commands_granted: Option<bool>,
162}
163
164impl LintEnv {
165 /// Everything that can be known without touching the user's config: the
166 /// built-in tools (aliases included), the sub-agent tools, the script tools
167 /// in `agent_dir/tools` and the global tools directory, and the model
168 /// catalogs this build ships.
169 ///
170 /// This is what the daemon lints against at spawn. It deliberately leaves
171 /// `available_providers` unset: the daemon already fails a spawn outright
172 /// when no listed provider is registered, so re-deriving that here would
173 /// cost a registry build per agent to say something the spawn will say
174 /// louder a moment later.
175 pub fn offline(agent_dir: &Path) -> Self {
176 // The four discovery rules live in `tool_inventory` rather than here,
177 // because `GET /api/tools` has to answer the same question and two
178 // copies of "where does a tool come from" would not have stayed equal.
179 // The lint wants only the names; the endpoint wants the sources too.
180 let known_tools =
181 crate::tool_inventory::ToolInventory::discover(Some(agent_dir), None).names();
182
183 Self {
184 known_tools,
185 known_models: crate::commands::models::closed_catalog_models(),
186 available_providers: None,
187 read_paths: None,
188 safe_commands_granted: None,
189 }
190 }
191
192 /// Add the answer to "can this install reach the providers the blueprint
193 /// names", asked of the same registry the runtime resolves stages against
194 /// so a script provider counts exactly when it would really load.
195 pub fn with_providers(mut self, blueprint: &Blueprint, config: &crate::config::Config) -> Self {
196 let registry = crate::commands::run::build_provider_registry_from_config(config);
197 self.available_providers = Some(
198 blueprint
199 .stages
200 .iter()
201 .flat_map(|s| s.model.models.iter())
202 .map(|e| e.provider.clone())
203 .filter(|p| registry.as_ref().is_ok_and(|r| r.has(p)))
204 .collect(),
205 );
206 self
207 }
208
209 /// Add the answer to "does this install's config grant what the blueprint
210 /// declares under `[read_paths]`", per entry.
211 ///
212 /// Separate from [`Self::with_providers`] because it needs a workdir:
213 /// relative entries resolve against the one a run would use, which for a
214 /// command run outside a run is the directory it was invoked from.
215 pub fn with_read_paths(
216 mut self,
217 blueprint: &Blueprint,
218 config: &crate::config::Config,
219 workdir: &Path,
220 ) -> Self {
221 self.read_paths = crate::read_path_report::build(blueprint, config, workdir);
222 // Asked here rather than in its own builder: both answers come from the
223 // same config, and a caller that has one always has the other.
224 self.safe_commands_granted = Some(
225 config.security.allow_blueprint_safe_commands
226 || config
227 .agent_safe_commands
228 .get(&blueprint.name)
229 .is_some_and(|a| a.allow_blueprint),
230 );
231 self
232 }
233}
234
235/// Lint `blueprint`, which was parsed from `content`.
236///
237/// The two arguments describe the same manifest: `blueprint` for what the
238/// engine will do with it, `content` for what the author actually wrote.
239pub fn lint_manifest(content: &str, blueprint: &Blueprint, env: &LintEnv) -> Vec<LintFinding> {
240 let declared = Declared::from_text(content);
241 let mut findings = Vec::new();
242
243 if declared.agent_model_block {
244 findings.push(
245 LintFinding::new(
246 LintSeverity::Warning,
247 "agent-model-block-ignored",
248 "the top-level [model] block is not read by anything: model \
249 selection is per stage"
250 .to_string(),
251 )
252 .with_fix("move it into each [stages.<name>.model] that needs it"),
253 );
254 }
255
256 findings.extend(lint_dropped_seeds(&declared, blueprint));
257 findings.extend(lint_command_seeds(blueprint));
258 findings.extend(lint_read_paths(blueprint, env));
259 findings.extend(lint_safe_commands(blueprint, env));
260 findings.extend(lint_held_checkpoints(blueprint));
261 findings.extend(lint_graph(blueprint));
262 findings.extend(lint_output_reachable(blueprint));
263 findings.extend(lint_dead_end_possible(blueprint));
264 findings.extend(lint_compacted_deliverables(blueprint));
265
266 let agent_permissions = blueprint.agent_tool_permissions();
267
268 for stage in &blueprint.stages {
269 let keys = declared.stage(&stage.name);
270 findings.extend(lint_declarations(stage, keys));
271 findings.extend(lint_tools(stage, env));
272 findings.extend(lint_blocking_tools(stage));
273 findings.extend(lint_tool_policies(stage, &agent_permissions));
274 findings.extend(lint_models(stage, env));
275 findings.extend(lint_output_stage(stage));
276 }
277
278 // Worst first, stable within a severity so the order a check ran in is the
279 // order its findings read in.
280 findings.sort_by_key(|f| f.severity);
281 findings
282}
283
284/// A region wrote a `seed` the parser could not read, so it has none.
285///
286/// `parse_region_seed` returns `None` for a seed table with no recognized key
287/// and for a seed that is neither a string nor a table, and the region then
288/// simply starts empty. That is deliberate - an unknown key is not worth
289/// rejecting a whole manifest over - but it is invisible, and a one-character
290/// typo (`caller_input` for `caller`) reads exactly like a working blueprint
291/// until an agent answers a question it was never given. This is the check that
292/// says so.
293fn lint_dropped_seeds(declared: &Declared, blueprint: &Blueprint) -> Vec<LintFinding> {
294 declared
295 .seeded_regions
296 .iter()
297 .filter(|name| {
298 blueprint
299 .context_layout
300 .get_region(name)
301 .is_some_and(|r| r.seed.is_none())
302 })
303 .map(|name| {
304 LintFinding::new(
305 LintSeverity::Warning,
306 "region-seed-not-understood",
307 format!(
308 "region '{name}' declares a seed that isn't one of the \
309 recognized forms, so it is ignored and the region starts empty"
310 ),
311 )
312 .with_fix(
313 "use a string (the caller input key), or one of \
314 { caller = }, { literal = }, { files = }, { glob = }, \
315 { rhai = }, { command = }",
316 )
317 })
318 .collect()
319}
320
321// ─── Declared keys ────────────────────────────────────────────────────────────
322
323/// Which optional keys the manifest text actually writes, per stage, plus the
324/// one agent-level block that is silently discarded.
325#[derive(Debug, Default)]
326struct Declared {
327 /// A top-level `[model]` table exists. Nothing reads it.
328 agent_model_block: bool,
329 /// Regions whose text writes a `seed` key, whatever its shape. Compared
330 /// against the parsed seed to catch the ones the parser threw away.
331 seeded_regions: Vec<String>,
332 /// Per stage name, the keys that stage wrote.
333 stages: HashMap<String, StageKeys>,
334 /// The manifest text could not be re-read. Every key is then reported as
335 /// declared, so an unreadable manifest produces no declaration warnings
336 /// rather than a full set of false ones.
337 opaque: bool,
338}
339
340#[derive(Debug, Default, Clone, Copy)]
341struct StageKeys {
342 mode: bool,
343 model: bool,
344}
345
346impl Declared {
347 fn from_text(content: &str) -> Self {
348 // `toml::from_str` and not `str::parse`: the latter deserializes a bare
349 // TOML *value*, not a document, and rejects every real manifest.
350 let Ok(root) = toml::from_str::<toml::Table>(content) else {
351 return Self {
352 opaque: true,
353 ..Self::default()
354 };
355 };
356 let agent_model_block = root.get("model").is_some_and(toml::Value::is_table);
357 // Both region spellings - inline `name = { seed = ... }` under
358 // `[context.regions]` and a `[context.regions.name]` section - land here
359 // as the same nested table, so one path covers both.
360 let seeded_regions = root
361 .get("context")
362 .and_then(toml::Value::as_table)
363 .and_then(|c| c.get("regions"))
364 .and_then(toml::Value::as_table)
365 .map(|regions| {
366 regions
367 .iter()
368 .filter(|(_, body)| body.get("seed").is_some())
369 .map(|(name, _)| name.clone())
370 .collect()
371 })
372 .unwrap_or_default();
373 let stages = root
374 .get("stages")
375 .and_then(toml::Value::as_table)
376 .map(|t| {
377 t.iter()
378 .map(|(name, body)| {
379 (
380 name.clone(),
381 StageKeys {
382 mode: body.get("mode").is_some(),
383 model: body.get("model").is_some(),
384 },
385 )
386 })
387 .collect()
388 })
389 .unwrap_or_default();
390 Self {
391 agent_model_block,
392 seeded_regions,
393 stages,
394 opaque: false,
395 }
396 }
397
398 /// What `stage` declared. An unreadable manifest, or a stage the text has
399 /// no entry for, reports everything as declared so nothing is warned about.
400 fn stage(&self, stage: &str) -> StageKeys {
401 if self.opaque {
402 return StageKeys {
403 mode: true,
404 model: true,
405 };
406 }
407 self.stages.get(stage).copied().unwrap_or(StageKeys {
408 mode: true,
409 model: true,
410 })
411 }
412}
413
414// ─── Checks ───────────────────────────────────────────────────────────────────
415
416// The checks themselves, one module per question they answer. Imported rather
417// than re-exported: `lint_manifest` is the only caller and the only entry point
418// anyone outside this module needs, so the individual checks stay internal.
419mod checks;
420use checks::*;
421mod security;
422use security::*;
423
424#[cfg(test)]
425mod tests;