Skip to content

fix: use opencode identity for zai clients. address #50663 - #51792

Closed
dlukt wants to merge 4 commits into
NousResearch:mainfrom
dlukt:fix/zai-opencode-identity-50663
Closed

dlukt wants to merge 4 commits into
NousResearch:mainfrom
dlukt:fix/zai-opencode-identity-50663

Conversation

@dlukt

@dlukt dlukt commented Jun 24, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes Z.ai/Zhipu peak-hour 429s by applying an OpenCode-shaped client identity only for Z.ai endpoints.

For api.z.ai and open.bigmodel.cn, including https://api.z.ai/api/coding/paas/v4 and https://open.bigmodel.cn/api/coding/paas/v4, Hermes now sends User-Agent: opencode/1.17.9 and strips SDK X-Stainless-* fingerprint headers from final outbound requests.

This is scoped to Z.ai/Zhipu hosts and covers the main OpenAI-compatible client path, auxiliary OpenAI/AsyncOpenAI clients, and Z.ai Anthropic-compatible clients. User model.default_headers still wins as the manual override. The implementation intentionally does not send Hermes session ids or parent session ids as third-party headers.

Related Issue

Fixes #50663

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Security fix
  • Documentation update
  • Tests (adding or improving test coverage)
  • Refactor (no behavior change)
  • New skill (bundled or hub)

Changes Made

  • Added agent/provider_client_identity.py for Z.ai host detection, OpenCode UA defaults, case-safe header merging, and final X-Stainless-* stripping hooks.
  • Updated agent/agent_init.py and run_agent.py so main OpenAI-compatible Z.ai clients receive the Z.ai identity behavior.
  • Updated agent/auxiliary_client.py so auxiliary OpenAI and AsyncOpenAI clients use the same Z.ai identity behavior.
  • Updated agent/anthropic_adapter.py so Z.ai Anthropic-compatible clients preserve Anthropic beta headers while stripping SDK fingerprint headers.
  • Updated gateway/TUI/ACP session context setup in gateway/run.py, tui_gateway/server.py, and acp_adapter/server.py to keep local session context propagation correct.
  • Added Z.ai-focused coverage in tests/agent/test_zai_opencode_identity.py.
  • Added regression coverage for canonical User-Agent overrides and session context propagation.

How to Test

  1. Run:
    uv run --extra dev python -m pytest -p no:enabler tests/agent/test_zai_opencode_identity.py tests/agent/test_auxiliary_client.py tests/agent/test_auxiliary_named_custom_providers.py tests/agent/test_auxiliary_user_default_headers.py tests/agent/test_anthropic_adapter.py tests/run_agent/test_provider_attribution_headers.py tests/run_agent/test_create_openai_client_proxy_env.py tests/run_agent/test_session_id_env.py tests/gateway/test_session_env.py tests/test_tui_gateway_server.py --tb=short
  2. Confirm Z.ai requests get User-Agent: opencode/1.17.9 and X-Stainless-* headers are stripped.
  3. Confirm non-Z.ai providers remain unchanged and user model.default_headers can override the Z.ai UA.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Linux

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) - or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys - or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows - or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide - or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior - or N/A

Screenshots / Logs

Targeted test suite passed:

764 passed in 38.08s

Additional checks passed:

  • python -m py_compile ...
  • git diff --check

@dlukt

dlukt commented Jun 24, 2026

Copy link
Copy Markdown
Author
pytest tests/ -q
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --mypy-report-style --mypy-no-status-check
  inifile: /home/darko/hermes-agent/pyproject.toml
  rootdir: /home/darko/hermes-agent

@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/zai ZAI provider P2 Medium — degraded but workaround exists labels Jun 24, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

This was generated by AI during triage.

Fix PR for #50663 (z.ai peak-hour 429s). Note the root cause is most likely upstream Z.ai throttling keyed on client signature, so this spoofs an OpenCode identity rather than fixing a Hermes-side defect. Related to #47685 (phrase-triggered 429 when the system prompt contains "Hermes Agent").

@dlukt dlukt closed this Jul 1, 2026
@dlukt

dlukt commented Jul 1, 2026

Copy link
Copy Markdown
Author

The proper fix is to spoof Zcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists provider/zai ZAI provider type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: z.ai limits hermes agent during "peak hours"

2 participants