Files
magpie/internal/gateway/deferred_usage_test.go
T
yetone 9916a94608 gateway, usage, plugins: OTel export is the first plugin on the gateway's observer point (P3)
P3 of docs/subsystems/plugin-runtime.md opens the gateway to plugins. The
observer point is first: it only reads, so it carries the least risk.
With plugins/otel off, the gateway exports nothing and does no observing
work. It keeps no reply whole and adds no request context.

- usage.Observer:
  - Methods: Serve, Bodies, Begin(Request) Watch and Ledger(Record). Watch
    has Attempt and End.
  - Observe(o) registers an observer until its remove is called.
  - ServeObservers is counted, and only Server.ListenAndServe calls it.
  - ObservedBodies takes the most any observer wants.
  - Record.SkipOTel becomes Watched, set by appendUsage for a request an
    observer watched.
- The gateway's otel.go becomes observe.go:
  - observeRequest asks each observer through Begin.
  - attempt and end call each Watch. Bodies are added only when an
    observer asks for them, and the whole reply only for WholeBodies.
- internal/otlp: the exporter (otel*.go) moves out of internal/usage, and
  the gateway's per-request span state moves into request.go. otlp.New()
  is an Exporter, a usage.Observer. What it sends is unchanged.
- plugins/otel:
  - Apply registers otlp.New() and disposes of it when turned off.
  - InUse is 1 while settings.OTelExport says to send. A user who exports
    only through MAGPIE_OTEL_* keeps the plugin on under Essentials.
- pluginrt.KeepFresh: the process serving the gateway refreshes every 2s
  when plugins.yml or settings.json changes. A switch made in the window or
  the CLI reaches a running gateway, or a headless serve, without a restart.
- docs: plugin-runtime.md gets a P3 section, a gateway runtime path and an
  updated failure section; reference.md says the export is the otel plugin.

Run at this tree on origin/main 82f48f093 (rebased onto 52ee84761 after,
which touches only the Linux tray icon):
- gofmt -l is clean. go vet passes on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- go test -count=1 -timeout 25m ./...: ok, 55 packages, none failed.
- New tests:
  - usage: TestObserverServesWithTheGateway and TestObserverLedgerAndBodies.
  - gateway: TestObserverPoint, covering unwatched, no bodies, captured,
    whole and whole streamed.
  - plugins/otel: TestOnExportsOffSendsNothing (on, off, on, with a real
    collector) and TestInUseByEnvironment.
  - pluginrt: TestKeepFreshFollowsPluginsYml.
  - The OTel tests moved with their code to internal/otlp.

Broken by hand, each red, then restored:
- Apply without Observe, and without its dispose.
- KeepFresh refreshing only once.
- InUse reading settings.json only.
- Ledger keeping Watched rows: 3 gateway OTel tests red.
- ObservedBodies taking the last observer's answer.
- Observe not serving a late registration.
- appendUsage not setting Watched.
- Whole bodies never kept.
- Bodies always on with no observer asking.
2026-10-15 05:39:01 +08:00

202 lines
8.8 KiB
Go

package gateway
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// The Cursor plugin's step that collected an earlier step's late usage
// event (yetone/magpie#1053) tells it apart, as magpie_deferred: the
// step's prompt_tokens stay its own, which a client reads as how full its
// context is (pi read 366940 = 181270 + 185670 of a 300k window, where
// 185670 was used), and magpie bills both.
const deferredJSON = `{"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":185670,"completion_tokens":2240,"total_tokens":187910,"prompt_tokens_details":{"cached_tokens":181266,"cache_write_tokens":4400},"magpie_deferred":{"prompt_tokens":181270,"completion_tokens":1913,"cached_tokens":177533,"cache_write_tokens":3735}}}`
var deferredStream = "data: " + deferredJSON + "\n\ndata: [DONE]\n\n"
func TestSniffChatDeferredUsage(t *testing.T) {
s := newSniffer(provider.Chat, "text/event-stream")
s.write([]byte(deferredStream))
got := s.usage()
if d := got.Deferred; d == nil || *d != (Usage{Input: 2, Output: 1913, CacheRead: 177533, CacheWrite: 3735}) {
t.Fatalf("deferred %+v", got.Deferred)
}
got.Deferred = nil
if own := (Usage{Input: 4, Output: 2240, CacheRead: 181266, CacheWrite: 4400, ResponseID: "c", Stop: "tool_calls"}); got != own {
t.Fatalf("own %+v want %+v", got, own)
}
}
func TestDeferredUsageIsBilledNotPrompt(t *testing.T) {
s := newSniffer(provider.Chat, "text/event-stream")
s.write([]byte(deferredStream))
u := s.usage()
b := u.billed()
if b.Deferred != nil || b.Input != 6 || b.Output != 2240+1913 || b.CacheRead != 181266+177533 || b.CacheWrite != 4400+3735 {
t.Fatalf("billed %+v", b)
}
if u.prompt() != 185670 {
t.Fatalf("the request's own prompt %d", u.prompt())
}
rs := routeUsage("cursor", "claude-opus-5-5", u)
if len(rs) != 2 {
t.Fatalf("tiers %+v", rs)
}
// the route's prompt, as its context is read, is the request's own
if n := promptCounted(rs); n != 185670 {
t.Fatalf("promptCounted %d", n)
}
var in, out int
for _, r := range rs {
in += r.Input + r.CacheRead + r.CacheWrite
out += r.Output
}
if in != 185670+181270 || out != 2240+1913 {
t.Fatalf("priced %d in %d out", in, out)
}
}
func TestChatDecoderKeepsDeferredUsage(t *testing.T) {
d := &chatDecoder{}
var u Usage
for _, line := range []string{`{"id":"c","choices":[{"index":0,"delta":{"content":""}}]}`, deferredJSON} {
if err := d.decode(line, func(ev Event) {
if ev.Kind == KUsage {
u.add(ev.Usage)
}
}); err != nil {
t.Fatal(err)
}
}
if u.prompt() != 185670 || u.Deferred == nil || u.Deferred.prompt() != 181270 {
t.Fatalf("usage %+v deferred %+v", u, u.Deferred)
}
// a client of another API is told the request's own, as before
if c := u.chat(); c["prompt_tokens"] != 185670 {
t.Fatalf("told %v", c)
}
}
func TestNoDeferredUsageAsBefore(t *testing.T) {
s := newSniffer(provider.Chat, "text/event-stream")
s.write([]byte("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":100,\"completion_tokens\":20,\"prompt_tokens_details\":{\"cached_tokens\":60}}}\n\n"))
u := s.usage()
if u.Deferred != nil || u.billed() != u || len(routeUsage("p", "m", u)) != 1 {
t.Fatalf("%+v", u)
}
}
// a real step of the Cursor plugin, its tool steps' events collected late
// (STEP_WAIT shortened): its own prompt 1371, and 2619 of earlier steps
const realDeferredChunk = `{"id":"chatcmpl-71dd4976e7f654f774cd7619","object":"chat.completion.chunk","created":1791866687,"model":"composer-2.5","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1371,"completion_tokens":48,"total_tokens":1419,"prompt_tokens_details":{"cached_tokens":1328,"cache_write_tokens":0},"magpie_deferred":{"prompt_tokens":2619,"completion_tokens":106,"cached_tokens":2533,"cache_write_tokens":0}}}`
// Claude Code on Cursor: the chat reply is translated, and Claude Code is
// told the step's own prompt, while magpie bills the earlier steps' too
func TestTranslatedReplyTellsOwnUsageBillsDeferred(t *testing.T) {
d := &chatDecoder{}
var c collector
if err := d.decode(realDeferredChunk, c.add); err != nil {
t.Fatal(err)
}
res := c.finish()
if res.Usage.prompt() != 1371 || res.Usage.Deferred == nil || res.Usage.Deferred.prompt() != 2619 || res.Usage.Deferred.Output != 106 {
t.Fatalf("usage %+v deferred %+v", res.Usage, res.Usage.Deferred)
}
a := res.Usage.anthropic()
if a.InputTokens+a.CacheReadInputTokens+a.CacheCreationInputTokens != 1371 || a.OutputTokens != 48 {
t.Fatalf("told Claude Code %+v", a)
}
if b := res.Usage.billed(); b.prompt() != 1371+2619 || b.Output != 48+106 {
t.Fatalf("billed %+v", b)
}
}
// Through the whole gateway: a client is told the request's own usage and
// never magpie_deferred's, while the ledger and the OTel span carry both.
// own 0 is a step whose event showed late: only its deferred is in the reply.
func TestDeferredUsageThroughTheGateway(t *testing.T) {
const deferred = `"magpie_deferred":{"prompt_tokens":181270,"completion_tokens":1913,"cached_tokens":177533,"cache_write_tokens":3735}`
own := `"usage":{"prompt_tokens":185670,"completion_tokens":2240,"prompt_tokens_details":{"cached_tokens":181266,"cache_write_tokens":4400},` + deferred + `}`
none := `"usage":{"prompt_tokens":0,"completion_tokens":0,` + deferred + `}`
chat := func(usage string) string {
return `{"id":"c","model":"m1","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"tool_calls"}],` + usage + `}`
}
stream := func(usage string) string {
return sse(`data: {"id":"c","model":"m1","choices":[{"index":0,"delta":{"content":"ok"}}]}`,
`data: {"id":"c","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],`+usage+`}`, "data: [DONE]")
}
for _, c := range []struct {
name, ctype, reply, path, request string
in, out, read, write int
}{
{"chat reply", "application/json", chat(own), "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, 6, 4153, 358799, 8135},
{"chat stream", "", stream(own), "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`, 6, 4153, 358799, 8135},
{"translated for Claude Code", "", stream(own), "/v1/messages", `{"model":"m1","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`, 6, 4153, 358799, 8135},
{"own is 0", "application/json", chat(none), "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, 2, 1913, 177533, 3735},
{"own is 0, streamed", "", stream(none), "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`, 2, 1913, 177533, 3735},
} {
t.Run(c.name, func(t *testing.T) {
setup(t, provider.Chat, &fake{ctype: c.ctype, reply: c.reply})
received := make(chan string, 1)
collector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
received <- string(b)
io.WriteString(w, `{}`)
}))
defer collector.Close()
t.Setenv("MAGPIE_OTEL_ENABLED", "true")
t.Setenv("MAGPIE_OTEL_ENDPOINT", collector.URL)
t.Setenv("MAGPIE_OTEL_HEADERS", "")
t.Setenv("MAGPIE_OTEL_METRICS", "false")
fastOTel(t)
stop := startOTel()
t.Cleanup(stop)
code, body := post(t, c.path, c.request)
if code != 200 {
t.Fatalf("gateway: %d %s", code, body)
}
stop()
if c.path == "/v1/messages" && strings.Contains(body, "magpie_deferred") {
t.Errorf("Claude Code was told magpie_deferred: %s", body)
}
if r := lastUsage(t); r.Input != c.in || r.Output != c.out || r.CacheRead != c.read || r.CacheWrite != c.write {
t.Errorf("ledger %+v", r)
}
select {
case exported := <-received:
// the span carries what the ledger does
if !strings.Contains(exported, fmt.Sprintf(`"intValue":"%d"`, c.out)) || !strings.Contains(exported, fmt.Sprintf(`"intValue":"%d"`, c.read)) {
t.Errorf("span: %s", exported)
}
case <-time.After(time.Second):
t.Error("gateway usage was not exported")
}
})
}
}
// a step with nothing of its own and an earlier step's usage still tells
// the gateway (chatDecoder gave up on a usage of 0 prompt and 0 completion)
func TestChatDecoderKeepsDeferredUsageWithNoOwn(t *testing.T) {
d := &chatDecoder{}
var got []Usage
line := `{"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":0,"completion_tokens":0,"magpie_deferred":{"prompt_tokens":1148,"completion_tokens":50,"cached_tokens":1100,"cache_write_tokens":0}}}`
if err := d.decode(line, func(ev Event) {
if ev.Kind == KUsage {
got = append(got, ev.Usage)
}
}); err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].prompt() != 0 || got[0].Deferred == nil || got[0].Deferred.prompt() != 1148 || got[0].Deferred.Output != 50 {
t.Fatalf("usage %+v", got)
}
}