Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

96.4 KB
Raw Blame
from __future__ import annotations

import hashlib
import json
import math
import random
from dataclasses import replace
from datetime import UTC, datetime, timedelta
from pathlib import Path

import pytest
from sie_placement.planner import (
    PlannerInputError,
    PlannerParameters,
    container_hourly_cost,
    envelope_for,
    plan,
)
from sie_placement.report import (
    Blocker,
    ComeAliveBasis,
    IdentityStatus,
    Measurement,
    RefusedRecord,
    RequestReason,
    UnplacedReason,
    render,
)
from sie_placement.schema import (
    BootEvidence,
    BootOutcome,
    Cell,
    ContainerShape,
    Demand,
    DemandHour,
    DemandSeries,
    Envelope,
    IdentityServiceLevel,
    MachineProfilePrice,
    Operation,
    Percentiles,
    Placement,
    Plan,
    PlanIdentity,
    PlanLane,
    PriceList,
    Receipt,
    RequestShape,
    SchemaError,
    ServiceLevel,
    ServiceLevels,
    SourceRef,
    Throughput,
    Warmth,
)

FIXTURES = Path(__file__).parent / "fixtures" / "planner"
START = datetime(2026, 11, 2, tzinfo=UTC)
WINDOW_HOURS = 24
MONTH = 730.0
SCALE = MONTH / WINDOW_HOURS

L4 = Cell(machine_profile="l4", gpu_product="NVIDIA-L4")
L40S = Cell(machine_profile="l40s", gpu_product="NVIDIA-L40S")
A100 = Cell(machine_profile="a100-80gb", gpu_product="NVIDIA-A100-SXM4-80GB")
SHAPE = ContainerShape(gpu_count=1, cpu_cores=4, memory_mib=16384)
WIDE = ContainerShape(gpu_count=1, cpu_cores=8, memory_mib=32768)
IDENTITY = "org/model"
ANY_P99_MS = 1000
WARM = ServiceLevel(warm_p99_ms=ANY_P99_MS)

ESTATE_PARAMETERS = PlannerParameters(
    placement="us-central1",
    max_containers=4,
    fallback_floor=2,
    cooldown_s=900,
    buffer_containers=1,
    bundles={"Qwen/Qwen3-8B": "sglang"},
)


CARD_MIB = {L4: 23034, A100: 81920}


def profile(cell: Cell, accelerator: float, core: float = 0.0, memory: int | None = None) -> MachineProfilePrice:
    return MachineProfilePrice(
        name=cell.machine_profile,
        gpu_product=cell.gpu_product,
        accelerator_per_hour=accelerator,
        core_per_hour=core,
        memory_gib_per_hour=0.0,
        gpu_memory_mib=memory,
    )


def price_list(*, l4=1.0, a100=3.0, core=0.0, multiplier=1.0, extra=(), cards=True) -> PriceList:
    profiles = [
        profile(cell, rate, core, CARD_MIB[cell] if cards else None)
        for cell, rate in ((L4, l4), (A100, a100))
        if rate is not None
    ]
    return PriceList(
        currency="USD",
        machine_profiles=(*profiles, *extra),
        placements=(Placement(name="test", region_multiplier=multiplier),),
    )


L4_ONLY = price_list(a100=None)


def receipt(
    identity=IDENTITY,
    cell=L4,
    *,
    shape=SHAPE,
    concurrency=8,
    rps=10.0,
    p99=100.0,
    uri=None,
    co_residents=(),
    footprint=None,
    working=None,
) -> Receipt:
    beside = "".join(f"+{name}" for name in co_residents)
    return Receipt(
        identity=identity,
        cell=cell,
        container_shape=shape,
        concurrency=concurrency,
        request_shape=RequestShape(operation=Operation.ENCODE, items_per_request=1),
        throughput=Throughput(requests_per_second=rps),
        latency=Percentiles(p99_ms=p99),
        co_residents=tuple(co_residents),
        footprint_mib=footprint,
        working_memory_mib=working,
        source=SourceRef(
            uri=uri or f"receipts/{identity}/{cell.machine_profile}-{concurrency}-{shape.memory_mib}{beside}"
        ),
    )


def generation_receipt(identity=IDENTITY, cell=A100, *, ttft=1000.0, itl=40.0, rps=2.0) -> Receipt:
    return Receipt(
        identity=identity,
        cell=cell,
        container_shape=SHAPE,
        concurrency=16,
        request_shape=RequestShape(operation=Operation.GENERATE, items_per_request=1, output_tokens=256),
        throughput=Throughput(requests_per_second=rps, output_tokens_per_second=rps * 256),
        ttft=Percentiles(p99_ms=ttft),
        itl=Percentiles(p99_ms=itl),
        source=SourceRef(uri=f"receipts/{identity}/{cell.machine_profile}-generate"),
    )


def boot(
    identity=IDENTITY,
    cell=L4,
    warmth=Warmth.COLD_WITH_BOUND,
    seconds=60.0,
    *,
    outcome=BootOutcome.SUCCEEDED,
    borrowed=False,
    shape=SHAPE,
    uri="boots/example.json",
) -> BootEvidence:
    return BootEvidence(
        identity=identity,
        cell=cell,
        container_shape=shape,
        warmth=warmth,
        boot_seconds=seconds,
        outcome=outcome,
        borrowed=borrowed,
        source=SourceRef(uri=uri),
    )


def levels(*entries: tuple[str, ServiceLevel]) -> ServiceLevels:
    return ServiceLevels(identities=tuple(IdentityServiceLevel(identity=name, level=lvl) for name, lvl in entries))


def level(**fields) -> ServiceLevels:
    if not fields.keys() & {"warm_p99_ms", "ttft_p99_ms", "itl_p99_ms"}:
        fields["warm_p99_ms"] = ANY_P99_MS
    return levels((IDENTITY, ServiceLevel(**fields)))


def demand(hourly=None) -> Demand:
    return Demand(
        start=START,
        end=START + timedelta(hours=WINDOW_HOURS),
        series=tuple(
            DemandSeries(
                identity=identity,
                operation=Operation.ENCODE,
                hours=tuple(
                    DemandHour(start=START + timedelta(hours=hour), requests=requests)
                    for hour, requests in sorted(counts.items())
                ),
            )
            for identity, counts in sorted((hourly or {}).items())
        ),
    )


def parameters(**overrides) -> PlannerParameters:
    values = {"placement": "test", "max_containers": 4, "fallback_floor": 2, "cooldown_s": 900} | overrides
    return PlannerParameters(**values)


def run(service_levels, receipts=(), boots=(), *, prices=None, hourly=None, current=None, **overrides):
    return plan(
        service_levels,
        receipts,
        boots,
        prices or price_list(),
        demand(hourly),
        current,
        parameters(**overrides),
    )


def single_lane_plan(
    name="shared",
    warmth=Warmth.PINNED,
    cell=L4,
    identity=IDENTITY,
    *,
    others=(),
    concurrency=8,
    max_containers=2,
    envelope=None,
) -> Plan:
    floor = 1 if warmth is Warmth.PINNED else 0
    return Plan(
        placement="test",
        lanes=(
            PlanLane(
                name=name,
                queue_pool=name if warmth is Warmth.PINNED else "default",
                bundle="default",
                cell=cell,
                container_shape=SHAPE,
                concurrency=concurrency,
                max_containers=max_containers,
                envelope=envelope or Envelope(floor_min=floor, floor_max=max(floor, 1)),
            ),
        ),
        identities=tuple(
            PlanIdentity(identity=resident, lane=name, warmth=warmth) for resident in sorted((identity, *others))
        ),
    )


ANCHOR = "org/anchor"


def plan_lane(name, *, cell=L4, floor=1, bundle="default", queue_pool="default") -> PlanLane:
    return PlanLane(
        name=name,
        queue_pool=queue_pool,
        bundle=bundle,
        cell=cell,
        container_shape=SHAPE,
        concurrency=8,
        max_containers=4,
        envelope=Envelope(floor_min=floor, floor_max=max(floor, 1)),
    )


def plan_of(lanes, residents) -> Plan:
    pinning = {name for name, warmth in residents.values() if warmth is Warmth.PINNED}
    return Plan(
        placement="test",
        lanes=tuple(
            replace(lane, queue_pool=lane.name) if lane.name in pinning and lane.queue_pool == "default" else lane
            for lane in lanes
        ),
        identities=tuple(
            PlanIdentity(identity=identity, lane=name, warmth=warmth)
            for identity, (name, warmth) in sorted(residents.items())
        ),
    )


def resident_levels(bound=60, anchor_bound=0) -> ServiceLevels:
    return levels(
        (ANCHOR, ServiceLevel(come_alive_p95_s=anchor_bound, warm_p99_ms=ANY_P99_MS)),
        (IDENTITY, ServiceLevel(come_alive_p95_s=bound, warm_p99_ms=ANY_P99_MS)),
    )


def resident_estate() -> Plan:
    return plan_of(
        [plan_lane("own"), plan_lane("shared")],
        {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("own", Warmth.PINNED)},
    )


RESIDENT_RECEIPTS = [receipt(identity=ANCHOR, footprint=6000, working=2000), receipt(footprint=3000, working=1000)]
LOADS = [boot(warmth=Warmth.LAZY_RESIDENT, seconds=20.0, uri="loads/example.json")]


def load(name: str):
    return json.loads((FIXTURES / name).read_text(encoding="utf-8"))


def estate() -> dict:
    return {
        "service_levels": ServiceLevels.from_dict(load("service-levels.json")),
        "receipts": [Receipt.from_dict(document) for document in load("receipts.json")],
        "boot_evidence": [BootEvidence.from_dict(document) for document in load("boot-evidence.json")],
        "price_list": PriceList.from_dict(load("price-list.json")),
        "demand": Demand.from_dict(load("demand.json")),
        "current_plan": Plan.from_dict(load("current-plan.json")),
        "parameters": ESTATE_PARAMETERS,
    }


def rendered(result) -> tuple[str, str]:
    return render(result.plan.to_dict()), render(result.report.to_dict())


def loosened(service_levels: ServiceLevels, identity: str, bound: int) -> ServiceLevels:
    return replace(
        service_levels,
        identities=tuple(
            replace(entry, level=replace(entry.level, come_alive_p95_s=bound)) if entry.identity == identity else entry
            for entry in service_levels.identities
        ),
    )


LEVEL_FIELDS = {
    "comeAliveP95S": "come_alive_p95_s",
    "warmP99Ms": "warm_p99_ms",
    "ttftP99Ms": "ttft_p99_ms",
    "itlP99Ms": "itl_p99_ms",
}


def relaxed(service_levels: ServiceLevels, identity: str, blockers) -> ServiceLevels:
    fields = {LEVEL_FIELDS[blocker.field]: max(1, math.ceil(blocker.measured)) for blocker in blockers}
    return replace(
        service_levels,
        identities=tuple(
            replace(entry, level=replace(entry.level, **fields)) if entry.identity == identity else entry
            for entry in service_levels.identities
        ),
    )


def test_container_hourly_cost_prices_accelerators_cores_and_memory():
    price = MachineProfilePrice(
        name="l4", gpu_product="NVIDIA-L4", accelerator_per_hour=2.0, core_per_hour=0.05, memory_gib_per_hour=0.01
    )
    shape = ContainerShape(gpu_count=2, cpu_cores=8, memory_mib=20480)
    assert container_hourly_cost(price, shape, 1.5) == pytest.approx(1.5 * (2 * 2.0 + 8 * 0.05 + 20 * 0.01))


def test_a_pinned_cell_costs_its_floor_plus_the_hours_demand_needs_above_it():
    result = run(level(come_alive_p95_s=0), [receipt(rps=1.0)], prices=L4_ONLY, hourly={IDENTITY: {3: 9000, 4: 100}})
    report = result.report.identity(IDENTITY)
    assert report.status is IdentityStatus.PLACED
    assert report.chosen.warmth is Warmth.PINNED
    assert report.chosen.come_alive_basis is ComeAliveBasis.WARM_FLOOR
    assert report.chosen.monthly_cost == pytest.approx(MONTH + (3 - 1) * SCALE)
    assert report.peak_containers == 3
    assert report.demand_requests == 9100


def test_demand_sums_its_operations_in_each_hour():
    hour = START + timedelta(hours=3)
    mixed = Demand(
        start=START,
        end=START + timedelta(hours=WINDOW_HOURS),
        series=tuple(
            DemandSeries(identity=IDENTITY, operation=operation, hours=(DemandHour(start=hour, requests=requests),))
            for operation, requests in ((Operation.ENCODE, 5000), (Operation.SCORE, 4000))
        ),
    )
    result = plan(level(come_alive_p95_s=0), [receipt(rps=1.0)], (), L4_ONLY, mixed, None, parameters())
    report = result.report.identity(IDENTITY)
    assert report.demand_requests == 9000
    assert report.peak_containers == 3
    assert report.chosen.monthly_cost == pytest.approx(MONTH + (3 - 1) * SCALE)


def test_needed_containers_stop_at_the_reviewed_ceiling():
    result = run(
        level(come_alive_p95_s=0), [receipt(rps=1.0)], prices=L4_ONLY, hourly={IDENTITY: {0: 36000}}, max_containers=2
    )
    report = result.report.identity(IDENTITY)
    assert report.chosen.monthly_cost == pytest.approx(MONTH + (2 - 1) * SCALE)
    assert report.peak_containers == 10


