minuit2 0.4.1

Pure Rust port of CERN Minuit2 parameter optimization engine
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
#!/usr/bin/env python3
"""
Generate function-level parity artifacts for minuit2-rs.

Outputs:
  - reports/parity/functions.csv
  - reports/parity/gaps.md
  - reports/parity/missing.csv
  - reports/parity/needs_review.csv

Method:
  1. Parse Port entries from INVENTORY.md
  2. Fetch upstream files from a pinned Minuit2 commit
  3. Extract upstream function-like symbols (heuristic parser)
  4. Extract Rust symbols from src/ (excluding cfg(test) blocks)
  5. Map upstream symbol -> Rust symbol with confidence buckets
"""

from __future__ import annotations

import csv
import argparse
import re
import subprocess
import sys
import textwrap
import urllib.error
import urllib.request
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path


REPO_ROOT = Path(__file__).resolve().parents[1]
INVENTORY_PATH = REPO_ROOT / "INVENTORY.md"
REPORT_DIR = REPO_ROOT / "reports" / "parity"
CACHE_DIR = REPO_ROOT / ".cache" / "parity"

DEFAULT_UPSTREAM_REPO = "root-project/root"
DEFAULT_UPSTREAM_SUBDIR = "math/minuit2"
DEFAULT_UPSTREAM_TAG = "v6-36-08"


CPP_KEYWORDS = {
    "if",
    "for",
    "while",
    "switch",
    "catch",
    "return",
    "sizeof",
    "do",
}

ARCHITECTURAL_BASENAMES = {
    "MnApplication",
    "MnPrint",
    "MnPrintImpl",
    "FunctionMinimizer",
    "ModularFunctionMinimizer",
    "MinimumBuilder",
    "MinimumSeedGenerator",
    "MinimumErrorUpdator",
    "GradientCalculator",
    "VariableMetricEDMEstimator",
    "ScanBuilder",
    "ScanMinimizer",
    "CombinedMinimizer",
    "CombinedMinimumBuilder",
    "MnTraceObject",
    "MnConfig",
    "MnTiny",
    "MnEigen",
    "mnteigen",
    "MnVectorTransform",
    "MnParabolaFactory",
}

