Skip to content

feat(agent-memory): Phase 6 — consolidate()#254

Open
jamby77 wants to merge 1 commit into
feature/agent-memory-phase5-evictionfrom
feature/agent-memory-phase6-consolidate
Open

feat(agent-memory): Phase 6 — consolidate()#254
jamby77 wants to merge 1 commit into
feature/agent-memory-phase5-evictionfrom
feature/agent-memory-phase6-consolidate

Conversation

@jamby77

@jamby77 jamby77 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #253 (Phase 5 — eviction & TTL).

What

Phase 6 of @betterdb/agent-memory: consolidation — summarize many old/low-value memories into fewer durable ones. Callable, not scheduled (per spec).

consolidate(options):

  • Selects candidates in scope (threadId/agentId/namespace + tags) filtered by olderThanSeconds and maxImportance.
  • Calls the caller-supplied summarize(items) => Promise<string> (the library never hard-codes an LLM — mirrors the judge-injection pattern).
  • Writes the result as a normal memory with source: 'summary' at summaryImportance (default 0.7), scoped/tagged to the request.
  • With deleteSources (default true) removes the originals; returns { consolidated, created, deleted }.

Design / review notes

  • Write-before-delete: the summary is written before sources are deleted, so a failure can never destroy memories without leaving their consolidated replacement.
  • Mass-delete guard: requires at least one selection criterion (scope, tags, olderThanSeconds, or maxImportance) — a criteria-less consolidate({summarize}) would otherwise summarize+delete the entire store. Mirrors forgetByScope's guard.
  • Efficiency: the candidate scan uses an explicit RETURN of only the fields parseMemoryItem needs, so it never transfers vector blobs.
  • Bounded scan (CONSOLIDATE_SCAN_LIMIT); a single summarize() over a whole huge scope is impractical anyway, and the remainder consolidates on subsequent calls.

Tests (MemoryStore.consolidate.test.ts, 8)

candidate summarize+write+delete+counts · olderThanSeconds filter · maxImportance filter · summary scope+summaryImportance · deleteSources:false keeps sources · nothing-matches → zeros & no summarize/write · no-criteria guard throws · defaults (0.7 / delete).

55/55 package tests green · tsc --noEmit clean · prettier clean.


Note

Medium Risk
The operation can bulk-delete stored memories if criteria match; guards and write-before-delete reduce accidental loss, but misconfigured scope or summarize failures still need careful caller handling.

Overview
Adds MemoryStore.consolidate() so callers can merge many scoped memories into one durable summary memory, with optional removal of the originals.

Candidates are found via scoped FT.SEARCH, then filtered by olderThanSeconds and maxImportance. The caller supplies summarize(items) (no built-in LLM). The summary is stored through remember() with source: 'summary', default summaryImportance 0.7, and the same scope/tags. deleteSources defaults to true; the API returns { consolidated, created, deleted }.

Safety: consolidation requires at least one selection criterion (scope, tags, age, or importance cap) to block whole-store runs. The summary is written before source deletes. Scans are capped at CONSOLIDATE_SCAN_LIMIT and use a narrow RETURN list (no vector payloads). New ConsolidateOptions / ConsolidateResult types are exported; behavior is covered by MemoryStore.consolidate.test.ts.

Reviewed by Cursor Bugbot for commit e27477f. Bugbot is set up for automated code reviews on this repo. Configure here.

@jamby77 jamby77 force-pushed the feature/agent-memory-phase5-eviction branch from 6e7c2e9 to 2dc4339 Compare June 18, 2026 06:57
@jamby77 jamby77 force-pushed the feature/agent-memory-phase6-consolidate branch from 5cdeb4c to c317978 Compare June 18, 2026 06:57

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c317978. Configure here.

Comment thread packages/agent-memory/src/MemoryStore.ts
@jamby77 jamby77 force-pushed the feature/agent-memory-phase5-eviction branch from 2dc4339 to e4646ec Compare June 18, 2026 07:19
@jamby77 jamby77 force-pushed the feature/agent-memory-phase6-consolidate branch from c317978 to d5a95b9 Compare June 18, 2026 07:19
- consolidate() summarizes old/low-importance memories into one durable
  source:'summary' memory via a caller-supplied summarize(items) callback
- Select candidates by scope/tags + olderThanSeconds + maxImportance;
  write the summary before deleting sources so a failure never destroys
  memories without leaving the replacement
- deleteSources defaults true; summaryImportance defaults 0.7
- Require at least one selection criterion to prevent whole-store
  consolidation (mirrors forgetByScope's mass-delete guard)
- Fetch only the fields parseMemoryItem needs (no vector blobs) on the
  candidate scan
- Add ConsolidateOptions/ConsolidateResult types
@jamby77 jamby77 force-pushed the feature/agent-memory-phase6-consolidate branch from d5a95b9 to e27477f Compare June 18, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant