Usage counts the gateway's calls and the calls the agents' own session files record (sessions.Calls). Kimi Code had no reader, so its calls straight to api.kimi.com on Kimi's own sign-in never showed (the reporter's 47 k3 calls of one session on 2026-10-11). The reader (calls_kimi.go) is built from the redacted real wire files infinitr0us shared from two machines (protocols 1.0, 1.4, 1.5; text replaced, every id, model and number kept) and their notes from Kimi Code's source. It reads every agents/*/wire.jsonl under $KIMI_CODE_HOME/sessions, else ~/.kimi-code/sessions (as agent.KimiDir finds the new Kimi Code), main and subagents, ses_* folders included. One call per usage.record: inputOther, inputCacheRead and inputCacheCreation go to Input, CacheRead and CacheWrite, output as is, the record's own model and time. Nothing is paired with step.end, since 1.4 and 1.5 write them in opposite orders and the June 1.4 build has no messageId or llm.request. A model beginning "magpie/" (magpie's own entry in Kimi's config) is skipped: the gateway counted it. kimi fork copies the parent's records unchanged, so a call is keyed by agent (agentId, else its agents/<id> folder), time, model and usage, and Calls counts a key once. It is a whole-file call source (callSources, wholeCallReaders) and the folder is watched (Dirs). The subsystem page gets the list of call readers. Fixtures (testdata/kimi-code) are the reporter's files cut to the lines a call is read from, each line verbatim (50 KB; one of the July session's eight agents kept). Run: the reader over the reporter's whole, untrimmed files gives exactly their README's totals (32/2,841,972/12,283; 151/7,025,587/122,782; 6/147,822/2,009; 10/338,328/2,176; 0 for the four others). TestKimiCodeCalls (both homes), TestKimiCodeSkipsCallsThroughMagpie, TestKimiCodeForkCountsOnce, TestKimiCodeBrokenFiles (cut mid-record, empty, junk, null/negative/timeless usage) pass. Broken by hand, each goes red: no magpie skip, no key, key without agent, no negative or time guard, main only, KIMI_CODE_HOME ignored, cache read dropped. go test -tags nogui ./internal/sessions ./internal/usage ./internal/cli ./internal/gui under a sandbox HOME, gofmt -l, go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet. Not tried: a live Kimi Code run (fixtures are the reporter's real files, not a sandbox capture), a successful call through magpie (the magpie-routed record in the test is the reporter's quoted 1.5 line with a magpie/ model), a compaction record (usageScope "session", none in the files), Kimi Code in WSL (not read), Windows paths at runtime. A subagent's calls carry the session but no Subagent field. Thanks to infinitr0us for the write-up from Kimi's source and the redacted wire files. Reported-by: infinitr0us
235 lines
8.9 KiB
Go
235 lines
8.9 KiB
Go
package sessions
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// #1607 (infinitr0us): Kimi Code's own calls reach the usage ledger. The
|
|
// sessions in testdata/kimi-code are the reporter's real wire files from
|
|
// two machines (redacted by them: text replaced, every id, model and number
|
|
// kept), cut down to the lines a call is read from (metadata, llm.request,
|
|
// step.end, usage.record, turn.cancel), each line as it was:
|
|
//
|
|
// - wd_v14-june-subagent: the June 1.4 build, no llm.request and no
|
|
// messageId; main and one subagent (agent-0), 25 + 7 calls
|
|
// - wd_v14-july-subagents: a July 1.4 subagent's file (agent-0), 10 calls
|
|
// - wd_v14-cancel: a turn cancelled mid-request, whose last llm.request
|
|
// has no usage.record, 6 calls
|
|
// - wd_v15-early-no-agentid: 1.5 records written before agentId, 10 calls
|
|
// - wd_v15-magpie-failed: a failed call through magpie, no usage.record
|
|
// - wd_v14-no-turn, wd_v14-reopened-ses, wd_v10-migrated: no calls
|
|
//
|
|
// The totals for the whole files (32, 151, 6 and 10 calls) are the
|
|
// reporter's README's, and the reader gives the same on them.
|
|
type kimiTotal struct{ n, input, cacheRead, cacheWrite, output int }
|
|
|
|
var kimiWant = map[string]kimiTotal{
|
|
"session_a0a7c8d3-2a5a-480d-9d42-740257598abd": {32, 132475 + 68345, 2399488 + 241664, 0, 12283},
|
|
"session_1e5ea5b2-382a-4788-aa81-dc0e5b99f818": {10, 48900, 312832, 0, 9249},
|
|
"session_169766c9-86f5-4577-97a3-a51d402db5b9": {6, 15726, 132096, 0, 2009},
|
|
"session_fc16731e-2bfe-4873-a6cb-4b9e73280360": {10, 20888, 317440, 0, 2176},
|
|
}
|
|
|
|
func kimiCalls() []Call {
|
|
var out []Call
|
|
for _, c := range Calls(time.Time{}) {
|
|
if c.Agent == "kimi" {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func kimiTotals(cs []Call) map[string]kimiTotal {
|
|
out := map[string]kimiTotal{}
|
|
for _, c := range cs {
|
|
t := out[c.Session]
|
|
t.n++
|
|
t.input += c.Input
|
|
t.cacheRead += c.CacheRead
|
|
t.cacheWrite += c.CacheWrite
|
|
t.output += c.Output
|
|
out[c.Session] = t
|
|
}
|
|
return out
|
|
}
|
|
|
|
// kimiHome is a sandbox's Kimi Code folder holding the reporter's sessions.
|
|
func kimiHome(t *testing.T) string {
|
|
claude, _ := setup(t)
|
|
dir := filepath.Join(filepath.Dir(claude), "home", ".kimi-code")
|
|
copyTree(t, "testdata/kimi-code", dir)
|
|
return dir
|
|
}
|
|
|
|
func TestKimiCodeCalls(t *testing.T) {
|
|
for _, viaEnv := range []bool{false, true} {
|
|
name := "~/.kimi-code"
|
|
if viaEnv {
|
|
name = "KIMI_CODE_HOME"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
claude, _ := setup(t)
|
|
dir := filepath.Join(filepath.Dir(claude), "home", ".kimi-code")
|
|
if viaEnv {
|
|
dir = filepath.Join(filepath.Dir(claude), "elsewhere")
|
|
t.Setenv("KIMI_CODE_HOME", dir)
|
|
}
|
|
copyTree(t, "testdata/kimi-code", dir)
|
|
got := kimiCalls()
|
|
if tot := kimiTotals(got); !equalTotals(tot, kimiWant) {
|
|
t.Fatalf("totals by session\n got %+v\nwant %+v", tot, kimiWant)
|
|
}
|
|
var first *Call
|
|
for i, c := range got {
|
|
if c.Msg == "" {
|
|
t.Errorf("call %+v has no key to count it once by", c)
|
|
}
|
|
if c.Time.Equal(time.UnixMilli(1781503063433)) {
|
|
first = &got[i]
|
|
}
|
|
}
|
|
if first == nil {
|
|
t.Fatal("the June subagent's first call is missing")
|
|
}
|
|
if first.Model != "kimi-code/kimi-for-coding" || first.Session != "session_a0a7c8d3-2a5a-480d-9d42-740257598abd" ||
|
|
first.Tokens != (Tokens{Input: 2823, Output: 253, CacheRead: 6400}) {
|
|
t.Errorf("first call %+v", *first)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func equalTotals(a, b map[string]kimiTotal) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for k, v := range a {
|
|
if b[k] != v {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// kimiRecordLine is a 1.5 usage.record with agentId, as the reporter quoted
|
|
// it from their own file on the issue.
|
|
const kimiRecordLine = `{"type":"usage.record","agentId":"main","model":"kimi-code/k3","usage":{"inputOther":20257,"output":230,"inputCacheRead":0,"inputCacheCreation":0},"usageScope":"turn","time":1791696952640}`
|
|
|
|
func writeKimiWire(t *testing.T, home, session, agent string, lines ...string) {
|
|
t.Helper()
|
|
p := filepath.Join(home, "sessions", "wd_test_000000000009", session, "agents", agent, "wire.jsonl")
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(strings.Join(lines, "\n")), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// A call Kimi made through magpie's own entry (a model table keyed
|
|
// magpie/<provider>/<model>, as the reporter's failed call's modelAlias
|
|
// is) is the gateway's to count. The record is the quoted one with that
|
|
// model: no successful call through magpie was in the files shared.
|
|
func TestKimiCodeSkipsCallsThroughMagpie(t *testing.T) {
|
|
home := kimiHome(t)
|
|
viaMagpie := strings.Replace(kimiRecordLine, `"kimi-code/k3"`, `"magpie/mistral/zai-glm-5-3"`, 1)
|
|
viaMagpie = strings.Replace(viaMagpie, `1791696952640`, `1791696960000`, 1)
|
|
writeKimiWire(t, home, "session_through-magpie", "main", kimiRecordLine, viaMagpie, "")
|
|
var got []Call
|
|
for _, c := range kimiCalls() {
|
|
if c.Session == "session_through-magpie" {
|
|
got = append(got, c)
|
|
}
|
|
}
|
|
if len(got) != 1 || got[0].Model != "kimi-code/k3" {
|
|
t.Fatalf("want only the call on Kimi's own sign-in, got %+v", got)
|
|
}
|
|
if want := (Tokens{Input: 20257, Output: 230}); got[0].Tokens != want || !got[0].Time.Equal(time.UnixMilli(1791696952640)) {
|
|
t.Errorf("call %+v", got[0])
|
|
}
|
|
}
|
|
|
|
// kimi fork copies its parent's records into the new session unchanged:
|
|
// each call counts once, in the session that made it.
|
|
func TestKimiCodeForkCountsOnce(t *testing.T) {
|
|
home := kimiHome(t)
|
|
parent := filepath.Join(home, "sessions", "wd_v14-cancel_000000000003", "session_169766c9-86f5-4577-97a3-a51d402db5b9", "agents", "main", "wire.jsonl")
|
|
b, err := os.ReadFile(parent)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a fork of a 1.4 session, then a 1.5 call of its own
|
|
own := strings.Replace(kimiRecordLine, `1791696952640`, `1791696970000`, 1)
|
|
writeKimiWire(t, home, "session_fork", "main", strings.TrimRight(string(b), "\n"), `{"type":"forked","time":1791696950000}`, own, "")
|
|
later := time.Now().Add(time.Hour)
|
|
fork := filepath.Join(home, "sessions", "wd_test_000000000009", "session_fork", "agents", "main", "wire.jsonl")
|
|
if err := os.Chtimes(fork, later, later); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// and two subagents of one session whose 1.5 records name themselves:
|
|
// the same call in two agents' files is two calls
|
|
sub := strings.Replace(kimiRecordLine, `"agentId":"main"`, `"agentId":"agent-0"`, 1)
|
|
writeKimiWire(t, home, "session_agents", "agent-0", sub)
|
|
writeKimiWire(t, home, "session_agents", "main", kimiRecordLine)
|
|
|
|
tot := kimiTotals(kimiCalls())
|
|
if got := tot["session_169766c9-86f5-4577-97a3-a51d402db5b9"]; got != kimiWant["session_169766c9-86f5-4577-97a3-a51d402db5b9"] {
|
|
t.Errorf("the parent's calls %+v", got)
|
|
}
|
|
if got := tot["session_fork"]; got.n != 1 || got.input != 20257 {
|
|
t.Errorf("the fork's own calls %+v, want its one call after the fork", got)
|
|
}
|
|
if got := tot["session_agents"]; got.n != 2 {
|
|
t.Errorf("two agents' calls at one time %+v, want 2", got)
|
|
}
|
|
}
|
|
|
|
// A file cut short, empty or holding lines that aren't records reads as
|
|
// what it holds: no crash, and no call made up.
|
|
func TestKimiCodeBrokenFiles(t *testing.T) {
|
|
home := kimiHome(t)
|
|
src := filepath.Join(home, "sessions", "wd_v14-cancel_000000000003", "session_169766c9-86f5-4577-97a3-a51d402db5b9", "agents", "main", "wire.jsonl")
|
|
b, err := os.ReadFile(src)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the copy below would count once with its original: take that away
|
|
if err := os.RemoveAll(filepath.Dir(filepath.Dir(filepath.Dir(src)))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// cut in the middle of the last usage.record: 5 whole calls are left
|
|
last := bytes.LastIndex(b, []byte(`{"type":"usage.record"`))
|
|
if last < 0 {
|
|
t.Fatal("no usage.record in the fixture")
|
|
}
|
|
writeKimiWire(t, home, "session_cut", "main", string(b[:last+60]))
|
|
writeKimiWire(t, home, "session_empty", "main")
|
|
writeKimiWire(t, home, "session_junk", "main",
|
|
"\x00\xff not json",
|
|
`{"type":"usage.record"`,
|
|
`{"type":"usage.record","model":"kimi-code/k3","usage":null,"time":1791696952640}`,
|
|
`{"type":"usage.record","model":"kimi-code/k3","usage":{"inputOther":-5,"output":3},"time":1791696952641}`,
|
|
`{"type":"usage.record","model":"kimi-code/k3","usage":{"inputOther":5,"output":3}}`,
|
|
`{"type":"usage.record","model":"kimi-code/k3","usage":{"inputOther":0,"output":0},"time":1791696952642}`,
|
|
`{"type":"context.append_loop_event","event":{"type":"step.end","usage":{"inputOther":7,"output":1},"messageId":"usage.record"},"time":1791696952643}`,
|
|
`["usage.record"]`,
|
|
strings.Replace(kimiRecordLine, `"agentId":"main"`, `"agentId":"main","usage":"x"`, 1),
|
|
)
|
|
tot := kimiTotals(kimiCalls())
|
|
if got := tot["session_cut"]; got.n != 5 || got.input+got.cacheRead != 147822-(468+25856) {
|
|
// the last record of the cancel session: inputOther 468, cache 25856
|
|
t.Errorf("cut file %+v, want its 5 whole calls", got)
|
|
}
|
|
for _, s := range []string{"session_empty", "session_junk"} {
|
|
if got, ok := tot[s]; ok {
|
|
t.Errorf("%s: %+v, want no calls", s, got)
|
|
}
|
|
}
|
|
}
|