behavior-contracts 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/authoring.d.ts +4 -4
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +11 -23
- package/dist/authoring.js.map +1 -1
- package/dist/behavior.d.ts +3 -3
- package/dist/behavior.d.ts.map +1 -1
- package/dist/behavior.js +7 -10
- package/dist/behavior.js.map +1 -1
- package/dist/generator/emit-native-dynamic.d.ts.map +1 -1
- package/dist/generator/emit-native-dynamic.js +7 -4
- package/dist/generator/emit-native-dynamic.js.map +1 -1
- package/dist/generator/emit-shared-typescript.d.ts.map +1 -1
- package/dist/generator/emit-shared-typescript.js +15 -20
- package/dist/generator/emit-shared-typescript.js.map +1 -1
- package/dist/generator/emit-typed-native-go.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-go.js +19 -11
- package/dist/generator/emit-typed-native-go.js.map +1 -1
- package/dist/generator/emit-typed-native-rust.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-rust.js +10 -10
- package/dist/generator/emit-typed-native-rust.js.map +1 -1
- package/go/api_test.go +675 -0
- package/go/behavior.go +1202 -0
- package/go/cmd/conformance/main.go +40 -0
- package/go/codec.go +170 -0
- package/go/coderuntime/behaviorcore.go +93 -0
- package/go/coderuntime/canonical.go +287 -0
- package/go/coderuntime/exprcore.go +334 -0
- package/go/coderuntime/plan.go +422 -0
- package/go/coderuntime/primitives.go +383 -0
- package/go/coderuntime/rawabi.go +281 -0
- package/go/coderuntime/spec.go +17 -0
- package/go/coderuntime/template.go +139 -0
- package/go/coderuntime/value.go +138 -0
- package/go/coderuntime/version.go +9 -0
- package/go/compat.go +289 -0
- package/go/conformance.go +1046 -0
- package/go/envelope.go +77 -0
- package/go/expr.go +347 -0
- package/go/fingerprint.go +147 -0
- package/go/fingerprint_test.go +94 -0
- package/go/go.mod +3 -0
- package/go/guard.go +538 -0
- package/go/jsonord.go +151 -0
- package/go/plan_parallel_test.go +279 -0
- package/go/primitives_test.go +227 -0
- package/package.json +5 -2
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"fmt"
|
|
6
|
+
"io"
|
|
7
|
+
"os"
|
|
8
|
+
"path/filepath"
|
|
9
|
+
"sort"
|
|
10
|
+
"sync"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
// RunConformance loads the 8 suite vector files from vectorsDir, runs every
|
|
14
|
+
// vector through the reference primitives, and writes progress to out. It returns
|
|
15
|
+
// (failed, versionMismatch). A version mismatch is fail-closed: the message is
|
|
16
|
+
// written to out and (failed=0, versionMismatch=true) is returned WITHOUT running
|
|
17
|
+
// any vectors. Callers map versionMismatch -> exit 2, failed>0 -> exit 1.
|
|
18
|
+
func RunConformance(vectorsDir string, out io.Writer) (failed int, versionMismatch bool) {
|
|
19
|
+
fmt.Fprintln(out, "dsl-contracts conformance kit — Go runner")
|
|
20
|
+
|
|
21
|
+
docs, mismatch := preflight(vectorsDir, out)
|
|
22
|
+
if mismatch {
|
|
23
|
+
return 0, true
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var passed, failedN int
|
|
27
|
+
runExpression(out, docs["expression"], &passed, &failedN)
|
|
28
|
+
runTemplate(out, docs["template"], &passed, &failedN)
|
|
29
|
+
runPlanSuite(out, docs["plan"], &passed, &failedN)
|
|
30
|
+
runCanonical(out, docs["canonical"], &passed, &failedN)
|
|
31
|
+
runBehaviorSuite(out, docs["behavior"], &passed, &failedN)
|
|
32
|
+
runGuardSuite(out, docs["guard"], &passed, &failedN)
|
|
33
|
+
runC2Suite(out, docs["c2"], &passed, &failedN)
|
|
34
|
+
runProvenanceSuite(out, docs["provenance"], &passed, &failedN)
|
|
35
|
+
|
|
36
|
+
total := passed + failedN
|
|
37
|
+
fmt.Fprintf(out, "\n%d passed, %d failed / %d vectors across 8 suites\n", passed, failedN, total)
|
|
38
|
+
return failedN, false
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
func loadDoc(dir, file string) *JObj {
|
|
42
|
+
data, err := os.ReadFile(filepath.Join(dir, file))
|
|
43
|
+
if err != nil {
|
|
44
|
+
panic(fmt.Sprintf("cannot read %s: %v", file, err))
|
|
45
|
+
}
|
|
46
|
+
n, err := ParseJSONOrdered(data)
|
|
47
|
+
if err != nil {
|
|
48
|
+
panic(fmt.Sprintf("cannot parse %s: %v", file, err))
|
|
49
|
+
}
|
|
50
|
+
o, ok := n.(*JObj)
|
|
51
|
+
if !ok {
|
|
52
|
+
panic(fmt.Sprintf("%s is not a JSON object", file))
|
|
53
|
+
}
|
|
54
|
+
return o
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
func lineOut(out io.Writer, ok bool, name, detail string) {
|
|
58
|
+
if ok {
|
|
59
|
+
fmt.Fprintf(out, " ✓ %s\n", name)
|
|
60
|
+
} else {
|
|
61
|
+
fmt.Fprintf(out, " ✗ %s\n", name)
|
|
62
|
+
if detail != "" {
|
|
63
|
+
fmt.Fprintf(out, " %s\n", detail)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func bump(ok bool, passed, failed *int) {
|
|
69
|
+
if ok {
|
|
70
|
+
*passed++
|
|
71
|
+
} else {
|
|
72
|
+
*failed++
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── pre-flight version sweep (PROTOCOL §5) ───────────────────────────────────
|
|
77
|
+
func preflight(dir string, out io.Writer) (map[string]*JObj, bool) {
|
|
78
|
+
specs := []struct {
|
|
79
|
+
file, suite, versionKey string
|
|
80
|
+
want int64
|
|
81
|
+
}{
|
|
82
|
+
{"expression.json", "expression", "exprVersion", SpecVersions["expression"]},
|
|
83
|
+
{"template.json", "template", "templateVersion", SpecVersions["template"]},
|
|
84
|
+
{"plan.json", "plan", "planVersion", SpecVersions["plan"]},
|
|
85
|
+
{"canonical.json", "canonical", "canonicalVersion", SpecVersions["canonical"]},
|
|
86
|
+
{"behavior.json", "behavior", "behaviorVersion", SpecVersions["behavior"]},
|
|
87
|
+
{"guard.json", "guard", "guardVersion", SpecVersions["guard"]},
|
|
88
|
+
{"c2-catalog-swap.json", "c2", "c2Version", SpecVersions["c2"]},
|
|
89
|
+
{"provenance.json", "provenance", "provenanceVersion", SpecVersions["provenance"]},
|
|
90
|
+
}
|
|
91
|
+
docs := map[string]*JObj{}
|
|
92
|
+
type mism struct {
|
|
93
|
+
suite string
|
|
94
|
+
got, want int64
|
|
95
|
+
}
|
|
96
|
+
var mismatches []mism
|
|
97
|
+
for _, s := range specs {
|
|
98
|
+
doc := loadDoc(dir, s.file)
|
|
99
|
+
docs[s.suite] = doc
|
|
100
|
+
got := int64(-1)
|
|
101
|
+
if v, ok := doc.Get(s.versionKey); ok {
|
|
102
|
+
got = jnumToInt(v)
|
|
103
|
+
}
|
|
104
|
+
if got != s.want {
|
|
105
|
+
mismatches = append(mismatches, mism{s.suite, got, s.want})
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if len(mismatches) > 0 {
|
|
109
|
+
for _, m := range mismatches {
|
|
110
|
+
fmt.Fprintf(os.Stderr, "FAIL-CLOSED: %s suite version %d != supported %d.\n", m.suite, m.got, m.want)
|
|
111
|
+
}
|
|
112
|
+
fmt.Fprintf(os.Stderr, "Refusing to run: %d suite version mismatch(es). No vectors executed.\n", len(mismatches))
|
|
113
|
+
return nil, true
|
|
114
|
+
}
|
|
115
|
+
return docs, false
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func jnumToInt(v JNode) int64 {
|
|
119
|
+
if n, ok := v.(json.Number); ok {
|
|
120
|
+
i, err := n.Int64()
|
|
121
|
+
if err != nil {
|
|
122
|
+
f, _ := n.Float64()
|
|
123
|
+
return int64(f)
|
|
124
|
+
}
|
|
125
|
+
return i
|
|
126
|
+
}
|
|
127
|
+
return -1
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
func vectorsOf(doc *JObj) []JNode {
|
|
131
|
+
v, _ := doc.Get("vectors")
|
|
132
|
+
arr, _ := v.([]JNode)
|
|
133
|
+
return arr
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
func mustStr(o *JObj, key string) string {
|
|
137
|
+
v, _ := o.Get(key)
|
|
138
|
+
s, _ := v.(string)
|
|
139
|
+
return s
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
func asObj(n JNode) *JObj {
|
|
143
|
+
o, _ := n.(*JObj)
|
|
144
|
+
return o
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ── expression ───────────────────────────────────────────────────────────────
|
|
148
|
+
func runExpression(out io.Writer, doc *JObj, passed, failed *int) {
|
|
149
|
+
vectors := vectorsOf(doc)
|
|
150
|
+
fmt.Fprintf(out, "\nexpression.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "exprVersion")), len(vectors))
|
|
151
|
+
for _, vn := range vectors {
|
|
152
|
+
v := asObj(vn)
|
|
153
|
+
name := mustStr(v, "name")
|
|
154
|
+
expect := asObj(mustNode(v, "expect"))
|
|
155
|
+
var scope *Obj
|
|
156
|
+
if sc, ok := v.Get("scope"); ok && sc != nil {
|
|
157
|
+
dv, err := DecodeValue(sc)
|
|
158
|
+
if err == nil {
|
|
159
|
+
scope, _ = dv.(*Obj)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
ok := false
|
|
163
|
+
detail := ""
|
|
164
|
+
result, err := EvaluateExpression(mustNode(v, "expr"), scope)
|
|
165
|
+
if err != nil {
|
|
166
|
+
ef, isEf := err.(*ExprFailure)
|
|
167
|
+
if !isEf {
|
|
168
|
+
panic(err)
|
|
169
|
+
}
|
|
170
|
+
if fv, has := expect.Get("failure"); has {
|
|
171
|
+
ok = ef.Code == fv.(string)
|
|
172
|
+
if !ok {
|
|
173
|
+
detail = fmt.Sprintf("expected Failure(%s), got Failure(%s)", fv, ef.Code)
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
detail = fmt.Sprintf("expected a value, got Failure(%s)", ef.Code)
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
if ev, has := expect.Get("value"); has {
|
|
180
|
+
want, derr := DecodeValue(ev)
|
|
181
|
+
if derr != nil {
|
|
182
|
+
panic(derr)
|
|
183
|
+
}
|
|
184
|
+
ok = DeepEquals(result, want)
|
|
185
|
+
if !ok {
|
|
186
|
+
detail = fmt.Sprintf("expected value, got %s", EncodeValue(result))
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
fv, _ := expect.Get("failure")
|
|
190
|
+
detail = fmt.Sprintf("expected Failure(%v), got a value", fv)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
lineOut(out, ok, name, detail)
|
|
194
|
+
bump(ok, passed, failed)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ── template ─────────────────────────────────────────────────────────────────
|
|
199
|
+
func runTemplate(out io.Writer, doc *JObj, passed, failed *int) {
|
|
200
|
+
vectors := vectorsOf(doc)
|
|
201
|
+
fmt.Fprintf(out, "\ntemplate.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "templateVersion")), len(vectors))
|
|
202
|
+
for _, vn := range vectors {
|
|
203
|
+
v := asObj(vn)
|
|
204
|
+
name := mustStr(v, "name")
|
|
205
|
+
tmpl := mustStr(v, "template")
|
|
206
|
+
expect := asObj(mustNode(v, "expect"))
|
|
207
|
+
params := NewObj()
|
|
208
|
+
if pobj := asObj(mustNode(v, "params")); pobj != nil {
|
|
209
|
+
for _, k := range pobj.Keys {
|
|
210
|
+
dv, err := DecodeValue(pobj.Vals[k])
|
|
211
|
+
if err != nil {
|
|
212
|
+
panic(err)
|
|
213
|
+
}
|
|
214
|
+
params.Set(k, dv)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
ok := false
|
|
218
|
+
detail := ""
|
|
219
|
+
res, err := RenderTemplate(tmpl, params)
|
|
220
|
+
if err != nil {
|
|
221
|
+
tf, isTf := err.(*TemplateFailure)
|
|
222
|
+
if !isTf {
|
|
223
|
+
panic(err)
|
|
224
|
+
}
|
|
225
|
+
if fv, has := expect.Get("failure"); has {
|
|
226
|
+
ok = tf.Code == fv.(string)
|
|
227
|
+
if !ok {
|
|
228
|
+
detail = fmt.Sprintf("expected Failure(%s), got Failure(%s)", fv, tf.Code)
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
detail = fmt.Sprintf("expected a value, got Failure(%s)", tf.Code)
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
if ev, has := expect.Get("ok"); has {
|
|
235
|
+
ok = res == ev.(string)
|
|
236
|
+
if !ok {
|
|
237
|
+
detail = fmt.Sprintf("expected %q, got %q", ev, res)
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
fv, _ := expect.Get("failure")
|
|
241
|
+
detail = fmt.Sprintf("expected Failure(%v), got %q", fv, res)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
lineOut(out, ok, name, detail)
|
|
245
|
+
bump(ok, passed, failed)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ── canonical ────────────────────────────────────────────────────────────────
|
|
250
|
+
func runCanonical(out io.Writer, doc *JObj, passed, failed *int) {
|
|
251
|
+
vectors := vectorsOf(doc)
|
|
252
|
+
fmt.Fprintf(out, "\ncanonical.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "canonicalVersion")), len(vectors))
|
|
253
|
+
for _, vn := range vectors {
|
|
254
|
+
v := asObj(vn)
|
|
255
|
+
name := mustStr(v, "name")
|
|
256
|
+
kind := mustStr(v, "kind")
|
|
257
|
+
expect := asObj(mustNode(v, "expect"))
|
|
258
|
+
ok := false
|
|
259
|
+
detail := ""
|
|
260
|
+
var res string
|
|
261
|
+
var err error
|
|
262
|
+
// DecodeValue may itself fail-closed (e.g. FORBIDDEN_KEY for a "__proto__"
|
|
263
|
+
// own key). Route a coded DecodeFailure into the Failure-compare path
|
|
264
|
+
// (PROTOCOL §3.4); an uncoded decode error is a real malformed-vector bug
|
|
265
|
+
// and still panics.
|
|
266
|
+
val, derr := DecodeValue(mustNode(v, "value"))
|
|
267
|
+
if derr != nil {
|
|
268
|
+
if df, isDf := derr.(*DecodeFailure); isDf {
|
|
269
|
+
err = canonFail(df.Code, df.Msg)
|
|
270
|
+
} else {
|
|
271
|
+
panic(derr)
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
switch kind {
|
|
275
|
+
case "canonicalValue":
|
|
276
|
+
res, err = CanonicalValue(val)
|
|
277
|
+
case "canonicalJson":
|
|
278
|
+
res, err = CanonicalJSON(val)
|
|
279
|
+
case "floatRepr":
|
|
280
|
+
f, isF := val.(float64)
|
|
281
|
+
if !isF {
|
|
282
|
+
panic(fmt.Sprintf("floatRepr expects a float, got %s", typeName(val)))
|
|
283
|
+
}
|
|
284
|
+
res, err = PyFloatRepr(f)
|
|
285
|
+
default:
|
|
286
|
+
panic("unknown kind: " + kind)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if err != nil {
|
|
290
|
+
cf, isCf := err.(*CanonicalFailure)
|
|
291
|
+
if !isCf {
|
|
292
|
+
panic(err)
|
|
293
|
+
}
|
|
294
|
+
if fv, has := expect.Get("failure"); has {
|
|
295
|
+
ok = cf.Code == fv.(string)
|
|
296
|
+
if !ok {
|
|
297
|
+
detail = fmt.Sprintf("expected Failure(%s), got Failure(%s)", fv, cf.Code)
|
|
298
|
+
}
|
|
299
|
+
} else {
|
|
300
|
+
detail = fmt.Sprintf("expected a value, got Failure(%s)", cf.Code)
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
if ev, has := expect.Get("ok"); has {
|
|
304
|
+
ok = res == ev.(string)
|
|
305
|
+
if !ok {
|
|
306
|
+
detail = fmt.Sprintf("expected %q, got %q", ev, res)
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
fv, _ := expect.Get("failure")
|
|
310
|
+
detail = fmt.Sprintf("expected Failure(%v), got %q", fv, res)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
lineOut(out, ok, name, detail)
|
|
314
|
+
bump(ok, passed, failed)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ── plan ─────────────────────────────────────────────────────────────────────
|
|
319
|
+
func parseOpSpec(o *JObj) OpSpec {
|
|
320
|
+
op := OpSpec{ID: mustStr(o, "id"), Parent: -1}
|
|
321
|
+
if p, ok := o.Get("parent"); ok && p != nil {
|
|
322
|
+
op.Parent = int(jnumToInt(p))
|
|
323
|
+
}
|
|
324
|
+
if bf, ok := o.Get("bindField"); ok {
|
|
325
|
+
if s, isS := bf.(string); isS {
|
|
326
|
+
op.BindField = s
|
|
327
|
+
op.HasBindField = true
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if rk, ok := o.Get("relationKind"); ok {
|
|
331
|
+
if s, isS := rk.(string); isS {
|
|
332
|
+
op.RelationKind = s
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if pol, ok := o.Get("policy"); ok {
|
|
336
|
+
if s, isS := pol.(string); isS {
|
|
337
|
+
op.Policy = s
|
|
338
|
+
op.HasPolicy = true
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return op
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
func parsePlanSpec(n JNode) *ExecutionPlanSpec {
|
|
345
|
+
o, ok := n.(*JObj)
|
|
346
|
+
if !ok {
|
|
347
|
+
return nil
|
|
348
|
+
}
|
|
349
|
+
groupsN, _ := o.Get("groups")
|
|
350
|
+
groupsArr, _ := groupsN.([]JNode)
|
|
351
|
+
groups := make([][]int, len(groupsArr))
|
|
352
|
+
for i, g := range groupsArr {
|
|
353
|
+
ga, _ := g.([]JNode)
|
|
354
|
+
row := make([]int, len(ga))
|
|
355
|
+
for j, idx := range ga {
|
|
356
|
+
row[j] = int(jnumToInt(idx))
|
|
357
|
+
}
|
|
358
|
+
groups[i] = row
|
|
359
|
+
}
|
|
360
|
+
conc := int64(1)
|
|
361
|
+
if c, has := o.Get("concurrency"); has {
|
|
362
|
+
conc = jnumToInt(c)
|
|
363
|
+
}
|
|
364
|
+
return &ExecutionPlanSpec{Groups: groups, Concurrency: conc}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
func sameSet(a []string, b []JNode) bool {
|
|
368
|
+
if len(a) != len(b) {
|
|
369
|
+
return false
|
|
370
|
+
}
|
|
371
|
+
sa := append([]string(nil), a...)
|
|
372
|
+
sb := make([]string, len(b))
|
|
373
|
+
for i, x := range b {
|
|
374
|
+
sb[i] = x.(string)
|
|
375
|
+
}
|
|
376
|
+
sort.Strings(sa)
|
|
377
|
+
sort.Strings(sb)
|
|
378
|
+
for i := range sa {
|
|
379
|
+
if sa[i] != sb[i] {
|
|
380
|
+
return false
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return true
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
func runPlanSuite(out io.Writer, doc *JObj, passed, failed *int) {
|
|
387
|
+
vectors := vectorsOf(doc)
|
|
388
|
+
fmt.Fprintf(out, "\nplan.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "planVersion")), len(vectors))
|
|
389
|
+
for _, vn := range vectors {
|
|
390
|
+
v := asObj(vn)
|
|
391
|
+
name := mustStr(v, "name")
|
|
392
|
+
expect := asObj(mustNode(v, "expect"))
|
|
393
|
+
|
|
394
|
+
var plan *ExecutionPlanSpec
|
|
395
|
+
if p, ok := v.Get("plan"); ok {
|
|
396
|
+
plan = parsePlanSpec(p)
|
|
397
|
+
}
|
|
398
|
+
opsN, _ := v.Get("ops")
|
|
399
|
+
opsArr, _ := opsN.([]JNode)
|
|
400
|
+
ops := make([]OpSpec, len(opsArr))
|
|
401
|
+
for i, o := range opsArr {
|
|
402
|
+
ops[i] = parseOpSpec(asObj(o))
|
|
403
|
+
}
|
|
404
|
+
execMap := asObj(mustNode(v, "exec"))
|
|
405
|
+
|
|
406
|
+
exec := func(op OpSpec, _ Value) ExecOutcome {
|
|
407
|
+
on, ok := execMap.Get(op.ID)
|
|
408
|
+
if !ok {
|
|
409
|
+
panic("no mock outcome for op '" + op.ID + "'")
|
|
410
|
+
}
|
|
411
|
+
oo := asObj(on)
|
|
412
|
+
if okv, has := oo.Get("ok"); has {
|
|
413
|
+
dv, err := DecodeValue(okv)
|
|
414
|
+
if err != nil {
|
|
415
|
+
panic(err)
|
|
416
|
+
}
|
|
417
|
+
return OkOutcome(dv)
|
|
418
|
+
}
|
|
419
|
+
ev, _ := oo.Get("error")
|
|
420
|
+
es, _ := ev.(string)
|
|
421
|
+
return ErrOutcome(es)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
ok := false
|
|
425
|
+
detail := ""
|
|
426
|
+
res, err := RunPlan(plan, ops, exec)
|
|
427
|
+
if err != nil {
|
|
428
|
+
pf, isPf := err.(*PlanFailure)
|
|
429
|
+
if !isPf {
|
|
430
|
+
panic(err)
|
|
431
|
+
}
|
|
432
|
+
if fv, has := expect.Get("failure"); has {
|
|
433
|
+
ok = pf.Code == fv.(string)
|
|
434
|
+
if !ok {
|
|
435
|
+
detail = fmt.Sprintf("expected Failure(%s), got Failure(%s)", fv, pf.Code)
|
|
436
|
+
}
|
|
437
|
+
} else {
|
|
438
|
+
detail = fmt.Sprintf("expected a full run, got Failure(%s)", pf.Code)
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
if fv, has := expect.Get("failure"); has {
|
|
442
|
+
detail = fmt.Sprintf("expected Failure(%v), got a full run", fv)
|
|
443
|
+
} else {
|
|
444
|
+
tree := res.FinalTree()
|
|
445
|
+
expTreeObj := asObj(mustNode(expect, "tree"))
|
|
446
|
+
expTree := NewObj()
|
|
447
|
+
for _, k := range expTreeObj.Keys {
|
|
448
|
+
dv, derr := DecodeValue(expTreeObj.Vals[k])
|
|
449
|
+
if derr != nil {
|
|
450
|
+
panic(derr)
|
|
451
|
+
}
|
|
452
|
+
expTree.Set(k, dv)
|
|
453
|
+
}
|
|
454
|
+
gotC, _ := CanonicalJSON(tree)
|
|
455
|
+
wantC, _ := CanonicalJSON(expTree)
|
|
456
|
+
treeOk := gotC == wantC
|
|
457
|
+
execArr, _ := mustNode(expect, "executed").([]JNode)
|
|
458
|
+
skipArr, _ := mustNode(expect, "skipped").([]JNode)
|
|
459
|
+
execOk := sameSet(res.Executed, execArr)
|
|
460
|
+
skipOk := sameSet(res.Skipped, skipArr)
|
|
461
|
+
ok = treeOk && execOk && skipOk
|
|
462
|
+
if !ok {
|
|
463
|
+
var parts []string
|
|
464
|
+
if !treeOk {
|
|
465
|
+
parts = append(parts, fmt.Sprintf("tree %s != %s", gotC, wantC))
|
|
466
|
+
}
|
|
467
|
+
if !execOk {
|
|
468
|
+
parts = append(parts, fmt.Sprintf("executed %v mismatch", res.Executed))
|
|
469
|
+
}
|
|
470
|
+
if !skipOk {
|
|
471
|
+
parts = append(parts, fmt.Sprintf("skipped %v mismatch", res.Skipped))
|
|
472
|
+
}
|
|
473
|
+
detail = joinSemi(parts)
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
lineOut(out, ok, name, detail)
|
|
478
|
+
bump(ok, passed, failed)
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
func joinSemi(parts []string) string {
|
|
483
|
+
s := ""
|
|
484
|
+
for i, p := range parts {
|
|
485
|
+
if i > 0 {
|
|
486
|
+
s += "; "
|
|
487
|
+
}
|
|
488
|
+
s += p
|
|
489
|
+
}
|
|
490
|
+
return s
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
func mustNode(o *JObj, key string) JNode {
|
|
494
|
+
v, ok := o.Get(key)
|
|
495
|
+
if !ok {
|
|
496
|
+
panic("missing key: " + key)
|
|
497
|
+
}
|
|
498
|
+
return v
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// ── behavior ─────────────────────────────────────────────────────────────────
|
|
502
|
+
// scriptedHandlers is a data-driven ComponentExec: handlers[name] = FIFO queue of
|
|
503
|
+
// raw outcome JNodes (a non-array bare {ok}/{error} is the "same element every call"
|
|
504
|
+
// shorthand). Each Exec call pops the next outcome for that component. The pop is
|
|
505
|
+
// mutex-guarded: vectors that ship plans with concurrency > 1 declare their
|
|
506
|
+
// handlers safe for concurrent invocation (bc#23), so RunBehavior may call this
|
|
507
|
+
// registry from multiple goroutines within a stage.
|
|
508
|
+
type scriptedHandlers struct {
|
|
509
|
+
mu sync.Mutex
|
|
510
|
+
queues map[string]*scriptQueue
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
type scriptQueue struct {
|
|
514
|
+
items []JNode
|
|
515
|
+
single bool
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// NewScriptedHandlers builds the scripted handler registry used by the
|
|
519
|
+
// conformance and codegen runners (PROTOCOL.md §3.5 spec: catalog name →
|
|
520
|
+
// scripted outcome(s)). Exported so external test drivers (e.g. the codegen
|
|
521
|
+
// conformance runner, bc#13 SP2) reuse the exact vector handler semantics
|
|
522
|
+
// instead of reimplementing them.
|
|
523
|
+
func NewScriptedHandlers(spec *JObj) ComponentExec { return newScriptedHandlers(spec) }
|
|
524
|
+
|
|
525
|
+
func newScriptedHandlers(spec *JObj) *scriptedHandlers {
|
|
526
|
+
h := &scriptedHandlers{queues: map[string]*scriptQueue{}}
|
|
527
|
+
if spec == nil {
|
|
528
|
+
return h
|
|
529
|
+
}
|
|
530
|
+
for _, name := range spec.Keys {
|
|
531
|
+
raw := spec.Vals[name]
|
|
532
|
+
if arr, ok := raw.([]JNode); ok {
|
|
533
|
+
h.queues[name] = &scriptQueue{items: append([]JNode(nil), arr...), single: false}
|
|
534
|
+
} else {
|
|
535
|
+
h.queues[name] = &scriptQueue{items: []JNode{raw}, single: true}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return h
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
func (h *scriptedHandlers) Exec(component string, ports *Obj, bound Value) (ExecOutcome, bool) {
|
|
542
|
+
return h.ExecCtx("", component, ports, bound)
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// ExecCtx implements ComponentCtxExec. v2 observation outcomes (PROTOCOL.md §3.5):
|
|
546
|
+
//
|
|
547
|
+
// {"echo":"ctx"} -> {ok: {nodeId, component}} (echoes the handler ctx)
|
|
548
|
+
// {"echo":"items"} -> {ok: ports["items"]} (what a batched handler received)
|
|
549
|
+
// {"echo":"port","port":"<name>"} -> {ok: ports["<name>"]} (the value that reached the named port —
|
|
550
|
+
// makes the port plane observable; a port not received echoes null)
|
|
551
|
+
func (h *scriptedHandlers) ExecCtx(nodeID, component string, ports *Obj, _ Value) (ExecOutcome, bool) {
|
|
552
|
+
h.mu.Lock()
|
|
553
|
+
q, ok := h.queues[component]
|
|
554
|
+
if !ok {
|
|
555
|
+
h.mu.Unlock()
|
|
556
|
+
return ExecOutcome{}, false
|
|
557
|
+
}
|
|
558
|
+
var raw JNode
|
|
559
|
+
if q.single || len(q.items) <= 1 {
|
|
560
|
+
raw = q.items[0]
|
|
561
|
+
} else {
|
|
562
|
+
raw = q.items[0]
|
|
563
|
+
q.items = q.items[1:]
|
|
564
|
+
}
|
|
565
|
+
h.mu.Unlock()
|
|
566
|
+
ro := asObj(raw)
|
|
567
|
+
if echoN, has := ro.Get("echo"); has {
|
|
568
|
+
echo, _ := echoN.(string)
|
|
569
|
+
switch echo {
|
|
570
|
+
case "ctx":
|
|
571
|
+
ctxObj := NewObj()
|
|
572
|
+
ctxObj.Set("nodeId", nodeID)
|
|
573
|
+
ctxObj.Set("component", component)
|
|
574
|
+
return OkOutcome(ctxObj), true
|
|
575
|
+
case "items":
|
|
576
|
+
if items, hasItems := ports.Get("items"); hasItems {
|
|
577
|
+
return OkOutcome(items), true
|
|
578
|
+
}
|
|
579
|
+
return OkOutcome(nil), true
|
|
580
|
+
case "port":
|
|
581
|
+
// {"echo":"port","port":"PK"} -> {ok: ports["PK"]}. Makes a single port's wiring
|
|
582
|
+
// OBSERVABLE end-to-end (the native-ports non-vacuous gate mutates a generated port
|
|
583
|
+
// assignment and expects the output to diverge). Shared by run_behavior + native runner.
|
|
584
|
+
// {"echo":"port","port":"X","wrap":"v"} -> {ok: {v: ports["X"]}} — the echoed port INSIDE a
|
|
585
|
+
// single-field record, so a de-box node whose result WRAPS an input port stays non-vacuous.
|
|
586
|
+
var wrapField string
|
|
587
|
+
if wN, hasW := ro.Get("wrap"); hasW {
|
|
588
|
+
wrapField, _ = wN.(string)
|
|
589
|
+
}
|
|
590
|
+
if pN, has := ro.Get("port"); has {
|
|
591
|
+
if pName, ok := pN.(string); ok {
|
|
592
|
+
var pv Value
|
|
593
|
+
if got, hasPort := ports.Get(pName); hasPort {
|
|
594
|
+
pv = got
|
|
595
|
+
}
|
|
596
|
+
if wrapField != "" {
|
|
597
|
+
rec := NewObj()
|
|
598
|
+
rec.Set(wrapField, pv)
|
|
599
|
+
return OkOutcome(rec), true
|
|
600
|
+
}
|
|
601
|
+
return OkOutcome(pv), true
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return OkOutcome(nil), true
|
|
605
|
+
default:
|
|
606
|
+
// an echo outside the closed set is a VECTOR-AUTHORING error — fail LOUD (PROTOCOL.md
|
|
607
|
+
// §3.5), never fall through to the ok/error reading and judge the vector on a silent misread.
|
|
608
|
+
panic(fmt.Sprintf("conformance: unknown echo directive %q (expected \"ctx\" | \"items\" | \"port\")", echo))
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if okv, has := ro.Get("ok"); has {
|
|
612
|
+
dv, err := DecodeValue(okv)
|
|
613
|
+
if err != nil {
|
|
614
|
+
panic(err)
|
|
615
|
+
}
|
|
616
|
+
return OkOutcome(dv), true
|
|
617
|
+
}
|
|
618
|
+
ev, _ := ro.Get("error")
|
|
619
|
+
es, _ := ev.(string)
|
|
620
|
+
// A leaf-produced structured Error Value (scp-error.md) rides through as the outcome's Detail.
|
|
621
|
+
if dN, has := ro.Get("detail"); has {
|
|
622
|
+
return ErrOutcomeDetail(es, decodeErrorDetail(dN)), true
|
|
623
|
+
}
|
|
624
|
+
return ErrOutcome(es), true
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// decodeErrorDetail decodes a vector's `detail` JNode into the structured Error Value. This is the
|
|
628
|
+
// leaf side of the contract: a scripted handler produces the detail exactly as a real leaf would.
|
|
629
|
+
func decodeErrorDetail(n JNode) *ErrorDetail {
|
|
630
|
+
o, ok := n.(*JObj)
|
|
631
|
+
if !ok {
|
|
632
|
+
return nil
|
|
633
|
+
}
|
|
634
|
+
str := func(k string) string {
|
|
635
|
+
if v, has := o.Get(k); has {
|
|
636
|
+
if s, ok2 := v.(string); ok2 {
|
|
637
|
+
return s
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
return ""
|
|
641
|
+
}
|
|
642
|
+
d := &ErrorDetail{
|
|
643
|
+
Kind: str("kind"),
|
|
644
|
+
Model: str("model"),
|
|
645
|
+
Field: str("field"),
|
|
646
|
+
ExpectedType: str("expectedType"),
|
|
647
|
+
ActualWireType: str("actualWireType"),
|
|
648
|
+
RawValue: str("rawValue"),
|
|
649
|
+
}
|
|
650
|
+
if cN, has := o.Get("context"); has {
|
|
651
|
+
if cObj, ok2 := cN.(*JObj); ok2 {
|
|
652
|
+
d.Context = map[string]string{}
|
|
653
|
+
for _, k := range cObj.Keys {
|
|
654
|
+
if s, ok3 := cObj.Vals[k].(string); ok3 {
|
|
655
|
+
d.Context[k] = s
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return d
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// behaviorErrCode maps a RunBehavior error to its stable conformance code.
|
|
664
|
+
func behaviorErrCode(err error) (string, bool) {
|
|
665
|
+
switch e := err.(type) {
|
|
666
|
+
case *BehaviorFailure:
|
|
667
|
+
return e.Code, true
|
|
668
|
+
case *PlanFailure:
|
|
669
|
+
return e.Code, true
|
|
670
|
+
case *ExprFailure:
|
|
671
|
+
return e.Code, true
|
|
672
|
+
default:
|
|
673
|
+
return "", false
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// behaviorErrDetail extracts the structured Error Value from a RunBehavior error, if any.
|
|
678
|
+
func behaviorErrDetail(err error) *ErrorDetail {
|
|
679
|
+
switch e := err.(type) {
|
|
680
|
+
case *BehaviorFailure:
|
|
681
|
+
return e.Detail
|
|
682
|
+
case *PlanFailure:
|
|
683
|
+
return e.Detail
|
|
684
|
+
case *ExprFailure:
|
|
685
|
+
return e.Detail
|
|
686
|
+
default:
|
|
687
|
+
return nil
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// detailMatches compares a failure's structured detail against a vector's expect.detail (present
|
|
692
|
+
// fields only — absent optional fields are not compared).
|
|
693
|
+
func detailMatches(got *ErrorDetail, expect JNode) (bool, string) {
|
|
694
|
+
want, ok := expect.(*JObj)
|
|
695
|
+
if !ok {
|
|
696
|
+
return false, "expect.detail is not an object"
|
|
697
|
+
}
|
|
698
|
+
gm := map[string]string{}
|
|
699
|
+
if got != nil {
|
|
700
|
+
for k, v := range map[string]string{"kind": got.Kind, "model": got.Model, "field": got.Field, "expectedType": got.ExpectedType, "actualWireType": got.ActualWireType, "rawValue": got.RawValue} {
|
|
701
|
+
if v != "" {
|
|
702
|
+
gm[k] = v
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
for _, k := range want.Keys {
|
|
707
|
+
if k == "context" {
|
|
708
|
+
continue
|
|
709
|
+
}
|
|
710
|
+
ws, _ := want.Vals[k].(string)
|
|
711
|
+
if gm[k] != ws {
|
|
712
|
+
return false, fmt.Sprintf("detail.%s = %q, want %q", k, gm[k], ws)
|
|
713
|
+
}
|
|
714
|
+
delete(gm, k)
|
|
715
|
+
}
|
|
716
|
+
if len(gm) != 0 {
|
|
717
|
+
return false, fmt.Sprintf("detail has extra fields %v", gm)
|
|
718
|
+
}
|
|
719
|
+
// context (map<string,string>), present only if the vector pins it.
|
|
720
|
+
if cN, has := want.Get("context"); has {
|
|
721
|
+
cObj, _ := cN.(*JObj)
|
|
722
|
+
if got == nil || len(got.Context) != len(cObj.Keys) {
|
|
723
|
+
return false, "detail.context size mismatch"
|
|
724
|
+
}
|
|
725
|
+
for _, k := range cObj.Keys {
|
|
726
|
+
ws, _ := cObj.Vals[k].(string)
|
|
727
|
+
if got.Context[k] != ws {
|
|
728
|
+
return false, fmt.Sprintf("detail.context.%s = %q, want %q", k, got.Context[k], ws)
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
} else if got != nil && len(got.Context) > 0 {
|
|
732
|
+
return false, "detail has unexpected context"
|
|
733
|
+
}
|
|
734
|
+
return true, ""
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// ── guard (AssertPortableComponentGraph: static portability check, bc#25) ─────
|
|
738
|
+
// The `ir` field is passed to the guard AS-IS (raw parsed JSON; no §2 typed
|
|
739
|
+
// decode — the guard is a static structural check over the JSON itself). A
|
|
740
|
+
// reject maps PortabilityError to the fixed code PORTABILITY and compares
|
|
741
|
+
// expect.path by exact string equality (message text is NOT compared;
|
|
742
|
+
// PROTOCOL.md §3.6). Function-residue rejects cannot ride a JSON vector and
|
|
743
|
+
// stay in unit tests (raw JSON node types cannot represent functions in Go).
|
|
744
|
+
func runGuardSuite(out io.Writer, doc *JObj, passed, failed *int) {
|
|
745
|
+
vectors := vectorsOf(doc)
|
|
746
|
+
fmt.Fprintf(out, "\nguard.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "guardVersion")), len(vectors))
|
|
747
|
+
for _, vn := range vectors {
|
|
748
|
+
v := asObj(vn)
|
|
749
|
+
name := mustStr(v, "name")
|
|
750
|
+
expect := asObj(mustNode(v, "expect"))
|
|
751
|
+
ok := false
|
|
752
|
+
detail := ""
|
|
753
|
+
err := AssertPortableComponentGraph(mustNode(v, "ir"))
|
|
754
|
+
if err != nil {
|
|
755
|
+
pe, isPe := err.(*PortabilityError)
|
|
756
|
+
if !isPe {
|
|
757
|
+
panic(err)
|
|
758
|
+
}
|
|
759
|
+
if fv, has := expect.Get("failure"); has {
|
|
760
|
+
wantPath, _ := expect.Get("path")
|
|
761
|
+
ok = fv.(string) == "PORTABILITY" && pe.Path == wantPath.(string)
|
|
762
|
+
if !ok {
|
|
763
|
+
detail = fmt.Sprintf("expected reject path %v, got %s", wantPath, pe.Path)
|
|
764
|
+
}
|
|
765
|
+
} else {
|
|
766
|
+
detail = fmt.Sprintf("expected guard to pass, got PORTABILITY at %s", pe.Path)
|
|
767
|
+
}
|
|
768
|
+
} else {
|
|
769
|
+
if _, has := expect.Get("ok"); has {
|
|
770
|
+
ok = true
|
|
771
|
+
} else {
|
|
772
|
+
fv, _ := expect.Get("failure")
|
|
773
|
+
wantPath, _ := expect.Get("path")
|
|
774
|
+
detail = fmt.Sprintf("expected Failure(%v at %v), guard passed", fv, wantPath)
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
lineOut(out, ok, name, detail)
|
|
778
|
+
bump(ok, passed, failed)
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// ── provenance (serialization-boundary canonical fingerprint gate, #128/A6) ──
|
|
783
|
+
// The token (the process-internal unforgeable compile capability) does NOT cross
|
|
784
|
+
// the JSON boundary; on the wire the gate is the canonical fingerprint. The runner
|
|
785
|
+
// RECOMPUTES FingerprintComponentGraph over the raw `ir` and compares to the
|
|
786
|
+
// declared `fingerprint`: equal = accept, unequal (stale/tampered) maps to the
|
|
787
|
+
// fixed code NON_COMPILED_IR reject. Key-order / int-vs-integral-float spelling do
|
|
788
|
+
// not change the fingerprint (canonical invariance). Non-vacuity
|
|
789
|
+
// (CONFORMANCE_MUTATE): a 1-nibble-flipped fingerprint MUST reject, proving the
|
|
790
|
+
// runner actually recomputes + compares. (scp-only-authoring-contract §3 SA3/§5.)
|
|
791
|
+
func runProvenanceSuite(out io.Writer, doc *JObj, passed, failed *int) {
|
|
792
|
+
vectors := vectorsOf(doc)
|
|
793
|
+
fmt.Fprintf(out, "\nprovenance.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "provenanceVersion")), len(vectors))
|
|
794
|
+
for _, vn := range vectors {
|
|
795
|
+
v := asObj(vn)
|
|
796
|
+
name := mustStr(v, "name")
|
|
797
|
+
expect := asObj(mustNode(v, "expect"))
|
|
798
|
+
declared := mustStr(v, "fingerprint")
|
|
799
|
+
ok := false
|
|
800
|
+
detail := ""
|
|
801
|
+
recomputed, err := FingerprintComponentGraph(mustNode(v, "ir"))
|
|
802
|
+
if err != nil {
|
|
803
|
+
panic(err)
|
|
804
|
+
}
|
|
805
|
+
if recomputed == declared {
|
|
806
|
+
if _, has := expect.Get("ok"); has {
|
|
807
|
+
ok = true
|
|
808
|
+
} else {
|
|
809
|
+
fv, _ := expect.Get("failure")
|
|
810
|
+
detail = fmt.Sprintf("expected Failure(%v), fingerprint matched", fv)
|
|
811
|
+
}
|
|
812
|
+
} else if fv, has := expect.Get("failure"); has {
|
|
813
|
+
// mismatch = boundary-loader NON_COMPILED_IR reject.
|
|
814
|
+
ok = fv.(string) == "NON_COMPILED_IR"
|
|
815
|
+
if !ok {
|
|
816
|
+
detail = fmt.Sprintf("expected Failure(%v), got NON_COMPILED_IR", fv)
|
|
817
|
+
}
|
|
818
|
+
} else {
|
|
819
|
+
detail = fmt.Sprintf("expected accept, got NON_COMPILED_IR (declared %s, recomputed %s)", declared, recomputed)
|
|
820
|
+
}
|
|
821
|
+
lineOut(out, ok, name, detail)
|
|
822
|
+
bump(ok, passed, failed)
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// ── c2 (c2-catalog-swap: catalog-swap execution + IR structural identity, bc#28) ──
|
|
827
|
+
// Each consumer's vectors run in the behavior-suite form (the SAME RunBehavior
|
|
828
|
+
// with the consumer's scripted handler set must yield the consumer's expected
|
|
829
|
+
// output), then a structural check proves the two consumers' IRs are deep-equal
|
|
830
|
+
// MODULO the catalog name map — and NOT equal before normalization (the catalog
|
|
831
|
+
// is observably the only difference). PROTOCOL.md §3.7.
|
|
832
|
+
func c2Normalize(decoded Value, roleByName map[string]string) error {
|
|
833
|
+
root, _ := decoded.(*Obj)
|
|
834
|
+
compsV, _ := root.Get("components")
|
|
835
|
+
comps, _ := compsV.([]Value)
|
|
836
|
+
for _, cV := range comps {
|
|
837
|
+
c, _ := cV.(*Obj)
|
|
838
|
+
bodyV, _ := c.Get("body")
|
|
839
|
+
body, _ := bodyV.([]Value)
|
|
840
|
+
for _, nV := range body {
|
|
841
|
+
n, _ := nV.(*Obj)
|
|
842
|
+
target := n
|
|
843
|
+
if mV, has := n.Get("map"); has {
|
|
844
|
+
target, _ = mV.(*Obj)
|
|
845
|
+
}
|
|
846
|
+
if compNameV, has := target.Get("component"); has {
|
|
847
|
+
name, _ := compNameV.(string)
|
|
848
|
+
role, mapped := roleByName[name]
|
|
849
|
+
if !mapped {
|
|
850
|
+
return fmt.Errorf("unmapped component '%s'", name)
|
|
851
|
+
}
|
|
852
|
+
target.Set("component", role)
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return nil
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
func runC2Suite(out io.Writer, doc *JObj, passed, failed *int) {
|
|
860
|
+
consumersObj := asObj(mustNode(doc, "consumers"))
|
|
861
|
+
consumerNames := append([]string(nil), consumersObj.Keys...)
|
|
862
|
+
sort.Strings(consumerNames)
|
|
863
|
+
nVec := 0
|
|
864
|
+
for _, cname := range consumerNames {
|
|
865
|
+
c := asObj(consumersObj.Vals[cname])
|
|
866
|
+
vecs, _ := mustNode(c, "vectors").([]JNode)
|
|
867
|
+
nVec += len(vecs)
|
|
868
|
+
}
|
|
869
|
+
fmt.Fprintf(out, "\nc2-catalog-swap.json (v%d) — %d vectors + structural\n", jnumToInt(mustNode(doc, "c2Version")), nVec)
|
|
870
|
+
|
|
871
|
+
// 1) execution per consumer.
|
|
872
|
+
for _, cname := range consumerNames {
|
|
873
|
+
consumer := asObj(consumersObj.Vals[cname])
|
|
874
|
+
vecs, _ := mustNode(consumer, "vectors").([]JNode)
|
|
875
|
+
for _, vn := range vecs {
|
|
876
|
+
v := asObj(vn)
|
|
877
|
+
name := mustStr(v, "name")
|
|
878
|
+
expect := asObj(mustNode(v, "expect"))
|
|
879
|
+
ok := false
|
|
880
|
+
detail := ""
|
|
881
|
+
if gerr := AssertPortableComponentGraph(mustNode(v, "ir")); gerr != nil {
|
|
882
|
+
lineOut(out, false, cname+": "+name, fmt.Sprintf("shared guard rejected consumer IR: %v", gerr))
|
|
883
|
+
bump(false, passed, failed)
|
|
884
|
+
continue
|
|
885
|
+
}
|
|
886
|
+
input := NewObj()
|
|
887
|
+
if inN, has := v.Get("input"); has {
|
|
888
|
+
if inObj := asObj(inN); inObj != nil {
|
|
889
|
+
for _, k := range inObj.Keys {
|
|
890
|
+
dv, err := DecodeValue(inObj.Vals[k])
|
|
891
|
+
if err != nil {
|
|
892
|
+
panic(err)
|
|
893
|
+
}
|
|
894
|
+
input.Set(k, dv)
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
var handlersSpec *JObj
|
|
899
|
+
if hN, has := v.Get("handlers"); has {
|
|
900
|
+
handlersSpec = asObj(hN)
|
|
901
|
+
}
|
|
902
|
+
entry := ""
|
|
903
|
+
if eN, has := v.Get("entry"); has {
|
|
904
|
+
entry, _ = eN.(string)
|
|
905
|
+
}
|
|
906
|
+
result, err := RunBehavior(mustNode(v, "ir"), newScriptedHandlers(handlersSpec), input, entry)
|
|
907
|
+
if err != nil {
|
|
908
|
+
code, coded := behaviorErrCode(err)
|
|
909
|
+
if !coded {
|
|
910
|
+
panic(err)
|
|
911
|
+
}
|
|
912
|
+
detail = fmt.Sprintf("expected a value, got Failure(%s)", code)
|
|
913
|
+
} else {
|
|
914
|
+
ev, _ := expect.Get("value")
|
|
915
|
+
want, derr := DecodeValue(ev)
|
|
916
|
+
if derr != nil {
|
|
917
|
+
panic(derr)
|
|
918
|
+
}
|
|
919
|
+
ok = DeepEquals(result, want)
|
|
920
|
+
if !ok {
|
|
921
|
+
detail = fmt.Sprintf("expected value, got %s", EncodeValue(result))
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
lineOut(out, ok, cname+": "+name, detail)
|
|
925
|
+
bump(ok, passed, failed)
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
// 2) structural: deep-equal modulo catalog names; NOT equal raw.
|
|
930
|
+
a, b := consumerNames[0], consumerNames[1]
|
|
931
|
+
roleOf := func(cname string) map[string]string {
|
|
932
|
+
catalog := asObj(mustNode(asObj(consumersObj.Vals[cname]), "catalog"))
|
|
933
|
+
m := map[string]string{}
|
|
934
|
+
for _, role := range catalog.Keys {
|
|
935
|
+
name, _ := catalog.Vals[role].(string)
|
|
936
|
+
m[name] = role
|
|
937
|
+
}
|
|
938
|
+
return m
|
|
939
|
+
}
|
|
940
|
+
va, _ := mustNode(asObj(consumersObj.Vals[a]), "vectors").([]JNode)
|
|
941
|
+
vb, _ := mustNode(asObj(consumersObj.Vals[b]), "vectors").([]JNode)
|
|
942
|
+
n := len(va)
|
|
943
|
+
if len(vb) < n {
|
|
944
|
+
n = len(vb)
|
|
945
|
+
}
|
|
946
|
+
for i := 0; i < n; i++ {
|
|
947
|
+
ok := false
|
|
948
|
+
detail := ""
|
|
949
|
+
vaObj := asObj(va[i])
|
|
950
|
+
da, errA := DecodeValue(mustNode(vaObj, "ir"))
|
|
951
|
+
db, errB := DecodeValue(mustNode(asObj(vb[i]), "ir"))
|
|
952
|
+
if errA != nil || errB != nil {
|
|
953
|
+
panic(fmt.Sprintf("decode c2 ir: %v / %v", errA, errB))
|
|
954
|
+
}
|
|
955
|
+
if DeepEquals(da, db) {
|
|
956
|
+
detail = "raw IRs must differ before normalization"
|
|
957
|
+
} else if err := c2Normalize(da, roleOf(a)); err != nil {
|
|
958
|
+
detail = err.Error()
|
|
959
|
+
} else if err := c2Normalize(db, roleOf(b)); err != nil {
|
|
960
|
+
detail = err.Error()
|
|
961
|
+
} else {
|
|
962
|
+
ok = DeepEquals(da, db)
|
|
963
|
+
if !ok {
|
|
964
|
+
detail = "IRs must be deep-equal modulo catalog names"
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
lineOut(out, ok, fmt.Sprintf("structural[%d]: IR equal modulo catalog (%s)", i, mustStr(vaObj, "name")), detail)
|
|
968
|
+
bump(ok, passed, failed)
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
func runBehaviorSuite(out io.Writer, doc *JObj, passed, failed *int) {
|
|
973
|
+
vectors := vectorsOf(doc)
|
|
974
|
+
fmt.Fprintf(out, "\nbehavior.json (v%d) — %d vectors\n", jnumToInt(mustNode(doc, "behaviorVersion")), len(vectors))
|
|
975
|
+
for _, vn := range vectors {
|
|
976
|
+
v := asObj(vn)
|
|
977
|
+
name := mustStr(v, "name")
|
|
978
|
+
expect := asObj(mustNode(v, "expect"))
|
|
979
|
+
ir := mustNode(v, "ir")
|
|
980
|
+
|
|
981
|
+
input := NewObj()
|
|
982
|
+
if inN, ok := v.Get("input"); ok {
|
|
983
|
+
if inObj := asObj(inN); inObj != nil {
|
|
984
|
+
for _, k := range inObj.Keys {
|
|
985
|
+
dv, err := DecodeValue(inObj.Vals[k])
|
|
986
|
+
if err != nil {
|
|
987
|
+
panic(err)
|
|
988
|
+
}
|
|
989
|
+
input.Set(k, dv)
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
var handlersSpec *JObj
|
|
995
|
+
if hN, ok := v.Get("handlers"); ok {
|
|
996
|
+
handlersSpec = asObj(hN)
|
|
997
|
+
}
|
|
998
|
+
handlers := newScriptedHandlers(handlersSpec)
|
|
999
|
+
|
|
1000
|
+
entry := ""
|
|
1001
|
+
if eN, ok := v.Get("entry"); ok {
|
|
1002
|
+
entry, _ = eN.(string)
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
ok := false
|
|
1006
|
+
detail := ""
|
|
1007
|
+
result, err := RunBehavior(ir, handlers, input, entry)
|
|
1008
|
+
if err != nil {
|
|
1009
|
+
code, coded := behaviorErrCode(err)
|
|
1010
|
+
if !coded {
|
|
1011
|
+
panic(err)
|
|
1012
|
+
}
|
|
1013
|
+
if fv, has := expect.Get("failure"); has {
|
|
1014
|
+
ok = code == fv.(string)
|
|
1015
|
+
if !ok {
|
|
1016
|
+
detail = fmt.Sprintf("expected Failure(%s), got Failure(%s)", fv, code)
|
|
1017
|
+
} else if dExp, hasD := expect.Get("detail"); hasD {
|
|
1018
|
+
// The structured Error Value (scp-error.md): compare the present fields.
|
|
1019
|
+
var msg string
|
|
1020
|
+
ok, msg = detailMatches(behaviorErrDetail(err), dExp)
|
|
1021
|
+
if !ok {
|
|
1022
|
+
detail = msg
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
} else {
|
|
1026
|
+
detail = fmt.Sprintf("expected a value, got Failure(%s)", code)
|
|
1027
|
+
}
|
|
1028
|
+
} else {
|
|
1029
|
+
if ev, has := expect.Get("value"); has {
|
|
1030
|
+
want, derr := DecodeValue(ev)
|
|
1031
|
+
if derr != nil {
|
|
1032
|
+
panic(derr)
|
|
1033
|
+
}
|
|
1034
|
+
ok = DeepEquals(result, want)
|
|
1035
|
+
if !ok {
|
|
1036
|
+
detail = fmt.Sprintf("expected value, got %s", EncodeValue(result))
|
|
1037
|
+
}
|
|
1038
|
+
} else {
|
|
1039
|
+
fv, _ := expect.Get("failure")
|
|
1040
|
+
detail = fmt.Sprintf("expected Failure(%v), got a value", fv)
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
lineOut(out, ok, name, detail)
|
|
1044
|
+
bump(ok, passed, failed)
|
|
1045
|
+
}
|
|
1046
|
+
}
|