Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

9.2 KB
Raw Blame
package provider

import (
	"cmp"
	"encoding/json"
	"os"
	"path/filepath"
	"slices"
	"testing"

	"github.com/yetone/magpie/internal/catalog"
	"github.com/yetone/magpie/internal/codexcat"
)

// A group reasons when a member does: a member that doesn't think is no
// reason to tell the agent the group can't, as a text-only member is no
// reason to say it can't see (#756's rule for images, applied to
// reasoning).
//
// Before this, one member that didn't think made the whole group
// 不支持推理, and the agent turned thinking off for it: a routing group of
// deepseek-v4-flash-0731-max (a Trae plugin model magpie read as one that
// doesn't reason) with a DeepSeek that does lost reasoning for both.
func TestGroupReasoningFromAMemberThatThinks(t *testing.T) {
	groupReasoningHome(t)
	for name, in := range map[string][]Entry{
		// the one that doesn't think is the group's own model (its first
		// member's), so the group's own entry says nothing of reasoning
		"quiet first": {quietMember(), thinksMember()},
		"quiet last":  {thinksMember(), quietMember()},
	} {
		g := groupEntries(in)
		if len(g) != 1 {
			t.Fatalf("%s: groups %+v", name, g)
		}
		if !g[0].Reasoning {
			t.Errorf("%s: a member thinks, but the group says it doesn't", name)
		}
	}
}

// A group of members that don't think reasons for none of them.
func TestGroupReasoningFromNoMember(t *testing.T) {
	groupReasoningHome(t)
	g := groupEntries([]Entry{quiet("q"), quiet("r")})
	if len(g) != 1 {
		t.Fatalf("groups: %+v", g)
	}
	if g[0].Reasoning || len(g[0].Efforts) != 0 {
		t.Fatalf("no member thinks: reasoning %v, levels %v", g[0].Reasoning, g[0].Efforts)
	}
}

// A group that thinks offers the levels its members have in common, as
// before: a member that thinks with none of its own doesn't take the
// others' away (the #597 rule for a model whose levels nothing knows), nor
// does one known not to think, which the gateway sends no reasoning ask
// (#2127: the group had none, and Codex no effort control for it), and two
// that think share theirs.
func TestGroupReasoningLevels(t *testing.T) {
	groupReasoningHome(t)
	for name, c := range map[string]struct {
		in   []Entry
		want []string
	}{
		"one thinks":       {[]Entry{thinksMember(), quietMember()}, levels},
		"quiet first":      {[]Entry{quietMember(), thinksMember()}, levels},
		"quiet between":    {[]Entry{thinksMember(), quietMember(), thinks("b", []string{"low", "high", "medium"})}, []string{"low", "high"}},
		"two think":        {[]Entry{thinksMember(), thinks("b", []string{"low", "high", "medium"})}, []string{"low", "high"}},
		"thinks no levels": {[]Entry{thinks("a", nil), thinks("b", []string{"low", "high"})}, []string{"low", "high"}},
	} {
		g := groupEntries(c.in)
		if len(g) != 1 {
			t.Fatalf("%s: groups %+v", name, g)
		}
		if !slices.Equal(g[0].Efforts, c.want) {
			t.Errorf("%s: levels %v, want %v", name, g[0].Efforts, c.want)
		}
		if !g[0].Reasoning {
			t.Errorf("%s: reasoning off", name)
		}
	}
}

// A group that names its own levels offers those, member that doesn't
// think or not (Group.Levels, #295); one that doesn't think itself is
// still told to reason, its levels named.
func TestGroupReasoningNamedLevels(t *testing.T) {
	groupReasoningHome(t)
	if err := SaveGroup(Group{Name: "named", Members: []string{"a/m", "a/n"}, Levels: []string{"low", "high"}}); err != nil {
		t.Fatal(err)
	}
	for _, g := range groupEntries(append([]Entry{quietMember()}, Catalog()...)) {
		if g.ID != GroupPrefix+"named" {
			continue
		}
		if !g.Reasoning || !slices.Equal(g.Efforts, []string{"low", "high"}) {
			t.Fatalf("named levels: reasoning %v, levels %v", g.Reasoning, g.Efforts)
		}
		return
	}
	t.Fatal("no group named")
}

// A member that doesn't think doesn't take the other members' images,
// window or reply limit either: those aggregate as they did (#756, #700).
func TestGroupReasoningQuietMemberKeepsTheRest(t *testing.T) {
	groupReasoningHome(t)
	yes := true
	quiet := quietMember()
	quiet.Context, quiet.Output = 200000, 8000
	other := thinksMember()
	other.Images, other.ImageInput, other.Context, other.Output = true, &yes, 1000000, 64000
	g := groupEntries([]Entry{quiet, other})
	if len(g) != 1 {
		t.Fatalf("groups: %+v", g)
	}
	e := g[0]
	if !e.Images || e.ImageInput == nil || !*e.ImageInput || e.Context != 1000000 || e.Output != 64000 {
		t.Fatalf("a quiet member took the rest: %+v", e)
	}
	if !e.Reasoning {
		t.Fatalf("reasoning off: %+v", e)
	}
}

