Skip to main content

spar/
config.rs

1//! Configuration, and the agent presets that make a new CLI a data change
2//! rather than a code change.
3//!
4//! Presets are compiled into the binary. That is not an optimisation: a
5//! `cargo install`ed binary has no source tree beside it, so a preset read from
6//! a relative path would work for the author and fail for everyone else. Files
7//! on disk still win over the built in copies, so a preset can be overridden or
8//! a new one added without rebuilding.
9
10use std::collections::BTreeMap;
11use std::path::{Path, PathBuf};
12
13use serde::{Deserialize, Serialize};
14use toml::Value;
15
16use crate::error::Result;
17use crate::proc::{expand_tilde, home_dir};
18use crate::style::Style;
19use crate::{bail, spar_err};
20
21/// Presets that ship inside the binary.
22pub const BUILTIN_PRESETS: &[(&str, &str)] = &[
23    ("aider", include_str!("../presets/aider.toml")),
24    ("claude", include_str!("../presets/claude.toml")),
25    ("codex", include_str!("../presets/codex.toml")),
26    ("cursor", include_str!("../presets/cursor.toml")),
27    ("gemini", include_str!("../presets/gemini.toml")),
28];
29
30// ---------------------------------------------------------------------------
31// Agents
32// ---------------------------------------------------------------------------
33
34/// One element of a command template: a bare argument, or a group that is
35/// dropped whole when its placeholder is unset.
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
37#[serde(untagged)]
38pub enum CommandPart {
39    One(String),
40    Group(Vec<String>),
41}
42
43impl CommandPart {
44    pub fn args(&self) -> &[String] {
45        match self {
46            CommandPart::One(s) => std::slice::from_ref(s),
47            CommandPart::Group(v) => v,
48        }
49    }
50}
51
52/// How to read the answer out of what a CLI printed.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(rename_all = "lowercase")]
55pub enum OutputMode {
56    /// Everything on stdout is the answer.
57    Text,
58    /// Same as text; named separately because it reads better in a preset.
59    Json,
60    /// An event stream, one JSON object per line.
61    Jsonl,
62}
63
64/// Where the style rules go when a CLI has a system prompt flag.
65#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
66#[serde(rename_all = "lowercase")]
67pub enum SystemVia {
68    /// Prepended to the prompt.
69    Prompt,
70    /// Passed through the `{system}` placeholder.
71    Placeholder,
72}
73
74fn default_timeout() -> u64 {
75    crate::proc::DEFAULT_TIMEOUT_SECS
76}
77
78fn default_output() -> OutputMode {
79    OutputMode::Text
80}
81
82fn default_system_via() -> SystemVia {
83    SystemVia::Prompt
84}
85
86/// Everything needed to drive one CLI. An agent is data, not a class:
87/// supporting a new tool is a preset file.
88#[derive(Debug, Clone, Serialize, Deserialize)]
89#[serde(deny_unknown_fields)]
90pub struct AgentSpec {
91    #[serde(skip)]
92    pub name: String,
93    pub command: Vec<CommandPart>,
94    #[serde(default)]
95    pub model: Option<String>,
96    #[serde(default)]
97    pub effort: Option<String>,
98    /// This agent's own schedule, which wins over the shared one.
99    ///
100    /// Effort words are each CLI's own vocabulary, so one string for both
101    /// agents is a value that has to be legal in two languages at once. This is
102    /// where a schedule says `ultra` to codex and `high` to claude.
103    #[serde(default)]
104    pub effort_schedule: EffortSchedule,
105    #[serde(default = "default_output")]
106    pub output: OutputMode,
107    /// For `jsonl`: the event fields that identify the agent's own message.
108    #[serde(default)]
109    pub message_match: BTreeMap<String, String>,
110    /// For `jsonl`: the dotted path to the text inside a matching event.
111    #[serde(default)]
112    pub message_path: Option<String>,
113    /// Extra places to look for the binary when it is not on PATH.
114    #[serde(default)]
115    pub search_paths: Vec<String>,
116    #[serde(default = "default_system_via")]
117    pub system_via: SystemVia,
118    #[serde(default = "default_timeout")]
119    pub timeout: u64,
120    /// A stand in for when this agent cannot answer at all: a CLI that is down,
121    /// out of quota, or refusing the request on policy grounds.
122    ///
123    /// Declared as a nested table rather than by naming a third agent, because
124    /// spar takes exactly two and a backup is not a third opinion. It never
125    /// reviews alongside the pair, it only answers in place of the one that
126    /// failed, so the alternation the design rests on is unchanged.
127    ///
128    /// Built by `build_spec` from the `[agents.NAME.fallback]` table, never
129    /// deserialized directly, so a preset of its own still resolves.
130    #[serde(skip)]
131    pub fallback: Option<Box<AgentSpec>>,
132
133    // -- hints, inert at runtime -----------------------------------------
134    //
135    // Written into a generated config as comments so nobody has to guess what
136    // to put in `model` or `effort`. Deliberately never validated against: a
137    // CLI's options drift, and a stale allow list that refuses a model which
138    // actually works would be worse than no hint at all.
139    /// Model names this CLI is known to accept.
140    #[serde(default)]
141    pub models: Vec<String>,
142    /// Effort levels this CLI is known to accept.
143    #[serde(default)]
144    pub efforts: Vec<String>,
145    /// Where to check the current list, when spar cannot enumerate it.
146    #[serde(default)]
147    pub options_note: Option<String>,
148}
149
150impl AgentSpec {
151    /// The model as configured, normalised. An unset and an empty value both
152    /// mean "let the CLI pick", because `render` drops the flag either way.
153    pub fn model_key(&self) -> String {
154        self.model.as_deref().unwrap_or("").trim().to_string()
155    }
156
157    pub fn describe(&self) -> String {
158        format!(
159            "{}/{}",
160            self.model.as_deref().unwrap_or("default model"),
161            self.effort.as_deref().unwrap_or("default effort")
162        )
163    }
164}
165
166// ---------------------------------------------------------------------------
167// Loop and style blocks
168// ---------------------------------------------------------------------------
169
170/// Where an out of scope finding goes. On your own repository an issue is the
171/// right home. On a large repository that is not yours it is somebody else's
172/// notification and somebody else's triage queue.
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
174#[serde(rename_all = "lowercase")]
175pub enum Followups {
176    Issues,
177    Local,
178    None,
179}
180
181impl std::fmt::Display for Followups {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        f.write_str(match self {
184            Followups::Issues => "issues",
185            Followups::Local => "local",
186            Followups::None => "none",
187        })
188    }
189}
190
191/// Whether a pull request spar opens starts as a draft, and when it stops being
192/// one.
193///
194/// A draft says the work is not for a person yet, which is exactly true while
195/// two agents are still arguing about it. `UntilApproved` makes that state mean
196/// something and clear itself: the loop marks the pull request ready the moment
197/// it has no blocking findings left. `Always` is for somebody who promotes
198/// every pull request by hand and wants spar to keep out of it.
199#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
200#[serde(rename_all = "snake_case")]
201pub enum Drafts {
202    /// Open ordinary pull requests. The default, and what spar has always done.
203    Never,
204    /// Open as a draft, and mark it ready when the review converges.
205    UntilApproved,
206    /// Open as a draft and leave it that way.
207    Always,
208}
209
210impl std::fmt::Display for Drafts {
211    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
212        f.write_str(match self {
213            Drafts::Never => "never",
214            Drafts::UntilApproved => "until_approved",
215            Drafts::Always => "always",
216        })
217    }
218}
219
220/// How much of its own working spar narrates into a pull request thread.
221///
222/// The agents never read the PR: they receive findings through their prompts,
223/// so nothing in the loop depends on any of this being posted. It exists purely
224/// for the person who reads the thread later, which is why the default is the
225/// outcome rather than the play by play.
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
227#[serde(rename_all = "lowercase")]
228pub enum PrComments {
229    /// One comment when the run finishes, and only if it has something to say.
230    Outcome,
231    /// A comment per review and per response, as it happens. An audit trail,
232    /// at the cost of a thread nobody wants to read.
233    Rounds,
234    /// Never comment on a pull request. Everything goes to the terminal.
235    None,
236}
237
238impl std::fmt::Display for PrComments {
239    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
240        f.write_str(match self {
241            PrComments::Outcome => "outcome",
242            PrComments::Rounds => "rounds",
243            PrComments::None => "none",
244        })
245    }
246}
247
248/// Where resume state lives. Local keeps the PR clean and costs no API calls;
249/// writing to the PR only buys anything if a run might be resumed from a
250/// different checkout.
251#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
252#[serde(rename_all = "lowercase")]
253pub enum StateStore {
254    Local,
255    Pr,
256    Both,
257}
258
259impl StateStore {
260    pub fn writes_local(self) -> bool {
261        matches!(self, StateStore::Local | StateStore::Both)
262    }
263    pub fn writes_pr(self) -> bool {
264        matches!(self, StateStore::Pr | StateStore::Both)
265    }
266}
267
268/// Whose comments `spar checkin` will act on, and whose comments under an issue
269/// reach a prompt.
270///
271/// The default is not timidity. Acting on a comment means a commit pushed to a
272/// branch because somebody typed a sentence, and `authorAssociation` is one
273/// field GitHub already returns on every comment endpoint that says whether
274/// they can write to this repository at all. Everybody is still answered in
275/// words either way; this governs only whether a comment can produce a commit.
276#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
277#[serde(rename_all = "lowercase")]
278pub enum Trust {
279    /// Anybody GitHub says can write here: OWNER, MEMBER, COLLABORATOR.
280    Write,
281    /// Anybody at all. Both agents still have to agree before anything changes.
282    Anyone,
283}
284
285impl std::fmt::Display for Trust {
286    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
287        f.write_str(match self {
288            Trust::Write => "write",
289            Trust::Anyone => "anyone",
290        })
291    }
292}
293
294impl Trust {
295    /// Whether a comment from somebody with this association may produce a
296    /// commit.
297    pub fn may_act_on(self, association: &str) -> bool {
298        match self {
299            Trust::Anyone => true,
300            Trust::Write => matches!(
301                association.trim().to_uppercase().as_str(),
302                "OWNER" | "MEMBER" | "COLLABORATOR"
303            ),
304        }
305    }
306}
307
308/// Which call is about to be made.
309///
310/// The schedule used to be two keys, `round_1` and `rest`, chosen by a round
311/// number that most of these calls do not have. Triage of the whole queue, the
312/// implement call, the follow-up screen, the check-in judge and the split
313/// proposer were all "round 1", so `round_1 = "ultra"` put every one of them at
314/// ultra, which is neither what the name says nor what anybody wants to pay
315/// for.
316#[derive(Debug, Clone, Copy, PartialEq, Eq)]
317pub enum Call {
318    /// Drafting one issue from a project prompt.
319    Code,
320    /// Both agents, over the whole queue, once per wave.
321    Triage,
322    /// Writing the first draft.
323    Implement,
324    /// A review round. The first is the deep one.
325    Review(u32),
326    /// The author answering a review.
327    Respond(u32),
328    /// The closing merge-safety pass.
329    Close,
330    /// The follow-up screen, one call for the whole queue.
331    Screen,
332    /// A check-in call: the judge is round 1, the checker round 2.
333    Checkin(u32),
334    /// A split call: the proposer is round 1, the checker round 2.
335    Split(u32),
336    /// A brainstorm round: both agents diverge in round 1, cross in the
337    /// middle rounds, and converge in the last.
338    Brainstorm(u32),
339    /// A second look at a reviewed pull request after commits or replies.
340    Recheck,
341}
342
343impl Call {
344    /// The schedule key this call reads.
345    pub fn key(self) -> &'static str {
346        match self {
347            Call::Code => "code",
348            Call::Triage => "triage",
349            Call::Implement => "implement",
350            Call::Review(round) if round <= 1 => "review_1",
351            Call::Review(_) => "review_rest",
352            Call::Respond(_) => "respond",
353            Call::Close => "close",
354            Call::Screen => "screen",
355            Call::Checkin(_) => "checkin",
356            Call::Split(_) => "split",
357            Call::Brainstorm(_) => "brainstorm",
358            Call::Recheck => "recheck",
359        }
360    }
361
362    /// What the two key schedule would have used, so a config written before
363    /// the keys existed behaves exactly as it did.
364    fn legacy_round(self) -> u32 {
365        match self {
366            Call::Code | Call::Triage | Call::Implement | Call::Screen => 1,
367            Call::Review(round) | Call::Respond(round) => round,
368            Call::Checkin(round) | Call::Split(round) | Call::Brainstorm(round) => round,
369            Call::Close | Call::Recheck => 2,
370        }
371    }
372
373    /// Every kind, for `doctor` to check a configuration against.
374    pub fn every() -> [Call; 12] {
375        [
376            Call::Code,
377            Call::Triage,
378            Call::Implement,
379            Call::Review(1),
380            Call::Review(2),
381            Call::Recheck,
382            Call::Respond(1),
383            Call::Close,
384            Call::Screen,
385            Call::Checkin(1),
386            Call::Split(1),
387            Call::Brainstorm(1),
388        ]
389    }
390}
391
392#[derive(Debug, Clone, Default, Serialize, Deserialize)]
393#[serde(deny_unknown_fields)]
394pub struct EffortSchedule {
395    /// Drafting an issue for `spar code`.
396    pub code: Option<String>,
397    /// The deep first review.
398    ///
399    /// Kept, and still read by every call that has no key of its own, so an
400    /// existing config keeps working exactly as it did.
401    pub round_1: Option<String>,
402    /// Every round after the first, and the closing pass. Both are asked a
403    /// narrower question than the first review, so neither buys its depth again.
404    pub rest: Option<String>,
405    /// Both agents, over the whole queue. The most expensive call in a run, and
406    /// the one the two key schedule never mentioned.
407    pub triage: Option<String>,
408    pub implement: Option<String>,
409    pub review_1: Option<String>,
410    pub review_rest: Option<String>,
411    pub respond: Option<String>,
412    pub close: Option<String>,
413    pub screen: Option<String>,
414    pub checkin: Option<String>,
415    pub split: Option<String>,
416    /// Every brainstorm round. Diverging is the deep call and the later rounds
417    /// answer narrower questions, but they read the same key: a brainstorm is
418    /// cheap next to a run, and one word for it is enough.
419    pub brainstorm: Option<String>,
420    /// A second look at a reviewed pull request: a delta and a list of claims
421    /// to check, so it sits with the later review rounds and reads
422    /// `review_rest` when it has no word of its own.
423    pub recheck: Option<String>,
424}
425
426/// A schedule entry that was actually written, blank meaning unset.
427fn set(value: &Option<String>) -> Option<String> {
428    value.clone().filter(|s| !s.trim().is_empty())
429}
430
431impl EffortSchedule {
432    /// What this schedule says about one call, if anything.
433    pub fn get(&self, call: Call) -> Option<String> {
434        let value = match call.key() {
435            "code" => &self.code,
436            "triage" => &self.triage,
437            "implement" => &self.implement,
438            "review_1" => &self.review_1,
439            "review_rest" => &self.review_rest,
440            "respond" => &self.respond,
441            "close" => &self.close,
442            "screen" => &self.screen,
443            "checkin" => &self.checkin,
444            "brainstorm" => &self.brainstorm,
445            "recheck" => {
446                return set(&self.recheck).or_else(|| set(&self.review_rest));
447            }
448            _ => &self.split,
449        };
450        set(value)
451    }
452
453    /// What the old two key schedule would have said.
454    fn legacy(&self, call: Call) -> Option<String> {
455        let value = if call.legacy_round() <= 1 {
456            &self.round_1
457        } else {
458            &self.rest
459        };
460        value.clone().filter(|s| !s.trim().is_empty())
461    }
462}
463
464/// A command spar runs itself: the check before a push, or the setup of a new
465/// worktree.
466///
467/// A string is split on whitespace, which covers `cargo test` and `npm test`.
468/// A list is taken as it is, for anything that needs an argument with a space
469/// in it. No shell either way: spar runs the program, so nothing here is
470/// interpreted.
471#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
472#[serde(untagged)]
473pub enum CheckCommand {
474    Line(String),
475    Argv(Vec<String>),
476}
477
478impl Default for CheckCommand {
479    /// An empty list, which is both "nothing to run" and a value that survives
480    /// a round trip through TOML, unlike a unit variant.
481    fn default() -> Self {
482        CheckCommand::Argv(Vec::new())
483    }
484}
485
486impl CheckCommand {
487    pub fn argv(&self) -> Vec<String> {
488        match self {
489            CheckCommand::Line(line) => line.split_whitespace().map(str::to_string).collect(),
490            CheckCommand::Argv(argv) => argv
491                .iter()
492                .map(|a| a.trim().to_string())
493                .filter(|a| !a.is_empty())
494                .collect(),
495        }
496    }
497
498    pub fn is_set(&self) -> bool {
499        !self.argv().is_empty()
500    }
501
502    /// How the command reads in a log line or a pull request body.
503    pub fn describe(&self) -> String {
504        self.argv().join(" ")
505    }
506}
507
508/// Every field takes its value from `LoopCfg::default()` when a config does not
509/// mention it, rather than from a per-field function saying the same thing in a
510/// second place. Two places is how a default goes stale.
511#[derive(Debug, Clone, Serialize, Deserialize)]
512#[serde(default, deny_unknown_fields)]
513pub struct LoopCfg {
514    /// Review rounds one invocation may spend asking for changes. A run that
515    /// spends the full budget may use one closing pass, which is not a round.
516    pub max_rounds: u32,
517    pub auto_merge: bool,
518    pub first_implementor: Option<String>,
519    pub base_branch: String,
520    pub worktrees: bool,
521    pub keep_worktrees: bool,
522    pub state_store: StateStore,
523    pub branch_prefix: String,
524    pub followups: Followups,
525    /// File a non-blocking finding as a follow-up.
526    ///
527    /// Off by default, and this is the setting that stops a run breeding. A
528    /// thorough reviewer always finds improvements, and turning each one into a
529    /// tracker item made a single issue spawn ten, which spawned more: mean
530    /// offspring above one never terminates. Not gating a merge is not the same
531    /// as being worth somebody's triage queue.
532    pub file_non_blocking: bool,
533    /// Most follow-ups one run may record before it stops and says what it
534    /// dropped. A backstop, not a target.
535    pub max_followups: usize,
536    /// Most parts `spar split` will make out of one issue or pull request.
537    ///
538    /// A backstop, not a target, in the shape of `max_followups`. There is no
539    /// setting to turn splitting off: it is a command somebody types, and a
540    /// setting to disable a command nobody has to run only ever confuses. There
541    /// is no mechanical threshold either, because a floor on files changed
542    /// would split a forty file rename and hold back a three file mess.
543    pub max_split_parts: usize,
544    /// Nits stay in the PR thread by default. A filed nit is somebody else's
545    /// notification: a run on a production codebase once opened an issue titled
546    /// "Log wording".
547    pub file_nits: bool,
548    /// Close an issue that both agents independently declined, after posting
549    /// the shared reasoning. One agent's opinion is never enough.
550    pub close_skipped: bool,
551    /// Ask both agents to triage at the same time. They only read during
552    /// triage, so there is nothing to serialise.
553    pub parallel_triage: bool,
554    /// Ignore issues and pull requests numbered below this when spar is picking
555    /// for itself. 0 is no floor.
556    ///
557    /// A repository that has been going a while carries a tail of old issues
558    /// nobody is going to reach, and since spar takes the lowest numbered open
559    /// items it walks straight into them. A number you name explicitly is still
560    /// honoured: naming it is the point.
561    pub min_number: i64,
562    /// Waves of newly filed follow-ups to fold back into the same run, rather
563    /// than leaving them for the next one.
564    ///
565    /// Off by default because it multiplies what a run costs, and because each
566    /// wave can file follow-ups of its own. Every wave is triaged like any
567    /// other issue, so both agents still have to agree it is worth doing.
568    pub absorb_new_issues: u32,
569    /// Turn the checklist in a tracking issue into issues, and work them.
570    ///
571    /// Off by default, for the reason `file_non_blocking` is: a tracker exists
572    /// to contain many things, so this is the shape of feature that multiplies.
573    /// `max_tracker_children` caps it, a child that triage calls a tracker is
574    /// held rather than decomposed in turn, and both agents still gate every
575    /// child at triage.
576    ///
577    /// The trigger is the checklist, never a judgement about what the parts
578    /// are. Writing `- [ ]` lines is something a person does on purpose, which
579    /// makes this opt in per issue as well as per repository.
580    pub decompose_trackers: bool,
581    /// Most items from one tracker's checklist that one run may take on. A cap,
582    /// not a target, and what it left is named out loud.
583    pub max_tracker_children: usize,
584    /// Whether a pull request spar opens starts as a draft.
585    pub drafts: Drafts,
586    /// Publish a new branch from the authenticated user's fork when origin
587    /// refuses the push, and open the pull request from there.
588    ///
589    /// On by default because the alternative is a run that implements an
590    /// issue, then fails at the push and throws the work away. A fork is how
591    /// anybody without write access contributes, and asking GitHub for one
592    /// returns the existing fork when there already is one.
593    pub fork_when_denied: bool,
594    /// Extra instructions handed to both agents with every request.
595    ///
596    /// For what a person wants of this repository that the code cannot say and
597    /// spar has no setting for: how far to go, what not to touch, what not to
598    /// wait on. A CLI reads its own conventions file already, CLAUDE.md or
599    /// AGENTS.md, but each reads only its own, and two agents given different
600    /// standing instructions are not the pair this design rests on.
601    ///
602    /// Subordinate to the request and to the schema, which is said in the
603    /// header they arrive under: they change how the work is done, never what
604    /// was asked for or the shape of the answer.
605    pub instructions: String,
606    /// The most of one issue, its body and the comments on it, that reaches a
607    /// prompt. Past it the oldest comments go first, then the body's tail.
608    ///
609    /// Sized so that no issue a person wrote is ever cut. It was 2000 for
610    /// triage and 6000 for implement, silently, and both were small enough to
611    /// clip an ordinary bug report: an agent given half an issue judges and
612    /// implements the half it saw and has no way to know the rest existed.
613    /// When this does fire it is said out loud, in the log and in the prompt.
614    pub max_issue_chars: usize,
615    /// The most every issue together, bodies and comments, may add to one
616    /// triage prompt.
617    ///
618    /// Triage reads the whole queue at once, so the only unbounded thing here
619    /// is the queue. Past this, whole issues are left for the next run rather
620    /// than every issue being shortened: a verdict is posted on the issue and
621    /// can close it, so judging one on part of its body is worse than not
622    /// reaching it yet.
623    pub max_triage_chars: usize,
624    /// Whose comments `spar checkin` will act on, and whose comments under an
625    /// issue reach the prompts that implement, triage, and review it.
626    ///
627    /// One setting, because both are the same question: whether what somebody
628    /// typed can become a commit. The person who filed an issue is read under
629    /// it whatever this says, since they wrote the body. The authenticated
630    /// GitHub user is also read under an issue regardless of write access.
631    pub checkin_trust: Trust,
632    /// Mark a review thread resolved when spar made the change it asked for.
633    ///
634    /// A thread spar disagreed with is left open whatever this says: the person
635    /// who raised it has not had their say yet, and it is their thread.
636    pub checkin_resolve: bool,
637    /// Most unanswered comments spar answers on one pull request in a run.
638    ///
639    /// A backstop against a long argument being read back to somebody, not a
640    /// target. What it held back is said out loud.
641    pub max_checkin_comments: usize,
642    /// A command spar runs in every worktree it creates, before any agent works
643    /// there or the check runs. Unset means nothing is run.
644    ///
645    /// A worktree holds only what git tracks, so a project that needs
646    /// `npm ci` or `uv sync` before it builds got a checkout that could not.
647    /// Whether it ever built then rested on an agent happening to install
648    /// dependencies on its own: an issue whose agent did passed the check, and
649    /// the next one failed it twice over types it could not find, spending the
650    /// one fix on a tree it was never asked to repair. The shared checkout is
651    /// left alone, since it is the person's own and already set up.
652    pub setup: CheckCommand,
653    /// A command spar runs in the worktree after an editing call committed and
654    /// before the branch is pushed. Unset means nothing is run.
655    ///
656    /// The design's rule is that a prompt is not a permission and every claim
657    /// an agent makes is checked mechanically where it can be. "I ran the
658    /// tests" was the one claim nothing checked: the schema records it, and
659    /// spar pushed and opened the pull request on the agent's word. This is not
660    /// waiting on CI, which is expensive and somebody else's machine. It is one
661    /// command, before the push, that fails closed.
662    pub check: CheckCommand,
663    pub effort_schedule: EffortSchedule,
664}
665
666impl Default for LoopCfg {
667    fn default() -> Self {
668        Self {
669            max_rounds: 3,
670            auto_merge: false,
671            first_implementor: None,
672            base_branch: "main".into(),
673            worktrees: true,
674            keep_worktrees: false,
675            state_store: StateStore::Local,
676            branch_prefix: String::new(),
677            followups: Followups::Local,
678            file_non_blocking: false,
679            max_followups: 5,
680            max_split_parts: 4,
681            file_nits: false,
682            close_skipped: true,
683            parallel_triage: true,
684            min_number: 0,
685            absorb_new_issues: 0,
686            decompose_trackers: false,
687            max_tracker_children: 5,
688            drafts: Drafts::Never,
689            fork_when_denied: true,
690            instructions: String::new(),
691            max_issue_chars: 60_000,
692            max_triage_chars: 200_000,
693            checkin_trust: Trust::Write,
694            checkin_resolve: true,
695            max_checkin_comments: 20,
696            setup: CheckCommand::default(),
697            check: CheckCommand::default(),
698            effort_schedule: EffortSchedule::default(),
699        }
700    }
701}
702
703#[derive(Debug, Clone, Serialize, Deserialize)]
704#[serde(default, deny_unknown_fields)]
705pub struct StyleCfg {
706    pub ban_em_dash: bool,
707    pub ban_ai_attribution: bool,
708    pub terse: bool,
709    pub max_detail_chars: usize,
710    pub max_summary_chars: usize,
711    pub max_body_chars: usize,
712    /// A filed issue's body. Far larger than a PR comment's on purpose: a
713    /// comment is read with the diff in front of you, an issue is picked up
714    /// cold months later by somebody who needs the whole story.
715    pub max_issue_body_chars: usize,
716    pub max_title_chars: usize,
717    pub pr_comments: PrComments,
718    pub inline_comments: bool,
719}
720
721impl Default for StyleCfg {
722    /// Taken from `Style`, which is where the budgets are decided, rather than
723    /// written out again here.
724    ///
725    /// They were written out again here, and they drifted. The functions
726    /// supplying them to serde were still named `d90`, `d200`, `d320`, `d900`
727    /// and `d4000` while returning 140, 2000, 6000, 8000 and 20000, and the
728    /// config `spar init` generated offered the old numbers as though they
729    /// were current. Uncommenting one of those lines to see what it did cut
730    /// every comment spar posts to a fifth of its length.
731    fn default() -> Self {
732        let style = Style::default();
733        Self {
734            ban_em_dash: style.ban_em_dash,
735            ban_ai_attribution: style.ban_ai_attribution,
736            terse: style.terse,
737            max_detail_chars: style.max_detail_chars,
738            max_summary_chars: style.max_summary_chars,
739            max_body_chars: style.max_body_chars,
740            max_issue_body_chars: style.max_issue_body_chars,
741            max_title_chars: style.max_title_chars,
742            pr_comments: style.pr_comments,
743            inline_comments: style.inline_comments,
744        }
745    }
746}
747
748impl StyleCfg {
749    pub fn to_style(&self) -> Style {
750        Style {
751            ban_em_dash: self.ban_em_dash,
752            ban_ai_attribution: self.ban_ai_attribution,
753            terse: self.terse,
754            max_detail_chars: self.max_detail_chars,
755            max_summary_chars: self.max_summary_chars,
756            max_body_chars: self.max_body_chars,
757            max_issue_body_chars: self.max_issue_body_chars,
758            max_title_chars: self.max_title_chars,
759            pr_comments: self.pr_comments,
760            inline_comments: self.inline_comments,
761        }
762    }
763}
764
765// ---------------------------------------------------------------------------
766// Listening
767// ---------------------------------------------------------------------------
768
769/// What `spar listen` does with a pull request somebody put in front of you.
770#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
771#[serde(rename_all = "lowercase")]
772pub enum ReviewOrIgnore {
773    /// Review it, with a verdict when `verdicts` is on.
774    Review,
775    /// Say so in the log and do nothing.
776    Ignore,
777}
778
779impl std::fmt::Display for ReviewOrIgnore {
780    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
781        f.write_str(match self {
782            ReviewOrIgnore::Review => "review",
783            ReviewOrIgnore::Ignore => "ignore",
784        })
785    }
786}
787
788/// What `spar listen` does when somebody speaks on a pull request you wrote.
789/// GitHub will not let the same account approve it, so the choice is between
790/// `spar checkin` with and without the power to push.
791#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
792#[serde(rename_all = "lowercase")]
793pub enum OwnPr {
794    /// `spar checkin`: a change both agents agree on is pushed, behind
795    /// `checkin_trust`.
796    Checkin,
797    /// `spar checkin --reply-only`: answered in words, nothing pushed.
798    Reply,
799    /// Say so in the log and do nothing.
800    Ignore,
801}
802
803impl std::fmt::Display for OwnPr {
804    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
805        f.write_str(match self {
806            OwnPr::Checkin => "checkin",
807            OwnPr::Reply => "reply",
808            OwnPr::Ignore => "ignore",
809        })
810    }
811}
812
813/// What `spar listen` watches for and what it does about it. As with `LoopCfg`,
814/// every field takes its value from `ListenCfg::default()` when a config does
815/// not mention it.
816#[derive(Debug, Clone, Serialize, Deserialize)]
817#[serde(default, deny_unknown_fields)]
818pub struct ListenCfg {
819    /// Seconds between polls of GitHub's notifications for the repository.
820    /// GitHub says how often it will answer, and the larger of the two is used.
821    pub interval: u64,
822    /// Seconds between sweeps of the open pull requests. The sweep sees what a
823    /// notification can miss: a review asked for again, an assignment, a
824    /// close, or a push, which raises no notification for a reviewer.
825    pub sweep: u64,
826    /// Seconds an item has to stay quiet before spar acts on it, so a review
827    /// request and three comments in the same minute cost one recheck rather
828    /// than four.
829    pub settle: u64,
830    /// Most seconds an item may keep settling before it is acted on anyway.
831    pub max_wait: u64,
832    /// Rechecks one pull request gets before a person has to run `spar review`
833    /// on it again. A cap against two bots arguing, not a target. 0 turns
834    /// automatic rechecks off.
835    pub max_rechecks: u32,
836    pub on_review_requested: ReviewOrIgnore,
837    pub on_assigned: ReviewOrIgnore,
838    pub on_own_pr: OwnPr,
839    /// Whether a review `spar listen` starts submits a formal verdict, approve
840    /// or request changes, rather than a comment. A recheck always does.
841    pub verdicts: bool,
842}
843
844impl Default for ListenCfg {
845    fn default() -> Self {
846        Self {
847            interval: 60,
848            sweep: 300,
849            settle: 120,
850            max_wait: 900,
851            max_rechecks: 5,
852            on_review_requested: ReviewOrIgnore::Review,
853            on_assigned: ReviewOrIgnore::Review,
854            on_own_pr: OwnPr::Checkin,
855            verdicts: true,
856        }
857    }
858}
859
860// ---------------------------------------------------------------------------
861// The whole config
862// ---------------------------------------------------------------------------
863
864#[derive(Debug, Clone)]
865pub struct Config {
866    /// In declaration order, which is what `first_implementor` defaults to.
867    pub agents: Vec<AgentSpec>,
868    pub loop_cfg: LoopCfg,
869    pub style: Style,
870    pub listen: ListenCfg,
871    /// Resolved: never empty, always one of the configured agents. Under the
872    /// alternating policy this is who goes first on the first issue.
873    pub first_implementor: String,
874    /// Whether the first implementor swaps from issue to issue.
875    pub alternate_first: bool,
876    /// Where this config was read from, for error messages.
877    pub source: Option<PathBuf>,
878}
879
880impl Config {
881    pub fn agent_names(&self) -> Vec<String> {
882        self.agents.iter().map(|a| a.name.clone()).collect()
883    }
884
885    pub fn has_agent(&self, name: &str) -> bool {
886        self.agents.iter().any(|a| a.name == name)
887    }
888
889    pub fn spec(&self, name: &str) -> Result<&AgentSpec> {
890        self.agents.iter().find(|a| a.name == name).ok_or_else(|| {
891            spar_err!(
892                "no agent named '{name}' ({})",
893                self.agent_names().join(", ")
894            )
895        })
896    }
897
898    /// The other agent. With exactly two configured, custody alternates by
899    /// definition.
900    pub fn other(&self, name: &str) -> String {
901        let names = self.agent_names();
902        if names.first().map(String::as_str) == Some(name) {
903            names.get(1).cloned().unwrap_or_else(|| name.to_string())
904        } else {
905            names.first().cloned().unwrap_or_else(|| name.to_string())
906        }
907    }
908
909    /// What effort one agent is asked for on one call.
910    ///
911    /// Four places, most specific first: this agent's own schedule, the shared
912    /// schedule, the old `round_1`/`rest` pair, and the agent's plain `effort`.
913    /// The per agent layer is what makes a schedule expressible at all when the
914    /// two CLIs do not share a vocabulary: `ultra` is not a value the claude
915    /// preset accepts, and `max` means different things to each.
916    pub fn effort_for(&self, spec: &AgentSpec, call: Call) -> Option<String> {
917        spec.effort_schedule
918            .get(call)
919            .or_else(|| self.loop_cfg.effort_schedule.get(call))
920            .or_else(|| self.loop_cfg.effort_schedule.legacy(call))
921            .or_else(|| spec.effort.clone())
922    }
923
924    /// Round 1 gets the deep pass; later rounds only see a small delta, and a
925    /// full ultra review of a three line delta is money on fire.
926    pub fn effort_for_round(&self, spec: &AgentSpec, round: u32) -> Option<String> {
927        self.effort_for(spec, Call::Review(round))
928    }
929
930    /// Who writes the first draft of the nth issue this run works.
931    ///
932    /// Fixed unless the policy is to alternate, in which case it swaps every
933    /// issue, so both models author and both models review first over a queue.
934    pub fn first_implementor_for(&self, nth: usize) -> String {
935        if !self.alternate_first || nth % 2 == 0 {
936            return self.first_implementor.clone();
937        }
938        self.other(&self.first_implementor)
939    }
940
941    /// What to print when somebody asks who goes first.
942    pub fn first_implementor_policy(&self) -> String {
943        if self.alternate_first {
944            format!("alternating, {} first", self.first_implementor)
945        } else {
946            self.first_implementor.clone()
947        }
948    }
949
950    pub fn base_branch(&self) -> &str {
951        &self.loop_cfg.base_branch
952    }
953}
954
955#[derive(Debug, Deserialize)]
956#[serde(deny_unknown_fields)]
957struct RawConfig {
958    #[serde(default)]
959    agents: toml::Table,
960    #[serde(default)]
961    #[serde(rename = "loop")]
962    loop_cfg: Option<LoopCfg>,
963    #[serde(default)]
964    style: Option<StyleCfg>,
965    #[serde(default)]
966    listen: Option<ListenCfg>,
967}
968
969// ---------------------------------------------------------------------------
970// Presets
971// ---------------------------------------------------------------------------
972
973/// Directories searched for preset overrides, nearest first.
974///
975/// Deliberately *not* a bare `presets/`. spar runs from inside the user's own
976/// repository, where `presets/` is a perfectly ordinary directory name for
977/// something unrelated (sampler configs, prompt libraries, editor themes), and
978/// a stray `presets/claude.toml` shadowing the built in preset produces a
979/// baffling failure: `spar init` reports Claude Code as missing while it sits
980/// on PATH. Overrides live somewhere that names spar.
981pub fn preset_dirs() -> Vec<PathBuf> {
982    let mut dirs = Vec::new();
983    if let Some(custom) = std::env::var_os("SPAR_PRESET_DIR") {
984        dirs.push(PathBuf::from(custom));
985    }
986    dirs.push(PathBuf::from(".spar").join("presets"));
987    if let Some(home) = home_dir() {
988        dirs.push(home.join(".config").join("spar").join("presets"));
989    }
990    dirs
991}
992
993/// Every preset name available, built in and on disk, sorted.
994pub fn available_presets() -> Vec<String> {
995    let mut names: Vec<String> = BUILTIN_PRESETS.iter().map(|(n, _)| n.to_string()).collect();
996    for dir in preset_dirs() {
997        if let Ok(entries) = std::fs::read_dir(&dir) {
998            for entry in entries.flatten() {
999                let path = entry.path();
1000                if path.extension().and_then(|e| e.to_str()) == Some("toml") {
1001                    if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
1002                        names.push(stem.to_string());
1003                    }
1004                }
1005            }
1006        }
1007    }
1008    names.sort();
1009    names.dedup();
1010    names
1011}
1012
1013/// Parse a whole TOML document into a `Value`.
1014///
1015/// `"...".parse::<Value>()` parses a single TOML *value*, not a document, so it
1016/// rejects the first comment line of every preset.
1017fn parse_document(text: &str, what: &str) -> Result<Value> {
1018    let table: toml::Table =
1019        toml::from_str(text).map_err(|e| spar_err!("{what} is not valid TOML: {e}"))?;
1020    Ok(Value::Table(table))
1021}
1022
1023/// Load a preset. A file on disk wins over the built in copy of the same name,
1024/// so a drifting CLI can be corrected without waiting for a release.
1025pub fn load_preset(name: &str) -> Result<Value> {
1026    for dir in preset_dirs() {
1027        let path = dir.join(format!("{name}.toml"));
1028        if path.is_file() {
1029            let text = std::fs::read_to_string(&path)
1030                .map_err(|e| spar_err!("could not read preset {}: {e}", path.display()))?;
1031            return parse_document(&text, &format!("preset {}", path.display()));
1032        }
1033    }
1034    for (builtin, text) in BUILTIN_PRESETS {
1035        if *builtin == name {
1036            return parse_document(text, &format!("built in preset {name}"));
1037        }
1038    }
1039    Err(spar_err!(
1040        "unknown preset '{name}'. Available: {}",
1041        available_presets().join(", ")
1042    ))
1043}
1044
1045/// Deep merge, with `over` winning. Used so a config block can override one
1046/// field of a preset without restating the whole command template.
1047fn merge(base: &Value, over: &Value) -> Value {
1048    match (base, over) {
1049        (Value::Table(b), Value::Table(o)) => {
1050            let mut out = b.clone();
1051            for (key, value) in o {
1052                let merged = match out.get(key) {
1053                    Some(existing) => merge(existing, value),
1054                    None => value.clone(),
1055                };
1056                out.insert(key.clone(), merged);
1057            }
1058            Value::Table(out)
1059        }
1060        _ => over.clone(),
1061    }
1062}
1063
1064fn build_spec(name: &str, raw: &Value) -> Result<AgentSpec> {
1065    let table = raw
1066        .as_table()
1067        .ok_or_else(|| spar_err!("agent '{name}' must be a table"))?;
1068
1069    let merged = match table.get("preset").and_then(Value::as_str) {
1070        Some(preset) => merge(&load_preset(preset)?, raw),
1071        None => raw.clone(),
1072    };
1073
1074    let mut merged_table = merged
1075        .as_table()
1076        .cloned()
1077        .ok_or_else(|| spar_err!("agent '{name}' must be a table"))?;
1078    merged_table.remove("preset");
1079    // Lifted out before the spec is deserialized: a fallback is a whole agent,
1080    // preset and all, and only this function knows how to resolve a preset.
1081    let fallback_raw = merged_table.remove("fallback");
1082
1083    if !merged_table.contains_key("command") {
1084        bail!(
1085            "agent '{name}' has no command and no preset. Set one of them, or pick a preset: {}",
1086            available_presets().join(", ")
1087        );
1088    }
1089
1090    let mut spec: AgentSpec = Value::Table(merged_table)
1091        .try_into()
1092        .map_err(|e| spar_err!("agent '{name}': {e}"))?;
1093    spec.name = name.to_string();
1094
1095    if spec.command.is_empty() {
1096        bail!("agent '{name}' has an empty command");
1097    }
1098    if matches!(spec.command.first(), Some(CommandPart::Group(_))) {
1099        bail!("agent '{name}': the first command element must be the program name, not a group");
1100    }
1101    if spec.output == OutputMode::Jsonl && spec.message_path.as_deref().unwrap_or("").is_empty() {
1102        bail!(
1103            "agent '{name}': output = \"jsonl\" needs a message_path saying where the answer lives"
1104        );
1105    }
1106
1107    if let Some(raw) = fallback_raw {
1108        if !raw.is_table() {
1109            bail!(
1110                "agent '{name}': fallback is a whole agent, so write it as a table:\n                   [agents.{name}.fallback]\n  preset = \"cursor\""
1111            );
1112        }
1113        // Named for the env override it answers to, SPAR_<NAME>_FALLBACK_BIN,
1114        // and so a log line says which agent stood in for which.
1115        let backup = build_spec(&format!("{name}-fallback"), &raw)?;
1116        if backup.fallback.is_some() {
1117            bail!(
1118                "agent '{name}': a fallback may not have a fallback of its own. Each one costs \
1119                 another full timeout on a call that has already failed once."
1120            );
1121        }
1122        spec.fallback = Some(Box::new(backup));
1123    }
1124
1125    Ok(spec)
1126}
1127
1128// ---------------------------------------------------------------------------
1129// Loading
1130// ---------------------------------------------------------------------------
1131
1132/// One option the parser accepts, with the value it takes when unset.
1133#[derive(Debug, Clone)]
1134pub struct OptionInfo {
1135    pub section: &'static str,
1136    pub key: String,
1137    pub default: String,
1138}
1139
1140/// Every option a config file may set, with its default.
1141///
1142/// Derived from the defaults themselves rather than written out by hand, so an
1143/// option added to the code cannot go missing here. That is what lets `doctor`
1144/// tell somebody upgrading which settings are new since they wrote their file.
1145pub fn known_options() -> Vec<OptionInfo> {
1146    fn lines<T: Serialize>(section: &'static str, value: &T) -> Vec<OptionInfo> {
1147        toml::to_string(value)
1148            .unwrap_or_default()
1149            .lines()
1150            .filter_map(|line| line.split_once(" = "))
1151            .map(|(key, default)| OptionInfo {
1152                section,
1153                key: key.trim().to_string(),
1154                default: default.trim().to_string(),
1155            })
1156            .collect()
1157    }
1158    let mut out = lines("loop", &LoopCfg::default());
1159    out.extend(lines("style", &StyleCfg::default()));
1160    out.extend(lines(
1161        "loop.effort_schedule",
1162        &EffortSchedule {
1163            round_1: Some("high".into()),
1164            rest: Some("low".into()),
1165            code: Some("high".into()),
1166            triage: Some("low".into()),
1167            implement: Some("high".into()),
1168            review_1: Some("high".into()),
1169            review_rest: Some("low".into()),
1170            respond: Some("high".into()),
1171            close: Some("low".into()),
1172            screen: Some("low".into()),
1173            checkin: Some("high".into()),
1174            split: Some("high".into()),
1175            brainstorm: Some("high".into()),
1176            recheck: Some("low".into()),
1177        },
1178    ));
1179    out.extend(lines("listen", &ListenCfg::default()));
1180    out
1181}
1182
1183/// Whether a config file mentions an option at all, set or commented out.
1184pub fn mentions(config_text: &str, key: &str) -> bool {
1185    config_text.lines().any(|line| {
1186        let bare = line.trim_start().trim_start_matches('#').trim_start();
1187        bare.starts_with(&format!("{key} ")) || bare.starts_with(&format!("{key}="))
1188    })
1189}
1190
1191/// Options this config file has never heard of, which is what somebody who
1192/// upgraded wants to know.
1193pub fn unmentioned_options(config_text: &str) -> Vec<OptionInfo> {
1194    known_options()
1195        .into_iter()
1196        .filter(|o| !mentions(config_text, &o.key))
1197        .collect()
1198}
1199
1200pub const CONFIG_NAMES: &[&str] = &["spar.toml", ".spar.toml"];
1201
1202/// Find a config: an explicit path, then the working directory, then
1203/// `~/.config/spar/spar.toml`.
1204pub fn find_config(explicit: Option<&Path>) -> Result<Option<PathBuf>> {
1205    if let Some(path) = explicit {
1206        if !path.is_file() {
1207            bail!("config not found: {}", path.display());
1208        }
1209        return Ok(Some(path.to_path_buf()));
1210    }
1211    for name in CONFIG_NAMES {
1212        let path = PathBuf::from(name);
1213        if path.is_file() {
1214            return Ok(Some(path));
1215        }
1216    }
1217    if let Some(home) = home_dir() {
1218        let path = home.join(".config").join("spar").join("spar.toml");
1219        if path.is_file() {
1220            return Ok(Some(path));
1221        }
1222    }
1223    Ok(None)
1224}
1225
1226pub fn load(explicit: Option<&Path>) -> Result<Config> {
1227    let Some(path) = find_config(explicit)? else {
1228        bail!(
1229            "no spar.toml found. Run `spar init` to generate one from the CLIs you have installed."
1230        );
1231    };
1232    let text = std::fs::read_to_string(&path)
1233        .map_err(|e| spar_err!("could not read {}: {e}", path.display()))?;
1234    let mut cfg = parse(&text).map_err(|e| spar_err!("{}: {e}", path.display()))?;
1235    cfg.source = Some(path);
1236    Ok(cfg)
1237}
1238
1239pub fn parse(text: &str) -> Result<Config> {
1240    let raw: RawConfig = toml::from_str(text)?;
1241
1242    if raw.agents.len() != 2 {
1243        bail!(
1244            "spar needs exactly two agents, found {}. The whole design is one reviewing the other.",
1245            raw.agents.len()
1246        );
1247    }
1248
1249    let mut agents = Vec::new();
1250    for (name, value) in raw.agents.iter() {
1251        agents.push(build_spec(name, value)?);
1252    }
1253
1254    let loop_cfg = raw.loop_cfg.unwrap_or_default();
1255    let style = raw.style.unwrap_or_default().to_style();
1256    let listen = raw.listen.unwrap_or_default();
1257
1258    if loop_cfg.max_rounds == 0 {
1259        bail!("max_rounds must be at least 1");
1260    }
1261    if listen.settle > listen.max_wait {
1262        bail!(
1263            "[listen] settle ({}) cannot be longer than max_wait ({}): an item that kept \
1264             changing would never be acted on. Raise max_wait or lower settle.",
1265            listen.settle,
1266            listen.max_wait
1267        );
1268    }
1269    if listen.interval == 0 || listen.sweep == 0 {
1270        bail!(
1271            "[listen] interval and sweep are seconds between polls and must be at least 1. \
1272             Polling never goes below 15 seconds whatever they say."
1273        );
1274    }
1275    // A draft cannot be merged, so merging one means promoting it first, which
1276    // is the one thing `always` asks spar not to do. Refusing is better than
1277    // picking a winner: either setting alone is coherent and only somebody who
1278    // set both can say which they meant.
1279    if loop_cfg.auto_merge && loop_cfg.drafts == Drafts::Always {
1280        bail!(
1281            "auto_merge cannot be on with drafts = \"always\": merging a draft means marking it \
1282             ready, which is what \"always\" asks spar not to do. Use drafts = \"until_approved\" \
1283             to have it promoted when the review converges, or turn auto_merge off."
1284        );
1285    }
1286
1287    // "alternate" is not an agent name, it is a policy: whoever is not the
1288    // agent that wrote the last first draft. Roles alternate within a pull
1289    // request and never between them, so over a queue of twenty issues one
1290    // model writes every first draft and the other reviews every one of them.
1291    // A model's blind spots as an author are systematic, and the ones the pair
1292    // shares go through every time.
1293    let alternate = loop_cfg
1294        .first_implementor
1295        .as_deref()
1296        .is_some_and(|name| name.trim().eq_ignore_ascii_case("alternate"));
1297    let first = match &loop_cfg.first_implementor {
1298        Some(name) if !name.trim().is_empty() && !alternate => name.trim().to_string(),
1299        _ => agents[0].name.clone(),
1300    };
1301    if !agents.iter().any(|a| a.name == first) {
1302        bail!(
1303            "first_implementor '{first}' is not a configured agent ({})",
1304            agents
1305                .iter()
1306                .map(|a| a.name.as_str())
1307                .collect::<Vec<_>>()
1308                .join(", ")
1309        );
1310    }
1311
1312    Ok(Config {
1313        agents,
1314        loop_cfg,
1315        style,
1316        listen,
1317        first_implementor: first,
1318        alternate_first: alternate,
1319        source: None,
1320    })
1321}
1322
1323/// Resolve a configured search path, expanding a leading `~`.
1324pub fn resolve_search_path(raw: &str) -> PathBuf {
1325    expand_tilde(raw)
1326}
1327
1328#[cfg(test)]
1329mod tests {
1330    use super::*;
1331
1332    const TWO_AGENTS: &str = r#"
1333[agents.claude]
1334preset = "claude"
1335model = "fable"
1336
1337[agents.codex]
1338preset = "codex"
1339model = "gpt-5.6-sol"
1340"#;
1341
1342    // -- fallback --------------------------------------------------------
1343
1344    #[test]
1345    fn a_fallback_is_a_whole_agent_with_its_own_preset() {
1346        let text = format!(
1347            "{TWO_AGENTS}\n[agents.codex.fallback]\npreset = \"cursor\"\nmodel = \"kimi-k3\"\n"
1348        );
1349        let cfg = parse(&text).expect("parses");
1350        // Still a pair. A backup is not a third opinion.
1351        assert_eq!(2, cfg.agents.len());
1352        let codex = cfg.spec("codex").expect("codex");
1353        let backup = codex.fallback.as_ref().expect("fallback");
1354        assert_eq!("codex-fallback", backup.name);
1355        assert_eq!(Some("kimi-k3"), backup.model.as_deref());
1356        assert_eq!(
1357            Some(&CommandPart::One("cursor-agent".into())),
1358            backup.command.first()
1359        );
1360    }
1361
1362    #[test]
1363    fn the_agent_without_a_fallback_does_not_grow_one() {
1364        let cfg = parse(TWO_AGENTS).expect("parses");
1365        assert!(cfg.agents.iter().all(|a| a.fallback.is_none()));
1366    }
1367
1368    #[test]
1369    fn a_fallback_may_not_have_one_of_its_own() {
1370        let text = format!(
1371            "{TWO_AGENTS}\n[agents.codex.fallback]\npreset = \"cursor\"\n\
1372             [agents.codex.fallback.fallback]\npreset = \"gemini\"\n"
1373        );
1374        let err = parse(&text).expect_err("rejected");
1375        assert!(err.message().contains("may not have a fallback"), "{err}");
1376    }
1377
1378    #[test]
1379    fn a_fallback_written_as_a_string_says_what_it_should_be() {
1380        let text = "[agents.claude]\npreset = \"claude\"\n\n\
1381                    [agents.codex]\npreset = \"codex\"\nfallback = \"cursor\"\n";
1382        let err = parse(text).expect_err("rejected");
1383        assert!(err.message().contains("[agents.codex.fallback]"), "{err}");
1384    }
1385
1386    /// A block that names one setting keeps the defaults for every setting it
1387    /// did not name. That is what the container level serde default buys: each
1388    /// field used to carry its own default function repeating a number that
1389    /// also lived in `Default`, and the two copies stopped agreeing.
1390    #[test]
1391    fn a_partial_block_keeps_the_defaults_it_did_not_name() {
1392        let text = format!("{TWO_AGENTS}\n[loop]\nmax_rounds = 9\n\n[style]\nterse = false\n");
1393        let cfg = parse(&text).expect("parses");
1394
1395        assert_eq!(9, cfg.loop_cfg.max_rounds);
1396        assert_eq!(LoopCfg::default().followups, cfg.loop_cfg.followups);
1397        assert_eq!(LoopCfg::default().close_skipped, cfg.loop_cfg.close_skipped);
1398
1399        assert!(!cfg.style.terse);
1400        assert_eq!(Style::default().max_body_chars, cfg.style.max_body_chars);
1401        assert_eq!(Style::default().max_title_chars, cfg.style.max_title_chars);
1402    }
1403
1404    /// The budgets are decided in `Style` and read from there by the config
1405    /// layer. When they were written out in both places they drifted, and the
1406    /// generated config offered the older set for months.
1407    #[test]
1408    fn the_config_layer_does_not_keep_its_own_copy_of_the_budgets() {
1409        assert_eq!(Style::default(), StyleCfg::default().to_style());
1410    }
1411
1412    // -- drafts ------------------------------------------------------------
1413
1414    #[test]
1415    fn pull_requests_are_not_drafts_unless_asked_for() {
1416        assert_eq!(Drafts::Never, parse(TWO_AGENTS).unwrap().loop_cfg.drafts);
1417    }
1418
1419    #[test]
1420    fn each_draft_setting_parses() {
1421        for (text, want) in [
1422            ("never", Drafts::Never),
1423            ("until_approved", Drafts::UntilApproved),
1424            ("always", Drafts::Always),
1425        ] {
1426            let cfg = parse(&format!("{TWO_AGENTS}\n[loop]\ndrafts = \"{text}\"\n"))
1427                .unwrap_or_else(|e| panic!("{text}: {e}"));
1428            assert_eq!(want, cfg.loop_cfg.drafts, "{text}");
1429        }
1430    }
1431
1432    /// Merging a draft means marking it ready, which is the one thing `always`
1433    /// asks spar not to do. Either setting alone is coherent, so refusing beats
1434    /// picking a winner between them.
1435    #[test]
1436    fn auto_merge_and_a_permanent_draft_are_refused_together() {
1437        let text = format!("{TWO_AGENTS}\n[loop]\nauto_merge = true\ndrafts = \"always\"\n");
1438        let err = parse(&text).expect_err("refused");
1439        assert!(err.message().contains("auto_merge"), "{err}");
1440        assert!(
1441            err.message().contains("until_approved"),
1442            "says the way out: {err}"
1443        );
1444    }
1445
1446    /// The pairing that does make sense: the draft clears when the review
1447    /// converges, and then it can merge.
1448    #[test]
1449    fn auto_merge_is_fine_with_a_draft_that_clears() {
1450        let text =
1451            format!("{TWO_AGENTS}\n[loop]\nauto_merge = true\ndrafts = \"until_approved\"\n");
1452        assert!(parse(&text).is_ok());
1453    }
1454
1455    #[test]
1456    fn every_builtin_preset_parses() {
1457        for (name, _) in BUILTIN_PRESETS {
1458            let value = load_preset(name).unwrap_or_else(|e| panic!("{name}: {e}"));
1459            assert!(value.get("command").is_some(), "{name} has no command");
1460        }
1461    }
1462
1463    #[test]
1464    fn every_builtin_preset_builds_a_spec() {
1465        for (name, _) in BUILTIN_PRESETS {
1466            let raw = parse_document(&format!("preset = \"{name}\""), "test").unwrap();
1467            build_spec(name, &raw).unwrap_or_else(|e| panic!("{name}: {e}"));
1468        }
1469    }
1470
1471    /// `--allowedTools` is variadic, so the separate form swallows the
1472    /// following positional prompt unless another flag happens to sit between
1473    /// them. The equals form is not cosmetic.
1474    #[test]
1475    fn claude_preset_uses_the_equals_form_for_allowed_tools() {
1476        let spec = build_spec(
1477            "claude",
1478            &parse_document("preset = \"claude\"", "test").unwrap(),
1479        )
1480        .unwrap();
1481        let flat: Vec<&String> = spec.command.iter().flat_map(|p| p.args()).collect();
1482        assert!(flat.iter().any(|a| a.starts_with("--allowedTools=")));
1483        assert!(!flat.iter().any(|a| a.as_str() == "--allowedTools"));
1484    }
1485
1486    #[test]
1487    fn codex_preset_declares_where_its_answer_lives() {
1488        let spec = build_spec(
1489            "codex",
1490            &parse_document("preset = \"codex\"", "test").unwrap(),
1491        )
1492        .unwrap();
1493        assert_eq!(OutputMode::Jsonl, spec.output);
1494        assert_eq!(Some("item.text"), spec.message_path.as_deref());
1495        assert!(!spec.message_match.is_empty());
1496    }
1497
1498    /// ChatGPT 26.924 moved the bundled CLI from `Resources/codex` to
1499    /// `Resources/codex-cli/bin/codex`. An app not yet updated still has the
1500    /// old one, so both are searched, the current layout first.
1501    #[test]
1502    fn codex_preset_looks_where_either_chatgpt_layout_keeps_the_cli() {
1503        let spec = build_spec(
1504            "codex",
1505            &parse_document("preset = \"codex\"", "test").unwrap(),
1506        )
1507        .unwrap();
1508        let at = |path: &str| spec.search_paths.iter().position(|p| p == path);
1509        let current = at("/Applications/ChatGPT.app/Contents/Resources/codex-cli/bin");
1510        let older = at("/Applications/ChatGPT.app/Contents/Resources");
1511        assert!(current.is_some(), "{:?}", spec.search_paths);
1512        assert!(older.is_some(), "{:?}", spec.search_paths);
1513        assert!(current < older, "{:?}", spec.search_paths);
1514    }
1515
1516    #[test]
1517    fn agent_order_follows_declaration_order() {
1518        let cfg = parse(TWO_AGENTS).unwrap();
1519        assert_eq!(vec!["claude", "codex"], cfg.agent_names());
1520        assert_eq!("claude", cfg.first_implementor);
1521    }
1522
1523    #[test]
1524    fn other_alternates() {
1525        let cfg = parse(TWO_AGENTS).unwrap();
1526        assert_eq!("codex", cfg.other("claude"));
1527        assert_eq!("claude", cfg.other("codex"));
1528    }
1529
1530    #[test]
1531    fn a_config_block_overrides_one_preset_field() {
1532        let cfg = parse(TWO_AGENTS).unwrap();
1533        let claude = cfg.spec("claude").unwrap();
1534        assert_eq!(Some("fable"), claude.model.as_deref());
1535        assert!(claude.command.len() > 1, "the preset command survived");
1536    }
1537
1538    #[test]
1539    fn exactly_two_agents_are_required() {
1540        let one = "[agents.claude]\npreset = \"claude\"\n";
1541        assert!(parse(one).unwrap_err().to_string().contains("exactly two"));
1542    }
1543
1544    #[test]
1545    fn an_unknown_agent_option_is_named() {
1546        let text = "[agents.a]\ncommand = [\"x\"]\nwidget = 3\n[agents.b]\ncommand = [\"y\"]\n";
1547        let err = parse(text).unwrap_err().to_string();
1548        assert!(err.contains("widget"), "{err}");
1549    }
1550
1551    #[test]
1552    fn an_unknown_loop_option_is_named() {
1553        let text = format!("{TWO_AGENTS}\n[loop]\nmax_round = 4\n");
1554        let err = parse(&text).unwrap_err().to_string();
1555        assert!(err.contains("max_round"), "{err}");
1556    }
1557
1558    #[test]
1559    fn an_unknown_listen_option_is_named() {
1560        let text = format!("{TWO_AGENTS}\n[listen]\ninterval_secs = 30\n");
1561        let err = parse(&text).unwrap_err().to_string();
1562        assert!(err.contains("interval_secs"), "{err}");
1563    }
1564
1565    #[test]
1566    fn a_listen_block_keeps_the_defaults_it_did_not_name() {
1567        let text = format!("{TWO_AGENTS}\n[listen]\nsweep = 30\n");
1568        let cfg = parse(&text).expect("parses");
1569        assert_eq!(30, cfg.listen.sweep);
1570        assert_eq!(ListenCfg::default().interval, cfg.listen.interval);
1571        assert_eq!(ListenCfg::default().settle, cfg.listen.settle);
1572        assert_eq!(OwnPr::Checkin, cfg.listen.on_own_pr);
1573        assert!(cfg.listen.verdicts);
1574
1575        let bare = parse(TWO_AGENTS).unwrap();
1576        assert_eq!(60, bare.listen.interval);
1577        assert_eq!(300, bare.listen.sweep);
1578        assert_eq!(120, bare.listen.settle);
1579        assert_eq!(900, bare.listen.max_wait);
1580        assert_eq!(5, bare.listen.max_rechecks);
1581        assert_eq!(ReviewOrIgnore::Review, bare.listen.on_review_requested);
1582        assert_eq!(ReviewOrIgnore::Review, bare.listen.on_assigned);
1583    }
1584
1585    #[test]
1586    fn settle_longer_than_max_wait_is_refused_with_a_reason() {
1587        let text = format!("{TWO_AGENTS}\n[listen]\nsettle = 1000\nmax_wait = 900\n");
1588        let err = parse(&text).unwrap_err().to_string();
1589        assert!(err.contains("settle"), "{err}");
1590        assert!(err.contains("Raise max_wait"), "{err}");
1591
1592        let text = format!("{TWO_AGENTS}\n[listen]\ninterval = 0\n");
1593        let err = parse(&text).unwrap_err().to_string();
1594        assert!(err.contains("at least 1"), "{err}");
1595    }
1596
1597    #[test]
1598    fn each_listen_action_word_parses() {
1599        for (text, want) in [
1600            ("review", ReviewOrIgnore::Review),
1601            ("ignore", ReviewOrIgnore::Ignore),
1602        ] {
1603            let cfg = parse(&format!(
1604                "{TWO_AGENTS}\n[listen]\non_review_requested = \"{text}\"\non_assigned = \"{text}\"\n"
1605            ))
1606            .unwrap();
1607            assert_eq!(want, cfg.listen.on_review_requested);
1608            assert_eq!(want, cfg.listen.on_assigned);
1609            assert_eq!(text, want.to_string());
1610        }
1611        for (text, want) in [
1612            ("checkin", OwnPr::Checkin),
1613            ("reply", OwnPr::Reply),
1614            ("ignore", OwnPr::Ignore),
1615        ] {
1616            let cfg = parse(&format!("{TWO_AGENTS}\n[listen]\non_own_pr = \"{text}\"\n")).unwrap();
1617            assert_eq!(want, cfg.listen.on_own_pr);
1618            assert_eq!(text, want.to_string());
1619        }
1620        let err = parse(&format!("{TWO_AGENTS}\n[listen]\non_own_pr = \"push\"\n"))
1621            .unwrap_err()
1622            .to_string();
1623        assert!(err.contains("on_own_pr") || err.contains("push"), "{err}");
1624    }
1625
1626    #[test]
1627    fn a_recheck_reads_its_own_key_before_review_rest_and_falls_back_to_it() {
1628        let text = format!(
1629            "{TWO_AGENTS}\n[loop.effort_schedule]\nreview_rest = \"low\"\nrecheck = \"high\"\n"
1630        );
1631        let cfg = parse(&text).unwrap();
1632        let spec = cfg.spec("claude").unwrap();
1633        assert_eq!(
1634            Some("high".to_string()),
1635            cfg.effort_for(spec, Call::Recheck)
1636        );
1637
1638        let text = format!("{TWO_AGENTS}\n[loop.effort_schedule]\nreview_rest = \"low\"\n");
1639        let cfg = parse(&text).unwrap();
1640        let spec = cfg.spec("claude").unwrap();
1641        assert_eq!(Some("low".to_string()), cfg.effort_for(spec, Call::Recheck));
1642
1643        let text = format!(
1644            "{TWO_AGENTS}\n[loop.effort_schedule]\nround_1 = \"ultra\"\nrest = \"medium\"\n"
1645        );
1646        let cfg = parse(&text).unwrap();
1647        let spec = cfg.spec("claude").unwrap();
1648        assert_eq!(
1649            Some("medium".to_string()),
1650            cfg.effort_for(spec, Call::Recheck)
1651        );
1652    }
1653
1654    #[test]
1655    fn every_call_kind_is_in_every() {
1656        let every = Call::every();
1657        assert_eq!(12, every.len());
1658        assert!(every.contains(&Call::Code));
1659        assert!(every.contains(&Call::Recheck));
1660        assert_eq!("recheck", Call::Recheck.key());
1661    }
1662
1663    #[test]
1664    fn an_agent_with_no_command_and_no_preset_is_rejected() {
1665        let text = "[agents.a]\nmodel = \"x\"\n[agents.b]\ncommand = [\"y\"]\n";
1666        let err = parse(text).unwrap_err().to_string();
1667        assert!(err.contains("no command and no preset"), "{err}");
1668    }
1669
1670    #[test]
1671    fn jsonl_without_a_message_path_is_rejected() {
1672        let text =
1673            "[agents.a]\ncommand = [\"x\"]\noutput = \"jsonl\"\n[agents.b]\ncommand = [\"y\"]\n";
1674        let err = parse(text).unwrap_err().to_string();
1675        assert!(err.contains("message_path"), "{err}");
1676    }
1677
1678    /// One agent wrote every first draft, so half the pair's diversity went
1679    /// unused: a model's blind spots as an author are systematic, and the ones
1680    /// the two share go through every time.
1681    /// The one claim a report makes that nothing else checks.
1682    #[test]
1683    fn a_check_is_read_as_an_argv_either_way_it_is_written() {
1684        let line = format!("{TWO_AGENTS}\n[loop]\ncheck = \"cargo test --all\"\n");
1685        let cfg = parse(&line).unwrap();
1686        assert!(cfg.loop_cfg.check.is_set());
1687        assert_eq!(vec!["cargo", "test", "--all"], cfg.loop_cfg.check.argv());
1688        assert_eq!("cargo test --all", cfg.loop_cfg.check.describe());
1689
1690        let listed =
1691            format!("{TWO_AGENTS}\n[loop]\ncheck = [\"npm\", \"run\", \"test -- --ci\"]\n");
1692        let cfg = parse(&listed).unwrap();
1693        assert_eq!(
1694            vec!["npm", "run", "test -- --ci"],
1695            cfg.loop_cfg.check.argv(),
1696            "a list is taken as it is, spaces and all"
1697        );
1698
1699        // Unset is the default, and runs nothing.
1700        let bare = parse(TWO_AGENTS).unwrap();
1701        assert!(!bare.loop_cfg.check.is_set());
1702        assert!(bare.loop_cfg.check.argv().is_empty());
1703    }
1704
1705    #[test]
1706    fn a_setup_is_read_like_a_check_and_runs_nothing_unset() {
1707        let line = format!("{TWO_AGENTS}\n[loop]\nsetup = \"npm ci --no-audit\"\n");
1708        let cfg = parse(&line).unwrap();
1709        assert_eq!(vec!["npm", "ci", "--no-audit"], cfg.loop_cfg.setup.argv());
1710        assert!(
1711            !cfg.loop_cfg.check.is_set(),
1712            "setting up a worktree is not checking it"
1713        );
1714
1715        let bare = parse(TWO_AGENTS).unwrap();
1716        assert!(!bare.loop_cfg.setup.is_set());
1717    }
1718
1719    #[test]
1720    fn alternating_swaps_the_author_from_issue_to_issue() {
1721        let text = format!("{TWO_AGENTS}\n[loop]\nfirst_implementor = \"alternate\"\n");
1722        let cfg = parse(&text).unwrap();
1723
1724        assert!(cfg.alternate_first);
1725        assert_eq!(
1726            "claude", cfg.first_implementor,
1727            "declaration order still leads"
1728        );
1729        assert_eq!("claude", cfg.first_implementor_for(0));
1730        assert_eq!("codex", cfg.first_implementor_for(1));
1731        assert_eq!("claude", cfg.first_implementor_for(2));
1732        assert!(cfg.first_implementor_policy().contains("alternating"));
1733    }
1734
1735    /// Naming an agent still pins it, which is what --first does for a run.
1736    #[test]
1737    fn a_named_first_implementor_never_swaps() {
1738        let text = format!("{TWO_AGENTS}\n[loop]\nfirst_implementor = \"codex\"\n");
1739        let cfg = parse(&text).unwrap();
1740        assert!(!cfg.alternate_first);
1741        for nth in 0..4 {
1742            assert_eq!("codex", cfg.first_implementor_for(nth));
1743        }
1744        assert_eq!("codex", cfg.first_implementor_policy());
1745    }
1746
1747    #[test]
1748    fn first_implementor_must_name_a_configured_agent() {
1749        let text = format!("{TWO_AGENTS}\n[loop]\nfirst_implementor = \"nobody\"\n");
1750        let err = parse(&text).unwrap_err().to_string();
1751        assert!(err.contains("not a configured agent"), "{err}");
1752    }
1753
1754    #[test]
1755    fn defaults_are_the_conservative_ones() {
1756        let cfg = parse(TWO_AGENTS).unwrap();
1757        assert!(
1758            !cfg.loop_cfg.auto_merge,
1759            "auto_merge must be off by default"
1760        );
1761        assert!(cfg.loop_cfg.worktrees);
1762        assert!(
1763            !cfg.loop_cfg.file_nits,
1764            "a filed nit is somebody else's triage queue"
1765        );
1766        assert_eq!(3, cfg.loop_cfg.max_rounds);
1767        assert_eq!(
1768            Followups::Local,
1769            cfg.loop_cfg.followups,
1770            "the tracker is somebody's queue; the default must not write to it"
1771        );
1772        assert!(
1773            !cfg.loop_cfg.file_non_blocking,
1774            "a suggestion is not a tracker item"
1775        );
1776        assert_eq!(StateStore::Local, cfg.loop_cfg.state_store);
1777        assert!(cfg.style.terse);
1778    }
1779
1780    #[test]
1781    fn effort_schedule_splits_round_one_from_the_rest() {
1782        let text =
1783            format!("{TWO_AGENTS}\n[loop.effort_schedule]\nround_1 = \"ultra\"\nrest = \"high\"\n");
1784        let cfg = parse(&text).unwrap();
1785        let spec = cfg.spec("claude").unwrap();
1786        assert_eq!(Some("ultra".into()), cfg.effort_for_round(spec, 1));
1787        assert_eq!(Some("high".into()), cfg.effort_for_round(spec, 2));
1788        assert_eq!(Some("high".into()), cfg.effort_for_round(spec, 9));
1789    }
1790
1791    /// The schedule is one string handed to whichever agent holds the round,
1792    /// and the two CLIs do not share a vocabulary.
1793    ///
1794    /// The README's own example pairs round_1 = "ultra" with a claude agent at
1795    /// effort = "high", and ultra is not a value the claude preset accepts, so
1796    /// the first review failed at the CLI whenever claude reviewed first.
1797    #[test]
1798    fn an_agent_can_carry_its_own_schedule_in_its_own_words() {
1799        let text = format!(
1800            "{TWO_AGENTS}\n[agents.claude.effort_schedule]\nreview_1 = \"xhigh\"\n\n\
1801             [loop.effort_schedule]\nreview_1 = \"ultra\"\n"
1802        );
1803        let cfg = parse(&text).unwrap();
1804        assert_eq!(
1805            Some("xhigh".into()),
1806            cfg.effort_for(cfg.spec("claude").unwrap(), Call::Review(1)),
1807            "the agent's own schedule has to win"
1808        );
1809        assert_eq!(
1810            Some("ultra".into()),
1811            cfg.effort_for(cfg.spec("codex").unwrap(), Call::Review(1))
1812        );
1813    }
1814
1815    /// Every call used to read round_1 or rest by a round number most of them
1816    /// do not have, so round_1 = "ultra" put triage over the whole queue, the
1817    /// implement call and the follow-up screen at ultra too.
1818    #[test]
1819    fn each_kind_of_call_reads_its_own_key_and_falls_back_to_the_old_pair() {
1820        let text = format!(
1821            "{TWO_AGENTS}\n[loop.effort_schedule]\nround_1 = \"ultra\"\nrest = \"low\"\n\
1822             triage = \"minimal\"\n"
1823        );
1824        let cfg = parse(&text).unwrap();
1825        let spec = cfg.spec("claude").unwrap();
1826
1827        assert_eq!(Some("minimal".into()), cfg.effort_for(spec, Call::Triage));
1828        // No key of its own: exactly what the two key schedule did.
1829        for call in [Call::Implement, Call::Review(1), Call::Screen] {
1830            assert_eq!(Some("ultra".into()), cfg.effort_for(spec, call), "{call:?}");
1831        }
1832        for call in [Call::Review(2), Call::Close, Call::Respond(3)] {
1833            assert_eq!(Some("low".into()), cfg.effort_for(spec, call), "{call:?}");
1834        }
1835    }
1836
1837    #[test]
1838    fn effort_falls_back_to_the_agents_own_setting() {
1839        let text = format!("{TWO_AGENTS}effort = \"low\"\n");
1840        let cfg = parse(&text).unwrap();
1841        let spec = cfg.spec("codex").unwrap();
1842        assert_eq!(Some("low".into()), cfg.effort_for_round(spec, 1));
1843    }
1844
1845    #[test]
1846    fn an_unset_model_and_an_empty_model_normalise_the_same() {
1847        let a = AgentSpec {
1848            name: "a".into(),
1849            command: vec![CommandPart::One("x".into())],
1850            model: None,
1851            effort: None,
1852            effort_schedule: EffortSchedule::default(),
1853            output: OutputMode::Text,
1854            message_match: BTreeMap::new(),
1855            message_path: None,
1856            search_paths: vec![],
1857            system_via: SystemVia::Prompt,
1858            timeout: 60,
1859            fallback: None,
1860            models: vec![],
1861            efforts: vec![],
1862            options_note: None,
1863        };
1864        let b = AgentSpec {
1865            model: Some("  ".into()),
1866            ..a.clone()
1867        };
1868        assert_eq!(a.model_key(), b.model_key());
1869    }
1870
1871    #[test]
1872    fn max_rounds_zero_is_rejected() {
1873        let text = format!("{TWO_AGENTS}\n[loop]\nmax_rounds = 0\n");
1874        assert!(parse(&text).is_err());
1875    }
1876
1877    #[test]
1878    fn an_inline_command_needs_no_preset() {
1879        let text = r#"
1880[agents.custom]
1881command = ["mytool", ["-m", "{model}"], "--prompt", "{prompt}"]
1882output = "text"
1883
1884[agents.other]
1885command = ["othertool", "{prompt}"]
1886"#;
1887        let cfg = parse(text).unwrap();
1888        assert_eq!(4, cfg.spec("custom").unwrap().command.len());
1889    }
1890
1891    #[test]
1892    fn style_budgets_are_configurable() {
1893        let text = format!("{TWO_AGENTS}\n[style]\nterse = false\nmax_detail_chars = 40\n");
1894        let cfg = parse(&text).unwrap();
1895        assert!(!cfg.style.terse);
1896        assert_eq!(40, cfg.style.max_detail_chars);
1897    }
1898}