Skip to content

fix(flags): respect feature enrollment in local evaluation - #1176

Merged
puemos merged 3 commits into
mainfrom
posthog/local-eval-feature-enrollment
Nov 4, 2026
Merged

puemos merged 3 commits into
mainfrom
posthog/local-eval-feature-enrollment

Conversation

@puemos

@puemos puemos commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Local evaluation ignores early access enrollment (filters.feature_enrollment). A person who turned on a feature in Feature previews, but matches no release condition, gets False on the server and True in posthog-js.

Example: the browser shows a beta page, and its API (gated with feature_enabled()) refuses every call.

This PR applies the flags service rule: the enrollment property decides first.

flowchart LR
  A{"feature_enrollment"} -->|"off"| C["release conditions"]
  A -->|"on"| B{"property passed?"}
  B -->|"yes"| D["true or 'true' = enrolled"]
  B -->|"no"| E["/flags"]
  classDef blue fill:#1d4aff,stroke:#1d4aff,color:#fff;
  classDef red fill:#f54e00,stroke:#f54e00,color:#fff;
  classDef gray fill:#e5e7eb,stroke:#c7ccd1,color:#000;
  class A,B blue;
  class E red;
  class C,D gray;
Loading
$feature_enrollment/<key> Release conditions Before After
Not passed No match False ❌ /flags
"true" No match False ❌ True, local
False Match True ❌ False, local
1 Match True ❌ False, local
Group flag, any value Any Reads the group ❌ /flags

Why this is correct

  • It matches the enrollment design: checked before holdouts and conditions, true or "true" means enrolled, and any other value means opted out.
  • A property that was not passed is unknown, not "not enrolled". The server reads the stored person property, and the SDK cannot see it, so local evaluation cannot decide.
  • missing_context is the existing reason for "pass this property to evaluate locally".
  • Flag dependencies use the same function, so a flag that depends on an enrollment flag is also correct.

Trade-off: an enrollment flag now calls /flags unless the property is passed, like experience continuity. With only_evaluate_locally=True, it is unresolved instead of a wrong False.

No PostHog SDK handles feature_enrollment today. posthog-node 5.61.2 has the same gap.

💚 How did you test it?

test_feature_enrollment_flag covers the four table rows, and each row failed before the fix. Not tested against a live flags service.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code, Claude Opus 5.5 (claude-opus-5-5). Found while debugging PostHog's Today page, where key numbers never loaded for Feature previews users. The app-side workaround is PostHog/posthog#128585.


Created with PostHog Desktop from a PostHog Desktop task

🤖 Generated with Claude Code

A flag with early access feature enrollment now uses the $feature_enrollment/<flag key> person property when it is passed, like the flags service. Without it, local evaluation is inconclusive (missing_context) and falls back to /flags, instead of returning a certain False for enrolled people.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Generated-By: PostHog Desktop
Task-Id: fecca688-37ac-44b6-a1b5-27e11cb0749b
@puemos puemos self-assigned this Nov 4, 2026

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed dbfb25f: 1 Should fix.

This is the only automatic review while this pull request is a draft. Comment @posthog review to review new changes sooner. Otherwise, a review of the new changes runs when you mark it ready for review.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

