Files
magpie/internal/provider/agentwindow_test.go
T
yetone e18aca1038 agents: an agent's own context window, and a routing group's own Compact at (ul3140773077392, NovaStudiosss on X)
ul3140773077392 on X: dsh compacts earlier than other harnesses at the same
window, so one global size per model fits none. An agent's sheet (Agents,
the agent's Settings line) now holds "Context window": a size for every
model, or model=size pairs (provider/model, group/<id>, provider/*, a bare
model id, *). Empty follows each model's own window. It is applied in
provider.CatalogFor, the choke point every agent's model list goes through,
so dsh's contextWindow, Codex's catalog and Claude Code's window all read
it, and a window set this way makes Codex/Claude compact at that window
(Entry.WindowSet). Stored in settings.AgentContexts, kept by a general
save (settingsapi); POST /api/settings/agent-contexts and
`magpie model window <agent> [model=size...|off]`.

NovaStudiosss on X: Routing, a group's Edit, Advanced already had "Context"
(the window agents are told). It now has "Compact at" beside it
(Group.Compact; compactSet reads it before the members' smallest), also
`magpie group ... compact=`.

GUI: agentwindow.js, loaded on demand through agentcompact.js so app.js
stays inside its budget (TestCoreScriptBudget passes; the "Context window
in {agent}" label reuses the existing "Context window" key). Every agent
with fields now has a sheet; agent-prefs' Pi expectation says so.

Run at the pushed head, on mote: go vet (darwin; linux -tags nogui;
windows) on provider, agent, cli, gui, settings, settingsapi; gofmt clean;
go test provider, cli, settings, settingsapi, gui (TestCoreScriptBudget
and cli's route table fixed here), agent -run Dsh; node --test agent-window
and group-compact (chromium and webkit, en zh zh-TW ja de, 900 and 420px),
agent-compact, agent-prefs, compact-at. Reds seen identically on a
detached origin/main 4e0026661: agent-prefs webkit "in the panel a
Settings line..." (#6139) and old-webkit "compat.js is the first" / "styles
hold up in Safari 15.0" (#5919, #3403).

Broken by hand, each red then restored: withAgentWindows out of CatalogFor
(TestAgentOwnContextWindow, TestDshOwnContextWindow); the group branch in
compactSet (TestGroupOwnCompactAt); the settingsapi keep line
(TestSettingsSaveKeepsWhatItDoesNotSend); the window row in the sheet
(agent-window tests); compact dropped from the group save
(group-compact tests).

Not tried: a real dsh with a real account (its compaction is dsh's own;
the test reads the window magpie hands it), the full GUI suite (queued
behind another run on mote), Windows and Linux runs of the new tests.
2026-11-12 23:01:29 +08:00

138 lines
4.7 KiB
Go

package provider
import (
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The window an agent is told for a model can be its own (ul3140773077392
// on X: dsh compacts earlier than the other agents at the same window).
// An agent's entry comes before the provider's own window, is a window the
// user typed (so Codex compacts at all of it), leaves every other agent
// alone, "*" only shortens a window that is known, and taking it away
// gives the model's own again.
func TestAgentOwnContextWindow(t *testing.T) {
isolate(t)
limitsHome(t)
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2"}, Contexts: map[string]int{"m1": 200000, "m2": 100000}}); err != nil {
t.Fatal(err)
}
if err := SetAutoGroups(false); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{ID: "mix", Name: "mix", Members: []string{"relay/m1", "relay/m2"}}); err != nil {
t.Fatal(err)
}
window := func(agent, id string) (int, bool) {
shown, _ := CatalogFor(agent)
for _, e := range shown {
if e.ID == id {
return e.Context, e.WindowSet
}
}
t.Fatalf("%s: no %s in %v", agent, id, ids(shown))
return 0, false
}
if n, set := window("dsh", "relay/m1"); n != 200000 || !set {
t.Fatalf("to begin with: %d %v", n, set)
}
if err := SetAgentContexts("dsh", map[string]int{"relay/m1": 500000, "*": 150000}); err != nil {
t.Fatal(err)
}
for _, c := range []struct {
agent, id string
n int
set bool
}{
{"dsh", "relay/m1", 500000, true}, // its own entry, even past the model's
{"dsh", "relay/m2", 100000, true}, // "*" only shortens: m2's 100K is shorter than 150K
{"codex", "relay/m1", 200000, true}, // the others keep what the provider says
{"pi", "relay/m2", 100000, true},
} {
if n, set := window(c.agent, c.id); n != c.n || set != c.set {
t.Errorf("%s %s: %d %v; want %d %v", c.agent, c.id, n, set, c.n, c.set)
}
}
// "*" shortens a window that is longer than it
if err := SetAgentContexts("dsh", map[string]int{"*": 150000}); err != nil {
t.Fatal(err)
}
if n, set := window("dsh", "relay/m1"); n != 150000 || !set {
t.Errorf("* on a longer window: %d %v", n, set)
}
// a provider's, and a group's own entry
if err := SetAgentContexts("dsh", map[string]int{"relay/*": 64000, "group/mix": 90000}); err != nil {
t.Fatal(err)
}
if n, _ := window("dsh", "relay/m2"); n != 64000 {
t.Errorf("provider/*: %d", n)
}
if n, set := window("dsh", "group/mix"); n != 90000 || !set {
t.Errorf("the group: %d %v", n, set)
}
// the window typed is what Codex-style readers compact at
shown, _ := CatalogFor("dsh")
s := settings.Load()
for _, e := range shown {
if e.ID == "relay/m1" {
if got := CompactFor(s, e, GroupFinder()); got != 64000 {
t.Errorf("compacts at %d; want the whole window, 64000", got)
}
}
}
if err := SetAgentContexts("dsh", map[string]int{"relay/m1": -1}); err == nil {
t.Error("a negative window was accepted")
}
if err := SetAgentContexts("dsh", nil); err != nil {
t.Fatal(err)
}
if n, set := window("dsh", "relay/m1"); n != 200000 || !set {
t.Errorf("taken away: %d %v", n, set)
}
if got := settings.Load().AgentContexts; len(got) != 0 {
t.Errorf("kept %v", got)
}
}
// A routing group carries its own compaction size (NovaStudiosss on X): it
// is where agents compact on the group, before the smallest its members
// say, and with none set nothing changes.
func TestGroupOwnCompactAt(t *testing.T) {
isolate(t)
limitsHome(t)
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2"}}); err != nil {
t.Fatal(err)
}
if err := SetModelCompacts("relay", map[string]int{"m1": 250000, "m2": 300000}); err != nil {
t.Fatal(err)
}
if err := SetAutoGroups(false); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{ID: "mix", Name: "mix", Members: []string{"relay/m1", "relay/m2"}}); err != nil {
t.Fatal(err)
}
if got := CompactSet("group/mix"); got != 250000 {
t.Errorf("none of its own: %d; want its smallest member's", got)
}
if err := SaveGroup(Group{ID: "mix", Name: "mix", Members: []string{"relay/m1", "relay/m2"}, Compact: 160000}); err != nil {
t.Fatal(err)
}
if got := CompactSet("group/mix"); got != 160000 {
t.Errorf("its own: %d", got)
}
if err := SetModelCompacts("relay", nil); err != nil {
t.Fatal(err)
}
if got := CompactSet("group/mix"); got != 160000 {
t.Errorf("its own with no model set: %d", got)
}
if err := SaveGroup(Group{ID: "mix", Name: "mix", Members: []string{"relay/m1"}, Compact: -5}); err != nil {
t.Fatal(err)
}
if g, _, ok := FindGroup(GroupPrefix + "mix"); !ok || g.Compact != 0 {
t.Errorf("a negative size was kept: %+v", g)
}
}