posthog-node 5.56.4 → 5.57.1
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.
- package/dist/client.d.ts +3 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +4 -0
- package/dist/client.mjs +4 -0
- package/dist/exports.d.ts +1 -1
- package/dist/exports.d.ts.map +1 -1
- package/dist/extensions/feature-flags/feature-flags.d.ts +9 -5
- package/dist/extensions/feature-flags/feature-flags.d.ts.map +1 -1
- package/dist/extensions/feature-flags/feature-flags.js +182 -64
- package/dist/extensions/feature-flags/feature-flags.mjs +183 -65
- package/dist/feature-flag-evaluations.d.ts +12 -1
- package/dist/feature-flag-evaluations.d.ts.map +1 -1
- package/dist/feature-flag-evaluations.js +7 -1
- package/dist/feature-flag-evaluations.mjs +7 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/types.mjs +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +2 -2
- package/src/client.ts +21 -2
- package/src/exports.ts +1 -1
- package/src/extensions/feature-flags/feature-flags.ts +319 -71
- package/src/feature-flag-evaluations.ts +24 -3
- package/src/types.ts +7 -0
- package/src/version.ts +1 -1
|
@@ -6,8 +6,15 @@ import {
|
|
|
6
6
|
PostHogFeatureFlag,
|
|
7
7
|
PropertyGroup,
|
|
8
8
|
} from '../../types'
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
FeatureFlagValue,
|
|
11
|
+
JsonType,
|
|
12
|
+
PostHogFetchOptions,
|
|
13
|
+
PostHogFetchResponse,
|
|
14
|
+
UnresolvedFlagReason,
|
|
15
|
+
} from '@posthog/core'
|
|
10
16
|
import {
|
|
17
|
+
assertFeatureFlagFilterSupported,
|
|
11
18
|
getFeatureFlagHash,
|
|
12
19
|
getFeatureFlagVariant,
|
|
13
20
|
getHoldoutVariant,
|
|
@@ -60,12 +67,41 @@ function setCustomErrorPrototype(error: Error, constructor: new (message: string
|
|
|
60
67
|
}
|
|
61
68
|
|
|
62
69
|
class RequiresServerEvaluation extends Error {
|
|
70
|
+
// Server-side data such as a static cohort is never available locally.
|
|
71
|
+
readonly reason: UnresolvedFlagReason = 'unsupported_definition'
|
|
72
|
+
|
|
63
73
|
constructor(message: string) {
|
|
64
74
|
super(message)
|
|
65
75
|
setCustomErrorPrototype(this, RequiresServerEvaluation)
|
|
66
76
|
}
|
|
67
77
|
}
|
|
68
78
|
|
|
79
|
+
// When several causes apply to one flag, the spec reports the first reason in this order, so
|
|
80
|
+
// every SDK picks the same reason whatever order it inspects the filters in.
|
|
81
|
+
const UNRESOLVED_REASON_PRECEDENCE: readonly UnresolvedFlagReason[] = [
|
|
82
|
+
'experience_continuity',
|
|
83
|
+
'unsupported_definition',
|
|
84
|
+
'unresolved_dependency',
|
|
85
|
+
'missing_context',
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
function pickUnresolvedReason(
|
|
89
|
+
current: UnresolvedFlagReason | undefined,
|
|
90
|
+
next: UnresolvedFlagReason
|
|
91
|
+
): UnresolvedFlagReason {
|
|
92
|
+
if (current === undefined) {
|
|
93
|
+
return next
|
|
94
|
+
}
|
|
95
|
+
return UNRESOLVED_REASON_PRECEDENCE.indexOf(next) < UNRESOLVED_REASON_PRECEDENCE.indexOf(current) ? next : current
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function preserveRegexUncertainty(error: InconclusiveMatchError, original: unknown): InconclusiveMatchError {
|
|
99
|
+
if (original instanceof InconclusiveMatchError && Symbol.for('posthog.requiresServerRegexEvaluation') in original) {
|
|
100
|
+
Object.defineProperty(error, Symbol.for('posthog.requiresServerRegexEvaluation'), { value: true })
|
|
101
|
+
}
|
|
102
|
+
return error
|
|
103
|
+
}
|
|
104
|
+
|
|
69
105
|
type FeatureFlagsPollerOptions = {
|
|
70
106
|
personalApiKey?: string
|
|
71
107
|
projectApiKey: string
|
|
@@ -147,6 +183,8 @@ class FeatureFlagsPoller {
|
|
|
147
183
|
// flag may still depend on one of these; the remote evaluator pre-seeds such flags as false,
|
|
148
184
|
// so dependency evaluation mirrors that instead of throwing "Missing flag dependency".
|
|
149
185
|
private filteredOutFlagKeys: Set<string> = new Set()
|
|
186
|
+
// Definition fingerprint per flag key already warned about as never locally resolvable.
|
|
187
|
+
private warnedUnresolvableFlags: Map<string, string> = new Map()
|
|
150
188
|
|
|
151
189
|
constructor({
|
|
152
190
|
pollingInterval,
|
|
@@ -189,6 +227,16 @@ class FeatureFlagsPoller {
|
|
|
189
227
|
}
|
|
190
228
|
}
|
|
191
229
|
|
|
230
|
+
private currentDefinitions(): FlagEvaluationSnapshot {
|
|
231
|
+
return {
|
|
232
|
+
featureFlagsByKey: this.featureFlagsByKey,
|
|
233
|
+
groupTypeMapping: this.groupTypeMapping,
|
|
234
|
+
cohorts: this.cohorts,
|
|
235
|
+
filteredOutFlagKeys: this.filteredOutFlagKeys,
|
|
236
|
+
propertyMatchingVersion: this.propertyMatchingVersion,
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
192
240
|
// Keep definitions and their matching version stable across asynchronous hashes/dependencies.
|
|
193
241
|
private withEvaluationSnapshot(context: FeatureFlagEvaluationContext): FeatureFlagEvaluationContext {
|
|
194
242
|
if (context.definitions) return context
|
|
@@ -196,13 +244,7 @@ class FeatureFlagsPoller {
|
|
|
196
244
|
...context,
|
|
197
245
|
// Dependency results belong to this evaluation snapshot, never a prior invocation.
|
|
198
246
|
evaluationCache: {},
|
|
199
|
-
definitions:
|
|
200
|
-
featureFlagsByKey: this.featureFlagsByKey,
|
|
201
|
-
groupTypeMapping: this.groupTypeMapping,
|
|
202
|
-
cohorts: this.cohorts,
|
|
203
|
-
filteredOutFlagKeys: this.filteredOutFlagKeys,
|
|
204
|
-
propertyMatchingVersion: this.propertyMatchingVersion,
|
|
205
|
-
},
|
|
247
|
+
definitions: this.currentDefinitions(),
|
|
206
248
|
}
|
|
207
249
|
}
|
|
208
250
|
|
|
@@ -264,12 +306,14 @@ class FeatureFlagsPoller {
|
|
|
264
306
|
): Promise<{
|
|
265
307
|
response: Record<string, FeatureFlagValue>
|
|
266
308
|
payloads: Record<string, JsonType>
|
|
309
|
+
unresolved: Record<string, UnresolvedFlagReason>
|
|
267
310
|
fallbackToFlags: boolean
|
|
268
311
|
}> {
|
|
269
312
|
await this.loadFeatureFlags()
|
|
270
313
|
|
|
271
314
|
const response: Record<string, FeatureFlagValue> = {}
|
|
272
315
|
const payloads: Record<string, JsonType> = {}
|
|
316
|
+
const unresolved: Record<string, UnresolvedFlagReason> = {}
|
|
273
317
|
let fallbackToFlags = this.featureFlags.length == 0
|
|
274
318
|
|
|
275
319
|
const flagsToEvaluate = flagKeysToExplicitlyEvaluate
|
|
@@ -296,6 +340,7 @@ class FeatureFlagsPoller {
|
|
|
296
340
|
} catch (e) {
|
|
297
341
|
if (e instanceof RequiresServerEvaluation || e instanceof InconclusiveMatchError) {
|
|
298
342
|
this.logMsgIfDebug(() => console.debug(`${e.name} when computing flag locally: ${flag.key}: ${e.message}`))
|
|
343
|
+
unresolved[flag.key] = e.reason
|
|
299
344
|
} else if (e instanceof Error) {
|
|
300
345
|
this.onError?.(new Error(`Error computing flag locally: ${flag.key}: ${e}`))
|
|
301
346
|
}
|
|
@@ -304,7 +349,7 @@ class FeatureFlagsPoller {
|
|
|
304
349
|
})
|
|
305
350
|
)
|
|
306
351
|
|
|
307
|
-
return { response, payloads, fallbackToFlags }
|
|
352
|
+
return { response, payloads, unresolved, fallbackToFlags }
|
|
308
353
|
}
|
|
309
354
|
|
|
310
355
|
async computeFlagAndPayloadLocally(
|
|
@@ -360,7 +405,7 @@ class FeatureFlagsPoller {
|
|
|
360
405
|
}
|
|
361
406
|
|
|
362
407
|
if (flag.ensure_experience_continuity) {
|
|
363
|
-
throw new InconclusiveMatchError('Flag has experience continuity enabled')
|
|
408
|
+
throw new InconclusiveMatchError('Flag has experience continuity enabled', 'experience_continuity')
|
|
364
409
|
}
|
|
365
410
|
|
|
366
411
|
const flagFilters = flag.filters || {}
|
|
@@ -406,7 +451,12 @@ class FeatureFlagsPoller {
|
|
|
406
451
|
`[FEATURE FLAGS] Can't compute feature flag: ${flag.key} without $device_id, falling back to server evaluation`
|
|
407
452
|
)
|
|
408
453
|
)
|
|
409
|
-
|
|
454
|
+
// No condition can be checked without a bucketing value, so a cause in the definition
|
|
455
|
+
// also applies, and it outranks missing_context.
|
|
456
|
+
throw new InconclusiveMatchError(
|
|
457
|
+
`Can't compute feature flag: ${flag.key} without $device_id`,
|
|
458
|
+
getDefinitionUnresolvedReason(flag, evaluationContext.definitions!) ?? 'missing_context'
|
|
459
|
+
)
|
|
410
460
|
}
|
|
411
461
|
return await this.matchFeatureFlagProperties(flag, bucketingValue, personProperties, evaluationContext)
|
|
412
462
|
}
|
|
@@ -470,21 +520,28 @@ class FeatureFlagsPoller {
|
|
|
470
520
|
)
|
|
471
521
|
}
|
|
472
522
|
|
|
523
|
+
// A dependency with no definition outranks one that is only inconclusive, so check the whole
|
|
524
|
+
// chain before evaluating any of it.
|
|
525
|
+
const missingDependency = dependencyChain.find(
|
|
526
|
+
(depFlagKey) =>
|
|
527
|
+
!(depFlagKey in evaluationCache) &&
|
|
528
|
+
!evaluationContext.definitions!.featureFlagsByKey[depFlagKey] &&
|
|
529
|
+
!evaluationContext.definitions!.filteredOutFlagKeys.has(depFlagKey)
|
|
530
|
+
)
|
|
531
|
+
if (missingDependency !== undefined) {
|
|
532
|
+
throw new InconclusiveMatchError(`Missing flag dependency '${missingDependency}' for flag '${targetFlagKey}'`)
|
|
533
|
+
}
|
|
534
|
+
|
|
473
535
|
// Evaluate all dependencies in the chain order
|
|
474
536
|
for (const depFlagKey of dependencyChain) {
|
|
475
537
|
if (!(depFlagKey in evaluationCache)) {
|
|
476
538
|
// Need to evaluate this dependency first
|
|
477
539
|
const depFlag = evaluationContext.definitions!.featureFlagsByKey[depFlagKey]
|
|
478
540
|
if (!depFlag) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
evaluationCache[depFlagKey] = false
|
|
484
|
-
} else {
|
|
485
|
-
// Missing flag dependency - cannot evaluate locally
|
|
486
|
-
throw new InconclusiveMatchError(`Missing flag dependency '${depFlagKey}' for flag '${targetFlagKey}'`)
|
|
487
|
-
}
|
|
541
|
+
// Dependency was dropped by evaluation-context filtering, not genuinely missing (the check
|
|
542
|
+
// above already threw for those). The remote evaluator pre-seeds context-filtered flags as
|
|
543
|
+
// false so conditions like `flag_evaluates_to=false` still match; do the same here.
|
|
544
|
+
evaluationCache[depFlagKey] = false
|
|
488
545
|
} else if (!depFlag.active) {
|
|
489
546
|
// Inactive flag evaluates to false
|
|
490
547
|
evaluationCache[depFlagKey] = false
|
|
@@ -494,8 +551,12 @@ class FeatureFlagsPoller {
|
|
|
494
551
|
const depResult = await this.computeFlagValueLocally(depFlag, evaluationContext)
|
|
495
552
|
evaluationCache[depFlagKey] = depResult
|
|
496
553
|
} catch (error) {
|
|
497
|
-
throw
|
|
498
|
-
|
|
554
|
+
throw preserveRegexUncertainty(
|
|
555
|
+
new InconclusiveMatchError(
|
|
556
|
+
`Error evaluating flag dependency '${depFlagKey}' for flag '${targetFlagKey}': ${error}`,
|
|
557
|
+
'unresolved_dependency'
|
|
558
|
+
),
|
|
559
|
+
error
|
|
499
560
|
)
|
|
500
561
|
}
|
|
501
562
|
}
|
|
@@ -504,7 +565,7 @@ class FeatureFlagsPoller {
|
|
|
504
565
|
// Check if dependency evaluation was inconclusive
|
|
505
566
|
const cachedResult = evaluationCache[depFlagKey]
|
|
506
567
|
if (cachedResult === null || cachedResult === undefined) {
|
|
507
|
-
throw new InconclusiveMatchError(`Dependency '${depFlagKey}' could not be evaluated
|
|
568
|
+
throw new InconclusiveMatchError(`Dependency '${depFlagKey}' could not be evaluated`, 'unresolved_dependency')
|
|
508
569
|
}
|
|
509
570
|
}
|
|
510
571
|
|
|
@@ -552,10 +613,13 @@ class FeatureFlagsPoller {
|
|
|
552
613
|
const flagAggregation = flagFilters.aggregation_group_type_index
|
|
553
614
|
const earlyExitEnabled = flagFilters.early_exit ?? false
|
|
554
615
|
const { groups, groupProperties } = evaluationContext
|
|
555
|
-
let
|
|
616
|
+
let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
|
|
617
|
+
let regexError: InconclusiveMatchError | undefined
|
|
618
|
+
const deferredRegexValues = new Set<FeatureFlagValue>()
|
|
556
619
|
let result = undefined
|
|
557
620
|
|
|
558
621
|
for (const condition of flagConditions) {
|
|
622
|
+
let effectiveBucketingValue = bucketingValue
|
|
559
623
|
try {
|
|
560
624
|
// Per-condition aggregation overrides only when the condition explicitly
|
|
561
625
|
// sets its own aggregation_group_type_index (mixed targeting).
|
|
@@ -566,7 +630,6 @@ class FeatureFlagsPoller {
|
|
|
566
630
|
: flagAggregation
|
|
567
631
|
|
|
568
632
|
let effectiveProperties = properties
|
|
569
|
-
let effectiveBucketingValue = bucketingValue
|
|
570
633
|
|
|
571
634
|
// Mixed-override path: condition-level aggregation differs from flag-level.
|
|
572
635
|
// This assumes flag-level aggregation is null/undefined for mixed flags.
|
|
@@ -582,7 +645,7 @@ class FeatureFlagsPoller {
|
|
|
582
645
|
continue
|
|
583
646
|
}
|
|
584
647
|
if (!(groupName in groupProperties)) {
|
|
585
|
-
|
|
648
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, 'missing_context')
|
|
586
649
|
continue
|
|
587
650
|
}
|
|
588
651
|
effectiveProperties = groupProperties[groupName]
|
|
@@ -610,7 +673,7 @@ class FeatureFlagsPoller {
|
|
|
610
673
|
// The condition's property filters (if any) matched and only the rollout check failed,
|
|
611
674
|
// so re-evaluating later groups can't change the outcome. If an earlier condition was
|
|
612
675
|
// inconclusive, stop here but preserve that result so the caller can fall back remotely.
|
|
613
|
-
if (
|
|
676
|
+
if (inconclusiveReason) {
|
|
614
677
|
break
|
|
615
678
|
}
|
|
616
679
|
return false
|
|
@@ -622,18 +685,43 @@ class FeatureFlagsPoller {
|
|
|
622
685
|
} else if (e instanceof InconclusiveMatchError) {
|
|
623
686
|
// Evaluation error (bad regex, invalid date, missing property, etc.)
|
|
624
687
|
// Track that we had an inconclusive match, but try other conditions
|
|
625
|
-
|
|
688
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, e.reason)
|
|
689
|
+
if (Symbol.for('posthog.requiresServerRegexEvaluation') in e) {
|
|
690
|
+
regexError = e
|
|
691
|
+
if (
|
|
692
|
+
earlyExitEnabled &&
|
|
693
|
+
condition.rollout_percentage != undefined &&
|
|
694
|
+
(await getFeatureFlagHash(flag.key, effectiveBucketingValue)) > condition.rollout_percentage / 100.0
|
|
695
|
+
) {
|
|
696
|
+
deferredRegexValues.add(false)
|
|
697
|
+
} else {
|
|
698
|
+
const variants = flagFilters.multivariate?.variants || []
|
|
699
|
+
const override = condition.variant
|
|
700
|
+
deferredRegexValues.add(
|
|
701
|
+
override && variants.some((variant) => variant.key === override)
|
|
702
|
+
? override
|
|
703
|
+
: (await this.getMatchingVariant(flag, effectiveBucketingValue)) || true
|
|
704
|
+
)
|
|
705
|
+
}
|
|
706
|
+
}
|
|
626
707
|
} else {
|
|
627
708
|
throw e
|
|
628
709
|
}
|
|
629
710
|
}
|
|
630
711
|
}
|
|
631
712
|
|
|
632
|
-
|
|
713
|
+
// A later match cannot choose a different value while an earlier regex rule is unresolved.
|
|
714
|
+
if (result !== undefined && !(regexError && [...deferredRegexValues].some((value) => value !== result))) {
|
|
633
715
|
return result
|
|
634
|
-
} else if (
|
|
635
|
-
//
|
|
636
|
-
throw
|
|
716
|
+
} else if (inconclusiveReason) {
|
|
717
|
+
// An unresolved condition can still change the selected value.
|
|
718
|
+
throw preserveRegexUncertainty(
|
|
719
|
+
new InconclusiveMatchError(
|
|
720
|
+
"Can't determine if feature flag is enabled or not with given properties",
|
|
721
|
+
inconclusiveReason
|
|
722
|
+
),
|
|
723
|
+
regexError
|
|
724
|
+
)
|
|
637
725
|
}
|
|
638
726
|
|
|
639
727
|
// We can only return False when all conditions are False
|
|
@@ -654,28 +742,58 @@ class FeatureFlagsPoller {
|
|
|
654
742
|
this.logMsgIfDebug(() => console.warn(msg))
|
|
655
743
|
}
|
|
656
744
|
if ((condition.properties || []).length > 0) {
|
|
745
|
+
// Filters in a condition have no defined order, so an inconclusive filter does not stop
|
|
746
|
+
// the loop: a later filter can still rule the condition out or carry a higher-precedence reason.
|
|
747
|
+
let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
|
|
748
|
+
let regexError: InconclusiveMatchError | undefined
|
|
749
|
+
let serverEvaluationError: RequiresServerEvaluation | undefined = undefined
|
|
657
750
|
for (const prop of condition.properties) {
|
|
658
751
|
const propertyType = prop.type
|
|
659
752
|
let matches = false
|
|
660
753
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
754
|
+
try {
|
|
755
|
+
if (propertyType === 'cohort') {
|
|
756
|
+
const inCohort = await matchCohort(
|
|
757
|
+
prop,
|
|
758
|
+
properties,
|
|
759
|
+
definitions.cohorts,
|
|
760
|
+
this.debugMode,
|
|
761
|
+
(depProp) => this.evaluateFlagDependency(depProp, properties, evaluationContext),
|
|
762
|
+
definitions.propertyMatchingVersion
|
|
763
|
+
)
|
|
764
|
+
// A flag-level cohort condition carries a membership operator ('in' | 'not_in').
|
|
765
|
+
// `matchCohort` only reports raw membership, so the operator must be applied here.
|
|
766
|
+
// Without this, 'not_in' is silently treated as 'in', inverting any cohort-exclusion
|
|
767
|
+
// condition (e.g. "enabled for everyone NOT in cohort X").
|
|
768
|
+
matches = prop.operator === 'not_in' ? !inCohort : inCohort
|
|
769
|
+
} else if (propertyType === 'flag') {
|
|
770
|
+
matches = await this.evaluateFlagDependency(prop, properties, evaluationContext)
|
|
771
|
+
} else if (!Object.prototype.hasOwnProperty.call(properties, prop.key)) {
|
|
772
|
+
// Same result as matchProperty for an absent key, without the cost of throwing for
|
|
773
|
+
// every missing filter now that the loop no longer stops at the first one.
|
|
774
|
+
assertFeatureFlagFilterSupported(prop)
|
|
775
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, 'missing_context')
|
|
776
|
+
continue
|
|
777
|
+
} else {
|
|
778
|
+
matches = matchProperty(prop, properties, warnFunction, definitions.propertyMatchingVersion)
|
|
779
|
+
}
|
|
780
|
+
} catch (e) {
|
|
781
|
+
if (e instanceof RequiresServerEvaluation) {
|
|
782
|
+
// Fall back to the server only when this is the first filter that could not be
|
|
783
|
+
// checked, as when the loop stopped there. After another inconclusive filter, later
|
|
784
|
+
// conditions must still be able to resolve the flag locally.
|
|
785
|
+
if (inconclusiveReason === undefined) {
|
|
786
|
+
serverEvaluationError ??= e
|
|
787
|
+
}
|
|
788
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, e.reason)
|
|
789
|
+
continue
|
|
790
|
+
}
|
|
791
|
+
if (e instanceof InconclusiveMatchError) {
|
|
792
|
+
if (Symbol.for('posthog.requiresServerRegexEvaluation') in e) regexError = e
|
|
793
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, e.reason)
|
|
794
|
+
continue
|
|
795
|
+
}
|
|
796
|
+
throw e
|
|
679
797
|
}
|
|
680
798
|
|
|
681
799
|
if (!matches) {
|
|
@@ -683,6 +801,18 @@ class FeatureFlagsPoller {
|
|
|
683
801
|
}
|
|
684
802
|
}
|
|
685
803
|
|
|
804
|
+
// Rethrow the original error type: matchFeatureFlagProperties stops at it and falls back to
|
|
805
|
+
// the server instead of trying later conditions.
|
|
806
|
+
if (serverEvaluationError) {
|
|
807
|
+
throw serverEvaluationError
|
|
808
|
+
}
|
|
809
|
+
if (inconclusiveReason) {
|
|
810
|
+
throw preserveRegexUncertainty(
|
|
811
|
+
new InconclusiveMatchError(`Can't determine if a condition of flag ${flag.key} matches`, inconclusiveReason),
|
|
812
|
+
regexError
|
|
813
|
+
)
|
|
814
|
+
}
|
|
815
|
+
|
|
686
816
|
if (rolloutPercentage == undefined) {
|
|
687
817
|
return 'match'
|
|
688
818
|
}
|
|
@@ -770,26 +900,62 @@ class FeatureFlagsPoller {
|
|
|
770
900
|
}
|
|
771
901
|
|
|
772
902
|
/**
|
|
773
|
-
* Warn about flags
|
|
774
|
-
* Called after loading flag definitions when local evaluation is enabled.
|
|
775
|
-
*
|
|
903
|
+
* Warn about flags whose definition local evaluation cannot resolve.
|
|
904
|
+
* Called after loading flag definitions when local evaluation is enabled. Each flag is
|
|
905
|
+
* warned about once until its definition changes.
|
|
776
906
|
*/
|
|
777
|
-
private
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
907
|
+
private warnAboutUnresolvableFlags(flags: PostHogFeatureFlag[]): void {
|
|
908
|
+
const experienceContinuityFlags: string[] = []
|
|
909
|
+
const unsupportedDefinitionFlags: string[] = []
|
|
910
|
+
// Rebuilt from the current definitions, so a flag that is deleted or becomes resolvable is
|
|
911
|
+
// forgotten and warns again if it later returns to an unresolvable definition.
|
|
912
|
+
const warnedFlags = new Map<string, string>()
|
|
913
|
+
const definitions = this.currentDefinitions()
|
|
914
|
+
for (const flag of flags) {
|
|
915
|
+
const reason = getDefinitionUnresolvedReason(flag, definitions)
|
|
916
|
+
if (!reason) {
|
|
917
|
+
continue
|
|
918
|
+
}
|
|
919
|
+
const fingerprint = JSON.stringify(flag)
|
|
920
|
+
warnedFlags.set(flag.key, fingerprint)
|
|
921
|
+
if (this.warnedUnresolvableFlags.get(flag.key) === fingerprint) {
|
|
922
|
+
continue
|
|
923
|
+
}
|
|
924
|
+
if (reason === 'experience_continuity') {
|
|
925
|
+
experienceContinuityFlags.push(flag.key)
|
|
926
|
+
} else {
|
|
927
|
+
unsupportedDefinitionFlags.push(flag.key)
|
|
928
|
+
}
|
|
781
929
|
}
|
|
930
|
+
this.warnedUnresolvableFlags = warnedFlags
|
|
782
931
|
|
|
783
|
-
const experienceContinuityFlags = flags.filter((f) => f.ensure_experience_continuity)
|
|
784
932
|
if (experienceContinuityFlags.length > 0) {
|
|
785
|
-
|
|
786
|
-
`
|
|
787
|
-
`
|
|
788
|
-
|
|
933
|
+
const consequence = this.strictLocalEvaluation
|
|
934
|
+
? `With strictLocalEvaluation enabled these flags are left unresolved. ` +
|
|
935
|
+
`To evaluate them locally, disable experience continuity on these flags in PostHog.`
|
|
936
|
+
: `Experience continuity is incompatible with local evaluation and will cause a server request on every ` +
|
|
789
937
|
`flag evaluation, negating local evaluation cost savings. ` +
|
|
790
938
|
`To avoid server requests and unexpected costs, either disable experience continuity on these flags ` +
|
|
791
939
|
`in PostHog, use strictLocalEvaluation: true in client init, or pass onlyEvaluateLocally: true ` +
|
|
792
940
|
`per flag call (flags that cannot be evaluated locally will return undefined).`
|
|
941
|
+
console.warn(
|
|
942
|
+
`[PostHog] You are using local evaluation but ${experienceContinuityFlags.length} flag(s) have experience ` +
|
|
943
|
+
`continuity enabled and cannot be resolved locally (reason: experience_continuity): ` +
|
|
944
|
+
`${experienceContinuityFlags.join(', ')}. ` +
|
|
945
|
+
consequence
|
|
946
|
+
)
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
if (unsupportedDefinitionFlags.length > 0) {
|
|
950
|
+
const consequence = this.strictLocalEvaluation
|
|
951
|
+
? `With strictLocalEvaluation enabled these flags are left unresolved when evaluation reaches that part.`
|
|
952
|
+
: `Evaluating these flags falls back to a server request when evaluation reaches that part.`
|
|
953
|
+
console.warn(
|
|
954
|
+
`[PostHog] You are using local evaluation but ${unsupportedDefinitionFlags.length} flag(s) use something ` +
|
|
955
|
+
`local evaluation does not support, such as a static cohort, an unknown operator, ` +
|
|
956
|
+
`or a missing or circular flag dependency ` +
|
|
957
|
+
`(reason: unsupported_definition): ${unsupportedDefinitionFlags.join(', ')}. ` +
|
|
958
|
+
`${consequence} To evaluate them locally, change these flags in PostHog or upgrade posthog-node.`
|
|
793
959
|
)
|
|
794
960
|
}
|
|
795
961
|
}
|
|
@@ -811,7 +977,7 @@ class FeatureFlagsPoller {
|
|
|
811
977
|
this.onLoad?.(this.featureFlags.length)
|
|
812
978
|
// Warn only about flags this instance actually evaluates locally; context-filtered flags
|
|
813
979
|
// are absent from the kept set and never take the server-fallback path.
|
|
814
|
-
this.
|
|
980
|
+
this.warnAboutUnresolvableFlags(this.featureFlags)
|
|
815
981
|
return true
|
|
816
982
|
}
|
|
817
983
|
return false
|
|
@@ -1057,7 +1223,7 @@ class FeatureFlagsPoller {
|
|
|
1057
1223
|
|
|
1058
1224
|
this.onLoad?.(this.featureFlags.length)
|
|
1059
1225
|
// Warn only about the kept (context-filtered) flags, not the full payload.
|
|
1060
|
-
this.
|
|
1226
|
+
this.warnAboutUnresolvableFlags(this.featureFlags)
|
|
1061
1227
|
break
|
|
1062
1228
|
}
|
|
1063
1229
|
|
|
@@ -1178,6 +1344,79 @@ class FeatureFlagsPoller {
|
|
|
1178
1344
|
}
|
|
1179
1345
|
}
|
|
1180
1346
|
|
|
1347
|
+
/**
|
|
1348
|
+
* The unresolved reason that comes from the flag's definition alone, so it is known when
|
|
1349
|
+
* definitions load. Matches the reason local evaluation reports for the same cause.
|
|
1350
|
+
*/
|
|
1351
|
+
function getDefinitionUnresolvedReason(
|
|
1352
|
+
flag: PostHogFeatureFlag,
|
|
1353
|
+
definitions: FlagEvaluationSnapshot
|
|
1354
|
+
): UnresolvedFlagReason | undefined {
|
|
1355
|
+
if (!flag.active) {
|
|
1356
|
+
return undefined
|
|
1357
|
+
}
|
|
1358
|
+
if (flag.ensure_experience_continuity) {
|
|
1359
|
+
return 'experience_continuity'
|
|
1360
|
+
}
|
|
1361
|
+
const flagFilters = flag.filters || {}
|
|
1362
|
+
const aggregation = flagFilters.aggregation_group_type_index
|
|
1363
|
+
if (aggregation != undefined && !definitions.groupTypeMapping[String(aggregation)]) {
|
|
1364
|
+
return 'unsupported_definition'
|
|
1365
|
+
}
|
|
1366
|
+
const usesUnsupportedProperty = (flagFilters.groups || []).some((condition) =>
|
|
1367
|
+
(condition.properties || []).some((property) => isUnsupportedProperty(property, definitions, new Set()))
|
|
1368
|
+
)
|
|
1369
|
+
return usesUnsupportedProperty ? 'unsupported_definition' : undefined
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
function isUnsupportedProperty(
|
|
1373
|
+
property: FlagProperty,
|
|
1374
|
+
definitions: FlagEvaluationSnapshot,
|
|
1375
|
+
visitedCohortIds: Set<string>
|
|
1376
|
+
): boolean {
|
|
1377
|
+
if (property.type === 'flag') {
|
|
1378
|
+
const dependencyChain = property.dependency_chain
|
|
1379
|
+
if (!Array.isArray(dependencyChain) || dependencyChain.length === 0) {
|
|
1380
|
+
return true
|
|
1381
|
+
}
|
|
1382
|
+
return dependencyChain.some(
|
|
1383
|
+
(key) => !definitions.featureFlagsByKey[key] && !definitions.filteredOutFlagKeys.has(key)
|
|
1384
|
+
)
|
|
1385
|
+
}
|
|
1386
|
+
if (property.type !== 'cohort') {
|
|
1387
|
+
try {
|
|
1388
|
+
assertFeatureFlagFilterSupported(property)
|
|
1389
|
+
return false
|
|
1390
|
+
} catch (e) {
|
|
1391
|
+
if (e instanceof InconclusiveMatchError) {
|
|
1392
|
+
return true
|
|
1393
|
+
}
|
|
1394
|
+
throw e
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
const cohortId = String(property.value)
|
|
1398
|
+
if (!(cohortId in definitions.cohorts)) {
|
|
1399
|
+
return true
|
|
1400
|
+
}
|
|
1401
|
+
if (visitedCohortIds.has(cohortId)) {
|
|
1402
|
+
return false
|
|
1403
|
+
}
|
|
1404
|
+
visitedCohortIds.add(cohortId)
|
|
1405
|
+
return isUnsupportedPropertyGroup(definitions.cohorts[cohortId], definitions, visitedCohortIds)
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
function isUnsupportedPropertyGroup(
|
|
1409
|
+
group: PropertyGroup | undefined,
|
|
1410
|
+
definitions: FlagEvaluationSnapshot,
|
|
1411
|
+
visitedCohortIds: Set<string>
|
|
1412
|
+
): boolean {
|
|
1413
|
+
return (group?.values ?? []).some((value: PropertyGroup | FlagProperty) =>
|
|
1414
|
+
'values' in value
|
|
1415
|
+
? isUnsupportedPropertyGroup(value, definitions, visitedCohortIds)
|
|
1416
|
+
: isUnsupportedProperty(value, definitions, visitedCohortIds)
|
|
1417
|
+
)
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1181
1420
|
function matchProperty(
|
|
1182
1421
|
property: FeatureFlagCondition['properties'][number],
|
|
1183
1422
|
propertyValues: Record<string, any>,
|
|
@@ -1243,7 +1482,8 @@ async function matchPropertyGroup(
|
|
|
1243
1482
|
return true
|
|
1244
1483
|
}
|
|
1245
1484
|
|
|
1246
|
-
let
|
|
1485
|
+
let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
|
|
1486
|
+
let regexError: InconclusiveMatchError | undefined
|
|
1247
1487
|
|
|
1248
1488
|
if ('values' in properties[0]) {
|
|
1249
1489
|
// a nested property group
|
|
@@ -1275,15 +1515,19 @@ async function matchPropertyGroup(
|
|
|
1275
1515
|
if (debugMode) {
|
|
1276
1516
|
console.debug(`Failed to compute property ${prop} locally: ${err}`)
|
|
1277
1517
|
}
|
|
1278
|
-
|
|
1518
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, err.reason)
|
|
1519
|
+
if (Symbol.for('posthog.requiresServerRegexEvaluation') in err) regexError = err
|
|
1279
1520
|
} else {
|
|
1280
1521
|
throw err
|
|
1281
1522
|
}
|
|
1282
1523
|
}
|
|
1283
1524
|
}
|
|
1284
1525
|
|
|
1285
|
-
if (
|
|
1286
|
-
throw
|
|
1526
|
+
if (inconclusiveReason) {
|
|
1527
|
+
throw preserveRegexUncertainty(
|
|
1528
|
+
new InconclusiveMatchError("Can't match cohort without a given cohort property value", inconclusiveReason),
|
|
1529
|
+
regexError
|
|
1530
|
+
)
|
|
1287
1531
|
}
|
|
1288
1532
|
// if we get here, all matched in AND case, or none matched in OR case
|
|
1289
1533
|
return propertyGroupType === 'AND'
|
|
@@ -1338,15 +1582,19 @@ async function matchPropertyGroup(
|
|
|
1338
1582
|
if (debugMode) {
|
|
1339
1583
|
console.debug(`Failed to compute property ${prop} locally: ${err}`)
|
|
1340
1584
|
}
|
|
1341
|
-
|
|
1585
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, err.reason)
|
|
1586
|
+
if (Symbol.for('posthog.requiresServerRegexEvaluation') in err) regexError = err
|
|
1342
1587
|
} else {
|
|
1343
1588
|
throw err
|
|
1344
1589
|
}
|
|
1345
1590
|
}
|
|
1346
1591
|
}
|
|
1347
1592
|
|
|
1348
|
-
if (
|
|
1349
|
-
throw
|
|
1593
|
+
if (inconclusiveReason) {
|
|
1594
|
+
throw preserveRegexUncertainty(
|
|
1595
|
+
new InconclusiveMatchError("can't match cohort without a given cohort property value", inconclusiveReason),
|
|
1596
|
+
regexError
|
|
1597
|
+
)
|
|
1350
1598
|
}
|
|
1351
1599
|
|
|
1352
1600
|
// if we get here, all matched in AND case, or none matched in OR case
|