File size: 23,774 Bytes
19d779e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path

import pytest

from gcmd_classifier.classification import (
    TermBranchSeed,
    VariableCandidate,
    build_variable_candidates,
    descend_variables,
    validate_variable_candidate_relationship,
)
from gcmd_classifier.config import ModelSettings
from gcmd_classifier.errors import StructuredModelResponseError, UnknownCandidateIDError
from gcmd_classifier.llm import FakeModelClient
from gcmd_classifier.llm.prompts import PromptCandidate
from gcmd_classifier.models import ArticleRecord, SupportType
from gcmd_classifier.vocabulary import build_vocabulary_index, load_vocabulary

FIXTURE_PATH = Path("tests/fixtures/gcmd_hierarchy_small.json")
FULL_HIERARCHY_PATH = Path("data/gcmd_hierarchy.json")


def _index():
    return load_vocabulary(FIXTURE_PATH)


def _article(abstract: str = "Carbon dioxide profiles and methane are discussed.") -> ArticleRecord:
    return ArticleRecord(
        DOI="10.example/variable-traversal",
        Title="Atmospheric carbon observations",
        Year=2025,
        Abstract=abstract,
    )


def _decision(candidate_id: str, confidence: float | None = 0.82) -> dict:
    return {
        "candidate_id": candidate_id,
        "confidence": confidence,
        "evidence": f"Evidence for {candidate_id}.",
        "support_type": "explicit",
        "reason": f"Reason for {candidate_id}.",
    }


def _term_branch(term_uuid: str = "term-atmospheric-chemistry") -> TermBranchSeed:
    index = _index()
    term = index.get(term_uuid)
    topic = index.get(term.parent_uuid or "")
    return TermBranchSeed(
        branch_id="topic:topic_0001/term:term_0001",
        parent_topic_uuid=topic.UUID,
        parent_topic_name=topic.name,
        term_uuid=term.UUID,
        term_name=term.name,
        term_level="Term",
        term_canonical_path=term.canonical_path,
        evidence="The article supports the Term.",
        support_type="explicit",
        confidence=0.77,
        reason="Term was selected.",
        candidate_id="term_0001",
        parent_branch_id="topic:topic_0001",
        prompt_version="term-test",
        model_provider="fake",
        model_name="fake-model",
        retry_count=0,
    )


def _multi_index():
    data = {
        "level": "Category",
        "name": "EARTH SCIENCE",
        "children": [
            {
                "level": "Topic",
                "name": "ATMOSPHERE",
                "UUID": "topic-atmosphere",
                "children": [
                    {
                        "level": "Term",
                        "name": "ATMOSPHERIC CHEMISTRY",
                        "UUID": "term-atmospheric-chemistry",
                        "children": [
                            {
                                "level": "Variable_Level_1",
                                "name": "CARBON",
                                "UUID": "vl1-carbon",
                                "children": [
                                    {
                                        "level": "Variable_Level_2",
                                        "name": "CARBON DIOXIDE",
                                        "UUID": "vl2-carbon-dioxide",
                                    }
                                ],
                            },
                            {
                                "level": "Variable_Level_1",
                                "name": "NITROGEN",
                                "UUID": "vl1-nitrogen",
                            },
                        ],
                    }
                ],
            }
        ],
    }
    return build_vocabulary_index(data, vocabulary_version="multi-variable-test")


def _multi_term_branch() -> TermBranchSeed:
    index = _multi_index()
    term = index.get("term-atmospheric-chemistry")
    topic = index.get(term.parent_uuid or "")
    return TermBranchSeed(
        branch_id="topic:topic_0001/term:term_0001",
        parent_topic_uuid=topic.UUID,
        parent_topic_name=topic.name,
        term_uuid=term.UUID,
        term_name=term.name,
        term_level="Term",
        term_canonical_path=term.canonical_path,
        evidence="The article supports the Term.",
        support_type="explicit",
        confidence=0.77,
        reason="Term was selected.",
        candidate_id="term_0001",
        parent_branch_id="topic:topic_0001",
        prompt_version="term-test",
        model_provider="fake",
        model_name="fake-model",
        retry_count=0,
    )


def test_variable_candidates_are_built_only_from_current_parent_direct_children() -> None:
    index = _index()
    candidates = build_variable_candidates(index, parent_uuid="term-atmospheric-chemistry")

    assert [candidate.variable_uuid for candidate in candidates] == ["vl1-atmosphere-carbon"]
    assert all(
        index.parent_of(candidate.variable_uuid) == "term-atmospheric-chemistry"
        for candidate in candidates
    )


def test_grandchildren_are_not_included_before_child_parent_is_selected() -> None:
    candidates = build_variable_candidates(_index(), parent_uuid="term-atmospheric-chemistry")

    assert "vl2-carbon-dioxide" not in [candidate.variable_uuid for candidate in candidates]
    assert "vl3-carbon-dioxide-profiles" not in [
        candidate.variable_uuid for candidate in candidates
    ]


def test_sibling_branches_are_not_included() -> None:
    candidates = build_variable_candidates(_index(), parent_uuid="vl1-atmosphere-carbon")

    assert [candidate.variable_uuid for candidate in candidates] == [
        "vl2-carbon-dioxide",
        "vl2-methane",
    ]
    assert "vl1-ocean-carbon" not in [candidate.variable_uuid for candidate in candidates]


def test_variable_candidate_ids_are_unique_within_prompt() -> None:
    candidates = build_variable_candidates(_index(), parent_uuid="vl1-atmosphere-carbon")
    candidate_ids = [candidate.candidate_id for candidate in candidates]

    assert candidate_ids == ["variable_0001", "variable_0002"]
    assert len(candidate_ids) == len(set(candidate_ids))


def test_variable_candidate_ids_map_to_uuid_bearing_variable_records() -> None:
    index = _index()

    for candidate in build_variable_candidates(index, parent_uuid="vl1-atmosphere-carbon"):
        record = index.get(candidate.variable_uuid)
        assert record.UUID
        assert record.level.startswith("Variable_Level_")
        assert candidate.prompt_candidate.candidate_id == candidate.candidate_id


def test_variable_candidate_construction_does_not_hard_code_names_or_uuids() -> None:
    index = _index()
    candidates = build_variable_candidates(index, parent_uuid="vl1-atmosphere-carbon")
    direct_children = [index.get(uuid) for uuid in index.children_of("vl1-atmosphere-carbon")]

    assert [candidate.variable_uuid for candidate in candidates] == [
        child.UUID for child in direct_children
    ]
    assert [candidate.prompt_candidate.name for candidate in candidates] == [
        child.name for child in direct_children
    ]


def test_parent_with_no_direct_children_is_terminal_outcome() -> None:
    result = descend_variables(
        article=_article(),
        term_branch=_term_branch("term-weather-events"),
        vocabulary=_index(),
        model_client=FakeModelClient([]),
        settings=ModelSettings(),
    )

    assert result.terminal_count == 1
    assert result.terminals[0].final_uuid == "term-weather-events"
    assert result.terminals[0].stop_reason == "No direct Variable children are available."


def test_stop_at_term_when_no_variable_child_supported() -> None:
    client = FakeModelClient(
        [
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "No Variable child is adequately supported.",
            }
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.terminals[0].final_uuid == "term-atmospheric-chemistry"
    assert result.terminals[0].final_level == "Term"
    assert result.terminals[0].stop_reason == "No Variable child is adequately supported."


def test_stop_at_variable_level_1() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "Variable_Level_1 is the deepest supported concept.",
            },
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.terminals[0].final_uuid == "vl1-atmosphere-carbon"
    assert result.terminals[0].final_level == "Variable_Level_1"
    assert result.terminals[0].stop_reason == "Variable_Level_1 is the deepest supported concept."


def test_stop_at_variable_level_2() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "Variable_Level_2 is specific enough.",
            },
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.terminals[0].final_uuid == "vl2-carbon-dioxide"
    assert result.terminals[0].final_level == "Variable_Level_2"
    assert result.terminals[0].stop_reason == "Variable_Level_2 is specific enough."


def test_select_variable_level_3_and_stop_naturally_at_leaf() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(prompt_version_variable="variable-test"),
    )

    terminal = result.terminals[0]
    assert terminal.final_uuid == "vl3-carbon-dioxide-profiles"
    assert terminal.final_level == "Variable_Level_3"
    assert terminal.stop_reason == "Selected Variable_Level_3 is a leaf node."
    assert terminal.prompt_version == "variable-test"
    assert len(client.requests) == 3


