diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index 1c117fa05..3f01997a3 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -884,7 +884,14 @@ Code's (#1779; its sessions are on the Sessions page too, from the same `TestCodeBuddySessionsListedUnderTheirName`), MiniMax Code's (#1605), Kimi Code's (#1607, [`calls_kimi.go`](../../internal/sessions/calls_kimi.go)) and Pi's, Raft's Pi agents' and omp's, here and in WSL (#1951, -[`calls_pi.go`](../../internal/sessions/calls_pi.go)). Each skips the +[`calls_pi.go`](../../internal/sessions/calls_pi.go)), and Qoder's and +Qoder CN's (`~/.qoder`, `~/.qoder-cn`), read as Claude Code's are +(`ccCallAgent`; eating_dream on X). Qoder writes a reply on its own models +(`auto` among them) with every count 0 unless `QODER_EXPOSE_TOKEN_USAGE` +is set (qodercli 1.1.0 on, and the app's worker), and with the reply's +`credits`: such a call has no tokens and no price, and its `Credits` are +summed as a plan's credits are; a reply written with no usage at all is no +call (`TestQoderCalls`, `TestDirectQoderCallsByCredits`). Each skips the calls the agent made through magpie's own entry, which the gateway logged with the account that answered: Kimi Code's are the ones whose `usage.record` model begins `magpie/`. Kimi Code's calls are its diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index 5f0b04710..ec897b444 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -427,7 +427,7 @@ func readCalls(f file) *callFile { if st.Claude != nil && (old == nil || old.Claude == nil) && !workingRevision(f.mod.UnixNano(), time.Now()) { st = st.withoutRevisions() } - if err == nil && (f.agent == "claude" || f.agent == "claude-desktop" || f.agent == "codex") { + if err == nil && (ccCallAgent(f.agent) || f.agent == "codex") { writeCallRevisions(st) } diff --git a/internal/sessions/call_continuation.go b/internal/sessions/call_continuation.go index 23033fb0b..bf157ca17 100644 --- a/internal/sessions/call_continuation.go +++ b/internal/sessions/call_continuation.go @@ -17,7 +17,7 @@ type callContinuation struct { // keepCallContinuation is called with callsMu held. Published continuations // are immutable; prepareCalls clones them before applying another append. func keepCallContinuation(st *callFile) { - if st.Agent != "codex" && st.Agent != "claude" && st.Agent != "claude-desktop" { + if st.Agent != "codex" && !ccCallAgent(st.Agent) { return } old, exists := callContinuations[st.Path] diff --git a/internal/sessions/call_revisions.go b/internal/sessions/call_revisions.go index d02b03157..3722d6b90 100644 --- a/internal/sessions/call_revisions.go +++ b/internal/sessions/call_revisions.go @@ -73,7 +73,7 @@ func withCallRevisions(old *callFile) *callFile { return old } codex := old.Agent == "codex" && (old.CX == nil || old.CX.Usage == nil) - if !codex && (old.Claude != nil || (old.Agent != "claude" && old.Agent != "claude-desktop")) { + if !codex && (old.Claude != nil || !ccCallAgent(old.Agent)) { return old } data, err := os.ReadFile(callRevisionsPath(old.Path)) diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go index 10cdb77e7..51ea84dcf 100644 --- a/internal/sessions/calls.go +++ b/internal/sessions/calls.go @@ -42,8 +42,11 @@ type Call struct { Upstream string `json:"upstream,omitempty"` AccountID string `json:"account_id,omitempty"` UserID string `json:"user_id,omitempty"` - Error string `json:"error,omitempty"` // Claude: the API error's kind (rate_limit, server_error …); such a call has no tokens - ErrorText string `json:"error_text,omitempty"` // its message, the first 400 characters + // Credits: what the call cost in its plan's own credits, as the file + // says (Qoder's, which writes no tokens of its own models) + Credits *float64 `json:"credits,omitempty"` + Error string `json:"error,omitempty"` // Claude: the API error's kind (rate_limit, server_error …); such a call has no tokens + ErrorText string `json:"error_text,omitempty"` // its message, the first 400 characters // Codex: the turn's own figures, set only when the turn made just this call TTFT int64 `json:"ttft_ms,omitempty"` // Compaction is Codex's explicit marker that this response compacted the context. @@ -335,7 +338,9 @@ func callFileLists(codex []file) [][]file { // file only appended to, read on as callFiles' are; DeepSeek Harness's, a // file only appended to that may be packed in zstd frames, is read on // from the frame it was left in (readDshCalls). Every agent here needs an -// entry in wholeCallReaders, lineCallReaders or resumeCallReaders. +// entry in wholeCallReaders, lineCallReaders or resumeCallReaders, but +// Qoder's and Qoder CN's (qoder.go), whose files are Claude Code's and read +// as Claude Code's are (ccCallAgent). func callSources() []file { return callSourcesWith(codexFiles()) } // callSourcesWith is callSources with codex the rollouts codexFiles found. @@ -349,7 +354,9 @@ func callSourcesInto(out []file, codex []file) []file { antigravityCallFiles(), // #2190 wslFiles("antigravity"), wslCallFiles("agy"), // @guyng35955 on X devinCallFiles(), // aohan0905 on X - workbuddyCallFiles(), grokCallFiles()) // #4423 + workbuddyCallFiles(), grokCallFiles(), // #4423 + // Claude Code's lines, read by claudeCallLine (eating_dream on X) + ccFiles("qoder", QoderDir("qoder")), ccFiles("qoder-cn", QoderDir("qoder-cn"))) if out == nil { return allowed(distinctOf(lists...)) } @@ -388,7 +395,7 @@ func prepareCalls(f file, old *callFile, owned bool) *callFile { } // Disk shards keep final calls, not message block/tool maps. A changed // Claude source reconstructs its transient revision index after restart. - if (f.agent == "claude" || f.agent == "claude-desktop") && old != nil && old.Claude == nil { + if ccCallAgent(f.agent) && old != nil && old.Claude == nil { old = nil } @@ -696,10 +703,37 @@ type ccCall struct { CacheRead int `json:"cache_read_input_tokens"` CacheWrite int `json:"cache_creation_input_tokens"` Creation *ccCacheCreation `json:"cache_creation"` + Credits ccNum `json:"credits"` } `json:"usage"` } `json:"message"` } +// ccNum is a number field that reads as unset when a file holds something +// else there, so a line is never lost for it. +type ccNum struct { + V float64 + Set bool +} + +func (n *ccNum) UnmarshalJSON(b []byte) error { + var v float64 + if json.Unmarshal(b, &v) == nil && !bytes.Equal(b, []byte("null")) { + *n = ccNum{v, true} + } + return nil +} + +// ccCallAgent says whether agent's session files are Claude Code's, its +// calls read by claudeCallLine: Claude Code's and Desktop's own, and +// Qoder's (qoder.go), whose lines are Claude Code's. +func ccCallAgent(agent string) bool { + switch agent { + case "claude", "claude-desktop", "qoder", "qoder-cn": + return true + } + return false +} + // claudeCallLine notes the call an assistant line is (a block of) in the // file: the latest of a message's lines stands for it, as it has the final // usage. A synthetic line is a call only as an API error. @@ -944,7 +978,15 @@ func claudeCall(st *callFile, b []byte, l *ccCall) { } c.Model = st.str(model) c.Tokens = ccTokens(u.Input, u.Output, u.CacheRead, u.CacheWrite, u.Creation) - if c.Tokens.zero() && m.Usage == nil { + if u.Credits.Set && (st.Agent == "qoder" || st.Agent == "qoder-cn") { + // Qoder writes its own models' replies with every count 0 + // unless QODER_EXPOSE_TOKEN_USAGE is set (qodercli 1.1.0 on, + // the app's worker too), and the credits the reply cost: a + // call of no tokens known, counted by what it cost + v := u.Credits.V + c.Credits = &v + } + if c.Tokens.zero() && m.Usage == nil && c.Credits == nil { return } if !asked.IsZero() && at.After(asked) { @@ -957,6 +999,10 @@ func claudeCall(st *callFile, b []byte, l *ccCall) { previous := &st.Calls[i] changed := previous.RequestID != m.Request previous.RequestID = m.Request + // Qoder's counts stay 0 while what the reply cost is told + if c.Credits != nil && (previous.Credits == nil || *previous.Credits != *c.Credits) { + previous.Credits, changed = c.Credits, true + } // A new block can finish a reply with unchanged usage. Retain // its duration/content boundary without moving usage across days. if newBlock && m.Usage.Model == c.Model && m.Usage.Tokens == c.Tokens && diff --git a/internal/sessions/calls_qoder_test.go b/internal/sessions/calls_qoder_test.go new file mode 100644 index 000000000..3a06ba8a4 --- /dev/null +++ b/internal/sessions/calls_qoder_test.go @@ -0,0 +1,78 @@ +package sessions + +import ( + "path/filepath" + "testing" + "time" +) + +// Qoder's and Qoder CN's own calls reach the usage ledger (eating_dream on +// X: Qoder's sessions were on the Sessions page and nowhere in Usage). The +// files in testdata/qoder-1.1.89 are lines as qodercli 1.1.89 writes them +// (its transcript writer, Xj and vUl, read from the npm bundle; the ids are +// made up): its own model is "auto", and a reply's usage has every count 0 +// with what it cost in Qoder's credits, as QODER_EXPOSE_TOKEN_USAGE unset +// leaves it. A reply whose usage named no credits is written with none at +// all, and is no call here. The fixture in testdata/qoder, with counts, is +// what a model of the user's own writes. +func TestQoderCalls(t *testing.T) { + home := setupAgents(t) + copyTree(t, "testdata/qoder-1.1.89/qoder", filepath.Join(home, ".qoder")) + copyTree(t, "testdata/qoder-1.1.89/qoder-cn", filepath.Join(home, ".qoder-cn")) + const qoderAuto, qoderCNAuto = "5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51", "7a2e4c6b-8d1f-4a3e-b5c7-9e0d2f4a6b83" + got := map[string][]Call{} + for _, c := range Calls(time.Time{}) { + if c.Agent == "qoder" || c.Agent == "qoder-cn" { + got[c.Agent+" "+c.Session] = append(got[c.Agent+" "+c.Session], c) + } + } + at := func(s string) time.Time { + v, _ := time.Parse(time.RFC3339Nano, s) + return v + } + for _, w := range []struct { + key string + msgs []string + times []string + credits []float64 + }{ + // newest first + {"qoder " + qoderAuto, []string{"msg_q2", "msg_q1"}, []string{"2026-11-15T08:12:09.330Z", "2026-11-15T08:12:05.871Z"}, []float64{0.18, 0.42}}, + {"qoder-cn " + qoderCNAuto, []string{"msg_c1"}, []string{"2026-11-15T09:00:03.250Z"}, []float64{1.5}}, + } { + cs := got[w.key] + if len(cs) != len(w.msgs) { + t.Fatalf("%s: want %d calls, got %d: %+v", w.key, len(w.msgs), len(cs), cs) + } + for i, c := range cs { + if c.Msg != w.msgs[i] || !c.Time.Equal(at(w.times[i])) || c.Model != "auto" || c.Tokens != (Tokens{}) || c.Credits == nil || *c.Credits != w.credits[i] { + t.Fatalf("%s call %d: %+v (credits %v)", w.key, i, c, c.Credits) + } + } + } + // a model of the user's own, its counts written: Qoder's subagent's + // reply is a call too + var tokens Tokens + for _, c := range got["qoder "+qoderID] { + tokens.Input += c.Input + tokens.Output += c.Output + tokens.CacheRead += c.CacheRead + if c.Model != "qwen3-coder-plus" || c.Credits != nil { + t.Fatalf("qoder %s: %+v", qoderID, c) + } + } + if tokens.Input == 0 || tokens.Output == 0 { + t.Fatalf("qoder %s: no tokens read from its counts: %+v", qoderID, got["qoder "+qoderID]) + } +} + +// A count that isn't a number loses only itself, not the reply's line. +func TestQoderCreditsOfAnotherType(t *testing.T) { + var l ccCall + if err := ccDecode([]byte(`{"type":"assistant","timestamp":"2026-11-15T08:12:05.871Z","message":{"id":"m","model":"auto","content":[],"usage":{"input_tokens":3,"credits":"0.4"}}}`), &l); err != nil { + t.Fatal(err) + } + if l.Message.Usage == nil || l.Message.Usage.Input != 3 || l.Message.Usage.Credits.Set { + t.Fatalf("%+v", l.Message.Usage) + } +} diff --git a/internal/sessions/qoder.go b/internal/sessions/qoder.go index b71410c47..66ef34cfd 100644 --- a/internal/sessions/qoder.go +++ b/internal/sessions/qoder.go @@ -14,6 +14,9 @@ import ( // "assistant" with the Anthropic usage (on a reply's last block only), the // cwd and the session id on each, "ai-title" and "summary" lines. They are // read as Claude Code's are. `qodercli --resume ` picks one up again. +// A reply on Qoder's own models ("auto" among them) has every count 0 +// unless QODER_EXPOSE_TOKEN_USAGE is set (qodercli 1.1.0 on), and the +// credits it cost (usage.credits), which its call carries instead. // QoderDir is the folder of Qoder's build of this id: $QODER_CONFIG_DIR, // else ~/.qoder, for qoder; $QODERCN_CONFIG_DIR, else ~/.qoder-cn, for diff --git a/internal/sessions/testdata/qoder-1.1.89/qoder-cn/projects/-Users-me-proj/7a2e4c6b-8d1f-4a3e-b5c7-9e0d2f4a6b83.jsonl b/internal/sessions/testdata/qoder-1.1.89/qoder-cn/projects/-Users-me-proj/7a2e4c6b-8d1f-4a3e-b5c7-9e0d2f4a6b83.jsonl new file mode 100644 index 000000000..8946cc5ee --- /dev/null +++ b/internal/sessions/testdata/qoder-1.1.89/qoder-cn/projects/-Users-me-proj/7a2e4c6b-8d1f-4a3e-b5c7-9e0d2f4a6b83.jsonl @@ -0,0 +1,2 @@ +{"type":"user","message":{"role":"user","content":"你好"},"uuid":"v1","timestamp":"2026-11-15T09:00:00.000Z","parentUuid":null,"isSidechain":false,"cwd":"/Users/me/proj","sessionId":"7a2e4c6b-8d1f-4a3e-b5c7-9e0d2f4a6b83","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} +{"type":"assistant","uuid":"b1","timestamp":"2026-11-15T09:00:03.250Z","message":{"id":"msg_c1","type":"message","role":"assistant","model":"auto","stop_reason":"end_turn","stop_sequence":null,"container":null,"context_management":null,"content":[{"type":"text","text":"你好!","citations":null}],"usage":{"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","credits":1.5,"original_credits":1.5,"billable":true}},"parentUuid":"v1","isSidechain":false,"cwd":"/Users/me/proj","sessionId":"7a2e4c6b-8d1f-4a3e-b5c7-9e0d2f4a6b83","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} diff --git a/internal/sessions/testdata/qoder-1.1.89/qoder/projects/-Users-me-proj/5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51.jsonl b/internal/sessions/testdata/qoder-1.1.89/qoder/projects/-Users-me-proj/5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51.jsonl new file mode 100644 index 000000000..beed8104c --- /dev/null +++ b/internal/sessions/testdata/qoder-1.1.89/qoder/projects/-Users-me-proj/5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51.jsonl @@ -0,0 +1,6 @@ +{"type":"user","message":{"role":"user","content":"Read main.go"},"uuid":"u1","timestamp":"2026-11-15T08:12:01.204Z","parentUuid":null,"isSidechain":false,"cwd":"/Users/me/proj","sessionId":"5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} +{"type":"assistant","uuid":"a1","timestamp":"2026-11-15T08:12:05.871Z","message":{"id":"msg_q1","type":"message","role":"assistant","model":"auto","stop_reason":"tool_use","stop_sequence":null,"container":null,"context_management":null,"content":[{"type":"tool_use","id":"call_q1","name":"Read","input":{"file_path":"/Users/me/proj/main.go"}}],"usage":{"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","credits":0.42,"original_credits":0.6,"billable":true}},"parentUuid":"u1","isSidechain":false,"cwd":"/Users/me/proj","sessionId":"5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} +{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"call_q1","content":"package main"}]},"uuid":"u2","timestamp":"2026-11-15T08:12:05.990Z","parentUuid":"a1","isSidechain":false,"cwd":"/Users/me/proj","sessionId":"5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} +{"type":"assistant","uuid":"a2","timestamp":"2026-11-15T08:12:09.330Z","message":{"id":"msg_q2","type":"message","role":"assistant","model":"auto","stop_reason":"end_turn","stop_sequence":null,"container":null,"context_management":null,"content":[{"type":"text","text":"It is an empty main package.","citations":null}],"usage":{"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","credits":0.18,"original_credits":0.25,"billable":true}},"parentUuid":"u2","isSidechain":false,"cwd":"/Users/me/proj","sessionId":"5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} +{"type":"user","message":{"role":"user","content":"Thanks"},"uuid":"u3","timestamp":"2026-11-15T08:13:00.000Z","parentUuid":"a2","isSidechain":false,"cwd":"/Users/me/proj","sessionId":"5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} +{"type":"assistant","uuid":"a3","timestamp":"2026-11-15T08:13:02.500Z","message":{"id":"msg_q3","type":"message","role":"assistant","model":"auto","stop_reason":"end_turn","stop_sequence":null,"container":null,"context_management":null,"content":[{"type":"text","text":"You're welcome.","citations":null}]},"parentUuid":"u3","isSidechain":false,"cwd":"/Users/me/proj","sessionId":"5f0c9a3e-2b7d-4c1a-9e8f-3a6b1d2c4e51","userType":"external","entrypoint":"cli","version":"1.1.89","gitBranch":"main"} diff --git a/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002.jsonl b/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002.jsonl index 6898ab9e2..ffb7c2eee 100644 --- a/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002.jsonl +++ b/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002.jsonl @@ -1,2 +1,2 @@ {"parentUuid":null,"isSidechain":false,"type":"user","uuid":"u1","timestamp":"2026-09-27T09:00:00.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000002","message":{"role":"user","content":"Explain the cache"}} -{"parentUuid":null,"isSidechain":false,"type":"assistant","uuid":"a1","timestamp":"2026-09-27T09:00:30.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000002","message":{"model":"glm-5","id":"m1","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":40,"cache_creation_input_tokens":0,"cache_read_input_tokens":300,"output_tokens":25}}} +{"parentUuid":null,"isSidechain":false,"type":"assistant","uuid":"a1","timestamp":"2026-09-27T09:00:30.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000002","message":{"model":"glm-5","id":"qcm1","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":40,"cache_creation_input_tokens":0,"cache_read_input_tokens":300,"output_tokens":25}}} diff --git a/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002/subagents/agent-a1.jsonl b/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002/subagents/agent-a1.jsonl index 5a6f6b7bc..7e254790a 100644 --- a/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002/subagents/agent-a1.jsonl +++ b/internal/sessions/testdata/qoder-cn/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000002/subagents/agent-a1.jsonl @@ -1 +1 @@ -{"parentUuid":null,"isSidechain":true,"type":"assistant","uuid":"a2","timestamp":"2026-09-27T09:00:20.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000002","message":{"model":"glm-5","id":"m2","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":5,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5}}} +{"parentUuid":null,"isSidechain":true,"type":"assistant","uuid":"a2","timestamp":"2026-09-27T09:00:20.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000002","message":{"model":"glm-5","id":"qcm2","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":5,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5}}} diff --git a/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001.jsonl b/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001.jsonl index eab78a636..1fcd95af4 100644 --- a/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001.jsonl +++ b/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001.jsonl @@ -1,2 +1,2 @@ {"parentUuid":null,"isSidechain":false,"type":"user","uuid":"u1","timestamp":"2026-09-27T09:00:00.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000001","message":{"role":"user","content":"Explain the cache"}} -{"parentUuid":null,"isSidechain":false,"type":"assistant","uuid":"a1","timestamp":"2026-09-27T09:00:30.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000001","message":{"model":"qwen3-coder-plus","id":"m1","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":40,"cache_creation_input_tokens":0,"cache_read_input_tokens":300,"output_tokens":25}}} +{"parentUuid":null,"isSidechain":false,"type":"assistant","uuid":"a1","timestamp":"2026-09-27T09:00:30.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000001","message":{"model":"qwen3-coder-plus","id":"qm1","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":40,"cache_creation_input_tokens":0,"cache_read_input_tokens":300,"output_tokens":25}}} diff --git a/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001/subagents/agent-a1.jsonl b/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001/subagents/agent-a1.jsonl index 1a983e989..f3411db1a 100644 --- a/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001/subagents/agent-a1.jsonl +++ b/internal/sessions/testdata/qoder/projects/-work-q/11111111-aaaa-4bbb-8ccc-000000000001/subagents/agent-a1.jsonl @@ -1 +1 @@ -{"parentUuid":null,"isSidechain":true,"type":"assistant","uuid":"a2","timestamp":"2026-09-27T09:00:20.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000001","message":{"model":"qwen3-coder-plus","id":"m2","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":5,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5}}} +{"parentUuid":null,"isSidechain":true,"type":"assistant","uuid":"a2","timestamp":"2026-09-27T09:00:20.000Z","cwd":"/work/q","sessionId":"11111111-aaaa-4bbb-8ccc-000000000001","message":{"model":"qwen3-coder-plus","id":"qm2","type":"message","role":"assistant","content":[{"type":"text","text":"..."}],"usage":{"input_tokens":5,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5}}} diff --git a/internal/usage/direct_test.go b/internal/usage/direct_test.go index 10e2b2f51..ae295a126 100644 --- a/internal/usage/direct_test.go +++ b/internal/usage/direct_test.go @@ -75,3 +75,56 @@ func TestDirectCountsOnlySessionFileCalls(t *testing.T) { t.Fatalf("today two days on: %+v", d.Totals) } } + +// Qoder's own calls are under Direct by Qoder (eating_dream on X), counted +// by the credits their replies cost, as Qoder writes its own models' with +// no tokens. Its "auto" routes inside Qoder and is no one model, so it has +// no price, though a catalog lists an "auto" of its own. +func TestDirectQoderCallsByCredits(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + os.WriteFile(catalog.CachePath(), []byte(`{"openrouter":{"id":"openrouter","models":{"auto":{"id":"auto","cost":{"input":2,"output":10}}}}}`), 0o644) + catalog.Reset() + t.Cleanup(catalog.Reset) + + now := holdClock(t, time.Date(2026, 11, 15, 12, 0, 0, 0, time.Local)) + credit := func(v float64) *float64 { return &v } + old := LogCalls + LogCalls = func(time.Time) []sessions.Call { + return []sessions.Call{ + {Time: now.Add(-time.Hour), Agent: "qoder", Session: "q1", Model: "auto", Msg: "msg_q2", Credits: credit(0.18)}, + {Time: now.Add(-time.Hour - time.Minute), Agent: "qoder", Session: "q1", Model: "auto", Msg: "msg_q1", Credits: credit(0.42)}, + {Time: now.Add(-2 * time.Hour), Agent: "qoder-cn", Session: "q2", Model: "auto", Msg: "msg_c1", Credits: credit(1.5)}, + // QODER_EXPOSE_TOKEN_USAGE=1 writes the counts + {Time: now.Add(-3 * time.Hour), Agent: "qoder", Session: "q3", Model: "auto", Msg: "msg_q4", Credits: credit(0.3), Tokens: sessions.Tokens{Input: 1000, Output: 100}}, + } + } + t.Cleanup(func() { LogCalls = old }) + + d := Direct(Month) + if d.Calls != 4 || d.Credited != 4 || d.Credits < 2.3999 || d.Credits > 2.4001 || d.Input != 1000 { + t.Fatalf("direct totals %+v", d.Totals) + } + if d.Cost != 0 || d.Unpriced != 1 { + t.Fatalf("Qoder's auto was priced: %+v", d.Totals) + } + agents := map[string]Group{} + for _, g := range d.Agents { + agents[g.ID] = g + } + if g := agents["qoder"]; g.Calls != 3 || g.Credited != 3 { + t.Fatalf("qoder: %+v of %+v", g, d.Agents) + } + if g := agents["qoder-cn"]; g.Calls != 1 || g.Credits != 1.5 { + t.Fatalf("qoder-cn: %+v of %+v", g, d.Agents) + } + for _, m := range d.Models { + if m.Model != "auto" || m.Cost != 0 { + t.Fatalf("models %+v", d.Models) + } + } +} diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index 1d15281fd..5d2eea72e 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -401,7 +401,8 @@ const UnknownProvider = "session-unknown" func logRecord(c sessions.Call) Record { r := Record{Time: c.Time, Agent: c.Agent, Provider: UnknownProvider, Model: c.Model, Served: c.Model, Requested: c.Requested, Input: c.Input, Output: c.Output, CacheRead: c.CacheRead, CacheWrite: c.CacheWrite, CacheWrite1h: c.CacheWrite1h, Reasoning: c.Reasoning, Effort: c.Effort, - Millis: c.Millis, TTFT: c.TTFT, Session: c.Session, RequestID: c.RequestID, MessageID: c.Msg, Error: c.ErrorText, ErrType: c.Error, Subagent: sessions.SubagentOf(c.File), ParentAgent: sessions.SubagentParent(c.File)} + Millis: c.Millis, TTFT: c.TTFT, Session: c.Session, RequestID: c.RequestID, MessageID: c.Msg, Error: c.ErrorText, ErrType: c.Error, Subagent: sessions.SubagentOf(c.File), ParentAgent: sessions.SubagentParent(c.File), + Credits: c.Credits} if c.Agent == "codex" { r.ResponseID = c.ResponseID r.Compaction = codexCompactionOf(c)