Skip to content

fix(flags): clear the flag-definition ETag when definitions are reset (sdk-specs flag-definition-loader) - #825

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/clear-flag-definition-etag-on-reset
Draft

fix(flags): clear the flag-definition ETag when definitions are reset (sdk-specs flag-definition-loader)#825
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/clear-flag-definition-etag-on-reset

Conversation

@posthog

@posthog posthog Bot commented Aug 3, 2026

Copy link
Copy Markdown

💡 Motivation and Context

Compliance gap against the cross-SDK contract in PostHog/sdk-specs.

openspec/specs/flag-definition-loader/spec.md, Behavior #10 — "Clear definitions on explicit reset/clear":

When local evaluation is disabled, quota limited, or reset, the loader drops the in-memory definitions and associated ETag state.

The same spec lists the ETag/cache validator under State written, and Behavior #5 is what makes the omission bite: "A 304 keeps current definitions."

What was out of compliance. _fetch_feature_flags_from_api() dropped the definitions on a 402 (quota limited) or 401 (unauthorized) definitions response, but never cleared _flags_etag — that attribute was only ever written on a successful fetch, and nothing else in the SDK reset it. So after a reset the next poll still sent If-None-Match: <stale etag>. Once the block lifted (quota reset, key fixed) the server saw an unchanged definition set, answered 304, and the loader took its "flags not modified, using cached data" early return — except there was no cached data left. Local evaluation stayed permanently empty until someone happened to edit a flag server-side.

How this fixes it. The reset now goes through a _reset_flag_definitions() helper that clears feature_flags / group_type_mapping / cohorts and _flags_etag, so the recovery poll is unconditional and repopulates the definitions. Both error handlers call it.

Behavior change / compatibility risk: minimal. The only observable difference is one unconditional (non-304-eligible) definitions request after a 401/402 reset, in exchange for local evaluation actually recovering. No public API, config, event, or property changes. Transient failures (503, timeouts) are untouched and still keep the last known good definitions and their ETag, per the spec's "Transient failures keep the last known good definitions" error-handling rule.

Scoped deliberately to the loader's reset paths. The disabled / missing-secret-key early returns in load_feature_flags() also zero feature_flags, but they return before the loader ever runs, so a stale ETag can't be acted on there; I left them alone rather than widen the diff.

💚 How did you test it?

Added test_load_feature_flags_clears_etag_when_definitions_are_reset, parameterized over both reset paths (402 and 401). It loads definitions with an ETag, takes the error response, asserts the definitions and the ETag are gone, then asserts the recovery poll sends no etag and repopulates the definitions.

Verified the test fails on main's behavior (AssertionError: '"etag-v1"' is not None) and passes with the fix. Also ran posthog/test/test_feature_flags.py, test_flag_definition_cache.py, test_feature_flag_called_minimization.py (214 passed) and test_client.py (152 passed), plus ruff format --check, ruff check, and mypy on posthog/client.py. No manual/integration testing against a live PostHog instance.

📝 Checklist

  • 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 (added by hand at .sampo/changesets/clear-flag-definition-etag-on-reset.md, matching the existing format)

🤖 Agent context

Autonomy: Fully autonomous

Opened by the scheduled "SDK Spec Compliance Enforcer" loop for posthog-python, running in PostHog Code (Claude Code harness). The loop reads PostHog/sdk-specs as the source of truth each run, audits the Python SDK against the contracts in scope for a server SDK, and opens one focused draft PR per confirmed divergence.

This run swept 30-ish in-scope specs across six parallel read-only audit agents (capture/identify/alias/groups; flag getters; batcher/flush/http/retry; local evaluation + definition loader; exceptions/before-send/tracing headers; logs/traces). This finding was picked over the alternatives because the spec language is unambiguous ("and associated ETag state"), the failure mode is a reproducible stuck-empty state rather than a stylistic difference, and the fix carries essentially no backward-compatibility risk. Candidates deliberately not acted on this run, for the reviewer's awareness: missing input validation on alias() / group_identify() (spec has explicit @both drop-and-warn scenarios, but fixing it stops emitting events some users may be counting today), inverted frame order in the synthesized traceback for never-raised exceptions (would re-fingerprint existing issues), and payload-type inconsistency between locally- and remotely-evaluated flags (str vs parsed). The module-level get_feature_flag_payload default was skipped as already-decided — #794 was closed wontfix.

Agent-authored, so no human co-author is claimed, and it needs human review before merge.


Created with PostHog Code

The sdk-specs flag-definition-loader contract requires a reset to drop the
in-memory definitions *and* the associated ETag state. Python cleared the
definitions on a 402 (quota limited) or 401 (unauthorized) definitions
response but kept `_flags_etag`, so the next poll sent `If-None-Match` for
definitions the SDK no longer held. The server answered 304, the loader took
its "definitions unchanged" early return, and local evaluation stayed empty
until the definitions changed server-side.

Fold the reset into a `_reset_flag_definitions()` helper that also clears the
ETag, and use it from both error handlers.

