Repository navigation
fix(flags): don't match person_metadata filters against person properties - #1288
Conversation
…ties A cohort can filter on Person metadata > First seen (type "person_metadata", key "created_at"). /flags reads that value from the persons table and ignores any created_at person property. Local evaluation treated every filter type other than cohort and flag as a person property, so a caller passing created_at got an answer based on that value, which could disagree with /flags. Only person and group filters are now matched against the properties the caller passes. Any other type is unsupported_definition: the definition check flags it when definitions load, and evaluation falls back to /flags (or stays unresolved with only_evaluate_locally). match_property itself is unchanged. Part of PostHog/posthog#137503 (posthog-python) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dustinbyrne
left a comment
There was a problem hiding this comment.
The runtime change in posthog/feature_flags.py looks minimal and matches both /flags (person_metadata reads from the persons table rather than person properties) and posthog-dotnet's allow-list approach for unsupported property filter types.
There are two test concerns inline before approval:
- The new
_is_unsupported_propertycheck (posthog/feature_flags.py:1862) has no test coverage, and the new test file only exercises the deprecatedget_feature_flagpath. TestPersonMetadataFilters._clientconstructs liveClient(..., send=True)instances withoutclient.shutdown(), leaking backgroundConsumerthreads that attempt outbound capture POSTs during longer test runs.
Outside the scope of this PR (non-blocking follow-ups): codifying the property filter type allow-list in PostHog/sdk-specs, aligning remaining peer server SDKs and the posthog.com local-evaluation restrictions docs, and evaluating separately whether sdk-specs should address multi-condition multivariate / early_exit catch-all fallthrough after an inconclusive condition.
AI-assisted review.
The error let later conditions decide the flag. Only /flags knows the value, so treat it like a static cohort. Also cover the load-time warning, evaluate_flags and negation, with a client that sends nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dustinbyrne
left a comment
There was a problem hiding this comment.
Thanks for updating the tests and fixing the test client cleanup — both concerns from the earlier review (_load_feature_flags() / evaluate_flags() / unresolved_flags / negation & negative-control coverage, and send=False + client.shutdown()) are addressed cleanly.
One Breaking regression remains inline on posthog/feature_flags.py before approval: switching _assert_property_filter_type_supported to raise RequiresServerEvaluation breaks three-valued AND/OR short-circuiting in match_property_group for dynamic cohorts that combine person_metadata with another filter (affecting both AND — including not_in — and OR cohorts, regressing merge-base behavior when created_at is omitted, and diverging from sdk-specs).
Optional Suggestion (non-blocking, outside the current diff): consider adding "an unsupported filter type" to the example list in the load-time unsupported_definition warning (posthog/client.py:4280-4288) and UnresolvedFlagReason.UNSUPPORTED_DEFINITION docstring (posthog/types.py:130-133) so operators seeing the warning for a dynamic cohort with a First seen filter can diagnose it directly.
(Outside this PR's scope as non-blocking follow-ups: pre-existing filter-order handling in _match_condition_properties at posthog/feature_flags.py:928 matches test_static_cohort_after_unchecked_filter_lets_later_conditions_resolve and peer SDKs; documenting person_metadata restrictions in PostHog/posthog.com and codifying unsupported filter types in PostHog/sdk-specs.)
AI-assisted review.
A cohort stopped at the first filter that only the server can check, even when another filter had already decided it. Raise the error only when the cohort stays undecided. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dustinbyrne
left a comment
There was a problem hiding this comment.
Thanks for the fast follow-up in 07ee9f6 — the _RequiresServerFilter deferral cleanly resolves the dynamic-cohort three-valued AND/OR regression (all 9 decisive-sibling regression cases now pass on 07ee9f6, alongside all 21 new tests and 1530 flag tests), and the warning/docstring updates look great.
One narrow Concern remains inline on posthog/feature_flags.py before approval: deferring _RequiresServerFilter in match_property_group drops higher-precedence UnresolvedFlagReason values (UNSUPPORTED_DEFINITION lost to UNRESOLVED_DEPENDENCY), with the same precedence drop amplified at _raise_condition_errors:960-961 and _match_flag_conditions:633-641. There is also one optional Suggestion inline on posthog/test/test_feature_flags_person_metadata.py.
(Outside this PR's scope as non-blocking follow-ups: documenting person_metadata / unsupported filter types in PostHog/posthog.com's local-evaluation restrictions, codifying them in PostHog/sdk-specs, and aligning peer server SDKs under PostHog/posthog#137503.)
AI-assisted review.
…lter An error that only the server can resolve kept its own reason, so a flag with a runtime unsupported_definition filter could be reported as unresolved_dependency. The reasons are now merged in the cohort group, in the condition and across conditions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SDK performance: posthog-pythonBenchmarks 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 |
|
lemme confirm PostHog/sdk-specs#251 before merging |
marandaneto
left a comment
There was a problem hiding this comment.
Agent-drafted and reviewed by Manoel.
Only a filter without a type is a person filter (PostHog/sdk-specs#251). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…c cohort dependency
marandaneto
left a comment
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
Thank you for your contribution!
…ests Main #1288 (sdk-specs#251) sends a person_metadata filter to /flags instead of matching it against a supplied created_at, even when created_at is listed. The test that expected a person_metadata filter to read the supplied value is deleted; test_listed_key_does_not_resolve_a_person_metadata_filter already asserts the new behavior. The duplicate person key test used a person_metadata filter to read the value outside the listing's scope. No supported filter reads a person listing outside its scope now, so it checks that a duplicated person key still reads as absent. The group duplicate test keeps covering the out-of-scope read.
💡 Motivation and Context
Part of PostHog/posthog#137503, for posthog-python, where the issue was reproduced.
A cohort can filter on Person metadata > First seen:
{"type": "person_metadata", "key": "created_at", ...}./flagsreads that value from the persons table and ignores anycreated_atperson property. Local evaluation treated every filter type other thancohortandflagas a person property, so:person_properties={}was inconclusive and fell back to/flags(correct);person_properties={"created_at": "2026-06-01"}returned a local answer from that value, which can disagree with/flagsfor teams that set their owncreated_atperson property.This follows the merged spec requirement "Unsupported property-filter types are inconclusive" (PostHog/sdk-specs#251):
_match_condition_filternow matches onlypersonandgroupfilters, or one whosetypeis missing,nullor"", against the caller's properties.cohortandflagfilters keep their own matching. Any other type raisesInconclusiveMatchErrorwithunsupported_definition. This covers flag conditions and cohort criteria, which both go through it.not_indon't turn it into a match, another filter can still decide a cohort (ANDwith a mismatch,ORwith a match) or a condition, and a later condition can still match. An inconclusive condition doesn't triggerearly_exit, as the spec's condition-group requirement says._is_unsupported_propertyapplies the same rule, so the definition check marks such a flagunsupported_definitionwhen definitions load.unsupported_definitionstill wins overunresolved_dependency.UNSUPPORTED_DEFINITIONdocstring mention filter types.match_propertyis unchanged.The same change for posthog-node is PostHog/posthog-js#5840, for posthog-go PostHog/posthog-go#465, and for posthog-rs PostHog/posthog-rs#397.
💚 How did you test it?
New
posthog/test/test_feature_flags_person_metadata.py, throughClient.evaluate_flags:person_metadatafilter in a cohort, caller passescreated_at/flagsand returns its answerperson_metadatafilter directly in a flag condition/flagsonly_evaluate_locally=True, with and withoutcreated_at, and with negation ornot_inunsupported_definition, no/flagscallunsupported_definitionwarningearly_exit, a later condition excluded by its rollout/flagsFalselocallyANDorORcohort, also withnot_in, in either order, with the filter nested, in another cohort or behind a flag dependency/flagscallpersonfilter oncreated_at, or one whosetypeis missing,nullor"", or agroupfilterReverting the filter-type check, the definition check or either reason-precedence line makes some of these fail.
posthog/testsuite passes (8432 passed) apart from 3 fork tests intest_metrics_autocapture.pythat also fail onmainthere.ruff check,ruff format --checkand mypy are clean.3d709c7, on Windows: 2139 passed, 1 failed, the digest of vendored fixtures below.posthog/testtree with thetestextra, on Windows, run on an earlier commit: 8938 passed, 41 skipped, 16 failed. 14 of them fail on the base commit too:forkhooks, path separators, and a digest of vendored fixtures. The other two are timing-dependent here: one also fails without this commit, the other passes when run alone.Posthogclient against a local HTTP server that serves the definitions and/flags, with anANDcohort of a plan filter and First seen. It answers locally for a person the plan rules out, and makes exactly one/flagsrequest otherwise.ruff format --checkis clean.ruff checkreports nothing in the new test file and nothing new infeature_flags.py.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
3d709c7.person,group) instead of special-casingperson_metadata, so a filter type added on the server later also falls back instead of being matched as a person property.nullor""type is a person filter. He also added the precedence test from review.🤖 Generated with Claude Code