From bc2c7ee4cf2521ebfa74ada43646c3e4f0792b19 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 15:03:44 -0700 Subject: [PATCH 1/4] Gave a Local Activity the Workflow Task that dispatched it. A Local Activity runs again with the same Activity id and with attempt 1 when its Workflow Task is retried, and nothing in `activity.Info` tells the two executions apart or orders them. `Info.workflow_task` has the started event id, the attempt and the started time of that task, which Core sends for a Local Activity. --- temporalio/activity.py | 26 +++++++++++++++++++++++ temporalio/worker/_activity.py | 9 ++++++++ tests/worker/test_activity.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/temporalio/activity.py b/temporalio/activity.py index bf1b4cb731..5cc113b84b 100644 --- a/temporalio/activity.py +++ b/temporalio/activity.py @@ -91,6 +91,24 @@ def decorator(fn: CallableType) -> CallableType: return decorator +@dataclass(frozen=True) +class WorkflowTaskAttempt: + """One attempt of a Workflow Task. + + .. warning:: + This API is experimental and unstable. + """ + + started_event_id: int + """ID of the task's ``WorkflowTaskStarted`` event.""" + + attempt: int + """Attempt of the task.""" + + started_time: datetime | None + """When the server started the task. None if the server did not send it.""" + + @dataclass(frozen=True) class Info: """Information about the running activity. @@ -155,6 +173,14 @@ class Info: This API is experimental and unstable. """ + workflow_task: WorkflowTaskAttempt | None = None + """For a Local Activity, the Workflow Task in which the Worker dispatched this execution. + None for other Activities. + + .. warning:: + This API is experimental and unstable. + """ + @property def in_workflow(self) -> bool: """Was this activity started by a workflow?""" diff --git a/temporalio/worker/_activity.py b/temporalio/worker/_activity.py index 79af99070a..7c95449df8 100644 --- a/temporalio/worker/_activity.py +++ b/temporalio/worker/_activity.py @@ -700,6 +700,15 @@ async def _execute_activity( if not started_by_workflow else None, nexus_serialization_context=nexus_context, + workflow_task=temporalio.activity.WorkflowTaskAttempt( + started_event_id=start.workflow_task.started_event_id, + attempt=start.workflow_task.attempt, + started_time=_proto_to_datetime(start.workflow_task.started_time) + if start.workflow_task.HasField("started_time") + else None, + ) + if start.HasField("workflow_task") + else None, ) if self._encode_headers: diff --git a/tests/worker/test_activity.py b/tests/worker/test_activity.py index 5b21c3d0ad..8dee94ee3a 100644 --- a/tests/worker/test_activity.py +++ b/tests/worker/test_activity.py @@ -2208,3 +2208,42 @@ async def test_local_activities_with_no_remote_activities_option(client: Client) ) assert result == "Hello from local activity, Temporal!" + + +@activity.defn +async def capture_workflow_task() -> list[int] | None: + task = activity.info().workflow_task + return None if task is None else [task.started_event_id, task.attempt] + + +@workflow.defn(sandboxed=False) +class WorkflowTaskInfoWorkflow: + @workflow.run + async def run(self) -> list[list[int] | None]: + local = await workflow.execute_local_activity( + capture_workflow_task, start_to_close_timeout=timedelta(seconds=5) + ) + regular = await workflow.execute_activity( + capture_workflow_task, start_to_close_timeout=timedelta(seconds=5) + ) + return [local, regular] + + +async def test_activity_info_workflow_task(client: Client): + task_queue = f"tq-{uuid.uuid4()}" + async with Worker( + client, + task_queue=task_queue, + activities=[capture_workflow_task], + workflows=[WorkflowTaskInfoWorkflow], + ): + local, regular = await client.execute_workflow( + WorkflowTaskInfoWorkflow.run, + id=f"workflow-{uuid.uuid4()}", + task_queue=task_queue, + ) + assert local is not None + started_event_id, attempt = local + assert started_event_id > 0 + assert attempt == 1 + assert regular is None From d0e537595a144a0481e22c82e1bcc545088d133f Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 17:44:40 -0700 Subject: [PATCH 2/4] Added the changelog entry of the Workflow Task info. Each PR with a change that users see needs a fragment. --- changelog/added/punctual-ticket-puffin.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/added/punctual-ticket-puffin.md diff --git a/changelog/added/punctual-ticket-puffin.md b/changelog/added/punctual-ticket-puffin.md new file mode 100644 index 0000000000..820efd50d9 --- /dev/null +++ b/changelog/added/punctual-ticket-puffin.md @@ -0,0 +1 @@ +Add `activity.Info.workflow_task` for a Local Activity. It is the Workflow Task attempt that dispatched the execution, so a Local Activity can tell its executions apart, and order them, when its Workflow Task runs again. From 5b6383bbae75858207ff38a23d27147bba8af568 Mon Sep 17 00:00:00 2001 From: Mohammad Dashti Date: Fri, 27 Nov 2026 02:04:48 -0700 Subject: [PATCH 3/4] Clarify the Local Activity dispatch identity --- changelog/added/punctual-ticket-puffin.md | 2 +- temporalio/activity.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog/added/punctual-ticket-puffin.md b/changelog/added/punctual-ticket-puffin.md index 820efd50d9..cf1dc9299e 100644 --- a/changelog/added/punctual-ticket-puffin.md +++ b/changelog/added/punctual-ticket-puffin.md @@ -1 +1 @@ -Add `activity.Info.workflow_task` for a Local Activity. It is the Workflow Task attempt that dispatched the execution, so a Local Activity can tell its executions apart, and order them, when its Workflow Task runs again. +Add `activity.Info.workflow_task` so Local Activities can distinguish executions across Workflow Task attempts. diff --git a/temporalio/activity.py b/temporalio/activity.py index 5cc113b84b..b2afae0179 100644 --- a/temporalio/activity.py +++ b/temporalio/activity.py @@ -174,8 +174,8 @@ class Info: """ workflow_task: WorkflowTaskAttempt | None = None - """For a Local Activity, the Workflow Task in which the Worker dispatched this execution. - None for other Activities. + """Workflow Task attempt that dispatched this Local Activity execution. + ``None`` for other Activities. .. warning:: This API is experimental and unstable. From e0cd5c7a7db93c26ef6b17ae6f1d0a64dad1a961 Mon Sep 17 00:00:00 2001 From: Mohammad Dashti Date: Sat, 28 Nov 2026 03:22:49 -0700 Subject: [PATCH 4/4] Check Local Activity task identity against History --- tests/worker/test_activity.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/worker/test_activity.py b/tests/worker/test_activity.py index 8dee94ee3a..01e7e88c05 100644 --- a/tests/worker/test_activity.py +++ b/tests/worker/test_activity.py @@ -2211,15 +2211,14 @@ async def test_local_activities_with_no_remote_activities_option(client: Client) @activity.defn -async def capture_workflow_task() -> list[int] | None: - task = activity.info().workflow_task - return None if task is None else [task.started_event_id, task.attempt] +async def capture_workflow_task() -> activity.WorkflowTaskAttempt | None: + return activity.info().workflow_task @workflow.defn(sandboxed=False) class WorkflowTaskInfoWorkflow: @workflow.run - async def run(self) -> list[list[int] | None]: + async def run(self) -> list[activity.WorkflowTaskAttempt | None]: local = await workflow.execute_local_activity( capture_workflow_task, start_to_close_timeout=timedelta(seconds=5) ) @@ -2237,13 +2236,26 @@ async def test_activity_info_workflow_task(client: Client): activities=[capture_workflow_task], workflows=[WorkflowTaskInfoWorkflow], ): - local, regular = await client.execute_workflow( + handle = await client.start_workflow( WorkflowTaskInfoWorkflow.run, id=f"workflow-{uuid.uuid4()}", task_queue=task_queue, ) + local, regular = await handle.result() + history = await handle.fetch_history() + started = next( + event + for event in history.events + if event.HasField("workflow_task_started_event_attributes") + ) + scheduled = next( + event + for event in history.events + if event.event_id + == started.workflow_task_started_event_attributes.scheduled_event_id + ) assert local is not None - started_event_id, attempt = local - assert started_event_id > 0 - assert attempt == 1 + assert local.started_event_id == started.event_id + assert local.attempt == scheduled.workflow_task_scheduled_event_attributes.attempt + assert local.started_time == started.event_time.ToDatetime(tzinfo=timezone.utc) assert regular is None