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.

fix(voice): release subscription transport on provider switch

rubenluetke10-beepa283692f30c5
diff --git a/jarvis/realtime/factory.py b/jarvis/realtime/factory.py
index f9ed193f6b..a1347067bb 100644
--- a/jarvis/realtime/factory.py
+++ b/jarvis/realtime/factory.py
@@ -242,6 +242,17 @@ def _realtime_is_the_configured_voice_mode(cfg: Any) -> bool:
CONFIGURATION, not about a live call: a warm transport is worth having
BEFORE the first wake word, which is the entire point of warming.
"""
+ # The stable ChatGPT-subscription composition intentionally runs through
+ # SpeechPipeline even when an older install still carries
+ # ``voice.mode = realtime`` or the legacy realtime-provider pin. Warming
+ # that retired duplex adapter starts a second App Server and holds the one
+ # subscription-profile lease before the text voice path can answer.
+ from jarvis.voice.subscription_profile import ( # noqa: PLC0415
+ subscription_voice_selected,
+ )
+
+ if subscription_voice_selected(cfg):
+ return False
return getattr(getattr(cfg, "voice", None), "mode", "pipeline") == "realtime"
diff --git a/jarvis/ui/web/provider_routes.py b/jarvis/ui/web/provider_routes.py
index 79efd20b8f..9997abf1da 100644
--- a/jarvis/ui/web/provider_routes.py
+++ b/jarvis/ui/web/provider_routes.py
@@ -4572,6 +4572,35 @@ async def stt_switch(body: SwitchBody, request: Request) -> dict[str, Any]:
}
+async def _release_codex_transports_for_voice_switch() -> None:
+ """Release warm/previous Codex voice leases before changing providers."""
+ from jarvis.codex_app_server import ( # noqa: PLC0415
+ close_shared_codex_app_servers,
+ )
+
+ try:
+ await asyncio.wait_for(
+ asyncio.shield(close_shared_codex_app_servers()),
+ timeout=15.0,
+ )
+ except TimeoutError as exc:
+ raise HTTPException(
+ status_code=409,
+ detail=(
+ "The previous ChatGPT subscription voice transport is still "
+ "stopping. Try the provider switch again in a moment."
+ ),
+ ) from exc
+ except Exception as exc: # noqa: BLE001 - a leaked lease must block the switch
+ raise HTTPException(
+ status_code=409,
+ detail=(
+ "The previous ChatGPT subscription voice transport could not "
+ "be stopped safely. Try the provider switch again."
+ ),
+ ) from exc
+
+
@router.post("/realtime/switch")
async def realtime_switch(body: SwitchBody, request: Request) -> dict[str, Any]:
"""Switch the active realtime provider.
@@ -4601,6 +4630,7 @@ async def realtime_switch(body: SwitchBody, request: Request) -> dict[str, Any]:
"the experimental transport before activating it."
),
)
+ codex_transport_released = False
if spec.id == "codex-subscription-realtime":
# One snapshot call answers both "configured?" and "busy?". A busy
# window is transient — telling the user to "connect first" while the
@@ -4609,12 +4639,25 @@ async def realtime_switch(body: SwitchBody, request: Request) -> dict[str, Any]:
_codex_subscription_status_payload, _codex_binary_path(request)
)
if codex_payload.get("reason_code") == "busy":
- raise HTTPException(
- status_code=409,
- detail=(
- "The ChatGPT subscription voice status is being checked. Try again in a moment."
- ),
+ # A warm transport reports the shared profile as busy. During an
+ # explicit provider switch that is the resource to hand off, not
+ # a reason to dead-end the button. Release it and judge the fresh
+ # post-handoff status once; a real login/profile mutation remains
+ # busy and still fails honestly below.
+ await _release_codex_transports_for_voice_switch()
+ codex_transport_released = True
+ codex_payload = await asyncio.to_thread(
+ _codex_subscription_status_payload,
+ _codex_binary_path(request),
)
+ if codex_payload.get("reason_code") == "busy":
+ raise HTTPException(
+ status_code=409,
+ detail=(
+ "The ChatGPT subscription voice status is being checked. "
+ "Try again in a moment."
+ ),
+ )
if codex_payload.get("reason_code") == "login_in_progress":
raise HTTPException(
status_code=409,
@@ -4658,6 +4701,12 @@ async def realtime_switch(body: SwitchBody, request: Request) -> dict[str, Any]:
CODEX_SUBSCRIPTION_VOICE_PROFILE,
)
+ # A previous stable-text session or the retired duplex adapter may be
+ # warm on another event loop. Provider selection is an explicit
+ # handoff: release that old owner before the activation probe acquires
+ # the single subscription-profile lease.
+ if not codex_transport_released:
+ await _release_codex_transports_for_voice_switch()
client = None
try:
client = get_shared_codex_app_server(_codex_binary_path(request), purpose="text")
@@ -4742,6 +4791,10 @@ async def realtime_switch(body: SwitchBody, request: Request) -> dict[str, Any]:
# A refused plan already recorded its sticky diagnosis where it was
# discovered (require_chatgpt_login) — the live call path shares it.
raise HTTPException(status_code=409, detail=str(exc)) from exc
+ # The replacement provider is viable. Release any stable or legacy Codex
+ # voice client before persisting/reopening the new session so switching
+ # away and back never leaves an invisible subscription-profile owner.
+ await _release_codex_transports_for_voice_switch()
if spec.id == "codex-subscription-realtime":
from jarvis.codex_app_server import set_codex_subscription_activation_block
diff --git a/tests/unit/realtime/test_factory.py b/tests/unit/realtime/test_factory.py
index 4f508330e2..4704997dc3 100644
--- a/tests/unit/realtime/test_factory.py
+++ b/tests/unit/realtime/test_factory.py
@@ -72,9 +72,13 @@ _PLUGINS = {
}
-def _cfg(mode: str = "realtime", provider: str = "openai-realtime") -> SimpleNamespace:
+def _cfg(
+ mode: str = "realtime",
+ provider: str = "openai-realtime",
+ profile: str = "",
+) -> SimpleNamespace:
return SimpleNamespace(
- voice=SimpleNamespace(mode=mode),
+ voice=SimpleNamespace(mode=mode, profile=profile),
brain=SimpleNamespace(
reply_language="en",
providers={},
@@ -447,12 +451,45 @@ async def test_warm_selected_transports_still_warms_the_realtime_install(
)
await factory.realtime_warm_selected_transports(
- _cfg(mode="realtime", provider="codex-subscription-realtime")
+ _cfg(mode="realtime", provider="openai-realtime")
)
assert warm.calls == 1
+ ("profile", "provider"),
+ [
+ ("codex-subscription-voice", "openai-realtime"),
+ ("", "codex-subscription-realtime"),
+ ],
+)
+async def test_warm_selected_transports_skips_the_subscription_pipeline_profile(
+ monkeypatch,
+ profile: str,
+ provider: str,
+) -> None:
+ """Neither the canonical profile nor its legacy pin may warm duplex Codex."""
+ warm = _WarmProbe()
+ monkeypatch.setattr(
+ factory,
+ "_explicit_provider_ids",
+ lambda _cfg: ["warm-me"],
+ )
+ monkeypatch.setattr(
+ factory,
+ "load",
+ lambda _group, _pid, protocol=None: warm,
+ )
+
+ await factory.realtime_warm_selected_transports(
+ _cfg(mode="realtime", provider=provider, profile=profile)
+ )
+
+ assert warm.calls == 0
+
+
def test_declared_handshake_budget_reaches_the_surface(monkeypatch) -> None:
"""A surface must not call a start attempt dead inside a declared budget.
diff --git a/tests/unit/ui/test_provider_spec_codex_subscription_realtime.py b/tests/unit/ui/test_provider_spec_codex_subscription_realtime.py
index c1bb6b54b0..8fdaca3789 100644
--- a/tests/unit/ui/test_provider_spec_codex_subscription_realtime.py
+++ b/tests/unit/ui/test_provider_spec_codex_subscription_realtime.py
@@ -273,6 +273,11 @@ async def test_realtime_activation_reports_busy_instead_of_demanding_reconnect(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A transient busy window must not tell the user to redo a working login."""
+ release_calls: list[str] = []
+
+ async def release_previous_transport() -> None:
+ release_calls.append("released")
+
monkeypatch.setattr(routes, "_codex_binary_path", lambda *_args: "codex")
monkeypatch.setattr(
routes,
@@ -285,6 +290,11 @@ async def test_realtime_activation_reports_busy_instead_of_demanding_reconnect(
"reason_code": "busy",
},
)
+ monkeypatch.setattr(
+ routes,
+ "_release_codex_transports_for_voice_switch",
+ release_previous_transport,
+ )
with pytest.raises(HTTPException) as caught:
await routes.realtime_switch(
@@ -300,6 +310,80 @@ async def test_realtime_activation_reports_busy_instead_of_demanding_reconnect(
detail = str(caught.value.detail)
assert "being checked" in detail
assert "Connect or configure" not in detail
+ assert release_calls == ["released"]
+
+
+async def test_realtime_activation_reclaims_a_warm_transport_before_retry(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from jarvis import codex_app_server
+
+ snapshots = [
+ {
+ "installed": True,
+ "connected": False,
+ "mode": "not_connected",
+ "message": "Subscription voice is busy.",
+ "reason_code": "busy",
+ },
+ {
+ "installed": True,
+ "connected": True,
+ "mode": "chatgpt",
+ "message": "ready",
+ "reason_code": "ready",
+ },
+ ]
+ release_calls: list[str] = []
+
+ async def release_previous_transport() -> None:
+ release_calls.append("released")
+
+ class _Client:
+ async def require_chatgpt_login(self) -> None:
+ return None
+
+ async def close(self) -> None:
+ return None
+
+ monkeypatch.setattr(
+ routes,
+ "_codex_subscription_status_payload",
+ lambda _binary_path: snapshots.pop(0),
+ )
+ monkeypatch.setattr(
+ routes,
+ "_release_codex_transports_for_voice_switch",
+ release_previous_transport,
+ )
+ monkeypatch.setattr(
+ codex_app_server,
+ "get_shared_codex_app_server",
+ lambda *_args, **_kwargs: _Client(),
+ )
+ monkeypatch.setattr(
+ codex_app_server,
+ "set_codex_subscription_activation_block",
+ lambda _value: None,
+ )
+ config = SimpleNamespace(
+ codex=SimpleNamespace(binary_path="codex"),
+ voice=SimpleNamespace(profile="", mode="realtime"),
+ brain=SimpleNamespace(realtime=None),
+ )
+
+ response = await routes.realtime_switch(
+ routes.SwitchBody(
+ provider="codex-subscription-realtime",
+ persist=False,
+ ),
+ SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(config=config))),
+ )
+
+ assert response["profile"] == "codex-subscription-voice"
+ assert release_calls == ["released"]
+ assert snapshots == []
def test_spec_payload_preserves_the_isolated_snapshot_diagnosis(
@@ -817,6 +901,10 @@ async def test_subscription_profile_no_longer_requires_experimental_acknowledgem
)
close_calls: list[object] = []
+ release_calls: list[str] = []
+
+ async def release_previous_transport() -> None:
+ release_calls.append("released")
class _Client:
async def require_chatgpt_login(self) -> None:
@@ -835,6 +923,11 @@ async def test_subscription_profile_no_longer_requires_experimental_acknowledgem
"set_codex_subscription_activation_block",
lambda _value: None,
)
+ monkeypatch.setattr(
+ routes,
+ "_release_codex_transports_for_voice_switch",
+ release_previous_transport,
+ )
config = SimpleNamespace(
codex=SimpleNamespace(binary_path="codex"),
voice=SimpleNamespace(profile="", mode="realtime"),
@@ -851,6 +944,87 @@ async def test_subscription_profile_no_longer_requires_experimental_acknowledgem
assert response["profile"] == "codex-subscription-voice"
assert response["mode"] == "pipeline"
assert len(close_calls) == 1
+ assert release_calls == ["released"]
+
+
+async def test_switching_away_releases_the_subscription_voice_transport(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import jarvis.core.registry as registry
+
+ spec = SimpleNamespace(
+ id="replacement-realtime",
+ tier="realtime",
+ experimental=False,
+ )
+ release_calls: list[str] = []
+
+ class Provider:
+ requires_webrtc_offer = False
+
+ @classmethod
+ async def verify_activation(cls, _cfg: object) -> None:
+ return None
+
+ async def credential_present(*_args, **_kwargs) -> bool:
+ return True
+
+ async def release_previous_transport() -> None:
+ release_calls.append("released")
+
+ monkeypatch.setattr(routes, "get_spec", lambda _provider: spec)
+ monkeypatch.setattr(
+ routes,
+ "_provider_credential_present_async",
+ credential_present,
+ )
+ monkeypatch.setattr(registry, "load", lambda *_args, **_kwargs: Provider)
+ monkeypatch.setattr(
+ routes,
+ "_release_codex_transports_for_voice_switch",
+ release_previous_transport,
+ )
+ config = SimpleNamespace(
+ voice=SimpleNamespace(
+ profile="codex-subscription-voice",
+ mode="pipeline",
+ ),
+ brain=SimpleNamespace(
+ realtime=SimpleNamespace(provider="codex-subscription-realtime")
+ ),
+ )
+
+ response = await routes.realtime_switch(
+ routes.SwitchBody(provider=spec.id, persist=False),
+ SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(config=config))),
+ )
+
+ assert response["active"] == spec.id
+ assert release_calls == ["released"]
+ assert config.voice.profile == ""
+
+
+async def test_voice_switch_release_closes_shared_codex_transports(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from jarvis import codex_app_server
+
+ calls: list[str] = []
+
+ async def close_shared() -> None:
+ calls.append("closed")
+
+ monkeypatch.setattr(
+ codex_app_server,
+ "close_shared_codex_app_servers",
+ close_shared,
+ )
+
+ await routes._release_codex_transports_for_voice_switch()
+
+ assert calls == ["closed"]
@pytest.mark.asyncio