File size: 1,892 Bytes
e67c48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03bed0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from ragforge.citations import normalize_citation_syntax, repair_missing_citations


def test_grouped_and_duplicate_trailing_citations_are_normalized():
    answer = "AI RMF covers four functions [D1, D2]. [D2] [D1]"
    assert normalize_citation_syntax(answer) == "AI RMF covers four functions [D1] [D2]."


def test_repair_skips_broad_preamble_ending_in_colon():
    answer = "The following documents contain escalation guidance:"
    sources = [
        {
            "id": "D1",
            "title": "acme_cloud_runbook.md",
            "snippet": "Incident escalation guidance and response procedures.",
        }
    ]
    repaired, count = repair_missing_citations(answer, sources)
    assert repaired == answer
    assert count == 0


def test_repair_adds_supported_citation_to_factual_bullet():
    answer = "* The Sev-1 acknowledgement target is five minutes."
    sources = [
        {
            "id": "D1",
            "title": "acme_cloud_runbook.md",
            "snippet": "The on-call engineer must acknowledge a Sev-1 page within five minutes.",
        }
    ]
    repaired, count = repair_missing_citations(answer, sources)
    assert "[D1]" in repaired
    assert count == 1


def test_repair_missing_citations_repairs_uncited_sentence_inside_cited_paragraph():
    from ragforge.citations import repair_missing_citations

    answer = (
        "Support tiers [T1]: Starter costs 0. "
        "Intermediate tiers include Team at 49 and Business at 199. "
        "Enterprise costs 799 [T1]."
    )
    sources = [{
        "id": "T1",
        "type": "table",
        "title": "support_matrix",
        "snippet": "Starter 0 Team 49 Business 199 Enterprise 799",
    }]
    repaired, count = repair_missing_citations(answer, sources, semantic_support=False)
    assert "Intermediate tiers include Team at 49 and Business at 199 [T1]." in repaired
    assert count >= 1