Skip to content

fix: recover Z.AI endpoints and register costs command - #22827

Open
DanielLSM wants to merge 2 commits into
NousResearch:mainfrom
DanielLSM:fix-zai-credential-pool-endpoint-sync
Open

DanielLSM wants to merge 2 commits into
NousResearch:mainfrom
DanielLSM:fix-zai-credential-pool-endpoint-sync

Conversation

@DanielLSM

Copy link
Copy Markdown
Contributor

Recover Z.AI credential pool endpoints and register the costs command for endpoint monitoring.

Daniel added 2 commits May 9, 2026 20:47
…ntries

Z.AI has two API surfaces (regular /api/paas/v4 and coding /api/coding/paas/v4).
An account may only have quota on one.  When a zai pool entry is marked
exhausted after a 429, the fallback path uses the provider registry's default
base_url — which is the regular endpoint — causing permanent failure even
when the coding endpoint still works.

This change adds two mechanisms:

1. _sync_zai_entry_from_auth_store(): when a zai entry is exhausted, check
   provider_state.zai.detected_endpoint in auth.json.  If runtime resolution
   detected a different working endpoint, adopt it and clear exhaustion
   immediately.

2. Re-probe on cooldown expiry: when clear_expired resets an exhausted zai
   entry after its cooldown, call detect_zai_endpoint() to verify the cached
   base_url is still valid.  If the endpoint changed, update it.  If no
   endpoint works, keep the entry exhausted.

Mirrors the existing sync patterns for anthropic, nous, and openai-codex.

Fixes: credential pool exhaustion causing wrong zai endpoint fallback
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard provider/zai ZAI provider labels May 9, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

Related to #21422 and #22826 (same author, overlapping Z.AI credential pool fix + /costs command). Also see closed #21404 and #21124 which attempted the same fix.

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for isolating the Z.AI pool-recovery path. The current main still lacks a Z.AI auth-state sync in agent/credential_pool.py:1381-1476, so the underlying issue remains actionable.

Problems

  • agent/credential_pool.py:620 copies a provider-global detected endpoint without validating key_hash against the pool entry. Current Z.AI cache reuse is deliberately key-bound in hermes_cli/auth.py:692-697; a pool with multiple keys can therefore inherit another key's endpoint. The added fixture at tests/agent/test_credential_pool.py:1669 explicitly disables that check.
  • hermes_cli/commands.py:189-190 aliases /credits to /costs, but current main has a separate canonical /credits top-up flow at hermes_cli/commands.py:232, cli.py:8718-8719, and gateway/run.py:9865-9866 (shipped in 7ba5df0d5).

Suggested changes

  • Bind endpoint adoption to the matching credential hash and add a two-key isolation regression test.
  • Preserve /credits; make /costs an alias of /usage if the additional spelling is still wanted.

This is an automated hermes-sweeper review.

Comment thread agent/credential_pool.py
detected = state.get("detected_endpoint")
if not isinstance(detected, dict):
return entry
detected_url = detected.get("base_url", "").rstrip("/")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detected_endpoint is keyed to a specific API key on current main (hermes_cli/auth.py:692-697). Verify its key_hash against this entry's access token before adopting the URL; otherwise a cached endpoint for one pooled Z.AI key can overwrite another key's routing. Please add a two-key regression case.

Comment thread hermes_cli/commands.py
gateway_only=True),
CommandDef("usage", "Show token usage and rate limits for the current session", "Info"),
CommandDef("costs", "Show token usage, costs, and provider rate limits", "Info",
aliases=("usage", "credits")),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not make credits an alias here. Current main now has a dedicated /credits balance/top-up flow (hermes_cli/commands.py:232, commit 7ba5df0d5); this mapping would send /credits to the usage handler instead. Preserve /credits and alias only /costs to /usage if needed.

@teknium1 teknium1 added sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform area/usage-cost Token accounting, usage reporting, billing, cost tracking labels Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/usage-cost Token accounting, usage reporting, billing, cost tracking comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have provider/zai ZAI provider sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants