Files
magpie/internal/agent/antigravity_test.go
T
yetone 7c712847b7 agent: Antigravity's desktop app is a row, wired through its gateway setting (ryougi_hachi on X)
Antigravity (Google's desktop app, 2.15 and later; not Antigravity IDE)
takes a gateway from ~/.antigravity/settings.json: Google's Antigravity
Enterprise Gateway, documented at
docs.cloud.google.com/gemini/enterprise/docs/ai-developer-tools/connect-enterprise-gateway.
magpie writes gateway {enabled, url: magpie's, apiKey: magpie-antigravity,
wireProtocol: openai} and models.defaultModelId, and takes out models.list
(it would narrow the picker). The user's gateway, default model and list
are stashed and put back on disconnect or when a model of their own is
picked; caCertPath and the rest of the file are untouched. Sync keeps what
the user added to magpie's block, and so does a set again (a moved port,
Reconnect). Check speaks when admin_settings.json or AGY_LLM_GATEWAY_URL
wins over the file, and when the block's url or key is no longer magpie's
(wiringOff), so drift and a port move see it. AGY_LLM_GATEWAY_URL joins
agentenv.Vars, cleared in every sandbox. Found by its bundle
(com.google.antigravity, 2.15+) or the file its backend writes
(~/.gemini/antigravity/agyhub_summaries_proto.pb); ~/.antigravity alone is
the IDE's and agy's too. The notices are translated (zh, zh-TW, ja, de).
`magpie antigravity` now names the app; agy stays `magpie agy` /
`antigravity-cli`.

Run (worktree base 647323409, on mote):
- Real app backend in a sandbox HOME: Antigravity.app 2.28.2's
  language_server (hub), wired by this branch's magpie built from the
  worktree (`magpie antigravity magpie/rec/pro`, MAGPIE_ADDR on a port of
  its own, a recording upstream as a custom provider).
  GetCascadeModelConfigData listed rec/pro from magpie's /v1/models; a
  cascade ("Say hi") went app -> magpie -> upstream as
  /v1/chat/completions (stream, 12 tools) and its answer came back into
  the trajectory. `magpie usage` counted both calls to Antigravity.
  `magpie antigravity default` gave back the user's genai gateway, model
  and list byte for byte (JSON-equal).
- Before that the same file was checked with the real agy CLI 1.4.12.
- go vet (darwin, GOOS=linux, GOOS=windows) for internal/agent; gofmt.
- go test -tags nogui -count=1 ./internal/agent ./internal/agentenv
  ./internal/gui ./internal/usage ./internal/cli: all ok at this tree.
  The first run was red in four guard tests (TestFolderVarsAreListed,
  TestDriftUnwiredEveryAgent/antigravity, TestReverseProxyIsStillMagpie,
  TestPortMovesEveryAgent): Check had no wiringOff and the variable
  wasn't listed. Both fixed here.
- GUI (node --test, Chromium + WebKit): i18n-keys, agent-notice-lang,
  about-notices, 46 of 46 ok.

Broken by hand: renamed the gateway's stash key and dropped the
models.list deletion: TestAntigravityGateway, TestAntigravityGatewayFromNothing
and TestAntigravityOwnModelBack each went red; the desktopApps entry
missing made TestAntigravityDetected red; writing magpie's block whole on a set
again made TestAntigravitySync red (the user's customHeaders lost). The new tests are red on the
old code (no such agent).

Not tried: the Electron window itself (the backend was driven over its
RPCs, headless); the app on Windows or Linux (its install paths there
aren't known; the backend's file finds it); whether Google sign-in comes
back on its own after disconnect; the full ./... suite, because another
session's full suite was running on mote.
2026-11-01 16:49:55 +08:00

281 lines
9.4 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// antigravityUser is ~/.antigravity/settings.json with a gateway of the
// user's own (Google's example, on genai), a default model, a model list
// and a key magpie never writes.
const antigravityUser = `{
"gateway": {
"enabled": true,
"url": "https://gateway.cymbal.com",
"apiKey": "GATEWAY_API_KEY",
"wireProtocol": "genai",
"customHeaders": {"X-Cost-Center": "Platform-Engineering"}
},
"caCertPath": "/etc/ssl/certs/cymbal-root-ca.pem",
"models": {
"defaultModelId": "gemini-3.8-flash",
"list": [{"modelId": "gemini-3.8-flash"}]
}
}
`
func antigravityHome(t *testing.T) string {
t.Helper()
home := syncHome(t)
t.Setenv("AGY_LLM_GATEWAY_URL", "")
if err := provider.Save(provider.Provider{ID: "anth", Name: "Anth", Key: "k", Anthropic: "http://127.0.0.1:1", Models: []string{"claude-sonnet-5"}}); err != nil {
t.Fatal(err)
}
return home
}
func antigravityFile(t *testing.T, path string) map[string]json.RawMessage {
t.Helper()
var m map[string]json.RawMessage
if err := json.Unmarshal([]byte(readFile(path)), &m); err != nil {
t.Fatalf("%v\n%s", err, readFile(path))
}
return m
}
// Antigravity's app takes a gateway from ~/.antigravity/settings.json
// (Google's Antigravity Enterprise Gateway, ryougi_hachi on X): magpie
// writes its own, on the openai wire protocol with a key naming the app,
// and the model it starts on; the user's own gateway, default model and
// model list come back when it is turned off, and what else the file holds
// is never touched.
func TestAntigravityGateway(t *testing.T) {
home := antigravityHome(t)
path := filepath.Join(home, ".antigravity", "settings.json")
if !slices.ContainsFunc(All(), func(x *Agent) bool { return x.ID == "antigravity" && x.Name == "Antigravity" }) {
t.Fatal("Antigravity is not among the agents")
}
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(antigravityUser), 0o600)
a := antigravity(home)
f := a.Field("model")
if f == nil {
t.Fatal("no model field")
}
if got := f.Get(); got != "gemini-3.8-flash" {
t.Fatalf("get: %q", got)
}
if a.Notice() != "" {
t.Errorf("a notice while on the user's own gateway: %q", a.Notice())
}
if err := f.Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
if got := f.Get(); got != "magpie/anth/claude-sonnet-5" {
t.Fatalf("get after set: %q", got)
}
file := antigravityFile(t, path)
var gw map[string]any
json.Unmarshal(file["gateway"], &gw)
want := map[string]any{"enabled": true, "url": gateway.URL(), "apiKey": agentKey("antigravity"), "wireProtocol": "openai"}
if !sameJSON(string(file["gateway"]), want) {
t.Errorf("gateway: %s, want %v", file["gateway"], want)
}
if !strings.HasSuffix(gw["apiKey"].(string), "-antigravity") {
t.Errorf("the key doesn't name the app: %v", gw["apiKey"])
}
if !sameJSON(string(file["models"]), map[string]any{"defaultModelId": "anth/claude-sonnet-5"}) {
t.Errorf("models: %s (the user's list would narrow the picker)", file["models"])
}
if string(file["caCertPath"]) != `"/etc/ssl/certs/cymbal-root-ca.pem"` {
t.Errorf("caCertPath: %s", file["caCertPath"])
}
if a.Notice() == "" {
t.Error("no notice to restart the app")
}
opts := f.Options(map[string]string{"model": f.Get()})
if !slices.ContainsFunc(opts, func(o Option) bool { return o.Value == "magpie/anth/claude-sonnet-5" }) {
t.Errorf("magpie's model not offered: %v", opts)
}
// another magpie model, then off: the user's as they were
if err := f.Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if !sameJSON(readFile(path), json.RawMessage(antigravityUser)) {
t.Errorf("not the user's file again:\n%s", readFile(path))
}
if got := f.Get(); got != "gemini-3.8-flash" {
t.Errorf("get after off: %q", got)
}
if a.Notice() != "" {
t.Errorf("a notice after off: %q", a.Notice())
}
}
// With no file of the user's, turning magpie on and off leaves nothing of
// it: no gateway, no models.
func TestAntigravityGatewayFromNothing(t *testing.T) {
home := antigravityHome(t)
path := filepath.Join(home, ".antigravity", "settings.json")
f := antigravity(home).Field("model")
if f.Get() != "" {
t.Fatalf("get: %q", f.Get())
}
if err := f.Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
if got := f.Get(); got != "magpie/anth/claude-sonnet-5" {
t.Fatalf("get: %q", got)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if !sameJSON(readFile(path), map[string]any{}) {
t.Errorf("left behind:\n%s", readFile(path))
}
}
// Picking a model of the user's own while on magpie gives their gateway
// back and sets that model.
func TestAntigravityOwnModelBack(t *testing.T) {
home := antigravityHome(t)
path := filepath.Join(home, ".antigravity", "settings.json")
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(antigravityUser), 0o600)
f := antigravity(home).Field("model")
if err := f.Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
if err := f.Set("gemini-3.1-pro-preview"); err != nil {
t.Fatal(err)
}
file := antigravityFile(t, path)
var user map[string]json.RawMessage
json.Unmarshal([]byte(antigravityUser), &user)
if !sameJSON(string(file["gateway"]), user["gateway"]) {
t.Errorf("gateway: %s", file["gateway"])
}
if got := f.Get(); got != "gemini-3.1-pro-preview" {
t.Errorf("get: %q", got)
}
if _, ok := stashShared()["antigravity:"+path+":defaultModelId"]; ok {
t.Error("the stashed model was kept after another was picked")
}
}
// A sync brings the gateway's address and key up to date and keeps what
// the user added to the block; a gateway that isn't magpie's is left alone.
func TestAntigravitySync(t *testing.T) {
home := antigravityHome(t)
path := filepath.Join(home, ".antigravity", "settings.json")
a := antigravity(home)
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(antigravityUser), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if !sameJSON(readFile(path), json.RawMessage(antigravityUser)) {
t.Fatalf("the user's gateway synced:\n%s", readFile(path))
}
if err := a.Field("model").Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
b := strings.Replace(readFile(path), gateway.URL(), "http://127.0.0.1:1", 1)
b = strings.Replace(b, `"wireProtocol"`, `"customHeaders": {"X-Mine": "1"}, "wireProtocol"`, 1)
os.WriteFile(path, []byte(b), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
file := antigravityFile(t, path)
var gw map[string]any
json.Unmarshal(file["gateway"], &gw)
if gw["url"] != gateway.URL() {
t.Errorf("url: %v", gw["url"])
}
if h, _ := gw["customHeaders"].(map[string]any); h["X-Mine"] != "1" {
t.Errorf("the user's headers lost: %s", file["gateway"])
}
// set again (a moved port, Reconnect): the headers stay too
if err := a.Field("model").Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
file = antigravityFile(t, path)
gw = nil
json.Unmarshal(file["gateway"], &gw)
if h, _ := gw["customHeaders"].(map[string]any); h["X-Mine"] != "1" {
t.Errorf("the user's headers lost on a set again: %s", file["gateway"])
}
}
// An organisation's admin_settings.json, or AGY_LLM_GATEWAY_URL, wins over
// the user's file, so the row says it doesn't reach magpie.
func TestAntigravityAdminWins(t *testing.T) {
home := antigravityHome(t)
a := antigravity(home)
if err := a.Field("model").Set("magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
if c := a.Check(); c != "" {
t.Fatalf("check: %q", c)
}
admin := filepath.Join(t.TempDir(), "admin_settings.json")
was := antigravityAdminPath
antigravityAdminPath = func() string { return admin }
t.Cleanup(func() { antigravityAdminPath = was })
os.WriteFile(admin, []byte(`{"gateway":{"url":"https://gateway.cymbal.com"}}`), 0o644)
if c := a.Check(); !strings.Contains(c, "admin_settings.json") {
t.Errorf("check with an admin gateway: %q", c)
}
os.Remove(admin)
t.Setenv("AGY_LLM_GATEWAY_URL", "https://gateway.cymbal.com")
if c := a.Check(); !strings.Contains(c, "AGY_LLM_GATEWAY_URL") {
t.Errorf("check with the variable: %q", c)
}
}
// ~/.antigravity is Antigravity IDE's and agy's too, so it isn't the app:
// the app's bundle, 2.15 or later, or the file its own backend writes is.
func TestAntigravityDetected(t *testing.T) {
home := antigravityHome(t)
a := antigravity(home)
os.MkdirAll(filepath.Join(home, ".antigravity", "extensions"), 0o755)
if a.Detected() {
t.Fatal("Antigravity IDE's folder taken for the app")
}
if runtime.GOOS == "darwin" {
plist := func(v string) {
c := filepath.Join(home, "Applications", "Antigravity.app", "Contents")
os.MkdirAll(c, 0o755)
os.WriteFile(filepath.Join(c, "Info.plist"), []byte(`<plist><dict><key>CFBundleIdentifier</key><string>com.google.antigravity</string><key>CFBundleShortVersionString</key><string>`+v+`</string></dict></plist>`), 0o644)
}
plist("1.11.3")
if a.Detected() {
t.Fatal("an app before 2.15, which takes no gateway, taken for it")
}
plist("2.28.2")
if !a.Detected() {
t.Fatal("Antigravity 2.28.2 not found")
}
os.RemoveAll(filepath.Join(home, "Applications"))
}
hub := filepath.Join(home, ".gemini", "antigravity")
os.MkdirAll(hub, 0o755)
os.WriteFile(filepath.Join(hub, "agyhub_summaries_proto.pb"), nil, 0o644)
if !a.Detected() {
t.Fatal("not found by its backend's file")
}
}