Repository navigation
Expand file tree
/
Copy pathcompiler_topics.go
More file actions
505 lines (500 loc) · 18.7 KB
/
Copy pathcompiler_topics.go
File metadata and controls
505 lines (500 loc) · 18.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package knowledge
import (
"errors"
"sort"
"strings"
"time"
)
func reconcileTopics(learning, previous document, decisions []document) (document, error) {
state := clone(previous)
if state["concepts"] == nil {
state = document{"concepts": document{}, "redirects": document{}, "history": []document{}, "applied": []string{}}
}
concepts := object(state["concepts"])
prior := map[string]bool{}
for id := range concepts {
prior[id] = true
}
matched := map[string]bool{}
for _, subject := range items(learning["subjects"]) {
id := textValue(subject["id"])
matches := []document{}
for _, raw := range concepts {
c := object(raw)
if textValue(c["state"]) == "active" && contains(stringsValue(c["source_subjects"]), id) {
matches = append(matches, c)
}
}
if len(matches) == 0 {
for _, raw := range concepts {
c := object(raw)
cid := textValue(c["id"])
if prior[cid] && !matched[cid] && textValue(c["state"]) == "active" && strings.EqualFold(textValue(c["scope"]), textValue(subject["scope"])) && identity(sortedUnique(stringsValue(c["notes"]))) == identity(sortedUnique(stringsValue(subject["notes"]))) {
matches = append(matches, c)
}
}
}
if len(matches) == 1 && !flag(matches[0]["membership_locked"]) {
c := matches[0]
matched[textValue(c["id"])] = true
c["source_subjects"] = sortedUnique(append(stringsValue(c["source_subjects"]), id))
c["notes"] = sortedUnique(append(stringsValue(c["notes"]), stringsValue(subject["notes"])...))
} else if len(matches) == 0 && concepts[id] == nil {
concepts[id] = document{"id": id, "title": subject["title"], "scope": subject["scope"], "notes": stringsValue(subject["notes"]), "source_subjects": []string{id}, "aliases": stringsValue(subject["aliases"]), "state": "active"}
}
}
for _, decision := range decisions {
did := textValue(decision["id"])
if did == "" || textValue(decision["reviewed_by"]) == "" {
return nil, errors.New("authenticated topic decision ID and reviewer required")
}
if contains(stringsValue(state["applied"]), did) {
continue
}
before := clone(choose(state, "concepts", "redirects"))
op := textValue(decision["op"])
if op == "undo" {
history := items(state["history"])
if len(history) == 0 || history[len(history)-1]["id"] != decision["target"] {
return nil, errors.New("topic undo must name the latest structural decision")
}
last := object(history[len(history)-1]["before"])
state["concepts"] = clone(object(last["concepts"]))
state["redirects"] = clone(object(last["redirects"]))
state["history"] = history[:len(history)-1]
state["applied"] = append(stringsValue(state["applied"]), did)
concepts = object(state["concepts"])
continue
}
target := textValue(decision["target"])
if target == "" {
target = textValue(decision["into"])
}
current := object(concepts[target])
if textValue(current["state"]) != "active" {
return nil, errors.New("topic decision target must be active")
}
switch op {
case "rename":
title := strings.TrimSpace(textValue(decision["title"]))
if title == "" {
return nil, errors.New("topic title cannot be empty")
}
current["aliases"] = sortedUnique(append(stringsValue(current["aliases"]), textValue(current["title"])))
current["title"] = title
case "merge":
ids := stringsValue(decision["from"])
if len(ids) == 0 || contains(ids, target) || len(unique(ids)) != len(ids) {
return nil, errors.New("distinct topic merge sources required")
}
for _, id := range ids {
c := object(concepts[id])
if textValue(c["state"]) != "active" || !strings.EqualFold(textValue(c["scope"]), textValue(current["scope"])) {
return nil, errors.New("topic merge sources must be active in the same scope")
}
}
for _, id := range ids {
c := object(concepts[id])
for _, key := range []string{"notes", "source_subjects", "aliases"} {
current[key] = sortedUnique(append(stringsValue(current[key]), stringsValue(c[key])...))
}
current["aliases"] = sortedUnique(append(stringsValue(current["aliases"]), textValue(c["title"])))
c["state"] = "retired"
object(state["redirects"])[id] = []string{target}
}
case "split":
children := items(decision["children"])
members := []string{}
for _, child := range children {
members = append(members, stringsValue(child["notes"])...)
}
if len(children) < 2 || len(unique(members)) != len(members) || identity(sortedUnique(members)) != identity(sortedUnique(stringsValue(current["notes"]))) {
return nil, errors.New("topic split must partition all member notes without overlap")
}
childIDs := []string{}
for _, child := range children {
title := strings.TrimSpace(textValue(child["title"]))
notes := stringsValue(child["notes"])
if title == "" || len(notes) == 0 {
return nil, errors.New("topic split child requires title and member notes")
}
cid := identity([]any{"concept-child", target, sortedUnique(notes)})
if concepts[cid] != nil {
return nil, errors.New("topic split identity already exists")
}
concepts[cid] = merge(clone(current), document{"id": cid, "title": title, "notes": sortedUnique(notes), "aliases": []string{}, "membership_locked": true})
childIDs = append(childIDs, cid)
}
current["state"] = "retired"
object(state["redirects"])[target] = childIDs
default:
return nil, errors.New("unknown topic structural decision")
}
state["history"] = append(items(state["history"]), document{"id": did, "op": op, "reviewed_by": decision["reviewed_by"], "before": before})
state["applied"] = append(stringsValue(state["applied"]), did)
}
subjects := index(items(learning["subjects"]))
for _, raw := range concepts {
c := object(raw)
if textValue(c["state"]) != "active" || flag(c["membership_locked"]) {
continue
}
notes := stringsValue(c["notes"])
for _, sid := range stringsValue(c["source_subjects"]) {
notes = append(notes, stringsValue(subjects[sid]["notes"])...)
}
c["notes"] = sortedUnique(notes)
}
return state, nil
}
func publishTopics(vault, learning, registry document) (document, error) {
result := clone(vault)
subjects := index(items(learning["subjects"]))
original := index(items(vault["notes"]))
active := activeClaims(vault)
projected := []document{}
concepts := object(registry["concepts"])
ids := []string{}
for id := range concepts {
ids = append(ids, id)
}
sort.Strings(ids)
for _, id := range ids {
c := object(concepts[id])
if textValue(c["state"]) != "active" {
continue
}
parts := []document{}
for _, sid := range stringsValue(c["source_subjects"]) {
if s := subjects[sid]; s != nil {
parts = append(parts, s)
}
}
if len(parts) == 0 {
continue
}
notes := []string{}
for _, nid := range stringsValue(c["notes"]) {
if original[nid] != nil {
notes = append(notes, nid)
}
}
claims := []string{}
allowed := map[string]document{}
for _, nid := range notes {
for _, cid := range stringsValue(original[nid]["claims"]) {
claims = append(claims, cid)
allowed[cid] = active[cid]
}
}
sections := []document{}
questions, conflicts := []document{}, []document{}
seen := map[string]bool{}
pending := []string{}
for _, part := range parts {
for _, s := range items(part["sections"]) {
paragraphs := []document{}
for _, p := range items(s["paragraphs"]) {
key := identity([]any{p["text"], stringsValue(p["claims"]), items(p["evidence"])})
if !seen[key] && (!flag(c["membership_locked"]) || (len(stringsValue(p["claims"])) > 0 && subset(stringsValue(p["claims"]), allowed))) {
seen[key] = true
paragraphs = append(paragraphs, clone(p))
}
}
if len(paragraphs) > 0 {
sections = append(sections, merge(s, document{"paragraphs": paragraphs}))
}
}
for _, q := range items(part["questions"]) {
if !flag(c["membership_locked"]) || subset(stringsValue(q["claims"]), allowed) {
questions = append(questions, clone(q))
}
}
for _, conflict := range items(part["conflicts"]) {
if subset(stringsValue(conflict["claims"]), allowed) {
conflicts = append(conflicts, clone(conflict))
}
}
for _, cid := range stringsValue(part["editorial_pending_claims"]) {
if _, ok := allowed[cid]; ok {
pending = append(pending, cid)
}
}
}
state := parts[0]["state"]
if len(sections) == 0 && state != "needs_partition" {
state = "needs_evidence"
}
projected = append(projected, merge(clone(parts[0]), document{"id": id, "title": c["title"], "scope": c["scope"], "aliases": stringsValue(c["aliases"]), "notes": notes, "claims": unique(claims), "sections": sections, "questions": questions, "conflicts": conflicts, "state": state, "editorial_pending_claims": sortedUnique(pending)}))
}
result, err := projectSubjects(result, document{"subjects": projected})
if err != nil {
return nil, err
}
groups := map[string][]document{}
for _, n := range items(result["notes"]) {
if textValue(n["kind"]) == "subject" {
key := strings.ToLower(textValue(n["scope"]))
groups[key] = append(groups[key], n)
}
}
keys := []string{}
for key := range groups {
keys = append(keys, key)
}
sort.Strings(keys)
notes := items(result["notes"])
existing := index(notes)
for _, key := range keys {
members := groups[key]
if len(members) < 2 {
continue
}
scopes := []string{}
for _, n := range members {
scopes = append(scopes, textValue(n["scope"]))
}
sort.Strings(scopes)
scope := scopes[0]
id := identity([]any{"scope-map", strings.ToLower(scope)})
if existing[id] != nil {
return nil, errors.New("topic map identity collides with existing knowledge")
}
sort.Slice(members, func(i, j int) bool {
return strings.ToLower(textValue(members[i]["title"])) < strings.ToLower(textValue(members[j]["title"]))
})
links := []document{}
for _, member := range members {
links = append(links, document{"id": member["id"], "kind": "contains", "reason": "Subject in this reading collection; not a factual relationship"})
member["links"] = append(items(member["links"]), document{"id": id, "kind": "index", "reason": "Browse the wider topic collection"})
}
folder := append([]string{}, stringsValue(members[0]["folder"])...)
for _, member := range members[1:] {
path := stringsValue(member["folder"])
common := 0
for common < len(folder) && common < len(path) && folder[common] == path[common] {
common++
}
folder = folder[:common]
}
if len(folder) == 0 {
folder = []string{scope}
}
notes = append(notes, document{"id": id, "kind": "map", "title": scope + " — topic map", "scope": scope, "folder": folder, "claims": []string{}, "sections": []document{}, "links": links})
}
result["notes"] = notes
result["topic_registry"] = clone(registry)
return result, nil
}
func validSourceEvidence(evidence []document, sources document, requireHash bool) bool {
for _, e := range evidence {
source, ok := sources[textValue(e["source"])]
quote := textValue(e["quote"])
if !ok || quote == "" || !strings.Contains(textValue(object(source)["text"]), quote) {
return false
}
if requireHash || e["source_hash"] != nil {
if textValue(e["source_hash"]) != identity(object(source)["text"]) {
return false
}
}
}
return true
}
func evidenceBasis(sources document, evidence []document) document {
basis, _ := evidenceBasisWithFreshness(sources, evidence, nil, nil)
return basis
}
// Freshness is an optional host policy, never a truth score or a reason to
// rewrite claims. Supplying only one policy value leaves age unclassified.
func evidenceBasisWithFreshness(sources document, evidence []document, asOf *time.Time, staleAfter *time.Duration) (document, error) {
if asOf != nil && staleAfter != nil && *staleAfter < 0 {
return nil, errors.New("nonnegative freshness horizon required")
}
ids := []string{}
for _, e := range evidence {
if sources[textValue(e["source"])] != nil {
ids = append(ids, textValue(e["source"]))
}
}
kinds, primary, observed, roles := []string{}, []string{}, []string{}, []string{}
for _, id := range sortedUnique(ids) {
s := object(sources[id])
p := object(s["provenance"])
kind := textValue(p["kind"])
if kind == "" {
kind = "legacy_unknown"
if textValue(s["role"]) == "user" {
kind = "user_statement"
}
}
kinds = append(kinds, kind)
roles = append(roles, textValue(s["role"]))
if contains([]string{"repository_snapshot", "document_fetch", "command_result"}, kind) {
primary = append(primary, id)
}
ts := textValue(p["valid_at"])
if ts == "" {
ts = textValue(s["ts"])
}
observed = append(observed, ts)
}
// Admission validates source timestamps. Compare their instants while
// retaining the exact recorded spellings in the resulting metadata.
stamps := make(map[string]time.Time, len(observed))
for _, ts := range observed {
stamp, err := time.Parse(time.RFC3339Nano, ts)
if err != nil {
return nil, errors.New("knowledge evidence has an invalid observation timestamp")
}
stamps[ts] = stamp
}
sort.SliceStable(observed, func(i, j int) bool { return stamps[observed[i]].Before(stamps[observed[j]]) })
var first, last, stale any
if len(observed) > 0 {
first = observed[0]
last = observed[len(observed)-1]
}
if asOf != nil && staleAfter != nil {
stale = false
for _, ts := range observed {
if asOf.After(stamps[ts].Add(*staleAfter)) {
stale = true
break
}
}
}
return document{"source_kinds": sortedUnique(kinds), "source_roles": sortedUnique(roles), "primary_artifact_sources": primary, "observed_from": first, "observed_to": last, "age_exceeds_policy": stale, "independently_verified": false}, nil
}
func projectSubjects(vault, learning document) (document, error) {
result := clone(vault)
original := index(items(result["notes"]))
active := activeClaims(vault)
sources := object(vault["sources"])
memberships := map[string][]string{}
for _, s := range items(learning["subjects"]) {
id := textValue(s["id"])
if original[id] != nil {
return nil, errors.New("subject identity collides with evidence note")
}
for _, nid := range stringsValue(s["notes"]) {
memberships[nid] = append(memberships[nid], id)
}
}
notes := items(result["notes"])
for _, subject := range items(learning["subjects"]) {
sections := []document{}
stale := false
for _, s := range items(subject["sections"]) {
paragraphs := []document{}
for _, p := range items(s["paragraphs"]) {
refs := append(append([]string{}, stringsValue(p["claims"])...), stringsValue(p["depends_on"])...)
evidence := items(p["evidence"])
supported := len(stringsValue(p["claims"])) > 0 || (textValue(p["derivation"]) == "subject_guide" && p["review"] != nil && len(evidence) > 0)
if supported && subset(refs, active) && validSourceEvidence(evidence, sources, true) {
paragraphs = append(paragraphs, clone(p))
} else {
stale = true
}
}
if len(paragraphs) > 0 {
sections = append(sections, merge(s, document{"paragraphs": paragraphs}))
}
}
questions := items(clone(document{"questions": subject["questions"]})["questions"])
answerClaims := []string{}
for _, q := range questions {
answer := object(q["answer"])
if len(answer) == 0 {
continue
}
if !subset(stringsValue(q["claims"]), active) || !subset(stringsValue(answer["claims"]), active) || !validSourceEvidence(items(answer["evidence"]), sources, true) {
delete(q, "answer")
q["state"] = "needs_review"
stale = true
continue
}
if contains([]string{"answered", "partially_answered"}, textValue(q["state"])) {
sections = append(sections, document{"heading": answer["heading"], "paragraphs": []document{{"text": answer["text"], "claims": answer["claims"], "evidence": answer["evidence"], "question": q["id"], "partial": textValue(q["state"]) == "partially_answered", "derivation": "gap_answer", "review": answer["review"], "depends_on": q["claims"]}}})
answerClaims = append(answerClaims, stringsValue(answer["claims"])...)
}
}
pending := []string{}
for _, id := range sortedUnique(stringsValue(subject["editorial_pending_claims"])) {
if active[id] != nil {
pending = append(pending, id)
}
}
refreshGuideEvidence(sections, items(subject["conflicts"]), active, sources)
roots := map[string]int{}
links := []document{}
related := map[string]bool{}
for _, nid := range stringsValue(subject["notes"]) {
n := original[nid]
if n == nil {
continue
}
folder := stringsValue(n["folder"])
if len(folder) > 0 {
roots[folder[0]]++
}
links = append(links, document{"id": nid, "kind": "explains", "reason": "Detailed evidence for this subject"})
}
for _, nid := range stringsValue(subject["notes"]) {
for _, edge := range items(original[nid]["links"]) {
for _, target := range memberships[textValue(edge["id"])] {
if target != textValue(subject["id"]) && textValue(edge["reason"]) != "" && !related[target] && len(related) < 3 {
related[target] = true
links = append(links, document{"id": target, "kind": "related", "reason": edge["reason"], "via": []string{nid, textValue(edge["id"])}})
}
}
}
}
root := textValue(subject["scope"])
count := 0
for key, n := range roots {
if n > count || (n == count && key < root) {
count = n
root = key
}
}
state := subject["state"]
if stale {
state = "needs_review"
}
notes = append(notes, document{"id": subject["id"], "title": subject["title"], "scope": subject["scope"], "folder": subjectFolder(subject, root), "claims": unique(append(stringsValue(subject["claims"]), answerClaims...)), "sections": sections, "questions": questions, "aliases": stringsValue(subject["aliases"]), "conflicts": items(subject["conflicts"]), "learning_state": state, "kind": "subject", "links": links, "needs_editorial_update": len(pending) > 0})
}
result["notes"] = notes
return result, nil
}
func subjectFolder(subject document, fallback string) []string {
if folder := stringsValue(subject["folder"]); len(folder) > 0 {
return folder
}
return []string{fallback}
}
func refreshGuideEvidence(sections, conflicts []document, active map[string]document, sources document) {
conflictIDs := map[string]bool{}
for _, c := range conflicts {
if textValue(c["state"]) == "unresolved" && subset(stringsValue(c["claims"]), active) {
for _, id := range stringsValue(c["claims"]) {
conflictIDs[id] = true
}
}
}
for _, s := range sections {
for _, p := range items(s["paragraphs"]) {
evidence := append([]document{}, items(p["evidence"])...)
kinds := []string{}
conflict := false
for _, id := range stringsValue(p["claims"]) {
if c := active[id]; c != nil {
kinds = append(kinds, textValue(c["kind"]))
evidence = append(evidence, items(c["evidence"])...)
}
conflict = conflict || conflictIDs[id]
}
p["evidence_basis"] = merge(evidenceBasis(sources, evidence), document{"claim_kinds": sortedUnique(kinds), "unresolved_conflict": conflict})
}
}
}