From ec3111d8731058b2b2112fb46302e07f0d664ba5 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 3 Nov 2026 15:43:00 +0800 Subject: [PATCH] usage: a pool set aside counts in its card's credits a day (jiabubu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jiabubu on Discord (#feedback): Usage → Overview's "Credits used per day" for WorkBuddy left out the extra credits, and read 0 every day while only extra credits were spent. The WorkBuddy plugin (0.2.19) sends the plan's "Credits" and "Extra credits", the one whose running out doesn't stop the account set aside (aside: true), usually Extra credits. creditWindows dropped every window set aside whenever the card had one that wasn't, a rule meant for CodeArts, whose buckets set aside are its 总积分's credits again. So WorkBuddy's extra credits, Kiro's bonus credits and a MiniMax credits window set aside were never counted. Now every credit window is read, and noteDailyCredits tells a breakdown from a pool by what the readings used: a window set aside counts unless the card's counted windows in its unit used at least as much over the same span, which makes it their credits again (CodeArts). It is kept as creditsAccount.Part; a window set aside that is a breakdown, with no days of its own, isn't shown. No vendor is named in the rule. Days before this release can't be recovered: magpie never kept the extra credits' readings, so the chart counts them from the first reading after the update. Tests: TestDailyCreditsPoolSetAside, WorkBuddy's card shaped as the plugin sends it for a China free account (CN_FREE_SUMMARY's 0 / 500 and 282 / 2900 in the plugins repo's workbuddy usage.test.mjs), the plan untouched while extra credits grow, then the plan spent and set aside instead; and a Kiro card with bonus credits. Red on the old credits_daily.go (no days for Extra credits or Bonus credits). TestDailyCreditsEveryVendor (CodeArts' buckets) still counts only 总积分. Broken by hand: Part always false makes TestDailyCreditsEveryVendor red (基础积分 counted beside 总积分). Run at the pushed head on mote: gofmt -l clean; go test -run 'TestDailyCredits|TestPluginPlanCredits|Credits' ./internal/provider ./internal/gateway ok; GOOS=linux go vet -tags nogui and GOOS=windows go vet of provider, gateway, gui ok (rebased on origin/main b92b3bc5b). The full GUI suite (WSL, both engines, every language) ran at the earlier base 50265256b; its 58 files red on head but not in main's run were run again, and the 11 files still in question were run on head and on origin/main b92b3bc5b side by side: routing-credits and i18n-keys pass, and every other red is a test main fails too, with an open issue (#4489 usage-ledger-ttft, #4719 usage-speed, #3593/#4560/#4585 routing-rules-line, #4713 routing-time-went, #2559/#4966 routing-bodies, #4400 sessions-bar-names, #1745 group-member-info, #5052 account-eye); head shows a few more languages of those same tests. Not tried: a real WorkBuddy account's readings over a day; the fixture is the plugin's own window shapes, not jiabubu's bytes (not seen). --- internal/provider/credits_daily.go | 66 +++++++++++++++--- internal/provider/credits_daily_pools_test.go | 67 +++++++++++++++++++ 2 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 internal/provider/credits_daily_pools_test.go diff --git a/internal/provider/credits_daily.go b/internal/provider/credits_daily.go index 74b07fd8e..cdac4fef9 100644 --- a/internal/provider/credits_daily.go +++ b/internal/provider/credits_daily.go @@ -16,8 +16,12 @@ package provider // window is counted on its own, by provider, account and window name, so // a card's several pools (Qoder's Add-on credits, Kiro's Free trial) never // mix. A pool's breakdown set aside (CodeArts' buckets under its total) -// counts only when the card has nothing else, and of one allowance seen -// over several spans (Open Design's 5 hours and week) only the longest. +// is told by what the readings used: one set aside counts unless the +// card's counted windows in its unit used at least as much over the same +// span, which makes it their credits again; a pool set aside on its own +// (WorkBuddy's Extra credits, which it spends while the plan's stand +// still: jiabubu) counts as any other. Of one allowance seen over several +// spans (Open Design's 5 hours and week) only the longest counts. // // A reading that has used less than the one before is a cycle started // again or a credit pack gone: what was used between the two can't be @@ -84,6 +88,11 @@ type creditsAccount struct { Since string `json:"since"` Last creditsReading `json:"last"` Days map[string]float64 `json:"days,omitempty"` + // Part is set on a window set aside that the last reading it used + // anything in took for another window's credits broken down (see + // noteDailyCredits); before it used any, on one set aside beside a + // window in its unit that isn't. + Part bool `json:"part,omitempty"` } // creditsMu keeps two readings in one magpie from writing the file at once; @@ -232,6 +241,10 @@ func creditOf(w QuotaWindow) (creditWindow, bool) { // creditWindows are q's windows counted by the day, from a reading just // made: none of a card that failed or one kept from before. +// +// Those set aside are among them: whether one is a pool of its own or the +// breakdown of another is told by what each reading used (see +// noteDailyCredits), not by its being set aside. func creditWindows(q SubscriptionQuota) []creditWindow { if q.Error != "" || q.AsOf != nil { return nil // a reading kept from before isn't a new one @@ -242,11 +255,6 @@ func creditWindows(q SubscriptionQuota) []creditWindow { all = append(all, c) } } - // a pool's breakdown set aside counts only when nothing else does: - // CodeArts' buckets are its total's credits again - if slices.ContainsFunc(all, func(c creditWindow) bool { return !c.aside }) { - all = slices.DeleteFunc(all, func(c creditWindow) bool { return c.aside }) - } // one allowance over several spans (5 hours, a week): the longest's // count alone, which a rolling short one's drops don't cut into for i := 0; i < len(all); i++ { @@ -284,7 +292,17 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { day := now.Local().Format("2006-01-02") changed := false for _, q := range qs { - for _, c := range creditWindows(q) { + ws := creditWindows(q) + type step struct { + c creditWindow + a *creditsAccount + d float64 + } + var steps []step + // what the card's windows used since their last readings, by unit: + // those counted (n) and those set aside (aside) + n, aside := map[string]float64{}, map[string]float64{} + for _, c := range ws { key := creditsKey(q, c.name) a := accts[key] if old := accts[creditsOldKey(q)]; a == nil && old != nil && c.name == "Credits" { @@ -294,7 +312,11 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { changed = true } if a == nil { - a = &creditsAccount{Since: day} + // one set aside beside a counted window in its unit is + // taken for that window's breakdown until it uses + // credits that window doesn't + part := c.aside && slices.ContainsFunc(ws, func(x creditWindow) bool { return !x.aside && x.unit == c.unit }) + a = &creditsAccount{Since: day, Part: part} accts[key] = a a.Last = creditsReading{Used: c.used, Total: c.total, At: now} changed = true @@ -303,7 +325,28 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { if c.used == a.Last.Used && c.total == a.Last.Total { continue } - if d := c.used - a.Last.Used; d > 0 { + d := max(c.used-a.Last.Used, 0) + if c.aside { + aside[c.unit] += d + } else { + n[c.unit] += d + } + steps = append(steps, step{c, a, d}) + } + for _, s := range steps { + c, a, d := s.c, s.a, s.d + if c.aside && d > 0 { + // A window set aside is a pool of its own (WorkBuddy's + // Extra credits, Kiro's bonuses, jiabubu) unless the + // counted windows in its unit used at least as much in + // the same span: then it is their credits broken down + // (CodeArts' buckets under its 总积分), counted there. + a.Part = n[c.unit] > 0 && aside[c.unit] <= n[c.unit]+0.005 + if a.Part { + d = 0 + } + } + if d > 0 { if a.Days == nil { a.Days = map[string]float64{} } @@ -373,6 +416,9 @@ func withDailyCredits(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota d.Days = append(d.Days, DayCredits{Day: day, Used: used}) } } + if a.Part && len(d.Days) == 0 { + continue // another window's breakdown, counted there + } slices.SortFunc(d.Days, func(x, y DayCredits) int { return strings.Compare(x.Day, y.Day) }) all = append(all, d) } diff --git a/internal/provider/credits_daily_pools_test.go b/internal/provider/credits_daily_pools_test.go new file mode 100644 index 000000000..220d0207d --- /dev/null +++ b/internal/provider/credits_daily_pools_test.go @@ -0,0 +1,67 @@ +package provider + +import ( + "encoding/json" + "reflect" + "testing" + "time" + + "github.com/yetone/magpie/internal/testenv" +) + +// TestDailyCreditsPoolSetAside: a pool set aside is counted by the day as +// any other (jiabubu on Discord: WorkBuddy's credits a day said 0 while +// only its Extra credits were spent). WorkBuddy's card is the plugin's +// own for a China-site free account (CN_FREE_SUMMARY in the plugins repo's +// workbuddy/usage.test.mjs): the plan's Credits, and Extra credits set +// aside until the plan's are spent. A breakdown set aside (CodeArts' +// buckets, TestDailyCreditsEveryVendor) still counts nothing of its own. +func TestDailyCreditsPoolSetAside(t *testing.T) { + testenv.SetHome(t, t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + day := func(d, h int) time.Time { return time.Date(2026, 11, d, h, 0, 0, 0, time.Local) } + wb := func(plan, extra float64, spent bool) SubscriptionQuota { + pa, ea := "", `,"aside":true` + if spent { + pa, ea = `,"aside":true`, "" + } + return pluginWindowsCard(t, "workbuddy", "jiabubu", `[{"name":"Credits","used":`+jsonNum(100*plan/500)+`,"display":"`+jsonNum(plan)+` / 500","amount":`+jsonNum(plan)+`,"limit":500,"unit":"credits"`+pa+`},`+ + `{"name":"Extra credits","used":`+jsonNum(100*extra/2900)+`,"display":"`+jsonNum(extra)+` / 2900","amount":`+jsonNum(extra)+`,"limit":2900,"unit":"credits"`+ea+`}]`) + } + kiro := func(month, bonus float64) SubscriptionQuota { + return pluginWindowsCard(t, "kiro", "k", `[{"name":"Credits","used":1,"display":"x","amount":`+jsonNum(month)+`,"limit":50,"unit":"credits","span":2592000},`+ + `{"name":"Bonus credits","used":1,"display":"x","amount":`+jsonNum(bonus)+`,"limit":500,"unit":"credits","aside":true}]`) + } + read := func(at time.Time, qs ...SubscriptionQuota) { noteDailyCredits(qs, at) } + read(day(1, 9), wb(0, 281.9999965, false), kiro(50, 0)) + // only the extra credits move: WorkBuddy spends them first + read(day(1, 12), wb(0, 290.5, false), kiro(50, 12)) + // the first reading of the shown pool alone: nothing yet + if d := withDailyCredits([]SubscriptionQuota{wb(0, 290.5, false)}, day(1, 13))[0]; d.Daily == nil || len(d.MoreDaily) != 1 { + t.Fatalf("after the extra credits moved: %+v %+v", d.Daily, d.MoreDaily) + } + read(day(2, 9), wb(20, 290.5, false), kiro(50, 30)) + // the plan's spent: the plugin sets its window aside instead + read(day(2, 12), wb(500, 290.5, true)) + read(day(2, 15), wb(500, 300.5, true)) + + got := withDailyCredits([]SubscriptionQuota{wb(500, 300.5, true), kiro(50, 30)}, day(2, 16)) + want := map[string][]*DailyCredits{ + "jiabubu": { + {Since: "2026-11-01", Days: []DayCredits{{"2026-11-02", 500}}, Window: "Credits"}, + {Since: "2026-11-01", Days: []DayCredits{{"2026-11-01", 8.5}, {"2026-11-02", 10}}, Window: "Extra credits"}, + }, + "k": { + {Since: "2026-11-01", Days: []DayCredits{}, Window: "Credits"}, + {Since: "2026-11-01", Days: []DayCredits{{"2026-11-01", 12}, {"2026-11-02", 18}}, Window: "Bonus credits"}, + }, + } + for _, q := range got { + all := append([]*DailyCredits{q.Daily}, q.MoreDaily...) + if !reflect.DeepEqual(all, want[q.User]) { + b, _ := json.Marshal(all) + w, _ := json.Marshal(want[q.User]) + t.Errorf("%s's days: %s, want %s", q.Provider, b, w) + } + } +}