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(realtime): close round-7 findings — warm-path plan recording, in-app recovery from an invalid profile

Ruben Lütkeaf844199b192
diff --git a/jarvis/codex_app_server.py b/jarvis/codex_app_server.py
index e9f9e8dbbc..1b05660b94 100644
--- a/jarvis/codex_app_server.py
+++ b/jarvis/codex_app_server.py
@@ -272,10 +272,14 @@ class CodexAppServerRPCError(CodexAppServerError):
# SSOT for the subscription-status reason-code vocabulary. Mirrors (five-layer
-# anti-drift guard, BUG-008 class): the payload assert in
-# ``jarvis/ui/web/provider_routes.py``, the TS union in ``useProviders.ts``,
-# the card's ternary in ``ProviderTierSection.tsx``, and one
-# ``apikeys_codex.status_*`` i18n key per member in de/en/es.
+# anti-drift guard, BUG-008 class): the runtime membership guard in
+# ``jarvis/ui/web/provider_routes.py::_codex_subscription_status_payload``
+# (unknown values degrade to busy with a warning), the TS union in
+# ``useProviders.ts``, the exhaustive ``CODEX_STATUS_KEY_BY_REASON`` record in
+# ``ProviderTierSection.tsx`` (a new member fails the TS build until mapped;
+# the i18n parity test derives its key list from it), and one
+# ``apikeys_codex.status_*`` i18n key per member in de/en/es. The Python side
+# is pinned by ``test_reason_code_vocabulary_is_pinned``.
# "busy" is transient: the profile is briefly owned by another probe, a
# login/logout, or a starting voice transport. Not a setup defect; callers
# keep their last known state instead of asking the user to reconnect.
@@ -411,7 +415,14 @@ def _validate_current_owner(metadata: os.stat_result) -> None:
def _validate_regular_private_file(path: Path) -> None:
- metadata = path.lstat()
+ try:
+ metadata = path.lstat()
+ except OSError as exc:
+ # An unreadable entry (antivirus hold, slow disk) is transiently
+ # unknown, never proof of a broken profile.
+ raise CodexSubscriptionInspectionFailed(
+ "The dedicated Codex voice profile could not be inspected."
+ ) from exc
if _is_link_or_reparse(path) or not stat.S_ISREG(metadata.st_mode):
raise CodexSubscriptionUnavailable(
"The dedicated Codex voice profile contains an unsafe filesystem entry."
@@ -462,7 +473,13 @@ def _validate_private_directory(
exact_posix_mode: int | None = None,
require_owner_only: bool = False,
) -> None:
- metadata = path.lstat()
+ try:
+ metadata = path.lstat()
+ except OSError as exc:
+ # Transiently unreadable, not proven-broken (see the file variant).
+ raise CodexSubscriptionInspectionFailed(
+ "The dedicated Codex voice profile could not be inspected."
+ ) from exc
if _is_link_or_reparse(path) or not stat.S_ISDIR(metadata.st_mode):
raise CodexSubscriptionUnavailable(
"The dedicated Codex voice profile contains an unsafe directory."
@@ -804,8 +821,50 @@ def _validated_subscription_home(
return canonical_home
+def _rebuild_invalid_subscription_home() -> None:
+ """Remove an invalid Jarvis-owned voice profile so login can rebuild it.
+
+ Reached only from an explicit user action (Connect / Disconnect): the
+ directory is Jarvis's own — never user documents — and its only meaningful
+ content is the dedicated login the user is actively replacing or removing.
+ Without this, ``setup_invalid`` was an in-app dead end whose card text
+ demanded a reconnect that failed on the very same validation (CLAUDE.md
+ §3: recoverable IN-APP). A symlinked profile is detached, never followed.
+ """
+ from jarvis.core.paths import user_data_dir
+
+ try:
+ root = user_data_dir().resolve(strict=True)
+ except OSError as exc:
+ raise CodexSubscriptionUnavailable(
+ "Jarvis's private data directory could not be verified."
+ ) from exc
+ candidate = root / _SUBSCRIPTION_HOME_DIRNAME
+ try:
+ if candidate.is_symlink():
+ candidate.unlink()
+ return
+ if candidate.exists():
+ shutil.rmtree(candidate)
+ except OSError as exc:
+ raise CodexSubscriptionUnavailable(
+ "The invalid Codex voice profile could not be removed. Delete the "
+ f"'{_SUBSCRIPTION_HOME_DIRNAME}' folder in Jarvis's data "
+ "directory manually."
+ ) from exc
+
+
def _prepare_subscription_login_home() -> Path:
- home = _validated_subscription_home(create=True, require_marker=False)
+ try:
+ home = _validated_subscription_home(create=True, require_marker=False)
+ except CodexSubscriptionInspectionFailed:
+ # Transiently unreadable is NOT license to delete anything.
+ raise
+ except CodexSubscriptionUnavailable:
+ # The profile is invalid and the user explicitly asked for a fresh
+ # login — rebuild the Jarvis-owned directory from scratch.
+ _rebuild_invalid_subscription_home()
+ home = _validated_subscription_home(create=True, require_marker=False)
marker = home / _SUBSCRIPTION_PROFILE_MARKER
auth_file = home / "auth.json"
if not marker.exists() and auth_file.exists():
@@ -892,7 +951,7 @@ def _sha256_file_cached(path: Path) -> str:
"""
try:
identity = path.stat()
- except OSError:
+ except OSError: # An unreadable identity cannot be memoized — hash fresh.
return _sha256_file(path)
key = os.path.normcase(str(path))
signature = (
@@ -1761,15 +1820,12 @@ class CodexAppServerClient:
# nobody left to release.
decision_lock = threading.Lock()
abandoned = threading.Event()
- reserve_failed: list[BaseException] = []
published_epoch: list[int] = []
def _reserve() -> None:
- try:
- epoch = _reserve_subscription_transport(self)
- except BaseException as exc:
- reserve_failed.append(exc)
- raise
+ # A raising reserve holds nothing and propagates through the
+ # awaited future; only a SUCCESSFUL reserve needs a decider.
+ epoch = _reserve_subscription_transport(self)
with decision_lock:
if not abandoned.is_set():
self._profile_transport_epoch = epoch
@@ -2502,19 +2558,24 @@ class CodexAppServerClient:
stdin.write(payload)
await stdin.drain()
except (BrokenPipeError, ConnectionError, OSError) as exc:
- await self._close_process(
- CodexAppServerDisconnected("Codex app-server input closed."),
- expected=False,
- )
+ # Identity guard (mirror of _reader_loop's finally): a drain
+ # that outlived a teardown-and-restart must not close the NEW
+ # process or release the successor's reservation.
+ if process is self._process:
+ await self._close_process(
+ CodexAppServerDisconnected("Codex app-server input closed."),
+ expected=False,
+ )
raise CodexAppServerDisconnected("Codex app-server input closed.") from exc
async def _reader_loop(self, process: asyncio.subprocess.Process) -> None:
stream = process.stdout
if stream is None:
- await self._close_process(
- CodexAppServerDisconnected("Codex app-server stdout is unavailable."),
- expected=False,
- )
+ if process is self._process:
+ await self._close_process(
+ CodexAppServerDisconnected("Codex app-server stdout is unavailable."),
+ expected=False,
+ )
return
try:
while True:
@@ -2854,7 +2915,19 @@ class CodexAppServerClient:
)
self._audit_effective_config(await self._read_effective_config())
except CodexSubscriptionUnavailable as exc:
- error = CodexSubscriptionUnavailable(str(exc))
+ if isinstance(exc, CodexSubscriptionPlanUnsupported):
+ # The WARM call path re-judges the live account before
+ # every thread — the same recording rule as the cold
+ # start applies, or a plan that changed between calls
+ # would fail every call while every surface says ready.
+ await asyncio.shield(
+ asyncio.to_thread(
+ set_codex_subscription_activation_block, str(exc)
+ )
+ )
+ # Preserve the subclass: callers and future recorders may
+ # discriminate on it.
+ error = type(exc)(str(exc))
await self._close_process(
CodexAppServerDisconnected(
"Codex app-server safety state changed."
@@ -3827,6 +3900,18 @@ def _delete_codex_subscription_auth_locked() -> tuple[bool, str | None]:
home = _validated_subscription_home(create=False, require_marker=True)
except CodexSubscriptionProfileMissing: # Logging out an absent profile is idempotent success.
return True, None
+ except CodexSubscriptionInspectionFailed as exc:
+ # Transiently unreadable — refuse honestly instead of deleting blind.
+ return False, f"{exc} Try again in a moment."
+ except CodexSubscriptionUnavailable:
+ # Disconnecting must work IN-APP even when the profile is invalid:
+ # removing the whole Jarvis-owned directory deletes the credential
+ # AND clears the invalid state in one explicit user action.
+ try:
+ _rebuild_invalid_subscription_home()
+ except CodexSubscriptionUnavailable as exc: # The honest failure text returns as the route's 409 detail.
+ return False, str(exc)
+ return True, None
auth_file = home / "auth.json"
if not auth_file.exists() and not auth_file.is_symlink():
return True, None
@@ -4039,6 +4124,22 @@ async def close_shared_codex_app_servers() -> None:
log.warning(
"Dropping Codex app-server entry whose owner loop is closed"
)
+ # The dead loop can never reap its child: kill the tree and
+ # close the lifeline best-effort BEFORE freeing the profile,
+ # so no orphan keeps running against CODEX_HOME while the
+ # reservation reads as free.
+ orphan_process = client._process
+ client._process = None
+ orphan_tree = client._process_tree
+ client._process_tree = None
+ if orphan_process is not None and orphan_process.returncode is None:
+ with suppress(ProcessLookupError, OSError):
+ orphan_process.kill()
+ if orphan_tree is not None:
+ with suppress(Exception):
+ orphan_tree.close()
+ with suppress(Exception):
+ client._close_lifeline()
epoch = client._profile_transport_epoch
client._profile_transport_epoch = None
if epoch is not None:
diff --git a/jarvis/plugins/realtime/codex_subscription.py b/jarvis/plugins/realtime/codex_subscription.py
index 20adff8e40..aefe7e4c88 100644
--- a/jarvis/plugins/realtime/codex_subscription.py
+++ b/jarvis/plugins/realtime/codex_subscription.py
@@ -737,7 +737,10 @@ class CodexSubscriptionRealtimeProvider:
try:
await client.require_chatgpt_login()
finally:
- if not was_ready:
+ # A cold call may have raced this gate onto the same client while
+ # we awaited: active thread subscriptions mean someone else is
+ # using it now — closing would cut their session mid-setup.
+ if not was_ready and not getattr(client, "_subscriptions", None):
await client.close()
def __init__(
diff --git a/jarvis/ui/web/frontend/src/components/providers/ProviderTierSection.tsx b/jarvis/ui/web/frontend/src/components/providers/ProviderTierSection.tsx
index ce89cb80b0..c6b404f80a 100644
--- a/jarvis/ui/web/frontend/src/components/providers/ProviderTierSection.tsx
+++ b/jarvis/ui/web/frontend/src/components/providers/ProviderTierSection.tsx
@@ -805,6 +805,19 @@ export function ProviderCard({
);
return;
}
+ // The experimental acknowledgement must come BEFORE the optimistic flip:
+ // a declined dialog used to return early with the radio stuck on the new
+ // card and no refetch to roll it back.
+ const isRealtimeSwitch = !["brain", "tts", "stt", "computer-use", "dictation"].includes(
+ descriptor.tier,
+ );
+ if (
+ isRealtimeSwitch &&
+ descriptor.experimental &&
+ !window.confirm(t("apikeys_view.experimental_subscription_consent"))
+ ) {
+ return;
+ }
// Flip the highlight immediately so the switch feels instant — the backend
// call below can take a few seconds (a TTS switch rebuilds the provider and
// injects it into the live pipeline). The refetch on success / failure then
@@ -860,12 +873,6 @@ export function ProviderCard({
// cheap here (a handful of tokens) and the only honest signal.
void verifyPolishProvider();
} else {
- if (
- descriptor.experimental &&
- !window.confirm(t("apikeys_view.experimental_subscription_consent"))
- ) {
- return;
- }
const result = await switchRealtimeProvider(
descriptor.id,
descriptor.experimental === true,
@@ -1865,6 +1872,19 @@ function CodexAuthWidget({
{t("apikeys_codex.install_codex")}
</a>
</Button>
+ {/* A plan-refused login is stored but unusable — offer the exit even
+ though the connected panel (with its Disconnect) never renders. */}
+ {status?.reason_code === "plan_unsupported" && (
+ <Button
+ size="sm"
+ variant="ghost"
+ onClick={handleLogout}
+ disabled={pending !== null}
+ >
+ <LogOut className="h-3.5 w-3.5" />
+ {t("apikeys_codex.disconnect")}
+ </Button>
+ )}
</div>
</div>
diff --git a/jarvis/ui/web/provider_routes.py b/jarvis/ui/web/provider_routes.py
index 9132c443ee..59e76601b0 100644
--- a/jarvis/ui/web/provider_routes.py
+++ b/jarvis/ui/web/provider_routes.py
@@ -952,10 +952,13 @@ def _codex_subscription_status_payload(binary_path: str | None) -> dict[str, Any
elif connected:
message = "Dedicated ChatGPT subscription voice login is ready."
reason_code = "ready"
- elif snapshot.available:
+ elif snapshot.available and snapshot.reason_code == "login_required":
message = "Connect the dedicated ChatGPT subscription voice login."
reason_code = snapshot.reason_code
else:
+ # Non-login states carry their own degraded reason (for example the
+ # headless-Linux text) — overwriting it with the login invite would
+ # ask for exactly the action the reason declares impossible.
message = snapshot.reason
reason_code = snapshot.reason_code
if reason_code not in CODEX_SUBSCRIPTION_REASON_CODES:
@@ -1376,6 +1379,22 @@ async def _tier_section_health(
),
subject_id=spec.id,
)
+ if codex_payload.get("reason_code") in {
+ "lifecycle_unavailable",
+ "not_installed",
+ }:
+ # Same rule: the hover text must name the actual remedy (an
+ # unsupported host, or installing the pinned release) — never
+ # "not connected or configured".
+ return SectionHealth(
+ status=_section_health.NEEDS_SETUP,
+ reason=str(codex_payload.get("reason_code")),
+ detail=(
+ f"{spec.label}: "
+ f"{codex_payload.get('message') or 'not available'}"
+ ),
+ subject_id=spec.id,
+ )
configured = bool(codex_payload.get("connected"))
else:
configured = await _provider_credential_present_for_binary_async(
diff --git a/tests/unit/codex_app_server/test_transport.py b/tests/unit/codex_app_server/test_transport.py
index 2a1e34e9c4..b911cd7374 100644
--- a/tests/unit/codex_app_server/test_transport.py
+++ b/tests/unit/codex_app_server/test_transport.py
@@ -2446,6 +2446,111 @@ def test_unreadable_profile_reports_busy_not_setup_invalid(
assert "could not be inspected" in capability.reason
+async def test_warm_thread_start_plan_refusal_is_recorded(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ """A plan that turns unsupported BETWEEN calls is discovered by the warm
+ per-thread account re-check — it must record the sticky diagnosis too."""
+ client = CodexAppServerClient("codex-test")
+
+ async def already_ready() -> None:
+ return None
+
+ async def refuse() -> None:
+ raise transport.CodexSubscriptionPlanUnsupported(
+ "Subscription voice permits only personal ChatGPT accounts."
+ )
+
+ async def no_close(*_args: object, **_kwargs: object) -> None:
+ return None
+
+ monkeypatch.setattr(client, "ensure_started", already_ready)
+ monkeypatch.setattr(client, "_verify_live_chatgpt_account", refuse)
+ monkeypatch.setattr(client, "_close_process", no_close)
+ monkeypatch.setattr(
+ transport, "_validated_subscription_home", lambda **_kwargs: tmp_path
+ )
+ client._workspace = SimpleNamespace(
+ root=tmp_path,
+ instructions=tmp_path / "instructions.md",
+ compact_prompt=tmp_path / "compact.md",
+ model_catalog=tmp_path / "models.json",
+ sqlite_home=tmp_path,
+ log_dir=tmp_path,
+ child_home=tmp_path,
+ child_appdata=tmp_path,
+ child_local_appdata=tmp_path,
+ child_tmp=tmp_path,
+ )
+ client._sink_base_url = "http://127.0.0.1:1/"
+
+ with pytest.raises(transport.CodexSubscriptionPlanUnsupported):
+ await client.thread_start(
+ base_instructions="transport only",
+ developer_instructions="no tools",
+ ephemeral=True,
+ )
+
+ assert "personal ChatGPT accounts" in (
+ transport.codex_subscription_activation_block() or ""
+ )
+
+
+def test_connect_rebuilds_an_invalid_profile(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ """setup_invalid must not be an in-app dead end: the explicit login action
+ rebuilds the Jarvis-owned profile instead of failing on the same check."""
+ calls: list[str] = []
+ attempts = {"n": 0}
+
+ def flaky_home(**kwargs: object) -> Path:
+ attempts["n"] += 1
+ if attempts["n"] == 1:
+ raise CodexSubscriptionUnavailable(
+ "The dedicated Codex voice profile contains configuration or "
+ "runtime state. Create a fresh voice-only login."
+ )
+ return tmp_path
+
+ monkeypatch.setattr(transport, "_validated_subscription_home", flaky_home)
+ monkeypatch.setattr(
+ transport,
+ "_rebuild_invalid_subscription_home",
+ lambda: calls.append("rebuilt"),
+ )
+
+ home = transport._prepare_subscription_login_home()
+
+ assert calls == ["rebuilt"]
+ assert home == tmp_path
+
+
+def test_logout_clears_an_invalid_profile(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Disconnect must work in-app even when the profile fails validation."""
+ calls: list[str] = []
+
+ def invalid_home(**_kwargs: object) -> Path:
+ raise CodexSubscriptionUnavailable(
+ "The dedicated Codex voice profile contains an unknown runtime directory."
+ )
+
+ monkeypatch.setattr(transport, "_validated_subscription_home", invalid_home)
+ monkeypatch.setattr(
+ transport,
+ "_rebuild_invalid_subscription_home",
+ lambda: calls.append("removed"),
+ )
+
+ assert transport._delete_codex_subscription_auth_locked() == (True, None)
+ assert calls == ["removed"]
+
+
def test_activation_block_survives_cache_invalidation_until_logout(
monkeypatch: pytest.MonkeyPatch,
) -> None: