arborist/tests/test_qa.py
russell@unturf.com 8d6961fcc1
aborist/arborist
modified:   .gitlab-ci.yml
	modified:   bench/qa_questions.txt
	modified:   bench/qa_sweep.py
	modified:   bench/run.sh
	modified:   docs/TICKETS.md
	modified:   docs/_source/README.md
	modified:   docs/_source/_ext/makefile_targets.py
	modified:   docs/_source/api/cli.rst
	modified:   docs/_source/api/distill.rst
	modified:   docs/_source/api/mesh.rst
	modified:   docs/_source/api/qa.rst
	modified:   docs/_source/api/retrieval.rst
	modified:   docs/_source/api/storage.rst
	modified:   docs/_source/api/substrate.rst
	modified:   docs/_source/concepts.rst
	modified:   docs/_source/conf.py
	modified:   docs/_source/cookbook.rst
	modified:   docs/_source/index.rst
	modified:   docs/_source/license.rst
	modified:   docs/_source/quickstart.rst
	modified:   docs/bench-maxing.md
	modified:   docs/benchmarks.md
	modified:   docs/cti-architecture.md
	modified:   docs/diagrams/aborist-modules.dot
	modified:   docs/diagrams/aborist-modules.svg
	modified:   docs/diagrams/mesh-data-flow.dot
	modified:   docs/diagrams/mesh-epoch-lifecycle.dot
	modified:   docs/diagrams/mesh-epoch-lifecycle.svg
	modified:   docs/diagrams/mesh-group-decisions.dot
	modified:   docs/diagrams/mesh-group-decisions.svg
	modified:   docs/diagrams/mesh-identity-stack.dot
	modified:   docs/diagrams/mesh-secret-envelope.dot
	modified:   docs/mesh.md
	modified:   docs/qa-modes-bench.md
	modified:   docs/seven-point-program.md
	modified:   docs/tickets/ticket-000001-retrieval-keywords-audit-gap.md
	modified:   docs/tickets/ticket-000002-reference-frame-polarity-contract.md
	modified:   docs/tickets/ticket-000003-anchor-class-warrant.md
	modified:   docs/tickets/ticket-000005-label-ladder-migration.md
	modified:   docs/tickets/ticket-000006-bench-emergent-findings.md
	modified:   docs/tickets/ticket-000007-query-layer-hyphen-fold.md
	modified:   docs/tickets/ticket-000008-broad-quantifier-preflight-guard.md
	modified:   docs/tickets/ticket-000009-quantifier-preflight-dag-binding.md
	modified:   docs/tickets/ticket-000010-metacognition-preflight-guard.md
	modified:   docs/tickets/ticket-000011-soft-preflight-hint-sidecar.md
	modified:   scripts/backfill_concepts.py
	modified:   scripts/bench_emergent.py
	modified:   tests/crawler/test_async_web_fetcher.py
	modified:   tests/crawler/test_bridge.py
	modified:   tests/crawler/test_web_fetch.py
	modified:   tests/test_bench_qa_sweep.py
	modified:   tests/test_burn.py
	modified:   tests/test_burn_doc.py
	modified:   tests/test_claim_lattice.py
	modified:   tests/test_cli_render.py
	modified:   tests/test_compress.py
	modified:   tests/test_concepts.py
	modified:   tests/test_dag.py
	modified:   tests/test_directives.py
	modified:   tests/test_distill.py
	modified:   tests/test_distill_recursive.py
	modified:   tests/test_evict.py
	modified:   tests/test_frame.py
	modified:   tests/test_grok_source.py
	modified:   tests/test_html_source.py
	modified:   tests/test_ingest.py
	modified:   tests/test_inspect.py
	modified:   tests/test_journal.py
	modified:   tests/test_keys.py
	modified:   tests/test_llm_context_base.py
	modified:   tests/test_merkle.py
	modified:   tests/test_mesh.py
	modified:   tests/test_mesh_aead.py
	modified:   tests/test_mesh_chain.py
	modified:   tests/test_mesh_cli.py
	modified:   tests/test_mesh_cli_pull.py
	modified:   tests/test_mesh_wire.py
	modified:   tests/test_mesh_wire_e2e.py
	modified:   tests/test_metacognition.py
	modified:   tests/test_migration_audit_mode.py
	modified:   tests/test_providence_source.py
	modified:   tests/test_qa.py
	modified:   tests/test_qa_quality_live.py
	modified:   tests/test_quantifier_caps.py
	modified:   tests/test_quantifier_classifier.py
	modified:   tests/test_quantifier_phase4.py
	modified:   tests/test_quantifier_reminder.py
	modified:   tests/test_query.py
	modified:   tests/test_reclassify.py
	modified:   tests/test_repair.py
	modified:   tests/test_resume.py
	modified:   tests/test_snapshot.py
	modified:   tests/test_soft_preflight.py
	modified:   tests/test_tfidf.py
	modified:   tests/test_vcs_source.py
	modified:   tests/test_verify.py
	modified:   tests/test_verify_json.py
	modified:   tests/test_versioned_ingest.py
	modified:   tests/test_warrant.py
	modified:   tests/test_wikipedia_old.py
	modified:   tests/test_wikipedia_xml.py
	modified:   tests/test_wikitext.py