# Manual file-level mapping to keep the first matrix deterministic.
MANUAL_BASE_TO_RUST = {
    "AnalyticalGradientCalculator": ["src/gradient/analytical.rs"],
    "BFGSErrorUpdator": ["src/migrad/builder.rs"],
    "BasicFunctionGradient": ["src/minimum/gradient.rs"],
    "BasicFunctionMinimum": ["src/minimum/mod.rs", "src/minimum/error.rs", "src/user_parameter_state.rs"],
    "BasicMinimumError": ["src/minimum/error.rs"],
    "BasicMinimumParameters": ["src/minimum/parameters.rs"],
    "BasicMinimumSeed": ["src/minimum/seed.rs"],
    "BasicMinimumState": ["src/minimum/state.rs"],
    "CombinedMinimizer": ["src/minimize/mod.rs"],
    "CombinedMinimumBuilder": ["src/minimize/mod.rs"],
    "ContoursError": ["src/contours/contours_error.rs"],
    "DavidonErrorUpdator": ["src/migrad/builder.rs"],
    "FCNBase": ["src/fcn.rs"],
    "FCNGradientBase": ["src/fcn.rs"],
    "FunctionGradient": ["src/minimum/gradient.rs"],
    "FunctionMinimizer": ["src/migrad/minimizer.rs", "src/simplex/minimizer.rs"],
    "FunctionMinimum": ["src/minimum/mod.rs", "src/minimum/error.rs", "src/user_parameter_state.rs"],
    "GradientCalculator": ["src/gradient/mod.rs"],
    "HessianGradientCalculator": ["src/hesse/gradient.rs"],
    "InitialGradientCalculator": ["src/gradient/initial.rs"],
    "MinimumBuilder": ["src/migrad/builder.rs", "src/simplex/builder.rs", "src/minimize/mod.rs"],
    "MinimumError": ["src/minimum/error.rs"],
    "MinimumErrorUpdator": ["src/migrad/builder.rs"],
    "MinimumParameters": ["src/minimum/parameters.rs"],
    "MinimumSeed": ["src/minimum/seed.rs"],
    "MinimumSeedGenerator": ["src/migrad/seed.rs", "src/simplex/seed.rs"],
    "MinimumState": ["src/minimum/state.rs"],
    "MinosError": ["src/minos/minos_error.rs"],
    "MinuitParameter": ["src/parameter.rs"],
    "MnApplication": ["src/application.rs"],
    "MnConfig": ["src/precision.rs", "src/strategy.rs"],
    "MnContours": ["src/contours/mod.rs"],
    "MnCovarianceSqueeze": ["src/covariance_squeeze.rs"],
    "MnCross": ["src/minos/cross.rs"],
    "MnEigen": ["src/posdef.rs"],
    "MnFcn": ["src/mn_fcn.rs"],
    "MnFunctionCross": ["src/minos/function_cross.rs"],
    "MnGlobalCorrelationCoeff": ["src/global_cc.rs"],
    "MnHesse": ["src/hesse/mod.rs"],
    "MnLineSearch": ["src/linesearch.rs"],
    "MnMachinePrecision": ["src/precision.rs"],
    "MnMigrad": ["src/migrad/mod.rs"],
    "MnMinimize": ["src/minimize/mod.rs"],
    "MnMinos": ["src/minos/mod.rs"],
    "MnParabola": ["src/parabola.rs"],
    "MnParabolaFactory": ["src/parabola.rs"],
    "MnParabolaPoint": ["src/parabola.rs"],
    "MnParameterScan": ["src/scan/mod.rs"],
    "MnPosDef": ["src/posdef.rs"],
    "MnPrint": ["src/print.rs"],
    "MnPrintImpl": ["src/print.rs"],
    "MnScan": ["src/scan/mod.rs"],
    "MnSeedGenerator": ["src/migrad/seed.rs"],
    "MnSimplex": ["src/simplex/mod.rs"],
    "MnStrategy": ["src/strategy.rs"],
    "MnTiny": ["src/precision.rs"],
    "MnTraceObject": [],
    "MnUserCovariance": ["src/user_covariance.rs"],
    "MnUserFcn": ["src/mn_fcn.rs", "src/fcn.rs"],
    "MnUserParameterState": ["src/user_parameter_state.rs", "src/user_parameters.rs", "src/user_transformation.rs"],
    "MnUserParameters": ["src/user_parameters.rs"],
    "MnUserTransformation": ["src/user_transformation.rs", "src/user_parameters.rs"],
    "MnVectorTransform": ["src/user_transformation.rs"],
    "ModularFunctionMinimizer": ["src/migrad/minimizer.rs", "src/simplex/minimizer.rs"],
    "NegativeG2LineSearch": ["src/migrad/builder.rs"],
    "Numerical2PGradientCalculator": ["src/gradient/numerical.rs"],
    "ScanBuilder": ["src/scan/mod.rs"],
    "ScanMinimizer": ["src/scan/mod.rs"],
    "SimplexBuilder": ["src/simplex/builder.rs"],
    "SimplexMinimizer": ["src/simplex/minimizer.rs"],
    "SimplexParameters": ["src/simplex/parameters.rs"],
    "SimplexSeedGenerator": ["src/simplex/seed.rs"],
    "SinParameterTransformation": ["src/transform/sin.rs"],
    "SqrtLowParameterTransformation": ["src/transform/sqrt_low.rs"],
    "SqrtUpParameterTransformation": ["src/transform/sqrt_up.rs"],
    "VariableMetricBuilder": ["src/migrad/builder.rs"],
    "VariableMetricEDMEstimator": ["src/migrad/builder.rs"],
    "VariableMetricMinimizer": ["src/migrad/minimizer.rs"],
    "mnteigen": ["src/posdef.rs"],
}


