panproto-mig 0.48.6

Migration engine 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
//! Type-signature alignment strategy.
//!
//! Proposes anchors between source and target vertices whose outgoing
//! edge-kind multisets and leaf-kind multisets match, even when their
//! names diverge entirely. This complements the alias and token-
//! similarity strategies by catching cases where the signature of a
//! record (what shape it has, not what it's called) is the dominant
//! evidence of correspondence.
//!
//! The strategy computes, for each vertex, its **kind signature**: a
//! sorted multiset of `(edge_kind, target_vertex_kind)` pairs over its
//! outgoing edges. Vertex pairs whose kind signatures are equal and
//! whose own kinds match receive a high-confidence anchor; pairs whose
//! signatures are non-equal but share a majority of entries receive a
//! lower-confidence anchor scaled by the overlap ratio.
//!
//! Because the strategy ignores names entirely, it runs well below
//! `exact`, `alias`, and `token_similarity` in priority. The CSP still
//! enforces naturality for every emitted anchor, so wrong matches are
//! rejected downstream.

use std::collections::HashMap;

use panproto_gat::Name;
use panproto_schema::Schema;

use super::{Anchor, StrategyTag, kinds_compatible};

/// Emit anchors for kind-signature-compatible vertex pairs.
///
/// `threshold` is the minimum overlap ratio `|A ∩ B| / max(|A|, |B|)`
/// for an anchor to be emitted. Values below `0.5` are clipped up to
/// `0.5` to avoid pairing vertices that agree on less than half of
/// their signature.
///
/// Tie-break: when multiple candidate targets share the best overlap
/// score, the lowest-alphabetical target wins (source and target ids
/// are both iterated in sorted order, and the ">" comparison retains
/// the first-seen maximum).
#[must_use]
pub fn type_signature_anchors(src: &Schema, tgt: &Schema, threshold: f64) -> Vec<Anchor> {
    let effective_threshold = threshold.max(0.5);
    let src_sigs: HashMap<Name, Vec<SignatureEntry>> = src
        .vertices
        .keys()
        .map(|id| (id.clone(), signature(src, id)))
        .collect();
    let tgt_sigs: HashMap<Name, Vec<SignatureEntry>> = tgt
        .vertices
        .keys()
        .map(|id| (id.clone(), signature(tgt, id)))
        .collect();

    let mut out = Vec::new();

    let mut src_ids: Vec<&Name> = src.vertices.keys().collect();
    src_ids.sort_by(|a, b| a.as_str().cmp(b.as_str()));
    let mut tgt_ids: Vec<&Name> = tgt.vertices.keys().collect();
    tgt_ids.sort_by(|a, b| a.as_str().cmp(b.as_str()));

    for src_id in src_ids.iter().copied() {
        let Some(src_sig) = src_sigs.get(src_id) else {
            continue;
        };
        if src_sig.is_empty() {
            continue;
        }

        let mut best: Option<(Name, f64, usize)> = None;
        for tgt_id in tgt_ids.iter().copied() {
            if !kinds_compatible(src, src_id, tgt, tgt_id) {
                continue;
            }
            let Some(tgt_sig) = tgt_sigs.get(tgt_id) else {
                continue;
            };
            if tgt_sig.is_empty() {
                continue;
            }
            let (overlap, _matched) = multiset_overlap(src_sig, tgt_sig);
            if overlap < effective_threshold {
                continue;
            }
            let larger = src_sig.len().max(tgt_sig.len());
            if best.as_ref().is_none_or(|(_, bs, _)| overlap > *bs) {
                best = Some((tgt_id.clone(), overlap, larger));
            }
        }

        if let Some((tgt_id, overlap, size)) = best {
            // Exact-signature equality earns a confidence boost.
            let boost = if (overlap - 1.0).abs() < f64::EPSILON {
                0.1
            } else {
                0.0
            };
            // Scale confidence down slightly for very small signatures
            // (two fields with one edge is weak evidence regardless).
            let size_penalty = if size < 2 { 0.1 } else { 0.0 };
            let confidence = (overlap + boost - size_penalty).clamp(0.4, 0.9);
            out.push(Anchor {
                src: src_id.clone(),
                tgt: tgt_id.clone(),
                confidence,
                strategy: StrategyTag::TypeSignature,
                explanation: format!(
                    "kind-signature overlap {:.2} on {size} field(s): {}{}",
                    overlap,
                    src_id.as_str(),
                    tgt_id.as_str()
                ),
            });
        }
    }

    out
}

