trae 0.2.37: the credits' date is the soonest end of a pack that still counts

credits() took the soonest end_time of every entitlement pack, spent or
already ended. A TraeCode CN account with a daily pack and a monthly pack
read "lapses today" every day, even once the daily pack was used up, so
routing kept it ahead of accounts that really lose credits today (#718;
#1157 measured an account whose date changed 15 times since 10-23). Only
a pack with credits left (or unlimited) whose end is still ahead names
the date now; none left, no date.

The enterprise and usage fixtures' end_times move into the future: with
the rule, 2026-09-21 already read as ended, and 2026-11-18 would three
days from now.

Run (mote): packages/trae `bun test` 267 pass; `bun scripts/check.mjs
trae` ok. Broken by hand: the old credits() fails the new test.
Not changed: a pack per window or runsOutAt (#718 items 1-3) need which
packs lapse and which renew, which the pack list doesn't say; Trae Global
still reads its first pack's end. #718 stays open for those. Not tried
with a real account.
This commit is contained in:
yetone
2026-11-15 00:08:38 +08:00
parent 09e5970996
commit 188c644f16
4 changed files with 37 additions and 9 deletions
+3 -3
View File
@@ -16,11 +16,11 @@ const spent = (costs) => json({ code: 0, message: "success", user_usage_list: [{
// the consumer credits page as it answers an enterprise account: a real
// allowance that real calls do not touch (measured), shown beside the pool
const page = (over = {}) => json({ is_credits_billing: true, user_entitlement_pack_list: [
{ display_desc: "老用户福利", entitlement_base_info: { quota: { credits_limit: 4000 }, end_time: 1795012095 }, usage: { credits_amount: 4.396 } },
{ display_desc: "每月登录赠送", entitlement_base_info: { quota: { credits_limit: 500 }, end_time: 1796054399 }, usage: {} },
{ display_desc: "老用户福利", entitlement_base_info: { quota: { credits_limit: 4000 }, end_time: 1895012095 }, usage: { credits_amount: 4.396 } },
{ display_desc: "每月登录赠送", entitlement_base_info: { quota: { credits_limit: 500 }, end_time: 1896054399 }, usage: {} },
...(over.extra ?? []),
] })
const creditsWindow = { name: "Credits", used: (4.396 / 4500) * 100, display: "4.4 / 4500", amount: 4.4, limit: 4500, unit: "credits", aside: true, resetsAt: new Date(1795012095 * 1000).toISOString() }
const creditsWindow = { name: "Credits", used: (4.396 / 4500) * 100, display: "4.4 / 4500", amount: 4.4, limit: 4500, unit: "credits", aside: true, resetsAt: new Date(1895012095 * 1000).toISOString() }
// the console's own period, which the usage page reads its reset date from
const RESET = 1796054400000
const core = (over = {}) => json({ code: 0, message: "", Data: { ChatUsed: 0, ChatTotal: 500, PayAsYouGoUsed: 0, PayAsYouGoTotal: 0, ResetTime: RESET, ChatStartTime: 1793462399000, ChatEndTime: 1796054399000, PayAsYouGoStartTime: 1793462399000, PayAsYouGoEndTime: 1796054399000, TotalSeats: 163, ...over }, count: 0 })
+7 -3
View File
@@ -3233,8 +3233,11 @@ const effortOf = (levels, effort) => {
// ---- usage ------------------------------------------------------------------------
// credits adds up the account's entitlement packs as the IDE shows them:
// each pack with a credit limit (-1 unlimited) and what it used
function credits(v) {
// each pack with a credit limit (-1 unlimited) and what it used. until is
// the soonest end of a pack that still has credits left and has not ended:
// a spent daily pack or one already past its end changes nothing on the
// account, so it does not name the date the credits turn over (#718, #1157).
function credits(v, now = Date.now()) {
let limit = 0
let used = 0
let unlimited = false
@@ -3248,7 +3251,8 @@ function credits(v) {
else if (l >= 0) limit += l
if (u > 0) used += u
const end = whenOf(bi.end_time ?? bi.expire_time ?? p.end_time)
if (end && (!soonest || end < soonest)) soonest = end
const left = l === -1 || (l > 0 && u < l)
if (end && end > now && left && (!soonest || end < soonest)) soonest = end
}
return { limit, used, unlimited, until: soonest }
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.36",
"version": "0.2.37",
"description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
"type": "module",
"main": "./opencode.mjs",
+26 -2
View File
@@ -11,7 +11,7 @@ afterEach(() => f?.close())
test("credits are the packs' limits and what they used", async () => {
f = fakeTrae()
f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ is_credits_billing: false, user_entitlement_pack_list: [
{ entitlement_base_info: { quota: { credits_limit: 300 }, end_time: 1790000000 }, usage: { credits_amount: 75 } },
{ entitlement_base_info: { quota: { credits_limit: 300 }, end_time: 4102444800 }, usage: { credits_amount: 75 } },
{ entitlement_base_info: { quota: {} }, usage: {} }, // a feature pack, no credits
{ entitlement_base_info: { quota: { credits_limit: 100 } }, usage: { credits_amount: 25 } },
] }))
@@ -23,7 +23,7 @@ test("credits are the packs' limits and what they used", async () => {
expect(u.user).toBe("Ann")
expect(u.signIn).toBe("kept")
expect(u.balance).toBeUndefined() // the window carries the count (yetone/magpie#694)
expect(u.windows).toEqual([{ name: "Credits", used: 25, amount: 100, limit: 400, unit: "credits", resetsAt: new Date(1790000000 * 1000).toISOString() }])
expect(u.windows).toEqual([{ name: "Credits", used: 25, amount: 100, limit: 400, unit: "credits", resetsAt: new Date(4102444800 * 1000).toISOString() }])
})
test("unlimited packs say so", () => {
@@ -364,3 +364,27 @@ test("a reply limit is Trae's own or none, and a request is held to Trae's", asy
expect(await ask("glm-5.2", 8000)).toBe("8000")
expect(await ask("deepseek-v4.1-flash", 384000)).toBe("384000")
})
// the date the credits turn over is a pack's that still counts: a spent
// pack, or one already past its end, names nothing (#718, #1157)
test("the soonest end is a live pack's", () => {
const now = Date.parse("2026-11-14T12:00:00Z")
const sec = (iso) => Date.parse(iso) / 1000
const pack = (limit, used, end) => ({ entitlement_base_info: { quota: { credits_limit: limit }, end_time: sec(end) }, usage: { credits_amount: used } })
const c = _internal.credits({ user_entitlement_pack_list: [
pack(300, 300, "2026-11-14T16:00:38Z"), // today's daily pack, spent
pack(500, 10, "2026-11-13T16:00:00Z"), // yesterday's, ended with credits left
pack(3000, 0, "2026-11-30T15:59:59Z"), // the monthly pack
] }, now)
expect(c.until).toBe(Date.parse("2026-11-30T15:59:59Z"))
expect(c.limit).toBe(3800)
expect(c.used).toBe(310)
// the daily pack with credits left is the soonest again
const d = _internal.credits({ user_entitlement_pack_list: [pack(300, 100, "2026-11-14T16:00:38Z"), pack(3000, 0, "2026-11-30T15:59:59Z")] }, now)
expect(d.until).toBe(Date.parse("2026-11-14T16:00:38Z"))
// an unlimited pack counts as one with credits left
const u = _internal.credits({ user_entitlement_pack_list: [pack(-1, 5, "2026-11-20T00:00:00Z"), pack(3000, 0, "2026-11-30T15:59:59Z")] }, now)
expect(u.until).toBe(Date.parse("2026-11-20T00:00:00Z"))
// nothing live: no date at all
expect(_internal.credits({ user_entitlement_pack_list: [pack(300, 300, "2026-11-20T00:00:00Z")] }, now).until).toBe(0)
})