The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(transports): strip replayed reasoning_details on the Nous Portal wire
Brooklyn Nicholson50d970e92b89
diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.pyindex d66dd5ab648..52ba191bf03 100644--- a/agent/transports/chat_completions.py+++ b/agent/transports/chat_completions.py@@ -258,7 +258,7 @@ def _model_consumes_thought_signature(model: Any) -> bool:def _route_replays_reasoning_details(base_url: Any) -> bool:"""True when the target route reads replayed ``reasoning_details`` (OpenRouter's unified- reasoning array, also consumed by the Nous Portal).+ reasoning array).Every other chat-completions endpoint either ignores the field or, when its schema isstrict (Groq, Mistral, Cerebras, opencode relays: ``property 'reasoning_details' is@@ -266,10 +266,16 @@ def _route_replays_reasoning_details(base_url: Any) -> bool:request with HTTP 400/422 — so a reasoning turn produced earlier in the session wedges everylater turn once the model is switched (#70233). The stored history keeps the field; only thewire copy drops it.++ The Nous Portal read the field too (multi-turn reasoning continuity), but it enforces a+ cumulative replayed-reasoning budget: replaying stored reasoning_details wedges long+ sessions with a non-retryable 400 (#118182), so the Portal now strips like every other+ route. Stored history keeps the field, so a route that genuinely replays it (OpenRouter,+ #129037) still receives it."""from utils import base_url_host_matches- return base_url_host_matches(base_url, "openrouter.ai") or base_url_host_matches(base_url, "nousresearch.com")+ return base_url_host_matches(base_url, "openrouter.ai")def _has_replayable_thought_signature(extra_content: Any) -> bool:diff --git a/tests/agent/test_provider_history_parity.py b/tests/agent/test_provider_history_parity.pyindex cf7f9b5b034..de82218a50b 100644--- a/tests/agent/test_provider_history_parity.py+++ b/tests/agent/test_provider_history_parity.py@@ -34,3 +34,52 @@ def test_native_carriers_follow_only_their_owner_on_each_request():only_native = [{"role": "assistant", "content": "answer", "reasoning_details": [carrier]}]assert "reasoning_details" not in transport.convert_messages(only_native, base_url=OPENROUTER)[0]+++NOUS_PORTAL = "https://inference-api.nousresearch.com/v1"+++def test_nous_portal_strips_replayed_reasoning_details_from_wire():+ """The Portal enforces a cumulative replayed-reasoning budget; replaying stored+ reasoning_details wedges long sessions with a non-retryable 400 (#118182). Only the+ wire copy is stripped — stored history keeps the field, so switching to a replaying+ route (OpenRouter) still replays it."""+ standard = {"type": "reasoning.encrypted", "data": "opaque-signature"}+ history = [{"role": "assistant", "content": "answer", "reasoning_details": [standard]}]+ original = deepcopy(history)+ transport = ChatCompletionsTransport()++ for base_url in (NOUS_PORTAL, "https://stg-inference-api.nousresearch.com/v1"):+ wire = transport.convert_messages(deepcopy(history), base_url=base_url)+ assert "reasoning_details" not in wire[0]+ assert history == original++ # Substring lookalikes never matched the allowlist; they still strip (strict routes).+ assert "reasoning_details" not in transport.convert_messages(+ deepcopy(history), base_url="https://nousresearch.com.evil.io/v1")[0]+++def test_openrouter_replay_unchanged_by_portal_strip():+ """Sibling #129037: the OpenRouter branch of the allowlist still keeps standard+ reasoning_details on the wire."""+ standard = {"type": "reasoning.encrypted", "data": "opaque-signature"}+ history = [{"role": "assistant", "content": "answer", "reasoning_details": [standard]}]+ transport = ChatCompletionsTransport()++ assert transport.convert_messages(deepcopy(history), base_url=OPENROUTER)[0]["reasoning_details"] == [standard]+++def test_profile_native_carrier_still_replayed_on_portal_route():+ """A profile declaring a native carrier type consumes replayed details by contract,+ independent of the route allowlist — the Portal strip must not break that."""+ owner = ProviderProfile(name="native-owner", base_url="process://native-owner")+ owner.native_reasoning_details_type = "native-owner.native_assistant"+ carrier = {"type": owner.native_reasoning_details_type, "messages": [{"text": "private"}]}+ standard = {"type": "reasoning.encrypted", "data": "opaque-signature"}+ history = [{"role": "assistant", "content": "answer", "reasoning_details": [carrier, standard]}]+ original = deepcopy(history)+ transport = ChatCompletionsTransport()++ wire = transport.build_kwargs("test", history, provider_profile=owner, base_url=NOUS_PORTAL)["messages"]+ assert wire[0]["reasoning_details"] == [carrier, standard]+ assert history == originaldiff --git a/tests/agent/transports/test_chat_completions_reasoning_details_replay.py b/tests/agent/transports/test_chat_completions_reasoning_details_replay.pyindex 6a686b08dca..7ea5dc373f6 100644--- a/tests/agent/transports/test_chat_completions_reasoning_details_replay.py+++ b/tests/agent/transports/test_chat_completions_reasoning_details_replay.py@@ -1,6 +1,7 @@-"""``reasoning_details`` replay is route-scoped: OpenRouter/Nous read it, every other+"""``reasoning_details`` replay is route-scoped: OpenRouter reads it, every otherchat-completions route gets a wire copy without it (strict schemas 400/422 on the field,-wedging the session after an in-session model switch — hermes-agent#70233)."""+wedging the session after an in-session model switch — hermes-agent#70233; the Nous Portal+additionally 400s on a cumulative replayed-reasoning budget — hermes-agent#118182)."""from openai import OpenAI@@ -24,8 +25,10 @@ def test_auxiliary_wire_drops_reasoning_details_only_for_non_replaying_routes():assert any("reasoning_details" in m for m in kwargs["messages"])-def test_openrouter_and_nous_routes_keep_reasoning_details():+def test_openrouter_keeps_and_nous_strips_reasoning_details():transport = get_transport("chat_completions")- for base_url in ("https://openrouter.ai/api/v1", "https://inference-api.nousresearch.com/v1"):- kwargs = transport.build_kwargs("m", _HISTORY, base_url=base_url)- assert any("reasoning_details" in m for m in kwargs["messages"]), base_url+ kwargs = transport.build_kwargs("m", _HISTORY, base_url="https://openrouter.ai/api/v1")+ assert any("reasoning_details" in m for m in kwargs["messages"])+ kwargs = transport.build_kwargs("m", _HISTORY, base_url="https://inference-api.nousresearch.com/v1")+ assert all("reasoning_details" not in m for m in kwargs["messages"]), "Nous Portal strips (#118182)"+ assert "reasoning_details" in _HISTORY[1] # durable history is untoucheddiff --git a/tests/e2e/core/providers/test_chat_reasoning_variants.py b/tests/e2e/core/providers/test_chat_reasoning_variants.pyindex 90e995bb6cd..0b1756cde00 100644--- a/tests/e2e/core/providers/test_chat_reasoning_variants.py+++ b/tests/e2e/core/providers/test_chat_reasoning_variants.py@@ -24,7 +24,6 @@ import pytestfrom tests.e2e.core.providers._openai_helpers import (READ_TOOL,Home,- bug_assertions,chat_messages,custom_chat_config,db_messages,@@ -35,12 +34,6 @@ from tests.fakes.providers.chat_variants import CText, CTools, FakeChatVariantSepytestmark = pytest.mark.skipif(not sys.platform.startswith("linux"), reason="subprocess harness is Linux-gated")-KNOWN: dict[str, tuple[str, str]] = {- "reasoning_budget_session_keeps_answering": (- r"turns \[\d[\d, ]*\] were not answered once replayed reasoning passed the budget",- "#118182 replayed reasoning_details grow past a route budget and wedge the session on a 400"),-}-def _rd(tag: str, text: str | None = None) -> list[dict]:return [{"type": "reasoning.text", "text": text or f"thinking {tag}", "signature": f"sig-{tag}",@@ -126,7 +119,9 @@ def test_reasoning_content_echoed_on_tool_call_messages(tmp_path) -> None:def test_long_session_does_not_wedge_on_replayed_reasoning_budget(tmp_path) -> None:"""Each turn mints ~1 KB of reasoning; the route 400s ("Provider returned error", not- retryable) once the replayed total passes 4 KB. Every turn must still be answered."""+ retryable) once the replayed total passes 4 KB. The Portal never sees replayed+ reasoning_details on the wire (#118182), so every turn is answered and the budget is+ never crossed, while state.db keeps the blocks for a later switch to a replaying route."""turns = 8script = [CText(f"ANSWER-{i}", reasoning_details=_rd(str(i), "r" * 1000)) for i in range(turns)]h = Home(tmp_path)@@ -143,7 +138,12 @@ def test_long_session_does_not_wedge_on_replayed_reasoning_budget(tmp_path) -> Nrecords = srv.main_records()assert records and {r["host"] for r in records} == {"inference-api.nousresearch.com"}, "precondition: impersonated"rejected = [i for i, r in enumerate(records) if r.get("response") == "route_rejection"]- assert rejected, "precondition: the replayed total crossed the route budget at least once"+ assert rejected == [], f"route budget crossed at requests {rejected}: replayed reasoning_details reached the wire"+ leaked = [(i, m) for i, r in enumerate(records) for m in chat_messages(r["body"], "assistant")+ if "reasoning_details" in m]+ assert leaked == [], f"reasoning_details sent to the Portal wire: {leaked}"missing = [i for i in range(turns) if f"ANSWER-{i}" not in answers[i]]- with bug_assertions(KNOWN, "reasoning_budget_session_keeps_answering"):- assert not missing, f"turns {missing} were not answered once replayed reasoning passed the budget: {answers}"+ assert not missing, f"turns {missing} were not answered: {answers}"+ stored = [json.loads(r["reasoning_details"]) for r in db_messages(h, sid)+ if r["role"] == "assistant" and r["reasoning_details"]]+ assert stored == [_rd(str(i), "r" * 1000) for i in range(turns)], "state.db must keep the blocks for a switch back"