Sub topic 13
Testing that important facts survive compaction
This chapter assumes you have read the earlier chapters on sliding-window truncation, rolling summaries, and the hybrid strategy. You should be comfortable with Python and chat message lists. Compaction is lossy by design, so you need a repeatable way to check what it lost.
Why you need a test
A compaction strategy can look fine in a demo and still drop a fact the user gave you 40 turns ago. Typical failures are a missing file path, a changed number, or a forgotten constraint. Reading summaries by hand does not scale. Instead, write down the facts that must survive and check for them after every compaction.
Build a fact-retention test
The idea has four steps:
- Write a scripted conversation that plants known facts early.
- Pad it with filler turns until compaction triggers.
- Run your compaction code.
- Ask whether each planted fact is still available, either in the recent messages or in the summary.
The check below uses plain string matching on exact values. Exact values such as /srv/app/config.yaml or 42 are the easiest to test and the most costly to lose.
def estimate_tokens(message):
return len(message["content"]) // 4 + 4
def compact(history, summary, trigger_tokens, keep_recent, summarize):
summary_cost = estimate_tokens({"content": summary}) if summary else 0
total = summary_cost + sum(estimate_tokens(m) for m in history)
if total <= trigger_tokens or len(history) <= keep_recent:
return summary, history
old = list(history[:-keep_recent])
recent = list(history[-keep_recent:])
while recent and recent[0]["role"] != "user":
old.append(recent.pop(0))
return summarize(summary, old), recent
def surviving_facts(summary, recent, facts):
visible = summary + "\n" + "\n".join(m["content"] for m in recent)
return {fact: fact in visible for fact in facts}
def fake_summarize(previous_summary, old_messages):
# Stand-in for a model call. It keeps only the first sentence of each
# user message, which makes the test fail in a visible way.
lines = [m["content"].split(".")[0] for m in old_messages if m["role"] == "user"]
return (previous_summary + "\n" + "\n".join(lines)).strip()
history = [
{"role": "user", "content": "My config is at /srv/app/config.yaml. Use port 8443."},
{"role": "assistant", "content": "Noted."},
]
for i in range(30):
history.append({"role": "user", "content": f"Filler question number {i}. " + "x" * 80})
history.append({"role": "assistant", "content": "Filler answer. " + "y" * 80})
summary, recent = compact(history, "", trigger_tokens=500, keep_recent=6,
summarize=fake_summarize)
facts = ["/srv/app/config.yaml", "8443"]
print(surviving_facts(summary, recent, facts))Running this prints {'/srv/app/config.yaml': False, '8443': False}. The fake summarizer keeps only the text before the first period, and the file path contains periods that cut it off. Its first sentence, "My config is at /srv/app/config", loses the .yaml ending. The port number sits in the second sentence and is dropped. That is the kind of bug this test exists to catch.
Go beyond string matching
String matching catches lost values but not changed meaning. A summary can keep "8443" and still say the user wants a different port. Add these checks as your needs grow:
- Question probes. Send the compacted request to the model with a question such as "Which port did I ask for?" and compare the answer to the expected one.
- Contradiction facts. Plant a fact, then correct it later ("Actually, use 9000"). The test passes only if the new value survives and the old one does not drive the answer.
- Repeated compaction. Run several rounds. A fact that survives one pass can be lost on the third, because each summary is built from the last one.
- Length checks. Assert that the summary stays under its word limit.
Common mistakes
- Testing only after a single compaction. Rolling summaries degrade over repeated rounds.
- Planting facts only in the first message. Place them at different depths so some fall in the old part and some in the recent window.
- Using filler that is too uniform. Real conversations contain code, numbers, and corrections.
Exercise
Your test plants the fact "deadline is 2026-12-01" in turn 1, then a correction in turn 20: "the deadline moved to 2026-12-15". After compaction, the string check finds both dates in the summary. Why is that still not a pass, and what check would you add?
Show answer
Both dates being present does not show the summary treats 2026-12-15 as the current one. The model may act on the stale date. Add a question probe such as "What is the deadline?" and assert the answer is 2026-12-15. You can also assert that the summary does not present the old date as current.
Exercise
Why should a retention test run compaction several times in a row instead of once?
Show answer
A rolling summary is rewritten from the previous summary each round, so small losses accumulate. A fact that survives the first pass can be dropped or distorted in a later one.