Files
alzeo/tests/test_patch_template.py
T
Hasan BasbunarandClaude Opus 5.5 482c3aa424 patch-template's fixes are chosen on its command line, and the CI check renders as the engines do
The selection of fixes v1.40.2 gave patch-template.py was TEMPLATE_PATCHES, read from the environment
by every caller: one exported in a shell made ./install.sh write the 27B's template without its three
effort fixes, the 27B restarted on it, and it refused Claude Code's "max" and pi's "lean" (found by the
code review on 2026-10-24). It is now --patches=NAMES, which the Ornith lane's installer alone passes;
TEMPLATE_PATCHES reaches nothing. tests/test_patch_template.py exports it and still gets the four.

tests/lane-templates-check.py rendered otherwise than the engines and the installers in three ways,
also found by the review, and each is now theirs:
- a lane's installer patches chat_template.jinja only, so a patched lane without one fails here, as
  its install would have;
- vLLM turns a message's content into a list of parts for a template that loops over it (its own
  reading of the template, vllm/renderers/hf.py) and SGLang leaves strings as they are
  (sglang/srt/parser/jinja_template_utils.py): both readings are ported, and each lane is rendered in
  the content form its engine gives the template, an SGLang lane with lists too where its template
  takes them (what each engine does to a tool's answer and to an empty content came later in this
  release). Rendering lists everywhere would have refused Kolibri's templates, which no engine hands a
  list;
- named templates give tool_use when tools are sent, as transformers picks.
And Hugging Face's 429 and brief 5xx are waited out on a runner's shared addresses, five tries, with
its Retry-After, a body cut short included. On the reference box: 13 targets, 240 renders, 0 refusals,
each identical to transformers 5.12.1's own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 18:41:32 +02:00

111 lines
6.0 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")
# The same template again leaves the file as it is, mtime included: the engine
# reads it at start, and install.sh keeps a running engine only when nothing it
# reads was written after it started (every run used to rewrite it, 2026-09-23).
os.utime(out, (1_000_000_000, 1_000_000_000))
log_same = run(base, out, sha, repo)
assert "patched template unchanged" in log_same, log_same
assert os.stat(out).st_mtime == 1_000_000_000, "an identical template was rewritten"
assert open(out).read() == patched
# 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"
# A lane whose template has no reasoning effort of its own asks for one fix by name
# (Ornith 1.5 122B, 2026-10-24, whose template refused a system message past the first one,
# as opencode 1.19.4 sends): that fix alone, no effort line, every other byte kept. Without
# the name the patcher refuses that template, as it refuses an upstream change.
orn_repo, orn_sha = "org/no-effort", "e" * 40
orn_body = "HEAD\n" + pt.SYSTEM_ANCHOR + "\nTAIL\n"
make_fixture(base, orn_repo, orn_sha, orn_body)
orn_out = os.path.join(base, "orn.jinja")
env = {k: v for k, v in os.environ.items() if k not in ("TEMPLATE_PATCHES", "LEAN_DEFAULT")}
argv = [sys.executable, SCRIPT, base, orn_out, orn_sha, orn_repo]
r = subprocess.run(argv[:2] + ["--patches=system-reminder"] + argv[2:], capture_output=True, text=True, env=env)
assert r.returncode == 0, r.stdout + r.stderr
assert "patch 'system-reminder': applied" in r.stdout, r.stdout
assert "reasoning effort" not in r.stdout and r.stdout.count("patch '") == 1, r.stdout
assert open(orn_out).read() == orn_body.replace(pt.SYSTEM_ANCHOR, pt.SYSTEM_PATCHED)
os.remove(orn_out)
r = subprocess.run(argv, capture_output=True, text=True, env=env)
assert r.returncode != 0 and "anchor not found" in r.stdout + r.stderr, r.stdout + r.stderr
assert not os.path.exists(orn_out), "a refused template was written"
r = subprocess.run(argv[:2] + ["--patches=system-reminders"] + argv[2:], capture_output=True, text=True, env=env)
assert r.returncode != 0 and "the fixes are" in r.stdout + r.stderr, r.stdout + r.stderr
# A selection left in the shell reaches no caller: the 27B's template keeps its four fixes with
# TEMPLATE_PATCHES=system-reminder exported, which made ./install.sh drop its effort fixes when
# v1.40.2 as first pushed read it from the environment (found in review, 2026-10-24).
ambient = os.path.join(base, "ambient.jinja")
r = subprocess.run([sys.executable, SCRIPT, base, ambient, sha, repo], capture_output=True, text=True,
env={**env, "TEMPLATE_PATCHES": "system-reminder"})
assert r.returncode == 0, r.stdout + r.stderr
assert r.stdout.count("patch '") == 4 and "reasoning_effort" in open(ambient).read(), r.stdout
print("test_patch_template: OK")
finally:
shutil.rmtree(base, ignore_errors=True)
if __name__ == "__main__":
main()