Comment thread posthog/feature_flags.py Outdated
Comment on lines +502 to +504
return (
properties[enrollment_key] is True or properties[enrollment_key] == "true"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Read enrollment from person properties for group-scoped flags

For group-scoped enrollment flags, _compute_flag_locally passes group properties as properties. This return therefore treats a group's enrollment value as the person's, producing True even when the person explicitly opted out. The flags service always reads person properties for enrollment; preserve person context separately for this check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fffef4d. A group flag with feature enrollment is now unresolved locally and falls back to /flags, because the flags service reads enrollment from the person. The enrollment test covers a group that says enrolled and a person who opted out.

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-11-04T13:37:41.923636+00:00
Duration: 260233ms

✅ All Tests Passed!

121/121 tests passed


Capture_V1 Tests

✅ 95/95 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 517ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 513ms
Required Headers.Has Authorization Bearer Header ✅ 513ms
Required Headers.Has Content Type Json ✅ 512ms
Required Headers.Has Posthog Sdk Info Format ✅ 513ms
Required Headers.Has Posthog Attempt Header ✅ 512ms
Required Headers.Has Posthog Request Id ✅ 512ms
Required Headers.Has Posthog Request Timestamp ✅ 513ms
Required Headers.Has User Agent ✅ 512ms
Body Format.Body Has Created At And Batch ✅ 511ms
Body Format.No Api Key In Body ✅ 511ms
Body Format.No Sent At In Body ✅ 512ms
Event Format.Event Has Required Root Fields ✅ 511ms
Event Format.Event Uuid Is Valid ✅ 512ms
Event Format.Event Timestamp Is Rfc3339 ✅ 511ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 527ms
Event Format.Distinct Id Is String ✅ 512ms
Event Format.Distinct Id At Root Not Properties ✅ 513ms
Event Format.Custom Properties Preserved ✅ 518ms
Event Format.Set Properties Preserved ✅ 511ms
Event Format.Set Once Properties Preserved ✅ 512ms
Event Format.Groups Properties Preserved ✅ 511ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 512ms
Event Format.Event Has Required Root Fields Batch ✅ 515ms
Event Format.Event Uuid Is Valid Batch ✅ 515ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 516ms
Event Format.Distinct Id Is String Batch ✅ 514ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 516ms
Event Format.Custom Properties Preserved Batch ✅ 515ms
Event Format.Set Properties Preserved Batch ✅ 515ms
Event Format.Set Once Properties Preserved Batch ✅ 516ms
Event Format.Groups Properties Preserved Batch ✅ 515ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 515ms
Batch Behavior.Multiple Events In Single Batch ✅ 519ms
Batch Behavior.Batch Envelope Smoke ✅ 517ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 507ms
Batch Behavior.Flush At Triggers Batch ✅ 1014ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 512ms
Deduplication.Generates Unique Uuids ✅ 521ms
Deduplication.Different Events Same Content Different Uuids ✅ 515ms
Deduplication.Preserves Uuid On Retry ✅ 6523ms
Deduplication.Preserves Timestamp On Retry ✅ 6521ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 6525ms
Deduplication.No Duplicate Events In Batch ✅ 520ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 512ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 13529ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 6521ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 3022ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 6518ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 512ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 516ms
Response Format Validation.Success No Retry After When All Ok ✅ 514ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 1517ms
Response Format Validation.Success No Retry After When Drop Only ✅ 515ms
Response Format Validation.Response Echoes Request Id ✅ 511ms
Retry Behavior.Retries On 408 ✅ 6522ms
Retry Behavior.Retries On 500 ✅ 6521ms
Retry Behavior.Retries On 503 ✅ 8524ms
Retry Behavior.Retries On 504 ✅ 6520ms
Retry Behavior.Retryable Errors Have Retry After ✅ 3519ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 11518ms
Retry Behavior.Does Not Retry On 400 ✅ 2515ms
Retry Behavior.Does Not Retry On 401 ✅ 2524ms
Retry Behavior.Does Not Retry On 402 ✅ 2515ms
Retry Behavior.Does Not Retry On 413 ✅ 2514ms
Retry Behavior.Does Not Retry On 415 ✅ 2514ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2514ms
Retry Behavior.Implements Backoff ✅ 22538ms
Retry Behavior.Max Retries Respected ✅ 22541ms
Partial Batch Handling.Handles 200 Full Success ✅ 2515ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3519ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3517ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3515ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 6525ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 6525ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 6525ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 6522ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 6522ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 6524ms
Partial Batch Handling.Respects Retry After On Partial ✅ 8522ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3514ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3518ms
Compression.Sends Gzip Content Encoding ✅ 512ms
Compression.No Content Encoding When Disabled ✅ 512ms
Compression.Compressed Body Is Decompressible ✅ 512ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2514ms
Event Options.Cookieless Mode Override ✅ 512ms
Event Options.Disable Skew Correction Override ✅ 512ms
Event Options.Process Person Profile Override ✅ 512ms
Event Options.Product Tour Id Override ✅ 512ms
Event Options.Unset Options Omitted ✅ 511ms
Event Options.Options Override In Batch ✅ 516ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 512ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 512ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 512ms

Capture_Ai Tests

✅ 5/5 tests passed

View Details
Test Status Duration
Routing.Capture Ai Posts To Ai Endpoint ✅ 512ms
Routing.Capture Does Not Reroute Ai Named Events ✅ 511ms
Identity.Capture Ai Event Has Uuid ✅ 511ms
Identity.Capture Ai Keeps Supplied Uuid ✅ 512ms
Timestamp Format.Non Utc Event Timestamp Is Converted To Utc ✅ 516ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 15ms
Request Payload.Flags Request Uses V4 Query Param ✅ 16ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 13ms
Request Payload.Flags Request Omits Authorization Header ✅ 10ms
Request Payload.Token In Flags Body Matches Init ✅ 10ms
Request Payload.Groups Round Trip ✅ 10ms
Request Payload.Groups Default To Empty Object ✅ 11ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 10ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 11ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 11ms
Request Lifecycle.No Flags Request On Init Alone ✅ 7ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 509ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 16ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 14ms
Retry Behavior.Retries Flags On 502 ✅ 316ms
Retry Behavior.Retries Flags On 504 ✅ 317ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 514ms

Feature_Flags_Local_Evaluation Tests

✅ 4/4 tests passed

View Details
Test Status Duration
Versioned Boolean Matching.Matching Version Missing ✅ 68ms
Versioned Boolean Matching.Matching Version 1 ✅ 65ms
Versioned Boolean Matching.Matching Version 2 ✅ 65ms
Versioned Boolean Matching.Version Only Reload 1 2 1 2 Missing ✅ 37ms

Check enrollment only when feature_enrollment is boolean true, and treat only true or "true" as enrolled, like the flags service. A value such as 1 now reads as opted out instead of enrolled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Generated-By: PostHog Desktop
Task-Id: fecca688-37ac-44b6-a1b5-27e11cb0749b
…vice

For a group flag, local evaluation passes group properties, but the flags service reads enrollment from the person. Local evaluation now treats a group flag with feature enrollment as unresolved, so it falls back to /flags.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Generated-By: PostHog Desktop
Task-Id: fecca688-37ac-44b6-a1b5-27e11cb0749b
@puemos
puemos marked this pull request as ready for review November 4, 2026 13:51
@puemos
puemos requested a review from a team as a code owner November 4, 2026 13:51
@puemos
puemos merged commit ae2dcf2 into main Nov 4, 2026
47 of 48 checks passed
@puemos
puemos deleted the posthog/local-eval-feature-enrollment branch November 4, 2026 19:14
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.

2 participants