Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(codex): rename managed provider id off reserved 'openai'

cyh0a71ac498b47
diff --git a/tests/test_codex_config.py b/tests/test_codex_config.py
index 5edb658ce..68154175a 100644
--- a/tests/test_codex_config.py
+++ b/tests/test_codex_config.py
@@ -89,7 +89,14 @@ def test_apply_api_key_mode_preserves_unrelated_blocks(tmp_path: Path) -> None:
parsed = tomllib.loads((codex_home / "config.toml").read_text(encoding="utf-8"))
assert parsed["model"] == "gpt-5"
assert parsed["model_provider"] == codex_config.MANAGED_PROVIDER_ID
- assert parsed["model_providers"]["openai"]["base_url"] == "https://api.example.com/v1"
+ # Managed section now lives under the non-reserved id since
+ # newer Codex versions refuse to load configs that override the
+ # built-in ``openai`` provider.
+ assert (
+ parsed["model_providers"][codex_config.MANAGED_PROVIDER_ID]["base_url"]
+ == "https://api.example.com/v1"
+ )
+ assert "openai" not in parsed.get("model_providers", {})
assert parsed["projects"]["/Users/alex/code/vibe-remote"]["trust_level"] == "trusted"
assert parsed["a"]["b"]["c"]["x"] == 1
@@ -97,6 +104,73 @@ def test_apply_api_key_mode_preserves_unrelated_blocks(tmp_path: Path) -> None:
assert auth["OPENAI_API_KEY"] == "sk-test-1234567890"
+def test_apply_purges_legacy_reserved_openai_section(tmp_path: Path) -> None:
+ """Older releases wrote our managed shape under
+ ``[model_providers.openai]``. Newer Codex versions reject that
+ (built-in provider), so any save / oauth-switch must purge the
+ legacy section even when we'd otherwise leave provider blocks
+ alone."""
+ home = tmp_path
+ codex_home = home / ".codex"
+ codex_home.mkdir()
+ seed = (
+ 'model_provider = "openai"\n'
+ "\n"
+ "[model_providers.OpenAI]\n" # user's custom (TitleCase) relay
+ 'name = "OpenAI"\n'
+ 'base_url = "https://relay.example/v1"\n'
+ "\n"
+ "[model_providers.openai]\n" # garbage we wrote previously
+ 'name = "OpenAI"\n'
+ )
+ (codex_home / "config.toml").write_text(seed, encoding="utf-8")
+
+ codex_config.apply_codex_auth(
+ auth_mode="oauth",
+ api_key=None,
+ base_url=None,
+ home=home,
+ )
+ parsed = tomllib.loads((codex_home / "config.toml").read_text(encoding="utf-8"))
+ providers = parsed.get("model_providers", {})
+ # Reserved-name section is gone; user's TitleCase relay survives.
+ assert "openai" not in providers
+ assert providers["OpenAI"]["base_url"] == "https://relay.example/v1"
+ # Our previously-set top-level pointer (``model_provider = "openai"``)
+ # is no longer ours to own — reverted because it pointed at the
+ # legacy managed id, freeing the user to choose their relay name.
+ assert "model_provider" not in parsed
+
+
+def test_apply_preserves_user_owned_model_provider(tmp_path: Path) -> None:
+ """When the user has aimed ``model_provider`` at their own custom
+ section (e.g. a TitleCase relay), an api_key save must not overwrite
+ that pointer — overriding it would silently bypass their relay
+ config and route through Codex's built-in ``openai`` provider."""
+ home = tmp_path
+ codex_home = home / ".codex"
+ codex_home.mkdir()
+ seed = (
+ 'model_provider = "OpenAI"\n'
+ "\n"
+ "[model_providers.OpenAI]\n"
+ 'name = "OpenAI"\n'
+ 'base_url = "https://relay.example/v1"\n'
+ )
+ (codex_home / "config.toml").write_text(seed, encoding="utf-8")
+
+ codex_config.apply_codex_auth(
+ auth_mode="api_key",
+ api_key="sk-test-userowned",
+ base_url=None,
+ home=home,
+ )
+ parsed = tomllib.loads((codex_home / "config.toml").read_text(encoding="utf-8"))
+ # User's pointer + section survive intact.
+ assert parsed["model_provider"] == "OpenAI"
+ assert parsed["model_providers"]["OpenAI"]["base_url"] == "https://relay.example/v1"
+
+
def test_round_trip_datetime_scalars() -> None:
"""``tomllib`` returns datetime/date/time for temporal TOML values; the
emitter must round-trip them unquoted instead of crashing on the
diff --git a/vibe/codex_config.py b/vibe/codex_config.py
index 26c6afc1c..7023d3a73 100644
--- a/vibe/codex_config.py
+++ b/vibe/codex_config.py
@@ -32,10 +32,14 @@ logger = logging.getLogger(__name__)
# so the emitter has to round-trip those correctly.
_BARE_KEY_RE = re.compile(r"^[A-Za-z0-9_-]+$")
-# Provider id we manage in ``[model_providers.<id>]``. Codex ships with a
-# built-in ``openai`` provider; if the user has hand-edited that block we
-# leave their fields alone except for ``base_url`` when one is supplied.
-MANAGED_PROVIDER_ID = "openai"
+# Provider id we manage in ``[model_providers.<id>]``. Newer Codex
+# versions reserve ``openai`` as a built-in (the CLI refuses to load a
+# config that overrides it: "model_providers contains reserved built-in
+# provider IDs: openai"), so our managed section uses a non-reserved
+# suffix. ``openai`` is kept as a legacy-cleanup target — see
+# ``LEGACY_MANAGED_PROVIDER_IDS`` and the migration in ``apply_codex_auth``.
+MANAGED_PROVIDER_ID = "openai-managed"
+LEGACY_MANAGED_PROVIDER_IDS = ("openai",)
# Codex's top-level ``cli_auth_credentials_store`` controls where the CLI
# reads/writes cached credentials: ``file`` → ``~/.codex/auth.json``,
@@ -303,7 +307,16 @@ def apply_codex_auth(
if not api_key:
raise ValueError("api_key is required when auth_mode='api_key'")
auth_data["OPENAI_API_KEY"] = api_key
- toml_data["model_provider"] = MANAGED_PROVIDER_ID
+ # Only steer ``model_provider`` to our managed entry when the
+ # field is unset or still points at one of our legacy / current
+ # managed names. If the user has aimed it at a hand-rolled
+ # provider (e.g. their ``[model_providers.OpenAI]`` relay
+ # section), keep their pointer — overriding it would silently
+ # bypass their custom base_url + wire_api config.
+ current_mp = toml_data.get("model_provider")
+ managed_known = {MANAGED_PROVIDER_ID, *LEGACY_MANAGED_PROVIDER_IDS, ""}
+ if not isinstance(current_mp, str) or current_mp in managed_known:
+ toml_data["model_provider"] = MANAGED_PROVIDER_ID
# Pin Codex to file-based credentials so it actually reads the
# ``OPENAI_API_KEY`` we just wrote. Without this, the documented
# default (``auto``) prefers the OS keychain, and Codex would
@@ -317,17 +330,30 @@ def apply_codex_auth(
managed.pop("base_url", None)
else: # oauth
auth_data.pop("OPENAI_API_KEY", None)
- # Leave model_provider and cli_auth_credentials_store as-is —
- # switching back to ChatGPT/OAuth is the user's responsibility
- # via ``codex login`` (which may legitimately want keyring
- # storage); we just stop pinning the keyed provider's overrides.
+ # Leave cli_auth_credentials_store as-is — switching back to
+ # ChatGPT/OAuth is the user's responsibility via ``codex login``
+ # (which may legitimately want keyring storage); we just stop
+ # pinning the keyed provider's overrides.
managed.pop("base_url", None)
- # If our managed entry is now empty, drop it entirely so we don't
- # leave a noisy ``[model_providers.openai]`` table behind.
- if not managed:
- providers.pop(MANAGED_PROVIDER_ID, None)
- if not providers:
- toml_data.pop("model_providers", None)
+ # Drop the managed section entirely on oauth — the api_key path
+ # is the only thing that needs the ``name = "OpenAI"`` stub, and
+ # leaving it behind under the legacy reserved name (``openai``)
+ # makes newer Codex versions refuse to load the config at all.
+ providers.pop(MANAGED_PROVIDER_ID, None)
+ # Revert top-level ``model_provider`` if we own it.
+ current_mp = toml_data.get("model_provider")
+ managed_known = {MANAGED_PROVIDER_ID, *LEGACY_MANAGED_PROVIDER_IDS}
+ if isinstance(current_mp, str) and current_mp in managed_known:
+ toml_data.pop("model_provider", None)
+
+ # Always purge any legacy reserved-name section we may have written
+ # in older releases. Codex 1.x refuses to load a config that defines
+ # ``[model_providers.openai]`` ("Built-in providers cannot be
+ # overridden"); leaving it around bricks the CLI for the user.
+ for legacy_id in LEGACY_MANAGED_PROVIDER_IDS:
+ providers.pop(legacy_id, None)
+ if not providers:
+ toml_data.pop("model_providers", None)
_atomic_write(auth_path, json.dumps(auth_data, indent=2) + "\n", mode=0o600)
_atomic_write(config_path, _dump_toml(toml_data), mode=0o600)
@@ -447,6 +473,16 @@ def read_codex_auth_state(home: Path | None = None) -> Dict[str, Any]:
active_section = providers[active_provider]
elif isinstance(providers.get(MANAGED_PROVIDER_ID), dict):
active_section = providers[MANAGED_PROVIDER_ID]
+ else:
+ # Legacy fallback: older releases wrote our managed shape
+ # under ``[model_providers.openai]``. New Codex rejects that
+ # name as reserved, so we'll purge it on the next save, but
+ # the UI should still surface the base_url until then.
+ for legacy_id in LEGACY_MANAGED_PROVIDER_IDS:
+ legacy_section = providers.get(legacy_id)
+ if isinstance(legacy_section, dict):
+ active_section = legacy_section
+ break
if isinstance(active_section, dict):
raw = active_section.get("base_url")
if isinstance(raw, str) and raw.strip():