Repository navigation
Expand file tree
/
Copy pathcompiler_response.go
More file actions
299 lines (295 loc) · 8.1 KB
/
Copy pathcompiler_response.go
File metadata and controls
299 lines (295 loc) · 8.1 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
package knowledge
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
// Provider wire types stay at this boundary. Invalid primitive/reference types
// fail the batch instead of silently discarding saved source material.
type modelEvidence struct{ Source, Quote string }
type modelClaim struct {
LocalID string `json:"local_id"`
Text, Scope, Topic, Kind, Key string
Evidence []modelEvidence
Entities []string
}
type modelNote struct {
ID, Title, Scope string
Folder, Claims []string
Links []struct{ Title, Scope, Reason string }
}
type modelRelation struct{ From, To, Kind, Reason string }
type modelAlias struct{ From, To string }
type modelParagraph struct {
Text string
Claims []string
}
type modelSection struct {
Heading string
Paragraphs []modelParagraph
}
type modelQuestion struct {
Question, Why string
Claims []string
SearchTerms []string `json:"search_terms"`
}
type modelConflict struct {
Claims []string
Reason string
}
type modelCheck struct {
ID, Reason string
Supported, Useful, Grounded bool
}
func validateCompilerResponse(role string, result document) error {
raw, err := json.Marshal(result)
if err != nil {
return err
}
var target any
required := ""
switch role {
case "update":
target = &struct {
Claims []modelClaim
Notes []modelNote
Relations []modelRelation
Aliases []modelAlias
}{}
required = "claims"
case "discover", "organize_subjects":
target = &struct {
Subjects []struct {
Title, Scope, Purpose, CreationReason string
ExistingSubjectID string `json:"existing_subject_id"`
Aliases, Notes, Folder []string
}
}{}
required = "subjects"
case "consolidate_subjects":
target = &struct {
Groups []struct {
Members, Folder []string
Title, Scope, Purpose string
}
}{}
required = "groups"
case "curate_guide":
target = &struct {
Sections []struct {
Heading string
Paragraphs []string
}
}{}
required = "sections"
case "review_additions":
target = &struct{ Checks []modelCheck }{}
required = "checks"
case "integrate_answers":
target = &struct {
Decisions []struct{ ID, Action, Section, Heading, Reason string }
}{}
required = "decisions"
case "merge_integration":
target = &struct {
Sections []struct {
Heading string
Paragraphs []struct {
Text string
Inputs []string
}
}
}{}
required = "sections"
case "review_integration":
target = &struct {
Checks []struct {
ID, Reason string
Supported, Useful bool
}
Coverage []struct {
ID, Reason string
Preserved bool
}
}{}
required = "checks"
case "understand":
target = &struct {
Sections []modelSection
Questions []modelQuestion
Conflicts []modelConflict
}{}
required = "sections"
case "review_understanding":
target = &struct{ Checks, Questions, Conflicts []modelCheck }{}
case "resolve_questions":
target = &struct {
Matches []struct {
ID, Relation, Reason string
Claims []string
}
}{}
required = "matches"
case "curate_questions":
target = &struct{ Keep []struct{ ID, Reason string } }{}
required = "keep"
case "frontier":
target = &struct{ Selected []struct{ ID, Reason string } }{}
required = "selected"
case "learner_step":
target = &struct {
Action, Query, Source string
Start, Length int
Sections []struct {
Heading string
Paragraphs []struct {
Text string
Claims, Blocks []string
}
}
Gaps []string
}{}
required = "action"
case "review_guide":
target = &struct {
Checks []struct {
ID, Reason string
Supported, Useful bool
}
}{}
case "investigate":
target = &struct {
Answers []struct {
Question, Heading, Text string
Claims []string
Evidence []modelEvidence
}
}{}
required = "answers"
case "review_answers":
target = &struct {
Checks []struct {
Question, Reason string
Supported, Complete bool
}
}{}
case "compose":
target = &struct {
Notes []struct {
ID string
Sections []modelSection
}
}{}
required = "notes"
case "review_articles":
target = &struct{ Checks []modelCheck }{}
case "audit_omissions":
target = &struct {
Omissions []struct{ Source, Text, Quote, Reason string }
}{}
required = "omissions"
case "recall":
target = &struct {
Answer string
Citations []string
Abstain bool
}{}
required = "answer"
case "query":
target = &struct{ Terms, Notes []string }{}
required = "terms"
default:
return fmt.Errorf("unknown native compiler role %q", role)
}
if err = json.Unmarshal(raw, target); err != nil {
return errors.New("knowledge provider result has invalid field or reference types")
}
if required != "" {
value, ok := result[required]
if !ok || value == nil {
return errors.New("knowledge provider result is missing its required collection")
}
}
return nil
}
func validateCompilerReferences(role string, payload, response document) error {
if role == "review_additions" {
return validateAdditionReview(payload, response)
}
if role == "merge_integration" {
return validateIntegrationMerge(payload, response)
}
if role == "review_integration" {
return validateIntegrationReview(payload, response)
}
if role == "integrate_answers" {
return validateAnswerIntegration(payload, response)
}
if role == "curate_guide" {
return validateGuideSelection(payload, response)
}
if role == "consolidate_subjects" {
catalog := index(items(payload["consolidation_catalog"]))
seen, identities := map[string]bool{}, map[string]bool{}
for _, group := range items(response["groups"]) {
title, scope := strings.TrimSpace(textValue(group["title"])), textValue(group["scope"])
members := stringsValue(group["members"])
if title == "" || scope == "" || len(members) == 0 || len(stringsValue(group["folder"])) > 2 {
return errors.New("knowledge consolidation requires a titled scoped group with members and at most two folder levels")
}
key := identity([]any{"subject", strings.ToLower(scope), strings.ToLower(title)})
if identities[key] {
return errors.New("knowledge consolidation returned duplicate canonical headings")
}
identities[key] = true
for _, id := range members {
member := catalog[id]
if member == nil || seen[id] {
return errors.New("knowledge consolidation must assign each inspected draft exactly once")
}
if textValue(member["scope"]) != scope {
return errors.New("knowledge consolidation cannot merge distinct scopes")
}
seen[id] = true
}
}
if len(seen) != len(catalog) {
return errors.New("knowledge consolidation must preserve the complete draft catalog")
}
return nil
}
if role != "discover" && role != "organize_subjects" {
return nil
}
existing, notes := index(items(payload["existing_subjects"])), index(items(payload["catalog"]))
for _, subject := range items(response["subjects"]) {
target := textValue(subject["existing_subject_id"])
if target != "" && existing[target] == nil {
return errors.New("knowledge organization referenced an uninspected existing page")
}
if flag(payload["reuse_only"]) && target == "" {
return errors.New("knowledge reuse-only organization proposed a new page")
}
for _, id := range stringsValue(subject["notes"]) {
if notes[id] == nil {
return errors.New("knowledge organization referenced an uninspected evidence note")
}
}
}
if role == "organize_subjects" {
before, after := []string{}, []string{}
for _, subject := range items(payload["proposed_subjects"]) {
before = append(before, stringsValue(subject["notes"])...)
}
for _, subject := range items(response["subjects"]) {
after = append(after, stringsValue(subject["notes"])...)
}
for _, id := range sortedUnique(before) {
if !contains(after, id) {
return fmt.Errorf("knowledge organization review must preserve planned evidence coverage (planned=%d reviewed=%d)", len(sortedUnique(before)), len(sortedUnique(after)))
}
}
}
return nil
}