brygge-ir 0.10.0

brygge's intermediate representation: the faithfulness-with-provenance model, its canonical content-addressed artifact, and the honesty machinery. The light core that links no source-decoder dependency.
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
//! Tests for `brygge_ir::transform` (RFC 018 D-6): `check`'s refusals, one per rule, and `Edit`'s own
//! round trip through `check`.

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]

use std::collections::BTreeMap;

use super::*;
use crate::Error;
use crate::artifact::{Decoded, digest_of, from_bytes, to_bytes};
use crate::builder::{AtomDraft, IrBuilder};
use crate::content::BlobId;
use crate::model::{
    Annotation, ChangedPart, Content, CopyRecord, DropRecord, Flag, FlagKind, Identity,
    ImportProvenance, Ir, LossBoundary, LossClass, MetadataClaims, PathOp, RefKind, RefRecord,
    SourceIdentity, SourceKind, Text, TransformRecord,
};
use crate::status::{Derivation, DerivationKind, EpistemicStatus};

fn provenance() -> ImportProvenance {
    ImportProvenance {
        source: SourceIdentity {
            kind: SourceKind::Git,
            repo_id: b"repo".to_vec(),
            atom_id: b"root".to_vec(),
            signatures: Vec::new(),
            extras: Vec::new(),
        },
        brygge_version: "0.1.0".into(),
        decoder: "brygge-decode-git".into(),
        decoder_version: "0.1.0".into(),
        params: BTreeMap::new(),
    }
}

fn src(atom: &str) -> SourceIdentity {
    SourceIdentity {
        kind: SourceKind::Git,
        repo_id: b"repo".to_vec(),
        atom_id: atom.as_bytes().to_vec(),
        signatures: Vec::new(),
        extras: Vec::new(),
    }
}

/// A two-atom, one-ref fixture: `root` (adds `a`), `child` (adds `b`, parent `root`), a branch ref at
/// `child`. Rich enough to exercise atoms/ops/refs/parents all at once.
fn decoded_fixture() -> Decoded {
    let mut b = IrBuilder::new(provenance());
    let blob_a = b.add_blob(b"aaa".to_vec()).unwrap();
    let root = b
        .add_atom(AtomDraft {
            parents: vec![],
            ops: vec![PathOp::Add {
                path: "a".into(),
                content: Content::blob(blob_a, 0o100_644),
                status: EpistemicStatus::Stated,
            }],
            copies: vec![],
            metadata: MetadataClaims {
                author: Some(Identity {
                    name: Text::utf8("Alice"),
                    email: Some(Text::utf8("alice@example.com")),
                }),
                message: Some(Text::utf8("root")),
                ..MetadataClaims::default()
            },
            source: src("c1"),
            status: EpistemicStatus::Stated,
        })
        .unwrap();
    let blob_b = b.add_blob(b"bbb".to_vec()).unwrap();
    let child = b
        .add_atom(AtomDraft {
            parents: vec![root],
            ops: vec![PathOp::Add {
                path: "b".into(),
                content: Content::blob(blob_b, 0o100_644),
                status: EpistemicStatus::Stated,
            }],
            copies: vec![],
            metadata: MetadataClaims {
                message: Some(Text::utf8("child")),
                ..MetadataClaims::default()
            },
            source: src("c2"),
            status: EpistemicStatus::Stated,
        })
        .unwrap();
    b.add_ref(RefRecord {
        name: "refs/heads/main".into(),
        kind: RefKind::Branch,
        target: child,
        status: EpistemicStatus::Stated,
        source: None,
        annotation: None,
    })
    .unwrap();
    let ir = b.finish().unwrap();
    from_bytes(&to_bytes(&ir)).unwrap()
}