def test_multiple_selected_variable_level_1_children_create_independent_branches() -> None:
    index = _multi_index()
    client = FakeModelClient(
        [
            {
                "selected": [_decision("variable_0001"), _decision("variable_0002")],
                "stop_at_parent": False,
            },
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "Stop at carbon.",
            },
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_multi_term_branch(),
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert {terminal.final_uuid for terminal in result.terminals} == {"vl1-carbon", "vl1-nitrogen"}
    assert len({terminal.branch_id for terminal in result.terminals}) == 2


def test_multiple_selected_descendants_under_one_branch_continue_independently() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {
                "selected": [_decision("variable_0001"), _decision("variable_0002")],
                "stop_at_parent": False,
            },
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert {terminal.final_uuid for terminal in result.terminals} == {
        "vl3-carbon-dioxide-profiles",
        "vl2-methane",
    }


def test_sibling_branch_continuation_after_one_branch_stops() -> None:
    index = _multi_index()
    client = FakeModelClient(
        [
            {
                "selected": [_decision("variable_0001"), _decision("variable_0002")],
                "stop_at_parent": False,
            },
            {"selected": [], "stop_at_parent": True, "stop_reason": "Stop at first sibling."},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_multi_term_branch(),
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert {terminal.final_uuid for terminal in result.terminals} == {"vl1-carbon", "vl1-nitrogen"}
    assert result.errors == ()


def test_sibling_branch_continuation_after_one_branch_fails() -> None:
    index = _multi_index()
    client = FakeModelClient(
        [
            {
                "selected": [_decision("variable_0001"), _decision("variable_0002")],
                "stop_at_parent": False,
            },
            {"selected": [_decision("unknown-variable")], "stop_at_parent": False},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_multi_term_branch(),
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert [terminal.final_uuid for terminal in result.terminals] == ["vl1-nitrogen"]
    assert result.has_errors is True
    assert result.errors[0].code == "UnknownCandidateIDError"


def test_no_forced_leaf_descent() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "Do not force leaf descent.",
            },
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.terminals[0].final_uuid == "vl1-atmosphere-carbon"
    assert len(client.requests) == 2


def test_no_skipped_hierarchy_levels_or_non_direct_descendant_evaluation() -> None:
    client = FakeModelClient(
        [{"selected": [_decision("vl3-carbon-dioxide-profiles")], "stop_at_parent": False}]
    )

    with pytest.raises(UnknownCandidateIDError):
        descend_variables(
            article=_article(),
            term_branch=_term_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_unknown_variable_candidate_id_is_rejected() -> None:
    client = FakeModelClient(
        [{"selected": [_decision("unknown-variable")], "stop_at_parent": False}]
    )

    with pytest.raises(UnknownCandidateIDError):
        descend_variables(
            article=_article(),
            term_branch=_term_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_duplicate_variable_candidate_id_is_rejected() -> None:
    client = FakeModelClient(
        [
            {
                "selected": [_decision("variable_0001"), _decision("variable_0001")],
                "stop_at_parent": False,
            }
        ]
    )

    with pytest.raises(UnknownCandidateIDError):
        descend_variables(
            article=_article(),
            term_branch=_term_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_empty_or_malformed_variable_candidate_id_rejected_by_structured_validation() -> None:
    client = FakeModelClient([{"selected": [_decision("")], "stop_at_parent": False}])

    with pytest.raises(StructuredModelResponseError):
        descend_variables(
            article=_article(),
            term_branch=_term_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_selected_candidate_from_sibling_parent_is_rejected() -> None:
    invalid_candidate = VariableCandidate(
        candidate_id="variable_0001",
        parent_uuid="vl1-ocean-carbon",
        variable_uuid="vl2-carbon-dioxide",
        prompt_candidate=PromptCandidate(
            candidate_id="variable_0001",
            name="CARBON DIOXIDE",
            level="Variable_Level_2",
        ),
    )

    with pytest.raises(ValueError):
        validate_variable_candidate_relationship(
            invalid_candidate,
            selected_parent_uuid="vl1-atmosphere-carbon",
            index=_index(),
        )


def test_manually_invalid_candidate_to_record_mapping_is_rejected() -> None:
    invalid_candidate = VariableCandidate(
        candidate_id="variable_0001",
        parent_uuid="vl1-atmosphere-carbon",
        variable_uuid="vl1-ocean-carbon",
        prompt_candidate=PromptCandidate(
            candidate_id="variable_0001",
            name="CARBON",
            level="Variable_Level_1",
        ),
    )

    with pytest.raises(ValueError):
        validate_variable_candidate_relationship(
            invalid_candidate,
            selected_parent_uuid="vl1-atmosphere-carbon",
            index=_index(),
        )


def test_terminal_outcome_includes_provenance_and_required_fields() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001", confidence=0.66)], "stop_at_parent": False},
            {"selected": [], "stop_at_parent": True, "stop_reason": "Stop at VL1."},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(model_name="unit-test-model"),
    )
    terminal = result.terminals[0]

    assert terminal.branch_id.endswith("/variable:variable_0001")
    assert terminal.parent_branch_id == _term_branch().branch_id
    assert terminal.topic_uuid == "topic-atmosphere"
    assert terminal.topic_name == "ATMOSPHERE"
    assert terminal.term_uuid == "term-atmospheric-chemistry"
    assert terminal.term_name == "ATMOSPHERIC CHEMISTRY"
    assert terminal.final_uuid == "vl1-atmosphere-carbon"
    assert terminal.final_name == "CARBON"
    assert terminal.final_level == "Variable_Level_1"
    assert terminal.final_canonical_path.endswith("CARBON")
    assert terminal.path_components[-1] == "CARBON"
    assert terminal.evidence == "Evidence for variable_0001."
    assert terminal.support_type is SupportType.EXPLICIT
    assert terminal.confidence == 0.66
    assert terminal.reason == "Reason for variable_0001."
    assert terminal.stop_reason == "Stop at VL1."
    assert terminal.candidate_id == "variable_0001"
    assert terminal.model_name == "unit-test-model"
    assert len(terminal.evidence_trail) == 2


def test_child_branch_ids_are_distinct_and_preserve_parent_context() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
            {
                "selected": [_decision("variable_0001"), _decision("variable_0002")],
                "stop_at_parent": False,
            },
            {"selected": [_decision("variable_0001")], "stop_at_parent": False},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    branch_ids = [terminal.branch_id for terminal in result.terminals]
    assert len(branch_ids) == len(set(branch_ids))
    assert all(branch_id.startswith(_term_branch().branch_id) for branch_id in branch_ids)


def test_confidence_is_preserved_only_as_uncalibrated_metadata() -> None:
    client = FakeModelClient(
        [
            {"selected": [_decision("variable_0001", confidence=0.01)], "stop_at_parent": False},
            {"selected": [], "stop_at_parent": True, "stop_reason": "Stop despite low confidence."},
        ]
    )

    result = descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.terminals[0].confidence == 0.01
    assert result.terminals[0].final_uuid == "vl1-atmosphere-carbon"


def test_variable_descent_uses_provider_neutral_model_interface() -> None:
    client = FakeModelClient(
        [{"selected": [], "stop_at_parent": True, "stop_reason": "Stop at Term."}]
    )

    descend_variables(
        article=_article(),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(provider="fake-provider", model_name="fake-model"),
    )

    request = client.requests[0]
    assert request.provider == "fake-provider"
    assert request.model_name == "fake-model"
    assert request.response_schema.__name__ == "VariableResponse"


def test_fake_model_receives_article_parent_context_and_direct_variable_candidates() -> None:
    client = FakeModelClient(
        [{"selected": [], "stop_at_parent": True, "stop_reason": "Stop at Term."}]
    )
    article = _article()

    descend_variables(
        article=article,
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    prompt = client.requests[0].prompt
    assert article.DOI in prompt
    assert article.Title in prompt
    assert article.Abstract in prompt
    assert "ATMOSPHERIC CHEMISTRY" in prompt
    assert "candidate_id: variable_0001" in prompt
    assert "CARBON" in prompt
    assert "CARBON DIOXIDE" not in prompt


def test_empty_abstract_remains_valid_and_is_passed_to_variable_prompt() -> None:
    client = FakeModelClient(
        [{"selected": [], "stop_at_parent": True, "stop_reason": "Stop at Term."}]
    )

    descend_variables(
        article=_article(abstract=""),
        term_branch=_term_branch(),
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert "<ABSTRACT>\n\n</ABSTRACT>" in client.requests[0].prompt


def test_full_data_current_variable_candidate_counts_are_direct_children_only() -> None:
    index = load_vocabulary(FULL_HIERARCHY_PATH)
    counts = {"Variable_Level_1": 0, "Variable_Level_2": 0, "Variable_Level_3": 0}
    variable_uuids: list[str] = []
    parent_uuids = [
        uuid
        for uuid, record in index.records_by_uuid.items()
        if record.level in {"Term", "Variable_Level_1", "Variable_Level_2"}
    ]

    for parent_uuid in parent_uuids:
        for candidate in build_variable_candidates(index, parent_uuid=parent_uuid):
            record = index.get(candidate.variable_uuid)
            counts[record.level] += 1
            variable_uuids.append(record.UUID)
            assert index.parent_of(record.UUID) == parent_uuid
            assert record.UUID

    assert counts["Variable_Level_1"] == 1368
    assert counts["Variable_Level_2"] == 1456
    assert counts["Variable_Level_3"] == 553
    assert len(variable_uuids) == len(set(variable_uuids))