fix(stepfunctions): bound $range allocation at the AWS memory limit (#2790)

* fix(stepfunctions): open a JSONata query evaluation cause with the AWS sentence

evaluateField now wraps a thrown JSONata failure with "The JSONata expression
'<expr>' specified for the field '<field>' threw an error during evaluation"
before the existing tail, matching real AWS. The separator is ". " when the
tail carries a JSONata error code and ": " when it is a bound message with
none. evaluate() keeps producing the bare tail unchanged for its direct
callers.

evaluate() stays the only place the JSONata engine is called and the only
catch around it: evaluateField calls it and enriches the failure it throws,
rather than reaching the engine itself. QueryEvaluationFailure carries the
separator across that throw, so a catch added to evaluate() later covers both
entry points. All 11 AslExecutor call sites reach the evaluator through
evaluateField or resolveTemplate, so a catch installed anywhere else would be
dead on every real execution path.

* fix(stepfunctions): catch Error from JSONata evaluation as States.QueryEvaluationError

An OutOfMemoryError or StackOverflowError raised while parsing or evaluating a JSONata
expression escaped JsonataEvaluator's catch (Exception) and reached AslExecutor's own
last-resort catch (Error), failing the whole execution as States.Runtime before the
state's own Retry and Catch ever ran. JsonataEvaluator.evaluate now also catches Error at
the same seam and translates it into a FailStateException with States.QueryEvaluationError,
so a Catch on the state (or on States.ALL) can recover, matching real AWS.

Closes #2738

* fix(stepfunctions): match AWS on the JSONata expressions it refuses

Four expression classes where Floci and AWS disagreed on whether the state
fails at all, all measured against us-east-1 with test-state.

The evaluation-depth ceiling drops from 500 to 100, which is AWS's own: five
nesting shapes bisected one n at a time all put its last accepted evaluation at
depth 100 and its first refusal at 101. An addition chain nests one level per
term and AWS accepts 100 terms; parentheses, brackets and the chain operator
nest one level more than their count and AWS accepts 99 of each; a
non-tail-recursive $f(n) nests 3n+5 and AWS accepts n=31, depth 98, refusing
n=32, depth 101.

The memory an expression may hold is now bounded, which it was not: one value an
evaluation step produces may hold 6,990,256 bytes, counting a number as eight
and a character as one. The two shapes AWS was bisected on, [1..873782] and a
string doubled 22 times, bracket a number at between five and nine bytes a
character: with the bound at 873,782 numbers, anything in that range leaves
4,194,304 characters inside it and 8,388,608 outside, and four or ten does not.
Eight is what a double costs, and it is inside the bracket. The check walks the
value and stops at the bound, and a lazy range is counted from its length rather
than materialised.

$eval is unbound, as AWS disables it, so a call to it answers T1006 the way AWS
does and $parse stays the replacement.

A non-finite number the expression itself evaluates to comes back as the string
AWS writes for it, since JSON has no literal for one: 1/0 and 1e308*10 are
"Infinity" and $parseInteger("abc","0") is "NaN", where the first two raised
D1001 and the third returned nothing and failed the field. One the library's
arithmetic buries inside a value, as in [1/0], still fails the state, because
the refusal is the only place the library leaves the value and it has already
unwound what was being built; one a function answers nests like any other value.
$formatNumber checks its picture string against the fourteen rules of XPath F&O
4.7.3 first, so $formatNumber(1, "x") fails with D3086 instead of answering "x1".
Which of the fourteen codes AWS reports is what fixes the order the rules are
read in: a picture with no digit breaks D3085 and D3086 at once and AWS reports
the second, so the last rule broken names the failure rather than the first.

Closes #2737

* fix(stepfunctions): bound $range allocation at the AWS memory limit

AWS accepts $range(1, 360145, 1), all 360,145 elements, and refuses one
element more on its own memory limit. Floci's $range had no such bound:
it built the array in full before the memory check on the value ever ran,
so a call far past AWS's limit spent gigabytes and either exhausted the
heap or hung instead of failing the state cleanly.

range() now refuses a call whose produced element count exceeds the
measured bound before allocating, throwing the same
States.QueryEvaluationError JException the held-memory check raises,
whose message is now a single shared constant. Other built arrays keep
the looser, byte-based bound.

* test(stepfunctions): pin the number model of the execution input

Measured against real AWS (test-state, 2026-08-30): a Pass state with
Output {"v": "{% $states.input.amount %}"} answers {"v": 1} for input
{"amount": 1.0} and {"v": 9.223372036854776E18} for input
{"amount": 9223372036854775808}. That is exactly what routing the
execution input through toJsonataValue/toJsonataNumber already
produces, so these two tests pin it against silent regression.

---------

Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
Facundo ParedesandFacundo Paredes authored and GitHub committed 2026-08-31 00:16:52 -04:00
1 parent ead70eae4d
commit f7f16cd2a3
6 files changed
+1102 -56

No files matched your search

+41 -10
View File
@@ -189,7 +189,7 @@ top of the JSONata language, alongside every function JSONata itself provides.
| Function | Returns |
| --- | --- |
| `$parse(jsonString)` | the deserialized value; the replacement for `$eval`, which AWS disables |
| `$parse(jsonString)` | the deserialized value; the replacement for `$eval`, which AWS disables and so does Floci, answering `T1006` to a call |
| `$partition(array, chunkSize)` | `array` split into chunks of `chunkSize`, the last one holding the remainder |
| `$range(start, end, step)` | the values from `start` to `end`, inclusive when `step` lands on `end` |
| `$hash(str, algorithm)` | the hex digest of `str`; `algorithm` is `MD5`, `SHA-1`, `SHA-256`, `SHA-384` or `SHA-512`, case-sensitive |
@@ -210,16 +210,47 @@ JSONata's own `$string` follows AWS's number notation: a whole number is written
`1e21` and in exponent notation from there, on both signs, so `$string(1e20)` is
`100000000000000000000` and `$string(1e21)` is `1e+21`.
Evaluation is bounded, as it is on AWS: one expression may run for five seconds and nest 500
levels deep, and past either the state fails with `States.QueryEvaluationError`. Both bounds sit
well above what AWS itself accepts, so an expression that evaluates there evaluates here. AWS
refuses a non-tail-recursive function past a nesting depth near 100, and the largest sequence it
accepts evaluates here in about a tenth of a second.
The execution input reaching `$states.input` follows the same number model as `$parse`: an integer
stays exact while it fits in a `long` and switches to a `double` past that boundary, matching AWS.
One deviation. AWS also bounds the *memory* an expression may use, refusing
`[1..900000] ~> $count()` with `Expression evaluation memory limit exceeded` while accepting the
same expression at 800,000 elements. Floci bounds time and depth but not memory, and its ranges
are lazy, so that expression answers immediately at any size.
JSONata's own `$formatNumber` checks its picture string against the fourteen rules of XPath F&O
4.7.3, as AWS does, so `$formatNumber(1, "x")` fails with `D3086` rather than answering `x1`: a
picture with no digit in it describes no number.
Evaluation is bounded on three axes, as it is on AWS, and past any of them the state fails with
`States.QueryEvaluationError`.
- **Depth**: one expression may nest 100 levels, which is AWS's own ceiling: AWS accepts `1+1+…+1`
at 100 terms, 99 parentheses, 99 brackets, 99 `~>` stages, and a non-tail-recursive `$f(31)`,
which nests `3n+5`, and refuses one level more of each. The refusal names the depth reached, as
in `Stack overflow error: … Depth=101 max=100`.
- **Memory**: one value an expression builds may hold 6,990,256 bytes, counting a number as eight
bytes and a character as one. That is AWS's own bound: AWS accepts `[1..873782]` and refuses one
element more, and it accepts a string doubled 22 times, 2^22 characters, refusing the 23rd. The
refusal is AWS's own
`Expression evaluation memory limit exceeded`, which is what `[1..900000] ~> $count()` and
`$sum([1..900000])` now answer. `$range` holds a tighter bound of its own, checked before it
allocates rather than on the array it would have built: AWS accepts `$range(1, 360145, 1)`, all
360,145 elements, and refuses one element more with the same refusal. A lazy literal range such
as `[1..873782]` is exempt from both bounds on AWS, and stays exempt here.
- **Time**: five seconds, which is the library's own default and roughly fifty times the slowest
evaluation of a payload AWS itself accepts. A recursive expression with no base case is a tail
call, so it loops rather than nesting and only the clock ends it.
JSON has no literal for a non-finite number, so AWS writes each one as the string JavaScript names
it by, wherever it lands: `1/0` is `"Infinity"`, `1e308 * 10` is `"Infinity"`, `0/0` is `"NaN"` and
`$parseInteger("abc", "0")` is `"NaN"`, and nested, `[1/0]` is `["Infinity"]` and `{"k": 1/0}` is
`{"k": "Infinity"}`.
One deviation, on the arithmetic half of that. A non-finite number a **function** answers is a value
like any other here, so `$parseInteger("abc", "0")` is `"NaN"` on its own, inside an array and
inside an object, as on AWS. One the **arithmetic** produces is not: the JSONata library refuses to
carry it and raises instead, so Floci sees the value only in that refusal, after it has unwound
whatever was being built around it. `1/0`, `-1/0` and `1e308 * 10` are answered because the
expression's own result is what was refused; `[1/0]`, `{"k": 1/0}` and `$string(1/0)` fail the state
with `States.QueryEvaluationError` where AWS answers a string, and `0/0` fails the field that holds
it, because NaN is dropped as not-a-number without even a refusal to read it from. A state that
fails is one a `Catch` fires on, which is the half of the divergence worth keeping.
## Nested workflows