Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
refactor(memory): deprecate the orphaned Mem0SystemAdapter (#4114)
Tejas Chopra7387b8cc11ec
diff --git a/headroom/memory/backends/__init__.py b/headroom/memory/backends/__init__.pyindex e99dd6e56..dc46ae683 100644--- a/headroom/memory/backends/__init__.py+++ b/headroom/memory/backends/__init__.py@@ -3,7 +3,8 @@This module provides backend adapters for the memory system:- LocalBackend: Fully local using SQLite + HNSW + InMemoryGraph (default)- Mem0Backend: Graph + vector memory via Mem0 (Neo4j + Qdrant)-- Mem0SystemAdapter: Adapter to use Mem0Backend with MemorySystem tools+- Mem0SystemAdapter: Adapter to use Mem0Backend with MemorySystem tools (deprecated;+ use DirectMem0Adapter for local mode; cloud mode has no replacement)- DirectMem0Adapter: Optimized adapter that bypasses Mem0's LLM extractionLocalBackend is always available. Mem0-based backends require optionaldiff --git a/headroom/memory/backends/mem0_system_adapter.py b/headroom/memory/backends/mem0_system_adapter.pyindex 1117419e7..c3c9f3245 100644--- a/headroom/memory/backends/mem0_system_adapter.py+++ b/headroom/memory/backends/mem0_system_adapter.py@@ -2,12 +2,23 @@This adapter bridges the Mem0Backend interface to the MemoryBackend protocolrequired by MemorySystem, enabling Mem0 to be used with the memory tools system.++.. deprecated::+ Nothing in Headroom uses this adapter. For local mode (Qdrant + Neo4j), use+ ``DirectMem0Adapter`` (``headroom.memory.backends.direct_mem0``), which also+ satisfies the MemoryBackend protocol. ``save_memory`` skips Mem0's LLM+ extraction only when the caller passes non-empty pre-extracted ``facts``,+ ``extracted_entities`` or ``extracted_relationships``. Passing only ``content``+ with ``entities``/``relationships`` still runs Mem0's extraction. Cloud mode+ (``Mem0Config(mode="cloud")``) has no replacement in Headroom; use the ``mem0``+ client directly."""from __future__ import annotationsimport asyncioimport uuid+import warningsfrom datetime import datetime, timezonefrom typing import Any@@ -24,6 +35,10 @@ def _utcnow() -> datetime:class Mem0SystemAdapter:"""Adapter that makes Mem0Backend conform to MemorySystem's MemoryBackend protocol.+ .. deprecated::+ Use ``DirectMem0Adapter`` for local mode. Cloud mode has no replacement in+ Headroom; use the ``mem0`` client directly.+This adapter wraps Mem0Backend and provides the interface expected byMemorySystem, enabling LLM-driven memory tools (memory_save, memory_search,memory_update, memory_delete) to work with Mem0's graph and vector capabilities.@@ -53,6 +68,15 @@ class Mem0SystemAdapter:Args:config: Configuration for Mem0. If None, uses default local config."""+ warnings.warn(+ "Mem0SystemAdapter is deprecated and will be removed in a future release; "+ "for local mode use DirectMem0Adapter (headroom.memory.backends.direct_mem0), "+ "which skips Mem0's LLM extraction only when given pre-extracted facts, "+ "extracted_entities or extracted_relationships; "+ "cloud mode has no Headroom replacement, so use the mem0 client directly.",+ DeprecationWarning,+ stacklevel=2,+ )self._backend = Mem0Backend(config)self._config = config or Mem0Config()diff --git a/tests/test_memory/test_mem0_system_adapter_deprecation.py b/tests/test_memory/test_mem0_system_adapter_deprecation.pynew file mode 100644index 000000000..2dc163fa0--- /dev/null+++ b/tests/test_memory/test_mem0_system_adapter_deprecation.py@@ -0,0 +1,60 @@+"""Mem0SystemAdapter is deprecated in favour of DirectMem0Adapter."""++from __future__ import annotations++import pytest++from headroom.memory.backends import mem0_system_adapter+++def test_mem0_system_adapter_warns_deprecated(monkeypatch: pytest.MonkeyPatch) -> None:+ # Mem0Backend's constructor needs no live services, but stub it so the test+ # does not depend on the optional mem0 install.+ monkeypatch.setattr(mem0_system_adapter, "Mem0Backend", lambda config: object())+ with pytest.warns(DeprecationWarning, match="Mem0SystemAdapter is deprecated"):+ mem0_system_adapter.Mem0SystemAdapter()+++_ENTITY = [{"entity": "Netflix", "entity_type": "organization"}]+_RELATION = [{"source": "user", "relationship": "works_at", "destination": "Netflix"}]+++ ("kwargs", "runs_mem0_extraction"),+ [+ ({"facts": ["Works at Netflix"]}, False),+ ({"extracted_entities": _ENTITY}, False),+ ({"extracted_relationships": _RELATION}, False),+ # The legacy names do not select the optimized path.+ ({"entities": ["Netflix"]}, True),+ ({"relationships": _RELATION}, True),+ # Empty pre-extracted inputs do not count either.+ ({"facts": [], "extracted_entities": [], "extracted_relationships": []}, True),+ ],+)+async def test_direct_adapter_skips_mem0_extraction_only_for_pre_extracted_args(+ kwargs: dict, runs_mem0_extraction: bool+) -> None:+ """The deprecation names facts/extracted_entities/extracted_relationships as the+ way to skip Mem0's LLM extraction; entities/relationships alone still run it."""+ from unittest.mock import AsyncMock, MagicMock++ from headroom.memory.backends.direct_mem0 import DirectMem0Adapter, Mem0Config++ adapter = DirectMem0Adapter(Mem0Config(enable_graph=True))+ adapter._ensure_initialized = AsyncMock() # type: ignore[method-assign]+ mem0_client = MagicMock()+ mem0_client.add.return_value = {"results": [{"id": "m1", "memory": "Works at Netflix"}]}+ adapter._mem0_client = mem0_client+ write_facts = AsyncMock(return_value=["f1"])+ write_graph = AsyncMock()+ adapter._write_facts_to_qdrant = write_facts # type: ignore[method-assign]+ adapter._write_graph_to_neo4j = write_graph # type: ignore[method-assign]++ await adapter.save_memory(+ content="I work at Netflix", user_id="u1", importance=0.5, background=False, **kwargs+ )++ assert mem0_client.add.called is runs_mem0_extraction+ assert write_facts.called is not runs_mem0_extraction