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.
135 lines
4.6 KiB
Go
135 lines
4.6 KiB
Go
package sessions
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// ZCode's calls: the same OpenCode tables its reader uses (zcode.go), one
|
|
// call per assistant message, with the AI SDK's tokens — input with the
|
|
// cache in it, output with the reasoning. A message's model is read the way
|
|
// the tables hold it: modelID, or model.id under a message written by a newer
|
|
// ZCode, and the provider the reply came from beside it. The rows are read
|
|
// here rather than taken from the session summary, because the summary counts
|
|
// a reply once however many of its lines carry it, while each assistant
|
|
// message is a call of its own.
|
|
//
|
|
// As with dsh, the Sessions page read these all along and the usage ledger
|
|
// did not, so ZCode's tokens never reached the Usage page.
|
|
//
|
|
// A reply through magpie's gateway is left out here as OpenCode's is
|
|
// (ocGatewayProvider): ZCode records the provider it was told to use, and
|
|
// the route magpie writes says "magpie", so unlike dsh's this one is a
|
|
// provider id magpie itself chose and can be trusted to mean the gateway.
|
|
// A provider the user added by hand at the gateway's address is the gateway
|
|
// too, by its address (zcodeGateway, calls_zcode_gateway.go).
|
|
|
|
// zcCallList is the last listing of ZCode's database, kept while the
|
|
// database and its WAL are as they were. Its lock is the one ocCallList
|
|
// uses: callSources runs on more than one read at a time, and two of them
|
|
// finding the stamp changed would write this at once.
|
|
var zcCallList struct {
|
|
sync.Mutex
|
|
stamp string
|
|
files []file
|
|
}
|
|
|
|
// zcodeCallFiles are ZCode's sessions as call sources.
|
|
func zcodeCallFiles() []file {
|
|
if !Reads("zcode") {
|
|
return nil
|
|
}
|
|
path := zcodeDB()
|
|
if !fileExists(path) {
|
|
return nil
|
|
}
|
|
stamp := ocStamp(path)
|
|
zcCallList.Lock()
|
|
defer zcCallList.Unlock()
|
|
if stamp != "" && stamp == zcCallList.stamp {
|
|
return append([]file(nil), zcCallList.files...)
|
|
}
|
|
var out []file
|
|
withOCCallDB(path, func(db *sql.DB) {
|
|
out = openCodeDBFilesIn("zcode", path, db, "COUNT(m.id) + COALESCE(SUM(m.time_updated), 0)")
|
|
})
|
|
for i := range out {
|
|
out[i].w().oc = nil // the handle is not kept; a read opens it again
|
|
}
|
|
zcCallList.stamp, zcCallList.files = stamp, out
|
|
return append([]file(nil), out...)
|
|
}
|
|
|
|
// readZCodeCalls reads a session's calls whole, as readOpenCodeCalls does.
|
|
// ZCode is OpenCode's tables with a title source of its own, so its store is
|
|
// zcDB: the model id is modelId or the older modelID, and its tokens the AI
|
|
// SDK's.
|
|
func readZCodeCalls(f file) (*callFile, bool) {
|
|
st := &callFile{Agent: "zcode", Path: f.path, Session: strings.TrimPrefix(f.key, "zcode:")}
|
|
i := strings.LastIndex(f.path, "#")
|
|
if i < 0 {
|
|
return st, false
|
|
}
|
|
path, sid := f.path[:i], f.path[i+1:]
|
|
var info ocInfo
|
|
var msgs [][]byte
|
|
var copies []bool
|
|
if !withOCCallDB(path, func(db *sql.DB) {
|
|
var store ocStore = zcDB{ocDB{db}}
|
|
if ocHasTable(db, "session_v2") {
|
|
var n int
|
|
if db.QueryRow(`SELECT COUNT(*) FROM session_v2 WHERE id = ?`, sid).Scan(&n) == nil && n > 0 {
|
|
store = ocV2DB{db}
|
|
}
|
|
}
|
|
info, _ = store.info(sid)
|
|
msgs = store.messages(sid)
|
|
// a copy of a message still in the database (zcDB.copied): the
|
|
// original's session counts its call. Marked by row, not by id: a
|
|
// row's id is in its own column and not in its data, so every
|
|
// message here reads with an empty ID, and one copy marked by id
|
|
// left out the whole session, a fork's own replies too (Sky,
|
|
// skyhuang1994: a fork's 11,409 calls of its own, 3.49B tokens).
|
|
copies = make([]bool, len(msgs))
|
|
if z, ok := store.(zcDB); ok {
|
|
for i, b := range msgs {
|
|
var m ocMessage
|
|
if json.Unmarshal(b, &m) == nil && m.Role == "assistant" && z.copied(m) {
|
|
copies[i] = true
|
|
}
|
|
}
|
|
}
|
|
}) {
|
|
return st, false
|
|
}
|
|
if st.Session == "" {
|
|
st.Session = info.ID
|
|
}
|
|
for i, b := range msgs {
|
|
var m ocMessage
|
|
if json.Unmarshal(b, &m) != nil || m.Role != "assistant" || zcodeGateway(m.ProviderID) || copies[i] {
|
|
continue
|
|
}
|
|
if c, ok := ocCall(st, m, info.Directory); ok {
|
|
c.Agent = "zcode"
|
|
// a request names its session without the prefix the database
|
|
// gives it (zcodeBareID), and a call is paired with the
|
|
// gateway's record of it by that session: under sess_… none was,
|
|
// and a call magpie had routed was counted again as a local
|
|
// session's (nori_tsukiji on X)
|
|
c.Session = zcodeBareID(c.Session)
|
|
st.Calls = append(st.Calls, c)
|
|
}
|
|
}
|
|
sort.SliceStable(st.Calls, func(i, j int) bool {
|
|
if !st.Calls[i].Time.Equal(st.Calls[j].Time) {
|
|
return st.Calls[i].Time.Before(st.Calls[j].Time)
|
|
}
|
|
return st.Calls[i].Msg < st.Calls[j].Msg
|
|
})
|
|
return st, true
|
|
}
|