diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index 344c4923a..e0756d190 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -57,7 +57,10 @@ const callCacheVersion = "calls-v15" // for one agent read every agent's history again, every call shard of a // long history (~16,000 files, minutes of CPU) after an update. An agent // not named here is at 0, as every shard written before this was. -var callParseVersions = map[string]int{} +// +// cindy 1: a call through magpie's link (provider "magpie-<8 hex>") is +// the gateway's, and left out (cindyGatewayProvider). +var callParseVersions = map[string]int{"cindy": 1} // CallCacheVersion is callCacheVersion and callParseVersions, for a cache // of what is made of the calls (usage's requests-v1.bin and heatmap): a diff --git a/internal/sessions/call_versions_test.go b/internal/sessions/call_versions_test.go index ce6a8f10d..ce011b2a7 100644 --- a/internal/sessions/call_versions_test.go +++ b/internal/sessions/call_versions_test.go @@ -56,8 +56,9 @@ func TestCallParseVersionRaiseReadsThatAgentAlone(t *testing.T) { } unraised := CallCacheVersion() + kept := callParseVersions callParseVersions = map[string]int{"claude": 1} - t.Cleanup(func() { callParseVersions = map[string]int{} }) + t.Cleanup(func() { callParseVersions = kept }) if CallCacheVersion() == unraised || !strings.Contains(CallCacheVersion(), "claude=1") { t.Fatalf("raised Claude's calls, and what is made of them is still %q", CallCacheVersion()) } diff --git a/internal/sessions/calls_pi.go b/internal/sessions/calls_pi.go index 60de8918f..270c854ed 100644 --- a/internal/sessions/calls_pi.go +++ b/internal/sessions/calls_pi.go @@ -32,7 +32,14 @@ import ( // A call made through magpie's own entry (provider "magpie", which // internal/agent's pi.go and omp.go write, "dial" before the rename) is the // gateway's to count: it logged the call itself, with the provider and -// account that answered. +// account that answered. Cindy's Pi names it otherwise: Cindy gives a +// custom provider it imports the link's id and eight hex digits of a new +// UUID (its import parser: `${id.slice(0,24)}-${randomUUID().slice(0,8)}`), +// so magpie's link, id "magpie" (internal/agent's cindy.go), is +// "magpie-beedee1a" there, and a call through it was counted twice: by the +// gateway, and again here as Cindy's own (jealousy03888 on Discord, ZCode's +// GLM-5.3 in Cindy listed once as a gateway request and once as a local +// session). // // A fork (pi --fork, /fork) starts with a copy of its parent's entries, ids // and times kept: they are the parent's calls, left out of the fork as @@ -47,8 +54,30 @@ func piCallFiles() []file { return append(out, wslFiles("pi", "raft", "omp")...) } -// piGateway is whether a provider is magpie's own entry. -func piGateway(provider string) bool { return provider == "magpie" || provider == "dial" } +// piGateway is whether a provider is magpie's own entry, in agent's files. +func piGateway(agent, provider string) bool { + if provider == "magpie" || provider == "dial" { + return true + } + return agent == "cindy" && cindyGatewayProvider(provider) +} + +// cindyGatewayProvider is whether a provider id is the one Cindy gives +// magpie's import link: "magpie-" and eight lowercase hex digits. A +// provider the user adds in Cindy by hand under the name Magpie is +// "magpie", or "magpie-2" beside another (its slug), never this shape. +func cindyGatewayProvider(id string) bool { + rest, ok := strings.CutPrefix(id, "magpie-") + if !ok || len(rest) != 8 { + return false + } + for _, r := range rest { + if !('0' <= r && r <= '9' || 'a' <= r && r <= 'f') { + return false + } + } + return true +} type piCallLine struct { Type string `json:"type"` @@ -156,7 +185,7 @@ func piCall(st *callFile, agent string, l piCallLine, provider, model string) (C default: return Call{}, false } - if u == nil || model == "" || piGateway(provider) { + if u == nil || model == "" || piGateway(agent, provider) { return Call{}, false } if u.Input < 0 || u.Output < 0 || u.CacheRead < 0 || u.CacheWrite < 0 { diff --git a/internal/sessions/cindy_test.go b/internal/sessions/cindy_test.go index 59b7cff2d..70ac7943d 100644 --- a/internal/sessions/cindy_test.go +++ b/internal/sessions/cindy_test.go @@ -143,3 +143,56 @@ func TestCindyGatewaySessionListedOnce(t *testing.T) { t.Fatal("Cindy's native ids leave its files out") } } + +// TestCindyGatewayCallsCountedOnce: Cindy imports magpie's link as the +// provider "magpie-<8 hex>" (its import parser adds eight hex digits of a +// new UUID to the link's id), and its Pi writes that id on each reply. A +// reply through it is the gateway's, already counted there with the +// account that answered; read again here it was listed and priced twice +// (jealousy03888 on Discord: ZCode's GLM-5.3 in Cindy, one gateway row and +// one "local session" row of the same 135/37/84,736 tokens). A provider +// of the user's own whose name only begins with magpie stays Cindy's. +func TestCindyGatewayCallsCountedOnce(t *testing.T) { + global, _ := cindySetup(t) + const id = "019a4c2e-5b1d-7f3a-9c6e-2d8f0a1b3c4d" + lines := []string{ + `{"type":"session","version":3,"id":"` + id + `","timestamp":"2026-11-03T09:30:52.410Z","cwd":"/work/app"}`, + `{"type":"model_change","id":"a1b2c3d4","parentId":null,"timestamp":"2026-11-03T09:30:52.611Z","provider":"magpie-beedee1a","modelId":"zcode/GLM-5.3"}`, + `{"type":"thinking_level_change","id":"b2c3d4e5","parentId":"a1b2c3d4","timestamp":"2026-11-03T09:30:52.611Z","thinkingLevel":"high"}`, + `{"type":"message","id":"c3d4e5f6","parentId":"b2c3d4e5","timestamp":"2026-11-03T09:30:57.802Z","message":{"role":"user","content":[{"type":"text","text":"hi"}],"timestamp":1793611857800}}`, + `{"type":"message","id":"d4e5f6a7","parentId":"c3d4e5f6","timestamp":"2026-11-03T09:31:03.122Z","message":{"role":"assistant","content":[{"type":"text","text":"Hello!"}],"api":"openai-completions","provider":"magpie-beedee1a","model":"zcode/GLM-5.3","usage":{"input":135,"output":37,"cacheRead":84736,"cacheWrite":0,"reasoning":0,"totalTokens":84908,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"stop","timestamp":1793611857830,"responseId":"chatcmpl-5d2e","durationMs":5292,"thinkingLevel":"high"}}`, + `{"type":"message","id":"e5f6a7b8","parentId":"d4e5f6a7","timestamp":"2026-11-03T09:32:10.001Z","message":{"role":"user","content":[{"type":"text","text":"again"}],"timestamp":1793611930000}}`, + `{"type":"message","id":"f6a7b8c9","parentId":"e5f6a7b8","timestamp":"2026-11-03T09:32:12.500Z","message":{"role":"assistant","content":[{"type":"text","text":"ok"}],"api":"openai-completions","provider":"magpie-relay","model":"glm-5","usage":{"input":40,"output":5,"cacheRead":0,"cacheWrite":0,"reasoning":0,"totalTokens":45,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"stop","timestamp":1793611930010,"responseId":"chatcmpl-6e3f","durationMs":2400,"thinkingLevel":"high"}}`, + } + p := filepath.Join(global, "2026-11-03T09-30-52-410Z_"+id+".jsonl") + if err := os.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil { + t.Fatal(err) + } + old := time.Now().Add(-time.Hour) + os.Chtimes(p, old, old) + Reset() + var own int + for _, c := range Calls(time.Time{}) { + if c.Session != id { + continue + } + if c.Upstream == "magpie-beedee1a" { + t.Errorf("a call through magpie's link in Cindy counted as Cindy's own: %+v", c) + } + if c.Upstream == "magpie-relay" && c.Agent == "cindy" && c.Tokens == (Tokens{Input: 40, Output: 5}) { + own++ + } + } + if own != 1 { + t.Fatalf("the call on the user's own magpie-relay counted %d times, want 1", own) + } + // Pi's own files name providers the user chose: the shape is Cindy's alone + if piGateway("pi", "magpie-beedee1a") || !piGateway("pi", "magpie") || !piGateway("cindy", "magpie-beedee1a") { + t.Fatal("piGateway's scope") + } + for _, s := range []string{"magpie-2", "magpie-BEEDEE1A", "magpie-beedee1", "magpie-beedee1a0", "magpie-beedee1g"} { + if cindyGatewayProvider(s) { + t.Errorf("%q taken for magpie's link", s) + } + } +}