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.

perf(realtime): overlap Codex session startup

rubenluetke10-beep8e5e0568e6cb
diff --git a/docs/codex-subscription-realtime-voice.md b/docs/codex-subscription-realtime-voice.md
index adff9ec092..a6fa29ab54 100644
--- a/docs/codex-subscription-realtime-voice.md
+++ b/docs/codex-subscription-realtime-voice.md
@@ -180,8 +180,10 @@ Codex 0.147 also disables its automatic delegation acknowledgement. All of
that configuration is therefore atomic with session creation: no synthetic
developer message can provoke a greeting or confirmation before the user's
first grounded utterance. The provider then applies the SDP answer and waits
-for media. It declares a 45-second handshake budget because a cold start has
-measured at 15-25 seconds.
+for media. WebRTC offer creation, the ephemeral Codex thread start, and local
+recognizer warm-up run concurrently; cleanup still unloads a thread if either
+parallel local task fails. The provider declares a 45-second handshake budget
+because a cold start has measured at 15-25 seconds.
`_CodexSubscriptionRealtimeSession.send_audio()` sends 24 kHz mono PCM through
the persistent, statefully resampled WebRTC track. Local energy-gated
diff --git a/jarvis/plugins/realtime/codex_subscription.py b/jarvis/plugins/realtime/codex_subscription.py
index aa6cbe6373..39d20047c4 100644
--- a/jarvis/plugins/realtime/codex_subscription.py
+++ b/jarvis/plugins/realtime/codex_subscription.py
@@ -495,6 +495,29 @@ def _consume_cleanup_task(task: asyncio.Task[Any]) -> None:
task.result()
+async def _cancel_background_task(task: asyncio.Task[Any] | None) -> None:
+ """Cancel and consume one locally owned startup task."""
+ if task is None:
+ return
+ if not task.done():
+ task.cancel()
+ with suppress(asyncio.CancelledError, Exception):
+ await task
+
+
+async def _warm_input_transcriber(transcriber: Any) -> None:
+ """Prime local STT concurrently with the independent transport startup."""
+ warm = getattr(transcriber, "warm", None)
+ if not callable(warm):
+ return
+ started_at = time.monotonic()
+ await warm()
+ log.info(
+ "RT-SPAWN span=transcriber_warm ms=%d",
+ int((time.monotonic() - started_at) * 1000.0),
+ )
+
+
async def _bounded_remote_call(
client: Any,
method_name: str,
@@ -2783,35 +2806,45 @@ class CodexSubscriptionRealtimeProvider:
# local state, and rebuilding + re-warming it on the STUN retry paid
# seconds for nothing.
input_transcriber = self._build_input_transcriber(cfg)
- for index, ice_factory in enumerate(attempts):
- try:
- session = await self._open_session_once(
- cfg,
- None if ice_factory is None else ice_factory(),
- input_transcriber=input_transcriber,
- # The STUN retry may ride the audit that passed seconds
- # ago on this very open; a first attempt never does.
- ride_recent_audit=index > 0,
- )
- if index > 0:
- # Postmortem marker: this call paid the full re-open (a
- # second thread_start bundle included) to reach STUN.
- session._diag["stun_retries"] = index
- log.info("RT-SPAWN stun_retry=%d", index)
- return session
- except transport_module.WebRtcMediaPathUnavailable as exc:
- last_error = exc
- if index + 1 < len(attempts):
- log.warning(
- "Realtime media path did not connect on host candidates "
- "(%s); retrying with a STUN server",
- exc,
+ warm_task: asyncio.Task[Any] | None = None
+ if callable(getattr(input_transcriber, "warm", None)):
+ warm_task = asyncio.create_task(
+ _warm_input_transcriber(input_transcriber),
+ name="codex-realtime-transcriber-warm",
+ )
+ try:
+ for index, ice_factory in enumerate(attempts):
+ try:
+ session = await self._open_session_once(
+ cfg,
+ None if ice_factory is None else ice_factory(),
+ input_transcriber=input_transcriber,
+ input_transcriber_warm_task=warm_task,
+ # The STUN retry may ride the audit that passed seconds
+ # ago on this very open; a first attempt never does.
+ ride_recent_audit=index > 0,
)
- continue
- raise
- raise RuntimeError( # pragma: no cover - the loop always returns or raises
- "Codex subscription realtime could not open a media path"
- ) from last_error
+ if index > 0:
+ # Postmortem marker: this call paid the full re-open (a
+ # second thread_start bundle included) to reach STUN.
+ session._diag["stun_retries"] = index
+ log.info("RT-SPAWN stun_retry=%d", index)
+ return session
+ except transport_module.WebRtcMediaPathUnavailable as exc:
+ last_error = exc
+ if index + 1 < len(attempts):
+ log.warning(
+ "Realtime media path did not connect on host candidates "
+ "(%s); retrying with a STUN server",
+ exc,
+ )
+ continue
+ raise
+ raise RuntimeError( # pragma: no cover - loop returns or raises
+ "Codex subscription realtime could not open a media path"
+ ) from last_error
+ finally:
+ await _cancel_background_task(warm_task)
def _build_input_transcriber(self, cfg: Any = None) -> Any:
"""Local user-speech recognition, or ``None`` when unavailable.
@@ -2858,44 +2891,50 @@ class CodexSubscriptionRealtimeProvider:
ice_servers: Any,
*,
input_transcriber: Any = None,
+ input_transcriber_warm_task: asyncio.Task[Any] | None = None,
ride_recent_audit: bool = False,
) -> _CodexSubscriptionRealtimeSession:
# Jarvis owns the media path in-process. The UI could only ever broker
# a signalling-shaped offer (no microphone), which ChatGPT-Live cannot
# use: on v3 the audio IS the WebRTC track.
- # RT-SPAWN spans: every step of this open is sequential today, so the
- # per-step numbers are what makes the latency budget (and any future
- # overlap) measurable instead of anecdotal.
- last_stamp = time.monotonic()
-
- def _stamp(name: str) -> None:
- nonlocal last_stamp
- now = time.monotonic()
+ # RT-SPAWN spans use each operation's own start so overlapping work
+ # stays measurable instead of producing misleading near-zero deltas.
+ def _stamp(name: str, started_at: float) -> None:
log.info(
- "RT-SPAWN span=%s ms=%d", name, int((now - last_stamp) * 1000.0)
+ "RT-SPAWN span=%s ms=%d",
+ name,
+ int((time.monotonic() - started_at) * 1000.0),
)
- last_stamp = now
audio_endpoint: Any = None
- if self._audio_endpoint_factory is not None:
- audio_endpoint = self._audio_endpoint_factory(ice_servers)
- else:
- transport_module = importlib.import_module("jarvis.realtime.webrtc_transport")
- audio_endpoint = transport_module.RealtimeWebRtcAudioEndpoint(ice_servers)
- offer_sdp = await audio_endpoint.create_offer()
- _stamp("offer_create")
-
client = self._client
- if client is None:
- app_server_module = importlib.import_module("jarvis.codex_app_server")
- client = app_server_module.get_shared_codex_app_server(self._binary_path)
-
+ offer_task: asyncio.Task[str] | None = None
subscription: Any = None
thread_id = ""
try:
+ if self._audio_endpoint_factory is not None:
+ audio_endpoint = self._audio_endpoint_factory(ice_servers)
+ else:
+ transport_module = importlib.import_module(
+ "jarvis.realtime.webrtc_transport"
+ )
+ audio_endpoint = transport_module.RealtimeWebRtcAudioEndpoint(
+ ice_servers
+ )
+ offer_started_at = time.monotonic()
+ offer_task = asyncio.create_task(
+ audio_endpoint.create_offer(), name="codex-realtime-create-offer"
+ )
+
+ if client is None:
+ app_server_module = importlib.import_module("jarvis.codex_app_server")
+ client = app_server_module.get_shared_codex_app_server(
+ self._binary_path
+ )
# ``thread_start`` lazily calls app-server ``ensure_started``. That
# performs the authoritative capability probe plus live
# ``account/read`` verification before accepting this thread.
+ thread_started_at = time.monotonic()
thread_result = await client.thread_start(
base_instructions=_THREAD_BASE_INSTRUCTIONS,
developer_instructions=_THREAD_DEVELOPER_INSTRUCTIONS,
@@ -2904,7 +2943,9 @@ class CodexSubscriptionRealtimeProvider:
thread_id = _thread_id_from_result(thread_result)
if not thread_id:
raise RuntimeError("Codex app-server did not return a thread id")
- _stamp("thread_start")
+ _stamp("thread_start", thread_started_at)
+ offer_sdp = await offer_task
+ _stamp("offer_create", offer_started_at)
subscription = client.subscribe(thread_id)
configured_model = str(getattr(cfg, "model", "") or "").strip()
@@ -2933,6 +2974,7 @@ class CodexSubscriptionRealtimeProvider:
),
)
initial_items = _history_initial_items(getattr(cfg, "history", ()))
+ realtime_started_at = time.monotonic()
start = await client.realtime_start(
thread_id,
output_modality="audio",
@@ -2953,12 +2995,13 @@ class CodexSubscriptionRealtimeProvider:
answer_sdp = str(getattr(start, "answer_sdp", "") or "").strip()
if not answer_sdp:
raise RuntimeError("Codex app-server did not return a WebRTC answer SDP")
- _stamp("realtime_start_sdp")
+ _stamp("realtime_start_sdp", realtime_started_at)
+ media_started_at = time.monotonic()
await audio_endpoint.apply_answer(answer_sdp)
# Fail here rather than mid-call: without a live media path the
# session would look connected and stay mute in both directions.
await audio_endpoint.wait_connected()
- _stamp("media_connect")
+ _stamp("media_connect", media_started_at)
session = _CodexSubscriptionRealtimeSession(
client=client,
subscription=subscription,
@@ -2974,16 +3017,16 @@ class CodexSubscriptionRealtimeProvider:
voice=voice,
initial_context=initial_context,
)
- # Build and prime the recognizer here, not inside the first
- # utterance: a local engine costs seconds to construct, and paying
- # that while the user is already speaking delays the first
- # transcript past its own turn. Idempotent and never raises.
- warm = getattr(session._input_transcriber, "warm", None)
- if callable(warm):
- await warm()
- _stamp("transcriber_warm")
+ # The recognizer was primed alongside offer/thread startup. Wait
+ # for that independent work before exposing the session so the
+ # first utterance never pays its cold-start cost.
+ if input_transcriber_warm_task is not None:
+ await input_transcriber_warm_task
+ else:
+ await _warm_input_transcriber(session._input_transcriber)
return session
except BaseException:
+ await _cancel_background_task(offer_task)
try:
await _cleanup_remote_thread(client, thread_id)
finally:
@@ -2991,7 +3034,8 @@ class CodexSubscriptionRealtimeProvider:
if subscription is not None:
await _close_subscription(subscription)
finally:
- await audio_endpoint.close()
+ if audio_endpoint is not None:
+ await audio_endpoint.close()
raise
diff --git a/tests/unit/realtime/test_codex_subscription.py b/tests/unit/realtime/test_codex_subscription.py
index 55eb39411d..ac9ca452fa 100644
--- a/tests/unit/realtime/test_codex_subscription.py
+++ b/tests/unit/realtime/test_codex_subscription.py
@@ -1237,9 +1237,8 @@ async def test_provider_error_and_app_server_death_are_normalized() -> None:
@pytest.mark.asyncio
-async def test_media_endpoint_failure_fails_before_app_server_launch() -> None:
- """No media path means no call: ChatGPT-Live audio IS the WebRTC track, so
- failing early beats a session that looks connected and stays mute."""
+async def test_media_endpoint_failure_cleans_parallel_thread_before_realtime() -> None:
+ """A failed local offer must not leave the parallel thread start behind."""
client = _Client()
class _BrokenEndpoint(_FakeAudioEndpoint):
@@ -1251,8 +1250,55 @@ async def test_media_endpoint_failure_fails_before_app_server_launch() -> None:
with pytest.raises(RuntimeError, match="no local WebRTC endpoint"):
await provider.open_session(RealtimeSessionConfig())
- assert client.capability_calls == 0
- assert client.thread_starts == []
+ assert len(client.thread_starts) == 1
+ assert client.realtime_starts == []
+ assert client.stops == ["thread-1"]
+ assert client.unsubscribes == ["thread-1"]
+ assert provider.test_endpoint.closed is True
+
+
+async def test_offer_thread_and_transcriber_warm_overlap() -> None:
+ """Independent cold-start work runs concurrently, without timing guesses."""
+ offer_started = asyncio.Event()
+ warm_started = asyncio.Event()
+ release_parallel_work = asyncio.Event()
+
+ class _GatedEndpoint(_FakeAudioEndpoint):
+ async def create_offer(self) -> str:
+ offer_started.set()
+ await release_parallel_work.wait()
+ return await super().create_offer()
+
+ class _GatedTranscriber:
+ async def warm(self) -> bool:
+ warm_started.set()
+ await release_parallel_work.wait()
+ return True
+
+ async def close(self) -> None:
+ return None
+
+ class _GatedClient(_Client):
+ async def thread_start(self, **kwargs):
+ await asyncio.gather(offer_started.wait(), warm_started.wait())
+ release_parallel_work.set()
+ return await super().thread_start(**kwargs)
+
+ client = _GatedClient()
+ provider = _provider(
+ client,
+ endpoint=_GatedEndpoint(),
+ input_transcriber_factory=_GatedTranscriber,
+ )
+
+ session = await asyncio.wait_for(
+ provider.open_session(RealtimeSessionConfig()), timeout=1.0
+ )
+
+ assert offer_started.is_set()
+ assert warm_started.is_set()
+ await session.close()
@pytest.mark.asyncio
@@ -1274,11 +1320,16 @@ async def test_broker_keeps_answered_lease_until_provider_release() -> None:
@pytest.mark.asyncio
-async def test_unconnectable_host_path_retries_with_stun() -> None:
+async def test_unconnectable_host_path_retries_with_stun(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
"""Host candidates cost no gathering time and connect on an ordinary
network; a network that needs a reflexive candidate must still work, so a
dead media path is retried once WITH a STUN server."""
- from jarvis.realtime.webrtc_transport import WebRtcMediaPathUnavailable
+ from jarvis.realtime import webrtc_transport
+
+ monkeypatch.setattr(webrtc_transport, "stun_ice_servers", lambda: ["stun"])
+ WebRtcMediaPathUnavailable = webrtc_transport.WebRtcMediaPathUnavailable
client = _Client()
ice_configs: list[object] = []