/// Like [`decoded_fixture`], but richer: three atoms (a chain), a copy record, and an annotated tag —
/// exercises every `check` rule at once (used for `Edit`'s property-style test).
fn richer_decoded_fixture() -> Decoded {
    let mut b = IrBuilder::new(provenance());
    let blob_a = b.add_blob(b"aaa".to_vec()).unwrap();
    let root = b
        .add_atom(AtomDraft {
            parents: vec![],
            ops: vec![PathOp::Add {
                path: "a".into(),
                content: Content::blob(blob_a, 0o100_644),
                status: EpistemicStatus::Stated,
            }],
            copies: vec![],
            metadata: MetadataClaims {
                author: Some(Identity {
                    name: Text::utf8("Alice"),
                    email: Some(Text::utf8("alice@example.com")),
                }),
                message: Some(Text::utf8("root")),
                ..MetadataClaims::default()
            },
            source: src("c1"),
            status: EpistemicStatus::Stated,
        })
        .unwrap();
    let blob_b = b.add_blob(b"bbb".to_vec()).unwrap();
    let mid = b
        .add_atom(AtomDraft {
            parents: vec![root],
            ops: vec![PathOp::Add {
                path: "b".into(),
                content: Content::blob(blob_b, 0o100_644),
                status: EpistemicStatus::Stated,
            }],
            copies: vec![],
            metadata: MetadataClaims {
                message: Some(Text::utf8("mid")),
                ..MetadataClaims::default()
            },
            source: src("c2"),
            status: EpistemicStatus::Stated,
        })
        .unwrap();
    let blob_c = b.add_blob(b"ccc".to_vec()).unwrap();
    let tip = b
        .add_atom(AtomDraft {
            parents: vec![mid],
            ops: vec![PathOp::Add {
                path: "c".into(),
                content: Content::blob(blob_c, 0o100_644),
                status: EpistemicStatus::Stated,
            }],
            copies: vec![CopyRecord {
                from: "a".into(),
                from_atom: root,
                to: "a-copy".into(),
                status: EpistemicStatus::Derived(Derivation {
                    kind: DerivationKind::InferredRename,
                    by: "brygge-decode-git".into(),
                    decoder_version: "0.1.0".into(),
                    params: BTreeMap::from([
                        ("rename_algorithm".to_string(), "similarity".to_string()),
                        ("rename_threshold".to_string(), "50".to_string()),
                    ]),
                    confidence: Some(80),
                }),
            }],
            metadata: MetadataClaims {
                message: Some(Text::utf8("tip")),
                ..MetadataClaims::default()
            },
            source: src("c3"),
            status: EpistemicStatus::Stated,
        })
        .unwrap();
    b.add_ref(RefRecord {
        name: "refs/heads/main".into(),
        kind: RefKind::Branch,
        target: tip,
        status: EpistemicStatus::Stated,
        source: None,
        annotation: None,
    })
    .unwrap();
    b.add_ref(RefRecord {
        name: "v1".into(),
        kind: RefKind::Tag,
        target: mid,
        status: EpistemicStatus::Stated,
        source: None,
        annotation: Some(Annotation {
            tagger: None,
            time: None,
            message: Some(Text::utf8("release")),
        }),
    })
    .unwrap();
    b.add_flag(Flag {
        kind: FlagKind::BelowConfidenceFloor,
        what: "an example flag".into(),
        count: 1,
        reason: "fixture".into(),
    });
    let ir = b.finish().unwrap();
    from_bytes(&to_bytes(&ir)).unwrap()
}

#[test]
fn an_honest_author_change_via_edit_passes_check() {
    let input = decoded_fixture();
    let mut edit = Edit::begin(&input, "authors-fixer", "1.0.0", BTreeMap::new());
    edit.atom_mut(0).unwrap().set_author(Some(Identity {
        name: Text::utf8("A. Alias"),
        email: Some(Text::utf8("alias@example.com")),
    }));
    let output_ir = edit.finish().unwrap();
    let output = from_bytes(&to_bytes(&output_ir)).unwrap();
    let summary = check(&input, &output, "authors-fixer").unwrap();
    assert_eq!(summary.step, 0);
    assert_eq!(summary.atoms_changed, 1);
    assert_eq!(summary.ops_changed, 0);
    assert_eq!(summary.refs_changed, 0);
    assert_eq!(summary.dropped, 0);
}

