include tool semantics in summary cache

This commit is contained in:
Ollie Agent 2026-08-15 13:46:29 +02:00
parent 82ab9dd2e3
commit 5dbfa3060f
3 changed files with 12 additions and 1 deletions

View File

@ -104,7 +104,8 @@ func toolMessageID(m backend.Message) string {
const coldSummaryCacheVersion = "v2"
func toolSummaryHash(m backend.Message) string {
h := sha256.Sum256([]byte(normalizeSummaryCacheContent(m.Content)))
input := coldSummaryCacheVersion + "\x00" + m.ToolName + "\x00" + normalizeSummaryCacheContent(m.Content)
h := sha256.Sum256([]byte(input))
return "cold-summary:" + coldSummaryCacheVersion + ":" + hex.EncodeToString(h[:])
}
@ -185,6 +186,7 @@ func (s *History) update(assistant backend.Message, results []toolResult) {
Content: r.Content,
ContentBlocks: r.ContentBlocks,
ToolCallID: r.ToolCallID,
ToolName: r.Name,
}
if r.ToolCallID != "" {
m.ID = toolMessageID(m)

View File

@ -11,6 +11,14 @@ func messageWithContent(callID, content string) backend.Message {
return backend.Message{Role: "tool", ToolCallID: callID, Content: content}
}
func TestToolSummaryHashSeparatesToolNames(t *testing.T) {
first := toolSummaryHash(backend.Message{Role: "tool", ToolName: "shell", Content: "same result"})
second := toolSummaryHash(backend.Message{Role: "tool", ToolName: "file_read", Content: "same result"})
if first == second {
t.Fatal("different tool names produced the same hash")
}
}
func TestNormalizeSummaryCacheContentPlainText(t *testing.T) {
got := normalizeSummaryCacheContent(" first line \r\n\r\n\nsecond line \t\n\n")
if got != "first line\n\nsecond line" {

View File

@ -39,6 +39,7 @@ type Message struct {
ContentBlocks []ContentBlock `json:"content_blocks,omitempty"` // when set, overrides Content
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // set by assistant when calling tools
ToolCallID string `json:"tool_call_id,omitempty"` // set on role=tool replies (required by OpenAI)
ToolName string `json:"tool_name,omitempty"` // local tool identity for history/cache semantics
SummaryApplied bool `json:"summary_applied,omitempty"` // local history state
}