# Per-basename explicit aliasing for naming drift between C++ and Rust APIs.
SYMBOL_ALIASES: dict[str, dict[str, list[str]]] = {
    "MnStrategy": {
        "GradientNCycles": ["grad_ncycles"],
        "GradientStepTolerance": ["grad_step_tol"],
        "GradientTolerance": ["grad_tol"],
        "HessianNCycles": ["hess_ncycles"],
        "HessianStepTolerance": ["hess_step_tol"],
        "HessianG2Tolerance": ["hess_g2_tol"],
        "HessianGradientNCycles": ["hess_grad_ncycles"],
        "StorageLevel": ["strategy"],
    },
    "BasicFunctionMinimum": {
        "HasAccurateCovar": ["is_accurate"],
        "HasPosDefCovar": ["is_pos_def"],
        "HasCovariance": ["has_covariance"],
        "HasValidCovariance": ["has_covariance"],
        "HesseFailed": ["hesse_failed"],
    },
    "FunctionMinimum": {
        "HasAccurateCovar": ["is_accurate"],
        "HasPosDefCovar": ["is_pos_def"],
        "HasCovariance": ["has_covariance"],
        "HasValidCovariance": ["has_covariance"],
        "HesseFailed": ["hesse_failed"],
    },
}


@dataclass(frozen=True)
class UpstreamFile:
    path: str
    basename: str


@dataclass(frozen=True)
class Symbol:
    name: str
    line: int
    param_count: int


@dataclass
class RustSymbol:
    name: str
    line: int
    file: str
    norm: str


def run(cmd: list[str]) -> str:
    out = subprocess.check_output(cmd, cwd=REPO_ROOT, text=True)
    return out.strip()


def normalize_repo(repo_arg: str) -> str:
    repo_arg = repo_arg.strip()
    m = re.search(r"github\.com[:/]+([^/]+/[^/.]+)", repo_arg)
    if m:
        return m.group(1)
    if "/" in repo_arg and not repo_arg.startswith("http"):
        return repo_arg
    raise ValueError(f"unsupported repo format: {repo_arg}")


def repo_git_url(repo_slug: str) -> str:
    return f"https://github.com/{repo_slug}.git"


def resolve_git_ref(repo_slug: str, ref: str) -> str:
    url = repo_git_url(repo_slug)
    # Resolve annotated tags to the peeled commit first.
    for query in [f"refs/tags/{ref}^{{}}", f"refs/tags/{ref}", f"refs/heads/{ref}", ref]:
        output = run(["git", "ls-remote", url, query])
        if output:
            return output.split()[0]
    raise RuntimeError(f"unable to resolve ref '{ref}' in {repo_slug}")


def parse_inventory_port_files() -> list[UpstreamFile]:
    text = INVENTORY_PATH.read_text()
    files: list[UpstreamFile] = []
    for line in text.splitlines():
        if "| Port" not in line:
            continue
        m = re.search(r"\[([^\]]+)\]", line)
        if not m:
            continue
        rel = m.group(1).strip()
        if not (rel.startswith("inc/") or rel.startswith("src/")):
            continue
        basename = re.sub(r"\.(h|hpp|cxx)$", "", rel.split("/")[-1])
        files.append(UpstreamFile(path=rel, basename=basename))
    # keep insertion order, drop exact duplicates
    unique: dict[tuple[str, str], UpstreamFile] = {}
    for f in files:
        unique[(f.path, f.basename)] = f
    return list(unique.values())


def download_upstream_file(repo_slug: str, subdir: str, commit: str, rel_path: str) -> Path:
    cache_path = CACHE_DIR / repo_slug.replace("/", "__") / subdir.replace("/", "__") / commit / rel_path
    if cache_path.exists():
        return cache_path
    cache_path.parent.mkdir(parents=True, exist_ok=True)
    prefix = subdir.strip("/")
    full_path = f"{prefix}/{rel_path}" if prefix else rel_path
    url = f"https://raw.githubusercontent.com/{repo_slug}/{commit}/{full_path}"
    try:
        with urllib.request.urlopen(url) as r:  # nosec - fixed github raw host
            data = r.read()
    except urllib.error.HTTPError as e:
        raise RuntimeError(f"failed to download {url}: {e.code}") from e
    cache_path.write_bytes(data)
    return cache_path


def strip_cpp_comments(source: str) -> str:
    source = re.sub(r"/\*.*?\*/", "", source, flags=re.S)
    source = re.sub(r"//.*", "", source)
    return source


def estimate_param_count(params: str) -> int:
    p = params.strip()
    if not p or p == "void":
        return 0
    # simple comma count that works for most Minuit2 signatures
    return p.count(",") + 1


def extract_cpp_symbols(path: Path, basename: str) -> list[Symbol]:
    text = path.read_text(errors="replace")
    stripped = strip_cpp_comments(text)
    lines = stripped.splitlines()
    is_source = path.suffix == ".cxx"

    # Standard function/method declaration/definition with an explicit return type.
    # Anchored to line start to avoid matching regular function calls.
    func_pat = re.compile(
        r"^\s*"
        r"(?:(?:inline|virtual|static|constexpr|friend|explicit|extern)\s+)*"
        r"[A-Za-z_][A-Za-z0-9_:<>\s*&~]*\s+"
        r"(?P<name>(?:~?[A-Za-z_][A-Za-z0-9_:~]*|operator\s*(?:\(\)|\[\]|[^\s(]+(?:\s+[^\s(]+)*)))\s*"
        r"\((?P<params>[^)]*)\)\s*"
        r"(?:(?:const|override|final|noexcept)\s*)*"
        r"(?:=\s*(?:0|default|delete)\s*)?"
        r"(?:;|\{|:|}\s*;?)\s*$"
    )
    # Constructor / destructor declarations (no explicit return type).
    ctor_pat = re.compile(
        r"^\s*(?P<name>[A-Za-z_][A-Za-z0-9_:~]*)\s*"
        r"\((?P<params>[^)]*)\)\s*"
        r"(?:(?:const|override|final|noexcept)\s*)*"
        r"(?:=\s*(?:0|default|delete)\s*)?"
        r"(?:;|\{|:|}\s*;?)\s*$"
    )

    # Build multi-line candidate signatures.
    candidates: list[tuple[int, str]] = []
    i = 0
    n = len(lines)
    depth = 0
    class_depth_stack: list[int] = []
    class_pending = False

    while i < n:
        raw = lines[i]
        line = raw.strip()
        if not line or line.startswith("#"):
            depth += raw.count("{") - raw.count("}")
            while class_depth_stack and depth < class_depth_stack[-1]:
                class_depth_stack.pop()
            i += 1
            continue

        if re.match(r"^\s*(class|struct)\b", raw) and not raw.rstrip().endswith(";"):
            # class definition starts now or on a following line.
            if "{" in raw:
                class_depth_stack.append(depth + raw.count("{"))
            else:
                class_pending = True

        if class_pending and "{" in raw:
            class_depth_stack.append(depth + raw.count("{"))
            class_pending = False

        in_class_scope = bool(class_depth_stack)
        allowed_scope = depth == 0 or in_class_scope

        if allowed_scope and "(" in line:
            # probable signature starts
            if re.match(r"^\s*(?:inline|virtual|static|constexpr|friend|explicit|extern|template|[A-Za-z_~])", raw):
                start = i + 1
                sig = raw.strip()
                # Multi-line declaration with return type above, e.g.
                #   virtual MinimumSeed
                #   operator()(...) const = 0;
                if re.match(r"^\s*operator", raw) and i > 0:
                    prev = lines[i - 1].strip()
                    if prev and "(" not in prev and not re.search(r"[;{}]$", prev):
                        sig = prev + " " + sig
                        start = i
                j = i
                while j + 1 < n and ")" not in sig:
                    j += 1
                    sig += " " + lines[j].strip()
                # consume trailing qualifiers / symbols if needed
                while j + 1 < n and not re.search(r"(;|\{|:|}\s*;?)\s*$", sig):
                    if re.match(r"^\s*(?:const|override|final|noexcept|->|\{|:|;|=)", lines[j + 1]):
                        j += 1
                        sig += " " + lines[j].strip()
                    else:
                        break
                candidates.append((start, sig))
                # advance depth across consumed lines
                for k in range(i, j + 1):
                    depth += lines[k].count("{") - lines[k].count("}")
                    while class_depth_stack and depth < class_depth_stack[-1]:
                        class_depth_stack.pop()
                i = j + 1
                continue

        depth += raw.count("{") - raw.count("}")
        while class_depth_stack and depth < class_depth_stack[-1]:
            class_depth_stack.pop()
        i += 1

    symbols: list[Symbol] = []
    seen_line_name: set[tuple[int, str]] = set()
    for line_no, sig in candidates:
        sig_trim = sig.strip()
        sig_match = re.sub(r"\{.*$", "{", sig_trim)

        if is_source and "{" not in sig_match and ":" not in sig_match:
            # In .cxx files keep definitions (and ctor init-list starts), skip declarations/initializations.
            continue

        m = func_pat.match(sig_match)
        if not m:
            m = ctor_pat.match(sig_match)
        if not m:
            continue
        raw_name = m.group("name").strip()
        short_name = raw_name.split("::")[-1].strip()
        if short_name in CPP_KEYWORDS:
            continue
        if short_name.startswith("~"):
            # Keep destructor only if it belongs to the target class basename.
            if short_name[1:] != basename:
                continue
        if not re.match(r"^[A-Za-z_]|^operator", short_name):
            continue
        # For constructor pattern, keep only if it is really this class ctor/dtor.
        if m.re is ctor_pat and short_name not in {basename, f"~{basename}"}:
            continue

        key = (line_no, short_name)
        if key in seen_line_name:
            continue
        seen_line_name.add(key)
        symbols.append(Symbol(name=short_name, line=line_no, param_count=estimate_param_count(m.group("params"))))

    # Deduplicate per basename by (name, arity), preferring earliest line.
    by_key: dict[tuple[str, int], Symbol] = {}
    for s in symbols:
        key = (s.name, s.param_count)
        old = by_key.get(key)
        if old is None or s.line < old.line:
            by_key[key] = s

    return sorted(by_key.values(), key=lambda s: (s.line, s.name))


