"""The launch-policy tables the app's host (JavaScript) reads, generated from the Python that owns them. `strata_control` decides the speed switches (`speed_switches.py`) and which models have a Drafting choice (`drafting.py`). The app's host used to carry its own copy of those tables. It now imports `apps/host/server/policy-generated.mjs`, which this module writes, and a test (`tests/terminal/test_app_policy.py`, and `apps/host/tests/policy-generated.test.mjs` on the JavaScript side) fails when the committed file differs from what `render()` makes now. python -m strata_control.app_policy # rewrite apps/host/server/policy-generated.mjs python -m strata_control.app_policy --check # exit 1 when it is out of date """ from __future__ import annotations import json import sys from pathlib import Path from strata_control import drafting, speed_switches TARGET = Path(__file__).resolve().parents[1] / "apps" / "host" / "server" / "policy-generated.mjs" VECTORS = Path(__file__).resolve().parents[1] / "apps" / "host" / "tests" / "data" / "policy-vectors.json" HEADER = ("// GENERATED by `python -m strata_control.app_policy` from strata_control/speed_switches.py and strata_control/drafting.py. Do not edit it by hand:\n" "// change the Python and regenerate; tests/terminal/test_app_policy.py and apps/host/tests/policy-generated.test.mjs fail when this file is out of date.\n") def _entries(pairs) -> dict: return {name: value for name, value in pairs} def table() -> list[dict]: out = [] for s in speed_switches.app_switches(): row = {"id": s.id, "label": s.label, "hint": s.effect, "default": s.default, "env": _entries(s.env)} if s.legacy: row["legacy"] = _entries(s.legacy) if s.group != "Speed": row["group"] = s.group if s.words: row["words"] = _entries(s.words) out.append(row) return out def render() -> str: rows = ",\n".join(" " + json.dumps(r, ensure_ascii=False, separators=(", ", ": ")) for r in table()) return (HEADER + "/** The speed switches of the app's Expert settings (strata_control/speed_switches.py app_switches()): id, words, default, the environment entries outside the default. */\n" + f"export const SPEED_SWITCH_TABLE = [\n{rows},\n];\n" + "/** The families that have a Drafting choice (strata_control/drafting.py SUPPORTED_FAMILIES). */\n" + f"export const DRAFTING_FAMILIES = {json.dumps(list(drafting.SUPPORTED_FAMILIES))};\n" + "/** The model names the DFlash2 drafter was made for (strata_control/drafting.py _SUPPORTED_NAME), a case-insensitive pattern. */\n" + f"export const DFLASH2_MODEL_PATTERN = {json.dumps(drafting._SUPPORTED_NAME.pattern)};\n") #: Cases the host's copies of the RULES (speedSwitchApplies, resolveDrafter) must answer as Python does. The table above is data and is generated; the rules are code, so the #: answers of the Python are written down here and apps/host/tests/policy-generated.test.mjs runs the host's rules over them. _CARDS = { "rtx4090": {"name": "NVIDIA GeForce RTX 4090", "vendor": "nvidia", "arch": "89", "vram_gb": 24.0}, "rtx3060": {"name": "NVIDIA GeForce RTX 3060", "vendor": "nvidia", "arch": "86", "vram_gb": 12.0}, "rtx4060ti16": {"name": "NVIDIA GeForce RTX 4060 Ti 16GB", "vendor": "nvidia", "arch": "89", "vram_gb": 16.0}, "spark": {"name": "NVIDIA GB10", "vendor": "nvidia", "arch": "121", "vram_gb": 120.0, "uma": True}, "r9700": {"name": "AMD Radeon AI PRO R9700", "vendor": "amd", "arch": "gfx1201", "vram_gb": 32.0}, "rx7900": {"name": "AMD Radeon RX 7900 XTX", "vendor": "amd", "arch": "gfx1100", "vram_gb": 24.0}, "rx9070": {"name": "AMD Radeon RX 9070 XT", "vendor": "amd", "vram_gb": 16.0}, "strix": {"name": "AMD Radeon 8060S", "vendor": "amd", "arch": "gfx1151", "uma": True}, "arc": {"name": "Intel(R) Arc(TM) A750", "vendor": "intel", "vram_gb": 8.0}, "nameonly3060": {"name": "GeForce RTX 3060", "vram_gb": 12.0}, } _MODELS = {"qwen": {}, "qwen36": {"arch": "qwen35moe"}, "qwen_pictures": {"vision": {"exe": "strata-vision"}}, "qwen36_pictures": {"arch": "qwen35moe", "vision": {"exe": "strata-vision"}}} _NAMES = ["qwen3.8-flash-next", "qwen3.8-flash-next-iq3_s", "qwen3.8-flash-next-Q2_0", "qwen3.8-flash-next-unsloth-ud-q4_k_xl", "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31", "qwen3.8-flash-next-gyro-iq4", "qwen3.8-flash-next-coder-iq1_m", "qwen3.8-flash-next-swift-iq3_s", "swift-1.5-iq3_s", "qwen3.6-35b-a3b-ud-q4_k_xl", ""] def vectors() -> dict: switches = [] for model, cfg in _MODELS.items(): for backend in ("cuda", "hip", "sycl", "metal", "cpu", None): for card, g in _CARDS.items(): row = {"model": model, "backend": backend, "card": card, "shows": sorted(s.id for s in speed_switches.applicable(cfg, [g], backend) if not s.terminal_only)} switches.append(row) names = [{"name": n, "supported": bool(drafting.supported_model(n, None))} for n in _NAMES] families = [{"family": f, "supported": bool(drafting.supported_model(None, f))} for f in ("qwen", "gyro", "unsloth", "swift", "coder", "nvfp4")] return {"cards": _CARDS, "models": _MODELS, "switches": switches, "names": names, "families": families} def render_vectors() -> str: return json.dumps(vectors(), indent=1, ensure_ascii=False) + "\n" def main(argv: list[str] | None = None) -> int: argv = sys.argv[1:] if argv is None else argv files = ((TARGET, render()), (VECTORS, render_vectors())) if "--check" in argv: bad = [str(p) for p, text in files if (p.read_text(encoding="utf-8") if p.is_file() else "") != text] for p in bad: print(f"{p} is out of date: run python -m strata_control.app_policy", file=sys.stderr) return 1 if bad else 0 for path, text in files: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(text, encoding="utf-8", newline="\n") print(f"wrote {path}") return 0 if __name__ == "__main__": raise SystemExit(main())