remote quotas: a card is wanted by its whole provider path, not its first part (748975219 on X)

With only relay/strata/model enabled on a Remote, the cards of relay/kiro and
relay/codex still passed, since remoteCardWanted compared the first part of
the card's id alone. A card is now wanted when its id is the provider path of
an enabled model (its id less the last part) or a parent of it, whole parts
only (relay/kir never matches relay/kiro). A model listed with no provider
still leaves it unknown, so all shown. cardWantedFor holds the rule.

Run (mote): TestRemoteCardWantedByWholePath (two levels, three levels,
non-boundary prefixes, several models, one level, no provider) is red with
the first-part rule put back by hand, green with the fix; -run Remote in
internal/provider ok; go vet darwin/linux/windows ok; gofmt clean. Full
internal/provider: TestQuotaSpanCarriesEveryWindowField and
TestKeptQuotaReportAsksNobody are red, and red the same on origin/main
bbfa8501f without this change (open issues 4457/4190 and 3487).
Siblings: remoteOf and ThroughSelf take one leading part on purpose (a
remote's own id), unchanged. Not tried: a real multi-level chain of magpies.
This commit is contained in:
yetone
2026-11-11 22:53:50 +08:00
parent 628f8fd840
commit 93b855d90e
3 changed files with 66 additions and 13 deletions
File diff suppressed because one or more lines are too long
+32 -12
View File
@@ -15,9 +15,10 @@ import (
// 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).
// model of the provider the card is of (remoteCardWanted): the
// card's id is the provider path of one of those models' ids, or a
// parent of it, whole parts only (provider/model, as that remote lists
// them; relay/strata enabled does not tell relay/kiro).
// - 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
@@ -35,18 +36,37 @@ func remoteCardWanted(p Provider) func(id string) bool {
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 {
ids := make([]string, len(ms))
for i, m := range ms {
ids[i] = m.ID
}
return cardWantedFor(ids)
}
// cardWantedFor is remoteCardWanted for the enabled model ids as the remote
// lists them: a model's provider path is its id less the last part
// ("relay/strata/m" is of relay/strata). A card is wanted when its id is
// such a path or one of its parents, whole parts only: relay/strata
// enabled tells relay/strata and relay, never relay/kiro or relay/strat.
// A model with no provider before its name leaves it unknown, so wanted.
func cardWantedFor(ids []string) func(id string) bool {
paths := map[string]bool{}
for _, mid := range ids {
i := strings.LastIndex(mid, "/")
if i <= 0 {
return func(string) bool { return true }
}
heads[head] = true
}
return func(id string) bool {
head, _, _ := strings.Cut(id, "/")
return heads[head]
path := mid[:i]
for {
paths[path] = true
j := strings.LastIndex(path, "/")
if j < 0 {
break
}
path = path[:j]
}
}
return func(id string) bool { return paths[id] }
}
// withoutLoops is cards less those whose way comes back to this magpie.
+33
View File
@@ -668,3 +668,36 @@ func TestRemoteCardsFollowEnabledModelsAndDedupe(t *testing.T) {
}
}
}
// @748975219 on X (0.1.3771): with only relay/strata/model enabled, the
// cards of relay/kiro and relay/codex still passed, as only the first part
// of the card's id was compared.
func TestRemoteCardWantedByWholePath(t *testing.T) {
cases := []struct {
name string
ids []string
yes []string
no []string
}{
{"two levels", []string{"relay/strata/m"}, []string{"relay/strata", "relay"}, []string{"relay/kiro", "relay/codex", "kiro", "relay/strata/x"}},
{"three levels", []string{"a/b/c/m"}, []string{"a/b/c", "a/b", "a"}, []string{"a/b/d", "a/x", "a/b/c/d", "b"}},
{"not at a boundary", []string{"relay/kiro/m"}, []string{"relay/kiro"}, []string{"relay/kir", "relay/kiro2", "relay/kiroo", "rel"}},
{"prefix of the other way", []string{"relay/kir/m"}, []string{"relay/kir"}, []string{"relay/kiro"}},
{"several", []string{"relay/strata/m", "relay/kiro/n"}, []string{"relay/strata", "relay/kiro"}, []string{"relay/codex"}},
{"one level", []string{"kiro/m"}, []string{"kiro"}, []string{"codex", "kiro/x"}},
{"unknown provider", []string{"m"}, []string{"anything", "a/b"}, nil},
}
for _, c := range cases {
w := cardWantedFor(c.ids)
for _, id := range c.yes {
if !w(id) {
t.Errorf("%s: %v should tell %q", c.name, c.ids, id)
}
}
for _, id := range c.no {
if w(id) {
t.Errorf("%s: %v should not tell %q", c.name, c.ids, id)
}
}
}
}