From 019a38f111f313898fff56e0b8a8924198a8b118 Mon Sep 17 00:00:00 2001 From: sausheong Date: Thu, 10 Sep 2026 16:34:12 +0800 Subject: [PATCH] Fix repeated stream finishes and preserve cancellation causes --- providers/openai/openai.go | 1 + providers/openai/repeated_finish_test.go | 42 ++++++++++++++++++++++++ runtime/runtime.go | 7 +++- runtime/steering.go | 2 +- runtime/steering_cause_test.go | 18 ++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 providers/openai/repeated_finish_test.go create mode 100644 runtime/steering_cause_test.go diff --git a/providers/openai/openai.go b/providers/openai/openai.go index 17b632a..0e614db 100644 --- a/providers/openai/openai.go +++ b/providers/openai/openai.go @@ -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) } } } diff --git a/providers/openai/repeated_finish_test.go b/providers/openai/repeated_finish_test.go new file mode 100644 index 0000000..f90c468 --- /dev/null +++ b/providers/openai/repeated_finish_test.go @@ -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) + } +} diff --git a/runtime/runtime.go b/runtime/runtime.go index 5e2cbda..8112544 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -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 { diff --git a/runtime/steering.go b/runtime/steering.go index 6977c2f..77be124 100644 --- a/runtime/steering.go +++ b/runtime/steering.go @@ -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 { diff --git a/runtime/steering_cause_test.go b/runtime/steering_cause_test.go new file mode 100644 index 0000000..c1cd49f --- /dev/null +++ b/runtime/steering_cause_test.go @@ -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) + } +}