1package core
2
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
8 "time"
9
10 "github.com/patriceckhart/zot/internal/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 // Serialize the summarizable transcript to text and wrap it in tags
44 // so the model treats it as material to summarize, not to continue.
45 transcript := serializeTranscript(summarizable)
46
47 prompt := "<conversation>\n" + transcript + "\n</conversation>\n\n" + compactionPrompt
48
49 req := provider.Request{
50 Model: a.Model,
51 System: summarizationSystem,
52 MaxTokens: 4096,
53 Messages: []provider.Message{
54 {
55 Role: provider.RoleUser,
56 Content: []provider.Content{provider.TextBlock{Text: prompt}},
57 Time: time.Now(),
58 },
59 },
60 }
61
62 stream, err := a.Client.Stream(ctx, req)
63 if err != nil {
64 return "", err
65 }
66
67 var sb strings.Builder
68 for ev := range stream {
69 switch e := ev.(type) {
70 case provider.EventTextDelta:
71 sb.WriteString(e.Delta)
72 if sink != nil {
73 sink(e.Delta)
74 }
75 case provider.EventDone:
76 if e.Err != nil {
77 return "", e.Err
78 }
79 }
80 }
81 summary = strings.TrimSpace(sb.String())
82 if summary == "" {
83 return "", fmt.Errorf("empty summary from model")
84 }
85
86 // Estimate token count before compaction (rough: 1 token ~ 4 chars).
87 tokensBefore := len(transcript) / 4
88
89 // Replace transcript: one synthetic user message with the summary,
90 // followed by the preserved tail (if any).
91 synthetic := provider.Message{
92 Role: provider.RoleUser,
93 Content: []provider.Content{
94 provider.TextBlock{Text: "## Context Summary (compacted)\n\n" + summary},
95 },
96 Time: time.Now(),
97 Meta: map[string]string{
98 "compaction": "true",
99 "tokens_before": strconv.Itoa(tokensBefore),
100 },
101 }
102
103 tail := msgs[len(msgs)-keepTail:]
104 // Repair the tail: remove orphaned tool_result blocks whose
105 // matching tool_use was in the compacted (now-removed) portion.
106 // Anthropic rejects transcripts where a tool_result references
107 // a tool_use ID that doesn't exist.
108 tail = repairOrphanedToolResults(tail)
109
110 next := make([]provider.Message, 0, 1+len(tail))
111 next = append(next, synthetic)
112 next = append(next, tail...)
113
114 a.mu.Lock()
115 a.messages = next
116 a.rev++
117 onCompacted := a.OnTranscriptCompacted
118 persisted := append([]provider.Message(nil), next...)
119 a.mu.Unlock()
120
121 if onCompacted != nil {
122 onCompacted(persisted)
123 }
124
125 return summary, nil
126}
127
128// repairOrphanedToolResults removes tool_result content blocks (and
129// entire messages that become empty) when the matching tool_use ID
130// does not appear anywhere in the given messages. This happens after
131// compaction when the tail preserves a tool_result but the tool_use
132// that produced it was summarized away.
133func repairOrphanedToolResults(msgs []provider.Message) []provider.Message {
134 return provider.RepairOrphanedToolResults(msgs)
135}
136
137// serializeTranscript renders a list of provider.Message into a plain
138// text transcript the summarization model can read without trying to
139// continue the conversation.
140func serializeTranscript(msgs []provider.Message) string {
141 var sb strings.Builder
142 for _, m := range msgs {
143 switch m.Role {
144 case provider.RoleUser:
145 sb.WriteString("\n--- user ---\n")
146 case provider.RoleAssistant:
147 sb.WriteString("\n--- assistant ---\n")
148 case provider.RoleTool:
149 sb.WriteString("\n--- tool ---\n")
150 }
151 for _, c := range m.Content {
152 switch v := c.(type) {
153 case provider.TextBlock:
154 sb.WriteString(v.Text)
155 sb.WriteString("\n")
156 case provider.ImageBlock:
157 fmt.Fprintf(&sb, "[image: %s, %d bytes]\n", v.MimeType, len(v.Data))
158 case provider.ToolCallBlock:
159 fmt.Fprintf(&sb, "[tool_call %s %s]\n", v.Name, string(v.Arguments))
160 case provider.ToolResultBlock:
161 for _, inner := range v.Content {
162 if tb, ok := inner.(provider.TextBlock); ok {
163 sb.WriteString("[tool_result] ")
164 sb.WriteString(tb.Text)
165 sb.WriteString("\n")
166 }
167 }
168 }
169 }
170 }
171 return sb.String()
172}
173
174const 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.
175
176Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`
177
178const compactionPrompt = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.
179
180Use this EXACT format:
181
182## Goal
183[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
184
185## Constraints & Preferences
186- [Any constraints, preferences, or requirements mentioned by user]
187- [Or "(none)" if none were mentioned]
188
189## Progress
190### Done
191- [x] [Completed tasks/changes]
192
193### In Progress
194- [ ] [Current work]
195
196### Blocked
197- [Issues preventing progress, if any]
198
199## Key Decisions
200- **[Decision]**: [Brief rationale]
201
202## Next Steps
2031. [Ordered list of what should happen next]
204
205## Critical Context
206- [Any data, examples, or references needed to continue]
207- [Or "(none)" if not applicable]
208
209Keep each section concise. Preserve exact file paths, function names, and error messages.`