posthog-ruby 3.40.0 → 3.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,9 +8,11 @@ require 'posthog/logging'
8
8
  require 'posthog/defaults'
9
9
  require 'posthog/backoff_policy'
10
10
  require 'posthog/feature_flag'
11
+ require 'posthog/feature_flag_result'
11
12
  require 'posthog/flag_definition_cache'
12
13
  require 'posthog/unresolved_flag_reason'
13
14
  require 'posthog/flag_evaluation_errors'
15
+ require 'posthog/known_absent_property_keys'
14
16
  require 'digest'
15
17
 
16
18
  module PostHog
@@ -43,6 +45,10 @@ module PostHog
43
45
  NUMERIC_OPERATORS = %w[gt gte lt lte].freeze
44
46
  DATE_OPERATORS = %w[is_date_before is_date_after].freeze
45
47
  SEMVER_COMPARISON_OPERATORS = %w[semver_eq semver_neq semver_gt semver_gte semver_lt semver_lte].freeze
48
+ # Every property-filter operator this SDK evaluates locally.
49
+ LOCAL_OPERATORS = (%w[exact is_not is_set is_not_set icontains not_icontains starts_with not_starts_with
50
+ ends_with not_ends_with semver_tilde semver_caret semver_wildcard] +
51
+ REGEX_OPERATORS + NUMERIC_OPERATORS + DATE_OPERATORS + SEMVER_COMPARISON_OPERATORS).freeze
46
52
 
47
53
  # @param polling_interval [Integer, nil] Seconds between local feature flag definition polls.
48
54
  # Defaults to {Defaults::FeatureFlags::POLLING_INTERVAL_SECONDS}.
@@ -154,7 +160,8 @@ module PostHog
154
160
  person_properties = {},
155
161
  group_properties = {},
156
162
  only_evaluate_locally = false,
157
- raise_on_error = false
163
+ raise_on_error = false,
164
+ known_absent: nil
158
165
  )
159
166
  groups, person_properties, group_properties = normalize_flag_evaluation_inputs(
160
167
  groups, person_properties, group_properties
@@ -166,7 +173,8 @@ module PostHog
166
173
  person_properties,
167
174
  group_properties,
168
175
  only_evaluate_locally,
169
- raise_on_error
176
+ raise_on_error,
177
+ known_absent: known_absent
170
178
  )
171
179
 
172
180
  if flags_data.key?(:featureFlags)
@@ -183,14 +191,14 @@ module PostHog
183
191
  person_properties = {},
184
192
  group_properties = {},
185
193
  _only_evaluate_locally = false,
186
- device_id: nil
194
+ known_absent: nil, device_id: nil
187
195
  )
188
196
  flags_data = get_all_flags_and_payloads(
189
197
  distinct_id,
190
198
  groups,
191
199
  person_properties,
192
200
  group_properties,
193
- device_id: device_id
201
+ known_absent: known_absent, device_id: device_id
194
202
  )
195
203
 
196
204
  if flags_data.key?(:featureFlagPayloads)
@@ -202,14 +210,16 @@ module PostHog
202
210
  end
203
211
 
204
212
  def get_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, flag_keys = nil,
205
- disable_geoip = nil, device_id: nil)
213
+ disable_geoip = nil, known_absent: nil, device_id: nil)
206
214
  flag_keys = flag_keys.map(&:to_s) if flag_keys.is_a?(Array)
215
+ groups, group_properties = without_non_finite_group_keys(groups, group_properties)
207
216
  request_data = {
208
217
  distinct_id: distinct_id,
209
218
  groups: groups,
210
219
  person_properties: person_properties,
211
220
  group_properties: group_properties
212
221
  }
222
+ request_data.merge!(known_absent.request_fields) if known_absent
213
223
  device_id = FeatureFlagsPoller.usable_device_id(device_id)
214
224
  request_data[:device_id] = device_id unless device_id.nil?
215
225
  request_data[:flag_keys_to_evaluate] = flag_keys if flag_keys && !flag_keys.empty?
@@ -253,7 +263,7 @@ module PostHog
253
263
  group_properties = {},
254
264
  only_evaluate_locally = false,
255
265
  snapshot: nil,
256
- device_id: nil
266
+ known_absent: nil, device_id: nil
257
267
  )
258
268
  key = key.to_s
259
269
 
@@ -268,7 +278,7 @@ module PostHog
268
278
  unless feature_flag.nil?
269
279
  begin
270
280
  response = _compute_flag_locally(feature_flag, distinct_id, groups, person_properties, group_properties,
271
- snapshot: snapshot, device_id: device_id)
281
+ snapshot: snapshot, known_absent: known_absent, device_id: device_id)
272
282
  payload = _compute_flag_payload_locally(key, response, snapshot: snapshot) unless response.nil?
273
283
  logger.debug "Successfully computed flag locally: #{key} -> #{response}"
274
284
  rescue RequiresServerEvaluation, InconclusiveMatchError => e
@@ -298,7 +308,8 @@ module PostHog
298
308
  begin
299
309
  errors = []
300
310
  flags_data = get_all_flags_and_payloads(distinct_id, groups, person_properties, group_properties,
301
- only_evaluate_locally, true, device_id: device_id)
311
+ only_evaluate_locally, true, known_absent: known_absent,
312
+ device_id: device_id)
302
313
  if flags_data.key?(:featureFlags)
303
314
  flags = stringify_keys(flags_data[:featureFlags] || {})
