Mem0 Dream — Memory Consolidation
This skill performs a memory consolidation pass: it fetches all project memories,
identifies near-duplicates, flags contradictions, and prunes stale entries based on
configured retention policies. All proposed changes are shown as a diff for user
approval before anything is modified.
IMPORTANT: Execute steps strictly in order (1 → 2 → 3 → 4 → 5 → 6). Each step depends on the previous one. Do NOT run steps in parallel or skip ahead.
Step 1: Load Retention Policies
Determine the active retention policy by running the parser script. Use the
appropriate
variable for the current platform (
,
, or
):
bash
python3 "<PLUGIN_ROOT>/scripts/parse_mem0_config.py" "<cwd>"
Parse the JSON output (a dict of
). If the script fails
or returns
, fall back to these built-in defaults:
| Default retention |
|---|
| 90 days |
| 90 days |
| all others | no pruning |
Store the resolved policies for use in Step 3.
Step 2: Fetch ALL Project Memories
Call
to retrieve every memory for the active project:
python
get_memories(
filters={"AND": [{"user_id": "<active_user_id>"}, {"app_id": "<active_project_id>"}]},
page_size=200,
)
If the response indicates more pages exist, paginate until all memories are fetched.
Collect the full list before proceeding. If zero memories are found, print:
No memories found for project <project_id>. Nothing to consolidate.
…and stop.
Step 3: Analyze — Find Issues
Work entirely in-memory; do not modify anything yet.
Group memories by
(use
when the field is absent).
For each group, identify the following:
3a. Near-duplicate pairs (merge candidates)
Two memories are near-duplicates when they express the same fact or decision but
phrased differently (e.g., "Use PostgreSQL for auth" and "Auth DB is PostgreSQL").
Heuristics — two memories are near-duplicates if all of these hold:
- Similarity threshold: estimated cosine similarity > 0.9 (use noun/keyword overlap as proxy — if >60% of significant nouns overlap, treat as >0.9 similarity).
- Same .
- Neither memory is pinned ().
For each qualifying pair, draft a merged version that is more complete and specific
than either original.
3b. Contradictions
Two memories contradict when they assert opposing facts about the same topic
(e.g., "Deploy to ECS" vs. "Deploy to Vercel").
Identify the likely winner: the more recent memory with higher confidence wins.
Store both IDs and their content for user review.
3c. Prune candidates
A memory is a prune candidate when any of the following is true:
- Its has a retention policy and the memory is older than the
configured number of days (compare to today).
- Its confidence score is below 0.3 AND it contains no information unique to
this project (no file paths, identifiers, or domain-specific nouns).
Always skip memories where , regardless of age or
confidence.
Step 4: Print Diff Report
Print a structured diff to the terminal before making any changes. Use exactly
this format:
## dream — consolidation report
Merges (<N>):
[mem0:<id1>] + [mem0:<id2>] → "<merged content, 100 chars>"
Conflicts (<N>):
[mem0:<idA>] vs [mem0:<idB>] — "<topic>" [A/B/skip]
Prune (<N>):
[mem0:<id>] — <type>, <age>d old
Proposed: <N> merges, <N> prunes, <N> conflicts. Apply? [Y/n]
If there are zero items in any category, omit that section entirely.
If there are zero total proposals (no merges, no prunes, no conflicts), print:
Dream complete. No duplicate, contradictory, or stale memories found.
…and stop.
Step 5: Wait for User Input and Apply
5a. Contradictions
For each
pair in the report, wait for the user to type
,
, or
(case-insensitive). If they enter nothing (empty), treat as
.
Record the winner for each pair before proceeding to the final apply confirmation.
5b. Final confirmation
After all conflict resolutions are collected, prompt:
If the user types
or
(case-insensitive), print
Cancelled. No changes made.
and stop.
If the user confirms (
,
, or empty / Enter), apply all changes in this order:
Merges
For each approved merge pair:
- with:
app_id=<active_project_id>
(top-level, not in metadata)
metadata={"type": "<original type>", "branch": "<active_branch>", "confidence": <higher of the two original scores>, "source": "mem0-dream"}
Contradictions (resolved)
For each resolved conflict where the user chose A or B:
- Delete the loser (the non-chosen memory):
delete_memory(memory_id=<loser_id>)
Contradictions where the user chose
are left untouched.
Prunes
For each prune candidate:
delete_memory(<memory_id>)
Step 6: Print Summary
After all changes are applied, print:
Dream complete — merged: <N>, pruned: <N>, conflicts resolved: <N>, skipped: <N>
Auto mode
When invoked with
(e.g.,
), run non-interactively:
- Merges: applied automatically (no contradiction, both are compatible).
- Prunes: applied automatically (age/confidence-based, no ambiguity).
- Contradictions: skipped — they require human judgment.
Concurrency guard
Before doing any work, check for a lock file at
/tmp/mem0_dream_auto.lock
:
- If the lock file exists and is less than 10 minutes old, print
[mem0-dream --auto] Another run in progress — skipping.
and stop.
- Otherwise, create the lock file (write the current timestamp). Delete it when done (in all exit paths).
Execution
In auto mode:
- Load policies and fetch memories (Steps 1–3) as normal.
- Apply merges and prunes silently without printing the diff or prompting.
- Print a compact summary:
[mem0-dream --auto] project=<id> merged=<N> pruned=<N> conflicts_skipped=<N>
- If contradictions were detected but skipped, check if a reminder already exists before storing one:
- Search for existing reminders:
search_memories(query="mem0-dream contradictions manual review", filters={"AND": [{"user_id": "<active_user_id>"}, {"app_id": "<active_project_id>"}, {"metadata": {"source": "mem0-dream-auto"}}]}, top_k=1)
- If a result exists with similarity > 0.9, skip storing the reminder (one already exists).
- If no match, store the reminder:
python
add_memory(
text="mem0-dream detected <N> contradiction(s) requiring manual review. Run /mem0:dream to resolve them interactively.",
user_id="<active_user_id>",
app_id="<active_project_id>",
metadata={"type": "task_learning", "source": "mem0-dream-auto", "branch": "<active_branch>"},
infer=False,
)
See also
- — targeted deletion of specific memories (search + confirm + delete)
- — quick quality scan without applying changes