agent: opencode.json lists magpie's models in the order the user set

The models of magpie's provider in opencode.json were a
map[string]any, whose keys encoding/json marshals sorted: the order the
user dragged to on the Agents page reached magpieModels and died at the
last step, the file listing the models A to Z (#4575). A pure
re-ordering wasn't even written — syncJSONInOrder's sameOrder compared
two alphabetical lists and found them alike. The variants under each
model kept their order since #713 (orderedJSON); the models keep theirs
the same way now, and openCodeKept looks a model up in the ordered
block to put a variant of the user's on it.

Every config in OpenCode's shape moves with the one fix: OpenCode,
OpenChamber, Air, MiMo Code and DevEco Code (openCodeLike and the
openCodeKept calls in air.go and openchamber.go). Pi, Crush and Codex
list models in arrays and were never touched.

Run: TestOpenCodeModelsInOrder in internal/agent (written first) fails
on the unmodified code — the fresh block lands alphabetical and a
dragged order is never written — and passes with the fix. The
models-as-map assertions in TestInferredGroupImagesReachAgentCatalogs,
TestMiMoCodeCatalogNarrowedSeparately and
TestGroupModelsCarryTheirFirstMembersPrice read the ordered block now.
go test -tags nogui -count=1 ./internal/agent ok (150s) but for
TestMothX, which fails the same on unmodified main cac655c14 on this
machine (mothx_test.go:154) — pre-existing, not this fix's. gofmt
clean; go vet -tags nogui clean on darwin, linux and windows; go build
-tags nogui ./... ok on the three.

Generated-by: GLM-5.3-Flash (ZCode)

Broken by hand: with the agents.go hunks reverted,
TestOpenCodeModelsInOrder is red (written: [aaa/m1 relay/glm-4.6]).
Not changed: Cline's models.json is built from a map too (cline.go
clineModelsAt and clineKept). Filed as #4594.
This commit is contained in:
ggbdpq
2026-11-01 02:45:01 +08:00
committed by yetone
parent b0361f7850
commit cdd470ce9f
5 changed files with 91 additions and 9 deletions
+11 -4
View File
@@ -493,7 +493,10 @@ func magpieProviderJSONAt(shape, catalog, gw string) any {
models := magpieModels(catalog) // the catalog is the agent's
switch shape {
case "opencode":
ms := map[string]any{}
// models in the order magpieModels has them (#4575): OpenCode lists
// the models in the file's key order, and a map's keys marshal
// sorted — the variants under each kept theirs since #713
ms := orderedJSON{}
for _, m := range models {
e := map[string]any{"name": m.Name}
// modalities alone says a model takes images: OpenCode 1 takes
@@ -519,7 +522,7 @@ func magpieProviderJSONAt(shape, catalog, gw string) any {
if p := m.Price; p != nil {
e["cost"] = map[string]any{"input": p.Input, "output": p.Output, "cache_read": p.CacheRead, "cache_write": p.CacheWrite}
}
ms[m.ID] = e
ms = append(ms, jsonPair{m.ID, e})
}
return map[string]any{"npm": "@ai-sdk/openai-compatible", "name": "magpie",
"options": map[string]any{"baseURL": gw + "/v1", "apiKey": openCodeKeyAt(catalog, gw)}, "models": ms}
@@ -609,12 +612,16 @@ func openCodeKept(path string, value func() any) func() any {
}
var have map[string]json.RawMessage
block, _ := v.(map[string]any)
ms, _ := block["models"].(map[string]any)
ms, _ := block["models"].(orderedJSON)
if json.Unmarshal([]byte(raw), &have) != nil || ms == nil {
return v
}
for id, h := range have {
e, _ := ms[id].(map[string]any)
i := slices.IndexFunc(ms, func(p jsonPair) bool { return p.k == id })
if i < 0 {
continue
}
e, _ := ms[i].v.(map[string]any)
ours, ok := e["variants"].(orderedJSON)
var was struct {
Variants json.RawMessage `json:"variants"`
+6
View File
@@ -1,6 +1,7 @@
package agent
import (
"slices"
"testing"
"github.com/yetone/magpie/internal/provider"
@@ -51,6 +52,11 @@ func TestGroupModelsCarryTheirFirstMembersPrice(t *testing.T) {
case map[string]any:
m, _ := v[id].(map[string]any)
return m
case orderedJSON:
if i := slices.IndexFunc(v, func(p jsonPair) bool { return p.k == id }); i >= 0 {
m, _ := v[i].v.(map[string]any)
return m
}
case []map[string]any:
for _, m := range v {
if m["id"] == id {
+3 -1
View File
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
@@ -156,7 +157,8 @@ func TestInferredGroupImagesReachAgentCatalogs(t *testing.T) {
t.Fatal(err)
}
id := "auto/claude-sonnet-4-5"
oc := magpieProviderJSON("opencode").(map[string]any)["models"].(map[string]any)[id].(map[string]any)
ms := magpieProviderJSON("opencode").(map[string]any)["models"].(orderedJSON)
oc, _ := ms[slices.IndexFunc(ms, func(p jsonPair) bool { return p.k == id })].v.(map[string]any)
if _, ok := oc["attachment"]; ok || !reflect.DeepEqual(oc["modalities"].(map[string]any)["input"], []string{"text", "image"}) {
t.Fatalf("OpenCode group image input: %v", oc)
}
+63
View File
@@ -132,6 +132,69 @@ func TestOpenCodeVariantsInOrder(t *testing.T) {
}
}
// modelKeys are the ids of magpie's models in an opencode config at path,
// in the order the file's bytes list them.
func modelKeys(t *testing.T, path string) []string {
t.Helper()
raw, ok := edit.GetJSON(path, "provider.magpie.models")
if !ok {
t.Fatalf("no models of magpie's in %s", path)
}
var out []string
for _, p := range objectPairs([]byte(raw)) {
out = append(out, p.k)
}
return out
}
// TestOpenCodeModelsInOrder: opencode.json lists magpie's models in the
// order the user dragged them to on the Agents page (#4575); they were
// alphabetical — the models were a map, whose keys marshal sorted, and a
// pure re-ordering wasn't even written, the file and the new block sorting
// alike. The variants under each model kept theirs since #713.
func TestOpenCodeModelsInOrder(t *testing.T) {
home := syncHome(t)
// a provider the catalog lists after relay's, whose models sort before:
// magpie's own order and the alphabetical one differ
if err := provider.Save(provider.Provider{ID: "aaa", Name: "AAA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".config", "opencode", "opencode.json")
writeFile(t, path, `{"theme":"dark"}`)
if err := opencode(home, filepath.Join(home, ".config")).Field("model").Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
want := []string{"relay/glm-4.6", "aaa/m1"}
if got := modelKeys(t, path); !slices.Equal(got, want) {
t.Fatalf("written: %v, want %v\n%s", got, want, readFile(path))
}
if err := provider.SetModelOrder("opencode", []string{"aaa/m1"}); err != nil {
t.Fatal(err)
}
if err := opencode(home, filepath.Join(home, ".config")).Sync(); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(path)
if got := modelKeys(t, path); !slices.Equal(got, []string{"aaa/m1", "relay/glm-4.6"}) {
t.Fatalf("reordered: %v, want aaa/m1 first\n%s", got, b)
}
if err := opencode(home, filepath.Join(home, ".config")).Sync(); err != nil {
t.Fatal(err)
}
if b2, _ := os.ReadFile(path); !bytes.Equal(b, b2) {
t.Fatalf("rewritten though in order:\n%s\n%s", b, b2)
}
if err := provider.SetModelOrder("opencode", nil); err != nil {
t.Fatal(err)
}
if err := opencode(home, filepath.Join(home, ".config")).Sync(); err != nil {
t.Fatal(err)
}
if got := modelKeys(t, path); !slices.Equal(got, want) {
t.Fatalf("default: %v, want %v\n%s", got, want, readFile(path))
}
}
func mustJSON(t *testing.T, v any) string {
t.Helper()
b, err := json.Marshal(v)
+8 -4
View File
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"testing"
@@ -439,12 +440,15 @@ func TestMiMoCodeCatalogNarrowedSeparately(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { settings.Save(settings.Settings{}) })
mimo := magpieProviderJSONFor("opencode", "mimocode").(map[string]any)["models"].(map[string]any)
oc := magpieProviderJSON("opencode").(map[string]any)["models"].(map[string]any)
if len(mimo) != 1 || mimo["relay/glm-4.6"] == nil {
mimo := magpieProviderJSONFor("opencode", "mimocode").(map[string]any)["models"].(orderedJSON)
oc := magpieProviderJSON("opencode").(map[string]any)["models"].(orderedJSON)
has := func(ms orderedJSON, id string) bool {
return slices.IndexFunc(ms, func(p jsonPair) bool { return p.k == id }) >= 0
}
if len(mimo) != 1 || !has(mimo, "relay/glm-4.6") {
t.Fatalf("mimocode catalog: %v", mimo)
}
if oc["relay/glm-4.6"] == nil {
if !has(oc, "relay/glm-4.6") {
t.Fatalf("opencode catalog was narrowed too: %v", oc)
}
}