The final AGI-spine step (A INSTRUMENT → B WIRE → C DEEPEN → D CLOSE → E ESTIMATION).
The engine may now SERVE a DISCLOSED estimate for a query it would otherwise refuse —
but only for a predicate-class that has measured itself reliable, and never as fact.
This executes the ADR-0206 §5 cognition-path widening: the bridge's LICENSE node
(reliability_gate.license_for), previously "built — not yet called from serving", is now
called. govern_response returns APPROXIMATE iff a genuine licensed Action.SERVE
LicenseDecision is passed (STRICT for every other input — so every existing serving call
site is byte-identical); shape_surface DISCLOSES the estimate as "[approximate] …".
Mechanism:
- generate/determine/estimate.py — a BLIND converse-guesser: told p(a,b), asked p(b,a),
it commits the converse. It never reads the pack's symmetry metadata; whether the guess
is right is MEASURED, not assumed.
- evals/determination_estimation/ — the gold lane: run_practice (sealed, ADR-0199) folds
the converse-guesser over symmetric (sibling_of) vs directed (parent_of) cases, scored
against the pack's graph.edge.symmetric truth (gold independent of the solver). The gate
DISCRIMINATES: sibling_of earns SERVE (660 correct → Wilson floor 0.990046 ≥ θ_SERVE),
parent_of does not (660 wrong → 0.0). The license is earned by VOLUME — 657 perfect
commits is the exact θ_SERVE=0.99 threshold (656 is below).
- generate/determine/data/estimation_ledger.json — the ratified committed ledger,
hash-verified on load (a hand-edited ledger raises RatifiedLedgerError); it IS the
deterministic sealed-practice output (a GSM8K-style --check test pins this).
- chat/runtime.py — when a converse query is refused and the class holds a SERVE license,
the disclosed estimate is surfaced through the bridge (gated by config.estimation_enabled,
default OFF; only meaningful with accrue_realized_knowledge).
Invariants:
- wrong=0 by construction — an estimate is ALWAYS disclosed ([approximate]), never a silent
commit (UNVERIFIED_POSSIBLE is never in APPROXIMATE's admissible set), and only a genuine
ratified license widens (a forged {"licensed":True} dict / a PROPOSE license / an
unlicensed SERVE all stay STRICT). Defense-in-depth: type-gate ∧ admissible-set ∧
hardcoded disclosed state.
- never self-authored — ceilings stay at safe defaults (θ_SERVE=0.99); the engine cannot
raise its own bar. The ledger is sealed practice, hash-verified.
- session/serving only — no corpus/pack/identity/proposal/vault mutation; the HITL teaching
path is untouched. Deterministic; no clock/random.
- byte-identical for every non-E turn (the 2643 govern_response call passes no license).
Out of scope (separate ADR-0206 §5 PRs): the math-serving seam (select_self_verified,
touches the sealed metric), SITUATE (stakes), and the live FEED-BACK loop.
Verified green: smoke (90), architectural invariants (56), response_governance (321,
incl. the new license-gated widening test), the determination-estimation lane (12), and
the B/D/determine regression net. Four-lens adversarial review (disclosure/wrong=0,
calibration integrity, byte-identity, boundary/determinism): all held. Design:
docs/analysis/E-estimation-design-2026-06-06.md.
206 lines
8.4 KiB
Python
206 lines
8.4 KiB
Python
"""Step E — the converse-guess estimator + its calibration gold lane.
|
|
|
|
The estimator is BLIND (never reads symmetry metadata); the reliability gate decides
|
|
licensing from MEASURED commitment precision. The load-bearing properties: the gate
|
|
DISCRIMINATES (a symmetric predicate's converse-guess earns SERVE; a directed one does
|
|
not), the SERVE license is earned by VOLUME (the Wilson floor binds at 657), and the
|
|
serving-side estimator fires only on a told converse.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace as _dc_replace
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from chat.runtime import ChatRuntime
|
|
from core.config import DEFAULT_CONFIG
|
|
from core.reliability_gate import N_MIN
|
|
from evals.determination_estimation import (
|
|
LICENSED_PREDICATE,
|
|
REFUSED_PREDICATE,
|
|
build_ledger,
|
|
load_symmetric_predicates,
|
|
reliability_at,
|
|
run,
|
|
)
|
|
from generate.determine.estimate import ConverseEstimate, converse_class_name, estimate_converse
|
|
from generate.meaning_graph.relational import comprehend_relational, load_relational_pack_lemmas
|
|
from generate.realize import realize_comprehension
|
|
from session.context import SessionContext
|
|
|
|
_HIGH = 10**9
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def vocab_persona():
|
|
rt = ChatRuntime(no_load_state=True)
|
|
return rt._context.vocab, rt._context.persona
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def rel_lemmas():
|
|
return load_relational_pack_lemmas()
|
|
|
|
|
|
def _ctx(vocab_persona) -> SessionContext:
|
|
vocab, persona = vocab_persona
|
|
return SessionContext(vocab=vocab, persona=persona, vault_reproject_interval=_HIGH)
|
|
|
|
|
|
def _tell_relational(text: str, ctx: SessionContext, lemmas) -> None:
|
|
realize_comprehension(comprehend_relational(text, lemmas), ctx)
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# The gate discriminates (the whole point of E)
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def test_gate_discriminates_symmetric_from_directed() -> None:
|
|
report = run()
|
|
assert report["gate_discriminates"] is True
|
|
licensed = report["classes"][converse_class_name(LICENSED_PREDICATE)]
|
|
refused = report["classes"][converse_class_name(REFUSED_PREDICATE)]
|
|
assert licensed["serve_licensed"] is True
|
|
assert refused["serve_licensed"] is False
|
|
# The symmetric class is right every time; the directed class is wrong every time.
|
|
assert licensed["tally"]["wrong"] == 0 and licensed["tally"]["correct"] > 0
|
|
assert refused["tally"]["correct"] == 0 and refused["tally"]["wrong"] > 0
|
|
|
|
|
|
def test_serve_license_is_earned_by_volume() -> None:
|
|
# Below the Wilson volume floor a PERFECT symmetric record is still NOT SERVE-licensed.
|
|
assert reliability_at(LICENSED_PREDICATE, 656) < 0.99
|
|
assert reliability_at(LICENSED_PREDICATE, 657) >= 0.99
|
|
# And below N_MIN no reliability is claimed at all.
|
|
assert reliability_at(LICENSED_PREDICATE, N_MIN - 1) == 0.0
|
|
|
|
|
|
def test_run_is_deterministic() -> None:
|
|
a, b = run(), run()
|
|
assert a == b
|
|
|
|
|
|
def test_gold_symmetry_matches_pack() -> None:
|
|
sym = load_symmetric_predicates()
|
|
assert LICENSED_PREDICATE in sym # sibling_of — graph.edge.symmetric
|
|
assert REFUSED_PREDICATE not in sym # parent_of — graph.edge.directed
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# The serving-side estimator
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def test_estimate_fires_only_on_a_told_converse(vocab_persona, rel_lemmas) -> None:
|
|
ctx = _ctx(vocab_persona)
|
|
_tell_relational("Alice is the sibling of Bob.", ctx, rel_lemmas)
|
|
# Told sibling_of(alice, bob); the converse query sibling_of(bob, alice) gets a guess.
|
|
est = estimate_converse(ctx, "sibling_of", "bob", "alice")
|
|
assert isinstance(est, ConverseEstimate)
|
|
assert est.answer is True
|
|
assert est.basis == "estimate_converse"
|
|
assert est.subject == "bob" and est.object == "alice"
|
|
assert est.told_structure_key # ties the guess to the realized fact
|
|
|
|
# No told converse → no guess (the estimator never invents evidence).
|
|
assert estimate_converse(ctx, "sibling_of", "carol", "dave") is None
|
|
|
|
|
|
def test_estimate_is_blind_to_symmetry(vocab_persona, rel_lemmas) -> None:
|
|
# The estimator commits the converse for a DIRECTED predicate too — being wrong
|
|
# there is exactly what the gate measures and refuses to license.
|
|
ctx = _ctx(vocab_persona)
|
|
_tell_relational("Alice is the parent of Bob.", ctx, rel_lemmas)
|
|
est = estimate_converse(ctx, "parent_of", "bob", "alice")
|
|
assert isinstance(est, ConverseEstimate) and est.answer is True
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# E-2 — the ratified ledger artifact + serving-side license
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def test_ratified_ledger_matches_sealed_practice() -> None:
|
|
# Provenance (the GSM8K-style --check): the committed artifact IS the deterministic
|
|
# sealed-practice output, not a hand-edited ledger.
|
|
from generate.determine.estimation_license import load_ratified_ledger
|
|
|
|
committed = load_ratified_ledger()
|
|
fresh = build_ledger()
|
|
assert {k: (t.correct, t.wrong, t.refused) for k, t in committed.items()} == {
|
|
k: (t.correct, t.wrong, t.refused) for k, t in fresh.items()
|
|
}
|
|
|
|
|
|
def test_serve_license_from_ratified_ledger() -> None:
|
|
from generate.determine.estimation_license import serve_license
|
|
|
|
assert serve_license(LICENSED_PREDICATE).licensed is True # sibling_of — earned SERVE
|
|
assert serve_license(REFUSED_PREDICATE).licensed is False # parent_of — never
|
|
assert serve_license("nonexistent_predicate") is None # no committed evidence → refuse
|
|
|
|
|
|
def test_tampered_ledger_is_rejected(tmp_path, monkeypatch) -> None:
|
|
# A hand-edited ledger (counts changed without the matching hash) must be REJECTED,
|
|
# never silently trusted — only the sealed-practice output is admissible.
|
|
import json as _json
|
|
|
|
import generate.determine.estimation_license as mod
|
|
from generate.determine.estimation_license import RatifiedLedgerError, load_ratified_ledger
|
|
|
|
good = _json.loads(mod._LEDGER_PATH.read_text(encoding="utf-8"))
|
|
good["classes"][converse_class_name(REFUSED_PREDICATE)]["correct"] = 999 # forge a SERVE
|
|
forged_path = tmp_path / "estimation_ledger.json"
|
|
forged_path.write_text(_json.dumps(good), encoding="utf-8")
|
|
|
|
load_ratified_ledger.cache_clear()
|
|
monkeypatch.setattr(mod, "_LEDGER_PATH", forged_path)
|
|
try:
|
|
with pytest.raises(RatifiedLedgerError, match="content_sha256 mismatch"):
|
|
load_ratified_ledger()
|
|
finally:
|
|
load_ratified_ledger.cache_clear() # don't poison other tests' cached load
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# E-3 — the runtime wire (chat turn → disclosed estimate, license-gated)
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
def _estimation_runtime(tmp_path: Path, *, enabled: bool = True) -> ChatRuntime:
|
|
cfg = _dc_replace(
|
|
DEFAULT_CONFIG,
|
|
estimation_enabled=enabled,
|
|
accrue_realized_knowledge=True,
|
|
persist_session_state=True,
|
|
)
|
|
return ChatRuntime(config=cfg, engine_state_path=tmp_path)
|
|
|
|
|
|
def test_licensed_converse_is_served_disclosed_approximate(tmp_path) -> None:
|
|
rt = _estimation_runtime(tmp_path)
|
|
rt.chat("Alice is the sibling of Bob.") # told sibling_of(alice, bob)
|
|
resp = rt.chat("Is Bob the sibling of Alice?") # converse — DETERMINE refuses
|
|
assert resp.reach_level == "approximate"
|
|
assert resp.surface.startswith("[approximate]") # DISCLOSED, never asserted as fact
|
|
assert "bob" in resp.surface and "alice" in resp.surface
|
|
|
|
|
|
def test_unlicensed_converse_stays_strict_refusal(tmp_path) -> None:
|
|
rt = _estimation_runtime(tmp_path)
|
|
rt.chat("Alice is the parent of Bob.") # told parent_of(alice, bob) — DIRECTED
|
|
resp = rt.chat("Is Bob the parent of Alice?")
|
|
# parent_of's converse-guess is not SERVE-licensed → no widening, honest refusal.
|
|
assert resp.reach_level == "strict"
|
|
assert not resp.surface.startswith("[approximate]")
|
|
|
|
|
|
def test_estimation_flag_off_is_strict(tmp_path) -> None:
|
|
rt = _estimation_runtime(tmp_path, enabled=False)
|
|
rt.chat("Alice is the sibling of Bob.")
|
|
resp = rt.chat("Is Bob the sibling of Alice?")
|
|
assert resp.reach_level == "strict"
|
|
assert not resp.surface.startswith("[approximate]")
|