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
package/go/envelope.go
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"strconv"
|
|
6
|
+
"strings"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// EnvelopeFailure is a spec-version validation Failure with a stable code.
|
|
10
|
+
type EnvelopeFailure struct {
|
|
11
|
+
Code string
|
|
12
|
+
Msg string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
func (e *EnvelopeFailure) Error() string { return e.Code + ": " + e.Msg }
|
|
16
|
+
|
|
17
|
+
func envFail(code, msg string) *EnvelopeFailure { return &EnvelopeFailure{Code: code, Msg: msg} }
|
|
18
|
+
|
|
19
|
+
func parseVersion(version, label string) (int, int, error) {
|
|
20
|
+
parts := strings.Split(version, ".")
|
|
21
|
+
if len(parts) != 2 {
|
|
22
|
+
return 0, 0, envFail("MALFORMED_VERSION", fmt.Sprintf("%s: version '%s' must be '<major>.<minor>'", label, version))
|
|
23
|
+
}
|
|
24
|
+
isInt := func(s string) bool {
|
|
25
|
+
if s == "" {
|
|
26
|
+
return false
|
|
27
|
+
}
|
|
28
|
+
for _, c := range s {
|
|
29
|
+
if c < '0' || c > '9' {
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
if !isInt(parts[0]) || !isInt(parts[1]) {
|
|
36
|
+
return 0, 0, envFail("MALFORMED_VERSION", fmt.Sprintf("%s: version '%s' must be two integers '<major>.<minor>'", label, version))
|
|
37
|
+
}
|
|
38
|
+
maj, _ := strconv.Atoi(parts[0])
|
|
39
|
+
min, _ := strconv.Atoi(parts[1])
|
|
40
|
+
return maj, min, nil
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ValidateEnvelope checks an envelope's spec version against supported
|
|
44
|
+
// ("<major>.<minor>"). field defaults to "specVersion", label to "envelope".
|
|
45
|
+
// major must equal supported major; minor must be <= supported minor.
|
|
46
|
+
func ValidateEnvelope(envelope *Obj, supported, field, label string) error {
|
|
47
|
+
if field == "" {
|
|
48
|
+
field = "specVersion"
|
|
49
|
+
}
|
|
50
|
+
if label == "" {
|
|
51
|
+
label = "envelope"
|
|
52
|
+
}
|
|
53
|
+
if envelope == nil {
|
|
54
|
+
return envFail("MISSING_VERSION", label+": not an object")
|
|
55
|
+
}
|
|
56
|
+
rawV, ok := envelope.Get(field)
|
|
57
|
+
raw, isStr := rawV.(string)
|
|
58
|
+
if !ok || !isStr {
|
|
59
|
+
return envFail("MISSING_VERSION", fmt.Sprintf("%s: missing string '%s'", label, field))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
gmaj, gmin, err := parseVersion(raw, label)
|
|
63
|
+
if err != nil {
|
|
64
|
+
return err
|
|
65
|
+
}
|
|
66
|
+
wmaj, wmin, err := parseVersion(supported, "supported")
|
|
67
|
+
if err != nil {
|
|
68
|
+
return err
|
|
69
|
+
}
|
|
70
|
+
if gmaj != wmaj {
|
|
71
|
+
return envFail("UNSUPPORTED_MAJOR", fmt.Sprintf("%s: version %s has major %d but runtime supports major %d", label, raw, gmaj, wmaj))
|
|
72
|
+
}
|
|
73
|
+
if gmin > wmin {
|
|
74
|
+
return envFail("UNSUPPORTED_MINOR", fmt.Sprintf("%s: version %s has minor %d > supported minor %d", label, raw, gmin, wmin))
|
|
75
|
+
}
|
|
76
|
+
return nil
|
|
77
|
+
}
|
package/go/expr.go
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
// expr.go — the Expression IR walker (interpreter endpoint). It consumes parsed
|
|
4
|
+
// IR nodes (JNode / json.Number) and delegates every operator's semantics to the
|
|
5
|
+
// value-core SSoT in coderuntime (exprcore.go) — the same functions the native
|
|
6
|
+
// codegen primitives call, so the interpreter and generated code cannot diverge.
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"encoding/json"
|
|
10
|
+
"fmt"
|
|
11
|
+
"strconv"
|
|
12
|
+
"strings"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
const safeInt = int64(9007199254740991) // 2^53 - 1
|
|
16
|
+
|
|
17
|
+
// EvaluateExpression evaluates an expression-ir.md IR node (a JNode) against a
|
|
18
|
+
// scope (an ordered *Obj of bindings), returning a runtime Value.
|
|
19
|
+
func EvaluateExpression(node JNode, scope *Obj) (Value, error) {
|
|
20
|
+
if scope == nil {
|
|
21
|
+
scope = NewObj()
|
|
22
|
+
}
|
|
23
|
+
return evaluate(node, scope)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
func evaluate(node JNode, scope *Obj) (Value, error) {
|
|
27
|
+
switch n := node.(type) {
|
|
28
|
+
case nil:
|
|
29
|
+
return nil, nil
|
|
30
|
+
case bool:
|
|
31
|
+
return n, nil
|
|
32
|
+
case string:
|
|
33
|
+
return n, nil
|
|
34
|
+
case json.Number:
|
|
35
|
+
return evalNumberLiteral(n)
|
|
36
|
+
case []JNode:
|
|
37
|
+
return nil, exprFail("INVALID_NODE", "bare array is not an expression (use {arr:[...]})")
|
|
38
|
+
case *JObj:
|
|
39
|
+
if n.Len() != 1 {
|
|
40
|
+
return nil, exprFail("INVALID_NODE", fmt.Sprintf("operator node must have exactly one key, got [%s]", strings.Join(n.Keys, ", ")))
|
|
41
|
+
}
|
|
42
|
+
op := n.Keys[0]
|
|
43
|
+
return evalOp(op, n.Vals[op], scope)
|
|
44
|
+
default:
|
|
45
|
+
return nil, exprFail("INVALID_NODE", fmt.Sprintf("invalid node: %T", node))
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
func evalNumberLiteral(n json.Number) (Value, error) {
|
|
50
|
+
s := n.String()
|
|
51
|
+
if !strings.ContainsAny(s, ".eE") {
|
|
52
|
+
i, err := strconv.ParseInt(s, 10, 64)
|
|
53
|
+
if err != nil {
|
|
54
|
+
return nil, exprFail("INVALID_LITERAL", fmt.Sprintf("integral literal %s exceeds safe range; use {int:\"…\"}", s))
|
|
55
|
+
}
|
|
56
|
+
if i < -safeInt || i > safeInt {
|
|
57
|
+
return nil, exprFail("INVALID_LITERAL", fmt.Sprintf("integral literal %d exceeds safe range; use {int:\"…\"}", i))
|
|
58
|
+
}
|
|
59
|
+
return i, nil
|
|
60
|
+
}
|
|
61
|
+
f, err := n.Float64()
|
|
62
|
+
if err != nil {
|
|
63
|
+
return nil, exprFail("INVALID_LITERAL", fmt.Sprintf("bad number literal %s", s))
|
|
64
|
+
}
|
|
65
|
+
return checkFinite(f)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func evalOp(op string, arg JNode, scope *Obj) (Value, error) {
|
|
69
|
+
switch op {
|
|
70
|
+
case "int":
|
|
71
|
+
s, ok := arg.(string)
|
|
72
|
+
if !ok {
|
|
73
|
+
return nil, exprFail("INVALID_NODE", "{int:…} expects a string")
|
|
74
|
+
}
|
|
75
|
+
return intLitValue(s)
|
|
76
|
+
case "float":
|
|
77
|
+
num, ok := arg.(json.Number)
|
|
78
|
+
if !ok {
|
|
79
|
+
return nil, exprFail("INVALID_NODE", "{float:…} expects a number")
|
|
80
|
+
}
|
|
81
|
+
f, err := num.Float64()
|
|
82
|
+
if err != nil {
|
|
83
|
+
return nil, exprFail("INVALID_NODE", "{float:…} expects a number")
|
|
84
|
+
}
|
|
85
|
+
return checkFinite(f)
|
|
86
|
+
case "ref", "refOpt":
|
|
87
|
+
return evalRef(op, arg, scope)
|
|
88
|
+
case "obj":
|
|
89
|
+
o, ok := arg.(*JObj)
|
|
90
|
+
if !ok {
|
|
91
|
+
return nil, exprFail("INVALID_NODE", "{obj:…} expects an object")
|
|
92
|
+
}
|
|
93
|
+
out := NewObj()
|
|
94
|
+
for _, k := range o.Keys {
|
|
95
|
+
if k == ForbiddenObjectKey {
|
|
96
|
+
return nil, exprFail("FORBIDDEN_KEY", fmt.Sprintf("obj key %q is forbidden (fail-closed)", ForbiddenObjectKey))
|
|
97
|
+
}
|
|
98
|
+
v, err := evaluate(o.Vals[k], scope)
|
|
99
|
+
if err != nil {
|
|
100
|
+
return nil, err
|
|
101
|
+
}
|
|
102
|
+
out.Set(k, v)
|
|
103
|
+
}
|
|
104
|
+
return out, nil
|
|
105
|
+
case "arr":
|
|
106
|
+
a, err := argArray(op, arg)
|
|
107
|
+
if err != nil {
|
|
108
|
+
return nil, err
|
|
109
|
+
}
|
|
110
|
+
out := make([]Value, 0, len(a))
|
|
111
|
+
for _, e := range a {
|
|
112
|
+
v, err := evaluate(e, scope)
|
|
113
|
+
if err != nil {
|
|
114
|
+
return nil, err
|
|
115
|
+
}
|
|
116
|
+
out = append(out, v)
|
|
117
|
+
}
|
|
118
|
+
return out, nil
|
|
119
|
+
case "add", "sub", "mul":
|
|
120
|
+
a, b, err := evalBinary(op, arg, scope)
|
|
121
|
+
if err != nil {
|
|
122
|
+
return nil, err
|
|
123
|
+
}
|
|
124
|
+
return arithValues(op, a, b)
|
|
125
|
+
case "neg":
|
|
126
|
+
u, err := argUnary(op, arg)
|
|
127
|
+
if err != nil {
|
|
128
|
+
return nil, err
|
|
129
|
+
}
|
|
130
|
+
a, err := evaluate(u, scope)
|
|
131
|
+
if err != nil {
|
|
132
|
+
return nil, err
|
|
133
|
+
}
|
|
134
|
+
return negValue(a)
|
|
135
|
+
case "div":
|
|
136
|
+
a, b, err := evalBinary(op, arg, scope)
|
|
137
|
+
if err != nil {
|
|
138
|
+
return nil, err
|
|
139
|
+
}
|
|
140
|
+
return divValues(a, b)
|
|
141
|
+
case "mod":
|
|
142
|
+
a, b, err := evalBinary(op, arg, scope)
|
|
143
|
+
if err != nil {
|
|
144
|
+
return nil, err
|
|
145
|
+
}
|
|
146
|
+
return modValues(a, b)
|
|
147
|
+
case "concat":
|
|
148
|
+
a, err := argArray(op, arg)
|
|
149
|
+
if err != nil {
|
|
150
|
+
return nil, err
|
|
151
|
+
}
|
|
152
|
+
// n-ary(min 2 args)。arity<2 は不正 IR(expression-ir.md §2.1/§3/§6)。
|
|
153
|
+
if len(a) < 2 {
|
|
154
|
+
return nil, exprFail("INVALID_NODE", fmt.Sprintf("concat expects >= 2 args, got %d", len(a)))
|
|
155
|
+
}
|
|
156
|
+
var b strings.Builder
|
|
157
|
+
for _, e := range a {
|
|
158
|
+
v, err := evaluate(e, scope)
|
|
159
|
+
if err != nil {
|
|
160
|
+
return nil, err
|
|
161
|
+
}
|
|
162
|
+
s, ok := v.(string)
|
|
163
|
+
if !ok {
|
|
164
|
+
return nil, exprFail("TYPE_MISMATCH", fmt.Sprintf("concat: strings only (got %s; no implicit toString)", typeName(v)))
|
|
165
|
+
}
|
|
166
|
+
b.WriteString(s)
|
|
167
|
+
}
|
|
168
|
+
return b.String(), nil
|
|
169
|
+
case "eq", "ne":
|
|
170
|
+
a, b, err := evalBinary(op, arg, scope)
|
|
171
|
+
if err != nil {
|
|
172
|
+
return nil, err
|
|
173
|
+
}
|
|
174
|
+
return eqNeValues(op, a, b)
|
|
175
|
+
case "lt", "le", "gt", "ge":
|
|
176
|
+
a, b, err := evalBinary(op, arg, scope)
|
|
177
|
+
if err != nil {
|
|
178
|
+
return nil, err
|
|
179
|
+
}
|
|
180
|
+
return compareOp(op, a, b)
|
|
181
|
+
case "and", "or":
|
|
182
|
+
ea, eb, err := rawBinary(op, arg)
|
|
183
|
+
if err != nil {
|
|
184
|
+
return nil, err
|
|
185
|
+
}
|
|
186
|
+
av, err := evaluate(ea, scope)
|
|
187
|
+
if err != nil {
|
|
188
|
+
return nil, err
|
|
189
|
+
}
|
|
190
|
+
a, err := requireBool(av, op)
|
|
191
|
+
if err != nil {
|
|
192
|
+
return nil, err
|
|
193
|
+
}
|
|
194
|
+
if op == "and" && !a {
|
|
195
|
+
return false, nil
|
|
196
|
+
}
|
|
197
|
+
if op == "or" && a {
|
|
198
|
+
return true, nil
|
|
199
|
+
}
|
|
200
|
+
bv, err := evaluate(eb, scope)
|
|
201
|
+
if err != nil {
|
|
202
|
+
return nil, err
|
|
203
|
+
}
|
|
204
|
+
return requireBool(bv, op)
|
|
205
|
+
case "not":
|
|
206
|
+
u, err := argUnary(op, arg)
|
|
207
|
+
if err != nil {
|
|
208
|
+
return nil, err
|
|
209
|
+
}
|
|
210
|
+
v, err := evaluate(u, scope)
|
|
211
|
+
if err != nil {
|
|
212
|
+
return nil, err
|
|
213
|
+
}
|
|
214
|
+
b, err := requireBool(v, "not")
|
|
215
|
+
if err != nil {
|
|
216
|
+
return nil, err
|
|
217
|
+
}
|
|
218
|
+
return !b, nil
|
|
219
|
+
case "coalesce":
|
|
220
|
+
ea, eb, err := rawBinary(op, arg)
|
|
221
|
+
if err != nil {
|
|
222
|
+
return nil, err
|
|
223
|
+
}
|
|
224
|
+
a, err := evaluate(ea, scope)
|
|
225
|
+
if err != nil {
|
|
226
|
+
return nil, err
|
|
227
|
+
}
|
|
228
|
+
if a == nil {
|
|
229
|
+
return evaluate(eb, scope)
|
|
230
|
+
}
|
|
231
|
+
return a, nil
|
|
232
|
+
case "cond":
|
|
233
|
+
a, ok := arg.([]JNode)
|
|
234
|
+
if !ok || len(a) != 3 {
|
|
235
|
+
return nil, exprFail("INVALID_NODE", "cond expects [c, t, e]")
|
|
236
|
+
}
|
|
237
|
+
cv, err := evaluate(a[0], scope)
|
|
238
|
+
if err != nil {
|
|
239
|
+
return nil, err
|
|
240
|
+
}
|
|
241
|
+
c, err := requireBool(cv, "cond")
|
|
242
|
+
if err != nil {
|
|
243
|
+
return nil, err
|
|
244
|
+
}
|
|
245
|
+
if c {
|
|
246
|
+
return evaluate(a[1], scope)
|
|
247
|
+
}
|
|
248
|
+
return evaluate(a[2], scope)
|
|
249
|
+
case "len":
|
|
250
|
+
u, err := argUnary(op, arg)
|
|
251
|
+
if err != nil {
|
|
252
|
+
return nil, err
|
|
253
|
+
}
|
|
254
|
+
v, err := evaluate(u, scope)
|
|
255
|
+
if err != nil {
|
|
256
|
+
return nil, err
|
|
257
|
+
}
|
|
258
|
+
return lenValue(v)
|
|
259
|
+
default:
|
|
260
|
+
return nil, exprFail("UNKNOWN_OP", fmt.Sprintf("unknown operator: %s (fail-closed)", op))
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
func evalRef(op string, arg JNode, scope *Obj) (Value, error) {
|
|
265
|
+
path, err := argArray(op, arg)
|
|
266
|
+
if err != nil {
|
|
267
|
+
return nil, err
|
|
268
|
+
}
|
|
269
|
+
if len(path) == 0 {
|
|
270
|
+
return nil, exprFail("INVALID_NODE", op+" expects a non-empty string path")
|
|
271
|
+
}
|
|
272
|
+
segs := make([]string, len(path))
|
|
273
|
+
for i, p := range path {
|
|
274
|
+
s, ok := p.(string)
|
|
275
|
+
if !ok {
|
|
276
|
+
return nil, exprFail("INVALID_NODE", op+" expects a non-empty string path")
|
|
277
|
+
}
|
|
278
|
+
segs[i] = s
|
|
279
|
+
}
|
|
280
|
+
head := segs[0]
|
|
281
|
+
cur, ok := scope.Get(head)
|
|
282
|
+
if !ok {
|
|
283
|
+
return nil, exprFail("UNKNOWN_BINDING", "unknown binding: "+head)
|
|
284
|
+
}
|
|
285
|
+
for _, seg := range segs[1:] {
|
|
286
|
+
if cur == nil {
|
|
287
|
+
if op == "refOpt" {
|
|
288
|
+
return nil, nil
|
|
289
|
+
}
|
|
290
|
+
return nil, exprFail("NULL_REF", "null intermediate at ."+seg+" (use ?.)")
|
|
291
|
+
}
|
|
292
|
+
o, ok := cur.(*Obj)
|
|
293
|
+
if !ok {
|
|
294
|
+
return nil, exprFail("TYPE_MISMATCH", fmt.Sprintf("cannot access .%s on %s", seg, typeName(cur)))
|
|
295
|
+
}
|
|
296
|
+
next, present := o.Get(seg)
|
|
297
|
+
if !present {
|
|
298
|
+
return nil, exprFail("MISSING_PROP", "missing property ."+seg)
|
|
299
|
+
}
|
|
300
|
+
cur = next
|
|
301
|
+
}
|
|
302
|
+
return cur, nil
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ── arg helpers ──────────────────────────────────────────────────────────────
|
|
306
|
+
func argArray(op string, arg JNode) ([]JNode, error) {
|
|
307
|
+
a, ok := arg.([]JNode)
|
|
308
|
+
if !ok {
|
|
309
|
+
return nil, exprFail("INVALID_NODE", op+" expects an args array")
|
|
310
|
+
}
|
|
311
|
+
return a, nil
|
|
312
|
+
}
|
|
313
|
+
func argUnary(op string, arg JNode) (JNode, error) {
|
|
314
|
+
a, err := argArray(op, arg)
|
|
315
|
+
if err != nil {
|
|
316
|
+
return nil, err
|
|
317
|
+
}
|
|
318
|
+
if len(a) != 1 {
|
|
319
|
+
return nil, exprFail("INVALID_NODE", op+" expects 1 arg")
|
|
320
|
+
}
|
|
321
|
+
return a[0], nil
|
|
322
|
+
}
|
|
323
|
+
func rawBinary(op string, arg JNode) (JNode, JNode, error) {
|
|
324
|
+
a, err := argArray(op, arg)
|
|
325
|
+
if err != nil {
|
|
326
|
+
return nil, nil, err
|
|
327
|
+
}
|
|
328
|
+
if len(a) != 2 {
|
|
329
|
+
return nil, nil, exprFail("INVALID_NODE", op+" expects 2 args")
|
|
330
|
+
}
|
|
331
|
+
return a[0], a[1], nil
|
|
332
|
+
}
|
|
333
|
+
func evalBinary(op string, arg JNode, scope *Obj) (Value, Value, error) {
|
|
334
|
+
ea, eb, err := rawBinary(op, arg)
|
|
335
|
+
if err != nil {
|
|
336
|
+
return nil, nil, err
|
|
337
|
+
}
|
|
338
|
+
a, err := evaluate(ea, scope)
|
|
339
|
+
if err != nil {
|
|
340
|
+
return nil, nil, err
|
|
341
|
+
}
|
|
342
|
+
b, err := evaluate(eb, scope)
|
|
343
|
+
if err != nil {
|
|
344
|
+
return nil, nil, err
|
|
345
|
+
}
|
|
346
|
+
return a, b, nil
|
|
347
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"fmt"
|
|
6
|
+
"math"
|
|
7
|
+
"strconv"
|
|
8
|
+
"strings"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// fingerprint.go — deterministic fingerprint of a portable IR document (common
|
|
12
|
+
// Generator, bc#13 — Go port of ts/src/generator/fingerprint.ts).
|
|
13
|
+
//
|
|
14
|
+
// Generated modules recompute the fingerprint over their embedded IR literal at
|
|
15
|
+
// load time and compare it against the constant baked in at generation time
|
|
16
|
+
// (skew is a LOUD reject — graphddb #208 prepared-artifact discipline).
|
|
17
|
+
//
|
|
18
|
+
// Cross-language identity discipline (must match TS fingerprint.ts / Python
|
|
19
|
+
// fingerprint.py / Rust fingerprint.rs / PHP Fingerprint.php exactly):
|
|
20
|
+
// 1. Lift the JSON document into the Value domain. Integral numbers become
|
|
21
|
+
// int; only non-integral numbers stay float. JSON's data model (RFC 8259)
|
|
22
|
+
// does not distinguish 1 from 1.0, so integral floats are canonicalized to
|
|
23
|
+
// int (TS loses the distinction at JSON.parse time — this specifies that
|
|
24
|
+
// behavior so all languages agree). -0.0 stays float (matches TS).
|
|
25
|
+
// 2. Serialize with CanonicalJSON (all-level code-point key sort —
|
|
26
|
+
// canonical-serialization.md §3).
|
|
27
|
+
// 3. FNV-1a 64 over the UTF-8 bytes. Representation: "fnv1a64:" + 16 lowercase
|
|
28
|
+
// hex digits.
|
|
29
|
+
//
|
|
30
|
+
// Constraint (fail-closed): integral numbers with |n| >= 2^53 are rejected
|
|
31
|
+
// (portable-IR big integers must use the Expression IR {int:"..."} wrapper —
|
|
32
|
+
// expression-ir.md §2.3. TS loses precision at JSON.parse, so allowing them
|
|
33
|
+
// here would let fingerprints diverge across languages).
|
|
34
|
+
|
|
35
|
+
// FingerprintFailure is a fingerprint input invariant violation (fail-closed).
|
|
36
|
+
// Code: UNSAFE_NUMBER | INVALID_VALUE.
|
|
37
|
+
type FingerprintFailure struct {
|
|
38
|
+
Code string
|
|
39
|
+
Msg string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func (e *FingerprintFailure) Error() string { return e.Code + ": " + e.Msg }
|
|
43
|
+
|
|
44
|
+
func fpFail(code, msg string) *FingerprintFailure { return &FingerprintFailure{Code: code, Msg: msg} }
|
|
45
|
+
|
|
46
|
+
const float64Exact = float64(1 << 53) // 2^53
|
|
47
|
+
|
|
48
|
+
func fpUnsafe(text, path string) *FingerprintFailure {
|
|
49
|
+
return fpFail(
|
|
50
|
+
"UNSAFE_NUMBER",
|
|
51
|
+
fmt.Sprintf("integral number %s at %s exceeds float64-exact range; portable IR must use {int:\"...\"} (fail-closed)", text, path),
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// fingerprintValueDomain lifts a parsed JSON document (JNode) into the runtime
|
|
56
|
+
// Value domain with the cross-language number canonicalization described above.
|
|
57
|
+
func fingerprintValueDomain(doc JNode, path string) (Value, error) {
|
|
58
|
+
switch v := doc.(type) {
|
|
59
|
+
case nil:
|
|
60
|
+
return nil, nil
|
|
61
|
+
case bool:
|
|
62
|
+
return v, nil
|
|
63
|
+
case string:
|
|
64
|
+
return v, nil
|
|
65
|
+
case json.Number:
|
|
66
|
+
return fingerprintNumber(v, path)
|
|
67
|
+
case []JNode:
|
|
68
|
+
out := make([]Value, len(v))
|
|
69
|
+
for i, e := range v {
|
|
70
|
+
lifted, err := fingerprintValueDomain(e, fmt.Sprintf("%s[%d]", path, i))
|
|
71
|
+
if err != nil {
|
|
72
|
+
return nil, err
|
|
73
|
+
}
|
|
74
|
+
out[i] = lifted
|
|
75
|
+
}
|
|
76
|
+
return out, nil
|
|
77
|
+
case *JObj:
|
|
78
|
+
out := NewObj()
|
|
79
|
+
for _, k := range v.Keys {
|
|
80
|
+
lifted, err := fingerprintValueDomain(v.Vals[k], path+"."+k)
|
|
81
|
+
if err != nil {
|
|
82
|
+
return nil, err
|
|
83
|
+
}
|
|
84
|
+
out.Set(k, lifted)
|
|
85
|
+
}
|
|
86
|
+
return out, nil
|
|
87
|
+
default:
|
|
88
|
+
return nil, fpFail(
|
|
89
|
+
"INVALID_VALUE",
|
|
90
|
+
fmt.Sprintf("value of type %T at %s cannot appear in a portable IR document (fail-closed)", doc, path),
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
func fingerprintNumber(n json.Number, path string) (Value, error) {
|
|
96
|
+
s := n.String()
|
|
97
|
+
if !strings.ContainsAny(s, ".eE") {
|
|
98
|
+
i, err := strconv.ParseInt(s, 10, 64)
|
|
99
|
+
if err != nil || i <= -(1<<53) || i >= 1<<53 {
|
|
100
|
+
return nil, fpUnsafe(s, path)
|
|
101
|
+
}
|
|
102
|
+
return i, nil
|
|
103
|
+
}
|
|
104
|
+
f, err := n.Float64()
|
|
105
|
+
if err != nil {
|
|
106
|
+
return nil, fpFail("INVALID_VALUE", fmt.Sprintf("bad number literal %s at %s (fail-closed)", s, path))
|
|
107
|
+
}
|
|
108
|
+
// Integral floats canonicalize to int; -0.0 stays float (matches TS/Python).
|
|
109
|
+
if f == math.Trunc(f) && !math.IsInf(f, 0) && !(f == 0 && math.Signbit(f)) {
|
|
110
|
+
if math.Abs(f) >= float64Exact {
|
|
111
|
+
return nil, fpUnsafe(s, path)
|
|
112
|
+
}
|
|
113
|
+
return int64(f), nil
|
|
114
|
+
}
|
|
115
|
+
return f, nil
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// fnv1a64 — FNV-1a 64 (offset 0xcbf29ce484222325, prime 0x100000001b3) as 16
|
|
119
|
+
// lowercase hex digits.
|
|
120
|
+
func fnv1a64(data []byte) string {
|
|
121
|
+
h := uint64(0xcbf29ce484222325)
|
|
122
|
+
const prime = uint64(0x100000001b3)
|
|
123
|
+
for _, b := range data {
|
|
124
|
+
h ^= uint64(b)
|
|
125
|
+
h *= prime
|
|
126
|
+
}
|
|
127
|
+
return fmt.Sprintf("%016x", h)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// FingerprintComponentGraph computes the deterministic fingerprint
|
|
131
|
+
// ("fnv1a64:<16hex>") of a portable IR document (parsed JNode).
|
|
132
|
+
//
|
|
133
|
+
// Structurally identical IR documents (modulo key order and int/integral-float
|
|
134
|
+
// spelling) produce the same fingerprint across all runtime languages. Generated
|
|
135
|
+
// modules use it for their load-time self-check; consumers can use it to compare
|
|
136
|
+
// a live IR against a generated module (#208 prepared-loader pattern).
|
|
137
|
+
func FingerprintComponentGraph(doc JNode) (string, error) {
|
|
138
|
+
lifted, err := fingerprintValueDomain(doc, "$")
|
|
139
|
+
if err != nil {
|
|
140
|
+
return "", err
|
|
141
|
+
}
|
|
142
|
+
s, err := CanonicalJSON(lifted)
|
|
143
|
+
if err != nil {
|
|
144
|
+
return "", err
|
|
145
|
+
}
|
|
146
|
+
return "fnv1a64:" + fnv1a64([]byte(s)), nil
|
|
147
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"strings"
|
|
6
|
+
"testing"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// fingerprint_test.go — FingerprintComponentGraph cross-language parity pins
|
|
10
|
+
// (bc#13 SP2). The pinned value must equal the TS reference
|
|
11
|
+
// (ts/test/generator.test.ts) and the Python/Rust/PHP ports for the same MINI_IR.
|
|
12
|
+
|
|
13
|
+
const fpMiniIR = `{
|
|
14
|
+
"irVersion": 2,
|
|
15
|
+
"exprVersion": 2,
|
|
16
|
+
"components": [
|
|
17
|
+
{
|
|
18
|
+
"name": "getA",
|
|
19
|
+
"inputPorts": { "id": { "type": "string", "required": true } },
|
|
20
|
+
"body": [{ "id": "a", "component": "GetItem", "ports": { "PK": { "concat": ["A#", { "ref": ["id"] }] } } }],
|
|
21
|
+
"output": { "ref": ["a"] },
|
|
22
|
+
"plan": { "groups": [[0]], "concurrency": 16 }
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}`
|
|
26
|
+
|
|
27
|
+
func mustParse(t *testing.T, s string) JNode {
|
|
28
|
+
t.Helper()
|
|
29
|
+
n, err := ParseJSONOrdered([]byte(s))
|
|
30
|
+
if err != nil {
|
|
31
|
+
t.Fatalf("parse: %v", err)
|
|
32
|
+
}
|
|
33
|
+
return n
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
func TestFingerprintPinnedCrossLanguageValue(t *testing.T) {
|
|
37
|
+
got, err := FingerprintComponentGraph(mustParse(t, fpMiniIR))
|
|
38
|
+
if err != nil {
|
|
39
|
+
t.Fatalf("fingerprint: %v", err)
|
|
40
|
+
}
|
|
41
|
+
const want = "fnv1a64:9424ef4d0842c841" // pinned in ts/test/generator.test.ts
|
|
42
|
+
if got != want {
|
|
43
|
+
t.Fatalf("fingerprint mismatch: got %s, want %s", got, want)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func TestFingerprintKeyOrderInsensitiveAndIntFloatCanonicalization(t *testing.T) {
|
|
48
|
+
a, err := FingerprintComponentGraph(mustParse(t, `{"a": 1, "b": [1, 2]}`))
|
|
49
|
+
if err != nil {
|
|
50
|
+
t.Fatal(err)
|
|
51
|
+
}
|
|
52
|
+
b, err := FingerprintComponentGraph(mustParse(t, `{"b": [1.0, 2.0], "a": 1.0}`))
|
|
53
|
+
if err != nil {
|
|
54
|
+
t.Fatal(err)
|
|
55
|
+
}
|
|
56
|
+
if a != b {
|
|
57
|
+
t.Fatalf("key order / integral-float spelling must not affect the fingerprint: %s != %s", a, b)
|
|
58
|
+
}
|
|
59
|
+
c, err := FingerprintComponentGraph(mustParse(t, `{"a": 1.5, "b": [1, 2]}`))
|
|
60
|
+
if err != nil {
|
|
61
|
+
t.Fatal(err)
|
|
62
|
+
}
|
|
63
|
+
if a == c {
|
|
64
|
+
t.Fatal("structural changes must change the fingerprint")
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func TestFingerprintUnsafeNumberFailClosed(t *testing.T) {
|
|
69
|
+
for _, src := range []string{`{"n": 9007199254740993}`, `{"n": 1e300}`} {
|
|
70
|
+
_, err := FingerprintComponentGraph(mustParse(t, src))
|
|
71
|
+
fe, ok := err.(*FingerprintFailure)
|
|
72
|
+
if !ok || fe.Code != "UNSAFE_NUMBER" {
|
|
73
|
+
t.Fatalf("%s: want FingerprintFailure(UNSAFE_NUMBER), got %v", src, err)
|
|
74
|
+
}
|
|
75
|
+
if !strings.Contains(fe.Msg, "fail-closed") {
|
|
76
|
+
t.Fatalf("failure message must be loud: %q", fe.Msg)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func TestFingerprintNegativeZeroStaysFloat(t *testing.T) {
|
|
82
|
+
// -0.0 must not collapse into int 0 (matches TS Object.is(n, -0) handling).
|
|
83
|
+
neg, err := FingerprintComponentGraph(JObjOf("n", json.Number("-0.0")))
|
|
84
|
+
if err != nil {
|
|
85
|
+
t.Fatal(err)
|
|
86
|
+
}
|
|
87
|
+
zero, err := FingerprintComponentGraph(JObjOf("n", json.Number("0")))
|
|
88
|
+
if err != nil {
|
|
89
|
+
t.Fatal(err)
|
|
90
|
+
}
|
|
91
|
+
if neg == zero {
|
|
92
|
+
t.Fatal("-0.0 must fingerprint differently from int 0")
|
|
93
|
+
}
|
|
94
|
+
}
|
package/go/go.mod
ADDED