llm-host/tests/test_reasoning.py
Neil Bantoc 51c2880a2e Add tests/: four suites, a runner, and a sandbox that can't escape
These grew one bug at a time in a scratch directory and were about to be thrown
away, having caught four real regressions between them. Move them in as they
are -- plain scripts, stdlib only, no test dependency -- behind a shared harness
and `python3 tests/run.py`.

The harness exists mostly for its guard. A suite testing the purge path once
sandboxed LLMHOST_CONFIG but not LLMHOST_HOME and deleted a live install's data
directory; BIN_DIR and SHIM have no env override at all, coming straight off
Path.home(). So the harness repoints those into the sandbox and then asserts
that every path llmhost could write or delete is inside it, aborting the run if
not -- verified by pointing it at a real path and watching it refuse.

The model plan the opencode suite needs is now a fixture rather than a file read
out of someone's home directory; it carries no key (the key lives in its own
file) and its machine-specific fields are pinned.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-03 02:23:15 -08:00

91 lines
4.9 KiB
Python

"""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"})
check.done(ROOT)