Repository navigation
feat(flags): evaluate feature enrollment locally - #411
Conversation
Early access feature flags (filters.feature_enrollment: true) now resolve from the person property $feature_enrollment/<flag key> before the holdout and release conditions, matching the flags service and the local-feature-flag-evaluator spec. true or "true" means enrolled, and any other passed value means opted out. Group flags read the person property too. A missing property is missing_context and falls back to /flags. A key listed as known absent continues through the holdout and conditions.
posthog-dotnet Compliance ReportDate: 2026-12-01T13:06:27.997887+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
… expose Add a PostHog.AspNetCore changeset for the feature enrollment change, which the feature manager picks up through local evaluation.
There was a problem hiding this comment.
PostHog Review · Standard · reviewed d08dabc: 1 Should fix.
New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.
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.
SDK performance: posthog-dotnet30 changes worth reviewing: 3 observed improvements, 27 observed regressions. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.
Showing the 12 largest flagged changes; 18 more are in all results. View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Compared 108 metric rows; 108 rows shown in all results. Omitted or shortened data remain in |
dustinbyrne
left a comment
There was a problem hiding this comment.
Core feature_enrollment evaluation, precedence over holdouts/conditions, group-flag person lookup, known-absent handling, JsonDocument/JsonElement unwrapping, and test coverage match PostHog/posthog, PostHog/sdk-specs, and peer server SDKs.
Left one inline Concern on TryGetDefinitionUnresolvedReason when $feature_enrollment/<flag key> is known absent and a preceding condition hits RequiresServerEvaluationException, plus a non-blocking Question on patch vs minor versioning and a minor Suggestion on FeatureFlagFilters.Equals.
Follow-up (external repo): Once this ships, PostHog/posthog.com (contents/docs/feature-flags/local-evaluation/index.mdx, line 365) still lists flags linked to an early access feature as unsupported for local evaluation and can be updated in a separate docs PR.
AI-assisted review.
A flag dependency no longer evaluates every flag in its dependency_chain first. Only the referenced flag is evaluated, with full flag evaluation, so a flag it skips, such as the release conditions of an enrollment flag, can't leave its dependents unresolved or abort a later fallback condition. A missing definition anywhere in the chain is still unsupported_definition, and a chain that loops back is caught. During evaluation, the definition reason of an enrollment flag now counts its conditions, which are only reached when its enrollment key is known absent, so unsupported_definition still outranks unresolved_dependency. FeatureFlagFilters equality includes feature_enrollment. The changesets are minor.
A very deep acyclic dependency chain recursed until the stack overflowed, which ends the .NET process. Dependency evaluation deeper than 100 levels is now inconclusive with UnresolvedDependency, as in the other server SDKs.
Only successful dependency results were cached, so a graph of flags sharing an unresolved dependency evaluated it again on every path, which took exponential time: 33 flags each depending on the previous two took 92 seconds. A dependency that can't be evaluated is now remembered for the evaluation and fails the same way without being evaluated again.
dustinbyrne
left a comment
There was a problem hiding this comment.
All earlier review items (TryGetDefinitionUnresolvedReason with ignoreFeatureEnrollment, minor changesets for PostHog and PostHog.AspNetCore, FeatureFlagFilters.Equals/GetHashCode, and evaluating only the referenced flagKey in EvaluateDependencyChain) look solid, and core feature_enrollment evaluation aligns with PostHog/sdk-specs, the Rust flags service, and server SDK peers.
Left one inline Concern on state.Failures in ade960b caching path-dependent depth-limit (and cycle) failures and poisoning shallower valid fallback conditions within the same evaluation, plus three optional inline Suggestions on the new dependency cycle guard, JsonValue/JsonNode unwrapping, and stale comments.
- Suggestion (outside diff —
src/PostHog/Features/FeatureFlagOptions.cs:53-60): Consider adding one sentence to the XML<remarks>onAllFeatureFlagsOptions.PersonPropertyKeysKnownAbsentnoting that listing$feature_enrollment/<flag key>treats the person as not enrolled so evaluation continues through holdouts and release conditions. Existing remarks cover property operators and__posthog_person_metadata__, but omit the new early-access enrollment bypass advertised in the changeset, so IDE tooltips and generated API docs won't surface it. - Follow-up (already noted): Skipping duplicate comment for the external
PostHog/posthog.comlocal-evaluation restrictions update (contents/docs/feature-flags/local-evaluation/index.mdx:365), previously noted in review #6116121755.
AI-assisted review.
A dependency that failed because of the depth limit or a cycle through a flag in progress was cached as its result, so a shallower reference later in the same evaluation failed too. Only failures that don't depend on the path are cached now, and a budget of 10,000 dependency evaluations per evaluation, as in posthog-rs, keeps the work bounded; dependencies past it are inconclusive with UnresolvedDependency. The flag being evaluated is now tracked as in progress, so a malformed self-cycle no longer evaluates itself twice, and every flag on a malformed cycle reports UnsupportedDefinition, as evaluate-flags defines a circular dependency. A flag that only depends on a cycle reports UnresolvedDependency.
A $feature_enrollment value passed as a System.Text.Json.Nodes.JsonValue, such as JsonNode.Parse("true") or JsonValue.Create(true), was read as not enrolled, while /flags reads it as enrolled. It is now unwrapped like a JsonDocument or JsonElement.
…chain evaluation PersonPropertyKeysKnownAbsent now explains that listing $feature_enrollment/<flag key> treats the person as not enrolled, and the dependency chain comments describe that only the referenced flag is evaluated.
|
Done in 323f691. The |
…rollment # Conflicts: # src/PostHog/Api/LocalEvaluationApiResult.cs # src/PostHog/Features/LocalEvaluator.cs
A flag that ran out of depth below it was not cached, so every path to it evaluated it again until the 10,000 budget ran out, which took seconds for a chain deeper than 100. Such a flag is now remembered with the depth it failed at and evaluated again only from a shallower path. A bulk evaluation shares one evaluation cache, so a dependency resolved for one flag is not evaluated again for the next, and the budget bounds the whole pass. The tests count dependency evaluations instead of timing them.
💡 Motivation and Context
Local evaluation ignored
filters.feature_enrollment, which marks an early access feature flag. Such a flag resolved from its release conditions, while/flagsreturns the person's enrollment. Since #397 evaluates holdouts locally, a flag with both could also returnholdout-<id>where the flags service returns the enrollment.This adds local enrollment evaluation per the
local-feature-flag-evaluatorspec in PostHog/sdk-specs: "Feature enrollment precedes holdouts and release conditions" and "Unavailable enrollment context remains inconclusive". It also matches the Rust flags service (FeatureFlagMatcher::get_match,FlagFilters::is_enrolled) and posthog-node.Behavior
For an active flag with
feature_enrollment: trueand no experience continuity:$feature_enrollment/<flag key>. The JSON booleantrueor the string"true"resolves the flag totrue. Any other passed value,nullincluded, resolves it tofalse.missing_contextand falls back to/flags, or stays unresolved whenOnlyEvaluateLocallyis true. Flags that depend on it reportunresolved_dependency.$feature_enrollment/<flag key>is listed inPersonPropertyKeysKnownAbsent, the person counts as not enrolled. Evaluation continues through the holdout and the release conditions, and the listing wins over a passed value.unsupported_definition.feature_enrollmentthat is absent,null,falseor any other non-boolean value keeps ordinary evaluation.Device-id bucketing, which the spec also covers, is not part of this PR.
Flag dependencies
A flag dependency now evaluates only the flag it references, as the flags service does, instead of every flag in its
dependency_chain. Without this, a flag that an enrollment flag's skipped release conditions depend on could leave its dependents unresolved or abort a fallback condition. A missing definition anywhere in the chain is stillUnsupportedDefinition, and a chain that loops back is caught. During evaluation, the definition reason of an enrollment flag whose key is known absent counts its conditions, soUnsupportedDefinitionstill outranksUnresolvedDependency.FeatureFlagFiltersequality includesfeature_enrollment. The changesets areminor.Dependencies deeper than 100 levels, or beyond 10,000 dependency evaluations in one flag evaluation, are
UnresolvedDependency. Failures caused by the depth limit or a cycle aren't cached, so a shallower reference can still resolve. Every flag on a malformed dependency cycle reportsUnsupportedDefinition; a flag that only depends on a cycle reportsUnresolvedDependency. Enrollment passed as aSystem.Text.Json.Nodes.JsonValueis read like aJsonElement.💚 How did you test it?
New
tests/UnitTests/Features/FeatureEnrollmentTests.cs(37 cases) covers the spec scenarios:missing_context/unresolved_dependency,/flagsfallback, local-only)25 of the 37 cases failed on
mainand pass with the change.dotnet test(UnitTests2232 on net8.0 and net10.0) anddotnet format --verify-no-changespassed.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
local-feature-flag-evaluatorspec, the Rust flags service onPostHog/posthogmaster, and posthog-node'smatchFeatureEnrollment.is_enrolled: only booleantrueand the exact string"true"count. The model keepsfeature_enrollmentas raw JSON, so an unexpected value doesn't fail loading the definitions.