Generated-By: PostHog Code
Task-Id: b2f32a21-8d44-4644-b548-60a2da6d1067
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-08-03 08:00:21 UTC
Duration: 338858ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 517ms
Endpoint And Method.Does Not Use Legacy Endpoints 1010ms
Required Headers.Has Authorization Bearer Header 1009ms
Required Headers.Has Content Type Json 1009ms
Required Headers.Has Posthog Sdk Info Format 1009ms
Required Headers.Has Posthog Attempt Header 1010ms
Required Headers.Has Posthog Request Id 1007ms
Required Headers.Has Posthog Request Timestamp 1009ms
Required Headers.Has User Agent 1009ms
Body Format.Body Has Created At And Batch 1009ms
Body Format.No Api Key In Body 1009ms
Body Format.No Sent At In Body 1008ms
Event Format.Event Has Required Root Fields 1009ms
Event Format.Event Uuid Is Valid 1008ms
Event Format.Event Timestamp Is Rfc3339 1009ms
Event Format.Distinct Id Is String 1010ms
Event Format.Distinct Id At Root Not Properties 1010ms
Event Format.Custom Properties Preserved 1008ms
Event Format.Set Properties Preserved 1008ms
Event Format.Set Once Properties Preserved 1009ms
Event Format.Groups Properties Preserved 1011ms
Event Format.Sdk Generates Uuid If Not Provided 1010ms
Event Format.Event Has Required Root Fields Batch 1012ms
Event Format.Event Uuid Is Valid Batch 1012ms
Event Format.Event Timestamp Is Rfc3339 Batch 1012ms
Event Format.Distinct Id Is String Batch 1012ms
Event Format.Distinct Id At Root Not Properties Batch 1013ms
Event Format.Custom Properties Preserved Batch 1012ms
Event Format.Set Properties Preserved Batch 1012ms
Event Format.Set Once Properties Preserved Batch 1011ms
Event Format.Groups Properties Preserved Batch 1013ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1012ms
Batch Behavior.Multiple Events In Single Batch 1508ms
Batch Behavior.Batch Envelope Smoke 1013ms
Batch Behavior.Flush With No Events Sends Nothing 1006ms
Batch Behavior.Flush At Triggers Batch 1508ms
Batch Behavior.Created At Reflects Batch Creation Time 1011ms
Deduplication.Generates Unique Uuids 1507ms
Deduplication.Different Events Same Content Different Uuids 1509ms
Deduplication.Preserves Uuid On Retry 7511ms
Deduplication.Preserves Timestamp On Retry 7514ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7515ms
Deduplication.No Duplicate Events In Batch 1504ms
Header Behavior On Retry.Attempt Header Starts At One 1009ms
Header Behavior On Retry.Attempt Header Increments On Retry 14516ms
Header Behavior On Retry.Request Id Preserved On Retry 7512ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3520ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7514ms
Response Format Validation.Success Response Has Uuid Keyed Results 1006ms
Response Format Validation.Success Response Has Ok For Each Event 1507ms
Response Format Validation.Success No Retry After When All Ok 1507ms
Response Format Validation.Success Retry After Present When Retry Events 2510ms
Response Format Validation.Success No Retry After When Drop Only 1508ms
Response Format Validation.Response Echoes Request Id 1008ms
Retry Behavior.Retries On 408 7516ms
Retry Behavior.Retries On 500 7511ms
Retry Behavior.Retries On 503 9514ms
Retry Behavior.Retries On 504 7512ms
Retry Behavior.Retryable Errors Have Retry After 4512ms
Retry Behavior.Respects Retry After On Retryable Error 12517ms
Retry Behavior.Does Not Retry On 400 3503ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Does Not Retry On 402 3509ms
Retry Behavior.Does Not Retry On 413 3507ms
Retry Behavior.Does Not Retry On 415 3507ms
Retry Behavior.Non Retryable Errors Have No Retry After 3508ms
Retry Behavior.Implements Backoff 23532ms
Retry Behavior.Max Retries Respected 23522ms
Partial Batch Handling.Handles 200 Full Success 3002ms
Partial Batch Handling.Handles 200 With All Ok 4508ms
Partial Batch Handling.Does Not Retry Dropped Events 4507ms
Partial Batch Handling.Does Not Retry Limited Events 4508ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7515ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7508ms
Partial Batch Handling.Retries Only Retry Events From Partial 7512ms
Partial Batch Handling.Partial Retry Preserves Uuids 7513ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7516ms
Partial Batch Handling.Partial Retry Request Id Preserved 7507ms
Partial Batch Handling.Respects Retry After On Partial 9517ms
Partial Batch Handling.Unknown Result Treated As Terminal 4506ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4509ms
Compression.Sends Gzip Content Encoding 1008ms
Compression.No Content Encoding When Disabled 1008ms
Compression.Compressed Body Is Decompressible 1009ms
Error Handling.Does Not Retry On Unknown 4Xx 3510ms
Event Options.Cookieless Mode Override 1007ms
Event Options.Disable Skew Correction Override 1009ms
Event Options.Process Person Profile Override 1009ms
Event Options.Product Tour Id Override 1010ms
Event Options.Unset Options Omitted 1009ms
Event Options.Options Override In Batch 1012ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1009ms
Geoip And Historical Migration.Historical Migration Set In Body 1008ms
Geoip And Historical Migration.Historical Migration Absent By Default 1009ms

Feature_Flags Tests

17/17 tests passed

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

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.

0 participants