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.
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
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.
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.
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.