Skip to content

fix: propagate evaluation context across sequential before hooks - #687

Closed
divyansh-1615 wants to merge 1 commit into
open-feature:mainfrom
divyansh-1615:fix/628-before-hooks-context-propagation
Closed

divyansh-1615 wants to merge 1 commit into
open-feature:mainfrom
divyansh-1615:fix/628-before-hooks-context-propagation

Conversation

@divyansh-1615

Copy link
Copy Markdown

Closes #628

Summary

  • Updates before_hooks in openfeature/hook/_hook_support.py to iterate over hooks sequentially, merging any accumulated EvaluationContext from earlier before hooks into each subsequent HookContext.evaluation_context prior to execution (per OpenFeature Spec Requirement 4.3.4).
  • Preserves existing behavior when a hook returns None or when HookContext.evaluation_context is mocked in unit tests.
  • Adds test_before_hooks_propagates_evaluation_context_to_subsequent_hooks in tests/hook/test_hook_support.py verifying intermediate hook visibility, targeting key updates, and attribute precedence.

Testing

  • Ran uv run --frozen pytest (216/216 passed)
  • Ran uv run --frozen poe cov (216/216 passed)

Signed-off-by: divyansh-1615 <dk5551419@gmail.com>
@divyansh-1615
divyansh-1615 requested review from a team as code owners November 24, 2026 21:23
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Before hooks now run sequentially and pass accumulated evaluation context to later supported hooks. Tests check context propagation, attribute and targeting-key merging, and the effect of a hook returning None.

Changes: Hook context propagation

Layer / File(s) Summary
Accumulate and propagate hook context
openfeature/hook/_hook_support.py, tests/hook/test_hook_support.py
before_hooks invokes supported hooks sequentially and merges each non-None result into the accumulated context. Later hooks receive that context. The function returns an empty EvaluationContext if no hook returns one. The test checks context propagation, merging and replacement, and None results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: gruebel, ajhelsby

Merge Risk: Minimal · up to 5cf67

Later before hooks now receive the evaluation context contributed by earlier hooks, as the OpenFeature specification requires. Returned contributions still reach the provider. No merge-blocking risk was identified; the main edge case is hooks that edit the context in place and return None, which the specification does not require to propagate.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: sequential before hooks now propagate accumulated evaluation context to later hooks. It matches the raw summary, the description, and the linked issue #628. It uses th…
Description check Passed The description states the change to before_hooks in openfeature/hook/_hook_support.py, the new test in tests/hook/test_hook_support.py, the spec reference, and the test results. It matches the raw su…
Linked Issues check Passed Directly linked issue #628 requires that an evaluation context returned by a before hook reaches subsequent before hooks through HookContext (Spec 4.3.4). The reviewed head changes `before_hooks…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes openfeature/hook/_hook_support.py only in before_hooks. The change removes the now-unused reduce import and the call to `_execute_hooks_unchecked…
Full details: Title check

Explanation

The title names the main change: sequential before hooks now propagate accumulated evaluation context to later hooks. It matches the raw summary, the description, and the linked issue #628. It uses the conventional fix: prefix and is specific.

Full details: Description check

Explanation

The description states the change to before_hooks in openfeature/hook/_hook_support.py, the new test in tests/hook/test_hook_support.py, the spec reference, and the test results. It matches the raw summary and the PR objectives.

Full details: Linked Issues check

Explanation

Directly linked issue #628 requires that an evaluation context returned by a before hook reaches subsequent before hooks through HookContext (Spec 4.3.4). The reviewed head changes before_hooks in openfeature/hook/_hook_support.py to run supported hooks sequentially. For each hook after the first, the code merges the accumulated context into hook_context.evaluation_context before calling hook.before. Non-None results are merged in order, and the accumulated context is returned, so the final merge behavior that the issue describes as already correct is kept. The PR adds test_before_hooks_propagates_evaluation_context_to_subsequent_hooks in tests/hook/test_hook_support.py, which checks intermediate visibility, targeting-key updates, and attribute precedence. The PR objectives mention a possible shared cross-SDK e2e/TCK case, but the issue presents it only as a suggested follow-up, so it is not a requirement here. The test was not inspected directly; this verdict relies on the summary for that file. The before_hooks diff was inspected directly.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes openfeature/hook/_hook_support.py only in before_hooks. The change removes the now-unused reduce import and the call to _execute_hooks_unchecked from that function. The other changes are a single test added to tests/hook/test_hook_support.py, as stated in the PR description and the change summary. Each change directly supports #628. No unrelated changes were found in the reviewed evidence. The test file was reviewed from its summary only, not from a direct diff.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 98296e9b-8dd8-4502-9e8a-38a1c739d03b
Commits

Reviewing files that changed from the base of the PR and between b8267bd and 5cf6708.

Files selected for processing: 2

  • openfeature/hook/_hook_support.py
  • tests/hook/test_hook_support.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@toddbaert

Copy link
Copy Markdown
Member

Thanks @divyansh-1615! I went with #649 for this one; it predates this PR and also covers the after/error/finally stages. Your targeting-key test coverage was a genuinely useful catch though; #649's suite didn't assert on it, and it does now. Hope to see you around the project!

@toddbaert toddbaert closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants