Generated banner for Engrava
Generated logo for Engrava

Engrava

Memory frameworkOfficial Sovantica project

Created by Sovantica

Published in sovantica/engrava. Publisher page

12 GitHub stars

What it is

The memory database for AI agents - graph memory, hybrid search, tamper-evident thought/edge journal.
— the description by Sovantica

AutoGineer overview, from the brain’s own files

Engrava is an in-process memory database designed for AI agents, built on SQLite. It provides a structured way to store and retrieve agent thoughts using a graph-based model, where thoughts are linked by typed edges. The system supports hybrid search, combining vector similarity, full-text search (FTS5), recency, and priority. It includes built-in mechanisms for memory consolidation (dreaming), memory hygiene (archiving and garbage collection), and a tamper-evident journal for tracking mutations. Engrava operates as a library with no external service dependencies in its core path, allowing agents to maintain persistent, queryable state locally.

How it works

Memory model: Hybrid

AutoGineer overview, from the brain’s own files

Engrava uses a SQLite database to store thoughts and their relationships. It organizes data into thoughts and typed edges, which can be queried using the MindQL language. The system supports hybrid search by combining vector embeddings, keyword search, and metadata signals. It manages memory through a lifecycle that includes archiving and garbage collection, and it can consolidate memories into reflections. The system is extensible via hooks and provides an MCP server for integration with various AI agent frameworks.

Folders
FolderRoleFiles
.githubagent-config16
docsdocs37
examplesexamples13
scriptsscripts24
srcsource-code89
teststests201
.githooksfolder1

The agent reads

Requirements

Runtimes: Python >=3.11

Databases: SQLite

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Google Antigravity, Kiro, OpenCode, Amp, Roo Code, Cline, JetBrains Junie, Devin (ex-Windsurf)

Set it up

  1. This brain runs code automatically: 2 scripts. Review .githooks/commit-msg, Makefile before opening the folder in Claude Code. No step below turns a hook on for you.

    • .githooks/commit-msg: runs on git commit-msg
    • Makefile: echo "$(BLUE)>> Installing engrava + dev dependencies$(RESET)"; $(PIP) install --upgrade pip setuptools wheel; $(PIP) install -e ".[dev]"; bash scripts/install_hooks.sh; echo "$(GREEN)>> Done. Run 'make check' to verify quality gate.$(RESET)"
  2. Not scanned by AutoGineerIts dependencies are installed by pip or uv when you run it and were not scanned by AutoGineer (5 declared). https://pypi.org/project/engrava/
  3. Created by Sovantica. Published in sovantica/engrava. Licence: MIT License.

Usage

AutoGineer overview, from the brain’s own files

  • Persistent memory for autonomous AI agents
  • Personal knowledge base management
  • Conversation storage with semantic retrieval
  • Research notes with associative linking
  • Memory-backed agent development
  • Audit-trailed thought and edge tracking
“Store a new observation about a project requirement.”
The agent uses the remember() method to persist the thought, which is then indexed for future hybrid search and graph traversal.
“Retrieve relevant context for a coding task.”
The agent uses the recall() method, which performs a hybrid search across FTS5, vector, and graph signals to return ranked, contextually relevant thoughts.
“Query the memory graph for all observations with high priority.”
The agent executes a MindQL query: FIND thoughts WHERE thought_type = 'OBSERVATION' AND priority = 'P1'.
“Consolidate old memories to free up space.”
The agent triggers the DreamingExtension to cluster related thoughts and compute reflections, or uses the memory hygiene loop to archive low-signal thoughts.

Documentation by Sovantica

Engrava

The memory database for AI agents.

A queryable memory graph with bi-temporal valid-time predicates — in an in-process Python library over one SQLite file, with no generative-model call in the core write path.

Image: CI Image: PyPI Image: Python Image: License: MIT

Engrava is a standalone, in-process database for AI agent memory. Built on SQLite, it provides thought CRUD, edge-based knowledge graphs, embedding-based similarity search, full-text search (FTS5/BM25), and a declarative extension system — all in a single package with zero external service dependencies.

Benchmark results are published, and the runs are reproducible from a separate repository. Every published run is Group A — memory_pipeline_llms: [], no language model anywhere in the memory layer's built-in path. That is a property of the architecture's default signals and hooks rather than a measurement, unless a custom Dreaming signal or Memory Hygiene hook is configured to call one.

  • Benchmark results — the live table: every published row with the comparability segment it belongs to.

  • engrava-benchmark — the runner. Clone it and reproduce a result against the package from PyPI. MIT.

  • Discussions — questions; reproduction questions belong in the Q&A category.

Use Cases

  • AI agent persistent memory

  • Personal knowledge base

  • Conversation storage with semantic search

  • Research notes with associative linking

  • Any application that needs a thought-graph with embeddings

Quick Start

Installation
pip install engrava

Optional extras:

pip install 'engrava[vec]'                # sqlite-vec vector search backend
pip install 'engrava[embeddings-local]'   # sentence-transformers embeddings (local model)
pip install 'engrava[embeddings-openai]'  # OpenAI-compatible embeddings API
pip install 'engrava[embeddings-ollama]'  # Ollama local embeddings server
pip install 'engrava[embeddings-hf]'      # HuggingFace Inference API embeddings

Dreaming/consolidation and the knowledge graph need no extra — they are part of the base install.

embeddings-local carries a large first-run download. It pulls sentence-transformers and torch, plus a further, separate model download (~88 MB for all-MiniLM-L6-v2, cached under ~/.cache/huggingface/hub) on first use. On Linux, the PyPI build of torch also pulls the NVIDIA CUDA libraries as dependencies: several gigabytes, most of it usable only with an NVIDIA GPU. Without one, install the CPU-only build first (see Configuration: No NVIDIA GPU for the exact command and numbers). engrava-mcp[local] is the exact same download, reached through the server package instead of this one. If you do not need semantic search in-process, pip install engrava alone (no extra, no download, no model) already gives you keyword search, the graph, and MindQL — see Configuration → Quick-start profiles for that and the Ollama-backed alternative that keeps the model out of this process entirely.

