Files
magpie/internal/sessions/calls_zcode_test.go
T
yetone e4e9eb0928 usage: a ZCode call to a provider added by hand at magpie's address is not a local session's (jiangshangmuyun on X)
The Usage page listed one ZCode call as "new-provider · 本地会话" among the
gateway's own rows for the same Trae CN DeepSeek calls. The reporter has one
ZCode window and routes everything through magpie.

Cause: the user added magpie to ZCode by hand as a provider of their own
("new-provider", base URL magpie's gateway), not through magpie's connect,
which writes the id "magpie". ZCode records that id beside each reply, and
readZCodeCalls left out only "magpie"/"dial" (ocGatewayProvider). Its calls
were read from ZCode's database as well as logged by the gateway, and only
hidden again where pairing by session, tokens and end time matched; a call
whose counts didn't agree was listed twice as a "local session".

Fix: zcodeGateway also takes any provider whose base URL, in ZCode's
config.json or provider_config.json, is this machine (loopback, localhost,
0.0.0.0, one of its own addresses) on the gateway's port (MAGPIE_ADDR or the
saved port). Such calls are left out before pairing, as "magpie" ones are.
zcode's call parse version is 4, so cached shards are read again. Like
workbuddyGateway it reads the configuration as it is now. The label itself
is unchanged: a call that did go straight to a vendor is still a local
session's.

