usageFromStatistics took the bonus window's runsOutAt/runsOut from the
soonest check-in batch while remaining/used were the whole bucket, and the
total's from every non-basic batch whatever the buckets had left. So an
account whose check-in was spent (bonus remaining 0) still had a total
runsOut of 82.05 at tonight's midnight, and one with 2344.81 long-lived
gift credits said 100 lapse in hours while the vendor's
package_credit_expiring_amount was 0. magpie's routing reads a total's
RunsOutAt as "what is left is lost then" (Allowance.Renewal), so these
accounts were ranked ahead of ones that really lapse sooner.
Now:
- each bucket's lapse is its own batches' soonest, held to that bucket's
package_credit_remain; an empty bucket has none;
- the total's lapse is the soonest of the buckets' lapses (same Beijing
day summed), never from batches of a bucket with nothing left;
- the ledger fallback's batches are marked `estimated` (what was spent of
a check-in isn't known): they say when, and how much is the vendor's
own expiring amount, so with 0 or none said there is no lapse; the
vendor's batch reads (with consumed/expired counts) are used as before;
- each window carries the vendor's package_credit_expiring_amount as
`expiring` when it is sent;
- comments and README say the ledger keeps the date the claim names (a
day on for today's check-ins); 30 days is only for a claim with none.
Run (wsl-run, magpie-desktop, at this tree rebased on origin/main):
bun test packages/codearts: 177 pass, 0 fail; bun scripts/check.mjs: ok.
Fixtures are the issue's two accounts as filed (A: bonus 100 used, 0
left, total 82.05 left; B: 4100 / 2344.81 left, ledger grant 100 claimed
2026-11-10T16:01:04.916Z expiring 2026-11-11T16:01:03.812Z, expiring 0),
clock held at 2026-11-11T10:00Z.
Broken by hand: cap = Infinity in bucketLapse: the three #982 tests red
(174 pass, 3 fail). cap = remain (no estimated gate): account B's test
red alone (176 pass, 1 fail).
Not tried: a real CodeArts account; the vendor's horizon for
package_credit_expiring_amount isn't known, so vendor batch reads are not
gated on it. A lagging vendor batch counter is bounded by the bucket's
remainder only.
Reported-by: KiMelody