Repository navigation
fix(conversation): repin prompt_cache_key on switch_profile - #2920
Conversation
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
Clean bug fix. The helper method eliminates duplication and the test coverage is solid. LGTM! ✅
|
@enyst If for you is ok, I will not re-run the evals, as we know that the prompt_cache_key and the mechanism which come together is working because of last PRs |
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
Fix successfully prevents _prompt_cache_key from becoming None after profile switches.
Does this PR achieve its stated goal?
Yes. The PR fixes issue #2918 where switch_profile() dropped the _prompt_cache_key, causing it to become None on every mid-conversation LLM switch. The fix centralizes the pinning logic in a new _pin_prompt_cache_key() helper and calls it from both __init__ and switch_profile(), ensuring the cache key is preserved across all profile switches.
| Phase | Result |
|---|---|
| Environment Setup | ✅ Dependencies installed, 445 conversation tests runnable |
| CI & Tests | ✅ sdk-tests pass (2m9s), tools/workspace/agent-server tests pass |
| Functional Verification | ✅ Bug reproduced on main, fix verified on PR branch |
Functional Verification
Test 1: Reproduce the bug on main branch
Step 1 — Reproduce the bug (without the fix):
Checked out main branch and added the new regression test to verify the bug exists:
git checkout main
# Added test_switch_profile_preserves_prompt_cache_key to test_switch_model.py
uv run pytest tests/sdk/conversation/test_switch_model.py::test_switch_profile_preserves_prompt_cache_key -vOutput:
FAILED tests/sdk/conversation/test_switch_model.py::test_switch_profile_preserves_prompt_cache_key
AssertionError: assert None == '3fbb7f6a-7e50-443f-8705-91a2aa8c4ba4'
+ where None = LLM(..., usage_id='profile:fast')._prompt_cache_key
Interpretation: This confirms the bug exists. After calling switch_profile("fast"), the _prompt_cache_key became None instead of preserving the conversation ID (3fbb7f6a-...). This would cause poor cache hit rates and higher LLM costs, reintroducing issue #2904.
Step 2 — Apply the PR's changes:
Checked out the PR branch:
git checkout 2918-switch_profile-drops-prompt_cache_key-restore-works-today-but-needs-explicit-coverageStep 3 — Re-run with the fix in place:
Ran the same test on the PR branch:
uv run pytest tests/sdk/conversation/test_switch_model.py::test_switch_profile_preserves_prompt_cache_key -vOutput:
tests/sdk/conversation/test_switch_model.py::test_switch_profile_preserves_prompt_cache_key PASSED [100%]
1 passed, 5 warnings in 0.04s
Interpretation: The fix works. The _prompt_cache_key is now preserved across profile switches (fast→slow→fast), ensuring the cache key remains equal to the conversation ID throughout.
Test 2: Verify no regressions in related tests
Step 1 — Run all switch_model tests:
uv run pytest tests/sdk/conversation/test_switch_model.py -vResult: 7 passed, 6 warnings in 0.09s
All existing switch_profile tests continue to pass:
test_switch_profiletest_switch_profile_updates_statetest_switch_between_profilestest_switch_reuses_registry_entrytest_switch_nonexistent_raisestest_switch_then_send_message
Step 2 — Run full conversation test suite:
uv run pytest tests/sdk/conversation/ -vResult: 445 passed, 142 warnings in 102.60s
No regressions detected in the broader conversation test suite.
Test 3: Code review of the fix
Verified implementation:
-
New helper method
_pin_prompt_cache_key()at line 602-604:def _pin_prompt_cache_key(self) -> None: # Pin the OpenAI prefix-cache shard to this conversation (#2904, #2918). self.agent.llm._prompt_cache_key = str(self._state.id)
-
Called from
__init__at line 192 (preserving existing behavior) -
Called from
switch_profile()at line 629, immediately after the agent LLM swap:with self._state: self.agent = self.agent.model_copy(update={"llm": new_llm}) self._state.agent = self.agent self._pin_prompt_cache_key() # <-- NEW
Checked for other places where agent.llm might be changed:
- Line 476:
model_copyupdates onlyagent_contextandmcp_config, notllm— no repin needed - Line 1015: Copies
agent.llmfor a temporary question LLM, doesn't replaceself.agent— no repin needed
The fix is complete and correctly placed.
Issues Found
None.
Summary: This PR successfully fixes the prompt_cache_key bug introduced by mid-conversation profile switches. The implementation is clean, well-tested, and introduces no regressions. The centralized helper method makes the pinning logic maintainable and ensures consistency across all code paths that modify the agent's LLM.
What
Centralize _prompt_cache_key pinning in LocalConversation via a new _pin_prompt_cache_key() helper, called from both init and switch_profile().
Why
Fixes #2918. switch_profile() replaced self.agent.llm via model_copy() without repinning the key, so _prompt_cache_key became None on every mid-conversation LLM switch, reintroducing #2904 for all subsequent calls in that conversation (including via the agent-server /switch_profile route).
Changes
Type
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:f4e8817-pythonRun
All tags pushed for this build
About Multi-Architecture Support
f4e8817-python) is a multi-arch manifest supporting both amd64 and arm64f4e8817-python-amd64) are also available if needed