/// A real bug found by RFC 019 batch G3's corpus run (request 073): `Edit::finish` cloned a surviving
/// copy's `from_atom` verbatim, in the *input's* id-space. The atom it names keeps its id only while
/// nothing about it changes; editing `root`'s own author here changes `root`'s id, and `tip`'s copy
/// (never removed, unlike the stress test above) must follow it — `check`'s own copy comparison
/// (`transform.rs`) already expected this remapping; only `Edit::finish` had not done it, so a real
/// transform over a real history (many an atom edited, far more copies surviving) hit `IrBuilder`'s own
/// "copy from an atom not yet added" invariant before `check` ever ran.
#[test]
fn a_surviving_copy_follows_its_from_atom_s_new_id() {
    let input = richer_decoded_fixture();
    let root_in = input
        .ir
        .atoms
        .iter()
        .find(|a| a.source == src("c1"))
        .unwrap()
        .id;
    let mut edit = Edit::begin(&input, "authors-fixer", "1.0.0", BTreeMap::new());
    edit.atom_mut(0).unwrap().set_author(Some(Identity {
        name: Text::utf8("A. Alias"),
        email: Some(Text::utf8("alias@example.com")),
    }));
    let output_ir = edit.finish().unwrap();
    let output = from_bytes(&to_bytes(&output_ir)).unwrap();
    let summary = check(&input, &output, "authors-fixer").unwrap();
    assert_eq!(summary.atoms_changed, 1, "only root's own claim changed");

    let root_out = output
        .ir
        .atoms
        .iter()
        .find(|a| a.source == src("c1"))
        .unwrap();
    assert_ne!(root_out.id, root_in, "root's own content changed its id");
    let tip = output
        .ir
        .atoms
        .iter()
        .find(|a| a.source == src("c3"))
        .unwrap();
    assert_eq!(
        tip.copies[0].from_atom, root_out.id,
        "the surviving copy must follow root's new id, not the stale input one"
    );
}

#[test]
fn an_honest_no_op_edit_passes_check_as_a_zero_change_step() {
    let input = decoded_fixture();
    let edit = Edit::begin(&input, "no-op", "1.0.0", BTreeMap::new());
    let output_ir = edit.finish().unwrap();
    let output = from_bytes(&to_bytes(&output_ir)).unwrap();
    let summary = check(&input, &output, "no-op").unwrap();
    assert_eq!(
        summary,
        StepSummary {
            step: 0,
            atoms_changed: 0,
            ops_changed: 0,
            refs_changed: 0,
            dropped: 0,
        }
    );
}

/// Review 159 C-2: `Edit::finish` rebuilds through its own `IrBuilder`, so `max_ir_ops` (0.8.1) holds
/// for a transform exactly as it does for a decoder — proven here with a small injected ceiling
/// (`decoded_fixture`'s two atoms together add two `PathOp`s, one over a ceiling of one).
#[test]
fn finish_raises_resource_limit_when_the_injected_max_ops_is_crossed() {
    let input = decoded_fixture();
    let edit = Edit::begin(&input, "no-op", "1.0.0", BTreeMap::new()).with_max_ops_for_test(1);
    let err = edit.finish().unwrap_err();
    assert!(
        matches!(err, Error::ResourceLimit { .. }),
        "expected Error::ResourceLimit, got {err:?}"
    );
}

