Skip to content

feat(agent-memory): Phase 9 — observability#257

Open
jamby77 wants to merge 2 commits into
feature/agent-memory-phase8-configfrom
feature/agent-memory-phase9-observability
Open

feat(agent-memory): Phase 9 — observability#257
jamby77 wants to merge 2 commits into
feature/agent-memory-phase8-configfrom
feature/agent-memory-phase9-observability

Conversation

@jamby77

@jamby77 jamby77 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #256 (Phase 8 — self-optimization config reads).

What

Phase 9 of @betterdb/agent-memory: OpenTelemetry spans + Prometheus metrics, mirroring the agent-cache/semantic-cache telemetry pattern.

createMemoryTelemetry({ tracerName?, metricsPrefix?, registry? }) builds a prom-client metric set + an OTel tracer. Metrics (prefix configurable, default agent_memory):

  • agent_memory_items (gauge)
  • agent_memory_recall_total / agent_memory_recall_hits_total / agent_memory_recall_empty_total
  • agent_memory_recall_latency_seconds (histogram)
  • agent_memory_embedding_calls_total
  • agent_memory_evictions_total
  • agent_memory_consolidations_total

Spans via a traced() helper (OK/ERROR status, always end()):

  • agent_memory.remembermemory.importance, memory.ttl
  • agent_memory.recallrecall.k, recall.candidate_count, recall.result_count
  • agent_memory.consolidateconsolidate.candidates, consolidate.created, consolidate.deleted

Metrics are wired through remember/recall/consolidate/forget/forgetByScope/embed and the capacity-eviction path. All store_name-labelled.

Design / review notes

  • Optional & no-op by default: the OTel tracer is a no-op until a provider is registered; metrics use prom-client's default registry unless one is passed — same as agent-cache. Telemetry creation does no I/O, so the existing .call-sequence unit tests are unaffected.
  • The items gauge is an in-process net delta (documented as approximate). Eviction/forget/consolidate decrement it; remember increments it.
  • A failed operation surfaces via the span's ERROR status rather than a success counter (e.g. the consolidate no-criteria guard does not count a consolidation).

Deps

Adds @opentelemetry/api, prom-client (deps, matching agent-cache) and @opentelemetry/sdk-trace-base (devDep, for the in-memory span exporter used in tests). Lockfile updated.

Tests

  • MemoryStore.telemetry.test.ts (6): embedding+items on remember · recall hit (+latency count) · empty recall · evictions · consolidations · configurable prefix — all read a real prom Registry.
  • MemoryStore.spans.test.ts (3): remember/recall/consolidate spans + attributes, asserted via an InMemorySpanExporter.

86/86 package tests green · tsc clean · prettier clean.


Note

Low Risk
Observability is additive and optional; the eviction counting fix only makes metrics/stats more accurate when the search index is stale.

Overview
Adds Phase 9 observability to @betterdb/agent-memory: optional telemetry on MemoryStore (and exported createMemoryTelemetry) following the same pattern as agent-cache—no-op OTel until a provider is registered, prom-client metrics on the default or a passed Registry.

remember, recall, and consolidate run inside a traced() helper that emits agent_memory.* spans with operation-specific attributes and OK/ERROR status. Recall also records totals, hit/empty counters, and a latency histogram; writes/deletes adjust an in-process items gauge; embed, eviction, and consolidation paths increment their counters (all labelled by store_name).

Capacity eviction now increments __mem_stats evictions and Prometheus using the DEL reply count, not the number of keys requested—so stale index entries do not inflate stats (new eviction test covers this).

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

@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from eea7699 to ee647cc Compare June 17, 2026 14:50
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch 2 times, most recently from 339567e to 312772a Compare June 17, 2026 14:58
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from 14b6e7c to 9412e5a Compare June 18, 2026 06:57
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from 312772a to 22b6f78 Compare June 18, 2026 06:57
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from 9412e5a to 37c7a4b Compare June 18, 2026 07:19
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from 22b6f78 to b160dd0 Compare June 18, 2026 07:19
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from 37c7a4b to 06b56f6 Compare June 18, 2026 07:28
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch 2 times, most recently from b29dd7e to 4ea754e Compare June 18, 2026 07:35
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from 7d0fa87 to 90ac526 Compare June 19, 2026 09:51
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from 4ea754e to 8711302 Compare June 19, 2026 09:51
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from 90ac526 to f325dee Compare June 19, 2026 10:01
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from 8711302 to 41e8dfe Compare June 19, 2026 10:01

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 41e8dfe. Configure here.

Comment thread packages/agent-memory/src/MemoryStore.ts Outdated
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from f325dee to 94e4101 Compare June 19, 2026 11:47
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from a97fc05 to ef0538d Compare June 19, 2026 11:47
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from 94e4101 to ff2e1f1 Compare June 19, 2026 13:10
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from ef0538d to 6bdf7dc Compare June 19, 2026 13:10
jamby77 added 2 commits June 19, 2026 16:33
- Add createMemoryTelemetry: prom-client metrics (items gauge, recall
  total/hits/empty, recall latency histogram, embedding calls, evictions,
  consolidations) with a configurable prefix, plus an OTel tracer
- Emit spans for remember/recall/consolidate via a traced() helper with
  OK/ERROR status, recording k/candidate-count/result-count and
  candidate/created/deleted attributes
- Wire metrics through remember/recall/consolidate/forget/forgetByScope/
  embed and the capacity-eviction path
- Telemetry option (tracerName/metricsPrefix/registry); no-op tracer and
  default registry when unset, mirroring agent-cache
- Add @opentelemetry/api + prom-client deps and the OTel SDK dev dep
…trics

enforceCapacity bumped the evictions stat/counter and decremented the items
gauge by evictKeys.length without reading the DEL reply, so a stale index
(keys already gone) drifted Prometheus. Use the actual removal count, guarded
> 0, matching forget/forgetByScope/consolidate.
@jamby77 jamby77 force-pushed the feature/agent-memory-phase8-config branch from ff2e1f1 to 0efd8ba Compare June 19, 2026 13:34
@jamby77 jamby77 force-pushed the feature/agent-memory-phase9-observability branch from 6bdf7dc to 9f72079 Compare June 19, 2026 13:34
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.

2 participants