Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8abd4ac
feat(placement): add versioned planner input and plan schemas
krisztian-gajdar Nov 4, 2026
ac80e65
fix(placement): keep number and hour parsing inside the schema contract
krisztian-gajdar Nov 4, 2026
a3f9193
fix(placement): name the plan lane's queue pool queuePool
krisztian-gajdar Nov 4, 2026
2ccf70d
docs(placement): state how telemetry route labels fold into operations
krisztian-gajdar Nov 4, 2026
a516e71
style(placement): format the float-range number check
krisztian-gajdar Nov 4, 2026
c7c9aff
docs(placement): rewrap the lanes paragraph
krisztian-gajdar Nov 4, 2026
73cb814
feat(placement): add the lane tuner decision step
krisztian-gajdar Nov 5, 2026
99a35fa
fix(placement): read an attainment given as its wire string
krisztian-gajdar Nov 5, 2026
18045bf
docs(placement): state how an applier feeds and applies the tuner step
krisztian-gajdar Nov 5, 2026
a05b5ba
feat(placement): add the sie-placement tune KEDA applier
krisztian-gajdar Nov 5, 2026
d99d236
fix(placement): harden the KEDA applier's tokens, series and leases
krisztian-gajdar Nov 5, 2026
818f848
fix(placement): refuse plans the chart cannot run before tuning their…
krisztian-gajdar Nov 5, 2026
89a5790
fix(placement): refuse non-finite numbers in documents built in code
krisztian-gajdar Nov 6, 2026
1d49d34
chore: merge feat/sie-placement-schemas
krisztian-gajdar Nov 6, 2026
f0685c1
chore: merge feat/sie-placement-tuner
krisztian-gajdar Nov 6, 2026
557696f
fix(placement): refuse booleans and non-integers in documents built i…
krisztian-gajdar Nov 6, 2026
751ef8a
chore: merge feat/sie-placement-schemas
krisztian-gajdar Nov 6, 2026
71b3bfc
chore: merge feat/sie-placement-tuner
krisztian-gajdar Nov 6, 2026
22e2192
fix(placement): refuse tuner counts that are not integers
krisztian-gajdar Nov 6, 2026
9415629
chore: merge feat/sie-placement-tuner
krisztian-gajdar Nov 6, 2026
aa3cf17
chore: merge origin/main
krisztian-gajdar Nov 6, 2026
8260460
chore: merge feat/sie-placement-schemas
krisztian-gajdar Nov 6, 2026
93467cb
chore: merge feat/sie-placement-tuner
krisztian-gajdar Nov 6, 2026
baef257
fix(placement): fail only the tick on a pool reply too deep to parse
krisztian-gajdar Nov 6, 2026
66a01bc
fix(placement): check the gateway URL as the Prometheus URL is checked
krisztian-gajdar Nov 6, 2026
b77337a
fix(placement): fail a pool call whose reply is neither a success nor…
krisztian-gajdar Nov 6, 2026
cdfeef9
docs(placement): say how each kind of gateway call failure is reported
krisztian-gajdar Nov 6, 2026
56ed3d7
fix(placement): refuse a URL urllib cannot split without repeating it
krisztian-gajdar Nov 6, 2026
e7952b2
fix(placement): redact the client's own token from reported gateway e…
krisztian-gajdar Nov 6, 2026
440321d
fix(placement): report a gateway error reply by its status alone
krisztian-gajdar Nov 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
source_pkgs =
sie_config
sie_mcp
sie_placement
sie_sdk
sie_server
relative_files = True
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ git switch -c <next-topic-branch> 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 |
Expand Down
291 changes: 291 additions & 0 deletions packages/sie_placement/README.md

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions packages/sie_placement/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[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 = []

[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"

[tool.hatch.build.targets.wheel]
packages = ["src/sie_placement"]

[dependency-groups]
dev = [
"pytest>=9.0.3",
]
Empty file.
183 changes: 183 additions & 0 deletions packages/sie_placement/src/sie_placement/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
"""Command-line entry point of the placement tools."""

from __future__ import annotations

import argparse
import json
import math
import os
import re
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
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


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 _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, 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 _token(value, f"the file {str(path)!r}", label)
if env_name is None:
return 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:
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)
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")
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)
Loading
Loading