fix(opencode): skip only the metadata map the proxy marked as generated

This commit is contained in:
Sean Murphy 2026-09-14 10:33:25 -07:00
parent b5bc7a0878
commit 0192faa6bd
2 changed files with 24 additions and 4 deletions

View file

@ -208,19 +208,23 @@ def _conversation_session_id(litellm_params: Mapping[str, object]) -> str | None
Trace ids and session ids the proxy generated for a session-less request are
per request, so they are never used: they would change the session every turn.
A ``litellm_session_id`` equal to the trace id was derived from it, not sent.
Only the metadata map marked as generated is skipped, so a session the caller
sent elsewhere still wins.
"""
metadata_maps: Final = tuple(
metadata
for metadata in (litellm_params.get("metadata"), litellm_params.get("litellm_metadata"))
if isinstance(metadata, Mapping)
)
if any(metadata.get(SESSION_ID_GENERATED_METADATA_KEY) for metadata in metadata_maps):
return None
caller_maps: Final = tuple(
metadata for metadata in metadata_maps if not metadata.get(SESSION_ID_GENERATED_METADATA_KEY)
)
proxy_generated: Final = len(caller_maps) != len(metadata_maps)
session_id: Final = litellm_params.get("litellm_session_id")
candidates: Final = (
*(metadata.get("session_id") for metadata in metadata_maps),
*(metadata.get("session_id") for metadata in caller_maps),
litellm_params.get("session_id"),
session_id if session_id != litellm_params.get("litellm_trace_id") else None,
session_id if not proxy_generated and session_id != litellm_params.get("litellm_trace_id") else None,
)
return next((candidate for candidate in candidates if isinstance(candidate, str) and candidate), None)

View file

@ -434,6 +434,22 @@ class TestMockedCompletion:
assert sent != "trace-123"
assert uuid.UUID(sent).version == 4
def test_go_caller_session_survives_a_generated_marker_in_the_other_metadata(self):
"""Only the metadata map the proxy marked as generated is ignored, so the caller's own session still wins."""
from litellm.llms.opencode.common_utils import with_opencode_session_header
headers = with_opencode_session_header(
"go",
{},
{
"litellm_session_id": "trace-123",
"metadata": {"session_id": "conv-1"},
"litellm_metadata": {"session_id": "trace-123", SESSION_ID_GENERATED_METADATA_KEY: True},
},
)
assert headers["x-opencode-session"] == "conv-1"
def test_zen_chat_sends_no_session_header(self, respx_mock, monkeypatch):
"""Only the Go surface requires the header."""
respx_mock.post("https://opencode.ai/zen/v1/chat/completions").mock(