vsoullai on X (0.1.4190, Windows): the gateway froze. Each request waited 15s in turn on the local masking model, routing was held 30s and got a 504, and a stopped WSL distro's probe dragged everything down until redactModel and WSL were turned off. #6167 (Codex held 30s then 504, fixed by restarting WSL) and #4856 (masking timeouts, 504s) are the same. WSL (internal/agent/wsl.go, wsl_links.go): - wsl's lock was held through wsl.exe and through the stat of \\wsl.localhost\<distro>, which can't be cancelled. The GUI's look held it, and every request's Codex/Claude stand-in, wslUp, distroAt and Copilot's WSL folders queued behind it. - A look (wslRefresh) now runs wsl.exe, the probes and the stat outside the lock. Looks take turns on wslRefreshMu, and the lock only guards reads and writes. - wslRoot's stat is bounded (wslRootWait 5s); a distro whose / can't be opened fails its probe and is tried again later. - What a request asks never waits on a look. "Which distros run" is asked single-flight in the background and waited for at most wslRunningWait (2s). A distro wsl.exe hasn't named as running counts as stopped, so nothing opens or wakes a distro on a guess. - Copilot's WSL sign-in (internal/provider/copilot_cli.go) is read the same way: single-flight, waited for at most copilotWSLWait (2s), with the last answer kept. Before, its own mutex queued every request behind the first one's read. Masking (internal/masking/masking.go): - With redactModel on, each request waited up to strictWait (15s). In a burst the backlog grew and the requests waited one after another. - Once a strict request runs out its wait with texts unread, the model is "behind". The next requests then wait only defaultWait (2s) until a request is read whole or a minute passes (behind, waitFor). The rules still mask everything the model hasn't read. Docs: docs/subsystems/agent-wiring.md (WSL twins) and gateway-routing.md (masking). Run: - New tests, on mote, -race -count=3: TestWSLHangHoldsNoRequest (wsl.exe --running, a distro's probe, the stat; each hang injected), TestWSLRootHangFailsProbe, TestCopilotWSLHangHoldsNoRequest and TestStrictModelBehindWaitsTheDefault, with the other WSL, Copilot WSL and masking tests. All ok. - Broken by hand: the same tests on the old code (origin/main's files with only the new tests added). Each WSL hang case failed ("a Codex stand-in waited on a hung WSL past 2.2s", "the GUI's look didn't wait on the hung WSL"). The Copilot test failed ("13 requests waited on a hung WSL past 2.2s"). The masking test failed ("request 2 after the model fell behind waited 2.0s"). - Full suite on mica at f08597a58 (this change onb754cb095): `go test -timeout 45m ./...`, exit 0, 101 packages ok. - Rebased onf03869520(a gateway/provider change). At this head, on mica: go test -count=1 ./internal/agent ./internal/provider ./internal/masking ./internal/gateway all ok, go vet ./internal/... and GOOS=windows vet of those four ok, gofmt clean. The full suite ran at the base before it, not again at this head. - Rebased again onc0cf352a8(four commits: gateway, provider Codex hold, providers base URL, LESSONS). At this pushed head, on mica: go test -count=1 ./internal/provider ./internal/masking ok, the WSL and Copilot tests of internal/agent ok, go vet of agent, provider and gateway (darwin and GOOS=windows) ok. - gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and GOOS=windows: ok. - Windows (xiaomi-gpu, win-run), internal/agent, internal/provider and internal/masking. masking: ok. agent and provider: 52 reds, all in the 53 that origin/mainf03869520has on the same box (#6068, #1827, #5068, #5344, #5950; this run's list is on #6068). None touch WSL, Copilot's WSL or masking. Not tried: a real hung WSL. On xiaomi-gpu, wsl -l -q and -l --running answered in ~55ms, and a terminated distro's \\wsl.localhost path failed in 131ms, so the hang was injected in the tests, not reproduced live. (vsoullai on X, #6167, #4856)
287 lines
8.9 KiB
Go
287 lines
8.9 KiB
Go
package provider
|
|
|
|
// The standalone Copilot CLI keeps its sign-in apart from the editors: who
|
|
// is signed in in ~/.copilot/config.json, the token in the system keychain
|
|
// (service copilot-cli), or in that file when the user chose plaintext. Its
|
|
// token is not traded for a session token the way an editor's is (that
|
|
// exchange answers 403): the CLI sends it as is, under its own integration.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/tidwall/jsonc"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// copilotCLIHeaders are what the CLI sends with its token.
|
|
var copilotCLIHeaders = map[string]string{
|
|
"Editor-Version": "copilot/1.0.88",
|
|
"Copilot-Integration-Id": "copilot-developer-cli",
|
|
"X-GitHub-Api-Version": "2026-07-01",
|
|
"User-Agent": "copilot/1.0.88",
|
|
}
|
|
|
|
// CopilotUserURL says where an account's Copilot API is; a var so tests can
|
|
// point it elsewhere.
|
|
var CopilotUserURL = "https://api.github.com/copilot_internal/user"
|
|
|
|
type copilotCLIUser struct {
|
|
Host string `json:"host"`
|
|
Login string `json:"login"`
|
|
}
|
|
|
|
// copilotCLIHome is where the CLI keeps its settings.
|
|
func copilotCLIHome() string {
|
|
if h := appdir.Getenv("COPILOT_HOME"); h != "" {
|
|
return h
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".copilot")
|
|
}
|
|
|
|
// CopilotWSLDirs, set by internal/agent, are the Copilot CLI's folders
|
|
// (~/.copilot), as magpie opens them, in the WSL distros that run now:
|
|
// magpie on Windows finds a Copilot CLI signed in inside one (#723,
|
|
// jia2). Where ~/.copilot is a link the folder is where it leads (#4502).
|
|
// nil, or none, off Windows. A stopped distro isn't in it: reading its
|
|
// files would start it.
|
|
var CopilotWSLDirs func() []string
|
|
|
|
// copilotWSLAge is how long a WSL distro's Copilot CLI sign-in is kept as
|
|
// read: its files are read over WSL's file share, slower than a disk's.
|
|
var copilotWSLAge = 20 * time.Second
|
|
|
|
// copilotWSLWait is how long a request waits for a distro's sign-in to
|
|
// be read again, from when that read started: one read at a time, outside
|
|
// copilotWSL's lock, and a read WSL holds up answers the sign-in read
|
|
// before. Held under the lock, a hung WSL queued every request behind
|
|
// the first, and the gateway sent 504s after 30s (vsoullai on X, #6167).
|
|
// A var for tests.
|
|
var copilotWSLWait = 2 * time.Second
|
|
|
|
var copilotWSL struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
app copilotApp
|
|
ok bool
|
|
done chan struct{} // closed when the read in flight ends
|
|
start time.Time // when it started
|
|
}
|
|
|
|
// copilotCLILogin is the account the Copilot CLI is signed in to, with its
|
|
// token: on this machine, or, for magpie on Windows, in a WSL distro that
|
|
// runs now. A distro's token is read only from its config.json, where the
|
|
// CLI keeps it when the distro has no keyring to put it in (WSL has none
|
|
// unless one is set up): magpie can't reach a distro's keyring.
|
|
func copilotCLILogin() (copilotApp, bool) {
|
|
if settings.AccountDiscoveryOff() {
|
|
return copilotApp{}, false
|
|
}
|
|
if app, ok := copilotCLILoginIn(copilotCLIHome(), copilotCLISecret); ok {
|
|
return app, true
|
|
}
|
|
if CopilotWSLDirs == nil {
|
|
return copilotApp{}, false
|
|
}
|
|
c := &copilotWSL
|
|
c.Lock()
|
|
if !c.at.IsZero() && time.Since(c.at) < copilotWSLAge {
|
|
defer c.Unlock()
|
|
return c.app, c.ok
|
|
}
|
|
if c.done == nil {
|
|
done, dirs := make(chan struct{}), CopilotWSLDirs
|
|
c.done, c.start = done, time.Now()
|
|
go func() {
|
|
var found copilotApp
|
|
ok := false
|
|
for _, dir := range dirs() {
|
|
if app, in := copilotCLILoginIn(dir, nil); in {
|
|
found, ok = app, true
|
|
break
|
|
}
|
|
}
|
|
c.Lock()
|
|
c.app, c.ok, c.at, c.done = found, ok, time.Now(), nil
|
|
c.Unlock()
|
|
close(done)
|
|
}()
|
|
}
|
|
done, start := c.done, c.start
|
|
c.Unlock()
|
|
t := time.NewTimer(time.Until(start.Add(copilotWSLWait)))
|
|
defer t.Stop()
|
|
select {
|
|
case <-done:
|
|
case <-t.C:
|
|
}
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
return c.app, c.ok
|
|
}
|
|
|
|
// copilotCLILoginIn is the account the Copilot CLI whose settings are in
|
|
// dir is signed in to, with its token: from its config.json, else from
|
|
// secret (the keychain), when given.
|
|
func copilotCLILoginIn(dir string, secret func(string) string) (copilotApp, bool) {
|
|
raw, err := readAccountFile(filepath.Join(dir, "config.json"))
|
|
if err != nil {
|
|
return copilotApp{}, false
|
|
}
|
|
var cfg struct {
|
|
Last *copilotCLIUser `json:"lastLoggedInUser"`
|
|
Users []copilotCLIUser `json:"loggedInUsers"`
|
|
Tokens map[string]string `json:"copilotTokens"`
|
|
}
|
|
if json.Unmarshal(jsonc.ToJSON(raw), &cfg) != nil {
|
|
return copilotApp{}, false
|
|
}
|
|
users := cfg.Users
|
|
if cfg.Last != nil {
|
|
users = append([]copilotCLIUser{*cfg.Last}, users...)
|
|
}
|
|
// a github.com account first, as before; then one an enterprise's
|
|
// `copilot login --host <name>.ghe.com` signed in (#723). A GitHub
|
|
// Enterprise Server's own host is left out: its API isn't GHE.com's.
|
|
for _, ghe := range []bool{false, true} {
|
|
for _, u := range users {
|
|
host, ok := copilotHostOf(u.Host)
|
|
if u.Login == "" || !ok || (host != "") != ghe {
|
|
continue
|
|
}
|
|
key := "https://github.com:" + u.Login
|
|
if ghe {
|
|
key = strings.TrimSuffix(u.Host, "/") + ":" + u.Login
|
|
}
|
|
tok := cfg.Tokens[key]
|
|
if tok == "" && secret != nil {
|
|
tok = secret(key)
|
|
}
|
|
if tok != "" {
|
|
return copilotApp{User: u.Login, Token: tok, Host: host, cli: true}, true
|
|
}
|
|
}
|
|
}
|
|
return copilotApp{}, false
|
|
}
|
|
|
|
var copilotSecrets struct {
|
|
sync.Mutex
|
|
at map[string]time.Time
|
|
tok map[string]string
|
|
}
|
|
|
|
// copilotCLISecret reads the CLI's token from the keychain, once in a
|
|
// while: each read may ask the user to allow it.
|
|
var copilotCLISecret = func(account string) string {
|
|
if settings.AccountDiscoveryOff() {
|
|
return ""
|
|
}
|
|
c := &copilotSecrets
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if c.at == nil {
|
|
c.at, c.tok = map[string]time.Time{}, map[string]string{}
|
|
}
|
|
ttl := time.Hour
|
|
if c.tok[account] == "" {
|
|
ttl = 10 * time.Minute // denied or absent; don't ask again at once
|
|
}
|
|
if at, ok := c.at[account]; ok && time.Since(at) < ttl {
|
|
return c.tok[account]
|
|
}
|
|
var out []byte
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
out, _ = proc.Command("security", "find-generic-password", "-s", "copilot-cli", "-a", account, "-w").Output()
|
|
case "linux":
|
|
if p, err := exec.LookPath("secret-tool"); err == nil {
|
|
out = []byte(copilotSecretTool(p, account))
|
|
}
|
|
}
|
|
c.at[account], c.tok[account] = time.Now(), string(bytes.TrimSpace(out))
|
|
return c.tok[account]
|
|
}
|
|
|
|
// copilotSecretWait bounds one secret-tool lookup: a keyring that waits on
|
|
// a prompt nobody answers would hold the sign-in that asked (#723).
|
|
var copilotSecretWait = 5 * time.Second
|
|
|
|
// copilotSecretTool is the CLI's token in the Secret Service, as
|
|
// secret-tool (at path) finds it. The CLI before 1.0 (keytar, libsecret)
|
|
// keyed it by service and account; 1.0's own keyring store
|
|
// (zbus-secret-service-keyring-store) by service and username, so both
|
|
// are asked (#723).
|
|
func copilotSecretTool(path, account string) string {
|
|
for _, attr := range []string{"account", "username"} {
|
|
ctx, cancel := context.WithTimeout(context.Background(), copilotSecretWait)
|
|
cmd := proc.CommandContext(ctx, path, "lookup", "service", "copilot-cli", attr, account)
|
|
cmd.WaitDelay = time.Second
|
|
out, _ := cmd.Output()
|
|
cancel()
|
|
if tok := strings.TrimSpace(string(out)); tok != "" {
|
|
return tok
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// copilotDirect is the session a CLI token makes: the token itself, at the
|
|
// API its account is served from.
|
|
func copilotDirect(ctx context.Context, app copilotApp) (copilotSession, error) {
|
|
github := app.Token
|
|
copilotMu.Lock()
|
|
defer copilotMu.Unlock()
|
|
if s, ok := copilotSessions[github]; ok && time.Until(time.Unix(s.ExpiresAt, 0)) > 2*time.Minute {
|
|
return s, nil
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, copilotUserURL(app.Host), nil)
|
|
if err != nil {
|
|
return copilotSession{}, err
|
|
}
|
|
req.Header.Set("Authorization", "token "+github)
|
|
req.Header.Set("Accept", "application/json")
|
|
res, err := Keyed(http.DefaultClient).Do(req)
|
|
if err != nil {
|
|
return copilotSession{}, errors.New("Copilot sign-in: " + err.Error())
|
|
}
|
|
defer res.Body.Close()
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
|
|
var u struct {
|
|
Endpoints struct {
|
|
API string `json:"api"`
|
|
} `json:"endpoints"`
|
|
}
|
|
if res.StatusCode != 200 || json.Unmarshal(b, &u) != nil {
|
|
// 401 is GitHub refusing the token itself; a 403 here can be an
|
|
// account without Copilot, and a 5xx is GitHub's own trouble
|
|
if res.StatusCode == http.StatusUnauthorized {
|
|
copilotRefuse(github, res.StatusCode)
|
|
}
|
|
return copilotSession{}, errors.New("the Copilot CLI's sign-in was refused (" + APIError(b, res.Status) + "); run `copilot login` again")
|
|
}
|
|
copilotRefusals.Lock()
|
|
delete(copilotRefusals.at, github)
|
|
delete(copilotRefusals.status, github)
|
|
copilotRefusals.Unlock()
|
|
s := copilotSession{Token: github, ExpiresAt: time.Now().Add(30 * time.Minute).Unix(), direct: true}
|
|
s.Endpoints.API = u.Endpoints.API
|
|
copilotSessions[github] = s
|
|
return s, nil
|
|
}
|