Skip to content

fix: prevent session context leakage by clearing Ollama KV cache on flush - #14673

Open
flysheep-ai wants to merge 1 commit into
danny-avila:mainfrom
flysheep-ai:openmeta/14669-bug-context-leak-due-to-lacking-clear-session
Open

fix: prevent session context leakage by clearing Ollama KV cache on flush#14673
flysheep-ai wants to merge 1 commit into
danny-avila:mainfrom
flysheep-ai:openmeta/14669-bug-context-leak-due-to-lacking-clear-session

Conversation

@flysheep-ai

Copy link
Copy Markdown

Summary

LibreChat currently does not clear the Ollama model's server-side KV cache when flushing the application cache, leading to context bleeding between separate chat sessions. A user reported receiving responses containing context from a previous BAML test run after starting a new chat (issue #14669).

This fix extends the cache flushing script (config/flush-cache.js) to also send a request to Ollama's /api/clear endpoint (if configured), ensuring the model's context cache is emptied alongside application caches.

Changes

  • Add flushOllamaCache() function that sends a POST to Ollama's /api/clear endpoint
  • Detect Ollama host from OLLAMA_HOST environment variable (defaults to http://localhost:11434)
  • Add proper error handling: timeouts and connection failures log warnings but never block the flush
  • Integrate the model cache clearing step after existing cache flushing operations

Validation

  • Dry-run mode (--dry-run) shows the endpoint that would be called without making the request
  • Verbose mode (--verbose) logs the exact endpoint being contacted
  • If Ollama is unreachable, a warning is logged and the flush continues normally
  • If Ollama is not in use (default state for most deployments), the /api/clear call simply fails gracefully

Risks

  • If the Ollama server is down or unreachable, a warning is logged but the flush continues — model cache clearing is best-effort
  • Clearing the model KV cache is a global operation affecting all active model contexts, which is expected during a cache flush

Closes #14669

…eakage

When flushing the application cache, also send a POST to Ollama's
/api/clear endpoint to purge the model's server-side KV cache. This
prevents context from previous conversations or runs from leaking into
new chat sessions.

The Ollama host is read from the OLLAMA_HOST environment variable,
defaulting to http://localhost:11434. If the Ollama server is
unreachable or the request times out, a warning is logged but the
flush operation continues — model cache clearing is best-effort and
should never block the main cache flush.
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.

[Bug]: Context leak due to lacking clear session

1 participant