kensenzhao saw a subscription's allowance spent by requests the agent
didn't make: a routing group's classifier, set to a subscription's model,
asks it as every turn begins, and those calls looked like the agent's own.
What changed
- usage.Group has an own share (calls, tokens, cost) of the calls magpie
made by itself, filled for models, keys and accounts. Record.ByMagpie
is the test: agent "magpie", or one of its own User-Agents
(magpie-router/1, magpie-search/1, magpie-vision/1) where the agents
aren't loaded. The calls still count in the group's totals, as they
went to the same key or account. A group with none sends no "own".
- The classifier's calls are Kind "routing": callKind marks RouterAgent's
requests, and the decision model's record (Jev / System One) is written
with it. A "Routing classifier" purpose names them in the ledger.
- Usage page: magpie's row among the agents and its ledger rows carry a
"magpie's own" mark; an account's, key's or model's row adds "{n} by
magpie itself"; an account's card says how many of the period's
requests were magpie's own, drawn again once the summary arrives after
the allowances.
- Routing page: /api/groups marks a model a subscription account serves
(modelRef.Sub, servedRef). Beside a classifier on such a model, or a
group whose first member is one, the editor says every turn starts
with a request to that subscription, two when it picks both intent and
effort in words. A classifier on a key or a free model says nothing.
- New strings in zh, zh-TW, ja and de. The mark is a muted chip with an
even border all round (no left stripe).
Tests
- TestSummaryCountsMagpiesOwnCallsApart, TestByMagpie (usage).
- TestIntentRoutesATurn and TestJevPicksTheIntent (gateway) now check
the classifier's records are Kind routing and magpie's own, and the
agent's records are not.
- TestServedRefSaysASubscription (gui).
- own-requests.test.cjs: Chromium and WebKit, 1100 and 390 wide,
en/zh/zh-TW/ja/de, the API faked; no click scroll, no sideways page.
- Broken by hand, each turned its test red: addOwn counting nothing;
callKind's routing case removed; decide.go's Kind blanked; the ledger
mark, the agents' mark, the "{n} by magpie itself" line, the card's
line, its redraw after the summary, the Routing hint, its group case
and modelRef.Sub each removed.
Run: gofmt -l; go vet on darwin, linux -tags nogui and windows; go test
for usage, access, catalog, gui (-tags nogui) and gateway in full; the
GUI tests above, the ledger, routing-intent and translation tests.
Not done: the warmups (warmClaude/askClaude) write no usage record at
all, so they can't be marked; that is left as it was. Not tried with a
real subscription classifier against a live account.
118 lines
5.1 KiB
Go
118 lines
5.1 KiB
Go
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 a@example.com", ProviderAccount: "a@example.com", 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 a@example.com", ProviderAccount: "a@example.com", Input: 300, Output: 2, Status: 200},
|
|
{Time: now, Agent: "magpie-router/1", Kind: "routing", Provider: "cx", Model: "gpt-5", Host: "chatgpt.com as a@example.com", ProviderAccount: "a@example.com", 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 a@example.com", ProviderAccount: "a@example.com", 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 b@example.com", ProviderAccount: "b@example.com", 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@a@example.com"]
|
|
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@b@example.com"]; 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@b@example.com"])
|
|
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)
|
|
}
|
|
}
|
|
}
|