"""Reasoning-effort tests: options must match what the template really accepts, and a client's own chat_template_kwargs must always win over the server default.""" import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import harness ROOT = harness.setup() from llmhost import paths, plan harness.guard(paths, ROOT) check = harness.Checks() BOTH = {"thinking": True, "effort": True} THINK = {"thinking": True, "effort": False} NONE = {"thinking": False, "effort": False} QWEN = {"name": "qwen3.8-27b", "reasoning": True, "effort": {"levels": ["low", "medium", "xhigh"], "default": "xhigh"}} ORNITH = {"name": "ornith-1.5-9b", "reasoning": True} CODER = {"name": "qwen3-coder", "reasoning": False} print("\n1. options follow the template, not the catalog alone") vals = lambda e, c: [v for v, _ in plan.reasoning_options(e, None, c)] check("full set when the template reads both", vals(QWEN, BOTH) == ["default", "xhigh", "medium", "low", "off"], vals(QWEN, BOTH)) check("most effort first", vals(QWEN, BOTH)[1] == "xhigh", vals(QWEN, BOTH)) check("levels dropped if the template ignores them", vals(QWEN, THINK) == ["default", "off"], vals(QWEN, THINK)) check("nothing at all if it reads neither", vals(QWEN, NONE) == [], vals(QWEN, NONE)) check("thinking-only model gets on/off", vals(ORNITH, THINK) == ["default", "off"], vals(ORNITH, THINK)) check("no levels invented for it", "low" not in vals(ORNITH, BOTH), vals(ORNITH, BOTH)) check("non-reasoning model gets no control", vals(CODER, BOTH) == [], vals(CODER, BOTH)) check("a lone 'default' is not a choice", vals({"name": "x", "reasoning": True}, NONE) == []) labels = dict(plan.reasoning_options(QWEN, None, BOTH)) check("xhigh reads as a word", labels["xhigh"] == "Extra high", labels) print("\n2. kwargs are only ever what the template accepts") kw = lambda v, e, c: plan.reasoning_kwargs(v, e, c) check("default sends nothing", kw("default", QWEN, BOTH) == {}) check("None sends nothing", kw(None, QWEN, BOTH) == {}) check("off -> enable_thinking false", kw("off", QWEN, BOTH) == {"enable_thinking": False}) check("level -> reasoning_effort", kw("low", QWEN, BOTH) == {"reasoning_effort": "low"}) check("xhigh passes through", kw("xhigh", QWEN, BOTH) == {"reasoning_effort": "xhigh"}) check("a level the catalog doesn't list is refused", kw("high", QWEN, BOTH) == {}, kw("high", QWEN, BOTH)) check("...which is the value that would make the template raise", "high" not in QWEN["effort"]["levels"]) check("a level the template can't read is refused", kw("low", QWEN, THINK) == {}, kw("low", QWEN, THINK)) check("off refused when thinking isn't readable", kw("off", QWEN, {"thinking": False, "effort": True}) == {}) check("nothing for a non-reasoning model", kw("low", CODER, BOTH) == {}) check("garbage is ignored, not forwarded", kw("../etc", QWEN, BOTH) == {}) print("\n3. the real catalog entry matches the real template") from llmhost import hfcache, paths cat = {m["name"]: m for m in paths.load_catalog()["models"]} q = cat["qwen3.8-27b"] check("qwen3.8-27b declares levels", (q.get("effort") or {}).get("levels") == ["low", "medium", "xhigh"], q.get("effort")) tmpl = hfcache.chat_template("lmstudio-community/Qwen3.8-27B-MLX-6bit") if tmpl: for lvl in q["effort"]["levels"]: check(f"template accepts {lvl!r}", f"'{lvl}'" in tmpl or f'"{lvl}"' in tmpl) check("template has no 'high' level", "'high'" not in tmpl.replace("'xhigh'", "")) else: print(" skip (template not on disk)") check("no other catalog model claims levels", [m for m in cat.values() if m.get("effort") and m["name"] != "qwen3.8-27b"] == []) print("\n4. a client's own kwargs win over the server default") def inject(server_kw, client_body): """The precedence rule from the proxy, which mirrors mlx_lm's own.""" if not server_kw: return client_body.get("chat_template_kwargs") mine = dict(server_kw) theirs = client_body.get("chat_template_kwargs") if isinstance(theirs, dict): mine.update(theirs) return mine check("server default applied when client says nothing", inject({"reasoning_effort": "low"}, {}) == {"reasoning_effort": "low"}) check("client overrides the same key", inject({"reasoning_effort": "low"}, {"chat_template_kwargs": {"reasoning_effort": "xhigh"}}) == {"reasoning_effort": "xhigh"}) check("client's other keys survive alongside", inject({"reasoning_effort": "low"}, {"chat_template_kwargs": {"enable_thinking": False}}) == {"reasoning_effort": "low", "enable_thinking": False}) check("no server default -> body untouched", inject({}, {}) is None) check("no server default -> client kwargs preserved", inject({}, {"chat_template_kwargs": {"enable_thinking": False}}) == {"enable_thinking": False}) check("a non-dict from the client isn't merged into", inject({"reasoning_effort": "low"}, {"chat_template_kwargs": "nope"}) == {"reasoning_effort": "low"}) print("\n5. the setting survives a setup re-run (`llmhost update`)") from llmhost import cli NAMES = ["qwen3.8-27b", "ornith-1.5-9b"] check("a set choice is carried over", cli.carried_settings({"reasoning": {"qwen3.8-27b": "xhigh"}}, NAMES) == {"reasoning": {"qwen3.8-27b": "xhigh"}}) check("every model's choice is kept, not just the active one", cli.carried_settings({"reasoning": {"qwen3.8-27b": "xhigh", "ornith-1.5-9b": "off"}}, NAMES) == {"reasoning": {"qwen3.8-27b": "xhigh", "ornith-1.5-9b": "off"}}) check("a model that no longer fits is dropped", cli.carried_settings({"reasoning": {"qwen3.8-27b": "xhigh", "gone-70b": "low"}}, NAMES) == {"reasoning": {"qwen3.8-27b": "xhigh"}}) check("nothing set -> no key written", cli.carried_settings({}, NAMES) == {}) check("empty setting -> no key written", cli.carried_settings({"reasoning": {}}, NAMES) == {}) check("no model still fits -> no key written", cli.carried_settings({"reasoning": {"gone-70b": "low"}}, NAMES) == {}) check("setup's own keys are not touched", set(cli.carried_settings({"reasoning": {"qwen3.8-27b": "low"}}, NAMES)) == {"reasoning"}) check.done(ROOT)