def test_a_cold_cell_costs_its_work_and_the_tail_each_burst_holds():
    result = run(
        level(come_alive_p95_s=120), [receipt(rps=10.0)], [boot()], prices=L4_ONLY, hourly={IDENTITY: {0: 1, 5: 10}}
    )
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.warmth is Warmth.COLD_WITH_BOUND
    assert chosen.come_alive_s == 60.0
    one_burst = 1 / 10.0 + 1 * 2 * 900
    capped_hour = 2 * 3600
    assert chosen.monthly_cost == pytest.approx((one_burst + capped_hour) / 3600 * SCALE)


def test_a_saturated_cold_hour_runs_at_most_the_ceiling():
    result = run(
        level(come_alive_p95_s=120),
        [receipt(rps=1.0)],
        [boot()],
        prices=L4_ONLY,
        hourly={IDENTITY: {0: 36000}},
        max_containers=2,
    )
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.warmth is Warmth.COLD_WITH_BOUND
    assert chosen.monthly_cost == pytest.approx(2 * SCALE)


def test_an_active_lane_without_a_fallback_floor_holds_one_container():
    result = run(
        level(come_alive_p95_s=120),
        [receipt(rps=10.0)],
        [boot()],
        prices=L4_ONLY,
        hourly={IDENTITY: {0: 2}},
        fallback_floor=0,
        cooldown_s=60,
    )
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.monthly_cost == pytest.approx((2 / 10.0 + 2 * 1 * 60) / 3600 * SCALE)


def test_the_active_floor_never_exceeds_the_ceiling():
    result = run(
        level(come_alive_p95_s=120),
        [receipt(rps=10.0)],
        [boot()],
        prices=L4_ONLY,
        hourly={IDENTITY: {0: 1}},
        fallback_floor=3,
        max_containers=1,
    )
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.monthly_cost == pytest.approx((1 / 10.0 + 1 * 1 * 900) / 3600 * SCALE)


def test_an_idle_cold_cell_costs_nothing_and_its_lane_keeps_no_floor():
    result = run(level(come_alive_p95_s=120), [receipt()], [boot()], prices=L4_ONLY)
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.warmth is Warmth.COLD_WITH_BOUND
    assert chosen.monthly_cost == 0
    (lane,) = result.plan.lanes
    assert lane.envelope == Envelope(floor_min=0, floor_max=0)
    assert result.plan.identities == (PlanIdentity(identity=IDENTITY, lane=lane.name, warmth=Warmth.COLD_WITH_BOUND),)


