From 987eac0aaee7cac09c68c4b9992b2ca53c045dd4 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 10 Nov 2026 17:35:08 +0800 Subject: [PATCH] gateway: a reported call keeps the model asked for and the one that answered (req, served) (#3534, realLemonawa on X) POST /v1/magpie/usage/requests took a usage.jsonl line's fields except req and served, and refused a record that carried them, so a call an app reported showed an empty Requested and Served model on the Requests page. Both are now optional fields: req is the model the app asked for (an alias or a group of its own), served the one its vendor's reply said answered. Each is checked as model is (a string, at most 256 characters, no control characters) and kept only as sent; a record without served is never filled from model, so it says nothing of what answered. The GUI already shows Record.Requested and Record.Served. The issue's other metadata (the rest of a gateway row's columns) is left out: only the two models were asked for by name. Run (mote): go test -count=1 -run 'TestUsageIngest|TestReported' ./internal/gateway/ ok. go vet darwin (gateway, usage), GOOS=linux -tags nogui and GOOS=windows go vet ./internal/gateway/ ok. gofmt clean. Broken by hand: req and served taken out of ingestFields again; TestUsageIngestRequestedAndServed went red ("req: not a field a reported record takes"). Not tried: a real app reporting its calls; the full suite runs after the tag. --- docs/reference.md | 5 +++ docs/subsystems/gateway-sessions.md | 9 ++++- internal/gateway/usage_ingest.go | 14 ++++++- internal/gateway/usage_ingest_test.go | 53 +++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 932aa6219..e7a25d961 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -2883,6 +2883,11 @@ curl -s http://127.0.0.1:3425/v1/magpie/usage/requests \ required. `in`, `out`, `cache_read`, `cache_write`, `cache_write_1h`, `reasoning`, `ms`, `ttft_ms`, `first_text_ms`, `flow_ms`, `status`, `cost` (USD), `effort`, `session` and `err` may be left out. +- **Asked and answered:** `req` is the model the app asked for (an alias + or a group of its own) and `served` the one its vendor's reply said + answered (#3534); the Requests page shows them as the requested and + served model. Each is kept as sent: left out, it is empty, never taken + from `model`. - **Speed:** the Requests page tells a reply's speed from `out`, `ms` and `ttft_ms` (ms from the request to the reply's first content: text, reasoning or a tool call). A reply that reasoned (`reasoning` above 0) diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index d08879f40..234609593 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -828,7 +828,7 @@ one record or an array of up to 5000 (32 MiB at most), in `usage.jsonl`'s shape: `t`, `agent`, `provider`, `model` and `rid` are required; `in`, `out`, `cache_read`, `cache_write`, `cache_write_1h`, `reasoning`, `ms`, `ttft_ms`, `first_text_ms`, `flow_ms`, `status`, `cost`, `effort`, -`session` and `err` are optional. +`session`, `err`, `req` and `served` are optional. Any other field, a quoted number, a negative or out-of-range count, a time more than an hour ahead or before 2000-01-01T00:00:00Z refuses the whole post with a 400 that names the record (`error.index`) and the field @@ -851,6 +851,13 @@ however old, so a client's history can be backfilled. was appended since. The reply counts `accepted` and `skipped` and gives each record's `index`, `rid` and `status`, so a re-run of a batch is safe. +- **Asked and answered:** `req` and `served` (#3534) are `Record.Requested` + and `Record.Served`, the Requests page's requested and served model: + what the client asked for (an alias, a group) and what its vendor's + reply said answered. Each is checked as `model` is (a string, at most + 256 characters, no control characters) and kept only as sent; a record + without `served` says nothing of what answered, so it is never filled + from `model`. `TestUsageIngestRequestedAndServed`. - **Pricing:** a record with `cost` keeps it (`Record.OwnCost`, the `rowOwnCost` flag on a packed row). Without one it is priced at magpie's own prices, like a gateway row. diff --git a/internal/gateway/usage_ingest.go b/internal/gateway/usage_ingest.go index aadcda90e..bd8050823 100644 --- a/internal/gateway/usage_ingest.go +++ b/internal/gateway/usage_ingest.go @@ -73,8 +73,12 @@ var ingestEarliest = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) // first_text_ms and flow_ms (#2637) are what a reply's speed is told by // (usage.DecodeOf): without first_text_ms a reply that reasoned tells // none, and without flow_ms one its vendor held and let go in a burst -// isn't told apart from one that streamed. -var ingestFields = []string{"t", "agent", "provider", "model", "in", "out", "cache_read", "cache_write", "cache_write_1h", "reasoning", "ms", "ttft_ms", "first_text_ms", "flow_ms", "status", "rid", "cost", "effort", "session", "err"} +// isn't told apart from one that streamed. req and served (#3534) are +// the model the client asked for (an alias, a group) and the one its +// vendor's reply said answered: the Requested and Served columns. Each is +// kept only as sent, so a record without served says nothing of what +// answered; it is never taken from model. +var ingestFields = []string{"t", "agent", "provider", "model", "req", "served", "in", "out", "cache_read", "cache_write", "cache_write_1h", "reasoning", "ms", "ttft_ms", "first_text_ms", "flow_ms", "status", "rid", "cost", "effort", "session", "err"} // ingestFault is what is wrong with one record of a post. type ingestFault struct { @@ -274,6 +278,12 @@ func parseReported(i int, raw json.RawMessage, now time.Time) (usage.Record, *in if rec.Model, f = text("model", true); f != nil { return rec, f } + if rec.Requested, f = text("req", false); f != nil { + return rec, f + } + if rec.Served, f = text("served", false); f != nil { + return rec, f + } if rec.Effort, f = text("effort", false); f != nil { return rec, f } diff --git a/internal/gateway/usage_ingest_test.go b/internal/gateway/usage_ingest_test.go index a5e34ef31..7396b489f 100644 --- a/internal/gateway/usage_ingest_test.go +++ b/internal/gateway/usage_ingest_test.go @@ -436,3 +436,56 @@ func TestUsageIngestTimesChecked(t *testing.T) { } } } + +// #3534 (Lemonawa): a reported call carries the model the client asked +// for (req) and the one its vendor's reply said answered (served), and +// the Requested and Served columns show them; a record without served +// keeps it unknown, never filled from model. Each is a name, checked as +// model is. +func TestUsageIngestRequestedAndServed(t *testing.T) { + do := ingestServer(t) + both := reportedRecord("named-1", `,"req":"friendly-alias","served":"claude-opus-5-5-20261101"`) + none := reportedRecord("plain-1", "") + if c, out, b := do("127.0.0.1:5000", "["+both+","+none+"]"); c != 200 || out.Accepted != 2 { + t.Fatal(c, b) + } + h := lanGuard(New().Handler()) + r := httptest.NewRequest("GET", "/v1/magpie/usage/requests?period=all&via=reported", nil) + r.RemoteAddr = "127.0.0.1:5000" + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + var l usageapi.Ledger + if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &l) != nil { + t.Fatal(w.Code, w.Body.String()) + } + got := map[string][3]string{} + for _, row := range l.Rows { + got[row.RequestID] = [3]string{row.Model, row.Requested, row.Served} + } + want := map[string][3]string{ + "named-1": {"claude-opus-5-5", "friendly-alias", "claude-opus-5-5-20261101"}, + "plain-1": {"claude-opus-5-5", "", ""}, + } + for rid, w := range want { + if got[rid] != w { + t.Errorf("%s: model, req, served %q, want %q", rid, got[rid], w) + } + } + + long := strings.Repeat("m", ingestMaxText+1) + cases := []struct{ extra, field string }{ + {`,"req":` + `"` + long + `"`, "req"}, + {`,"served":` + `"` + long + `"`, "served"}, + {`,"req":42`, "req"}, + {`,"served":"a\u0007b"`, "served"}, + } + for _, c := range cases { + code, out, b := do("127.0.0.1:5000", "["+reportedRecord("ok-x", "")+","+reportedRecord("bad-x", c.extra)+"]") + if code != 400 || out.Error == nil || out.Error.Param != c.field || out.Error.Index == nil || *out.Error.Index != 1 { + t.Errorf("%s: %d %s", c.extra, code, b) + } + } + if n := reportedLines(t); n != 2 { + t.Fatal("a refused batch wrote; lines:", n) + } +}