Skip to content

feat(sdk): add AT_LEAST_ONCE_ASYNC and default_step_semantics - #996

Merged
ParidelPooya merged 17 commits into
mainfrom
feat/step-at-least-once-async
Nov 27, 2026
Merged

ParidelPooya merged 17 commits into
mainfrom
feat/step-at-least-once-async

Conversation

@ParidelPooya

@ParidelPooya ParidelPooya commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Issue #, if available

Ports aws-durable-execution-sdk-js#1296 to Python.

Description of changes

What

  • Adds StepSemantics.AT_LEAST_ONCE_ASYNC, marked experimental like the pause option. The step queues its SUCCEED checkpoint and returns at once. AT_LEAST_ONCE still waits for the SUCCEED checkpoint. So the new mode has lower latency.
  • START is not awaited, as in AT_LEAST_ONCE. RETRY and FAIL are still awaited.
  • A plugin gets the step's end hook from the service's answer, not at once. The SDK runs that hook when a thread reads the result, or at the latest before on_invocation_end.

Why it keeps at-least-once

  • The batcher sends checkpoints in queue order. So a later checkpoint that the service accepts also saves this result.
  • Before this change, the batcher stopped without sending the checkpoints still queued. It now sends them before it stops. Every exit of durable_execution stops the batcher and waits for it. So the SUCCEED is sent before the invocation returns SUCCEEDED, FAILED or PENDING.
  • A failed batch fails the invocation. The step then runs again on replay.
  • The handler may not see the failure, for example when the SUCCEED batch fails after the handler returned or raised. Then an invocation-scope failure raises, so Lambda retries the invocation. This holds for a handler error too, which would otherwise answer FAILED and end the execution without the step's result.
  • An execution-scope failure, such as a validation error, answers FAILED, as when the handler sees it. A handler error keeps its own FAILED answer then.

Trade-off

  • The code after the step can run before the result is saved. If the invocation ends before the save, that code and the step run again on replay.
  • The step can then return a different value, and later steps run again with it. So the docs say not to use the mode for a step whose result becomes an idempotency key or a client token for a later step.

Renames

  • AT_MOST_ONCE and AT_LEAST_ONCE are the new names. They keep the old values, so nothing on the wire changes.
  • AT_MOST_ONCE_PER_RETRY and AT_LEAST_ONCE_PER_RETRY stay the canonical members, declared first as on main. AT_MOST_ONCE and AT_LEAST_ONCE are enum aliases of them. StepSemantics.AT_LEAST_ONCE_PER_RETRY is StepSemantics.AT_LEAST_ONCE is true, so existing code keeps working. .name, repr and list(StepSemantics) report the old names, as on main.
  • Python enum aliases cannot carry @deprecated or a warning without a custom metaclass. So the deprecation is a comment and a docstring note.
  • Code, tests, examples and docs use the new names. The conformance test handlers keep the old names, because AGENTS.md asks to coordinate changes there. They still work through the aliases.

Default step semantics

  • durable_execution takes default_step_semantics. It sets the semantics of every step whose StepConfig does not set step_semantics, in every context of the execution: child contexts, map items, parallel branches, and steps that extensions create.
  • A step's own step_semantics wins. The default stays AT_LEAST_ONCE.
  • StepConfig.step_semantics keeps its type and its AT_LEAST_ONCE default. StepConfig records privately whether the caller set the field, and DurableContext.step applies the execution default only when it is not set.
@durable_execution(default_step_semantics=StepSemantics.AT_LEAST_ONCE_ASYNC)
def handler(event, context): ...
Options considered for how a step takes the default
Option Why not chosen
step_semantics defaults to None Breaks StepConfig().step_semantics.value and code typed as StepSemantics.
Use the execution default only when no StepConfig is passed A step with StepConfig(retry_strategy=...) would skip the execution default, which is surprising.
Keep the field and its default, and record privately whether the caller set it (chosen) Old code keeps working. The default reaches every step that does not set the field. A config made with dataclasses.replace counts as set, so it can only lose the default, never turn async.

Extensions

  • The microvm and agentcore session ID steps now set AT_LEAST_ONCE_PER_RETRY themselves. The microvm ID becomes the launch's client token, and the agentcore ID becomes the runtime session. So default_step_semantics=AT_LEAST_ONCE_ASYNC cannot launch a second MicroVM on replay.
  • They use the deprecated name, so the extensions' core requirement stays >=3.0.0.

