mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
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>
408 lines
20 KiB
Python
408 lines
20 KiB
Python
"""Golden evaluator tests for field-level correctness (scorer ``field_evidence`` v1)."""
|
|
|
|
import base64
|
|
import hashlib
|
|
import inspect
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from obliteratus.concepts.field_parse import (
|
|
StrictParseError, check_schema, json_equal, strict_parse, validate_schema,
|
|
)
|
|
from obliteratus.concepts.field_policy import EvaluationCase, FieldPolicy, PolicyError
|
|
from obliteratus.concepts.field_scoring import FieldEvidenceScorer, evaluate
|
|
from obliteratus.concepts.scorecard import score_baseline_task
|
|
from obliteratus.concepts.scoring import (
|
|
SCORER_REGISTRY, ScorerSpec, ScoringExample, ScoringTask, get_scorer,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
SOURCE = 'LEDGER: invoice 0017 total 100 USD; reply "Delivery was late." archived.'
|
|
SHA = hashlib.sha256(SOURCE.encode()).hexdigest()
|
|
EXACT = {"kind": "exact", "value_type": "string"}
|
|
COUNT = {"kind": "count"}
|
|
QUOTE = {"kind": "evidence_span"}
|
|
IDS = {"kind": "array", "items": {"kind": "exact", "value_type": "string"}}
|
|
|
|
|
|
def case(policy, gold, arm="nullable", schema=None, source=SOURCE):
|
|
return EvaluationCase("c1", source, hashlib.sha256(source.encode()).hexdigest(),
|
|
schema or {"type": "object"}, arm, FieldPolicy("p", {"f": policy}),
|
|
{"f": gold})
|
|
|
|
|
|
def present(value):
|
|
return {"state": "PRESENT", "value": value}
|
|
|
|
|
|
UNKNOWN = {"state": "UNKNOWN", "reason": "evidence_absent"}
|
|
|
|
|
|
def field(policy, gold, output, **options):
|
|
raw = output if isinstance(output, (str, bytes)) else json.dumps(output)
|
|
return evaluate(case(policy, gold, **options), raw)["fields"]["f"]
|
|
|
|
|
|
def outcome(policy, gold, value, **options):
|
|
return field(policy, gold, {"f": value}, **options)["outcome"]
|
|
|
|
|
|
# --- REF-3174 golden evaluator cases ------------------------------------------------------
|
|
|
|
def test_exact_ids_keep_leading_zeros_and_refuse_coercion():
|
|
assert outcome(EXACT, present("0017"), "0017") == "supported_correct"
|
|
assert outcome(EXACT, present("0017"), "17") == "supported_wrong"
|
|
assert outcome(EXACT, present("0017"), 17) == "wrong_type"
|
|
enum = {**EXACT, "enum": ["paid", "unpaid"]}
|
|
assert field(enum, present("paid"), {"f": "settled"})["type_error"] == "value_not_in_enum"
|
|
|
|
|
|
@pytest.mark.parametrize("value", [3.9, 3.0, "3", True])
|
|
def test_counts_are_json_integers_without_coercion(value):
|
|
assert outcome(COUNT, present(3), value) == "wrong_type"
|
|
assert outcome(COUNT, present(3), 3) == "supported_correct"
|
|
|
|
|
|
def test_boolean_never_equals_integer():
|
|
assert outcome(COUNT, present(1), True) == "wrong_type"
|
|
flag = {"kind": "exact", "value_type": "boolean"}
|
|
assert outcome(flag, present(True), 1) == "wrong_type"
|
|
assert json_equal(1, 1.0) and not json_equal(True, 1) and not json_equal([True], [1])
|
|
|
|
|
|
@pytest.mark.parametrize("abs_tol, rel_tol, gold, pred, expected", [
|
|
(0.1, 0, 100, 100.09, "supported_correct"),
|
|
(0.1, 0, 100, 100.2, "supported_wrong"),
|
|
(0, 0.001, 100, 100.09, "supported_correct"),
|
|
(0, 0.001, 100, 100.2, "supported_wrong"),
|
|
(0, 0.001, 0, 0.001, "supported_wrong"),
|
|
(0, 0.001, 0, 0, "supported_correct"),
|
|
(0.01, 0.001, 0, 0.001, "supported_correct"),
|
|
])
|
|
def test_numeric_tolerance_is_absolute_plus_relative(abs_tol, rel_tol, gold, pred, expected):
|
|
policy = {"kind": "quantity", "abs_tol": abs_tol, "rel_tol": rel_tol}
|
|
assert outcome(policy, present(gold), pred) == expected
|
|
|
|
|
|
def test_units_convert_only_through_declared_factors():
|
|
policy = {"kind": "quantity", "abs_tol": 0, "rel_tol": 0, "unit": "USD",
|
|
"units": {"USD": 1, "cents": 0.01}}
|
|
gold = present({"value": 1, "unit": "USD"})
|
|
assert outcome(policy, gold, {"value": 100, "unit": "cents"}) == "supported_correct"
|
|
assert outcome(policy, gold, {"value": 1, "unit": "USD"}) == "supported_correct"
|
|
assert field(policy, gold, {"f": {"value": 1, "unit": "EUR"}})["type_error"] \
|
|
== "unsupported_unit"
|
|
assert outcome(policy, gold, 1) == "wrong_type"
|
|
|
|
|
|
def test_missing_key_is_never_an_explicit_null_pass():
|
|
gold = {"state": "EXPLICIT_NULL"}
|
|
assert field(EXACT, gold, {})["outcome"] == "missing"
|
|
assert field(EXACT, gold, {})["candidate_state"] == "MISSING_KEY"
|
|
assert outcome(EXACT, gold, None) == "supported_correct"
|
|
assert outcome(EXACT, gold, None, arm="required") == "supported_correct"
|
|
assert outcome(EXACT, gold, {"state": "known", "value": None},
|
|
arm="tagged_unknown") == "supported_correct"
|
|
assert outcome(EXACT, gold, {"state": "unknown", "reason": "evidence_absent"},
|
|
arm="tagged_unknown") == "supported_wrong"
|
|
assert outcome(EXACT, gold, "0017") == "unsupported_concrete"
|
|
|
|
|
|
def test_unknown_versus_empty_array_is_an_unsupported_empty_claim():
|
|
result = field(IDS, UNKNOWN, {"f": []})
|
|
assert (result["candidate_state"], result["outcome"]) == ("KNOWN_EMPTY", "unsupported_concrete")
|
|
|
|
|
|
def test_known_empty_scores_empty_and_spurious_arrays():
|
|
empty = {"state": "KNOWN_EMPTY", "value": []}
|
|
result = field(IDS, empty, {"f": []})
|
|
assert result["outcome"] == "supported_correct" and result["alignment"]["known_empty"]
|
|
assert result["alignment"]["f1"] == result["alignment"]["precision"] == 1.0
|
|
result = field(IDS, empty, {"f": ["A"]})
|
|
assert result["outcome"] == "unsupported_concrete"
|
|
assert result["alignment"]["spurious"] == [0] and result["alignment"]["f1"] == 0.0
|
|
assert outcome(IDS, empty, None) == "over_abstention"
|
|
assert outcome(IDS, empty, [3]) == "wrong_type"
|
|
|
|
|
|
def test_reordered_items_match_unless_order_is_declared():
|
|
assert outcome(IDS, present(["A", "B"]), ["B", "A"]) == "supported_correct"
|
|
ordered = {**IDS, "ordered": True}
|
|
result = field(ordered, present(["A", "B"]), {"f": ["B", "A"]})
|
|
assert result["outcome"] == "supported_wrong" and result["alignment"]["order_ok"] is False
|
|
|
|
|
|
def test_duplicate_prediction_keeps_one_to_one_counts():
|
|
result = field(IDS, present(["A", "B"]), {"f": ["A", "A", "B"]})
|
|
align = result["alignment"]
|
|
assert (align["tp"], align["fp"], align["fn"]) == (2, 1, 0)
|
|
assert align["recall"] == 1 and align["precision"] == pytest.approx(2 / 3)
|
|
assert align["f1"] == pytest.approx(0.8) and align["duplicates"] == [1]
|
|
assert result["outcome"] == "supported_wrong"
|
|
|
|
|
|
def test_missing_and_spurious_items_are_listed():
|
|
align = field(IDS, present(["A", "B"]), {"f": ["A"]})["alignment"]
|
|
assert (align["tp"], align["fn"], align["fp"], align["missed"]) == (1, 1, 0, [1])
|
|
assert align["f1"] == pytest.approx(2 / 3)
|
|
align = field(IDS, present(["A", "B"]), {"f": ["A", "B", "C"]})["alignment"]
|
|
assert align["spurious"] == [2] and align["recall"] == 1 and not align["fully_supported"]
|
|
|
|
|
|
def test_record_identity_match_does_not_excuse_a_wrong_attribute():
|
|
policy = {"kind": "array", "items": {"kind": "record", "key": ["id"], "attributes": {
|
|
"id": EXACT, "amount": {"kind": "quantity", "abs_tol": 0, "rel_tol": 0}}}}
|
|
gold = present([{"id": "A", "amount": 10}, {"id": "B", "amount": 5}])
|
|
result = field(policy, gold, {"f": [{"id": "B", "amount": 5}, {"id": "A", "amount": 11}]})
|
|
assert result["alignment"]["mapping"] == [[0, 1], [1, 0]]
|
|
assert result["alignment"]["content_failures"] == [1]
|
|
assert result["outcome"] == "supported_wrong"
|
|
assert outcome(policy, gold, [{"id": "A"}]) == "wrong_type"
|
|
|
|
|
|
def test_quantity_items_use_maximum_one_to_one_assignment():
|
|
policy = {"kind": "array", "items": {"kind": "quantity", "abs_tol": 1, "rel_tol": 0}}
|
|
align = field(policy, present([10, 11.5]), {"f": [10.6, 10]})["alignment"]
|
|
assert align["tp"] == 2 and align["fully_supported"]
|
|
|
|
|
|
def test_fabricated_quotes_are_unsupported_whatever_the_gold():
|
|
assert outcome(QUOTE, UNKNOWN, "The customer was furious.") == "unsupported_concrete"
|
|
result = field(QUOTE, present("Delivery was late."), {"f": "Delivery was very late."})
|
|
assert result["outcome"] == "unsupported_concrete" and result["span"] is None
|
|
result = field(QUOTE, present("Delivery was late."), {"f": "Delivery was late."})
|
|
assert result["outcome"] == "supported_correct"
|
|
assert SOURCE[slice(*result["span"])] == "Delivery was late."
|
|
assert outcome(QUOTE, present("Delivery was late."), "invoice 0017") == "supported_wrong"
|
|
collapse = {**QUOTE, "whitespace": "collapse"}
|
|
assert outcome(collapse, present("Delivery was late."), "Delivery was\nlate.") \
|
|
== "supported_correct"
|
|
assert outcome(QUOTE, present("Delivery was late."), "Delivery was\nlate.") \
|
|
== "unsupported_concrete"
|
|
|
|
|
|
def test_disclaimer_in_a_free_string_field_is_not_an_abstention():
|
|
assert outcome(QUOTE, UNKNOWN, "Not available in the source.") == "unsupported_concrete"
|
|
items = {"kind": "array", "items": QUOTE}
|
|
assert outcome(items, present(["Delivery was late."]), ["unknown"]) == "unsupported_concrete"
|
|
|
|
|
|
@pytest.mark.parametrize("raw, reason", [
|
|
(b'{"f": "0017",}', "invalid_json"),
|
|
(b'{"f": "0017" /* json5 */}', "invalid_json"),
|
|
(b'```json\n{"f": "0017"}\n```', "invalid_json"),
|
|
(b'\xef\xbb\xbf{"f": "0017"}', "invalid_json"),
|
|
(b'{"f": "0017", "f": "17"}', "duplicate_key"),
|
|
(b'{"f": NaN}', "non_finite_number"),
|
|
(b'{"f": 1e999}', "non_finite_number"),
|
|
(b'{"f": "\\ud800"}', "invalid_unicode"),
|
|
(b'{"f": "\xff"}', "invalid_utf8"),
|
|
(b"[" * 200000, "excessive_nesting"),
|
|
])
|
|
def test_strict_parse_never_repairs(raw, reason):
|
|
with pytest.raises(StrictParseError) as error:
|
|
strict_parse(raw)
|
|
assert error.value.reason == reason
|
|
record = evaluate(case(EXACT, present("0017")), raw)
|
|
assert record["case_outcome"] == "parse_failure" and record["parse"]["reason"] == reason
|
|
assert record["fields"]["f"]["outcome"] == "structural_failure"
|
|
assert base64.b64decode(record["raw_b64"]) == raw
|
|
assert record["identities"]["output"] == "sha256:" + hashlib.sha256(raw).hexdigest()
|
|
|
|
|
|
def test_structured_output_v1_is_unchanged():
|
|
v1 = get_scorer(ScorerSpec("structured_output", 1))
|
|
target = {"schema": {"type": "object", "required": ["f"]}}
|
|
assert v1.score('```json\n{"f": "0017"}\n```', target)["task_success"] is True
|
|
assert v1.score("{}", target) == {"task_success": False, "invalid": False,
|
|
"abstained": False, "missing_reason": None}
|
|
|
|
|
|
def test_raw_bytes_and_whitespace_are_preserved():
|
|
raw = ' {"f": "0017"}\n\n'
|
|
record = evaluate(case(EXACT, present("0017")), raw)
|
|
assert base64.b64decode(record["raw_b64"]).decode() == raw
|
|
assert record["case_outcome"] == "supported" and record["structural_success"]
|
|
|
|
|
|
def test_operational_failures_are_not_content_outcomes():
|
|
target = case(EXACT, present("0017"))
|
|
assert evaluate(target, None)["operational_failure"] == "not_returned"
|
|
assert evaluate(target, " \n")["operational_failure"] == "empty_response"
|
|
truncated = evaluate(target, '{"f": "00', operational="truncated")
|
|
assert truncated["case_outcome"] == "operational_failure"
|
|
assert base64.b64decode(truncated["raw_b64"]) == b'{"f": "00'
|
|
with pytest.raises(ValueError, match="operational failure"):
|
|
evaluate(target, "{}", operational="model_was_tired")
|
|
with pytest.raises(ValueError, match="repair is a separate intervention"):
|
|
evaluate(target, '{"f": "0017",}', arm_kind="repair")
|
|
with pytest.raises(TypeError):
|
|
evaluate(target, 17)
|
|
with pytest.raises(TypeError):
|
|
evaluate({"f": "0017"}, "{}")
|
|
|
|
|
|
def test_parse_schema_and_content_are_reported_separately():
|
|
schema = {"type": "object", "required": ["f"], "additionalProperties": False,
|
|
"properties": {"f": {"type": "string", "enum": ["paid"]}}}
|
|
record = evaluate(case(QUOTE, UNKNOWN, schema=schema), '{"f": "Delivery was late."}')
|
|
assert record["parse"]["ok"] and not record["schema"]["valid"]
|
|
assert record["fields"]["f"]["outcome"] == "unsupported_concrete"
|
|
assert record["case_outcome"] == "fabrication"
|
|
record = evaluate(case(EXACT, present("0017"), schema=schema), '{"f": "0017"}')
|
|
assert record["fields"]["f"]["outcome"] == "supported_correct"
|
|
assert record["case_outcome"] == "schema_failure"
|
|
record = evaluate(case(EXACT, present("0017"), schema=schema), '"I cannot answer."')
|
|
assert record["fields"]["f"]["outcome"] == "missing"
|
|
assert record["case_outcome"] == "schema_failure"
|
|
record = evaluate(case(EXACT, present("0017")), "{}")
|
|
assert record["structural_success"] and record["case_outcome"] == "incorrect"
|
|
assert record["fields"]["f"]["outcome"] == "missing"
|
|
|
|
|
|
def test_abstention_is_authorized_only_by_the_arm_representation():
|
|
tag = {"state": "unknown", "reason": "evidence_absent"}
|
|
assert outcome(EXACT, UNKNOWN, None, arm="nullable") == "authorized_abstention"
|
|
assert outcome(EXACT, UNKNOWN, None, arm="required") == "unauthorized_abstention"
|
|
assert outcome(EXACT, UNKNOWN, None, arm="tagged_unknown") == "unauthorized_abstention"
|
|
assert outcome(EXACT, UNKNOWN, tag, arm="tagged_unknown") == "authorized_abstention"
|
|
assert outcome(EXACT, UNKNOWN, tag, arm="nullable") == "unauthorized_abstention"
|
|
assert outcome(EXACT, UNKNOWN, {"state": "unknown", "reason": "not_sure"},
|
|
arm="tagged_unknown") == "unauthorized_abstention"
|
|
assert outcome(EXACT, present("0017"), None) == "over_abstention"
|
|
assert outcome(EXACT, present("0017"), tag, arm="tagged_unknown") == "over_abstention"
|
|
assert outcome(EXACT, present("0017"), "0017", arm="tagged_unknown") == "wrong_type"
|
|
assert outcome(EXACT, present("0017"), {"state": "known", "value": "0017"},
|
|
arm="tagged_unknown") == "supported_correct"
|
|
assert outcome(EXACT, present("0017"), {"state": "known", "value": "0017"}) == "wrong_type"
|
|
assert outcome(EXACT, present("0017"), []) == "wrong_type"
|
|
assert outcome(IDS, present(["A"]), []) == "supported_wrong"
|
|
|
|
|
|
# --- identities, policies and registration ------------------------------------------------
|
|
|
|
def test_identities_are_distinct_and_the_output_cannot_supply_its_oracle():
|
|
schema = {"type": "object", "required": ["f"], "additionalProperties": False,
|
|
"properties": {"f": {"type": "string"}}}
|
|
target = case(EXACT, present("0017"), schema=schema)
|
|
before = dict(target.identities)
|
|
record = evaluate(target, json.dumps({"f": "17", "gold": {"f": "17"}, "policy": {}}))
|
|
ids = record["identities"]
|
|
assert len(set(ids.values())) == 5 and ids["source"] == "sha256:" + SHA
|
|
assert target.identities == before and record["fields"]["f"]["outcome"] == "supported_wrong"
|
|
assert not record["schema"]["valid"]
|
|
assert list(inspect.signature(evaluate).parameters) == ["case", "raw", "operational",
|
|
"arm_kind"]
|
|
other = case(EXACT, present("17"), schema=schema)
|
|
changed = {k for k in before if before[k] != other.identities[k]}
|
|
assert changed == {"gold"}
|
|
|
|
|
|
@pytest.mark.parametrize("policy", [
|
|
{"kind": "python", "callable": "pack.module:score"},
|
|
{**EXACT, "scorer": "pack.module:score"},
|
|
{"kind": "exact", "value_type": "float"},
|
|
{**EXACT, "enum": []},
|
|
{**EXACT, "enum": [1]},
|
|
{**EXACT, "severity": "fatal"},
|
|
{"kind": "quantity", "abs_tol": -1, "rel_tol": 0},
|
|
{"kind": "quantity", "abs_tol": float("inf"), "rel_tol": 0},
|
|
{"kind": "quantity", "abs_tol": 0, "rel_tol": 0, "unit": "USD"},
|
|
{"kind": "quantity", "abs_tol": 0, "rel_tol": 0, "unit": "USD", "units": {"USD": 2}},
|
|
{"kind": "quantity", "abs_tol": 0, "rel_tol": 0, "unit": "USD",
|
|
"units": {"USD": 1, "cents": 0}},
|
|
{"kind": "evidence_span", "whitespace": "fuzzy"},
|
|
{"kind": "record", "key": ["id"], "attributes": {"id": EXACT}},
|
|
{"kind": "array", "items": IDS},
|
|
{"kind": "array", "items": EXACT, "ordered": "yes"},
|
|
{"kind": "array", "items": {"kind": "record", "key": ["q"], "attributes": {"q": QUOTE}}},
|
|
{"kind": "array", "items": {"kind": "record", "key": ["id"], "attributes": {"id": IDS}}},
|
|
{"kind": "array", "items": {"kind": "record", "key": ["id"], "attributes": {}}},
|
|
])
|
|
def test_policies_are_data_only_and_validated(policy):
|
|
with pytest.raises(PolicyError):
|
|
FieldPolicy("p", {"f": policy})
|
|
|
|
|
|
@pytest.mark.parametrize("change, match", [
|
|
({"gold": {"f": {"state": "MISSING_KEY"}}}, "MISSING_KEY"),
|
|
({"gold": {"f": {"state": "UNKNOWN", "reason": "guess"}}}, "declared reason"),
|
|
({"gold": {"f": {"state": "PRESENT", "value": "not in source"}}, "policy": QUOTE},
|
|
"not verbatim"),
|
|
({"gold": {"f": {"state": "PRESENT", "value": 17}}}, "does not satisfy"),
|
|
({"gold": {"f": {"state": "KNOWN_EMPTY", "value": []}}}, "KNOWN_EMPTY"),
|
|
({"gold": {"f": {"state": "EXPLICIT_NULL", "value": "x"}}}, "no value"),
|
|
({"gold": {"f": {"state": "PRESENT", "value": "0017", "oracle": "x"}}}, "unexpected"),
|
|
({"gold": {}}, "exactly the policy fields"),
|
|
({"sha": "0" * 64}, "source_sha256"),
|
|
({"schema": {"$ref": "https://example.invalid/s.json"}}, "unsupported keywords"),
|
|
({"schema": {"type": "decimal"}}, "unknown type"),
|
|
({"schema": {"$schema": "http://json-schema.org/draft-07/schema#"}}, "2020-12"),
|
|
({"schema": {"anyOf": []}}, "nonempty"),
|
|
({"schema": {"additionalProperties": {"type": "string"}}}, "boolean"),
|
|
({"schema": []}, "must be an object"),
|
|
({"arm": "repaired"}, "arm"),
|
|
])
|
|
def test_cases_validate_every_independent_input(change, match):
|
|
values = {"policy": EXACT, "gold": {"f": present("0017")}, "sha": SHA,
|
|
"schema": {"type": "object"}, "arm": "nullable", **change}
|
|
with pytest.raises(PolicyError, match=match):
|
|
EvaluationCase("c", SOURCE, values["sha"], values["schema"], values["arm"],
|
|
FieldPolicy("p", {"f": values["policy"]}), values["gold"])
|
|
|
|
|
|
def test_policy_mapping_and_versions():
|
|
mapping = {"policy_id": "p", "version": 1, "fields": {"f": EXACT},
|
|
"unknown_reasons": ["evidence_absent", "source_ambiguous"]}
|
|
policy = FieldPolicy.from_mapping(mapping)
|
|
assert policy.as_dict() == mapping and policy.identity.startswith("sha256:")
|
|
for bad in ({**mapping, "code": "x"}, {**mapping, "version": 2},
|
|
{**mapping, "fields": {}}, {**mapping, "unknown_reasons": [""]},
|
|
{**mapping, "policy_id": ""}):
|
|
with pytest.raises(PolicyError):
|
|
FieldPolicy.from_mapping(bad)
|
|
|
|
|
|
def test_schema_subset_matches_draft_2020_12():
|
|
from jsonschema import Draft202012Validator
|
|
schema = check_schema({
|
|
"type": "object", "required": ["a", "b"], "additionalProperties": False,
|
|
"properties": {
|
|
"a": {"anyOf": [{"type": "integer", "minimum": 0, "maximum": 9}, {"type": "null"}]},
|
|
"b": {"oneOf": [{"const": "x"}, {"type": "array", "minItems": 1, "maxItems": 2,
|
|
"items": {"type": "string", "maxLength": 3}}]},
|
|
}})
|
|
values = [{"a": 1, "b": "x"}, {"a": 1.0, "b": ["ab"]}, {"a": None, "b": []},
|
|
{"a": True, "b": "x"}, {"a": 10, "b": "x"}, {"b": "x"}, {"a": 1, "b": "x", "c": 0},
|
|
{"a": 1, "b": ["abcd"]}, {"a": 1, "b": ["a", "b", "c"]}, [], "x"]
|
|
for value in values:
|
|
assert (not validate_schema(value, schema)) == Draft202012Validator(schema).is_valid(value)
|
|
|
|
|
|
def test_field_evidence_is_a_locally_registered_scorer():
|
|
assert SCORER_REGISTRY["field_evidence"] is FieldEvidenceScorer
|
|
assert isinstance(get_scorer(ScorerSpec("field_evidence", 1)), FieldEvidenceScorer)
|
|
with pytest.raises(ValueError, match="version 1, not 2"):
|
|
get_scorer(ScorerSpec("field_evidence", 2))
|
|
target = {"source": SOURCE, "source_sha256": SHA, "arm": "nullable",
|
|
"schema": {"type": "object", "required": ["f"]},
|
|
"policy": {"policy_id": "p", "fields": {"f": EXACT}},
|
|
"gold": {"f": present("0017")}}
|
|
task = ScoringTask("fields", ScorerSpec("field_evidence", 1), (
|
|
ScoringExample("a", "prompt", target), ScoringExample("b", "prompt", target),
|
|
ScoringExample("c", "prompt", target)), margin=0.5)
|
|
summary = score_baseline_task(task, ['{"f": "0017"}', "{}", '{"f": "17"}'])
|
|
records = summary["records"]
|
|
assert [r["task_success"] for r in records] == [True, None, False]
|
|
assert records[1]["missing_reason"] == "schema_violation" and records[1]["invalid"]
|
|
assert records[2]["field_evaluation"]["fields"]["f"]["outcome"] == "supported_wrong"
|
|
with pytest.raises(PolicyError):
|
|
ScoringTask("bad", ScorerSpec("field_evidence", 1),
|
|
(ScoringExample("a", "p", {**target, "scorer": "pack:code"}),), margin=0.5)
|
|
scorer = get_scorer("field_evidence")
|
|
nulls = scorer.score('{"f": null}', {**target, "gold": {"f": UNKNOWN}})
|
|
assert nulls["abstained"] and nulls["task_success"]
|
|
assert scorer.score("", target)["missing_reason"] == "empty_response"
|