/// One entry in a vertex's kind signature: `(edge_kind, leaf_kind)`.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
struct SignatureEntry {
    edge_kind: String,
    leaf_kind: String,
}

/// Build the kind signature for `vertex`: the sorted multiset of
/// (edge.kind, edge.tgt.kind) over its outgoing edges. Edges whose
/// target vertex is unknown (should not happen in a well-formed
/// schema) are skipped.
fn signature(schema: &Schema, vertex: &Name) -> Vec<SignatureEntry> {
    let mut out: Vec<SignatureEntry> = schema
        .outgoing_edges(vertex)
        .iter()
        .filter_map(|edge| {
            schema.vertex(&edge.tgt).map(|v| SignatureEntry {
                edge_kind: edge.kind.as_str().to_owned(),
                leaf_kind: v.kind.as_str().to_owned(),
            })
        })
        .collect();
    out.sort();
    out
}

/// Compute `(overlap_ratio, matched_count)` between two multisets.
/// Overlap is defined as `|intersection| / max(|A|, |B|)` so identical
/// multisets score `1.0`, disjoint score `0.0`.
fn multiset_overlap(a: &[SignatureEntry], b: &[SignatureEntry]) -> (f64, usize) {
    if a.is_empty() && b.is_empty() {
        return (1.0, 0);
    }
    let mut counts_a: HashMap<&SignatureEntry, usize> = HashMap::new();
    for e in a {
        *counts_a.entry(e).or_insert(0) += 1;
    }
    let mut counts_b: HashMap<&SignatureEntry, usize> = HashMap::new();
    for e in b {
        *counts_b.entry(e).or_insert(0) += 1;
    }
    let mut intersection = 0usize;
    for (entry, &ca) in &counts_a {
        if let Some(&cb) = counts_b.get(entry) {
            intersection += ca.min(cb);
        }
    }
    let denom_count = a.len().max(b.len());
    let inter_f = f64::from(u32::try_from(intersection).unwrap_or(u32::MAX));
    let denom_f = f64::from(u32::try_from(denom_count).unwrap_or(u32::MAX));
    let ratio = inter_f / denom_f;
    (ratio, intersection)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::float_cmp)]
mod tests {
    use super::*;
    use panproto_schema::{Protocol, SchemaBuilder};

    fn test_protocol() -> Protocol {
        Protocol {
            name: "test".into(),
            schema_theory: "ThTest".into(),
            instance_theory: "ThWType".into(),
            edge_rules: vec![],
            obj_kinds: vec!["object".into(), "string".into(), "integer".into()],
            constraint_sorts: vec![],
            ..Protocol::default()
        }
    }

    fn build_schema(vertices: &[(&str, &str)], edges: &[(&str, &str, &str, &str)]) -> Schema {
        let proto = test_protocol();
        let mut b = SchemaBuilder::new(&proto);
        for (id, kind) in vertices {
            b = b.vertex(id, kind, None::<&str>).unwrap();
        }
        for (src, tgt, kind, name) in edges {
            b = b.edge(src, tgt, kind, Some(*name)).unwrap();
        }
        b.build().unwrap()
    }

    #[test]
    fn signature_equal_multisets_full_overlap() {
        let a = vec![
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
        ];
        let (ratio, count) = multiset_overlap(&a, &a);
        assert_eq!(ratio, 1.0);
        assert_eq!(count, 2);
    }

