返回 DeepSeek-Reasonix
compact.go
根目录 / internal / agent / compact.go
1 package agent
2
3 import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "os"
9 "path/filepath"
10 "sort"
11 "strings"
12 "time"
13 "unicode/utf8"
14
15 "reasonix/internal/ablation"
16 "reasonix/internal/event"
17 "reasonix/internal/provider"
18 )
19
20 // Compaction is a low-frequency cache-reset point: the prompt grows append-only
21 // (high cache hits) until a turn nears compactRatio of the window, then it is
22 // compacted down to a tail budget. The budget is a fixed token count, not a
23 // fraction of the window, so a huge window still compacts rarely while a small
24 // one still lands below the trigger (which is what stops the re-compaction loop).
25 const (
26 defaultSoftCompactRatio = 0.5 // report growing context here, but keep the cache-stable prefix intact
27 defaultToolResultSnipRatio = 0.6 // rewrite stale tool results cheaply before summary compaction
28 defaultCompactRatio = 0.8 // trigger: prompt at this fraction of the window compacts
29 defaultCompactForceRatio = 0.9 // force compaction at this high-water mark even for low-value folds
30 defaultCompactTarget = 0.5 // safety cap: the kept tail never exceeds this fraction of the window
31 defaultTailTokens = 16384 // verbatim recent-tail budget, in tokens
32 minRecentKeep = 2 // never keep fewer recent messages than this
33 minCompactMessages = 2 // skip compaction below this many compactable messages
34 fallbackTokPerChar = 0.25 // ~4 chars/token, used before any usage is available to calibrate
35 maxPinnedFirstUserTokens = 1500 // ceiling on pinning the first user turn verbatim; larger first turns (pasted content) stay foldable
36 pinnedFirstUserWindowFrac = 0.15 // and never pin a first turn worth more than this fraction of the window
37 )
38
39 // summaryTag wraps the compaction summary so the model can distinguish it from
40 // live user input and later strip or skip it when reasoning about the current turn.
41 const (
42 summaryTagOpen = "<compaction-summary>"
43 summaryTagClose = "</compaction-summary>"
44 )
45
46 // summaryTimeout bounds one summarizer call so a stalled stream surfaces a clear
47 // failure (then a mechanical fold) instead of hanging compaction indefinitely.
48 const summaryTimeout = 90 * time.Second
49
50 // summarySystemPrompt steers the executor to distill older history into a
51 // structured briefing it can keep relying on after the originals are dropped.
52 // The section layout mirrors what a coding agent actually needs to resume work
53 // mid-task: the goal verbatim, the concrete state of the code, and an explicit
54 // next step — so the post-compaction turn doesn't lose the thread or re-derive
55 // decisions already made.
56 const summarySystemPrompt = `You are compacting the earlier part of a coding agent's conversation to save context.
57 The agent keeps your summary alongside the user's own turns (kept verbatim) and the recent tail; your job is to fold the assistant/tool work into a briefing it can resume from.
58 Write under these exact headings, omitting a heading only if it has no content:
59
60 ## Standing facts & constraints
61 Everything the user stated that still governs the work — names, paths, IDs, versions, tokens, preferences, and hard "never do X" rules — in their own words. Be exhaustive; this is the durable contract, so prefer over- to under-including.
62
63 ## Goal
64 The user's request and intent.
65
66 ## Decisions & rationale
67 Key choices made so far and why — so they are not re-litigated or reversed.
68
69 ## Files & code
70 Files read or modified, with the specific facts that matter: signatures, line locations, data shapes, and exact edits applied. Be concrete; this is what lets the agent act without re-reading everything.
71
72 ## Commands & outcomes
73 Commands run (builds, tests, git) and their relevant results — what passed, what failed, and the error text that matters.
74
75 ## Errors & fixes
76 Problems hit and how they were resolved (or not), so the same dead ends are not repeated.
77
78 ## Pending & next step
79 What is still in progress or unstarted, and the single most concrete next action to take.
80
81 Rules: be terse — bullet points and fragments, not prose. Preserve identifiers, paths, and numbers exactly. Do NOT invent anything not present in the messages; if something is unknown, leave it out rather than guessing.`
82
83 // compactThresholds returns the prompt-token boundaries maybeCompact switches
84 // on. The compaction ablation arm collapses the snip and fold triggers onto
85 // soft, so the cache-preserving deferral branch is unreachable and the session
86 // folds as soon as it grows — what a harness with no prompt-cache strategy does.
87 func (a *Agent) compactThresholds() (soft, snip, high int) {
88 high = int(float64(a.contextWindow) * a.compactRatio)
89 snip = int(float64(a.contextWindow) * a.toolResultSnipRatio)
90 soft = int(float64(a.contextWindow) * a.softCompactRatio)
91 if a.ablation.Off(ablation.Compaction) {
92 high, snip = soft, soft
93 }
94 return soft, snip, high
95 }
96
97 // maybeCompact compacts the session when the last turn's prompt has grown to the
98 // configured fraction of the context window. It is a no-op when compaction is
99 // disabled (no window) or usage is unavailable.
100 func (a *Agent) maybeCompact(ctx context.Context, u *provider.Usage) {
101 if a.contextWindow <= 0 || u == nil || u.PromptTokens == 0 {
102 return
103 }
104 soft, snip, high := a.compactThresholds()
105 // A turn that sits under the trigger is the breathing room a healthy
106 // compaction buys; it clears the stuck latch and the run counter. This has to
107 // happen before the soft/snip branches return, because a compaction that
108 // settles the prompt anywhere in [snip, high) is working exactly as intended:
109 // leaving a stale run count behind there would latch the *next* compaction as
110 // "the window is too small" and silently disable auto-compaction for the rest
111 // of the session.
112 if u.PromptTokens < high {
113 a.consecutiveCompacts = 0
114 a.compactStuck = false
115 }
116 // Between the soft ratio and the trigger, report growing context once without
117 // rewriting the prefix — a compaction here would needlessly crater the cache.
118 if u.PromptTokens >= soft && u.PromptTokens < snip && !a.softCompactNoticed {
119 a.softCompactNoticed = true
120 detail := fmt.Sprintf("context reached %.0f%% of window; keeping cache-first prefix until compact threshold %.0f%%", a.softCompactRatio*100, a.compactRatio*100)
121 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: "Context is getting large; preserving cache until cleanup is needed.", Detail: detail})
122 return
123 }
124 if u.PromptTokens >= snip && u.PromptTokens < high {
125 ratio := a.tokPerChar()
126 if st, err := a.SnipStaleToolResults(); err == nil && st.Results > 0 {
127 saved := int(float64(st.SavedChars) * ratio)
128 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: fmt.Sprintf(
129 "snipped %d stale tool results (~%d tokens est.) before compaction", st.Results, saved)})
130 }
131 return
132 }
133 if u.PromptTokens < high {
134 return // the latch was already cleared above
135 }
136 if a.compactStuck {
137 return
138 }
139 force := u.PromptTokens >= int(float64(a.contextWindow)*a.compactForceRatio)
140 // Prune before folding: when eliding stale tool results alone clears the
141 // trigger, this turn's (paid) summarize call is skipped entirely.
142 ratio := a.tokPerChar()
143 if st, err := a.PruneStaleToolResults(); err == nil && st.Results > 0 {
144 saved := int(float64(st.SavedChars) * ratio)
145 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: fmt.Sprintf(
146 "pruned %d stale tool results (~%d tokens est.) before compaction", st.Results, saved)})
147 if !force && u.PromptTokens-saved < high {
148 return
149 }
150 }
151 if err := a.compact(ctx, "auto", "", force); err != nil {
152 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: "Context cleanup skipped for now.", Detail: fmt.Sprintf("compaction skipped: %v", err)})
153 return
154 }
155 // A healthy compaction drops the prompt under the trigger, so the next turn
156 // won't compact. Compacting on consecutive turns means the kept tail alone
157 // exceeds the trigger — the system prompt plus one verbatim turn is bigger than
158 // the window allows. Re-firing every turn is the loop users hit, so pause
159 // auto-compaction and say why, once.
160 a.consecutiveCompacts++
161 if a.consecutiveCompacts >= 2 {
162 a.compactStuck = true
163 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: "Automatic context cleanup paused because the context window is too small.", Detail: fmt.Sprintf(
164 "context_window=%d is too small for compaction to help (the system prompt plus one turn already exceeds %.0f%% of it); raise context_window or shrink tool output. Auto-compaction paused until the prompt drops.",
165 a.contextWindow, a.compactRatio*100)})
166 }
167 }
168
169 // foldEconomics estimates whether compacting the given region saves enough
170 // tokens to justify the summarization API call. It returns false when the
171 // region is too small for the savings to outweigh the extra round-trip cost
172 // and latency of calling the summarizer.
173 func foldEconomics(region []provider.Message) bool {
174 const minFoldTokens = 400
175 return estimateMessagesTokens(region) >= minFoldTokens
176 }
177
178 func estimateMessagesTokens(msgs []provider.Message) int {
179 total := 0
180 for _, m := range msgs {
181 if m.LocalOnly {
182 continue
183 }
184 total += 4 // chat-message framing overhead
185 total += estimateTextTokens(m.Content)
186 total += estimateTextTokens(m.ReasoningContent)
187 total += estimateTextTokens(m.Name)
188 total += estimateTextTokens(m.ToolCallID)
189 for _, tc := range m.ToolCalls {
190 total += 8
191 total += estimateTextTokens(tc.ID)
192 total += estimateTextTokens(tc.Name)
193 total += estimateTextTokens(tc.Arguments)
194 }
195 for _, item := range m.ResponsesItems {
196 total += estimateTextTokens(string(item))
197 }
198 }
199 return total
200 }
201
202 func estimateTextTokens(s string) int {
203 if s == "" {
204 return 0
205 }
206 // A conservative cross-language approximation: English-ish text trends near
207 // four bytes per token, while CJK-heavy text is closer to one rune per token.
208 bytes := len(s)
209 runes := utf8.RuneCountInString(s)
210 byBytes := (bytes + 3) / 4
211 if runes > byBytes {
212 return runes
213 }
214 return byBytes
215 }
216
217 // compact summarizes the older middle of the session and replaces it in place:
218 // the session becomes system + summary + recent tail. The dropped originals are
219 // archived first, so the full history stays traceable. trigger is "auto" (the
220 // window threshold) or "manual" (/compact); it rides the Compaction events so a
221 // frontend can label the card. instructions is optional extra summary guidance
222 // (the user's `/compact <focus>` text); a PreCompact hook can contribute more.
223 // force bypasses the fold-economics skip (manual /compact and the force-ratio
224 // high-water mark always compact). A Started event is emitted before the (network)
225 // summarize so the UI can show a "compacting…" placeholder, and a Done event
226 // (carrying the summary) replaces it.
227 func (a *Agent) compact(ctx context.Context, trigger, instructions string, force bool) error {
228 msgs := a.session.Messages
229 head, start, ok := a.planCompaction(msgs, minCompactMessages)
230 if !ok {
231 // A single huge message can still be worth folding. Keep the normal
232 // message-count guard for small histories, but let content size decide
233 // whether a one-message region has real compaction value.
234 head, start, ok = a.planCompaction(msgs, 1)
235 }
236 if !ok {
237 return nil // recent tail already covers everything worth keeping
238 }
239 // A controller in-flight marker records the pre-turn message count, but a
240 // compaction rewrites message indexes. Keep the entire active turn outside
241 // the fold so completed tool call/result pairs remain available for a later
242 // cancellation or crash recovery instead of surviving only as prose in a
243 // summary.
244 if active := a.activeTurnStart(msgs); active >= head && active < start {
245 start = active
246 if start <= head {
247 return nil
248 }
249 }
250 region := msgs[head:start]
251
252 // Base layer: every small user turn in the region is kept verbatim (the
253 // deterministic floor — a fact the user stated is never summarized away,
254 // wherever in the session they said it); only the rest folds into the digest.
255 kept, fold := a.partitionFold(region)
256 if len(fold) == 0 {
257 return nil // nothing but kept user turns — a fold would save nothing
258 }
259
260 // Economic check on the foldable part (kept user turns don't count toward the
261 // savings): skip if too small to justify the call, unless force demands it.
262 if !force && !foldEconomics(fold) {
263 return nil
264 }
265
266 a.sink.Emit(event.Event{Kind: event.CompactionStarted, Compaction: event.Compaction{Trigger: trigger}})
267
268 // A PreCompact hook can steer what the summary keeps; its stdout joins any
269 // explicit /compact <focus> text.
270 if a.hooks != nil {
271 if hookInstr := a.hooks.PreCompact(ctx, trigger); hookInstr != "" {
272 if instructions != "" {
273 instructions += "\n"
274 }
275 instructions += hookInstr
276 }
277 }
278
279 // compaction.prepare: extensions rule on the fold and the accumulated
280 // guidance (hook + /compact focus) for THIS pass only. A block skips the
281 // pass; the caller surfaces the reason through its usual notice path.
282 var err error
283 fold, instructions, err = a.interceptCompactionPrepare(ctx, fold, instructions)
284 if err != nil {
285 a.emitCompactionAborted(trigger)
286 return err
287 }
288 if len(fold) == 0 {
289 a.emitCompactionAborted(trigger)
290 return nil // the extension replaced the fold with nothing to fold
291 }
292
293 archived := ""
294 if a.archiveDir != "" {
295 path, err := archiveMessages(a.archiveDir, fold)
296 if err != nil {
297 a.emitCompactionAborted(trigger)
298 return fmt.Errorf("archive: %w", err)
299 }
300 archived = path
301 }
302
303 // The digest covers only the foldable work; kept user turns and prior digests
304 // are spliced back verbatim, so a fact that reached a digest once is never
305 // re-summarized away and the user's own words are never touched. Digests
306 // accumulate (small) rather than collapsing into one lossy rolling summary.
307 summary, err := a.summarizeWithRetry(ctx, fold, instructions)
308 if err != nil {
309 // Mechanical fold: the foldable region is already archived, so stand in a
310 // deterministic marker rather than aborting. /compact then always frees
311 // context (and auto-compaction can't loop on a still-full window); the
312 // verbatim user turns kept above are untouched.
313 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo, Text: "Context was compacted without a generated summary.", Detail: "compaction summary unavailable (" + err.Error() + "); folded mechanically"})
314 summary = mechanicalFoldDigest(len(fold), archived)
315 }
316
317 // compaction.complete: extensions rule on the produced summary before it
318 // is written into the session; a replacement is persisted as the summary.
319 summary, err = a.interceptCompactionComplete(ctx, summary)
320 if err != nil {
321 a.emitCompactionAborted(trigger)
322 return err
323 }
324
325 compacted := make([]provider.Message, 0, head+len(kept)+1+len(msgs)-start)
326 compacted = append(compacted, msgs[:head]...)
327 compacted = append(compacted, kept...)
328 compacted = append(compacted, provider.Message{
329 Role: provider.RoleUser,
330 Content: summaryTagOpen + "\n" +
331 "Summary of earlier conversation (older messages were compacted to save context):\n" +
332 summary + "\n" +
333 summaryTagClose,
334 })
335 compacted = append(compacted, msgs[start:]...)
336 a.session.Rewrite(compacted, "compact_"+trigger)
337
338 a.sink.Emit(event.Event{Kind: event.CompactionDone, Compaction: event.Compaction{
339 Trigger: trigger, Messages: len(fold), Summary: summary, Archive: archived,
340 }})
341 return nil
342 }
343
344 // emitCompactionAborted resolves a "compacting…" placeholder when a pass fails
345 // after the Started event: a Done with no summary tells a frontend to drop the
346 // placeholder. The caller still surfaces the reason (a Notice), so this carries
347 // no text of its own.
348 func (a *Agent) emitCompactionAborted(trigger string) {
349 a.sink.Emit(event.Event{Kind: event.CompactionDone, Compaction: event.Compaction{Trigger: trigger}})
350 }
351
352 // SummarizeFrom replaces the messages from fromIdx onward with a single summary,
353 // keeping everything before it verbatim ("summarize from here"). fromIdx is a turn
354 // boundary (a user message), so the split never severs a tool_call/result pair —
355 // those live within one turn. A no-op when the region is empty.
356 func (a *Agent) SummarizeFrom(ctx context.Context, fromIdx int) error {
357 msgs := a.session.Messages
358 if fromIdx < 0 || fromIdx >= len(msgs) {
359 return nil
360 }
361 region, localOnly := splitLocalOnlyMessages(msgs[fromIdx:])
362 if len(region) == 0 {
363 return nil
364 }
365 if a.archiveDir != "" {
366 _, _ = archiveMessages(a.archiveDir, region) // best-effort traceability
367 }
368 summary, err := a.summarize(ctx, region, "")
369 if err != nil {
370 return err
371 }
372 next := make([]provider.Message, 0, fromIdx+1+len(localOnly))
373 next = append(next, msgs[:fromIdx]...)
374 next = append(next, provider.Message{
375 Role: provider.RoleUser,
376 Content: "Summary of the later conversation (compacted from here on):\n" + summary,
377 })
378 next = append(next, localOnly...)
379 a.session.Rewrite(next, "summarize_from")
380 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo,
381 Text: fmt.Sprintf("summarized %d later messages → summary", len(region))})
382 return nil
383 }
384
385 // SummarizeUpTo replaces the messages before toIdx (after the system prompt) with
386 // a single summary, keeping toIdx onward verbatim ("summarize up to here"). toIdx
387 // is a turn boundary, so no tool pair is split. A no-op when the region is empty.
388 func (a *Agent) SummarizeUpTo(ctx context.Context, toIdx int) error {
389 msgs := a.session.Messages
390 head := 0
391 if len(msgs) > 0 && msgs[0].Role == provider.RoleSystem {
392 head = 1
393 }
394 if toIdx <= head || toIdx > len(msgs) {
395 return nil
396 }
397 region, localOnly := splitLocalOnlyMessages(msgs[head:toIdx])
398 if len(region) == 0 {
399 return nil
400 }
401 if a.archiveDir != "" {
402 _, _ = archiveMessages(a.archiveDir, region)
403 }
404 summary, err := a.summarize(ctx, region, "")
405 if err != nil {
406 return err
407 }
408 next := make([]provider.Message, 0, head+1+len(localOnly)+len(msgs)-toIdx)
409 next = append(next, msgs[:head]...)
410 next = append(next, provider.Message{
411 Role: provider.RoleUser,
412 Content: "Summary of earlier conversation (compacted up to here):\n" + summary,
413 })
414 next = append(next, localOnly...)
415 next = append(next, msgs[toIdx:]...)
416 a.session.Rewrite(next, "summarize_up_to")
417 a.sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelInfo,
418 Text: fmt.Sprintf("summarized %d earlier messages → summary", len(region))})
419 return nil
420 }
421
422 // IsCompactionSummary reports whether m is a rolling digest inserted by a
423 // prior compaction fold. Exported for session owners outside this package
424 // (e.g. the guardian) whose turn rollback must not treat a digest as a
425 // disposable user message.
426 func IsCompactionSummary(m provider.Message) bool { return isCompactionSummary(m) }
427
428 func (a *Agent) activeTurnStart(msgs []provider.Message) int {
429 createdAt := a.activeTurnCreatedAt.Load()
430 if createdAt == 0 {
431 return -1
432 }
433 for i, m := range msgs {
434 if m.Role == provider.RoleUser && m.CreatedAt == createdAt {
435 return i
436 }
437 }
438 return -1
439 }
440
441 // splitLocalOnlyMessages removes display-only interrupted output from the
442 // summarizer/archive input while returning it in transcript order for durable
443 // reattachment. Explicit range summaries are user-requested rewrites, but they
444 // must not erase visible output or expose private partial reasoning to a model.
445 func splitLocalOnlyMessages(msgs []provider.Message) (model, localOnly []provider.Message) {
446 for _, m := range msgs {
447 if m.LocalOnly {
448 localOnly = append(localOnly, m)
449 continue
450 }
451 model = append(model, m)
452 }
453 return model, localOnly
454 }
455
456 // isCompactionSummary reports whether m is a rolling summary from a prior fold.
457 func isCompactionSummary(m provider.Message) bool {
458 return m.Role == provider.RoleUser &&
459 strings.HasPrefix(strings.TrimLeft(m.Content, "\n "), summaryTagOpen)
460 }
461
462 // pinnedPrefixLen counts the leading messages a fold keeps verbatim: the system
463 // prompt, the first user turn (its task + stated facts/constraints) when it is
464 // small enough to be a brief, and any prior summaries — so a fold never
465 // summarizes the user's facts away, and a later fold never re-summarizes an
466 // earlier summary into nothing (the drift that silently dropped user-stated facts
467 // after the second compaction). A large first turn (pasted content) stays
468 // foldable so pinning never starves the window.
469 func (a *Agent) pinnedPrefixLen(msgs []provider.Message) int {
470 i := 0
471 if i < len(msgs) && msgs[i].Role == provider.RoleSystem {
472 i++
473 }
474 if i < len(msgs) && msgs[i].Role == provider.RoleUser && !isCompactionSummary(msgs[i]) && a.pinnableUserTurn(msgs[i]) {
475 i++
476 }
477 for i < len(msgs) && isCompactionSummary(msgs[i]) {
478 i++
479 }
480 return i
481 }
482
483 // pinnableUserTurn reports whether a user turn is small enough to keep verbatim. A
484 // turn larger than a brief (pasted content) folds like any other message so the
485 // kept-verbatim floor never starves the window.
486 func (a *Agent) pinnableUserTurn(m provider.Message) bool {
487 budget := maxPinnedFirstUserTokens
488 if a.contextWindow > 0 {
489 if f := int(float64(a.contextWindow) * pinnedFirstUserWindowFrac); f < budget {
490 budget = f
491 }
492 }
493 return int(float64(msgChars(m))*a.tokPerChar()) <= budget
494 }
495
496 // partitionFold splits a compaction region into what is kept verbatim — small user
497 // turns (a fact the user stated is never summarized away) and prior digests (so a
498 // later fold never re-summarizes an earlier digest and drops the facts it already
499 // captured) — and the rest, which folds. Order within each group is preserved.
500 func (a *Agent) partitionFold(region []provider.Message) (kept, fold []provider.Message) {
501 policyKeep := keepIndexes(region, a.keepPolicy)
502 for i, m := range region {
503 if m.LocalOnly || policyKeep[i] || isCompactionSummary(m) || (m.Role == provider.RoleUser && a.pinnableUserTurn(m)) {
504 kept = append(kept, m)
505 } else {
506 fold = append(fold, m)
507 }
508 }
509 return kept, fold
510 }
511
512 func keepIndexes(region []provider.Message, policy KeepPolicy) []bool {
513 keep := make([]bool, len(region))
514 policyStart := 0
515 for i, m := range region {
516 if isCompactionSummary(m) {
517 policyStart = i + 1
518 }
519 }
520 // Retention applies only to messages since the latest digest; older kept
521 // messages are allowed to fold on the next pass so they cannot grow forever.
522 for i, m := range region {
523 if i >= policyStart && shouldKeepMessage(m, policy) {
524 keep[i] = true
525 }
526 }
527 for i, m := range region {
528 if !keep[i] {
529 continue
530 }
531 switch m.Role {
532 case provider.RoleTool:
533 if j := findToolCaller(region, i, m.ToolCallID); j >= 0 {
534 keepToolCallGroup(region, keep, j)
535 }
536 case provider.RoleAssistant:
537 keepToolCallGroup(region, keep, i)
538 }
539 }
540 return keep
541 }
542
543 func keepToolCallGroup(region []provider.Message, keep []bool, assistantIndex int) {
544 if assistantIndex < 0 || assistantIndex >= len(region) {
545 return
546 }
547 m := region[assistantIndex]
548 if m.Role != provider.RoleAssistant || len(m.ToolCalls) == 0 {
549 return
550 }
551 keep[assistantIndex] = true
552 ids := toolCallIDs(m)
553 for j := assistantIndex + 1; j < len(region) && region[j].Role == provider.RoleTool; j++ {
554 if ids[region[j].ToolCallID] {
555 keep[j] = true
556 }
557 }
558 }
559
560 func shouldKeepMessage(m provider.Message, policy KeepPolicy) bool {
561 if policy&KeepErrors != 0 && isErrorMessage(m) {
562 return true
563 }
564 if policy&KeepUserMarked != 0 && isUserMarked(m) {
565 return true
566 }
567 return false
568 }
569
570 func isErrorMessage(m provider.Message) bool {
571 if m.Role != provider.RoleTool {
572 return false
573 }
574 s := strings.TrimSpace(strings.ToLower(m.Content))
575 return strings.HasPrefix(s, "error:") || strings.HasPrefix(s, "blocked:")
576 }
577
578 func isUserMarked(m provider.Message) bool {
579 if m.Role != provider.RoleUser {
580 return false
581 }
582 content := strings.TrimSpace(strings.ToLower(m.Content))
583 return strings.HasPrefix(content, "[[keep]]") ||
584 strings.HasPrefix(content, "[keep]") ||
585 strings.HasPrefix(content, "<keep>") ||
586 strings.HasPrefix(content, "<!-- keep -->")
587 }
588
589 func findToolCaller(region []provider.Message, toolIndex int, id string) int {
590 for i := toolIndex - 1; i >= 0; i-- {
591 if region[i].Role != provider.RoleAssistant {
592 continue
593 }
594 for _, tc := range region[i].ToolCalls {
595 if tc.ID == id {
596 return i
597 }
598 }
599 }
600 return -1
601 }
602
603 func toolCallIDs(m provider.Message) map[string]bool {
604 ids := make(map[string]bool, len(m.ToolCalls))
605 for _, tc := range m.ToolCalls {
606 ids[tc.ID] = true
607 }
608 return ids
609 }
610
611 // planCompaction locates the region to summarize. head is the count of leading
612 // messages preserved verbatim (see pinnedPrefixLen); start is where the preserved
613 // recent tail begins, so msgs[head:start] is compacted. The tail is bounded by a
614 // token budget (not a message count), so a few large tool outputs can't keep it
615 // above the trigger and re-fire compaction every turn. ok is false when there is
616 // too little to compact.
617 func (a *Agent) planCompaction(msgs []provider.Message, min int) (head, start int, ok bool) {
618 head = a.pinnedPrefixLen(msgs)
619 if a.contextWindow > 0 {
620 budget := defaultTailTokens
621 if maxByWin := int(float64(a.contextWindow) * defaultCompactTarget); maxByWin < budget {
622 budget = maxByWin
623 }
624 start = tailStart(msgs, head, budget, a.tokPerChar(), a.tailFloor())
625 } else {
626 // No window to budget against (manual /compact on an unconfigured
627 // provider): keep a fixed count of recent messages, aligned off any tool.
628 start = len(msgs) - a.tailFloor()
629 for start > head && msgs[start].Role == provider.RoleTool {
630 start--
631 }
632 }
633 if start < head {
634 start = head
635 }
636 if start-head < min {
637 return head, start, false
638 }
639 return head, start, true
640 }
641
642 func (a *Agent) tailFloor() int {
643 if a.recentKeep > minRecentKeep {
644 return a.recentKeep
645 }
646 return minRecentKeep
647 }
648
649 // tailStart walks newest→oldest, growing the verbatim tail until the next
650 // message would push its token estimate past budgetTokens (but never below
651 // minKeep messages), then aligns the boundary back off any tool result so the
652 // tail never begins with an orphan whose assistant tool_calls were summarized
653 // away.
654 func tailStart(msgs []provider.Message, head, budgetTokens int, tokPerChar float64, minKeep int) int {
655 start := len(msgs)
656 acc := 0
657 for i := len(msgs) - 1; i > head; i-- {
658 c := int(float64(msgChars(msgs[i])) * tokPerChar)
659 if len(msgs)-i > minKeep && acc+c > budgetTokens {
660 break
661 }
662 acc += c
663 start = i
664 }
665 // start == len(msgs) when nothing fit the tail (a session too small to have a
666 // message after head); there is no msgs[start] to align off, and the caller's
667 // minCompactMessages check then no-ops the pass.
668 for start > head && start < len(msgs) && msgs[start].Role == provider.RoleTool {
669 start--
670 }
671 return start
672 }
673
674 // tokPerChar derives a tokens-per-character ratio from the last turn's real
675 // usage so per-message estimates track the provider's tokenizer without a local
676 // one. Reasoning content is excluded from the char count to match the prompt
677 // actually sent (the provider strips it). Falls back to ~4 chars/token before
678 // any usage is known, and ignores absurd ratios.
679 func (a *Agent) tokPerChar() float64 {
680 if u := a.lastUsage.Load(); u != nil && u.PromptTokens > 0 {
681 if c := charsOfMessages(a.session.Messages); c > 0 {
682 if r := float64(u.PromptTokens) / float64(c); r > 0.05 && r < 2 {
683 return r
684 }
685 }
686 }
687 return fallbackTokPerChar
688 }
689
690 // msgChars counts the characters that ride to the provider for one message —
691 // content plus tool-call names and arguments, but not reasoning (stripped on
692 // send).
693 func msgChars(m provider.Message) int {
694 if m.LocalOnly {
695 return 0
696 }
697 n := len(m.Content)
698 for _, tc := range m.ToolCalls {
699 n += len(tc.Name) + len(tc.Arguments)
700 }
701 return n
702 }
703
704 func charsOfMessages(msgs []provider.Message) int {
705 n := 0
706 for _, m := range msgs {
707 n += msgChars(m)
708 }
709 return n
710 }
711
712 // summarize asks the executor's own provider (no tools) to distill the region
713 // into a briefing, returning the collected text. instructions, when non-empty,
714 // is appended to the system prompt as extra focus guidance (from /compact <focus>
715 // and/or a PreCompact hook).
716 func (a *Agent) summarize(ctx context.Context, region []provider.Message, instructions string) (string, error) {
717 ctx, cancel := context.WithTimeout(ctx, summaryTimeout)
718 defer cancel()
719 ctx = provider.WithRequestAttemptCounter(ctx)
720 sys := summarySystemPrompt
721 if strings.TrimSpace(instructions) != "" {
722 sys += "\n\nAdditional focus for this compaction (prioritize keeping this):\n" + strings.TrimSpace(instructions)
723 }
724 var usage *provider.Usage
725 defer func() {
726 usage = provider.UsageWithRequestAttemptCount(ctx, usage)
727 if usage != nil && (usage.TotalTokens > 0 || usage.RequestCount > 0) {
728 a.sink.Emit(event.Event{Kind: event.Usage, ModelRef: a.modelRef, Usage: usage, Pricing: a.pricing, UsageSource: event.UsageSourceCompaction})
729 }
730 }()
731 ch, err := a.prov.Stream(ctx, provider.Request{
732 Messages: []provider.Message{
733 {Role: provider.RoleSystem, Content: sys},
734 {Role: provider.RoleUser, Content: renderTranscript(region)},
735 },
736 Temperature: provider.OptionalTemperature(a.temperature),
737 })
738 if err != nil {
739 return "", err
740 }
741
742 // select on ctx.Done so a stalled stream (open but never delivering or closing)
743 // unblocks on timeout instead of pinning the "compacting…" placeholder forever.
744 var b strings.Builder
745 for {
746 select {
747 case <-ctx.Done():
748 return "", ctx.Err()
749 case chunk, ok := <-ch:
750 if !ok {
751 s := strings.TrimSpace(b.String())
752 if s == "" {
753 return "", fmt.Errorf("summarizer returned empty output")
754 }
755 return s, nil
756 }
757 switch chunk.Type {
758 case provider.ChunkText:
759 b.WriteString(chunk.Text)
760 case provider.ChunkUsage:
761 usage = chunk.Usage
762 case provider.ChunkError:
763 return "", chunk.Err
764 }
765 }
766 }
767 }
768
769 // summarizeWithRetry retries one non-timeout failure (a transient stream drop or
770 // rate blip); a timeout or a second failure returns so the caller folds
771 // mechanically rather than waiting again.
772 func (a *Agent) summarizeWithRetry(ctx context.Context, fold []provider.Message, instructions string) (string, error) {
773 summary, err := a.summarize(ctx, fold, instructions)
774 if err == nil || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
775 return summary, err
776 }
777 return a.summarize(ctx, fold, instructions)
778 }
779
780 // mechanicalFoldDigest is the deterministic stand-in used when the summarizer is
781 // unreachable: the foldable region is already archived, so the digest just notes
782 // the gap and points the model at the user for anything it needs from before it.
783 func mechanicalFoldDigest(n int, archive string) string {
784 where := "."
785 if archive != "" {
786 where = " (archived to " + archive + ")."
787 }
788 return fmt.Sprintf("%d earlier message(s) were folded here to free context, but the automatic summary was unavailable%s Ask the user if you need details from before this point.", n, where)
789 }
790
791 // renderTranscript flattens messages into a readable transcript for summarization.
792 func renderTranscript(msgs []provider.Message) string {
793 var b strings.Builder
794 for _, m := range msgs {
795 if m.LocalOnly {
796 continue
797 }
798 switch m.Role {
799 case provider.RoleUser:
800 fmt.Fprintf(&b, "[user]\n%s\n\n", m.Content)
801 case provider.RoleAssistant:
802 if m.Content != "" {
803 fmt.Fprintf(&b, "[assistant]\n%s\n", m.Content)
804 }
805 for _, tc := range m.ToolCalls {
806 fmt.Fprintf(&b, "[assistant calls %s] %s\n", tc.Name, summarizeToolArgs(tc.Arguments))
807 }
808 b.WriteString("\n")
809 case provider.RoleTool:
810 fmt.Fprintf(&b, "[tool %s result]\n%s\n\n", m.Name, m.Content)
811 case provider.RoleSystem:
812 fmt.Fprintf(&b, "[system]\n%s\n\n", m.Content)
813 }
814 }
815 return b.String()
816 }
817
818 // summarizeToolArgs returns a short summary of tool-call arguments instead of
819 // the full JSON. This prevents the summarizer from reproducing long argument
820 // text (like sub-agent task prompts) in the compaction summary, which would
821 // leak into the session as a user message (#4317).
822 func summarizeToolArgs(args string) string {
823 if args == "" {
824 return "(no arguments)"
825 }
826 var parsed map[string]any
827 if err := json.Unmarshal([]byte(args), &parsed); err != nil {
828 // Not valid JSON — return a length hint instead of raw text.
829 return fmt.Sprintf("(%d bytes)", len(args))
830 }
831 keys := make([]string, 0, len(parsed))
832 for k := range parsed {
833 keys = append(keys, k)
834 }
835 sort.Strings(keys)
836 return fmt.Sprintf("{%s} (%d keys)", strings.Join(keys, ", "), len(parsed))
837 }
838
839 // archiveMessages writes the dropped originals to a timestamped .jsonl (one
840 // message per line) under dir, returning the file path.
841 func archiveMessages(dir string, msgs []provider.Message) (string, error) {
842 if err := os.MkdirAll(dir, 0o755); err != nil {
843 return "", err
844 }
845 path := filepath.Join(dir, time.Now().Format("20060102-150405.000")+".jsonl")
846 f, err := os.Create(path)
847 if err != nil {
848 return "", err
849 }
850 defer f.Close()
851
852 enc := json.NewEncoder(f)
853 for _, m := range msgs {
854 if err := enc.Encode(m); err != nil {
855 return "", err
856 }
857 }
858 return path, nil
859 }
860
860 lines GO