launch: TUI screens, runner events, monitor Connect view, harness check, tests for flags and rules
This commit is contained in:
1 parent
e61e90e555
commit
5fbd539628
11 files changed
+2741
-12
No files matched your search
@@ -148,19 +148,19 @@ def options(port: int, *, tunnel_url: str | None = None) -> list[AccessOption]:
|
||||
cf = find_cloudflared()
|
||||
ts = tailscale_status()
|
||||
out = [AccessOption("local", "This computer only", f"http://127.0.0.1:{port}",
|
||||
"for apps on this computer (OpenCode, Codex, Claude Code...)", True)]
|
||||
"for apps on this computer", True)]
|
||||
out.append(AccessOption("lan", "My local network", f"http://{lan or '<network address>'}:{port}",
|
||||
"other computers at home or in the office" if lan else
|
||||
"other computers on your network; no network address was found on this computer", bool(lan), warn=not lan))
|
||||
"other computers at home or the office" if lan else
|
||||
"no network address found on this computer", bool(lan), warn=not lan))
|
||||
out.append(AccessOption("cloudflare", "Cloudflare tunnel", tunnel_url or "https://<random>.trycloudflare.com",
|
||||
"from anywhere, no router setup · cloudflared found" if cf else "cloudflared is not installed",
|
||||
"from anywhere · cloudflared found" if cf else "cloudflared is not installed",
|
||||
bool(cf), warn=not cf))
|
||||
if ts["running"]:
|
||||
out.append(AccessOption("tailscale", "Tailscale", f"http://{ts['name'] or ts['ip']}:{port}",
|
||||
f"your Tailscale devices · tailnet address {ts['ip']}", True))
|
||||
"your Tailscale devices", True))
|
||||
else:
|
||||
out.append(AccessOption("tailscale", "Tailscale", f"http://<tailnet address>:{port}",
|
||||
"Tailscale is installed but not connected (run: tailscale up)" if ts["installed"] else
|
||||
"Tailscale is not connected" if ts["installed"] else
|
||||
"Tailscale is not installed", False, warn=True))
|
||||
return out
|
||||
|
||||
|
||||
@@ -0,0 +1,354 @@
|
||||
"""The `strata launch` application: the screens in order, the run, and the hand-over to the monitor.
|
||||
|
||||
`run_tui` opens the screens (or, with settings already chosen, goes straight to the run), then the Strata Monitor with the
|
||||
launch-aware extras: a Connect view (key 7) and a quit question (keep serving in the background, or stop).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from rich.text import Text
|
||||
from textual.app import App
|
||||
from textual.binding import Binding
|
||||
|
||||
from strata_cli.launch import access as acc
|
||||
from strata_cli.launch import params as P
|
||||
from strata_cli.launch import settings as S
|
||||
from strata_cli.launch import store
|
||||
from strata_cli.launch.catalog import Catalog
|
||||
from strata_cli.launch.runner import Cancelled, Event, LaunchError, Result, Runner, read_config
|
||||
from strata_cli.launch.screens import (AccessScreen, AdvancedScreen, CardsScreen, ConfirmScreen, ContextScreen, FolderScreen,
|
||||
ModelScreen, ParallelScreen, PicturesScreen, RunScreen, SaveScreen, StartScreen,
|
||||
TuningScreen, flow_for)
|
||||
from strata_cli.launch.widgets import CSS, Glyphs
|
||||
|
||||
SCREENS = {"model": ModelScreen, "cards": CardsScreen, "folder": FolderScreen, "context": ContextScreen, "pictures": PicturesScreen,
|
||||
"parallel": ParallelScreen, "tuning": TuningScreen, "advanced": AdvancedScreen, "access": AccessScreen, "save": SaveScreen}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Ctx:
|
||||
"""Everything the screens share: the catalog, this computer, the choices so far."""
|
||||
|
||||
catalog: Catalog
|
||||
computer: Any
|
||||
rows: list
|
||||
settings: S.LaunchSettings
|
||||
glyphs: Glyphs = field(default_factory=Glyphs)
|
||||
feed: str | None = None
|
||||
notes: list[str] = field(default_factory=list)
|
||||
start_own_files: bool = False
|
||||
picked_in_list: bool = False
|
||||
tune_chosen: bool = False
|
||||
fresh_setup: bool = True
|
||||
engine_note: str = ""
|
||||
engine_flags: list[str] | None = None
|
||||
_adv: list | None = None
|
||||
_help: str | None = None
|
||||
|
||||
# ---- names and warnings
|
||||
def default_name(self) -> str:
|
||||
s = self.settings
|
||||
base = f"{s.model or 'my model'} {S.context_label(s.context)}"
|
||||
names = {p.name for p in store.list_presets()}
|
||||
if base not in names:
|
||||
return base
|
||||
n = 2
|
||||
while f"{base} ({n})" in names:
|
||||
n += 1
|
||||
return f"{base} ({n})"
|
||||
|
||||
def warnings(self) -> list[str]:
|
||||
s = self.settings
|
||||
out = [n.line() for n in S.check_access(s)]
|
||||
out += [n.line() for n in S.check_parallel(s, vram_gb=self.computer.main_vram_gb or None)]
|
||||
out += [n.line() for n in S.check_context(s)]
|
||||
r = next((x for x in self.rows if (x.family, x.model) == (s.family, s.model)), None)
|
||||
if r is not None and r.verdict != "fits":
|
||||
out.append(f"{r.title}: {r.words}.")
|
||||
missing = acc.missing_helper(s.access)
|
||||
if missing:
|
||||
out.append(missing)
|
||||
return out
|
||||
|
||||
# ---- the installed model's configuration (for the Advanced table and the command)
|
||||
def base_config(self) -> dict | None:
|
||||
s = self.settings
|
||||
if not (s.family and s.model):
|
||||
return None
|
||||
path = self.catalog.installed_config(self.catalog.tag_of(s.family, s.model))
|
||||
if path is None:
|
||||
return None
|
||||
try:
|
||||
return read_config(path)
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
|
||||
def engine_exe(self) -> str | None:
|
||||
try:
|
||||
from strata_control import engine_component as ec
|
||||
rep = ec.status()
|
||||
return (rep.get("engine") or {}).get("exe") if rep.get("usable") else None
|
||||
except Exception: # noqa: BLE001
|
||||
return None
|
||||
|
||||
def invalidate_rows(self) -> None:
|
||||
self._adv = None
|
||||
|
||||
def advanced_rows(self) -> list[P.Row]:
|
||||
if self._adv is not None:
|
||||
return self._adv
|
||||
s = self.settings
|
||||
cfg = self.base_config()
|
||||
base_args = list((cfg or {}).get("args") or [])
|
||||
base_env = dict((cfg or {}).get("env") or {})
|
||||
tuned_args, tuned_env = None, None
|
||||
if cfg:
|
||||
try:
|
||||
from strata_control import presets
|
||||
merged, info = presets.overlay(cfg)
|
||||
if info.get("applied"):
|
||||
tuned_args, tuned_env = list(merged.get("args") or []), dict(merged.get("env") or {})
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
exe = self.engine_exe()
|
||||
text = P.engine_help_text(exe) if exe else None
|
||||
if exe and text:
|
||||
self.engine_flags = sorted({p.name for p in P.parse_help(text)})
|
||||
self.engine_note = ""
|
||||
else:
|
||||
self.engine_flags = None
|
||||
self.engine_note = ("The engine is not installed yet, so this list shows only what this launch uses. The full list "
|
||||
"appears after the first launch installs it.") if not exe else \
|
||||
"The engine's own list could not be read here; names are not checked."
|
||||
if not cfg and not s.extra:
|
||||
self.engine_note = self.engine_note or "The model is not installed yet; the defaults and tuned values appear after it is."
|
||||
self._adv = P.build_rows(text, base_args, tuned_args, base_env, tuned_env, s.extra)
|
||||
return self._adv
|
||||
|
||||
def command_text(self) -> str:
|
||||
s = self.settings
|
||||
name = "launch-" + S.slug(s.name or self.default_name())
|
||||
cfg = self.base_config()
|
||||
line = f"strata runtime start --profile {name}"
|
||||
if cfg is None:
|
||||
return line + " (the engine command is shown once the model is installed)"
|
||||
try:
|
||||
from strata_control import tune_knobs
|
||||
from strata_cli.launch.runner import derive_config
|
||||
from serve.server import engine_args
|
||||
import contextlib
|
||||
import io
|
||||
d = derive_config(cfg, s, bool_flags=tune_knobs.BOOL_FLAGS)
|
||||
if s.parallel:
|
||||
d["parallel"] = s.parallel
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
argv = engine_args(d)
|
||||
argv = ["***" if (i and argv[i - 1] in ("--api-key",)) else a for i, a in enumerate(argv)]
|
||||
return f"{Path(str(d.get('exe', 'engine'))).name} {' '.join(str(a) for a in argv)}"
|
||||
except Exception: # noqa: BLE001
|
||||
return line
|
||||
|
||||
|
||||
class LaunchApp(App):
|
||||
TITLE = "strata launch"
|
||||
CSS = CSS
|
||||
|
||||
def __init__(self, ctx: Ctx, *, auto_run: bool = False) -> None:
|
||||
super().__init__()
|
||||
self.ctx = ctx
|
||||
self.history: list[str] = []
|
||||
self.result: Result | None = None
|
||||
self.stopped_after_save = False
|
||||
self.cancel = threading.Event()
|
||||
self.auto_run = auto_run
|
||||
self.runner: Runner | None = None
|
||||
self.run_screen: RunScreen | None = None
|
||||
self.running = False
|
||||
|
||||
def on_mount(self) -> None:
|
||||
if self.auto_run:
|
||||
self.run_launch()
|
||||
else:
|
||||
self.push_screen(StartScreen())
|
||||
|
||||
# ---- navigation
|
||||
def flow(self) -> list[str]:
|
||||
return flow_for(self.ctx)
|
||||
|
||||
def goto(self, step_id: str) -> None:
|
||||
self.current = step_id
|
||||
self.switch_screen(SCREENS[step_id]())
|
||||
|
||||
def begin_setup(self, *, preset: str | None = None, own_files: bool = False) -> None:
|
||||
self.ctx.tune_chosen = False
|
||||
self.ctx.fresh_setup = not preset
|
||||
if preset:
|
||||
s = store.load(preset)
|
||||
self.ctx.settings = s
|
||||
else:
|
||||
self.ctx.settings = S.LaunchSettings()
|
||||
self.ctx.start_own_files = own_files
|
||||
self.ctx.picked_in_list = False
|
||||
self.ctx.invalidate_rows()
|
||||
self.current = "model"
|
||||
self.switch_screen(ModelScreen())
|
||||
|
||||
def advance(self) -> None:
|
||||
flow = self.flow()
|
||||
i = flow.index(self.current) if self.current in flow else -1
|
||||
if i + 1 < len(flow):
|
||||
self.goto(flow[i + 1])
|
||||
|
||||
def back(self) -> None:
|
||||
flow = self.flow()
|
||||
i = flow.index(self.current) if self.current in flow else 0
|
||||
if i > 0:
|
||||
self.goto(flow[i - 1])
|
||||
else:
|
||||
self.current = "start"
|
||||
self.switch_screen(StartScreen())
|
||||
|
||||
# ---- starting
|
||||
def start_preset(self, name: str) -> None:
|
||||
s = store.load(name)
|
||||
self.ctx.settings = s
|
||||
self.run_launch()
|
||||
|
||||
def run_launch(self) -> None:
|
||||
s = self.ctx.settings
|
||||
self.cancel = threading.Event()
|
||||
self.runner = Runner(s, catalog=self.ctx.catalog, emit=self._emit, cancel=self.cancel, feed=self.ctx.feed)
|
||||
screen = RunScreen()
|
||||
self.run_screen = screen
|
||||
self.current = "run"
|
||||
self.switch_screen(screen)
|
||||
self.running = True
|
||||
threading.Thread(target=self._work, daemon=True).start()
|
||||
|
||||
def _emit(self, ev: Event) -> None:
|
||||
screen = self.run_screen
|
||||
if screen is not None:
|
||||
try:
|
||||
self.call_from_thread(screen.on_event, ev)
|
||||
except Exception: # noqa: BLE001 - the app is closing
|
||||
pass
|
||||
|
||||
def _work(self) -> None:
|
||||
screen = self.run_screen
|
||||
try:
|
||||
result = self.runner.run() # type: ignore[union-attr]
|
||||
except Cancelled:
|
||||
self.running = False
|
||||
self.call_from_thread(self._after_cancel)
|
||||
return
|
||||
except LaunchError as e:
|
||||
self.running = False
|
||||
self.call_from_thread(screen.on_failure, f"{e.text} {e.next}".strip())
|
||||
return
|
||||
except Exception as e: # noqa: BLE001
|
||||
self.running = False
|
||||
self.call_from_thread(screen.on_failure, f"Something unexpected went wrong: {type(e).__name__}: {e}")
|
||||
return
|
||||
self.running = False
|
||||
s = self.ctx.settings
|
||||
try:
|
||||
if s.name and store.load(s.name, missing_ok=True) is not None:
|
||||
store.save(s, s.name)
|
||||
store.touch_used(s.name)
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
self.result = result
|
||||
self.call_from_thread(screen.on_finished)
|
||||
self.call_from_thread(self.exit, result)
|
||||
|
||||
def _after_cancel(self) -> None:
|
||||
self.run_screen.on_failure("Stopped. Nothing was started.") # type: ignore[union-attr]
|
||||
|
||||
def stop_run(self, screen) -> None:
|
||||
if not self.running:
|
||||
if screen.failed:
|
||||
self.back_to_start()
|
||||
else:
|
||||
self.exit(None)
|
||||
return
|
||||
|
||||
def done(choice: str | None) -> None:
|
||||
if choice == "stop":
|
||||
self.cancel.set()
|
||||
|
||||
self.push_screen(ConfirmScreen("Stop now?", "Files already downloaded are kept; a download continues where it stopped next time. "
|
||||
"Nothing is started.", [("stop", "Stop"), ("go", "Keep going")]), done)
|
||||
|
||||
def back_to_start(self) -> None:
|
||||
self.current = "start"
|
||||
self.switch_screen(StartScreen())
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------------------------ the monitor
|
||||
|
||||
def _info_for(result: Result, s: S.LaunchSettings) -> dict:
|
||||
cfg = {}
|
||||
try:
|
||||
cfg = read_config(Path(result.config))
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
return {"url": result.urls[0] if result.urls else f"http://127.0.0.1:{s.port}", "urls": result.urls, "key": result.key,
|
||||
"model": cfg.get("model_name") or "strata", "name": s.name, "profile": result.profile, "running": True,
|
||||
"tunnel": result.tunnel}
|
||||
|
||||
|
||||
def run_monitor(info: dict, *, keep_default: bool = True, color: str = "auto") -> int:
|
||||
"""Open the monitor for a running launch. q asks whether to keep serving or stop."""
|
||||
from strata_cli.client import Endpoint
|
||||
from strata_cli.launch.monitor_ext import LaunchMonitor
|
||||
local = next((u for u in info.get("urls", []) if "127.0.0.1" in u), None) or f"http://127.0.0.1:{info.get('port', S.DEFAULT_PORT)}"
|
||||
endpoint = Endpoint(local, info.get("key"), 3.0, "")
|
||||
app = LaunchMonitor(endpoint, None, info=info, color=color)
|
||||
app.run()
|
||||
if app.stop_requested:
|
||||
from strata_cli.launch.runner import Runner as R
|
||||
from strata_control import runtime
|
||||
try:
|
||||
runtime.stop(info["profile"], timeout=180)
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"strata: could not stop the server: {e}", file=sys.stderr)
|
||||
return 6
|
||||
acc.stop_tunnel(info["profile"])
|
||||
print("Stopped. The model is unloaded and the graphics memory is free.")
|
||||
else:
|
||||
print(f"Still serving at {info['url']}. Watch it again: strata monitor --url {info['url']} Stop: strata launch --stop")
|
||||
return 0
|
||||
|
||||
|
||||
def run_tui(settings: S.LaunchSettings | None, *, catalog: Catalog | None = None, feed: str | None = None, auto_run: bool = False,
|
||||
save: bool = False) -> int:
|
||||
"""Open the launch screens. `settings` given and `auto_run`: skip the questions and go straight to the run."""
|
||||
catalog = catalog or Catalog()
|
||||
print("Looking at this computer…", flush=True)
|
||||
computer = catalog.computer()
|
||||
rows = catalog.rows(computer.raw)
|
||||
from strata_cli.launch.cmd import apply_defaults
|
||||
s = settings or S.LaunchSettings()
|
||||
notes: list[S.Notice] = []
|
||||
if settings is not None:
|
||||
s, notes = apply_defaults(s, catalog)
|
||||
if not s.name:
|
||||
s.name = f"{s.model or 'model'} {S.context_label(s.context)}"
|
||||
if save:
|
||||
store.save(s, s.name)
|
||||
ctx = Ctx(catalog=catalog, computer=computer, rows=rows, settings=s, glyphs=Glyphs(), feed=feed)
|
||||
app = LaunchApp(ctx, auto_run=auto_run)
|
||||
result = app.run()
|
||||
if app.stopped_after_save:
|
||||
print(f"Saved the preset '{ctx.settings.name}'. Start it with: strata launch --preset \"{ctx.settings.name}\"")
|
||||
return 0
|
||||
if result is None:
|
||||
return 0
|
||||
return run_monitor(_info_for(result, ctx.settings))
|
||||
@@ -70,12 +70,12 @@ def verdict(on_this_pc: str | None) -> tuple[str, str]:
|
||||
if not text:
|
||||
return TIGHT, "not checked (this computer's memory could not be read)"
|
||||
if low.startswith("does not fit"):
|
||||
return NO, "does not fit (you can still try)"
|
||||
return NO, "does not fit (can still try)"
|
||||
if "low-ram mode" in low:
|
||||
return FITS, "fits (slower: part of it stays in RAM or on the SSD)"
|
||||
return FITS, "fits, a bit slower"
|
||||
if "part of its experts" in low or "read from the ssd" in low:
|
||||
base = "slow here" in low
|
||||
return TIGHT, ("tight: part is read from the SSD, noticeably slower" if base else "fits, with part read from the SSD")
|
||||
return (TIGHT, "tight: much read from SSD") if base else (FITS, "fits, part read from SSD")
|
||||
if low.startswith("tight"):
|
||||
return TIGHT, "tight: close other programs"
|
||||
if "fits" in low:
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
"""Check my harness: send one simple prompt and one tool call to the running server the way an app configured by
|
||||
`strata connect` would, and say in plain words whether each worked.
|
||||
|
||||
The check uses the harness's own wire format (chat completions, responses, or messages) with the address, key and model name its
|
||||
config carries, so it proves the settings `strata connect` prints reach a server that answers correctly. It does not start the app
|
||||
itself. The prompt's answer text is checked (it must contain the expected word) and the tool call's arguments are checked (the
|
||||
city must be named), not just the HTTP status.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from dataclasses import dataclass
|
||||
|
||||
#: harness id -> the endpoint family it speaks (the others use chat completions)
|
||||
WIRE = {"codex": "responses", "claude-code": "messages"}
|
||||
|
||||
PROMPT = "Reply with exactly one word: pong"
|
||||
TOOL_PROMPT = "What is the weather in Paris? Use the get_weather tool."
|
||||
TOOL_SCHEMA = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Check:
|
||||
name: str # "prompt" | "tool call"
|
||||
ok: bool
|
||||
detail: str # plain words
|
||||
seconds: float = 0.0
|
||||
|
||||
|
||||
def _post(url: str, key: str, body: dict, headers: dict | None = None, timeout: float = 240.0) -> dict:
|
||||
h = {"Content-Type": "application/json", "Accept": "application/json", **(headers or {})}
|
||||
if key:
|
||||
h["Authorization"] = "Bearer " + key
|
||||
h["x-api-key"] = key
|
||||
req = urllib.request.Request(url, data=json.dumps(body).encode("utf-8"), headers=h, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode("utf-8", "replace"))
|
||||
|
||||
|
||||
def wire_for(harness_id: str) -> str:
|
||||
return WIRE.get(harness_id, "chat")
|
||||
|
||||
|
||||
def _prompt_request(wire: str, base: str, model: str) -> tuple[str, dict]:
|
||||
if wire == "responses":
|
||||
return base + "/v1/responses", {"model": model, "input": PROMPT, "max_output_tokens": 256}
|
||||
if wire == "messages":
|
||||
return base + "/v1/messages", {"model": model, "max_tokens": 256, "messages": [{"role": "user", "content": PROMPT}]}
|
||||
return base + "/v1/chat/completions", {"model": model, "max_tokens": 256, "messages": [{"role": "user", "content": PROMPT}]}
|
||||
|
||||
|
||||
def _tool_request(wire: str, base: str, model: str) -> tuple[str, dict]:
|
||||
if wire == "responses":
|
||||
return base + "/v1/responses", {"model": model, "input": TOOL_PROMPT, "max_output_tokens": 512, "tool_choice": "auto",
|
||||
"tools": [{"type": "function", "name": "get_weather", "description": "Weather of a city",
|
||||
"parameters": TOOL_SCHEMA}]}
|
||||
if wire == "messages":
|
||||
return base + "/v1/messages", {"model": model, "max_tokens": 512, "messages": [{"role": "user", "content": TOOL_PROMPT}],
|
||||
"tools": [{"name": "get_weather", "description": "Weather of a city", "input_schema": TOOL_SCHEMA}]}
|
||||
return base + "/v1/chat/completions", {"model": model, "max_tokens": 512, "messages": [{"role": "user", "content": TOOL_PROMPT}],
|
||||
"tools": [{"type": "function", "function": {"name": "get_weather", "description": "Weather of a city",
|
||||
"parameters": TOOL_SCHEMA}}]}
|
||||
|
||||
|
||||
def answer_text(wire: str, data: dict) -> str:
|
||||
if wire == "responses":
|
||||
if isinstance(data.get("output_text"), str):
|
||||
return data["output_text"]
|
||||
parts = []
|
||||
for item in data.get("output") or []:
|
||||
for c in item.get("content") or []:
|
||||
if isinstance(c, dict) and c.get("text"):
|
||||
parts.append(c["text"])
|
||||
return "".join(parts)
|
||||
if wire == "messages":
|
||||
return "".join(b.get("text", "") for b in data.get("content") or [] if isinstance(b, dict) and b.get("type") == "text")
|
||||
try:
|
||||
return data["choices"][0]["message"].get("content") or ""
|
||||
except (KeyError, IndexError, TypeError, AttributeError):
|
||||
return ""
|
||||
|
||||
|
||||
def tool_calls(wire: str, data: dict) -> list[tuple[str, dict]]:
|
||||
out: list[tuple[str, dict]] = []
|
||||
try:
|
||||
if wire == "responses":
|
||||
for item in data.get("output") or []:
|
||||
if item.get("type") == "function_call":
|
||||
out.append((item.get("name", ""), json.loads(item.get("arguments") or "{}")))
|
||||
elif wire == "messages":
|
||||
for b in data.get("content") or []:
|
||||
if isinstance(b, dict) and b.get("type") == "tool_use":
|
||||
out.append((b.get("name", ""), b.get("input") or {}))
|
||||
else:
|
||||
for tc in data["choices"][0]["message"].get("tool_calls") or []:
|
||||
fn = tc.get("function") or {}
|
||||
out.append((fn.get("name", ""), json.loads(fn.get("arguments") or "{}")))
|
||||
except (KeyError, IndexError, TypeError, ValueError, AttributeError):
|
||||
return out
|
||||
return out
|
||||
|
||||
|
||||
def run_checks(harness_id: str, url: str, key: str, model: str, *, timeout: float = 240.0, poster=_post) -> list[Check]:
|
||||
"""The two checks for one harness. `poster` is replaceable for tests."""
|
||||
base = url.rstrip("/")
|
||||
wire = wire_for(harness_id)
|
||||
checks: list[Check] = []
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
u, body = _prompt_request(wire, base, model)
|
||||
data = poster(u, key, body, None, timeout)
|
||||
text = answer_text(wire, data).strip()
|
||||
ok = "pong" in text.lower()
|
||||
checks.append(Check("prompt", ok, f"It answered: {text[:60]!r}." if ok else
|
||||
(f"It answered {text[:60]!r}, not the word asked for." if text else "The server replied but with no text."),
|
||||
time.monotonic() - t0))
|
||||
except Exception as e: # noqa: BLE001
|
||||
checks.append(Check("prompt", False, _why(e), time.monotonic() - t0))
|
||||
t1 = time.monotonic()
|
||||
try:
|
||||
u, body = _tool_request(wire, base, model)
|
||||
data = poster(u, key, body, None, timeout)
|
||||
calls = tool_calls(wire, data)
|
||||
hit = next(((n, a) for n, a in calls if n == "get_weather" and "paris" in json.dumps(a).lower()), None)
|
||||
if hit:
|
||||
checks.append(Check("tool call", True, f"It called {hit[0]} with {json.dumps(hit[1])}.", time.monotonic() - t1))
|
||||
elif calls:
|
||||
checks.append(Check("tool call", False, f"It called {calls[0][0]} with {json.dumps(calls[0][1])[:60]}, not the city asked for.",
|
||||
time.monotonic() - t1))
|
||||
else:
|
||||
checks.append(Check("tool call", False, "The model answered in words and did not call the tool.", time.monotonic() - t1))
|
||||
except Exception as e: # noqa: BLE001
|
||||
checks.append(Check("tool call", False, _why(e), time.monotonic() - t1))
|
||||
return checks
|
||||
|
||||
|
||||
def _why(e: Exception) -> str:
|
||||
if isinstance(e, urllib.error.HTTPError):
|
||||
if e.code in (401, 403):
|
||||
return "The server refused the key. Check the API key in the config."
|
||||
if e.code == 404:
|
||||
return "The server does not have that address. Check the server address and the app's endpoint type."
|
||||
return f"The server answered with an error (HTTP {e.code})."
|
||||
if isinstance(e, urllib.error.URLError):
|
||||
return "The server could not be reached. Is it running (strata launch --status)?"
|
||||
return f"The check could not finish: {type(e).__name__}."
|
||||
@@ -0,0 +1,255 @@
|
||||
"""The Strata Monitor as `strata launch` opens it: the same monitor plus a Connect view and a quit question.
|
||||
|
||||
Everything about the monitor itself (the views, colours, polling, key hints) is `strata_cli.monitor.MonitorApp`, unchanged.
|
||||
This subclass adds
|
||||
* key 7: the Connect view (the address, the API key hint and copy-ready setup for each app, with `c` copy, `w` write the config for
|
||||
you after a backup, `h` check that the app's settings reach a server that answers a prompt and a tool call correctly);
|
||||
* q: asks whether to keep serving in the background or stop (stopping frees the graphics memory).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
|
||||
from rich.text import Text
|
||||
from textual.binding import Binding
|
||||
from textual.widgets import Static
|
||||
|
||||
from strata_cli.launch import check, harness
|
||||
from strata_cli.launch.screens import ConfirmScreen
|
||||
from strata_cli.launch.widgets import ACCENT, BAD, MUTE, WARN, Glyphs
|
||||
from strata_cli.monitor import MonitorApp
|
||||
from strata_cli.render import VIEWS, safe
|
||||
|
||||
|
||||
class LaunchMonitor(MonitorApp):
|
||||
BINDINGS = MonitorApp.BINDINGS + [Binding("7", "connect", "Connect view", show=False)]
|
||||
|
||||
def __init__(self, endpoint, snapshot, *, info: dict, **kw) -> None:
|
||||
super().__init__(endpoint, snapshot, **kw)
|
||||
self.info = info
|
||||
self.connect_open = False
|
||||
self.connect_index = 0
|
||||
self.show_key = False
|
||||
self.check_results: dict[str, list[check.Check] | str] = {}
|
||||
self.glyphs = Glyphs()
|
||||
self.stop_requested = False
|
||||
self.notice = ""
|
||||
|
||||
# ---- quit asks
|
||||
def action_quit(self) -> None:
|
||||
if self.closing:
|
||||
return
|
||||
|
||||
def done(choice: str | None) -> None:
|
||||
if choice == "keep":
|
||||
self.closing = True
|
||||
self.exit()
|
||||
elif choice == "stop":
|
||||
self.stop_requested = True
|
||||
self.closing = True
|
||||
self.exit()
|
||||
|
||||
self.push_screen(ConfirmScreen("Leave the monitor?", "Keeping it serving lets your apps go on using the model. "
|
||||
"Stopping unloads the model and frees the graphics memory.",
|
||||
[("keep", "Keep serving in the background"), ("stop", "Stop serving and free the memory"),
|
||||
("cancel", "Stay in the monitor")]), done)
|
||||
|
||||
# ---- the Connect view
|
||||
def _harness(self) -> harness.Harness:
|
||||
return harness.HARNESSES[self.connect_index % len(harness.HARNESSES)]
|
||||
|
||||
def action_connect(self) -> None:
|
||||
self.connect_open = not self.connect_open
|
||||
self.notice = ""
|
||||
self._paint()
|
||||
|
||||
def _set_view(self, index: int) -> None:
|
||||
self.connect_open = False
|
||||
super()._set_view(index)
|
||||
|
||||
def action_next_row(self) -> None:
|
||||
if self.connect_open:
|
||||
self.connect_index = (self.connect_index + 1) % len(harness.HARNESSES)
|
||||
self.notice = ""
|
||||
self._paint()
|
||||
else:
|
||||
super().action_next_row()
|
||||
|
||||
def action_previous_row(self) -> None:
|
||||
if self.connect_open:
|
||||
self.connect_index = (self.connect_index - 1) % len(harness.HARNESSES)
|
||||
self.notice = ""
|
||||
self._paint()
|
||||
else:
|
||||
super().action_previous_row()
|
||||
|
||||
def action_filter_clear(self) -> None:
|
||||
if self.connect_open:
|
||||
self.connect_open = False
|
||||
self._paint()
|
||||
else:
|
||||
super().action_filter_clear()
|
||||
|
||||
def action_request_detail(self) -> None:
|
||||
if not self.connect_open:
|
||||
super().action_request_detail()
|
||||
|
||||
def action_next_view(self) -> None:
|
||||
if self.connect_open:
|
||||
self.connect_open = False
|
||||
self._paint()
|
||||
super().action_next_view()
|
||||
|
||||
def on_key(self, event) -> None:
|
||||
if not self.connect_open:
|
||||
return
|
||||
key = event.key
|
||||
if key == "c":
|
||||
self._copy()
|
||||
elif key == "w":
|
||||
self._write()
|
||||
elif key == "k":
|
||||
self.show_key = not self.show_key
|
||||
self._paint()
|
||||
elif key == "h":
|
||||
self._check()
|
||||
else:
|
||||
return
|
||||
event.stop()
|
||||
|
||||
def _rendered(self) -> dict:
|
||||
h = self._harness()
|
||||
key = self.info.get("key") or ""
|
||||
return harness.render(h, url=self.info["url"], key=key or "<your API key>", model=self.info.get("model") or "strata")
|
||||
|
||||
def _copy(self) -> None:
|
||||
out = self._rendered()
|
||||
text = "\n\n".join(f"# {f['path']}\n{f['text']}" for f in out["files"])
|
||||
if out["env"]:
|
||||
text += "\n\n" + "\n".join(f"{k}={v}" for k, v in out["env"].items())
|
||||
try:
|
||||
self.copy_to_clipboard(text)
|
||||
self.notice = "Copied (if your terminal allows it)."
|
||||
except Exception: # noqa: BLE001
|
||||
self.notice = "! Your terminal does not allow copying; use: strata connect " + self._harness().id
|
||||
self._paint()
|
||||
|
||||
def _write(self) -> None:
|
||||
h = self._harness()
|
||||
out = self._rendered()
|
||||
if not out["files"]:
|
||||
self.notice = "! Nothing to write for this app."
|
||||
self._paint()
|
||||
return
|
||||
|
||||
def done(choice: str | None) -> None:
|
||||
if choice != "write":
|
||||
return
|
||||
lines = []
|
||||
for f in out["files"]:
|
||||
try:
|
||||
r = harness.write_file(f)
|
||||
lines.append(f"{r['action']} {r['path']}" + (f" (backup {r['backup']})" if r["backup"] else ""))
|
||||
except OSError as e:
|
||||
lines.append(f"could not write {f['path']}: {e}")
|
||||
self.notice = "; ".join(lines)
|
||||
self._paint()
|
||||
|
||||
self.push_screen(ConfirmScreen(f"Write {h.title}'s settings?", "A file that exists is copied to a backup first.\n"
|
||||
+ "\n".join(f" {f['path']}" for f in out["files"]),
|
||||
[("write", "Write it"), ("no", "Not now")]), done)
|
||||
|
||||
def _check(self) -> None:
|
||||
h = self._harness()
|
||||
self.check_results[h.id] = "running"
|
||||
self.notice = "Checking: one prompt and one tool call. This can take a minute on a big model."
|
||||
self._paint()
|
||||
key = self.info.get("key") or ""
|
||||
url = self.info["url"]
|
||||
model = self.info.get("model") or "strata"
|
||||
|
||||
def work() -> None:
|
||||
res = check.run_checks(h.id, url, key, model)
|
||||
self.check_results[h.id] = res
|
||||
try:
|
||||
self.call_from_thread(self._paint)
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
|
||||
threading.Thread(target=work, daemon=True).start()
|
||||
|
||||
# ---- painting
|
||||
def _paint(self, size=None) -> None:
|
||||
if not getattr(self, "connect_open", False):
|
||||
return super()._paint(size)
|
||||
size = self.screen.size if size is None else size
|
||||
width = max(40, size.width - 2)
|
||||
self.query_one("#content", Static).update(self.connect_text(width))
|
||||
idle = "c copy w write it for me (keeps a backup) h check it 1-7 views q quit (asks: keep serving / stop)"
|
||||
msg = self.notice or idle
|
||||
self.query_one("#message", Static).update(Text(msg, style=WARN if msg.startswith("!") else ""))
|
||||
|
||||
def connect_text(self, width: int) -> Text:
|
||||
g = self.glyphs
|
||||
state = {"connected": "[LIVE]", "stale": "[STALE]", "connecting": "[CONNECTING]", "disconnected": "[DISCONNECTED]",
|
||||
"auth_required": "[AUTH REQUIRED]"}.get(self.connection_state, "[LIVE]")
|
||||
t = Text(no_wrap=True, overflow="crop")
|
||||
head = f"STRATA Connect {self.info['url']}"
|
||||
t.append(head, style=f"bold {ACCENT}")
|
||||
t.append(" " * max(2, width - len(head) - len(state)) + state + "\n", style=f"bold {ACCENT}")
|
||||
nav = []
|
||||
for i, name in enumerate(VIEWS):
|
||||
nav.append(f"{i + 1} {name.title()}")
|
||||
nav.append("[7 Connect]")
|
||||
t.append(" ".join(nav) + "\n")
|
||||
t.append(g.rule * min(width, 100) + "\n", style=MUTE)
|
||||
key = self.info.get("key") or ""
|
||||
shown = key if self.show_key else (key[:10] + g.mask * 4 if key else "off")
|
||||
t.append("Connect an app", style=f"bold {ACCENT}")
|
||||
t.append(f" URL {self.info['url']}/v1 model {self.info.get('model') or 'strata'} key {shown} ")
|
||||
t.append("(k hide)" if self.show_key else "(k show)", style=MUTE)
|
||||
t.append("\n")
|
||||
for u in self.info.get("urls", [])[1:]:
|
||||
t.append(f" also reachable at {u}/v1\n", style=MUTE)
|
||||
t.append("\n " + "App".ljust(20) + "Works with".ljust(22) + "Status\n", style="bold")
|
||||
for i, h in enumerate(harness.HARNESSES):
|
||||
sel = i == self.connect_index % len(harness.HARNESSES)
|
||||
res = self.check_results.get(h.id)
|
||||
status = h.status + (f" {g.tick}" if h.tested else "")
|
||||
style = ""
|
||||
if res == "running":
|
||||
status, style = "checking…", WARN
|
||||
elif isinstance(res, list):
|
||||
okn = sum(1 for c in res if c.ok)
|
||||
status, style = (f"works {g.tick} ({okn}/{len(res)} checks)" if okn == len(res) else f"problem ({okn}/{len(res)} checks)"), \
|
||||
(ACCENT if okn == len(res) else BAD)
|
||||
row = (" " + (g.cursor + " " if sel else " ")) + h.title.ljust(20) + h.speaks.ljust(22) + status
|
||||
t.append(row.ljust(width - 1) + "\n", style=(f"bold {ACCENT} on #1f2a27" if sel else style))
|
||||
h = self._harness()
|
||||
out = self._rendered()
|
||||
t.append("\n")
|
||||
paths = ", ".join(f["path"] for f in out["files"]) or ("built into the Strata app" if h.built_in else "")
|
||||
t.append(f"{h.title} setup", style=f"bold {ACCENT}")
|
||||
t.append(f" {paths}\n", style=MUTE)
|
||||
if not h.tested and not h.built_in:
|
||||
t.append(" Not tested with this app yet: a starting point, not a promise.\n", style=WARN)
|
||||
for n in h.notes:
|
||||
t.append(f" {n}\n", style=MUTE)
|
||||
shown_lines = []
|
||||
for f in out["files"][:1]:
|
||||
body = f["text"] if self.show_key else harness.mask(f["text"], key)
|
||||
shown_lines += body.splitlines()
|
||||
for k, v in out["env"].items():
|
||||
shown_lines.append(f"{k}={v if self.show_key else harness.mask(v, key)}")
|
||||
room = max(3, 0)
|
||||
for line in shown_lines[:14]:
|
||||
t.append(" " + line + "\n")
|
||||
if len(shown_lines) > 14:
|
||||
t.append(f" … {len(shown_lines) - 14} more lines (c copies all)\n", style=MUTE)
|
||||
res = self.check_results.get(h.id)
|
||||
if isinstance(res, list):
|
||||
t.append("\n")
|
||||
for c in res:
|
||||
t.append(f" {g.tick if c.ok else 'x'} {c.name}: {c.detail}\n", style=ACCENT if c.ok else BAD)
|
||||
return t
|
||||
File diff suppressed because it is too large.
Load diff
@@ -276,7 +276,7 @@ def flags_text(s: LaunchSettings, *, preset: str | None = None) -> str:
|
||||
|
||||
def slug(name: str) -> str:
|
||||
"""A name safe for profile ids and file names: lowercase letters, digits, - and _, starting with a letter."""
|
||||
text = re.sub(r"[^a-z0-9_-]+", "-", name.lower()).strip("-_") or "launch"
|
||||
text = re.sub(r"-{2,}", "-", re.sub(r"[^a-z0-9_-]+", "-", name.lower())).strip("-_") or "launch"
|
||||
if not text[0].isalpha():
|
||||
text = "l" + text
|
||||
return text[:48]
|
||||
@@ -60,7 +60,8 @@ def _private_write(path: Path, payload: bytes) -> None:
|
||||
|
||||
|
||||
def now_text() -> str:
|
||||
return time.strftime("%Y-%m-%d %H:%M")
|
||||
now = time.time()
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) + f".{int((now % 1) * 1000000):06d}"
|
||||
|
||||
|
||||
def save(settings: LaunchSettings, name: str | None = None) -> LaunchSettings:
|
||||
|
||||
@@ -83,7 +83,7 @@ def cell(c: Cell | str) -> Cell:
|
||||
class Menu(Static):
|
||||
"""A column list with a cursor: `items` is a list of `Item`, `columns` a list of (title, width) (width 0 = the rest)."""
|
||||
|
||||
DEFAULT_CSS = "Menu { height: auto; }"
|
||||
DEFAULT_CSS = "Menu { height: auto; text-wrap: nowrap; text-overflow: clip; }"
|
||||
|
||||
def __init__(self, columns: list[tuple[str, int]] | None = None, *, glyphs: Glyphs | None = None, id: str | None = None,
|
||||
window: int = 0) -> None:
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Shared fixtures for the `strata launch` tests: an isolated home, a fake computer, and a text dump of a Textual screen."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
HW_12 = {"ram_gb": 64.0, "os": "Test OS", "cpu": {"avx2": True, "name": "test cpu"},
|
||||
"gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 5070", "vram_gb": 12.0, "usable": True, "vendor": "nvidia"}]}
|
||||
HW_24 = {**HW_12, "gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 4090", "vram_gb": 24.0, "usable": True, "vendor": "nvidia"}]}
|
||||
HW_2X16 = {**HW_12, "gpus": [{"index": 0, "name": "NVIDIA RTX A4000", "vram_gb": 16.0, "usable": True, "vendor": "nvidia"},
|
||||
{"index": 1, "name": "NVIDIA RTX A4000", "vram_gb": 16.0, "usable": True, "vendor": "nvidia"}]}
|
||||
HW_32GB_RAM = {**HW_12, "ram_gb": 32.0}
|
||||
|
||||
|
||||
class Home:
|
||||
"""A throw-away per-user home: profiles, presets, launch presets, state, components and install folder."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.tmp = tempfile.TemporaryDirectory(prefix="strata-launch-test-")
|
||||
base = Path(self.tmp.name)
|
||||
self.base = base
|
||||
self.env = {
|
||||
"STRATA_PROFILE_DIR": str(base / "config" / "profiles"),
|
||||
"STRATA_PRESET_DIR": str(base / "config" / "presets"),
|
||||
"STRATA_LAUNCH_PRESET_DIR": str(base / "config" / "launch-presets"),
|
||||
"STRATA_COMPONENTS_DIR": str(base / "components"),
|
||||
"STRATA_INSTALL_DIR": str(base / "install"),
|
||||
"STRATA_RUNTIME_DIR": str(base / "runtime"),
|
||||
"XDG_STATE_HOME": str(base / "state"),
|
||||
"LOCALAPPDATA": str(base / "local"),
|
||||
"HOME": str(base / "home"),
|
||||
"USERPROFILE": str(base / "home"),
|
||||
}
|
||||
(base / "home").mkdir(parents=True, exist_ok=True)
|
||||
self.patch = mock.patch.dict(os.environ, self.env)
|
||||
|
||||
def __enter__(self) -> "Home":
|
||||
self.patch.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
self.patch.stop()
|
||||
self.tmp.cleanup()
|
||||
|
||||
|
||||
def catalog(hw: dict | None = None, root: Path | None = None):
|
||||
from strata_cli.launch.catalog import Catalog
|
||||
return Catalog(root=root or Path(os.environ["STRATA_INSTALL_DIR"]), hardware=hw or HW_12)
|
||||
|
||||
|
||||
def text_of(app) -> str:
|
||||
"""What the current screen shows, one line per terminal row (for assertions and the text previews)."""
|
||||
strips = app.screen._compositor.render_strips()
|
||||
return "\n".join(s.text.rstrip() for s in strips)
|
||||
|
||||
|
||||
def run_cli(argv: list[str]) -> tuple[int, str, str]:
|
||||
from strata_cli.cli import main
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
try:
|
||||
code = main(argv)
|
||||
except SystemExit as e:
|
||||
code = int(e.code or 0)
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
@@ -0,0 +1,558 @@
|
||||
"""`strata launch` without screens: the settings rules, the catalog, presets, parameters, access, connect, and every flag."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from strata_cli.launch import access, harness, params, settings as S, store
|
||||
from strata_cli.launch.catalog import Catalog, CatalogUnavailable, fit_on, verdict
|
||||
from strata_cli.launch.runner import Result, Runner, derive_config, desc_matches, tune_words
|
||||
from tests.terminal.launch_support import HW_12, HW_24, HW_2X16, Home, catalog, run_cli
|
||||
|
||||
|
||||
class SettingsRules(unittest.TestCase):
|
||||
def test_parse_context(self):
|
||||
self.assertEqual(S.parse_context("128k"), 131072)
|
||||
self.assertEqual(S.parse_context("128K"), 131072)
|
||||
self.assertEqual(S.parse_context("32 k"), 32768)
|
||||
self.assertEqual(S.parse_context("65536"), 65536)
|
||||
with self.assertRaises(ValueError):
|
||||
S.parse_context("lots")
|
||||
with self.assertRaises(ValueError):
|
||||
S.parse_context("100")
|
||||
|
||||
def test_fit_parallel_follows_the_spec(self):
|
||||
self.assertEqual([S.fit_parallel(v) for v in (8, 12, 16, 20, 24, 32, 48)], [1, 1, 1, 2, 3, 4, 4])
|
||||
self.assertEqual(S.fit_parallel(None), 1)
|
||||
|
||||
def test_words_and_labels(self):
|
||||
self.assertEqual(S.context_label(131072), "128K")
|
||||
self.assertEqual(S.words_for(131072), "about 98,000 words")
|
||||
|
||||
def test_parallel_warning_is_a_warning_not_an_error(self):
|
||||
notes = S.check_parallel(S.LaunchSettings(parallel=4), vram_gb=12)
|
||||
self.assertEqual([n.level for n in notes], ["warn"])
|
||||
self.assertIn("still start", notes[0].line())
|
||||
self.assertEqual(S.check_parallel(S.LaunchSettings(parallel=9), vram_gb=12)[0].level, "error")
|
||||
self.assertEqual(S.check_parallel(S.LaunchSettings(parallel=2), vram_gb=24), [])
|
||||
|
||||
def test_access_notes(self):
|
||||
self.assertEqual(S.check_access(S.LaunchSettings(access="local")), [])
|
||||
lan = S.check_access(S.LaunchSettings(access="lan"))
|
||||
self.assertEqual([n.level for n in lan], ["warn"])
|
||||
self.assertIn("API key", lan[0].line())
|
||||
bad = S.check_access(S.LaunchSettings(access="lann"))
|
||||
self.assertEqual(bad[0].level, "error")
|
||||
self.assertIn("lan", bad[0].line())
|
||||
self.assertEqual(S.check_port("abc").level, "error")
|
||||
self.assertEqual(S.check_port(70000).level, "error")
|
||||
self.assertEqual(S.check_port(80).level, "warn")
|
||||
self.assertIsNone(S.check_port(8080))
|
||||
|
||||
def test_split_setting(self):
|
||||
self.assertEqual(S.split_setting("prefill=65536"), ("--prefill", "65536"))
|
||||
self.assertEqual(S.split_setting("--kv-grow"), ("--kv-grow", ""))
|
||||
self.assertEqual(S.split_setting("STRATA_PIN_BY_NEED=1"), ("STRATA_PIN_BY_NEED", "1"))
|
||||
self.assertEqual(S.split_setting("expert_cache=auto"), ("--expert-cache", "auto"))
|
||||
with self.assertRaises(ValueError):
|
||||
S.split_setting("no spaces allowed=1")
|
||||
|
||||
def test_flags_text_round_trips_to_the_same_choices(self):
|
||||
s = S.LaunchSettings(family="qwen", model="IQ3_XXS", context=131072, parallel=2, tune="quick", access="lan", port=9000,
|
||||
extra={"--prefill": "65536", "STRATA_PIN_BY_NEED": "1"})
|
||||
text = S.flags_text(s)
|
||||
for part in ("--model iq3_xxs", "--context 128k", "--parallel 2", "--tune quick", "--access lan", "--port 9000",
|
||||
"--set prefill=65536", "--set STRATA_PIN_BY_NEED=1"):
|
||||
self.assertIn(part, text)
|
||||
self.assertIn('--preset "Fast"', S.flags_text(s, preset="Fast"))
|
||||
|
||||
def test_slug(self):
|
||||
self.assertEqual(S.slug("IQ3_XXS 128K - Full speed"), "iq3_xxs-128k-full-speed")
|
||||
self.assertTrue(S.slug("123 go")[0].isalpha())
|
||||
|
||||
def test_roundtrip_dict(self):
|
||||
s = S.LaunchSettings(name="a", model="Q2_0", family="qwen", context=32768, extra={"--spec": "3"}, tuned_preset="abc")
|
||||
again = S.LaunchSettings.from_dict(json.loads(json.dumps(s.to_dict())))
|
||||
self.assertEqual(again, s)
|
||||
self.assertEqual(s.to_dict(with_key=False)["api_key"], None)
|
||||
self.assertEqual(S.LaunchSettings(api_key="sk-x").to_dict(with_key=False)["api_key"], "set")
|
||||
|
||||
|
||||
class CatalogRules(unittest.TestCase):
|
||||
def test_one_source_of_truth_and_fit_numbers(self):
|
||||
with Home():
|
||||
cat = catalog(HW_12)
|
||||
rows = cat.rows()
|
||||
ids = {(r.family, r.model) for r in rows}
|
||||
self.assertIn(("qwen", "IQ3_XXS"), ids)
|
||||
models, families, contexts, source = cat.tables()
|
||||
self.assertEqual(source, "setup.py") # the same tables setup.py and the app read
|
||||
for r in rows:
|
||||
self.assertAlmostEqual(r.download_gb, models[r.model]["download_gb"])
|
||||
rec = [r for r in rows if r.recommended]
|
||||
self.assertEqual([(r.family, r.model) for r in rec], [("qwen", "IQ3_XXS")])
|
||||
|
||||
def test_fit_on_matches_native_fit_ts(self):
|
||||
# apps/host/src/lib/native-fit.ts: memory = experts + 1; GPU holds experts up to vram - 5; RAM holds up to ram - 10; the rest spills
|
||||
f = fit_on({"experts_gb": 42.9, "ram_needed_gb": 60}, 64.0, 12.0)
|
||||
self.assertEqual((f["memoryGb"], f["vramUseGb"], f["spillGb"]), (43.9, 12.0, 0.0))
|
||||
self.assertAlmostEqual(f["ramUseGb"], 35.9, places=1)
|
||||
spill = fit_on({"experts_gb": 98.4, "ram_needed_gb": 108}, 64.0, 12.0)
|
||||
self.assertGreater(spill["spillGb"], 30)
|
||||
self.assertFalse(fit_on({"experts_gb": 10}, None, 12)["known"])
|
||||
gyro = fit_on({"mem_total": True, "ram_needed_gb": 27.6}, 64, 12)
|
||||
self.assertEqual(gyro["memoryGb"], 27.6)
|
||||
|
||||
def test_verdict_words_never_block(self):
|
||||
self.assertEqual(verdict("fits")[0], "fits")
|
||||
self.assertEqual(verdict("does not fit")[0], "no")
|
||||
self.assertIn("can still try", verdict("does not fit")[1])
|
||||
self.assertEqual(verdict("tight")[0], "tight")
|
||||
self.assertEqual(verdict("fits in the low-RAM mode (slower)")[0], "fits")
|
||||
|
||||
def test_resolve_names_and_closest(self):
|
||||
with Home():
|
||||
cat = catalog()
|
||||
self.assertEqual(cat.resolve("iq3_xxs"), ("qwen", "IQ3_XXS"))
|
||||
self.assertEqual(cat.resolve("IQ1_M"), ("coder", "IQ1_M"))
|
||||
self.assertEqual(cat.resolve("iq3_xxs", "swift"), ("swift", "IQ3_XXS"))
|
||||
with self.assertRaises(ValueError) as e:
|
||||
cat.resolve("iq3xxs")
|
||||
self.assertIn("Did you mean 'IQ3_XXS'", str(e.exception))
|
||||
with self.assertRaises(ValueError) as e:
|
||||
cat.resolve("q2_0", "swiift")
|
||||
self.assertIn("swift", str(e.exception))
|
||||
with self.assertRaises(ValueError) as e:
|
||||
cat.resolve("IQ1_M", "swift")
|
||||
self.assertIn("has no size", str(e.exception))
|
||||
|
||||
def test_small_computer_gets_warnings_not_blocks(self):
|
||||
with Home():
|
||||
rows = catalog({**HW_12, "ram_gb": 32.0}).rows()
|
||||
big = [r for r in rows if r.model == "IQ3_S" and r.family == "qwen"][0]
|
||||
self.assertIn(big.verdict, ("no", "tight"))
|
||||
|
||||
def test_kv_figures(self):
|
||||
with Home():
|
||||
cat = catalog()
|
||||
self.assertAlmostEqual(cat.kv_gb(131072, "int8"), 1.799, places=2)
|
||||
self.assertLess(cat.kv_gb(131072, "q4_0"), cat.kv_gb(131072, "int8"))
|
||||
self.assertEqual(cat.max_context("qwen", "IQ3_XXS", HW_12), 32768)
|
||||
self.assertEqual(cat.max_context("qwen", "IQ3_XXS", HW_24), 131072)
|
||||
|
||||
def test_missing_installer_files_are_said_in_words(self):
|
||||
with Home():
|
||||
with self.assertRaises(CatalogUnavailable) as e:
|
||||
Catalog(src="/nonexistent-folder", hardware=HW_12)
|
||||
self.assertIn("one-line installer", str(e.exception))
|
||||
|
||||
|
||||
class PresetStore(unittest.TestCase):
|
||||
def test_save_load_list_delete(self):
|
||||
with Home():
|
||||
s = S.LaunchSettings(name="IQ3_XXS 128K", family="qwen", model="IQ3_XXS", context=131072, api_key="sk-secret")
|
||||
store.save(s)
|
||||
store.save(S.LaunchSettings(name="Coder 64K", family="coder", model="IQ1_M", context=65536))
|
||||
names = [p.name for p in store.list_presets()]
|
||||
self.assertEqual(sorted(names), ["Coder 64K", "IQ3_XXS 128K"])
|
||||
back = store.load("IQ3_XXS 128K")
|
||||
self.assertEqual((back.model, back.context, back.api_key), ("IQ3_XXS", 131072, "sk-secret"))
|
||||
store.touch_used("IQ3_XXS 128K")
|
||||
self.assertTrue(store.load("IQ3_XXS 128K").last_used)
|
||||
self.assertEqual(store.list_presets()[0].name, "IQ3_XXS 128K") # most recently used first
|
||||
store.delete("Coder 64K")
|
||||
self.assertEqual([p.name for p in store.list_presets()], ["IQ3_XXS 128K"])
|
||||
with self.assertRaises(store.PresetError) as e:
|
||||
store.load("IQ3 XXS 128")
|
||||
self.assertIn("Did you mean", str(e.exception))
|
||||
with self.assertRaises(store.PresetError):
|
||||
store.delete("nope")
|
||||
|
||||
def test_key_is_not_world_readable_on_posix(self):
|
||||
if os.name == "nt":
|
||||
self.skipTest("POSIX modes")
|
||||
with Home():
|
||||
store.save(S.LaunchSettings(name="k", api_key="sk-secret"))
|
||||
path = next(store.preset_dir().glob("*.json"))
|
||||
self.assertEqual(path.stat().st_mode & 0o077, 0)
|
||||
|
||||
|
||||
class Advanced(unittest.TestCase):
|
||||
HELP = """strata generate --pack DIR [options]
|
||||
|
||||
--pack DIR the pack directory (default pack/full)
|
||||
--kv fp16|int8 KV storage (plan v0.3 P7): int8 codes, half the
|
||||
VRAM; default fp16 until the gate accepts int8
|
||||
--kv q4_0 4-bit K/V after a Hadamard rotation
|
||||
--kv-grow the K/V takes VRAM only for the cells the requests reach
|
||||
--native-rope experimental pinned text-only CUDA rotary arithmetic
|
||||
--prefill N prompt chunk size (default 2048)
|
||||
"""
|
||||
|
||||
def test_parse_help(self):
|
||||
ps = {p.name: p for p in params.parse_help(self.HELP)}
|
||||
self.assertEqual(set(ps), {"--pack", "--kv", "--kv-grow", "--native-rope", "--prefill"})
|
||||
self.assertEqual(ps["--pack"].metavar, "DIR")
|
||||
self.assertEqual(ps["--pack"].help_default, "pack/full")
|
||||
self.assertEqual(ps["--kv"].values, ["fp16", "int8"])
|
||||
self.assertIn("4-bit", ps["--kv"].description) # a flag listed twice keeps both texts
|
||||
self.assertTrue(ps["--native-rope"].experimental)
|
||||
self.assertFalse(ps["--prefill"].experimental)
|
||||
self.assertEqual(ps["--kv-grow"].metavar, "")
|
||||
self.assertLessEqual(len(ps["--prefill"].short), 70)
|
||||
|
||||
def test_rows_show_default_tuned_and_yours(self):
|
||||
rows = {r.name: r for r in params.build_rows(self.HELP, ["--kv", "int8", "--prefill", "auto"], ["--kv", "int8", "--prefill", "auto:32768"],
|
||||
{}, {}, {"--prefill": "65536"})}
|
||||
self.assertEqual((rows["--prefill"].default, rows["--prefill"].tuned, rows["--prefill"].yours), ("auto", "auto:32768", "65536"))
|
||||
self.assertEqual(rows["--kv"].yours, "")
|
||||
self.assertIn("--native-rope", rows)
|
||||
names = list(rows)
|
||||
self.assertEqual(names[:2], ["--kv", "--prefill"]) # what this launch uses comes first
|
||||
|
||||
def test_unknown_names_warn_and_are_kept(self):
|
||||
rows = params.build_rows(self.HELP, [], None, {}, {}, {"--prefil": "1"})
|
||||
r = next(x for x in rows if x.name == "--prefil")
|
||||
self.assertFalse(r.known)
|
||||
warn = params.check_name("--prefil", ["--prefill", "--kv"])
|
||||
self.assertIn("Did you mean --prefill", warn)
|
||||
self.assertIsNone(params.check_name("--prefill", ["--prefill"]))
|
||||
self.assertIsNone(params.check_name("--prefil", None)) # no list to check against: no warning
|
||||
|
||||
def test_extra_becomes_argv_and_env(self):
|
||||
a, e = S.as_argv_changes({"--prefill": "65536", "--kv-grow": "", "--spec": "off", "STRATA_PIN_BY_NEED": "1"}, frozenset({"--kv-grow"}))
|
||||
self.assertEqual(a, {"--prefill": "65536", "--kv-grow": True, "--spec": None})
|
||||
self.assertEqual(e, {"STRATA_PIN_BY_NEED": "1"})
|
||||
|
||||
|
||||
class RunConfig(unittest.TestCase):
|
||||
BASE = {"exe": "component:strata-engine", "args": ["--native", "x.gguf", "--max-context", "32768", "--kv", "int8", "--prefill", "auto"],
|
||||
"port": 8095, "model_name": "m", "env": {}}
|
||||
|
||||
def test_derive_applies_access_key_and_advanced(self):
|
||||
s = S.LaunchSettings(access="lan", port=9001, api_key="sk-k", extra={"--prefill": "65536", "--kv-grow": "", "STRATA_PIN_BY_NEED": "1"})
|
||||
cfg = derive_config(self.BASE, s, bool_flags=frozenset({"--kv-grow"}))
|
||||
self.assertEqual((cfg["host"], cfg["port"], cfg["api_key"]), ("0.0.0.0", 9001, "sk-k"))
|
||||
args = cfg["args"]
|
||||
self.assertEqual(args[args.index("--prefill") + 1], "65536")
|
||||
self.assertIn("--kv-grow", args)
|
||||
self.assertEqual(cfg["env"], {"STRATA_PIN_BY_NEED": "1"})
|
||||
self.assertEqual(self.BASE["args"][-1], "auto") # the installed config is never edited
|
||||
|
||||
def test_local_and_cloudflare_stay_on_loopback_without_a_key(self):
|
||||
for kind in ("local", "cloudflare"):
|
||||
cfg = derive_config(self.BASE, S.LaunchSettings(access=kind), bool_flags=frozenset())
|
||||
self.assertEqual(cfg["host"], "127.0.0.1")
|
||||
self.assertNotIn("api_key", cfg)
|
||||
|
||||
def test_installed_config_matching(self):
|
||||
d = {"context": 32768, "kv": "int8", "images": "off"}
|
||||
self.assertTrue(desc_matches(d, S.LaunchSettings(context=32768)))
|
||||
self.assertFalse(desc_matches(d, S.LaunchSettings(context=65536)))
|
||||
self.assertFalse(desc_matches(d, S.LaunchSettings(context=32768, vision="on")))
|
||||
self.assertFalse(desc_matches({**d, "images": "cpu"}, S.LaunchSettings(context=32768, vision="off")))
|
||||
self.assertFalse(desc_matches(d, S.LaunchSettings(context=32768, kv="q4_0")))
|
||||
|
||||
def test_tune_words(self):
|
||||
self.assertEqual(tune_words({"outcome": "improved", "gain_pct": 4.2})[0], "faster")
|
||||
self.assertEqual(tune_words({"outcome": "default_confirmed"})[0], "already fastest")
|
||||
self.assertEqual(tune_words({"outcome": "inconclusive", "reason_code": "noisy"})[0], "busy PC")
|
||||
self.assertEqual(tune_words({"outcome": "base_failed", "message": "Not tuned: no memory"})[0], "failed")
|
||||
self.assertEqual(tune_words(None)[0], "failed")
|
||||
self.assertEqual(tune_words({"outcome": "cancelled"})[0], "stopped")
|
||||
|
||||
|
||||
class AccessRules(unittest.TestCase):
|
||||
def test_hosts_and_keys(self):
|
||||
self.assertEqual(access.host_for("local"), "127.0.0.1")
|
||||
self.assertEqual(access.host_for("cloudflare"), "127.0.0.1")
|
||||
self.assertEqual(access.host_for("lan"), "0.0.0.0")
|
||||
self.assertTrue(access.new_key().startswith("sk-strata-"))
|
||||
self.assertNotIn(access.new_key(), access.mask_key(access.new_key()))
|
||||
self.assertEqual(access.mask_key(None), "off")
|
||||
|
||||
def test_private_ranges(self):
|
||||
for ip in ("10.1.2.3", "192.168.88.21", "172.16.0.9", "100.101.1.1"):
|
||||
self.assertTrue(access._is_private(ip), ip)
|
||||
for ip in ("8.8.8.8", "172.32.0.1", "1.2.3.4"):
|
||||
self.assertFalse(access._is_private(ip), ip)
|
||||
|
||||
def test_missing_helpers_say_how_to_install(self):
|
||||
with mock.patch.object(access, "find_cloudflared", return_value=None):
|
||||
msg = access.missing_helper("cloudflare")
|
||||
self.assertIn("cloudflared is not installed", msg)
|
||||
self.assertIn("quick tunnel", msg)
|
||||
with mock.patch.object(access, "find_tailscale", return_value=None):
|
||||
self.assertIn("Tailscale is not installed", access.missing_helper("tailscale"))
|
||||
with mock.patch.object(access, "tailscale_status", return_value={"installed": True, "running": False, "ip": None, "name": None}):
|
||||
self.assertIn("tailscale up", access.missing_helper("tailscale"))
|
||||
self.assertIsNone(access.missing_helper("local"))
|
||||
|
||||
def test_options_and_urls(self):
|
||||
with mock.patch.object(access, "lan_ips", return_value=["192.168.1.5"]), \
|
||||
mock.patch.object(access, "find_cloudflared", return_value="/bin/cloudflared"), \
|
||||
mock.patch.object(access, "tailscale_status", return_value={"installed": True, "running": True, "ip": "100.64.0.1", "name": "pc.tail.ts.net"}):
|
||||
opts = {o.kind: o for o in access.options(8080)}
|
||||
self.assertEqual(opts["lan"].url, "http://192.168.1.5:8080")
|
||||
self.assertTrue(opts["cloudflare"].ready)
|
||||
self.assertEqual(opts["tailscale"].url, "http://pc.tail.ts.net:8080")
|
||||
urls = access.urls_for(S.LaunchSettings(access="lan", port=8080))
|
||||
self.assertEqual(urls, ["http://127.0.0.1:8080", "http://192.168.1.5:8080"])
|
||||
self.assertEqual(access.urls_for(S.LaunchSettings(access="cloudflare", port=8080), tunnel_url="https://a.trycloudflare.com")[0],
|
||||
"https://a.trycloudflare.com")
|
||||
|
||||
def test_helper_output_filter_drops_login_lines(self):
|
||||
self.assertEqual(access.filter_line("Please open this URL to login: https://dash.cloudflare.com/argotunnel?token=abc"), "")
|
||||
self.assertEqual(access.filter_line("Your quick Tunnel has been created: https://x.trycloudflare.com"),
|
||||
"Your quick Tunnel has been created: https://x.trycloudflare.com")
|
||||
self.assertEqual(access.TUNNEL_URL.search("| https://calm-river-1234.trycloudflare.com |").group(0),
|
||||
"https://calm-river-1234.trycloudflare.com")
|
||||
|
||||
|
||||
class FlagsCommand(unittest.TestCase):
|
||||
"""Every `strata launch` flag, run through the real parser with the computer faked and nothing installed."""
|
||||
|
||||
def setUp(self):
|
||||
self.home = Home().__enter__()
|
||||
self.addCleanup(self.home.__exit__, None, None, None)
|
||||
p = mock.patch.object(Catalog, "hardware", lambda self, fresh=False: dict(HW_12, disk_free_gb=500.0))
|
||||
p.start()
|
||||
self.addCleanup(p.stop)
|
||||
|
||||
def plan(self, *args):
|
||||
code, out, err = run_cli(["launch", "--plan", "--output", "json", *args])
|
||||
self.assertEqual(code, 0, err or out)
|
||||
return json.loads(out)["plan"]
|
||||
|
||||
def test_help_lists_everything_with_examples(self):
|
||||
code, out, err = run_cli(["launch", "--help"])
|
||||
self.assertEqual(code, 0)
|
||||
for flag in ("--preset", "--model", "--family", "--model-path", "--data-dir", "--context", "--kv", "--vision", "--parallel", "--gpu",
|
||||
"--tune", "--tune-allow-small", "--set", "--access", "--port", "--api-key", "--api-key-env", "--new-key", "--save-preset",
|
||||
"--no-tui", "--plan", "--feed", "--list-presets", "--delete-preset", "--status", "--stop", "--show-key", "examples:"):
|
||||
self.assertIn(flag, out)
|
||||
code, out, _ = run_cli(["--help"])
|
||||
self.assertIn("launch", out)
|
||||
self.assertIn("connect", out)
|
||||
|
||||
def test_model_context_parallel_vision_tune_access_port(self):
|
||||
p = self.plan("--model", "iq3_xxs", "--context", "128k", "--parallel", "2", "--vision", "off", "--tune", "quick", "--access", "lan",
|
||||
"--port", "9090")
|
||||
s = p["settings"]
|
||||
self.assertEqual((s["family"], s["model"], s["context"], s["parallel"], s["vision"], s["tune"], s["access"], s["port"]),
|
||||
("qwen", "IQ3_XXS", 131072, 2, "off", "quick", "lan", 9090))
|
||||
self.assertEqual(s["api_key"], "set") # shared beyond this computer: a key was made
|
||||
self.assertTrue(any("2 requests" in w and "12 GB" in w for w in p["warnings"]))
|
||||
|
||||
def test_defaults_come_from_the_recommendation(self):
|
||||
p = self.plan()
|
||||
s = p["settings"]
|
||||
self.assertEqual((s["family"], s["model"], s["context"], s["access"], s["tune"]), ("qwen", "IQ3_XXS", 32768, "local", "skip"))
|
||||
self.assertTrue(any("picked" in w for w in p["warnings"]))
|
||||
|
||||
def test_family_and_kv_and_gpu_and_data_dir_and_set(self):
|
||||
p = self.plan("--model", "iq3_xxs", "--family", "swift", "--kv", "q4_0", "--gpu", "0,1", "--data-dir", "/data/x",
|
||||
"--set", "prefill=65536", "--set", "STRATA_PIN_BY_NEED=1", "--tune-allow-small")
|
||||
s = p["settings"]
|
||||
self.assertEqual((s["family"], s["kv"], s["gpus"], s["data_dir"], s["tune_allow_small"]), ("swift", "q4_0", [0, 1], "/data/x", True))
|
||||
self.assertEqual(s["extra"], {"--prefill": "65536", "STRATA_PIN_BY_NEED": "1"})
|
||||
self.assertIn("--set prefill=65536", p["flags"])
|
||||
|
||||
def test_api_key_flags(self):
|
||||
p = self.plan("--model", "q2_0", "--access", "lan", "--api-key", "sk-mine")
|
||||
self.assertEqual(p["settings"]["api_key"], "set")
|
||||
self.assertFalse(any("new API key was made" in w for w in p["warnings"]))
|
||||
with mock.patch.dict(os.environ, {"MY_KEY": "sk-env"}):
|
||||
p = self.plan("--model", "q2_0", "--api-key-env", "MY_KEY")
|
||||
self.assertEqual(p["settings"]["api_key"], "set")
|
||||
code, out, err = run_cli(["launch", "--plan", "--api-key-env", "NOT_SET_ANYWHERE"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("NOT_SET_ANYWHERE", err)
|
||||
p = self.plan("--model", "q2_0", "--new-key")
|
||||
self.assertEqual(p["settings"]["api_key"], "set")
|
||||
|
||||
def test_model_path_is_used_instead_of_a_catalog_model(self):
|
||||
p = self.plan("--model-path", "/data/models/IQ3_XXS")
|
||||
s = p["settings"]
|
||||
self.assertEqual(s["model_path"], str(Path("/data/models/IQ3_XXS")))
|
||||
self.assertIsNone(s["model"])
|
||||
|
||||
def test_unknown_model_names_the_closest_one(self):
|
||||
code, out, err = run_cli(["launch", "--plan", "--model", "iq3xxs"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("Did you mean 'IQ3_XXS'", err)
|
||||
self.assertNotIn("Traceback", err)
|
||||
|
||||
def test_unknown_flag_names_the_closest_one(self):
|
||||
code, out, err = run_cli(["launch", "--modle", "iq3_xxs"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("Did you mean '--model'", err)
|
||||
|
||||
def test_bad_values_are_plain_sentences(self):
|
||||
for argv, word in ((["--tune", "quik"], "quick"), (["--access", "lann"], "lan"), (["--context", "huge"], "context"),
|
||||
(["--port", "99999"], "1 to 65535"), (["--parallel", "12"], "1 to 8"), (["--vision", "maybe"], "off")):
|
||||
code, out, err = run_cli(["launch", "--plan", *argv])
|
||||
self.assertEqual(code, 2, argv)
|
||||
self.assertIn(word, err, argv)
|
||||
|
||||
def test_preset_with_flags_on_top(self):
|
||||
store.save(S.LaunchSettings(name="Office", family="qwen", model="IQ3_XXS", context=65536, parallel=2, access="local"))
|
||||
p = self.plan("--preset", "Office", "--context", "128k")
|
||||
s = p["settings"]
|
||||
self.assertEqual((s["name"], s["context"], s["parallel"]), ("Office", 131072, 2))
|
||||
code, out, err = run_cli(["launch", "--plan", "--preset", "Offce"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("Did you mean 'Office'", err)
|
||||
|
||||
def test_vision_is_turned_off_for_a_model_without_pictures(self):
|
||||
p = self.plan("--model", "gyro-s", "--vision", "on")
|
||||
self.assertEqual(p["settings"]["vision"], "off")
|
||||
self.assertTrue(any("cannot read pictures" in w for w in p["warnings"]))
|
||||
|
||||
def test_does_not_fit_is_a_warning_with_the_reason(self):
|
||||
p = self.plan("--model", "nvfp4")
|
||||
self.assertTrue(any("does not fit" in w for w in p["warnings"]))
|
||||
|
||||
def test_human_plan_changes_nothing(self):
|
||||
code, out, err = run_cli(["launch", "--plan", "--model", "q2_0", "--save-preset", "X"])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Nothing was changed.", out)
|
||||
self.assertEqual(store.list_presets(), [])
|
||||
|
||||
def test_run_without_screens_prints_the_address_and_saves_the_preset(self):
|
||||
fake = Result(profile="launch-office", urls=["http://127.0.0.1:8080", "http://192.168.1.5:8080"], key="sk-strata-abcdefghijkl",
|
||||
tune_word=None, notices=[], config="/x")
|
||||
with mock.patch.object(Runner, "run", return_value=fake):
|
||||
code, out, err = run_cli(["launch", "--no-tui", "--model", "q2_0", "--access", "lan", "--save-preset", "Office"])
|
||||
self.assertEqual(code, 0, err)
|
||||
self.assertIn("Strata is serving.", out)
|
||||
self.assertIn("http://127.0.0.1:8080/v1", out)
|
||||
self.assertIn("http://192.168.1.5:8080/v1", out)
|
||||
self.assertNotIn("sk-strata-abcdefghijkl", out) # the key is masked
|
||||
self.assertIn("strata connect", out)
|
||||
self.assertEqual([p.name for p in store.list_presets()], ["Office"])
|
||||
code, out, err = run_cli(["launch", "--list-presets"])
|
||||
self.assertIn("Office", out)
|
||||
code, out, _ = run_cli(["launch", "--show-key", "--preset", "Office"])
|
||||
self.assertEqual(code, 0) # the preset's own key
|
||||
self.assertTrue(out.strip().startswith("sk-strata-"))
|
||||
|
||||
def test_launch_error_is_one_plain_sentence(self):
|
||||
from strata_cli.launch.runner import LaunchError
|
||||
with mock.patch.object(Runner, "run", side_effect=LaunchError("The engine could not be installed.", "Check your internet.", 6)):
|
||||
code, out, err = run_cli(["launch", "--no-tui", "--model", "q2_0"])
|
||||
self.assertEqual(code, 6)
|
||||
self.assertIn("The engine could not be installed. Check your internet.", err)
|
||||
self.assertNotIn("Traceback", err)
|
||||
|
||||
def test_list_delete_status_stop_without_anything_running(self):
|
||||
store.save(S.LaunchSettings(name="A", family="qwen", model="Q2_0", context=32768))
|
||||
code, out, _ = run_cli(["launch", "--list-presets"])
|
||||
self.assertIn("A", out)
|
||||
code, out, _ = run_cli(["launch", "--delete-preset", "A"])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Models and tuning results were not touched", out)
|
||||
code, out, _ = run_cli(["launch", "--list-presets"])
|
||||
self.assertIn("No saved presets", out)
|
||||
code, out, _ = run_cli(["launch", "--status"])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Nothing from `strata launch` is running.", out)
|
||||
code, out, _ = run_cli(["launch", "--stop"])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Nothing", out)
|
||||
code, out, err = run_cli(["launch", "--delete-preset", "gone"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("no saved preset", err)
|
||||
|
||||
def test_feed_flag_reaches_the_runner(self):
|
||||
seen = {}
|
||||
|
||||
def fake(self, *a, **k):
|
||||
seen["feed"] = self.feed
|
||||
return Result("launch-x", ["http://127.0.0.1:8080"], None, None, [], "/x")
|
||||
|
||||
with mock.patch.object(Runner, "run", fake):
|
||||
code, _, err = run_cli(["launch", "--no-tui", "--model", "q2_0", "--feed", "http://127.0.0.1:9/feed"])
|
||||
self.assertEqual(code, 0, err)
|
||||
self.assertEqual(seen["feed"], "http://127.0.0.1:9/feed")
|
||||
|
||||
def test_output_json_for_scripts(self):
|
||||
fake = Result("launch-x", ["http://127.0.0.1:8080"], "sk-k", "faster", ["a note"], "/x")
|
||||
with mock.patch.object(Runner, "run", return_value=fake):
|
||||
code, out, err = run_cli(["launch", "--no-tui", "--output", "json", "--model", "q2_0"])
|
||||
data = json.loads(out)
|
||||
self.assertTrue(data["ok"])
|
||||
self.assertEqual(data["urls"], ["http://127.0.0.1:8080"])
|
||||
self.assertEqual(data["api_key"], "set") # never the key itself
|
||||
self.assertEqual(data["tuned"], "faster")
|
||||
|
||||
|
||||
class ConnectCommand(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.home = Home().__enter__()
|
||||
self.addCleanup(self.home.__exit__, None, None, None)
|
||||
|
||||
def test_list_and_unknown_names(self):
|
||||
code, out, _ = run_cli(["connect"])
|
||||
self.assertEqual(code, 0)
|
||||
for h in ("opencode", "codex", "claude-code", "omp", "pi", "hermes", "openclaw", "deepseek-harness"):
|
||||
self.assertIn(h, out)
|
||||
code, out, err = run_cli(["connect", "opncode"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("Did you mean 'opencode'", err)
|
||||
code, out, _ = run_cli(["connect", "--output", "json"])
|
||||
self.assertEqual(len(json.loads(out)["harnesses"]), 8)
|
||||
|
||||
def test_prints_the_config_with_a_masked_key(self):
|
||||
code, out, err = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8123", "--key", "sk-strata-abcdefghij1234", "--model", "m1"])
|
||||
self.assertEqual(code, 0, err)
|
||||
self.assertIn("http://127.0.0.1:8123/v1", out)
|
||||
self.assertIn("opencode.json", out)
|
||||
self.assertNotIn("abcdefghij1234", out)
|
||||
code, out, _ = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8123", "--key", "sk-strata-abcdefghij1234", "--show-key"])
|
||||
self.assertIn("sk-strata-abcdefghij1234", out)
|
||||
|
||||
def test_claude_code_uses_the_anthropic_variables_without_a_login(self):
|
||||
code, out, _ = run_cli(["connect", "claude-code", "--url", "http://127.0.0.1:8080", "--key", "sk-x1234567890abc", "--show-key"])
|
||||
self.assertIn("ANTHROPIC_BASE_URL", out)
|
||||
self.assertIn("ANTHROPIC_AUTH_TOKEN", out)
|
||||
self.assertNotIn("/v1", out.split("ANTHROPIC_BASE_URL")[1].splitlines()[0]) # the Anthropic client adds /v1 itself
|
||||
|
||||
def test_write_keeps_a_backup_and_merges(self):
|
||||
target = Path(os.environ["HOME"]) / ".config" / "opencode" / "opencode.json"
|
||||
target.parent.mkdir(parents=True)
|
||||
target.write_text(json.dumps({"theme": "dark", "provider": {"other": {"a": 1}}}))
|
||||
code, out, err = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8080", "--key", "sk-x1234567890abc", "--write", "--yes"])
|
||||
self.assertEqual(code, 0, err)
|
||||
data = json.loads(target.read_text())
|
||||
self.assertEqual(data["theme"], "dark")
|
||||
self.assertIn("other", data["provider"])
|
||||
self.assertIn("strata", data["provider"])
|
||||
backups = list(target.parent.glob("opencode.json.strata-bak-*"))
|
||||
self.assertEqual(len(backups), 1)
|
||||
self.assertEqual(json.loads(backups[0].read_text())["theme"], "dark")
|
||||
self.assertIn("backup", out)
|
||||
|
||||
def test_write_asks_first_without_yes(self):
|
||||
code, out, err = run_cli(["connect", "opencode", "--write"])
|
||||
self.assertEqual(code, 2)
|
||||
self.assertIn("asks first", err)
|
||||
|
||||
def test_built_in_harness_has_nothing_to_write(self):
|
||||
code, out, _ = run_cli(["connect", "deepseek-harness", "--write", "--yes"])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("Nothing to write", out)
|
||||
|
||||
def test_untested_drafts_say_so(self):
|
||||
h = harness.find("hermes")
|
||||
if not h.tested:
|
||||
code, out, _ = run_cli(["connect", "hermes"])
|
||||
self.assertIn("not been tested", out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user