diff --git a/internal/agent/window_typed_test.go b/internal/agent/window_typed_test.go index 8a1570b09..9c7f7e16c 100644 --- a/internal/agent/window_typed_test.go +++ b/internal/agent/window_typed_test.go @@ -7,6 +7,7 @@ import ( "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" ) @@ -112,3 +113,49 @@ func TestTypedWindowReachesCodexAndClaudeCode(t *testing.T) { t.Errorf("Claude Code on the group with Compact at 300K: %q", c) } } + +// #2224 (Zypixh): a group that follows its smallest member is told that +// member's window, and when the user typed it (500K on the provider for +// gpt-6.1-sol) Claude Desktop and Codex run to it whole, as they do for a +// group set to 500K itself, not to magpie's default 272K. A group in a +// group passes it on. A group whose smallest member's window nobody typed +// keeps the default. +func TestSmallestMemberTypedWindow(t *testing.T) { + home, path := desktopWindowSandbox(t) + must := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + must(provider.Save(provider.Provider{ID: "w", Name: "W", Key: "k", Chat: "http://127.0.0.1:1/v1", + Models: []string{"sol", "other"}, Contexts: map[string]int{"sol": 500000}})) + must(catalog.SaveLive("w", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "sol", Context: 1000000}, {ID: "other", Context: 600000}})) + must(provider.SaveGroup(provider.Group{ID: "pool", Name: "Pool", Members: []string{"w/sol", "v/glm"}, Context: provider.ContextSmallest})) + must(provider.SaveGroup(provider.Group{ID: "outer", Name: "Outer", Members: []string{"group/pool", "v/glm"}, Context: provider.ContextSmallest})) + must(provider.SaveGroup(provider.Group{ID: "plain", Name: "Plain", Members: []string{"w/other", "v/glm"}, Context: provider.ContextSmallest})) + + // Codex: the gateway's list and its context_window + must(codex(home).Fields[0].Set("group/pool")) + compacts := map[string]int{} + for _, m := range provider.CodexListed() { + compacts[m.ID] = m.Compact + } + for id, want := range map[string]int{"group/pool": 500000, "group/outer": 500000, "group/plain": 0} { + if compacts[id] != want { + t.Errorf("Codex: the gateway's list compacts %s at %d; want %d", id, compacts[id], want) + } + } + + // Claude Desktop: each group's own autoCompactWindow + must(claudeDesktop(home).Field("provider").Set("magpie")) + ids := map[string]string{} + for _, e := range provider.Catalog() { + ids[e.ID] = gateway.DesktopID(e) + } + for id, want := range map[string]string{"group/pool": "500000", "group/outer": "500000", "group/plain": "272000"} { + if v, _ := edit.GetJSON(path, budgetPath(ids[id])); v != want { + t.Errorf("Claude Desktop: %s (%s) compacts at %q; want %q", id, ids[id], v, want) + } + } +} diff --git a/internal/provider/compact.go b/internal/provider/compact.go index b47a008cf..a16e1a4ea 100644 --- a/internal/provider/compact.go +++ b/internal/provider/compact.go @@ -53,14 +53,30 @@ func CompactFor(s settings.Settings, e Entry, find func(string) (Group, []Member // WindowTyped says the window agents are told for a model, by its // catalog id (as CompactSet takes it), is one the user typed: the -// group's own size, or one set on the provider for the model or all of -// its models. Claude Code then isn't handed magpie's default compaction -// window for it (CompactFor's rule). +// group's own size, a group's smallest member's that the user typed +// (smallestWindow, #2224), or one set on the provider for the model or +// all of its models. Claude Code then isn't handed magpie's default +// compaction window for it (CompactFor's rule, as Entry.WindowSet). func WindowTyped(id string) bool { id = catalogID(id) if strings.HasPrefix(id, GroupPrefix) { - g, _, ok := GroupFinder()(id) - return ok && g.Context > 0 + gid, ok := GroupIDOf(id) + if !ok { + return false + } + entries := providerEntries() + all := groupsIn(entries) + g, ok := groupOf(all, gid) + switch { + case !ok || g.Disabled: + return false + case g.Context > 0: + return true + case g.Context == ContextSmallest: + _, typed := smallestWindow(entries, membersIn(entries, all, g)) + return typed + } + return false } pid, model, ok := strings.Cut(id, "/") if !ok { diff --git a/internal/provider/group.go b/internal/provider/group.go index 83131ac57..786e0cfe3 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -774,7 +774,6 @@ func groupEntries(entries []Entry) []Entry { // gateway sends max to the members that have no ultra ultra := false chatgpt := false // a ChatGPT account answers for a member - smallest := 0 // the smallest window a member is known to have for i, m := range ms { chatgpt = chatgpt || m.Provider.Account != nil && m.Provider.Account.Agent == "codex" if !slices.ContainsFunc(ms[:i], func(o Member) bool { return o.Provider.ID == m.Provider.ID }) { @@ -822,9 +821,6 @@ func groupEntries(entries []Entry) []Entry { if ctx > e.Context { e.Context = ctx } - if ctx > 0 && (smallest == 0 || ctx < smallest) { - smallest = ctx - } if i == 0 { e.ImageInput = imageInput } else { @@ -899,8 +895,8 @@ func groupEntries(entries []Entry) []Entry { ruledEntry(&e, g.Live(), ms, entries) if g.Context > 0 { e.Context, e.WindowSet = g.Context, true - } else if g.Context == ContextSmallest && smallest > 0 { - e.Context = smallest + } else if smallest, typed := smallestWindow(entries, ms); g.Context == ContextSmallest && smallest > 0 { + e.Context, e.WindowSet = smallest, typed } e.Family = g.Family out = append(out, e) @@ -908,6 +904,33 @@ func groupEntries(entries []Entry) []Entry { return out } +// smallestWindow is the smallest window a member of ms is known to have +// (0 when none is), and whether the user typed it, on the member's +// provider for its model or all of its models. A group told its +// smallest member's window is then told one the user typed (#2224: a +// group following its smallest member, a 500K the user set on it, was +// handed to Claude Desktop and Codex as magpie's default 272K, which a +// group set to 500K itself wasn't). A tie counts as typed: the window is +// the user's either way. +func smallestWindow(entries []Entry, ms []Member) (n int, typed bool) { + for _, m := range ms { + ctx, set := 0, false + for _, x := range entries { + if x.Provider.ID == m.Provider.ID && x.Model == m.Model { + ctx, set = x.Context, x.WindowSet + } + } + switch { + case ctx <= 0: + case n == 0 || ctx < n: + n, typed = ctx, set + case ctx == n: + typed = typed || set + } + } + return n, typed +} + // levelsUnknown reports whether nothing magpie reads says which reasoning // levels the model of x takes, or that it takes none: it has none in the // catalog, no account's list gives it, its vendor and its maker don't list