def test_a_cold_posture_without_boot_evidence_is_a_measurement_request():
    result = run(level(come_alive_p95_s=120), [receipt()], prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.PINNED
    (request,) = result.report.measurement_requests
    assert request.measure == (Measurement.BOOT_EVIDENCE,)
    assert request.warmth is Warmth.COLD_WITH_BOUND
    assert request.reason is RequestReason.UNMEASURED
    assert (request.cell, request.container_shape) == (L4, SHAPE)
    assert request.cost_bound == 0
    assert request.saving_bound == pytest.approx(MONTH)


@pytest.mark.parametrize("reverse", [False, True])
def test_a_cell_without_boot_evidence_asks_once_at_its_cheapest_receipt(reverse):
    receipts = [receipt(rps=0.01), receipt(concurrency=16, rps=0.02)]
    result = run(
        level(come_alive_p95_s=120),
        receipts[::-1] if reverse else receipts,
        prices=L4_ONLY,
        hourly={IDENTITY: {0: 1}},
    )
    (request,) = result.report.measurement_requests
    assert request.measure == (Measurement.BOOT_EVIDENCE,)
    assert request.cost_bound == pytest.approx((1 / 0.02 + 1 * 2 * 900) / 3600 * SCALE)


def test_snapshot_restores_are_never_requested():
    result = run(level(come_alive_p95_s=120), [receipt()], [boot(seconds=300.0)], prices=L4_ONLY)
    report = result.report
    assert report.identity(IDENTITY).chosen.warmth is Warmth.PINNED
    assert report.measurement_requests == ()
    assert [lever.warmth for lever in report.come_alive_levers] == [Warmth.COLD_WITH_BOUND]


def test_a_failed_boot_is_not_selectable_and_asks_for_a_new_measurement():
    boots = [boot(seconds=900.0, outcome=BootOutcome.TIMED_OUT)]
    result = run(level(come_alive_p95_s=600), [receipt()], boots, prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.PINNED
    (request,) = result.report.measurement_requests
    assert request.reason is RequestReason.FAILED
    assert result.report.come_alive_levers == ()


def test_a_failed_boot_disqualifies_the_successful_ones_beside_it():
    boots = [boot(seconds=40.0, uri="boots/1.json"), boot(seconds=0.0, outcome=BootOutcome.FAILED, uri="boots/2.json")]
    result = run(level(come_alive_p95_s=600), [receipt()], boots, prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.PINNED


def test_fewer_than_twenty_boots_stand_for_the_come_alive_at_their_slowest():
    boots = [boot(seconds=80.0, uri="boots/1.json"), boot(seconds=130.0, uri="boots/2.json")]
    result = run(level(come_alive_p95_s=120), [receipt()], boots, prices=L4_ONLY)
    report = result.report.identity(IDENTITY)
    assert report.chosen.warmth is Warmth.PINNED
    assert report.next_cheaper.blocked_by == (Blocker(field="comeAliveP95S", bound=120, measured=130.0),)


@pytest.mark.parametrize(
    ("seconds", "come_alive"),
    [
        ([50.0] * 18 + [130.0], 130.0),
        ([50.0] * 19 + [130.0], 50.0),
        ([50.0] * 37 + [130.0] * 3, 130.0),
        ([50.0] * 38 + [130.0] * 2, 50.0),
    ],
)
def test_twenty_or_more_boots_stand_for_the_come_alive_at_their_95th_percentile(seconds, come_alive):
    boots = [boot(seconds=value, uri=f"boots/{index}.json") for index, value in enumerate(seconds)]
    result = run(level(come_alive_p95_s=600), [receipt()], boots, prices=L4_ONLY)
    chosen = result.report.identity(IDENTITY).chosen
    assert (chosen.warmth, chosen.come_alive_s) == (Warmth.COLD_WITH_BOUND, come_alive)
    assert rendered(run(level(come_alive_p95_s=600), [receipt()], boots[::-1], prices=L4_ONLY)) == rendered(result)


@pytest.mark.parametrize(
    ("succeeded", "failed", "come_alive"),
    [(18, 1, None), (19, 1, 50.0), (30, 1, 50.0), (29, 2, None), (38, 2, 50.0), (37, 3, None)],
)
def test_a_failed_boot_ranks_above_every_success_at_the_95th_percentile(succeeded, failed, come_alive):
    boots = [boot(seconds=50.0, uri=f"boots/{index}.json") for index in range(succeeded)]
    boots.extend(
        boot(seconds=0.0, outcome=BootOutcome.FAILED, uri=f"boots/failed-{index}.json") for index in range(failed)
    )
    result = run(level(come_alive_p95_s=600), [receipt()], boots, prices=L4_ONLY)
    chosen = result.report.identity(IDENTITY).chosen
    if come_alive is None:
        assert chosen.warmth is Warmth.PINNED
        (request,) = result.report.measurement_requests
        assert request.reason is RequestReason.FAILED
    else:
        assert (chosen.warmth, chosen.come_alive_s) == (Warmth.COLD_WITH_BOUND, come_alive)


def test_boot_seconds_render_the_same_whatever_their_number_type():
    boots = [boot(seconds=60, uri="boots/1.json"), boot(seconds=60.0, uri="boots/2.json")]
    first = run(level(come_alive_p95_s=120), [receipt()], boots, prices=L4_ONLY)
    second = run(level(come_alive_p95_s=120), [receipt()], boots[::-1], prices=L4_ONLY)
    assert rendered(first) == rendered(second)
    assert first.report.to_dict()["identities"][0]["chosen"]["comeAliveS"] == 60.0


def test_own_boot_evidence_replaces_borrowed_evidence():
    borrowed = boot(seconds=150.0, borrowed=True, uri="boots/borrowed.json")
    own = boot(seconds=60.0, uri="boots/own.json")
    result = run(level(come_alive_p95_s=120), [receipt()], [borrowed, own], prices=L4_ONLY)
    chosen = result.report.identity(IDENTITY).chosen
    assert (chosen.warmth, chosen.come_alive_s) == (Warmth.COLD_WITH_BOUND, 60.0)
    assert chosen.come_alive_basis is ComeAliveBasis.BOOT_EVIDENCE
    failed = boot(seconds=0.0, outcome=BootOutcome.FAILED, uri="boots/own-failed.json")
    result = run(level(come_alive_p95_s=200), [receipt()], [boot(seconds=60.0, borrowed=True), failed], prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.PINNED


def test_borrowed_boot_evidence_stands_in_and_is_labelled():
    result = run(level(come_alive_p95_s=120), [receipt()], [boot(seconds=60.0, borrowed=True)], prices=L4_ONLY)
    chosen = result.report.identity(IDENTITY).chosen
    assert (chosen.warmth, chosen.come_alive_s) == (Warmth.COLD_WITH_BOUND, 60.0)
    assert chosen.come_alive_basis is ComeAliveBasis.BORROWED_BOOT_EVIDENCE
    assert chosen.to_dict()["comeAliveBasis"] == "borrowed-boot-evidence"


def test_a_zero_come_alive_bound_never_selects_a_cold_posture():
    boots = [boot(seconds=0.0), boot(warmth=Warmth.SNAPSHOT_COLD, seconds=0.0, uri="boots/snapshot.json")]
    result = run(level(come_alive_p95_s=0), [receipt()], boots, prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.PINNED
    assert result.report.measurement_requests == ()


def test_an_undeclared_come_alive_bound_admits_any_measured_boot():
    result = run(level(warm_p99_ms=250), [receipt()], [boot(seconds=5000.0)], prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.COLD_WITH_BOUND


def test_a_snapshot_restore_under_the_bound_beats_a_slow_boot():
    boots = [boot(seconds=300.0), boot(warmth=Warmth.SNAPSHOT_COLD, seconds=20.0, uri="boots/snapshot.json")]
    result = run(level(come_alive_p95_s=60), [receipt()], boots, prices=L4_ONLY)
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.warmth is Warmth.SNAPSHOT_COLD
    assert chosen.come_alive_s == 20.0
    assert result.plan.identities[0].warmth is Warmth.SNAPSHOT_COLD


def test_equal_costs_prefer_the_faster_come_alive():
    boots = [boot(seconds=50.0), boot(warmth=Warmth.SNAPSHOT_COLD, seconds=20.0, uri="boots/snapshot.json")]
    result = run(level(come_alive_p95_s=60), [receipt()], boots, prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.SNAPSHOT_COLD


def test_a_cell_whose_receipt_misses_the_warm_bound_is_never_chosen():
    receipts = [receipt(cell=L4, p99=300.0), receipt(cell=A100, p99=120.0)]
    result = run(level(come_alive_p95_s=0, warm_p99_ms=250), receipts)
    report = result.report.identity(IDENTITY)
    assert report.chosen.cell == A100
    assert report.chosen.monthly_cost == pytest.approx(3 * MONTH)
    assert report.next_cheaper.cell == L4
    assert report.next_cheaper.blocked_by == (Blocker(field="warmP99Ms", bound=250, measured=300.0),)
    assert report.next_cheaper.saving == pytest.approx(2 * MONTH)
    assert result.plan.lanes[0].cell == A100


def test_generation_cells_meet_both_the_first_token_and_the_per_token_bound():
    receipts = [generation_receipt(cell=L4, ttft=2000.0, itl=95.0), generation_receipt(cell=A100, ttft=1200.0)]
    result = run(level(come_alive_p95_s=0, ttft_p99_ms=1500, itl_p99_ms=80), receipts)
    report = result.report.identity(IDENTITY)
    assert report.chosen.cell == A100
    assert report.next_cheaper.blocked_by == (
        Blocker(field="ttftP99Ms", bound=1500, measured=2000.0),
        Blocker(field="itlP99Ms", bound=80, measured=95.0),
    )
    assert report.next_cheaper.saving == pytest.approx(2 * MONTH)


def test_a_receipt_whose_realized_product_differs_from_its_profile_is_refused():
    faster = Cell(machine_profile="l4", gpu_product="NVIDIA-L40S")
    result = run(level(come_alive_p95_s=0), [receipt(cell=faster), receipt(cell=A100)])
    report = result.report.identity(IDENTITY)
    assert report.chosen.cell == A100
    (refusal,) = result.report.refusals
    assert refusal.record is RefusedRecord.RECEIPT
    assert refusal.cell == faster
    assert refusal.reason == (
        "realized GPU product 'NVIDIA-L40S' differs from 'NVIDIA-L4', the product machine profile 'l4' requests"
    )
    (request,) = result.report.measurement_requests
    assert (request.cell, request.reason, request.measure) == (L4, RequestReason.REFUSED, (Measurement.RECEIPT,))
    assert request.saving_bound == pytest.approx(2 * MONTH)
    assert all(lane.cell != faster for lane in result.plan.lanes)


def test_boot_evidence_whose_realized_product_differs_from_its_profile_is_refused():
    other = Cell(machine_profile="l4", gpu_product="NVIDIA-A10G")
    result = run(level(come_alive_p95_s=120), [receipt()], [boot(cell=other)], prices=L4_ONLY)
    assert result.report.identity(IDENTITY).chosen.warmth is Warmth.PINNED
    (refusal,) = result.report.refusals
    assert refusal.record is RefusedRecord.BOOT_EVIDENCE
    (request,) = result.report.measurement_requests
    assert (request.measure, request.reason) == ((Measurement.BOOT_EVIDENCE,), RequestReason.UNMEASURED)


def test_a_receipt_on_an_unpriced_profile_is_refused():
    unpriced = Cell(machine_profile="t4", gpu_product="NVIDIA-T4")
    result = run(level(come_alive_p95_s=0), [receipt(cell=unpriced)], prices=L4_ONLY)
    report = result.report.identity(IDENTITY)
    assert report.status is IdentityStatus.UNPLACED
    assert report.unplaced_reason is UnplacedReason.NO_RECEIPT
    assert result.report.refusals[0].reason == "machine profile 't4' has no price"
    (request,) = result.report.measurement_requests
    assert (request.cell, request.reason, request.saving_bound) == (L4, RequestReason.UNMEASURED, None)


@pytest.mark.parametrize(
    ("service_level", "candidate", "reason"),
    [
        (
            ServiceLevel(ttft_p99_ms=1000, itl_p99_ms=50),
            receipt(),
            "the receipt reports one latency and the service level declares ttftP99Ms and itlP99Ms",
        ),
        (
            ServiceLevel(warm_p99_ms=250),
            generation_receipt(cell=L4),
            "the receipt reports ttft and itl and the service level declares warmP99Ms",
        ),
    ],
)
def test_a_receipt_that_cannot_answer_the_service_level_is_refused(service_level, candidate, reason):
    result = run(levels((IDENTITY, service_level)), [candidate], prices=L4_ONLY)
    assert result.report.identity(IDENTITY).status is IdentityStatus.UNPLACED
    assert [refusal.reason for refusal in result.report.refusals] == [reason]


def test_records_of_identities_without_a_service_level_are_ignored():
    stranger = Cell(machine_profile="l4", gpu_product="NVIDIA-A10G")
    result = run(
        level(come_alive_p95_s=0),
        [receipt(), receipt(identity="org/other", cell=stranger)],
        [boot(identity="org/other", cell=stranger)],
        prices=L4_ONLY,
    )
    assert result.report.refusals == ()
    assert [entry.identity for entry in result.plan.identities] == [IDENTITY]
    assert [entry.identity for entry in result.report.identities] == [IDENTITY]


def test_an_unmeasured_cell_is_bounded_by_its_accelerator_price_alone():
    result = run(level(come_alive_p95_s=0), [receipt(cell=A100)], prices=price_list(core=0.5))
    chosen = result.report.identity(IDENTITY).chosen
    assert chosen.monthly_cost == pytest.approx((3.0 + 4 * 0.5) * MONTH)
    (request,) = result.report.measurement_requests
    assert (request.cell, request.measure, request.warmth) == (L4, (Measurement.RECEIPT,), Warmth.PINNED)
    assert request.cost_bound == pytest.approx(1.0 * MONTH)
    assert request.saving_bound == pytest.approx((5.0 - 1.0) * MONTH)


def test_an_unmeasured_cell_under_a_finite_bound_is_bounded_by_its_burst_tails():
    result = run(level(come_alive_p95_s=300), [receipt(cell=A100)], hourly={IDENTITY: {0: 1}})
    requests = {request.cell: request for request in result.report.measurement_requests}
    request = requests[L4]
    assert request.measure == (Measurement.RECEIPT, Measurement.BOOT_EVIDENCE)
    assert request.warmth is Warmth.COLD_WITH_BOUND
    assert request.cost_bound == pytest.approx(1.0 * (1 * 2 * 900) / 3600 * SCALE)
    assert requests[A100].measure == (Measurement.BOOT_EVIDENCE,)


def test_a_cell_that_cannot_save_is_not_requested():
    result = run(level(come_alive_p95_s=0), [receipt(cell=L4)])
    assert result.report.measurement_requests == ()


def test_measurement_requests_rank_unplaced_identities_first_then_by_saving():
    service_levels = levels(
        ("org/big", ServiceLevel(come_alive_p95_s=0, warm_p99_ms=ANY_P99_MS)),
        ("org/small", ServiceLevel(come_alive_p95_s=120, warm_p99_ms=ANY_P99_MS)),
        ("org/unplaced", ServiceLevel(come_alive_p95_s=0)),
    )
    receipts = [receipt(identity="org/big", cell=A100), receipt(identity="org/small", cell=L4)]
    result = run(service_levels, receipts)
    ranked = [
        (request.identity, request.cell.machine_profile, request.measure, request.saving_bound)
        for request in result.report.measurement_requests
    ]
    assert ranked == [
        ("org/unplaced", "a100-80gb", (Measurement.RECEIPT,), None),
        ("org/unplaced", "l4", (Measurement.RECEIPT,), None),
        ("org/big", "l4", (Measurement.RECEIPT,), pytest.approx(2 * MONTH)),
        ("org/small", "a100-80gb", (Measurement.RECEIPT, Measurement.BOOT_EVIDENCE), pytest.approx(MONTH)),
        ("org/small", "l4", (Measurement.BOOT_EVIDENCE,), pytest.approx(MONTH)),
    ]


def test_the_next_cheaper_option_is_the_nearest_cheaper_one_and_names_its_bound():
    prices = price_list(extra=(profile(L40S, 2.0),))
    receipts = [receipt(cell=L4, p99=400.0), receipt(cell=L40S, p99=300.0), receipt(cell=A100, p99=100.0)]
    result = run(level(come_alive_p95_s=0, warm_p99_ms=250), receipts, prices=prices)
    report = result.report.identity(IDENTITY)
    assert report.chosen.cell == A100
    assert report.next_cheaper.cell == L40S
    assert report.next_cheaper.saving == pytest.approx(MONTH)
    assert report.next_cheaper.blocked_by == (Blocker(field="warmP99Ms", bound=250, measured=300.0),)


@pytest.mark.parametrize(("others", "saving"), [((), MONTH), (("org/kept",), 0.0)])
def test_a_next_cheaper_saving_is_what_loosening_its_bounds_saves_the_plan(others, saving):
    service_levels = levels(*((name, ServiceLevel(come_alive_p95_s=0)) for name in (IDENTITY, *others)))
    current = single_lane_plan(others=others)
    receipts = [receipt(), *([receipt(co_residents=others)] if others else [])]
    result = run(service_levels, receipts, [boot(seconds=30.0)], prices=L4_ONLY, current=current)
    report = result.report.identity(IDENTITY)
    assert (report.lane, report.chosen.warmth) == ("shared", Warmth.PINNED)
    assert (report.next_cheaper.warmth, report.next_cheaper.monthly_cost) == (Warmth.COLD_WITH_BOUND, 0.0)
    assert report.chosen.monthly_cost - report.next_cheaper.monthly_cost == pytest.approx(MONTH)
    assert report.next_cheaper.saving == pytest.approx(saving)
    again = run(
        loosened(service_levels, IDENTITY, 30), receipts, [boot(seconds=30.0)], prices=L4_ONLY, current=result.plan
    )
    assert again.report.identity(IDENTITY).chosen.warmth is (Warmth.PINNED if others else Warmth.COLD_WITH_BOUND)
    assert result.report.monthly_cost - again.report.monthly_cost == pytest.approx(saving)


def test_a_placement_with_nothing_cheaper_names_no_next_option():
    result = run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY)
    assert result.report.identity(IDENTITY).next_cheaper is None


@pytest.mark.parametrize(
    ("warmth", "buffer", "fallback", "ceiling", "expected"),
    [
        (Warmth.PINNED, 0, 2, 4, (1, 1)),
        (Warmth.PINNED, 2, 2, 4, (1, 3)),
        (Warmth.PINNED, 5, 2, 4, (1, 4)),
        (Warmth.COLD_WITH_BOUND, 0, 2, 4, (0, 0)),
        (Warmth.COLD_WITH_BOUND, 1, 2, 4, (0, 2)),
        (Warmth.COLD_WITH_BOUND, 3, 2, 4, (0, 3)),
        (Warmth.COLD_WITH_BOUND, 1, 0, 4, (0, 1)),
        (Warmth.COLD_WITH_BOUND, 1, 3, 2, (0, 2)),
        (Warmth.SNAPSHOT_COLD, 1, 2, 4, (0, 2)),
    ],
)
def test_envelopes_add_the_declared_buffer_at_realizable_floors(warmth, buffer, fallback, ceiling, expected):
    envelope = envelope_for(
        warmth, parameters(buffer_containers=buffer, fallback_floor=fallback, max_containers=ceiling)
    )
    assert (envelope.floor_min, envelope.floor_max) == expected
    assert (envelope.buffer_min, envelope.scaledown_window_min_s) == (None, None)


def test_the_plan_carries_the_envelope_and_the_report_prices_its_maximum():
    result = run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY, buffer_containers=2)
    (lane,) = result.plan.lanes
    assert lane.envelope == Envelope(floor_min=1, floor_max=3)
    assert (lane.max_containers, lane.concurrency, lane.container_shape) == (4, 8, SHAPE)
    (lane_report,) = result.report.lanes
    assert lane_report.hourly_cost == pytest.approx(1.0)
    assert lane_report.floor_monthly_cost == pytest.approx(MONTH)
    assert lane_report.envelope_max_monthly_cost == pytest.approx(3 * MONTH)
    assert result.report.envelope_max_monthly_cost == pytest.approx(3 * MONTH)
    assert result.report.monthly_cost == pytest.approx(MONTH)
    assert lane_report.monthly_cost == pytest.approx(MONTH)
    assert result.report.identity(IDENTITY).monthly_cost == pytest.approx(MONTH)
    assert result.report.unpriced_lanes == ()


def test_every_declared_queue_wait_bound_is_reported_unattained():
    service_levels = levels(
        ("org/a", ServiceLevel(come_alive_p95_s=0, queue_wait_p99_ms=500)),
        ("org/b", ServiceLevel(come_alive_p95_s=0)),
        ("org/c", ServiceLevel(queue_wait_p99_ms=30000)),
    )
    result = run(service_levels, [receipt(identity="org/a")], prices=L4_ONLY)
    assert result.report.queue_wait_unattained == ("org/a", "org/c")


def test_an_identity_without_a_feasible_cell_keeps_its_current_lane():
    current = single_lane_plan()
    result = run(level(come_alive_p95_s=0, warm_p99_ms=50), [receipt(p99=100.0)], prices=L4_ONLY, current=current)
    assert result.plan == current
    report = result.report.identity(IDENTITY)
    assert report.status is IdentityStatus.KEPT
    assert report.unplaced_reason is UnplacedReason.WARM_BOUND_MISSED
    assert report.lane == "shared"
    assert report.current == current.identities[0]
    assert result.report.unchanged
    assert result.report.lanes[0].floor_monthly_cost == pytest.approx(MONTH)
    assert report.monthly_cost == pytest.approx(MONTH)
    assert report.blocked_by == (Blocker(field="warmP99Ms", bound=50, measured=100.0),)
    assert result.report.monthly_cost == pytest.approx(MONTH)
    assert result.report.to_dict()["totals"] == {
        "monthlyCost": MONTH,
        "envelopeMaxMonthlyCost": MONTH,
        "unpricedLanes": [],
    }


@pytest.mark.parametrize("reverse", [False, True])
def test_a_kept_lane_prices_its_demand_at_the_slowest_receipt_for_its_cell(reverse):
    current = single_lane_plan(max_containers=4, envelope=Envelope(floor_min=1, floor_max=2))
    receipts = [receipt(rps=1.0, p99=100.0, uri="receipts/slow"), receipt(rps=2.0, p99=100.0, uri="receipts/fast")]
    result = run(
        level(come_alive_p95_s=0, warm_p99_ms=50),
        receipts[::-1] if reverse else receipts,
        prices=L4_ONLY,
        hourly={IDENTITY: {3: 9000}},
        current=current,
    )
    assert result.plan == current
    report = result.report.identity(IDENTITY)
    assert report.status is IdentityStatus.KEPT
    assert report.monthly_cost == pytest.approx(MONTH + (3 - 1) * SCALE)
    (lane,) = result.report.lanes
    assert lane.monthly_cost == pytest.approx(MONTH + (3 - 1) * SCALE)
    assert result.report.monthly_cost == pytest.approx(MONTH + (3 - 1) * SCALE)
    assert result.report.envelope_max_monthly_cost == pytest.approx(2 * MONTH)


@pytest.mark.parametrize(
    ("warmth", "ceiling", "hourly", "hours"),
    [
        (Warmth.PINNED, 2, {3: 9000}, MONTH + (2 - 1) * SCALE),
        (Warmth.COLD_WITH_BOUND, 1, {0: 1}, (1 / 1.0 + 1 * 1 * 900) / 3600 * SCALE),
    ],
)
def test_a_kept_lane_prices_its_demand_under_its_own_ceiling(warmth, ceiling, hourly, hours):
    current = single_lane_plan(warmth=warmth, max_containers=ceiling)
    result = run(
        level(come_alive_p95_s=600, warm_p99_ms=50),
        [receipt(rps=1.0, p99=100.0)],
        prices=L4_ONLY,
        hourly={IDENTITY: hourly},
        current=current,
    )
    assert result.plan == current
    assert result.report.identity(IDENTITY).monthly_cost == pytest.approx(hours)


def test_a_kept_lane_without_a_receipt_for_its_demand_lists_it_unpriced():
    current = single_lane_plan()
    result = run(level(come_alive_p95_s=0), [], prices=L4_ONLY, hourly={IDENTITY: {0: 10}}, current=current)
    report = result.report.identity(IDENTITY)
    assert (report.status, report.monthly_cost) == (IdentityStatus.KEPT, None)
    (lane,) = result.report.lanes
    assert lane.unpriced_identities == (IDENTITY,)
    assert lane.monthly_cost == pytest.approx(MONTH)
    assert result.report.unpriced_lanes == ("shared",)
    assert result.report.monthly_cost == pytest.approx(MONTH)


def test_a_kept_lane_on_a_refused_cell_is_kept_and_reported_unpriced():
    other = Cell(machine_profile="l4", gpu_product="NVIDIA-A10G")
    current = single_lane_plan(cell=other)
    result = run(level(come_alive_p95_s=0), [], prices=L4_ONLY, current=current)
    assert result.plan == current
    (lane,) = result.report.lanes
    assert lane.unpriced_reason == (
        "realized GPU product 'NVIDIA-A10G' differs from 'NVIDIA-L4', the product machine profile 'l4' requests"
    )
    assert (lane.hourly_cost, lane.monthly_cost, lane.envelope_max_monthly_cost) == (None, None, None)
    assert result.report.identity(IDENTITY).monthly_cost is None
    assert result.report.to_dict()["totals"] == {
        "monthlyCost": 0.0,
        "envelopeMaxMonthlyCost": 0.0,
        "unpricedLanes": ["shared"],
    }


@pytest.mark.parametrize(
    "cell",
    [Cell(machine_profile="l4", gpu_product="NVIDIA-A10G"), Cell(machine_profile="h100", gpu_product="NVIDIA-H100")],
)
def test_a_current_lane_on_a_refused_cell_is_kept_beside_a_measured_priced_option(cell):
    current = single_lane_plan(cell=cell)
    result = run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY, current=current)
    assert result.plan == current
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason) == (IdentityStatus.KEPT, UnplacedReason.CURRENT_UNPRICED)
    assert report.to_dict()["unplacedReason"] == "current-unpriced"
    assert result.report.unpriced_lanes == ("shared",)
    assert result.report.measurement_requests == ()


def test_a_kept_lane_never_exceeds_the_reviewed_ceiling():
    current = single_lane_plan(max_containers=4, envelope=Envelope(floor_min=2, floor_max=3))
    result = run(level(come_alive_p95_s=0), [], prices=L4_ONLY, current=current, max_containers=1)
    (lane,) = result.plan.lanes
    assert (lane.max_containers, lane.envelope) == (1, Envelope(floor_min=1, floor_max=1))
    (change,) = result.report.lane_changes
    assert (change.name, change.before, change.after) == ("shared", current.lanes[0], lane)


@pytest.mark.parametrize(
    ("bound", "boots", "expected"),
    [
        (0, (), (Blocker(field="comeAliveP95S", bound=0, measured=None),)),
        (120, (), ()),
        (120, (boot(seconds=200.0),), (Blocker(field="comeAliveP95S", bound=120, measured=200.0),)),
        (300, (boot(seconds=200.0),), ()),
        (120, (boot(outcome=BootOutcome.FAILED),), (Blocker(field="comeAliveP95S", bound=120, measured=None),)),
        (None, (boot(outcome=BootOutcome.TIMED_OUT),), (Blocker(field="comeAliveP95S", bound=None, measured=None),)),
    ],
)
def test_a_kept_cold_placement_names_the_come_alive_bound_it_is_known_to_miss(bound, boots, expected):
    current = single_lane_plan(warmth=Warmth.COLD_WITH_BOUND)
    result = run(level(come_alive_p95_s=bound), [], boots, prices=L4_ONLY, current=current)
    report = result.report.identity(IDENTITY)
    assert report.status is IdentityStatus.KEPT
    assert report.blocked_by == expected
    assert report.monthly_cost == 0
    if expected and expected[0].measured is None:
        bounded = {} if bound is None else {"bound": bound}
        assert report.to_dict()["blockedBy"] == [{"field": "comeAliveP95S", **bounded}]


def test_an_identity_without_a_feasible_cell_or_a_current_lane_is_left_out():
    result = run(level(come_alive_p95_s=0), [], prices=L4_ONLY)
    assert (result.plan.lanes, result.plan.identities) == ((), ())
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason) == (IdentityStatus.UNPLACED, UnplacedReason.NO_RECEIPT)
    assert result.report.monthly_cost == 0


def test_lane_names_derive_from_the_identity_and_its_machine_profile():
    result = run(levels(("BAAI/bge-m3:sparse", WARM)), [receipt(identity="BAAI/bge-m3:sparse")])
    assert result.plan.lanes[0].name == "baai-bge-m3-sparse-l4"


def test_colliding_lane_names_take_an_identity_digest():
    service_levels = levels(("org/model", WARM), ("org-model", WARM))
    receipts = [receipt(identity="org/model"), receipt(identity="org-model")]
    result = run(service_levels, receipts, prices=L4_ONLY)
    names = {entry.identity: entry.lane for entry in result.plan.identities}
    assert names == {
        identity: f"org-model-l4-{hashlib.sha256(identity.encode()).hexdigest()[:8]}" for identity in names
    }
    assert len(set(names.values())) == 2


def test_a_derived_lane_name_never_takes_a_kept_lane_name():
    current = single_lane_plan(name="org-model-l4", identity="org/kept")
    service_levels = levels(("org/kept", ServiceLevel(come_alive_p95_s=0)), (IDENTITY, WARM))
    result = run(service_levels, [receipt()], prices=L4_ONLY, current=current)
    names = {entry.identity: entry.lane for entry in result.plan.identities}
    assert names["org/kept"] == "org-model-l4"
    assert names[IDENTITY] == f"org-model-l4-{hashlib.sha256(IDENTITY.encode()).hexdigest()[:8]}"


def test_a_current_placement_keeps_its_lane_name():
    current = single_lane_plan(name="legacy")
    result = run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY, current=current, max_containers=2)
    assert result.plan == current
    assert result.report.identity(IDENTITY).lane == "legacy"
    assert result.report.unchanged


def test_a_current_placement_keeps_its_lane_as_it_is_under_the_reviewed_ceiling():
    current = single_lane_plan(name="legacy", max_containers=8, envelope=Envelope(floor_min=2, floor_max=5))
    result = run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY, current=current, buffer_containers=2)
    (lane,) = result.plan.lanes
    assert (lane.name, lane.max_containers, lane.envelope) == ("legacy", 4, Envelope(floor_min=2, floor_max=4))
    assert (lane.cell, lane.container_shape, lane.concurrency) == (L4, SHAPE, 8)
    assert result.report.identity(IDENTITY).status is IdentityStatus.PLACED
    assert result.report.identity_changes == ()
    assert [change.name for change in result.report.lane_changes] == ["legacy"]
    within = single_lane_plan(name="legacy", max_containers=4, envelope=Envelope(floor_min=1, floor_max=3))
    assert run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY, current=within).plan == within


