credentials.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. """GitHub Copilot credentials: token resolution (COPILOT_GITHUB_TOKEN, GH_TOKEN, GITHUB_TOKEN, the order
  2. the Copilot CLI uses, read through Hermes' secret scope), device-code login, the GitHub→Copilot token
  3. exchange, and :class:`CopilotCredentialsMixin`, which fills core's generic ``ProviderProfile`` credential
  4. seams.
  5. Catalog rule 11 (credentials only through Hermes): this plugin never runs ``gh auth token`` and never
  6. reads the gh CLI's or the Copilot CLI's token files. A GitHub token reaches it only through Hermes:
  7. the env vars above (``.env`` or the process environment) or a ``hermes auth add copilot`` pool row."""
  8. from __future__ import annotations
  9. import contextlib
  10. import hashlib
  11. import json
  12. import logging
  13. import re
  14. import threading
  15. import time
  16. import urllib.parse
  17. import urllib.request
  18. from pathlib import Path
  19. from typing import Any, Optional
  20. from agent.secret_scope import get_secret
  21. from utils import atomic_json_write
  22. logger = logging.getLogger(__name__)
  23. # VS Code's GitHub App client ID: mints ghu_* tokens exchangeable for Copilot API JWTs (needed for
  24. # internal-only models / enterprise endpoints). The opencode App ID mints gho_* tokens that 404.
  25. COPILOT_OAUTH_CLIENT_ID = "Iv1.b507a08c87ecfe98"
  26. _CLASSIC_PAT_PREFIX = "ghp_" # rejected by the Copilot API
  27. # Token families the Copilot API exchanges. Anything else (a random string in GITHUB_TOKEN) used
  28. # to pass validation and then fail downstream with an opaque auth error (#12650).
  29. _SUPPORTED_PREFIXES = ("gho_", "github_pat_", "ghu_")
  30. COPILOT_ENV_VARS = ("COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN")
  31. _DEVICE_CODE_POLL_INTERVAL = 5 # seconds
  32. _DEVICE_CODE_POLL_SAFETY_MARGIN = 3 # seconds
  33. def validate_copilot_token(token: str) -> tuple[bool, str]:
  34. """Validate that a token is usable with the Copilot API."""
  35. token = token.strip()
  36. if not token:
  37. return False, "Empty token"
  38. if token.startswith(_CLASSIC_PAT_PREFIX):
  39. return False, (
  40. "Classic Personal Access Tokens (ghp_*) are not supported by the "
  41. "Copilot API. Use one of:\n"
  42. " → `hermes auth add copilot` to sign in with GitHub (device code)\n"
  43. " → A fine-grained PAT (github_pat_*) with Copilot Requests permission")
  44. if not token.startswith(_SUPPORTED_PREFIXES):
  45. return False, (
  46. "Unsupported GitHub token format for the Copilot API. "
  47. f"Supported token prefixes: {', '.join(_SUPPORTED_PREFIXES)}.")
  48. return True, "OK"
  49. def resolve_copilot_token() -> tuple[str, str]:
  50. """GitHub token for Copilot API use from the first env var holding a supported one → (token, source
  51. env var name); ("", "") when none does."""
  52. for env_var in COPILOT_ENV_VARS:
  53. val = (get_secret(env_var) or "").strip()
  54. if not val:
  55. continue
  56. valid, msg = validate_copilot_token(val)
  57. if valid:
  58. return val, env_var
  59. logger.warning("Token from %s is not supported: %s", env_var, msg)
  60. return "", ""
  61. _DEVICE_CODE_TERMINAL_ERRORS = {"expired_token": " ✗ Device code expired. Please try again.",
  62. "access_denied": " ✗ Authorization was denied."}
  63. def _post_form(url: str, fields: dict, timeout: float) -> dict:
  64. req = urllib.request.Request(
  65. url, data=urllib.parse.urlencode(fields).encode(),
  66. headers={"Accept": "application/json", "User-Agent": "HermesAgent/1.0",
  67. "Content-Type": "application/x-www-form-urlencoded"})
  68. with urllib.request.urlopen(req, timeout=timeout) as resp:
  69. return json.loads(resp.read().decode())
  70. def copilot_device_code_login(
  71. *, host: str = "github.com", timeout_seconds: float = 300) -> Optional[str]:
  72. """Run the GitHub OAuth device code flow for Copilot."""
  73. domain = host.rstrip("/")
  74. try:
  75. device_data = _post_form(f"https://{domain}/login/device/code",
  76. {"client_id": COPILOT_OAUTH_CLIENT_ID, "scope": "read:user"}, 15)
  77. except Exception as exc:
  78. logger.error("Failed to initiate device authorization: %s", exc)
  79. print(f" ✗ Failed to start device authorization: {exc}")
  80. return None
  81. verification_uri = device_data.get("verification_uri", "https://github.com/login/device")
  82. user_code = device_data.get("user_code", "")
  83. device_code = device_data.get("device_code", "")
  84. interval = max(device_data.get("interval", _DEVICE_CODE_POLL_INTERVAL), 1)
  85. if not device_code or not user_code:
  86. print(" ✗ GitHub did not return a device code.")
  87. return None
  88. print(f"\n Open this URL in your browser: {verification_uri}\n"
  89. f" Enter this code: {user_code}\n")
  90. print(" Waiting for authorization...", end="", flush=True)
  91. poll_fields = {"client_id": COPILOT_OAUTH_CLIENT_ID, "device_code": device_code,
  92. "grant_type": "urn:ietf:params:oauth:grant-type:device_code"}
  93. deadline = time.monotonic() + timeout_seconds
  94. while time.monotonic() < deadline:
  95. time.sleep(interval + _DEVICE_CODE_POLL_SAFETY_MARGIN)
  96. try:
  97. result = _post_form(f"https://{domain}/login/oauth/access_token", poll_fields, 10)
  98. except Exception:
  99. print(".", end="", flush=True)
  100. continue
  101. if result.get("access_token"):
  102. print(" ✓")
  103. return result["access_token"]
  104. error = result.get("error", "")
  105. if error == "slow_down":
  106. # RFC 8628: add 5 seconds to polling interval (or honor a server-supplied one)
  107. server_interval = result.get("interval")
  108. is_num = isinstance(server_interval, (int, float)) and server_interval > 0
  109. interval = int(server_interval) if is_num else interval + 5
  110. if error in ("authorization_pending", "slow_down"):
  111. print(".", end="", flush=True)
  112. continue
  113. if error:
  114. print("\n" + _DEVICE_CODE_TERMINAL_ERRORS.get(error,
  115. f" ✗ Authorization failed: {error}"))
  116. return None
  117. print("\n ✗ Timed out waiting for authorization.")
  118. return None
  119. # In-process cache: raw_token_fingerprint -> (api_token, expires_at_epoch, base_url).
  120. _jwt_cache: dict[str, tuple[str, float, Optional[str]]] = {}
  121. _JWT_REFRESH_MARGIN_SECONDS = 120 # refresh 2 min before expiry
  122. # Exchange endpoint and headers (matching VS Code / Copilot CLI)
  123. _TOKEN_EXCHANGE_URL = "https://api.github.com/copilot_internal/v2/token"
  124. _EDITOR_VERSION = "vscode/1.104.1"
  125. _EXCHANGE_USER_AGENT = "GitHubCopilotChat/0.26.7"
  126. # Transient-failure hardening: gateway startup races network readiness, and a single-shot
  127. # exchange failing there silently degrades to the RAW GitHub token, whose integrator allowlist
  128. # omits enterprise-only models → HTTP 400 every turn until restart. Retry, and persist the last
  129. # good JWT so a restart during a blip reuses the still-valid ~30-min token.
  130. _EXCHANGE_MAX_ATTEMPTS = 3
  131. _EXCHANGE_BACKOFF_BASE_SECONDS = 1.5 # sleeps ~1.5s, ~3.0s between attempts
  132. _JWT_DISK_FILENAME = ".copilot_jwt.json"
  133. _JWT_DISK_MAX_BYTES = 1_048_576 # 1 MiB cap on the persisted JWT store read
  134. # Negative cache: fingerprint -> epoch until which attempts raise immediately (success clears
  135. # it). Without it a permanently-rejected token burned ~4.5s of retry backoff on EVERY
  136. # provider-discovery pass (/model picker, delegation spawns, dashboard).
  137. _exchange_failure_cache: dict[str, float] = {}
  138. # Single-flight per fingerprint: concurrent callers (dashboard polls every few seconds) wait on
  139. # the ONE in-flight exchange instead of each spawning a hung resolver thread during a DNS outage.
  140. _exchange_locks: dict[str, threading.Lock] = {}
  141. _exchange_locks_guard = threading.Lock()
  142. def _exchange_lock_for(fp: str) -> threading.Lock:
  143. with _exchange_locks_guard:
  144. lock = _exchange_locks.get(fp)
  145. if lock is None:
  146. lock = _exchange_locks[fp] = threading.Lock()
  147. return lock
  148. _EXCHANGE_FAILURE_TTL_TRANSIENT_SECONDS = 60.0 # network blips: retry soon
  149. _EXCHANGE_FAILURE_TTL_PERMANENT_SECONDS = 1800.0 # 401/403/404: won't heal
  150. # The token itself is rejected — retrying with backoff just blocks the caller.
  151. _EXCHANGE_PERMANENT_HTTP_STATUSES = frozenset({401, 403, 404})
  152. def _token_fingerprint(raw_token: str) -> str:
  153. """Short fingerprint of a raw token for cache keying (avoids storing full token)."""
  154. return hashlib.sha256(raw_token.encode()).hexdigest()[:16]
  155. def _read_jwt_store(path: Path) -> Optional[dict]:
  156. """Bounded read of the on-disk JWT store → dict, or None if missing/unusable (a store over
  157. the 1 MiB cap or non-dict can't balloon memory or get rewritten back out)."""
  158. if not path.exists():
  159. return None
  160. try:
  161. if path.stat().st_size > _JWT_DISK_MAX_BYTES:
  162. logger.debug("Persisted Copilot JWT store exceeds %d bytes; ignoring",
  163. _JWT_DISK_MAX_BYTES)
  164. return None
  165. loaded = json.loads(path.read_text(encoding="utf-8-sig"))
  166. return loaded if isinstance(loaded, dict) else None
  167. except Exception as exc:
  168. logger.debug("Failed to read persisted Copilot JWT store: %s", exc)
  169. return None
  170. def _jwt_disk_path() -> Optional[Path]:
  171. """Path to the on-disk exchanged-JWT cache (profile-aware), or None."""
  172. try:
  173. from hermes_constants import get_hermes_home
  174. return Path(get_hermes_home()) / _JWT_DISK_FILENAME
  175. except Exception:
  176. return None
  177. def _with_jwt_store(verb: str, op):
  178. """Run ``op(path, store_or_None)`` against the disk store; failures are logged, never raised."""
  179. path = _jwt_disk_path()
  180. if not path:
  181. return None
  182. try:
  183. return op(path, _read_jwt_store(path))
  184. except Exception as exc:
  185. logger.debug("Failed to %s Copilot JWT: %s", verb, exc)
  186. return None
  187. def evict_cached_exchanged_token(raw_token: str) -> None:
  188. """Drop any cached exchanged JWT for ``raw_token`` (in-process + on-disk) — the runtime
  189. stale-credential recovery path for ``model_not_available_for_integrator`` 400s."""
  190. if not raw_token:
  191. return
  192. fp = _token_fingerprint(raw_token)
  193. _jwt_cache.pop(fp, None)
  194. # Eviction = "force a fresh exchange": the negative-cache entry must go too.
  195. _exchange_failure_cache.pop(fp, None)
  196. def _evict(path, store):
  197. if store is not None and fp in store:
  198. del store[fp]
  199. atomic_json_write(path, store, indent=None, mode=0o600)
  200. _with_jwt_store("evict cached", _evict)
  201. def _load_jwt_from_disk(fp: str) -> Optional[tuple[str, float, Optional[str]]]:
  202. """Persisted exchanged JWT for ``fp`` → (api_token, expires_at, base_url), or None."""
  203. def _load(path, store):
  204. entry = (store or {}).get(fp)
  205. if not isinstance(entry, dict):
  206. return None
  207. api_token = entry.get("api_token", "")
  208. expires_at = float(entry.get("expires_at", 0) or 0)
  209. return (api_token, expires_at, entry.get("base_url")) if api_token and expires_at else None
  210. return _with_jwt_store("load persisted", _load)
  211. def _save_jwt_to_disk(fp: str, api_token: str, expires_at: float, base_url: Optional[str]) -> None:
  212. """Persist an exchanged JWT (0o600), pruning expired entries."""
  213. def _save(path, store):
  214. now = time.time()
  215. kept = {
  216. k: v for k, v in (store or {}).items()
  217. if isinstance(v, dict) and float(v.get("expires_at", 0) or 0) > now}
  218. kept[fp] = {"api_token": api_token, "expires_at": expires_at, "base_url": base_url}
  219. atomic_json_write(path, kept, indent=None, mode=0o600)
  220. _with_jwt_store("persist", _save)
  221. # urllib's ``timeout`` only bounds socket ops AFTER DNS; getaddrinfo ignores it, so a networkless
  222. # Windows host can hang for minutes (observed: a 17-minute event-loop stall).
  223. _DNS_GRACE_SECONDS = 5.0
  224. def _urlopen_bounded(req, timeout: float):
  225. """urlopen() on a daemon thread, abandoned after timeout + _DNS_GRACE_SECONDS so a
  226. DNS/getaddrinfo hang cannot block the caller. Raises the worker's exception or TimeoutError."""
  227. box: dict = {}
  228. abandoned = threading.Event()
  229. def _worker() -> None:
  230. try:
  231. resp = urllib.request.urlopen(req, timeout=timeout)
  232. except BaseException as exc: # re-raised on the caller's thread
  233. box["exc"] = exc
  234. return
  235. if abandoned.is_set(): # caller already timed out — release the socket
  236. with contextlib.suppress(Exception):
  237. resp.close()
  238. return
  239. box["resp"] = resp
  240. t = threading.Thread(target=_worker, name="copilot-token-exchange", daemon=True)
  241. t.start()
  242. t.join(timeout + _DNS_GRACE_SECONDS)
  243. if t.is_alive():
  244. abandoned.set()
  245. raise TimeoutError("copilot token exchange exceeded hard cap of "
  246. f"{timeout + _DNS_GRACE_SECONDS:.0f}s (DNS/getaddrinfo hang?)")
  247. if "exc" in box:
  248. raise box["exc"]
  249. if "resp" not in box:
  250. raise TimeoutError("copilot token exchange worker died without result")
  251. return box["resp"]
  252. def _fetch_exchange_with_retry(req, timeout: float, fp: str) -> dict:
  253. """GET the exchange with backoff for startup network races; raises ValueError on failure.
  254. Permanent rejections (401/403/404) skip the retry loop. Failures populate the negative
  255. cache (long TTL for permanent, short for transient); success clears it.
  256. """
  257. last_exc: Optional[Exception] = None
  258. permanent_failure = False
  259. for attempt in range(1, _EXCHANGE_MAX_ATTEMPTS + 1):
  260. try:
  261. with _urlopen_bounded(req, timeout) as resp:
  262. data = json.loads(resp.read().decode())
  263. _exchange_failure_cache.pop(fp, None)
  264. return data
  265. except Exception as exc:
  266. last_exc = exc
  267. status = getattr(exc, "code", None) or getattr(exc, "status", None)
  268. permanent_failure = status in _EXCHANGE_PERMANENT_HTTP_STATUSES
  269. if permanent_failure:
  270. logger.debug("Copilot token exchange rejected (HTTP %s); not retrying", status)
  271. break
  272. if attempt < _EXCHANGE_MAX_ATTEMPTS:
  273. sleep_s = _EXCHANGE_BACKOFF_BASE_SECONDS * attempt
  274. logger.debug("Copilot token exchange attempt %d/%d failed (%s); retrying in %.1fs",
  275. attempt, _EXCHANGE_MAX_ATTEMPTS, exc, sleep_s)
  276. time.sleep(sleep_s)
  277. _exchange_failure_cache[fp] = time.time() + (
  278. _EXCHANGE_FAILURE_TTL_PERMANENT_SECONDS if permanent_failure
  279. else _EXCHANGE_FAILURE_TTL_TRANSIENT_SECONDS)
  280. raise ValueError(f"Copilot token exchange failed after {_EXCHANGE_MAX_ATTEMPTS} attempts: "
  281. f"{last_exc}") from last_exc
  282. def _cache_entry_fresh(cached) -> bool:
  283. return bool(cached) and time.time() < cached[1] - _JWT_REFRESH_MARGIN_SECONDS
  284. def exchange_copilot_token(
  285. raw_token: str, *, timeout: float = 10.0) -> tuple[str, float, Optional[str]]:
  286. """Exchange a raw GitHub token for a Copilot API token → (token, expires_at, base_url).
  287. The token is a semicolon-separated string (not a JWT) used as a Bearer token. ``base_url``
  288. is the account-specific host: the exchange's ``endpoints.api`` (enterprise/proxied
  289. accounts), else derived from the token's ``proxy-ep``; individual accounts have neither,
  290. so it is None. Cached in-process until close to expiry. Raises ``ValueError`` on failure.
  291. """
  292. fp = _token_fingerprint(raw_token)
  293. # Fast paths outside the lock: a valid in-process JWT needs no exchange, and a recent failure
  294. # means queueing behind the in-flight holder (up to ~50 s) would only park an executor thread
  295. # to learn the same answer.
  296. cached = _jwt_cache.get(fp)
  297. if _cache_entry_fresh(cached):
  298. return cached
  299. _fail_until = _exchange_failure_cache.get(fp, 0.0)
  300. if time.time() < _fail_until:
  301. raise ValueError("Copilot token exchange recently failed; skipping re-attempt "
  302. f"for another {int(_fail_until - time.time())}s")
  303. # Note: a waiter's own ``timeout`` is not honoured across the lock wait — by design of
  304. # single-flight, it observes the holder's outcome instead.
  305. with _exchange_lock_for(fp):
  306. return _exchange_copilot_token_locked(raw_token, fp, timeout=timeout)
  307. def _exchange_copilot_token_locked(
  308. raw_token: str, fp: str, *, timeout: float) -> tuple[str, float, Optional[str]]:
  309. # Re-check in-process under the lock (a queued-behind caller may have just exchanged), then
  310. # on-disk: a fresh process may hold a still-valid persisted JWT, avoiding a network
  311. # round-trip precisely when the network is most likely flaky.
  312. for lookup in (_jwt_cache.get, _load_jwt_from_disk):
  313. cached = lookup(fp)
  314. if _cache_entry_fresh(cached):
  315. _jwt_cache[fp] = cached
  316. return cached
  317. # Negative cache: fail fast so provider discovery / picker opens don't block.
  318. _fail_until = _exchange_failure_cache.get(fp, 0.0)
  319. if time.time() < _fail_until:
  320. raise ValueError("Copilot token exchange recently failed; skipping re-attempt "
  321. f"for another {int(_fail_until - time.time())}s")
  322. req = urllib.request.Request(
  323. _TOKEN_EXCHANGE_URL, method="GET",
  324. headers={"Authorization": f"token {raw_token}", "User-Agent": _EXCHANGE_USER_AGENT,
  325. "Accept": "application/json", "Editor-Version": _EDITOR_VERSION})
  326. data = _fetch_exchange_with_retry(req, timeout, fp)
  327. api_token = data.get("token", "")
  328. if not api_token:
  329. raise ValueError("Copilot token exchange returned empty token")
  330. expires_at = float(data.get("expires_at") or 0) or time.time() + 1800
  331. # ``endpoints.api`` is authoritative (Copilot Enterprise / proxied accounts); else derive from
  332. # the token's ``proxy-ep``. Individual accounts have neither → None (registry default).
  333. endpoints = data.get("endpoints")
  334. base_url: Optional[str] = (
  335. str(endpoints.get("api") or "").strip().rstrip("/") if isinstance(endpoints, dict) else ""
  336. ) or _derive_base_url_from_proxy_ep(api_token)
  337. _jwt_cache[fp] = (api_token, expires_at, base_url)
  338. _save_jwt_to_disk(fp, api_token, expires_at, base_url)
  339. logger.debug("Copilot token exchanged, expires_at=%s, base_url=%s", expires_at, base_url)
  340. return api_token, expires_at, base_url
  341. def _derive_base_url_from_proxy_ep(token: str) -> Optional[str]:
  342. """Copilot API base URL from the token's ``proxy-ep=proxy.<host>`` field (→ ``api.``)."""
  343. m = re.search(r'(?:^|;)\s*proxy-ep=([^;\s]+)', token)
  344. if not m:
  345. return None
  346. proxy_ep = re.sub(r"^https?://", "", m.group(1), count=1).rstrip("/")
  347. proxy_ep = re.sub(r"^proxy\.", "api.", proxy_ep, count=1)
  348. return f"https://{proxy_ep}"
  349. def get_copilot_api_token(raw_token: str) -> tuple[str, Optional[str]]:
  350. """``(api_token, base_url)`` from the exchange, or ``(raw_token, None)`` when it fails
  351. (accounts that don't need exchange keep working)."""
  352. if not raw_token:
  353. return raw_token, None
  354. try:
  355. api_token, _, base_url = exchange_copilot_token(raw_token)
  356. return api_token, base_url
  357. except Exception as exc:
  358. logger.debug("Copilot token exchange failed, using raw token: %s", exc)
  359. return raw_token, None
  360. def copilot_request_headers(
  361. *, is_agent_turn: bool = True, is_vision: bool = False) -> dict[str, str]:
  362. """Build the standard headers for Copilot API requests."""
  363. headers: dict[str, str] = {"Editor-Version": _EDITOR_VERSION, "User-Agent": "HermesAgent/1.0",
  364. "Copilot-Integration-Id": "vscode-chat",
  365. "Openai-Intent": "conversation-edits",
  366. "x-initiator": "agent" if is_agent_turn else "user"}
  367. if is_vision:
  368. headers["Copilot-Vision-Request"] = "true"
  369. return headers
  370. # ── ProviderProfile credential seams ────────────────────────────────────────────────────────────
  371. # Warn once per token per process when the exchange degrades to the raw token (#114740).
  372. _RAW_DEGRADATION_WARNED: set[str] = set()
  373. def _warn_raw_degradation_once(token: str) -> None:
  374. """WARN once per token per process when Copilot exchange degrades to raw token (#114740)."""
  375. fingerprint = _token_fingerprint(token) if token else "unknown"
  376. if fingerprint in _RAW_DEGRADATION_WARNED:
  377. return
  378. _RAW_DEGRADATION_WARNED.add(fingerprint)
  379. logger.warning(
  380. "Copilot token exchange degraded to RAW token (exchange "
  381. "unavailable); enterprise-only models may 400 with "
  382. "model_not_available_for_integrator until exchange recovers."
  383. )
  384. def _reset_raw_degradation_warned() -> None:
  385. """Clear the degradation warning cache (for test isolation)."""
  386. _RAW_DEGRADATION_WARNED.clear()
  387. # Every pool source the same token can be seeded under (one per env var).
  388. _POOL_SOURCES = tuple(f"env:{v}" for v in COPILOT_ENV_VARS)
  389. _STALE_CREDENTIAL_MARKERS = (
  390. "model_not_available_for_integrator",
  391. "not available for integrator",
  392. "model_not_supported",
  393. "the requested model is not supported",
  394. )
  395. _GITHUB_MODELS_FREE_TIER_HOST = "models.inference.ai.azure.com"
  396. _GITHUB_MODELS_HINT = (
  397. " 💡 GitHub Models free tier (models.inference.ai.azure.com) caps every",
  398. " request at ~8K tokens. Hermes' system prompt + tool schemas baseline",
  399. " exceeds that floor, so this endpoint cannot run an agentic loop.",
  400. " Use the `copilot` provider with a Copilot subscription token (`hermes",
  401. " setup` → GitHub Copilot), or pick any other provider.",
  402. )
  403. def _refresh_exchanged_token() -> Optional[tuple[str, Optional[str]]]:
  404. """401 on the main client: the raw GitHub token is stable, the short-TTL *exchanged* IDE token is
  405. what expires mid-turn (``401 IDE token expired``), so force a fresh exchange; fall back to the
  406. raw token only if the exchange itself is unavailable."""
  407. try:
  408. new_token, token_source = resolve_copilot_token()
  409. except Exception as exc: # health: allow BLE001 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  410. logger.debug("Copilot credential refresh failed: %s", exc)
  411. return None
  412. new_token, enterprise_base_url = (new_token.strip() if isinstance(new_token, str) else ""), None
  413. if not new_token:
  414. return None
  415. try:
  416. evict_cached_exchanged_token(new_token)
  417. api_token, exchanged_base_url = get_copilot_api_token(new_token)
  418. if isinstance(api_token, str) and api_token.strip():
  419. new_token, enterprise_base_url = api_token.strip(), exchanged_base_url
  420. except Exception as exc: # health: allow BLE001 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  421. logger.debug("Copilot 401 re-exchange failed, using resolved token: %s", exc)
  422. logger.info("Copilot credentials refreshed from %s", token_source)
  423. return new_token, enterprise_base_url
  424. def _reexchange_stale_token() -> Optional[tuple[str, Optional[str]]]:
  425. """Stale-credential 400 (``model_not_available_for_integrator``): typically a raw ``ghu_`` token
  426. cached when the startup exchange degraded. Evict + re-exchange; None when still degraded (a
  427. rebuild would not help, so the caller's single-shot retry is not burned)."""
  428. try:
  429. raw_token, token_source = resolve_copilot_token()
  430. if not isinstance(raw_token, str) or not raw_token.strip():
  431. return None
  432. raw_token = raw_token.strip()
  433. evict_cached_exchanged_token(raw_token)
  434. api_token, enterprise_base_url = get_copilot_api_token(raw_token)
  435. except Exception as exc: # health: allow BLE001 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  436. logger.debug("Copilot stale-credential recovery failed: %s", exc)
  437. return None
  438. if not isinstance(api_token, str) or not api_token.strip():
  439. return None
  440. if api_token == raw_token and not enterprise_base_url:
  441. logger.warning(
  442. "Copilot stale-credential recovery: exchange still degraded to raw token; skipping retry "
  443. "(network/exchange endpoint unavailable)."
  444. )
  445. return None
  446. logger.info("Copilot credentials re-exchanged after stale-credential 400 (source=%s)", token_source)
  447. return api_token.strip(), enterprise_base_url
  448. def _revalidate_in_memory_token() -> Optional[tuple[str, Optional[str]]]:
  449. """Auxiliary-client 401: drop only the in-process JWT and exchange again (a persisted, still-valid
  450. JWT and the negative cache are honoured); raises when the exchange fails."""
  451. raw_token, _source = resolve_copilot_token()
  452. if not str(raw_token or "").strip():
  453. return None
  454. _jwt_cache.pop(_token_fingerprint(raw_token), None)
  455. api_token, _expires_at, base_url = exchange_copilot_token(raw_token)
  456. return api_token, base_url
  457. _RUNTIME_REFRESHERS = {
  458. "unauthorized": _refresh_exchanged_token,
  459. "stale": _reexchange_stale_token,
  460. "auxiliary_unauthorized": _revalidate_in_memory_token,
  461. }
  462. class CopilotCredentialsMixin:
  463. """Fills the generic credential seams of ``providers.base.ProviderProfile`` for Copilot."""
  464. def resolve_api_key_secret(self) -> tuple[str, str]:
  465. """The exchanged Copilot token. A home that never chose Copilot gets the raw env token (it
  466. still reads as present) and GitHub is not called: provider listings resolve this for every
  467. provider with a token in reach (``activation``)."""
  468. from .activation import github_calls_allowed
  469. try:
  470. token, source = resolve_copilot_token()
  471. if token and not github_calls_allowed():
  472. return token, source
  473. if token:
  474. api_token, _base_url = get_copilot_api_token(token)
  475. return api_token, source
  476. except Exception: # health: allow BLE001 S110 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  477. pass
  478. return "", ""
  479. def runtime_base_url(self, *, api_key: str, default: str, env_url: str) -> str:
  480. """The API base comes from the token-exchange response (endpoints.api, proxy-ep fallback),
  481. authoritative for Enterprise / proxied accounts; falls back to the registry default."""
  482. from .activation import github_calls_allowed
  483. base_url = env_url.rstrip("/") if env_url else default
  484. try:
  485. raw_token, _ = resolve_copilot_token()
  486. if raw_token and github_calls_allowed():
  487. resolved = (get_copilot_api_token(raw_token)[1] or "").strip()
  488. if resolved:
  489. base_url = resolved
  490. except Exception as exc: # health: allow BLE001 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  491. logger.debug("Copilot base URL resolution fell back to default: %s", exc)
  492. return base_url
  493. def prepare_pool_entry(self, entry: Any, api_key: str) -> str:
  494. """A pool row may still carry the RAW GitHub token: the seeder skips the exchange while
  495. copilot is merely discovered (ambient gh login, not in config). Here copilot IS the runtime
  496. target, and a raw token routes to the language-server integrator whose allowlist omits
  497. enterprise-only models (400 model_not_available_for_integrator)."""
  498. if not api_key or not validate_copilot_token(api_key)[0]:
  499. return api_key # already an exchanged API token
  500. api_token, enterprise_base_url = get_copilot_api_token(api_key)
  501. if api_token == api_key and not enterprise_base_url:
  502. _warn_raw_degradation_once(api_key)
  503. return api_key
  504. entry.access_token = api_token
  505. if enterprise_base_url:
  506. entry.base_url = enterprise_base_url
  507. return api_token
  508. def seed_pool_credentials(self, *, suppressed_sources: frozenset,
  509. explicitly_configured: bool) -> list[dict]:
  510. # Env tokens are resolved dynamically; they don't live in the auth store. All-sources gate
  511. # BEFORE any work: the exchange retries 3x with backoff (~35s worst case); a user who
  512. # suppressed every copilot source must not pay that on every pool load.
  513. try:
  514. if all(s in suppressed_sources for s in _POOL_SOURCES):
  515. return []
  516. token, source = resolve_copilot_token()
  517. if not token:
  518. return []
  519. source_name = f"env:{source}"
  520. if source_name in suppressed_sources: # per-source gate BEFORE the network exchange
  521. return []
  522. from .activation import github_calls_allowed
  523. # Core's ``explicitly_configured`` counts any profile env var, so a GH_TOKEN kept for
  524. # other tools would pass it; the plugin's own check does not (``activation``).
  525. del explicitly_configured
  526. if not github_calls_allowed():
  527. # Only discovered here (an ambient env token), not selected anywhere: no model is
  528. # routed to it, so the exchange — and its degradation warning on every pool load —
  529. # buys nothing (#114740). Seed the raw token; selecting copilot re-seeds and exchanges.
  530. api_token, enterprise_base_url = token, None
  531. else:
  532. api_token, enterprise_base_url = get_copilot_api_token(token)
  533. # A failed exchange falls back to the RAW token, which the Copilot API routes to the
  534. # "copilot-language-server" integrator whose allowlist omits enterprise-only models
  535. # -> HTTP 400 on every turn. Surface it once.
  536. if api_token == token and not enterprise_base_url:
  537. _warn_raw_degradation_once(token)
  538. return [{"source": source_name, "access_token": api_token, "base_url": enterprise_base_url,
  539. "label": source}]
  540. except Exception as exc: # health: allow BLE001 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  541. logger.debug("Copilot token seed failed: %s", exc)
  542. return []
  543. def remove_credential_source(self, source: str) -> Optional[tuple[tuple[str, ...], tuple[str, ...]]]:
  544. """The same token can be seeded under several env:<VAR> rows, so suppress every variant or the
  545. duplicates resurrect the entry. Shell state is left untouched."""
  546. if not source.startswith("env:"):
  547. return None
  548. return _POOL_SOURCES, (
  549. "Suppressed all copilot env token sources — they will not be re-seeded.",
  550. "Note: Your shell environment is unchanged.",
  551. "Run `hermes auth add copilot` to re-enable if needed.",
  552. )
  553. def refresh_runtime_credentials(self, *, reason: str) -> Optional[tuple[str, Optional[str]]]:
  554. refresher = _RUNTIME_REFRESHERS.get(reason)
  555. return refresher() if refresher else None
  556. def credential_refresh_notice(self, *, reason: str) -> str:
  557. if reason == "stale":
  558. return "Copilot credential re-exchanged after model_not_available 400. Retrying request..."
  559. return "Copilot credentials refreshed after 401. Retrying request..." if reason == "unauthorized" else ""
  560. def is_stale_credential_error(self, *, status_code: Optional[int], message: str) -> bool:
  561. """A 400 that is really a STALE / DEGRADED credential (status 400 AND an integrator /
  562. model-not-supported marker, so a wrong model name never triggers the re-exchange)."""
  563. lowered = (message or "").lower()
  564. if status_code != 400 and "error code: 400" not in lowered:
  565. return False
  566. return any(marker in lowered for marker in _STALE_CREDENTIAL_MARKERS)
  567. def request_rejection_hint(self, *, status_code: Optional[int], base_url: str) -> tuple[str, ...]:
  568. """GitHub Models free tier caps requests at 8K tokens, under the system prompt + tool schema
  569. floor; compression can't help, so say so."""
  570. from .helpers import base_url_host_matches
  571. if status_code == 413 and base_url_host_matches(base_url, _GITHUB_MODELS_FREE_TIER_HOST):
  572. return _GITHUB_MODELS_HINT
  573. return ()
  574. def missing_credentials_hint(self) -> str:
  575. """Appended to core's "No usable credentials found" message: since the rule-11 cut a
  576. `gh auth login` alone no longer signs Hermes in, so name the plugin's own login. The env vars
  577. stay out: core's runtime message already lists them right before this hint."""
  578. return "Run `hermes auth add copilot` to sign in."
  579. def copilot_cli_auth_evidence() -> tuple[bool, Optional[str]]:
  580. """Positive sign-in evidence for copilot-acp without spawning the CLI: a supported env token (the
  581. same vars the Copilot CLI honors). The CLI's own token files are not read (catalog rule 11), so a
  582. CLI-only login reads as "managed by the GitHub Copilot CLI", never as signed out."""
  583. try:
  584. for env_var in COPILOT_ENV_VARS:
  585. val = (get_secret(env_var) or "").strip()
  586. if val and validate_copilot_token(val)[0]:
  587. return True, f"env: {env_var}"
  588. except Exception as exc: # health: allow BLE001 -- moved verbatim from core (hermes_cli/copilot_auth.py, credential_pool, client_lifecycle, auth.py)
  589. logger.debug("copilot-acp env token evidence check failed: %s", exc)
  590. return False, None