provider: a cap set on a window a plugin later sets aside still holds; WorkBuddy min 0.2.24 (ferock on Discord)

WorkBuddy 0.2.24 stops an account by a new "Total credits" window (the
plan's credits and the extra ones together) and sets "Credits" and
"Extra credits" aside, so the Routing page reads every account the same,
its plan's credits spent or not. Routing skipped every window set aside,
caps included, so a cap the user had set on "Credits" would have stopped
holding the account: magpie's change overriding the user's value.

Now a window set aside, not a pool's or one model's, rides in each
limit's pool (allowanceOf), as a pool's windows do, and holds the
account at a cap set on that window itself (WindowCaps.own), never at the
account's cap or a Codex credits hold, which never reached it before.
WithCapped marks such a window Capped with OwnCap, so the GUI shows the
mark and the "at its cap" note for it, counts no account cap on it
(windowCapOf), and offers its meter as a button only while it has a cap
of its own, to change or lift it; no new cap on a window set aside.
app.js grows 48 bytes, inside the core budget (no raise).

Run (mote): go test ./internal/provider (ok) and ./internal/gui
(TestCoreScriptBudget ok); window-cap, window-cap-pool and account-cap
GUI tests, both engines, 420/1100, five languages: 136 pass.
internal/gui's TestMainWindowCorners/macOS_26_SDK (#3949) and
i18n-keys "a plugin's words are its own" (#5327, #5332) are red the same
on origin/main a9b1877cf, files this change doesn't touch.
Broken by hand: allowanceOf without the set-aside loop,
TestAsideWindowKeepsItsOwnCap red ("the user's 50% cap on Credits, at
60%, no longer held: <nil>"); app.js without the meter's ownCap guard,
window-cap 20 of 80 red (the new test in every engine, width and
language).
Not tried: a real WorkBuddy account with a cap on Credits; the whole GUI
suite (the full Go suite runs after the tag).
This commit is contained in:
yetone
2026-11-05 18:22:56 +08:00
parent a9b1877cfc
commit 51bdc985bc
8 changed files with 119 additions and 6 deletions
+2 -2
View File
@@ -14019,7 +14019,7 @@ function windowCapsOf(p, user) { return Object.keys(p?.accountWindowCaps?.[Strin
function windowCapOf(p, user, w) {
const own = windowOwnCapOf(p, user, w);
if (own) return own >= 100 ? 0 : own;
return accountCapOf(p, user);
return w?.ownCap ? 0 : accountCapOf(p, user);
}
// capHeldOf: how the account is held at cap, as the gateway reads it —
// a window at or past its cap (capFor: its own, else the account's), not
@@ -14112,7 +14112,7 @@ function pillNumber(at, val, ph, cls, label, parse, bad, own, set) {
// sets the window's own cap, a family's its models' (tiers); not on-demand's.
function windowCapMeter(m, p, user, w) {
const ids = (w.tiers || [w]).map((x) => x.capId), owns = ids.map((capId) => windowOwnCapOf(p, user, { capId }));
if (!ids.every(Boolean)) return m;
if (!ids.every(Boolean) || w.ownCap && !(owns[0] % 100)) return m;
const own = new Set(owns).size > 1 ? 0 : owns[0], cap = accountCapOf(p, user);
const name = t(w.name);
const b = spanBtn(el("span", m.className + " aq-set" + (own ? " own" : "")));
+29 -1
View File
@@ -30,7 +30,9 @@ const fresh = () => ({
],
},
// the friend's five hours stop at 50%, its week at 40%; no account cap
accountWindowCaps: { "friend@example.com": { "5 hours": 50, weekly: 40 } },
// and its plan's credits at 50%, set before a plugin set that window
// aside (ferock on Discord)
accountWindowCaps: { "friend@example.com": { "5 hours": 50, weekly: 40, credits: 50 } },
},
});
const win = (name, used, h) => ({ name, capId: name.toLowerCase(), used, resetsAt: inHours(h), capped: true });
@@ -44,6 +46,11 @@ const past = () => ({
"me@example.com": under()["me@example.com"],
"Friend@Example.com": { provider: "codex", windows: [win("5 hours", 60, 3), win("Weekly", 10, 100)] },
});
// a window set aside holds at its own cap only (ownCap): WorkBuddy's
// plan's credits and the extra ones, the account stopped by their total
const aside = (name, used) => ({ ...win(name, used, 600), ownCap: true });
const credits = (plan) => ({ provider: "codex", windows: [aside("Credits", plan), aside("Extra credits", 0), win("Total credits", plan / 4, 600)] });
const split = () => ({ "me@example.com": credits(60), "Friend@Example.com": credits(60) });
function serve(lang, posts, use) {
const ps = fresh();
@@ -170,6 +177,27 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert.deepEqual(errors, []);
});
test(`${label}: a window set aside holds the account at the cap set on it before, and only that one`, async (t) => {
const { page, errors, posts } = await open(t, "aside", split);
// the friend's Credits at 60%, past the 50% set on it: held, marked
const held = row(page, "Friend@Example.com").locator(".acap-held");
if (w.held) assert.match(await held.textContent(), /^[^·]+·/);
assert.equal(await held.count(), 1);
assert.deepEqual(await row(page, "Friend@Example.com").locator(".aq-cap").evaluateAll((ms) => ms.map((m) => m.style.left)), ["50%"]);
// me has none on it: no note, no mark, and no new cap on a window set aside
assert.equal(await row(page, "me@example.com").locator(".acap-held, .aq-cap, .aq-own").count(), 0);
assert.equal(await row(page, "me@example.com").locator('.aq-set[role="button"]').count(), 0);
// the friend's is lifted from its meter
await still(page, "Friend@Example.com", meter(page, "Friend@Example.com", 0));
await menu(page).waitFor();
const n = posts.length;
await item(page, w.account).click();
assert.deepEqual(await posted(posts, n), { id: "codex", account: "Friend@Example.com", window: "credits", cap: 0 });
await page.waitForFunction(() => !document.querySelector('.editor .acc[data-account-id="Friend@Example.com"] .acap-held'));
assert.equal(await row(page, "Friend@Example.com").locator('.aq-set[role="button"]').count(), 0);
assert.deepEqual(errors, []);
});
test(`${label}: a window's cap is set from its meter in the app's menu, typed, and lifted`, async (t) => {
const { page, errors, posts } = await open(t, "set", under);
// me's five hours: the menu, headed by the window, no native select
+20
View File
@@ -94,6 +94,16 @@ func (c WindowCaps) Of(name string) (share int, credits bool) {
return share, false
}
// own is the share at which a window set aside holds the account (Limit's
// aside): only a cap the user set on that window itself, never the
// account's or the credits', 0 for none.
func (c WindowCaps) own(name string) int {
if v := c.Windows[WindowCapID(name)]; v >= MinCap && v <= MaxCap {
return v
}
return 0
}
// Holds says whether any window can hold the account before the vendor
// does.
func (c WindowCaps) Holds() bool {
@@ -303,6 +313,9 @@ func (a Allowance) CapHeld(model string, caps WindowCaps, now time.Time) *CapHol
unknown, byCap := false, false
for _, l := range a.capLimits(model) {
share, credits := caps.Of(l.Name)
if l.aside {
share, credits = caps.own(l.Name), false
}
if share <= 0 || l.Used < float64(share) {
continue
}
@@ -344,6 +357,9 @@ func (a Allowance) NearCap(model string, caps WindowCaps, points float64, now ti
model = strings.ToLower(model)
for _, l := range a.capLimits(model) {
share, _ := caps.Of(l.Name)
if l.aside {
share = caps.own(l.Name)
}
if share <= 0 || l.Used < float64(share)-points {
continue
}
@@ -387,6 +403,10 @@ func WithCapped(m map[string]SubscriptionQuota) map[string]SubscriptionQuota {
pool := w.PoolWindow() && len(PoolMembers(q.Windows, w)) > 0
w.Capped = (!w.Aside || pool) && !w.Unlimited
w.CapsSome = w.Capped && (w.Model != "" || w.matches != nil || pool)
// set aside, it still holds at a cap the user set on it
// before (allowanceOf), and at no other
w.OwnCap = w.Aside && !w.Unlimited && !w.Text && w.Model == "" && !pool
w.Capped = w.Capped || w.OwnCap
if w.Capped {
w.CapID = WindowCapID(w.Name)
}
+44 -2
View File
@@ -94,8 +94,8 @@ func TestWithCappedMarksPerModelWindows(t *testing.T) {
if w[0].CapsSome || !w[1].CapsSome {
t.Error("the window one model's own is not told from the account's")
}
if w[2].Capped {
t.Error("on-demand spending is marked")
if w[2].Capped && !w[2].OwnCap {
t.Error("on-demand spending is marked for the account's cap, not only a cap of its own")
}
if w[3].Capped {
t.Error("an unlimited window is marked")
@@ -395,3 +395,45 @@ func TestSetWindowCaps(t *testing.T) {
t.Fatalf("cleared, still %+v", c)
}
}
// A cap the user set on a window that a plugin later sets aside still
// holds the account (ferock on Discord): WorkBuddy 0.2.24 sets "Credits"
// aside and stops the account by "Total credits", plan and extra
// together. Only the window's own cap does: the account's never reaches a
// window set aside.
func TestAsideWindowKeepsItsOwnCap(t *testing.T) {
now := time.Now()
month := now.Add(25 * 24 * time.Hour)
ws := []QuotaWindow{
{Name: "Credits", Used: 60, Amount: 300, Limit: 500, ResetsAt: &month, Aside: true},
{Name: "Extra credits", Used: 0, Amount: 0, Limit: 1300, Aside: true},
{Name: "Total credits", Used: 300.0 / 1800 * 100, Amount: 300, Limit: 1800, ResetsAt: &month},
}
a := allowanceOf(ws, now)
if used, _ := a.For("glm-5.1", now); used > 17 {
t.Fatalf("routing went by a window set aside: %v", used)
}
plan := WindowCaps{Windows: map[string]int{"credits": 50}}
if h := a.CapHeld("glm-5.1", plan, now); h == nil || h.Used != 60 || h.Cap != 50 || !h.Back.Equal(month) || h.Credits {
t.Fatalf("the user's 50%% cap on Credits, at 60%%, no longer held: %+v", h)
}
if !a.NearCap("glm-5.1", WindowCaps{Windows: map[string]int{"credits": 70}}, 15, now) {
t.Fatal("Credits at 60% is not near its own 70% cap")
}
if h := a.CapHeld("glm-5.1", WindowCaps{Windows: map[string]int{"credits": 70}}, now); h != nil {
t.Fatalf("held under its own 70%%: %+v", h)
}
if h := a.CapHeld("glm-5.1", WindowCaps{All: 50}, now); h != nil {
t.Fatalf("the account's 50%% held by a window set aside: %+v", h)
}
if h := a.CapHeld("glm-5.1", WindowCaps{Credits: true}, now); h != nil {
t.Fatalf("a Codex credits hold reached a window set aside: %+v", h)
}
got := WithCapped(map[string]SubscriptionQuota{"me@example.com": {Windows: ws}})["me@example.com"].Windows
if w := got[0]; !w.Capped || !w.OwnCap || w.CapsSome || w.CapID != "credits" {
t.Fatalf("Credits, set aside: capped %v, own %v, some %v, id %q", w.Capped, w.OwnCap, w.CapsSome, w.CapID)
}
if w := got[2]; !w.Capped || w.OwnCap || w.CapID != "total credits" {
t.Fatalf("Total credits: capped %v, own %v, id %q", w.Capped, w.OwnCap, w.CapID)
}
}
File diff suppressed because one or more lines are too long
+1
View File
@@ -61,6 +61,7 @@ func TestQuotaSpanCarriesEveryWindowField(t *testing.T) {
"capped": "the GUI's cap marks (WithCapped)",
"capsSome": "the GUI's cap marks (WithCapped)",
"capId": "the GUI's cap marks (WithCapped)",
"ownCap": "the GUI's cap marks (WithCapped)",
"startsAt": "the GUI's Pace number (WithWindowStarts)",
"trayKind": "the GUI's menu bar picker (gui.withTrayKinds)",
"trayExtra": "the GUI's menu bar picker (gui.withTrayKinds)",
+18
View File
@@ -311,6 +311,11 @@ type Limit struct {
// (Antigravity's "Gemini · 7 days"), which hold the account at its
// caps only (capLimits): routing goes on by the model's own window
pool []Limit
// aside is set on a window set aside for routing (QuotaWindow.Aside)
// that a cap the user set on it while it counted still holds the
// account at (WindowCaps.own); it rides in pool, never in the
// allowance itself
aside bool
// ResetRunsOut is when the reset the account spends by itself before
// it runs out does (resetRunsOut): spent then, it starts this window
// again — at Restarts, which routing takes for the window's renewal
@@ -1156,6 +1161,19 @@ func allowanceOf(ws []QuotaWindow, now time.Time) Allowance {
}
}
}
// a window set aside since the user capped it (WorkBuddy's "Credits"
// once the plan's and the extra credits are counted together, ferock
// on Discord) holds at that cap still: the user's value outlives the
// plugin's change of which window stops the account
for _, w := range ws {
if !w.Aside || w.Text || w.Model != "" || w.PoolWindow() {
continue
}
al := Limit{Used: w.Used, Span: w.Span, Name: w.Name, Resets: resets(w), aside: true}
for i := range a {
a[i].pool = append(a[i].pool, al)
}
}
return a
}
+4
View File
@@ -97,6 +97,10 @@ type QuotaWindow struct {
// CapID is, beside Capped, how the window is known to a cap of its
// own (WindowCapID), for the GUI to set and show one.
CapID string `json:"capId,omitempty"`
// OwnCap is set beside Capped on a window set aside: only a cap the
// user set on it itself holds the account there (WindowCaps.own),
// never the account's, and the GUI offers none new.
OwnCap bool `json:"ownCap,omitempty"`
// Holds is what the whole window holds, reckoned from magpie's own
// calls through the account (usage.WithWindowHolds), for the GUI and,
// through ReckonHolds, magpie quota --json and /v1/magpie/quotas