Commit Graph
10 Commits
Author SHA1 Message Date
yetone d1436466a6 gui: Kimi Code's region picker says to pick by the account's region (Kimi)
Kimi (a sponsor) asked for two things on 10-27: start the partner card
at the user's region, which ef155a5ca did, and "并在编辑器加一句「按你的
Kimi 账号所在区域选择」", because China and Global accounts don't work
on each other's endpoints. The second was not done: the picker still
said "which endpoint Kimi Code is reached through".

Now a partner whose regions include both China's and Global's (placeOf,
the same reading homeRegion uses) is sent with splitAccounts, and its
picker's hint reads "pick the region of your {p} account" (zh: 按你的
{p} 账号所在区域选择, and zh-TW, ja, de). Of the partners served on
10-27 only Kimi has both; APIKEY.FAN's Default/Direct and the partners
without regions keep the old hint. Built-in presets are left alone:
yylx lists Global and China Mainland, and one yylx account works at
both, so the rule is not true for every China/Global pair.

app.js stays within TestCoreScriptBudget without a raise: the test is
decided in Go, the English key is short, and the Bedrock comment above
the picker is one line.

Tests:
- TestPartnerStartsAtTheUsersRegion: SplitAccounts is set on kimi and on
  no other partner of testdata/partners-2026-10-27.json, at home and
  abroad. Broken by hand (SplitAccounts always false): "kimi (-1):
  SplitAccounts false".
- partners.test.cjs: the region hint in en/zh/ja/de at 900 and 440,
  chromium and webkit, 19/19 each. Broken by hand (app.js back to the
  old hint): "actual: 'which endpoint Acme AI is reached through'".
- add-sheet, key-plan, segment-press-dialog, tencent-cloud,
  strip-scrollbar: chromium and webkit, all pass.
