llm-host/tests/test_reasoning.py
Neil Bantoc e24c0b7712 Reasoning effort is the dashboard's to set, not the client's
One model is loaded at a time under one setting, and that setting belongs next
to the model it applies to -- the same reason the client can't switch models.
So the proxy now takes a client's reasoning settings off the request before
applying the dashboard's: a top-level reasoning_effort, and enable_thinking or
reasoning_effort inside chat_template_kwargs. Other template kwargs, which
llm-host has no opinion about, still pass through.

This reverses the precedence the proxy had (a client's own kwargs won, mirroring
mlx_lm's own rule), which was the wrong model for a single-model server: clients
offer a generic menu -- opencode's low/medium/high -- and a level the template
doesn't define makes it raise, so every request failed with nothing on the
user's screen to explain it.

- opencode: the provider block disables all three of opencode's effort levels
  instead of curating them. A chooser there would be a menu that does nothing.
- Deletes the nearest-effort mapping (EFFORT_SCALE, nearest_effort,
  plan.effort_levels, the effort_levels= parameter through both backends): it
  existed only to rescue a client-supplied level, and none survives now.
- plan.REASONING_KEYS names the knobs this server drives, with a test asserting
  it stays exactly equal to what reasoning_kwargs can emit -- so a new template
  knob can't be added without also taking it away from clients.
- Two strings that still described a menu of models, from before the block held
  a single entry.

Verified against the live MLX server: with the dashboard on xhigh, five request
shapes (nothing, enable_thinking: false, effort low top-level, effort low in
kwargs, and the off-scale "turbo" that used to 404) all answer identically, and
the reply tracks the dashboard alone as it moves off / low / xhigh.

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

98 lines
5.6 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. every knob the dashboard can set is one the proxy takes away from clients")
# The proxy drops plan.REASONING_KEYS from a client's request before applying the
# dashboard's setting. If reasoning_kwargs learns a new template knob and this list
# doesn't, a client could set that knob itself -- so the two must stay in step.
produced = set()
for entry, controls in ((QWEN, BOTH), (ORNITH, THINK), (QWEN, THINK)):
for value, _ in plan.reasoning_options(entry, None, controls):
produced |= set(plan.reasoning_kwargs(value, entry, controls))
check("the dashboard's knobs are exactly the ones taken from clients",
produced == set(plan.REASONING_KEYS), (sorted(produced), plan.REASONING_KEYS))
check("both knobs are covered", set(plan.REASONING_KEYS) == {"enable_thinking", "reasoning_effort"},
plan.REASONING_KEYS)
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)