fix(memory): truncate oversized mem0 sync messages at the source

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>
This commit is contained in:
liuhao1024
2026-09-09 11:29:35 +08:00
committed by Teknium
parent 2b9b3b829f
commit 040742d06b
2 changed files with 63 additions and 1 deletions
+28 -1
View File
@@ -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:
+35
View File
@@ -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.