// The group a Trae plugin model was reported in: its provider is signed in
// (so its levels are known to be none, not unknown — it is the plugin's own
// list that says nothing of reasoning), it has no levels, and the DeepSeek
// beside it has some. The group is told to reason, whatever it offers as
// levels.
func TestGroupReasoningOfAPluginModelThatDoesNotThink(t *testing.T) {
	groupReasoningHome(t)
	trae := Provider{ID: "trae", Name: "Trae", Key: "k", Chat: "http://127.0.0.1:1/v1",
		Models:  []string{"deepseek-v4-flash-0731-max"},
		Account: &Account{Agent: "plugin", models: func() []catalog.Model { return nil }}}
	if err := Save(trae); err != nil {
		t.Fatal(err)
	}
	if err := SaveGroup(Group{Name: "mix", Members: []string{"trae/deepseek-v4-flash-0731-max", "a/m"}}); err != nil {
		t.Fatal(err)
	}
	found := false
	for _, e := range Catalog() {
		if e.ID != GroupPrefix+"mix" {
			continue
		}
		found = true
		if !e.Reasoning {
			t.Errorf("the group is 不支持推理, though a member thinks")
		}
	}
	if !found {
		t.Fatal("no group mix")
	}
}

// #2127: a routing group with a member known not to think, as Codex is
// handed it. The group's entry lists the levels of the member that thinks,
// so Codex shows its effort control and starts new sessions at the level
// set for them, not at its own medium with no control to change it.
func TestGroupQuietMemberKeepsCodexEffortControl(t *testing.T) {
	isolate(t)
	groupReasoningHome(t)
	codex := t.TempDir()
	t.Setenv("CODEX_HOME", codex)
	// a vendor whose own list says its model doesn't think (models.dev's
	// reasoning false): known quiet, not #597's unknown
	if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(catalog.CachePath(), []byte(`{"opencode-go": {"models": {"plain": {"id":"plain","reasoning":false}}}}`), 0o644); err != nil {
		t.Fatal(err)
	}
	catalog.Reset()
	t.Cleanup(catalog.Reset)
	if err := Save(Provider{ID: "q", Name: "Q", Key: "k", Chat: "http://127.0.0.1:1/v1", Catalog: "opencode-go", Models: []string{"plain"}}); err != nil {
		t.Fatal(err)
	}
	if e, ok := ServedEntryOf("q/plain"); !ok || !e.Quiet() {
		t.Fatalf("q/plain isn't a member known not to think: %+v", e)
	}
	if err := SaveGroup(Group{Name: "high", Members: []string{"q/plain", "a/m"}}); err != nil {
		t.Fatal(err)
	}
	for _, effort := range []string{"", "max"} {
		if effort != "" {
			if err := os.WriteFile(filepath.Join(codex, "config.toml"), []byte("model_reasoning_effort = \""+effort+"\"\n"), 0o600); err != nil {
				t.Fatal(err)
			}
		}
		type entry struct {
			Slug    string `json:"slug"`
			Default string `json:"default_reasoning_level"`
			Levels  []struct {
				Effort string `json:"effort"`
			} `json:"supported_reasoning_levels"`
		}
		var out struct {
			Models []entry `json:"models"`
		}
		if err := json.Unmarshal(codexcat.Catalog(CodexListed()), &out); err != nil {
			t.Fatal(err)
		}
		i := slices.IndexFunc(out.Models, func(m entry) bool { return m.Slug == GroupPrefix+"high" })
		if i < 0 {
			t.Fatalf("no group/high in Codex's list: %+v", out.Models)
		}
		m := out.Models[i]
		var got []string
		for _, l := range m.Levels {
			got = append(got, l.Effort)
		}
		want := cmp.Or(effort, "high") // DefaultEffort: medium, else high
		if !slices.Equal(got, levels) || m.Default != want {
			t.Errorf("new sessions at %q: levels %v, default %q; want %v, %q", effort, got, m.Default, levels, want)
		}
	}
}

// levels are the levels of the member that thinks in these tests.
var levels = []string{"low", "high", "max"}

// thinksMember is a member that thinks, with levels.
func thinksMember() Entry { return thinks("a", levels) }

// thinks is a model that does: its own levels as given. Same model id as
// quietMember's, so the two are members of one group.
func thinks(p string, efforts []string) Entry {
	return Entry{ID: p + "/m", Model: "m", Name: "m", Provider: Provider{ID: p}, Efforts: efforts, Reasoning: true}
}

// quietMember is a member that doesn't think, and has no levels of its own:
// the shape a plugin's model had before its plugin listed capabilities. Its
// provider's account gives a model list (so its levels are known to be none,
// not unknown: the gateway asks it the effort as it is, no more than high).
func quietMember() Entry {
	e := quiet("q")
	e.Provider.Account = &Account{Agent: "plugin", models: func() []catalog.Model { return nil }}
	return e
}

// quiet is a model that doesn't think: its provider gives no levels for it,
// and nothing says it reasons.
func quiet(p string) Entry {
	return Entry{ID: p + "/m", Model: "m", Name: "m", Provider: Provider{ID: p}}
}

// groupReasoningHome is a machine of its own with a provider whose model
// has levels the user set.
func groupReasoningHome(t *testing.T) {
	t.Helper()
	effortHome(t)
	if err := SetModelEfforts("a/m", levels); err != nil {
		t.Fatal(err)
	}
}