[feat] 设置里的 GitHub 令牌失效时,插件更新检查应回退到匿名额度,而不是整轮失败
现象
在 Settings → Network and sharing 里填了 GitHub 令牌,令牌过期(或被撤销)之后,插件页「检查更新」里所有 GitHub 来源的插件一起变成「无法检查」:
已检查的插件都没有更新 · 3 个无法检查:没能向 github:MYCF711/magpie-officeace 查询最新提交:GitHub refused the GitHub token in Settings → Network and sharing: it may have expired or been revoked
结果是填了令牌反而比不填更糟:
| 状态 | 结果 |
|---|---|
| 不填令牌 | 匿名额度 60 次/小时,检查正常工作(实测 3 个 GitHub 源的仓库全部返回 200) |
| 填了但已过期 | 一个都查不了,整轮检查失败 |
现在这样是有意的
internal/ghtoken/ghtoken.go 的包注释写得很清楚:
A token from the environment that GitHub refuses (401) is one the user may not know is there, often stale: the request is asked again without it, and the token isn't sent again while magpie runs (till on Discord). The token set in Settings is the user's own choice, so a refusal of it is said (Refused).
Do() 里照此实现:
if err != nil || t == "" || from == "settings" || resp.StatusCode != http.StatusUnauthorized {
return resp, t != "" && err == nil, err
}
from == "settings" 时不重发,直接把 401 交出去,最后由 Refused() 变成界面上那句话。
这个取舍我理解:环境变量里的令牌用户可能根本不知道它在,所以要静默丢弃;Settings 里的是用户自己填的,坏了就该说出来。
但代价是:GitHub 的 PAT 有有效期,过期是必然事件,不是意外。一旦过期,用户除非主动点「检查更新」,否则不会知道;而这段时间里插件更新检查整体停摆,比他从没填过令牌还糟糕。
建议
两者兼得:回退到匿名额度,同时继续明确告知令牌已失效。
-
ghtoken.Do():from == "settings"且收到 401 时,也重发一次不带Authorization的请求(和环境变量那条完全同构),把匿名请求的应答交回给调用方。 -
让调用方知道这一轮是「降级」而不是「正常」:
Limited{Token: true}已经有先例,可以再加一个同类标志(Refused/Token),VersionCheck上对应加字段。 -
插件页把提示改成「已降级 + 怎么修」,例如:
GitHub 令牌已失效,本次用匿名额度(60 次/小时)完成检查。去 Settings → Network and sharing 换一个令牌。
这样用户仍然知道令牌坏了(不比现在差),但检查不会整个停摆(比现在好),而且给了明确的修复入口。
临时变通
- 清空 Settings 里的令牌即可恢复:3 个 GitHub 源 × 每 6 小时检查一次 ≈ 12 次/天,匿名 60 次/小时完全够。
- 或者把令牌放进
GITHUB_TOKEN环境变量 —— 这条路径已经会自动丢弃被拒的令牌并重发匿名请求,等于变相实现了我上面建议的行为。
环境
- magpie 0.1.1756,Windows 11
- 相关:#1942(令牌未被插件更新检查使用 / 限流提示),本 issue 是它在「令牌被拒」这一分支上的续篇
Implemented on a branch — I could not open the PR, so it is here.
- Repo/branch:
MYCF711/magpie→fix/settings-token-fallback - Commit
7d93f1b5, based onf8cd964a(= v0.1.1767) - Fetch:
git fetch https://github.com/MYCF711/magpie.git fix/settings-token-fallback
Why there is no PR. POST /repos/yetone/magpie/pulls answers 404, and the GraphQL createPullRequest answers FORBIDDEN — "MYCF711 does not have the correct permissions to execute CreatePullRequest". If pull requests to this repo are restricted, pull the branch and I will follow the review here.
What the user sees. A PAT in Settings that GitHub refuses no longer fails the whole check: the token is dropped for the rest of the run and the request is asked again without it, as an environment token's refusal already did. The Plugins page says, beside the list it did produce, that the token in Settings → Network and sharing needs replacing — and a token typed there afterwards is sent normally.
What changed (+135/−32, six files):
internal/ghtoken: the 401 retry no longer stops atfrom == "settings";SettingsRefused()keeps which token was refused, andRefused()says it even though the token is no longer sent.internal/gui/plugins.go:/api/plugins/githubcarriestokenRefused.internal/gui/assets/plugins.jsandi18n.js: the GitHub section says so, in zh, zh-TW, ja and de.
Run at 7d93f1b5, Windows amd64, Go 1.26.8: gofmt -l internal/ nothing; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... clean; go build ./..., GOOS=linux go build -tags nogui ./..., GOOS=windows go build ./... clean; go test ./internal/ghtoken/ ./internal/plugin/ ok.
A test that fails without the change: TestSettingsTokenRefusedAskedAgainWithout. With from == "settings" put back in Do, it fails with
ghtoken_test.go:131: got 401 token=true {"message":"Bad credentials"}
and passes with the change. TestGitPluginCheckCarriesGitHubToken's refusal case now expects the check to finish on the anonymous answer, and asserts both requests.
Not run: the Playwright suite under internal/gui/tests. Five tests in internal/gui fail the same on this checkout of origin/main (TestGatewayHeldByAContainer, TestLodyImportHandoff, TestImportStatusUnreadableIsUnknown, TestAionUiImportRow, TestUpdateThatWontStart), identically with the change; TestBuiltinsTranslated and TestCoreScriptBudget pass.
This reverses a stated decision — the package comment said a refused Settings token is said and not retried, and TestSettingsTokenRefusedStaysAnError held it. Putting the old behaviour back is one line in Do if you would rather keep it.
Taken, in v0.1.1774 (dc1af8231). Patch by @MYCF711, credited in the commit body.
The decision: a token GitHub has refused is worth nothing, so failing every check on it only made a stale token worse than none. Your fallback is in as written. A Settings token that gets a 401 is dropped for the run, or until the field changes, and the request is asked again anonymously (60 an hour). TestSettingsTokenRefusedStaysAnError and the package comment are overturned.
What changed from the branch:
- Where it is said. Settings › Network and sharing keeps showing the token, masked, with Remove. Its line now says in red that GitHub refused it and requests go without one until it is replaced (
githubTokenRefusedon /api/settings). Before, a refused token would have read as no token set. I left out the Plugins page note: it cost ~630 bytes of core script, and the budget had 28 left. Settings' row also covers the Library's skill checks. - No fall-through to the environment. After the Settings token is refused, requests go anonymous and do not switch to GITHUB_TOKEN/GH_TOKEN, so the page's "without one" is true. TestSettingsTokenRefusedGoesWithoutEnvToken holds this.
- The sibling. internal/library's TestGitHubRateLimitSaid was left red by the patch. It now expects the anonymous answer.
Tests: github-token-refused.test.cjs is red 8 of 8 on the old assets and green in Chromium and WebKit, en and zh, at 1000 and 440px. The ghtoken, plugin, library and gui packages are ok. Not tried with a real revoked token. Leaving this open for you to confirm.
Confirmed — v0.1.1774 (dc1af8231) does what you wrote, and I ran the part you left open: a token in Settings that api.github.com answers 401 to.
At that tree, against the real API, not a fake:
internal/ghtoken—DoonGET /repos/yetone/magpiewith aghp_token GitHub refuses: asked again without it, the answer taken is the anonymous 200 (carried=false),Token()is("",""),SettingsRefused()reports the refusal, and the next request goes without it at once.internal/library— the same throughdoGitHub, the door every skill check's API request goes through: 200,carried=false,SettingsRefused()true. The check finishes on the anonymous answer instead of failing whole, which is whatTestGitHubRateLimitSaidnow holds.internal/gui— after that refusal/api/settingsanswers"githubTokenMask":"ghp_…CCCC","githubTokenFrom":"settings","githubTokenRefused":true: still shown, from Settings, marked refused, never the token itself. That is whatrenderGitHubTokenturns into the red line.
One caveat on "real": I had no previously-valid token left to revoke, so the token was ghp_-shaped and GitHub answers it 401 {"message":"Bad credentials"} — the same status, the same body, and so the same branch in Do that a revoked or an expired one takes. A token that really was valid and then revoked needs a second account to produce; the 401 path itself is covered.
Both of your changes from the branch are better than what I had:
- The notice in Settings' row, not on the Plugins page. The token lives there, and 630 bytes of core script against 28 left is not a trade worth making for a second copy of the same sentence. Settings' row covering the Library's skill checks too is what I should have done in the first place.
Token()not falling through toGITHUB_TOKEN/GH_TOKENafter the Settings refusal. That is the honest reading: the user put that one ahead of the environment's, so "without one" on the page stays true.TestSettingsTokenRefusedGoesWithoutEnvTokenpins it, and it is the case I would have got wrong by hand.
Nothing left from my side. Closing this.