Memory (Implementation): backend selection, fallback, healing, scope guard

Memory (Implementation): backend selection, fallback, healing, scope guard

This page is the implementation manual for making memory usable, observable, and bounded — without letting it blow up the main conversation. Concept entry points: Memory and the group:memory tools (Tools).

From source (pinned revision 9741e91, 2026-11-05)

Runtime claims below are read from the fixed implementation (source analysis, not runs on this site): fallback wiring in src/memory/search-manager.ts (FallbackMemoryManager), tool behavior in src/agents/tools/memory-tool.ts, startup arming in src/gateway/server-startup-memory.ts.

Key behaviors (summary)

  • A primary search failure can invoke the builtin fallback; fallback search can still fail. readFile does not implement the same automatic retry path.
  • cache eviction enables “retry primary later” self-healing behavior
  • a scope-denied search returns an empty result with a qmd search denied by scope log (policy decision); an unavailable manager makes the tools answer disabled: true (dependency failure) — two different situations with two different observables
  • injected-characters caps apply on the qmd backend’s search results (not a bound on the whole context)
  • startup arming surfaces the “manager is null” case as a warn log at gateway startup; an internal fallback that still produces a manager will not log it — absence of the log is not proof qmd is healthy

The three-layer split (recommended)

  1. config parsing (backend/citations/limits/scope)
  2. manager acquisition (primary qmd → fallback builtin)
  3. tools (memory_search / memory_get) with safe degradation

The key self-healing point: evict cached primary on failure

In the pinned FallbackMemoryManager.search (L102–123): the first primary failure marks primaryFailed, closes the primary, and evicts the cache entry — the source comment is explicit: “Evict the failed wrapper so the next request can retry QMD with a fresh manager.” Subsequent searches go to the fallback; note the limits of this wiring: the fallback’s own search can still throw, and readFile does not repeat this auto-fallback flow — a primary readFile error propagates as-is.

Scope guard: policy boundary, not backend failure

Two different denials with two different observables (pinned revision):

  • scope denial (src/memory/qmd-manager.ts search, ~L634–640): when the session’s scope is not allowed, qmd logs qmd search denied by scope (channel=…, chatType=…, session=…) and returns an empty result list — a normal answer, not an error. This guard lives on the qmd search path; memory_get is not part of this branch.
  • unavailable manager (src/agents/tools/memory-tool.ts): when no manager can be produced, memory_search/memory_get return a JSON result with disabled: true and the reason (the tool description tells the model to surface this to the user).

So when a search comes back empty: a qmd search denied by scope log means policy refused this session. Absence of that log alone doesn’t identify the cause — read it together with the active backend (status()), the fallback/availability logs above, and the tool result (disabled: true vs empty list) before concluding policy, no-match, or dependency.

Injection caps: bound the cost to the window

  • Apply a total injected-characters cap to search results so memory cannot crowd out the conversation — in the pinned source this clamp (clampResultsByInjectedChars(…, qmd.limits.maxInjectedChars)) is applied on the qmd backend branch only; it is not a bound on the whole context window.
  • Have memory_get read slices (from/lines) instead of whole files.

Startup probing: surface dependency failures early

The startup warning qmd memory startup initialization failed for agent "…" is emitted when the manager is null. A usable manager returned after fallback avoids that particular warning, but does not imply silence: search-manager.ts can log “qmd memory unavailable; falling back to builtin: …”, and startup logs “qmd memory startup initialization armed for agent …” for a returned manager. Check the backend and related logs before concluding that qmd is healthy. (Pinned sources: src/memory/search-manager.ts L74, src/gateway/server-startup-memory.ts L28.)

Failure modes and troubleshooting

  • Missing qmd binary/deps: verify automatic fallback to builtin and inspect Logging.
  • Replies slowing down / results crowding the context: inspect the active backend, the QMD search-output cap where applicable, memory_get slice size, and context/compaction diagnostics separately; the search-output cap does not bound the whole conversation (see Memory and Compaction).
  • Memory leaking into group contexts: audit scope rules and how session keys are matched (see Sessions) — a policy boundary, not a backend failure.

Acceptance checks

  1. Primary search failures invoke the fallback when available; fallback search may still fail. readFile errors do not take the same automatic retry path.
  2. cache eviction enables recreating primary later.
  3. scope-denied searches return empty results with the qmd search denied by scope log; unavailable managers return disabled: true JSON — neither throws.
  4. The QMD search-output cap applies only to that search branch; whole-context bounds require separate context/compaction checks.
  5. tools degrade safely (disabled), not fatal throws.