WeKnora/internal/agent/compaction
linus-liu-web 436d02fdf3 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。
2026-09-29 01:30:01 +08:00
..
checkpoint_test.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
compaction_test.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
compactor.go fix(compaction): 流因取消而关闭时不得当成空摘要成功返回 2026-09-29 01:30:01 +08:00
cutpoint.go feat(agent): keep long sandbox turns from truncating, overflowing, or rewriting files 2026-09-02 22:09:09 +08:00
fileops.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
fileops_test.go feat(agent): unify sandbox tools and drop host preloaded skills (#3033) 2026-09-05 16:45:59 +08:00
overflow.go feat(agent): keep long sandbox turns from truncating, overflowing, or rewriting files 2026-09-02 22:09:09 +08:00
overflow_test.go feat(agent): keep long sandbox turns from truncating, overflowing, or rewriting files 2026-09-02 22:09:09 +08:00
prepare.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
prompts.go feat(agent): keep long sandbox turns from truncating, overflowing, or rewriting files 2026-09-02 22:09:09 +08:00
serialize.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
settings.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
stream_test.go fix(compaction): 流因取消而关闭时不得当成空摘要成功返回 2026-09-29 01:30:01 +08:00