Skip to content

fix(model): clear DeepSeek reasoning cache on ChatAgent reset - #4288

Open
Alphaxiaoteng wants to merge 2 commits into
camel-ai:masterfrom
Alphaxiaoteng:fix/deepseek-reasoning-cache-reset
Open

Alphaxiaoteng wants to merge 2 commits into
camel-ai:masterfrom
Alphaxiaoteng:fix/deepseek-reasoning-cache-reset

Conversation

@Alphaxiaoteng

Copy link
Copy Markdown

Summary

Fixes #4283.

DeepSeekModel caches reasoning_content by session ID. In ChatAgent, the reasoning cache was not cleared upon calling reset(). When an agent is reused or pooled across distinct tasks, previous task reasoning could leak into subsequent tasks when assistant messages shared identical content (e.g. "Done.").

Changes

  1. Added clear_reasoning_cache(session_id) to DeepSeekModel to invalidate reasoning caches per session ID or globally.
  2. In ChatAgent.reset(), iterate over backend models and invoke clear_reasoning_cache with the agent's ID when available.
  3. Added unit test test_deepseek_reasoning_cache_cleared_on_agent_reset in test/models/test_deepseek_model.py.

Verification

  • Ran pytest test/models/test_deepseek_model.py (16 passed).

@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 514e9644-0651-4a1e-a7ba-4ba32200c662

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

if hasattr(self, "model_backend"):
for model in getattr(self.model_backend, "models", []):
if hasattr(model, "clear_reasoning_cache"):
model.clear_reasoning_cache(self.agent_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran this against 4a356ce9 in a clean python:3.11-slim container. reset() clears the entry keyed by self.agent_id, but the cache is written under get_current_agent_id() (deepseek_model.py:167), and the sync streaming path never sets that. step() dispatches to _stream() at chat_agent.py:2932, while set_current_agent_id is called at 2962 inside _step_impl, the non-streaming branch. A streaming step therefore caches under __default__ and this loop misses it.

Driven through agent.step(), with only the HTTP call stubbed: the stub calls the real _get_reasoning_session_id() and the real _cache_reasoning_content(), so the key derivation is untouched.

stream=True    session id at cache-write : '__default__'
               cache keys AFTER reset    : ['__default__']
               reasoning still injected  : SECRET TASK A REASONING

stream=False   session id at cache-write : 'b2d9cf72-e4da-...'
               cache keys AFTER reset    : []
               reasoning still injected  : None

astep() already sets the id at line 3231, ahead of both of its branches, so the async side does not have this gap. Moving those same two lines out of _step_impl and into step() above the stream check makes the streaming arm behave like the second block, and test/models/test_deepseek_model.py still passes 16. I did not exercise async streaming.

Unrelated to the fix: camel/toolkits/base.py carries an mcp import shim that #4294 is already handling as a dependency cap.

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] DeepSeekModel's per-agent reasoning cache is never cleared by ChatAgent.reset(), leaking reasoning_content into unrelated later tasks

2 participants