posthog-ruby 3.39.0 → 3.40.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d310b8c92f6b3933968050b20012090e3c1ac220346762dc9cf051ab9d82bd8
4
- data.tar.gz: 50fca2bf0450dd266a6a6f516bbfef5297b4bac6e7228b2e26faf6a6771d49bc
3
+ metadata.gz: 16949639bc7e275d0e914a05589108fac9d8c19a41b54e97e5aac196f6d75132
4
+ data.tar.gz: f1e09622f31b1fa977dc2dcf8385125984fbbc3fcd218012c3cf41cbad4157eb
5
5
  SHA512:
6
- metadata.gz: cb6b5d4ce868ae8eb338ce627d7b41c46c121b1cd4b0a41a6367c2b80aa727a085cbb9a5a99bfd2cd3f6c666ccd94c902c12d8a580b0827fbf9d13ab6a67bd44
7
- data.tar.gz: 65978c26cfcf66fa940e13693f8f3a2ecc6831a4f1d0be44a2724e9a4dfccab20b1384a267f7f7c5f49550ecac0a4c6396605cf699a144c47afdde80d9bfdad9
6
+ metadata.gz: 277c507553f1fa823ff53030faff2d235877289409d728e96357265d33a2165862d7e604188ab5b610e7dce0907c8998c0a0dd8422e464feb28b32930375d9da
7
+ data.tar.gz: b6875faf06f12a2b1e012be6ad49ed185b7f5bcc78b2877d5d24c10e3831bdedbdd8d97a8dd728f6e6f103249a5d04560f07cb95574a67b00cfd339ea4c9ba28
@@ -531,6 +531,8 @@ module PostHog
531
531
  # @param group_properties [Hash] Properties to use when evaluating groups locally or remotely.
532
532
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
533
533
  # @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
534
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
535
+ # person property. An explicit device id is sent to /flags.
534
536
  # @return [Boolean, nil] Whether the flag is enabled, or nil when the flag could not be evaluated.
535
537
  # TODO: In future version, rename to `feature_flag_enabled?`
536
538
  def is_feature_enabled( # rubocop:disable Naming/PredicatePrefix
@@ -540,7 +542,8 @@ module PostHog
540
542
  person_properties: {},
541
543
  group_properties: {},
542
544
  only_evaluate_locally: false,
543
- send_feature_flag_events: true
545
+ send_feature_flag_events: true,
546
+ device_id: nil
544
547
  )
545
548
  _emit_deprecation(
546
549
  :is_feature_enabled,
@@ -553,7 +556,8 @@ module PostHog
553
556
  result = _get_feature_flag_result(
554
557
  flag_key, distinct_id,
555
558
  groups: groups, person_properties: person_properties, group_properties: group_properties,
556
- only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events
559
+ only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events,
560
+ device_id: device_id
557
561
  )
558
562
  response = result&.value
559
563
  return nil if response.nil?
@@ -578,6 +582,8 @@ module PostHog
578
582
  # @param [Hash] group_properties
579
583
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
580
584
  # @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
585
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
586
+ # person property. An explicit device id is sent to /flags.
581
587
  #
582
588
  # @return [String, Boolean, nil] The value of the feature flag
583
589
  #
@@ -600,7 +606,8 @@ module PostHog
600
606
  person_properties: {},
601
607
  group_properties: {},
602
608
  only_evaluate_locally: false,
603
- send_feature_flag_events: true
609
+ send_feature_flag_events: true,
610
+ device_id: nil
604
611
  )