    #[test]
    fn disjoint_multisets_zero_overlap() {
        let a = vec![SignatureEntry {
            edge_kind: "prop".into(),
            leaf_kind: "string".into(),
        }];
        let b = vec![SignatureEntry {
            edge_kind: "prop".into(),
            leaf_kind: "integer".into(),
        }];
        let (ratio, _) = multiset_overlap(&a, &b);
        assert_eq!(ratio, 0.0);
    }

    #[test]
    fn aligns_records_with_same_type_signature_and_different_names() {
        // Two records with two string children each — renames across
        // both sides. Type signature still pairs them.
        let src = build_schema(
            &[
                ("post", "object"),
                ("post.text", "string"),
                ("post.createdAt", "string"),
            ],
            &[
                ("post", "post.text", "prop", "text"),
                ("post", "post.createdAt", "prop", "createdAt"),
            ],
        );
        let tgt = build_schema(
            &[
                ("message", "object"),
                ("message.body", "string"),
                ("message.sentAt", "string"),
            ],
            &[
                ("message", "message.body", "prop", "body"),
                ("message", "message.sentAt", "prop", "sentAt"),
            ],
        );

        let anchors = type_signature_anchors(&src, &tgt, 0.5);
        assert!(
            anchors
                .iter()
                .any(|a| a.src.as_str() == "post" && a.tgt.as_str() == "message"),
            "expected post ↔ message anchor from matching type signature; got {anchors:?}"
        );
    }

    #[test]
    fn rejects_mismatched_leaf_kinds() {
        let src = build_schema(
            &[("r", "object"), ("r.x", "string")],
            &[("r", "r.x", "prop", "x")],
        );
        let tgt = build_schema(
            &[("r", "object"), ("r.y", "integer")],
            &[("r", "r.y", "prop", "y")],
        );
        let anchors = type_signature_anchors(&src, &tgt, 0.5);
        assert!(
            anchors.is_empty(),
            "string vs integer leaves must not align via type signature: {anchors:?}"
        );
    }

    #[test]
    fn signature_is_order_insensitive() {
        let a = build_schema(
            &[("r", "object"), ("r.x", "string"), ("r.y", "integer")],
            &[("r", "r.x", "prop", "x"), ("r", "r.y", "prop", "y")],
        );
        let b = build_schema(
            &[("r", "object"), ("r.y", "integer"), ("r.x", "string")],
            &[("r", "r.y", "prop", "y"), ("r", "r.x", "prop", "x")],
        );
        let sig_a = signature(&a, &Name::from("r"));
        let sig_b = signature(&b, &Name::from("r"));
        assert_eq!(
            sig_a, sig_b,
            "multiset signatures must be equal regardless of edge insertion order"
        );
    }

    #[test]
    fn multiset_overlap_empty_nonempty() {
        let a: Vec<SignatureEntry> = vec![];
        let b = vec![SignatureEntry {
            edge_kind: "prop".into(),
            leaf_kind: "string".into(),
        }];
        let (ratio, count) = multiset_overlap(&a, &b);
        assert_eq!(count, 0);
        // Per code: |A|=0, |B|=1, intersection=0 → 0/1 = 0.0. Only
        // empty-empty returns 1.0.
        assert_eq!(ratio, 0.0);
    }

    #[test]
    fn type_signature_leaf_only_schema() {
        // Leaves have empty signatures; strategy must skip them without panic.
        let src = build_schema(&[("x", "string")], &[]);
        let tgt = build_schema(&[("y", "string")], &[]);
        assert!(type_signature_anchors(&src, &tgt, 0.5).is_empty());
    }

