Skip to content

Your lightweight open-source personal agent.

25.4 KB
Raw Blame
from __future__ import annotations

import base64
import json
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from types import SimpleNamespace

import httpx
import pytest

from nanobot.providers.oauth_model_catalog import (
    OAuthCatalogAuthRequiredError,
    OAuthModelCatalog,
    get_oauth_model_catalog,
    invalidate_oauth_model_catalog,
)
from nanobot.providers.openai_codex_provider import DEFAULT_OPENAI_CODEX_MODELS_URL
from nanobot.providers.registry import ProviderModelSpec
from nanobot.providers.xai_grok_provider import DEFAULT_XAI_GROK_MODELS_URL
from nanobot.providers.xai_oauth import XAIToken


@pytest.fixture(autouse=True)
def _clear_oauth_catalogs() -> None:
    for provider in ("openai_codex", "xai_grok", "github_copilot"):
        invalidate_oauth_model_catalog(provider)
    yield
    for provider in ("openai_codex", "xai_grok", "github_copilot"):
        invalidate_oauth_model_catalog(provider)


def _fallback_model() -> ProviderModelSpec:
    return ProviderModelSpec(id="provider/fallback", label="Fallback")


def test_xai_catalog_fetches_remote_models_and_reuses_capability_metadata(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
) -> None:
    original_client = httpx.Client
    captured: dict[str, object] = {}
    payload = (
        base64.urlsafe_b64encode(
            json.dumps({"sub": "user-42", "email": "[email protected]"}).encode()
        )
        .decode()
        .rstrip("=")
    )
    token = XAIToken(
        access=f"header.{payload}.signature",
        refresh="refresh-token",
        expires=int(time.time() * 1000) + 3_600_000,
        account_id="[email protected]",
    )

    def handler(request: httpx.Request) -> httpx.Response:
        captured["request"] = request
        return httpx.Response(
            200,
            json={
                "data": [
                    {
                        "id": "grok-4.6",
                        "name": "Grok 4.6",
                        "description": "Latest frontier model",
                        "owned_by": "xAI",
                        "context_window": 500_000,
                        "supports_backend_search": True,
                        "reasoning_efforts": [
                            {"value": "xhigh"},
                            {"value": "high"},
                            {"value": "low"},
                        ],
                    },
                    {
                        "id": "grok-next",
                        "_meta": {
                            "name": "Grok Next",
                            "context_window": 750_000,
                            "reasoning_efforts": ["high", "low"],
                        },
                    },
                ]
            },
            request=request,
        )

    def fake_client(**kwargs: object) -> httpx.Client:
        captured["kwargs"] = kwargs
        return original_client(
            transport=httpx.MockTransport(handler),
            timeout=kwargs["timeout"],
            follow_redirects=kwargs["follow_redirects"],
        )

    monkeypatch.setattr(
        "nanobot.providers.xai_grok_provider.get_xai_oauth_storage_path",
        lambda: tmp_path / "auth" / "xai.json",
    )
    monkeypatch.setattr(
        "nanobot.providers.xai_grok_provider.get_xai_oauth_login_status",
        lambda: token,
    )
    monkeypatch.setattr(
        "nanobot.providers.xai_grok_provider.get_xai_oauth_token",
        lambda **_kwargs: token,
    )
    monkeypatch.setattr("nanobot.providers.xai_grok_provider.httpx.Client", fake_client)

    catalog = get_oauth_model_catalog("xai_grok")

    assert catalog.source == "remote"
    assert [model.id for model in catalog.models] == [
        "xai-grok/grok-4.6",
        "xai-grok/grok-next",
    ]
    grok = catalog.find("grok-4.6")
    assert grok is not None
    assert grok.description == "Latest frontier model"
    assert grok.context_window == 500_000
    assert grok.reasoning_efforts == ("xhigh", "high", "low")
    assert grok.supports_backend_search is True
    next_model = catalog.find("xai-grok/grok-next")
    assert next_model is not None
    assert next_model.label == "Grok Next"
    assert next_model.context_window == 750_000
    assert next_model.reasoning_efforts == ("high", "low")

    request = captured["request"]
    assert isinstance(request, httpx.Request)
    assert str(request.url) == DEFAULT_XAI_GROK_MODELS_URL
    assert request.headers["Authorization"] == f"Bearer {token.access}"
    assert request.headers["X-XAI-Token-Auth"] == "xai-grok-cli"
    assert request.headers["x-userid"] == "user-42"
    assert request.headers["x-email"] == "[email protected]"
    assert captured["kwargs"] == {"timeout": 10.0, "follow_redirects": False}
    assert get_oauth_model_catalog("xai_grok").source == "cache"


@pytest.mark.parametrize(
    ("model_id", "label"),
    [
        ("gpt-new", "GPT New"),
        ("gpt-6-astra", "GPT-6-Astra"),
        ("gpt-6-sol", "GPT-6-Sol"),
        ("gpt-6-luna", "GPT-6-Luna"),
    ],
)
def test_openai_codex_catalog_uses_account_catalog_and_filters_hidden_models(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
    model_id: str,
    label: str,
) -> None:
    original_client = httpx.Client
    captured: dict[str, object] = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["request"] = request
        return httpx.Response(
            200,
            json={
                "models": [
                    {
                        "slug": model_id,
                        "display_name": label,
                        "description": "New model",
                        "context_window": 300_000,
                        "priority": 2,
                        "visibility": "list",
                        "supported_reasoning_levels": [
                            {"effort": "low"},
                            {"effort": "high"},
                        ],
                    },
                    {
                        "slug": "gpt-first",
                        "display_name": "GPT First",
                        "priority": 1,
                    },
                    {
                        "slug": "internal-model",
                        "display_name": "Internal",
                        "visibility": "hide",
                        "priority": 0,
                    },
                ]
            },
            request=request,
        )

    def fake_client(**kwargs: object) -> httpx.Client:
        captured["kwargs"] = kwargs
        return original_client(
            transport=httpx.MockTransport(handler),
            timeout=kwargs["timeout"],
            follow_redirects=kwargs["follow_redirects"],
        )

    class Storage:
        def load(self) -> SimpleNamespace:
            return SimpleNamespace(access="secret", account_id="account-42")

        def get_token_path(self) -> Path:
            return tmp_path / "auth" / "openai-codex.json"

    monkeypatch.setattr(
        "nanobot.providers.openai_codex_provider.FileTokenStorage",
        lambda **_kwargs: Storage(),
    )
    monkeypatch.setattr(
        "nanobot.providers.openai_codex_provider.get_codex_token",
        lambda **_kwargs: SimpleNamespace(access="secret", account_id="account-42"),
    )
    monkeypatch.setattr("nanobot.providers.openai_codex_provider.httpx.Client", fake_client)

    catalog = get_oauth_model_catalog("openai_codex")

    assert catalog.source == "remote"
    assert [model.id for model in catalog.models] == [
        "openai-codex/gpt-first",
        f"openai-codex/{model_id}",
    ]
    assert catalog.models[1].label == label
    assert catalog.models[1].context_window == 300_000
    assert catalog.models[1].reasoning_efforts == ("low", "high")
    request = captured["request"]
    assert isinstance(request, httpx.Request)
    assert request.url.copy_with(query=None) == httpx.URL(DEFAULT_OPENAI_CODEX_MODELS_URL)
    assert request.url.params["client_version"] == "99.99.99"
    assert request.headers["Authorization"] == "Bearer secret"
    assert request.headers["chatgpt-account-id"] == "account-42"


@pytest.mark.parametrize("detail", [
    'Token refresh failed: 400 {"error":"invalid_grant","access_token":"synthetic-secret"}',
    "OAuth credentials not found. Please run the login command.",
])
def test_codex_inference_classifies_sdk_reauth_without_exposing_response(detail: str) -> None:
    from nanobot.providers.openai_codex_provider import _codex_error_response

    response = _codex_error_response(RuntimeError(detail))
    assert response.error_kind == "oauth_auth_required"
    assert response.error_should_retry is False
    assert "synthetic-secret" not in str(response)


def test_xai_inference_classifies_typed_reauth() -> None:
    from nanobot.providers.xai_grok_provider import _xai_error_response
    from nanobot.providers.xai_oauth import XAIOAuthReauthRequiredError

    response = _xai_error_response(XAIOAuthReauthRequiredError("synthetic-secret"))
    assert response.error_kind == "oauth_auth_required"
    assert response.error_should_retry is False
    assert "synthetic-secret" not in str(response)


@pytest.mark.parametrize(
    "responses_model",
    ["gpt-5.4-mini", "gpt-6-astra", "gpt-6-sol", "gpt-6-luna"],
)
def test_github_copilot_catalog_routes_advertised_model_apis(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
    responses_model: str,
) -> None:
    original_client = httpx.Client
    captured: list[httpx.Request] = []

    def handler(request: httpx.Request) -> httpx.Response:
        captured.append(request)
        if request.url.path.endswith("/copilot_internal/v2/token"):
            return httpx.Response(
                200,
                json={
                    "token": "copilot-secret",
                    "endpoints": {"api": "https://api.individual.githubcopilot.com"},
                },
                request=request,
            )
        return httpx.Response(
            200,
            json={
                "data": [
                    {
                        "id": "claude-sonnet",
                        "name": "Claude Sonnet",
                        "model_picker_enabled": True,
                        "policy": {"state": "enabled"},
                        "supported_endpoints": ["/chat/completions"],
                        "capabilities": {
                            "supports": {"reasoning_effort": ["low", "high"]},
                            "limits": {"max_context_window_tokens": 200_000},
                        },
                    },
                    {
                        "id": responses_model,
                        "model_picker_enabled": True,
                        "supported_endpoints": ["/responses"],
                    },
                    {
                        "id": "unknown-responses-only",
                        "name": "Unknown Responses only",
                        "model_picker_enabled": True,
                        "supported_endpoints": ["/responses"],
                    },
                    {
                        "id": "disabled",
                        "model_picker_enabled": True,
                        "policy": {"state": "disabled"},
                        "supported_endpoints": ["/chat/completions"],
                    },
                ]
            },
            request=request,
        )

    def fake_client(**kwargs: object) -> httpx.Client:
        return original_client(
            transport=httpx.MockTransport(handler),
            timeout=kwargs["timeout"],
            follow_redirects=kwargs["follow_redirects"],
        )

    class Storage:
        def load(self) -> SimpleNamespace:
            return SimpleNamespace(access="github-secret", account_id="octocat")

        def get_token_path(self) -> Path:
            return tmp_path / "auth" / "github-copilot.json"

    monkeypatch.setattr(
        "nanobot.providers.github_copilot_provider.get_storage",
        lambda: Storage(),
    )
    monkeypatch.setattr("nanobot.providers.github_copilot_provider.httpx.Client", fake_client)

    catalog = get_oauth_model_catalog("github_copilot")

    assert catalog.source == "remote"
    assert [model.id for model in catalog.models] == [
        "github-copilot/claude-sonnet",
        f"github-copilot/{responses_model}",
        "github-copilot/unknown-responses-only",
    ]
    assert catalog.models[0].context_window == 200_000
    assert catalog.models[0].reasoning_efforts == ("low", "high")
    assert catalog.models[0].api.supported_apis == ("chat_completions",)
    assert catalog.models[2].api.supported_apis == ("responses",)
    from nanobot.providers.github_copilot_provider import GitHubCopilotProvider

    provider = GitHubCopilotProvider(default_model="github-copilot/unknown-responses-only")
    assert provider._should_use_responses_api(None, None)
    assert not provider._should_use_responses_api("github-copilot/claude-sonnet", "high")
    from nanobot.config.schema import Config, ModelPresetConfig
    from nanobot.providers.factory import resolve_automatic_model_api

    for model, effort, expected in (
        ("github-copilot/unknown-responses-only", None, "responses"),
        ("github-copilot/claude-sonnet", "high", "chat_completions"),
    ):
        assert resolve_automatic_model_api(Config(), preset=ModelPresetConfig(
            provider="github_copilot", model=model, reasoning_effort=effort,
        )) == ("github_copilot", expected)
    assert len(captured) == 2
    assert captured[0].headers["Authorization"] == "token github-secret"
    assert captured[1].headers["Authorization"] == "Bearer copilot-secret"
    assert str(captured[1].url) == "https://api.individual.githubcopilot.com/models"
    assert get_oauth_model_catalog("github_copilot").source == "cache"
    assert get_oauth_model_catalog(
        "github_copilot",
        proxy="http://proxy.example:8080",
    ).source == "remote"
    assert len(captured) == 4


