From 8abd4acbcb79af7d56e000537f3fcbdd4b07fc11 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Wed, 4 Nov 2026 21:55:33 +0100 Subject: [PATCH 01/22] feat(placement): add versioned planner input and plan schemas Add sie-placement, a standard-library workspace package with strict, versioned documents for a measured-only placement planner: - service levels per served identity (comeAliveP95S, warmP99Ms or the ttftP99Ms and itlP99Ms pair for generation, queueWaitP99Ms), with optional named tiers that identities inherit and override - receipt v1, keyed by machine profile and realized GPU product, with container shape, concurrency, request shape, throughput, latency percentiles and a source reference - boot evidence v1 for cold boots, snapshot restores and resident loads - price list v1 with per-profile accelerator, core and memory rates and a region multiplier per placement - hourly demand per identity and operation over a UTC window - the plan: lanes with cell, shape, concurrency, ceiling and envelope (floorMin, floorMax and optional buffer and scale-down window ranges), and each identity's lane and warmth Every type parses from JSON-compatible dicts with path-qualified errors and serializes back without loss. The package joins the workspace, pytest, lint, typecheck and coverage lists. It stays out of the PyPI distribution list and the release contract. --- .coveragerc | 1 + CONTRIBUTING.md | 1 + packages/sie_placement/README.md | 79 ++ packages/sie_placement/pyproject.toml | 19 + .../src/sie_placement/__init__.py | 0 .../sie_placement/src/sie_placement/schema.py | 1054 +++++++++++++++++ .../tests/fixtures/boot-evidence.json | 21 + .../sie_placement/tests/fixtures/demand.json | 34 + .../sie_placement/tests/fixtures/plan.json | 68 ++ .../tests/fixtures/price-list.json | 31 + .../tests/fixtures/receipt-encode.json | 32 + .../tests/fixtures/receipt-generate.json | 37 + .../tests/fixtures/service-levels.json | 40 + packages/sie_placement/tests/test_schema.py | 528 +++++++++ pyproject.toml | 3 + tools/mise_tasks/lint.bash | 1 + tools/mise_tasks/typecheck.bash | 1 + uv.lock | 16 + 18 files changed, 1966 insertions(+) create mode 100644 packages/sie_placement/README.md create mode 100644 packages/sie_placement/pyproject.toml create mode 100644 packages/sie_placement/src/sie_placement/__init__.py create mode 100644 packages/sie_placement/src/sie_placement/schema.py create mode 100644 packages/sie_placement/tests/fixtures/boot-evidence.json create mode 100644 packages/sie_placement/tests/fixtures/demand.json create mode 100644 packages/sie_placement/tests/fixtures/plan.json create mode 100644 packages/sie_placement/tests/fixtures/price-list.json create mode 100644 packages/sie_placement/tests/fixtures/receipt-encode.json create mode 100644 packages/sie_placement/tests/fixtures/receipt-generate.json create mode 100644 packages/sie_placement/tests/fixtures/service-levels.json create mode 100644 packages/sie_placement/tests/test_schema.py diff --git a/.coveragerc b/.coveragerc index 23e4ec855..3eb71ea6c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -9,6 +9,7 @@ source_pkgs = sie_config sie_mcp + sie_placement sie_sdk sie_server relative_files = True diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7f713626a..a73b5b105 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -67,6 +67,7 @@ git switch -c upstream/main | [`packages/sie_sdk`](packages/sie_sdk/README.md) | Python client SDK and public request and response types | | `packages/sie_config` | Python configuration service | | [`packages/sie_mcp`](packages/sie_mcp/README.md) | Python MCP edge service, plugin, and agent skill | +| [`packages/sie_placement`](packages/sie_placement/README.md) | Standard-library Python schemas for placement planner inputs and plans; a workspace package that is not published | | [`packages/sie_audio_prep`](packages/sie_audio_prep/README.md) | Native audio preparation extension with Python bindings | | [`packages/sie_ts_sdk`](packages/sie_ts_sdk/README.md) | TypeScript client SDK | | [`packages/sie_gateway`](packages/sie_gateway/README.md) | Rust gateway for routing, queuing, API compatibility, and cluster state | diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md new file mode 100644 index 000000000..cf72c6183 --- /dev/null +++ b/packages/sie_placement/README.md @@ -0,0 +1,79 @@ +# sie-placement + +Versioned documents for a measured-only placement planner: the service level +each served identity must meet, the measurements a placement may be chosen +from, the prices those placements cost, the demand they serve, and the plan +that results. The package uses only the Python standard library. It is a +workspace package and is not published to PyPI. + +A served identity is a model id or a `model:profile` pair, the routing key a +request resolves to. A measurement cell is a machine profile together with the +GPU product the measurement actually ran on, such as the +`nvidia.com/gpu.product` node label value `NVIDIA-L4`. Recording the realized +product rather than the requested one keeps a measurement taken on different +silicon from standing in for the profile it was requested on. + +## Documents + +Every document is a JSON object with a `kind` and a `schemaVersion`. Readers +reject another kind, any version other than `1`, unknown fields, `null` +values, booleans where numbers are expected and non-finite numbers. Errors +name the offending field by its JSON path, for example +`$.lanes[1].envelope.floorMax: must not exceed maxContainers (2), got 3`. + +| Kind | Type | Contents | +| --- | --- | --- | +| `placement-service-levels` | `ServiceLevels` | Optional named `tiers` and the `identities` that declare or inherit service levels | +| `placement-receipt` | `Receipt` | One throughput and latency measurement of one identity on one cell, container shape, concurrency and request shape | +| `placement-boot-evidence` | `BootEvidence` | One measured boot, snapshot restore or resident load of one identity on one cell and container shape | +| `placement-price-list` | `PriceList` | Accelerator, core and memory rates per machine profile, and the region multiplier per placement | +| `placement-demand` | `Demand` | Hourly request counts per identity and operation over a UTC window | +| `placement-plan` | `Plan` | Lanes with their cell, container shape, concurrency, ceiling and envelope, and the lane and warmth of every identity | + +Each type parses with `from_dict(data)` and serializes with `to_dict()`. +Constructing a type in code applies the same rules as parsing it: + +```python +import json + +from sie_placement.schema import Plan + +with open("plan.json", encoding="utf-8") as handle: + plan = Plan.from_dict(json.load(handle)) +assert Plan.from_dict(plan.to_dict()) == plan +``` + +## Service levels + +An identity declares up to three numbers, or inherits them from the tier it +names. Fields it declares override the tier's. + +| Field | Meaning | +| --- | --- | +| `comeAliveP95S` | Seconds from a request for an idle identity to its first result, at the 95th percentile. `0` means never cold | +| `warmP99Ms` | Warm latency at the 99th percentile, at the receipt's request shape | +| `ttftP99Ms`, `itlP99Ms` | Time to first token and inter-token latency at the 99th percentile. A generation identity declares both instead of `warmP99Ms` | +| `queueWaitP99Ms` | Time a request may wait for capacity under load, at the 99th percentile | + +## Warmth + +A plan gives every identity one warmth: `pinned`, `cold-with-bound`, +`bridged`, `snapshot-cold`, `scheduled-warm` or `lazy-resident`. A `pinned` or +`lazy-resident` identity sits on a lane whose envelope keeps `floorMin` at 1 +or more; every other warmth sits on a lane whose `floorMin` is 0. Boot +evidence records `cold-with-bound` boots, `snapshot-cold` restores and +`lazy-resident` loads, with outcome `succeeded`, `failed` or `timed-out`. + +## Envelope + +A plan lane's envelope bounds what an online tuner may change without a new +plan. `floorMin` and `floorMax` are required, with `floorMax` at most the +lane's `maxContainers`. `bufferMin` with `bufferMax`, and +`scaledownWindowMinS` with `scaledownWindowMaxS`, are optional pairs for +platforms that can move a warm buffer or a scale-down window per lane. + +## Tests + +```bash +mise run test -- packages/sie_placement/tests +``` diff --git a/packages/sie_placement/pyproject.toml b/packages/sie_placement/pyproject.toml new file mode 100644 index 000000000..91b0c19d6 --- /dev/null +++ b/packages/sie_placement/pyproject.toml @@ -0,0 +1,19 @@ +[project] +name = "sie-placement" +version = "0.0.0" +description = "SIE placement planner - versioned input and plan documents for measured-only placement" +requires-python = ">=3.12,<3.13" +license = { text = "Apache-2.0" } +dependencies = [] + +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[tool.hatch.build.targets.wheel] +packages = ["src/sie_placement"] + +[dependency-groups] +dev = [ + "pytest>=9.0.3", +] diff --git a/packages/sie_placement/src/sie_placement/__init__.py b/packages/sie_placement/src/sie_placement/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/packages/sie_placement/src/sie_placement/schema.py b/packages/sie_placement/src/sie_placement/schema.py new file mode 100644 index 000000000..3171da492 --- /dev/null +++ b/packages/sie_placement/src/sie_placement/schema.py @@ -0,0 +1,1054 @@ +"""Versioned input and plan documents of the measured-only placement planner.""" + +from __future__ import annotations + +import math +import re +from collections.abc import Callable, Hashable, Iterable, Mapping +from dataclasses import dataclass +from datetime import UTC, datetime +from enum import StrEnum +from typing import Self + +SCHEMA_VERSION = 1 + +SERVICE_LEVELS_KIND = "placement-service-levels" +RECEIPT_KIND = "placement-receipt" +BOOT_EVIDENCE_KIND = "placement-boot-evidence" +PRICE_LIST_KIND = "placement-price-list" +DEMAND_KIND = "placement-demand" +PLAN_KIND = "placement-plan" + +ROOT_PATH = "$" + +_SHA256 = re.compile(r"[0-9a-f]{64}") +_CURRENCY = re.compile(r"[A-Z]{3}") +_HOUR = re.compile(r"(\d{4})-(\d{2})-(\d{2})T(\d{2}):00:00Z") +_HOUR_FORMAT = "%Y-%m-%dT%H:00:00Z" + + +class SchemaError(ValueError): + def __init__(self, path: str, message: str) -> None: + super().__init__(f"{path}: {message}" if path else message) + self.path = path + self.message = message + + +class Operation(StrEnum): + ENCODE = "encode" + SCORE = "score" + EXTRACT = "extract" + GENERATE = "generate" + + +class Warmth(StrEnum): + PINNED = "pinned" + COLD_WITH_BOUND = "cold-with-bound" + BRIDGED = "bridged" + SNAPSHOT_COLD = "snapshot-cold" + SCHEDULED_WARM = "scheduled-warm" + LAZY_RESIDENT = "lazy-resident" + + +BOOT_WARMTHS = frozenset({Warmth.COLD_WITH_BOUND, Warmth.SNAPSHOT_COLD, Warmth.LAZY_RESIDENT}) +WARM_FLOOR_WARMTHS = frozenset({Warmth.PINNED, Warmth.LAZY_RESIDENT}) + + +class BootOutcome(StrEnum): + SUCCEEDED = "succeeded" + FAILED = "failed" + TIMED_OUT = "timed-out" + + +def _join(path: str, key: str) -> str: + if not key: + return path + if not path: + return key + if key.startswith("["): + return f"{path}{key}" + return f"{path}.{key}" + + +def _within(path: str, error: SchemaError) -> SchemaError: + return SchemaError(_join(path, error.path), error.message) + + +def _describe(value: object) -> str: + if value is None: + return "null" + if isinstance(value, bool): + return f"boolean {str(value).lower()}" + if isinstance(value, int | float): + return f"number {value!r}" + if isinstance(value, str): + return f"string {value!r}" + if isinstance(value, Mapping): + return "an object" + if isinstance(value, list | tuple): + return "an array" + return type(value).__name__ + + +def _format_hour(value: datetime) -> str: + return value.strftime(_HOUR_FORMAT) + + +def _compact(fields: dict[str, object | None]) -> dict[str, object]: + return {key: value for key, value in fields.items() if value is not None} + + +class _Reader: + def __init__(self, data: object, path: str) -> None: + if not isinstance(data, Mapping): + raise SchemaError(path, f"expected an object, got {_describe(data)}") + self._data: dict[str, object] = {} + for key, value in data.items(): + if not isinstance(key, str): + raise SchemaError(path, f"expected string keys, got {_describe(key)}") + self._data[key] = value + self._path = path + self._read: set[str] = set() + + def _at(self, key: str) -> str: + return _join(self._path, key) + + def _required(self, key: str) -> object: + self._read.add(key) + if key not in self._data: + raise SchemaError(self._at(key), "is required") + value = self._data[key] + if value is None: + raise SchemaError(self._at(key), "must not be null") + return value + + def _optional(self, key: str) -> object | None: + self._read.add(key) + if key not in self._data: + return None + value = self._data[key] + if value is None: + raise SchemaError(self._at(key), "must be omitted rather than null") + return value + + def _string(self, key: str, value: object) -> str: + if not isinstance(value, str): + raise SchemaError(self._at(key), f"expected a string, got {_describe(value)}") + return value + + def _integer(self, key: str, value: object) -> int: + if isinstance(value, bool) or not isinstance(value, int): + raise SchemaError(self._at(key), f"expected an integer, got {_describe(value)}") + return value + + def _number(self, key: str, value: object) -> float: + if isinstance(value, bool) or not isinstance(value, int | float): + raise SchemaError(self._at(key), f"expected a number, got {_describe(value)}") + if not math.isfinite(value): + raise SchemaError(self._at(key), f"expected a finite number, got {value!r}") + return value + + def header(self, kind: str) -> None: + found = self._string("kind", self._required("kind")) + if found != kind: + raise SchemaError(self._at("kind"), f"expected {kind!r}, got {found!r}") + version = self._integer("schemaVersion", self._required("schemaVersion")) + if version != SCHEMA_VERSION: + raise SchemaError( + self._at("schemaVersion"), + f"unsupported version {version}; this reader supports version {SCHEMA_VERSION}", + ) + + def string(self, key: str) -> str: + return self._string(key, self._required(key)) + + def optional_string(self, key: str) -> str | None: + value = self._optional(key) + return None if value is None else self._string(key, value) + + def integer(self, key: str) -> int: + return self._integer(key, self._required(key)) + + def optional_integer(self, key: str) -> int | None: + value = self._optional(key) + return None if value is None else self._integer(key, value) + + def number(self, key: str) -> float: + return self._number(key, self._required(key)) + + def optional_number(self, key: str) -> float | None: + value = self._optional(key) + return None if value is None else self._number(key, value) + + def enum[E: StrEnum](self, key: str, kind: type[E]) -> E: + value = self.string(key) + try: + return kind(value) + except ValueError: + choices = ", ".join(repr(member.value) for member in kind) + raise SchemaError(self._at(key), f"expected one of {choices}, got {value!r}") from None + + def hour(self, key: str) -> datetime: + value = self.string(key) + match = _HOUR.fullmatch(value) + if match is None: + raise SchemaError(self._at(key), f"expected a UTC hour such as '2026-01-01T00:00:00Z', got {value!r}") + year, month, day, hour = (int(part) for part in match.groups()) + try: + return datetime(year, month, day, hour, tzinfo=UTC) + except ValueError as error: + raise SchemaError(self._at(key), f"is not a valid time: {error}") from None + + def child[T](self, key: str, parse: Callable[[object, str], T]) -> T: + return parse(self._required(key), self._at(key)) + + def optional_child[T](self, key: str, parse: Callable[[object, str], T]) -> T | None: + value = self._optional(key) + return None if value is None else parse(value, self._at(key)) + + def children[T](self, key: str, parse: Callable[[object, str], T], *, required: bool = True) -> tuple[T, ...]: + value = self._required(key) if required else self._optional(key) + if value is None: + return () + if not isinstance(value, list | tuple): + raise SchemaError(self._at(key), f"expected an array, got {_describe(value)}") + return tuple(parse(item, f"{self._at(key)}[{index}]") for index, item in enumerate(value)) + + def service_level(self) -> ServiceLevel: + return self.construct( + ServiceLevel, + come_alive_p95_s=self.optional_integer("comeAliveP95S"), + warm_p99_ms=self.optional_integer("warmP99Ms"), + ttft_p99_ms=self.optional_integer("ttftP99Ms"), + itl_p99_ms=self.optional_integer("itlP99Ms"), + queue_wait_p99_ms=self.optional_integer("queueWaitP99Ms"), + ) + + def construct[**P, T](self, factory: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: + try: + return factory(*args, **kwargs) + except SchemaError as error: + raise _within(self._path, error) from None + + def build[**P, T](self, factory: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: + unknown = sorted(set(self._data) - self._read) + if unknown: + raise SchemaError(self._at(unknown[0]), "is not a field of this object") + return self.construct(factory, *args, **kwargs) + + +def _require_identifier(name: str, value: str) -> None: + if not value or any(character.isspace() for character in value): + raise SchemaError(name, f"must be a non-empty string without whitespace, got {value!r}") + + +def _require_at_least(name: str, value: float | None, minimum: int) -> None: + if value is not None and value < minimum: + raise SchemaError(name, f"must be at least {minimum}, got {value!r}") + + +def _require_positive(name: str, value: float | None) -> None: + if value is not None and value <= 0: + raise SchemaError(name, f"must be greater than 0, got {value!r}") + + +def _require_ordered(lower_name: str, lower: float | None, upper_name: str, upper: float | None) -> None: + if lower is not None and upper is not None and lower > upper: + raise SchemaError(upper_name, f"must be at least {lower_name} ({lower!r}), got {upper!r}") + + +def _require_pair(first_name: str, first: object | None, second_name: str, second: object | None) -> None: + if (first is None) != (second is None): + missing = first_name if first is None else second_name + raise SchemaError(missing, f"{first_name} and {second_name} are declared together") + + +def _require_absent(name: str, value: object | None, reason: str) -> None: + if value is not None: + raise SchemaError(name, f"must be omitted {reason}") + + +def _require_unique(collection: str, field: str, keys: Iterable[Hashable]) -> None: + seen: set[Hashable] = set() + for index, key in enumerate(keys): + if key in seen: + raise SchemaError(f"{collection}[{index}].{field}", f"duplicates {key!r}") + seen.add(key) + + +def _require_hour(name: str, value: datetime) -> None: + offset = value.utcoffset() + if offset is None or offset.total_seconds() != 0 or value.minute or value.second or value.microsecond: + raise SchemaError(name, f"must be a UTC hour boundary, got {value.isoformat()}") + + +def _prefer(override: int | None, inherited: int | None) -> int | None: + return inherited if override is None else override + + +@dataclass(frozen=True, slots=True, kw_only=True) +class ServiceLevel: + come_alive_p95_s: int | None = None + warm_p99_ms: int | None = None + ttft_p99_ms: int | None = None + itl_p99_ms: int | None = None + queue_wait_p99_ms: int | None = None + + def __post_init__(self) -> None: + for name, value in self._fields(): + _require_at_least(name, value, 0) + if self.warm_p99_ms is not None and (self.ttft_p99_ms is not None or self.itl_p99_ms is not None): + raise SchemaError("warmP99Ms", "must be omitted when ttftP99Ms and itlP99Ms declare a generation identity") + + def _fields(self) -> tuple[tuple[str, int | None], ...]: + return ( + ("comeAliveP95S", self.come_alive_p95_s), + ("warmP99Ms", self.warm_p99_ms), + ("ttftP99Ms", self.ttft_p99_ms), + ("itlP99Ms", self.itl_p99_ms), + ("queueWaitP99Ms", self.queue_wait_p99_ms), + ) + + @property + def is_generation(self) -> bool: + return self.ttft_p99_ms is not None or self.itl_p99_ms is not None + + def require_complete(self) -> None: + _require_pair("ttftP99Ms", self.ttft_p99_ms, "itlP99Ms", self.itl_p99_ms) + + def overlay(self, override: ServiceLevel) -> ServiceLevel: + return ServiceLevel( + come_alive_p95_s=_prefer(override.come_alive_p95_s, self.come_alive_p95_s), + warm_p99_ms=_prefer(override.warm_p99_ms, self.warm_p99_ms), + ttft_p99_ms=_prefer(override.ttft_p99_ms, self.ttft_p99_ms), + itl_p99_ms=_prefer(override.itl_p99_ms, self.itl_p99_ms), + queue_wait_p99_ms=_prefer(override.queue_wait_p99_ms, self.queue_wait_p99_ms), + ) + + def to_dict(self) -> dict[str, object]: + return _compact(dict(self._fields())) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class ServiceLevelTier: + name: str + level: ServiceLevel + + def __post_init__(self) -> None: + _require_identifier("name", self.name) + self.level.require_complete() + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build(cls, name=reader.string("name"), level=reader.service_level()) + + def to_dict(self) -> dict[str, object]: + return {"name": self.name, **self.level.to_dict()} + + +@dataclass(frozen=True, slots=True, kw_only=True) +class IdentityServiceLevel: + identity: str + level: ServiceLevel + tier: str | None = None + + def __post_init__(self) -> None: + _require_identifier("identity", self.identity) + if self.tier is not None: + _require_identifier("tier", self.tier) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + identity=reader.string("identity"), + tier=reader.optional_string("tier"), + level=reader.service_level(), + ) + + def to_dict(self) -> dict[str, object]: + return _compact({"identity": self.identity, "tier": self.tier}) | self.level.to_dict() + + +@dataclass(frozen=True, slots=True, kw_only=True) +class ServiceLevels: + identities: tuple[IdentityServiceLevel, ...] + tiers: tuple[ServiceLevelTier, ...] = () + + def __post_init__(self) -> None: + _require_unique("tiers", "name", (tier.name for tier in self.tiers)) + _require_unique("identities", "identity", (entry.identity for entry in self.identities)) + tiers = {tier.name: tier.level for tier in self.tiers} + for index, entry in enumerate(self.identities): + if entry.tier is not None and entry.tier not in tiers: + raise SchemaError(f"identities[{index}].tier", f"names no declared tier: {entry.tier!r}") + try: + self._resolve(entry, tiers).require_complete() + except SchemaError as error: + raise _within(f"identities[{index}]", error) from None + + @staticmethod + def _resolve(entry: IdentityServiceLevel, tiers: Mapping[str, ServiceLevel]) -> ServiceLevel: + if entry.tier is None: + return entry.level + return tiers[entry.tier].overlay(entry.level) + + def effective(self, identity: str) -> ServiceLevel: + tiers = {tier.name: tier.level for tier in self.tiers} + for entry in self.identities: + if entry.identity == identity: + return self._resolve(entry, tiers) + raise KeyError(identity) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + reader.header(SERVICE_LEVELS_KIND) + return reader.build( + cls, + tiers=reader.children("tiers", ServiceLevelTier.from_dict, required=False), + identities=reader.children("identities", IdentityServiceLevel.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + document: dict[str, object] = {"kind": SERVICE_LEVELS_KIND, "schemaVersion": SCHEMA_VERSION} + if self.tiers: + document["tiers"] = [tier.to_dict() for tier in self.tiers] + document["identities"] = [entry.to_dict() for entry in self.identities] + return document + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Cell: + machine_profile: str + gpu_product: str + + def __post_init__(self) -> None: + _require_identifier("machineProfile", self.machine_profile) + _require_identifier("gpuProduct", self.gpu_product) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + machine_profile=reader.string("machineProfile"), + gpu_product=reader.string("gpuProduct"), + ) + + def to_dict(self) -> dict[str, object]: + return {"machineProfile": self.machine_profile, "gpuProduct": self.gpu_product} + + +@dataclass(frozen=True, slots=True, kw_only=True) +class ContainerShape: + gpu_count: int + cpu_cores: float + memory_mib: int + + def __post_init__(self) -> None: + _require_at_least("gpuCount", self.gpu_count, 1) + _require_positive("cpuCores", self.cpu_cores) + _require_at_least("memoryMiB", self.memory_mib, 1) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + gpu_count=reader.integer("gpuCount"), + cpu_cores=reader.number("cpuCores"), + memory_mib=reader.integer("memoryMiB"), + ) + + def to_dict(self) -> dict[str, object]: + return {"gpuCount": self.gpu_count, "cpuCores": self.cpu_cores, "memoryMiB": self.memory_mib} + + +@dataclass(frozen=True, slots=True, kw_only=True) +class RequestShape: + operation: Operation + items_per_request: int + input_tokens_per_item: int | None = None + output_tokens: int | None = None + + def __post_init__(self) -> None: + _require_at_least("itemsPerRequest", self.items_per_request, 1) + _require_at_least("inputTokensPerItem", self.input_tokens_per_item, 0) + if self.operation is Operation.GENERATE: + if self.output_tokens is None: + raise SchemaError("outputTokens", "is required for the generate operation") + _require_at_least("outputTokens", self.output_tokens, 1) + else: + _require_absent("outputTokens", self.output_tokens, "for an operation other than generate") + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + operation=reader.enum("operation", Operation), + items_per_request=reader.integer("itemsPerRequest"), + input_tokens_per_item=reader.optional_integer("inputTokensPerItem"), + output_tokens=reader.optional_integer("outputTokens"), + ) + + def to_dict(self) -> dict[str, object]: + return _compact( + { + "operation": self.operation.value, + "itemsPerRequest": self.items_per_request, + "inputTokensPerItem": self.input_tokens_per_item, + "outputTokens": self.output_tokens, + } + ) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Throughput: + requests_per_second: float + output_tokens_per_second: float | None = None + + def __post_init__(self) -> None: + _require_positive("requestsPerSecond", self.requests_per_second) + _require_positive("outputTokensPerSecond", self.output_tokens_per_second) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + requests_per_second=reader.number("requestsPerSecond"), + output_tokens_per_second=reader.optional_number("outputTokensPerSecond"), + ) + + def to_dict(self) -> dict[str, object]: + return _compact( + {"requestsPerSecond": self.requests_per_second, "outputTokensPerSecond": self.output_tokens_per_second} + ) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Percentiles: + p99_ms: float + p50_ms: float | None = None + p95_ms: float | None = None + + def __post_init__(self) -> None: + _require_at_least("p50Ms", self.p50_ms, 0) + _require_at_least("p95Ms", self.p95_ms, 0) + _require_at_least("p99Ms", self.p99_ms, 0) + _require_ordered("p50Ms", self.p50_ms, "p95Ms", self.p95_ms) + _require_ordered("p95Ms", self.p95_ms, "p99Ms", self.p99_ms) + _require_ordered("p50Ms", self.p50_ms, "p99Ms", self.p99_ms) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + p50_ms=reader.optional_number("p50Ms"), + p95_ms=reader.optional_number("p95Ms"), + p99_ms=reader.number("p99Ms"), + ) + + def to_dict(self) -> dict[str, object]: + return _compact({"p50Ms": self.p50_ms, "p95Ms": self.p95_ms, "p99Ms": self.p99_ms}) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class SourceRef: + uri: str + sha256: str | None = None + + def __post_init__(self) -> None: + _require_identifier("uri", self.uri) + if self.sha256 is not None and _SHA256.fullmatch(self.sha256) is None: + raise SchemaError("sha256", f"must be 64 lowercase hexadecimal characters, got {self.sha256!r}") + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build(cls, uri=reader.string("uri"), sha256=reader.optional_string("sha256")) + + def to_dict(self) -> dict[str, object]: + return _compact({"uri": self.uri, "sha256": self.sha256}) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Receipt: + identity: str + cell: Cell + container_shape: ContainerShape + concurrency: int + request_shape: RequestShape + throughput: Throughput + source: SourceRef + latency: Percentiles | None = None + ttft: Percentiles | None = None + itl: Percentiles | None = None + + def __post_init__(self) -> None: + _require_identifier("identity", self.identity) + _require_at_least("concurrency", self.concurrency, 1) + if self.request_shape.operation is Operation.GENERATE: + _require_absent("latency", self.latency, "for the generate operation, which reports ttft and itl") + if self.ttft is None: + raise SchemaError("ttft", "is required for the generate operation") + if self.itl is None: + raise SchemaError("itl", "is required for the generate operation") + else: + if self.latency is None: + raise SchemaError("latency", "is required for an operation other than generate") + _require_absent("ttft", self.ttft, "for an operation other than generate") + _require_absent("itl", self.itl, "for an operation other than generate") + _require_absent( + "throughput.outputTokensPerSecond", + self.throughput.output_tokens_per_second, + "for an operation other than generate", + ) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + reader.header(RECEIPT_KIND) + return reader.build( + cls, + identity=reader.string("identity"), + cell=reader.child("cell", Cell.from_dict), + container_shape=reader.child("containerShape", ContainerShape.from_dict), + concurrency=reader.integer("concurrency"), + request_shape=reader.child("requestShape", RequestShape.from_dict), + throughput=reader.child("throughput", Throughput.from_dict), + latency=reader.optional_child("latency", Percentiles.from_dict), + ttft=reader.optional_child("ttft", Percentiles.from_dict), + itl=reader.optional_child("itl", Percentiles.from_dict), + source=reader.child("source", SourceRef.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return _compact( + { + "kind": RECEIPT_KIND, + "schemaVersion": SCHEMA_VERSION, + "identity": self.identity, + "cell": self.cell.to_dict(), + "containerShape": self.container_shape.to_dict(), + "concurrency": self.concurrency, + "requestShape": self.request_shape.to_dict(), + "throughput": self.throughput.to_dict(), + "latency": None if self.latency is None else self.latency.to_dict(), + "ttft": None if self.ttft is None else self.ttft.to_dict(), + "itl": None if self.itl is None else self.itl.to_dict(), + "source": self.source.to_dict(), + } + ) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class BootEvidence: + identity: str + cell: Cell + container_shape: ContainerShape + warmth: Warmth + boot_seconds: float + outcome: BootOutcome + source: SourceRef + + def __post_init__(self) -> None: + _require_identifier("identity", self.identity) + if self.warmth not in BOOT_WARMTHS: + choices = ", ".join(repr(warmth.value) for warmth in Warmth if warmth in BOOT_WARMTHS) + raise SchemaError("warmth", f"boot evidence records one of {choices}, got {self.warmth.value!r}") + _require_at_least("bootSeconds", self.boot_seconds, 0) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + reader.header(BOOT_EVIDENCE_KIND) + return reader.build( + cls, + identity=reader.string("identity"), + cell=reader.child("cell", Cell.from_dict), + container_shape=reader.child("containerShape", ContainerShape.from_dict), + warmth=reader.enum("warmth", Warmth), + boot_seconds=reader.number("bootSeconds"), + outcome=reader.enum("outcome", BootOutcome), + source=reader.child("source", SourceRef.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return { + "kind": BOOT_EVIDENCE_KIND, + "schemaVersion": SCHEMA_VERSION, + "identity": self.identity, + "cell": self.cell.to_dict(), + "containerShape": self.container_shape.to_dict(), + "warmth": self.warmth.value, + "bootSeconds": self.boot_seconds, + "outcome": self.outcome.value, + "source": self.source.to_dict(), + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class MachineProfilePrice: + name: str + gpu_product: str + accelerator_per_hour: float + core_per_hour: float + memory_gib_per_hour: float + + def __post_init__(self) -> None: + _require_identifier("name", self.name) + _require_identifier("gpuProduct", self.gpu_product) + _require_at_least("acceleratorPerHour", self.accelerator_per_hour, 0) + _require_at_least("corePerHour", self.core_per_hour, 0) + _require_at_least("memoryGiBPerHour", self.memory_gib_per_hour, 0) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + name=reader.string("name"), + gpu_product=reader.string("gpuProduct"), + accelerator_per_hour=reader.number("acceleratorPerHour"), + core_per_hour=reader.number("corePerHour"), + memory_gib_per_hour=reader.number("memoryGiBPerHour"), + ) + + def to_dict(self) -> dict[str, object]: + return { + "name": self.name, + "gpuProduct": self.gpu_product, + "acceleratorPerHour": self.accelerator_per_hour, + "corePerHour": self.core_per_hour, + "memoryGiBPerHour": self.memory_gib_per_hour, + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Placement: + name: str + region_multiplier: float + + def __post_init__(self) -> None: + _require_identifier("name", self.name) + _require_positive("regionMultiplier", self.region_multiplier) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build(cls, name=reader.string("name"), region_multiplier=reader.number("regionMultiplier")) + + def to_dict(self) -> dict[str, object]: + return {"name": self.name, "regionMultiplier": self.region_multiplier} + + +@dataclass(frozen=True, slots=True, kw_only=True) +class PriceList: + currency: str + machine_profiles: tuple[MachineProfilePrice, ...] + placements: tuple[Placement, ...] + + def __post_init__(self) -> None: + if _CURRENCY.fullmatch(self.currency) is None: + raise SchemaError("currency", f"must be a three-letter upper-case currency code, got {self.currency!r}") + if not self.machine_profiles: + raise SchemaError("machineProfiles", "must list at least one machine profile") + if not self.placements: + raise SchemaError("placements", "must list at least one placement") + _require_unique("machineProfiles", "name", (profile.name for profile in self.machine_profiles)) + _require_unique("placements", "name", (placement.name for placement in self.placements)) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + reader.header(PRICE_LIST_KIND) + return reader.build( + cls, + currency=reader.string("currency"), + machine_profiles=reader.children("machineProfiles", MachineProfilePrice.from_dict), + placements=reader.children("placements", Placement.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return { + "kind": PRICE_LIST_KIND, + "schemaVersion": SCHEMA_VERSION, + "currency": self.currency, + "machineProfiles": [profile.to_dict() for profile in self.machine_profiles], + "placements": [placement.to_dict() for placement in self.placements], + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class DemandHour: + start: datetime + requests: int + units: int | None = None + + def __post_init__(self) -> None: + _require_hour("start", self.start) + _require_at_least("requests", self.requests, 0) + _require_at_least("units", self.units, 0) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + start=reader.hour("start"), + requests=reader.integer("requests"), + units=reader.optional_integer("units"), + ) + + def to_dict(self) -> dict[str, object]: + return _compact({"start": _format_hour(self.start), "requests": self.requests, "units": self.units}) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class DemandSeries: + identity: str + operation: Operation + hours: tuple[DemandHour, ...] + + def __post_init__(self) -> None: + _require_identifier("identity", self.identity) + for index in range(1, len(self.hours)): + if self.hours[index].start <= self.hours[index - 1].start: + raise SchemaError(f"hours[{index}].start", "must be later than the previous hour") + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + identity=reader.string("identity"), + operation=reader.enum("operation", Operation), + hours=reader.children("hours", DemandHour.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return { + "identity": self.identity, + "operation": self.operation.value, + "hours": [hour.to_dict() for hour in self.hours], + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Demand: + start: datetime + end: datetime + series: tuple[DemandSeries, ...] + + def __post_init__(self) -> None: + _require_hour("start", self.start) + _require_hour("end", self.end) + if self.end <= self.start: + raise SchemaError("end", "must be later than start") + _require_unique("series", "identity", (f"{entry.identity} {entry.operation.value}" for entry in self.series)) + for series_index, entry in enumerate(self.series): + for hour_index, hour in enumerate(entry.hours): + if not self.start <= hour.start < self.end: + raise SchemaError( + f"series[{series_index}].hours[{hour_index}].start", + f"must fall inside the window [{_format_hour(self.start)}, {_format_hour(self.end)})", + ) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + reader.header(DEMAND_KIND) + return reader.build( + cls, + start=reader.hour("start"), + end=reader.hour("end"), + series=reader.children("series", DemandSeries.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return { + "kind": DEMAND_KIND, + "schemaVersion": SCHEMA_VERSION, + "start": _format_hour(self.start), + "end": _format_hour(self.end), + "series": [entry.to_dict() for entry in self.series], + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Envelope: + floor_min: int + floor_max: int + buffer_min: int | None = None + buffer_max: int | None = None + scaledown_window_min_s: int | None = None + scaledown_window_max_s: int | None = None + + def __post_init__(self) -> None: + for name, value in self._fields(): + _require_at_least(name, value, 0) + _require_ordered("floorMin", self.floor_min, "floorMax", self.floor_max) + _require_pair("bufferMin", self.buffer_min, "bufferMax", self.buffer_max) + _require_ordered("bufferMin", self.buffer_min, "bufferMax", self.buffer_max) + _require_pair( + "scaledownWindowMinS", self.scaledown_window_min_s, "scaledownWindowMaxS", self.scaledown_window_max_s + ) + _require_ordered( + "scaledownWindowMinS", self.scaledown_window_min_s, "scaledownWindowMaxS", self.scaledown_window_max_s + ) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + floor_min=reader.integer("floorMin"), + floor_max=reader.integer("floorMax"), + buffer_min=reader.optional_integer("bufferMin"), + buffer_max=reader.optional_integer("bufferMax"), + scaledown_window_min_s=reader.optional_integer("scaledownWindowMinS"), + scaledown_window_max_s=reader.optional_integer("scaledownWindowMaxS"), + ) + + def _fields(self) -> tuple[tuple[str, int | None], ...]: + return ( + ("floorMin", self.floor_min), + ("floorMax", self.floor_max), + ("bufferMin", self.buffer_min), + ("bufferMax", self.buffer_max), + ("scaledownWindowMinS", self.scaledown_window_min_s), + ("scaledownWindowMaxS", self.scaledown_window_max_s), + ) + + def to_dict(self) -> dict[str, object]: + return _compact(dict(self._fields())) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class PlanLane: + name: str + pool: str + bundle: str + cell: Cell + container_shape: ContainerShape + concurrency: int + max_containers: int + envelope: Envelope + + def __post_init__(self) -> None: + _require_identifier("name", self.name) + _require_identifier("pool", self.pool) + _require_identifier("bundle", self.bundle) + _require_at_least("concurrency", self.concurrency, 1) + _require_at_least("maxContainers", self.max_containers, 1) + if self.envelope.floor_max > self.max_containers: + raise SchemaError( + "envelope.floorMax", + f"must not exceed maxContainers ({self.max_containers}), got {self.envelope.floor_max}", + ) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + name=reader.string("name"), + pool=reader.string("pool"), + bundle=reader.string("bundle"), + cell=reader.child("cell", Cell.from_dict), + container_shape=reader.child("containerShape", ContainerShape.from_dict), + concurrency=reader.integer("concurrency"), + max_containers=reader.integer("maxContainers"), + envelope=reader.child("envelope", Envelope.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return { + "name": self.name, + "pool": self.pool, + "bundle": self.bundle, + "cell": self.cell.to_dict(), + "containerShape": self.container_shape.to_dict(), + "concurrency": self.concurrency, + "maxContainers": self.max_containers, + "envelope": self.envelope.to_dict(), + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class PlanIdentity: + identity: str + lane: str + warmth: Warmth + + def __post_init__(self) -> None: + _require_identifier("identity", self.identity) + _require_identifier("lane", self.lane) + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + return reader.build( + cls, + identity=reader.string("identity"), + lane=reader.string("lane"), + warmth=reader.enum("warmth", Warmth), + ) + + def to_dict(self) -> dict[str, object]: + return {"identity": self.identity, "lane": self.lane, "warmth": self.warmth.value} + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Plan: + placement: str + lanes: tuple[PlanLane, ...] + identities: tuple[PlanIdentity, ...] + + def __post_init__(self) -> None: + _require_identifier("placement", self.placement) + _require_unique("lanes", "name", (lane.name for lane in self.lanes)) + _require_unique("identities", "identity", (entry.identity for entry in self.identities)) + lanes = {lane.name: lane for lane in self.lanes} + for index, entry in enumerate(self.identities): + lane = lanes.get(entry.lane) + if lane is None: + raise SchemaError(f"identities[{index}].lane", f"names no lane of this plan: {entry.lane!r}") + warm_floor = lane.envelope.floor_min >= 1 + if (entry.warmth in WARM_FLOOR_WARMTHS) != warm_floor: + expectation = "at least 1" if entry.warmth in WARM_FLOOR_WARMTHS else "0" + raise SchemaError( + f"identities[{index}].warmth", + f"{entry.warmth.value} needs lane {lane.name!r} to have floorMin {expectation}, " + f"got {lane.envelope.floor_min}", + ) + hosted = {entry.lane for entry in self.identities} + for index, lane in enumerate(self.lanes): + if lane.name not in hosted: + raise SchemaError(f"lanes[{index}].name", f"lane {lane.name!r} hosts no identity") + + @classmethod + def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: + reader = _Reader(data, path) + reader.header(PLAN_KIND) + return reader.build( + cls, + placement=reader.string("placement"), + lanes=reader.children("lanes", PlanLane.from_dict), + identities=reader.children("identities", PlanIdentity.from_dict), + ) + + def to_dict(self) -> dict[str, object]: + return { + "kind": PLAN_KIND, + "schemaVersion": SCHEMA_VERSION, + "placement": self.placement, + "lanes": [lane.to_dict() for lane in self.lanes], + "identities": [entry.to_dict() for entry in self.identities], + } diff --git a/packages/sie_placement/tests/fixtures/boot-evidence.json b/packages/sie_placement/tests/fixtures/boot-evidence.json new file mode 100644 index 000000000..be9e58814 --- /dev/null +++ b/packages/sie_placement/tests/fixtures/boot-evidence.json @@ -0,0 +1,21 @@ +{ + "kind": "placement-boot-evidence", + "schemaVersion": 1, + "identity": "Qwen/Qwen3-8B", + "cell": { + "machineProfile": "a100-80gb", + "gpuProduct": "NVIDIA-A100-SXM4-80GB" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 12, + "memoryMiB": 87040 + }, + "warmth": "cold-with-bound", + "bootSeconds": 187.4, + "outcome": "succeeded", + "source": { + "uri": "https://example.com/boots/qwen3-8b-a100-80gb-0001.json", + "sha256": "0a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f9" + } +} diff --git a/packages/sie_placement/tests/fixtures/demand.json b/packages/sie_placement/tests/fixtures/demand.json new file mode 100644 index 000000000..9facb2166 --- /dev/null +++ b/packages/sie_placement/tests/fixtures/demand.json @@ -0,0 +1,34 @@ +{ + "kind": "placement-demand", + "schemaVersion": 1, + "start": "2026-11-02T00:00:00Z", + "end": "2026-11-03T00:00:00Z", + "series": [ + { + "identity": "BAAI/bge-m3", + "operation": "encode", + "hours": [ + { + "start": "2026-11-02T09:00:00Z", + "requests": 1840, + "units": 14720 + }, + { + "start": "2026-11-02T10:00:00Z", + "requests": 2210, + "units": 17680 + } + ] + }, + { + "identity": "Qwen/Qwen3-8B", + "operation": "generate", + "hours": [ + { + "start": "2026-11-02T14:00:00Z", + "requests": 12 + } + ] + } + ] +} diff --git a/packages/sie_placement/tests/fixtures/plan.json b/packages/sie_placement/tests/fixtures/plan.json new file mode 100644 index 000000000..cc601e491 --- /dev/null +++ b/packages/sie_placement/tests/fixtures/plan.json @@ -0,0 +1,68 @@ +{ + "kind": "placement-plan", + "schemaVersion": 1, + "placement": "us-central1", + "lanes": [ + { + "name": "default-l4-default", + "pool": "default", + "bundle": "default", + "cell": { + "machineProfile": "l4", + "gpuProduct": "NVIDIA-L4" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 7.5, + "memoryMiB": 30720 + }, + "concurrency": 16, + "maxContainers": 6, + "envelope": { + "floorMin": 1, + "floorMax": 3, + "bufferMin": 0, + "bufferMax": 1, + "scaledownWindowMinS": 60, + "scaledownWindowMaxS": 900 + } + }, + { + "name": "default-a100-80gb-generation", + "pool": "default", + "bundle": "generation", + "cell": { + "machineProfile": "a100-80gb", + "gpuProduct": "NVIDIA-A100-SXM4-80GB" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 12, + "memoryMiB": 87040 + }, + "concurrency": 32, + "maxContainers": 2, + "envelope": { + "floorMin": 0, + "floorMax": 1 + } + } + ], + "identities": [ + { + "identity": "BAAI/bge-m3", + "lane": "default-l4-default", + "warmth": "pinned" + }, + { + "identity": "BAAI/bge-m3:sparse", + "lane": "default-l4-default", + "warmth": "lazy-resident" + }, + { + "identity": "Qwen/Qwen3-8B", + "lane": "default-a100-80gb-generation", + "warmth": "cold-with-bound" + } + ] +} diff --git a/packages/sie_placement/tests/fixtures/price-list.json b/packages/sie_placement/tests/fixtures/price-list.json new file mode 100644 index 000000000..328ef3d0f --- /dev/null +++ b/packages/sie_placement/tests/fixtures/price-list.json @@ -0,0 +1,31 @@ +{ + "kind": "placement-price-list", + "schemaVersion": 1, + "currency": "USD", + "machineProfiles": [ + { + "name": "l4", + "gpuProduct": "NVIDIA-L4", + "acceleratorPerHour": 0.56, + "corePerHour": 0.031, + "memoryGiBPerHour": 0.0042 + }, + { + "name": "a100-80gb", + "gpuProduct": "NVIDIA-A100-SXM4-80GB", + "acceleratorPerHour": 3.4, + "corePerHour": 0.031, + "memoryGiBPerHour": 0.0042 + } + ], + "placements": [ + { + "name": "us-central1", + "regionMultiplier": 1 + }, + { + "name": "europe-west4", + "regionMultiplier": 1.15 + } + ] +} diff --git a/packages/sie_placement/tests/fixtures/receipt-encode.json b/packages/sie_placement/tests/fixtures/receipt-encode.json new file mode 100644 index 000000000..d77d41895 --- /dev/null +++ b/packages/sie_placement/tests/fixtures/receipt-encode.json @@ -0,0 +1,32 @@ +{ + "kind": "placement-receipt", + "schemaVersion": 1, + "identity": "BAAI/bge-m3", + "cell": { + "machineProfile": "l4", + "gpuProduct": "NVIDIA-L4" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 7.5, + "memoryMiB": 30720 + }, + "concurrency": 16, + "requestShape": { + "operation": "encode", + "itemsPerRequest": 8, + "inputTokensPerItem": 256 + }, + "throughput": { + "requestsPerSecond": 42.5 + }, + "latency": { + "p50Ms": 110.2, + "p95Ms": 171.9, + "p99Ms": 214 + }, + "source": { + "uri": "benchmarks/bge-m3/l4/encode-8x256.json", + "sha256": "4f2d8c1e9b7a6053d2e1f0c9b8a7d6e5f4c3b2a1908f7e6d5c4b3a2918f7e6d5" + } +} diff --git a/packages/sie_placement/tests/fixtures/receipt-generate.json b/packages/sie_placement/tests/fixtures/receipt-generate.json new file mode 100644 index 000000000..9b39c905a --- /dev/null +++ b/packages/sie_placement/tests/fixtures/receipt-generate.json @@ -0,0 +1,37 @@ +{ + "kind": "placement-receipt", + "schemaVersion": 1, + "identity": "Qwen/Qwen3-8B", + "cell": { + "machineProfile": "a100-80gb", + "gpuProduct": "NVIDIA-A100-SXM4-80GB" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 12, + "memoryMiB": 87040 + }, + "concurrency": 32, + "requestShape": { + "operation": "generate", + "itemsPerRequest": 1, + "inputTokensPerItem": 1024, + "outputTokens": 256 + }, + "throughput": { + "requestsPerSecond": 3.1, + "outputTokensPerSecond": 793.6 + }, + "ttft": { + "p50Ms": 310, + "p99Ms": 1480 + }, + "itl": { + "p50Ms": 24.5, + "p95Ms": 31, + "p99Ms": 44.8 + }, + "source": { + "uri": "https://example.com/receipts/qwen3-8b-a100-80gb.json" + } +} diff --git a/packages/sie_placement/tests/fixtures/service-levels.json b/packages/sie_placement/tests/fixtures/service-levels.json new file mode 100644 index 000000000..49c6e59b4 --- /dev/null +++ b/packages/sie_placement/tests/fixtures/service-levels.json @@ -0,0 +1,40 @@ +{ + "kind": "placement-service-levels", + "schemaVersion": 1, + "tiers": [ + { + "name": "interactive-retrieval", + "comeAliveP95S": 0, + "warmP99Ms": 250, + "queueWaitP99Ms": 500 + }, + { + "name": "batch-generation", + "comeAliveP95S": 600, + "ttftP99Ms": 4000, + "itlP99Ms": 120, + "queueWaitP99Ms": 30000 + } + ], + "identities": [ + { + "identity": "BAAI/bge-m3", + "tier": "interactive-retrieval" + }, + { + "identity": "BAAI/bge-m3:sparse", + "tier": "interactive-retrieval", + "comeAliveP95S": 120 + }, + { + "identity": "Qwen/Qwen3-8B", + "tier": "batch-generation", + "itlP99Ms": 80 + }, + { + "identity": "Qwen/Qwen3-Reranker-0.6B", + "comeAliveP95S": 90, + "warmP99Ms": 400 + } + ] +} diff --git a/packages/sie_placement/tests/test_schema.py b/packages/sie_placement/tests/test_schema.py new file mode 100644 index 000000000..609455835 --- /dev/null +++ b/packages/sie_placement/tests/test_schema.py @@ -0,0 +1,528 @@ +from __future__ import annotations + +import copy +import json +import math +from collections.abc import Callable +from datetime import UTC, datetime, timedelta, timezone +from pathlib import Path + +import pytest +from sie_placement.schema import ( + BootEvidence, + Cell, + ContainerShape, + Demand, + DemandHour, + DemandSeries, + Envelope, + IdentityServiceLevel, + Operation, + Percentiles, + Plan, + PlanIdentity, + PlanLane, + PriceList, + Receipt, + SchemaError, + ServiceLevel, + ServiceLevels, + Warmth, +) + +FIXTURES = Path(__file__).parent / "fixtures" + +DOCUMENTS: list[tuple[str, Callable[..., object]]] = [ + ("service-levels.json", ServiceLevels.from_dict), + ("receipt-encode.json", Receipt.from_dict), + ("receipt-generate.json", Receipt.from_dict), + ("boot-evidence.json", BootEvidence.from_dict), + ("price-list.json", PriceList.from_dict), + ("demand.json", Demand.from_dict), + ("plan.json", Plan.from_dict), +] + + +def load(name: str) -> dict: + return json.loads((FIXTURES / name).read_text(encoding="utf-8")) + + +def schema_error(parse: Callable[[dict], object], data: dict) -> SchemaError: + with pytest.raises(SchemaError) as caught: + parse(data) + return caught.value + + +@pytest.mark.parametrize(("name", "parse"), DOCUMENTS) +def test_canonical_documents_round_trip(name, parse): + data = load(name) + document = parse(data) + assert document.to_dict() == data + assert json.dumps(document.to_dict()) == json.dumps(data) + assert parse(json.loads(json.dumps(document.to_dict()))) == document + + +@pytest.mark.parametrize(("name", "parse"), DOCUMENTS) +def test_documents_reject_another_kind(name, parse): + data = load(name) + data["kind"] = "placement-other" + error = schema_error(parse, data) + assert error.path == "$.kind" + assert "expected 'placement-" in error.message + + +@pytest.mark.parametrize(("name", "parse"), DOCUMENTS) +def test_documents_reject_unsupported_versions(name, parse): + data = load(name) + data["schemaVersion"] = 2 + error = schema_error(parse, data) + assert str(error) == "$.schemaVersion: unsupported version 2; this reader supports version 1" + + +@pytest.mark.parametrize(("name", "parse"), DOCUMENTS) +def test_documents_require_a_version(name, parse): + data = load(name) + del data["schemaVersion"] + assert str(schema_error(parse, data)) == "$.schemaVersion: is required" + + +@pytest.mark.parametrize(("name", "parse"), DOCUMENTS) +def test_documents_reject_unknown_top_level_fields(name, parse): + data = load(name) + data["notes"] = "free text" + assert str(schema_error(parse, data)) == "$.notes: is not a field of this object" + + +@pytest.mark.parametrize("value", [[], "receipt", 1, None]) +def test_documents_must_be_objects(value): + error = schema_error(Receipt.from_dict, value) + assert error.path == "$" + assert error.message.startswith("expected an object, got ") + + +def test_nested_unknown_fields_are_reported_at_their_path(): + data = load("receipt-encode.json") + data["cell"]["gpuType"] = "nvidia-l4" + assert str(schema_error(Receipt.from_dict, data)) == "$.cell.gpuType: is not a field of this object" + + +def test_missing_nested_fields_are_reported_at_their_path(): + data = load("plan.json") + del data["lanes"][1]["containerShape"]["memoryMiB"] + assert str(schema_error(Plan.from_dict, data)) == "$.lanes[1].containerShape.memoryMiB: is required" + + +def test_null_is_never_a_value(): + data = load("receipt-encode.json") + data["latency"]["p50Ms"] = None + assert str(schema_error(Receipt.from_dict, data)) == "$.latency.p50Ms: must be omitted rather than null" + data = load("receipt-encode.json") + data["concurrency"] = None + assert str(schema_error(Receipt.from_dict, data)) == "$.concurrency: must not be null" + + +@pytest.mark.parametrize(("value", "described"), [(True, "boolean true"), (16.0, "number 16.0"), ("16", "string '16'")]) +def test_integers_are_strict(value, described): + data = load("receipt-encode.json") + data["concurrency"] = value + assert str(schema_error(Receipt.from_dict, data)) == f"$.concurrency: expected an integer, got {described}" + + +@pytest.mark.parametrize("value", [math.nan, math.inf]) +def test_numbers_must_be_finite(value): + data = load("receipt-encode.json") + data["throughput"]["requestsPerSecond"] = value + error = schema_error(Receipt.from_dict, data) + assert error.path == "$.throughput.requestsPerSecond" + assert error.message.startswith("expected a finite number") + + +def test_numbers_reject_booleans(): + data = load("price-list.json") + data["placements"][0]["regionMultiplier"] = False + assert ( + str(schema_error(PriceList.from_dict, data)) + == "$.placements[0].regionMultiplier: expected a number, got boolean false" + ) + + +def test_enumerations_list_their_choices(): + data = load("plan.json") + data["identities"][0]["warmth"] = "warm" + error = schema_error(Plan.from_dict, data) + assert error.path == "$.identities[0].warmth" + assert error.message == ( + "expected one of 'pinned', 'cold-with-bound', 'bridged', 'snapshot-cold', 'scheduled-warm', " + "'lazy-resident', got 'warm'" + ) + + +def test_arrays_are_required_where_declared(): + data = load("plan.json") + data["lanes"] = {"default-l4-default": {}} + assert str(schema_error(Plan.from_dict, data)) == "$.lanes: expected an array, got an object" + + +@pytest.mark.parametrize("value", ["", "two words", "trailing\n"]) +def test_identifiers_reject_empty_and_whitespace(value): + data = load("receipt-encode.json") + data["identity"] = value + error = schema_error(Receipt.from_dict, data) + assert error.path == "$.identity" + assert error.message.startswith("must be a non-empty string without whitespace") + + +def test_service_levels_resolve_tier_defaults_and_overrides(): + levels = ServiceLevels.from_dict(load("service-levels.json")) + assert levels.effective("BAAI/bge-m3") == ServiceLevel(come_alive_p95_s=0, warm_p99_ms=250, queue_wait_p99_ms=500) + assert levels.effective("BAAI/bge-m3:sparse").come_alive_p95_s == 120 + generation = levels.effective("Qwen/Qwen3-8B") + assert generation.is_generation + assert (generation.ttft_p99_ms, generation.itl_p99_ms) == (4000, 80) + assert levels.effective("Qwen/Qwen3-Reranker-0.6B") == ServiceLevel(come_alive_p95_s=90, warm_p99_ms=400) + with pytest.raises(KeyError): + levels.effective("unknown/model") + + +def test_service_levels_omit_an_empty_tier_list(): + data = load("service-levels.json") + data["tiers"] = [] + data["identities"] = [{"identity": "BAAI/bge-m3", "warmP99Ms": 200}] + assert ServiceLevels.from_dict(data).to_dict() == { + "kind": "placement-service-levels", + "schemaVersion": 1, + "identities": [{"identity": "BAAI/bge-m3", "warmP99Ms": 200}], + } + + +def test_generation_identities_do_not_declare_a_warm_latency(): + data = load("service-levels.json") + data["identities"][3] = {"identity": "Qwen/Qwen3-8B-Chat", "warmP99Ms": 300, "ttftP99Ms": 900, "itlP99Ms": 60} + error = schema_error(ServiceLevels.from_dict, data) + assert error.path == "$.identities[3].warmP99Ms" + assert "generation identity" in error.message + + +def test_a_generation_override_cannot_join_a_warm_latency_tier(): + data = load("service-levels.json") + data["identities"][0]["ttftP99Ms"] = 900 + data["identities"][0]["itlP99Ms"] = 60 + error = schema_error(ServiceLevels.from_dict, data) + assert error.path == "$.identities[0].warmP99Ms" + + +def test_generation_latencies_are_declared_together(): + data = load("service-levels.json") + data["identities"][3] = {"identity": "Qwen/Qwen3-8B-Chat", "ttftP99Ms": 900} + assert ( + str(schema_error(ServiceLevels.from_dict, data)) + == "$.identities[3].itlP99Ms: ttftP99Ms and itlP99Ms are declared together" + ) + data = load("service-levels.json") + del data["tiers"][1]["itlP99Ms"] + assert ( + str(schema_error(ServiceLevels.from_dict, data)) + == "$.tiers[1].itlP99Ms: ttftP99Ms and itlP99Ms are declared together" + ) + + +def test_service_level_tiers_must_exist_and_names_must_be_unique(): + data = load("service-levels.json") + data["identities"][0]["tier"] = "realtime" + assert ( + str(schema_error(ServiceLevels.from_dict, data)) == "$.identities[0].tier: names no declared tier: 'realtime'" + ) + data = load("service-levels.json") + data["identities"][2]["identity"] = "BAAI/bge-m3" + assert str(schema_error(ServiceLevels.from_dict, data)) == "$.identities[2].identity: duplicates 'BAAI/bge-m3'" + data = load("service-levels.json") + data["tiers"][1]["name"] = "interactive-retrieval" + assert str(schema_error(ServiceLevels.from_dict, data)) == "$.tiers[1].name: duplicates 'interactive-retrieval'" + + +def test_service_level_bounds_are_non_negative(): + data = load("service-levels.json") + data["identities"][3]["comeAliveP95S"] = -1 + assert ( + str(schema_error(ServiceLevels.from_dict, data)) == "$.identities[3].comeAliveP95S: must be at least 0, got -1" + ) + + +def test_generate_receipts_report_ttft_and_itl(): + data = load("receipt-generate.json") + data["latency"] = data.pop("ttft") + error = schema_error(Receipt.from_dict, data) + assert error.path == "$.latency" + assert error.message.startswith("must be omitted for the generate operation") + data = load("receipt-generate.json") + del data["itl"] + assert str(schema_error(Receipt.from_dict, data)) == "$.itl: is required for the generate operation" + + +def test_other_receipts_report_one_latency(): + data = load("receipt-encode.json") + data["ttft"] = data.pop("latency") + assert str(schema_error(Receipt.from_dict, data)) == "$.latency: is required for an operation other than generate" + data = load("receipt-encode.json") + data["throughput"]["outputTokensPerSecond"] = 10 + error = schema_error(Receipt.from_dict, data) + assert error.path == "$.throughput.outputTokensPerSecond" + + +def test_request_shapes_tie_output_tokens_to_generation(): + data = load("receipt-generate.json") + del data["requestShape"]["outputTokens"] + assert ( + str(schema_error(Receipt.from_dict, data)) + == "$.requestShape.outputTokens: is required for the generate operation" + ) + data = load("receipt-encode.json") + data["requestShape"]["outputTokens"] = 32 + error = schema_error(Receipt.from_dict, data) + assert error.path == "$.requestShape.outputTokens" + assert error.message.startswith("must be omitted") + + +def test_receipt_shapes_and_rates_are_bounded(): + data = load("receipt-encode.json") + data["containerShape"]["gpuCount"] = 0 + assert str(schema_error(Receipt.from_dict, data)) == "$.containerShape.gpuCount: must be at least 1, got 0" + data = load("receipt-encode.json") + data["throughput"]["requestsPerSecond"] = 0 + assert str(schema_error(Receipt.from_dict, data)) == "$.throughput.requestsPerSecond: must be greater than 0, got 0" + data = load("receipt-encode.json") + data["concurrency"] = 0 + assert str(schema_error(Receipt.from_dict, data)) == "$.concurrency: must be at least 1, got 0" + + +def test_percentiles_are_ordered(): + data = load("receipt-encode.json") + data["latency"]["p95Ms"] = 300 + assert str(schema_error(Receipt.from_dict, data)) == "$.latency.p99Ms: must be at least p95Ms (300), got 214" + data = load("receipt-generate.json") + data["ttft"]["p50Ms"] = 2000 + assert str(schema_error(Receipt.from_dict, data)) == "$.ttft.p99Ms: must be at least p50Ms (2000), got 1480" + + +def test_source_digests_are_sha256_hex(): + data = load("receipt-encode.json") + data["source"]["sha256"] = data["source"]["sha256"].upper() + error = schema_error(Receipt.from_dict, data) + assert error.path == "$.source.sha256" + + +def test_boot_evidence_records_only_booting_warmths(): + data = load("boot-evidence.json") + data["warmth"] = "pinned" + assert str(schema_error(BootEvidence.from_dict, data)) == ( + "$.warmth: boot evidence records one of 'cold-with-bound', 'snapshot-cold', 'lazy-resident', got 'pinned'" + ) + + +def test_boot_evidence_keeps_failed_outcomes(): + data = load("boot-evidence.json") + data["outcome"] = "timed-out" + data["bootSeconds"] = 900 + evidence = BootEvidence.from_dict(data) + assert evidence.outcome.value == "timed-out" + assert evidence.to_dict() == data + data["bootSeconds"] = -0.5 + assert str(schema_error(BootEvidence.from_dict, data)) == "$.bootSeconds: must be at least 0, got -0.5" + + +def test_price_lists_validate_currency_rates_and_names(): + data = load("price-list.json") + data["currency"] = "usd" + assert schema_error(PriceList.from_dict, data).path == "$.currency" + data = load("price-list.json") + data["machineProfiles"][1]["name"] = "l4" + assert str(schema_error(PriceList.from_dict, data)) == "$.machineProfiles[1].name: duplicates 'l4'" + data = load("price-list.json") + data["machineProfiles"][0]["corePerHour"] = -0.01 + assert ( + str(schema_error(PriceList.from_dict, data)) + == "$.machineProfiles[0].corePerHour: must be at least 0, got -0.01" + ) + data = load("price-list.json") + data["placements"][1]["regionMultiplier"] = 0 + assert ( + str(schema_error(PriceList.from_dict, data)) + == "$.placements[1].regionMultiplier: must be greater than 0, got 0" + ) + data = load("price-list.json") + data["placements"] = [] + assert str(schema_error(PriceList.from_dict, data)) == "$.placements: must list at least one placement" + + +@pytest.mark.parametrize( + "value", ["2026-11-02T09:30:00Z", "2026-11-02T09:00:00+00:00", "2026-11-02 09:00:00Z", "2026-02-30T09:00:00Z"] +) +def test_demand_hours_are_utc_hour_boundaries(value): + data = load("demand.json") + data["series"][0]["hours"][0]["start"] = value + error = schema_error(Demand.from_dict, data) + assert error.path == "$.series[0].hours[0].start" + + +def test_demand_hours_are_ordered_and_inside_the_window(): + data = load("demand.json") + data["series"][0]["hours"].reverse() + assert ( + str(schema_error(Demand.from_dict, data)) == "$.series[0].hours[1].start: must be later than the previous hour" + ) + data = load("demand.json") + data["series"][1]["hours"][0]["start"] = "2026-11-03T00:00:00Z" + assert str(schema_error(Demand.from_dict, data)) == ( + "$.series[1].hours[0].start: must fall inside the window [2026-11-02T00:00:00Z, 2026-11-03T00:00:00Z)" + ) + data = load("demand.json") + data["end"] = data["start"] + assert str(schema_error(Demand.from_dict, data)) == "$.end: must be later than start" + + +def test_demand_series_are_unique_per_identity_and_operation(): + data = load("demand.json") + data["series"].append(copy.deepcopy(data["series"][0])) + assert str(schema_error(Demand.from_dict, data)) == "$.series[2].identity: duplicates 'BAAI/bge-m3 encode'" + data["series"][2]["operation"] = "score" + assert len(Demand.from_dict(data).series) == 3 + + +def test_demand_hours_built_in_code_must_be_utc_boundaries(): + with pytest.raises(SchemaError, match="UTC hour boundary"): + DemandHour(start=datetime(2026, 11, 2, 9, 30, tzinfo=UTC), requests=1) + with pytest.raises(SchemaError, match="UTC hour boundary"): + DemandHour(start=datetime(2026, 11, 2, 9, tzinfo=timezone(timedelta(hours=1))), requests=1) + with pytest.raises(SchemaError, match="UTC hour boundary"): + DemandHour(start=datetime(2026, 11, 2, 9), requests=1) # noqa: DTZ001 + hour = DemandHour(start=datetime(2026, 11, 2, 9, tzinfo=UTC), requests=4) + series = DemandSeries(identity="BAAI/bge-m3", operation=Operation.ENCODE, hours=(hour,)) + demand = Demand(start=hour.start, end=hour.start + timedelta(hours=1), series=(series,)) + assert Demand.from_dict(demand.to_dict()) == demand + + +@pytest.mark.parametrize( + ("envelope", "path", "message"), + [ + ({"floorMin": 2, "floorMax": 1}, "floorMax", "must be at least floorMin (2), got 1"), + ({"floorMin": -1, "floorMax": 1}, "floorMin", "must be at least 0, got -1"), + ({"floorMin": 0, "floorMax": 1, "bufferMin": 0}, "bufferMax", "bufferMin and bufferMax are declared together"), + ( + {"floorMin": 0, "floorMax": 1, "bufferMin": 2, "bufferMax": 1}, + "bufferMax", + "must be at least bufferMin (2), got 1", + ), + ( + {"floorMin": 0, "floorMax": 1, "scaledownWindowMaxS": 60}, + "scaledownWindowMinS", + "scaledownWindowMinS and scaledownWindowMaxS are declared together", + ), + ( + {"floorMin": 0, "floorMax": 1, "scaledownWindowMinS": 600, "scaledownWindowMaxS": 60}, + "scaledownWindowMaxS", + "must be at least scaledownWindowMinS (600), got 60", + ), + ], +) +def test_envelopes_are_ordered_ranges(envelope, path, message): + error = schema_error(Envelope.from_dict, envelope) + assert error.path == f"$.{path}" + assert error.message == message + + +def test_envelope_ranges_are_optional_for_floor_only_appliers(): + envelope = Envelope.from_dict({"floorMin": 0, "floorMax": 2}) + assert envelope.buffer_min is None + assert envelope.scaledown_window_max_s is None + assert envelope.to_dict() == {"floorMin": 0, "floorMax": 2} + + +def test_plan_envelopes_stay_under_the_lane_ceiling(): + data = load("plan.json") + data["lanes"][0]["maxContainers"] = 2 + assert str(schema_error(Plan.from_dict, data)) == ( + "$.lanes[0].envelope.floorMax: must not exceed maxContainers (2), got 3" + ) + + +def test_plan_identities_name_a_lane_of_the_plan(): + data = load("plan.json") + data["identities"][2]["lane"] = "default-h100-generation" + assert str(schema_error(Plan.from_dict, data)) == ( + "$.identities[2].lane: names no lane of this plan: 'default-h100-generation'" + ) + + +def test_plan_lanes_host_an_identity(): + data = load("plan.json") + del data["identities"][2] + assert str(schema_error(Plan.from_dict, data)) == ( + "$.lanes[1].name: lane 'default-a100-80gb-generation' hosts no identity" + ) + + +@pytest.mark.parametrize( + ("index", "warmth", "expectation"), + [ + (0, "cold-with-bound", "floorMin 0, got 1"), + (1, "snapshot-cold", "floorMin 0, got 1"), + (2, "pinned", "floorMin at least 1, got 0"), + (2, "lazy-resident", "floorMin at least 1, got 0"), + ], +) +def test_plan_warmth_matches_the_lane_floor(index, warmth, expectation): + data = load("plan.json") + data["identities"][index]["warmth"] = warmth + error = schema_error(Plan.from_dict, data) + assert error.path == f"$.identities[{index}].warmth" + assert error.message.startswith(f"{warmth} needs lane ") + assert error.message.endswith(expectation) + + +@pytest.mark.parametrize("warmth", ["bridged", "scheduled-warm", "snapshot-cold"]) +def test_plan_accepts_every_zero_floor_warmth(warmth): + data = load("plan.json") + data["identities"][2]["warmth"] = warmth + assert Plan.from_dict(data).identities[2].warmth.value == warmth + + +def test_plan_lane_names_and_identities_are_unique(): + data = load("plan.json") + data["lanes"][1]["name"] = "default-l4-default" + assert str(schema_error(Plan.from_dict, data)) == "$.lanes[1].name: duplicates 'default-l4-default'" + data = load("plan.json") + data["identities"][1]["identity"] = "BAAI/bge-m3" + assert str(schema_error(Plan.from_dict, data)) == "$.identities[1].identity: duplicates 'BAAI/bge-m3'" + + +def test_documents_built_in_code_report_relative_paths(): + with pytest.raises(SchemaError) as caught: + Envelope(floor_min=3, floor_max=1) + assert caught.value.path == "floorMax" + with pytest.raises(SchemaError) as caught: + IdentityServiceLevel(identity="model", level=ServiceLevel(ttft_p99_ms=1, warm_p99_ms=1)) + assert caught.value.path == "warmP99Ms" + with pytest.raises(SchemaError) as caught: + Percentiles(p50_ms=5, p99_ms=1) + assert str(caught.value) == "p99Ms: must be at least p50Ms (5), got 1" + + +def test_plans_built_in_code_round_trip(): + cell = Cell(machine_profile="l4", gpu_product="NVIDIA-L4") + shape = ContainerShape(gpu_count=1, cpu_cores=4, memory_mib=16384) + lane = PlanLane( + name="default-l4-default", + pool="default", + bundle="default", + cell=cell, + container_shape=shape, + concurrency=8, + max_containers=4, + envelope=Envelope(floor_min=1, floor_max=2), + ) + plan = Plan( + placement="us-central1", + lanes=(lane,), + identities=(PlanIdentity(identity="BAAI/bge-m3", lane=lane.name, warmth=Warmth.PINNED),), + ) + assert Plan.from_dict(json.loads(json.dumps(plan.to_dict()))) == plan diff --git a/pyproject.toml b/pyproject.toml index 0d4371428..7dab46203 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,7 @@ torchvision = [ sie-audio-prep = { workspace = true } sie-config = { workspace = true } sie-mcp = { workspace = true } +sie-placement = { workspace = true } sie-sdk = { workspace = true } sie-server = { workspace = true } @@ -63,6 +64,7 @@ members = [ "packages/sie_audio_prep", "packages/sie_config", "packages/sie_mcp", + "packages/sie_placement", "packages/sie_sdk", "packages/sie_server", "integrations/sie_chroma", @@ -83,6 +85,7 @@ testpaths = [ "packages/sie_audio_prep/tests", "packages/sie_config/tests", "packages/sie_mcp/tests", + "packages/sie_placement/tests", "packages/sie_sdk/tests", "packages/sie_server/tests", ] diff --git a/tools/mise_tasks/lint.bash b/tools/mise_tasks/lint.bash index fa8531aac..f3d5fb421 100755 --- a/tools/mise_tasks/lint.bash +++ b/tools/mise_tasks/lint.bash @@ -8,6 +8,7 @@ PUBLIC_PATHS=( packages/sie_audio_prep packages/sie_config packages/sie_mcp + packages/sie_placement packages/sie_sdk packages/sie_server integrations diff --git a/tools/mise_tasks/typecheck.bash b/tools/mise_tasks/typecheck.bash index ec09e0fa4..6f397d0ab 100755 --- a/tools/mise_tasks/typecheck.bash +++ b/tools/mise_tasks/typecheck.bash @@ -10,6 +10,7 @@ else PATHS_TO_CHECK=( packages/sie_config packages/sie_mcp + packages/sie_placement packages/sie_sdk packages/sie_server ) diff --git a/uv.lock b/uv.lock index 78041689d..d07c8964a 100644 --- a/uv.lock +++ b/uv.lock @@ -21,6 +21,7 @@ members = [ "sie-langchain", "sie-llamaindex", "sie-mcp", + "sie-placement", "sie-qdrant", "sie-sdk", "sie-server", @@ -4760,6 +4761,21 @@ dev = [ { name = "pytest-asyncio", specifier = ">=1.0" }, ] +[[package]] +name = "sie-placement" +version = "0.0.0" +source = { editable = "packages/sie_placement" } + +[package.dev-dependencies] +dev = [ + { name = "pytest" }, +] + +[package.metadata] + +[package.metadata.requires-dev] +dev = [{ name = "pytest", specifier = ">=9.0.3" }] + [[package]] name = "sie-qdrant" version = "0.11.0" From ac80e65a24fead645b687a225881de6e6b021e13 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Wed, 4 Nov 2026 22:09:41 +0100 Subject: [PATCH 02/22] fix(placement): keep number and hour parsing inside the schema contract An integer beyond the float range in a number field raised OverflowError from math.isfinite instead of a path-qualified SchemaError. Report it as a non-finite number at its JSON path. UTC hours matched any Unicode digit, so an hour written in non-ASCII digits parsed and then serialized differently. Match ASCII digits only, and format the hour explicitly so a year below 1000 keeps four digits on every platform's strftime. --- .../sie_placement/src/sie_placement/schema.py | 11 ++++++---- packages/sie_placement/tests/test_schema.py | 21 +++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/sie_placement/src/sie_placement/schema.py b/packages/sie_placement/src/sie_placement/schema.py index 3171da492..533b0b3bd 100644 --- a/packages/sie_placement/src/sie_placement/schema.py +++ b/packages/sie_placement/src/sie_placement/schema.py @@ -23,8 +23,7 @@ _SHA256 = re.compile(r"[0-9a-f]{64}") _CURRENCY = re.compile(r"[A-Z]{3}") -_HOUR = re.compile(r"(\d{4})-(\d{2})-(\d{2})T(\d{2}):00:00Z") -_HOUR_FORMAT = "%Y-%m-%dT%H:00:00Z" +_HOUR = re.compile(r"(\d{4})-(\d{2})-(\d{2})T(\d{2}):00:00Z", re.ASCII) class SchemaError(ValueError): @@ -91,7 +90,7 @@ def _describe(value: object) -> str: def _format_hour(value: datetime) -> str: - return value.strftime(_HOUR_FORMAT) + return f"{value.year:04d}-{value.month:02d}-{value.day:02d}T{value.hour:02d}:00:00Z" def _compact(fields: dict[str, object | None]) -> dict[str, object]: @@ -144,7 +143,11 @@ def _integer(self, key: str, value: object) -> int: def _number(self, key: str, value: object) -> float: if isinstance(value, bool) or not isinstance(value, int | float): raise SchemaError(self._at(key), f"expected a number, got {_describe(value)}") - if not math.isfinite(value): + try: + finite = math.isfinite(value) + except OverflowError: + raise SchemaError(self._at(key), "expected a finite number, got an integer beyond the float range") from None + if not finite: raise SchemaError(self._at(key), f"expected a finite number, got {value!r}") return value diff --git a/packages/sie_placement/tests/test_schema.py b/packages/sie_placement/tests/test_schema.py index 609455835..39833b128 100644 --- a/packages/sie_placement/tests/test_schema.py +++ b/packages/sie_placement/tests/test_schema.py @@ -128,7 +128,11 @@ def test_integers_are_strict(value, described): assert str(schema_error(Receipt.from_dict, data)) == f"$.concurrency: expected an integer, got {described}" -@pytest.mark.parametrize("value", [math.nan, math.inf]) +@pytest.mark.parametrize( + "value", + [math.nan, math.inf, json.loads("1" + "0" * 400), -(10**400)], + ids=["nan", "infinity", "integer-above-float-range", "integer-below-float-range"], +) def test_numbers_must_be_finite(value): data = load("receipt-encode.json") data["throughput"]["requestsPerSecond"] = value @@ -355,7 +359,14 @@ def test_price_lists_validate_currency_rates_and_names(): @pytest.mark.parametrize( - "value", ["2026-11-02T09:30:00Z", "2026-11-02T09:00:00+00:00", "2026-11-02 09:00:00Z", "2026-02-30T09:00:00Z"] + "value", + [ + "2026-11-02T09:30:00Z", + "2026-11-02T09:00:00+00:00", + "2026-11-02 09:00:00Z", + "2026-02-30T09:00:00Z", + "٢٠٢٦-11-02T09:00:00Z", + ], ) def test_demand_hours_are_utc_hour_boundaries(value): data = load("demand.json") @@ -401,6 +412,12 @@ def test_demand_hours_built_in_code_must_be_utc_boundaries(): assert Demand.from_dict(demand.to_dict()) == demand +def test_demand_hours_serialize_four_digit_years(): + hour = DemandHour(start=datetime(999, 1, 1, 7, tzinfo=UTC), requests=1) + assert hour.to_dict()["start"] == "0999-01-01T07:00:00Z" + assert DemandHour.from_dict(hour.to_dict()) == hour + + @pytest.mark.parametrize( ("envelope", "path", "message"), [ From a3f9193a1608d7c36525d49f0f9a7c57ace450ff Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Wed, 4 Nov 2026 22:10:43 +0100 Subject: [PATCH 03/22] fix(placement): name the plan lane's queue pool queuePool The plan lane's `pool` field was ambiguous: in the sie-cluster chart a pool is a worker group (`workers.pools.`), while a lane is named by its queuePool, machineProfile and bundle tokens. Rename the wire key to `queuePool`, the chart's lane token, and document the three tokens and how they differ from the worker group key and an SDK logical pool. --- packages/sie_placement/README.md | 11 +++++++++++ packages/sie_placement/src/sie_placement/schema.py | 8 ++++---- packages/sie_placement/tests/fixtures/plan.json | 4 ++-- packages/sie_placement/tests/test_schema.py | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index cf72c6183..8915201cd 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -64,6 +64,17 @@ or more; every other warmth sits on a lane whose `floorMin` is 0. Boot evidence records `cold-with-bound` boots, `snapshot-cold` restores and `lazy-resident` loads, with outcome `succeeded`, `failed` or `timed-out`. +## Lanes + +A plan lane carries the three tokens that name a lane of the `sie-cluster` +Helm chart: `queuePool`, the `machineProfile` of its `cell`, and `bundle`. +`queuePool` is the physical queue pool the lane's workers consume: +`workers.common.queuePool` (`default`), or a worker group's `queuePool` +override. It is neither the `workers.pools` map key, which names a worker +group, nor an SDK logical pool. `bundle` is a key of that worker group's `bundles` map. `name` +is the lane's handle inside the plan, and each identity refers to its lane by +that name. + ## Envelope A plan lane's envelope bounds what an online tuner may change without a new diff --git a/packages/sie_placement/src/sie_placement/schema.py b/packages/sie_placement/src/sie_placement/schema.py index 533b0b3bd..2acffb7c5 100644 --- a/packages/sie_placement/src/sie_placement/schema.py +++ b/packages/sie_placement/src/sie_placement/schema.py @@ -936,7 +936,7 @@ def to_dict(self) -> dict[str, object]: @dataclass(frozen=True, slots=True, kw_only=True) class PlanLane: name: str - pool: str + queue_pool: str bundle: str cell: Cell container_shape: ContainerShape @@ -946,7 +946,7 @@ class PlanLane: def __post_init__(self) -> None: _require_identifier("name", self.name) - _require_identifier("pool", self.pool) + _require_identifier("queuePool", self.queue_pool) _require_identifier("bundle", self.bundle) _require_at_least("concurrency", self.concurrency, 1) _require_at_least("maxContainers", self.max_containers, 1) @@ -962,7 +962,7 @@ def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: return reader.build( cls, name=reader.string("name"), - pool=reader.string("pool"), + queue_pool=reader.string("queuePool"), bundle=reader.string("bundle"), cell=reader.child("cell", Cell.from_dict), container_shape=reader.child("containerShape", ContainerShape.from_dict), @@ -974,7 +974,7 @@ def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: def to_dict(self) -> dict[str, object]: return { "name": self.name, - "pool": self.pool, + "queuePool": self.queue_pool, "bundle": self.bundle, "cell": self.cell.to_dict(), "containerShape": self.container_shape.to_dict(), diff --git a/packages/sie_placement/tests/fixtures/plan.json b/packages/sie_placement/tests/fixtures/plan.json index cc601e491..ed9a2f5fa 100644 --- a/packages/sie_placement/tests/fixtures/plan.json +++ b/packages/sie_placement/tests/fixtures/plan.json @@ -5,7 +5,7 @@ "lanes": [ { "name": "default-l4-default", - "pool": "default", + "queuePool": "default", "bundle": "default", "cell": { "machineProfile": "l4", @@ -29,7 +29,7 @@ }, { "name": "default-a100-80gb-generation", - "pool": "default", + "queuePool": "default", "bundle": "generation", "cell": { "machineProfile": "a100-80gb", diff --git a/packages/sie_placement/tests/test_schema.py b/packages/sie_placement/tests/test_schema.py index 39833b128..2ca86915c 100644 --- a/packages/sie_placement/tests/test_schema.py +++ b/packages/sie_placement/tests/test_schema.py @@ -529,7 +529,7 @@ def test_plans_built_in_code_round_trip(): shape = ContainerShape(gpu_count=1, cpu_cores=4, memory_mib=16384) lane = PlanLane( name="default-l4-default", - pool="default", + queue_pool="default", bundle="default", cell=cell, container_shape=shape, From 2ccf70dcb6d577394b462f33add040c43f9d249a Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Wed, 4 Nov 2026 22:10:47 +0100 Subject: [PATCH 04/22] docs(placement): state how telemetry route labels fold into operations Receipts and demand name the work primitive a lane runs, while gateway telemetry labels requests by route. Document the fold an adapter applies: `embeddings` is rewritten into an encode request and counts as `encode`, `moderations` answers 501 without serving a model and carries no demand, and `other` names no primitive. Also describe the service level fields without a fixed count, since a generation identity declares four. --- packages/sie_placement/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 8915201cd..38c5a069c 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -43,9 +43,20 @@ with open("plan.json", encoding="utf-8") as handle: assert Plan.from_dict(plan.to_dict()) == plan ``` +## Operations + +Receipts and demand name the primitive a lane runs: `encode`, `score`, +`extract` or `generate`. Gateway telemetry labels requests by route and +already reports the generation and transcription routes as `generate` and +`extract`. An adapter folds the remaining route labels the same way: +`embeddings`, the OpenAI-compatible route that the gateway rewrites into an +encode request, counts as `encode`; `moderations` answers 501 without serving +a model, so it carries no demand to place; `other` names no primitive and is +not assigned to one. + ## Service levels -An identity declares up to three numbers, or inherits them from the tier it +An identity declares the fields below, or inherits them from the tier it names. Fields it declares override the tier's. | Field | Meaning | From a516e71c516f7285b68462014d7f648b52a42b7c Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Wed, 4 Nov 2026 22:11:13 +0100 Subject: [PATCH 05/22] style(placement): format the float-range number check --- packages/sie_placement/src/sie_placement/schema.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/sie_placement/src/sie_placement/schema.py b/packages/sie_placement/src/sie_placement/schema.py index 2acffb7c5..410d6cf7e 100644 --- a/packages/sie_placement/src/sie_placement/schema.py +++ b/packages/sie_placement/src/sie_placement/schema.py @@ -146,7 +146,9 @@ def _number(self, key: str, value: object) -> float: try: finite = math.isfinite(value) except OverflowError: - raise SchemaError(self._at(key), "expected a finite number, got an integer beyond the float range") from None + raise SchemaError( + self._at(key), "expected a finite number, got an integer beyond the float range" + ) from None if not finite: raise SchemaError(self._at(key), f"expected a finite number, got {value!r}") return value From c7c9aff697e47a728e036b5c586a5032e7a9cf5e Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Wed, 4 Nov 2026 22:12:09 +0100 Subject: [PATCH 06/22] docs(placement): rewrap the lanes paragraph --- packages/sie_placement/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 38c5a069c..2e434f900 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -82,9 +82,9 @@ Helm chart: `queuePool`, the `machineProfile` of its `cell`, and `bundle`. `queuePool` is the physical queue pool the lane's workers consume: `workers.common.queuePool` (`default`), or a worker group's `queuePool` override. It is neither the `workers.pools` map key, which names a worker -group, nor an SDK logical pool. `bundle` is a key of that worker group's `bundles` map. `name` -is the lane's handle inside the plan, and each identity refers to its lane by -that name. +group, nor an SDK logical pool. `bundle` is a key of that worker group's +`bundles` map. `name` is the lane's handle inside the plan, and each identity +refers to its lane by that name. ## Envelope From 73cb814681cb8b94ac49f42bf5c004b256a9a085 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Thu, 5 Nov 2026 01:00:04 +0100 Subject: [PATCH 07/22] feat(placement): add the lane tuner decision step Add sie_placement.tuner, a pure standard-library step function that an online lane tuner calls once per tick: - step(envelope, observation, current) returns one action (scale_up, scale_down, hold, hold_at_cap or raise_to_floor) and the floor, buffer and scale-down window to apply - every returned setting lies inside the plan's envelope; a floor below floorMin is raised with raise_to_floor, and a floor above floorMax is lowered to it with hold_at_cap - the floor moves one container per tick, falls only when nothing waits, nothing is in flight and the service level is not breached, and rises again only once the lane runs its current floor, so a slow boot does not ratchet it - the buffer and window are tuned only when the envelope bounds them and the caller supplies them, so an applier that moves only the floor gets the same floor decisions - an unmeasured attainment steers on the queue alone The tests run the step over a grid of envelopes, current settings and observations that covers every comparison the step makes, and check the cap and minimum edges, idle-only scale-down, a slow boot and re-convergence after a reset to the envelope minimum. --- packages/sie_placement/README.md | 29 ++ .../sie_placement/src/sie_placement/tuner.py | 145 ++++++ packages/sie_placement/tests/test_tuner.py | 412 ++++++++++++++++++ 3 files changed, 586 insertions(+) create mode 100644 packages/sie_placement/src/sie_placement/tuner.py create mode 100644 packages/sie_placement/tests/test_tuner.py diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 2e434f900..8ccc8864f 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -94,6 +94,35 @@ lane's `maxContainers`. `bufferMin` with `bufferMax`, and `scaledownWindowMinS` with `scaledownWindowMaxS`, are optional pairs for platforms that can move a warm buffer or a scale-down window per lane. +## Tuner + +`sie_placement.tuner.step(envelope, observation, current)` decides one tick of +an online tuner for one lane. It is a pure function, so every applier calls the +same decision. The `Observation` gives the containers running on the lane, the +requests waiting for capacity, the requests in flight and an `attainment` of +`met`, `breached` or `unmeasured`. `current` is the `Settings` applied now: the +floor, and optionally the buffer and the scale-down window. The result is a +`Step`, one action and the settings to apply. + +| Action | When | Settings | +| --- | --- | --- | +| `raise_to_floor` | The current floor is below `floorMin` | Floor `floorMin` | +| `hold_at_cap` | The current floor is above `floorMax`, or the lane is under pressure and every tuned setting is at its maximum | Floor `floorMax` if it was above it, otherwise unchanged | +| `scale_up` | Requests wait or the service level is breached, and the lane runs at least its current floor | Floor and buffer one container higher, window halfway to its maximum | +| `scale_down` | Nothing waits, nothing is in flight and the service level is not breached | Floor and buffer one container lower, window halfway to its minimum | +| `hold` | Otherwise, including a lane under pressure that has not yet started its current floor | Unchanged | + +Every returned setting lies inside the envelope. A buffer or window outside its +range is brought inside it on the same tick. The buffer and the window are +tuned only when the envelope declares their range and `current` carries a +value, and otherwise pass through unchanged, so an applier that moves only the +floor passes `Settings(floor=n)`. The floor never depends on the buffer or the +window. An `unmeasured` attainment steers on the queue alone, as `met` does. + +A deploy that resets the applied settings to the envelope minimum is a fresh +start: under steady demand the settings climb back one step per tick until +the lane serves the demand or reaches the top of its envelope. + ## Tests ```bash diff --git a/packages/sie_placement/src/sie_placement/tuner.py b/packages/sie_placement/src/sie_placement/tuner.py new file mode 100644 index 000000000..003d713e8 --- /dev/null +++ b/packages/sie_placement/src/sie_placement/tuner.py @@ -0,0 +1,145 @@ +"""One tick of the online lane tuner, kept inside the envelope a plan writes.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from enum import StrEnum + +from sie_placement.schema import Envelope + + +class Action(StrEnum): + SCALE_UP = "scale_up" + SCALE_DOWN = "scale_down" + HOLD = "hold" + HOLD_AT_CAP = "hold_at_cap" + RAISE_TO_FLOOR = "raise_to_floor" + + +class Attainment(StrEnum): + MET = "met" + BREACHED = "breached" + UNMEASURED = "unmeasured" + + +def _require_non_negative(name: str, value: int | None) -> None: + if value is not None and value < 0: + raise ValueError(f"{name} must be at least 0, got {value!r}") + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Observation: + runners: int + backlog: int + running: int + attainment: Attainment = Attainment.UNMEASURED + + def __post_init__(self) -> None: + _require_non_negative("runners", self.runners) + _require_non_negative("backlog", self.backlog) + _require_non_negative("running", self.running) + + @property + def pressured(self) -> bool: + return self.backlog > 0 or self.attainment is Attainment.BREACHED + + @property + def idle(self) -> bool: + return not self.pressured and self.running == 0 + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Settings: + floor: int + buffer: int | None = None + scaledown_window_s: int | None = None + + def __post_init__(self) -> None: + _require_non_negative("floor", self.floor) + _require_non_negative("buffer", self.buffer) + _require_non_negative("scaledown_window_s", self.scaledown_window_s) + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Step: + action: Action + settings: Settings + + +def _one_toward(value: int, target: int) -> int: + if value < target: + return value + 1 + if value > target: + return value - 1 + return value + + +def _halfway_toward(value: int, target: int) -> int: + if value < target: + return value + (target - value + 1) // 2 + return value - (value - target + 1) // 2 + + +def _clamp(value: int, lower: int, upper: int) -> int: + return max(lower, min(value, upper)) + + +def _optional(move: Callable[[int, int], int], value: int | None, target: int | None) -> int | None: + if value is None or target is None: + return value + return move(value, target) + + +def _optional_clamp(value: int | None, lower: int | None, upper: int | None) -> int | None: + if value is None or lower is None or upper is None: + return value + return _clamp(value, lower, upper) + + +def _edge(envelope: Envelope, current: Settings, *, upper: bool) -> Settings: + buffer_end = envelope.buffer_max if upper else envelope.buffer_min + window_end = envelope.scaledown_window_max_s if upper else envelope.scaledown_window_min_s + return Settings( + floor=envelope.floor_max if upper else envelope.floor_min, + buffer=current.buffer if current.buffer is None or buffer_end is None else buffer_end, + scaledown_window_s=( + current.scaledown_window_s if current.scaledown_window_s is None or window_end is None else window_end + ), + ) + + +def _toward(settings: Settings, edge: Settings) -> Settings: + return Settings( + floor=_one_toward(settings.floor, edge.floor), + buffer=_optional(_one_toward, settings.buffer, edge.buffer), + scaledown_window_s=_optional(_halfway_toward, settings.scaledown_window_s, edge.scaledown_window_s), + ) + + +def step(envelope: Envelope, observation: Observation, current: Settings) -> Step: + lowest = _edge(envelope, current, upper=False) + highest = _edge(envelope, current, upper=True) + inside = Settings( + floor=_clamp(current.floor, lowest.floor, highest.floor), + buffer=_optional_clamp(current.buffer, lowest.buffer, highest.buffer), + scaledown_window_s=_optional_clamp( + current.scaledown_window_s, lowest.scaledown_window_s, highest.scaledown_window_s + ), + ) + if current.floor < envelope.floor_min: + return Step(action=Action.RAISE_TO_FLOOR, settings=inside) + if current.floor > envelope.floor_max: + return Step(action=Action.HOLD_AT_CAP, settings=inside) + if observation.pressured: + raised = _toward(inside, highest) + if raised == inside: + return Step(action=Action.HOLD_AT_CAP, settings=inside) + if observation.runners < inside.floor: + return Step(action=Action.HOLD, settings=inside) + return Step(action=Action.SCALE_UP, settings=raised) + if observation.idle: + lowered = _toward(inside, lowest) + if lowered != inside: + return Step(action=Action.SCALE_DOWN, settings=lowered) + return Step(action=Action.HOLD, settings=inside) diff --git a/packages/sie_placement/tests/test_tuner.py b/packages/sie_placement/tests/test_tuner.py new file mode 100644 index 000000000..663df8760 --- /dev/null +++ b/packages/sie_placement/tests/test_tuner.py @@ -0,0 +1,412 @@ +from __future__ import annotations + +import itertools +from collections.abc import Iterator + +import pytest +from sie_placement.schema import Envelope +from sie_placement.tuner import Action, Attainment, Observation, Settings, Step, step + +FLOOR_RANGES = [(0, 0), (0, 1), (0, 3), (1, 1), (1, 4), (2, 3)] +BUFFER_RANGES: list[tuple[int, int] | None] = [None, (0, 0), (0, 2), (1, 3)] +WINDOW_RANGES: list[tuple[int, int] | None] = [None, (60, 60), (10, 300)] + + +def make_envelope( + floor: tuple[int, int], + buffer: tuple[int, int] | None = None, + window: tuple[int, int] | None = None, +) -> Envelope: + return Envelope( + floor_min=floor[0], + floor_max=floor[1], + buffer_min=None if buffer is None else buffer[0], + buffer_max=None if buffer is None else buffer[1], + scaledown_window_min_s=None if window is None else window[0], + scaledown_window_max_s=None if window is None else window[1], + ) + + +ENVELOPES = [ + make_envelope(floor, buffer, window) + for floor, buffer, window in itertools.product(FLOOR_RANGES, BUFFER_RANGES, WINDOW_RANGES) +] + + +def floor_only(envelope: Envelope) -> Envelope: + return Envelope(floor_min=envelope.floor_min, floor_max=envelope.floor_max) + + +def around(bounds: tuple[int | None, int | None]) -> list[int | None]: + lower, upper = bounds + if lower is None or upper is None: + return [None, 0, 5] + values = {lower - 1, lower, (lower + upper) // 2, upper, upper + 1} + return [None, *sorted(value for value in values if value >= 0)] + + +def currents(envelope: Envelope) -> Iterator[Settings]: + for floor, buffer, window in itertools.product( + range(envelope.floor_max + 2), + around((envelope.buffer_min, envelope.buffer_max)), + around((envelope.scaledown_window_min_s, envelope.scaledown_window_max_s)), + ): + yield Settings(floor=floor, buffer=buffer, scaledown_window_s=window) + + +def observations(current: Settings) -> Iterator[Observation]: + for runners, backlog, running, attainment in itertools.product( + sorted({max(0, current.floor - 1), current.floor, current.floor + 1}), + (0, 2), + (0, 1), + Attainment, + ): + yield Observation(runners=runners, backlog=backlog, running=running, attainment=attainment) + + +def tuned(lower: int | None, value: int | None) -> bool: + return lower is not None and value is not None + + +def bounds_of(envelope: Envelope) -> list[tuple[str, int | None, int | None]]: + return [ + ("floor", envelope.floor_min, envelope.floor_max), + ("buffer", envelope.buffer_min, envelope.buffer_max), + ("scaledown_window_s", envelope.scaledown_window_min_s, envelope.scaledown_window_max_s), + ] + + +def clamped(envelope: Envelope, current: Settings) -> Settings: + values: dict[str, int | None] = {} + for name, lower, upper in bounds_of(envelope): + value = getattr(current, name) + if lower is None or upper is None or value is None: + values[name] = value + else: + values[name] = max(lower, min(value, upper)) + return Settings(**values) + + +def tuned_pairs(envelope: Envelope, current: Settings, result: Settings) -> list[tuple[int, int, int, int]]: + pairs = [] + for name, lower, upper in bounds_of(envelope): + before = getattr(current, name) + if lower is None or upper is None or before is None: + continue + pairs.append((before, getattr(result, name), lower, upper)) + return pairs + + +def assert_step_invariants(envelope: Envelope, current: Settings, observation: Observation, result: Step) -> None: + settings = result.settings + assert envelope.floor_min <= settings.floor <= envelope.floor_max + for name, lower, upper in bounds_of(envelope)[1:]: + before, after = getattr(current, name), getattr(settings, name) + if tuned(lower, before): + assert lower is not None + assert upper is not None + assert after is not None + assert lower <= after <= upper + else: + assert after == before + + idle = observation.backlog == 0 and observation.running == 0 and observation.attainment is not Attainment.BREACHED + pressured = observation.backlog > 0 or observation.attainment is Attainment.BREACHED + inside = clamped(envelope, current) + pairs = tuned_pairs(envelope, current, settings) + in_envelope = inside == current + + if current.floor < envelope.floor_min: + assert result.action is Action.RAISE_TO_FLOOR + assert settings == inside + assert settings.floor == envelope.floor_min + return + if current.floor > envelope.floor_max: + assert result.action is Action.HOLD_AT_CAP + assert settings == inside + assert settings.floor == envelope.floor_max + return + + assert abs(settings.floor - current.floor) <= 1 + if result.action is Action.SCALE_UP: + assert pressured + assert observation.runners >= current.floor + assert all(after >= min(max(before, lower), upper) for before, after, lower, upper in pairs) + assert settings != inside + elif result.action is Action.SCALE_DOWN: + assert idle + assert all(after <= min(max(before, lower), upper) for before, after, lower, upper in pairs) + assert settings != inside + elif result.action is Action.HOLD_AT_CAP: + assert pressured + assert settings == inside + assert all(after == upper for _, after, _, upper in pairs) + elif result.action is Action.HOLD: + assert settings == inside + else: + pytest.fail(f"unexpected {result.action} for a floor inside the envelope") + + if in_envelope and result.action in {Action.HOLD, Action.HOLD_AT_CAP}: + assert settings == current + if not idle: + assert all(after >= min(max(before, lower), upper) for before, after, lower, upper in pairs) + if not pressured: + assert all(after <= min(max(before, lower), upper) for before, after, lower, upper in pairs) + + room_above = any(min(max(before, lower), upper) < upper for before, _, lower, upper in pairs) + room_below = any(min(max(before, lower), upper) > lower for before, _, lower, upper in pairs) + if pressured and not room_above: + assert result.action is Action.HOLD_AT_CAP + if pressured and room_above and observation.runners >= current.floor: + assert result.action is Action.SCALE_UP + if idle and room_below: + assert result.action is Action.SCALE_DOWN + + +@pytest.mark.parametrize("envelope", ENVELOPES, ids=lambda envelope: repr(envelope.to_dict())) +def test_every_step_over_the_grid_stays_inside_the_envelope(envelope): + for current in currents(envelope): + for observation in observations(current): + result = step(envelope, observation, current) + assert_step_invariants(envelope, current, observation, result) + + floor_result = step(floor_only(envelope), observation, Settings(floor=current.floor)) + assert result.settings.floor == floor_result.settings.floor + assert floor_result.settings == Settings(floor=floor_result.settings.floor) + + if current.buffer is None and current.scaledown_window_s is None: + assert result == step(floor_only(envelope), observation, current) + + +@pytest.mark.parametrize("floor", [0, 1, 3]) +def test_raise_to_floor_restores_a_floor_reset_below_the_envelope(floor): + envelope = make_envelope((4, 6), (1, 2), (60, 600)) + current = Settings(floor=floor, buffer=0, scaledown_window_s=30) + for observation in observations(current): + assert step(envelope, observation, current) == Step( + action=Action.RAISE_TO_FLOOR, settings=Settings(floor=4, buffer=1, scaledown_window_s=60) + ) + + +def test_a_floor_above_the_envelope_is_held_at_the_cap(): + envelope = make_envelope((1, 3)) + idle = Observation(runners=5, backlog=0, running=0) + assert step(envelope, idle, Settings(floor=5)) == Step(action=Action.HOLD_AT_CAP, settings=Settings(floor=3)) + + +def test_hold_at_cap_applies_nothing_at_the_edge(): + envelope = make_envelope((1, 3), (0, 2), (60, 600)) + current = Settings(floor=3, buffer=2, scaledown_window_s=600) + for observation in ( + Observation(runners=3, backlog=4, running=3), + Observation(runners=3, backlog=0, running=0, attainment=Attainment.BREACHED), + Observation(runners=1, backlog=4, running=1), + ): + assert step(envelope, observation, current) == Step(action=Action.HOLD_AT_CAP, settings=current) + + +def test_scale_up_moves_every_tuned_setting_one_step(): + envelope = make_envelope((0, 4), (0, 3), (60, 900)) + current = Settings(floor=1, buffer=1, scaledown_window_s=60) + observation = Observation(runners=1, backlog=3, running=1) + assert step(envelope, observation, current) == Step( + action=Action.SCALE_UP, settings=Settings(floor=2, buffer=2, scaledown_window_s=480) + ) + + +def test_scale_down_happens_only_when_the_lane_is_idle(): + envelope = make_envelope((0, 4), (0, 3), (60, 900)) + current = Settings(floor=3, buffer=2, scaledown_window_s=900) + busy = Observation(runners=3, backlog=0, running=2) + queued = Observation(runners=3, backlog=1, running=0) + breached = Observation(runners=3, backlog=0, running=0, attainment=Attainment.BREACHED) + idle = Observation(runners=3, backlog=0, running=0) + assert step(envelope, busy, current) == Step(action=Action.HOLD, settings=current) + assert step(envelope, queued, current).action is Action.SCALE_UP + assert step(envelope, breached, current).action is Action.SCALE_UP + assert step(envelope, idle, current) == Step( + action=Action.SCALE_DOWN, settings=Settings(floor=2, buffer=1, scaledown_window_s=480) + ) + + +def test_an_idle_lane_at_the_envelope_minimum_holds(): + envelope = make_envelope((1, 4), (0, 3), (60, 900)) + current = Settings(floor=1, buffer=0, scaledown_window_s=60) + idle = Observation(runners=1, backlog=0, running=0) + assert step(envelope, idle, current) == Step(action=Action.HOLD, settings=current) + + +def test_a_boot_in_progress_is_not_answered_with_another_raise(): + envelope = make_envelope((0, 8)) + current = Settings(floor=2) + booting = Observation(runners=1, backlog=5, running=1) + assert step(envelope, booting, current) == Step(action=Action.HOLD, settings=current) + + +@pytest.mark.parametrize("attainment", [Attainment.MET, Attainment.UNMEASURED]) +def test_a_service_level_that_is_not_breached_steers_on_the_queue_alone(attainment): + envelope = make_envelope((0, 4)) + current = Settings(floor=2) + for runners, backlog, running in itertools.product(range(4), range(3), range(3)): + with_attainment = Observation(runners=runners, backlog=backlog, running=running, attainment=attainment) + queue_only = Observation(runners=runners, backlog=backlog, running=running) + assert step(envelope, with_attainment, current) == step(envelope, queue_only, current) + + +def test_a_breached_service_level_raises_an_empty_lane(): + envelope = make_envelope((0, 2)) + breached = Observation(runners=0, backlog=0, running=0, attainment=Attainment.BREACHED) + assert step(envelope, breached, Settings(floor=0)) == Step(action=Action.SCALE_UP, settings=Settings(floor=1)) + + +def test_the_window_moves_halfway_to_the_edge_of_its_range(): + envelope = make_envelope((0, 0), window=(60, 900)) + pressure = Observation(runners=0, backlog=1, running=0) + idle = Observation(runners=0, backlog=0, running=0) + current = Settings(floor=0, scaledown_window_s=60) + rising = [] + while (result := step(envelope, pressure, current)).action is Action.SCALE_UP: + current = result.settings + rising.append(current.scaledown_window_s) + falling = [] + while (result := step(envelope, idle, current)).action is Action.SCALE_DOWN: + current = result.settings + falling.append(current.scaledown_window_s) + assert rising == [480, 690, 795, 848, 874, 887, 894, 897, 899, 900] + assert falling == [480, 270, 165, 112, 86, 73, 66, 63, 61, 60] + + +def test_untuned_settings_pass_through_unchanged(): + envelope = make_envelope((0, 2)) + current = Settings(floor=1, buffer=7, scaledown_window_s=1200) + pressure = Observation(runners=1, backlog=2, running=1) + assert step(envelope, pressure, current) == Step( + action=Action.SCALE_UP, settings=Settings(floor=2, buffer=7, scaledown_window_s=1200) + ) + + +def observe(settings: Settings, demand: int, runners: int | None = None) -> Observation: + active = demand > 0 + capacity = settings.floor + ((settings.buffer or 0) if active else 0) + started = capacity if runners is None else runners + running = min(demand, started) + return Observation(runners=started, backlog=demand - running, running=running) + + +def run(envelope: Envelope, start: Settings, demand: int, ticks: int) -> list[Step]: + steps = [] + current = start + for _ in range(ticks): + result = step(envelope, observe(current, demand), current) + steps.append(result) + current = result.settings + return steps + + +def ticks_to_settle(steps: list[Step]) -> int: + for index, result in enumerate(steps): + if result.action in {Action.HOLD, Action.HOLD_AT_CAP}: + return index + pytest.fail("the tuner did not settle") + + +def minimum(envelope: Envelope) -> Settings: + return Settings( + floor=envelope.floor_min, + buffer=envelope.buffer_min, + scaledown_window_s=envelope.scaledown_window_min_s, + ) + + +@pytest.mark.parametrize("demand", [0, 1, 3, 4, 6, 9]) +@pytest.mark.parametrize("start", [1, 2, 5, 6]) +def test_a_reset_to_the_envelope_minimum_reconverges_to_the_floor_demand_needs(demand, start): + envelope = make_envelope((1, 6)) + before = run(envelope, Settings(floor=start), demand, 12) + settled = before[-1].settings + assert before[-1].action in {Action.HOLD, Action.HOLD_AT_CAP} + + after = run(envelope, minimum(envelope), demand, 12) + needed = max(envelope.floor_min, min(demand, envelope.floor_max)) + assert ticks_to_settle(after) <= envelope.floor_max - envelope.floor_min + assert after[-1].settings == Settings(floor=needed) + if demand > envelope.floor_max: + assert after[-1].action is Action.HOLD_AT_CAP + if start <= needed: + assert settled == after[-1].settings + else: + assert settled.floor >= needed + + +@pytest.mark.parametrize("envelope", ENVELOPES, ids=lambda envelope: repr(envelope.to_dict())) +def test_sustained_pressure_from_the_minimum_reaches_the_cap_within_the_bound(envelope): + start = minimum(envelope) + pressure = Observation(runners=envelope.floor_max, backlog=1, running=envelope.floor_max) + bound = max( + envelope.floor_max - envelope.floor_min, + 0 if envelope.buffer_max is None or envelope.buffer_min is None else envelope.buffer_max - envelope.buffer_min, + 0 + if envelope.scaledown_window_max_s is None or envelope.scaledown_window_min_s is None + else (envelope.scaledown_window_max_s - envelope.scaledown_window_min_s).bit_length(), + ) + current = start + for _ in range(bound): + result = step(envelope, pressure, current) + assert result.action is Action.SCALE_UP + current = result.settings + assert step(envelope, pressure, current) == Step(action=Action.HOLD_AT_CAP, settings=current) + + idle = Observation(runners=current.floor, backlog=0, running=0) + for _ in range(bound): + result = step(envelope, idle, current) + assert result.action is Action.SCALE_DOWN + current = result.settings + assert step(envelope, idle, current) == Step(action=Action.HOLD, settings=start) + + +@pytest.mark.parametrize("demand", [0, 1, 4, 7, 30]) +def test_a_reset_of_every_tuned_setting_reconverges_to_a_fixed_point_that_serves_demand(demand): + envelope = make_envelope((1, 4), (0, 3), (60, 900)) + bound = max(3, 3, (900 - 60).bit_length()) + after = run(envelope, minimum(envelope), demand, bound + 2) + assert ticks_to_settle(after) <= bound + settled = after[-1].settings + observation = observe(settled, demand) + result = step(envelope, observation, settled) + assert result.settings == settled + assert observation.backlog == 0 or result.action is Action.HOLD_AT_CAP + + +def test_a_slow_boot_raises_the_floor_once_per_realized_step(): + envelope = make_envelope((0, 8)) + current = Settings(floor=0) + boot_ticks = 3 + history = [0] + for _ in range(30): + runners = history[max(0, len(history) - 1 - boot_ticks)] + result = step(envelope, observe(current, 5, runners), current) + assert result.settings.floor <= runners + 1 + current = result.settings + history.append(current.floor) + assert current.floor == 5 + + +@pytest.mark.parametrize( + "make", + [ + lambda: Observation(runners=-1, backlog=0, running=0), + lambda: Observation(runners=0, backlog=-1, running=0), + lambda: Observation(runners=0, backlog=0, running=-1), + lambda: Settings(floor=-1), + lambda: Settings(floor=0, buffer=-1), + lambda: Settings(floor=0, scaledown_window_s=-1), + ], +) +def test_negative_counts_are_rejected(make): + with pytest.raises(ValueError, match="must be at least 0"): + make() + + +def test_actions_carry_their_wire_names(): + assert [action.value for action in Action] == ["scale_up", "scale_down", "hold", "hold_at_cap", "raise_to_floor"] + assert [attainment.value for attainment in Attainment] == ["met", "breached", "unmeasured"] From 99a35fa338bc0d697bed5efa6d2a776fd9b91666 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Thu, 5 Nov 2026 01:11:01 +0100 Subject: [PATCH 08/22] fix(placement): read an attainment given as its wire string An Observation built from JSON, a database row or metric labels carries the attainment as a plain string. The step compared it by identity with Attainment.BREACHED, so a breach given as "breached" was never seen as pressure. Observation now converts the attainment to the enum, and an unknown value raises ValueError as a negative count does. --- .../sie_placement/src/sie_placement/tuner.py | 1 + packages/sie_placement/tests/test_tuner.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/sie_placement/src/sie_placement/tuner.py b/packages/sie_placement/src/sie_placement/tuner.py index 003d713e8..98e982fe3 100644 --- a/packages/sie_placement/src/sie_placement/tuner.py +++ b/packages/sie_placement/src/sie_placement/tuner.py @@ -39,6 +39,7 @@ def __post_init__(self) -> None: _require_non_negative("runners", self.runners) _require_non_negative("backlog", self.backlog) _require_non_negative("running", self.running) + object.__setattr__(self, "attainment", Attainment(self.attainment)) @property def pressured(self) -> bool: diff --git a/packages/sie_placement/tests/test_tuner.py b/packages/sie_placement/tests/test_tuner.py index 663df8760..4c19eab49 100644 --- a/packages/sie_placement/tests/test_tuner.py +++ b/packages/sie_placement/tests/test_tuner.py @@ -2,6 +2,7 @@ import itertools from collections.abc import Iterator +from typing import Any import pytest from sie_placement.schema import Envelope @@ -407,6 +408,24 @@ def test_negative_counts_are_rejected(make): make() +@pytest.mark.parametrize("attainment", list(Attainment)) +def test_an_attainment_given_as_its_wire_string_reads_as_its_value(attainment): + wire: Any = attainment.value + envelope = make_envelope((0, 4)) + for runners, backlog, running in itertools.product(range(2), range(2), range(2)): + from_wire = Observation(runners=runners, backlog=backlog, running=running, attainment=wire) + typed = Observation(runners=runners, backlog=backlog, running=running, attainment=attainment) + assert from_wire.attainment is attainment + assert from_wire == typed + assert step(envelope, from_wire, Settings(floor=1)) == step(envelope, typed, Settings(floor=1)) + + +def test_an_unknown_attainment_is_rejected(): + wire: Any = "late" + with pytest.raises(ValueError, match="not a valid Attainment"): + Observation(runners=0, backlog=0, running=0, attainment=wire) + + def test_actions_carry_their_wire_names(): assert [action.value for action in Action] == ["scale_up", "scale_down", "hold", "hold_at_cap", "raise_to_floor"] assert [attainment.value for attainment in Attainment] == ["met", "breached", "unmeasured"] From 18045bf5956f576bbd189d3fe9711459283c1c16 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Thu, 5 Nov 2026 01:11:07 +0100 Subject: [PATCH 09/22] docs(placement): state how an applier feeds and applies the tuner step - current is the settings in force on the lane, read from the platform each tick or recorded again after a deploy resets them, never the previous Step.settings carried across a reset - an applier applies Step.settings whenever they differ from current, whatever the action, and records the action as the audit label - the action table now says that raise_to_floor, hold_at_cap and hold bring a buffer or window outside its range inside it, and that scale_up and scale_down stop at the edge of each range --- packages/sie_placement/README.md | 44 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 8ccc8864f..be45f5483 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -100,28 +100,38 @@ platforms that can move a warm buffer or a scale-down window per lane. an online tuner for one lane. It is a pure function, so every applier calls the same decision. The `Observation` gives the containers running on the lane, the requests waiting for capacity, the requests in flight and an `attainment` of -`met`, `breached` or `unmeasured`. `current` is the `Settings` applied now: the -floor, and optionally the buffer and the scale-down window. The result is a -`Step`, one action and the settings to apply. +`met`, `breached` or `unmeasured`. An attainment given as its wire string reads +as that value, and any other value is rejected. `current` is the `Settings` in +force on the lane: the floor, and optionally the buffer and the scale-down +window. The result is a `Step`, one action and the settings to apply. + +An applier reads `current` on every tick from the platform that scales the +lane, or records it again whenever a deploy or another writer resets those +settings. It never feeds the previous `Step.settings` back in as `current` +across such a reset, because the step then steers from settings the lane no +longer has. It applies `Step.settings` whenever they differ from `current`, +whatever the action. The action labels the decision for the audit record. | Action | When | Settings | | --- | --- | --- | -| `raise_to_floor` | The current floor is below `floorMin` | Floor `floorMin` | -| `hold_at_cap` | The current floor is above `floorMax`, or the lane is under pressure and every tuned setting is at its maximum | Floor `floorMax` if it was above it, otherwise unchanged | -| `scale_up` | Requests wait or the service level is breached, and the lane runs at least its current floor | Floor and buffer one container higher, window halfway to its maximum | -| `scale_down` | Nothing waits, nothing is in flight and the service level is not breached | Floor and buffer one container lower, window halfway to its minimum | -| `hold` | Otherwise, including a lane under pressure that has not yet started its current floor | Unchanged | - -Every returned setting lies inside the envelope. A buffer or window outside its -range is brought inside it on the same tick. The buffer and the window are -tuned only when the envelope declares their range and `current` carries a -value, and otherwise pass through unchanged, so an applier that moves only the -floor passes `Settings(floor=n)`. The floor never depends on the buffer or the -window. An `unmeasured` attainment steers on the queue alone, as `met` does. +| `raise_to_floor` | The current floor is below `floorMin` | Floor `floorMin`, and a buffer or window outside its range brought inside it | +| `hold_at_cap` | The current floor is above `floorMax`, or the lane is under pressure and every tuned setting is at its maximum | Floor `floorMax` if it was above it, and a buffer or window outside its range brought inside it. Otherwise unchanged | +| `scale_up` | Requests wait or the service level is breached, and the lane runs at least its current floor | Floor and buffer one container higher and window halfway to its maximum, each up to the top of its range | +| `scale_down` | Nothing waits, nothing is in flight and the service level is not breached | Floor and buffer one container lower and window halfway to its minimum, each down to the bottom of its range | +| `hold` | Otherwise, including a lane under pressure that has not yet started its current floor | Unchanged, except that a buffer or window outside its range is brought inside it | + +Every returned setting lies inside the envelope. `hold` and `hold_at_cap` +return settings other than `current` only when `current` lay outside it. The +buffer and the window are tuned only when the envelope declares their range +and `current` carries a value, and otherwise pass through unchanged, so an +applier that moves only the floor passes `Settings(floor=n)`. The floor never +depends on the buffer or the window. An `unmeasured` attainment steers on the +queue alone, as `met` does. A deploy that resets the applied settings to the envelope minimum is a fresh -start: under steady demand the settings climb back one step per tick until -the lane serves the demand or reaches the top of its envelope. +start: read back as `current`, the settings climb one step per tick under +steady demand until the lane serves the demand or reaches the top of its +envelope. ## Tests From a05b5ba49bbd9e1f7cb8e8208fcfc9d8d97fdb76 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Thu, 5 Nov 2026 03:15:54 +0100 Subject: [PATCH 10/22] feat(placement): add the sie-placement tune KEDA applier Add a `sie-placement tune` command that keeps each plan lane's warm floor inside its envelope on a KEDA-scaled sie-cluster deployment: - every tick it reads four lane series from Prometheus (lane queue depth, pending demand, warm floor and ready workers), reads the lane's floor-carrier pool with SIEClient.get_pool, and calls the tuner step with that pool's floor as current - shadow mode is the default and writes nothing; with --apply it upserts one floor-carrier logical pool per lane through SIEClient.create_pool on every tick, which also renews the pool's lease - a carrier is named placement-floor--- and has gpus {: 0}, the lane's queue pool and bundle, no pins, no caps and the gateway's default lease; a pool with that name and any other spec is reported and never written - the applier writes only its carriers, so it never lowers another pool's floor, and each audit row reports a lane whose warm floor exceeds floorMax - a lane missing a series is held and only brought inside its envelope - the plan is refused before any call when an envelope's floorMax is above the gateway's per-pool floor limit, when it has more lanes than the gateway's API pool limit, or when its lane tokens cannot name a carrier Pool reads use a gateway client token and pool writes the gateway admin token, each read from a file or an environment variable. The admin token is read only with --apply, is sent only on pool writes, and is never printed or logged. The README documents the permission each call needs. The SIE SDK and httpx come from a new keda extra; the schema, tuner and applier core keep to the standard library. Tests drive the applier with a fake Prometheus and fake pools, and drive the command through the real SDK against fake HTTP Prometheus and gateway servers. --- packages/sie_placement/README.md | 111 +++- packages/sie_placement/pyproject.toml | 9 + .../sie_placement/src/sie_placement/cli.py | 168 +++++ .../sie_placement/src/sie_placement/keda.py | 435 +++++++++++++ .../sie_placement/src/sie_placement/pools.py | 41 ++ packages/sie_placement/tests/test_cli.py | 370 +++++++++++ packages/sie_placement/tests/test_keda.py | 577 ++++++++++++++++++ uv.lock | 10 + 8 files changed, 1719 insertions(+), 2 deletions(-) create mode 100644 packages/sie_placement/src/sie_placement/cli.py create mode 100644 packages/sie_placement/src/sie_placement/keda.py create mode 100644 packages/sie_placement/src/sie_placement/pools.py create mode 100644 packages/sie_placement/tests/test_cli.py create mode 100644 packages/sie_placement/tests/test_keda.py diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index be45f5483..873dd8c19 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -3,8 +3,9 @@ Versioned documents for a measured-only placement planner: the service level each served identity must meet, the measurements a placement may be chosen from, the prices those placements cost, the demand they serve, and the plan -that results. The package uses only the Python standard library. It is a -workspace package and is not published to PyPI. +that results. The package uses only the Python standard library, except the +KEDA applier, whose `keda` extra adds the SIE Python SDK. It is a workspace +package and is not published to PyPI. A served identity is a model id or a `model:profile` pair, the routing key a request resolves to. A measurement cell is a machine profile together with the @@ -133,6 +134,112 @@ start: read back as `current`, the settings climb one step per tick under steady demand until the lane serves the demand or reaches the top of its envelope. +## KEDA applier + +`sie-placement tune` keeps each plan lane's warm floor inside its envelope on a +`sie-cluster` deployment that autoscales workers with KEDA. It needs the `keda` +extra. + +```bash +sie-placement tune --plan plan.json \ + --prometheus-url http://prometheus:9090 \ + --gateway-url http://sie-gateway:8080 \ + --api-key-file /run/secrets/gateway-client-token +``` + +Every tick, 60 seconds apart by default or once with `--once`, it reads the +four lane series below from Prometheus, reads each lane's floor-carrier pool +from the gateway with `SIEClient.get_pool`, and calls `step` with the carrier's +floor as `current`. It prints one JSON audit row per lane to standard output. Without +`--apply` it runs in shadow mode: it decides and records and writes nothing. +With `--apply` it upserts every lane's carrier on every tick through +`SIEClient.create_pool`, which renews the carrier's lease and sets its floor to +the step's floor. A lane that has no carrier and whose floor stays at 0 gets +none. + +Under KEDA only the floor moves per lane. The scale-down window and the queue +threshold are chart-wide settings, and there is no buffer to tune, so the +applier passes `Settings(floor=n)` and ignores the envelope's buffer and window +ranges. + +### Floor carriers + +A floor carrier is an API logical pool named +`placement-floor---`, with `queue_pool` and +`bundle` set to the lane's, `gpus` set to `{: 0}`, the +gateway's default lease, no GPU caps, no pinned models, and +`minimum_worker_count` set to the step's floor. The gateway keeps each lane at +the largest floor among the logical pools on it, so the carrier can raise the +lane above its chart floor and never lowers another pool's floor. The applier +writes only its carriers: a pool that has a carrier's name and any other spec is +reported and left unchanged. A carrier carries no pins, because every floor +change is a spec change, which resets the pool's worker assignment; pinned +models stay in the chart values. + +When the applier stops renewing, each carrier's lease lapses, 1200 seconds by +default, the gateway removes it, and the lane returns to its chart floor. On a +lane whose chart `minReplicas` is 0, a floor of 1 or more activates the lane, +which then holds at least the chart's fallback floor. + +### Series + +| Series | Query | Step input | +| --- | --- | --- | +| Queue depth | `max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{})` | `running` | +| Pending demand | `max by (pool, machine_profile, bundle) (sie_gateway_pending_demand{})` | `backlog` | +| Warm floor | `max by (pool, machine_profile, bundle) (sie_gateway_pool_warm_floor{})` | Reported only | +| Running workers | `count by (lane) (max by (lane, producer_instance) (sie_worker_gpu_slots{state="ready"}) > 0)` | `runners` | + +`--series-selector` adds label matchers to every selector, for example +`namespace="sie"` when one Prometheus scrapes several releases. Lane labels +match the plan's lane tokens after trimming and lowering them, as the chart +names lanes. The attainment is `unmeasured`, so a lane is under pressure while +the gateway reports pending demand for it, which it does when a request found no +ready capacity, and idle when it reports neither pending demand nor queued or +in-flight work. A lane with no ready worker has 0 runners. A lane whose queue +depth, pending demand or worker series is missing is held: the step is given a +busy lane with nothing waiting, so it only brings the carrier's floor inside the +envelope. + +### Audit rows + +Each row carries `at`, `mode` (`shadow` or `apply`), the lane's `lane`, +`queuePool`, `machineProfile` and `bundle`, the `carrier` pool name, the +`telemetry` read for the lane, `currentFloor`, the step's `action` and `floor`, +whether the write was `applied`, `laneFloorAboveEnvelope` and `error`. +`laneFloorAboveEnvelope` is true when the lane's warm floor exceeds `floorMax`. +The applier does not lower another pool's floor, so a lane held above its +envelope by another pool stays reported until that pool changes. + +### Refusals + +The command exits with status 2 before it calls Prometheus or the gateway when: + +- a lane's `floorMax` exceeds `--max-pool-floor`, which must match the gateway's + `SIE_GATEWAY_POOL_MAX_MINIMUM_WORKER_COUNT` (default 4); +- the plan has more lanes than `--max-pools`, which must match the gateway's + `SIE_GATEWAY_MAX_POOLS` (default 64); +- a lane's `queuePool`, machine profile or bundle is not 1 to 63 letters, + digits, `_` or `-`, two lanes need the same carrier, or a carrier name is + longer than the gateway's 128 characters. + +With `--once`, a tick whose query, pool read or pool write failed exits with +status 1. + +### Credentials + +| Call | Permission | Source | +| --- | --- | --- | +| Prometheus `/api/v1/query` | Read access to the query API | None sent | +| `GET /v1/pools/{name}` | A gateway client token, when gateway token auth is on | `--api-key-file`, or `SIE_API_KEY` when `SIE_BASE_URL` names the same origin, as the SDK resolves it | +| `POST /v1/pools` and its lease renewal | The gateway admin token | `--admin-token-file`, or `SIE_ADMIN_TOKEN`, read only with `--apply` | + +The gateway admin token authorizes every gateway admin route, not only pools. +The applier reads it only with `--apply`, sends it only on pool writes to +`--gateway-url`, never takes it as a command-line value and never prints or +logs it. Without it, `--apply` sends its writes with no credential, which a +gateway with token auth refuses. The client token is never sent on writes. + ## Tests ```bash diff --git a/packages/sie_placement/pyproject.toml b/packages/sie_placement/pyproject.toml index 91b0c19d6..60fd9258a 100644 --- a/packages/sie_placement/pyproject.toml +++ b/packages/sie_placement/pyproject.toml @@ -6,6 +6,15 @@ requires-python = ">=3.12,<3.13" license = { text = "Apache-2.0" } dependencies = [] +[project.optional-dependencies] +keda = [ + "httpx>=0.28,<1", + "sie-sdk>=0.11", +] + +[project.scripts] +sie-placement = "sie_placement.cli:main" + [build-system] requires = ["hatchling"] build-backend = "hatchling.build" diff --git a/packages/sie_placement/src/sie_placement/cli.py b/packages/sie_placement/src/sie_placement/cli.py new file mode 100644 index 000000000..8f653dc5b --- /dev/null +++ b/packages/sie_placement/src/sie_placement/cli.py @@ -0,0 +1,168 @@ +"""Command-line entry point of the placement tools.""" + +from __future__ import annotations + +import argparse +import json +import math +import os +import sys +import time +from collections.abc import Callable, Sequence +from pathlib import Path + +from sie_placement import keda +from sie_placement.schema import Plan + +ADMIN_TOKEN_ENV = "SIE_ADMIN_TOKEN" # noqa: S105 - an environment variable name +DEFAULT_INTERVAL_S = 60.0 +EXIT_FAILED = 1 +EXIT_REFUSED = 2 + + +class _UsageError(ValueError): + pass + + +def _positive_float(text: str) -> float: + value = float(text) + if not math.isfinite(value) or value <= 0: + raise argparse.ArgumentTypeError(f"must be greater than 0, got {text!r}") + return value + + +def _integer_at_least(minimum: int) -> Callable[[str], int]: + def parse(text: str) -> int: + value = int(text) + if value < minimum: + raise argparse.ArgumentTypeError(f"must be at least {minimum}, got {text!r}") + return value + + return parse + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(prog="sie-placement", description="Placement planner tools.") + commands = parser.add_subparsers(dest="command", required=True) + tune = commands.add_parser( + "tune", + help="keep each plan lane's warm floor inside its envelope through gateway floor-carrier pools", + description=( + "Read each plan lane's series from Prometheus, decide one tuner step per lane and, with --apply, " + "upsert one floor-carrier pool per lane through the gateway pool API. Without --apply nothing is written." + ), + ) + tune.add_argument("--plan", required=True, type=Path, help="placement-plan document") + tune.add_argument("--prometheus-url", required=True, help="Prometheus base URL that serves /api/v1/query") + tune.add_argument("--gateway-url", required=True, help="gateway base URL") + tune.add_argument( + "--series-selector", + default="", + help="label matchers added to every series selector, for example 'namespace=\"sie\"'", + ) + tune.add_argument("--apply", action="store_true", help="write the floor-carrier pools") + tune.add_argument("--once", action="store_true", help="run one tick and exit") + tune.add_argument( + "--interval-s", type=_positive_float, default=DEFAULT_INTERVAL_S, help="seconds between ticks (default 60)" + ) + tune.add_argument( + "--max-pool-floor", + type=_integer_at_least(0), + default=keda.DEFAULT_MAX_POOL_FLOOR, + help="the gateway's SIE_GATEWAY_POOL_MAX_MINIMUM_WORKER_COUNT (default 4)", + ) + tune.add_argument( + "--max-pools", + type=_integer_at_least(1), + default=keda.DEFAULT_MAX_POOLS, + help="the gateway's SIE_GATEWAY_MAX_POOLS (default 64)", + ) + tune.add_argument("--api-key-file", type=Path, help="file holding a gateway client token for pool reads") + tune.add_argument( + "--admin-token-file", + type=Path, + help=f"file holding the gateway admin token for pool writes; read only with --apply (default ${ADMIN_TOKEN_ENV})", + ) + return parser + + +def _secret(path: Path | None, env_name: str | None, label: str) -> str | None: + if path is not None: + try: + value = path.read_text(encoding="utf-8").strip() + except OSError as error: + raise _UsageError(f"cannot read the {label} file {str(path)!r}: {error.strerror}") from None + if not value: + raise _UsageError(f"the {label} file {str(path)!r} is empty") + return value + if env_name is None: + return None + return os.environ.get(env_name, "").strip() or None + + +def _load_plan(path: Path) -> Plan: + try: + with path.open(encoding="utf-8") as handle: + document = json.load(handle) + except OSError as error: + raise _UsageError(f"cannot read the plan {str(path)!r}: {error.strerror}") from None + except ValueError as error: + raise _UsageError(f"the plan {str(path)!r} is not JSON: {error}") from None + return Plan.from_dict(document) + + +def _emit(result: keda.Tick) -> None: + for row in result.audit_rows(): + print(json.dumps(row), flush=True) + for error in result.series_errors: + print(f"sie-placement tune: Prometheus series {error}", file=sys.stderr, flush=True) + + +def _tune(args: argparse.Namespace) -> int: + try: + carriers = keda.carriers(_load_plan(args.plan), max_pool_floor=args.max_pool_floor, max_pools=args.max_pools) + keda.series_queries(args.series_selector) + prometheus = keda.PrometheusHttp(args.prometheus_url) + read_token = _secret(args.api_key_file, None, "gateway client token") + admin_token = _secret(args.admin_token_file, ADMIN_TOKEN_ENV, "gateway admin token") if args.apply else None + except ValueError as error: + print(f"sie-placement tune: {error}", file=sys.stderr) + return EXIT_REFUSED + try: + from sie_placement import pools # noqa: PLC0415 + except ImportError: + print("sie-placement tune needs the sie-placement keda extra, which installs the SIE SDK", file=sys.stderr) + return EXIT_REFUSED + if args.apply and admin_token is None: + print( + f"sie-placement tune: no gateway admin token in --admin-token-file or ${ADMIN_TOKEN_ENV}; " + "pool writes carry no credential", + file=sys.stderr, + ) + try: + reader = pools.connect(args.gateway_url, read_token) + writer = pools.connect(args.gateway_url, admin_token or "") if args.apply else None + except ValueError as error: + print(f"sie-placement tune: {error}", file=sys.stderr) + return EXIT_REFUSED + try: + while True: + started = time.monotonic() + result = keda.tick( + carriers, prometheus=prometheus, reader=reader, writer=writer, selector=args.series_selector + ) + _emit(result) + if args.once: + return EXIT_FAILED if result.failed else 0 + time.sleep(max(0.0, args.interval_s - (time.monotonic() - started))) + except KeyboardInterrupt: + return 0 + finally: + reader.close() + if writer is not None: + writer.close() + + +def main(argv: Sequence[str] | None = None) -> int: + args = _parser().parse_args(argv) + return _tune(args) diff --git a/packages/sie_placement/src/sie_placement/keda.py b/packages/sie_placement/src/sie_placement/keda.py new file mode 100644 index 000000000..3c297b70e --- /dev/null +++ b/packages/sie_placement/src/sie_placement/keda.py @@ -0,0 +1,435 @@ +"""Keep each plan lane's warm floor inside its envelope through one gateway floor-carrier pool per lane.""" + +from __future__ import annotations + +import json +import math +import re +import urllib.error +import urllib.parse +import urllib.request +from collections.abc import Callable, Mapping, Sequence +from dataclasses import dataclass +from datetime import UTC, datetime +from typing import Protocol + +from sie_placement.schema import Plan, PlanLane +from sie_placement.tuner import Observation, Settings, Step, step + +CARRIER_PREFIX = "placement-floor" +DEFAULT_MAX_POOL_FLOOR = 4 +DEFAULT_MAX_POOLS = 64 +MAX_POOL_NAME_LENGTH = 128 +PROMETHEUS_TIMEOUT_S = 10.0 + +QUEUE_DEPTH = "queueDepth" +PENDING_DEMAND = "pendingDemand" +WARM_FLOOR = "warmFloor" +RUNNERS = "runners" + +_LANE_TOKEN = re.compile(r"[a-z0-9_-]{1,63}") +_SELECTOR_FORBIDDEN = re.compile(r"[{}\r\n]") +_LANE_LABELS = "pool, machine_profile, bundle" +_LANE_TOKEN_COUNT = 3 +_VALUE_PAIR = 2 + + +class PlanRefusedError(ValueError): + def __init__(self, problems: Sequence[str]) -> None: + super().__init__("\n".join(problems)) + self.problems = tuple(problems) + + +class GatewayError(RuntimeError): + pass + + +class PrometheusError(RuntimeError): + pass + + +class CarrierConflictError(ValueError): + pass + + +@dataclass(frozen=True, slots=True) +class LaneKey: + queue_pool: str + machine_profile: str + bundle: str + + @classmethod + def canonical(cls, queue_pool: str, machine_profile: str, bundle: str) -> LaneKey | None: + tokens = [value.strip().lower() for value in (queue_pool, machine_profile, bundle)] + if any(_LANE_TOKEN.fullmatch(token) is None for token in tokens): + return None + return cls(queue_pool=tokens[0], machine_profile=tokens[1], bundle=tokens[2]) + + @property + def otel_lane(self) -> str: + return f"{self.queue_pool}|{self.machine_profile}|{self.bundle}" + + +@dataclass(frozen=True, slots=True) +class Carrier: + lane: PlanLane + key: LaneKey + + @property + def name(self) -> str: + return f"{CARRIER_PREFIX}-{self.key.queue_pool}-{self.key.machine_profile}-{self.key.bundle}" + + @property + def gpus(self) -> dict[str, int]: + return {self.key.machine_profile: 0} + + +def carriers( + plan: Plan, *, max_pool_floor: int = DEFAULT_MAX_POOL_FLOOR, max_pools: int = DEFAULT_MAX_POOLS +) -> tuple[Carrier, ...]: + problems: list[str] = [] + if len(plan.lanes) > max_pools: + problems.append( + f"$.lanes: {len(plan.lanes)} lanes need as many floor-carrier pools, " + f"above the gateway's limit of {max_pools} API pools" + ) + result: list[Carrier] = [] + first_index: dict[str, int] = {} + for index, lane in enumerate(plan.lanes): + path = f"$.lanes[{index}]" + key = LaneKey.canonical(lane.queue_pool, lane.cell.machine_profile, lane.bundle) + if key is None: + problems.append( + f"{path}: queuePool, cell.machineProfile and bundle must each be 1 to 63 letters, digits, '_' or '-'" + ) + continue + if lane.envelope.floor_max > max_pool_floor: + problems.append( + f"{path}.envelope.floorMax: {lane.envelope.floor_max} exceeds the gateway's " + f"per-pool floor limit of {max_pool_floor}" + ) + carrier = Carrier(lane=lane, key=key) + if len(carrier.name) > MAX_POOL_NAME_LENGTH: + problems.append( + f"{path}: floor-carrier pool name {carrier.name!r} is longer than {MAX_POOL_NAME_LENGTH} characters" + ) + earlier = first_index.setdefault(carrier.name, index) + if earlier != index: + problems.append(f"{path}: needs the same floor-carrier pool {carrier.name!r} as $.lanes[{earlier}]") + result.append(carrier) + if problems: + raise PlanRefusedError(problems) + return tuple(result) + + +@dataclass(frozen=True, slots=True) +class Sample: + labels: Mapping[str, str] + value: float + + +class Prometheus(Protocol): + def query(self, promql: str) -> Sequence[Sample]: ... + + +def _object(value: object) -> dict[str, object] | None: + if not isinstance(value, Mapping): + return None + fields: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + return None + fields[key] = item + return fields + + +def parse_vector(document: object) -> tuple[Sample, ...]: + response = _object(document) + if response is None or response.get("status") != "success": + raise PrometheusError("the query did not succeed") + data = _object(response.get("data")) + if data is None or data.get("resultType") != "vector": + raise PrometheusError("the query did not return an instant vector") + result = data.get("result") + if not isinstance(result, list): + raise PrometheusError("the query result is not a list") + samples: list[Sample] = [] + for item in result: + entry = _object(item) or {} + metric = _object(entry.get("metric")) + value = entry.get("value") + if metric is None or not isinstance(value, list) or len(value) != _VALUE_PAIR: + raise PrometheusError("the query returned a malformed sample") + text = value[1] + try: + number = float(text) if isinstance(text, str) else None + except ValueError: + number = None + if number is None: + raise PrometheusError("the query returned a sample that is not a number") + samples.append(Sample(labels={str(name): str(label) for name, label in metric.items()}, value=number)) + return tuple(samples) + + +class PrometheusHttp: + def __init__(self, base_url: str, *, timeout_s: float = PROMETHEUS_TIMEOUT_S) -> None: + parts = urllib.parse.urlsplit(base_url) + if parts.scheme not in {"http", "https"} or not parts.hostname: + raise ValueError("the Prometheus URL must be an absolute http or https URL") + if parts.username is not None or parts.password is not None: + raise ValueError("the Prometheus URL must not embed credentials") + if parts.query or parts.fragment: + raise ValueError("the Prometheus URL must not carry a query or a fragment") + self._endpoint = f"{base_url.rstrip('/')}/api/v1/query" + self._timeout_s = timeout_s + + def query(self, promql: str) -> tuple[Sample, ...]: + url = f"{self._endpoint}?{urllib.parse.urlencode({'query': promql})}" + try: + with urllib.request.urlopen(url, timeout=self._timeout_s) as response: # noqa: S310 + document = json.load(response) + except urllib.error.HTTPError as error: + raise PrometheusError(f"the query failed with HTTP {error.code}") from None + except (OSError, ValueError) as error: + raise PrometheusError(f"the query failed: {error}") from None + return parse_vector(document) + + +def series_queries(selector: str = "") -> dict[str, str]: + if _SELECTOR_FORBIDDEN.search(selector): + raise ValueError("the series selector must be label matchers without braces or line breaks") + + def matchers(*extra: str) -> str: + return ",".join(part for part in (selector.strip(), *extra) if part) + + return { + QUEUE_DEPTH: f"max by ({_LANE_LABELS}) (sie_gateway_lane_queue_depth{{{matchers()}}})", + PENDING_DEMAND: f"max by ({_LANE_LABELS}) (sie_gateway_pending_demand{{{matchers()}}})", + WARM_FLOOR: f"max by ({_LANE_LABELS}) (sie_gateway_pool_warm_floor{{{matchers()}}})", + RUNNERS: ( + "count by (lane) (max by (lane, producer_instance) " + f"(sie_worker_gpu_slots{{{matchers('state="ready"')}}}) > 0)" + ), + } + + +def _sample_lane(labels: Mapping[str, str], *, otel_lane: bool) -> LaneKey | None: + if otel_lane: + tokens = labels.get("lane", "").split("|") + if len(tokens) != _LANE_TOKEN_COUNT: + return None + return LaneKey.canonical(*tokens) + return LaneKey.canonical(labels.get("pool", ""), labels.get("machine_profile", ""), labels.get("bundle", "")) + + +def _count(value: float) -> int | None: + if not math.isfinite(value) or value < 0: + return None + return round(value) + + +def _lane_values(samples: Sequence[Sample], *, otel_lane: bool) -> dict[LaneKey, int]: + values: dict[LaneKey, int] = {} + for sample in samples: + key = _sample_lane(sample.labels, otel_lane=otel_lane) + count = _count(sample.value) + if key is not None and count is not None: + values[key] = max(count, values.get(key, 0)) + return values + + +@dataclass(frozen=True, slots=True, kw_only=True) +class LaneTelemetry: + queue_depth: int | None = None + pending_demand: int | None = None + warm_floor: int | None = None + runners: int | None = None + + def observation(self, current_floor: int) -> Observation: + if self.queue_depth is None or self.pending_demand is None or self.runners is None: + return Observation(runners=current_floor, backlog=0, running=1) + return Observation(runners=self.runners, backlog=self.pending_demand, running=self.queue_depth) + + def to_dict(self) -> dict[str, object]: + return { + QUEUE_DEPTH: self.queue_depth, + PENDING_DEMAND: self.pending_demand, + WARM_FLOOR: self.warm_floor, + RUNNERS: self.runners, + } + + +@dataclass(frozen=True, slots=True) +class Telemetry: + series: Mapping[str, Mapping[LaneKey, int] | None] + errors: tuple[str, ...] = () + + def _value(self, name: str, key: LaneKey, absent: int | None) -> int | None: + values = self.series.get(name) + if values is None: + return None + return values.get(key, absent) + + def lane(self, key: LaneKey) -> LaneTelemetry: + return LaneTelemetry( + queue_depth=self._value(QUEUE_DEPTH, key, None), + pending_demand=self._value(PENDING_DEMAND, key, None), + warm_floor=self._value(WARM_FLOOR, key, None), + runners=self._value(RUNNERS, key, 0), + ) + + +def read_telemetry(prometheus: Prometheus, selector: str = "") -> Telemetry: + series: dict[str, Mapping[LaneKey, int] | None] = {} + errors: list[str] = [] + for name, promql in series_queries(selector).items(): + try: + samples = prometheus.query(promql) + except PrometheusError as error: + errors.append(f"{name}: {error}") + series[name] = None + continue + series[name] = _lane_values(samples, otel_lane=name == RUNNERS) + return Telemetry(series=series, errors=tuple(errors)) + + +class PoolApi(Protocol): + def get_pool(self, name: str) -> Mapping[str, object] | None: ... + + def create_pool( + self, name: str, gpus: dict[str, int], *, bundle: str, minimum_worker_count: int, queue_pool: str + ) -> None: ... + + +def _matches(value: object, expected: str) -> bool: + return isinstance(value, str) and value.strip().lower() == expected + + +def carrier_floor(carrier: Carrier, pool: Mapping[str, object]) -> int: + spec = _object(pool.get("spec")) + if spec is None: + raise CarrierConflictError("it has no spec") + if not _matches(spec.get("queue_pool", "default"), carrier.key.queue_pool): + raise CarrierConflictError(f"it does not draw from queue pool {carrier.key.queue_pool!r}") + if not _matches(spec.get("bundle"), carrier.key.bundle): + raise CarrierConflictError(f"it is not filtered to bundle {carrier.key.bundle!r}") + gpus = _object(spec.get("gpus")) + if not ( + gpus is not None + and len(gpus) == 1 + and all( + _matches(profile, carrier.key.machine_profile) and count == 0 and not isinstance(count, bool) + for profile, count in gpus.items() + ) + ): + raise CarrierConflictError(f"its gpus are not {carrier.gpus!r}") + for field in ("gpu_caps", "pinned_models", "ttl_seconds"): + if spec.get(field): + raise CarrierConflictError(f"it sets {field}") + floor = spec.get("minimum_worker_count", 0) + if isinstance(floor, bool) or not isinstance(floor, int) or floor < 0: + raise CarrierConflictError("its minimum_worker_count is not a non-negative integer") + return floor + + +@dataclass(frozen=True, slots=True, kw_only=True) +class LaneRecord: + carrier: Carrier + telemetry: LaneTelemetry + current_floor: int | None = None + step: Step | None = None + applied: bool = False + error: str | None = None + + @property + def lane_floor_above_envelope(self) -> bool: + warm_floor = self.telemetry.warm_floor + return warm_floor is not None and warm_floor > self.carrier.lane.envelope.floor_max + + def to_dict(self) -> dict[str, object]: + return { + "lane": self.carrier.lane.name, + "queuePool": self.carrier.key.queue_pool, + "machineProfile": self.carrier.key.machine_profile, + "bundle": self.carrier.key.bundle, + "carrier": self.carrier.name, + "telemetry": self.telemetry.to_dict(), + "currentFloor": self.current_floor, + "action": None if self.step is None else self.step.action.value, + "floor": None if self.step is None else self.step.settings.floor, + "applied": self.applied, + "laneFloorAboveEnvelope": self.lane_floor_above_envelope, + "error": self.error, + } + + +@dataclass(frozen=True, slots=True, kw_only=True) +class Tick: + at: datetime + apply: bool + records: tuple[LaneRecord, ...] + series_errors: tuple[str, ...] = () + + @property + def failed(self) -> bool: + return bool(self.series_errors) or any(record.error is not None for record in self.records) + + def audit_rows(self) -> list[dict[str, object]]: + at = self.at.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%SZ") + mode = "apply" if self.apply else "shadow" + return [{"at": at, "mode": mode, **record.to_dict()} for record in self.records] + + +def _tune_lane(carrier: Carrier, telemetry: LaneTelemetry, reader: PoolApi, writer: PoolApi | None) -> LaneRecord: + try: + pool = reader.get_pool(carrier.name) + except GatewayError as error: + return LaneRecord(carrier=carrier, telemetry=telemetry, error=f"reading pool {carrier.name!r} failed: {error}") + try: + current = 0 if pool is None else carrier_floor(carrier, pool) + except CarrierConflictError as error: + return LaneRecord( + carrier=carrier, + telemetry=telemetry, + error=f"pool {carrier.name!r} is not this lane's floor carrier ({error}) and is left unchanged", + ) + result = step(carrier.lane.envelope, telemetry.observation(current), Settings(floor=current)) + record = LaneRecord(carrier=carrier, telemetry=telemetry, current_floor=current, step=result) + if writer is None or (pool is None and result.settings.floor == 0): + return record + try: + writer.create_pool( + carrier.name, + carrier.gpus, + bundle=carrier.key.bundle, + minimum_worker_count=result.settings.floor, + queue_pool=carrier.key.queue_pool, + ) + except GatewayError as error: + return LaneRecord( + carrier=carrier, + telemetry=telemetry, + current_floor=current, + step=result, + error=f"writing pool {carrier.name!r} failed: {error}", + ) + return LaneRecord(carrier=carrier, telemetry=telemetry, current_floor=current, step=result, applied=True) + + +def _utc_now() -> datetime: + return datetime.now(UTC) + + +def tick( + lanes: Sequence[Carrier], + *, + prometheus: Prometheus, + reader: PoolApi, + writer: PoolApi | None = None, + selector: str = "", + now: Callable[[], datetime] = _utc_now, +) -> Tick: + at = now() + telemetry = read_telemetry(prometheus, selector) + records = tuple(_tune_lane(carrier, telemetry.lane(carrier.key), reader, writer) for carrier in lanes) + return Tick(at=at, apply=writer is not None, records=records, series_errors=telemetry.errors) diff --git a/packages/sie_placement/src/sie_placement/pools.py b/packages/sie_placement/src/sie_placement/pools.py new file mode 100644 index 000000000..860d6f8c1 --- /dev/null +++ b/packages/sie_placement/src/sie_placement/pools.py @@ -0,0 +1,41 @@ +"""Gateway pool reads and floor-carrier writes through the SIE Python SDK, installed by the keda extra.""" + +from __future__ import annotations + +from collections.abc import Mapping + +import httpx +from sie_sdk import SIEClient, SIEError + +from sie_placement.keda import GatewayError + +TIMEOUT_S = 10.0 + + +class SdkPools: + def __init__(self, client: SIEClient) -> None: + self._client = client + + def get_pool(self, name: str) -> Mapping[str, object] | None: + try: + pool = self._client.get_pool(name) + except (SIEError, httpx.HTTPError, ValueError) as error: + raise GatewayError(str(error)) from None + return pool + + def create_pool( + self, name: str, gpus: dict[str, int], *, bundle: str, minimum_worker_count: int, queue_pool: str + ) -> None: + try: + self._client.create_pool( + name, gpus, bundle=bundle, minimum_worker_count=minimum_worker_count, queue_pool=queue_pool + ) + except (SIEError, httpx.HTTPError, ValueError) as error: + raise GatewayError(str(error)) from None + + def close(self) -> None: + self._client.close() + + +def connect(base_url: str, api_key: str | None) -> SdkPools: + return SdkPools(SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S)) diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py new file mode 100644 index 000000000..54be76b9f --- /dev/null +++ b/packages/sie_placement/tests/test_cli.py @@ -0,0 +1,370 @@ +from __future__ import annotations + +import json +import logging +import sys +import threading +from collections.abc import Iterator +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from urllib.parse import parse_qs, urlsplit + +import pytest + +pytest.importorskip("sie_sdk") + +from sie_placement import cli + +FIXTURES = Path(__file__).parent / "fixtures" +READ_TOKEN = "read-token-for-tests" # noqa: S105 - a test value +ADMIN_TOKEN = "admin-token-for-tests" # noqa: S105 - a test value +L4 = {"pool": "default", "machine_profile": "l4", "bundle": "default"} +A100 = {"pool": "default", "machine_profile": "a100-80gb", "bundle": "generation"} + + +def vector(*samples: tuple[dict[str, str], float]) -> dict: + return { + "status": "success", + "data": { + "resultType": "vector", + "result": [{"metric": labels, "value": [0, str(value)]} for labels, value in samples], + }, + } + + +class _Handler(BaseHTTPRequestHandler): + server: _Server + + def _reply(self, status: int, body: object) -> None: + payload = json.dumps(body).encode() + self.send_response(status) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(payload))) + self.end_headers() + self.wfile.write(payload) + + def _body(self) -> object: + length = int(self.headers.get("Content-Length") or 0) + return json.loads(self.rfile.read(length)) if length else None + + def do_GET(self) -> None: + self.server.respond(self, "GET", None) + + def do_POST(self) -> None: + self.server.respond(self, "POST", self._body()) + + def log_message(self, format: str, *args: object) -> None: + return + + +class _Server(ThreadingHTTPServer): + def __init__(self) -> None: + super().__init__(("127.0.0.1", 0), _Handler) + self.requests: list[dict] = [] + + @property + def url(self) -> str: + return f"http://127.0.0.1:{self.server_address[1]}" + + def respond(self, handler: _Handler, method: str, body: object) -> None: + raise NotImplementedError + + +class FakeGateway(_Server): + def __init__(self, *, auth: bool = True) -> None: + super().__init__() + self.auth = auth + self.pools: dict[str, dict] = {} + + def respond(self, handler: _Handler, method: str, body: object) -> None: + path = urlsplit(handler.path).path + authorization = handler.headers.get("Authorization") + self.requests.append({"method": method, "path": path, "authorization": authorization, "body": body}) + if method == "GET" and path.startswith("/v1/pools/"): + if self.auth and authorization != f"Bearer {READ_TOKEN}": + handler._reply(401, {"detail": {"code": "UNAUTHORIZED", "message": "Invalid token"}}) + return + spec = self.pools.get(path.removeprefix("/v1/pools/")) + if spec is None: + handler._reply(404, {"detail": {"code": "POOL_NOT_FOUND", "message": "Pool not found"}}) + return + handler._reply(200, {"spec": spec, "status": {"state": "active", "assigned_workers": []}}) + return + if method == "POST" and path.startswith("/v1/pools"): + if self.auth and authorization != f"Bearer {ADMIN_TOKEN}": + handler._reply(403, {"detail": {"code": "FORBIDDEN", "message": "Admin token required"}}) + return + if path == "/v1/pools" and isinstance(body, dict): + spec = { + "name": body["name"], + "queue_pool": body.get("queue_pool", "default"), + "bundle": body.get("bundle"), + "gpus": body.get("gpus", {}), + "ttl_seconds": body.get("ttl_seconds"), + "minimum_worker_count": body.get("minimum_worker_count", 0), + } + self.pools[body["name"]] = spec + handler._reply(201, {"spec": spec, "status": {"state": "pending", "assigned_workers": []}}) + return + handler._reply(200, {"message": "Pool renewed"}) + return + handler._reply(404, {"detail": {"code": "NOT_FOUND", "message": "not found"}}) + + +class FakePrometheus(_Server): + def __init__(self) -> None: + super().__init__() + self.series: dict[str, dict] = { + "sie_gateway_lane_queue_depth": vector((L4, 3), (A100, 0)), + "sie_gateway_pending_demand": vector((L4, 1), (A100, 0)), + "sie_gateway_pool_warm_floor": vector((L4, 1), (A100, 0)), + "sie_worker_gpu_slots": vector(({"lane": "default|l4|default"}, 1)), + } + + def respond(self, handler: _Handler, method: str, body: object) -> None: + url = urlsplit(handler.path) + query = parse_qs(url.query).get("query", [""])[0] + self.requests.append({"path": url.path, "query": query, "authorization": handler.headers.get("Authorization")}) + for metric, document in self.series.items(): + if metric in query: + handler._reply(200, document) + return + handler._reply(200, vector()) + + +def _serve(server: _Server) -> Iterator[_Server]: + thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True) + thread.start() + yield server + server.shutdown() + server.server_close() + + +@pytest.fixture(autouse=True) +def clean_environment(monkeypatch): + for name in ( + "HTTP_PROXY", + "HTTPS_PROXY", + "ALL_PROXY", + "http_proxy", + "https_proxy", + "all_proxy", + "SIE_API_KEY", + "SIE_BASE_URL", + "SIE_ADMIN_TOKEN", + ): + monkeypatch.delenv(name, raising=False) + + +@pytest.fixture +def gateway() -> Iterator[FakeGateway]: + yield from _serve(FakeGateway()) + + +@pytest.fixture +def prometheus() -> Iterator[FakePrometheus]: + yield from _serve(FakePrometheus()) + + +@pytest.fixture +def read_token_file(tmp_path) -> Path: + path = tmp_path / "read-token" + path.write_text(READ_TOKEN + "\n", encoding="utf-8") + return path + + +def tune(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "plan.json") -> int: + return cli.main( + [ + "tune", + "--plan", + str(plan), + "--prometheus-url", + prometheus.url, + "--gateway-url", + gateway.url, + "--once", + *extra, + ] + ) + + +def rows(captured: str) -> list[dict]: + return [json.loads(line) for line in captured.splitlines()] + + +def assert_no_secret(capsys_text: str, caplog) -> None: + for secret in (READ_TOKEN, ADMIN_TOKEN): + assert secret not in capsys_text + assert secret not in caplog.text + + +def test_shadow_mode_records_decisions_and_writes_no_pool(gateway, prometheus, read_token_file, capsys, caplog): + caplog.set_level(logging.DEBUG) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == 0 + captured = capsys.readouterr() + lines = rows(captured.out) + assert [(row["mode"], row["carrier"], row["action"], row["floor"], row["applied"]) for row in lines] == [ + ("shadow", "placement-floor-default-l4-default", "raise_to_floor", 1, False), + ("shadow", "placement-floor-default-a100-80gb-generation", "hold", 0, False), + ] + assert lines[0]["telemetry"] == {"queueDepth": 3, "pendingDemand": 1, "warmFloor": 1, "runners": 1} + assert [request["method"] for request in gateway.requests] == ["GET", "GET"] + assert {request["authorization"] for request in gateway.requests} == {f"Bearer {READ_TOKEN}"} + assert [request["path"] for request in prometheus.requests] == ["/api/v1/query"] * 4 + assert_no_secret(captured.out + captured.err, caplog) + + +def test_shadow_mode_never_reads_the_admin_token(gateway, prometheus, read_token_file, monkeypatch, capsys): + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--admin-token-file", "/nonexistent") == 0 + assert all(request["method"] == "GET" for request in gateway.requests) + assert all(request["authorization"] != f"Bearer {ADMIN_TOKEN}" for request in gateway.requests) + capsys.readouterr() + + +def test_apply_upserts_carriers_with_the_admin_token_only_on_writes( + gateway, prometheus, read_token_file, monkeypatch, capsys, caplog +): + caplog.set_level(logging.DEBUG) + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == 0 + captured = capsys.readouterr() + writes = [request for request in gateway.requests if request["method"] == "POST"] + reads = [request for request in gateway.requests if request["method"] == "GET"] + assert [request["body"] for request in writes] == [ + { + "name": "placement-floor-default-l4-default", + "gpus": {"l4": 0}, + "queue_pool": "default", + "bundle": "default", + "minimum_worker_count": 1, + } + ] + assert {request["authorization"] for request in writes} == {f"Bearer {ADMIN_TOKEN}"} + assert {request["authorization"] for request in reads} == {f"Bearer {READ_TOKEN}"} + assert [(row["mode"], row["applied"]) for row in rows(captured.out)] == [("apply", True), ("apply", False)] + assert_no_secret(captured.out + captured.err, caplog) + + +def test_apply_reads_the_admin_token_from_a_file(gateway, prometheus, read_token_file, tmp_path, capsys): + admin = tmp_path / "admin-token" + admin.write_text(ADMIN_TOKEN, encoding="utf-8") + assert ( + tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply", "--admin-token-file", str(admin)) + == 0 + ) + writes = [request for request in gateway.requests if request["method"] == "POST"] + assert {request["authorization"] for request in writes} == {f"Bearer {ADMIN_TOKEN}"} + capsys.readouterr() + + +def test_apply_renews_an_existing_carrier_and_steers_from_its_floor( + gateway, prometheus, read_token_file, monkeypatch, capsys +): + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + gateway.pools["placement-floor-default-l4-default"] = { + "name": "placement-floor-default-l4-default", + "queue_pool": "default", + "bundle": "default", + "gpus": {"l4": 0}, + "ttl_seconds": None, + "minimum_worker_count": 1, + } + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == 0 + l4_row = rows(capsys.readouterr().out)[0] + assert (l4_row["currentFloor"], l4_row["action"], l4_row["floor"]) == (1, "scale_up", 2) + assert gateway.pools["placement-floor-default-l4-default"]["minimum_worker_count"] == 2 + + +def test_the_client_token_from_the_environment_is_never_sent_on_writes(prometheus, monkeypatch, capsys): + unauthenticated = FakeGateway(auth=False) + for server in _serve(unauthenticated): + monkeypatch.setenv("SIE_BASE_URL", server.url) + monkeypatch.setenv("SIE_API_KEY", READ_TOKEN) + assert tune(server, prometheus, "--apply") == 0 + reads = [request for request in server.requests if request["method"] == "GET"] + writes = [request for request in server.requests if request["method"] == "POST"] + assert {request["authorization"] for request in reads} == {f"Bearer {READ_TOKEN}"} + assert writes + assert {request["authorization"] for request in writes} == {None} + assert "pool writes carry no credential" in capsys.readouterr().err + + +def test_a_rejected_write_fails_the_tick_and_names_the_pool(gateway, prometheus, read_token_file, capsys): + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == 1 + l4_row = rows(capsys.readouterr().out)[0] + assert l4_row["applied"] is False + assert l4_row["error"].startswith("writing pool 'placement-floor-default-l4-default' failed") + assert "Admin token required" in l4_row["error"] + + +def test_an_envelope_above_the_pool_floor_limit_is_refused_before_any_call( + gateway, prometheus, read_token_file, tmp_path, capsys +): + document = json.loads((FIXTURES / "plan.json").read_text(encoding="utf-8")) + document["lanes"][0]["envelope"]["floorMax"] = 5 + plan = tmp_path / "plan.json" + plan.write_text(json.dumps(document), encoding="utf-8") + assert tune(gateway, prometheus, "--apply", plan=plan) == cli.EXIT_REFUSED + assert "floorMax: 5 exceeds the gateway's per-pool floor limit of 4" in capsys.readouterr().err + assert gateway.requests == [] + assert prometheus.requests == [] + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--max-pool-floor", "5", plan=plan) == 0 + capsys.readouterr() + + +def test_more_lanes_than_the_pool_limit_are_refused_before_any_call(gateway, prometheus, capsys): + assert tune(gateway, prometheus, "--apply", "--max-pools", "1") == cli.EXIT_REFUSED + assert "above the gateway's limit of 1 API pools" in capsys.readouterr().err + assert gateway.requests == [] + assert prometheus.requests == [] + + +@pytest.mark.parametrize( + ("extra", "message"), + [ + (("--api-key-file", "/nonexistent/token"), "cannot read the gateway client token file"), + (("--apply", "--admin-token-file", "/nonexistent/token"), "cannot read the gateway admin token file"), + (("--series-selector", 'a="b"}'), "without braces"), + ], +) +def test_bad_inputs_are_refused_before_any_call(gateway, prometheus, extra, message, capsys): + assert tune(gateway, prometheus, *extra) == cli.EXIT_REFUSED + assert message in capsys.readouterr().err + assert gateway.requests == [] + + +def test_an_empty_token_file_is_refused(gateway, prometheus, tmp_path, capsys): + empty = tmp_path / "token" + empty.write_text("\n", encoding="utf-8") + assert tune(gateway, prometheus, "--api-key-file", str(empty)) == cli.EXIT_REFUSED + assert "is empty" in capsys.readouterr().err + + +def test_a_plan_that_is_not_a_plan_is_refused(gateway, prometheus, tmp_path, capsys): + plan = tmp_path / "plan.json" + plan.write_text("{}", encoding="utf-8") + assert tune(gateway, prometheus, plan=plan) == cli.EXIT_REFUSED + assert "$.kind: is required" in capsys.readouterr().err + plan.write_text("{", encoding="utf-8") + assert tune(gateway, prometheus, plan=plan) == cli.EXIT_REFUSED + assert "is not JSON" in capsys.readouterr().err + + +def test_an_unreachable_prometheus_holds_every_lane_and_fails_the_tick(gateway, read_token_file, capsys): + unreachable = FakePrometheus() + unreachable.server_close() + assert tune(gateway, unreachable, "--api-key-file", str(read_token_file)) == 1 + captured = capsys.readouterr() + assert [row["action"] for row in rows(captured.out)] == ["raise_to_floor", "hold"] + assert "Prometheus series queueDepth: the query failed" in captured.err + + +def test_the_core_imports_without_the_sdk_and_tune_names_the_missing_extra(gateway, prometheus, monkeypatch, capsys): + monkeypatch.setitem(sys.modules, "sie_sdk", None) + monkeypatch.delitem(sys.modules, "sie_placement.pools", raising=False) + monkeypatch.delattr(sys.modules["sie_placement"], "pools", raising=False) + assert tune(gateway, prometheus) == cli.EXIT_REFUSED + assert "needs the sie-placement keda extra" in capsys.readouterr().err + assert gateway.requests == [] diff --git a/packages/sie_placement/tests/test_keda.py b/packages/sie_placement/tests/test_keda.py new file mode 100644 index 000000000..5e80308b1 --- /dev/null +++ b/packages/sie_placement/tests/test_keda.py @@ -0,0 +1,577 @@ +from __future__ import annotations + +import copy +import itertools +import json +import threading +from collections.abc import Iterator, Mapping, Sequence +from datetime import UTC, datetime +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from urllib.parse import parse_qs, urlsplit + +import pytest +from sie_placement import keda +from sie_placement.schema import Plan +from sie_placement.tuner import Action + +FIXTURES = Path(__file__).parent / "fixtures" +AT = datetime(2026, 1, 1, 12, 0, tzinfo=UTC) + +QUEUE_METRIC = "sie_gateway_lane_queue_depth" +PENDING_METRIC = "sie_gateway_pending_demand" +FLOOR_METRIC = "sie_gateway_pool_warm_floor" +SLOTS_METRIC = "sie_worker_gpu_slots" + + +def plan_document() -> dict: + return json.loads((FIXTURES / "plan.json").read_text(encoding="utf-8")) + + +def make_plan(*lanes: dict) -> Plan: + document = plan_document() + template = document["lanes"][0] + document["lanes"] = [] + document["identities"] = [] + for index, overrides in enumerate(lanes): + lane = copy.deepcopy(template) + lane["name"] = f"lane-{index}" + envelope = overrides.pop("envelope", None) + machine_profile = overrides.pop("machineProfile", None) + lane.update(overrides) + lane["envelope"] = envelope or {"floorMin": 0, "floorMax": 3} + if machine_profile is not None: + lane["cell"]["machineProfile"] = machine_profile + document["lanes"].append(lane) + warmth = "pinned" if lane["envelope"]["floorMin"] >= 1 else "cold-with-bound" + document["identities"].append({"identity": f"model-{index}", "lane": lane["name"], "warmth": warmth}) + return Plan.from_dict(document) + + +def one_lane(floor_min: int = 0, floor_max: int = 3) -> tuple[keda.Carrier, ...]: + return keda.carriers(make_plan({"envelope": {"floorMin": floor_min, "floorMax": floor_max}})) + + +def lane_labels(queue_pool: str = "default", machine_profile: str = "l4", bundle: str = "default") -> dict[str, str]: + return {"pool": queue_pool, "machine_profile": machine_profile, "bundle": bundle} + + +class FakePrometheus: + def __init__(self, series: Mapping[str, Sequence[tuple[dict[str, str], float]]] | None = None) -> None: + self.series = dict(series or {}) + self.failing: set[str] = set() + self.queries: list[str] = [] + + @classmethod + def lane( + cls, + *, + queue_depth: int = 0, + pending: int = 0, + warm_floor: int = 0, + runners: int = 0, + labels: dict | None = None, + ) -> FakePrometheus: + labels = labels or lane_labels() + otel = f"{labels['pool']}|{labels['machine_profile']}|{labels['bundle']}" + series: dict[str, list[tuple[dict[str, str], float]]] = { + QUEUE_METRIC: [(labels, queue_depth)], + PENDING_METRIC: [(labels, pending)], + FLOOR_METRIC: [(labels, warm_floor)], + SLOTS_METRIC: [({"lane": otel}, runners)] if runners else [], + } + return cls(series) + + def query(self, promql: str) -> Sequence[keda.Sample]: + self.queries.append(promql) + for metric, samples in self.series.items(): + if metric in promql: + if metric in self.failing: + raise keda.PrometheusError("the query failed with HTTP 503") + return tuple(keda.Sample(labels=labels, value=value) for labels, value in samples) + for metric in self.failing: + if metric in promql: + raise keda.PrometheusError("the query failed with HTTP 503") + return () + + +class FakePools: + def __init__(self, pools: Mapping[str, dict] | None = None) -> None: + self.pools: dict[str, dict] = {name: copy.deepcopy(spec) for name, spec in (pools or {}).items()} + self.raw: dict[str, dict] = {} + self.reads: list[str] = [] + self.writes: list[tuple[tuple, dict]] = [] + self.fail_reads = False + self.fail_writes = False + + def get_pool(self, name: str) -> Mapping[str, object] | None: + self.reads.append(name) + if self.fail_reads: + raise keda.GatewayError("connection refused") + if name in self.raw: + return self.raw[name] + spec = self.pools.get(name) + return None if spec is None else {"name": name, "spec": copy.deepcopy(spec), "status": {"state": "active"}} + + def create_pool( + self, name: str, gpus: dict[str, int], *, bundle: str, minimum_worker_count: int, queue_pool: str + ) -> None: + self.writes.append( + ((name, gpus), {"bundle": bundle, "minimum_worker_count": minimum_worker_count, "queue_pool": queue_pool}) + ) + if self.fail_writes: + raise keda.GatewayError("HTTP 403") + self.pools[name] = { + "name": name, + "queue_pool": queue_pool, + "bundle": bundle, + "gpus": dict(gpus), + "ttl_seconds": None, + "minimum_worker_count": minimum_worker_count, + } + + +def carrier_spec(floor: int, *, machine_profile: str = "l4", bundle: str = "default", **extra: object) -> dict: + return { + "name": f"placement-floor-default-{machine_profile}-{bundle}", + "queue_pool": "default", + "bundle": bundle, + "gpus": {machine_profile: 0}, + "ttl_seconds": None, + "minimum_worker_count": floor, + **extra, + } + + +def run_tick( + lanes: Sequence[keda.Carrier], + prometheus: FakePrometheus, + reader: FakePools, + writer: FakePools | None = None, + selector: str = "", +) -> keda.Tick: + return keda.tick(lanes, prometheus=prometheus, reader=reader, writer=writer, selector=selector, now=lambda: AT) + + +def test_each_fixture_lane_gets_one_floor_carrier(): + lanes = keda.carriers(Plan.from_dict(plan_document())) + assert [carrier.name for carrier in lanes] == [ + "placement-floor-default-l4-default", + "placement-floor-default-a100-80gb-generation", + ] + assert [carrier.gpus for carrier in lanes] == [{"l4": 0}, {"a100-80gb": 0}] + assert lanes[0].key.otel_lane == "default|l4|default" + + +def test_lane_tokens_are_canonicalized_the_way_the_chart_names_lanes(): + lanes = keda.carriers(make_plan({"queuePool": "Default", "machineProfile": "L4-Spot", "bundle": "Default"})) + assert lanes[0].key == keda.LaneKey(queue_pool="default", machine_profile="l4-spot", bundle="default") + assert lanes[0].name == "placement-floor-default-l4-spot-default" + + +def test_an_envelope_above_the_gateway_per_pool_floor_limit_is_refused(): + plan = make_plan({"envelope": {"floorMin": 0, "floorMax": 5}, "maxContainers": 6}) + with pytest.raises(keda.PlanRefusedError) as raised: + keda.carriers(plan) + assert raised.value.problems == ("$.lanes[0].envelope.floorMax: 5 exceeds the gateway's per-pool floor limit of 4",) + assert keda.carriers(plan, max_pool_floor=5)[0].lane.envelope.floor_max == 5 + + +def test_more_lanes_than_the_gateway_pool_limit_are_refused(): + plan = make_plan({"bundle": "a"}, {"bundle": "b"}, {"bundle": "c"}) + with pytest.raises(keda.PlanRefusedError, match="3 lanes need as many floor-carrier pools"): + keda.carriers(plan, max_pools=2) + assert len(keda.carriers(plan, max_pools=3)) == 3 + + +def test_two_lanes_that_need_one_carrier_are_refused(): + plan = make_plan({"bundle": "default"}, {"bundle": "DEFAULT"}) + with pytest.raises(keda.PlanRefusedError, match=r"\$\.lanes\[1\]: needs the same floor-carrier pool"): + keda.carriers(plan) + hyphens = make_plan({"queuePool": "a-b", "machineProfile": "c"}, {"queuePool": "a", "machineProfile": "b-c"}) + with pytest.raises(keda.PlanRefusedError, match="placement-floor-a-b-c-default"): + keda.carriers(hyphens) + + +@pytest.mark.parametrize("token", ["l4.x", "l4/x", "x" * 64, 'l4"']) +def test_a_lane_token_the_gateway_would_reject_is_refused(token): + with pytest.raises(keda.PlanRefusedError, match="must each be 1 to 63 letters"): + keda.carriers(make_plan({"machineProfile": token})) + + +def test_a_carrier_name_longer_than_the_gateway_accepts_is_refused(): + plan = make_plan({"queuePool": "q" * 40, "machineProfile": "m" * 40, "bundle": "b" * 40}) + with pytest.raises(keda.PlanRefusedError, match="longer than 128 characters"): + keda.carriers(plan) + + +def test_the_series_queries_read_each_lane_from_the_gateway_and_worker_series(): + assert keda.series_queries() == { + "queueDepth": "max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{})", + "pendingDemand": "max by (pool, machine_profile, bundle) (sie_gateway_pending_demand{})", + "warmFloor": "max by (pool, machine_profile, bundle) (sie_gateway_pool_warm_floor{})", + "runners": ('count by (lane) (max by (lane, producer_instance) (sie_worker_gpu_slots{state="ready"}) > 0)'), + } + scoped = keda.series_queries('namespace="sie"') + assert scoped["queueDepth"].endswith('(sie_gateway_lane_queue_depth{namespace="sie"})') + assert scoped["runners"].endswith('(sie_worker_gpu_slots{namespace="sie",state="ready"}) > 0)') + + +@pytest.mark.parametrize("selector", ['namespace="sie"}', '{namespace="sie"', 'a="b"\nc="d"']) +def test_a_selector_that_could_leave_its_braces_is_rejected(selector): + with pytest.raises(ValueError, match="without braces or line breaks"): + keda.series_queries(selector) + + +def test_parse_vector_reads_an_instant_vector(): + document = { + "status": "success", + "data": {"resultType": "vector", "result": [{"metric": {"pool": "default"}, "value": [1.0, "3"]}]}, + } + assert keda.parse_vector(document) == (keda.Sample(labels={"pool": "default"}, value=3.0),) + + +@pytest.mark.parametrize( + "document", + [ + {"status": "error", "error": "bad query"}, + {"status": "success", "data": {"resultType": "matrix", "result": []}}, + {"status": "success", "data": {"resultType": "vector", "result": {}}}, + {"status": "success", "data": {"resultType": "vector", "result": [{"metric": {}}]}}, + {"status": "success", "data": {"resultType": "vector", "result": [{"metric": {}, "value": [1, "x"]}]}}, + [], + ], +) +def test_parse_vector_rejects_anything_else(document): + with pytest.raises(keda.PrometheusError): + keda.parse_vector(document) + + +class _PrometheusHandler(BaseHTTPRequestHandler): + server: _PrometheusServer + + def do_GET(self) -> None: + url = urlsplit(self.path) + self.server.requests.append((url.path, parse_qs(url.query))) + status, body = self.server.reply + payload = json.dumps(body).encode() + self.send_response(status) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(payload))) + self.end_headers() + self.wfile.write(payload) + + def log_message(self, format: str, *args: object) -> None: + return + + +class _PrometheusServer(ThreadingHTTPServer): + def __init__(self) -> None: + super().__init__(("127.0.0.1", 0), _PrometheusHandler) + self.requests: list[tuple[str, dict]] = [] + self.reply: tuple[int, object] = (200, {"status": "success", "data": {"resultType": "vector", "result": []}}) + + @property + def url(self) -> str: + return f"http://127.0.0.1:{self.server_address[1]}" + + +@pytest.fixture +def prometheus_server(monkeypatch) -> Iterator[_PrometheusServer]: + for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + monkeypatch.delenv(name, raising=False) + server = _PrometheusServer() + thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True) + thread.start() + yield server + server.shutdown() + server.server_close() + + +def test_the_http_reader_sends_the_query_to_the_query_api(prometheus_server): + prometheus_server.reply = ( + 200, + {"status": "success", "data": {"resultType": "vector", "result": [{"metric": {}, "value": [1, "2"]}]}}, + ) + reader = keda.PrometheusHttp(prometheus_server.url + "/") + assert reader.query('max(up{job="x"})') == (keda.Sample(labels={}, value=2.0),) + assert prometheus_server.requests == [("/api/v1/query", {"query": ['max(up{job="x"})']})] + + +def test_the_http_reader_reports_a_failed_query(prometheus_server): + prometheus_server.reply = (400, {"status": "error", "error": "parse error"}) + with pytest.raises(keda.PrometheusError, match="HTTP 400"): + keda.PrometheusHttp(prometheus_server.url).query("up{") + + +def test_the_http_reader_reports_an_unreachable_prometheus(): + with pytest.raises(keda.PrometheusError, match="the query failed"): + keda.PrometheusHttp("http://127.0.0.1:9", timeout_s=2).query("up") + + +@pytest.mark.parametrize( + "url", + ["file:///etc/passwd", "ftp://prometheus", "prometheus:9090", "http://user:secret@prometheus", "http://p/?x=1"], +) +def test_the_http_reader_accepts_only_a_plain_http_base_url(url): + with pytest.raises(ValueError, match="Prometheus URL"): + keda.PrometheusHttp(url) + + +def test_shadow_mode_decides_every_lane_and_writes_nothing(): + lanes = keda.carriers(Plan.from_dict(plan_document())) + prometheus = FakePrometheus.lane(queue_depth=4, pending=1, warm_floor=1, runners=1) + reader = FakePools({"placement-floor-default-l4-default": carrier_spec(1)}) + result = run_tick(lanes, prometheus, reader) + assert result.apply is False + assert reader.writes == [] + assert reader.reads == [carrier.name for carrier in lanes] + first, second = result.records + assert (first.current_floor, first.step.action, first.step.settings.floor, first.applied) == ( + 1, + Action.SCALE_UP, + 2, + False, + ) + assert (second.current_floor, second.step.action, second.step.settings.floor) == (0, Action.HOLD, 0) + assert second.telemetry == keda.LaneTelemetry(queue_depth=None, pending_demand=None, warm_floor=None, runners=0) + + +def test_apply_upserts_one_unpinned_carrier_per_lane_through_the_pool_api(): + lanes = one_lane(1, 3) + pools = FakePools() + result = run_tick(lanes, FakePrometheus.lane(), pools, pools) + assert pools.writes == [ + ( + ("placement-floor-default-l4-default", {"l4": 0}), + {"bundle": "default", "minimum_worker_count": 1, "queue_pool": "default"}, + ) + ] + assert result.records[0].step.action is Action.RAISE_TO_FLOOR + assert result.records[0].applied is True + + +def test_apply_renews_the_carrier_lease_on_every_tick(): + lanes = one_lane(1, 3) + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(2)}) + busy = FakePrometheus.lane(queue_depth=3, runners=2) + for _ in range(3): + result = run_tick(lanes, busy, pools, pools) + assert result.records[0].step.action is Action.HOLD + assert [kwargs["minimum_worker_count"] for _, kwargs in pools.writes] == [2, 2, 2] + + +def test_a_lane_without_a_carrier_that_stays_at_zero_gets_no_carrier(): + pools = FakePools() + result = run_tick(one_lane(0, 3), FakePrometheus.lane(), pools, pools) + assert pools.writes == [] + assert result.records[0].step.action is Action.HOLD + assert result.records[0].applied is False + + +def test_an_existing_carrier_at_zero_is_still_renewed(): + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(0)}) + run_tick(one_lane(0, 3), FakePrometheus.lane(), pools, pools) + assert [kwargs["minimum_worker_count"] for _, kwargs in pools.writes] == [0] + + +@pytest.mark.parametrize(("floor_min", "floor_max"), [(0, 0), (0, 3), (1, 1), (1, 4), (2, 3)]) +def test_the_written_floor_never_leaves_the_envelope(floor_min, floor_max): + lanes = one_lane(floor_min, floor_max) + for current, queue_depth, pending, runners in itertools.product(range(7), (0, 3), (0, 1), (0, 1, 3, 6)): + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(current)}) + prometheus = FakePrometheus.lane(queue_depth=queue_depth, pending=pending, runners=runners) + record = run_tick(lanes, prometheus, pools, pools).records[0] + (_, kwargs) = pools.writes[-1] + assert floor_min <= kwargs["minimum_worker_count"] <= floor_max + assert kwargs["minimum_worker_count"] == record.step.settings.floor + assert record.current_floor == current + + +def test_pending_demand_raises_the_floor_and_an_idle_lane_lowers_it(): + lanes = one_lane(0, 3) + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(1)}) + waiting = FakePrometheus.lane(queue_depth=2, pending=1, runners=1) + serving = FakePrometheus.lane(queue_depth=2, pending=0, runners=1) + idle = FakePrometheus.lane(queue_depth=0, pending=0, runners=1) + assert run_tick(lanes, waiting, pools, pools).records[0].step.action is Action.SCALE_UP + assert pools.pools["placement-floor-default-l4-default"]["minimum_worker_count"] == 2 + assert run_tick(lanes, serving, pools, pools).records[0].step.action is Action.HOLD + assert run_tick(lanes, idle, pools, pools).records[0].step.action is Action.SCALE_DOWN + assert pools.pools["placement-floor-default-l4-default"]["minimum_worker_count"] == 1 + + +def test_a_floor_whose_workers_have_not_started_is_not_raised_again(): + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(2)}) + booting = FakePrometheus.lane(queue_depth=5, pending=1, runners=1) + record = run_tick(one_lane(0, 3), booting, pools, pools).records[0] + assert (record.step.action, record.step.settings.floor) == (Action.HOLD, 2) + + +def test_current_is_read_from_the_gateway_on_every_tick(): + lanes = one_lane(0, 3) + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(1)}) + waiting = FakePrometheus.lane(queue_depth=2, pending=1, runners=3) + assert run_tick(lanes, waiting, pools, pools).records[0].step.settings.floor == 2 + pools.pools["placement-floor-default-l4-default"]["minimum_worker_count"] = 0 + record = run_tick(lanes, waiting, pools, pools).records[0] + assert (record.current_floor, record.step.settings.floor) == (0, 1) + + +def test_a_floor_held_above_the_envelope_by_another_pool_is_reported_and_left_alone(): + lanes = one_lane(1, 3) + other = { + "name": "team-pool", + "queue_pool": "default", + "bundle": "default", + "gpus": {"l4": 1}, + "minimum_worker_count": 4, + } + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(1), "team-pool": other}) + record = run_tick(lanes, FakePrometheus.lane(warm_floor=4, runners=4), pools, pools).records[0] + assert record.lane_floor_above_envelope is True + assert record.to_dict()["laneFloorAboveEnvelope"] is True + assert [args[0] for args, _ in pools.writes] == ["placement-floor-default-l4-default"] + assert pools.pools["team-pool"] == other + assert ( + run_tick(lanes, FakePrometheus.lane(warm_floor=3), pools, pools).records[0].lane_floor_above_envelope is False + ) + + +def test_a_carrier_above_a_lowered_envelope_is_brought_down_to_the_cap(): + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(4)}) + record = run_tick(one_lane(0, 2), FakePrometheus.lane(warm_floor=4, runners=4), pools, pools).records[0] + assert (record.step.action, record.step.settings.floor) == (Action.HOLD_AT_CAP, 2) + assert pools.pools["placement-floor-default-l4-default"]["minimum_worker_count"] == 2 + + +@pytest.mark.parametrize( + ("change", "reason"), + [ + ({"pinned_models": ["BAAI/bge-m3"]}, "it sets pinned_models"), + ({"gpu_caps": {"l4": 2}}, "it sets gpu_caps"), + ({"ttl_seconds": 3600}, "it sets ttl_seconds"), + ({"gpus": {"l4": 1}}, "its gpus are not"), + ({"gpus": {"l4": 0, "a100": 0}}, "its gpus are not"), + ({"bundle": "other"}, "bundle 'default'"), + ({"bundle": None}, "bundle 'default'"), + ({"queue_pool": "dedicated"}, "queue pool 'default'"), + ({"minimum_worker_count": -1}, "minimum_worker_count"), + ({"minimum_worker_count": True}, "minimum_worker_count"), + ], +) +def test_a_pool_that_is_not_the_lane_carrier_is_never_written(change, reason): + spec = carrier_spec(2) | change + pools = FakePools({"placement-floor-default-l4-default": spec}) + record = run_tick(one_lane(0, 3), FakePrometheus.lane(pending=1, runners=2), pools, pools).records[0] + assert pools.writes == [] + assert record.step is None + assert reason in (record.error or "") + assert "is not this lane's floor carrier" in (record.error or "") + + +def test_a_pool_without_a_spec_is_never_written(): + pools = FakePools() + pools.raw["placement-floor-default-l4-default"] = {"name": "placement-floor-default-l4-default"} + record = run_tick(one_lane(0, 3), FakePrometheus.lane(), pools, pools).records[0] + assert record.error is not None + assert pools.writes == [] + + +def test_missing_telemetry_holds_the_lane_and_only_brings_it_inside_the_envelope(): + lanes = one_lane(1, 3) + prometheus = FakePrometheus.lane(pending=1, runners=3) + prometheus.failing.add(QUEUE_METRIC) + for current, expected, action in ((0, 1, Action.RAISE_TO_FLOOR), (2, 2, Action.HOLD), (5, 3, Action.HOLD_AT_CAP)): + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(current)}) + result = run_tick(lanes, prometheus, pools, pools) + record = result.records[0] + assert (record.step.action, record.step.settings.floor) == (action, expected) + assert record.telemetry.queue_depth is None + assert result.series_errors == ("queueDepth: the query failed with HTTP 503",) + assert result.failed is True + + +def test_a_lane_the_gateway_does_not_report_is_held(): + prometheus = FakePrometheus.lane(pending=1, runners=1, labels=lane_labels(bundle="other")) + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(1)}) + result = run_tick(one_lane(0, 3), prometheus, pools, pools) + record = result.records[0] + assert (record.step.action, record.step.settings.floor) == (Action.HOLD, 1) + assert result.failed is False + + +def test_a_lane_with_no_ready_worker_reports_zero_runners(): + prometheus = FakePrometheus.lane(pending=1) + record = run_tick(one_lane(0, 3), prometheus, FakePools(), FakePools()).records[0] + assert record.telemetry.runners == 0 + assert (record.step.action, record.step.settings.floor) == (Action.SCALE_UP, 1) + + +def test_samples_are_matched_to_lanes_by_canonical_labels(): + labels = {"pool": "Default", "machine_profile": "L4", "bundle": "Default"} + prometheus = FakePrometheus( + { + QUEUE_METRIC: [(labels, 2), (lane_labels(), 5), ({"pool": "default"}, 9)], + PENDING_METRIC: [(labels, 0), (lane_labels(), float("nan"))], + FLOOR_METRIC: [(labels, -1)], + SLOTS_METRIC: [({"lane": "Default|L4|Default"}, 2), ({"lane": "default|l4"}, 7)], + } + ) + record = run_tick(one_lane(0, 3), prometheus, FakePools()).records[0] + assert record.telemetry == keda.LaneTelemetry(queue_depth=5, pending_demand=0, warm_floor=None, runners=2) + + +def test_a_gateway_read_failure_skips_only_that_lane(): + lanes = keda.carriers(make_plan({"bundle": "a"}, {"bundle": "b"})) + reader = FakePools() + reader.fail_reads = True + writer = FakePools() + result = run_tick(lanes, FakePrometheus.lane(), reader, writer) + assert writer.writes == [] + assert [record.error for record in result.records] == [ + "reading pool 'placement-floor-default-l4-a' failed: connection refused", + "reading pool 'placement-floor-default-l4-b' failed: connection refused", + ] + assert result.failed is True + + +def test_a_gateway_write_failure_is_recorded(): + pools = FakePools() + pools.fail_writes = True + record = run_tick(one_lane(1, 3), FakePrometheus.lane(), pools, pools).records[0] + assert record.applied is False + assert record.step.settings.floor == 1 + assert record.error == "writing pool 'placement-floor-default-l4-default' failed: HTTP 403" + + +def test_audit_rows_carry_the_tick_time_mode_and_decision(): + pools = FakePools({"placement-floor-default-l4-default": carrier_spec(1)}) + prometheus = FakePrometheus.lane(queue_depth=4, pending=1, warm_floor=1, runners=1) + rows = run_tick(one_lane(1, 3), prometheus, pools, pools).audit_rows() + assert rows == [ + { + "at": "2026-01-01T12:00:00Z", + "mode": "apply", + "lane": "lane-0", + "queuePool": "default", + "machineProfile": "l4", + "bundle": "default", + "carrier": "placement-floor-default-l4-default", + "telemetry": {"queueDepth": 4, "pendingDemand": 1, "warmFloor": 1, "runners": 1}, + "currentFloor": 1, + "action": "scale_up", + "floor": 2, + "applied": True, + "laneFloorAboveEnvelope": False, + "error": None, + } + ] + assert json.loads(json.dumps(rows)) == rows + + +def test_the_selector_scopes_every_series_query(): + prometheus = FakePrometheus.lane() + run_tick(one_lane(), prometheus, FakePools(), selector='namespace="sie"') + assert len(prometheus.queries) == 4 + assert all('namespace="sie"' in query for query in prometheus.queries) diff --git a/uv.lock b/uv.lock index d07c8964a..0fc3486a8 100644 --- a/uv.lock +++ b/uv.lock @@ -4766,12 +4766,22 @@ name = "sie-placement" version = "0.0.0" source = { editable = "packages/sie_placement" } +[package.optional-dependencies] +keda = [ + { name = "httpx" }, + { name = "sie-sdk" }, +] + [package.dev-dependencies] dev = [ { name = "pytest" }, ] [package.metadata] +requires-dist = [ + { name = "httpx", marker = "extra == 'keda'", specifier = ">=0.28,<1" }, + { name = "sie-sdk", marker = "extra == 'keda'", editable = "packages/sie_sdk" }, +] [package.metadata.requires-dev] dev = [{ name = "pytest", specifier = ">=9.0.3" }] From d99d236cb35cf098a50ace102e559ff9625049cd Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Thu, 5 Nov 2026 03:46:49 +0100 Subject: [PATCH 11/22] fix(placement): harden the KEDA applier's tokens, series and leases - refuse a token file, SIE_ADMIN_TOKEN or SIE_API_KEY that is not one run of printable ASCII without spaces, naming its source and never its value, and report a gateway transport failure by its exception type only, so no error text carries an Authorization header value - map malformed Prometheus replies (a bad status line, a truncated body) to a failed series, so the tick holds its lanes and the loop keeps running, and refuse a Prometheus URL with an invalid port at startup - join each gateway series to producers whose snapshot is at most 20 seconds old, as the chart's KEDA triggers do: the lane's queue snapshot for queue depth, the capacity snapshot for pending demand and warm floor, so a stale producer's last point reads as missing - write each carrier through its own SDK client and close it after the write, so the SDK's background lease renewal never runs and a carrier whose reads fail or whose name another pool holds lapses after its last write - report a malformed pool reply from the gateway as a failed lane instead of ending the loop - document that pending demand is a 0 or 1 flag that a publish in flight also sets, so steady traffic can read as pressure, and that the worker series has no freshness timestamp --- packages/sie_placement/README.md | 79 +++++--- .../sie_placement/src/sie_placement/cli.py | 22 ++- .../sie_placement/src/sie_placement/keda.py | 26 ++- .../sie_placement/src/sie_placement/pools.py | 39 +++- packages/sie_placement/tests/test_cli.py | 182 ++++++++++++++++-- packages/sie_placement/tests/test_keda.py | 92 ++++++++- 6 files changed, 379 insertions(+), 61 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 873dd8c19..3ff3dfab8 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -154,8 +154,10 @@ floor as `current`. It prints one JSON audit row per lane to standard output. Wi `--apply` it runs in shadow mode: it decides and records and writes nothing. With `--apply` it upserts every lane's carrier on every tick through `SIEClient.create_pool`, which renews the carrier's lease and sets its floor to -the step's floor. A lane that has no carrier and whose floor stays at 0 gets -none. +the step's floor. Each write uses its own SDK client and closes it after the +write, so the SDK's background lease renewal never runs: a carrier's lease is +renewed only by a tick that writes it. A lane that has no carrier and whose +floor stays at 0 gets none. Under KEDA only the floor moves per lane. The scale-down window and the queue threshold are chart-wide settings, and there is no buffer to tune, so the @@ -176,37 +178,57 @@ reported and left unchanged. A carrier carries no pins, because every floor change is a spec change, which resets the pool's worker assignment; pinned models stay in the chart values. -When the applier stops renewing, each carrier's lease lapses, 1200 seconds by -default, the gateway removes it, and the lane returns to its chart floor. On a -lane whose chart `minReplicas` is 0, a floor of 1 or more activates the lane, -which then holds at least the chart's fallback floor. +A tick that cannot read a lane's carrier, or finds a pool with its name that is +not its carrier, does not write it. A carrier that no tick writes, because those +reads keep failing or the applier has stopped, lapses 1200 seconds after its +last write by default, the gateway removes it, and the lane returns to its +chart floor. On a lane whose chart `minReplicas` is 0, a floor of 1 or more +activates the lane, which then holds at least the chart's fallback floor. ### Series | Series | Query | Step input | | --- | --- | --- | -| Queue depth | `max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{})` | `running` | -| Pending demand | `max by (pool, machine_profile, bundle) (sie_gateway_pending_demand{})` | `backlog` | -| Warm floor | `max by (pool, machine_profile, bundle) (sie_gateway_pool_warm_floor{})` | Reported only | +| Queue depth | `max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{} and on(producer_instance, collector_generation, pool, machine_profile, bundle) (abs(time() - sie_gateway_lane_queue_snapshot_timestamp_seconds{}) < 20))` | `running` | +| Pending demand | `max by (pool, machine_profile, bundle) (sie_gateway_pending_demand{} and on(producer_instance, collector_generation) (abs(time() - sie_gateway_capacity_snapshot_timestamp_seconds{}) < 20))` | `backlog` | +| Warm floor | `max by (pool, machine_profile, bundle) (sie_gateway_pool_warm_floor{} and on(producer_instance, collector_generation) (abs(time() - sie_gateway_capacity_snapshot_timestamp_seconds{}) < 20))` | Reported only | | Running workers | `count by (lane) (max by (lane, producer_instance) (sie_worker_gpu_slots{state="ready"}) > 0)` | `runners` | -`--series-selector` adds label matchers to every selector, for example -`namespace="sie"` when one Prometheus scrapes several releases. Lane labels -match the plan's lane tokens after trimming and lowering them, as the chart -names lanes. The attainment is `unmeasured`, so a lane is under pressure while -the gateway reports pending demand for it, which it does when a request found no -ready capacity, and idle when it reports neither pending demand nor queued or -in-flight work. A lane with no ready worker has 0 runners. A lane whose queue -depth, pending demand or worker series is missing is held: the step is given a -busy lane with nothing waiting, so it only brings the carrier's floor inside the -envelope. +`--series-selector` adds label matchers to every selector, timestamps included, +for example `namespace="sie"` when one Prometheus scrapes several releases. +Lane labels match the plan's lane tokens after trimming and lowering them, as +the chart names lanes. + +The collector keeps a producer's last point for minutes after the producer +stops reporting, so each gateway series keeps only the producers whose snapshot +is at most 20 seconds old, as the chart's KEDA triggers do: the lane's own +queue snapshot for queue depth, and the producer's capacity snapshot for +pending demand and warm floor. A lane whose gateway values are all stale reads +as missing. The worker series has no snapshot timestamp, so a worker that +stopped reporting still counts among the runners until the collector drops it. + +Pending demand is a 0 or 1 flag per lane, not a count. The gateway sets it +while a request that found no ready capacity is recent, while any request's +durable publish to the lane awaits its acknowledgement, and for 120 seconds +after a failed dispatch. The attainment is `unmeasured`, so a lane is under +pressure whenever the flag is set, and idle when neither the flag nor queued or +in-flight work is reported. Because a publish in flight sets the flag, any +traffic can read as pressure even when the lane has capacity to spare, and a +lane that serves steady traffic seldom reads idle, so its floor can climb +towards `floorMax` while traffic lasts and falls one step per idle tick +afterwards. Run the applier in shadow mode first to see how often each lane +reads as under pressure. A lane with no +ready worker has 0 runners. A lane whose queue depth, pending demand or worker +series is missing is held: the step is given a busy lane with nothing waiting, +so it only brings the carrier's floor inside the envelope. ### Audit rows Each row carries `at`, `mode` (`shadow` or `apply`), the lane's `lane`, `queuePool`, `machineProfile` and `bundle`, the `carrier` pool name, the -`telemetry` read for the lane, `currentFloor`, the step's `action` and `floor`, -whether the write was `applied`, `laneFloorAboveEnvelope` and `error`. +`telemetry` read for the lane (`pendingDemand` is the 0 or 1 flag), +`currentFloor`, the step's `action` and `floor`, whether the write was +`applied`, `laneFloorAboveEnvelope` and `error`. `laneFloorAboveEnvelope` is true when the lane's warm floor exceeds `floorMax`. The applier does not lower another pool's floor, so a lane held above its envelope by another pool stays reported until that pool changes. @@ -221,7 +243,13 @@ The command exits with status 2 before it calls Prometheus or the gateway when: `SIE_GATEWAY_MAX_POOLS` (default 64); - a lane's `queuePool`, machine profile or bundle is not 1 to 63 letters, digits, `_` or `-`, two lanes need the same carrier, or a carrier name is - longer than the gateway's 128 characters. + longer than the gateway's 128 characters; +- a token file or `SIE_ADMIN_TOKEN` holds anything but one run of printable + ASCII characters without spaces after trimming, or the Prometheus URL has an + invalid port. + +A failed query, pool read or pool write fails only its tick, which is recorded, +and the next tick runs as usual. With `--once`, a tick whose query, pool read or pool write failed exits with status 1. @@ -232,13 +260,16 @@ status 1. | --- | --- | --- | | Prometheus `/api/v1/query` | Read access to the query API | None sent | | `GET /v1/pools/{name}` | A gateway client token, when gateway token auth is on | `--api-key-file`, or `SIE_API_KEY` when `SIE_BASE_URL` names the same origin, as the SDK resolves it | -| `POST /v1/pools` and its lease renewal | The gateway admin token | `--admin-token-file`, or `SIE_ADMIN_TOKEN`, read only with `--apply` | +| `POST /v1/pools`, which also renews the lease | The gateway admin token | `--admin-token-file`, or `SIE_ADMIN_TOKEN`, read only with `--apply` | The gateway admin token authorizes every gateway admin route, not only pools. The applier reads it only with `--apply`, sends it only on pool writes to `--gateway-url`, never takes it as a command-line value and never prints or logs it. Without it, `--apply` sends its writes with no credential, which a -gateway with token auth refuses. The client token is never sent on writes. +gateway with token auth refuses. The client token is never sent on writes. A +refused token is named by its source, never by its value, and a failed gateway +call is reported by the server's error message or, for a transport failure, by +the exception type alone, so no reported error carries a header value. ## Tests diff --git a/packages/sie_placement/src/sie_placement/cli.py b/packages/sie_placement/src/sie_placement/cli.py index 8f653dc5b..03eb2f80a 100644 --- a/packages/sie_placement/src/sie_placement/cli.py +++ b/packages/sie_placement/src/sie_placement/cli.py @@ -6,6 +6,7 @@ import json import math import os +import re import sys import time from collections.abc import Callable, Sequence @@ -15,10 +16,13 @@ from sie_placement.schema import Plan ADMIN_TOKEN_ENV = "SIE_ADMIN_TOKEN" # noqa: S105 - an environment variable name +SDK_API_KEY_ENV = "SIE_API_KEY" DEFAULT_INTERVAL_S = 60.0 EXIT_FAILED = 1 EXIT_REFUSED = 2 +_TOKEN = re.compile(r"[\x21-\x7e]+") + class _UsageError(ValueError): pass @@ -86,18 +90,26 @@ def _parser() -> argparse.ArgumentParser: return parser +def _token(value: str, source: str, label: str) -> str: + if _TOKEN.fullmatch(value) is None: + raise _UsageError(f"the {label} in {source} must be one run of printable ASCII characters without spaces") + return value + + def _secret(path: Path | None, env_name: str | None, label: str) -> str | None: if path is not None: try: value = path.read_text(encoding="utf-8").strip() - except OSError as error: - raise _UsageError(f"cannot read the {label} file {str(path)!r}: {error.strerror}") from None + except (OSError, UnicodeError) as error: + reason = error.strerror if isinstance(error, OSError) else "it is not UTF-8 text" + raise _UsageError(f"cannot read the {label} file {str(path)!r}: {reason}") from None if not value: raise _UsageError(f"the {label} file {str(path)!r} is empty") - return value + return _token(value, f"the file {str(path)!r}", label) if env_name is None: return None - return os.environ.get(env_name, "").strip() or None + value = os.environ.get(env_name, "").strip() + return _token(value, f"${env_name}", label) if value else None def _load_plan(path: Path) -> Plan: @@ -124,6 +136,8 @@ def _tune(args: argparse.Namespace) -> int: keda.series_queries(args.series_selector) prometheus = keda.PrometheusHttp(args.prometheus_url) read_token = _secret(args.api_key_file, None, "gateway client token") + if read_token is None: + _secret(None, SDK_API_KEY_ENV, "gateway client token") admin_token = _secret(args.admin_token_file, ADMIN_TOKEN_ENV, "gateway admin token") if args.apply else None except ValueError as error: print(f"sie-placement tune: {error}", file=sys.stderr) diff --git a/packages/sie_placement/src/sie_placement/keda.py b/packages/sie_placement/src/sie_placement/keda.py index 3c297b70e..1282e2474 100644 --- a/packages/sie_placement/src/sie_placement/keda.py +++ b/packages/sie_placement/src/sie_placement/keda.py @@ -2,6 +2,7 @@ from __future__ import annotations +import http.client import json import math import re @@ -21,6 +22,7 @@ DEFAULT_MAX_POOLS = 64 MAX_POOL_NAME_LENGTH = 128 PROMETHEUS_TIMEOUT_S = 10.0 +SNAPSHOT_FRESHNESS_S = 20 QUEUE_DEPTH = "queueDepth" PENDING_DEMAND = "pendingDemand" @@ -30,6 +32,7 @@ _LANE_TOKEN = re.compile(r"[a-z0-9_-]{1,63}") _SELECTOR_FORBIDDEN = re.compile(r"[{}\r\n]") _LANE_LABELS = "pool, machine_profile, bundle" +_PRODUCER_LABELS = "producer_instance, collector_generation" _LANE_TOKEN_COUNT = 3 _VALUE_PAIR = 2 @@ -180,6 +183,10 @@ def __init__(self, base_url: str, *, timeout_s: float = PROMETHEUS_TIMEOUT_S) -> raise ValueError("the Prometheus URL must not embed credentials") if parts.query or parts.fragment: raise ValueError("the Prometheus URL must not carry a query or a fragment") + try: + parts.port # noqa: B018 + except ValueError: + raise ValueError("the Prometheus URL has an invalid port") from None self._endpoint = f"{base_url.rstrip('/')}/api/v1/query" self._timeout_s = timeout_s @@ -192,6 +199,8 @@ def query(self, promql: str) -> tuple[Sample, ...]: raise PrometheusError(f"the query failed with HTTP {error.code}") from None except (OSError, ValueError) as error: raise PrometheusError(f"the query failed: {error}") from None + except (http.client.HTTPException, RecursionError) as error: + raise PrometheusError(f"the query failed: {type(error).__name__}") from None return parse_vector(document) @@ -202,10 +211,21 @@ def series_queries(selector: str = "") -> dict[str, str]: def matchers(*extra: str) -> str: return ",".join(part for part in (selector.strip(), *extra) if part) + def fresh(metric: str, timestamp: str, join_labels: str) -> str: + return ( + f"max by ({_LANE_LABELS}) ({metric}{{{matchers()}}} and on({join_labels}) " + f"(abs(time() - {timestamp}{{{matchers()}}}) < {SNAPSHOT_FRESHNESS_S}))" + ) + + capacity = "sie_gateway_capacity_snapshot_timestamp_seconds" return { - QUEUE_DEPTH: f"max by ({_LANE_LABELS}) (sie_gateway_lane_queue_depth{{{matchers()}}})", - PENDING_DEMAND: f"max by ({_LANE_LABELS}) (sie_gateway_pending_demand{{{matchers()}}})", - WARM_FLOOR: f"max by ({_LANE_LABELS}) (sie_gateway_pool_warm_floor{{{matchers()}}})", + QUEUE_DEPTH: fresh( + "sie_gateway_lane_queue_depth", + "sie_gateway_lane_queue_snapshot_timestamp_seconds", + f"{_PRODUCER_LABELS}, {_LANE_LABELS}", + ), + PENDING_DEMAND: fresh("sie_gateway_pending_demand", capacity, _PRODUCER_LABELS), + WARM_FLOOR: fresh("sie_gateway_pool_warm_floor", capacity, _PRODUCER_LABELS), RUNNERS: ( "count by (lane) (max by (lane, producer_instance) " f"(sie_worker_gpu_slots{{{matchers('state="ready"')}}}) > 0)" diff --git a/packages/sie_placement/src/sie_placement/pools.py b/packages/sie_placement/src/sie_placement/pools.py index 860d6f8c1..a6d49ddd0 100644 --- a/packages/sie_placement/src/sie_placement/pools.py +++ b/packages/sie_placement/src/sie_placement/pools.py @@ -10,32 +10,51 @@ from sie_placement.keda import GatewayError TIMEOUT_S = 10.0 +_UNEXPECTED_REPLY = "the gateway returned an unexpected response" + + +def _client(base_url: str, api_key: str | None) -> SIEClient: + try: + return SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S) + except UnicodeError: + raise ValueError("a gateway token holds a character that an HTTP header cannot carry") from None + + +def _gateway_error(error: Exception) -> GatewayError: + if isinstance(error, httpx.HTTPError): + return GatewayError(f"the request failed ({type(error).__name__})") + if isinstance(error, AttributeError): + return GatewayError(_UNEXPECTED_REPLY) + return GatewayError(str(error)) class SdkPools: - def __init__(self, client: SIEClient) -> None: - self._client = client + def __init__(self, base_url: str, api_key: str | None) -> None: + self._base_url = base_url + self._api_key = api_key + self._client = _client(base_url, api_key) def get_pool(self, name: str) -> Mapping[str, object] | None: try: pool = self._client.get_pool(name) - except (SIEError, httpx.HTTPError, ValueError) as error: - raise GatewayError(str(error)) from None + except (SIEError, httpx.HTTPError, ValueError, AttributeError) as error: + raise _gateway_error(error) from None return pool def create_pool( self, name: str, gpus: dict[str, int], *, bundle: str, minimum_worker_count: int, queue_pool: str ) -> None: try: - self._client.create_pool( - name, gpus, bundle=bundle, minimum_worker_count=minimum_worker_count, queue_pool=queue_pool - ) - except (SIEError, httpx.HTTPError, ValueError) as error: - raise GatewayError(str(error)) from None + with _client(self._base_url, self._api_key) as client: + client.create_pool( + name, gpus, bundle=bundle, minimum_worker_count=minimum_worker_count, queue_pool=queue_pool + ) + except (SIEError, httpx.HTTPError, ValueError, AttributeError) as error: + raise _gateway_error(error) from None def close(self) -> None: self._client.close() def connect(base_url: str, api_key: str | None) -> SdkPools: - return SdkPools(SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S)) + return SdkPools(base_url, api_key) diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index 54be76b9f..81eb8aacf 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -4,16 +4,18 @@ import logging import sys import threading +import time from collections.abc import Iterator from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path +from types import SimpleNamespace from urllib.parse import parse_qs, urlsplit import pytest pytest.importorskip("sie_sdk") -from sie_placement import cli +from sie_placement import cli, keda, pools FIXTURES = Path(__file__).parent / "fixtures" READ_TOKEN = "read-token-for-tests" # noqa: S105 - a test value @@ -75,13 +77,18 @@ def __init__(self, *, auth: bool = True) -> None: super().__init__() self.auth = auth self.pools: dict[str, dict] = {} + self.fail_reads = False + self.read_reply: object | None = None def respond(self, handler: _Handler, method: str, body: object) -> None: path = urlsplit(handler.path).path authorization = handler.headers.get("Authorization") self.requests.append({"method": method, "path": path, "authorization": authorization, "body": body}) if method == "GET" and path.startswith("/v1/pools/"): - if self.auth and authorization != f"Bearer {READ_TOKEN}": + if self.read_reply is not None: + handler._reply(200, self.read_reply) + return + if self.fail_reads or (self.auth and authorization != f"Bearer {READ_TOKEN}"): handler._reply(401, {"detail": {"code": "UNAUTHORIZED", "message": "Invalid token"}}) return spec = self.pools.get(path.removeprefix("/v1/pools/")) @@ -120,11 +127,19 @@ def __init__(self) -> None: "sie_gateway_pool_warm_floor": vector((L4, 1), (A100, 0)), "sie_worker_gpu_slots": vector(({"lane": "default|l4|default"}, 1)), } + self.truncate = False def respond(self, handler: _Handler, method: str, body: object) -> None: url = urlsplit(handler.path) query = parse_qs(url.query).get("query", [""])[0] self.requests.append({"path": url.path, "query": query, "authorization": handler.headers.get("Authorization")}) + if self.truncate: + handler.send_response(200) + handler.send_header("Content-Type", "application/json") + handler.send_header("Content-Length", "500") + handler.end_headers() + handler.wfile.write(b'{"status": "succ') + return for metric, document in self.series.items(): if metric in query: handler._reply(200, document) @@ -173,22 +188,16 @@ def read_token_file(tmp_path) -> Path: return path -def tune(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "plan.json") -> int: +def run(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "plan.json") -> int: return cli.main( - [ - "tune", - "--plan", - str(plan), - "--prometheus-url", - prometheus.url, - "--gateway-url", - gateway.url, - "--once", - *extra, - ] + ["tune", "--plan", str(plan), "--prometheus-url", prometheus.url, "--gateway-url", gateway.url, *extra] ) +def tune(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "plan.json") -> int: + return run(gateway, prometheus, "--once", *extra, plan=plan) + + def rows(captured: str) -> list[dict]: return [json.loads(line) for line in captured.splitlines()] @@ -368,3 +377,148 @@ def test_the_core_imports_without_the_sdk_and_tune_names_the_missing_extra(gatew assert tune(gateway, prometheus) == cli.EXIT_REFUSED assert "needs the sie-placement keda extra" in capsys.readouterr().err assert gateway.requests == [] + + +TWO_LINE_SECRET = "first-line-of-a-secret\nsecond-line-of-a-secret" # noqa: S105 - a test value + + +def assert_secret_absent(secret: str, captured, caplog) -> None: + for part in (secret, *secret.split("\n")): + assert part not in captured.out + assert part not in captured.err + assert part not in caplog.text + + +@pytest.mark.parametrize("secret", [TWO_LINE_SECRET, "secret\x00value"]) +@pytest.mark.parametrize("flag", ["--api-key-file", "--admin-token-file"]) +def test_a_token_file_with_a_control_character_is_refused_without_echoing_it( + gateway, prometheus, tmp_path, flag, secret, capsys, caplog +): + caplog.set_level(logging.DEBUG) + token = tmp_path / "token" + token.write_text(secret + "\n", encoding="utf-8") + assert tune(gateway, prometheus, "--apply", flag, str(token)) == cli.EXIT_REFUSED + captured = capsys.readouterr() + assert "must be one run of printable ASCII characters without spaces" in captured.err + assert_secret_absent(secret, captured, caplog) + assert gateway.requests == [] + assert prometheus.requests == [] + + +@pytest.mark.parametrize("value", [TWO_LINE_SECRET, "secret with spaces", "caf\u00e9-secret"]) +def test_an_admin_token_variable_that_a_header_cannot_carry_is_refused( + gateway, prometheus, read_token_file, monkeypatch, value, capsys, caplog +): + caplog.set_level(logging.DEBUG) + monkeypatch.setenv("SIE_ADMIN_TOKEN", value) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == cli.EXIT_REFUSED + captured = capsys.readouterr() + assert "the gateway admin token in $SIE_ADMIN_TOKEN must be one run of printable ASCII" in captured.err + assert_secret_absent(value, captured, caplog) + assert gateway.requests == [] + + +def test_a_token_file_that_is_not_utf8_is_refused_without_echoing_it(gateway, prometheus, tmp_path, capsys): + token = tmp_path / "token" + token.write_bytes(b"secret\xff\xfevalue") + assert tune(gateway, prometheus, "--api-key-file", str(token)) == cli.EXIT_REFUSED + err = capsys.readouterr().err + assert "it is not UTF-8 text" in err + assert "secret" not in err + assert "0xff" not in err + + +@pytest.mark.parametrize("value", [TWO_LINE_SECRET, "caf\u00e9-secret"]) +def test_an_sdk_client_token_that_a_header_cannot_carry_is_refused( + gateway, prometheus, monkeypatch, value, capsys, caplog +): + caplog.set_level(logging.DEBUG) + monkeypatch.setenv("SIE_BASE_URL", gateway.url) + monkeypatch.setenv("SIE_API_KEY", value) + assert tune(gateway, prometheus) == cli.EXIT_REFUSED + captured = capsys.readouterr() + assert "the gateway client token in $SIE_API_KEY must be one run of printable ASCII" in captured.err + assert_secret_absent(value, captured, caplog) + assert gateway.requests == [] + + +def test_a_transport_failure_is_reported_without_the_header_it_carried(gateway): + reader = pools.connect(gateway.url, TWO_LINE_SECRET) + try: + with pytest.raises(keda.GatewayError) as raised: + reader.get_pool("placement-floor-default-l4-default") + finally: + reader.close() + assert str(raised.value) == "the request failed (LocalProtocolError)" + writer = pools.connect(gateway.url, TWO_LINE_SECRET) + try: + with pytest.raises(keda.GatewayError) as raised: + writer.create_pool( + "placement-floor-default-l4-default", + {"l4": 0}, + bundle="default", + minimum_worker_count=1, + queue_pool="default", + ) + finally: + writer.close() + assert str(raised.value) == "the request failed (LocalProtocolError)" + assert gateway.pools == {} + + +def test_a_token_an_http_header_cannot_encode_is_refused_without_echoing_it(gateway): + with pytest.raises(ValueError, match=r"^a gateway token holds a character that an HTTP header cannot carry$"): + pools.connect(gateway.url, "caf\u00e9-secret") + + +def test_a_malformed_pool_reply_fails_only_the_tick(gateway, prometheus, read_token_file, capsys): + gateway.read_reply = ["not", "a", "pool"] + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED + errors = [row["error"] for row in rows(capsys.readouterr().out)] + assert errors == [ + "reading pool 'placement-floor-default-l4-default' failed: the gateway returned an unexpected response", + "reading pool 'placement-floor-default-a100-80gb-generation' failed: " + "the gateway returned an unexpected response", + ] + + +def test_the_loop_survives_failed_ticks_and_never_renews_a_lease_in_the_background( + gateway, prometheus, read_token_file, monkeypatch, capsys +): + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + leases: list[list[str]] = [] + closed: list[pools.SdkPools] = [] + close = pools.SdkPools.close + + def record_close(self: pools.SdkPools) -> None: + closed.append(self) + close(self) + + def between_ticks(_seconds: float) -> None: + leases.append([thread.name for thread in threading.enumerate() if thread.name.startswith("pool-lease-")]) + if len(leases) == 1: + prometheus.truncate = True + elif len(leases) == 2: + prometheus.truncate = False + gateway.fail_reads = True + else: + raise KeyboardInterrupt + + monkeypatch.setattr(pools.SdkPools, "close", record_close) + monkeypatch.setattr(cli, "time", SimpleNamespace(monotonic=time.monotonic, sleep=between_ticks)) + assert run(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply", "--interval-s", "1") == 0 + captured = capsys.readouterr() + lines = rows(captured.out) + assert [(row["action"], row["applied"], row["error"] is None) for row in lines] == [ + ("raise_to_floor", True, True), + ("hold", False, True), + ("hold", True, True), + ("hold", False, True), + (None, False, False), + (None, False, False), + ] + assert "Prometheus series queueDepth: the query failed: IncompleteRead" in captured.err + assert leases == [[], [], []] + posts = [request["path"] for request in gateway.requests if request["method"] == "POST"] + assert posts == ["/v1/pools", "/v1/pools"] + assert len(closed) == 2 diff --git a/packages/sie_placement/tests/test_keda.py b/packages/sie_placement/tests/test_keda.py index 5e80308b1..c867cd32a 100644 --- a/packages/sie_placement/tests/test_keda.py +++ b/packages/sie_placement/tests/test_keda.py @@ -3,6 +3,7 @@ import copy import itertools import json +import socketserver import threading from collections.abc import Iterator, Mapping, Sequence from datetime import UTC, datetime @@ -205,18 +206,49 @@ def test_a_carrier_name_longer_than_the_gateway_accepts_is_refused(): keda.carriers(plan) -def test_the_series_queries_read_each_lane_from_the_gateway_and_worker_series(): +def test_the_series_queries_read_each_lane_from_fresh_gateway_producers_and_the_worker_series(): + capacity_fresh = ( + "and on(producer_instance, collector_generation) " + "(abs(time() - sie_gateway_capacity_snapshot_timestamp_seconds{}) < 20))" + ) assert keda.series_queries() == { - "queueDepth": "max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{})", - "pendingDemand": "max by (pool, machine_profile, bundle) (sie_gateway_pending_demand{})", - "warmFloor": "max by (pool, machine_profile, bundle) (sie_gateway_pool_warm_floor{})", + "queueDepth": ( + "max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{} " + "and on(producer_instance, collector_generation, pool, machine_profile, bundle) " + "(abs(time() - sie_gateway_lane_queue_snapshot_timestamp_seconds{}) < 20))" + ), + "pendingDemand": f"max by (pool, machine_profile, bundle) (sie_gateway_pending_demand{{}} {capacity_fresh}", + "warmFloor": f"max by (pool, machine_profile, bundle) (sie_gateway_pool_warm_floor{{}} {capacity_fresh}", "runners": ('count by (lane) (max by (lane, producer_instance) (sie_worker_gpu_slots{state="ready"}) > 0)'), } scoped = keda.series_queries('namespace="sie"') - assert scoped["queueDepth"].endswith('(sie_gateway_lane_queue_depth{namespace="sie"})') + assert scoped["queueDepth"] == ( + 'max by (pool, machine_profile, bundle) (sie_gateway_lane_queue_depth{namespace="sie"} ' + "and on(producer_instance, collector_generation, pool, machine_profile, bundle) " + '(abs(time() - sie_gateway_lane_queue_snapshot_timestamp_seconds{namespace="sie"}) < 20))' + ) + for name in ("pendingDemand", "warmFloor"): + assert scoped[name].endswith( + '{namespace="sie"} and on(producer_instance, collector_generation) ' + '(abs(time() - sie_gateway_capacity_snapshot_timestamp_seconds{namespace="sie"}) < 20))' + ) assert scoped["runners"].endswith('(sie_worker_gpu_slots{namespace="sie",state="ready"}) > 0)') +def test_a_lane_whose_gateway_producers_are_all_stale_is_held(): + stale = lane_labels(bundle="stale") + prometheus = FakePrometheus.lane(queue_depth=4, pending=1, runners=2) + prometheus.series[QUEUE_METRIC] = [(lane_labels(), 4)] + prometheus.series[PENDING_METRIC] = [(lane_labels(), 1)] + lanes = keda.carriers(make_plan({"bundle": "default"}, {"bundle": "stale"})) + pools = FakePools({carrier.name: carrier_spec(1, bundle=carrier.key.bundle) for carrier in lanes}) + fresh, held = run_tick(lanes, prometheus, pools, pools).records + assert held.carrier.key.bundle == stale["bundle"] + assert (fresh.step.action, fresh.step.settings.floor) == (Action.SCALE_UP, 2) + assert (held.telemetry.queue_depth, held.telemetry.pending_demand) == (None, None) + assert (held.step.action, held.step.settings.floor) == (Action.HOLD, 1) + + @pytest.mark.parametrize("selector", ['namespace="sie"}', '{namespace="sie"', 'a="b"\nc="d"']) def test_a_selector_that_could_leave_its_braces_is_rejected(selector): with pytest.raises(ValueError, match="without braces or line breaks"): @@ -309,9 +341,57 @@ def test_the_http_reader_reports_an_unreachable_prometheus(): keda.PrometheusHttp("http://127.0.0.1:9", timeout_s=2).query("up") +class _RawHandler(socketserver.StreamRequestHandler): + server: _RawServer + + def handle(self) -> None: + self.rfile.readline() + self.wfile.write(self.server.reply) + + +class _RawServer(socketserver.ThreadingTCPServer): + daemon_threads = True + + def __init__(self, reply: bytes) -> None: + super().__init__(("127.0.0.1", 0), _RawHandler) + self.reply = reply + + +@pytest.mark.parametrize( + ("reply", "error"), + [ + (b"garbage\r\n\r\n", "BadStatusLine"), + ( + b'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 500\r\n\r\n{"status": "s', + "IncompleteRead", + ), + ], +) +def test_the_http_reader_reports_a_malformed_reply(monkeypatch, reply, error): + for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + monkeypatch.delenv(name, raising=False) + server = _RawServer(reply) + thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True) + thread.start() + try: + with pytest.raises(keda.PrometheusError, match=f"the query failed: {error}"): + keda.PrometheusHttp(f"http://127.0.0.1:{server.server_address[1]}", timeout_s=2).query("up") + finally: + server.shutdown() + server.server_close() + + @pytest.mark.parametrize( "url", - ["file:///etc/passwd", "ftp://prometheus", "prometheus:9090", "http://user:secret@prometheus", "http://p/?x=1"], + [ + "file:///etc/passwd", + "ftp://prometheus", + "prometheus:9090", + "http://user:secret@prometheus", + "http://p/?x=1", + "http://prometheus:notaport", + "http://prometheus:99999", + ], ) def test_the_http_reader_accepts_only_a_plain_http_base_url(url): with pytest.raises(ValueError, match="Prometheus URL"): From 818f848b6995fc050bc4b4d3c6240e0eaffa4103 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Thu, 5 Nov 2026 19:37:21 +0100 Subject: [PATCH 12/22] fix(placement): refuse plans the chart cannot run before tuning their floors sie-placement tune now refuses, before it calls Prometheus or the gateway, a plan that pins an identity on a lane of the default queue pool or gives an identity a warmth the sie-cluster chart does not realize (bridged, snapshot-cold or scheduled-warm), in the wording the Helm values emitter uses for the same plans, so it never moves floors for a plan the chart cannot run. The tune tests read a plan the chart can run. --- packages/sie_placement/README.md | 4 ++ .../sie_placement/src/sie_placement/keda.py | 33 ++++++++- .../tests/fixtures/tune-plan.json | 68 +++++++++++++++++++ packages/sie_placement/tests/test_cli.py | 19 +++++- packages/sie_placement/tests/test_keda.py | 49 ++++++++++++- 5 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 packages/sie_placement/tests/fixtures/tune-plan.json diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 3ff3dfab8..7cf3d5ce6 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -244,6 +244,10 @@ The command exits with status 2 before it calls Prometheus or the gateway when: - a lane's `queuePool`, machine profile or bundle is not 1 to 63 letters, digits, `_` or `-`, two lanes need the same carrier, or a carrier name is longer than the gateway's 128 characters; +- the `sie-cluster` chart cannot run the plan: a lane on the `default` queue + pool pins an identity, whose pins no static queue pool carries, or an + identity is `bridged`, `snapshot-cold` or `scheduled-warm`, warmths the chart + does not realize; - a token file or `SIE_ADMIN_TOKEN` holds anything but one run of printable ASCII characters without spaces after trimming, or the Prometheus URL has an invalid port. diff --git a/packages/sie_placement/src/sie_placement/keda.py b/packages/sie_placement/src/sie_placement/keda.py index 1282e2474..70a43e153 100644 --- a/packages/sie_placement/src/sie_placement/keda.py +++ b/packages/sie_placement/src/sie_placement/keda.py @@ -14,10 +14,11 @@ from datetime import UTC, datetime from typing import Protocol -from sie_placement.schema import Plan, PlanLane +from sie_placement.schema import Plan, PlanLane, Warmth from sie_placement.tuner import Observation, Settings, Step, step CARRIER_PREFIX = "placement-floor" +DEFAULT_QUEUE_POOL = "default" DEFAULT_MAX_POOL_FLOOR = 4 DEFAULT_MAX_POOLS = 64 MAX_POOL_NAME_LENGTH = 128 @@ -29,6 +30,12 @@ WARM_FLOOR = "warmFloor" RUNNERS = "runners" +UNREALIZED_WARMTHS = { + Warmth.BRIDGED: "the overlay configures no remote bridge to serve it while the lane boots", + Warmth.SNAPSHOT_COLD: "the chart restores no snapshot, so the lane would boot cold", + Warmth.SCHEDULED_WARM: "the chart keeps no warm window, so the lane would stay cold inside it", +} + _LANE_TOKEN = re.compile(r"[a-z0-9_-]{1,63}") _SELECTOR_FORBIDDEN = re.compile(r"[{}\r\n]") _LANE_LABELS = "pool, machine_profile, bundle" @@ -72,6 +79,10 @@ def canonical(cls, queue_pool: str, machine_profile: str, bundle: str) -> LaneKe def otel_lane(self) -> str: return f"{self.queue_pool}|{self.machine_profile}|{self.bundle}" + @property + def chart_lane(self) -> str: + return f"{self.queue_pool}/{self.machine_profile}/{self.bundle}" + @dataclass(frozen=True, slots=True) class Carrier: @@ -98,6 +109,10 @@ def carriers( ) result: list[Carrier] = [] first_index: dict[str, int] = {} + pins: dict[str, dict[str, int]] = {} + for index, entry in enumerate(plan.identities): + if entry.warmth is Warmth.PINNED: + pins.setdefault(entry.lane, {})[entry.identity] = index for index, lane in enumerate(plan.lanes): path = f"$.lanes[{index}]" key = LaneKey.canonical(lane.queue_pool, lane.cell.machine_profile, lane.bundle) @@ -106,6 +121,14 @@ def carriers( f"{path}: queuePool, cell.machineProfile and bundle must each be 1 to 63 letters, digits, '_' or '-'" ) continue + pinned = pins.get(lane.name) + if key.queue_pool == DEFAULT_QUEUE_POOL and pinned: + identity, pin = min(pinned.items(), key=lambda item: item[1]) + problems.append( + f"$.identities[{pin}].warmth: pins {identity!r} on chart lane {key.chart_lane!r}, but the gateway " + f"keeps no static queue pool for the {DEFAULT_QUEUE_POOL!r} queue pool, so its pinnedModels have no " + "carrier; place pinned identities on a named queuePool" + ) if lane.envelope.floor_max > max_pool_floor: problems.append( f"{path}.envelope.floorMax: {lane.envelope.floor_max} exceeds the gateway's " @@ -120,6 +143,14 @@ def carriers( if earlier != index: problems.append(f"{path}: needs the same floor-carrier pool {carrier.name!r} as $.lanes[{earlier}]") result.append(carrier) + for index, entry in enumerate(plan.identities): + reason = UNREALIZED_WARMTHS.get(entry.warmth) + if reason is not None: + problems.append( + f"$.identities[{index}].warmth: is {entry.warmth.value!r} for {entry.identity!r} on lane " + f"{entry.lane!r}, which the sie-cluster chart does not realize: {reason}; plan the identity pinned, " + "lazy-resident or cold-with-bound" + ) if problems: raise PlanRefusedError(problems) return tuple(result) diff --git a/packages/sie_placement/tests/fixtures/tune-plan.json b/packages/sie_placement/tests/fixtures/tune-plan.json new file mode 100644 index 000000000..b36c3b28f --- /dev/null +++ b/packages/sie_placement/tests/fixtures/tune-plan.json @@ -0,0 +1,68 @@ +{ + "kind": "placement-plan", + "schemaVersion": 1, + "placement": "us-central1", + "lanes": [ + { + "name": "default-l4-default", + "queuePool": "default", + "bundle": "default", + "cell": { + "machineProfile": "l4", + "gpuProduct": "NVIDIA-L4" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 7.5, + "memoryMiB": 30720 + }, + "concurrency": 16, + "maxContainers": 6, + "envelope": { + "floorMin": 1, + "floorMax": 3, + "bufferMin": 0, + "bufferMax": 1, + "scaledownWindowMinS": 60, + "scaledownWindowMaxS": 900 + } + }, + { + "name": "default-a100-80gb-generation", + "queuePool": "default", + "bundle": "generation", + "cell": { + "machineProfile": "a100-80gb", + "gpuProduct": "NVIDIA-A100-SXM4-80GB" + }, + "containerShape": { + "gpuCount": 1, + "cpuCores": 12, + "memoryMiB": 87040 + }, + "concurrency": 32, + "maxContainers": 2, + "envelope": { + "floorMin": 0, + "floorMax": 1 + } + } + ], + "identities": [ + { + "identity": "BAAI/bge-m3", + "lane": "default-l4-default", + "warmth": "lazy-resident" + }, + { + "identity": "BAAI/bge-m3:sparse", + "lane": "default-l4-default", + "warmth": "lazy-resident" + }, + { + "identity": "Qwen/Qwen3-8B", + "lane": "default-a100-80gb-generation", + "warmth": "cold-with-bound" + } + ] +} diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index 81eb8aacf..7adb7ef84 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -188,13 +188,15 @@ def read_token_file(tmp_path) -> Path: return path -def run(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "plan.json") -> int: +def run(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "tune-plan.json") -> int: return cli.main( ["tune", "--plan", str(plan), "--prometheus-url", prometheus.url, "--gateway-url", gateway.url, *extra] ) -def tune(gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "plan.json") -> int: +def tune( + gateway: FakeGateway, prometheus: FakePrometheus, *extra: str, plan: Path = FIXTURES / "tune-plan.json" +) -> int: return run(gateway, prometheus, "--once", *extra, plan=plan) @@ -311,7 +313,7 @@ def test_a_rejected_write_fails_the_tick_and_names_the_pool(gateway, prometheus, def test_an_envelope_above_the_pool_floor_limit_is_refused_before_any_call( gateway, prometheus, read_token_file, tmp_path, capsys ): - document = json.loads((FIXTURES / "plan.json").read_text(encoding="utf-8")) + document = json.loads((FIXTURES / "tune-plan.json").read_text(encoding="utf-8")) document["lanes"][0]["envelope"]["floorMax"] = 5 plan = tmp_path / "plan.json" plan.write_text(json.dumps(document), encoding="utf-8") @@ -323,6 +325,17 @@ def test_an_envelope_above_the_pool_floor_limit_is_refused_before_any_call( capsys.readouterr() +def test_a_plan_the_chart_cannot_run_is_refused_before_any_call(gateway, prometheus, tmp_path, capsys): + document = json.loads((FIXTURES / "tune-plan.json").read_text(encoding="utf-8")) + document["identities"][0]["warmth"] = "pinned" + plan = tmp_path / "plan.json" + plan.write_text(json.dumps(document), encoding="utf-8") + assert tune(gateway, prometheus, plan=plan) == cli.EXIT_REFUSED + assert "pins 'BAAI/bge-m3' on chart lane 'default/l4/default'" in capsys.readouterr().err + assert gateway.requests == [] + assert prometheus.requests == [] + + def test_more_lanes_than_the_pool_limit_are_refused_before_any_call(gateway, prometheus, capsys): assert tune(gateway, prometheus, "--apply", "--max-pools", "1") == cli.EXIT_REFUSED assert "above the gateway's limit of 1 API pools" in capsys.readouterr().err diff --git a/packages/sie_placement/tests/test_keda.py b/packages/sie_placement/tests/test_keda.py index c867cd32a..3fa82a226 100644 --- a/packages/sie_placement/tests/test_keda.py +++ b/packages/sie_placement/tests/test_keda.py @@ -26,7 +26,7 @@ def plan_document() -> dict: - return json.loads((FIXTURES / "plan.json").read_text(encoding="utf-8")) + return json.loads((FIXTURES / "tune-plan.json").read_text(encoding="utf-8")) def make_plan(*lanes: dict) -> Plan: @@ -44,7 +44,7 @@ def make_plan(*lanes: dict) -> Plan: if machine_profile is not None: lane["cell"]["machineProfile"] = machine_profile document["lanes"].append(lane) - warmth = "pinned" if lane["envelope"]["floorMin"] >= 1 else "cold-with-bound" + warmth = "lazy-resident" if lane["envelope"]["floorMin"] >= 1 else "cold-with-bound" document["identities"].append({"identity": f"model-{index}", "lane": lane["name"], "warmth": warmth}) return Plan.from_dict(document) @@ -206,6 +206,51 @@ def test_a_carrier_name_longer_than_the_gateway_accepts_is_refused(): keda.carriers(plan) +def test_a_pin_on_the_default_queue_pool_is_refused_as_the_helm_emitter_refuses_it(): + document = plan_document() + document["lanes"][0]["queuePool"] = "Default" + document["identities"][1]["warmth"] = "pinned" + document["identities"][0]["warmth"] = "pinned" + with pytest.raises(keda.PlanRefusedError) as raised: + keda.carriers(Plan.from_dict(document)) + assert raised.value.problems == ( + "$.identities[0].warmth: pins 'BAAI/bge-m3' on chart lane 'default/l4/default', but the gateway keeps no " + "static queue pool for the 'default' queue pool, so its pinnedModels have no carrier; place pinned " + "identities on a named queuePool", + ) + document["lanes"][0]["queuePool"] = "embed" + (pinned, _) = keda.carriers(Plan.from_dict(document)) + assert pinned.name == "placement-floor-embed-l4-default" + + +@pytest.mark.parametrize("warmth", sorted(keda.UNREALIZED_WARMTHS)) +def test_a_warmth_the_chart_does_not_realize_is_refused_as_the_helm_emitter_refuses_it(warmth): + document = plan_document() + document["identities"][2]["warmth"] = warmth.value + with pytest.raises(keda.PlanRefusedError) as raised: + keda.carriers(Plan.from_dict(document)) + assert raised.value.problems == ( + f"$.identities[2].warmth: is {warmth.value!r} for 'Qwen/Qwen3-8B' on lane 'default-a100-80gb-generation', " + f"which the sie-cluster chart does not realize: {keda.UNREALIZED_WARMTHS[warmth]}; plan the identity " + "pinned, lazy-resident or cold-with-bound", + ) + + +def test_every_problem_of_a_refused_plan_is_reported(): + document = plan_document() + document["identities"][0]["warmth"] = "pinned" + document["identities"][2]["warmth"] = "snapshot-cold" + document["lanes"][1]["envelope"]["floorMax"] = 5 + document["lanes"][1]["maxContainers"] = 6 + with pytest.raises(keda.PlanRefusedError) as raised: + keda.carriers(Plan.from_dict(document)) + assert [problem.split(":", 1)[0] for problem in raised.value.problems] == [ + "$.identities[0].warmth", + "$.lanes[1].envelope.floorMax", + "$.identities[2].warmth", + ] + + def test_the_series_queries_read_each_lane_from_fresh_gateway_producers_and_the_worker_series(): capacity_fresh = ( "and on(producer_instance, collector_generation) " From 89a5790bae2c313694b14f45c3ca2ddfe077b9c7 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 10:16:49 +0100 Subject: [PATCH 13/22] fix(placement): refuse non-finite numbers in documents built in code - The minimum and positivity checks that every number field passes through now refuse NaN, infinities and integers beyond the float range, as parsing does, so a document built in code serializes to one that parses again. --- .../sie_placement/src/sie_placement/schema.py | 19 +++++++++++-- packages/sie_placement/tests/test_schema.py | 28 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/sie_placement/src/sie_placement/schema.py b/packages/sie_placement/src/sie_placement/schema.py index 410d6cf7e..9c7a1317f 100644 --- a/packages/sie_placement/src/sie_placement/schema.py +++ b/packages/sie_placement/src/sie_placement/schema.py @@ -247,13 +247,28 @@ def _require_identifier(name: str, value: str) -> None: raise SchemaError(name, f"must be a non-empty string without whitespace, got {value!r}") +def _require_finite(name: str, value: float) -> None: + try: + finite = math.isfinite(value) + except OverflowError: + raise SchemaError(name, "must be a finite number, got an integer beyond the float range") from None + if not finite: + raise SchemaError(name, f"must be a finite number, got {value!r}") + + def _require_at_least(name: str, value: float | None, minimum: int) -> None: - if value is not None and value < minimum: + if value is None: + return + _require_finite(name, value) + if value < minimum: raise SchemaError(name, f"must be at least {minimum}, got {value!r}") def _require_positive(name: str, value: float | None) -> None: - if value is not None and value <= 0: + if value is None: + return + _require_finite(name, value) + if value <= 0: raise SchemaError(name, f"must be greater than 0, got {value!r}") diff --git a/packages/sie_placement/tests/test_schema.py b/packages/sie_placement/tests/test_schema.py index 2ca86915c..eb729e56d 100644 --- a/packages/sie_placement/tests/test_schema.py +++ b/packages/sie_placement/tests/test_schema.py @@ -17,8 +17,10 @@ DemandSeries, Envelope, IdentityServiceLevel, + MachineProfilePrice, Operation, Percentiles, + Placement, Plan, PlanIdentity, PlanLane, @@ -27,6 +29,7 @@ SchemaError, ServiceLevel, ServiceLevels, + Throughput, Warmth, ) @@ -141,6 +144,31 @@ def test_numbers_must_be_finite(value): assert error.message.startswith("expected a finite number") +@pytest.mark.parametrize( + ("build", "field"), + [ + (lambda value: Throughput(requests_per_second=value), "requestsPerSecond"), + (lambda value: Percentiles(p99_ms=value), "p99Ms"), + (lambda value: ContainerShape(gpu_count=1, cpu_cores=value, memory_mib=1024), "cpuCores"), + (lambda value: Placement(name="default", region_multiplier=value), "regionMultiplier"), + ( + lambda value: MachineProfilePrice( + name="l4", gpu_product="NVIDIA-L4", accelerator_per_hour=value, core_per_hour=0, memory_gib_per_hour=0 + ), + "acceleratorPerHour", + ), + ], +) +@pytest.mark.parametrize( + "value", [math.nan, math.inf, -math.inf, 10**400], ids=["nan", "infinity", "negative-infinity", "integer"] +) +def test_numbers_built_in_code_must_be_finite(build, field, value): + with pytest.raises(SchemaError) as caught: + build(value) + assert caught.value.path == field + assert caught.value.message.startswith("must be a finite number") + + def test_numbers_reject_booleans(): data = load("price-list.json") data["placements"][0]["regionMultiplier"] = False From 557696f65962600e5c9e2e480af470ce698b4ce2 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 10:28:15 +0100 Subject: [PATCH 14/22] fix(placement): refuse booleans and non-integers in documents built in code - Number fields refuse booleans and values that are not numbers, as the parser does, and integer fields refuse booleans and floats through a dedicated integer check, so a document built in code serializes to one that parses again. --- .../sie_placement/src/sie_placement/schema.py | 41 ++++++++++++------- packages/sie_placement/tests/test_schema.py | 23 +++++++++++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/packages/sie_placement/src/sie_placement/schema.py b/packages/sie_placement/src/sie_placement/schema.py index 9c7a1317f..1e319f28d 100644 --- a/packages/sie_placement/src/sie_placement/schema.py +++ b/packages/sie_placement/src/sie_placement/schema.py @@ -247,7 +247,9 @@ def _require_identifier(name: str, value: str) -> None: raise SchemaError(name, f"must be a non-empty string without whitespace, got {value!r}") -def _require_finite(name: str, value: float) -> None: +def _require_number(name: str, value: float) -> None: + if isinstance(value, bool) or not isinstance(value, int | float): + raise SchemaError(name, f"must be a number, got {_describe(value)}") try: finite = math.isfinite(value) except OverflowError: @@ -256,10 +258,19 @@ def _require_finite(name: str, value: float) -> None: raise SchemaError(name, f"must be a finite number, got {value!r}") +def _require_count(name: str, value: int | None, minimum: int) -> None: + if value is None: + return + if isinstance(value, bool) or not isinstance(value, int): + raise SchemaError(name, f"must be an integer, got {_describe(value)}") + if value < minimum: + raise SchemaError(name, f"must be at least {minimum}, got {value!r}") + + def _require_at_least(name: str, value: float | None, minimum: int) -> None: if value is None: return - _require_finite(name, value) + _require_number(name, value) if value < minimum: raise SchemaError(name, f"must be at least {minimum}, got {value!r}") @@ -267,7 +278,7 @@ def _require_at_least(name: str, value: float | None, minimum: int) -> None: def _require_positive(name: str, value: float | None) -> None: if value is None: return - _require_finite(name, value) + _require_number(name, value) if value <= 0: raise SchemaError(name, f"must be greater than 0, got {value!r}") @@ -316,7 +327,7 @@ class ServiceLevel: def __post_init__(self) -> None: for name, value in self._fields(): - _require_at_least(name, value, 0) + _require_count(name, value, 0) if self.warm_p99_ms is not None and (self.ttft_p99_ms is not None or self.itl_p99_ms is not None): raise SchemaError("warmP99Ms", "must be omitted when ttftP99Ms and itlP99Ms declare a generation identity") @@ -469,9 +480,9 @@ class ContainerShape: memory_mib: int def __post_init__(self) -> None: - _require_at_least("gpuCount", self.gpu_count, 1) + _require_count("gpuCount", self.gpu_count, 1) _require_positive("cpuCores", self.cpu_cores) - _require_at_least("memoryMiB", self.memory_mib, 1) + _require_count("memoryMiB", self.memory_mib, 1) @classmethod def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: @@ -495,12 +506,12 @@ class RequestShape: output_tokens: int | None = None def __post_init__(self) -> None: - _require_at_least("itemsPerRequest", self.items_per_request, 1) - _require_at_least("inputTokensPerItem", self.input_tokens_per_item, 0) + _require_count("itemsPerRequest", self.items_per_request, 1) + _require_count("inputTokensPerItem", self.input_tokens_per_item, 0) if self.operation is Operation.GENERATE: if self.output_tokens is None: raise SchemaError("outputTokens", "is required for the generate operation") - _require_at_least("outputTokens", self.output_tokens, 1) + _require_count("outputTokens", self.output_tokens, 1) else: _require_absent("outputTokens", self.output_tokens, "for an operation other than generate") @@ -612,7 +623,7 @@ class Receipt: def __post_init__(self) -> None: _require_identifier("identity", self.identity) - _require_at_least("concurrency", self.concurrency, 1) + _require_count("concurrency", self.concurrency, 1) if self.request_shape.operation is Operation.GENERATE: _require_absent("latency", self.latency, "for the generate operation, which reports ttft and itl") if self.ttft is None: @@ -813,8 +824,8 @@ class DemandHour: def __post_init__(self) -> None: _require_hour("start", self.start) - _require_at_least("requests", self.requests, 0) - _require_at_least("units", self.units, 0) + _require_count("requests", self.requests, 0) + _require_count("units", self.units, 0) @classmethod def from_dict(cls, data: object, path: str = ROOT_PATH) -> Self: @@ -912,7 +923,7 @@ class Envelope: def __post_init__(self) -> None: for name, value in self._fields(): - _require_at_least(name, value, 0) + _require_count(name, value, 0) _require_ordered("floorMin", self.floor_min, "floorMax", self.floor_max) _require_pair("bufferMin", self.buffer_min, "bufferMax", self.buffer_max) _require_ordered("bufferMin", self.buffer_min, "bufferMax", self.buffer_max) @@ -965,8 +976,8 @@ def __post_init__(self) -> None: _require_identifier("name", self.name) _require_identifier("queuePool", self.queue_pool) _require_identifier("bundle", self.bundle) - _require_at_least("concurrency", self.concurrency, 1) - _require_at_least("maxContainers", self.max_containers, 1) + _require_count("concurrency", self.concurrency, 1) + _require_count("maxContainers", self.max_containers, 1) if self.envelope.floor_max > self.max_containers: raise SchemaError( "envelope.floorMax", diff --git a/packages/sie_placement/tests/test_schema.py b/packages/sie_placement/tests/test_schema.py index eb729e56d..f1dfb3c34 100644 --- a/packages/sie_placement/tests/test_schema.py +++ b/packages/sie_placement/tests/test_schema.py @@ -169,6 +169,29 @@ def test_numbers_built_in_code_must_be_finite(build, field, value): assert caught.value.message.startswith("must be a finite number") +@pytest.mark.parametrize(("value", "described"), [(True, "boolean true"), ("1", "string '1'")]) +def test_numbers_built_in_code_must_be_numbers(value, described): + with pytest.raises(SchemaError) as caught: + Throughput(requests_per_second=value) + assert str(caught.value) == f"requestsPerSecond: must be a number, got {described}" + + +@pytest.mark.parametrize( + ("build", "field"), + [ + (lambda value: ContainerShape(gpu_count=value, cpu_cores=1.0, memory_mib=1024), "gpuCount"), + (lambda value: ServiceLevel(come_alive_p95_s=value), "comeAliveP95S"), + (lambda value: Envelope(floor_min=0, floor_max=value), "floorMax"), + (lambda value: DemandHour(start=datetime(2026, 1, 1, tzinfo=UTC), requests=value), "requests"), + ], +) +@pytest.mark.parametrize(("value", "described"), [(1.5, "number 1.5"), (2.0, "number 2.0"), (True, "boolean true")]) +def test_integers_built_in_code_must_be_integers(build, field, value, described): + with pytest.raises(SchemaError) as caught: + build(value) + assert str(caught.value) == f"{field}: must be an integer, got {described}" + + def test_numbers_reject_booleans(): data = load("price-list.json") data["placements"][0]["regionMultiplier"] = False From 22e21929e99a2963ff0c5d03f5286d2378df950e Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 10:33:57 +0100 Subject: [PATCH 15/22] fix(placement): refuse tuner counts that are not integers - Observation and Settings counts refuse booleans and floats before their non-negative check, and tests pin zero as accepted. --- .../sie_placement/src/sie_placement/tuner.py | 6 ++++- packages/sie_placement/tests/test_tuner.py | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/sie_placement/src/sie_placement/tuner.py b/packages/sie_placement/src/sie_placement/tuner.py index 98e982fe3..d265892bb 100644 --- a/packages/sie_placement/src/sie_placement/tuner.py +++ b/packages/sie_placement/src/sie_placement/tuner.py @@ -24,7 +24,11 @@ class Attainment(StrEnum): def _require_non_negative(name: str, value: int | None) -> None: - if value is not None and value < 0: + if value is None: + return + if isinstance(value, bool) or not isinstance(value, int): + raise ValueError(f"{name} must be an integer, got {value!r}") + if value < 0: raise ValueError(f"{name} must be at least 0, got {value!r}") diff --git a/packages/sie_placement/tests/test_tuner.py b/packages/sie_placement/tests/test_tuner.py index 4c19eab49..15ba1b1c0 100644 --- a/packages/sie_placement/tests/test_tuner.py +++ b/packages/sie_placement/tests/test_tuner.py @@ -408,6 +408,28 @@ def test_negative_counts_are_rejected(make): make() +@pytest.mark.parametrize("value", [True, 1.0]) +@pytest.mark.parametrize( + "make", + [ + lambda value: Observation(runners=value, backlog=0, running=0), + lambda value: Observation(runners=0, backlog=value, running=0), + lambda value: Observation(runners=0, backlog=0, running=value), + lambda value: Settings(floor=value), + lambda value: Settings(floor=0, buffer=value), + lambda value: Settings(floor=0, scaledown_window_s=value), + ], +) +def test_counts_that_are_not_integers_are_rejected(make, value): + with pytest.raises(ValueError, match="must be an integer"): + make(value) + + +def test_zero_counts_are_accepted(): + assert Observation(runners=0, backlog=0, running=0).idle + assert Settings(floor=0, buffer=0, scaledown_window_s=0).floor == 0 + + @pytest.mark.parametrize("attainment", list(Attainment)) def test_an_attainment_given_as_its_wire_string_reads_as_its_value(attainment): wire: Any = attainment.value From baef2579ccb63f8b75c8b084572265795eb57af1 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:08:48 +0100 Subject: [PATCH 16/22] fix(placement): fail only the tick on a pool reply too deep to parse - A gateway reply nested deeper than the JSON decoder's recursion limit raised RecursionError, which escaped the tick and ended the applier. Pool reads and writes now report it as an unexpected response, as the Prometheus reader already does for its replies. --- .../sie_placement/src/sie_placement/pools.py | 6 +-- packages/sie_placement/tests/test_cli.py | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/packages/sie_placement/src/sie_placement/pools.py b/packages/sie_placement/src/sie_placement/pools.py index a6d49ddd0..886cfa29c 100644 --- a/packages/sie_placement/src/sie_placement/pools.py +++ b/packages/sie_placement/src/sie_placement/pools.py @@ -23,7 +23,7 @@ def _client(base_url: str, api_key: str | None) -> SIEClient: def _gateway_error(error: Exception) -> GatewayError: if isinstance(error, httpx.HTTPError): return GatewayError(f"the request failed ({type(error).__name__})") - if isinstance(error, AttributeError): + if isinstance(error, AttributeError | RecursionError): return GatewayError(_UNEXPECTED_REPLY) return GatewayError(str(error)) @@ -37,7 +37,7 @@ def __init__(self, base_url: str, api_key: str | None) -> None: def get_pool(self, name: str) -> Mapping[str, object] | None: try: pool = self._client.get_pool(name) - except (SIEError, httpx.HTTPError, ValueError, AttributeError) as error: + except (SIEError, httpx.HTTPError, ValueError, AttributeError, RecursionError) as error: raise _gateway_error(error) from None return pool @@ -49,7 +49,7 @@ def create_pool( client.create_pool( name, gpus, bundle=bundle, minimum_worker_count=minimum_worker_count, queue_pool=queue_pool ) - except (SIEError, httpx.HTTPError, ValueError, AttributeError) as error: + except (SIEError, httpx.HTTPError, ValueError, AttributeError, RecursionError) as error: raise _gateway_error(error) from None def close(self) -> None: diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index 7adb7ef84..b9a7f63d7 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -79,11 +79,21 @@ def __init__(self, *, auth: bool = True) -> None: self.pools: dict[str, dict] = {} self.fail_reads = False self.read_reply: object | None = None + self.raw_reply: tuple[int, bytes] | None = None def respond(self, handler: _Handler, method: str, body: object) -> None: path = urlsplit(handler.path).path authorization = handler.headers.get("Authorization") self.requests.append({"method": method, "path": path, "authorization": authorization, "body": body}) + if self.raw_reply is not None: + status, payload = self.raw_reply + handler.send_response(status) + handler.send_header("Content-Type", "application/json") + handler.send_header("Content-Length", str(len(payload))) + handler.send_header("Location", "http://127.0.0.1:9/v1/pools") + handler.end_headers() + handler.wfile.write(payload) + return if method == "GET" and path.startswith("/v1/pools/"): if self.read_reply is not None: handler._reply(200, self.read_reply) @@ -484,6 +494,46 @@ def test_a_token_an_http_header_cannot_encode_is_refused_without_echoing_it(gate pools.connect(gateway.url, "caf\u00e9-secret") +CARRIER = "placement-floor-default-l4-default" +NESTED = b"[" * 100_000 + + +def write_carrier(writer: pools.SdkPools) -> None: + writer.create_pool(CARRIER, {"l4": 0}, bundle="default", minimum_worker_count=1, queue_pool="default") + + +@pytest.mark.parametrize( + ("reply", "message"), + [ + ((200, NESTED), "the gateway returned an unexpected response"), + ], +) +def test_a_pool_reply_the_applier_cannot_use_fails_the_call(gateway, reply, message): + gateway.raw_reply = reply + reader = pools.connect(gateway.url, READ_TOKEN) + try: + with pytest.raises(keda.GatewayError) as raised: + reader.get_pool(CARRIER) + finally: + reader.close() + assert str(raised.value).startswith(message) + with pytest.raises(keda.GatewayError) as raised: + write_carrier(pools.connect(gateway.url, ADMIN_TOKEN)) + assert str(raised.value).startswith(message) + assert [request["method"] for request in gateway.requests] == ["GET", "POST"] + + +def test_a_deeply_nested_pool_reply_fails_only_the_tick(gateway, prometheus, read_token_file, capsys): + gateway.raw_reply = (200, NESTED) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED + errors = [row["error"] for row in rows(capsys.readouterr().out)] + assert errors == [ + f"reading pool {CARRIER!r} failed: the gateway returned an unexpected response", + "reading pool 'placement-floor-default-a100-80gb-generation' failed: " + "the gateway returned an unexpected response", + ] + + def test_a_malformed_pool_reply_fails_only_the_tick(gateway, prometheus, read_token_file, capsys): gateway.read_reply = ["not", "a", "pool"] assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED From 66a01bc79e47ea0434fbce5a325c04b4b8a30c92 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:09:09 +0100 Subject: [PATCH 17/22] fix(placement): check the gateway URL as the Prometheus URL is checked - tune refuses with exit 2, before it builds any client, a --gateway-url that is not an absolute http or https URL with a host, embeds credentials, carries a query or a fragment, or has an invalid port, and never repeats the URL. Credentials in the URL made httpx send Basic auth in place of the bearer token, and an unparsable URL's error quoted its password. - Both URLs share one check, require_base_url. --- packages/sie_placement/README.md | 6 ++-- .../sie_placement/src/sie_placement/cli.py | 1 + .../sie_placement/src/sie_placement/keda.py | 26 ++++++++------- packages/sie_placement/tests/test_cli.py | 33 +++++++++++++++++++ 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 7cf3d5ce6..2daaf00ce 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -249,8 +249,10 @@ The command exits with status 2 before it calls Prometheus or the gateway when: identity is `bridged`, `snapshot-cold` or `scheduled-warm`, warmths the chart does not realize; - a token file or `SIE_ADMIN_TOKEN` holds anything but one run of printable - ASCII characters without spaces after trimming, or the Prometheus URL has an - invalid port. + ASCII characters without spaces after trimming; +- `--prometheus-url` or `--gateway-url` is not an absolute http or https URL + with a host, embeds credentials, carries a query or a fragment, or has an + invalid port. The refusal does not repeat the URL. A failed query, pool read or pool write fails only its tick, which is recorded, and the next tick runs as usual. diff --git a/packages/sie_placement/src/sie_placement/cli.py b/packages/sie_placement/src/sie_placement/cli.py index 03eb2f80a..ccd1df9b9 100644 --- a/packages/sie_placement/src/sie_placement/cli.py +++ b/packages/sie_placement/src/sie_placement/cli.py @@ -135,6 +135,7 @@ def _tune(args: argparse.Namespace) -> int: carriers = keda.carriers(_load_plan(args.plan), max_pool_floor=args.max_pool_floor, max_pools=args.max_pools) keda.series_queries(args.series_selector) prometheus = keda.PrometheusHttp(args.prometheus_url) + keda.require_base_url("gateway", args.gateway_url) read_token = _secret(args.api_key_file, None, "gateway client token") if read_token is None: _secret(None, SDK_API_KEY_ENV, "gateway client token") diff --git a/packages/sie_placement/src/sie_placement/keda.py b/packages/sie_placement/src/sie_placement/keda.py index 70a43e153..167ef842e 100644 --- a/packages/sie_placement/src/sie_placement/keda.py +++ b/packages/sie_placement/src/sie_placement/keda.py @@ -205,19 +205,23 @@ def parse_vector(document: object) -> tuple[Sample, ...]: return tuple(samples) +def require_base_url(label: str, base_url: str) -> None: + parts = urllib.parse.urlsplit(base_url) + if parts.scheme not in {"http", "https"} or not parts.hostname: + raise ValueError(f"the {label} URL must be an absolute http or https URL") + if parts.username is not None or parts.password is not None: + raise ValueError(f"the {label} URL must not embed credentials") + if parts.query or parts.fragment: + raise ValueError(f"the {label} URL must not carry a query or a fragment") + try: + parts.port # noqa: B018 + except ValueError: + raise ValueError(f"the {label} URL has an invalid port") from None + + class PrometheusHttp: def __init__(self, base_url: str, *, timeout_s: float = PROMETHEUS_TIMEOUT_S) -> None: - parts = urllib.parse.urlsplit(base_url) - if parts.scheme not in {"http", "https"} or not parts.hostname: - raise ValueError("the Prometheus URL must be an absolute http or https URL") - if parts.username is not None or parts.password is not None: - raise ValueError("the Prometheus URL must not embed credentials") - if parts.query or parts.fragment: - raise ValueError("the Prometheus URL must not carry a query or a fragment") - try: - parts.port # noqa: B018 - except ValueError: - raise ValueError("the Prometheus URL has an invalid port") from None + require_base_url("Prometheus", base_url) self._endpoint = f"{base_url.rstrip('/')}/api/v1/query" self._timeout_s = timeout_s diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index b9a7f63d7..20c2f6427 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -489,6 +489,39 @@ def test_a_transport_failure_is_reported_without_the_header_it_carried(gateway): assert gateway.pools == {} +PASSWORD = "s3cret-password" # noqa: S105 - a test value + + +@pytest.mark.parametrize( + ("url", "message"), + [ + ("sie-gateway:8080", "must be an absolute http or https URL"), + ("ftp://sie-gateway:8080", "must be an absolute http or https URL"), + ("http:///v1", "must be an absolute http or https URL"), + (f"http://user:{PASSWORD}@sie-gateway:8080", "must not embed credentials"), + (f"http://user:{PASSWORD}@sie-gateway:notaport", "must not embed credentials"), + (f"http://sie-gateway:8080/?token={PASSWORD}", "must not carry a query or a fragment"), + (f"http://sie-gateway:8080/#{PASSWORD}", "must not carry a query or a fragment"), + ("http://sie-gateway:notaport", "has an invalid port"), + ], +) +def test_a_gateway_url_that_is_not_a_plain_base_url_is_refused_without_echoing_it( + gateway, prometheus, read_token_file, monkeypatch, url, message, capsys, caplog +): + caplog.set_level(logging.DEBUG) + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + argv = ["tune", "--plan", str(FIXTURES / "tune-plan.json"), "--prometheus-url", prometheus.url] + argv += ["--gateway-url", url, "--api-key-file", str(read_token_file), "--apply", "--once"] + assert cli.main(argv) == cli.EXIT_REFUSED + captured = capsys.readouterr() + assert f"the gateway URL {message}" in captured.err + for text in (captured.out, captured.err, caplog.text): + assert url not in text + assert PASSWORD not in text + assert gateway.requests == [] + assert prometheus.requests == [] + + def test_a_token_an_http_header_cannot_encode_is_refused_without_echoing_it(gateway): with pytest.raises(ValueError, match=r"^a gateway token holds a character that an HTTP header cannot carry$"): pools.connect(gateway.url, "caf\u00e9-secret") From b77337adaffce3ad501e3758895033eac7c400b1 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:09:30 +0100 Subject: [PATCH 18/22] fix(placement): fail a pool call whose reply is neither a success nor an error - The SDK treats every status below 400 as success, so a proxy answering a pool write with a redirect and a JSON body made the audit row report the write as applied. The applier now gives the SDK its own httpx client, through SIEClient's http_client parameter, with redirects off and a response hook that fails any reply outside 2xx below 400. Error replies still reach the SDK, so get_pool keeps reading a 404 as no pool. - A gateway URL that httpx cannot parse is refused without quoting it. --- .../sie_placement/src/sie_placement/pools.py | 24 ++++++++++++++++- packages/sie_placement/tests/test_cli.py | 26 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/sie_placement/src/sie_placement/pools.py b/packages/sie_placement/src/sie_placement/pools.py index 886cfa29c..dcc50f1c9 100644 --- a/packages/sie_placement/src/sie_placement/pools.py +++ b/packages/sie_placement/src/sie_placement/pools.py @@ -10,14 +10,36 @@ from sie_placement.keda import GatewayError TIMEOUT_S = 10.0 +HTTP_OK_MIN = 200 +HTTP_OK_MAX = 299 +HTTP_ERROR_MIN = 400 _UNEXPECTED_REPLY = "the gateway returned an unexpected response" +def _require_success_or_error(response: httpx.Response) -> None: + status = response.status_code + if status < HTTP_OK_MIN or HTTP_OK_MAX < status < HTTP_ERROR_MIN: + raise GatewayError(f"the gateway answered HTTP {status}, which is neither a success nor an error") + + def _client(base_url: str, api_key: str | None) -> SIEClient: try: - return SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S) + http_client = httpx.Client( + base_url=base_url, + timeout=TIMEOUT_S, + follow_redirects=False, + event_hooks={"response": [_require_success_or_error]}, + ) + except httpx.InvalidURL: + raise ValueError("the gateway URL is not a valid URL") from None + try: + return SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S, http_client=http_client) except UnicodeError: + http_client.close() raise ValueError("a gateway token holds a character that an HTTP header cannot carry") from None + except BaseException: + http_client.close() + raise def _gateway_error(error: Exception) -> GatewayError: diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index 20c2f6427..b4cfef5ec 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -522,6 +522,11 @@ def test_a_gateway_url_that_is_not_a_plain_base_url_is_refused_without_echoing_i assert prometheus.requests == [] +def test_a_gateway_url_httpx_cannot_parse_is_refused_without_echoing_it(): + with pytest.raises(ValueError, match=r"^the gateway URL is not a valid URL$"): + pools.connect(f"http://user:{PASSWORD}@sie-gateway:notaport", READ_TOKEN) + + def test_a_token_an_http_header_cannot_encode_is_refused_without_echoing_it(gateway): with pytest.raises(ValueError, match=r"^a gateway token holds a character that an HTTP header cannot carry$"): pools.connect(gateway.url, "caf\u00e9-secret") @@ -539,6 +544,8 @@ def write_carrier(writer: pools.SdkPools) -> None: ("reply", "message"), [ ((200, NESTED), "the gateway returned an unexpected response"), + ((307, json.dumps({"spec": {"name": CARRIER}}).encode()), "the gateway answered HTTP 307"), + ((302, b"{}"), "the gateway answered HTTP 302"), ], ) def test_a_pool_reply_the_applier_cannot_use_fails_the_call(gateway, reply, message): @@ -567,6 +574,25 @@ def test_a_deeply_nested_pool_reply_fails_only_the_tick(gateway, prometheus, rea ] +def test_a_redirected_write_is_not_recorded_as_applied(gateway, prometheus, read_token_file, monkeypatch, capsys): + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + gateway.auth = False + original = gateway.respond + + def redirect_writes(handler: _Handler, method: str, body: object) -> None: + gateway.raw_reply = (307, json.dumps({"spec": body}).encode()) if method == "POST" else None + original(handler, method, body) + + monkeypatch.setattr(gateway, "respond", redirect_writes) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == cli.EXIT_FAILED + written = [row for row in rows(capsys.readouterr().out) if row["floor"]] + assert written + for row in written: + assert row["applied"] is False + assert "the gateway answered HTTP 307" in row["error"] + assert gateway.pools == {} + + def test_a_malformed_pool_reply_fails_only_the_tick(gateway, prometheus, read_token_file, capsys): gateway.read_reply = ["not", "a", "pool"] assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED From cdfeef9f209c4a0fa52263e214c25d4a3489eb31 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:09:36 +0100 Subject: [PATCH 19/22] docs(placement): say how each kind of gateway call failure is reported - A failed connection is reported with the SDK's connection error message, which carries no header value because no header has been sent, and any other transport failure by its exception type alone. - A reply outside 2xx below 400 fails the call with its status code, and a reply the SDK cannot parse fails it as an unexpected response. --- packages/sie_placement/README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 2daaf00ce..0498d48d1 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -273,9 +273,13 @@ The applier reads it only with `--apply`, sends it only on pool writes to `--gateway-url`, never takes it as a command-line value and never prints or logs it. Without it, `--apply` sends its writes with no credential, which a gateway with token auth refuses. The client token is never sent on writes. A -refused token is named by its source, never by its value, and a failed gateway -call is reported by the server's error message or, for a transport failure, by -the exception type alone, so no reported error carries a header value. +refused token is named by its source, never by its value. A failed gateway call +is reported by the server's error message, a failed connection by the SDK's +connection error message, and any other transport failure by its exception type +alone, so no reported error carries a header value: a connection fails before +any header is sent. A reply that is neither a success nor an error, such as a +redirect, fails the call with its status code, and a reply the SDK cannot parse +fails it as an unexpected response. ## Tests From 56ed3d78b84da969c441ed94a1b8d79e26643fa3 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:32:34 +0100 Subject: [PATCH 20/22] fix(placement): refuse a URL urllib cannot split without repeating it - urlsplit raises a ValueError that quotes the whole netloc, credentials included, when the netloc holds a character that NFKC normalization turns into a separator, such as a fullwidth slash or at sign. require_base_url now reports such a --gateway-url or --prometheus-url as not a valid URL. --- packages/sie_placement/README.md | 4 ++-- packages/sie_placement/src/sie_placement/keda.py | 10 +++++++--- packages/sie_placement/tests/test_cli.py | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index 0498d48d1..f4761707b 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -250,8 +250,8 @@ The command exits with status 2 before it calls Prometheus or the gateway when: does not realize; - a token file or `SIE_ADMIN_TOKEN` holds anything but one run of printable ASCII characters without spaces after trimming; -- `--prometheus-url` or `--gateway-url` is not an absolute http or https URL - with a host, embeds credentials, carries a query or a fragment, or has an +- `--prometheus-url` or `--gateway-url` is not a valid absolute http or https + URL with a host, embeds credentials, carries a query or a fragment, or has an invalid port. The refusal does not repeat the URL. A failed query, pool read or pool write fails only its tick, which is recorded, diff --git a/packages/sie_placement/src/sie_placement/keda.py b/packages/sie_placement/src/sie_placement/keda.py index 167ef842e..2ae451ed3 100644 --- a/packages/sie_placement/src/sie_placement/keda.py +++ b/packages/sie_placement/src/sie_placement/keda.py @@ -206,10 +206,14 @@ def parse_vector(document: object) -> tuple[Sample, ...]: def require_base_url(label: str, base_url: str) -> None: - parts = urllib.parse.urlsplit(base_url) - if parts.scheme not in {"http", "https"} or not parts.hostname: + try: + parts = urllib.parse.urlsplit(base_url) + scheme, hostname, username, password = parts.scheme, parts.hostname, parts.username, parts.password + except ValueError: + raise ValueError(f"the {label} URL is not a valid URL") from None + if scheme not in {"http", "https"} or not hostname: raise ValueError(f"the {label} URL must be an absolute http or https URL") - if parts.username is not None or parts.password is not None: + if username is not None or password is not None: raise ValueError(f"the {label} URL must not embed credentials") if parts.query or parts.fragment: raise ValueError(f"the {label} URL must not carry a query or a fragment") diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index b4cfef5ec..64c9d7148 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -503,6 +503,8 @@ def test_a_transport_failure_is_reported_without_the_header_it_carried(gateway): (f"http://sie-gateway:8080/?token={PASSWORD}", "must not carry a query or a fragment"), (f"http://sie-gateway:8080/#{PASSWORD}", "must not carry a query or a fragment"), ("http://sie-gateway:notaport", "has an invalid port"), + (f"http://user:{PASSWORD}@gw\uff0fx:8080", "is not a valid URL"), + (f"http://user:{PASSWORD}\uff20gw:8080", "is not a valid URL"), ], ) def test_a_gateway_url_that_is_not_a_plain_base_url_is_refused_without_echoing_it( @@ -522,6 +524,20 @@ def test_a_gateway_url_that_is_not_a_plain_base_url_is_refused_without_echoing_i assert prometheus.requests == [] +def test_a_prometheus_url_that_is_not_a_valid_url_is_refused_without_echoing_it(gateway, prometheus, capsys, caplog): + caplog.set_level(logging.DEBUG) + url = f"http://user:{PASSWORD}@prometheus\uff0fx:9090" + argv = ["tune", "--plan", str(FIXTURES / "tune-plan.json"), "--prometheus-url", url] + assert cli.main([*argv, "--gateway-url", gateway.url, "--once"]) == cli.EXIT_REFUSED + captured = capsys.readouterr() + assert "the Prometheus URL is not a valid URL" in captured.err + for text in (captured.out, captured.err, caplog.text): + assert url not in text + assert PASSWORD not in text + assert gateway.requests == [] + assert prometheus.requests == [] + + def test_a_gateway_url_httpx_cannot_parse_is_refused_without_echoing_it(): with pytest.raises(ValueError, match=r"^the gateway URL is not a valid URL$"): pools.connect(f"http://user:{PASSWORD}@sie-gateway:notaport", READ_TOKEN) From e7952b2c2a94491315dba63bd928ac0bedb78d5f Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:32:49 +0100 Subject: [PATCH 21/22] fix(placement): redact the client's own token from reported gateway errors - A gateway call's error text is copied from the reply, so an intermediary that quotes the request's Authorization header in its error body put the token in an audit row. SdkPools now replaces the bearer token its own client sent, the client token on reads and the admin token on writes, with [redacted], and cuts the reported text to 512 characters, which also bounds an audit row against a large error body. --- packages/sie_placement/README.md | 11 +++-- .../sie_placement/src/sie_placement/pools.py | 26 +++++++---- packages/sie_placement/tests/test_cli.py | 43 ++++++++++++++++++- 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index f4761707b..bb444a26f 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -276,10 +276,13 @@ gateway with token auth refuses. The client token is never sent on writes. A refused token is named by its source, never by its value. A failed gateway call is reported by the server's error message, a failed connection by the SDK's connection error message, and any other transport failure by its exception type -alone, so no reported error carries a header value: a connection fails before -any header is sent. A reply that is neither a success nor an error, such as a -redirect, fails the call with its status code, and a reply the SDK cannot parse -fails it as an unexpected response. +alone; a connection fails before any header is sent. A reply that is neither a +success nor an error, such as a redirect, fails the call with its status code, +and a reply the SDK cannot parse fails it as an unexpected response. The +applier cuts a reported error to 512 characters and replaces the token its +client sent with `[redacted]`, so an intermediary that quotes the request's +`Authorization` header in its error body does not put the token in an audit +row. Other text a server returns is reported as it is. ## Tests diff --git a/packages/sie_placement/src/sie_placement/pools.py b/packages/sie_placement/src/sie_placement/pools.py index dcc50f1c9..46750f7a3 100644 --- a/packages/sie_placement/src/sie_placement/pools.py +++ b/packages/sie_placement/src/sie_placement/pools.py @@ -13,6 +13,9 @@ HTTP_OK_MIN = 200 HTTP_OK_MAX = 299 HTTP_ERROR_MIN = 400 +MAX_ERROR_LENGTH = 512 +REDACTED = "[redacted]" +_BEARER = "Bearer " _UNEXPECTED_REPLY = "the gateway returned an unexpected response" @@ -22,7 +25,7 @@ def _require_success_or_error(response: httpx.Response) -> None: raise GatewayError(f"the gateway answered HTTP {status}, which is neither a success nor an error") -def _client(base_url: str, api_key: str | None) -> SIEClient: +def _client(base_url: str, api_key: str | None) -> tuple[SIEClient, str | None]: try: http_client = httpx.Client( base_url=base_url, @@ -33,46 +36,53 @@ def _client(base_url: str, api_key: str | None) -> SIEClient: except httpx.InvalidURL: raise ValueError("the gateway URL is not a valid URL") from None try: - return SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S, http_client=http_client) + client = SIEClient(base_url, api_key=api_key, timeout_s=TIMEOUT_S, http_client=http_client) except UnicodeError: http_client.close() raise ValueError("a gateway token holds a character that an HTTP header cannot carry") from None except BaseException: http_client.close() raise + authorization = http_client.headers.get("Authorization", "") + token = authorization.removeprefix(_BEARER) if authorization.startswith(_BEARER) else "" + return client, token or None -def _gateway_error(error: Exception) -> GatewayError: +def _gateway_error(error: Exception, token: str | None) -> GatewayError: if isinstance(error, httpx.HTTPError): return GatewayError(f"the request failed ({type(error).__name__})") if isinstance(error, AttributeError | RecursionError): return GatewayError(_UNEXPECTED_REPLY) - return GatewayError(str(error)) + message = str(error) + if token: + message = message.replace(token, REDACTED) + return GatewayError(message[:MAX_ERROR_LENGTH]) class SdkPools: def __init__(self, base_url: str, api_key: str | None) -> None: self._base_url = base_url self._api_key = api_key - self._client = _client(base_url, api_key) + self._client, self._token = _client(base_url, api_key) def get_pool(self, name: str) -> Mapping[str, object] | None: try: pool = self._client.get_pool(name) except (SIEError, httpx.HTTPError, ValueError, AttributeError, RecursionError) as error: - raise _gateway_error(error) from None + raise _gateway_error(error, self._token) from None return pool def create_pool( self, name: str, gpus: dict[str, int], *, bundle: str, minimum_worker_count: int, queue_pool: str ) -> None: try: - with _client(self._base_url, self._api_key) as client: + client, _ = _client(self._base_url, self._api_key) + with client: client.create_pool( name, gpus, bundle=bundle, minimum_worker_count=minimum_worker_count, queue_pool=queue_pool ) except (SIEError, httpx.HTTPError, ValueError, AttributeError, RecursionError) as error: - raise _gateway_error(error) from None + raise _gateway_error(error, self._token) from None def close(self) -> None: self._client.close() diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index 64c9d7148..95ed302e5 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -80,13 +80,15 @@ def __init__(self, *, auth: bool = True) -> None: self.fail_reads = False self.read_reply: object | None = None self.raw_reply: tuple[int, bytes] | None = None + self.raw_replies: dict[str, tuple[int, bytes]] = {} def respond(self, handler: _Handler, method: str, body: object) -> None: path = urlsplit(handler.path).path authorization = handler.headers.get("Authorization") self.requests.append({"method": method, "path": path, "authorization": authorization, "body": body}) - if self.raw_reply is not None: - status, payload = self.raw_reply + raw = self.raw_replies.get(method, self.raw_reply) + if raw is not None: + status, payload = raw handler.send_response(status) handler.send_header("Content-Type", "application/json") handler.send_header("Content-Length", str(len(payload))) @@ -609,6 +611,43 @@ def redirect_writes(handler: _Handler, method: str, body: object) -> None: assert gateway.pools == {} +ECHOES = { + "403-json": lambda token: ( + 403, + json.dumps({"detail": {"code": "FORBIDDEN", "message": f"token not allowed: Bearer {token}"}}).encode(), + ), + "502-text": lambda token: (502, f"upstream echoed Authorization: Bearer {token}".encode()), +} + + +@pytest.mark.parametrize("echo", sorted(ECHOES)) +@pytest.mark.parametrize(("method", "token"), [("GET", READ_TOKEN), ("POST", ADMIN_TOKEN)]) +def test_a_reply_that_echoes_the_clients_token_is_redacted_in_the_audit_row( + gateway, prometheus, read_token_file, monkeypatch, method, token, echo, capsys +): + monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) + gateway.raw_replies[method] = ECHOES[echo](token) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == cli.EXIT_FAILED + captured = capsys.readouterr() + errors = [row["error"] for row in rows(captured.out) if row["error"]] + assert errors + assert all("Bearer [redacted]" in error for error in errors) + for secret in (READ_TOKEN, ADMIN_TOKEN): + assert secret not in captured.out + assert secret not in captured.err + + +def test_a_long_gateway_error_is_cut_to_512_characters(gateway, prometheus, read_token_file, capsys): + gateway.raw_replies["GET"] = (502, b"x" * 5000) + assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED + errors = [row["error"] for row in rows(capsys.readouterr().out)] + assert errors + for error in errors: + prefix, message = error.split(" failed: ", 1) + assert prefix.startswith("reading pool ") + assert len(message) == pools.MAX_ERROR_LENGTH + + def test_a_malformed_pool_reply_fails_only_the_tick(gateway, prometheus, read_token_file, capsys): gateway.read_reply = ["not", "a", "pool"] assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED From 440321d761f67ede811cdd7f23a3697646c79f91 Mon Sep 17 00:00:00 2001 From: krisztian-gajdar Date: Fri, 6 Nov 2026 11:58:22 +0100 Subject: [PATCH 22/22] fix(placement): report a gateway error reply by its status alone - Redacting the client's token from the reply text missed any encoding of it: the SDK reports a detail object without a message as its repr, which doubles each backslash, so a token holding one reached the audit row. A pool call that gets an error reply is now reported only by its HTTP status, recorded by the response hook of the applier's own httpx client, and a success reply the SDK cannot parse as an unexpected response. - A message the SDK raises before any reply, such as a connection error, keeps its text, cut to 512 characters with the client's token redacted. --- packages/sie_placement/README.md | 20 +++---- .../sie_placement/src/sie_placement/pools.py | 36 +++++++----- packages/sie_placement/tests/test_cli.py | 56 +++++++++++-------- 3 files changed, 65 insertions(+), 47 deletions(-) diff --git a/packages/sie_placement/README.md b/packages/sie_placement/README.md index bb444a26f..326cbcbf5 100644 --- a/packages/sie_placement/README.md +++ b/packages/sie_placement/README.md @@ -273,16 +273,16 @@ The applier reads it only with `--apply`, sends it only on pool writes to `--gateway-url`, never takes it as a command-line value and never prints or logs it. Without it, `--apply` sends its writes with no credential, which a gateway with token auth refuses. The client token is never sent on writes. A -refused token is named by its source, never by its value. A failed gateway call -is reported by the server's error message, a failed connection by the SDK's -connection error message, and any other transport failure by its exception type -alone; a connection fails before any header is sent. A reply that is neither a -success nor an error, such as a redirect, fails the call with its status code, -and a reply the SDK cannot parse fails it as an unexpected response. The -applier cuts a reported error to 512 characters and replaces the token its -client sent with `[redacted]`, so an intermediary that quotes the request's -`Authorization` header in its error body does not put the token in an audit -row. Other text a server returns is reported as it is. +refused token is named by its source, never by its value. A gateway call that +gets an error reply is reported by its HTTP status alone, and so is a reply +that is neither a success nor an error, such as a redirect; a success reply the +SDK cannot parse is reported as an unexpected response. No text a server +returns reaches an audit row, so an intermediary that quotes the request's +`Authorization` header in its error body cannot put the token there. A failed +connection is reported by the SDK's connection error message, sent before any +header, and any other transport failure by its exception type alone. A message +the SDK raises before any reply is cut to 512 characters, with the token the +client holds replaced by `[redacted]`. ## Tests diff --git a/packages/sie_placement/src/sie_placement/pools.py b/packages/sie_placement/src/sie_placement/pools.py index 46750f7a3..f91556f37 100644 --- a/packages/sie_placement/src/sie_placement/pools.py +++ b/packages/sie_placement/src/sie_placement/pools.py @@ -19,19 +19,25 @@ _UNEXPECTED_REPLY = "the gateway returned an unexpected response" -def _require_success_or_error(response: httpx.Response) -> None: - status = response.status_code - if status < HTTP_OK_MIN or HTTP_OK_MAX < status < HTTP_ERROR_MIN: - raise GatewayError(f"the gateway answered HTTP {status}, which is neither a success nor an error") +class _Replies: + def __init__(self) -> None: + self.status: int | None = None + def __call__(self, response: httpx.Response) -> None: + status = response.status_code + self.status = status + if status < HTTP_OK_MIN or HTTP_OK_MAX < status < HTTP_ERROR_MIN: + raise GatewayError(f"the gateway answered HTTP {status}, which is neither a success nor an error") -def _client(base_url: str, api_key: str | None) -> tuple[SIEClient, str | None]: + +def _client(base_url: str, api_key: str | None) -> tuple[SIEClient, _Replies, str | None]: + replies = _Replies() try: http_client = httpx.Client( base_url=base_url, timeout=TIMEOUT_S, follow_redirects=False, - event_hooks={"response": [_require_success_or_error]}, + event_hooks={"response": [replies]}, ) except httpx.InvalidURL: raise ValueError("the gateway URL is not a valid URL") from None @@ -45,13 +51,15 @@ def _client(base_url: str, api_key: str | None) -> tuple[SIEClient, str | None]: raise authorization = http_client.headers.get("Authorization", "") token = authorization.removeprefix(_BEARER) if authorization.startswith(_BEARER) else "" - return client, token or None + return client, replies, token or None -def _gateway_error(error: Exception, token: str | None) -> GatewayError: +def _gateway_error(error: Exception, status: int | None, token: str | None) -> GatewayError: if isinstance(error, httpx.HTTPError): return GatewayError(f"the request failed ({type(error).__name__})") - if isinstance(error, AttributeError | RecursionError): + if status is not None and status >= HTTP_ERROR_MIN: + return GatewayError(f"the gateway answered HTTP {status}") + if status is not None or isinstance(error, AttributeError | RecursionError): return GatewayError(_UNEXPECTED_REPLY) message = str(error) if token: @@ -63,26 +71,28 @@ class SdkPools: def __init__(self, base_url: str, api_key: str | None) -> None: self._base_url = base_url self._api_key = api_key - self._client, self._token = _client(base_url, api_key) + self._client, self._replies, self._token = _client(base_url, api_key) def get_pool(self, name: str) -> Mapping[str, object] | None: + self._replies.status = None try: pool = self._client.get_pool(name) except (SIEError, httpx.HTTPError, ValueError, AttributeError, RecursionError) as error: - raise _gateway_error(error, self._token) from None + raise _gateway_error(error, self._replies.status, self._token) from None return pool def create_pool( self, name: str, gpus: dict[str, int], *, bundle: str, minimum_worker_count: int, queue_pool: str ) -> None: + replies = None try: - client, _ = _client(self._base_url, self._api_key) + client, replies, _ = _client(self._base_url, self._api_key) with client: client.create_pool( name, gpus, bundle=bundle, minimum_worker_count=minimum_worker_count, queue_pool=queue_pool ) except (SIEError, httpx.HTTPError, ValueError, AttributeError, RecursionError) as error: - raise _gateway_error(error, self._token) from None + raise _gateway_error(error, None if replies is None else replies.status, self._token) from None def close(self) -> None: self._client.close() diff --git a/packages/sie_placement/tests/test_cli.py b/packages/sie_placement/tests/test_cli.py index 95ed302e5..5a0a1fc16 100644 --- a/packages/sie_placement/tests/test_cli.py +++ b/packages/sie_placement/tests/test_cli.py @@ -16,6 +16,7 @@ pytest.importorskip("sie_sdk") from sie_placement import cli, keda, pools +from sie_sdk import SIEError FIXTURES = Path(__file__).parent / "fixtures" READ_TOKEN = "read-token-for-tests" # noqa: S105 - a test value @@ -318,8 +319,7 @@ def test_a_rejected_write_fails_the_tick_and_names_the_pool(gateway, prometheus, assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == 1 l4_row = rows(capsys.readouterr().out)[0] assert l4_row["applied"] is False - assert l4_row["error"].startswith("writing pool 'placement-floor-default-l4-default' failed") - assert "Admin token required" in l4_row["error"] + assert l4_row["error"] == "writing pool 'placement-floor-default-l4-default' failed: the gateway answered HTTP 403" def test_an_envelope_above_the_pool_floor_limit_is_refused_before_any_call( @@ -611,41 +611,49 @@ def redirect_writes(handler: _Handler, method: str, body: object) -> None: assert gateway.pools == {} +BACKSLASH_READ_TOKEN = "read\\token-for-tests" # noqa: S105 - a test value +BACKSLASH_ADMIN_TOKEN = "admin\\token-for-tests" # noqa: S105 - a test value ECHOES = { - "403-json": lambda token: ( + "403-message": lambda token: ( 403, json.dumps({"detail": {"code": "FORBIDDEN", "message": f"token not allowed: Bearer {token}"}}).encode(), ), + "403-detail": lambda token: ( + 403, + json.dumps({"detail": {"code": "FORBIDDEN", "seen": f"Bearer {token}"}}).encode(), + ), "502-text": lambda token: (502, f"upstream echoed Authorization: Bearer {token}".encode()), } @pytest.mark.parametrize("echo", sorted(ECHOES)) -@pytest.mark.parametrize(("method", "token"), [("GET", READ_TOKEN), ("POST", ADMIN_TOKEN)]) -def test_a_reply_that_echoes_the_clients_token_is_redacted_in_the_audit_row( - gateway, prometheus, read_token_file, monkeypatch, method, token, echo, capsys +@pytest.mark.parametrize("method", ["GET", "POST"]) +def test_a_reply_that_echoes_the_clients_token_is_reported_by_its_status_alone( + gateway, prometheus, tmp_path, monkeypatch, method, echo, capsys ): - monkeypatch.setenv("SIE_ADMIN_TOKEN", ADMIN_TOKEN) - gateway.raw_replies[method] = ECHOES[echo](token) - assert tune(gateway, prometheus, "--api-key-file", str(read_token_file), "--apply") == cli.EXIT_FAILED + read_token = tmp_path / "read-token" + read_token.write_text(BACKSLASH_READ_TOKEN + "\n", encoding="utf-8") + monkeypatch.setenv("SIE_ADMIN_TOKEN", BACKSLASH_ADMIN_TOKEN) + gateway.auth = False + status, body = ECHOES[echo](BACKSLASH_READ_TOKEN if method == "GET" else BACKSLASH_ADMIN_TOKEN) + gateway.raw_replies[method] = (status, body) + assert tune(gateway, prometheus, "--api-key-file", str(read_token), "--apply") == cli.EXIT_FAILED captured = capsys.readouterr() errors = [row["error"] for row in rows(captured.out) if row["error"]] assert errors - assert all("Bearer [redacted]" in error for error in errors) - for secret in (READ_TOKEN, ADMIN_TOKEN): - assert secret not in captured.out - assert secret not in captured.err - - -def test_a_long_gateway_error_is_cut_to_512_characters(gateway, prometheus, read_token_file, capsys): - gateway.raw_replies["GET"] = (502, b"x" * 5000) - assert tune(gateway, prometheus, "--api-key-file", str(read_token_file)) == cli.EXIT_FAILED - errors = [row["error"] for row in rows(capsys.readouterr().out)] - assert errors - for error in errors: - prefix, message = error.split(" failed: ", 1) - assert prefix.startswith("reading pool ") - assert len(message) == pools.MAX_ERROR_LENGTH + assert all(error.endswith(f" failed: the gateway answered HTTP {status}") for error in errors) + for secret in (BACKSLASH_READ_TOKEN, BACKSLASH_ADMIN_TOKEN): + for form in (secret, secret.replace("\\", "\\\\")): + assert form not in captured.out + assert form not in captured.err + + +def test_an_error_the_sdk_raises_before_any_reply_is_redacted_and_cut(): + message = f"connection error after sending Bearer {BACKSLASH_ADMIN_TOKEN} " + "x" * 5000 + error = str(pools._gateway_error(SIEError(message), None, BACKSLASH_ADMIN_TOKEN)) + assert BACKSLASH_ADMIN_TOKEN not in error + assert error.startswith("connection error after sending Bearer [redacted] x") + assert len(error) == pools.MAX_ERROR_LENGTH def test_a_malformed_pool_reply_fails_only_the_tick(gateway, prometheus, read_token_file, capsys):