feat(sfn): publish history events for Parallel branches and Map iterations (#3167)

* feat(sfn): publish Parallel branch and Map iteration history events

The states inside a Parallel branch or an inline Map iteration now publish
their events into the execution history.

The cause of a failure the interpreter raises now carries the AWS prefix
"An error occurred while executing the state '<name>' (entered at the event
id #<n>). ". It is written once, at the innermost state.

Every shape was verified against real Step Functions in us-east-1.

* feat(sfn): record EvaluationFailed for JSONata failures

A JSONata expression that fails now leaves the EvaluationFailed event AWS
records, with error, the attributed cause, location and state. The location
names the field the way the cause does. The event is recorded once per
attempt when the state retries, and before the Failed event and the Catch.
A Catch clause whose own expression fails records it from where the state
stood before its first Failed event, then fails the state again.

Verified against real Step Functions in us-east-1

* fix(sfn): record every Distributed Map run so DescribeMapRun resolves its ARN

* fix(sfn): stop counting history events once the execution has ended
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-07 23:18:05 -04:00
1 parent 5eacd01636
commit fd21ac9a6d
19 files changed
+1726 -333

No files matched your search

@@ -126,6 +126,149 @@ class StepFunctionsExecutionHistoryTest {
}
}
/**
* Issue #2868: the states inside a Parallel branch and an inline Map iteration publish into the
* execution's history, chained through previousEventId to the Started event that opened them.
* The SDK reads each event's details under the field named for its type, which is what this
* test pins. Shapes verified against real AWS in us-east-1.
*/
@Test
void parallelBranchesAndMapIterationsPublishTheirEvents() throws Exception {
var smDef = """
{
"StartAt": "P",
"States": {
"P": {
"Type": "Parallel",
"Branches": [
{"StartAt": "A1", "States": {"A1": {"Type": "Pass", "End": true}}}
],
"Next": "M"
},
"M": {
"Type": "Map",
"ItemsPath": "$[0].items",
"MaxConcurrency": 1,
"ItemProcessor": {
"ProcessorConfig": {"Mode": "INLINE"},
"StartAt": "I1",
"States": {"I1": {"Type": "Pass", "End": true}}
},
"End": true
}
}
}
""";
var smArn = sfn.createStateMachine(b -> b
.name(TestFixtures.uniqueName("sfn-history-branches-sm"))
.definition(smDef)
.roleArn(ROLE_ARN)).stateMachineArn();
try {
var execArn = sfn.startExecution(b -> b
.stateMachineArn(smArn)
.input("{\"items\": [1, 2]}")).executionArn();
var execution = pollUntilDone(execArn);
assertThat(execution.status()).isEqualTo(ExecutionStatus.SUCCEEDED);
var events = sfn.getExecutionHistory(b -> b
.executionArn(execArn)
.includeExecutionData(true)).events();
var types = events.stream().map(HistoryEvent::type).toList();
assertThat(types).containsExactly(
HistoryEventType.EXECUTION_STARTED,
HistoryEventType.PARALLEL_STATE_ENTERED,
HistoryEventType.PARALLEL_STATE_STARTED,
HistoryEventType.PASS_STATE_ENTERED,
HistoryEventType.PASS_STATE_EXITED,
HistoryEventType.PARALLEL_STATE_SUCCEEDED,
HistoryEventType.PARALLEL_STATE_EXITED,
HistoryEventType.MAP_STATE_ENTERED,
HistoryEventType.MAP_STATE_STARTED,
HistoryEventType.MAP_ITERATION_STARTED,
HistoryEventType.PASS_STATE_ENTERED,
HistoryEventType.PASS_STATE_EXITED,
HistoryEventType.MAP_ITERATION_SUCCEEDED,
HistoryEventType.MAP_ITERATION_STARTED,
HistoryEventType.PASS_STATE_ENTERED,
HistoryEventType.PASS_STATE_EXITED,
HistoryEventType.MAP_ITERATION_SUCCEEDED,
HistoryEventType.MAP_STATE_SUCCEEDED,
HistoryEventType.MAP_STATE_EXITED,
HistoryEventType.EXECUTION_SUCCEEDED);
var expectedPreviousEventIds = List.of(0L, 0L, 2L, 3L, 4L, 5L, 5L, 7L, 8L, 9L, 10L, 11L, 12L,
9L, 14L, 15L, 16L, 17L, 17L, 19L);
for (var i = 0; i < events.size(); i++) {
var event = events.get(i);
assertThat(event.id()).isEqualTo(i + 1L);
assertThat(event.previousEventId()).as("previousEventId of event %d", i + 1)
.isEqualTo(expectedPreviousEventIds.get(i));
}
assertThat(events.get(8).mapStateStartedEventDetails().length()).isEqualTo(2);
assertThat(events.get(9).mapIterationStartedEventDetails().name()).isEqualTo("M");
assertThat(events.get(9).mapIterationStartedEventDetails().index()).isEqualTo(0);
assertThat(events.get(16).mapIterationSucceededEventDetails().name()).isEqualTo("M");
assertThat(events.get(16).mapIterationSucceededEventDetails().index()).isEqualTo(1);
assertThat(events.get(18).stateExitedEventDetails().output()).isEqualTo("[1,2]");
} finally {
try { sfn.deleteStateMachine(b -> b.stateMachineArn(smArn)); } catch (Exception ignored) {}
}
}
/** Issue #2868: the cause of a failure inside a branch names the branch state, as on AWS. */
@Test
void failureInsideABranchNamesTheBranchStateInItsCause() throws Exception {
var smDef = """
{
"StartAt": "P",
"States": {
"P": {
"Type": "Parallel",
"Branches": [
{"StartAt": "Fast", "States": {"Fast": {"Type": "Pass", "Parameters": {"m.$": "$.nope"}, "End": true}}}
],
"End": true
}
}
}
""";
var smArn = sfn.createStateMachine(b -> b
.name(TestFixtures.uniqueName("sfn-history-branch-failure-sm"))
.definition(smDef)
.roleArn(ROLE_ARN)).stateMachineArn();
try {
var execArn = sfn.startExecution(b -> b
.stateMachineArn(smArn)
.input("{\"x\":\"a\"}")).executionArn();
var execution = pollUntilDone(execArn);
assertThat(execution.status()).isEqualTo(ExecutionStatus.FAILED);
assertThat(execution.error()).isEqualTo("States.Runtime");
assertThat(execution.cause()).isEqualTo(
"An error occurred while executing the state 'Fast' (entered at the event id #4). "
+ "The JSONPath '$.nope' specified for the field 'm.$' could not be found in the input '{\"x\":\"a\"}'");
var events = sfn.getExecutionHistory(b -> b.executionArn(execArn)).events();
assertThat(events.stream().map(HistoryEvent::type).toList()).containsExactly(
HistoryEventType.EXECUTION_STARTED,
HistoryEventType.PARALLEL_STATE_ENTERED,
HistoryEventType.PARALLEL_STATE_STARTED,
HistoryEventType.PASS_STATE_ENTERED,
HistoryEventType.EXECUTION_FAILED);
assertThat(events.get(3).stateEnteredEventDetails().name()).isEqualTo("Fast");
assertThat(events.get(4).executionFailedEventDetails().cause()).isEqualTo(execution.cause());
} finally {
try { sfn.deleteStateMachine(b -> b.stateMachineArn(smArn)); } catch (Exception ignored) {}
}
}
private DescribeExecutionResponse pollUntilDone(String execArn) throws InterruptedException {
for (var i = 0; i < 120; i++) {
var resp = sfn.describeExecution(b -> b.executionArn(execArn));