Files
magpie/internal/provider/plugin_context_test.go
yetone 19fafad16f provider: a test holds a plugin model and a group of it to one window (#3654)
mycf_711 (#3654) saw loomy/DeepSeek-V4.1-Flash told 1000000 (the "*"
window typed on the provider) and its group told 1048576 (the plugin's
declaration), and dsh then sent two histories in one request.

On main the member's entry is already the typed window (WindowOf, since
#2112 and 7aad6a50e), and buildGroupEntries reads that entry, so a group
of it that names no size is told 1000000 too. This test holds that. A
group told more than one member is told is a group with a larger member
in it (#700/#712), which is by design.

The doubling itself was not reproduced: in a sandbox HOME, dsh
0.2.1-rc.2 against a fake server, with the window flipped 1000000 <->
1048576 on resume, live (reload on and off), mid-request, after a
compaction, and with the model id flipped between a group and its
member, every request grew by its own two messages only. dsh builds a
request from session.deriveMessages() alone; a window change only adds a
request/context event.

Run (mote): go test -tags nogui -run
'TestGroupOfAPluginModelToldTheWindowSetOnIt|TestPluginModelWindow'
./internal/provider ok.
Not broken by hand: the test passes on main as it was; it guards, it
doesn't fix. Not tried: dsh's desktop/web profile, and the reporter's
own group (its other members).
2026-10-28 04:18:21 +08:00

139 lines
5.0 KiB
Go

package provider
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// clinePlugin is Cline's plugin signed in, its models as its feed lists
// them: ids with Cline's own prefixes and no window, as models.dev knows
// them under their makers.
func clinePlugin(t *testing.T) {
t.Helper()
claudeHome(t)
t.Setenv("MAGPIE_BUN", "")
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{
"zai":{"id":"zai","models":{"glm-5.3-flash":{"id":"glm-5.3-flash","limit":{"context":1000000,"output":131072}}}},
"xiaomi":{"id":"xiaomi","models":{"mimo-v2.6-flash":{"id":"mimo-v2.6-flash","limit":{"context":1048576,"output":131072}}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
spec := "@magpie-community/opencode-cline-auth"
write := func(name string, v any) {
t.Helper()
b, _ := json.Marshal(v)
os.MkdirAll(settings.Dir(), 0o700)
if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {
t.Fatal(err)
}
}
write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}})
write("plugin-auth.json", map[string]map[string]any{"cline": {"type": "api", "key": "sk-cline-1234"}})
plugin.UseCached([]plugin.Provider{{ID: "cline", Spec: spec, Name: "Cline",
Methods: []plugin.Method{{Type: "api", Label: "API key"}},
Accounts: []plugin.Account{{Key: "cline", Type: "api"}},
Models: []plugin.Model{
{ID: "cline-pass/glm-5.3-flash", Name: "GLM 5.3 Flash (ClinePass)"},
{ID: "cline-free/mimo-v2.6-flash", Name: "MiMo V2.6 Flash (free)", Free: true},
{ID: "stealth/space-bunny-alpha", Name: "Space Bunny Alpha", Context: 1_000_000},
}}})
t.Cleanup(func() { plugin.UseCached(nil) })
}
func pluginWindow(t *testing.T, id string) int {
t.Helper()
for _, e := range Served() {
if e.ID == id {
return e.Context
}
}
t.Fatalf("no %s in %v", id, IDs())
return 0
}
// A plugin's model listed without a window (Cline's feed says none) takes
// the one models.dev gives its model under the vendor's prefix, and the
// window the user sets on the provider over both (ARNO on Discord, v0.1.815).
func TestPluginModelWindow(t *testing.T) {
clinePlugin(t)
p, err := Find("cline")
if err != nil {
t.Fatal(err)
}
for id, want := range map[string]int{"cline/cline-pass/glm-5.3-flash": 1_000_000, "cline/cline-free/mimo-v2.6-flash": 1_048_576, "cline/stealth/space-bunny-alpha": 1_000_000} {
if got := pluginWindow(t, id); got != want {
t.Errorf("%s: %d, want %d", id, got, want)
}
}
if err := SetContext(*p, "cline-free/mimo-v2.6-flash", 400_000); err != nil {
t.Fatal(err)
}
if got := pluginWindow(t, "cline/cline-free/mimo-v2.6-flash"); got != 400_000 {
t.Errorf("set: %d", got)
}
// the page lists it so too (gui's providerInfo reads WindowOf)
p, _ = Find("cline")
for _, m := range p.Available() {
if m.ID == "cline-free/mimo-v2.6-flash" && (p.WindowOf(m) != 400_000 || ListedWindow(m) != 1_048_576) {
t.Errorf("WindowOf %d, ListedWindow %d", p.WindowOf(m), ListedWindow(m))
}
}
}
// A plugin model naming both a window and a prompt cap keeps the cap, but
// a cap above the window is held to the window: opencode/hy3-free's row
// (190K window, 192K input) was served past its window, and GPT-5's real
// 272K prompt cap still wins over its 400K window (#1286).
func TestPluginCatalogClampsInputAboveWindow(t *testing.T) {
pp := plugin.Provider{ID: "opencode", Models: []plugin.Model{
{ID: "hy3-free", Context: 190_000, Input: 192_000, Output: 64_000},
{ID: "gpt-5-codex", Context: 400_000, Input: 272_000, Output: 128_000},
{ID: "input-only", Input: 200_000, Output: 64_000},
{ID: "context-only", Context: 1_000_000, Output: 64_000},
}}
got := map[string]int{}
for _, m := range pluginCatalog(pp) {
got[m.ID] = m.Context
}
want := map[string]int{
"hy3-free": 190_000,
"gpt-5-codex": 272_000,
"input-only": 200_000,
"context-only": 1_000_000,
}
for id, n := range want {
if got[id] != n {
t.Errorf("%s: %d, want %d", id, got[id], n)
}
}
}
// #3654 (mycf_711): a plugin's model declared at 1048576, its provider
// set to 1000000 for all of its models, is told 1000000 alone and in a
// group of its own that names no size: the same model is never told two
// windows, which had dsh reassemble its history when it moved between
// the two.
func TestGroupOfAPluginModelToldTheWindowSetOnIt(t *testing.T) {
clinePlugin(t)
p, err := Find("cline")
if err != nil {
t.Fatal(err)
}
if err := SetContext(*p, "*", 1_000_000); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{ID: "flash", Name: "flash", Members: []string{"cline/cline-free/mimo-v2.6-flash"}}); err != nil {
t.Fatal(err)
}
alone, group := pluginWindow(t, "cline/cline-free/mimo-v2.6-flash"), pluginWindow(t, GroupPrefix+"flash")
if alone != 1_000_000 || group != alone {
t.Errorf("alone %d, in its group %d: want 1000000 both", alone, group)
}
}