Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(vlm): resync rotated Codex credentials (#4540)
AirHuac5755f5ae60e
diff --git a/openviking/models/vlm/backends/codex_auth.py b/openviking/models/vlm/backends/codex_auth.pyindex fdeb37e63..50a2ee103 100644--- a/openviking/models/vlm/backends/codex_auth.py+++ b/openviking/models/vlm/backends/codex_auth.py@@ -552,7 +552,6 @@ def has_codex_auth_available() -> bool:)-def resolve_codex_runtime_credentials(*,force_refresh: bool = False,@@ -594,15 +593,20 @@ def resolve_codex_runtime_credentials().expanduser()elif _default_codex_auth_path().exists():external_path = _default_codex_auth_path()- external_missing = False- should_resync = force_refresh or (- refresh_if_expiring- and _codex_access_token_is_expiring(access_token, refresh_skew_seconds)- )- if should_resync:- if external_path is not None:+ external_missing = external_path is None or not external_path.exists()+ external_unavailable = False+ external_payload = None+ if external_path is not None and not external_missing:+ if not external_path.is_file():+ external_unavailable = True+ else:external_payload = _load_tokens_from_source("codex-cli", external_path)- if external_payload is not None:+ if external_payload is None:+ external_unavailable = True+ elif (+ external_payload["access_token"] != access_token+ or external_payload["refresh_token"] != refresh_token+ ):_write_tokens_to_ov_store(ov_auth_path,external_payload["access_token"],@@ -615,15 +619,12 @@ def resolve_codex_runtime_credentials(payload = external_payloadaccess_token = payload["access_token"]refresh_token = payload["refresh_token"]- else:- external_missing = True- else:- external_missing = True- should_refresh = force_refresh or (- refresh_if_expiring- and _codex_access_token_is_expiring(access_token, refresh_skew_seconds)+ should_refresh = refresh_if_expiring and _codex_access_token_is_expiring(+ access_token, refresh_skew_seconds)+ if force_refresh and external_missing:+ should_refresh = Trueif should_refresh and external_missing:refreshed = refresh_codex_oauth(refresh_token,@@ -650,6 +651,12 @@ def resolve_codex_runtime_credentials("auth_owner": CODEX_AUTH_OWNER_OPENVIKING,}+ if (force_refresh or should_refresh) and external_unavailable:+ raise CodexAuthError(+ "Externally managed Codex auth exists but could not be read. "+ "Retry after Codex CLI finishes updating it or re-run openviking-server init."+ )+if should_refresh:raise CodexAuthError("Externally managed Codex auth is expiring. Refresh it via Codex CLI or re-run openviking-server init."diff --git a/openviking/models/vlm/backends/codex_responses_adapter.py b/openviking/models/vlm/backends/codex_responses_adapter.pyindex f650de40c..1e1f3e597 100644--- a/openviking/models/vlm/backends/codex_responses_adapter.py+++ b/openviking/models/vlm/backends/codex_responses_adapter.py@@ -227,9 +227,15 @@ def _build_final_response_from_stream_events(class CodexCompletionsAdapter:- def __init__(self, client_factory: Callable[[], Any], model: str):+ def __init__(+ self,+ client_factory: Callable[[], Any],+ model: str,+ auth_retry_client_factory: Optional[Callable[[], Any]] = None,+ ):self._client_factory = client_factoryself._model = model+ self._auth_retry_client_factory = auth_retry_client_factorydef _create_response(self, **kwargs) -> Any:client = self._client_factory()@@ -263,7 +269,16 @@ class CodexCompletionsAdapter:collected_text_deltas: List[str] = []has_function_calls = Falsecompleted_response = None- stream = client.responses.create(**response_kwargs, stream=True)+ try:+ stream = client.responses.create(**response_kwargs, stream=True)+ except Exception as exc:+ status_code = getattr(exc, "status_code", None)+ if status_code is None:+ status_code = getattr(getattr(exc, "response", None), "status_code", None)+ if status_code != 401 or self._auth_retry_client_factory is None:+ raise+ client = self._auth_retry_client_factory()+ stream = client.responses.create(**response_kwargs, stream=True)try:for event in stream:event_type = getattr(event, "type", "")diff --git a/openviking/models/vlm/backends/codex_vlm.py b/openviking/models/vlm/backends/codex_vlm.pyindex c045469a8..d09faf76b 100644--- a/openviking/models/vlm/backends/codex_vlm.py+++ b/openviking/models/vlm/backends/codex_vlm.py@@ -57,12 +57,24 @@ class CodexVLM(OpenAIVLM):)return openai.OpenAI(**kwargs)+ def _build_completions_adapter(self) -> CodexCompletionsAdapter:+ auth_retry_client_factory = (+ self._build_refreshed_responses_client+ if not str(self.config.get("api_key", "") or "").strip()+ else None+ )+ return CodexCompletionsAdapter(+ lambda: self._build_responses_client(*self._resolve_runtime_credentials()),+ self.model or "gpt-5.3-codex",+ auth_retry_client_factory=auth_retry_client_factory,+ )++ def _build_refreshed_responses_client(self):+ return self._build_responses_client(*self._resolve_runtime_credentials(force_refresh=True))+def _get_or_create_sync_responses_client(self):if self._sync_client is None:- adapter = CodexCompletionsAdapter(- lambda: self._build_responses_client(*self._resolve_runtime_credentials()),- self.model or "gpt-5.3-codex",- )+ adapter = self._build_completions_adapter()self._sync_client = SimpleNamespace(chat=CodexChatShim(adapter))return self._sync_client@@ -70,23 +82,24 @@ class CodexVLM(OpenAIVLM):# The async path uses a sync Responses client behind asyncio.to_thread so# credential refresh and auth-store I/O do not block the event loop.if self._async_client is None:- sync_adapter = CodexCompletionsAdapter(- lambda: self._build_responses_client(*self._resolve_runtime_credentials()),- self.model or "gpt-5.3-codex",- )+ sync_adapter = self._build_completions_adapter()self._async_client = SimpleNamespace(chat=CodexAsyncChatShim(CodexAsyncCompletionsAdapter(sync_adapter)))return self._async_client- def _resolve_runtime_credentials(self) -> tuple[str, str]:+ def _resolve_runtime_credentials(self, *, force_refresh: bool = False) -> tuple[str, str]:explicit_api_key = str(self.config.get("api_key", "") or "").strip()explicit_api_base = str(self.config.get("api_base", "") or "").strip().rstrip("/")if explicit_api_key:self.api_key = explicit_api_keyself.api_base = explicit_api_base or DEFAULT_CODEX_BASE_URLreturn self.api_key, self.api_base- credentials = resolve_codex_runtime_credentials()+ credentials = (+ resolve_codex_runtime_credentials(force_refresh=True)+ if force_refresh+ else resolve_codex_runtime_credentials()+ )self.api_key = credentials["api_key"]self.api_base = explicit_api_base or credentials["base_url"]return self.api_key, self.api_basediff --git a/tests/unit/test_codex_vlm.py b/tests/unit/test_codex_vlm.pyindex 664b7012f..69ff92af9 100644--- a/tests/unit/test_codex_vlm.py+++ b/tests/unit/test_codex_vlm.py@@ -4,7 +4,7 @@import base64import jsonfrom types import SimpleNamespace-from unittest.mock import MagicMock, patch+from unittest.mock import MagicMock, call, patchimport pytest@@ -302,6 +302,126 @@ def test_codex_auth_bootstraps_into_openviking_store(tmp_path, monkeypatch):assert persisted["imported_from"] == str(bootstrap_path)+def test_codex_auth_resyncs_external_tokens_before_cached_token_expires(tmp_path, monkeypatch):+ ov_auth_path = tmp_path / "codex_auth.json"+ bootstrap_path = tmp_path / "codex_cli_auth.json"+ old_access_token = _make_jwt_token({"exp": 9999999999, "version": "old"})+ new_access_token = _make_jwt_token({"exp": 9999999999, "version": "new"})+ ov_auth_path.write_text(+ json.dumps(+ {+ "auth_owner": "external",+ "imported_from": str(bootstrap_path),+ "tokens": {+ "access_token": old_access_token,+ "refresh_token": "old-refresh",+ },+ }+ ),+ encoding="utf-8",+ )+ bootstrap_path.write_text(+ json.dumps(+ {+ "tokens": {+ "access_token": new_access_token,+ "refresh_token": "new-refresh",+ }+ }+ ),+ encoding="utf-8",+ )+ monkeypatch.setenv("OPENVIKING_CODEX_AUTH_PATH", str(ov_auth_path))++ creds = resolve_codex_runtime_credentials()++ assert creds["api_key"] == new_access_token+ persisted = json.loads(ov_auth_path.read_text(encoding="utf-8"))+ assert persisted["tokens"]["refresh_token"] == "new-refresh"+++def test_codex_auth_force_refresh_resyncs_external_credentials_once(tmp_path, monkeypatch):+ ov_auth_path = tmp_path / "codex_auth.json"+ bootstrap_path = tmp_path / "codex_cli_auth.json"+ old_access_token = _make_jwt_token({"exp": 9999999999, "version": "old"})+ new_access_token = _make_jwt_token({"exp": 9999999999, "version": "new"})+ payload = {"tokens": {"access_token": new_access_token, "refresh_token": "new-refresh"}}+ bootstrap_path.write_text(json.dumps(payload), encoding="utf-8")+ ov_auth_path.write_text(+ json.dumps(+ {+ "tokens": {+ "access_token": old_access_token,+ "refresh_token": "old-refresh",+ },+ "auth_owner": "external",+ "imported_from": str(bootstrap_path),+ }+ ),+ encoding="utf-8",+ )+ monkeypatch.setenv("OPENVIKING_CODEX_AUTH_PATH", str(ov_auth_path))++ with patch.object(+ codex_auth,+ "_write_tokens_to_ov_store",+ wraps=codex_auth._write_tokens_to_ov_store,+ ) as mock_write:+ creds = resolve_codex_runtime_credentials(force_refresh=True)++ assert creds["api_key"] == new_access_token+ assert creds["auth_owner"] == "external"+ assert mock_write.call_count == 1+++def test_codex_auth_parse_failure_preserves_external_owner(tmp_path, monkeypatch):+ ov_auth_path = tmp_path / "codex_auth.json"+ bootstrap_path = tmp_path / "codex_cli_auth.json"+ old_access_token = _make_jwt_token({"exp": 9999999999, "version": "old"})+ new_access_token = _make_jwt_token({"exp": 9999999999, "version": "new"})+ ov_payload = {+ "tokens": {+ "access_token": old_access_token,+ "refresh_token": "old-refresh",+ },+ "auth_owner": "external",+ "imported_from": str(bootstrap_path),+ }+ ov_auth_path.write_text(json.dumps(ov_payload), encoding="utf-8")+ bootstrap_path.write_text("{invalid", encoding="utf-8")+ monkeypatch.setenv("OPENVIKING_CODEX_AUTH_PATH", str(ov_auth_path))++ with patch.object(codex_auth, "refresh_codex_oauth") as mock_refresh:+ with pytest.raises(codex_auth.CodexAuthError, match="exists but could not be read"):+ resolve_codex_runtime_credentials(force_refresh=True)++ persisted = json.loads(ov_auth_path.read_text(encoding="utf-8"))+ assert persisted["auth_owner"] == "external"+ assert persisted["imported_from"] == str(bootstrap_path)+ assert persisted["tokens"]["refresh_token"] == "old-refresh"+ mock_refresh.assert_not_called()++ bootstrap_path.write_text(+ json.dumps(+ {+ "tokens": {+ "access_token": new_access_token,+ "refresh_token": "new-refresh",+ }+ }+ ),+ encoding="utf-8",+ )++ recovered = resolve_codex_runtime_credentials()++ assert recovered["api_key"] == new_access_token+ assert recovered["auth_owner"] == "external"+ recovered_persisted = json.loads(ov_auth_path.read_text(encoding="utf-8"))+ assert recovered_persisted["auth_owner"] == "external"+ assert recovered_persisted["imported_from"] == str(bootstrap_path)++def test_codex_auth_native_login_defaults_to_openviking_owner(tmp_path, monkeypatch):ov_auth_path = tmp_path / "codex_auth.json"monkeypatch.setenv("OPENVIKING_CODEX_AUTH_PATH", str(ov_auth_path))@@ -501,6 +621,56 @@ def test_codex_sync_client_re_resolves_credentials_per_request(mock_resolve, mocassert mock_openai_class.call_args_list[1].kwargs["api_key"] == "oauth-token-b"+@patch("openviking.models.vlm.backends.codex_vlm.openai.OpenAI")+@patch("openviking.models.vlm.backends.codex_vlm.resolve_codex_runtime_credentials")+def test_codex_retries_unauthorized_once_with_refreshed_credentials(+ mock_resolve, mock_openai_class+):+ class _UnauthorizedError(Exception):+ status_code = 401++ mock_resolve.side_effect = [+ {"api_key": "stale-token", "base_url": "https://example.test"},+ {"api_key": "fresh-token", "base_url": "https://example.test"},+ ]+ stale_client = MagicMock()+ stale_client.responses.create.side_effect = _UnauthorizedError()+ fresh_client = _build_fake_openai_client("recovered")+ mock_openai_class.side_effect = [stale_client, fresh_client]++ vlm = CodexVLM({"provider": "openai-codex", "model": "gpt-5.3-codex"})++ assert vlm.get_completion("hello") == "recovered"+ assert mock_resolve.call_args_list == [call(), call(force_refresh=True)]+ stale_client.responses.create.assert_called_once()+ fresh_client.responses.create.assert_called_once()+++@patch("openviking.models.vlm.backends.codex_vlm.openai.OpenAI")+@patch("openviking.models.vlm.backends.codex_vlm.resolve_codex_runtime_credentials")+def test_codex_does_not_retry_a_second_unauthorized_response(mock_resolve, mock_openai_class):+ class _UnauthorizedError(Exception):+ status_code = 401++ mock_resolve.side_effect = [+ {"api_key": "stale-token", "base_url": "https://example.test"},+ {"api_key": "still-invalid-token", "base_url": "https://example.test"},+ ]+ first_client = MagicMock()+ second_client = MagicMock()+ first_client.responses.create.side_effect = _UnauthorizedError()+ second_client.responses.create.side_effect = _UnauthorizedError()+ mock_openai_class.side_effect = [first_client, second_client]+ vlm = CodexVLM({"provider": "openai-codex", "model": "gpt-5.3-codex"})++ with pytest.raises(_UnauthorizedError):+ vlm.get_completion("hello")++ assert mock_resolve.call_args_list == [call(), call(force_refresh=True)]+ first_client.responses.create.assert_called_once()+ second_client.responses.create.assert_called_once()++@patch("openviking.models.vlm.backends.codex_vlm.openai.OpenAI")@patch("openviking.models.vlm.backends.codex_vlm.resolve_codex_runtime_credentials")def test_codex_translates_tool_history_into_responses_input(mock_resolve, mock_openai_class):