diffr-core 0.1.13

The diffr engine: parsing, structural diffing, configuration and the plugin cursor.
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
//! `diffr config`: read the schema and resolved values, and write one key to
//! the global file.
use super::{directory_of, Config, ConfigError};
use crate::plugin::config::PATH;
use serde_json::{json, Value};
use std::path::Path;

/// The resolved configuration as JSON, with the same nesting as the TOML.
/// The summarizer's API key is redacted unless `reveal` is set.
pub fn show(config: &Config, reveal: bool) -> serde_json::Value {
    serde_json::to_value(redacted(config, reveal)).expect("config serializes")
}

/// The configuration to print: the summarizer's API key replaced by
/// `<redacted>` unless `reveal` is set.
pub fn redacted(config: &Config, reveal: bool) -> Config {
    let mut shown = config.clone();
    if !reveal {
        for entry in shown.plugins.shape.entries.values_mut() {
            if let Some(key) = entry
                .options
                .get_mut("api_key")
                .filter(|key| key.as_str().is_some_and(|key| !key.is_empty()))
            {
                *key = Value::String("<redacted>".into());
            }
        }
    }
    shown
}

/// Write `key = value`, preserving comments, then drop every key that only
/// restates a default (see [`prune`]). `value` is read as the type the schema gives the key (see
/// [`typed_value`]), then the whole file is validated, before anything
/// touches the disk: unknown keys, text that is not the key's type, and
/// values the configuration rejects are errors.
pub fn set(path: &Path, key: &str, value: &str) -> Result<(), ConfigError> {
    if key.is_empty() || key.split('.').any(str::is_empty) {
        return Err(ConfigError(format!("invalid key {key:?}")));
    }
    let existing = read(path)?;
    let directory = directory_of(path);
    let typed = typed_value(key, &setting_schema(key, &existing, directory)?, value)?;
    let mut document: toml_edit::DocumentMut = existing
        .parse()
        .map_err(|error| ConfigError(format!("{}: {error}", path.display())))?;
    reset(&mut document, key, &typed, &existing, directory)?;
    assign(&mut document, key, &typed)?;
    prune(&mut document, directory)?;
    write(path, document)
}

fn read(path: &Path) -> Result<String, ConfigError> {
    match std::fs::read_to_string(path) {
        Ok(text) => Ok(text),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(String::new()),
        Err(error) => Err(ConfigError(format!("{}: {error}", path.display()))),
    }
}

/// When `key` takes a new value, clear the options of its plugin that
/// belong to it: a provider's key, endpoint and model go with the provider.
fn reset(
    document: &mut toml_edit::DocumentMut,
    key: &str,
    value: &toml::Value,
    existing: &str,
    directory: &Path,
) -> Result<(), ConfigError> {
    let segments: Vec<_> = key.split('.').collect();
    let Some(("shape", name, field)) = plugin_setting(&segments) else {
        return Ok(());
    };
    let config = Config::from_toml_in(existing, directory)?;
    let Some(entry) = config.plugins.shape.entries.get(&name) else {
        return Ok(());
    };
    if entry.options.get(field) == serde_json::to_value(value).ok().as_ref() {
        return Ok(());
    }
    for option in entry.folder().manifest.reset_by(field) {
        let mut path = segments[..segments.len() - 1].to_vec();
        path.push(option);
        super::prune::remove(document.as_table_mut(), &path);
    }
    Ok(())
}

/// Validate the document, then keep only what differs from the defaults,
/// and the file's `version`.
fn prune(document: &mut toml_edit::DocumentMut, directory: &Path) -> Result<(), ConfigError> {
    if !document.contains_key("version") {
        document.insert(
            "version",
            toml_edit::value(i64::from(super::CONFIG_VERSION)),
        );
    }
    Config::from_toml_in(&document.to_string(), directory)?;
    super::prune::prune(document, |text| {
        let config = Config::from_toml_in(text, directory).ok()?;
        toml::Value::try_from(config).ok()
    });
    Ok(())
}

