DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(llm): keep Codex prompt cache key stable across summary updates (#1991)
drakeo338b9bf160e5e15
diff --git a/deeptutor/services/llm/provider_core/openai_codex_provider.py b/deeptutor/services/llm/provider_core/openai_codex_provider.pyindex 9b27da3c2..de0dbd1e9 100644--- a/deeptutor/services/llm/provider_core/openai_codex_provider.py+++ b/deeptutor/services/llm/provider_core/openai_codex_provider.py@@ -56,6 +56,7 @@ class OpenAICodexProvider(LLMProvider):model_slug = _strip_model_prefix(model_name)system_prompt, input_items = convert_messages(messages)+ prompt_cache_key = _prompt_cache_key(messages)body: dict[str, Any] = {"model": model_slug,"store": False,@@ -64,7 +65,7 @@ class OpenAICodexProvider(LLMProvider):"input": input_items,"text": {"verbosity": "medium"},"include": ["reasoning.encrypted_content"],- "prompt_cache_key": _prompt_cache_key(messages),+ "prompt_cache_key": prompt_cache_key,"tool_choice": convert_tool_choice(tool_choice) or "auto","parallel_tool_calls": True,}@@ -78,7 +79,9 @@ class OpenAICodexProvider(LLMProvider):try:token = await self._load_token()service.validate_runtime_profile(token, model_slug, reasoning_effort)- headers = _build_headers(token.account_id, token.access_token)+ headers = _build_headers(+ token.account_id, token.access_token, session_id=prompt_cache_key+ )try:content, tool_calls, finish_reason = await _request_codex(CODEX_RESPONSES_URL,@@ -181,7 +184,7 @@ def _strip_model_prefix(model: str) -> str:return model-def _build_headers(account_id: str, token: str) -> dict[str, str]:+def _build_headers(account_id: str, token: str, session_id: str | None = None) -> dict[str, str]:if not token:raise RuntimeError("OpenAI Codex is not logged in. Run `deeptutor provider login openai-codex`."@@ -196,6 +199,8 @@ def _build_headers(account_id: str, token: str) -> dict[str, str]:}if account_id:headers["chatgpt-account-id"] = str(account_id)+ if session_id:+ headers["session-id"] = session_idreturn headers@@ -225,13 +230,13 @@ async def _request_codex(def _prompt_cache_key(messages: list[dict[str, Any]]) -> str:- # Routing affinity must survive ordinary history growth. The system prefix- # identifies reusable instructions; the provider still verifies all tokens.+ # 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.prefix = []- for message in messages:- if message.get("role") != "system":- break- prefix.append({"role": "system", "content": message.get("content")})+ if messages and messages[0].get("role") == "system":+ prefix.append({"role": "system", "content": messages[0].get("content")})raw = json.dumps(prefix, ensure_ascii=True, sort_keys=True)return hashlib.sha256(raw.encode("utf-8")).hexdigest()diff --git a/tests/services/llm/test_openai_codex_oauth_provider.py b/tests/services/llm/test_openai_codex_oauth_provider.pyindex 32ffb0378..0759bec30 100644--- a/tests/services/llm/test_openai_codex_oauth_provider.py+++ b/tests/services/llm/test_openai_codex_oauth_provider.py@@ -301,3 +301,55 @@ def test_provider_source_no_longer_imports_oauth_cli_kit() -> None:assert "oauth_cli_kit" not in sourceassert "get_codex_oauth_service" in sourceassert OpenAICodexProvider().get_default_model() == "openai-codex/gpt-5.6-sol"+++def _summary_messages(summary: str) -> list[dict[str, Any]]:+ return [+ {"role": "system", "content": "Tutor instructions"},+ {"role": "system", "content": summary},+ {"role": "user", "content": "Q"},+ ]+++def test_prompt_cache_key_is_stable_across_summary_updates() -> None:+ key_a = module._prompt_cache_key(_summary_messages("Summary A"))+ key_b = module._prompt_cache_key(_summary_messages("Summary B"))++ assert key_a == key_b+++def test_prompt_cache_key_differs_for_different_instructions() -> None:+ other = _summary_messages("Summary A")+ other[0]["content"] = "Other instructions"++ assert module._prompt_cache_key(other) != module._prompt_cache_key(+ _summary_messages("Summary A")+ )+++async def test_session_id_header_matches_body_prompt_cache_key(+ monkeypatch: pytest.MonkeyPatch,+) -> 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()+ await provider.chat(_summary_messages("Summary A"), model="openai-codex/gpt-5.6-sol")+ await provider.chat(_summary_messages("Summary B"), model="openai-codex/gpt-5.6-sol")++ for headers, body in requests:+ assert headers["session-id"] == body["prompt_cache_key"]+ assert requests[0][0]["session-id"] == requests[1][0]["session-id"]