Skip to content

The agent that grows with you

chore: stack on #115759 to resolve agent/codex_runtime.py conflict

teknium1fddc8d1a0f52
diff --cc agent/codex_runtime.py
index 568c8973527,0c03bd6ef23..27c81c5280f
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@@ -406,18 -421,16 +421,24 @@@ def _ensure_codex_session(agent) -> Non
# _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,
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 agent/transports/codex_app_server_session.py
index e82f9453d6f,80d1caa70ce..e0440d858b3
--- a/agent/transports/codex_app_server_session.py
+++ b/agent/transports/codex_app_server_session.py
@@@ -156,15 -156,16 +156,21 @@@ class CodexAppServerSession
on_event: Optional[Callable[[dict], None]] = None,
request_routing: Optional[_ServerRequestRouting] = None,
client_factory: Optional[Callable[..., CodexAppServerClient]] = None,
+ model: Optional[str] = None, model_provider: Optional[str] = None,
+ developer_instructions: Optional[str] = None,
) -> None:
self._cwd = cwd or os.getcwd()
self._codex_bin = codex_bin
self._codex_home = codex_home
+ # ``thread/start.model`` / ``.modelProvider``: select a provider from codex's own
+ # ``[model_providers.<id>]`` table. Only the id travels; codex reads base_url/env_key itself.
+ self._model = (model or "").strip() or None
+ self._model_provider = (model_provider or "").strip() or None
+ # Hermes' composed system prompt (SOUL.md, memory, channel overrides). Sent ONCE per thread as
+ # ``thread/start.developerInstructions``: codex keeps its own base instructions (tool guidance) and
+ # inserts this as the first developer message of every model request. ``baseInstructions`` would
+ # REPLACE codex's base and ``instructions`` is accepted but ignored (verified against codex 0.147).
+ self._developer_instructions = developer_instructions
self._permission_profile = permission_profile or _HERMES_TO_CODEX_PERMISSION_PROFILE.get(
os.environ.get("HERMES_TERMINAL_SECURITY_MODE", "auto"), "workspace-write"
)
@@@ -192,11 -193,11 +198,15 @@@
self._client.initialize(client_name="hermes", client_title="Hermes Agent", client_version=_get_hermes_version())
# Permissions are NOT sent on thread/start: codex gates ``thread/start.permissions``
# behind experimentalApi + a matching ``[permissions]`` table in ~/.codex/config.toml.
- params: dict[str, Any] = {"cwd": self._cwd}
+ # Hermes supplies the agent identity through its own system prompt; ``personality: "none"`` strips
+ # codex's built-in "# Personality" section from the base instructions so it cannot compete (#72104).
+ params: dict[str, Any] = {"cwd": self._cwd, "personality": "none"}
+ if self._developer_instructions and self._developer_instructions.strip():
+ params["developerInstructions"] = self._developer_instructions
+ if self._model_provider:
+ params["modelProvider"] = self._model_provider
+ if self._model:
+ params["model"] = self._model
result = self._client.request("thread/start", params, timeout=15)
# Different codex versions serialize the id under thread.id / sessionId / threadId.
thread_obj = result.get("thread") or {}
diff --cc tests/agent/transports/test_codex_app_server_session.py
index f64dc82e404,a3a494a8485..e642ccf4cd6
--- a/tests/agent/transports/test_codex_app_server_session.py
+++ b/tests/agent/transports/test_codex_app_server_session.py
@@@ -165,46 -164,24 +165,54 @@@ class TestLifecycle
method_calls = [m for (m, _) in client.requests if m == "thread/start"]
assert len(method_calls) == 1
- def test_thread_start_passes_cwd_only(self):
- """thread/start carries cwd. We intentionally do NOT pass `permissions`
- on this codex version (experimentalApi-gated + requires matching
- config.toml [permissions] table). Letting codex use its default
- (read-only unless user configures otherwise) is the documented path."""
+ def test_thread_start_carries_hermes_prompt_and_disables_codex_personality(self):
+ """thread/start carries cwd, Hermes' composed prompt as developerInstructions and
+ personality "none" (#74712, #72104, #26035). We intentionally do NOT pass `permissions`
+ (experimentalApi-gated + requires a matching config.toml [permissions] table)."""
client = FakeClient()
- s = make_session(client, permission_profile="workspace-write")
+ s = make_session(client, permission_profile="workspace-write", developer_instructions="SOUL: be terse")
s.ensure_started()
method, params = next(r for r in client.requests if r[0] == "thread/start")
- assert params["cwd"] == "/tmp"
- assert "permissions" not in params # see session.ensure_started() comment
+ assert params == {"cwd": "/tmp", "developerInstructions": "SOUL: be terse", "personality": "none"}
+
+ def test_thread_start_omits_developer_instructions_when_prompt_empty(self):
+ """No prompt (or a blank one) never sends an empty developerInstructions field."""
+ client = FakeClient()
+ make_session(client, developer_instructions=" ").ensure_started()
+ method, params = next(r for r in client.requests if r[0] == "thread/start")
+ assert "developerInstructions" not in params
+ assert params["personality"] == "none"
+ def test_named_custom_provider_selects_codex_model_provider(self, monkeypatch):
+ """#75186: for ``provider=custom`` + a configured ``providers.<name>`` entry, the session built by
+ ``_ensure_codex_session`` sends ``model`` + ``modelProvider=<name>`` on thread/start and never the
+ API key; openai/openai-codex agents keep codex's defaults (cwd only)."""
+ import hermes_cli.runtime_provider as rp
+ from agent.codex_runtime import _ensure_codex_session
+ from agent.transports import codex_app_server_session as sess_mod
+ monkeypatch.setattr(rp, "load_config", lambda: {
+ "providers": {"my-gateway": {"api": "https://gateway.example.com/v1", "api_key": "sk-secret"}}})
+ clients: list[FakeClient] = []
+
+ def build(**kw):
+ clients.append(FakeClient())
+ return CodexAppServerSession(**{**kw, "client_factory": lambda **_: clients[-1]})
+ monkeypatch.setattr(sess_mod, "CodexAppServerSession", build)
+
+ def thread_start_params(**agent_attrs):
+ agent = SimpleNamespace(_codex_session=None, session_cwd="/tmp", api_key="sk-secret", **agent_attrs)
+ _ensure_codex_session(agent)
+ agent._codex_session.ensure_started()
+ return next(p for (m, p) in clients[-1].requests if m == "thread/start")
+
+ named = thread_start_params(provider="custom", requested_provider="custom:my-gateway", model="gpt-5.4")
- assert named == {"cwd": "/tmp", "modelProvider": "my-gateway", "model": "gpt-5.4"}
++ # ``personality: "none"`` rides on every thread/start (#72104); only the provider selection varies.
++ base = {"cwd": "/tmp", "personality": "none"}
++ assert named == {**base, "modelProvider": "my-gateway", "model": "gpt-5.4"}
+ assert "sk-secret" not in repr(named)
- assert thread_start_params(provider="openai-codex", requested_provider="openai-codex", model="gpt-5.4") == {"cwd": "/tmp"}
- assert thread_start_params(provider="custom", requested_provider="custom", model="gpt-5.4") == {"cwd": "/tmp"}
++ assert thread_start_params(provider="openai-codex", requested_provider="openai-codex", model="gpt-5.4") == base
++ assert thread_start_params(provider="custom", requested_provider="custom", model="gpt-5.4") == base
+
def test_close_idempotent(self):
client = FakeClient()
s = make_session(client)