605
612
  _emit_deprecation(
606
613
  :get_feature_flag,
@@ -612,7 +619,8 @@ module PostHog
612
619
  result = _get_feature_flag_result(
613
620
  key, distinct_id,
614
621
  groups: groups, person_properties: person_properties, group_properties: group_properties,
615
- only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events
622
+ only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events,
623
+ device_id: device_id
616
624
  )
617
625
  result&.value
618
626
  end
@@ -626,6 +634,8 @@ module PostHog
626
634
  # @param group_properties [Hash] Properties to use when evaluating groups locally or remotely.
627
635
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
628
636
  # @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
637
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
638
+ # person property. An explicit device id is sent to /flags.
629
639
  # @return [PostHog::FeatureFlagResult, nil]
630
640
  def get_feature_flag_result(
631
641
  key,
@@ -634,7 +644,8 @@ module PostHog
634
644
  person_properties: {},
635
645
  group_properties: {},
636
646
  only_evaluate_locally: false,
637
- send_feature_flag_events: true
647
+ send_feature_flag_events: true,
648
+ device_id: nil
638
649
  )
639
650
  _emit_deprecation(
640
651
  :get_feature_flag_result,
@@ -648,7 +659,8 @@ module PostHog
648
659
  _get_feature_flag_result(
649
660
  key, distinct_id,
650
661
  groups: groups, person_properties: person_properties, group_properties: group_properties,
651
- only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events
662
+ only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events,
663
+ device_id: device_id
652
664
  )
653
665
  end
654
666
 
@@ -671,6 +683,8 @@ module PostHog
671
683
  # @param [Array<String, Symbol>, nil] flag_keys When set, scopes evaluation to only these flag keys.
672
684
  # An empty array returns an empty snapshot without evaluating flags; +nil+ evaluates all flags.
673
685
  # Distinct from {FeatureFlagEvaluations#only}, which filters the already-fetched snapshot in memory.
686
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
687
+ # person property. An explicit device id is sent to /flags.
674
688
  # @return [PostHog::FeatureFlagEvaluations]
675
689
  def evaluate_flags(
676
690
  distinct_id,
@@ -679,7 +693,8 @@ module PostHog
679
693
  group_properties: {},
680
694
  only_evaluate_locally: false,
681
695
  disable_geoip: nil,
682
- flag_keys: nil
696
+ flag_keys: nil,
697
+ device_id: nil
683
698
  )
684
699
  host = _feature_flag_evaluations_host
685
700
 
@@ -714,7 +729,8 @@ module PostHog
714
729
  begin
715
730
  match = @feature_flags_poller.send(
716
731
  :_compute_flag_locally,
717
- definition, distinct_id, groups, person_properties, group_properties, snapshot: definition_snapshot
732
+ definition, distinct_id, groups, person_properties, group_properties,
733
+ snapshot: definition_snapshot, device_id: device_id
718
734
  )
719
735
  rescue PostHog::RequiresServerEvaluation, PostHog::InconclusiveMatchError => e
720
736
  unresolved_flags[key.to_s] = e.reason
@@ -765,7 +781,8 @@ module PostHog
765
781
  minimal_flag_called_events = false
766
782
  begin
767
783
  flags_response = @feature_flags_poller.get_flags(
768
- distinct_id, groups, person_properties, group_properties, flag_keys, disable_geoip
784
+ distinct_id, groups, person_properties, group_properties, flag_keys, disable_geoip,
785
+ device_id: device_id
769
786
  )
770
787
  minimal_flag_called_events = flags_response[:minimalFlagCalledEvents] == true
771
788
  request_id = flags_response[:requestId]
@@ -822,6 +839,8 @@ module PostHog
822
839
  # @param [Hash] person_properties key-value pairs of properties to associate with the user.
823
840
  # @param [Hash] group_properties
824
841
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
842
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
843
+ # person property. An explicit device id is sent to /flags.
825
844
  #
826
845
  # @return [Hash] String (not symbol) key value pairs of flag and their values
827
846
  def get_all_flags(
@@ -829,7 +848,8 @@ module PostHog
829
848
  groups: {},
830
849
  person_properties: {},
831
850
  group_properties: {},
832
- only_evaluate_locally: false
851
+ only_evaluate_locally: false,
852
+ device_id: nil
833
853
  )
834
854
  return {} if @disabled
835
855
 
@@ -837,7 +857,7 @@ module PostHog
837
857
  groups, person_properties, group_properties
838
858
  )
839
859
  @feature_flags_poller.get_all_flags(distinct_id, groups, person_properties, group_properties,
840
- only_evaluate_locally)
860
+ only_evaluate_locally, device_id: device_id)
841
861
  end
842
862
 
843
863
  # Returns payload for a given feature flag
@@ -852,6 +872,8 @@ module PostHog
852
872
  # @param person_properties [Hash] key-value pairs of properties to associate with the user.
853
873
  # @param group_properties [Hash]
854
874
  # @param only_evaluate_locally [Boolean]
875
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
876
+ # person property. An explicit device id is sent to /flags.
855
877
  # @return [Object, nil] The parsed payload for the matched flag value.
856
878
  #
857
879
  # @deprecated Use {#evaluate_flags} and {FeatureFlagEvaluations#get_flag_payload} instead.
@@ -862,7 +884,8 @@ module PostHog
862
884
  groups: {},
863
885
  person_properties: {},
864
886
  group_properties: {},
865
- only_evaluate_locally: false
887
+ only_evaluate_locally: false,
888
+ device_id: nil
866
889
  )
867
890
  _emit_deprecation(
868
891
  :get_feature_flag_payload,
@@ -878,7 +901,7 @@ module PostHog
878
901
  groups, person_properties, group_properties
879
902
  )
880
903
  @feature_flags_poller.get_feature_flag_payload(key, distinct_id, match_value, groups, person_properties,
881
- group_properties, only_evaluate_locally)
904
+ group_properties, only_evaluate_locally, device_id: device_id)
882
905
  end
883
906
 
884
907
  # Returns all flags and payloads for a given user
@@ -892,13 +915,16 @@ module PostHog
892
915
  # @param person_properties [Hash] key-value pairs of properties to associate with the user.
893
916
  # @param group_properties [Hash]
894
917
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
918
+ # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
919
+ # person property. An explicit device id is sent to /flags.
895
920
  #
896
921
  def get_all_flags_and_payloads(
897
922
  distinct_id,
898
923
  groups: {},
899
924
  person_properties: {},
900
925
  group_properties: {},
901
- only_evaluate_locally: false
926
+ only_evaluate_locally: false,
927
+ device_id: nil
902
928
  )
903
929
  return { featureFlags: {}, featureFlagPayloads: {} } if @disabled
904
930
 
@@ -906,7 +932,7 @@ module PostHog
906
932
  groups, person_properties, group_properties
907
933
  )
908
934
  response = @feature_flags_poller.get_all_flags_and_payloads(
909
- distinct_id, groups, person_properties, group_properties, only_evaluate_locally
935
+ distinct_id, groups, person_properties, group_properties, only_evaluate_locally, device_id: device_id
910
936
  )
911
937
 
912
938
  # Remove internal information
@@ -1152,7 +1178,8 @@ module PostHog
1152
1178
  person_properties: {},
1153
1179
  group_properties: {},
1154
1180
  only_evaluate_locally: false,
1155
- send_feature_flag_events: true
1181
+ send_feature_flag_events: true,
1182
+ device_id: nil
1156
1183
  )
1157
1184
  return nil if @disabled
1158
1185
 
