Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(voice): stream subscription turns through app server

rubenluetke10-beep11f048138813
diff --git a/jarvis/codex_app_server.py b/jarvis/codex_app_server.py
index 985fd7ca3f..babde42c7b 100644
--- a/jarvis/codex_app_server.py
+++ b/jarvis/codex_app_server.py
@@ -62,6 +62,7 @@ _STUN_MEDIA_PATH_TTL_S: Final = 3600.0
_SHUTDOWN_TIMEOUT_S: Final = 2.0
_DEFAULT_SDP_TIMEOUT_S: Final = 15.0
_DEFAULT_NOTIFICATION_QUEUE_SIZE: Final = 512
+CodexAppServerPurpose = Literal["realtime", "text"]
_MAX_REALTIME_START_PROMPT_BYTES: Final = 64_000
_MAX_REALTIME_INITIAL_ITEMS: Final = 128
_MAX_REALTIME_INITIAL_TEXT_BYTES: Final = 32_768
@@ -312,6 +313,17 @@ _TRANSPORT_DEVELOPER_INSTRUCTIONS: Final = (
"plugins, skills, web search, MCP servers, or other agents. Do not read or "
"write the filesystem. Yield all realtime handoffs to the client."
)
+_TEXT_BASE_INSTRUCTIONS: Final = (
+ "This ephemeral thread serves a conversational voice assistant. Answer the "
+ "user directly in plain text. Never use tools, inspect files, access the "
+ "workspace, run commands, browse, or perform actions."
+)
+_TEXT_DEVELOPER_INSTRUCTIONS: Final = (
+ "Conversational text-only boundary: return only the assistant reply. Do not "
+ "call tools, shell commands, applications, plugins, skills, web search, MCP "
+ "servers, or other agents. Do not read or write the filesystem."
+)
+_TEXT_MODEL_PROVIDER: Final = "openai"
_OFFICIAL_OPENAI_API_BASE: Final = "https://api.openai.com/v1"
_OFFICIAL_OPENAI_REALTIME_BASE: Final = "https://api.openai.com/v1"
@@ -1866,14 +1878,19 @@ def _subscription_environment(
return environment
-def _create_safe_transport_workspace() -> _SafeTransportWorkspace:
+def _create_safe_transport_workspace(
+ purpose: CodexAppServerPurpose = "realtime",
+) -> _SafeTransportWorkspace:
from jarvis.core.private_directory import ( # noqa: PLC0415
ensure_owner_only_directory,
)
try:
temporary_root = Path(tempfile.gettempdir()).resolve(strict=True)
- root = temporary_root / f"jarvis-codex-voice-{secrets.token_hex(16)}"
+ workspace_label = "voice" if purpose == "realtime" else "text"
+ root = temporary_root / (
+ f"jarvis-codex-{workspace_label}-{secrets.token_hex(16)}"
+ )
ensure_owner_only_directory(root, create=True)
root = root.resolve(strict=True)
if root.parent != temporary_root:
@@ -1892,8 +1909,13 @@ def _create_safe_transport_workspace() -> _SafeTransportWorkspace:
child_local_appdata = root / "local-appdata"
child_tmp = root / "tmp"
try:
- instructions.write_text(_TRANSPORT_BASE_INSTRUCTIONS, encoding="utf-8")
- compact_prompt.write_text(_TRANSPORT_BASE_INSTRUCTIONS, encoding="utf-8")
+ instruction_floor = (
+ _TEXT_BASE_INSTRUCTIONS
+ if purpose == "text"
+ else _TRANSPORT_BASE_INSTRUCTIONS
+ )
+ instructions.write_text(instruction_floor, encoding="utf-8")
+ compact_prompt.write_text(instruction_floor, encoding="utf-8")
for directory in (
sqlite_home,
log_dir,
@@ -1918,7 +1940,7 @@ def _create_safe_transport_workspace() -> _SafeTransportWorkspace:
"priority": 0,
"availability_nux": None,
"upgrade": None,
- "base_instructions": _TRANSPORT_BASE_INSTRUCTIONS,
+ "base_instructions": instruction_floor,
"model_messages": None,
"support_verbosity": False,
"default_verbosity": None,
@@ -2287,8 +2309,13 @@ class CodexAppServerClient:
self,
binary_path: str | None = None,
request_timeout_s: float = _DEFAULT_REQUEST_TIMEOUT_S,
+ *,
+ purpose: CodexAppServerPurpose = "realtime",
) -> None:
+ if purpose not in ("realtime", "text"):
+ raise ValueError("purpose must be 'realtime' or 'text'")
self._binary_path = (binary_path or "").strip() or None
+ self._purpose: CodexAppServerPurpose = purpose
self._request_timeout_s = max(0.1, float(request_timeout_s))
self._child_environment: dict[str, str] = {}
self._process: asyncio.subprocess.Process | None = None
@@ -2559,13 +2586,14 @@ class CodexAppServerClient:
)
if self._workspace is None:
self._workspace = await asyncio.to_thread(
- _create_safe_transport_workspace
+ _create_safe_transport_workspace, self._purpose
)
self._child_environment = _subscription_environment(
codex_home,
self._workspace,
)
- await self._ensure_sink_started()
+ if self._purpose == "realtime":
+ await self._ensure_sink_started()
_stamp("home_workspace_sink")
await asyncio.to_thread(
_verify_spawn_binary, capability.binary_path
@@ -2642,7 +2670,7 @@ class CodexAppServerClient:
"version": __version__,
},
"capabilities": {
- "experimentalApi": True,
+ "experimentalApi": self._purpose == "realtime",
"mcpServerOpenaiFormElicitation": False,
"requestAttestation": False,
},
@@ -2888,16 +2916,6 @@ class CodexAppServerClient:
("approval_policy",): "never",
("cli_auth_credentials_store",): "file",
("chatgpt_base_url",): _OFFICIAL_CHATGPT_BASE,
- ("experimental_realtime_webrtc_call_base_url",): (
- _OFFICIAL_CHATGPT_CODEX_BASE
- ),
- ("experimental_realtime_start_instructions",): "",
- ("experimental_realtime_ws_backend_prompt",): (
- ""
- ),
- ("experimental_realtime_ws_base_url",): (
- _OFFICIAL_OPENAI_REALTIME_BASE
- ),
("history", "persistence"): "none",
("include_apps_instructions",): False,
("include_collaboration_mode_instructions",): False,
@@ -2906,10 +2924,7 @@ class CodexAppServerClient:
("memories", "dedicated_tools"): False,
("memories", "generate_memories"): False,
("memories", "use_memories"): False,
- ("model_provider",): self._transport_provider_id,
- ("experimental_compact_prompt_file",): self._compact_prompt_file(),
("log_dir",): self._log_dir(),
- ("model_catalog_json",): self._model_catalog_file(),
("model_instructions_file",): self._safe_instructions_file(),
("notify",): [],
("openai_base_url",): _OFFICIAL_OPENAI_API_BASE,
@@ -2928,20 +2943,45 @@ class CodexAppServerClient:
}
for feature in _DISABLED_APP_SERVER_FEATURES:
expected_paths[("features", feature)] = False
- expected_paths[("features", "realtime_conversation")] = True
- provider_prefix = ("model_providers", self._transport_provider_id)
- expected_paths.update(
- {
- (*provider_prefix, "base_url"): self._provider_sink_base_url(),
- (*provider_prefix, "name"): "OpenAI ChatGPT subscription voice",
- (*provider_prefix, "requires_openai_auth"): True,
- (*provider_prefix, "request_max_retries"): 0,
- (*provider_prefix, "supports_standalone_web_search"): False,
- (*provider_prefix, "supports_websockets"): False,
- (*provider_prefix, "stream_max_retries"): 0,
- (*provider_prefix, "wire_api"): "responses",
- }
- )
+ if self._purpose == "realtime":
+ expected_paths.update(
+ {
+ ("experimental_realtime_webrtc_call_base_url",): (
+ _OFFICIAL_CHATGPT_CODEX_BASE
+ ),
+ ("experimental_realtime_start_instructions",): "",
+ ("experimental_realtime_ws_backend_prompt",): "",
+ ("experimental_realtime_ws_base_url",): (
+ _OFFICIAL_OPENAI_REALTIME_BASE
+ ),
+ ("model_provider",): self._transport_provider_id,
+ ("experimental_compact_prompt_file",): (
+ self._compact_prompt_file()
+ ),
+ ("model_catalog_json",): self._model_catalog_file(),
+ ("features", "realtime_conversation"): True,
+ }
+ )
+ provider_prefix = ("model_providers", self._transport_provider_id)
+ expected_paths.update(
+ {
+ (*provider_prefix, "base_url"): self._provider_sink_base_url(),
+ (*provider_prefix, "name"): "OpenAI ChatGPT subscription voice",
+ (*provider_prefix, "requires_openai_auth"): True,
+ (*provider_prefix, "request_max_retries"): 0,
+ (*provider_prefix, "supports_standalone_web_search"): False,
+ (*provider_prefix, "supports_websockets"): False,
+ (*provider_prefix, "stream_max_retries"): 0,
+ (*provider_prefix, "wire_api"): "responses",
+ }
+ )
+ else:
+ expected_paths.update(
+ {
+ ("model_provider",): _TEXT_MODEL_PROVIDER,
+ ("features", "realtime_conversation"): False,
+ }
+ )
if sys.platform == "win32":
expected_paths[("windows", "sandbox")] = "unelevated"
for path, expected in expected_paths.items():
@@ -3027,7 +3067,11 @@ class CodexAppServerClient:
public_expected = {
"analytics": {"enabled": False},
"approval_policy": "never",
- "model_provider": self._transport_provider_id,
+ "model_provider": (
+ self._transport_provider_id
+ if self._purpose == "realtime"
+ else _TEXT_MODEL_PROVIDER
+ ),
"sandbox_mode": "read-only",
}
for key, expected in public_expected.items():
@@ -3040,13 +3084,13 @@ class CodexAppServerClient:
self,
binary_path: str,
) -> None:
- command = [
- binary_path,
- "app-server",
- "--strict-config",
- "--enable",
- "realtime_conversation",
- ]
+ command = [binary_path, "app-server", "--strict-config"]
+ command.extend(
+ (
+ "--enable" if self._purpose == "realtime" else "--disable",
+ "realtime_conversation",
+ )
+ )
for feature in _DISABLED_APP_SERVER_FEATURES:
command.extend(("--disable", feature))
command.extend(
@@ -3058,23 +3102,6 @@ class CodexAppServerClient:
"-c",
'chatgpt_base_url="https://chatgpt.com/backend-api/"',
"-c",
- (
- 'experimental_realtime_ws_base_url='
- '"https://api.openai.com/v1"'
- ),
- "-c",
- (
- 'experimental_realtime_webrtc_call_base_url='
- '"https://chatgpt.com/backend-api/codex"'
- ),
- "-c",
- 'experimental_realtime_start_instructions=""',
- "-c",
- (
- "experimental_realtime_ws_backend_prompt="
- '""'
- ),
- "-c",
"tools.web_search=false",
"-c",
'history.persistence="none"',
@@ -3106,15 +3133,8 @@ class CodexAppServerClient:
f"{json.dumps(self._safe_instructions_file())}"
),
"-c",
- (
- "experimental_compact_prompt_file="
- f"{json.dumps(self._compact_prompt_file())}"
- ),
- "-c",
f"log_dir={json.dumps(self._log_dir())}",
"-c",
- f"model_catalog_json={json.dumps(self._model_catalog_file())}",
- "-c",
f"sqlite_home={json.dumps(self._sqlite_home())}",
"-c",
"analytics.enabled=false",
@@ -3136,9 +3156,28 @@ class CodexAppServerClient:
'sandbox_mode="read-only"',
)
)
- provider = f"model_providers.{self._transport_provider_id}"
- command.extend(
- (
+ if self._purpose == "realtime":
+ command.extend(
+ (
+ "-c",
+ 'experimental_realtime_ws_base_url="https://api.openai.com/v1"',
+ "-c",
+ "experimental_realtime_webrtc_call_base_url="
+ '"https://chatgpt.com/backend-api/codex"',
+ "-c",
+ 'experimental_realtime_start_instructions=""',
+ "-c",
+ 'experimental_realtime_ws_backend_prompt=""',
+ "-c",
+ "experimental_compact_prompt_file="
+ f"{json.dumps(self._compact_prompt_file())}",
+ "-c",
+ f"model_catalog_json={json.dumps(self._model_catalog_file())}",
+ )
+ )
+ provider = f"model_providers.{self._transport_provider_id}"
+ command.extend(
+ (
"-c",
f'{provider}.name="OpenAI ChatGPT subscription voice"',
"-c",
@@ -3157,8 +3196,10 @@ class CodexAppServerClient:
f"{provider}.stream_max_retries=0",
"-c",
f'model_provider="{self._transport_provider_id}"',
+ )
)
- )
+ else:
+ command.extend(("-c", f'model_provider="{_TEXT_MODEL_PROVIDER}"'))
if sys.platform == "win32":
command.extend(("-c", 'windows.sandbox="unelevated"'))
kwargs: dict[str, Any] = {
@@ -3594,6 +3635,193 @@ class CodexAppServerClient:
"Codex returned an unsafe or unverifiable voice thread boundary."
)
+ def _audit_text_thread_start_response(
+ self,
+ result: Mapping[str, Any],
+ *,
+ expected_model: str | None,
+ ) -> None:
+ """Verify the stable text thread kept the subscription safety floor."""
+ thread = result.get("thread")
+ sandbox = result.get("sandbox")
+ expected_cwd = os.path.normcase(os.path.abspath(self._safe_thread_cwd()))
+ response_cwd = result.get("cwd")
+ thread_cwd = thread.get("cwd") if isinstance(thread, Mapping) else None
+ response_model = result.get("model")
+ thread_model = thread.get("model") if isinstance(thread, Mapping) else None
+ model_ok = (
+ isinstance(response_model, str)
+ and bool(response_model)
+ and isinstance(thread_model, str)
+ and bool(thread_model)
+ )
+ if expected_model:
+ model_ok = (
+ model_ok
+ and response_model == expected_model
+ and thread_model == expected_model
+ )
+ if (
+ not isinstance(thread, Mapping)
+ or not isinstance(thread.get("id"), str)
+ or not thread["id"]
+ or thread.get("ephemeral") is not True
+ or thread.get("modelProvider") != _TEXT_MODEL_PROVIDER
+ or not isinstance(response_cwd, str)
+ or os.path.normcase(os.path.abspath(response_cwd)) != expected_cwd
+ or not isinstance(thread_cwd, str)
+ or os.path.normcase(os.path.abspath(thread_cwd)) != expected_cwd
+ or result.get("approvalPolicy") != "never"
+ or result.get("modelProvider") != _TEXT_MODEL_PROVIDER
+ or not model_ok
+ or sandbox != {"type": "readOnly", "networkAccess": False}
+ or result.get("instructionSources", []) != []
+ ):
+ raise CodexSubscriptionUnavailable(
+ "Codex returned an unsafe or unverifiable text thread boundary."
+ )
+
+ async def text_thread_start(
+ self,
+ *,
+ base_instructions: str | None = None,
+ developer_instructions: str | None = None,
+ model: str | None = None,
+ ) -> dict[str, Any]:
+ """Start one isolated stable App Server text thread for subscription voice."""
+ if self._purpose != "text":
+ raise CodexSubscriptionUnavailable(
+ "A realtime Codex transport cannot create text-generation threads."
+ )
+ selected_model = str(model or "").strip()
+ if selected_model and not re.fullmatch(
+ r"[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}", selected_model
+ ):
+ raise CodexSubscriptionUnavailable(
+ "Codex subscription model id contains unsupported characters."
+ )
+ await self.ensure_started()
+ session_config: dict[str, Any] = {
+ "analytics": {"enabled": False},
+ "cli_auth_credentials_store": "file",
+ "chatgpt_base_url": _OFFICIAL_CHATGPT_BASE,
+ "features": {
+ **{feature: False for feature in _DISABLED_APP_SERVER_FEATURES},
+ "realtime_conversation": False,
+ },
+ "history": {"persistence": "none"},
+ "include_apps_instructions": False,
+ "include_collaboration_mode_instructions": False,
+ "include_environment_context": False,
+ "include_permissions_instructions": False,
+ "memories": {
+ "dedicated_tools": False,
+ "generate_memories": False,
+ "use_memories": False,
+ },
+ "log_dir": self._log_dir(),
+ "model_instructions_file": self._safe_instructions_file(),
+ "model_provider": _TEXT_MODEL_PROVIDER,
+ "notify": [],
+ "openai_base_url": _OFFICIAL_OPENAI_API_BASE,
+ "orchestrator": {
+ "mcp": {"enabled": False},
+ "skills": {"enabled": False},
+ },
+ "otel": {
+ "exporter": "none",
+ "log_user_prompt": False,
+ "metrics_exporter": "none",
+ "trace_exporter": "none",
+ },
+ "project_doc_max_bytes": 0,
+ "sandbox_mode": "read-only",
+ "skills": {
+ "bundled": {"enabled": False},
+ "include_instructions": False,
+ },
+ "sqlite_home": self._sqlite_home(),
+ "tools": {"web_search": False},
+ }
+ params: dict[str, Any] = {
+ "allowProviderModelFallback": False,
+ "approvalPolicy": "never",
+ "baseInstructions": (
+ str(base_instructions or "").strip() or _TEXT_BASE_INSTRUCTIONS
+ ),
+ "config": session_config,
+ "cwd": self._safe_thread_cwd(),
+ "developerInstructions": (
+ str(developer_instructions or "").strip()
+ or _TEXT_DEVELOPER_INSTRUCTIONS
+ ),
+ "dynamicTools": None,
+ "environments": [],
+ "ephemeral": True,
+ "modelProvider": _TEXT_MODEL_PROVIDER,
+ "runtimeWorkspaceRoots": [],
+ "sandbox": "read-only",
+ "selectedCapabilityRoots": [],
+ }
+ if selected_model:
+ params["model"] = selected_model
+
+ async with self._thread_start_lock:
+ await self.ensure_started()
+ live_process = self._process
+ audit_is_fresh = (
+ live_process is not None
+ and self._startup_audit_process is live_process
+ )
+ self._startup_audit_process = None
+ try:
+ if not audit_is_fresh:
+ await asyncio.to_thread(
+ _validated_subscription_home,
+ create=False,
+ require_marker=True,
+ trusted_binary_path=self._trusted_binary_path,
+ )
+ await self._verify_live_chatgpt_account()
+ self._audit_config_requirements(
+ await self._read_config_requirements()
+ )
+ self._audit_effective_config(await self._read_effective_config())
+ except CodexSubscriptionUnavailable as exc:
+ if isinstance(exc, CodexSubscriptionPlanUnsupported):
+ await asyncio.shield(
+ asyncio.to_thread(
+ set_codex_subscription_activation_block, str(exc)
+ )
+ )
+ error = type(exc)(str(exc))
+ await self._close_process(
+ CodexAppServerDisconnected(
+ "Codex app-server safety state changed."
+ ),
+ expected=False,
+ )
+ raise error from None
+ result = _result_dict(
+ "thread/start",
+ await self._request_live(
+ "thread/start", params, timeout_s=self._request_timeout_s
+ ),
+ )
+ try:
+ self._audit_text_thread_start_response(
+ result, expected_model=selected_model or None
+ )
+ except CodexSubscriptionUnavailable:
+ await self._close_process(
+ CodexAppServerDisconnected(
+ "Codex app-server returned an unsafe text thread boundary."
+ ),
+ expected=False,
+ )
+ raise
+ return result
+
async def thread_start(
self,
*,
@@ -3611,6 +3839,10 @@ class CodexAppServerClient:
ride_recent_audit: bool = False,
) -> dict[str, Any]:
"""Start a verified ChatGPT-only transport thread outside the workspace."""
+ if self._purpose != "realtime":
+ raise CodexSubscriptionUnavailable(
+ "A text Codex transport cannot create realtime voice threads."
+ )
# cwd/model/ephemeral stay caller-proof: the audit below pins them to
# the safe values, so a caller cannot widen the boundary. Instructions
# are different — they are the voice's identity, one-speaker rule and
@@ -4266,7 +4498,7 @@ class CodexAppServerClient:
# app-server child bound to it), so a per-loop client could only ever be a
# client that can never reserve the profile — the second loop got one and
# every call from it failed with "already owned by another client".
-_shared_clients: dict[str, CodexAppServerClient] = {}
+_shared_clients: dict[tuple[str, CodexAppServerPurpose], CodexAppServerClient] = {}
_shared_clients_lock = threading.Lock()
# Invariant: WORKER threads may hold this lock across filesystem work (the
# cross-process profile lock is acquired under it: mkdir, resolve, ACL
@@ -4644,10 +4876,13 @@ def _evict_shared_codex_app_server(client: CodexAppServerClient) -> None:
def get_shared_codex_app_server(
binary_path: str | None = None,
+ *,
+ purpose: CodexAppServerPurpose = "realtime",
) -> CodexAppServerClient:
- """Return the shared client for Jarvis's fixed subscription-voice identity.
+ """Return a shared client for Jarvis's fixed subscription identity.
- One client per binary, process-wide. A caller on a different event loop is
+ One client per binary and purpose, process-wide. The dedicated profile still
+ permits only one live child at a time. A caller on a different event loop is
refused HERE with an actionable message rather than being handed a client
that cannot reserve the single-owner profile and fails deep inside a call.
"""
@@ -4657,7 +4892,9 @@ def get_shared_codex_app_server(
raise CodexSubscriptionUnavailable(
"The shared Codex app-server must be acquired from its owning event loop."
) from exc
- key = (binary_path or "").strip() or "<default>"
+ if purpose not in ("realtime", "text"):
+ raise ValueError("purpose must be 'realtime' or 'text'")
+ key = ((binary_path or "").strip() or "<default>", purpose)
with _shared_clients_lock:
client = _shared_clients.get(key)
if client is not None:
@@ -4678,7 +4915,7 @@ def get_shared_codex_app_server(
"Replacing a Codex app-server client whose owner loop is closed"
)
_shared_clients.pop(key, None)
- client = CodexAppServerClient(binary_path=binary_path)
+ client = CodexAppServerClient(binary_path=binary_path, purpose=purpose)
client._owner_loop = loop
_shared_clients[key] = client
return client
@@ -5330,6 +5567,7 @@ async def close_shared_codex_app_servers() -> None:
__all__ = [
+ "CodexAppServerPurpose",
"CodexAppServerCapability",
"CodexAppServerClient",
"CodexAppServerDisconnected",
diff --git a/jarvis/plugins/brain/codex.py b/jarvis/plugins/brain/codex.py
index 848ad521e4..a0762f43f0 100644
--- a/jarvis/plugins/brain/codex.py
+++ b/jarvis/plugins/brain/codex.py
@@ -4,12 +4,10 @@ Codex can back the conversational *brain* two ways:
* **API key** (``codex_openai_api_key`` / ``openai_api_key``): the fast, cheap
path — a normal OpenAI chat-completions stream via ``_openai_base``.
-* **ChatGPT login (OAuth)**: when no API key is configured but ``codex login``
- has stored a ChatGPT subscription token, we drive the ``codex`` CLI
- (``codex exec``) directly — no per-call billing. This is SLOW (the codex agent
- spins up per turn, ~15-20 s, and burns many tokens) and tool-limited, so it is
- a deliberate fallback the user opts into, not the default. A chat-completions
- endpoint genuinely cannot run on the subscription; the CLI is the only bridge.
+* **ChatGPT login (OAuth)**: the explicit subscription voice profile uses the
+ persistent ``codex app-server`` stable text protocol and streams assistant
+ deltas. The ordinary Codex OAuth fallback retains ``codex exec`` for
+ compatibility with the user's non-isolated CLI profile.
The CLI path runs ``codex exec`` in a throwaway temp dir with ``--sandbox
read-only`` (no writes, no dangerous commands), a light "answer conversationally"
@@ -28,7 +26,7 @@ import shutil
import sys
import tempfile
import time
-from collections.abc import AsyncIterator
+from collections.abc import AsyncIterator, Mapping
from contextlib import suppress
from typing import Any
@@ -59,6 +57,49 @@ _CLI_TIMEOUT_S: float = 90.0
_CLI_MODEL_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$")
+def _app_server_id(value: object, key: str) -> str:
+ if not isinstance(value, Mapping):
+ return ""
+ direct = value.get(key)
+ if isinstance(direct, str):
+ return direct
+ nested_name = "turn" if key == "turnId" else "thread"
+ nested = value.get(nested_name)
+ if isinstance(nested, Mapping) and isinstance(nested.get("id"), str):
+ return str(nested["id"])
+ return ""
+
+
+def _agent_message_text(params: Mapping[str, Any]) -> str:
+ item = params.get("item")
+ if not isinstance(item, Mapping):
+ return ""
+ item_type = str(item.get("type") or "").replace("_", "").lower()
+ if item_type != "agentmessage":
+ return ""
+ text = item.get("text")
+ if isinstance(text, str):
+ return text
+ content = item.get("content")
+ if not isinstance(content, list):
+ return ""
+ parts: list[str] = []
+ for block in content:
+ if isinstance(block, Mapping) and isinstance(block.get("text"), str):
+ parts.append(str(block["text"]))
+ return "".join(parts)
+
+
+def _completed_turn_status(params: Mapping[str, Any]) -> str:
+ turn = params.get("turn")
+ status: object = turn.get("status") if isinstance(turn, Mapping) else None
+ if isinstance(status, Mapping):
+ status = status.get("type")
+ if not isinstance(status, str):
+ status = params.get("status")
+ return str(status or "").strip().lower()
+
+
def _resolve_codex_binary() -> str | None:
"""On-PATH ``codex`` binary (Windows shim variants included), or None.
@@ -222,7 +263,7 @@ def _build_cli_command(binary: str, model: str | None) -> list[str]:
# ``--sandbox read-only`` so it physically cannot write files or run risky
# commands even if it tries.
_CLI_SYSTEM = (
- "You are Jarvis, a concise and friendly voice assistant. Answer the user's "
+ "You are the user's concise and friendly voice assistant. Answer the user's "
"message directly in one to three short sentences. Reply in plain text only "
"— do not run any commands, do not read or edit files, do not use tools."
)
@@ -356,6 +397,120 @@ class CodexBrain:
return render_structured_prompt(req)
return _build_cli_prompt(req)
+ async def _complete_via_app_server(
+ self, req: BrainRequest
+ ) -> AsyncIterator[BrainDelta]:
+ """Stream a ChatGPT-subscription turn over the stable App Server API."""
+ from jarvis.codex_app_server import ( # noqa: PLC0415
+ CodexAppServerDisconnected,
+ CodexAppServerTimeout,
+ get_shared_codex_app_server,
+ )
+
+ binary_path = (
+ str(getattr(getattr(cfg.load_config(), "codex", None), "binary_path", ""))
+ .strip()
+ or None
+ )
+ client = get_shared_codex_app_server(binary_path, purpose="text")
+ prompt = self._render_prompt(req)
+ started = await client.text_thread_start()
+ thread_id = _app_server_id(started, "threadId")
+ if not thread_id:
+ raise RuntimeError("Codex App Server returned no text thread id.")
+
+ subscription = client.subscribe(thread_id)
+ turn_id = ""
+ emitted = ""
+ authoritative = ""
+ finished = False
+ started_at = time.monotonic()
+ try:
+ turn_result = await client.turn_start(thread_id, prompt)
+ turn_id = _app_server_id(turn_result, "turnId")
+ deadline = started_at + self._cli_timeout_s
+ while not finished:
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ raise TimeoutError
+ notification = await subscription.get(timeout_s=remaining)
+ params = notification.params
+ event_turn_id = _app_server_id(params, "turnId")
+ if turn_id and event_turn_id and event_turn_id != turn_id:
+ continue
+ if notification.method == "turn/started":
+ turn_id = turn_id or event_turn_id
+ continue
+ if notification.method == "item/agentMessage/delta":
+ delta = params.get("delta")
+ if isinstance(delta, str) and delta:
+ emitted += delta
+ yield BrainDelta(content=delta)
+ continue
+ if notification.method == "item/completed":
+ final_text = _agent_message_text(params)
+ if not final_text:
+ continue
+ authoritative += final_text
+ if final_text.startswith(emitted):
+ suffix = final_text[len(emitted) :]
+ if suffix:
+ emitted += suffix
+ yield BrainDelta(content=suffix)
+ elif not emitted:
+ emitted = final_text
+ yield BrainDelta(content=final_text)
+ else:
+ log.warning(
+ "Codex App Server final text diverged from streamed deltas; "
+ "keeping the already delivered stream to avoid duplicate speech"
+ )
+ continue
+ if notification.method == "turn/completed":
+ status = _completed_turn_status(params)
+ if status and status not in {"completed", "interrupted"}:
+ raise RuntimeError(
+ f"Codex App Server text turn ended with status {status}."
+ )
+ if status == "interrupted":
+ raise RuntimeError("Codex App Server text turn was interrupted.")
+ finished = True
+ elif notification.method in {"error", "turn/failed"}:
+ raise RuntimeError("Codex App Server text turn failed.")
+ except asyncio.CancelledError:
+ if turn_id:
+ with suppress(Exception):
+ await asyncio.shield(client.turn_interrupt(thread_id, turn_id))
+ raise
+ except (TimeoutError, CodexAppServerTimeout) as exc:
+ if turn_id:
+ with suppress(Exception):
+ await asyncio.shield(client.turn_interrupt(thread_id, turn_id))
+ raise RuntimeError(
+ "Codex App Server did not finish the subscription turn within "
+ f"{self._cli_timeout_s:.0f}s."
+ ) from exc
+ finally:
+ subscription.close()
+ try:
+ await asyncio.shield(
+ asyncio.wait_for(client.thread_unsubscribe(thread_id), timeout=3.0)
+ )
+ except (TimeoutError, CodexAppServerDisconnected):
+ log.warning("Codex App Server text thread cleanup did not complete")
+ except Exception:
+ log.debug("Codex App Server text thread cleanup failed", exc_info=True)
+
+ answer = (authoritative or emitted).strip()
+ if not answer:
+ raise RuntimeError("Codex App Server returned no subscription answer.")
+ log.info(
+ "Codex App Server text turn ok: %d chars in %.1fs via ChatGPT login",
+ len(answer),
+ time.monotonic() - started_at,
+ )
+ yield BrainDelta(finish_reason="stop")
+
async def _complete_via_cli(self, req: BrainRequest) -> AsyncIterator[BrainDelta]:
"""Drive ``codex exec`` over the ChatGPT login and stream its answer.
@@ -544,17 +699,12 @@ class CodexBrain:
raise RuntimeError(
"Codex ChatGPT subscription mode cannot execute brain tools."
)
- oauth = await asyncio.to_thread(_codex_oauth_connected)
- if not oauth:
- raise RuntimeError(
- "Codex ChatGPT subscription login is not connected."
- )
log.info(
"CodexBrain.complete: explicit ChatGPT-subscription path "
"(model=%s)",
self._model,
)
- async for delta in self._complete_via_cli(req):
+ async for delta in self._complete_via_app_server(req):
yield delta
return
diff --git a/tests/unit/codex_app_server/test_transport.py b/tests/unit/codex_app_server/test_transport.py
index 0624543e35..f032d8c861 100644
--- a/tests/unit/codex_app_server/test_transport.py
+++ b/tests/unit/codex_app_server/test_transport.py
@@ -215,6 +215,55 @@ def _default_responder(process: FakeProcess, message: dict[str, Any]) -> None:
_respond(process, request_id, {})
+async def test_text_transport_uses_stable_app_server_without_realtime_flags(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ harness = SpawnHarness(monkeypatch)
+ client = CodexAppServerClient(purpose="text")
+
+ await client.ensure_started()
+
+ argv, _kwargs = harness.calls[0]
+ argv_text = "\n".join(str(value) for value in argv)
+ assert "--disable\nrealtime_conversation" in argv_text
+ assert "--enable\nrealtime_conversation" not in argv_text
+ assert "experimental_realtime_" not in argv_text
+ assert f'model_provider="{transport._TEXT_MODEL_PROVIDER}"' in argv
+ assert client._sink_server is None
+ initialize = harness.processes[0].stdin.messages[0]
+ assert initialize["params"]["capabilities"]["experimentalApi"] is False
+
+ await client.close()
+
+
+async def test_text_thread_is_ephemeral_read_only_and_uses_managed_model(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ harness = SpawnHarness(monkeypatch)
+ client = CodexAppServerClient(purpose="text")
+ monkeypatch.setattr(client, "_audit_text_thread_start_response", lambda *_a, **_k: None)
+
+ result = await client.text_thread_start()
+
+ assert result == {"thread": {"id": "thread-1"}}
+ params = next(
+ message["params"]
+ for message in harness.processes[0].stdin.messages
+ if message.get("method") == "thread/start"
+ )
+ assert params["modelProvider"] == transport._TEXT_MODEL_PROVIDER
+ assert "model" not in params
+ assert params["ephemeral"] is True
+ assert params["sandbox"] == "read-only"
+ assert params["dynamicTools"] is None
+ assert params["runtimeWorkspaceRoots"] == []
+ assert params["config"]["features"]["realtime_conversation"] is False
+
+ await client.close()
+
+
class SpawnHarness:
def __init__(
self,
diff --git a/tests/unit/plugins/brain/test_codex_brain.py b/tests/unit/plugins/brain/test_codex_brain.py
index 85c7a5c193..1342a0fdfd 100644
--- a/tests/unit/plugins/brain/test_codex_brain.py
+++ b/tests/unit/plugins/brain/test_codex_brain.py
@@ -9,7 +9,9 @@ unfulfillable by instruction. These tests pin both fixes.
"""
from __future__ import annotations
+import asyncio
from collections.abc import AsyncIterator
+from types import SimpleNamespace
from typing import Any
import pytest
@@ -80,7 +82,13 @@ def _arm_api_and_oauth(
yield BrainDelta(content="cli-answer")
yield BrainDelta(finish_reason="stop")
+ async def _fake_app_server(self: CodexBrain, req: BrainRequest):
+ calls.append("app-server")
+ yield BrainDelta(content="app-server-answer")
+ yield BrainDelta(finish_reason="stop")
+
monkeypatch.setattr(CodexBrain, "_complete_via_cli", _fake_cli)
+ monkeypatch.setattr(CodexBrain, "_complete_via_app_server", _fake_app_server)
return calls
@@ -107,8 +115,133 @@ async def test_explicit_subscription_choice_bypasses_a_stored_api_key(
answer = await _collect(brain.complete(_wiki_request()))
- assert answer == "cli-answer"
- assert calls == ["cli"]
+ assert answer == "app-server-answer"
+ assert calls == ["app-server"]
+
+
+class _TextSubscription:
+ def __init__(self, notifications: list[object]) -> None:
+ self._notifications = list(notifications)
+ self.closed = False
+ self.waiting = asyncio.Event()
+
+ async def get(self, timeout_s: float | None = None) -> object:
+ del timeout_s
+ if self._notifications:
+ return self._notifications.pop(0)
+ self.waiting.set()
+ await asyncio.Future()
+ raise AssertionError("unreachable")
+
+ def close(self) -> None:
+ self.closed = True
+
+
+class _TextClient:
+ def __init__(self, notifications: list[object]) -> None:
+ self.subscription = _TextSubscription(notifications)
+ self.interrupts: list[tuple[str, str]] = []
+ self.unsubscribed: list[str] = []
+
+ async def text_thread_start(self) -> dict[str, object]:
+ return {"thread": {"id": "thread-voice"}}
+
+ def subscribe(self, thread_id: str) -> _TextSubscription:
+ assert thread_id == "thread-voice"
+ return self.subscription
+
+ async def turn_start(self, thread_id: str, prompt: str) -> dict[str, object]:
+ assert thread_id == "thread-voice"
+ assert prompt
+ return {"turn": {"id": "turn-voice"}}
+
+ async def turn_interrupt(self, thread_id: str, turn_id: str) -> dict[str, object]:
+ self.interrupts.append((thread_id, turn_id))
+ return {}
+
+ async def thread_unsubscribe(self, thread_id: str) -> dict[str, object]:
+ self.unsubscribed.append(thread_id)
+ return {}
+
+
+def _notification(method: str, **params: object) -> object:
+ return SimpleNamespace(method=method, params=params)
+
+
+async def test_app_server_subscription_streams_deltas_without_repeating_final(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ client = _TextClient(
+ [
+ _notification(
+ "item/agentMessage/delta",
+ threadId="thread-voice",
+ turnId="turn-voice",
+ delta="Hello ",
+ ),
+ _notification(
+ "item/agentMessage/delta",
+ threadId="thread-voice",
+ turnId="turn-voice",
+ delta="there.",
+ ),
+ _notification(
+ "item/completed",
+ threadId="thread-voice",
+ turnId="turn-voice",
+ item={"type": "agentMessage", "text": "Hello there."},
+ ),
+ _notification(
+ "turn/completed",
+ threadId="thread-voice",
+ turn={"id": "turn-voice", "status": "completed"},
+ ),
+ ]
+ )
+ monkeypatch.setattr(
+ "jarvis.codex_app_server.get_shared_codex_app_server",
+ lambda *_a, **_k: client,
+ )
+ brain = CodexBrain(prefer_subscription=True)
+
+ answer = await _collect(brain._complete_via_app_server(_wiki_request()))
+
+ assert answer == "Hello there."
+ assert client.unsubscribed == ["thread-voice"]
+
+
+async def test_app_server_subscription_interrupts_exact_turn_on_cancel(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ client = _TextClient(
+ [
+ _notification(
+ "item/agentMessage/delta",
+ threadId="thread-voice",
+ turnId="turn-voice",
+ delta="Starting",
+ )
+ ]
+ )
+ monkeypatch.setattr(
+ "jarvis.codex_app_server.get_shared_codex_app_server",
+ lambda *_a, **_k: client,
+ )
+ stream = CodexBrain(prefer_subscription=True)._complete_via_app_server(
+ _wiki_request()
+ )
+ assert (await stream.__anext__()).content == "Starting"
+ pending = asyncio.create_task(stream.__anext__())
+ await asyncio.wait_for(client.subscription.waiting.wait(), timeout=1.0)
+
+ pending.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await pending
+
+ assert client.interrupts == [("thread-voice", "turn-voice")]
+ assert client.unsubscribed == ["thread-voice"]
def test_subscription_cli_command_carries_the_selected_model() -> None: