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(claude): explicit ``auth_mode_set`` marker to distinguish UI-driven OAuth from legacy default

cyh8565b5d5117f
diff --git a/config/v2_compat.py b/config/v2_compat.py
index 47643d612..06f219bad 100644
--- a/config/v2_compat.py
+++ b/config/v2_compat.py
@@ -26,6 +26,10 @@ class ClaudeCompatConfig:
auth_mode: str = "oauth"
api_key: Optional[str] = None
base_url: Optional[str] = None
+ # Mirrors ``ClaudeConfig.auth_mode_set`` — see the docstring there.
+ # Defaults to ``False`` so legacy installs that never saved through
+ # the Settings UI keep their inherited ``ANTHROPIC_*`` env-var auth.
+ auth_mode_set: bool = False
def __post_init__(self) -> None:
self.permission_mode = str(self.permission_mode)
@@ -99,6 +103,7 @@ def to_app_config(v2: V2Config) -> AppCompatConfig:
auth_mode=v2.agents.claude.auth_mode,
api_key=v2.agents.claude.api_key,
base_url=v2.agents.claude.base_url,
+ auth_mode_set=v2.agents.claude.auth_mode_set,
)
codex = None
if v2.agents.codex.enabled:
diff --git a/config/v2_config.py b/config/v2_config.py
index 930fb08c4..7b8da9a88 100644
--- a/config/v2_config.py
+++ b/config/v2_config.py
@@ -172,6 +172,17 @@ class ClaudeConfig:
auth_mode: Literal["oauth", "api_key"] = "oauth"
api_key: Optional[str] = None
base_url: Optional[str] = None
+ # ``True`` once the user has saved a Claude auth choice through the
+ # Settings UI (or removed the API key, or signed out). Legacy installs
+ # — V2 configs that predate the Settings page or have never touched
+ # it — load with ``False`` because the field defaults to ``False`` and
+ # isn't in their on-disk JSON. ``build_claude_subprocess_env`` reads
+ # this to decide whether to honor ``auth_mode`` strictly (strip
+ # inherited ``ANTHROPIC_*`` env in OAuth mode) or preserve the
+ # legacy env-var-only auth path. Without this flag the schema's
+ # ``auth_mode == "oauth"`` default is indistinguishable between
+ # "explicit OAuth pick" and "user has never opened Settings".
+ auth_mode_set: bool = False
@dataclass
diff --git a/core/agent_auth_service.py b/core/agent_auth_service.py
index 3a0376858..64ec677af 100644
--- a/core/agent_auth_service.py
+++ b/core/agent_auth_service.py
@@ -1613,11 +1613,21 @@ class AgentAuthService:
# override, sending OAuth requests to a relay
# that only accepts API keys (401).
target.base_url = None
+ # Sign out is an explicit user choice — flip
+ # the marker so ``build_claude_subprocess_env``
+ # honors ``auth_mode == "oauth"`` strictly
+ # and strips inherited ``ANTHROPIC_*`` env vars
+ # (Codex-only field; setattr is a no-op for
+ # other backends).
+ if backend == "claude":
+ target.auth_mode_set = True
saver()
except ImportError:
target.auth_mode = "oauth"
target.api_key = None
target.base_url = None
+ if backend == "claude":
+ target.auth_mode_set = True
saver()
except Exception as err: # noqa: BLE001
# Disk state has already been cleared; surface the V2Config
@@ -2395,16 +2405,33 @@ class AgentAuthService:
saver = getattr(config, "save", None) if config is not None else None
if target is None or not callable(saver):
return
- if getattr(target, "auth_mode", None) == auth_mode:
+ # An explicit OAuth save must also flip ``auth_mode_set``
+ # for Claude — otherwise a successful OAuth flow on a
+ # legacy install never trips the marker (auth_mode was
+ # already "oauth" from the schema default), so
+ # ``build_claude_subprocess_env`` keeps preserving env-var
+ # auth and the OAuth credentials are ignored at launch.
+ needs_mode_write = getattr(target, "auth_mode", None) != auth_mode
+ needs_marker_write = (
+ backend == "claude"
+ and not bool(getattr(target, "auth_mode_set", False))
+ )
+ if not needs_mode_write and not needs_marker_write:
return
try:
from config.v2_config import CONFIG_LOCK
with CONFIG_LOCK:
- target.auth_mode = auth_mode
+ if needs_mode_write:
+ target.auth_mode = auth_mode
+ if needs_marker_write:
+ target.auth_mode_set = True
saver()
except ImportError:
- target.auth_mode = auth_mode
+ if needs_mode_write:
+ target.auth_mode = auth_mode
+ if needs_marker_write:
+ target.auth_mode_set = True
saver()
except Exception as err: # noqa: BLE001
logger.warning(
diff --git a/vibe/api.py b/vibe/api.py
index be79b1eb0..da078066f 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -2452,6 +2452,12 @@ def remove_backend_api_key(backend: str) -> dict:
# OAuth), the OAuth credentials would still be routed
# to the api-key-only relay and silently 401.
target.base_url = None
+ # User explicitly chose OAuth by clicking Remove key —
+ # mark the flag so legacy env-var fallback in
+ # ``build_claude_subprocess_env`` is bypassed and the
+ # inherited ``ANTHROPIC_*`` env actually gets stripped.
+ if backend == "claude":
+ target.auth_mode_set = True
config.save()
except Exception as exc: # noqa: BLE001
logger.warning("V2Config clear during remove-key failed for %s: %s", backend, exc)
@@ -2922,6 +2928,12 @@ def save_claude_auth(payload: dict) -> dict:
except FileNotFoundError:
config = V2Config()
config.agents.claude.auth_mode = auth_mode
+ # Flip the explicit marker so ``build_claude_subprocess_env``
+ # honors ``auth_mode`` strictly (strip inherited env in OAuth
+ # mode) for this and subsequent launches. Legacy installs that
+ # have never been through this save path keep the flag at its
+ # ``False`` default and continue to inherit shell env vars.
+ config.agents.claude.auth_mode_set = True
config.agents.claude.api_key = api_key if auth_mode == "api_key" else None
if base_url_present:
config.agents.claude.base_url = base_url_change
diff --git a/vibe/claude_config.py b/vibe/claude_config.py
index e1afd0ab8..f39d99261 100644
--- a/vibe/claude_config.py
+++ b/vibe/claude_config.py
@@ -235,24 +235,28 @@ def build_claude_subprocess_env(
auth_mode = getattr(claude_cfg, "auth_mode", "oauth")
configured_key_raw = (getattr(claude_cfg, "api_key", None) or "").strip()
configured_base = (getattr(claude_cfg, "base_url", None) or "").strip()
+ # ``auth_mode_set`` is False on V2 configs that predate the Settings
+ # → Backends → Claude page (or that the user has simply never
+ # opened). On those installs the user is running on shell-exported
+ # ``ANTHROPIC_*`` vars and we must preserve them — stripping would
+ # break working legacy deployments. Once the user saves any auth
+ # choice through Settings (api_key save, OAuth save, Sign out,
+ # Remove key), the writer flips this to True and we honor
+ # ``auth_mode`` strictly.
+ auth_mode_set = bool(getattr(claude_cfg, "auth_mode_set", False))
if auth_mode == "oauth":
- # ``ClaudeConfig.auth_mode`` defaults to ``"oauth"`` in the
- # schema, so an upgraded install that never touched the new
- # Settings UI ends up here too — even though the user is still
- # running on env-var auth (legacy path: ``ANTHROPIC_API_KEY``
- # exported from the shell). Stripping in that case would break
- # working deployments. Only strip when there is some explicit
- # signal that the user has committed to OAuth via the new UI:
- # a stored ``api_key`` (always None in oauth mode, kept as a
- # safety check) or a stored ``base_url`` (set by an api_key →
- # oauth transition where we preserved the relay URL). When
- # nothing is stored, treat the config as legacy / unset and
- # forward whatever the shell exported.
- has_explicit_signal = bool(configured_key_raw) or bool(configured_base)
- if has_explicit_signal:
+ if auth_mode_set:
+ # Explicit OAuth pick from the UI: Claude Code must read
+ # credentials from ``~/.claude/credentials.json``. Strip
+ # any inherited API-key / bearer-token vars so they can't
+ # suppress that path. Covers the "Sign out", "Remove
+ # key", and "save OAuth with cleared fields" flows that
+ # all land here with both ``api_key`` and ``base_url``
+ # already empty.
claude_env.pop("ANTHROPIC_API_KEY", None)
claude_env.pop("ANTHROPIC_AUTH_TOKEN", None)
+ # else: legacy install — preserve inherited env vars verbatim.
elif auth_mode == "api_key":
if configured_key_raw:
claude_env["ANTHROPIC_API_KEY"] = configured_key_raw