Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 85 additions & 23 deletions python/packages/redis/agent_framework_redis/_context_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
_SCOPE_SCHEMA_VERSION = 1
_SCOPE_FIELDS = ("application_id", "agent_id", "user_id", "thread_id", "conversation_id")
_SCOPE_TAG_SEPARATOR = "\x1f"
_KEY_HASH_VERSION = 1


def _document_key_suffix(*, scope: dict[str, str | None], role: str, content: str) -> str:
"""Stable hash of (scope, role, content) used as the document key suffix."""
payload = json.dumps(
[_KEY_HASH_VERSION, [scope.get(name) or "" for name in _SCOPE_FIELDS], role, content],
ensure_ascii=False,
separators=(",", ":"),
).encode("utf-8")
return hashlib.sha256(payload).hexdigest()


_TAG_DEFAULTS = {"separator": ",", "case_sensitive": False, "withsuffixtrie": False}
_TEXT_DEFAULTS = {"weight": 1.0, "no_stem": False}
Expand Down Expand Up @@ -341,29 +353,72 @@ async def _existing_index(self) -> Any:
redis_url=None if existing_client is not None else self.redis_url,
)

def _scope_values(self, session_id: str | None) -> dict[str, str | None]:
"""Scope fields stored on, and used to key and filter, every memory document."""
return {
"application_id": self.application_id,
"agent_id": self.agent_id,
"user_id": self.user_id,
"thread_id": session_id,
"conversation_id": session_id,
}

def _document_redis_key(self, *, role: str, content: str, session_id: str | None) -> str:
"""Full Redis key for a memory document, built from the real index prefix."""
suffix = _document_key_suffix(scope=self._scope_values(session_id), role=role, content=content)
return self.redis_index.key(suffix)

async def _existing_keys(self, keys: list[str]) -> set[str]:
"""Return the subset of `keys` that already exist in Redis."""
if not keys:
return set()
async with self.redis_index.client.pipeline(transaction=False) as pipe:
for key in keys:
pipe.exists(key)
results = await pipe.execute()
return {key for key, count in zip(keys, results, strict=True) if count}
Comment thread
PratikWayase marked this conversation as resolved.
Resolved

async def _add(
self,
*,
data: dict[str, Any] | list[dict[str, Any]],
session_id: str | None = None,
metadata: dict[str, Any] | None = None,
) -> None:
"""Inserts one or many documents with partition fields populated."""
"""Inserts one or many documents with partition fields populated.

Documents are deduplicated by their scope fields, role, and content.
When the AG-UI client resends the whole conversation on every run,
already-stored messages are detected via a pipelined EXISTS check
and skipped, so they are not re-embedded or re-stored.

Tradeoffs:
- The same text from the same role in one conversation is stored
once. Legitimate repeats ("yes", "thanks") collapse to one entry.
- Documents saved before this fix keep their old random keys, so an
existing conversation gets one extra copy of each message on the
first post-fix run and then stays stable.
- Content is normalized with .strip() before hashing, so whitespace
differences between the stored model text and the replayed AG-UI
text do not produce a second copy.

Concurrency: the EXISTS check and the subsequent load are not atomic.
Two concurrent _add calls for the same message can both observe the
key absent, both embed, and both write. Because keys are
deterministic, the second write overwrites the first at the same
key, so the final state is always one document per (scope, role,
content). The cost of the race is at most one redundant embedding
call per concurrent duplicate; this is accepted over the complexity
of a SET NX reservation with crash recovery.
"""
self._validate_filters()
await self._ensure_index()
docs = data if isinstance(data, list) else [data]

