Skip to main content

Diff-triggered retrieval for compression-resilient agent sessions

Project description

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

  1. Ledger: a JSON file per session tracking which concepts have been injected, at which turn, and with what reference count.
  2. Extraction: qwen3:1.7b via Ollama reads the incoming message and extracts named concepts (~1s on CPU).
  3. Diff: extracted concepts are checked against the live ledger entries. Anything missing is a gap.
  4. Fill: each gap triggers a vector search against your retrieval backend. Results are injected into the prompt and recorded in the ledger.
  5. 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffledger-0.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diffledger-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file diffledger-0.1.1.tar.gz.

File metadata

  • Download URL: diffledger-0.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for diffledger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 345fcda63c9da45fad6c44d2a1d93a821ad8c6aa06f2ed95ceb306f8becb8599
MD5 afb984f58a4d7f9707c25c0f8bc7b865
BLAKE2b-256 c79ceefff256aa4314f52710b19d3465cb9c3622e6277ea7e75dca71e2d1579e

See more details on using hashes here.

File details

Details for the file diffledger-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: diffledger-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for diffledger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a684be20e53557fede40453c2ebd18f8948b746c487795aa10e794af7981f26
MD5 bdeb05f0cc05ec664aa95c103d97848b
BLAKE2b-256 84506a0665998cd3896d4b0fb7615f9e992306fc600ca0688b540cb92c35624c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page