Commit Graph
8 Commits
Author SHA1 Message Date
yetone 6fc532198f provider: a CLI's sign-in is asked again only when its files change (#3185)
magpie asked each agent CLI who is signed in once a minute, for as long
as it ran. cursor-agent is a Node program that asks Cursor's servers:
~0.5s of CPU and ~200 MB each time. devin auth status took ~1.9s wall.
On a laptop that is a process spawned every minute, all day.

cliIdentity now takes a mark: a cheap fingerprint of where the CLI keeps
its sign-in (Cursor: authInfo in ~/.cursor/cli-config.json; Devin: a hash
of its credentials file). While the mark reads as it did when the answer
was asked, nobody can have signed in or out, and the CLI is asked again
only after 30 minutes. A changed mark, or one that can't be read, is
asked at once as before. The mark is a file read, never the keychain, so
nothing that can block runs under the identity's lock.

Run:
- TestCLIIdentityAskedOnASignInChange: red with unchanged() forced false
  ("sign-in unchanged, a minute on: asked 2 times"), green with the fix.
- go test ./internal/provider -run 'CLIIdentity|Cursor|Devin': ok.
- Real use: magpie serve in a sandbox HOME holding copies of the owner's
  Devin and Cursor CLI installs and sign-ins, Providers open in WebKit,
  every child of serve logged every 0.3s for 5 minutes, caches warm:
  origin/main b1a2eec2c: devin auth status 3 times, serve CPU 12.66s
  this change:           devin auth status 0 times, serve CPU 12.32s
- Not tried: cursor-agent about in that run; the sandbox's Cursor plugin
  wasn't signed in, so Cursor's path is covered by the unit test only.
- Full suite at 4354a7db2 (on origin/main b1a2eec2c): go test -timeout 45m
  ./..., 68 packages ok (gateway 660s, agent 530s, provider 272s).
  gofmt -l clean; go vet for darwin, and linux and windows -tags nogui.
- Rebased onto 21fb17e4d (only internal/gateway/gateway.go and a test
  changed); at the pushed head go build ./... and go test
  ./internal/provider -run 'CLIIdentity|Cursor|Devin' ok.
2026-10-22 02:25:34 +08:00
yetone 49caff3caf provider tests: a no-wait return is asserted by holding the slow thing, not a wall-clock bound (#1617)
The siblings of #1586 that 7b151cf54 left alone. Each said "comes back
without waiting" by measuring time.Since(start) against a bound (50ms,
500ms, 1s, 2s, 3s) that a loaded runner can pass and so make red without
the guarantee breaking. Each now holds the slow thing until it has its
answer, so a call that comes back at all came back without waiting for it.
A deadline is kept only so a call that did wait fails as a FAIL rather
than a hang, and each is short of the slow thing's own timeout, so a call
that waited cannot come back in time by having it time out:

- cli_identity_test TestCLIIdentityFirstAskBounded: both looks run while
  the ask is held (it was already). The second look is run with firstAsk
  set to an hour, so a second look that waited again (not only waited the
  whole ask) is caught; 10s guard.
- claude_status_test TestClaudeIdentityServedWhileAsked: the stand-in CLI
  now waits for a file instead of `sleep 1`, and writes another when
  reached. The stale look must come back while it is held, with the ask
  still in flight (claudeStatusBusy), and the CLI must have been asked
  again. The guard is 4s, short of askClaudeStatus' own 5s kill. Its
  cleanup lets the CLI go and waits for the ask to end.
