feat(fase2): promote blocking validator + IR to src
This commit is contained in:
parent
99f0dd79fe
commit
bbba6e7337
3 changed files with 294 additions and 0 deletions
44
src/portfolio_optimiser/ir.py
Normal file
44
src/portfolio_optimiser/ir.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
"""Typed Pydantic IR for a candidate cost-saving measure (B1).
|
||||
|
||||
Pure module — **no** ``agent_framework`` and no solver. The two structural invariants
|
||||
(non-negative quantities; a claimed saving may not exceed the affected items' own total)
|
||||
are enforced at construction by Pydantic, so a malformed proposal can never be built. This
|
||||
IR is the D7-portable contract both the deterministic validator and the LLM->IR generator
|
||||
speak.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
|
||||
|
||||
class AffectedItem(BaseModel):
|
||||
"""One project cost line a proposal claims to save against."""
|
||||
|
||||
code: str
|
||||
quantity: float = Field(ge=0) # quantities must be >= 0
|
||||
unit_cost: float = Field(gt=0)
|
||||
|
||||
@property
|
||||
def total(self) -> float:
|
||||
return self.quantity * self.unit_cost
|
||||
|
||||
|
||||
class SavingsProposal(BaseModel):
|
||||
"""Typed IR for a candidate cost-saving measure (B1)."""
|
||||
|
||||
project_id: str
|
||||
measure: str
|
||||
affected_items: list[AffectedItem] = Field(min_length=1)
|
||||
claimed_saving_nok: float = Field(gt=0)
|
||||
# code -> (low_unit_cost, high_unit_cost) for the Monte Carlo step; empty = degenerate.
|
||||
assumptions: dict[str, tuple[float, float]] = Field(default_factory=dict)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _claim_within_affected_total(self) -> SavingsProposal:
|
||||
total = sum(item.total for item in self.affected_items)
|
||||
if self.claimed_saving_nok > total:
|
||||
raise ValueError(
|
||||
f"claimed saving {self.claimed_saving_nok} exceeds affected items' total {total}"
|
||||
)
|
||||
return self
|
||||
173
src/portfolio_optimiser/validator.py
Normal file
173
src/portfolio_optimiser/validator.py
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
"""Deterministic, blocking hybrid validator (B1) — the obligatory, non-optional gate.
|
||||
|
||||
Pure module: **NO** ``agent_framework`` import (D7-portable core). Three stages over the
|
||||
typed IR (``ir.py``):
|
||||
|
||||
1. **Pydantic IR** invariants already ran at construction (``ir.SavingsProposal``).
|
||||
2. **PuLP solver-in-the-loop** — a real CBC solve bounds the maximum feasible saving (R2).
|
||||
CBC ships in PuLP's wheel; if it is genuinely absent the step **escalates**
|
||||
(``CbcUnavailable``) — no silent LP-relaxation fallback.
|
||||
3. **Monte Carlo** — stdlib ``random`` (seeded ``_MC_SEED``) + ``statistics.quantiles`` over
|
||||
uncertain unit-costs give P10/P50/P90 of the feasible saving.
|
||||
|
||||
The structural block (stage 4) returns a ``Rejection`` that is a *different type* from
|
||||
``ValidatedProposal`` and carries no percentiles, so it can never be consumed as validated.
|
||||
|
||||
Promoted verbatim from ``spikes/c_validator.py``. The one deliberate change vs the spike:
|
||||
``self_repair`` no longer borrows the harness ``Budget`` (that pulls ``agent_framework`` via
|
||||
``spikes/_harness``) — it loops directly on ``max_attempts``; the token-budget bound is
|
||||
layered on by the Step 10 generate loop, keeping THIS module pure.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
import statistics
|
||||
import warnings
|
||||
from collections.abc import Callable
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pulp
|
||||
|
||||
from portfolio_optimiser.ir import AffectedItem, SavingsProposal
|
||||
from portfolio_optimiser.reference_domain import Project
|
||||
|
||||
MAX_SAVING_FRACTION = 0.30
|
||||
"""Policy cap: at most 30% of an affected item's cost is realistically recoverable as a
|
||||
saving. The LP bounds the feasible saving by this fraction."""
|
||||
|
||||
_MC_SAMPLES = 512
|
||||
_MC_SEED = 20260624
|
||||
|
||||
|
||||
class CbcUnavailable(RuntimeError):
|
||||
"""PuLP's bundled CBC solver is not available — escalate (no silent fallback)."""
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _quiet_pulp():
|
||||
"""Silence PuLP 3.x's ``PULP_CBC_CMD`` DeprecationWarning. The bundled CBC is only
|
||||
reachable via ``PULP_CBC_CMD``; PuLP 4.0 will require ``pip install pulp[cbc]`` +
|
||||
COIN_CMD (a migration note). The warning is cosmetic here."""
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
yield
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ValidatedProposal:
|
||||
"""A proposal that passed every stage. Carries the Monte Carlo percentiles."""
|
||||
|
||||
proposal: SavingsProposal
|
||||
p10: float
|
||||
p50: float
|
||||
p90: float
|
||||
nominal_feasible: float
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Rejection:
|
||||
"""A structurally-blocked proposal. Distinct type, no percentiles — it can never be
|
||||
consumed as a ``ValidatedProposal``."""
|
||||
|
||||
proposal: SavingsProposal
|
||||
reason: str
|
||||
|
||||
|
||||
def _solve_max_feasible(items: list[AffectedItem], fraction: float) -> float:
|
||||
"""Real CBC solve: maximize total saving subject to a per-item upper bound and a
|
||||
global fraction cap. Raises ``CbcUnavailable`` if CBC is genuinely missing."""
|
||||
with _quiet_pulp():
|
||||
solver = pulp.PULP_CBC_CMD(msg=False)
|
||||
if not solver.available():
|
||||
raise CbcUnavailable("PuLP's bundled CBC solver is not available on this platform")
|
||||
prob = pulp.LpProblem("max_feasible_saving", pulp.LpMaximize)
|
||||
xs = [pulp.LpVariable(f"x_{i}", lowBound=0, upBound=it.total) for i, it in enumerate(items)]
|
||||
prob += pulp.lpSum(xs)
|
||||
prob += pulp.lpSum(xs) <= fraction * sum(it.total for it in items)
|
||||
status = prob.solve(solver)
|
||||
if pulp.LpStatus[status] != "Optimal":
|
||||
raise CbcUnavailable(
|
||||
f"CBC did not reach an optimal solution (status={pulp.LpStatus[status]})"
|
||||
)
|
||||
return float(pulp.value(prob.objective))
|
||||
|
||||
|
||||
def _monte_carlo(
|
||||
proposal: SavingsProposal, *, fraction: float = MAX_SAVING_FRACTION
|
||||
) -> tuple[float, float, float]:
|
||||
"""Vary uncertain unit-costs (seeded) and return (P10, P50, P90) of the feasible
|
||||
saving. Uses the LP's closed-form optimum (= fraction x sum of sampled totals), which
|
||||
is exact here, so we do NOT spawn a CBC subprocess per sample (D6)."""
|
||||
rng = random.Random(_MC_SEED)
|
||||
feasibles: list[float] = []
|
||||
for _ in range(_MC_SAMPLES):
|
||||
total = 0.0
|
||||
for item in proposal.affected_items:
|
||||
rng_range = proposal.assumptions.get(item.code)
|
||||
unit_cost = rng.uniform(*rng_range) if rng_range else item.unit_cost
|
||||
total += item.quantity * unit_cost
|
||||
feasibles.append(fraction * total)
|
||||
deciles = statistics.quantiles(feasibles, n=10, method="inclusive")
|
||||
return deciles[0], deciles[4], deciles[8] # P10, P50, P90
|
||||
|
||||
|
||||
def validate_proposal(proposal: SavingsProposal) -> ValidatedProposal | Rejection:
|
||||
"""Deterministic blocking validation. Returns a ``ValidatedProposal`` only when the
|
||||
claim is feasible; otherwise a ``Rejection`` that cannot be consumed as validated."""
|
||||
# Stage 1 (Pydantic) already ran at construction. Stage 2: real CBC solve.
|
||||
nominal = _solve_max_feasible(proposal.affected_items, MAX_SAVING_FRACTION)
|
||||
# Stage 3: Monte Carlo percentiles of the feasible saving.
|
||||
p10, p50, p90 = _monte_carlo(proposal)
|
||||
# Stage 4: structural block — a claim above the optimistic feasible (P90) is out of range.
|
||||
if proposal.claimed_saving_nok > p90:
|
||||
return Rejection(
|
||||
proposal=proposal,
|
||||
reason=f"claimed saving {proposal.claimed_saving_nok:.0f} exceeds P90 feasible {p90:.0f}",
|
||||
)
|
||||
return ValidatedProposal(proposal=proposal, p10=p10, p50=p50, p90=p90, nominal_feasible=nominal)
|
||||
|
||||
|
||||
def self_repair(
|
||||
generate: Callable[[int], SavingsProposal],
|
||||
*,
|
||||
max_attempts: int = 3,
|
||||
) -> ValidatedProposal | Rejection:
|
||||
"""Call ``generate(attempt)`` and validate; retry on rejection up to ``max_attempts``,
|
||||
then hard-stop and return the last rejection. Attempts-bounded — never loops forever
|
||||
(B4). The token-budget bound is layered on by the Step 10 generate loop, not here (this
|
||||
module stays pure: no ``agent_framework`` import)."""
|
||||
if max_attempts <= 0:
|
||||
raise ValueError(f"max_attempts must be positive, got {max_attempts}")
|
||||
last: Rejection | None = None
|
||||
for attempt in range(1, max_attempts + 1):
|
||||
result = validate_proposal(generate(attempt))
|
||||
if isinstance(result, ValidatedProposal):
|
||||
return result
|
||||
last = result
|
||||
assert last is not None
|
||||
return last
|
||||
|
||||
|
||||
def proposal_for(
|
||||
project: Project,
|
||||
codes: list[str],
|
||||
*,
|
||||
claimed_saving_nok: float,
|
||||
measure: str = "Reduce scope on selected cost codes",
|
||||
assumptions: dict[str, tuple[float, float]] | None = None,
|
||||
) -> SavingsProposal:
|
||||
"""Build a ``SavingsProposal`` from a real reference project's cost items (helper)."""
|
||||
items = [
|
||||
AffectedItem(code=ci.code, quantity=ci.quantity, unit_cost=ci.unit_cost)
|
||||
for ci in project.cost_items
|
||||
if ci.code in codes
|
||||
]
|
||||
return SavingsProposal(
|
||||
project_id=project.id,
|
||||
measure=measure,
|
||||
affected_items=items,
|
||||
claimed_saving_nok=claimed_saving_nok,
|
||||
assumptions=assumptions or {},
|
||||
)
|
||||
77
tests/test_validator.py
Normal file
77
tests/test_validator.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
"""Step 2 tests — the promoted blocking validator + IR (B1). Crafted IR, deterministic.
|
||||
|
||||
Determinism is asserted over FIXED inputs (seed 20260624), never through an LLM. The
|
||||
out-of-range path returns a ``Rejection`` that carries no percentiles, so it can never be
|
||||
consumed as validated. Pattern: tests/spikes/test_c_validator.py.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from portfolio_optimiser.ir import SavingsProposal
|
||||
from portfolio_optimiser.reference_domain import load_reference_projects
|
||||
from portfolio_optimiser.validator import (
|
||||
Rejection,
|
||||
ValidatedProposal,
|
||||
proposal_for,
|
||||
validate_proposal,
|
||||
)
|
||||
|
||||
_ASSUMPTIONS = {"05.2": (200.0, 230.0), "03.1": (290.0, 330.0)}
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def project():
|
||||
return load_reference_projects()[0] # FV42-GSV-E1
|
||||
|
||||
|
||||
def _valid(project) -> SavingsProposal:
|
||||
# Affected total ~1.48M NOK; 30% feasible cap ~0.44M. Claim 200k is comfortably feasible.
|
||||
return proposal_for(
|
||||
project, ["05.2", "03.1"], claimed_saving_nok=200_000, assumptions=_ASSUMPTIONS
|
||||
)
|
||||
|
||||
|
||||
def _out_of_range(project) -> SavingsProposal:
|
||||
# Constructs fine (800k <= 1.48M total) but far exceeds the ~0.44M feasible cap.
|
||||
return proposal_for(
|
||||
project, ["05.2", "03.1"], claimed_saving_nok=800_000, assumptions=_ASSUMPTIONS
|
||||
)
|
||||
|
||||
|
||||
def test_valid_proposal_yields_ordered_percentiles(project) -> None:
|
||||
result = validate_proposal(_valid(project))
|
||||
assert isinstance(result, ValidatedProposal)
|
||||
assert result.p10 <= result.p50 <= result.p90 # P10 <= P50 <= P90
|
||||
assert result.nominal_feasible > 0 # the CBC solve produced a feasible bound
|
||||
|
||||
|
||||
def test_out_of_range_is_structurally_blocked(project) -> None:
|
||||
result = validate_proposal(_out_of_range(project))
|
||||
assert isinstance(result, Rejection)
|
||||
assert result.reason # carries a human-readable reason
|
||||
# A Rejection carries no percentiles -> it can never be consumed as validated.
|
||||
with pytest.raises(AttributeError):
|
||||
_ = result.p50 # type: ignore[attr-defined]
|
||||
|
||||
|
||||
def test_monte_carlo_is_reproducible(project) -> None:
|
||||
a = validate_proposal(_valid(project))
|
||||
b = validate_proposal(_valid(project))
|
||||
assert isinstance(a, ValidatedProposal) and isinstance(b, ValidatedProposal)
|
||||
assert (a.p10, a.p50, a.p90) == (b.p10, b.p50, b.p90)
|
||||
|
||||
|
||||
def test_pydantic_blocks_negative_quantity() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
SavingsProposal(
|
||||
project_id="X",
|
||||
measure="bad",
|
||||
affected_items=[{"code": "A", "quantity": -1, "unit_cost": 10}],
|
||||
claimed_saving_nok=1,
|
||||
)
|
||||
|
||||
|
||||
def test_pydantic_blocks_claim_above_affected_total(project) -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
proposal_for(project, ["05.2"], claimed_saving_nok=99_000_000)
|
||||
Loading…
Add table
Add a link
Reference in a new issue