Skip to content

The agent that grows with you

chore: merge origin/main (resolve hermes_cli/runtime_provider.py, website/docs/user-guide/features/codex-app-server-runtime.md)

teknium154c01bc19a46
diff --cc agent/codex_runtime.py
index 27c81c5280f,2b1736359a1..84d1e83ede3
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@@ -382,27 -451,14 +451,29 @@@ def _consume_user_interrupt(agent, acti
return interrupted, message
+def _codex_developer_instructions(agent) -> str:
+ """The prompt composition the standard loop sends as its system message (turn_context order)."""
+ developer_instructions = getattr(agent, "_cached_system_prompt", None) or ""
+ if getattr(agent, "ephemeral_system_prompt", None):
+ developer_instructions = (developer_instructions + "\n\n" + agent.ephemeral_system_prompt).strip()
+ return developer_instructions
+
+
def _ensure_codex_session(agent) -> None:
- """Lazily spawn one CodexAppServerSession per AIAgent (reused across turns, closed by the _cleanup hook)."""
+ """Lazily spawn one CodexAppServerSession per AIAgent (reused across turns, closed by the _cleanup hook).
+ A live session whose thread was started with a different prompt composition (TUI/Desktop ``/personality``
+ or a prompt mirror mutate the agent in place) is retired first so the new thread carries the current one."""
+ developer_instructions = _codex_developer_instructions(agent)
if getattr(agent, "_codex_session", None) is not None:
- return
+ # Only a session whose recorded composition differs is stale; one attached without a record is kept.
+ recorded = getattr(agent, "_codex_session_prompt", None)
+ if recorded is None or recorded == developer_instructions:
+ return
+ _close_codex_session(agent)
from agent.runtime_cwd import resolve_agent_cwd
from agent.transports.codex_app_server_session import CodexAppServerSession, _ServerRequestRouting
+ from hermes_cli.codex_runtime_switch import get_configured_codex_binary
+ from hermes_cli.config import load_config
# Approval callback: Hermes' standard prompt flow when a CLI thread installed one.
approval_callback = None
with suppress(Exception):
@@@ -421,24 -477,11 +492,25 @@@
# _emit_interim_assistant_message). Without this, Discord/Telegram users see no live tool-progress or
# interim commentary while codex_app_server is running — only the final answer (#33200). Supersedes the
# narrower item/started-only bridge from #38835.
+ # Hermes owns the prompt: the same composition the standard loop sends as its system message
+ # (cached per-session prompt + ephemeral additions such as channel overrides) rides along ONCE per
+ # thread as developerInstructions. A retired/recreated session re-sends the current composition;
+ # conversation history is still not projected into the codex thread (#74712, #26035).
+ agent._codex_session_prompt = developer_instructions
+ # A named custom provider (``providers.<name>``) maps onto codex's own ``[model_providers.<name>]``
+ # table: send the stable id plus the active model and let codex resolve base_url/env_key itself, so
+ # Hermes' credential never enters the JSON-RPC payload (#75186). openai/openai-codex keep codex's defaults.
+ model_provider = None
+ if str(getattr(agent, "provider", "") or "").strip().lower() == "custom":
+ from hermes_cli.runtime_provider_custom import codex_model_provider_id
+ model_provider = codex_model_provider_id(str(getattr(agent, "requested_provider", "") or ""))
agent._codex_session = CodexAppServerSession(
cwd=getattr(agent, "session_cwd", None) or str(resolve_agent_cwd()), approval_callback=approval_callback,
+ codex_bin=get_configured_codex_binary(load_config()),
request_routing=_ServerRequestRouting(auto_approve_exec=auto_approve_requests, auto_approve_apply_patch=auto_approve_requests),
on_event=make_codex_app_server_event_bridge(agent),
+ developer_instructions=developer_instructions or None,
+ model=getattr(agent, "model", None) if model_provider else None, model_provider=model_provider,
)
diff --cc hermes_cli/runtime_provider.py
index 4ef290ad916,5e90d901bb1..012db711df7
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@@ -261,15 -261,11 +261,16 @@@ def _api_key_provider_api_mode(provider
return _configured_or_fallback_api_mode(provider, model_cfg, base_url, effective_model, opencode_by_model=opencode_by_model)
-def _maybe_apply_codex_app_server_runtime(*, provider: str, api_mode: str, model_cfg: Optional[Dict[str, Any]]) -> str:
- """Opt-in rewrite to "codex_app_server" via ``model.openai_runtime``; only ``openai`` /
- ``openai-codex`` are eligible. No-op when unset, "auto", or empty. Applied once, on the
+def _maybe_apply_codex_app_server_runtime(*, provider: str, api_mode: str, model_cfg: Optional[Dict[str, Any]],
+ requested_provider: str = "") -> str:
+ """Opt-in rewrite to "codex_app_server" via ``model.openai_runtime``. Eligible: ``openai`` /
+ ``openai-codex``, and a configured named custom provider (``providers.<name>``) whose id codex
+ looks up in its own ``[model_providers.<name>]`` table (#75186). Anonymous ``custom`` has no
- stable id and stays ineligible. No-op when unset, "auto", or empty."""
++ stable id and stays ineligible. No-op when unset, "auto", or empty. Applied once, on the
+ runtime ``resolve_runtime_provider`` picked — never inside an individual ladder rung."""
- if model_cfg and provider in {"openai", "openai-codex"} and str(model_cfg.get("openai_runtime") or "").strip().lower() == "codex_app_server":
+ if not model_cfg or str(model_cfg.get("openai_runtime") or "").strip().lower() != "codex_app_server":
+ return api_mode
+ if provider in {"openai", "openai-codex"} or (provider == "custom" and codex_model_provider_id(requested_provider)):
return "codex_app_server"
return api_mode
@@@ -923,6 -911,8 +928,8 @@@ def resolve_runtime_provider(*, request
keyless fallback as ``auth_error``) → minimax-oauth
→ external-process → anthropic env → bedrock → registry api_key providers
8. OpenRouter / bare-custom fallback
- 9. ``model.openai_runtime`` overlay (openai/openai-codex only): rewrites the picked rung's
++ 9. ``model.openai_runtime`` overlay (openai/openai-codex, named custom providers): rewrites the picked rung's
+ api_mode to ``codex_app_server``; the rung's credential/endpoint is then not used
target_model overrides model_cfg["default"] when computing provider-specific api_mode (e.g.
OpenCode Zen/Go where different models route through different API surfaces)."""
requested_provider = resolve_requested_provider(requested)
@@@ -930,6 -920,15 +937,16 @@@
_raise_if_local_alias_missing_endpoint(requested_provider, explicit_base_url)
runtime = next(r for r in _ladder_rungs(requested_provider, explicit_api_key, explicit_base_url, target_model) if r)
_raise_for_credentialless_bare_custom(requested_provider, runtime)
+ # model.openai_runtime is applied ONCE, after the ladder: every rung (pool, OAuth store,
+ # explicit --api-key/--base-url, env key) hardcodes the wire api_mode for openai/openai-codex,
+ # so applying the opt-in inside one rung left the others on codex_responses (#115169).
+ api_mode = _maybe_apply_codex_app_server_runtime(
- provider=runtime.get("provider", ""), api_mode=runtime.get("api_mode", ""), model_cfg=_get_model_config())
++ provider=runtime.get("provider", ""), api_mode=runtime.get("api_mode", ""), model_cfg=_get_model_config(),
++ requested_provider=requested_provider)
+ if api_mode != runtime.get("api_mode"):
+ logger.info("model.openai_runtime=codex_app_server overrides the %s runtime (source=%s); its credential/endpoint "
+ "is not used — the app-server authenticates with its own login", runtime.get("provider"), runtime.get("source"))
+ runtime["api_mode"] = api_mode
return runtime
diff --cc website/docs/user-guide/features/codex-app-server-runtime.md
index 2c5998e3b73,e7923cc8d6f..bd3be92de94
--- a/website/docs/user-guide/features/codex-app-server-runtime.md
+++ b/website/docs/user-guide/features/codex-app-server-runtime.md
@@@ -440,7 -436,7 +468,8 @@@ Known limitations
- **Hermes auth and codex auth are separate sessions.** You need both `codex login` AND `hermes auth add openai-codex` for the cleanest UX (the runtime uses codex's session for the LLM call). This is a deliberate design choice in Hermes' `_import_codex_cli_tokens` — Hermes won't share OAuth state with codex CLI to avoid clobbering each other on token refresh.
- **`delegate_task`, `memory`, `session_search`, `todo` are unavailable on this runtime.** They need the running AIAgent context which a stateless MCP callback can't provide. Use `/codex-runtime auto` when you need these.
- **No inline patch preview in approval prompts when codex doesn't track the changeset.** Codex's `fileChange` approval params don't always carry the changeset. Hermes caches the data from the corresponding `item/started` notification when possible, but if approval arrives before the item has streamed, the prompt falls back to whatever `reason` codex provides.
+- **Conversation history is not projected into the codex thread.** The codex thread receives Hermes' system prompt when it starts plus each new user message; prior Hermes history (e.g. from a resumed session) is not replayed into it. When the composed prompt changes mid-session (for example `/personality` in the TUI or Desktop), the next turn retires the running thread and starts a new one carrying the updated prompt; that new thread does not inherit the retired thread's history.
+ - **`fallback_providers` fail over only on quota and rate-limit failures.** When a codex app-server turn fails with a billing / usage-limit / rate-limit error, Hermes switches to the configured [fallback provider](./fallback-providers.md) and retries the same turn on it; auth failures (`codex login` expired), turn timeouts and unknown-model errors do not fail over on this runtime and surface as the turn's error instead.
- **Sub-second cancellation isn't guaranteed.** Mid-stream interrupts (Ctrl+C while codex is responding) are sent via `turn/interrupt`, but if codex has already flushed the final message, you get the response anyway.
If you find a bug, [open an issue](https://github.com/NousResearch/hermes-agent/issues) with the output of `hermes logs --since 5m`. Mention `codex-runtime` in the title so it's easy to triage.