fix(library): 当前生效版本改按精确查询

接管分类、基线判定、已知索引映射、同步闸口的创建判据与归档保留的保护标记,
原来都用 LibraryVersions(scope, 50/100) 这个分页窗口再在内存里筛。生效版本每
scope 只有一行、可能很久以前登记,历史一长就被挤出窗口 —— 于是"已生效"被读成
"没有生效":接管会把已经上线的目标重写一遍,基线入口会给已有权威版本的目标
再建一次基线。

现在统一走两条精确查询:LibraryCurrentVersion(kind='production_effective' AND
superseded_at IS NULL,每 scope 至多一行)与 LibraryVersionOfSnapshot(按快照
身份取回"已顶替"证据)。多行、自相矛盾、读不到一律 fail closed;同一含义只留
一份实现(knownIndexMapping 同步与恢复共用)。LibraryVersions 仍是 1..100 的
展示窗口,只用于列表。

验证:真实 PG 上先复现(窗口实现 ⇒ found=false),改后 7 条 store 契约用例与
worker 端到端判定用例全绿;本机 make verify 15 项 success(用例 1264 通过 /
0 失败 / 93 已声明可选跳过)。
This commit is contained in:
姚章浩
2026-10-30 03:09:21 +08:00
parent 67743f2285
commit 0088004cd6
15 changed files with 655 additions and 130 deletions
+41 -20
View File
@@ -152,38 +152,59 @@ func TestFinishLibrarySuccessReportsTerminalWriteFailure(t *testing.T) {
}
}
// 接管分类:阶段证据独立成立;查询失败=未知(不得写入);被顶替=不得覆盖。
// 接管分类:阶段证据独立成立;查询失败=未知(不得写入);被顶替=不得覆盖;
// 台账自相矛盾(未顶替但不是当前生效 / 已顶替却是当前生效)=未知,fail closed。
func TestClassifyTakeoverSeparatesUnknownPendingSupersededAndFresh(t *testing.T) {
job := governance.LibraryJob{Scope: "internal", Kind: governance.LibraryJobSync, TargetSnapshotID: "snap-1"}
finalized := job
finalized.Stages = []governance.LibraryStage{{Stage: "finalize", State: "succeeded"}}
effective := []governance.LibraryVersion{{Kind: governance.LibraryVersionProductionEffective, SnapshotID: "snap-1"}}
supersededVersion := []governance.LibraryVersion{{Kind: governance.LibraryVersionProductionEffective,
SnapshotID: "snap-1", SupersededAt: ptrTime(time.Now())}}
unrelated := []governance.LibraryVersion{{Kind: governance.LibraryVersionProductionEffective, SnapshotID: "snap-9"}}
queryErr := errors.New("version storage temporarily unavailable")
supersededAt := time.Now().UTC()
facts := func(current string, hasCurrent bool, snapshot string, hasSnapshot bool, superseded bool) libraryVersionFacts {
result := libraryVersionFacts{HasCurrent: hasCurrent, HasSnapshot: hasSnapshot}
if hasCurrent {
result.Current = governance.LibraryVersion{Kind: governance.LibraryVersionProductionEffective,
SnapshotID: current, SourceJobID: "job-cur"}
}
if hasSnapshot {
result.Snapshot = governance.LibraryVersion{Kind: governance.LibraryVersionProductionEffective,
SnapshotID: snapshot, SourceJobID: job.ID}
if superseded {
result.Snapshot.SupersededAt = &supersededAt
}
}
return result
}
queryErr := libraryVersionFacts{Err: errors.New("version storage temporarily unavailable")}
cases := []struct {
name string
job governance.LibraryJob
versions []governance.LibraryVersion
err error
want takeoverKind
name string
job governance.LibraryJob
facts libraryVersionFacts
want takeoverKind
}{
// 关键回归:查询失败 + 没有阶段证据 ⇒ 未知,不能当成"没有收尾证据"继续写。
{"query fails without stage evidence", job, nil, queryErr, takeoverUnknown},
{"query fails without stage evidence", job, queryErr, takeoverUnknown},
// 阶段证据先独立成立:查询失败不影响"只差收尾"的判定。
{"query fails but finalize stage succeeded", finalized, nil, queryErr, takeoverPendingFinal},
{"effective version for this snapshot", job, effective, nil, takeoverPendingFinal},
{"finalize stage succeeded", finalized, nil, nil, takeoverPendingFinal},
{"this snapshot was superseded", job, supersededVersion, nil, takeoverSuperseded},
{"no evidence at all", job, nil, nil, takeoverFresh},
{"another snapshot effective but this one has no row", job, unrelated, nil, takeoverFresh},
{"empty target snapshot", governance.LibraryJob{Scope: "internal"}, nil, nil, takeoverFresh},
{"query fails but finalize stage succeeded", finalized, queryErr, takeoverPendingFinal},
{"effective version for this snapshot", job, facts("snap-1", true, "snap-1", true, false), takeoverPendingFinal},
// 当前生效的就是本快照,但没有本作业的登记行(恢复/重放会改写作业归属)。
{"current is this snapshot without own row", job, facts("snap-1", true, "", false, false), takeoverPendingFinal},
{"finalize stage succeeded", finalized, libraryVersionFacts{}, takeoverPendingFinal},
{"this snapshot was superseded", job, facts("snap-9", true, "snap-1", true, true), takeoverSuperseded},
{"no evidence at all", job, libraryVersionFacts{}, takeoverFresh},
{"another snapshot effective but this one has no row", job, facts("snap-9", true, "", false, false), takeoverFresh},
{"empty target snapshot", governance.LibraryJob{Scope: "internal"}, libraryVersionFacts{}, takeoverFresh},
// 未顶替的登记行却不是当前生效版本:两个事实互相矛盾,不许挑一个继续写。
{"own row not superseded but current differs", job, facts("snap-9", true, "snap-1", true, false), takeoverUnknown},
// 已顶替却又是当前生效版本:同样是矛盾台账。
{"own row superseded but current is it", job, facts("snap-1", true, "snap-1", true, true), takeoverUnknown},
// 有登记行但读不到当前生效版本:无法确认它是不是生效版本。
{"own row without current", job, facts("", false, "snap-1", true, false), takeoverUnknown},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
if got := classifyTakeover(test.job, test.versions, test.err); got != test.want {
if got := classifyTakeover(test.job, test.facts); got != test.want {
t.Fatalf("classify = %q, want %q", got, test.want)
}
})