Files
alzeo/tests/test_patch_template.py
T
Hasan BasbunarandClaude Opus 5 f01c79882e v1.13.0: lean, a fourth reasoning-effort level, and it is the default
Qwen3.8-27B defaults to xhigh, the most expensive of its three levels, and the
levels are not modes: they are three sentences the chat template prepends to the
system message, measurable as 42, 30 and 0 tokens of prompt. That is the whole
mechanism, which is what makes it addressable with words.

Measured here over 7,008 runs, the shipped default is not only slower, it is
worse. On 364 public problems (HumanEval 164 with the dataset's tests executed,
GSM8K 200 on the exact final number), xhigh costs 3.19x the thinking tokens of
medium [2.73, 3.69] and scores 2.2 points lower on a paired McNemar test
(p=0.057). On HumanEval alone it is 4.3 points lower at p=0.039, and five of its
ten failures there are truncations: it spent the whole 16,000-token budget
thinking and returned nothing. low, the fix the community recommends, loses
significantly (p=0.021) for an 11% saving.

- lean: 74 words, added as a fourth level and made the default. On those same
  364 problems it costs 0.71x the thinking tokens of medium [0.66, 0.77] with no
  detectable quality change (-1.1 points, p=0.344). On 58 deliberately
  underspecified requests, the shape that makes this model spiral, it costs
  0.436x of medium [0.40, 0.47] and 0.047x of xhigh [0.04, 0.06] while producing
  17.2 points more usable answers than xhigh [+10.9, +24.1], with zero empty
  answers and zero truncations against 6.9% and 10.3%.
- Qwen's three levels come out byte-identical. Ask for medium and you get Qwen's
  medium: a repo that quietly redefines a standard level makes every number
  measured with it incomparable with everyone else's.
- LEAN_DEFAULT=0 installs the level without taking the default.

Proxy v6.18: SGLang validates reasoning_effort at the API boundary against an
enum compiled into its own request model, so a level the chat template
understands is refused before the template runs. The same value inside
chat_template_kwargs is not validated and reaches it, which is the door opencode
happens to use. The proxy moves a level SGLang refuses into that field and
touches nothing in SGLang's own enum.

The level also has to reach the config opencode actually reads. install.sh
writes a complete config into CONFIG_DIR but only merged limits into the
operator's own file, so a new level appeared everywhere except where it could be
selected. oc-merge-limits.py --add-variant closes that.

LEAN.md carries the method, the pre-registered falsification criteria, the
three-state functional scoring (the answer's code is run, with "not checkable"
kept separate from "broken"), the fairness replay that tripled the token cap to
show the truncations were not an artefact, and what did not work: an expert
persona (no gain, twice the tokens used alone), a decisiveness instruction
(nothing left to remove, lean already sits at 0.00 backtracking markers per
thousand), Chain of Draft (cheapest, loses quality), and four candidates from an
error analysis that all cost tokens and gained nothing. It also states what was
prepared and not run: a 557-problem held-out set with an identical-twin null
control.

Verified live before shipping: 20 end-to-end checks, and on one open request
990 thinking tokens in 19.0 s for lean against 9,084 in 190.9 s for xhigh.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 14:05:59 +02:00

74 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""patch-template.py on a fixture built from the script's own anchor
constants: both patches applied, idempotent message on a pre-patched template,
exact-revision snapshot selection, custom repo argument. No network."""
import importlib.util
import os
import subprocess
import sys
import tempfile
import shutil
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SCRIPT = os.path.join(REPO_DIR, "patch-template.py")
spec = importlib.util.spec_from_file_location("pt", SCRIPT)
pt = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pt)
def make_fixture(base: str, repo: str, sha: str, body: str) -> str:
d = f"{base}/hub/models--{repo.replace('/', '--')}/snapshots/{sha}"
os.makedirs(d, exist_ok=True)
with open(f"{d}/chat_template.jinja", "w") as f:
f.write(body)
return f"{d}/chat_template.jinja"
def run(base: str, out: str, rev: str, repo: str) -> str:
r = subprocess.run([sys.executable, SCRIPT, base, out, rev, repo],
capture_output=True, text=True)
assert r.returncode == 0, r.stdout + r.stderr
return r.stdout
def main() -> None:
base = tempfile.mkdtemp(prefix="tpl-fixture-")
try:
sha = "c" * 40
repo = "org/custom-model"
# Every anchor the script looks for, in the order the upstream template has
# them. A fixture missing one makes the patcher refuse the whole template,
# which is the behaviour tested below for a REAL upstream change and not
# something this fixture should be triggering by accident.
stock_body = ("HEAD\n" + pt.EFFORT_ANCHOR + "\n" + pt.MSG_ANCHOR + "\nMID\n"
+ pt.LEAN_ANCHOR + pt.SYSTEM_ANCHOR + "\nTAIL\n")
make_fixture(base, repo, sha, stock_body)
out = os.path.join(base, "out.jinja")
log = run(base, out, sha, repo)
assert log.count("applied") == 4, log
patched = open(out).read()
assert "'minimal'" in patched, "effort patch must map the minimal tier"
assert "resolved_reasoning_effort == 'lean'" in patched, "lean patch must add the level"
assert "default('lean')" in patched, "lean must become the default"
assert "lean (default), xhigh, medium, and low" in patched, "the refusal must name lean"
assert "<system-reminder>" in patched, "system patch must render reminders"
assert patched.startswith("HEAD\n") and patched.endswith("TAIL\n")
# A template that already carries both fixes: succeed, change nothing.
make_fixture(base, repo, sha, patched)
log2 = run(base, out, sha, repo)
assert log2.count("already present") == 4, log2
assert open(out).read() == patched
# Exact-revision selection with a newer decoy snapshot.
decoy = make_fixture(base, repo, "d" * 40, "DECOY " + stock_body)
os.utime(decoy, None)
run(base, out, sha, repo)
assert not open(out).read().startswith("DECOY"), "pinned snapshot must win"
print("test_patch_template: OK")
finally:
shutil.rmtree(base, ignore_errors=True)
if __name__ == "__main__":
main()