arborist/tests/test_canonical_projection.py
russell@unturf.com 0a2e347f9c
qa: math/logic π* reach the user — arborist canon + query preflight
`make query Q="0.1 + 0.2"` used to return `no_sources` because
arithmetic-shaped input has no FTS5 hits in any text shard. Two
surfaces close that gap.

**`arborist canon <key> "<input>"`** — direct π* call, no shards,
no LLM, no audit chain. Pure projection:

    $ arborist canon arithmetic@v1 "0.1 + 0.2"      → 3/10
    $ arborist canon logic-kernel@v1 "A IMPL B"     → (NOT A OR B)
    $ arborist canon --list                         → registry contents
    $ arborist canon --json arithmetic@v1 "0.1+0.2" → SHA-256 envelope

**Math/logic preflight in `arborist query`** — pure-arithmetic and
pure-propositional questions short-circuit RAG and answer through
arithmetic@v1 / logic-kernel@v1 directly. Synthetic
`audit_mode=CANONICAL_PROJECTION`, renders as
`CANONICAL · via <pi_star_ref>`:

    $ arborist query "0.1 + 0.2"
    0.1 + 0.2
      CANONICAL · via arithmetic@v1   0.0s   (projected)
    3/10

    $ arborist query "(NOT B) IMPL (NOT A)"
    (NOT B) IMPL (NOT A)
      CANONICAL · via logic-kernel@v1   0.0s   (projected)
    (NOT A OR B)

Sniff is conservative: pure-arithmetic shape (digits + ops, no
letters) or pure-propositional shape (uppercase atoms + reserved
keywords only). Natural-language wrapping ("what is 0.1+0.2?")
falls through to RAG. PiStarError on a shape match also falls
through — preflight is best-effort, never blocking.

Disable per-call: `--no-canonical-preflight` flag,
`policy["canonical_projection_preflight"]=False`.

No schema changes: CANONICAL_PROJECTION is a render-layer audit_mode
token. No providence_cache writes, no audit_events, no
governance_policy_hash bump. The canonical bytes ARE the answer;
SHA-256 of the bytes is the equivalence-class identity (already
committed via the π* registry).

Side housekeeping: arborist/pi_star/__init__.py docstring caught up
with reality — six concrete π*'s ship today, only tabular-pinned@v1
remains as a stub.

31 new tests (preflight sniff + dispatch, query short-circuit,
contrapositive equivalence-class collapse, CLI subcommand exit codes
and JSON envelope, --no-canonical-preflight policy gate). Full
suite: 1300 passed, 36 skipped.
2026-05-08 12:18:28 -04:00

248 lines
7.9 KiB
Python

