🛡️ DreamGuard
Reversible memory compilation for AI agents.
Long-running agents need to compress their memory ("dreaming" / consolidation) or the context window bloats. The common approach rewrites memory in place: once a detail is summarised away, it is gone for good.
DreamGuard compiles raw episodic memory into compact semantic memory like a build step: the raw episodes are never deleted, and every compressed fact is linked back to its source episodes. If recall quality drops, you roll back and the exact raw context is restored.
record() ──► episodes (immutable)
│ dream()
▼
semantic_memories ──provenance──► episodes
│ rollback()
▼
compressed facts deactivated, raw context restored
Demo
Run the dashboard and open http://localhost:8000. It plots detail-recall accuracy over 50 turns for three systems and lets you trigger a rollback:
| System | What it does | Behaviour in the demo |
|---|---|---|
| Full raw context (reference) | Never compresses | 100%, but context grows without bound |
| In-place dreaming | Rewrites memory, deletes raw data | Loss compounds, cannot recover |
| DreamGuard | Compresses, keeps raw + provenance | Same loss as in-place, then recovers to 100% on rollback |
📸 Add a screenshot or GIF here:
docs/demo.gif
Install
pip install dreamguard # core library, zero dependencies
pip install "dreamguard[server]" # + dashboard, API and Groq support
pip install "dreamguard[groq]" # + Groq compressor only
Quick start
pip install "dreamguard[server]"
echo "GROQ_API_KEY=gsk_..." > .env # only needed for the /live demo
dreamguard serve # open http://localhost:8000
dreamguard benchmark # offline simulation in the terminal
Development setup: git clone ... && pip install -e ".[dev,server]" && pytest.
Use it as a library
from dreamguard import MemoryEngine, HeuristicCompressor
mem = MemoryEngine(HeuristicCompressor(retention=0.7), destructive=False)
mem.record(1, "api_port=8421")
mem.record(2, "region=ap-south-1")
mem.dream() # compress; raw episodes + provenance are kept
print(mem.context()) # what your agent would see (digest)
mem.rollback() # back to exact raw episodes
print(mem.context())
Live demo with a real LLM (Groq)
# .env
GROQ_API_KEY=gsk_...
Run uvicorn dreamguard.api:app --reload, open http://localhost:8000/live, then click:
1 Load scenario → 2 Evaluate → 3 Dream → 2 Evaluate → 4 Rollback → 2 Evaluate.
Groq compresses 12 engineering-log episodes into a short digest, then answers 8 factual questions (ports, ticket owners, hosts...) using only each system's remaining memory. Answers are graded against known ground truth. Both the in-place and DreamGuard engines get the same input; only DreamGuard can roll back. Set GROQ_MODEL to change the model (default llama-3.1-8b-instant).
Results are real LLM output, so they vary by model and run: a strong model may retain some details after compression. The rollback guarantee does not vary, because it restores the raw text.
Real LLM compression (optional)
# .env
COMPRESSOR=groq
GROQ_API_KEY=gsk_...
Uses Groq's llama-3.1-8b-instant to produce the digest. Note: the built-in benchmark always uses the heuristic compressor so that its results are reproducible.
How it works
| Table | Purpose |
|---|---|
episodes |
Raw, append-only memory (turn, content) |
semantic_memories |
Compressed digests, with an active flag |
provenance |
memory_id → episode_id links from each digest to its sources |
record(turn, content)appends an episode.dream()compresses episodes not yet covered by an active digest.rollback()sets all digests to inactive. Their episodes become "uncovered" again, socontext()returns the raw text.context()is what an agent reads: active digests plus any uncovered raw episodes.- With
destructive=Truethe engine mimics in-place dreaming: it deletes raw data and re-compresses digests together with new episodes, so loss compounds.
Benchmark methodology (read this before quoting numbers)
The benchmark is a synthetic simulation, not a measurement of any production system:
- Each turn records one exact detail, e.g.
param_7=48213. - Every 10 turns the memory is compressed with a seeded lossy compressor that keeps each value with probability 0.7 and otherwise keeps only the key.
- After every turn, recall = fraction of all details seen so far that appear verbatim (
key=value) in the agent's visible context.
What this shows: if compression is lossy, an irreversible design loses information permanently, while a design that keeps raw data can restore it. What it does not show: how any specific LLM, product, or published system behaves. The absolute accuracy numbers come from the chosen retention parameter, not from real-world measurement.
Known trade-off: after a rollback, the agent context is raw again and therefore larger. The benefit of compression (smaller context) is traded for recall until the next dream.
API
| Method | Path | Description |
|---|---|---|
| POST | /api/record |
{"content": "..."} record an episode |
| POST | /api/dream |
Compress uncovered episodes |
| POST | /api/rollback |
Deactivate compressed memory |
| POST | /api/reset |
Clear the live engine |
| POST | /api/demo/seed?n=10 |
Add demo episodes |
| GET | /api/memory |
Episodes, facts, provenance, context size |
| GET | /api/benchmark |
Run benchmark (seed, retention, auto_rollback, rollback_at) |
Interactive docs at /docs.
Project layout
dreamguard/
├── dreamguard/
│ ├── db.py # SQLite schema
│ ├── compiler.py # Heuristic + Groq compressors
│ ├── engine.py # record / dream / rollback / evaluate
│ ├── benchmark.py # reproducible simulation
│ ├── live.py # live demo: Groq compression + Groq QA grading
│ ├── api.py # FastAPI app
│ ├── cli.py # `dreamguard` command
│ └── static/ # dashboard (HTML + CSS + Chart.js, no build step)
├── tests/
└── pyproject.toml
Testing
pip install -e ".[dev,server]"
pytest
Roadmap
- Selective rollback (restore only the episodes behind a specific fact)
- Benchmark with real LLM compression and an LLM-answered QA task
- Pluggable storage backends (Postgres, vector DBs)
- Rollback triggers driven by task-level eval signals
Contributing
Issues and PRs are welcome. Please run pytest before submitting.
License
MIT, see LICENSE. Built by Manojkumar V.
Metadata
Release files for dreamguard 0.1.0
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 | |
|---|---|---|---|---|
| dreamguard-0.1.0.tar.gz | 22.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dreamguard-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.3 kB
Release files / dreamguard-0.1.0.tar.gz
| Download URL | dreamguard-0.1.0.tar.gz |
|---|---|
| Size | 22.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a36261f34d550fe5caf385da79f5f14d708fec47cbd3fe4bb351e1c6e915d87b
|
|
BLAKE2b-256 checksum How to use checksums |
b77178b215cbc659f8a404235b3e14d6af6f838ff6006a81f03e319ce4c003e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.6
|
Release files / dreamguard-0.1.0-py3-none-any.whl
| Download URL | dreamguard-0.1.0-py3-none-any.whl |
|---|---|
| Size | 22.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c16050e8c2315b17d3f227638539d9e7dbe2ee414f4592bf824b692bbdb17cb
|
|
BLAKE2b-256 checksum How to use checksums |
b5abfd3a24153d659ea677cf5b09d4025a05e722a2ef33ebc1d067f644c83f48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.6
|
Security for dreamguard 0.1.0
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.