package v071 import ( "context" "errors" "fmt" "net/http" "net/http/httptest" "os" "sync/atomic" "testing" "time" "knowledge-governance/internal/libraryfreeze" "knowledge-governance/internal/librarysnapshot" ) var errTestContractChanged = errors.New("source contract changed inside the maintenance window") type captureWriter struct { running atomic.Bool db *libraryFixture late bool lateSource bool stops int } func (c *captureWriter) Inventory(context.Context) ([]libraryfreeze.Service, error) { r := c.running.Load() return []libraryfreeze.Service{{ID: "test-writer", Running: r, Healthy: r}}, nil } func (c *captureWriter) Stop(context.Context, string) error { c.stops++ c.running.Store(false) if c.late { c.db.tx.pending = 1 } if c.lateSource { c.db.tx.sourcePending = 1 } return nil } func (c *captureWriter) Start(context.Context, string) error { c.running.Store(true); return nil } // The contract is read before the window and must be re-checked inside it: a // model or mapping change between those two moments must abort, restore writers // and seal nothing. func TestLibraryCaptureRechecksContractInsideFrozenWindow(t *testing.T) { writer := &captureWriter{db: newLibraryFixture()} writer.running.Store(true) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/chunks/_mapping": fmt.Fprint(w, `{"chunks":{"mappings":{"properties":{"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"},"tenant_id":{"type":"long"}}}}}`) case "/chunks/_pit": fmt.Fprint(w, `{"id":"fixed-pit"}`) case "/_search": fmt.Fprint(w, `{"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"hits":[]}}`) case "/_pit": fmt.Fprint(w, `{"succeeded":true}`) default: http.Error(w, "unexpected", 400) } })) defer server.Close() search, err := NewSearchExporter(SearchExporterConfig{BaseURL: server.URL, Index: "chunks", PageSize: 10}) if err != nil { t.Fatal(err) } mapping, err := search.inspectMapping(context.Background()) if err != nil { t.Fatal(err) } root := t.TempDir() _ = os.Chmod(root, 0700) freeze, err := libraryfreeze.Open(root, "test-source", writer) if err != nil { t.Fatal(err) } defer freeze.Close() archiveRoot := t.TempDir() _ = os.Chmod(archiveRoot, 0700) archive, err := librarysnapshot.Open(archiveRoot, 1024*1024) if err != nil { t.Fatal(err) } defer archive.Close() req := LibraryCaptureRequest{JobID: "contract-job", Scope: "internal", Identity: PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, Compatibility: librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024, IndexMappingSHA256: mapping.digest}, CreatedAt: time.Date(2026, 10, 28, 0, 0, 0, 0, time.UTC), MaxDocuments: 100} called := 0 req.VerifyContract = func(context.Context) error { called++ if writer.running.Load() { t.Error("contract re-check ran before writers stopped") } return errTestContractChanged } ready := func(context.Context) error { return nil } result, err := CaptureLibrary(context.Background(), freeze, archive, dbFixture(t, writer), search, &libraryCandidateReader{}, ready, req) if err == nil || result.State == "succeeded" || result.SnapshotID != "" { t.Fatalf("contract change was sealed: %+v %v", result, err) } if called != 1 { t.Fatalf("contract re-check called %d times", called) } if !writer.running.Load() { t.Fatal("writer left stopped after a contract abort") } if usage, usageErr := archive.Usage(); usageErr != nil || len(usage) != 0 { t.Fatalf("aborted capture left an artifact: %v %v", usage, usageErr) } } func dbFixture(t *testing.T, writer *captureWriter) *libraryFixture { t.Helper() return writer.db } func TestLibraryCaptureMaintenanceOwnsArchiveAndRestoresOnFailure(t *testing.T) { for _, mode := range []string{"success", "initial-busy", "late-pending", "search-failure", "other-library-busy", "other-library-late-pending"} { t.Run(mode, func(t *testing.T) { db := newLibraryFixture() writer := &captureWriter{db: db, late: mode == "late-pending", lateSource: mode == "other-library-late-pending"} if mode == "other-library-busy" { db.tx.sourcePending = 1 } writer.running.Store(true) if mode == "initial-busy" { db.tx.pending = 1 } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/chunks/_mapping": fmt.Fprint(w, `{"chunks":{"mappings":{"properties":{"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"},"tenant_id":{"type":"long"}}}}}`) case "/chunks/_pit": if writer.running.Load() { t.Error("PIT opened before writers stopped") } fmt.Fprint(w, `{"id":"fixed-pit"}`) case "/_search": if mode == "search-failure" { http.Error(w, "fixture failure", 503) return } fmt.Fprint(w, `{"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"hits":[]}}`) case "/_pit": fmt.Fprint(w, `{"succeeded":true}`) default: http.Error(w, "unexpected", 400) } })) defer server.Close() search, err := NewSearchExporter(SearchExporterConfig{BaseURL: server.URL, Index: "chunks", PageSize: 10}) if err != nil { t.Fatal(err) } mapping, err := search.inspectMapping(context.Background()) if err != nil { t.Fatal(err) } root := t.TempDir() _ = os.Chmod(root, 0700) freeze, err := libraryfreeze.Open(root, "test-source", writer) if err != nil { t.Fatal(err) } defer freeze.Close() archiveRoot := t.TempDir() _ = os.Chmod(archiveRoot, 0700) archive, err := librarysnapshot.Open(archiveRoot, 1024*1024) if err != nil { t.Fatal(err) } defer archive.Close() req := LibraryCaptureRequest{JobID: "capture-job", Scope: "internal", Identity: PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, Compatibility: librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024, IndexMappingSHA256: mapping.digest}, CreatedAt: time.Date(2026, 10, 27, 0, 0, 0, 0, time.UTC), MaxDocuments: 100} checks := 0 ready := func(context.Context) error { checks++; return nil } result, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req) if !writer.running.Load() { t.Fatal("test writer left stopped") } if mode != "success" { if err == nil { t.Fatal("invalid candidate succeeded") } if (mode == "initial-busy" || mode == "other-library-busy") && writer.stops != 0 { t.Fatal("busy source stopped") } return } if err != nil || result.State != "succeeded" { t.Fatalf("capture=%+v err=%v", result, err) } saved, err := archive.Load(result.SnapshotID) if err != nil || saved.Scope != "internal" || checks != 2 { t.Fatalf("archive=%+v checks=%d err=%v", saved, checks, err) } db.tx.pending = 1 replay, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req) if err != nil || replay.SnapshotID != result.SnapshotID || writer.stops != 1 { t.Fatal("sealed replay consulted current source") } req.Scope = "external" if got, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req); err == nil || got.State == "succeeded" { t.Fatal("conflicting replay returned a successful candidate") } req.Scope = "internal" if err := os.Remove(archiveRoot + "/versions/" + result.SnapshotID + ".json"); err != nil { t.Fatal(err) } if got, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req); err == nil || got.State == "succeeded" { t.Fatal("missing archive returned a successful candidate") } // Simulate a durable successful job created before the policy marker. // Replaying it must not silently upgrade its validation contract. saved.ValidationPolicy = "" legacyID, err := archive.Seal(saved) if err != nil { t.Fatal(err) } req.JobID = "legacy-capture-job" if _, err := freeze.RunPrepared(context.Background(), req.JobID, func(context.Context) error { return nil }, func(context.Context) (string, error) { return legacyID, nil }); err != nil { t.Fatal(err) } stops, previousChecks := writer.stops, checks if got, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req); err == nil || got.State == "succeeded" || got.SnapshotID != "" { t.Fatal("legacy policy replay silently upgraded to artifact acceptance") } if writer.stops != stops || checks != previousChecks { t.Fatal("policy conflict touched current source") } }) } }