Files
magpie/internal/gateway/agenteffort_test.go
T
yetone 5361a2178e agent, gateway: Cursor Private Inference's row has an effort, which magpie's gateway asks its requests for (#1003, gjf7) — other agents' rows pick a reasoning effort that magpie writes into their config, but Cursor Private Inference reads only its base URL and key from the environment, and its app offers no Reasoning control for most of magpie's models, so there was no way to set one short of a routing group with a member fixed at a level, which the reporter used as a workaround. Its row now has an effort field like the others': the default (as Cursor asks) first, then every level the models it lists have (cursorLocalEfforts: their levels in MemberEfforts order, low/medium/high where a model reasons with levels unknown, none at all when nothing it lists reasons or it isn't connected). The pick is kept in magpie, settings' agentEfforts by agent (provider.AgentEffort / SetAgentEffort, a level of MemberEfforts or "" for none), and the gateway's candidate loop asks a cursor-local request for it with withFixedEffort, fitted to the model's levels and bounded by a group's, on a model that reasons (the new Provider.Thinks: levels, its list's Reasoning, or models.dev's). Precedence: a group member's fixed effort, then a :effort suffix, then a routing rule's turn pick, then the agent's pick, then the request's own; the pick wins over a level Cursor sent, as Cursor sends its own default as often as a choice, and Default hands the choice back to Cursor. A model that doesn't reason and every other agent's requests go as they came. The Settings page's save carries agentEfforts over as it does fastPicks (TestSettingsSaveKeepsWhatItDoesNotSend's fixture names it). In the window the field sits under "Settings" rather than "New sessions" for this agent alone, as it applies to every request from then on, not to what a session starts on; no new t() strings (Settings, effort and default are translated in zh, ja and de already). TestAgentEffortOnCursorLocal (Chat and Responses with and without a level of their own, Claude on Messages made to think, OpenCode's request untouched, a non-reasoning model untouched, a member fixed at low kept, the pick taken off) fails without the gateway change and passes; TestCursorLocalEffort covers the options (none with no reasoning model, the union of levels, the low/medium/high fallback, none while disconnected), Apply and a bad level refused; both pass -race (-count=20 gateway, -count=10 agent). cursor-local-effort.test.cjs (the window at 560px and the tray panel at 440px: the field under Settings, the slider's five stops with Default first, a pick posting the field, no sideways scroll) passes on Chromium and WebKit in en, zh, ja and de; claude-effort, subagent-effort, agent-field-names, agent-connect, agent-layout, agent-tweak-fold, agent-models, cursor-local-launch and i18n pass on both. go test -tags nogui ./internal/..., go vet, gofmt -l, linux (nogui) and windows builds pass. Not tried with the real Cursor Private Inference: whether it sends a reasoning level of its own on models it shows a Reasoning control for is unverified, which the precedence makes moot. docs/subsystems/gateway-routing.md and agent-wiring.md say it.
2026-10-06 17:29:28 +08:00

100 lines
4.4 KiB
Go

package gateway
import (
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Cursor Private Inference can't say an effort in its environment nor, for
// most models, in its app (#1003): the one picked for it in magpie is asked
// on its requests to a model that reasons, whichever API it speaks, in
// place of a level the request sent; another agent's requests, a model
// that doesn't reason and a member a group fixes at a level go as they
// were, and with the pick taken off Cursor's go as it asks.
func TestAgentEffortOnCursorLocal(t *testing.T) {
s, up := fasted(t)
if err := provider.Save(provider.Provider{ID: "plain", Name: "Plain", Key: "kp", Chat: "https://plain.example/v1", Models: []string{"gpt-4o-mini"}}); err != nil {
t.Fatal(err)
}
// the levels each vendor lists its model with
if err := catalog.SaveLive("oa", "https://api.openai.com/v1", []catalog.Model{{ID: "gpt-6.1-sol", Efforts: []string{"low", "medium", "high", "xhigh"}}, {ID: "gpt-6.1-mini"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("an", "https://api.anthropic.com", []catalog.Model{{ID: "claude-opus-5-5", Reasoning: true}, {ID: "claude-sonnet-5"}}); err != nil {
t.Fatal(err)
}
if p, err := provider.Find("plain"); err != nil || p.Thinks("gpt-4o-mini") {
t.Fatal("the fixture's plain model reasons")
}
if err := provider.SetAgentEffort("cursor-local", "HIGH"); err != nil {
t.Fatal(err)
}
if got := provider.AgentEffort("cursor-local"); got != "high" {
t.Fatalf("kept %q", got)
}
if err := provider.SetAgentEffort("cursor-local", "hard"); err == nil {
t.Fatal("a word that isn't a level was kept")
}
chat := func(model, extra string) string {
return `{"model":"` + model + `","messages":[{"role":"user","content":"hi"}]` + extra + `}`
}
effortOf := func(body map[string]any) any {
if r, ok := body["reasoning"].(map[string]any); ok {
return r["effort"]
}
return body["reasoning_effort"]
}
for _, a := range []struct{ name, path, body string }{
{"chat, none asked", "/v1/chat/completions", chat("oa/gpt-6.1-sol", "")},
{"chat, low asked", "/v1/chat/completions", chat("oa/gpt-6.1-sol", `,"reasoning_effort":"low"`)},
{"responses, none asked", "/v1/responses", `{"model":"oa/gpt-6.1-sol","input":"hi"}`},
{"responses, low asked", "/v1/responses", `{"model":"oa/gpt-6.1-sol","input":"hi","reasoning":{"effort":"low"}}`},
} {
postFrom(t, s, "cursor-local", a.path, a.body)
if host, _, _, body := up.last(t); host != "api.openai.com" || effortOf(body) != "high" {
t.Fatalf("%s: sent to %s %v", a.name, host, body)
}
}
// a Claude on Messages thinks, at the pick's level
postFrom(t, s, "cursor-local", "/v1/messages", `{"model":"an/claude-opus-5-5","max_tokens":32000,"messages":[{"role":"user","content":"hi"}]}`)
if host, _, _, body := up.last(t); host != "api.anthropic.com" || body["thinking"] == nil {
t.Fatalf("claude: sent to %s %v", host, body)
}
// another agent's request goes as it asked
postFrom(t, s, "opencode", "/v1/chat/completions", chat("oa/gpt-6.1-sol", `,"reasoning_effort":"low"`))
if _, _, _, body := up.last(t); effortOf(body) != "low" {
t.Fatalf("opencode's request took Cursor's pick: %v", body)
}
// a model that doesn't reason isn't asked to
postFrom(t, s, "cursor-local", "/v1/chat/completions", chat("plain/gpt-4o-mini", ""))
if host, _, _, body := up.last(t); host != "plain.example" || effortOf(body) != nil {
t.Fatalf("a model that doesn't reason: sent to %s %v", host, body)
}
// a group's member fixed at a level keeps it
if err := provider.SaveGroup(provider.Group{ID: "fx", Name: "FX", Members: []string{"oa/gpt-6.1-sol:low"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
postFrom(t, s, "cursor-local", "/v1/chat/completions", chat("group/fx", ""))
if _, _, _, body := up.last(t); effortOf(body) != "low" {
t.Fatalf("a member fixed at low: %v", body)
}
// taken off: Cursor's requests go as it asks
if err := provider.SetAgentEffort("cursor-local", ""); err != nil {
t.Fatal(err)
}
postFrom(t, s, "cursor-local", "/v1/chat/completions", chat("oa/gpt-6.1-sol", `,"reasoning_effort":"low"`))
if _, _, _, body := up.last(t); effortOf(body) != "low" {
t.Fatalf("taken off, still asked: %v", body)
}
postFrom(t, s, "cursor-local", "/v1/chat/completions", chat("oa/gpt-6.1-sol", ""))
if _, _, _, body := up.last(t); effortOf(body) != nil {
t.Fatalf("taken off, an effort was added: %v", body)
}
}