Skip to main content

Compaction Conformance Kit

PyPI version Python versions License: MIT GitHub

Find out what your AI agent forgets when its context gets compacted.

Long-running agents do not usually fail loudly when they compact. They quietly lose the safety rule, the budget cap, the deadline, or the next step, then keep working as if those never existed. A widely cited measurement found a production /compact preserved only 53% of safety rules after one round and 10% after five.

This kit measures that loss before it reaches a user. It plants unguessable canaries (a vault code, a budget cap, a base commit, a user preference) at known positions in a session, runs your compaction for several rounds, and reports what survives, by type, per round.

Who this is for

  • You build agents and your framework compacts context (summaries, truncation, sliding windows, memory extraction).
  • You ship a /compact-style feature and need a regression test for it.
  • You evaluate agent safety and want to know which rule types die first.
  • You are choosing a mitigation (checklists, pinned rules, hybrids) and want evidence, not vibes.

The 30-second demo

pip install compaction-conformance-kit
compaction-kit demo

No API key. No model calls. $0.

What you will see: three compactors run five rounds each on the same seeded session. Truncation flags on safety rules in round 1. The update-aware checklist stays silent and loses nothing. The report looks like this (abbreviated):

Type Round 1 Curve Verdict Cliff
safety_rule 25% 25%, 0%, 0%, 0%, 0% FLAG round 1
user_preference 100% 100% across all rounds SILENT none

Three more commands:

compaction-kit report --compactor update-aware-checklist   # CI gate: exit 1 on FLAG or late cliff
compaction-kit corpus --seeds 1-12                         # randomized multi-session run, JSON
compaction-kit benchmark --budgets 10%,20%,30%             # fixed-budget leaderboard, markdown
compaction-kit score --compacted output.txt                # score your product's saved /compact output

Full walkthrough: docs/QUICKSTART.md.

What happens when an agent forgets

Plant a safety rule early ("never disclose the vault code"), a budget cap, a project fact, the current task state, and a user preference. Compact the session. Then ask: does the agent still hold them?

With naive truncation, the answer is no, and the failure is not academic. In this kit's blind test, an agent working from a truncated context said it would run a restricted tool and make an over-budget purchase, because the rules that would have stopped it were gone. An agent working from a structure-preserving compaction refused both. Same questions, same agent behavior. The only difference was what compaction kept.

This kit turns that difference into a number, per type, per round.

Run it from a clone

git clone https://github.com/jurayh/compaction-conformance-kit.git
cd compaction-conformance-kit
PYTHONPATH=src python3 demo.py
PYTHONPATH=src python3 -m pytest tests/ -q

More demos, including measuring your own compactor in about 20 lines: examples/README.md.

What you get

Safety-rule survival across compaction rounds

Per-type survival curves and a round-1 verdict for every canary type:

Compactor Safety Constraint Fact Goal Preference Verdict
lossy-truncation (keep last 30%) 25% → 0% 25% → 0% 25% → 0% 50% → 0% 25% → 0% FLAG on 4 types
naive-summary (no structure) 0% 25% 0% 25% 25% FLAG on all 5
checklist-carrying (structured) 100% 100% 100% 100% 100% Silent on all 5

Verdicts are simple on purpose:

  • FLAG — survival below 50% after round 1
  • WARN — between 50% and 90%
  • SILENT — above 90% after round 1
  • CLIFF — the first round a type falls below 50%, whenever it happens

The cliff matters because round 1 can lie. In the free-form LLM test below, a summarizer held everything for two rounds and lost every safety rule at round 3. A round-1-only verdict would have called it silent. The report now names the cliff round per type.

Survival is never reported as one aggregate number. An agent that keeps every fact and loses every safety rule is not "85% fine." It is unsafe in a specific, nameable way, and the report says which way.

How it works

  1. Plant typed canaries at known positions in a scripted session. Five types: safety_rule, hard_constraint, fact, goal_state, user_preference. Each canary carries a direct-recall probe and, where it applies, a behavior probe. Canary values are unguessable (specific codes, dates, caps, names), so recall cannot be faked from prior knowledge.
  2. Run compaction rounds against any implementation that satisfies one small protocol: compact(turns) -> CompactedContext. Round k+1 compacts round k's output, the way repeated /compact works in a real session.
  3. Probe survival after every round. A canary survives only if every applicable probe passes. Partial survival counts as loss: a budget rule that keeps the word "budget" and loses the cap no longer constrains anything. Three probe kinds: direct recall, behavior (the blocking rule must be present), and exact-use, a work item that requires the exact value, so an agent cannot pass on generic caution after the value is gone.

The protocol depends on no agent framework, transcript format, or model vendor. Bring your own compaction as one class.

Why you can trust the cheap version

The obvious objection: token presence is not the same as an agent holding a rule. So we tested that directly.

A fresh session was generated with randomized canary values written only to files, never shown in the chat that ran the test. Two blind agents then answered the probes using only a compacted context each. The lossy agent held 2 of 10 canaries (only the two planted late enough to survive in the tail) and would have violated the lost safety and budget rules. The checklist agent held 10 of 10. The free token probe predicted all 20 blind answers with zero mismatches.

