core/tests/test_pack_grounded_comparison.py
Shay 51aad0c2cd feat(adr-0065): OOV cliff → five-tier honesty gradient (Phase 2.1 + 2.2)
Replaces the flat "I don't know — insufficient grounding" disclosure
with a deterministic gradient that names specific vocabulary gaps
and gives operators concrete next steps.

P2.1 — OOV "teach me" surface (chat/oov_surface.py).

  When the intent classifier extracts a clean subject lemma but that
  lemma is not resident in any mounted lexicon pack, the runtime now
  emits a deterministic learning-invitation surface tagged
  ``grounding_source="oov"`` instead of the universal disclosure.

  Surface format (fixed template):

    "I haven't learned '{token}' yet (intent: {intent}).
     Mounted lexicon packs: {pack_list}.
     Teach me via a reviewed PackMutationProposal."

  The OOV token passes through ``core._safe_display.safe_display``
  before persistence — user-input sanitization at the trust boundary.
  No vocabulary is invented; no domain is inferred.  Honours the
  ADR-0027 proposal-only invariant: the surface invites a reviewed
  pack mutation, never silently mutates any pack.

  Refactored ``_maybe_pack_grounded_surface`` so every existing
  intent branch (COMPARISON / CAUSE / VERIFICATION / CORRECTION /
  PROCEDURE / DEFINITION+RECALL) falls through on a None composer
  result instead of early-returning.  The OOV invitation is the
  deterministic fall-through for any clean-subject prompt whose
  subject doesn't resolve.

P2.2 — Partial-grounding tier (chat/partial_surface.py).

  When exactly one of two COMPARISON lemmas resolves, the runtime
  emits a hedged surface that grounds the known side verbatim and
  disclaims the OOV side explicitly:

    "Whatever '{oov}' is, I can ground '{known}' — pack-grounded
     ({pack_id}): {d1}; {d2}.  I cannot ground the comparison
     without learning '{oov}' — teach me via a reviewed
     PackMutationProposal."

  Tagged ``grounding_source="partial"``.  Falls through to OOV
  invitation when both lemmas are OOV, and to full pack-grounded
  COMPARISON when both resolve — partial is the middle tier in the
  five-tier gradient.

  Also normalises trailing sentence punctuation on
  intent.secondary_subject at the COMPARISON boundary so prompts
  like "Compare A and B." (with the period) still resolve B
  correctly.

Five-tier gradient (vault → teaching → pack → partial → oov → none).

Test debt retired: four pre-existing tests asserted "OOV → universal
disclosure", which is exactly the contract P2.1/P2.2 inverted.
Rewritten to the new contract.  Plus test_procedure_surface.py
gained a test for the OOV gradient on procedure intents.

Verification:
  tests/test_oov_surface.py                       22 passed
  tests/test_partial_surface.py                   16 passed
  Cognition eval byte-identical:
    public  100% / 100% / 91.7% / 100%
    holdout 100% / 100% / 83.3% / 100%
  Curated lanes all green.
2026-05-18 16:41:45 -07:00

163 lines
6.1 KiB
Python

"""ADR-0050 — pack-grounded COMPARISON surface tests.
Contract pinned here:
- ``pack_grounded_comparison_surface(a, b)`` returns a deterministic
surface composed of both lemmas + their pack ``semantic_domains``
(up to two per side) joined by the fixed connective
``"contrasts with"``. No synthesis.
- Returns ``None`` when either lemma is missing, not a pack lemma,
or when the two lemmas are identical (no contrastive evidence).
- The runtime wiring engages only when:
- the gate fires with ``source="empty_vault"``,
- ``output_language == "en"``,
- intent is ``COMPARISON``,
- both ``subject`` and ``secondary_subject`` are pack lemmas.
- ``ChatResponse.grounding_source`` and ``TurnEvent.grounding_source``
both carry ``"pack"`` on this branch.
- Refusal still takes priority — pack-grounded comparison never
bypasses safety / ethics verdict refusal.
"""
from __future__ import annotations
import pytest
from chat.pack_grounding import (
PACK_ID,
pack_grounded_comparison_surface,
)
from chat.runtime import ChatRuntime, _UNKNOWN_DOMAIN_SURFACE
# ---------------------------------------------------------------------------
# pack_grounded_comparison_surface — pure-function contracts
# ---------------------------------------------------------------------------
def test_two_known_lemmas_produce_comparison_surface() -> None:
surface = pack_grounded_comparison_surface("memory", "recall")
assert surface is not None
assert "memory" in surface
assert "recall" in surface
assert "contrasts with" in surface
assert PACK_ID in surface
assert "No session evidence yet." in surface
def test_unknown_lemma_a_returns_none() -> None:
assert pack_grounded_comparison_surface("nonexistentxyz", "memory") is None
def test_unknown_lemma_b_returns_none() -> None:
assert pack_grounded_comparison_surface("memory", "nonexistentxyz") is None
def test_identical_lemmas_return_none() -> None:
"""``Compare X and X`` carries no contrastive evidence — defer."""
assert pack_grounded_comparison_surface("memory", "memory") is None
assert pack_grounded_comparison_surface("MEMORY", "memory") is None # case-insensitive
@pytest.mark.parametrize("bad", ["", " ", None])
def test_empty_or_invalid_returns_none(bad) -> None:
assert pack_grounded_comparison_surface(bad, "memory") is None # type: ignore[arg-type]
assert pack_grounded_comparison_surface("memory", bad) is None # type: ignore[arg-type]
def test_comparison_surface_is_deterministic() -> None:
"""Same input must produce byte-identical surface on repeat."""
a = pack_grounded_comparison_surface("memory", "recall")
b = pack_grounded_comparison_surface("memory", "recall")
assert a == b
assert a is not None
def test_comparison_surface_is_order_sensitive() -> None:
"""``compare(a, b)`` and ``compare(b, a)`` produce distinct surfaces —
the connective ``"contrasts with"`` orients the comparison."""
ab = pack_grounded_comparison_surface("memory", "recall")
ba = pack_grounded_comparison_surface("recall", "memory")
assert ab is not None and ba is not None
assert ab != ba
# ---------------------------------------------------------------------------
# ChatRuntime integration — cold-start COMPARISON path
# ---------------------------------------------------------------------------
def test_cold_start_comparison_returns_pack_grounded_surface() -> None:
rt = ChatRuntime()
resp = rt.chat("Compare memory and recall")
assert resp.grounding_source == "pack"
assert "memory" in resp.surface
assert "recall" in resp.surface
assert "contrasts with" in resp.surface
def test_cold_start_comparison_with_unknown_lemma_routes_to_partial() -> None:
"""ADR-0065 / P2.2 — when exactly one COMPARISON lemma resolves
and the other is OOV, the runtime emits the partial-grounding
surface (grounds the known side, hedges the OOV side) instead of
the universal disclosure.
Pre-P2.2 this returned the flat disclosure; post-P2.2 it emits
an explicit partial surface that names which side could be
grounded and which side needs a reviewed PackMutationProposal."""
rt = ChatRuntime()
resp = rt.chat("Compare memory and zigzagxyz")
assert resp.grounding_source == "partial"
assert "memory" in resp.surface
assert "zigzagxyz" in resp.surface
assert "PackMutationProposal" in resp.surface
def test_cold_start_comparison_with_identical_lemmas_disclosure() -> None:
"""``Compare X and X`` defers to the universal disclosure."""
rt = ChatRuntime()
resp = rt.chat("Compare memory and memory")
assert resp.surface == _UNKNOWN_DOMAIN_SURFACE
assert resp.grounding_source == "none"
def test_turn_event_carries_grounding_source_on_comparison() -> None:
rt = ChatRuntime()
rt.chat("Compare memory and recall")
last_event = rt.turn_log[-1]
assert getattr(last_event, "grounding_source", None) == "pack"
def test_comparison_pack_grounded_passes_verdict_audit() -> None:
rt = ChatRuntime()
resp = rt.chat("Compare memory and recall")
assert resp.safety_verdict is not None
assert resp.ethics_verdict is not None
# ---------------------------------------------------------------------------
# Doctrine — no synthesis, no inference
# ---------------------------------------------------------------------------
def test_comparison_surface_atoms_are_verbatim_from_pack() -> None:
"""Every visible non-template token must be either the lemma or a
verbatim ``semantic_domains`` string from the pack — no rewording."""
from chat.pack_grounding import _pack_index
surface = pack_grounded_comparison_surface("memory", "recall")
assert surface is not None
index = _pack_index()
memory_domains = index["memory"][:2]
recall_domains = index["recall"][:2]
for domain in memory_domains:
assert domain in surface
for domain in recall_domains:
assert domain in surface
# The two fixed-template tokens
assert "contrasts with" in surface
assert "pack-grounded" in surface
assert "No session evidence yet." in surface