Python: fix(redis): scope RedisHistoryProvider keys by source_id - #7845
Python: fix(redis): scope RedisHistoryProvider keys by source_id#7845Yufeng He (he-yufeng) wants to merge 11 commits into
Conversation
Two providers with different source_ids but the same key_prefix shared one Redis list per session, so a write-only audit sink contaminated the primary provider's loaded history, and clear() on one deleted the other's conversation. The key now includes source_id, matching the Cosmos provider's scoping. Existing keys written under the old layout are left in place; deleting them would risk removing a sibling provider's data, and they simply become unreadable by the new code.
…t in tests Colon-joined keys were ambiguous for source ids or session ids containing a colon (a:b + c vs a + b:c). Join with the ASCII unit separator instead. The clear-isolation test now asserts on the other provider's key too, so the unused variable lint is gone as well.
…rovider-source-id-key
The lazy renamenx migration made the first reader claim a shared legacy list, forked history during rolling upgrades, swallowed every Redis error around the move, and let one source's clear() delete another source's pre-upgrade data. Reads now merge the legacy list in place (legacy entries are older by construction, since writes only ever land on the scoped key), writes stay scoped-only, and clear() deletes only the scoped key. Removing a legacy key is an explicit admin call.
There was a problem hiding this comment.
Pull request overview
Scopes Redis history keys by source_id while preserving access to legacy session data.
Changes:
- Adds collision-safe, length-prefixed scoped keys.
- Merges legacy and scoped histories.
- Updates isolation, compatibility, trimming, and clearing tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/redis/agent_framework_redis/_history_provider.py |
Implements scoped keys and legacy reads. |
python/packages/redis/tests/test_providers.py |
Tests key isolation and compatibility behavior. |
Suppressed comments (3)
python/packages/redis/agent_framework_redis/_history_provider.py:225
- Because
get_messages()always merges the legacy list, deleting only the scoped key does not fulfillclear()'s contract: a subsequent read immediately returns the legacy messages again. This also conflicts with the constructor documentation that directs callers toclear()to remove stored history. Keep the shared legacy list intact, but add source-scoped state (for example, a cutoff/tombstone) that makes this provider stop reading legacy data after it is cleared.
Only the scoped key is deleted. A pre-scoping legacy list belongs to
whichever sources shared it, so it is left for an explicit admin
cleanup rather than being removed by one source's clear().
python/packages/redis/agent_framework_redis/_history_provider.py:159
- Merging both complete lists bypasses
max_messages. For example, after upgrading a provider configured withmax_messages=10, ten retained legacy entries plus ten scoped entries are all returned and retained as a 20-message session, even though the public option promises a per-session maximum. Apply the configured limit to the merged view (while preserving the documented zero behavior), or define a migration policy that retires legacy entries without modifying another source's data.
legacy_messages: list[str] = await self._redis_client.lrange(legacy_key, 0, -1) # type: ignore[misc]
if legacy_messages:
redis_messages = [*legacy_messages, *redis_messages]
python/packages/redis/agent_framework_redis/_history_provider.py:157
- This adds a second sequential Redis round trip to every history read, including fresh deployments where the legacy key never existed, and the cost remains indefinitely because there is no migration cutoff. Fetch both lists in one non-transactional pipeline or make legacy fallback an explicit/temporary compatibility mode so normal scoped reads retain their previous latency.
legacy_messages: list[str] = await self._redis_client.lrange(legacy_key, 0, -1) # type: ignore[misc]
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
Rebased onto current main in 5509aad. The merge combined upstream's |
…nto fix/redis-history-provider-source-id-key
…nto fix/redis-history-provider-source-id-key # Conflicts: # python/uv.lock
…nto fix/redis-history-provider-source-id-key
Motivation & Context
Two
RedisHistoryProviderinstances with differentsource_ids that share a session id used to read and write the same Redis list, so one provider's history leaked into another's model context and either side'sclear()wiped both. This scopes the Redis key bysource_idand keeps pre-scoping data readable.Successor of #7494: moonbox3 closed that one until every review thread was addressed, and GitHub will not reopen it now that the branch has moved, so this continues from there. All four threads are answered and resolved on #7494, and the rework they prompted is in here.
Description & Review Guide
clear()deletes only the scoped key. Compared to Python: fix(redis): scope RedisHistoryProvider keys by source_id #7494 the lazyrenamenxmigration is gone: nothing moves on read, so a shared legacy list is never claimed by one reader, rolling upgrades cannot fork history, no Redis error is swallowed around a move, and one source'sclear()cannot erase another source's pre-upgrade history.Related Issue
Fixes #7471
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.