feat(ai-sdk): introduce Working Memory layer for low-latency local context retrieval - #1643
Open
Sravanjangam wants to merge 1 commit into
Open
feat(ai-sdk): introduce Working Memory layer for low-latency local context retrieval#1643Sravanjangam wants to merge 1 commit into
Sravanjangam wants to merge 1 commit into
Conversation
…ntext RFC supermemoryai#1625 — V1: explicit decorator (Staff review 7.8/10) Fixes supermemoryai#1625 V1 scope (mergeable): LRU, TTL, promise dedup, invalidate, clear, stats. Deferred: pin/unpin, auto-populate, _source mutation, persistent cache, cross-process sharing. API: createWorkingMemory(tools, { ttlMs, maxEntries }) wraps searchMemories without mutating return shape. Base supermemoryTools behavior unchanged — zero breaking change. Non-goals (V1): persistent disk, cross-process, semantic embedding cache, write caching, background refresh, cross-device sync. Co-authored-by: Sravanjangam <163002695+Sravanjangam@users.noreply.github.com>
Contributor
Author
|
Successor to #1626 — original PR was auto-closed by |
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.
Summary
Introduces a Working Memory layer — Layer 1 of the Hierarchical Memory Pyramid — in
packages/ai-sdkas an explicit, opt-in decorator (createWorkingMemory) with zero change to existingsupermemoryToolsbehavior. V1 is intentionally small: LRU, TTL, promise dedup, invalidate.Fixes #1625
All 5 review points fixed in this revision (amended
ec47bedf).Problem
packages/ai-sdk/src/tools.ts:34—supermemoryToolscalledclient.search.executeon everysearchMemorieswith no memoization. Agent loops pay full RTT each time.supermemoryToolshad no local session behaviorImpact
Solution — V1 scope (mergeable)
New file
packages/ai-sdk/src/working-memory.ts(225 lines, zero deps) +packages/ai-sdk/src/working-memory.test.ts(10 Vitest tests).packages/ai-sdk/src/tools.tsunchanged — noworkingMemoryconfig, no auto-populate, no_sourcemutation.V1 public API (explicit decorator, zero breaking behavior):
Internals handle TTL, LRU, promise dedup. Everything else stays private.
supermemoryTools({ workingMemory: { enabled: true }})changedsearchMemoriessemanticscreateWorkingMemory(tools, opts)decorator —tools.searchMemoriesstill hits backend,memory.searchMemorieshits cache.workingMemoryisenumerable: false.searchMemoriesonly. Writes/updates/deletes deferred to V2._sourcemutates shape{ ...memory, _source: "cache" }{ success, results, count }unchanged. Cache metadata viaworkingMemory.stats()only.bunharnesspackages/ai-sdk/src/working-memory.test.ts, 10 tests): cache hit/miss, TTL with fake timers, dedup 20→1, LRU, invalidate, disabled-cache, stats, decorator shape, no auto-populate.Benchmark
Isolated harness,
delay: 50 mssimulates RTT (real 80–150 ms):Failure Handling (V1)
invalidate(query)clears single key;invalidate()clears all;clear()resets map + stats.get/search; next call fetches fresh.fetchFndoes not poison cache —stats().missesincrements, no entry written.maxEntries(oldest evicted), prevents unbounded growth in long-lived agents.Memory Footprint
Bounded 100 entries default → ~10–20 KB worst-case +
Mapoverhead. Configurable perWorkingMemoryOptions.Testing
Real Vitest —
bun x vitest run packages/ai-sdk/src/working-memory.test.ts:Tests: cache hit, cache miss, TTL expiry (fake timers), promise dedup 20→1, LRU eviction, invalidate single vs all, stats, cache-disabled control, decorator non-mutating shape, no auto-populate on
addMemory.Harness previously at
/tmp/prs/wm-parallel-harness.sh(10 isolatedbunprocesses) validated the same; replaced by Vitest per review.Biome:
bun x biome check packages/ai-sdk/src/working-memory.ts— clean (5 warnings pre-existinganysuppression in unrelated file).Typecheck:
bun x tsc --noEmit --project packages/ai-sdk/tsconfig.json— clean.Environment
feat/working-memory-layer@ec47bedf(force-pushed toSravanjangam/supermemory)supermemoryai/supermemory@main d436792eNon-goals (Phase 1)
This RFC intentionally does not include: persistent disk cache, cross-process sharing, semantic embedding cache, memory write caching, background refresh, cross-device synchronization. Deferred to V2–V4.
Deferred roadmap
pin/unpin(high-value context)addMemoryauto-populate + update/delete invalidationstats()/sourcemetadata channel