    #[test]
    fn type_signature_deterministic_emission() {
        let perms: [&[(&str, &str)]; 2] = [
            &[
                ("aa", "object"),
                ("bb", "object"),
                ("aa.x", "string"),
                ("bb.x", "string"),
            ],
            &[
                ("bb", "object"),
                ("aa", "object"),
                ("bb.x", "string"),
                ("aa.x", "string"),
            ],
        ];
        let tgt = build_schema(
            &[
                ("tt", "object"),
                ("uu", "object"),
                ("tt.x", "string"),
                ("uu.x", "string"),
            ],
            &[("tt", "tt.x", "prop", "x"), ("uu", "uu.x", "prop", "x")],
        );
        let mut results = Vec::new();
        for verts in perms {
            let edges: Vec<(&str, &str, &str, &str)> = verts
                .iter()
                .filter(|(id, _)| !id.contains('.'))
                .map(|(id, _)| {
                    (
                        *id,
                        Box::leak(format!("{id}.x").into_boxed_str()) as &str,
                        "prop",
                        "x",
                    )
                })
                .collect();
            let src = build_schema(verts, &edges);
            let anchors = type_signature_anchors(&src, &tgt, 0.5);
            let mut pairs: Vec<_> = anchors
                .iter()
                .map(|a| (a.src.as_str().to_owned(), a.tgt.as_str().to_owned()))
                .collect();
            pairs.sort();
            results.push(pairs);
        }
        assert_eq!(results[0], results[1]);
    }

    #[test]
    fn type_signature_emits_kind_compatible_only() {
        let src = build_schema(
            &[("r", "object"), ("r.x", "string")],
            &[("r", "r.x", "prop", "x")],
        );
        let tgt = build_schema(
            &[("r", "object"), ("r.x", "string")],
            &[("r", "r.x", "prop", "x")],
        );
        for anchor in type_signature_anchors(&src, &tgt, 0.5) {
            assert!(kinds_compatible(&src, &anchor.src, &tgt, &anchor.tgt));
        }
    }

    #[test]
    fn type_signature_single_isolated_vertex() {
        // Smallest legal schema: one vertex with no edges → empty signature
        // → no anchors emitted.
        let s = build_schema(&[("lone", "string")], &[]);
        let t = build_schema(&[("other", "string")], &[]);
        assert!(type_signature_anchors(&s, &t, 0.5).is_empty());
    }

    #[test]
    fn type_signature_tie_break_picks_lowest_alpha_target() {
        // Source matches two targets with identical signatures. Sorted
        // iteration + strict > means the first-seen (alphabetically
        // lowest) target wins.
        let src = build_schema(
            &[("r", "object"), ("r.x", "string")],
            &[("r", "r.x", "prop", "x")],
        );
        let tgt = build_schema(
            &[
                ("aa", "object"),
                ("aa.x", "string"),
                ("zz", "object"),
                ("zz.x", "string"),
            ],
            &[("aa", "aa.x", "prop", "x"), ("zz", "zz.x", "prop", "x")],
        );
        let anchors = type_signature_anchors(&src, &tgt, 0.5);
        let r_anchor = anchors.iter().find(|a| a.src.as_str() == "r").unwrap();
        assert_eq!(
            r_anchor.tgt.as_str(),
            "aa",
            "tie-break favors lowest-alpha target"
        );
    }

    #[test]
    fn type_signature_tie_break_is_stable_across_three_identical_targets() {
        // When three candidate targets all tie at ratio = 1.0, the
        // strict `>` check retains the first-seen maximum; combined
        // with sorted target iteration this must pick the
        // alphabetically lowest target regardless of insertion order
        // in the backing `HashMap`.
        let src = build_schema(
            &[("r", "object"), ("r.x", "string")],
            &[("r", "r.x", "prop", "x")],
        );
        let tgt = build_schema(
            &[
                ("bbb", "object"),
                ("bbb.x", "string"),
                ("aaa", "object"),
                ("aaa.x", "string"),
                ("ccc", "object"),
                ("ccc.x", "string"),
            ],
            &[
                ("bbb", "bbb.x", "prop", "x"),
                ("aaa", "aaa.x", "prop", "x"),
                ("ccc", "ccc.x", "prop", "x"),
            ],
        );
        let anchors = type_signature_anchors(&src, &tgt, 0.5);
        let r_anchor = anchors.iter().find(|a| a.src.as_str() == "r").unwrap();
        assert_eq!(
            r_anchor.tgt.as_str(),
            "aaa",
            "three-way tie must still resolve to lowest-alpha target"
        );
    }

