Memory that improves the agent — without quietly poisoning it.
A three-layer memory system: readable workspace files, a fast local hybrid index and a shared global brain. The additions that matter are provenance, incremental sync and an evaluation gate on everything that claims to be an improvement.
The three layers
Each layer has a different job. Do not collapse them into one database.
Workspace memory
~/.openclaw/workspace-{agent}/memory/ Human-readable Markdown, materialised from the shared brain. This is the agent’s context window: knowledge digest, peer knowledge, strategies, reflections and lessons.
Local hybrid index
~/.openclaw/memory/{agent}.sqlite Fast, offline retrieval with FTS5 for exact terms and a vector table for semantic recall. Embeds once and caches by content hash.
Shared global brain
PostgreSQL · schema: agency Durable, cross-agent knowledge: knowledge, lessons, strategies, reflections, scores, competency and retrieval feedback. The source of truth for learning.
The memory loop
A write is not finished when it is stored. It is finished when it is attributable, retrievable and evaluated.
Research or task output becomes a candidate record with source, author and timestamp.
Deduplicate, classify and score trust. Unverified content goes to quarantine.
Write to PostgreSQL with embedding + tsvector; keep the event log.
Incremental materialisation to workspace Markdown, then reindex only changed chunks.
Local or global hybrid search: BM25 + vector, fused with RRF.
Log results and feedback; consolidate lessons; evaluate before any strategy or α change.
Hybrid retrieval
BM25 catches exact identifiers, error codes and names that embeddings blur. Vectors catch conceptual matches. Fuse both; do not choose one.
Fusion
score = (1 - α) / (k + rank_bm25)
+ α / (k + rank_vector)
k = 60
α = 0.5 initially, clamped [0.2, 0.8]
α is per agent and changes only after
an offline evaluation gate. When to use which index
- Local SQLite — current task context, private workspace, offline.
- Global PostgreSQL — peer knowledge, durable lessons, cross-agent strategies.
- Both — merge candidates, then rerank with a small model or deterministic rules.
Schema that supports trust
The original seven tables are a good base. Add provenance and trust to every knowledge record.
| Table | Purpose |
|---|---|
agency.knowledge | Durable facts and research with embeddings and full-text index. |
agency.lessons | Trigger → action lessons extracted from incidents and reviews. |
agency.strategy_registry | Versioned strategy prompts, scores, parent/child lineage. |
agency.reflections | Post-task analysis and proposed rewrites. |
agency.score_ledger | Immutable score events — not a mutable average. |
agency.agent_competency | Per-agent, per-skill tier and evidence. |
agency.retrieval_feedback | Query, result, rank and usefulness — the evaluation signal. |
-- minimum trust fields
source_url, source_hash, author_agent, created_at,
trust_level: raw | verified | trusted,
trust_reason, expires_at, acl_scope Consolidation, decay and forgetting
Decay keeps memory lean, but forgetting must be deliberate and auditable.
| Class | Default | Rule |
|---|---|---|
| Permanent | Identity, soul, core memory | Never auto-archive; human pin only. |
| Short-term | Daily logs | 14 days, importance-weighted; archived, not deleted. |
| Long-term | Reports, audits, digests | 90 days, then summary retained. |
Importance multiplies the effective age, but keep a hard retention floor and an archive. A production incident at 2× importance should not become immortal by accident.
Strategy evolution with a brake
UCB selection and reflection are useful. They are not evidence on their own.
- Minimum sample: do not promote or clone a strategy on fewer than N scored tasks.
- Holdout gate: a new strategy must beat its parent on a frozen evaluation set.
- Canary: route a small share of live tasks; automatic rollback on regression.
- Diversity: cosine similarity alone is not enough; require task-level diversity too.
- Lineage: every strategy stores parent, evidence, score history and why it was retired.
What to measure
| Metric | Question it answers |
|---|---|
| Recall@k | Are the right memories retrieved for a known query set? |
| Precision / noise | How much irrelevant context enters the window? |
| Staleness | Median age of retrieved memory; drift from source. |
| Poison rate | Quarantined or rejected records per 1,000 ingests. |
| Strategy delta | Win rate of a new strategy versus its parent on a holdout set. |
| Cost / query | Embedding, vector and rerank cost per successful retrieval. |
Failure modes
| Failure | Cause | Fix |
|---|---|---|
| Stale memory | Six-hour full sync; no watermark | Event-driven updates + reconciliation; surface freshness. |
| Poisoned knowledge | Any agent can ingest web/Discord text | Provenance, trust level, quarantine, injection checks. |
| Runaway evolution | Self-scored strategies; no holdout | Offline evaluation gate, canary, minimum sample, rollback. |
| Index drift | SQLite rebuilt from changed files | Content hashes, incremental index, embedding version. |
| Privacy leak | Shared brain across agents | ACL by agent/tenant, PII scan, retention policy. |