Files
magpie/internal/gateway/antigravity_cloak_test.go
T
yetone 1885c607e2 gateway: Antigravity answers Codex's switched threads and Hermes' old persona with no setup
Antigravity answers some agents' system prompts with a fake 429 "Resource
has been exhausted", whatever quota is left. Jodiezzzzzz on X (10-27): a
Codex thread begun on GPT-5.x and switched to an Antigravity model got 429
again and again. zgoodz on X: "antigravity在Hermes里一样429". v0.1.1625
told the Codex case as a 400 with "start a new chat", which isn't zero
setup.

What Google matches, each tried on a real Antigravity account on 10-27
(in the system instruction, any case):
- "x-anthropic-billing-header:" (Claude Code)
- "built on Anthropic's Claude Agent SDK" ("running within the Claude
  Agent SDK" goes through)
- "You are Codex, an agent based on GPT-5" / "..., a coding agent based
  on GPT-5"
- new: "Hermes Agent, an intelligent AI assistant created by Nous
  Research", Hermes' earlier default SOUL.md, kept by anyone who edited
  their SOUL.md. Hermes' current default ("You are Hermes Agent, built by
  Nous Research") goes through.
Each goes through with a zero-width space after its first letter.
"Y​ou are Hermes Agent, an intelligent..." still fails, so the
break goes inside the matched phrase itself.

Now codeAssistBody puts that zero-width space into each matched line
(antigravityCloak), in the system instruction only, and only for an
Antigravity account. Gemini CLI's account on the same envelope, and
every other provider and protocol, get the prompt as the agent wrote
it. The text added names nothing; it is one U+200B per line.
`magpie provider set antigravity prompt=as-written` (PromptAsWritten,
kept through Save/withPicks and the GUI's save) turns it off. Then the
v0.1.1625/#2156 fallback stands: a 400 with a hint (now one for Hermes
too, in the Routing page's five languages) and Antigravity skipped for
6h in a group.

Default-on, as the community antigravity-cloak middleware's README
says, changes what Google is sent so a request it means to refuse goes
through. That may be against Antigravity's terms. The owner asked for
it ("破解过 antigravity 提示词让其可以在别的 agent 中使用"), and it is
scoped to Antigravity accounts with an off switch.

Run at this tree (rebased on origin/main):
- go test -count=1 -timeout 45m ./internal/gateway/ ./internal/provider/
  ./internal/cli/ ./internal/gui/: gateway, cli and gui ok; provider
  died in TestUnreadLoginsWriteErrorsKeepCredentials ("close of synctest
  channel from outside bubble" in filememo.Forget), #3632, red on main
  since 091153bf; with -skip of that test, provider ok.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. go build: ok.
- GUI (node --test, Playwright 1.62, chromium and webkit):
  routing-ag-turned-away, i18n-keys, gui-ja, gui-de, gui-zh-tw,
  routing-js, panel-routing: 45 pass, 0 fail.
- Broken by hand: with the cloak skipped, TestAntigravityAnswersHermes
  AndCodexByDefault and TestOnlyAntigravityIsSentThePromptBrokenUp go
  red; without withPicks' line, "prompt=as-written isn't kept".
- Real account, sandbox HOME with only the Antigravity login copied,
  this build on 127.0.0.1:3496:
  - real Hermes Agent (AIAgent, sandbox SOUL.md = the old persona plus
    one line): "OK", 200. With prompt=as-written: 400 with the Hermes
    hint. Back on default: 200.
  - real Codex 0.162.1: a thread begun on gpt-5.5 (against a local
    fake Responses server), then `codex exec resume <id> -m
    antigravity/gemini-3-flash` through this build: "OK". With
    prompt=as-written: 400 with the Codex hint.
  - a synthetic switched Codex Responses thread, with and without
    web_search: 200.
  - opencode, droid, kimi, pi, gemini-cli, qwen, crush, goose, amp and
    openclaw identity sentences: never refused, as written.
Not tried: Claude models on Antigravity (the sandbox account's Claude
quota is spent, so the Claude Code lines were checked on Gemini 3
Flash only); other agents' whole prompts, only their identity lines;
the Windows box; the whole GUI suite.

The plugins repo's antigravity-cloak 0.1.2 adds the Hermes line for
magpies before this one.
2026-10-27 04:16:20 +08:00

221 lines
9.0 KiB
Go
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// hermesOldSystem is Hermes Agent's earlier default persona, the SOUL.md it
// seeded until it replaced it, which a SOUL.md the user edited keeps.
const hermesOldSystem = "You are Hermes Agent, an intelligent AI assistant created by Nous Research. You are helpful, knowledgeable, and direct. You assist users with a wide range of tasks including answering questions, writing and editing code, analyzing information, creative work, and executing actions via your tools."
// googleTurnsAway is what a real Antigravity account turned away, each tried
// on one on 10-27: the line as written, in the system instruction, in any
// case. Broken up by a zero-width space after the first letter, each went
// through.
var googleTurnsAway = []string{
"x-anthropic-billing-header:",
"built on anthropic's claude agent sdk",
"you are codex, an agent based on gpt-5",
"you are codex, a coding agent based on gpt-5",
"hermes agent, an intelligent ai assistant created by nous research",
}
func TestAntigravityCloakBreaksEveryLineItTurnsAway(t *testing.T) {
for _, system := range []string{
claudeCodeSystem,
"You are a Claude agent, built on Anthropic's Claude Agent SDK.",
codexGPT5System,
"You are Codex, a coding agent based on GPT-5. You and the user share the same workspace.",
hermesOldSystem,
strings.ToUpper(hermesOldSystem),
// twice, and after text whose lower case is longer than it is
"İstanbul ✓ " + codexGPT5System + "\n\n" + hermesOldSystem + "\n" + codexGPT5System,
} {
if !antigravityTurnsAway(system) {
t.Fatalf("%q isn't taken for what Antigravity turns away", system)
}
got := antigravityCloak(system)
if antigravityTurnsAway(got) {
t.Errorf("a line Antigravity turns away is still whole in %q", got)
}
if strings.ReplaceAll(got, zeroWidth, "") != system {
t.Errorf("more than zero-width spaces changed: %q", got)
}
if !strings.Contains(got, zeroWidth) {
t.Errorf("nothing broken up: %q", got)
}
// nothing else: each break sits after a line's first letter
low := strings.ToLower(strings.ReplaceAll(got, zeroWidth, "|"))
for _, line := range googleTurnsAway {
if strings.Contains(strings.ToLower(system), line) && !strings.Contains(low, line[:1]+"|"+line[1:]) {
t.Errorf("%q isn't broken after its first letter in %q", line, got)
}
}
if antigravityCloak(got) != got {
t.Errorf("a prompt broken up once is broken again")
}
}
// what Antigravity answers is sent as written, word for word
for _, system := range []string{
"",
"You are opencode, an interactive CLI tool.",
"You are Claude Code, Anthropic's official CLI for Claude, running within the Claude Agent SDK.",
"You are Codex, based on GPT-5. You are running as a coding agent in the Codex CLI on a user's computer.",
"You are Hermes Agent, built by Nous Research. Be direct.",
"You are Hermes Agent, an intelligent AI assistant created by Nous.",
} {
if got := antigravityCloak(system); got != system {
t.Errorf("%q went as %q", system, got)
}
}
}
func TestAntigravityHermesHint(t *testing.T) {
if antigravityHint(hermesOldSystem) != antigravityHermesHint {
t.Error("Hermes' old persona isn't told its own hint")
}
if antigravityHint(codexGPT5System) != antigravityCodexHint || antigravityHint(claudeCodeSystem) != antigravityTurnedAwayHint {
t.Error("the other prompts lost their hints")
}
}
// fakeGoogle answers as a real Antigravity account did on 10-27: the 429
// for a system instruction holding one of googleTurnsAway's lines whole,
// and a reply otherwise. sent is the system instruction it was last sent.
func fakeGoogle(sent *string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var env struct {
Request struct {
SystemInstruction struct {
Parts []struct {
Text string `json:"text"`
} `json:"parts"`
} `json:"systemInstruction"`
} `json:"request"`
}
b, _ := io.ReadAll(r.Body)
json.Unmarshal(b, &env)
*sent = ""
for _, p := range env.Request.SystemInstruction.Parts {
*sent += p.Text
}
low := strings.ToLower(*sent)
for _, line := range googleTurnsAway {
if strings.Contains(low, line) {
w.WriteHeader(http.StatusTooManyRequests)
io.WriteString(w, turnedAway)
return
}
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"from google"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":3}}}`))
}
}
// hermesTurn is a Hermes Agent turn on Antigravity's model, on the Chat
// Completions API it reaches magpie by, with system as its system prompt.
func hermesTurn(s *Server, system string) (int, string) {
body := `{"model":"antigravity/gemini-3-flash","stream":false,"messages":[{"role":"system","content":` + quote(system) + `},{"role":"user","content":"hi"}]}`
rec := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
r.Header.Set("User-Agent", "hermes-agent/0.12")
s.Handler().ServeHTTP(rec, r)
return rec.Code, rec.Body.String()
}
func codexAGPost(s *Server, body string) (int, string) {
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)))
return rec.Code, rec.Body.String()
}
// zgoodz on X (10-27): "antigravity在Hermes里一样429", and Jodiezzzzzz's Codex
// thread begun on a GPT model: both are answered by Antigravity with no setup,
// as the lines it turns away go broken up. Sent as written, the setting the
// user can turn on, they are refused as before, told why at a status the
// agent doesn't retry.
func TestAntigravityAnswersHermesAndCodexByDefault(t *testing.T) {
var sent string
s, _ := antigravityGroupFor(t, []string{"u@example.com"}, "gemini-3-flash", fakeGoogle(&sent))
if code, out := hermesTurn(s, hermesOldSystem); code != 200 || !strings.Contains(out, "from google") {
t.Fatalf("Hermes: %d %s", code, out)
}
if strings.ReplaceAll(sent, zeroWidth, "") != hermesOldSystem || sent == hermesOldSystem {
t.Fatalf("Hermes' prompt went as %q", sent)
}
for _, search := range []bool{false, true} {
if code, out := codexAGPost(s, codexAGTurn(true, search)); code != 200 || !strings.Contains(out, "from google") {
t.Fatalf("Codex's switched thread (search=%v): %d %s", search, code, out)
}
if !strings.Contains(sent, "Y"+zeroWidth+"ou are Codex, an agent based on GPT-5") || antigravityTurnsAway(sent) {
t.Fatalf("Codex's prompt went as %q", sent)
}
}
p, err := provider.Find("antigravity")
if err != nil {
t.Fatal(err)
}
p.PromptAsWritten = true
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
if p, _ := provider.Find("antigravity"); !p.PromptAsWritten {
t.Fatal("prompt=as-written isn't kept")
}
code, out := hermesTurn(s, hermesOldSystem)
if sent != hermesOldSystem {
t.Fatalf("sent as written, Hermes' prompt went as %q", sent)
}
if code != http.StatusBadRequest || !strings.Contains(out, antigravityHermesHint) {
t.Fatalf("Hermes, as written: %d %s", code, out)
}
code, out = codexAGPost(s, codexAGTurn(true, false))
if !strings.Contains(sent, codexGPT5System) {
t.Fatalf("sent as written, Codex's prompt went as %q", sent)
}
if code != http.StatusBadRequest || !strings.Contains(out, antigravityCodexHint) {
t.Fatalf("Codex, as written: %d %s", code, out)
}
}
// Only an Antigravity account's system instruction is broken up: Gemini
// CLI's own sign-in, on the same Code Assist envelope, and every other
// provider are sent the prompt as the agent wrote it.
func TestOnlyAntigravityIsSentThePromptBrokenUp(t *testing.T) {
r := &Request{System: hermesOldSystem + "\n" + codexGPT5System, Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}}}}
gemini := provider.Provider{ID: "gemini-cli", Account: &provider.Account{Agent: "gemini"}}
if b := string(codeAssistBody(gemini, r, "gemini-3-flash", nil)); !strings.Contains(b, "You are Hermes Agent, an intelligent") || strings.Contains(b, `​`) || strings.Contains(b, zeroWidth) {
t.Fatalf("Gemini CLI's account was sent %s", b)
}
ag := provider.Provider{ID: "antigravity", Account: &provider.Account{Agent: "antigravity"}}
if b := string(codeAssistBody(ag, r, "gemini-3-flash", nil)); strings.Contains(b, "You are Hermes Agent, an intelligent") {
t.Fatalf("Antigravity's account was sent %s", b)
}
if r.System != hermesOldSystem+"\n"+codexGPT5System {
t.Fatalf("the caller's request was changed: %q", r.System)
}
gem, err := buildGemini(r, "m")
if err != nil {
t.Fatal(err)
}
for name, b := range map[string][]byte{
"chat": buildChat(r, "m", "", false),
"anthropic": buildAnthropic(r, "m"),
"responses": buildResponses(r, "m", "", false),
"gemini": gem,
} {
if !strings.Contains(string(b), "You are Hermes Agent, an intelligent") || strings.Contains(string(b), zeroWidth) || strings.Contains(string(b), `\u200b`) {
t.Errorf("%s was sent the prompt broken up: %s", name, b)
}
}
}