Skip to main content

Programming Model

View as Markdown

Temporal replays Workflow code against its Event History, so new code must produce the same commands for runs that are already open. A Dex Flow is a set of Steps, and each Step runs the code of the Worker that takes it.

Watch on YouTube: The same process in Temporal and Dex code

Lines of code

The agent-reply example drafts a reply, waits for a human to approve it, and sends it. Version 2 adds a step after the draft that notifies the reviewer. The table counts the lines of each flow definition file, without blank and comment lines.

TemporalDex
Version 1, flow_v1.py2626
Version 2, flow_v2.py3430
Change from version 1 to 28 lines added6 lines added, 2 removed

The Temporal change adds the notify_reviewer Activity and guards it with workflow.patched. Without the guard, open runs fail, as shown below. See flow_v2.py.

Diff
+@activity.defn
+async def notify_reviewer(draft: str) -> str:
+ return f"reviewer notified: {draft}"
+
+
@activity.defn
async def send_reply(draft: str) -> str:
@@
draft = await workflow.execute_activity(
draft_reply, ticket,
start_to_close_timeout=timedelta(seconds=10),
)
+ if workflow.patched("notify-reviewer"):
+ await workflow.execute_activity(
+ notify_reviewer, draft,
+ start_to_close_timeout=timedelta(seconds=10),
+ )
await workflow.wait_condition(lambda: self.approved)

The Dex change adds NotifyReviewerStep and points DraftReplyStep to it. It has no versioning code. See flow_v2.py.

Diff
 class DraftReplyStep(Step[str]):
def execute(self, context: Context, input: str) -> StepDecision:
- return go_to(SendReplyStep, f"draft reply for {input}")
+ return go_to(NotifyReviewerStep, f"draft reply for {input}")
+
+
+class NotifyReviewerStep(Step[str]):
+ def execute(self, context: Context, input: str) -> StepDecision:
+ return go_to(SendReplyStep, input)
@@
self.draft_reply = DraftReplyStep()
+ self.notify_reviewer = NotifyReviewerStep()
self.send_reply = SendReplyStep()
@@
return StepList.start_step(self.draft_reply).other_steps(
- self.send_reply
+ self.notify_reviewer, self.send_reply
)

The demo started replies on version 1, stopped that Worker, and deployed version 2. On Temporal, this run used version 2 without workflow.patched.

Reply at the deployTemporalDex
Approved, sending the reply, and retrying itThe next Workflow Task fails with a nondeterminism error. The run stays Running.Completes.
Waiting for approvalCompletes, and notifies the reviewer after the approval.Completes without NotifyReviewerStep.
Completedtemporal workflow stack fails with the same error.Reading the result works, with or without a Worker.

The error on Temporal:

[TMPRL1100] Nondeterminism error: Activity type of scheduled event 'send_reply' does not match activity type of activity command 'notify_reviewer'

With workflow.patched, the approved Temporal runs complete too. The demo repository lists the versions used for these results.

The scenario

Watch at 0:40

The rest of this page follows Episode 1. An AI support agent answers a customer. One Temporal workflow or one Dex Flow runs per conversation.

  • The conversation starts with the customer's first message.
  • Each customer message gets one AI reply.
  • After a reply, a 10-minute idle timer starts. A follow-up message is answered, and the timer starts again after that reply.
  • If the customer sends nothing for 10 minutes, the application sends a nudge email. One hour after the nudge with no message, the conversation closes.

The demos use 30 seconds in place of 10 minutes. In the timer demo, the first message arrives at second 0 and a follow-up at second 20. The nudge is due 30 seconds after the second reply, at second 50.

The natural Temporal code

Watch at 3:55

This is one direct way to write the timer. The Signal handler replies to the message and sets a nudge_at field. The main function sleeps until nudge_at, then sends the nudge. See naive_conversation_workflow.py.

A real run

Watch at 4:51

The nudge goes out at second 30. The run reports no error and raises no alert.

ImplementationNudge sent atErrors
Temporal, NaiveConversationWorkflow30.0 snone
Temporal, ConversationWorkflow50.0 snone
Dex, SupportConversationFlow50.4 snone

Why the timer fires early

Watch at 5:49

Temporal runs workflow code as workflow tasks. The Worker completes each workflow task with commands: "A Command is an action a Worker requests from the Temporal Service when it completes a Workflow Task Execution."

A timer is one of those commands. The call to sleep sends a StartTimer command, and the server records the timer with the first value of nudge_at. The Signal handler later changes nudge_at. Changing a variable sends no command. So the first timer still fires at second 30.

The code is valid Python, and the Timer fired as scheduled. The code does not show which statements become commands, or when.

The correct Temporal code

Watch at 8:06

The correct workflow follows the official updatable timer sample. It gets five things right:

  1. The Signal handler only puts the message in a queue. It does no work.
  2. The main function is a loop.
  3. Each pass answers every queued message, in order.
  4. workflow.wait_condition with a timeout waits for a new message or the end of the idle window.
  5. The timeout raises asyncio.TimeoutError, which the code catches.

See conversation_workflow.py.

The Dex Flow

Watch at 9:28

A Step says what it waits for and where it goes next. WaitForCustomerStep waits for any of two conditions: a message on the CustomerMessages Channel, or a Timer for the idle window. If the Timer fired, the Flow moves to NudgeCustomerStep. Otherwise it moves to GenerateReplyStep, which replies and moves back to WaitForCustomerStep. Each time WaitForCustomerStep starts, it starts a new Timer. The post_message RPC publishes each customer message to the Channel. See flow.py.

The Idle Timer pattern has the same shape in every SDK. Notice the two conditions in WaitFor and the decision in Execute, which moves the Step to itself on activity:

inactiveness_tracker_flow.pyPython
class Tracker(Step[None]):
def __init__(
self,
process_inactiveness: ProcessInactiveness,
active_channel: Channel[None],
) -> None:
self.process_inactiveness = process_inactiveness
self.active_channel = active_channel

def wait_for(self, context: Context, input: None) -> Wait:
return Wait.any_of(
Timer.by_duration(TRACKER_DURATION),
self.active_channel.for_one(),
)

def execute(self, context: Context, input: None) -> StepDecision:
if context.has_timer_fired():
return go_to(ProcessInactiveness, None)
return go_to(Tracker, None)

Example: examples/python/dex_examples/patterns/inactiveness-tracker-timer/inactiveness_tracker_flow.py

The Step has one wait and one decision. The Timer and the Channel are the same Wait, so the code needs no queue, loop, or caught exception.

Changing the code

Watch at 10:54

A new requirement arrives while conversations are open: analyze the sentiment of each customer message before replying.

Temporal. Version 2 runs an analyze_sentiment activity before generate_reply. Conversation A started on version 1. After the deploy, its next message is accepted and never answered. The workflow task fails and is retried, and the run stays open:

[TMPRL1100] Nondeterminism error: Activity type of scheduled event 'generate_reply' does not match activity type of activity command 'analyze_sentiment'

Queries to conversation A fail too. A conversation started on version 2 works. Temporal documents two fixes:

  • Patching. Guard the new code with workflow.patched. Temporal describes three steps. First, patch in the new code and run it alongside the old code. Second, remove the old code and mark the patch with deprecate_patch(). Third, remove deprecate_patch() once no open Workflow Execution runs the previous version of the code. For the second and third steps, Temporal also says to wait until the Workflows started with the pre-patch code "have left retention". In the demo, version 3 with workflow.patched let conversation A recover without a reset. Conversation A never runs the sentiment activity, because its history has no patch marker. In the demo, conversation B had started on the unguarded version 2. On version 3, it failed with the mirrored non-determinism error.
  • Worker Versioning. "A Pinned Workflow is guaranteed to complete on a single Worker Deployment Version." Other versions can keep polling "to allow pinned Workflows to finish executing". An Auto-Upgrade Workflow moves to its target version, which is the Current Version or the Ramping Version of its Task Queue. Auto-Upgrade Workflows "need to be kept replay-safe manually", with patching.

Dex. Version 2 adds AnalyzeSentimentStep. WaitForCustomerStep and WaitAfterNudgeStep now move to it instead of GenerateReplyStep. See flow_v2.py.

  • Conversation A was waiting in WaitForCustomerStep, which a version 1 Worker had started. After the deploy, a version 2 Worker handled its next message. The message went through AnalyzeSentimentStep, then got its reply.
  • A conversation started on version 2 begins at AnalyzeSentimentStep.
  • A wait that started before the deploy keeps its Timer. In a variation that also shortens the idle window, the old 60-second Timer fired at 60.1 seconds. The next wait used the new 10-second window.

Deploy Code Changes covers what a deploy changes for open Flows and how to keep them working.

The complete code

Watch at 20:24

TemporalDex
Programconversation_workflow_inline_transcript.py and activities.pyflow.py
Post a customer messageSignal post_messageRPC post_message, which publishes to a Channel
Assign an agentUpdate assign_agentRPC assign_agent
Read the transcriptQuery get_transcript_pageRPC get_transcript_page
Control flowImplicit in the workflow tasks that the code producesExplicit Steps and transitions

Because the transitions are explicit, Dex draws the Flow design from the source code. dexcli visualize produces the graph, and Dex Web draws it. A run's page in Dex Web shows the Step that the conversation waits in. The demo's flow-definitions folder holds the graphs of its Flow versions. This is the graph of the Idle Timer pattern above:

Flow visualization describes the analyzer and the canvas.

Run it yourself

The demo needs uv, the temporal CLI, and dexcli. Each script starts its own server and prints its results. Both servers run in their local development mode with default limits. The AI reply and the sentiment analysis are local mocks, not a real model.

The Episode 1 scripts are in the scripts folder, with their versions used. The reply-with-approval scripts are in the agent-reply folder and also need python3.

Every script except d-design.sh stops what it started. d-design.sh keeps Dex running after a successful run, because its default is KEEP=1. Stop it with scripts/dex/stop.sh, or start the script with KEEP=0.

TopicTemporalDex
Reply with approval, version 1 and 2agent-reply/temporal/run.sh, upgrade.sh, upgrade-in-send.sh, query-completed.shagent-reply/dex/run.sh, upgrade.sh, upgrade-in-send.sh, read-completed.sh
The idle timerscripts/temporal/t1-naive-timer.shscripts/dex/d1-timer.sh
Changing the codescripts/temporal/t2-change-code.sh, scripts/temporal/t2-patch-fix.shscripts/dex/d2-change-code.sh, scripts/dex/d2-idle-window-variation.sh
The history limitscripts/temporal/t5-flood.sh, scripts/temporal/t6-continue-as-new.shscripts/dex/d5-flood-fair.sh
External StorageEXTERNAL_STORAGE=1 scripts/temporal/t6-continue-as-new.sh
Context in each activity, transcript budgetscripts/temporal/t8-context-in-activity.shscripts/dex/d7-transcript-budget.sh
The Flow designscripts/dex/d-design.sh

Episode 1

Episode 1 is the full walkthrough of this page's support conversation: the idle timer, the code change, and the conversation that keeps going.

Watch on YouTube: Dex vs Temporal, Ep. 1: one idle timer, three production problems

Next