remote quotas: a Remote's cards follow its enabled models, one reading per account, loops dropped (748975219 on X)
A Remote magpie's quota cards ignored which models the Remote has enabled, so providers it does not serve (Kiro, Codex) showed up, and an A to B to C to A cycle grew paths like remote-B/remote-C/remote-A/remote-B/remote-C/kiro with snapshots hours apart. catalog.Self() is random per run, so after a restart the stale copies in other caches no longer looked like ours. - catalog.loops: a magpie id repeated inside one Via is a loop, restart-proof. - namedRemoteCards keeps only cards whose provider head is among the Remote's exposed model heads (none served: none; no list: all). - CachedCards and RemoteCards keep one card per subscription account (kind, leaf provider, user): the newest complete reading, then the shorter path. Key plan and balance cards are not merged. - keepRemoteCards evicts looped copies before caching. - TestRemoteChainPassesOnCards: magpie3 now knows magpie2 alone, as the same account by two paths is shown once. Run (WSL, magpie-desktop): gofmt -l clean, go vet linux and windows, go test internal/catalog and internal/provider ok, gateway Remote/Quota subset ok. Broken by hand: no enabled-model filter, no dedupe and no repeated-id rule each turn TestRemoteCardsFollowEnabledModelsAndDedupe or TestLoopsOnARepeatedMagpie red. Not tried: a real A to B to C to A cycle in sandbox HOMEs (fake peer in test), the GUI suite, win-run, the Usage page's own-versus-remote copies.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -67,7 +67,18 @@ func loops(via []string) bool {
|
||||
if len(via) > MaxVia {
|
||||
return true
|
||||
}
|
||||
return len(via) > 1 && slices.Contains(via, Self())
|
||||
if len(via) > 1 && slices.Contains(via, Self()) {
|
||||
return true
|
||||
}
|
||||
// a magpie twice on one way is a loop through magpies this one may
|
||||
// not know by name (this magpie restarted since, so its old name is
|
||||
// no longer Self)
|
||||
for i, v := range via {
|
||||
if slices.Contains(via[:i], v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Loops reports whether what another magpie passes on by way of via (a
|
||||
|
||||
@@ -67,3 +67,23 @@ func TestFetchLeavesOutWhatCameFromHere(t *testing.T) {
|
||||
t.Errorf("an older magpie's model = %v, %v; want kept, through nothing said", v, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// A way that names one magpie twice came round, even through magpies this
|
||||
// one doesn't know by name: this magpie restarted since, and its old
|
||||
// name is not Self.
|
||||
func TestLoopsOnARepeatedMagpie(t *testing.T) {
|
||||
defer SetSelfForTest("me")()
|
||||
for via, want := range map[string]bool{
|
||||
"a": false,
|
||||
"a,b,c": false,
|
||||
"me": false,
|
||||
"a,me": true,
|
||||
"c,b,a,b": true,
|
||||
"a,b,c,a2,b,c": true,
|
||||
"a,a": true,
|
||||
} {
|
||||
if got := Loops(strings.Split(via, ",")); got != want {
|
||||
t.Errorf("Loops(%s) = %v, want %v", via, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,13 @@ func TestRemoteChainPassesOnCards(t *testing.T) {
|
||||
w.Write(told)
|
||||
}))
|
||||
defer m2.Close()
|
||||
// one process plays both magpies; magpie3 knows magpie2 alone, as the
|
||||
// same account by two paths would be shown once
|
||||
m1Saved := provider.Provider{ID: "m1", Name: "M1", Preset: provider.RemoteMagpiePreset, Key: "sk-magpie-m1", Chat: m1.URL}
|
||||
if err := provider.Delete("m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider.ForgetRemoteCardsForTest()
|
||||
if err := provider.Save(provider.Provider{ID: "m2", Name: "M2", Preset: provider.RemoteMagpiePreset, Key: "sk-magpie-m2", Chat: m2.URL}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -136,6 +143,12 @@ func TestRemoteChainPassesOnCards(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := provider.Delete("m2"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := provider.Save(m1Saved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// a refresh pressed at magpie3 is read again at magpie1, by magpie2
|
||||
res, err := http.Post(gw.URL+provider.RemoteRefreshPath+"?provider=m1/codex&user=a@x.com", "", nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -154,7 +154,7 @@ func CachedCards(now time.Time) []SubscriptionQuota {
|
||||
}
|
||||
}
|
||||
}
|
||||
return append(out, passedOnCards()...)
|
||||
return oneCardPerAccount(append(out, passedOnCards()...))
|
||||
}
|
||||
|
||||
// passedOnCards are the cards of each Remote magpie this one uses, as
|
||||
@@ -266,7 +266,7 @@ func RemoteCards(ctx context.Context) []SubscriptionQuota {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return slices.Concat(got...)
|
||||
return oneCardPerAccount(slices.Concat(got...))
|
||||
}
|
||||
|
||||
// remoteCards is p's cards, from what was kept here when it is recent and
|
||||
@@ -308,9 +308,10 @@ func remoteCards(ctx context.Context, p Provider, fresh bool) []SubscriptionQuot
|
||||
// model listed that way is (catalog.Loops).
|
||||
func namedRemoteCards(p Provider, cards []SubscriptionQuota) []SubscriptionQuota {
|
||||
name := remoteName(p)
|
||||
wanted := remoteCardWanted(p)
|
||||
out := make([]SubscriptionQuota, 0, len(cards))
|
||||
for _, q := range cards {
|
||||
if q.Provider == "" || catalog.Loops(q.Via) {
|
||||
if q.Provider == "" || catalog.Loops(q.Via) || !wanted(q.Provider) {
|
||||
continue
|
||||
}
|
||||
q.Provider = p.ID + "/" + q.Provider
|
||||
@@ -404,7 +405,8 @@ func keepRemoteCards(id string, asked time.Time, cards []SubscriptionQuota, err
|
||||
case err != nil:
|
||||
r.err = err.Error()
|
||||
default:
|
||||
r.cards, r.err, r.held = cards, "", false
|
||||
// a card that came back round isn't kept, nor told again
|
||||
r.cards, r.err, r.held = withoutLoops(cards), "", false
|
||||
}
|
||||
if c.m == nil {
|
||||
c.m = map[string]remoteCardsRead{}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
)
|
||||
|
||||
// Which of a Remote magpie's cards are shown, and which of several copies
|
||||
// of one account's card (X on 0.1.3678: three magpies each other's Remote,
|
||||
// A → B → C → A, and A's "only one local model" Remote still told Kiro and
|
||||
// Codex, by remote-B/kiro and remote-B/remote-C/remote-A/remote-B/…/kiro,
|
||||
// the same account at different hours).
|
||||
//
|
||||
// - A card is shown for a Remote only when the Remote is enabled for a
|
||||
// model of the provider the card is of (remoteCardWanted): the first
|
||||
// part of the card's id is the first part of those models' ids
|
||||
// (provider/model, as that remote lists them).
|
||||
// - A card whose way names a magpie twice, or this one and another, is
|
||||
// no card (catalog.Loops): it isn't kept (keepRemoteCards) nor told.
|
||||
// - One account's copies are one card, the newest complete reading
|
||||
// (oneCardPerAccount), whatever the way each came by.
|
||||
|
||||
// remoteCardWanted says whether a card of provider id (as the remote p
|
||||
// tells it: "kiro", "remote-C/kiro") is of a provider p is enabled for.
|
||||
// It is unknown, and so wanted, while p's list isn't read, or a model of
|
||||
// it is listed with no provider before its name.
|
||||
func remoteCardWanted(p Provider) func(id string) bool {
|
||||
if p.ServesNone() {
|
||||
return func(string) bool { return false }
|
||||
}
|
||||
ms := p.Exposed()
|
||||
if len(ms) == 0 {
|
||||
return func(string) bool { return true }
|
||||
}
|
||||
heads := map[string]bool{}
|
||||
for _, m := range ms {
|
||||
head, _, ok := strings.Cut(m.ID, "/")
|
||||
if !ok {
|
||||
return func(string) bool { return true }
|
||||
}
|
||||
heads[head] = true
|
||||
}
|
||||
return func(id string) bool {
|
||||
head, _, _ := strings.Cut(id, "/")
|
||||
return heads[head]
|
||||
}
|
||||
}
|
||||
|
||||
// withoutLoops is cards less those whose way comes back to this magpie.
|
||||
func withoutLoops(cards []SubscriptionQuota) []SubscriptionQuota {
|
||||
if !slices.ContainsFunc(cards, func(q SubscriptionQuota) bool { return catalog.Loops(q.Via) }) {
|
||||
return cards
|
||||
}
|
||||
out := make([]SubscriptionQuota, 0, len(cards))
|
||||
for _, q := range cards {
|
||||
if !catalog.Loops(q.Via) {
|
||||
out = append(out, q)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// cardAccountKey is what one account's copies share, whatever the way they
|
||||
// came by; "" for a card that is no account's (a remote's own stand-in,
|
||||
// a gateway key's limit, a key).
|
||||
func cardAccountKey(q SubscriptionQuota) string {
|
||||
// a key's User is its name, which two keys may share
|
||||
if q.GatewayKey != nil || q.Provider == "" || q.Kind != "subscription" {
|
||||
return ""
|
||||
}
|
||||
leaf := q.Provider[strings.LastIndex(q.Provider, "/")+1:]
|
||||
if q.User != "" {
|
||||
return q.Kind + "\x00" + leaf + "\x00" + strings.ToLower(strings.TrimSpace(q.User))
|
||||
}
|
||||
// a key has no one's name: two keys of one name stay two cards
|
||||
return ""
|
||||
}
|
||||
|
||||
func cardComplete(q SubscriptionQuota) bool {
|
||||
return q.Error == "" && (len(q.Windows) > 0 || q.Balance != "")
|
||||
}
|
||||
|
||||
func cardReadAt(q SubscriptionQuota) time.Time {
|
||||
switch {
|
||||
case q.ReadAt != nil:
|
||||
return *q.ReadAt
|
||||
case q.AsOf != nil:
|
||||
return *q.AsOf
|
||||
case q.From == "":
|
||||
// this computer's own, with no time said, is just read
|
||||
return time.Now()
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// newerCard says whether a is to be shown in place of b, copies of one
|
||||
// account: a complete reading over an incomplete one, the later read,
|
||||
// then the shorter way.
|
||||
func newerCard(a, b SubscriptionQuota) bool {
|
||||
if ca, cb := cardComplete(a), cardComplete(b); ca != cb {
|
||||
return ca
|
||||
}
|
||||
if ta, tb := cardReadAt(a), cardReadAt(b); !ta.Equal(tb) {
|
||||
return ta.After(tb)
|
||||
}
|
||||
return len(a.Via) < len(b.Via)
|
||||
}
|
||||
|
||||
// oneCardPerAccount is cards with one card of each account: the newest
|
||||
// complete reading of its copies, in the place of the first copy.
|
||||
func oneCardPerAccount(cards []SubscriptionQuota) []SubscriptionQuota {
|
||||
best := map[string]int{}
|
||||
drop := map[int]bool{}
|
||||
for i, q := range cards {
|
||||
k := cardAccountKey(q)
|
||||
if k == "" {
|
||||
continue
|
||||
}
|
||||
j, ok := best[k]
|
||||
if !ok {
|
||||
best[k] = i
|
||||
continue
|
||||
}
|
||||
if newerCard(q, cards[j]) {
|
||||
drop[j] = true
|
||||
best[k] = i
|
||||
} else {
|
||||
drop[i] = true
|
||||
}
|
||||
}
|
||||
if len(drop) == 0 {
|
||||
return cards
|
||||
}
|
||||
out := make([]SubscriptionQuota, 0, len(cards)-len(drop))
|
||||
for i, q := range cards {
|
||||
if !drop[i] {
|
||||
out = append(out, q)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -597,3 +597,74 @@ func TestRemoteMagpieRefreshWaitedFor(t *testing.T) {
|
||||
t.Fatalf("after the remote's refresh of every card: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// X on 0.1.3678: three magpies each other's Remote (A → B → C → A). A's
|
||||
// Remote B was enabled for one local model, and still told Kiro and
|
||||
// Codex, by B/kiro and B/C/A/B/C/kiro, the same account read at different
|
||||
// hours. A card is shown for the providers the Remote is enabled for, a
|
||||
// card whose way repeats a magpie is neither kept nor told, and one
|
||||
// account's copies are the newest complete reading.
|
||||
func TestRemoteCardsFollowEnabledModelsAndDedupe(t *testing.T) {
|
||||
old, fresh := time.Now().Add(-5*time.Hour), time.Now().Add(-time.Minute)
|
||||
peer := &fakePeer{cards: []SubscriptionQuota{
|
||||
{Provider: "kiro", Name: "Kiro", Kind: "subscription", User: "k@x.com", Via: []string{"c", "b"}, ReadAt: &old, Windows: []QuotaWindow{{Name: "credits", Used: 10}}},
|
||||
{Provider: "c/kiro", Name: "Kiro · C", Kind: "subscription", User: "k@x.com", Via: []string{"c", "b"}, ReadAt: &fresh, Windows: []QuotaWindow{{Name: "credits", Used: 30}}},
|
||||
{Provider: "codex", Name: "Codex", Kind: "subscription", User: "a@x.com", Via: []string{"b"}, Windows: []QuotaWindow{{Name: "5h", Used: 40}}},
|
||||
{Provider: "c/a/b/c/zed", Name: "Zed", Kind: "subscription", User: "z@x.com", Via: []string{"a", "b", "c", "a2", "b", "c"}, Windows: []QuotaWindow{{Name: "credits", Used: 99}}},
|
||||
}}
|
||||
remoteQuotaHome(t, peer)
|
||||
ctx := context.Background()
|
||||
// before the list is read, nothing says what the remote is enabled for
|
||||
got := map[string]SubscriptionQuota{}
|
||||
for _, q := range RemoteCards(ctx) {
|
||||
if q.Kind == "subscription" {
|
||||
got[q.Provider] = q
|
||||
}
|
||||
}
|
||||
if len(got) != 2 || got["office/c/kiro"].Windows[0].Used != 30 || got["office/codex"].Name == "" {
|
||||
t.Fatalf("one Kiro, the newest, and Codex: %+v", got)
|
||||
}
|
||||
for _, q := range got {
|
||||
if len(q.Via) > 3 {
|
||||
t.Errorf("a card that went round was kept: %+v", q)
|
||||
}
|
||||
}
|
||||
// enabled for one model of the remote's own Codex alone
|
||||
list := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`{"object":"list","data":[{"id":"codex/gpt-6","object":"model","owned_by":"codex","magpie_via":["b"]},{"id":"kiro/claude","object":"model","owned_by":"kiro","magpie_via":["b"]}]}`))
|
||||
}))
|
||||
defer list.Close()
|
||||
p, err := Find("office")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
peerURL, anth := p.Chat, p.Anthropic
|
||||
p.Chat, p.Anthropic = list.URL, list.URL
|
||||
p.Models = []string{"codex/gpt-6"}
|
||||
if err := Save(*p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, _ = Find("office")
|
||||
if _, err := p.Fetch(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.Chat, p.Anthropic = peerURL, anth
|
||||
if err := Save(*p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ForgetRemoteCardsForTest()
|
||||
got = map[string]SubscriptionQuota{}
|
||||
for _, q := range RemoteCards(ctx) {
|
||||
if q.Kind == "subscription" {
|
||||
got[q.Provider] = q
|
||||
}
|
||||
}
|
||||
if _, ok := got["office/codex"]; !ok || len(got) != 1 {
|
||||
t.Fatalf("a Remote enabled for Codex alone tells Codex alone: %+v", got)
|
||||
}
|
||||
for _, q := range CachedCards(time.Now()) {
|
||||
if strings.Contains(q.Provider, "kiro") {
|
||||
t.Errorf("a card of a provider the Remote isn't enabled for is passed on: %+v", q)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user