Files
magpie/internal/gui/assets/plugins/gateway
yetone 98e23350f1 gateway: a key's limit lines are read again while the page is open
Young (Discord #feedback, v0.1.18xx) reset his gateway keys' usage
(three keys held to 2-3 models, $30 per 5 hours, $153 per 7 days and a
token cap per 30 days) and their lines read "$0.00 / $30.00" from then
on, while Usage's gateway-key section showed the same keys spending.

The cause is in the GUI: the Gateway page read /api/caller-keys, where
each key's used limits come from, only on a visit to the page (the
`open` hook). Coming back to the window and Refresh re-read providers
(`render`) but not the keys, and nothing ticked, so a page left open
after Reset usage kept the $0.00 it drew then. The app reopens on its
last page, so it could read $0 for days.

Now refreshGatewayKeys reads the keys again from `render` (the window
back, Refresh) and every 15s while the Gateway page is in sight and a
key has a limit, as Usage's own timer does (#3185). The list is drawn
again only when the keys changed, and never over a key being added,
renamed or its limit edited, nor while an action is on its way.

The gateway side was checked first and counts right: a probe test with
Young's key shape (model scope incl. a group, $ and token limits),
reset, the budget cache dropped as a restart does, and requests on
chat/completions, messages and responses, streamed and not, counted
every call into each limit's window. The probe is not committed.

Test: internal/gui/tests/gateway-key-used-live.test.cjs, Young's key
shape, chromium and webkit, en and zh: the line goes from $0.00 to
$9.93 on window focus, to $12.50 on the timer, and an open limit editor
keeps its typed value and focus through both.

Run on mote:
- the new file: 4 of 4 ok.
- every gateway-*, connect-* GUI file and i18n-keys, both engines
  (262 tests): 259 ok. The 3 reds are the same on the base without
  this change and filed: gateway-caller-keys webkit en and
  gateway-key-models chromium/webkit en at 560px (#3797), i18n-keys
  "Runs as" (#3897).
- go test -tags nogui -timeout 45m ./...: 76 packages ok, exit 0, at 5ed6669ce plus this change.
- after rebasing on 6f0e40572 (two commits, neither in the gateway
  page): go build ./..., the new file with gateway-key-limits and
  i18n-keys (28 of 28 ok), and internal/gui's Go tests 3 times at
  -count=1: 2 ok, 1 red in TestRestartHandsTheGatewayOver (a request
  reset during the handover, #3451, not from this change).

Broken by hand, each red in the new test:
- render not calling refreshGatewayKeys: red at the focus step.
- the timer at 15e6 ms: red at the timer step.
- refreshGatewayKeys drawing over an open editor: red (focus lost).

Not covered: Young also says Usage's per-key spend stays still while
the Overview's gateway tokens move. Usage reads its own data on its own
timer, so this change doesn't touch that. The Overview counts every
gateway call, keyed or not, while a key's row and limits count only
calls that carried that enabled key. A loopback call (an agent here, or
a TCP forward like frp tcp or ssh -R to the gateway's port) with a
stale or rotated key is still answered, by design, but is counted under
no key. Whether that is Young's case is not known yet.

Not tried: the real app with a real key over hours.
2026-10-28 12:17:18 +08:00
..