"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(providers): expire cached Copilot CLI token
lorenzozaneef740e906f08b
diff --git a/agent/src/providers/capabilities.py b/agent/src/providers/capabilities.pyindex 74b15cfdd..c17ca1aea 100644--- a/agent/src/providers/capabilities.py+++ b/agent/src/providers/capabilities.py@@ -8,6 +8,8 @@ from dataclasses import dataclass, fieldfrom functools import lru_cachefrom importlib.metadata import PackageNotFoundError, versionfrom pathlib import Path+from threading import Lock+from time import monotonicfrom typing import Mapping, Optional@@ -74,12 +76,28 @@ def _package_version() -> str:_VIBE_USER_AGENT = f"Vibe-Trading/{_package_version()}"-@lru_cache(maxsize=1)+_GH_CLI_TOKEN_TTL_SECONDS = 60.0+_gh_cli_token_cache: tuple[float, str] | None = None+_gh_cli_token_lock = Lock()++def _gh_cli_token() -> str:"""Return an explicit Copilot token; the SDK handles stored credentials."""- from src.providers.copilot_auth import resolve_copilot_token+ global _gh_cli_token_cache++ with _gh_cli_token_lock:+ now = monotonic()+ if (+ _gh_cli_token_cache is not None+ and now - _gh_cli_token_cache[0] < _GH_CLI_TOKEN_TTL_SECONDS+ ):+ return _gh_cli_token_cache[1]++ from src.providers.copilot_auth import resolve_copilot_token- return resolve_copilot_token()[0]+ token = resolve_copilot_token()[0]+ _gh_cli_token_cache = (monotonic(), token)+ return token_MOONSHOT_CAPABILITIES = ProviderCapabilities(diff --git a/agent/tests/test_copilot_provider.py b/agent/tests/test_copilot_provider.pyindex a02afd2a7..d9d716654 100644--- a/agent/tests/test_copilot_provider.py+++ b/agent/tests/test_copilot_provider.py@@ -20,9 +20,9 @@ from src.providers.capabilities import (@pytest.fixture(autouse=True)def _clear_token_cache():- caps_mod._gh_cli_token.cache_clear()+ caps_mod._gh_cli_token_cache = Noneyield- caps_mod._gh_cli_token.cache_clear()+ caps_mod._gh_cli_token_cache = None@pytest.fixture@@ -106,6 +106,31 @@ def test_no_credential_is_left_for_sdk_resolution(no_ambient_credentials) -> Nonassert copilot_auth.resolve_copilot_token() == ("", "")+def test_gh_cli_token_cache_expires_after_sixty_seconds(+ no_ambient_credentials, monkeypatch+) -> None:+ now = [0.0]+ resolutions = []+ tokens = iter(("token-A", "token-B-rotated"))++ def resolve_token():+ resolutions.append(None)+ return next(tokens), "gh auth token"++ monkeypatch.setattr(caps_mod, "monotonic", lambda: now[0], raising=False)+ monkeypatch.setattr(copilot_auth, "resolve_copilot_token", resolve_token)++ first = get_llm_credentials("copilot", "claude-sonnet-5")["api_key"]+ now[0] = 30.0+ within_ttl = get_llm_credentials("copilot", "claude-sonnet-5")["api_key"]+ now[0] = 61.0+ after_expiry = get_llm_credentials("copilot", "claude-sonnet-5")["api_key"]++ assert first == within_ttl == "token-A"+ assert after_expiry == "token-B-rotated"+ assert len(resolutions) == 2++def test_sdk_client_options_keep_stored_cli_credentials_enabled(no_ambient_credentials, monkeypatch) -> None: