执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
package v071
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// This is the finite v071 structured-reference contract, not a heuristic that
|
||||
// treats arbitrary text/numbers as IDs. Historical log refs are display facts.
|
||||
func validateLibraryStructuredReferences(pg PostgresSnapshot) error {
|
||||
type row map[string]json.RawMessage
|
||||
tables := map[string]map[string]row{}
|
||||
for _, table := range pg.Tables {
|
||||
tables[table.Name] = map[string]row{}
|
||||
for _, raw := range table.Rows {
|
||||
var r row
|
||||
if e := json.Unmarshal(raw, &r); e != nil {
|
||||
return e
|
||||
}
|
||||
var id string
|
||||
json.Unmarshal(r["id"], &id)
|
||||
if id == "" {
|
||||
id = string(r["id"])
|
||||
}
|
||||
tables[table.Name][id] = r
|
||||
}
|
||||
}
|
||||
text := func(r row, key string) (string, error) {
|
||||
v := bytes.TrimSpace(r[key])
|
||||
if len(v) == 0 || bytes.Equal(v, []byte("null")) {
|
||||
return "", nil
|
||||
}
|
||||
var out string
|
||||
if e := json.Unmarshal(v, &out); e != nil {
|
||||
return "", fmt.Errorf("unsupported reference %s", key)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
ref := func(r row, key, table string, sameKnowledge bool) error {
|
||||
v, e := text(r, key)
|
||||
if e != nil || v == "" {
|
||||
return e
|
||||
}
|
||||
parent, ok := tables[table][v]
|
||||
if !ok {
|
||||
return fmt.Errorf("unresolved %s reference to %s", key, table)
|
||||
}
|
||||
if sameKnowledge {
|
||||
a, _ := text(r, "knowledge_id")
|
||||
b, _ := text(parent, "knowledge_id")
|
||||
if a != b {
|
||||
return fmt.Errorf("cross-parent %s reference", key)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
refs := func(r row, key, table string, legacyTitle bool) error {
|
||||
raw := bytes.TrimSpace(r[key])
|
||||
if len(raw) == 0 || bytes.Equal(raw, []byte("null")) {
|
||||
return nil
|
||||
}
|
||||
var ids []string
|
||||
if e := json.Unmarshal(raw, &ids); e != nil {
|
||||
return fmt.Errorf("unsupported %s reference representation", key)
|
||||
}
|
||||
for _, id := range ids {
|
||||
if legacyTitle {
|
||||
id = strings.SplitN(id, "|", 2)[0]
|
||||
}
|
||||
if _, ok := tables[table][id]; !ok {
|
||||
return fmt.Errorf("unresolved %s reference to %s", key, table)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, r := range tables["chunks"] {
|
||||
for _, key := range []string{"pre_chunk_id", "next_chunk_id", "parent_chunk_id"} {
|
||||
if e := ref(r, key, "chunks", true); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if e := ref(r, "tag_id", "knowledge_tags", false); e != nil {
|
||||
return e
|
||||
}
|
||||
for _, key := range []string{"relation_chunks", "indirect_relation_chunks"} {
|
||||
if e := refs(r, key, "chunks", false); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, r := range tables["wiki_folders"] {
|
||||
if e := ref(r, "parent_id", "wiki_folders", false); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
// Parent edges must be acyclic; pre/next chunk adjacency is intentionally not
|
||||
// run through this check because a normal bidirectional adjacency is a cycle.
|
||||
for _, kind := range []struct{ table, key string }{{"wiki_folders", "parent_id"}, {"chunks", "parent_chunk_id"}} {
|
||||
for id := range tables[kind.table] {
|
||||
seen := map[string]bool{}
|
||||
for id != "" {
|
||||
if seen[id] {
|
||||
return fmt.Errorf("cyclic %s reference", kind.key)
|
||||
}
|
||||
seen[id] = true
|
||||
next, e := text(tables[kind.table][id], kind.key)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
id = next
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, r := range tables["wiki_pages"] {
|
||||
if e := ref(r, "folder_id", "wiki_folders", false); e != nil {
|
||||
return e
|
||||
}
|
||||
if e := refs(r, "source_refs", "knowledges", true); e != nil {
|
||||
return e
|
||||
}
|
||||
if e := refs(r, "chunk_refs", "chunks", false); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
for _, r := range tables["wiki_log_entries"] {
|
||||
raw := bytes.TrimSpace(r["pages_affected"])
|
||||
if len(raw) == 0 || bytes.Equal(raw, []byte("null")) {
|
||||
continue
|
||||
}
|
||||
var items []json.RawMessage
|
||||
if e := json.Unmarshal(raw, &items); e != nil {
|
||||
return fmt.Errorf("unsupported Wiki log pages_affected")
|
||||
}
|
||||
shape := ""
|
||||
for _, item := range items {
|
||||
var slug string
|
||||
if json.Unmarshal(item, &slug) == nil {
|
||||
if shape == "object" {
|
||||
return fmt.Errorf("mixed Wiki log reference representations")
|
||||
}
|
||||
shape = "string"
|
||||
continue
|
||||
}
|
||||
var object map[string]json.RawMessage
|
||||
if e := json.Unmarshal(item, &object); e != nil || object == nil {
|
||||
return fmt.Errorf("unsupported Wiki log page reference")
|
||||
}
|
||||
if shape == "string" {
|
||||
return fmt.Errorf("mixed Wiki log reference representations")
|
||||
}
|
||||
shape = "object"
|
||||
if e := json.Unmarshal(object["slug"], &slug); e != nil {
|
||||
return fmt.Errorf("Wiki log reference lacks string slug")
|
||||
}
|
||||
for key, value := range object {
|
||||
if key != "slug" && key != "title" {
|
||||
return fmt.Errorf("unreviewed Wiki log reference field: %s", key)
|
||||
}
|
||||
var s string
|
||||
if e := json.Unmarshal(value, &s); e != nil {
|
||||
return fmt.Errorf("unsupported Wiki log reference %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user