def parse_replaces_mirrors_mapping() -> dict[str, set[str]]:
    mapping: dict[str, set[str]] = defaultdict(set)
    for file in sorted((REPO_ROOT / "src").rglob("*.rs")):
        head = "\n".join(file.read_text(errors="replace").splitlines()[:40])
        for m in re.finditer(r"(Replaces|Mirrors)\s+([^\n]+)", head):
            desc = m.group(2)
            for token in re.findall(r"([A-Za-z0-9_./]+\.(?:h|hpp|cxx))", desc):
                base = token.split("/")[-1].rsplit(".", 1)[0]
                mapping[base].add(str(file.relative_to(REPO_ROOT)))
    return mapping


def normalize_symbol(name: str) -> str:
    return "".join(ch for ch in name.lower() if ch.isalnum())


def alternative_upstream_names(name: str, basename: str) -> list[str]:
    n = name.strip()
    alternatives = [n]

    if n.startswith("Get") and len(n) > 3:
        alternatives.append(n[3:])
    if n.startswith("Is") and len(n) > 2:
        alternatives.append(n[2:])
    if n.startswith("Has") and len(n) > 3:
        alternatives.append(n[3:])
    if n == basename:
        alternatives.extend(["new", "default"])
    if n.startswith("~") and n[1:] == basename:
        alternatives.extend(["drop"])
    if n.startswith("operator"):
        alternatives.extend(["call", "value"])

    base_aliases = SYMBOL_ALIASES.get(basename, {})
    if n in base_aliases:
        alternatives.extend(base_aliases[n])

    # preserve order and uniqueness
    out: list[str] = []
    seen: set[str] = set()
    for a in alternatives:
        if a not in seen:
            seen.add(a)
            out.append(a)
    return out


