Files
DeepTutor/tests/services/memory/test_prompt_cache_layout.py

60 lines
2.0 KiB
Python

"""Memory consolidator system prompts keep run-specific values at the tail.
Every consolidation run issues one call per chunk for every surface / slot.
Providers cache the request prefix, so the static instructions must come
first and the per-run values (user label, surface or slot, its focus and
allowed sections, today's date) must only appear at the end of the system
prompt. Two runs for different surfaces then share the whole static part.
"""
from __future__ import annotations
import pytest
from deeptutor.services.memory.consolidator.modes._runtime import load_prompt
# The keyword sets each mode passes to ``system.format`` (modes/update.py,
# modes/audit.py, modes/dedup.py).
_RUN_A = {
"user_label": "alice",
"surface": "chat",
"slot": "learning",
"sections": "Misconceptions, Mastery, Topics",
"focus": "Stable misconceptions",
"today": "2026-01-01",
}
_RUN_B = {
"user_label": "bob",
"surface": "quiz",
"slot": "habits",
"sections": "Error patterns, Strong topics",
"focus": "Error patterns across quiz attempts",
"today": "2026-01-02",
}
_MARKER = {"en": "Run details:", "zh": "本次运行信息:"}
def _shared_prefix(a: str, b: str) -> int:
n = 0
while n < min(len(a), len(b)) and a[n] == b[n]:
n += 1
return n
@pytest.mark.parametrize("language", ["en", "zh"])
@pytest.mark.parametrize("name", ["update_l2", "update_l3", "audit_l2", "audit_l3", "dedup"])
def test_run_specific_values_only_follow_the_static_instructions(name, language) -> None:
template = load_prompt(name, language)["system"]
system_a = template.format(**_RUN_A)
system_b = template.format(**_RUN_B)
marker = system_a.index(_MARKER[language])
assert _shared_prefix(system_a, system_b) >= marker
static = system_a[:marker]
for run in (_RUN_A, _RUN_B):
for key in ("user_label", "focus", "sections"):
assert run[key] not in static
# The values still reach the model, after the static part.
tail = system_a[marker:]
assert "alice" in tail and "2026-01-01" in tail