magpie icon indicating copy to clipboard operation
magpie copied to clipboard

routing: the seat carries when an allowance expires but not how much — strategies cannot rank by what is about to be lost

Open hxyinan opened this issue 1 month ago • 1 comments

What

A strategy can see when an allowance is lost (Seat.Soon, from Allowance.Renewal) but not how much is lost then. Seat.Used is a share (0–100), and the absolute count that a vendor reports beside its expiry — QuotaWindow.RunsOut ("how many credits are lost at RunsOutAt") — is dropped one layer earlier: allowanceOf copies w.RunsOutAt into Limit.RunsOut but has nowhere to put w.RunsOut.

Limit does carry Amount/Of/Unit, but those are the window's whole count (used of limit), not the part about to expire. On a plan where credits arrive in batches with different end dates, the interesting number is the second one.

Why it matters

WorkBuddy is the case I hit. One account holds:

batch credits left lost at
1500-point bonus pack 1500 11-14
small pack 100 11-23
small pack 100 11-24
daily slice / base credits 387 2035 (never)

Renewal() correctly puts 11-14 first, so Smart and free order these accounts by that date. But a strategy cannot answer the question the user actually has — which account wastes the most by waiting? — because:

  • two accounts both expiring on 11-14 are indistinguishable when one has 10 credits left and the other 2000;
  • "credits lost per hour" (count ÷ hours until RunsOutAt), which is what you want to rank by, needs the count.

Percentage does not substitute: Used is a share of the whole plan (2200 here), and the expiring batch is only part of it. An account at 5% used may still hold its full 1500-point batch, because the vendor spends the daily slice first.

What this costs a user today

With free routing (or any custom strategy) the best available ranking is by expiry date only. On this plan that happens to give the right order, but only because every account's earliest batch is the same size (1500). Add an account whose first batch is 200 credits and the order silently goes wrong — it will be burned ahead of 1500-point batches expiring on the same day.

What I patched locally (to show the shape of the fix)

Four small changes, all additive:

  1. provider.Limit — add RunsOutAmount float64; in allowanceOf, l.RunsOutAmount = w.RunsOut where w.RunsOutAt != nil.
  2. provider.Allowance — add LostNow(model string, now time.Time) float64: the RunsOutAmount of the window whose RunsOut comes soonest, 0 when none says.
  3. routes.Seat — add RunsOut float64; fill it in weighRouted from a.LostNow(c.model, now) (via the existing left cache).
  4. middleware.seatJSON — add RunsOut float64 json:"runsOut"`` so JS strategies get it.

Then a strategy can do what the user wants:

tier 1: s.RunsOut > 0 && !s.Soon[0].After(now.Add(90*24*time.Hour))
rank  : s.RunsOut / hoursUntil(s.Soon[0])   // credits lost per hour, most first

Suggestion

  • Carry the count into the seat, as above. It is one field on Limit, one on Seat, one on seatJSON, and it costs nothing when a vendor reports no RunsOut.
  • While in that file: seatJSON also omits Seat.Free and Seat.Credits, so a JS strategy cannot see whether a model is free at all — FreeFirst relies on the Go side filling it. Both are already on Seat; exposing them costs nothing and lets the same strategy be written in JS.
  • Say in the docs that Used is a share and RunsOut an absolute count in the vendor's unit, so strategies can combine them instead of guessing.

Related

  • #3385 — Free first ordering each band by another routing: this issue is the missing input for "which expiring band first".
  • #2265 — plugin host drops runsOutAt/runsOut on the provider side; that one is about the value never reaching the router at all.
  • #3064 — balance-only accounts have no Count for Smart to compare; same family of "the seat has less than the vendor told us".

Happy to send the four patches as a PR if the shape looks right.

hxyinan avatar Oct 23 '26 21:10 hxyinan

Adding a second, worse case for the same missing field — a prepaid wallet, not a batch plan.

SuperMagic (超级麦吉 / MagiCrew) exposes only total_points (current balance) and expiring_quota_details (remaining + expires_at per batch). No total, no consumed: organization_limit_points / user_limit_points are null on personal accounts, and POST /api/v1/quota/points/records/queries answers HTTP 500 on both sites (5 body shapes tried). So unlike WorkBuddy there isn't even a Used share to work with — the entire allowance is a balance.

The plugin currently sends three windows:

window what used limit runsOut
今日积分 today's 23:59 batch (daily login gift, 200 CN / 50 intl per day) real % daily cap —
限时积分 remaining of all other expiring batches 0 current remainder earliest batch's remaining
永久积分 total − Σexpiring (never expires) 0 — —

That gets Known = true and a routing row, but the ranking still cannot answer any of the three useful questions:

  • who runs out first — yes: Renewal() folds RunsOutAt in, so Soon works.
  • who loses more by waiting — no: w.RunsOut (the count) is dropped in allowanceOf, exactly as you describe.
  • who has more left at all — no: Seat.Used is 0 for every wallet account, and the remainder only reaches the GUI as limit/amount (a tautology — denominator = remainder).

Measured today on 4 accounts: 1,363 / 1,693 / 6,291 / 750 points, earliest expiries 178 / 29 / 29 / 372 days out. Under smart all four land in the same tier and tie on Soon whenever expiry days are alike; under pace they are exactly identical (no resetsAt, used 0), so a 6,291-point account and a 750-point one are interchangeable.

Your four patches are exactly the missing half. One more field would cover the wallet shape too: a balance on the seat — Left float64, filled from a QuotaWindow.Balance (or from limit when the window is a balance rather than a share) — so Smart / Pace can fall back to "most left" when Used says nothing.

Happy to test a PR against a real prepaid provider if you push a branch.

MYCF711 avatar Oct 27 '26 22:10 MYCF711