304
315
  payloads = stringify_keys(flags_data[:featureFlagPayloads] || {})
@@ -353,7 +364,7 @@ module PostHog
353
364
  person_properties = {},
354
365
  group_properties = {},
355
366
  only_evaluate_locally = false,
356
- device_id: nil
367
+ known_absent: nil, device_id: nil
357
368
  )
358
369
  if @quota_limited.true?
359
370
  logger.debug 'Not fetching flags from server - quota limited'
@@ -367,7 +378,7 @@ module PostHog
367
378
  person_properties,
368
379
  group_properties,
369
380
  only_evaluate_locally,
370
- device_id: device_id
381
+ known_absent: known_absent, device_id: device_id
371
382
  )
372
383
 
373
384
  response[:featureFlags]
@@ -380,7 +391,7 @@ module PostHog
380
391
  group_properties = {},
381
392
  only_evaluate_locally = false,
382
393
  raise_on_error = false,
383
- device_id: nil
394
+ known_absent: nil, device_id: nil
384
395
  )
385
396
  load_feature_flags
386
397
  snapshot = _evaluation_snapshot
@@ -393,7 +404,7 @@ module PostHog
393
404
 
394
405
  snapshot[:flags].each do |flag|
395
406
  match_value = _compute_flag_locally(flag, distinct_id, groups, person_properties, group_properties,
396
- snapshot: snapshot, device_id: device_id)
407
+ snapshot: snapshot, known_absent: known_absent, device_id: device_id)
397
408
  flags[flag[:key]] = match_value
398
409
  match_payload = _compute_flag_payload_locally(flag[:key], match_value, snapshot: snapshot)
399
410
  payloads[flag[:key]] = match_payload if match_payload
@@ -413,7 +424,7 @@ module PostHog
413
424
  if fallback_to_server && !only_evaluate_locally
414
425
  begin
415
426
  flags_and_payloads = get_flags(distinct_id, groups, person_properties, group_properties,
416
- device_id: device_id)
427
+ known_absent: known_absent, device_id: device_id)
417
428
  flag_details = flags_and_payloads[:flags]
418
429
  errors_while_computing = flags_and_payloads[:errorsWhileComputingFlags] || false
419
430
  quota_limited = flags_and_payloads[:quotaLimited]
@@ -477,7 +488,7 @@ module PostHog
477
488
  person_properties = {},
478
489
  group_properties = {},
479
490
  only_evaluate_locally = false,
480
- device_id: nil
491
+ known_absent: nil, device_id: nil
481
492
  )
482
493
  key = key.to_s
483
494
  load_feature_flags
@@ -492,17 +503,20 @@ module PostHog
492
503
  group_properties,
493
504
  true,
494
505
  snapshot: snapshot,
495
- device_id: device_id
506
+ known_absent: known_absent, device_id: device_id
496
507
  )[0]
497
508
  end
498
509
  response = nil
499
510
  response = _compute_flag_payload_locally(key, match_value, snapshot: snapshot) unless match_value.nil?
500
511
  if response.nil? && !only_evaluate_locally
501
512
  flags_payloads = get_feature_payloads(distinct_id, groups, person_properties, group_properties,
502
- device_id: device_id)
513
+ known_absent: known_absent, device_id: device_id)
503
514
  response = flags_payloads[key.downcase] || nil
504
515
  end
505
- response
516
+ # This getter hands back the payload in its stored, serialized form, so
517
+ # validate it rather than decoding it: a payload that isn't valid JSON
518
+ # reads as no payload here too.
519
+ FeatureFlagResult.validate_serialized_payload(response)
506
520
  end
507
521
 
508
522
  def shutdown_poller
@@ -518,6 +532,20 @@ module PostHog
518
532
 
519
533
  # Class methods
520
534
 
535
+ # A usable group key is a non-empty string or a finite number, as it is sent to the server as JSON. A
536
+ # Symbol is sent as its string. Any other key counts as no key, matching the server: conditions
537
+ # aggregated on that group type are skipped.
538
+ def self.usable_group_key?(group_key)
539
+ case group_key
540
+ when String, Symbol
541
+ !group_key.empty?
542
+ when Numeric
543
+ group_key.finite?
544
+ else
545
+ false
546
+ end
547
+ end
548
+
521
549
  # A device id is usable only as a non-empty string; anything else counts as missing.
522
550
  def self.usable_device_id(device_id)
523
551
  device_id.is_a?(String) && !device_id.empty? ? device_id : nil
@@ -790,17 +818,32 @@ module PostHog
790
818
 
791
819
  private_class_method :boolean_like?, :legacy_truthy?, :sorted_composite, :property_string, :exact_property_match?
792
820
 
793
- def self.match_property(property, property_values, cohort_properties = {}, property_matching_version: 1)
821
+ # @param known_absent_scope [PostHog::KnownAbsentPropertyKeys::Scope, nil] Keys of property_values known
822
+ # to be absent. A filter the scope covers resolves definitively by operator, even when property_values
823
+ # has a value for it.
824
+ def self.match_property(property, property_values, cohort_properties = {}, property_matching_version: 1,
825
+ known_absent_scope: nil)
794
826
  # only looks for matches where key exists in property_values
795
827
 
796
828
  # Handle cohort properties
797
829
  if extract_value(property, :type) == 'cohort'
798
830
  return match_cohort(property, property_values, cohort_properties,
799
- property_matching_version: property_matching_version)
831
+ property_matching_version: property_matching_version,
832
+ known_absent_scope: known_absent_scope)
800
833
  end
