OpenAI bills gpt-6-astra, gpt-6.1-sol and gpt-6-luna over 272K input at 2× input and cache and 1.5× output, for the whole request. A price now has tiers: `magpie model price <m> 10,50,1,12.5 --tier 272k 20,75,2,25` (--tier can repeat). A tier applies when input + cache reads + cache writes is over its size, so 272,000 is not over and 272,001 is. The built-in prices take tiers only where models.dev lists one (cost.tiers of type "context"), and it does list one for those three. The ledger, each request and OTLP are costed at the tier the call reached. Session and day totals sum many calls, so they stay at the base price.
Anthropic bills a cache write kept for an hour at 2× input and one kept for 5 minutes at 1.25×. Its usage reports which is which in cache_creation.ephemeral_5m/1h_input_tokens. The gateway (API and Claude subscription) and Claude Code's transcripts now keep that split. A price's fifth part is the 1-hour write. When it isn't given, it is 2× input: that is Anthropic's rule, and only Anthropic reports 1-hour writes. Claude list prices carry the 2× too. cache_write_tokens is still the total, and the usage CSV adds cache_write_5m_tokens and cache_write_1h_tokens at the end. The app shows the split on a request's Cache and on the usage tooltips. Old prices and old records, which have no 1-hour part, read as before. The sessions cache (18) and the call cache (calls-v8) are re-read once.
In the GUI, the price boxes were number inputs and dropped what they didn't parse ("0,25"). They are now decimal text boxes that take both "0.25" and "0,25". A model's price row has a 1-hour box (empty means 2× input) and a quiet Long-context price row. That row shows the list's tier greyed in, or sits behind a link when the list has none. A bad size is refused by name. en/zh/ja/de.
Tests:
- New: TestPriceTiers, TestCostSplit, TestPriceJSON, TestModelPriceTiers, TestModelPriceTierCmd, TestAnthropicCacheWriteSplit and TestClaudeCacheWriteSplit. The ledger CSV test expects the new columns.
- model-price-tiers.test.cjs is new; model-price-edit and provider-price-rate are updated. All of them fail against the old assets and pass in WebKit. Chromium couldn't be launched here.
- go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
252 lines
9.8 KiB
Go
252 lines
9.8 KiB
Go
package sessions
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// These fixtures contain only invented identities, usage and tool names.
|
|
func claudeUsageLine(at time.Time, id, request, model string, tokens Tokens, content string, fast bool) []byte {
|
|
if content == "" {
|
|
content = `[]`
|
|
}
|
|
message := fmt.Sprintf(`"model":%q,"id":%q`, model, id)
|
|
if !fast {
|
|
message = fmt.Sprintf(`"id":%q,"model":%q`, id, model)
|
|
}
|
|
return []byte(fmt.Sprintf(`{"type":"assistant","timestamp":%q,"requestId":%q,"message":{%s,"role":"assistant","content":%s,"usage":{"input_tokens":%d,"output_tokens":%d,"cache_read_input_tokens":%d,"cache_creation_input_tokens":%d}}}`,
|
|
at.Format(time.RFC3339Nano), request, message, content, tokens.Input, tokens.Output, tokens.CacheRead, tokens.CacheWrite))
|
|
}
|
|
|
|
func claudeUsageReplies(s *state) int {
|
|
n := 0
|
|
for _, d := range s.Days {
|
|
n += d.Replies
|
|
}
|
|
return n
|
|
}
|
|
|
|
func TestClaudeUsageNonAdjacentReplay(t *testing.T) {
|
|
for _, fast := range []bool{false, true} {
|
|
t.Run(fmt.Sprint("fast=", fast), func(t *testing.T) {
|
|
s := &state{}
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
a, b := Tokens{10, 2, 30, 4, 0}, Tokens{20, 4, 60, 8, 0}
|
|
claudeLine(s, claudeUsageLine(at, "a", "req-a", "m", a, "", fast), true)
|
|
claudeLine(s, claudeUsageLine(at.Add(time.Second), "b", "req-b", "m", b, "", fast), true)
|
|
claudeLine(s, claudeUsageLine(at.Add(48*time.Hour), "a", "req-a", "m", a, "", fast), true)
|
|
want := a
|
|
want.add(b)
|
|
if s.Models["m"] != want || claudeUsageReplies(s) != 2 {
|
|
t.Fatalf("tokens=%+v replies=%d, want %+v / 2", s.Models, claudeUsageReplies(s), want)
|
|
}
|
|
if s.Days[dateOf(at)].Models["m"] != want {
|
|
t.Fatal("replay moved the original contribution to its observation date")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClaudeUsageRevisionAndTools(t *testing.T) {
|
|
for _, fast := range []bool{false, true} {
|
|
t.Run(fmt.Sprint("fast=", fast), func(t *testing.T) {
|
|
s := &state{}
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
old, updated := Tokens{10, 1, 30, 4, 0}, Tokens{10, 7, 30, 4, 0}
|
|
tool := `[{"type":"tool_use","id":"tool-1","name":"Read","input":{"requestId":"not-the-response"}}]`
|
|
newTool := `[{"type":"tool_use","id":"tool-2","name":"Skill","input":{"skill":"review"}}]`
|
|
line := func(d time.Duration, model string, tok Tokens, tools string) []byte {
|
|
return claudeUsageLine(at.Add(d), "a", "req-a", model, tok, tools, fast)
|
|
}
|
|
claudeLine(s, line(0, "m", old, tool), true)
|
|
claudeLine(s, line(time.Second, "m", updated, newTool), true)
|
|
claudeLine(s, line(2*time.Second, "m", old, tool), true)
|
|
claudeLine(s, line(time.Millisecond, "m", Tokens{10, 99, 30, 4, 0}, tool), true)
|
|
if s.Models["m"] != updated || claudeUsageReplies(s) != 1 {
|
|
t.Fatalf("old replay undid newer usage: %+v", s)
|
|
}
|
|
d := s.Days[dateOf(at)]
|
|
if d.Tools["Read"] != 1 || d.Tools["Skill"] != 1 || d.Skills["review"] != 1 {
|
|
t.Fatalf("tools=%v skills=%v", d.Tools, d.Skills)
|
|
}
|
|
// A genuinely newer correction can reduce tokens and change model.
|
|
correction := Tokens{8, 5, 20, 0, 0}
|
|
claudeLine(s, line(48*time.Hour, "m2", correction, ""), true)
|
|
if !s.Models["m"].zero() || s.Models["m2"] != correction || d.Replies != 1 {
|
|
t.Fatalf("correction failed: %+v", s)
|
|
}
|
|
if !d.Models["m"].zero() || s.Days[dateOf(at.Add(48*time.Hour))].Models["m2"] != correction {
|
|
t.Fatal("correction did not replace both date/model buckets")
|
|
}
|
|
claudeLine(s, line(49*time.Hour, "m", updated, newTool), true)
|
|
if s.Models["m2"] != correction || !s.Models["m"].zero() {
|
|
t.Fatal("known old snapshot with new observation time replaced correction")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClaudeUsageIdentity(t *testing.T) {
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
tok := Tokens{3, 4, 5, 6, 0}
|
|
cases := []struct {
|
|
name string
|
|
identities [][2]string
|
|
calls int
|
|
}{
|
|
{"missing request upgrade", [][2]string{{"a", ""}, {"a", "r1"}, {"a", ""}}, 1},
|
|
{"request conflict", [][2]string{{"a", "r1"}, {"a", "r2"}, {"a", "r1"}}, 2},
|
|
{"ambiguous missing request", [][2]string{{"a", "r1"}, {"a", "r2"}, {"a", ""}}, 3},
|
|
{"request only", [][2]string{{"", "r1"}, {"", "r1"}}, 1},
|
|
{"no identity", [][2]string{{"", ""}, {"", ""}}, 2},
|
|
{"different message same request", [][2]string{{"a", "r1"}, {"b", "r1"}}, 2},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s := &state{}
|
|
st := &callFile{Agent: "claude"}
|
|
for i, identity := range tc.identities {
|
|
b := claudeUsageLine(at.Add(time.Duration(i)*time.Second), identity[0], identity[1], "m", tok, "", true)
|
|
claudeLine(s, b, true)
|
|
claudeCallLine(st, b)
|
|
}
|
|
want := Tokens{}
|
|
for range tc.calls {
|
|
want.add(tok)
|
|
}
|
|
if s.Models["m"] != want || claudeUsageReplies(s) != tc.calls || len(st.Calls) != tc.calls {
|
|
t.Fatalf("tokens=%+v replies=%d calls=%d, want %+v / %d", s.Models, claudeUsageReplies(s), len(st.Calls), want, tc.calls)
|
|
}
|
|
if tc.name == "missing request upgrade" && st.Calls[0].RequestID != "r1" {
|
|
t.Fatal("request upgrade was not retained in call")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClaudeUsageMissingToolIdentityAndSubagent(t *testing.T) {
|
|
s := &state{}
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
tok := Tokens{3, 4, 5, 6, 0}
|
|
content := `[{"type":"tool_use","name":"Read","input":{}}]`
|
|
for i := range 2 {
|
|
claudeLine(s, claudeUsageLine(at.Add(time.Duration(i)*time.Second), "a", "r", "m", tok, content, true), false)
|
|
}
|
|
if s.Models["m"] != tok || claudeUsageReplies(s) != 0 || s.Days[dateOf(at)].Tools["Read"] != 2 {
|
|
t.Fatalf("subagent/missing tool ID behavior changed: %+v", s)
|
|
}
|
|
}
|
|
|
|
func TestClaudeToolRevisionRejectsReplayedName(t *testing.T) {
|
|
s := &state{}
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
for i, name := range []string{"Read", "Write", "Read"} {
|
|
content := fmt.Sprintf(`[{"type":"tool_use","id":"tool-1","name":%q,"input":{}}]`, name)
|
|
claudeLine(s, claudeUsageLine(at.Add(time.Duration(i)*time.Second), "a", "r", "m", Tokens{Input: 3}, content, true), true)
|
|
}
|
|
d := s.Days[dateOf(at)]
|
|
if d.Tools["Read"] != 0 || d.Tools["Write"] != 1 {
|
|
t.Fatalf("old tool snapshot undid newer name: %v", d.Tools)
|
|
}
|
|
}
|
|
|
|
func TestClaudeCallReplayKeepsCompletionWithoutUUID(t *testing.T) {
|
|
st := &callFile{Agent: "claude"}
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
tok := Tokens{3, 4, 5, 6, 0}
|
|
content := `[{"type":"text","text":"original block"}]`
|
|
claudeCallLine(st, claudeUsageLine(at, "a", "ra", "m", tok, content, true))
|
|
claudeCallLine(st, claudeUsageLine(at.Add(time.Second), "b", "rb", "m", tok, "", true))
|
|
claudeCallLine(st, claudeUsageLine(at.Add(2*time.Second), "a", "ra", "m", tok, content, true))
|
|
if len(st.Calls) != 2 || !st.Calls[0].Time.Equal(at) {
|
|
t.Fatalf("replay changed call completion time: %+v", st.Calls)
|
|
}
|
|
}
|
|
|
|
func TestClaudeRepeatedBlockRejectsZeroedHistoricalSnapshot(t *testing.T) {
|
|
for _, fast := range []bool{false, true} {
|
|
t.Run(fmt.Sprint("fast=", fast), func(t *testing.T) {
|
|
s, calls := &state{}, &callFile{Agent: "claude"}
|
|
at := time.Date(2026, 9, 25, 3, 0, 0, 0, time.UTC)
|
|
tokens := Tokens{69, 31852, 185103, 998, 0}
|
|
// Two blocks report the same whole-message usage. Historical
|
|
// copies later retain both original timestamps but zero usage.
|
|
for i, usage := range []Tokens{tokens, tokens, {}, {}} {
|
|
b := claudeUsageLine(at.Add(time.Duration(i%2)*time.Second), "a", "r", "m", usage, "", fast)
|
|
claudeLine(s, b, true)
|
|
claudeCallLine(calls, b)
|
|
}
|
|
if s.Models["m"] != tokens || len(calls.Calls) != 1 || calls.Calls[0].Tokens != tokens {
|
|
t.Fatalf("zeroed historical blocks removed original usage: summary=%v calls=%+v", s.Models, calls.Calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClaudeUsageResumeEquivalence(t *testing.T) {
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
old, updated := Tokens{10, 1, 30, 4, 0}, Tokens{10, 7, 30, 4, 0}
|
|
tool := `[{"type":"tool_use","id":"tool-1","name":"Read","input":{}}]`
|
|
lines := [][]byte{
|
|
claudeUsageLine(at, "a", "", "m", old, tool, true),
|
|
claudeUsageLine(at.Add(time.Second), "b", "rb", "m", old, "", false),
|
|
claudeUsageLine(at.Add(2*time.Second), "a", "ra", "m", updated, tool, true),
|
|
claudeUsageLine(at.Add(48*time.Hour), "a", "ra", "m", old, tool, false),
|
|
claudeUsageLine(at.Add(49*time.Hour), "a", "ra-other", "m", old, tool, true),
|
|
}
|
|
parse := func(split int) (*state, *callFile) {
|
|
s, st := &state{}, &callFile{Agent: "claude"}
|
|
for i, b := range lines {
|
|
if i == split {
|
|
s = s.clone()
|
|
st = st.clone()
|
|
}
|
|
claudeLine(s, b, true)
|
|
claudeCallLine(st, b)
|
|
}
|
|
return s, st
|
|
}
|
|
want, wantCalls := parse(-1)
|
|
for split := range len(lines) {
|
|
got, gotCalls := parse(split)
|
|
if !reflect.DeepEqual(got.Models, want.Models) || !reflect.DeepEqual(got.Days, want.Days) || !reflect.DeepEqual(gotCalls.Calls, wantCalls.Calls) {
|
|
t.Fatalf("split=%d differs from full parse", split)
|
|
}
|
|
}
|
|
// Cloning must not mutate identities, revisions or tool maps of the source.
|
|
s := &state{}
|
|
claudeLine(s, lines[0], true)
|
|
before := s.clone()
|
|
c := s.clone()
|
|
for _, b := range lines[1:] {
|
|
claudeLine(c, b, true)
|
|
}
|
|
if !reflect.DeepEqual(before, s) {
|
|
t.Fatal("continuing the clone modified its source state")
|
|
}
|
|
}
|
|
|
|
func TestClaudeFastRequestIdentityMatchesFullReader(t *testing.T) {
|
|
for _, raw := range []string{`"outer"`, `null`, `123`, `{}`, `true`} {
|
|
var expected string
|
|
for _, fast := range []bool{false, true} {
|
|
s := &state{}
|
|
line := claudeUsageLine(time.Now(), "msg", "placeholder", "m", Tokens{Input: 10}, `[{"type":"tool_use","id":"t","name":"Read","input":{"requestId":"nested"}}]`, fast)
|
|
line = []byte(strings.Replace(string(line), `"requestId":"placeholder"`, `"requestId":`+raw, 1))
|
|
claudeLine(s, line, true)
|
|
for _, branches := range s.Claude.Messages {
|
|
for _, m := range branches {
|
|
if !fast {
|
|
expected = m.Request
|
|
} else if m.Request != expected {
|
|
t.Fatalf("identity %s: got %q want %q", raw, m.Request, expected)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|