arborist/tests/test_llm_context_base.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

168 lines
5.4 KiB
Python

"""LLM context is wikitext-stripped before send.
Before this lands, runner.ask() and query.query() handed Hermes raw
[[wikitext]] markup. The model paraphrased it into clean prose then
labeled the result as a verbatim quote — verifier correctly flagged
those as UNGROUNDED. With base_version in policy, both paths run
to_base() on the assembled context before message construction; Hermes
sees prose so it CAN quote verbatim, and the verifier compares prose-
to-prose end-to-end (idempotent — verify_quotes also runs to_base).
These tests use StubClient to capture exactly what messages would be
sent to the LLM, asserting the wikitext markup is gone before the
model would see it.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from arborist.document import Document
from arborist.ingest import ingest_source
from arborist.qa.client import StubClient
from arborist.qa.runner import DEFAULT_POLICY, ask
from arborist.store import connect
from arborist.wikitext import BASE_VERSION
WIKITEXT_DOC = (
"{{Refimprove|date=October 2009}}\n"
"[[File:foo.jpg|thumb|caption]]\n"
"''[[Final Fantasy VII]]'' follows [[protagonist]] [[Cloud Strife]], "
"a troubled mercenary.<ref>Smith 2010</ref> "
"He joins [[AVALANCHE]] to stop [[Shinra Electric Power Company|Shinra]]."
)
class _OneDocSource:
source_type = "llm_ctx_test"
def __init__(self, content: str):
self._doc = Document(
uri="https://example.com/ff7",
content=content,
source_type=self.source_type,
title="Test",
)
def iter_documents(self):
yield self._doc
def _ingest_one(db_path: Path, content: str) -> str:
conn = connect(db_path)
try:
ingest_source(conn, _OneDocSource(content))
row = conn.execute(
"SELECT document_root FROM documents WHERE document_uri=?",
("https://example.com/ff7",),
).fetchone()
finally:
conn.close()
return row["document_root"]
# ---------------------------------------------------------------------------
# DEFAULT_POLICY carries base_version
# ---------------------------------------------------------------------------
def test_default_policy_includes_base_version():
"""If base_version disappears from default policy, the LLM goes back
to seeing raw wikitext silently. Tripwire."""
assert DEFAULT_POLICY["base_version"] == BASE_VERSION
# ---------------------------------------------------------------------------
# Stripped context flows to the LLM (ask path)
# ---------------------------------------------------------------------------
def _user_messages_text(client: StubClient) -> str:
"""Concatenate every user-role message from the LAST captured call."""
last = client.calls[-1]
return "\n".join(m["content"] for m in last["messages"] if m["role"] == "user")
def test_ask_passes_base_form_to_llm(tmp_path):
db = tmp_path / "qa.db"
root = _ingest_one(db, WIKITEXT_DOC)
client = StubClient(answer="I don't know based on the provided document.")
conn = connect(db)
try:
ask(
conn,
document_root=root,
question="who is the protagonist?",
client=client,
model_id="stub-model",
)
finally:
conn.close()
sent = _user_messages_text(client)
# Wikitext markers MUST be gone.
assert "[[" not in sent
assert "]]" not in sent
assert "{{Refimprove" not in sent
assert "<ref>" not in sent
assert "File:" not in sent
# Topical content survives.
assert "Cloud Strife" in sent
assert "Final Fantasy VII" in sent
assert "AVALANCHE" in sent
def test_ask_with_base_version_disabled_passes_raw_wikitext(tmp_path):
"""Operator escape hatch: setting base_version=None reverts to raw
wikitext context. Confirms the gate is effective and reversible."""
db = tmp_path / "qa.db"
root = _ingest_one(db, WIKITEXT_DOC)
custom_policy = dict(DEFAULT_POLICY)
custom_policy["base_version"] = None
client = StubClient(answer="stub")
conn = connect(db)
try:
ask(
conn,
document_root=root,
question="who is the protagonist?",
client=client,
model_id="stub-model",
policy=custom_policy,
)
finally:
conn.close()
sent = _user_messages_text(client)
# Raw wikitext markers should now BE present in what the LLM saw.
assert "[[" in sent
assert "]]" in sent
# ---------------------------------------------------------------------------
# governance_policy_hash flows base_version (cache_key separation)
# ---------------------------------------------------------------------------
def test_base_version_changes_governance_hash(tmp_path):
"""Two policies that differ only in base_version produce different
governance_policy_hash values. This is what keeps prior cached
answers (under raw-wikitext policy) from satisfying lookups under
the new (stripped) policy on the same question/source — they remain
distinct cache_keys."""
from arborist.qa.keys import governance_policy_hash
p_with = dict(DEFAULT_POLICY)
p_with["base_version"] = "wikitext-base-v1"
p_without = dict(DEFAULT_POLICY)
p_without["base_version"] = None
h_with = governance_policy_hash(p_with)
h_without = governance_policy_hash(p_without)
assert h_with != h_without