diffledger
Diff-triggered retrieval for compression-resilient agent sessions.
Standard RAG fires on the wrong trigger after a context compression event. It asks "what is similar to this message?" The right question is "what is the user referring to that the agent no longer holds?" These fail differently — especially on anaphoric references like "how does it work again" or "the first thing we set up."
diffledger maintains an external ledger of what the agent has been told this session. Before each message, it extracts the concepts being referenced, diffs them against the ledger, and fills any gaps via vector retrieval before the agent responds.
29% → 96% on post-compression turns (5 constructed cases, keyword-presence scoring).
Install
pip install diffledger
Requires Ollama with qwen3:1.7b:
ollama pull qwen3:1.7b
CLI
diffledger status # show current ledger state
diffledger inject "how does it work again" # run gap detection for a message
diffledger crunch # simulate a compression event
diffledger reset # clear the ledger
diffledger extract "your message here" # dry-run concept extraction
Python API
from diffledger import Ledger, detect_gaps, fill_gaps, detect_crunch
ledger = Ledger("my-session")
# after a compression event
if detect_crunch():
ledger.mark_all_stale()
# for each incoming message
gaps = detect_gaps("how does it work again", ledger)
fills = fill_gaps(gaps, ledger) # queries your vector store, injects results
ledger.advance_turn()
ledger.save()
# fills is a list of {"concept": str, "text": str}
# inject these into the agent's context before it sees the message
Environment variables
| Variable | Default | Description |
|---|---|---|
DIFFLEDGER_SESSION |
"default" |
Session ID for ledger file |
DIFFLEDGER_HYPHAE_URL |
http://127.0.0.1:8100 |
Vector store /recall endpoint |
DIFFLEDGER_OLLAMA_URL |
http://127.0.0.1:11434 |
Ollama endpoint |
DIFFLEDGER_MODEL |
qwen3:1.7b |
Concept extraction model |
DIFFLEDGER_STALE_TURNS |
30 |
Turns before an entry is re-injectable |
DIFFLEDGER_GAP_MAX |
2 |
Max gap fills per turn |
DIFFLEDGER_JACCARD |
0.50 |
Jaccard threshold for fuzzy concept matching |
How it works
- Ledger: a JSON file per session tracking which concepts have been injected, at which turn, and with what reference count.
- Extraction:
qwen3:1.7bvia Ollama reads the incoming message and extracts named concepts (~1s on CPU). - Diff: extracted concepts are checked against the live ledger entries. Anything missing is a gap.
- Fill: each gap triggers a vector search against your retrieval backend. Results are injected into the prompt and recorded in the ledger.
- Crunch handling: when a compression event is detected (via transcript scan or manual call), all entries are marked stale. Post-crunch turns use resolution mode: the model picks from a numbered list of stale concepts, handling ordinals and pronouns ("the first thing", "how does it work") that carry no embedding signal.
Vector store interface
diffledger expects a /recall POST endpoint:
POST /recall
{"query": "concept name", "top_k": 1}
→ {"results": [{"text": "..."}]}
Compatible with Hyphae and anything with the same interface.
Paper
Diff, Don't Retrieve: A Context Ledger for Compression-Resilient Agent Sessions
License
MIT
Metadata
Release files for diffledger 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| diffledger-0.1.1.tar.gz | 8.4 kB | Without provenance | Details for diffledger-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| diffledger-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for diffledger-0.1.1-py3-none-any.whl |
Total release size: 18.4 kB
Release files / diffledger-0.1.1.tar.gz
| Download URL | diffledger-0.1.1.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
345fcda63c9da45fad6c44d2a1d93a821ad8c6aa06f2ed95ceb306f8becb8599
|
|
BLAKE2b-256 checksum How to use checksums |
c79ceefff256aa4314f52710b19d3465cb9c3622e6277ea7e75dca71e2d1579e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / diffledger-0.1.1-py3-none-any.whl
| Download URL | diffledger-0.1.1-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2a684be20e53557fede40453c2ebd18f8948b746c487795aa10e794af7981f26
|
|
BLAKE2b-256 checksum How to use checksums |
84506a0665998cd3896d4b0fb7615f9e992306fc600ca0688b540cb92c35624c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for diffledger 0.1.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.