Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2fc6dc068b | |||
| fe2e7ac4f4 | |||
| c4759bb61c |
@@ -1,5 +1,26 @@
|
||||
# Changelog
|
||||
|
||||
## 开发快照 snapshot/v0.1.103 - 2026-10-28
|
||||
|
||||
### Fixed
|
||||
|
||||
- 整库索引校验支持从分块持久元数据核验生成问题:严格匹配分块、问题 source_id 与问题原文,拒绝未知、过期或内容不一致的索引。
|
||||
- 分开核验正文与生成问题的覆盖,问题记录不能掩盖正文索引缺失;元数据中已记录的问题缺少索引同样阻断。未放宽临时章节上下文或未知索引变换的检查。
|
||||
|
||||
## 开发快照 snapshot/v0.1.102 - 2026-10-28
|
||||
|
||||
### Fixed
|
||||
|
||||
- 阻止旧式 status=0 普通分块缺失索引时被误判完整;仅明确的上下文父分块允许没有索引,显式标记已索引的分块仍须存在。
|
||||
- 缺少可核验的无索引策略时返回覆盖未证实,不推断索引已禁用;Wiki-only 无索引库的完整支持仍待实现,不因此宣称整库验收通过。
|
||||
|
||||
## 开发快照 snapshot/v0.1.101 - 2026-10-28
|
||||
|
||||
### Fixed
|
||||
|
||||
- 修复正常正文索引因标题前缀或正文首尾空白处理而被整库归档误拒的问题;仅按已持久化的标题与正文重建确定性内容,保持逐字核验,不使用后缀包含匹配。
|
||||
- 无法由持久数据核验的临时章节上下文、非正文类型的变换及未知内容仍阻断;不将本次兼容修复视为完整索引或整库业务验收通过。
|
||||
|
||||
## 开发快照 snapshot/v0.1.100 - 2026-10-28
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -105,18 +105,40 @@ func ArchiveLibraryCandidate(ctx context.Context, archive *librarysnapshot.Archi
|
||||
}
|
||||
|
||||
type libraryRow struct {
|
||||
ID string `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
ChunkID string `json:"chunk_id"`
|
||||
TagID string `json:"tag_id"`
|
||||
ResourceID string `json:"resource_id"`
|
||||
Handle string `json:"handle"`
|
||||
OwnerType string `json:"owner_type"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Content string `json:"content"`
|
||||
ChunkStatus int `json:"-"`
|
||||
ID string `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
ChunkID string `json:"chunk_id"`
|
||||
TagID string `json:"tag_id"`
|
||||
ResourceID string `json:"resource_id"`
|
||||
Handle string `json:"handle"`
|
||||
OwnerType string `json:"owner_type"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Content string `json:"content"`
|
||||
Title string `json:"title"`
|
||||
ChunkType string `json:"chunk_type"`
|
||||
ChunkStatus int `json:"-"`
|
||||
SourceID string `json:"source_id"`
|
||||
Questions map[string]string `json:"-"`
|
||||
}
|
||||
|
||||
func libraryIndexContentMatches(indexContent string, chunk, knowledge libraryRow) bool {
|
||||
if indexContent == chunk.Content {
|
||||
return true
|
||||
}
|
||||
if chunk.ChunkType != "text" {
|
||||
return false
|
||||
}
|
||||
// v0.7.1 text indexing trims the body and may prepend the title. Only
|
||||
// reconstruct transformations backed by archived fields. ContextHeader is
|
||||
// transient upstream; suffix matching would silently accept stale content.
|
||||
body := strings.TrimSpace(chunk.Content)
|
||||
if indexContent == body {
|
||||
return true
|
||||
}
|
||||
title := strings.TrimSpace(knowledge.Title)
|
||||
return title != "" && indexContent == title+"\n"+body
|
||||
}
|
||||
|
||||
func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es SearchSnapshot) error {
|
||||
@@ -156,12 +178,26 @@ func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es
|
||||
// use strings. Decode chunk metadata only for its owning table.
|
||||
if table.Name == "chunks" {
|
||||
var metadata struct {
|
||||
Status int `json:"status"`
|
||||
Status int `json:"status"`
|
||||
Metadata struct {
|
||||
GeneratedQuestions []struct {
|
||||
ID string `json:"id"`
|
||||
Question string `json:"question"`
|
||||
} `json:"generated_questions"`
|
||||
} `json:"metadata"`
|
||||
}
|
||||
if json.Unmarshal(raw, &metadata) != nil {
|
||||
return errors.New("invalid library chunk status")
|
||||
}
|
||||
row.ChunkStatus = metadata.Status
|
||||
row.Questions = map[string]string{}
|
||||
for _, question := range metadata.Metadata.GeneratedQuestions {
|
||||
sourceID := row.ID + "-" + question.ID
|
||||
if row.ChunkType != "text" || !validSearchIdentifier(question.ID) || strings.TrimSpace(question.Question) == "" || row.Questions[sourceID] != "" {
|
||||
return errors.New("invalid library generated question metadata")
|
||||
}
|
||||
row.Questions[sourceID] = question.Question
|
||||
}
|
||||
}
|
||||
if table.Name == "knowledge_tag_relations" {
|
||||
if !validSearchIdentifier(row.KnowledgeID) || !validSearchIdentifier(row.TagID) {
|
||||
@@ -233,6 +269,7 @@ func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
indexedChunks := map[string]bool{}
|
||||
indexedQuestions := map[string]map[string]bool{}
|
||||
for _, document := range es.Documents {
|
||||
var row libraryRow
|
||||
if json.Unmarshal(document.Source, &row) != nil || !validSearchIdentifier(document.ID) || seen[document.ID] {
|
||||
@@ -240,18 +277,43 @@ func validateLibraryCandidate(identity PostgresIdentity, pg PostgresSnapshot, es
|
||||
}
|
||||
seen[document.ID] = true
|
||||
chunk, ok := tables["chunks"][row.ChunkID]
|
||||
if !ok || row.KnowledgeID != chunk.KnowledgeID || row.Content != chunk.Content {
|
||||
if !ok || row.KnowledgeID != chunk.KnowledgeID {
|
||||
return errors.New("library index does not match database chunks")
|
||||
}
|
||||
if !searchHitMatches(document.Source, SearchIdentity{TenantID: identity.TenantID, KnowledgeBaseID: identity.KnowledgeBaseID, KnowledgeID: row.KnowledgeID}) {
|
||||
return errors.New("cross-scope library index")
|
||||
}
|
||||
indexedChunks[row.ChunkID] = true
|
||||
if row.SourceID != "" && row.SourceID != row.ChunkID {
|
||||
question, exists := chunk.Questions[row.SourceID]
|
||||
if !exists || question != row.Content {
|
||||
return errors.New("library question index does not match persisted metadata")
|
||||
}
|
||||
if indexedQuestions[row.ChunkID] == nil {
|
||||
indexedQuestions[row.ChunkID] = map[string]bool{}
|
||||
}
|
||||
indexedQuestions[row.ChunkID][row.SourceID] = true
|
||||
} else {
|
||||
if !libraryIndexContentMatches(row.Content, chunk, tables["knowledges"][chunk.KnowledgeID]) {
|
||||
return errors.New("library index does not match database chunks")
|
||||
}
|
||||
indexedChunks[row.ChunkID] = true
|
||||
}
|
||||
}
|
||||
// v0.7.1 ChunkStatusIndexed is 2. One chunk can have several search
|
||||
// records, so compare identities rather than counts. Stored-only parent
|
||||
// chunks intentionally have no index and must not be rejected as missing.
|
||||
// Text ingestion also leaves indexed chunks at the legacy default status
|
||||
// zero. Without an archived no-index policy, absence is unverified, not
|
||||
// proof that indexing was disabled. Parent chunks are context-only.
|
||||
for id, chunk := range tables["chunks"] {
|
||||
for sourceID := range chunk.Questions {
|
||||
if !indexedQuestions[id][sourceID] {
|
||||
return errors.New("missing library generated question index")
|
||||
}
|
||||
}
|
||||
if chunk.ChunkStatus == 0 && chunk.ChunkType != "parent_text" && !indexedChunks[id] {
|
||||
return errors.New("unverified legacy library chunk index coverage")
|
||||
}
|
||||
if chunk.ChunkStatus == 2 && !indexedChunks[id] {
|
||||
return errors.New("missing indexed library chunk")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package v071
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLibraryCandidateValidatesPersistedTextIndexTransform(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, kind, body, index string
|
||||
valid bool
|
||||
}{
|
||||
{"current title prefix", "text", "\n chunk \t", "Document title\nchunk", true},
|
||||
{"legacy raw body", "text", "\n chunk \t", "\n chunk \t", true},
|
||||
{"trimmed body", "text", "\n chunk \t", "chunk", true},
|
||||
{"wrong title", "text", "chunk", "Other title\nchunk", false},
|
||||
{"missing body", "text", "chunk", "Document title\n", false},
|
||||
{"different body", "text", "chunk", "Document title\nchanged", false},
|
||||
{"unverifiable context header", "text", "chunk", "Document title\nTransient heading\n\nchunk", false},
|
||||
{"nontext transform not assumed", "faq", "chunk", "Document title\nchunk", false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pg, es, _ := libraryCandidateFixture()
|
||||
k, _ := json.Marshal(map[string]any{"id": "doc-safe", "tenant_id": 10000, "knowledge_base_id": "kb-safe", "title": " Document title\n"})
|
||||
c, _ := json.Marshal(map[string]any{"id": "chunk-safe", "tenant_id": 10000, "knowledge_base_id": "kb-safe", "knowledge_id": "doc-safe", "content": tc.body, "chunk_type": tc.kind, "status": 2})
|
||||
x, _ := json.Marshal(map[string]any{"knowledge_base_id": "kb-safe", "knowledge_id": "doc-safe", "chunk_id": "chunk-safe", "content": tc.index})
|
||||
pg.Tables[0] = newTableSnapshot("knowledges", []json.RawMessage{k})
|
||||
pg.Tables[1] = newTableSnapshot("chunks", []json.RawMessage{c})
|
||||
es.Documents[0].Source = x
|
||||
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
|
||||
err := validateLibraryCandidate(PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, pg, es)
|
||||
if tc.valid && err != nil {
|
||||
t.Fatalf("valid indexed text refused: %v", err)
|
||||
}
|
||||
if !tc.valid && err == nil {
|
||||
t.Fatal("unverifiable index content accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package v071
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLibraryCandidateLegacyChunkIndexCoverage(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, kind string
|
||||
status int
|
||||
indexed, valid bool
|
||||
}{
|
||||
{"legacy text missing", "text", 0, false, false},
|
||||
{"legacy text indexed", "text", 0, true, true},
|
||||
{"legacy unknown type missing", "", 0, false, false},
|
||||
{"context parent not indexed", "parent_text", 0, false, true},
|
||||
{"stored parent not indexed", "parent_text", 1, false, true},
|
||||
{"indexed parent missing", "parent_text", 2, false, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pg, es, _ := libraryCandidateFixture()
|
||||
raw, err := json.Marshal(map[string]any{"id": "chunk-safe", "tenant_id": 10000, "knowledge_base_id": "kb-safe", "knowledge_id": "doc-safe", "content": "chunk", "status": tc.status, "chunk_type": tc.kind})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pg.Tables[1] = newTableSnapshot("chunks", []json.RawMessage{raw})
|
||||
if !tc.indexed {
|
||||
es.Documents = nil
|
||||
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
|
||||
}
|
||||
err = validateLibraryCandidate(PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, pg, es)
|
||||
if tc.valid && err != nil {
|
||||
t.Fatalf("valid chunk rejected: %v", err)
|
||||
}
|
||||
if !tc.valid && err == nil {
|
||||
t.Fatal("missing legacy index escaped completeness validation")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package v071
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLibraryCandidateGeneratedQuestionIndex(t *testing.T) {
|
||||
for _, mode := range []string{"complete", "wrong-id", "wrong-content", "missing-question", "missing-body", "foreign-source", "question-as-body"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
pg, es, _ := libraryCandidateFixture()
|
||||
pg.Tables[1] = newTableSnapshot("chunks", []json.RawMessage{json.RawMessage(`{"id":"chunk-safe","tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-safe","content":"chunk","chunk_type":"text","status":0,"metadata":{"generated_questions":[{"id":"q1","question":"How does it work?"}]}}`)})
|
||||
questionID, content := "chunk-safe-q1", "How does it work?"
|
||||
if mode == "wrong-id" {
|
||||
questionID = "chunk-safe-q2"
|
||||
}
|
||||
if mode == "wrong-content" {
|
||||
content = "Different question"
|
||||
}
|
||||
if mode == "foreign-source" {
|
||||
questionID = "foreign-q1"
|
||||
}
|
||||
if mode == "question-as-body" {
|
||||
content = "chunk"
|
||||
}
|
||||
raw, _ := json.Marshal(map[string]any{"chunk_id": "chunk-safe", "knowledge_id": "doc-safe", "knowledge_base_id": "kb-safe", "source_id": questionID, "content": content})
|
||||
q := SearchDocument{ID: "question-index", Source: raw}
|
||||
if mode == "missing-body" {
|
||||
es.Documents = []SearchDocument{q}
|
||||
} else if mode != "missing-question" {
|
||||
es.Documents = append(es.Documents, q)
|
||||
}
|
||||
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
|
||||
err := validateLibraryCandidate(PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, pg, es)
|
||||
if mode == "complete" && err != nil {
|
||||
t.Fatalf("valid persisted question rejected: %v", err)
|
||||
}
|
||||
if mode != "complete" && err == nil {
|
||||
t.Fatal("incomplete or inconsistent question index accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user