- i18n-keys: 3/4; "a plugin's words are its own" fails the same at
  origin/main 2b0387574 without this change (#3897).
- go test -tags nogui ./internal/gui ok. ./internal/provider: crashed in
  TestUnreadLoginsWriteErrorsKeepCredentials (#3632); with that test
  skipped, three reds, TestFetchAsksASharedBaseOnce and two siblings,
  which fail the same at 2b0387574 without this change (#3854).
- gofmt -l clean; go vet on darwin, linux (nogui) and windows for
  internal/provider.

Not tried: the real app with Kimi's live partner card; no full ./... run.
2026-10-28 00:19:58 +08:00
yetone ef155a5ca2 provider: a new partner provider starts at the region where the user is, Kimi Code's Global plan abroad (Kimi)
Kimi (a sponsor) reported: "伙伴卡编辑器默认区为 China,建议按用户区域分流".
A partner given only by regions started at its first one, and Kimi
Code's partner card lists China first, so a user abroad was offered the
China plan's endpoints, key page and catalog.

Now, when a partner's first region is China's or Global's and the other
is listed too, a new provider of it starts at the one where the user is:
China's in mainland China, Global's elsewhere. The place is read from
this computer's time zone: LocalZone ($TZ, /etc/localtime) on macOS and
Linux, the registry's TimeZoneKeyName on Windows. Asia/Shanghai,
Asia/Urumqi and their old names (Chongqing, Harbin, Kashgar, PRC) and
Windows' "China Standard Time" are mainland China; Hong Kong, Macau and
Taipei have zones of their own and get Global. Windows' China Standard
Time also covers Hong Kong, which Windows doesn't tell apart. A zone
that can't be read keeps the partner's first region, as before.

It is decided in validPartners, where the partner's default endpoints
are filled in, so every add path takes it: the GUI editor (its region
picker marks the region at the preset's endpoints, and Get a key and the
site link follow), the TUI's and CLI's add (FromPreset). The default
region's key page, site, siteHost and catalog become the preset's with
its endpoints, as atRegionOf does for a provider.

Which regions count: only one with id cn/china/global/intl or named
China, China Mainland, Mainland China, Global or International. Checked
against each partner in site/partners.js: Kimi (China, Global, "Pay as
you go · China", "Pay as you go · Global") switches between its two
plans only; APIKEY.FAN (Default, Direct) gives its own top-level
endpoints, so no region is defaulted; WandAPI, CoreCode, Sidrune and
FastAIToken have no regions. yylx (a built-in preset, not a partner)
lists Auto first and would stay at Auto anyway.

Providers already added keep their region: a provider stores its own
endpoints when it is added, and its region is read from them
(regionOf), so a user who moves keeps theirs. The list kept on disk
from before is fixed at the next fetch, which runs at start.

Tests:
- TestPartnerStartsAtTheUsersRegion, from usemagpie.ai/api/partners as
  served on 2026-10-27 (testdata/partners-2026-10-27.json): abroad Kimi
  Code starts at api.kimi.ai, keys /go/kimi/keys/global, site host
  www.kimi.ai, catalog kimi-code-plan-global; in China and with no zone
  at api.kimi.com and China's; a provider added in China stays there
  after the user is abroad and the list is fetched again; APIKEY.FAN
  starts at its own endpoints either way.
- TestHomeRegion: Kimi's four regions, yylx's Auto first, Global first,
  plans only, no Global; chinaZone for Shanghai, Urumqi, PRC, China
  Standard Time, Hong Kong, Taipei, Singapore, New York, UTC and "".
- Broken by hand: with the region back at p.Regions[0], the test fails
  "abroad: a new Kimi Code provider is at {chat:https://api.kimi.com/
  coding/v1 … catalog:kimi-code-plan-cn}, want {… api.kimi.ai …
  kimi-code-plan-global}" and "the preset's endpoints … aren't its home
  region's". With the key page/site/catalog line dropped, it fails on
  keys https://usemagpie.ai/go/kimi/keys (not …/keys/cn) in China.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/cli ./internal/tui at 56de2d192 + these two commits: cli
  ok, tui ok. internal/provider has three reds that are already on
  main, and they fail the same way at origin/main 14e4d8040, just
  fetched, run alone -count=3: TestFetchAsksASharedBaseOnce,
  TestFetchAsksOneListOnceForTwoBasesOfIt and
  TestFetchUnionsProtocolListsWithoutChat (#3854). The first run, with
  nothing skipped, also crashed in TestUnreadLoginsWriteErrorsKeepCredentials
  ("close of synctest channel from outside bubble", #3632, red on main),
  so the second run skipped that one test. No full ./... run: the
  machine's disk was full (ENOSPC) from other sessions' caches.
- go build; GOOS=linux go build -tags nogui; GOOS=windows go build.
- Rebased onto 14e4d8040 (45 files changed on main since 56de2d192,
  none of them these): go build, go vet ./internal/provider and
  -run 'TestPartner|TestHomeRegion|TestPreset' ./internal/provider ok
  at the pushed head; the package run above was not repeated there.
- gofmt -l: nothing; go vet, GOOS=linux go vet -tags nogui and
  GOOS=windows go vet on internal/provider: clean; GOOS=windows go build.

Not tried: the Windows registry read on a real Windows machine (the
Windows box didn't answer ssh); the GUI editor with a real partner list
in a sandbox; the GUI suite (no file under internal/gui changed).
2026-10-27 18:45:45 +08:00
yetone 1537817e0d provider: a partners.json that doesn't parse is kept aside before a try writes over it (#3362)
loadPartners reads a partners.json that is there but doesn't parse as
an empty cache: no list, and no Seen. Before 603408f38 only a fetch
that got the list wrote the file, so the loss waited for one. Now every
try writes it before the fetch is sent (Tried), offline too, so the
first try after a start wrote the empty cache over it: Seen holds the
presets of partners no longer listed, which providers added from them
still use (their endpoints, regions and key page), and those were gone.
A failed read is unknown, not empty (docs/code-standards.md).

savePartners now goes through lastgood.KeepUnparsed first, as
balance_history, credits_daily and codex_switch do for files their
readers take as empty when they don't parse: such a file is copied
aside as partners.json.bad-<time> before it is written over, and a
file that can't be read at all (or a copy that can't be kept) is not
written over. A file not there, or empty, writes as before.

Old data: a file already written over by 603408f38 is not in any
release (603408f38 lands in the same push), so nothing is lost before
this; a partners.json damaged earlier was written over by the next
successful fetch, as it still is, now with its copy kept.

Run (tree of this stack, on f6a96b2f0, go1.26.8, darwin/arm64):
- TestPartnersUnparsedFileKeptAside (a cut-short partners.json, the
  list server answering 503, one try): with savePartners' KeepUnparsed
  removed by hand, red: "partners.json that didn't parse was written
  over with nothing kept aside ([])". With it: green, the .bad- copy
  byte for byte the file, and the try's error recorded.
- go test -tags nogui -race -count=5 -run 'Partner|Icon'
  ./internal/provider: ok.
- Broken by hand in 603408f38's own code, each red in
  TestPartnersKeepTheirLastTry: no save before the fetch (line 590:
  "while a fetch is out the file says ... error "partners: 503 ..."");
  the failure logged every try (573: "logged 3 times, want once").
- internal/gateway and internal/cli -run
  'Goroutine|Heap|CPU|Help|Profile': ok. goroutinesRefused made to
  return "" by hand: TestGoroutines... red ("Origin null with a key
  (lanGuard true): 200 ..."), restored green. The dump's frames carry
  argument words in hex, not what they point at, as 54636afa2 says.
- Full suite at this stack's head, go test -tags nogui -count=1
  -timeout 45m ./... (tree 0ef74708, one suite on the machine's
  other runs' load): exit 0, 70 packages ok, no red.
Not tried: a real app left running against usemagpie.ai; an unreadable
(EACCES) partners.json; Windows.
2026-10-24 21:33:06 +08:00
JoeyWu-tech a2cef8bf4f provider: partners.json keeps when the list was last tried and how it ended (#3362)
"partners":null} for three days, and nothing told a fetch that failed
every time from one that never came back, or from none being asked. A
failed fetch wrote nothing and logged nothing.

refreshPartners now keeps two fields with the list:
- tried: when the last fetch started, written before it is sent;
- error: how it failed (whitespace folded, cut at 500 runes), cleared
  when the next one starts.
Read together: error set, the last try failed; tried after at with no
error, a try is still out; tried long ago, nothing has asked since. A
failure is logged once until its error changes or a fetch gets the list
again, and the fetch that gets it after a failure is logged too. The
desktop app's log goes nowhere a user can read, so for the app the file
is what tells.

Checked for #3362 on the way: v0.1.1296's fetch, keep and save, given
the bytes /api/partners serves now (Kimi Code, listed from 10-23 00:00
+08:00) over the 10-20 file, writes the file, with the icon fetched or
not. So it isn't the save or the list.

Run on b437e4ad, go1.26.8. internal/provider is the same here and at
the branch's head (tree 613df3a9), where the checks of it ran:
- gofmt -l clean; go vet -tags nogui of internal/provider, gateway, cli
  and the root package with GOOS=linux, darwin and windows: clean.
- go test ./internal/provider -run Partner -count=1: ok.
- go test ./internal/provider -run '^TestPartners' -race -count=20: ok.
- TestPartnersKeepTheirLastTry on b437e4ad's partners.go: red at
  partners_test.go:511 (after a 503 there is no partners.json at all).
- Broken by hand, each red in TestPartnersKeepTheirLastTry: no write
  before the fetch (line 535: a try out shows no try since the last);
  the error not cleared when a try starts (535: a try out still shows
  the last error); the failure logged on every try (518: "logged 3
  times, want once"). Restored, green.
- Full suite at this commit, go test -tags nogui -count=1 -timeout 45m
  ./... (tree a661f529, 482s): 62 packages ok. 15 reds in 7 packages,
  none in a file this commit touches, each red on b437e4ad without it:
    internal/agent TestInstalledVersion, TestHowInstalled,
    TestOmpDetectedWithPiDir, TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
    TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestZedAppPath:
    proc.Look keeps "not found" for a tool written in the tick of its
    folder's last change (#3141).
    internal/agent TestReasonixCLIRequiresSupportedNativeVersion and
    TestReasonixCLIRecoversAfterProbeTimeout: their fixture build under
    /tmp meets a /tmp/.git that isn't a repository of this user's
    ("error obtaining VCS status: exit status 128").
    internal/claudebridge TestCallbackGoesPastTheAgentsProxy,
    internal/gateway TestClaudeToolCallbackOnANonLoopbackAddress,
    internal/gui TestGatewayHeldByAContainer and
    TestGatewayFixQuitsOnlyAMagpie: the processes this run started had
    their outgoing connections sent through a proxy (graftcp).
    internal/edit TestWriteAtomicReadOnlyHardLink and internal/testenv
    TestProgram: umask 077 here; the fixtures' own modes go through it
    (0444 is written as 0400).
    internal/netproxy TestBunProxyLoginWithReservedCharacters: Bun
    1.3.11 on PATH names port 80 in the request line (#3397).
Not tried: a real app left running against usemagpie.ai, on macOS or
Windows.
2026-10-24 21:33:06 +08:00
JoeyWu-tech f252b403d3 provider: the icon prune leaves a listed partner's logo, so the add sheet keeps drawing it (#3424)
partnerIcon keeps a partner's logo in the icons folder beside
providers.json and, while its iconUrl stays the same, uses the file it
kept as it is (97902e93), so the file only ages. No provider points at
it until one is added from the partner, and pruneIcons, after every
save of providers.json, removes a picture no provider or plugin names
once it is an hour old. A logo kept from an earlier day is that old at
once, so the first save after a start (a key, a model picked, an order,
a sign-in, a sync's pull) took it: /api/icons/<name> answered 404 while
the partner still named file:<name>, the add sheet's img fell back to
the generic icon, and the next fetch, finding the file gone, downloaded
it again, for a save an hour on to take. Before 97902e93 every fetch
downloaded it again, six hours apart, and a save an hour after each
took it the same way.

Now pruneIcons also leaves the pictures of the partners listed now
(keptPartnerIcons), as it leaves a plugin's (keptPluginIcons). A
process that hasn't asked for the list reads it from partners.json, as
Partners does, so a save from the CLI leaves them too. A partner no
longer listed loses its logo at the next prune unless a provider added
from it names it, as before. With the list off (MAGPIE_PARTNERS=off,
and every test that names no list) nothing is kept for it.

Lock order: store, then pruneIcons, then partnerMu. Nothing under
partnerMu saves providers.json or takes a lock store holds, and no
fetch runs under it.

Old data: a logo pruned before this is fetched again by the list's next
fetch (at start, then hourly), as it was; nothing to move.

What was run (tree 0409d43e, on a93190ae, go1.26.8):
  broken by hand, with this change: pruneIcons without the
  keptPartnerIcons loop:
    partners_test.go:489: the listed partner's logo was pruned: stat
    .../icons/d1774929a9b62027.png: no such file or directory
    partners_test.go:496: the logo was downloaded 2 times, want once
  a real run: magpie web in a sandbox HOME with the real list
  (usemagpie.ai/api/partners, Kimi Code) and stats off. A first start
  kept Kimi Code's logo; its file was set two hours back, as one kept
  from an earlier day is; a second start's fetch used it as it was;
  then magpie provider add Local url=http://127.0.0.1:9/v1 saved
  providers.json:
    main (a93190ae): the file gone, GET /api/icons/<name> 404,
    /api/providers still naming file:<name>, and a new Chromium drew
    Kimi Code in the add sheet with the generic icon
    this change: the file kept, 200, and the add sheet drew the logo
    (128 px wide)
  gofmt -l internal/provider: nothing
  GOOS=linux, darwin, windows go vet -tags nogui ./internal/provider:
  exit 0; CGO_ENABLED=0 GOOS=darwin and windows go build -tags nogui
  .: exit 0
  full suite, go test -tags nogui -count=1 -timeout 45m ./...: a first
  run stalled, the test binaries of 14 packages sitting in close(2) for
  45 minutes until the timeout (internal/provider ok); run again with
  nothing changed (445 s): 62 packages ok, internal/provider among them.
  13 tests red in 8 packages, none in a file this change touches:
    internal/agent TestInstalledVersion, TestOmpDetectedWithPiDir and
    TestZedAppPath: proc.Look keeps "not found" for a tool written in
    the tick of its folder's last change (#3141).
    internal/agent TestReasonixCLIRequiresSupportedNativeVersion and
    TestReasonixCLIRecoversAfterProbeTimeout: their fixture build under
    /tmp meets a /tmp/.git that isn't a repository of this user's
    ("error obtaining VCS status: exit status 128").
    internal/claudebridge TestCallbackGoesPastTheAgentsProxy,
    internal/gateway TestClaudeToolCallbackOnANonLoopbackAddress,
    internal/gui TestGatewayHeldByAContainer and
    TestGatewayFixQuitsOnlyAMagpie: the processes this run started had
    their outgoing connections sent through a proxy (graftcp).
    internal/edit TestWriteAtomicReadOnlyHardLink and internal/testenv
    TestProgram: umask 077 here; the fixtures' own modes go through it
    (0444 is written as 0400).
    internal/netproxy TestBunProxyLoginWithReservedCharacters: Bun
    1.3.11 on PATH names port 80 in the request line (#3397).
    internal/middleware TestFailOpen: "spin took 1.508697807s", past
    its 1 s bound under the run's load; alone, -count=10: ok.
Not tried: the desktop app's webview (a browser that drew the logo
before keeps drawing it from its cache, since /api/icons answers
immutable for a year), macOS, Windows.
2026-10-24 21:33:06 +08:00
yetone 7e0c031059 provider, gateway: the magpie serving the gateway fetches the partner list every hour, asked for or not
yetone: 改成后台每小时定时请求一次. Until now the list was fetched again
only when the Add sheet or the TUI asked for it, so an app left open on
another page never learned of a partner added.

KeepPartnersFresh runs beside the gateway's other Keep* loops: it
fetches when the gateway starts, then whenever partnerNext is due (an
hour, ten minutes after a failed fetch). It shares Partners'
one-fetch-at-a-time flag, so it never runs beside a fetch a page
started; while one runs it looks again a second later. Partners and
PartnersNow keep their own due check for a magpie that doesn't serve
the gateway (the CLI, a TUI beside a running app). MAGPIE_PARTNERS=off,
and every test (partnersSource), still asks nobody.

Run at this tree (base d3f3df746):
- go build ./..., GOOS=linux and GOOS=windows go vet of internal/provider
  and internal/gateway, gofmt -l clean.
- go test ./internal/provider -count=1: ok (94s).
- go test ./internal/provider -run Partner -count=3 -race: ok.
- Broken by hand: KeepPartnersFresh returning at once makes
  TestPartnersKeptFreshUnasked fail ("fetched on start").
Not tried: a real app left running for an hour against usemagpie.ai;
the internal/gateway suite (only a goroutine added to ListenAndServe).
2026-10-23 21:45:32 +08:00
yetone 97902e9383 provider: a started app fetches the partner list at once, and hourly after
The owner's MacBook Air, on yesterday's release, listed no partners hours
after Kimi Code joined the list. The list kept on disk set the next
fetch: six hours after it was fetched, read again at each start. A list
fetched empty minutes before Kimi was added held it back for up to six
hours, and quitting and opening magpie didn't help, since the new
process read the same time from disk.

- A process fetches the list on its first ask, whatever the kept list's
  age (loadPartners no longer sets partnerNext).
- It is fetched again after an hour, not six; the feed is ~1.7KB.
- An icon kept from the same URL is used without downloading it again,
  so the hourly fetch costs one small request. A partner's new logo goes
  at a new URL (e6bed2f06 moved Kimi's to partner-kimi.png). A failed
  icon fetch still falls back to the kept one.

Not explained by this: this mac's app (v0.1.1296, running since 10-20
20:39) kept a list from 10-20 15:40 for three days, past many six-hour
marks. The same binary fetches Kimi from the CLI at once. Nothing in the
fetch path can hang past its 20s bound, no proxy is set, and the app has
no goroutine dump to look at, so the cause is open: #3362.

Run at origin/main e6bed2f06:
- TestPartnersFetchedOnStart (new): a fresh empty list kept, a start
  lists acme. Red on the old partners.go ("" want acme).
- TestPartnersFromTheFeed: an icon from one URL is fetched once over two
  list fetches. Red on the old code (fetched 3 times).
- go test ./internal/provider ok (87s); go vet on darwin, linux and
  windows ok; gofmt clean.
Not tried: a real app across an hour's refetch.
2026-10-23 16:22:04 +08:00
yetone 40eba22141 provider: the partners tests fetch one list at a time, as magpie does, so a fetch Partners started can't store its older list over the one fetched after it (TestPartnersFromTheFeed red on ubuntu CI at 439cfc4a: "partners after the list emptied = acme,regioned")
Cause: TestPartnersFromTheFeed's first Partners() call (to see that
nothing is listed before a fetch) starts a background refreshPartners,
since no list was ever fetched. The test helper fetchPartnersNow called
refreshPartners directly, skipping the partnerFetches guard that
Partners and PartnersNow use, so the two fetches ran together. When the
background fetch read the full list and stored it after the helper's
fetch of the emptied list, the test saw acme,regioned. magpie itself
never runs two fetches at once (only Partners and PartnersNow start one,
both through the guard), so this is a test-only race, not a product
bug. resetPartners also cleared the guard while a fetch could still be
running, which let a late fetch store into the next test's state.

Fix: fetchPartnersNow waits for a running fetch and claims the guard
before fetching (claimPartnerFetch), and resetPartners waits for a
running fetch before it forgets the list.

New test TestPartnersFetchedLastIsHeld: the fetch Partners starts reads
the full list and answers late; the list is emptied and
fetchPartnersNow runs; the empty list must be what is held. With the old
fetchPartnersNow it fails 5/5 ("partners after the list emptied =
acme"). The CI failure itself was reproduced by injecting the delay: a
300ms first response from the feed server and a 400ms pause before the
check made TestPartnersFromTheFeed fail with CI's exact message on the
old code; with the fix it passes 5/5 under the same injection.

Run (temp HOME, Go caches pinned):
- go test -tags nogui -race -count=20 -cpu 1,2 -shuffle=on -run Partner ./internal/provider/: ok
- go test -tags nogui ./internal/provider/...: ok
- gofmt -l internal/provider/: nothing
- go vet -tags nogui ./internal/provider/ (darwin, GOOS=linux, GOOS=windows): ok
2026-10-09 07:21:19 +08:00
yetone 05b5965872 providers, gui: a partner listed since the add sheet last showed the partners puts a small dot on the "+ Add provider" button, its name in the title, until the sheet shows it (yetone: 有了新的合作伙伴的时候,这里能否有个小小的精致优雅的提醒吸引用户点击查看)
Ran: go test ./internal/provider ./internal/gui ./internal/tui .;
partners.test.cjs (chromium + webkit, en/zh at 900 and 440), i18n-keys,
gui-ja, gui-de; GOOS=linux -tags nogui and GOOS=windows builds.
2026-10-09 01:45:43 +08:00
yetone 10578ce683 providers: the add sheet lists Partners first, above Subscriptions, under a heading that says they sponsor magpie; who they are comes from usemagpie.ai/api/partners (site/partners.js), so a paid listing changes with a deploy, not a release (yetone)
The app fetches the list behind the call every 6 hours (10 minutes after a
failure), never waiting, and keeps it in the cache folder. Each entry is
checked: its own id, never a built-in preset's; a name and an endpoint;
https only; at most 12; no decision API, no-key or user endpoint. Its logo
goes through FetchIcon. A partner is a preset to the rest of magpie, and
one that has left still is to the providers added from it, unsponsored.
The window lists a partner in the languages it names only, with its tagline
in the page's language; `magpie presets` and the TUI list them first too.
MAGPIE_PARTNERS moves the list, off lists none.

Run: go test -tags nogui -race ./internal/provider ./internal/gui
./internal/tui . (ok); TestPartners* -race -count=3; node --test
internal/gui/tests/partners.test.cjs add-sheet gui-ja gui-de i18n-keys in
Chromium and WebKit (en/zh/ja/de, 900 and 440px); node --test
site/worker.test.mjs site/install.test.mjs; the built binary in a sandbox
HOME against a local feed: listed, an http entry left out, listed from the
cache with the site down, `provider add` from it.
2026-10-08 22:52:30 +08:00