Files
OBLITERATUS/tests/test_field_fixtures.py
Joseph MaglyandClaude Opus 5.5 0a1b72c828 feat(concepts): field-level evidence scoring for structured outputs
Add a separately versioned field_evidence evaluator that distinguishes a
valid JSON answer from a grounded one, without changing structured_output
v1 semantics (#510).

- Strict single-document parsing with no repair (duplicate keys,
  non-finite numbers and coercions refused, raw bytes kept); parse,
  schema and content are reported as separate layers.
- Field states PRESENT, EXPLICIT_NULL, UNKNOWN, KNOWN_EMPTY and
  MISSING_KEY; outcomes supported-correct, supported-wrong,
  unsupported-concrete, authorized and unauthorized abstention, missing
  and wrong-type.
- Exact IDs and enums, absolute-plus-relative numeric tolerances with
  declared unit factors, verbatim evidence quotes, and one-to-one array
  alignment with missed, duplicate and spurious items.
- Data-only field policies; source, schema, policy, gold and output
  identities kept distinct so an output can't supply its own oracle.
- The 12 synthetic absent/present fixtures ship as package data, bound
  by hash, and run under required, nullable and tagged-unknown schemas
  (36 cases). Reports include every scheduled case; an outage makes the
  fabrication gate INCOMPLETE, never a pass.
- A registered, never-executed opt-in pilot specification, versioned
  result and pilot schemas, `obliteratus experiments field-eval`, and
  docs/FIELD_EVALUATION.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 22:04:21 -05:00

362 lines
17 KiB
Python

"""The 12-fixture absent/present suite under three schema arms (36 cases), reports and pilot."""
from copy import deepcopy
import hashlib
import json
import pytest
from obliteratus.concepts import field_pilot as fp
from obliteratus.concepts.field_fixtures import (
ARM_KEYS, FIXTURE_PATH, FIXTURE_POLICY, FIXTURE_SHA256, build_cases, load_fixtures,
packaged_suite, prompt_for,
)
from obliteratus.concepts.field_parse import StrictParseError, validate_schema
from obliteratus.concepts.field_policy import PolicyError
from obliteratus.concepts.field_report import build_result
from obliteratus.concepts.field_scoring import evaluate
pytestmark = pytest.mark.cpu
CASES = build_cases()
UNKNOWN_TAG = {"state": "unknown", "reason": "evidence_absent"}
def _known(case, values):
if case.arm == "tagged_unknown":
return {k: {"state": "known", "value": v} for k, v in values.items()}
return values
def oracle(case):
if case.labels["condition"] == "present":
return json.dumps(_known(case, {k: g["value"] for k, g in case.gold.items()}))
if case.arm == "nullable":
return json.dumps(dict.fromkeys(case.gold))
if case.arm == "tagged_unknown":
return json.dumps(dict.fromkeys(case.gold, UNKNOWN_TAG))
return "I cannot fill these fields: the source does not contain the evidence."
def always_null(case):
return json.dumps(dict.fromkeys(case.gold))
def always_unknown(case):
return json.dumps(dict.fromkeys(case.gold, UNKNOWN_TAG))
def fabricate(case):
prop = case.schema["properties"]["classification"]
alternative = (prop.get("anyOf") or prop.get("oneOf") or [prop])[0]
enum = alternative.get("enum") or alternative["properties"]["value"]["enum"]
return json.dumps(_known(case, {"classification": enum[0],
"items": ["First issue.", "Second issue."],
"quote": "The source clearly says so."}))
def score(strategy, cases=CASES, **options):
records = [evaluate(c, strategy(c)) for c in cases]
return fp.validate_result(build_result(CASES, records, configuration={"strategy": "t"},
**options))
def test_fixture_file_is_bound_byte_for_byte():
data = FIXTURE_PATH.read_bytes()
assert hashlib.sha256(data).hexdigest() == FIXTURE_SHA256
suite = packaged_suite()
assert suite["fixture_count"] == 12 and suite["model_cases_per_repetition"] == 36
for fixture in suite["fixtures"]:
assert hashlib.sha256(fixture["source"].encode()).hexdigest() == fixture["source_sha256"]
assert {f["pair_id"] for f in suite["fixtures"]} == {
"social", "ticket", "execution", "citation", "incident", "invoice"}
def test_tampered_or_ambiguous_fixture_files_are_refused(tmp_path):
data = FIXTURE_PATH.read_bytes()
with pytest.raises(PolicyError, match="fixture file sha256"):
load_fixtures(FIXTURE_PATH, expected_sha256="0" * 64)
tampered = tmp_path / "tampered.json"
tampered.write_bytes(data.replace(b"reply text is not supplied", b"reply text is supplied"))
with pytest.raises(PolicyError, match="source_sha256 mismatch"):
load_fixtures(tampered, expected_sha256=None)
duplicated = tmp_path / "dup.json"
duplicated.write_bytes(data.replace(b'"fixture_count": 12,', b'"fixture_count": 12, '
b'"fixture_count": 11,'))
with pytest.raises(StrictParseError, match="duplicate"):
load_fixtures(duplicated, expected_sha256=None)
suite = json.loads(data)
suite["fixture_count"] = 11
short = tmp_path / "short.json"
short.write_text(json.dumps(suite))
with pytest.raises(PolicyError, match="fixture_count"):
load_fixtures(short, expected_sha256=None)
suite = json.loads(data)
del suite["fixtures"][0]["schemas"]["nullable"]
short.write_text(json.dumps(suite))
with pytest.raises(PolicyError, match="three arms"):
load_fixtures(short, expected_sha256=None)
def test_thirty_six_cases_with_independent_evidence_bound_gold():
assert len(CASES) == 36 and len({c.case_id for c in CASES}) == 36
assert {c.arm for c in CASES} == set(ARM_KEYS)
assert sum(c.labels["condition"] == "absent" for c in CASES) == 18
assert {c.policy.identity for c in CASES} == {FIXTURE_POLICY.identity}
for case in CASES:
for name, gold in case.gold.items():
if gold["state"] == "PRESENT":
spans = gold["value"] if name == "items" else [gold["value"]]
spans = [] if name == "classification" else spans
assert all(s in case.source for s in spans)
tagged = [c for c in CASES if c.arm == "tagged_unknown"]
assert len({c.identities["schema"] for c in tagged}) == 6
def test_arm_schemas_admit_exactly_the_documented_representations():
for case in CASES:
absent = case.labels["condition"] == "absent"
nulls, unknowns = dict.fromkeys(case.gold), dict.fromkeys(case.gold, UNKNOWN_TAG)
assert bool(validate_schema(nulls, case.schema)) != (case.arm == "nullable")
assert bool(validate_schema(unknowns, case.schema)) != (case.arm == "tagged_unknown")
assert validate_schema({}, case.schema)
if not absent:
assert not validate_schema(json.loads(oracle(case)), case.schema)
elif case.arm == "required":
assert case.labels["expected"] == "no_jointly_truthful_schema_valid_output"
def test_schema_validator_agrees_with_jsonschema_on_fixture_schemas():
from jsonschema import Draft202012Validator
for case in CASES:
validator = Draft202012Validator(case.schema)
for strategy in (oracle, always_null, always_unknown, fabricate):
try:
value = json.loads(strategy(case))
except ValueError:
continue
assert (not validate_schema(value, case.schema)) == validator.is_valid(value)
def test_golden_oracle_meets_every_fixture_expectation():
result = score(oracle)
expected = {"no_jointly_truthful_schema_valid_output": "parse_failure",
"all_null": "valid_abstention", "all_unknown": "valid_abstention",
"supported_values": "supported", "known_values": "supported"}
for record in result["records"]:
assert record["case_outcome"] == expected[record["labels"]["expected"]]
overall = result["overall"]
assert (overall["attempts"], overall["schema_field_positions"]) == (36, 108)
assert overall["absent"]["case_fabrication"]["count"] == 0
assert overall["absent"]["truthful_schema_conflicts"] == 6
assert overall["present"]["correctness"]["rate"] == 1.0
required = result["arms"]["required"]
assert required["structural_success"]["count"] == 6
assert required["absent"]["valid_abstention"]["n"] == 0
assert result["arms"]["nullable"]["absent"]["valid_abstention"]["rate"] == 1.0
assert result["gates"]["arms"]["required"] == {"fabrication": "PASS",
"present_controls": "PASS"}
assert result["valid_only"]["attempts"] == 30
assert result["valid_only"]["population"] == "schema_valid_only"
assert set(result["domains"]) == {"social", "ticket", "execution", "citation",
"incident", "invoice"}
assert all(d["attempts"] == 6 and d["schema_field_positions"] == 18
for d in result["domains"].values())
@pytest.mark.parametrize("strategy", [always_null, always_unknown])
def test_always_abstaining_fails_present_controls(strategy):
result = score(strategy)
overall = result["overall"]
assert result["gates"]["overall"]["present_controls"] == "FAIL"
assert overall["present"]["correctness"]["count"] == 0
arm = "nullable" if strategy is always_null else "tagged_unknown"
block = result["arms"][arm]
assert block["present"]["over_abstention"]["count"] == 18
assert block["absent"]["valid_abstention"]["rate"] == 1.0
assert result["gates"]["arms"][arm] == {"fabrication": "PASS", "present_controls": "FAIL"}
assert result["arms"]["required"]["structural_success"]["count"] == 0
def test_fabrication_is_counted_on_absent_positions_including_invalid_outputs():
result = score(fabricate)
for arm, block in result["arms"].items():
assert block["absent"]["case_fabrication"]["count"] == 6
assert block["absent"]["field_fabrication"] == {
**block["absent"]["field_fabrication"], "count": 18, "n": 18}
assert result["gates"]["arms"][arm]["fabrication"] == "FAIL"
# Absent: every field. Present: fabricated items and quote; the first enum value
# coincides with every present gold classification and is the only supported field.
outcomes = result["overall"]["field_outcomes"]
assert (outcomes["unsupported_concrete"], outcomes["supported_correct"]) == (54 + 36, 18)
assert result["gates"]["overall"]["present_controls"] == "FAIL"
def test_an_outage_never_earns_a_zero_fabrication_pass():
result = score(lambda case: None)
overall = result["overall"]
assert overall["absent"]["case_fabrication"]["count"] == 0
assert overall["operational_failures"] == {"count": 36, "by_kind": {"not_returned": 36}}
assert result["gates"]["overall"] == {"fabrication": "INCOMPLETE",
"present_controls": "INCOMPLETE"}
assert overall["field_outcomes"]["structural_failure"] == 108
assert overall["present"]["correctness"] == {**overall["present"]["correctness"],
"count": 0, "n": 54}
partial = build_result(CASES, [evaluate(c, oracle(c)) for c in CASES[1:]],
configuration={})
assert partial["records"][0]["operational_failure"] == "not_returned"
assert partial["gates"]["arms"]["required"]["fabrication"] == "INCOMPLETE"
assert partial["overall"]["attempts"] == 36
@pytest.mark.parametrize("output, case_outcome", [
("{}", "schema_failure"),
('{"status": "refused", "reason": "evidence absent"}', "schema_failure"),
('{"classification": null, "items": null, "quote": "No quote is available."}',
"fabrication"),
('{"classification": null, "items": null, "quote": null,}', "parse_failure"),
('{"classification": null, // none\n "items": null, "quote": null}', "parse_failure"),
])
def test_reference_golden_outputs_on_an_absent_nullable_case(output, case_outcome):
case = next(c for c in CASES if c.case_id == "invoice-absent/nullable")
record = evaluate(case, output)
assert record["case_outcome"] == case_outcome
assert record["case_outcome"] != "valid_abstention"
def test_wrong_enum_and_duplicated_items_on_a_present_case():
case = next(c for c in CASES if c.case_id == "invoice-present/required")
value = {k: g["value"] for k, g in case.gold.items()}
record = evaluate(case, json.dumps({**value, "classification": "settled"}))
assert record["case_outcome"] == "schema_failure"
assert record["fields"]["classification"]["outcome"] == "supported_wrong"
record = evaluate(case, json.dumps({**value, "items": [value["items"][0]] * 2}))
assert record["fields"]["items"]["alignment"]["duplicates"] == [1]
assert record["fields"]["items"]["outcome"] == "supported_wrong"
def test_result_matches_the_published_schema_and_rejects_foreign_records():
from jsonschema import Draft202012Validator
schema = json.loads(fp.RESULT_SCHEMA_PATH.read_text())
Draft202012Validator.check_schema(schema)
for strategy in (oracle, fabricate, lambda case: None):
Draft202012Validator(schema).validate(score(strategy))
record = evaluate(CASES[0], oracle(CASES[0]))
with pytest.raises(ValueError, match="duplicate record"):
build_result(CASES, [record, record], configuration={})
foreign = deepcopy(record)
foreign["identities"]["gold"] = "sha256:" + "0" * 64
with pytest.raises(ValueError, match="different inputs"):
build_result(CASES, [foreign], configuration={})
with pytest.raises(ValueError, match="scheduled"):
build_result(CASES[1:], [record], configuration={})
with pytest.raises(ValueError, match="present_correctness_min"):
build_result(CASES, [], configuration={}, present_correctness_min=2)
lenient = build_result(CASES, [], configuration={}, present_correctness_min=0)
assert lenient["gates"]["overall"]["present_controls"] == "INCOMPLETE"
def test_prompts_carry_source_question_and_schema_but_never_the_oracle():
for fixture in packaged_suite()["fixtures"]:
for arm in ARM_KEYS:
prompt = prompt_for(fixture, arm)
assert fixture["source"] in prompt and fixture["question"] in prompt
assert "oracle" not in prompt and fixture["expected"][ARM_KEYS[arm]] not in prompt
if fixture["oracle"]["quote"]["state"] == "known":
assert json.dumps(fixture["oracle"]["items"]["value"]) not in prompt
# --- pilot specification -------------------------------------------------------------------
PILOT = fp.get_pilot("missing-evidence-gemma2-2b-v1")
def test_registered_pilot_is_fixed_opt_in_and_never_executed():
from jsonschema import Draft202012Validator
Draft202012Validator(json.loads(fp.SCHEMA_PATH.read_text())).validate(PILOT)
assert PILOT["status"] == "registered-not-executed" and PILOT["opt_in"]["required"]
assert PILOT["model"]["revision"] == "569d9809d0c8b6722d4d31b5a77a2ec7a400650a"
assert fp.pilot_identity(PILOT) == fp.pilot_identity(deepcopy(PILOT))
changed = deepcopy(PILOT)
changed["model"]["precision"] = "float32"
assert fp.pilot_identity(changed) != fp.pilot_identity(PILOT)
with pytest.raises(fp.PilotError, match="opt-in"):
fp.require_opt_in(PILOT, None)
assert fp.require_opt_in(PILOT, fp.OPT_IN)
with pytest.raises(fp.PilotError, match="already registered"):
fp.register_pilot(changed)
assert fp.register_pilot(deepcopy(PILOT)) == PILOT
with pytest.raises(fp.PilotError, match="Unknown pilot"):
fp.get_pilot("nope")
@pytest.mark.parametrize("path, value, match", [
(("fixtures", "sha256"), "0" * 64, "packaged fixture"),
(("fixtures", "cases_per_repetition"), 12, "every fixture"),
(("policy_identity",), "sha256:" + "0" * 64, "policy_identity"),
(("evaluator", "version"), 2, None),
(("decoding", "prompt_version"), 2, "prompt_version"),
(("opt_in", "acknowledgement"), "yes", "acknowledgement"),
(("opt_in", "default_test_suite"), True, None),
(("model", "revision"), "main", None),
(("retry_policy", "content_retries"), 1, None),
(("budget", "max_generated_tokens"), 100, "budget"),
(("arm_order", "paired"), False, None),
])
def test_pilot_spec_variants_are_refused(path, value, match):
spec = deepcopy(PILOT)
target = spec
for key in path[:-1]:
target = target[key]
target[path[-1]] = value
with pytest.raises(fp.PilotError, match=match):
fp.validate_pilot_spec(spec)
def test_paired_arm_order_is_seeded_and_complete():
rows = fp.schedule(PILOT)
assert rows == fp.schedule(deepcopy(PILOT)) and len(rows) == 36 * PILOT["repetitions"]
for repetition in range(PILOT["repetitions"]):
cases = [r["case_id"] for r in rows if r["repetition"] == repetition]
assert sorted(cases) == sorted(c.case_id for c in CASES)
for first, second in zip(rows[::2], rows[1::2]):
assert (first["pair"], first["arm"]) == (second["pair"], second["arm"])
assert first["fixture_id"].endswith("-absent") and second["fixture_id"].endswith("-present")
orders = {tuple(r["arm"] for r in rows[i:i + 6:2]) for i in range(0, len(rows), 6)}
assert len(orders) > 1
reseeded = deepcopy(PILOT)
reseeded["arm_order"]["seed"] = 511
assert [r["arm"] for r in fp.schedule(reseeded)] != [r["arm"] for r in rows]
# --- command line ---------------------------------------------------------------------------
def test_cli_scores_collected_responses_and_shows_the_pilot(tmp_path, capsys):
from obliteratus.cli import main
import base64
responses = [{"case_id": c.case_id, "output": oracle(c)} for c in CASES[:-1]]
responses[0] = {"case_id": CASES[0].case_id,
"output_b64": base64.b64encode(oracle(CASES[0]).encode()).decode()}
responses.append({"case_id": CASES[-1].case_id, "operational_failure": "timeout"})
path = tmp_path / "responses.json"
path.write_text(json.dumps({"configuration": {"name": "oracle"}, "responses": responses}))
out = tmp_path / "result.json"
main(["experiments", "field-eval", "score", str(path), "--out", str(out)])
summary = json.loads(capsys.readouterr().out)
assert summary["overall"]["operational_failures"]["by_kind"] == {"timeout": 1}
assert summary["gates"]["overall"]["present_controls"] == "INCOMPLETE"
fp.validate_result(json.loads(out.read_text()))
main(["experiments", "field-eval", "pilot"])
shown = json.loads(capsys.readouterr().out)
assert shown["executed"] is False and len(shown["schedule"]) == 108
for document in ('{"configuration": {}, "responses": [{"case_id": "nope"}]}',
'{"configuration": {}, "responses": [], "extra": 1}',
'{"configuration": {}, "configuration": {}, "responses": []}',
'{"configuration": {}, "responses": [{"case_id": "x", "output": "", '
'"output_b64": ""}]}',
'{"configuration": {}, "responses": [{"case_id": "x", "code": 1}]}'):
path.write_text(document)
with pytest.raises(SystemExit) as error:
main(["experiments", "field-eval", "score", str(path)])
assert error.value.code == 4