Skip to content

fix(sdk): reconcile recorded batch outcomes - #999

Open
MaxonT wants to merge 3 commits into
aws:mainfrom
MaxonT:contrib/835-auto
Open

MaxonT wants to merge 3 commits into
aws:mainfrom
MaxonT:contrib/835-auto

Conversation

@MaxonT

@MaxonT MaxonT commented Nov 27, 2026 •

Copy link
Copy Markdown

This is a small change to when a resumed map or parallel decides it is complete, so please check that waiting for the recorded items matches how you want replay to behave.

Thanks for the precise write-up of the [SUCCEEDED, SUCCEEDED, STARTED] case; it made the failing test easy to build.

Issue #, if available

Fixes #835

Description of changes

On resume, a map or parallel batch can contain child contexts that already recorded a terminal outcome. Their replays reach the coordinator in thread completion order, so the batch could decide it was complete before every recorded item had been settled, which left a recorded FAILED item reported as STARTED. ConcurrentExecutor now reads which NESTED child contexts already have a terminal record (_recorded_terminal_indexes), defers the completion decision until each of those has settled, and starts no new item past the last recorded index while any are unsettled. FLAT branches never checkpoint themselves, so they have no record and are unaffected.

Design

  • Design note (rule 3): not needed, no new feature, option or public API.
  • Simpler options considered (rule 2): I did not find one that keeps live behaviour unchanged; with no recorded items both new guards are no-ops (recorded_unsettled is empty and last_recorded is -1).
  • Root cause (rule 5): the completion check ran as soon as the policy said complete, before the replayed events of recorded items had all been applied.
  • Net change: about 38 added and 5 removed source lines in executor.py, no public API members.

Assumptions

  • Only NESTED child contexts carry a terminal record that matters here; I read this from the code and the existing FLAT replay tests, not from the service. Marked unverified against a real service run.

Decisions not covered by the issue or the design note

Checklist

  • Every new branch has a test that fails without it: removing the pending[0].index <= last_recorded condition, or the FLAT early return, makes tests fail.
  • The first commit adds a test that fails on the current code: not as separate commits here, the test and the fix are in one commit. The test (test_resumed_batch_settles_recorded_terminal_items_before_completing) gave [SUCCEEDED, SUCCEEDED, STARTED] instead of [..., FAILED] on the base executor.py when I ran it separately. I can split it into two commits if you want that.
  • No test expectation was edited.

How I checked: the full core suite gives 2284 passed with the fix (it needs the testing package installed), and ruff and mypy on the core source are clean. Not run: hatch run test:all, the CI checks script, commit-message lint, and the examples and wheel lanes. While running the suite repeatedly I saw parallel_test::test_parallel_item_deserialize fail about twice in 30 runs, and a different test once in 15 runs on main; both looked timing related and I did not chase them.

…etes

Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
@MaxonT
MaxonT deployed to ai-pr-review-runtime November 27, 2026 22:55 — with GitHub Actions Active

@yaythomas yaythomas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repository's commit-message check rejects the current 73-character subject because subjects are limited to 50 characters. Can the squash/merge subject use a shorter form, such as fix(sdk): reconcile recorded batch outcomes?

@@ -342,6 +356,7 @@ def submit(branch: Branch[CallableType, ResultType]) -> None:
pending
and in_flight < max_in_flight
and self.policy.should_continue(failed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coordinator postpones completion while recorded_unsettled is non-empty, but should_continue(failed) can stop admission before every recorded terminal branch is reached. Once the running branches drain, the coordinator raises SuspendExecution, and the same history repeats on the next invocation. Can the reconciling path continue through last_recorded independently of the normal stop rule, while omitting any unrecorded gap whose outcome is already known?

I reproduced this with the reachable history [FAILED, SUCCEEDED, SUCCEEDED, STARTED, SUCCEEDED], max_concurrency=3, and fail-fast behavior. Indices 0–2 start, index 0's cached failure arrives first, index 4 is never admitted, and execute() raises SuspendExecution. The JavaScript reference continues reconciliation and skips gap items. Can this include the equivalent fail-fast and failure-tolerance regression case from Python SDK #835: resumed batches can omit recorded terminal outcomes?

@@ -330,10 +341,13 @@ def submit(branch: Branch[CallableType, ResultType]) -> None:
)
decision = self.policy.evaluate(succeeded, failed, items_snapshot)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decision is recomputed and overwritten whenever a recorded item settles. A supported predicate can remain complete while changing its outcome, so resume returns the last outcome rather than the first completion decision that live execution used. Can this retain the first complete CompletionDecision during reconciliation and use it when calculating the completion reason?

I reproduced this with a recorded success followed by a delayed recorded failure. The predicate continues at zero progress, then always completes, first with SUCCEEDED and later with FAILED. This head returns CUSTOM_COMPLETION_FAILED instead of retaining CUSTOM_COMPLETION_SUCCEEDED. The predicate satisfies the documented monotonic contract, and the JavaScript parity implementation latches its first decision.

assert binding.get() == "empty"


def test_resumed_batch_settles_recorded_terminal_items_before_completing():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test function and its executor override are untyped, so mypy does not check their bodies. My type-check run passed but emitted annotation-unchecked for this test. Can these signatures be fully annotated as required by CONTRIBUTING.md's typing rule, including -> None, the executor's generic arguments, and the override parameters and return type?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the unchecked bodies. 237016f fully annotates the test: -> None, the ConcurrentExecutor[Callable[[], str], str] generic arguments, and the parameters and return type of the _execute_item_in_child_context override. It also annotates the evaluate wrapper and the local variables. mypy on tests/concurrency_test.py now reports no issues and no annotation-unchecked notes for this test. The test still fails on the base executor, and tests/concurrency_test.py passes (211 passed).

def _execute_item_in_child_context(self, executor_context, executable):
started.append(executable.index)
if executable.index == 2:
time.sleep(0.2) # replays its recorded failure after items 0, 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 200 ms delay makes the intended event order likely rather than guaranteed under a loaded test host. Can this use threading.Event or a barrier to hold and release item 2 at an observed point in the coordinator sequence?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 237016f. The 200 ms sleep is gone. Item 2 now waits on a threading.Event (5 s timeout). The event is set when the coordinator's evaluate first sees two successes. So item 2's recorded failure arrives after both successes are applied, which is the order that broke the base code.

The test fails on the base executor (index 2 is STARTED, not FAILED) and passes with the fix. tests/concurrency_test.py passes (211 passed) and ruff is clean. I haven't run the full suite or the hatch lanes. The executor.py threads on admission past last_recorded and on latching the first CompletionDecision are still open.

Annotate the regression test and its executor override so mypy checks
their bodies, and replace the fixed 200 ms delay with an event that
releases the failing item only after the coordinator has applied both
recorded successes. The test still fails on the base executor.
@MaxonT MaxonT changed the title fix(sdk): settle recorded terminal items before a resumed batch completes fix(sdk): reconcile recorded batch outcomes Dec 1, 2026
Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>

This branch was successfully deployed

1 active (outdated) deployment
ai-pr-review-runtime — 9719f6fd Deployed Nov 27, 2026 by MaxonT via notify / Generate notification summary #513
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Resumed map or parallel can omit recorded terminal item outcomes

2 participants