From 436d02fdf3865279df3ff3bc14a91057b56d0464 Mon Sep 17 00:00:00 2001 From: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com> Date: Sun, 27 Sep 2026 11:17:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(compaction):=20=E6=B5=81=E5=9B=A0=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E8=80=8C=E5=85=B3=E9=97=AD=E6=97=B6=E4=B8=8D=E5=BE=97?= =?UTF-8?q?=E5=BD=93=E6=88=90=E7=A9=BA=E6=91=98=E8=A6=81=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit streamSummary 的 select 里 ctx.Done() 分支与 stream 关闭分支会同时就绪, select 在就绪分支之间随机选择:已经取消的调用有一半概率走关闭分支返回 nil, 被报成"成功返回空摘要",另一半才返回 context.Canceled。CI 上因此偶发: --- FAIL: TestSummarizationIsNotRetriedOnceTheTurnIsCancelled (0.00s) stream_test.go:122: Error: Should be true FAIL github.com/Tencent/WeKnora/internal/agent/compaction 这不只是测试问题。流因取消而关闭被当成成功,会让调用方把中途被截断的摘要 当有效 checkpoint 落盘(成功路径会走到 saveContextCheckpoint),而 validateSummary 只拦得住空摘要和 finish_reason 为 length 的截断。 修复:在流关闭分支返回成功之前先查 ctx.Err()。既纠正语义(取消就是取消, 不是空摘要),也消除两个分支同时就绪时的随机性。streamErr 的优先级仍在 ctx.Err() 之前,因为供应商报的错更具体;ctx 未取消、流自然关闭的正常完成 仍返回内容加 nil,stall 与重试逻辑未动。 回归用例 TestSummarizationIsCancelledWhenTheStreamCloses:假模型同步返回 一个已经关闭的 channel,传入已取消的 ctx,两个分支必然同时就绪。修复前 -count=50 有 20 次 FAIL,修复后 -count=50 全 PASS;既有 flaky 用例 TestSummarizationIsNotRetriedOnceTheTurnIsCancelled -count=100 全 PASS。 --- internal/agent/compaction/compactor.go | 8 +++++++ internal/agent/compaction/stream_test.go | 29 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/internal/agent/compaction/compactor.go b/internal/agent/compaction/compactor.go index 8c7b9c93..2cc85a35 100644 --- a/internal/agent/compaction/compactor.go +++ b/internal/agent/compaction/compactor.go @@ -269,6 +269,14 @@ func (c *Compactor) streamSummary( if streamErr != "" { return "", finishReason, fmt.Errorf("summarization stream error: %s", streamErr) } + // A stream that closes because the call was cancelled is a + // cancelled call, not an empty successful summary. Checking the + // context here, and not only in the ctx.Done() arm above, also + // settles the race where a cancellation closes the stream while + // both arms are ready and select picks between them at random. + if err := ctx.Err(); err != nil { + return "", finishReason, err + } return sb.String(), finishReason, nil } stall.Reset(timeout) diff --git a/internal/agent/compaction/stream_test.go b/internal/agent/compaction/stream_test.go index c7be0240..5230cedd 100644 --- a/internal/agent/compaction/stream_test.go +++ b/internal/agent/compaction/stream_test.go @@ -50,6 +50,22 @@ func (p *pacedChat) ChatStream( return ch, nil } +// closedStreamChat hands back a stream that is already closed: a provider that +// gives up the instant the call is cancelled. Closing before ChatStream returns +// makes the closed-stream arm and the ctx.Done() arm of the select ready at the +// same moment, so the outcome cannot depend on which one select happens to pick. +type closedStreamChat struct { + stubChat +} + +func (c *closedStreamChat) ChatStream( + context.Context, []chat.Message, *chat.ChatOptions, +) (<-chan types.StreamResponse, error) { + ch := make(chan types.StreamResponse) + close(ch) + return ch, nil +} + func pacedSettings() Settings { s := testSettings() s.StallTimeout = 80 * time.Millisecond @@ -97,6 +113,19 @@ func TestSummarizationIsCancelledWhenItStalls(t *testing.T) { "a single live turn has only its prefix to summarize, attempted twice") } +// A stream that closes because the call was cancelled is a cancelled call, not +// an empty successful summary. Both the ctx.Done() arm and the closed-stream arm +// are ready at once here, so this pins down the answer instead of letting select +// choose it at random. +func TestSummarizationIsCancelledWhenTheStreamCloses(t *testing.T) { + c := New(&closedStreamChat{}, newEstimator(t), pacedSettings()) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, _, err := c.streamSummary(ctx, nil, &chat.ChatOptions{}) + require.True(t, errors.Is(err, context.Canceled), "got %v", err) +} + // An error reported inside the stream is a failed attempt, not a summary. func TestSummarizationStreamErrorIsAFailure(t *testing.T) { chatModel := &pacedChat{chunks: []types.StreamResponse{