feat(ai-sdk): introduce Working Memory layer for low-latency local context - #1626
feat(ai-sdk): introduce Working Memory layer for low-latency local context#1626Sravanjangam wants to merge 1 commit into
Conversation
96501de to
ec47bed
Compare
TracePull Review by @yesprasad returned below reviewThe new Working Memory layer is self-contained, opt-in, and has coverage for cache hits and misses, TTL, LRU eviction, invalidation, and request deduplication. One validation area remains:
These checks matter because DeepGraph analysis:
No blocking review comment is required based on the available evidence.
|
…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>
ec47bed to
9ddd057
Compare
|
Thanks @yesprasad for the DeepGraph review — addressed all four validation points in the latest push 1.
2. Backend default
3. Lowercasing + trimming matches backend semantics — ✅ documented
4. Cached responses preserve same shape/fields as uncached — ✅ verified
Appreciate the thorough DeepGraph trace (3 files, 49 imports). No blocking comment per review, but happy to adjust casing/limit handling if maintainers prefer stricter keying! |
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