801
834
 
835
+ # An unsupported filter type stays inconclusive even when its key is listed known absent.
802
836
  assert_property_filter_type_supported(property)
803
837
 
838
+ if known_absent_scope&.covers?(property)
839
+ # Like the flags service, a listed key resolves without parsing the condition value. An operator
840
+ # this SDK does not implement stays inconclusive, so it still falls back to the server.
841
+ operator = property[:operator] || property['operator'] || 'exact'
842
+ assert_operator_supported(operator)
843
+ assert_condition_value_present(property, operator)
844
+ return KnownAbsentPropertyKeys::MATCHING_OPERATORS.include?(operator)
845
+ end
846
+
804
847
  begin
805
848
  match_property_value(property, property_values, property_matching_version)
806
849
  rescue InconclusiveMatchError => e
@@ -810,6 +853,17 @@ module PostHog
810
853
  end
811
854
  end
812
855
 
856
+ # An omitted or JSON null condition value leaves a value-requiring operator inconclusive, even for a
857
+ # known-absent key, because is_not would otherwise match.
858
+ def self.assert_condition_value_present(property, operator)
859
+ return if %w[is_set is_not_set].include?(operator)
860
+ return unless (property.key?(:value) ? property[:value] : property['value']).nil?
861
+
862
+ raise InconclusiveMatchError.new("Missing condition value for operator #{operator}",
863
+ reason: UnresolvedFlagReason::UNSUPPORTED_DEFINITION)
864
+ end
865
+ private_class_method :assert_condition_value_present
866
+
813
867
  def self.match_property_value(property, property_values, property_matching_version)
814
868
  key = property[:key] || property['key']
815
869
  key = key.to_s if key.is_a?(Numeric)
@@ -902,6 +956,13 @@ module PostHog
902
956
  end
903
957
  private_class_method :match_property_value
904
958
 
959
+ def self.assert_operator_supported(operator)
960
+ return if LOCAL_OPERATORS.include?(operator)
961
+
962
+ raise InconclusiveMatchError.new("Unknown operator: #{operator}",
963
+ reason: UnresolvedFlagReason::UNSUPPORTED_DEFINITION)
964
+ end
965
+
905
966
  # Filter types matched against the person or group properties the call passes. A missing, null, or empty
906
967
  # type is a person filter. Cohort and flag filters have their own matching. Any other type, such as
907
968
  # person_metadata (a value the flags service reads from the persons table, not from person properties),
@@ -926,10 +987,8 @@ module PostHog
926
987
  def self.assert_filter_supported(property)
927
988
  value = property.key?(:value) ? property[:value] : property['value']
928
989
  operator = property[:operator] || property['operator'] || 'exact'
990
+ assert_operator_supported(operator)
929
991
  case operator
930
- when 'exact', 'is_not', 'is_set', 'is_not_set', 'icontains', 'not_icontains', 'starts_with',
931
- 'not_starts_with', 'ends_with', 'not_ends_with'
932
- nil
933
992
  when *REGEX_OPERATORS
934
993
  # An empty input is the most portable one, so this raises only for a pattern that needs the
935
994
  # server whatever the value. Compile without matching, so a server-supplied pattern never runs
@@ -948,9 +1007,6 @@ module PostHog
948
1007
  semver_caret_bounds(value)
949
1008
  when 'semver_wildcard'
950
1009
  semver_wildcard_bounds(value)
951
- else
952
- raise InconclusiveMatchError.new("Unknown operator: #{operator}",
953
- reason: UnresolvedFlagReason::UNSUPPORTED_DEFINITION)
954
1010
  end
955
1011
  end
956
1012
 
@@ -1125,7 +1181,8 @@ module PostHog
1125
1181
  end
1126
1182
  private_class_method :regex_repetition_end
1127
1183
 
1128
- def self.match_cohort(property, property_values, cohort_properties, property_matching_version: 1)
1184
+ def self.match_cohort(property, property_values, cohort_properties, property_matching_version: 1,
1185
+ known_absent_scope: nil)
1129
1186
  # Cohort properties are in the form of property groups like this:
1130
1187
  # {
1131
1188
  # "cohort_id" => {
@@ -1147,10 +1204,12 @@ module PostHog
1147
1204
 
1148
1205
  assert_cohort_flag_leaves_parse(property_group, cohort_properties, Set.new)
1149
1206
  match_property_group(property_group, property_values, cohort_properties,
1150
- property_matching_version: property_matching_version)
1207
+ property_matching_version: property_matching_version,
1208
+ known_absent_scope: known_absent_scope)
1151
1209
  end
1152
1210
 
1153
- def self.match_property_group(property_group, property_values, cohort_properties, property_matching_version: 1)
1211
+ def self.match_property_group(property_group, property_values, cohort_properties, property_matching_version: 1,
1212
+ known_absent_scope: nil)
1154
1213
  return true if property_group.nil? || property_group.empty?
1155
1214
 
1156
1215
  group_type = extract_value(property_group, :type)
@@ -1161,7 +1220,8 @@ module PostHog
1161
1220
  if nested_property_group?(properties)
1162
1221
  property_group_items_match?(properties, group_type) do |group|
1163
1222
  match_property_group(group, property_values, cohort_properties,
1164
- property_matching_version: property_matching_version)
1223
+ property_matching_version: property_matching_version,
1224
+ known_absent_scope: known_absent_scope)
1165
1225
  end
1166
1226
  else
1167
1227
  property_group_items_match?(properties, group_type) do |prop|
@@ -1169,7 +1229,8 @@ module PostHog
1169
1229
  false
1170
1230
  else
1171
1231
  match_property(prop, property_values, cohort_properties,
1172
- property_matching_version: property_matching_version)
1232
+ property_matching_version: property_matching_version,
1233
+ known_absent_scope: known_absent_scope)
1173
1234
  end
1174
1235
  extract_value(prop, :negation) ? !matches : matches
1175
1236
  end
@@ -1274,10 +1335,14 @@ module PostHog
1274
1335
  # @param distinct_id [String] The distinct ID being evaluated
1275
1336
  # @param properties [Hash] Person properties for evaluation
1276
1337
  # @param cohort_properties [Hash] Cohort properties for evaluation
1338
+ # @param context [Hash, nil] The evaluation inputs of the top-level flag. Each dependency is evaluated from
1339
+ # them like a standalone flag, with its own aggregation, bucketing identifier and properties, as the flags
1340
+ # service does.
1277
1341
  # @return [Boolean] True if the referenced flag's value matches property[:value], false otherwise
1278
- def evaluate_flag_dependency(property, evaluation_cache, distinct_id, properties, cohort_properties,
1279
- person_properties: properties, snapshot: _evaluation_snapshot,
1280
- device_id: nil, dependency_evaluation: nil)
1342
+ def evaluate_flag_dependency(property, evaluation_cache, distinct_id, properties, _cohort_properties = {},
1343
+ snapshot: _evaluation_snapshot, context: nil, dependency_evaluation: nil)
1344
+ context ||= { distinct_id: distinct_id, groups: {}, person_properties: properties, group_properties: {},
1345
+ known_absent: nil, device_id: nil }
1281
1346
  dependency_evaluation ||= DependencyEvaluation.new
1282
1347
  if snapshot[:flags_by_key].nil? || evaluation_cache.nil?
1283
1348
  # Cannot evaluate flag dependencies without required context
@@ -1302,9 +1367,8 @@ module PostHog
1302
1367
  actual_value = if evaluation_cache.key?(dep_flag_key)
1303
1368
  cached_flag_dependency_value(dep_flag_key, evaluation_cache)
1304
1369
  else
1305
- evaluate_flag_dependency_value(dep_flag_key, evaluation_cache, distinct_id, properties,
1306
- cohort_properties, person_properties, snapshot,
1307
- dependency_evaluation, device_id: device_id)
1370
+ evaluate_flag_dependency_value(dep_flag_key, evaluation_cache, snapshot, context,
1371
+ dependency_evaluation)
1308
1372
  end
1309
1373
 
1310
1374
  self.class.matches_dependency_value(property[:value], actual_value)
@@ -1324,8 +1388,7 @@ module PostHog
1324
1388
  value
1325
1389
  end
1326
1390
 
1327
- def evaluate_flag_dependency_value(dep_flag_key, evaluation_cache, distinct_id, properties, cohort_properties,
1328
- person_properties, snapshot, dependency_evaluation, device_id: nil)
1391
+ def evaluate_flag_dependency_value(dep_flag_key, evaluation_cache, snapshot, context, dependency_evaluation)
1329
1392
  dep_flag = snapshot[:flags_by_key][dep_flag_key]
1330
1393
  # Same check as in _compute_flag_locally.
1331
1394
  return evaluation_cache[dep_flag_key] = false unless dep_flag[:active]
@@ -1338,9 +1401,8 @@ module PostHog
1338
1401
  )
1339
1402
  end
1340
1403
 
1341
- evaluate_uncached_flag_dependency(dep_flag_key, dep_flag, evaluation_cache, distinct_id, properties,
1342
- cohort_properties, person_properties, snapshot, dependency_evaluation,
1343
- device_id: device_id)
1404
+ evaluate_uncached_flag_dependency(dep_flag_key, dep_flag, evaluation_cache, snapshot, context,
1405
+ dependency_evaluation)
1344
1406
  end
1345
1407
 
1346
1408
  # Per-evaluation bookkeeping for flag dependencies, kept apart from the result cache. A cycle depends on the
@@ -1438,9 +1500,8 @@ module PostHog
1438
1500
  end
1439
1501
  end
1440
1502
 
1441
- def evaluate_uncached_flag_dependency(dep_flag_key, dep_flag, evaluation_cache, distinct_id, properties,
1442
- cohort_properties, person_properties, snapshot, dependency_evaluation,
1443
- device_id: nil)
1503
+ def evaluate_uncached_flag_dependency(dep_flag_key, dep_flag, evaluation_cache, snapshot, context,
1504
+ dependency_evaluation)
1444
1505
  dependency_evaluation.cycle!(dep_flag_key) if dependency_evaluation.in_progress.include?(dep_flag_key)
1445
1506
  if dependency_evaluation.evaluations >= MAX_FLAG_DEPENDENCY_EVALUATIONS
1446
1507
  dependency_evaluation.depth_limit!(dep_flag_key,
@@ -1490,15 +1551,11 @@ module PostHog
1490
1551
  end
1491
1552
  dependency_evaluation.enter(dep_flag_key)
1492
1553
  begin
1493
- dep_result = if FeatureFlagsPoller.feature_enrollment?(dep_flag)
1494
- FeatureFlagsPoller.feature_enrolled?(dep_flag, person_properties || {})
1495
- else
1496
- match_feature_flag_properties(
1497
- dep_flag, distinct_id, properties, evaluation_cache, cohort_properties,
1498
- person_properties: person_properties, snapshot: snapshot, device_id: device_id,
1499
- dependency_evaluation: dependency_evaluation
1500
- )
1501
- end
1554
+ dep_result = _compute_flag_locally(
1555
+ dep_flag, context[:distinct_id], context[:groups], context[:person_properties], context[:group_properties],
1556
+ snapshot: snapshot, known_absent: context[:known_absent], evaluation_cache: evaluation_cache,
1557
+ dependency_evaluation: dependency_evaluation, device_id: context[:device_id]
1558
+ )
1502
1559
  rescue RequiresServerEvaluation => e
1503
1560
  error = RequiresServerEvaluation.new("Cannot evaluate flag dependency '#{dep_flag_key}': #{e.message}",
1504
1561
  reason: UnresolvedFlagReason::UNRESOLVED_DEPENDENCY)
@@ -1559,8 +1616,12 @@ module PostHog
1559
1616
  # An early-access feature flag resolves to the person's enrollment: only true or "true" means enrolled,
1560
1617
  # and any other present value, nil included, means opted out. Without the property only /flags knows
1561
1618
  # the stored enrollment.
1619
+ def self.feature_enrollment_key(flag)
1620
+ "$feature_enrollment/#{flag[:key]}"
1621
+ end
1622
+
1562
1623
  def self.feature_enrolled?(flag, person_properties)
1563
- enrollment_key = "$feature_enrollment/#{flag[:key]}"
1624
+ enrollment_key = feature_enrollment_key(flag)
1564
1625
  enrollment = person_properties.fetch(enrollment_key.to_sym) do
1565
1626
  person_properties.fetch(enrollment_key) do
1566
1627
  raise InconclusiveMatchError.new(
@@ -1574,13 +1635,15 @@ module PostHog
1574
1635
 
1575
1636
  # The unresolved reason that comes from the flag's definition alone, so it is known when
1576
1637
  # definitions load. Matches the reason local evaluation reports for the same cause.
1577
- def self.definition_unresolved_reason(flag, flags_by_key, cohorts, group_type_mapping)
1638
+ # ignore_feature_enrollment is for a call whose known-absent enrollment key lets the release conditions decide.
1639
+ def self.definition_unresolved_reason(flag, flags_by_key, cohorts, group_type_mapping,
1640
+ ignore_feature_enrollment: false)
1578
1641
  return nil unless flag[:active]
1579
1642
  return UnresolvedFlagReason::EXPERIENCE_CONTINUITY if flag[:ensure_experience_continuity]
1580
1643
 
1581
1644
  flag_filters = flag[:filters] || {}
1582
1645
  # Enrollment decides the flag before its group aggregation and release conditions are read.
1583
- return nil if feature_enrollment?(flag)
1646
+ return nil if !ignore_feature_enrollment && feature_enrollment?(flag)
1584
1647
 
1585
1648
  aggregation = flag_filters[:aggregation_group_type_index]
1586
1649
  if !aggregation.nil? && group_type_mapping[aggregation.to_s.to_sym].nil?
@@ -1675,7 +1738,7 @@ module PostHog
1675
1738
  :assert_cohort_flag_leaves_parse,
1676
1739
  :find_cohort_property, :nested_property_group?,
1677
1740
  :property_group_items_match?, :assert_property_filter_type_supported,
1678
- :assert_filter_supported,
1741
+ :assert_filter_supported, :assert_operator_supported,
1679
1742
  :unsupported_property?, :unsupported_property_group?, :unsupported_filter?
1680
1743
 
1681
1744
  # Publish/capture all matching state together so a poll cannot switch semantics
@@ -1685,7 +1748,8 @@ module PostHog
1685
1748
  end
1686
1749
 
1687
1750
  def _compute_flag_locally(flag, distinct_id, groups = {}, person_properties = {}, group_properties = {},
1688
- snapshot: _evaluation_snapshot, device_id: nil)
1751
+ snapshot: _evaluation_snapshot, known_absent: nil, evaluation_cache: nil,
1752
+ dependency_evaluation: nil, device_id: nil)
1689
1753
  # An inactive flag is false whatever its experience continuity setting.
1690
1754
  return false unless flag[:active]
1691
1755
 
@@ -1695,12 +1759,14 @@ module PostHog
1695
1759
  end
1696
1760
 
1697
1761
  person_properties ||= {}
1698
- # Enrollment is a person property, so group-aggregated flags read it from the person too.
1699
- return self.class.feature_enrolled?(flag, person_properties) if self.class.feature_enrollment?(flag)
1700
-
1701
- # Create evaluation cache for flag dependencies
1702
- evaluation_cache = {}
1762
+ # Enrollment is a person property, so group-aggregated flags read it from the person too. A known-absent
1763
+ # enrollment key means not enrolled, so the holdout and release conditions decide the flag.
1764
+ if self.class.feature_enrollment?(flag) &&
1765
+ !known_absent&.person_keys&.include?(self.class.feature_enrollment_key(flag))
1766
+ return self.class.feature_enrolled?(flag, person_properties)
1767
+ end
1703
1768
 
1769
+ evaluation_cache ||= {}
1704
1770
  flag_filters = flag[:filters] || {}
1705
1771
  # An explicit device id wins; otherwise use the `$device_id` person property, as /flags does.
1706
1772
  # Without a device-bucketed flag in the definitions (dependencies included), no flag reads it.
@@ -1712,18 +1778,19 @@ module PostHog
1712
1778
  person_properties&.fetch(:$device_id, nil) || person_properties&.fetch('$device_id', nil)
1713
1779
  )
1714
1780
  end
1781
+ context = { distinct_id: distinct_id, groups: groups, person_properties: person_properties,
1782
+ group_properties: group_properties, known_absent: known_absent, device_id: device_id }
1715
1783
 
1716
1784
  aggregation_group_type_index = flag_filters[:aggregation_group_type_index]
1717
1785
  if aggregation_group_type_index.nil?
1718
- local_person_properties = person_properties
1719
- unless local_person_properties.key?(:distinct_id) || local_person_properties.key?('distinct_id')
1720
- local_person_properties = local_person_properties.merge(distinct_id: distinct_id)
1721
- end
1722
-
1723
- return match_feature_flag_properties(flag, distinct_id, local_person_properties, evaluation_cache,
1786
+ return match_feature_flag_properties(flag, distinct_id, person_properties_with_distinct_id(context),
1787
+ evaluation_cache,
1724
1788
  snapshot[:cohorts], groups: groups, group_properties: group_properties,
1725
- person_properties: person_properties,
1726
- snapshot: snapshot, device_id: device_id)
1789
+ snapshot: snapshot, known_absent: known_absent,
1790
+ known_absent_scope: known_absent&.person_scope,
1791
+ context: context,
1792
+ dependency_evaluation: dependency_evaluation,
1793
+ device_id: device_id)
1727
1794
  end
1728
1795
 
1729
1796
  group_name = snapshot[:group_type_mapping][aggregation_group_type_index.to_s.to_sym]
@@ -1739,17 +1806,24 @@ module PostHog
1739
1806
 
1740
1807
  group_name_symbol = group_name.to_sym
1741
1808
 
1742
- unless groups.key?(group_name_symbol)
1743
- # Group flags are never enabled if appropriate `groups` aren't passed in
1744
- # don't failover to `/flags/`, since response will be the same
1745
- logger.warn "[FEATURE FLAGS] Can't compute group feature flag: #{flag[:key]} without group names passed in"
1746
- return false
1809
+ # Without a usable group key, conditions aggregated on the group are skipped one by one below, so a
1810
+ # person condition can still match. The holdout runs first and buckets on the group key, so a holdout
1811
+ # that excludes anyone can't be resolved without one.
1812
+ if holdout_excludes_anyone?(flag_filters[:holdout]) &&
1813
+ !FeatureFlagsPoller.usable_group_key?(groups[group_name_symbol])
1814
+ reason = _definition_unresolved_reason(flag, snapshot) || UnresolvedFlagReason::MISSING_CONTEXT
1815
+ raise InconclusiveMatchError.new('Flag has a holdout bucketed on a group key that was not passed in',
1816
+ reason: reason)
1747
1817
  end
1748
1818
 
1749
1819
  focused_group_properties = group_properties[group_name_symbol]
1750
1820
  match_feature_flag_properties(flag, groups[group_name_symbol], focused_group_properties, evaluation_cache,
1751
1821
  snapshot[:cohorts], groups: groups, group_properties: group_properties,
1752
- person_properties: person_properties, snapshot: snapshot,
1822
+ snapshot: snapshot, known_absent: known_absent,
1823
+ known_absent_scope: known_absent&.group_scope(
1824
+ group_name, aggregation_group_type_index
1825
+ ), context: context,
1826
+ dependency_evaluation: dependency_evaluation,
1753
1827
  device_id: device_id)
1754
1828
  end
1755
1829
 
@@ -1766,12 +1840,13 @@ module PostHog
1766
1840
  response
1767
1841
  end
1768
1842
 
1769
- # person_properties defaults to properties, which hold the person's properties unless the flag is
1770
- # group-aggregated. Flag dependencies read feature enrollment from them. `device_id` is the resolved device
1771
- # id, or nil when none is usable.
1843
+ # known_absent_scope holds the listed keys for `properties`; known_absent holds every listing, so a
1844
+ # condition aggregated by another group type can use that group type's keys. `device_id` is the resolved
1845
+ # device id, or nil when none is usable.
1772
1846
  def match_feature_flag_properties(flag, distinct_id, properties, evaluation_cache, cohort_properties = {},
1773
- groups: {}, group_properties: {}, person_properties: properties,
1774
- snapshot: _evaluation_snapshot, device_id: nil, dependency_evaluation: nil)
1847
+ groups: {}, group_properties: {}, snapshot: _evaluation_snapshot,
1848
+ known_absent: nil, known_absent_scope: nil, context: nil,
1849
+ dependency_evaluation: nil, device_id: nil)
1775
1850
  dependency_evaluation ||= DependencyEvaluation.new(flag[:key])
1776
1851
  flag_filters = flag[:filters] || {}
1777
1852
  flag_aggregation = flag_filters[:aggregation_group_type_index]
@@ -1812,10 +1887,36 @@ module PostHog
1812
1887
 
1813
1888
  effective_properties = properties
1814
1889
  effective_bucketing = distinct_id
1815
- condition_distinct_id = distinct_id
1890
+ effective_known_absent_scope = known_absent_scope
1891
+
1892
+ if condition_aggregation.nil?
1893
+ # A person condition under a group flag uses the person, not the group the caller resolved.
1894
+ unless flag_aggregation.nil? || context.nil?
1895
+ effective_properties = person_properties_with_distinct_id(context)
1896
+ effective_bucketing = context[:distinct_id]
1897
+ effective_known_absent_scope = known_absent&.person_scope
1898
+ end
1899
+ else
1900
+ # A condition aggregated on a group without a usable key is skipped, and later conditions still run.
1901
+ group_name = snapshot[:group_type_mapping][condition_aggregation.to_s.to_sym]
1902
+ if group_name.nil? || !FeatureFlagsPoller.usable_group_key?(groups[group_name.to_sym])
1903
+ logger.debug do
1904
+ "[FEATURE FLAGS] Skipping group condition for flag '#{flag[:key]}': " \
1905
+ "group type index #{condition_aggregation} not available"
1906
+ end
1907
+ next
1908
+ end
1909
+ if condition_aggregation != flag_aggregation
1910
+ # Missing group properties don't skip the condition: each filter decides, as on the flags service.
1911
+ group_key = groups[group_name.to_sym]
1912
+ effective_properties = { '$group_key': group_key }.merge(group_properties[group_name.to_sym] || {})
1913
+ effective_bucketing = group_key
1914
+ effective_known_absent_scope = known_absent&.group_scope(group_name, condition_aggregation)
1915
+ end
1916
+ end
1816
1917
 
1918
+ condition_distinct_id = effective_bucketing
1817
1919
  missing_device_id = false
1818
-
1819
1920
  # A person condition of a device-bucketed flag hashes the device id. Without one, it is
1820
1921
  # inconclusive once its filters match, whatever its rollout or variants, rather than
1821
1922
  # bucketed on the distinct id.
@@ -1827,44 +1928,20 @@ module PostHog
1827
1928
  end
1828
1929
  end
1829
1930
 
1830
- # Mixed-override path: condition-level aggregation differs from flag-level.
1831
- # This assumes flag-level aggregation is nil for mixed flags.
1832
- if condition_aggregation != flag_aggregation
1833
- if condition_aggregation.nil?
1834
- # Person condition under a mixed flag — caller already passed person props/bucketing.
1835
- else
1836
- group_name = snapshot[:group_type_mapping][condition_aggregation.to_s.to_sym]
1837
- if group_name.nil? || !groups.key?(group_name.to_sym)
1838
- logger.debug do
1839
- "[FEATURE FLAGS] Skipping group condition for flag '#{flag[:key]}': " \
1840
- "group type index #{condition_aggregation} not available"
1841
- end
1842
- next
1843
- end
1844
- unless group_properties.key?(group_name.to_sym)
1845
- inconclusive_reason = FeatureFlagsPoller.pick_unresolved_reason(
1846
- inconclusive_reason, UnresolvedFlagReason::MISSING_CONTEXT
1847
- )
1848
- next
1849
- end
1850
- effective_properties = group_properties[group_name.to_sym]
1851
- effective_bucketing = groups[group_name.to_sym]
1852
- condition_distinct_id = effective_bucketing
1853
- end
1854
- end
1855
-
1856
1931
  outcome =
1857
- if device_bucketed || !device_id.nil?
1932
+ if device_bucketed
1858
1933
  condition_match_outcome(flag, condition_distinct_id, condition, effective_properties, evaluation_cache,
1859
- cohort_properties, person_properties: person_properties, snapshot: snapshot,
1860
- device_id: device_id, bucketing_value: effective_bucketing,
1934
+ cohort_properties, snapshot: snapshot, context: context,
1935
+ known_absent_scope: effective_known_absent_scope,
1936
+ bucketing_value: effective_bucketing,
1861
1937
  missing_device_id: missing_device_id,
1862
1938
  dependency_evaluation: dependency_evaluation)
1863
1939
  else
1864
1940
  # Without device bucketing the condition hashes its distinct id; skipping the extra keywords
1865
1941
  # keeps the common path as cheap as before.
1866
1942
  condition_match_outcome(flag, condition_distinct_id, condition, effective_properties, evaluation_cache,
1867
- cohort_properties, person_properties: person_properties, snapshot: snapshot,
1943
+ cohort_properties, snapshot: snapshot, context: context,
1944
+ known_absent_scope: effective_known_absent_scope,
1868
1945
  dependency_evaluation: dependency_evaluation)
1869
1946
  end
1870
1947
  case outcome
@@ -1906,9 +1983,17 @@ module PostHog
1906
1983
  false
1907
1984
  end
1908
1985
 
1986
+ def person_properties_with_distinct_id(context)
1987
+ person_properties = context[:person_properties] || {}
1988
+ return person_properties if person_properties.key?(:distinct_id) || person_properties.key?('distinct_id')
1989
+
1990
+ person_properties.merge(distinct_id: context[:distinct_id])
1991
+ end
1992
+
1993
+ # Called once evaluation is past the enrollment check, so enrollment never hides a release condition's cause.
1909
1994
  def _definition_unresolved_reason(flag, snapshot)
1910
1995
  self.class.definition_unresolved_reason(flag, snapshot[:flags_by_key] || {}, snapshot[:cohorts],
1911
- snapshot[:group_type_mapping])
1996
+ snapshot[:group_type_mapping], ignore_feature_enrollment: true)
1912
1997
  rescue StandardError
1913
1998
  # A malformed definition must not stop the fallback to the server.
1914
1999
  nil
@@ -1931,8 +2016,9 @@ module PostHog
1931
2016
  # `bucketing_value` is hashed for the rollout. `missing_device_id` marks a person condition of a
1932
2017
  # device-bucketed flag without a device id, so it is inconclusive once its filters match.