- partners_test TestPartnersDoesNotWait: the five Partners() calls run
  while the site is held; 10s guard (the fetch's own is 20s).
- partners_test TestPartnersNow and plugin_icon_test
  TestRepoIconsWaitForFirstFetch had a floor and a ceiling. The floor is
  the guarantee that the call waited for its bound, and a busy machine only
  lengthens it, so it stays (100ms, and 250ms for a 300ms wait as before).
  The ceiling was the "doesn't wait past its bound" half: it is now that
  the call comes back while the site/fetch is held; 10s guard (the fetch's
  own are 20s and 30s).
- sub2api_usage_test TestKeyAllowanceNeverWaits: the ten looks run while
  sub2api is held and report back over a channel, not t.Errorf from a
  goroutine that could outlive a failed test; 10s guard (the read's own is
  30s).

What was run (temp HOME, caches pinned):
  go test -tags nogui -race -count=10 -run '<the six tests>' ./internal/provider  ok
  go test -tags nogui -count=1 ./internal/provider                                ok 395.6s
  gofmt -l; go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider  clean

Broken by hand, each red at the new assertion:
  cli_identity.go, drop c.waited = true (every look waits):
    :124 a second look waited
  cli_identity.go, drop the firstAsk case (the first look waits the ask):
    :116 the first look waited for the held ask
  account.go, askClaudeStatus() in the stale case under the lock:
    claude_status_test.go:111 a stale look waited for the held CLI
  partners.go, Partners runs refreshPartners in line:
    :246 Partners waited for the held site
  partners.go, PartnersNow without its time.After(wait):
    :316 PartnersNow waited past its bound for the held site
  key_allowance.go, KeyAllowance reads in line (lock let go around it):
    sub2api_usage_test.go:279 a look waited for the held read
  plugin_icon.go, RepoIcons without its timer case:
    :168 RepoIcons waited past its 300ms for the held fetch
2026-10-11 22:02:50 +08:00
yetone 9c37723110 provider: a test binary keeps no CLI identity on disk, so a late ask can't write into the next test's folder (#1538, sibling of #1524)
TestDshHomePickSurvivesSyncAndEffort failed under full-suite load and
passed alone. The cause is cliIdentity: a look starts an ask of the CLI
(a failing testenv stand-in) and, once the answer is a minute old, a
refresh in a goroutine. keep() writes cli-identity.json under the
XDG_CONFIG_HOME current when it finishes, so an ask slow under load,
still going when its test ended, wrote into the config folder of the
test running then while t.TempDir's cleanup was removing it:
"TempDir RemoveAll cleanup: ... directory not empty".

4ee3a76b fixed this for internal/gateway alone (KeepCLIIdentities=false
in its TestMain). internal/agent, and every other package whose tests
read the accounts (., access, backup, davsync, gui, profile, sessions,
stats, tui, usage), still could. keep() now skips in any binary testenv
isolated (MAGPIE_TEST_SANDBOX, as internal/proc's sandboxVar reads it),
and the provider tests of the file itself turn keeping back on with
keepIdentities(t). The gateway's own switch and its test are gone;
TestCLIIdentityNotKeptInATestBinary covers every package.

Ran:
- Reproduced by injection (get refreshes on every look, 15ms sleep after
  c.ask()), agent Dsh tests -count=3: old code red 2 of 6 runs with the
  exact TempDir cleanup error; new code 0 of 6.
- Broke by hand (keepsIdentities() returns true):
  TestCLIIdentityNotKeptInATestBinary red, "written in a test binary".
- go test -count=1 ./internal/provider ./internal/agent ./internal/gateway: ok
- gofmt -l clean; go vet provider, agent, gateway on darwin,
  GOOS=linux -tags nogui, GOOS=windows: clean.
2026-10-11 03:28:48 +08:00
yetone 4ee3a76b7f gateway: a test's config folder isn't written into by a CLI identity asked behind its request (#1524)
TestEncodedBodyInflatedPastLimit failed under load at its TempDir cleanup
("unlinkat .../001/magpie: directory not empty"). The server wasn't the
writer: New() starts no goroutine, and httptest's Close waits for the
handlers. The writer is provider's cliIdentity. Reading the accounts
reads Cursor's and Devin's identity. In the gateway's tests, testenv's
stand-in cursor-agent and devin are on PATH, so their CLIs count as
installed. After the first answer, a look older than a minute asks the
CLI again in a goroutine and doesn't wait for it. When the stand-in
answers signed out, keep() writes cli-identity.json (MkdirAll, then a
temp file renamed in) under the XDG_CONFIG_HOME of whatever test is
running then. Under load the stand-in takes seconds, so the write lands
as that test's TempDir is removed.

Found by injecting a delay, not by rerunning. With get() refreshing at
every look and a 15ms sleep after the ask, a -count=3 run of
TestEncodedBodyInflatedPastLimit failed 3 subtests with the exact error
from the issue (/v1/responses/zstd, /v1/chat/completions/zstd,
/v1/videos/gzip). With this change and the same injection,
-count=5 of TestEncodedBody*, TestUnknownBodyEncodingRefused and
TestKeyLimitReservesDecodedBody passed.

provider.KeepCLIIdentities (default true) now decides whether the
answers are kept on disk. The gateway's TestMain turns it off, as it
already turns off keepRoutes for the same reason. Production doesn't
change. TestNoCLIIdentityKeptInTests goes red if TestMain leaves it on.
TestCLIIdentityNotKeptWhenOff checks that an answer is still served and
nothing is written.

The same pattern: every gateway test that calls New() and never stops it
was a suspect. Server has no Stop and New() starts nothing, so the pattern
itself is harmless; the background write was the cause wherever a
request looks at the accounts. provider's own tests still keep
identities on, since several of them test the file; they haven't failed
this way.

Run: go test -tags nogui -count=1 -timeout 40m ./internal/gateway
./internal/provider (ok); gofmt -l; GOOS=linux and GOOS=windows go vet
-tags nogui ./internal/gateway ./internal/provider.
2026-10-11 00:52:58 +08:00
yetone 1f14004676 provider: a CLI that fails to say who is signed in, or takes 5s or more, is asked again after 1, 2, 4… minutes up to 30, not every minute: a cursor-agent that calls itself no longer runs for 10s every minute (#1278, JJ-Lin)
Cursor's and Devin's sign-in is read from their CLIs (cliIdentity), asked
again behind what is served once the last answer is a minute old. A
cursor-agent that calls itself (an old ~/.local/bin wrapper running
`cursor agent`) never answers: each ask ran its 10s timeout and started
hundreds of processes, every minute magpie's pages or gateway read the
accounts. With no Cursor token kept, it still counted as an answer (nobody
signed in), so a "failed asks back off" rule alone would not have reached
JJ-Lin's case: an ask that takes slowAsk (5s) or longer backs off too. A
quick answer, or a sign-in or out (forget), has the CLI asked every minute
again. What is served is unchanged: a failed ask keeps it, a slow answer is
taken as before.

Verified:
- red without the change: TestCLIIdentityFailingAskedLessOften/fails and
  /slow both fail with "asked again a minute after the second"; pass with
  it (-race -count=3 with the other TestCLIIdentity tests).
- go test -tags nogui ./internal/provider/ in a sandbox HOME; go vet -tags
  nogui of provider for darwin, linux and windows.
2026-10-08 18:18:15 +08:00
yetone 2de043dabf provider: Cursor stays listed when cursor-agent can't say who is signed in (cataglyphis: 供应商选型中间歇无法显示 cursor,导致路由里不能正常路由到 cursor 的模型) — who Cursor's account is comes from cursor-agent about --format json, asked again behind the served answer every minute, and any about that failed, ran past its 10s (it asks Cursor's servers), or printed something besides its JSON was taken for nobody signed in: Cursor dropped out of the Providers page and of routing (All() lists the accounts for both), and that was kept on disk, until an ask a minute or more on came back. Now an ask that couldn't tell leaves what was served, on disk too, and is asked again in a minute (cliIdentity's ask returns an error for that); Cursor is taken as signed out only when about says so in its JSON or cursor-agent keeps no token. About's JSON after a line of its own (an update notice) is read too. Devin's ask is as before. Tested with fake cursor-agent scripts in a temp HOME (signed in, update notice, says nobody, fails, no JSON, fails with the token gone) and the cache through a failed ask and a sure sign-out; not tested against a real cursor-agent, and what made reporter's about fail isn't known 2026-09-28 21:02:48 +08:00
yetone 8ad73e8bfc accounts: a slow or hanging agent CLI no longer holds the providers page (#123) — its timeout now ends the call (a CLI that is a script running node left node holding the output, and the call waited for it however long, the 10s timeouts notwithstanding: a CLI's pipes are closed 2s after); a CLI never answered before is waited for 3s, once, then served as nobody signed in until its answer comes behind; and a CLI saying nobody is signed in is kept like any answer, where before it wasn't and every start waited for it again. Tried with cursor-agent and devin hanging 20s and the network black-holed: the first providers call went from 40.6s to 6s on a first start, and to 13ms on the next 2026-09-28 11:20:14 +08:00
yetone 0686bdffa7 accounts: a magpie just started no longer holds every page and request for cursor-agent and devin — who their CLIs say is signed in is kept on disk (cli-identity.json), served at once on the next start and asked again behind it; only a first look ever, or one after a sign-in or out, waits for the CLI. The window's first read went from ~4s to ~0.15s here with both signed in, and the gateway page shows its skeleton, not blank cards, while the state loads (#123) 2026-09-28 09:21:24 +08:00