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.
90 lines
3.8 KiB
Go
90 lines
3.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCachePartialUpdateMatrix(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, fields string
|
|
hour int
|
|
}{
|
|
{"A_zeroFilled", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0}`, 15},
|
|
{"B_partial5m", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":3,"ephemeral_1h_input_tokens":0}`, 15},
|
|
{"C_partialBoth", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":3,"ephemeral_1h_input_tokens":2}`, 12},
|
|
{"D_partial1h", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":2}`, 12},
|
|
{"E_pure5m", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":20,"ephemeral_1h_input_tokens":0}`, 15},
|
|
{"F_pure1h", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":20}`, 30},
|
|
{"G_unreported", `"cache_creation_input_tokens":20`, 15},
|
|
{"H_zeroTotal", `"cache_creation_input_tokens":0`, 15},
|
|
{"I_zeroTotalSplit", `"cache_creation_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0}`, 15},
|
|
{"J_noCache", `"output_tokens":7`, 15},
|
|
{"K_splitOnly", `"cache_creation":{"ephemeral_5m_input_tokens":15,"ephemeral_1h_input_tokens":5}`, 15},
|
|
{"L_consistent", `"cache_creation_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":15,"ephemeral_1h_input_tokens":5}`, 15},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
delta := `{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{` + tc.fields + `}}`
|
|
t.Run("bridge", func(t *testing.T) { checkCacheBridge(t, delta, tc.hour) })
|
|
t.Run("continuation", func(t *testing.T) { checkCacheContinuation(t, delta, tc.hour) })
|
|
t.Run("search", func(t *testing.T) { checkCacheSearch(t, delta, tc.hour) })
|
|
})
|
|
}
|
|
}
|
|
|
|
func checkCacheBridge(t *testing.T, delta string, hour int) {
|
|
t.Helper()
|
|
ev := func(e string) string { return `{"type":"stream_event","event":` + e + `}` }
|
|
lines := []string{
|
|
ev(cacheSplitRoundStart(zeroSplitBefore)),
|
|
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(cacheSplitRoundStart(zeroSplitStart)),
|
|
ev(`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"answer"}}`),
|
|
ev(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
|
|
for e := range seg {
|
|
if e.Kind == KStart || e.Kind == KUsage {
|
|
client.add(e.Usage)
|
|
}
|
|
}
|
|
<-done
|
|
if client.CacheWrite != 50 || client.CacheWrite1h != hour {
|
|
t.Errorf("client writes: %d/hour%d, want50/hour%d", client.CacheWrite, client.CacheWrite1h, hour)
|
|
}
|
|
}
|
|
|
|
func TestCacheZeroFilledPartialUpdateOmitsSplit(t *testing.T) {
|
|
before := Usage{CacheWrite: 30, CacheWrite1h: 10}
|
|
for _, tc := range []struct {
|
|
name string
|
|
total int
|
|
}{
|
|
{"positiveTotal", 20},
|
|
{"zeroTotal", 0},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
u := aUsage{CacheCreationInputTokens: tc.total, CacheCreation: &aCacheCreation{}}.usage().plus(before, true)
|
|
got := u.anthropic().CacheCreation
|
|
if tc.total > 0 {
|
|
if got != nil {
|
|
t.Fatalf("zero-filled partial update rendered split %+v, want none", got)
|
|
}
|
|
} else if got == nil || *got != (aCacheCreation{}) {
|
|
t.Fatalf("zero-total split = %+v, want reported zero split", got)
|
|
}
|
|
})
|
|
}
|
|
}
|