arborist/tests/test_quantifier_caps.py
russell@unturf.com 84d5b5cd76
qa(#000008): Phase 2 — model-profile caps + governance hash (dry-run)
Lands aborist/qa/model_profiles.py with two profiles:
  - adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic
      tight caps for broad intensities (ALL=8, COMPREHENSIVE=5,
      OPEN_REQUEST=5) reflecting the 2026-05-02 runaway case.
  - default
      large-reasoner-class fallback (ALL=12, COMPREHENSIVE=15,
      OPEN_REQUEST=12). Used when model_profile_id has no entry.

cap_for_intensity() resolves caps via three-source chain:
  1. policy_overrides (per-call dict, highest priority)
  2. per-model profile from PROFILES
  3. "default" profile fallback

EXPLICIT_COUNT sentinel handles SMALL_NUM_EXPLICIT and
COMPARATIVE_BOUND — cap is the question's explicit count, not a
profile-set value. Defensive fallback to MANY cap if classifier
fired the rung without extracting a count.

Four new policy fields, all folded into governance_policy_hash via
_VERIFIER_POLICY_FIELDS:
  - quantifier_guard_enabled    master kill (default True)
  - quantifier_guard_apply_caps dry-run gate (default False per
                                §10.11.3 — cap LOOKED UP and reported
                                on result, but NOT applied to the
                                verifier until operator flips True)
  - quantifier_caps_by_intensity per-call override dict
  - quantifier_guard_modes      per-mode opt-in list (default
                                ["claim_lattice_pointer",
                                 "claim_lattice"]; quote opts out)

Six-level disable hierarchy (§10.11.2) implemented:
  - Per-test:    policy={"quantifier_guard_enabled": False}
  - Per-call:    --no-quantifier-guard (Phase 4)
  - Per-phase:   each policy switch is independent
  - Per-mode:    quantifier_guard_modes filter
  - Per-model:   model_profiles.py lookup
  - Master:      governance_policy_hash invalidation on flip

Wired through both query() and runner.ask() — both compute
effective_max_claims from the (classifier_intensity, model_profile,
policy_overrides) triple and pass it as max_claims_per_answer to
the verifier. Dry-run mode keeps effective_max_claims at the policy
default (12) until apply_caps flips True.

Result dict surfaces claim_cap_applied (the LOOKED-UP cap, even in
dry-run) plus all Phase-1 quantifier fields on miss-path AND
cache-hit path so bench rows stay column-aligned.

19 new tests pin: per-model selection, EXPLICIT_COUNT sentinel,
override precedence, governance-hash invalidation on every cap
field, profile shape (all ten rungs covered), default profile
presence.
2026-05-03 07:30:28 -04:00

221 lines
8.1 KiB
Python

"""Quantifier cap table + governance hash + dry-run discipline.
Ticket #000008 Phase 2: per-model cap profiles
(``aborist/qa/model_profiles.py``) and the four policy fields that
gate cap behavior:
- ``quantifier_guard_enabled`` — master kill
- ``quantifier_guard_apply_caps`` — dry-run gate (default False)
- ``quantifier_caps_by_intensity`` — per-call override dict
- ``quantifier_guard_modes`` — per-mode opt-in list
Tests pin:
1. Per-model cap selection (Hermes profile vs default fallback).
2. EXPLICIT_COUNT sentinel — SMALL_NUM_EXPLICIT respects the
question's count.
3. Policy override beats per-model profile.
4. Governance-hash invalidation on every cap-related field flip.
5. Six-level disable hierarchy (master, dry-run, per-mode,
per-call override).
"""
from __future__ import annotations
import pytest
from aborist.qa.keys import _VERIFIER_POLICY_FIELDS, verifier_policy_hash
from aborist.qa.model_profiles import (
EXPLICIT_COUNT,
PROFILES,
cap_for_intensity,
profile_id_present,
)
# ---------------------------------------------------------------- profile lookup
def test_hermes_fp8_profile_present():
"""The Hermes-3-FP8 endpoint profile must exist verbatim — the
string is what the bench harness sends as ``model_profile_id``."""
assert profile_id_present("adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic")
def test_default_profile_fallback():
"""Unknown model ids fall through to the ``default`` profile."""
assert not profile_id_present("some-future-model-v2")
cap = cap_for_intensity(
model_profile_id="some-future-model-v2",
intensity="ALL",
)
# Default profile sets ALL = 12.
assert cap == 12
def test_hermes_caps_are_tighter_than_default():
"""Hermes broad-quantifier caps must be tighter than the default
profile — that's the whole point of the per-model split. Pin the
relationship so a future profile edit can't accidentally loosen
Hermes back to default."""
hermes_id = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"
for intensity in ("ALL", "COMPREHENSIVE", "OPEN_REQUEST", "MANY"):
hermes_cap = cap_for_intensity(
model_profile_id=hermes_id, intensity=intensity,
)
default_cap = cap_for_intensity(
model_profile_id="unknown", intensity=intensity,
)
assert hermes_cap is not None
assert default_cap is not None
assert hermes_cap <= default_cap, (
f"Hermes {intensity} cap ({hermes_cap}) must not exceed "
f"default cap ({default_cap})"
)
# ---------------------------------------------------------------- EXPLICIT_COUNT
def test_explicit_count_resolves_from_question_count():
"""SMALL_NUM_EXPLICIT cap = the explicit count from the question."""
cap = cap_for_intensity(
model_profile_id="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
intensity="SMALL_NUM_EXPLICIT",
explicit_count=3,
)
assert cap == 3
def test_explicit_count_falls_back_when_missing():
"""If the classifier reported SMALL_NUM_EXPLICIT but didn't
extract a count (defensive — shouldn't happen), fall back to the
profile's MANY cap rather than crash."""
cap = cap_for_intensity(
model_profile_id="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
intensity="SMALL_NUM_EXPLICIT",
explicit_count=None,
)
# Hermes MANY = 8.
assert cap == 8
def test_comparative_bound_uses_explicit_count():
"""COMPARATIVE_BOUND also reads explicit_count (the upper bound
extracted by the classifier — for `between A and B` the upper)."""
cap = cap_for_intensity(
model_profile_id="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
intensity="COMPARATIVE_BOUND",
explicit_count=7,
)
assert cap == 7
# ---------------------------------------------------------------- override
def test_policy_override_beats_per_model_profile():
"""A per-call ``quantifier_caps_by_intensity`` dict wins over the
profile table. Lets an operator override caps without editing
model_profiles.py."""
cap = cap_for_intensity(
model_profile_id="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
intensity="ALL",
policy_overrides={"ALL": 4},
)
assert cap == 4
def test_policy_override_partial_falls_back_to_profile():
"""An override dict with one intensity doesn't suppress the
others — the profile still answers for unspecified intensities."""
cap_all = cap_for_intensity(
model_profile_id="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
intensity="ALL",
policy_overrides={"COMPREHENSIVE": 3}, # only COMPREHENSIVE overridden
)
# ALL still pulls from Hermes profile (8).
assert cap_all == 8
def test_unknown_intensity_returns_none():
"""Caller should treat None as 'no cap; use the policy default'."""
cap = cap_for_intensity(
model_profile_id="adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
intensity="MADE_UP_INTENSITY",
)
assert cap is None
# ---------------------------------------------------------------- governance hash
@pytest.mark.parametrize("field", [
"quantifier_guard_enabled",
"quantifier_guard_apply_caps",
"quantifier_caps_by_intensity",
"quantifier_guard_modes",
])
def test_each_quantifier_field_is_in_verifier_policy_fields(field):
"""All four Phase-2 policy fields must be in
``_VERIFIER_POLICY_FIELDS`` so flipping any of them invalidates
prior cache records on lookup. Without this binding, an operator
could change caps and silently re-use stale cached verdicts."""
assert field in _VERIFIER_POLICY_FIELDS
def test_governance_hash_changes_when_apply_caps_flips():
"""Dry-run → cap-on flip MUST bump governance_policy_hash. Same
classifier output, different verifier behavior — distinct
cache_key per §10.11.2 Level 6."""
base_policy = dict.fromkeys(_VERIFIER_POLICY_FIELDS, "default")
base_policy["quantifier_guard_apply_caps"] = False
h_dry = verifier_policy_hash(base_policy)
base_policy["quantifier_guard_apply_caps"] = True
h_apply = verifier_policy_hash(base_policy)
assert h_dry != h_apply
def test_governance_hash_changes_when_caps_dict_changes():
"""Editing the per-call override dict must bump the hash too."""
base_policy = dict.fromkeys(_VERIFIER_POLICY_FIELDS, "default")
base_policy["quantifier_caps_by_intensity"] = {}
h_empty = verifier_policy_hash(base_policy)
base_policy["quantifier_caps_by_intensity"] = {"ALL": 4}
h_override = verifier_policy_hash(base_policy)
assert h_empty != h_override
def test_governance_hash_changes_when_master_switch_flips():
base_policy = dict.fromkeys(_VERIFIER_POLICY_FIELDS, "default")
base_policy["quantifier_guard_enabled"] = True
h_on = verifier_policy_hash(base_policy)
base_policy["quantifier_guard_enabled"] = False
h_off = verifier_policy_hash(base_policy)
assert h_on != h_off
def test_governance_hash_changes_when_mode_list_changes():
base_policy = dict.fromkeys(_VERIFIER_POLICY_FIELDS, "default")
base_policy["quantifier_guard_modes"] = ["claim_lattice_pointer", "claim_lattice"]
h_default = verifier_policy_hash(base_policy)
base_policy["quantifier_guard_modes"] = ["claim_lattice_pointer"]
h_pointer_only = verifier_policy_hash(base_policy)
assert h_default != h_pointer_only
# ---------------------------------------------------------------- profile shape
def test_profile_keys_include_all_ten_rungs():
"""Every profile must cover all ten intensity rungs so cap
lookup can't fall through to the default profile due to a
missing key."""
expected_rungs = {
"ABSENT", "SINGULAR", "PROPORTIONAL",
"SMALL_NUM_EXPLICIT", "COMPARATIVE_BOUND",
"FEW", "MANY", "ALL", "COMPREHENSIVE", "OPEN_REQUEST",
}
for profile_id, profile in PROFILES.items():
missing = expected_rungs - set(profile.keys())
assert not missing, f"{profile_id} missing rungs: {missing}"
def test_default_profile_present():
"""Lookup-fallback target must exist or unknown models would
return None on every call."""
assert "default" in PROFILES