-
Notifications
You must be signed in to change notification settings - Fork 74
feat(agent-memory): Phase 10 — AgentMemory facade #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamby77
wants to merge
4
commits into
feature/agent-memory-phase9-observability
Choose a base branch
from
feature/agent-memory-phase10-facade
base: feature/agent-memory-phase9-observability
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d6b49e8
feat(agent-memory): Phase 10 — AgentMemory facade
jamby77 f2fd3de
test(agent-memory): assert the facade memory marker uses a distinct {…
jamby77 963c172
fix(agent-memory): propagate a cache discovery collision from initial…
jamby77 edb1937
docs(agent-memory): document the AgentMemory client cast assumption
jamby77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,86 @@ | ||
| export class AgentMemory {} | ||
| import { AgentCache, type AgentCacheOptions } from '@betterdb/agent-cache'; | ||
| import { | ||
| MemoryStore, | ||
| type MemoryDiscoveryConfig, | ||
| type MemoryConfigRefreshConfig, | ||
| } from './MemoryStore'; | ||
| import type { RecallWeights } from './compositeScore'; | ||
| import type { EmbedFn, MemoryStoreClient } from './types'; | ||
|
|
||
| const DEFAULT_NAME = 'betterdb_ac'; | ||
|
|
||
| export interface AgentMemoryConfig { | ||
| defaultThreshold?: number; | ||
| recall?: { | ||
| weights?: RecallWeights; | ||
| halfLifeSeconds?: number; | ||
| }; | ||
| maxItemsPerScope?: number; | ||
| discovery?: boolean | MemoryDiscoveryConfig; | ||
| configRefresh?: boolean | MemoryConfigRefreshConfig; | ||
| } | ||
|
|
||
| export interface AgentMemoryOptions extends AgentCacheOptions { | ||
| embedFn: EmbedFn; | ||
| memory?: AgentMemoryConfig; | ||
| } | ||
|
|
||
| export class AgentMemory { | ||
| readonly llm: AgentCache['llm']; | ||
| readonly tool: AgentCache['tool']; | ||
| readonly session: AgentCache['session']; | ||
| readonly memory: MemoryStore; | ||
| private readonly cache: AgentCache; | ||
|
|
||
| constructor(options: AgentMemoryOptions) { | ||
| if (typeof options.embedFn !== 'function') { | ||
| throw new Error('AgentMemory requires an embedFn to back the memory tier'); | ||
| } | ||
|
|
||
| // Resolve the name once and hand the same value to both tiers so their key | ||
| // prefixes, discovery markers, and stats keys can never drift apart. | ||
| const name = options.name ?? DEFAULT_NAME; | ||
| this.cache = new AgentCache({ ...options, name }); | ||
| this.llm = this.cache.llm; | ||
| this.tool = this.cache.tool; | ||
| this.session = this.cache.session; | ||
|
|
||
| const memory = options.memory ?? {}; | ||
| this.memory = new MemoryStore({ | ||
| // AgentCacheOptions.client doesn't surface the `.call` method MemoryStore | ||
| // needs; a real ioredis/iovalkey client has it, so we assert the contract | ||
| // here. A method-only client/mock would compile but fail at runtime. | ||
| client: options.client as unknown as MemoryStoreClient, | ||
| name, | ||
| embedFn: options.embedFn, | ||
| defaultThreshold: memory.defaultThreshold, | ||
| weights: memory.recall?.weights, | ||
| halfLifeSeconds: memory.recall?.halfLifeSeconds, | ||
| maxItemsPerScope: memory.maxItemsPerScope, | ||
| // The facade is the batteries-included product: discover the memory tier | ||
| // alongside the cache tiers by default, unless explicitly disabled. | ||
| discovery: memory.discovery ?? true, | ||
| configRefresh: memory.configRefresh, | ||
| telemetry: options.telemetry?.registry ? { registry: options.telemetry.registry } : undefined, | ||
| }); | ||
| } | ||
|
|
||
| async initialize(): Promise<void> { | ||
| // Surface a discovery name-collision from either tier: awaiting | ||
| // ensureDiscoveryReady() is AgentCache's documented strict collision check, | ||
| // and the memory tier already propagates, so the cache side isn't swallowed. | ||
| await Promise.all([ | ||
| this.cache.ensureDiscoveryReady(), | ||
| this.memory.ensureDiscoveryReady(), | ||
| ]); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async close(): Promise<void> { | ||
| // Tear down both tiers even if one fails, so timers and heartbeats can't leak. | ||
| try { | ||
| await this.memory.close(); | ||
| } finally { | ||
| await this.cache.shutdown(); | ||
| } | ||
| } | ||
| } | ||
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
185 changes: 185 additions & 0 deletions
185
packages/agent-memory/src/__tests__/AgentMemory.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { Registry } from 'prom-client'; | ||
| import { AgentMemory, type AgentMemoryOptions } from '../AgentMemory'; | ||
| import { MemoryStore } from '../MemoryStore'; | ||
| import { fakeEmbed } from './helpers/fakeEmbed'; | ||
|
|
||
| function fakeValkey() { | ||
| const ok = vi.fn(async () => 'OK'); | ||
| const nul = vi.fn(async () => null); | ||
| return { | ||
| call: vi.fn(async () => 'OK'), | ||
| get: nul, | ||
| set: ok, | ||
| del: ok, | ||
| hget: nul, | ||
| hset: ok, | ||
| hgetall: vi.fn(async () => ({})), | ||
| hincrby: ok, | ||
| expire: ok, | ||
| exists: vi.fn(async () => 0), | ||
| scan: vi.fn(async () => ['0', []]), | ||
| }; | ||
| } | ||
|
|
||
| type FakeClient = ReturnType<typeof fakeValkey>; | ||
|
|
||
| function makeOptions(overrides: Partial<AgentMemoryOptions> = {}): AgentMemoryOptions { | ||
| return { | ||
| client: fakeValkey() as unknown as AgentMemoryOptions['client'], | ||
| embedFn: fakeEmbed(8), | ||
| discovery: { enabled: false }, | ||
| configRefresh: { enabled: false }, | ||
| analytics: { disabled: true }, | ||
| ...overrides, | ||
| } as AgentMemoryOptions; | ||
| } | ||
|
|
||
| describe('AgentMemory facade', () => { | ||
| it('exposes the three short-term tiers plus the memory tier', async () => { | ||
| const mem = new AgentMemory(makeOptions()); | ||
|
|
||
| expect(mem.llm).toBeDefined(); | ||
| expect(mem.tool).toBeDefined(); | ||
| expect(mem.session).toBeDefined(); | ||
| expect(mem.memory).toBeInstanceOf(MemoryStore); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
|
|
||
| it('throws a clear error when constructed without an embedFn', () => { | ||
| const options = { ...makeOptions(), embedFn: undefined } as unknown as AgentMemoryOptions; | ||
| expect(() => new AgentMemory(options)).toThrow(/embedFn/i); | ||
| }); | ||
|
|
||
| it('wires the memory tier to the shared client and default prefix', async () => { | ||
| const client = fakeValkey(); | ||
| const mem = new AgentMemory( | ||
| makeOptions({ client: client as unknown as AgentMemoryOptions['client'] }), | ||
| ); | ||
|
|
||
| const id = await mem.memory.remember('hello'); | ||
|
|
||
| expect(typeof id).toBe('string'); | ||
| const hset = (client.call.mock.calls as unknown[][]).find( | ||
| (c) => c[0] === 'HSET' && typeof c[1] === 'string' && c[1].startsWith('betterdb_ac:mem:'), | ||
| ); | ||
| expect(hset).toBeDefined(); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
|
|
||
| it('shares the configured name as the memory key prefix', async () => { | ||
| const client = fakeValkey(); | ||
| const mem = new AgentMemory( | ||
| makeOptions({ client: client as unknown as AgentMemoryOptions['client'], name: 'myapp' }), | ||
| ); | ||
|
|
||
| await mem.memory.remember('hello'); | ||
|
|
||
| const hset = (client.call.mock.calls as unknown[][]).find( | ||
| (c) => c[0] === 'HSET' && typeof c[1] === 'string' && c[1].startsWith('myapp:mem:'), | ||
| ); | ||
| expect(hset).toBeDefined(); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
|
|
||
| it('maps the memory sub-config onto the MemoryStore', async () => { | ||
| const mem = new AgentMemory( | ||
| makeOptions({ | ||
| memory: { | ||
| defaultThreshold: 0.4, | ||
| recall: { | ||
| weights: { similarity: 0.5, recency: 0.3, importance: 0.2 }, | ||
| halfLifeSeconds: 3600, | ||
| }, | ||
| maxItemsPerScope: 100, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| expect(mem.memory.currentConfig()).toEqual({ | ||
| threshold: 0.4, | ||
| weights: { similarity: 0.5, recency: 0.3, importance: 0.2 }, | ||
| halfLifeSeconds: 3600, | ||
| maxItemsPerScope: 100, | ||
| }); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
|
|
||
| it('initialize() resolves and close() tears down both tiers', async () => { | ||
| const mem = new AgentMemory(makeOptions()); | ||
| const memoryClose = vi.spyOn(mem.memory, 'close'); | ||
|
|
||
| await expect(mem.initialize()).resolves.toBeUndefined(); | ||
| await mem.close(); | ||
|
|
||
| expect(memoryClose).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('initialize() surfaces a cache discovery collision instead of swallowing it', async () => { | ||
| const mem = new AgentMemory(makeOptions()); | ||
| const cache = (mem as unknown as { cache: { ensureDiscoveryReady: () => Promise<void> } }).cache; | ||
| vi.spyOn(cache, 'ensureDiscoveryReady').mockRejectedValue(new Error('cache name collision')); | ||
|
|
||
| await expect(mem.initialize()).rejects.toThrow(/collision/i); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
|
|
||
| it('registers a memory discovery marker by default', async () => { | ||
| const client = fakeValkey(); | ||
| const mem = new AgentMemory( | ||
| makeOptions({ client: client as unknown as AgentMemoryOptions['client'] }), | ||
| ); | ||
|
|
||
| await mem.initialize(); | ||
|
|
||
| const marker = (client.call.mock.calls as unknown[][]).find( | ||
| (c) => c[0] === 'HSET' && c[1] === '__betterdb:caches', | ||
| ); | ||
| expect(marker).toBeDefined(); | ||
| expect(JSON.parse(marker?.[3] as string).type).toBe('agent_memory'); | ||
| // The memory marker registers under a distinct `{name}:mem` field so it | ||
| // can't clobber an agent_cache marker sharing the same name. | ||
| expect(marker?.[2]).toBe('betterdb_ac:mem'); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
|
|
||
| it('allows disabling memory discovery', async () => { | ||
| const client = fakeValkey(); | ||
| const mem = new AgentMemory( | ||
| makeOptions({ | ||
| client: client as unknown as AgentMemoryOptions['client'], | ||
| memory: { discovery: false }, | ||
| }), | ||
| ); | ||
|
|
||
| await mem.initialize(); | ||
| await mem.close(); | ||
|
|
||
| const marker = (client.call.mock.calls as unknown[][]).find( | ||
| (c) => c[0] === 'HSET' && c[1] === '__betterdb:caches', | ||
| ); | ||
| expect(marker).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('shares one prom registry across the cache and memory tiers', async () => { | ||
| const registry = new Registry(); | ||
| const mem = new AgentMemory(makeOptions({ telemetry: { registry } })); | ||
|
|
||
| await mem.memory.remember('x'); | ||
|
|
||
| const text = await registry.metrics(); | ||
| expect(text).toMatch(/agent_memory_embedding_calls_total/); | ||
| expect(text).toMatch(/agent_cache_/); | ||
|
|
||
| await mem.close(); | ||
| }); | ||
| }); | ||
|
|
||
| // Touch the FakeClient type so it is exercised by the suite. | ||
| export type { FakeClient }; |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.