Files
magpie/internal/provider/factory_client_test.go
T
yetone e413dd8445 factory: another agent's request to Factory's GPT, Grok and open models opens as Droid's does (#506 aohun: Codex on glm-5.3-flash through a Factory account, 403 Forbidden; the user: 只要不是 claude code,都可以尝试伪装一下) — in #242 the same account's GPT, Grok and GLM answered Droid through magpie every time and refused Grok Build and Claude Code every time, on the same models, efforts, endpoint and headers, the body the only difference, and glm-5.3-flash is a model Factory serves (droid 0.231.0's registry: provider factory, generic chat completions, fireworks/baseten/databricks), sent where and as droid sends it. droid 0.231.0 (its binary, read, not run) opens every system prompt with YOU_ARE_DROID_SYSTEM_PROMPT, "You are Droid, an AI software engineering agent built by Factory.": its system blocks [that line, the agent prompt, …] go as Responses' instructions joined with "\n", and on chat completions as one system message put first, joined the same way; its small calls (the latency ping) send the line alone. A request on /api/llm/o now does too: Responses' instructions, or the first system message of chat completions (text parts joined as droid joins them), open with the line and "\n" before the agent's own; with none, the line alone goes first. A request that already opens with it (droid's own) goes on byte for byte, as does everything on /api/llm/a (Claude, MiniMax M2.7), which is left as the agent sent it. The 403 text now says magpie opens other agents' GPT, Grok and open-model requests as Droid's but Factory may still tell them apart, that Claude models from other agents are refused, to use the model from Droid, and that the org's policy or plan is left when Droid is refused too. GLM-5.3-Flash takes images (droid 0.231.0 says images: true; the rest of the table matches), and the requests say droid 0.231.0. Not tried with a real Factory account: whether the line is all Factory checks is not known. TestFactoryOpensAsDroid (Codex-shaped chat and Responses, Claude Code's system parts, no system, droid's own chat and Responses unchanged byte for byte, Anthropic's Messages unchanged) and TestFactoryGLMFlashImages fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./internal/gateway/ ./internal/provider/ pass.
2026-10-02 13:03:41 +08:00

141 lines
5.9 KiB
Go

package provider
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strings"
"sync"
"testing"
"time"
)
// #506: Codex on glm-5.3-flash through a Factory account got 403, as Grok
// Build's and Claude Code's requests did in #242, where Droid's own through
// magpie went through on the same models, endpoint and headers. droid
// 0.231.0 opens every system prompt with its line, sent as Responses'
// instructions and chat completions' first system message, each joined
// with "\n". Another agent's request to /api/llm/o now opens so, its own
// prompt after; droid's goes on byte for byte, and so does anything on
// Anthropic's Messages.
func TestFactoryOpensAsDroid(t *testing.T) {
signIn(t)
tok := factoryToken(map[string]any{"sub": "user_d", "org_id": "org_D"})
var mu sync.Mutex
got := map[string][]byte{}
factorySite(t, func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
if int64(len(b)) != r.ContentLength {
t.Errorf("%s: read %d bytes, Content-Length %d", r.URL.Path, len(b), r.ContentLength)
}
mu.Lock()
got[r.URL.Path] = b
mu.Unlock()
if r.URL.Path == "/api/cli/whoami" {
factoryJSON(w, 200, map[string]any{"userId": "user_d", "orgId": "fac_D"})
return
}
io.WriteString(w, `{"id":"ok"}`)
})
auth, _ := json.Marshal(map[string]any{"accessToken": tok, "refreshToken": "r1",
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "orgId": "org_D", "activeOrganizationId": "fac_D", "email": "d@example.com"})
if err := addSideLogin(savedLogin{Agent: "factory", User: "d@example.com", Auth: auth}, "", func(savedLogin) {}); err != nil {
t.Fatal(err)
}
p, ok := find(Accounts(), "factory")
if !ok {
t.Fatal("no factory provider")
}
send := func(url string, proto Protocol, body string) []byte {
t.Helper()
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
req.ContentLength = int64(len(body))
if err := p.Sign(context.Background(), req, proto, []byte(body)); err != nil {
t.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
u, _ := http.NewRequest("POST", url, nil)
mu.Lock()
defer mu.Unlock()
return got[u.URL.Path]
}
chat, responses, messages := p.Chat+"/chat/completions", p.Responses+"/responses", p.Anthropic+"/v1/messages"
type msg struct {
Role string `json:"role"`
Content any `json:"content"`
}
read := func(b []byte) (out struct {
Model string `json:"model"`
Instructions *string `json:"instructions"`
Effort string `json:"reasoning_effort"`
Stream bool `json:"stream"`
Messages []msg `json:"messages"`
Input any `json:"input"`
}) {
t.Helper()
if err := json.Unmarshal(b, &out); err != nil {
t.Fatalf("sent %s: %v", b, err)
}
return out
}
// Codex on glm-5.3-flash, its Responses request made chat completions:
// its instructions as the first system message
b := read(send(chat, Chat, `{"model":"glm-5.3-flash","messages":[{"role":"system","content":"You are Codex, a coding agent."},{"role":"user","content":"hi <b> & co"}],"stream":true,"reasoning_effort":"high"}`))
if len(b.Messages) != 2 || b.Messages[0].Role != "system" || b.Messages[0].Content != factoryDroidLine+"\nYou are Codex, a coding agent." ||
b.Messages[1].Content != "hi <b> & co" || b.Model != "glm-5.3-flash" || b.Effort != "high" || !b.Stream {
t.Errorf("codex on chat: %+v", b)
}
// Claude Code's system blocks, made chat completions as parts: joined
b = read(send(chat, Chat, `{"model":"kimi-k3","messages":[{"role":"system","content":[{"type":"text","text":"You are Claude Code."},{"type":"text","text":"Be brief."}]},{"role":"user","content":"hi"}]}`))
if len(b.Messages) != 2 || b.Messages[0].Content != factoryDroidLine+"\nYou are Claude Code.\nBe brief." {
t.Errorf("claude code on chat: %+v", b)
}
// no system prompt: droid's line alone, before the rest
b = read(send(chat, Chat, `{"model":"glm-5.3","messages":[{"role":"user","content":"hi"}]}`))
if len(b.Messages) != 2 || b.Messages[0].Role != "system" || b.Messages[0].Content != factoryDroidLine || b.Messages[1].Role != "user" {
t.Errorf("no system on chat: %+v", b)
}
// Codex on GPT, Responses as it is: the instructions open with the line
b = read(send(responses, Responses, `{"model":"gpt-5.5","instructions":"You are Codex, a coding agent.","input":[{"role":"user","content":"hi"}],"stream":true}`))
if b.Instructions == nil || *b.Instructions != factoryDroidLine+"\nYou are Codex, a coding agent." || len(b.Input.([]any)) != 1 || !b.Stream {
t.Errorf("codex on responses: %+v", b)
}
b = read(send(responses, Responses, `{"model":"grok-4.7","input":"hi"}`))
if b.Instructions == nil || *b.Instructions != factoryDroidLine {
t.Errorf("no instructions on responses: %+v", b)
}
// droid's own requests, and Anthropic's Messages, go on byte for byte
for _, c := range []struct {
url string
proto Protocol
body string
}{
{chat, Chat, `{"model":"glm-5.3-flash","messages":[{"role":"system","content":"` + factoryDroidLine + `\nYou work in the user's terminal."},{"role":"user","content":"hi"}],"stream":true,"n":1.0}`},
{responses, Responses, `{"model":"gpt-5.5","input":[],"store":false,"instructions":"` + factoryDroidLine + `\nYou work in the user's terminal.","stream":true}`},
{messages, Anthropic, `{"model":"claude-opus-5-5","system":"You are Claude Code.","messages":[{"role":"user","content":"hi"}]}`},
{messages, Anthropic, `{"model":"minimax-m2.7","messages":[{"role":"user","content":"hi"}]}`},
} {
if sent := send(c.url, c.proto, c.body); !bytes.Equal(sent, []byte(c.body)) {
t.Errorf("changed:\n%s\nsent:\n%s", c.body, sent)
}
}
}
// droid 0.231.0's registry: GLM-5.3-Flash takes images.
func TestFactoryGLMFlashImages(t *testing.T) {
if m, ok := factoryModelOf("glm-5.3-flash"); !ok || !m.images {
t.Errorf("glm-5.3-flash: %+v", m)
}
if factoryVersion != "0.231.0" {
t.Errorf("factoryVersion %s", factoryVersion)
}
}