Files
alzeo/tests/test_patch_template_default_kept.py
T
Hasan BasbunarandClaude Opus 5.5 dbe7cfa9ad patch-template: a template from before the lean level keeps no choice
Since v1.18.7 a run that does not set LEAN_DEFAULT keeps the default the
installed template has, so that a box installed with LEAN_DEFAULT=0
stays on xhigh. It read that choice from default('xhigh') alone. A
template written before v1.13 has no lean level and defaults to xhigh,
the one default there was: it chose nothing, and a box updated from
there to v1.18.7 or later stayed on xhigh without anyone asking for it.
A user's box did (2026-10-05), and every bench.sh probe measured other
text than the reference box's, 10 to 16% slower on code and reasoning.

Only a template that offers the lean level and still defaults to xhigh
is a kept choice now; one without the level gets the current default,
and the run says so. Reproduced with the patch-template.py of v1.12.1 on
the reference box's checkpoint (edp1096 abliterated NVFP4 @21565d38):
its template, then the script of v1.22.6, gave xhigh (md5 f6c09ac8);
with this one, lean (md5 30454bff, the reference box's own), then
unchanged on the next run. A template written with LEAN_DEFAULT=0 still
keeps xhigh, and the reference box's lean template is left as it is.

On a box that had such a template, the update rewrites it, so the
engine that reads it restarts once to serve lean. Three tests in
test_patch_template_default_kept.py, and five mutants of the new lines
are each caught. LEAN.md says what is kept, and what is not a choice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:19:41 +02:00

101 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""The lean level's default survives a run that does not name it.
LEAN_DEFAULT=0 installs the lean level without making it the default. patch-template.py
read the choice from the environment of each run, and neither a plain ./install.sh nor
the cockpit's Switch passes it, so a box installed with LEAN_DEFAULT=0 went back to lean at
the next of either; LEAN.md presents it as a kept choice. And the refusal of an unknown
level said "lean (default)" whatever the default was (found in review, 2026-09-24).
A template written before v1.13 has no lean level and defaults to xhigh, the one default
there was: read as a kept LEAN_DEFAULT=0, it held a box updated from there on xhigh without
anyone asking for it (a user's box, 2026-10-05). It chose nothing, and gets the default.
"""
import importlib.util
import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
SCRIPT = REPO / "patch-template.py"
spec = importlib.util.spec_from_file_location("pt_kept", SCRIPT)
pt = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pt)
SHA = "d" * 40
REPO_ID = "org/model"
class TheDefaultIsKept(unittest.TestCase):
def setUp(self):
self.base = pathlib.Path(tempfile.mkdtemp(prefix="lean-kept-"))
snap = self.base / "hub" / "models--org--model" / "snapshots" / SHA
snap.mkdir(parents=True)
(snap / "chat_template.jinja").write_text(
"HEAD\n" + pt.EFFORT_ANCHOR + "\n" + pt.MSG_ANCHOR + "\nMID\n" + pt.LEAN_ANCHOR + pt.SYSTEM_ANCHOR + "\nTAIL\n")
self.out = self.base / "out.jinja"
def run_patch(self, lean_default=None):
env = {k: v for k, v in os.environ.items() if k != "LEAN_DEFAULT"}
if lean_default is not None:
env["LEAN_DEFAULT"] = lean_default
r = subprocess.run([sys.executable, str(SCRIPT), str(self.base), str(self.out), SHA, REPO_ID],
capture_output=True, text=True, env=env, timeout=30)
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.log = r.stdout
return self.out.read_text()
def before_the_lean_level(self):
"""What the patcher wrote before v1.13: the effort and system fixes, default xhigh,
and no lean level."""
old = (self.base / "hub" / "models--org--model" / "snapshots" / SHA / "chat_template.jinja").read_text()
old = old.replace(pt.EFFORT_ANCHOR, pt.EFFORT_PATCHED.replace("@@DEF@@", "xhigh"), 1)
old = old.replace(pt.SYSTEM_ANCHOR, pt.SYSTEM_PATCHED, 1)
self.assertIn("reasoning_effort|default('xhigh')", old)
self.assertNotIn(pt.LEAN_MARKER, old)
return old
def test_a_run_without_the_variable_keeps_xhigh(self):
self.assertIn("reasoning_effort|default('xhigh')", self.run_patch("0"))
kept = self.run_patch()
self.assertIn("reasoning_effort|default('xhigh')", kept, "the plain run went back to lean")
def test_the_refusal_names_the_real_default(self):
text = self.run_patch("0")
self.assertIn("lean, xhigh (default), medium, and low", text)
self.assertNotIn("lean (default)", text)
def test_naming_it_again_moves_it(self):
self.run_patch("0")
self.assertIn("reasoning_effort|default('lean')", self.run_patch("1"))
def test_a_template_from_before_the_lean_level_chose_nothing(self):
self.out.write_text(self.before_the_lean_level())
text = self.run_patch()
self.assertIn("reasoning_effort|default('lean')", text, "it kept xhigh, which nobody chose")
self.assertIn("lean (default), xhigh, medium, and low", text)
self.assertIn("predates the lean level", self.log)
self.assertNotIn("kept from the installed template", self.log)
def test_it_says_lean_kept_on_the_run_after(self):
self.out.write_text(self.before_the_lean_level())
first = self.run_patch()
self.assertEqual(self.run_patch(), first)
self.assertIn("lean (kept from the installed template)", self.log)
def test_naming_xhigh_on_such_a_box_still_keeps_it(self):
self.out.write_text(self.before_the_lean_level())
self.run_patch("0")
self.assertIn("reasoning_effort|default('xhigh')", self.run_patch(), "the choice made then was lost")
def test_a_fresh_install_defaults_to_lean(self):
text = self.run_patch()
self.assertIn("reasoning_effort|default('lean')", text)
self.assertIn("lean (default), xhigh, medium, and low", text)
if __name__ == "__main__":
unittest.main(verbosity=2)