diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index ff8dcdfd0..ae2c84fa5 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -537,6 +537,16 @@ for: `provider_test`, `api_detect` or `model_list` request page lists them as a chunk of their own (`probeChunk`) and adds them to no total, cell, group, chart or filter count; `?via=` other than all leaves them out. +- **A model list answered is no row:** magpie fetches lists by itself + (start-up's `FetchNew`, the panel's and the Providers page's, and + `KeepListsFresh`, which asks a Remote magpie every 10 minutes), so a + `model_list` row with a 2xx/3xx status and no error is neither written + (`usage`'s `probelog.Sink`) nor read back from an older log + (`quietProbe`, in the index's parse): ferock on Discord, a Requests page + full of 0-token rows no agent sent. A list that failed is still a row, + under the Failed filter too, as is every Test and Detect APIs. Older + lines stay in `usage.jsonl`, read by nothing. + `TestAnsweredModelListIsNoRow` covers it. - **Whose:** a row recorded under its host, sent before its provider had an id, is listed under the provider saved on that host now (`Renames.ProbeOf`), and with none on it, a deleted one's, under the id diff --git a/internal/probelog/probelog.go b/internal/probelog/probelog.go index c44985604..dae8aee6f 100644 --- a/internal/probelog/probelog.go +++ b/internal/probelog/probelog.go @@ -3,7 +3,9 @@ // Detect APIs and per-model detection, and a model list's refresh (neiko // and gys619 on Discord: a test left no trace in the request log). Each // such request is one row of the request log, marked as magpie's own test -// (usage.SourceProbe) and never counted in a usage or cost total. +// (usage.SourceProbe) and never counted in a usage or cost total; a model +// list that was answered is left out there (usage's quietProbe), as magpie +// asks for lists by itself all day. // // The provider package marks a context with the kind of test (With); every // request sent under it is written down once, by the provider's Do or by diff --git a/internal/usage/log_index.go b/internal/usage/log_index.go index 129c6053d..341e2f3af 100644 --- a/internal/usage/log_index.go +++ b/internal/usage/log_index.go @@ -179,7 +179,9 @@ func readLogSnapshot() *logSnapshot { } next.sawHost(r) if r.IsProbe() { - next.probes = append(next.probes, probeRow{r, next.off}) + if !quietProbe(r) { // a model list answered is no row (ferock) + next.probes = append(next.probes, probeRow{r, next.off}) + } continue } if tail == nil { diff --git a/internal/usage/probes.go b/internal/usage/probes.go index 3ea11e58c..9f0db4814 100644 --- a/internal/usage/probes.go +++ b/internal/usage/probes.go @@ -34,12 +34,29 @@ type probeRow struct { func init() { probelog.Sink = func(p probelog.Row) { - Append(Record{Time: p.Time, Agent: "magpie", Provider: p.Provider, ProviderAccount: p.Account, + r := Record{Time: p.Time, Agent: "magpie", Provider: p.Provider, ProviderAccount: p.Account, Host: p.Host, Model: p.Model, Requested: p.Model, Status: p.Status, Millis: p.Millis, - Error: clipProbeError(p.Error), Endpoint: p.Endpoint, Kind: p.Kind, Source: SourceProbe}) + Error: clipProbeError(p.Error), Endpoint: p.Endpoint, Kind: p.Kind, Source: SourceProbe} + if quietProbe(r) { + return + } + Append(r) } } +// quietProbe is a request of magpie's own the request log leaves out: a +// model list that was answered. magpie asks for one by itself, at start-up, +// behind the panel and the Providers page, and on a schedule (a Remote +// magpie's every 10 minutes, RemoteMagpieEvery), so the Requests page +// filled with rows of 0 tokens that no agent sent (ferock on Discord). One +// that failed is listed, as is every Test and Detect APIs: a list that +// couldn't be fetched is why models are missing, and the row says why. +// Rows written before this are left out the same way as the log is read +// (logSnapshot.probes); the lines stay in the file, counted nowhere. +func quietProbe(r Record) bool { + return r.Kind == probelog.KindList && r.Status >= 200 && r.Status < 400 && r.Error == "" +} + // clipProbeError keeps an error's first 500 bytes, as a vendor's answer can // be a whole page. func clipProbeError(s string) string { diff --git a/internal/usage/probes_test.go b/internal/usage/probes_test.go index 5861d6f6c..68112d02e 100644 --- a/internal/usage/probes_test.go +++ b/internal/usage/probes_test.go @@ -1,6 +1,9 @@ package usage import ( + "os" + "slices" + "strconv" "strings" "testing" "time" @@ -28,7 +31,7 @@ func TestProbeRowsListedNotCounted(t *testing.T) { Append(Record{Time: now, Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5", Input: 1000, Output: 100, Status: 200}) probelog.Sink(probelog.Row{Time: now.Add(time.Second), Kind: probelog.KindTest, Provider: "relay", Account: "me", Model: "gpt-9", Host: "relay.example", Endpoint: "/v1/chat/completions", Status: 401, Millis: 120, Error: strings.Repeat("้กต", 400)}) - probelog.Sink(probelog.Row{Time: now.Add(2 * time.Second), Kind: probelog.KindList, Provider: "relay", Host: "relay.example", Endpoint: "GET /v1/models", Status: 200, Millis: 30}) + probelog.Sink(probelog.Row{Time: now.Add(2 * time.Second), Kind: probelog.KindList, Provider: "relay", Host: "relay.example", Endpoint: "GET /v1/models", Status: 502, Millis: 30, Error: "bad gateway"}) page := QueryPage(All, Filter{}, 0, 50) if page.Total != 3 || len(page.Rows) != 3 { @@ -85,3 +88,46 @@ func TestProbeRowsListedNotCounted(t *testing.T) { t.Fatalf("provider filter: total %d sum %+v", p.Total, p.Sum) } } + +// ferock on Discord: Usage > Requests filled with "Model list" rows, 0 +// tokens and 200, one every 10 minutes for a Remote magpie, that no agent +// sent. A model list that was answered is no row, neither written nor +// read back from a log that has it from before; one that failed is still +// listed (it is why models are missing), and so is every Test. +func TestAnsweredModelListIsNoRow(t *testing.T) { + reportedHome(t) + now := time.Now().Add(-time.Hour) + list := func(at time.Duration, status int, err string) probelog.Row { + return probelog.Row{Time: now.Add(at), Kind: probelog.KindList, Provider: "remote", Host: "10.0.0.2:3425", + Endpoint: "GET /v1/models", Status: status, Millis: 757, Error: err} + } + probelog.Sink(list(time.Second, 200, "")) + probelog.Sink(list(2*time.Second, 304, "")) + probelog.Sink(list(3*time.Second, 503, "upstream down")) + probelog.Sink(list(4*time.Second, 0, "dial tcp 10.0.0.2:3425: connection refused")) + probelog.Sink(probelog.Row{Time: now.Add(5 * time.Second), Kind: probelog.KindTest, Provider: "remote", Host: "10.0.0.2:3425", + Endpoint: "/v1/chat/completions", Model: "m", Status: 200, Millis: 900}) + // a log written before: its answered lists are left out as it is read + Append(Record{Time: now.Add(6 * time.Second), Agent: "magpie", Provider: "remote", Host: "10.0.0.2:3425", + Endpoint: "GET /v1/models", Kind: probelog.KindList, Status: 200, Millis: 700, Source: SourceProbe}) + + b, err := os.ReadFile(Path()) + if err != nil { + t.Fatal(err) + } + if n := strings.Count(string(b), `"kind":"model_list"`); n != 3 { + t.Fatalf("%d model list lines written, want the 2 that failed and the old one:\n%s", n, b) + } + page := QueryPage(All, Filter{}, 0, 50) + var got []string + for _, r := range page.Rows { + got = append(got, r.Kind+" "+strconv.Itoa(r.Status)) + } + want := []string{"provider_test 200", "model_list 0", "model_list 503"} + if page.Total != 3 || !slices.Equal(got, want) { + t.Fatalf("total %d, rows %v, want %v", page.Total, got, want) + } + if p := QueryPage(All, Filter{Failed: true}, 0, 50); p.Total != 2 { + t.Fatalf("failed filter: total %d", p.Total) + } +} diff --git a/internal/usage/rename_usage_test.go b/internal/usage/rename_usage_test.go index 12c70c38d..68252c755 100644 --- a/internal/usage/rename_usage_test.go +++ b/internal/usage/rename_usage_test.go @@ -25,7 +25,7 @@ func TestRenamedProviderShownOnce(t *testing.T) { } then := time.Now().Add(-time.Hour) Append(Record{Time: then, Agent: "codex", Session: "s1", Provider: "myrelay", Model: "gpt-9", Input: 100, Output: 10, Status: 200}) - probelog.Sink(probelog.Row{Time: then.Add(time.Second), Kind: probelog.KindList, Provider: "myrelay", Host: "relay.example", Endpoint: "GET /v1/models", Status: 200, Millis: 30}) + probelog.Sink(probelog.Row{Time: then.Add(time.Second), Kind: probelog.KindList, Provider: "myrelay", Host: "relay.example", Endpoint: "GET /v1/models", Status: 502, Millis: 30, Error: "bad gateway"}) // read once under the old id, so what is kept of it is built again if p := QueryPage(All, Filter{}, 0, 50); !slices.Equal(p.Providers, []string{"myrelay"}) { t.Fatalf("before: %v", p.Providers) @@ -183,7 +183,7 @@ func TestProbesOfAProviderOnAnotherHostOfItsSiteListIt(t *testing.T) { "https://leee.baby/v1/models", "https://Leee.Baby/v1/models", "https://www.leee.baby:443/v1/models", "https://two.example/v1/models", "https://mine.vercel.app/v1/models", "http://127.0.0.1:7/v1/models", } { - probelog.Note(adding, u, http.MethodGet, then, 200, nil) + probelog.Note(adding, u, http.MethodGet, then, 401, nil) // an answered list is no row (quietProbe) } id, err := provider.Add(provider.Provider{Name: "Leee", Key: "k", Chat: "https://api.leee.baby/v1", Models: []string{"gpt-9"}}) if err != nil {