onerig-core 0.1.1

Core library of onerig: run state machine, lane policy and harness adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//! The configuration file of design section 11: its data model, loading and
//! validation.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::Duration;

use serde::Deserialize;

use crate::plan::is_valid_id;

#[derive(Debug, Clone, PartialEq)]
pub struct Config {
    pub run: RunSettings,
    pub policy: PolicySettings,
    pub roles: Roles,
    pub lanes: BTreeMap<String, Lane>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunSettings {
    pub max_attempts: u32,
    pub idle_timeout: Duration,
    pub job_timeout: Duration,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PolicySettings {
    pub min_remaining: f64,
}

/// Lane names per role. Planner and reviewer have exactly one lane each.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Roles {
    pub planner: String,
    pub reviewer: String,
    pub worker: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HarnessKind {
    Claude,
    Agy,
    Codex,
    /// Scripted lane for tests; accepted only when `ONERIG_TEST` is set.
    Fake,
}

impl HarnessKind {
    /// The name used in the configuration file.
    pub fn name(self) -> &'static str {
        match self {
            HarnessKind::Claude => "claude",
            HarnessKind::Agy => "agy",
            HarnessKind::Codex => "codex",
            HarnessKind::Fake => "fake",
        }
    }
}

/// How an `agy` lane is driven. Lanes of every other harness are always `Direct`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Off,
    /// A human runs the harness; onerig starts no process for this lane.
    Handover,
    Direct,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Effort {
    Low,
    Medium,
    High,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Lane {
    pub harness: HarnessKind,
    /// Binary name or path; defaults to the harness's usual name.
    pub bin: String,
    /// Required for every harness except `Fake`.
    pub model: Option<String>,
    pub effort: Effort,
    pub mode: Mode,
    pub quota_group: Option<String>,
    pub accept_vendor_terms_risk: bool,
    /// Script file of a `Fake` lane.
    pub script: Option<PathBuf>,
}

impl Lane {
    /// A manual lane is done by a human: onerig writes a hand-over and pauses.
    pub fn is_manual(&self) -> bool {
        self.mode == Mode::Handover
    }

    pub fn is_off(&self) -> bool {
        self.mode == Mode::Off
    }
}

/// The example configuration of design section 11; printed when no file exists.
pub const EXAMPLE: &str = r#"[run]
max_attempts = 5
idle_timeout = "5m"
job_timeout = "30m"

[policy]
min_remaining = 0.05

[roles]
planner  = ["plan"]
worker   = ["gemini", "deepseek"]
reviewer = ["review"]

[lanes.plan]
harness = "claude"
model = "sonnet"
effort = "medium"

[lanes.review]
harness = "claude"
model = "sonnet"
effort = "low"

[lanes.deepseek]
harness = "codex"
model = "deepseek-flash"

[lanes.gemini]
harness = "agy"
mode = "handover"
model = "gemini-4-hydrogen-medium"
quota_group = "Gemini Models"
# accept_vendor_terms_risk = true    # required for mode = "direct"
"#;

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ConfigError {
    #[error("no configuration file at {}; create one (an example follows)", .0.display())]
    Missing(PathBuf),
    #[error("cannot read {}: {message}", path.display())]
    Unreadable { path: PathBuf, message: String },
    #[error("the configuration is not valid TOML of the expected shape: {0}")]
    Syntax(String),
    #[error("{key}: {problem}")]
    Invalid { key: String, problem: String },
}

fn invalid(key: impl Into<String>, problem: impl Into<String>) -> ConfigError {
    ConfigError::Invalid {
        key: key.into(),
        problem: problem.into(),
    }
}

/// `$XDG_CONFIG_HOME/onerig/config.toml`, else `~/.config/onerig/config.toml`.
/// The caller reads the environment.
pub fn default_path(xdg_config_home: Option<&str>, home: Option<&str>) -> Option<PathBuf> {
    let base = match (xdg_config_home.filter(|v| !v.is_empty()), home) {
        (Some(xdg), _) => PathBuf::from(xdg),
        (None, Some(home)) if !home.is_empty() => Path::new(home).join(".config"),
        _ => return None,
    };
    Some(base.join("onerig").join("config.toml"))
}

pub fn load(path: &Path, allow_fake: bool) -> Result<Config, ConfigError> {
    let text = std::fs::read_to_string(path).map_err(|e| match e.kind() {
        std::io::ErrorKind::NotFound => ConfigError::Missing(path.to_path_buf()),
        _ => ConfigError::Unreadable {
            path: path.to_path_buf(),
            message: e.to_string(),
        },
    })?;
    parse(&text, allow_fake)
}

/// An integer followed by `s`, `m` or `h`.
pub fn parse_duration(text: &str) -> Option<Duration> {
    let seconds_per_unit = match text.chars().last()? {
        's' => 1,
        'm' => 60,
        'h' => 3600,
        _ => return None,
    };
    // The unit is one ASCII byte; `parse` alone would also accept a sign.
    let number = &text[..text.len() - 1];
    if !number.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let value: u64 = number.parse().ok()?;
    value.checked_mul(seconds_per_unit).map(Duration::from_secs)
}

pub fn parse(text: &str, allow_fake: bool) -> Result<Config, ConfigError> {
    let raw: RawConfig = toml::from_str(text).map_err(|e| ConfigError::Syntax(e.to_string()))?;
    let run = run_settings(&raw.run)?;
    let policy = policy_settings(&raw.policy)?;
    let lanes = raw
        .lanes
        .iter()
        .map(|(name, lane)| Ok((name.clone(), lane_from(name, lane, allow_fake)?)))
        .collect::<Result<BTreeMap<_, _>, ConfigError>>()?;
    let roles = roles_from(&raw.roles, &lanes)?;
    Ok(Config {
        run,
        policy,
        roles,
        lanes,
    })
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawConfig {
    #[serde(default)]
    run: RawRun,
    #[serde(default)]
    policy: RawPolicy,
    roles: RawRoles,
    lanes: BTreeMap<String, RawLane>,
}

#[derive(Deserialize, Default)]
#[serde(deny_unknown_fields)]
struct RawRun {
    max_attempts: Option<i64>,
    idle_timeout: Option<String>,
    job_timeout: Option<String>,
}

#[derive(Deserialize, Default)]
#[serde(deny_unknown_fields)]
struct RawPolicy {
    min_remaining: Option<f64>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawRoles {
    planner: Vec<String>,
    reviewer: Vec<String>,
    worker: Vec<String>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawLane {
    harness: String,
    bin: Option<String>,
    model: Option<String>,
    effort: Option<String>,
    mode: Option<String>,
    quota_group: Option<String>,
    accept_vendor_terms_risk: Option<bool>,
    script: Option<PathBuf>,
}

fn run_settings(raw: &RawRun) -> Result<RunSettings, ConfigError> {
    let max_attempts = raw.max_attempts.unwrap_or(5);
    if !(1..=20).contains(&max_attempts) {
        return Err(invalid("run.max_attempts", "use a number from 1 to 20"));
    }
    Ok(RunSettings {
        max_attempts: max_attempts as u32,
        idle_timeout: duration_setting("run.idle_timeout", raw.idle_timeout.as_deref(), 300)?,
        job_timeout: duration_setting("run.job_timeout", raw.job_timeout.as_deref(), 1800)?,
    })
}

fn duration_setting(
    key: &str,
    text: Option<&str>,
    default_secs: u64,
) -> Result<Duration, ConfigError> {
    let Some(text) = text else {
        return Ok(Duration::from_secs(default_secs));
    };
    match parse_duration(text) {
        Some(duration) if duration >= Duration::from_secs(1) => Ok(duration),
        Some(_) => Err(invalid(key, "use at least 1s")),
        None => Err(invalid(
            key,
            "use a whole number followed by s, m or h, for example \"5m\"",
        )),
    }
}

fn policy_settings(raw: &RawPolicy) -> Result<PolicySettings, ConfigError> {
    let min_remaining = raw.min_remaining.unwrap_or(0.05);
    if !(0.0..=1.0).contains(&min_remaining) {
        return Err(invalid("policy.min_remaining", "use a number from 0 to 1"));
    }
    Ok(PolicySettings { min_remaining })
}

fn lane_from(name: &str, raw: &RawLane, allow_fake: bool) -> Result<Lane, ConfigError> {
    if !is_valid_id(name) {
        return Err(invalid(
            format!("lanes.{name}"),
            "a lane name uses lowercase letters, digits, '-' and '_', at most 32 characters",
        ));
    }
    let harness = harness_kind(name, &raw.harness, allow_fake)?;
    check_keys_apply(name, raw, harness)?;
    let mode = mode_of(name, raw, harness)?;
    check_direct_agy(name, raw, harness, mode)?;
    Ok(Lane {
        harness,
        bin: raw
            .bin
            .clone()
            .unwrap_or_else(|| harness.name().to_string()),
        model: required(
            name,
            "model",
            raw.model.clone(),
            harness != HarnessKind::Fake,
        )?,
        effort: effort_of(name, raw)?,
        mode,
        quota_group: raw.quota_group.clone(),
        accept_vendor_terms_risk: raw.accept_vendor_terms_risk.unwrap_or(false),
        script: required(
            name,
            "script",
            raw.script.clone(),
            harness == HarnessKind::Fake,
        )?,
    })
}

fn harness_kind(name: &str, text: &str, allow_fake: bool) -> Result<HarnessKind, ConfigError> {
    let key = format!("lanes.{name}.harness");
    match text {
        "claude" => Ok(HarnessKind::Claude),
        "agy" => Ok(HarnessKind::Agy),
        "codex" => Ok(HarnessKind::Codex),
        "fake" if allow_fake => Ok(HarnessKind::Fake),
        "fake" => Err(invalid(
            key,
            "fake lanes are for tests and need ONERIG_TEST to be set",
        )),
        _ => Err(invalid(key, "use claude, agy or codex")),
    }
}

fn check_keys_apply(name: &str, raw: &RawLane, harness: HarnessKind) -> Result<(), ConfigError> {
    use HarnessKind::{Agy, Claude, Codex, Fake};
    let rules: [(&str, bool, &[HarnessKind]); 6] = [
        ("model", raw.model.is_some(), &[Claude, Agy, Codex]),
        ("effort", raw.effort.is_some(), &[Claude]),
        ("mode", raw.mode.is_some(), &[Agy]),
        ("quota_group", raw.quota_group.is_some(), &[Agy]),
        (
            "accept_vendor_terms_risk",
            raw.accept_vendor_terms_risk.is_some(),
            &[Agy],
        ),
        ("script", raw.script.is_some(), &[Fake]),
    ];
    match rules
        .iter()
        .find(|(_, set, allowed)| *set && !allowed.contains(&harness))
    {
        Some((key, _, _)) => Err(invalid(
            format!("lanes.{name}.{key}"),
            format!("does not apply to a {} lane", harness.name()),
        )),
        None => Ok(()),
    }
}

fn required<T>(
    name: &str,
    key: &str,
    value: Option<T>,
    needed: bool,
) -> Result<Option<T>, ConfigError> {
    match (needed, value) {
        (true, None) => Err(invalid(format!("lanes.{name}.{key}"), "is required")),
        (_, value) => Ok(value),
    }
}

fn mode_of(name: &str, raw: &RawLane, harness: HarnessKind) -> Result<Mode, ConfigError> {
    if harness != HarnessKind::Agy {
        return Ok(Mode::Direct);
    }
    match raw.mode.as_deref().unwrap_or("handover") {
        "off" => Ok(Mode::Off),
        "handover" => Ok(Mode::Handover),
        "direct" => Ok(Mode::Direct),
        _ => Err(invalid(
            format!("lanes.{name}.mode"),
            "use off, handover or direct",
        )),
    }
}

fn check_direct_agy(
    name: &str,
    raw: &RawLane,
    harness: HarnessKind,
    mode: Mode,
) -> Result<(), ConfigError> {
    if harness != HarnessKind::Agy || mode != Mode::Direct {
        return Ok(());
    }
    if raw.accept_vendor_terms_risk != Some(true) {
        return Err(invalid(
            format!("lanes.{name}.accept_vendor_terms_risk"),
            "mode = \"direct\" lets onerig drive agy on your Google sign-in, which Google's \
             Antigravity terms treat as a breach that can suspend the account; set this to true \
             to accept that risk",
        ));
    }
    required(name, "quota_group", raw.quota_group.as_ref(), true).map(|_| ())
}

fn effort_of(name: &str, raw: &RawLane) -> Result<Effort, ConfigError> {
    match raw.effort.as_deref().unwrap_or("medium") {
        "low" => Ok(Effort::Low),
        "medium" => Ok(Effort::Medium),
        "high" => Ok(Effort::High),
        _ => Err(invalid(
            format!("lanes.{name}.effort"),
            "use low, medium or high",
        )),
    }
}

fn roles_from(raw: &RawRoles, lanes: &BTreeMap<String, Lane>) -> Result<Roles, ConfigError> {
    Ok(Roles {
        planner: single_lane("roles.planner", &raw.planner, lanes)?,
        reviewer: single_lane("roles.reviewer", &raw.reviewer, lanes)?,
        worker: worker_lanes(&raw.worker, lanes)?,
    })
}

fn existing<'a>(
    key: &str,
    name: &str,
    lanes: &'a BTreeMap<String, Lane>,
) -> Result<&'a Lane, ConfigError> {
    lanes
        .get(name)
        .ok_or_else(|| invalid(key, format!("there is no lane named {name}")))
}

fn single_lane(
    key: &str,
    names: &[String],
    lanes: &BTreeMap<String, Lane>,
) -> Result<String, ConfigError> {
    let [name] = names else {
        return Err(invalid(key, "name exactly one lane"));
    };
    let lane = existing(key, name, lanes)?;
    if !matches!(lane.harness, HarnessKind::Claude | HarnessKind::Fake) {
        return Err(invalid(key, format!("lane {name} must use harness claude")));
    }
    Ok(name.clone())
}

fn worker_lanes(
    names: &[String],
    lanes: &BTreeMap<String, Lane>,
) -> Result<Vec<String>, ConfigError> {
    const KEY: &str = "roles.worker";
    if names.is_empty() {
        return Err(invalid(KEY, "name at least one lane"));
    }
    for (index, name) in names.iter().enumerate() {
        if names[..index].contains(name) {
            return Err(invalid(KEY, format!("lane {name} is named twice")));
        }
        let lane = existing(KEY, name, lanes)?;
        if lane.harness == HarnessKind::Claude {
            return Err(invalid(
                KEY,
                format!("lane {name} must use harness codex or agy"),
            ));
        }
    }
    if names.iter().all(|name| lanes[name].is_off()) {
        return Err(invalid(KEY, "at least one worker lane must not be off"));
    }
    Ok(names.to_vec())
}

#[cfg(test)]
mod tests {
    use super::*;

    const BASE: &str = r#"
[roles]
planner = ["plan"]
reviewer = ["review"]
worker = ["deepseek"]

[lanes.plan]
harness = "claude"
model = "sonnet"

[lanes.review]
harness = "claude"
model = "sonnet"

[lanes.deepseek]
harness = "codex"
model = "deepseek-flash"
"#;

    const GEMINI: &str = "\n[lanes.gemini]\nharness = \"agy\"\nmodel = \"m\"\n";

    /// BASE with `extra` appended; appended keys land in the last table.
    fn with(extra: &str) -> String {
        format!("{BASE}{extra}\n")
    }

    fn with_gemini(extra: &str) -> String {
        with(&format!("{GEMINI}{extra}")).replace(
            r#"worker = ["deepseek"]"#,
            r#"worker = ["deepseek", "gemini"]"#,
        )
    }

    fn invalid_key(text: &str) -> String {
        match parse(text, false) {
            Err(ConfigError::Invalid { key, .. }) => key,
            other => panic!("expected an invalid key, got {other:?}"),
        }
    }

    #[test]
    fn the_example_is_valid_and_keeps_agy_manual() {
        let config = parse(EXAMPLE, false).unwrap();

        assert_eq!(config.roles.worker, ["gemini", "deepseek"]);
        assert_eq!(config.lanes["gemini"].mode, Mode::Handover);
        assert!(config.lanes["gemini"].is_manual());
        assert_eq!(config.lanes["deepseek"].mode, Mode::Direct);
        assert_eq!(config.lanes["review"].effort, Effort::Low);
    }

    #[test]
    fn missing_settings_take_their_defaults() {
        let config = parse(BASE, false).unwrap();

        assert_eq!(config.run.max_attempts, 5);
        assert_eq!(config.run.idle_timeout, Duration::from_secs(300));
        assert_eq!(config.run.job_timeout, Duration::from_secs(1800));
        assert_eq!(config.policy.min_remaining, 0.05);
        assert_eq!(config.lanes["deepseek"].bin, "codex");
        assert_eq!(config.lanes["plan"].effort, Effort::Medium);
        assert_eq!(config.roles.planner, "plan");
    }

    #[test]
    fn an_unknown_key_or_a_wrong_type_is_a_syntax_error_naming_it() {
        let unknown = parse(&with("\n[run]\nmax_attempt = 3"), false);
        let wrong_type = parse(&with("\n[run]\nmax_attempts = \"3\""), false);

        assert!(matches!(&unknown, Err(ConfigError::Syntax(m)) if m.contains("max_attempt")));
        assert!(matches!(wrong_type, Err(ConfigError::Syntax(_))));
    }

    #[test]
    fn max_attempts_must_be_from_1_to_20() {
        assert_eq!(
            invalid_key(&with("\n[run]\nmax_attempts = 0")),
            "run.max_attempts"
        );
        assert_eq!(
            invalid_key(&with("\n[run]\nmax_attempts = 21")),
            "run.max_attempts"
        );
        assert_eq!(
            parse(&with("\n[run]\nmax_attempts = 20"), false)
                .unwrap()
                .run
                .max_attempts,
            20
        );
    }

    #[test]
    fn durations_need_a_unit_and_at_least_one_second() {
        for bad in ["\"5\"", "\"5d\"", "\"m\"", "\"0s\"", "\"-1s\""] {
            assert_eq!(
                invalid_key(&with(&format!("\n[run]\nidle_timeout = {bad}"))),
                "run.idle_timeout",
                "{bad}"
            );
        }
        let config = parse(
            &with("\n[run]\nidle_timeout = \"1s\"\njob_timeout = \"2h\""),
            false,
        )
        .unwrap();
        assert_eq!(config.run.idle_timeout, Duration::from_secs(1));
        assert_eq!(config.run.job_timeout, Duration::from_secs(7200));
    }

    #[test]
    fn parse_duration_reads_seconds_minutes_and_hours_only() {
        assert_eq!(parse_duration("45s"), Some(Duration::from_secs(45)));
        assert_eq!(parse_duration("5m"), Some(Duration::from_secs(300)));
        assert_eq!(parse_duration("1h"), Some(Duration::from_secs(3600)));
        for bad in ["", "s", "5", "5x", " 5m", "5.5m", "+5m", "é"] {
            assert_eq!(parse_duration(bad), None, "{bad:?}");
        }
    }

    #[test]
    fn min_remaining_must_be_from_0_to_1() {
        assert_eq!(
            invalid_key(&with("\n[policy]\nmin_remaining = 1.5")),
            "policy.min_remaining"
        );
        assert_eq!(
            invalid_key(&with("\n[policy]\nmin_remaining = -0.1")),
            "policy.min_remaining"
        );
        assert_eq!(
            parse(&with("\n[policy]\nmin_remaining = 1.0"), false)
                .unwrap()
                .policy
                .min_remaining,
            1.0
        );
    }

    #[test]
    fn a_lane_name_must_be_safe() {
        let text = with("\n[lanes.Deep]\nharness = \"codex\"\nmodel = \"m\"");

        assert_eq!(invalid_key(&text), "lanes.Deep");
    }

    #[test]
    fn an_unknown_harness_is_rejected() {
        let text = BASE.replace(r#"harness = "codex""#, r#"harness = "aider""#);

        assert_eq!(invalid_key(&text), "lanes.deepseek.harness");
    }

    #[test]
    fn a_key_on_a_lane_whose_harness_it_does_not_apply_to_is_rejected() {
        assert_eq!(
            invalid_key(&with("effort = \"low\"")),
            "lanes.deepseek.effort"
        );
        assert_eq!(invalid_key(&with("mode = \"off\"")), "lanes.deepseek.mode");
        assert_eq!(
            invalid_key(&with("quota_group = \"g\"")),
            "lanes.deepseek.quota_group"
        );
        assert_eq!(
            invalid_key(&with("accept_vendor_terms_risk = true")),
            "lanes.deepseek.accept_vendor_terms_risk"
        );
        assert_eq!(
            invalid_key(&with("script = \"s.json\"")),
            "lanes.deepseek.script"
        );
    }

    #[test]
    fn a_lane_needs_a_model() {
        let text = BASE.replace("model = \"deepseek-flash\"\n", "");

        assert_eq!(invalid_key(&text), "lanes.deepseek.model");
    }

    #[test]
    fn effort_must_be_low_medium_or_high() {
        let text = BASE.replacen(
            "model = \"sonnet\"",
            "model = \"sonnet\"\neffort = \"max\"",
            1,
        );

        assert_eq!(invalid_key(&text), "lanes.plan.effort");
    }

    #[test]
    fn an_agy_lane_is_manual_unless_told_otherwise() {
        let config = parse(&with_gemini(""), false).unwrap();

        assert_eq!(config.lanes["gemini"].mode, Mode::Handover);
        assert_eq!(
            invalid_key(&with_gemini("mode = \"auto\"")),
            "lanes.gemini.mode"
        );
    }

    #[test]
    fn direct_agy_needs_the_risk_acknowledgement_and_a_quota_group() {
        let no_ack = with_gemini("mode = \"direct\"\nquota_group = \"Gemini Models\"");
        let no_group = with_gemini("mode = \"direct\"\naccept_vendor_terms_risk = true");
        let both =
            with_gemini("mode = \"direct\"\nquota_group = \"G\"\naccept_vendor_terms_risk = true");

        assert_eq!(
            invalid_key(&no_ack),
            "lanes.gemini.accept_vendor_terms_risk"
        );
        assert_eq!(invalid_key(&no_group), "lanes.gemini.quota_group");
        assert_eq!(
            parse(&both, false).unwrap().lanes["gemini"].mode,
            Mode::Direct
        );
    }

    #[test]
    fn fake_lanes_need_the_test_switch_and_a_script() {
        let fake = BASE.replace(
            "harness = \"codex\"\nmodel = \"deepseek-flash\"",
            "harness = \"fake\"\nscript = \"s.json\"",
        );
        let no_script = BASE.replace(
            "harness = \"codex\"\nmodel = \"deepseek-flash\"",
            "harness = \"fake\"",
        );

        assert_eq!(invalid_key(&fake), "lanes.deepseek.harness");
        assert_eq!(
            parse(&fake, true).unwrap().lanes["deepseek"].script,
            Some(PathBuf::from("s.json"))
        );
        assert!(
            matches!(parse(&no_script, true), Err(ConfigError::Invalid { key, .. }) if key == "lanes.deepseek.script")
        );
    }

    #[test]
    fn a_fake_lane_may_serve_every_role() {
        let text = r#"
[roles]
planner = ["f"]
reviewer = ["f"]
worker = ["f"]

[lanes.f]
harness = "fake"
script = "s.json"
"#;

        assert!(parse(text, true).is_ok());
    }

    #[test]
    fn planner_and_reviewer_name_exactly_one_existing_claude_lane() {
        let two = BASE.replace(r#"planner = ["plan"]"#, r#"planner = ["plan", "review"]"#);
        let unknown = BASE.replace(r#"reviewer = ["review"]"#, r#"reviewer = ["nobody"]"#);
        let worker_as_reviewer =
            BASE.replace(r#"reviewer = ["review"]"#, r#"reviewer = ["deepseek"]"#);

        assert_eq!(invalid_key(&two), "roles.planner");
        assert_eq!(invalid_key(&unknown), "roles.reviewer");
        assert_eq!(invalid_key(&worker_as_reviewer), "roles.reviewer");
    }

    #[test]
    fn the_worker_chain_has_distinct_worker_lanes_and_one_not_off() {
        let empty = BASE.replace(r#"worker = ["deepseek"]"#, "worker = []");
        let twice = BASE.replace(
            r#"worker = ["deepseek"]"#,
            r#"worker = ["deepseek", "deepseek"]"#,
        );
        let claude = BASE.replace(r#"worker = ["deepseek"]"#, r#"worker = ["plan"]"#);
        let all_off = with_gemini("mode = \"off\"").replace(
            r#"worker = ["deepseek", "gemini"]"#,
            r#"worker = ["gemini"]"#,
        );

        for text in [empty, twice, claude, all_off] {
            assert_eq!(invalid_key(&text), "roles.worker");
        }
    }

    #[test]
    fn the_error_message_names_the_key_and_the_fix() {
        let error = parse(&with("\n[run]\nmax_attempts = 0"), false).unwrap_err();

        assert_eq!(
            error.to_string(),
            "run.max_attempts: use a number from 1 to 20"
        );
    }

    #[test]
    fn load_reports_a_missing_file_with_its_path() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");

        assert_eq!(load(&path, false), Err(ConfigError::Missing(path.clone())));
        std::fs::write(&path, BASE).unwrap();
        assert!(load(&path, false).is_ok());
    }

    #[test]
    fn the_default_path_prefers_xdg_and_falls_back_to_home() {
        assert_eq!(
            default_path(Some("/x"), Some("/h")),
            Some(PathBuf::from("/x/onerig/config.toml"))
        );
        assert_eq!(
            default_path(Some(""), Some("/h")),
            Some(PathBuf::from("/h/.config/onerig/config.toml"))
        );
        assert_eq!(
            default_path(None, Some("/h")),
            Some(PathBuf::from("/h/.config/onerig/config.toml"))
        );
        assert_eq!(default_path(None, None), None);
    }
}