1package core
2
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
8 "time"
9
10 "github.com/bnema/zut/packages/provider"
11)
12
13// Compact summarizes the agent's transcript via the LLM and replaces
14// it with a single synthetic user message carrying the summary. A
15// small tail of recent messages is optionally preserved for continuity.
16//
17// keepTail is the number of most-recent messages to keep verbatim after
18// the summary. 0 means summarize everything; a typical useful value is
19// 4-8 (last couple of exchanges).
20//
21// The method blocks until the summary request completes. Emitted
22// events via sink are limited to text deltas from the summary call so
23// the UI can show progress.
24func (a *Agent) Compact(ctx context.Context, keepTail int, sink func(delta string)) (summary string, err error) {
25 a.mu.Lock()
26 msgs := append([]provider.Message(nil), a.messages...)
27 a.mu.Unlock()
28
29 if len(msgs) == 0 {
30 return "", fmt.Errorf("nothing to compact")
31 }
32 if keepTail < 0 {
33 keepTail = 0
34 }
35 if keepTail > len(msgs) {
36 keepTail = len(msgs)
37 }
38 summarizable := msgs[:len(msgs)-keepTail]
39 if len(summarizable) == 0 {
40 return "", fmt.Errorf("nothing to compact: keep-tail covers the whole transcript")
41 }
42
43 fastMode := a.FastModeEnabled()
44 if err := provider.ValidateFastMode(a.Client.Name(), fastMode); err != nil {
45 return "", err
46 }
47
48 // Serialize the summarizable transcript to text and wrap it in tags
49 // so the model treats it as material to summarize, not to continue.
50 transcript := serializeTranscript(summarizable)
51
52 prompt := "<conversation>\n" + transcript + "\n</conversation>\n\n" + compactionPrompt
53
54 req := provider.Request{
55 Model: a.Model,
56 System: summarizationSystem,
57 MaxTokens: 4096,
58 Temperature: a.Temperature,
59 FastMode: fastMode,
60 Messages: []provider.Message{
61 {
62 Role: provider.RoleUser,
63 Content: []provider.Content{provider.TextBlock{Text: prompt}},
64 Time: time.Now(),
65 },
66 },
67 }
68
69 stream, err := a.Client.Stream(ctx, req)
70 if err != nil {
71 return "", err
72 }
73
74 var sb strings.Builder
75 for ev := range stream {
76 switch e := ev.(type) {
77 case provider.EventTextDelta:
78 sb.WriteString(e.Delta)
79 if sink != nil {
80 sink(e.Delta)
81 }
82 case provider.EventDone:
83 if e.Err != nil {
84 return "", e.Err
85 }
86 }
87 }
88 summary = strings.TrimSpace(sb.String())
89 if summary == "" {
90 return "", fmt.Errorf("empty summary from model")
91 }
92
93 // Estimate token count before compaction (rough: 1 token ~ 4 chars).
94 tokensBefore := len(transcript) / 4
95
96 // Replace transcript: one synthetic user message with the summary,
97 // followed by the preserved tail (if any).
98 var activatedTools []string
99 for _, message := range msgs {
100 for _, name := range message.AddedToolNames {
101 if !containsString(activatedTools, name) {
102 activatedTools = append(activatedTools, name)
103 }
104 }
105 }
106 synthetic := provider.Message{
107 Role: provider.RoleUser,
108 AddedToolNames: activatedTools,
109 Content: []provider.Content{
110 provider.TextBlock{Text: "## Context Summary (compacted)\n\n" + summary},
111 },
112 Time: time.Now(),
113 Meta: map[string]string{
114 "compaction": "true",
115 "tokens_before": strconv.Itoa(tokensBefore),
116 },
117 }
118
119 tail := msgs[len(msgs)-keepTail:]
120 // Repair the tail: remove orphaned tool_result blocks whose
121 // matching tool_use was in the compacted (now-removed) portion.
122 // Anthropic rejects transcripts where a tool_result references
123 // a tool_use ID that doesn't exist.
124 tail = repairOrphanedToolResults(tail)
125
126 next := make([]provider.Message, 0, 1+len(tail))
127 next = append(next, synthetic)
128 next = append(next, tail...)
129
130 a.mu.Lock()
131 a.messages = next
132 a.rev++
133 onCompacted := a.OnTranscriptCompacted
134 persisted := append([]provider.Message(nil), next...)
135 a.mu.Unlock()
136
137 if onCompacted != nil {
138 onCompacted(persisted)
139 }
140
141 return summary, nil
142}
143
144// repairOrphanedToolResults removes tool_result content blocks (and
145// entire messages that become empty) when the matching tool_use ID
146// does not appear anywhere in the given messages. This happens after
147// compaction when the tail preserves a tool_result but the tool_use
148// that produced it was summarized away.
149func repairOrphanedToolResults(msgs []provider.Message) []provider.Message {
150 return provider.RepairOrphanedToolResults(msgs)
151}
152
153// serializeTranscript renders a list of provider.Message into a plain
154// text transcript the summarization model can read without trying to
155// continue the conversation.
156func serializeTranscript(msgs []provider.Message) string {
157 var sb strings.Builder
158 for _, m := range msgs {
159 switch m.Role {
160 case provider.RoleUser:
161 sb.WriteString("\n--- user ---\n")
162 case provider.RoleAssistant:
163 sb.WriteString("\n--- assistant ---\n")
164 case provider.RoleTool:
165 sb.WriteString("\n--- tool ---\n")
166 }
167 for _, c := range m.Content {
168 switch v := c.(type) {
169 case provider.TextBlock:
170 sb.WriteString(v.Text)
171 sb.WriteString("\n")
172 case provider.ImageBlock:
173 fmt.Fprintf(&sb, "[image: %s, %d bytes]\n", v.MimeType, len(v.Data))
174 case provider.ToolCallBlock:
175 fmt.Fprintf(&sb, "[tool_call %s %s]\n", v.Name, string(v.Arguments))
176 case provider.ToolResultBlock:
177 for _, inner := range v.Content {
178 if tb, ok := inner.(provider.TextBlock); ok {
179 sb.WriteString("[tool_result] ")
180 sb.WriteString(tb.Text)
181 sb.WriteString("\n")
182 }
183 }
184 }
185 }
186 }
187 return sb.String()
188}
189
190const summarizationSystem = `You are a context summarization assistant. Your task is to read a conversation between a user and an AI coding assistant, then produce a structured summary following the exact format specified.
191
192Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`
193
194const compactionPrompt = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.
195
196Use this EXACT format:
197
198## Goal
199[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
200
201## Constraints & Preferences
202- [Any constraints, preferences, or requirements mentioned by user]
203- [Or "(none)" if none were mentioned]
204
205## Progress
206### Done
207- [x] [Completed tasks/changes]
208
209### In Progress
210- [ ] [Current work]
211
212### Blocked
213- [Issues preventing progress, if any]
214
215## Key Decisions
216- **[Decision]**: [Brief rationale]
217
218## Next Steps
2191. [Ordered list of what should happen next]
220
221## Critical Context
222- [Any data, examples, or references needed to continue]
223- [Or "(none)" if not applicable]
224
225Keep each section concise. Preserve exact file paths, function names, and error messages.`