Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
monitor: the approved Overview as view 1 (state bar, 8 tiles with sparklines, bars, recent requests) from /metrics; b opens the browser
Niko1221efa38a99ca0e
diff --git a/strata_cli/launch/monitor_ext.py b/strata_cli/launch/monitor_ext.pyindex ce3dd0613..7302c8919 100644--- a/strata_cli/launch/monitor_ext.py+++ b/strata_cli/launch/monitor_ext.py@@ -28,6 +28,7 @@ class LaunchMonitor(MonitorApp):def __init__(self, endpoint, snapshot, *, info: dict, **kw) -> None:super().__init__(endpoint, snapshot, **kw)self.info = info+ self.connect_enabled = Trueself.connect_open = Falseself.connect_index = 0self.show_key = Falsediff --git a/strata_cli/monitor.py b/strata_cli/monitor.pyindex cd501926f..81723a72b 100644--- a/strata_cli/monitor.py+++ b/strata_cli/monitor.py@@ -92,6 +92,7 @@ class MonitorApp(App):Binding("right", "next_view", "Next view", show=False),Binding("enter", "request_detail", "Row detail", show=False),Binding("question_mark", "help", "Help"), Binding("f1", "help", "Help", show=False),+ Binding("b", "browser", "Open in browser", show=False),]def __init__(self, endpoint: Endpoint, snapshot: dict | None, *, interval: float = 1.0,@@ -106,6 +107,8 @@ class MonitorApp(App):self.connection_state = ("telemetry_unavailable" if self.has_snapshot and(self.latest.get("quality") or {}).get("telemetry") == "unavailable" else"connected" if self.has_snapshot else "connecting")+ self.metrics_model = None # the Overview's numbers, from /metrics (None: an older server; the earlier Overview is drawn)+ self.connect_enabled = False # launch's monitor adds a Connect view (key 7)self.interval = intervalself.requests = requestsself.view = 0@@ -169,7 +172,14 @@ class MonitorApp(App):snapshot["activity_continuity"] = self.displayed_activity_continuitysnapshot["work"] = {**(self.displayed.get("work") or {}),"requests": ((self.displayed.get("work") or {}).get("requests") or [])[:self.requests]}- if self.view == 0 and size.width >= 78 and size.height >= 23:+ if self.view == 0 and size.width >= 78 and size.height >= 23 and self.metrics_model is not None and self.connection_state in ("connected", "stale", "telemetry_unavailable"):+ from strata_cli import overview+ display = overview.render(+ self.metrics_model, width=max(30, size.width - self.margin), height=max(8, size.height - 2),+ connection=getattr(self.endpoint, "base_url", getattr(self.endpoint, "root", "")),+ connection_state=self.connection_state, frozen=self.frozen, glyphs=self.glyph_choice,+ color=self.color_choice != "never", theme=self.theme_choice, connect=self.connect_enabled)+ elif self.view == 0 and size.width >= 78 and size.height >= 23:display = render_dashboard(snapshot, width=max(30, size.width - self.margin), height=max(8, size.height - 2),history=self.history, connection=getattr(self.endpoint, "base_url", getattr(self.endpoint, "root", "")),@@ -233,6 +243,7 @@ class MonitorApp(App):fresh = with_aggregate_rate(self.rate_previous or {},await asyncio.to_thread(self.endpoint.snapshot))self.latest = fresh+ await self._poll_metrics()self.rate_previous = fresh if _fresh_rate_sample(fresh) else Noneself.activity_continuity.add(fresh)if not self.frozen:@@ -270,6 +281,28 @@ class MonitorApp(App):if not self.closing and self.is_mounted:self._paint()+ async def _poll_metrics(self) -> None:+ """The browser Monitor's own data (/metrics: live state, engine facts, hardware with a minute of history, last requests)."""+ getter = getattr(self.endpoint, "get", None)+ if getter is None:+ self.metrics_model = None+ return+ try:+ from strata_cli import overview+ self.metrics_model = overview.build(await asyncio.to_thread(getter, "/metrics"))+ except Exception: # noqa: BLE001 - an older server has no /metrics: keep the earlier Overview+ self.metrics_model = None++ def action_browser(self) -> None:+ import webbrowser+ url = getattr(self.endpoint, "base_url", None) or getattr(self.endpoint, "root", "")+ try:+ opened = webbrowser.open(url) if url else False+ except Exception: # noqa: BLE001+ opened = False+ self.error = f"Opened {url} in your browser" if opened else f"Open {url} in a browser (none could be started here)"+ self._paint()+def on_resize(self, event) -> None:self._paint(event.size)diff --git a/strata_cli/overview.py b/strata_cli/overview.pynew file mode 100644index 000000000..0746b89c3--- /dev/null+++ b/strata_cli/overview.py@@ -0,0 +1,329 @@+"""The monitor's Overview (view 1): the browser Monitor's page redone for the terminal.++A model-state bar, eight tiles with sparklines (speed, GPU load, VRAM, GPU temperature, power, PCIe, CPU, disk), bars for context fill,+experts in VRAM and system RAM, and the recent requests. It reads what the browser Monitor reads: the server's `/metrics`+(live state, engine facts, the hardware sample and a minute of history per series, the last requests) as the app's+apps/host/src/lib/native-monitor.ts maps it: the context used is the current request's prompt + generated tokens while one runs, else the+last request's; the speed is the live rate while generating, else the last request's.++`build(metrics)` turns the raw metrics into plain numbers (None where the server did not report one: never a made-up zero);+`render(...)` draws them. Neither needs Textual.+"""++from __future__ import annotations++import time+from typing import Any++from rich import box+from rich.console import Group+from rich.panel import Panel+from rich.table import Table+from rich.text import Text++GIB = 2 ** 30+BLUE = "#5aa9ff"+AMBER = "#f5c56b"+RED = "#f28b82"+ACCENT = "#34d399"+GREY = "#59636b"+STATES = ("Idle", "Reading", "Generating", "Queued", "Error")+++def _num(v: Any) -> float | None:+ if isinstance(v, bool) or not isinstance(v, (int, float)):+ return None+ if v != v or v in (float("inf"), float("-inf")):+ return None+ return float(v)+++def _gib(v: Any) -> float | None:+ n = _num(v)+ return None if n is None else n / GIB+++def _series(history: dict, key: str, scale: float = 1.0) -> list[float | None]:+ raw = history.get(key) if isinstance(history, dict) else None+ if not isinstance(raw, list):+ return []+ return [(None if _num(x) is None else _num(x) / scale) for x in raw[-60:]]+++def request_speed(r: dict) -> float | None:+ out, ms = _num(r.get("output_tokens")), _num(r.get("decode_ms"))+ if r.get("decode_tok_s") is not None and _num(r.get("decode_tok_s")) is not None:+ return _num(r.get("decode_tok_s"))+ return round(out / (ms / 1000.0), 1) if out and ms and ms > 0 else None+++def build(metrics: dict | None) -> dict | None:+ """The numbers the Overview draws, or None when the server does not serve /metrics (an old server: the caller falls back)."""+ if not isinstance(metrics, dict) or not isinstance(metrics.get("live"), dict):+ return None+ live, engine = metrics["live"], metrics.get("engine") if isinstance(metrics.get("engine"), dict) else {}+ hw = metrics.get("hardware") if isinstance(metrics.get("hardware"), dict) else {}+ static = metrics.get("hardware_static") if isinstance(metrics.get("hardware_static"), dict) else {}+ history = metrics.get("history") if isinstance(metrics.get("history"), dict) else {}+ recent = [r for r in (metrics.get("requests") or []) if isinstance(r, dict)]+ state_raw = str(live.get("state") or "idle")+ queued = _num(live.get("queued")) or 0+ err = bool(engine.get("error")) or state_raw == "error"+ if err:+ state = "Error"+ elif state_raw == "reading":+ state = "Reading"+ elif state_raw == "generating":+ state = "Generating"+ elif queued > 0 and state_raw not in ("reading", "generating"):+ state = "Queued"+ else:+ state = "Idle"+ active = state in ("Reading", "Generating")+ prompt_read, prompt_total = _num(live.get("prompt_read")), _num(live.get("prompt_total"))+ generated, max_tokens = _num(live.get("generated")), _num(live.get("max_tokens"))+ if state == "Reading" and prompt_total:+ done, total = prompt_read or 0.0, prompt_total+ elif state == "Generating" and max_tokens:+ done, total = generated or 0.0, max_tokens+ else:+ done, total = None, None+ last = recent[0] if recent else None+ max_ctx = _num(engine.get("max_context"))+ if active and _num(live.get("prompt_tokens")) is not None and generated is not None:+ used = _num(live.get("prompt_tokens")) + generated+ elif last and _num(last.get("prompt_tokens")) is not None and _num(last.get("output_tokens")) is not None:+ used = _num(last["prompt_tokens"]) + _num(last["output_tokens"])+ else:+ used = None+ if state == "Generating" and _num(live.get("tok_s")) is not None:+ speed, speed_note = _num(live.get("tok_s")), "now"+ elif state == "Reading" and _num(live.get("prefill_tok_s_mean")) is not None:+ speed, speed_note = _num(live.get("prefill_tok_s_mean")), "reading the prompt"+ else:+ speed, speed_note = (request_speed(last) if last else None), "last request"+ slots, total_slots = _num(engine.get("expert_slots")), _num(engine.get("experts"))+ cache_mib = _num(engine.get("expert_cache_mib"))+ gen, genmax, width = _num(hw.get("gpu_pcie_gen")), _num(hw.get("gpu_pcie_gen_max")), _num(hw.get("gpu_pcie_width"))+ return {+ "model": engine.get("model"), "max_context": max_ctx, "state": state, "state_raw": state_raw, "queued": int(queued),+ "progress": (done, total), "phase_text": {"Idle": "Waiting for a request", "Reading": "Reading prompt", "Generating": "Generating a reply",+ "Queued": "Requests are queued", "Error": "Model unavailable"}[state],+ "context": (used, max_ctx),+ "speed": (speed, speed_note, _series(history, "tok_s") if speed_note == "now" or not _series(history, "prefill_tok_s_mean") else+ _series(history, "tok_s")),+ "gpu": {"name": static.get("gpu_name"), "util": _num(hw.get("gpu_util")), "util_hist": _series(history, "gpu_util"),+ "vram_used": _gib(hw.get("gpu_mem_used")), "vram_total": _gib(hw.get("gpu_mem_total")) or+ (_num(static.get("gpu_vram_total_mib")) / 1024.0 if _num(static.get("gpu_vram_total_mib")) else None),+ "vram_hist": _series(history, "gpu_mem_used", GIB), "temp": _num(hw.get("gpu_temp")), "temp_hist": _series(history, "gpu_temp"),+ "power": _num(hw.get("gpu_power")), "power_limit": _num(hw.get("gpu_power_limit")), "power_hist": _series(history, "gpu_power"),+ "pcie_rx": _num(hw.get("gpu_pcie_rx_mb")), "pcie_hist": _series(history, "gpu_pcie_rx_mb"),+ "pcie_gen": gen if gen is not None else genmax, "pcie_width": width, "count": _num(static.get("gpu_count"))},+ "cpu": {"load": _num(hw.get("cpu_pct") if hw.get("cpu_pct") is not None else hw.get("cpu")), "hist": _series(history, "cpu"),+ "cores": _num(static.get("cores")), "threads": _num(static.get("threads"))},+ "disk": {"read": _num(hw.get("disk_read_mb")), "write": _num(hw.get("disk_write_mb")), "hist": _series(history, "disk_read_mb")},+ "ram": (_gib(hw.get("ram_used")), _gib(hw.get("ram_total"))),+ "experts": {"slots": slots, "total": total_slots, "gb": (cache_mib / 1024.0 if cache_mib is not None else None)},+ "recent": [{"time": _num(r.get("time")), "state": ("Error" if r.get("error") else+ {"stop": "Done", "length": "Done", "tool_calls": "Done"}.get(r.get("finish_reason") or r.get("finish"), "Done")+ if (r.get("finish_reason") or r.get("finish")) not in ("cancelled", "error", "abort") else "Stopped"),+ "prompt": _num(r.get("prompt_tokens")), "reused": _num(r.get("reused")), "output": _num(r.get("output_tokens")),+ "tps": request_speed(r), "duration": _num(r.get("duration_s"))} for r in recent[:6]],+ "sampled_at": _num(metrics.get("time")),+ }+++# ------------------------------------------------------------------------------------------------ drawing++def spark(values: list[float | None], width: int, glyphs: str, lo: float | None = None, hi: float | None = None) -> str:+ """One row of block characters for the last `width` samples; blanks where a sample is missing; ASCII ramp when `glyphs` is ascii."""+ ramp = ".:-=+*#@" if glyphs == "ascii" else "▁▂▃▄▅▆▇█"+ vals = values[-width:]+ present = [v for v in vals if v is not None]+ if not present:+ return " " * width+ low = min(present) if lo is None else lo+ high = max(present) if hi is None else hi+ if high - low < 1e-9:+ high = low + 1.0+ cells = []+ for v in vals:+ if v is None:+ cells.append(" ")+ else:+ cells.append(ramp[min(7, max(0, int((v - low) / (high - low) * 7.999)))])+ return " " * (width - len(cells)) + "".join(cells)+++def bar(frac: float | None, width: int, glyphs: str, color: str, empty: str = GREY) -> Text:+ full, hollow = ("#", ".") if glyphs == "ascii" else ("█", "░")+ t = Text(no_wrap=True)+ if frac is None:+ t.append(hollow * width, style=GREY)+ return t+ n = int(round(max(0.0, min(1.0, frac)) * width))+ t.append(full * n, style=color)+ t.append(hollow * (width - n), style=empty)+ return t+++def fmt(v: float | None, digits: int = 0, unit: str = "") -> str:+ if v is None:+ return "--"+ return f"{v:,.{digits}f}{unit}"+++def tokens(v: float | None) -> str:+ return "--" if v is None else f"{v:,.0f}"+++def k(v: float | None) -> str:+ if v is None:+ return "--"+ return f"{v / 1024:.0f}K" if v >= 1024 else f"{v:.0f}"+++def _tile(title: str, big: str, sub: str, series: str, width: int, *, accent: str, box_style, big_style: str = "bold") -> Panel:+ body = Text(no_wrap=True, overflow="crop")+ body.append(big + "\n", style=big_style)+ body.append(sub + "\n", style="dim")+ body.append(series, style=accent)+ return Panel(body, title=Text(title, style="dim"), title_align="left", box=box_style, padding=(0, 1), width=width,+ border_style="dim")+++def render(model: dict, *, width: int, height: int, connection: str, connection_state: str = "connected", frozen: bool = False,+ glyphs: str = "unicode", color: bool = True, theme: str = "dark", connect: bool = False, view_label: str = "Overview",+ now: float | None = None) -> Group:+ now = time.time() if now is None else now+ ascii_mode = glyphs == "ascii"+ box_style = box.SQUARE if ascii_mode else box.ROUNDED+ accent = ("#0c8a60" if theme == "light" else ACCENT) if color else "default"+ plain = (lambda s: "default") if not color else (lambda s: s)+ state = model["state"]+ state_color = plain({"Idle": "default", "Reading": BLUE, "Generating": accent, "Queued": AMBER, "Error": RED}[state])+ age = None if model.get("sampled_at") is None else now - model["sampled_at"]+ marker = ("FROZEN" if frozen else {"connected": "LIVE", "stale": "STALE", "connecting": "CONNECTING", "disconnected": "DISCONNECTED",+ "auth_required": "AUTH REQUIRED", "incompatible": "INCOMPATIBLE",+ "telemetry_unavailable": "LIVE"}.get(connection_state, "STALE"))+ if marker == "LIVE" and age is not None and age > 6:+ marker = "STALE"+ ctx_used, ctx_max = model["context"]+ done, total = model["progress"]+ pct = int(round(100 * done / total)) if done is not None and total else None++ # ---- heading, tabs, rule+ head = Text(no_wrap=True, overflow="crop")+ head.append("STRATA Serving ", style=f"bold {accent}")+ head.append(connection + " ", style=f"bold {accent}")+ label = f"{model.get('model') or ''} {k(ctx_max) if ctx_max else ''}".strip()+ head.append(label, style="bold")+ right = Text()+ right.append(("● " if not ascii_mode else "* ") + model["phase_text"] + (f" · {pct}%" if pct is not None and state in ("Reading", "Generating") else ""),+ style=state_color if state != "Idle" else "dim")+ right.append(" ")+ right.append(f"[{marker}]", style=f"bold {accent}" if marker == "LIVE" else "bold yellow")+ pad = max(2, width - len(head.plain) - len(right.plain))+ head.append(" " * pad)+ head.append_text(right)+ names = ["Overview", "Serving", "Requests", "Engine", "Hardware", "Logs"] + (["Connect"] if connect else [])+ tabs = Text(no_wrap=True, overflow="crop")+ for i, n in enumerate(names):+ tabs.append((f"[{i + 1} {n}]" if i == 0 else f"{i + 1} {n}") + " ", style=f"bold {accent}" if i == 0 else "")+ rule = Text(("-" if ascii_mode else "─") * min(width, 110), style="dim")++ # ---- model state bar+ bar_w = max(20, min(width, 110) - 2)+ st = Text(no_wrap=True, overflow="crop")+ st.append("Model state ", style=f"bold {accent}")+ for n in STATES:+ txt = f"[{n}]" if n == state else n+ st.append(txt + " ", style=(state_color if n == state and state != "Idle" else "bold" if n == state else "dim"))+ if done is not None and total:+ right_txt = f"{tokens(done)} / {tokens(total)} tokens · {pct}%"+ elif model["queued"]:+ right_txt = f"{model['queued']} queued"+ else:+ right_txt = ""+ st.append(" " * max(2, min(width, 110) - len(st.plain) - len(right_txt)) + right_txt, style="dim")+ state_bar = bar((done / total) if done is not None and total else None, bar_w, glyphs, state_color if state != "Idle" else accent)++ # ---- tiles+ g = model["gpu"]+ tile_w = max(18, (min(width, 110) - 3) // 4)+ inner = tile_w - 4+ spd, spd_note, spd_hist = model["speed"]+ util_scale = dict(lo=0, hi=100)+ tiles1 = [+ _tile("Speed", f"{fmt(spd, 1)} tok/s" if spd is not None else "-- tok/s", spd_note, spark(spd_hist, inner, glyphs), tile_w, accent=accent, box_style=box_style),+ _tile("GPU load", f"{fmt(g['util'])} %", (g["name"] or "graphics card") if g["count"] in (None, 1) else f"{g['name']} +{int(g['count']) - 1}",+ spark(g["util_hist"], inner, glyphs, **util_scale), tile_w, accent=accent, box_style=box_style),+ _tile("VRAM", f"{fmt(g['vram_used'], 1)} / {fmt(g['vram_total'], 0)} GB",+ (f"{int(model['experts']['slots']):,} experts cached" if model["experts"]["slots"] is not None else "graphics memory"),+ spark(g["vram_hist"], inner, glyphs, lo=0, hi=g["vram_total"]), tile_w, accent=accent, box_style=box_style),+ _tile("GPU temp", f"{fmt(g['temp'])} °C" if not ascii_mode else f"{fmt(g['temp'])} C", "",+ spark(g["temp_hist"], inner, glyphs, lo=20, hi=100), tile_w, accent=plain(AMBER) if color else "default", box_style=box_style),+ ]+ c, d = model["cpu"], model["disk"]+ gen = f"Gen{int(g['pcie_gen'])}" if g["pcie_gen"] else "PCIe"+ link = f"{gen} x{int(g['pcie_width'])}" if g["pcie_width"] else gen+ cpu_sub = (f"{int(c['cores'])} cores · {int(c['threads'])} threads" if c["cores"] and c["threads"] else+ f"{int(c['threads'])} threads" if c["threads"] else "")+ tiles2 = [+ _tile("Power", f"{fmt(g['power'])} W", f"of {fmt(g['power_limit'])} W limit" if g["power_limit"] else "", spark(g["power_hist"], inner, glyphs, lo=0),+ tile_w, accent=accent, box_style=box_style),+ _tile("PCIe", link, f"to GPU {fmt(g['pcie_rx'])} MB/s" if g["pcie_rx"] is not None else "to GPU --", spark(g["pcie_hist"], inner, glyphs, lo=0), tile_w,+ accent=accent, box_style=box_style),+ _tile("CPU", f"{fmt(c['load'])} %", cpu_sub, spark(c["hist"], inner, glyphs, lo=0, hi=100), tile_w, accent=accent, box_style=box_style),+ _tile("Disk read", f"{fmt(d['read'], 1)} MB/s", f"write {fmt(d['write'], 1)} MB/s" if d["write"] is not None else "write --",+ spark(d["hist"], inner, glyphs, lo=0), tile_w, accent=accent, box_style=box_style),+ ]++ def row(tiles: list[Panel]) -> Table:+ t = Table.grid(padding=(0, 1))+ for _ in tiles:+ t.add_column(width=tile_w)+ t.add_row(*tiles)+ return t++ # ---- bars and requests+ left_w = max(24, min(width, 110) * 2 // 5)+ used_ctx = (ctx_used / ctx_max) if ctx_used is not None and ctx_max else None+ e = model["experts"]+ e_frac = (e["slots"] / e["total"]) if e["slots"] is not None and e["total"] else None+ ram_used, ram_total = model["ram"]+ ram_frac = (ram_used / ram_total) if ram_used is not None and ram_total else None+ lines: list[Text] = []+ t = Text(no_wrap=True, overflow="crop")+ t.append("Context fill ", style=f"bold {accent}")+ t.append(f"{k(ctx_used)} / {k(ctx_max)}" + (f" {used_ctx * 100:.0f}%" if used_ctx is not None else ""))+ lines += [t, bar(used_ctx, left_w, glyphs, accent)]+ t = Text(no_wrap=True, overflow="crop")+ t.append("Experts in VRAM ")+ t.append(f"{fmt(e['slots'])}" + (f" · {e['gb']:.1f} GB" if e["gb"] is not None else ""), style="dim")+ lines += [t, bar(e_frac, left_w, glyphs, accent)]+ t = Text(no_wrap=True, overflow="crop")+ t.append("System RAM ")+ t.append(f"{fmt(ram_used, 1)} / {fmt(ram_total, 0)} GB", style="dim")+ lines += [t, bar(ram_frac, left_w, glyphs, plain(RED) if ram_frac is not None and ram_frac > 0.9 else accent)]++ req = Table(box=None, padding=(0, 1), show_header=True, header_style="dim", pad_edge=False)+ for col, just in (("TIME", "left"), ("STATUS", "left"), ("PROMPT", "right"), ("REUSED", "right"), ("OUTPUT", "right"), ("TOK/S", "right"),+ ("DURATION", "right")):+ req.add_column(col, justify=just, no_wrap=True)+ show = max(2, min(6, height - 20))+ for r in model["recent"][:show]:+ stamp = time.strftime("%H:%M:%S", time.localtime(r["time"])) if r["time"] else "--"+ req.add_row(stamp, Text(r["state"], style=plain(accent) if r["state"] == "Done" else plain(RED if r["state"] == "Error" else AMBER)),+ tokens(r["prompt"]), tokens(r["reused"]), tokens(r["output"]), fmt(r["tps"], 1), fmt(r["duration"], 1, " s"))+ if not model["recent"]:+ req.add_row("No requests yet", "", "", "", "", "", "")+ bottom = Table.grid(padding=(0, 3))+ bottom.add_column(width=left_w)+ bottom.add_column()+ right = Group(Text("Recent requests", style=f"bold {accent}"), req)+ bottom.add_row(Group(*lines), right)++ hint = Text(("1-7 views" if connect else "1-6 views") + " / filter Space freeze r refresh t theme b open in browser ? help q quit", style="dim")+ parts: list[Any] = [head, tabs, rule, st, state_bar, Text(""), row(tiles1), row(tiles2), bottom, Text(""), hint]+ return Group(*parts)diff --git a/tests/terminal/test_launch_tui.py b/tests/terminal/test_launch_tui.pyindex 3f3cd10aa..c1b0cb1ea 100644--- a/tests/terminal/test_launch_tui.py+++ b/tests/terminal/test_launch_tui.py@@ -219,7 +219,9 @@ class ModelStep(Base):def test_a_model_that_does_not_fit_can_still_be_chosen(self):async def case(app, pilot, ctx):await press(pilot, "enter", "end", "enter")- self.assertEqual(ctx.settings.model, "NVFP4-NVIDIA")+ last = ctx.rows[-1]+ self.assertEqual(last.verdict, "no") # the last row does not fit a 12 GB card+ self.assertEqual((ctx.settings.family, ctx.settings.model), (last.family, last.model))self.assertIn("Where should the model files be stored?", text_of(app))self.assertTrue(any("does not fit" in n for n in ctx.notes))self.run_app(case)diff --git a/tests/terminal/test_overview.py b/tests/terminal/test_overview.pynew file mode 100644index 000000000..a3faddf2e--- /dev/null+++ b/tests/terminal/test_overview.py@@ -0,0 +1,182 @@+"""The monitor's Overview (view 1): numbers from /metrics, the drawing, glyph fallbacks, and the monitor showing it."""++from __future__ import annotations++import asyncio+import io+import time+import unittest++from rich.console import Console++from strata_cli import overview+from strata_cli.monitor import MonitorApp++GIB = 2 ** 30+++def metrics(state="reading", **over):+ now = time.time()+ hist = lambda base, amp: [base + amp * ((i * 7) % 5) / 4 for i in range(60)] # noqa: E731+ m = {"time": now,+ "engine": {"model": "IQ3_S", "max_context": 131072, "expert_slots": 1644, "experts": 5120, "expert_cache_mib": 3280},+ "live": {"state": state, "queued": 0, "prompt_read": 35610 if state == "reading" else None, "prompt_total": 36742 if state == "reading" else None,+ "prompt_tokens": 36742, "generated": 120 if state == "generating" else 0, "max_tokens": 4096,+ "tok_s": 50.4 if state == "generating" else None, "prefill_tok_s_mean": 820.0},+ "hardware": {"gpu_util": 88, "gpu_mem_used": 11.2 * GIB, "gpu_mem_total": 12 * GIB, "gpu_temp": 54, "gpu_power": 114, "gpu_power_limit": 250,+ "gpu_pcie_rx_mb": 18237, "gpu_pcie_gen": 4, "gpu_pcie_width": 16, "cpu": 14, "ram_used": 58.5 * GIB, "ram_total": 63 * GIB,+ "disk_read_mb": 0.1, "disk_write_mb": 0.0},+ "hardware_static": {"gpu_name": "RTX 5070", "gpu_count": 1, "cores": 6, "threads": 12},+ "history": {"tok_s": hist(40, 10), "gpu_util": hist(80, 10), "gpu_mem_used": [11.2 * GIB] * 60, "gpu_temp": [54] * 60,+ "gpu_power": hist(110, 10), "gpu_pcie_rx_mb": hist(10000, 9000), "cpu": hist(10, 8), "disk_read_mb": hist(0, 3)},+ "requests": [{"id": "a", "time": now - 5, "duration_s": 429.8, "finish_reason": "stop", "prompt_tokens": 17183, "reused": 0,+ "output_tokens": 19511, "decode_ms": 386000},+ {"id": "b", "time": now - 9, "duration_s": 4.0, "finish_reason": "stop", "prompt_tokens": 626, "reused": 0,+ "output_tokens": 11, "decode_ms": 410}]}+ m.update(over)+ return m+++def draw(model, width=110, height=30, **kw) -> str:+ c = Console(width=width + 2, record=True, file=io.StringIO(), force_terminal=False, legacy_windows=False)+ c.print(overview.render(model, width=width, height=height, connection="http://127.0.0.1:8080", **kw))+ return c.export_text()+++class Build(unittest.TestCase):+ def test_reading_state_and_progress(self):+ m = overview.build(metrics("reading"))+ self.assertEqual(m["state"], "Reading")+ self.assertEqual(m["progress"], (35610.0, 36742.0))+ self.assertEqual(m["context"], (36742.0, 131072.0))+ self.assertEqual(m["speed"][:2], (820.0, "reading the prompt"))++ def test_generating_uses_the_live_rate_and_prompt_plus_generated_for_context(self):+ m = overview.build(metrics("generating"))+ self.assertEqual(m["state"], "Generating")+ self.assertEqual(m["speed"][:2], (50.4, "now"))+ self.assertEqual(m["context"][0], 36742 + 120)+ self.assertEqual(m["progress"], (120.0, 4096.0))++ def test_idle_shows_the_last_request(self):+ m = overview.build(metrics("idle"))+ self.assertEqual(m["state"], "Idle")+ self.assertEqual(m["speed"][1], "last request")+ self.assertAlmostEqual(m["speed"][0], 50.5, places=1) # 19511 tokens in 386 s+ self.assertEqual(m["context"][0], 17183 + 19511)+ self.assertEqual(m["progress"], (None, None))++ def test_queued_and_error(self):+ self.assertEqual(overview.build(metrics("idle", live={"state": "idle", "queued": 3}))["state"], "Queued")+ self.assertEqual(overview.build(metrics("idle", engine={"model": "x", "error": "boom"}))["state"], "Error")++ def test_hardware_in_gb(self):+ m = overview.build(metrics())+ self.assertAlmostEqual(m["gpu"]["vram_used"], 11.2)+ self.assertEqual(m["gpu"]["vram_total"], 12.0)+ self.assertAlmostEqual(m["ram"][0], 58.5)+ self.assertEqual(m["experts"], {"slots": 1644.0, "total": 5120.0, "gb": 3280 / 1024.0})++ def test_missing_numbers_stay_missing(self):+ m = overview.build({"live": {"state": "idle"}, "engine": {}, "hardware": {}, "requests": []})+ self.assertIsNone(m["gpu"]["util"])+ self.assertIsNone(m["speed"][0])+ text = draw(m)+ self.assertIn("-- tok/s", text)+ self.assertIn("No requests yet", text)+ self.assertNotIn("0 %", text.replace("-- %", "")) # no invented zero++ def test_a_server_without_metrics_gives_none(self):+ self.assertIsNone(overview.build(None))+ self.assertIsNone(overview.build({"engine": {}}))+++class Draw(unittest.TestCase):+ def test_the_approved_layout(self):+ text = draw(overview.build(metrics("reading")), connect=True)+ for part in ("STRATA Serving", "IQ3_S 128K", "Reading prompt · 97%", "[LIVE]", "[1 Overview]", "7 Connect",+ "Model state", "[Reading]", "35,610 / 36,742 tokens · 97%",+ "Speed", "GPU load", "VRAM", "GPU temp", "Power", "PCIe", "CPU", "Disk read",+ "88 %", "RTX 5070", "11.2 / 12 GB", "1,644 experts cached", "54 °C", "114 W", "of 250 W limit", "Gen4 x16",+ "to GPU 18,237 MB/s", "6 cores · 12 threads", "0.1 MB/s", "write 0.0 MB/s",+ "Context fill", "36K / 128K", "Experts in VRAM", "1,644 · 3.2 GB", "System RAM", "58.5 / 63 GB",+ "Recent requests", "TIME", "STATUS", "PROMPT", "REUSED", "OUTPUT", "TOK/S", "DURATION", "17,183", "19,511", "429.8 s",+ "1-7 views", "b open in browser", "q quit"):+ self.assertIn(part, text, part)++ def test_without_connect_the_hint_says_six_views(self):+ text = draw(overview.build(metrics()), connect=False)+ self.assertIn("1-6 views", text)+ self.assertNotIn("7 Connect", text)++ def test_ascii_mode_has_no_glyphs_the_classic_windows_console_font_lacks(self):+ text = draw(overview.build(metrics()), glyphs="ascii", color=False)+ for bad in "▁▂▃▄▅▆▇█░╭╮╰╯✓●":+ self.assertNotIn(bad, text, bad)+ self.assertIn("#", text)+ self.assertIn("54 C", text)++ def test_fits_the_width_it_is_given(self):+ for w in (80, 100, 120):+ text = draw(overview.build(metrics()), width=w - 2)+ self.assertTrue(all(len(line) <= w for line in text.splitlines()), w)++ def test_spark(self):+ self.assertEqual(len(overview.spark([1, 2, 3], 8, "unicode")), 8)+ self.assertEqual(overview.spark([None, None], 4, "unicode"), " ")+ self.assertEqual(overview.spark([0, 100], 2, "unicode", lo=0, hi=100), "▁█")+ self.assertEqual(overview.spark([0, 100], 2, "ascii", lo=0, hi=100), ".@")+++class Source:+ root = "http://127.0.0.1:8095"++ def __init__(self, raw):+ self.raw = raw+ self.base_url = self.root++ def get(self, path):+ if path == "/metrics":+ return self.raw+ raise AssertionError(path)++ def snapshot(self):+ return {"source": "fixture", "sampled_at": time.time(), "schema_version": 1, "instance": {"id": "x"},+ "model": {"id": "IQ3_S", "loaded": True}, "work": {}, "rates": {}, "devices": [], "recent": [], "totals": {},+ "topology": {}, "quality": {}}+++class InTheMonitor(unittest.TestCase):+ def test_view_one_is_the_new_overview_and_the_others_stay(self):+ async def go():+ app = MonitorApp(Source(metrics("generating")), None, interval=30)+ async with app.run_test(size=(120, 36)) as pilot:+ await pilot.pause(0.5)+ shown = "\n".join(s.text for s in app.screen._compositor.render_strips())+ self.assertIn("Model state", shown)+ self.assertIn("Generating", shown)+ await pilot.press("2")+ await pilot.pause(0.2)+ shown = "\n".join(s.text for s in app.screen._compositor.render_strips())+ self.assertNotIn("Model state", shown)+ self.assertIn("Serving", shown)+ asyncio.run(go())++ def test_an_older_server_without_metrics_keeps_the_earlier_overview(self):+ class Old(Source):+ def get(self, path):+ from strata_cli.client import ClientError+ raise ClientError("not found", http_status=404)++ async def go():+ app = MonitorApp(Old(None), None, interval=30)+ async with app.run_test(size=(120, 36)) as pilot:+ await pilot.pause(0.5)+ shown = "\n".join(s.text for s in app.screen._compositor.render_strips())+ self.assertNotIn("Model state", shown)+ self.assertIn("MONITOR", shown)+ asyncio.run(go())+++if __name__ == "__main__":+ unittest.main()