fn write(path: &Path, document: toml_edit::DocumentMut) -> Result<(), ConfigError> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|error| ConfigError(format!("{}: {error}", parent.display())))?;
    }
    std::fs::write(path, document.to_string())
        .map_err(|error| ConfigError(format!("{}: {error}", path.display())))
}

/// The JSON Schema of the setting at the dotted `key`, as `diffr config
/// schema` describes it. A plugin entry's keys come from the entry's own
/// `plugin.toml`, which for an entry with `path` is in the folder the file `existing`
/// (in `directory`) points it at; `path` is diffr's, and a string.
fn setting_schema(key: &str, existing: &str, directory: &Path) -> Result<Value, ConfigError> {
    let unknown = || ConfigError(format!("{key}: unknown key"));
    let config = Config::from_toml_in(existing, directory)?;
    let segments: Vec<_> = key.split('.').collect();
    if let Some((namespace, name, field)) = plugin_setting(&segments) {
        let (manifest, custom) = match namespace {
            "shape" => {
                let entry = config
                    .plugins
                    .shape
                    .entries
                    .get(&name)
                    .ok_or_else(unknown)?;
                (&entry.folder().manifest, !name.starts_with("bundled."))
            }
            "classify" if config.plugins.classify.name == name && field != "enabled" => (
                &config.plugins.classify.folder().manifest,
                name != "bundled",
            ),
            _ => return Err(unknown()),
        };
        if field == PATH && custom {
            return Ok(json!({"type": "string"}));
        }
        return manifest.settings_schema()["properties"]
            .get(field)
            .cloned()
            .ok_or_else(unknown);
    }
    let root = Config::schema();
    let resolve = |node: &Value| -> Value {
        match node.get("$ref").and_then(Value::as_str) {
            Some(reference) => {
                let name = reference.rsplit('/').next().expect("split yields a piece");
                root["$defs"][name].clone()
            }
            None => node.clone(),
        }
    };
    let mut node = root.clone();
    for segment in key.split('.') {
        node = resolve(
            resolve(&node)
                .get("properties")
                .and_then(|properties| properties.get(segment))
                .ok_or_else(unknown)?,
        );
    }
    if node.get("properties").is_some() {
        return Err(ConfigError(format!(
            "{key} is a table; set one of its keys"
        )));
    }
    Ok(node)
}

/// `text` read as the type `schema` gives the setting `key`, and nothing
/// else: a string setting takes the text as it is, an integer, number or
/// boolean setting takes only that TOML literal, an array setting a TOML
/// array, and an enum setting one of its choices.
fn typed_value(key: &str, schema: &Value, text: &str) -> Result<toml::Value, ConfigError> {
    let mistyped =
        |expected: &str| ConfigError(format!("{key}: expected {expected}, got {text:?}"));
    if let Some(choices) = schema.get("enum").and_then(Value::as_array) {
        if !choices.iter().any(|choice| choice.as_str() == Some(text)) {
            let choices: Vec<String> = choices.iter().map(Value::to_string).collect();
            return Err(mistyped(&format!("one of {}", choices.join(", "))));
        }
        return Ok(toml::Value::String(text.to_owned()));
    }
    // An optional setting is its type or null; `set` writes the type.
    let kinds: Vec<&str> = match schema.get("type") {
        Some(Value::String(kind)) => vec![kind.as_str()],
        Some(Value::Array(kinds)) => kinds
            .iter()
            .filter_map(Value::as_str)
            .filter(|kind| *kind != "null")
            .collect(),
        _ => Vec::new(),
    };
    let [kind] = kinds.as_slice() else {
        return Err(ConfigError(format!(
            "{key}: the schema gives it no single type, so config set cannot write it"
        )));
    };
    Ok(match *kind {
        "string" => toml::Value::String(text.to_owned()),
        "integer" => toml::Value::Integer(text.parse().map_err(|_| mistyped("an integer"))?),
        "number" => toml::Value::Float(text.parse().map_err(|_| mistyped("a number"))?),
        "boolean" => match text {
            "true" => toml::Value::Boolean(true),
            "false" => toml::Value::Boolean(false),
            _ => return Err(mistyped("true or false")),
        },
        "array" => {
            let mut table =
                toml::from_str::<toml::Table>(&format!("value = {text}")).map_err(|error| {
                    ConfigError(format!(
                        "{key}: expected a TOML array such as [\"a\", \"b\"], got {text:?}: {}",
                        error.message()
                    ))
                })?;
            match table
                .remove("value")
                .expect("the table has the one key parsed")
            {
                value @ toml::Value::Array(_) => value,
                _ => return Err(mistyped("a TOML array such as [\"a\", \"b\"]")),
            }
        }
        other => {
            return Err(ConfigError(format!(
                "{key}: config set cannot write a value of type {other}"
            )));
        }
    })
}

