"""serve/test_server.py - the max tokens budget over both APIs, against the mock engine (no GPU, no pack). python -m unittest serve.test_server -v """ from __future__ import annotations import contextlib import io import json import os import socket import sys import tempfile import threading import time import unittest import urllib.error import urllib.request from pathlib import Path from unittest import mock sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from serve.frontend import ChatTemplate # noqa: E402 from serve.server import (CTX_SLACK, ByteTokenizer, EngineDied, GpuBusy, MockEngine, Service, StrataEngine, # noqa: E402 engine_args, layer_split_value, prompt_tokens_seen, request_timings, serve, start_failure_hint) from types import SimpleNamespace # noqa: E402 ROOT = Path(__file__).resolve().parents[1] CTX = 4096 ANSWER = "xy" * 1000 # longer than the old 1024 fallback: one token per byte (#606: not # one token repeated, which the server ends at 256) class RecordingEngine(MockEngine): def generate(self, ids, max_new, sampling, cancel, embeddings=None): self.last_max_new = max_new yield from super().generate(ids, max_new, sampling, cancel, embeddings) class MaxTokens(unittest.TestCase): @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.engine = RecordingEngine(tok, "\n\n" + ANSWER, max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def post(self, path, body): req = urllib.request.Request(self.base + path, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, json.loads(r.read()) except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()) def call(self, api, text="hi", **budget): """-> (status, body, prompt tokens, completion tokens); `budget` is merged into the request as given.""" msgs = [{"role": "user", "content": text}] if api == "openai": s, b = self.post("/v1/chat/completions", {"model": "m", "messages": msgs, **budget}) u = b.get("usage", {}) return s, b, u.get("prompt_tokens"), u.get("completion_tokens") s, b = self.post("/v1/messages", {"model": "m", "messages": msgs, **budget}) u = b.get("usage", {}) return s, b, u.get("input_tokens"), u.get("output_tokens") def test_anthropic_thinks_only_when_asked(self): # #278: Anthropic's thinking is opt-in; "thinking", an effort or a reasoning_budget_tokens (#123) asks for it from serve.frontend import anthropic_to_messages msgs = [{"role": "user", "content": "u"}] kw = lambda **r: anthropic_to_messages({"messages": msgs, **r}, think_unasked=False)[2] # noqa: E731 # the default ("anthropic_thinking": "model") renders an unasked request as 0.1.31 did self.assertNotIn("enable_thinking", anthropic_to_messages({"messages": msgs})[2]) self.assertEqual(kw(), {"enable_thinking": False}) self.assertEqual(kw(thinking={"type": "disabled"}), {"enable_thinking": False}) self.assertNotIn("enable_thinking", kw(thinking={"type": "enabled", "budget_tokens": 2048})) self.assertNotIn("enable_thinking", kw(output_config={"effort": "high"})) self.assertNotIn("enable_thinking", kw(reasoning_budget_tokens=30)) def test_count_tokens_is_the_prompt_messages_reads(self): # /v1/messages/count_tokens renders and tokenizes the same prompt /v1/messages would read, without running it msgs = [{"role": "user", "content": "how many tokens is this?"}] s, b = self.post("/v1/messages/count_tokens", {"model": "m", "messages": msgs}) self.assertEqual(s, 200) s2, _, n_in, _ = self.call("anthropic", "how many tokens is this?", max_tokens=8) self.assertEqual(s2, 200) self.assertEqual(b["input_tokens"], n_in) def test_request_line_parses_the_engine_summary(self): line = ("strata serve: prompt 1200 tokens = 1000 reused + 200 read in 50 ms (4000.0 tok/s), 30 generated in " "300 ms (100.0 tok/s), drafts accepted 20 of 28, 2 checkpoints") from serve.server import ENGINE_REQUEST m = ENGINE_REQUEST.search(line) self.assertIsNotNone(m) self.assertEqual((m["prompt"], m["reused"], m["gen"], m["tg"]), ("1200", "1000", "30", "100.0")) # #471: a request cancelled while its prompt was read says how far it got m = ENGINE_REQUEST.search("strata serve: prompt 98179 tokens = 0 reused + 12288 of 98179 read in 17565 ms " "(699.6 tok/s), 0 generated in 0 ms (0.0 tok/s), drafts accepted 0 of 0, " "0 checkpoints (cancelled)") self.assertIsNotNone(m) self.assertEqual((m["prompt"], m["reused"], m["read"], m["pp"], m["gen"]), ("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}]} for api, budgets in cases.items(): for budget in budgets: with self.subTest(api=api, budget=budget): s, b, pt, ct = self.call(api, **budget) self.assertEqual(s, 200, b) 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_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}), ("anthropic", {"max_tokens": 50}), ("openai", {"max_tokens": 1500}), ("anthropic", {"max_tokens": 1500})]: with self.subTest(api=api, budget=budget): want = budget.get("max_completion_tokens") or budget["max_tokens"] s, b, _, ct = self.call(api, **budget) self.assertEqual(s, 200, b) self.assertEqual(self.engine.last_max_new, want) self.assertEqual(ct, want) def test_explicit_budget_over_the_context_is_rejected(self): for api in ("openai", "anthropic"): with self.subTest(api=api): s, b, _, _ = self.call(api, max_tokens=CTX) self.assertEqual(s, 400) self.assertIn("exceeds the context", b["error"]["message"]) self.assertIn("\"fit_max_tokens\": true", b["error"]["message"]) # #545: says how to get past it self.assertRegex(b["error"]["message"], r"at most \d+ here") def test_unset_budget_with_a_near_full_prompt(self): _, _, pt0, _ = self.call("openai", max_tokens=1) overhead = pt0 - len("hi") # the template's tokens around the user text for api in ("openai", "anthropic"): _, _, pa, _ = self.call(api, max_tokens=1) 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) self.assertEqual(s, 200, b) self.assertEqual(self.engine.last_max_new, 5) self.assertEqual(ct, 5) with self.subTest(api=api, room=0): # nothing left: rejected, not truncated text = "y" * (CTX - CTX_SLACK - overhead - over) s, b, _, _ = self.call(api, text=text) self.assertEqual(s, 400, b) self.assertIn("no room to answer", b["error"]["message"]) def test_debug_log_shows_the_resolved_budget(self): import contextlib import io os.environ["STRATA_DEBUG"] = "1" try: for api in ("openai", "anthropic"): with self.subTest(api=api): out = io.StringIO() with contextlib.redirect_stdout(out): _, _, pt, _ = self.call(api, max_tokens=-1) self.assertIn(f"max_new={CTX - CTX_SLACK - pt} ", out.getvalue()) finally: del os.environ["STRATA_DEBUG"] class FitMaxTokens(unittest.TestCase): """PR #24: --fit-max-tokens clamps an explicit budget that overshoots the context instead of a 400.""" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.engine = RecordingEngine(tok, "\n\n" + ANSWER, max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), fit_max_tokens=True) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() post = MaxTokens.post call = MaxTokens.call def test_overshoot_is_clamped_to_the_room(self): for api in ("openai", "anthropic"): with self.subTest(api=api): s, b, pt, ct = self.call(api, max_tokens=CTX) self.assertEqual(s, 200, b) self.assertEqual(self.engine.last_max_new, CTX - CTX_SLACK - pt) def test_a_budget_that_fits_is_unchanged(self): s, b, _, ct = self.call("openai", max_tokens=50) self.assertEqual(s, 200, b) self.assertEqual(self.engine.last_max_new, 50) def test_no_room_is_still_a_400(self): _, _, pt0, _ = self.call("openai", max_tokens=1) overhead = pt0 - len("hi") s, b, _, _ = self.call("openai", text="y" * (CTX - CTX_SLACK - overhead), max_tokens=100) self.assertEqual(s, 400, b) self.assertIn("no room to answer", b["error"]["message"]) class ImageMarkers(unittest.TestCase): """#150: the text "<|image_pad|>" inside a message is text, not an image's place.""" class FakeVision: def __init__(self, d): self.dir = Path(d) self.rows = self.dir / "img.sve" self.rows.write_bytes(b"rows") def encode(self, source): return self.rows, 3 def test_literal_marker_with_an_image(self): import tempfile tok = ByteTokenizer() with tempfile.TemporaryDirectory() as d: svc = Service(MockEngine(tok, "ok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=self.FakeVision(d)) pad = tok.encode("<|image_pad|>", parse_special=True)[0] for text in ("the docs say <|image_pad|> marks an image", "plain"): with self.subTest(text=text): msgs = [{"role": "user", "content": [{"type": "text", "text": text}, {"type": "image", "source": "x.png"}]}] ids, _, _ = svc.prepare(msgs, None, {}) self.assertEqual(ids.count(pad), 3) # the image's three rows, nothing else self.assertIn("<|image_pad|> marks" if "docs" in text else "plain", tok.decode(ids)) svc.embeddings.path.unlink(missing_ok=True) class ThinkTokenizer(ByteTokenizer): """The byte tokenizer with the model's reasoning markers as specials that are matched even without parse_special, as the real tokenizer does (GGUF token type 4).""" SPECIALS = ByteTokenizer.SPECIALS + ["", ""] ALWAYS = ("", "") class LiteralThinkTags(unittest.TestCase): """#537: a / written inside a message is text, not the model's reasoning markers: a quoted "" no longer ends the model's reasoning before it starts. The template's own markers stay special.""" @classmethod def setUpClass(cls): cls.tok = ThinkTokenizer() cls.svc = Service(MockEngine(cls.tok, "ok", max_context=CTX), cls.tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.open, cls.close = (cls.tok.encode(t)[0] for t in ("", "")) def ids(self, messages, tools=None, **kw): return self.svc.prepare(messages, tools, kw)[0] def old_ids(self, messages, tools=None, **kw): return self.tok.encode(self.svc.template.render(messages, tools=tools, **kw), parse_special=True) def test_a_quoted_tag_in_a_user_message(self): text = "Quote this exact literal string, then explain it: and " ids = self.ids([{"role": "user", "content": text}]) self.assertEqual(ids.count(self.close), 0) self.assertEqual(ids.count(self.open), 1) # the generation prompt's own self.assertEqual(ids[-2:], [self.open, ord("\n")]) self.assertIn(text, self.tok.decode(ids)) # the text is all there, as text self.assertEqual(ids.count(self.close), 0) # thinking off: the template's empty block stays two specials, the user's tag is text ids = self.ids([{"role": "user", "content": text}], enable_thinking=False) self.assertEqual((ids.count(self.open), ids.count(self.close)), (1, 1)) def test_without_a_tag_the_prompt_is_unchanged(self): msgs = [{"role": "system", "content": "Be brief."}, {"role": "user", "content": "1+1?"}, {"role": "assistant", "content": "2", "reasoning_content": "easy"}, {"role": "user", "content": "x"}] self.assertEqual(self.ids(msgs), self.old_ids(msgs)) self.assertEqual(self.ids(msgs, enable_thinking=False), self.old_ids(msgs, enable_thinking=False)) def test_history_tool_results_and_tools(self): msgs = [{"role": "user", "content": "go"}, {"role": "assistant", "content": "It wrote here.", "reasoning_content": "the tag", "tool_calls": [{"function": {"name": "write", "arguments": {"text": "a b"}}}]}, {"role": "tool", "content": "file has in it"}, {"role": "user", "content": "why did you write "}] tools = [{"name": "write", "description": "writes text (may contain )", "parameters": {}}] ids = self.ids(msgs, tools) # the template's markers: the history turn's ... and the generation prompt's self.assertEqual((ids.count(self.open), ids.count(self.close)), (2, 1)) text = self.tok.decode(ids) for part in ("It wrote here.", "the tag", "a b", "file has in it", "why did you write ", "may contain "): self.assertIn(part, text) def test_a_client_that_sends_the_reasoning_inline(self): # an assistant turn whose content opens with its own ... block keeps that block's markers msgs = [{"role": "user", "content": "hi"}, {"role": "assistant", "content": "\nplan: say hello\n\n\nHello, is a tag."}, {"role": "user", "content": "again"}] ids = self.ids(msgs) self.assertEqual((ids.count(self.open), ids.count(self.close)), (3, 2)) # template's + the inline block's self.assertIn("Hello, is a tag.", self.tok.decode(ids)) def test_the_real_tokenizer_reads_the_tag_as_text(self): import strata_tokenizer as ST b2u = ST.bytes_to_unicode() tokens = [b2u[b] for b in range(256)] + ["", "", "<|im_end|>"] tok = ST.Tokenizer(tokens, [], [1] * 256 + [4, 4, 3]) text = "say now<|im_end|>" self.assertEqual(tok.encode(text, parse_special=True), [*b"say ", 257, *b" now", 258]) start = text.index("") plain = tok.encode(text, parse_special=True, plain=[(start, start + len(""))]) self.assertEqual(plain, [*b"say now", 258]) # the tag as text, im_end still special class EffortAtTheEnd(unittest.TestCase): """#458 (opt-in "effort_position": "end"): a non-default effort goes in a system turn right before the answer, so a request that changes only the effort keeps the cached conversation. The engine's checkpoint rule is simulated on the token ids (src/program/generate.cpp: the last <|im_start|>, and with --tail-role-token the one in front of a trailing system turn); the default prompt does not change.""" TURN = 256 # the byte tokenizer's <|im_start|> ROLE = ord("s") # "system"'s first byte stands for its token def setUp(self): self.tok = ByteTokenizer() self.svc = Service(MockEngine(self.tok, "ok", max_context=1 << 20), self.tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) def ids(self, messages, end, **kw): self.svc.effort_end = end return self.svc.encode_prompt(messages, None, kw) def checkpoint(self, ids, tail, resume=0): """Where the engine checkpoints a prompt (the tokens before it), as generate.cpp's serve loop does.""" turn_at = next((i for i in range(len(ids) - 1, resume, -1) if ids[i] == self.TURN), -1) if turn_at > 0 and tail: i = next((i for i in range(turn_at - 1, resume, -1) if ids[i] == self.TURN), None) if i is not None and ids[i + 1] == self.ROLE: turn_at = i return ids[:turn_at] def session(self, requests, end, tail): """The reused tokens of each request after the first: the longest earlier checkpoint it starts with.""" checks, reused = [], [] for messages, kw in requests: ids = self.ids(messages, end, **kw) reused.append(max([len(c) for c in checks if ids[:len(c)] == c], default=0)) checks.append(self.checkpoint(ids, tail)) return reused[1:] CHAT = [{"role": "system", "content": "You are a careful assistant. " * 40}, {"role": "user", "content": "Explain the conversation cache. " * 20}] REPLY = {"role": "assistant", "content": "It keeps the prompt's state. " * 20, "reasoning_content": "ok"} NEXT = {"role": "user", "content": "And the checkpoints?"} def test_the_default_prompt_is_unchanged(self): for kw in ({}, {"reasoning_effort": "xhigh"}): self.assertEqual(self.ids(self.CHAT, True, **kw), self.ids(self.CHAT, False, **kw)) self.assertEqual(self.ids(self.CHAT, True, **kw), self.tok.encode(self.svc.template.render(self.CHAT, **kw), parse_special=True)) for kw in ({"reasoning_effort": "low"}, {"reasoning_effort": "medium"}, {"enable_thinking": False}): self.assertEqual(self.ids(self.CHAT, False, **kw), # off: every effort renders as before self.tok.encode(self.svc.template.render(self.CHAT, **kw), parse_special=True)) def test_the_trailing_turn(self): text = self.tok.decode(self.ids(self.CHAT, True, reasoning_effort="low")) self.assertTrue(text.endswith("<|im_end|>\n<|im_start|>system\nReasoning effort is set to low. Keep your " "thinking brief and focused, moving directly to the conclusion without " "unnecessary elaboration.<|im_end|>\n<|im_start|>assistant\n\n"), text[-300:]) self.assertIn("Reasoning effort is set to xhigh", text) # the top stays the default's text = self.tok.decode(self.ids(self.CHAT, True, enable_thinking=False)) self.assertTrue(text.endswith("<|im_start|>assistant\n\n\n\n\n")) self.assertNotIn("<|im_start|>system\nReasoning effort", text[-200:]) default = self.ids(self.CHAT, True) for kw in ({"reasoning_effort": "low"}, {"reasoning_effort": "medium"}, {"enable_thinking": False}): ids = self.ids(self.CHAT, True, **kw) head = len(self.checkpoint(default, False)) # all before the answer's turn self.assertEqual(ids[:head], default[:head], kw) # the same prompt up to the answer def test_changing_the_effort_keeps_the_conversation(self): efforts = [{}, {"reasoning_effort": "low"}, {"enable_thinking": False}, {"reasoning_effort": "medium"}, {}] history = len(self.checkpoint(self.ids(self.CHAT, False), False)) # at the top (the default): another effort differs a few tokens in, and the whole prompt is read again self.assertEqual(self.session([(self.CHAT, kw) for kw in efforts], False, False)[:2], [0, 0]) # at the end, with the engine's rule: every request reuses the whole conversation self.assertEqual(self.session([(self.CHAT, kw) for kw in efforts], True, True), [history] * 4) def test_the_next_turn_reuses_the_checkpoint(self): turn1, turn2 = self.CHAT, self.CHAT + [self.REPLY, self.NEXT] low = {"reasoning_effort": "low"} first = len(self.checkpoint(self.ids(turn1, True, **low), True)) # the engine's rule: the next turn (the same effort) reuses the first turn's whole conversation self.assertEqual(self.session([(turn1, low), (turn2, low)], True, True), [first]) # without it the checkpoint held the effort turn and the next turn reused nothing (what #458 measured) self.assertEqual(self.session([(turn1, low), (turn2, low)], True, False), [0]) def test_the_config(self): from serve.server import effort_end_args class Tok: def encode(self, text, parse_special=False): return [8678] if text == "system" else [1, 2] with tempfile.TemporaryDirectory() as d: new, old = Path(d) / "new.exe", Path(d) / "old.exe" new.write_bytes(b"... --tail-role-token ID --serve: ...") old.write_bytes(b"... --turn-token ID ...") out = io.StringIO() with contextlib.redirect_stdout(out): self.assertIsNone(effort_end_args({}, str(new), Tok())) self.assertIsNone(effort_end_args({"effort_position": "start"}, str(new), Tok())) self.assertEqual(effort_end_args({"effort_position": "end"}, str(new), Tok()), ["--tail-role-token", "8678"]) self.assertIsNone(effort_end_args({"effort_position": "end"}, str(old), Tok())) self.assertIsNone(effort_end_args({"effort_position": "end"}, str(new), ByteTokenizer())) self.assertIn("needs engine 0.1.39 or newer", out.getvalue()) with self.assertRaises(ValueError): effort_end_args({"effort_position": "middle"}, str(new), Tok()) class StatusNeedsTheKey(unittest.TestCase): """#212: /status shows the end of the answer being written, so it needs the key like /v1/*.""" def test_status(self): tok = ByteTokenizer() svc = Service(MockEngine(tok, "ok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) svc.api_key = "k3y" httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}/status" try: with self.assertRaises(urllib.error.HTTPError) as e: urllib.request.urlopen(base, timeout=10) self.assertEqual(e.exception.code, 401) e.exception.close() req = urllib.request.Request(base, headers={"Authorization": "Bearer k3y"}) with urllib.request.urlopen(req, timeout=10) as r: self.assertEqual(r.status, 200) self.assertNotIn("tail", json.loads(r.read())) finally: httpd.shutdown() httpd.server_close() class ToolCallTerminators(unittest.TestCase): """#210: a value that contains or (a file documenting the call format) is kept whole.""" CONTENT = ("Close each value with and the call with .\n" "\nnot a parameter\n\nend") SCHEMA = [{"name": "write", "parameters": {"properties": {"path": {"type": "string"}, "content": {"type": "string"}}}}] def run_parser(self, stream_tools, step): from serve.frontend import OutputParser text = ("\n\n\n\n\ndoc.md\n\n" f"\n{self.CONTENT}\n\n\n") p = OutputParser(thinking=True, tools=self.SCHEMA, stream_tools=stream_tools) evs = [] for i in range(0, len(text), step): evs += p.feed(text[i:i + step]) evs += p.finish() return evs def test_values_keep_the_terminators(self): for stream_tools in (False, True): for step in (1, 7, 10_000): with self.subTest(stream_tools=stream_tools, step=step): evs = self.run_parser(stream_tools, step) calls = [e.call for e in evs if e.kind == "tool_call"] self.assertEqual(len(calls), 1) self.assertEqual(calls[0].arguments, {"path": "doc.md", "content": self.CONTENT}) self.assertFalse([e for e in evs if e.kind == "content" and e.text.strip()]) if stream_tools: streamed = "".join(e.text for e in evs if e.kind == "tool_args") self.assertEqual(json.loads(streamed), {"path": "doc.md", "content": self.CONTENT}) class UnfinishedToolCall(unittest.TestCase): """#211: a call the output ends inside is not reported as a whole one - its streamed JSON is not closed and the finish reason is not "tool_calls" / "tool_use" - so a client can tell it from a call to run.""" CALL = ("\n\n\n\n\nnotes.txt\n\n" "\n") CUT = CALL + "first half of the fi" # the model's turn ends here WHOLE = CALL + "all of it\n\n\n" PROPS = {"path": {"type": "string"}, "content": {"type": "string"}} def test_parser(self): from serve.frontend import OutputParser schema = [{"name": "write", "parameters": {"properties": self.PROPS}}] for text, content in ((self.CUT, None), (self.WHOLE, "all of it"), (self.WHOLE[:-len("")], "all of it")): # only missing: whole for step in (1, 7, 10_000): with self.subTest(end=text[-12:], step=step): p = OutputParser(thinking=True, tools=schema, stream_tools=True) evs = [] for i in range(0, len(text), step): evs += p.feed(text[i:i + step]) evs += p.finish() streamed = "".join(e.text for e in evs if e.kind == "tool_args") calls = [e for e in evs if e.kind == "tool_call"] if content is None: self.assertEqual((calls, streamed), ([], '{"path":"notes.txt","content":"first half of the fi')) else: self.assertEqual(len(calls), 1) self.assertEqual(json.loads(streamed), {"path": "notes.txt", "content": content}) def answers(self, script, max_tokens=500): """(finish reason, the call's arguments) from OpenAI and Anthropic, whole and streamed, for the model's `script`.""" tok = ByteTokenizer() svc = Service(MockEngine(tok, script, max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" tools = {"openai": [{"type": "function", "function": {"name": "write", "parameters": { "type": "object", "properties": self.PROPS}}}], "anthropic": [{"name": "write", "input_schema": {"type": "object", "properties": self.PROPS}}]} out = {} try: for api, path in (("openai", "/v1/chat/completions"), ("anthropic", "/v1/messages")): for stream in (False, True): body = {"model": "x", "max_tokens": max_tokens, "stream": stream, "tools": tools[api], "messages": [{"role": "user", "content": "save my notes"}]} req = urllib.request.Request(base + path, data=json.dumps(body).encode(), headers={ "Content-Type": "application/json", "anthropic-version": "2023-06-01"}) with urllib.request.urlopen(req, timeout=30) as r: raw = r.read().decode() if stream: evs = [json.loads(line[6:]) for line in raw.splitlines() if line.startswith("data: {")] if api == "openai": out[api, stream] = (evs[-1]["choices"][0]["finish_reason"], "".join( (tc.get("function") or {}).get("arguments") or "" for e in evs for tc in e["choices"][0]["delta"].get("tool_calls") or [])) else: out[api, stream] = (evs[-2]["delta"]["stop_reason"], "".join( e["delta"]["partial_json"] for e in evs if e["type"] == "content_block_delta" and e["delta"]["type"] == "input_json_delta")) elif api == "openai": c = json.loads(raw)["choices"][0] out[api, stream] = (c["finish_reason"], [tc["function"]["arguments"] for tc in c["message"].get("tool_calls") or []]) else: m = json.loads(raw) out[api, stream] = (m["stop_reason"], [b["input"] for b in m["content"] if b["type"] == "tool_use"]) finally: httpd.shutdown() httpd.server_close() return out def test_a_cut_call(self): cut = '{"path":"notes.txt","content":"first half of the fi' self.assertEqual(self.answers(self.CUT), { ("openai", False): ("stop", []), ("openai", True): ("stop", cut), # whole answers leave the cut ("anthropic", False): ("end_turn", []), # call out: it has no arguments to give ("anthropic", True): ("end_turn", cut)}) def test_a_call_cut_at_the_token_limit(self): """The same cut by max_tokens: "length" / "max_tokens", and the whole (non-streamed) answers leave the call out in both APIs.""" a = self.answers(self.CUT + "rest of the file, never reached" * 40, max_tokens=len(self.CUT)) self.assertEqual((a["openai", False], a["anthropic", False]), (("length", []), ("max_tokens", []))) self.assertEqual((a["openai", True][0], a["anthropic", True][0]), ("length", "max_tokens")) def test_collect_keeps_calls_whose_arguments_parse(self): from serve.server import openai_collect def chunk(delta, finish=None): return {"id": "c", "created": 1, "model": "m", "usage": {}, "choices": [{"index": 0, "delta": delta, "finish_reason": finish}]} whole = {"index": 0, "id": "a", "type": "function", "function": {"name": "f", "arguments": '{"x": 1}'}} cut = {"index": 1, "id": "b", "type": "function", "function": {"name": "g", "arguments": '{"y": "ha'}} for finish, want in (("stop", ["a"]), ("length", ["a"]), ("tool_calls", ["a", "b"])): with self.subTest(finish=finish): msg = openai_collect([chunk({"tool_calls": [whole]}), chunk({"tool_calls": [cut]}), chunk({}, finish)])["choices"][0]["message"] self.assertEqual([c["id"] for c in msg["tool_calls"]], want) msg = openai_collect([chunk({"tool_calls": [cut]}), chunk({}, "stop")])["choices"][0]["message"] self.assertNotIn("tool_calls", msg) def test_a_whole_call(self): whole = {"path": "notes.txt", "content": "all of it"} a = self.answers(self.WHOLE) self.assertEqual((a["openai", False][0], [json.loads(x) for x in a["openai", False][1]]), ("tool_calls", [whole])) self.assertEqual((a["openai", True][0], json.loads(a["openai", True][1])), ("tool_calls", whole)) self.assertEqual(a["anthropic", False], ("tool_use", [whole])) self.assertEqual((a["anthropic", True][0], json.loads(a["anthropic", True][1])), ("tool_use", whole)) class ClientShapes(unittest.TestCase): """What real clients send: Claude Code posts /v1/messages?beta=true (issue #55) and puts hook context into the conversation as a mid-conversation system message (issue #56); some OpenAI clients send a late developer message.""" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.engine = RecordingPrompt(tok, "\n\n2", max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def post(self, path, body): req = urllib.request.Request(self.base + path, data=json.dumps(body).encode(), headers={"Content-Type": "application/json", "anthropic-version": "2023-06-01"}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, json.loads(r.read()) except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()) def prompt_text(self): return bytes(i for i in self.engine.last_ids if i < 256).decode("utf-8", "replace") def test_query_string(self): body = {"model": "x", "max_tokens": 20, "messages": [{"role": "user", "content": "hi"}]} for path in ("/v1/messages?beta=true", "/v1/chat/completions?api-version=1", "/v1/messages/?beta=true"): status, b = self.post(path, body) self.assertEqual(status, 200, (path, b)) status, _ = self.post("/v1/nothing?beta=true", body) self.assertEqual(status, 404) def test_anthropic_mid_conversation_system(self): status, b = self.post("/v1/messages?beta=true", { "model": "x", "max_tokens": 50, "system": [{"type": "text", "text": "You are terse."}], "messages": [ {"role": "user", "content": [{"type": "text", "text": "1+1? digits only"}]}, {"role": "system", "content": [{"type": "text", "text": "answer in digits"}]}]}) self.assertEqual(status, 200, b) text = self.prompt_text() self.assertIn("You are terse.", text) self.assertIn("answer in digits", text) self.assertLess(text.index("You are terse."), text.index("1+1?")) # the first system stays first self.assertLess(text.index("1+1?"), text.index("answer in digits")) # the late one stays in place def test_openai_late_developer_and_system(self): status, b = self.post("/v1/chat/completions", { "model": "x", "max_tokens": 50, "messages": [{"role": "system", "content": "Be brief."}, {"role": "user", "content": "hello"}, {"role": "assistant", "content": "hi"}, {"role": "developer", "content": "Now use digits."}, {"role": "system", "content": "Also this."}, {"role": "user", "content": "1+1?"}]}) self.assertEqual(status, 200, b) text = self.prompt_text() for part in ("Be brief.", "Now use digits.", "Also this.", "1+1?"): self.assertIn(part, text) def test_no_user_turn_is_a_400(self): # #365: the template's own refusal (Qwen's "No user query found in messages." when no turn is a user's query) # answers 400, not a dropped connection with tempfile.TemporaryDirectory() as d: tpl = Path(d) / "chat_template.jinja" tpl.write_text("{% if messages[-1].role != 'user' %}{{ raise_exception('No user query found in messages.') }}" "{% endif %}{{ messages[-1].content }}", encoding="utf-8") template, self.svc.template = self.svc.template, ChatTemplate(tpl) try: status, b = self.post("/v1/chat/completions", { "model": "x", "max_tokens": 20, "messages": [{"role": "system", "content": "Only a system."}]}) self.assertEqual(status, 400, b) self.assertIn("No user query found", b["error"]["message"]) finally: self.svc.template = template status, b = self.post("/v1/chat/completions", {"model": "x", "max_tokens": 20, "messages": [{"role": "user", "content": "hi"}]}) self.assertEqual(status, 200, b) # the server goes on def test_messages_as_a_json_string_over_http(self): # #460: a double-encoded "messages" is answered; one that is not a list of objects is a 400, not a 500 encoded = json.dumps([{"role": "user", "content": "1+1?"}]) for path in ("/v1/chat/completions", "/v1/messages"): with self.subTest(path=path): status, b = self.post(path, {"model": "x", "max_tokens": 20, "messages": encoded}) self.assertEqual(status, 200, b) self.assertIn("1+1?", self.prompt_text()) status, b = self.post(path, {"model": "x", "max_tokens": 20, "messages": ["hi"]}) self.assertEqual(status, 400, b) self.assertIn("messages must be a list of objects", b["error"]["message"]) def test_malformed_tools_are_a_400(self): # #592: a "tools" value that is not a list of named tool objects is a 400 naming the field, on both APIs, # not a dropped connection (an AttributeError/TypeError in the request thread) msgs = [{"role": "user", "content": "hi"}] bad = ("auto", ["get_weather"], [{"description": "no name"}], {"name": "x"}, [{"type": "function", "function": "get_weather"}], [{"type": "function", "function": {"name": ""}}]) for path in ("/v1/chat/completions", "/v1/messages"): for tools in bad: with self.subTest(path=path, tools=tools): status, b = self.post(path, {"model": "x", "max_tokens": 8, "messages": msgs, "tools": tools}) self.assertEqual(status, 400, b) self.assertIn("tools", b["error"]["message"]) status, b = self.post("/v1/chat/completions", {"model": "x", "max_tokens": 8, "messages": msgs}) self.assertEqual(status, 200, b) # the server goes on def test_well_formed_tools_still_work(self): msgs = [{"role": "user", "content": "hi"}] fn = {"name": "get_weather", "description": "the weather", "parameters": {"type": "object", "properties": {}}} for path, tools in (("/v1/chat/completions", [{"type": "function", "function": fn}]), ("/v1/chat/completions", [fn]), # the bare shape some clients send ("/v1/chat/completions", json.dumps([{"type": "function", "function": fn}])), ("/v1/chat/completions", []), ("/v1/chat/completions", None), ("/v1/messages", [{"name": "get_weather", "input_schema": {"type": "object"}}]), ("/v1/messages", [])): with self.subTest(path=path, tools=tools): status, b = self.post(path, {"model": "x", "max_tokens": 8, "messages": msgs, "tools": tools}) self.assertEqual(status, 200, b) if tools: self.assertIn("get_weather", self.prompt_text()) def test_vision_temp_image_removed_when_the_pipe_fails(self): # #352: the temporary image goes even when the encoder's pipe raises from serve.server import Vision class Gone: def write(self, _): raise BrokenPipeError("the encoder is gone") v = Vision.__new__(Vision) v.dir, v.lock, v.cache = Path(tempfile.mkdtemp(prefix="strata-vision-test-")), threading.Lock(), {} v.proc = mock.Mock(stdin=Gone()) with mock.patch.object(Vision, "load", return_value=b""), mock.patch.object(Vision, "normalize", return_value=b"png"): with self.assertRaises(BrokenPipeError): v.encode("x") self.assertEqual(list(v.dir.iterdir()), []) v.dir.rmdir() def test_leading_system_unchanged(self): from serve.frontend import anthropic_to_messages, openai_to_messages msgs, _, _ = openai_to_messages({"messages": [{"role": "developer", "content": "D"}, {"role": "user", "content": "u"}]}) self.assertEqual([m["role"] for m in msgs], ["system", "user"]) msgs, _, _ = anthropic_to_messages({"system": "S", "messages": [{"role": "user", "content": "u"}]}) self.assertEqual([m["role"] for m in msgs], ["system", "user"]) def test_messages_sent_as_a_json_string(self): # #460: a client that double-encodes "messages" (and "tool_calls") as a JSON string gets them decoded; what is # still not a list of objects is a ValueError (the server's 400), not an AttributeError on m.get from serve.frontend import anthropic_to_messages, openai_to_messages call = [{"id": "c1", "type": "function", "function": {"name": "f", "arguments": "{\"x\": 1}"}}] listed = [{"role": "user", "content": "u"}, {"role": "assistant", "content": "", "tool_calls": call}] encoded = [listed[0], dict(listed[1], tool_calls=json.dumps(call))] want = openai_to_messages({"messages": listed})[0] self.assertEqual(want[1]["tool_calls"], [{"function": {"name": "f", "arguments": {"x": 1}}}]) self.assertEqual(openai_to_messages({"messages": json.dumps(listed)})[0], want) self.assertEqual(openai_to_messages({"messages": json.dumps(encoded)})[0], want) anth = [{"role": "user", "content": "u"}] self.assertEqual(anthropic_to_messages({"messages": json.dumps(anth)})[0], anthropic_to_messages({"messages": anth})[0]) self.assertEqual(openai_to_messages({})[0], []) # no field: nothing, as before self.assertEqual(openai_to_messages({"messages": None})[0], []) for bad in ("not json", "\"a string\"", json.dumps({"role": "user"}), ["hi"], [{"role": "user"}, 3], 5, {"role": "user", "content": "u"}): for fn in (openai_to_messages, anthropic_to_messages): with self.subTest(bad=bad, fn=fn.__name__): with self.assertRaisesRegex(ValueError, "messages must be a list of objects"): fn({"messages": bad}) for calls in (["f"], "[1]", [{"function": "f"}], "{"): with self.subTest(calls=calls), self.assertRaisesRegex(ValueError, "tool_calls must be a list of objects"): openai_to_messages({"messages": [{"role": "assistant", "content": "", "tool_calls": calls}]}) def test_tool_call_malformed_arguments_fallback(self): from serve.frontend import openai_to_messages bad_call = [{"id": "c1", "type": "function", "function": {"name": "f", "arguments": "{malformed_json"}}] msgs, tools, kwargs = openai_to_messages({"messages": [{"role": "user", "content": "u"}, {"role": "assistant", "content": "", "tool_calls": bad_call}]}) self.assertEqual(msgs[1]["tool_calls"], [{"function": {"name": "f", "arguments": {"arguments": "{malformed_json"}}}]) tpl = ChatTemplate(ROOT / "serve/chat_template.jinja") rendered = tpl.render(msgs, tools=tools, **kwargs) self.assertIn("", rendered) self.assertIn("\n{malformed_json", rendered) class ToolArguments(unittest.TestCase): """#510: one helper for both frontends; a history whose arguments are not a JSON object is kept, not a 500.""" def test_helper(self): from serve.frontend import tool_arguments for raw, want in ((None, {}), ("", {}), (" ", {}), ({"a": 1}, {"a": 1}), ('{"a": 1}', {"a": 1}), ("{cut", {"arguments": "{cut"}), ("[1, 2]", {"arguments": "[1, 2]"}), ("5", {"arguments": "5"}), ([1, 2], {"arguments": "[1, 2]"}), (7, {"arguments": "7"})): with self.subTest(raw=raw): self.assertEqual(tool_arguments(raw), want) def test_both_frontends_render_it(self): from serve.frontend import anthropic_to_messages, openai_to_messages for bad in ("{cut", "[1]", "3"): with self.subTest(bad=bad): m = openai_to_messages({"messages": [{"role": "user", "content": "u"}, {"role": "assistant", "content": "", "tool_calls": [{"id": "c", "type": "function", "function": {"name": "f", "arguments": bad}}]}]})[0] self.assertEqual(m[1]["tool_calls"][0]["function"]["arguments"], {"arguments": bad}) for bad in ([1], 3, "{cut"): with self.subTest(anthropic=bad): m = anthropic_to_messages({"messages": [{"role": "user", "content": "u"}, {"role": "assistant", "content": [ {"type": "tool_use", "id": "t", "name": "f", "input": bad}]}]})[0] self.assertIn("arguments", m[1]["tool_calls"][0]["function"]["arguments"]) class SamplingKeys(unittest.TestCase): """The GEN line's sampling keys: top_k 0 ("off") or wider than the engine's 64 get the widest list, 64 (they used to fall back to the engine default 20); a penalty always carries its window.""" def keys(self, **sampling): return StrataEngine.sampling_keys(sampling).split() def test_top_k(self): self.assertIn("top_k=10", self.keys(temperature=0.7, top_k=10)) self.assertIn("top_k=64", self.keys(temperature=0.7, top_k=64)) self.assertIn("top_k=64", self.keys(temperature=0.7, top_k=0)) self.assertIn("top_k=64", self.keys(temperature=0.7, top_k=100)) for bad in (-1, True, 2.5, "20"): self.assertFalse([k for k in self.keys(temperature=0.7, top_k=bad) if k.startswith("top_k=")], bad) def test_tune_keys(self): k = self.keys(temperature=0, strata_tune={"pcie_frac": 0.2, "spec_min_p": 0.7}) self.assertIn("pcie_frac=0.2", k) self.assertIn("spec_min_p=0.7", k) bad = self.keys(strata_tune={"pcie_frac": 3, "spec_min_p": True, "pool_workers": 2}) self.assertFalse([x for x in bad if x.split("=")[0] in ("pcie_frac", "spec_min_p", "pool_workers")]) def test_penalty_window(self): self.assertIn("penalty_last_n=64", self.keys(presence_penalty=1.5)) self.assertIn("penalty_last_n=4096", self.keys(repetition_penalty=1.1, penalty_last_n=4096)) self.assertFalse([k for k in self.keys(temperature=0.7) if k.startswith("penalty")]) class GpuChoice(unittest.TestCase): """Issue #51: the config's \"gpu\" reaches the engine as CUDA_VISIBLE_DEVICES, numbered like nvidia-smi.""" def test_env(self): from serve.server import child_env env = child_env({"gpu": 1}) self.assertEqual(env["CUDA_VISIBLE_DEVICES"], "1") self.assertEqual(env["CUDA_DEVICE_ORDER"], "PCI_BUS_ID") plain = child_env({}) # no choice: the environment as it was (existing installs) self.assertEqual(plain.get("CUDA_VISIBLE_DEVICES"), os.environ.get("CUDA_VISIBLE_DEVICES")) self.assertEqual(plain.get("CUDA_DEVICE_ORDER"), os.environ.get("CUDA_DEVICE_ORDER")) def test_vision_device(self): # #408: the image encoder on its own card; the engine's environment stays as it was from serve.server import child_env, vision_env cfg = {"gpu": [0, 1], "vision": {"exe": "v", "cuda_device": 2}} env = child_env(cfg) venv = vision_env(cfg, env) self.assertEqual(venv["CUDA_VISIBLE_DEVICES"], "2") self.assertEqual(venv["CUDA_DEVICE_ORDER"], "PCI_BUS_ID") self.assertEqual(env["CUDA_VISIBLE_DEVICES"], "0,1") plain = {"gpu": [0, 1], "vision": {"exe": "v"}} self.assertIs(vision_env(plain, env), env) # no cuda_device: the engine's environment, unchanged def test_hip_ordinal(self): """#325: on Windows the HIP ordinal setup resolved wins over the config's "gpu" (an iGPU takes HIP's 0).""" from serve.server import child_env self.assertEqual(child_env({"backend": "hip", "gpu": 1})["HIP_VISIBLE_DEVICES"], "1") # Linux: KFD order self.assertEqual(child_env({"backend": "hip", "hip_ordinal": 1})["HIP_VISIBLE_DEVICES"], "1") self.assertEqual(child_env({"backend": "hip", "gpu": 0, "hip_ordinal": 1})["HIP_VISIBLE_DEVICES"], "1") self.assertEqual(child_env({"backend": "hip", "gpu": [1, 0], "hip_ordinal": 2})["HIP_VISIBLE_DEVICES"], "1,0") # a layer split keeps its list self.assertEqual(child_env({"backend": "hip", "gpu": 0, "hip_ordinal": "x"})["HIP_VISIBLE_DEVICES"], "0") plain = child_env({"backend": "hip"}) self.assertEqual(plain.get("HIP_VISIBLE_DEVICES"), os.environ.get("HIP_VISIBLE_DEVICES")) class RecordingPrompt(MockEngine): def generate(self, ids, max_new, sampling, cancel, embeddings=None): self.last_ids = list(ids) yield from super().generate(ids, max_new, sampling, cancel, embeddings) class DyingEngine(MockEngine): """Issue #27: an engine that dies after a few tokens of its first answer, and comes back when restarted.""" def __init__(self, tok, script, max_context): super().__init__(tok, script, max_context=max_context) self.dead, self.restarts, self.die_after = False, 0, 5 def alive(self): return not self.dead def restart(self): self.dead, self.die_after = False, None self.restarts += 1 def generate(self, ids, max_new, sampling, cancel, embeddings=None): for i, t in enumerate(super().generate(ids, max_new, sampling, cancel, embeddings)): if self.die_after is not None and i == self.die_after: self.dead = True raise EngineDied("the engine stopped unexpectedly (exit code -9)") yield t class SlowPromptEngine(MockEngine): """Reads a "long prompt" for up to 20 s without a token (the engine sends nothing then), stopping on cancel.""" def generate(self, ids, max_new, sampling, cancel, embeddings=None): self.cancelled_after = None t0 = time.monotonic() while time.monotonic() - t0 < 20: if cancel.is_set(): self.cancelled_after = time.monotonic() - t0 return time.sleep(0.05) yield from super().generate(ids, max_new, sampling, cancel, embeddings) class ClientHangUp(unittest.TestCase): """#430 #431: a client that hangs up during a long prompt read cancels the request within about a second - non-streamed (which writes nothing until the end) and streamed (one keep-alive per prompt chunk) alike.""" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.engine = SlowPromptEngine(tok, "\n\nOK", max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.httpd = serve(cls.svc, port=0) cls.port = cls.httpd.server_address[1] @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def hang_up(self, stream): import socket as so body = json.dumps({"model": "x", "max_tokens": 20, "stream": stream, "messages": [{"role": "user", "content": "a long prompt"}]}).encode() c = so.create_connection(("127.0.0.1", self.port)) c.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\n" b"Content-Length: " + str(len(body)).encode() + b"\r\n\r\n" + body) time.sleep(1.0) c.close() t0 = time.monotonic() while self.engine.cancelled_after is None and time.monotonic() - t0 < 10: time.sleep(0.05) self.assertIsNotNone(self.engine.cancelled_after, "the request was not cancelled") self.assertLess(self.engine.cancelled_after, 3.0) def test_non_streamed(self): self.hang_up(False) def test_streamed(self): self.hang_up(True) class EngineDeath(unittest.TestCase): """Issue #27: a dead engine is an error (not "length"), and the next request starts it again.""" def test_error_then_restart(self): tok = ByteTokenizer() eng = DyingEngine(tok, "\n\n" + ANSWER, max_context=CTX) svc = Service(eng, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: def post(body): req = urllib.request.Request(base + "/v1/chat/completions", data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, r.read().decode() except urllib.error.HTTPError as e: with e: return e.code, e.read().decode() msgs = [{"role": "user", "content": "hi"}] code, text = post({"model": "m", "messages": msgs, "max_tokens": 50, "stream": True}) self.assertEqual(code, 200) self.assertIn('"error"', text) self.assertIn("stopped unexpectedly", text) self.assertTrue(text.rstrip().endswith("data: [DONE]")) self.assertEqual(svc.metrics()["requests"][0]["finish"], "error") code, text = post({"model": "m", "messages": msgs, "max_tokens": 50}) self.assertEqual(code, 200, text) self.assertEqual(eng.restarts, 1) self.assertEqual(json.loads(text)["usage"]["completion_tokens"], 50) finally: httpd.shutdown() httpd.server_close() def test_engine_err_mid_stream(self): """The engine's ERR line after the stream started reaches the client as an error event (it used to be a 400 written into the open stream, which clients read as an empty answer).""" class ErrEngine(MockEngine): def generate(self, ids, max_new, sampling, cancel, embeddings=None): yield None # a prompt-progress heartbeat: the stream has started raise ValueError("verify: layer 31 never rang (an illegal memory access was encountered)") tok = ByteTokenizer() svc = Service(ErrEngine(tok, ANSWER, max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: for path, body in [("/v1/chat/completions", {"model": "m", "stream": True, "max_tokens": 20, "messages": [{"role": "user", "content": "hi"}]}), ("/v1/messages", {"model": "m", "stream": True, "max_tokens": 20, "messages": [{"role": "user", "content": "hi"}]})]: req = urllib.request.Request(base + path, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=30) as r: text = r.read().decode() self.assertIn("illegal memory access", text, path) self.assertNotIn("HTTP/1", text, path) self.assertEqual(svc.metrics()["requests"][0]["finish"], "error") finally: httpd.shutdown() httpd.server_close() class DoneLineEngine(MockEngine): """The mock engine whose `last` comes from a DONE line, parsed as StrataEngine parses it.""" def __init__(self, *a, done_lines=(), **kw): super().__init__(*a, **kw) self.done_lines = list(done_lines) def generate(self, ids, max_new, sampling, cancel, embeddings=None): try: yield from super().generate(ids, max_new, sampling, cancel, embeddings) finally: StrataEngine._parse_done(self, self.done_lines.pop(0)) class DraftCounts(unittest.TestCase): """#457: GET /metrics gives each request's speculative draft counts (offered / accepted, from the engine's DONE line; None when the line has no such fields) and their running sums in the totals.""" def test_drafts_in_history_and_totals(self): tok = ByteTokenizer() engine = DoneLineEngine(tok, "\n\nok", max_context=CTX, done_lines=[ "DONE 4 20 40.0 30.0 stop 7 12 0", # 7 of 12 drafts accepted "DONE 4 20 40.0 30.0 stop", # an engine that reports no drafts "DONE 4 20 40.0 30.0 stop 3 5 0 9 10"]) svc = Service(engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) self.assertEqual((svc.totals["drafts_offered"], svc.totals["drafts_accepted"]), (0, 0)) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: for _ in range(3): body = json.dumps({"model": "m", "max_tokens": 10, "messages": [{"role": "user", "content": "hi"}]}) with urllib.request.urlopen(urllib.request.Request(base + "/v1/chat/completions", data=body.encode(), headers={"Content-Type": "application/json"}), timeout=30) as r: self.assertEqual(r.status, 200) with urllib.request.urlopen(base + "/metrics", timeout=10) as r: m = json.loads(r.read()) finally: httpd.shutdown() httpd.server_close() rows = m["requests"] # newest first self.assertEqual([(r["drafts_offered"], r["drafts_accepted"]) for r in rows], [(5, 3), (None, None), (12, 7)]) self.assertEqual((m["totals"]["drafts_offered"], m["totals"]["drafts_accepted"]), (17, 10)) class PcieShare(unittest.TestCase): """#588: the hit rate stays the VRAM share of the lookups; the routed experts the GPU read over PCIe (the DONE line's 16th field, engine 0.1.39+) are given as their own share of all routed experts.""" def test_history(self): tok = ByteTokenizer() engine = DoneLineEngine(tok, "\n\nok", max_context=CTX, done_lines=[ "DONE 4 20 40.0 30.0 stop 3 5 0 60 100 0 0 0.0 20 25", # 25 more over PCIe: 20% of 125 routed "DONE 4 20 40.0 30.0 stop 3 5 0 60 100 0 0 0.0 20 0", "DONE 4 20 40.0 30.0 stop 3 5 0 60 100 0 0 0.0 20"]) # an older engine svc = Service(engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) with contextlib.redirect_stdout(io.StringIO()) as out: for _ in range(3): list(svc.run(tok.encode("hi"), False, None, 10, {}, threading.Event())) rows = list(svc.history) self.assertEqual([r["hit_rate"] for r in rows], [0.6, 0.6, 0.6]) self.assertEqual([r["pcie_share"] for r in rows], [0.2, 0.0, None]) self.assertIn("expert cache 60.0% hit (+20.0% of the routed experts over PCIe)", out.getvalue()) class LearnedProfile(unittest.TestCase): """#477: "expert_profile_save" in the config: the engine saves its learned profile there, and the next start begins from it when it is a profile of the same model; without the key the arguments are unchanged.""" def write(self, path, nl=48, ne=512, n=4): sys.path.insert(0, str(ROOT / "tools")) import make_profile old = make_profile.N_LAYER make_profile.N_LAYER = nl try: make_profile.write_profile(path, [(i % nl, i // nl) for i in range(n)], n_expert=ne) finally: make_profile.N_LAYER = old def test_without_the_key_nothing_changes(self): args = ["--native", "x", "--expert-profile", "data/expert-profile.bin", "--adapt-every", "4"] self.assertEqual(engine_args({"args": list(args)}), args) self.assertEqual(engine_args({"args": list(args), "expert_profile_save": ""}), args) def test_save_and_start_from_it(self): d = Path(tempfile.mkdtemp()) self.write(d / "base.bin") cfg = {"args": ["--expert-profile", "base.bin"], "cwd": str(d), "expert_profile_save": "learned.bin", "expert_profile_save_every": 5} # nothing saved yet: the config's profile, and the engine is told where to save self.assertEqual(engine_args(cfg), ["--expert-profile", "base.bin", "--expert-profile-save", "learned.bin", "--expert-profile-save-every", "5"]) self.write(d / "learned.bin") self.assertEqual(engine_args(cfg)[:2], ["--expert-profile", "learned.bin"]) self.write(d / "learned.bin", ne=256) # another model's: not used self.assertEqual(engine_args(cfg)[:2], ["--expert-profile", "base.bin"]) (d / "learned.bin").write_bytes(b"STRP" + bytes(20)) # not a whole profile self.assertEqual(engine_args(cfg)[:2], ["--expert-profile", "base.bin"]) self.write(d / "learned.bin") (d / "learned.bin").write_bytes((d / "learned.bin").read_bytes()[:30]) # truncated self.assertEqual(engine_args(cfg)[:2], ["--expert-profile", "base.bin"]) def test_no_profile_in_the_args(self): cfg = {"args": ["--native", "x"], "expert_profile_save": "learned.bin"} self.assertEqual(engine_args(cfg), ["--native", "x", "--expert-profile-save", "learned.bin"]) class RepeatStop(unittest.TestCase): """#606: one token repeated repeat_stop_tokens times in a row ends the reply as "length"; 0 turns it off.""" def run_reply(self, script, limit=None): tok = ByteTokenizer() svc = Service(MockEngine(tok, script, max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) if limit is not None: svc.repeat_stop_tokens = limit ids = tok.encode("hi") with contextlib.redirect_stdout(io.StringIO()) as out: done = [x for kind, x in svc.run(ids, False, None, 3000, {}, threading.Event()) if kind == "done"][0] return done, out.getvalue() def test_a_long_run_is_ended(self): done, log = self.run_reply("ok " + "!" * 1000 + " never") self.assertEqual(done["finish"], "length") self.assertEqual(done["completion_tokens"], 3 + 256) self.assertIn("repeated one token ('!') 256 times", log) def test_short_runs_and_off(self): done, _ = self.run_reply("=" * 255 + " fine") self.assertEqual(done["finish"], "stop") done, log = self.run_reply("!" * 1000, limit=0) self.assertEqual((done["finish"], done["completion_tokens"]), ("stop", 1001)) self.assertNotIn("repeated one token", log) done, _ = self.run_reply("ab" * 400, limit=8) # alternating tokens are not one run self.assertEqual(done["finish"], "stop") class LayerSplit(unittest.TestCase): """#644: "layer_split" is the first layer of each later GPU; a list is accepted, counts per card are not.""" def cfg(self, split, gpus=(2, 0, 1, 3)): c = {"args": ["--native", "x"], "gpu": list(gpus)} if split is not ...: c["layer_split"] = split return c def test_auto_and_absent(self): for v in (..., None, "", "auto", "AUTO"): self.assertEqual(engine_args(self.cfg(v))[-2:], ["--layer-split", "auto"]) def test_string_and_list(self): self.assertEqual(engine_args(self.cfg("24,36,42"))[-2:], ["--layer-split", "24,36,42"]) self.assertEqual(engine_args(self.cfg(" 24, 36 ,42 "))[-2:], ["--layer-split", "24,36,42"]) self.assertEqual(engine_args(self.cfg([24, 36, 42]))[-2:], ["--layer-split", "24,36,42"]) self.assertEqual(engine_args(self.cfg(18, gpus=(0, 1)))[-2:], ["--layer-split", "18"]) self.assertEqual(engine_args(self.cfg([18], gpus=(0, 1)))[-2:], ["--layer-split", "18"]) def test_counts_per_card_are_refused_with_the_format(self): for bad in ("24,16,12,12", [24, 16, 12, 12], "24,16,12", "24,36", "x", [24.5, 30, 40], "1,20,30", [True]): with self.assertRaises(ValueError) as e: layer_split_value(self.cfg(bad)) self.assertIn("first layer of each later GPU", str(e.exception)) self.assertIn('"12,24,36"', str(e.exception)) # the example for 4 GPUs def test_one_gpu_ignores_it(self): self.assertEqual(engine_args({"args": ["--native", "x"], "gpu": [0], "layer_split": "24,16"}), ["--native", "x"]) class DraftHeadHint(unittest.TestCase): """#474: a start that stopped at "the draft head does not fit" says what to change, from this start's log lines.""" def log(self, text, before=""): d = tempfile.mkdtemp() p = Path(d) / "engine.log" p.write_text(before + text, encoding="utf-8") return str(p), len(before.encode()) def test_the_engines_hint_is_relayed(self): p, off = self.log("strata mtp: the draft head over 106299 tokens needs 348 MiB of VRAM and 120 MiB is free.\n" "strata mtp: hint: a smaller draft vocabulary needs less VRAM: --draft-vocab en (...)\n" "strata serve: mtp: the draft head does not fit\n") h = start_failure_hint(p, off) self.assertIn("the draft head does not fit", h) self.assertIn("348 MiB", h) self.assertIn("--draft-vocab en", h) def test_an_older_engine_gets_the_advice_in_words(self): p, off = self.log("strata serve: mtp: the draft head does not fit\n") self.assertIn("--draft-vocab en", start_failure_hint(p, off)) def test_other_failures_and_earlier_starts_add_nothing(self): p, off = self.log("strata serve: cannot open the pack\n") self.assertEqual(start_failure_hint(p, off), "") # an earlier start's failure (before this start's offset) is not this one's p, off = self.log("strata serve: cannot open the pack\n", before="strata serve: mtp: the draft head does not fit\n") self.assertEqual(start_failure_hint(p, off), "") self.assertEqual(start_failure_hint(None, 0), "") self.assertEqual(start_failure_hint(str(Path(tempfile.mkdtemp()) / "missing.log"), 0), "") class DesktopVramNote(unittest.TestCase): """#560 #516: an AMD card on a Linux desktop with little VRAM left after the start gets a recommended reserve.""" def test_when_it_applies(self): from serve.server import desktop_vram_note note = desktop_vram_note("hip", 624, ["--kv", "int8"], True) self.assertIn("624 MiB of VRAM free", note) self.assertIn("--vram-reserve-mib 3072", note) self.assertIn("2.3 GB less", note) def test_when_it_does_not(self): from serve.server import desktop_vram_note self.assertEqual(desktop_vram_note(None, 624, [], True), "") # NVIDIA self.assertEqual(desktop_vram_note("hip", 624, [], False), "") # no desktop session self.assertEqual(desktop_vram_note("hip", 2994, [], True), "") # room left self.assertEqual(desktop_vram_note("hip", None, [], True), "") # lazy start: no INFO yet self.assertEqual(desktop_vram_note("hip", 900, ["--vram-reserve-mib", "4000"], True), "") # already raised def test_desktop_detection(self): from serve import server with mock.patch.object(server.os, "name", "posix"), mock.patch.object(server.sys, "platform", "linux"): self.assertTrue(server.linux_desktop({"WAYLAND_DISPLAY": "wayland-0"})) self.assertFalse(server.linux_desktop({})) class StartFailureLog(unittest.TestCase): """#496: whatever stopped the engine before READY, the error carries this start's last log lines.""" def test_the_last_lines_of_this_start(self): from serve.server import start_log_tail d = tempfile.mkdtemp() p = Path(d) / "engine.log" before = "strata serve: an earlier start's line\n" p.write_text(before + "".join(f"strata serve: line {i}\n" for i in range(30)) + "\n" "strata serve: cannot open the pack\n", encoding="utf-8") tail = start_log_tail(str(p), len(before.encode())) self.assertIn("the engine log's last lines:", tail) self.assertIn("cannot open the pack", tail) self.assertIn("line 29", tail) self.assertNotIn("line 10\n", tail + "\n") # 20 lines: 11..29 and the last one self.assertIn("line 11", tail) self.assertNotIn("earlier start", tail) short = start_log_tail(str(p), len(before.encode()), n=3) self.assertEqual(short.count("\n "), 3) self.assertEqual(start_log_tail(str(p), p.stat().st_size), "") # nothing from this start self.assertEqual(start_log_tail(None, 0), "") self.assertEqual(start_log_tail(str(Path(d) / "missing.log"), 0), "") def test_the_start_error_has_them(self): import serve.server as server fake = "import sys\nsys.stderr.write('strata serve: cannot open the pack packs/x\\n')\nsys.exit(2)\n" with tempfile.TemporaryDirectory() as d: script, log = Path(d) / "fake_strata.py", Path(d) / "strata.log" script.write_text(fake, encoding="utf-8") log.write_text("an earlier start\n", encoding="utf-8") real = server.subprocess.Popen with mock.patch.object(server.subprocess, "Popen", lambda cmd, **kw: real([sys.executable, str(script), *cmd[1:]], **kw)), \ mock.patch.object(server, "narrate_start", lambda *a, **k: None): with self.assertRaises(RuntimeError) as cm: StrataEngine("strata", [], log=str(log)) text = str(cm.exception) self.assertIn("exited before it was ready", text) self.assertIn("cannot open the pack packs/x", text) self.assertNotIn("an earlier start", text) class StartNarrator(unittest.TestCase): """#505: the start's words say what happens to the experts - --mmap-experts loads nothing into RAM up front.""" def test_the_words_follow_the_flags(self): from serve.server import experts_loading_words self.assertIn("loading the experts into RAM (about 38 GB)", experts_loading_words([], "about 38 GB")) mapped = experts_loading_words(["--mmap-experts"], "about 47 GB") self.assertIn("mapping the experts from the model files (about 47 GB", mapped) self.assertIn("not loaded into RAM", mapped) self.assertIn("the GPU does not hold", experts_loading_words(["--resident-experts"], "about 47 GB")) budget = experts_loading_words(["--mmap-experts", "--resident-budget-gib", "71"], "about 50 GB") self.assertIn("up to 71 GiB", budget) self.assertNotIn("about 50 GB", budget) def narrate(self, args, lines): import contextlib import io from serve.server import narrate_start d = tempfile.mkdtemp() log = Path(d) / "engine.log" log.write_text("".join(x + "\n" for x in lines), encoding="utf-8") done, out = threading.Event(), io.StringIO() with contextlib.redirect_stdout(out): t = threading.Thread(target=narrate_start, args=(str(log), 0, args, done)) t.start() time.sleep(0.8) done.set() t.join(5) return out.getvalue() def test_a_mapped_start(self): said = self.narrate(["--mmap-experts"], ["strata generate: experts via mmap (--mmap-experts; the GGUF shards " "in place, no experts.bin)"]) self.assertIn("mapping the experts from the model files", said) self.assertNotIn("loading the experts into RAM", said) def test_an_arena_start(self): said = self.narrate([], ["strata generate: expert arena: resident, 31.64 GiB", "strata generate: loaded 31.64 GiB at 3.17 GiB/s"]) self.assertIn("loading the experts into RAM (tens of GB)", said) self.assertIn("experts loaded: 31.64 GiB at 3.17 GiB/s", said) class CancelledRead(unittest.TestCase): """#471: a request cancelled while its prompt was read is recorded with the tokens the engine read (the DONE line's 15th field), not the whole prompt; an older engine's line (no such field) keeps the whole prompt.""" def test_parse_done_read_field(self): e = SimpleNamespace() StrataEngine._parse_done(e, "DONE 0 98179 17565.0 0.0 cancel 0 0 0 0 0 0 0 0.0 12288") self.assertEqual((e.last["prompt_tokens"], e.last["prompt_read"], e.last["finish"]), (98179, 12288, "cancel")) StrataEngine._parse_done(e, "DONE 0 98179 17565.0 0.0 cancel 0 0 0 0 0 0 0 0.0") self.assertNotIn("prompt_read", e.last) def test_prompt_tokens_seen(self): last = {"finish": "cancel", "reused": 1000, "prompt_read": 2000} self.assertEqual(prompt_tokens_seen(98179, last), 3000) self.assertEqual(prompt_tokens_seen(98179, {**last, "finish": "stop"}), 98179) # read in full: all of it self.assertEqual(prompt_tokens_seen(98179, {"finish": "cancel", "reused": 0}), 98179) # an older engine self.assertEqual(prompt_tokens_seen(98179, {}), 98179) # no DONE at all self.assertEqual(prompt_tokens_seen(10, {**last, "prompt_read": 50}), 10) # never past the prompt def test_history_and_totals(self): tok = ByteTokenizer() engine = DoneLineEngine(tok, "\n\nok", max_context=CTX, done_lines=[ "DONE 0 20 400.0 0.0 cancel 0 0 0 0 0 0 0 0.0 8", # stopped after 8 prompt tokens "DONE 4 20 40.0 30.0 stop 0 0 0 0 0 0 0 0.0 20", "DONE 0 20 400.0 0.0 cancel 0 0 0"]) # an older engine: no read count svc = Service(engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: for _ in range(3): body = json.dumps({"model": "m", "max_tokens": 10, "messages": [{"role": "user", "content": "hi"}]}) with urllib.request.urlopen(urllib.request.Request(base + "/v1/chat/completions", data=body.encode(), headers={"Content-Type": "application/json"}), timeout=30) as r: self.assertEqual(r.status, 200) with urllib.request.urlopen(base + "/metrics", timeout=10) as r: m = json.loads(r.read()) finally: httpd.shutdown() httpd.server_close() rows = m["requests"][::-1] # oldest first total = rows[0]["prompt_total"] self.assertGreater(total, 8) self.assertEqual([r["prompt_total"] for r in rows], [total] * 3) self.assertEqual([(r["prompt_tokens"], r["prompt_read"]) for r in rows], [(8, 8), (total, 20), (total, None)]) self.assertEqual(m["totals"]["prompt_tokens"], 8 + 2 * total) class LiveRate(unittest.TestCase): """The Monitor's Speed readout: live.tok_s is a rate, and a request that never got a DONE keeps no counters. It used to be `generated / (now - first_token)` - the mean since the first token, whose first sample is 1/elapsed. Against a paced engine that reads five-digit numbers for the first instant of every answer and undershoots for the first second after that. It is now the rate over the last RATE_WINDOW_S, with the mean still available as `live.tok_s_mean` for anyone who wants it.""" PACE_S = 0.02 # 50 tokens/s: a 30-token answer takes about 0.6 s TOKENS = 30 def setUp(self): self.tok = ByteTokenizer() self.engine = MockEngine(self.tok, "x" * self.TOKENS, max_context=CTX, delay_s=self.PACE_S) self.svc = Service(self.engine, self.tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) self.httpd = serve(self.svc, port=0) self.base = f"http://127.0.0.1:{self.httpd.server_address[1]}" def tearDown(self): self.httpd.shutdown() self.httpd.server_close() def metrics(self): with urllib.request.urlopen(self.base + "/metrics", timeout=10) as r: return json.loads(r.read()) def test_prefill_rate_excludes_cached_tokens(self): import io import queue from types import SimpleNamespace engine = StrataEngine.__new__(StrataEngine) engine.proc = SimpleNamespace(stdin=io.StringIO(), poll=lambda: None) # alive() asks it (#208) engine.lines = queue.Queue() engine.can_stop = False engine.max_context = 262144 engine.prefill_tok_s_mean = 9999.0 engine.lines.put("PP 10000 12000 2000 1000.0") # 8000 cached, 2000 newly read in two seconds engine.lines.put("DONE 1 12000 4000 10 stop 0 0 8000") gen = engine.generate([1], 1, {}, threading.Event()) self.assertIsNone(next(gen)) self.assertEqual(engine.progress, (10000, 12000)) self.assertEqual(engine.prefill_tok_s_mean, 1000.0) self.svc.engine = engine self.svc.status.update(busy=True, first_token=None) self.assertEqual(self.metrics()["live"]["prefill_tok_s_mean"], 1000.0) self.assertNotIn("prefill_tok_s", self.metrics()["live"]) self.assertEqual(self.svc._prefill_tok_s_mean(), 1000.0) self.svc.status.update(first_token=time.time(), generated=1) self.assertEqual(self.svc._prefill_tok_s_mean(), 0.0) self.assertEqual(list(gen), []) timings = request_timings(12000, 1, engine.last) self.assertEqual(timings["prompt_per_second"], 1000.0) engine.lines.put("PP 8000 12000") engine.lines.put("DONE 0 12000 0 0 stop 0 0 12000") gen = engine.generate([1], 1, {}, threading.Event()) next(gen) self.assertIsNone(engine.prefill_tok_s_mean) list(gen) self.svc.status["busy"] = False self.assertIsNone(self.metrics()["live"]["prefill_tok_s_mean"]) def test_the_live_number_is_a_rate(self): live_samples, stop = [], threading.Event() def poll(): # what the Monitor polls, at 10 ms while not stop.is_set(): live = self.metrics()["live"] if live["state"] == "generating" and live["tok_s"] is not None: live_samples.append((live["generated"], live["tok_s"], live["tok_s_mean"])) time.sleep(0.01) body = json.dumps({"model": "m", "max_tokens": self.TOKENS, "temperature": 0, "messages": [{"role": "user", "content": "hi"}]}).encode() watcher = threading.Thread(target=poll, daemon=True) watcher.start() t0 = time.time() try: with urllib.request.urlopen(urllib.request.Request(self.base + "/v1/chat/completions", data=body, headers={"Content-Type": "application/json"}), timeout=30) as r: usage = json.loads(r.read())["usage"] finally: stop.set() watcher.join(2) true_rate = usage["completion_tokens"] / (time.time() - t0) self.assertGreaterEqual(len(live_samples), 3, "too few live readings to judge the readout") self.assertLess(max(s for _g, s, _m in live_samples), 4 * true_rate, f"live.tok_s peaked at {max(s for _g, s, _m in live_samples):.1f} tok/s " f"for a {true_rate:.1f} tok/s engine") self.assertEqual(self.metrics()["live"]["state"], "idle") self.assertIsNone(self.metrics()["live"]["tok_s"]) def test_a_request_without_a_done_keeps_no_engine_counters(self): """An engine that dies mid-answer: the previous request's `last` must not become this row's decode rate.""" class HalfDead(MockEngine): last = {"generated": 99, "prompt_tokens": 9, "prompt_ms": 10.0, "decode_ms": 100.0, "finish": "stop"} def generate(self, ids, max_new, sampling, cancel, embeddings=None): for i, t in enumerate(super().generate(ids, max_new, sampling, cancel, embeddings)): if i == 3: raise EngineDied("the engine stopped unexpectedly (exit code -9)") yield t tok = ByteTokenizer() svc = Service(HalfDead(tok, "x" * self.TOKENS, max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: body = json.dumps({"model": "m", "max_tokens": self.TOKENS, "stream": True, "messages": [{"role": "user", "content": "hi"}]}).encode() with urllib.request.urlopen(urllib.request.Request(base + "/v1/chat/completions", data=body, headers={"Content-Type": "application/json"}), timeout=30) as r: text = r.read().decode() self.assertIn('"error"', text) row = svc.metrics()["requests"][0] self.assertEqual(row["finish"], "error") self.assertEqual(row["output_tokens"], 3) self.assertIsNone(row["decode_tok_s"], "the previous request's counters were recorded as this one's") self.assertIsNone(row["engine_generated"]) finally: httpd.shutdown() httpd.server_close() class SharedSettings(unittest.TestCase): """The web app's "Use for other apps too": POST /settings makes its Chat settings every client's defaults.""" @classmethod def setUpClass(cls): import tempfile class Sampled(RecordingEngine): def generate(self, ids, max_new, sampling, cancel, embeddings=None): self.last_sampling = dict(sampling or {}) yield from super().generate(ids, max_new, sampling, cancel, embeddings) tok = ByteTokenizer() cls.engine = Sampled(tok, "\n\nhello", max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.tmp = tempfile.TemporaryDirectory() cls.svc.shared_path = os.path.join(cls.tmp.name, "strata-x.shared-settings.json") cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() cls.tmp.cleanup() def req(self, path, body, headers=None, raw=None): h = {"Content-Type": "application/json", **(headers or {})} r = urllib.request.Request(self.base + path, data=raw if raw is not None else json.dumps(body).encode(), headers=h) try: with urllib.request.urlopen(r, timeout=30) as resp: return resp.status, json.loads(resp.read()) except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()) def chat(self, **extra): return self.req("/v1/chat/completions", {"model": "m", "messages": [{"role": "user", "content": "hi"}], **extra}) def tearDown(self): self.svc.set_shared(None) def test_other_apps_get_the_chat_settings(self): d = {"temperature": 0.3, "top_p": 0.9, "top_k": 10, "seed": 7, "max_tokens": 77, "reasoning_effort": "low", "experimental_speed_projection": False} code, b = self.req("/settings", {"defaults": d}) self.assertEqual(code, 200, b) self.assertTrue(b["shared"]) self.assertTrue(os.path.exists(self.svc.shared_path)) code, _ = self.chat() # a client that sets nothing self.assertEqual(code, 200) got = self.engine.last_sampling for k in ("temperature", "top_p", "top_k", "seed", "experimental_speed_projection"): self.assertEqual(got[k], d[k], k) self.assertEqual(self.engine.last_max_new, 77) code, _ = self.chat(temperature=0.9, max_tokens=5) # its own values win self.assertEqual(self.engine.last_sampling["temperature"], 0.9) self.assertEqual(self.engine.last_max_new, 5) r = self.svc.with_shared({"messages": []}, "openai") self.assertEqual(r["reasoning_effort"], "low") self.assertEqual(self.svc.with_shared({"reasoning_effort": "high"}, "openai")["reasoning_effort"], "high") self.assertEqual(self.svc.with_shared({}, "anthropic")["output_config"], {"effort": "low"}) def test_off_again(self): self.req("/settings", {"defaults": {"temperature": 0.3}}) code, b = self.req("/settings", {"defaults": None}) self.assertEqual((code, b["shared"]), (200, False)) self.assertFalse(os.path.exists(self.svc.shared_path)) self.chat() self.assertNotIn("temperature", self.engine.last_sampling) def test_only_strata_s_own_page_may_set_them(self): code, _ = self.req("/settings", None, {"Content-Type": "text/plain"}, raw=b'{"defaults": {"temperature": 1}}') self.assertEqual(code, 415) code, _ = self.req("/settings", {"defaults": {"temperature": 1}}, {"Origin": "http://evil.example"}) self.assertEqual(code, 403) code, b = self.req("/settings", {"defaults": {"temperature": 9}}) self.assertEqual(code, 400) self.assertIn("temperature", b["error"]["message"]) self.assertEqual(self.svc.shared, {}) host = self.base.split("://", 1)[1] code, _ = self.req("/settings", {"defaults": {"temperature": 1}}, {"Origin": "http://" + host}) self.assertEqual(code, 200) def test_they_need_the_key_when_one_is_set(self): self.svc.api_key = "secret" try: self.assertEqual(self.req("/settings", {"defaults": {"temperature": 1}})[0], 401) self.assertEqual(self.req("/settings", {"defaults": {"temperature": 1}}, {"Authorization": "Bearer secret"})[0], 200) finally: self.svc.api_key = "" def test_proxy_headers_do_not_make_a_page_strata_s_own(self): # #321: X-Forwarded-*, CF-Ray or CF-Connecting-IP say nothing about the page that sent the request - any web # page behind any proxy would otherwise change the settings (or run MCP tools) for extra in ({"X-Forwarded-Host": "proxy.example.com"}, {"CF-Ray": "1234567890"}, {"X-Forwarded-For": "203.0.113.9"}, {"CF-Connecting-IP": "203.0.113.9"}): code, _ = self.req("/settings", {"defaults": {"temperature": 1}}, {"Origin": "https://proxy.example.com", **extra}) self.assertEqual(code, 403, extra) # another port on the same host is another site (a local dev server's page) code, _ = self.req("/settings", {"defaults": {"temperature": 1}}, {"Origin": "http://127.0.0.1:1"}) self.assertEqual(code, 403) def test_a_trusted_origin_is_strata_s_own_page(self): # the web app behind a reverse proxy or tunnel: the config's trusted_origins self.svc.trusted_origins = ["https://strata.example.com"] try: code, _ = self.req("/settings", {"defaults": {}}, {"Origin": "https://strata.example.com"}) self.assertEqual(code, 200) code, _ = self.req("/settings", {"defaults": {}}, {"Origin": "https://evil.example.com"}) self.assertEqual(code, 403) finally: self.svc.trusted_origins = [] class WebApp(unittest.TestCase): """The web app (PR #22's dashboard idea, rebuilt): its page and files, and GET /metrics.""" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.svc = Service(RecordingEngine(tok, "\n\nhello", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() def get(self, path, headers=None): req = urllib.request.Request(self.base + path, headers=headers or {}) try: with urllib.request.urlopen(req, timeout=10) as r: return r.status, r.headers.get("Content-Type", ""), r.read() except urllib.error.HTTPError as e: return e.code, e.headers.get("Content-Type", ""), e.read() def test_page_and_files(self): code, ctype, body = self.get("/") self.assertEqual(code, 200) self.assertIn("text/html", ctype) self.assertIn(b"\"web/app.js\"", body) # relative since #82 (works behind a path-prefixed proxy) for path, want in (("/web/app.js", "javascript"), ("/web/app.css", "text/css"), ("/web/tokens.css", "text/css"), ("/web/components.css", "text/css"), ("/web/sprite.svg", "image/svg+xml")): with self.subTest(path=path): code, ctype, _ = self.get(path) self.assertEqual(code, 200) self.assertIn(want, ctype) def test_only_the_app_files_are_served(self): for path in ("/web/..%2Fserver.py", "/web/index.html", "/web/test.py", "/fonts/..%2F..%2Fsetup.py", "/fonts/missing.woff2", "/fonts/x.ttf"): with self.subTest(path=path): self.assertEqual(self.get(path)[0], 404) def test_metrics(self): data = json.dumps({"model": "m", "messages": [{"role": "user", "content": "hi"}], "max_tokens": 5}).encode() urllib.request.urlopen(urllib.request.Request(self.base + "/v1/chat/completions", data=data, headers={"Content-Type": "application/json"}), timeout=10).read() code, ctype, body = self.get("/metrics") self.assertEqual(code, 200) m = json.loads(body) for key in ("engine", "live", "requests", "hardware", "hardware_static", "history"): self.assertIn(key, m) self.assertEqual(m["engine"]["max_context"], CTX) self.assertEqual(m["live"]["state"], "idle") self.assertEqual(m["requests"][0]["output_tokens"], 5) def test_model_discovery_and_props(self): svc = self.svc previous = svc.engine.max_context, svc.vision, svc.sampling_defaults, svc.shared try: svc.engine.max_context = 262144 svc.sampling_defaults = {"temperature": 1.0, "repetition_penalty": 1.1} svc.shared = {"temperature": 0.7, "max_tokens": 4096} for vision in (None, object()): svc.vision = vision for path in ("/models", "/v1/models"): code, _, body = self.get(path) self.assertEqual(code, 200) models = json.loads(body)["data"] self.assertEqual(len(models), 1) model = models[0] self.assertEqual(model["id"], svc.model) self.assertEqual(model["status"]["value"], "loaded") self.assertEqual(model["meta"]["n_ctx"], 262144) self.assertEqual(model["architecture"]["input_modalities"], ["text", "image"] if vision else ["text"]) code, _, body = self.get("/props?model=" + svc.model + "&autoload=false") self.assertEqual(code, 200) props = json.loads(body) self.assertEqual(props["default_generation_settings"]["n_ctx"], 262144) self.assertEqual(props["default_generation_settings"]["params"], {"temperature": 0.7, "repeat_penalty": 1.1, "n_predict": 4096}) self.assertEqual(props["chat_template"], (ROOT / "serve/chat_template.jinja").read_text(encoding="utf-8")) self.assertEqual(props["modalities"]["vision"], vision is not None) self.assertEqual(props["total_slots"], 1) self.assertFalse(props["models_autoload"]) svc.shared = {} props = json.loads(self.get("/props")[2]) self.assertEqual(props["default_generation_settings"]["params"]["n_predict"], -1) self.assertEqual(self.get("/props?model=not-loaded&autoload=true")[0], 404) finally: svc.engine.max_context, svc.vision, svc.sampling_defaults, svc.shared = previous def test_discovery_needs_the_api_key(self): self.svc.api_key = "secret" try: for path in ("/models", "/v1/models", "/props", "/slots"): self.assertEqual(self.get(path)[0], 401) self.assertEqual(self.get(path, {"Authorization": "Bearer secret"})[0], 200) finally: self.svc.api_key = "" def test_build_model_path_and_slot_status(self): engine = self.svc.engine engine.model_path = "models/example.gguf" engine.info = {"version": "0.1.21"} try: props = json.loads(self.get("/props")[2]) self.assertEqual(props["model_path"], engine.model_path) self.assertEqual(props["build_info"], "Strata 0.1.21") for busy in (True, False): with self.svc.status_lock: self.svc.status["busy"] = busy code, _, body = self.get("/slots") self.assertEqual(code, 200) self.assertEqual(json.loads(body), [{"id": 0, "n_ctx": CTX, "is_processing": busy}]) finally: with self.svc.status_lock: self.svc.status["busy"] = False del engine.model_path, engine.info props = json.loads(self.get("/props")[2]) self.assertNotIn("build_info", props) self.assertNotIn("model_path", props) def test_discovery_does_not_restart_a_dead_engine(self): self.svc.engine.alive = lambda: False try: for path in ("/models", "/v1/models"): code, _, body = self.get(path) self.assertEqual(code, 200) self.assertEqual(json.loads(body)["data"], []) self.assertEqual(self.get("/props")[0], 503) self.assertEqual(json.loads(self.get("/slots")[2]), []) finally: del self.svc.engine.alive def test_metrics_need_the_key_when_one_is_set(self): self.svc.api_key = "secret" try: self.assertEqual(self.get("/metrics")[0], 401) self.assertEqual(self.get("/metrics", {"Authorization": "Bearer secret"})[0], 200) self.assertEqual(self.get("/")[0], 200) # the page itself asks for the key finally: self.svc.api_key = "" class ClockedEngine(MockEngine): """The mock engine with StrataEngine's clock: `last` as the engine's DONE line gives it, the conversation cache holding the first REUSED tokens of every prompt.""" REUSED = 5 def generate(self, ids, max_new, sampling, cancel, embeddings=None): n = 0 try: for t in super().generate(ids, max_new, sampling, cancel, embeddings): n += 1 yield t finally: # as StrataEngine reads its DONE line: also when the server closes the request at a stop token self.last = {"generated": n, "prompt_tokens": len(ids), "prompt_ms": 40.0, "decode_ms": 20.0 * n, "finish": "stop", "reused": min(self.REUSED, len(ids)), "hits": 9, "lookups": 10} class UsageAndStatus(unittest.TestCase): """What clients read besides the text: the part of the prompt the conversation cache held (OpenAI's prompt_tokens_details.cached_tokens, Anthropic's cache_read_input_tokens), llama.cpp's timings, GET /v1/status.""" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.engine = ClockedEngine(tok, "\n\nok", max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def request(self, path, body=None): req = urllib.request.Request(self.base + path, data=None if body is None else json.dumps(body).encode(), headers={"Content-Type": "application/json", "anthropic-version": "2023-06-01"}) with urllib.request.urlopen(req, timeout=30) as r: return r.status, r.read() def chat(self, path, stream=False): body = {"model": "x", "max_tokens": 20, "messages": [{"role": "user", "content": "hi"}], "stream": stream} status, raw = self.request(path, body) self.assertEqual(status, 200) if not stream: return json.loads(raw) return [json.loads(line[6:]) for line in raw.decode().splitlines() if line.startswith("data: {")] def preflight(self, path, origin="https://chat.example.com"): req = urllib.request.Request(self.base + path, method="OPTIONS", headers={"Origin": origin, "Access-Control-Request-Method": "POST", "Access-Control-Request-Headers": "authorization, content-type"}) with urllib.request.urlopen(req, timeout=10) as r: return r.status, r.headers def test_cors_is_off_by_default(self): # #321: OPTIONS is answered, but without cors_origins no page of another origin is let in status, h = self.preflight("/v1/chat/completions") self.assertEqual(status, 204) self.assertIsNone(h.get("Access-Control-Allow-Origin")) with urllib.request.urlopen(self.base + "/health", timeout=10) as r: self.assertIsNone(r.headers.get("Access-Control-Allow-Origin")) def test_cors_for_the_configured_origins_on_the_api_only(self): self.svc.cors_origins = ["https://chat.example.com"] try: status, h = self.preflight("/v1/chat/completions") self.assertEqual((status, h.get("Access-Control-Allow-Origin")), (204, "https://chat.example.com")) self.assertIn("OPTIONS", h.get("Access-Control-Allow-Methods", "")) self.assertIn("authorization", h.get("Access-Control-Allow-Headers", "")) self.assertIsNone(self.preflight("/v1/chat/completions", "https://evil.example.com")[1] .get("Access-Control-Allow-Origin")) # never on the app's own endpoints (/settings, /unload, ...) self.assertIsNone(self.preflight("/settings")[1].get("Access-Control-Allow-Origin")) req = urllib.request.Request(self.base + "/v1/models", headers={"Origin": "https://chat.example.com"}) with urllib.request.urlopen(req, timeout=10) as r: self.assertEqual(r.headers.get("Access-Control-Allow-Origin"), "https://chat.example.com") self.svc.cors_origins = ["*"] self.assertEqual(self.preflight("/v1/messages", "https://any.example.com")[1] .get("Access-Control-Allow-Origin"), "*") finally: self.svc.cors_origins = [] def test_sse_is_not_buffered_by_proxies(self): req = urllib.request.Request(self.base + "/v1/chat/completions", headers={"Content-Type": "application/json"}, data=json.dumps({"model": "m", "stream": True, "messages": [{"role": "user", "content": "hi"}]}).encode()) with urllib.request.urlopen(req, timeout=30) as r: self.assertEqual(r.headers.get("X-Accel-Buffering"), "no") r.read() def test_origin_lists_are_checked(self): from serve.server import origins_of self.assertEqual(origins_of(None, "k", True), []) self.assertEqual(origins_of("https://a.example.com/", "k", False), ["https://a.example.com"]) self.assertEqual(origins_of(["*", "http://localhost:3000"], "k", True), ["*", "http://localhost:3000"]) for bad in ("*", "a.example.com", "https://a.example.com/path", "https://*.example.com", 5): with self.assertRaises(SystemExit): origins_of(bad, "k", False) def test_openai(self): b = self.chat("/v1/chat/completions") u, t = b["usage"], b["timings"] self.assertEqual(u["prompt_tokens_details"]["cached_tokens"], ClockedEngine.REUSED) self.assertEqual(t["cache_n"], ClockedEngine.REUSED) self.assertEqual(t["prompt_n"] + t["cache_n"], u["prompt_tokens"]) self.assertEqual(t["predicted_n"], u["completion_tokens"]) self.assertAlmostEqual(t["prompt_per_second"], t["prompt_n"] / 0.040, delta=0.1) self.assertAlmostEqual(t["predicted_per_second"], 50.0, delta=0.1) # 20 ms a token def test_openai_stream(self): last = self.chat("/v1/chat/completions", stream=True)[-1] self.assertEqual(last["usage"]["prompt_tokens_details"]["cached_tokens"], ClockedEngine.REUSED) self.assertEqual(last["timings"]["cache_n"], ClockedEngine.REUSED) def test_anthropic(self): u = self.chat("/v1/messages")["usage"] self.assertEqual(u["cache_read_input_tokens"], ClockedEngine.REUSED) self.assertEqual(u["input_tokens"] + u["cache_read_input_tokens"], len(self.engine.last_prompt)) self.assertGreater(u["output_tokens"], 0) def test_v1_status(self): self.chat("/v1/chat/completions") status, raw = self.request("/v1/status") self.assertEqual(status, 200) s = json.loads(raw) self.assertEqual(s["model"], self.svc.model) self.assertEqual(s["context"]["max_positions"], CTX) self.assertEqual(s["concurrency"]["serving"], 1) self.assertFalse(s["vision"]["available"]) self.assertEqual(s["activity"]["in_flight"], 0) self.assertGreaterEqual(s["activity"]["requests"], 1) self.assertEqual(s["last_timings"]["cache_n"], ClockedEngine.REUSED) self.assertIn("at", s["last_timings"]) def test_no_clock(self): """An engine without a clock (MockEngine): no timings, nothing cached.""" tok = ByteTokenizer() svc = Service(MockEngine(tok, "\n\nok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) try: data = json.dumps({"model": "x", "max_tokens": 5, "messages": [{"role": "user", "content": "hi"}]}).encode() req = urllib.request.Request(f"http://127.0.0.1:{httpd.server_address[1]}/v1/chat/completions", data=data, headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=30) as r: b = json.loads(r.read()) self.assertNotIn("timings", b) self.assertEqual(b["usage"]["prompt_tokens_details"]["cached_tokens"], 0) self.assertIsNone(svc.v1_status()["last_timings"]) finally: httpd.shutdown() httpd.server_close() class TimingsDrafts(unittest.TestCase): """`timings` carries the speculative draft counts (PR #83's fields) only when the engine reported them.""" def test_draft_fields(self): base = {"prompt_ms": 100.0, "decode_ms": 200.0, "generated": 20, "reused": 4} t = request_timings(24, 20, dict(base, drafts_offered=15, drafts_accepted=11)) self.assertEqual((t["draft_n"], t["draft_n_accepted"]), (15, 11)) self.assertEqual((t["prompt_n"], t["cache_n"]), (20, 4)) self.assertNotIn("draft_n", request_timings(24, 20, base)) self.assertIsNone(request_timings(24, 20, {})) class UnloadableEngine(MockEngine): """A mock engine that can be stopped and started again like StrataEngine (alive / unload / restart).""" def __init__(self, *a, **kw): super().__init__(*a, **kw) self.running, self.unloaded, self.starts = True, False, 0 def alive(self): return self.running def unload(self): self.running, self.unloaded = False, True def restart(self): self.running, self.unloaded = True, False self.starts += 1 class SharingTheGpu(unittest.TestCase): """Idle unload, POST /unload and /load, the free-VRAM guard and the before_load hook (all off by default).""" def setUp(self): tok = ByteTokenizer() self.engine = UnloadableEngine(tok, "\n\nok", max_context=CTX) self.svc = Service(self.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) self.httpd = serve(self.svc, port=0) self.base = f"http://127.0.0.1:{self.httpd.server_address[1]}" def tearDown(self): self.httpd.shutdown() self.httpd.server_close() def req(self, path, body=None): r = urllib.request.Request(self.base + path, data=None if body is None else json.dumps(body).encode(), headers={"Content-Type": "application/json"}, method="GET" if body is None else "POST") try: with urllib.request.urlopen(r, timeout=30) as resp: return resp.status, json.loads(resp.read()) except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()) def chat(self): return self.req("/v1/chat/completions", {"model": "m", "messages": [{"role": "user", "content": "hi"}]}) def test_unload_then_the_next_request_loads(self): self.assertEqual(self.req("/unload", {}), (200, {"status": "unloaded"})) self.assertFalse(self.engine.alive()) self.assertEqual(self.req("/health")[1]["loaded"], False) self.assertEqual(self.req("/v1/models")[1]["data"][0]["status"]["value"], "unloaded") self.assertEqual(self.req("/unload", {}), (200, {"status": "not loaded"})) s, b = self.chat() self.assertEqual(s, 200) self.assertEqual(b["choices"][0]["message"]["content"], "ok") self.assertEqual(self.engine.starts, 1) self.assertEqual(self.req("/health")[1]["loaded"], True) def test_load_endpoint(self): self.svc.unload() self.assertEqual(self.req("/load", {}), (200, {"status": "loaded"})) self.assertTrue(self.engine.alive()) self.assertEqual(self.req("/load", {}), (200, {"status": "loaded"})) self.assertEqual(self.engine.starts, 1) def test_unload_refused_while_a_request_runs(self): with self.svc.fifo: self.assertEqual(self.svc.unload(), "busy") self.assertTrue(self.engine.alive()) def test_idle_unload(self): self.svc.idle_unload_s = 1 self.svc.last_request_at = time.time() self.assertEqual(self.svc.unload(idle_for=1), "busy") # a request just now: not idle yet self.svc.start_idle_unload() deadline = time.time() + 10 while self.engine.alive() and time.time() < deadline: time.sleep(0.1) self.assertFalse(self.engine.alive()) self.assertEqual(self.chat()[0], 200) def test_min_free_vram_refuses_to_load(self): self.svc.min_free_vram_mib = 8000 self.svc.free_vram_mib = lambda: 2000 self.svc.unload() t0 = time.time() s, b = self.chat() self.assertEqual(s, 503) self.assertIn("in use by another program", b["error"]["message"]) self.assertFalse(self.engine.alive()) self.assertGreater(time.time() - t0, 10) # waited for memory being given back first self.svc.free_vram_mib = lambda: 9000 self.assertEqual(self.chat()[0], 200) def test_min_free_vram_unreadable_loads(self): self.svc.min_free_vram_mib = 8000 self.svc.free_vram_mib = lambda: None # no NVML: never refuse self.svc.unload() self.assertEqual(self.chat()[0], 200) def test_before_load_runs_first(self): mark = Path(tempfile.mkdtemp()) / "ran" self.svc.before_load = [sys.executable, "-c", f"open({str(mark)!r}, 'w').close()"] self.svc.unload() self.assertFalse(mark.exists()) self.assertEqual(self.chat()[0], 200) self.assertTrue(mark.exists()) def test_vision_encoder_unloads_and_starts_first(self): order = [] class FakeVision: running = True def alive(self): return self.running def unload(self): self.running = False def restart(self): order.append("vision") self.running = True engine_restart = self.engine.restart self.engine.restart = lambda: (order.append("engine"), engine_restart()) self.svc.vision = FakeVision() self.assertEqual(self.svc.unload(), "unloaded") self.assertFalse(self.svc.vision.alive()) self.assertEqual(self.chat()[0], 200) self.assertEqual(order, ["vision", "engine"]) # the encoder first, as at a start self.assertTrue(self.svc.vision.alive()) def test_off_by_default(self): self.assertEqual((self.svc.idle_unload_s, self.svc.min_free_vram_mib, self.svc.before_load), (0, 0, None)) self.assertEqual(self.req("/health")[1]["loaded"], True) class ThinkingEngine(MockEngine): """Thinks THOUGHT, then answers; a prompt that already ends its thinking (the budget's wrap-up) gets the answer at once, the way the model continues after . Records every prompt it is given.""" THOUGHT = "Let me think step by step about two plus two. " * 4 # 184 reasoning tokens (one per byte) ANSWER = "The answer is 4." def __init__(self, tok): super().__init__(tok, "x", max_context=CTX) self.prompts = [] def generate(self, ids, max_new, sampling, cancel, embeddings=None): self.prompts.append(list(ids)) done = self.tok.decode(ids).endswith("\n\n") text = self.ANSWER if done else self.THOUGHT + "\n\n" + self.ANSWER for t in (self.tok.encode(text) + self.tok.encode("<|im_end|>", parse_special=True))[:max_new]: if cancel.is_set(): return yield t class ThinkingBudget(unittest.TestCase): """#123: reasoning_budget_tokens (opt-in): at the budget the thinking is wrapped up and the model answers, continuing from the prompt plus what it generated plus the wrap-up (a prefix the engine already holds).""" def setUp(self): self.tok = ByteTokenizer() self.engine = ThinkingEngine(self.tok) self.svc = Service(self.engine, self.tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) self.httpd = serve(self.svc, port=0) self.base = f"http://127.0.0.1:{self.httpd.server_address[1]}" def tearDown(self): self.httpd.shutdown() self.httpd.server_close() def post(self, path, body): req = urllib.request.Request(self.base + path, data=json.dumps(body).encode(), headers={ "Content-Type": "application/json", "anthropic-version": "2023-06-01"}) try: with urllib.request.urlopen(req, timeout=30) as r: raw = r.read().decode() return r.status, (json.loads(raw) if not body.get("stream") else raw) except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()) def openai(self, **extra): return self.post("/v1/chat/completions", {"model": "m", "messages": [{"role": "user", "content": "2+2?"}], "max_tokens": 400, **extra}) def test_off_by_default(self): code, b = self.openai() self.assertEqual(code, 200, b) msg = b["choices"][0]["message"] self.assertEqual((msg["reasoning_content"], msg["content"]), (ThinkingEngine.THOUGHT, ThinkingEngine.ANSWER)) self.assertEqual(len(self.engine.prompts), 1) def test_the_budget_wraps_up_the_thinking(self): from serve.server import REASONING_WRAP_UP code, b = self.openai(reasoning_budget_tokens=20) self.assertEqual(code, 200, b) msg = b["choices"][0]["message"] wrap = REASONING_WRAP_UP.split("")[0] self.assertEqual(msg["reasoning_content"], ThinkingEngine.THOUGHT[:20] + wrap) self.assertEqual(msg["content"], ThinkingEngine.ANSWER) self.assertEqual(b["choices"][0]["finish_reason"], "stop") first, second = self.engine.prompts extra = self.tok.encode(REASONING_WRAP_UP, parse_special=True) self.assertEqual(second, first + self.tok.encode(ThinkingEngine.THOUGHT[:20]) + extra) # a prefix + more self.assertEqual(b["usage"]["completion_tokens"], 20 + len(extra) + len(ThinkingEngine.ANSWER) + 1) self.assertEqual(b["usage"]["prompt_tokens"], len(first)) def test_a_stop_string_is_found_in_the_answer_after_the_wrap_up(self): """#454: the continuation after the thinking budget is cut at a stop string too.""" code, b = self.openai(reasoning_budget_tokens=20, stop=["answer is"]) self.assertEqual(code, 200, b) msg = b["choices"][0]["message"] self.assertEqual((msg["content"], b["choices"][0]["finish_reason"]), ("The ", "stop")) def test_anthropic_stream(self): from serve.server import REASONING_WRAP_UP code, raw = self.post("/v1/messages", {"model": "m", "max_tokens": 400, "stream": True, "reasoning_budget_tokens": 30, "messages": [{"role": "user", "content": "2+2?"}]}) self.assertEqual(code, 200) evs = [json.loads(line[6:]) for line in raw.splitlines() if line.startswith("data: {")] thinking = "".join(e["delta"].get("thinking", "") for e in evs if e["type"] == "content_block_delta") text = "".join(e["delta"].get("text", "") for e in evs if e["type"] == "content_block_delta") self.assertEqual(thinking, ThinkingEngine.THOUGHT[:30] + REASONING_WRAP_UP.split("")[0]) self.assertEqual(text, ThinkingEngine.ANSWER) self.assertEqual(evs[-2]["delta"]["stop_reason"], "end_turn") def test_a_budget_the_thinking_stays_under(self): code, b = self.openai(reasoning_budget_tokens=10_000) self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT) self.assertEqual(len(self.engine.prompts), 1) def test_the_config_default_and_a_request_that_turns_it_off(self): self.svc.reasoning_budget_tokens = 20 code, b = self.openai() self.assertEqual(len(self.engine.prompts), 2) self.assertTrue(b["choices"][0]["message"]["reasoning_content"].startswith(ThinkingEngine.THOUGHT[:20] + "\n")) code, b = self.openai(reasoning_budget_tokens=0) self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT) self.assertEqual(len(self.engine.prompts), 3) def test_without_thinking_there_is_nothing_to_limit(self): self.engine.THOUGHT = "" code, b = self.openai(reasoning_budget_tokens=5, reasoning_effort="none") self.assertEqual(code, 200, b) self.assertEqual(len(self.engine.prompts), 1) def test_no_room_left_to_answer(self): code, b = self.openai(reasoning_budget_tokens=20, max_tokens=40) # 20 thought + the wrap-up > 40 self.assertEqual(b["choices"][0]["finish_reason"], "length") self.assertEqual(len(self.engine.prompts), 1) self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT[:20]) def test_a_reply_cut_while_thinking_is_named_in_the_log(self): """#530: max tokens reached inside the thinking gives an empty answer; the server log says what helps.""" hint = "reached max tokens while still thinking" for extra, said in (({"max_tokens": 10}, True), ({}, False)): with self.subTest(extra=extra): out = io.StringIO() with contextlib.redirect_stdout(out): code, b = self.openai(**extra) self.assertEqual(code, 200, b) self.assertEqual(b["choices"][0]["finish_reason"], "length" if said else "stop") self.assertEqual(hint in out.getvalue(), said, out.getvalue()) if said: self.assertIn("reasoning_budget_tokens", out.getvalue()) def test_a_bad_value_is_a_400(self): for bad in ("lots", 2.5, True, [1]): with self.subTest(value=bad): code, b = self.openai(reasoning_budget_tokens=bad) self.assertEqual(code, 400) self.assertIn("reasoning_budget_tokens", b["error"]["message"]) self.assertEqual(self.engine.prompts, []) class StatusHandover(unittest.TestCase): """#266: a stream aborted mid-way and the next request, which was waiting for the fifo. The aborted request's status/history block ran after the fifo was released, so the waiting request could start in that gap: the old request then recorded the NEW request's status as its own, set busy=False and popped `tail`, and the new request crashed in _note (KeyError 'tail'). The fifo below lets the waiting request run to its first token as soon as it is released, before the releasing thread goes on - the worst case of that gap, every time.""" def test_abort_then_the_next_request(self): tok = ByteTokenizer() second_running = threading.Event() class Engine(MockEngine): calls = 0 def generate(self, ids, max_new, sampling, cancel, embeddings=None): Engine.calls += 1 me = Engine.calls for i, t in enumerate(super().generate(ids, max_new, sampling, cancel, embeddings)): if me == 2 and i == 2: second_running.set() # the second request has its status and two tokens noted yield t class SlowRelease: """A Lock whose release waits (briefly) until the thread it let in has started generating.""" def __init__(self): self.lock, self.armed = threading.Lock(), False def acquire(self, blocking=True): return self.lock.acquire(blocking) def __enter__(self): self.lock.acquire() def __exit__(self, *exc): self.lock.release() if self.armed: self.armed = False second_running.wait(5) def release(self): self.lock.release() svc = Service(Engine(tok, "\n\n" + "y" * 40, max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) svc.fifo = SlowRelease() ids = tok.encode("hi") first = svc.run(ids, True, None, 30, {}, threading.Event()) for _ in range(5): next(first) # mid-answer out, errors = [], [] def second(): try: out.extend(svc.run(ids, True, None, 20, {}, threading.Event())) except Exception as e: # noqa: BLE001 - the crash this test is about errors.append(e) waiter = threading.Thread(target=second) waiter.start() deadline = time.time() + 5 while svc.status.get("queued") != 1 and time.time() < deadline: time.sleep(0.005) self.assertEqual(svc.status.get("queued"), 1, "the second request never queued") svc.fifo.armed = True first.close() # the client went away: GeneratorExit in the first request waiter.join(10) self.assertFalse(waiter.is_alive()) self.assertEqual(errors, []) self.assertEqual(out[-1][0], "done") self.assertEqual(out[-1][1]["completion_tokens"], 20) rows = list(svc.history) self.assertEqual([r["finish"] for r in rows], ["disconnect", "length"]) self.assertTrue(0 < rows[0]["output_tokens"] < 30, rows) # where the first one stopped, not the second's self.assertEqual(rows[1]["output_tokens"], 20) self.assertEqual(svc.totals["requests"], 2) self.assertEqual(svc.totals["output_tokens"], rows[0]["output_tokens"] + 20) self.assertFalse(svc.status["busy"]) self.assertNotIn("tail", svc.status) FAKE_STRATA = '''import pathlib, sys, time gate = pathlib.Path(sys.argv[sys.argv.index("--gate") + 1]) print("INFO engine=0.0.0", flush=True) while not gate.exists(): # the test says when the engine is "ready" time.sleep(0.01) print("READY 4096 stop", flush=True) for line in sys.stdin: if line.startswith("QUIT"): break ''' FAKE_STRATA_FAIL_ONCE = '''import pathlib, sys, time fail = pathlib.Path(sys.argv[sys.argv.index("--fail") + 1]) if fail.exists(): # this start fails before READY (as one next to a dying engine did) fail.unlink() sys.exit(1) ''' + FAKE_STRATA.split("\n", 1)[1] class RestartWindow(unittest.TestCase): """#344: while the engine restarts it is not alive (a request waits for the restart instead of reading max_context 0), and a request that still meets max_context 0 gets a 503 "starting", not a 400 about its prompt.""" def test_not_alive_until_ready(self): from unittest import mock import serve.server as server with tempfile.TemporaryDirectory() as d: script, gate = Path(d) / "fake_strata.py", Path(d) / "ready" script.write_text(FAKE_STRATA, encoding="utf-8") real = server.subprocess.Popen with mock.patch.object(server.subprocess, "Popen", lambda cmd, **kw: real([sys.executable, str(script), *cmd[1:]], **kw)): gate.touch() eng = StrataEngine("strata", ["--gate", str(gate)]) try: self.assertEqual(eng.max_context, 4096) self.assertTrue(eng.alive()) gate.unlink() old = eng.proc old.kill() old.wait(10) deadline = time.time() + 10 while not getattr(eng, "ended", False) and time.time() < deadline: time.sleep(0.01) # the server has noticed: its output closed self.assertFalse(eng.alive()) t = threading.Thread(target=eng.restart) t.start() deadline = time.time() + 10 while eng.proc is old and time.time() < deadline: time.sleep(0.005) self.assertIsNot(eng.proc, old, "restart never started the engine") time.sleep(0.2) # the new engine is up, but has not said READY self.assertEqual(eng.max_context, 0) self.assertFalse(eng.alive(), "alive before READY: a request would plan with context 0") gate.touch() t.join(10) self.assertFalse(t.is_alive()) self.assertTrue(eng.alive()) self.assertEqual(eng.max_context, 4096) # restart() of a running engine kills it first: that engine's output thread ends after the new # one is up, and must not mark it dead (it did: every request after restarted it again) eng.restart() time.sleep(0.5) self.assertTrue(eng.alive()) self.assertEqual(eng.max_context, 4096) finally: gate.touch() eng.unload() def test_restart_retries_a_start_that_fails(self): """A dead engine's VRAM is freed only when its process is gone, so a new engine started at once can exit before READY; restart() tries again (3 times) instead of leaving the server with max_context 0.""" from unittest import mock import serve.server as server with tempfile.TemporaryDirectory() as d: script, gate, fail = Path(d) / "fake_strata.py", Path(d) / "ready", Path(d) / "fail_once" script.write_text(FAKE_STRATA_FAIL_ONCE, encoding="utf-8") real = server.subprocess.Popen with mock.patch.object(server.subprocess, "Popen", lambda cmd, **kw: real([sys.executable, str(script), *cmd[1:]], **kw)), \ mock.patch.object(StrataEngine, "RESTART_RETRY_S", 0.0): gate.touch() eng = StrataEngine("strata", ["--gate", str(gate), "--fail", str(fail)]) try: self.assertEqual(eng.max_context, 4096) self.assertEqual(eng.known_ctx, 4096) fail.touch() # the next start exits before READY, the one after works eng.proc.kill() eng.proc.wait(10) eng.restart() self.assertFalse(fail.exists(), "the failing start never ran") self.assertTrue(eng.alive()) self.assertEqual(eng.max_context, 4096) self.assertFalse(eng.starting) finally: gate.touch() eng.unload() def test_failed_restart_keeps_the_known_context(self): """After a restart that failed, max_context is 0 but the engine is not starting: a request is checked against the last known context and reaches run() (which starts the engine again), not a 400 about context 0.""" tok = ByteTokenizer() eng = MockEngine(tok, "\n\nok", max_context=0) eng.known_ctx, eng.starting = 4096, False svc = Service(eng, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: req = urllib.request.Request(base + "/v1/chat/completions", data=json.dumps({"model": "m", "max_tokens": 16, "messages": [{"role": "user", "content": "hi"}]}).encode(), headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=30) as r: self.assertEqual(r.status, 200) finally: httpd.shutdown() def test_context_zero_is_503(self): tok = ByteTokenizer() svc = Service(MockEngine(tok, "\n\nok", max_context=0), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" try: for path, body in (("/v1/chat/completions", {"model": "m", "messages": [{"role": "user", "content": "hi"}], "max_tokens": 16}), ("/v1/messages", {"model": "m", "max_tokens": 16, "messages": [{"role": "user", "content": "hi"}]})): req = urllib.request.Request(base + path, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) with self.assertRaises(urllib.error.HTTPError) as cm: urllib.request.urlopen(req, timeout=30) with cm.exception as e: self.assertEqual(e.code, 503, path) text = e.read().decode() self.assertIn("starting", text) self.assertNotIn("leaves no room", text) finally: httpd.shutdown() httpd.server_close() class ModelAliases(unittest.TestCase): """#297: the config's `aliases` are listed by /v1/models and accepted as model names (answered under that name).""" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.svc = Service(MockEngine(tok, "\n\nok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), model_name="qwen3.8-flash-next-iq3_xxs") cls.svc.set_aliases(["qwen", "local-model", "qwen", " ", "qwen3.8-flash-next-iq3_xxs"]) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def get(self, path): with urllib.request.urlopen(self.base + path, timeout=30) as r: return json.loads(r.read()) def post(self, path, body): req = urllib.request.Request(self.base + path, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=30) as r: return r.read().decode() def test_set_aliases(self): self.assertEqual(self.svc.aliases, ["qwen", "local-model"]) # duplicates, blanks and the name itself tok = ByteTokenizer() svc = Service(MockEngine(tok, "x", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) svc.set_aliases("a, b") self.assertEqual(svc.aliases, ["a", "b"]) svc.set_aliases(None) self.assertEqual(svc.aliases, []) for bad in (3, ["a", 1], {"a": 1}): with self.assertRaises(ValueError): svc.set_aliases(bad) def test_models_lists_them(self): data = self.get("/v1/models")["data"] self.assertEqual([m["id"] for m in data], ["qwen3.8-flash-next-iq3_xxs", "qwen", "local-model"]) self.assertEqual(data[0]["aliases"], ["qwen", "local-model"]) self.assertEqual(data[1]["alias_of"], "qwen3.8-flash-next-iq3_xxs") self.assertEqual(self.get("/props?model=local-model")["model_alias"], "qwen3.8-flash-next-iq3_xxs") def test_requests_are_answered_under_the_alias(self): msgs = [{"role": "user", "content": "hi"}] for asked, want in (("qwen", "qwen"), ("local-model", "local-model"), ("qwen3.8-flash-next-iq3_xxs", "qwen3.8-flash-next-iq3_xxs"), ("something-else", "qwen3.8-flash-next-iq3_xxs")): # still served, as before with self.subTest(asked=asked): out = json.loads(self.post("/v1/chat/completions", {"model": asked, "messages": msgs, "max_tokens": 8})) self.assertEqual(out["model"], want) text = self.post("/v1/chat/completions", {"model": asked, "messages": msgs, "max_tokens": 8, "stream": True}) first = json.loads(text.split("data: ", 2)[1].strip()) self.assertEqual(first["model"], want) out = json.loads(self.post("/v1/messages", {"model": asked, "messages": msgs, "max_tokens": 8})) self.assertEqual(out["model"], want) def test_without_aliases_nothing_changes(self): tok = ByteTokenizer() svc = Service(MockEngine(tok, "x", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) try: with urllib.request.urlopen(f"http://127.0.0.1:{httpd.server_address[1]}/v1/models", timeout=30) as r: data = json.loads(r.read())["data"] self.assertEqual(len(data), 1) self.assertNotIn("aliases", data[0]) self.assertEqual(svc.model_for({"model": "x"}), svc.model) finally: httpd.shutdown() httpd.server_close() class AmdTelemetry(unittest.TestCase): """#301: the AMD backend's readings from a fake amdgpu sysfs tree: KFD node -> render node, as setup numbers the cards (the CPU node skipped), and free_vram_mib on HIP.""" def tree(self, d): nodes = Path(d) / "class/kfd/kfd/topology/nodes" for n, props in ((0, "cpu_cores_count 16\nsimd_count 0\ngfx_target_version 0\ndrm_render_minor 0\n"), (1, "simd_count 128\ngfx_target_version 120001\ndrm_render_minor 129\n"), (2, "simd_count 128\ngfx_target_version 120001\ndrm_render_minor 128\n")): (nodes / str(n)).mkdir(parents=True) (nodes / str(n) / "properties").write_text(props) for minor, used, busy, temp, power in ((129, 2 << 30, 37, 51000, 85000000), (128, 6 << 30, 99, 64000, None)): dev = Path(d) / f"class/drm/renderD{minor}/device" hw = dev / "hwmon" / "hwmon4" hw.mkdir(parents=True) (dev / "gpu_busy_percent").write_text(f"{busy}\n") (dev / "mem_info_vram_used").write_text(f"{used}\n") (dev / "mem_info_vram_total").write_text(f"{32 << 30}\n") (dev / "product_name").write_text("AMD Radeon AI PRO R9700\n") (hw / "temp1_input").write_text(f"{temp}\n") if power is not None: (hw / "power1_average").write_text(f"{power}\n") else: (hw / "power1_input").write_text("120000000\n") (hw / "power1_cap").write_text("300000000\n") def test_readings(self): from serve import telemetry with tempfile.TemporaryDirectory() as d: self.tree(d) with mock.patch.object(telemetry, "SYSFS", d): self.assertTrue(telemetry.amd_device_dir(0).endswith(os.path.join("renderD129", "device"))) self.assertTrue(telemetry.amd_device_dir(1).endswith(os.path.join("renderD128", "device"))) self.assertIsNone(telemetry.amd_device_dir(2)) g = telemetry.gpu_reader(0, amd=True) self.assertTrue(g.ok()) self.assertEqual(g.name(), "AMD Radeon AI PRO R9700") self.assertEqual(g.read(), {"util": 37, "mem_used": 2 << 30, "mem_total": 32 << 30, "temp": 51.0, "power": 85.0, "power_limit": 300.0}) r = telemetry.gpu_reader(1, amd=True).read() self.assertEqual((r["util"], r["temp"], r["power"]), (99, 64.0, 120.0)) # power1_input self.assertEqual(telemetry.free_vram_mib(0, amd=True), 30 << 10) self.assertIsNone(telemetry.free_vram_mib(5, amd=True)) t = telemetry.Telemetry(gpu_index=0, gpu_indices=[0, 1], amd=True) s = t.sample() self.assertEqual(t.static["gpu_name"], "AMD Radeon AI PRO R9700 + AMD Radeon AI PRO R9700") self.assertEqual((s["gpu_mem_used"], s["gpu_util"], s["gpu_temp"], s["gpu_power"]), (8 << 30, 68.0, 64.0, 205.0)) with tempfile.TemporaryDirectory() as d: # no amdgpu: nothing, and nothing breaks with mock.patch.object(telemetry, "SYSFS", d): self.assertFalse(telemetry.gpu_reader(0, amd=True).ok()) self.assertIsNone(telemetry.free_vram_mib(0, amd=True)) def test_free_vram_on_hip(self): from serve import telemetry tok = ByteTokenizer() svc = Service(MockEngine(tok, "x", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) svc.backend, svc.gpu_index = "hip", 1 with tempfile.TemporaryDirectory() as d: self.tree(d) with mock.patch.object(telemetry, "SYSFS", d): self.assertEqual(svc.free_vram_mib(), 26 << 10) class SilentEngine(unittest.TestCase): """#481: an engine that prints nothing for engine_silence_s during a request (or never acknowledges a STOP) has lost step with the server: it is ended and the request fails with EngineDied, instead of waiting forever.""" def bare(self, silence, can_stop=False): import io import queue engine = StrataEngine.__new__(StrataEngine) engine.proc = mock.Mock() engine.proc.stdin = io.StringIO() engine.proc.poll.return_value = None engine.lines, engine.can_stop, engine.max_context = queue.Queue(), can_stop, 4096 engine.silence_s, engine.log_path = silence, None return engine def later(self, engine, delay, *lines): def put(): time.sleep(delay) for x in lines: engine.lines.put(x) threading.Thread(target=put, daemon=True).start() def test_silence_mid_answer_ends_the_engine(self): from serve.server import EngineSilent engine = self.bare(0.3) engine.lines.put("T 5") gen = engine.generate([1], 10, {}, threading.Event()) self.assertEqual(next(gen), 5) t0 = time.monotonic() with self.assertRaises(EngineSilent) as cm: next(gen) self.assertIsInstance(cm.exception, EngineDied) # every EngineDied path handles it self.assertLess(time.monotonic() - t0, 5) engine.proc.kill.assert_called_once() self.assertFalse(engine.alive()) # the next request restarts it self.assertIn("#481", engine.death_note()) self.assertNotIn("STOP", engine.proc.stdin.getvalue()) # nothing is listening: no STOP, no drain def test_prompt_chunks_set_the_wait(self): # a PP line every second, at 100 tok/s: far over a 0.3 s silence, but each chunk is on time for its size engine = self.bare(0.3) engine.lines.put("RESUME 0") engine.lines.put("PP 100 300 1000 100.0") self.later(engine, 1.0, "PP 200 300 2000 100.0", "T 7", "DONE 1 300 2000 1 length") self.assertEqual(list(engine.generate([1], 10, {}, threading.Event())), [None, None, 7]) engine.proc.kill.assert_not_called() def test_a_long_first_chunk_is_allowed(self): # 100 prompt tokens at the slowest prompt reading (50 tok/s): 2 s on top of the silence before the first PP engine = self.bare(0.3) self.later(engine, 1.0, "PP 100 100 1000 100.0", "DONE 0 100 1000 0 length") self.assertEqual(list(engine.generate([1] * 100, 10, {}, threading.Event())), [None]) engine.proc.kill.assert_not_called() def test_a_stop_never_acknowledged(self): from serve.server import EngineSilent engine = self.bare(0.3, can_stop=True) engine.lines.put("T 5") gen = engine.generate([1], 10, {}, threading.Event()) self.assertEqual(next(gen), 5) with self.assertRaises(EngineSilent): gen.close() # the consumer stopped: STOP, then the drain self.assertIn("STOP", engine.proc.stdin.getvalue()) engine.proc.kill.assert_called_once() self.assertFalse(engine.alive()) def test_zero_waits_as_before(self): engine = self.bare(0) self.later(engine, 0.5, "T 5", "DONE 1 1 1 1 length") self.assertEqual(list(engine.generate([1], 10, {}, threading.Event())), [5]) def test_config(self): from serve.server import ENGINE_SILENCE_S, engine_silence_s self.assertEqual(engine_silence_s({}), ENGINE_SILENCE_S) self.assertEqual(engine_silence_s({"engine_silence_s": 0}), 0.0) self.assertEqual(engine_silence_s({"engine_silence_s": 900}), 900.0) for bad in (-1, "300", True): with self.assertRaises(ValueError): engine_silence_s({"engine_silence_s": bad}) FAKE_LOST_STEP = '''import pathlib, sys, time mark = pathlib.Path(sys.argv[sys.argv.index("--mark") + 1]) mode = sys.argv[sys.argv.index("--mode") + 1] print("READY 4096 stop", flush=True) for line in sys.stdin: if line.startswith("QUIT"): break if line.startswith("GEN"): if not mark.exists(): # the first engine loses step (#481): it never says DONE mark.touch() print("T 104", flush=True) if mode == "stop": print("T 257", flush=True) # <|im_end|>: the server STOPs and drains, and the engine is silent time.sleep(3600) print("T 111", flush=True) print("T 107", flush=True) print("DONE 2 5 1.0 1.0 length", flush=True) ''' class LostStep(unittest.TestCase): """#481 over HTTP, with a real process: the request with the silent engine ends with an error, and the next one starts the engine again and is answered (the server used to wait forever, holding the request FIFO).""" def run_mode(self, mode, stream): import serve.server as server with tempfile.TemporaryDirectory() as d: script, mark = Path(d) / "fake_strata.py", Path(d) / "lost" script.write_text(FAKE_LOST_STEP, encoding="utf-8") real = server.subprocess.Popen with mock.patch.object(server.subprocess, "Popen", lambda cmd, **kw: real([sys.executable, str(script), *cmd[1:]], **kw)): eng = StrataEngine("strata", ["--mark", str(mark), "--mode", mode]) eng.silence_s = 0.5 tok = ByteTokenizer() svc = Service(eng, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) httpd = serve(svc, port=0) base = f"http://127.0.0.1:{httpd.server_address[1]}" body = {"model": "m", "max_tokens": 2, "reasoning_effort": "none", "stream": stream, "messages": [{"role": "user", "content": "hi"}]} try: first = eng.proc req = urllib.request.Request(base + "/v1/chat/completions", data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) try: with urllib.request.urlopen(req, timeout=60) as r: text = r.read().decode() except urllib.error.HTTPError as e: self.assertEqual(e.code, 503) text = e.read().decode() self.assertIn("the next request restarts it", text) self.assertIsNotNone(first.poll(), "the silent engine still runs") self.assertEqual(svc.history[-1]["finish"], "error") with urllib.request.urlopen(req, timeout=60) as r: text = r.read().decode() self.assertIsNot(eng.proc, first) if stream: answer = "".join(json.loads(x[6:])["choices"][0]["delta"].get("content") or "" for x in text.splitlines() if x.startswith("data: {") and "choices" in x) else: answer = json.loads(text)["choices"][0]["message"]["content"] self.assertEqual(answer, "ok") finally: httpd.shutdown() httpd.server_close() eng.unload() def test_silent_mid_answer(self): self.run_mode("silent", stream=False) def test_silent_mid_stream(self): self.run_mode("silent", stream=True) def test_stop_never_acknowledged(self): self.run_mode("stop", stream=False) class ImageSources(unittest.TestCase): """What an image source may name: never a network path (Windows signs in to the host at the first look), a URL up to IMAGE_URL_MAX and downloaded outside the request FIFO, a file on this computer not for a page of another site.""" NETWORK = [r"\\host\share\x.png", "//host/share/x.png", r"\\?\UNC\host\share\x.png", r"\\.\UNC\host\share\x.png", r"\??\UNC\host\share\x.png", r"/\host\share\x.png", "file://host/share/x.png", "file:////host/share/x.png", r"file://\\host\share\x.png"] class Vision(ImageMarkers.FakeVision): def __init__(self, d): super().__init__(d) self.got = [] def encode(self, source): self.got.append(source) return super().encode(source) def test_network_paths_are_refused_before_any_look(self): from serve.server import Vision with mock.patch("os.path.isfile") as isfile, mock.patch.object(Path, "read_bytes") as read: for src in self.NETWORK: with self.subTest(src=src), self.assertRaisesRegex(ValueError, "network paths"): Vision.load(src) isfile.assert_not_called() read.assert_not_called() with tempfile.TemporaryDirectory() as d: # files on this computer load as before f = Path(d) / "x.png" f.write_bytes(b"\x89PNG\r\n\x1a\n") for src in (str(f), "file://" + str(f), "data:image/png;base64,iVBORw0KGgo="): with self.subTest(src=src): self.assertEqual(Vision.load(src), b"\x89PNG\r\n\x1a\n") def test_unreadable_sources_are_a_value_error(self): """From #582: a bad data: URL or an unreadable file is a 400, not a dropped connection.""" from serve.server import Vision for src in ("data:image/png", "data:image/png;base64,abcde"): with self.subTest(src=src), self.assertRaisesRegex(ValueError, "data: URL could not be read"): Vision.load(src) with tempfile.TemporaryDirectory() as d: f = Path(d) / "x.png" f.write_bytes(b"x") with mock.patch.object(Path, "read_bytes", side_effect=PermissionError("denied")), self.assertRaisesRegex(ValueError, "could not be read from"): Vision.load(str(f)) def test_a_url_is_read_up_to_the_cap(self): import http.server import serve.server as server class Files(http.server.BaseHTTPRequestHandler): def log_message(self, *args): pass def do_GET(self): body = {"/small": b"x" * 100, "/big": b"x" * 5000}.get(urllib.parse.urlsplit(self.path).path) if body is None: self.send_error(404) return self.send_response(200) if "length" in self.path: # else the body ends when the connection does self.send_header("Content-Length", str(len(body))) self.end_headers() try: self.wfile.write(body) except OSError: # the reader stopped at the cap pass httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Files) threading.Thread(target=httpd.serve_forever, daemon=True).start() base = f"http://127.0.0.1:{httpd.server_address[1]}" try: with mock.patch.object(server, "IMAGE_URL_MAX", 1000): for q in ("", "?length"): with self.subTest(q=q): self.assertEqual(server.Vision.load(base + "/small" + q), b"x" * 100) with self.assertRaisesRegex(ValueError, "is over"): server.Vision.load(base + "/big" + q) with self.assertRaisesRegex(ValueError, "could not be read: HTTP Error 404"): server.Vision.load(base + "/missing") finally: httpd.shutdown() httpd.server_close() def test_a_url_is_downloaded_outside_the_fifo(self): import serve.server as server tok = ByteTokenizer() with tempfile.TemporaryDirectory() as d: vision = self.Vision(d) svc = Service(MockEngine(tok, "ok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=vision) held = [] def download(url): held.append(svc.fifo.locked()) return b"the picture" msgs = [{"role": "user", "content": [{"type": "image", "source": "https://example.com/x.png"}, {"type": "image", "source": "x.png"}]}] with mock.patch.object(server.Vision, "download", side_effect=download): svc.prepare(msgs, None, {}) self.assertEqual(held, [False]) # not while every other request waits self.assertEqual(vision.got, [b"the picture", "x.png"]) svc.embeddings.path.unlink(missing_ok=True) def test_a_page_of_another_site_cannot_name_a_file(self): tok = ByteTokenizer() with tempfile.TemporaryDirectory() as d: vision = self.Vision(d) svc = Service(MockEngine(tok, "\n\nok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=vision) httpd = serve(svc, port=0) host = f"127.0.0.1:{httpd.server_address[1]}" def post(path, src, headers): image = {"type": "image_url", "image_url": {"url": src}} if path.endswith("completions") else \ {"type": "image", "source": {"type": "url", "url": src}} body = {"model": "m", "max_tokens": 8, "messages": [{"role": "user", "content": [ {"type": "text", "text": "what is it?"}, image]}]} req = urllib.request.Request(f"http://{host}{path}", data=json.dumps(body).encode(), headers=headers) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, json.loads(r.read()) except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()) try: f = str(Path(d) / "secret.png") # a page on any site, no api_key (the default): text/plain needs no CORS preflight - 0.1.38 refuses # such a page outright (403) page = {"Content-Type": "text/plain;charset=UTF-8", "Origin": "https://evil.example"} status, b = post("/v1/chat/completions", f, page) self.assertEqual(status, 403, b) # a page cors_origins lets in (here every page) may send JSON, and still cannot name a file svc.cors_origins = ["*"] page = {"Content-Type": "application/json", "Origin": "https://evil.example"} for path in ("/v1/chat/completions", "/v1/messages"): for src in (f, "file://" + f): with self.subTest(path=path, src=src): status, b = post(path, src, page) self.assertEqual(status, 400, b) self.assertIn("another origin", b["error"]["message"]) self.assertEqual(vision.got, []) # nothing was read svc.trusted_origins = ["https://app.example"] data = "data:image/png;base64,iVBORw0KGgo=" for src, headers in ((data, page), # the page's own picture (f, {"Content-Type": "application/json"}), # curl, SDKs, agents (f, {"Content-Type": "application/json", "Origin": "http://" + host}), (f, {"Content-Type": "application/json", "Origin": "https://app.example"})): with self.subTest(src=src[:20], headers=headers): status, b = post("/v1/chat/completions", src, headers) self.assertEqual(status, 200, b) self.assertEqual(vision.got, [data, f, f, f]) finally: httpd.shutdown() httpd.server_close() class CountingEngine(MockEngine): """Counts the tokens the engine was asked for, so a test can see it stopped early.""" def generate(self, ids, max_new, sampling, cancel, embeddings=None): self.yielded = 0 for t in super().generate(ids, max_new, sampling, cancel, embeddings): self.yielded += 1 yield t class StopStrings(unittest.TestCase): """OpenAI `stop` and Anthropic `stop_sequences`: the answer ends before the first stop string, which is not sent, and the engine stops there. The byte tokenizer gives one token per byte, so every stop string here is split across tokens (and across streamed chunks).""" ANSWER = "alpha END beta STOP gamma" @classmethod def setUpClass(cls): tok = ByteTokenizer() cls.engine = CountingEngine(tok, "\n\n" + cls.ANSWER, max_context=CTX) cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) cls.httpd = serve(cls.svc, port=0) cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}" @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def post(self, api, stream, **extra): """-> (status, text, finish or stop_reason, stop_sequence, the streamed text pieces)""" path = "/v1/chat/completions" if api == "openai" else "/v1/messages" body = {"model": "x", "max_tokens": 200, "stream": stream, "messages": [{"role": "user", "content": "hi"}], **extra} req = urllib.request.Request(self.base + path, data=json.dumps(body).encode(), headers={ "Content-Type": "application/json", "anthropic-version": "2023-06-01"}) try: with urllib.request.urlopen(req, timeout=30) as r: raw = r.read().decode() except urllib.error.HTTPError as e: with e: return e.code, json.loads(e.read()), None, None, None if not stream: b = json.loads(raw) if api == "openai": c = b["choices"][0] return 200, c["message"]["content"] or "", c["finish_reason"], None, None text = "".join(x.get("text", "") for x in b["content"] if x["type"] == "text") return 200, text, b["stop_reason"], b["stop_sequence"], None evs = [json.loads(line[6:]) for line in raw.splitlines() if line.startswith("data: {")] if api == "openai": pieces = [e["choices"][0]["delta"].get("content") or "" for e in evs] return 200, "".join(pieces), evs[-1]["choices"][0]["finish_reason"], None, [p for p in pieces if p] pieces = [e["delta"]["text"] for e in evs if e["type"] == "content_block_delta" and e["delta"]["type"] == "text_delta"] delta = [e for e in evs if e["type"] == "message_delta"][0]["delta"] return 200, "".join(pieces), delta["stop_reason"], delta["stop_sequence"], pieces def stop_field(self, api, stops): return {"stop": stops} if api == "openai" else {"stop_sequences": stops} def test_cut_at_the_stop_string(self): for api in ("openai", "anthropic"): for stream in (False, True): with self.subTest(api=api, stream=stream): code, text, finish, seq, pieces = self.post(api, stream, **self.stop_field(api, ["END"])) self.assertEqual(code, 200, text) self.assertEqual(text, "alpha ") if api == "openai": self.assertEqual(finish, "stop") else: self.assertEqual((finish, seq), ("stop_sequence", "END")) if stream: self.assertFalse(any("E" in p for p in pieces), pieces) # no part of it was sent # the engine stopped at the stop string, it did not run to the end of the answer self.assertLess(self.engine.yielded, len("\n\n" + self.ANSWER)) def test_the_first_of_several(self): for api in ("openai", "anthropic"): for stream in (False, True): with self.subTest(api=api, stream=stream): _, text, finish, seq, _ = self.post(api, stream, **self.stop_field(api, ["STOP", "beta", "zzz"])) self.assertEqual(text, "alpha END ") if api == "anthropic": self.assertEqual((finish, seq), ("stop_sequence", "beta")) def test_openai_takes_a_string(self): _, text, finish, _, _ = self.post("openai", False, stop="STOP") self.assertEqual((text, finish), ("alpha END beta ", "stop")) def test_a_prefix_that_is_not_a_stop_is_sent(self): """"ENDX" begins like the answer's "END" but is not in it: the held tail goes out and nothing is lost.""" for api in ("openai", "anthropic"): for stream in (False, True): with self.subTest(api=api, stream=stream): _, text, finish, seq, _ = self.post(api, stream, **self.stop_field(api, ["ENDX", "gammaZ"])) self.assertEqual(text, self.ANSWER) self.assertEqual(finish, "stop" if api == "openai" else "end_turn") self.assertIsNone(seq) def test_no_stop_and_an_empty_list(self): for api in ("openai", "anthropic"): for stream in (False, True): for extra in ({}, self.stop_field(api, []), self.stop_field(api, ["nowhere"])): with self.subTest(api=api, stream=stream, extra=extra): _, text, finish, seq, _ = self.post(api, stream, **extra) self.assertEqual(text, self.ANSWER) self.assertEqual(finish, "stop" if api == "openai" else "end_turn") self.assertIsNone(seq) def test_bad_values_are_a_400(self): for api, extra in (("openai", {"stop": ["a", "b", "c", "d", "e"]}), ("openai", {"stop": 5}), ("anthropic", {"stop_sequences": "END"}), ("anthropic", {"stop_sequences": [1]})): with self.subTest(api=api, extra=extra): code, *_ = self.post(api, False, **extra) self.assertEqual(code, 400) def test_matcher_holds_back_only_a_possible_prefix(self): from serve.server import StopMatcher m = StopMatcher([""]) self.assertEqual(m.push("a <"), "a ") # "<" may start "" self.assertEqual(m.push("b"), " d"), "") self.assertEqual(m.hit, "") self.assertEqual(m.push("more"), "") class AnswerBeforeTheBody(unittest.TestCase): """An answer sent before the request body was read must still reach a client that sends the body after the headers (http.client, urllib and requests do): closing the connection on unread bytes sends a reset that eats the answer.""" def setUp(self): tok = ByteTokenizer() self.engine = UnloadableEngine(tok, "\n\nok", max_context=CTX) self.svc = Service(self.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) self.httpd = serve(self.svc, port=0) self.port = self.httpd.server_address[1] def tearDown(self): self.httpd.shutdown() self.httpd.server_close() def status(self, method, path, headers=None, body=b'{"x": 1}'): """The status line of the answer to a request whose body follows the headers after a pause.""" head = {"Host": f"127.0.0.1:{self.port}", "Content-Type": "application/json", "Content-Length": str(len(body)), **(headers or {})} with socket.create_connection(("127.0.0.1", self.port), timeout=10) as s: s.sendall((f"{method} {path} HTTP/1.1\r\n" + "".join(f"{k}: {v}\r\n" for k, v in head.items()) + "\r\n").encode()) time.sleep(0.3) # the server answers (and, unfixed, closes) meanwhile try: s.sendall(body) except OSError: pass answer = b"" while chunk := s.recv(65536): # Windows raises a reset here, where Linux keeps the answer answer += chunk time.sleep(0.2) # Linux shows the reset only as a pending socket error, after the answer and the end of the stream self.assertEqual(s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR), 0, "the connection was reset") return answer.split(b"\r\n", 1)[0].decode() def test_a_wrong_key(self): self.svc.api_key = "secret" try: self.assertEqual(self.status("POST", "/v1/chat/completions"), "HTTP/1.0 401 Unauthorized") finally: self.svc.api_key = "" def test_a_wrong_key_and_a_body_of_megabytes(self): """An agent client's conversation, or one screenshot, is several MiB, and a rotated key is when the 401 matters.""" self.svc.api_key = "secret" try: self.assertEqual(self.status("POST", "/v1/chat/completions", body=b'{"x": "' + b"a" * (80 << 20) + b'"}'), "HTTP/1.0 401 Unauthorized") finally: self.svc.api_key = "" def test_a_body_that_comes_in_drops_does_not_hold_the_connection(self): """A client that announces a body and sends it a byte at a time is let go after DRAIN_SECONDS, with its answer: the time limit is on the whole body, not on each read.""" self.svc.api_key = "secret" try: with mock.patch.object(self.httpd.RequestHandlerClass, "DRAIN_SECONDS", 0.5), \ socket.create_connection(("127.0.0.1", self.port), timeout=10) as s: s.sendall((f"POST /v1/chat/completions HTTP/1.1\r\nHost: 127.0.0.1:{self.port}\r\n" f"Content-Length: {1 << 20}\r\n\r\n").encode()) started, answer = time.monotonic(), b"" s.settimeout(0.2) while True: try: if not (chunk := s.recv(65536)): break answer += chunk except TimeoutError: s.sendall(b"a") # a byte every 0.2 s keeps each read of the server alive self.assertEqual(answer.split(b"\r\n", 1)[0], b"HTTP/1.0 401 Unauthorized") self.assertLess(time.monotonic() - started, 5) finally: self.svc.api_key = "" def test_load_and_unload(self): self.assertEqual(self.status("POST", "/unload"), "HTTP/1.0 200 OK") self.assertEqual(self.status("POST", "/load"), "HTTP/1.0 200 OK") def test_not_the_apps_own_page(self): self.assertEqual(self.status("POST", "/load", {"Origin": "https://example.com"}), "HTTP/1.0 403 Forbidden") def test_a_body_a_handler_read_is_not_waited_for(self): """#594: /config and /load read their body themselves: the close must not wait for the drain's 5 s.""" for path in ("/config", "/load", "/unload"): with self.subTest(path=path): t0 = time.monotonic() self.status("POST", path) self.assertLess(time.monotonic() - t0, 1.5) # the status() helper itself pauses 0.5 s def test_a_host_the_server_does_not_answer_to(self): self.assertEqual(self.status("POST", "/v1/chat/completions", {"Host": "rebind.example.com"}), "HTTP/1.0 403 Forbidden") def test_a_method_with_no_handler(self): self.assertEqual(self.status("PUT", "/v1/chat/completions"), "HTTP/1.0 501 Unsupported method ('PUT')") if __name__ == "__main__": unittest.main()