magpie icon indicating copy to clipboard operation
magpie copied to clipboard

A usage window with no `used` reads as 0% — which also puts a spent account first in the routing order

Open MYCF711 opened this issue 1 month ago • 3 comments

What happens

A provider plugin that leaves out used on its usage windows makes a spent account read as full, and — because used is what the routing order sorts by — it puts that account at the front of the route. On my machine a Loomy account with 0 credits was picked 255 out of 256 times over three days, and 11 of those requests failed with the vendor's "insufficient credits".

Two separate things combine here. Both are in the plugin contract today; the second one is the one I'd like to see changed.

1. used is missing ⇒ silently treated as 0

usageOf() normalises with:

used: Math.max(0, num(w?.used)),   // past 100 when overspent

Nothing distinguishes "the plugin says 0% used" from "the plugin didn't say". So a plugin that only sends display/amount (which is what the two Loomy plugins on npm do — one of them literally cannot get the ceiling, see below) gets used: 0 on every window.

The Settings meter has a "how much is left" toggle, and it reads:

function quotaFill(w){ const used = Math.round(to100(w.used)); return quotaLeft ? 100 - used : used }

⇒ the card shows "0 积分 · 剩余 100%" — the number from display, the percentage from a field that was never sent.

Suggestion: treat an absent used as unknown rather than 0 — e.g. carry used: null through, and have the meter show an indeterminate bar / the display text only. At minimum, don't let an absent used mean "full".

2. used (so an absent one) is a routing input

const group = (w) => w.used >= 98 ? "spent" : w.used >= 90 ? "low" : "fine"

and the smart-routing explanation for spent is "all but used up, it answers only when nothing else can".

So an absent used doesn't merely misdraw a bar — it reorders the route. That's a costly failure mode for exactly the accounts people care about (a plan that ran out).

Suggestion: a window that didn't state used should not participate in the spent/low ranking (like aside windows already don't), instead of ranking as fine.

3. (minor) check-in outcome outside the enum is rewritten to failed, and its message is kept

const checkinOutcomes = ["claimed", "done", "ineligible", "inactive", "captcha", "failed"]
if (!checkinOutcomes.includes(r.outcome)) {
  return { outcome: "failed", credit: 0, streak: 0,
           message: message || `the plugin answered no outcome (${JSON.stringify(r.outcome ?? null)})` }
}

One of the Loomy plugins on npm returns { outcome: "success", message: "每日赠送积分已刷新" }. Not in the enum ⇒ rewritten to failed, with its message kept, so the UI reads "签到失败 · 每日赠送积分已刷新" — "check-in failed · credits refreshed". The check-in actually succeeds every day.

