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 scopelog (policy decision); an unavailable manager makes the tools answerdisabled: 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)
- config parsing (backend/citations/limits/scope)
- manager acquisition (primary qmd → fallback builtin)
- 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.tssearch, ~L634–640): when the session’s scope is not allowed, qmd logsqmd 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_getis not part of this branch. - unavailable manager (
src/agents/tools/memory-tool.ts): when no manager can be produced,memory_search/memory_getreturn a JSON result withdisabled: trueand 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_getread 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
- Primary search failures invoke the fallback when available; fallback search may still fail. readFile errors do not take the same automatic retry path.
- cache eviction enables recreating primary later.
- scope-denied searches return empty results with the
qmd search denied by scopelog; unavailable managers returndisabled: trueJSON — neither throws. - The QMD search-output cap applies only to that search branch; whole-context bounds require separate context/compaction checks.
- tools degrade safely (disabled), not fatal throws.