Skip to content

fix(server): keep dependency work linear after a depth-limit failure - #1026

Merged
marandaneto merged 6 commits into
mainfrom
fix/server-dependency-depth-linear
Dec 2, 2026
Merged

marandaneto merged 6 commits into
mainfrom
fix/server-dependency-depth-linear

Conversation

@marandaneto

@marandaneto marandaneto commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

💡 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

  • A flag that ran out of depth or budget below it is remembered with the depth it was reached at (see the follow-up below for when it's evaluated again), so each flag is evaluated at most once per depth.
  • A failure caused by a cycle is still never cached.
  • Follow-up (review feedback): A dependency result computed while the depth limit (100) or the evaluation budget (10,000) cut off a flag below it, whether a value or a failure, isn't cached as resolved. A value is kept per (flag, depth) and reused only at that exact depth, where evaluating again would give the same result. A failure is kept per flag at the shallowest depth that failed and reused there or deeper. That's a conservative approximation that keeps the work linear: deeper, more of the chain is cut off, so it stays inconclusive and defers to /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 new a chain deeper than the limit reached alternately from two depths is walked once per depth used 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 internal FlagEvaluator constructor 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?

  • New FeatureEnrollmentTest case a 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 on main (the top flag stayed unresolved) and now resolves to true with a bounded number of dependency evaluations, asserted on FlagEvaluationContext.dependencyEvaluations rather than time.
  • ./gradlew :posthog-server:test: 825 tests, the only failures are the 5 PostHogTest Mockito/JDK 21 tests that fail the same way on main. spotlessCheck and :posthog-server:apiCheck pass.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

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.
@marandaneto
marandaneto requested a review from a team as a code owner December 1, 2026 13:09
@marandaneto marandaneto self-assigned this Dec 1, 2026
@veria-ai

veria-ai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed 7391ea5: 2 Should fix.

New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.

…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 dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dustinbyrne
dustinbyrne requested a review from a team December 1, 2026 16:17
…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 dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@marandaneto
marandaneto merged commit aea816b into main Dec 2, 2026
18 checks passed
@marandaneto
marandaneto deleted the fix/server-dependency-depth-linear branch December 2, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants