package v071 import ( "bytes" "context" "encoding/json" "errors" "sort" "strings" "knowledge-governance/internal/libraryrelease" "knowledge-governance/internal/librarysnapshot" ) func archiveIndexMap(archive *librarysnapshot.Archive, manifest librarysnapshot.Manifest) (map[string]json.RawMessage, error) { out := map[string]json.RawMessage{} for _, part := range manifest.Parts { if part.Kind != "index" { continue } reader, err := archive.OpenPart(part) if err != nil { return nil, err } docs, err := librarysnapshot.ReadIndexDocuments(reader) reader.Close() if err != nil { return nil, err } for _, doc := range docs { if doc.ID == "" { return nil, errors.New("empty index ID") } if _, ok := out[doc.ID]; ok { return nil, errors.New("duplicate index ID") } raw, err := canonicalJSON(doc.Source) if err != nil { return nil, err } out[doc.ID] = raw } } return out, nil } func targetIndexSource(raw json.RawMessage, tenant uint64, kb string, withTenant bool) (json.RawMessage, error) { var source map[string]json.RawMessage if json.Unmarshal(raw, &source) != nil || source == nil { return nil, errors.New("index source must be object") } var content string if json.Unmarshal(source["content"], &content) != nil || strings.HasPrefix(content, indexClaimPrefix) { return nil, errors.New("index source lacks string content or contains an unfinished transient claim") } if withTenant { source["tenant_id"], _ = json.Marshal(tenant) } source["knowledge_base_id"], _ = json.Marshal(kb) encoded, err := json.Marshal(source) if err != nil { return nil, err } return canonicalJSON(encoded) } func (t *ProductionLibraryTarget) planIndex(ctx context.Context, archive *librarysnapshot.Archive, source librarysnapshot.Manifest, set *libraryrelease.WriteSet) error { backup, err := archive.Load(set.BackupSnapshotID) if err != nil { return err } before, err := archiveIndexMap(archive, backup) if err != nil { return err } after, err := archiveIndexMap(archive, source) if err != nil { return err } after, err = t.remapIndexMap(after) if err != nil { return err } ids := map[string]bool{} for id := range before { ids[id] = true } for id := range after { ids[id] = true } sorted := make([]string, 0, len(ids)) for id := range ids { sorted = append(sorted, id) } sort.Strings(sorted) docs := make([]librarysnapshot.IndexDocument, 0, len(ids)) for _, id := range sorted { docs = append(docs, librarysnapshot.IndexDocument{ID: id}) } guards, err := t.search.indexWriteGuards(ctx, docs, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID, NoTenantField: !t.schema.IndexTenantField}) if err != nil { return err } for _, id := range sorted { g := guards[id] expected, existed := before[id] if existed != g.Found { return errors.New("index before-image no longer covers exact write set") } if existed { raw, err := canonicalJSON(g.Source) if err != nil || !bytes.Equal(raw, expected) { return errors.New("index changed since before-image") } } var wanted json.RawMessage if raw, ok := after[id]; ok { wanted, err = targetIndexSource(raw, t.scope.TenantID, t.scope.KnowledgeBaseID, t.schema.IndexTenantField) if err != nil { return err } } set.Index = append(set.Index, libraryrelease.IndexWrite{Index: t.search.index, ID: id, Before: expected, SeqNo: g.SeqNo, PrimaryTerm: g.PrimaryTerm, After: wanted}) } return nil } func (t *ProductionLibraryTarget) validateIndexPlanBinding(archive *librarysnapshot.Archive, source, backup librarysnapshot.Manifest, set libraryrelease.WriteSet) error { before, err := archiveIndexMap(archive, backup) if err != nil { return err } after, err := archiveIndexMap(archive, source) if err != nil { return err } after, err = t.remapIndexMap(after) if err != nil { return err } ids := map[string]bool{} for id := range before { ids[id] = true } for id := range after { ids[id] = true } if len(ids) != len(set.Index) { return errors.New("index plan coverage mismatch") } for _, change := range set.Index { if !ids[change.ID] || change.Index != t.search.index { return errors.New("index plan physical identity mismatch") } delete(ids, change.ID) var wanted json.RawMessage if raw, ok := after[change.ID]; ok { wanted, err = targetIndexSource(raw, set.TenantID, set.KnowledgeBaseID, t.schema.IndexTenantField) if err != nil { return err } } if !bytes.Equal(before[change.ID], change.Before) || !bytes.Equal(wanted, change.After) { return errors.New("index plan bytes differ from immutable archives") } if len(change.Before) > 0 && (change.SeqNo == nil || change.PrimaryTerm == nil) { return errors.New("existing index document lacks CAS identity") } if len(change.Before) == 0 && (change.SeqNo != nil || change.PrimaryTerm != nil) { return errors.New("absent index document has invalid CAS identity") } } return nil }