diff --git a/apps/host/server/update-messages.mjs b/apps/host/server/update-messages.mjs index 00804375..bf7d5e56 100644 --- a/apps/host/server/update-messages.mjs +++ b/apps/host/server/update-messages.mjs @@ -35,7 +35,7 @@ export function plainUpdateError(error, subject = 'engine') { } else if (/another update is already running/i.test(raw)) { message = 'An update is already running.'; next = `Wait a moment, then press ${button} in Settings.`; - } else if (/test start|did not answer a one-token|did not become ready/i.test(raw)) { + } else if (/test start|did not start correctly|did not answer a one-token|did not become ready/i.test(raw)) { message = `The new ${subject} didn't start correctly, so nothing was changed. ${keep}`; } else if (looksOffline(error) || looksOffline(raw)) { message = 'No internet connection.'; diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 92ba6924..c66ea6d4 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -1069,6 +1069,12 @@ print(r.choices[0].message.content) Claude Code ask for 32,000 on every request); the thinking budget follows the shortened cap. For the strict llama.cpp behaviour (400) set `"fit_max_tokens": false` in `strata-.json` or pass `--no-fit-max-tokens` to `serve/server.py`. A prompt that leaves no room at all is always refused. +- **`max_tokens` and the `model` field (0.2.3).** `max_tokens`, `max_completion_tokens` and `max_output_tokens` must be at least 1 when + they are sent: `-1` and `0` used to mean "no cap" and are now a 400 that names the field ("max_tokens must be at least 1 ... Leave it + out to let the answer use the room that is left"). Leave the field out for the rest of the context. The `model` field is not matched + against the loaded model on purpose: one server serves one model, and OpenAI clients often send a name of their own (`gpt-4o`, `local`), + so any name is answered by the loaded model (a name from `aliases` and the model's own name work the same). Only `GET /v1/models` says what + is loaded. - **Model aliases** (0.1.32). `"aliases": ["qwen", "local-model"]` in `strata-.json` lists the model under those names too in `/v1/models` (each with its own `id`, and in the model's `aliases`), like llama-server's `--alias`; a request naming one is answered under that name. Any other name is still served, as before. diff --git a/serve/server.py b/serve/server.py index 87f59459..011fd8a4 100644 --- a/serve/server.py +++ b/serve/server.py @@ -7914,6 +7914,18 @@ def start_replicas(cfg: dict, groups: list[list[int]], exe: str, effort_end: lis return rp.ReplicaEngine(engines, load_tokens=load) +def check_max_tokens(req) -> None: + """K05: an answer length below 1 (``"max_tokens": -1``, 0) is a 400 that names the field and says what to do. It used to be read as "no cap", so a + client bug could hold the only slot for the whole context. A field that is absent or null is not checked (the answer then uses the room left).""" + if not isinstance(req, dict): + return + for key in ("max_tokens", "max_completion_tokens", "max_output_tokens"): + value = req.get(key) + if isinstance(value, (int, float)) and not isinstance(value, bool) and value < 1: + raise ValueError(f"{key} must be at least 1 (the request said {value:g}). Leave it out to let the answer use the room that is left " + "in the context.") + + def check_request_sampling(req) -> None: """A request's sampling field of the wrong JSON type ("temperature": "0.7", "top_k": 40.0, "seed": true) is a 400 that names the field, as the same value is through POST /settings or the run config. Unchecked, the value @@ -7921,6 +7933,7 @@ def check_request_sampling(req) -> None: types are checked: a number (int or float, never bool) for temperature, top_p, min_p and the three penalties; an integer (never bool or float) for top_k, penalty_last_n and seed. Ranges behave as before (a temperature <= 0 is greedy, top_k 0 or above 64 is clamped, a seed <= 0 is unset), and an absent or null field is not checked.""" + check_max_tokens(req) if isinstance(req, dict): # #1819: llama.cpp's names; the OpenAI-style field wins when both are sent if req.get("repeat_penalty") is not None and req.get("repetition_penalty") is None: req["repetition_penalty"] = req["repeat_penalty"] diff --git a/serve/test_server.py b/serve/test_server.py index 3e60dee4..62a17d10 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -115,9 +115,8 @@ class MaxTokens(unittest.TestCase): ("98179", "0", "17565", "699.6", "0")) def test_unset_budget_is_the_rest_of_the_context(self): - cases = {"openai": [{"max_tokens": -1}, {"max_tokens": 0}, {}, {"max_tokens": None}, - {"max_completion_tokens": -1}, {"max_completion_tokens": None, "max_tokens": None}], - "anthropic": [{"max_tokens": -1}, {"max_tokens": 0}, {}, {"max_tokens": None}]} + cases = {"openai": [{}, {"max_tokens": None}, {"max_completion_tokens": None, "max_tokens": None}], + "anthropic": [{}, {"max_tokens": None}]} for api, budgets in cases.items(): for budget in budgets: with self.subTest(api=api, budget=budget): @@ -126,6 +125,16 @@ class MaxTokens(unittest.TestCase): self.assertEqual(self.engine.last_max_new, CTX - CTX_SLACK - pt) self.assertGreater(ct, 1024) # the whole answer, not cut at the old 1024 fallback + def test_a_budget_below_one_is_a_400_that_names_the_field(self): + """K05 (0.2.3): -1 and 0 used to mean "no cap"; a client bug could then hold the only slot for the whole context.""" + for api, field in (("openai", "max_tokens"), ("openai", "max_completion_tokens"), ("anthropic", "max_tokens")): + for value in (-1, 0): + with self.subTest(api=api, field=field, value=value): + s, b, _, _ = self.call(api, **{field: value}) + self.assertEqual(s, 400, b) + self.assertIn(f"{field} must be at least 1", b["error"]["message"]) + self.assertIn("Leave it out", b["error"]["message"]) + def test_explicit_budget_is_honoured(self): for api, budget in [("openai", {"max_tokens": 50}), ("openai", {"max_completion_tokens": 50}), ("openai", {"max_completion_tokens": 50, "max_tokens": 9}), @@ -225,7 +234,7 @@ class MaxTokens(unittest.TestCase): over = pa - pt0 # the Anthropic template may differ slightly with self.subTest(api=api, room=5): # a few tokens left: the budget is exactly those text = "y" * (CTX - CTX_SLACK - overhead - over - 5) - s, b, pt, ct = self.call(api, text=text, max_tokens=-1) + s, b, pt, ct = self.call(api, text=text, max_tokens=None) self.assertEqual(s, 200, b) self.assertEqual(self.engine.last_max_new, 5) self.assertEqual(ct, 5) @@ -244,7 +253,7 @@ class MaxTokens(unittest.TestCase): with self.subTest(api=api): out = io.StringIO() with contextlib.redirect_stdout(out): - _, _, pt, _ = self.call(api, max_tokens=-1) + _, _, pt, _ = self.call(api, max_tokens=None) self.assertIn(f"max_new={CTX - CTX_SLACK - pt} ", out.getvalue()) finally: del os.environ["STRATA_DEBUG"] diff --git a/strata_control/updater.py b/strata_control/updater.py index cbe23c28..7a55b804 100644 --- a/strata_control/updater.py +++ b/strata_control/updater.py @@ -76,6 +76,35 @@ class TestStartFailed(UpdateError): BUSY = re.compile(r"timed?[ -]?out|out of memory|\boom\b|not enough (gpu |video |free )?memory|vram|memory allocation|" r"cudaerrormemory|device (is )?(busy|in use)|resource busy|no free (gpu|memory)", re.I) + @staticmethod + def plain(label: str, reason: str, busy: bool, hint: str | None) -> str: + """The one text of a failed test start (X01): what did not work in plain words, that nothing was changed, and one next step. The engine's + own words stay in ``reason`` (the state file, the JSON output and the kept log), never in this sentence.""" + what = "Control" if label.startswith("Control") else "engine" + head = (f"The new {label} did not start correctly on this computer, so it was not installed. Your current {what} is unchanged." + if what == "Control" else + f"The new engine {label} did not start correctly on this computer, so it was not installed. Your current engine is unchanged.") + low = reason.lower() + if what == "Control": + cause = f"It answered `--version` with: {reason.split('--version said', 1)[1].strip()}" if "--version said" in low else \ + "It did not answer `--version` in time or at all." + elif "one-token probe" in low: + cause = "The engine started but stopped before it answered a test question." + elif "--version exited" in low or "could not run" in low: + cause = "The engine program did not run on this computer." + elif "no installed model" in low: + cause = "There is no installed model to test the engine with." + elif low.startswith("it "): # Strata's own plain sentences ("it was still loading the model after 10 minutes") + cause = reason[:1].upper() + reason[1:].rstrip(".") + "." + elif busy: + cause = "The graphics card or memory was in use." + else: + cause = "The engine reported a problem while it started." + if hint is None: + hint = ("Run `strata update --force` to try again; if it fails again, report it with the log." if not busy and what == "engine" + else "Run `strata update` again.") + return f"{head} {cause} {hint}" + def __init__(self, label: str, reason: str, *, busy: bool | None = None, hint: str | None = None): # busy: True/False says outright whether the card or memory was busy (the wait-expired reasons carry the engine's log tail, which # mentions VRAM on every healthy start and must not be read as a busy card, #3683); None reads it from the words of the reason @@ -83,8 +112,7 @@ class TestStartFailed(UpdateError): if hint is None and self.busy: hint = ("The graphics card or memory looks busy: close programs that use the card (games, video, other models) " "and run `strata update` again.") - super().__init__(f"The new engine {label} did not pass its test start ({reason}). It was not installed; " - "your current engine is unchanged." + (" " + hint if hint else ""), 6) + super().__init__(self.plain(label, reason, self.busy, hint), 6) self.label = label self.reason = reason @@ -1007,6 +1035,10 @@ def rollback_engine(ctx: Context) -> dict: except (ec.ComponentProblem, OSError) as exc: raise UpdateError(f"the previous engine ({previous}) can no longer be used: {exc}", 5) from None current, _ = installed_engine(ctx.components, ctx.hardware) + if current is not None and current.root.name == previous: + # an earlier rollback is still in force (S3: the text said "0.1.45.3 -> 0.1.45.3"): there is nothing to leave behind + raise UpdateError(f"the engine is already the previous one ({current.version}); there is nothing older to return to. " + "Run `strata update --force` to try the newer version again.", 2) leaving = current.root.name if current else None before_identity = preset_marks.identity_of(current, _manifest_sha(current.root)) if current else None section["pinned"] = previous diff --git a/tests/terminal/test_tui2_023_messages.py b/tests/terminal/test_tui2_023_messages.py new file mode 100644 index 00000000..5dd38ff1 --- /dev/null +++ b/tests/terminal/test_tui2_023_messages.py @@ -0,0 +1,70 @@ +"""0.2.3 RC stress test: X01 (a failed engine test start gives a plain cause and one next step, not the engine's words) and #18 (the rollback text +never says "0.1.45.3 -> 0.1.45.3").""" + +import json +import unittest + +from strata_control import updater +from tests.terminal import test_updater +from tests.terminal import update_fixture as fx + + +class TestStartText(unittest.TestCase): + RAW = "it did not answer a one-token probe (server_exited) | CUDA error: out of memory at gr.cu:123 | stack" + + def test_the_engines_words_stay_out_of_the_sentence(self): + err = updater.TestStartFailed("0.1.48", "it did not answer a one-token probe (server_exited)") + text = str(err) + self.assertIn("did not start correctly on this computer", text) + self.assertIn("so it was not installed", text) + self.assertIn("Your current engine is unchanged", text) + self.assertIn("The engine started but stopped before it answered a test question.", text) + self.assertIn("`strata update --force`", text) + self.assertNotIn("server_exited", text) + self.assertNotIn("probe", text) + self.assertEqual(err.reason, "it did not answer a one-token probe (server_exited)") # kept for the state file and the JSON + self.assertEqual(err.exit_code, 6) + + def test_a_busy_card_says_so_and_asks_for_a_plain_retry(self): + err = updater.TestStartFailed("0.1.48", "CUDA error: out of memory") + self.assertTrue(err.busy) + self.assertIn("graphics card or memory looks busy", str(err)) + self.assertNotIn("CUDA", str(err)) + self.assertNotIn("--force", str(err)) + + def test_an_engine_that_would_not_run_and_a_missing_model(self): + self.assertIn("The engine program did not run on this computer.", str(updater.TestStartFailed("0.1.48", "--version exited 3: dll missing"))) + self.assertNotIn("dll missing", str(updater.TestStartFailed("0.1.48", "--version exited 3: dll missing"))) + self.assertIn("no installed model", str(updater.TestStartFailed("0.1.48", "no installed model to test with")).lower()) + + def test_control_is_called_control(self): + text = str(updater.TestStartFailed("Control 0.1.48", "--version said 'oops' (exit 7)")) + self.assertTrue(text.startswith("The new Control 0.1.48 did not start correctly"), text) + self.assertIn("Your current Control is unchanged.", text) + self.assertNotIn("engine", text) + + +class RollbackText(test_updater.UpdaterCase): + def test_from_names_the_version_that_is_left(self): + self.install_current() + self.publish(("0.1.46", fx.GOOD_ENGINE)) + updater.run_update(self.ctx(), control=False) + out = updater.rollback_engine(self.ctx()) + self.assertEqual((out["from"], out["to"]), ("0.1.46", "0.1.45")) + self.assertNotEqual(out["from"], out["to"]) + + def test_a_second_rollback_says_there_is_nothing_older_instead_of_the_same_version_twice(self): + self.install_current() + self.publish(("0.1.46", fx.GOOD_ENGINE)) + updater.run_update(self.ctx(), control=False) + updater.rollback_engine(self.ctx()) + state = self.components / ".updates" / "state.json" + data = json.loads(state.read_text(encoding="utf-8")) + data["engine"]["previous"] = data["engine"]["pinned"] # the stale state a second run can meet + state.write_text(json.dumps(data), encoding="utf-8") + with self.assertRaisesRegex(updater.UpdateError, "already the previous one .0.1.45.*strata update --force"): + updater.rollback_engine(self.ctx()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/terminal/test_updater.py b/tests/terminal/test_updater.py index 803bdba0..153bdc4f 100644 --- a/tests/terminal/test_updater.py +++ b/tests/terminal/test_updater.py @@ -218,8 +218,10 @@ class EngineUpdate(UpdaterCase): out = updater.run_update(self.ctx(), control=False) result = out["results"][0] self.assertEqual((out["exit_code"], result["status"]), (6, "failed"), out) - self.assertIn("did not pass its test start", result["error"]) + self.assertIn("did not start correctly on this computer", result["error"]) self.assertIn("unchanged", result["error"]) + self.assertNotIn("server_exited", result["error"], "the engine's own words stay out of the sentence") + self.assertIn("strata update --force", result["error"]) self.assertEqual(self.resolved(), "0.1.45") self.assertEqual(self.engine_folders(), [f"0.1.45-{PLATFORM}-cuda-cuda13"], "the failed engine was never put in the live folder") self.assertEqual(sorted(p.name for p in (self.components / ".s").iterdir()), []) @@ -478,7 +480,7 @@ class LazyModel(UpdaterCase): self.assertEqual(verify.call_args.kwargs["load"], True) def test_an_engine_that_cannot_answer_does_not_pass(self): - with self.assertRaisesRegex(updater.TestStartFailed, "one-token probe"): + with self.assertRaisesRegex(updater.TestStartFailed, "before it answered a test question"): self.run_default({"ok": True, "state": "ready", "generation_ready": False}, {"ok": False, "state": "failed", "error": "probe_generation_no_tokens"}) def expired(self, phase, words, busy): # #3683: the engine log always mentions VRAM, so a wait that expired used to be blamed on a busy graphics card