Fix repeated stream finishes and preserve cancellation causes
This commit is contained in:
1 parent
3170b28251
commit
4e10f36eed
5 files changed
+68
-2
No files matched your search
@@ -339,6 +339,7 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
||||
// Finish reason
|
||||
if choice.FinishReason == openai.FinishReasonToolCalls || choice.FinishReason == openai.FinishReasonStop {
|
||||
emitToolCalls(events, toolCalls)
|
||||
clear(toolCalls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/sausheong/harness/llm"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRepeatedFinishEmitsToolOnlyOnce(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_one\",\"type\":\"function\",\"function\":{\"name\":\"bash\",\"arguments\":\"{}\"}}]}}]}\n\n")
|
||||
for i := 0; i < 2; i++ {
|
||||
fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n")
|
||||
}
|
||||
fmt.Fprint(w, "data: [DONE]\n\n")
|
||||
}))
|
||||
defer srv.Close()
|
||||
p := NewOpenAIProviderWithKind("fixture", srv.URL, "openai-compatible")
|
||||
stream, err := p.ChatStream(context.Background(), llm.ChatRequest{Model: "fixture"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls, done := 0, 0
|
||||
for e := range stream {
|
||||
if e.Type == llm.EventToolCallDone {
|
||||
calls++
|
||||
}
|
||||
if e.Type == llm.EventDone {
|
||||
done++
|
||||
}
|
||||
if e.Type == llm.EventError {
|
||||
t.Fatal(e.Error)
|
||||
}
|
||||
}
|
||||
if calls != 1 || done != 1 {
|
||||
t.Fatalf("calls=%d done=%d", calls, done)
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -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
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user