Skip to content

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++

a failed start ends its own runtime host, checks the port first and quotes only this attempt's logs (X01)

Niko12219222b6a1d33e
diff --git a/strata_cli/launch/runner.py b/strata_cli/launch/runner.py
index 4af64a97e..b505e3018 100644
--- a/strata_cli/launch/runner.py
+++ b/strata_cli/launch/runner.py
@@ -1392,6 +1392,8 @@ class Runner:
runtime.stop(name, timeout=120)
except ProfileError as e:
raise LaunchError(f"The running server could not be stopped: {e}.", "Run `strata launch --stop` first.", 6) from None
+ self.check_port_free()
+ self._marks = self.log_marks() # what the logs already held: only this attempt's lines are ever quoted
self.emit("start", "Starting the engine and loading the model. The first start can take a few minutes.", None)
done = threading.Event()
box: dict[str, Any] = {}
@@ -1415,14 +1417,69 @@ class Runner:
raise Cancelled()
self.emit("start", f"Loading the model… {int(time.monotonic() - t0)} s", None)
if "e" in box:
+ self.end_failed_start()
raise LaunchError(f"The server could not start: {box['e']}", _log_hint(self.config_path), 6)
r = box["r"]
if not r.get("ok"):
- text, nxt = explain_engine_exit(r, self.config_path)
+ text, nxt = explain_engine_exit(r, self.config_path, marks=self._marks, server_log=self.server_log_path())
+ self.end_failed_start()
raise LaunchError(text, nxt, 6)
self.emit("start", "The model is loaded and answering.", 100)
self.report_drafting()
+ _marks: dict | None = None
+
+ def server_log_path(self) -> Path | None:
+ try:
+ from strata_control import runtime
+ return runtime.runtime_dir() / f"{self.profile}.server.log"
+ except Exception: # noqa: BLE001
+ return None
+
+ def log_marks(self) -> dict:
+ """{log path: size now} for the engine log and the runtime's server log, so a failed start quotes only what this attempt wrote."""
+ marks = {}
+ paths = [self.server_log_path()]
+ try:
+ paths.append(Path(read_config(self.config_path).get("log"))) if self.config_path else None
+ except (OSError, ValueError, TypeError):
+ pass
+ for path in paths:
+ try:
+ marks[str(path)] = Path(path).stat().st_size if path else 0
+ except OSError:
+ if path:
+ marks[str(path)] = 0
+ return marks
+
+ def check_port_free(self) -> None:
+ """Before the host starts: a port another program holds is said at once, in words (X01), and nothing is left running."""
+ import socket
+ port = int(self.s.port)
+ busy = False
+ for host in ("127.0.0.1",):
+ try:
+ with socket.create_connection((host, port), timeout=1.0):
+ busy = True
+ except OSError:
+ pass
+ if not busy:
+ return
+ raise LaunchError(f"Port {port} is already in use by another program.",
+ f"Close it, or start with a different port (for example --port {port + 1}).", 6)
+
+ def end_failed_start(self) -> None:
+ """A start that failed leaves nothing alive: the runtime host of this attempt is stopped, or ended by its recorded identity."""
+ from strata_control import runtime
+ try:
+ runtime.stop(self.profile, timeout=30)
+ except Exception: # noqa: BLE001
+ pass
+ try:
+ runtime.cleanup(self.profile)
+ except Exception: # noqa: BLE001
+ pass
+
def report_drafting(self) -> None:
"""Say which drafter runs when the engine fell back from the one that was asked for ("Drafting: MTP (DFlash2 didn't fit)"). Best effort."""
try:
@@ -1691,8 +1748,25 @@ KNOWN_CAUSES = (
)
-def engine_log_tail(config: Path | None, limit: int = 65536) -> tuple[list[str], str | None]:
- """(last lines of the engine log, its path); ([], path-or-None) when it cannot be read."""
+def _lines_since(path: str | os.PathLike | None, mark: int | None, limit: int = 65536) -> list[str]:
+ """Lines written to a log after `mark` bytes (the whole tail when there is no mark); a log that got shorter was started over."""
+ if not path:
+ return []
+ try:
+ with open(path, "rb") as stream:
+ stream.seek(0, os.SEEK_END)
+ size = stream.tell()
+ start = 0 if (mark is None or mark > size) else mark
+ stream.seek(max(start, size - limit))
+ data = stream.read().decode("utf-8", "replace")
+ except OSError:
+ return []
+ return [x.rstrip() for x in data.splitlines() if x.strip()]
+
+
+def engine_log_tail(config: Path | None, limit: int = 65536, since: dict | None = None) -> tuple[list[str], str | None]:
+ """(last lines of the engine log, its path); ([], path-or-None) when it cannot be read. With `since` (log sizes before the attempt)
+ only this attempt's lines are returned."""
log = None
if config:
try:
@@ -1701,6 +1775,8 @@ def engine_log_tail(config: Path | None, limit: int = 65536) -> tuple[list[str],
log = None
if not log:
return [], None
+ if since is not None:
+ return _lines_since(log, since.get(str(log)), limit), str(log)
try:
with open(log, "rb") as stream:
stream.seek(0, os.SEEK_END)
@@ -1711,10 +1787,20 @@ def engine_log_tail(config: Path | None, limit: int = 65536) -> tuple[list[str],
return [x.rstrip() for x in data.splitlines() if x.strip()], str(log)
-def explain_engine_exit(result: dict, config: Path | None) -> tuple[str, str]:
+def explain_engine_exit(result: dict, config: Path | None, marks: dict | None = None, server_log: Path | None = None) -> tuple[str, str]:
"""(text, next) for a start that did not reach ready: the stage, the cause line from the engine log, the next step and the log path
(round 8, L3: an Arc engine died with 'No device of requested type available' and the person saw only 'server_exited')."""
- lines, log = engine_log_tail(config)
+ server_lines = _lines_since(server_log, (marks or {}).get(str(server_log))) if (server_log and marks is not None) else []
+ for x in server_lines: # the server's own refusal (server.py: error: ...) is the cause when there is one
+ busy = re.search(r"port (\d+) is already in use", x)
+ if busy:
+ return (f"Port {busy.group(1)} is already in use by another program.",
+ f"Close it, or start with a different port (for example --port {int(busy.group(1)) + 1}). The server's log: {server_log}")
+ lines, log = engine_log_tail(config, since=marks)
+ if marks is not None and not lines:
+ own = [x for x in server_lines if re.search(r"server\.py: error:|error:", x, re.I)]
+ if own:
+ lines, log = own, str(server_log)
lines = lines or [x.strip() for x in str(result.get("log_tail") or "").split(" | ") if x.strip()]
cause = next((x for x in reversed(lines) if CAUSE_LINE.search(x) and not x.lstrip().startswith(("strata generate: ", "strata serve: expert"))), None)
cause = (cause or (lines[-1] if lines else "")).strip()
diff --git a/tests/terminal/test_tui3.py b/tests/terminal/test_tui3.py
new file mode 100644
index 000000000..4dd0a64f9
--- /dev/null
+++ b/tests/terminal/test_tui3.py
@@ -0,0 +1,102 @@
+"""Fixes after the TUI stress re-run on 67a48427b: a failed start cleans up and quotes this attempt's logs, the Folder text, the save/restart order,
+the saved config without --mtp when drafting is off, and the reused-pack line."""
+
+import json
+import os
+import socket
+import tempfile
+import unittest
+from pathlib import Path
+from unittest import mock
+
+from strata_cli.launch import settings as S
+from strata_cli.launch import runner as R
+from strata_cli.launch.catalog import Catalog
+from strata_cli.launch.runner import LaunchError, Runner
+from tests.terminal.launch_support import HW_12, Home, catalog
+
+
+class FailedStart(unittest.TestCase):
+ 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)
+ self.tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(self.tmp.cleanup)
+ self.base = Path(self.tmp.name)
+
+ def runner(self, port=8080):
+ s = S.LaunchSettings(family="qwen", model="IQ3_XXS", context=32768, name="gyro", port=port)
+ r = Runner(s, catalog=catalog())
+ r.said = []
+ r.emit_cb = lambda e: r.said.append(e.text)
+ return r
+
+ def test_a_busy_port_is_said_before_anything_starts(self):
+ with socket.socket() as srv:
+ srv.bind(("127.0.0.1", 0))
+ srv.listen(1)
+ port = srv.getsockname()[1]
+ r = self.runner(port)
+ with mock.patch("strata_control.runtime.status", return_value={"state": "stopped"}), \
+ mock.patch("strata_control.runtime.start", side_effect=AssertionError("must not start")):
+ with self.assertRaises(LaunchError) as cm:
+ r.start()
+ self.assertIn(f"Port {port} is already in use by another program.", cm.exception.text)
+ self.assertIn("--port", cm.exception.next)
+
+ def test_the_cause_comes_from_this_attempts_server_log_not_the_old_engine_log(self):
+ engine = self.base / "engine.log"
+ server = self.base / "srv.log"
+ engine.write_text("strata verify: captured the 2-token window\nlong ago error: old cause\n")
+ server.write_text("")
+ cfg = self.base / "cfg.json"
+ cfg.write_text(json.dumps({"log": str(engine)}))
+ marks = {str(engine): engine.stat().st_size, str(server): 0}
+ with open(server, "a") as f:
+ f.write("server.py: error: port 8190 is already in use - is Strata (or another server) already running?\n")
+ text, nxt = R.explain_engine_exit({"state": "failed", "error": "server_exited", "engine_exit_code": 2}, cfg, marks=marks, server_log=server)
+ self.assertIn("Port 8190 is already in use by another program.", text)
+ self.assertIn("--port 8191", nxt)
+ self.assertNotIn("old cause", text + nxt)
+ self.assertNotIn("captured the 2-token", text + nxt)
+
+ def test_an_old_engine_log_is_never_quoted_when_this_attempt_wrote_nothing(self):
+ engine = self.base / "engine.log"
+ engine.write_text("error: from the earlier successful run\n")
+ cfg = self.base / "cfg.json"
+ cfg.write_text(json.dumps({"log": str(engine)}))
+ marks = {str(engine): engine.stat().st_size}
+ text, _ = R.explain_engine_exit({"state": "failed", "error": "server_exited"}, cfg, marks=marks, server_log=None)
+ self.assertNotIn("earlier successful run", text)
+
+ def test_a_failed_start_ends_its_own_host(self):
+ r = self.runner()
+ r.config_path = None
+ calls = []
+ with mock.patch("strata_control.runtime.status", return_value={"state": "stopped"}), \
+ mock.patch.object(Runner, "check_port_free"), \
+ mock.patch("strata_control.runtime.start", return_value={"ok": False, "state": "failed", "error": "server_exited", "engine_exit_code": 2}), \
+ mock.patch("strata_control.runtime.stop", side_effect=lambda n, timeout=0: calls.append(("stop", n))), \
+ mock.patch("strata_control.runtime.cleanup", side_effect=lambda n=None: calls.append(("cleanup", n))):
+ with self.assertRaises(LaunchError):
+ r.start()
+ self.assertEqual(calls, [("stop", "launch-gyro"), ("cleanup", "launch-gyro")])
+
+ def test_a_start_that_raises_also_ends_its_host(self):
+ r = self.runner()
+ calls = []
+ with mock.patch("strata_control.runtime.status", return_value={"state": "stopped"}), \
+ mock.patch.object(Runner, "check_port_free"), \
+ mock.patch("strata_control.runtime.start", side_effect=RuntimeError("runtime host failed to start")), \
+ mock.patch("strata_control.runtime.stop", side_effect=lambda n, timeout=0: calls.append("stop")), \
+ mock.patch("strata_control.runtime.cleanup", side_effect=lambda n=None: calls.append("cleanup")):
+ with self.assertRaises(LaunchError):
+ r.start()
+ self.assertEqual(calls, ["stop", "cleanup"])
+
+
+if __name__ == "__main__":
+ unittest.main()