diff --git a/internal/gateway/desktop_list_test.go b/internal/gateway/desktop_list_test.go index 06138054f..fd737866b 100644 --- a/internal/gateway/desktop_list_test.go +++ b/internal/gateway/desktop_list_test.go @@ -9,6 +9,7 @@ import ( "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/settings" + "github.com/yetone/magpie/internal/usage" ) // moer2831 on Discord: a model picked for one of Claude Desktop's tiers ran @@ -230,3 +231,21 @@ func TestDesktopInferenceModelsNamed(t *testing.T) { t.Fatalf("the reporter's shape needs models listed as %s: %v", desktopEffortAlias, list) } } + +// usage.MagpieAlias reads the alias ids this package lists, so the +// Usage page counts a call under one as the gateway's own: it must agree with +// magpieAliasAsked on every form claudeLooking makes. +func TestUsageKnowsEveryAliasForm(t *testing.T) { + for _, id := range []string{ + "anthropic/magpie-" + aliasNumber("sub/m"), + "mythos-magpie-" + aliasNumber("sub/m"), + "claude-magpie-" + aliasNumber("sub/m"), + "magpie-" + aliasNumber("sub/claude-opus-5-5") + desktopClaudeInfix + "claude-opus-5-5", + "magpie-" + aliasNumber("sub/claude-opus-5-5") + desktopClaudeInfix + "claude-opus-5-5[1m]", + "claude-opus-5-5", "gpt-6", "magpie", + } { + if got, want := usage.MagpieAlias(id), magpieAliasAsked(id); got != want { + t.Errorf("usage.MagpieAlias(%q) = %v, gateway says %v", id, got, want) + } + } +} diff --git a/internal/usage/direct.go b/internal/usage/direct.go index 55a11a073..5cb6e82bc 100644 --- a/internal/usage/direct.go +++ b/internal/usage/direct.go @@ -44,7 +44,7 @@ func directRows(p Period, now time.Time) []Row { if reader == nil { reader = sessions.Calls } - logs, others := reader(since), sharedRecords(since) + logs, others := ownCalls(reader(since)), sharedRecords(since) matched := map[int]bool{} actors := map[int]string{} _, _, compactions := duplicateCodexCalls(logs) diff --git a/internal/usage/gateway_alias.go b/internal/usage/gateway_alias.go new file mode 100644 index 000000000..6b87679bc --- /dev/null +++ b/internal/usage/gateway_alias.go @@ -0,0 +1,73 @@ +package usage + +import ( + "strings" + + "github.com/yetone/magpie/internal/sessions" +) + +// MagpieAlias says id is one of magpie's own alias ids for a model, the names +// the gateway lists Claude Desktop, Claude Code under it and Claude Science +// by: anthropic/magpie-, mythos-magpie-, claude-magpie- +// and magpie-.anthropic., with or without Claude Code's +// "[1m]", the number ten digits (gateway's aliasNumber). Only the gateway +// knows them, so a call that names one went through it. A test in the +// gateway keeps this in step with the ids it lists. +func MagpieAlias(id string) bool { + id = strings.TrimSuffix(id, "[1m]") + for _, p := range []string{"anthropic/magpie-", "mythos-magpie-", "claude-magpie-"} { + if n, ok := strings.CutPrefix(id, p); ok { + return tenDigits(n) + } + } + if rest, ok := strings.CutPrefix(id, "magpie-"); ok { + if n, _, ok := strings.Cut(rest, ".anthropic."); ok { + return tenDigits(n) + } + } + return false +} + +func tenDigits(s string) bool { + if len(s) != 10 { + return false + } + for _, r := range s { + if r < '0' || r > '9' { + return false + } + } + return true +} + +// viaGateway is a call its agent's session file records under one of +// magpie's alias ids (Claude Desktop's Code tab writes the id the gateway +// listed it by). The gateway's own log counts that call under the model that +// served it, so the file's is not another call but the same one: counted +// beside it, it showed twice, once under an alias that is no model +// (CrazyJal on X). A call that never went through the gateway never names +// an alias. +func viaGateway(c sessions.Call) bool { + return MagpieAlias(c.Model) || c.Requested != "" && MagpieAlias(c.Requested) +} + +// ownCalls is cs without the calls the gateway's log counts (viaGateway). cs +// is left as it is: a reader's slice may be one it keeps. +func ownCalls(cs []sessions.Call) []sessions.Call { + n := 0 + for _, c := range cs { + if viaGateway(c) { + n++ + } + } + if n == 0 { + return cs + } + out := make([]sessions.Call, 0, len(cs)-n) + for _, c := range cs { + if !viaGateway(c) { + out = append(out, c) + } + } + return out +} diff --git a/internal/usage/gateway_alias_test.go b/internal/usage/gateway_alias_test.go new file mode 100644 index 000000000..b93d07ec8 --- /dev/null +++ b/internal/usage/gateway_alias_test.go @@ -0,0 +1,68 @@ +package usage + +import ( + "testing" + "time" + + "github.com/yetone/magpie/internal/sessions" +) + +// CrazyJal on X: Claude Desktop's Code tab writes the id the gateway listed +// the model by (magpie-.anthropic.) as the model of +// each turn, and the gateway's own log counts the turn under the model that +// served it. The file's call is the same call: it showed again under the +// alias, a row for a model that is none, its cost counted twice. A call under +// a model of the agent's own stays (it never went through the gateway). +func TestAliasCallsAreTheGatewaysNotDirect(t *testing.T) { + now := time.Now() + tk := sessions.Tokens{Input: 120, Output: 40} + logs := []sessions.Call{ + {Time: now.Add(-1 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "magpie-7416897221.anthropic.claude-opus-5-5", Tokens: tk}, + {Time: now.Add(-2 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "magpie-7416897221.anthropic.claude-opus-5-5[1m]", Tokens: tk}, + {Time: now.Add(-3 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "mythos-magpie-0123456789", Tokens: tk}, + {Time: now.Add(-4 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "anthropic/magpie-0123456789", Tokens: tk}, + {Time: now.Add(-5 * time.Minute), Agent: "claude", Session: "s2", Model: "claude-sonnet-5-5", Requested: "claude-magpie-0123456789", Tokens: tk}, + {Time: now.Add(-6 * time.Minute), Agent: "claude", Session: "s3", Model: "claude-opus-5-5", Tokens: tk}, // direct, never through the gateway + } + recs := []Record{{Time: now.Add(-time.Minute), Agent: "claude-desktop", Provider: "relay", Model: "claude-opus-5-5", Input: 120, Output: 40}} + + rows, _, _, _ := ledgerWith(now.Add(-time.Hour), Filter{}, recs, logs) + if len(rows) != 2 { + t.Fatalf("the gateway's call and the one direct call, got %d rows: %+v", len(rows), rows) + } + for _, r := range rows { + if MagpieAlias(r.Model) || MagpieAlias(r.Requested) { + t.Errorf("a row under an alias: %+v", r) + } + } + + old := LogCalls + LogCalls = func(time.Time) []sessions.Call { return logs } + t.Cleanup(func() { LogCalls = old }) + d := Direct(Today) + if d.Calls != 1 || len(d.Models) != 1 || d.Models[0].Model != "claude-opus-5-5" { + t.Fatalf("Direct counts the one call outside the gateway: %d calls, models %+v", d.Calls, d.Models) + } + if got := ownCalls(logs); len(got) != 1 || got[0].Session != "s3" { + t.Fatalf("ownCalls: %+v", got) + } +} + +func TestMagpieAliasForms(t *testing.T) { + for id, want := range map[string]bool{ + "magpie-7416897221.anthropic.claude-opus-5-5": true, + "magpie-7416897221.anthropic.claude-opus-5[1m]": true, + "mythos-magpie-0123456789": true, + "anthropic/magpie-0123456789": true, + "claude-magpie-0123456789[1m]": true, + "magpie": false, + "magpie-123.anthropic.claude-opus-5-5": false, // not ten digits + "claude-opus-5-5": false, + "magpie-agent/claude-opus-5-5": false, + "": false, + } { + if got := MagpieAlias(id); got != want { + t.Errorf("MagpieAlias(%q) = %v, want %v", id, got, want) + } + } +} diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index f8ba55665..0be0f6d7b 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -460,6 +460,7 @@ func ledgerWith(since time.Time, f Filter, recs []Record, logs []sessions.Call) // priced and judged here as this computer's are, and none from until on (a // picked range's end, #1492; zero for none). func ledgerWithShared(since, until time.Time, f Filter, recs []Record, logs []sessions.Call, others []SharedCall) (rows []Row, sum Totals, agents, providers []string) { + logs = ownCalls(logs) matched, pairs, actors, compactions := gatewayMatchesDetails(recs, logs) return ledgerMatched(since, until, f, recs, logs, others, matched, pairs, actors, compactions) } diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index f8bf741cf..0184a53f7 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1265,7 +1265,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now, settle makeChunk := func() func(sessions.CallSource) *rowChunk { priceRow := pricers() build := func(s sessions.CallSource) *rowChunk { - cs := readSource(s) + cs := ownCalls(readSource(s)) c := &rowChunk{Source: s, Rows: make([]packedRow, 0, len(cs))} for _, call := range cs { r := logRecord(call)