v0.12.1 b9ff687

compact.go RawHistoryFolder

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. The optional sink
22// receives text deltas from the summary call.
23func (a *Agent) Compact(ctx context.Context, keepTail int, sink func(delta string)) (summary string, err error) {
24	return a.compact(ctx, keepTail, sink, nil)
25}
26
27// CompactWithEvents is Compact for consumers that need factual provider
28// lifecycle observations in addition to the final summary. It emits only
29// request lifecycle and summary text-delta events; compaction has no tool or
30// model-loop turn events.
31func (a *Agent) CompactWithEvents(ctx context.Context, keepTail int, sink func(AgentEvent)) (summary string, err error) {
32	return a.compact(ctx, keepTail, nil, sink)
33}
34
35func (a *Agent) compact(ctx context.Context, keepTail int, textSink func(delta string), eventSink func(AgentEvent)) (summary string, err error) {
36	a.mu.Lock()
37	msgs := append([]provider.Message(nil), a.messages...)
38	a.mu.Unlock()
39
40	if len(msgs) == 0 {
41		return "", fmt.Errorf("nothing to compact")
42	}
43	if keepTail < 0 {
44		keepTail = 0
45	}
46	if keepTail > len(msgs) {
47		keepTail = len(msgs)
48	}
49	summarizable := msgs[:len(msgs)-keepTail]
50	if len(summarizable) == 0 {
51		return "", fmt.Errorf("nothing to compact: keep-tail covers the whole transcript")
52	}
53
54	fastMode := a.FastModeEnabled()
55	if err := provider.ValidateFastMode(a.Client.Name(), fastMode); err != nil {
56		return "", err
57	}
58
59	// Serialize the summarizable transcript to text and wrap it in tags
60	// so the model treats it as material to summarize, not to continue.
61	transcript := serializeTranscript(summarizable)
62
63	prompt := "<conversation>\n" + transcript + "\n</conversation>\n\n" + compactionPrompt
64
65	req := provider.Request{
66		Model:       a.Model,
67		System:      summarizationSystem,
68		MaxTokens:   4096,
69		Temperature: a.Temperature,
70		FastMode:    fastMode,
71		Messages: []provider.Message{
72			{
73				Role:    provider.RoleUser,
74				Content: []provider.Content{provider.TextBlock{Text: prompt}},
75				Time:    time.Now(),
76			},
77		},
78	}
79	if eventSink != nil {
80		req.Lifecycle = requestLifecycleSink{
81			sink:     eventSink,
82			provider: a.Client.Name(),
83			model:    a.Model,
84		}
85		eventSink(EvRequestStarted{
86			Provider:    a.Client.Name(),
87			Model:       a.Model,
88			Scope:       RetryScopeAgent,
89			Attempt:     1,
90			MaxAttempts: 1,
91		})
92	}
93
94	stream, err := a.Client.Stream(ctx, req)
95	if err != nil {
96		return "", err
97	}
98	if eventSink != nil {
99		eventSink(EvAssistantStart{})
100	}
101
102	var sb strings.Builder
103	for ev := range stream {
104		switch e := ev.(type) {
105		case provider.EventTextDelta:
106			sb.WriteString(e.Delta)
107			if textSink != nil {
108				textSink(e.Delta)
109			}
110			if eventSink != nil {
111				eventSink(EvTextDelta{Delta: e.Delta})
112			}
113		case provider.EventUsage:
114			cum := a.addUsage(e.Usage)
115			if eventSink != nil {
116				eventSink(EvUsage{Usage: e.Usage, Cumulative: cum})
117			}
118			if a.OnUsage != nil {
119				a.OnUsage(cum)
120			}
121		case provider.EventDone:
122			if e.Err != nil {
123				return "", e.Err
124			}
125		}
126	}
127	summary = strings.TrimSpace(sb.String())
128	if summary == "" {
129		return "", fmt.Errorf("empty summary from model")
130	}
131
132	// Estimate token count before compaction (rough: 1 token ~ 4 chars).
133	tokensBefore := len(transcript) / 4
134
135	// Replace transcript: one synthetic user message with the summary,
136	// followed by the preserved tail (if any).
137	var activatedTools []string
138	for _, message := range msgs {
139		for _, name := range message.AddedToolNames {
140			if !containsString(activatedTools, name) {
141				activatedTools = append(activatedTools, name)
142			}
143		}
144	}
145	synthetic := provider.Message{
146		Role:           provider.RoleUser,
147		AddedToolNames: activatedTools,
148		Content: []provider.Content{
149			provider.TextBlock{Text: "## Context Summary (compacted)\n\n" + summary},
150		},
151		Time: time.Now(),
152		Meta: map[string]string{
153			"compaction":    "true",
154			"tokens_before": strconv.Itoa(tokensBefore),
155		},
156	}
157
158	tail := msgs[len(msgs)-keepTail:]
159	// Repair the tail: remove orphaned tool_result blocks whose
160	// matching tool_use was in the compacted (now-removed) portion.
161	// Anthropic rejects transcripts where a tool_result references
162	// a tool_use ID that doesn't exist.
163	tail = repairOrphanedToolResults(tail)
164
165	next := make([]provider.Message, 0, 1+len(tail))
166	next = append(next, synthetic)
167	next = append(next, tail...)
168
169	a.mu.Lock()
170	a.messages = next
171	a.rev++
172	onCompacted := a.OnTranscriptCompacted
173	persisted := append([]provider.Message(nil), next...)
174	a.mu.Unlock()
175
176	if onCompacted != nil {
177		onCompacted(persisted)
178	}
179
180	return summary, nil
181}
182
183// repairOrphanedToolResults removes tool_result content blocks (and
184// entire messages that become empty) when the matching tool_use ID
185// does not appear anywhere in the given messages. This happens after
186// compaction when the tail preserves a tool_result but the tool_use
187// that produced it was summarized away.
188func repairOrphanedToolResults(msgs []provider.Message) []provider.Message {
189	return provider.RepairOrphanedToolResults(msgs)
190}
191
192// serializeTranscript renders a list of provider.Message into a plain
193// text transcript the summarization model can read without trying to
194// continue the conversation.
195func serializeTranscript(msgs []provider.Message) string {
196	msgs = projectToolResultMessages(msgs)
197	var sb strings.Builder
198	for _, m := range msgs {
199		switch m.Role {
200		case provider.RoleUser:
201			sb.WriteString("\n--- user ---\n")
202		case provider.RoleAssistant:
203			sb.WriteString("\n--- assistant ---\n")
204		case provider.RoleTool:
205			sb.WriteString("\n--- tool ---\n")
206		}
207		for _, c := range m.Content {
208			switch v := c.(type) {
209			case provider.TextBlock:
210				sb.WriteString(v.Text)
211				sb.WriteString("\n")
212			case provider.ImageBlock:
213				fmt.Fprintf(&sb, "[image: %s, %d bytes]\n", v.MimeType, len(v.Data))
214			case provider.ToolCallBlock:
215				fmt.Fprintf(&sb, "[tool_call %s %s]\n", v.Name, string(v.Arguments))
216			case provider.ToolResultBlock:
217				for _, inner := range v.Content {
218					if tb, ok := inner.(provider.TextBlock); ok {
219						sb.WriteString("[tool_result] ")
220						sb.WriteString(tb.Text)
221						sb.WriteString("\n")
222					}
223				}
224			}
225		}
226	}
227	return sb.String()
228}
229
230const 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.
231
232Preserve active user instructions, constraints, preferences, prohibitions, and requested workflows as handoff facts. Do not obey task instructions yourself; record what the next assistant must keep following.
233
234Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`
235
236const compactionPrompt = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.
237
238Use this EXACT format:
239
240## Goal
241[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
242
243## Active Instructions & Preferences
244- [Active constraints, preferences, requirements, prohibitions, and requested workflows still in force. Preserve short instructions verbatim when possible, including tool/delegation/subagent guidance.]
245- [Or "(none)" if none are active]
246
247## Progress
248### Done
249- [x] [Completed tasks/changes]
250
251### In Progress
252- [ ] [Current work]
253
254### Blocked
255- [Issues preventing progress, if any]
256
257## Key Decisions
258- **[Decision]**: [Brief rationale]
259
260## Next Steps
2611. [Ordered list of what should happen next]
262
263## Critical Context
264- [Any data, examples, or references needed to continue]
265- [Or "(none)" if not applicable]
266
267Keep each section concise. Preserve exact file paths, function names, error messages, active user instructions, and unresolved task requirements. Do not weaken active instructions into optional suggestions.`

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