1933
2018
  def condition_match_outcome(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties = {},
1934
- person_properties: properties, snapshot: _evaluation_snapshot, device_id: nil,
1935
- bucketing_value: distinct_id, missing_device_id: false, dependency_evaluation: nil)
2019
+ snapshot: _evaluation_snapshot, known_absent_scope: nil, context: nil,
2020
+ bucketing_value: distinct_id, missing_device_id: false,
2021
+ dependency_evaluation: nil)
1936
2022
  rollout_percentage = condition[:rollout_percentage]
1937
2023
 
1938
2024
  unless (condition[:properties] || []).empty?
@@ -1944,11 +2030,12 @@ module PostHog
1944
2030
  matches =
1945
2031
  if prop[:type] == 'flag'
1946
2032
  evaluate_flag_dependency(prop, evaluation_cache, distinct_id, properties, cohort_properties,
1947
- person_properties: person_properties, snapshot: snapshot,
1948
- device_id: device_id, dependency_evaluation: dependency_evaluation)
2033
+ snapshot: snapshot, context: context,
2034
+ dependency_evaluation: dependency_evaluation)
1949
2035
  else
1950
2036
  FeatureFlagsPoller.match_property(prop, properties, cohort_properties,
1951
- property_matching_version: snapshot[:property_matching_version])
2037
+ property_matching_version: snapshot[:property_matching_version],
2038
+ known_absent_scope: known_absent_scope)
1952
2039
  end
1953
2040
  return :no_match unless matches
1954
2041
  rescue RequiresServerEvaluation => e
@@ -2005,16 +2092,11 @@ module PostHog
2005
2092
  # no usable holdout or the identifier falls outside it, leaving ordinary evaluation
2006
2093
  # unchanged.
2007
2094
  def holdout_variant_for(holdout, bucketing_value, missing_device_id: false)
2008
- return nil unless holdout.is_a?(Hash)
2095
+ return nil if holdout.nil?
2009
2096
 
2010
- id = holdout[:id]
2011
- exclusion_percentage = holdout[:exclusion_percentage]
2012
- return nil if id.nil? || !exclusion_percentage.is_a?(Numeric)
2097
+ percentage = holdout_exclusion_percentage(holdout)
2098
+ return nil if percentage.nil?
2013
2099
 
2014
- # The server clamps an out-of-range percentage rather than rejecting it, and treats
2015
- # 100 as everyone without hashing, so a full holdout can't miss on a hash boundary.
2016
- # Fractional percentages are kept as-is.
2017
- percentage = exclusion_percentage.to_f.clamp(0, 100)
2018
2100
  if missing_device_id
2019
2101
  # A device-bucketed person flag without a device id: hashing another identity would
2020
2102
  # hold out the wrong population. Even 100% is inconclusive, as for a missing group key.
@@ -2023,9 +2105,26 @@ module PostHog
2023
2105
  raise InconclusiveMatchError.new("Can't evaluate a device-bucketed holdout without a device id",
2024
2106
  reason: UnresolvedFlagReason::MISSING_CONTEXT)
2025
2107
  end
2108
+ # The server treats 100 as everyone without hashing, so a full holdout can't miss on a hash boundary.
2026
2109
  return nil if percentage < 100 && _holdout_hash(bucketing_value) > (percentage / 100)
2027
2110
 
2028
- "holdout-#{id}"
2111
+ "holdout-#{holdout[:id]}"
2112
+ end
2113
+
2114
+ def holdout_excludes_anyone?(holdout)
2115
+ percentage = holdout_exclusion_percentage(holdout)
2116
+ !percentage.nil? && percentage.positive?
2117
+ end
2118
+
2119
+ # The exclusion percentage of a usable holdout, or nil. The server clamps an out-of-range percentage
2120
+ # rather than rejecting it. Fractional percentages are kept as-is.
2121
+ def holdout_exclusion_percentage(holdout)
2122
+ return nil unless holdout.is_a?(Hash)
2123
+
2124
+ exclusion_percentage = holdout[:exclusion_percentage]
2125
+ return nil if holdout[:id].nil? || !exclusion_percentage.is_a?(Numeric)
2126
+
2127
+ exclusion_percentage.to_f.clamp(0, 100)
2029
2128
  end
2030
2129
 
2031
2130
  # Holdout membership hashes `holdout-<bucketing value>` with no separator, salt, flag
@@ -2269,6 +2368,20 @@ module PostHog
2269
2368
  _request(uri, req, nil, include_etag: true)
2270
2369
  end
2271
2370
 
2371
+ # JSON has no NaN or infinity, so a non-finite group key would fail the whole request. It counts as no key
2372
+ # locally, so it's left out of the request too.
2373
+ def without_non_finite_group_keys(groups, group_properties)
2374
+ non_finite = groups&.select { |_name, key| key.is_a?(Float) && !key.finite? }
2375
+ return [groups, group_properties] if non_finite.nil? || non_finite.empty?
2376
+
2377
+ group_properties = group_properties.to_h do |name, properties|
2378
+ next [name, properties] unless non_finite.key?(name) && properties.is_a?(Hash)
2379
+
2380
+ [name, properties.reject { |key, value| key.to_s == '$group_key' && value.equal?(non_finite[name]) }]
2381
+ end
2382
+ [groups.except(*non_finite.keys), group_properties]
2383
+ end
2384
+
2272
2385
  def _request_feature_flag_evaluation(data = {})
2273
2386
  uri = URI("#{@host}/flags/?v=2")
2274
2387
  req = Net::HTTP::Post.new(uri)