-
Notifications
You must be signed in to change notification settings - Fork 421
LLMChat add session consistency using redis #1236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: Keval Mahajan <[email protected]>
…or session management Signed-off-by: Keval Mahajan <[email protected]>
Signed-off-by: Keval Mahajan <[email protected]>
Signed-off-by: Keval Mahajan <[email protected]>
Signed-off-by: Keval Mahajan <[email protected]>
Signed-off-by: Keval Mahajan <[email protected]>
Signed-off-by: Keval Mahajan <[email protected]>
crivetimihai
approved these changes
Oct 14, 2025
Member
crivetimihai
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add documentation for Redis setup requirements
- Consider: Adding integration tests for multi-worker scenarios
- Monitor: Session recreation behavior in production - it may need adjustment
- Consider: Exposing tunables (SESSION_TTL, LOCK_TTL) in config.py instead of just env vars
Have created a separate issue.
Potential Concerns
- Complexity: The get_active_session() function is quite complex with multiple retry logic paths
- Session Recreation Logic: Auto-recreation from config could be surprising behavior
- Lock Timeouts: Default LOCK_TTL=30s and LOCK_RETRIES=10 may need tuning for high-load scenarios
- Testing: No visible unit tests for the complex coordination logic (though CI passes)
- Documentation: No update to .env.example or README for new Redis requirements
Member
Author
Thanks!! |
11 tasks
p4yl04d3r
pushed a commit
to p4yl04d3r/mcp-context-forge
that referenced
this pull request
Nov 19, 2025
* redis Implementation for chat session consistency Signed-off-by: Keval Mahajan <[email protected]> * Improved chat history manager - fix chat history/context with redis for session management Signed-off-by: Keval Mahajan <[email protected]> * linting Signed-off-by: Keval Mahajan <[email protected]> * fix tool name display in tool invocations Signed-off-by: Keval Mahajan <[email protected]> * safe disconnect before reconnection for same user Signed-off-by: Keval Mahajan <[email protected]> * reconnection clears previous chat history Signed-off-by: Keval Mahajan <[email protected]> * web linting Signed-off-by: Keval Mahajan <[email protected]> --------- Signed-off-by: Keval Mahajan <[email protected]> Signed-off-by: p4yl04d3r <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📌 Summary
Previously, the LLM chat system operated with a single Gunicorn worker. With the recent integration of Redis, session management, configuration storage, chat history, and other data are now centralized, enabling support for multiple worker processes and improved scalability.
🔧Detailed Implementation:
Class
ChatHistoryManagerPurpose: Centralized Chat History Manages user chat histories with Redis backend and in-memory fallback.
Storage Modes:
Redis: For distributed, multi-worker environments (shared state).
In-memory (Python dict): For single-worker or Redis-unavailable scenarios.
Initialization Parameters:
redis: Optional Redis async client.
maxmessages: Max messages to store per user (default 50).
ttl: Time-to-live for Redis entries (default 3600 sec).
Key Methods:
gethistory(): Fetches chat history (handles JSON errors).savehistory(): Saves and trims history to max message count.appendmessage(): Adds a message (read-modify-write).clearhistory(): Deletes a user's chat history.getlangchainmessages(): Converts stored dicts to LangChain message objects.Storage Key Format:
Redis keys:
chat:history:{userid}(ensures user isolation).Concurrent User Management:
Session Management Components:
activesessions: Maps userid ➝ MCPChatService (local only).userconfigs: Maps userid ➝ configuration (local only).Redis keys for distributed coordination:
user:config:{userid}: Serialized config.active:session:{userid}: Current session owner (stores WORKER_ID).session:lock:{userid}: Prevents race conditions during init.Worker Coordination:
WORKER_ID: Uniquely identifies worker (from ENV, HOSTNAME, or PID).
Per-User Configuration:
Each user can have:
Built on /llmchat/connect request + env var fallbacks → stored locally + in Redis.
Chat History Isolation:
ChatHistoryManageruses per-user Redis key:chat:history:{userid}Ensures strict user isolation even in shared Redis.
Session Lifecycle:
/connect:Ends existing session → creates new with fresh config.
/disconnect:Cleans up: shuts down service, clears history, removes Redis keys.
Redis ownership TTL ensures recovery from crashes (session becomes available after expiry).
🔁 Reproduction Steps
make serve🧪 Verification
make lintmake testmake coverage📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)