Files
magpie/internal/sessions/calls_opencode_test.go
T
yetone b8fdba5d64 usage: OpenCode's own calls are counted, as Claude Code's and Codex's are (#680, Sun1090: cc-switch 会统计 OpenCode 的用量,我这样不会被统计吗?) — OpenCode used on its own providers (OpenCode Zen/Go, its own keys), not through magpie's gateway, never reached the Usage page: the local calls were Claude Code's and Codex's session files only. Now each OpenCode assistant message is a call (agent opencode): its model, provider (as the session provider), input, output with the reasoning in it (reasoning kept apart too, as Codex's), cache read and write, its end time and how long it took, the session it ran under (a subagent's in its parent's), and a reply that failed with no tokens as a failed call (a stopped one is none). They are read where the Sessions page reads OpenCode: opencode.db — OpenCode 2's session_v2/session_message (a compaction on the reply before it) or the older session/message, the old ones not yet copied while OpenCode 2 migrates — else the JSON files under storage/. A reply through magpie's gateway (provider "magpie", or "dial") is left out: the gateway logged it already. Each session is a call source of its own, read again whole only when it changed (its size is the messages' count plus the sum of their time_updated, so an older message written again tells too) and kept in the call cache on disk as a file's calls are; the database listing is kept while opencode.db and its WAL are unchanged, and a read-only handle is shared between sources and closed after 10 s unused. The Sessions listing is unchanged (allFiles still takes callFiles; Calls and CallSources take callSources). ContentOf says no place for an OpenCode call. OpenCode's own cost isn't used: calls are priced at magpie's effective prices, as the others are. TestOpenCodeCallsJSON, TestOpenCodeCallsDB (cache kept unchanged and across a restart, an updated message read again, a failed and a stopped reply) and TestOpenCodeCallsV2 (compactions follow the reply before them, magpie's skipped) fail on the old code (no OpenCode calls). With a read-only copy of this computer's ~/.local/share/opencode (JSON storage, 3 OpenCode Zen kimi-k2.5-free replies): 3 calls, input 12281, output 235 (123 + 112 reasoning), cache read 24573 — the same as jq over the message files and as SQL SUM over an opencode.db built from them. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 21:27:17 +08:00

123 lines
5.1 KiB
Go

package sessions
import (
"path/filepath"
"testing"
"time"
)
// ocCalls are the OpenCode calls Calls gives, by message id.
func ocCalls(t *testing.T) map[string]Call {
t.Helper()
out := map[string]Call{}
for _, c := range Calls(time.Time{}) {
if c.Agent == "opencode" {
if _, dup := out[c.Msg]; dup {
t.Fatalf("message %s counted twice", c.Msg)
}
out[c.Msg] = c
}
}
return out
}
// checkOCCalls checks the fixtures' replies as calls: the one through
// magpie's gateway (msg_0003, provider "magpie") left to the gateway's log,
// the subagent's in the session it ran under, the reasoning in the output.
func checkOCCalls(t *testing.T, cs map[string]Call) {
t.Helper()
if _, ok := cs["msg_0003"]; ok {
t.Fatalf("a reply through magpie's gateway counted again: %+v", cs["msg_0003"])
}
a, ok := cs["msg_0002"]
if !ok {
t.Fatalf("no call for OpenCode's own reply: %+v", cs)
}
if a.Model != "gpt-6-astra" || a.Upstream != "openai" || a.Session != ocMain || a.Cwd != "/work/oc" ||
a.Tokens != (Tokens{1000, 120, 5000, 0}) || a.Reasoning != 20 || a.Millis != 24000 ||
!a.Time.Equal(at("2026-09-26T10:00:30Z")) {
t.Fatalf("astra: %+v", a)
}
o, ok := cs["msg_0004"]
if !ok || o.Model != "claude-opus-5-5" || o.Upstream != "anthropic" || o.Session != ocMain ||
o.Tokens != (Tokens{40, 60, 700, 300}) || o.Millis != 30000 {
t.Fatalf("the subagent's opus: %+v", o)
}
if len(cs) != 2 {
t.Fatalf("want 2 OpenCode calls, got %+v", cs)
}
}
func TestOpenCodeCallsJSON(t *testing.T) {
setupMore(t)
checkOCCalls(t, ocCalls(t))
}
func TestOpenCodeCallsDB(t *testing.T) {
data, _ := setupMore(t)
db := ocMakeDB(t, filepath.Join(data, "opencode", "opencode.db"))
checkOCCalls(t, ocCalls(t))
// a session that hasn't changed (no message added, none updated) is
// not read again: what was read is kept
if _, err := db.Exec(`UPDATE message SET data = json_set(data, '$.tokens.input', 9999) WHERE id = 'msg_0002'`); err != nil {
t.Fatal(err)
}
if a := ocCalls(t)["msg_0002"]; a.Input != 1000 {
t.Fatalf("an unchanged session read again: %+v", a)
}
// nor after a restart: the call cache on disk has it
resetCalls()
if a := ocCalls(t)["msg_0002"]; a.Input != 1000 {
t.Fatalf("the call cache wasn't kept: %+v", a)
}
// a message updated as its reply goes on is read again
if _, err := db.Exec(`UPDATE message SET time_updated = time_updated + 1 WHERE id = 'msg_0002'`); err != nil {
t.Fatal(err)
}
if a := ocCalls(t)["msg_0002"]; a.Input != 9999 {
t.Fatalf("an updated reply: %+v", a)
}
// a failed reply is a failed call; one only stopped is none
ocInsert(t, db, ocChild, "msg_0005", 1790416990000, 1790417000000,
`{"id":"msg_0005","role":"assistant","modelID":"claude-opus-5-5","providerID":"anthropic","tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},"error":{"name":"APIError","data":{"message":"Overloaded","statusCode":529}},"time":{"created":1790416990000,"completed":1790417000000}}`)
ocInsert(t, db, ocChild, "msg_0006", 1790417010000, 1790417020000,
`{"id":"msg_0006","role":"assistant","modelID":"claude-opus-5-5","providerID":"anthropic","tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},"error":{"name":"MessageAbortedError","data":{"message":"aborted"}},"time":{"created":1790417010000}}`)
cs := ocCalls(t)
if e := cs["msg_0005"]; e.Error != "APIError" || e.ErrorText != "Overloaded" || e.Session != ocMain {
t.Fatalf("a failed reply: %+v", e)
}
if _, ok := cs["msg_0006"]; ok {
t.Fatalf("a stopped reply counted: %+v", cs["msg_0006"])
}
}
func TestOpenCodeCallsV2(t *testing.T) {
data, _ := setupMore(t)
db := ocMakeV2(t, filepath.Join(data, "opencode", "opencode.db"))
checkOCCalls(t, ocCalls(t))
// a compaction after a reply through magpie went through magpie too
if _, err := db.Exec(`INSERT INTO session_message VALUES ('msg_0005', ?, 'compaction', 9, 1790417000000, 1790417010000, ?)`, ocMain,
`{"status":"completed","reason":"auto","cost":0,"tokens":{"input":5,"output":1,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":1790417000000}}`); err != nil {
t.Fatal(err)
}
// and one after OpenCode's own reply is OpenCode's
if _, err := db.Exec(`INSERT INTO session_message VALUES ('msg_0007', ?, 'assistant', 10, 1790417020000, 1790417030000, ?), ('msg_0008', ?, 'compaction', 11, 1790417040000, 1790417050000, ?)`,
ocMain, `{"model":{"id":"big-pickle","providerID":"opencode"},"content":[],"cost":0,"tokens":{"input":7,"output":3,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":1790417020000,"completed":1790417030000}}`,
ocMain, `{"status":"completed","reason":"auto","cost":0,"tokens":{"input":11,"output":2,"reasoning":0,"cache":{"read":0,"write":0}},"time":{"created":1790417040000}}`); err != nil {
t.Fatal(err)
}
cs := ocCalls(t)
if _, ok := cs["msg_0005"]; ok {
t.Fatalf("a compaction through magpie counted: %+v", cs["msg_0005"])
}
if c := cs["msg_0007"]; c.Model != "big-pickle" || c.Upstream != "opencode" || c.Tokens != (Tokens{7, 3, 0, 0}) {
t.Fatalf("an OpenCode Zen reply: %+v", c)
}
if c := cs["msg_0008"]; c.Model != "big-pickle" || c.Upstream != "opencode" || c.Tokens != (Tokens{11, 2, 0, 0}) {
t.Fatalf("its compaction: %+v", c)
}
}