Files
magpie/internal/gui/assets/autoresetown.js
T
yetone 995f84a0f9 codex: how early an expiring reset is used is a setting
A Codex reset card that would run out unused was spent 30 minutes before
it expired, a fixed resetExpiryLead. Now the lead is codexResetLead in
settings.json (minutes, 15 to 1440). Unset, out of range or unreadable
reads as 30m, never 0, and magpie never writes it unless the user does.

- settings: CodexResetLead and Settings.ResetLead(); the menu's values
  are 15m, 30m (default), 1h, 2h, 3h, 6h, 12h, 24h.
- provider: the spend (expiringResetSpent) and routing
  (Allowance.Restarts, through nextResetLook) read the same resetLead(),
  so they still agree (#717, #718). The "near" look cadence follows it:
  lead + 30m, at least 1h, so a look lands inside a long lead.
- GUI: Settings > Usage, under the Codex auto-use rows, a row "Codex:
  how early before one expires" with an app menu (no <select>), and the
  sub text says spending earlier loses what the windows have left. The
  four "before it expires" strings say the chosen lead. POST
  /api/settings/codex-reset-lead; the general save keeps the field.
  en, zh, zh-TW, ja, de.
- CLI: magpie quota auto-reset lead [30m|3h|<minutes>|default].
- app.js budget: the auto-use rows moved out to lazy autoresetown.js
  (painted in the same draw once loaded), so app.js and the core script
  stay under their budgets with no raise. The button sizes to its widest
  label (a grid of every label), so picking one never rewraps the row.

Run (all on mote through mote-run):
- go vet: darwin, GOOS=linux -tags nogui, GOOS=windows; nogui build.
- go test -tags nogui -timeout 45m ./... at 3955adccf (this change
  before the paint and sizer fixes, which touch only GUI assets): 81 ok.
  go test ./internal/gui at this head: ok.
- GUI, Chromium: codex-reset-lead, codex-auto-reset,
  codex-auto-reset-settings, segs-press-redraw, i18n-keys: 62/62.
  WebKit: every case of those files failed at browserType.launch
  (180s timeout) before a page loaded, both runs; mote's browser
  launches hang under #4125 (load ~26), so WebKit is not run.
- Rebased onto f7f2d75f2 (old-webkit's lazy list now has traywindows.js
  too): go build ./..., go vet -tags nogui ./internal/..., go test
  internal/gui (budgets included), cli, settings, gateway: ok. provider:
  ok but TestQuotaSpanCarriesEveryWindowField, red on main since
  7d4a63148 (#4457, fixed in ccbefe2a2). GUI old-webkit, i18n-keys,
  codex-reset-lead in Chromium: pass.
- Rebased again onto 62a1dcdd7 (clean): go test internal/gui (budgets)
  and internal/provider: ok; i18n-keys and Chromium codex-reset-lead
  12/12 pass; old-webkit 9/10, its Safari 15.0 case a WebKit launch
  timeout (#4125).

Broken by hand, each went red:
- resetLead() pinned to 30m: all 3 provider tests.
- resetExpiryNear pinned to 1h: the long-lead look test.
- ResetLead's range check turned into m == 0: TestResetLeadUnreadIsDefault.
- GUI resetLeadMinutes returning 30: codex-reset-lead in every language.
- The button without the sizer: de at 420px, "no click moved the page".

Not tried: a real Codex account spending a card at a non-default lead;
Windows and Linux runs (vet only). The whole GUI suite in Chromium on
mote has ~45 tests in 31 files crash with "browser has been closed" on
untouched origin/main c84c3e1c0 too (reducedMotion files, #4458).
2026-10-31 00:41:29 +08:00

103 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Settings › Usage's Codex auto-use rows: each ChatGPT account's switches
// and, under them, how early a reset about to expire is used. app.js
// imports this when Settings draws them (renderAutoResetOwn), so it weighs
// nothing on the core.
// renderAutoResetOwn lists each ChatGPT account under Settings › Usage's
// "Codex: auto-use resets", with its switch, the same say as on the
// account's card (settings' codexAutoReset, the CLI's quota auto-reset).
// Avalon on Discord, after foobra12 asked where it was: it was only on the
// accounts' cards, and they found it from the menu bar's panel alone,
// nothing for it in the window's Settings. The row shows while a Codex
// account is signed in, or one is still on.
window.autoResetOwnPaint = function (s) {
const head = $("#codexAutoResetRow");
const list = head.parentElement;
list.querySelectorAll(".ar-own").forEach((r) => r.remove());
const on = s.codexAutoReset || [];
const users = [...(s.codexUsers || [])];
for (const u of on) if (!users.some((x) => x.toLowerCase() === u)) users.push(u);
head.hidden = !users.length;
let after = head;
for (const user of users) {
const r = el("div", "row pref ar-own");
r.dataset.user = user;
const who = el("div", "who");
const name = el("div", "name", user);
name.title = user;
who.append(name);
// each of the two times its own Off/On (#2078)
const was = on.includes(user.toLowerCase()) ? autoResetWhenOf(s.codexAutoResetOnly?.[user.toLowerCase()]) : { week: false, expiring: false };
const cases = el("div", "ar-own-cases");
for (const c of AUTO_RESET_CASES) {
const line = el("div", "ar-own-case");
line.dataset.case = c.id;
const say = el("span", "ar-own-say", t(c.say));
say.title = t(was[c.id] ? c.on : c.off, resetLeadVars());
const pick = segs([["off", t("Off")], ["on", t("On")]], was[c.id] ? "on" : "off", async (v) => {
try {
prefs = await writingPrefs(api("settings/codex-auto-reset", { user, on: v === "on", when: c.id }));
if (state) state.settings = prefs;
const now = (prefs.codexAutoReset || []).includes(user.toLowerCase()) ? autoResetWhenOf(prefs.codexAutoResetOnly?.[user.toLowerCase()]) : { week: false, expiring: false };
status(autoResetStatus(user, now), "ok");
renderSettings();
if (quotas) renderQuotas(); // the account's card says the same
} catch (e) {
statusTErr(e);
renderSettings();
}
});
pick.setAttribute("aria-label", t("Auto-use resets") + " · " + t(c.say) + " · " + user);
line.append(say, pick);
cases.append(line);
}
r.append(who, cases);
after.after(r);
after = r;
}
renderResetLead(s, head.hidden);
};
// renderResetLead: how long before a reset about to run out unused it is
// used, under the accounts' switches, one for them all; a pick from the
// app's menu (settings/codex-reset-lead). A value set outside the choices
// (the CLI takes any minutes in range) is shown and kept.
function renderResetLead(s, hidden) {
const row = $("#codexResetLeadRow");
if (!row) return;
row.hidden = hidden;
const cur = resetLeadMinutes(s);
const leads = RESET_LEADS.includes(cur) ? RESET_LEADS : [...RESET_LEADS, cur].sort((a, b) => a - b);
const name = (m) => t("{lead} before", { lead: resetLeadWords(m) });
const b = el("button", "sess-pick reset-lead");
b.type = "button";
b.setAttribute("aria-haspopup", "menu");
b.setAttribute("aria-label", t("Codex: how early before one expires") + ": " + name(cur));
// every choice in one cell, the current one seen: the button is as wide
// as the widest, so a pick doesn't rewrap the row and move the page
const say = el("span", "rl-say");
for (const m of leads) {
const w = el("span", m === cur ? "on" : "", name(m));
if (m !== cur) w.setAttribute("aria-hidden", "true");
say.append(w);
}
b.append(say, svg(CHEV, 11, 1.6));
b.onclick = (e) => {
e.stopPropagation();
if (b.classList.contains("open")) return closeProtoMenu();
openProtoMenu(b, leads.map((m) => ({ v: String(m), name: name(m), note: m === 30 ? "Default" : "", literalName: true })), String(cur), async (v) => {
if (Number(v) === cur) return;
try {
prefs = await writingPrefs(api("settings/codex-reset-lead", { minutes: Number(v) }));
if (state) state.settings = prefs;
renderSettings();
if (quotas) renderQuotas(); // the cards' titles say it too
} catch (err) {
statusTErr(err);
renderSettings();
}
}, "Used before it expires", "sess-menu", "right");
};
$("#codexResetLeadPick").replaceChildren(b);
}