@@ -1163,7 +1190,8 @@ module PostHog
1163
1190
  feature_flag_response, flag_was_locally_evaluated, request_id, evaluated_at, feature_flag_error, payload,
1164
1191
  has_experiment, minimal_flag_called_events =
1165
1192
  @feature_flags_poller.get_feature_flag(
1166
- key, distinct_id, groups, person_properties, group_properties, only_evaluate_locally
1193
+ key, distinct_id, groups, person_properties, group_properties, only_evaluate_locally,
1194
+ device_id: device_id
1167
1195
  )
1168
1196
  if send_feature_flag_events
1169
1197
  properties = {
@@ -182,13 +182,15 @@ module PostHog
182
182
  groups = {},
183
183
  person_properties = {},
184
184
  group_properties = {},
185
- _only_evaluate_locally = false
185
+ _only_evaluate_locally = false,
186
+ device_id: nil
186
187
  )
187
188
  flags_data = get_all_flags_and_payloads(
188
189
  distinct_id,
189
190
  groups,
190
191
  person_properties,
191
- group_properties
192
+ group_properties,
193
+ device_id: device_id
192
194
  )
193
195
 
194
196
  if flags_data.key?(:featureFlagPayloads)
@@ -200,7 +202,7 @@ module PostHog
200
202
  end
201
203
 
202
204
  def get_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, flag_keys = nil,
203
- disable_geoip = nil)
205
+ disable_geoip = nil, device_id: nil)
204
206
  flag_keys = flag_keys.map(&:to_s) if flag_keys.is_a?(Array)
205
207
  request_data = {
206
208
  distinct_id: distinct_id,
@@ -208,6 +210,8 @@ module PostHog
208
210
  person_properties: person_properties,
209
211
  group_properties: group_properties
210
212
  }
213
+ device_id = FeatureFlagsPoller.usable_device_id(device_id)
214
+ request_data[:device_id] = device_id unless device_id.nil?
211
215
  request_data[:flag_keys_to_evaluate] = flag_keys if flag_keys && !flag_keys.empty?
212
216
  # The flags endpoint treats an omitted/null value as false; serialize that same default explicitly.
213
217
  request_data[:geoip_disable] = disable_geoip.nil? ? false : disable_geoip
@@ -248,7 +252,8 @@ module PostHog
248
252
  person_properties = {},
249
253
  group_properties = {},
250
254
  only_evaluate_locally = false,
251
- snapshot: nil
255
+ snapshot: nil,
256
+ device_id: nil
252
257
  )
253
258
  key = key.to_s
254
259
 
@@ -263,7 +268,7 @@ module PostHog
263
268
  unless feature_flag.nil?
264
269
  begin
265
270
  response = _compute_flag_locally(feature_flag, distinct_id, groups, person_properties, group_properties,
266
- snapshot: snapshot)
271
+ snapshot: snapshot, device_id: device_id)
267
272
  payload = _compute_flag_payload_locally(key, response, snapshot: snapshot) unless response.nil?
268
273
  logger.debug "Successfully computed flag locally: #{key} -> #{response}"
269
274
  rescue RequiresServerEvaluation, InconclusiveMatchError => e
@@ -293,7 +298,7 @@ module PostHog
293
298
  begin
294
299
  errors = []
295
300
  flags_data = get_all_flags_and_payloads(distinct_id, groups, person_properties, group_properties,
296
- only_evaluate_locally, true)
301
+ only_evaluate_locally, true, device_id: device_id)
297
302
  if flags_data.key?(:featureFlags)
298
303
  flags = stringify_keys(flags_data[:featureFlags] || {})
299
304
  payloads = stringify_keys(flags_data[:featureFlagPayloads] || {})
@@ -347,7 +352,8 @@ module PostHog
347
352
  groups = {},
348
353
  person_properties = {},
349
354
  group_properties = {},
350
- only_evaluate_locally = false
355
+ only_evaluate_locally = false,
356
+ device_id: nil
351
357
  )
352
358
  if @quota_limited.true?
353
359
  logger.debug 'Not fetching flags from server - quota limited'
@@ -360,7 +366,8 @@ module PostHog
360
366
  groups,
361
367
  person_properties,
362
368
  group_properties,
363
- only_evaluate_locally
369
+ only_evaluate_locally,
370
+ device_id: device_id
364
371
  )
365
372
 
366
373
  response[:featureFlags]
@@ -372,7 +379,8 @@ module PostHog
372
379
  person_properties = {},
373
380
  group_properties = {},
374
381
  only_evaluate_locally = false,
375
- raise_on_error = false
382
+ raise_on_error = false,
383
+ device_id: nil
376
384
  )
377
385
  load_feature_flags
378
386
  snapshot = _evaluation_snapshot
@@ -385,7 +393,7 @@ module PostHog
385
393
 
386
394
  snapshot[:flags].each do |flag|
387
395
  match_value = _compute_flag_locally(flag, distinct_id, groups, person_properties, group_properties,
388
- snapshot: snapshot)
396
+ snapshot: snapshot, device_id: device_id)
389
397
  flags[flag[:key]] = match_value
390
398
  match_payload = _compute_flag_payload_locally(flag[:key], match_value, snapshot: snapshot)
391
399
  payloads[flag[:key]] = match_payload if match_payload
@@ -404,7 +412,8 @@ module PostHog
404
412
 
405
413
  if fallback_to_server && !only_evaluate_locally
406
414
  begin
407
- flags_and_payloads = get_flags(distinct_id, groups, person_properties, group_properties)
415
+ flags_and_payloads = get_flags(distinct_id, groups, person_properties, group_properties,
416
+ device_id: device_id)
408
417
  flag_details = flags_and_payloads[:flags]
