Files
magpie/internal/agent/goose_test.go
T
yetone 2eb9266395 goose: runs on magpie as a custom provider that hands Goose each model's window and says which models Goose can send a thinking level (wani on Discord: goose gui不支持设置推理等级,而且上下文也不对)
magpie never wired Goose to its gateway: its model field offered only Goose's own providers, so a user had to add magpie by hand as a custom provider. Goose then treats every declared model as one that doesn't think (ModelInfo.reasoning defaults to false). With no context_limit, it guesses each window from its own registry by the model's name, or falls back to 128K. Goose's model field now offers magpie's models. Picking one writes custom_providers/magpie.json next to Goose's config.yaml (~/.config/goose, or %APPDATA%\Block\goose\config on Windows), then sets GOOSE_PROVIDER=magpie and GOOSE_MODEL. The file holds only keys Goose reads, from DeclarativeProviderConfig (crates/goose-providers/src/declarative.rs) and ModelInfo (crates/goose-provider-types/src/base.rs):
- engine openai, base_url set to the gateway's /v1, Authorization: Bearer magpie as a header, api_key_env "" and requires_auth false;
- dynamic_models false, so Goose uses this list rather than /v1/models, which has no windows;
- skip_canonical_filtering, so models Goose's registry doesn't know stay in the list;
- for each model, its context_limit (ContextLimitResolver takes it before anything but GOOSE_CONTEXT_LIMIT) and reasoning.

reasoning is true only for the models Goose actually sends a level to on an OpenAI-compatible provider. Those are OpenAI's reasoning models, as is_openai_responses_model in crates/goose-provider-types/src/formats/openai.rs tells them (gpt-5/6, o-series). For every other model (Claude, GLM, DeepSeek, Gemini…) Goose keeps thinking_effort to itself and never sends it, so a Thinking Level shown for them would do nothing. Sync rewrites the file as the catalog changes. Check reports a base_url or header someone else rewrote. Picking no model removes the file. The Notice asks for a restart, because Goose loads custom_providers at start-up. GOOSE_CONTEXT_LIMIT is left alone: it is one global override, and it would beat every per-model window.

What Goose itself can't do:
- On an OpenAI-compatible provider, Goose sends no effort for a model that isn't OpenAI's.
- The in-session thinking menu follows Goose's name heuristics and its registry, not the provider's reasoning.
- Before a session starts, the desktop app's context ring prefers Goose's registry to the declared window.

TestGooseRunsOnMagpie covers the options, the file's keys, the windows, reasoning for gpt-5.5 but not glm-4.6, Check, Sync and removal; the old code fails it. go vet, the builds (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass, except TestProvidersAnswerWhileListsComeIn and TestSavedButSignedOutClaudeServed: these timing tests pass when run alone. Not tried against a real Goose.
2026-10-03 19:18:46 +08:00

155 lines
5.1 KiB
Go

