speccoder_runtime/verify/
config.rs1use std::time::Duration;
4
5use serde::Deserialize;
6
7pub const FILE: &str = "speccoder.toml";
9
10const DEFAULT_TIMEOUT_SECS: u64 = 600;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum Tier {
16 Fast,
18 Full,
20}
21
22impl Tier {
23 #[must_use]
25 pub const fn as_str(self) -> &'static str {
26 match self {
27 Self::Fast => "fast",
28 Self::Full => "full",
29 }
30 }
31}
32
33#[derive(Debug, Deserialize)]
34#[serde(deny_unknown_fields)]
35struct RawCheck {
36 argv: Vec<String>,
37 cwd: Option<String>,
38 when_exists: Option<String>,
39 tier: Option<Tier>,
40 timeout_secs: Option<u64>,
41 junit: Option<String>,
42 list: Option<Vec<String>>,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct Check {
48 pub name: String,
50 pub argv: Vec<String>,
52 pub cwd: String,
54 pub when_exists: Option<String>,
56 pub tier: Tier,
58 pub timeout: Duration,
60 pub junit: Option<String>,
62 pub list: Option<Vec<String>>,
65}
66
67pub const SECTIONS: &[&str] = &[
69 "checks",
70 "model",
71 "budget",
72 "commands",
73 "git",
74 "acceptance",
75 "tests",
76 "monitor",
77 "converge",
78 "review",
79 "format",
80 "sandbox",
81 "mutation",
82 "executor",
83 "mcp",
84 "spec",
85 "approvals",
86 "attest",
87 "context",
88 "bug",
89 "worktree",
90];
91
92pub fn parse(text: &str) -> Result<Vec<Check>, String> {
94 let table: toml::Table = toml::from_str(text).map_err(|e| e.to_string())?;
95 if table.contains_key("ui") {
100 return Err(format!(
101 "[ui] is a person's setting, not project policy; move it to {}",
102 crate::user_config::path().map_or_else(
103 || "~/.config/speccoder/config.toml".to_owned(),
104 |p| p.display().to_string()
105 )
106 ));
107 }
108 if let Some(unknown) = table.keys().find(|k| !SECTIONS.contains(&k.as_str())) {
109 return Err(format!(
110 "unknown section `[{unknown}]` (known: {})",
111 SECTIONS.join(", ")
112 ));
113 }
114 let Some(checks) = table.get("checks") else {
115 return Ok(Vec::new());
116 };
117 let checks = checks
118 .as_table()
119 .ok_or_else(|| "`checks` must be a table of [checks.<name>] tables".to_owned())?;
120 checks
121 .iter()
122 .map(|(name, value)| {
123 let raw: RawCheck = value
124 .clone()
125 .try_into()
126 .map_err(|e| format!("[checks.{name}]: {e}"))?;
127 if raw.argv.is_empty() || raw.argv[0].is_empty() {
128 return Err(format!("[checks.{name}]: `argv` must name a program"));
129 }
130 Ok(Check {
131 name: name.clone(),
132 argv: raw.argv,
133 cwd: raw.cwd.unwrap_or_else(|| ".".to_owned()),
134 when_exists: raw.when_exists,
135 tier: raw.tier.unwrap_or(Tier::Fast),
136 timeout: Duration::from_secs(raw.timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS)),
137 junit: raw.junit,
138 list: raw
139 .list
140 .filter(|l| l.first().is_some_and(|p| !p.is_empty())),
141 })
142 })
143 .collect()
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149
150 #[test]
151 fn a_misspelled_section_is_an_error_not_ignored() {
152 let e = parse("[checks.t]\nargv = [\"true\"]\n[budgett]\nmax_turns = 3\n").unwrap_err();
153 assert!(e.contains("unknown section `[budgett]`"), "{e}");
154 assert!(parse("[checks.t]\nargv = [\"true\"]\n[sandbox]\nmode = \"off\"\n").is_ok());
155 }
156
157 #[test]
158 fn parses_checks_in_file_order_with_defaults() {
159 let checks = parse(
160 r#"
161[model]
162provider = "anthropic"
163
164[checks.test]
165argv = ["cargo", "nextest", "run"]
166junit = "target/nextest/speccoder/junit.xml"
167when_exists = "Cargo.toml"
168
169[checks.clippy]
170argv = ["cargo", "clippy"]
171tier = "full"
172timeout_secs = 30
173"#,
174 )
175 .unwrap();
176 let names: Vec<_> = checks.iter().map(|c| c.name.as_str()).collect();
177 assert_eq!(names, ["test", "clippy"]);
178 assert_eq!(checks[0].tier, Tier::Fast);
179 assert_eq!(checks[0].cwd, ".");
180 assert_eq!(checks[0].timeout, Duration::from_secs(600));
181 assert_eq!(checks[1].tier, Tier::Full);
182 assert_eq!(checks[1].timeout, Duration::from_secs(30));
183 assert_eq!(checks[0].list, None);
184 let listed = parse("[checks.t]\nargv = [\"x\"]\nlist = [\"x\", \"--list\"]\n").unwrap();
185 assert_eq!(
186 listed[0].list.as_deref(),
187 Some(&["x".to_owned(), "--list".to_owned()][..])
188 );
189 }
190
191 #[test]
192 fn rejects_unknown_keys_and_empty_argv() {
193 let e = parse("[checks.t]\nargv = [\"x\"]\nshell = true\n").unwrap_err();
194 assert!(e.contains("[checks.t]") && e.contains("shell"), "{e}");
195 assert!(
196 parse("[checks.t]\nargv = []\n")
197 .unwrap_err()
198 .contains("argv")
199 );
200 }
201}