Differences from the JavaScript change

  • Plugin end hooks: JavaScript reports SUCCEEDED at once with a local end time. Python records end hooks from the service's answer. This change keeps that model, so the hook carries the service's timestamps and can run later.
  • Dropped SUCCEED of an abandoned step: JavaScript marks an abandoned operation COMPLETED when its checkpoint is dropped, and the async SUCCEED skips that. Python has no such state. create_checkpoint raises OrphanedChildException for an orphaned operation in both modes (state.py, _reject_if_parent_done), so the async step does not return, and the branch unwinds. So that fix does not apply.
  • CHANGELOG: this repository has none, so no deprecation entry.
  • Batch docs: the JavaScript docs say an abandoned map or parallel item keeps running new async steps. In Python, an abandoned branch is rejected only for operations it started before the parent completed, in both modes. So this mode does not change that behavior, and the docs do not add the note.

Example

  • step_semantics_async_default sets default_step_semantics=AT_LEAST_ONCE_ASYNC. Its order ID step sets AT_LEAST_ONCE itself, because the ID is the idempotency key of the charge. A wait ends the first invocation, so the second invocation replays the saved steps.

Tests

  • tests/operation/step_at_least_once_async_test.py runs handlers through durable_execution with a slow fake client and the real batcher thread. It checks that:
    • the async step returns before SUCCEED is answered, and AT_LEAST_ONCE still waits;
    • FAIL and RETRY are still awaited;
    • a STARTED async step runs again on replay;
    • a SUCCEED still queued when the handler returns is sent before SUCCEEDED is returned;
    • a failed SUCCEED batch does not return SUCCEEDED;
    • a handler error after an async step whose SUCCEED then fails with an invocation-scope error raises a retryable CheckpointError;
    • with an execution-scope error, a handler error answers FAILED with its own error, and a returned result answers FAILED with the checkpoint error;
    • a plugin sees the step's SUCCEEDED end hook before on_invocation_end;
    • default_step_semantics reaches the root, a child context and map items, and a step's own semantics wins.
  • tests/e2e/default_step_semantics_int_test.py runs an async-default handler with a wait on the local test runner. Every step is SUCCEEDED in the history, and each step ran once.
  • context_test.py checks how DurableContext.step resolves the semantics. config_test.py checks that the new names are the same members, that .name and list(StepSemantics) match main plus AT_LEAST_ONCE_ASYNC, and that StepConfig().step_semantics is AT_LEAST_ONCE. context_test.py also checks that an explicit AT_LEAST_ONCE wins over an async execution default, and that A config made with dataclasses.replace counts the field as set, so it keeps its value and does not take the execution default.
  • state_test.py checks that an async SUCCEED left only in the overflow queue at stop is sent before the batcher thread stops.
  • The extension tests check that both session ID steps set their own semantics.
  • Mutation checks: awaiting SUCCEED in the new mode fails 2 tests. Not sending queued checkpoints at stop fails 5 tests, including the e2e test. Checking a late failure only for SUCCEEDED fails 1 test. Raising every late failure fails the 2 execution-scope tests. Ignoring the default in DurableContext.step fails 4 tests. Removing the extension pins fails both extension tests. Removing the overflow check from the stop condition fails the overflow test. Each change to the set-flag logic in StepConfig fails 1 to 7 tests.
  • Results: core 2311 passed, extensions 630 passed, otel 1021 passed, testing 1886 passed, examples 209 passed and 10 skipped. Compute worker: 510 passed and 1 failed. That failure, test_create_raises_when_boto3_cannot_build_the_client, also fails on main on the same machine. Type checks and hatch fmt --check pass for every package.

Design

  • Design note (rule 3): this description, following the JavaScript change.
  • Simpler options considered (rule 2):
    • An empty synchronous checkpoint in ExecutionState.close() to flush the queue. It waits forever in a state whose batcher thread is not running, as in many unit tests, and it costs an API call.
    • A flag on close(). Existing tests patch close with a one-argument function.
    • So the batcher's stop condition changed instead: stop once the stop signal is set and both queues are empty.
  • Net change in source lines: +221 / -43 across src, including the 54-line example. Public API: one new enum member, two new alias names, and one new durable_execution keyword.

Assumptions

  • (unverified) A Lambda invocation that raises is retried by the service, and the next invocation replays from the last saved checkpoint. This PR does not add this behavior. It reuses the existing SDK error path for a retryable checkpoint error. The tests use a fake client, so they do not verify the service retry.

Migration

  • StepConfig.step_semantics keeps its type and default. The execution default applies when the step does not set it.

Decisions not covered by the issue or the design note

  • The default lives on durable_execution, because it is the only execution-level setting point.
  • Edited test expectations: test_step_config_defaults uses the new name AT_LEAST_ONCE. test_collect_checkpoint_batch_shutdown_path and test_stop_settles_pending_refresh_and_sends_queued_items now expect queued checkpoints to be sent at stop. test_step_with_name_and_config passes an explicit semantics, because a config without one is now copied with the default. The step_semantics_at_most_once example returns the new name.

Checklist

  • Every new branch has a test that fails without it (rule 4)
  • For a bug fix: the first commit adds a test that fails on the current code (rule 5). Not a bug fix.
  • Every edited test expectation is listed in the description, with the reason (rule 5)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Review notes

  • A handler can suspend while an async SUCCEED is still queued, for example on a replayed wait. A failed SUCCEED call now gets the same answer as AT_LEAST_ONCE: FAILED for a permanent failure, a raised CheckpointError for a retryable one. A withdrawn token still answers PENDING.
  • Divergence from JS: an async step's end hook fires when the service answers, with service timestamps. So it can come after the end hooks of later operations. It still fires once, before on_invocation_end.
  • Compatibility: .name and list(StepSemantics) keep the old names, and list(StepSemantics) adds AT_LEAST_ONCE_ASYNC at the end. The new names are the same members, and lookup by the old name or value still works. StepConfig.step_semantics keeps its default AT_LEAST_ONCE. An explicit value, also AT_LEAST_ONCE, wins over the execution default.
  • The conformance handlers keep the old names, which still work. AGENTS.md asks to coordinate conformance changes.

An AT_LEAST_ONCE_ASYNC step queues its SUCCEED checkpoint and returns
at once. RETRY and FAIL are still awaited.

The batcher now sends the checkpoints still queued when it is told to
stop. So the SUCCEED is saved before the invocation returns. A batch
that fails without the handler seeing it fails the invocation instead
of reporting SUCCEEDED.
AT_MOST_ONCE and AT_LEAST_ONCE are the new names. They keep the old
values, so nothing on the wire changes. AT_MOST_ONCE_PER_RETRY and
AT_LEAST_ONCE_PER_RETRY stay as deprecated aliases of the same members.
Code, tests, examples and docs use the new names.
durable_execution takes default_step_semantics. It sets the semantics
of every step whose StepConfig sets none, in every context of the
execution. A step's own step_semantics wins. The default stays
AT_LEAST_ONCE.

StepConfig.step_semantics now defaults to None, which means the
execution default.
The microvm and agentcore session ID steps feed a client token or a
runtime session to later steps. They now set AT_LEAST_ONCE themselves,
so default_step_semantics=AT_LEAST_ONCE_ASYNC cannot make them async.
They use the deprecated AT_LEAST_ONCE_PER_RETRY name, so the core
requirement stays >=3.0.0.
An AT_LEAST_ONCE_ASYNC step can return, and the handler can then raise
before the step's SUCCEED is sent. If that checkpoint then failed, the
invocation answered FAILED for the handler error, so the execution
ended without the step's result.

After the batcher stops, an invocation-scope checkpoint failure now
raises, so Lambda retries the invocation. An execution-scope failure,
such as a validation error, still answers FAILED. A handler error keeps
its own FAILED answer then.
The handler sets default_step_semantics=AT_LEAST_ONCE_ASYNC. The step
that makes the idempotency key sets AT_LEAST_ONCE itself. A wait ends
the first invocation, so the second invocation replays the saved steps.
The StepSemantics docstring, the StepConfig docstring, the README and
the example say AT_LEAST_ONCE_ASYNC is experimental, as the pause
option does. default_step_semantics, AT_LEAST_ONCE and AT_MOST_ONCE
are not experimental.
A handler can suspend while an AT_LEAST_ONCE_ASYNC SUCCEED is still
queued, for example on a replayed wait. close() sends it, and when that
call failed the invocation still answered PENDING. A permanent failure
then let each later invocation rerun the step, and a retryable one
waited for the timer. The late-failure check now covers PENDING too,
except when the token was withdrawn. Both cases now match
AT_LEAST_ONCE: FAILED, or a raised CheckpointError that Lambda retries.
@ParidelPooya
ParidelPooya marked this pull request as ready for review November 27, 2026 05:42
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 05:42 — with GitHub Actions Active
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 05:43 — with GitHub Actions Active
@github-actions

