behavior-contracts 0.12.1 → 0.13.1
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 +55 -2
- package/dist/commands/ts-extract.d.ts.map +1 -1
- package/dist/commands/ts-extract.js +9 -0
- package/dist/commands/ts-extract.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 +3 -1
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
// plan_parallel_test.go — bc#23: bounded parallel stage execution.
|
|
4
|
+
//
|
|
5
|
+
// Proofs (run under -race in CI):
|
|
6
|
+
// 1. overlap — siblings in one stage actually run concurrently (channel barrier;
|
|
7
|
+
// sequential execution would deadlock, detected via timeout → test failure).
|
|
8
|
+
// 2. bounded — peak in-flight equals the plan.Concurrency bound, never above.
|
|
9
|
+
// 3. identical — failure identity (code+message) and results match forced-sequential
|
|
10
|
+
// execution regardless of completion order. (Vector-level determinism is proven
|
|
11
|
+
// by api_test.go running the full conformance suite, whose concurrency>1 vectors
|
|
12
|
+
// now take the parallel path.)
|
|
13
|
+
// 4. behavior — RunBehavior with a concurrency>1 plan calls handlers concurrently
|
|
14
|
+
// and still merges results deterministically (exercises the scope mutex).
|
|
15
|
+
|
|
16
|
+
import (
|
|
17
|
+
"sync"
|
|
18
|
+
"testing"
|
|
19
|
+
"time"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
func parallelOps() []OpSpec {
|
|
23
|
+
return []OpSpec{
|
|
24
|
+
{ID: "root", Parent: -1},
|
|
25
|
+
{ID: "sibA", Parent: 0},
|
|
26
|
+
{ID: "sibB", Parent: 0},
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
func TestRunPlanSiblingsOverlapWithBarrier(t *testing.T) {
|
|
31
|
+
plan := &ExecutionPlanSpec{Groups: [][]int{{0}, {1, 2}}, Concurrency: 2}
|
|
32
|
+
ops := parallelOps()
|
|
33
|
+
|
|
34
|
+
arrived := make(chan struct{}, 2)
|
|
35
|
+
release := make(chan struct{})
|
|
36
|
+
var once sync.Once
|
|
37
|
+
var count int
|
|
38
|
+
var mu sync.Mutex
|
|
39
|
+
|
|
40
|
+
exec := func(op OpSpec, _ Value) ExecOutcome {
|
|
41
|
+
if op.Parent < 0 {
|
|
42
|
+
return OkOutcome(NewObj())
|
|
43
|
+
}
|
|
44
|
+
mu.Lock()
|
|
45
|
+
count++
|
|
46
|
+
if count == 2 {
|
|
47
|
+
once.Do(func() { close(release) })
|
|
48
|
+
}
|
|
49
|
+
mu.Unlock()
|
|
50
|
+
arrived <- struct{}{}
|
|
51
|
+
select {
|
|
52
|
+
case <-release: // both siblings in flight → proceed
|
|
53
|
+
case <-time.After(2 * time.Second):
|
|
54
|
+
t.Error("siblings never overlapped (sequential execution?)")
|
|
55
|
+
}
|
|
56
|
+
return OkOutcome(op.ID + "-done")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
res, err := RunPlan(plan, ops, exec)
|
|
60
|
+
if err != nil {
|
|
61
|
+
t.Fatalf("unexpected error: %v", err)
|
|
62
|
+
}
|
|
63
|
+
if len(res.Executed) != 3 {
|
|
64
|
+
t.Fatalf("executed = %v", res.Executed)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func TestRunPlanInflightBoundedByConcurrency(t *testing.T) {
|
|
69
|
+
ops := []OpSpec{{ID: "root", Parent: -1}}
|
|
70
|
+
for _, id := range []string{"sib1", "sib2", "sib3", "sib4"} {
|
|
71
|
+
ops = append(ops, OpSpec{ID: id, Parent: 0})
|
|
72
|
+
}
|
|
73
|
+
plan := &ExecutionPlanSpec{Groups: [][]int{{0}, {1, 2, 3, 4}}, Concurrency: 2}
|
|
74
|
+
|
|
75
|
+
var mu sync.Mutex
|
|
76
|
+
inflight, peak := 0, 0
|
|
77
|
+
exec := func(op OpSpec, _ Value) ExecOutcome {
|
|
78
|
+
if op.Parent < 0 {
|
|
79
|
+
return OkOutcome(NewObj())
|
|
80
|
+
}
|
|
81
|
+
mu.Lock()
|
|
82
|
+
inflight++
|
|
83
|
+
if inflight > peak {
|
|
84
|
+
peak = inflight
|
|
85
|
+
}
|
|
86
|
+
mu.Unlock()
|
|
87
|
+
time.Sleep(20 * time.Millisecond)
|
|
88
|
+
mu.Lock()
|
|
89
|
+
inflight--
|
|
90
|
+
mu.Unlock()
|
|
91
|
+
return OkOutcome(op.ID)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if _, err := RunPlan(plan, ops, exec); err != nil {
|
|
95
|
+
t.Fatalf("unexpected error: %v", err)
|
|
96
|
+
}
|
|
97
|
+
if peak != 2 {
|
|
98
|
+
t.Fatalf("peak in-flight %d != concurrency bound 2", peak)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Concurrency = 1 must not overlap at all (sequential fallback).
|
|
102
|
+
inflight, peak = 0, 0
|
|
103
|
+
seqPlan := &ExecutionPlanSpec{Groups: plan.Groups, Concurrency: 1}
|
|
104
|
+
if _, err := RunPlan(seqPlan, ops, exec); err != nil {
|
|
105
|
+
t.Fatalf("unexpected error: %v", err)
|
|
106
|
+
}
|
|
107
|
+
if peak != 1 {
|
|
108
|
+
t.Fatalf("concurrency=1 must not overlap (peak %d)", peak)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
func TestRunPlanFailureIdentityMatchesDeclarationOrder(t *testing.T) {
|
|
113
|
+
// sibC (index 3) fails instantly; sibB (index 2) fails late. The committed
|
|
114
|
+
// failure must be sibB's OP_FAILED (first failing member in declaration order),
|
|
115
|
+
// with a message identical to forced-sequential execution.
|
|
116
|
+
ops := []OpSpec{
|
|
117
|
+
{ID: "root", Parent: -1},
|
|
118
|
+
{ID: "sibA", Parent: 0},
|
|
119
|
+
{ID: "sibB", Parent: 0},
|
|
120
|
+
{ID: "sibC", Parent: 0},
|
|
121
|
+
}
|
|
122
|
+
groups := [][]int{{0}, {1, 2, 3}}
|
|
123
|
+
|
|
124
|
+
mkExec := func(slow bool) Exec {
|
|
125
|
+
return func(op OpSpec, _ Value) ExecOutcome {
|
|
126
|
+
switch op.ID {
|
|
127
|
+
case "root":
|
|
128
|
+
return OkOutcome(NewObj())
|
|
129
|
+
case "sibA":
|
|
130
|
+
if slow {
|
|
131
|
+
time.Sleep(5 * time.Millisecond)
|
|
132
|
+
}
|
|
133
|
+
return OkOutcome("a")
|
|
134
|
+
case "sibB":
|
|
135
|
+
if slow {
|
|
136
|
+
time.Sleep(30 * time.Millisecond)
|
|
137
|
+
}
|
|
138
|
+
return ErrOutcome("b-broke")
|
|
139
|
+
default:
|
|
140
|
+
return ErrOutcome("c-broke-first")
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
_, seqErr := RunPlan(&ExecutionPlanSpec{Groups: groups, Concurrency: 1}, ops, mkExec(false))
|
|
146
|
+
if seqErr == nil {
|
|
147
|
+
t.Fatal("sequential reference must fail")
|
|
148
|
+
}
|
|
149
|
+
_, parErr := RunPlan(&ExecutionPlanSpec{Groups: groups, Concurrency: 3}, ops, mkExec(true))
|
|
150
|
+
if parErr == nil {
|
|
151
|
+
t.Fatal("parallel run must fail")
|
|
152
|
+
}
|
|
153
|
+
if seqErr.Error() != parErr.Error() {
|
|
154
|
+
t.Fatalf("failure identity diverged:\n seq: %v\n par: %v", seqErr, parErr)
|
|
155
|
+
}
|
|
156
|
+
pf, ok := parErr.(*PlanFailure)
|
|
157
|
+
if !ok || pf.Code != "OP_FAILED" {
|
|
158
|
+
t.Fatalf("expected OP_FAILED, got %v", parErr)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
func TestRunPlanIntraStageDependencyFallsBackToSequential(t *testing.T) {
|
|
163
|
+
// An (invalid, §1-violating) plan that groups a child with its parent must be
|
|
164
|
+
// executed sequentially, preserving the sequential visibility semantics.
|
|
165
|
+
ops := []OpSpec{
|
|
166
|
+
{ID: "root", Parent: -1},
|
|
167
|
+
{ID: "child", Parent: 0, BindField: "k", HasBindField: true},
|
|
168
|
+
}
|
|
169
|
+
plan := &ExecutionPlanSpec{Groups: [][]int{{0, 1}}, Concurrency: 8}
|
|
170
|
+
|
|
171
|
+
var mu sync.Mutex
|
|
172
|
+
inflight, peak := 0, 0
|
|
173
|
+
exec := func(op OpSpec, _ Value) ExecOutcome {
|
|
174
|
+
mu.Lock()
|
|
175
|
+
inflight++
|
|
176
|
+
if inflight > peak {
|
|
177
|
+
peak = inflight
|
|
178
|
+
}
|
|
179
|
+
mu.Unlock()
|
|
180
|
+
time.Sleep(10 * time.Millisecond)
|
|
181
|
+
mu.Lock()
|
|
182
|
+
inflight--
|
|
183
|
+
mu.Unlock()
|
|
184
|
+
if op.Parent < 0 {
|
|
185
|
+
o := NewObj()
|
|
186
|
+
o.Set("k", "v")
|
|
187
|
+
return OkOutcome(o)
|
|
188
|
+
}
|
|
189
|
+
return OkOutcome("child-done")
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
res, err := RunPlan(plan, ops, exec)
|
|
193
|
+
if err != nil {
|
|
194
|
+
t.Fatalf("unexpected error: %v", err)
|
|
195
|
+
}
|
|
196
|
+
if peak != 1 {
|
|
197
|
+
t.Fatalf("intra-stage dependency must fall back to sequential (peak %d)", peak)
|
|
198
|
+
}
|
|
199
|
+
if len(res.Executed) != 2 {
|
|
200
|
+
t.Fatalf("executed = %v (child must see the parent's fresh result)", res.Executed)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// barrierExec is a ComponentExec whose two sibling handlers block until both are
|
|
205
|
+
// in flight (proving RunBehavior dispatches stage members concurrently) and which
|
|
206
|
+
// records the peak number of concurrent handler calls.
|
|
207
|
+
type barrierExec struct {
|
|
208
|
+
t *testing.T
|
|
209
|
+
mu sync.Mutex
|
|
210
|
+
count int
|
|
211
|
+
peak int
|
|
212
|
+
release chan struct{}
|
|
213
|
+
once sync.Once
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
func (b *barrierExec) Exec(component string, ports *Obj, _ Value) (ExecOutcome, bool) {
|
|
217
|
+
if component == "Root" {
|
|
218
|
+
o := NewObj()
|
|
219
|
+
o.Set("a", "A")
|
|
220
|
+
o.Set("b", "B")
|
|
221
|
+
return OkOutcome(o), true
|
|
222
|
+
}
|
|
223
|
+
b.mu.Lock()
|
|
224
|
+
b.count++
|
|
225
|
+
if b.count > b.peak {
|
|
226
|
+
b.peak = b.count
|
|
227
|
+
}
|
|
228
|
+
if b.count == 2 {
|
|
229
|
+
b.once.Do(func() { close(b.release) })
|
|
230
|
+
}
|
|
231
|
+
b.mu.Unlock()
|
|
232
|
+
select {
|
|
233
|
+
case <-b.release:
|
|
234
|
+
case <-time.After(2 * time.Second):
|
|
235
|
+
b.t.Error("sibling handlers never overlapped")
|
|
236
|
+
}
|
|
237
|
+
v, _ := ports.Get("tag")
|
|
238
|
+
b.mu.Lock()
|
|
239
|
+
b.count--
|
|
240
|
+
b.mu.Unlock()
|
|
241
|
+
return OkOutcome(v), true
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
func TestRunBehaviorParallelStageMergesDeterministically(t *testing.T) {
|
|
245
|
+
irJSON := `{
|
|
246
|
+
"irVersion": 1, "exprVersion": 2,
|
|
247
|
+
"components": [{
|
|
248
|
+
"name": "pair", "inputPorts": {},
|
|
249
|
+
"body": [
|
|
250
|
+
{"id": "r", "component": "Root", "ports": {}},
|
|
251
|
+
{"id": "x", "component": "Leaf", "parent": "r", "bindField": "a", "ports": {"tag": "left"}},
|
|
252
|
+
{"id": "y", "component": "Leaf", "parent": "r", "bindField": "b", "ports": {"tag": "right"}}
|
|
253
|
+
],
|
|
254
|
+
"output": {"obj": {"x": {"ref": ["x"]}, "y": {"ref": ["y"]}}},
|
|
255
|
+
"plan": {"groups": [[0], [1, 2]], "concurrency": 2}
|
|
256
|
+
}]
|
|
257
|
+
}`
|
|
258
|
+
ir, err := ParseJSONOrdered([]byte(irJSON))
|
|
259
|
+
if err != nil {
|
|
260
|
+
t.Fatalf("decode: %v", err)
|
|
261
|
+
}
|
|
262
|
+
h := &barrierExec{t: t, release: make(chan struct{})}
|
|
263
|
+
out, err := RunBehavior(ir, h, nil, "")
|
|
264
|
+
if err != nil {
|
|
265
|
+
t.Fatalf("RunBehavior: %v", err)
|
|
266
|
+
}
|
|
267
|
+
obj, ok := out.(*Obj)
|
|
268
|
+
if !ok {
|
|
269
|
+
t.Fatalf("output not an object: %#v", out)
|
|
270
|
+
}
|
|
271
|
+
x, _ := obj.Get("x")
|
|
272
|
+
y, _ := obj.Get("y")
|
|
273
|
+
if x != "left" || y != "right" {
|
|
274
|
+
t.Fatalf("merge mismatch: x=%v y=%v", x, y)
|
|
275
|
+
}
|
|
276
|
+
if h.peak != 2 {
|
|
277
|
+
t.Fatalf("sibling handlers peak %d != 2", h.peak)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
// primitives_test.go — codegen primitive surface (bc#36) のユニットテスト。
|
|
4
|
+
//
|
|
5
|
+
// 各式演算子 primitive が「正常系 + fail-closed エッジ」で EvaluateExpression(SSoT)と
|
|
6
|
+
// 完全一致することを差分検証する。さらに MapWithConcurrency(順序保存・bounded)/
|
|
7
|
+
// unproduced helper を検証する。
|
|
8
|
+
|
|
9
|
+
import (
|
|
10
|
+
"sync"
|
|
11
|
+
"sync/atomic"
|
|
12
|
+
"testing"
|
|
13
|
+
"time"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
// parsePrim は JSON 文字列を IR ノード(JNode)へ復元する(int/float 区別を保つ)。
|
|
17
|
+
func parsePrim(t *testing.T, s string) JNode {
|
|
18
|
+
t.Helper()
|
|
19
|
+
n, err := ParseJSONOrdered([]byte(s))
|
|
20
|
+
if err != nil {
|
|
21
|
+
t.Fatalf("parse %q: %v", s, err)
|
|
22
|
+
}
|
|
23
|
+
return n
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// sameOK は primitive 結果が同一 IR を EvaluateExpression に流した結果と一致することを主張する。
|
|
27
|
+
func sameOK(t *testing.T, got Value, gotErr error, node string, scope *Obj) {
|
|
28
|
+
t.Helper()
|
|
29
|
+
want, wantErr := EvaluateExpression(parsePrim(t, node), scope)
|
|
30
|
+
if (gotErr == nil) != (wantErr == nil) {
|
|
31
|
+
t.Fatalf("ok/err disposition mismatch for %s: primErr=%v evalErr=%v", node, gotErr, wantErr)
|
|
32
|
+
}
|
|
33
|
+
if gotErr == nil && !DeepEquals(got, want) {
|
|
34
|
+
t.Fatalf("value mismatch for %s: %v != %v", node, got, want)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// bothFail は両経路が同一 Failure code で fail-closed することを主張する。
|
|
39
|
+
func bothFail(t *testing.T, gotErr error, node string, scope *Obj) {
|
|
40
|
+
t.Helper()
|
|
41
|
+
if gotErr == nil {
|
|
42
|
+
t.Fatalf("primitive did not fail for %s", node)
|
|
43
|
+
}
|
|
44
|
+
pe, ok := gotErr.(*ExprFailure)
|
|
45
|
+
if !ok {
|
|
46
|
+
t.Fatalf("primitive error not ExprFailure: %T", gotErr)
|
|
47
|
+
}
|
|
48
|
+
_, wantErr := EvaluateExpression(parsePrim(t, node), scope)
|
|
49
|
+
ee, ok := wantErr.(*ExprFailure)
|
|
50
|
+
if !ok {
|
|
51
|
+
t.Fatalf("eval error not ExprFailure: %T", wantErr)
|
|
52
|
+
}
|
|
53
|
+
if pe.Code != ee.Code {
|
|
54
|
+
t.Fatalf("Failure code mismatch: prim=%s eval=%s", pe.Code, ee.Code)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// litT は評価済み Value を返す被演算子 thunk(native primitive のテスト用)。
|
|
59
|
+
func litT(v Value) ExprFn { return func(*Obj) (Value, error) { return v, nil } }
|
|
60
|
+
|
|
61
|
+
// failT は評価されたら fail する thunk(短絡の pin 用)。
|
|
62
|
+
func failT() ExprFn {
|
|
63
|
+
return func(*Obj) (Value, error) { return nil, exprFail("INVALID_LITERAL", "must not be evaluated") }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// bc no-ir: native primitive は被演算子を評価済み Value(thunk 経由)で受ける。結果が同じ意味の
|
|
67
|
+
// IR を EvaluateExpression に流した結果と一致することを差分検証する(意味論 SSoT は evaluate 一本)。
|
|
68
|
+
// ref/refOpt/concat は生成 module 内 native helper なので codegen conformance 側で pin する。
|
|
69
|
+
|
|
70
|
+
func TestNativeLiteralsAndConstruct(t *testing.T) {
|
|
71
|
+
sc := NewObj()
|
|
72
|
+
v, e := IntLitNative("42")
|
|
73
|
+
sameOK(t, v, e, `{"int":"42"}`, sc)
|
|
74
|
+
v, e = FloatLitNative(3.5)
|
|
75
|
+
sameOK(t, v, e, `{"float":3.5}`, sc)
|
|
76
|
+
// bare number 分類(整数値→int / 小数→float)。
|
|
77
|
+
v, e = NumberLitNative(42)
|
|
78
|
+
sameOK(t, v, e, `42`, sc)
|
|
79
|
+
v, e = NumberLitNative(2.5)
|
|
80
|
+
sameOK(t, v, e, `2.5`, sc)
|
|
81
|
+
v, e = ObjNative([]string{"x"}, []ExprFn{litT(int64(1))}, sc)
|
|
82
|
+
sameOK(t, v, e, `{"obj":{"x":{"int":"1"}}}`, sc)
|
|
83
|
+
v, e = ArrNative([]ExprFn{litT(int64(1)), litT(int64(2))}, sc)
|
|
84
|
+
sameOK(t, v, e, `{"arr":[{"int":"1"},{"int":"2"}]}`, sc)
|
|
85
|
+
// __proto__ は fail-closed(FORBIDDEN_KEY)。
|
|
86
|
+
_, e = ObjNative([]string{"__proto__"}, []ExprFn{litT(int64(1))}, sc)
|
|
87
|
+
bothFail(t, e, `{"obj":{"__proto__":{"int":"1"}}}`, sc)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
func TestNativeArithmetic(t *testing.T) {
|
|
91
|
+
sc := NewObj()
|
|
92
|
+
v, e := AddNative(litT(int64(1)), litT(int64(2)), sc)
|
|
93
|
+
sameOK(t, v, e, `{"add":[{"int":"1"},{"int":"2"}]}`, sc)
|
|
94
|
+
v, e = SubNative(litT(int64(5)), litT(int64(3)), sc)
|
|
95
|
+
sameOK(t, v, e, `{"sub":[{"int":"5"},{"int":"3"}]}`, sc)
|
|
96
|
+
v, e = MulNative(litT(int64(4)), litT(int64(6)), sc)
|
|
97
|
+
sameOK(t, v, e, `{"mul":[{"int":"4"},{"int":"6"}]}`, sc)
|
|
98
|
+
v, e = NegNative(litT(int64(9)), sc)
|
|
99
|
+
sameOK(t, v, e, `{"neg":[{"int":"9"}]}`, sc)
|
|
100
|
+
v, e = DivNative(litT(int64(7)), litT(int64(2)), sc)
|
|
101
|
+
sameOK(t, v, e, `{"div":[{"int":"7"},{"int":"2"}]}`, sc)
|
|
102
|
+
v, e = ModNative(litT(int64(7)), litT(int64(3)), sc)
|
|
103
|
+
sameOK(t, v, e, `{"mod":[{"int":"7"},{"int":"3"}]}`, sc)
|
|
104
|
+
|
|
105
|
+
_, e = AddNative(litT(int64(9223372036854775807)), litT(int64(1)), sc)
|
|
106
|
+
bothFail(t, e, `{"add":[{"int":"9223372036854775807"},{"int":"1"}]}`, sc)
|
|
107
|
+
_, e = ModNative(litT(int64(1)), litT(int64(0)), sc)
|
|
108
|
+
bothFail(t, e, `{"mod":[{"int":"1"},{"int":"0"}]}`, sc)
|
|
109
|
+
_, e = AddNative(litT(int64(1)), litT(2.5), sc)
|
|
110
|
+
bothFail(t, e, `{"add":[{"int":"1"},{"float":2.5}]}`, sc)
|
|
111
|
+
_, e = DivNative(litT(int64(9007199254740993)), litT(int64(1)), sc)
|
|
112
|
+
bothFail(t, e, `{"div":[{"int":"9007199254740993"},{"int":"1"}]}`, sc)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
func TestNativeComparisonBoolLen(t *testing.T) {
|
|
116
|
+
sc := NewObj()
|
|
117
|
+
v, e := EqNative(litT(int64(1)), litT(int64(1)), sc)
|
|
118
|
+
sameOK(t, v, e, `{"eq":[{"int":"1"},{"int":"1"}]}`, sc)
|
|
119
|
+
v, e = NeNative(litT(int64(1)), litT(int64(2)), sc)
|
|
120
|
+
sameOK(t, v, e, `{"ne":[{"int":"1"},{"int":"2"}]}`, sc)
|
|
121
|
+
v, e = LtNative(litT("a"), litT("b"), sc)
|
|
122
|
+
sameOK(t, v, e, `{"lt":["a","b"]}`, sc)
|
|
123
|
+
v, e = LeNative(litT("a"), litT("a"), sc)
|
|
124
|
+
sameOK(t, v, e, `{"le":["a","a"]}`, sc)
|
|
125
|
+
v, e = GtNative(litT(int64(3)), litT(int64(2)), sc)
|
|
126
|
+
sameOK(t, v, e, `{"gt":[{"int":"3"},{"int":"2"}]}`, sc)
|
|
127
|
+
v, e = GeNative(litT(int64(2)), litT(int64(2)), sc)
|
|
128
|
+
sameOK(t, v, e, `{"ge":[{"int":"2"},{"int":"2"}]}`, sc)
|
|
129
|
+
v, e = NotNative(litT(true), sc)
|
|
130
|
+
sameOK(t, v, e, `{"not":[true]}`, sc)
|
|
131
|
+
v, e = AndNative(litT(true), litT(false), sc)
|
|
132
|
+
sameOK(t, v, e, `{"and":[true,false]}`, sc)
|
|
133
|
+
v, e = OrNative(litT(false), litT(true), sc)
|
|
134
|
+
sameOK(t, v, e, `{"or":[false,true]}`, sc)
|
|
135
|
+
v, e = CoalesceNative(litT(nil), litT("fb"), sc)
|
|
136
|
+
sameOK(t, v, e, `{"coalesce":[null,"fb"]}`, sc)
|
|
137
|
+
v, e = CondNative(litT(true), litT("t"), litT("e"), sc)
|
|
138
|
+
sameOK(t, v, e, `{"cond":[true,"t","e"]}`, sc)
|
|
139
|
+
v, e = LenNative(litT([]Value{true, false}), sc)
|
|
140
|
+
sameOK(t, v, e, `{"len":[{"arr":[true,false]}]}`, sc)
|
|
141
|
+
|
|
142
|
+
_, e = LenNative(litT("nope"), sc)
|
|
143
|
+
bothFail(t, e, `{"len":["nope"]}`, sc)
|
|
144
|
+
_, e = CondNative(litT(int64(1)), litT("t"), litT("e"), sc)
|
|
145
|
+
bothFail(t, e, `{"cond":[{"int":"1"},"t","e"]}`, sc)
|
|
146
|
+
_, e = NotNative(litT(int64(1)), sc)
|
|
147
|
+
bothFail(t, e, `{"not":[{"int":"1"}]}`, sc)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
func TestNativeShortCircuit(t *testing.T) {
|
|
151
|
+
sc := NewObj()
|
|
152
|
+
// 短絡側の被演算子(failT)は評価されてはならない。
|
|
153
|
+
v, err := AndNative(litT(false), failT(), sc)
|
|
154
|
+
if err != nil || v != false {
|
|
155
|
+
t.Fatalf("and short-circuit: %v %v", v, err)
|
|
156
|
+
}
|
|
157
|
+
v, err = OrNative(litT(true), failT(), sc)
|
|
158
|
+
if err != nil || v != true {
|
|
159
|
+
t.Fatalf("or short-circuit: %v %v", v, err)
|
|
160
|
+
}
|
|
161
|
+
v, err = CondNative(litT(true), litT("ok"), failT(), sc)
|
|
162
|
+
if err != nil || v != "ok" {
|
|
163
|
+
t.Fatalf("cond short-circuit: %v %v", v, err)
|
|
164
|
+
}
|
|
165
|
+
v, err = CoalesceNative(litT("present"), failT(), sc)
|
|
166
|
+
if err != nil || v != "present" {
|
|
167
|
+
t.Fatalf("coalesce short-circuit: %v %v", v, err)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
func TestPrimMapWithConcurrency(t *testing.T) {
|
|
172
|
+
items := []int{0, 1, 2, 3, 4, 5, 6, 7}
|
|
173
|
+
var inFlight, maxInFlight int64
|
|
174
|
+
var mu sync.Mutex
|
|
175
|
+
out := MapWithConcurrency(items, 3, func(x int, _ int) int {
|
|
176
|
+
cur := atomic.AddInt64(&inFlight, 1)
|
|
177
|
+
mu.Lock()
|
|
178
|
+
if cur > maxInFlight {
|
|
179
|
+
maxInFlight = cur
|
|
180
|
+
}
|
|
181
|
+
mu.Unlock()
|
|
182
|
+
time.Sleep(5 * time.Millisecond)
|
|
183
|
+
atomic.AddInt64(&inFlight, -1)
|
|
184
|
+
return x * 10
|
|
185
|
+
})
|
|
186
|
+
for i, x := range items {
|
|
187
|
+
if out[i] != x*10 {
|
|
188
|
+
t.Fatalf("order not preserved at %d: %d", i, out[i])
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if maxInFlight > 3 {
|
|
192
|
+
t.Fatalf("in-flight %d exceeded concurrency 3", maxInFlight)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
func TestPrimMapWithConcurrencyDegenerate(t *testing.T) {
|
|
197
|
+
out := MapWithConcurrency([]int{1, 2, 3}, 1, func(x int, _ int) int { return x + 1 })
|
|
198
|
+
if len(out) != 3 || out[0] != 2 || out[1] != 3 || out[2] != 4 {
|
|
199
|
+
t.Fatalf("degenerate seq: %v", out)
|
|
200
|
+
}
|
|
201
|
+
empty := MapWithConcurrency([]int{}, 4, func(x int, _ int) int { return x })
|
|
202
|
+
if len(empty) != 0 {
|
|
203
|
+
t.Fatalf("empty: %v", empty)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
func TestPrimSkipUnproduced(t *testing.T) {
|
|
208
|
+
// 未生成の既定は nil で、注記の**不在**がそれを意味する(`"single"` という 2 つ目の綴りは削除済み)。
|
|
209
|
+
if UnproducedValue("") != nil {
|
|
210
|
+
t.Fatal("the default unproduced value should be nil")
|
|
211
|
+
}
|
|
212
|
+
conn := SkipConnection()
|
|
213
|
+
o, ok := conn.(*Obj)
|
|
214
|
+
if !ok {
|
|
215
|
+
t.Fatalf("connection should be *Obj, got %T", conn)
|
|
216
|
+
}
|
|
217
|
+
items, _ := o.Get("items")
|
|
218
|
+
cursor, hasCursor := o.Get("cursor")
|
|
219
|
+
arr, isArr := items.([]Value)
|
|
220
|
+
if !isArr || len(arr) != 0 || !hasCursor || cursor != nil {
|
|
221
|
+
t.Fatalf("connection shape wrong: %v", o)
|
|
222
|
+
}
|
|
223
|
+
uc := UnproducedValue("connection").(*Obj)
|
|
224
|
+
if _, ok := uc.Get("items"); !ok {
|
|
225
|
+
t.Fatal("unproduced connection missing items")
|
|
226
|
+
}
|
|
227
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "behavior-contracts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"description": "Language-agnostic runtime core for behavior contracts (dsl-contracts): validateEnvelope / evaluateExpression / renderTemplate / runPlan / canonical serialization. Single source of truth for the shared DSL runtime primitives.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
13
|
+
"go",
|
|
13
14
|
"cli-contract.yaml",
|
|
14
15
|
"README.md",
|
|
15
16
|
"LICENSE"
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
"scripts": {
|
|
28
29
|
"build": "tsc -p tsconfig.json && node scripts/fix-dts-extensions.mjs",
|
|
29
30
|
"clean": "rm -rf dist",
|
|
31
|
+
"prepack": "node scripts/bundle-go-module.mjs",
|
|
30
32
|
"cli:generate": "cli-contracts generate",
|
|
31
33
|
"test": "node --disable-warning=ExperimentalWarning --test \"test/*.test.ts\"",
|
|
32
34
|
"conformance": "node --disable-warning=ExperimentalWarning --test \"test/conformance.test.ts\"",
|