409
418
  errors_while_computing = flags_and_payloads[:errorsWhileComputingFlags] || false
410
419
  quota_limited = flags_and_payloads[:quotaLimited]
@@ -467,7 +476,8 @@ module PostHog
467
476
  groups = {},
468
477
  person_properties = {},
469
478
  group_properties = {},
470
- only_evaluate_locally = false
479
+ only_evaluate_locally = false,
480
+ device_id: nil
471
481
  )
472
482
  key = key.to_s
473
483
  load_feature_flags
@@ -481,13 +491,15 @@ module PostHog
481
491
  person_properties,
482
492
  group_properties,
483
493
  true,
484
- snapshot: snapshot
494
+ snapshot: snapshot,
495
+ device_id: device_id
485
496
  )[0]
486
497
  end
487
498
  response = nil
488
499
  response = _compute_flag_payload_locally(key, match_value, snapshot: snapshot) unless match_value.nil?
489
500
  if response.nil? && !only_evaluate_locally
490
- flags_payloads = get_feature_payloads(distinct_id, groups, person_properties, group_properties)
501
+ flags_payloads = get_feature_payloads(distinct_id, groups, person_properties, group_properties,
502
+ device_id: device_id)
491
503
  response = flags_payloads[key.downcase] || nil
492
504
  end
493
505
  response
@@ -506,6 +518,19 @@ module PostHog
506
518
 
507
519
  # Class methods
508
520
 
521
+ # A device id is usable only as a non-empty string; anything else counts as missing.
522
+ def self.usable_device_id(device_id)
523
+ device_id.is_a?(String) && !device_id.empty? ? device_id : nil
524
+ end
525
+
526
+ # Only the exact value "device_id" buckets person conditions on the device id.
527
+ def self.device_bucketed?(flag, flag_filters = flag[:filters] || {})
528
+ identifier = flag[:bucketing_identifier]
529
+ # A malformed flag fails when it is evaluated, not while the definitions load.
530
+ identifier ||= flag_filters[:bucketing_identifier] if flag_filters.is_a?(Hash)
531
+ identifier == 'device_id'
532
+ end
533
+
509
534
  def self.pick_unresolved_reason(current, reason)
510
535
  return reason if current.nil?
511
536
 
@@ -1252,7 +1277,7 @@ module PostHog
1252
1277
  # @return [Boolean] True if the referenced flag's value matches property[:value], false otherwise
1253
1278
  def evaluate_flag_dependency(property, evaluation_cache, distinct_id, properties, cohort_properties,
1254
1279
  person_properties: properties, snapshot: _evaluation_snapshot,
1255
- dependency_evaluation: nil)
1280
+ device_id: nil, dependency_evaluation: nil)
1256
1281
  dependency_evaluation ||= DependencyEvaluation.new
1257
1282
  if snapshot[:flags_by_key].nil? || evaluation_cache.nil?
1258
1283
  # Cannot evaluate flag dependencies without required context
@@ -1279,7 +1304,7 @@ module PostHog
1279
1304
  else
1280
1305
  evaluate_flag_dependency_value(dep_flag_key, evaluation_cache, distinct_id, properties,
1281
1306
  cohort_properties, person_properties, snapshot,
1282
- dependency_evaluation)
1307
+ dependency_evaluation, device_id: device_id)
1283
1308
  end
1284
1309
 
1285
1310
  self.class.matches_dependency_value(property[:value], actual_value)
@@ -1300,7 +1325,7 @@ module PostHog
1300
1325
  end
1301
1326
 
1302
1327
  def evaluate_flag_dependency_value(dep_flag_key, evaluation_cache, distinct_id, properties, cohort_properties,
1303
- person_properties, snapshot, dependency_evaluation)
1328
+ person_properties, snapshot, dependency_evaluation, device_id: nil)
1304
1329
  dep_flag = snapshot[:flags_by_key][dep_flag_key]
1305
1330
  # Same check as in _compute_flag_locally.
1306
1331
  return evaluation_cache[dep_flag_key] = false unless dep_flag[:active]
@@ -1314,28 +1339,65 @@ module PostHog
1314
1339
  end
1315
1340
 
1316
1341
  evaluate_uncached_flag_dependency(dep_flag_key, dep_flag, evaluation_cache, distinct_id, properties,
1317
- cohort_properties, person_properties, snapshot, dependency_evaluation)
1342
+ cohort_properties, person_properties, snapshot, dependency_evaluation,
1343
+ device_id: device_id)
1318
1344
  end
1319
1345
 
1320
1346
  # Per-evaluation bookkeeping for flag dependencies, kept apart from the result cache. A cycle depends on the
1321
- # path to a flag, so the flags on it are not cached. A result computed while the depth limit or budget was hit
1322
- # below it holds only on paths at least as deep, so it is depth-tagged instead of cached: it is reused only at
1323
- # the same depth or deeper while the cache has not grown, so each flag is evaluated at most once per depth and
1324
- # cache size.
1347
+ # path to a flag, so the flags on it are not cached. A result computed while the depth limit or budget cut
1348
+ # something off below it is kept as a depth-limited result instead of cached, with the depth and resolution
1349
+ # version it was computed at. Reusing a failure at the same or deeper depth is a conservative approximation that
1350
+ # keeps the work linear: a deeper cut-off could hide what failed, so it stays inconclusive and defers to the
1351
+ # server. A value is reused only at exactly the same depth, where re-evaluation would give the same result, so
1352
+ # values are kept per depth and a failure keeps the shallowest depth it was seen at. Both hold only until a key
1353
+ # that was cut off or has a depth-limited result is resolved, which bumps the resolution version.
1325
1354
  class DependencyEvaluation
1326
- attr_reader :in_progress, :depth_tagged
1355
+ attr_reader :in_progress, :resolution_version
1327
1356
  attr_accessor :evaluations, :cycles, :depth_limits
1328
1357
 
1329
1358
  def initialize(flag_key = nil)
1330
1359
  @path = flag_key.nil? ? [] : [flag_key]
1331
1360
  @in_progress = Set.new(@path)
1332
1361
  @cycle_edges = Set.new
1333
- @depth_tagged = {}
1362
+ # Allocated on first use: most evaluations never hit the depth limit or budget.
1363
+ @depth_limited_values = nil
1364
+ @depth_limited_failures = nil
1365
+ @depth_limited_keys = nil
1366
+ @resolution_version = 0
1334
1367
  @evaluations = 0
1335
1368
  @cycles = 0
1336
1369
  @depth_limits = 0
1337
1370
  end
1338
1371
 
1372
+ def store_depth_limited_value(flag_key, depth, value, resolution_version)
1373
+ (@depth_limited_values ||= {})[[flag_key, depth]] = { value: value, resolution_version: resolution_version }
1374
+ add_depth_limited_key(flag_key)
1375
+ end
1376
+
1377
+ def store_depth_limited_failure(flag_key, depth, error, resolution_version)
1378
+ @depth_limited_failures ||= {}
1379
+ stored = @depth_limited_failures[flag_key]
1380
+ unless !stored.nil? && stored[:resolution_version] == resolution_version && stored[:depth] <= depth
1381
+ @depth_limited_failures[flag_key] = { error: error, depth: depth, resolution_version: resolution_version }
1382
+ end
1383
+ add_depth_limited_key(flag_key)
1384
+ end
1385
+
1386
+ # The stored value from exactly this depth, or else the stored failure from this depth or shallower, while
1387
+ # the resolution version is unchanged.
1388
+ def depth_limited_result(flag_key, depth)
1389
+ value = @depth_limited_values&.[]([flag_key, depth])
1390
+ return value if !value.nil? && value[:resolution_version] == @resolution_version
1391
+
1392
+ failure = @depth_limited_failures&.[](flag_key)
1393
+ failure if !failure.nil? && failure[:resolution_version] == @resolution_version && failure[:depth] <= depth
1394
+ end
1395
+
1396
+ # Called when a flag's result is cached. Results that a cut-off of this flag limited may now differ.
1397
+ def resolved!(flag_key)
1398
+ @resolution_version += 1 if @depth_limited_keys&.delete?(flag_key)
1399
+ end
1400
+
1339
1401
  # The flag whose conditions are being evaluated, or nil before the first one is entered.
1340
1402
  def current_flag
1341
1403
  @path.last
@@ -1363,47 +1425,67 @@ module PostHog
1363
1425
  @cycle_edges.include?([parent_key, flag_key])
1364
1426
  end
1365
1427
 
1366
- def depth_limit!(message)
1428
+ def depth_limit!(flag_key, message)
1367
1429
  @depth_limits += 1
1430
+ add_depth_limited_key(flag_key)
1368
1431
  raise InconclusiveMatchError.new(message, reason: UnresolvedFlagReason::UNRESOLVED_DEPENDENCY)
1369
1432
  end
1433
+
1434
+ private
1435
+
1436
+ def add_depth_limited_key(flag_key)
1437
+ (@depth_limited_keys ||= Set.new).add(flag_key)
1438
+ end
1370
1439
  end
1371
1440
 
1372
1441
  def evaluate_uncached_flag_dependency(dep_flag_key, dep_flag, evaluation_cache, distinct_id, properties,
1373
- cohort_properties, person_properties, snapshot, dependency_evaluation)
1442
+ cohort_properties, person_properties, snapshot, dependency_evaluation,
1443
+ device_id: nil)
1374
1444
  dependency_evaluation.cycle!(dep_flag_key) if dependency_evaluation.in_progress.include?(dep_flag_key)
1375
- # The top-level flag is in progress too, so the depth is one less than the number of flags in progress.
1376
- entry_depth = dependency_evaluation.in_progress.size
1377
- tagged = dependency_evaluation.depth_tagged[dep_flag_key]
1378
- if !tagged.nil? && entry_depth >= tagged[:depth] && evaluation_cache.size == tagged[:cache_size]
1379
- # The reused result is depth-tagged, so the flags that depend on it are too.
1380
- dependency_evaluation.depth_limits += 1
1381
- raise tagged[:error] if tagged.key?(:error)
1382
-
1383
- return tagged[:value]
1384
- end
1385
1445
  if dependency_evaluation.evaluations >= MAX_FLAG_DEPENDENCY_EVALUATIONS
1386
- dependency_evaluation.depth_limit!("Evaluated more than #{MAX_FLAG_DEPENDENCY_EVALUATIONS} flag dependencies")
1446
+ dependency_evaluation.depth_limit!(dep_flag_key,
1447
+ "Evaluated more than #{MAX_FLAG_DEPENDENCY_EVALUATIONS} flag dependencies")
1387
1448
  end
1449
+ # The top-level flag is in progress too, so the depth is one less than the number of flags in progress.
1450
+ entry_depth = dependency_evaluation.in_progress.size
1388
1451
  # Deeper chains are inconclusive, so a long chain can't exhaust the stack.