2026-05-07 09:31:49 -04:00

235 lines
7.4 KiB
Python

"""Q&A: 8-dim cache_key, falsification-aware lookup, audit chain.
Mock client only — no network. Verifies the v9.8 admissibility invariant
end-to-end.
"""
from __future__ import annotations
import hashlib
from typing import Iterator
from arborist.document import Document
from arborist.ingest import ingest_source
from arborist.merkle import proof_from_dict, verify_proof
from arborist.qa import ask
from arborist.qa.client import StubClient
from arborist.qa.keys import (
cache_key,
conversation_hash,
governance_policy_hash,
model_profile_hash,
question_hash,
)
from arborist.source import Source
from arborist.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)
LONG = (
"The eight forms of capital include living, social, and intellectual. " * 10
+ "Merkle providence proves answer derives from a specific source. " * 10
)
def _ingest_one(conn) -> str:
ingest_source(conn, FakeSource([_doc("test://qa", LONG)]))
return conn.execute(
"SELECT document_root FROM documents WHERE document_uri='test://qa'"
).fetchone()["document_root"]
def test_ask_writes_record_with_strict_proof(tmp_path):
db = tmp_path / "qa.db"
conn = connect(db)
try:
root = _ingest_one(conn)
# Verbatim quote from LONG → audit_mode=STRICT.
client = StubClient(
answer=(
'The document states: '
'"eight forms of capital include living, social, and intellectual"'
)
)
result = ask(
conn,
document_root=root,
question="What are the forms of capital?",
client=client,
model_id="test-model",
revision="r1",
quantization="fp8",
)
assert result["status"] == "cache_miss_then_written"
assert result["audit_mode"] == "STRICT"
assert result["n_quotes"] == 1
assert result["n_verified"] == 1
assert result["unverified_quotes"] == []
# Stored proof reconstructs the source root.
proof = proof_from_dict(result["merkle_proof"]["chunk_0_proof"])
assert verify_proof(proof)
assert proof.root.hex() == root
# Providence cache row landed with correct fields.
row = conn.execute("SELECT * FROM providence_cache").fetchone()
assert row["source_root"] == root
assert row["falsification_state"] == "live"
assert row["chain"] == "private"
assert row["audit_event_hash"] is not None
# Audit chain has providence_write event.
last = conn.execute(
"SELECT event_type FROM audit_events ORDER BY seq DESC LIMIT 1"
).fetchone()["event_type"]
assert last == "providence_write"
finally:
conn.close()
def test_ask_cache_hit_does_not_call_client(tmp_path):
db = tmp_path / "hit.db"
conn = connect(db)
try:
root = _ingest_one(conn)
client = StubClient(answer="first answer")
ask(conn, document_root=root, question="Q1?", client=client, model_id="m")
assert len(client.calls) == 1
# Second ask with identical inputs -> hit, no client call.
result = ask(
conn, document_root=root, question="Q1?", client=client, model_id="m"
)
assert result["status"] == "cache_hit"
assert result["answer_text"] == "first answer"
assert len(client.calls) == 1 # unchanged
row = conn.execute(
"SELECT hit_count FROM providence_cache"
).fetchone()
assert row["hit_count"] == 1
finally:
conn.close()
def test_different_model_yields_different_cache_key(tmp_path):
db = tmp_path / "model.db"
conn = connect(db)
try:
root = _ingest_one(conn)
client = StubClient(answer="ans")
ask(conn, document_root=root, question="Q?", client=client, model_id="A")
ask(conn, document_root=root, question="Q?", client=client, model_id="B")
n = conn.execute("SELECT COUNT(*) FROM providence_cache").fetchone()[0]
assert n == 2
assert len(client.calls) == 2
finally:
conn.close()
def test_falsification_skips_cache_hit(tmp_path):
"""Stale records are ignored even when the 8-dim key matches."""
db = tmp_path / "stale.db"
conn = connect(db)
try:
root = _ingest_one(conn)
client = StubClient(answer="first")
first = ask(
conn, document_root=root, question="Q?", client=client, model_id="m"
)
ckey = first["cache_key"]
# Mark stale.
conn.execute(
"UPDATE providence_cache SET falsification_state='stale' "
"WHERE cache_key=?",
(ckey,),
)
# Asking again must MISS and call client; new record inserted (or
# rejected on PRIMARY KEY collision since cache_key is the PK).
try:
ask(conn, document_root=root, question="Q?", client=client, model_id="m")
# If accepted, we'd have 2 records — but PK collision will throw.
assert False, "expected PK collision on stale-then-write"
except Exception: # IntegrityError from cache_key PK
pass
# Falsified record stays stale; no fresh record landed.
rows = conn.execute(
"SELECT cache_key, falsification_state FROM providence_cache"
).fetchall()
assert len(rows) == 1
assert rows[0]["falsification_state"] == "stale"
assert len(client.calls) == 2 # second call did happen
finally:
conn.close()
def test_unknown_document(tmp_path):
db = tmp_path / "u.db"
conn = connect(db)
try:
result = ask(
conn,
document_root="00" * 32,
question="Q?",
client=StubClient(),
model_id="m",
)
assert result["status"] == "unknown_document"
finally:
conn.close()
def test_cold_source_refuses(tmp_path):
"""Source must be hot — answer derived from evicted content can't be proved."""
from arborist.evict import evict_to_cold
db = tmp_path / "cold.db"
conn = connect(db)
try:
root = _ingest_one(conn)
evict_to_cold(conn)
result = ask(
conn,
document_root=root,
question="Q?",
client=StubClient(),
model_id="m",
)
assert result["status"] == "source_cold"
finally:
conn.close()
def test_cache_key_is_pure_function():
"""Hashes are deterministic; manual computation matches the runner."""
src_root = "a" * 64
qh = question_hash("What is X?")
mh = model_profile_hash("m", "r", "q")
msg = [{"role": "user", "content": "x"}]
ch = conversation_hash(msg)
gh = governance_policy_hash({"temperature": 0.1})
k1 = cache_key(src_root, qh, mh, ch, gh, "v1", "v1", "v1")
k2 = cache_key(src_root, qh, mh, ch, gh, "v1", "v1", "v1")
assert k1 == k2
# Bumping any dim changes the key.
k3 = cache_key(src_root, qh, mh, ch, gh, "v2", "v1", "v1")
assert k3 != k1
# Pure SHA-256 of the joined string.
expected = hashlib.sha256(
"|".join([src_root, qh, mh, ch, gh, "v1", "v1", "v1"]).encode()
).hexdigest()
assert k1 == expected