refactor(auth): drop dead select() fallback in anthropic pool resolver

/simplify-code QUALITY finding: the `if callable(_available_entries): ... else:
pool.select()` ladder was dead for the real CredentialPool type (`_available_entries`
is always a bound method) AND the select() fallback violated the helper's read-only
contract — select() -> _select_unlocked() runs _available_entries(clear_expired=True,
refresh=True), which persists to auth.json and triggers a network refresh. Call
_available_entries(clear_expired=False, refresh=False) directly inside the existing
try/except instead.

Also drops the now-dead `select=` stubs from the 6 pool tests (they only existed to
satisfy the removed fallback branch). Behavior unchanged; 6 pool tests pass and the
read-only / null-token contract tests were mutation-checked (flipping the flags /
removing the None-guard fails the respective test).
This commit is contained in:
kshitijk4poor 2026-06-22 15:50:26 +05:30
parent 3463188512
commit 3545d29422
2 changed files with 7 additions and 21 deletions

View file

@ -1175,25 +1175,15 @@ def _resolve_anthropic_pool_token() -> Optional[str]:
try:
pool = load_pool("anthropic")
# Enumerate read-only (clear_expired=False, refresh=False): never persist
# to auth.json or trigger a network refresh from a bare resolve. select()
# is deliberately NOT used — it runs clear_expired=True, refresh=True,
# which would violate this read-only contract.
entries = pool._available_entries(clear_expired=False, refresh=False)
except Exception:
logger.debug("Failed to load Anthropic credential_pool", exc_info=True)
logger.debug("Failed to read Anthropic credential_pool", exc_info=True)
return None
available_entries = getattr(pool, "_available_entries", None)
if callable(available_entries):
try:
entries = available_entries(clear_expired=False, refresh=False)
except Exception:
logger.debug("Failed to enumerate Anthropic credential_pool entries", exc_info=True)
entries = []
else:
try:
selected = pool.select()
except Exception:
logger.debug("Failed to select Anthropic credential_pool entry", exc_info=True)
selected = None
entries = [selected] if selected is not None else []
for entry in entries:
if getattr(entry, "auth_type", None) != AUTH_TYPE_OAUTH:
continue

View file

@ -347,7 +347,6 @@ class TestResolveAnthropicToken:
)
pool = SimpleNamespace(
_available_entries=lambda **_kwargs: [pool_entry],
select=lambda: pool_entry,
)
monkeypatch.setattr("agent.credential_pool.load_pool", lambda provider: pool)
@ -369,7 +368,6 @@ class TestResolveAnthropicToken:
)
pool = SimpleNamespace(
_available_entries=lambda **_kwargs: [pool_entry],
select=lambda: pool_entry,
)
monkeypatch.setattr("agent.credential_pool.load_pool", lambda provider: pool)
@ -389,7 +387,6 @@ class TestResolveAnthropicToken:
broken_entry = SimpleNamespace(auth_type="oauth", access_token=None)
pool = SimpleNamespace(
_available_entries=lambda **_kwargs: [broken_entry],
select=lambda: broken_entry,
)
monkeypatch.setattr("agent.credential_pool.load_pool", lambda provider: pool)
@ -410,7 +407,6 @@ class TestResolveAnthropicToken:
api_key_entry = SimpleNamespace(auth_type="api_key", access_token="sk-pool-apikey")
pool = SimpleNamespace(
_available_entries=lambda **_kwargs: [api_key_entry],
select=lambda: api_key_entry,
)
monkeypatch.setattr("agent.credential_pool.load_pool", lambda provider: pool)
@ -454,7 +450,7 @@ class TestResolveAnthropicToken:
captured.update(kwargs)
return [pool_entry]
pool = SimpleNamespace(_available_entries=_available_entries, select=lambda: pool_entry)
pool = SimpleNamespace(_available_entries=_available_entries)
monkeypatch.setattr("agent.credential_pool.load_pool", lambda provider: pool)
assert resolve_anthropic_token() == "pool-oauth-token"