def extract_rust_symbols() -> tuple[list[RustSymbol], dict[str, list[RustSymbol]], dict[str, list[RustSymbol]]]:
    symbols: list[RustSymbol] = []
    by_file: dict[str, list[RustSymbol]] = defaultdict(list)
    by_norm: dict[str, list[RustSymbol]] = defaultdict(list)

    for file in sorted((REPO_ROOT / "src").rglob("*.rs")):
        rel = str(file.relative_to(REPO_ROOT))
        content = file.read_text(errors="replace")
        # Skip in-file test module declarations by cutting at first cfg(test).
        main_part = content.split("#[cfg(test)]", 1)[0]
        pattern = re.compile(r"\bfn\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(")
        for m in pattern.finditer(main_part):
            name = m.group(1)
            line = main_part.count("\n", 0, m.start()) + 1
            norm = normalize_symbol(name)
            rs = RustSymbol(name=name, line=line, file=rel, norm=norm)
            symbols.append(rs)
            by_file[rel].append(rs)
            by_norm[norm].append(rs)
    return symbols, by_file, by_norm


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Generate function-level parity report artifacts")
    parser.add_argument(
        "--repo",
        default=DEFAULT_UPSTREAM_REPO,
        help=f"Upstream GitHub repo slug or URL (default: {DEFAULT_UPSTREAM_REPO})",
    )
    parser.add_argument(
        "--subdir",
        default=DEFAULT_UPSTREAM_SUBDIR,
        help=f"Subdirectory within upstream repo (default: {DEFAULT_UPSTREAM_SUBDIR})",
    )
    parser.add_argument(
        "--tag",
        default=DEFAULT_UPSTREAM_TAG,
        help=f"Upstream tag to resolve when --commit is not provided (default: {DEFAULT_UPSTREAM_TAG})",
    )
    parser.add_argument(
        "--commit",
        help="Upstream commit SHA or ref (overrides --tag)",
    )
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    repo_slug = normalize_repo(args.repo)
    ref = args.commit or args.tag
    commit = resolve_git_ref(repo_slug, ref)
    subdir = args.subdir.strip("/")
    upstream_files = parse_inventory_port_files()
    file_groups: dict[str, list[UpstreamFile]] = defaultdict(list)
    for f in upstream_files:
        file_groups[f.basename].append(f)

    replaces_map = parse_replaces_mirrors_mapping()
    _, rust_by_file, rust_by_norm = extract_rust_symbols()

    rows: list[dict[str, str]] = []

    for basename in sorted(file_groups):
        # Candidate Rust files from manual map + doc comments.
        candidates = set(MANUAL_BASE_TO_RUST.get(basename, []))
        candidates.update(replaces_map.get(basename, set()))
        candidates = {c for c in candidates if (REPO_ROOT / c).exists()}

        # Extract and dedupe symbols across all upstream files for this basename.
        extracted: dict[tuple[str, int], tuple[Symbol, str]] = {}
        fetch_errors: list[str] = []
        for uf in sorted(file_groups[basename], key=lambda u: u.path):
            try:
                local_path = download_upstream_file(repo_slug, subdir, commit, uf.path)
            except RuntimeError as e:
                fetch_errors.append(f"{uf.path}: {e}")
                continue
            syms = extract_cpp_symbols(local_path, basename)
            for sym in syms:
                key = (sym.name, sym.param_count)
                old = extracted.get(key)
                if old is None:
                    extracted[key] = (sym, uf.path)
                    continue
                old_sym, old_path = old
                # Prefer source definitions over headers if available.
                old_is_src = old_path.startswith("src/")
                new_is_src = uf.path.startswith("src/")
                if new_is_src and not old_is_src:
                    extracted[key] = (sym, uf.path)
                elif new_is_src == old_is_src and sym.line < old_sym.line:
                    extracted[key] = (sym, uf.path)

        if not extracted:
            rows.append(
                {
                    "upstream_repo": repo_slug,
                    "upstream_subdir": subdir,
                    "upstream_ref": ref,
                    "upstream_commit": commit,
                    "upstream_file": ";".join(sorted({f.path for f in file_groups[basename]})),
                    "upstream_symbol": "<no_symbol_extracted>",
                    "upstream_line": "",
                    "rust_file": ";".join(sorted(candidates)),
                    "rust_symbol": "",
                    "rust_line": "",
                    "status": "needs-review",
                    "rationale": (
                        "unable to extract symbols with heuristic parser"
                        if not fetch_errors
                        else f"source unavailable: {fetch_errors[0]}"
                    ),
                }
            )
            continue

        for key in sorted(extracted, key=lambda k: (k[0].lower(), k[1])):
            sym, source_path = extracted[key]
            alts = alternative_upstream_names(sym.name, basename)
            alt_norms = [normalize_symbol(a) for a in alts if a]

            candidate_matches: list[RustSymbol] = []
            # First pass: mapped files only.
            if candidates:
                for rust_file in sorted(candidates):
                    for rs in rust_by_file.get(rust_file, []):
                        if rs.norm in alt_norms:
                            candidate_matches.append(rs)

            # Second pass: global search only when no candidate file mapping exists.
            if not candidate_matches and not candidates:
                seen = set()
                for n in alt_norms:
                    for rs in rust_by_norm.get(n, []):
                        ident = (rs.file, rs.line, rs.name)
                        if ident not in seen:
                            seen.add(ident)
                            candidate_matches.append(rs)

            status = "needs-review"
            rationale = "symbol match ambiguous"
            rust_file = ""
            rust_symbol = ""
            rust_line = ""

            is_ctor = sym.name == basename
            is_dtor = sym.name == f"~{basename}"
            is_operator = sym.name.startswith("operator")

            if candidate_matches and len(candidate_matches) == 1:
                match = candidate_matches[0]
                rust_file = match.file
                rust_symbol = match.name
                rust_line = str(match.line)
                status = "implemented"
                rationale = "symbol name match"
            elif candidate_matches and len(candidate_matches) > 1:
                # Keep one evidence line, but mark for review.
                match = sorted(candidate_matches, key=lambda m: (m.file, m.line, m.name))[0]
                rust_file = match.file
                rust_symbol = match.name
                rust_line = str(match.line)
                status = "needs-review"
                rationale = "multiple Rust symbol candidates"
            else:
                if is_ctor or is_dtor or is_operator:
                    status = "intentionally-skipped"
                    rationale = "constructor/destructor/operator handled idiomatically in Rust"
                    # provide a best-effort constructor evidence if available
                    for rust_file_candidate in sorted(candidates):
                        ctor_candidates = [rs for rs in rust_by_file.get(rust_file_candidate, []) if rs.name in {"new", "default"}]
                        if ctor_candidates:
                            rs = ctor_candidates[0]
                            rust_file = rs.file
                            rust_symbol = rs.name
                            rust_line = str(rs.line)
                            break
                elif basename in ARCHITECTURAL_BASENAMES:
                    status = "needs-review"
                    rationale = "architectural refactor; no 1:1 symbol match"
                elif candidates:
                    status = "missing"
                    rationale = "no symbol match in mapped Rust files"
                else:
                    status = "needs-review"
                    rationale = "no mapped Rust file for upstream basename"

            rows.append(
                {
                    "upstream_repo": repo_slug,
                    "upstream_subdir": subdir,
                    "upstream_ref": ref,
                    "upstream_commit": commit,
                    "upstream_file": source_path,
                    "upstream_symbol": sym.name,
                    "upstream_line": str(sym.line),
                    "rust_file": rust_file,
                    "rust_symbol": rust_symbol,
                    "rust_line": rust_line,
                    "status": status,
                    "rationale": rationale,
                }
            )

    REPORT_DIR.mkdir(parents=True, exist_ok=True)

    csv_path = REPORT_DIR / "functions.csv"
    with csv_path.open("w", newline="") as f:
        writer = csv.DictWriter(
            f,
            fieldnames=[
                "upstream_repo",
                "upstream_subdir",
                "upstream_ref",
                "upstream_commit",
                "upstream_file",
                "upstream_symbol",
                "upstream_line",
                "rust_file",
                "rust_symbol",
                "rust_line",
                "status",
                "rationale",
            ],
        )
        writer.writeheader()
        writer.writerows(rows)

    missing_csv = REPORT_DIR / "missing.csv"
    with missing_csv.open("w", newline="") as f:
        writer = csv.DictWriter(
            f,
            fieldnames=[
                "upstream_repo",
                "upstream_subdir",
                "upstream_ref",
                "upstream_commit",
                "upstream_file",
                "upstream_symbol",
                "upstream_line",
                "rust_file",
                "rust_symbol",
                "rust_line",
                "status",
                "rationale",
            ],
        )
        writer.writeheader()
        for row in rows:
            if row["status"] == "missing":
                writer.writerow(row)

    review_csv = REPORT_DIR / "needs_review.csv"
    with review_csv.open("w", newline="") as f:
        writer = csv.DictWriter(
            f,
            fieldnames=[
                "upstream_repo",
                "upstream_subdir",
                "upstream_ref",
                "upstream_commit",
                "upstream_file",
                "upstream_symbol",
                "upstream_line",
                "rust_file",
                "rust_symbol",
                "rust_line",
                "status",
                "rationale",
            ],
        )
        writer.writeheader()
        for row in rows:
            if row["status"] == "needs-review":
                writer.writerow(row)

    by_status: dict[str, list[dict[str, str]]] = defaultdict(list)
    by_file_missing: dict[str, int] = defaultdict(int)
    by_file_review: dict[str, int] = defaultdict(int)
    for row in rows:
        by_status[row["status"]].append(row)
        if row["status"] == "missing":
            by_file_missing[row["upstream_file"]] += 1
        if row["status"] == "needs-review":
            by_file_review[row["upstream_file"]] += 1

    total = len(rows)
    implemented = len(by_status["implemented"])
    missing = len(by_status["missing"])
    review = len(by_status["needs-review"])
    skipped = len(by_status["intentionally-skipped"])

    missing_top = sorted(by_file_missing.items(), key=lambda x: (-x[1], x[0]))[:20]
    review_top = sorted(by_file_review.items(), key=lambda x: (-x[1], x[0]))[:20]

    gaps_md = REPORT_DIR / "gaps.md"
    lines = [
        "# Function Parity Gaps",
        "",
        f"Upstream repo: `{repo_slug}`",
        f"Upstream subdir: `{subdir}`",
        f"Upstream ref: `{ref}`",
        f"Upstream commit: `{commit}`",
        "",
        "## Summary",
        "",
        f"- Total upstream symbols in scope: **{total}**",
        f"- `implemented`: **{implemented}**",
        f"- `missing`: **{missing}**",
        f"- `needs-review`: **{review}**",
        f"- `intentionally-skipped`: **{skipped}**",
        "",
        "## Top Files by `missing` Symbols",
        "",
    ]
    if missing_top:
        lines.extend([f"- `{path}`: {count}" for path, count in missing_top])
    else:
        lines.append("- None")

    lines.extend(["", "## Top Files by `needs-review` Symbols", ""])
    if review_top:
        lines.extend([f"- `{path}`: {count}" for path, count in review_top])
    else:
        lines.append("- None")

    lines.extend(["", "## Notes", ""])
    lines.extend(
        textwrap.dedent(
            """
            - Symbol extraction is heuristic (regex-based), not a full C++ parser.
            - `intentionally-skipped` currently captures constructor/destructor/operator-style symbols that map to Rust idioms.
            - `needs-review` includes architectural refactors where strict 1:1 symbol naming is not expected.
            - Use `reports/parity/functions.csv` as the source of truth for triage and manual confirmation.
            """
        )
        .strip()
        .splitlines()
    )
    gaps_md.write_text("\n".join(lines) + "\n")

    print(f"Wrote {csv_path.relative_to(REPO_ROOT)} ({len(rows)} rows)")
    print(f"Wrote {gaps_md.relative_to(REPO_ROOT)}")
    print(f"Wrote {missing_csv.relative_to(REPO_ROOT)}")
    print(f"Wrote {review_csv.relative_to(REPO_ROOT)}")
    print(f"Status counts: implemented={implemented} missing={missing} needs-review={review} intentionally-skipped={skipped}")
    return 0


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except Exception as exc:
        print(f"error: {exc}", file=sys.stderr)
        raise