Skip to content

Bound in-memory chat sessions#105

Open
pragnyanramtha wants to merge 3 commits into
INCF:mainfrom
pragnyanramtha:fix-session-memory-eviction-76
Open

Bound in-memory chat sessions#105
pragnyanramtha wants to merge 3 commits into
INCF:mainfrom
pragnyanramtha:fix-session-memory-eviction-76

Conversation

@pragnyanramtha
Copy link
Copy Markdown

@pragnyanramtha pragnyanramtha commented May 14, 2026

Summary

Closes #76.

Adds bounded in-memory session retention for long-running backend processes:

  • tracks chat sessions with LRU ordering
  • evicts inactive sessions after SESSION_TTL_SECONDS, default 3600
  • limits total sessions with SESSION_MAX_COUNT, default 1000
  • keeps the existing per-session chat history cap configurable through SESSION_HISTORY_LIMIT, default 20
  • preserves active in-flight chat sessions during TTL/LRU eviction and reset races
  • documents the memory behavior and environment variables in the README

Verification

  • python3 -m py_compile backend/agents.py backend/main.py tests/test_session_lifecycle.py
  • /tmp/ksa-pr105-venv/bin/python -m pytest -q tests/test_session_lifecycle.py -> 4 passed
  • git diff --check
  • Dependency-isolated session lifecycle coverage for LRU eviction, TTL eviction, active in-flight requests, reset invalidation, history trimming, and more pagination state

Copilot AI review requested due to automatic review settings May 14, 2026 01:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds bounded in-memory retention for backend chat sessions to address unbounded growth in long-running processes.

Changes:

  • Adds configurable session count, TTL, and chat history limits.
  • Tracks session recency with OrderedDict and evicts expired/overflow sessions.
  • Documents session memory behavior and related environment variables.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
README.md Documents session retention environment variables and memory behavior.
backend/agents.py Adds LRU/TTL session tracking, configurable history trimming, and reset cleanup updates.
Comments suppressed due to low confidence (1)

backend/agents.py:656

  • The graph invocation above yields control, so another chat request or reset can evict this session before the response is stored. This direct history append can then fail with KeyError and leave newly assigned session_memory untracked by _session_last_seen; revalidate/recreate the session state atomically after the await or guard these mutations with a lock.
            self.chat_history[session_id].extend([f"User: {query}", f"Assistant: {response_text}"])
            self._trim_history(session_id)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/agents.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enhancement: Session memory grows unbounded — add eviction and chat history cap

2 participants