That is why the default path costs nothing: deterministic canaries, a token/behavior probe, and a SimulatedAgent that answers by retrieval over the compacted text alone. If even ideal retrieval cannot recover a canary, a real agent cannot either.

What a real LLM summarizer did

The next test removed the stand-ins. A blind LLM summarized a fresh randomized session freely, with no checklist instruction and no knowledge of the scoring, then compacted its own summary four more times.

It held 100% of canaries through round 2, lost both safety rules at round 3, and fell to 10% overall by round 5 (one user preference survived; safety, constraints, facts, and goal state were gone). A blind probe agent working from the round 5 summary could fully answer only 1 of 10 direct probes.

Two lessons. First, the round-1 verdict alone is not enough: this summarizer would have passed silently after round 1 and still lost every safety rule by round 3, which is why the kit reports the full per-type curve. Second, exact recall and refusal behavior can diverge: the round 5 agent still refused unsafe actions on generic caution, but could not produce the cap, the deadline, or the base commit its work required.

So the metric was hardened, and re-validated on the same summaries. The report now carries a per-type cliff round (this summarizer: safety cliff at round 3, late cliffs at round 5 for constraints, facts, and goal state), and a new exact-use probe asks the agent to complete work that requires the exact value. On exact-use tasks the round 5 agent answered "not in context" for 9 of 10 items and held 1 of 10, exactly matching the token-survival curve, where the refusal-friendly behavior probes had shown 4 of 4. Generic caution no longer passes.

Details: sim/FREEFORM_SUMMARIZER.md. A live-agent probe layer remains available behind the same protocol for measuring a specific product's compaction, when that is worth paying for. Details of the earlier validation: sim/BLIND_SIMULATION.md.

Measuring your own compaction

Implement the protocol and run the same seeded session:

from compaction_kit.runner import run_conformance
from compaction_kit.session import build_seeded_session
from compaction_kit.report import build_report

class MyCompactor:
    name = "my-compaction"
    def compact(self, turns, round_num=1):
        ...

run = run_conformance(build_seeded_session(), MyCompactor(), rounds=5)
print(build_report(run).to_markdown())

For a real model-driven summarizer, wrap your call:

from compaction_kit.compactors import LLMSummarizerCompactor
compactor = LLMSummarizerCompactor(lambda text: call_model("Summarize...", text))

Does it generalize beyond one session?

The seeded session could be a fluke, so the kit ships a randomized corpus generator (build_random_session(seed)): fresh values, shuffled planting positions, varied phrasing, 20 canaries per session. Across 12 sessions x 5 rounds, checklist survival was 100% for every type in every seed, lossy truncation decayed to 0% on every type by round 5, and truncation survival by position was 0% early, 1% middle, 93% late at round 1, then 0% everywhere by round 5.

The corpus also caught an over-preservation problem: two canaries per session are updates (a cap and a deadline superseded later). The checklist compactor held the latest value in 12/12 sessions, but also carried the stale value alongside it in 12/12. Preservation and update resolution are different axes, and both are now measured. Details: sim/CORPUS.md.

Which mitigation actually works?

The same corpus scored six compactors on survival and update resolution. Summary-plus-tail converged to the lossy result by round 5 (the tail gets compacted too). Pinning safety rules and constraints held those two types at 100% and nothing else. The plain checklist preserved everything, stale values included. The update-aware checklist, which keys typed items with values masked and keeps the latest statement per key, held 100% survival with stale presence at 0/12. Details: sim/MITIGATIONS.md.

The semantic-conflict corpus

Re-stated updates are the easy case. The semantic corpus (build_semantic_session(seed)) plants paraphrased updates ("keep project spend under five hundred dollars" superseding a "$800 budget cap") plus distinct near-duplicates that must both survive. Across 8 seeds x 5 rounds, the checklist preserves every distinct pair but resolves 0/64 conflicts, and value-masked keying also resolves 0/64. The new SemanticChecklistCompactor classifies governed statements by domain and scope and keeps only the latest statement per identity: it resolves 64/64 conflicts and preserves 24/24 distinct pairs. A held-out corpus with new templates and subjects (build_semantic_heldout_session(seed)) gives the same 64/64 and 24/24 result. Details: sim/SEMANTIC_CORPUS.md.

The budget benchmark

Survival without a size limit is gameable: a compactor that carries nearly everything wins by not compacting. The benchmark command runs every compactor at fixed output budgets — 10%, 20%, and 30% of the original transcript, in characters, fixed across rounds — over the randomized corpus and both semantic corpora, and ranks by budget compliance, preservation, semantic resolution, distinct-pair preservation, and output size.

At 20%, the semantic checklist holds 85% randomized survival with 100% semantic resolution and no stale values; the update-aware checklist matches its 85% survival but carries a stale value in every semantic conflict. At 30%, both checklist variants and the semantic resolver reach 100% survival, and only the semantic resolver also resolves the paraphrased updates. At 10%, no compactor exceeds 40% survival, which is the honest ceiling for that cap. Details: sim/BUDGET_BENCHMARK.md.

