@@ -435,11 +435,20 @@ def _establish_hooks_and_provider(
435435 evaluation_hooks = flag_evaluation_options .hooks
436436 hook_hints = flag_evaluation_options .hook_hints
437437
438+ # Merge transaction context into evaluation context before creating hook_context
439+ # This ensures hooks have access to the complete context including transaction context
440+ merged_eval_context = (
441+ get_evaluation_context ()
442+ .merge (get_transaction_context ())
443+ .merge (self .context )
444+ .merge (evaluation_context )
445+ )
446+
438447 hook_context = HookContext (
439448 flag_key = flag_key ,
440449 flag_type = flag_type ,
441450 default_value = default_value ,
442- evaluation_context = evaluation_context ,
451+ evaluation_context = merged_eval_context ,
443452 client_metadata = self .get_metadata (),
444453 provider_metadata = provider .get_metadata (),
445454 )
@@ -465,7 +474,7 @@ def _assert_provider_status(
465474 return ProviderFatalError ()
466475 return None
467476
468- def _before_hooks_and_merge_context (
477+ def _run_before_hooks_and_update_context (
469478 self ,
470479 flag_type : FlagType ,
471480 hook_context : HookContext ,
@@ -480,16 +489,11 @@ def _before_hooks_and_merge_context(
480489 invocation_context = before_hooks (
481490 flag_type , hook_context , merged_hooks , hook_hints
482491 )
483- if evaluation_context :
484- invocation_context = invocation_context .merge (ctx2 = evaluation_context )
485492
486- # Requirement 3.2.2 merge: API.context->transaction.context->client.context->invocation.context
487- merged_context = (
488- get_evaluation_context ()
489- .merge (get_transaction_context ())
490- .merge (self .context )
491- .merge (invocation_context )
492- )
493+ # The hook_context.evaluation_context already contains the merged context from
494+ # _establish_hooks_and_provider, so we just need to merge with the before hooks result
495+ merged_context = hook_context .evaluation_context .merge (invocation_context )
496+
493497 return merged_context
494498
495499 @typing .overload
@@ -599,7 +603,7 @@ async def evaluate_flag_details_async(
599603 )
600604 return flag_evaluation
601605
602- merged_context = self ._before_hooks_and_merge_context (
606+ merged_context = self ._run_before_hooks_and_update_context (
603607 flag_type ,
604608 hook_context ,
605609 merged_hooks ,
@@ -775,7 +779,7 @@ def evaluate_flag_details(
775779 )
776780 return flag_evaluation
777781
778- merged_context = self ._before_hooks_and_merge_context (
782+ merged_context = self ._run_before_hooks_and_update_context (
779783 flag_type ,
780784 hook_context ,
781785 merged_hooks ,
0 commit comments