v0.1.24 ec79818

compact.go RawHistoryFolder

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.mu.Unlock()
117
118	return summary, nil
119}
120
121// repairOrphanedToolResults removes tool_result content blocks (and
122// entire messages that become empty) when the matching tool_use ID
123// does not appear anywhere in the given messages. This happens after
124// compaction when the tail preserves a tool_result but the tool_use
125// that produced it was summarized away.
126func repairOrphanedToolResults(msgs []provider.Message) []provider.Message {
127	// Collect all tool_use IDs present in the messages.
128	useIDs := map[string]bool{}
129	for _, m := range msgs {
130		for _, c := range m.Content {
131			if tc, ok := c.(provider.ToolCallBlock); ok {
132				useIDs[tc.ID] = true
133			}
134		}
135	}
136
137	// Filter out tool_result blocks referencing missing tool_use IDs.
138	out := make([]provider.Message, 0, len(msgs))
139	for _, m := range msgs {
140		var filtered []provider.Content
141		for _, c := range m.Content {
142			if tr, ok := c.(provider.ToolResultBlock); ok {
143				if !useIDs[tr.CallID] {
144					continue // orphaned
145				}
146			}
147			filtered = append(filtered, c)
148		}
149		if len(filtered) > 0 {
150			copy := m
151			copy.Content = filtered
152			out = append(out, copy)
153		}
154		// Drop messages that became empty after filtering.
155	}
156	return out
157}
158
159// serializeTranscript renders a list of provider.Message into a plain
160// text transcript the summarization model can read without trying to
161// continue the conversation.
162func serializeTranscript(msgs []provider.Message) string {
163	var sb strings.Builder
164	for _, m := range msgs {
165		switch m.Role {
166		case provider.RoleUser:
167			sb.WriteString("\n--- user ---\n")
168		case provider.RoleAssistant:
169			sb.WriteString("\n--- assistant ---\n")
170		case provider.RoleTool:
171			sb.WriteString("\n--- tool ---\n")
172		}
173		for _, c := range m.Content {
174			switch v := c.(type) {
175			case provider.TextBlock:
176				sb.WriteString(v.Text)
177				sb.WriteString("\n")
178			case provider.ImageBlock:
179				fmt.Fprintf(&sb, "[image: %s, %d bytes]\n", v.MimeType, len(v.Data))
180			case provider.ToolCallBlock:
181				fmt.Fprintf(&sb, "[tool_call %s %s]\n", v.Name, string(v.Arguments))
182			case provider.ToolResultBlock:
183				for _, inner := range v.Content {
184					if tb, ok := inner.(provider.TextBlock); ok {
185						sb.WriteString("[tool_result] ")
186						sb.WriteString(tb.Text)
187						sb.WriteString("\n")
188					}
189				}
190			}
191		}
192	}
193	return sb.String()
194}
195
196const 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.
197
198Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`
199
200const compactionPrompt = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.
201
202Use this EXACT format:
203
204## Goal
205[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
206
207## Constraints & Preferences
208- [Any constraints, preferences, or requirements mentioned by user]
209- [Or "(none)" if none were mentioned]
210
211## Progress
212### Done
213- [x] [Completed tasks/changes]
214
215### In Progress
216- [ ] [Current work]
217
218### Blocked
219- [Issues preventing progress, if any]
220
221## Key Decisions
222- **[Decision]**: [Brief rationale]
223
224## Next Steps
2251. [Ordered list of what should happen next]
226
227## Critical Context
228- [Any data, examples, or references needed to continue]
229- [Or "(none)" if not applicable]
230
231Keep each section concise. Preserve exact file paths, function names, and error messages.`

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close