Skip to content

fix(conversation): repin prompt_cache_key on switch_profile - #2920

Merged
VascoSch92 merged 1 commit into
mainfrom
2918-switch_profile-drops-prompt_cache_key-restore-works-today-but-needs-explicit-coverage
Apr 22, 2026
Merged

VascoSch92 merged 1 commit into
mainfrom
2918-switch_profile-drops-prompt_cache_key-restore-works-today-but-needs-explicit-coverage

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Apr 22, 2026 •

Copy link
Copy Markdown
Member

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

  • local_conversation.py: extract pin logic into _pin_prompt_cache_key(); call it from init and from switch_profile() after the agent LLM swap.
  • tests/sdk/conversation/test_switch_model.py: add test_switch_profile_preserves_prompt_cache_key covering initial pin, fast→slow switch, and switching back to a cached registry entry.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:f4e8817-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-f4e8817-python \
  ghcr.io/openhands/agent-server:f4e8817-python

All tags pushed for this build

ghcr.io/openhands/agent-server:f4e8817-golang-amd64
ghcr.io/openhands/agent-server:f4e8817-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:f4e8817-golang-arm64
ghcr.io/openhands/agent-server:f4e8817-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:f4e8817-java-amd64
ghcr.io/openhands/agent-server:f4e8817-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:f4e8817-java-arm64
ghcr.io/openhands/agent-server:f4e8817-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:f4e8817-python-amd64
ghcr.io/openhands/agent-server:f4e8817-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:f4e8817-python-arm64
ghcr.io/openhands/agent-server:f4e8817-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:f4e8817-golang
ghcr.io/openhands/agent-server:f4e8817-java
ghcr.io/openhands/agent-server:f4e8817-python

About Multi-Architecture Support

  • Each variant tag (e.g., f4e8817-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., f4e8817-python-amd64) are also available if needed

@github-actions

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean bug fix. The helper method eliminates duplication and the test coverage is solid. LGTM! ✅

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py4332594%296, 301, 430, 473, 491, 507, 572, 769–770, 773, 933, 941, 943, 947–948, 959, 961–963, 988, 1183, 1187, 1257, 1264–1265
TOTAL23096650871% 

@VascoSch92

Copy link
Copy Markdown
Member Author

@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

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, thank you!

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ 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 -v

Output:

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-coverage

Step 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 -v

Output:

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 -v

Result: 7 passed, 6 warnings in 0.09s

All existing switch_profile tests continue to pass:

  • test_switch_profile
  • test_switch_profile_updates_state
  • test_switch_between_profiles
  • test_switch_reuses_registry_entry
  • test_switch_nonexistent_raises
  • test_switch_then_send_message

Step 2 — Run full conversation test suite:

uv run pytest tests/sdk/conversation/ -v

Result: 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:

  1. 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)
  2. Called from __init__ at line 192 (preserving existing behavior)

  3. 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_copy updates only agent_context and mcp_config, not llm — no repin needed
  • Line 1015: Copies agent.llm for a temporary question LLM, doesn't replace self.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.

@VascoSch92
VascoSch92 merged commit 1ea8d64 into main Apr 22, 2026
40 of 41 checks passed
@VascoSch92
VascoSch92 deleted the 2918-switch_profile-drops-prompt_cache_key-restore-works-today-but-needs-explicit-coverage branch April 22, 2026 13:04
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
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.

switch_profile() drops prompt_cache_key; restore works today but needs explicit coverage

3 participants