Repository navigation
fix(flags): don't fall back to the server for flag leaves in cohorts - #396
marandaneto wants to merge 2 commits into
Conversation
The flags service doesn't resolve a flag dependency inside a cohort. Its property matcher rejects flag_evaluates_to, so the leaf is a definitive non-match and a negated leaf matches. Local evaluation treated the leaf as an unknown operator, so the flag fell back to /flags, and the load-time check reported it as an unsupported definition.
posthog-ruby-sync Compliance ReportDate: 2026-11-30T09:28:37.381934+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
|
posthog-ruby-async Compliance ReportDate: 2026-11-30T09:28:46.917146+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
|
SDK performance: posthog-rubyBenchmarks completed. No changes flagged for review. 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. 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. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
There was a problem hiding this comment.
PostHog Review · Standard · reviewed ee0240c: 2 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.
| matches = if cohort_flag_dependency?(prop) | ||
| false |
There was a problem hiding this comment.
P2 · Reconcile cohort flag handling with the published SDK contract
For a cohort flag_evaluates_to leaf referencing a matching flag, this shortcut returns false instead of evaluating the dependency. The published local-feature-flag-evaluator spec explicitly applies flag dependency handling to dynamic-cohort leaves, so this changes behavior away from the canonical contract. Reconcile the spec with the intended server behavior and document the divergence.
There was a problem hiding this comment.
I checked this against the flags service, and the spec is what's wrong here, so I kept the code. In rust/feature-flags, evaluate_cohort_filter only special-cases cohort leaves. A type: "flag" leaf goes to the ordinary property matcher, which rejects flag_evaluates_to, and evaluate_property_with_negation turns that into false before applying negation. Running evaluate_dynamic_cohorts on posthog d72ad144b3c gives false for the leaf whatever the referenced flag returns, and true when it's negated. Resolving the dependency, as the spec says now, could return true where /flags returns false. PostHog/sdk-specs#267 changes the spec to match the service, and the PR description now notes the difference. I'll leave this thread open for you to confirm.
There was a problem hiding this comment.
Not approved — this change needs a human reviewer.
Add the stamphog label to request another review once you have addressed this.
Both substantive inline concerns remain unaddressed, so the current-head comment-only review does not provide independent assurance. Address @PostHog's findings and request review from a teammate or agent reviewer, with Gustavo H. Strassburger or Phil Haack as suggested reviewers, before re-requesting.
- Author wrote 0% of the modified lines and has 51 merged PRs in these paths (familiarity MODERATE).
- PostHog Review (Standard) reviewed the current head: 2 findings, posted on the PR.
- posthog[bot] reviewed the current head.
- @PostHog, lib/posthog/feature_flags.rb: The shortcut checks only leaf type and operator, without validating the required key. A keyless negated leaf therefore produces a positive match and is also classified as supported at load time. Address the missing-key validation concern before re-requesting.
- @PostHog, lib/posthog/feature_flags.rb: The concern about compatibility with the published cross-SDK dependency contract remains unresolved: current code unconditionally rejects these dependencies before negation, and no non-author acceptance or contract reconciliation is provided. The external specification and server behavior cannot be independently verified from this checkout. Reconcile the intended contract and document any divergence; if the finding is incorrect, explain why in the review thread and resolve it.
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✓ | no deny categories matched |
| every_file_attested | ✓ | not a bot PR |
| size | ✓ | 20L, 1F substantive, 85L/3F incl. docs/generated/snapshots — within ceiling |
| tier | ✓ | T1-agent / T1d-complex (85L, 3F, cross-cutting, fix) |
| stamphog 2.9.1 | .stamphog/policy.yml @ ee0240c · reviewed head ee0240c |
The flags service can't parse a flag leaf whose key isn't a string or number, and fails the whole cohort. Such a leaf now raises RequiresServerEvaluation, so the flag falls back to /flags, and the load-time check reports it as an unsupported definition. Before, a negated keyless leaf matched and enabled the flag locally.
There was a problem hiding this comment.
Not approved — this change needs a human reviewer.
Add the stamphog label to request another review once you have addressed this.
The missing-key concern is addressed by validation and regression tests, but @PostHog's contract-reconciliation concern remains unresolved and there is no current-head review covering the behavioral contract change. Request review from Gustavo H. Strassburger, Phil Haack, or another teammate to confirm the intended contract and resolve that concern before re-requesting.
- Author wrote 0% of the modified lines and has 51 merged PRs in these paths (familiarity MODERATE).
- @PostHog on lib/posthog/feature_flags.rb: The contract-reconciliation concern remains open. The current implementation deliberately skips dependency evaluation for these cohort leaves; the checkout contains no referenced evaluator specification or reconciliation establishing that this departure is accepted. Obtain independent confirmation of the intended contract and address the thread before re-requesting; if the finding is incorrect, explain why in the bot's thread and resolve it.
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✓ | no deny categories matched |
| every_file_attested | ✓ | not a bot PR |
| size | ✓ | 32L, 1F substantive, 117L/3F incl. docs/generated/snapshots — within ceiling |
| tier | ✓ | T1-agent / T1d-complex (117L, 3F, cross-cutting, fix) |
| stamphog 2.9.1 | .stamphog/policy.yml @ b7be793 · reviewed head b7be793 |
💡 Motivation and Context
A cohort definition can contain a flag dependency leaf:
{"type": "flag", "operator": "flag_evaluates_to", ...}. Local evaluation sent it to the ordinary property matcher, which raisedUnknown operator. So the flag fell back to/flags,only_evaluate_locallyreturnednil, and the load-time check reported the flag as an unsupported definition.The flags service (
rust/feature-flagsin PostHog/posthog) doesn't resolve flag dependencies inside a cohort. It parses the leaf as an ordinary property filter, and its property matcher rejectsflag_evaluates_to, so the leaf is a definitive non-match. I checked this by running the server'sevaluate_dynamic_cohortsdirectly. It returnsfalsefor the leaf whatever the referenced flag returns, andtruewhen the leaf is negated. Any other operator on aflagleaf compares the leaf's key with person properties. Ruby already did that, so it doesn't change.This change treats a
flag_evaluates_toleaf inside a cohort as a non-match beforenegationis applied, and stops the load-time check from reporting it as unsupported. A leaf whosekeyisn't a string or number is the one exception. The flags service can't parse it and fails the whole cohort, so the SDK now falls back to the server for it, and the load-time check reports it. Flag dependencies in release conditions are unchanged.Spec divergence: the local evaluator spec currently says every
flagfilter, including one inside a cohort, evaluates the dependency. That doesn't match the flags service, so this PR follows the service. PostHog/sdk-specs#267 changes the spec to match.The cohort API only accepts
behavioral,cohort,personandperson_metadataleaves, so this shape should be rare. It mainly matters for older stored cohorts.Follows #394, which kept this case out of scope. Some SDKs, for example posthog-go and posthog-python, evaluate this leaf as a real dependency, which can disagree with the server.
💚 How did you test it?
spec/posthog/feature_flags_cohort_flag_leaf_spec.rb(10 examples). They cover non-match whatever the referenced flag and caller properties are, negation, a leaf withoutvalueordependency_chain,exactleaves against person properties, leaves with a missing,nil, array or hash key (fall back to the server) and a numeric key (stays local), and the load-time check. Each was checked against the flags service'sevaluate_dynamic_cohorts. They fail without the fix.bundle exec rspec: 2635 examples, 0 failures (49 pending need the optional otel bundle).bundle exec rubocopandrake public_api:checkpass.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)