Repository navigation
Expand file tree
/
Copy pathapi_knowledge.go
More file actions
323 lines (293 loc) · 11.7 KB
/
Copy pathapi_knowledge.go
File metadata and controls
323 lines (293 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package marksplice
import (
"fmt"
"math"
"strings"
"unicode/utf8"
)
// KnowledgeAlias is one exact, syntax-independent alternate name for a document.
// Marksplice does not normalize, parse, or derive aliases from Markdown or metadata source.
type KnowledgeAlias string
// KnowledgeTag is one exact, syntax-independent classification value for a document.
// Matching is byte-exact and case-sensitive; Marksplice performs no normalization.
type KnowledgeTag string
// KnowledgeDocument supplies caller-owned semantic metadata for one document already
// present in the explicit DocumentGraph. Graph documents omitted from the input simply
// have no knowledge metadata; this value never changes the underlying document snapshot.
type KnowledgeDocument struct {
Document DocumentKey
Aliases []KnowledgeAlias
Tags []KnowledgeTag
References []DocumentKey
}
// KnowledgeReference is one immutable caller-declared logical document relationship.
// It has no source offset because the knowledge layer does not infer Markdown or metadata syntax.
type KnowledgeReference struct {
sourceDocument DocumentKey
targetDocument DocumentKey
}
// SourceDocument returns the caller-defined logical source document key.
func (r KnowledgeReference) SourceDocument() DocumentKey { return r.sourceDocument }
// TargetDocument returns the caller-defined logical target document key.
func (r KnowledgeReference) TargetDocument() DocumentKey { return r.targetDocument }
type knowledgeDocumentState struct {
aliases []KnowledgeAlias
tags []KnowledgeTag
}
// KnowledgeIndex is an immutable syntax-independent semantic overlay on one DocumentGraph.
// It retains no parser, resolver callback, filesystem/network authority, or source mutation capability.
type KnowledgeIndex struct {
graph *DocumentGraph
documents map[DocumentKey]knowledgeDocumentState
aliasOwners map[KnowledgeAlias]DocumentKey
references []KnowledgeReference
outgoing map[DocumentKey]adjacencyRange
backlinks map[DocumentKey][]int
}
// BuildKnowledgeIndex builds syntax-independent aliases, tags, and logical references
// over an already-authorized document graph. Metadata may be supplied for any subset of
// graph documents. Every logical reference target must already belong to the graph;
// aliases never resolve or discover additional documents.
func BuildKnowledgeIndex(graph *DocumentGraph, documents []KnowledgeDocument) (*KnowledgeIndex, error) {
if graph == nil {
return nil, fmt.Errorf("%w: nil document graph", ErrInvalidKnowledge)
}
states := make(map[DocumentKey]knowledgeDocumentState, len(documents))
aliasOwners := make(map[KnowledgeAlias]DocumentKey)
referenceTargets := make(map[DocumentKey][]DocumentKey, len(documents))
referenceCount := 0
referenceSourceCount := 0
for position, document := range documents {
if !graph.hasDocument(document.Document) {
return nil, fmt.Errorf("%w: metadata document %q at position %d is outside the graph", ErrInvalidKnowledge, document.Document, position)
}
if _, exists := states[document.Document]; exists {
return nil, fmt.Errorf("%w: duplicate metadata for document %q", ErrInvalidKnowledge, document.Document)
}
state, references, err := validateKnowledgeDocument(document, graph, aliasOwners)
if err != nil {
return nil, err
}
if len(references) > math.MaxInt-referenceCount {
return nil, fmt.Errorf("%w: too many logical references", ErrInvalidKnowledge)
}
referenceCount += len(references)
if len(references) != 0 {
referenceSourceCount++
}
states[document.Document] = state
referenceTargets[document.Document] = references
}
index := &KnowledgeIndex{
graph: graph,
documents: states,
aliasOwners: aliasOwners,
references: make([]KnowledgeReference, 0, referenceCount),
outgoing: make(map[DocumentKey]adjacencyRange, referenceSourceCount),
backlinks: make(map[DocumentKey][]int),
}
for _, source := range graph.keys {
index.addReferences(source, referenceTargets[source])
}
return index, nil
}
func validateKnowledgeDocument(document KnowledgeDocument, graph *DocumentGraph, aliasOwners map[KnowledgeAlias]DocumentKey) (knowledgeDocumentState, []DocumentKey, error) {
state := knowledgeDocumentState{
aliases: append([]KnowledgeAlias(nil), document.Aliases...),
tags: append([]KnowledgeTag(nil), document.Tags...),
}
for _, alias := range state.aliases {
if !validKnowledgeText(string(alias)) {
return knowledgeDocumentState{}, nil, fmt.Errorf("%w: document %q has invalid alias", ErrInvalidKnowledge, document.Document)
}
if graph.hasDocument(DocumentKey(alias)) {
return knowledgeDocumentState{}, nil, fmt.Errorf("%w: alias %q collides with a canonical document key", ErrInvalidKnowledge, alias)
}
if owner, exists := aliasOwners[alias]; exists {
return knowledgeDocumentState{}, nil, fmt.Errorf("%w: alias %q is already owned by %q", ErrInvalidKnowledge, alias, owner)
}
aliasOwners[alias] = document.Document
}
if err := validateKnowledgeTags(document.Document, state.tags); err != nil {
return knowledgeDocumentState{}, nil, err
}
// References are consumed synchronously into owned scalar index entries.
references := document.References
if err := validateKnowledgeReferences(document.Document, references, graph); err != nil {
return knowledgeDocumentState{}, nil, err
}
return state, references, nil
}
func validateKnowledgeTags(document DocumentKey, tags []KnowledgeTag) error {
seen := make(map[KnowledgeTag]struct{}, len(tags))
for _, tag := range tags {
if !validKnowledgeText(string(tag)) {
return fmt.Errorf("%w: document %q has invalid tag", ErrInvalidKnowledge, document)
}
if _, exists := seen[tag]; exists {
return fmt.Errorf("%w: document %q repeats tag %q", ErrInvalidKnowledge, document, tag)
}
seen[tag] = struct{}{}
}
return nil
}
func validateKnowledgeReferences(document DocumentKey, references []DocumentKey, graph *DocumentGraph) error {
seen := make(map[DocumentKey]struct{}, len(references))
for _, target := range references {
if !graph.hasDocument(target) {
return fmt.Errorf("%w: document %q references unknown target %q", ErrInvalidKnowledge, document, target)
}
if _, exists := seen[target]; exists {
return fmt.Errorf("%w: document %q repeats logical reference to %q", ErrInvalidKnowledge, document, target)
}
seen[target] = struct{}{}
}
return nil
}
func validKnowledgeText(value string) bool {
return value != "" && utf8.ValidString(value) && !strings.ContainsAny(value, "\x00\r\n")
}
func (k *KnowledgeIndex) addReferences(source DocumentKey, targets []DocumentKey) {
if len(targets) == 0 {
return
}
// Build visits each source once, keeping its references contiguous.
start := len(k.references)
for _, target := range targets {
index := len(k.references)
k.references = append(k.references, KnowledgeReference{sourceDocument: source, targetDocument: target})
k.backlinks[target] = append(k.backlinks[target], index)
}
k.outgoing[source] = adjacencyRange{start: start, end: len(k.references)}
}
func (k *KnowledgeIndex) outgoingReferences(source DocumentKey) []KnowledgeReference {
range_ := k.outgoing[source]
return k.references[range_.start:range_.end]
}
func (k *KnowledgeIndex) hasDocument(key DocumentKey) bool {
return k != nil && k.graph != nil && k.graph.hasDocument(key)
}
// Aliases returns exact aliases for key in caller-provided order. The returned slice is caller-owned.
func (k *KnowledgeIndex) Aliases(key DocumentKey) ([]KnowledgeAlias, bool) {
if !k.hasDocument(key) {
return nil, false
}
return append([]KnowledgeAlias(nil), k.documents[key].aliases...), true
}
// Tags returns exact tags for key in caller-provided order. The returned slice is caller-owned.
func (k *KnowledgeIndex) Tags(key DocumentKey) ([]KnowledgeTag, bool) {
if !k.hasDocument(key) {
return nil, false
}
return append([]KnowledgeTag(nil), k.documents[key].tags...), true
}
// ResolveAlias resolves one exact globally unique alias to an existing graph document.
// Canonical DocumentKey values are not aliases and should be queried through DocumentGraph.
func (k *KnowledgeIndex) ResolveAlias(alias KnowledgeAlias) (DocumentKey, bool) {
if k == nil {
return "", false
}
key, ok := k.aliasOwners[alias]
return key, ok
}
// DocumentsWithTag returns graph documents carrying the exact tag in original graph-input order.
func (k *KnowledgeIndex) DocumentsWithTag(tag KnowledgeTag) []DocumentKey {
if k == nil || k.graph == nil {
return nil
}
result := make([]DocumentKey, 0)
for _, key := range k.graph.keys {
if containsKnowledgeTag(k.documents[key].tags, tag) {
result = append(result, key)
}
}
if len(result) == 0 {
return nil
}
return result
}
func containsKnowledgeTag(tags []KnowledgeTag, target KnowledgeTag) bool {
for _, tag := range tags {
if tag == target {
return true
}
}
return false
}
// References returns all logical references in graph document order and per-document caller order.
func (k *KnowledgeIndex) References() []KnowledgeReference {
if k == nil || len(k.references) == 0 {
return nil
}
return append([]KnowledgeReference(nil), k.references...)
}
// ReferencesFrom returns logical references whose source is key in caller order.
func (k *KnowledgeIndex) ReferencesFrom(key DocumentKey) ([]KnowledgeReference, bool) {
if !k.hasDocument(key) {
return nil, false
}
return append([]KnowledgeReference(nil), k.outgoingReferences(key)...), true
}
// ReferencedBy returns logical references whose target is key in global reference order.
func (k *KnowledgeIndex) ReferencedBy(key DocumentKey) ([]KnowledgeReference, bool) {
if !k.hasDocument(key) {
return nil, false
}
return k.referencesAt(k.backlinks[key]), true
}
func (k *KnowledgeIndex) referencesAt(indices []int) []KnowledgeReference {
if len(indices) == 0 {
return nil
}
result := make([]KnowledgeReference, len(indices))
for position, index := range indices {
result[position] = k.references[index]
}
return result
}
// ReachableFrom returns every other document reachable through the union of resolved
// Markdown graph edges and caller-declared logical references. For each visited source,
// graph edges are considered first, then logical references. Results are deterministic
// breadth-first discovery order and self/cyclic paths never duplicate a document.
func (k *KnowledgeIndex) ReachableFrom(key DocumentKey) ([]DocumentKey, bool) {
if !k.hasDocument(key) {
return nil, false
}
visited := make(map[DocumentKey]bool, len(k.graph.documents))
visited[key] = true
queue := make([]DocumentKey, 1, len(k.graph.documents))
queue[0] = key
for head := 0; head < len(queue); head++ {
current := queue[head]
edges := k.graph.outgoingEdges(current)
for index := range edges {
appendUnvisitedDocument(edges[index].targetDocument, visited, &queue)
}
for _, reference := range k.outgoingReferences(current) {
appendUnvisitedDocument(reference.targetDocument, visited, &queue)
}
}
// The queue already has discovery order. Do not retain the excluded root.
queue[0] = ""
return queue[1:], true
}
// RelatedDocuments returns unique direct neighbors across both resolved Markdown graph
// edges and caller-declared logical references in original graph-input order. Self
// relationships are omitted.
func (k *KnowledgeIndex) RelatedDocuments(key DocumentKey) ([]DocumentKey, bool) {
if !k.hasDocument(key) {
return nil, false
}
related := make(map[DocumentKey]bool)
k.graph.markRelatedDocuments(key, related)
k.markRelatedKnowledgeDocuments(key, related)
return k.graph.orderedRelatedDocuments(key, related), true
}
func (k *KnowledgeIndex) markRelatedKnowledgeDocuments(key DocumentKey, related map[DocumentKey]bool) {
for _, reference := range k.outgoingReferences(key) {
related[reference.targetDocument] = true
}
for _, referenceIndex := range k.backlinks[key] {
related[k.references[referenceIndex].sourceDocument] = true
}
}