posthog-node 5.56.4 → 5.57.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.
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -0
- package/dist/client.mjs +3 -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 +127 -36
- package/dist/extensions/feature-flags/feature-flags.mjs +128 -37
- 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 +2 -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 +4 -4
- package/src/client.ts +18 -2
- package/src/exports.ts +1 -1
- package/src/extensions/feature-flags/feature-flags.ts +270 -67
- package/src/feature-flag-evaluations.ts +24 -3
- package/src/types.ts +2 -0
- package/src/version.ts +1 -1
package/src/exports.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type {
|
|
|
12
12
|
// Re-export FeatureFlagError from core for backwards compatibility.
|
|
13
13
|
// These were originally defined in posthog-node and moved to core for reuse across SDKs.
|
|
14
14
|
export { FeatureFlagError } from '@posthog/core'
|
|
15
|
-
export type { FeatureFlagErrorType } from '@posthog/core'
|
|
15
|
+
export type { FeatureFlagErrorType, UnresolvedFlagReason } from '@posthog/core'
|
|
16
16
|
|
|
17
17
|
// Metrics types re-exported so consumers can name the `metrics` client option
|
|
18
18
|
// and API surface without a direct @posthog/core dependency.
|
|
@@ -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,34 @@ 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
|
+
|
|
69
98
|
type FeatureFlagsPollerOptions = {
|
|
70
99
|
personalApiKey?: string
|
|
71
100
|
projectApiKey: string
|
|
@@ -147,6 +176,8 @@ class FeatureFlagsPoller {
|
|
|
147
176
|
// flag may still depend on one of these; the remote evaluator pre-seeds such flags as false,
|
|
148
177
|
// so dependency evaluation mirrors that instead of throwing "Missing flag dependency".
|
|
149
178
|
private filteredOutFlagKeys: Set<string> = new Set()
|
|
179
|
+
// Definition fingerprint per flag key already warned about as never locally resolvable.
|
|
180
|
+
private warnedUnresolvableFlags: Map<string, string> = new Map()
|
|
150
181
|
|
|
151
182
|
constructor({
|
|
152
183
|
pollingInterval,
|
|
@@ -189,6 +220,16 @@ class FeatureFlagsPoller {
|
|
|
189
220
|
}
|
|
190
221
|
}
|
|
191
222
|
|
|
223
|
+
private currentDefinitions(): FlagEvaluationSnapshot {
|
|
224
|
+
return {
|
|
225
|
+
featureFlagsByKey: this.featureFlagsByKey,
|
|
226
|
+
groupTypeMapping: this.groupTypeMapping,
|
|
227
|
+
cohorts: this.cohorts,
|
|
228
|
+
filteredOutFlagKeys: this.filteredOutFlagKeys,
|
|
229
|
+
propertyMatchingVersion: this.propertyMatchingVersion,
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
192
233
|
// Keep definitions and their matching version stable across asynchronous hashes/dependencies.
|
|
193
234
|
private withEvaluationSnapshot(context: FeatureFlagEvaluationContext): FeatureFlagEvaluationContext {
|
|
194
235
|
if (context.definitions) return context
|
|
@@ -196,13 +237,7 @@ class FeatureFlagsPoller {
|
|
|
196
237
|
...context,
|
|
197
238
|
// Dependency results belong to this evaluation snapshot, never a prior invocation.
|
|
198
239
|
evaluationCache: {},
|
|
199
|
-
definitions:
|
|
200
|
-
featureFlagsByKey: this.featureFlagsByKey,
|
|
201
|
-
groupTypeMapping: this.groupTypeMapping,
|
|
202
|
-
cohorts: this.cohorts,
|
|
203
|
-
filteredOutFlagKeys: this.filteredOutFlagKeys,
|
|
204
|
-
propertyMatchingVersion: this.propertyMatchingVersion,
|
|
205
|
-
},
|
|
240
|
+
definitions: this.currentDefinitions(),
|
|
206
241
|
}
|
|
207
242
|
}
|
|
208
243
|
|
|
@@ -264,12 +299,14 @@ class FeatureFlagsPoller {
|
|
|
264
299
|
): Promise<{
|
|
265
300
|
response: Record<string, FeatureFlagValue>
|
|
266
301
|
payloads: Record<string, JsonType>
|
|
302
|
+
unresolved: Record<string, UnresolvedFlagReason>
|
|
267
303
|
fallbackToFlags: boolean
|
|
268
304
|
}> {
|
|
269
305
|
await this.loadFeatureFlags()
|
|
270
306
|
|
|
271
307
|
const response: Record<string, FeatureFlagValue> = {}
|
|
272
308
|
const payloads: Record<string, JsonType> = {}
|
|
309
|
+
const unresolved: Record<string, UnresolvedFlagReason> = {}
|
|
273
310
|
let fallbackToFlags = this.featureFlags.length == 0
|
|
274
311
|
|
|
275
312
|
const flagsToEvaluate = flagKeysToExplicitlyEvaluate
|
|
@@ -296,6 +333,7 @@ class FeatureFlagsPoller {
|
|
|
296
333
|
} catch (e) {
|
|
297
334
|
if (e instanceof RequiresServerEvaluation || e instanceof InconclusiveMatchError) {
|
|
298
335
|
this.logMsgIfDebug(() => console.debug(`${e.name} when computing flag locally: ${flag.key}: ${e.message}`))
|
|
336
|
+
unresolved[flag.key] = e.reason
|
|
299
337
|
} else if (e instanceof Error) {
|
|
300
338
|
this.onError?.(new Error(`Error computing flag locally: ${flag.key}: ${e}`))
|
|
301
339
|
}
|
|
@@ -304,7 +342,7 @@ class FeatureFlagsPoller {
|
|
|
304
342
|
})
|
|
305
343
|
)
|
|
306
344
|
|
|
307
|
-
return { response, payloads, fallbackToFlags }
|
|
345
|
+
return { response, payloads, unresolved, fallbackToFlags }
|
|
308
346
|
}
|
|
309
347
|
|
|
310
348
|
async computeFlagAndPayloadLocally(
|
|
@@ -360,7 +398,7 @@ class FeatureFlagsPoller {
|
|
|
360
398
|
}
|
|
361
399
|
|
|
362
400
|
if (flag.ensure_experience_continuity) {
|
|
363
|
-
throw new InconclusiveMatchError('Flag has experience continuity enabled')
|
|
401
|
+
throw new InconclusiveMatchError('Flag has experience continuity enabled', 'experience_continuity')
|
|
364
402
|
}
|
|
365
403
|
|
|
366
404
|
const flagFilters = flag.filters || {}
|
|
@@ -406,7 +444,12 @@ class FeatureFlagsPoller {
|
|
|
406
444
|
`[FEATURE FLAGS] Can't compute feature flag: ${flag.key} without $device_id, falling back to server evaluation`
|
|
407
445
|
)
|
|
408
446
|
)
|
|
409
|
-
|
|
447
|
+
// No condition can be checked without a bucketing value, so a cause in the definition
|
|
448
|
+
// also applies, and it outranks missing_context.
|
|
449
|
+
throw new InconclusiveMatchError(
|
|
450
|
+
`Can't compute feature flag: ${flag.key} without $device_id`,
|
|
451
|
+
getDefinitionUnresolvedReason(flag, evaluationContext.definitions!) ?? 'missing_context'
|
|
452
|
+
)
|
|
410
453
|
}
|
|
411
454
|
return await this.matchFeatureFlagProperties(flag, bucketingValue, personProperties, evaluationContext)
|
|
412
455
|
}
|
|
@@ -470,21 +513,28 @@ class FeatureFlagsPoller {
|
|
|
470
513
|
)
|
|
471
514
|
}
|
|
472
515
|
|
|
516
|
+
// A dependency with no definition outranks one that is only inconclusive, so check the whole
|
|
517
|
+
// chain before evaluating any of it.
|
|
518
|
+
const missingDependency = dependencyChain.find(
|
|
519
|
+
(depFlagKey) =>
|
|
520
|
+
!(depFlagKey in evaluationCache) &&
|
|
521
|
+
!evaluationContext.definitions!.featureFlagsByKey[depFlagKey] &&
|
|
522
|
+
!evaluationContext.definitions!.filteredOutFlagKeys.has(depFlagKey)
|
|
523
|
+
)
|
|
524
|
+
if (missingDependency !== undefined) {
|
|
525
|
+
throw new InconclusiveMatchError(`Missing flag dependency '${missingDependency}' for flag '${targetFlagKey}'`)
|
|
526
|
+
}
|
|
527
|
+
|
|
473
528
|
// Evaluate all dependencies in the chain order
|
|
474
529
|
for (const depFlagKey of dependencyChain) {
|
|
475
530
|
if (!(depFlagKey in evaluationCache)) {
|
|
476
531
|
// Need to evaluate this dependency first
|
|
477
532
|
const depFlag = evaluationContext.definitions!.featureFlagsByKey[depFlagKey]
|
|
478
533
|
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
|
-
}
|
|
534
|
+
// Dependency was dropped by evaluation-context filtering, not genuinely missing (the check
|
|
535
|
+
// above already threw for those). The remote evaluator pre-seeds context-filtered flags as
|
|
536
|
+
// false so conditions like `flag_evaluates_to=false` still match; do the same here.
|
|
537
|
+
evaluationCache[depFlagKey] = false
|
|
488
538
|
} else if (!depFlag.active) {
|
|
489
539
|
// Inactive flag evaluates to false
|
|
490
540
|
evaluationCache[depFlagKey] = false
|
|
@@ -495,7 +545,8 @@ class FeatureFlagsPoller {
|
|
|
495
545
|
evaluationCache[depFlagKey] = depResult
|
|
496
546
|
} catch (error) {
|
|
497
547
|
throw new InconclusiveMatchError(
|
|
498
|
-
`Error evaluating flag dependency '${depFlagKey}' for flag '${targetFlagKey}': ${error}
|
|
548
|
+
`Error evaluating flag dependency '${depFlagKey}' for flag '${targetFlagKey}': ${error}`,
|
|
549
|
+
'unresolved_dependency'
|
|
499
550
|
)
|
|
500
551
|
}
|
|
501
552
|
}
|
|
@@ -504,7 +555,7 @@ class FeatureFlagsPoller {
|
|
|
504
555
|
// Check if dependency evaluation was inconclusive
|
|
505
556
|
const cachedResult = evaluationCache[depFlagKey]
|
|
506
557
|
if (cachedResult === null || cachedResult === undefined) {
|
|
507
|
-
throw new InconclusiveMatchError(`Dependency '${depFlagKey}' could not be evaluated
|
|
558
|
+
throw new InconclusiveMatchError(`Dependency '${depFlagKey}' could not be evaluated`, 'unresolved_dependency')
|
|
508
559
|
}
|
|
509
560
|
}
|
|
510
561
|
|
|
@@ -552,7 +603,7 @@ class FeatureFlagsPoller {
|
|
|
552
603
|
const flagAggregation = flagFilters.aggregation_group_type_index
|
|
553
604
|
const earlyExitEnabled = flagFilters.early_exit ?? false
|
|
554
605
|
const { groups, groupProperties } = evaluationContext
|
|
555
|
-
let
|
|
606
|
+
let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
|
|
556
607
|
let result = undefined
|
|
557
608
|
|
|
558
609
|
for (const condition of flagConditions) {
|
|
@@ -582,7 +633,7 @@ class FeatureFlagsPoller {
|
|
|
582
633
|
continue
|
|
583
634
|
}
|
|
584
635
|
if (!(groupName in groupProperties)) {
|
|
585
|
-
|
|
636
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, 'missing_context')
|
|
586
637
|
continue
|
|
587
638
|
}
|
|
588
639
|
effectiveProperties = groupProperties[groupName]
|
|
@@ -610,7 +661,7 @@ class FeatureFlagsPoller {
|
|
|
610
661
|
// The condition's property filters (if any) matched and only the rollout check failed,
|
|
611
662
|
// so re-evaluating later groups can't change the outcome. If an earlier condition was
|
|
612
663
|
// inconclusive, stop here but preserve that result so the caller can fall back remotely.
|
|
613
|
-
if (
|
|
664
|
+
if (inconclusiveReason) {
|
|
614
665
|
break
|
|
615
666
|
}
|
|
616
667
|
return false
|
|
@@ -622,7 +673,7 @@ class FeatureFlagsPoller {
|
|
|
622
673
|
} else if (e instanceof InconclusiveMatchError) {
|
|
623
674
|
// Evaluation error (bad regex, invalid date, missing property, etc.)
|
|
624
675
|
// Track that we had an inconclusive match, but try other conditions
|
|
625
|
-
|
|
676
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, e.reason)
|
|
626
677
|
} else {
|
|
627
678
|
throw e
|
|
628
679
|
}
|
|
@@ -631,9 +682,12 @@ class FeatureFlagsPoller {
|
|
|
631
682
|
|
|
632
683
|
if (result !== undefined) {
|
|
633
684
|
return result
|
|
634
|
-
} else if (
|
|
685
|
+
} else if (inconclusiveReason) {
|
|
635
686
|
// Had evaluation errors and no successful match - can't determine locally
|
|
636
|
-
throw new InconclusiveMatchError(
|
|
687
|
+
throw new InconclusiveMatchError(
|
|
688
|
+
"Can't determine if feature flag is enabled or not with given properties",
|
|
689
|
+
inconclusiveReason
|
|
690
|
+
)
|
|
637
691
|
}
|
|
638
692
|
|
|
639
693
|
// We can only return False when all conditions are False
|
|
@@ -654,28 +708,56 @@ class FeatureFlagsPoller {
|
|
|
654
708
|
this.logMsgIfDebug(() => console.warn(msg))
|
|
655
709
|
}
|
|
656
710
|
if ((condition.properties || []).length > 0) {
|
|
711
|
+
// Filters in a condition have no defined order, so an inconclusive filter does not stop
|
|
712
|
+
// the loop: a later filter can still rule the condition out or carry a higher-precedence reason.
|
|
713
|
+
let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
|
|
714
|
+
let serverEvaluationError: RequiresServerEvaluation | undefined = undefined
|
|
657
715
|
for (const prop of condition.properties) {
|
|
658
716
|
const propertyType = prop.type
|
|
659
717
|
let matches = false
|
|
660
718
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
719
|
+
try {
|
|
720
|
+
if (propertyType === 'cohort') {
|
|
721
|
+
const inCohort = await matchCohort(
|
|
722
|
+
prop,
|
|
723
|
+
properties,
|
|
724
|
+
definitions.cohorts,
|
|
725
|
+
this.debugMode,
|
|
726
|
+
(depProp) => this.evaluateFlagDependency(depProp, properties, evaluationContext),
|
|
727
|
+
definitions.propertyMatchingVersion
|
|
728
|
+
)
|
|
729
|
+
// A flag-level cohort condition carries a membership operator ('in' | 'not_in').
|
|
730
|
+
// `matchCohort` only reports raw membership, so the operator must be applied here.
|
|
731
|
+
// Without this, 'not_in' is silently treated as 'in', inverting any cohort-exclusion
|
|
732
|
+
// condition (e.g. "enabled for everyone NOT in cohort X").
|
|
733
|
+
matches = prop.operator === 'not_in' ? !inCohort : inCohort
|
|
734
|
+
} else if (propertyType === 'flag') {
|
|
735
|
+
matches = await this.evaluateFlagDependency(prop, properties, evaluationContext)
|
|
736
|
+
} else if (!Object.prototype.hasOwnProperty.call(properties, prop.key)) {
|
|
737
|
+
// Same result as matchProperty for an absent key, without the cost of throwing for
|
|
738
|
+
// every missing filter now that the loop no longer stops at the first one.
|
|
739
|
+
assertFeatureFlagFilterSupported(prop)
|
|
740
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, 'missing_context')
|
|
741
|
+
continue
|
|
742
|
+
} else {
|
|
743
|
+
matches = matchProperty(prop, properties, warnFunction, definitions.propertyMatchingVersion)
|
|
744
|
+
}
|
|
745
|
+
} catch (e) {
|
|
746
|
+
if (e instanceof RequiresServerEvaluation) {
|
|
747
|
+
// Fall back to the server only when this is the first filter that could not be
|
|
748
|
+
// checked, as when the loop stopped there. After another inconclusive filter, later
|
|
749
|
+
// conditions must still be able to resolve the flag locally.
|
|
750
|
+
if (inconclusiveReason === undefined) {
|
|
751
|
+
serverEvaluationError ??= e
|
|
752
|
+
}
|
|
753
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, e.reason)
|
|
754
|
+
continue
|
|
755
|
+
}
|
|
756
|
+
if (e instanceof InconclusiveMatchError) {
|
|
757
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, e.reason)
|
|
758
|
+
continue
|
|
759
|
+
}
|
|
760
|
+
throw e
|
|
679
761
|
}
|
|
680
762
|
|
|
681
763
|
if (!matches) {
|
|
@@ -683,6 +765,18 @@ class FeatureFlagsPoller {
|
|
|
683
765
|
}
|
|
684
766
|
}
|
|
685
767
|
|
|
768
|
+
// Rethrow the original error type: matchFeatureFlagProperties stops at it and falls back to
|
|
769
|
+
// the server instead of trying later conditions.
|
|
770
|
+
if (serverEvaluationError) {
|
|
771
|
+
throw serverEvaluationError
|
|
772
|
+
}
|
|
773
|
+
if (inconclusiveReason) {
|
|
774
|
+
throw new InconclusiveMatchError(
|
|
775
|
+
`Can't determine if a condition of flag ${flag.key} matches`,
|
|
776
|
+
inconclusiveReason
|
|
777
|
+
)
|
|
778
|
+
}
|
|
779
|
+
|
|
686
780
|
if (rolloutPercentage == undefined) {
|
|
687
781
|
return 'match'
|
|
688
782
|
}
|
|
@@ -770,26 +864,62 @@ class FeatureFlagsPoller {
|
|
|
770
864
|
}
|
|
771
865
|
|
|
772
866
|
/**
|
|
773
|
-
* Warn about flags
|
|
774
|
-
* Called after loading flag definitions when local evaluation is enabled.
|
|
775
|
-
*
|
|
867
|
+
* Warn about flags whose definition local evaluation cannot resolve.
|
|
868
|
+
* Called after loading flag definitions when local evaluation is enabled. Each flag is
|
|
869
|
+
* warned about once until its definition changes.
|
|
776
870
|
*/
|
|
777
|
-
private
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
871
|
+
private warnAboutUnresolvableFlags(flags: PostHogFeatureFlag[]): void {
|
|
872
|
+
const experienceContinuityFlags: string[] = []
|
|
873
|
+
const unsupportedDefinitionFlags: string[] = []
|
|
874
|
+
// Rebuilt from the current definitions, so a flag that is deleted or becomes resolvable is
|
|
875
|
+
// forgotten and warns again if it later returns to an unresolvable definition.
|
|
876
|
+
const warnedFlags = new Map<string, string>()
|
|
877
|
+
const definitions = this.currentDefinitions()
|
|
878
|
+
for (const flag of flags) {
|
|
879
|
+
const reason = getDefinitionUnresolvedReason(flag, definitions)
|
|
880
|
+
if (!reason) {
|
|
881
|
+
continue
|
|
882
|
+
}
|
|
883
|
+
const fingerprint = JSON.stringify(flag)
|
|
884
|
+
warnedFlags.set(flag.key, fingerprint)
|
|
885
|
+
if (this.warnedUnresolvableFlags.get(flag.key) === fingerprint) {
|
|
886
|
+
continue
|
|
887
|
+
}
|
|
888
|
+
if (reason === 'experience_continuity') {
|
|
889
|
+
experienceContinuityFlags.push(flag.key)
|
|
890
|
+
} else {
|
|
891
|
+
unsupportedDefinitionFlags.push(flag.key)
|
|
892
|
+
}
|
|
781
893
|
}
|
|
894
|
+
this.warnedUnresolvableFlags = warnedFlags
|
|
782
895
|
|
|
783
|
-
const experienceContinuityFlags = flags.filter((f) => f.ensure_experience_continuity)
|
|
784
896
|
if (experienceContinuityFlags.length > 0) {
|
|
785
|
-
|
|
786
|
-
`
|
|
787
|
-
`
|
|
788
|
-
|
|
897
|
+
const consequence = this.strictLocalEvaluation
|
|
898
|
+
? `With strictLocalEvaluation enabled these flags are left unresolved. ` +
|
|
899
|
+
`To evaluate them locally, disable experience continuity on these flags in PostHog.`
|
|
900
|
+
: `Experience continuity is incompatible with local evaluation and will cause a server request on every ` +
|
|
789
901
|
`flag evaluation, negating local evaluation cost savings. ` +
|
|
790
902
|
`To avoid server requests and unexpected costs, either disable experience continuity on these flags ` +
|
|
791
903
|
`in PostHog, use strictLocalEvaluation: true in client init, or pass onlyEvaluateLocally: true ` +
|
|
792
904
|
`per flag call (flags that cannot be evaluated locally will return undefined).`
|
|
905
|
+
console.warn(
|
|
906
|
+
`[PostHog] You are using local evaluation but ${experienceContinuityFlags.length} flag(s) have experience ` +
|
|
907
|
+
`continuity enabled and cannot be resolved locally (reason: experience_continuity): ` +
|
|
908
|
+
`${experienceContinuityFlags.join(', ')}. ` +
|
|
909
|
+
consequence
|
|
910
|
+
)
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if (unsupportedDefinitionFlags.length > 0) {
|
|
914
|
+
const consequence = this.strictLocalEvaluation
|
|
915
|
+
? `With strictLocalEvaluation enabled these flags are left unresolved when evaluation reaches that part.`
|
|
916
|
+
: `Evaluating these flags falls back to a server request when evaluation reaches that part.`
|
|
917
|
+
console.warn(
|
|
918
|
+
`[PostHog] You are using local evaluation but ${unsupportedDefinitionFlags.length} flag(s) use something ` +
|
|
919
|
+
`local evaluation does not support, such as a static cohort, an unknown operator, ` +
|
|
920
|
+
`or a missing or circular flag dependency ` +
|
|
921
|
+
`(reason: unsupported_definition): ${unsupportedDefinitionFlags.join(', ')}. ` +
|
|
922
|
+
`${consequence} To evaluate them locally, change these flags in PostHog or upgrade posthog-node.`
|
|
793
923
|
)
|
|
794
924
|
}
|
|
795
925
|
}
|
|
@@ -811,7 +941,7 @@ class FeatureFlagsPoller {
|
|
|
811
941
|
this.onLoad?.(this.featureFlags.length)
|
|
812
942
|
// Warn only about flags this instance actually evaluates locally; context-filtered flags
|
|
813
943
|
// are absent from the kept set and never take the server-fallback path.
|
|
814
|
-
this.
|
|
944
|
+
this.warnAboutUnresolvableFlags(this.featureFlags)
|
|
815
945
|
return true
|
|
816
946
|
}
|
|
817
947
|
return false
|
|
@@ -1057,7 +1187,7 @@ class FeatureFlagsPoller {
|
|
|
1057
1187
|
|
|
1058
1188
|
this.onLoad?.(this.featureFlags.length)
|
|
1059
1189
|
// Warn only about the kept (context-filtered) flags, not the full payload.
|
|
1060
|
-
this.
|
|
1190
|
+
this.warnAboutUnresolvableFlags(this.featureFlags)
|
|
1061
1191
|
break
|
|
1062
1192
|
}
|
|
1063
1193
|
|
|
@@ -1178,6 +1308,79 @@ class FeatureFlagsPoller {
|
|
|
1178
1308
|
}
|
|
1179
1309
|
}
|
|
1180
1310
|
|
|
1311
|
+
/**
|
|
1312
|
+
* The unresolved reason that comes from the flag's definition alone, so it is known when
|
|
1313
|
+
* definitions load. Matches the reason local evaluation reports for the same cause.
|
|
1314
|
+
*/
|
|
1315
|
+
function getDefinitionUnresolvedReason(
|
|
1316
|
+
flag: PostHogFeatureFlag,
|
|
1317
|
+
definitions: FlagEvaluationSnapshot
|
|
1318
|
+
): UnresolvedFlagReason | undefined {
|
|
1319
|
+
if (!flag.active) {
|
|
1320
|
+
return undefined
|
|
1321
|
+
}
|
|
1322
|
+
if (flag.ensure_experience_continuity) {
|
|
1323
|
+
return 'experience_continuity'
|
|
1324
|
+
}
|
|
1325
|
+
const flagFilters = flag.filters || {}
|
|
1326
|
+
const aggregation = flagFilters.aggregation_group_type_index
|
|
1327
|
+
if (aggregation != undefined && !definitions.groupTypeMapping[String(aggregation)]) {
|
|
1328
|
+
return 'unsupported_definition'
|
|
1329
|
+
}
|
|
1330
|
+
const usesUnsupportedProperty = (flagFilters.groups || []).some((condition) =>
|
|
1331
|
+
(condition.properties || []).some((property) => isUnsupportedProperty(property, definitions, new Set()))
|
|
1332
|
+
)
|
|
1333
|
+
return usesUnsupportedProperty ? 'unsupported_definition' : undefined
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
function isUnsupportedProperty(
|
|
1337
|
+
property: FlagProperty,
|
|
1338
|
+
definitions: FlagEvaluationSnapshot,
|
|
1339
|
+
visitedCohortIds: Set<string>
|
|
1340
|
+
): boolean {
|
|
1341
|
+
if (property.type === 'flag') {
|
|
1342
|
+
const dependencyChain = property.dependency_chain
|
|
1343
|
+
if (!Array.isArray(dependencyChain) || dependencyChain.length === 0) {
|
|
1344
|
+
return true
|
|
1345
|
+
}
|
|
1346
|
+
return dependencyChain.some(
|
|
1347
|
+
(key) => !definitions.featureFlagsByKey[key] && !definitions.filteredOutFlagKeys.has(key)
|
|
1348
|
+
)
|
|
1349
|
+
}
|
|
1350
|
+
if (property.type !== 'cohort') {
|
|
1351
|
+
try {
|
|
1352
|
+
assertFeatureFlagFilterSupported(property)
|
|
1353
|
+
return false
|
|
1354
|
+
} catch (e) {
|
|
1355
|
+
if (e instanceof InconclusiveMatchError) {
|
|
1356
|
+
return true
|
|
1357
|
+
}
|
|
1358
|
+
throw e
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
const cohortId = String(property.value)
|
|
1362
|
+
if (!(cohortId in definitions.cohorts)) {
|
|
1363
|
+
return true
|
|
1364
|
+
}
|
|
1365
|
+
if (visitedCohortIds.has(cohortId)) {
|
|
1366
|
+
return false
|
|
1367
|
+
}
|
|
1368
|
+
visitedCohortIds.add(cohortId)
|
|
1369
|
+
return isUnsupportedPropertyGroup(definitions.cohorts[cohortId], definitions, visitedCohortIds)
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
function isUnsupportedPropertyGroup(
|
|
1373
|
+
group: PropertyGroup | undefined,
|
|
1374
|
+
definitions: FlagEvaluationSnapshot,
|
|
1375
|
+
visitedCohortIds: Set<string>
|
|
1376
|
+
): boolean {
|
|
1377
|
+
return (group?.values ?? []).some((value: PropertyGroup | FlagProperty) =>
|
|
1378
|
+
'values' in value
|
|
1379
|
+
? isUnsupportedPropertyGroup(value, definitions, visitedCohortIds)
|
|
1380
|
+
: isUnsupportedProperty(value, definitions, visitedCohortIds)
|
|
1381
|
+
)
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1181
1384
|
function matchProperty(
|
|
1182
1385
|
property: FeatureFlagCondition['properties'][number],
|
|
1183
1386
|
propertyValues: Record<string, any>,
|
|
@@ -1243,7 +1446,7 @@ async function matchPropertyGroup(
|
|
|
1243
1446
|
return true
|
|
1244
1447
|
}
|
|
1245
1448
|
|
|
1246
|
-
let
|
|
1449
|
+
let inconclusiveReason: UnresolvedFlagReason | undefined = undefined
|
|
1247
1450
|
|
|
1248
1451
|
if ('values' in properties[0]) {
|
|
1249
1452
|
// a nested property group
|
|
@@ -1275,15 +1478,15 @@ async function matchPropertyGroup(
|
|
|
1275
1478
|
if (debugMode) {
|
|
1276
1479
|
console.debug(`Failed to compute property ${prop} locally: ${err}`)
|
|
1277
1480
|
}
|
|
1278
|
-
|
|
1481
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, err.reason)
|
|
1279
1482
|
} else {
|
|
1280
1483
|
throw err
|
|
1281
1484
|
}
|
|
1282
1485
|
}
|
|
1283
1486
|
}
|
|
1284
1487
|
|
|
1285
|
-
if (
|
|
1286
|
-
throw new InconclusiveMatchError("Can't match cohort without a given cohort property value")
|
|
1488
|
+
if (inconclusiveReason) {
|
|
1489
|
+
throw new InconclusiveMatchError("Can't match cohort without a given cohort property value", inconclusiveReason)
|
|
1287
1490
|
}
|
|
1288
1491
|
// if we get here, all matched in AND case, or none matched in OR case
|
|
1289
1492
|
return propertyGroupType === 'AND'
|
|
@@ -1338,15 +1541,15 @@ async function matchPropertyGroup(
|
|
|
1338
1541
|
if (debugMode) {
|
|
1339
1542
|
console.debug(`Failed to compute property ${prop} locally: ${err}`)
|
|
1340
1543
|
}
|
|
1341
|
-
|
|
1544
|
+
inconclusiveReason = pickUnresolvedReason(inconclusiveReason, err.reason)
|
|
1342
1545
|
} else {
|
|
1343
1546
|
throw err
|
|
1344
1547
|
}
|
|
1345
1548
|
}
|
|
1346
1549
|
}
|
|
1347
1550
|
|
|
1348
|
-
if (
|
|
1349
|
-
throw new InconclusiveMatchError("can't match cohort without a given cohort property value")
|
|
1551
|
+
if (inconclusiveReason) {
|
|
1552
|
+
throw new InconclusiveMatchError("can't match cohort without a given cohort property value", inconclusiveReason)
|
|
1350
1553
|
}
|
|
1351
1554
|
|
|
1352
1555
|
// if we get here, all matched in AND case, or none matched in OR case
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FeatureFlagValue, JsonType } from '@posthog/core'
|
|
1
|
+
import { FeatureFlagValue, JsonType, UnresolvedFlagReason } from '@posthog/core'
|
|
2
2
|
|
|
3
3
|
import { FeatureFlagError } from './types'
|
|
4
4
|
|
|
@@ -73,6 +73,7 @@ export class FeatureFlagEvaluations {
|
|
|
73
73
|
private readonly _groups: Record<string, string | number> | undefined
|
|
74
74
|
private readonly _disableGeoip: boolean | undefined
|
|
75
75
|
private readonly _flags: Record<string, EvaluatedFlagRecord>
|
|
76
|
+
private readonly _unresolvedFlags: Readonly<Record<string, UnresolvedFlagReason>>
|
|
76
77
|
private readonly _requestId: string | undefined
|
|
77
78
|
private readonly _evaluatedAt: number | undefined
|
|
78
79
|
private readonly _flagDefinitionsLoadedAt: number | undefined
|
|
@@ -92,6 +93,7 @@ export class FeatureFlagEvaluations {
|
|
|
92
93
|
groups?: Record<string, string | number>
|
|
93
94
|
disableGeoip?: boolean
|
|
94
95
|
flags: Record<string, EvaluatedFlagRecord>
|
|
96
|
+
unresolvedFlags?: Record<string, UnresolvedFlagReason>
|
|
95
97
|
requestId?: string
|
|
96
98
|
evaluatedAt?: number
|
|
97
99
|
flagDefinitionsLoadedAt?: number
|
|
@@ -105,6 +107,7 @@ export class FeatureFlagEvaluations {
|
|
|
105
107
|
this._groups = init.groups
|
|
106
108
|
this._disableGeoip = init.disableGeoip
|
|
107
109
|
this._flags = init.flags
|
|
110
|
+
this._unresolvedFlags = Object.freeze({ ...init.unresolvedFlags })
|
|
108
111
|
this._requestId = init.requestId
|
|
109
112
|
this._evaluatedAt = init.evaluatedAt
|
|
110
113
|
this._flagDefinitionsLoadedAt = init.flagDefinitionsLoadedAt
|
|
@@ -216,6 +219,18 @@ export class FeatureFlagEvaluations {
|
|
|
216
219
|
return Object.keys(this._flags)
|
|
217
220
|
}
|
|
218
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Flags that have a loaded local definition but that neither local evaluation nor a remote
|
|
224
|
+
* fallback resolved, mapped to the reason local evaluation was inconclusive. These flags are
|
|
225
|
+
* absent from `keys`, and their accessors return undefined (`isEnabled` returns false). Reading
|
|
226
|
+
* this does not count as an access and does not fire any event.
|
|
227
|
+
*
|
|
228
|
+
* Always empty on snapshots returned by `only()` / `onlyAccessed()`.
|
|
229
|
+
*/
|
|
230
|
+
get unresolvedFlags(): Readonly<Record<string, UnresolvedFlagReason>> {
|
|
231
|
+
return this._unresolvedFlags
|
|
232
|
+
}
|
|
233
|
+
|
|
219
234
|
/**
|
|
220
235
|
* Build the `$feature/*` and `$active_feature_flags` event properties derived
|
|
221
236
|
* from the current flag set. Called by `capture()` when an event is captured
|
|
@@ -247,6 +262,7 @@ export class FeatureFlagEvaluations {
|
|
|
247
262
|
groups: this._groups,
|
|
248
263
|
disableGeoip: this._disableGeoip,
|
|
249
264
|
flags,
|
|
265
|
+
// `unresolvedFlags` is left out on purpose: slices carry no unresolved entries.
|
|
250
266
|
requestId: this._requestId,
|
|
251
267
|
evaluatedAt: this._evaluatedAt,
|
|
252
268
|
flagDefinitionsLoadedAt: this._flagDefinitionsLoadedAt,
|
|
@@ -304,7 +320,8 @@ export class FeatureFlagEvaluations {
|
|
|
304
320
|
|
|
305
321
|
// Build the comma-joined `$feature_flag_error` matching the single-flag path's
|
|
306
322
|
// granularity: response-level errors (errors-while-computing, quota-limited) are
|
|
307
|
-
// combined with per-flag errors (flag-missing) so consumers
|
|
323
|
+
// combined with per-flag errors (flag-missing or local-evaluation-inconclusive) so consumers
|
|
324
|
+
// can filter by type.
|
|
308
325
|
const errors: string[] = []
|
|
309
326
|
if (this._errorsWhileComputing) {
|
|
310
327
|
errors.push(FeatureFlagError.ERRORS_WHILE_COMPUTING)
|
|
@@ -313,7 +330,11 @@ export class FeatureFlagEvaluations {
|
|
|
313
330
|
errors.push(FeatureFlagError.QUOTA_LIMITED)
|
|
314
331
|
}
|
|
315
332
|
if (flag === undefined) {
|
|
316
|
-
errors.push(
|
|
333
|
+
errors.push(
|
|
334
|
+
Object.prototype.hasOwnProperty.call(this._unresolvedFlags, key)
|
|
335
|
+
? FeatureFlagError.LOCAL_EVALUATION_INCONCLUSIVE
|
|
336
|
+
: FeatureFlagError.FLAG_MISSING
|
|
337
|
+
)
|
|
317
338
|
}
|
|
318
339
|
if (errors.length > 0) {
|
|
319
340
|
properties.$feature_flag_error = errors.join(',')
|
package/src/types.ts
CHANGED
|
@@ -455,12 +455,14 @@ export type PostHogFeatureFlag = {
|
|
|
455
455
|
* Error values:
|
|
456
456
|
* ERRORS_WHILE_COMPUTING: Server returned errorsWhileComputingFlags=true
|
|
457
457
|
* FLAG_MISSING: Requested flag not in API response
|
|
458
|
+
* LOCAL_EVALUATION_INCONCLUSIVE: Flag has a loaded local definition but local evaluation could not resolve it
|
|
458
459
|
* QUOTA_LIMITED: Rate/quota limit exceeded
|
|
459
460
|
* UNKNOWN_ERROR: Unexpected exceptions
|
|
460
461
|
*/
|
|
461
462
|
export const FeatureFlagError = {
|
|
462
463
|
ERRORS_WHILE_COMPUTING: 'errors_while_computing_flags',
|
|
463
464
|
FLAG_MISSING: 'flag_missing',
|
|
465
|
+
LOCAL_EVALUATION_INCONCLUSIVE: 'local_evaluation_inconclusive',
|
|
464
466
|
QUOTA_LIMITED: 'quota_limited',
|
|
465
467
|
UNKNOWN_ERROR: 'unknown_error',
|
|
466
468
|
} as const
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "5.
|
|
1
|
+
export const version = "5.57.0"
|