prepared: list[dict[str, Any]] = []
for doc in docs:
d = dict(doc)
scope_values = {
"application_id": self.application_id,
"agent_id": self.agent_id,
"user_id": self.user_id,
"thread_id": session_id,
"conversation_id": session_id,
}
for name, value in scope_values.items():
for name, value in self._scope_values(session_id).items():
d[name] = value or None
d[_scope_token_field(name)] = _scope_token(name, value) if value else None
if "content" not in d:
Expand All @@ -372,23 +427,36 @@ async def _add(
d.setdefault(self.vector_field_name, None)
prepared.append(d)

docs_by_key: dict[str, dict[str, Any]] = {}
for d in prepared:
content = (d.get("content") or "").strip()
if not content:
continue
key = self._document_redis_key(role=d.get("role") or "", content=content, session_id=session_id)
docs_by_key.setdefault(key, d)

existing = await self._existing_keys(list(docs_by_key))
Comment thread
PratikWayase marked this conversation as resolved.
Resolved
new_docs = {k: d for k, d in docs_by_key.items() if k not in existing}
if not new_docs:
return
keys_to_load = list(new_docs)
prepared_to_load = list(new_docs.values())

if self.redis_vectorizer and self.vector_field_name:
text_list = [d["content"] for d in prepared]
text_list = [d["content"] for d in prepared_to_load]
embeddings = await self.redis_vectorizer.aembed_many( # pyright: ignore[reportUnknownMemberType]
text_list, batch_size=len(text_list)
)
for i, d in enumerate(prepared):
for i, d in enumerate(prepared_to_load):
# aembed_many returns lists unless as_buffer is enabled.
vec = array_to_buffer(
d[self.vector_field_name] = array_to_buffer(
embeddings[i], # pyright: ignore[reportArgumentType]
dtype=self.redis_vectorizer.dtype,
)
field_name: str = self.vector_field_name
d[field_name] = vec

# Redis hashes cannot encode None, including with non-validating borrowed indexes.
prepared = [{key: value for key, value in doc.items() if value is not None} for doc in prepared]
await self.redis_index.load(prepared)
prepared_to_load = [{k: v for k, v in d.items() if v is not None} for d in prepared_to_load]
await self.redis_index.load(prepared_to_load, keys=keys_to_load)

async def _redis_search(
self,
Expand All @@ -410,13 +478,7 @@ async def _redis_search(
raise IntegrationInvalidRequestException("text_search() requires non-empty text")
num_results = max(int(num_results or 10), 1)

combined_filter = self._build_filter_from_dict({
"application_id": self.application_id,
"agent_id": self.agent_id,
"user_id": self.user_id,
"thread_id": session_id,
"conversation_id": session_id,
})
combined_filter = self._build_filter_from_dict(self._scope_values(session_id))
if filter_expression is not None:
combined_filter = (combined_filter & filter_expression) if combined_filter else filter_expression

Expand Down
17 changes: 17 additions & 0 deletions python/packages/redis/tests/test_context_provider_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,28 @@
)


def _make_pipeline_mock(**_: Any) -> MagicMock:
"""Fresh pipeline mock per client.pipeline() call.

A shared mock accumulates exists.call_count across _add calls, so a
second _add with nonempty candidates returns too many EXISTS results
and _existing_keys raises at its strict zip.
"""
pipe = MagicMock()
pipe.exists = MagicMock()
pipe.execute = AsyncMock(side_effect=lambda: [0] * pipe.exists.call_count)
pipe.__aenter__ = AsyncMock(return_value=pipe)
pipe.__aexit__ = AsyncMock(return_value=None)
return pipe


@pytest.fixture
def mock_index() -> AsyncMock:
index = AsyncMock()
client = MagicMock()
client.pipeline = MagicMock(side_effect=_make_pipeline_mock)
index.client = client
index.key = MagicMock(side_effect=lambda suffix: f"context:{suffix}")
index.create = AsyncMock()
index.exists = AsyncMock(return_value=False)
index.load = AsyncMock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,73 @@ async def test_live_legacy_index_fails_closed_until_recreated(
assert await _provider(url, index_name, prefix, user_id="alice")._redis_search("re-ingested marker") == []
adjacent_record = await cast(Awaitable[dict[bytes, bytes]], client.hgetall(adjacent_key))
assert adjacent_record[b"content"] == b"foreign"


@pytest.mark.integration
async def test_live_deduplicates_replayed_conversation_messages(
live_redis: tuple[str, Redis, str, str],
) -> None:
"""Re-sending the whole conversation does not duplicate documents.

The AG-UI client sends all earlier messages on every run. Without
deduplication, a 15-fact conversation produces 240 documents and search
results are dominated by copies of early messages. With the fix, the
same conversation produces exactly one document per message and search
returns distinct results.
"""
url, client, index_name, prefix = live_redis
provider = _provider(url, index_name, prefix, user_id="user-1")

facts = [
"Our release day is Thursday.",
"We deploy to westus3.",
"Rollbacks need approval from Mira.",
"The on-call engineer this week is Priya.",
"Our database is Postgres 16.",
]

session_id = "repro-session"
sent_messages: list[dict[str, str]] = []
for fact in facts:
sent_messages.append({"role": "user", "content": fact})
await provider._add(data=list(sent_messages), session_id=session_id)

encoded_prefix = prefix.encode()
keys = [
key
async for key in client.scan_iter(_type="hash")
if (key if isinstance(key, bytes) else str(key).encode()).startswith(encoded_prefix)
]
assert len(keys) == 5

stored_contents = []
for key in keys:
key_str = key.decode() if isinstance(key, bytes) else str(key)
record = await cast(Awaitable[dict[bytes, bytes]], client.hgetall(key_str))
stored_contents.append(record[b"content"].decode())
assert stored_contents.count("We deploy to westus3.") == 1

results = await provider._redis_search("deploy", session_id=session_id, num_results=10)
assert len(results) >= 1
assert results[0]["content"] == "We deploy to westus3."


@pytest.mark.integration
async def test_live_dedup_key_is_stable_across_runs(
live_redis: tuple[str, Redis, str, str],
) -> None:
"""Storing the same message twice maps to the same Redis key."""
url, client, index_name, prefix = live_redis
provider = _provider(url, index_name, prefix, user_id="user-1")

session_id = "stable-key-session"
await provider._add(data={"role": "user", "content": "Remember tea"}, session_id=session_id)
await provider._add(data={"role": "user", "content": "Remember tea"}, session_id=session_id)

encoded_prefix = prefix.encode()
keys = [
key
async for key in client.scan_iter(_type="hash")
if (key if isinstance(key, bytes) else str(key).encode()).startswith(encoded_prefix)
]
assert len(keys) == 1
Loading
Loading