From 07ce256e69fdbe67756eb795ef2bc7458028fa9a Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sun, 29 Nov 2026 02:35:37 -0800 Subject: [PATCH] fix(credential-pool): side tasks run on the agent-CLI login the main agent is on round_robin saves the pool order past the login the runtime picked, so the auxiliary client's fresh peek named the other login (random could too): titles, compression and vision ran on a different account than the conversation. The auxiliary external_process arm now takes the main runtime's login while it is still in rotation, and the pool's next row once it is benched; standalone probes keep the fresh peek. --- agent/auxiliary_client.py | 2 +- agent/credential_pool_plugin.py | 25 +++++++++++++-- .../test_external_process_provider_seam.py | 31 +++++++++++++++++++ .../developer-guide/model-provider-plugin.md | 2 +- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py index d175fa86d9..378c9b4700 100644 --- a/agent/auxiliary_client.py +++ b/agent/auxiliary_client.py @@ -5188,7 +5188,7 @@ def _resolve_registry_branch(req: _ResolveRequest) -> _ResolveResult: return _resolve_api_key_branch(req, pconfig, resolve_api_key_provider_credentials) if auth_type == "external_process": from agent.credential_pool_plugin import external_process_client_credentials - creds = external_process_client_credentials(provider) # None: unavailable; the caller walks its fallbacks + creds = external_process_client_credentials(provider, _normalize_main_runtime(req.main_runtime)) # None: fallbacks return _resolve_external_process_branch(req, creds) if creds is not None else (None, None) from agent.auxiliary_client_registry import REGISTRY_AUTHTYPE_ARMS arm = REGISTRY_AUTHTYPE_ARMS.get(auth_type) diff --git a/agent/credential_pool_plugin.py b/agent/credential_pool_plugin.py index 3b27b90dd7..ab7171948a 100644 --- a/agent/credential_pool_plugin.py +++ b/agent/credential_pool_plugin.py @@ -79,20 +79,39 @@ def external_process_login(provider: str, *, mode: Literal["select", "peek"], re provider=provider, code="pool_signed_out", relogin_required=True) -def external_process_client_credentials(provider: str) -> Optional[dict[str, Any]]: +def external_process_client_credentials(provider: str, main_runtime: Optional[Mapping[str, Any]] = None + ) -> Optional[dict[str, Any]]: """``resolve_external_process_provider_credentials`` for an auxiliary client, or None when the provider is unavailable. Every pooled login benched or signed out raises there, so the CLI never runs on its default login; for a side task that is "no client", and the auxiliary router goes on to the task's ``fallback_chain`` (or, on the auto - route, the main fallback providers) instead of failing the task. + route, the main fallback providers) instead of failing the task. ``main_runtime`` is the main agent's: while its + login is still in rotation the side task runs on it, since a fresh read of the pool names another row under + ``round_robin`` (the saved order already moved past the pick) or ``random``. """ from hermes_cli.auth import resolve_external_process_provider_credentials try: - return resolve_external_process_provider_credentials(provider) + creds = resolve_external_process_provider_credentials(provider) except AuthError as exc: logger.info("Auxiliary client: %s unavailable: %s", provider, exc) return None + main_login = (main_runtime or {}).get("api_key") if (main_runtime or {}).get("provider") == provider else None + if creds["source"] == "pool" and main_login != creds["api_key"] and _login_in_rotation(provider, main_login): + return {**creds, "api_key": main_login} + return creds + + +def _login_in_rotation(provider: str, login: Any) -> bool: + """*login* is the secret of one of *provider*'s pooled rows that a request may use now.""" + from agent.credential_pool import load_pool + + if not isinstance(login, str) or not login.strip(): + return False + pool = load_pool(provider) + with pool._lock: + available, _pending = pool._available_entries() + return any(entry.runtime_api_key == login for entry in available) def plugin_row_is_expiring(entry: PooledCredential) -> bool: diff --git a/tests/hermes_cli/test_external_process_provider_seam.py b/tests/hermes_cli/test_external_process_provider_seam.py index 41078cdb04..24bb6e57ae 100644 --- a/tests/hermes_cli/test_external_process_provider_seam.py +++ b/tests/hermes_cli/test_external_process_provider_seam.py @@ -239,3 +239,34 @@ def test_each_runtime_resolution_picks_the_login_the_configured_strategy_names(f for rt in runtimes: current = rt["credential_pool"].current() assert current is not None and current.access_token == rt["api_key"] + + +def test_side_tasks_run_on_the_login_the_main_agent_is_on(fake_cli): + """``round_robin`` moves the saved order past the login the runtime picked, so a fresh read of the pool names the + other login. An auxiliary client built for the same provider (titles, compression, vision) takes the main + agent's login while that login is in rotation, and the pool's next one once it is benched.""" + import time + + import hermes_yaml as yaml + from agent.auxiliary_client import resolve_provider_client + from agent.credential_pool import AUTH_TYPE_OAUTH, PooledCredential, load_pool + from hermes_cli.runtime_provider import resolve_runtime_provider + from hermes_constants import get_hermes_home + + (get_hermes_home() / "config.yaml").write_text(yaml.safe_dump({"credential_pool_strategies": {"acme-acp": "round_robin"}})) + pool = load_pool("acme-acp") + for index, name in enumerate(("work", "home")): + pool.add_entry(PooledCredential( + provider="acme-acp", id=f"{name}01", label=name, auth_type=AUTH_TYPE_OAUTH, priority=index, + source="manual:acme_login", access_token=f"acme-login:/logins/{name}", base_url="acp://acme")) + + runtime = resolve_runtime_provider(requested="acme", target_model="acme") + assert runtime["api_key"] == "acme-login:/logins/work" + main = {key: runtime[key] for key in ("provider", "base_url", "api_key", "api_mode")} | {"model": "acme"} + client, _model = resolve_provider_client("acme-acp", model="acme", main_runtime=main) + assert client[1]["api_key"] == "acme-login:/logins/work" + + load_pool("acme-acp").mark_exhausted_and_rotate(status_code=429, credential_id="work01", + error_context={"reset_at": time.time() + 600}) + client, _model = resolve_provider_client("acme-acp", model="acme", main_runtime=main) + assert client[1]["api_key"] == "acme-login:/logins/home" diff --git a/website/docs/developer-guide/model-provider-plugin.md b/website/docs/developer-guide/model-provider-plugin.md index 8631f99acb..1f2f397dca 100644 --- a/website/docs/developer-guide/model-provider-plugin.md +++ b/website/docs/developer-guide/model-provider-plugin.md @@ -445,7 +445,7 @@ An agent CLI keeps its login outside Hermes (a config directory, a keychain entr | Step | What core does | |---|---| -| Every client | The main agent, its rebuild after a rotation, and the auxiliary client (titles, compression, vision) call `create_client(api_key=, command=..., args=...)` with a pooled row: the runtime picks it through the pool's selection (so `credential_pool_strategies` applies), the auxiliary client reads the row the pool would serve without moving it. With an empty pool, `api_key` is the placeholder (the provider id) and your client keeps its default login; a pool whose every row is benched or signed out refuses the request instead (`AuthError`, code `model_rate_limited` naming the soonest reset, or a sign-in-again message), so the CLI never falls back to its default login, which is another account or none. An auxiliary task then moves on to its `fallback_chain` / the main fallback providers. `agent.credential_pool_plugin.external_process_pool_key(name)` returns that same row for spawns core does not make (setup probes, `/usage`), or `None`; a plugin can also import it to detect a Hermes that has this seam. | +| Every client | The main agent, its rebuild after a rotation, and the auxiliary client (titles, compression, vision) call `create_client(api_key=, command=..., args=...)` with a pooled row: the runtime picks it through the pool's selection (so `credential_pool_strategies` applies), and the auxiliary client uses the main agent's row while it is in rotation (else the row the pool would serve, read without moving the pool). With an empty pool, `api_key` is the placeholder (the provider id) and your client keeps its default login; a pool whose every row is benched or signed out refuses the request instead (`AuthError`, code `model_rate_limited` naming the soonest reset, or a sign-in-again message), so the CLI never falls back to its default login, which is another account or none. An auxiliary task then moves on to its `fallback_chain` / the main fallback providers. `agent.credential_pool_plugin.external_process_pool_key(name)` returns that same row for spawns core does not make (setup probes, `/usage`), or `None`; a plugin can also import it to detect a Hermes that has this seam. | | Usage limit | An error with `status_code = 429` (a `body` with `resets_at` epoch seconds sets the bench until then) benches the row and the next request rotates to the next one, after one retry (at once when the error carries the rejected plan window's `anthropic-ratelimit-unified-*` headers as `response.headers`); `status_code = 402` rotates at once. The default `fill_first` strategy stays on the first row until it is benched, which keeps the prompt cache warm on one account; `round_robin` moves to the next row each time core resolves the runtime (a new session, a one-shot run). | | Signed out | An error with `status_code = 401` and `body = {"type": "invalid_token"}` (any reason in the pool's terminal set) takes the row out of rotation; `hermes auth list` shows it as signed out until `hermes auth reset `. |