File size: 26,366 Bytes
3e363e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Load bounded, high-value public-world graph sources.

These loaders deliberately preserve source semantics.  ATT&CK and NVD edges are
security knowledge, not causal claims; GeoNames edges are geographic structure;
and Exoplanet Archive rows are astronomical observations.  Keeping the source
kind explicit prevents a mixed corpus from teaching the model that every edge
means cause and effect.
"""

from __future__ import annotations

import csv
import gzip
import json
import re
import zipfile
from collections.abc import Mapping
from pathlib import Path
from urllib.parse import unquote

from graph.graph import EdgeRecord
from graph.real.causal import _make_graph, _node_id
from graph.real.corpus import RealGraph, SourceMetadata, relation_phrase


def _short(value: object, limit: int = 4000) -> str:
    return " ".join(str(value or "").split())[:limit]


def _stix_identifier(value: str) -> str:
    return "stix:" + re.sub(r"[^A-Za-z0-9_.:-]+", "_", value)


def load_mitre_attack_stix(
    path: str | Path,
    source: SourceMetadata,
    *,
    max_objects: int | None = None,
) -> RealGraph:
    """Load named ATT&CK STIX objects and their relationships."""
    payload = json.loads(Path(path).read_text(encoding="utf-8"))
    objects = payload.get("objects", [])
    if not isinstance(objects, list):
        raise TypeError("ATT&CK STIX bundle has no object list")

    selected: dict[str, Mapping[str, object]] = {}
    for item in objects:
        if not isinstance(item, dict):
            continue
        object_id = str(item.get("id", ""))
        object_type = str(item.get("type", ""))
        if not object_id or object_type in {"relationship", "marking-definition"}:
            continue
        if not item.get("name") and object_type not in {"identity"}:
            continue
        selected[object_id] = item
        if max_objects is not None and len(selected) >= max_objects:
            break

    nodes: dict[str, str] = {}
    labels: dict[str, str] = {}
    attributes: dict[str, dict[str, str]] = {}
    for object_id, item in selected.items():
        node_id = _stix_identifier(object_id)
        object_type = str(item.get("type", "stix_object"))
        nodes[node_id] = object_type
        labels[node_id] = _short(item.get("name") or object_id, 240)
        external_ids = []
        references = item.get("external_references", ())
        if not isinstance(references, list):
            references = []
        for reference in references:
            if isinstance(reference, dict) and reference.get("external_id"):
                external_ids.append(str(reference["external_id"]))
        attributes[node_id] = {
            "stix_type": object_type,
            "description": _short(item.get("description")),
            "external_ids": ", ".join(external_ids[:16]),
            "evidence_kind": "mitre_attack_stix_object",
        }

    edges: list[EdgeRecord] = []
    relation_ids: set[str] = set()
    for item in objects:
        if not isinstance(item, dict) or item.get("type") != "relationship":
            continue
        left = str(item.get("source_ref", ""))
        right = str(item.get("target_ref", ""))
        if left not in selected or right not in selected or left == right:
            continue
        relation = (
            re.sub(
                r"[^a-z0-9_]+",
                "_",
                str(item.get("relationship_type", "related_to")).lower(),
            ).strip("_")
            or "related_to"
        )
        left_id = _stix_identifier(left)
        right_id = _stix_identifier(right)
        relation_ids.add(relation)
        edges.append(
            EdgeRecord(
                left_id,
                relation,
                right_id,
                nodes[left_id],
                nodes[right_id],
                relation_phrase(relation),
                {
                    "evidence_kind": "mitre_attack_stix_relationship",
                    "relationship_id": str(item.get("id", "")),
                    "description": _short(item.get("description"), 1000),
                },
            )
        )
    return _make_graph(
        source,
        nodes,
        edges,
        labels=labels,
        node_attributes=attributes,
        relation_ids=relation_ids or {"related_to"},
    )


def _read_json(path: str | Path) -> Mapping[str, object]:
    file_path = Path(path)
    opener = gzip.open if file_path.name.endswith(".gz") else open
    with opener(file_path, "rt", encoding="utf-8") as handle:  # type: ignore[arg-type]
        payload = json.load(handle)
    if not isinstance(payload, dict):
        raise TypeError(f"expected JSON object in {file_path}")
    return payload


def _cpe_label(value: str) -> str:
    decoded = unquote(value)
    parts = decoded.split(":")
    if len(parts) >= 5:
        return ":".join(parts[:5])
    return decoded[:240]


def load_nvd_json(
    path: str | Path,
    source: SourceMetadata,
    *,
    max_records: int | None = None,
) -> RealGraph:
    """Load NVD 2.0 CVE records, weaknesses, products, scores, and references."""
    payload = _read_json(path)
    raw_records = payload.get("vulnerabilities", payload.get("CVE_Items", ()))
    if not isinstance(raw_records, list):
        raise TypeError("NVD feed has no vulnerabilities list")
    records = raw_records if max_records is None else raw_records[:max_records]

    nodes: dict[str, str] = {}
    labels: dict[str, str] = {}
    attributes: dict[str, dict[str, str]] = {}
    edges: list[EdgeRecord] = []
    relation_ids = {"has_weakness", "affects_product", "has_reference", "has_severity"}

    def add_node(node_id: str, node_type: str, label: str, **attrs: str) -> None:
        nodes[node_id] = node_type
        labels[node_id] = _short(label, 240)
        if attrs:
            attributes.setdefault(node_id, {}).update(attrs)

    def add_edge(
        left: str,
        relation: str,
        right: str,
        left_type: str,
        right_type: str,
        **attrs: str,
    ) -> None:
        edges.append(
            EdgeRecord(
                left,
                relation,
                right,
                left_type,
                right_type,
                relation_phrase(relation),
                {"evidence_kind": "nvd_cve_record", **attrs},
            )
        )

    for index, item in enumerate(records):
        if not isinstance(item, dict):
            continue
        cve = item.get("cve", item)
        if not isinstance(cve, dict):
            continue
        cve_id = str(
            cve.get("id")
            or cve.get("CVE_data_meta", {}).get("ID", f"CVE-unknown-{index}")
        )
        cve_node = f"nvd:cve:{cve_id}"
        descriptions = cve.get(
            "descriptions", cve.get("description", {}).get("description_data", ())
        )
        description = ""
        if isinstance(descriptions, list):
            for entry in descriptions:
                if isinstance(entry, dict) and (
                    not description or entry.get("lang") == "en"
                ):
                    description = _short(entry.get("value"))
                    if entry.get("lang") == "en":
                        break
        add_node(
            cve_node,
            "cve",
            cve_id,
            description=description,
            published=_short(cve.get("published")),
            last_modified=_short(cve.get("lastModified")),
        )

        weaknesses = cve.get("weaknesses", ())
        for weakness in weaknesses if isinstance(weaknesses, list) else ():
            if not isinstance(weakness, dict):
                continue
            descriptions = weakness.get("description", ())
            for entry in descriptions if isinstance(descriptions, list) else ():
                if not isinstance(entry, dict):
                    continue
                value = str(entry.get("value", "")).strip()
                if not value:
                    continue
                weakness_node = _node_id("nvd_cwe", value)
                add_node(weakness_node, "weakness", value)
                add_edge(cve_node, "has_weakness", weakness_node, "cve", "weakness")

        configurations = cve.get("configurations", ())
        stack: list[object] = (
            list(configurations) if isinstance(configurations, list) else []
        )
        while stack:
            current = stack.pop()
            if not isinstance(current, dict):
                continue
            stack.extend(
                current.get("children", ())
                if isinstance(current.get("children"), list)
                else ()
            )
            stack.extend(
                current.get("nodes", ())
                if isinstance(current.get("nodes"), list)
                else ()
            )
            matches = current.get("cpeMatch", ())
            for match in matches if isinstance(matches, list) else ():
                if not isinstance(match, dict):
                    continue
                cpe = str(match.get("criteria") or match.get("cpe23Uri") or "").strip()
                if not cpe:
                    continue
                product_node = _node_id("nvd_product", cpe)
                add_node(product_node, "affected_product", _cpe_label(cpe), cpe=cpe)
                add_edge(
                    cve_node, "affects_product", product_node, "cve", "affected_product"
                )

        references = cve.get("references", ())
        if isinstance(references, dict):
            references = references.get("reference_data", ())
        for reference in references if isinstance(references, list) else ():
            if not isinstance(reference, dict):
                continue
            url = str(reference.get("url", "")).strip()
            if not url:
                continue
            reference_node = _node_id("nvd_reference", url)
            add_node(
                reference_node,
                "reference",
                url,
                source=str(reference.get("source", "")),
            )
            add_edge(cve_node, "has_reference", reference_node, "cve", "reference")

        metrics = cve.get("metrics", {})
        if not isinstance(metrics, dict):
            metrics = {}
        score = severity = ""
        for metric_name in (
            "cvssMetricV40",
            "cvssMetricV31",
            "cvssMetricV30",
            "cvssMetricV2",
        ):
            entries = metrics.get(metric_name, ())
            if not isinstance(entries, list):
                continue
            for entry in entries:
                if not isinstance(entry, dict):
                    continue
                cvss = entry.get("cvssData", entry)
                if isinstance(cvss, dict):
                    score = str(cvss.get("baseScore", ""))
                    severity = str(cvss.get("baseSeverity", ""))
                if score or severity:
                    break
            if score or severity:
                break
        if score or severity:
            severity_node = _node_id("nvd_severity", f"{severity}-{score}")
            add_node(
                severity_node,
                "severity",
                f"{severity} {score}".strip(),
                score=score,
                severity=severity,
            )
            add_edge(cve_node, "has_severity", severity_node, "cve", "severity")

    return _make_graph(
        source,
        nodes,
        edges,
        labels=labels,
        node_attributes=attributes,
        relation_ids=relation_ids,
    )


def load_nvd_cpe_dictionary(
    path: str | Path,
    source: SourceMetadata,
    *,
    max_records: int | None = None,
) -> RealGraph:
    """Load the NVD CPE dictionary as a product/vendor/version ontology.

    CPE names are software/product identifiers, not vulnerability assertions.
    The adapter therefore emits product taxonomy edges and keeps the NVD CPE
    identifier in node attributes for auditability.
    """
    payload = _read_json(path)
    raw_products = payload.get("products", ())
    if not isinstance(raw_products, list):
        raise TypeError("NVD CPE feed has no products list")
    records = raw_products if max_records is None else raw_products[:max_records]
    nodes: dict[str, str] = {}
    labels: dict[str, str] = {}
    attributes: dict[str, dict[str, str]] = {}
    edges: list[EdgeRecord] = []
    relation_ids = {"has_vendor", "has_product", "has_version"}

    def add_node(node_id: str, node_type: str, label: str, **attrs: str) -> None:
        nodes[node_id] = node_type
        labels[node_id] = _short(label, 240)
        attributes.setdefault(node_id, {}).update(attrs)

    for item in records:
        if not isinstance(item, dict):
            continue
        cpe = item.get("cpe", item)
        if not isinstance(cpe, dict):
            continue
        uri = str(cpe.get("cpe23Uri") or cpe.get("cpeName") or "").strip()
        if not uri:
            continue
        parts = uri.split(":")
        if len(parts) < 6:
            continue
        vendor = unquote(parts[3]).replace("_", " ") or "unknown"
        product = unquote(parts[4]).replace("_", " ") or "unknown"
        version = unquote(parts[5]).replace("_", " ") or "unspecified"
        cpe_id = _node_id("nvd_cpe", uri)
        vendor_id = _node_id("nvd_vendor", vendor)
        product_id = _node_id("nvd_product_family", f"{vendor} {product}")
        version_id = _node_id("nvd_version", f"{vendor} {product} {version}")
        title = ""
        titles = cpe.get("titles", ())
        if isinstance(titles, list):
            for value in titles:
                if isinstance(value, dict) and value.get("title"):
                    title = str(value["title"])
                    if value.get("lang") in {"en", "en-us"}:
                        break
        add_node(cpe_id, "cpe_name", title or _cpe_label(uri), cpe_uri=uri)
        add_node(vendor_id, "vendor", vendor)
        add_node(product_id, "product_family", f"{vendor} {product}")
        add_node(version_id, "product_version", f"{product} {version}")
        edge_attrs = {"evidence_kind": "nvd_cpe_dictionary"}
        for left, relation, right, left_type, right_type in (
            (cpe_id, "has_vendor", vendor_id, "cpe_name", "vendor"),
            (cpe_id, "has_product", product_id, "cpe_name", "product_family"),
            (cpe_id, "has_version", version_id, "cpe_name", "product_version"),
        ):
            edges.append(
                EdgeRecord(
                    left,
                    relation,
                    right,
                    left_type,
                    right_type,
                    relation_phrase(relation),
                    edge_attrs,
                )
            )
    return _make_graph(
        source,
        nodes,
        edges,
        labels=labels,
        node_attributes=attributes,
        relation_ids=relation_ids,
    )


def load_cisa_kev(path: str | Path, source: SourceMetadata) -> RealGraph:
    """Load CISA's Known Exploited Vulnerabilities catalog."""
    payload = _read_json(path)
    records = payload.get("vulnerabilities", ())
    if not isinstance(records, list):
        raise TypeError("CISA KEV feed has no vulnerabilities list")
    nodes: dict[str, str] = {}
    labels: dict[str, str] = {}
    attributes: dict[str, dict[str, str]] = {}
    edges: list[EdgeRecord] = []
    relation_ids = {"affects_product", "involves_vendor", "has_remediation", "due_by"}
    for item in records:
        if not isinstance(item, dict):
            continue
        cve = str(item.get("cveID", "")).strip()
        if not cve:
            continue
        cve_node = f"kev:cve:{cve}"
        nodes[cve_node] = "exploited_vulnerability"
        labels[cve_node] = cve
        attributes[cve_node] = {
            "vulnerability_name": _short(item.get("vulnerabilityName")),
            "description": _short(item.get("shortDescription")),
            "date_added": _short(item.get("dateAdded")),
        }
        for field, relation, node_type in (
            ("vendorProject", "involves_vendor", "vendor"),
            ("product", "affects_product", "product"),
            ("requiredAction", "has_remediation", "remediation"),
            ("dueDate", "due_by", "date"),
        ):
            value = str(item.get(field, "")).strip()
            if not value:
                continue
            target = _node_id("kev_" + node_type, value)
            nodes[target] = node_type
            labels[target] = _short(value, 240)
            edges.append(
                EdgeRecord(
                    cve_node,
                    relation,
                    target,
                    "exploited_vulnerability",
                    node_type,
                    relation_phrase(relation),
                    {"evidence_kind": "cisa_known_exploited_vulnerability"},
                )
            )
    return _make_graph(
        source,
        nodes,
        edges,
        labels=labels,
        node_attributes=attributes,
        relation_ids=relation_ids,
    )


