Files
OBLITERATUS/tests/test_chat_template_date.py
Joseph Magly 834a33a692 fix(identity-security): deny memfd in the sandbox and fix chat-template id extraction
A memfd that was written, mapped for one page and closed kept its backing
outside every limit. Install a classic-BPF seccomp filter through bwrap
--seccomp: on x86-64 and AArch64, memfd_create and memfd_secret return
ENOSYS; x32 calls and any other architecture kill the process. The live
isolation probe now requires memfd_create to fail, hosts other than x86-64
and AArch64 are refused, and the docs drop the false memfd bound.

Under Transformers 5, apply_chat_template returns a BatchEncoding, so the
generator and paired capture took the wrong object; extract input_ids as
recover does. Paired capture records the pinned template date, and tests
cover date-aware rendering at both call sites.
2026-11-27 17:48:50 -05:00

489 lines
21 KiB
Python

"""Chat templates render the same prompt on every calendar day (#690).
Llama-3.2's template renders ``strftime_now("%d %b %Y")`` into its system
header unless ``date_string`` is passed. These tests use a tiny local tokenizer
with such templates (no downloads) and move the wall clock between two days.
"""
from __future__ import annotations
import ast
import json
from datetime import datetime
from pathlib import Path
from types import SimpleNamespace
import pytest
from obliteratus import chat_template_date as ctd
pytestmark = pytest.mark.cpu
ROOT = Path(__file__).resolve().parents[1]
# Llama-3.2 style: date_string, falling back to strftime_now, then a constant.
LLAMA32 = (
'{%- if not date_string is defined %}{%- if strftime_now is defined %}'
'{%- set date_string = strftime_now("%d %b %Y") %}{%- else %}'
'{%- set date_string = "26 Jul 2024" %}{%- endif %}{%- endif %}'
"<|system|> Today Date: {{ date_string }}\n"
"{% for m in messages %}<|{{ m.role }}|> {{ m.content }}\n{% endfor %}"
"{% if add_generation_prompt %}<|assistant|>{% endif %}"
)
# Templates that call strftime_now directly (Mistral/Granite style).
DIRECT = (
'<|system|> Today is {{ strftime_now("%Y-%m-%d") }}\n'
"{% for m in messages %}<|{{ m.role }}|> {{ m.content }}\n{% endfor %}"
"{% if add_generation_prompt %}<|assistant|>{% endif %}"
)
NODATE = (
"{% for m in messages %}<|{{ m.role }}|> {{ m.content }}\n{% endfor %}"
"{% if add_generation_prompt %}<|assistant|>{% endif %}"
)
DAYS = (datetime(2026, 11, 22, 9, 30), datetime(2026, 11, 25, 18, 5))
_WORDS = ["<|system|>", "<|user|>", "<|assistant|>", "Today", "Date", ":", "is", "-",
"hello", "22", "25", "26", "Nov", "Jul", "2024", "2026", "01", "Jan", "2025",
"11", "07", "2026-11-22", "2026-11-25", "2024-07-26", "2025-01-01"]
def _tokenizer(template):
from tokenizers import Tokenizer, models, pre_tokenizers
from transformers import PreTrainedTokenizerFast
vocab = {"[UNK]": 0, "[PAD]": 1, "[EOS]": 2}
vocab.update({word: index + 3 for index, word in enumerate(_WORDS)})
backend = Tokenizer(models.WordLevel(vocab, unk_token="[UNK]"))
backend.pre_tokenizer = pre_tokenizers.WhitespaceSplit()
tokenizer = PreTrainedTokenizerFast(tokenizer_object=backend, unk_token="[UNK]",
pad_token="[PAD]", eos_token="[EOS]")
tokenizer.chat_template = template
return tokenizer
@pytest.fixture
def clock(monkeypatch):
"""Set the wall clock transformers' ``strftime_now`` reads."""
import transformers.utils.chat_template_utils as utils
def set_day(day):
class Frozen(datetime):
@classmethod
def now(cls, tz=None):
return day
monkeypatch.setattr(utils, "datetime", Frozen)
return set_day
def _measure_format(tokenizer):
from obliteratus.verify_edit.measure import ModelRunner
runner = object.__new__(ModelRunner)
runner.tokenizer, runner.chat_template = tokenizer, True
return runner.format("hello")
def _pipeline_format(tokenizer):
from obliteratus.abliterate import AbliterationPipeline
pipeline = object.__new__(AbliterationPipeline)
pipeline.handle = SimpleNamespace(tokenizer=tokenizer)
return pipeline._apply_chat_template_no_think([{"role": "user", "content": "hello"}])
def _entry_point(tokenizer):
from obliteratus.concepts.entry_points import EntryPoint, render_entry_point
return str(render_entry_point(EntryPoint(), {"text": "hello"}, tokenizer,
use_chat_template=True))
def _experiment_prompts(tokenizer):
from obliteratus.experiments.generation import render_prompts
example = SimpleNamespace(example_id="e1", prompt="hello")
return render_prompts(tokenizer, [example], {"chat_template": True, "system": None})["e1"]
def _recover_ids(tokenizer):
from obliteratus.recover import _chat_ids
return _chat_ids(tokenizer, [{"role": "user", "content": "hello"}],
add_generation_prompt=True)
def _checkpoint_ids(tokenizer):
from obliteratus.verify_checkpoint import render_input_ids
return render_input_ids(tokenizer, "hello")
class _PromptEcho:
"""Stand-in model for ``TransformersGenerator``: records the prompt ids and stops at once."""
def __init__(self):
import torch
self.weight, self.prompts = torch.zeros(1), []
def parameters(self):
yield self.weight
def generate(self, input_ids, **options):
import torch
self.prompts.append(input_ids)
return torch.cat([input_ids, torch.tensor([[options["pad_token_id"]]])], dim=1)
def _identity_settings():
from obliteratus.identity_security.conditions import GenerationSettings
return GenerationSettings("local/fake", "a" * 40, "b" * 40, "transformers", "1", temperature=0.0)
def _identity_generation(tokenizer):
from obliteratus.identity_security.generation import TransformersGenerator
model, settings = _PromptEcho(), _identity_settings()
generator = TransformersGenerator(model, tokenizer, settings=settings, resolved_revision="a" * 40,
engine_version="1")
generator.generate([{"role": "user", "content": "hello"}], seed=1, settings=settings)
return tokenizer.decode(model.prompts[0][0])
def _identity_plan():
from obliteratus.identity_security.conditions import Identity, PromptDesign, TaskPrompt, build_plan
design = PromptDesign(identities=(Identity("neutral", "a customer", "control"),
Identity("target", "a target organization", "target")),
secure_arms=(False,))
return build_plan([TaskPrompt("t", "hello")], _identity_settings(), 1, design)
def _identity_paired_capture(tokenizer):
from obliteratus.identity_security.paired_capture import build_paired_batches
batches, _ = build_paired_batches(tokenizer, _identity_plan())
return tokenizer.decode(batches[0]["input_ids"][0], skip_special_tokens=True)
CALL_SITES = {
"identity_security.generation.TransformersGenerator.generate": _identity_generation,
"identity_security.paired_capture.build_paired_batches": _identity_paired_capture,
"verify_edit.measure.ModelRunner.format": _measure_format,
"abliterate._apply_chat_template_no_think": _pipeline_format,
"concepts.entry_points.render_entry_point": _entry_point,
"experiments.generation.render_prompts": _experiment_prompts,
"recover._chat_ids": _recover_ids,
"verify_checkpoint.render_input_ids": _checkpoint_ids,
}
@pytest.mark.parametrize("template", [LLAMA32, DIRECT], ids=["date_string", "strftime_now"])
@pytest.mark.parametrize("site", sorted(CALL_SITES))
def test_call_sites_render_identically_on_different_days(site, template, clock):
tokenizer = _tokenizer(template)
# The unpinned template really does follow the clock.
clock(DAYS[0])
raw_first = tokenizer.apply_chat_template([{"role": "user", "content": "hello"}],
tokenize=False, add_generation_prompt=True)
clock(DAYS[1])
raw_second = tokenizer.apply_chat_template([{"role": "user", "content": "hello"}],
tokenize=False, add_generation_prompt=True)
assert raw_first != raw_second
rendered = []
for day in DAYS:
clock(day)
rendered.append(CALL_SITES[site](tokenizer))
assert rendered[0] == rendered[1]
@pytest.mark.parametrize("site", ["verify_edit.measure.ModelRunner.format",
"experiments.generation.render_prompts"])
def test_pinned_date_is_the_conventional_default(site, clock):
clock(DAYS[0])
assert "Today Date: 26 Jul 2024" in CALL_SITES[site](_tokenizer(LLAMA32))
assert "Today is 2024-07-26" in CALL_SITES[site](_tokenizer(DIRECT))
@pytest.mark.parametrize("site", ["identity_security.generation.TransformersGenerator.generate",
"identity_security.paired_capture.build_paired_batches"])
def test_identity_security_call_sites_render_the_pinned_date(site, clock):
clock(DAYS[0])
assert "26 Jul 2024" in CALL_SITES[site](_tokenizer(LLAMA32))
assert "2024-07-26" in CALL_SITES[site](_tokenizer(DIRECT))
assert "2026" not in CALL_SITES[site](_tokenizer(LLAMA32)) + CALL_SITES[site](_tokenizer(DIRECT))
@pytest.mark.parametrize("template, uses_date", [(LLAMA32, True), (DIRECT, True), (NODATE, False)],
ids=["date_string", "strftime_now", "no_date"])
def test_identity_security_records_the_pinned_date(template, uses_date):
from obliteratus.identity_security.generation import TransformersGenerator, run_generation
from obliteratus.identity_security.paired_capture import build_paired_batches
tokenizer, settings = _tokenizer(template), _identity_settings()
expected = {"date_string": ctd.DEFAULT_CHAT_TEMPLATE_DATE, "template_uses_date": uses_date}
generator = TransformersGenerator(_PromptEcho(), tokenizer, settings=settings, resolved_revision="a" * 40,
engine_version="1")
assert generator.metadata()["chat_template_date"] == expected
generations = run_generation(_identity_plan(), generator, limit=1)
assert generations["generator"]["chat_template_date"] == expected
assert generations["samples"][0]["error"] is None
_, manifest = build_paired_batches(tokenizer, _identity_plan())
assert manifest["chat_template_date"] == expected
def test_templates_without_a_date_are_rendered_exactly_as_before():
tokenizer = _tokenizer(NODATE)
conversation = [{"role": "user", "content": "hello"}]
assert ctd.date_kwargs(tokenizer) == {}
assert ctd.apply_chat_template(tokenizer, conversation, tokenize=False,
add_generation_prompt=True) == tokenizer.apply_chat_template(
conversation, tokenize=False, add_generation_prompt=True)
# Passing the kwargs to a template that ignores them is harmless too.
forced = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True,
**ctd.date_kwargs(_tokenizer(LLAMA32)))
assert forced == ctd.apply_chat_template(tokenizer, conversation, tokenize=False,
add_generation_prompt=True)
assert ctd.date_record(tokenizer) == {"date_string": "26 Jul 2024",
"template_uses_date": False}
def test_strict_tokenizers_without_a_date_template_get_no_extra_kwargs():
calls = []
class Strict:
chat_template = NODATE
def apply_chat_template(self, conversation, tokenize, add_generation_prompt):
calls.append(conversation)
return "ok"
assert ctd.apply_chat_template(Strict(), [], tokenize=False, add_generation_prompt=True) == "ok"
assert calls == [[]]
# Unset, mocked or named-template dicts are inspected safely.
assert not ctd.template_uses_date(SimpleNamespace(chat_template=None))
assert ctd.template_uses_date(SimpleNamespace(chat_template={"default": LLAMA32, "x": 3}))
@pytest.mark.parametrize("bad", ["2024-07-26", "26 July 2024", "31 Feb 2024", "6 Jul 2024", 3, None])
def test_date_strings_are_validated(bad):
with pytest.raises(ValueError):
ctd.parse_date_string(bad)
def test_override_date_renders_in_both_template_styles(clock):
clock(DAYS[0])
for template, expected in ((LLAMA32, "Today Date: 01 Jan 2025"), (DIRECT, "Today is 2025-01-01")):
text = ctd.apply_chat_template(_tokenizer(template), [{"role": "user", "content": "hi"}],
date_string="01 Jan 2025", tokenize=False,
add_generation_prompt=True)
assert expected in text
def test_verify_edit_spec_override_flows_to_runner_and_vllm(tmp_path, monkeypatch, clock):
from obliteratus.cli import main
from obliteratus.verify_edit import measure, runner
from obliteratus.verify_edit.spec import SpecError, resolve_spec
assert resolve_spec({})["generation"]["date_string"] == "26 Jul 2024"
with pytest.raises(SpecError, match="date_string"):
resolve_spec({"generation": {"date_string": "2025-01-01"}})
spec = tmp_path / "spec.json"
spec.write_text(json.dumps({"generation": {"date_string": "01 Jan 2025"}}))
opened, seen = [], {}
class Runner(measure.ModelRunner):
def __init__(self, path, **options):
opened.append(options)
self.tokenizer, self.chat_template = _tokenizer(LLAMA32), True
self.date_string = options.get("date_string", ctd.DEFAULT_CHAT_TEMPLATE_DATE)
def fake_verify_edit(**kwargs):
seen["options"] = kwargs["capability_options"]
seen["rendered"] = kwargs["runner_factory"]("stock", None).format("hello")
seen["resolved"] = kwargs["spec_record"]["resolved"]
return {"verdict": "pass", "endpoints": {}}, 0
monkeypatch.setattr(measure, "ModelRunner", Runner)
monkeypatch.setattr(runner, "verify_edit", fake_verify_edit)
clock(DAYS[0])
main(["verify-edit", "--stock", "stock", "--edited", "edited", "--spec", str(spec),
"--output", str(tmp_path / "report.json"), "--device", "cpu"])
assert opened[0]["date_string"] == "01 Jan 2025"
assert "Today Date: 01 Jan 2025" in seen["rendered"]
assert seen["resolved"]["generation"]["date_string"] == "01 Jan 2025"
assert seen["options"].chat_template_date == "01 Jan 2025"
def test_observation_records_the_pinned_date():
from obliteratus.verify_edit.measure import ModelRunner, date_record
runner = object.__new__(ModelRunner)
runner.tokenizer, runner.date_string = _tokenizer(LLAMA32), "01 Jan 2025"
assert date_record(runner.tokenizer, runner.date_string) == {
"date_string": "01 Jan 2025", "template_uses_date": True}
@pytest.mark.parametrize("command, expected", [
("vllm serve m --port 1", '{"date_string":"26 Jul 2024"}'),
("env VLLM_X=0 /opt/v/bin/vllm serve m", '{"date_string":"26 Jul 2024"}'),
("python -m my_server m", None),
])
def test_vllm_launch_pins_the_template_date(command, expected):
import shlex
from obliteratus.verify_edit.capability import pin_chat_template_date
argv = shlex.split(command)
before = list(argv)
assert pin_chat_template_date(argv, "26 Jul 2024") == expected
if expected == '{"date_string":"26 Jul 2024"}':
assert argv == before + ["--default-chat-template-kwargs", expected]
else:
assert argv == before
@pytest.mark.parametrize("operator, expected", [
# Operator kwargs without a date: the pinned date is merged in, operator keys kept.
('{"enable_thinking":false}', '{"enable_thinking":false,"date_string":"26 Jul 2024"}'),
# Operator sets the date: their value wins and the flag is left exactly as given.
('{"x":1,"date_string":"01 Jan 2025"}', '{"x":1,"date_string":"01 Jan 2025"}'),
# Not a JSON object: left for vLLM to reject.
("not json", "not json"),
])
@pytest.mark.parametrize("form", ["separate", "equals"])
def test_vllm_operator_template_kwargs_get_the_pinned_date(operator, expected, form):
from obliteratus.verify_edit.capability import pin_chat_template_date
flag = "--default-chat-template-kwargs"
tail = [flag, operator] if form == "separate" else [f"{flag}={operator}"]
argv = ["vllm", "serve", "m", *tail]
assert pin_chat_template_date(argv, "26 Jul 2024") == expected
assert argv[3:] == ([flag, expected] if form == "separate" else [f"{flag}={expected}"])
assert argv.count(flag) + sum(t.startswith(flag + "=") for t in argv) == 1
def test_vllm_server_records_merged_operator_kwargs(tmp_path):
from obliteratus.verify_edit.capability import VllmServer
launcher = tmp_path / "vllm"
launcher.write_text("#!/bin/sh\n")
launcher.chmod(0o755)
server = VllmServer(
f"{launcher} serve {{model}} --default-chat-template-kwargs '{{{{\"enable_thinking\":false}}}}'",
model="m", served_name="s", port=1, ready_timeout=1, log_path=tmp_path / "log",
chat_template_date="01 Jan 2025")
merged = '{"enable_thinking":false,"date_string":"01 Jan 2025"}'
assert server.argv[-2:] == ["--default-chat-template-kwargs", merged]
assert server.chat_template_kwargs == merged
def test_vllm_server_records_the_pinned_kwargs(tmp_path):
from obliteratus.verify_edit.capability import VllmServer
launcher = tmp_path / "vllm"
launcher.write_text("#!/bin/sh\n")
launcher.chmod(0o755)
server = VllmServer(f"{launcher} serve {{model}}", model="m", served_name="s", port=1,
ready_timeout=1, log_path=tmp_path / "log",
chat_template_date="01 Jan 2025")
assert server.argv[-2:] == ["--default-chat-template-kwargs", '{"date_string":"01 Jan 2025"}']
assert server.chat_template_kwargs == '{"date_string":"01 Jan 2025"}'
_METHOD = "apply_chat_template"
# Dynamic lookups that can reach the method by name.
_NAME_LOOKUPS = {"getattr", "attrgetter", "methodcaller"}
# Capability probes: they test that the method exists and cannot render anything.
_PROBES = {"callable", "hasattr"}
def _call_name(node):
func = node.func
return func.id if isinstance(func, ast.Name) else getattr(func, "attr", None)
def _unpinned_uses(source):
"""Lines that reach ``apply_chat_template`` other than through the pinning helper.
Any ``x.apply_chat_template`` attribute and any ``getattr(x, "apply_chat_template")``
(or ``operator.attrgetter`` / ``methodcaller`` by that name) is flagged, whether it is
called, aliased, bound or wrapped in ``functools.partial``. Only a lookup passed
directly to ``callable(...)`` or ``hasattr(...)`` is allowed, because a probe never
renders a template.
"""
tree = ast.parse(source)
parents = {child: node for node in ast.walk(tree) for child in ast.iter_child_nodes(node)}
def probed(node):
parent = parents.get(node)
return (isinstance(parent, ast.Call) and node in parent.args
and isinstance(parent.func, ast.Name) and parent.func.id in _PROBES)
found = []
for node in ast.walk(tree):
if isinstance(node, ast.Attribute) and node.attr == _METHOD:
reaches = True
elif isinstance(node, ast.Call) and _call_name(node) in _NAME_LOOKUPS:
reaches = any(isinstance(arg, ast.Constant) and arg.value == _METHOD
for arg in node.args)
else:
reaches = False
if reaches and not probed(node):
found.append(node.lineno)
return sorted(found)
def test_no_package_call_site_renders_a_template_without_the_pinned_date():
sources = [ROOT / "app.py", *sorted((ROOT / "obliteratus").rglob("*.py"))]
helper = ROOT / "obliteratus" / "chat_template_date.py"
found = {str(path.relative_to(ROOT)): lines for path in sources if path != helper
for lines in [_unpinned_uses(path.read_text(encoding="utf-8"))] if lines}
assert found == {}
@pytest.mark.parametrize("snippet", [
"tok.apply_chat_template(messages)",
'getattr(tok, "apply_chat_template")(messages)',
'apply = getattr(tok, "apply_chat_template", None)\napply(messages)',
"apply = tok.apply_chat_template\napply(messages)",
"functools.partial(tok.apply_chat_template, tokenize=False)(messages)",
'functools.partial(getattr(tok, "apply_chat_template"), tokenize=False)(messages)',
'operator.methodcaller("apply_chat_template", messages)(tok)',
'operator.attrgetter("apply_chat_template")(tok)(messages)',
'if callable(apply := getattr(tok, "apply_chat_template", None)):\n apply(messages)',
], ids=["direct", "getattr-call", "getattr-alias", "bound-alias", "partial", "getattr-partial",
"methodcaller", "attrgetter", "walrus-in-probe"])
def test_completeness_guard_flags_every_way_to_reach_the_method(snippet):
assert _unpinned_uses(snippet) != []
@pytest.mark.parametrize("snippet", [
'if not callable(getattr(tok, "apply_chat_template", None)):\n raise E',
"ok = callable(tok.apply_chat_template)",
'if hasattr(tok, "apply_chat_template"):\n pass',
"ctd_apply(tok, messages)",
"from obliteratus.chat_template_date import apply_chat_template as render",
'getattr(pipeline, "_maybe_apply_chat_template", None)',
"pipeline._apply_chat_template_no_think(messages)",
], ids=["callable-getattr", "callable-attr", "hasattr", "helper-call", "helper-import",
"other-name-getattr", "other-name-attr"])
def test_completeness_guard_allows_capability_probes_and_the_helper(snippet):
assert _unpinned_uses(snippet) == []
def test_evaluation_profiles_carry_the_spec_date():
from obliteratus.recipes.evaluation_profiles import profile_from_spec, resolve_profile
from obliteratus.recipes.validation import RecipeError
from obliteratus.verify_edit.spec import resolve_spec
profile = profile_from_spec(resolve_spec({"generation": {"date_string": "01 Jan 2025"}}),
name="family", scope="family")
assert profile["spec"]["generation"]["date_string"] == "01 Jan 2025"
spec, _ = resolve_profile(profile)
assert spec["generation"]["date_string"] == "01 Jan 2025"
profile["spec"]["generation"]["date_string"] = "2025-01-01"
with pytest.raises(RecipeError, match="date_string"):
profile_from_spec(profile["spec"], name="family", scope="family")