Skip to content

fix(flags): request flags /flags already returned without waiting on other callers - #260

Merged
marandaneto merged 8 commits into
mainfrom
fix/flags-skip-coordination-known-present
Nov 20, 2026
Merged

marandaneto merged 8 commits into
mainfrom
fix/flags-skip-coordination-known-present

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

Follows @dustinbyrne's suggestion on #259: #259 (comment)

#258 coordinates evaluate_flags requests for flags that aren't in the local definitions, so concurrent callers share one /flags existence check. The SDK already remembers keys that /flags cleanly said don't exist. It did not use the fact that /flags returned a key. Those keys still went through coordination, so a caller could wait up to 2 seconds behind another caller's in-flight request, even though the flag was known to exist and each caller needs its own /flags result anyway.

This is common for flags created since the last definitions poll. Every new batch of callers for such a flag waited behind one request.

Changes

Each flag key now has one of three states until the next definitions refresh:

Notes:

  • Presence comes from the returned-key history fix(flags): stop missing-flag probes from queueing across the cluster #258 already keeps. It has the same 1,000-key limit and is cleared on every successful definitions refresh. A key dropped from the history becomes unknown again and is coordinated.
  • A known-present key is still sent with each request, so a later clean omission can record it as missing. A deleted flag moves to missing instead of staying present until the next refresh.
  • Only whether a flag exists is shared. Each caller still makes its own /flags request for its own context.
  • The evaluate_flags docs mention the new behavior.

💚 How did you test it?

  • The full test suite and mix credo --strict pass.
  • New tests in missing_key_knowledge_test.exs failed before the change:
    • A caller asking for a key /flags already returned makes its request while another caller's request for that key is still in flight.
    • A clean omission of a known-present key records it as missing, and later calls skip the request.
    • A definitions refresh forgets known-present keys.
    • A request with more unknown keys than the coordination limit still updates a known-present key it omits.
  • Autoreview reported no findings on the final head.

📝 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 sampo add to generate a changeset file

Spec

PostHog/sdk-specs#231 is being updated to allow SDKs to remember confirmed-present keys and skip coordination for them.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Implemented with Pi (Claude). Autoreview with GPT found that requests over the coordination limit dropped known-present keys, so a deleted flag could stay marked present. That's fixed with a regression test.
  • This replaces the shared-retry approach in fix(flags): give waiting callers one shared retry for inconclusive missing-flag requests #259. That PR adds about 1,200 lines and only helps while /flags returns errors. This change is small and helps every later batch of callers for a flag that exists.

@marandaneto marandaneto self-assigned this Nov 19, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

posthog-elixir Compliance Report

Date: 2026-11-20T09:10:00.141751+00:00
Duration: 111108ms

✅ All Tests Passed!

47/47 tests passed


Capture Tests

✅ 30/30 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields ✅ 610ms
Format Validation.Event Has Uuid ✅ 610ms
Format Validation.Event Has Lib Properties ✅ 609ms
Format Validation.Distinct Id Is String ✅ 610ms
Format Validation.Token Is Present ✅ 610ms
Format Validation.Custom Properties Preserved ✅ 609ms
Format Validation.Event Has Timestamp ✅ 609ms
Format Validation.Non Utc Event Timestamp Is Converted To Utc ✅ 607ms
Retry Behavior.Retries On 503 ✅ 5614ms
Retry Behavior.Does Not Retry On 400 ✅ 2611ms
Retry Behavior.Does Not Retry On 401 ✅ 2610ms
Retry Behavior.Respects Retry After Header ✅ 5615ms
Retry Behavior.Implements Backoff ✅ 15626ms
Retry Behavior.Retries On 500 ✅ 5614ms
Retry Behavior.Retries On 502 ✅ 5615ms
Retry Behavior.Retries On 504 ✅ 5614ms
Retry Behavior.Max Retries Respected ✅ 15611ms
Deduplication.Generates Unique Uuids ✅ 620ms
Deduplication.Preserves Uuid On Retry ✅ 5613ms
Deduplication.Preserves Uuid And Timestamp On Retry ✅ 10616ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 5616ms
Deduplication.No Duplicate Events In Batch ✅ 614ms
Deduplication.Different Events Have Different Uuids ✅ 612ms
Compression.Sends Gzip When Enabled ✅ 610ms
Batch Format.Uses Proper Batch Structure ✅ 609ms
Batch Format.Flush With No Events Sends Nothing ✅ 607ms
Batch Format.Multiple Events Batched Together ✅ 614ms
Error Handling.Does Not Retry On 403 ✅ 2612ms
Error Handling.Does Not Retry On 413 ✅ 2613ms
Error Handling.Retries On 408 ✅ 5615ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 9ms
Request Payload.Flags Request Uses V2 Query Param ✅ 6ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 6ms
Request Payload.Flags Request Omits Authorization Header ✅ 5ms
Request Payload.Token In Flags Body Matches Init ✅ 6ms
Request Payload.Groups Round Trip ✅ 5ms
Request Payload.Groups Default To Empty Object ✅ 5ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 6ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 5ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 6ms
Request Lifecycle.No Flags Request On Init Alone ✅ 2ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 607ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 10ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 608ms
Retry Behavior.Retries Flags On 502 ✅ 912ms
Retry Behavior.Retries Flags On 504 ✅ 912ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 1213ms

@veria-ai

veria-ai Bot commented Nov 19, 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

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed 3a45676: 1 Should fix.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

@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.

The exact-capacity fix now clears stale presence, and the known-present fast path still works. The same fix can also erase the return history that protects against an older omission; I reproduced a skipped /flags request that would have returned the flag. The mixed-scope refresh finding also still reproduces. Consider keeping presence separate from ordering history, rechecking key state after a wait, and aligning the transition with PostHog/sdk-specs#231 before release.

AI-assisted review.

Comment thread lib/posthog/feature_flags.ex Resolved
@dustinbyrne
dustinbyrne requested a review from a team November 19, 2026 17:47

@ioannisj ioannisj 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.

🤖 An AI agent wrote this on behalf of @ioannisj.

Nothing from me, this looks good. The known-present path matches the positive-knowledge rule in the evaluate-flags spec, and the later coordination rounds stay under the one shared deadline.

@marandaneto
marandaneto merged commit d837a95 into main Nov 20, 2026
30 checks passed
@marandaneto
marandaneto deleted the fix/flags-skip-coordination-known-present branch November 20, 2026 12:55
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.

3 participants