diff --git a/CHANGELOG.md b/CHANGELOG.md index bcc9339..92127c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 开发快照(空目标首次同步准入) + +### Fixed + +- 发布概览与同步提交统一探测未知目标:真实空库允许进入首次同步条件判断,无法连接不再误报为非空库。探测超时后拒绝放行,授权、恢复演练及写入闸口仍须满足。 + ## 开发快照(发布目标历史隔离) ### Fixed diff --git a/internal/httpapi/library_flow.go b/internal/httpapi/library_flow.go index a4f7ec2..49c72a9 100644 --- a/internal/httpapi/library_flow.go +++ b/internal/httpapi/library_flow.go @@ -90,8 +90,27 @@ func productionScopeState(ctx context.Context, config Config, scope string) (boo v071.ProductionScope{TenantID: config.LibraryProductionTenantID, KnowledgeBaseID: kb}) } -// productionScopeEmpty 保留"只问空不空"的调用形状;逐表行数在需要举证时用 -// productionScopeState 取。 +// unknownLibraryTarget distinguishes an empty target from unreadable state. +// Overview and enqueue share this probe; neither may infer nonempty from failure. +func unknownLibraryTarget(ctx context.Context, config Config, scope string) libraryKnownView { + probe := config.LibraryScopeProbe + if probe == nil { + probe = func(ctx context.Context, scope string) (bool, map[string]int64, error) { + return productionScopeState(ctx, config, scope) + } + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + empty, _, err := probe(ctx, scope) + if err != nil { + return libraryKnownView{State: "unreachable", Reason: "无法读取目标整库状态:请检查目标连接后重试,不能据此判定空库或建立基线"} + } + if empty { + return libraryKnownView{State: "unknown", EmptyBaseline: true, Reason: "生产目标可读且为空:本次是首次发布,写入前仍会生成完整预备份"} + } + return libraryKnownView{State: "unknown", Reason: "目标非空且尚无权威版本"} +} + func productionScopeEmpty(ctx context.Context, config Config, scope string) (bool, error) { empty, _, err := productionScopeState(ctx, config, scope) return empty, err @@ -638,7 +657,9 @@ func librarySyncGateWithGrant(config Config, production libraryKnownView, prebac // 这就是一个合法的"空基线",不应该被永久挡住。非空但无权威版本仍然阻断, // 指向真实可行的入口(在维护隔离下捕获目标整库并校验后登记基线), // 而不是让人去手写版本或清空目标。 - if production.State != "known" && !production.EmptyBaseline { + if production.State == "unreachable" { + gate.Reasons = append(gate.Reasons, production.Reason) + } else if production.State != "known" && !production.EmptyBaseline { gate.Reasons = append(gate.Reasons, "目标非空且没有生效版本记录:先建立目标权威基线(维护隔离下捕获整库并逐类校验),或确认目标确实是空库") } if target == "" { @@ -713,6 +734,8 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi if hasCurrent { view := libraryVersionViewOf(current) overview.Production = libraryKnownView{State: "known", Version: &view} + } else { + overview.Production = unknownLibraryTarget(r.Context(), config, scope) } target := "" if overview.TestSealed != nil { @@ -879,9 +902,8 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) { overview.Production = libraryKnownView{State: "known", Version: &view} } overview.Prebackup = libraryKnownView{State: "unknown"} - if empty, probeErr := productionScopeEmpty(r.Context(), config, request.Scope); probeErr == nil && empty { - overview.Production = libraryKnownView{State: "unknown", EmptyBaseline: true, - Reason: "生产目标可读且为空:本次是首次发布,写入前仍会生成完整预备份"} + if !hasCurrent { + overview.Production = unknownLibraryTarget(r.Context(), config, request.Scope) } for index := range versions { item := versions[index] diff --git a/internal/httpapi/library_overview_empty_test.go b/internal/httpapi/library_overview_empty_test.go new file mode 100644 index 0000000..fc32142 --- /dev/null +++ b/internal/httpapi/library_overview_empty_test.go @@ -0,0 +1,101 @@ +package httpapi + +import ( + "context" + "encoding/json" + "errors" + "knowledge-governance/internal/governance" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +func TestEmptyTargetOverviewAndEnqueueAgree(t *testing.T) { + for _, failed := range []bool{false, true} { + root := libraryFixtureRoot(t) + target := sealSharedVersions(t, root, 1, 4)[0] + digest, err := librarySyncParamsDigest("internal", target) + if err != nil { + t.Fatal(err) + } + now := time.Now().UTC() + store := &fakeLibraryStore{versions: []governance.LibraryVersion{{ID: "seal", Scope: "internal", Kind: governance.LibraryVersionTestSealed, SnapshotID: target, CreatedAt: now}}} + store.enqueueEchoRequestedID = true + store.grant = &governance.LibraryReleaseAuthorization{ID: "grant", Scope: "internal", SnapshotID: target, ParamsDigest: digest, AuthorizedBy: "admin", ExpiresAt: now.Add(time.Hour), CreatedAt: now} + config := Config{Store: store, LibraryArchiveRoot: root, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true, LibraryProductionDatabaseURL: "postgres://unused.invalid/test", LibraryProductionInternalKB: "kb", LibraryCapacityBudgetBytes: 1 << 40, LibraryScopeProbe: func(context.Context, string) (bool, map[string]int64, error) { + if failed { + return false, nil, errors.New("offline") + } + return true, nil, nil + }} + rec := httptest.NewRecorder() + libraryOverviewHandler(rec, httptest.NewRequest(http.MethodGet, "/api/v1/library/overview?scope=internal", nil), config) + var overview libraryOverview + if err := json.Unmarshal(rec.Body.Bytes(), &overview); err != nil { + t.Fatal(err) + } + if overview.Gate.SyncAllowed == failed { + t.Fatalf("failed=%v gate=%+v", failed, overview.Gate) + } + rec = httptest.NewRecorder() + librarySyncEnqueue(rec, httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs", strings.NewReader(`{"scope":"internal","target_snapshot_id":"`+target+`"}`)), config) + if failed { + if rec.Code != http.StatusServiceUnavailable || store.createN != 0 { + t.Fatalf("unreachable target enqueued: %d %s", rec.Code, rec.Body.String()) + } + } else if rec.Code != http.StatusAccepted || store.createN != 1 { + t.Fatalf("empty target rejected: %d %s", rec.Code, rec.Body.String()) + } + } +} + +func TestLibraryOverviewProbesUnknownTarget(t *testing.T) { + for _, tc := range []struct { + name string + empty bool + err error + want string + }{ + {name: "empty", empty: true}, + {name: "nonempty", want: "目标非空"}, + {name: "unreachable", err: errors.New("connection unavailable"), want: "无法读取目标整库状态"}, + } { + t.Run(tc.name, func(t *testing.T) { + calls := 0 + config := Config{Store: &fakeLibraryStore{}, LibraryArchiveRoot: t.TempDir(), LibraryScopeProbe: func(ctx context.Context, scope string) (bool, map[string]int64, error) { + calls++ + if scope != "internal" { + t.Fatalf("scope=%s", scope) + } + return tc.empty, nil, tc.err + }} + recorder := httptest.NewRecorder() + libraryOverviewHandler(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/library/overview?scope=internal", nil), config) + if recorder.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String()) + } + var body libraryOverview + if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { + t.Fatal(err) + } + if calls != 1 { + t.Fatalf("target probe calls=%d, want 1", calls) + } + if body.Production.EmptyBaseline != tc.empty { + t.Fatalf("production=%+v", body.Production) + } + reasons := strings.Join(body.Gate.Reasons, "|") + if tc.want != "" && !strings.Contains(reasons, tc.want) { + t.Fatalf("reasons=%s", reasons) + } + if (tc.empty || tc.err != nil) && strings.Contains(reasons, "目标非空") { + t.Fatalf("not proven nonempty: %s", reasons) + } + if body.Gate.SyncAllowed { + t.Fatal("empty target must not bypass authorization and write gates") + } + }) + } +}