def test_explicit_refresh_bypasses_cached_models_and_retry_delay() -> None:
    calls = 0

    def fetch(_proxy):
        nonlocal calls
        calls += 1
        if calls == 2:
            request = httpx.Request("GET", "https://example.com/models")
            raise httpx.HTTPStatusError("expired", request=request, response=httpx.Response(401))
        return (ProviderModelSpec(id=f"provider/model-{calls}"),)

    catalog = OAuthModelCatalog(fallback_models=(), fetch=fetch)
    assert catalog.get(cache_key="account").models[0].id == "provider/model-1"
    assert catalog.get(cache_key="account").source == "cache"
    assert calls == 1

    failed = catalog.get(cache_key="account", refresh=True)
    assert failed.source == "stale"
    assert failed.error_kind == "auth_required"
    assert catalog.get(cache_key="account").error_kind == "auth_required"
    assert calls == 2

    refreshed = catalog.get(cache_key="account", refresh=True)
    assert refreshed.source == "remote"
    assert refreshed.error_kind is None
    assert refreshed.models[0].id == "provider/model-3"
    assert calls == 3


def test_catalog_single_flights_concurrent_refreshes() -> None:
    calls = 0
    calls_lock = threading.Lock()
    barrier = threading.Barrier(8)

    def fetch(_proxy: str | None) -> tuple[ProviderModelSpec, ...]:
        nonlocal calls
        with calls_lock:
            calls += 1
        time.sleep(0.05)
        return (ProviderModelSpec(id="provider/remote", label="Remote"),)

    catalog = OAuthModelCatalog(fallback_models=(_fallback_model(),), fetch=fetch)

    def get_catalog(_index: int):
        barrier.wait()
        return catalog.get(cache_key="shared")

    with ThreadPoolExecutor(max_workers=8) as pool:
        results = list(pool.map(get_catalog, range(8)))

    assert calls == 1
    assert {result.models[0].id for result in results} == {"provider/remote"}
    assert [result.source for result in results].count("remote") == 1
    assert [result.source for result in results].count("cache") == 7


def test_catalog_invalidation_discards_an_inflight_account_refresh() -> None:
    started = threading.Event()
    release = threading.Event()
    identity = ["old-account"]

    def fetch(_proxy: str | None) -> tuple[ProviderModelSpec, ...]:
        current = identity[0]
        if current == "old-account":
            started.set()
            assert release.wait(timeout=2)
        return (ProviderModelSpec(id=f"provider/{current}", label=current),)

    catalog = OAuthModelCatalog(fallback_models=(_fallback_model(),), fetch=fetch)
    with ThreadPoolExecutor(max_workers=2) as pool:
        old_future = pool.submit(catalog.get, cache_key="old-key")
        assert started.wait(timeout=2)
        identity[0] = "new-account"
        catalog.invalidate()
        new_future = pool.submit(catalog.get, cache_key="new-key")
        new_result = new_future.result(timeout=2)
        release.set()
        old_result = old_future.result(timeout=2)

    assert old_result.source == "fallback"
    assert new_result.models[0].id == "provider/new-account"

    identity[0] = "old-account"
    assert catalog.get(cache_key="old-key").models[0].id == "provider/old-account"


def test_catalog_bounds_failure_only_keys() -> None:
    calls = 0

    def fetch(_proxy: str | None) -> tuple[ProviderModelSpec, ...]:
        nonlocal calls
        calls += 1
        raise httpx.ConnectError("offline")

    # Real monotonic time can serve identical timestamps to all three
    # lookups, making the eviction victim in _reserve() depend on set
    # iteration order (hash-seeded). A synthetic strictly-increasing clock
    # pins the oldest entry deterministically.
    ticks = iter(range(10_000))
    catalog = OAuthModelCatalog(
        fallback_models=(_fallback_model(),),
        fetch=fetch,
        max_entries=2,
        monotonic=lambda: float(next(ticks)),
    )

    for key in ("one", "two", "three"):
        assert catalog.get(cache_key=key).source == "fallback"

    assert calls == 3
    assert catalog.get(cache_key="one").source == "fallback"
    assert calls == 4


