AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(providers): keep the question's shape so the second call reads its cache (#445)
Rufus0f55f643954f
diff --git a/packages/core/openexecutive/providers/translator.py b/packages/core/openexecutive/providers/translator.pyindex 8fd1a966..5f908f26 100644--- a/packages/core/openexecutive/providers/translator.py+++ b/packages/core/openexecutive/providers/translator.py@@ -215,6 +215,23 @@ def _request_caches_assistant_text(messages: list[Any]) -> bool:return False+def _request_caches_messages(messages: list[Any]) -> bool:+ """True iff any block of any message in the request carries a marker.++ Sets one shape for the text of every user turn sent as blocks, as the two+ checks above do for tool results and replies. The Executive caches the+ question being answered on the loop's first call+ (``executive._mark_turn_message``) and takes that marker off once the+ loop marker moves to the first tool result. If the shape followed the+ marker, the question would go out typed on the first call and flattened+ on the second, and the second call could not read what the first wrote.+ """+ for m in messages:+ if isinstance(m, dict) and _any_block_has_cache_control(m.get("content")):+ return True+ return False++def _anthropic_messages_to_openai(messages: list[Any]) -> list[dict[str, Any]]:"""Convert Anthropic ``messages`` to OpenAI chat-completions ``messages``.@@ -226,11 +243,12 @@ def _anthropic_messages_to_openai(messages: list[Any]) -> list[dict[str, Any]]:out: list[dict[str, Any]] = []typed_tool_results = _request_caches_tool_results(messages)typed_assistant_text = _request_caches_assistant_text(messages)+ typed_user_text = _request_caches_messages(messages)for m in messages:role = m.get("role")content = m.get("content")if role == "user":- out.extend(_user_content_to_openai(content, typed_tool_results))+ out.extend(_user_content_to_openai(content, typed_tool_results, typed_user_text))elif role == "assistant":out.append(_assistant_content_to_openai(content, typed_assistant_text))else:@@ -254,7 +272,7 @@ def _content_to_text(content: Any) -> str:def _user_content_to_openai(- content: Any, typed_tool_results: bool = False+ content: Any, typed_tool_results: bool = False, typed_text: bool = False) -> list[dict[str, Any]]:"""User-turn content: emit one user message, plus one ``tool`` role messageper Anthropic ``tool_result`` block so the OpenAI chat history threads@@ -263,8 +281,11 @@ def _user_content_to_openai(When any text block in ``content`` carries ``cache_control`` (used forAnthropic's rolling user-turn cache), the user message's ``content``stays as a typed-block array so the cache hint survives translation to- OpenRouter. Otherwise we flatten to a plain string — broader upstream+ OpenRouter. So does ``typed_text`` (a marker anywhere in the request, see+ ``_request_caches_messages``), so the text keeps one shape while the+ markers move. Otherwise we flatten to a plain string — broader upstreamcompatibility for non-Anthropic routing and slightly smaller wire bytes.+ A turn sent as a plain string stays one either way.A base64 PDF ``document`` block becomes an OpenAI ``file`` part (see``_file_part``) and forces the typed-array form, text and files in their@@ -279,7 +300,7 @@ def _user_content_to_openai(text_blocks: list[dict[str, Any]] = [] # populated when cache_control presenttext_chunks: list[str] = [] # populated for the flat-string fallbacktool_messages: list[dict[str, Any]] = []- preserve_typed = _any_block_has_cache_control(content)+ preserve_typed = typed_text or _any_block_has_cache_control(content)# Text and PDF parts in their original order, used only when a PDF is# present: a file part cannot ride in the flat-string form.ordered_parts: list[dict[str, Any]] = []diff --git a/packages/core/tests/unit/test_openrouter_translator.py b/packages/core/tests/unit/test_openrouter_translator.pyindex a8a8c327..508029ae 100644--- a/packages/core/tests/unit/test_openrouter_translator.py+++ b/packages/core/tests/unit/test_openrouter_translator.py@@ -1389,3 +1389,64 @@ def test_assistant_turns_stay_flat_without_a_history_marker() -> None:out = _anthropic_messages_to_openai(_history_request(marked=False))assert out[1]["content"] == "first answer"assert out[3]["content"] == "second answer"+++# --- the question being answered, cached on the loop's first call ------------+++def _strip_markers(message: dict) -> dict:+ out = dict(message)+ if isinstance(out.get("content"), list):+ out["content"] = [+ {k: v for k, v in blk.items() if k != "cache_control"} for blk in out["content"]+ ]+ return out+++def test_turn_message_keeps_its_shape_once_the_loop_marker_moves() -> None:+ """The loop's first call caches the question; from the second call the+ marker sits on the newest tool result instead. The question must go out+ in the same shape on both calls, or the second call cannot read the first+ call's write and pays for the question again."""+ from openexecutive.orchestrator.executive import (+ _apply_loop_cache_marker,+ _clear_turn_marker,+ _mark_turn_message,+ )+ from openexecutive.providers.translator import _anthropic_messages_to_openai++ messages: list[dict] = [+ {"role": "user", "content": "earlier question"},+ {"role": "assistant", "content": "earlier answer"},+ {"role": "user", "content": "facts and excerpts\n\nwhat did we pay for water?"},+ ]+ caller_count = len(messages)+ turn = _mark_turn_message(messages)+ first = _anthropic_messages_to_openai(messages)++ messages.append(+ {"role": "assistant", "content": [{"type": "tool_use", "id": "tu_1", "name": "search", "input": {}}]}+ )+ messages.append(+ {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "tu_1", "content": "found it"}]}+ )+ assert _apply_loop_cache_marker(messages, caller_count)+ _clear_turn_marker(turn)+ second = _anthropic_messages_to_openai(messages)++ assert isinstance(first[2]["content"], list)+ assert "cache_control" in first[2]["content"][-1]+ assert _strip_markers(first[2]) == _strip_markers(second[2]), (+ "the question changed shape between the loop's first and second call"+ )+ # History turns sent as strings stay strings.+ assert second[0]["content"] == "earlier question"+++def test_user_text_stays_flat_without_any_marker() -> None:+ from openexecutive.providers.translator import _anthropic_messages_to_openai++ out = _anthropic_messages_to_openai(+ [{"role": "user", "content": [{"type": "text", "text": "a"}, {"type": "text", "text": "b"}]}]+ )+ assert out == [{"role": "user", "content": "a\n\nb"}]