fix(reconciliation): R6 改用 removal_candidates 视图,定时批每日只占一个名额
build / build (push) Successful in 2m28s

白盒补充复核提出的三项:
- P1 R6 与 /monitor/overview「需下架」计数口径不一致:R6 原先只在扫描结果里存在
  且上游 parse_status=completed 时才生成,上游仍在 processing/finalizing 或单条读取
  失败导致扫描缺行时,总览有数、对账无记录(陷阱 15)。改为直接由 removal_candidates
  视图行生成,台账细节按 publication 回填,计数与总览同一来源;补单测覆盖
  「上游 processing」「扫描未见」两种差异。
- P2 定时批失败后每个 tick 都重建:每日占位只排除 running/succeeded,failed 不占位,
  一次失败当天会每分钟刷批。占位条件加入 failed,重跑改为显式手动触发;补 PG 集成用例。
- P2 队列不可用时前端显示 0 等待 · 0 运行:改为按 available 判定并显示不可用文案;
  补组件用例。
This commit is contained in:
2026-12-01 18:56:05 +08:00
committed by zheromiku
parent a655f4cca8
commit 082f77fc18
6 changed files with 190 additions and 17 deletions
+58
View File
@@ -480,3 +480,61 @@ func TestRemoveR2RechecksOwnershipAndTreats404AsSuccess(t *testing.T) {
t.Fatalf("owned removal err=%v hit=%t", err, store.removeHit)
}
}
// TestExecuteRunR6UsesRemovalCandidatesViewOnly freezes 陷阱 15: R6 and
// /monitor/overview must read one implementation. An upstream document still
// processing/finalizing, or a single read that misses the listing, must not
// remove a row from the R6 set — otherwise the two pages report different
// "需下架" numbers for the same instant.
func TestExecuteRunR6UsesRemovalCandidatesViewOnly(t *testing.T) {
now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC)
ledger := []governance.ReconciliationLedgerRow{
{PublicationID: "pub-a", KnowledgeID: "doc-a", ProcessingState: "completed", RetrievalState: "unpublished", TargetScope: "internal", UpdatedAt: now.Add(-time.Hour)},
{PublicationID: "pub-b", KnowledgeID: "doc-b", ProcessingState: "completed", RetrievalState: "unpublished", TargetScope: "internal", UpdatedAt: now.Add(-time.Hour)},
{PublicationID: "pub-c", KnowledgeID: "doc-c", ProcessingState: "completed", RetrievalState: "unpublished", TargetScope: "internal", UpdatedAt: now.Add(-time.Hour)},
}
items := []weknora.Knowledge{
{ID: "doc-a", KnowledgeBaseID: "kb-1", Channel: "governance", ParseStatus: "processing", Metadata: map[string]any{"publication_id": "pub-a"}},
{ID: "doc-c", KnowledgeBaseID: "kb-1", Channel: "governance", ParseStatus: "completed", Metadata: map[string]any{"publication_id": "pub-c"}},
}
store := &fakeReconciliationStore{
run: governance.ReconciliationRun{ID: "run-r6", KBID: "kb-1", State: "running", Trigger: "manual", StartedAt: now},
ledger: ledger,
// The view is the authority: it lists three rows, including one whose
// upstream document is still processing and one absent from the scan.
removal: []governance.ReconciliationLedgerRow{
{PublicationID: "pub-a", KnowledgeID: "doc-a"},
{PublicationID: "pub-b", KnowledgeID: "doc-b"},
{PublicationID: "pub-c", KnowledgeID: "doc-c"},
},
}
remote := &fakeReconciliationRemote{items: items, missing: map[string]bool{"doc-b": true}}
service, err := New(Config{Store: store, Remote: remote, Now: func() time.Time { return now },
Grace: 30 * time.Minute, StallAfter: 2 * time.Hour, StaleAfter: 6 * time.Hour,
ConfirmConcurrency: 1, KeepRuns: 30, MaxAge: 90 * 24 * time.Hour,
WeKnoraWebBase: func(context.Context) string { return "https://test.example/" }})
if err != nil {
t.Fatal(err)
}
if err := service.ExecuteRun(context.Background(), "run-r6"); err != nil {
t.Fatal(err)
}
r6 := map[string]governance.ReconciliationFinding{}
for _, finding := range store.findings {
if finding.Type == "R6" {
r6[finding.PublicationID] = finding
}
}
if len(r6) != len(store.removal) {
t.Fatalf("R6 count = %d, removal candidates = %d; findings=%#v", len(r6), len(store.removal), store.findings)
}
if processing, ok := r6["pub-a"]; !ok || processing.WeKnoraParseState != "processing" || processing.NormalizedState != "processing" {
t.Fatalf("R6 must keep an upstream-processing row: %#v", processing)
}
if missing, ok := r6["pub-b"]; !ok || missing.DocumentID != "doc-b" {
t.Fatalf("R6 must keep a row the scan could not list: %#v", missing)
}
if store.completedRun == nil || store.completedRun.Counts["R6"] != len(store.removal) {
t.Fatalf("run counts R6 = %#v", store.completedRun)
}
}