From 2a0658e93a2157b9613fac176ba3e706b03283d4 Mon Sep 17 00:00:00 2001 From: Moe Dashti Date: Fri, 13 Nov 2026 15:03:44 -0700 Subject: [PATCH] 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 bf1b4cb73..5cc113b84 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 2af18dc5f..dbd967fed 100644 --- a/temporalio/worker/_activity.py +++ b/temporalio/worker/_activity.py @@ -691,6 +691,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 5b21c3d0a..8dee94ee3 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