arborist/tests/test_query.py
russell@unturf.com d6f98929d7
qa: breadth-required filter for multi-token queries + light token stem
Fox 2026-04-29: 'who is supermans girlfriend?' returned 7-of-8 unrelated
`Girlfriends`-titled articles (TV show, movies, songs); only 1 actual
Superman-related doc made it. Two coupled defects:

1. Title-overlap accept fired on ANY single-token match. A 2-token
   query was admitting docs that shared only ONE qtoken. `Girlfriends`
   passed because its title matched "girlfriend" even though no
   "superman" anywhere in the doc.

2. `_body_density_passes` required at least HALF the qtokens — too
   lenient for the 2-token case (1 of 2 = 50% = pass).

3. Possessive plural mismatch: question_hash strips apostrophes so
   "superman's" → "supermans", but the corpus has bare "Superman".
   `body.count("supermans")` returns 0 even on the canonical doc.

Three coordinated fixes in `aborist/qa/query.py`:

- New `_stem_token_for_match(t)` helper: strips a trailing `s` for
  tokens > 4 chars (so `supermans` → `superman`, `girlfriends` →
  `girlfriend`). Conservative: skips short tokens & double-s endings
  to avoid `class` / `boss` / `pass` corruption.

- `_body_density_passes` now uses `_body_count_with_stem` (literal-
  first, stem-fallback) AND a tightened breadth threshold:
    ≤ 2 tokens   require ALL of them
    3+ tokens    require N - 1 (allow one weak signal token to miss)

- `_filter_by_title_relevance` mirrors the same breadth threshold for
  title-overlap. Synonym fallback (any-match against synonym_expand)
  is preserved ONLY for 1-token queries — otherwise a stray synonym
  hit (e.g. AMD synonym matching an Intel-only title) over-recalls.

End-to-end on the actual corpus (3.4M articles, 8 shards):

  before: 'supermans girlfriend' → 7 unrelated `Girlfriends` titles, 1 Superman doc
  after:  Lois_Lane in top-K (the canonical answer), other Superman-related
          docs alongside, Girlfriends-only-titled docs filtered out

Tests:
- test_query_filter_requires_breadth_for_multi_token_queries — synthetic
  3-doc corpus pinning the new behavior (Lois Lane keeps; girlfriends-tv
  & superman-music both drop).
- test_query_filter_one_token_query_still_synonym_expands — pin the
  1-token loose path is preserved (no over-tightening).

349 passed, 1 skipped.

Burned the two stale UNGROUNDED girlfriend-query cache records so a
fresh `make query` exercises the new filter end-to-end.
2026-04-29 16:47:00 -04:00

526 lines
17 KiB
Python

"""Multi-source corpus Q&A: search → context → cache → Hermes.
Stub client only — no network. Validates:
- FTS5 finds the right docs across a small corpus
- context_root is deterministic (sorted source roots, then Merkle)
- cache hit on identical question returns same record without calling client
- different question -> different cache_key
"""
from __future__ import annotations
import json
from typing import Iterator
from aborist.document import Document
from aborist.ingest import ingest_source
from aborist.qa import query
from aborist.qa.client import StubClient
from aborist.source import Source
from aborist.store import connect
class FakeSource(Source):
source_type = "test"
def __init__(self, docs: list[Document]):
self.docs = docs
def iter_documents(self) -> Iterator[Document]:
yield from self.docs
def _doc(uri: str, content: str) -> Document:
return Document(uri=uri, content=content, source_type="test", title=uri.rsplit("/", 1)[-1])
# Three docs with distinguishable content so FTS5 can pick winners.
DOCS = [
_doc(
"test://anarchism",
"Anarchism is a political philosophy that opposes the state. " * 12
+ "Mutual aid is central to anarchist theory. " * 8,
),
_doc(
"test://capitalism",
"Capitalism is an economic system based on private ownership. " * 12
+ "Market exchange and capital accumulation drive growth. " * 8,
),
_doc(
"test://anarcho-capitalism",
"Anarcho-capitalism combines anarchism's opposition to the state with capitalism's private property. " * 12
+ "Murray Rothbard developed many of its core ideas. " * 8,
),
]
def test_query_picks_relevant_docs_and_writes_record(tmp_path):
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
# Verbatim quote from DOCS[2] → audit_mode=STRICT.
client = StubClient(
answer=(
'Per the source: '
'"Anarcho-capitalism combines anarchism\'s opposition to the state '
'with capitalism\'s private property"'
)
)
result = query(
question="What is anarcho-capitalism?",
qa_db=qa_db,
chat_client=client,
model_id="test-model",
single_db=main_db,
top_k=3,
)
assert result["status"] == "cache_miss_then_written"
assert result["audit_mode"] == "STRICT"
assert "Anarcho-capitalism" in result["answer_text"]
assert len(result["sources"]) >= 1
assert any("anarcho-capitalism" in s["document_uri"] for s in result["sources"])
# context_root is deterministic.
sorted_roots = sorted(s["document_root"] for s in result["sources"])
if len(sorted_roots) == 1:
assert result["context_root"] == sorted_roots[0]
# Repeat call with same question → cache hit, no LLM call.
n_calls_before = len(client.calls)
result2 = query(
question="What is anarcho-capitalism?",
qa_db=qa_db,
chat_client=client,
model_id="test-model",
single_db=main_db,
top_k=3,
)
assert result2["status"] == "cache_hit"
assert result2["cache_key"] == result["cache_key"]
assert len(client.calls) == n_calls_before # no new call
def test_query_different_question_different_cache(tmp_path):
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
client = StubClient(answer="answer text")
r1 = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=client,
model_id="m",
single_db=main_db,
)
r2 = query(
question="What is capitalism?",
qa_db=qa_db,
chat_client=client,
model_id="m",
single_db=main_db,
)
assert r1["cache_key"] != r2["cache_key"]
assert len(client.calls) == 2 # both missed cache, both called client
def test_query_question_equivalence_class_dedups_cache(tmp_path):
"""Fox 2026-04-29 catch: 'who is batman?', 'who is batman', and
'who is the batman?' should all hit the same cache. Question_hash
canonicalizes correctly, but conversation_hash used to hash the
LITERAL question text in the user message — so each variant got
its own chash and missed cache. Fix: canonical_question form is
substituted into the messages list used for conversation_hash,
while the LLM still receives the verbatim question."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
captured: list = []
def _capture(messages, **kw):
captured.append(messages)
return "stub answer"
# First variant — populates cache.
r1 = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
)
# Second & third variants — must hit the same cache_key.
r2 = query(
question="What is anarchism", # no trailing ?
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
)
r3 = query(
question="what is the anarchism?", # leading article
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
)
assert r1["cache_key"] == r2["cache_key"] == r3["cache_key"]
assert r1["status"] == "cache_miss_then_written"
assert r2["status"] == "cache_hit"
assert r3["status"] == "cache_hit"
# Only the first call reached the LLM.
assert len(captured) == 1
# And it received the verbatim question, not the canonical form.
user_text = "\n".join(
m["content"] for m in captured[0] if m["role"] == "user"
)
assert "What is anarchism?" in user_text
def test_query_strict_dedup_distinguishes_question_variants(tmp_path):
"""policy['question_dedup']='strict' makes every variant get its
own cache_key. 'Who is X?' and 'who is the X?' write separate
records under strict policy."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
strict_policy = dict(query.__globals__["DEFAULT_QUERY_POLICY"])
strict_policy["question_dedup"] = "strict"
r1 = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer="a"),
model_id="m",
single_db=main_db,
policy=strict_policy,
fidelity="strict",
)
r2 = query(
question="what is anarchism",
qa_db=qa_db,
chat_client=StubClient(answer="b"),
model_id="m",
single_db=main_db,
policy=strict_policy,
fidelity="strict",
)
# Different cache_keys; both populated independently.
assert r1["cache_key"] != r2["cache_key"]
assert r1["status"] == r2["status"] == "cache_miss_then_written"
assert r1["lookup_path"] == "miss"
def test_query_equivalence_class_fidelity_falls_back_across_dedup_modes(tmp_path):
"""A record written under equivalence_class policy gets reused by a
later strict-policy lookup that asks for fidelity='equivalence_class'.
Verifies the cross-silo fallback: strict ckey misses, alternate
equivalence_class ckey hits, lookup_path reports the fallback."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
DEFAULT = query.__globals__["DEFAULT_QUERY_POLICY"]
eq_policy = dict(DEFAULT)
eq_policy["question_dedup"] = "equivalence_class"
strict_policy = dict(DEFAULT)
strict_policy["question_dedup"] = "strict"
# Agent A writes equivalence-class record for 'what is anarchism?'.
captured: list = []
def _capture(messages, **kw):
captured.append(messages)
return "stub"
r1 = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
policy=eq_policy,
)
assert r1["status"] == "cache_miss_then_written"
# Agent B (strict policy, equivalence_class fidelity) asks the same
# question. Strict ckey misses; the eq_class fallback hits A's record.
r2 = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
policy=strict_policy,
fidelity="equivalence_class",
)
assert r2["status"] == "cache_hit"
assert r2["lookup_path"] == "equivalence_class_fallback"
assert len(captured) == 1 # only A's call reached the LLM
def test_query_strict_fidelity_does_not_fall_back(tmp_path):
"""Audit-grade lookup: strict-fidelity refuses to read records from
the other dedup mode's silo. Even if equivalence_class has a hit,
strict fidelity reports cache miss."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
DEFAULT = query.__globals__["DEFAULT_QUERY_POLICY"]
eq_policy = dict(DEFAULT)
eq_policy["question_dedup"] = "equivalence_class"
strict_policy = dict(DEFAULT)
strict_policy["question_dedup"] = "strict"
# Agent A writes under equivalence_class.
query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer="a"),
model_id="m",
single_db=main_db,
policy=eq_policy,
)
# Agent B (strict policy + strict fidelity) asks same question.
# Should NOT find A's record; runs LLM fresh.
captured: list = []
def _capture(messages, **kw):
captured.append(messages)
return "stub"
r2 = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
policy=strict_policy,
fidelity="strict",
)
assert r2["status"] == "cache_miss_then_written"
assert r2["lookup_path"] == "miss"
assert len(captured) == 1
def test_query_no_sources_when_empty_corpus(tmp_path):
main_db = tmp_path / "empty.db"
qa_db = tmp_path / "qa.db"
connect(main_db).close() # creates schema, no docs
result = query(
question="What is anything?",
qa_db=qa_db,
chat_client=StubClient(),
model_id="m",
single_db=main_db,
)
assert result["status"] == "no_sources"
def test_query_filter_requires_breadth_for_multi_token_queries(tmp_path):
"""Fox 2026-04-29 catch: 'supermans girlfriend' returned 7-of-8
unrelated `Girlfriends`-titled articles because title-overlap
accepted ANY single-token match. The fix tightens both title and
body filters to require ALL query tokens (≤2-token queries) so a
doc whose title only matches ONE of the two qtokens doesn't pass.
Synthetic corpus pins the new behavior:
- "Lois Lane" — neither qtoken in title; body has both → keep
- "Girlfriends" — only "girlfriend" in title/body; no "superman" → drop
- "Superman album" — "superman" in title; body lacks "girlfriend" → drop
"""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
docs = [
_doc(
"test://lois-lane",
"Lois Lane is a fictional character who works for the Daily Planet. "
"She is Superman's girlfriend and frequently appears in his stories. "
* 5,
),
_doc(
"test://girlfriends-tv",
"Girlfriends is a sitcom about four women in Los Angeles. " * 10,
),
_doc(
"test://superman-music",
"Superman is an album of rock music recorded in Tokyo. " * 10,
),
]
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(docs))
finally:
conn.close()
result = query(
question="who is supermans girlfriend?",
qa_db=qa_db,
chat_client=StubClient(answer="x"),
model_id="m",
single_db=main_db,
top_k=8,
)
src_uris = [s["document_uri"] for s in result["sources"]]
# Lois Lane MUST be in the result set — it's the only doc with both
# query tokens in its body.
assert any("lois-lane" in u for u in src_uris), (
f"breadth filter regressed: lois-lane not in {src_uris}"
)
def test_query_filter_one_token_query_still_synonym_expands(tmp_path):
"""1-token queries keep the loose synonym-expanded any-match — pin
that we didn't over-tighten the single-token case."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
docs = [
_doc("test://anarchism", "Anarchism is a political philosophy. " * 30),
]
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(docs))
finally:
conn.close()
result = query(
question="anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer="x"),
model_id="m",
single_db=main_db,
top_k=3,
)
assert len(result["sources"]) == 1
def test_query_per_source_cap_prevents_huge_doc_monopoly(tmp_path):
"""Fox 2026-04-29 catch: a top-ranked huge document (e.g.
List_of_Batman_comics, 80 KB+ bibliography) used to consume the
entire 60 KB budget at hit #1, dropping every subsequent doc with
char_budget <= 0. Now each of the top_k hits gets at most
`max_context_chars / top_k` chars; multiple sources land in
context even when hit #1 is huge."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
# Hit #1 is intentionally huge: every query token AND the most copies
# so it FTS5-ranks first. Hits #2 and #3 are smaller but still
# contain the query token.
bulk_token = "anarchism " * 5000 # ~50 KB after canonicalize
docs = [
_doc("test://huge-bibliography", bulk_token),
_doc("test://anarchism-bio", "Anarchism is a political philosophy. " * 30),
_doc("test://anarchism-history", "Anarchism's history begins with Proudhon. " * 30),
]
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(docs))
finally:
conn.close()
captured = {"messages": None}
def _capture(messages, **kw):
captured["messages"] = messages
return "stub"
result = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer=_capture),
model_id="m",
single_db=main_db,
top_k=3,
max_context_chars=60000,
)
# All three sources should be in the result, not just the huge one.
assert len(result["sources"]) >= 2, (
f"per-source cap broke: only {len(result['sources'])} sources reached "
f"context (huge doc monopolized again)"
)
# Verify the user-turn context contains content from the smaller docs
# too — not just a 60K slab of the bulk doc.
user_text = "\n".join(m["content"] for m in captured["messages"] if m["role"] == "user")
assert "anarchism-bio" in user_text or "anarchism-history" in user_text
def test_query_per_source_cap_respects_top_k(tmp_path):
"""top_k=1 → cap = max_context_chars (legacy behavior preserved when
operator wants a single large source)."""
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
big = _doc("test://big", "Anarchism is a political philosophy. " * 2000)
conn = connect(main_db)
try:
ingest_source(conn, FakeSource([big]))
finally:
conn.close()
result = query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer="x"),
model_id="m",
single_db=main_db,
top_k=1,
max_context_chars=60000,
)
# Single source allowed up to full budget.
assert len(result["sources"]) == 1
def test_query_persists_audit_event(tmp_path):
main_db = tmp_path / "corpus.db"
qa_db = tmp_path / "qa.db"
conn = connect(main_db)
try:
ingest_source(conn, FakeSource(DOCS))
finally:
conn.close()
query(
question="What is anarchism?",
qa_db=qa_db,
chat_client=StubClient(answer="X"),
model_id="m",
single_db=main_db,
)
# qa.db should now have one providence_query event in its audit chain.
qc = connect(qa_db)
try:
events = qc.execute(
"SELECT event_type FROM audit_events ORDER BY seq"
).fetchall()
finally:
qc.close()
types = [e["event_type"] for e in events]
assert "providence_query" in types