Files
magpie/docs/subsystems
yetone 35aa50cfb3 zhipu: a GLM Coding Plan key refused 200 in the platform's envelope reads as refused, in the test, the gateway and the quota card (@xanahopper on X)
On a Coding Plan with a key Zhipu refuses, Test showed OpenAI 401 and
Anthropic 401 but Responses (https://open.bigmodel.cn/api/v1) a green
check, and the 5h/weekly windows showed only "Allowance unavailable".

Cause: /api/v1 (Zhipu's real Responses endpoint, docs.bigmodel.cn
coding-plan/tool/codex) and /api/monitor answer a refused key HTTP 200
with {"code":1000,"msg":"身份验证失败。","success":false}. probe (endpoint
Test, model tests, Detect) took any 2xx as ok, and the gateway handed
the 200 to Codex as a reply, so no failover and no rest.

- provider.ZhipuEnveloped: a 2xx JSON, non-stream, unencoded reply from
  open.bigmodel.cn or api.z.ai whose body is the envelope with
  success:false becomes {"error":{...}} under the status Zhipu's code
  table gives (1000-1005 401, 1113/13xx 429, 1220 403, 121x/1261/1301
  400, else 502). Anything else passes through unchanged (64KB cap,
  body restored). Applied in probe and in the gateway's forwardOnce.
- planquota: a refused key (envelope 1000-1005 or HTTP 401 from a
  Zhipu/Z.ai host) says "Zhipu didn't take this key (...): it has
  expired, was deleted, or is mistyped; paste a current one from <keys
  page>", and skips the team retry. The card shows it in en, zh, zh-TW,
  ja, de.
- docs: providers-accounts and gateway-routing rows.

Run at this head (before rebase, base 95e7c6bcc):
- go test -timeout 20m ./internal/gateway ./internal/provider: ok
  (343s, 93s). New: TestZhipuResponsesRefusalNotGreen,
  TestZhipuEnveloped, TestZhipuRefusedKeyCard,
  TestZhipuResponsesRefusalIsRefusal (zhipu and zai presets).
- Full go test ./...: every package ok except internal/gateway, which
  hit the default 10m timeout while the full GUI suite and another
  session's GUI tests ran beside it; alone with -timeout 20m it passed.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet on provider and
  gateway clean.
- GUI: glm-quota-error (new) and opencode-go-quota-error 20/20 in
  chromium and webkit, five languages. The full GUI suite ran once (under
  load) and exited 1; each of its 16 red files was rerun alone here and
  on an origin/main 03eb0633a copy of internal/gui: 10 pass on both;
  quota-count-wrap (1), quota-name-translated (1),
  refusal-retries-setting (2), usage-agents-hide (22), usage-fold (2)
  fail the same counts on both; routing-held-member fails 1 here, 2 on
  main. None touches quotaError's GLM line.

Broken by hand: ZhipuEnveloped returning res unchanged, and
zhipuEnvelope returning ok=false, turned the provider tests red;
reverting the gateway line turned TestZhipuResponsesRefusalIsRefusal
red; breaking the GLM regex in app.js turned glm-quota-error 0/5 in
chromium. All restored.

Not tried: no real Zhipu or Z.ai key, valid or expired. The refusal
bytes are from a curl of /api/v1 with a bad key; the reporter's
"令牌已过期或验证不正确" code is unknown (assumed 1000-1005 or HTTP 401,
both handled). A valid key's quota read with a real plan was not run.
2026-10-15 15:55:24 +08:00
..

Subsystem design references

These pages describe current responsibilities, runtime paths, state, and contracts. Start with the subsystem relevant to your task, then follow its source links to verify the implementation you change.

Subsystem Reference
Built-in subscriptions moving to plugins Provider and plugin ownership
Built-in plugins: registry, lifecycle, plugins.yml, presets, settings schemas, the window's kernel (slots, error boundary, kit) Plugin runtime
Plugins that run on the gateway's requests and replies Gateway middleware
Claude subscription turns across Claude Code runs Claude subscription bridge
How the gateway picks a provider, key or account, and falls back Gateway routing and fallback
Gateway Session Aggregation Gateway Session Aggregation
Connecting an agent's config to the gateway, drift, disconnect, WSL twins Agent wiring
Providers, keys, subscription accounts, refresh and allowance Providers and accounts
Instructions, MCP servers, skills and RTK written into agents Library
Tray, panel, window, magpie web and the page's JSON API GUI app shell
Encrypted backups and setup synchronization over WebDAV, S3 or GitHub Sync and backup

A subsystem can span several packages or repositories. Its reference describes the behavior those parts provide together; it does not need to list every function.

Maintain a reference

Update the owning reference in the same PR when a change alters a documented responsibility, interface, state transition, runtime path, compatibility rule, or failure behavior. An internal refactor that preserves these contracts does not require a prose change.

Keep each fact in one reference and link to it from agent instructions or other pages. Link to source files and named symbols rather than copying implementation details that change frequently. Explain field meanings and constraints that signatures alone do not express.

For a new reference, include the responsibilities and boundaries, source of truth, main runtime paths, state transitions, important constraints, and existing verification commands. Include failure and compatibility behavior where relevant. Document current behavior separately from proposed changes; the proposal belongs in its issue or PR.

Describe a PR's semantic changes

For each affected subsystem, explain the observable change with its trigger and before/after behavior. Include changes to interfaces, state, compatibility, and failure behavior when relevant. Link the affected reference, implementation, and verification evidence. State when a refactor preserves the documented behavior.

For example, a change to plugin adoption should explain which migration state and account condition trigger it, which implementation handles requests before and after adoption, and which test exercises that transition.

This is an author-provided explanation that a reviewer verifies against the diff and surrounding code. It is not an automatically proven compatibility verdict.

Review a change

  1. Identify the affected subsystem and the actual runtime path, including plugin or built-in ownership.
  2. Compare the claimed before/after behavior with the base and head implementation. Trace relevant callers and state transitions beyond the changed lines.
  3. Check that changed contracts appear in the owning reference and that source links still resolve.
  4. Check that the cited tests or runtime evidence exercise the claimed behavior. Report checks that were not run.

A documentation update alone does not prove the implementation. Existing tests and runtime evidence remain necessary for behavior changes.