Hyeonsang Cho 9146bfa03d
feature(gateway): issue request trace ids unconditionally (#5119)
* refactor(gateway): issue request trace ids unconditionally

The request trace id was gated behind logging.enhance.enabled at every
entry point, so downstream code had to keep asking whether one existed:
a header-provenance flag in its own ContextVar, a precedence resolver,
and three-level carrier fallbacks at each consumer.

Bind one unconditionally instead. TraceMiddleware covers Gateway HTTP;
ensure_trace_context covers the entry points that never touch ASGI --
scheduled occurrences, MCP task notification runs, IM channel messages,
and the embedded client -- each scoped to one unit of work so a
long-lived worker task cannot leak one occurrence's id into the next.
The ContextVar becomes the only source; the response header, runtime
context, run metadata and log records are derived outputs.

Consumers now use ensure_trace_id() or resolve_trace_id(*carriers) and
drop their presence guards. Removed: resolve_deerflow_trace_id, the
header-provenance flag and its three helpers, set/reset_current_trace_id,
is_trace_correlation_enabled and its gateway alias.

BREAKING CHANGE: every Gateway HTTP response now carries X-Trace-Id and
it cannot be turned off; logging.enhance.enabled controls log output
only. Installations on the default enabled: false will start seeing the
header. No config keys were added or removed.

* fix(gateway): stop persisting a caller-supplied trace id on the run record

body.metadata forks two ways: through build_run_config into the live run
config, which the run worker restamps, and through create_or_reject into
the run record that the runs API echoes verbatim. Only the first was
covered, so a client sending metadata.deerflow_trace_id made the most
durable and most visible surface of a run disagree with the X-Trace-Id
and the log lines the same request produced -- a correlation id that
does not match the logs is worse than none.

Stamp the server-issued id once at the trust boundary so both forks
receive it, preserving the caller's own metadata keys. Close the same
gap on config.context, which reaches the runtime context by a separate
path: _build_runtime_context no longer merges server-owned keys from the
caller, and _install_runtime_context assigns rather than setdefaults.

A thread's metadata is no longer seeded with the run-scoped id of
whichever run created it -- one thread spans many runs and as many
trace ids.

Found by driving a real run through the Gateway and reading the run back
from the runs API; every unit test built its metadata by hand and so
could not see it.

* fix(gateway): expose X-Trace-Id to split-origin browser clients

X-Trace-Id is not on the CORS safelist, so a browser client served from
a separate origin could not read it -- and those are exactly the clients
that cannot read the Gateway's logs either, leaving them with nothing to
quote in a bug report. Same-origin nginx deployments were unaffected,
which is why this stayed hidden.

Add it to CORS_EXPOSED_HEADERS beside Content-Location, referencing
TRACE_ID_HEADER rather than repeating the literal.

* fix(gateway): keep X-Trace-Id on unhandled-exception 500s

Starlette's ServerErrorMiddleware sits outside every user middleware and
emits unhandled-exception 500s through the raw send, so those responses
never pass TraceMiddleware's header-writing wrapper. The 500 for a server
bug is exactly the response a user most needs to correlate with a log line,
and it was the one response that shipped without the id.

TraceMiddleware now tracks whether http.response.start has been sent. On an
exception with no response started it emits its own plain 500 carrying the
header, then re-raises: the outer ServerErrorMiddleware sees the response
already started and only re-raises too, so the server's exception logging is
untouched. An exception mid-stream keeps propagating unchanged — a second
response start cannot be sent, and the already-written header stands.

The trace id is printable ASCII by construction (normalize_trace_id /
generate_trace_id), which is what makes the raw latin-1 header encoding
safe.

* fix(gateway): strip the forged trace id from the persisted request echo

The run-record fix stopped a forged metadata.deerflow_trace_id on the
authoritative metadata surface, but the raw request echo still carried one:
create_or_reject persists body.config verbatim as runs.kwargs_json, which
the runs API serves back. A client posting config.context.deerflow_trace_id
therefore still got its forged value stored and echoed on one API surface
while the header, logs, run metadata, and checkpoint all carried the real
id — the id is ignored as input there, so echoing it back only manufactures
disagreement.

Two changes close it. redact_config_secrets — already the shared scrub for
that echo, applied at admission and again at serve time, so historical
records are covered too — now also drops deerflow_trace_id from
config.metadata and config.context. And build_run_config now merges run
metadata onto a copy of the caller's config["metadata"] instead of updating
it in place: the nested values of the request config are reference copies,
so the in-place merge was writing the server-stamped key through into
body.config, contaminating the "what the client sent" record before it was
persisted (and incidentally masking the forged-value echo on the metadata
container).

The regression test posts a forged id through body.metadata,
config.metadata, and config.context at once and reads the kwargs echo back
off the run record, failing if either leak returns.

* docs(harness): record the trace-echo scrub, 500 fallback, and accepted retry divergence

The trace section of the harness AGENTS.md now covers the two fixes that
close the derived-output rule (the kwargs-echo scrub in
redact_config_secrets plus build_run_config's copy merge, and
TraceMiddleware's own 500 for unhandled exceptions), and CHANGELOG gains
their Fixed entries.

It also writes down the one accepted divergence: a crash-recovered
scheduled launch reuses the durable run through its idempotency key, and
start_run returns early on idempotency_reused without restamping — so the
run record keeps the first attempt's deerflow_trace_id while the retry's
own log lines carry the freshly minted id of its ensure_trace_context
binding. The divergence is confined to the crash-recovery window and is
accepted rather than fixed: restamping on reuse would rewrite a persisted
record for a run that already exists, which is worse than two ids that each
correlate their own attempt's logs. Written down so the next reader of the
scheduler recovery path does not diagnose it as a bug.

* docs(config): align the logging.enhance schema note with the unconditional trace id

The config-module AGENTS.md still described logging.enhance as the gate for
the Gateway X-Trace-Id header and Langfuse deerflow_trace_id. That model is
gone: ids are issued unconditionally and this block decides log output only.
Left as-is, the stale wording invites an agent to "restore" a header gate it
believes was lost. Reworded to match the sibling AGENTS.md files and
config.example.yaml, with a pointer to the Request Trace Context section
that owns the full model.

* docs(changelog): link the trace entries to #5119

The five new entries pointed at the ([#XXXX]) placeholder with no reference
definition, rendering as literal text instead of a link — and RELEASING.md
step 2 relies on those references when the section becomes release notes.
All five now point at #5119, with the definition appended to the reference
block.

* refactor(harness): rename _stream_without_trace_context to _stream_turn

The name asserted the opposite of what the method now does. It was accurate
while logging.enhance.enabled could route stream() around the trace scope;
with the gate gone it is the only stream implementation left, and it binds
the id itself via ensure_trace_id(). Private, so the rename touches only the
definition and the one stream() call site.

* docs(harness): fit the trace-context guidance inside the AGENTS.md chain budget

The expanded Request Trace Context section pushed the effective AGENTS.md
chain for agents/middlewares to 99,815 bytes, past the 98,304 hard limit
scripts/check_agent_guidance.py enforces in CI (AG002). Compressed the
section from 7,359 to 4592 bytes with no facts removed: the entry-point
table, the derived-output rule and its enforcement points, the accepted
scheduled-retry divergence, the two resolution helpers, the stream()
binding rationale, the log-output-only gate, the CORS listing, the 500
fallback, and the test map all remain.

Sized against the merge, not just the branch: current main grew the same
chain by ~724 bytes, so the check was verified on the merged tree as well
(97,772 bytes; branch tree 97,048).

* fix(gateway): declare content-length on the fallback 500

The pre-response 500 declared content-type but no content-length, leaving
the framing to the ASGI server: chunked on HTTP/1.1, close-delimited on
HTTP/1.0 — the one wire difference from the ServerErrorMiddleware response
it replaces, which sends content-length: 21. The explicit header keeps the
fallback byte-identical to what clients saw before.

* docs(readme): drop the trace-correlation condition from the translations

The zh/ja/fr/ru Langfuse sections still said metadata.deerflow_trace_id
matches X-Trace-Id "when request trace correlation is enabled". The id now
always matches and that condition no longer exists, so each bullet states
the unconditional match and that logging.enhance.enabled only controls
whether the id is printed into logs — the one piece of the feature a user
can still configure.

* test(gateway): pin TraceMiddleware wiring through create_app()

Every X-Trace-Id test exercised a hand-built four-route app, so the real
stack's add_middleware(TraceMiddleware) line was pinned by nothing: deleting
it — or short-circuiting above it — passed CI while silently dropping both
the response header and the ambient id the run-record stamp and enhanced log
records derive from. One case now drives /health through create_app() and
asserts the inbound id round-trips; mutation-checked by removing the wiring
line, which fails exactly this test.

* docs(gateway): note the fallback 500 is CORS-opaque

The pre-response 500 is emitted outside CORSMiddleware — the exception has
already unwound past it — so it carries no Access-Control-Allow-Origin and
a split-origin browser client cannot read the id on this one response,
unchanged from the ServerErrorMiddleware 500 it replaces. Documented on the
class and in the CHANGELOG entry rather than fixed: replicating the origin
allowlist outside CORSMiddleware would let the two policies drift.

* fix(harness): keep abandoned-stream cleanup inside the trace binding

stream() binds the turn's id around each next(inner) and resets it before
yielding, but the finally's inner.close() ran after that binding was gone.
Abandoning the stream therefore drove the inner LangGraph generator's
GeneratorExit/finally path with no trace id — or an unrelated ambient one
from whichever context ran the close — so cancellation and finalization
logs and callbacks did not correlate with the turn they belong to.

inner.close() is now wrapped in a local bind/reset of the same turn id. The
token is set and reset in the same frame, never across a yield, so the
per-step cross-context safety is preserved even when GC closes the
generator from another Context — pinned by the existing copy_context close
test, which now exercises this path. The regression test records the id
from the inner generator's finally and fails without the binding.

* test(harness): teach the worker-trace fake about RunManager.cleanup

Upstream #5112 (bound gateway memory after terminal runs) added a
run_manager.cleanup(run_id) call to run_agent's finalization, so the
merge-commit CI run failed all five worker-trace-binding tests with
AttributeError on this PR's _FakeRunManager. The fake gains the same no-op
shape as its other methods.

* docs(gateway): bring the gateway AGENTS.md back under its soft budget

Upstream #5092 grew backend/app/gateway/AGENTS.md to 40,966 bytes, 6 over
the 40,960 soft budget that
test_agent_guidance_check.py::test_repository_guidance_stays_below_soft_budgets_and_avoids_doc_indexes
enforces — its Unit Tests run on main was cancelled by push concurrency, so
main is currently red on that test and every PR merge-run inherits the
failure. Two whitespace/wording trims in the row #5092 touched (a doubled
space, and "its configured `context_window`" → "its `context_window`")
bring the file to 40,953 with no content change.

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-09-01 16:49:39 +08:00

913 lines
40 KiB
Python

"""Memory manager contract + pluggable backend factory.
This module is the shared, backend-agnostic core of the memory package. It
defines the :class:`MemoryManager` interface (9 methods) that every backend
implements, plus a singleton :func:`get_memory_manager` factory that resolves
the active backend from ``MemoryConfig.manager_class``.
Swap backend = drop a ``backends/<name>/`` folder exposing ``MANAGER_CLASS``
and set ``manager_class: <name>``. Nothing else in deer-flow changes.
Scope note: this phase is *pluggable only*, not black-box. Agent-side
conventions (``enabled`` gating at call sites, ``<memory>`` wrapping in
``_get_memory_context``) stay where they are; they are backend-agnostic and
do not impede pluggability.
"""
from __future__ import annotations
import importlib
import logging
import os
import threading
from abc import abstractmethod
from pathlib import Path
from types import ModuleType
from typing import Any, ClassVar, Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from deerflow.config.memory_config import get_memory_config
logger = logging.getLogger(__name__)
# Backend packages live in <this dir>/backends/<name>/.
_BACKENDS_DIR = Path(__file__).parent / "backends"
# Sentinel attribute each backend's __init__ exposes (a MemoryManager subclass).
_MANAGER_CLASS_ATTR = "MANAGER_CLASS"
# Singleton instance + backend-registry cache (reset together by reset_memory_manager).
# _manager_lock guards get_memory_manager()'s double-checked init (multi-threaded).
_memory_manager: MemoryManager | None = None
_backends_cache: dict[str, type[MemoryManager]] | None = None
_manager_lock = threading.Lock()
class MemoryCallbacks:
"""Observability hooks for memory backends. Default implementations are
no-ops; override the ones you need. The pre-LLM-call hook
``on_memory_llm_call`` mutates ``invoke_config`` before the LLM call so a
tracer (e.g. langfuse) emits a span at the LLM boundary. (More hooks --
post-extract / search / inject / error -- can be added when callers need
them.)"""
def on_memory_llm_call(
self,
invoke_config: dict[str, Any],
*,
thread_id: str | None,
user_id: str | None,
trace_id: str | None,
model_name: str | None,
) -> None:
"""Pre-LLM-call: mutate ``invoke_config`` (e.g. merge trace metadata)
before the backend invokes the model. Default: no-op."""
def on_memory_llm_result(
self,
invoke_config: dict[str, Any],
*,
prompt: Any,
response: Any,
error: BaseException | None,
duration_ms: float,
model_name: str | None,
) -> None:
"""Post-LLM-call hook for host-owned observation. Default: no-op.
This callback keeps the vendorable DeerMem backend independent from
DeerFlow's extension API. It is invoked for both provider success and
failure, and backend callers isolate exceptions raised by an
implementation.
"""
class MemoryManagerError(RuntimeError):
"""Backend-neutral base error exposed at the MemoryManager boundary."""
class MemoryConflictError(MemoryManagerError):
"""The requested write lost an optimistic-concurrency race."""
class MemoryCorruptionError(MemoryManagerError):
"""Persisted memory cannot be read safely."""
class MemoryManager(BaseModel):
"""Backend-neutral memory manager contract.
A pydantic ``BaseModel`` (not a bare ``ABC``) so the contract gains field
validation + serialization for free and shares the pydantic v2 type system
with backend configs (e.g. ``DeerMemConfig``). Subclasses still MUST
implement the ``@abstractmethod``s -- pydantic's ``ModelMetaclass`` derives
from ``ABCMeta``, so unimplemented abstractmethods raise ``TypeError`` at
instantiation (memory is persistent state; a backend missing ``add`` /
``get_context`` is a severe bug, caught at construction). Backend-private
dependencies (storage / llm / queue / ...) are NOT fields -- they are
``PrivateAttr`` set in ``model_post_init`` (or ``from_config``), kept out of
validation / serialization.
Memories are bucketed per ``(agent_name, user_id)``; ``thread_id`` aligns
with the deer-flow conversation thread. The contract is deliberately
neutral so a third-party memory system can be adapted without deer-flow
code changes:
- :meth:`get_context` returns plain injection text; the *format* is the
implementation's own choice and is NOT part of the contract (DeerMem
does load + ``format_memory_for_injection``; another backend may do
its own search + formatting).
- :meth:`add` / :meth:`add_nowait` take raw conversation messages; any
filtering / correction-/reinforcement-detection is the implementation's
private concern (not on the contract).
- No facts-model assumption: a backend need not store "facts" at all.
Methods are tiered: tier-1 (``add`` / ``get_context``) are ``@abstractmethod``;
tier-2 management ops and tier-3 optional hooks carry defaults (``raise
NotImplementedError`` or a no-op) so a backend implements only what it
supports. ``delete_memory`` / ``export_memory`` are dead contract (zero
callers; ``/memory/export`` routes via ``get_memory``) kept available via the
default raise.
"""
model_config = ConfigDict(arbitrary_types_allowed=True)
# Backend-private config (factory passes it through). Backends that need to
# parse it (DeerMem -> DeerMemConfig) do so in model_post_init / from_config.
# None is coerced to {} so zero-config ``Backend(backend_config=None)`` stays
# valid (BaseModel would otherwise reject None for a dict field).
backend_config: dict[str, Any] = Field(default_factory=dict)
# Operation mode mirrors host ``MemoryConfig.mode`` ("middleware" | "tool");
# the factory passes ``cfg.mode``. The invariant validator requires
# tool-mode backends to support search.
mode: Literal["middleware", "tool"] = "middleware"
# Observability callbacks (optional; a ``MemoryCallbacks`` instance). The
# factory injects ``LangfuseMemoryCallbacks`` so memory-LLM calls surface in
# langfuse; None = no callbacks (direct construction / standalone). Backends
# pass this to their LLM path and call ``on_memory_llm_call`` before invoke.
callbacks: MemoryCallbacks | None = None
@field_validator("backend_config", mode="before")
@classmethod
def _coerce_backend_config(cls, value: Any) -> dict[str, Any]:
"""Accept None (zero-config) as an empty dict; leave dicts untouched."""
return value or {}
# Search capability flag (ClassVar, not a field): set True iff the backend
# overrides search(). The invariant validator checks the flag MATCHES whether
# search() is actually overridden (type(self).search is not MemoryManager.search),
# so the two can't drift -- required for mode="tool" (the agent calls
# memory_search in tool mode, so a non-search backend is a misconfiguration
# that fails fast at instantiation rather than silently returning empty
# results). Default False: a new backend must explicitly opt in to tool mode.
supports_search: ClassVar[bool] = False
# Backends that rely on conversation-level extraction instead of fact CRUD
# can retain MemoryMiddleware writes while tool mode supplies query-aware
# search. Most backends keep tool mode fully model-directed.
requires_passive_writes_in_tool_mode: ClassVar[bool] = False
@model_validator(mode="after")
def _check_invariants(self) -> MemoryManager:
"""Cross-field invariants every backend must satisfy at instantiation.
Fires on the factory path AND when a backend is constructed directly
(bypassing the factory), since it lives on the base model. DeerMem-private
invariants (e.g. storage_path is a directory) stay on ``DeerMemConfig``.
``supports_search`` (ClassVar flag) must match whether ``search()`` is
actually overridden, so the declarative flag can't drift from the
implementation -- a backend that overrides ``search()`` but forgets
``supports_search = True`` (or sets the flag without overriding) is a bug
caught at instantiation, not a misleading tool-mode rejection or a runtime
``NotImplementedError`` on the first ``memory_search`` call.
"""
search_overridden = type(self).search is not MemoryManager.search
if type(self).supports_search != search_overridden:
raise ValueError(
f"{type(self).__name__}.supports_search={type(self).supports_search} "
f"is inconsistent with search(): search() is "
f"{'overridden' if search_overridden else 'inherited (not implemented)'}. "
f"Set supports_search={search_overridden} on the backend to match."
)
if self.mode == "tool" and not search_overridden:
raise ValueError(
f"memory mode='tool' requires a backend that implements search(), but {type(self).__name__} does not override search(). Use mode='middleware' or a backend that overrides search() (and sets supports_search=True)."
)
return self
# ── Tier 1: @abstractmethod ─────────────────────────────────────────
# Every backend MUST implement these (write + read-inject are the backend's
# fundamental duties). Missing one is a severe bug (memory is persistent
# state) -- @abstractmethod catches it at instantiation. noop implements
# them as no-op / "".
@abstractmethod
def add(
self,
thread_id: str,
messages: list[Any],
*,
agent_name: str | None = None,
user_id: str | None = None,
trace_id: str | None = None,
) -> None:
"""Queue a conversation for memory update (debounced, asynchronous).
Args:
thread_id: Conversation thread id.
messages: Raw conversation messages; the implementation filters
to user inputs + final assistant responses itself.
agent_name: Per-agent bucket; ``None`` = global memory.
user_id: Per-user bucket.
trace_id: Request trace id captured for memory-LLM tracing.
"""
@abstractmethod
def get_context(
self,
user_id: str | None,
*,
agent_name: str | None = None,
thread_id: str | None = None,
) -> str:
"""Return injection-ready memory text for the given bucket.
Implementations load their memory and format it however they choose;
the returned string is injected verbatim by call sites. Format
parameters are the backend's own private config (received via
``backend_config`` at construction), NOT a host config on this method.
"""
# ── Tier 2: management ops with defaults ────────────────────────────
# A backend that does not support an operation inherits the default (raise
# ``NotImplementedError``) instead of having to write the raise. ``add_nowait``
# defaults to delegating to ``add`` (a backend without a debounce queue has no
# "immediate" vs "queued" distinction); ``shutdown_flush`` defaults to True (a
# backend without a buffer has nothing to drain) so the host can call it
# unconditionally. ``delete_memory`` / ``export_memory`` are dead contract
# (zero callers; /memory/export routes via get_memory) -- the default raise
# keeps them available without forcing backends to implement.
def add_nowait(
self,
thread_id: str,
messages: list[Any],
*,
agent_name: str | None = None,
user_id: str | None = None,
) -> None:
"""Queue a conversation for *immediate* memory update (emergency flush).
Used right before summarization removes messages from state, so the
content is captured instead of lost. Default: delegate to :meth:`add`
(backends without a debounce queue override to enqueue with nowait
priority).
"""
self.add(thread_id, messages, agent_name=agent_name, user_id=user_id)
def search(
self,
query: str,
top_k: int = 5,
*,
user_id: str | None = None,
agent_name: str | None = None,
category: str | None = None,
) -> list[dict[str, Any]]:
"""Search the bucket's memory for facts matching ``query``; return up to
``top_k`` ranked by relevance. ``category`` (optional) filters BEFORE the
``top_k`` slice so a category-scoped search is not starved by other
categories' higher-ranked facts. Default: unsupported (raise); backends
with retrieval override AND set ``supports_search = True`` (required for
``mode='tool'``)."""
raise NotImplementedError(f"search not supported by {type(self).__name__}")
def get_memory(
self,
*,
user_id: str | None = None,
agent_name: str | None = None,
) -> dict[str, Any]:
"""Return the full memory document for the bucket. Default: unsupported."""
raise NotImplementedError(f"get_memory not supported by {type(self).__name__}")
def delete_memory(
self,
*,
user_id: str | None = None,
agent_name: str | None = None,
) -> None:
"""Delete the entire memory document for the bucket. Default: unsupported
(dead contract -- zero callers)."""
raise NotImplementedError(f"delete_memory not supported by {type(self).__name__}")
def clear_memory(
self,
*,
user_id: str | None = None,
agent_name: str | None = None,
) -> dict[str, Any]:
"""Clear the bucket's memory; return the cleared (now-empty) document.
``agent_name=None`` means all memory owned by the user. An explicit
agent name clears only that agent's memory and must preserve shared
user-level summaries. Default: unsupported (raise
``NotImplementedError``); backends that support clearing override.
"""
raise NotImplementedError(f"clear_memory not supported by {type(self).__name__}")
def import_memory(
self,
memory_data: dict[str, Any],
*,
user_id: str | None = None,
agent_name: str | None = None,
) -> dict[str, Any]:
"""Import a memory document into the bucket; return the merged result.
Default: unsupported."""
raise NotImplementedError(f"import_memory not supported by {type(self).__name__}")
def export_memory(
self,
*,
user_id: str | None = None,
agent_name: str | None = None,
) -> dict[str, Any]:
"""Export the memory document for the bucket. Default: unsupported (dead
contract -- zero callers; /memory/export routes via get_memory)."""
raise NotImplementedError(f"export_memory not supported by {type(self).__name__}")
def shutdown_flush(self, timeout: float) -> bool:
"""Best-effort bounded drain of pending updates on graceful shutdown.
Runs on the Gateway shutdown path (after IM channels and the scheduler
stop, so no new IM/scheduler updates arrive during the drain) to flush
updates still sitting in the backend's debounce buffer. Without it, any
update enqueued since the last timer fire is lost on restart / rolling
deploy / SIGTERM, because the buffer is pure in-memory and the debounce
worker is a daemon thread killed on process exit.
Implementations must honour a *hard* ``timeout``: the drain makes a
synchronous LLM call that cannot be interrupted, so the caller (the
Gateway lifespan) needs a real upper bound that lines up with the K8s
``terminationGracePeriodSeconds`` (the drain must finish inside the pod
grace window, or K8s SIGKILLs it mid-drain and the loss the drain is
fixing is silently re-introduced).
Returns ``True`` if the drain genuinely finished within ``timeout``
(buffer empty, no worker still running, no exception); ``False`` on
timeout or failure (the caller logs a warning and proceeds to exit --
any unfinished tail is dropped, strictly better than no flush). Default:
``True`` (a backend with no buffer has nothing to drain), so the host may
call this unconditionally when memory is enabled; backends with a debounce
queue override to flush within ``timeout``.
"""
return True
# ── Tier 3: optional hooks ──────────────────────────────────────────
# A-class: agent-side has real callers (startup warm-up, manual reload, fact
# CRUD). Previously reached via ``hasattr`` probing; now contracted with
# defaults so callers invoke directly and catch ``NotImplementedError``.
# ``warm`` defaults to None (nothing to warm); the rest default to raise.
def warm(self) -> bool | None:
"""Pre-warm backend resources at startup (e.g. the tiktoken encoding
cache). Probed off the event loop by the Gateway lifespan.
Return contract (tri-state so the host logs accurately):
* ``True`` -- warmed successfully (or already cached / unnecessary).
* ``False`` -- warming was attempted and failed (host falls back).
* ``None`` -- this backend has nothing to warm (the default). The
host logs a "skipping" message instead of the misleading "warmed
successfully", so a non-DeerMem backend doesn't claim a tiktoken
cache it never touched.
Backends with heavy one-time init override and return ``True``/``False``.
"""
return None
def reload_memory(
self,
*,
user_id: str | None = None,
agent_name: str | None = None,
) -> dict[str, Any]:
"""Drop the cached memory document and reload from storage. Default:
unsupported (callers fall back to :meth:`get_memory`). Backends with a
cache override."""
raise NotImplementedError(f"reload_memory not supported by {type(self).__name__}")
def create_fact(
self,
content: str,
category: str = "context",
confidence: float = 0.5,
*,
agent_name: str | None = None,
user_id: str | None = None,
) -> tuple[dict[str, Any], str | None]:
"""Manually add one fact. Returns ``(memory_data, fact_id)`` -- ``fact_id``
is None when a storage cap evicted the just-added fact. Default: unsupported."""
raise NotImplementedError(f"create_fact not supported by {type(self).__name__}")
def delete_fact(
self,
fact_id: str,
*,
agent_name: str | None = None,
user_id: str | None = None,
) -> dict[str, Any]:
"""Delete one fact by id. Default: unsupported."""
raise NotImplementedError(f"delete_fact not supported by {type(self).__name__}")
def update_fact(
self,
fact_id: str,
content: str | None = None,
category: str | None = None,
confidence: float | None = None,
*,
agent_name: str | None = None,
user_id: str | None = None,
) -> dict[str, Any]:
"""Update one fact by id (preserving omitted fields). Default: unsupported."""
raise NotImplementedError(f"update_fact not supported by {type(self).__name__}")
# B-class: no agent-side caller yet -- signatures only, for future scenarios.
# Default no-op so callers can invoke unconditionally without gating. (The
# self-serving hooks on_delegation / on_session_end / on_memory_write are
# deliberately NOT contracted: no caller, no event source, or subsumed by
# the callbacks field.)
def on_pre_compress(self, messages: list[Any]) -> str:
"""Memory -> compressor feedback (future memory-driven summary
enrichment). Returns text to inject into the compression prompt
(default: none)."""
return ""
def on_turn_start(self, turn_number: int, message: Any, **kwargs: Any) -> None:
"""Turn-start nudge (future background review). Default: no-op."""
return None
# ── Async (speculative) ──────────────────────────────────────────────
# Interface placeholders so a future async LLM client can override without
# changing the contract. Defaults delegate to the sync methods (no
# concurrency benefit -- the real LLM call stays sync); callers today use
# the sync path.
async def aadd(
self,
thread_id: str,
messages: list[Any],
*,
agent_name: str | None = None,
user_id: str | None = None,
trace_id: str | None = None,
) -> None:
return self.add(thread_id, messages, agent_name=agent_name, user_id=user_id, trace_id=trace_id)
async def aget_context(
self,
user_id: str | None,
*,
agent_name: str | None = None,
thread_id: str | None = None,
) -> str:
return self.get_context(user_id, agent_name=agent_name, thread_id=thread_id)
async def asearch(
self,
query: str,
top_k: int = 5,
*,
user_id: str | None = None,
agent_name: str | None = None,
category: str | None = None,
) -> list[dict[str, Any]]:
return self.search(query, top_k, user_id=user_id, agent_name=agent_name, category=category)
# ── Construction ─────────────────────────────────────────────────────
@classmethod
@abstractmethod
def from_config(
cls,
backend_config: dict[str, Any],
*,
mode: Literal["middleware", "tool"] = "middleware",
**host_hooks: Any,
) -> MemoryManager:
"""Build a fully-wired instance from backend config + host-provided hooks.
The factory calls this instead of constructing the class directly, so
each backend owns its own assembly (parse config, wire dependencies,
consume the host hooks it needs). Adding a backend = implement
``from_config``; the factory stays unchanged. ``host_hooks`` carries
host-provided callables/values (tracing, hidden-message filter,
trace-context manager, a host-llm factory); a backend that needs none
of them (e.g. noop) ignores them and returns
``cls(backend_config=backend_config, mode=mode)``.
"""
def close(self) -> None:
"""Release backend resources during graceful process shutdown.
Backends that own external resources may override this hook. The
default is intentionally a no-op for lightweight or third-party
implementations.
"""
return None
# ── Backend discovery (drop-in) ───────────────────────────────────────────
def _scan_backends() -> dict[str, type[MemoryManager]]:
"""Discover pluggable backends under ``backends/<name>/``.
Each subpackage that exposes a ``MANAGER_CLASS`` attribute (a
:class:`MemoryManager` subclass) is registered under its folder name.
Results are cached for the process. Folder name == backend name ==
``manager_class`` config value (drop-in contract). A backend that fails
to import is logged and skipped so a broken optional backend never breaks
the factory.
"""
global _backends_cache
if _backends_cache is not None:
return _backends_cache
registry: dict[str, type[MemoryManager]] = {}
if not _BACKENDS_DIR.is_dir():
_backends_cache = registry
return registry
for entry in sorted(_BACKENDS_DIR.iterdir()):
if not entry.is_dir() or entry.name.startswith(("_", ".")):
continue
if not (entry / "__init__.py").is_file():
continue
dotted = f"deerflow.agents.memory.backends.{entry.name}"
try:
module: ModuleType = importlib.import_module(dotted)
except Exception: # noqa: BLE001 - a broken backend must not break the factory
logger.exception("Failed to import memory backend %r; skipping", entry.name)
continue
cls = getattr(module, _MANAGER_CLASS_ATTR, None)
if cls is None:
continue
if not (isinstance(cls, type) and issubclass(cls, MemoryManager)):
logger.warning(
"Memory backend %r exposes MANAGER_CLASS=%r which is not a MemoryManager subclass; skipping",
entry.name,
cls,
)
continue
registry[entry.name] = cls
_backends_cache = registry
return registry
def _resolve_manager_class(manager_class: str) -> type[MemoryManager]:
"""Resolve a ``manager_class`` config value to a concrete class.
Resolution order:
1. Registered short name (from :func:`_scan_backends`).
2. Dotted import path (``pkg.mod:Cls`` or ``pkg.mod.Cls``).
A value that resolves to neither is a config error: raise rather than
silently fall back to a different storage backend. Memory is persistent
state, so silently substituting DeerMem when an explicit ``manager_class``
fails to resolve (typo / import error / missing attr) would route writes to
the wrong store -- a silent data-integrity footgun. Fail loud (the manager
is resolved eagerly at startup so it can be warmed) so the operator fixes
``memory.manager_class`` instead of discovering the mismatch later.
"""
registry = _scan_backends()
if manager_class in registry:
return registry[manager_class]
# Treat as a dotted path: support both "pkg.mod:Cls" and "pkg.mod.Cls".
dotted_error: str | None = None
if ":" in manager_class:
module_path, _, attr = manager_class.partition(":")
else:
module_path, _, attr = manager_class.rpartition(".")
if module_path and attr:
try:
module = importlib.import_module(module_path)
except ImportError as e:
dotted_error = f"cannot import module {module_path!r}: {e}"
else:
cls = getattr(module, attr, None)
if cls is None:
dotted_error = f"attribute {attr!r} not found in {module_path!r}"
elif not (isinstance(cls, type) and issubclass(cls, MemoryManager)):
dotted_error = f"{manager_class!r} resolved to non-MemoryManager {cls!r}"
else:
return cls
raise ValueError(
f"memory.manager_class={manager_class!r} is not a registered backend name "
f"(known: {sorted(registry)}) nor a resolvable 'pkg.mod:Cls' path" + (f": {dotted_error}" if dotted_error else "") + ". Fix memory.manager_class in config; refusing to silently fall back to a "
"different storage backend (memory is persistent state -- a wrong store is a "
"silent data-integrity footgun)."
)
def backend_requires_passive_writes_in_tool_mode(manager_class: str) -> bool:
"""Return whether a backend needs middleware writes in tool mode.
Resolve the class without constructing it so agent assembly does not run
backend startup checks or perform network I/O.
"""
return _resolve_manager_class(manager_class).requires_passive_writes_in_tool_mode
# ── Host-default hook providers (passed to from_config by the factory) ────
#
# These callables are the host's defaults for the slots a backend may consume
# (tracing, hidden-message filtering, trace-context binding, a host default
# LLM). The portable backend package never names a deer-flow concept; the host
# supplies them HERE (host code outside ``backends/deermem/``). The factory
# passes them to ``cls.from_config(..., **host_hooks)``; each backend's
# ``from_config`` consumes the ones it needs (DeerMem does; noop ignores them).
# An explicit value in ``backend_config`` (set programmatically) takes
# precedence and is left untouched (from_config's merge skips present keys).
#
# Imports are lazy (matching the ``runtime_home`` precedent) so this module
# stays cheap to import and so another agent vendoring the contract only has
# to edit these helpers, not the top-level imports.
class LangfuseMemoryCallbacks(MemoryCallbacks):
"""Host default callbacks: emit langfuse spans at the memory-LLM boundary.
Implements ``on_memory_llm_call`` (pre-LLM-call) by merging langfuse trace
metadata into ``invoke_config`` -- identical to the former
``_host_default_tracing_callback`` (same signature, same timing, same
mutation), repackaged as a callbacks method so the langfuse binding lives in
host code and the portable backend package never names langfuse. No-op when
langfuse is not an enabled tracing provider.
"""
def __init__(self, *, extensions=None) -> None:
if extensions is None:
from deerflow.extensions import get_loaded_extensions
extensions = get_loaded_extensions()
self._extensions = extensions
def on_memory_llm_call(
self,
invoke_config: dict[str, Any],
*,
thread_id: str | None,
user_id: str | None,
trace_id: str | None,
model_name: str | None,
) -> None:
from deerflow.tracing import inject_langfuse_metadata
inject_langfuse_metadata(
invoke_config,
thread_id=thread_id,
user_id=user_id,
assistant_id="memory_agent",
model_name=model_name,
environment=os.environ.get("DEER_FLOW_ENV") or os.environ.get("ENVIRONMENT"),
deerflow_trace_id=trace_id,
)
def on_memory_llm_result(
self,
invoke_config: dict[str, Any],
*,
prompt: Any,
response: Any,
error: BaseException | None,
duration_ms: float,
model_name: str | None,
) -> None:
"""Forward a DeerMem provider result using the captured snapshot."""
extensions = self._extensions
if not extensions.has_system_model_observers:
return
try:
from deerflow_extension_api import (
SystemModelRequest,
SystemModelResult,
SystemOperationKind,
)
from deerflow.extensions.notify import (
dispatch_system_model_observation,
notify_system_model_call,
task_store_for_system_call,
)
dispatch_system_model_observation(
notify_system_model_call(
extensions,
task_store_for_system_call(invoke_config),
SystemOperationKind.MEMORY,
SystemModelRequest(
messages=prompt,
model_name=model_name,
invoke_config=invoke_config,
),
SystemModelResult(
response=response,
error=error,
duration_ms=duration_ms,
),
),
SystemOperationKind.MEMORY.value,
)
except Exception:
# Only the bridge's own failures are non-fatal. A teardown signal
# must propagate, matching the boundary the DeerMem-side call
# site documents and tests.
logger.warning(
"Extension observation of the memory model call failed (non-fatal)",
exc_info=True,
)
def _host_default_should_keep_hidden_message(additional_kwargs: Any) -> bool:
"""deer-flow default for DeerMem's ``should_keep_hidden_message`` slot.
Keep a ``hide_from_ui`` message only when it carries a human-input
clarification response, so the user's clarification is captured into
memory; drop all other hidden messages (framework-internal reminders,
view-image payloads, etc.). Restores the pre-abstraction behaviour where
``message_processing`` imported ``read_human_input_response`` directly.
"""
from deerflow.agents.human_input import read_human_input_response
return read_human_input_response(additional_kwargs) is not None
def _host_default_llm() -> Any:
"""deer-flow default for DeerMem's ``host_llm`` slot (zero-config extraction).
Builds the host's default chat model (``create_chat_model(name=None)`` ->
app default, ``attach_tracing=True`` so memory LLM calls surface in langfuse
via the callbacks' ``on_memory_llm_call`` metadata merge), mirroring pre-abstraction
``model_name: null``. Returns ``None`` if no model is available (no models
configured) so DeerMem no-ops extraction with a clear error rather than
crashing startup.
"""
try:
from deerflow.models import create_chat_model
return create_chat_model(name=None)
except Exception: # noqa: BLE001 - no default model is a config state, not a crash
logger.warning("Could not build host default model for DeerMem memory extraction; memory extraction will be disabled", exc_info=True)
return None
def _host_default_extraction_callback(payload: Any) -> None:
"""deer-flow default for DeerMem's ``extraction_callback`` slot.
Logs post-extraction metrics (token usage, facts passing/rejected by the
confidence filter, gate rejection rate) for ops observability, and flags a
high rejection rate
(>60%) so a prompt/threshold regression is visible without inspecting every
trace. A Langfuse-aware callback can replace this to emit a dedicated
extraction span; the metrics keys are stable for that handoff. Exceptions
are never raised (the DeerMem side already wraps the call).
"""
if not isinstance(payload, dict):
return
extracted = payload.get("facts_extracted")
passed_confidence = payload.get("facts_passed_confidence")
rejected = payload.get("rejected_low_confidence", 0)
rejected_by_scope = payload.get("rejected_by_scope_gate", 0)
scope_breakdown = payload.get("scope_gate_rejections")
thread_id = payload.get("thread_id")
model_name = payload.get("model_name")
if isinstance(extracted, int) and isinstance(passed_confidence, int) and extracted > 0:
rejection_rate = (extracted - passed_confidence) / extracted
logger.info(
"Memory extraction metrics: thread=%s model=%s extracted=%d passed_confidence=%d rejected=%d rejection_rate=%.2f",
thread_id,
model_name,
extracted,
passed_confidence,
rejected,
rejection_rate,
)
if rejection_rate > 0.6:
logger.warning(
"Memory extraction rejection rate %.0f%% exceeds 60%% - review extraction prompt / confidence threshold (thread=%s)",
rejection_rate * 100,
thread_id,
)
else:
logger.info(
"Memory extraction metrics: thread=%s model=%s success=%s token_usage=%s",
thread_id,
model_name,
payload.get("success"),
payload.get("token_usage"),
)
if isinstance(scope_breakdown, dict):
logger.info(
"Memory scope-gate metrics: thread=%s model=%s rejected=%s breakdown=%s",
thread_id,
model_name,
rejected_by_scope,
scope_breakdown,
)
fact_breakdown = scope_breakdown.get("facts")
fact_scope_rejected = sum(value for value in fact_breakdown.values() if isinstance(value, int)) if isinstance(fact_breakdown, dict) else 0
if isinstance(extracted, int) and extracted > 0 and fact_scope_rejected / extracted > 0.6:
logger.warning(
"Memory fact scope-gate rejection rate %.0f%% exceeds 60%% - review extraction model classification / prompt (thread=%s)",
fact_scope_rejected / extracted * 100,
thread_id,
)
def _collect_host_hooks() -> dict[str, Any]:
"""Provide host hook callables for backends to consume in ``from_config``.
The factory is a hook *provider*; each backend's ``from_config`` is the
*consumer* that decides which hooks to use (so adding a backend does not
change this factory). ``host_llm`` is provided as a factory callable
(``host_llm_factory``) rather than a built instance so a backend only
builds the host default model when it actually needs one (i.e. has no model
of its own) -- building an unused default on every startup would waste
time. The others are direct values (cheap function refs).
"""
from deerflow.trace_context import ensure_trace_context
return {
"callbacks": LangfuseMemoryCallbacks(),
"should_keep_hidden_message": _host_default_should_keep_hidden_message,
"trace_context_manager": ensure_trace_context,
"host_llm_factory": _host_default_llm,
"extraction_callback": _host_default_extraction_callback,
}
# ── Singleton factory ─────────────────────────────────────────────────────
def get_memory_manager() -> MemoryManager:
"""Return the singleton :class:`MemoryManager` for the active config.
Reads ``MemoryConfig.manager_class`` and resolves it via
:func:`_resolve_manager_class`. The instance is cached; call
:func:`reset_memory_manager` to force re-resolution (tests / runtime
backend switching).
"""
global _memory_manager
if _memory_manager is not None:
return _memory_manager
# deer-flow is multi-threaded: memory injection runs via asyncio.to_thread,
# the update queue fires on a Timer thread, and gateway/agent threads all
# reach here. Double-checked locking ensures only one instance is built even
# on first-call contention -- essential since backends now own stateful
# dependencies (DeerMem owns its storage/queue/updater; others may open
# connections) constructed here in __init__.
with _manager_lock:
if _memory_manager is not None:
return _memory_manager
cfg = get_memory_config()
manager_class = cfg.manager_class
cls = _resolve_manager_class(manager_class)
backend_config = dict(cfg.backend_config or {})
# Zero-config UX: default DeerMem storage to deer-flow's state dir
# (absolute, CWD-independent) so memory lands at
# {runtime_home}/users/{user_id}/memory.json (deer-flow's base_dir,
# same as pre-abstraction) unless the host explicitly sets storage_path.
if not backend_config.get("storage_path"):
from deerflow.config.runtime_paths import runtime_home
backend_config["storage_path"] = str(runtime_home())
elif not Path(backend_config.get("storage_path", "")).is_absolute():
# A relative storage_path is resolved against runtime_home() (base_dir-
# relative, CWD-independent) to preserve pre-abstraction semantics; left
# as-is it would be CWD-relative and fragile. (Resolved here in host code
# so the portable paths.py stays free of any runtime_home dependency.)
from deerflow.config.runtime_paths import runtime_home
backend_config["storage_path"] = str((Path(runtime_home()) / backend_config["storage_path"]).resolve())
# storage_path-is-a-file guard lives on DeerMemConfig.model_validator
# now (DeerMem-private semantics; fires even when the factory bypassed).
# Host hook providers: the factory supplies these as kwargs; each
# backend's from_config decides which to consume (the factory stays
# backend-agnostic -- it no longer knows which hooks a backend needs).
# An explicit value in backend_config still wins (from_config's merge
# skips keys already present).
host_hooks = _collect_host_hooks()
# ``mode`` mirrors host MemoryConfig.mode so the invariant validator
# (mode=="tool" requires search) can fire on the factory path too.
_memory_manager = cls.from_config(backend_config, mode=cfg.mode, **host_hooks)
logger.info("Memory manager resolved: %s (manager_class=%r)", cls.__name__, manager_class)
return _memory_manager
def reset_memory_manager() -> None:
"""Clear the cached singleton manager and the backend registry.
The next :func:`get_memory_manager` call re-reads the config and re-scans
backends. Use this in tests or when switching backends at runtime.
"""
global _memory_manager, _backends_cache
with _manager_lock:
_memory_manager = None
_backends_cache = None