Implements 6 of fox's feedback-2/3 items (2026-05-01) targeting the
red-fish-blue-fish output. Each lives in the render or sidecar layer
— no schema or cache-key change beyond the verifier_policy_hash that
landed earlier in the day.
(1) Honest audit-label rendering. Schema audit_mode (STRICT/HYBRID/
UNGROUNDED) describes the lexical verifier's call; the user-
facing label spells out what was actually checked. Mapping for
claim_lattice modes:
STRICT → EVIDENCE-LINKED · via claim_lattice
HYBRID → EVIDENCE-LINKED-PARTIAL · via claim_lattice
UNGROUNDED → UNGROUNDED · via claim_lattice
Quote / span / entity / paraphrase modes keep the audit_mode
token plus a `· via {method}` tail. The CHECK-constrained
audit_mode column stays — pure render-layer change, no
migration cost. Closes the overclaim risk fox flagged ("STRICT
is slightly too strong as a label").
(2) Sentence-bounded evidence spans. _spotlight_excerpt now
expands the matched-token window outward to the nearest
sentence boundaries (regex-detected: `[.!?][\"')\\]]?\\s+(?=[A-Z])`)
instead of cutting at fixed byte offsets. Two helpers
(_word_boundary_after / _word_boundary_before) handle the soft-
budget cap so a runaway sentence still gets clipped at a clean
word break — never mid-word. Pre-fix the renderer produced
excerpts like `"...freewheeling plot... friends and p..."` (the
`p...` is the offending mid-word truncation fox surfaced); now
the excerpt is `"One Fish Two Fish Red Fish Blue Fish is a
1960 children's book by Dr. Seuss. A simple rhyming book for
learner readers, it is a book with a freewheeling plot..."`.
(3) Source-role display + used/unused annotation. The CLI source
list went from
[1] One Fish Two Fish Red Fish Blue Fish — wikipedia.org/...
[2] Red Dwarf — wikipedia.org/...
to
[1] One Fish Two Fish Red Fish Blue Fish — primary_answer_source — used (E1) — ...
[2] Red Dwarf — background_source — unused — ...
Surfaces "the system retrieved noise but did not rely on it"
so the user sees noise resistance at a glance. Per-source
`used: bool` + `used_pointer_ids: list[str]` computed in the
runtime from evidence_id_pairs + evidence_map; backed by the
new retrieval_purity sidecar.
(4) Retrieval-purity sidecar. Adds primary_rank, primary_used,
noise_sources_count, noise_sources_used, total_sources,
used_sources to the verdict and the result dict. Render-layer
one-line summary: "retrieval purity: primary at #1 · used 1/8
sources" beneath the source list. Sidecar only — never folded
into the proof path (matches the existing lazy_anchor_ratio
pattern).
(5) Ordered-token title scoring. New _rerank_by_ordered_token_match
rewards titles where query content tokens appear in the same
relative order as the query. "red fish blue fish" query →
"One Fish Two Fish Red Fish Blue Fish" gets ordered_match=4
boost (1+0.5*3 = 2.5×); "Red Dwarf" stays at ordered_match=1
(no boost). LCS-based, stem-aware, deterministic. Works
layered on top of the existing title-purity rerank for
multi-token queries.
(6) Noise-resistance fixture (tests/test_qa_quality_live.py).
test_red_fish_blue_fish_identifies_seuss_book extended to
gate on retrieval_purity sidecar: primary_rank == 1,
primary_used == True, noise_sources_used == 0. Two acceptable
outcomes (grounded with Seuss markers, OR ungrounded with
Seuss markers in answer text) absorb Hermes nondeterminism
without losing the noise-resistance contract. Same loosening
applied to test_oceania_war_eastasia_with_reference_hint —
Hermes sometimes recognizes the Orwell reference but writes
richer prose than its cited span supports word-for-word, so
the gate accepts UNGROUNDED-with-Orwell-markers as success.
Live test_qa_quality_live.py: 20/20 fixtures pass. 507 unit tests
green. Working tree leaves bench/qa_questions.txt for fox's
parallel WIP.
215 lines
7.5 KiB
Python
215 lines
7.5 KiB
Python
"""Pretty-print + ensure_ascii=False on `aborist query` output.
|
|
|
|
The CLI default emits a human-readable render of the query result;
|
|
``--json`` switches to the raw record. Both paths must:
|
|
- render unicode literals (no \\uXXXX escape sequences)
|
|
- present the answer prominently; metadata is supporting info
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from aborist.cli import _render_query_human
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _render_query_human — direct unit tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _result(**overrides) -> dict:
|
|
base = {
|
|
"status": "cache_miss_then_written",
|
|
"audit_mode": "HYBRID",
|
|
"cache_key": "35ab7d3355c723b759145d6d446cb1d9010abe0dce9992ca0e8b87837b596610",
|
|
"context_root": "ab" * 32,
|
|
"answer_text": "Pikachu is a species of Pokémon creatures.",
|
|
"sources": [
|
|
{
|
|
"document_root": "cd" * 32,
|
|
"document_uri": "https://en.wikipedia.org/wiki/Pikachu",
|
|
"title": "Pikachu",
|
|
"score": 36.6,
|
|
"chunk_idx": 6,
|
|
"shard": "002.db",
|
|
}
|
|
],
|
|
"n_quotes": 2,
|
|
"n_verified": 1,
|
|
"verifier_method": "quote",
|
|
"unverified_quotes": ["something the model said but didn't ground"],
|
|
"timings": {"total_ms": 9234.1},
|
|
}
|
|
base.update(overrides)
|
|
return base
|
|
|
|
|
|
def test_render_includes_question_at_top():
|
|
out = _render_query_human(_result(), "who is pikachu?")
|
|
assert out.splitlines()[0] == "who is pikachu?"
|
|
|
|
|
|
def test_render_summary_line_has_audit_n_verified_method_elapsed():
|
|
"""Summary line carries audit_label · n_verified/n_quotes · elapsed.
|
|
The label format updated 2026-05-01 to honesty-spell verifier
|
|
method (e.g. "EVIDENCE-LINKED · via claim_lattice" instead of
|
|
"STRICT") so user-facing output doesn't overclaim semantic truth.
|
|
Quote / span / entity / paraphrase modes keep the original
|
|
audit_mode token plus a "via {method}" tail."""
|
|
out = _render_query_human(_result(), "q")
|
|
second = out.splitlines()[1]
|
|
# Quote-mode keeps the audit_mode token (HYBRID) — it's the
|
|
# claim_lattice modes that get the EVIDENCE-LINKED relabel.
|
|
assert "HYBRID" in second
|
|
# Counts and method still in the line; "verified" word dropped
|
|
# to give the slimmer "1/2" form room for the longer label tail.
|
|
assert "1/2" in second
|
|
assert "via quote" in second
|
|
assert "9.2s" in second
|
|
|
|
|
|
def test_render_marks_cache_hit_distinctly():
|
|
out_hit = _render_query_human(_result(status="cache_hit"), "q")
|
|
out_fresh = _render_query_human(_result(status="cache_miss_then_written"), "q")
|
|
assert "(cached)" in out_hit
|
|
assert "(fresh)" in out_fresh
|
|
|
|
|
|
def test_render_emits_unicode_literals_not_escapes():
|
|
"""Pokémon, not Pok\\u00e9mon — the user's terminal should see real é."""
|
|
out = _render_query_human(_result(), "who is pikachu?")
|
|
assert "Pokémon" in out
|
|
assert "\\u00e9" not in out
|
|
|
|
|
|
def test_render_lists_sources_with_clean_host_path():
|
|
out = _render_query_human(_result(), "q")
|
|
# "[1] Pikachu — en.wikipedia.org/wiki/Pikachu (002.db)"
|
|
assert "[1] Pikachu" in out
|
|
assert "en.wikipedia.org/wiki/Pikachu" in out
|
|
# scheme stripped
|
|
assert "https://" not in out.split("sources (")[1] if "sources (" in out else True
|
|
assert "(002.db)" in out
|
|
|
|
|
|
def test_render_truncates_long_unverified_quotes():
|
|
long = "x" * 200
|
|
out = _render_query_human(_result(unverified_quotes=[long]), "q")
|
|
# Truncated form ends in ellipsis
|
|
assert "..." in out
|
|
# Original doesn't fully appear
|
|
assert long not in out
|
|
|
|
|
|
def test_render_omits_unverified_section_when_empty():
|
|
out = _render_query_human(_result(unverified_quotes=[]), "q")
|
|
assert "unverified" not in out.lower()
|
|
|
|
|
|
def test_render_omits_sources_section_when_empty():
|
|
out = _render_query_human(_result(sources=[]), "q")
|
|
assert "sources (" not in out
|
|
|
|
|
|
def test_render_falls_back_for_error_status():
|
|
"""no_sources / unknown_document / etc. produce a one-line status."""
|
|
out = _render_query_human(
|
|
{"status": "no_sources", "msg": "FTS5 returned no hits"},
|
|
"q",
|
|
)
|
|
assert "no_sources" in out
|
|
assert "FTS5" in out
|
|
|
|
|
|
def test_render_includes_short_cache_key_with_pointer_to_json():
|
|
out = _render_query_human(_result(), "q")
|
|
# Short prefix only — full key requires --json
|
|
assert "cache_key: 35ab7d33" in out
|
|
assert "--json" in out
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# json.dumps everywhere uses ensure_ascii=False (regression on the global pass)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_unicode_round_trips_through_print_path(capsys):
|
|
"""Pin: a result with a Pokémon-style answer prints é, not \\u00e9.
|
|
Uses argparse via build_parser to exercise the real CLI dispatch."""
|
|
from aborist.cli import build_parser
|
|
|
|
parser = build_parser()
|
|
# Build a fake result and call _cmd_query's render path indirectly.
|
|
out = _render_query_human(_result(), "who is pikachu?")
|
|
print(out)
|
|
captured = capsys.readouterr().out
|
|
assert "Pokémon" in captured
|
|
assert "\\u00e9" not in captured
|
|
|
|
|
|
def test_json_mode_also_uses_unicode():
|
|
"""The --json path should also emit unicode literals, not escapes."""
|
|
payload = {"answer": "Pokémon"}
|
|
s = json.dumps(payload, ensure_ascii=False)
|
|
assert "Pokémon" in s
|
|
assert "\\u00e9" not in s
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# capacity metrics — human render surfaces prompt_chars breakdown
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_render_shows_capacity_line_when_prompt_chars_present():
|
|
"""When the result carries `prompt_chars`, the human render
|
|
emits a one-liner summarizing prompt size + answer size so an
|
|
operator can tell at a glance whether STRICT came from a small
|
|
prompt or a context-stuffed one."""
|
|
r = _result(
|
|
prompt_chars={
|
|
"system_prompt": 1292,
|
|
"grounding_reminder": 342,
|
|
"user_question": 30,
|
|
"evidence_or_context": 20304,
|
|
"messages_total": 21996,
|
|
},
|
|
answer_chars=247,
|
|
)
|
|
out = _render_query_human(r, "what is the capital of france?")
|
|
assert "capacity:" in out
|
|
assert "21,996 chars" in out # messages_total
|
|
assert "sys 1,292" in out
|
|
assert "evidence 20,304" in out
|
|
assert "answer 247 chars" in out
|
|
|
|
|
|
def test_render_omits_capacity_line_on_legacy_results_without_prompt_chars():
|
|
"""Backwards-compat: results that pre-date capacity metrics (no
|
|
prompt_chars key) render without the capacity line — no KeyError,
|
|
no awkward 'capacity: 0 chars' noise."""
|
|
r = _result() # no prompt_chars
|
|
out = _render_query_human(r, "q")
|
|
assert "capacity:" not in out
|
|
|
|
|
|
def test_render_capacity_thousand_separators():
|
|
"""Large prompts must format with thousand-separator commas so
|
|
a 60K prompt reads as '60,000' not '60000' — operator legibility
|
|
on the daily render."""
|
|
r = _result(
|
|
prompt_chars={
|
|
"system_prompt": 1500,
|
|
"grounding_reminder": 0,
|
|
"user_question": 50,
|
|
"evidence_or_context": 60000,
|
|
"messages_total": 61550,
|
|
},
|
|
answer_chars=1234,
|
|
)
|
|
out = _render_query_human(r, "q")
|
|
assert "61,550 chars" in out
|
|
assert "60,000" in out
|
|
assert "1,234 chars" in out
|