1389
1452
  if entry_depth > MAX_FLAG_DEPENDENCY_DEPTH
1390
1453
  dependency_evaluation.depth_limit!(
1391
- "Flag dependency chain for '#{dep_flag_key}' is deeper than #{MAX_FLAG_DEPENDENCY_DEPTH}"
1454
+ dep_flag_key, "Flag dependency chain for '#{dep_flag_key}' is deeper than #{MAX_FLAG_DEPENDENCY_DEPTH}"
1392
1455
  )
1393
1456
  end
1457
+ stored = dependency_evaluation.depth_limited_result(dep_flag_key, entry_depth)
1458
+ unless stored.nil?
1459
+ # The reused result is depth-limited, so the flags that depend on it are too.
1460
+ dependency_evaluation.depth_limits += 1
1461
+ raise stored[:error] if stored.key?(:error)
1462
+
1463
+ return stored[:value]
1464
+ end
1394
1465
 
1395
1466
  dependency_evaluation.evaluations += 1
1396
1467
  cycles = dependency_evaluation.cycles
1397
1468
  depth_limits = dependency_evaluation.depth_limits
1469
+ resolution_version = dependency_evaluation.resolution_version
1398
1470
  parent_key = dependency_evaluation.current_flag
1399
1471
  # Whether a result can be cached: not when a cycle or the depth limit or budget was hit below it. A cycle's
1400
- # result is dropped; a depth-limited one is depth-tagged with the depth and cache size it was computed at.
1472
+ # result is dropped; a depth-limited one is stored with the depth and the resolution version from before
1473
+ # the flag was evaluated, so a key its own conditions resolved still invalidates it.
1401
1474
  cacheable = lambda do |outcome|
1402
1475
  next false unless dependency_evaluation.cycles == cycles
1403
- next true if dependency_evaluation.depth_limits == depth_limits
1404
1476
 
1405
- dependency_evaluation.depth_tagged[dep_flag_key] = outcome.merge(depth: entry_depth,
1406
- cache_size: evaluation_cache.size)
1477
+ if dependency_evaluation.depth_limits == depth_limits
1478
+ dependency_evaluation.resolved!(dep_flag_key)
1479
+ next true
1480
+ end
1481
+
1482
+ if outcome.key?(:error)
1483
+ dependency_evaluation.store_depth_limited_failure(dep_flag_key, entry_depth, outcome[:error],
1484
+ resolution_version)
1485
+ else
1486
+ dependency_evaluation.store_depth_limited_value(dep_flag_key, entry_depth, outcome[:value],
1487
+ resolution_version)
1488
+ end
1407
1489
  false
1408
1490
  end
1409
1491
  dependency_evaluation.enter(dep_flag_key)
@@ -1413,7 +1495,7 @@ module PostHog
1413
1495
  else
1414
1496
  match_feature_flag_properties(
1415
1497
  dep_flag, distinct_id, properties, evaluation_cache, cohort_properties,
1416
- person_properties: person_properties, snapshot: snapshot,
1498
+ person_properties: person_properties, snapshot: snapshot, device_id: device_id,
1417
1499
  dependency_evaluation: dependency_evaluation
1418
1500
  )
1419
1501
  end
@@ -1603,7 +1685,7 @@ module PostHog
1603
1685
  end
1604
1686
 
1605
1687
  def _compute_flag_locally(flag, distinct_id, groups = {}, person_properties = {}, group_properties = {},
1606
- snapshot: _evaluation_snapshot)
1688
+ snapshot: _evaluation_snapshot, device_id: nil)
1607
1689
  # An inactive flag is false whatever its experience continuity setting.
1608
1690
  return false unless flag[:active]
1609
1691
 
@@ -1620,6 +1702,16 @@ module PostHog
1620
1702
  evaluation_cache = {}
1621
1703
 
1622
1704
  flag_filters = flag[:filters] || {}
1705
+ # An explicit device id wins; otherwise use the `$device_id` person property, as /flags does.
1706
+ # Without a device-bucketed flag in the definitions (dependencies included), no flag reads it.
1707
+ device_id = if snapshot[:has_device_bucketed_flags] == false
1708
+ nil
1709
+ else
1710
+ FeatureFlagsPoller.usable_device_id(device_id) ||
1711
+ FeatureFlagsPoller.usable_device_id(
1712
+ person_properties&.fetch(:$device_id, nil) || person_properties&.fetch('$device_id', nil)
1713
+ )
1714
+ end
1623
1715
 
1624
1716
  aggregation_group_type_index = flag_filters[:aggregation_group_type_index]
1625
1717
  if aggregation_group_type_index.nil?
@@ -1631,7 +1723,7 @@ module PostHog
1631
1723
  return match_feature_flag_properties(flag, distinct_id, local_person_properties, evaluation_cache,
1632
1724
  snapshot[:cohorts], groups: groups, group_properties: group_properties,
1633
1725
  person_properties: person_properties,
1634
- snapshot: snapshot)
1726
+ snapshot: snapshot, device_id: device_id)
1635
1727
  end
1636
1728
 
1637
1729
  group_name = snapshot[:group_type_mapping][aggregation_group_type_index.to_s.to_sym]
@@ -1657,7 +1749,8 @@ module PostHog
1657
1749
  focused_group_properties = group_properties[group_name_symbol]
1658
1750
  match_feature_flag_properties(flag, groups[group_name_symbol], focused_group_properties, evaluation_cache,
1659
1751
  snapshot[:cohorts], groups: groups, group_properties: group_properties,
1660
- person_properties: person_properties, snapshot: snapshot)
1752
+ person_properties: person_properties, snapshot: snapshot,
1753
+ device_id: device_id)
1661
1754
  end