def test_a_receipt_at_the_current_concurrency_keeps_the_current_lane():
    current = single_lane_plan(name="legacy", max_containers=4)
    receipts = [receipt(concurrency=16), receipt(concurrency=8)]
    result = run(level(come_alive_p95_s=0), receipts, prices=L4_ONLY, current=current)
    (lane,) = result.plan.lanes
    assert (lane.name, lane.concurrency) == ("legacy", 8)
    assert result.report.unchanged


def test_a_current_lane_without_a_receipt_at_its_concurrency_is_kept_and_asks_for_one():
    current = single_lane_plan(name="legacy", max_containers=4)
    result = run(level(come_alive_p95_s=0), [receipt(concurrency=16)], prices=L4_ONLY, current=current)
    assert result.plan == current
    assert result.report.unchanged
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason) == (IdentityStatus.KEPT, UnplacedReason.CURRENT_UNMEASURED)
    (request,) = result.report.measurement_requests
    assert (request.cell, request.container_shape, request.concurrency, request.lane) == (L4, SHAPE, 8, "legacy")
    assert (request.measure, request.warmth, request.reason) == (
        (Measurement.RECEIPT,),
        Warmth.PINNED,
        RequestReason.UNMEASURED,
    )
    assert (request.cost_bound, request.saving_bound) == (pytest.approx(MONTH), None)
    assert request.to_dict()["lane"] == "legacy"
    assert request.to_dict()["concurrency"] == 8


def test_a_cold_lane_without_boot_evidence_is_kept_and_asks_for_it():
    current = single_lane_plan(name="cold", warmth=Warmth.COLD_WITH_BOUND)
    result = run(level(come_alive_p95_s=120), [receipt()], prices=L4_ONLY, hourly={IDENTITY: {0: 1}}, current=current)
    assert result.plan == current
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason) == (IdentityStatus.KEPT, UnplacedReason.CURRENT_UNMEASURED)
    (request,) = result.report.measurement_requests
    assert (request.measure, request.warmth, request.lane, request.concurrency, request.reason) == (
        (Measurement.BOOT_EVIDENCE,),
        Warmth.COLD_WITH_BOUND,
        "cold",
        8,
        RequestReason.UNMEASURED,
    )
    assert request.cost_bound == pytest.approx((1 / 10.0 + 1 * 2 * 900) / 3600 * SCALE)


@pytest.mark.parametrize("outcome", [BootOutcome.FAILED, BootOutcome.TIMED_OUT])
def test_a_cold_lane_whose_boot_did_not_succeed_moves_to_a_measured_option(outcome):
    current = single_lane_plan(name="cold", warmth=Warmth.COLD_WITH_BOUND)
    boots = [boot(seconds=0.0, outcome=outcome)]
    result = run(
        level(come_alive_p95_s=120), [receipt()], boots, prices=L4_ONLY, hourly={IDENTITY: {0: 1}}, current=current
    )
    report = result.report.identity(IDENTITY)
    assert (report.status, report.chosen.warmth) == (IdentityStatus.PLACED, Warmth.PINNED)
    assert [(entry.lane, entry.warmth) for entry in result.plan.identities] == [("org-model-l4", Warmth.PINNED)]
    (request,) = result.report.measurement_requests
    assert (request.measure, request.warmth, request.reason) == (
        (Measurement.BOOT_EVIDENCE,),
        Warmth.COLD_WITH_BOUND,
        RequestReason.FAILED,
    )
    assert request.saving_bound == pytest.approx(MONTH - (1 / 10.0 + 1 * 2 * 900) / 3600 * SCALE)


def test_a_lazy_resident_whose_load_failed_moves_to_a_measured_option():
    current = plan_of(
        [plan_lane("shared")], {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("shared", Warmth.LAZY_RESIDENT)}
    )
    loads = [boot(warmth=Warmth.LAZY_RESIDENT, outcome=BootOutcome.FAILED, uri="loads/failed.json")]
    result = run(resident_levels(), RESIDENT_RECEIPTS, loads, prices=L4_ONLY, current=current)
    report = result.report.identity(IDENTITY)
    assert (report.status, report.lane, report.chosen.warmth) == (IdentityStatus.PLACED, "org-model-l4", Warmth.PINNED)
    requests = [(request.warmth, request.lane, request.reason) for request in result.report.measurement_requests]
    assert (Warmth.LAZY_RESIDENT, "shared", RequestReason.FAILED) in requests


def test_an_unmeasured_current_lane_moves_to_a_measured_option_that_is_cheaper():
    current = single_lane_plan(name="cold", warmth=Warmth.COLD_WITH_BOUND, cell=A100)
    result = run(
        level(come_alive_p95_s=120),
        [receipt(cell=A100), receipt()],
        [boot()],
        hourly={IDENTITY: {0: 1}},
        current=current,
    )
    report = result.report.identity(IDENTITY)
    assert (report.status, report.chosen.cell, report.chosen.warmth) == (
        IdentityStatus.PLACED,
        L4,
        Warmth.COLD_WITH_BOUND,
    )
    assert [entry.lane for entry in result.plan.identities] == ["org-model-l4"]


def test_a_cold_current_lane_under_a_zero_bound_moves_to_a_warm_floor_without_evidence():
    current = single_lane_plan(name="cold", warmth=Warmth.COLD_WITH_BOUND)
    result = run(level(come_alive_p95_s=0), [receipt()], prices=L4_ONLY, current=current)
    assert [(entry.lane, entry.warmth) for entry in result.plan.identities] == [("org-model-l4", Warmth.PINNED)]
    assert result.report.measurement_requests == ()


def test_an_undeclared_warm_bound_keeps_the_current_class_and_is_reported():
    current = single_lane_plan(cell=A100)
    receipts = [receipt(cell=A100), receipt(p99=300.0)]
    result = run(levels((IDENTITY, ServiceLevel(come_alive_p95_s=0))), receipts, current=current)
    assert result.plan == current
    report = result.report.identity(IDENTITY)
    assert (report.status, report.chosen.cell) == (IdentityStatus.PLACED, A100)
    assert report.next_cheaper.cell == L4
    assert report.next_cheaper.blocked_by == (Blocker(field="warmP99Ms", bound=None, measured=300.0),)
    assert report.next_cheaper.to_dict()["blockedBy"] == [{"field": "warmP99Ms", "measured": 300.0}]
    assert report.next_cheaper.saving == pytest.approx(2 * MONTH)
    assert result.report.warm_bound_undeclared == (IDENTITY,)
    assert result.report.to_dict()["warmBoundUndeclared"] == [IDENTITY]


def test_an_undeclared_generation_bound_blocks_both_token_latencies_off_the_current_class():
    current = single_lane_plan(cell=A100, concurrency=16)
    receipts = [generation_receipt(cell=A100), generation_receipt(cell=L4, ttft=2000.0, itl=95.0)]
    result = run(levels((IDENTITY, ServiceLevel(come_alive_p95_s=0))), receipts, current=current)
    report = result.report.identity(IDENTITY)
    assert report.chosen.cell == A100
    assert report.next_cheaper.blocked_by == (
        Blocker(field="ttftP99Ms", bound=None, measured=2000.0),
        Blocker(field="itlP99Ms", bound=None, measured=95.0),
    )
    assert report.next_cheaper.saving == pytest.approx(2 * MONTH)


def test_an_undeclared_warm_bound_without_a_current_class_places_nothing():
    result = run(levels((IDENTITY, ServiceLevel(come_alive_p95_s=0))), [receipt()], prices=L4_ONLY)
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason) == (IdentityStatus.UNPLACED, UnplacedReason.WARM_BOUND_UNDECLARED)
    assert result.plan.identities == ()
    assert result.report.warm_bound_undeclared == (IDENTITY,)


def test_an_undeclared_warm_bound_without_a_receipt_on_the_current_class_keeps_it():
    current = single_lane_plan(cell=A100)
    result = run(levels((IDENTITY, ServiceLevel(come_alive_p95_s=0))), [receipt()], current=current)
    assert result.plan == current
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason) == (IdentityStatus.KEPT, UnplacedReason.WARM_BOUND_UNDECLARED)
    assert [(request.cell, request.measure, request.lane) for request in result.report.measurement_requests] == [
        (A100, (Measurement.RECEIPT,), "shared")
    ]
    assert report.next_cheaper.saving == pytest.approx(2 * MONTH)


def test_an_identity_without_any_bound_has_no_undeclared_warm_bound_until_it_has_a_cell():
    result = run(levels((IDENTITY, ServiceLevel())), [], prices=L4_ONLY)
    assert result.report.warm_bound_undeclared == ()


def test_a_lazy_resident_without_evidence_keeps_its_place_and_asks_for_it():
    current = plan_of(
        [plan_lane("shared")], {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("shared", Warmth.LAZY_RESIDENT)}
    )
    receipts = [receipt(identity=ANCHOR), receipt(concurrency=16)]
    result = run(
        resident_levels(), receipts, [boot(seconds=30.0)], prices=L4_ONLY, hourly={IDENTITY: {0: 10}}, current=current
    )
    assert result.plan == current
    assert result.report.unchanged
    report = result.report.identity(IDENTITY)
    assert (report.status, report.unplaced_reason, report.lane) == (
        IdentityStatus.KEPT,
        UnplacedReason.CURRENT_UNMEASURED,
        "shared",
    )
    requests = [
        (request.measure, request.warmth, request.lane, request.concurrency, request.saving_bound)
        for request in result.report.measurement_requests
        if request.identity == IDENTITY
    ]
    assert requests == [
        ((Measurement.BOOT_EVIDENCE,), Warmth.LAZY_RESIDENT, "shared", 8, None),
        ((Measurement.FOOTPRINT,), Warmth.LAZY_RESIDENT, "shared", 8, None),
        ((Measurement.RECEIPT,), Warmth.LAZY_RESIDENT, "shared", 8, None),
    ]
    (anchor,) = [request for request in result.report.measurement_requests if request.identity == ANCHOR]
    assert (anchor.measure, anchor.warmth, anchor.lane, anchor.saving_bound) == (
        (Measurement.FOOTPRINT,),
        Warmth.PINNED,
        "shared",
        None,
    )


def test_a_measured_load_onto_a_lane_a_never_cold_neighbour_pins_makes_a_lazy_resident():
    current = resident_estate()
    hourly = {IDENTITY: {3: 90000}}
    result = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, hourly=hourly, current=current)
    report = result.report.identity(IDENTITY)
    assert (report.status, report.lane, report.chosen.warmth, report.chosen.lane) == (
        IdentityStatus.PLACED,
        "shared",
        Warmth.LAZY_RESIDENT,
        "shared",
    )
    assert (report.chosen.come_alive_s, report.chosen.come_alive_basis) == (20.0, ComeAliveBasis.BOOT_EVIDENCE)
    assert report.chosen.monthly_cost == pytest.approx(2 * SCALE)
    assert report.chosen.to_dict()["lane"] == "shared"
    assert result.plan.lanes == (current.lanes[1],)
    assert [(entry.identity, entry.lane, entry.warmth) for entry in result.plan.identities] == [
        (ANCHOR, "shared", Warmth.PINNED),
        (IDENTITY, "shared", Warmth.LAZY_RESIDENT),
    ]
    assert result.report.monthly_cost == pytest.approx(MONTH + 2 * SCALE)
    again = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, hourly=hourly, current=result.plan)
    assert again.plan == result.plan
    assert again.report.unchanged


def test_an_identity_pinned_beside_a_never_cold_neighbour_stays_pinned_there():
    current = plan_of([plan_lane("shared")], {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("shared", Warmth.PINNED)})
    result = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, current=current)
    assert result.plan == current
    assert result.report.unchanged
    assert result.report.come_alive_levers == ()


def test_a_load_over_the_bound_is_a_come_alive_lever():
    result = run(resident_levels(bound=10), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, current=resident_estate())
    report = result.report.identity(IDENTITY)
    assert (report.lane, report.chosen.warmth) == ("own", Warmth.PINNED)
    assert report.next_cheaper.blocked_by == (Blocker(field="comeAliveP95S", bound=10, measured=20.0),)
    (lever,) = result.report.come_alive_levers
    assert (lever.identity, lever.warmth, lever.permitted_bound_s) == (IDENTITY, Warmth.LAZY_RESIDENT, 20)
    assert lever.saving_at_permitted_bound == pytest.approx(MONTH)
    assert lever.saving_under_bound == pytest.approx(MONTH)


def test_a_lane_a_run_creates_for_a_never_cold_identity_hosts_lazy_residents_in_the_same_run():
    current = plan_of([plan_lane("own")], {IDENTITY: ("own", Warmth.PINNED)})
    first = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, current=current)
    assert [(entry.identity, entry.lane, entry.warmth) for entry in first.plan.identities] == [
        (ANCHOR, "org-anchor-l4", Warmth.PINNED),
        (IDENTITY, "org-anchor-l4", Warmth.LAZY_RESIDENT),
    ]
    assert first.report.monthly_cost == pytest.approx(MONTH)
    chosen = first.report.identity(IDENTITY).chosen
    assert (chosen.lane, chosen.come_alive_s, chosen.monthly_cost) == ("org-anchor-l4", 20.0, 0)
    second = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, current=first.plan)
    assert second.plan == first.plan
    assert second.report.unchanged


def test_a_lazy_resident_never_joins_a_lane_its_never_cold_neighbour_leaves():
    current = plan_of(
        [plan_lane("own"), plan_lane("shared", cell=A100)],
        {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("own", Warmth.PINNED)},
    )
    receipts = [receipt(identity=ANCHOR, cell=A100), receipt(identity=ANCHOR), receipt(cell=A100), receipt()]
    loads = [boot(cell=A100, warmth=Warmth.LAZY_RESIDENT, seconds=20.0, uri="loads/a100.json")]
    result = run(resident_levels(), receipts, loads, current=current)
    assert [(entry.identity, entry.lane, entry.warmth) for entry in result.plan.identities] == [
        (ANCHOR, "org-anchor-l4", Warmth.PINNED),
        (IDENTITY, "own", Warmth.PINNED),
    ]
    assert result.report.monthly_cost == pytest.approx(2 * MONTH)
    assert all(request.lane != "shared" for request in result.report.measurement_requests)
    lazy = [request for request in result.report.measurement_requests if request.warmth is Warmth.LAZY_RESIDENT]
    assert [(request.lane, request.measure) for request in lazy] == [
        ("org-anchor-l4", (Measurement.BOOT_EVIDENCE, Measurement.FOOTPRINT))
    ]
    again = run(resident_levels(), receipts, loads, current=result.plan)
    assert again.report.unchanged


def test_a_lazy_resident_whose_lane_keeps_no_never_cold_neighbour_pays_the_lane_floor():
    current = plan_of(
        [plan_lane("shared")], {"org/warm": ("shared", Warmth.PINNED), IDENTITY: ("shared", Warmth.LAZY_RESIDENT)}
    )
    service_levels = levels(
        ("org/warm", ServiceLevel(come_alive_p95_s=120, warm_p99_ms=ANY_P99_MS)),
        (IDENTITY, ServiceLevel(come_alive_p95_s=60, warm_p99_ms=ANY_P99_MS)),
    )
    receipts = [receipt(identity="org/warm", footprint=6000, working=2000), receipt(footprint=3000, working=1000)]
    boots = [boot(identity="org/warm"), *LOADS, boot(seconds=30.0)]
    hourly = {"org/warm": {0: 1}, IDENTITY: {0: 1}}
    result = run(service_levels, receipts, boots, prices=L4_ONLY, hourly=hourly, current=current)
    cold = (1 / 10.0 + 1 * 2 * 900) / 3600 * SCALE
    assert [(entry.identity, entry.lane, entry.warmth) for entry in result.plan.identities] == [
        (IDENTITY, "org-model-l4", Warmth.COLD_WITH_BOUND),
        ("org/warm", "org-warm-l4", Warmth.COLD_WITH_BOUND),
    ]
    assert result.report.monthly_cost == pytest.approx(2 * cold)
    report = result.report.identity(IDENTITY)
    assert report.chosen.monthly_cost == pytest.approx(cold)
    assert report.next_cheaper is None


def test_a_lazy_resident_left_alone_on_its_lane_stays_when_nothing_is_cheaper_than_the_lane_floor():
    current = plan_of([plan_lane("shared")], {IDENTITY: ("shared", Warmth.LAZY_RESIDENT)})
    receipts = [receipt(footprint=3000, working=1000)]
    result = run(level(come_alive_p95_s=60), receipts, LOADS, prices=L4_ONLY, current=current)
    assert result.plan == current
    assert result.report.unchanged
    report = result.report.identity(IDENTITY)
    assert (report.status, report.chosen.warmth, report.chosen.lane) == (
        IdentityStatus.PLACED,
        Warmth.LAZY_RESIDENT,
        "shared",
    )
    assert report.chosen.monthly_cost == pytest.approx(MONTH)
    assert result.report.monthly_cost == pytest.approx(MONTH)


def test_a_lazy_resident_needs_a_lane_of_its_bundle():
    current = plan_of(
        [plan_lane("own"), plan_lane("shared", bundle="sglang")],
        {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("own", Warmth.PINNED)},
    )
    result = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, current=current)
    assert {entry.identity: entry.lane for entry in result.plan.identities} == {
        ANCHOR: "org-anchor-l4",
        IDENTITY: "org-anchor-l4",
    }
    assert all(entry.lane != "shared" for entry in result.plan.identities)
    assert all(request.lane != "shared" for request in result.report.measurement_requests)


def test_a_lazy_resident_joins_a_pinned_lane_on_its_named_queue_pool():
    current = plan_of(
        [plan_lane("own"), plan_lane("shared", queue_pool="batch")],
        {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("own", Warmth.PINNED)},
    )
    hourly = {IDENTITY: {3: 90000}}
    result = run(resident_levels(), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, hourly=hourly, current=current)
    assert [(entry.identity, entry.lane, entry.warmth) for entry in result.plan.identities] == [
        (ANCHOR, "shared", Warmth.PINNED),
        (IDENTITY, "shared", Warmth.LAZY_RESIDENT),
    ]
    assert result.plan.lanes == (current.lanes[1],)


def test_a_lazy_resident_needs_a_never_cold_neighbour_on_a_current_lane_and_on_a_packed_one():
    current = plan_of(
        [plan_lane("own"), plan_lane("shared")], {ANCHOR: ("shared", Warmth.PINNED), IDENTITY: ("own", Warmth.PINNED)}
    )
    result = run(resident_levels(anchor_bound=30), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY, current=current)
    report = result.report.identity(IDENTITY)
    assert (report.lane, report.chosen.warmth) == ("own", Warmth.PINNED)
    assert result.report.monthly_cost == pytest.approx(2 * MONTH)
    assert all(lever.warmth is not Warmth.LAZY_RESIDENT for lever in result.report.come_alive_levers)
    assert all(request.warmth is not Warmth.LAZY_RESIDENT for request in result.report.measurement_requests)
    fresh = run(resident_levels(anchor_bound=30), RESIDENT_RECEIPTS, LOADS, prices=L4_ONLY)
    assert {entry.identity: entry.warmth for entry in fresh.plan.identities} == {
        ANCHOR: Warmth.PINNED,
        IDENTITY: Warmth.PINNED,
    }
    assert len(fresh.plan.lanes) == 2


