portfolio-optimiser/tests/test_provenance.py
Kjell Tore Guttormsen d74f32dc1c feat(p19): a cost code must have a FORM where the input offers forms
P18's round 2 ended with two VALIDATED proposals whose affected_item codes
were ordinary words from a road standard's prose -- impulsventilator (4 of
270 N500 documents) and bituminoest baerelag (4 of 1133 N200). Both are
grounded in P7's sense and neither is inert in P18/B1's sense; they are simply
not identifiers of a cost line, and the gate had no stage that could say so.
Known positive MEASURED, not asserted: replayed offline against the bases
those runs were given, both come back Rejection naming the denominator.

IDENTIFIER_FORMS moved from generate.py to validator.py: they now drive both
P8's report and this gate, and two copies of "what an identifier looks like"
would let the two disagree about one run's own input.

B1 -- two new forms, transcribed from measurement. R761's requirement numbers
are bare dotted numbers and all six refs in kontrakt-sorasen's fasit are of
that shape, which neither pre-P19 form matched: r761's whole offer was 3
identifiers over 6.5 MB, and is now 2332. The FIRST form was widened in the
same pass because B2 made these forms decide prose vs identifier, and this
repo's own ENERGI-TOTAL-EL matched none of them -- a gate may only be wrong in
the direction that admits too much.

Three things keep the gate from being a rule about shapes: the generality
guard (it fires only where the input offers forms), the baseline exemption
(stage 0 has already ruled that code real), and full-matching.

Honesty limit, measured and given its OWN arm: a decimal and an R761 process
number are typographically identical, so the form counts both. P8's existing
"bare numbers" arm is narrowed to bare INTEGERS accordingly.

Measured over all nine round-1+2 outboxes: 26 of 36 codes are prose.

Load-bearing measured (22 arms), four mutations all red against the whole
suite (16 / 10 / 1 / 2), green control 1734/5 and the golden byte-unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 02:11:29 +02:00

53 lines
1.7 KiB
Python

"""Step 6 tests — first-class Pydantic provenance stamp.
At least one citation is mandatory (the load-bearing NFR); a valid stamp exposes the
proposal's provenance fields; the annotation adapter yields MAF citation dicts for display.
Pattern: tests/spikes/test_c_validator.py (pydantic raises + field assertions).
"""
import pytest
from pydantic import ValidationError
from portfolio_optimiser.provenance import Citation, ProvenanceStamp
from portfolio_optimiser.retrieval import TextSpan
def _stamp() -> ProvenanceStamp:
return ProvenanceStamp(
citations=[Citation(file="asphalt.txt", locator=TextSpan(0, 10), snippet="0123456789")],
model="qwen3:4b",
role="proposer",
validator_decision="validated",
token_usage=42,
cost_baseline_anchored=True,
bundle_id_source=None,
code_forms={},
)
def test_zero_citations_is_rejected() -> None:
with pytest.raises(ValidationError):
ProvenanceStamp(
citations=[],
model="m",
role="proposer",
validator_decision="validated",
token_usage=1,
cost_baseline_anchored=True,
bundle_id_source=None,
code_forms={},
)
def test_valid_stamp_exposes_provenance_fields() -> None:
stamp = _stamp()
assert len(stamp.citations) >= 1
assert stamp.model and stamp.role
assert stamp.validator_decision in {"validated", "rejected"}
assert isinstance(stamp.token_usage, int) and stamp.token_usage > 0
def test_to_annotations_yields_citation_dicts() -> None:
anns = _stamp().to_annotations()
assert anns and all(a["type"] == "citation" for a in anns)
assert anns[0]["annotated_regions"][0]["type"] == "text_span"