Skip to content

[codex] fix(zai): sanitize GLM system prompt at runtime - #59975

Draft
RyderFreeman4Logos wants to merge 1 commit into
NousResearch:mainfrom
RyderFreeman4Logos:codex/zai-prompt-sanitizer
Draft

[codex] fix(zai): sanitize GLM system prompt at runtime#59975
RyderFreeman4Logos wants to merge 1 commit into
NousResearch:mainfrom
RyderFreeman4Logos:codex/zai-prompt-sanitizer

Conversation

@RyderFreeman4Logos

Copy link
Copy Markdown
Contributor

Summary

  • sanitize the per-request system prompt copy for Z.AI / GLM requests
  • strip or neutralize Hermes/Nous branding strings that trigger Z.AI Coding overload-style 429/code-1305 failures
  • add focused tests that verify Z.AI routing is sanitized without mutating the original cached messages

Root Cause

Z.AI GLM-5.x can reject the normal Hermes-branded system prompt at the provider boundary, returning overloaded/rate-limit style failures even for tiny non-streaming requests. Direct testing showed the request succeeds when the triggering Hermes Agent branding phrase is removed, so this patch rewrites only the API-boundary copy sent to Z.AI/GLM endpoints.

This preserves per-conversation prompt caching because the cached system prompt and conversation history are not modified.

This is intentionally scoped to the prompt sanitizer path. It does not duplicate the retry/backoff work from #59523.

Validation

  • /usr/local/share/uv/tools/bin/pytest tests/agent/test_zai_prompt_policy.py
  • /home/obj/.hermes/hermes-agent/venv/bin/python -m py_compile agent/zai_prompt_policy.py agent/chat_completion_helpers.py agent/conversation_loop.py tests/agent/test_zai_prompt_policy.py

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for isolating the outbound-prompt approach and for preserving the cached message objects.

Problems

  • agent/zai_prompt_policy.py:28 rewrites bare Hermes case-insensitively. Current prompts contain operational ~/.hermes/... profile paths at agent/system_prompt.py:397-409; this rule changes those paths for Z.AI users.
  • agent/zai_prompt_policy.py:40 enables the policy for every glm-* model. Current main explicitly treats arbitrary local/private GLM endpoints differently from direct provider identity (run_agent.py:1527-1547), so model-name-only gating is too broad.
  • Current chat-completions conversion is centralized at agent/transports/chat_completions.py:331-338; move normal-request policy there and retain explicit coverage for the direct summary call at agent/chat_completion_helpers.py:1761-1764.

Suggested changes

  • Preserve bare CLI/path/environment identifiers and test ~/.hermes/... content.
  • Gate on explicit provider identity or parsed direct Z.AI hostnames, with a localhost GLM regression test.

Automated hermes-sweeper review.

(r"hermes-agent", "local-agent"),
(r"\bHermes\b", "the local assistant"),
(r"Nous Research", "the platform provider"),
(r"\bHERMES_[A-Z0-9_]+\b", "LOCAL_AGENT_ENV"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please keep this replacement set narrow. With re.IGNORECASE, this rule rewrites the hermes segment of current system-prompt paths such as ~/.hermes/profiles/... (agent/system_prompt.py:397-409), making profile-isolation instructions point to a nonexistent path. Preserve bare operational identifiers and add a regression test for them.

return (
provider in {"zai", "z-ai", "z.ai", "glm", "zhipu"}
or model.startswith("glm-")
or "api.z.ai" in base_url

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A glm-* model name does not establish a direct Z.AI endpoint. Current main deliberately distinguishes Ollama-hosted GLM from arbitrary local/private endpoints (run_agent.py:1527-1547). Restrict this to explicit Z.AI/Zhipu provider identity or parsed direct Z.AI hostnames, and cover a custom localhost GLM case.

@teknium1 teknium1 added sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 15, 2026
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 sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) 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