feat(rag): feed whole matched documents to the LLM — no context truncation (A7 revised)

This commit is contained in:
2026-08-24 23:37:44 -04:00
parent d7a4064616
commit 1e6ae360e0
16 changed files with 923 additions and 60 deletions
+21 -30
View File
@@ -1,4 +1,4 @@
"""Unit: retriever — ordering, dedup, and the context cap (fake rows).
"""Unit: retriever — ordering and dedup, no context cap (A7 revised) (fake rows).
The SQL side of :func:`app.rag.retriever.retrieve` is exercised by the
chat integration tests against real Postgres; the pure mapping logic in
@@ -47,14 +47,14 @@ def test_ranks_by_best_chunk_score_not_first_hit() -> None:
_chunk(a, 0.9, position=2), # a's best chunk comes last
_chunk(c, 0.5),
]
docs = select_documents(chunks, n=3, max_chars=10_000)
docs = select_documents(chunks, n=3)
assert [d.path for d in docs] == ["a.md", "b.md", "c.md"]
def test_dedups_to_one_document_per_hit_set() -> None:
a = _doc("a.md", "A" * 50)
chunks = [_chunk(a, 0.2), _chunk(a, 0.7), _chunk(a, 0.5)]
docs = select_documents(chunks, n=2, max_chars=10_000)
docs = select_documents(chunks, n=2)
assert len(docs) == 1
assert docs[0] is a
@@ -62,40 +62,31 @@ def test_dedups_to_one_document_per_hit_set() -> None:
def test_caps_at_n_documents() -> None:
docs_in = [_doc(f"d{i}.md", "X" * 20) for i in range(4)]
chunks = [_chunk(d, 0.5 - 0.1 * i) for i, d in enumerate(docs_in)]
out = select_documents(chunks, n=2, max_chars=10_000)
out = select_documents(chunks, n=2)
assert [d.path for d in out] == ["d0.md", "d1.md"]
def test_combined_content_capped_with_truncation_marker() -> None:
big = _doc("big.md", "B" * 100)
small = _doc("small.md", "S" * 100)
def test_content_never_truncated_even_past_old_budget() -> None:
"""Whole documents, never truncated (A7 revised, owner permission 2026-08-24).
Two documents of 20 000 + 15 000 chars — 35 000 combined, well past
the old 24 000 context budget — come back with content
**byte-identical** to the originals, and the truncation marker is
absent from both.
"""
big = _doc("big.md", "B" * 20_000)
small = _doc("small.md", "S" * 15_000)
chunks = [_chunk(big, 0.9), _chunk(small, 0.6)]
out = select_documents(chunks, n=2, max_chars=150)
# Best doc stays intact; the overflowing one is truncated in place.
assert out[0].content == "B" * 100
assert out[1].content.endswith(TRUNCATION_MARKER)
assert out[1].content.startswith("S")
assert len(out[0].content) + len(out[1].content) <= 150
def test_single_doc_over_budget_is_truncated_to_budget() -> None:
big = _doc("big.md", "Z" * 200)
out = select_documents([_chunk(big, 0.9)], n=2, max_chars=50)
assert len(out[0].content) == 50
assert out[0].content.endswith(TRUNCATION_MARKER)
def test_under_budget_no_truncation() -> None:
a = _doc("a.md", "A" * 80)
b = _doc("b.md", "B" * 60)
out = select_documents([_chunk(b, 0.5), _chunk(a, 0.9)], n=2, max_chars=200)
assert [d.path for d in out] == ["a.md", "b.md"]
assert a.content == "A" * 80 and b.content == "B" * 60
assert TRUNCATION_MARKER not in a.content + b.content
out = select_documents(chunks, n=2)
assert [d.path for d in out] == ["big.md", "small.md"]
assert out[0].content == "B" * 20_000
assert out[1].content == "S" * 15_000
assert TRUNCATION_MARKER not in out[0].content
assert TRUNCATION_MARKER not in out[1].content
def test_empty_hits_yield_no_documents() -> None:
assert select_documents([], n=2, max_chars=24_000) == []
assert select_documents([], n=2) == []
# ---------------------------------------------------------------------------