Files
magpie/internal/catalog/magpie_via.go
T
yetone 08a42c40ff provider: a Remote magpie's cards are passed on down a chain of magpies
yuyong_id (Discord) chains three magpies through Remote: magpie1 has a
Codex subscription, magpie2 uses magpie1, magpie3 uses magpie2. magpie2
showed magpie1's Codex card; magpie3 showed none.

Cause: since 8c71cd27f a magpie told only its own cards
(CachedCards kept From == "") and the receiver skipped any card it was
told with a From (remoteCards). So no card from a remote, of any
provider, reached a third magpie; Codex wasn't special. Antigravity and
DeepSeek showing at magpie3 means magpie2 has those accounts itself
(not checked with the reporter).

Fix:
- CachedCards passes on the cards it already holds from each Remote
  magpie (passedOnCards, from the 30s cache: it asks no remote, and a
  held remote's cards stay home).
- Each card told carries Via, the magpies (catalog.Self) it passed
  through; a receiver leaves out a card that comes back to it or has
  passed more than MaxVia magpies (catalog.Loops, the same rule as
  magpie_via for models).
- Refresh on a passed-on card ("m1/codex" at magpie2) goes to
  RefreshUsage, which asks that remote again, instead of a 400.
- Sibling: the status line (KeptQuotaReport, /v1/magpie/quotas?kept=1)
  read CachedCards too, so a magpie's own Remote cards were missing
  from it; now there.
- A held key still sees only own cards (quotaScope unchanged).
- Codex's resets stay with the magpie next door only, as before.
- catalog.Self becomes a func with SetSelfForTest, so one test process
  can play magpie2 and then magpie3.

Run on mote at b9cdac0c6 (rebased on origin/main 8a7f77cb7):
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build ./...: ok.
- New TestRemoteChainPassesOnCards (magpie1 fake -> this gateway as
  magpie2 -> magpie3 reading it: Codex, Antigravity, GLM plan and a
  DeepSeek balance arrive, named "Codex · M1 · M2", Via [m1 m2],
  refresh reaches magpie1, a looped card is dropped),
  TestKeptReportHasRemoteCards and the updated TestRemoteMagpieCards,
  with TestRemoteMagpie*, TestRemoteSelf, Quota* and TestCachedCards:
  ok; -race -count=3 on the new and changed ones: ok.
- On origin/main with only these tests (and the Via field / Self func
  they compile against): all three FAIL. magpie3 gets just the
  "nothing read on that magpie yet" stand-in for magpie2, the kept
  report is empty, and the third magpie's card is nil.
- Full suite (-tags nogui -timeout 45m ./... under the suite lock):
  cut short when mote closed the connection, before any package
  reported. Rerunning at the pushed head.
- Rebased again onto origin/main e0f6eda64 (gateway changed): go vet
  of gateway, provider and catalog and the same targeted tests: ok on
  Linux (wsl-run magpie-desktop, -tags nogui); mote was refusing ssh.

Not tried: three real magpies on separate machines.
Not changed: a passed-on card's quota history (#5747).

Reported-by: yuyong_id (Discord)
2026-11-09 12:47:12 +08:00

84 lines
3.0 KiB
Go

package catalog
import (
"crypto/rand"
"encoding/hex"
"slices"
"sync"
"sync/atomic"
)
// Two magpies can each be the other's Remote magpie (cdredfox on Discord:
// two computers on one Tailscale network). Each one's list to the other
// had the other's own models in it, under its Remote magpie provider, and
// each fetch, every ten minutes (provider.RemoteMagpieEvery), took a turn
// more of the loop: rem/back/own/m1, then rem/back/rem/back/own/m1, … on
// both computers, without end. Every one of them joined its model's found
// group (auto/<model>), and a request to one went round the loop, masked
// again on each computer each time it came back.
//
// So a magpie's list to another magpie names, of each model, the magpies
// it passes through on its way to its vendor (magpie_via): the one
// listing it, and those its own list had from, each by its Self. A list
// that names this magpie and another along the way is of a model that
// leaves here and comes back: it isn't taken. A Remote magpie that is
// this magpie itself (sync brought one to the computer it names,
// hanslin1666 on Discord; the gateway's tests serve their remote from
// one process) has this magpie's own models, through this one alone,
// once: a model that went that way twice or more (rem/rem/own/m1) isn't
// taken either. A list from a magpie from before names nothing, and is
// taken as it was.
// MaxVia is the most magpies a model's way may pass through before a list
// is taken to loop through a magpie from before magpie_via, which
// stamps none of its own.
const MaxVia = 8
// Self is this run of magpie's name in magpie_via: random, made up once a
// run, and told only to a magpie that asks for its list.
func Self() string {
if s := selfForTest.Load(); s != nil {
return *s
}
return self()
}
var self = sync.OnceValue(func() string {
b := make([]byte, 8)
rand.Read(b)
return hex.EncodeToString(b)
})
// selfForTest stands for Self while a test plays another magpie
// (SetSelfForTest).
var selfForTest atomic.Pointer[string]
// SetSelfForTest has Self be s, for a test that plays several magpies
// in one process one after another, until the returned func puts it
// back.
func SetSelfForTest(s string) (restore func()) {
was := selfForTest.Swap(&s)
return func() { selfForTest.Store(was) }
}
// loops reports whether a model another magpie lists by way of via leaves
// this magpie and comes back to it.
func loops(via []string) bool {
if len(via) > MaxVia {
return true
}
return len(via) > 1 && slices.Contains(via, Self())
}
// Loops reports whether what another magpie passes on by way of via (a
// model's magpie_via, a quota card's Via) leaves this magpie and comes
// back to it, or has passed through more magpies than MaxVia.
func Loops(via []string) bool { return loops(via) }
// ViaHere is the magpie_via a model goes out with in this magpie's list
// to another: the way it came here by, when it came from another magpie,
// then this one.
func ViaHere(came []string) []string {
return append(slices.Clip(came), Self())
}