1662
1755
 
1663
1756
  def _compute_flag_payload_locally(key, match_value, snapshot: _evaluation_snapshot)
@@ -1674,22 +1767,38 @@ module PostHog
1674
1767
  end
1675
1768
 
1676
1769
  # person_properties defaults to properties, which hold the person's properties unless the flag is
1677
- # group-aggregated. Flag dependencies read feature enrollment from them.
1770
+ # group-aggregated. Flag dependencies read feature enrollment from them. `device_id` is the resolved device
1771
+ # id, or nil when none is usable.
1678
1772
  def match_feature_flag_properties(flag, distinct_id, properties, evaluation_cache, cohort_properties = {},
1679
1773
  groups: {}, group_properties: {}, person_properties: properties,
1680
- snapshot: _evaluation_snapshot, dependency_evaluation: nil)
1774
+ snapshot: _evaluation_snapshot, device_id: nil, dependency_evaluation: nil)
1681
1775
  dependency_evaluation ||= DependencyEvaluation.new(flag[:key])
1682
1776
  flag_filters = flag[:filters] || {}
1777
+ flag_aggregation = flag_filters[:aggregation_group_type_index]
1778
+ device_bucketed = FeatureFlagsPoller.device_bucketed?(flag, flag_filters)
1683
1779
 
1684
1780
  # Holdouts precede release conditions: a held-out identifier is excluded from
1685
1781
  # the flag's targeting entirely rather than bucketed into one of its variants.
1686
1782
  # The per-condition aggregation handled below never changes holdout membership,
1687
1783
  # so this uses the flag-level bucketing identity the caller resolved.
1688
- holdout_variant = holdout_variant_for(flag_filters[:holdout], distinct_id)
1689
- return holdout_variant unless holdout_variant.nil?
1784
+ if (holdout = flag_filters[:holdout])
1785
+ person_device_bucketed = flag_aggregation.nil? && device_bucketed
1786
+ begin
1787
+ holdout_variant = holdout_variant_for(holdout, person_device_bucketed ? device_id : distinct_id,
1788
+ missing_device_id: person_device_bucketed && device_id.nil?)
1789
+ rescue InconclusiveMatchError => e
1790
+ # No condition can be checked without the holdout, so a cause in the definition outranks it.
1791
+ definition_reason = _definition_unresolved_reason(flag, snapshot)
1792
+ raise e if definition_reason.nil?
1793
+
1794
+ raise InconclusiveMatchError.new(
1795
+ e.message, reason: FeatureFlagsPoller.pick_unresolved_reason(e.reason, definition_reason)
1796
+ )
1797
+ end
1798
+ return holdout_variant unless holdout_variant.nil?
1799
+ end
1690
1800
 
1691
1801
  flag_conditions = flag_filters[:groups] || []
1692
- flag_aggregation = flag_filters[:aggregation_group_type_index]
1693
1802
  early_exit = flag_filters[:early_exit] == true
1694
1803
  inconclusive_reason = nil
1695
1804
  result = nil
@@ -1703,6 +1812,20 @@ module PostHog
1703
1812
 
1704
1813
  effective_properties = properties
1705
1814
  effective_bucketing = distinct_id
1815
+ condition_distinct_id = distinct_id
1816
+
1817
+ missing_device_id = false
1818
+
1819
+ # A person condition of a device-bucketed flag hashes the device id. Without one, it is
1820
+ # inconclusive once its filters match, whatever its rollout or variants, rather than
1821
+ # bucketed on the distinct id.
1822
+ if condition_aggregation.nil? && device_bucketed
1823
+ if device_id.nil?
1824
+ missing_device_id = true
1825
+ else
1826
+ effective_bucketing = device_id
1827
+ end
1828
+ end
1706
1829
 
1707
1830
  # Mixed-override path: condition-level aggregation differs from flag-level.
1708
1831
  # This assumes flag-level aggregation is nil for mixed flags.
@@ -1726,13 +1849,25 @@ module PostHog
1726
1849
  end
1727
1850
  effective_properties = group_properties[group_name.to_sym]
1728
1851
  effective_bucketing = groups[group_name.to_sym]
1852
+ condition_distinct_id = effective_bucketing
1729
1853
  end
1730
1854
  end
1731
1855
 
1732
- case condition_match_outcome(flag, effective_bucketing, condition, effective_properties, evaluation_cache,
1733
- cohort_properties, person_properties: person_properties,
1734
- snapshot: snapshot,
1735
- dependency_evaluation: dependency_evaluation)
1856
+ outcome =
1857
+ if device_bucketed || !device_id.nil?
1858
+ 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,
1861
+ missing_device_id: missing_device_id,
1862
+ dependency_evaluation: dependency_evaluation)
1863
+ else
1864
+ # Without device bucketing the condition hashes its distinct id; skipping the extra keywords
1865
+ # keeps the common path as cheap as before.
1866
+ condition_match_outcome(flag, condition_distinct_id, condition, effective_properties, evaluation_cache,
1867
+ cohort_properties, person_properties: person_properties, snapshot: snapshot,
1868
+ dependency_evaluation: dependency_evaluation)
1869
+ end
1870
+ case outcome
1736
1871
  when :match
1737
1872
  variant_override = condition[:variant]
1738
1873
  flag_multivariate = flag_filters[:multivariate] || {}