def test_catalog_returns_stale_then_negative_caches_refresh_failure() -> None:
    now = [0.0]
    calls = 0

    def fetch(_proxy: str | None) -> tuple[ProviderModelSpec, ...]:
        nonlocal calls
        calls += 1
        if calls > 1:
            raise httpx.ConnectError("offline")
        return (ProviderModelSpec(id="provider/remote", label="Remote"),)

    catalog = OAuthModelCatalog(
        fallback_models=(_fallback_model(),),
        fetch=fetch,
        fresh_ttl_s=10,
        stale_ttl_s=100,
        failure_ttl_s=30,
        monotonic=lambda: now[0],
        wall_clock=lambda: 123.0,
    )

    assert catalog.get(cache_key="one").source == "remote"
    now[0] = 11
    stale = catalog.get(cache_key="one")
    assert stale.source == "stale"
    assert stale.models[0].id == "provider/remote"
    assert catalog.get(cache_key="one").source == "stale"
    assert calls == 2

    now[0] = 101
    fallback = catalog.get(cache_key="one")
    assert fallback.source == "fallback"
    assert fallback.models[0].id == "provider/fallback"
    assert calls == 3


@pytest.mark.parametrize(
    "failure",
    [
        httpx.ConnectError("offline"),
        ValueError("invalid JSON"),
        httpx.HTTPStatusError(
            "unauthorized",
            request=httpx.Request("GET", DEFAULT_XAI_GROK_MODELS_URL),
            response=httpx.Response(401),
        ),
        httpx.HTTPStatusError(
            "rate limited",
            request=httpx.Request("GET", DEFAULT_XAI_GROK_MODELS_URL),
            response=httpx.Response(429),
        ),
        httpx.HTTPStatusError(
            "upstream failure",
            request=httpx.Request("GET", DEFAULT_XAI_GROK_MODELS_URL),
            response=httpx.Response(503),
        ),
    ],
)
def test_catalog_falls_back_for_remote_failures(failure: Exception) -> None:
    calls = 0

    def fetch(_proxy: str | None) -> tuple[ProviderModelSpec, ...]:
        nonlocal calls
        calls += 1
        raise failure

    catalog = OAuthModelCatalog(
        fallback_models=(_fallback_model(),),
        fetch=fetch,
        failure_ttl_s=30,
    )

    first = catalog.get(cache_key="one")
    second = catalog.get(cache_key="one")

    assert first.source == "fallback"
    assert second.source == "fallback"
    assert first.models == (_fallback_model(),)
    assert calls == 1


def test_catalog_treats_empty_remote_list_as_failure_and_can_be_invalidated() -> None:
    calls = 0

    def fetch(_proxy: str | None) -> tuple[ProviderModelSpec, ...]:
        nonlocal calls
        calls += 1
        return () if calls == 1 else (ProviderModelSpec(id="provider/new", label="New"),)

    catalog = OAuthModelCatalog(
        fallback_models=(_fallback_model(),),
        fetch=fetch,
        failure_ttl_s=30,
    )

    assert catalog.get(cache_key="one").source == "fallback"
    catalog.invalidate()
    refreshed = catalog.get(cache_key="one")
    assert refreshed.source == "remote"
    assert refreshed.models[0].id == "provider/new"
    assert calls == 2


@pytest.mark.parametrize("with_cache", [False, True])
@pytest.mark.parametrize(
    ("status", "body", "expected"),
    [
        (401, {"error": {"code": "token_revoked"}}, "auth_required"),
        (401, "non-json", "auth_required"),
        (400, {"error": "invalid_grant"}, "auth_required"),
        (403, {"error": {"code": "token_revoked"}}, "auth_required"),
        (403, {"error": "insufficient_scope"}, "unavailable"),
        (429, {"error": "invalid_grant"}, "unavailable"),
        (503, {}, "unavailable"),
    ],
)
def test_catalog_retains_safe_error_kind_until_reauthentication(
    with_cache: bool, status: int, body: object, expected: str,
) -> None:
    from loguru import logger

    now = [0.0]
    failing = [not with_cache]
    calls = 0
    sentinel = "private-token-and-account-fixture"
    logs: list[str] = []

    def fetch(_proxy):
        nonlocal calls
        calls += 1
        if failing[0]:
            request = httpx.Request("GET", f"https://example.com/{sentinel}")
            response = httpx.Response(status, json=body, request=request)
            raise httpx.HTTPStatusError(sentinel, request=request, response=response)
        return (ProviderModelSpec(id="provider/new"),)

    catalog = OAuthModelCatalog(
        fallback_models=(_fallback_model(),), fetch=fetch,
        fresh_ttl_s=10, stale_ttl_s=100, monotonic=lambda: now[0],
    )
    if with_cache:
        assert catalog.get(cache_key="same-account").error_kind is None
        now[0] = 11
        failing[0] = True
    sink = logger.add(lambda message: logs.append(str(message)))
    try:
        first = catalog.get(cache_key="same-account")
        second = catalog.get(cache_key="same-account")
    finally:
        logger.remove(sink)
    assert first.source == ("stale" if with_cache else "fallback")
    assert first.error_kind == second.error_kind == expected
    assert calls == (2 if with_cache else 1)
    assert sentinel not in repr(first) + "".join(logs)
    failing[0] = False
    catalog.invalidate()
    recovered = catalog.get(cache_key="same-account")
    assert recovered.source == "remote"
    assert recovered.error_kind is None
    assert recovered.message is None
    assert recovered.models[0].id == "provider/new"


@pytest.mark.parametrize("failure", [httpx.ConnectError("offline"), httpx.ReadTimeout("slow")])
def test_catalog_network_errors_do_not_request_sign_in(failure: Exception) -> None:
    def fetch(_proxy):
        raise failure

    catalog = OAuthModelCatalog(fallback_models=(_fallback_model(),), fetch=fetch)
    assert catalog.get(cache_key="one").error_kind == "unavailable"


@pytest.mark.parametrize(
    ("message", "reauth"),
    [
        ('Token refresh failed: 400 {"error":"invalid_grant","token":"private"}', True),
        ('Token refresh failed: 401 private', True),
        ('Token refresh failed: 429 {"error":"invalid_grant"}', False),
        ('Token refresh failed: 503 private', False),
        ('network error mentions invalid_grant', False),
        ('OAuth credentials not found. Please run the login command.', True),
    ],
)
def test_codex_catalog_classifies_sdk_refresh_envelope(monkeypatch, message, reauth):
    from nanobot.providers.openai_codex_provider import _fetch_openai_codex_models

    def fail(**_kwargs):
        raise RuntimeError(message)

    monkeypatch.setattr("nanobot.providers.openai_codex_provider.get_codex_token", fail)
    with pytest.raises(RuntimeError) as error:
        _fetch_openai_codex_models(None)
    assert isinstance(error.value, OAuthCatalogAuthRequiredError) is reauth
    if reauth:
        assert "private" not in str(error.value)


def test_xai_catalog_classifies_expired_refresh_credentials(monkeypatch):
    from nanobot.providers.xai_grok_provider import _fetch_xai_grok_models
    from nanobot.providers.xai_oauth import XAIOAuthReauthRequiredError, _oauth_http_error

    failure = _oauth_http_error(
        httpx.Response(400, json={"error": "invalid_grant", "error_description": "private"}),
        "token refresh",
    )
    assert isinstance(failure, XAIOAuthReauthRequiredError)
    assert "private" not in str(failure)

    def fail(**_kwargs):
        raise failure

    monkeypatch.setattr("nanobot.providers.xai_grok_provider.get_xai_oauth_token", fail)
    with pytest.raises(OAuthCatalogAuthRequiredError):
        _fetch_xai_grok_models(None)


@pytest.mark.parametrize("status", [403, 429, 503])
def test_xai_oauth_other_failures_are_not_reauthentication(status):
    from nanobot.providers.xai_oauth import XAIOAuthReauthRequiredError, _oauth_http_error

    failure = _oauth_http_error(httpx.Response(status, json={}), "token refresh")
    assert not isinstance(failure, XAIOAuthReauthRequiredError)