cfggraft 0.2.2

Inject declared items into config files owned by another program. Applies what it can prove it wrote, reports what it cannot, and never overwrites a disagreement.
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
//! JSON documents, addressed by key path.
//!
//! A declaration is a JSON value, and it expands into one item per leaf. An
//! object contributes an item per key, recursively; an array contributes an item
//! per element; anything else is a single item. So this declaration
//!
//! ```json
//! { "env": { "EDITOR": "hx" }, "permissions": { "deny": ["rm", "dd"] } }
//! ```
//!
//! declares four items, and each is decided on separately. Adding a key to the
//! declaration therefore has no bearing on any other key, and a disagreement
//! about one value never blocks the rest.
//!
//! # Arrays are sets
//!
//! List-valued settings are combined across configuration layers by the programs
//! that read them, so an element is a membership statement rather than a
//! position: declaring `["rm"]` says *`rm` should be denied*, not *the deny list
//! should be exactly this*. Elements are therefore addressed by value and never
//! by index — an index would name a different element as soon as a neighbour
//! were inserted, and provenance recorded under it would point at the wrong
//! value on the next run.
//!
//! The address of an element is its array's path followed by a digest of the
//! element's canonical form, as in `permissions.deny[#3f9a1c4e]`. The digest
//! makes the address unique and stable; the value itself is carried alongside it
//! in provenance and in reports, so nothing depends on reading the digest back.
//!
//! Because an element's value *is* its identity, an element can only be present
//! or absent. It cannot be found changed, which is why a changed element reads
//! as one item withdrawn and another added.
//!
//! # Canonical form
//!
//! Values are compared as JSON text with object keys sorted recursively. The
//! other program may rewrite the document with its keys in a different order,
//! and that is not a change to the value; comparing the text as written would
//! report it as one. Sorting is for comparison only — a value is always written
//! from the declaration, so the declaration's own key order is what lands in the
//! file.
//!
//! # Addressing limits
//!
//! Paths are dotted, so a key containing `.` cannot be addressed, and a key
//! containing `[#` may be misread as an array element when provenance is read
//! back. Neither appears in the configuration formats this addresses.

use crate::error::{Error, Result};
use crate::port::{Addr, Item, Observed, Output, Ownership, Repr, State, Store};
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
use std::collections::BTreeMap;

/// Split a dotted path into segments, tolerating a leading dot.
///
/// Both `env` and `.env` name the same slot; the leading-dot form is what
/// scripts that previously called `jq` will already be passing.
///
/// ```
/// use cfggraft::adapter::json::split_path;
///
/// assert_eq!(split_path(".env.EDITOR"), vec!["env", "EDITOR"]);
/// assert!(split_path("").is_empty());
/// ```
pub fn split_path(path: &str) -> Vec<String> {
    let trimmed = path.trim().trim_start_matches('.');
    if trimmed.is_empty() {
        return Vec::new();
    }
    trimmed.split('.').map(str::to_string).collect()
}

/// Render a path for display, naming the document root when empty.
pub fn display_path(segs: &[String]) -> String {
    if segs.is_empty() {
        "<root>".to_string()
    } else {
        segs.join(".")
    }
}

/// Read the value at `segs`, if every segment exists and is reachable.
pub fn get_path<'a>(root: &'a Value, segs: &[String]) -> Option<&'a Value> {
    let mut cur = root;
    for s in segs {
        cur = cur.as_object()?.get(s)?;
    }
    Some(cur)
}

/// The canonical form of a value: JSON text with object keys sorted recursively.
///
/// ```
/// use cfggraft::adapter::json::canonical;
/// use serde_json::json;
///
/// // Key order is not part of the value.
/// assert_eq!(canonical(&json!({"b": 1, "a": 2})).as_str(), r#"{"a":2,"b":1}"#);
/// ```
pub fn canonical(v: &Value) -> Repr {
    Repr::new(canonical_text(v))
}

fn canonical_text(v: &Value) -> String {
    match v {
        Value::Object(m) => {
            let mut keys: Vec<&String> = m.keys().collect();
            keys.sort_unstable();
            let body: Vec<String> = keys
                .iter()
                .map(|k| {
                    let key = Value::String((*k).to_string());
                    format!("{}:{}", key, canonical_text(&m[*k]))
                })
                .collect();
            format!("{{{}}}", body.join(","))
        }
        Value::Array(a) => {
            let body: Vec<String> = a.iter().map(canonical_text).collect();
            format!("[{}]", body.join(","))
        }
        other => serde_json::to_string(other).unwrap_or_else(|_| "null".to_string()),
    }
}

/// How to reach one item inside the document.
#[derive(Debug, Clone)]
enum Locator {
    /// A value at a key path.
    Key(Vec<String>),
    /// An element of the array at a key path, identified by its canonical form.
    Element {
        /// Path of the array holding it.
        path: Vec<String>,
        /// The element's canonical form, which is its identity.
        repr: Repr,
    },
}

/// The address of an array element: the array's path and a digest of the value.
fn element_addr(path: &[String], repr: &Repr) -> Addr {
    let mut h = Sha256::new();
    h.update(repr.as_str().as_bytes());
    let digest = format!("{:x}", h.finalize());
    Addr::new(format!("{}[#{}]", display_path(path), &digest[..8]))
}

/// Recover a locator from an address recorded on a previous run.
fn parse_addr(addr: &Addr, repr: &Repr) -> Locator {
    if let Some(rest) = addr.as_str().strip_suffix(']') {
        if let Some((path, _digest)) = rest.rsplit_once("[#") {
            return Locator::Element {
                path: split_path(path),
                repr: repr.clone(),
            };
        }
    }
    Locator::Key(split_path(addr.as_str()))
}

/// Whether a recorded item lies at or under the key path `key`.
fn within(key: &[String], addr: &Addr, repr: &Repr) -> bool {
    let path = match parse_addr(addr, repr) {
        Locator::Key(path) | Locator::Element { path, .. } => path,
    };
    path.starts_with(key)
}

/// A JSON document being managed at a key path.
#[derive(Debug)]
pub struct JsonDoc {
    root: Value,
    declared: Vec<Item>,
    /// Where each item lives, for declared items and for ones known only to
    /// provenance.
    locators: BTreeMap<Addr, Locator>,
    /// Declared values as written, preserving the declaration's key order.
    values: BTreeMap<Addr, Value>,
    /// Recorded items under the key path, which this run decides on.
    prior: Vec<Item>,
    /// Recorded items outside the key path. Another declaration manages them,
    /// so this run neither walks them nor drops them from the record.
    beyond: Vec<Item>,
}

impl JsonDoc {
    /// Parse `target` and expand `declaration` into items rooted at `key`.
    ///
    /// An empty target is an empty document, so a first run can create the file.
    /// `prior` is what a previous run recorded for this target; an empty slice
    /// leaves every item unowned and reduces the run to add-only.
    ///
    /// Only the part of `prior` under `key` is this run's. One file is often
    /// managed by several declarations at different key paths, and the record is
    /// kept per file, so an item outside `key` belongs to a declaration this run
    /// cannot see. Treating it as withdrawn would report it stale and, with
    /// retraction on, delete it. It is passed through to the returned record
    /// instead, unchanged.
    pub fn new(
        target: &str,
        target_name: &std::path::Path,
        declaration: &Value,
        key: &[String],
        prior: Vec<Item>,
    ) -> Result<Self> {
        let root: Value = if target.trim().is_empty() {
            Value::Object(Map::new())
        } else {
            serde_json::from_str(target).map_err(|e| Error::parse(target_name, e))?
        };

        let mut declared = Vec::new();
        let mut locators = BTreeMap::new();
        let mut values = BTreeMap::new();
        expand(key, declaration, &mut |addr, locator, repr, value| {
            declared.push(Item::new(addr.clone(), repr));
            locators.insert(addr.clone(), locator);
            values.insert(addr, value);
        });

        let (prior, beyond): (Vec<Item>, Vec<Item>) = prior
            .into_iter()
            .partition(|i| within(key, &i.addr, &i.repr));

        for item in &prior {
            locators
                .entry(item.addr.clone())
                .or_insert_with(|| parse_addr(&item.addr, &item.repr));
        }

        Ok(JsonDoc {
            root,
            declared,
            locators,
            values,
            prior,
            beyond,
        })
    }

    /// The document as it now stands, for callers that render it themselves.
    pub fn value(&self) -> &Value {
        &self.root
    }

    fn locator(&self, addr: &Addr) -> Result<&Locator> {
        self.locators
            .get(addr)
            .ok_or_else(|| Error::invalid(format!("no such item: {addr}")))
    }

    fn prior_repr(&self, addr: &Addr) -> Option<&Repr> {
        self.prior.iter().find(|i| &i.addr == addr).map(|i| &i.repr)
    }

    /// Walk to the container holding `segs`, reporting where the path stops.
    fn resolve<'a>(root: &'a Value, segs: &[String]) -> Resolved<'a> {
        let mut cur = root;
        for (i, s) in segs.iter().enumerate() {
            let Some(obj) = cur.as_object() else {
                return Resolved::Blocked {
                    at: display_path(&segs[..i]),
                    found: cur,
                };
            };
            match obj.get(s) {
                Some(next) => cur = next,
                None => return Resolved::Absent,
            }
        }
        Resolved::Found(cur)
    }
}

/// Where a walk down a key path ended.
enum Resolved<'a> {
    /// The path exists and holds this value.
    Found(&'a Value),
    /// A segment along the way is missing.
    Absent,
    /// A value along the way is not an object, so the path cannot continue.
    Blocked {
        /// The path of the offending value.
        at: String,
        /// What was found there instead of an object.
        found: &'a Value,
    },
}

/// A value that blocks a path is reported as a disagreement about that value,
/// not as a failure: the target is telling us it disagrees about the shape, and
/// that is drift like any other.
fn blocked_state(at: &str, found: &Value) -> State {
    State::new(Observed::Present(canonical(found)), Ownership::Unknown).with_note(format!(
        "`{at}` is {}, so the path cannot be followed",
        type_name(found)
    ))
}

fn type_name(v: &Value) -> &'static str {
    match v {
        Value::Null => "null",
        Value::Bool(_) => "a bool",
        Value::Number(_) => "a number",
        Value::String(_) => "a string",
        Value::Array(_) => "an array",
        Value::Object(_) => "an object",
    }
}

/// Expand a declared value into leaf items.
///
/// An empty object or array is a leaf: declaring `{}` asks for an empty object
/// to exist, and expanding it into nothing would silently declare nothing at
/// all.
fn expand(path: &[String], value: &Value, emit: &mut impl FnMut(Addr, Locator, Repr, Value)) {
    match value {
        Value::Object(m) if !m.is_empty() => {
            for (k, v) in m {
                let mut child = path.to_vec();
                child.push(k.clone());
                expand(&child, v, emit);
            }
        }
        Value::Array(elems) if !elems.is_empty() => {
            for e in elems {
                let repr = canonical(e);
                let addr = element_addr(path, &repr);
                let locator = Locator::Element {
                    path: path.to_vec(),
                    repr: repr.clone(),
                };
                emit(addr, locator, repr, e.clone());
            }
        }
        leaf => {
            let addr = Addr::new(display_path(path));
            emit(
                addr,
                Locator::Key(path.to_vec()),
                canonical(leaf),
                leaf.clone(),
            );
        }
    }
}

/// Borrow the container at `segs`, creating missing objects along the way.
///
/// A non-object encountered on the way down is an error rather than something to
/// replace: replacing it would discard the other program's data, which is the
/// one thing this crate never does. Reaching this is not expected, because such
/// a path reads as [`Resolved::Blocked`] and is refused before any write.
fn ensure_path<'a>(root: &'a mut Value, segs: &[String]) -> Result<&'a mut Value> {
    if root.is_null() {
        *root = Value::Object(Map::new());
    }
    let mut cur = root;
    let mut walked = Vec::new();
    for s in segs {
        walked.push(s.clone());
        if !cur.is_object() {
            return Err(Error::invalid(format!(
                "cannot descend into `{}`: {} is not an object",
                display_path(&walked),
                display_path(&walked[..walked.len() - 1])
            )));
        }
        cur = cur
            .as_object_mut()
            .expect("checked immediately above")
            .entry(s.clone())
            .or_insert_with(|| Value::Object(Map::new()));
    }
    Ok(cur)
}

