dex-python-sdk 1.43.0__tar.gz → 1.44.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/PKG-INFO +1 -1
  2. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/__init__.py +3 -0
  3. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_invocation_context.py +16 -0
  4. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/channel.py +5 -2
  5. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/context.py +2 -0
  6. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/flow.py +15 -3
  7. dex_python_sdk-1.44.0/dex/join.py +631 -0
  8. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/pyproject.toml +1 -1
  9. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/LEGACY_NOTICES.md +0 -0
  10. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/LICENSE +0 -0
  11. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/README.md +0 -0
  12. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_async_value_hydrator.py +0 -0
  13. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_async_worker_dispatcher.py +0 -0
  14. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_attribute_reads.py +0 -0
  15. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_credential_transport.py +0 -0
  16. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_flow_service_tls.py +0 -0
  17. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_grpc_errors.py +0 -0
  18. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_message_size.py +0 -0
  19. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_native.pyi +0 -0
  20. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_pull_worker.py +0 -0
  21. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_server_protocol.py +0 -0
  22. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_utils.py +0 -0
  23. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_value_hydrator.py +0 -0
  24. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_value_mapper.py +0 -0
  25. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/_worker_dispatcher.py +0 -0
  26. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/action.py +0 -0
  27. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/async_client.py +0 -0
  28. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/async_worker.py +0 -0
  29. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/attribute.py +0 -0
  30. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/attribute_match.py +0 -0
  31. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/attribute_values.py +0 -0
  32. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/blob_cache.py +0 -0
  33. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/client.py +0 -0
  34. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/client_options.py +0 -0
  35. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/codec.py +0 -0
  36. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/condition.py +0 -0
  37. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/credential.py +0 -0
  38. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/dexpb/__init__.py +0 -0
  39. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/dexpb/dex_pb2.py +0 -0
  40. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/dexpb/dex_pb2.pyi +0 -0
  41. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/dexpb/dex_pb2_grpc.py +0 -0
  42. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/flow_config.py +0 -0
  43. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/flow_info.py +0 -0
  44. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/flow_options.py +0 -0
  45. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/flow_result.py +0 -0
  46. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/flow_service_tls_options.py +0 -0
  47. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/pull_registration.py +0 -0
  48. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/py.typed +0 -0
  49. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/rpc_invoke_options.py +0 -0
  50. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/runtime_errors.py +0 -0
  51. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/step.py +0 -0
  52. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/step_execution.py +0 -0
  53. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/stream.py +0 -0
  54. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/subflow.py +0 -0
  55. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/timer.py +0 -0
  56. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/wait.py +0 -0
  57. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/wait_options.py +0 -0
  58. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/worker.py +0 -0
  59. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/dex/worker_options.py +0 -0
  60. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/Cargo.lock +0 -0
  61. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/Cargo.toml +0 -0
  62. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/Cargo.toml +0 -0
  63. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/LICENSE +0 -0
  64. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/README.md +0 -0
  65. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/config.rs +0 -0
  66. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/entry.rs +0 -0
  67. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/error.rs +0 -0
  68. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/format.rs +0 -0
  69. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/lib.rs +0 -0
  70. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/policy.rs +0 -0
  71. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/src/store.rs +0 -0
  72. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache/tests/blob_cache_integration.rs +0 -0
  73. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache-python/Cargo.toml +0 -0
  74. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache-python/LICENSE +0 -0
  75. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache-python/README.md +0 -0
  76. {dex_python_sdk-1.43.0 → dex_python_sdk-1.44.0}/sdk-rust/crates/dex-blob-cache-python/src/lib.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dex-python-sdk
3
- Version: 1.43.0
3
+ Version: 1.44.0
4
4
  Requires-Dist: grpcio>=1.83.0
5
5
  Requires-Dist: grpcio-status>=1.83.0
6
6
  Requires-Dist: protobuf>=7.35.1
@@ -75,6 +75,7 @@ from dex.flow_options import (
75
75
  TimeTravelStepMethod,
76
76
  TimeTravelType,
77
77
  )
78
+ from dex.join import Join, JoinRound
78
79
  from dex.flow_service_tls_options import FlowServiceTlsOptions
79
80
  from dex.flow_result import FlowResult, StepCompletion
80
81
  from dex.pull_registration import (
@@ -204,6 +205,8 @@ __all__ = [
204
205
  "IdReusePolicy",
205
206
  "InvalidStepResultError",
206
207
  "IndexType",
208
+ "Join",
209
+ "JoinRound",
207
210
  "JsonCodec",
208
211
  "LongPollTimeoutError",
209
212
  "RequestTimeoutError",
@@ -108,6 +108,7 @@ class InvocationContext:
108
108
  self._event_names: set[str] = set()
109
109
  self._step_output_finalizers: list[_StepOutputFinalizer] = []
110
110
  self._step_outputs_finalized = False
111
+ self._forked_join_names: set[str] = set()
111
112
 
112
113
  @property
113
114
  def flow_id(self) -> str:
@@ -468,6 +469,21 @@ class InvocationContext:
468
469
  )
469
470
  )
470
471
 
472
+ def _begin_join_fork(self, definition: ChannelMap[Any]) -> str:
473
+ self._require_registered(definition)
474
+ if self._method is not InvocationMethod.EXECUTE:
475
+ raise ValueError(
476
+ f"Join {definition.name} fork requires a Step execute Context; "
477
+ "use fork_with_round_key in an RPC handler"
478
+ )
479
+ if definition.name in self._forked_join_names:
480
+ raise ValueError(
481
+ f"Join {definition.name} was already forked in this Step execution; "
482
+ "use another Join or fork_with_round_key"
483
+ )
484
+ self._forked_join_names.add(definition.name)
485
+ return self.step_execution_id
486
+
471
487
  def _channel_size(
472
488
  self,
473
489
  definition: Channel[object] | ChannelMap[object],
@@ -75,7 +75,8 @@ class Channel(Generic[ValueT]):
75
75
  Channel size metadata loads automatically. Pending message envelopes require
76
76
  explicit load options; ChannelMessagesNotLoadedError means the snapshot did
77
77
  not load them, not that the queue is empty. A Channel cannot be observed
78
- through Client.get_attributes.
78
+ through Client.get_attributes. To wait for parallel branches of a Flow, use
79
+ :class:`~dex.Join` instead of counting signals on a Channel.
79
80
 
80
81
  Attributes:
81
82
  name: The non-empty Channel name without ``/``, unique within its Flow.
@@ -285,7 +286,9 @@ class ChannelMap(Generic[ValueT]):
285
286
  Instance keys must be non-empty and must not contain ``/``.
286
287
  Each instance follows Channel consumption and publication rules. Select exact
287
288
  instance messages with load_messages in RPC invocation options; declaring a
288
- map does not load all of its pending values.
289
+ map does not load all of its pending values. To wait for parallel branches of
290
+ a Flow, use :class:`~dex.Join`, which builds on a ChannelMap and chooses the
291
+ instance key and branch count for you.
289
292
 
290
293
  Attributes:
291
294
  name: The non-empty Channel name without ``/``, unique within its Flow.
@@ -390,6 +390,8 @@ class Context(Protocol):
390
390
  definition: ChannelMap[object],
391
391
  ) -> tuple[str, ...]: ...
392
392
 