    #[test]
    fn type_signature_bit_identical_across_100_runs() {
        let src = build_schema(
            &[
                ("post", "object"),
                ("post.text", "string"),
                ("post.createdAt", "string"),
            ],
            &[
                ("post", "post.text", "prop", "text"),
                ("post", "post.createdAt", "prop", "createdAt"),
            ],
        );
        let tgt = build_schema(
            &[
                ("message", "object"),
                ("message.body", "string"),
                ("message.sentAt", "string"),
            ],
            &[
                ("message", "message.body", "prop", "body"),
                ("message", "message.sentAt", "prop", "sentAt"),
            ],
        );
        let baseline: Vec<(String, String, u64)> = type_signature_anchors(&src, &tgt, 0.5)
            .iter()
            .map(|a| {
                (
                    a.src.as_str().into(),
                    a.tgt.as_str().into(),
                    a.confidence.to_bits(),
                )
            })
            .collect();
        for _ in 0..100 {
            let again: Vec<(String, String, u64)> = type_signature_anchors(&src, &tgt, 0.5)
                .iter()
                .map(|a| {
                    (
                        a.src.as_str().into(),
                        a.tgt.as_str().into(),
                        a.confidence.to_bits(),
                    )
                })
                .collect();
            assert_eq!(again, baseline);
        }
    }

    #[test]
    fn multiset_overlap_asymmetric_counts() {
        // Pins the counts-based intersection semantics. Multiset A =
        // {prop-string: 3, prop-integer: 1}, B = {prop-string: 2,
        // prop-integer: 2}. Intersection = min(3,2) + min(1,2) = 3.
        // |A| = 4, |B| = 4, denom = max = 4, ratio = 3/4 = 0.75.
        let a = vec![
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "integer".into(),
            },
        ];
        let b = vec![
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "string".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "integer".into(),
            },
            SignatureEntry {
                edge_kind: "prop".into(),
                leaf_kind: "integer".into(),
            },
        ];
        let (ratio, count) = multiset_overlap(&a, &b);
        assert_eq!(count, 3);
        assert!(
            (ratio - 0.75).abs() < 1e-9,
            "asymmetric multiset ratio should be 0.75, got {ratio}"
        );
        // Symmetric in argument order.
        let (ratio_swapped, count_swapped) = multiset_overlap(&b, &a);
        assert_eq!(count_swapped, 3);
        assert!((ratio_swapped - 0.75).abs() < 1e-9);
    }

    #[test]
    fn threshold_gates_emission() {
        // Source record with 4 children; target with 1 matching child + 3 different.
        let src = build_schema(
            &[
                ("r", "object"),
                ("r.a", "string"),
                ("r.b", "string"),
                ("r.c", "string"),
                ("r.d", "string"),
            ],
            &[
                ("r", "r.a", "prop", "a"),
                ("r", "r.b", "prop", "b"),
                ("r", "r.c", "prop", "c"),
                ("r", "r.d", "prop", "d"),
            ],
        );
        let tgt = build_schema(
            &[
                ("r", "object"),
                ("r.a", "string"),
                ("r.b", "integer"),
                ("r.c", "integer"),
                ("r.d", "integer"),
            ],
            &[
                ("r", "r.a", "prop", "a"),
                ("r", "r.b", "prop", "b"),
                ("r", "r.c", "prop", "c"),
                ("r", "r.d", "prop", "d"),
            ],
        );
        // Overlap is 1/4 which is below the 0.5 floor.
        let anchors = type_signature_anchors(&src, &tgt, 0.5);
        assert!(
            !anchors.iter().any(|a| a.src.as_str() == "r"),
            "low overlap should not emit an anchor: {anchors:?}"
        );
    }
}