Skip to content

Commit d9717ef

Browse files
NiteshDhanpalclaude
andcommitted
refactor(tracing): key the dispatch discriminator on TracingActivityName
Compare activity.info().activity_type against TracingActivityName.START_SPAN / END_SPAN instead of the string literals "start-span" / "end-span", so the check can't silently drift from the enum that actually names the activities (@activity.defn(name=TracingActivityName.START_SPAN)). Uses a lazy import inside the function to avoid the activities -> TracingService -> AsyncTracer -> trace import cycle (the reason literals were used originally); at call time, inside an activity, the module graph is fully loaded so the import is safe. activity_type round-trips as the enum's str value, which a str-Enum member compares equal to. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2bc7a74 commit d9717ef

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/agentex/lib/core/tracing/trace.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,29 @@ def _in_tracing_dispatch_activity() -> bool:
116116
created directly inside a *business* activity (an agent turn's own
117117
``adk.tracing.span``) runs start AND end in the same activity process, so a
118118
wrapper there is safe -- it nests under the interceptor's ambient RunActivity
119-
span and closes in-process. The tracing dispatch activities are named
120-
``start-span`` / ``end-span`` (``TracingActivityName``). Never raises; False
119+
span and closes in-process. The tracing dispatch activities are named by
120+
``TracingActivityName`` (``start-span`` / ``end-span``). Never raises; False
121121
when temporalio isn't importable or we're not in an activity."""
122122
try:
123123
from temporalio import activity
124124

125+
# Lazy import: TracingActivityName lives in the activities package whose
126+
# module graph imports back into this one (activities -> TracingService ->
127+
# AsyncTracer -> trace), so a top-level import would be circular. At call
128+
# time (inside an activity) that graph is fully loaded, so this is safe --
129+
# and it keeps the discriminator keyed on the enum, not on drifting string
130+
# literals. ``activity_type`` round-trips as the enum's str value, which a
131+
# str-Enum member compares equal to.
132+
from agentex.lib.core.temporal.activities.adk.tracing_activities import (
133+
TracingActivityName,
134+
)
135+
125136
if not activity.in_activity():
126137
return False
127-
return activity.info().activity_type in ("start-span", "end-span")
138+
return activity.info().activity_type in (
139+
TracingActivityName.START_SPAN,
140+
TracingActivityName.END_SPAN,
141+
)
128142
except Exception:
129143
return False
130144

0 commit comments

Comments
 (0)