panproto-lens 0.52.0

Bidirectional lens combinators for panproto
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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
//! Edit lens: translates individual edits between source and view schemas.
//!
//! An [`EditLens`] operates on edits (patches) rather than whole states.
//! Each edit flows through the lens, and the complement updates
//! incrementally. The complement is a state machine, not a snapshot.
//!
//! Translation is derived from two sources:
//!
//! 1. **Structural translation**: remap anchors and edges via the
//!    compiled migration's vertex/edge remap tables. Field transforms
//!    from the compiled migration are applied to surviving nodes.
//! 2. **Semantic translation**: apply directed equations from the
//!    protocol theory for value-level coercions. The `impl_term`
//!    expression is evaluated forward; the `inverse` expression is
//!    evaluated backward.
//!
//! Complement policies (from the schema's policy sorts) determine how
//! the complement reacts to view edits.

use std::collections::HashMap;
use std::sync::Arc;

use panproto_gat::{DirectedEquation, Name, Theory};
use panproto_inst::{CompiledMigration, TreeEdit, WInstance};
use panproto_schema::{Edge, Protocol, Schema};

use crate::Lens;
use crate::asymmetric::Complement;
use crate::edit_error::EditLensError;
use crate::edit_pipeline::EditPipeline;
use crate::edit_provenance::EditProvenance;
use crate::optic::OpticKind;

/// An edit lens between source and view schemas.
///
/// Translates individual `TreeEdit` values through a migration,
/// maintaining a stateful complement that tracks discarded data.
pub struct EditLens {
    /// The compiled migration driving structural translation.
    pub compiled: CompiledMigration,
    /// The source schema.
    pub src_schema: Schema,
    /// The target (view) schema.
    pub tgt_schema: Schema,
    /// The current complement state.
    pub complement: Complement,
    /// The protocol definition (carries the enriched theory).
    pub protocol: Protocol,
    /// Cached reverse vertex remap (target -> source).
    pub(crate) reverse_vertex_remap: HashMap<Name, Name>,
    /// Cached reverse edge remap (target -> source).
    pub(crate) reverse_edge_remap: HashMap<Edge, Edge>,
    /// Per-pipeline-step incremental translator.
    pub pipeline: Option<EditPipeline>,
}

impl EditLens {
    /// Construct an `EditLens` from an existing state-based `Lens` and protocol.
    #[must_use]
    pub fn from_lens(lens: Lens, protocol: Protocol) -> Self {
        let reverse_vertex_remap = lens
            .compiled
            .vertex_remap
            .iter()
            .map(|(k, v)| (v.clone(), k.clone()))
            .collect();
        let reverse_edge_remap = lens
            .compiled
            .edge_remap
            .iter()
            .map(|(k, v)| (v.clone(), k.clone()))
            .collect();
        Self {
            compiled: lens.compiled,
            src_schema: lens.src_schema,
            tgt_schema: lens.tgt_schema,
            complement: Complement::empty(),
            protocol,
            reverse_vertex_remap,
            reverse_edge_remap,
            pipeline: None,
        }
    }

    /// Initialize the complement from a whole-state `get` on the given source.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `get` fails.
    pub fn initialize(&mut self, source: &WInstance) -> Result<(), EditLensError> {
        let lens = Lens {
            compiled: self.compiled.clone(),
            src_schema: self.src_schema.clone(),
            tgt_schema: self.tgt_schema.clone(),
        };
        let (_, complement) = crate::get(&lens, source)
            .map_err(|e| EditLensError::TranslationFailed(e.to_string()))?;
        self.complement = complement;
        self.pipeline = Some(EditPipeline::from_lens_and_instance(self, source));
        Ok(())
    }

    /// Classify this edit lens's optic kind from the migration structure.
    ///
    /// The classification follows from the data-preservation properties
    /// of the compiled migration:
    ///
    /// - **Iso**: all source vertices and edges survive (bijection).
    /// - **Lens**: some vertices or edges are dropped but none added (projection).
    /// - **Prism**: variant-related changes are present (injection).
    /// - **Affine**: everything else (lens composed with prism).
    #[must_use]
    pub fn optic_kind(&self) -> OpticKind {
        let all_src_verts_survive = self
            .src_schema
            .vertices
            .keys()
            .all(|v| self.compiled.surviving_verts.contains(v));
        let all_src_edges_survive = self
            .src_schema
            .edges
            .keys()
            .all(|e| self.compiled.surviving_edges.contains(e));

        if all_src_verts_survive && all_src_edges_survive {
            return OpticKind::Iso;
        }

        // Check for variant-related changes (prism indicator).
        let has_variant_changes = self
            .src_schema
            .variants
            .keys()
            .any(|v| !self.tgt_schema.variants.contains_key(v))
            || self
                .tgt_schema
                .variants
                .keys()
                .any(|v| !self.src_schema.variants.contains_key(v));

        if has_variant_changes {
            return OpticKind::Prism;
        }

        // No added vertices: pure projection (lens).
        let has_added_verts = self
            .tgt_schema
            .vertices
            .keys()
            .any(|v| !self.src_schema.vertices.contains_key(v));

        if !has_added_verts {
            return OpticKind::Lens;
        }

        OpticKind::Affine
    }

