Honor cancellation during compaction streams

This commit is contained in:
Ollie Agent 2026-08-18 06:59:35 +02:00
parent c6d9b4aa23
commit 7ec8583e06
1 changed files with 34 additions and 14 deletions

View File

@ -270,15 +270,25 @@ func (s *History) compact(ctx context.Context, b backend.Backend) (int, string,
}
var summary strings.Builder
for ev := range ch {
if ev.Content != "" {
summary.WriteString(ev.Content)
}
if ev.Done {
break
for {
select {
case <-ctx.Done():
return 0, "", ctx.Err()
case ev, ok := <-ch:
if !ok {
return 0, "", fmt.Errorf("compact: stream closed before completion")
}
if ev.Content != "" {
summary.WriteString(ev.Content)
}
if ev.Done {
goto summaryDone
}
}
}
summaryDone:
summaryText := strings.TrimSpace(summary.String())
if summaryText == "" {
return 0, "", fmt.Errorf("compact: empty summary")
@ -571,16 +581,26 @@ func (s *History) stripCold(ctx context.Context, b backend.Backend) (backend.Usa
var resp strings.Builder
var usage backend.Usage
var reported bool
for ev := range ch {
if ev.Content != "" {
resp.WriteString(ev.Content)
}
if ev.Done {
usage = ev.Usage
reported = true
break
for {
select {
case <-ctx.Done():
return usage, true
case ev, ok := <-ch:
if !ok {
return usage, true
}
if ev.Content != "" {
resp.WriteString(ev.Content)
}
if ev.Done {
usage = ev.Usage
reported = true
goto responseDone
}
}
}
responseDone:
if !reported {
usage.InputTokens = len(sb.String()) / 4
usage.OutputTokens = resp.Len() / 4