From 040742d06bab282291d054e26fe9bd6fd5e8d9a2 Mon Sep 17 00:00:00 2001 From: liuhao1024 Date: Wed, 9 Sep 2026 11:29:35 +0800 Subject: [PATCH] fix(memory): truncate oversized mem0 sync messages at the source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sync_turn() sent the whole turn to backend.add() untruncated. OSS embedding models with small context windows (Ollama bge-small-zh-v1.5: 512 tokens) reject the request with HTTP 500, and hosted APIs answer INPUT_TOKEN_LIMIT_EXCEEDED — in both cases _try() only logs, silently dropping the turn's memory extraction after long conversations. Cap each synced message at its last sentence boundary within 450 chars before ingestion: short turns pass through unchanged, long turns keep a coherent statement for fact extraction. This replaces the previous retry-on-error approach, which could not match Ollama's HTTP 500 shape. Salvaged from #37427 with the test suite trimmed to the one invariant (oversized turn still reaches a small-context backend, short text untouched, no breaker failure). Refs #37421 #106235 Co-authored-by: szicely <140148567+szicely@users.noreply.github.com> --- plugins/memory/mem0/__init__.py | 29 ++++++++++++++++++++++- tests/plugins/memory/test_mem0_v3.py | 35 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/plugins/memory/mem0/__init__.py b/plugins/memory/mem0/__init__.py index f45773ec5e..4464a2917b 100644 --- a/plugins/memory/mem0/__init__.py +++ b/plugins/memory/mem0/__init__.py @@ -33,6 +33,30 @@ _CLIENT_ERROR_TYPES = ("MemoryNotFoundError", "ValidationError") # so legacy mem0.json files written by the wizard don't override gateway-native ids. _DEFAULT_USER_ID = "hermes-user" +# sync_turn sends the whole turn to the backend for fact extraction. OSS embedding +# models often have small context windows (bge-small-zh-v1.5: 512 tokens ≈ 500 chars; +# jina-embeddings-v3: 8192), and oversized turns make backend.add() raise — Ollama +# answers HTTP 500, hosted APIs return INPUT_TOKEN_LIMIT_EXCEEDED — which _try only +# logs, silently dropping the turn's memory extraction. Cap each message up front. +_SYNC_MSG_MAX_CHARS = 450 + + +def _truncate_for_sync(text: str, max_len: int = _SYNC_MSG_MAX_CHARS) -> str: + """Cap a synced message at its last sentence boundary within ``max_len``. + + Short messages pass through unchanged; long ones keep the last complete + sentence inside the window so fact extraction still sees coherent statements, + with a hard cut as fallback when no boundary exists (or one only appears in + the first third of the window, which usually means unsegmented input). + """ + if len(text) <= max_len: + return text + for sep in ("。", "!", "?", ".\n", ".", "!", "?"): + cut = text[:max_len].rfind(sep) + if cut > max_len // 3: + return text[:cut + 1] + return text[:max_len] + def _is_client_error(exc: Exception) -> bool: """True for user-caused errors (bad ID, not found) that should NOT trip circuit breaker.""" @@ -266,7 +290,10 @@ class Mem0MemoryProvider(MemoryProvider): def _sync(): if self._backend is not None: - messages = [{"role": "user", "content": user_content}, {"role": "assistant", "content": assistant_content}] + messages = [ + {"role": "user", "content": _truncate_for_sync(user_content)}, + {"role": "assistant", "content": _truncate_for_sync(assistant_content)}, + ] self._try(lambda: self._add(messages, infer=True), logger.warning, "Mem0 sync failed: %s") with self._sync_lock: diff --git a/tests/plugins/memory/test_mem0_v3.py b/tests/plugins/memory/test_mem0_v3.py index 48d66d480b..bb72d87615 100644 --- a/tests/plugins/memory/test_mem0_v3.py +++ b/tests/plugins/memory/test_mem0_v3.py @@ -146,6 +146,41 @@ class TestMem0V3Internal: assert call[2]["infer"] is True +class TestSyncTurnTruncation: + """sync_turn must cap messages before ingestion so small-context embedding + backends (OSS Ollama bge-small-zh-v1.5: 512 tokens; jina-embeddings-v3 token + limits) don't fail the whole extraction — a failure _try only logs.""" + + def _make_provider(self, monkeypatch, backend): + provider = Mem0MemoryProvider() + provider.initialize("test-session") + provider._user_id = "u123" + provider._agent_id = "hermes" + provider._backend = backend + return provider + + def test_small_context_backend_never_sees_oversized_input(self, monkeypatch): + """Regression for #106235/#37421: an OSS embedding backend with a small + context window raises on oversized input; truncation up front keeps the + extraction from being silently dropped (no breaker failures).""" + + class SmallContextBackend(FakeBackend): + def add(self, messages, **kwargs): + if any(len(m["content"]) > mem0_plugin._SYNC_MSG_MAX_CHARS for m in messages): + raise RuntimeError("HTTP 500: embedding input exceeds model context") + return super().add(messages, **kwargs) + + backend = SmallContextBackend() + provider = self._make_provider(monkeypatch, backend) + provider.sync_turn("Short question?", "".join(f"Fact {i}. " for i in range(200)), session_id="s1") + provider._sync_thread.join(timeout=2) + assert len(backend.captured) == 1 + sent = backend.captured[0][1] + assert sent[0]["content"] == "Short question?" # under the cap: untouched + assert len(sent[1]["content"]) <= mem0_plugin._SYNC_MSG_MAX_CHARS and sent[1]["content"].endswith(".") + assert provider._consecutive_failures == 0 + + class TestMem0Prefetch: """prefetch() must recall on the CURRENT question, synchronously.