fn plugin_setting<'a>(parts: &'a [&str]) -> Option<(&'a str, String, &'a str)> {
    match parts {
        ["plugins", "shape", "bundled", name, field] => {
            Some(("shape", format!("bundled.{name}"), field))
        }
        ["plugins", namespace @ ("shape" | "classify"), name, field] => {
            Some((namespace, (*name).to_owned(), field))
        }
        _ => None,
    }
}

fn assign(
    document: &mut toml_edit::DocumentMut,
    key: &str,
    value: &toml::Value,
) -> Result<(), ConfigError> {
    let mut segments = key.split('.').peekable();
    let mut item = document.as_item_mut();
    while let Some(segment) = segments.next() {
        if segments.peek().is_none() {
            match item.as_table_like_mut() {
                Some(table) => table.insert(segment, toml_edit::Item::Value(edit_value(value)?)),
                None => return Err(ConfigError(format!("{key}: parent is not a table"))),
            };
            return Ok(());
        }
        let table = item
            .as_table_like_mut()
            .ok_or_else(|| ConfigError(format!("{key}: parent is not a table")))?;
        if !table.contains_key(segment) {
            let mut nested = toml_edit::Table::new();
            nested.set_implicit(true);
            table.insert(segment, toml_edit::Item::Table(nested));
        }
        item = table.get_mut(segment).expect("just inserted");
    }
    Err(ConfigError(format!("invalid key {key:?}")))
}

fn edit_value(value: &toml::Value) -> Result<toml_edit::Value, ConfigError> {
    Ok(match value {
        toml::Value::String(text) => toml_edit::Value::from(text.as_str()),
        toml::Value::Integer(number) => toml_edit::Value::from(*number),
        toml::Value::Float(number) => toml_edit::Value::from(*number),
        toml::Value::Boolean(flag) => toml_edit::Value::from(*flag),
        toml::Value::Array(items) => {
            let mut array = toml_edit::Array::new();
            for item in items {
                array.push(edit_value(item)?);
            }
            toml_edit::Value::Array(array)
        }
        toml::Value::Table(_) | toml::Value::Datetime(_) => {
            return Err(ConfigError(
                "config set takes a scalar or array value".into(),
            ));
        }
    })
}

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

    #[test]
    fn set_writes_typed_values_and_keeps_the_rest() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nested").join("config.toml");
        set(&path, "diff.graph_limit", "20").unwrap();
        // `theme.name` is a string, so digits are its text.
        set(&path, "theme.name", "1234").unwrap();
        set(&path, "theme.path", "themes/mine.toml").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(text.contains("graph_limit = 20"), "{text}");
        assert!(text.contains("name = \"1234\""), "{text}");
        assert!(text.contains("path = \"themes/mine.toml\""), "{text}");
        let config = Config::from_toml(&text).unwrap();
        assert_eq!(config.diff.graph_limit, 20);
        assert_eq!(config.theme.name, "1234");
    }

    #[test]
    fn plugin_keys_are_written_under_their_quoted_names() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        set(
            &path,
            "plugins.shape.bundled.deleted-bodies.min_lines",
            "30",
        )
        .unwrap();
        set(&path, "plugins.shape.bundled.summarize.api_key", "secret").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        let config = Config::from_toml(&text).unwrap();
        assert_eq!(
            config.plugins.shape.entries["bundled.deleted-bodies"].options["min_lines"],
            30
        );
        assert!(set(&path, "plugins.shape.bundled.deleted-bodies.typo", "1").is_err());
        assert!(set(
            &path,
            "plugins.shape.bundled.deleted-bodies.min_lines",
            "-1"
        )
        .is_err());
        assert!(set(&path, "plugins.shape.order", "[\"group\"]").is_err());
        let error = |key: &str, value: &str| set(&path, key, value).unwrap_err().to_string();
        assert_eq!(
            error("plugins.shape.bundled.context.enabled", "yes"),
            "plugins.shape.bundled.context.enabled: expected true or false, got \"yes\""
        );
        assert!(error("plugins.shape.order", "context")
            .starts_with("plugins.shape.order: expected a TOML array such as [\"a\", \"b\"]"));
        assert_eq!(
            error("plugins.shape.bundled.context.lines", "many"),
            "plugins.shape.bundled.context.lines: expected an integer, got \"many\""
        );
        assert_eq!(
            error("plugins.shape.bundled.summarize.provider", "mistral"),
            "plugins.shape.bundled.summarize.provider: expected one of \"gemini\", \"openai\", \"anthropic\", got \"mistral\""
        );
        assert_eq!(
            error("plugins.shape.mine.enabled", "true"),
            "plugins.shape.mine.enabled: unknown key"
        );
        set(&path, "plugins.shape.bundled.context.enabled", "false").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert_eq!(
            Config::from_toml(&text).unwrap().plugins.shape.entries["bundled.context"].enabled,
            Some(false)
        );
        let shown = show(&config, false);
        assert_eq!(
            shown["plugins"]["shape"]["bundled"]["summarize"]["api_key"],
            "<redacted>"
        );
        assert_eq!(
            show(&config, true)["plugins"]["shape"]["bundled"]["summarize"]["api_key"],
            "secret"
        );
        assert_eq!(
            shown["plugins"]["shape"]["bundled"]["deleted-bodies"],
            serde_json::json!({"enabled": true, "min_lines": 30})
        );
        let text = toml::to_string_pretty(&redacted(&config, false)).unwrap();
        assert!(text.contains("[plugins.shape.bundled.summarize]"), "{text}");
        assert!(text.contains("system_prompt = "), "{text}");
    }

    #[test]
    fn classifier_settings_use_the_selected_manifests_types() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("plugin.toml"),
            "name = 'custom'\ntitle = 'Custom classifier'\n[options.depth]\ntype = 'integer'\ntitle = 'Depth'\ndefault = 2\n",
        ).unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[plugins.classify.custom]\npath = '.'\n").unwrap();
        set(&path, "plugins.classify.custom.depth", "5").unwrap();
        let saved = std::fs::read_to_string(&path).unwrap();
        let config = Config::from_toml_in(&saved, dir.path()).unwrap();
        assert_eq!(config.plugins.classify.options["depth"], 5);
        assert!(set(&path, "plugins.classify.custom.depth", "many").is_err());
        assert!(set(&path, "plugins.classify.custom.enabled", "false").is_err());
        assert_eq!(std::fs::read_to_string(&path).unwrap(), saved);
    }

    #[test]
    fn a_custom_name_with_spaces_keeps_its_identity_when_edited() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("plugin.toml"),
            "name = 'my context'\ntitle = 'Custom'\n[options.depth]\ntype = 'integer'\ntitle = 'Depth'\ndefault = 2\n").unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "[plugins.shape]\norder = ['my context']\n[plugins.shape.\"my context\"]\npath = '.'\n",
        )
        .unwrap();
        set(&path, "plugins.shape.my context.depth", "5").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        let config = Config::from_toml_in(&text, dir.path()).unwrap();
        assert_eq!(
            config.plugins.shape.entries["my context"].options["depth"],
            5
        );
        assert_eq!(config.plugins.shape.queries().unwrap()[0].0, "my context");
        set(&path, "plugins.shape.my context.depth", "2").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(!text.contains("depth"), "{text}");
        let config = Config::from_toml_in(&text, dir.path()).unwrap();
        assert_eq!(
            config.plugins.shape.entries["my context"].options["depth"],
            2
        );
    }

    #[test]
    fn an_old_format_is_reported_before_its_keys_and_left_as_it_is() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        let old = "version = 1\n[plugins.bundled.context]\nlines = 8\n";
        std::fs::write(&path, old).unwrap();
        let error = set(&path, "plugins.bundled.context.lines", "9").unwrap_err();
        assert!(
            error.to_string().contains("unsupported config version 1"),
            "{error}"
        );
        assert_eq!(std::fs::read_to_string(&path).unwrap(), old);
    }

    #[test]
    fn a_wasm_plugin_option_takes_the_type_its_folder_declares() {
        let dir = tempfile::tempdir().unwrap();
        let folder = dir.path().join("plugins/mine");
        std::fs::create_dir_all(&folder).unwrap();
        std::fs::write(
            folder.join("plugin.toml"),
            "name = 'mine'\ntitle = 'Mine'\n[options.depth]\ntype = 'integer'\ntitle = 'Depth'\ndefault = 2\n",
        )
        .unwrap();
        let path = dir.path().join("config.toml");
        let order = "order = ['bundled.context', 'bundled.deleted-bodies', 'bundled.test-bodies', 'bundled.removed-runs', 'bundled.summarize', 'mine']";
        std::fs::write(
            &path,
            format!("[plugins.shape]\n{order}\n[plugins.shape.mine]\npath = 'plugins/mine'\n"),
        )
        .unwrap();
        set(&path, "plugins.shape.mine.depth", "3").unwrap();
        set(&path, "plugins.shape.mine.enabled", "false").unwrap();
        set(&path, "plugins.shape.mine.path", "plugins/mine").unwrap();
        let error = |key: &str, value: &str| set(&path, key, value).unwrap_err().to_string();
        assert_eq!(
            error("plugins.shape.mine.depth", "deep"),
            "plugins.shape.mine.depth: expected an integer, got \"deep\""
        );
        assert_eq!(
            error("plugins.shape.mine.typo", "1"),
            "plugins.shape.mine.typo: unknown key"
        );
        let text = std::fs::read_to_string(&path).unwrap();
        let config = Config::from_toml_in(&text, dir.path()).unwrap();
        assert_eq!(config.plugins.shape.entries["mine"].options["depth"], 3);
        assert_eq!(config.plugins.shape.entries["mine"].enabled, Some(false));
    }

    #[test]
    fn set_rejects_unknown_keys_and_wrong_types_without_writing() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "# keep me\n[diff]\ngraph_limit = 4\n").unwrap();
        let error = |key: &str, value: &str| set(&path, key, value).unwrap_err().to_string();
        assert_eq!(error("diff.typo", "1"), "diff.typo: unknown key");
        assert_eq!(
            error("diff.graph_limit", "abc"),
            "diff.graph_limit: expected an integer, got \"abc\""
        );
        assert_eq!(
            error("diff.graph_limit", "\"20\""),
            "diff.graph_limit: expected an integer, got \"\\\"20\\\"\""
        );
        assert!(error("diff.graph_limit", "-1").starts_with("diff.graph_limit: "));
        assert_eq!(error("diff", "1"), "diff is a table; set one of its keys");
        assert_eq!(error("", "1"), "invalid key \"\"");
        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            "# keep me\n[diff]\ngraph_limit = 4\n"
        );
    }

    #[test]
    fn values_are_read_as_the_setting_type_only() {
        let value = |schema: Value, text: &str| typed_value("k", &schema, text);
        let error = |schema: Value, text: &str| value(schema, text).unwrap_err().to_string();
        assert_eq!(
            value(json!({"type": "string"}), "12").unwrap(),
            toml::Value::String("12".into())
        );
        assert_eq!(
            value(json!({"type": ["string", "null"]}), "true").unwrap(),
            toml::Value::String("true".into())
        );
        assert_eq!(
            value(json!({"type": "integer"}), "12").unwrap(),
            toml::Value::Integer(12)
        );
        assert_eq!(
            error(json!({"type": "integer"}), "1.5"),
            "k: expected an integer, got \"1.5\""
        );
        assert_eq!(
            value(json!({"type": "number"}), "1.5").unwrap(),
            toml::Value::Float(1.5)
        );
        assert_eq!(
            value(json!({"type": "boolean"}), "false").unwrap(),
            toml::Value::Boolean(false)
        );
        assert_eq!(
            error(json!({"type": "boolean"}), "yes"),
            "k: expected true or false, got \"yes\""
        );
        assert_eq!(
            value(json!({"type": "array"}), "[\"a\", \"b\"]").unwrap(),
            toml::Value::Array(vec!["a".into(), "b".into()])
        );
        assert_eq!(
            error(json!({"type": "array"}), "12"),
            "k: expected a TOML array such as [\"a\", \"b\"], got \"12\""
        );
        assert!(error(json!({"type": "array"}), "a, b")
            .starts_with("k: expected a TOML array such as [\"a\", \"b\"], got \"a, b\": "));
        assert_eq!(
            value(json!({"enum": ["gemini"]}), "gemini").unwrap(),
            toml::Value::String("gemini".into())
        );
        assert_eq!(
            error(json!({"enum": ["gemini"]}), "openai"),
            "k: expected one of \"gemini\", got \"openai\""
        );
        assert_eq!(
            error(json!({"type": ["integer", "string"]}), "1"),
            "k: the schema gives it no single type, so config set cannot write it"
        );
        assert_eq!(
            error(json!({"type": "object"}), "{}"),
            "k: config set cannot write a value of type object"
        );
    }
}

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

    fn read_toml(path: &Path) -> toml::Value {
        toml::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
    }

    #[test]
    fn set_writes_only_what_differs_from_the_defaults_and_keeps_comments() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        set(&path, "diff.graph_limit", "42").unwrap();
        let raw = read_toml(&path);
        assert_eq!(raw["version"].as_integer(), Some(2));
        assert_eq!(raw["diff"]["graph_limit"].as_integer(), Some(42));
        assert!(raw.get("plugins").is_none(), "{raw}");
        let text = std::fs::read_to_string(&path).unwrap();
        std::fs::write(&path, format!("# personal config\n{text}")).unwrap();
        set(&path, "plugins.shape.bundled.context.lines", "8").unwrap();
        set(&path, "plugins.shape.bundled.summarize.api_key", "").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(text.starts_with("# personal config\n"), "{text}");
        let raw = read_toml(&path);
        assert_eq!(
            raw["plugins"]["shape"]["bundled"]["context"]["lines"].as_integer(),
            Some(8)
        );
        // An empty key is not the same as no key, so it stays.
        assert_eq!(
            raw["plugins"]["shape"]["bundled"]["summarize"]["api_key"].as_str(),
            Some("")
        );
        // Setting a default removes the key.
        set(&path, "plugins.shape.bundled.context.lines", "3").unwrap();
        let raw = read_toml(&path);
        assert!(
            raw["plugins"]["shape"]["bundled"].get("context").is_none(),
            "{raw}"
        );
        assert_eq!(Config::from_toml(&text).unwrap().diff.graph_limit, 42);
    }

    #[test]
    fn a_materialized_file_becomes_sparse() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        // What earlier versions wrote on the first edit: every default.
        let mut old = toml::Value::try_from(Config::default()).unwrap();
        let summarize = old["plugins"]["shape"]["bundled"]["summarize"]
            .as_table_mut()
            .unwrap();
        summarize.insert("api_key".into(), "secret".into());
        old["plugins"]["shape"]["bundled"]["context"]
            .as_table_mut()
            .unwrap()
            .insert("lines".into(), 8.into());
        std::fs::write(&path, toml::to_string(&old).unwrap()).unwrap();
        set(&path, "diff.graph_limit", "42").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(!text.contains("[plugins.shape]\n"), "{text}");
        let raw = read_toml(&path);
        let mut keys = Vec::new();
        fn walk(value: &toml::Value, prefix: String, keys: &mut Vec<String>) {
            match value.as_table() {
                Some(table) => {
                    for (key, child) in table {
                        walk(child, format!("{prefix}{key}."), keys);
                    }
                }
                None => keys.push(prefix.trim_end_matches('.').to_owned()),
            }
        }
        walk(&raw, String::new(), &mut keys);
        assert_eq!(
            keys,
            [
                "diff.graph_limit",
                "plugins.shape.bundled.context.lines",
                "plugins.shape.bundled.summarize.api_key",
                "version",
            ]
        );
    }

    #[test]
    fn commented_keys_and_tables_are_kept() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "# my settings\n[plugins.shape] # note\n[plugins.shape.bundled.context]\n# lines = 10 later\nenabled = true\nlines = 8\n",
        )
        .unwrap();
        set(&path, "diff.graph_limit", "42").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        for comment in ["# my settings", "# note", "# lines = 10 later"] {
            assert!(text.contains(comment), "{comment} in {text}");
        }
    }

    #[test]
    fn a_comment_inside_a_default_list_keeps_it() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "[plugins.classify.bundled]\nhide = [\n  # keep generated code out\n  \"generated\",\n  \"vendored\",\n  \"test\", # tests too\n]\n",
        )
        .unwrap();
        set(&path, "diff.graph_limit", "42").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        for comment in ["# keep generated code out", "# tests too"] {
            assert!(text.contains(comment), "{comment} in {text}");
        }
    }

    #[test]
    fn tables_left_empty_by_pruning_go_too() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        // Earlier versions wrote every table with its own header.
        std::fs::write(
            &path,
            "version = 2\n\n[plugins.shape]\n[plugins.shape.bundled.context]\nlines = 3\n",
        )
        .unwrap();
        set(&path, "diff.graph_limit", "42").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(!text.contains("shape"), "{text}");
    }

    #[test]
    fn a_model_equal_to_its_providers_default_is_dropped() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        set(&path, "plugins.shape.bundled.summarize.provider", "openai").unwrap();
        set(&path, "plugins.shape.bundled.summarize.model", "gpt-6-luna").unwrap();
        let raw = read_toml(&path);
        assert!(
            raw["plugins"]["shape"]["bundled"]["summarize"]
                .get("model")
                .is_none(),
            "{raw}"
        );
        set(
            &path,
            "plugins.shape.bundled.summarize.model",
            "gemini-3.8-flash",
        )
        .unwrap();
        let raw = read_toml(&path);
        assert_eq!(
            raw["plugins"]["shape"]["bundled"]["summarize"]["model"].as_str(),
            Some("gemini-3.8-flash")
        );
    }

    #[test]
    fn a_new_provider_clears_the_old_ones_settings_but_keeps_the_prompt() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        let summarize = |key: &str, value: &str| {
            set(
                &path,
                &format!("plugins.shape.bundled.summarize.{key}"),
                value,
            )
            .unwrap()
        };
        summarize("provider", "anthropic");
        summarize("api_key", "anthropic-key");
        summarize("endpoint", "https://proxy.example/anthropic");
        summarize("model", "claude-sonnet-5-5");
        summarize("system_prompt", "Be terse.");
        summarize("min_lines", "7");
        // The same provider again changes nothing.
        summarize("provider", "anthropic");
        let raw = read_toml(&path);
        assert_eq!(
            raw["plugins"]["shape"]["bundled"]["summarize"]["api_key"].as_str(),
            Some("anthropic-key")
        );
        summarize("provider", "gemini");
        let raw = read_toml(&path);
        let entry = raw["plugins"]["shape"]["bundled"]["summarize"]
            .as_table()
            .unwrap();
        let mut keys: Vec<&str> = entry.keys().map(String::as_str).collect();
        keys.sort_unstable();
        // Gemini is the default provider, so it is pruned too.
        assert_eq!(keys, ["min_lines", "system_prompt"], "{raw}");
    }

    #[test]
    fn an_explicit_plugin_list_still_pins_membership() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "# just context\n[plugins.shape]\norder = ['bundled.context']\n",
        )
        .unwrap();
        set(&path, "diff.graph_limit", "42").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        let config = Config::from_toml(&text).unwrap();
        assert_eq!(config.plugins.shape.entries.len(), 1);
        assert!(text.contains("# just context\n[plugins.shape]"), "{text}");
        assert!(!text.contains("deleted-bodies"));
    }
}