loomy: the total names no reset while only permanent credits are left (#1153)

With the daily credits spent and permanent ones left, the 积分合计 window
still said it reset at Beijing midnight. magpie's smart routing ranks the
soonest reset first, so such an account went ahead of trae-cn's, Qoder's
and WorkBuddy's day and month windows, whose credits are lost at their
reset, while midnight loses nothing of Loomy's. The total now names a
reset (and its span: magpie reads a span with no reset as renewing a span
from now) only while daily credits are left, or when nothing is left at
all, so a used-up account still says when it is back. The daily wallet
beside it, set aside for routing, keeps showing midnight.

loomy 0.1.9.

Run on mica (Bun 1.4.2), sandbox HOME: bun test in packages/loomy 33
pass; scripts/check.mjs loomy clean. Broken by hand: with main's
index.mjs the usage test fails (resetsAt 1794672000000, expected none).
Checked in magpie origin/main: Smart puts a window with no known reset
after those with one (internal/routes/strategies.go), and a window
set aside never enters the allowance (provider/routing.go).
Not tried: a real Loomy account in routing.
This commit is contained in:
yetone
2026-11-14 22:53:12 +08:00
parent 9f02ad545f
commit 09e5970996
3 changed files with 20 additions and 7 deletions
+11 -4
View File
@@ -80,8 +80,11 @@ const beijingMidnight = () => Math.floor((Date.now() + 8 * 3600e3) / 86400e3) *
// wallet has spent; the permanent balance has nothing spent to tell, so the
// total is spent of spent + left, and with nothing left it is all used. It
// renews with the daily wallet at Beijing midnight, when the daily credits
// left are lost (runsOutAt, runsOut), so routing spends those first. null
// with neither wallet told.
// left are lost (runsOutAt, runsOut), so routing spends those first. With
// the daily credits spent and permanent ones left, midnight loses nothing,
// so the total names no reset: smart routing puts an account whose reset
// does lose credits first (plugins#1153). Used up, it names midnight, when
// it is back. null with neither wallet told.
function totalOf(dailyWallet, dailyLeft, permanent) {
if (dailyLeft == null && permanent == null) return null
const spent = dailyWallet?.amount ?? 0
@@ -96,8 +99,12 @@ function totalOf(dailyWallet, dailyLeft, permanent) {
unit: "credits",
}
if (dailyWallet) {
w.resetsAt = beijingMidnight()
if (dailyWallet.span) w.span = dailyWallet.span
// no span either: magpie reads a window with a span and no reset as
// renewing a span from now, which would rank it again
if (dailyLeft > 0 || left === 0) {
w.resetsAt = beijingMidnight()
if (dailyWallet.span) w.span = dailyWallet.span
}
if (dailyLeft > 0) Object.assign(w, { runsOutAt: w.resetsAt, runsOut: dailyLeft })
}
return w
+8 -2
View File
@@ -361,13 +361,19 @@ describe("usage", () => {
// the daily wallet at 100% is shown, set aside
expect(ws.find((w) => w.name === "每日赠送积分")).toMatchObject({ used: 100, aside: true })
expect(ws.find((w) => w.name === "永久积分")).toMatchObject({ display: "1200 credits", aside: true })
// nothing of the daily wallet left: nothing lapses at midnight
// nothing of the daily wallet left: nothing lapses at midnight, so the
// total names no reset and smart routing puts accounts whose reset
// loses credits ahead of it (plugins#1153); the daily wallet beside it
// still shows midnight
expect(total.runsOutAt).toBeUndefined()
expect(total.resetsAt).toBe(_internal.beijingMidnight())
expect(total.resetsAt).toBeUndefined()
expect(total.span).toBeUndefined()
expect(ws.find((w) => w.name === "每日赠送积分").resetsAt).toBe(_internal.beijingMidnight())
// both gone: used up, back at Beijing midnight
ws = await read({ balance: 0, dailyBalance: 0, dailyLimitPoints: 5000, dailyRemainingPoints: 0 })
expect(counted(ws)[0]).toMatchObject({ used: 100, amount: 5000, limit: 5000 })
expect(counted(ws)[0].resetsAt).toBe(_internal.beijingMidnight())
// 3,000 daily credits left are lost at midnight: runsOutAt / runsOut
ws = await read({ balance: 1200, dailyBalance: 3000, dailyLimitPoints: 5000, dailyRemainingPoints: 3000 })
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-loomy-auth",
"version": "0.1.8",
"version": "0.1.9",
"description": "OpenCode & Magpie provider plugin for Loomy (讯飞, loomyad.xunfei.cn): SMS sign-in, chat, models, the two point wallets and the daily first-login grant",
"type": "module",
"main": "./opencode.mjs",