fix(credential_pool): classify copilot sources by exact match
Review fold on the #76341 salvage: the substring test ('gh' in source.lower()) classified GH_TOKEN and GITHUB_TOKEN as gh_cli, so a user's env-var-specific suppression was silently bypassed (and suppressing gh_cli silently dropped env tokens). Pre-existing bug on main, but the PR's early gate makes the classification decide whether the exchange runs at all. Match resolve_copilot_token's exact 'gh auth token' sentinel instead. Adds 3 regression tests: env-var suppression gates the exchange, gh_cli suppression doesn't swallow env tokens, all-sources suppression skips the resolve subprocess entirely. Also corrects the ~13s comment (actual worst case ~35s: 3x10s timeouts + 4.5s backoff).
This commit is contained in:
@@ -2395,7 +2395,7 @@ def _seed_from_singletons(provider: str, entries: List[PooledCredential]) -> Tup
|
||||
# All-sources suppression gate BEFORE any work — including the
|
||||
# `gh auth token` subprocess spawn. resolve_copilot_token()
|
||||
# shells out (~30ms), and the exchange retries 3x with backoff
|
||||
# (~13s worst case); a user who suppressed every copilot source
|
||||
# (~35s worst case); a user who suppressed every copilot source
|
||||
# (hermes auth remove copilot gh_cli) must not pay either on
|
||||
# every pool load (model picker open, /model, agent startup).
|
||||
# Enumerating the full source space here matches what
|
||||
@@ -2406,11 +2406,17 @@ def _seed_from_singletons(provider: str, entries: List[PooledCredential]) -> Tup
|
||||
return changed, active_sources
|
||||
token, source = resolve_copilot_token()
|
||||
if token:
|
||||
source_name = "gh_cli" if "gh" in source.lower() else f"env:{source}"
|
||||
# ``resolve_copilot_token`` returns exactly "gh auth token"
|
||||
# for the CLI path; env-sourced tokens return the var name.
|
||||
# Match exactly — a substring test classifies GH_TOKEN and
|
||||
# GITHUB_TOKEN as gh_cli, silently bypassing a user's
|
||||
# per-env-var suppression.
|
||||
source_name = "gh_cli" if source == "gh auth token" else f"env:{source}"
|
||||
# Per-source suppression gate (a user may suppress only the
|
||||
# gh CLI path and keep an env var, or vice versa) BEFORE the
|
||||
# network exchange. The exchange retries 3x with backoff
|
||||
# (~13s worst case), so a source the user already suppressed
|
||||
# network exchange. The exchange retries 3x with 10s
|
||||
# timeouts and 4.5s total backoff (~35s worst case), so a
|
||||
# source the user already suppressed
|
||||
# must not burn that dead time just to have the entry
|
||||
# discarded afterwards. Same early-gate pattern every other
|
||||
# singleton branch uses.
|
||||
|
||||
@@ -1397,6 +1397,106 @@ def test_load_pool_skips_exchange_for_suppressed_copilot(tmp_path, monkeypatch):
|
||||
assert pool.entries() == []
|
||||
|
||||
|
||||
def test_load_pool_respects_env_var_copilot_suppression(tmp_path, monkeypatch):
|
||||
"""Suppressing env:GH_TOKEN must gate a GH_TOKEN-sourced token.
|
||||
|
||||
Regression test for the source_name classification: a substring match
|
||||
(``"gh" in source.lower()``) classified GH_TOKEN/GITHUB_TOKEN as gh_cli,
|
||||
so a user's env-var-specific suppression was silently bypassed and the
|
||||
exchange ran anyway.
|
||||
"""
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
|
||||
_write_auth_store(
|
||||
tmp_path,
|
||||
{
|
||||
"version": 1,
|
||||
"credential_pool": {},
|
||||
"suppressed_sources": {"copilot": ["env:GH_TOKEN"]},
|
||||
},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.copilot_auth.resolve_copilot_token",
|
||||
lambda: ("gho_fake_token_env", "GH_TOKEN"),
|
||||
)
|
||||
|
||||
exchange_called = False
|
||||
|
||||
def _boom(token):
|
||||
nonlocal exchange_called
|
||||
exchange_called = True
|
||||
raise AssertionError("exchange must not run for a suppressed env source")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.copilot_auth.get_copilot_api_token",
|
||||
_boom,
|
||||
)
|
||||
|
||||
from agent.credential_pool import load_pool
|
||||
pool = load_pool("copilot")
|
||||
|
||||
assert not exchange_called
|
||||
assert pool.entries() == []
|
||||
|
||||
|
||||
def test_load_pool_gh_cli_suppression_does_not_block_env_tokens(tmp_path, monkeypatch):
|
||||
"""Suppressing gh_cli must NOT swallow an env-var-sourced token.
|
||||
|
||||
The inverse of the substring bug: GH_TOKEN misclassified as gh_cli meant
|
||||
suppressing the CLI path also silently dropped env tokens.
|
||||
"""
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
|
||||
_write_auth_store(
|
||||
tmp_path,
|
||||
{
|
||||
"version": 1,
|
||||
"credential_pool": {},
|
||||
"suppressed_sources": {"copilot": ["gh_cli"]},
|
||||
},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.copilot_auth.resolve_copilot_token",
|
||||
lambda: ("gho_fake_token_env", "GH_TOKEN"),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.copilot_auth.get_copilot_api_token",
|
||||
lambda token: ("capi_exchanged_token", None),
|
||||
)
|
||||
|
||||
from agent.credential_pool import load_pool
|
||||
pool = load_pool("copilot")
|
||||
|
||||
assert [e.source for e in pool.entries()] == ["env:GH_TOKEN"]
|
||||
|
||||
|
||||
def test_load_pool_skips_resolve_when_all_copilot_sources_suppressed(tmp_path, monkeypatch):
|
||||
"""With every copilot source suppressed, resolve_copilot_token (which
|
||||
shells out to ``gh auth token``) must not run at all."""
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
|
||||
from hermes_cli.copilot_auth import COPILOT_ENV_VARS
|
||||
_write_auth_store(
|
||||
tmp_path,
|
||||
{
|
||||
"version": 1,
|
||||
"credential_pool": {},
|
||||
"suppressed_sources": {
|
||||
"copilot": ["gh_cli"] + [f"env:{v}" for v in COPILOT_ENV_VARS],
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
def _boom():
|
||||
raise AssertionError("resolve_copilot_token must not run when all sources are suppressed")
|
||||
|
||||
monkeypatch.setattr("hermes_cli.copilot_auth.resolve_copilot_token", _boom)
|
||||
|
||||
from agent.credential_pool import load_pool
|
||||
pool = load_pool("copilot")
|
||||
|
||||
assert pool.entries() == []
|
||||
|
||||
|
||||
|
||||
|
||||
def test_load_pool_seeds_qwen_oauth_via_cli_tokens(tmp_path, monkeypatch):
|
||||
|
||||
Reference in New Issue
Block a user