feat(validator): anchor the deterministic gate to the project's real cost baseline (S4.0)

Every stage of validate_proposal reasoned only about numbers the proposal itself
supplied, so an internally-consistent hallucination cleared the whole gate (F3).
A new stage 0 reconciles each affected_item against the project's CostBaseline
before the CBC solve: an unknown cost code is rejected, and a real code carrying
a quantity/unit_cost outside the configured tolerance (5% default, relative to
the baseline value) is rejected. Validation, never repair.

The baseline argument is OPTIONAL (None = pre-S4.0 behaviour), but both run
paths set it: the road path projects project.cost_items, the bundle path loads
cost-baseline.json when the bundle ships one. Bundles written before the
amendment stay un-anchored, so the commons-owned goldens run byte-identically;
a baseline that exists but is malformed still raises on both loaders.

F8: the method-specific cap now comes from the METHOD_CAPS registry (measure
type -> fraction, injectable) instead of an energy_efficiency string comparison.

The baseline format and tolerance semantics were decided locally — the commons
amendment (D-A pt. 2) never arrived, exactly as in S3.2. D7 mirroring stays open.

Three portfolio fixtures quoted cost codes belonging to OTHER projects; the new
gate caught them. They now quote each project's own lines, and the two copied
REPLIES tables import the single source instead of drifting from it.

Load-bearing measured (tests/test_s40_cost_baseline_loadbearing.py), six
mutations all red: detach the reconciliation stage; detach the magnitude
tolerance; detach the road wiring; detach the bundle wiring; ignore the injected
cap registry; make the optional loader tolerant of malformed content. Control:
with the road wiring detached the repaired portfolio fixtures still pass, so
they are not masking the seam. 597 -> 612 tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JdwK7bQ4BZkWH4t8MRDKb4
This commit is contained in:
Kjell Tore Guttormsen 2026-08-03 17:19:31 +02:00
commit 126807aee7
16 changed files with 645 additions and 69 deletions

View file

@ -28,29 +28,34 @@ _OFFLINE_MODEL_MAP = {
# The synthetic reply IS the proposal: generate._parse_ir builds affected_items (each with its
# own quantity/unit_cost) straight from this JSON, and the validator's P90 = 0.30 x Σ(qty·unit_cost)
# ONLY when ``assumptions`` is empty (degenerate Monte Carlo, validator.py:108-113). All three
# replies therefore OMIT ``assumptions`` and carry explicit magnitudes so each
# ``claimed_saving_nok`` <= P90. Verified against validator + ir:
# FV42-GSV-E1 Σ=1,482,500 P90=444,750 claimed 200,000 -> validates
# RV13-RAS-TP Σ= 756,000 P90=226,800 claimed 130,000 -> validates (decoy)
# BRU-LAKS-REHAB Σ=2,580,500 P90=774,150 claimed 210,000 -> validates
# ONLY when ``assumptions`` is empty (degenerate Monte Carlo, validator.py). All three replies
# therefore OMIT ``assumptions`` and carry explicit magnitudes so each ``claimed_saving_nok`` <= P90.
#
# S4.0: every line below quotes a cost line the project ACTUALLY has, verbatim from
# reference_projects.json — the road path now anchors the validator to ``project.cost_items``, so a
# reply quoting another project's code (which these fixtures used to do) is rejected as a fabricated
# cost line. Verified against reference_projects.json + validator + ir:
# FV42-GSV-E1 01.1 1x850,000 + 05.2 4300x215 Σ=1,774,500 P90=532,350 claimed 200,000
# RV13-RAS-TP 22.4 610x3,850 Σ=2,348,500 P90=704,550 claimed 130,000 (decoy)
# BRU-LAKS-REHAB 01.1 1x620,000 + 87.3 640x980 Σ=1,247,200 P90=374,160 claimed 210,000
# ``measure`` is byte-identical "Reduce scope" for FV42+BRU (measure-match is exact string
# equality, verdicts.py:68) and "Material substitution" for the decoy, so the BRU<->FV42 pair
# overlaps (shared code 05.2 + measure + magnitude bucket) while the decoy does not.
# equality, verdicts.py:68) and "Material substitution" for the decoy, so the BRU<->FV42 pair still
# overlaps (shared code 01.1 + measure + magnitude bucket) while the decoy does not. 01.1 replaces
# 05.2 as the shared code because it is the only code both projects genuinely carry.
REPLIES = {
"FV42-GSV-E1": (
'{"measure":"Reduce scope","affected_items":['
'{"code":"05.2","quantity":4300,"unit_cost":215},'
'{"code":"03.1","quantity":1800,"unit_cost":310}],"claimed_saving_nok":200000}'
'{"code":"01.1","quantity":1,"unit_cost":850000},'
'{"code":"05.2","quantity":4300,"unit_cost":215}],"claimed_saving_nok":200000}'
),
"RV13-RAS-TP": (
'{"measure":"Material substitution","affected_items":['
'{"code":"88.2","quantity":180,"unit_cost":4200}],"claimed_saving_nok":130000}'
'{"code":"22.4","quantity":610,"unit_cost":3850}],"claimed_saving_nok":130000}'
),
"BRU-LAKS-REHAB": (
'{"measure":"Reduce scope","affected_items":['
'{"code":"05.2","quantity":4300,"unit_cost":215},'
'{"code":"07.4","quantity":2400,"unit_cost":690}],"claimed_saving_nok":210000}'
'{"code":"01.1","quantity":1,"unit_cost":620000},'
'{"code":"87.3","quantity":640,"unit_cost":980}],"claimed_saving_nok":210000}'
),
}