feat(agent-memory): Phase 5 — eviction & TTL#253
Open
jamby77 wants to merge 2 commits into
Open
Conversation
8739aa6 to
dcfde4b
Compare
6e7c2e9 to
2dc4339
Compare
- Add selectEvictions(): pure lowest-(importance, recency) selection, reusing the recall weights minus similarity, renormalized - Extract recencyDecay() from compositeScore for shared half-life decay - remember(): write expiring memories atomically via MULTI/HSET/EXPIRE/EXEC when ttl is set; durable HSET otherwise - Enforce maxItemsPerScope on write (count-first probe, then bounded candidate scan) and record evictions in __mem_stats; best-effort so it never breaks the write path - Add ttl to RememberOptions and maxItemsPerScope to MemoryStoreOptions
dcfde4b to
38a6515
Compare
2dc4339 to
e4646ec
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e4646ec. Configure here.
enforceCapacity built its filter with empty tags, so a tag-only write collapsed to '*' and counted/evicted across the whole index instead of the tag partition used by recall and forgetByScope. Pass the write tags through so capacity is enforced on the same partition.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Stacked on #251 (Phase 4 — recall reinforcement).
What
Phase 5 of
@betterdb/agent-memory: capacity-based eviction and TTL writes.selectEvictions()(pure): selects the lowest-ranked keys to drop somaxItemsremain. Ranks by a blend of importance and last-access recency — the recall weights minus similarity, renormalized — lowest first, ties broken toward the older last-access.recencyDecay()extracted fromcompositeScoreso recall ranking and eviction share one true half-life decay.remember(content, { ttl })sets the hash and its expiry atomically viaMULTI/HSET/EXPIRE/EXEC; a non-positive/absentttlwrites a durableHSET.maxItemsPerScopeis configured, each write runs a count-first probe (cheapLIMIT 0 0) and only fetches candidates when over capacity, then evicts the excess and incrementsevictionsin{name}:__mem_stats. Best-effort — a failed eviction pass never rejects an already-durable write.Design notes
last_accessed_at(fed by reinforcement); recall recency usescreated_at— deliberate split per spec.EVICTION_SCAN_LIMIT; larger scopes evict from the scanned window and reclaim the remainder on subsequent writes.Tests
selectEvictions.test.ts— capacity boundary, drop count, importance- vs recency-driven ordering, weight-dominance, tie-breaks.MemoryStore.eviction.test.ts— atomic TTL sequence, durable/non-positive-ttl paths, over/under-capacity eviction + counter, scope-filtered query, unconfigured no-op, best-effort failure isolation.47/47 package tests green ·
tsc --noEmitclean · prettier clean.Note
Medium Risk
Deletes stored memories on write when over capacity and changes remember's Redis command pattern; failures are isolated but eviction is destructive and approximate beyond the scan window.
Overview
Adds TTL and per-scope capacity limits to
@betterdb/agent-memoryMemoryStore.remember.remembernow accepts optionalttl(seconds). Positive TTL persists the hash andEXPIREin oneMULTI/EXEC; missing or non-positive TTL stays a plain durableHSET. After each write, optionalmaxItemsPerScoperuns a best-effort capacity pass: a cheapFT.SEARCHcount on the same scope/tag filter as recall/forget, then eviction only when over limit. Eviction uses newselectEvictions(), ranking by importance pluslast_accessed_atrecency (recall weights minus similarity, sharedrecencyDecay()), deletes losers, and increments{name}:__mem_statsevictions`. Enforcement failures are swallowed so writes still succeed.Tests cover TTL command sequences, scope/tag partitioning, under-capacity no-ops, eviction selection, and write isolation when search fails.
Reviewed by Cursor Bugbot for commit a820313. Bugbot is set up for automated code reviews on this repo. Configure here.