@pytest.mark.parametrize(
    ("bound", "expected"),
    [
        (
            60,
            [
                (Warmth.COLD_WITH_BOUND, None, (Measurement.BOOT_EVIDENCE,)),
                (Warmth.LAZY_RESIDENT, "shared", (Measurement.BOOT_EVIDENCE,)),
                (Warmth.PINNED, "shared", (Measurement.CO_RESIDENCY_RECEIPT,)),
            ],
        ),
        (0, [(Warmth.PINNED, "shared", (Measurement.CO_RESIDENCY_RECEIPT,))]),
    ],
)
def test_a_warm_lane_without_a_load_row_asks_for_one_at_the_saving_it_unlocks(bound, expected):
    result = run(resident_levels(bound=bound), RESIDENT_RECEIPTS, prices=L4_ONLY, current=resident_estate())
    requests = [request for request in result.report.measurement_requests if request.identity == IDENTITY]
    assert [(request.warmth, request.lane, request.measure) for request in requests] == expected
    for request in requests:
        assert (request.reason, request.cost_bound) == (RequestReason.UNMEASURED, 0)
        assert request.saving_bound == pytest.approx(MONTH)
    (copin,) = [request for request in requests if request.warmth is Warmth.PINNED]
    assert copin.co_residents == (ANCHOR,)
    assert copin.to_dict()["coResidents"] == [ANCHOR]


def test_a_current_placement_on_a_shared_lane_stays_beside_its_kept_neighbour():
    current = single_lane_plan(others=("org/kept",), max_containers=4)
    service_levels = levels(
        (IDENTITY, ServiceLevel(come_alive_p95_s=0)), ("org/kept", ServiceLevel(come_alive_p95_s=0))
    )
    result = run(service_levels, [receipt()], prices=L4_ONLY, current=current, hourly={IDENTITY: {3: 300000}})
    assert result.plan == current
    assert result.report.unchanged
    (lane,) = result.report.lanes
    assert lane.identities == ("org/kept", IDENTITY)
    above = (min(4, math.ceil(300000 / 36000)) - 1) * SCALE
    assert lane.monthly_cost == pytest.approx(MONTH + above)
    assert result.report.identity(IDENTITY).monthly_cost == pytest.approx(MONTH + above)
    assert result.report.identity("org/kept").monthly_cost == pytest.approx(MONTH)
    assert result.report.monthly_cost == pytest.approx(MONTH + above)


def test_current_placements_that_share_a_lane_keep_sharing_it():
    current = single_lane_plan(others=("org/other",), max_containers=4)
    service_levels = levels((IDENTITY, ServiceLevel()), ("org/other", ServiceLevel()))
    result = run(service_levels, [receipt(), receipt(identity="org/other")], prices=L4_ONLY, current=current)
    assert result.plan == current
    assert result.report.unchanged
    assert result.report.monthly_cost == pytest.approx(MONTH)
    assert result.report.shared_chart_lanes == ()


def test_a_placement_that_moves_leaves_its_shared_lane_to_the_kept_neighbour():
    current = single_lane_plan(others=("org/kept",), max_containers=4)
    service_levels = levels(
        (IDENTITY, ServiceLevel(come_alive_p95_s=0, warm_p99_ms=250)), ("org/kept", ServiceLevel(come_alive_p95_s=0))
    )
    receipts = [receipt(p99=300.0), receipt(cell=A100)]
    result = run(service_levels, receipts, current=current)
    names = {entry.identity: entry.lane for entry in result.plan.identities}
    assert names == {IDENTITY: "org-model-a100-80gb", "org/kept": "shared"}
    assert result.report.identity(IDENTITY).current == current.identities[1]


@pytest.mark.parametrize(
    ("overrides", "expected"),
    [
        (
            {"bundles": {IDENTITY: "sglang"}},
            {"org-model-l4": ("org-model-l4", "sglang"), "shared": ("shared", "default")},
        ),
        ({"queue_pool": "batch"}, {"shared": ("shared", "default")}),
    ],
)
def test_a_new_bundle_moves_the_identity_off_its_current_lane_and_a_new_default_queue_pool_does_not(
    overrides, expected
):
    current = single_lane_plan(others=("org/kept",), max_containers=4)
    service_levels = levels(
        (IDENTITY, ServiceLevel(come_alive_p95_s=0)), ("org/kept", ServiceLevel(come_alive_p95_s=0))
    )
    result = run(service_levels, [receipt()], prices=L4_ONLY, current=current, **overrides)
    lanes = {lane.name: (lane.queue_pool, lane.bundle) for lane in result.plan.lanes}
    assert lanes == expected


def test_removing_one_of_two_colliding_identities_keeps_the_other_lane_name():
    service_levels = levels(("org/model", WARM), ("org-model", WARM))
    receipts = [receipt(identity="org/model"), receipt(identity="org-model")]
    first = run(service_levels, receipts, prices=L4_ONLY)
    second = run(levels(("org/model", WARM)), receipts, prices=L4_ONLY, current=first.plan)
    digest = hashlib.sha256(b"org-model").hexdigest()[:8]
    kept_name = f"org-model-l4-{hashlib.sha256(b'org/model').hexdigest()[:8]}"
    assert [entry.lane for entry in second.plan.identities] == [kept_name]
    assert [(change.identity, change.after) for change in second.report.identity_changes] == [("org-model", None)]
    assert [(change.name, change.after) for change in second.report.lane_changes] == [(f"org-model-l4-{digest}", None)]


def test_plan_lanes_that_name_one_chart_lane_are_reported():
    service_levels = levels(("org/a", WARM), ("org/b", WARM), ("org/c", WARM))
    receipts = [receipt(identity=identity) for identity in ("org/a", "org/b", "org/c")]
    boots = [boot(identity=identity) for identity in ("org/a", "org/b", "org/c")]
    result = run(service_levels, receipts, boots, prices=L4_ONLY, bundles={"org/c": "sglang"})
    assert {entry.warmth for entry in result.plan.identities} == {Warmth.COLD_WITH_BOUND}
    (shared,) = result.report.shared_chart_lanes
    assert shared.to_dict() == {
        "queuePool": "default",
        "machineProfile": "l4",
        "bundle": "default",
        "lanes": ["org-a-l4", "org-b-l4"],
    }
    apart = run(service_levels, receipts, boots, prices=L4_ONLY, bundles={"org/a": "a", "org/b": "b"})
    assert apart.report.shared_chart_lanes == ()
    pinned = run(service_levels, receipts, prices=L4_ONLY)
    assert {entry.warmth for entry in pinned.plan.identities} == {Warmth.PINNED}
    assert pinned.report.shared_chart_lanes == ()


def test_each_lane_that_pins_an_identity_gets_its_own_named_queue_pool():
    long = "org/" + "x" * 70
    never_cold = ServiceLevel(come_alive_p95_s=0, warm_p99_ms=ANY_P99_MS)
    service_levels = levels(("org/a", never_cold), ("org/b", never_cold), (long, never_cold), ("org/c", WARM))
    receipts = [
        receipt(identity="org/a"),
        receipt(identity="org/b", cell=A100),
        receipt(identity=long, cell=A100),
        receipt(identity="org/c"),
    ]
    result = run(service_levels, receipts, [boot(identity="org/c")])
    warmths = {entry.identity: (entry.lane, entry.warmth) for entry in result.plan.identities}
    long_lane = warmths[long][0]
    assert warmths == {
        "org/a": ("org-a-l4", Warmth.PINNED),
        "org/b": ("org-b-a100-80gb", Warmth.PINNED),
        long: (long_lane, Warmth.PINNED),
        "org/c": ("org-c-l4", Warmth.COLD_WITH_BOUND),
    }
    pools = {lane.name: lane.queue_pool for lane in result.plan.lanes}
    long_pool = f"{long_lane[:54]}-{hashlib.sha256(long_lane.encode()).hexdigest()[:8]}"
    assert pools == {
        "org-a-l4": "org-a-l4",
        "org-b-a100-80gb": "org-b-a100-80gb",
        long_lane: long_pool,
        "org-c-l4": "default",
    }
    assert len(long_pool) == 63
    again = run(service_levels, receipts, [boot(identity="org/c")], current=result.plan)
    assert again.plan == result.plan
    assert again.report.unchanged


def test_a_replan_keeps_a_named_queue_pool_and_derives_one_for_a_pinned_lane_on_the_default_pool():
    named = single_lane_plan(name="kept", identity="org/kept")
    named_lane = replace(named.lanes[0], queue_pool="org-model-l4")
    unnamed = single_lane_plan(name="org-model-l4")
    unnamed_lane = replace(unnamed.lanes[0], queue_pool="default")
    current = Plan(
        placement="test",
        lanes=(unnamed_lane, named_lane),
        identities=(*unnamed.identities, *named.identities),
    )
    service_levels = levels(
        (IDENTITY, ServiceLevel(come_alive_p95_s=0)), ("org/kept", ServiceLevel(come_alive_p95_s=0))
    )
    result = run(service_levels, [receipt()], prices=L4_ONLY, current=current)
    assert {entry.identity: entry.lane for entry in result.plan.identities} == {
        IDENTITY: "org-model-l4",
        "org/kept": "kept",
    }
    digest = hashlib.sha256(b"org-model-l4").hexdigest()[:8]
    assert {lane.name: lane.queue_pool for lane in result.plan.lanes} == {
        "kept": "org-model-l4",
        "org-model-l4": f"org-model-l4-{digest}",
    }
    assert [(change.name, change.after.queue_pool) for change in result.report.lane_changes] == [
        ("org-model-l4", f"org-model-l4-{digest}")
    ]
    moved = run(service_levels, [receipt()], prices=L4_ONLY, current=current, queue_pool="org-model-l4")
    assert {lane.name: lane.queue_pool for lane in moved.plan.lanes} == {
        "kept": "kept",
        "org-model-l4": f"org-model-l4-{digest}",
    }


def test_a_kept_queue_pool_is_compared_without_regard_to_case():
    kept = [single_lane_plan(name=name, identity=f"org/{name}") for name in ("first", "second", "third")]
    pools = ("Batch", "batch", "Default")
    current = Plan(
        placement="test",
        lanes=tuple(replace(entry.lanes[0], queue_pool=pool) for entry, pool in zip(kept, pools, strict=True)),
        identities=tuple(identity for entry in kept for identity in entry.identities),
    )
    pinned = ServiceLevel(come_alive_p95_s=0)
    service_levels = levels(*((f"org/{name}", pinned) for name in ("first", "second", "third")))
    result = run(service_levels, [], prices=L4_ONLY, current=current)
    assert {lane.name: lane.queue_pool for lane in result.plan.lanes} == {
        "first": "Batch",
        "second": "second",
        "third": "third",
    }


def test_a_derived_queue_pool_is_the_slug_of_the_lane_name():
    current = single_lane_plan(name="Shared_Lane")
    current = replace(current, lanes=(replace(current.lanes[0], queue_pool="default"),))
    result = run(levels((IDENTITY, ServiceLevel(come_alive_p95_s=0))), [receipt()], prices=L4_ONLY, current=current)
    assert {lane.name: lane.queue_pool for lane in result.plan.lanes} == {"Shared_Lane": "shared-lane"}


def test_plan_lanes_whose_tokens_differ_only_in_case_name_one_chart_lane():
    service_levels = levels(("org/a", WARM), ("org/b", WARM))
    receipts = [receipt(identity=identity) for identity in ("org/a", "org/b")]
    boots = [boot(identity=identity) for identity in ("org/a", "org/b")]
    result = run(service_levels, receipts, boots, prices=L4_ONLY, bundles={"org/a": "Default"})
    assert {lane.name: lane.bundle for lane in result.plan.lanes} == {"org-a-l4": "Default", "org-b-l4": "default"}
    (shared,) = result.report.shared_chart_lanes
    assert shared.to_dict() == {
        "queuePool": "default",
        "machineProfile": "l4",
        "bundle": "default",
        "lanes": ["org-a-l4", "org-b-l4"],
    }


def test_a_derived_lane_name_never_takes_a_kept_lane_named_with_its_digest():
    digest = hashlib.sha256(IDENTITY.encode()).hexdigest()
    first = single_lane_plan(name="org-model-l4", identity="org/kept")
    second = single_lane_plan(name=f"org-model-l4-{digest[:8]}", identity="org/kept-too")
    current = Plan(
        placement="test", lanes=(*first.lanes, *second.lanes), identities=(*first.identities, *second.identities)
    )
    service_levels = levels(
        (IDENTITY, WARM),
        ("org/kept", ServiceLevel(come_alive_p95_s=0)),
        ("org/kept-too", ServiceLevel(come_alive_p95_s=0)),
    )
    result = run(service_levels, [receipt()], prices=L4_ONLY, current=current)
    names = {entry.identity: entry.lane for entry in result.plan.identities}
    assert names == {
        IDENTITY: f"org-model-l4-{digest}",
        "org/kept": "org-model-l4",
        "org/kept-too": f"org-model-l4-{digest[:8]}",
    }
    assert {lane.name: lane for lane in result.plan.lanes}[f"org-model-l4-{digest[:8]}"] == second.lanes[0]


def test_bundles_and_the_default_queue_pool_come_from_the_parameters():
    service_levels = levels(("org/embed", WARM), ("org/generate", WARM))
    result = run(
        service_levels,
        [receipt(identity="org/embed"), receipt(identity="org/generate")],
        [boot(identity="org/embed")],
        prices=L4_ONLY,
        queue_pool="batch",
        bundle="default",
        bundles={"org/generate": "sglang"},
    )
    lanes = {lane.name: (lane.queue_pool, lane.bundle) for lane in result.plan.lanes}
    assert lanes == {"org-embed-l4": ("batch", "default"), "org-generate-l4": ("org-generate-l4", "sglang")}


def test_equal_costs_prefer_the_current_placement():
    twin = Cell(machine_profile="l4-b", gpu_product="NVIDIA-L4")
    prices = price_list(a100=None, extra=(profile(twin, 1.0),))
    receipts = [receipt(cell=L4), receipt(cell=twin)]
    first = run(level(come_alive_p95_s=0), receipts, prices=prices)
    assert first.plan.lanes[0].cell == L4
    current = single_lane_plan(name="org-model-l4-b", cell=twin)
    second = run(level(come_alive_p95_s=0), receipts, prices=prices, current=current)
    assert second.plan.lanes[0].cell == twin
    assert second.report.identity(IDENTITY).current == current.identities[0]


def test_the_region_multiplier_scales_every_price():
    result = run(level(come_alive_p95_s=0), [receipt()], prices=price_list(a100=None, multiplier=1.25))
    assert result.report.identity(IDENTITY).chosen.monthly_cost == pytest.approx(1.25 * MONTH)
    assert result.report.lanes[0].hourly_cost == pytest.approx(1.25)


@pytest.mark.parametrize(
    ("overrides", "path"),
    [
        ({"placement": ""}, "placement"),
        ({"max_containers": 0}, "maxContainers"),
        ({"fallback_floor": -1}, "fallbackFloor"),
        ({"cooldown_s": -1}, "cooldownS"),
        ({"buffer_containers": -1}, "bufferContainers"),
        ({"hold_budget_s": -1}, "holdBudgetS"),
        ({"hours_per_month": 0}, "hoursPerMonth"),
        ({"hours_per_month": math.inf}, "hoursPerMonth"),
        ({"queue_pool": "two words"}, "queuePool"),
        ({"bundle": ""}, "bundle"),
        ({"bundles": {"": "sglang"}}, "bundles"),
        ({"bundles": {IDENTITY: ""}}, "bundles['org/model']"),
    ],
)
def test_parameters_are_validated(overrides, path):
    with pytest.raises(SchemaError) as caught:
        parameters(**overrides)
    assert caught.value.path == path


def test_parameters_echo_into_the_report():
    echoed = parameters(bundles={"org/b": "sglang", "org/a": "default"}).to_dict()
    assert echoed == {
        "placement": "test",
        "maxContainers": 4,
        "fallbackFloor": 2,
        "cooldownS": 900,
        "bufferContainers": 0,
        "holdBudgetS": 0,
        "hoursPerMonth": 730.0,
        "queuePool": "default",
        "bundle": "default",
        "bundles": {"org/a": "default", "org/b": "sglang"},
    }
    assert list(echoed["bundles"]) == ["org/a", "org/b"]
    assert parameters(unshared=frozenset({"org/b", "org/a"})).to_dict()["unshared"] == ["org/a", "org/b"]
    with pytest.raises(SchemaError, match="unshared"):
        parameters(unshared=frozenset({"org/a b"}))


def test_the_placement_must_be_in_the_price_list():
    with pytest.raises(PlannerInputError, match="placement 'elsewhere' is not in the price list"):
        run(level(), [receipt()], placement="elsewhere")


def test_the_current_plan_must_be_for_the_same_placement():
    current = Plan(placement="other", lanes=(), identities=())
    with pytest.raises(PlannerInputError, match="the current plan is for placement 'other'"):
        run(level(), [receipt()], current=current)


def test_the_estate_plan_and_report_are_byte_stable():
    plan_text, report_text = rendered(plan(**estate()))
    assert plan_text == (FIXTURES / "expected-plan.json").read_text(encoding="utf-8")
    assert report_text == (FIXTURES / "expected-report.json").read_text(encoding="utf-8")
    assert rendered(plan(**estate())) == (plan_text, report_text)


def test_input_order_does_not_change_the_output():
    baseline = rendered(plan(**estate()))
    shuffler = random.Random(11)  # noqa: S311 -- reproducible test data
    for _ in range(5):
        inputs = estate()
        shuffler.shuffle(inputs["receipts"])
        shuffler.shuffle(inputs["boot_evidence"])
        service_levels = inputs["service_levels"]
        inputs["service_levels"] = ServiceLevels(
            identities=tuple(reversed(service_levels.identities)), tiers=tuple(reversed(service_levels.tiers))
        )
        source = inputs["demand"]
        inputs["demand"] = Demand(start=source.start, end=source.end, series=tuple(reversed(source.series)))
        assert rendered(plan(**inputs)) == baseline


def test_replanning_the_plan_it_wrote_changes_nothing():
    first = plan(**estate())
    second = plan(**{**estate(), "current_plan": first.plan})
    assert second.plan == first.plan
    assert render(second.plan.to_dict()) == render(first.plan.to_dict())
    assert second.report.unchanged
    assert second.report.to_dict()["changes"] == {"identities": [], "lanes": []}


def test_the_estate_plan_parses_as_a_plan_document():
    result = plan(**estate())
    assert Plan.from_dict(json.loads(render(result.plan.to_dict()))) == result.plan


def test_the_estate_places_every_identity_on_a_measured_feasible_cell():
    inputs = estate()
    result = plan(**inputs)
    receipts = {document.source.uri: document for document in inputs["receipts"]}
    for entry in result.report.identities:
        if entry.status is not IdentityStatus.PLACED:
            continue
        chosen = entry.chosen
        assert chosen.blocked_by == ()
        measured = receipts[chosen.receipt.uri]
        assert measured.cell == chosen.cell
        if measured.latency is not None and entry.service_level.warm_p99_ms is not None:
            assert measured.latency.p99_ms <= entry.service_level.warm_p99_ms
        if measured.itl is not None:
            assert measured.ttft.p99_ms <= entry.service_level.ttft_p99_ms
            assert measured.itl.p99_ms <= entry.service_level.itl_p99_ms
        if chosen.warmth is not Warmth.PINNED:
            assert entry.service_level.come_alive_p95_s > 0
            assert chosen.come_alive_s <= entry.service_level.come_alive_p95_s


def test_the_estate_report_states_its_findings():
    report = plan(**estate()).report
    dense = report.identity("BAAI/bge-m3")
    assert (dense.chosen.cell, dense.chosen.warmth, dense.lane) == (L4, Warmth.PINNED, "default-l4-default")
    assert dense.chosen.monthly_cost == pytest.approx(1.04 * MONTH)
    assert dense.next_cheaper.warmth is Warmth.SNAPSHOT_COLD
    assert dense.next_cheaper.blocked_by == (Blocker(field="comeAliveP95S", bound=0, measured=8.5),)
    sparse = report.identity("BAAI/bge-m3:sparse")
    assert (sparse.chosen.warmth, sparse.chosen.come_alive_s) == (Warmth.COLD_WITH_BOUND, 95.0)
    assert sparse.chosen.monthly_cost == pytest.approx(1.04 * (3 / 40 + 3 * 2 * 900) / 3600 * SCALE)
    generation = report.identity("Qwen/Qwen3-8B")
    assert (generation.chosen.cell, generation.chosen.warmth) == (A100, Warmth.COLD_WITH_BOUND)
    assert generation.chosen.monthly_cost == pytest.approx(3.14 * 2 * 2 * SCALE)
    assert generation.next_cheaper.blocked_by == (Blocker(field="itlP99Ms", bound=80, measured=118.0),)
    assert report.identity("Qwen/Qwen3-Reranker-0.6B").status is IdentityStatus.UNPLACED
    kept = report.identity("intfloat/e5-base-v2")
    assert (kept.status, kept.lane, kept.monthly_cost) == (IdentityStatus.KEPT, "default-l4-default", None)
    assert [refusal.record for refusal in report.refusals] == [RefusedRecord.BOOT_EVIDENCE, RefusedRecord.RECEIPT]
    assert report.queue_wait_unattained == ("BAAI/bge-m3", "BAAI/bge-m3:sparse", "intfloat/e5-base-v2")
    assert report.warm_bound_undeclared == ()
    on_lanes = [
        (request.identity, request.measure, request.warmth, request.lane, request.saving_bound)
        for request in report.measurement_requests
        if request.lane is not None
    ]
    assert on_lanes == [
        ("intfloat/e5-base-v2", (Measurement.CO_RESIDENCY_RECEIPT,), Warmth.PINNED, "default-l4-default", None),
        (
            "BAAI/bge-m3:sparse",
            (Measurement.BOOT_EVIDENCE,),
            Warmth.LAZY_RESIDENT,
            "default-l4-default",
            pytest.approx(sparse.chosen.monthly_cost),
        ),
        (
            "intfloat/e5-base-v2",
            (Measurement.FOOTPRINT,),
            Warmth.PINNED,
            "default-l4-default",
            pytest.approx(sparse.chosen.monthly_cost),
        ),
    ]
    assert dense.chosen.co_residents == ("intfloat/e5-base-v2",)
    assert dense.chosen.receipt.uri == "receipts/baai-bge-m3/l4-c16-16384-beside-e5-base-v2.json"
    assert [change.identity for change in report.identity_changes] == ["BAAI/bge-m3:sparse", "Qwen/Qwen3-Embedding-4B"]
    assert report.monthly_cost == pytest.approx(sum(lane.monthly_cost for lane in report.lanes))
    shared = {lane.name: lane for lane in report.lanes}["default-l4-default"]
    assert shared.identities == ("BAAI/bge-m3", "intfloat/e5-base-v2")
    assert shared.monthly_cost == pytest.approx(dense.monthly_cost)
    assert shared.unpriced_identities == ("intfloat/e5-base-v2",)
    assert report.unpriced_lanes == ("default-l4-default",)
    assert report.shared_chart_lanes == ()
    assert {lane.name: lane.queue_pool for lane in plan(**estate()).plan.lanes} == {
        "baai-bge-m3-sparse-l4": "default",
        "default-l4-default": "default-l4-default",
        "qwen-qwen3-8b-a100-80gb": "default",
        "qwen-qwen3-embedding-4b-l4": "qwen-qwen3-embedding-4b-l4",
    }
    current = estate()["current_plan"]
    assert sum(lane.envelope.floor_min for lane in plan(**estate()).plan.lanes) == 1 + sum(
        lane.envelope.floor_min for lane in current.lanes
    )


def test_an_estate_lever_saves_what_planning_again_at_its_permitted_bound_saves():
    inputs = estate()
    result = plan(**inputs)
    report = result.report
    assert [(lever.identity, lever.warmth) for lever in report.come_alive_levers] == [
        ("Qwen/Qwen3-Embedding-4B", Warmth.COLD_WITH_BOUND),
        ("Qwen/Qwen3-Embedding-4B", Warmth.SNAPSHOT_COLD),
    ]
    for lever in report.come_alive_levers:
        service_levels = loosened(inputs["service_levels"], lever.identity, lever.permitted_bound_s)
        again = plan(**{**inputs, "service_levels": service_levels, "current_plan": result.plan})
        assert report.monthly_cost - again.report.monthly_cost == pytest.approx(lever.saving_at_permitted_bound)
        assert lever.saving_at_permitted_bound > 0


def test_an_estate_identity_beside_a_kept_neighbour_offers_no_lever_and_no_saving():
    inputs = estate()
    result = plan(**inputs)
    dense = result.report.identity("BAAI/bge-m3")
    assert (dense.lane, dense.chosen.warmth) == ("default-l4-default", Warmth.PINNED)
    assert dense.next_cheaper.come_alive_s == 8.5
    assert dense.chosen.monthly_cost > dense.next_cheaper.monthly_cost
    assert dense.next_cheaper.saving == 0
    assert not [lever for lever in result.report.come_alive_levers if lever.identity == "BAAI/bge-m3"]
    for bound in (9, 95):
        service_levels = loosened(inputs["service_levels"], "BAAI/bge-m3", bound)
        again = plan(**{**inputs, "service_levels": service_levels, "current_plan": result.plan})
        assert again.report.identity("BAAI/bge-m3").chosen.warmth is Warmth.PINNED
        assert again.plan == result.plan
        assert again.report.monthly_cost == pytest.approx(result.report.monthly_cost)


def test_an_estate_next_cheaper_saving_is_what_planning_again_with_its_bounds_loosened_saves():
    inputs = estate()
    result = plan(**inputs)
    offered = [entry for entry in result.report.identities if entry.next_cheaper is not None]
    assert [entry.identity for entry in offered] == ["BAAI/bge-m3", "Qwen/Qwen3-8B", "Qwen/Qwen3-Embedding-4B"]
    for entry in offered:
        service_levels = relaxed(inputs["service_levels"], entry.identity, entry.next_cheaper.blocked_by)
        again = plan(**{**inputs, "service_levels": service_levels, "current_plan": result.plan})
        fall = result.report.monthly_cost - again.report.monthly_cost
        assert entry.next_cheaper.saving == pytest.approx(max(0.0, fall), abs=0.01)