Siblings filed, not changed: OpenCode/MiMo (ocGatewayProvider), Pi
(piGateway) and dsh still match the gateway by provider name only (#7193).

Go run on mote at this change on base 9bf5848c1, rebased onto e2e8ea253
(no internal/sessions, usage or GUI file changed between): go test -count=1 ./internal/sessions
./internal/usage ./internal/usageapi (ok); GOOS=darwin/linux/windows go vet of
those packages (clean); gofmt -l clean. Broken by hand: with ocGatewayProvider
back in readZCodeCalls, TestZCodeHandAddedGatewayProvider is red ("a call to a
provider at the gateway's address was read as a local session's").
GUI (no GUI file changed, so as a check of the Usage page that shows these
rows): every usage-*, session-identity and i18n-* file, chromium and webkit,
each language they cover, on mote under load ~40: 1670 of 1777 pass. The 107
reds are in 12 usage files (usage-agents-hide, usage-chart-line,
usage-speed-all-models, usage-upload, usage-ledger and 7 more) and fail the
same at origin/main e2e8ea253 without this change (107 of 308 in those files,
same tests). Filed: #2276, #5834, #6003, #6743, #6790, #6975.
Not tried: the reporter's real ZCode database and config (this computer has
no ZCode); why that one call's tokens didn't pair (no data); the full Go suite.
2026-11-19 17:39:35 +08:00

110 lines
4.3 KiB
Go

package sessions
import (
"database/sql"
"os"
"path/filepath"
"strconv"
"testing"
"time"
)
// zcodeCalls are the ZCode calls Calls gives, by message id.
func zcodeCalls(t *testing.T) map[string]Call {
t.Helper()
out := map[string]Call{}
for _, c := range Calls(time.Time{}) {
if c.Agent == "zcode" {
if _, dup := out[c.Msg]; dup {
t.Fatalf("message %s counted twice", c.Msg)
}
out[c.Msg] = c
}
}
return out
}
// ZCode's calls reach the usage ledger too: the Sessions page read its
// database all along, and the Usage page showed none of it.
func TestZCodeCalls(t *testing.T) {
setupAgents(t)
cs := zcodeCalls(t)
// ses_zcA's own reply went through magpie's gateway (the route magpie
// writes names it "magpie"), so it is the gateway's record to keep; the
// others are read here: ses_zcB's (a subagent, counted under the session
// it ran in) and ses_zcC's
if len(cs) != 2 {
t.Fatalf("want 2 ZCode calls, got %d: %+v", len(cs), cs)
}
if _, dup := cs["msg_z3"]; dup {
t.Fatalf("a call made to magpie's gateway was read from the database too: %+v", cs["msg_z3"])
}
if b := cs["msg_z4"]; b.Model != "glm-5.1-air" || b.Tokens != (Tokens{50, 5, 0, 0, 0}) {
t.Fatalf("the subagent's reply: %+v", b)
}
if c := cs["msg_z5"]; c.Tokens != (Tokens{10, 1, 0, 0, 0}) {
t.Fatalf("ses_zcC's reply: %+v", c)
}
}
// jiangshangmuyun on X: a provider the user added in ZCode by hand
// ("new-provider"), pointed at this magpie's gateway, is the gateway's as
// "magpie" is. Its calls were listed again as a local session's beside the
// gateway's rows for the same calls. A provider elsewhere, or on this
// machine at another port, keeps its calls.
func TestZCodeHandAddedGatewayProvider(t *testing.T) {
home := setupAgents(t)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3499")
dir := filepath.Join(home, ".zcode", "v2")
os.MkdirAll(dir, 0o755)
// ZCode 3.14 and later: provider_config.json's rules
os.WriteFile(filepath.Join(dir, "provider_config.json"), []byte(`{"schemaVersion":1,"config":{"providerConfigRules":{"providerRules":[
{"providerId":"new-provider","providerName":"new-provider","enabled":true,"config":{"api":{"type":"anthropic-messages","baseUrl":"http://127.0.0.1:3499"}}},
{"providerId":"zai","providerName":"Z.ai","enabled":true,"config":{"api":{"type":"anthropic-messages","baseUrl":"https://open.bigmodel.cn/api/anthropic"}}},
{"providerId":"ollama","providerName":"Ollama","enabled":true,"config":{"api":{"type":"openai-chat-completions","baseUrl":"http://127.0.0.1:11434/v1"}}}]}}}`), 0o600)
// an older ZCode's config.json
os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"provider":{"mine":{"name":"mine","kind":"openai-compatible","options":{"baseURL":"http://localhost:3499/v1"}}}}`), 0o600)
db, err := sql.Open("sqlite", filepath.Join(home, ".zcode", "cli", "db", "db.sqlite"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
for i, p := range []string{"new-provider", "mine", "zai", "ollama"} {
at := int64(1790496200000 + i*10000)
ocInsert(t, db, "ses_zcC", "msg_h"+p, at, at+5000, `{"id":"msg_h`+p+`","role":"assistant","modelId":"trae-cn/deepseek-v4.1-flash","providerID":"`+p+`","tokens":{"input":20,"output":2,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":`+strconv.FormatInt(at, 10)+`,"completed":`+strconv.FormatInt(at+5000, 10)+`}}`)
}
Reset()
cs := zcodeCalls(t)
for _, gone := range []string{"msg_hnew-provider", "msg_hmine"} {
if c, ok := cs[gone]; ok {
t.Fatalf("a call to a provider at the gateway's address was read as a local session's: %+v", c)
}
}
for _, kept := range []string{"msg_hzai", "msg_hollama"} {
if _, ok := cs[kept]; !ok {
t.Fatalf("%s, to a provider elsewhere, was left out: %+v", kept, cs)
}
}
}
func TestGatewayBase(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3499")
for base, want := range map[string]bool{
"http://127.0.0.1:3499": true,
"http://localhost:3499/v1": true,
"https://127.0.0.1:3499/": true,
"http://[::1]:3499": true,
"http://0.0.0.0:3499": true,
"http://127.0.0.1:11434/v1": false,
"https://open.bigmodel.cn": false,
"http://example.com:3499": false,
"ws://127.0.0.1:3499": false,
"": false,
"127.0.0.1:3499": false,
} {
if got := gatewayBase(base); got != want {
t.Errorf("gatewayBase(%q) = %v, want %v", base, got, want)
}
}
}