Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(llm): scope Codex prompt cache key to the conversation

drakeo3382791c70e2d57
diff --git a/deeptutor/services/llm/capabilities.py b/deeptutor/services/llm/capabilities.py
index 927eea0f5..cf2d5e8a6 100644
--- a/deeptutor/services/llm/capabilities.py
+++ b/deeptutor/services/llm/capabilities.py
@@ -686,7 +686,7 @@ def get_effective_temperature(
#: supplies, so each request carries the turn's own session id. Stated here
#: rather than tested inline: the chat loop and the explore capability both
#: need it, and neither should have to name a provider to run a turn.
-SESSION_SCOPED_BINDINGS: frozenset[str] = frozenset({"codebuddy"})
+SESSION_SCOPED_BINDINGS: frozenset[str] = frozenset({"codebuddy", "openai_codex"})
def threads_session_id(binding: str | None) -> bool:
diff --git a/deeptutor/services/llm/provider_core/openai_codex_provider.py b/deeptutor/services/llm/provider_core/openai_codex_provider.py
index de0dbd1e9..78fe5245b 100644
--- a/deeptutor/services/llm/provider_core/openai_codex_provider.py
+++ b/deeptutor/services/llm/provider_core/openai_codex_provider.py
@@ -51,12 +51,13 @@ class OpenAICodexProvider(LLMProvider):
reasoning_effort: str | None,
tool_choice: str | dict[str, Any] | None,
on_content_delta: Callable[[str], Awaitable[None]] | None = None,
+ session_id: str | None = None,
) -> LLMResponse:
model_name = model or self.default_model
model_slug = _strip_model_prefix(model_name)
system_prompt, input_items = convert_messages(messages)
- prompt_cache_key = _prompt_cache_key(messages)
+ prompt_cache_key = _prompt_cache_key(messages, session_id)
body: dict[str, Any] = {
"model": model_slug,
"store": False,
@@ -148,8 +149,11 @@ class OpenAICodexProvider(LLMProvider):
tool_choice: str | dict[str, Any] | None = None,
**kwargs: Any,
) -> LLMResponse:
- del max_tokens, temperature, kwargs
- return await self._call_codex(messages, tools, model, reasoning_effort, tool_choice)
+ del max_tokens, temperature
+ session_id = _pop_session_id(kwargs)
+ return await self._call_codex(
+ messages, tools, model, reasoning_effort, tool_choice, session_id=session_id
+ )
async def chat_stream(
self,
@@ -164,7 +168,8 @@ class OpenAICodexProvider(LLMProvider):
on_reasoning_delta: Callable[[str], Awaitable[None]] | None = None,
**kwargs: Any,
) -> LLMResponse:
- del max_tokens, temperature, on_reasoning_delta, kwargs
+ del max_tokens, temperature, on_reasoning_delta
+ session_id = _pop_session_id(kwargs)
return await self._call_codex(
messages,
tools,
@@ -172,6 +177,7 @@ class OpenAICodexProvider(LLMProvider):
reasoning_effort,
tool_choice,
on_content_delta,
+ session_id=session_id,
)
def get_default_model(self) -> str:
@@ -229,11 +235,20 @@ async def _request_codex(
return await consume_sse(response, on_content_delta)
-def _prompt_cache_key(messages: list[dict[str, Any]]) -> str:
- # Routing affinity must survive ordinary history growth. Only the first
- # system message identifies the reusable instructions; later system
- # messages (e.g. a rolling summary) can change between calls and must not move the
- # key. The provider still verifies all tokens.
+def _pop_session_id(kwargs: dict[str, Any]) -> str | None:
+ return str(kwargs.pop("deeptutor_session_id", "") or "").strip() or None
+
+
+def _prompt_cache_key(messages: list[dict[str, Any]], session_id: str | None = None) -> str:
+ # Routing affinity must survive ordinary history growth. A conversation id,
+ # when the caller supplies one, scopes the key to that conversation so two
+ # chats sharing the same system prompt do not share a cache route; it is
+ # hashed so the raw id is not sent upstream. Without one, fall back to the
+ # first system message, which identifies the reusable instructions. Later
+ # system messages (e.g. a rolling summary) can change between calls and
+ # must not move the key. The provider still verifies all tokens.
+ if session_id:
+ return hashlib.sha256(f"session:{session_id}".encode()).hexdigest()
prefix = []
if messages and messages[0].get("role") == "system":
prefix.append({"role": "system", "content": messages[0].get("content")})
diff --git a/tests/services/llm/test_openai_codex_oauth_provider.py b/tests/services/llm/test_openai_codex_oauth_provider.py
index 0759bec30..b6b77b7a4 100644
--- a/tests/services/llm/test_openai_codex_oauth_provider.py
+++ b/tests/services/llm/test_openai_codex_oauth_provider.py
@@ -353,3 +353,56 @@ async def test_session_id_header_matches_body_prompt_cache_key(
for headers, body in requests:
assert headers["session-id"] == body["prompt_cache_key"]
assert requests[0][0]["session-id"] == requests[1][0]["session-id"]
+
+
+def test_prompt_cache_key_is_scoped_by_session_id() -> None:
+ messages = _summary_messages("Summary A")
+
+ key_a = module._prompt_cache_key(messages, "session-a")
+ key_b = module._prompt_cache_key(messages, "session-b")
+
+ assert key_a != key_b
+ assert "session-a" not in key_a
+ # A summary change inside one conversation must not move the key.
+ assert key_a == module._prompt_cache_key(_summary_messages("Summary B"), "session-a")
+ assert key_a != module._prompt_cache_key(messages)
+
+
[email protected]("stream", [False, True])
+async def test_session_id_kwarg_scopes_header_and_body(
+ monkeypatch: pytest.MonkeyPatch, stream: bool
+) -> None:
+ service = FakeCodexService()
+ requests: list[tuple[dict[str, str], dict[str, Any]]] = []
+
+ async def request(
+ url: str,
+ headers: dict[str, str],
+ body: dict[str, Any],
+ **_kwargs: Any,
+ ) -> tuple[str, list[Any], str]:
+ requests.append((headers, body))
+ return "ok", [], "stop"
+
+ monkeypatch.setattr(module, "get_codex_oauth_service", lambda: service)
+ monkeypatch.setattr(module, "_request_codex", request)
+
+ provider = OpenAICodexProvider()
+ call = provider.chat_stream if stream else provider.chat
+ for session in ("session-a", "session-b"):
+ await call(
+ _summary_messages("Summary A"),
+ model="openai-codex/gpt-5.6-sol",
+ deeptutor_session_id=session,
+ )
+
+ for headers, body in requests:
+ assert headers["session-id"] == body["prompt_cache_key"]
+ assert requests[0][1]["prompt_cache_key"] != requests[1][1]["prompt_cache_key"]
+
+
+def test_openai_codex_binding_threads_session_id() -> None:
+ from deeptutor.services.llm.capabilities import threads_session_id
+
+ assert threads_session_id("openai_codex")