core/core/reliability_gate/gate.py
Shay 8775765881 feat(adr-0175-phase1): reliability ledger + attempt/refuse gate substrate
ADR-0175 Phase 1 — standalone, deterministic, zero serving change. Nothing in
the serving/eval path imports it.

core/reliability_gate/:
- floor.py: conservative_floor(s,k) — pinned one-sided Wilson lower bound over
  COMMITTED trials. z=2.576, N_MIN=10; range [0,1) (never exactly 1.0); float64
  rounded half-to-even to 1e-9 for cross-backend replay. Perfect record reduces
  to k/(k+z²) (earned by volume).
- ledger.py: ClassTally — immutable per-class counts; reliability = commitment
  precision (refusals excluded so coverage never penalizes reliability);
  t2_precision over the anchor set; coverage tracked separately.
- ceilings.py: Action{PRACTICE,PROPOSE,SERVE} + Ceilings — human-set θ
  (practice=0, propose=.85, serve=.99). Frozen; with_override returns a NEW
  instance (no in-place self-authorization).
- gate.py: license_for() — deterministic gate, measured/required≥1 (≡ measured≥
  required; required=0 ⟹ always). Pure; never mutates/emits ceilings.

34 tests, each ADR invariant exercised by a test that fails under its violation:
#3 determinism/replay (idempotent, pre-rounded, deterministic decisions),
#4 no self-authorization (frozen ceilings; gate never emits/mutates them),
#1 proxy (zero serving coupling). Plus the §4a worked examples (38 clean
commitments clear propose; one wrong in 40 drops below; serve needs ~657).

Verified: 34/34 pass; architectural invariants 40/40; smoke 67/67; ruff clean;
no serving/eval import of the package.
2026-05-28 15:04:48 -07:00

72 lines
2.3 KiB
Python

"""ADR-0175 §3 — the deterministic attempt/refuse gate.
``license_for`` decides whether an action is permitted for a class:
measured_reliability(class) / θ_required(action, class) ≥ 1
Equivalently ``measured ≥ required`` (and ``required == 0`` -> always licensed),
which avoids a divide-by-zero on the sealed-practice ceiling. The ratio is kept
as an inspectable field. Pure and deterministic: never mutates or emits the
``Ceilings`` it is given (invariant #4), identical inputs -> identical decision
(invariant #3).
"""
from __future__ import annotations
from dataclasses import dataclass
from core.reliability_gate.ceilings import Action, Ceilings
from core.reliability_gate.ledger import ClassTally
# Which earned number gates an action.
_CHECKERS: frozenset[str] = frozenset({"reliability", "t2_precision"})
@dataclass(frozen=True, slots=True)
class LicenseDecision:
"""Inspectable result of the gate. Carries the numbers behind the verdict."""
class_name: str
action: Action
checker: str
measured: float
required: float
ratio: float # measured / required; +inf when required == 0
licensed: bool
def license_for(
tally: ClassTally,
action: Action,
ceilings: Ceilings,
*,
checker: str = "reliability",
) -> LicenseDecision:
"""Decide whether ``action`` is licensed for ``tally``'s class.
``checker`` selects the earned number that gates this action:
``"reliability"`` (commitment precision) or ``"t2_precision"`` (Tier-2
self-verification trust, used for widening past gold).
"""
if checker not in _CHECKERS:
raise ValueError(f"unknown checker {checker!r}; expected one of {sorted(_CHECKERS)}")
measured = tally.reliability if checker == "reliability" else tally.t2_precision
required = ceilings.required(tally.class_name, action)
if required <= 0.0:
licensed = True
ratio = float("inf")
else:
# measured / required ≥ 1 ⟺ measured ≥ required (both pre-rounded to 1e-9)
licensed = round(measured, 9) >= round(required, 9)
ratio = round(measured / required, 9)
return LicenseDecision(
class_name=tally.class_name,
action=action,
checker=checker,
measured=measured,
required=required,
ratio=ratio,
licensed=licensed,
)