393
+ def _begin_join_fork(self, definition: ChannelMap[Any]) -> str: ...
394
+
393
395
  def _write_stream(
394
396
  self,
395
397
  definition: Stream[ValueT],
@@ -48,6 +48,7 @@ from dex.channel import Channel, ChannelMap, ChannelMapLoad
48
48
  from dex.codec import Codec, CodecRegistry, WireKind
49
49
  from dex.context import AsyncContext, Context
50
50
  from dex.dexpb import dex_pb2 as pb
51
+ from dex.join import Join
51
52
  from dex.runtime_errors import FlowDefinitionError
52
53
  from dex.rpc_invoke_options import RPCInvokeOptions
53
54
  from dex.step import (
@@ -70,6 +71,7 @@ CallableT = TypeVar("CallableT", bound=Callable[..., Any])
70
71
  _PersistenceDefinition = (
71
72
  Attribute[Any] | AttributeMap[Any] | Channel[Any] | ChannelMap[Any] | Stream[Any]
72
73
  )
74
+ _DeclaredPersistenceDefinition = _PersistenceDefinition | Join[Any]
73
75
 
74
76
 
75
77
  @dataclass(frozen=True)
@@ -237,7 +239,8 @@ class PersistenceSchema:
237
239
 
238
240
  Attributes:
239
241
  attributes: Singleton and map Attribute definitions.
240
- channels: Singleton and map Channel definitions.
242
+ channels: Singleton and map Channel definitions. ``of`` stores each Join
243
+ as its underlying ChannelMap.
241
244
  streams: Best-effort Stream definitions.
242
245
 
243
246
  Examples:
@@ -249,11 +252,18 @@ class PersistenceSchema:
249
252
  streams: tuple[Stream[Any], ...] = ()
250
253
 
251
254
  @staticmethod
252
- def of(*definitions: _PersistenceDefinition) -> PersistenceSchema:
255
+ def of(*definitions: _DeclaredPersistenceDefinition) -> PersistenceSchema:
253
256
  """Partition persistence definitions into Attributes, Channels, and Streams.
254
257
 
258
+ A Join registers as its underlying ChannelMap, so its name must differ
259
+ from every other definition of the Flow.
260
+
261
+ .. versionchanged:: 1.44.0
262
+ Accepts Join definitions.
263
+
255
264
  Args:
256
- *definitions: Attribute, AttributeMap, Channel, ChannelMap, or Stream definitions.
265
+ *definitions: Attribute, AttributeMap, Channel, ChannelMap, Join, or
266
+ Stream definitions.
257
267
 
258
268
  Returns:
259
269
  A schema with each category kept in argument order.
@@ -269,6 +279,8 @@ class PersistenceSchema:
269
279
  attributes.append(definition)
270
280
  elif isinstance(definition, (Channel, ChannelMap)):
271
281
  channels.append(definition)
282
+ elif isinstance(definition, Join):
283
+ channels.append(definition.channel_map)
272
284
  elif isinstance(definition, Stream):
273
285
  streams.append(definition)
274
286
  else:
@@ -0,0 +1,631 @@
1
+ # Copyright (c) 2026 Super Durable, Inc.
2
+ #
3
+ # Licensed under the Sustainable Use License 1.0.
4
+ # You may not use this file except in compliance with the License.
5
+ # See the LICENSE file in the repository root.
6
+ #
7
+ # SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
8
+
9
+ from __future__ import annotations
10
+
11
+ from dataclasses import dataclass, field
12
+ from typing import Any, Callable, Generic, Sequence, TypeVar
13
+
14
+ from dex.channel import ChannelMap
15
+ from dex.condition import Condition
16
+ from dex.context import Context
17
+ from dex.step import StepDecision, StepMovement, dead_end
18
+
19
+ ResultT = TypeVar("ResultT")
20
+
21
+
22
+ @dataclass(frozen=True)
23
+ class JoinRound:
24
+ """Identify one round of a :class:`Join` and the number of its branches.
25
+
26
+ :meth:`Join.fork` and :meth:`Join.fork_with_round_key` create a JoinRound and
27
+ pass the same value to every branch builder and to the join builder. Put it in
28
+ the input of each branch Step, of the join Step, and of the next round's split
29
+ Step. Treat it as an opaque token: do not build one by hand, and do not change
30
+ its fields.
31
+
32
+ A JoinRound is part of persisted Step input. Its JSON form is
33
+ ``{"roundKey": string, "branchCount": integer}`` in every Dex SDK, so a Step
34
+ written in one SDK can hand it to a Step in another SDK.
35
+
36
+ .. versionadded:: 1.44.0
37
+
38
+ Attributes:
39
+ round_key: The ChannelMap instance key that holds this round's branch
40
+ results. :meth:`Join.fork` uses the split Step's execution ID, such as
41
+ ``"RequestQuotes-2"``. It is non-empty and contains no ``/``.
42
+ branch_count: The number of branches the fork started. It is zero or
43
+ greater.
44
+
45
+ Examples:
46
+ A branch Step whose whole input is the JoinRound::
47
+
48
+ class QuoteFromSupplierA(Step[JoinRound]):
49
+ def execute(self, context: Context, input: JoinRound) -> StepDecision:
50
+ return QUOTE_RECEIVED.end_branch(context, input, "A")
51
+ """
52
+
53
+ round_key: str = field(metadata={"dex_json_name": "roundKey"})
54
+ branch_count: int = field(metadata={"dex_json_name": "branchCount"})
55
+
56
+
57
+ class Join(Generic[ResultT]):
58
+ """Synchronize parallel branches of a Flow and collect one result from each branch.
59
+
60
+ Use a Join when one Step starts several branches and a later Step must wait for
61
+ all of them, the first of them, or ``n`` of them. The Join counts the branches
62
+ and chooses a separate round key for every fork, so the application keeps no
63
+ branch counter and no round number.
64
+
65
+ A Join is a :class:`~dex.ChannelMap` with the same name. Each branch result is
66
+ one ordinary ChannelMap message of type ``result_type``, stored in the instance
67
+ named by the round key. Dex Web and Client tools show the Join as that
68
+ ChannelMap. Register the Join in the Flow's ``PersistenceSchema``. Its name
69
+ must differ from every other Attribute, Channel, ChannelMap, and Stream of the
70
+ Flow.
71
+
72
+ How to use it:
73
+
74
+ 1. The split Step calls :meth:`fork` in ``execute``. It passes one builder per
75
+ branch and one builder for the join Step. Each builder receives the same
76
+ :class:`JoinRound` and returns a :class:`~dex.StepMovement`. Return the
77
+ movements with :func:`~dex.go_to_many`.
78
+ 2. Each branch reports exactly one result per round. Its last Step calls
79
+ :meth:`end_branch`, or :meth:`report_branch_result` when it continues
80
+ after reporting.
81
+ 3. The join Step waits with :meth:`for_all_branches`, :meth:`for_first_branch`,
82
+ or :meth:`for_branches`, and reads the results with :meth:`branch_results`.
83
+ 4. When the Flow forks the same Join again in a loop, pass the previous
84
+ JoinRound to the next split Step. Its ``wait_for`` waits with
85
+ :meth:`for_remaining_branches` to consume the results that arrived after
86
+ the join fired. This reset also blocks the next round until every branch of
87
+ the previous round has reported.
88
+
89
+ The condition methods return ordinary conditions. Combine them with other
90
+ conditions in :meth:`~dex.Wait.all_of`, :meth:`~dex.Wait.any_of`, and
91
+ :meth:`~dex.Wait.any_combination_of`. For example, ``Wait.any_of(
92
+ join.for_all_branches(round), Timer.by_duration(timedelta(hours=1)))`` gives
93
+ the join a deadline.
94
+
95
+ Rules and traps:
96
+
97
+ - Each branch reports exactly one result in each round. A branch that reports
98
+ twice is counted twice. A branch that never reports, for example because its
99
+ failure policy moves it to a recovery Step that does not report, keeps an
100
+ all-branches join waiting forever. Make recovery Steps report a result that
101
+ means failure, or give the join a Timer deadline.
102
+ - Branches only pass the JoinRound to :meth:`end_branch` or
103
+ :meth:`report_branch_result`. They must not wait on a Join condition.
104
+ - Only the Step built by the join builder waits on the round. Two Steps that
105
+ wait on one round split its results between them.
106
+ - One Step invocation forks one Join at most once. A second :meth:`fork` of
107
+ the same Join in the same invocation raises ``ValueError``. Use a second
108
+ Join, or :meth:`fork_with_round_key` with a different key.
109
+ - An RPC handler has no Step execution ID, so :meth:`fork` raises
110
+ ``ValueError`` there. Use :meth:`fork_with_round_key`.
111
+ - The ``received_count`` of :meth:`for_remaining_branches` must equal the
112
+ number of results the join consumed: ``1`` after :meth:`for_first_branch`,
113
+ ``count`` after :meth:`for_branches`, and ``branch_count`` after
114
+ :meth:`for_all_branches`.
115
+ - Without a reset, results that arrive after the join stay pending in the
116
+ round's instance until the Flow ends. They never count toward another round.
117
+ A Flow that forks in a loop should reset every round.
118
+ - A canceling join returns its decision with
119
+ :meth:`~dex.StepDecision.with_canceling_sibling_steps` and the branch Step
120
+ types. A canceled branch does not report. Sibling selection matches only the
121
+ first Step of each branch. When :meth:`fork_with_round_key` runs in an RPC,
122
+ every Step started by any call of that RPC is a sibling, so cancel those
123
+ branches explicitly with :meth:`~dex.StepDecision.with_canceling_steps`
124
+ while only one round is active.
125
+ - A canceled branch never reports, so a canceling join's round cannot be
126
+ fully reset by :meth:`for_remaining_branches` alone. Its
127
+ ``received_count`` is still the number of results the join consumed, not
128
+ a count that includes canceled branches. To reset such a round, report a
129
+ result for each canceled branch, for example from an RPC with
130
+ :meth:`report_branch_result`. Otherwise start the next round without
131
+ the reset; the late results stay pending until the Flow ends.
132
+ - A branch canceled from outside never reports, so an all-branches join waits
133
+ forever. Cancel the join Step too, report a result for the canceled branch
134
+ from the RPC that cancels it with :meth:`report_branch_result`, or give the
135
+ join a Timer deadline.
136
+ - When a join Step is canceled after its wait matched but before its
137
+ ``execute`` committed, what happens to the results that wait consumed
138
+ depends on the server version. A server that requeues consumed messages
139
+ on cancellation returns them to the round, so a new join for the round
140
+ sees them. An older server drops them, and a new join waits forever. See
141
+ the server change log at https://docs.superdurable.io/reference/server/changelog.
142
+
143
+ .. versionadded:: 1.44.0
144
+
145
+ Examples:
146
+ A two-round partial join. Each round accepts the first two of three
147
+ quotes, and the next round resets the previous one::
148
+
149
+ from dataclasses import dataclass
150
+
151
+ from dex import (
152
+ Channel, Context, Flow, Join, JoinRound, PersistenceSchema, Step,
153
+ StepDecision, StepList, StepMovement, Wait, go_to_many,
154
+ graceful_complete, rpc,
155
+ )
156
+
157
+ QUOTE_RECEIVED = Join("quoteReceived", str)
158
+ SUPPLIER_RELEASED = Channel("supplierReleased", str)
159
+ REQUIRED_QUOTES = 2
160
+
161
+ @dataclass
162
+ class QuoteRequest:
163
+ round_number: int
164
+ previous_round: JoinRound | None = None
165
+
166
+ @dataclass
167
+ class QuoteJoinInput:
168
+ request: QuoteRequest
169
+ join_round: JoinRound
170
+
171
+ class RequestQuotes(Step[QuoteRequest]):
172
+ def wait_for(self, context: Context, input: QuoteRequest) -> Wait:
173
+ if input.previous_round is None:
174
+ return Wait.skip_immediately()
175
+ return Wait.until(
176
+ QUOTE_RECEIVED.for_remaining_branches(
177
+ input.previous_round, REQUIRED_QUOTES
178
+ )
179
+ )
180
+
181
+ def execute(self, context: Context, input: QuoteRequest) -> StepDecision:
182
+ if input.previous_round is not None:
183
+ late_quotes = QUOTE_RECEIVED.remaining_branch_results(
184
+ context, input.previous_round
185
+ )
186
+ context.record_event("lateQuotes", list(late_quotes))
187
+ movements = QUOTE_RECEIVED.fork(
188
+ context,
189
+ [
190
+ lambda join_round: StepMovement.of(QuoteFromSupplier, join_round),
191
+ lambda join_round: StepMovement.of(QuoteFromSupplier, join_round),
192
+ lambda join_round: StepMovement.of(QuoteFromSupplier, join_round),
193
+ ],
194
+ lambda join_round: StepMovement.of(
195
+ AcceptQuotes, QuoteJoinInput(input, join_round)
196
+ ),
197
+ )
198
+ return go_to_many(*movements)
199
+
200
+ class QuoteFromSupplier(Step[JoinRound]):
201
+ def wait_for(self, context: Context, input: JoinRound) -> Wait:
202
+ return Wait.until(SUPPLIER_RELEASED.for_one())
203
+
204
+ def execute(self, context: Context, input: JoinRound) -> StepDecision:
205
+ supplier = SUPPLIER_RELEASED.results(context)[0]
206
+ return QUOTE_RECEIVED.end_branch(context, input, supplier)
207
+
208
+ class AcceptQuotes(Step[QuoteJoinInput]):
209
+ def wait_for(self, context: Context, input: QuoteJoinInput) -> Wait:
210
+ return Wait.until(
211
+ QUOTE_RECEIVED.for_branches(input.join_round, REQUIRED_QUOTES)
212
+ )
213
+
214
+ def execute(self, context: Context, input: QuoteJoinInput) -> StepDecision:
215
+ accepted = QUOTE_RECEIVED.branch_results(context, input.join_round)
216
+ if input.request.round_number == 2:
217
+ return graceful_complete(list(accepted))
218
+ next_request = QuoteRequest(
219
+ input.request.round_number + 1, input.join_round
220
+ )
221
+ return go_to_many(StepMovement.of(RequestQuotes, next_request))
222
+
223
+ class QuoteFlow(Flow[QuoteRequest]):
224
+ @rpc
225
+ def release_supplier(self, context: Context, supplier: str) -> None:
226
+ SUPPLIER_RELEASED.publish(context, supplier)
227
+
228
+ def get_steps(self) -> StepList[QuoteRequest]:
229
+ return StepList.start_step(RequestQuotes()).other_steps(
230
+ QuoteFromSupplier(), AcceptQuotes()
231
+ )
232
+
233
+ def get_persistence_schema(self) -> PersistenceSchema:
234
+ return PersistenceSchema.of(QUOTE_RECEIVED, SUPPLIER_RELEASED)
235
+
236
+ A run-time number of branches. Map the inputs to builders with a helper
237
+ function, which binds each input; a lambda written directly in the
238
+ comprehension would see only the last input::
239
+
240
+ from dataclasses import dataclass
241
+ from typing import Callable
242
+
243
+ from dex import (
244
+ Context, Flow, Join, JoinRound, PersistenceSchema, Step,
245
+ StepDecision, StepList, StepMovement, Wait, go_to_many,
246
+ graceful_complete,
247
+ )
248
+
249
+ REVIEW_DONE = Join("reviewDone", str)
250
+
251
+ @dataclass
252
+ class ReviewInput:
253
+ reviewer: str
254
+ join_round: JoinRound
255
+
256
+ def build_review(reviewer: str) -> Callable[[JoinRound], StepMovement[ReviewInput]]:
257
+ return lambda join_round: StepMovement.of(
258
+ ReviewDocument, ReviewInput(reviewer, join_round)
259
+ )
260
+
261
+ class RequestReviews(Step[list[str]]):
262
+ def execute(self, context: Context, input: list[str]) -> StepDecision:
263
+ movements = REVIEW_DONE.fork(
264
+ context,
265
+ [build_review(reviewer) for reviewer in input],
266
+ lambda join_round: StepMovement.of(CollectReviews, join_round),
267
+ )
268
+ return go_to_many(*movements)
269
+
270
+ class ReviewDocument(Step[ReviewInput]):
271
+ def execute(self, context: Context, input: ReviewInput) -> StepDecision:
272
+ return REVIEW_DONE.end_branch(
273
+ context, input.join_round, f"approved by {input.reviewer}"
274
+ )
275
+
276
+ class CollectReviews(Step[JoinRound]):
277
+ def wait_for(self, context: Context, input: JoinRound) -> Wait:
278
+ return Wait.until(REVIEW_DONE.for_all_branches(input))
279
+
280
+ def execute(self, context: Context, input: JoinRound) -> StepDecision:
281
+ return graceful_complete(list(REVIEW_DONE.branch_results(context, input)))
282
+
283
+ class ReviewFlow(Flow[list[str]]):
284
+ def get_steps(self) -> StepList[list[str]]:
285
+ return StepList.start_step(RequestReviews()).other_steps(
286
+ ReviewDocument(), CollectReviews()
287
+ )
288
+
289
+ def get_persistence_schema(self) -> PersistenceSchema:
290
+ return PersistenceSchema.of(REVIEW_DONE)
291
+ """
292
+
293
+ def __init__(self, name: str, result_type: type[ResultT]) -> None:
294
+ """Define a Join whose branch results use the ChannelMap named ``name``.
295
+
296
+ Define each Join once, for example as a module constant, and register that
297
+ same object in the Flow's ``PersistenceSchema``.
298
+
299
+ Args:
300
+ name: The durable Join name. It is also the ChannelMap name, so it is
301
+ non-empty, contains no ``/``, and is unique within the Flow.
302
+ Renaming it orphans the results of running Flows.
303
+ result_type: The Python type of every branch result. Changing it
304
+ must keep already published results decodable.
305
+
306
+ Raises:
307
+ ValueError: If ``name`` is empty or contains ``/``.
308
+ """
309
+ self._channel_map: ChannelMap[ResultT] = ChannelMap(name, result_type)
310
+
311
+ @property
312
+ def name(self) -> str:
313
+ """Return the durable Join name.
314
+
315
+ Returns:
316
+ The name passed to the constructor, which is also the name of the
317
+ underlying ChannelMap.
318
+ """
319
+ return self._channel_map.name
320
+
321
+ @property
322
+ def result_type(self) -> type[ResultT]:
323
+ """Return the Python type of every branch result.
324
+
325
+ Returns:
326
+ The result type passed to the constructor.
327
+ """
328
+ return self._channel_map.value_type
329
+
330
+ @property
331
+ def channel_map(self) -> ChannelMap[ResultT]:
332
+ """Return the ChannelMap that holds this Join's branch results.
333
+
334
+ Use it to inspect a round's pending results with the existing ChannelMap
335
+ API. The instance key of a round is its :attr:`JoinRound.round_key`. For
336
+ example, an RPC declares ``@rpc(load_channel_maps=(join.channel_map,))``
337
+ and then reads ``join.channel_map.pending_messages(context,
338
+ join_round.round_key)``, or reads the count with ``size``. The returned
339
+ object is the definition that ``PersistenceSchema.of`` registers for this
340
+ Join, so every API that checks registration accepts it.
341
+
342
+ Never publish to it, delete from it, or wait on it directly. Report
343
+ results with :meth:`end_branch` or :meth:`report_branch_result`, and wait
344
+ with the Join condition methods. A direct publication counts as a branch
345
+ result and breaks the exactly-once count of the round.
346
+
347
+ .. versionadded:: 1.44.0
348
+
349
+ Returns:
350
+ The registered ChannelMap with the Join's name and result type.
351
+ """
352
+ return self._channel_map
353
+
354
+ def fork(
355
+ self,
356
+ context: Context,
357
+ branch_builders: Sequence[Callable[[JoinRound], StepMovement[Any]]],
358
+ join_builder: Callable[[JoinRound], StepMovement[Any]],
359
+ ) -> tuple[StepMovement[Any], ...]:
360
+ """Start a new round and build the movements of its branches and its join Step.
361
+
362
+ Call it in the split Step's ``execute``. The round key is the current Step
363
+ execution ID, which stays the same across retries and is unique within the
364
+ run. The branch count is ``len(branch_builders)``. The method builds the
365
+ JoinRound first, then calls each branch builder in order, then the join
366
+ builder. It makes no server call.
367
+
368
+ An empty ``branch_builders`` creates a round with zero branches.
369
+ :meth:`for_all_branches` on that round completes immediately.
370
+
371
+ Args:
372
+ context: The current Step ``execute`` Context.
373
+ branch_builders: One builder per branch. Each receives the JoinRound
374
+ and returns the movement that starts its branch.
375
+ join_builder: Receives the JoinRound and returns the movement of the
376
+ Step that waits for the branches.
377
+
378
+ Returns:
379
+ The branch movements in builder order, followed by the join movement.
380
+ Pass them to :func:`~dex.go_to_many`, optionally with other movements.
381
+
382
+ Raises:
383
+ ValueError: If the Context is not a Step ``execute`` Context, for
384
+ example in an RPC handler, if this invocation already forked this
385
+ Join, or if the Join is not registered with the current Flow.
386
+ """
387
+ round_key = context._begin_join_fork(self._channel_map)
388
+ return self._build_round_movements(round_key, branch_builders, join_builder)
389
+
390
+ def fork_with_round_key(
391
+ self,
392
+ round_key: str,
393
+ branch_builders: Sequence[Callable[[JoinRound], StepMovement[Any]]],
394
+ join_builder: Callable[[JoinRound], StepMovement[Any]],
395
+ ) -> tuple[StepMovement[Any], ...]:
396
+ """Start a round with an application round key and build its movements.
397
+
398
+ Use it in an RPC handler, which has no Step execution ID, or to keep the
399
+ instance keys of an existing hand-written ChannelMap join. Every round of
400
+ this Join that can be active at the same time needs a distinct key. Reusing
401
+ a key mixes the results of two rounds. Otherwise it behaves like
402
+ :meth:`fork`.
403
+
404
+ Args:
405
+ round_key: The ChannelMap instance key for this round. It must be
406
+ non-empty and must not contain ``/``.
407
+ branch_builders: One builder per branch. Each receives the JoinRound
408
+ and returns the movement that starts its branch.
409
+ join_builder: Receives the JoinRound and returns the movement of the
410
+ Step that waits for the branches.
411
+
412
+ Returns:
413
+ The branch movements in builder order, followed by the join movement.
414
+ Return them with ``go_to_many`` or as RPC next Steps.
415
+
416
+ Raises:
417
+ ValueError: If ``round_key`` is empty or contains ``/``.
418
+ """
419
+ if not round_key.strip() or "/" in round_key:
420
+ raise ValueError(
421
+ f"Join {self.name} round key must be non-empty and must not contain '/'"
422
+ )
423
+ return self._build_round_movements(round_key, branch_builders, join_builder)
424
+
425
+ def end_branch(
426
+ self, context: Context, join_round: JoinRound, result: ResultT
427
+ ) -> StepDecision:
428
+ """Report this branch's result and end the branch.
429
+
430
+ Return its value from the last Step of a branch. The result commits with
431
+ the Step's decision. A failed attempt reports nothing, and a retry does
432
+ not report twice.
433
+
434
+ Args:
435
+ context: The current Step Context.
436
+ join_round: The JoinRound this branch received.
437
+ result: This branch's result.
438
+
439
+ Returns:
440
+ A dead-end decision. Other paths of the Flow continue.
441
+ """
442
+ self.report_branch_result(context, join_round, result)
443
+ return dead_end()
444
+
445
+ def report_branch_result(
446
+ self, context: Context, join_round: JoinRound, result: ResultT
447
+ ) -> None:
448
+ """Report a branch result without ending the current Step path.
449
+
450
+ Use it when a branch continues after it reports, or in an RPC that reports
451
+ for a branch it cancels. The result commits only when the handler's result
452
+ commits.
453
+
454
+ Args:
455
+ context: The current Step or RPC Context.
456
+ join_round: The JoinRound of the branch being reported.
457
+ result: The branch result.
458
+
459
+ Raises:
460
+ ValueError: If the JoinRound is invalid or the Join is not registered
461
+ with the current Flow.
462
+ """
463
+ self._require_valid_round(join_round)
464
+ self._channel_map.publish(context, join_round.round_key, result)
465
+
466
+ def for_all_branches(
467
+ self, join_round: JoinRound, *, condition_id: str | None = None
468
+ ) -> Condition:
469
+ """Create a condition that waits for a result from every branch of a round.
470
+
471
+ It consumes ``branch_count`` results. A round with zero branches completes
472
+ immediately.
473
+
474
+ Args:
475
+ join_round: The round to wait for.
476
+ condition_id: Optional stable condition identifier.
477
+
478
+ Returns:
479
+ A condition for the join Step's ``wait_for``.
480
+
481
+ Raises:
482
+ ValueError: If the JoinRound is invalid.
483
+ """
484
+ self._require_valid_round(join_round)
485
+ return self._channel_map.for_n(
486
+ join_round.round_key, join_round.branch_count, condition_id=condition_id
487
+ )
488
+
489
+ def for_first_branch(
490
+ self, join_round: JoinRound, *, condition_id: str | None = None
491
+ ) -> Condition:
492
+ """Create a condition that waits for the first result of a round.
493
+
494
+ It consumes exactly one result, the earliest committed one. Results that
495
+ were already pending stay for :meth:`for_remaining_branches`. Put the
496
+ winner's identity in the result when the join needs it. A round with
497
+ zero branches raises ``ValueError``, because the wait would never
498
+ complete. It behaves like ``for_branches(join_round, 1)``.
499
+
500
+ Args:
501
+ join_round: The round to wait for. It needs at least one branch.
502
+ condition_id: Optional stable condition identifier.
503
+
504
+ Returns:
505
+ A condition for the join Step's ``wait_for``.
506
+
507
+ Raises:
508
+ ValueError: If the JoinRound is invalid or has no branches.
509
+ """
510
+ return self.for_branches(join_round, 1, condition_id=condition_id)
511
+
512
+ def for_branches(
513
+ self, join_round: JoinRound, count: int, *, condition_id: str | None = None
514
+ ) -> Condition:
515
+ """Create a condition that waits for ``count`` results of a round.
516
+
517
+ It consumes exactly ``count`` results in commit order. Use it for a
518
+ partial join that continues after ``count`` of the branches.
519
+
520
+ Args:
521
+ join_round: The round to wait for.
522
+ count: The number of results to wait for, from 1 to ``branch_count``.
523
+ condition_id: Optional stable condition identifier.
524
+
525
+ Returns:
526
+ A condition for the join Step's ``wait_for``.
527
+
528
+ Raises:
529
+ ValueError: If the JoinRound is invalid or ``count`` is outside
530
+ ``[1, branch_count]``.
531
+ """
532
+ self._require_valid_round(join_round)
533
+ if not 1 <= count <= join_round.branch_count:
534
+ raise ValueError(
535
+ f"Join {self.name} branch count {count} must be between 1 and "
536
+ f"{join_round.branch_count}"
537
+ )
538
+ return self._channel_map.for_n(
539
+ join_round.round_key, count, condition_id=condition_id
540
+ )
541
+
542
+ def branch_results(
543
+ self, context: Context, join_round: JoinRound
544
+ ) -> Sequence[ResultT]:
545
+ """Return the results that the join Step's wait consumed for a round.
546
+
547
+ Args:
548
+ context: The join Step's ``execute`` Context.
549
+ join_round: The round the Step waited for.
550
+
551
+ Returns:
552
+ The consumed results in commit order. The sequence is empty when the
553
+ Step did not wait on this round or its condition did not win.
554
+ """
555
+ return self._channel_map.results(context, join_round.round_key)
556
+
557
+ def for_remaining_branches(
558
+ self,
559
+ join_round: JoinRound,
560
+ received_count: int,
561
+ *,
562
+ condition_id: str | None = None,
563
+ ) -> Condition:
564
+ """Create a condition that waits for and consumes the rest of a round's results.
565
+
566
+ This is the reset of a round. Use it in the next round's split Step
567
+ ``wait_for``. It waits for ``branch_count - received_count`` results, so
568
+ the next round starts only after every branch of the previous round has
569
+ reported. When nothing remains, it completes immediately.
570
+
571
+ Args:
572
+ join_round: The previous round.
573
+ received_count: The number of results the previous join consumed,
574
+ from 0 to ``branch_count``. Pass the same count the join waited
575
+ for, not a counter.
576
+ condition_id: Optional stable condition identifier.
577
+
578
+ Returns:
579
+ A condition for the next split Step's ``wait_for``.
580
+
581
+ Raises:
582
+ ValueError: If the JoinRound is invalid or ``received_count`` is
583
+ outside ``[0, branch_count]``.
584
+ """
585
+ self._require_valid_round(join_round)
586
+ if not 0 <= received_count <= join_round.branch_count:
587
+ raise ValueError(
588
+ f"Join {self.name} received count {received_count} must be between "
589
+ f"0 and {join_round.branch_count}"
590
+ )
591
+ return self._channel_map.for_n(
592
+ join_round.round_key,
593
+ join_round.branch_count - received_count,
594
+ condition_id=condition_id,
595
+ )
596
+
597
+ def remaining_branch_results(
598
+ self, context: Context, join_round: JoinRound
599
+ ) -> Sequence[ResultT]:
600
+ """Return the late results that the reset of a round consumed.
601
+
602
+ Args:
603
+ context: The ``execute`` Context of the Step that waited with
604
+ :meth:`for_remaining_branches`.
605
+ join_round: The previous round.
606
+
607
+ Returns:
608
+ The consumed late results in commit order. The sequence is empty when
609
+ nothing remained or the Step did not wait on this round.
610
+ """
611
+ return self._channel_map.results(context, join_round.round_key)
612
+
613
+ def _build_round_movements(
614
+ self,
615
+ round_key: str,
616
+ branch_builders: Sequence[Callable[[JoinRound], StepMovement[Any]]],
617
+ join_builder: Callable[[JoinRound], StepMovement[Any]],
618
+ ) -> tuple[StepMovement[Any], ...]:
619
+ join_round = JoinRound(round_key, len(branch_builders))
620
+ branch_movements = tuple(builder(join_round) for builder in branch_builders)
621
+ return (*branch_movements, join_builder(join_round))
622
+
623
+ def _require_valid_round(self, join_round: JoinRound) -> None:
624
+ if not join_round.round_key.strip() or "/" in join_round.round_key:
625
+ raise ValueError(
626
+ f"Join {self.name} round key must be non-empty and must not contain '/'"
627
+ )
628
+ if join_round.branch_count < 0:
629
+ raise ValueError(
630
+ f"Join {self.name} branch count {join_round.branch_count} must not be negative"
631
+ )
@@ -1,7 +1,7 @@
1
1
  [project]
2
2
  name = "dex-python-sdk"
3
3
  # Publish uses tag sdk-python/v*; CI stamps this for release builds.
4
- version = "1.43.0"
4
+ version = "1.44.0"
5
5
  description = "Python SDK for the Dex workflow engine"
6
6
  authors = [{name = "Super Durable"}]
7
7
  readme = "README.md"
File without changes