open-feature / open-feature/python-sdk

[BUG] before hooks do not receive the evaluation context returned by earlier before hooks

未关闭
#628 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug
主要语言
Python
星标
111
派生
44
平均合并
2 小时 37 分钟
30 天内合并 PR
14

描述

Observed behaviour

Requirement 4.3.4 says:

Any evaluation context returned from a before hook MUST be passed to subsequent before
hooks (via HookContext).

Each hook receives a HookContext built before any hook ran, so a hook never sees what an earlier
hook contributed.

The final merge into the evaluation context is correct — the provider does receive every hook's
contribution, so 3.2.2
and 4.3.5 are satisfied.
Only the propagation half is missing.

Reproducer

Against main (bd3b1e6):

from openfeature import api
from openfeature.client import OpenFeatureClient
from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagEvaluationOptions
from openfeature.hook import Hook, HookContext
from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider

observed = {}


class ContributingHook(Hook):
    """Adds one attribute, and records what it could see when it ran."""

    def __init__(self, name, adds):
        self.name = name
        self.adds = adds

    def before(self, hook_context: HookContext, hints):
        observed[self.name] = dict(hook_context.evaluation_context.attributes)
        return EvaluationContext(attributes=self.adds)


class RecordingProvider(InMemoryProvider):
    def resolve_boolean_details(self, flag_key, default_value, evaluation_context=None):
        observed["provider"] = dict(evaluation_context.attributes) if evaluation_context else {}
        return super().resolve_boolean_details(flag_key, default_value, evaluation_context)


api.set_provider(RecordingProvider({"flag": InMemoryFlag("on", {"on": True, "off": False})}))

client: OpenFeatureClient = api.get_client()
client.add_hooks([ContributingHook("hook_a", {"from_hook_a": "a"})])

# hook_b is an invocation hook, so it runs after the client hook (API -> client -> invocation).
client.get_boolean_details(
    "flag",
    False,
    evaluation_context=EvaluationContext(attributes={"from_invocation": "i"}),
    flag_evaluation_options=FlagEvaluationOptions(
        hooks=[ContributingHook("hook_b", {"from_hook_b": "b"})]
    ),
)

print("hook_a saw :", observed["hook_a"])
print("hook_b saw :", observed["hook_b"])
print("provider   :", observed["provider"])

Output:

hook_a saw : {'from_invocation': 'i'}
hook_b saw : {'from_invocation': 'i'}
provider   : {'from_invocation': 'i', 'from_hook_a': 'a', 'from_hook_b': 'b'}

hook_b should have seen from_hook_a: a. The provider line shows the final merge is fine.

Cause

OpenFeatureClient._establish_hooks_and_provider constructs one HookContext per hook up front,
all from the same merged_eval_context:

merged_hooks_and_context = [
    (
        hook,
        HookContext(
            flag_key=flag_key,
            flag_type=flag_type,
            default_value=default_value,
            evaluation_context=merged_eval_context,
            client_metadata=client_metadata,
            provider_metadata=provider_metadata,
            hook_data={},
        ),
    )
    for hook in chain(get_hooks(), self.hooks, evaluation_hooks, provider.get_provider_hooks())
]

_hook_support._execute_hooks_unchecked then iterates those pairs without ever updating one:

return [
    getattr(hook, hook_method.value)(hook_context=hook_context, **kwargs)
    for (hook, hook_context) in hooks_and_context
    if hook.supports_flag_value_type(flag_type)
]

before_hooks reduces the results correctly (reduce(lambda a, b: a.merge(b), filtered_hooks)),
which is why the provider still gets everything — but that accumulation is never fed back into the
hook contexts.

The docstring on _run_before_hooks_and_update_context cites 4.3.4 while implementing only the
merge half.

Suggested fix

Accumulate as the hooks run and refresh the context handed to each subsequent hook, rather than
collecting results and merging once at the end. Roughly:

def before_hooks(flag_type, hooks_and_context, hints=None):
    accumulated = EvaluationContext()
    for hook, hook_context in hooks_and_context:
        if not hook.supports_flag_value_type(flag_type):
            continue
        hook_context.evaluation_context = hook_context.evaluation_context.merge(accumulated)
        result = hook.before(hook_context=hook_context, hints=hints)
        if result is not None:
            accumulated = accumulated.merge(result)
    return accumulated

HookContext is a plain mutable class (__init__-assigned attributes, not frozen), so assigning
evaluation_context in place works; rebuilding the context per hook is equally fine. dotnet-sdk does
the latter (WithNewEvaluationContext applied to every pending hook context); ruby-sdk does the
former.

Cross-language note

Surveyed 2026-09-14 — js-sdk, java-sdk, dotnet-sdk and ruby-sdk all propagate correctly.
go-sdk had a related but distinct bug (results replaced rather than merged), reported as
go-sdk#549 and fixed by
go-sdk#569. php-sdk has the same propagation gap
as this one.

Given four SDKs have now been found wanting on one half or the other, this seems like a good
candidate for a shared e2e/TCK case: two before hooks at different levels, each returning a distinct
key; assert both reach the provider and that the second hook observed the first's key. Asserting
only the merged result would miss this bug entirely.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 OpenFeatureClient._establish_hooks_and_provider 和 _hook_support._execute_hooks_unchecked 开始,然后跟踪 _run_before_hooks_and_update_context 以及 before_hooks 的归约过程。确保每个后续的 before hook 都能接收到之前的贡献,同时保留 provider 最终合并后的上下文。添加一个使用不同层级的两个 hook 的回归用例,检查第二个 hook 观察到的内容以及 provider 接收到的内容。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend-api-design
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
活跃
描述清晰度
描述清楚
新手友好度
78/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。