feat(search): add calibrated retrieval and LangChain knowledge tool
Add exact and semantic evidence admission, measured calibration, bounded tool execution, and independent held-out evaluation preparation with regression coverage and operator guides. Real-provider quality gates remain pending. Validation: 1300 provider-free tests passed; 3 Windows symlink skips. PostgreSQL acceptance not rerun because TEST_DATABASE_URL is unset.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""Gate regressions; synthetic outcomes prove scoring only, not model quality."""
|
||||
import pytest
|
||||
from tests.phase4_gate import assess, gate, validate_manifest
|
||||
|
||||
def fixture():
|
||||
cases = [dict(id=str(i), answerable=i < 20, critical=i == 0) for i in range(26)]
|
||||
observations = {c["id"]: dict(status="ok" if c["answerable"] else "no_evidence",
|
||||
passed=True, sources_valid=True) for c in cases}
|
||||
return cases, observations
|
||||
|
||||
def test_exact_18_with_all_critical_and_unsupported_passes():
|
||||
cases, rows = fixture()
|
||||
rows["18"]["passed"] = rows["19"]["passed"] = False
|
||||
assert gate(cases, rows, authorization_confirmed=True)["passed"]
|
||||
rows["17"]["passed"] = False
|
||||
assert not gate(cases, rows, authorization_confirmed=True)["passed"]
|
||||
|
||||
@pytest.mark.parametrize("failed", ["0", "20"])
|
||||
def test_critical_or_unsupported_failure_vetoes_score(failed):
|
||||
cases, rows = fixture()
|
||||
rows[failed]["passed"] = False
|
||||
assert not gate(cases, rows, authorization_confirmed=True)["passed"]
|
||||
|
||||
def test_incomplete_or_unauthorized_run_cannot_pass():
|
||||
cases, rows = fixture()
|
||||
assert not gate(cases, rows, authorization_confirmed=False)["passed"]
|
||||
rows["4"]["sources_valid"] = False
|
||||
assert not gate(cases, rows, authorization_confirmed=True)["passed"]
|
||||
del rows["4"]
|
||||
assert not gate(cases, rows, authorization_confirmed=True)["passed"]
|
||||
assert not gate(cases[:-1], rows, authorization_confirmed=True)["passed"]
|
||||
|
||||
def test_error_never_counts_as_unsupported_success():
|
||||
case = dict(answerable=False)
|
||||
assert not assess(case, dict(status="error", sources=[]), {})
|
||||
assert not assess(case, dict(status="no_evidence", sources=[dict(text="x")]), {})
|
||||
|
||||
def test_qualifiers_must_share_the_bound_original_chunk():
|
||||
case = dict(answerable=True, original_sha256="hash",
|
||||
evidence_groups=[["75%", "more than", "25 units"]])
|
||||
source = dict(document_id="doc", text="more than 75% with 25 units")
|
||||
assert assess(case, dict(status="ok", sources=[source]), {"hash": "doc"})
|
||||
for changed in [dict(source, document_id="foreign"), dict(source, text="at least 75% with 25 units")]:
|
||||
assert not assess(case, dict(status="ok", sources=[changed]), {"hash": "doc"})
|
||||
|
||||
def test_draft_cannot_run():
|
||||
with pytest.raises(ValueError, match="Frozen"):
|
||||
validate_manifest(dict(status="incomplete_not_frozen", cases=[]))
|
||||
|
||||
|
||||
def test_gate_rejects_error_even_if_assessment_flag_is_incorrect():
|
||||
cases, rows = fixture()
|
||||
rows["20"]["status"] = "error"
|
||||
assert not gate(cases, rows, authorization_confirmed=True)["passed"]
|
||||
|
||||
def test_provider_budget_counts_failed_calls_and_prevents_extra_transport():
|
||||
from tests.evaluate_search import BudgetProvider
|
||||
class Provider:
|
||||
calls = 0
|
||||
def embed(self, texts):
|
||||
self.calls += 1
|
||||
raise RuntimeError("provider unavailable")
|
||||
provider = Provider()
|
||||
bounded = BudgetProvider(provider, 1)
|
||||
with pytest.raises(RuntimeError, match="unavailable"):
|
||||
bounded.embed(["query"])
|
||||
with pytest.raises(RuntimeError, match="budget"):
|
||||
bounded.embed(["query"])
|
||||
assert bounded.calls == provider.calls == 1
|
||||
|
||||
def test_incomplete_annotations_fail_before_database_or_provider(tmp_path, monkeypatch):
|
||||
import json
|
||||
import tests.evaluate_search as evaluator
|
||||
draft = tmp_path / "draft.json"
|
||||
draft.write_text(json.dumps(dict(status="incomplete_not_frozen", cases=[])), encoding="utf-8")
|
||||
def forbidden(*args, **kwargs):
|
||||
pytest.fail("Incomplete annotations reached database/provider setup")
|
||||
monkeypatch.setattr(evaluator, "create_database", forbidden)
|
||||
output = tmp_path / "new.jsonl"
|
||||
assert evaluator.main(["--cases", str(draft), "--bindings", str(tmp_path / "missing"),
|
||||
"--calibration", str(tmp_path / "missing"),
|
||||
"--user-id", "00000000-0000-0000-0000-000000000001",
|
||||
"--output", str(output), "--allow-provider-calls",
|
||||
"--provider-call-budget", "26"]) == 1
|
||||
assert not output.exists()
|
||||
Reference in New Issue
Block a user