Repository navigation
Expand file tree
/
Copy patheval.go
More file actions
440 lines (420 loc) · 11.2 KB
/
Copy patheval.go
File metadata and controls
440 lines (420 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package execir
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
)
// evalArgs resolves each argument value against scope.
func evalArgs(scope map[string]any, args map[string]Value) (map[string]any, error) {
if len(args) == 0 {
return nil, nil
}
out := make(map[string]any, len(args))
for k, v := range args {
val, err := evalValue(scope, v)
if err != nil {
return nil, err
}
out[k] = val
}
return out, nil
}
// evalValue resolves a Ref against the scope or returns a Lit's Go value. The
// composite forms (Object/List/Template) evaluate their members recursively —
// they are pure over already-resolved refs, so a straight-line program that
// returns an object or interpolates a string is fully executable in isolation
// (control-flow nodes Graph/Approval are the parts deferred to later phases).
func evalValue(scope map[string]any, v Value) (any, error) {
switch x := v.(type) {
case Lit:
return x.V, nil
case Ref:
return resolvePath(scope, x.Path)
case Object:
out := make(map[string]any, len(x.Fields))
for _, f := range x.Fields {
fv, err := evalValue(scope, f.Val)
if err != nil {
return nil, err
}
out[f.Key] = fv
}
return out, nil
case List:
out := make([]any, len(x.Elems))
for i, e := range x.Elems {
ev, err := evalValue(scope, e)
if err != nil {
return nil, err
}
out[i] = ev
}
return out, nil
case Template:
var sb strings.Builder
for _, p := range x.Parts {
pv, err := evalValue(scope, p)
if err != nil {
return nil, err
}
sb.WriteString(stringify(pv))
}
return sb.String(), nil
case nil:
return nil, nil
default:
return nil, fmt.Errorf("execir: unknown value %T", v)
}
}
// stringify renders a resolved value for embedding in an interpolated Template:
// scalars print directly, and composites JSON-encode (mirroring how the engine's
// string interpolation embeds objects/arrays), so a Template part that resolves
// to a map/list does not print as a Go %v map.
func stringify(v any) string {
switch x := v.(type) {
case nil:
return ""
case string:
return x
case bool:
return strconv.FormatBool(x)
case int:
return strconv.Itoa(x)
case int64:
return strconv.FormatInt(x, 10)
case float64:
return strconv.FormatFloat(x, 'g', -1, 64)
default:
if b, err := json.Marshal(x); err == nil {
return string(b)
}
return fmt.Sprintf("%v", x)
}
}
// resolvePath resolves a dotted path against scope. The head must be a bound
// name (an unbound head is a programming error the checker/lowering should have
// caught, so it is surfaced loudly); a missing NESTED field resolves to nil
// (gradual — agent and tool outputs are dynamically shaped).
func resolvePath(scope map[string]any, path []string) (any, error) {
if len(path) == 0 {
return nil, fmt.Errorf("execir: empty reference path")
}
cur, ok := scope[path[0]]
if !ok {
return nil, fmt.Errorf("execir: unresolved reference %q", path[0])
}
for _, seg := range path[1:] {
m, ok := cur.(map[string]any)
if !ok {
return nil, nil
}
cur = m[seg]
}
return cur, nil
}
// evalCollection resolves a value expected to be an iterable and returns its
// elements. A JSON array ([]any) iterates its elements; nil is an empty
// collection (an absent field yields zero iterations rather than an error). Any
// other concrete type is a runtime error — a loop needs a collection.
func evalCollection(scope map[string]any, v Value) ([]any, error) {
val, err := evalValue(scope, v)
if err != nil {
return nil, err
}
switch xs := val.(type) {
case nil:
return nil, nil
case []any:
return xs, nil
default:
return nil, fmt.Errorf("execir: loop collection is %T, not a list", val)
}
}
// evalExpr evaluates a boolean condition tree.
func evalExpr(scope map[string]any, e Expr) (bool, error) {
switch x := e.(type) {
case nil:
return false, fmt.Errorf("execir: nil condition")
case Leaf:
val, err := evalValue(scope, x.V)
if err != nil {
return false, err
}
return truthy(val), nil
case Not:
b, err := evalExpr(scope, x.X)
if err != nil {
return false, err
}
return !b, nil
case BinOp:
return evalBinOp(scope, x)
default:
return false, fmt.Errorf("execir: unknown condition %T", e)
}
}
func evalBinOp(scope map[string]any, x BinOp) (bool, error) {
// Logical connectives short-circuit and operate on boolean sub-conditions.
switch x.Op {
case "&&":
l, err := evalExpr(scope, x.X)
if err != nil || !l {
return false, err
}
return evalExpr(scope, x.Y)
case "||":
l, err := evalExpr(scope, x.X)
if err != nil {
return false, err
}
if l {
return true, nil
}
return evalExpr(scope, x.Y)
}
// Comparisons operate on values.
lv, err := leafValue(scope, x.X)
if err != nil {
return false, err
}
rv, err := leafValue(scope, x.Y)
if err != nil {
return false, err
}
switch x.Op {
case "==":
return valuesEqual(lv, rv), nil
case "!=":
return !valuesEqual(lv, rv), nil
case "<", "<=", ">", ">=":
return compareOrdered(x.Op, lv, rv)
default:
return false, fmt.Errorf("execir: unknown operator %q", x.Op)
}
}
// leafValue evaluates an expression that must be a value leaf (the operand of a
// comparison). The parser only ever places Leaf nodes under a comparison, so a
// non-leaf here is an internal lowering error.
func leafValue(scope map[string]any, e Expr) (any, error) {
leaf, ok := e.(Leaf)
if !ok {
return nil, fmt.Errorf("execir: comparison operand is not a value")
}
return evalValue(scope, leaf.V)
}
// truthy defines the boolean coercion for a bare condition leaf (`if flag`):
// booleans are themselves, null/absent is false, everything present is true.
func truthy(v any) bool {
switch x := v.(type) {
case nil:
return false
case bool:
return x
default:
return true
}
}
// valuesEqual is a total, panic-free equality over every value this IR can
// carry, so `==` is defined on the JSON objects and arrays a workflow input
// holds — a bare `a == b` on `any` would panic ("comparing uncomparable type")
// for a map or slice operand. Numbers compare numerically across int64/float64
// (1 == 1.0) without rounding integers through float64, so values above 2^53
// stay distinct. That only holds if the operands are still exact integers when
// they get here: runtime ingress (run input, HITL edits, agent output and tool-call
// arguments, HTTP/MCP and native GitHub/Slack tool results) and checkpoint
// hydration decode JSON through internal/jsonnum (int64 for a whole number,
// float64 otherwise, by exact value), and the interpreter canonicalizes its input
// and every leaf result (jsonnum.Canonical) to a form that is a checkpoint
// round-trip fixed point, so live and replayed values are identical. Program
// literals are not canonicalized; the comparison below is exact across
// int64/float64, so a float literal 2^60 equals the int64 2^60. A producer that
// decodes JSON with plain encoding/json has already rounded past 2^53, and a
// float64 inside a typed producer value (a struct field, []float64) takes
// encoding/json's shortest spelling; Canonical keeps both deterministic but
// cannot restore the digits. strings/bools compare by value, arrays and objects structurally
// (element- and key-wise, recursively, with the same numeric normalization),
// and anything else via reflect.DeepEqual, which never panics. A type mismatch
// is unequal.
func valuesEqual(a, b any) bool {
if cmp, ok := tryCompareNumeric(a, b); ok {
return cmp == 0
}
if isNumeric(a) || isNumeric(b) {
return false
}
switch av := a.(type) {
case []any:
bv, ok := b.([]any)
if !ok || len(av) != len(bv) {
return false
}
for i := range av {
if !valuesEqual(av[i], bv[i]) {
return false
}
}
return true
case map[string]any:
bv, ok := b.(map[string]any)
if !ok || len(av) != len(bv) {
return false
}
for k, va := range av {
vb, present := bv[k]
if !present || !valuesEqual(va, vb) {
return false
}
}
return true
default:
return reflect.DeepEqual(a, b)
}
}
// compareOrdered evaluates <, <=, >, >= over numeric operands. A non-numeric
// operand is an error — ordering strings or booleans is not defined in the
// surface. Integer operands are compared exactly; mixed integer/float
// comparisons do not round the integer through float64.
func compareOrdered(op string, a, b any) (bool, error) {
cmp, ok := tryCompareNumeric(a, b)
if !ok {
if isNumeric(a) && isNumeric(b) {
// NaN is unordered: every relational operator is false.
return false, nil
}
return false, fmt.Errorf("execir: operator %q needs numeric operands, got %T and %T", op, a, b)
}
switch op {
case "<":
return cmp < 0, nil
case "<=":
return cmp <= 0, nil
case ">":
return cmp > 0, nil
case ">=":
return cmp >= 0, nil
}
return false, fmt.Errorf("execir: unknown operator %q", op)
}
func isNumeric(v any) bool {
_, i := asInt64(v)
_, f := asFloat64(v)
return i || f
}
func asInt64(v any) (int64, bool) {
switch x := v.(type) {
case int:
return int64(x), true
case int64:
return x, true
default:
return 0, false
}
}
func asFloat64(v any) (float64, bool) {
switch x := v.(type) {
case float64:
return x, true
case float32:
return float64(x), true
default:
return 0, false
}
}
// maxExactInt is the largest integer magnitude float64 can represent exactly
// (the IEEE-754 binary64 significand is 53 bits, including the implicit bit).
const maxExactInt int64 = 1 << 53
// floatMinInt64 is MinInt64 as float64 (−2^63), which is exact.
// floatMaxInt64Exclusive is 2^63: MaxInt64 is not a float64, and every finite
// float64 ≥ 2^63 is strictly greater than any int64.
const (
floatMinInt64 = -9223372036854775808.0
floatMaxInt64Exclusive = 9223372036854775808.0
)
// tryCompareNumeric compares two numeric values. ok is false when either
// operand is non-numeric, or when a float operand is NaN (unordered).
func tryCompareNumeric(a, b any) (int, bool) {
ai, aInt := asInt64(a)
bi, bInt := asInt64(b)
af, aFlt := asFloat64(a)
bf, bFlt := asFloat64(b)
switch {
case aInt && bInt:
return cmpInt64(ai, bi), true
case aInt && bFlt:
return compareIntFloat(ai, bf)
case aFlt && bInt:
c, ok := compareIntFloat(bi, af)
return -c, ok
case aFlt && bFlt:
switch {
case math.IsNaN(af) || math.IsNaN(bf):
return 0, false
case af < bf:
return -1, true
case af > bf:
return 1, true
default:
return 0, true
}
default:
return 0, false
}
}
func cmpInt64(a, b int64) int {
switch {
case a < b:
return -1
case a > b:
return 1
default:
return 0
}
}
// compareIntFloat compares an integer to a float without rounding the integer
// through float64 when |i| > 2^53. ok is false when f is NaN.
func compareIntFloat(i int64, f float64) (int, bool) {
if math.IsNaN(f) {
return 0, false
}
if math.IsInf(f, 1) {
return -1, true
}
if math.IsInf(f, -1) {
return 1, true
}
if i >= -maxExactInt && i <= maxExactInt {
fi := float64(i)
switch {
case fi < f:
return -1, true
case fi > f:
return 1, true
default:
return 0, true
}
}
if f >= floatMaxInt64Exclusive {
return -1, true
}
if f < floatMinInt64 {
return 1, true
}
trunc := math.Trunc(f)
ti := int64(trunc)
if f == trunc {
return cmpInt64(i, ti), true
}
if f > 0 {
if i <= ti {
return -1, true
}
return 1, true
}
if i < ti {
return -1, true
}
return 1, true
}