1use serde::Deserialize;
4
5#[derive(Debug, Default, Deserialize)]
6#[serde(deny_unknown_fields)]
7struct RawBudget {
8 max_turns: Option<u32>,
9 max_tool_calls: Option<u32>,
10 max_attempts: Option<u32>,
11 best_of: Option<u32>,
12}
13
14#[derive(Debug, Default, Deserialize)]
15#[serde(deny_unknown_fields)]
16struct RawBug {
17 reproduce_tries: Option<u32>,
18 test: Option<Vec<String>>,
19}
20
21#[derive(Debug, Default, Deserialize)]
22#[serde(deny_unknown_fields)]
23struct RawAcceptance {
24 generate: Option<bool>,
25 held_out: Option<bool>,
26 strength: Option<bool>,
27 min_strength: Option<f64>,
28 near_miss: Option<u32>,
29 permitted: Option<u32>,
30 strength_timeout_secs: Option<u64>,
31}
32
33#[derive(Debug, Clone, PartialEq)]
37pub struct StrengthConfig {
38 pub enabled: bool,
40 pub min: Option<f64>,
42 pub near_miss: u32,
44 pub permitted: u32,
47 pub timeout: std::time::Duration,
49}
50
51impl Eq for StrengthConfig {}
53
54impl Default for StrengthConfig {
55 fn default() -> Self {
56 Self {
57 enabled: true,
58 min: None,
59 near_miss: 2,
60 permitted: 1,
61 timeout: std::time::Duration::from_secs(900),
62 }
63 }
64}
65
66#[derive(Debug, Default, Deserialize)]
67#[serde(deny_unknown_fields)]
68struct RawFormat {
69 argv: Vec<String>,
70}
71
72#[derive(Debug, Default, Deserialize)]
73#[serde(deny_unknown_fields)]
74struct RawTests {
75 frozen: Option<String>,
76}
77
78#[derive(Debug, Default, Deserialize)]
79#[serde(deny_unknown_fields)]
80struct RawMonitor {
81 enabled: Option<bool>,
82}
83
84#[derive(Debug, Default, Deserialize)]
85#[serde(deny_unknown_fields)]
86struct RawContext {
87 instructions: Option<Vec<String>>,
88}
89
90#[derive(Debug, Default, Deserialize)]
91#[serde(deny_unknown_fields)]
92struct RawConverge {
93 max_rounds: Option<u32>,
94}
95
96#[derive(Debug, Default, Deserialize)]
97#[serde(deny_unknown_fields)]
98struct RawReview {
99 required: Option<bool>,
100 oracle_samples: Option<i64>,
101}
102
103#[derive(Debug, Default, Deserialize)]
104#[serde(deny_unknown_fields)]
105struct RawAttest {
106 enabled: Option<bool>,
107 signer: Option<String>,
108 sign_at: Option<String>,
109 sigstore: Option<RawSigstore>,
110}
111
112#[derive(Debug, Default, Deserialize)]
113#[serde(deny_unknown_fields)]
114struct RawSigstore {
115 identity: Option<String>,
116 issuer: Option<String>,
117}
118
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
121pub enum Signer {
122 Git,
124 Sigstore,
126 None,
128}
129
130impl Signer {
131 #[must_use]
133 pub const fn word(self) -> &'static str {
134 match self {
135 Self::Git => "git",
136 Self::Sigstore => "sigstore",
137 Self::None => "none",
138 }
139 }
140}
141
142#[derive(Debug, Clone, PartialEq, Eq)]
144pub struct AttestConfig {
145 pub enabled: bool,
147 pub signer: Signer,
149 pub sign_later: bool,
151 pub sigstore_identity: Option<String>,
154 pub sigstore_issuer: Option<String>,
156}
157
158impl Default for AttestConfig {
159 fn default() -> Self {
160 Self {
161 enabled: true,
162 signer: Signer::Git,
163 sign_later: false,
164 sigstore_identity: None,
165 sigstore_issuer: None,
166 }
167 }
168}
169
170#[derive(Debug, Default, Deserialize)]
171#[serde(deny_unknown_fields)]
172struct RawApprovals {
173 wait: Option<String>,
174 budget: Option<i64>,
175}
176
177#[derive(Debug, Default, Deserialize)]
178#[serde(deny_unknown_fields)]
179struct RawGit {
180 commit: Option<String>,
181}
182
183#[derive(Debug, Default, Deserialize)]
184#[serde(deny_unknown_fields)]
185struct RawCommands {
186 #[serde(default)]
187 allow: Vec<Vec<String>>,
188 #[serde(default)]
189 allow_prefix: Vec<Vec<String>>,
190}
191
192pub const DEFAULT_BUDGET: u32 = 12;
194
195#[derive(Debug, Clone, PartialEq, Eq)]
197pub struct RunConfig {
198 pub max_turns: u32,
200 pub max_tool_calls: u32,
202 pub max_attempts: u32,
204 pub best_of: u32,
206 pub allow: Vec<Vec<String>>,
208 pub allow_prefix: Vec<Vec<String>>,
211 pub approval_budget: u32,
214 pub commit_per_task: bool,
216 pub acceptance: bool,
218 pub held_out: bool,
221 pub strength: StrengthConfig,
223 pub max_converge_rounds: u32,
225 pub monitor: bool,
227 pub review_required: bool,
230 pub oracle_samples: u8,
234 pub frozen_additions: bool,
237 pub format: Option<Vec<String>>,
240 pub approval_wait: std::time::Duration,
243 pub attest: AttestConfig,
245 pub instructions: Vec<String>,
248 pub reproduce_tries: u32,
250 pub bug_test: Option<Vec<String>>,
253}
254
255impl Default for RunConfig {
256 fn default() -> Self {
257 Self {
258 max_turns: 30,
259 max_tool_calls: 100,
260 max_attempts: 2,
261 best_of: 1,
262 allow: Vec::new(),
263 allow_prefix: Vec::new(),
264 approval_budget: DEFAULT_BUDGET,
265 commit_per_task: true,
266 acceptance: true,
267 held_out: true,
268 strength: StrengthConfig::default(),
269 max_converge_rounds: 3,
270 monitor: false,
271 review_required: false,
272 oracle_samples: 2,
273 frozen_additions: false,
274 format: None,
275 approval_wait: std::time::Duration::from_secs(7 * 24 * 3600),
276 attest: AttestConfig::default(),
277 instructions: vec!["AGENTS.md".to_owned()],
278 reproduce_tries: speccoder_core::bug::DEFAULT_TRIES,
279 bug_test: None,
280 }
281 }
282}
283
284fn wait(text: &str) -> Result<std::time::Duration, String> {
286 let text = text.trim();
287 let (n, unit) = text.split_at(text.char_indices().last().map_or(0, |(i, _)| i));
289 let secs = match unit {
290 "m" => 60,
291 "h" => 3600,
292 "d" => 86_400,
293 _ => 0,
294 };
295 match n.parse::<u64>() {
296 Ok(n) if n > 0 && secs > 0 => Ok(std::time::Duration::from_secs(n.saturating_mul(secs))),
297 _ => Err(format!(
298 "[approvals]: wait = \"{text}\" (a positive number of m, h or d, e.g. \"7d\")"
299 )),
300 }
301}
302
303pub fn parse(text: &str) -> Result<RunConfig, String> {
305 let table: toml::Table = toml::from_str(text).map_err(|e| e.to_string())?;
306 let budget: RawBudget = match table.get("budget") {
307 Some(v) => v.clone().try_into().map_err(|e| format!("[budget]: {e}"))?,
308 None => RawBudget::default(),
309 };
310 let commands: RawCommands = match table.get("commands") {
311 Some(v) => v
312 .clone()
313 .try_into()
314 .map_err(|e| format!("[commands]: {e}"))?,
315 None => RawCommands::default(),
316 };
317 let git: RawGit = match table.get("git") {
318 Some(v) => v.clone().try_into().map_err(|e| format!("[git]: {e}"))?,
319 None => RawGit::default(),
320 };
321 let commit_per_task = match git.commit.as_deref() {
322 None | Some("per-task") => true,
323 Some("none") => false,
324 Some(other) => return Err(format!("[git]: commit = \"{other}\" (per-task or none)")),
325 };
326 let acceptance: RawAcceptance = match table.get("acceptance") {
327 Some(v) => v
328 .clone()
329 .try_into()
330 .map_err(|e| format!("[acceptance]: {e}"))?,
331 None => RawAcceptance::default(),
332 };
333 let converge: RawConverge = match table.get("converge") {
334 Some(v) => v
335 .clone()
336 .try_into()
337 .map_err(|e| format!("[converge]: {e}"))?,
338 None => RawConverge::default(),
339 };
340 let review: RawReview = match table.get("review") {
341 Some(v) => v.clone().try_into().map_err(|e| format!("[review]: {e}"))?,
342 None => RawReview::default(),
343 };
344 let monitor: RawMonitor = match table.get("monitor") {
345 Some(v) => v
346 .clone()
347 .try_into()
348 .map_err(|e| format!("[monitor]: {e}"))?,
349 None => RawMonitor::default(),
350 };
351 let tests: RawTests = match table.get("tests") {
352 Some(v) => v.clone().try_into().map_err(|e| format!("[tests]: {e}"))?,
353 None => RawTests::default(),
354 };
355 let frozen_additions = match tests.frozen.as_deref() {
356 None | Some("ask") => false,
357 Some("additions") => true,
358 Some(other) => {
359 return Err(format!("[tests]: frozen = \"{other}\" (ask or additions)"));
360 }
361 };
362 let format: Option<RawFormat> = match table.get("format") {
363 Some(v) => Some(v.clone().try_into().map_err(|e| format!("[format]: {e}"))?),
364 None => None,
365 };
366 if format
367 .as_ref()
368 .is_some_and(|f| f.argv.first().is_none_or(String::is_empty))
369 {
370 return Err("[format]: `argv` must name a program".to_owned());
371 }
372 let approvals: RawApprovals = match table.get("approvals") {
373 Some(v) => v
374 .clone()
375 .try_into()
376 .map_err(|e| format!("[approvals]: {e}"))?,
377 None => RawApprovals::default(),
378 };
379 let raw_attest: RawAttest = match table.get("attest") {
380 Some(v) => v.clone().try_into().map_err(|e| format!("[attest]: {e}"))?,
381 None => RawAttest::default(),
382 };
383 let attest = AttestConfig {
384 enabled: raw_attest.enabled.unwrap_or(true),
385 signer: match raw_attest.signer.as_deref() {
386 None | Some("git") => Signer::Git,
387 Some("sigstore") => Signer::Sigstore,
388 Some("none") => Signer::None,
389 Some(other) => {
390 return Err(format!(
391 "[attest]: signer = \"{other}\" (git, sigstore or none)"
392 ));
393 }
394 },
395 sign_later: match raw_attest.sign_at.as_deref() {
396 None | Some("tick") => false,
397 Some("later") => true,
398 Some(other) => {
399 return Err(format!("[attest]: sign_at = \"{other}\" (tick or later)"));
400 }
401 },
402 sigstore_identity: raw_attest
403 .sigstore
404 .as_ref()
405 .and_then(|s| s.identity.clone()),
406 sigstore_issuer: raw_attest.sigstore.and_then(|s| s.issuer),
407 };
408 let context: RawContext = match table.get("context") {
409 Some(v) => v
410 .clone()
411 .try_into()
412 .map_err(|e| format!("[context]: {e}"))?,
413 None => RawContext::default(),
414 };
415 if let Some(bad) = context.instructions.iter().flatten().find(|n| {
416 n.is_empty() || n.contains('/') || n.contains('\\') || matches!(n.as_str(), "." | "..")
417 }) {
418 return Err(format!(
419 "[context]: instructions = \"{bad}\": a file name such as AGENTS.md, without a directory"
420 ));
421 }
422 let d = RunConfig::default();
423 let strength = {
424 let ds = StrengthConfig::default();
425 if let Some(m) = acceptance.min_strength
426 && !(0.0..=1.0).contains(&m)
427 {
428 return Err(format!(
429 "[acceptance]: min_strength = {m} (a share from 0.0 to 1.0)"
430 ));
431 }
432 let near_miss = acceptance.near_miss.unwrap_or(ds.near_miss);
433 if near_miss > 3 {
434 return Err(format!("[acceptance]: near_miss = {near_miss} (0 to 3)"));
435 }
436 let permitted = acceptance.permitted.unwrap_or(ds.permitted);
437 if permitted > 2 {
438 return Err(format!("[acceptance]: permitted = {permitted} (0 to 2)"));
439 }
440 StrengthConfig {
441 enabled: acceptance.strength.unwrap_or(ds.enabled),
442 min: acceptance.min_strength,
443 near_miss,
444 permitted,
445 timeout: acceptance
446 .strength_timeout_secs
447 .map_or(ds.timeout, |s| std::time::Duration::from_secs(s.max(1))),
448 }
449 };
450 let bug: RawBug = match table.get("bug") {
451 Some(v) => v.clone().try_into().map_err(|e| format!("[bug]: {e}"))?,
452 None => RawBug::default(),
453 };
454 let reproduce_tries = bug.reproduce_tries.unwrap_or(d.reproduce_tries);
455 if !(1..=speccoder_core::bug::MAX_TRIES).contains(&reproduce_tries) {
456 return Err(format!(
457 "[bug]: reproduce_tries = {reproduce_tries} (1 to {})",
458 speccoder_core::bug::MAX_TRIES
459 ));
460 }
461 if bug.test.as_ref().is_some_and(Vec::is_empty) {
462 return Err("[bug]: test = [] (a program and its arguments)".to_owned());
463 }
464 let approval_wait = match approvals.wait.as_deref() {
465 Some(w) => wait(w)?,
466 None => d.approval_wait,
467 };
468 let approval_budget = match approvals.budget {
469 None => d.approval_budget,
470 Some(n) => u32::try_from(n)
471 .ok()
472 .filter(|n| *n <= 100)
473 .ok_or_else(|| format!("[approvals]: budget = {n} (a whole number from 0 to 100)"))?,
474 };
475 let oracle_samples = match review.oracle_samples {
476 None => d.oracle_samples,
477 Some(n @ 1..=2) => u8::try_from(n).unwrap_or(d.oracle_samples),
478 Some(n) => return Err(format!("[review]: oracle_samples = {n} (1 or 2)")),
479 };
480 if let Some(why) = commands
481 .allow_prefix
482 .iter()
483 .find_map(|p| crate::tools::policy::prefix_refusal(p))
484 {
485 return Err(format!("[commands] allow_prefix: {why}"));
486 }
487 Ok(RunConfig {
488 max_turns: budget.max_turns.unwrap_or(d.max_turns).max(1),
489 max_tool_calls: budget.max_tool_calls.unwrap_or(d.max_tool_calls),
490 max_attempts: budget.max_attempts.unwrap_or(d.max_attempts).max(1),
491 best_of: budget.best_of.unwrap_or(d.best_of).clamp(1, 10),
492 allow: commands
493 .allow
494 .into_iter()
495 .filter(|p| !p.is_empty())
496 .collect(),
497 allow_prefix: commands.allow_prefix,
498 approval_budget,
499 commit_per_task,
500 acceptance: acceptance.generate.unwrap_or(true),
501 held_out: acceptance.held_out.unwrap_or(true),
502 strength,
503 max_converge_rounds: converge.max_rounds.unwrap_or(d.max_converge_rounds),
504 monitor: monitor.enabled.unwrap_or(false),
505 review_required: review.required.unwrap_or(false),
506 oracle_samples,
507 frozen_additions,
508 format: format.map(|f| f.argv),
509 approval_wait,
510 attest,
511 instructions: context.instructions.unwrap_or(d.instructions),
512 reproduce_tries,
513 bug_test: bug.test,
514 })
515}
516
517#[cfg(test)]
518mod tests {
519 use super::*;
520
521 #[test]
522 fn bug_tries_default_to_three_and_stay_within_five() {
523 assert_eq!(parse("").unwrap().reproduce_tries, 3);
524 assert_eq!(
525 parse("[bug]\nreproduce_tries = 5\n")
526 .unwrap()
527 .reproduce_tries,
528 5
529 );
530 assert!(parse("[bug]\nreproduce_tries = 6\n").is_err());
531 assert!(parse("[bug]\nreproduce_tries = 0\n").is_err());
532 assert!(parse("[bug]\ntries = 2\n").is_err());
533 let c = parse("[bug]\ntest = [\"sh\", \"t.sh\", \"{file}\", \"{name}\"]\n").unwrap();
534 assert_eq!(c.bug_test.unwrap_or_default().len(), 4);
535 }
536
537 #[test]
538 fn agents_md_is_read_unless_configured_otherwise() {
539 assert_eq!(parse("").unwrap().instructions, ["AGENTS.md"]);
540 let c = parse("[context]\ninstructions = [\"AGENTS.md\", \"CLAUDE.md\"]\n").unwrap();
541 assert_eq!(c.instructions, ["AGENTS.md", "CLAUDE.md"]);
542 assert!(
543 parse("[context]\ninstructions = []\n")
544 .unwrap()
545 .instructions
546 .is_empty()
547 );
548 assert!(parse("[context]\ninstructions = [\"docs/AGENTS.md\"]\n").is_err());
549 assert!(parse("[context]\nfiles = []\n").is_err());
550 }
551
552 #[test]
553 fn attestations_are_on_and_signed_by_git_unless_configured() {
554 let d = parse("").unwrap_or_default().attest;
555 assert!(d.enabled && d.signer == Signer::Git && !d.sign_later);
556 let c = parse("[attest]\nsigner = \"sigstore\"\nsign_at = \"later\"\n[attest.sigstore]\nidentity = \"ada@example.com\"\nissuer = \"https://accounts.example\"\n")
557 .unwrap_or_default()
558 .attest;
559 assert_eq!((c.signer, c.sign_later), (Signer::Sigstore, true));
560 assert_eq!(c.sigstore_identity.as_deref(), Some("ada@example.com"));
561 assert!(parse("[attest]\nsigner = \"x509\"\n").is_err());
562 assert!(parse("[attest]\nsign_at = \"never\"\n").is_err());
563 assert!(parse("[attest]\nkey = \"x\"\n").is_err());
564 }
565
566 #[test]
567 fn an_approval_waits_seven_days_unless_configured() {
568 assert_eq!(
569 parse("").unwrap_or_default().approval_wait.as_secs(),
570 7 * 86_400
571 );
572 let w = |t: &str| {
573 parse(&format!("[approvals]\nwait = \"{t}\"\n")).map(|c| c.approval_wait.as_secs())
574 };
575 assert_eq!(w("30m"), Ok(1800));
576 assert_eq!(w("12h"), Ok(43_200));
577 for bad in ["0d", "7", "7w", "-1d", ""] {
578 assert!(w(bad).is_err(), "{bad}");
579 }
580 }
581
582 #[test]
583 fn strength_is_measured_by_default_and_bounded() {
584 let c = parse("").unwrap().strength;
585 assert!(c.enabled && c.min.is_none());
586 assert_eq!((c.near_miss, c.permitted), (2, 1));
587 let c = parse("[acceptance]\nmin_strength = 0.8\nnear_miss = 3\npermitted = 0\nstrength_timeout_secs = 60\n")
588 .unwrap()
589 .strength;
590 assert_eq!(c.min, Some(0.8));
591 assert_eq!((c.near_miss, c.permitted, c.timeout.as_secs()), (3, 0, 60));
592 assert!(
593 !parse("[acceptance]\nstrength = false\n")
594 .unwrap()
595 .strength
596 .enabled
597 );
598 for bad in ["min_strength = 1.5", "near_miss = 4", "permitted = 3"] {
599 assert!(parse(&format!("[acceptance]\n{bad}\n")).is_err(), "{bad}");
600 }
601 }
602
603 #[test]
604 fn oracle_samples_are_one_or_two() {
605 assert_eq!(parse("").unwrap().oracle_samples, 2);
606 assert_eq!(
607 parse("[review]\noracle_samples = 1\n")
608 .unwrap()
609 .oracle_samples,
610 1
611 );
612 for bad in ["0", "3", "-1"] {
613 assert!(parse(&format!("[review]\noracle_samples = {bad}\n")).is_err());
614 }
615 }
616
617 #[test]
618 fn held_out_is_on_by_default_and_can_be_switched_off() {
619 assert!(parse("").unwrap().held_out);
620 let c = parse("[acceptance]\nheld_out = false\n").unwrap();
621 assert!(!c.held_out && c.acceptance);
622 assert!(parse("[acceptance]\nheldout = true\n").is_err());
623 }
624
625 #[test]
626 fn defaults_and_overrides() {
627 assert_eq!(parse("").unwrap(), RunConfig::default());
628 let c =
629 parse("[budget]\nmax_turns = 5\n[commands]\nallow = [[\"cargo\", \"fmt\"]]\n").unwrap();
630 assert_eq!(c.max_turns, 5);
631 assert_eq!(c.max_tool_calls, 100);
632 assert_eq!(c.max_attempts, 2);
633 assert_eq!(
634 parse("[budget]\nmax_attempts = 0\n").unwrap().max_attempts,
635 1
636 );
637 assert_eq!(c.allow, [vec!["cargo".to_owned(), "fmt".to_owned()]]);
638 assert!(parse("[budget]\nmax_cost = 1\n").is_err());
639 assert!(!parse("[git]\ncommit = \"none\"\n").unwrap().commit_per_task);
640 assert!(parse("[git]\ncommit = \"always\"\n").is_err());
641 assert!(
642 parse("[tests]\nfrozen = \"additions\"\n")
643 .unwrap()
644 .frozen_additions
645 );
646 assert!(
647 !parse("[tests]\nfrozen = \"ask\"\n")
648 .unwrap()
649 .frozen_additions
650 );
651 assert!(parse("[tests]\nfrozen = \"anything\"\n").is_err());
652 }
653
654 #[test]
655 fn the_asking_budget_and_prefixes_are_checked() {
656 assert_eq!(parse("").unwrap().approval_budget, 12);
657 assert_eq!(
658 parse("[approvals]\nbudget = 0\n").unwrap().approval_budget,
659 0
660 );
661 for bad in ["101", "-1"] {
662 let e = parse(&format!("[approvals]\nbudget = {bad}\n")).unwrap_err();
663 assert!(e.contains("budget"), "{e}");
664 }
665 let c = parse("[commands]\nallow_prefix = [[\"npm\", \"run\"]]\n").unwrap();
666 assert_eq!(c.allow_prefix, [vec!["npm".to_owned(), "run".to_owned()]]);
667 let e = parse("[commands]\nallow_prefix = [[\"python3\"]]\n").unwrap_err();
668 assert!(e.contains("a bare program allows any argument"), "{e}");
669 }
670}