This comment has been minimized.

_apply_late_checkpoint_failure only receives SUCCEEDED, FAILED or
PENDING output from invoke_handler. So the status filter at its top
never returns early, and this change removes it.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 06:05 — with GitHub Actions Active
@github-actions

This comment has been minimized.

StepConfig.step_semantics keeps its type StepSemantics and its default
AT_LEAST_ONCE, as on main. So StepConfig().step_semantics.value works.

StepConfig records privately whether the caller set the field. So
DurableContext.step applies default_step_semantics only to a step
that did not set it, and an explicit AT_LEAST_ONCE wins. The flag is
an init field, so dataclasses.replace keeps it.
Add a test for the overflow part of the batcher's stop condition. An
async SUCCEED that does not fit next to a START goes to the overflow
queue. Stop is signaled while the START is in flight, so only the
overflow queue has work. The test runs the real batch loop in a thread
and checks that the SUCCEED is sent before the thread stops.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 15:33 — with GitHub Actions Active
@github-actions

This comment has been minimized.

dataclasses.replace cannot tell an explicit AT_LEAST_ONCE from the
default. Tell users to build StepConfig with step_semantics set to pin
AT_LEAST_ONCE under an async execution default.
Declaring AT_MOST_ONCE and AT_LEAST_ONCE first made them the canonical
members. That changed .name of the PerRetry members and the order of
list(StepSemantics).

Declare the PerRetry members first, as on main, and make the new names
aliases with the same values. Add a test for .name, identity, and
iteration order.
The checkpoint thread sets the revoked flag or the failed flag, then
leaves its loop. So both are never set, and the early return for a
revoked token could not change the answer.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 16:24 — with GitHub Actions Active
@github-actions

This comment has been minimized.

dataclasses.replace passes the stored step_semantics to the copy, so
an explicit AT_LEAST_ONCE looked unset and an async execution default
replaced it. The set flag is no longer an init field: __post_init__
sets it from the value it gets, so every replaced config counts as set.
A replaced config can then only lose the execution default, never turn
async when the caller pinned AT_LEAST_ONCE.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 18:00 — with GitHub Actions Active
@github-actions

This comment has been minimized.

The session ID pin tests read StepConfig.step_semantics. StepConfig
stores AT_LEAST_ONCE for both a set and an unset field, and the tests
ran under the default semantics. So they passed without the pin.

The tests now run the session under default_step_semantics set to
AT_LEAST_ONCE_ASYNC. They delay the answer to the session ID's SUCCEED
checkpoint. The AgentCore test checks that the session handler starts
only after the answer. The MicroVM test checks that the launch has not
run before the answer. Each test fails when its pin is removed.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 21:09 — with GitHub Actions Active
@github-actions

This comment has been minimized.

Sizing an update for a checkpoint batch can raise, for example when
PassThroughSerDes leaves bytes in the payload. This ran outside the
batcher's failure handling. So the error was not observed, waiters
were not woken, and an unsaved AT_LEAST_ONCE_ASYNC SUCCEED could
still answer SUCCEEDED.

Move the failure handling into _fail_checkpointing and call it for
both a failed API call and a failed batch collection. The operation
that failed to size is failed with the rest of the batch.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime November 27, 2026 22:41 — with GitHub Actions Active
@github-actions

Copy link
Copy Markdown
Contributor

Codex AI review

No actionable findings in the current diff. Tests were not executed under the read-only review constraints. The PR’s documented service-retry assumption remains unverified.

Reviewed commit 3bfac47834350fb29ddf2cc2192749073be672ce. Workflow run

@ParidelPooya
ParidelPooya merged commit de2c529 into main Nov 27, 2026
51 checks passed
@ParidelPooya
ParidelPooya deleted the feat/step-at-least-once-async branch November 27, 2026 23:55

This branch was successfully deployed

1 active deployment
ai-pr-review-runtime — 3bfac478 Deployed Nov 27, 2026 by ParidelPooya via ai-pr-review / Codex review / Generate Codex review #3511
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants