Files
magpie/internal/provider/agy_move.go
T
yetone a922e3864e provider: moving Antigravity onto agy goes through Antigravity's proxy (huiqiang6 on X)
huiqiang6 on X: in the Docker image, moving Antigravity's accounts onto
"Antigravity (local agy CLI)" always fails at installing agy.

Reproduced with ghcr.io/yetone/magpie:latest (v0.1.4377) on
xiaomi-gpu's Docker, with the owner's real Antigravity account in
plugin-auth.json:
- plain network, amd64 and emulated arm64: the move works (26s, 39s);
  agy 1.5.10 installs into /config/data/magpie-agy-cli.
- Google unreachable directly, reachable only through a squid proxy set
  on Antigravity alone (providers.json proxy, as a user in China sets
  it): the move fails in 7s, every time, with "installing Antigravity
  CLI failed (couldn't reach Antigravity's release server: Unable to
  connect…); install it yourself with `curl … | bash`".

Cause: MoveToAgy ran agy's install and the accounts' import under
agy-cli's proxy, which a new agy-cli doesn't have, so both went direct
and never through the proxy Antigravity's requests used.

Now MoveToAgy first carries Antigravity's proxy onto agy-cli (its
provider proxy while agy-cli has no account and no proxy of its own;
each moving account's own, or Antigravity's, as an account proxy where
agy-cli would take another), then installs agy and imports through
agy-cli's. A proxy the user set on agy-cli is kept.

Run:
- go test ./internal/provider -run Agy on mote: ok. The two new proxy
  tests are red with the carry and Via lines taken out (installed
  through "", imported through "").
- A linux/amd64 nogui build of this tree in the same blocked container
  (fresh volume, same account, same proxy): the move gives
  kumw95ya25@gmail.com "added" in 26s, agy 1.5.10 installed,
  providers.json has agy-cli's proxy, and a chat request to
  agy-cli/gemini-3.8-flash answers "OK", squid logging the CONNECTs.
- gofmt -l; go vet GOOS=linux -tags nogui ./..., GOOS=windows ./...
- Full suite: not run before the push. mote and mica were each running
  another session's suite; it runs after, one per machine, and a red
  gets its own issue.

Not tried: native arm64 hardware with the proxy; the reporter's own
config (the tweet has no screenshot, so their proxy setup is inferred);
an agy-cli that already has accounts and no proxy (the move's import
then goes through agy-cli's, direct; the moving accounts still keep
their proxy afterwards). Moves that already failed simply work on retry;
none half-moved anything.
2026-11-17 21:43:12 +08:00

223 lines
6.3 KiB
Go

package provider
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"sort"
"strings"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// Antigravity's accounts, onto Antigravity (local agy CLI). The withdrawn
// Antigravity plugin (and the built-in before it) signed in with
// Antigravity's own Google OAuth client, the one agy's own sign-in uses
// (1071006060591-…), so agy takes those sign-ins as its own: agy-cli's
// auth.import gives each an agy home of its own and keeps it once agy runs
// with it there. The token goes nowhere it didn't go: agy sends it to
// Google, as Antigravity did (@tongzhenbao on X).
const (
agyCLIID = "agy-cli"
agyCLIPackage = "@magpie-community/opencode-agy-cli-auth"
// 0.1.68: auth.import takes the old plugin's sign-ins
agyCLIMin = "0.1.68"
)
// agyMoving are the Antigravity accounts agy-cli doesn't have yet, as the
// withdrawn plugin keeps them: its own sign-ins, then the built-in's that
// never moved onto it. One agy-cli has (by email) is left out.
func agyMoving() []map[string]any {
if settings.AccountDiscoveryOff() {
return nil
}
have := map[string]bool{}
for _, a := range plugin.Auths(agyCLIID) {
if who := agyWho(a); who != "" {
have[who] = true
}
}
seen := map[string]bool{}
var out []map[string]any
add := func(a map[string]any) {
rt, _, _ := strings.Cut(str(a["refresh"]), "|")
who := agyWho(a)
if a["type"] != "oauth" || rt == "" || seen[rt] || (who != "" && (have[who] || seen[who])) {
return
}
seen[rt] = true
if who != "" {
seen[who] = true
}
out = append(out, a)
}
auths := plugin.Auths("antigravity")
// the provider's first account first, then the rest by key
keys := make([]string, 0, len(auths))
for k := range auths {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keyBefore(keys[i], keys[j]) })
for _, k := range keys {
add(auths[k])
}
if mv := movers["antigravity"]; mv != nil && !Moved("antigravity") {
if accts, err := mv.out(); err == nil {
for _, m := range accts {
add(m.Auth)
}
}
}
return out
}
// keyBefore puts a provider's bare key (its first account) before its
// "#…" ones, the rest in order.
func keyBefore(a, b string) bool {
ah, bh := strings.Contains(a, "#"), strings.Contains(b, "#")
if ah != bh {
return !ah
}
return a < b
}
func agyWho(a map[string]any) string {
who := str(a["accountId"])
if who == "" {
if md, ok := a["metadata"].(map[string]any); ok {
who = str(md["email"])
}
}
if who == "" {
who = str(a["email"])
}
return strings.ToLower(strings.TrimSpace(who))
}
// AgyMoveUsers are the accounts MoveToAgy would bring in, by email ("" for
// one that doesn't say), for the Providers page's line.
func AgyMoveUsers() []string {
var out []string
for _, a := range agyMoving() {
out = append(out, agyWho(a))
}
return out
}
// MoveToAgy brings Antigravity's accounts into agy-cli: the plugin
// installed (or brought up to the version that takes them) and agy with
// it, then each account checked with agy and kept as agy-cli's. The old
// sign-ins are left as they are; one agy refuses is said, not moved.
func MoveToAgy(ctx context.Context) ([]ImportedAccount, error) {
accts := agyMoving()
if len(accts) == 0 {
return nil, errors.New("no Antigravity account is left to move")
}
if err := installPlugin(ctx, agyCLIPackage, agyCLIMin); err != nil {
return nil, installFailed(agyCLIPackage, err)
}
pps, err := pluginProviders(ctx)
if err != nil {
return nil, err
}
found := false
for _, p := range pps {
if p.ID == agyCLIID {
found = p.Importable
}
}
if !found {
return nil, fmt.Errorf("%s takes no Antigravity accounts", agyCLIPackage)
}
forgetAccountCaches()
// agy is downloaded from Google and checks each account with Google,
// so it goes the way Antigravity's requests went: where Google is
// reached only through Antigravity's own proxy (magpie in a container
// in China), going direct failed the install every time (huiqiang6 on
// X). agy-cli keeps that proxy for its requests after the move too.
if err := carryAgyProxy(accts); err != nil {
return nil, err
}
ctx = Via(ctx, PluginID(agyCLIID))
if agyCLIMissing(agyCLIID) != "" {
if err := installAgy(ctx, agyCLIID); err != nil {
return nil, err
}
}
text, err := json.Marshal(accts)
if err != nil {
return nil, err
}
return importPlugin(ctx, PluginID(agyCLIID), []string{string(text)})
}
// carryAgyProxy gives agy-cli the proxy Antigravity's requests went
// through: Antigravity's own, when agy-cli has no account yet and no proxy
// of its own; and each moving account's, where agy-cli would take another
// for it and has none of the account's own. A proxy the user chose in
// agy-cli is kept.
func carryAgyProxy(accts []map[string]any) error {
from, ok := agyProxyFrom()
if !ok {
return nil
}
id := PluginID(agyCLIID)
f, err := read()
if err != nil {
return err
}
i := slices.IndexFunc(f.Providers, func(p Provider) bool { return p.ID == id })
if i < 0 {
f.Providers = append(f.Providers, Provider{ID: id})
i = len(f.Providers) - 1
}
p := &f.Providers[i]
changed := false
if p.Proxy == "" && from.Proxy != "" && len(plugin.Auths(agyCLIID)) == 0 {
p.Proxy, changed = from.Proxy, true
}
own := maps.Clone(p.AccountProxies)
if own == nil {
own = map[string]string{}
}
for _, a := range accts {
k := accountKey(agyWho(a))
want := from.AccountProxies[k]
if want == "" {
want = from.Proxy
}
if k != "" && want != "" && want != p.Proxy && own[k] == "" {
own[k], changed = want, true
}
}
if !changed {
return nil
}
p.AccountProxies = normalAccountProxies(own)
return store(f)
}
// agyProxyFrom is where Antigravity's proxy is kept: under the id its
// accounts were served by (the plugin's own, "antigravity-plugin", while
// the built-in was beside it), the first of them that has one.
func agyProxyFrom() (Provider, bool) {
for _, id := range []string{PluginID("antigravity"), "antigravity", "antigravity-plugin"} {
if p, ok := storedPicks(id); ok && (p.Proxy != "" || len(p.AccountProxies) > 0) {
return p, true
}
}
return Provider{}, false
}
// agyCLIMissing, installAgy and importPlugin are PluginCLIMissing,
// InstallPluginCLI and ImportPluginFiles, for tests to stand in for.
var (
agyCLIMissing = PluginCLIMissing
installAgy = InstallPluginCLI
importPlugin = ImportPluginFiles
)