@pytest.mark.parametrize("seed", range(25))
def test_random_estates_choose_only_feasible_cells_and_replan_to_themselves(seed):
    rng = random.Random(seed)  # noqa: S311 -- reproducible test data
    identities = [f"org/model-{index}" for index in range(rng.randint(1, 5))]
    service_levels = levels(
        *(
            (
                identity,
                ServiceLevel(
                    come_alive_p95_s=rng.choice([None, 0, 30, 120, 600]),
                    warm_p99_ms=rng.choice([None, 150, 250, 400]),
                ),
            )
            for identity in identities
        )
    )
    receipts = [
        receipt(
            identity=identity,
            cell=cell,
            concurrency=rng.choice([4, 8]),
            rps=rng.choice([0.5, 5.0, 50.0]),
            p99=rng.choice([100.0, 200.0, 300.0, 500.0]),
            uri=f"receipts/{identity}/{cell.machine_profile}/{index}",
        )
        for identity in identities
        for index, cell in enumerate([L4, A100, L4])
        if rng.random() < 0.7
    ]
    boots = [
        boot(
            identity=identity,
            cell=cell,
            warmth=warmth,
            seconds=rng.choice([5.0, 60.0, 200.0]),
            outcome=rng.choice([BootOutcome.SUCCEEDED, BootOutcome.SUCCEEDED, BootOutcome.FAILED]),
            borrowed=rng.random() < 0.3,
            uri=f"boots/{identity}/{cell.machine_profile}/{warmth.value}/{index}",
        )
        for identity in identities
        for cell in (L4, A100)
        for warmth in (Warmth.COLD_WITH_BOUND, Warmth.SNAPSHOT_COLD)
        for index in range(2)
        if rng.random() < 0.4
    ]
    hourly = {
        identity: {hour: rng.choice([0, 1, 50, 20000]) for hour in rng.sample(range(WINDOW_HOURS), rng.randint(0, 6))}
        for identity in identities
    }
    prices = price_list(l4=rng.choice([0.5, 1.0]), a100=rng.choice([2.0, 3.0]), core=0.1)
    overrides = {
        "buffer_containers": rng.randint(0, 2),
        "fallback_floor": rng.randint(0, 3),
        "cooldown_s": rng.choice([0, 60, 900]),
    }
    result = run(service_levels, receipts, boots, prices=prices, hourly=hourly, **overrides)
    assert Plan.from_dict(result.plan.to_dict()) == result.plan
    by_uri = {document.source.uri: document for document in receipts}
    for entry in result.report.identities:
        if entry.chosen is None:
            continue
        chosen = entry.chosen
        assert chosen.blocked_by == ()
        bound = entry.service_level.warm_p99_ms
        assert bound is None or by_uri[chosen.receipt.uri].latency.p99_ms <= bound
        come_alive = entry.service_level.come_alive_p95_s
        if chosen.warmth is not Warmth.PINNED:
            assert come_alive is None or come_alive > 0
            assert come_alive is None or chosen.come_alive_s <= come_alive
        if entry.next_cheaper is not None:
            assert entry.next_cheaper.blocked_by
            assert entry.next_cheaper.monthly_cost < chosen.monthly_cost
            looser = relaxed(service_levels, entry.identity, entry.next_cheaper.blocked_by)
            again = run(looser, receipts, boots, prices=prices, hourly=hourly, current=result.plan, **overrides)
            fall = result.report.monthly_cost - again.report.monthly_cost
            assert entry.next_cheaper.saving == pytest.approx(max(0.0, fall), abs=0.01)
    costs = {entry.identity: entry.chosen.monthly_cost for entry in result.report.identities if entry.chosen}
    for lever in result.report.come_alive_levers:
        assert lever.monthly_cost < costs[lever.identity]
        assert lever.saving_at_permitted_bound >= lever.saving_under_bound >= 0
        assert lever.saving_at_permitted_bound > 0
        looser = loosened(service_levels, lever.identity, lever.permitted_bound_s)
        again = run(looser, receipts, boots, prices=prices, hourly=hourly, current=result.plan, **overrides)
        assert result.report.monthly_cost - again.report.monthly_cost == pytest.approx(lever.saving_at_permitted_bound)
        if lever.bound_s == 0:
            assert lever.saving_under_bound == 0
            continue
        key = (lever.identity, lever.cell, lever.container_shape, lever.warmth)
        within = [
            replace(record, boot_seconds=min(record.boot_seconds, lever.bound_s))
            if (record.identity, record.cell, record.container_shape, record.warmth) == key
            else record
            for record in boots
        ]
        booted = run(service_levels, receipts, within, prices=prices, hourly=hourly, current=result.plan, **overrides)
        fall = result.report.monthly_cost - booted.report.monthly_cost
        assert lever.saving_under_bound == pytest.approx(max(0.0, fall), abs=0.01)
    for request in result.report.measurement_requests:
        assert request.saving_bound is None or request.saving_bound > 0
    again = run(service_levels, receipts, boots, prices=prices, hourly=hourly, current=result.plan, **overrides)
    assert again.plan == result.plan
    assert again.report.unchanged
    shuffled = list(receipts)
    rng.shuffle(shuffled)
    reordered = run(service_levels, shuffled, list(reversed(boots)), prices=prices, hourly=hourly, **overrides)
    assert rendered(reordered) == rendered(result)


def capped(lane: PlanLane, ceiling: int) -> PlanLane:
    if lane.max_containers <= ceiling:
        return lane
    envelope = replace(
        lane.envelope,
        floor_min=min(lane.envelope.floor_min, ceiling),
        floor_max=min(lane.envelope.floor_max, ceiling),
    )
    return replace(lane, max_containers=ceiling, envelope=envelope)


@pytest.mark.parametrize("seed", range(200))
def test_random_current_estates_keep_what_no_measured_option_beats(seed):
    rng = random.Random(seed)  # noqa: S311 -- reproducible test data
    identities = [f"org/model-{index}" for index in range(rng.randint(2, 6))]
    lanes = []
    for index in range(rng.randint(1, 3)):
        floor = 1 if index == 0 or rng.random() < 0.6 else 0
        ceiling = rng.choice([2, 4, 6])
        lanes.append(
            PlanLane(
                name=f"lane-{index}",
                queue_pool="default",
                bundle="default",
                cell=rng.choice([L4, A100]),
                container_shape=rng.choice([SHAPE, WIDE]),
                concurrency=rng.choice([4, 8]),
                max_containers=ceiling,
                envelope=Envelope(floor_min=floor, floor_max=min(ceiling, floor + rng.randint(0, 2))),
            )
        )
    anchor_lane = lanes[0]
    residents = {identities[0]: (anchor_lane.name, Warmth.PINNED)}
    for identity in identities[1:]:
        if rng.random() < 0.8:
            host = rng.choice(lanes)
            if host.envelope.floor_min:
                warmth = rng.choice([Warmth.PINNED, Warmth.PINNED, Warmth.LAZY_RESIDENT])
            else:
                warmth = rng.choice([Warmth.COLD_WITH_BOUND, Warmth.SNAPSHOT_COLD])
            residents[identity] = (host.name, warmth)
    hosted = {name for name, _ in residents.values()}
    current = plan_of([lane for lane in lanes if lane.name in hosted], residents)
    service_levels = levels(
        *(
            (
                identity,
                ServiceLevel(
                    come_alive_p95_s=0 if identity == identities[0] else rng.choice([None, 0, 30, 120, 600]),
                    warm_p99_ms=rng.choice([None, 150, 250, 400]),
                ),
            )
            for identity in identities
        )
    )
    on_anchor = (anchor_lane.cell, anchor_lane.container_shape, anchor_lane.concurrency)
    receipts = [
        receipt(
            identity=identity,
            cell=cell,
            shape=shape,
            concurrency=concurrency,
            rps=rng.choice([0.5, 5.0, 50.0]),
            p99=rng.choice([100.0, 200.0, 300.0, 500.0]),
            uri=f"receipts/{identity}/{cell.machine_profile}/{shape.memory_mib}/{concurrency}",
        )
        for identity in identities
        for cell in (L4, A100)
        for shape in (SHAPE, WIDE)
        for concurrency in (4, 8)
        if rng.random() < (0.6 if (cell, shape, concurrency) == on_anchor else 0.3)
    ]
    boots = [
        boot(
            identity=identity,
            cell=cell,
            warmth=warmth,
            seconds=rng.choice([5.0, 60.0, 200.0]),
            outcome=rng.choice([BootOutcome.SUCCEEDED, BootOutcome.SUCCEEDED, BootOutcome.FAILED]),
            shape=shape,
            uri=f"boots/{identity}/{cell.machine_profile}/{shape.memory_mib}/{warmth.value}",
        )
        for identity in identities
        for cell in (L4, A100)
        for shape in (SHAPE, WIDE)
        for warmth in (Warmth.COLD_WITH_BOUND, Warmth.SNAPSHOT_COLD, Warmth.LAZY_RESIDENT)
        if rng.random() < (0.6 if (cell, shape, warmth) == (*on_anchor[:2], Warmth.LAZY_RESIDENT) else 0.3)
    ]
    hourly = {
        identity: {hour: rng.choice([1, 50, 20000]) for hour in rng.sample(range(WINDOW_HOURS), rng.randint(0, 4))}
        for identity in identities
    }
    prices = price_list(l4=rng.choice([0.5, 1.0]), a100=rng.choice([2.0, 3.0]), core=0.1)
    overrides = {"buffer_containers": rng.randint(0, 2), "fallback_floor": rng.randint(0, 3)}
    result = run(service_levels, receipts, boots, prices=prices, hourly=hourly, current=current, **overrides)
    assert Plan.from_dict(result.plan.to_dict()) == result.plan
    before = {lane.name: lane for lane in current.lanes}
    after = {lane.name: lane for lane in result.plan.lanes}
    pinning = {entry.lane for entry in result.plan.identities if entry.warmth is Warmth.PINNED}
    for name, lane in after.items():
        if name in before:
            assert replace(lane, queue_pool=before[name].queue_pool) == capped(before[name], 4)
        assert lane.queue_pool == (name if name in pinning else "default")
    placements = {entry.identity: entry for entry in result.plan.identities}
    never_cold = {entry.identity for entry in service_levels.identities if entry.level.come_alive_p95_s == 0}
    anchored = {
        entry.lane
        for entry in result.plan.identities
        if entry.identity in never_cold and residents.get(entry.identity) == (entry.lane, Warmth.PINNED)
    }
    rates = {price.name: price for price in prices.machine_profiles}
    for entry in result.report.identities:
        level = entry.service_level
        was = residents.get(entry.identity)
        if entry.status is IdentityStatus.KEPT:
            assert (placements[entry.identity].lane, placements[entry.identity].warmth) == was
            continue
        declared = level.warm_p99_ms is not None
        if entry.chosen is None:
            assert entry.identity not in placements
            continue
        chosen = entry.chosen
        assert chosen.blocked_by == ()
        if not declared:
            assert was is not None
            assert chosen.cell == before[was[0]].cell
        if chosen.warmth is not Warmth.PINNED:
            assert level.come_alive_p95_s != 0
            assert level.come_alive_p95_s is None or chosen.come_alive_s <= level.come_alive_p95_s
        if chosen.warmth is Warmth.LAZY_RESIDENT:
            assert chosen.lane in before
            assert before[chosen.lane].envelope.floor_min >= 1
            if chosen.lane not in anchored:
                lane = after[chosen.lane]
                hourly_cost = container_hourly_cost(rates[lane.cell.machine_profile], lane.container_shape, 1.0)
                assert was == (chosen.lane, Warmth.LAZY_RESIDENT)
                assert chosen.monthly_cost >= hourly_cost * lane.envelope.floor_min * MONTH - 0.01
        if entry.next_cheaper is not None:
            looser = relaxed(service_levels, entry.identity, entry.next_cheaper.blocked_by)
            again = run(looser, receipts, boots, prices=prices, hourly=hourly, current=result.plan, **overrides)
            fall = result.report.monthly_cost - again.report.monthly_cost
            assert entry.next_cheaper.saving == pytest.approx(max(0.0, fall), abs=0.01)
    shuffled = list(receipts)
    rng.shuffle(shuffled)
    reordered = run(
        ServiceLevels(identities=tuple(reversed(service_levels.identities))),
        shuffled,
        list(reversed(boots)),
        prices=prices,
        hourly=hourly,
        current=current,
        **overrides,
    )
    assert rendered(reordered) == rendered(result)
    replanned = result
    for _ in range(3):
        again = run(service_levels, receipts, boots, prices=prices, hourly=hourly, current=replanned.plan, **overrides)
        if again.report.unchanged:
            break
        lanes = {lane.name for lane in replanned.plan.lanes}
        for change in again.report.identity_changes:
            assert (change.after.lane in lanes, change.after.warmth) == (True, Warmth.LAZY_RESIDENT)
        assert all(change.after is None for change in again.report.lane_changes)
        replanned = again
    assert again.report.unchanged