fix(compaction): 流因取消而关闭时不得当成空摘要成功返回
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。
main
parent
cf079b8dcd
commit
436d02fdf3
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
Loading…
Reference in New Issue