Files
magpie/internal/usage/gateway_alias_test.go
T
yetone ffc1d64e5e usage: a call recorded under magpie's alias id is the gateway's, not a direct one (CrazyJal on X)
Claude Desktop's Code tab, and Claude Code under it, write the id the gateway
listed the model by (magpie-<number>.anthropic.<Claude model>,
mythos-magpie-<n>, anthropic/magpie-<n>, claude-magpie-<n>) as each turn's
model. The gateway's log counts the turn under the model that served it, and
the agent's file counted it again under the alias: a row for a model that is
none, its cost on top of the real one.

Only the gateway knows those ids, so a call that names one went through it.
Such calls are now left out of what Direct, the ledger and the Requests
page's chunks take from session files, for every agent that records one.
Calls under a model of the agent's own are unchanged. usage.MagpieAlias
mirrors the gateway's alias forms; a gateway test fails when the two differ.

Run (mote, at the pushed head): TestAliasCallsAreTheGatewaysNotDirect, TestMagpieAliasForms and
the gateway's TestUsageKnowsEveryAliasForm pass; with ownCalls returning its
input the first fails (7 rows, four under aliases). go test internal/usage,
internal/gui, internal/sessions ok; vet darwin, linux nogui and windows of
usage and gateway clean; gofmt clean. Go-only change, so no GUI file was run.
Not tried: a real Claude Desktop 3p session (none on this machine; the model
forms come from the gateway's own alias code and its tests, not JSONL bytes).
Existing persisted request chunks rebuild with the new binary.
2026-11-12 13:14:43 +08:00

69 lines
3.1 KiB
Go

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-<number>.anthropic.<Claude model>) 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)
}
}
}