The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(agent): single owner for Responses route predicates
Teknium420e156bf36e
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex e63e28f3bd3..936d1d7463b 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -513,15 +513,9 @@ def estimate_request_context_tokens(api_payload: Any) -> int:def _is_openai_codex_backend(agent) -> bool:- base_url_lower = str(getattr(agent, "_base_url_lower", "") or "")- base_url_hostname = str(getattr(agent, "_base_url_hostname", "") or "")- return (- getattr(agent, "provider", None) == "openai-codex"- or (- base_url_hostname == "chatgpt.com"- and "/backend-api/codex" in base_url_lower- )- )+ from agent.codex_responses_adapter import classify_responses_route++ return classify_responses_route(agent).is_codex_backenddef openai_codex_stale_timeout_floor(est_tokens: int) -> float:@@ -1878,18 +1872,11 @@ def build_api_kwargs(agent, api_messages: list, tools_for_api: list | None = Nonif agent.api_mode == "codex_responses":_ct = agent._get_transport()- is_github_responses = (- base_url_host_matches(agent.base_url, "models.github.ai")- or base_url_host_matches(agent.base_url, "githubcopilot.com")- )- is_codex_backend = (- agent.provider == "openai-codex"- or (- agent._base_url_hostname == "chatgpt.com"- and "/backend-api/codex" in agent._base_url_lower- )+ from agent.codex_responses_adapter import classify_responses_route++ is_codex_backend, is_xai_responses, is_github_responses = (+ classify_responses_route(agent))- is_xai_responses = agent.provider in {"xai", "xai-oauth"} or agent._base_url_hostname == "api.x.ai"_msgs_for_codex = agent._prepare_messages_for_non_vision_model(api_messages)# Native server-side compaction (gpt-5.6 on direct OpenAI API /diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.pyindex 42c4b693879..7dc3795ef80 100644--- a/agent/codex_responses_adapter.py+++ b/agent/codex_responses_adapter.py@@ -17,7 +17,7 @@ import reimport unicodedataimport uuidfrom types import SimpleNamespace-from typing import Any, Dict, List, Optional+from typing import Any, Dict, List, NamedTuple, Optionalfrom agent.message_sanitization import deterministic_call_idfrom agent.prompt_builder import DEFAULT_AGENT_IDENTITY@@ -823,30 +823,50 @@ def _chat_messages_to_responses_input(return prune_pre_checkpoint_items(items, item_sources=item_sources)-def _agent_is_codex_backend(agent: Any) -> bool:- if getattr(agent, "provider", None) == "openai-codex":- return True- hostname = str(getattr(agent, "_base_url_hostname", "") or "").lower()- lower = str(getattr(agent, "_base_url_lower", "") or "")- if not lower:- lower = str(getattr(agent, "base_url", "") or "").lower()- if hostname == "chatgpt.com" and "/backend-api/codex" in lower:- return True- return "chatgpt.com" in lower and "/backend-api/codex" in lower+class ResponsesRouteFlags(NamedTuple):+ """Which special Responses-API route an agent is talking to.+ Single owner of the codex/xai/github route predicates. Every site that+ needs these flags (request kwargs build, preflight estimation, silent-+ reject hints) must call :func:`classify_responses_route` instead of+ re-implementing the string comparisons inline — inline copies drift+ (backend-identity class: #22548/#70893/#59561/#72468).+ """-def _agent_is_github_responses(agent: Any) -> bool:- hostname = str(getattr(agent, "_base_url_hostname", "") or "").lower()- lower = str(getattr(agent, "_base_url_lower", "") or getattr(agent, "base_url", "") or "").lower()- return hostname in {"models.github.ai", "githubcopilot.com"} or (- "models.github.ai" in lower or "githubcopilot.com" in lower- )+ is_codex_backend: bool+ is_xai_responses: bool+ is_github_responses: bool-def _agent_is_xai_responses(agent: Any) -> bool:+def classify_responses_route(agent: Any) -> ResponsesRouteFlags:+ """Classify the agent's Responses route from provider + base URL.++ Host checks are exact-host-or-subdomain (``base_url_hostname``+ semantics), never substring matching — ``https://evil.com/models.github.ai``+ must not classify as a GitHub route.+ """+ from utils import base_url_hostname+provider = getattr(agent, "provider", None)+ base_url = str(getattr(agent, "base_url", "") or "")hostname = str(getattr(agent, "_base_url_hostname", "") or "").lower()- return provider in {"xai", "xai-oauth"} or hostname == "api.x.ai"+ if not hostname:+ hostname = base_url_hostname(base_url)+ lower = str(getattr(agent, "_base_url_lower", "") or base_url).lower()++ def _host_is(domain: str) -> bool:+ return hostname == domain or hostname.endswith("." + domain)++ is_codex_backend = provider == "openai-codex" or (+ _host_is("chatgpt.com") and "/backend-api/codex" in lower+ )+ is_github_responses = _host_is("models.github.ai") or _host_is("githubcopilot.com")+ is_xai_responses = provider in {"xai", "xai-oauth"} or hostname == "api.x.ai"+ return ResponsesRouteFlags(+ is_codex_backend=is_codex_backend,+ is_xai_responses=is_xai_responses,+ is_github_responses=is_github_responses,+ )def estimate_native_responses_preflight_tokens(@@ -872,9 +892,7 @@ def estimate_native_responses_preflight_tokens(if not isinstance(messages, list):return None- is_codex_backend = _agent_is_codex_backend(agent)- is_xai_responses = _agent_is_xai_responses(agent)- is_github_responses = _agent_is_github_responses(agent)+ is_codex_backend, is_xai_responses, is_github_responses = classify_responses_route(agent)from agent.native_compaction import native_compaction_context_managementdiff --git a/run_agent.py b/run_agent.pyindex d9707a3e0af..b710a2263bc 100644--- a/run_agent.py+++ b/run_agent.py@@ -1559,14 +1559,9 @@ class AIAgent:"""if self.api_mode != "codex_responses":return None- is_codex_backend = (- self.provider == "openai-codex"- or (- getattr(self, "_base_url_hostname", "") == "chatgpt.com"- and "/backend-api/codex" in (getattr(self, "_base_url_lower", "") or "")- )- )- if not is_codex_backend:+ from agent.codex_responses_adapter import classify_responses_route++ if not classify_responses_route(self).is_codex_backend:return Noneeff_model = (model if model is not None else self.model) or ""model_lower = eff_model.lower()