Reject empty and truncated answers and expose complete tool calls

This commit is contained in:
sausheong committed 2026-09-11 23:48:25 +08:00
1 parent 019a38f111
commit 3ca0994437
14 files changed
+278 -7

No files matched your search

+6
View File
@@ -119,6 +119,7 @@ func (s *Summarizer) callOnce(ctx context.Context, transcript, additionalInstruc
}
var sb strings.Builder
var terminalReason string
for ev := range stream {
switch ev.Type {
case llm.EventTextDelta:
@@ -126,6 +127,8 @@ func (s *Summarizer) callOnce(ctx context.Context, transcript, additionalInstruc
return "", errors.New("summariser response exceeds 256 KiB")
}
sb.WriteString(ev.Text)
case llm.EventDone:
terminalReason = ev.StopReason
case llm.EventError:
return "", fmt.Errorf("compaction: stream error: %w", ev.Error)
}
@@ -133,6 +136,9 @@ func (s *Summarizer) callOnce(ctx context.Context, transcript, additionalInstruc
if err := callCtx.Err(); err != nil {
return "", err
}
if terminalReason == "length" || terminalReason == "max_tokens" {
return "", errors.New("summariser reached its output limit; original history retained")
}
out := strings.TrimSpace(sb.String())
if out == "" {
return "", ErrEmptySummary
+26
View File
@@ -0,0 +1,26 @@
package compaction
import (
"context"
"github.com/sausheong/harness/llm"
"github.com/sausheong/harness/llm/llmtest"
"strings"
"testing"
)
type truncatedSummaryProvider struct{ llmtest.Base }
func (truncatedSummaryProvider) ChatStream(context.Context, llm.ChatRequest) (<-chan llm.ChatEvent, error) {
ch := make(chan llm.ChatEvent, 2)
ch <- llm.ChatEvent{Type: llm.EventTextDelta, Text: "Incomplete summary"}
ch <- llm.ChatEvent{Type: llm.EventDone, StopReason: "length"}
close(ch)
return ch, nil
}
func TestRejectTruncatedSummary(t *testing.T) {
s := Summarizer{Provider: truncatedSummaryProvider{}, Model: "fixture"}
out, err := s.callOnce(context.Background(), "history", "")
if err == nil || out != "" || !strings.Contains(err.Error(), "output limit") {
t.Fatalf("%q %v", out, err)
}
}