Files
magpie/internal/agent/responses_strict_test.go
T
ggbdpq 1cf66e4b17 agent: a tool asked on Responses says strict: false, its optional parameters stay optional (#3055)
What changed: OpenAI's Responses API takes a function tool whose strict
is left out as a strict-mode one and normalizes its schema, where every
property is required, so a tool's optional parameters came back filled
(#3055, ArakawaHenri). pi-ai (dsh's llm-pi-ai, Pi and its kin) says
strict: false on an ordinary tool only when the model's compat says
supportsStrictMode, which it takes as false where the compat is left
out. magpie now says it: every model of a dsh route whose api stands as
openai-responses carries compat.supportsStrictMode, by the api the route
has now (the one the user picked in dsh's Models page kept), not by its
name, beside the keys they set there (dshKeepCompat); and piModelJSON
says it for a model best served on Responses, which Pi's and StepCode's
models.json carry (Pencil's entries stay on Chat throughout). omp's
models.yml is left alone: omp turns a models.yml away over a field its
schema doesn't name (16.3.5 over a max), so the compat needs an omp
version of its own pinned first. docs/subsystems/agent-wiring.md says
so.

Run: new TestDshTellsAResponsesToolsStrictIsOff and
TestPiTellsAResponsesToolsStrictIsOff fail without the change (the
first with "a model asked on Responses isn't told strict is off:
Compat:<nil>", the second with "resp/gpt-5.5 is asked on Responses and
isn't told strict is off") and pass with it; removing only the dsh case
turns the first red and only it, removing only the piModelJSON compat
line turns the second red and only it. go test ./internal/agent/
(TestMothX fails the same with both files back at the baseline here, so
not from this change), gofmt -l on the changed files (nothing), go vet
./..., GOOS=linux GOARCH=amd64 go vet -tags nogui ./...,
GOOS=windows GOARCH=amd64 go vet ./..., go build . (darwin arm64),
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -tags nogui .,
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -tags nogui .,
GOOS=windows GOARCH=amd64 go build -o /dev/null .

Landed (yetone). Broken by hand on mote: with the dsh case and
piModelJSON's compat line taken out, both new tests are red ("a model
asked on Responses isn't told strict is off: … Compat:<nil>",
"resp/gpt-5.5 is asked on Responses and isn't told strict is off").
Run at the pushed head: go test ./... -timeout 45m on mote with this branch on 41b5c0f19: 83 packages ok. internal/gui is red on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners (#3949), both red alone at 41b5c0f19 too, and on TestColdTurnsTaken (#4510). internal/update is red on TestRelaunchOpenTriedAgain (#3881). The last two pass alone at 41b5c0f19 and here. Rebased on f856d381a: agent, gateway, plugin, provider and sessions ok, gui red on #4514 and #3949 only, gofmt -l clean, go vet ok for linux (nogui), windows and darwin. Rebased on bb478f0e5: agent and gateway ok, gui the same. The last rebase, onto 573b21f4b, brings in only internal/gui/providers*.go and internal/provider/models.go, which this branch doesn't touch.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 06:21:39 +08:00

167 lines
6.2 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// dshModelOf is the model id of a route as dshPiProviders parsed it.
func dshModelOf(t *testing.T, ps map[string]dshPiRoute, key, id string) *dshPiModel {
t.Helper()
r, ok := ps[key]
if !ok {
t.Fatalf("no route %s", key)
}
for i := range r.Models {
if r.Models[i].ID == id {
return &r.Models[i]
}
}
t.Fatalf("route %s has no %s: %+v", key, id, r.Models)
return nil
}
// #3055 (ArakawaHenri): OpenAI's Responses API takes a function tool whose
// strict is left out as a strict-mode one and normalizes its schema, where
// every property is required, so a tool's optional parameters came back
// filled. pi-ai says strict: false on an ordinary tool only when the
// model's compat says supportsStrictMode, which it takes as false where
// the compat is left out — as magpie's routes were. magpie now says it on
// every model of a route whose api stands as openai-responses (the one a
// user picked in dsh's Models page kept), and on none of a Chat or
// Messages route.
func TestDshTellsAResponsesToolsStrictIsOff(t *testing.T) {
home, dir, web := dshProtocolHome(t)
dshKeyFixture(dir)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
a := dsh(home)
if err := a.Field("model").Set("magpie/resp/gpt-5.5"); err != nil {
t.Fatal(err)
}
ps := dshPiProviders(t, web)
if m := dshModelOf(t, ps, "magpie-responses", "resp/gpt-5.5"); m.Compat == nil || !m.Compat.SupportsStrictMode {
t.Fatalf("a model asked on Responses isn't told strict is off: %+v", m)
}
if m := dshModelOf(t, ps, "magpie", "deepseek/pro"); m.Compat != nil && m.Compat.SupportsStrictMode {
t.Fatalf("a Chat model was told strict is off: %+v", m)
}
if dshModelOf(t, ps, "magpie", "deepseek/flash").Compat != nil {
t.Fatal("a Chat model took a compat of its own")
}
if m := dshModelOf(t, ps, "magpie-anthropic", "anth/claude-sonnet-5"); m.Compat == nil || !m.Compat.ForceAdaptiveThinking || m.Compat.SupportsStrictMode {
t.Fatalf("a Claude on Messages: %+v", m)
}
// a round over what it wrote leaves it as it is
was, _ := os.ReadFile(web)
dshWiredOnce()
if again, _ := os.ReadFile(web); string(again) != string(was) {
t.Fatalf("written again:\n%s", again)
}
// the api a user picked for a route in dsh's Models page decides, not
// the route's name: magpie's first route on Responses tells its Chat
// models strict is off and keeps a key of theirs beside magpie's; the
// responses route on Chat tells its model nothing there
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"+
"- id: llm-pi-ai # magpie\n name: \"@deepseek-ai/dsh-llm-pi-ai\"\n config:\n providers:\n"+
" magpie:\n displayName: Magpie\n apiKeyEnv: "+dshKeyRef+"\n api: openai-responses\n baseURL: "+gatewayV1()+"\n models:\n"+
" - id: deepseek/pro\n compat:\n maxTokensField: max_tokens\n"+
" magpie-responses:\n displayName: Magpie · Responses\n apiKeyEnv: "+dshKeyRef+"\n api: openai-completions\n baseURL: "+gatewayV1()+"\n models:\n"+
" - id: resp/gpt-5.5\n"), 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(web)
_, compat := dshRouteCompat(t, web, "magpie")
c := compat["deepseek/pro"]
if c["supportsStrictMode"] != true || c["maxTokensField"] != "max_tokens" {
t.Fatalf("a Chat model on a route switched to Responses: %v\n%s", c, b)
}
_, compat = dshRouteCompat(t, web, "magpie-responses")
if c = compat["resp/gpt-5.5"]; c != nil && c["supportsStrictMode"] == true {
t.Fatalf("a model on a route switched to Chat was told strict is off: %v\n%s", c, b)
}
}
// The same for Pi's and StepCode's model entries (piModelJSON): a model
// best served on Responses carries compat.supportsStrictMode, a Chat
// model's entry carries no compat at all. Pencil's entries stay on Chat
// throughout, so it takes none.
func TestPiTellsAResponsesToolsStrictIsOff(t *testing.T) {
home := syncHome(t)
if err := provider.Save(provider.Provider{ID: "resp", Name: "Resp", Key: "k", Responses: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("resp", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "gpt-5.5", Efforts: []string{"high"}}}); err != nil {
t.Fatal(err)
}
piModels := filepath.Join(home, ".pi", "agent", "models.json")
// the provider block as magpie wrote it before this provider was, with
// a key of pi-cache-optimizer's in it
writeFile(t, piModels, `{
"providers": {
"magpie": {
"api": "openai-completions",
"apiKey": "magpie",
"baseUrl": "http://127.0.0.1:3425/v1",
"compat": {
"sendSessionAffinityHeaders": true
},
"models": [
{
"id": "gone/old-model",
"name": "Old"
}
],
"name": "magpie"
}
}
}
`)
// the models.json the test writes is one magpie wrote while Pi was
// connected: a sync only tends a ListOnly agent's file while it
// reads as connected (Connect), and a block magpie never set up is
// the user's and is left alone
if err := pi(home).Connect(); err != nil {
t.Fatal(err)
}
SyncCatalog()
b, err := os.ReadFile(piModels)
if err != nil {
t.Fatal(err)
}
var f struct {
Providers struct {
Magpie struct {
Models []struct {
ID string `json:"id"`
API string `json:"api"`
Compat map[string]any `json:"compat"`
} `json:"models"`
} `json:"magpie"`
} `json:"providers"`
}
if err := json.Unmarshal(b, &f); err != nil {
t.Fatalf("%v:\n%s", err, b)
}
strict := map[string]bool{}
for _, m := range f.Providers.Magpie.Models {
if m.API == "openai-responses" && m.Compat["supportsStrictMode"] != true {
t.Fatalf("%s is asked on Responses and isn't told strict is off:\n%s", m.ID, b)
}
strict[m.ID] = m.Compat["supportsStrictMode"] == true
}
if !strict["resp/gpt-5.5"] {
t.Fatalf("no responses model of magpie's carries supportsStrictMode:\n%s", b)
}
if strict["relay/glm-4.6"] {
t.Fatalf("a Chat model was told strict is off:\n%s", b)
}
}