routes: a frugal/expiring routing — free first, then what is lost soonest, then what never runs out
Note on the form: this repository only accepts pull requests from collaborators (the PR button says so), so this cannot be opened as a PR. The change is already written, tested and pushed to a branch — it applies cleanly if a maintainer wants to take it:
https://github.com/MYCF711/magpie/tree/frugal-routing(one commit ahead ofmain, 2 files added, 0 lines of upstream edited). Close this if the routing isn't wanted; nothing here asks to be merged.
What it is
A routing in four tiers, for a pool whose seats are worth different amounts at different times:
| tier | who | why here |
|---|---|---|
| free | its vendor serves the model at no cost to the subscription's allowance or credits (Seat.Free) |
a free seat spends nothing by running out of, so running low is no reason to push it behind one that spends |
| expiring | what it holds runs out at a known moment (Allowance.Renewal) |
the sooner it is lost, the sooner it should be spent |
| endless | nothing it holds ever runs out: no window, or one that never renews | spending it costs something still there tomorrow |
| unknown | its allowance isn't known | kept after those known, as Smart keeps them |
Within the expiring tier the earliest loss wins; seats alike to the hour
keep their listed order, so a few minutes don't reorder and the vendor's
prompt cache stays warm — the same reading Smart takes of a renewal.
Unlike Smart, this goes by the moment for every window that counts
the model, not the week alone: a daily allowance with an hour left goes
before a weekly one with two, whichever the vendor calls it.
Why it is wholly additive
The id is frugal/expiring, not frugal, on purpose. provider.PluginRouting
already accepts "<plugin>/<name>", so:
- saving it needs no line added to the whitelists in
provider.goandgroup.go - the CLI and TUI take it for the same reason
routes.Registerininit()only needs the name, not an entry inBuiltin
So the change is two new files under internal/routes/ and nothing else.
Verification
At 7499ebb5 (the head the branch is based on):
gofmt -l internal/routes/frugal.go internal/routes/frugal_test.go → no output
go vet ./internal/routes/ → no output
go test ./internal/routes -count=1 → ok
Nine cases: each tier, all four together, the hour-rounding both ways (alike-to-the-hour keeps order; an hour apart does not), and that the first window decides.
The test fails without the change. Two break checks by hand:
FrugalFirst's body replaced by identity(len(seats)) → the four order
cases fail; ByFree cleared → the free tier empties and
free before any that spends fails.
Against a real magpie (0.1.1559, Windows) with a group of 14 seats over 7 providers: with the routing set on the group, the request reached the free model first and the used-up accounts went last.
Why not a gateway plugin
A JavaScript gateway routing (*.gateway.js) can express tiers 2–4 today and
needs no Go at all. It cannot express tier 1: seatJSON
(internal/middleware/points.go) carries no Free, and the moejs sandbox has
no fetch and no fs, so a plugin can only recover "is this free" from a
static table it was handed — a guess that goes stale when a vendor changes its
price.
Strategy.ByFree is what has the gateway fill Free from provider.FreeModel,
which reads the vendor's own word and the live list price. Only a Go strategy
can set it. That is the one thing a plugin cannot do, and the reason this asks
to live in the core rather than in the community's gateway plugins.
分支已重建:pr/frugal-expiring
上面指的 frugal-routing 分支是基于 7499ebb5 的,上游这几天走了很远,它已经落后太多、不能直接用了。重建在当前上游 de54b4ff 上:
git fetch https://github.com/MYCF711/magpie.git pr/frugal-expiring
新分支比原来多两件事(主体 frugal.go 与它的测试不变,仍是新增文件、不动上游代码):
- 两行 GUI,把它列进组与服务商的编辑器 —— magpie 提供、但页面不认识的路由,用户是选不到的(
ROUTING_OPTS与MODES)。 - 子系统文档
docs/subsystems/gateway-routing.md里 Order 那一行补上它。
仍是基于当前上游的一笔提交、零行上游代码被改:
git diff --stat de54b4ff pr/frugal-expiring
docs/subsystems/gateway-routing.md | 2 +-
internal/gui/assets/app.js | 1 +
internal/gui/assets/plugins/routing/client.js | 10 +-
internal/routes/frugal.go | 113 +++++
internal/routes/frugal_test.go | 162 +++++
验证(在该分支上单独检出后跑,不是只在合并状态下):
gofmt -l → 无输出
go vet linux / windows / nogui → 三平台都过
go build linux / windows → 都过
go test ./internal/routes/ → ok(4 个测试,13 个子用例)
internal/gui 的脚本预算测试 → ok
真实 magpie(14 个座位、7 个服务商的组):设上这个路由后,请求先到免费模型,用尽的账号排在最后,与层级一致。
Thanks @MYCF711 for the branch (pr/frugal-expiring). I read every line and ran it, and we're not taking it into magpie. Here's why.
- It ignores how much of a seat is used.
FrugalFirstsorts known seats by their soonest renewal alone. In a probe on your branch, a seat at 100% used that renews in 1h went before a seat at 10% used. The request then goes to the spent seat first, fails, and falls over. Smart keeps spent seats last for exactly this reason. Your four tests (9 cases) pass, but none of them has a used-up seat. - Free first and Smart already cover it. Free first puts the seats that cost nothing first. Paid seats are then routed by their provider's own routing, and Smart or Weekly pace already spends first what is lost soonest at its reset. "Free, then the allowance that expires soonest" is Free first on a group whose providers are on Smart.
FreeFirst's own doc says to set it up that way. - How it's built. It registers under
frugal/expiring, which looks like a JS gateway plugin's<plugin>/<name>id, from aninit()that always runs. So it isn't tied to the rotation plugin as the description says. It also carries unused code (FrugalSummary,FrugalRoutings,FrugalIsRoutingName, a hard-coded Chinese name) and comments about fork builds.
If Free first plus Smart misses a case you hit, please post the route (Routing page → the request → copy) and we'll look at it. If you want to experiment with routings like this, the path we'd take is to expose Seat.Free to JS gateway routings, so it can live as a community plugin. That's open for discussion here.
I'm leaving this open for you to reply.
Thanks — all three land, and I won't argue them.
-
You're right, and it's the fatal one.
FrugalFirstsorts the paid tier bySoon[0]alone and never readsUsed. Reproduced on the branch: seatsspent(Used 100, renews in 1h) andfresh(Used 10, renews in 24h) giveFrugalFirst = [0 1]— the spent one first — againstSmart = [1 0]. None of my four cases had a used-up seat in it, so the suite passed a routing that would hand the first request of every window to a seat with nothing left. -
Agreed on the composition: it shouldn't put in the core what
freeplus a provider's own routing already says. -
Fair on the shape too.
init()registers it whether or not the rotation plugin is on, so it isn't the plugin routing its id pretends to be, andFrugalSummary,FrugalRoutings,FrugalIsRoutingNameand the hard-coded Chinese name are dead weight I shouldn't have shipped.
One measurement worth having, since it took me a reading of ownRouting to believe it — Free first alone does not leave the paid seats to Smart. A provider left on Smart inside a free group has p.Routing == "", so ownRouting is false and its keys are pooled with the group and ordered by Free first, i.e. their listed order. At 479de890, a group paida(10d) paidb(1h) freev routed free orders freev paida paidb; the same pool with the paid two in a nested Smart group orders freev paidb paida. So "Free first on a group whose providers are on Smart" needs the paid ones in a group of their own, and FreeFirst's doc — "a provider or group in this group keeps its own routing either way" — reads as if a provider on Smart is one of those, which it isn't.
The tier I'd still put on the table is the first, since it's the one a gateway routing can't reach: seatJSON carries no Free, and with no fetch/fs in moejs a plugin can only recover "is this free" from a table it was handed, which goes stale when a vendor changes its price. If exposing Seat.Free to gateway routings is on the table, I'd take that over anything in the core — the other three tiers a plugin can do today.
Leaving the branch as it is and closing this; nothing here needs merging.