Fix repeated stream finishes and preserve cancellation causes

This commit is contained in:
sausheong committed 2026-09-10 16:34:12 +08:00
1 parent 3170b28251
commit 019a38f111
5 files changed
+68 -2

No files matched your search

+6 -1
View File
@@ -602,7 +602,12 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
thresholdHit := window > 0 && estimate > int(threshold*float64(window))
msgCap := r.Compaction.MessageCap
countHit := msgCap > 0 && len(msgs) > msgCap
if thresholdHit || countHit {
keep := r.Compaction.PreserveTurns
if keep <= 0 {
keep = 4
}
_, _, eligible := compaction.Split(r.Session.View(), keep)
if (thresholdHit || countHit) && eligible {
r.emit(AgentEvent{Type: EventCompactionStart})
res, _ := r.Compaction.MaybeCompact(ctx, r.Session, compaction.ReasonPreventive, "")
if res.Compacted {
+1 -1
View File
@@ -45,7 +45,7 @@ func (r *Runtime) applySteering(ctx context.Context, pending []llm.ToolCall) (bo
return false, nil
}
if err := ctx.Err(); err != nil {
return false, err
return false, context.Cause(ctx)
}
message, err := source(ctx)
if err != nil || message == nil {
+18
View File
@@ -0,0 +1,18 @@
package runtime
import (
"context"
"errors"
"testing"
)
func TestSteeringPreservesProtocolCancellationCause(t *testing.T) {
cause := errors.New("duplicate tool call ID")
ctx, cancel := context.WithCancelCause(context.Background())
cancel(cause)
ctx = WithSteering(ctx, func(context.Context) (*SteeringMessage, error) { t.Fatal("called after cancellation"); return nil, nil })
_, err := (&Runtime{}).applySteering(ctx, nil)
if !errors.Is(err, cause) {
t.Fatalf("lost cause: %v", err)
}
}