fix(worker): 封存来源校验失败写入失败终态
build / build (push) Successful in 1m35s

verifyCapturedSource 失败时原实现只 return err:队列作业已是 failed,业务行
library_jobs 却停在 running/export、error 为空,页面永远显示"导出中"(实测
libseal-lib-internal-20261102T234053)。同函数其余四条失败分支都同时写 stage 与
job,这一条漏了。

现在 ensureCapturedSource 在校验失败时按既有惯例写 stage failed + job failed,
原错原样进取值、stage detail 与 job.error,恢复文案指向"修配置后重新封存"(快照
字节完好,不需要重采)。成功路径不变。

验证:
- RED:新增用例在修复前失败于 "stage writes = [], want exactly one failed stage"
- GREEN:TestCapturedSourceMismatchPersistsFailedStageAndJob /
  TestCapturedSourceMatchStaysSuccessful 通过
- make verify:16 项必需检查全绿(tests-go 含新用例)
This commit is contained in:
姚章浩
2026-11-03 07:59:40 +08:00
parent 22fb33aa75
commit 4421dd38ce
4 changed files with 124 additions and 1 deletions
+4
View File
@@ -25,6 +25,10 @@
done 任一状态都拒绝绑定冲突;合法 dispatching + 精确 marker 仍可确认一次。
- 真 PG/adapter 反例:effect 锁等待过期、恢复投递九类错配、错治理域/错批次/unknown、
四状态绑定冲突与合法 marker 正例。
- **整库封存来源校验失败不再悬挂**:快照来源与配置不一致时,队列作业已 failed 而页面
一直显示"导出中"(实测 `libseal-lib-internal-20261102T234053`)。现在失败同时写入
`library_job_stages` 与 `library_jobs`(failed + 原错 + "修配置后重新封存"),原错
不被遮蔽;来源一致的成功路径不变。
### Added
@@ -0,0 +1,100 @@
package main
import (
"context"
"os"
"strings"
"testing"
"time"
"knowledge-governance/internal/governance"
)
// fakeStageStore 记录封存失败分支对业务作业行的两次落库调用。
type fakeStageStore struct {
stageCalls []string
jobCalls []string
}
func (f *fakeStageStore) UpdateLibraryStage(_ context.Context, jobID, stage, state string, _, _, _, _ *int64, detail string, _ time.Time) error {
f.stageCalls = append(f.stageCalls, jobID+"|"+stage+"|"+state+"|"+detail)
return nil
}
func (f *fakeStageStore) AdvanceLibraryJob(_ context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, _, _ string, _ time.Time) error {
f.jobCalls = append(f.jobCalls, jobID+"|"+string(state)+"|"+stage+"|"+message+"|"+recovery)
return nil
}
func (f *fakeStageStore) LibraryVersionByRef(context.Context, string, governance.LibraryVersionKind, string, string) (governance.LibraryVersion, bool, error) {
return governance.LibraryVersion{}, false, nil
}
func (f *fakeStageStore) RecordLibraryVersion(context.Context, governance.LibraryVersion) error {
return nil
}
// 归档根必须是非符号链接的 0700 目录;t.TempDir 的权限受 umask 影响。
func privateDir(t *testing.T) string {
t.Helper()
root := t.TempDir()
if err := os.Chmod(root, 0700); err != nil {
t.Fatal(err)
}
return root
}
func sourceFor(scope, kb string) governance.LibrarySourceIdentity {
return governance.LibrarySourceIdentity{Project: "kgmaint-weknora", App: "kgmaint-app", Scope: scope, TenantID: 1, KnowledgeBaseID: kb}
}
// RED 用例:快照来源与配置不一致时,队列已 failed、页面却永远"导出中"(真实事故:
// libseal-lib-internal-20261102T234053 running/export/error 空)。失败必须同时写进
// stage 与 job,且原错原样出现在两处与返回值里 —— 不能被"已记录失败"遮住。
func TestCapturedSourceMismatchPersistsFailedStageAndJob(t *testing.T) {
root := privateDir(t)
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
// 清单里是 kb-baseline;worker 配置漂移后拿的是另一个 KB(256 验收遗留正是这样)。
source := sourceFor("internal", "0c84a098-d3ed-4b82-a165-e6de9691d6a8")
store := &fakeStageStore{}
err := ensureCapturedSource(context.Background(), store, "libseal-lib-internal-20261102T234053",
root, snapshotID, source, time.Now().UTC())
if err == nil || !strings.Contains(err.Error(), "captured manifest differs from configured source") {
t.Fatalf("mismatch must keep the original error, got %v", err)
}
if len(store.stageCalls) != 1 {
t.Fatalf("stage writes = %v, want exactly one failed stage", store.stageCalls)
}
if !strings.HasPrefix(store.stageCalls[0], "libseal-lib-internal-20261102T234053|export|failed|") ||
!strings.Contains(store.stageCalls[0], "captured manifest differs from configured source") {
t.Fatalf("stage write = %q, want failed+original error", store.stageCalls[0])
}
if len(store.jobCalls) != 1 {
t.Fatalf("job writes = %v, want exactly one failed job", store.jobCalls)
}
if !strings.HasPrefix(store.jobCalls[0], "libseal-lib-internal-20261102T234053|failed|export|") ||
!strings.Contains(store.jobCalls[0], "captured manifest differs from configured source") {
t.Fatalf("job write = %q, want failed+original error", store.jobCalls[0])
}
// 恢复说明必须指向真实下一步,而不是暗示数据损坏。
if !strings.Contains(store.jobCalls[0], "重新封存") {
t.Fatalf("job recovery = %q, want a re-seal instruction", store.jobCalls[0])
}
}
// 正例对照:身份一致时不得写任何失败,也不得把成功路径变成失败。
func TestCapturedSourceMatchStaysSuccessful(t *testing.T) {
root := privateDir(t)
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
store := &fakeStageStore{}
if err := ensureCapturedSource(context.Background(), store, "libseal-ok",
root, snapshotID, sourceFor("internal", "kb-baseline"), time.Now().UTC()); err != nil {
t.Fatalf("matching source rejected: %v", err)
}
if len(store.stageCalls) != 0 || len(store.jobCalls) != 0 {
t.Fatalf("success path wrote failures: stage=%v job=%v", store.stageCalls, store.jobCalls)
}
}
+19
View File
@@ -1,10 +1,12 @@
package main
import (
"context"
"errors"
"knowledge-governance/internal/appconfig"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/librarysnapshot"
"time"
)
func configuredCaptureSource(config appconfig.Config, scope string) governance.LibrarySourceIdentity {
@@ -32,3 +34,20 @@ func verifyCapturedSource(root, snapshotID string, source governance.LibrarySour
}
return nil
}
// ensureCapturedSource 校验快照来源。校验失败必须把失败写进业务行:
// 队列侧此时已 failed,只 return err 会让页面永远停在"导出中"
// (实际事故:libseal-lib-internal-20261102T234053 running/export/error 空)。
func ensureCapturedSource(ctx context.Context, store libraryStageStore, jobID, root, snapshotID string, source governance.LibrarySourceIdentity, now time.Time) error {
err := verifyCapturedSource(root, snapshotID, source)
if err == nil {
return nil
}
// 快照本身已产生且字节完好,错的只是 worker 读到的来源身份;因此恢复路径是
// "修配置后重新登记/重新封存",不是"重采数据"。原错原样写进 stage、job 与
// 返回值:改写成更友好的文案会让真正的配置漂移查不出来。
libraryStageRecorder{store: store, jobID: jobID}.Stage(ctx, "export", "failed", err.Error())
_ = store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobFailed, "export", err.Error(),
"快照已产生但来源身份与配置不一致,未登记为测试封存;修正来源配置后重新封存", "", "", now)
return err
}
+1 -1
View File
@@ -368,7 +368,7 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
"快照已产生但来源身份不符,未登记为测试封存;核对执行器后重新封存", "", "", finishedAt)
return identityErr
}
if err := verifyCapturedSource(config.LibraryArchiveRoot, result.SnapshotID, source); err != nil {
if err := ensureCapturedSource(ctx, store, libraryJobID, config.LibraryArchiveRoot, result.SnapshotID, source, finishedAt); err != nil {
return err
}
// 封存产物成版本记录:只登记"测试封存",绝不写成生产生效版本。