def _country_info(path: str | Path | None) -> dict[str, tuple[str, str]]:
    if path is None:
        return {}
    result: dict[str, tuple[str, str]] = {}
    with Path(path).open(encoding="utf-8") as handle:
        for line in handle:
            if not line.strip() or line.startswith("#"):
                continue
            fields = line.rstrip("\n").split("\t")
            if len(fields) > 8:
                result[fields[0]] = (fields[4], fields[8])
    return result


def load_geonames_cities(
    archive: str | Path,
    source: SourceMetadata,
    *,
    country_info: str | Path | None = None,
    max_rows: int | None = None,
) -> RealGraph:
    """Load a bounded GeoNames city/country/region/time-zone graph."""
    countries = _country_info(country_info)
    nodes: dict[str, str] = {}
    labels: dict[str, str] = {}
    attributes: dict[str, dict[str, str]] = {}
    edges: list[EdgeRecord] = []
    relation_ids = {"in_country", "in_region", "in_timezone", "within_continent"}
    with zipfile.ZipFile(archive) as handle:
        member = next(name for name in handle.namelist() if name.endswith(".txt"))
        with handle.open(member) as binary:
            reader = csv.reader(
                (line.decode("utf-8", "replace") for line in binary), delimiter="\t"
            )
            for index, fields in enumerate(reader):
                if max_rows is not None and index >= max_rows:
                    break
                if len(fields) < 18:
                    continue
                geoname_id, name, ascii_name = fields[:3]
                country_code, admin1, timezone = fields[8], fields[10], fields[17]
                if not geoname_id or not country_code:
                    continue
                city = f"geonames:city:{geoname_id}"
                country = f"geonames:country:{country_code}"
                region = _node_id("geonames_region", f"{country_code}:{admin1}")
                tz = _node_id("geonames_timezone", timezone) if timezone else ""
                nodes[city] = "city"
                labels[city] = ascii_name or name
                attributes[city] = {
                    "name": _short(name, 240),
                    "latitude": fields[4],
                    "longitude": fields[5],
                    "population": fields[14],
                    "feature_code": fields[7],
                }
                nodes[country] = "country"
                labels[country] = (
                    countries.get(country_code, (country_code, ""))[0] or country_code
                )
                edges.append(
                    EdgeRecord(
                        city,
                        "in_country",
                        country,
                        "city",
                        "country",
                        relation_phrase("in_country"),
                        {"evidence_kind": "geonames_city"},
                    )
                )
                if admin1:
                    nodes[region] = "admin_region"
                    labels[region] = f"{country_code} {admin1}"
                    edges.append(
                        EdgeRecord(
                            city,
                            "in_region",
                            region,
                            "city",
                            "admin_region",
                            relation_phrase("in_region"),
                            {"evidence_kind": "geonames_city"},
                        )
                    )
                if tz:
                    nodes[tz] = "timezone"
                    labels[tz] = timezone
                    edges.append(
                        EdgeRecord(
                            city,
                            "in_timezone",
                            tz,
                            "city",
                            "timezone",
                            relation_phrase("in_timezone"),
                            {"evidence_kind": "geonames_city"},
                        )
                    )
                continent = countries.get(country_code, ("", ""))[1]
                if continent:
                    continent_node = _node_id("geonames_continent", continent)
                    nodes[continent_node] = "continent"
                    labels[continent_node] = continent
                    edges.append(
                        EdgeRecord(
                            country,
                            "within_continent",
                            continent_node,
                            "country",
                            "continent",
                            relation_phrase("within_continent"),
                            {"evidence_kind": "geonames_country_info"},
                        )
                    )
    return _make_graph(
        source,
        nodes,
        edges,
        labels=labels,
        node_attributes=attributes,
        relation_ids=relation_ids,
    )


def load_exoplanet_csv(
    path: str | Path,
    source: SourceMetadata,
    *,
    max_rows: int | None = None,
) -> RealGraph:
    """Load confirmed-planet rows from NASA's Exoplanet Archive."""
    nodes: dict[str, str] = {}
    labels: dict[str, str] = {}
    attributes: dict[str, dict[str, str]] = {}
    edges: list[EdgeRecord] = []
    relation_ids = {"orbits", "discovered_by", "discovered_in", "has_measurement"}
    with Path(path).open(encoding="utf-8", newline="") as handle:
        reader = csv.DictReader(handle)
        for index, row in enumerate(reader):
            if max_rows is not None and index >= max_rows:
                break
            planet_name = str(row.get("pl_name", "")).strip()
            host_name = str(row.get("hostname", "")).strip()
            if not planet_name or not host_name:
                continue
            planet = _node_id("exoplanet", planet_name)
            host = _node_id("exostar", host_name)
            nodes[planet] = "exoplanet"
            labels[planet] = planet_name
            nodes[host] = "host_star"
            labels[host] = host_name
            attributes[planet] = {
                "discovery_method": _short(row.get("discoverymethod")),
                "discovery_year": _short(row.get("disc_year")),
                "orbital_period_days": _short(row.get("pl_orbper")),
                "radius_earth": _short(row.get("pl_rade")),
                "mass_earth": _short(row.get("pl_bmasse")),
            }
            edges.append(
                EdgeRecord(
                    planet,
                    "orbits",
                    host,
                    "exoplanet",
                    "host_star",
                    relation_phrase("orbits"),
                    {"evidence_kind": "nasa_exoplanet_archive"},
                )
            )
            method = str(row.get("discoverymethod", "")).strip()
            if method:
                method_node = _node_id("exoplanet_method", method)
                nodes[method_node] = "discovery_method"
                labels[method_node] = method
                edges.append(
                    EdgeRecord(
                        planet,
                        "discovered_by",
                        method_node,
                        "exoplanet",
                        "discovery_method",
                        relation_phrase("discovered_by"),
                        {"evidence_kind": "nasa_exoplanet_archive"},
                    )
                )
            year = str(row.get("disc_year", "")).strip()
            if year:
                year_node = f"exoplanet:year:{year}"
                nodes[year_node] = "year"
                labels[year_node] = year
                edges.append(
                    EdgeRecord(
                        planet,
                        "discovered_in",
                        year_node,
                        "exoplanet",
                        "year",
                        relation_phrase("discovered_in"),
                        {"evidence_kind": "nasa_exoplanet_archive"},
                    )
                )
            for field, value in (
                ("pl_rade", row.get("pl_rade")),
                ("pl_bmasse", row.get("pl_bmasse")),
                ("st_teff", row.get("st_teff")),
            ):
                text = str(value or "").strip()
                if not text:
                    continue
                measure_node = _node_id("exoplanet_measure", f"{field}:{text}")
                nodes[measure_node] = "measurement"
                labels[measure_node] = f"{field} {text}"
                edges.append(
                    EdgeRecord(
                        planet,
                        "has_measurement",
                        measure_node,
                        "exoplanet",
                        "measurement",
                        relation_phrase("has_measurement"),
                        {"evidence_kind": "nasa_exoplanet_archive", "field": field},
                    )
                )
    return _make_graph(
        source,
        nodes,
        edges,
        labels=labels,
        node_attributes=attributes,
        relation_ids=relation_ids,
    )