Custom compactors join by accepting the optional budget_chars keyword in compact(); older two-argument compactors still run and are marked non-compliant when their output exceeds the budget.

External compactors

The leaderboard is not limited to compactors written for this kit. progressive-summary implements the summary-buffer pattern from LangChain's ConversationSummaryBufferMemory (running summary plus a verbatim recent-turn buffer) and ranks with the unstructured summarizers, below every structure-preserving compactor.

And the kit does not need to run your compactor at all. Run your product's /compact, save the output, and score it:

compaction-kit score --compacted output.txt --name my-product

Default ground truth is the seeded session's canaries; --canaries canaries.json scores against your own definitions for your own transcripts. Details: sim/ADAPTERS.md.

The spike gate

This kit exists only because it passed a kill criterion set before the build: it had to separate a lossy compaction from a structure-preserving one (flag below 50%, stay silent above 90%, and rank them in ground-truth order for every type at every round), or stop. It passed on all four checks, and the lossy survival curve decays monotonically, the same shape as the published 53% → 10% measurement. The criterion is encoded as tests in tests/test_spike.py, so a future change that breaks the separation breaks the build.

Layout

File What it does
src/compaction_kit/canaries.py Canary types and the seeded set
src/compaction_kit/session.py Scripted session with known canary positions
src/compaction_kit/corpus.py Randomized multi-seed session generator
src/compaction_kit/semantic_corpus.py Development and held-out paraphrased-update conflicts and distinct near-duplicate items
src/compaction_kit/semantic.py Dependency-free semantic update resolver (SemanticChecklistCompactor)
src/compaction_kit/benchmark.py Fixed-budget benchmark: randomized + semantic suites, compliance, and leaderboard ranking
src/compaction_kit/adapters.py External-pattern adapters: LangChain-style progressive summary, and replay of precomputed output
src/compaction_kit/transcripts.py External transcript loading, custom canary loading, and scoring of externally produced output
src/compaction_kit/compactors.py The Compactor protocol and reference implementations, including update-aware checklist, pinned rules, and summary-plus-tail mitigations
src/compaction_kit/probes.py Direct-recall, behavior, and exact-use probes
src/compaction_kit/simulated_agent.py $0 retrieval agent for probing
src/compaction_kit/runner.py Iterative rounds and survival rates
src/compaction_kit/report.py Per-type findings, cliff rounds, JSON and markdown reports
demo.py Runnable demo: seeded session vs three compactors
DEMO.md Recorded demo output
SPEC.md Protocol specification
tests/test_spike.py The kill criterion as tests
tests/test_metric_hardening.py Cliff-round and exact-use tests
tests/test_corpus.py Multi-seed corpus and supersession tests
tests/test_mitigations.py Mitigation comparison tests
tests/test_semantic_corpus.py Semantic-conflict corpus tests, including the earlier compactors' diagnostic failure
tests/test_semantic.py Semantic resolver classification, stale-drop, false-merge, and held-out success tests
tests/test_budget.py Budget protocol compatibility, compliance, priority, and benchmark ranking tests
tests/test_adapters.py External adapter, transcript loading, and score-command tests

Extending it is one class at a time: a new compactor implements the protocol, a new probe implements probe(canary, context_text).

Status

v0.3 release. Python 3.11+, zero dependencies, zero model spend for the default path. MIT license. The 0.3.0 release adds the fixed-budget benchmark and external adapters, including a LangChain-style progressive summarizer and scoring for compacted output produced by other systems.

Not a compaction fix. A measurement. Fixes are easier to trust once something independent can say what they preserve, and what they lose.

Metadata

Release files for compaction-conformance-kit 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for compaction-conformance-kit 0.3.0
File Size Uploaded
compaction_conformance_kit-0.3.0.tar.gz 49.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for compaction-conformance-kit 0.3.0
File Interpreter ABI Platform
compaction_conformance_kit-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 99.2 kB

Release files / compaction_conformance_kit-0.3.0.tar.gz

Download URL compaction_conformance_kit-0.3.0.tar.gz
Size 49.4 kB
Tags Source
SHA-256 checksum
How to use checksums
be6f5b58428d84ce2bec9a949ad26ca21080c1e56ecef4edeed322820b3a933b
BLAKE2b-256 checksum
How to use checksums
9903ab3ec4edc93075e788e25891e1a4941348923097a85b3e54aa0c9bb093ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / compaction_conformance_kit-0.3.0-py3-none-any.whl

Download URL compaction_conformance_kit-0.3.0-py3-none-any.whl
Size 49.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b8596972fe266c22713cb330d495e26e412354a1fb221b00bcbbdd15bd88bcc
BLAKE2b-256 checksum
How to use checksums
a111be2c18f7a683d0adb1a63ed5070930da2d949faedf7a52dd3f7f9e71eec3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.1

2 release files

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page