#1716 chunked only the history: the messages before historyEnd's split. A conversation with no reply that calls no tool, as a sub-agent's loop of tool calls that Claude Code's AgentSummary asks a summary of with its last results (JinJieBeWater on #1716), has split 0, so it was told in one block and its one mark stayed on the instructions: every such request wrote the whole loop again. Such a conversation now has its every message but the last chunked. A later request with a history is told it wrapped from its first message, so it never matched past the instructions before either. What was run: - TestClaudeToolLoopCachedAcrossNewRuns (new), TestClaudeHistoryCachedAcrossNewRuns, ClaudePrompt*, ClaudeInstructions*: pass. - Broken by hand, each seen red: no split-0 chunking; the last message chunked when it ends a chunk (a 32-message loop); boundaries counted from the end; the mark left on the instructions too; no chunk mark. - Real Max account, claude-haiku-5-5, Claude Code 2.1.296 through a recording proxy, each run started anew with magpie's rendered prompt, a loop of 20 then 24 tool rounds (42 then 50 messages): origin/main: second run read 3,790, wrote 70,764 #1716 alone: second run read 3,791, wrote 70,813 with this: second run read 48,661, wrote 25,993 Grown history (21 then 41 messages) and same history with a new ask are unchanged from #1716 alone (22,348/28,178 and 40,848/9,678). Every request kept to four marks, all 1h. - Not tried: a long turn to answer after a history is still one block, and images count toward Anthropic's 20-block lookback. Co-authored-by: deephbz <13776377+deephbz@users.noreply.github.com>
209 lines
8.3 KiB
Go
209 lines
8.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The caller's instructions are a block of their own, marked for an hour's
|
|
// cache, ahead of the messages: two requests with the same instructions
|
|
// and different messages share them in the cache. Claude Code writes an
|
|
// hour's cache too, as a 1h mark may not follow a 5m one.
|
|
func TestClaudeInstructionsCachedApart(t *testing.T) {
|
|
for _, said := range []string{"one", "two"} {
|
|
blocks, err := renderClaudePrompt(&Request{System: "rules", Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: said}}}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(blocks) != 2 || blocks[0]["text"] != "<external_system_instructions>\nrules\n</external_system_instructions>\n\n" {
|
|
t.Fatalf("blocks: %v", blocks)
|
|
}
|
|
if cc, _ := blocks[0]["cache_control"].(map[string]string); cc["ttl"] != "1h" || blocks[1]["cache_control"] != nil {
|
|
t.Fatalf("cache marks: %v", blocks)
|
|
}
|
|
if !strings.Contains(blocks[1]["text"].(string), "Human: "+said) {
|
|
t.Fatalf("messages: %v", blocks[1])
|
|
}
|
|
}
|
|
blocks, _ := renderClaudePrompt(&Request{Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}}}})
|
|
if len(blocks) != 1 || blocks[0]["cache_control"] != nil {
|
|
t.Fatalf("no instructions: %v", blocks)
|
|
}
|
|
env := cleanClaudeEnv([]string{"CLAUDE_CODE_PROMPT_CACHE_TTL=5m"})
|
|
if !slices.Contains(env, "CLAUDE_CODE_PROMPT_CACHE_TTL=1h") || slices.Contains(env, "CLAUDE_CODE_PROMPT_CACHE_TTL=5m") {
|
|
t.Fatalf("env: %q", env)
|
|
}
|
|
}
|
|
|
|
// Claude Code's system prompt begins with a billing line whose hash follows
|
|
// the request's first user message. Its auto mode classifier sends the same
|
|
// policy with another transcript each time: with the line kept, no check's
|
|
// instructions were ever read from the cache (X, AncientTwo). Two checks
|
|
// now share them.
|
|
func TestClaudeInstructionsWithoutBillingHeader(t *testing.T) {
|
|
var first []any
|
|
for i, hash := range []string{"8c9", "4ff"} {
|
|
body := `{"model":"m","max_tokens":64,"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.289.` + hash + `; cc_entrypoint=cli; cch=00000;"},{"type":"text","text":"You are a security monitor.","cache_control":{"type":"ephemeral"}}],"messages":[{"role":"user","content":"transcript ` + hash + `"}]}`
|
|
req, err := parseAnthropic([]byte(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blocks, err := renderClaudePrompt(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if blocks[0]["text"] != "<external_system_instructions>\nYou are a security monitor.\n</external_system_instructions>\n\n" {
|
|
t.Fatalf("check %d: %q", i, blocks[0]["text"])
|
|
}
|
|
first = append(first, blocks[0]["text"])
|
|
}
|
|
if first[0] != first[1] {
|
|
t.Fatalf("instructions differ: %q", first)
|
|
}
|
|
if got := withoutBillingHeader("Rules mention x-anthropic-billing-header: here"); got != "Rules mention x-anthropic-billing-header: here" {
|
|
t.Fatalf("a mention later on: %q", got)
|
|
}
|
|
}
|
|
|
|
// A run started anew for a conversation with history marks the history's
|
|
// last whole chunk, not the instructions, and a later run told more of the
|
|
// same conversation repeats every block up to that mark: Anthropic finds
|
|
// what the first wrote 20 blocks back from the second's mark. Before, the
|
|
// history went in one block with the turn to answer, and each run started
|
|
// anew wrote all of it again (a change of tools, a turn that missed its run).
|
|
func TestClaudeHistoryCachedAcrossNewRuns(t *testing.T) {
|
|
conversation := func(replies int) *Request {
|
|
req := &Request{System: "rules", Tools: []Tool{{Name: "read"}}}
|
|
for i := 0; i < replies; i++ {
|
|
req.Messages = append(req.Messages,
|
|
Message{Role: "user", Parts: []Part{{Kind: Text, Text: fmt.Sprintf("question %d", i)}}},
|
|
Message{Role: "assistant", Parts: []Part{{Kind: Text, Text: fmt.Sprintf("answer %d", i)}, {Kind: ToolCall, ID: fmt.Sprintf("c%d", i), Name: "read", Args: json.RawMessage(`{"path":"a"}`)}}},
|
|
Message{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: fmt.Sprintf("c%d", i), Text: "contents"}}},
|
|
Message{Role: "assistant", Parts: []Part{{Kind: Text, Text: fmt.Sprintf("done %d", i)}}})
|
|
}
|
|
req.Messages = append(req.Messages, Message{Role: "user", Parts: []Part{{Kind: Text, Text: "the turn to answer"}}})
|
|
return req
|
|
}
|
|
marked := func(blocks []map[string]any) []int {
|
|
var at []int
|
|
for i, b := range blocks {
|
|
if cc, ok := b["cache_control"].(map[string]string); ok {
|
|
if cc["ttl"] != "1h" {
|
|
t.Fatalf("block %d marked %v", i, cc)
|
|
}
|
|
at = append(at, i)
|
|
}
|
|
}
|
|
return at
|
|
}
|
|
earlier, err := renderClaudePrompt(conversation(10))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
later, err := renderClaudePrompt(conversation(25))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first, second := marked(earlier), marked(later)
|
|
if len(first) != 1 || len(second) != 1 || first[0] == 0 || second[0] <= first[0] {
|
|
t.Fatalf("marks: earlier %v, later %v", first, second)
|
|
}
|
|
if second[0]-first[0] > 20 {
|
|
t.Fatalf("the earlier mark is %d blocks back, past Anthropic's lookback", second[0]-first[0])
|
|
}
|
|
for i := 0; i <= first[0]; i++ {
|
|
a, b := earlier[i]["text"], later[i]["text"]
|
|
if a != b {
|
|
t.Fatalf("block %d differs:\n%q\n%q", i, a, b)
|
|
}
|
|
}
|
|
if strings.Contains(earlier[first[0]]["text"].(string), "the turn to answer") {
|
|
t.Fatalf("the marked block holds the turn to answer")
|
|
}
|
|
// a conversation with less than a chunk of history keeps the mark on the instructions
|
|
short, _ := renderClaudePrompt(conversation(1))
|
|
if at := marked(short); len(at) != 1 || at[0] != 0 {
|
|
t.Fatalf("short conversation marks %v", at)
|
|
}
|
|
}
|
|
|
|
// A conversation with no reply that calls no tool, as a sub-agent's loop
|
|
// of tool calls that Claude Code's AgentSummary asks a summary of with its
|
|
// last results, has no history to chunk: its messages before the last are
|
|
// chunked, so a later request of the same loop, started anew, reads what
|
|
// the earlier one wrote (#1716, JinJieBeWater).
|
|
func TestClaudeToolLoopCachedAcrossNewRuns(t *testing.T) {
|
|
loop := func(rounds int, ask string) *Request {
|
|
req := &Request{System: "rules", Tools: []Tool{{Name: "read"}}}
|
|
req.Messages = append(req.Messages, Message{Role: "user", Parts: []Part{{Kind: Text, Text: "the task"}}})
|
|
for i := 0; i < rounds; i++ {
|
|
last := []Part{{Kind: ToolResult, CallID: fmt.Sprintf("c%d", i), Text: "contents"}}
|
|
if i == rounds-1 {
|
|
last = append(last, Part{Kind: Text, Text: ask})
|
|
}
|
|
req.Messages = append(req.Messages,
|
|
Message{Role: "assistant", Parts: []Part{{Kind: ToolCall, ID: fmt.Sprintf("c%d", i), Name: "read", Args: json.RawMessage(`{"path":"a"}`)}}},
|
|
Message{Role: "user", Parts: last})
|
|
}
|
|
return req
|
|
}
|
|
marked := func(blocks []map[string]any) []int {
|
|
var at []int
|
|
for i, b := range blocks {
|
|
if _, ok := b["cache_control"].(map[string]string); ok {
|
|
at = append(at, i)
|
|
}
|
|
}
|
|
return at
|
|
}
|
|
earlier, err := renderClaudePrompt(loop(20, "summarize so far"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
later, err := renderClaudePrompt(loop(24, "summarize again"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if historyEnd(loop(20, "x").Messages) != 0 {
|
|
t.Fatal("the loop has a history")
|
|
}
|
|
first, second := marked(earlier), marked(later)
|
|
if len(first) != 1 || len(second) != 1 || first[0] == 0 || second[0] < first[0] || second[0]-first[0] > 20 {
|
|
t.Fatalf("marks: earlier %v, later %v", first, second)
|
|
}
|
|
for i := 0; i <= first[0]; i++ {
|
|
if earlier[i]["text"] != later[i]["text"] {
|
|
t.Fatalf("block %d differs:\n%q\n%q", i, earlier[i]["text"], later[i]["text"])
|
|
}
|
|
}
|
|
if strings.Contains(earlier[first[0]]["text"].(string), "summarize") {
|
|
t.Fatalf("the marked block holds the last message")
|
|
}
|
|
// the last message is never in a chunk, when it would end one either
|
|
even := loop(15, "summarize at the end of a chunk")
|
|
even.Messages = append([]Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "context"}}}}, even.Messages...)
|
|
if len(even.Messages)%historyChunk != 0 {
|
|
t.Fatalf("%d messages", len(even.Messages))
|
|
}
|
|
blocks := mustRender(t, even)
|
|
if at := marked(blocks); len(at) != 1 || strings.Contains(blocks[at[0]]["text"].(string), "summarize") {
|
|
t.Fatalf("marks %v in %v", at, blocks)
|
|
}
|
|
// a loop shorter than a chunk keeps the mark on the instructions
|
|
if at := marked(mustRender(t, loop(3, "x"))); len(at) != 1 || at[0] != 0 {
|
|
t.Fatalf("short loop marks %v", at)
|
|
}
|
|
}
|
|
|
|
func mustRender(t *testing.T, req *Request) []map[string]any {
|
|
t.Helper()
|
|
blocks, err := renderClaudePrompt(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return blocks
|
|
}
|