Suggestion: when the outcome is unrecognised, keep the plugin's message but prefix it, so the two halves can't be read as a contradiction (or surface the raw value in the message when one is present, not only when it isn't).

Reproduction (no plugin needed)

usage() returning { windows: [{ name: "x", display: "0 / 5000" }] } is enough to see (1) and (2): the card shows "100% left" and the account sorts before accounts reporting real percentages.

Context

I wrote a Loomy plugin that reports the two pools properly (the daily pool counted, the permanent pool aside) and the measured result here was: the spent account moved from 3/13 → 13/13 in the routing order, was rested by window until the next Beijing day (rest: {by: "window", until: "2026-10-27T00:05:00+08:00"}), and the never-picked-again count went from 255 to 0. So the mechanism works exactly as designed once used is actually sent — the request here is only about the case where a plugin doesn't send it.

Happy to send a PR for any of these if you tell me which shape you prefer (for (1): used: null vs. dropping the window vs. an explicit unknown: true).

Verified against magpie 0.1.1254 (Windows amd64 build, 2026-10-25).

MYCF711 avatar Oct 25 '26 20:10 MYCF711

Both halves come from the same coercion: host.js's usageOf did used: Math.max(0, num(w?.used)), so absent became 0; Go's UsageWindow.Used float64 could not carry absence either; that 0 became a QuotaWindow with a used of 0, which the meter read as "100% left" (and as NaN% where the page had no used at all) and which allowanceOf put at the front of the smart-routing order - an account that is spent ranking as the one with the most left.

Fixed on fix/usage-absent-used (6791aa99, on 01468003):

  • host.js passes an absent used through as null; plugin.UsageWindow.Used is a *float64, so absent and 0 are different values end to end.
  • quotaOfPlugin marks such a window Text (its display only) and Aside (no share in the allowance), and allowanceOf skips both - a window that said nothing no longer ranks as "fine". An account whose windows all say nothing has no Allowance, so routing leaves it unknown instead of preferring it.
  • app.js gained quotaNoShare: no used (or a text-only window) draws its display text or "No allowance reported", never a meter, never NaN, and takes no part in the pace.
  • the check-in's unrecognised outcome keeps the plugin's message but names the plugin's own word first: the plugin answered "yes": 每日赠送积分已刷新, instead of "check-in failed · credits refreshed".

RED -> GREEN, re-run independently here:

  • clean base with only the tests: the browser test fails on Chromium and WebKit with 0 / 5000 · NaN% used and a meter drawn; the Go regression TestAWindowWithoutUsedIsNoAllowance fails with both windows in the allowance, Credits (never said used) beside Daily (said 0).
  • at the head: the browser test passes 2/2, the Go tests pass, and a window that said used: 0 is still an allowance while one that said nothing is not.
  • go test ./internal/provider and ./internal/plugin pass with two known failures skipped: TestUnreadLoginsWriteErrorsKeepCredentials (fatal close of synctest channel from outside bubble) and TestPluginSOCKSProxy, both red on the clean base too. go vet and go build for linux, windows and -tags nogui pass; gofmt -l is empty.

Why two commits on the branch: the first test builds Used: fptr(0), which the base's float64 field cannot express, so the second commit adds a regression whose own code avoids fptr - a plugin reply decoded as the host sends it. That is the one shown failing above, and it reproduces the failure when added to the clean base on its own; the branch's test file as a whole does not compile there, since it shares a file with the fptr test.

Limits: the routing outcome is asserted at quotaOfPlugin/allowanceOf, not through a live request to a real vendor; no real plugin on this machine reports a window without used; the check-in prefix is tested through the host's fake provider. Playwright is not vendored in this checkout - the browser test ran on this machine's 1.64.1 (Chromium and WebKit) via NODE_PATH. Nothing in internal/gui's Go package tests was run.

skygunner avatar Oct 25 '26 21:10 skygunner

Fixed in v0.1.1608 (9733ed1b4). Thanks to skygunner for the fix: it was ported from the fix/usage-absent-used branch, with credit in the commit.

A plugin usage window that leaves out used is no longer read as 0:

  • The card shows the plugin's own text, or "No allowance reported", with no percentage and no meter. It no longer says "100% left" or NaN%.
  • Smart routing treats such an account as not known yet, so it no longer puts spent accounts first. This holds for a fresh reading and for the one magpie kept on disk.
  • A window that does say used: 0 still reads as 0%.

The branch's check-in wording change went in its own commit (51202cd43).

I'm keeping this open until you confirm it works for your accounts.

yetone avatar Oct 26 '26 14:10 yetone

Confirming late. You asked for a confirmation on my accounts and I closed this on 2026-10-27 without writing one — that was my mistake, so here is the sentence.

Version. magpie 0.1.1756, Windows amd64. That is past 9733ed1b / v0.1.1608, and I checked the commit's diff against the tree I run, so what I am running is the code you shipped: plugin.UsageWindow.Used is a *float64, quotaOfPlugin marks a window with no used as Text + Aside, and routedAllowance keeps such an account out of the ranking rather than ranking it as "all left".

What I cannot give you. The account-side red→green. My reproducer was a Loomy account whose plugin sent only display/amount; in the install that runs today that plugin reports used on both pools, so the "no used" state does not occur here any more and I have no card reading to show you. At close time I had also not recorded the before/after reading.

So the close rests on the fix being in the build I run, not on a fresh measurement of my own. If the shape turns up again in one of my plugins I will reopen with the log rather than another silent close.

MYCF711 avatar Oct 27 '26 17:10 MYCF711