From 42f78ba08e52809678f3c8d5b20c0388bdcfea2f Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 7 Nov 2026 08:07:58 +0800 Subject: [PATCH] usage: WorkBuddy's credits a day count each pool, adding up to Total credits (yapechan) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit yapechan on Discord (#feedback, 11-07, WorkBuddy China): the card's "Credits used per day" read 今天 0 for 积分 and 额外积分 and 今天 34.06 for 积分合计, so the total wasn't the two pools' sum. Since the plugin's 0.2.22/0.2.24 the card has both pools set aside, Credits and Extra credits, and Total credits, the two together, as the window that stops the account. noteDailyCredits takes a window set aside for a counted window's breakdown when that window used at least as much over the same span (the CodeArts rule, ec3111d87), and the total always does, so both pools were marked Part and counted nothing; they still showed because they had days from before 0.2.22. The card's figures themselves (x / 500, y / 2900, x+y / 3400) were right; only the days were. Now pools set aside that together grant what a counted window grants (poolsOfWhole: two or more, same unit, limits summing to its limit) are that whole's pools: each counts its own days beside the whole's, and is shown even with none. A bucket that is only part of its whole (CodeArts' 基础积分 under 总积分) stays its breakdown. Both WorkBuddy builds send the same shape, so workbuddy-ai is covered too. Days already counted can't be split again: today's 34.06 stays on the total alone. TestDailyCreditsPoolsOfWhole (new): the plugin's current card for CN_FREE_SUMMARY's shape, the extra credits moving alone, then the plan's, then both: Credits 20 + Extra credits 28 = Total credits 48; and a CodeArts-like card with two buckets that don't make up its whole shows 总积分 alone. Run at this head (origin/main 8e350155b) on mote: gofmt -l clean; go test -count=1 ./internal/provider ok (115s); GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider ok. Broken by hand: the old credits_daily.go makes the new test red (Total credits 48 alone); dropping the sum check makes its CodeArts half red (基础积分 shown). Not tried: yapechan's own readings or a real China account with extra credits (the desktop's sign-in on this Mac gets 401 from the meter); no GUI change, so no GUI run. Reported-by: yapechan --- internal/provider/credits_daily.go | 52 +++++++++++++++-- internal/provider/credits_daily_pools_test.go | 56 +++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/internal/provider/credits_daily.go b/internal/provider/credits_daily.go index cdac4fef9..9a48da25d 100644 --- a/internal/provider/credits_daily.go +++ b/internal/provider/credits_daily.go @@ -20,7 +20,8 @@ package provider // 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 +// still: jiabubu) counts as any other, and so do the pools set aside that +// together grant what a counted window does, its whole (poolsOfWhole). 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 @@ -293,6 +294,7 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { changed := false for _, q := range qs { ws := creditWindows(q) + pools := poolsOfWhole(ws) type step struct { c creditWindow a *creditsAccount @@ -314,8 +316,9 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { if a == nil { // 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 }) + // credits that window doesn't; one of a whole's pools + // never is + part := c.aside && !pools[c.name] && 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} @@ -341,7 +344,7 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { // 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 + a.Part = !pools[c.name] && n[c.unit] > 0 && aside[c.unit] <= n[c.unit]+0.005 if a.Part { d = 0 } @@ -380,6 +383,38 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { _ = writeFileAtomic(path, b) } +// poolsOfWhole are the windows set aside that a counted window is the +// whole of: together, in its unit, they grant what it grants (WorkBuddy's +// Credits and Extra credits under its Total credits, plugin 0.2.24 on). +// Each is a pool of its own and counts its own days beside the whole's, +// so the pools' days add up to the whole's (yapechan on Discord: 今天 0 +// for 积分 and 额外积分 beside 34.06 for 积分合计, the pools taken for the +// whole's breakdown and counted there). A bucket that is only part of +// what the whole grants (CodeArts' 基础积分 under its 总积分) stays its +// breakdown. +func poolsOfWhole(ws []creditWindow) map[string]bool { + out := map[string]bool{} + for _, whole := range ws { + if whole.aside { + continue + } + var of []string + sum := 0.0 + for _, c := range ws { + if c.aside && c.unit == whole.unit { + of = append(of, c.name) + sum += c.total + } + } + if len(of) > 1 && math.Abs(sum-whole.total) <= 0.005 { + for _, name := range of { + out[name] = true + } + } + } + return out +} + // withDailyCredits gives each card in qs the days of its credits windows, // as kept: Daily the first's, MoreDaily the rest's. func withDailyCredits(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota { @@ -395,6 +430,13 @@ func withDailyCredits(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota out := slices.Clone(qs) for i, q := range out { var all []*DailyCredits + var ws []creditWindow + for _, w := range q.Windows { + if c, ok := creditOf(w); ok { + ws = append(ws, c) + } + } + pools := poolsOfWhole(ws) for _, w := range q.Windows { c, ok := creditOf(w) if !ok || slices.ContainsFunc(all, func(d *DailyCredits) bool { return d.Window == c.name }) { @@ -416,7 +458,7 @@ 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 { + if a.Part && !pools[c.name] && 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) }) diff --git a/internal/provider/credits_daily_pools_test.go b/internal/provider/credits_daily_pools_test.go index 220d0207d..7795dc6e1 100644 --- a/internal/provider/credits_daily_pools_test.go +++ b/internal/provider/credits_daily_pools_test.go @@ -65,3 +65,59 @@ func TestDailyCreditsPoolSetAside(t *testing.T) { } } } + +// TestDailyCreditsPoolsOfWhole: WorkBuddy's card since its plugin's +// 0.2.24 sets both pools aside, Credits and Extra credits, and Total +// credits, the two together, is what stops the account +// (packages/workbuddy usage.test.mjs, CN_FREE_SUMMARY: 0 / 500, 282 / +// 2900, 282 / 3400). yapechan on Discord: credits a day read 今天 0 for +// 积分 and 额外积分 and 34.06 for 积分合计, the pools taken for the total's +// breakdown. Each pool counts its own days, and they add up to the +// whole's. +func TestDailyCreditsPoolsOfWhole(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) } + win := func(name string, used, limit float64, aside bool) string { + a := "" + if aside { + a = `,"aside":true` + } + return `{"name":"` + name + `","used":` + jsonNum(100*used/limit) + `,"display":"` + jsonNum(used) + ` / ` + jsonNum(limit) + `","amount":` + jsonNum(used) + `,"limit":` + jsonNum(limit) + `,"unit":"credits"` + a + `}` + } + wb := func(plan, extra float64) SubscriptionQuota { + return pluginWindowsCard(t, "workbuddy", "yapechan", `[`+win("Credits", plan, 500, true)+`,`+win("Extra credits", extra, 2900, true)+`,`+win("Total credits", plan+extra, 3400, false)+`]`) + } + read := func(at time.Time, qs ...SubscriptionQuota) { noteDailyCredits(qs, at) } + read(day(7, 8), wb(0, 281.9999965)) + // the extra credits move alone, then the plan's alone, then both + read(day(7, 9), wb(0, 300)) + read(day(7, 10), wb(16.06, 300)) + read(day(7, 11), wb(20, 310)) + + got := withDailyCredits([]SubscriptionQuota{wb(20, 310)}, day(7, 12))[0] + all := append([]*DailyCredits{got.Daily}, got.MoreDaily...) + sums := map[string]float64{} + for _, d := range all { + for _, x := range d.Days { + sums[d.Window] += x.Used + } + } + if len(all) != 3 || sums["Credits"] != 20 || sums["Extra credits"] != 28 || sums["Total credits"] != 48 { + b, _ := json.Marshal(all) + t.Fatalf("days: %s, want Credits 20 + Extra credits 28 = Total credits 48", b) + } + + // a CodeArts-like bucket that is only part of its whole stays its + // breakdown (TestDailyCreditsEveryVendor), even beside a second one + part := func(total, a, b float64) SubscriptionQuota { + return pluginWindowsCard(t, "codearts", "hw", `[`+win("总积分", total, 30000, false)+`,`+win("基础积分", a, 10000, true)+`,`+win("赠送积分", b, 5000, true)+`]`) + } + read(day(7, 8), part(100, 100, 0)) + read(day(7, 9), part(150, 150, 0)) + got = withDailyCredits([]SubscriptionQuota{part(150, 150, 0)}, day(7, 12))[0] + if got.Daily == nil || got.Daily.Window != "总积分" || len(got.MoreDaily) != 0 { + b, _ := json.Marshal(append([]*DailyCredits{got.Daily}, got.MoreDaily...)) + t.Fatalf("a partial breakdown's days: %s, want 总积分 alone", b) + } +}