2626
2727from .errors import NonDeterminismWarning , SandboxViolationError
2828
29+ # Capture environment variable at module load to avoid triggering non-determinism detection
30+ _DISABLE_DETECTION = os .getenv ("DAPR_WF_DISABLE_DETECTION" ) == "true"
31+
2932
3033class SandboxMode (str , Enum ):
3134 """Sandbox mode options.
@@ -295,8 +298,8 @@ def __enter__(self) -> "_Sandbox":
295298
296299 # Expose originals/mode to the async workflow context for controlled unsafe access
297300 try :
298- setattr ( self .async_ctx , " _sandbox_originals" , dict (self .originals ) )
299- setattr ( self .async_ctx , " _sandbox_mode" , self .mode )
301+ self .async_ctx . _sandbox_originals = dict (self .originals )
302+ self .async_ctx . _sandbox_mode = self .mode
300303 except Exception :
301304 # Context may not support attribute assignment; ignore
302305 pass
@@ -398,7 +401,7 @@ def __await__(self) -> Any:
398401 now_dt = None
399402 if now_dt is None :
400403 if hasattr (self .async_ctx , "current_utc_datetime" ):
401- now_dt = getattr ( self .async_ctx , " current_utc_datetime" )
404+ now_dt = self .async_ctx . current_utc_datetime
402405 else :
403406 base = getattr (self .async_ctx , "_base_ctx" , None )
404407 now_dt = getattr (base , "current_utc_datetime" , None ) if base is not None else None
@@ -407,7 +410,7 @@ def __await__(self) -> Any:
407410 rng = deterministic_random (iid or "" , now_dt )
408411 # Mark as deterministic so the detector can whitelist bound method calls
409412 try :
410- setattr ( rng , " _dt_deterministic" , True )
413+ rng . _dt_deterministic = True
411414 except Exception :
412415 pass
413416
@@ -581,10 +584,10 @@ async def _run_mixed() -> list[Any]:
581584 else :
582585 wf_group = wf_items
583586 wf_results : list [Any ] = await WhenAllAwaitable (wf_group ) # type: ignore[assignment]
584- for pos , val in zip (wf_indices , wf_results ):
587+ for pos , val in zip (wf_indices , wf_results , strict = False ):
585588 merged [pos ] = val
586589 # Then process native sequentially, honoring return_exceptions
587- for pos , it in zip (native_indices , native_items ):
590+ for pos , it in zip (native_indices , native_items , strict = False ):
588591 try :
589592 merged [pos ] = await it
590593 except Exception as e : # noqa: BLE001
@@ -737,8 +740,8 @@ def sandbox_scope(async_ctx: Any, mode: Union[str, SandboxMode]) -> Any:
737740 if mode_str not in valid_modes :
738741 raise ValueError (f"Invalid sandbox mode '{ mode_str } '. Must be one of { valid_modes } " )
739742
740- # Check for global disable
741- if mode_str != "off" and os . getenv ( "DAPR_WF_DISABLE_DETECTION" ) == "true" :
743+ # Check for global disable (captured at module load to avoid non-determinism detection)
744+ if mode_str != "off" and _DISABLE_DETECTION :
742745 mode_str = "off"
743746
744747 with _Sandbox (async_ctx , mode_str ):
0 commit comments