Files
magpie/internal/gateway/usage_cache_rounds_test.go
T
deephbz e72b6a3327 gateway: preserve cache splits and known 1h writes across updates (#1717)
Keep reported Anthropic cache_creation splits, including pure-5m replies,
through API and subscription parsing and client output. Only positive
counters replace known usage. Completed sums retain all counters and
split presence.

Across continuation tries, search rounds and internal bridge messages,
partial updates add earlier total writes only with a positive total. A
split with counts and zero 1h keeps the current message's known 1h before
adding earlier messages' usage. A zero-filled or absent split keeps the
per-counter rule. Omit a zero-filled partial split with a positive total
from output; retain a reported zero-total split. Split presence and the
parsed update's own count evidence remain separate.

Yetone's zero-filled bridge probe and matching continuation/search probes
failed at 0c9298d with 10 instead of 15 1h writes. Fable's partial-5m and
pure-5m cases failed at e1de9bc7 in bridge, continuation and both search
modes with the same loss. All pass after the repair. The 12-case matrix
matches main's client totals and 1h counts and continuation/search ledger
counts. A renderer test rejects a fabricated 50/0 split on a sparse delta.

Verified under sandbox HOME, pinned Go caches and nogui: focused race,
Darwin vet ./..., Linux/Windows gateway vet, gofmt and diff checks. Archive
mutations removing current-message carry, bypassing each of three callers,
and restoring the fabricated split each fail behavior assertions. Restored
archive passes. Earlier zero-total, unreported-split and pure-5m cases stay
green. No live-provider check in this repair.

Review (yetone's session, landed from the PR's head c4cf224 and 90cdb2f):
the partial-cache probes go red when carryUpdate is reverted by hand,
and the 12-case matrix gives the same client totals and 1h counts as
main a15e57656. Rebased on 8b84dc902; gofmt clean; GOOS=darwin/linux/windows
go vet -tags nogui ./... clean.
Full suite: set -o pipefail; go test -timeout 45m ./... at the rebased head -> 65 packages ok, internal/gui red only on TestPanelJoinsFullScreenSpaces ("the panel shown doesn't take the keyboard"), red the same without this change, filed as #2450.
2026-10-16 15:14:48 +08:00

103 lines
4.3 KiB
Go

package gateway
import (
"fmt"
"strings"
"testing"
)
// anthCacheStart reports the writes billed for one Anthropic round.
func anthCacheStart(writes int, split bool) string {
usage := fmt.Sprintf(`"cache_creation_input_tokens":%d`, writes)
if split {
usage += fmt.Sprintf(`,"cache_creation":{"ephemeral_5m_input_tokens":%d,"ephemeral_1h_input_tokens":0}`, writes)
}
return `{"type":"message_start","message":{"id":"m1","type":"message","role":"assistant","model":"m","content":[],"usage":{` + usage + `}}}`
}
func TestStreamCutZeroCacheSplitKeepsWrites(t *testing.T) {
f := &cutAnthropic{
cuts: map[int][]string{0: {anthCacheStart(30, false), anthText("hel")}},
whole: []string{
anthCacheStart(20, true), anthText("lo"),
`{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":4,"cache_creation_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0}}}`,
`{"type":"message_stop"}`,
},
}
body := continuationChatServer(t, "m", f)
if f.calls() != 2 {
t.Fatalf("calls = %d, want 2", f.calls())
}
if prefill, ok := f.lastAssistantText(t, 1); !ok || prefill != "hel" {
t.Fatalf("second try did not continue the cut reply: %q, %v", prefill, ok)
}
if got := chatTextOf(body); got != "hello" {
t.Fatalf("client's text = %q, want hello", got)
}
u := chatUsageOf(body)
details, _ := u["prompt_tokens_details"].(map[string]any)
if got := details["cache_write_tokens"]; got != 50.0 {
t.Fatalf("zero delta after cut try reduced client cache writes: got %v, want 50; usage %v", got, u)
}
}
func TestClaudeInternalMessagesKeepCacheWrites(t *testing.T) {
for _, tc := range []struct {
name, before, start, delta string
hour int
}{
{"zeroDelta", `"cache_creation_input_tokens":30`,
`"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":20,"ephemeral_1h_input_tokens":0}`,
`"cache_creation_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0}`, 0},
{"pure5mAfterHour", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":20}`,
`"cache_creation_input_tokens":30,"cache_creation":{"ephemeral_5m_input_tokens":30,"ephemeral_1h_input_tokens":0}`,
`"cache_creation_input_tokens":30,"cache_creation":{"ephemeral_5m_input_tokens":30,"ephemeral_1h_input_tokens":0}`, 20},
} {
t.Run(tc.name, func(t *testing.T) {
ev := func(e string) string { return `{"type":"stream_event","event":` + e + `}` }
lines := []string{
ev(`{"type":"message_start","message":{"id":"m1","model":"x","usage":{` + tc.before + `}}}`),
ev(`{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"t1","name":"WebSearch"}}`),
ev(`{"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":5}}`),
ev(`{"type":"message_stop"}`),
`{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"results"}]}}`,
ev(`{"type":"message_start","message":{"id":"m2","model":"x","usage":{` + tc.start + `}}}`),
ev(`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"answer"}}`),
ev(`{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":7,` + tc.delta + `}}`),
ev(`{"type":"message_stop"}`),
}
run := &subscriptionRun{}
seg := run.attach()
done := make(chan struct{})
go func() {
defer close(done)
run.readOutput(strings.NewReader(strings.Join(lines, "\n") + "\n"))
}()
var client Usage
var starts, stops int
var text string
for e := range seg {
switch e.Kind {
case KStart:
starts++
client.add(e.Usage)
case KUsage:
client.add(e.Usage)
case KStop:
stops++
case KText:
text += e.Text
}
}
<-done
if starts != 1 || stops != 1 || text != "answer" {
t.Fatalf("internal messages did not produce one reply: starts=%d stops=%d text=%q", starts, stops, text)
}
if client.CacheWrite != 50 || client.CacheWrite1h != tc.hour || client.Output != 12 {
t.Fatalf("internal-message usage = writes %d/%d, output %d; want 50/%d, 12", client.CacheWrite, client.CacheWrite1h, client.Output, tc.hour)
}
wantUsageSplit(t, client, &aCacheCreation{Ephemeral5m: 50 - tc.hour, Ephemeral1h: tc.hour})
})
}
}