Commit Graph
4 Commits
Author SHA1 Message Date
yetone e4ccf67b11 plugin: GitHub's search gets 60s a page, and a mirror is asked without the token (#3923)
MYCF711 (#3923) measured a page of GitHub's search from mainland China:
29-36s straight to api.github.com, 3-7s through gh-proxy. The Plugins
page's GitHub list was empty there, for two reasons:

- askTagged's taggedWait (8s) bounded the search pages as well as the
  wait for the repositories' reads. The first page timed out, the ask
  failed whole, and nothing was cached. Each search page now has its own
  taggedSearch (60s). taggedWait starts after the search and bounds only
  the reads, as before. Through a mirror, candidateContext gives the
  official address half of the 60s and the mirror the rest.
- source.Do never used a mirror for a request carrying Authorization,
  and ghtoken sets it on every api.github.com request when a GitHub
  token is set. With a token in Settings, MAGPIE_MIRRORS did nothing.
  A request to a GitHub host whose only credential is Authorization
  (bare) now goes to the official address with the token and to the
  mirror without it. A cookie, a key header, a token in the query, URL
  userinfo, or any other host still keeps the request official. The
  caller's request keeps its header.
- ghtoken.Do now reads who answered from resp.Request: a mirror's answer
  to the bare request doesn't count as carrying the token, and a 401 from
  a mirror doesn't mark the token refused.

Tests:
- TestTaggedReposSlowSearch: a search answering after 3x taggedWait
  still lists; one that never answers fails at taggedSearch.
- TestDoKeepsCredentialsOfficial, rewritten to the new contract: the
  official address gets the token, the mirror gets none, and the
  caller's request is unchanged. Cookie, query token and npm still stay
  official.
- TestMirrorAnswerIsNotTheToken (ghtoken).

Run on mote:
- go test -tags nogui -count=1 ./internal/source ./internal/ghtoken
  ./internal/plugin ./internal/library: all ok.
- Broken by hand: with the old source.go, ghtoken.go and github.go, all
  three tests went red ("requests = [https://api.github.com/repos/o/r]";
  "token=true"; "context deadline exceeded" at taggedWait).
- Locally: gofmt -l clean; go vet for linux and windows on the three
  packages; go build -tags nogui ./...

Not tried:
- The real network from mainland China.
- A live gh-proxy answer to an anonymous api.github.com search.
- Not changed: the mirror for api.github.com still waits for
  MAGPIE_MIRRORS. Without it, the 60s alone covers the ~30s page.
- No GUI file was run: the page's fixtures fake /api/plugins/github, and
  nothing in the window changed.

Reported by MYCF711.
2026-10-28 03:57:51 +08:00
yetone dc1af8231b ghtoken: a Settings token GitHub refuses is said, and requests go without one (#3926)
A GitHub token in Settings → Network and sharing that GitHub refused
(401, expired or revoked) failed every plugin and skill check, so a
stale token was worse than none. Now it is treated as an environment
token already was: the request is asked again without it, the token
isn't sent again while magpie runs or until the field changes, and the
anonymous answer (60 an hour) is the one taken. This overturns the
package comment and TestSettingsTokenRefusedStaysAnError: the token
being the user's choice is a reason to tell them, not to fail the
check.

The page stays honest about it:
- Settings › Network and sharing still shows the token, masked, with
  Remove, and says in red that GitHub refused it and requests go
  without one until it is replaced (/api/settings githubTokenRefused).
  Before, a refused token simply read as none being set.
- After a Settings token's refusal requests go anonymous, not with
  GITHUB_TOKEN/GH_TOKEN: the user put the Settings one ahead of those,
  and the page says "without one".

Patch by MYCF711 (fix/settings-token-fallback, 7d93f1b5): the ghtoken
change and its tests. Changed from it: the notice is in Settings' row,
where the token lives, instead of the patch's Plugins page note,
which cost ~630 bytes of core script the budget (28 bytes of room at
c54478904) doesn't have; the Library's skill checks reach Settings'
row too. Token() doesn't fall through to the environment; the
library's skill check test (the sibling the patch left red) expects
the anonymous answer; the unreachable new Refused() message is
dropped (Refused's "where" now holds for a refused Settings token).
app.js stays in its budget by shortening renderGitHubToken's comment.

Run at this tree (base c54478904), on mote:
- go test -tags nogui ./internal/ghtoken ./internal/plugin
  ./internal/library ./internal/gui: ok (TestCoreScriptBudget
  included; no budget raised). GOOS=linux and windows go vet, go build
  ./...: clean.
- github-token-refused.test.cjs (new; chromium and webkit, en and zh,
  1000 and 440px): red 8 of 8 against origin/main's assets, green
  with the change. With github-token and i18n-keys: 21 of 22; the red
  is i18n-keys "a plugin's words are its own" ("Runs as"), red at
  origin/main 74dcf40aa alone too (#3897).
- Every GUI file that opens Settings › Network and sharing
  (allow-ips, api-key-theme, bare-ids, browser-access, cors-origins,
  gateway-port, lan-docker, proxy-test, settings-proxy-auth,
  settings-proxy-remember): 126 of 126.
- Broken by hand: Token() falling through to GITHUB_TOKEN after the
  Settings token's refusal turns TestSettingsTokenRefusedGoesWithoutEnvToken
  red; MYCF711 showed TestSettingsTokenRefusedAskedAgainWithout red with
  from == "settings" back in Do.

Not tried: a real revoked token against api.github.com. The Plugins and
Library pages have no notice of their own (Settings' row says it).
2026-10-28 03:50:46 +08:00
yetone 0f044e9447 plugins: a GITHUB_TOKEN GitHub refuses is dropped for anonymous requests, not shown as an error (till on Discord)
till's Plugins page said "无法连接 GitHub: GitHub refused GITHUB_TOKEN
(no GitHub token is set in Settings → Network and sharing)". ghtoken.Token
falls back to the environment's GITHUB_TOKEN / GH_TOKEN, and a stale one
there made every GitHub read fail, though GitHub serves the same reads
without a token.

ghtoken.Do now sends a GitHub API request with the token. When the token
came from the environment and GitHub answers 401, it remembers that token
as refused for the rest of the process (Token() skips it, so GH_TOKEN is
still tried after a refused GITHUB_TOKEN, and Settings no longer shows it)
and asks once more without Authorization, returning that answer. A token
set in Settings is never dropped: its 401 is still Refused()'s error. An
anonymous rate limit after the drop is said by LimitedBy as before.

Every caller goes through it: plugin fetchJSONFrom (tagged-repo search,
git plugin checks, githubHead) and getURLFrom (Bun), and library apiGet,
skills fetch, skilltree fetchBlob and latestRTK via doGitHub.
internal/davsync/github.go stays strict: its token is the one the user gave
sync to write a private repo, outside ghtoken, and an anonymous retry can't
write there, so it would only hide why sync failed.

Tests (httptest only): ghtoken_test covers an env token refused then asked
again without, not sent again (and GH_TOKEN tried next), a Settings token
refused stays an error, an env token refused then an anonymous rate limit
is reported, and another host untouched. plugin TestTaggedReposStaleEnvTokenDropped
is till's case: TaggedRepos with GITHUB_TOKEN=stale-till lists 7 repos, no
error. github_token_test and library TestGitHubRateLimitSaid now expect the
env token dropped and the Settings token still refused.

Broken by hand, each red: (a) no anonymous retry: every new env test, the
plugin and library tests red, and TestTaggedReposStaleEnvTokenDropped shows
till's exact error; (b) no memory of the refusal: NotSentAgain, the plugin
and library tests red; (c) a Settings token also retried:
SettingsTokenRefusedStaysAnError, the plugin and library tests red.

Run on the commit before the rebase (base 9b04c4e20):
- gofmt -l: clean. go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
- set -o pipefail; go test -timeout 45m ./... at load ~30: 62 packages ok.
  agent, gateway and library ran past 45m while still progressing; gui
  failed TestSetAnswersWhileDevinAndProvidersHang (set took 10.26s, #1375)
  and TestMainWindowCorners (measuring killed, #1936/#2645); provider failed
  TestPluginPicksComeBackWithTheirList (#3051, fixed on main in c922225f1).
- Each rerun alone at load 21-56: library ok 809.7s, provider ok 308.8s,
  gui ok 159.5s, agent ok 1673.3s, gateway ok 780.4s.

After rebasing onto a3ebed202 (main moved 8 commits): gofmt -l clean; go vet
darwin, linux -tags nogui, windows clean; go test -count=1 at load ~57:
ghtoken ok 2.9s, plugin ok 277.6s, library ok 144.2s.
2026-10-21 06:16:31 +08:00
yetone 431707944e plugin, library: Check for updates asks GitHub with the GitHub token from Settings, and says when GitHub's rate limit is used up (#1942, bingqilinweimaotai)
What changed: a plugin installed from GitHub (github:owner/repo) is
checked by asking GitHub's API for its branch's commit (githubHead,
through fetchJSON). That request went without a token, so a token saved
in Settings → Network and sharing did nothing for it: the check stayed
at GitHub's 60 requests an hour for the whole address, which a shared
proxy exit uses up, and the row said only "<url>: 403 Forbidden".

- The token lookup, where a token may go and how a rate limit is told
  move from internal/library into a new internal/ghtoken, shared by the
  library and the plugin package (plugin can't import library).
  The order is unchanged: Settings, then GITHUB_TOKEN, then GH_TOKEN.
- plugin's fetchJSON has a request to GitHub's API's host (githubAPI)
  carry it, which covers the git plugins' check and the Plugins page's
  topic search. Raw files, npm and every other host never get it, and
  source.Do already sends nothing with Authorization to a mirror.
- An answer from GitHub's API that is a rate limit (429, or 403 with
  X-RateLimit-Remaining 0, Retry-After or a rate-limit message) is a
  ghtoken.Limited: the check's row has Why "limited" and Limited {until,
  token} for the page, as the library's skill check already had. A 401
  to a request with a token names which token was refused, never the
  token.

What was run:
- TestGitPluginCheckCarriesGitHubToken (new, internal/plugin): asks with
  no token / GH_TOKEN / GITHUB_TOKEN / Settings in that order of
  precedence, a 403 rate limit with and without a token, a 429 with
  Retry-After, a refused GH_TOKEN, and a raw-file host that must get no
  Authorization. Broken by hand twice: with fetchJSON's Authorize removed
  it fails "GH_TOKEN: GitHub's API was asked with Authorization "", want
  "Bearer gh-env"" (and the other two sources); with the limit check
  removed it fails with Why "registry", Limited nil, "403 Forbidden".
- go test -tags nogui -count=1 ./internal/library ./internal/plugin
  (temp HOME, pinned caches): ok, the library's TestGitHubToken*,
  TestGitHubRateLimitSaid among them.
- gofmt -l clean; go vet -tags nogui on darwin, GOOS=linux and
  GOOS=windows for internal/plugin, library, ghtoken, gui: clean.
- Not tried against the real api.github.com (tests stay off the network).
2026-10-13 15:19:53 +08:00