Basic Usage

Store a memory and search for it in two calls — no IDs to generate, no record to assemble:

import asyncio

import aiosqlite

from engrava import SqliteEngravaCore


async def main() -> None:
    # SqliteEngravaCore wraps an open aiosqlite connection.
    async with aiosqlite.connect(":memory:") as conn:
        conn.row_factory = aiosqlite.Row
        store = SqliteEngravaCore(conn)
        await store.ensure_schema()

        await store.remember("Python is great for AI agents")
        await store.remember("SQLite needs no server")

        result = await store.recall("what language is good for agents?")
        for thought_id, score in result.results:
            thought = await store.get_thought(thought_id)
            if thought is not None:
                print(f"{thought.essence}  (score: {score:.3f})")


asyncio.run(main())

remember() stores the text as a thought (generating its ID for you) and returns the stored ThoughtRecord; recall() runs the same hybrid search as search_hybrid() and returns the ranked results. This store has no SearchConfig of its own, and it still ranks with the same default weights — FTS 0.30, vector 0.55, recency 0.10, priority 0.05, graph 0.00 (opt-in) — as a store built with one; see Hybrid Search. For full control — setting priority, thought type, metadata, or the cognitive cycle on a write — build a ThoughtRecord yourself and call create_thought().

From here, link thoughts with typed edges, query them with MindQL, or run the full ingest → dream → search tour in the Quick Start guide.

Configuration-Driven Setup
from engrava import SqliteEngravaCore

# from_config opens and OWNS the connection — use it as an async context manager.
async with await SqliteEngravaCore.from_config("engrava.yaml") as store:
    # The schema is already applied by from_config.
    thought = await store.get_thought("some-id")

See docs/configuration.md for the full YAML schema.

Upgrading

Automatic schema migration runs on first connection. See the upgrade guide for compatibility notes, backup guidance, and troubleshooting steps.

Features

Thought CRUD

Create, read, update, and archive thoughts with full lifecycle management. All models are frozen Pydantic objects — mutations happen via evolve().

Edge-Based Knowledge Graph

Link thoughts with typed, weighted edges. Edge types include ASSOCIATED, DEPENDS_ON, DERIVED_FROM, CONSOLIDATED_FROM (created by dreaming), and CONTESTED_BY.

Store embeddings alongside thoughts and search them with the built-in NumPy cosine backend or the optional sqlite-vec backend (pip install 'engrava[vec]'). Pluggable embedding providers:

ProviderExtraBackend
SentenceTransformerProviderembeddings-localLocal model via sentence-transformers
OpenAICompatibleProviderembeddings-openaiAny OpenAI-compatible API
OllamaProviderembeddings-ollamaLocal Ollama server
HuggingFaceProviderembeddings-hfHuggingFace Inference API
CallbackProvider(built-in)Custom callable
Full-Text Search (FTS5)

SQLite FTS5 virtual table with BM25 ranking. Hybrid search combines vector similarity, text relevance, recency, priority, and graph connectivity. Signals that cannot run for a query are skipped and the remaining weights are redistributed.

MindQL Query Language

Declarative query language for the thought-graph:

FIND thoughts WHERE thought_type = 'OBSERVATION' AND priority = 'P1' LIMIT 10
COUNT thoughts WHERE lifecycle_status = 'ACTIVE'
SELECT thought_id, essence FROM thought WHERE thought_type = 'BELIEF'

Extensible with custom commands through MindQLExecutor or an ExtensionManifest; the lifecycle hook registry is reserved and is not consulted by the core executor.

Extension System

Plug into the thought lifecycle via EngravaHooksProtocol. Subclass DefaultEngravaHooks when you only need selected active methods:

from engrava import DefaultEngravaHooks, ThoughtRecord

class MyHooks(DefaultEngravaHooks):
    async def on_store(self, thought: ThoughtRecord) -> ThoughtRecord:
        # Observe or enrich the object returned after persistence.
        return thought

    async def decay_function(
        self, thought: ThoughtRecord, elapsed_cycles: int
    ) -> float:
        # Supply a decay multiplier to an enabled Memory Hygiene pass.
        return 1.0

Core currently invokes on_store, on_retrieve, and decay_function. on_store runs after the source thought's row is inserted; changing its return value does not rewrite the persisted row. score_function and mindql_extension_registry() remain reserved protocol methods and are not called by core.

Dreaming / Memory Consolidation

Built-in DreamingExtension for periodic memory consolidation — scores thoughts via its default signals (no LLM calls), promotes high-value entries, and creates REFLECTION thoughts by clustering semantically related thoughts and computing centroid embeddings through a deterministic structural function, not an LLM. A custom signal you register with DreamingExtension runs whatever code it contains. Available since 0.3.0.

→ See docs/benchmarks.md for reproducible evidence (synthetic benchmark suite runnable in ~5 minutes).

Forgetting / Memory Hygiene

The subtractive half of memory maintenance, paired with Dreaming: an opt-in loop whose built-in scoring makes no LLM calls, that archives cold, low-signal thoughts — the default action, reversible via restore_thought — and, as a separately opted-in step, garbage-collects them (not reversible) once both restore windows have elapsed under their non-zero defaults — a cycle count and a wall-clock duration, either of which can be configured to 0 to disable that window. OFF by default; once enabled, archived thoughts drop out of default retrieval and can be restored (restore_thought / include_archived).

→ See docs/memory-hygiene.md for the loop, protection, restore windows, and the honest deletion posture.

Tamper-Evident Thought/Edge Journal

Opt-in hash-chain journal that records thought and edge mutations (plus action status/verification_status transitions) as SHA-256-linked, before/after entries — a tamper-evident thought/edge journal, not a whole-database audit (embeddings and action creation are not covered). Off by default, one config flag to enable. Query history with store.journal.get_entries(...) and validate the chain with store.verify_journal(), which audits whatever chain is on disk independent of the current journal.enabled state — the store.journal writer handle does not exist while journaling is off, but the chain earlier sessions wrote is still in journal_entry and still needs verifying.

→ See docs/audit-trail.md for enabling, querying, verification, and the security model (what "tamper-evident" does and does not guarantee).

Multi-Service Isolation

Run multiple independent databases under one EngravaManager:

from pathlib import Path

from engrava import EngravaManager

async with EngravaManager(data_dir=Path("./data")) as mgr:
    agent_a = await mgr.get_store("agent-a")
    agent_b = await mgr.get_store("agent-b")
    # Completely isolated databases
MCP Server

Want Engrava as a memory server for your agent? The MCP server ships as its own package, engrava-mcp — a native stdio server (no HTTP shim) with read tools, optional write tools, attachable engrava:// resources, and guided prompts, for any MCP client (Claude Desktop, Claude Code, Cursor, Windsurf, VS Code):

uvx engrava-mcp        # or: pip install engrava-mcp

engrava-mcp pulls engrava in transitively, so installing it also gives you the import engrava library. See the engrava-mcp package for install, client configuration, the full tool/resource/prompt reference, and read-only mode.

uvx vs. a persistent install. uvx engrava-mcp (equivalently uv tool run engrava-mcp) installs into "an ephemeral virtual environment in the uv cache directory" per uv's own --help text — fine for the lexical/network profiles above, which add nothing heavier than httpx. Once you are on the local profile (engrava-mcp[local], the same sentence-transformers + torch download as engrava[embeddings-local]), uv tool install 'engrava-mcp[local]' is the better fit: it installs once into a persistent environment — the same uvx engrava-mcp invocation then reuses that installed environment instead of resolving a fresh ephemeral one — and a later uv tool upgrade engrava-mcp re-pays only the changed packages, not the whole dependency tree.

CLI

engrava --db mydata.db info          # Database stats
engrava --db mydata.db query "FIND thoughts WHERE thought_type = 'OBSERVATION' LIMIT 5"
engrava --db mydata.db snapshot -o backup.jsonl
engrava --db mydata.db restore -i backup.jsonl
engrava --db mydata.db gc            # Collect ARCHIVED thoughts + their edges/embeddings/actions
engrava --db mydata.db migrate       # Ensure schema is up-to-date
engrava --db mydata.db export -o portable.json

gc physically deletes ARCHIVED thoughts together with every edge touching one on either end — including edges whose other end is still live — their embeddings and the actions sourced from them, then reconciles the vector index by removing every vec0 row no embedding row owns; on a vec0-indexed store where sqlite-vec cannot be loaded — most commonly because engrava[vec] is not installed — a pass that is about to delete stops before deleting anything and exits 1 rather than stranding those vectors in an index nothing can then reach them through.

engrava info's --format json output carries every field of the metrics snapshot exposed by await store.metrics(), with the snapshot's own schema version renamed metrics_schema_version, plus two fields the snapshot itself does not carry, db_path and database_schema_version — see Upgrade Guide. The default text output is a shorter summary of that same data, not the full snapshot.

See the CLI reference for every command and option.

Architecture

  • SQLite with WAL mode for concurrent reads

  • Frozen Pydantic models — immutable domain objects

  • Async-first — all I/O via aiosqlite

  • Hook-based extension — zero monkey-patching

  • Template method pattern — subclass SqliteEngravaCore for extended schemas

  • Zero external services — everything runs locally in-process

