Skip to content

Comments

feat: add pluggable chat storage with optional SQLite persistence and session-scoped caching#61

Open
tisha-varma wants to merge 1 commit intoINCF:mainfrom
tisha-varma:feat/persistent-chat-history-and-caching
Open

feat: add pluggable chat storage with optional SQLite persistence and session-scoped caching#61
tisha-varma wants to merge 1 commit intoINCF:mainfrom
tisha-varma:feat/persistent-chat-history-and-caching

Conversation

@tisha-varma
Copy link

Summary

Closes #60

Adds a modular storage abstraction for chat history with optional SQLite persistence and an optional session-scoped response cache to reduce redundant LLM calls.

Both features are opt-in. Default behavior remains fully in-memory and unchanged.

No new dependencies — implementation uses only Python stdlib (sqlite3, hashlib, collections.OrderedDict, threading).


What Changed

New Files

File Purpose
backend/chat_storage.py ChatStorage abstraction + InMemoryChatStorage (default) + SQLiteChatStorage (opt-in, WAL-enabled)
backend/response_cache.py ResponseCache with TTL expiry and LRU eviction (session-scoped)
tests/test_chat_storage.py 17 unit tests for both storage backends + factory
tests/test_response_cache.py 12 unit tests for cache (hit/miss, TTL, LRU eviction, session scoping)

Modified Files

File Change
backend/agents.py NeuroscienceAssistant now uses pluggable storage and cache instead of raw dicts
backend/main.py Added lightweight observability endpoints: GET /api/sessions, GET /api/cache/stats, POST /api/cache/clear
.env.template Added ENABLE_PERSISTENCE, ENABLE_CACHE, and tuning variables
.gitignore Excludes generated chat_history.db

Design Notes

  • Backward-compatibleNeuroscienceAssistant() works unchanged. handle_chat signature remains -> str. Default behavior is still in-memory.
  • Graceful fallback — If SQLite fails (e.g., read-only filesystem), the factory falls back to in-memory storage with a warning.
  • Session-scoped cache — Cache keys include session_id to prevent cross-session response leakage.
  • Large data remains in-memoryall_results lists are kept in _results_buffer to avoid database bloat.
  • Thread-safe SQLite — Uses a persistent connection (check_same_thread=False) guarded by a lock.

Configuration

# Optional features (both default to false)
ENABLE_PERSISTENCE=true
ENABLE_CACHE=true

# Storage configuration
CHAT_DB_PATH=./chat_history.db

# Cache tuning
CACHE_MAX_SIZE=128
CACHE_TTL_SECONDS=3600

Testing

  • 29 unit tests, all passing
  • Covers storage backends, cross-instance persistence, fallback behavior
  • Covers cache TTL, LRU eviction, session scoping
  • Suite runtime: ~0.27s
  • No flaky tests

All features are opt-in and can be toggled independently.

Happy to adjust scope or split changes based on feedback.

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.

Proposal: Persistent Chat History & Optional Semantic Caching

1 participant