@@ -1792,9 +1927,12 @@ module PostHog
1792
1927
  # rollout percentage excluded the user
1793
1928
  # Distinguishing :no_match from :out_of_rollout_bound lets the caller implement the
1794
1929
  # early_exit behavior (mirrors the server-side Rust evaluation engine).
1930
+ #
1931
+ # `bucketing_value` is hashed for the rollout. `missing_device_id` marks a person condition of a
1932
+ # device-bucketed flag without a device id, so it is inconclusive once its filters match.
1795
1933
  def condition_match_outcome(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties = {},
1796
- person_properties: properties, snapshot: _evaluation_snapshot,
1797
- dependency_evaluation: nil)
1934
+ person_properties: properties, snapshot: _evaluation_snapshot, device_id: nil,
1935
+ bucketing_value: distinct_id, missing_device_id: false, dependency_evaluation: nil)
1798
1936
  rollout_percentage = condition[:rollout_percentage]
1799
1937
 
1800
1938
  unless (condition[:properties] || []).empty?
@@ -1807,7 +1945,7 @@ module PostHog
1807
1945
  if prop[:type] == 'flag'
1808
1946
  evaluate_flag_dependency(prop, evaluation_cache, distinct_id, properties, cohort_properties,
1809
1947
  person_properties: person_properties, snapshot: snapshot,
1810
- dependency_evaluation: dependency_evaluation)
1948
+ device_id: device_id, dependency_evaluation: dependency_evaluation)
1811
1949
  else
1812
1950
  FeatureFlagsPoller.match_property(prop, properties, cohort_properties,
1813
1951
  property_matching_version: snapshot[:property_matching_version])
@@ -1838,11 +1976,14 @@ module PostHog
1838
1976
  raise InconclusiveMatchError.new("Can't determine if a condition of flag #{flag[:key]} matches",
1839
1977
  reason: inconclusive_reason)
1840
1978
  end
1979
+ end
1841
1980
 
1842
- return :match if rollout_percentage.nil?
1981
+ if missing_device_id
1982
+ raise InconclusiveMatchError.new("Can't bucket a condition of flag #{flag[:key]} without a device id",
1983
+ reason: UnresolvedFlagReason::MISSING_CONTEXT)
1843
1984
  end
1844
1985
 
1845
- if !rollout_percentage.nil? && (_hash(flag[:key], distinct_id) > (rollout_percentage.to_f / 100))
1986
+ if !rollout_percentage.nil? && (_hash(flag[:key], bucketing_value) > (rollout_percentage.to_f / 100))
1846
1987
  return :out_of_rollout_bound
1847
1988
  end
1848
1989
 
@@ -1863,7 +2004,7 @@ module PostHog
1863
2004
  # not need to exist in `filters.multivariate.variants`. Returns nil when the flag has
1864
2005
  # no usable holdout or the identifier falls outside it, leaving ordinary evaluation
1865
2006
  # unchanged.
1866
- def holdout_variant_for(holdout, bucketing_value)
2007
+ def holdout_variant_for(holdout, bucketing_value, missing_device_id: false)
1867
2008
  return nil unless holdout.is_a?(Hash)
1868
2009
 
1869
2010
  id = holdout[:id]
@@ -1874,6 +2015,14 @@ module PostHog
1874
2015
  # 100 as everyone without hashing, so a full holdout can't miss on a hash boundary.
1875
2016
  # Fractional percentages are kept as-is.
1876
2017
  percentage = exclusion_percentage.to_f.clamp(0, 100)
2018
+ if missing_device_id
2019
+ # A device-bucketed person flag without a device id: hashing another identity would
2020
+ # hold out the wrong population. Even 100% is inconclusive, as for a missing group key.
2021
+ return nil if percentage.zero?
2022
+
2023
+ raise InconclusiveMatchError.new("Can't evaluate a device-bucketed holdout without a device id",
2024
+ reason: UnresolvedFlagReason::MISSING_CONTEXT)
2025
+ end
1877
2026
  return nil if percentage < 100 && _holdout_hash(bucketing_value) > (percentage / 100)
1878
2027
 
1879
2028
  "holdout-#{id}"
@@ -2038,6 +2187,7 @@ module PostHog
2038
2187
  new_group_type_mapping = Concurrent::Hash[deep_symbolize_keys(group_type_mapping)]
2039
2188
  new_cohorts = Concurrent::Hash[deep_symbolize_keys(cohorts)]
2040
2189
  unresolvable_flags = _unresolvable_flags(new_flags, new_by_key, new_cohorts, new_group_type_mapping)
2190
+ has_device_bucketed_flags = new_flags.any? { |flag| FeatureFlagsPoller.device_bucketed?(flag) }
2041
2191
  unwarned_flags = nil
2042
2192
 
2043
2193
  # A read of the new definitions must not record its event before the tracker reset.
@@ -2046,7 +2196,8 @@ module PostHog
2046
2196
  flags: new_flags, flags_by_key: new_by_key,
2047
2197
  group_type_mapping: new_group_type_mapping, cohorts: new_cohorts,
2048
2198
  minimal_flag_called_events: minimal_flag_called_events == true,
2049
- property_matching_version: property_matching_version
2199
+ property_matching_version: property_matching_version,
2200
+ has_device_bucketed_flags: has_device_bucketed_flags
2050
2201
  }.freeze
2051
2202
  @feature_flags = new_flags
2052
2203
  @feature_flags_by_key = new_by_key
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PostHog
4
- VERSION = '3.39.0'
4
+ VERSION = '3.40.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.39.0
4
+ version: 3.40.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PostHog