impl Store for JsonDoc {
    fn declared(&self) -> Result<Vec<Item>> {
        Ok(self.declared.clone())
    }

    fn owned(&self) -> Vec<Item> {
        self.prior.clone()
    }

    fn state(&self, addr: &Addr) -> Result<State> {
        let observed = match self.locator(addr)? {
            Locator::Key(path) => match JsonDoc::resolve(&self.root, path) {
                Resolved::Found(v) => Observed::Present(canonical(v)),
                Resolved::Absent => Observed::Absent,
                Resolved::Blocked { at, found } => return Ok(blocked_state(&at, found)),
            },
            Locator::Element { path, repr } => match JsonDoc::resolve(&self.root, path) {
                Resolved::Found(Value::Array(elems)) => {
                    if elems.iter().any(|e| &canonical(e) == repr) {
                        Observed::Present(repr.clone())
                    } else {
                        Observed::Absent
                    }
                }
                Resolved::Found(other) => {
                    return Ok(blocked_state(&display_path(path), other));
                }
                Resolved::Absent => Observed::Absent,
                Resolved::Blocked { at, found } => return Ok(blocked_state(&at, found)),
            },
        };

        let ownership = match (self.prior_repr(addr), &observed) {
            (None, _) => Ownership::Unknown,
            (Some(_), Observed::Absent) => Ownership::Removed,
            (Some(p), Observed::Present(o)) if p == o => Ownership::Ours,
            (Some(p), Observed::Present(_)) => {
                return Ok(State::new(observed, Ownership::Diverged)
                    .with_note(format!("this tool last wrote {}", p.truncated(48))));
            }
        };
        Ok(State::new(observed, ownership))
    }

    fn write(&mut self, addr: &Addr) -> Result<()> {
        let locator = self.locator(addr)?.clone();
        let value = self
            .values
            .get(addr)
            .ok_or_else(|| Error::invalid(format!("{addr} is not declared")))?
            .clone();

        match locator {
            Locator::Key(path) => {
                let Some((last, parents)) = path.split_last() else {
                    // The declaration named the document root, so it replaces
                    // the whole document rather than a key within it.
                    self.root = value;
                    return Ok(());
                };
                let parent = ensure_path(&mut self.root, parents)?;
                let map = parent.as_object_mut().ok_or_else(|| {
                    Error::invalid(format!(
                        "cannot write `{addr}`: `{}` is not an object",
                        display_path(parents)
                    ))
                })?;
                map.insert(last.clone(), value);
            }
            Locator::Element { path, repr } => {
                let slot = ensure_array(&mut self.root, &path)?;
                if !slot.iter().any(|e| canonical(e) == repr) {
                    slot.push(value);
                }
            }
        }
        Ok(())
    }

    fn retract(&mut self, addr: &Addr) -> Result<()> {
        let locator = self.locator(addr)?.clone();
        match locator {
            Locator::Key(path) => {
                let Some((last, parents)) = path.split_last() else {
                    self.root = Value::Object(Map::new());
                    return Ok(());
                };
                if let Resolved::Found(_) = JsonDoc::resolve(&self.root, parents) {
                    if let Some(map) =
                        value_at_mut(&mut self.root, parents).and_then(Value::as_object_mut)
                    {
                        map.remove(last);
                    }
                }
            }
            Locator::Element { path, repr } => {
                if let Some(arr) = value_at_mut(&mut self.root, &path).and_then(Value::as_array_mut)
                {
                    arr.retain(|e| canonical(e) != repr);
                }
            }
        }
        Ok(())
    }

    fn finish(&mut self, owned: &[Item]) -> Result<Output> {
        // Two-space indent with a trailing newline, matching how the programs
        // that own these files write them. Matching the existing style keeps a
        // diff to the items actually touched.
        let mut document = serde_json::to_string_pretty(&self.root)
            .map_err(|e| Error::invalid(format!("cannot render the document: {e}")))?;
        document.push('\n');
        Ok(Output {
            document,
            prior: Some(owned.iter().chain(&self.beyond).cloned().collect()),
        })
    }
}