package agent
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// A goose on PATH that is pressly's migration tool (a Go program) is not the
// Goose agent (Discord: Jun, goose shown without ~/.config/goose); Block's
// goose, not a Go program, is.
func TestGooseDetectSkipsGoGoose(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("PATH lookup of a script")
}
home := t.TempDir()
bin := t.TempDir()
t.Setenv("PATH", bin)
a := goose(home, filepath.Join(home, ".config"))
if a.Detected() {
t.Fatal("detected with no goose at all")
}
src := filepath.Join(t.TempDir(), "main.go")
os.WriteFile(src, []byte("package main\nfunc main() {}\n"), 0o644)
gobin, err := exec.LookPath("go")
if err != nil {
gobin = filepath.Join(runtime.GOROOT(), "bin", "go")
}
build := exec.Command(gobin, "build", "-o", filepath.Join(bin, "goose"), src)
build.Env = append(os.Environ(), "PATH="+os.Getenv("PATH"))
if out, err := build.CombinedOutput(); err != nil {
t.Skipf("go build: %v %s", err, out)
}
if a.Detected() {
t.Fatal("a Go goose on PATH was taken for the agent")
}
os.WriteFile(filepath.Join(bin, "goose"), []byte("#!/bin/sh\necho 1.9.0\n"), 0o755)
if !a.Detected() {
t.Fatal("Block's goose on PATH not detected")
}
os.Remove(filepath.Join(bin, "goose"))
os.MkdirAll(filepath.Join(home, ".config", "goose"), 0o755)
if !a.Detected() {
t.Fatal("goose's config dir not detected")
}
}
// Goose is offered magpie's models, and picking one makes magpie a custom
// provider of Goose's that hands it each model's window and says which
// models Goose can send a thinking level (wani on Discord: Goose's GUI
// offered no thinking level and had the context wrong). A model Goose sends
// no level for (GLM, on an OpenAI-compatible provider) is not said to think.
func TestGooseRunsOnMagpie(t *testing.T) {
home := syncHome(t)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}}); err != nil {
t.Fatal(err)
}
a := goose(home, filepath.Join(home, ".config"))
f := a.Fields[0]
var vals []string
for _, o := range f.Options(map[string]string{}) {
vals = append(vals, o.Value)
}
if !slices.Contains(vals, "magpie/relay/glm-4.6") || !slices.Contains(vals, "magpie/oa/gpt-5.5") {
t.Fatalf("magpie's models not offered: %v", vals)
}
if err := f.Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
if got := f.Get(); got != "magpie/relay/glm-4.6" {
t.Fatalf("model = %q", got)
}
file := gooseProviderPath(a.Path)
if filepath.Dir(filepath.Dir(file)) != filepath.Dir(a.Path) {
t.Fatalf("custom provider at %s, config at %s", file, a.Path)
}
var p struct {
Name string `json:"name"`
Engine string `json:"engine"`
BaseURL string `json:"base_url"`
APIKeyEnv *string `json:"api_key_env"`
RequiresAuth *bool `json:"requires_auth"`
DynamicModels *bool `json:"dynamic_models"`
Skip bool `json:"skip_canonical_filtering"`
Headers map[string]string `json:"headers"`
Models []struct {
Name string `json:"name"`
Context int `json:"context_limit"`
Reasoning bool `json:"reasoning"`
} `json:"models"`
}
if err := json.Unmarshal([]byte(readFile(file)), &p); err != nil {
t.Fatalf("%v: %s", err, readFile(file))
}
if p.Name != "magpie" || p.Engine != "openai" || p.BaseURL != gatewayV1() ||
p.Headers["Authorization"] != "Bearer "+gateway.Token || p.APIKeyEnv == nil || *p.APIKeyEnv != "" ||
p.RequiresAuth == nil || *p.RequiresAuth || p.DynamicModels == nil || *p.DynamicModels || !p.Skip {
t.Fatalf("provider: %s", readFile(file))
}
think := map[string]bool{}
window := map[string]int{}
for _, m := range p.Models {
think[m.Name], window[m.Name] = m.Reasoning, m.Context
}
if window["relay/glm-4.6"] != 204800 {
t.Fatalf("glm window = %d: %s", window["relay/glm-4.6"], readFile(file))
}
if think["relay/glm-4.6"] || !think["oa/gpt-5.5"] {
t.Fatalf("reasoning: %v", think)
}
if c := a.Check(); c != "" {
t.Fatalf("check: %s", c)
}
if err := edit.SetJSON(file, edit.KV{Path: "base_url", Value: "http://elsewhere/v1"}); err != nil {
t.Fatal(err)
}
if a.Check() == "" {
t.Fatal("a rewritten base_url not noticed")
}
// the catalog as it is now reaches the file
writeFile(t, catalog.CachePath(), `{"zai":{"models":{"glm-4.6":{"id":"glm-4.6","name":"GLM-4.6","limit":{"context":131072}}}}}`)
catalog.Reset()
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if !strings.Contains(readFile(file), `"context_limit": 131072`) || a.Check() != "" {
t.Fatalf("not synced: %s", readFile(file))
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(file); !os.IsNotExist(err) {
t.Fatalf("custom provider left behind: %v", err)
}
if f.Get() != "" {
t.Fatalf("model left: %q", f.Get())
}
}