Skip to content

fix(agents): clear Responses API chain state on reset - #4302

Open
businessarshgoyal wants to merge 5 commits into
camel-ai:masterfrom
businessarshgoyal:devin/1787905137-clear-response-chain
Open

businessarshgoyal wants to merge 5 commits into
camel-ai:masterfrom
businessarshgoyal:devin/1787905137-clear-response-chain

Conversation

@businessarshgoyal

@businessarshgoyal businessarshgoyal commented Aug 28, 2026

Copy link
Copy Markdown

Related Issue

Closes #4299

Description

ChatAgent.clone() shares model backend instances, while Responses API continuation state is stored per agent session. Clearing or resetting a pooled clone previously rebuilt its local memory but left previous_response_id and message-offset state behind, allowing its next task to continue an unrelated conversation.

This adds a model-backend reset hook and invokes it from the common clear_memory() path, covering reset(), system-message changes, the memory toolkit, context summarization, and direct callers. OpenAI and OpenAI-compatible backends remove both response-chain maps for only that agent session, preserving state for sibling clones sharing the backend.

Agent context is also restored immediately before each sync or async model request. This keeps lazy streaming responses scoped to their owning clone even when multiple streams are created before they are consumed.

Regression coverage verifies full agent reset, direct memory clearing, and lazy synchronous and asynchronous streaming all use fresh per-agent Responses conversations without disturbing a sibling clone’s chain.

Testing:

  • uv run pytest -q test/agents/test_chat_agent_clone.py test/models/test_openai_model.py test/models/test_openai_compatible_model.py test/models/test_azure_openai_model.py (68 passed)
  • uv run pre-commit run --files camel/models/base_model.py camel/models/openai_model.py camel/models/openai_compatible_model.py camel/agents/chat_agent.py test/agents/test_chat_agent_clone.py test/models/test_openai_compatible_model.py
  • uv run mypy --namespace-packages camel/models/base_model.py camel/models/openai_model.py camel/models/openai_compatible_model.py camel/agents/chat_agent.py
  • uv lock --check

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Checklist

  • I have read and agree to the AI-Generated Code Policy (required)
  • I have linked this PR to an issue (required)
  • I have checked if any dependencies need to be added or updated in pyproject.toml and run uv lock
  • I have updated the tests accordingly (required for a bug fix or a new feature)
  • I have updated the documentation if needed
  • I have added examples if this is a new feature

Co-Authored-By: business.arshgoyal@gmail.com <business.arshgoyal@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 28, 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: Team

Run ID: 87017999-c0ac-49be-9d52-69a928b3bd53

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fd82f6416f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread camel/agents/chat_agent.py Outdated
Co-Authored-By: business.arshgoyal@gmail.com <business.arshgoyal@gmail.com>
Comment thread camel/agents/chat_agent.py
devin-ai-integration Bot and others added 2 commits August 28, 2026 14:29
Co-Authored-By: business.arshgoyal@gmail.com <business.arshgoyal@gmail.com>
Co-Authored-By: business.arshgoyal@gmail.com <business.arshgoyal@gmail.com>
@businessarshgoyal

Copy link
Copy Markdown
Author

Found and fixed one additional lazy-stream edge case in 8da0a35f: if streams for two clones were created before either was consumed, the later step()/astep() context could scope the first clone’s eventual model request. Context is now restored immediately before every sync and async model request, with delayed-consumption regressions for both streaming paths. The focused suite now passes 68 tests.

@businessarshgoyal

Copy link
Copy Markdown
Author

Thanks for re-running it, and for reverting the step() block to check the regression is load-bearing rather than taking my word for it.

On the async streaming path you left open: it is covered, and I checked the test is load-bearing the same way you did the sync one. test_async_streaming_clones_use_independent_model_sessions in test/agents/test_chat_agent_clone.py drives two clones through astep() with stream=True, consumes both async streams, then resets one clone. Pinning the _set_current_agent_context() call in the async streaming request path (chat_agent.py:5468, the one added in 8da0a35) makes only that test go red:

FAILED test/agents/test_chat_agent_clone.py::test_async_streaming_clones_use_independent_model_sessions
1 failed, 6 passed

7 passed with it restored. The reason there is a distinct call site per arm: astep() does set the context ahead of its stream branch, but for a lazily consumed stream the write happens whenever the caller iterates, so the context has to be re-set immediately before each model request, not just at entry.

I also merged current master into the branch (2dfcf313). The red checks were the repo-wide cannot import name 'FastMCP' from 'mcp.server' collection errors across 117 test modules, which master has since fixed; nothing in them touched camel/agents/chat_agent.py. test/agents/test_chat_agent_clone.py is 7 passed on the merge result.

Written by Devin

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] ChatAgent.reset() never clears OpenAIModel previous_response_id chain state, so a pooled agent's next task can continue the wrong conversation

2 participants