Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

5.1 KB
Raw Blame
package usage

import (
	"encoding/json"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/yetone/magpie/internal/catalog"
	"github.com/yetone/magpie/internal/provider"
)

// #1839: the calls magpie made by itself — its routing classifier's (UA
// magpie-router/1), a web search's, System One's decision — go to the same
// accounts and keys as the agents' and are counted in their totals, and
// each account, key and model says how many of them were magpie's own,
// what they came to and cost. A group with none says nothing of it.
func TestSummaryCountsMagpiesOwnCallsApart(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
	os.WriteFile(catalog.CachePath(), []byte(`{"openai":{"id":"openai","models":{"gpt-5":{"id":"gpt-5","cost":{"input":1,"output":10}}}}}`), 0o644)
	catalog.Reset()
	t.Cleanup(catalog.Reset)
	if err := provider.Save(provider.Provider{ID: "cx", Name: "Codex", Key: "cx-secret", Chat: "https://chatgpt.example/v1", Catalog: "openai"}); err != nil {
		t.Fatal(err)
	}
	if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "relay-secret", Chat: "https://relay.example/v1", Catalog: "openai"}); err != nil {
		t.Fatal(err)
	}
	now := time.Now()
	key := provider.KeyID("relay-secret")
	recs := []Record{
		{Time: now, Agent: "codex_cli_rs/0.50", Provider: "cx", Model: "gpt-5", Host: "chatgpt.com as [email protected]", ProviderAccount: "[email protected]", Input: 1000, Output: 100, Status: 200},
		// the classifier's two asks of the turn, on the same account
		{Time: now, Agent: "magpie-router/1", Kind: "routing", Provider: "cx", Model: "gpt-5", Host: "chatgpt.com as [email protected]", ProviderAccount: "[email protected]", Input: 300, Output: 2, Status: 200},
		{Time: now, Agent: "magpie-router/1", Kind: "routing", Provider: "cx", Model: "gpt-5", Host: "chatgpt.com as [email protected]", ProviderAccount: "[email protected]", Input: 200, Output: 1, Status: 200},
		// a try of it handed on: its tokens, not a request
		{Time: now, Agent: "magpie-router/1", Kind: "routing", Provider: "cx", Model: "gpt-5", Host: "chatgpt.com as [email protected]", ProviderAccount: "[email protected]", Input: 10, Status: 429, HandedOn: true},
		// another account: the agent's alone
		{Time: now, Agent: "codex_cli_rs/0.50", Provider: "cx", Model: "gpt-5", Host: "chatgpt.com as [email protected]", ProviderAccount: "[email protected]", Input: 50, Output: 5, Status: 200},
		// a web search magpie made, on a key
		{Time: now, Agent: "magpie-search/1", Kind: "web_search", Provider: "relay", Model: "gpt-5", Host: "relay.example", ProviderKeyID: key, Input: 40, Output: 4, Status: 200},
		{Time: now, Agent: "claude-cli/2.1", Provider: "relay", Model: "gpt-5", Host: "relay.example", ProviderKeyID: key, Input: 60, Output: 6, Status: 200},
	}
	s := summarize(Month, now, recs)
	acct := map[string]Group{}
	for _, g := range s.Accounts {
		acct[g.ID] = g
	}
	a := acct["cx@[email protected]"]
	if a.Calls != 3 || a.Input != 1510 {
		t.Fatalf("magpie's calls are still the account's: %+v", a.Totals)
	}
	if a.Own.Calls != 2 || a.Own.Tokens != 513 { // the try handed on: its tokens, as Totals has them
		t.Fatalf("account a's own share: %+v", a.Own)
	}
	if want := (510*1.0 + 3*10.0) / 1e6; a.Own.Cost < want*0.99 || a.Own.Cost > want*1.01 {
		t.Fatalf("own cost %v, want %v", a.Own.Cost, want)
	}
	if b := acct["cx@[email protected]"]; b.Own != (OwnShare{}) {
		t.Fatalf("account b has none of magpie's: %+v", b.Own)
	}
	var k Group
	for _, g := range s.ProviderKeys {
		if g.Provider == "relay" {
			k = g
		}
	}
	if k.Calls != 2 || k.Own.Calls != 1 || k.Own.Tokens != 44 {
		t.Fatalf("key: %+v own %+v", k.Totals, k.Own)
	}
	// the model's, told apart by the account it went out as
	ownModel := 0
	for _, g := range s.Models {
		if g.Provider == "cx" {
			ownModel += g.Own.Calls
		}
	}
	if ownModel != 2 {
		t.Fatalf("model: %d own calls: %+v", ownModel, s.Models)
	}
	// magpie's calls are an agent's row of their own, as before ("magpie"
	// where the agents are loaded, its User-Agent's name here)
	mine := 0
	for _, g := range s.Agents {
		if (Record{Agent: g.ID}).ByMagpie() {
			mine += g.Calls
		} else if g.Own != (OwnShare{}) {
			t.Fatalf("an agent's row has an own share: %+v", g)
		}
	}
	if mine != 3 {
		t.Fatalf("magpie's agent rows: %d calls", mine)
	}
	// none is said as nothing at all
	b, _ := json.Marshal(acct["cx@[email protected]"])
	if strings.Contains(string(b), `"own"`) {
		t.Fatalf("a group with none of magpie's calls: %s", b)
	}
	b, _ = json.Marshal(a)
	if !strings.Contains(string(b), `"own":{"calls":2,"tokens":513`) {
		t.Fatalf("a's own share in JSON: %s", b)
	}
}

// Every request magpie sends by itself is magpie's own: the classifier, the
// search, the image description; an agent's is not.
func TestByMagpie(t *testing.T) {
	for ua, want := range map[string]bool{"magpie-router/1": true, "magpie-search/1": true, "magpie-vision/1": true, "magpie": true, "claude-cli/2.1": false, "codex_cli_rs/0.50": false, "": false} {
		if got := (Record{Agent: ua}).ByMagpie(); got != want {
			t.Errorf("%q: %v, want %v", ua, got, want)
		}
	}
}