fix(snapshot): 按表解析状态并保留完整Wiki内容

This commit is contained in:
姚章浩
2026-10-28 00:57:43 +08:00
parent ce2f90c1c5
commit 57d259076d
4 changed files with 108 additions and 3 deletions
+7
View File
@@ -1,5 +1,12 @@
# Changelog
## 开发快照 snapshot/v0.1.99 - 2026-10-28
### Fixed
- 修复整库归档将 Wiki 页面与问题记录的字符串状态误按分块整数状态解析的问题;分块状态仅在分块表中解析,Wiki 状态和正文原样保留。
- 候选捕获回归改用非空 Wiki 页面与问题记录,防止空表夹具漏检。本次仍不代表完整同步恢复业务已交付。
## 开发快照 snapshot/v0.1.98 - 2026-10-28
### Fixed
@@ -116,7 +116,7 @@ type libraryRow struct {
OwnerType string `json:"owner_type"`
OwnerID string `json:"owner_id"`
Content string `json:"content"`
Status int `json:"status"`
ChunkStatus int `json:"-"`
}
func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es SearchSnapshot) error {
@@ -152,6 +152,17 @@ func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es
if json.Unmarshal(raw, &row) != nil {
return errors.New("invalid library content row")
}
// Status is table-specific: chunks use integers, Wiki pages/issues
// use strings. Decode chunk metadata only for its owning table.
if table.Name == "chunks" {
var metadata struct {
Status int `json:"status"`
}
if json.Unmarshal(raw, &metadata) != nil {
return errors.New("invalid library chunk status")
}
row.ChunkStatus = metadata.Status
}
if table.Name == "knowledge_tag_relations" {
if !validSearchIdentifier(row.KnowledgeID) || !validSearchIdentifier(row.TagID) {
return errors.New("invalid library tag relation")
@@ -241,7 +252,7 @@ func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es
// records, so compare identities rather than counts. Stored-only parent
// chunks intentionally have no index and must not be rejected as missing.
for id, chunk := range tables["chunks"] {
if chunk.Status == 2 && !indexedChunks[id] {
if chunk.ChunkStatus == 2 && !indexedChunks[id] {
return errors.New("missing indexed library chunk")
}
}
@@ -88,7 +88,14 @@ func libraryCandidateFixture() (PostgresSnapshot, SearchSnapshot, *libraryCandid
newTableSnapshot("resources", []json.RawMessage{json.RawMessage(`{"id":"res-safe","tenant_id":10000,"handle":"ABCDEFGHIJKLMNOPQRSTUV","provider":"oss","physical_path":"oss://fixture/source.txt","size":4}`)}),
}
for _, name := range []string{"resource_bindings", "knowledge_tags", "knowledge_tag_relations", "wiki_folders", "wiki_pages", "wiki_page_issues", "wiki_log_entries"} {
tables = append(tables, newTableSnapshot(name, nil))
var rows []json.RawMessage
switch name {
case "wiki_pages":
rows = []json.RawMessage{json.RawMessage(`{"id":"wiki-safe","tenant_id":10000,"knowledge_base_id":"kb-safe","status":"published","content":"complete wiki body"}`)}
case "wiki_page_issues":
rows = []json.RawMessage{json.RawMessage(`{"id":"issue-safe","tenant_id":10000,"knowledge_base_id":"kb-safe","status":"open"}`)}
}
tables = append(tables, newTableSnapshot(name, rows))
}
es := SearchSnapshot{MappingSHA256: shaBytes([]byte("mapping")), Documents: []SearchDocument{{ID: "es-safe", Source: json.RawMessage(`{"knowledge_base_id":"kb-safe","knowledge_id":"doc-safe","chunk_id":"chunk-safe","content":"chunk"}`)}}}
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
@@ -0,0 +1,80 @@
package v071
import (
"context"
"encoding/json"
"io"
"os"
"testing"
"time"
"knowledge-governance/internal/librarysnapshot"
)
func TestLibraryCandidateArchivesWikiStringStatuses(t *testing.T) {
for _, tc := range []struct{ table, status string }{
{"wiki_pages", "draft"}, {"wiki_pages", "published"}, {"wiki_pages", "archived"},
{"wiki_page_issues", "open"}, {"wiki_page_issues", "resolved"},
} {
t.Run(tc.table+"/"+tc.status, func(t *testing.T) {
pg, es, objects := libraryCandidateFixture()
raw := json.RawMessage(`{"id":"wiki-safe","tenant_id":10000,"knowledge_base_id":"kb-safe","content":"complete wiki body","status":"` + tc.status + `"}`)
for i := range pg.Tables {
if pg.Tables[i].Name == tc.table {
pg.Tables[i] = newTableSnapshot(tc.table, []json.RawMessage{raw})
}
}
root := t.TempDir()
if err := os.Chmod(root, 0700); err != nil {
t.Fatal(err)
}
archive, err := librarysnapshot.Open(root, 1024*1024)
if err != nil {
t.Fatal(err)
}
defer archive.Close()
contract := librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024, IndexMappingSHA256: es.MappingSHA256}
id, err := ArchiveLibraryCandidate(context.Background(), archive, "internal", PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, contract, pg, es, objects, time.Now().UTC())
if err != nil {
t.Fatalf("valid Wiki string status rejected: %v", err)
}
saved, err := archive.Load(id)
if err != nil {
t.Fatal(err)
}
found := false
for _, part := range saved.Parts {
if part.Name != tc.table {
continue
}
found = true
r, e := archive.OpenPart(part)
if e != nil {
t.Fatal(e)
}
data, e := io.ReadAll(r)
r.Close()
if e != nil {
t.Fatal(e)
}
var rows []json.RawMessage
if e = json.Unmarshal(data, &rows); e != nil {
t.Fatal(e)
}
if len(rows) != 1 {
t.Fatalf("wiki rows=%d", len(rows))
}
var row map[string]any
if e = json.Unmarshal(rows[0], &row); e != nil {
t.Fatal(e)
}
if row["status"] != tc.status || row["content"] != "complete wiki body" {
t.Fatal("Wiki status/body changed in archive")
}
}
if !found {
t.Fatal("Wiki part missing")
}
})
}
}