Files
magpie/internal/gateway/zcode_trial.go
T
yetoneandvincent 65ee8c65c6 gateway: a ZCode Start Plan trial that ended or is spent rests that trial alone, not the account (#3661)
orion-nebula-dev's routing log: GLM-5.3-Flash-Trial and GLM-5.3-Trial,
whose Start Plan trial had ended, were refused by the ZCode plugin with
400 "ZCode has no active limited quota for GLM-5.3-Trial". The word
"quota" made it failQuota, and restAfterMarked rested the account
("zcode@zcode"), so the subscription's GLM-5.3 and GLM-5.3-Flash were
paused with it.

zcodeTrialRefused narrows that rest to the trial entry (restID) when the
candidate is a zcode plugin account, the model ends in -Trial, and the
refusal is the plugin's own words naming that model:
- 400 "ZCode has no active limited quota for <model>" (the trial ended);
- 429 "ZCode's limited quota for <bucket> is exhausted or temporarily
  unavailable", where the bucket is the entry less -Trial (its bucket
  spent). skygunner's patch covered the 400 only and kept this 429 as an
  account rest; it is the trial bucket's, given by the plugin before
  anything is sent, so it rests the entry too.
Another model named, a model without -Trial, the ended words on a 429,
the account's own quota (400 or 429) or key (401), and the same words
from another vendor still rest the account.

The plugin is not changed: zcode 0.1.31 already leaves ended trials out
of its model list and its two refusals are exact, and open plugins PRs
#395 and #421 hold zcode's next version. A member the user kept in a
group is still asked, refused locally, and asked again a minute on (a
guessed quota rest); the Routing page strikes such a member as
"skipped", which the gateway doesn't do: filed as #3805.

Ported from skygunner's e04a1b994 (branch
fix/r25-expired-model-not-account-fault): the test plugin's
FAKE_ZCODE_TRIAL and FAKE_NO_FREE fixture is his; the matcher, the 429
case and the test were rewritten. Reported by orion-nebula-dev.

Run:
- TestZCodeTrialRefusalRestsTheTrialAlone: 12 cases, the reporter's
  literal plugin bodies. Each posts to a group of the trial then another
  provider, checks the try's rest key, then posts to a group of the
  account's GLM-5.3 then another provider: served by the account when
  only the trial rests, by the other provider when the account does.
- Broken by hand: the routing.go hook off -> the four trial cases red
  ("refusal rested \"zcode@api key\", want \"zcode@api key/GLM-5.3-Trial\"");
  the 429 branch off -> "trial spent" red; the zcode-only check off ->
  "another vendor's words" red. Green again after each.
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and
  GOOS=windows; GOOS=linux go build -tags nogui ./...
- go test -timeout 45m -count=1 ./internal/gateway ./internal/plugin at
  f34a9f8cf (this change on b7d5a97fe): both ok (436s, 87s).

Not tried: a real ZCode account with an ended Start Plan trial (none to
hand); the full ./... suite (other sessions' suites were running); the
Windows box.

Co-authored-by: vincent <vincent@users.noreply.github.com>
2026-10-27 09:10:09 +08:00

47 lines
1.9 KiB
Go

package gateway
import (
"net/http"
"regexp"
"strings"
)
// zcodeEndedWords and zcodeSpentWords are the ZCode plugin's own answers
// for a Start Plan trial entry (<model>-Trial) it can't serve, given before
// anything is sent: a 400 "ZCode has no active limited quota for
// GLM-5.3-Trial" when no live plan holds the model any more (the trial
// ended), and a 429
// "ZCode's limited quota for glm-5.3 is exhausted or temporarily
// unavailable" when its bucket is spent. The plugin's words, after any
// "<provider>: " put in front of them.
var (
zcodeEndedWords = regexp.MustCompile(`(?:^|: )ZCode has no active limited quota for (\S+)$`)
zcodeSpentWords = regexp.MustCompile(`(?:^|: )ZCode's limited quota for (\S+) is exhausted or temporarily unavailable$`)
)
// zcodeTrialRefused says c, a ZCode plugin account asked for one of its
// Start Plan trial entries, was refused for that entry's own quota
// (zcodeEndedWords, zcodeSpentWords), naming the model asked for. The
// trial is that model's: the account's GLM Coding Plan still serves its
// own models, so only the trial entry rests (#3661, orion-nebula-dev: two
// expired trials rested "zcode@zcode", and GLM-5.3 and GLM-5.3-Flash with
// it). Any other
// refusal of a ZCode account, an account-wide 429 or 1113 among them,
// rests the account as before.
func zcodeTrialRefused(c candidate, status int, body []byte) bool {
if c.p.PluginProvider() != "zcode" || !strings.HasSuffix(strings.ToLower(c.model), "-trial") {
return false
}
said := strings.TrimSpace(vendorMessage(body))
switch status {
case http.StatusBadRequest:
m := zcodeEndedWords.FindStringSubmatch(said)
return m != nil && strings.EqualFold(m[1], c.model)
case http.StatusTooManyRequests:
// the plugin names the bucket's own model, the entry less -Trial
m := zcodeSpentWords.FindStringSubmatch(said)
return m != nil && strings.EqualFold(m[1]+"-Trial", c.model)
}
return false
}