Repository navigation
fix(server): keep dependency work linear after a depth-limit failure - #1026
Conversation
A flag that ran out of depth or budget below it was not cached, so every path to it evaluated it again until the 10,000 budget ran out, and a flag that a shorter path could resolve stayed unresolved. Such a failure is now remembered with the depth it was reached at, and only a shallower path evaluates the flag again. A resolved dependency is cached even when a deeper branch below it ran out of depth. Cycles are still never cached.
…limit from another path A dependency computed while the depth limit or the budget cut off a flag below it, whether it resolved or failed, was reused by every later reference: a fallback variant reached only on a deep path was read by a shallower one, and a remembered failure blocked a reference that could finish once another branch cached the chain's tail. Such a result is now kept with the depth it was reached at and the size of the evaluation cache, and reused only from that depth or deeper while the cache gained no entry. Cycles are still never cached.
dustinbyrne
left a comment
There was a problem hiding this comment.
Two correctness regressions introduced in 6b488836 in FlagEvaluator.computeDependency can return wrong locally evaluated flag values without falling back to /flags (or keep a resolvable flag unresolved) — details and executed base/head regression tests are inline.
Follow-up (non-blocking for this PR): Since the description notes the same design is being ported in PostHog/posthog-dotnet#414 and PostHog/posthog-ruby#404, consider carrying both fixes over there as well (and PostHog/posthog-dotnet main currently still caches depth-limited fallback values in evaluationCache without cache-size tracking).
AI-assisted review.
…ut-off flag is cached A depth-limited failure is reused from its depth or deeper, a value only at its depth, and only after the depth-limit check. Both are invalidated by a resolution version that's read before the flag is evaluated and bumped only when a flag that was cut off, or has a depth-limited result, is cached, so unrelated cached flags no longer force the chain to be walked again.
dustinbyrne
left a comment
There was a problem hiding this comment.
Looks great — 63353120 cleanly fixes both prior findings (depthCutOff ordering, exact-depth value reuse, and pre-evaluation resolutionVersion with depthLimitedKeys so unrelated cached flags don't trigger re-walks). All 4 earlier regression cases and all 33 FeatureEnrollmentTest cases pass locally. One tiny non-blocking comment-wording suggestion inline.
AI-assisted review.
💡 Motivation and Context
Follow-up to #1014, from the same review on the matching PostHog/posthog-dotnet#411. Since #1014, a flag dependency evaluates only the flag it references, with depth (100) and budget (10,000) limits, and a failure caused by those limits isn't cached so a shorter path can still resolve the flag.
That last rule made the work grow with the number of paths, not the number of flags. With a chain deeper than 100 that reaches the same flags again from shorter paths, every path re-evaluated them until the budget ran out, and a flag that a shorter path could resolve stayed unresolved.
Changes
/flags. Both are reused only while a resolution version, read before the flag was evaluated, is unchanged. The version goes up only when a flag that was cut off, or that has a depth-limited result, is cached, so caching unrelated flags doesn't force deep chains to be walked again. Checks run in this order: failure cache, cycle, depth/budget cut-off, depth-limited value at this depth, depth-limited failure, evaluate. Because values and failures are stored separately and values per depth, a deep chain reached alternately from two depths no longer overwrites its own entries and gets re-walked until the budget runs out. The newa chain deeper than the limit reached alternately from two depths is walked once per depthused 10,000 evaluations and failed before this change, and uses 261 now. Cycles are still never cached; behaviour is unchanged when no limit is hit; the linear-work tests still do 1870 and 222 dependency evaluations. The depth limit is an internalFlagEvaluatorconstructor parameter (default 100) so tests can lower it. The same fix is in fix(flags): reuse depth-limited dependency results only where they hold posthog-dotnet#414 and fix(flags): reuse depth-limited dependency results only where they hold posthog-ruby#406.💚 How did you test it?
FeatureEnrollmentTestcasea deep chain reached again from shorter paths resolves with linear work: 160 flags, each depending on the previous flag or the one before it. It failed onmain(the top flag stayed unresolved) and now resolves totruewith a bounded number of dependency evaluations, asserted onFlagEvaluationContext.dependencyEvaluationsrather than time../gradlew :posthog-server:test: 825 tests, the only failures are the 5PostHogTestMockito/JDK 21 tests that fail the same way onmain.spotlessCheckand:posthog-server:apiCheckpass.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)