Entry.Provider was a Provider value, 1,328 of an Entry's 1,760 bytes, copied into every model of every provider each time a catalog, an agent's list or a routing group's entries were built. It is now a *Provider: entriesOf and decide's list give every model of one provider the same one (sharedEntry), and a group's entry points at its first member's. It is read only; every place that hands it on as a value (Resolve, CanFast, ownAgent, modelAllowed, …) dereferences it, so what they get is a copy as before. entryFor keeps its signature for the one model ServedEntryOf builds; offEntries' entries now carry their provider too. Measured on mote with the owner's real accounts (acctp sandbox, page open for 2m32s, w40 = before, w41 = this, alternating): - catalogOf's allocations 106 MB -> 67 MB, providerEntries' 88 -> 78 MB - CPU at the page's settle mark 3.42/3.33 s -> 3.18/3.30 s - live heap unchanged (76.3/76.1 -> 76.7/75.9 MB): the lists are rebuilt, not kept, so the copies were churn for the GC, not held Run: go build and go vet ./... and the tests compiled (go test -run ^$) on mote; the full suite at this tree (d1e823d5f) on mica: 99 packages ok, 0 FAIL (agent 317s, gateway 972s, gui 188s, provider 220s, usage 56s).
117 lines
4.4 KiB
Go
117 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// movedDshCompact is the reason DSH's compaction went on the model set for
|
|
// it (provider.DshCompactModel) rather than the one it asked for.
|
|
const movedDshCompact = "dsh-compact"
|
|
|
|
// movedDshCompactRested is the reason DSH's compaction went on the same
|
|
// model from another provider: the one it asked for rests out of its
|
|
// limit (dshCompactElsewhere).
|
|
const movedDshCompactRested = "dsh-compact-rested"
|
|
|
|
// dshCompactTo is the model a DeepSeek Harness compaction (kind, as
|
|
// serve recorded it: DSH's mark, or its compaction ask on a route that
|
|
// sends no mark) goes on: the one set for DSH's compactions, "" to leave
|
|
// it on asked. A switch away from a model whose provider is out of its
|
|
// limit compacts on that model first, and failed there (#5395). The model
|
|
// set must be enabled, served by its exact id and allowed to the gateway
|
|
// key; when it isn't, the compaction goes on as asked and the log says
|
|
// why. The cheap checks come first, so an unset model costs a held
|
|
// settings read.
|
|
func dshCompactTo(r *http.Request, agent, kind, asked string) string {
|
|
if agent != "dsh" || kind != KindCompact {
|
|
return ""
|
|
}
|
|
to := provider.DshCompactModel()
|
|
if to == "" || to == asked {
|
|
return ""
|
|
}
|
|
if why := compactRefused(r, to); why != "" {
|
|
log.Printf("gateway: DeepSeek Harness's compaction stays on %s, not %s: %s", asked, to, why)
|
|
return ""
|
|
}
|
|
return to
|
|
}
|
|
|
|
// compactRefused says why a compaction can't be put on to, a model or
|
|
// routing group: disabled, not served by that exact id, or not the
|
|
// gateway key's to use; "" when it can.
|
|
func compactRefused(r *http.Request, to string) string {
|
|
if _, off := idOff(to); off {
|
|
return "it is disabled in magpie"
|
|
}
|
|
if w := provider.Unserved(to); w != "" {
|
|
return w
|
|
}
|
|
if who, held := keyHolds(r); held || withGatewayKey(r) {
|
|
g, ms, isGroup := provider.FindGroup(to)
|
|
p, model, ok := provider.Resolve(to)
|
|
if isGroup && !groupGiven(r, who, held, g, ms) || held && !isGroup && (!ok || !modelAllowed(who, p, model)) {
|
|
return "the gateway key may not use it"
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// dshCompactElsewhere is the model a DeepSeek Harness compaction asked on
|
|
// asked goes on instead, "" to leave it: when every account of asked's
|
|
// provider rests out of its limit, the same model (as magpie's found
|
|
// groups merge models, provider.AutoGroupOf) from another provider that
|
|
// isn't resting, the largest window first. DSH compacts on the session's
|
|
// previous model before a switch to one with a smaller window, so a
|
|
// switch away from a provider out of its limit failed on that provider's
|
|
// refusal, and the model switched to was never asked (qtwaiter on X:
|
|
// WorkBuddy's deepseek-v4.1-flash out of its rate, Trae CN's
|
|
// deepseek-v4.1-flash picked, "Cannot switch to magpie/trae-cn/
|
|
// deepseek-v4.1-flash: context compaction failed: 429 ... WorkBuddy").
|
|
// Without magpie DSH's own DeepSeek routes all have 1M windows and never
|
|
// compact on a switch. A routing group's request is left: the group
|
|
// already goes past a resting member. The cheap checks come first: only
|
|
// a DSH compaction on a resting model reads the served list.
|
|
func (s *Server) dshCompactElsewhere(r *http.Request, agent, kind, asked string, from provider.Protocol) string {
|
|
if agent != "dsh" || kind != KindCompact {
|
|
return ""
|
|
}
|
|
if _, _, isGroup := provider.FindGroup(asked); isGroup {
|
|
return ""
|
|
}
|
|
p, model, ok := provider.Resolve(asked)
|
|
if !ok || !s.allResting(p, model, from) {
|
|
return ""
|
|
}
|
|
key := provider.AutoGroupOf(p.ID, model)
|
|
best, window := "", -1
|
|
for _, e := range provider.Served() {
|
|
if e.Group != "" || e.Provider.ID == p.ID || e.Context <= window || provider.AutoGroupOf(e.Provider.ID, e.Model) != key {
|
|
continue
|
|
}
|
|
if compactRefused(r, e.ID) != "" || s.allResting(*e.Provider, e.Model, from) {
|
|
continue
|
|
}
|
|
best, window = e.ID, e.Context
|
|
}
|
|
if best == "" {
|
|
log.Printf("gateway: DeepSeek Harness's compaction stays on %s, out of its limit: no other provider serves %s", asked, model)
|
|
}
|
|
return best
|
|
}
|
|
|
|
// allResting says whether every candidate a request for p's model would
|
|
// try rests out of its limit, so it would be answered with the refusal
|
|
// held for it (quotaHeld, timedHeld) without asking anyone.
|
|
func (s *Server) allResting(p provider.Provider, model string, from provider.Protocol) bool {
|
|
cs := s.candidates(p, model, from)
|
|
if _, _, ok := quotaHeld(cs); ok {
|
|
return true
|
|
}
|
|
_, _, ok := timedHeld(cs)
|
|
return ok
|
|
}
|