"""Tests for the canonical-projection preflight + ``arborist canon`` CLI.
Two surfaces, one substrate. The π* registry already has unit-level
coverage in ``tests/test_pi_star.py``; these tests exercise the ways
those π*'s become reachable from the user — without RAG, without an
LLM, and without a shard.
- ``_canonical_projection_preflight`` — the sniff + π* dispatch
helper inside ``arborist.qa.query``.
- ``query()`` short-circuit path — math/logic-shaped questions return
``status="canonical_projection"`` before retrieval starts.
- ``arborist canon ...`` CLI subcommand — direct π* call.
"""
from __future__ import annotations
import json
import subprocess
import sys
from pathlib import Path
import pytest
from arborist.qa.client import StubClient
from arborist.qa.query import _canonical_projection_preflight, query
# ----- preflight helper ---------------------------------------------------
@pytest.mark.parametrize(
"question,expected_ref,expected_canonical",
[
("0.1 + 0.2", "arithmetic@v1", b"3/10"),
("0.3", "arithmetic@v1", b"3/10"),
("1/3 + 1/6", "arithmetic@v1", b"1/2"),
("6/4", "arithmetic@v1", b"3/2"),
("(2+3)*4", "arithmetic@v1", b"20/1"),
("A IMPL B", "logic-kernel@v1", b"(NOT A OR B)"),
("(NOT B) IMPL (NOT A)", "logic-kernel@v1", b"(NOT A OR B)"),
("A OR NOT A", "logic-kernel@v1", b"TRUE"),
("NOT NOT A", "logic-kernel@v1", b"A"),
],
)
def test_preflight_matches_math_or_logic(question, expected_ref, expected_canonical):
result = _canonical_projection_preflight(question)
assert result is not None, f"expected match, got None for {question!r}"
ref, canonical = result
assert ref == expected_ref
assert canonical == expected_canonical
@pytest.mark.parametrize(
"question",
[
# natural language → no match
"what is 0.1 + 0.2?",
"tell me about the capital of france",
"is A IMPL B the same as B IMPL A",
# mixed case (lowercase letters) → no logic match
"a IMPL b",
# bare uppercase atom without operator → no logic match
"A",
"B",
# empty / whitespace
"",
" ",
],
)
def test_preflight_falls_through_for_natural_language(question):
assert _canonical_projection_preflight(question) is None
def test_preflight_returns_none_on_pi_star_error():
"""Shape-matched but π*-rejected input falls through gracefully.
``arithmetic@v1`` rejects identifiers; a string that LOOKS
arithmetic but contains nothing parseable should not crash query().
"""
# Empty parens — passes the regex shape but arithmetic@v1 rejects.
assert _canonical_projection_preflight("()") is None
# ----- query() short-circuit ----------------------------------------------
def test_query_math_short_circuits_with_no_shards(tmp_path: Path):
"""Math-shaped question returns canonical_projection without
needing a shard or hitting the LLM."""
qa_db = tmp_path / "qa.db"
no_shards = tmp_path / "shards" # absent on purpose
result = query(
question="0.1 + 0.2",
qa_db=qa_db,
chat_client=StubClient(""),
model_id="test/model",
shards_dir=no_shards,
)
assert result["status"] == "canonical_projection"
assert result["audit_mode"] == "CANONICAL_PROJECTION"
assert result["verifier_method"] == "canonical_projection"
assert result["pi_star_ref"] == "arithmetic@v1"
assert result["answer_text"] == "3/10"
assert result["lookup_path"] == "preflight_canonical"
assert result["sources"] == []
assert result["cache_key"] is None
def test_query_logic_short_circuits(tmp_path: Path):
qa_db = tmp_path / "qa.db"
no_shards = tmp_path / "shards"
result = query(
question="A IMPL B",
qa_db=qa_db,
chat_client=StubClient(""),
model_id="test/model",
shards_dir=no_shards,
)
assert result["status"] == "canonical_projection"
assert result["pi_star_ref"] == "logic-kernel@v1"
assert result["answer_text"] == "(NOT A OR B)"
def test_query_contrapositive_collapses_to_same_canonical(tmp_path: Path):
"""A IMPL B and (NOT B) IMPL (NOT A) are the same equivalence class."""
qa_db = tmp_path / "qa.db"
no_shards = tmp_path / "shards"
a = query(
question="A IMPL B",
qa_db=qa_db,
chat_client=StubClient(""),
model_id="m",
shards_dir=no_shards,
)
b = query(
question="(NOT B) IMPL (NOT A)",
qa_db=qa_db,
chat_client=StubClient(""),
model_id="m",
shards_dir=no_shards,
)
assert a["answer_text"] == b["answer_text"] == "(NOT A OR B)"
def test_query_natural_language_falls_through_to_rag(tmp_path: Path):
"""Natural-language question doesn't trigger the preflight; goes
to RAG and (with no corpus) returns no_sources."""
qa_db = tmp_path / "qa.db"
no_shards = tmp_path / "shards"
result = query(
question="what is the capital of france?",
qa_db=qa_db,
chat_client=StubClient(""),
model_id="m",
shards_dir=no_shards,
)
# Falls through — preflight didn't match, RAG runs, finds no shards.
assert result["status"] != "canonical_projection"
assert result.get("audit_mode") != "CANONICAL_PROJECTION"
def test_query_canonical_preflight_disable_via_policy(tmp_path: Path):
"""policy['canonical_projection_preflight']=False disables the
short-circuit even on math-shaped input."""
qa_db = tmp_path / "qa.db"
no_shards = tmp_path / "shards"
result = query(
question="0.1 + 0.2",
qa_db=qa_db,
chat_client=StubClient(""),
model_id="m",
shards_dir=no_shards,
policy={"canonical_projection_preflight": False},
)
assert result["status"] != "canonical_projection"
# ----- `arborist canon` CLI subcommand ------------------------------------
def _run_canon(*args: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, "-m", "arborist.cli", "canon", *args],
capture_output=True,
text=True,
check=False,
)
def test_cli_canon_arithmetic():
r = _run_canon("arithmetic@v1", "0.1 + 0.2")
assert r.returncode == 0, r.stderr
assert r.stdout.strip() == "3/10"
def test_cli_canon_logic():
r = _run_canon("logic-kernel@v1", "A IMPL B")
assert r.returncode == 0, r.stderr
assert r.stdout.strip() == "(NOT A OR B)"
def test_cli_canon_time_series():
r = _run_canon(
"time-series-quantized@v1",
'{"dt":1,"dv":0.1,"samples":[[0,1.0],[1,2.0]]}',
)
assert r.returncode == 0, r.stderr
assert r.stdout.strip() == "dt=1;dv=0.1;n=2;t0=0:10|20"
def test_cli_canon_list():
r = _run_canon("--list")
assert r.returncode == 0, r.stderr
keys = {line.split("\t")[0] for line in r.stdout.strip().split("\n")}
assert "arithmetic@v1" in keys
assert "logic-kernel@v1" in keys
assert "time-series-quantized@v1" in keys
assert "code-py-ast@v1" in keys
assert "claim-lattice@v1" in keys
assert "wikitext-base@v1" in keys
def test_cli_canon_json_emits_sha256():
r = _run_canon("--json", "arithmetic@v1", "0.1 + 0.2")
assert r.returncode == 0, r.stderr
obj = json.loads(r.stdout)
assert obj["pi_star_ref"] == "arithmetic@v1"
assert obj["input"] == "0.1 + 0.2"
assert obj["canonical"] == "3/10"
assert len(obj["canonical_sha256"]) == 64
def test_cli_canon_unknown_key_returns_2():
r = _run_canon("does-not-exist@v1", "anything")
assert r.returncode == 2
assert "unknown π*" in r.stderr
def test_cli_canon_pi_star_error_returns_1():
r = _run_canon("arithmetic@v1", "x + 1") # identifier rejected
assert r.returncode == 1
assert "error" in r.stderr.lower()
def test_cli_canon_missing_args_returns_2():
r = _run_canon()
assert r.returncode == 2
assert "required" in r.stderr.lower()