|
| 1 | +"""Carry an eval task's span attrs from its Temporal workflow memo into the worker's activities. |
| 2 | +
|
| 3 | +Spans are emitted by activities in the worker process, which never sees the ACP |
| 4 | +server's task. The ACP server puts the attrs in the workflow memo, the outbound |
| 5 | +interceptor copies them onto each activity's headers, and the activity |
| 6 | +interceptor registers them for the task so the SGP processor can stamp spans. |
| 7 | +Non-eval workflows have no memo entry and get no header. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import Any, override |
| 13 | + |
| 14 | +from temporalio import activity, workflow |
| 15 | +from temporalio.worker import ( |
| 16 | + Interceptor, |
| 17 | + StartActivityInput, |
| 18 | + ExecuteActivityInput, |
| 19 | + ActivityInboundInterceptor, |
| 20 | + WorkflowInboundInterceptor, |
| 21 | + WorkflowOutboundInterceptor, |
| 22 | +) |
| 23 | +from temporalio.converter import default |
| 24 | + |
| 25 | +from agentex.lib.utils.logging import make_logger |
| 26 | +from agentex.lib.core.tracing.sgp_evals import MEMO_KEY, register_task |
| 27 | + |
| 28 | +logger = make_logger(__name__) |
| 29 | + |
| 30 | +ATTRS_HEADER = "sgp-evals-span-attrs" |
| 31 | +_converter = default().payload_converter |
| 32 | + |
| 33 | + |
| 34 | +class SGPEvalsInterceptor(Interceptor): |
| 35 | + @override |
| 36 | + def intercept_activity(self, next: ActivityInboundInterceptor) -> ActivityInboundInterceptor: |
| 37 | + return _ActivityInbound(next) |
| 38 | + |
| 39 | + @override |
| 40 | + def workflow_interceptor_class(self, input: Any) -> type[WorkflowInboundInterceptor] | None: |
| 41 | + return _WorkflowInbound |
| 42 | + |
| 43 | + |
| 44 | +class _WorkflowInbound(WorkflowInboundInterceptor): |
| 45 | + @override |
| 46 | + def init(self, outbound: WorkflowOutboundInterceptor) -> None: |
| 47 | + super().init(_WorkflowOutbound(outbound)) |
| 48 | + |
| 49 | + |
| 50 | +class _WorkflowOutbound(WorkflowOutboundInterceptor): |
| 51 | + @override |
| 52 | + def start_activity(self, input: StartActivityInput) -> workflow.ActivityHandle[Any]: |
| 53 | + attrs = workflow.memo_value(MEMO_KEY, default=None) |
| 54 | + if isinstance(attrs, dict) and attrs: |
| 55 | + input.headers = {**input.headers, ATTRS_HEADER: _converter.to_payload(attrs)} |
| 56 | + return super().start_activity(input) |
| 57 | + |
| 58 | + |
| 59 | +class _ActivityInbound(ActivityInboundInterceptor): |
| 60 | + @override |
| 61 | + async def execute_activity(self, input: ExecuteActivityInput) -> Any: |
| 62 | + payload = input.headers.get(ATTRS_HEADER) |
| 63 | + # The workflow id is the task id (see TemporalTaskService.submit_task). |
| 64 | + task_id = activity.info().workflow_id |
| 65 | + if payload is not None and task_id: |
| 66 | + try: |
| 67 | + register_task(task_id, _converter.from_payload(payload, dict)) |
| 68 | + except Exception: |
| 69 | + logger.warning("failed to read sgp evals span attrs from activity headers", exc_info=True) |
| 70 | + return await super().execute_activity(input) |
0 commit comments