sync: collector/ 同步自 inkstone/arcrun-rag@85cb90d(桌面小幫手 0.18.40)
This commit is contained in:
@@ -201,6 +201,131 @@ func TestDirect_LargeBacklog_ProcessedInNewestFirstBatches(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── 4) 退避中的檔案不該永久佔滿單輪名額(arcrun-rag#104 comment 4480,t217)──
|
||||
//
|
||||
// 背景:leo 實測 leo21c 帳號的積壓「1691→1880→1936 筆從不減少」,
|
||||
// 「已經好久沒有加過任何檔案,哪來的這些筆數?那就是之前卡住的,就是你要解決的問題」
|
||||
// 「佇列就是問題本身」。
|
||||
//
|
||||
// 根因:舊版把 perRunCap 套在「排序後的原始清單」上。mtime 最新的幾個檔如果持續
|
||||
// 失敗(進入退避),它們不會因為在退避就往後排,於是每一輪都繼續佔著最前面的
|
||||
// perRunCap 個名額——即使這一輪根本不會被嘗試,只是被跳過。排在它們後面、
|
||||
// 從沒被嘗試過的健康檔案因此永遠排不到,不管跑幾輪都一樣。
|
||||
//
|
||||
// 這個測試重現該情境:3 個 mtime 最新的檔一直失敗(模擬持續性錯誤,例如票上量到的
|
||||
// 雲端 subrequest 上限或萃取回傳格式錯誤),5 個 mtime較舊、原本會成功的健康檔案
|
||||
// 排在它們後面。單輪上限=3。
|
||||
//
|
||||
// - 第一輪:全部檔案都還沒失敗過(FailCount=0),cap 選中 mtime 最新的 3 個
|
||||
// (也就是那 3 個會一直失敗的檔),全部失敗,記下退避(下次重試在 60 秒後)。
|
||||
// - 第二輪(緊接著呼叫,真實時間遠不到 60 秒):那 3 個檔仍在退避中。
|
||||
// 舊版行為:cap 依然套在原始排序上,選中的還是同一批退避中的檔案 ⇒
|
||||
// 這一輪 0 個健康檔案被嘗試,健康檔案永遠排不到。
|
||||
// 修好後的行為:退避中的檔案被分流到 waiting、不佔 ready 的名額,
|
||||
// cap 改套用在 ready 上 ⇒ 健康檔案的前 3 名遞補上來,這一輪就會被嘗試並成功。
|
||||
func TestDirect_StarvedBacklog_HealthyFilesEventuallyGetATurn(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
|
||||
// 3 個「一直失敗」的檔,mtime 最新(若 bug 還在,會永久佔滿 cap)。
|
||||
blockers := []string{"blocker-a", "blocker-b", "blocker-c"}
|
||||
for i, n := range blockers {
|
||||
writeFile(t, root, n+".md", "持續失敗的內容 "+n, baseTime.Add(time.Duration(10+i)*time.Minute))
|
||||
}
|
||||
// 5 個「健康」的檔,mtime 較舊(排在後面,理應遞補上來)。
|
||||
healthy := []string{"h5", "h4", "h3", "h2", "h1"}
|
||||
for i, n := range healthy {
|
||||
writeFile(t, root, n+".md", "健康內容 "+n, baseTime.Add(time.Duration(4-i)*time.Minute))
|
||||
}
|
||||
|
||||
failingPages := map[string]bool{"blocker-a": true, "blocker-b": true, "blocker-c": true}
|
||||
var mu sync.Mutex
|
||||
var succeededPages []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if answeredFolderTreePost(w, r) {
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(body, &m)
|
||||
pageName, _ := m["page_name"].(string)
|
||||
if strings.HasPrefix(pageName, "資料夾總覽") {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"success": true})
|
||||
return
|
||||
}
|
||||
if failingPages[pageName] {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = w.Write([]byte(`{"success":false,"error":"boom"}`))
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
succeededPages = append(succeededPages, pageName)
|
||||
mu.Unlock()
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"success": true})
|
||||
}))
|
||||
defer srv.Close()
|
||||
defer gemmaStub(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"candidates": []map[string]any{{
|
||||
"content": map[string]any{"parts": []map[string]any{{"text": cardFixture("卡", "測試")}}},
|
||||
}},
|
||||
})
|
||||
})()
|
||||
|
||||
cfg := &DirectConfig{
|
||||
WatchFolders: []string{root},
|
||||
Manifest: filepath.Join(t.TempDir(), "m.json"),
|
||||
CypherURL: srv.URL, Namespace: "demo", APIKey: "demo",
|
||||
Library: "kb", Extractor: "gemma", ExtractorExplicit: true, GeminiAPIKey: "k-test",
|
||||
CardIngestWF: "rag_ingest_card", MaxRemoved: DefaultMaxRemovedRatio,
|
||||
MaxEventsPerRun: 3,
|
||||
}
|
||||
|
||||
// 第一輪:8 個檔都還沒失敗過,cap 選中 mtime 最新的 3 個(blocker-a/b/c),全部失敗。
|
||||
results1, _, _ := RunDirectOnce(cfg, false)
|
||||
if got := ingestedPaths(results1); len(got) != 0 {
|
||||
t.Fatalf("第一輪不該有任何成功(cap 選中的 3 個全會失敗),got %v", got)
|
||||
}
|
||||
var failedCount int
|
||||
for _, r := range results1 {
|
||||
if r.Status == "failed" {
|
||||
failedCount++
|
||||
}
|
||||
}
|
||||
if failedCount != 3 {
|
||||
t.Fatalf("第一輪應該有 3 筆真的被嘗試且失敗(blocker-a/b/c),got %d:%+v", failedCount, results1)
|
||||
}
|
||||
|
||||
// 第二輪:緊接著呼叫(真實時間遠不到 60 秒退避窗口)。blocker-a/b/c 仍在退避中。
|
||||
results2, _, _ := RunDirectOnce(cfg, false)
|
||||
ingested2 := ingestedPaths(results2)
|
||||
if len(ingested2) != 3 {
|
||||
t.Fatalf("🔴 第二輪應該有 3 個健康檔案遞補上來被嘗試並成功——"+
|
||||
"如果這裡是 0,代表退避中的 blocker-a/b/c 又佔滿了本輪名額,"+
|
||||
"健康檔案永遠排不到(這正是 leo 實測「佇列從不減少」的那個 bug)。got %d:%+v",
|
||||
len(ingested2), results2)
|
||||
}
|
||||
wantSecond := []string{"h5.md", "h4.md", "h3.md"}
|
||||
for i, w := range wantSecond {
|
||||
if ingested2[i] != w {
|
||||
t.Fatalf("第二輪順序=%v,want %v(健康檔案仍照 mtime 新到舊遞補)", ingested2, wantSecond)
|
||||
}
|
||||
}
|
||||
// blocker-a/b/c 這一輪不該再被真的嘗試(還在退避中)——它們只會以「skipped」出現。
|
||||
for _, r := range results2 {
|
||||
if r.Path == "blocker-a.md" || r.Path == "blocker-b.md" || r.Path == "blocker-c.md" {
|
||||
if r.Status != "skipped" {
|
||||
t.Fatalf("退避中的 %s 這一輪不該被真的嘗試,got status=%s", r.Path, r.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(succeededPages) != 3 {
|
||||
t.Fatalf("雲端應該收到 3 筆健康卡片,got %d: %v", len(succeededPages), succeededPages)
|
||||
}
|
||||
}
|
||||
|
||||
func ingestedPaths(results []DirectResult) []string {
|
||||
var out []string
|
||||
for _, r := range results {
|
||||
|
||||
Reference in New Issue
Block a user