#[test]
fn removing_a_middle_atom_gives_its_child_the_documented_parents() {
    let input = richer_decoded_fixture();
    let mut edit = Edit::begin(&input, "squash", "1.0.0", BTreeMap::new());
    edit.remove_atom(1, "squashed into root").unwrap();
    // `v1` targets the atom being removed; `Edit` has no implicit retargeting, so the ref must go too.
    edit.remove_ref(RefKind::Tag, "v1", "its target was squashed")
        .unwrap();
    let output_ir = edit.finish().unwrap();
    let output = from_bytes(&to_bytes(&output_ir)).unwrap();
    let summary = check(&input, &output, "squash").unwrap();
    assert_eq!(
        summary.atoms_changed, 1,
        "tip's parents changed, so it must be marked"
    );
    assert_eq!(
        summary.dropped, 2,
        "the removed atom and the ref that targeted it"
    );

    let tip = output
        .ir
        .atoms
        .iter()
        .find(|a| a.source == src("c3"))
        .unwrap();
    let root_out = output
        .ir
        .atoms
        .iter()
        .find(|a| a.source == src("c1"))
        .unwrap();
    assert_eq!(tip.parents, vec![root_out.id]);
    match &tip.status {
        EpistemicStatus::Transformed { changed, .. } => {
            assert_eq!(changed, &vec![ChangedPart::Parents]);
        }
        other => panic!("expected Transformed, got {other:?}"),
    }
}

#[test]
fn changed_parents_left_unmarked_is_refused() {
    let input = richer_decoded_fixture();
    let mut edit = Edit::begin(&input, "t", "1.0.0", BTreeMap::new());
    edit.remove_atom(1, "squashed").unwrap();
    edit.remove_ref(RefKind::Tag, "v1", "its target was squashed")
        .unwrap();
    let mut ir = edit.finish().unwrap();
    // Tip's parents genuinely changed (mid -> root); un-mark it, pretending they did not.
    let tip = ir.atoms.iter_mut().find(|a| a.source == src("c3")).unwrap();
    if let EpistemicStatus::Transformed { prior, .. } = tip.status.clone() {
        tip.status = *prior;
    }
    let tip_index = ir.atoms.iter().position(|a| a.source == src("c3")).unwrap();
    fix_id(&mut ir, tip_index);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn edit_property_a_sequence_of_edits_on_the_richest_fixture_always_passes_check() {
    let input = richer_decoded_fixture();
    let mut edit = Edit::begin(&input, "stress", "1.0.0", BTreeMap::new());
    edit.atom_mut(0)
        .unwrap()
        .set_message(Some(Text::utf8("root, renamed")));
    let new_blob = edit.add_blob(b"zzz".to_vec());
    edit.atom_mut(1)
        .unwrap()
        .set_op_blob("b", new_blob)
        .unwrap();
    edit.atom_mut(2)
        .unwrap()
        .remove_copy("a-copy", "no longer needed")
        .unwrap();
    edit.rename_ref(RefKind::Tag, "v1", "v1.0").unwrap();
    let output_ir = edit.finish().unwrap();
    let output = from_bytes(&to_bytes(&output_ir)).unwrap();
    let summary = check(&input, &output, "stress").unwrap();
    assert!(summary.atoms_changed >= 2);
    assert!(summary.ops_changed >= 1);
    assert!(summary.refs_changed >= 1);
    assert_eq!(summary.dropped, 1);
}

// ---- `check`'s refusals (RFC 018 D-6, §6 item 2) ------------------------------------------------

/// Builds `output` from `input` by replacing its decoded `Ir` with `mutate`'s result, then round-
/// trips it through the real writer/reader (so `check` always sees a genuinely well-formed `Decoded`,
/// matching what `verify`'s internal checks would have already passed by the time `transform::check`
/// runs in the real CLI).
fn with_output(_input: &Decoded, ir: Ir) -> Decoded {
    from_bytes(&to_bytes(&ir)).unwrap()
}

/// An honest step 0 over `input`, as a starting point for refusal tests to then corrupt by hand.
fn honest_step(input: &Decoded, name: &str) -> Ir {
    Edit::begin(input, name, "1.0.0", BTreeMap::new())
        .finish()
        .unwrap()
}

/// After hand-mutating one atom's own content (status/metadata) in a refusal test, its stored `id` (a
/// hash over that same content) must be recomputed — and since `AtomId` is itself part of every
/// *later* atom's own content (via `parents`/`copies`), relinking one id can make every atom after it
/// stale too, cascading. Recomputes every atom's id, in order, relinking each one's old id to its new
/// one in every later atom/ref before moving on — or `from_bytes` refuses the result at decode time
/// (a referential-integrity or self-consistency error) before `check` ever gets a chance to run.
fn fix_id(ir: &mut Ir, atom_index: usize) {
    for i in atom_index..ir.atoms.len() {
        let old_id = ir.atoms[i].id;
        let new_id = ir.atoms[i].compute_id();
        if old_id == new_id {
            continue;
        }
        ir.atoms[i].id = new_id;
        for atom in &mut ir.atoms {
            for p in &mut atom.parents {
                if *p == old_id {
                    *p = new_id;
                }
            }
            for c in &mut atom.copies {
                if c.from_atom == old_id {
                    c.from_atom = new_id;
                }
            }
        }
        for rf in &mut ir.refs {
            if rf.target == old_id {
                rf.target = new_id;
            }
        }
    }
}

#[test]
fn an_unmarked_author_change_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.atoms[0].metadata.author = Some(Identity {
        name: Text::utf8("Mallory"),
        email: None,
    });
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_mark_naming_an_unchanged_part_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.atoms[0].status = EpistemicStatus::Transformed {
        step: 0,
        changed: vec![ChangedPart::Message],
        prior: Box::new(EpistemicStatus::Stated),
    };
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    match check(&input, &output, "t") {
        Err(_) => {}
        Ok(s) => panic!("expected a refusal, got {s:?}"),
    }
}

#[test]
fn a_missing_part_in_changed_is_refused() {
    let input = decoded_fixture();
    let mut edit = Edit::begin(&input, "t", "1.0.0", BTreeMap::new());
    edit.atom_mut(0).unwrap().set_author(Some(Identity {
        name: Text::utf8("A"),
        email: None,
    }));
    edit.atom_mut(0)
        .unwrap()
        .set_message(Some(Text::utf8("also changed")));
    let mut ir = edit.finish().unwrap();
    // Strip `message` from `changed`, leaving only `author` — a missing part.
    if let EpistemicStatus::Transformed { changed, .. } = &mut ir.atoms[0].status {
        changed.retain(|c| *c != ChangedPart::Message);
    }
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_wrong_prior_is_refused() {
    let input = decoded_fixture();
    let mut edit = Edit::begin(&input, "t", "1.0.0", BTreeMap::new());
    edit.atom_mut(0).unwrap().set_author(Some(Identity {
        name: Text::utf8("A"),
        email: None,
    }));
    let mut ir = edit.finish().unwrap();
    let EpistemicStatus::Transformed { prior, .. } = &mut ir.atoms[0].status else {
        panic!("expected the author edit to mark atom 0 Transformed");
    };
    **prior = EpistemicStatus::Derived(Derivation {
        kind: DerivationKind::NormalizedMetadata,
        by: "x".into(),
        decoder_version: "0".into(),
        params: BTreeMap::new(),
        confidence: None,
    });
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn an_invented_atom_is_refused() {
    let input = decoded_fixture();
    let mut b = IrBuilder::new(input.ir.provenance.clone());
    for id in input.ir.content.ids() {
        b.add_blob(input.ir.content.get(id).unwrap().unwrap())
            .unwrap();
    }
    for atom in &input.ir.atoms {
        b.add_atom(AtomDraft {
            parents: atom.parents.clone(),
            ops: atom.ops.clone(),
            copies: atom.copies.clone(),
            metadata: atom.metadata.clone(),
            source: atom.source.clone(),
            status: atom.status.clone(),
        })
        .unwrap();
    }
    let blob = b.add_blob(b"invented".to_vec()).unwrap();
    let last = *input.ir.atoms.last().map(|a| &a.id).unwrap();
    let invented = b
        .add_atom(AtomDraft {
            parents: vec![],
            ops: vec![PathOp::Add {
                path: "invented".into(),
                content: Content::blob(blob, 0o100_644),
                status: EpistemicStatus::Stated,
            }],
            copies: vec![],
            metadata: MetadataClaims::default(),
            source: src("invented-atom"),
            status: EpistemicStatus::Stated,
        })
        .unwrap();
    let _ = (last, invented);
    for rf in &input.ir.refs {
        b.add_ref(rf.clone()).unwrap();
    }
    b.add_transform(TransformRecord {
        name: "t".into(),
        version: "1.0.0".into(),
        params: BTreeMap::new(),
        consumed: input.digest,
    });
    let ir = b.finish().unwrap();
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn an_invented_op_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    let blob = BlobId::of(b"invented");
    ir.content.insert(b"invented".to_vec()).unwrap();
    ir.atoms[0].ops.push(PathOp::Add {
        path: "invented".into(),
        content: Content::blob(blob, 0o100_644),
        status: EpistemicStatus::Transformed {
            step: 0,
            changed: vec![ChangedPart::Path],
            prior: Box::new(EpistemicStatus::Stated),
        },
    });
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_gone_op_with_no_drop_is_refused() {
    let input = decoded_fixture();
    let mut edit = Edit::begin(&input, "t", "1.0.0", BTreeMap::new());
    edit.remove_op(0, "a", "legitimately gone").unwrap();
    let mut ir = edit.finish().unwrap();
    // `remove_op` recorded the drop honestly; strip it back out to simulate an op that went missing
    // with nothing recording it (`IrBuilder::finish` already pruned the now-unreferenced blob, so
    // nothing else needs cleaning up).
    ir.loss.dropped.clear();
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn an_atom_drop_for_an_atom_still_present_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.loss.dropped.push(DropRecord {
        class: LossClass::Transform,
        what: format!("atom {}", input.ir.atoms[0].id.to_hex()),
        reason: "bogus".into(),
        step: Some(0),
    });
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn more_moved_ops_than_gone_ones_is_refused() {
    let input = decoded_fixture();
    let mut edit = Edit::begin(&input, "t", "1.0.0", BTreeMap::new());
    edit.atom_mut(0).unwrap().move_op("a", "a-moved").unwrap();
    let mut ir = edit.finish().unwrap();
    // Add a second "new" op with no corresponding gone one at all.
    let blob = BlobId::of(b"extra");
    ir.content.insert(b"extra".to_vec()).unwrap();
    ir.atoms[0].ops.push(PathOp::Add {
        path: "also-new".into(),
        content: Content::blob(blob, 0o100_644),
        status: EpistemicStatus::Transformed {
            step: 0,
            changed: vec![ChangedPart::Path],
            prior: Box::new(EpistemicStatus::Stated),
        },
    });
    fix_id(&mut ir, 0);
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_wrong_consumed_is_refused() {
    let input = decoded_fixture();
    let mut edit = Edit::begin(&input, "t", "1.0.0", BTreeMap::new());
    edit.atom_mut(0).unwrap().set_message(Some(Text::utf8("x")));
    let mut ir = edit.finish().unwrap();
    ir.transforms[0].consumed = [0u8; 32];
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_wrong_name_is_refused() {
    let input = decoded_fixture();
    let ir = honest_step(&input, "t");
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "a-different-name").is_err());
}

#[test]
fn two_new_records_at_once_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.transforms.push(TransformRecord {
        name: "t2".into(),
        version: "1.0.0".into(),
        params: BTreeMap::new(),
        consumed: digest_of(&ir).unwrap(),
    });
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_changed_provenance_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.provenance.decoder_version = "9.9.9".into();
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_changed_flag_is_refused() {
    let input = richer_decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.flags[0].count = 99;
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn an_edited_earlier_drop_is_refused() {
    let mut b = IrBuilder::new(provenance());
    let blob = b.add_blob(b"x".to_vec()).unwrap();
    b.add_atom(AtomDraft {
        parents: vec![],
        ops: vec![PathOp::Add {
            path: "a".into(),
            content: Content::blob(blob, 0o100_644),
            status: EpistemicStatus::Stated,
        }],
        copies: vec![],
        metadata: MetadataClaims::default(),
        source: src("c1"),
        status: EpistemicStatus::Stated,
    })
    .unwrap();
    b.set_loss(LossBoundary {
        dropped: vec![DropRecord {
            class: LossClass::Representation,
            what: "packfiles".into(),
            reason: "representation".into(),
            step: None,
        }],
    });
    let ir0 = b.finish().unwrap();
    let input = from_bytes(&to_bytes(&ir0)).unwrap();

    let mut ir = honest_step(&input, "t");
    ir.loss.dropped[0].reason = "edited".into();
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn a_ref_renamed_without_a_mark_is_refused() {
    let input = decoded_fixture();
    let mut ir = honest_step(&input, "t");
    ir.refs[0].name = "refs/heads/renamed".into();
    let output = with_output(&input, ir);
    assert!(check(&input, &output, "t").is_err());
}

#[test]
fn duplicate_source_identities_in_the_input_are_refused() {
    let mut b = IrBuilder::new(provenance());
    let blob1 = b.add_blob(b"a".to_vec()).unwrap();
    b.add_atom(AtomDraft {
        parents: vec![],
        ops: vec![PathOp::Add {
            path: "a".into(),
            content: Content::blob(blob1, 0o100_644),
            status: EpistemicStatus::Stated,
        }],
        copies: vec![],
        metadata: MetadataClaims::default(),
        source: src("same"),
        status: EpistemicStatus::Stated,
    })
    .unwrap();
    let blob2 = b.add_blob(b"b".to_vec()).unwrap();
    b.add_atom(AtomDraft {
        parents: vec![],
        ops: vec![PathOp::Add {
            path: "b".into(),
            content: Content::blob(blob2, 0o100_644),
            status: EpistemicStatus::Stated,
        }],
        copies: vec![],
        metadata: MetadataClaims::default(),
        source: src("same"), // duplicate on purpose
        status: EpistemicStatus::Stated,
    })
    .unwrap();
    let ir0 = b.finish().unwrap();
    let input = from_bytes(&to_bytes(&ir0)).unwrap();

    // `Edit` itself would refuse to touch this input at all (its own `finish` runs `check`, which
    // refuses first) — build a trivial "no real change" output directly, bypassing `Edit`, so this
    // test actually reaches `check`'s own duplicate-identity refusal rather than `Edit`'s.
    let mut b2 = IrBuilder::new(input.ir.provenance.clone());
    for id in input.ir.content.ids() {
        b2.add_blob(input.ir.content.get(id).unwrap().unwrap())
            .unwrap();
    }
    for atom in &input.ir.atoms {
        b2.add_atom(AtomDraft {
            parents: atom.parents.clone(),
            ops: atom.ops.clone(),
            copies: atom.copies.clone(),
            metadata: atom.metadata.clone(),
            source: atom.source.clone(),
            status: atom.status.clone(),
        })
        .unwrap();
    }
    b2.add_transform(TransformRecord {
        name: "t".into(),
        version: "1.0.0".into(),
        params: BTreeMap::new(),
        consumed: input.digest,
    });
    let output_ir = b2.finish().unwrap();
    let output = with_output(&input, output_ir);
    match check(&input, &output, "t") {
        Err(v) => assert!(v.to_string().contains("same source identity"), "{v}"),
        Ok(s) => panic!("expected a refusal, got {s:?}"),
    }
}