diff --git a/strata_cli/launch/app.py b/strata_cli/launch/app.py index 6fe1c6c2..1ae6c96d 100644 --- a/strata_cli/launch/app.py +++ b/strata_cli/launch/app.py @@ -185,7 +185,7 @@ class LaunchApp(App): def goto(self, step_id: str) -> None: self.current = step_id - self.switch_screen(SCREENS[step_id]()) + self.show(SCREENS[step_id]()) def begin_setup(self, *, preset: str | None = None, own_files: bool = False) -> None: self.ctx.tune_chosen = False @@ -199,7 +199,7 @@ class LaunchApp(App): self.ctx.picked_in_list = False self.ctx.invalidate_rows() self.current = "model" - self.switch_screen(ModelScreen()) + self.show(ModelScreen()) def advance(self) -> None: flow = self.flow() @@ -214,7 +214,7 @@ class LaunchApp(App): self.goto(flow[i - 1]) else: self.current = "start" - self.switch_screen(StartScreen()) + self.show(StartScreen()) # ---- starting def start_preset(self, name: str) -> None: @@ -222,6 +222,15 @@ class LaunchApp(App): self.ctx.settings = s self.run_launch() + def show(self, screen) -> None: + """Put `screen` in front. The first screen of a run that skips the questions has nothing to replace (the app starts with only its + empty base screen), so it is pushed; every later one replaces the one on top. (switch_screen on the base screen raised + IndexError: pop from empty list, tester report T02.)""" + if len(self.screen_stack) <= 1: + self.push_screen(screen) + else: + self.switch_screen(screen) + def run_launch(self, confirmed: bool = False) -> None: s = self.ctx.settings self.cancel = threading.Event() @@ -244,7 +253,7 @@ class LaunchApp(App): screen = RunScreen() self.run_screen = screen self.current = "run" - self.switch_screen(screen) + self.show(screen) self.running = True threading.Thread(target=self._work, daemon=True).start() @@ -304,7 +313,7 @@ class LaunchApp(App): def back_to_start(self) -> None: self.current = "start" - self.switch_screen(StartScreen()) + self.show(StartScreen()) # ------------------------------------------------------------------------------------------------ the monitor @@ -362,6 +371,11 @@ def run_tui(settings: S.LaunchSettings | None, *, catalog: Catalog | None = None 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 getattr(app, "return_code", 0) not in (0, None) or getattr(app, "_exception", None) is not None: + # The screens stopped on an error of their own (not a launch failure, which the run screen shows). Never end quietly with exit 0. + print("strata: The launch screens stopped because of an unexpected problem, and nothing was started. " + "Run the same command with --no-tui to see the steps as plain lines, and send the output if you ask for help.", file=sys.stderr) + return 1 if app.stopped_after_save: print(f"Saved the preset '{ctx.settings.name}'. Start it with: strata launch --preset \"{ctx.settings.name}\"") return 0 diff --git a/strata_cli/launch/cmd.py b/strata_cli/launch/cmd.py index fb810705..b4367003 100644 --- a/strata_cli/launch/cmd.py +++ b/strata_cli/launch/cmd.py @@ -307,6 +307,19 @@ def _stop_all(name: str | None) -> int: return 0 +def _tui(run, *args, **kw) -> int: + """Open the screens. An error of the screens themselves ends with a plain sentence and a non-zero exit code, never a traceback and exit 0 + (tester report T02).""" + try: + return int(run(*args, **kw) or 0) + except KeyboardInterrupt: + raise + except Exception as e: # noqa: BLE001 + print(f"strata: The launch screens stopped because of an unexpected problem ({type(e).__name__}: {e}), and nothing was started. " + "Run the same command with --no-tui to see the steps as plain lines.", file=sys.stderr) + return 1 + + def running_launches() -> list[dict]: """Launch profiles whose server is up now: [{name, profile, url, state}].""" from strata_control import profiles, runtime @@ -435,7 +448,7 @@ def run_launch(a: argparse.Namespace) -> int: if catalog is None: return fail("This Terminal does not carry the model list, so the screens cannot open. Install it again with the one-line installer.", 5) from strata_cli.launch import app - return app.run_tui(None, catalog=catalog, feed=a.feed) + return _tui(app.run_tui, None, catalog=catalog, feed=a.feed) if catalog is None: return fail("This Terminal does not carry the model list. Install it again with the one-line installer.", 5) s, notes = apply_defaults(s, catalog) @@ -473,7 +486,7 @@ def run_launch(a: argparse.Namespace) -> int: _say(f"[!] {fw}") if interactive and have_choices: from strata_cli.launch import app - return app.run_tui(s, catalog=catalog, feed=a.feed, auto_run=True, save=bool(a.save_preset)) + return _tui(app.run_tui, s, catalog=catalog, feed=a.feed, auto_run=True, save=bool(a.save_preset)) try: result = runner.run() except LaunchError as e: diff --git a/tests/terminal/test_tuiqa_launch.py b/tests/terminal/test_tuiqa_launch.py new file mode 100644 index 00000000..db635808 --- /dev/null +++ b/tests/terminal/test_tuiqa_launch.py @@ -0,0 +1,109 @@ +"""Tester report 2026-10-18 (Terminal 0.1.45), T02: `strata launch` with flags opened the screens straight into the run, which crashed +(IndexError: pop from empty list in switch_screen) and exited 0. The run now opens on its own screen; an error of the screens ends non-zero.""" + +from __future__ import annotations + +import asyncio +import contextlib +import io +import unittest +from unittest import mock + +from textual.app import App + +from strata_cli.launch import app as launch_app +from strata_cli.launch import settings as S +from strata_cli.launch.app import Ctx, LaunchApp +from strata_cli.launch.widgets import Glyphs +from tests.terminal.launch_support import HW_12, Home, catalog, text_of +from tests.terminal.test_launch_tui import FakeRunner, SIZE, make_ctx + + +def flags_settings() -> S.LaunchSettings: + """What `strata launch --model q2_0 --family qwen --context 128k --kv int8 --vision cpu --parallel 1 --tune skip --access local --port 8081` gives.""" + return S.LaunchSettings(name="QA flags", family="qwen", model="Q2_0", context=131072, kv="int8", vision="cpu", parallel=1, tune="skip", + access="local", port=8081) + + +class FlagsOpenTheRun(unittest.TestCase): + def setUp(self): + self.home = Home().__enter__() + self.addCleanup(self.home.__exit__, None, None, None) + FakeRunner.instances.clear() + FakeRunner.behaviour = "ok" + FakeRunner.download = 0.0 + p = mock.patch("strata_cli.launch.app.Runner", FakeRunner) + p.start() + self.addCleanup(p.stop) + + def test_auto_run_shows_the_run_screen_and_finishes(self): + app = LaunchApp(make_ctx(settings=flags_settings()), auto_run=True) + + async def go(): + async with app.run_test(size=SIZE) as pilot: + for _ in range(60): + await pilot.pause(0.05) + if app.result is not None: + break + return app.result + + result = asyncio.run(go()) + self.assertIsNotNone(result, "the run did not finish: the screens crashed instead of opening the run") + self.assertEqual(result.urls, ["http://127.0.0.1:8081"]) + + def test_auto_run_with_a_big_download_asks_first_and_then_runs(self): + FakeRunner.download = 66.0 + app = LaunchApp(make_ctx(settings=flags_settings()), auto_run=True) + + async def go(): + async with app.run_test(size=SIZE) as pilot: + await pilot.pause(0.3) + self.assertIn("Download about 66 GB?", text_of(app)) + await pilot.press("enter") # Download and start + for _ in range(60): + await pilot.pause(0.05) + if app.result is not None: + break + return app.result + + self.assertIsNotNone(asyncio.run(go())) + + def headless_run(self, app): + return App.run(app, headless=True) + + def test_run_tui_with_flags_returns_zero_and_opens_the_monitor(self): + calls = [] + with mock.patch.object(LaunchApp, "run", lambda self, **kw: App.run(self, headless=True)), \ + mock.patch.object(launch_app, "run_monitor", lambda info, **kw: calls.append(info) or 0), \ + mock.patch.object(launch_app, "Catalog", create=True): + cat = catalog(HW_12) + code = launch_app.run_tui(flags_settings(), catalog=cat, auto_run=True) + self.assertEqual(code, 0) + self.assertEqual(len(calls), 1) + + def test_an_error_of_the_screens_ends_non_zero_with_a_plain_sentence(self): + class Broken(FakeRunner): + def __init__(self, *a, **kw): + raise RuntimeError("boom") + + err = io.StringIO() + with mock.patch.object(LaunchApp, "run", lambda self, **kw: App.run(self, headless=True)), \ + mock.patch.object(launch_app, "Runner", Broken), \ + contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): + code = launch_app.run_tui(flags_settings(), catalog=catalog(HW_12), auto_run=True) + self.assertNotEqual(code, 0) + self.assertIn("nothing was started", err.getvalue()) + self.assertIn("--no-tui", err.getvalue()) + + def test_the_command_turns_a_crash_into_exit_one(self): + from strata_cli.launch import cmd + err = io.StringIO() + with contextlib.redirect_stderr(err): + code = cmd._tui(lambda *a, **k: (_ for _ in ()).throw(IndexError("pop from empty list"))) + self.assertEqual(code, 1) + self.assertIn("unexpected problem", err.getvalue()) + self.assertNotIn("Traceback", err.getvalue()) + + +if __name__ == "__main__": + unittest.main()