/// Borrow the value at `segs`, if it is reachable.
fn value_at_mut<'a>(root: &'a mut Value, segs: &[String]) -> Option<&'a mut Value> {
    let mut cur = root;
    for s in segs {
        cur = cur.as_object_mut()?.get_mut(s)?;
    }
    Some(cur)
}

/// Borrow the array at `segs`, creating it and its parents if absent.
fn ensure_array<'a>(root: &'a mut Value, segs: &[String]) -> Result<&'a mut Vec<Value>> {
    let Some((last, parents)) = segs.split_last() else {
        return Err(Error::invalid(
            "the document root cannot be an array element",
        ));
    };
    let parent = ensure_path(root, parents)?;
    let map = parent.as_object_mut().ok_or_else(|| {
        Error::invalid(format!(
            "cannot write into `{}`: it is not an object",
            display_path(parents)
        ))
    })?;
    let slot = map
        .entry(last.clone())
        .or_insert_with(|| Value::Array(Vec::new()));
    slot.as_array_mut().ok_or_else(|| {
        Error::invalid(format!(
            "cannot add to `{}`: it is not an array",
            display_path(segs)
        ))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::{run, Options};
    use crate::policy::{Decision, Reason};
    use serde_json::json;
    use std::path::Path;

    /// Build a document the way the command line does: the declaration is the
    /// value *at* the key path within the fragment, not the whole fragment.
    fn doc(target: &str, fragment: Value, key: &str, prior: &[(&str, &str)]) -> JsonDoc {
        let prior = prior
            .iter()
            .map(|(a, r)| Item::new(Addr::new(*a), Repr::new(*r)))
            .collect();
        let segs = split_path(key);
        let declaration = get_path(&fragment, &segs)
            .cloned()
            .expect("the fragment declares something at the key path");
        JsonDoc::new(target, Path::new("target.json"), &declaration, &segs, prior)
            .expect("target parses")
    }

    /// The rendered document, what is now owned, and what was decided.
    type Applied = (String, Vec<(String, String)>, Vec<(String, Decision)>);

    /// Apply a declaration and hand back the rendered document and what is now
    /// owned, so a second run can be written the way a real one happens.
    fn apply(
        target: &str,
        declaration: Value,
        key: &str,
        prior: &[(&str, &str)],
        opts: Options,
    ) -> Applied {
        let mut d = doc(target, declaration, key, prior);
        let (report, out) = run(&mut d, opts).expect("run");
        let owned = out
            .prior
            .unwrap()
            .iter()
            .map(|i| (i.addr.to_string(), i.repr.to_string()))
            .collect();
        let decisions = report
            .entries
            .iter()
            .map(|e| (e.addr.to_string(), e.decision))
            .collect();
        (out.document, owned, decisions)
    }

    fn parsed(text: &str) -> Value {
        serde_json::from_str(text).expect("rendered document parses")
    }

    #[test]
    fn adds_an_absent_key() {
        let (text, _, d) = apply(
            r#"{"env":{"A":"1"}}"#,
            json!({"env": {"B": "2"}}),
            ".env",
            &[],
            Options::default(),
        );
        assert_eq!(parsed(&text)["env"]["B"], json!("2"));
        assert_eq!(parsed(&text)["env"]["A"], json!("1"), "untouched");
        assert_eq!(d, vec![("env.B".into(), Decision::Add)]);
    }

    #[test]
    fn skips_an_equal_key_and_adopts_it() {
        let (_, owned, d) = apply(
            r#"{"env":{"A":"1"}}"#,
            json!({"env": {"A": "1"}}),
            ".env",
            &[],
            Options::default(),
        );
        assert_eq!(d, vec![("env.A".into(), Decision::Skip)]);
        assert_eq!(owned, vec![("env.A".into(), "\"1\"".into())]);
    }

    #[test]
    fn refuses_a_value_it_never_wrote() {
        let (text, _, d) = apply(
            r#"{"env":{"A":"1"}}"#,
            json!({"env": {"A": "2"}}),
            ".env",
            &[],
            Options::default(),
        );
        assert_eq!(
            parsed(&text)["env"]["A"],
            json!("1"),
            "target value survives"
        );
        assert_eq!(
            d,
            vec![("env.A".into(), Decision::Refuse(Reason::Conflict))]
        );
    }

    #[test]
    fn updates_a_value_it_did_write() {
        // Ownership is what separates this from the test above: same target,
        // same declaration, different provenance.
        let (text, _, d) = apply(
            r#"{"env":{"A":"1"}}"#,
            json!({"env": {"A": "2"}}),
            ".env",
            &[("env.A", "\"1\"")],
            Options::default(),
        );
        assert_eq!(parsed(&text)["env"]["A"], json!("2"));
        assert_eq!(d, vec![("env.A".into(), Decision::Update)]);
    }

    #[test]
    fn a_changed_declaration_settles_in_one_run() {
        let first = apply(
            "{}",
            json!({"env": {"A": "1"}}),
            ".env",
            &[],
            Options::default(),
        );
        let prior: Vec<(&str, &str)> = first
            .1
            .iter()
            .map(|(a, r)| (a.as_str(), r.as_str()))
            .collect();

        let (text, _, d) = apply(
            &first.0,
            json!({"env": {"A": "2"}}),
            ".env",
            &prior,
            Options::default(),
        );
        assert_eq!(parsed(&text)["env"]["A"], json!("2"));
        assert_eq!(d, vec![("env.A".into(), Decision::Update)]);
    }

    #[test]
    fn array_elements_are_added_without_duplicating() {
        let (text, _, _) = apply(
            r#"{"permissions":{"deny":["a","b"]}}"#,
            json!({"permissions": {"deny": ["b", "c"]}}),
            ".permissions.deny",
            &[],
            Options::default(),
        );
        assert_eq!(parsed(&text)["permissions"]["deny"], json!(["a", "b", "c"]));
    }

    #[test]
    fn array_union_is_idempotent() {
        let target = r#"{"permissions":{"deny":["a","b"]}}"#;
        let decl = json!({"permissions": {"deny": ["b", "c"]}});
        let (once, owned, _) = apply(
            target,
            decl.clone(),
            ".permissions.deny",
            &[],
            Options::default(),
        );
        let prior: Vec<(&str, &str)> = owned
            .iter()
            .map(|(a, r)| (a.as_str(), r.as_str()))
            .collect();
        let (twice, _, d) = apply(&once, decl, ".permissions.deny", &prior, Options::default());
        assert_eq!(parsed(&once), parsed(&twice));
        assert!(d.iter().all(|(_, d)| *d == Decision::Skip));
    }

    #[test]
    fn a_withdrawn_element_is_removed_and_a_foreign_one_is_not() {
        // The whole point of addressing elements by value: an element this tool
        // put there can be taken back, and one it did not put there cannot be
        // reached at all.
        let first = apply(
            "{}",
            json!({"permissions": {"deny": ["old"]}}),
            ".permissions.deny",
            &[],
            Options::default(),
        );
        let target = first
            .0
            .replace(r#""old""#, "\"old\",\n      \"added-by-the-other-program\"");
        let prior: Vec<(&str, &str)> = first
            .1
            .iter()
            .map(|(a, r)| (a.as_str(), r.as_str()))
            .collect();

        let opts = Options {
            retract: true,
            ..Options::default()
        };
        let (text, _, d) = apply(
            &target,
            json!({"permissions": {"deny": ["new"]}}),
            ".permissions.deny",
            &prior,
            opts,
        );
        assert_eq!(
            parsed(&text)["permissions"]["deny"],
            json!(["added-by-the-other-program", "new"])
        );
        assert!(d.iter().any(|(_, d)| *d == Decision::Retract));
        assert!(d.iter().any(|(_, d)| *d == Decision::Add));
    }

    #[test]
    fn without_retraction_a_withdrawn_element_stays() {
        let first = apply(
            "{}",
            json!({"permissions": {"deny": ["old"]}}),
            ".permissions.deny",
            &[],
            Options::default(),
        );
        let prior: Vec<(&str, &str)> = first
            .1
            .iter()
            .map(|(a, r)| (a.as_str(), r.as_str()))
            .collect();
        let (text, owned, _) = apply(
            &first.0,
            json!({"permissions": {"deny": ["new"]}}),
            ".permissions.deny",
            &prior,
            Options::default(),
        );
        assert_eq!(parsed(&text)["permissions"]["deny"], json!(["old", "new"]));
        assert_eq!(owned.len(), 2, "the old element stays claimed");
    }

    #[test]
    fn a_record_outside_the_key_path_is_neither_walked_nor_dropped() {
        // Two declarations manage one file at different key paths, and the
        // record is per file. Each run sees the other's items in the record.
        let env = apply(
            "{}",
            json!({"env": {"A": "1"}}),
            ".env",
            &[],
            Options::default(),
        );
        let prior: Vec<(&str, &str)> = env
            .1
            .iter()
            .map(|(a, r)| (a.as_str(), r.as_str()))
            .collect();

        let opts = Options {
            retract: true,
            ..Options::default()
        };
        let (text, owned, d) = apply(
            &env.0,
            json!({"hooks": {"Stop": ["x"]}}),
            ".hooks",
            &prior,
            opts,
        );
        assert_eq!(parsed(&text)["env"]["A"], json!("1"), "still there");
        assert_eq!(d.len(), 1, "only the hooks item is decided on: {d:?}");
        assert_eq!(d[0].1, Decision::Add);
        assert!(
            owned.contains(&("env.A".into(), "\"1\"".into())),
            "the other declaration's claim survives: {owned:?}"
        );
        assert_eq!(owned.len(), 2);
    }

    #[test]
    fn a_sibling_key_sharing_a_prefix_is_outside_the_key_path() {
        // `envx` starts with the text `env` but is not under `.env`.
        let (_, owned, d) = apply(
            r#"{"envx":{"A":"1"}}"#,
            json!({"env": {"B": "2"}}),
            ".env",
            &[("envx.A", "\"1\"")],
            Options {
                retract: true,
                ..Options::default()
            },
        );
        assert_eq!(d, vec![("env.B".into(), Decision::Add)]);
        assert!(owned.contains(&("envx.A".into(), "\"1\"".into())));
    }

    #[test]
    fn creates_a_missing_parent_path() {
        let (text, _, _) = apply(
            "{}",
            json!({"permissions": {"deny": ["x"]}}),
            ".permissions.deny",
            &[],
            Options::default(),
        );
        assert_eq!(parsed(&text)["permissions"]["deny"], json!(["x"]));
    }

    #[test]
    fn creates_a_missing_parent_for_a_scalar() {
        let (text, _, d) = apply(
            "{}",
            json!({"a": {"b": 7}}),
            ".a.b",
            &[],
            Options::default(),
        );
        assert_eq!(parsed(&text)["a"]["b"], json!(7));
        assert_eq!(d, vec![("a.b".into(), Decision::Add)]);
    }

    #[test]
    fn recurses_into_nested_objects_as_separate_items() {
        let (text, _, d) = apply(
            r#"{"hooks":{"PreToolUse":[{"matcher":"Bash"}]}}"#,
            json!({"hooks": {"Stop": [{"matcher": "x"}], "PreToolUse": [{"matcher": "Read"}]}}),
            ".hooks",
            &[],
            Options::default(),
        );
        assert_eq!(
            parsed(&text)["hooks"]["PreToolUse"]
                .as_array()
                .unwrap()
                .len(),
            2
        );
        assert!(parsed(&text)["hooks"].get("Stop").is_some());
        assert_eq!(d.len(), 2, "one item per element, decided separately");
    }

    #[test]
    fn key_order_within_an_element_is_not_a_change() {
        // The other program may rewrite the document with its keys reordered.
        let (text, _, d) = apply(
            r#"{"hooks":{"Stop":[{"b":2,"a":1}]}}"#,
            json!({"hooks": {"Stop": [{"a": 1, "b": 2}]}}),
            ".hooks",
            &[],
            Options::default(),
        );
        assert_eq!(d.len(), 1);
        assert_eq!(d[0].1, Decision::Skip);
        assert_eq!(parsed(&text)["hooks"]["Stop"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn a_type_mismatch_is_drift_not_a_replacement() {
        let (text, _, d) = apply(
            r#"{"env":{"A":{"nested":true}}}"#,
            json!({"env": {"A": "scalar"}}),
            ".env",
            &[],
            Options::default(),
        );
        assert_eq!(parsed(&text)["env"]["A"], json!({"nested": true}));
        assert_eq!(
            d,
            vec![("env.A".into(), Decision::Refuse(Reason::Conflict))]
        );
    }

    #[test]
    fn a_blocking_parent_is_reported_rather_than_replaced() {
        let mut d = doc(
            r#"{"permissions":"not-an-object"}"#,
            json!({"permissions": {"deny": ["x"]}}),
            ".permissions.deny",
            &[],
        );
        let (report, out) = run(&mut d, Options::default()).unwrap();
        assert!(report.has_drift());
        assert_eq!(
            parsed(&out.document)["permissions"],
            json!("not-an-object"),
            "the other program's value survives"
        );
        assert!(
            report.entries[0].note.as_ref().unwrap().contains("string"),
            "the report should say what blocked the path"
        );
    }

    #[test]
    fn declaring_at_the_root_is_supported() {
        let (text, _, d) = apply(
            r#"{"keep":1}"#,
            json!({"add": 2}),
            "",
            &[],
            Options::default(),
        );
        assert_eq!(parsed(&text), json!({"keep": 1, "add": 2}));
        assert_eq!(d, vec![("add".into(), Decision::Add)]);
    }

    #[test]
    fn an_empty_container_is_declared_as_a_value() {
        let (text, _, d) = apply("{}", json!({"a": {}}), ".a", &[], Options::default());
        assert_eq!(parsed(&text)["a"], json!({}));
        assert_eq!(d, vec![("a".into(), Decision::Add)]);
    }

    #[test]
    fn an_empty_target_is_an_empty_document() {
        let (text, _, _) = apply("", json!({"a": 1}), ".a", &[], Options::default());
        assert_eq!(parsed(&text), json!({"a": 1}));
    }

    #[test]
    fn the_rendered_document_keeps_the_usual_shape() {
        let (text, _, _) = apply("{}", json!({"a": {"b": 1}}), ".a", &[], Options::default());
        assert!(
            text.starts_with("{\n  \"a\": {\n    \"b\": 1"),
            "got: {text}"
        );
        assert!(text.ends_with("}\n"));
    }

    #[test]
    fn canonical_form_sorts_nested_keys_only_for_comparison() {
        assert_eq!(
            canonical(&json!({"b": {"d": 1, "c": 2}, "a": 3})).as_str(),
            r#"{"a":3,"b":{"c":2,"d":1}}"#
        );
    }

    #[test]
    fn element_addresses_are_stable_and_distinct() {
        let path = vec!["permissions".to_string(), "deny".to_string()];
        let a = element_addr(&path, &canonical(&json!("x")));
        let b = element_addr(&path, &canonical(&json!("y")));
        assert_eq!(a, element_addr(&path, &canonical(&json!("x"))));
        assert_ne!(a, b);
        assert!(a.as_str().starts_with("permissions.deny[#"));
    }

    #[test]
    fn an_element_address_round_trips_from_provenance() {
        let path = vec!["permissions".to_string(), "deny".to_string()];
        let repr = canonical(&json!("x"));
        let addr = element_addr(&path, &repr);
        match parse_addr(&addr, &repr) {
            Locator::Element { path: p, repr: r } => {
                assert_eq!(p, path);
                assert_eq!(r, repr);
            }
            other => panic!("expected an element locator, got {other:?}"),
        }
        match parse_addr(&Addr::new("env.A"), &repr) {
            Locator::Key(p) => assert_eq!(p, vec!["env".to_string(), "A".to_string()]),
            other => panic!("expected a key locator, got {other:?}"),
        }
    }

    #[test]
    fn descending_through_a_scalar_when_writing_is_an_error() {
        let mut root = json!({"permissions": "not-an-object"});
        let err = ensure_path(&mut root, &split_path(".permissions.deny")).unwrap_err();
        assert!(err.to_string().contains("permissions.deny"), "got: {err}");
    }
}