Documentation

  • Documentation Index — choose a path by task: learn, build, operate, extend, or migrate

  • Core Concepts — the mental model (thought, edge, reflection, cycle, …) — start here

  • The Bi-temporal Model — the optional valid-time axis: query a fact as of any instant, invalidate without deleting

  • Positioning — when Engrava is (and isn't) the right tool, and how it compares

  • Quick Start — 5-minute setup guide

  • Tutorial — build a small notes memory end to end

  • Recipes — copy-paste snippets for common tasks (store a turn, retrieve context, TTL, dedup, …)

  • Building a memory-backed agent — the end-to-end agent turn loop (ingest → retrieve → generate → consolidate)

  • Migrating from another memory system — concept mapping, porting calls, bulk import, and scoping/multi-tenancy

  • Embeddings — wiring a real embedding provider (local / OpenAI / Ollama / HuggingFace / custom)

  • MCP server (engrava-mcp) — expose a store to MCP clients (Claude Desktop, Claude Code, Cursor, Windsurf, VS Code) via the standalone server package: install, run, client config, tools/resources/prompts

  • Configuration — YAML config format and options

  • Upgrade Guide — compatibility matrix, backups, and troubleshooting

  • Extensions — Writing custom extensions and hooks

  • Observability — Metrics snapshot API

  • Audit Trail — Tamper-evident hash-chain journal (enabling, querying, verifying, security model)

  • Evidence and Conflicts — model claims, provenance, contested facts, and caller-owned clarification

  • Security — trust boundaries, data egress, tenant isolation, encryption posture, and journal limits

  • Error Handling and Recovery — decide when to retry, repair input, or replace a store

  • Benchmarks — release gates, versioned observed values, and LongMemEval

  • API Reference — Full protocol and class reference

  • CLI Reference — every engrava command and option

  • Glossary — quick definitions of every Engrava term

  • MindQL — Query language syntax and examples

  • Troubleshooting — symptom → cause → fix for common errors

  • FAQ — quick answers (LLM/keys, embeddings-optional, scale, concurrency, backups, …)

  • Performance & Scaling — the vector-backend switch, bulk-ingest, and dreaming cost at scale

  • Data Lifecycle & Retention — lifecycle states, TTL, archive-vs-delete, GDPR erasure, disk reclamation

  • Deployment — process model, database files on disk, containers, graceful shutdown

  • Concurrency — the WAL single-writer model, busy timeout, and per-service isolation

  • Backup & Recovery — WAL-safe backups, snapshot vs file copy, restore verification

  • Known Limitations — Platform notes and constraints

Development

make install   # deps + dev extras + local git hooks -- from the primary checkout
make gate      # fast: hooks-active + lint + format check + type check + goldens drift, ~70s
make check     # lint + format check + type check + the full test suite with coverage (unchanged; no hooks-active, no goldens drift)

make install also wires a commit-msg hook that lints the message of the commit a squash merge creates — including on a local branch that never opens a pull request, where nothing else does. It refuses to install from a linked worktree (core.hooksPath is shared across all of them). See CONTRIBUTING.md for what it covers and what it does not.

License

MIT — see LICENSE for details.

docs/dreaming.md

Dreaming — Memory Consolidation

Engrava's dreaming extension provides periodic memory consolidation: it evaluates eligible candidate thoughts, scores them against configurable signals, and — when the corresponding gates, thresholds, and caps permit — may promote qualifying ones by setting their priority to P1.

Dreaming runs outside the normal CRUD path — the consumer decides when to invoke run_consolidation() (after N cycles, in a cron job, or manually).

How memory consolidation works (the dreaming loop)

Think of a single memory's journey through an agent's lifetime. The first two steps — ingest and confirmation — happen on the normal write path as you use the store. The consolidation part is manual: when you call run_consolidation(), that one call runs promotion → edge creation → reflection clustering/creation → an orphan sweep, in order.

  ingest        you create an OBSERVATION ("user prefers email")   (write path)
    │
    ▼
  confirm       the same fact is re-encountered over time, so its  (write path)
    │           confirmation_count grows (e.g. via deduplicate=True)
    │
    ▼  run_consolidation(current_cycle=N)   ── manual ──
    │
  ┌─┴───────────────────────────────────────────────────────┐
  │ 1. promote   thoughts that pass the gates and clear       │
  │              promote_threshold are raised to priority P1  │
  │ 2. link      a promoted thought *may* gain ASSOCIATED      │
  │              edges to similar neighbours (when enabled)    │
  │ 3. reflect   related thoughts *may* be clustered into      │
  │              REFLECTION meta-thoughts (when enabled)       │
  │ 4. sweep     stale REFLECTIONs whose sources left the      │
  │              active set are retired                         │
  └─┬───────────────────────────────────────────────────────┘
    │
    ▼
  improved      later searches rank the P1 memory higher (priority
  retrieval     signal), follow any new edges (graph signal), and can
                surface a REFLECTION instead of many raw thoughts

Walking the journey:

  1. Ingest. You store memories as thoughts (typically OBSERVATIONs) on the normal write path. Dreaming does nothing yet.

  2. Confirm. As the same knowledge recurs, its confirmation_count rises — automatically when you write with deduplicate=True (identical content collapses and bumps the count), or via your own logic. This is evidence the memory matters, and it feeds dreaming's confirmation signal. (Distinct from confidence, the static belief-strength you set — see Core Concepts.)

  3. Promote. When you run consolidation, each candidate must first pass the gates (e.g. old enough, enough confirmations) and then score above promote_threshold across the weighted signals. Survivors are promoted to P1. (Both bars matter: a thought that passes the gates but scores low is not promoted — see Troubleshooting.)

  4. Link. A promoted thought may gain ASSOCIATED edges to similar neighbours — when edge creation is enabled, the thought has a stored embedding, and qualifying neighbours (above min_similarity) are found. New edges persist the structure in the graph, idempotently (re-runs don't duplicate edges).

  5. Reflect. Related thoughts may be clustered and summarised into REFLECTION meta-thoughts — a centroid embedding plus CONSOLIDATED_FROM edges back to the members — when reflections are enabled and eligible clusters pass the clustering/quality gates. This turns a pile of observations into fewer, higher-level memories. (A REFLECTION whose source cluster later leaves the active set — or is hard-deleted — is automatically retired, unless it is pinned or protected, so a stale summary can't resurface; see the orphan sweep.)

  6. How it shows up in retrieval. All of this changes future hybrid search mechanically: the P1 memory ranks higher via the priority signal, any new edges feed the opt-in graph signal, and reflections let one high-level memory stand in for many raw ones. These are deterministic ranking effects — Engrava makes no claim that dreaming improves retrieval accuracy on any benchmark (see Known Limitations).

The rest of this page is the knob-by-knob reference for each phase.

Quick start

from engrava.config import DreamingConfig, DreamingGates
from engrava.extensions.dreaming import DreamingExtension

config = DreamingConfig(
    enabled=True,
    promote_threshold=0.55,
    gates=DreamingGates(
        allow_zero_confirmation=True,
        min_age_cycles=1,
    ),
)
ext = DreamingExtension(config=config)

# After ingesting thoughts into `store`:
result = await ext.run_consolidation(store, current_cycle=1)
print(f"Promoted {result.promoted_count} thoughts")
Attaching a hand-built extension to a store

Calling run_consolidation() on the extension directly (above) never needs the store to know about it. If you instead want store.consolidate() to run your hand-built extension — the same call site used by an from_config-built store — wire it on with attach_dreaming_extension():

store.attach_dreaming_extension(ext)
result = await store.consolidate(current_cycle=2)

This is the supported way to wire an extension onto a manually built store; it does not itself construct or configure a DreamingExtension. Calling it again replaces whatever was attached before — there is no separate error for a second call, and no way to detach through this method. attach_dreaming_extension() checks the argument against the runtime_checkable DreamingConsolidatorProtocol (which looks for a run_consolidation member) and raises TypeError if that check fails. It does not check the method's signature or that it is a coroutine function, so a same-named but wrong-shaped run_consolidation (sync instead of async, or a different signature) passes this check and fails only once consolidate() actually calls it.

From YAML config

If you enable dreaming in engrava.yaml (extensions.dreaming.enabled: true) and build the store with SqliteEngravaCore.from_config(...), you do not need to construct a DreamingExtension by hand — call consolidate() on the store directly:

from engrava import SqliteEngravaCore

async with await SqliteEngravaCore.from_config("engrava.yaml") as store:
    result = await store.consolidate(current_cycle=1)
    print(f"Promoted {result.promoted_count} thoughts")

store.consolidate(current_cycle=...) first flushes any pending access events (so the frequency signal sees the latest access counts this cycle), then runs the configured extension. It raises RuntimeError if dreaming is not enabled/wired on the store (built manually, or dreaming.enabled is false) — there is no extension to run.

schedule_every_n_cycles does not start a background scheduler. Applications with a cycle loop can call DreamingExtension.run_if_due(store, current_cycle); it runs only on positive cycle multiples while Dreaming is enabled and returns None otherwise. is_due(current_cycle) exposes the same decision without a write. Explicit store.consolidate() and run_consolidation() calls remain unconditional.

Gates

A thought is scored against the promotion threshold regardless; it must pass all active gates to be promoted. Gates are cheap boolean checks that filter out clearly ineligible candidates before a passing score can result in a promotion.

GateFieldDefaultDescription
Minimum agemin_age_cycles1current_cycle - created_cycle must be ≥ this value. Prevents promoting thoughts that were just created in the same cycle.
Confirmation countmin_confirmations2confirmation_count must be ≥ this value. Bypassed when allow_zero_confirmation is True (the default).
Max promotedmax_promoted_per_run20Cap on the number of promotions per consolidation run.
allow_zero_confirmation

When True (default), the confirmation gate is skipped entirely. This is essential for single-write batch-ingest scenarios where thoughts are stored once and never confirmed — without this flag, no thought would ever pass the confirmation gate and dreaming would be effectively dead.

Set to False only when your application explicitly tracks confirmations and you want to require at least min_confirmations experience-based validations before a thought is eligible for promotion.

Eligibility filters and corpus caps

After the age/confirmation gates, promotion and REFLECTION members also pass metadata-aware eligibility filters. They read caller-provided metadata and are not identity or authorization controls.

FieldDefaultEffect
eligible_perspectivesnullOptional allow-list for metadata.perspective: percept, utterance, thought.
self_filter_modeanyself_only / external_only use strict boolean metadata.source.is_self.
min_source_confidencelowMinimum metadata.source.confidence in low < medium < high order.
excluded_content_types["code"]Reject matching declared metadata.content_type values.
eligible_content_typesnullOptional allow-list for declared content types.

Missing perspective, is_self, or content-type annotations remain eligible for backward compatibility. Once a non-empty metadata mapping exists, missing or malformed source confidence is treated as low, so a higher configured threshold rejects it. A record with absent or empty metadata bypasses every metadata-driven filter, including the confidence threshold.

The remaining caps operate at different stages:

  • candidates_limit (default 200) bounds the ACTIVE promotion query and each type query used by agglomerative clustering;

  • max_promoted_per_run (default 20) limits writes in one promotion phase;

  • max_p1_fraction (default 0.05) limits the corpus-wide P1 population;

  • min_cluster_size / max_cluster_size (defaults 3 / 200) reject clusters that are too small or too broad — but not symmetrically: max_cluster_size is applied once, to the raw cluster, before eligibility filtering; only min_cluster_size is re-checked afterward, against the eligible member count. A raw cluster over max_cluster_size is rejected even if filtering would have brought it back under the cap;

  • clustering_min_new_candidates (default 50) skips repeat clustering when the eligible ACTIVE population has not grown enough. It does not skip signal scoring, promotion, or edge creation.

Signals

Signals compute a score in [0.0, 1.0] for each candidate thought. The candidate's promotion score is a weighted sum over the signals active for this run — an inactive signal's weight is set to zero (see below) — and that score is compared against promote_threshold.

SignalWeightDescription
recency0.25Exponential decay based on updated_cycle age.
staleness0.20Activity span (updated_cycle - created_cycle).
confirmation0.20Ratio of confirmation_count to max (5).
confidence0.15Thought's confidence field (default 0.5).
frequency0.20Ratio of access_count to max (10).
action_outcome0.15Thought's action_outcome_score — the mean outcome value over its terminal linked actions (None ⇒ contributes 0.0).

Which signals are active is decided once per run, over the whole candidate pool:

  • recency and staleness are always active: a consolidation run always has a cycle.

  • confirmation is active when some candidate has confirmation_count > 0.

  • confidence is active when some candidate has a confidence value.

  • action_outcome is active when some candidate has an action_outcome_score.

  • frequency is active when access_tracking_enabled is true and some candidate has access_count > 0 (see Access tracking below).

  • A custom signal registered via custom_signals under a new name is always active. One that reuses a default name follows that name's rule.

An inactive signal's weight is set to 0.0, and each active signal's weight is divided by the sum of the active weights. If no signal is active, or that sum is zero, every weight is 0.0 and nothing is promoted. The default weights sum to more than 1.0: they are relative priorities, not a probability distribution.

Custom signals can be provided via DreamingSignalProtocol. custom_signals only supplies the callable for a name — the name itself must also carry a weight in config.signals, or it is never resolved:

from engrava import ThoughtRecord, DreamingContext
from engrava.config import DreamingConfig
from engrava.extensions.dreaming import DreamingExtension


class MySignal:
    def __call__(self, thought: ThoughtRecord, ctx: DreamingContext) -> float:
        return 0.42


config = DreamingConfig(enabled=True)
config.signals["my_signal"] = 0.30  # required: registers the name and its weight
ext = DreamingExtension(
    config=config,
    custom_signals={"my_signal": MySignal()},
)
Access tracking (the frequency substrate)

The frequency signal scores a thought by its access_count. For that to be meaningful, reads have to be counted — otherwise access_count stays at 0 and frequency contributes nothing.

When you build the store via from_config with dreaming enabled, retrieval paths record access automatically, controlled by extensions.dreaming.access_tracking_enabled (default true). To keep the read path fast, access events are buffered and flushed at the cycle boundary rather than written on every read; store.consolidate() flushes the buffer before scoring so the current cycle sees up-to-date counts. Access tracking is deliberately not journaled — the counts are regenerable telemetry, not part of the tamper-evident chain (see Audit Trail).

Set access_tracking_enabled: false to stop counting new accesses. With it false, the frequency signal is always dropped and its weight redistributed over the remaining active signals — even on a store where candidates already carry a non-zero access_count from before tracking was turned off, since access_tracking_enabled gates the signal on its own, independent of any stored value.

After dreaming promotes thoughts to P1, the hybrid search search_hybrid() can use priority as a 4th scoring signal alongside FTS5, vector similarity, and recency.

The priority signal maps each thought's Priority enum to a boost multiplier:

PriorityDefault boost
P11.0
P20.6
P30.3
P40.0

The default priority weight is 0.05 (5% of the total score). Configure it via SearchConfig:

search:
  default_priority_weight: 0.05
  priority_boost_p1: 1.0
  priority_boost_p2: 0.6
  priority_boost_p3: 0.3
  priority_boost_p4: 0.0

To disable the priority signal entirely, set default_priority_weight: 0.0.

Edge creation

When dreaming promotes a thought to P1, it can also create ASSOCIATED edges connecting the promoted thought to its nearest neighbours by embedding similarity. This persists the dream's structural knowledge in the graph so it survives application restarts.

Edges are created with source=KnowledgeSource.DREAMING for attribution.

Configuration
extensions:
  dreaming:
    edges:
      enabled: true          # create edges on promotion (default: true)
      top_k: 1               # max neighbours per promoted thought
      min_similarity: 0.7    # cosine threshold for edge creation
      edge_weight_factor: 0.5  # edge.weight = factor × similarity
Idempotence

Re-running run_consolidation() on the same data does not create duplicate edges. Before creating an edge, the extension checks whether the promoted thought already has any edge connecting it to the candidate neighbour (regardless of type).

Edge weight formula
edge.weight = edge_weight_factor × cosine_similarity

With the default edge_weight_factor=0.5 and min_similarity=0.7, practical edge weights range from 0.35 to 0.50.

After dream-created edges exist in the graph, search_hybrid() can use them as a 5th scoring signal (graph signal). The signal uses 1-hop-weighted neighbour boost: if a candidate thought's graph neighbours also match the query, the candidate receives a boost proportional to the neighbour's score and the connecting edge weight.

Algorithm
candidate_scores = { C: max(fts[C], vector[C]) for C in fusion pool }  # pool members only
For each candidate C in the fusion pool:
  neighbours = get_edges(C, direction="BOTH", limit=max_neighbors)
  For each (edge, neighbour):
    neighbour_base = candidate_scores.get(neighbour, 0.0)  # 0 if neighbour is off-pool
    boost[C] += edge.weight × neighbour_base × graph_edge_decay
final_score[C] += graph_weight × boost[C]

A neighbour outside the fusion pool contributes 0, not its own fts/vector score — only pool members' base scores propagate.

Configuration
search:
  default_graph_weight: 0.0     # opt-in (0.0 = disabled)
  graph_edge_decay: 0.5         # decay factor for 1-hop distance
  max_neighbors_per_candidate: 5  # safety cap

Per-query override:

result = await store.search_hybrid(
    "python async",
    graph_weight=0.1,
    graph_edge_decay=0.3,
)

The graph signal is opt-in in v0.3.0 (default_graph_weight=0.0). When the weight is 0.0, the graph ranking signal is off and costs nothing. Candidate-pool expansion over CONSOLIDATED_FROM edges is controlled separately by graph_expansion_enabled (default true) and reads those edges only when a reflection ranks among the top candidates.

Complete YAML surface

This example names every YAML-configurable DreamingConfig, DreamingGates, and EdgeCreationConfig field with its default. It is a reference, not a recommended production profile:

extensions:
  dreaming:
    enabled: false
    schedule_every_n_cycles: 100   # used by is_due() / run_if_due()
    promote_threshold: 0.7
    candidates_limit: 200
    signals:
      recency: 0.25
      staleness: 0.20
      confirmation: 0.20
      confidence: 0.15
      frequency: 0.20
      action_outcome: 0.15

    clustering_backend: numpy      # numpy or python
    top_keyphrases_count: 3
    top_member_excerpts_count: 5
    member_excerpt_max_chars: 150
    max_p1_fraction: 0.05
    promote_targets: OBS_ONLY      # OBS_ONLY, REFL_ONLY, or ALL
    reflection_default_priority: P2

    eligible_perspectives: null    # optional list: percept, utterance, thought
    self_filter_mode: any          # any, self_only, external_only
    min_source_confidence: low     # low, medium, high
    excluded_content_types: [code]
    eligible_content_types: null

    boilerplate_threshold: 0.30
    boilerplate_min_corpus_size: 5
    boilerplate_min_keyphrases_per_refl: 1
    access_tracking_enabled: true

    gates:
      min_confirmations: 2
      min_age_cycles: 1
      max_promoted_per_run: 20
      allow_zero_confirmation: true

      min_cluster_size: 3
      cluster_similarity_threshold: 0.7
      cluster_algorithm: lpa       # lpa or agglomerative
      enable_reflections: true
      cold_start_clustering: false
      cluster_allowed_types: [OBSERVATION]
      clustering_min_new_candidates: 50
      max_cluster_size: 200        # null disables the upper bound

      cluster_quality_gating_enabled: true
      cluster_quality_persona_threshold: 0.75
      cluster_quality_cohesion_threshold: 0.40
      cluster_quality_external_homogeneity_threshold: 0.95
      cluster_quality_ne_consistency_threshold: 0.60
      cluster_quality_require_meaningful_keyphrases: true

    edges:
      enabled: true
      top_k: 1
      min_similarity: 0.7
      edge_weight_factor: 0.5

A partial signals mapping merges onto the six defaults. Unknown signal names require a matching custom_signals implementation when constructing DreamingExtension; the YAML-only from_config() path has no custom-signal registry and rejects unknown names when it constructs the extension.

As described in Configuration, unknown keys in the Dreaming, gates, and edge mappings raise ConfigError. See Configuration for field-by-field types and validation ranges.

Reflections (meta-consolidation)

run_consolidation() runs a third phase after promotion and edge creation: it clusters semantically related thoughts and creates ThoughtType.REFLECTION meta-thoughts that aggregate each cluster.

What is a REFLECTION?

A REFLECTION is a first-class ThoughtRecord that represents a higher-order abstraction over a cluster of related thoughts:

FieldValue
thought_typeREFLECTION
embeddingCentroid of member embeddings (mean, L2-normalised)
contentStructural JSON schema v2 (fields below)
priorityreflection_default_priority (P2 by default)
source"dreaming:<cluster_hash>" (hex-16)
source_typeKnowledgeSource.DREAMING
EdgesCONSOLIDATED_FROM → every cluster member

The v2 content payload preserves the legacy member_ids, keywords, and cluster_hash fields and adds:

Content fieldMeaning
type / version"reflection" / 2 schema discriminator
member_countNumber of eligible source members
cluster_algorithmAlgorithm used for the cluster
created_atISO-8601 payload build time
top_keyphrasesBounded TF-IDF phrase/score objects after cross-cluster boilerplate filtering
member_excerptsBounded excerpts from priority/recency-ordered members
temporal_spanMember creation-time bounds and span
named_entitiesSorted unique structurally extracted entities

No LLM is involved. Content, essence, centroid, metadata enrichments, and valid-time extent are derived structurally from member records and vectors; the payload's created_at records the current UTC build time. LLM-generated prose summaries belong in downstream extensions, not in the core graph layer.

Navigating the lineage. The CONSOLIDATED_FROM edges are queryable through dedicated store helpers — consolidated_member_ids(reflection_id), consolidated_source_statuses(reflection_id), and the reverse reflections_consolidated_from(source_id). Use them to walk from a REFLECTION to its sources and back (e.g. for provenance views or orphan detection) instead of querying the edge table directly. See REFLECTION lineage in the API reference.

How clustering works

Two algorithms are available via DreamingGates.cluster_algorithm:

"lpa" (default) — Label Propagation Algorithm

  • Operates over the ASSOCIATED dream-edge graph built in phase 2.

  • Deterministic via seeded PRNG (seed=42 by default).

  • O(E × iterations), no external dependencies.

  • Works when the graph is dense enough to form communities.

"agglomerative" — cosine-similarity single-linkage

  • Operates over bounded ACTIVE candidates whose thought types are listed in cluster_allowed_types (default: OBSERVATION), independent of graph edges.

  • Intended for sparse-graph / first-run scenarios where LPA finds no clusters.

  • Nodes whose cosine similarity ≥ cluster_similarity_threshold are merged via Union-Find.

  • Use when you want clustering before dreams have built up a graph.

clustering_backend selects the similarity implementation for the agglomerative path. numpy (default) uses vectorised float32 matrix operations and chunks large candidate sets to bound peak matrix memory. python uses the legacy pure-Python O(n²) loop and is intended only as a debugging escape hatch. It does not change the LPA implementation.

Cold-start fallback (cold_start_clustering, default false)

The "lpa" path reads communities only from the ASSOCIATED dream-edge graph, and those edges appear only after promotions create them. On a fresh or sparse graph the edge set is empty, so LPA finds nothing and no REFLECTIONs are produced no matter how many eligible OBSERVATIONs exist.

Set cold_start_clustering: true to opt into a fallback: when the LPA path finds the edge graph empty, it falls back within the same cycle to the cosine-similarity agglomerative clustering described above, run over the eligible ACTIVE candidate pool. This lets dreaming form clusters (and REFLECTIONs) before any dream edges exist.

  • Default is false — the shipped "lpa" behaviour is unchanged, and the fallback never alters cluster output when edges are present.

  • The fallback reuses the exact agglomerative path, so its clusters flow through the same min_cluster_size / max_cluster_size size gates and the content-quality gates — nothing bypasses them.

  • The candidate pool is bounded by candidates_limit, so a large active set does not make the fallback unbounded.

Idempotence

Before creating a REFLECTION, the extension derives a 16-hex content-hash from the sorted, eligibility-filtered member IDs — the same subset the metadata-aware eligibility filter above narrows the raw cluster down to, not the raw cluster itself — and checks whether any REFLECTION with source = "dreaming:<hash>" already exists via SELECT ... WHERE thought_type = ? AND source = ? LIMIT 1. This is not an O(1) index lookup: the schema indexes thought(thought_type) but not thought(source), so EXPLAIN QUERY PLAN shows the query using idx_thought_type and then scanning every matching row for the source filter — O(number of REFLECTIONs) per cluster per run, not O(1). If found, the cluster is skipped. Because the hash is over the filtered subset, the same raw cluster scanned under a different eligibility configuration can legitimately hash differently and yield a new REFLECTION.

Re-running run_consolidation() on unchanged data creates zero duplicate REFLECTIONs.

Recovery from a failed embedding

REFLECTION creation embeds the synthesis text itself (when the store has auto-embed configured) before the extension stores its own centroid vector. If that embedding attempt fails — a transient provider timeout, a rate limit — the extension attempts to delete the reflection thought it had already inserted, rather than rolling the transaction back; no centroid and no CONSOLIDATED_FROM edges are created for that cluster, because those steps only run after the reflection thought is created successfully. If the deletion attempt itself fails, or finds nothing to remove, the reflection thought remains with its content-hash recorded, and the next consolidation pass skips the cluster instead of retrying it. When the deletion succeeds, no thought carrying the cluster's hash remains, so the next pass processes the cluster as new; with journaling enabled, the journal still holds both the insert and the deletion.

Orphan sweep

The fourth consolidation phase retires a REFLECTION whose source cluster has left the active set, so a stale synthesis can't keep resurfacing in search. Two cases retire a REFLECTION, both checked against the same protection rule first:

  • All sources left ACTIVE. Every thought the REFLECTION was consolidated from is now ARCHIVED / DONE (or otherwise non-ACTIVE) — the synthesis now summarises nothing live.

  • Every source was hard-deleted. Deleting a thought cascades its CONSOLIDATED_FROM edge away with it, so a REFLECTION whose sources were all hard-deleted (rather than archived) is left with zero resolvable edges. This is retired exactly like the all-non-ACTIVE case, but only for a REFLECTION dreaming itself created (source_type is DREAMING) — one with no edges that was not created by dreaming (for example built directly through the API) is left alone, the same defensive guard that has always kept a malformed or legacy zero-edge REFLECTION from being wrongly swept.

Protection applies here too. A pinned REFLECTION, or one at a priority in protected_priorities (default P1), is never retired by this sweep — see Protection. A REFLECTION's priority defaults to reflection_default_priority (P2), so default dreaming is unaffected; pin a synthesis, or raise its priority into the protected set, to keep it past this sweep regardless of its source cluster.

A swept REFLECTION is not Memory Hygiene GC-eligible. Retiring a REFLECTION here is a direct ACTIVE -> ARCHIVED flip, not a Memory Hygiene archival — it does not stamp archived_at_cycle / archived_at, and Memory Hygiene's GC stage only ever reaps a row it archived (selected on archived_at_cycle IS NOT NULL). A swept REFLECTION therefore stops surfacing in search immediately, and is physically removed by any of: an explicit engrava gc, which keeps it if it is protected unless --include-pinned is given; an explicit delete, whatever its protection; or TTL cleanup under the delete strategy, if it carries an expired TTL and is not pinned (protection is a hygiene policy, not a TTL exemption) — see Data lifecycle → Running cleanup.

Configuration
extensions:
  dreaming:
    gates:
      min_cluster_size: 3             # min members for a reflection to be created
      cluster_similarity_threshold: 0.7  # cosine threshold (agglomerative only)
      cluster_algorithm: lpa          # "lpa" or "agglomerative"
      enable_reflections: true        # set to false to skip phase 3 entirely
      cold_start_clustering: false    # opt-in: LPA falls back to agglomerative
                                      #   clustering when the edge graph is empty
      cluster_allowed_types: [OBSERVATION]
      clustering_min_new_candidates: 50
      max_cluster_size: 200
Cluster-quality gates

After clustering, metadata eligibility is applied again to the resolved members. The cluster must still contain at least min_cluster_size eligible members. max_cluster_size is not re-checked here — it was already enforced once, against the raw (pre-filtering) cluster in _build_clusters — so an eligible-member count under the cap does not rescue a raw cluster that exceeded it, and no upper bound applies to the eligible subset itself.

With cluster_quality_gating_enabled: true (default), a cluster is rejected on the first failed content-quality check:

  • duplicate member content;

  • persona-only share at or above cluster_quality_persona_threshold (0.75);

  • contradictions flagged by the lightweight English token-pair heuristic;

  • mean pairwise cosine below cluster_quality_cohesion_threshold (0.40);

  • external-source fraction below cluster_quality_external_homogeneity_threshold (0.95);

  • named-entity overlap below cluster_quality_ne_consistency_threshold (0.60); or

  • no meaningful post-boilerplate keyphrase when cluster_quality_require_meaningful_keyphrases is true.

Disabling the master switch bypasses these content-quality checks, but not cluster size, metadata eligibility, embedding availability, or idempotence. The cross-cluster boilerplate filter is controlled separately by boilerplate_threshold, boilerplate_min_corpus_size, and boilerplate_min_keyphrases_per_refl.

ConsolidationResult fields
FieldMeaning
candidates_evaluatedNumber of ACTIVE candidates in the bounded promotion pool
promoted_count / promoted_idsPromotions written and their thought IDs
skipped_gate_countCandidates rejected by age/confirmation gates
scoresComputed score for each candidate the loop scores. Every candidate is scored before its gate, metadata and threshold checks run, so scoring continues past the max_promoted_per_run cap; once the cap is reached, the loop stops at the next candidate that passes the gates, the metadata filter and the threshold, after scoring it, and candidates after that one are absent from the map
edges_createdNew dream-created ASSOCIATED edges
reflections_createdNew REFLECTION thoughts
promotion_cappedWhether the corpus-wide P1 fraction prevented a promotion
p1_fraction_afterP1 share after the run
orphans_retiredACTIVE REFLECTIONs archived by the orphan sweep (all sources left the active set, or all were hard-deleted), excluding protected ones
active_signal_weightsEffective weights after flat-signal redistribution
flat_signalsConfigured signals dropped as inactive for this run (see Signals)
Querying reflections

See search.md — "Querying reflections" section.

402 files · 357 not served here

Advantages

Limitations

Security

Risk level: Low risk

This brain runs

Git hooks (1)

  • .githooks/commit-msg (commit-msg): runs on git commit-msg

Agent instructions that run or fetch something (4)

  • AGENTS.md: Run `git commit` and it fires on the patterns it checks for.
  • CLAUDE.md: pip install -e ".[dev]"
  • CLAUDE.md: Run a clean test suite locally before pushing;
  • CLAUDE.md: Branch from `dev`, follow Conventional Commits, run the local quality gate before pushing.

Install scripts (1)

  • Makefile (make install): echo "$(BLUE)>> Installing engrava + dev dependencies$(RESET)"; $(PIP) install --upgrade pip setuptools wheel; $(PIP) install -e ".[dev]"; bash scripts/install_hooks.sh; echo "$(GREEN)>> Done. Run 'make check' to verify quality gate.$(RESET)"
  • Low risk: scripts-present (.githooks/commit-msg)
  • Low risk: network-instructions (CLAUDE.md:40)
  • Low risk: network-instructions (pyproject.toml:98)
  • Low risk: scripts-present (scripts/install_hooks.sh)

Attribution and licence

Created by
Sovantica
Declared authors
  • Sovantica (declared in pyproject.toml)
Publisher
Sovantica (Official Sovantica project)
Source
sovantica/engrava · commit f6a8b73
Licence
MIT License (files served here)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by Sovantica.

Install from source

357 of its files are not served here, so there is no ZIP. sovantica/engrava at f6a8b73