Repository navigation
fix(flags): reuse depth-limited dependency results only where they hold - #414
Conversation
A dependency that ran out of depth below it and then resolved through a later condition was cached, so a shallower reference read a value that only held on the deep path. A dependency that ran out of depth was also remembered as failed at that depth, so it stayed unresolved after another condition cached the rest of its chain. A result or failure computed while the depth limit or budget cut off a branch below it is now kept outside the evaluation cache, with the depth it was reached at and the cache size when it was recorded. It is reused only at the same depth or deeper while the cache has not grown, and is evaluated again otherwise. A flag computed under such a cut-off is not cached at the top level either. Cycle failures are still never cached, and the evaluation counts of the deep-chain tests are unchanged. Tests can lower the depth limit through an internal MaxDependencyDepth init property.
posthog-dotnet Compliance ReportDate: 2026-12-02T09:34:57.562066+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
SDK performance: posthog-dotnet5 changes worth reviewing: 5 observed regressions. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.
View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Compared 108 metric rows; 108 rows shown in all results. Omitted or shortened data remain in |
dustinbyrne
left a comment
There was a problem hiding this comment.
Separating depth-limited results from evaluationCache fixes the two targeted scenarios cleanly, and the PostHog / PostHog.AspNetCore changesets and internal MaxDependencyDepth hook look good. Left three concerns inline before approval (two regressions relative to main in EvaluateAllFlags budget exhaustion and cycle-detection ordering, plus reusing non-failure depth-limited values at deeper depths), along with two optional suggestions on cache-invalidation edge cases.
AI-assisted review.
…ut-off resolves A depth-limited value was reused deeper than where it was computed, even past the depth limit, and before the cycle check, so a cycle re-entering a flag read an earlier fallback instead of being detected. Results were invalidated by the evaluation cache size, which a flag's own later condition could grow unnoticed, a server-only failure did not grow at all, and every unrelated flag of a bulk evaluation grew, so a chain deeper than the limit was walked again per flag until the budget ran out and healthy flags were left unresolved. Checks now run in order: path-independent failures, cycles, depth limit and budget, then depth-limited results. A resolution version replaces the cache size: it changes only when a flag that was cut off or has a depth-limited result is cached, at the top level too, or recorded as a path-independent failure. The version is read before the flag is evaluated. A failure is reused at the same depth or deeper and a value only at the same depth, and neither is stored when a cycle was hit below it.
dustinbyrne
left a comment
There was a problem hiding this comment.
9007dd60 cleanly resolves all five earlier findings (confirmed locally: all 23 SkippedTargetingDependencies tests and all 7 Round-1 regression tests now pass). Left one new concern inline: pairing exact-depth value reuse with a single-slot DepthLimitedResults[flagKey] dictionary causes alternating-depth thrashing in bulk evaluation and can exhaust the 10,000 dependency budget for unrelated healthy flags. Also left two optional suggestions on CycleHits cache-guard symmetry and trimming the unshipped Round-1 regression from the changesets.
AI-assisted review.
…hallowest depth Values computed under a depth or budget cut-off are stored per (flag key, depth), and failures per flag key at the shallowest failing depth of the current resolution version, so references to a deep fallback chain from different depths no longer evict each other's result and re-walk the chain until the dependency budget runs out.
…ogress Both the top-level and the dependency evaluation cache writes now skip a value whose evaluation hit a cycle, matching the failure path and the other SDKs. The value is still returned.
dustinbyrne
left a comment
There was a problem hiding this comment.
All earlier review items are cleanly addressed in d2297e3 and a59d0a6 — splitting depth-limited values per (flagKey, depth) from shallowest-depth failures eliminates the alternating-depth bulk thrashing, both evaluationCache success writes now guard on CycleHits, and both changesets accurately reflect the delta against main. Verified locally (25/25 SkippedTargetingDependencies tests passing) alongside 24/24 green CI checks. Looks good to merge.
AI-assisted review.
|
Re the SDK performance report: the ~9% (~170 B/op) allocation increase on single-flag evaluation came from |
💡 Motivation and Context
Follow-up to #411. Near the depth limit of 100, a dependency whose branch was cut off and which then resolved through a later condition was cached, so a shorter path read a value that only held on the deep path. A cut-off dependency also stayed unresolved after the rest of its chain was resolved. The fix keeps such results (values or failures) out of the shared cache, in a per-evaluation map that stores the entry depth and a resolution version. A failure is reused at the same depth or deeper; a value only at the same depth. Either is reused only while the version is unchanged, and the version moves only when a cut-off or depth-limited key is later resolved (cached, or recorded as a path-independent failure). Checks run in this order: failures → cycle → depth/budget → depth-limited lookup. Results with a cycle beneath them are never stored. Behavior without depth or budget hits is unchanged, and unrelated flags in a bulk pass no longer re-trigger the deep chain.
The same fix is in PostHog/posthog-android#1026 and PostHog/posthog-ruby#406 (a follow-up to the merged PostHog/posthog-ruby#404).
💚 How did you test it?
tests/UnitTests/Features/FeatureEnrollmentTests.cs, written first and red on the previous caching, green now:ValueResolvedThroughAFallbackAfterTheDepthLimitIsNotReusedFromAShallowerPath,DepthLimitFailureIsEvaluatedAgainAtTheSameDepthOnceItsTailIsCachedDepthLimitedValueIsNotReusedAtADeeperDepthWithinLimit,DepthLimitedValueIsNotReusedPastTheLimit,DepthLimitedValueIsEvaluatedAgainOnceItsOwnLaterConditionCachedTheTailInterleavedUnrelatedFlagsDoNotExhaustDependencyBudgetForHealthyFlags(bulk pass, not ordered dependencies-first: 10,317 dependency evaluations before, 101 now)ServerOnlyFailureOfACutOffDependencyInvalidatesADepthLimitedValueDepthLimitedValueDoesNotBypassCycleDetectionInBulkEvaluation,DepthLimitedValueDoesNotBypassSelfCycleDetectionInBulkEvaluation,DepthLimitedValueComputedUnderACycleIsNotReusedAlternatingDepthsAboveADepthLimitedFallbackChainDoNotExhaustDependencyBudgetForHealthyFlags(a chain deeper than the limit referenced alternately from two depths in one bulk pass: 10,283 dependency evaluations before, 260 now)ValueComputedUnderACycleWithoutDepthLimitIsNotReuseddotnet format --verify-no-changes.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Implemented with a Claude coding agent following the maintainer's cross-SDK rule for depth-limited dependency results, the same rule as PostHog/posthog-ruby#404 and PostHog/posthog-android#1026. The new tests were written first and shown failing against the #411 caching. The "generation" is the evaluation-cache size, read when a depth-tagged result is recorded rather than when its evaluation starts, so entries cached by its own subtree don't force a re-evaluation; this kept the deep-chain evaluation counts unchanged. The depth limit is lowered for tests only through an internal init property.