Files
magpie/internal/gateway/zcode_trial.go
T
yetone 59d9f5c7d9 gateway, redact: compile their 275 package-level regexps on first use, not at every start
magpie is one binary, so every package's init runs on every launch,
including `magpie statusline`, which Claude Code runs on every message.
internal/gateway and internal/redact compiled 275 patterns at init:
3.7 MB + 2.3 MB and ~38k allocations per launch, most of them for
vendors and rules that launch never reaches.

New internal/lazyre: RE holds the source and compiles it through a
sync.Once on first use, with the *regexp.Regexp methods magpie calls
delegating to it (Of wraps one already compiled, for the user's own
privacy rules). A one-off Go AST rewrite turned every regexp.MustCompile
in a package-level var (outside func literals) into lazyre.New; the
struct fields, map and parameter that hold them took the new type. A
bad pattern now panics on first use instead of at start, so
TestAllCompile in gateway and redact compiles every registered one.

Measured on mote (M5 Pro), sandbox HOME with a copy of real accounts,
v0.1.3515 (273b9f5a9) vs this change:
- GODEBUG=inittrace=1: all init 13.0 MB / 106k allocs -> 7.4 MB / 70k;
  gateway 3.68 MB -> 70 KB, redact 2.31 MB -> 261 KB.
- `echo {} | magpie statusline`, /usr/bin/time -l, 3 runs each:
  instructions retired 260-265M -> 212-215M (-18%), peak footprint
  22.1-22.3 MB -> 18.6-19.2 MB.

Run: go vet ./... (mote); GOOS=linux and GOOS=windows go vet of
gateway, redact, lazyre; go test -race ./internal/lazyre (incl. a
bad-pattern test that MustAll panics); go test ./internal/redact (all);
go test ./internal/gateway -run 'TestAllCompile|OtherOff|WorkDir|
WorkPlace|RunNoise': ok. Broken by hand: TestBadPatternPanicsInMustAll
registers `(` and fails if MustAll does not panic.
Not run before the push: the whole internal/gateway package and the
full suite (a full suite of 273b9f5a9 was running on mote).
2026-11-10 12:34:14 +08:00

82 lines
3.9 KiB
Go

package gateway
import (
"net/http"
"strings"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/provider"
)
// 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 = lazyre.New(`(?:^|: )ZCode has no active limited quota for (\S+)$`)
zcodeSpentWords = lazyre.New(`(?:^|: )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
}
// zcodeTrialBlocked says c, a ZCode plugin account asked for one of its
// Start Plan trial entries, got zcode.z.ai's edge firewall's block page
// (Alibaba Cloud's 405, provider.EdgeBlocked, as the plugin's startBlocked
// words it). A trial entry is sent to zcode.z.ai alone; a dual account's
// plain models go to its GLM Coding Plan at api.z.ai, which answered while
// zcode.z.ai blocked the address (plugins#545, jinpy666). So the block
// rests the trial entry, and the account's plain model is still asked: on
// a gift-only account it goes to zcode.z.ai too, is blocked the same, and
// that rests the account (windy_66454 on Discord, 10-31: glm-trial's two
// members, GLM-5.3-Flash and GLM-5.3-Flash-Trial, shared one account rest
// and the plain one was never asked). ZCode's risk control blocking the
// account (code 3012, failRestricted) is read before, and rests as
// zcodeTrialRestricted says.
func zcodeTrialBlocked(c candidate, status int, body []byte) bool {
if status < 400 || c.p.PluginProvider() != "zcode" || !strings.HasSuffix(strings.ToLower(c.model), "-trial") {
return false
}
return provider.EdgeBlocked(wordsOf(body)) && !restrictedSaid(status, wordsOf(body))
}
// zcodeTrialRestricted says c, a ZCode plugin account asked for one of its
// Start Plan trial entries, was refused by ZCode's risk control (code
// 3012, failRestricted). That is zcode.z.ai's answer, and the entry alone
// rests: a dual account's plain models go to its GLM Coding Plan on
// api.z.ai, which kept serving them (young02036 on Discord, 11-07:
// GLM-5.3-Flash-Trial answered 3012 while the account's GLM-5.3 worked,
// and the account rested whole for two hours). On a gift-only account the
// plain model goes to zcode.z.ai too, gets its own 3012, and that rests
// the account, as zcodeTrialBlocked's block page does.
func zcodeTrialRestricted(c candidate, why string) bool {
return why == failRestricted && c.p.PluginProvider() == "zcode" && strings.HasSuffix(strings.ToLower(c.model), "-trial")
}