    /// Translate an edit through an isomorphic lens (bijection).
    ///
    /// Since the lens is an isomorphism, the complement is unit and
    /// never changes. This method only remaps anchors and edges.
    #[must_use]
    pub fn translate_iso(&self, edit: TreeEdit) -> TreeEdit {
        match edit {
            TreeEdit::Identity => TreeEdit::Identity,
            TreeEdit::SetField {
                node_id,
                ref field,
                ref value,
            } => {
                let translated = self.translate_field_edit(field.as_ref(), value);
                TreeEdit::SetField {
                    node_id,
                    field: Name::from(translated.0.as_str()),
                    value: translated.1,
                }
            }
            TreeEdit::RemoveField { node_id, ref field } => {
                let new_name = self.translate_field_name(field.as_ref());
                TreeEdit::RemoveField {
                    node_id,
                    field: Name::from(new_name.as_str()),
                }
            }
            TreeEdit::RelabelNode { id, ref new_anchor } => TreeEdit::RelabelNode {
                id,
                new_anchor: self.remap_anchor_forward(new_anchor),
            },
            TreeEdit::InsertNode {
                parent,
                child_id,
                ref node,
                ref edge,
            } => {
                let remapped_node = self.remap_and_transform_node(node);
                let remapped_edge = self.remap_edge_forward(edge);
                TreeEdit::InsertNode {
                    parent,
                    child_id,
                    node: remapped_node,
                    edge: remapped_edge,
                }
            }
            TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                ref edge,
            } => TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                edge: self.remap_edge_forward(edge),
            },
            other => other,
        }
    }

    /// Translate an edit through a prismatic lens.
    ///
    /// Edits targeting an inactive variant are absorbed into the
    /// complement. Edits targeting the active variant pass through
    /// with structural remapping.
    ///
    /// # Errors
    ///
    /// Returns [`EditLensError::TranslationFailed`] if the edit targets
    /// a variant that cannot be resolved.
    pub fn translate_prism(&mut self, edit: TreeEdit) -> Result<TreeEdit, EditLensError> {
        match &edit {
            TreeEdit::InsertNode { node, .. } => {
                let target_anchor = self
                    .compiled
                    .vertex_remap
                    .get(&node.anchor)
                    .unwrap_or(&node.anchor);
                if !self.compiled.surviving_verts.contains(target_anchor) {
                    // Inactive variant: absorb into complement.
                    return Ok(TreeEdit::Identity);
                }
            }
            TreeEdit::SetField { node_id, .. }
                if self.complement.dropped_nodes.contains_key(node_id) =>
            {
                return Ok(TreeEdit::Identity);
            }
            _ => {}
        }
        self.get_edit(edit)
    }

    /// Check a value against refinement constraints on a target vertex.
    ///
    /// Returns `Ok(())` if the value satisfies all constraints, or
    /// `Err(EditLensError::RefinementViolation)` if any constraint fails.
    fn check_refinement(
        &self,
        vertex: &Name,
        value: &panproto_inst::Value,
    ) -> Result<(), EditLensError> {
        let Some(constraints) = self.tgt_schema.constraints.get(vertex) else {
            return Ok(());
        };

        let string_val = match value {
            panproto_inst::Value::Str(s) => Some(s.as_str()),
            _ => None,
        };

        for constraint in constraints {
            let sort = constraint.sort.as_ref();
            match sort {
                "maxLength" => {
                    if let Some(s) = string_val {
                        if let Ok(max) = constraint.value.parse::<usize>() {
                            if s.len() > max {
                                return Err(EditLensError::RefinementViolation {
                                    vertex: vertex.to_string(),
                                    constraint_sort: sort.to_owned(),
                                    constraint_value: constraint.value.clone(),
                                    detail: format!(
                                        "string length {} exceeds maximum {}",
                                        s.len(),
                                        max
                                    ),
                                });
                            }
                        }
                    }
                }
                "minLength" => {
                    if let Some(s) = string_val {
                        if let Ok(min) = constraint.value.parse::<usize>() {
                            if s.len() < min {
                                return Err(EditLensError::RefinementViolation {
                                    vertex: vertex.to_string(),
                                    constraint_sort: sort.to_owned(),
                                    constraint_value: constraint.value.clone(),
                                    detail: format!(
                                        "string length {} below minimum {}",
                                        s.len(),
                                        min
                                    ),
                                });
                            }
                        }
                    }
                }
                "format" => {
                    if let Some(s) = string_val {
                        let pattern = &constraint.value;
                        let valid = match pattern.as_str() {
                            "at-uri" => s.starts_with("at://"),
                            "did" => s.starts_with("did:"),
                            "datetime" | "date-time" => {
                                // Simple heuristic: must contain 'T' or be YYYY-MM-DD.
                                s.len() >= 10
                                    && s.as_bytes().get(4) == Some(&b'-')
                                    && s.as_bytes().get(7) == Some(&b'-')
                            }
                            "uri" | "url" => {
                                s.starts_with("http://")
                                    || s.starts_with("https://")
                                    || s.starts_with("at://")
                            }
                            _ => true, // Unknown formats pass by default.
                        };
                        if !valid {
                            return Err(EditLensError::RefinementViolation {
                                vertex: vertex.to_string(),
                                constraint_sort: sort.to_owned(),
                                constraint_value: constraint.value.clone(),
                                detail: format!("value {s:?} does not match format {pattern:?}"),
                            });
                        }
                    }
                }
                _ => {} // Unknown constraint sorts are ignored.
            }
        }

        Ok(())
    }

    /// Translate a source edit to a view edit, recording provenance.
    ///
    /// Calls [`get_edit`](Self::get_edit) and also records which
    /// translation rules fired, which complement policy was consulted,
    /// and whether the translation was total.
    ///
    /// # Errors
    ///
    /// Returns [`EditLensError`] if the edit cannot be translated.
    pub fn get_edit_with_provenance(
        &mut self,
        edit: TreeEdit,
    ) -> Result<(TreeEdit, EditProvenance), EditLensError> {
        let desc = format!("{edit:?}");
        let mut provenance = EditProvenance::new(desc);

        // Record structural remap rule (always fires).
        provenance.record_rule(Arc::from("structural_remap"));

        // Record field transform rules.
        if let TreeEdit::SetField { ref field, .. } = edit {
            let field_str = field.to_string();
            for transforms in self.compiled.field_transforms.values() {
                for transform in transforms {
                    let applies = match transform {
                        panproto_inst::FieldTransform::RenameField { old_key, .. } => {
                            old_key == &field_str
                        }
                        panproto_inst::FieldTransform::ApplyExpr { key, .. } => key == &field_str,
                        _ => false,
                    };
                    if applies {
                        provenance.record_rule(Arc::from(format!("field_{field_str}").as_str()));
                    }
                }
            }
        }

        // Record complement policy if one exists for the edit's anchor.
        if let TreeEdit::InsertNode { ref node, .. } = edit {
            if let Some(_policy) = self.policy_for(&node.anchor) {
                provenance.record_policy(Arc::from(node.anchor.as_ref()));
            }
        }

        // Perform the translation.
        let translated = self.get_edit(edit)?;

        // Check if the optic is Iso; if so, complement was not updated,
        // so the translation is always total.
        if self.optic_kind() != OpticKind::Iso {
            // For non-iso lenses, check if the result is identity (absorbed).
            if translated.is_identity() {
                provenance.mark_partial();
            }
        }

        Ok((translated, provenance))
    }

    /// Translate a source edit to a view edit, updating the complement.
    ///
    /// The translation pipeline for each edit:
    /// 1. **Anchor survival**: check if the edit's target anchor is in
    ///    `surviving_verts`. Non-surviving edits are absorbed into the
    ///    complement.
    /// 2. **Conditional survival**: evaluate value-dependent predicates
    ///    from `conditional_survival` on the node's fields.
    /// 3. **Structural remap**: remap anchors and edges via the compiled
    ///    migration's vertex/edge remap tables.
    /// 4. **Field transforms**: apply `FieldTransform` operations from
    ///    the compiled migration to surviving nodes' fields.
    /// 5. **Complement update**: update the complement state based on
    ///    which data was absorbed or released.
    ///
    /// # Errors
    ///
    /// Returns [`EditLensError`] if the edit cannot be translated.
    pub fn get_edit(&mut self, edit: TreeEdit) -> Result<TreeEdit, EditLensError> {
        match edit {
            TreeEdit::Identity => Ok(TreeEdit::Identity),
            TreeEdit::InsertNode {
                parent,
                child_id,
                node,
                edge,
            } => Ok(self.get_edit_insert(parent, child_id, node, edge)),
            TreeEdit::DeleteNode { id } => Ok(self.get_edit_delete(id)),
            TreeEdit::SetField {
                node_id,
                ref field,
                ref value,
            } => self.get_edit_set_field(node_id, field, value),
            TreeEdit::RemoveField { node_id, ref field } => {
                Ok(self.get_edit_remove_field(node_id, field))
            }
            TreeEdit::RelabelNode { id, new_anchor } => self.get_edit_relabel(id, new_anchor),
            TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                edge,
            } => Ok(TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                edge: self.remap_edge_forward(&edge),
            }),
            TreeEdit::InsertFan { fan } => Ok(self.get_edit_insert_fan(fan)),
            TreeEdit::DeleteFan { hyper_edge_id } => Ok(self.get_edit_delete_fan(hyper_edge_id)),
            TreeEdit::ContractNode { id } => {
                if self.complement.dropped_nodes.remove(&id).is_some() {
                    Ok(TreeEdit::Identity)
                } else {
                    Ok(TreeEdit::ContractNode { id })
                }
            }
            TreeEdit::JoinFeatures {
                primary,
                joined,
                produce,
            } => Ok(TreeEdit::JoinFeatures {
                primary,
                joined,
                produce: self.remap_and_transform_node(&produce),
            }),
            TreeEdit::Sequence(steps) => self.get_edit_sequence(steps),
        }
    }

    fn get_edit_insert(
        &mut self,
        parent: u32,
        child_id: u32,
        node: panproto_inst::Node,
        edge: Edge,
    ) -> TreeEdit {
        // Step 1: anchor survival.
        let target_anchor = self
            .compiled
            .vertex_remap
            .get(&node.anchor)
            .unwrap_or(&node.anchor);
        if !self.compiled.surviving_verts.contains(target_anchor) {
            self.complement.dropped_nodes.insert(child_id, node);
            self.complement.dropped_arcs.push((parent, child_id, edge));
            return TreeEdit::Identity;
        }

        // Step 2: conditional survival.
        if let Some(pred) = self.compiled.conditional_survival.get(&node.anchor) {
            let env = panproto_inst::build_env_from_extra_fields(&node.extra_fields);
            let config = panproto_expr::EvalConfig::default();
            if matches!(
                panproto_expr::eval(pred, &env, &config),
                Ok(panproto_expr::Literal::Bool(false))
            ) {
                self.complement.dropped_nodes.insert(child_id, node);
                self.complement.dropped_arcs.push((parent, child_id, edge));
                return TreeEdit::Identity;
            }
        }

        // Steps 3-4: structural remap and field transforms.
        let remapped_node = self.remap_and_transform_node(&node);
        let remapped_edge = self.remap_edge_forward(&edge);
        TreeEdit::InsertNode {
            parent,
            child_id,
            node: remapped_node,
            edge: remapped_edge,
        }
    }

    fn get_edit_delete(&mut self, id: u32) -> TreeEdit {
        if self.complement.dropped_nodes.contains_key(&id) {
            self.complement.dropped_nodes.remove(&id);
            self.complement
                .dropped_arcs
                .retain(|&(_, child, _)| child != id);
            TreeEdit::Identity
        } else {
            TreeEdit::DeleteNode { id }
        }
    }

    fn get_edit_set_field(
        &mut self,
        node_id: u32,
        field: &Name,
        value: &panproto_inst::Value,
    ) -> Result<TreeEdit, EditLensError> {
        // If the node is in the complement, update the complement's copy.
        if let Some(node) = self.complement.dropped_nodes.get_mut(&node_id) {
            node.extra_fields.insert(field.to_string(), value.clone());
            return Ok(TreeEdit::Identity);
        }

        // The node is in the view. Apply field transforms if the migration
        // specifies any for this node's source anchor: translate the field
        // name and possibly coerce the value.
        let field_str = field.to_string();
        let translated = self.translate_field_edit(&field_str, value);

        // Refinement type checking: validate the translated value against
        // the target schema's constraints for the target vertex.
        let translated_field_name = Name::from(translated.0.as_str());
        // Find the target vertex that this field belongs to by looking
        // for edges whose name matches the translated field name.
        for edge in self.tgt_schema.edges.keys() {
            if edge.name.as_ref() == Some(&translated_field_name) {
                self.check_refinement(&edge.tgt, &translated.1)?;
            }
        }

        Ok(TreeEdit::SetField {
            node_id,
            field: translated_field_name,
            value: translated.1,
        })
    }

    fn get_edit_remove_field(&mut self, node_id: u32, field: &Name) -> TreeEdit {
        if let Some(node) = self.complement.dropped_nodes.get_mut(&node_id) {
            node.extra_fields.remove(field.as_ref());
            return TreeEdit::Identity;
        }

        // If this field was renamed by field transforms, the remove applies
        // to the renamed field in the target schema.
        let new_name = self.translate_field_name(field.as_ref());
        TreeEdit::RemoveField {
            node_id,
            field: Name::from(new_name.as_str()),
        }
    }

    fn get_edit_relabel(&mut self, id: u32, new_anchor: Name) -> Result<TreeEdit, EditLensError> {
        let old_in_complement = self.complement.dropped_nodes.contains_key(&id);
        let target_anchor = self
            .compiled
            .vertex_remap
            .get(&new_anchor)
            .unwrap_or(&new_anchor);
        let new_survives = self.compiled.surviving_verts.contains(target_anchor);

        match (old_in_complement, new_survives) {
            (true, true) => {
                // Was in complement, now survives: becomes an insert in the view.
                let node = self.complement.dropped_nodes.remove(&id).ok_or_else(|| {
                    EditLensError::ComplementInconsistent(format!(
                        "node {id} expected in complement"
                    ))
                })?;
                self.complement
                    .dropped_arcs
                    .retain(|&(_, child, _)| child != id);
                let mut remapped = self.remap_and_transform_node(&node);
                remapped.anchor = self.remap_anchor_forward(&new_anchor);
                let parent = self.complement.original_parent.get(&id).copied();
                if let Some(p) = parent {
                    // Resolve the parent's anchor from the complement or schema.
                    let parent_anchor = self
                        .complement
                        .dropped_nodes
                        .get(&p)
                        .map(|n| n.anchor.clone())
                        .or_else(|| self.tgt_schema.vertices.keys().next().cloned())
                        .unwrap_or_else(|| Name::from(self.src_schema.protocol.as_str()));
                    // Find the correct edge from the target schema.
                    let edge = self
                        .tgt_schema
                        .edges_between(&parent_anchor, &remapped.anchor)
                        .first()
                        .cloned()
                        .unwrap_or_else(|| Edge {
                            src: parent_anchor,
                            tgt: remapped.anchor.clone(),
                            kind: "prop".into(),
                            name: None,
                        });
                    Ok(TreeEdit::InsertNode {
                        parent: p,
                        child_id: id,
                        node: remapped,
                        edge,
                    })
                } else {
                    Ok(TreeEdit::RelabelNode {
                        id,
                        new_anchor: self.remap_anchor_forward(&new_anchor),
                    })
                }
            }
            (true, false) => {
                // Was in complement, still doesn't survive. Update complement.
                if let Some(node) = self.complement.dropped_nodes.get_mut(&id) {
                    node.anchor = new_anchor;
                }
                Ok(TreeEdit::Identity)
            }
            (false, true) => Ok(TreeEdit::RelabelNode {
                id,
                new_anchor: self.remap_anchor_forward(&new_anchor),
            }),
            (false, false) => {
                // Was in view, now doesn't survive. Delete from view and
                // add to complement.
                Ok(TreeEdit::DeleteNode { id })
            }
        }
    }

    fn get_edit_insert_fan(&mut self, fan: panproto_inst::Fan) -> TreeEdit {
        // Check if the parent survives.
        let parent_survives = !self.complement.dropped_nodes.contains_key(&fan.parent);
        if !parent_survives {
            self.complement.dropped_fans.push(fan);
            return TreeEdit::Identity;
        }

        // Check which children survive. Collect surviving and dropped.
        let mut all_survive = true;
        for &child_id in fan.children.values() {
            if self.complement.dropped_nodes.contains_key(&child_id) {
                all_survive = false;
                break;
            }
        }

        if all_survive {
            // All participants survive: remap hyper-edge if needed and pass through.
            let remapped_fan = self.remap_fan_forward(&fan);
            TreeEdit::InsertFan { fan: remapped_fan }
        } else {
            // Some participants don't survive: store the fan in complement.
            self.complement.dropped_fans.push(fan);
            TreeEdit::Identity
        }
    }

    fn get_edit_delete_fan(&mut self, hyper_edge_id: Name) -> TreeEdit {
        // Check if the fan is in the complement.
        let id_str = hyper_edge_id.as_ref();
        let in_complement = self
            .complement
            .dropped_fans
            .iter()
            .any(|f| f.hyper_edge_id == id_str);
        if in_complement {
            self.complement
                .dropped_fans
                .retain(|f| f.hyper_edge_id != id_str);
            TreeEdit::Identity
        } else {
            TreeEdit::DeleteFan { hyper_edge_id }
        }
    }

    fn get_edit_sequence(&mut self, steps: Vec<TreeEdit>) -> Result<TreeEdit, EditLensError> {
        let mut translated = Vec::with_capacity(steps.len());
        for step in steps {
            let t = self.get_edit(step)?;
            if !t.is_identity() {
                translated.push(t);
            }
        }
        Ok(match translated.len() {
            0 => TreeEdit::Identity,
            1 => translated.into_iter().next().unwrap_or(TreeEdit::Identity),
            _ => TreeEdit::Sequence(translated),
        })
    }

    /// Translate a view edit back to a source edit, updating the complement.
    ///
    /// Uses the inverse vertex/edge remap. For value-level edits, applies
    /// the inverse directed equations (from `DirectedEquation.inverse`)
    /// when available.
    ///
    /// # Errors
    ///
    /// Returns [`EditLensError`] if the edit cannot be translated.
    pub fn put_edit(&mut self, edit: TreeEdit) -> Result<TreeEdit, EditLensError> {
        match edit {
            TreeEdit::Identity => Ok(TreeEdit::Identity),
            TreeEdit::InsertNode {
                parent,
                child_id,
                ref node,
                ref edge,
            } => Ok(self.put_edit_insert(parent, child_id, node, edge)),
            TreeEdit::DeleteNode { id } => Ok(TreeEdit::DeleteNode { id }),
            TreeEdit::SetField {
                node_id,
                ref field,
                ref value,
            } => Ok(self.put_edit_set_field(node_id, field, value)),
            TreeEdit::RemoveField { node_id, field } => {
                let source_name = self.translate_field_name_backward(field.as_ref());
                Ok(TreeEdit::RemoveField {
                    node_id,
                    field: Name::from(source_name.as_str()),
                })
            }
            TreeEdit::RelabelNode { id, new_anchor } => {
                let source_anchor = self.remap_anchor_backward(&new_anchor);
                Ok(TreeEdit::RelabelNode {
                    id,
                    new_anchor: source_anchor,
                })
            }
            TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                edge,
            } => Ok(TreeEdit::MoveSubtree {
                node_id,
                new_parent,
                edge: self.remap_edge_backward(&edge),
            }),
            TreeEdit::InsertFan { fan } => {
                let source_fan = self.remap_fan_backward(&fan);
                Ok(TreeEdit::InsertFan { fan: source_fan })
            }
            TreeEdit::DeleteFan { hyper_edge_id } => Ok(TreeEdit::DeleteFan { hyper_edge_id }),
            TreeEdit::ContractNode { id } => Ok(TreeEdit::ContractNode { id }),
            TreeEdit::JoinFeatures {
                primary,
                joined,
                produce,
            } => Ok(TreeEdit::JoinFeatures {
                primary,
                joined,
                produce: self.remap_node_backward(&produce),
            }),
            TreeEdit::Sequence(steps) => {
                let mut translated = Vec::with_capacity(steps.len());
                for step in steps {
                    let t = self.put_edit(step)?;
                    if !t.is_identity() {
                        translated.push(t);
                    }
                }
                Ok(match translated.len() {
                    0 => TreeEdit::Identity,
                    1 => translated.into_iter().next().unwrap_or(TreeEdit::Identity),
                    _ => TreeEdit::Sequence(translated),
                })
            }
        }
    }

    fn put_edit_insert(
        &self,
        parent: u32,
        child_id: u32,
        node: &panproto_inst::Node,
        edge: &Edge,
    ) -> TreeEdit {
        let source_node = self.remap_node_backward(node);
        let source_edge = self.remap_edge_backward(edge);
        TreeEdit::InsertNode {
            parent,
            child_id,
            node: source_node,
            edge: source_edge,
        }
    }

    fn put_edit_set_field(
        &self,
        node_id: u32,
        field: &Name,
        value: &panproto_inst::Value,
    ) -> TreeEdit {
        let source_name = self.translate_field_name_backward(field.as_ref());
        // For value transforms, the complement stores the original if needed
        // for lossless round-tripping. Field transform expressions are
        // forward-only; inverse behavior requires protocol-level directed
        // equations with an `inverse` field.
        TreeEdit::SetField {
            node_id,
            field: Name::from(source_name.as_str()),
            value: value.clone(),
        }
    }

    /// Get the policy expression for a given vertex kind, reading from
    /// the source schema's policy sorts.
    #[must_use]
    pub fn policy_for(&self, kind: &Name) -> Option<&panproto_expr::Expr> {
        self.src_schema.policies.get(kind)
    }

    /// Get directed equations from the protocol theory that are relevant
    /// to this migration. Filters to equations whose LHS references sorts
    /// that appear in the migration's surviving vertex set.
    #[must_use]
    pub fn translation_rules<'a>(&self, theory: &'a Theory) -> Vec<&'a DirectedEquation> {
        // The protocol theory is already scoped to this protocol, so all
        // its directed equations are relevant to this migration.
        theory.directed_eqs.iter().collect()
    }

    // -- Private helpers: structural remap --

    fn remap_anchor_forward(&self, anchor: &Name) -> Name {
        self.compiled
            .vertex_remap
            .get(anchor)
            .cloned()
            .unwrap_or_else(|| anchor.clone())
    }

    fn remap_anchor_backward(&self, anchor: &Name) -> Name {
        self.reverse_vertex_remap
            .get(anchor)
            .cloned()
            .unwrap_or_else(|| anchor.clone())
    }

    /// Remap a node's anchor and apply field transforms from the compiled migration.
    ///
    /// Note: this operates on an individual node without instance context,
    /// so child scalar values are unavailable. `ComputeField` transforms that
    /// read child scalars will only see `extra_fields` here. For full fiber
    /// access, use the restrict/extend pipeline instead.
    fn remap_and_transform_node(&self, node: &panproto_inst::Node) -> panproto_inst::Node {
        let mut remapped = node.clone();
        // Apply field transforms if any exist for this source anchor.
        if let Some(transforms) = self.compiled.field_transforms.get(&node.anchor) {
            panproto_inst::wtype::apply_field_transforms(
                &mut remapped,
                transforms,
                &std::collections::HashMap::new(),
            );
        }
        remapped.anchor = self.remap_anchor_forward(&node.anchor);
        remapped
    }

    fn remap_node_backward(&self, node: &panproto_inst::Node) -> panproto_inst::Node {
        let mut remapped = node.clone();
        remapped.anchor = self.remap_anchor_backward(&node.anchor);
        remapped
    }

    fn remap_edge_forward(&self, edge: &Edge) -> Edge {
        self.compiled
            .edge_remap
            .get(edge)
            .cloned()
            .unwrap_or_else(|| {
                let mut e = edge.clone();
                e.src = self.remap_anchor_forward(&edge.src);
                e.tgt = self.remap_anchor_forward(&edge.tgt);
                e
            })
    }

    fn remap_edge_backward(&self, edge: &Edge) -> Edge {
        self.reverse_edge_remap
            .get(edge)
            .cloned()
            .unwrap_or_else(|| {
                let mut e = edge.clone();
                e.src = self.remap_anchor_backward(&edge.src);
                e.tgt = self.remap_anchor_backward(&edge.tgt);
                e
            })
    }

    fn remap_fan_forward(&self, fan: &panproto_inst::Fan) -> panproto_inst::Fan {
        let new_he_id = if let Some((new_id, _)) =
            self.compiled.hyper_resolver.get(fan.hyper_edge_id.as_str())
        {
            new_id.to_string()
        } else {
            fan.hyper_edge_id.clone()
        };

        let new_children = if let Some((_, label_map)) =
            self.compiled.hyper_resolver.get(fan.hyper_edge_id.as_str())
        {
            fan.children
                .iter()
                .map(|(label, &node_id)| {
                    let new_label = label_map
                        .get(label.as_str())
                        .map_or_else(|| label.clone(), std::string::ToString::to_string);
                    (new_label, node_id)
                })
                .collect()
        } else {
            fan.children.clone()
        };

        panproto_inst::Fan {
            hyper_edge_id: new_he_id,
            parent: fan.parent,
            children: new_children,
        }
    }

    fn remap_fan_backward(&self, fan: &panproto_inst::Fan) -> panproto_inst::Fan {
        // Reverse remap: check if any hyper_resolver entry maps to this fan's ID.
        for (old_id, (new_id, label_map)) in &self.compiled.hyper_resolver {
            if new_id.as_ref() == fan.hyper_edge_id.as_str() {
                let reverse_labels: HashMap<String, String> = label_map
                    .iter()
                    .map(|(k, v)| (v.to_string(), k.to_string()))
                    .collect();
                let children = fan
                    .children
                    .iter()
                    .map(|(label, &node_id)| {
                        let old_label = reverse_labels
                            .get(label)
                            .cloned()
                            .unwrap_or_else(|| label.clone());
                        (old_label, node_id)
                    })
                    .collect();
                return panproto_inst::Fan {
                    hyper_edge_id: old_id.to_string(),
                    parent: fan.parent,
                    children,
                };
            }
        }
        fan.clone()
    }

    // -- Private helpers: field translation --

    /// Translate a field name through the migration's field transforms.
    /// If a `RenameField` transform renames this field, return the new name.
    fn translate_field_name(&self, field: &str) -> String {
        // Check all vertex anchors for rename transforms that affect this field.
        for transforms in self.compiled.field_transforms.values() {
            for transform in transforms {
                if let panproto_inst::FieldTransform::RenameField { old_key, new_key } = transform {
                    if old_key == field {
                        return new_key.clone();
                    }
                }
            }
        }
        field.to_owned()
    }

    /// Reverse a field name translation (target name -> source name).
    fn translate_field_name_backward(&self, field: &str) -> String {
        for transforms in self.compiled.field_transforms.values() {
            for transform in transforms {
                if let panproto_inst::FieldTransform::RenameField { old_key, new_key } = transform {
                    if new_key == field {
                        return old_key.clone();
                    }
                }
            }
        }
        field.to_owned()
    }

    /// Translate a field edit (name + value) through field transforms.
    /// Returns the translated (name, value) pair.
    fn translate_field_edit(
        &self,
        field: &str,
        value: &panproto_inst::Value,
    ) -> (String, panproto_inst::Value) {
        let mut name = field.to_owned();
        let mut val = value.clone();

        // Apply all relevant field transforms in order.
        for transforms in self.compiled.field_transforms.values() {
            for transform in transforms {
                match transform {
                    panproto_inst::FieldTransform::RenameField { old_key, new_key }
                        if old_key == field =>
                    {
                        name.clone_from(new_key);
                    }
                    panproto_inst::FieldTransform::ApplyExpr { key, expr, .. } if key == field => {
                        let input = panproto_inst::value_to_expr_literal(&val);
                        let env = panproto_expr::Env::new()
                            .extend(std::sync::Arc::from(key.as_str()), input);
                        let config = panproto_expr::EvalConfig::default();
                        if let Ok(result) = panproto_expr::eval(expr, &env, &config) {
                            val = expr_literal_to_value(&result);
                        }
                    }
                    _ => {}
                }
            }
        }

        (name, val)
    }
}

/// Convert an expression literal to a panproto `Value`.
fn expr_literal_to_value(lit: &panproto_expr::Literal) -> panproto_inst::Value {
    match lit {
        panproto_expr::Literal::Bool(b) => panproto_inst::Value::Bool(*b),
        panproto_expr::Literal::Int(i) => panproto_inst::Value::Int(*i),
        panproto_expr::Literal::Float(f) => {
            // Normalize integer-valued floats for JSON round-trip fidelity.
            #[allow(clippy::cast_precision_loss)]
            let fits = f.fract() == 0.0 && *f >= i64::MIN as f64 && *f <= i64::MAX as f64;
            if fits {
                #[allow(clippy::cast_possible_truncation)]
                let i = *f as i64;
                panproto_inst::Value::Int(i)
            } else {
                panproto_inst::Value::Float(*f)
            }
        }
        panproto_expr::Literal::Str(s) => panproto_inst::Value::Str(s.clone()),
        _ => panproto_inst::Value::Null,
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use panproto_gat::Name;
    use panproto_inst::{Node, TreeEdit};
    use panproto_schema::{Edge, Protocol};

    use crate::tests::{identity_lens, three_node_instance, three_node_schema};

    use super::EditLens;

    fn test_protocol() -> Protocol {
        Protocol {
            name: "test".into(),
            schema_theory: "ThTest".into(),
            instance_theory: "ThWType".into(),
            schema_composition: None,
            instance_composition: None,
            edge_rules: vec![],
            obj_kinds: vec![],
            constraint_sorts: vec![],
            has_order: false,
            has_coproducts: false,
            has_recursion: false,
            has_causal: false,
            nominal_identity: false,
            has_defaults: false,
            has_coercions: false,
            has_mergers: false,
            has_policies: false,
        }
    }

    #[test]
    fn identity_edit_lens_passes_through() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let edit = TreeEdit::SetField {
            node_id: 1,
            field: Name::from("text"),
            value: panproto_inst::Value::Str("updated".into()),
        };

        let result = edit_lens.get_edit(edit).unwrap();
        match &result {
            TreeEdit::SetField { node_id, field, .. } => {
                assert_eq!(*node_id, 1);
                assert_eq!(field, &Name::from("text"));
            }
            other => panic!("expected SetField, got {other:?}"),
        }
    }

    #[test]
    fn non_surviving_insert_goes_to_complement() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let edit = TreeEdit::InsertNode {
            parent: 0,
            child_id: 99,
            node: Node::new(99, "post:body.createdAt"),
            edge: Edge {
                src: "post:body".into(),
                tgt: "post:body.createdAt".into(),
                kind: "prop".into(),
                name: Some("createdAt".into()),
            },
        };

        let result = edit_lens.get_edit(edit).unwrap();
        assert!(
            result.is_identity(),
            "non-surviving anchor should produce Identity"
        );
        assert!(
            edit_lens.complement.dropped_nodes.contains_key(&99),
            "node should be in complement"
        );
    }

    #[test]
    fn set_field_on_complement_node_is_absorbed() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        assert!(edit_lens.complement.dropped_nodes.contains_key(&2));

        let edit = TreeEdit::SetField {
            node_id: 2,
            field: Name::from("value"),
            value: panproto_inst::Value::Str("2025-01-01".into()),
        };

        let result = edit_lens.get_edit(edit).unwrap();
        assert!(result.is_identity());

        let node = &edit_lens.complement.dropped_nodes[&2];
        assert_eq!(
            node.extra_fields.get("value"),
            Some(&panproto_inst::Value::Str("2025-01-01".into()))
        );
    }

    #[test]
    fn delete_node_in_complement() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        assert!(edit_lens.complement.dropped_nodes.contains_key(&2));

        let edit = TreeEdit::DeleteNode { id: 2 };
        let result = edit_lens.get_edit(edit).unwrap();
        assert!(result.is_identity());
        assert!(!edit_lens.complement.dropped_nodes.contains_key(&2));
    }

    #[test]
    fn delete_node_in_view_passes_through() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let edit = TreeEdit::DeleteNode { id: 1 };
        let result = edit_lens.get_edit(edit).unwrap();
        match result {
            TreeEdit::DeleteNode { id } => assert_eq!(id, 1),
            other => panic!("expected DeleteNode, got {other:?}"),
        }
    }

    #[test]
    fn put_edit_passes_through_for_identity() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let edit = TreeEdit::SetField {
            node_id: 1,
            field: Name::from("text"),
            value: panproto_inst::Value::Str("from_view".into()),
        };

        let result = edit_lens.put_edit(edit).unwrap();
        match &result {
            TreeEdit::SetField { node_id, .. } => assert_eq!(*node_id, 1),
            other => panic!("expected SetField, got {other:?}"),
        }
    }

    #[test]
    fn sequence_edit_filters_identity() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let edit = TreeEdit::Sequence(vec![
            TreeEdit::SetField {
                node_id: 1,
                field: Name::from("text"),
                value: panproto_inst::Value::Str("hi".into()),
            },
            TreeEdit::SetField {
                node_id: 2,
                field: Name::from("val"),
                value: panproto_inst::Value::Int(1),
            },
        ]);

        let result = edit_lens.get_edit(edit).unwrap();
        match result {
            TreeEdit::SetField { node_id, .. } => assert_eq!(node_id, 1),
            other => panic!("expected single SetField, got {other:?}"),
        }
    }

    #[test]
    fn insert_fan_with_dropped_participant_goes_to_complement() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        // Node 2 is in the complement (createdAt was dropped).
        let fan = panproto_inst::Fan::new("test_he", 0)
            .with_child("a", 1)
            .with_child("b", 2);
        let edit = TreeEdit::InsertFan { fan };

        let result = edit_lens.get_edit(edit).unwrap();
        assert!(
            result.is_identity(),
            "fan with dropped participant should be absorbed"
        );
        assert_eq!(edit_lens.complement.dropped_fans.len(), 1);
    }

    #[test]
    fn insert_fan_all_surviving_passes_through() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        let fan = panproto_inst::Fan::new("test_he", 0)
            .with_child("a", 1)
            .with_child("b", 0);
        let edit = TreeEdit::InsertFan { fan };

        let result = edit_lens.get_edit(edit).unwrap();
        match result {
            TreeEdit::InsertFan { .. } => {}
            other => panic!("expected InsertFan, got {other:?}"),
        }
    }

    #[test]
    fn delete_fan_in_complement_is_absorbed() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let instance = three_node_instance();
        let mut edit_lens = EditLens::from_lens(lens, test_protocol());
        edit_lens.initialize(&instance).unwrap();

        // Manually add a fan to the complement.
        edit_lens
            .complement
            .dropped_fans
            .push(panproto_inst::Fan::new("dropped_he", 0).with_child("x", 2));

        let edit = TreeEdit::DeleteFan {
            hyper_edge_id: Name::from("dropped_he"),
        };
        let result = edit_lens.get_edit(edit).unwrap();
        assert!(result.is_identity());
        assert!(edit_lens.complement.dropped_fans.is_empty());
    }

    #[test]
    fn optic_kind_identity_is_iso() {
        let schema = three_node_schema();
        let lens = identity_lens(&schema);
        let edit_lens = EditLens::from_lens(lens, test_protocol());
        assert_eq!(
            edit_lens.optic_kind(),
            crate::OpticKind::Iso,
            "identity lens should classify as Iso"
        );
    }

    #[test]
    fn optic_kind_projection_is_lens() {
        let schema = three_node_schema();
        let lens = crate::tests::projection_lens(&schema, "createdAt");
        let edit_lens = EditLens::from_lens(lens, test_protocol());
        assert_eq!(
            edit_lens.optic_kind(),
            crate::OpticKind::Lens,
            "projection lens should classify as Lens"
        );
    }
}