Skip to content

feat(agent-memory): Phase 7 — discovery marker#255

Open
jamby77 wants to merge 2 commits into
feature/agent-memory-phase6-consolidatefrom
feature/agent-memory-phase7-discovery
Open

feat(agent-memory): Phase 7 — discovery marker#255
jamby77 wants to merge 2 commits into
feature/agent-memory-phase6-consolidatefrom
feature/agent-memory-phase7-discovery

Conversation

@jamby77

@jamby77 jamby77 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #254 (Phase 6 — consolidate).

What

Phase 7 of @betterdb/agent-memory: a discovery marker so BetterDB Monitor auto-discovers memory stores on any Valkey it watches, reusing the shared registry protocol from agent-cache.

  • MemoryDiscovery registers a marker on __betterdb:caches keyed by store name: type: 'agent_memory', prefix, version, protocol_version, capabilities: ['recall','consolidate','reinforce'], stats_key: {name}:__mem_stats, plus started_at/pid/hostname.
  • Heartbeats on an unref'd interval, refreshing a TTL'd __betterdb:heartbeat:{name} key and the marker; sets __betterdb:protocol with NX.
  • Detects a name collision with a different cache type and throws; overwrites a same-type marker.
  • MemoryStore gains an opt-in discovery option (registers on construct) and close() to stop the heartbeat and delete the marker.

Design / reuse notes

  • The agent-cache DiscoveryManager uses a method-style Valkey client and hard-codes its cache type in the collision check, while MemoryStore is built on a .call-only client. Rather than refactor the merged manager (and its FakeClient tests), this reuses the protocol as the single source of truth — agent-cache now additively re-exports its discovery constants (REGISTRY_KEY, PROTOCOL_KEY, HEARTBEAT_KEY_PREFIX, TTL/interval, PROTOCOL_VERSION, MarkerMetadata) — and implements the register/heartbeat loop against .call, so Monitor reads the memory marker identically to a cache marker.
  • Registration is fire-and-forget (construction stays sync); a rejected registration (collision) can't leak as an unhandled rejection, and close() awaits readiness before teardown.
  • Best-effort writes (an onWriteFailed hook is in place for the Phase 9 observability wiring).

Tests

  • discovery.test.ts (8): marker shape/capabilities/stats_key · protocol NX + heartbeat TTL · collision throws (and writes nothing) · same-type overwrite · stop deletes heartbeat · tickHeartbeat re-writes · interval fires (fake timers) · best-effort on write failure.
  • MemoryStore.discovery.test.ts (2): opt-in registers on construct + close() tears down · disabled → no registry I/O.

@betterdb/agent-cache unchanged behaviorally (253/253 still green after the additive export). agent-memory: 65/65 green · tsc clean · prettier clean.


Note

Low Risk
Discovery is opt-in and additive; registry I/O is best-effort and mirrors existing agent-cache patterns without changing recall/remember paths.

Overview
Adds opt-in Valkey discovery for @betterdb/agent-memory so BetterDB Monitor can find memory stores the same way it finds agent caches.

MemoryDiscovery registers an agent_memory marker on the shared __betterdb:caches registry (field {name}:mem to avoid clashing with an agent-cache of the same name), sets protocol/heartbeat keys from the agent-cache protocol, runs an unref’d heartbeat loop, throws on cross-type name collision, and treats registry writes as best-effort.

MemoryStore accepts discovery?: boolean | MemoryDiscoveryConfig, starts registration asynchronously on construct, and adds close() to await registration and tear down the heartbeat key.

@betterdb/agent-cache only additively re-exports discovery constants/types (REGISTRY_KEY, PROTOCOL_KEY, heartbeat TTL/interval, MarkerMetadata, etc.); existing cache behavior is unchanged.

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

@jamby77 jamby77 force-pushed the feature/agent-memory-phase6-consolidate branch from 5cdeb4c to c317978 Compare June 18, 2026 06:57
@jamby77 jamby77 force-pushed the feature/agent-memory-phase7-discovery branch from 524f3a8 to e58dbb7 Compare June 18, 2026 06:57
@jamby77 jamby77 force-pushed the feature/agent-memory-phase6-consolidate branch from c317978 to d5a95b9 Compare June 18, 2026 07:19
@jamby77 jamby77 force-pushed the feature/agent-memory-phase7-discovery branch from e58dbb7 to 0d89d67 Compare June 18, 2026 07:19
jamby77 added 2 commits June 18, 2026 10:27
- MemoryDiscovery registers an agent_memory marker on the shared
  __betterdb:caches registry: type 'agent_memory', prefix, version,
  capabilities ['recall','consolidate','reinforce'], stats_key
- Reuses the agent-cache discovery protocol constants (additively
  re-exported) so Monitor reads memory markers identically; writes via
  MemoryStore's .call client (no method-style client needed)
- Heartbeat on an unref'd interval with a TTL'd heartbeat key; best-effort
  writes; name-collision detection against a different cache type
- MemoryStore gains an opt-in discovery option and close() to stop the
  heartbeat and remove the marker
- Re-export discovery constants/MarkerMetadata from @betterdb/agent-cache
…:mem

Register the agent_memory marker under the {name}:mem registry field and
heartbeat key so a memory store and an agent-cache sharing the same name
no longer collide on the same __betterdb:caches field / heartbeat key
(reported on the AgentMemory facade, which discovers both tiers).
@jamby77 jamby77 force-pushed the feature/agent-memory-phase6-consolidate branch from d5a95b9 to e27477f Compare June 18, 2026 07:28
@jamby77 jamby77 force-pushed the feature/agent-memory-phase7-discovery branch from 0d89d67 to 73d91d7 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