File size: 7,233 Bytes
a964978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from unittest.mock import AsyncMock, patch

import pytest

from app.llm.provider import ResolvedProvider
from app.pipeline.chunker import Chunk
from app.pipeline.graph import (
    KnowledgeGraph,
    expand_graph_knowledge,
    extract_entities_and_graph,
    extract_heuristic_entities,
)
from app.pipeline.indexes import DocumentIndex
from app.pipeline.refmap import ReferenceMap
from app.pipeline.twohop import TwoHopChunk
from app.sessions import DocumentSession
from app.trace import get_trace_manager


def test_knowledge_graph_neighbor_lookup_and_cap():
    """Verify neighbor fact retrieval and strict <= 4 cap enforcement."""
    kg = KnowledgeGraph()
    kg.add_node("Resignation Policy", entity_type="POLICY")
    kg.add_node("Notice Period", entity_type="REQUIREMENT")
    kg.add_node("Severance Pay", entity_type="BENEFIT")
    kg.add_node("Exit Interview", entity_type="PROCESS")
    kg.add_node("Company Laptop", entity_type="EQUIPMENT")
    kg.add_node("Security Clearance", entity_type="SECURITY")

    kg.add_edge("Resignation Policy", "Notice Period", "requires", "Voluntary resignation requires 60 days advance written notice.")
    kg.add_edge("Resignation Policy", "Severance Pay", "provides", "Standard resignation does not provide severance pay.")
    kg.add_edge("Resignation Policy", "Exit Interview", "mandates", "All departing personnel must attend an Exit Interview.")
    kg.add_edge("Resignation Policy", "Company Laptop", "requires", "Departing personnel must return Company Laptop within 48 hours.")
    kg.add_edge("Resignation Policy", "Security Clearance", "governs", "Security Clearance is revoked on the final working day.")

    # There are 5 connected edges, but cap must strictly enforce max 4
    neighbors = kg.get_neighbor_facts(["Resignation Policy"], max_facts=4)
    assert len(neighbors) == 4, f"Expected strictly <= 4 facts, got {len(neighbors)}"
    facts = [e.fact for e in neighbors]
    assert "Voluntary resignation requires 60 days advance written notice." in facts


def test_heuristic_entity_extraction():
    """Verify pattern-based entity extraction on chunk text."""
    chunks = [
        Chunk(
            id="c1",
            doc_id="d1",
            text="Voluntary Resignation requires 60 days notice to the Human Resources department.",
            token_count=12,
            content_hash="h1",
        ),
        Chunk(
            id="c2",
            doc_id="d1",
            text="Health Insurance provides comprehensive dental and optical coverage.",
            token_count=10,
            content_hash="h2",
        ),
    ]

    kg = extract_heuristic_entities(chunks)
    assert len(kg.nodes) >= 2
    assert len(kg.edges) >= 1
    edge_relations = [e.relation for e in kg.edges]
    assert "requires" in edge_relations or "provides" in edge_relations


@pytest.mark.anyio
async def test_llm_graph_extraction_mock():
    """Verify LLM extraction populates graph nodes and directed edges."""
    provider = ResolvedProvider(
        provider_type="groq",
        api_key="gsk_test",
        base_url="https://api.groq.com/openai/v1",
        model="llama-3.3-70b-versatile",
    )

    mock_json = """
    {
      "nodes": [
        {"name": "Leave Policy", "entity_type": "POLICY", "description": "Annual vacation and sick leave rules"},
        {"name": "Manager Approval", "entity_type": "REQUIREMENT", "description": "Supervisor sign-off"}
      ],
      "edges": [
        {"source_node": "Leave Policy", "target_node": "Manager Approval", "relation": "requires", "fact": "All leave requests require Manager Approval 2 weeks in advance."}
      ]
    }
    """

    with patch("app.pipeline.graph.get_brain_client") as mock_get_brain:
        mock_brain = AsyncMock()
        mock_brain.generate.return_value = mock_json
        mock_get_brain.return_value = mock_brain

        chunks = [
            Chunk(id="c1", doc_id="d1", text="Leave Policy text", token_count=4, content_hash="h1")
        ]
        kg = await extract_entities_and_graph(chunks, provider=provider)

        assert "leave policy" in kg.nodes
        assert len(kg.edges) == 1
        assert kg.edges[0].relation == "requires"
        assert "Manager Approval" in kg.edges[0].fact


def test_expand_graph_knowledge_cap_and_labels():
    """Verify entity-adjacent query pulls in related facts formatted as (related: ...)."""
    kg = KnowledgeGraph()
    kg.add_node("Resignation Policy")
    kg.add_node("Handover Document")
    kg.add_edge(
        "Resignation Policy",
        "Handover Document",
        "requires",
        "Employees resigning must submit a Handover Document to their team lead.",
    )

    candidate_chunk = Chunk(
        id="c1",
        doc_id="d1",
        text="Section 9: Resignation Policy details for full-time staff.",
        token_count=10,
        content_hash="h1",
    )
    initial_candidates = [TwoHopChunk(chunk=candidate_chunk)]

    expanded, added_count = expand_graph_knowledge(
        query="What are the resignation rules?",
        candidates=initial_candidates,
        graph=kg,
        max_expansion=4,
    )

    assert added_count == 1
    assert len(expanded) == 2
    related_chunk = expanded[1]
    assert related_chunk.is_two_hop is True
    assert "related:" in (related_chunk.added_reason or "")
    assert "(related: Employees resigning must submit a Handover Document" in related_chunk.chunk.text


@pytest.mark.anyio
async def test_stream_answer_pipeline_graph_expansion_integration():
    """
    End-to-end gate: an entity-adjacent question pulls in a related fact with the label;
    cap respected in trace.
    """
    from app.pipeline.answer import stream_answer_pipeline

    kg = KnowledgeGraph()
    kg.add_node("Notice Period")
    kg.add_node("Transition Plan")
    kg.add_edge(
        "Notice Period",
        "Transition Plan",
        "requires",
        "Voluntary resignation notice requires an approved written Transition Plan.",
    )

    chunk = Chunk(
        id="c1",
        doc_id="d1",
        text="Section 5.1: Voluntary resignation requires a Notice Period of 60 days.",
        token_count=12,
        content_hash="h1",
        section_path=["5. Termination", "5.1 Notice Period"],
    )
    doc_index = DocumentIndex()
    doc_index.build([chunk])

    session = DocumentSession(
        session_id="test_graph_stream_sess",
        filename="policy.txt",
        file_size_bytes=100,
        page_count=1,
        blocks=[],
        chunks=[chunk],
        ref_map=ReferenceMap(),
        doc_index=doc_index,
        knowledge_graph=kg,
    )

    events = []
    async for sse in stream_answer_pipeline(
        session, "What does the Notice Period require?", question_id="q_graph_test"
    ):
        events.append(sse)

    # Check expand event contains graph_expansion_count
    expand_events = [e for e in events if "event: expand" in e]
    assert len(expand_events) == 1
    assert '"graph_expansion_count"' in expand_events[0]

    # Check trace
    trace = get_trace_manager().get_trace("q_graph_test")
    assert trace is not None
    assert "graph_expansion_count" in trace
    assert trace["graph_expansion_count"] <= 4
    assert trace["graph_expansion_count"] >= 1