Skip to content

feat(flags): choose the local evaluator by each flag's config version - #1295

Open
andehen wants to merge 11 commits into
feat/flag-rules-v2-local-evaluatorfrom
feat/flag-rules-v2-local-dispatch
Open

andehen wants to merge 11 commits into
feat/flag-rules-v2-local-evaluatorfrom
feat/flag-rules-v2-local-dispatch

Conversation

@andehen

@andehen andehen commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Stacked on #1250, which adds an evaluator for flags whose filters.version is 2 and does not call it. This PR calls it. It stays inert: the definitions request is unchanged (no v=2), and the definitions the server sends today leave these flags out, so none arrives from the API. A later PR requests them.

  • Dispatch. Local evaluation chooses the evaluator of each loaded definition by its config version, in _compute_flag_locally. filters.version absent or 1 runs the version 1 evaluator, unchanged. Any other value goes to the new evaluator, whose closed parser accepts only 2. Each such document is parsed once, when definitions load.
  • Same record as remote. A local result is the record a /flags?v=4 response gives, for single reads and evaluate_flags() snapshots. The existing event step then sends $feature_flag_called with the rule attribution, and $experiment_exposure for a complete split, with locally_evaluated: true.
  • Never a guess. An unsupported document, a missing property, an invalid pattern or an unusable property value never reads as False or the default. The flag falls back to /flags on its own, or is listed in unresolved_flags with only_evaluate_locally=True, as an inconclusive version 1 flag is.
  • Flag dependencies. A version 1 flag that depends on a config version 2 flag goes to the server; the dependency is never read as version 1. Config version 2 documents with flag filters are rejected by the parser.
  • Definition cache. flags keeps the version 1 definitions; other definitions go to a separate flags_v2 field, so an older release that shares the cache never evaluates one. The field is left out when empty, so the cached data of a project without such flags is unchanged. The name is provisional and private until the sdk-specs change for local evaluation names it.

Evaluator inputs: the person properties are never treated as complete, so a filter on a property the call did not pass falls back; exact matching follows property_matching_version; dates without an offset and relative dates are read in UTC, as version 1 local evaluation reads them; the distinct ID is read as a string; experience continuity and device bucketing do not apply, because the flags service evaluates these flags by distinct ID only.

No published sdk-spec covers local evaluation of this format yet; the draft is PostHog/sdk-specs#244, which uses the same flags_v2 cache field. The contract of posthog-sdk-test-harness 1.36.0 (contract 10.1.0) is the cross-SDK reference.

💚 How did you test it?

posthog/test/test_feature_flags_v2_dispatch.py (tests written first; they failed before the change):

  • version 1 definitions (absent, 1, 1.0) never reach the new evaluator; every other version does;
  • boolean, string, number and object values through every single, bulk and snapshot read, with no /flags request; the local record equals the record the v4 reader builds, also for an integral id or version such as 41.0; object values are copies;
  • inactive flags read as disabled with reason flag_disabled; each undecided case falls back per flag in the default mode and is unresolved, with its reason, when evaluating only locally;
  • the contract's whole definitions feed (definitions_feed.json): each entry gets the evaluator and the exact result it states, and in one read of the feed only the 3 unsupported entries fall back (default mode) or are unresolved with unsupported_definition (only_evaluate_locally), while every other entry, version 1 included, keeps its local result;
  • the dependency path; the load-time warning; both events for single and snapshot reads, with dedupe shared with a remote read of the same assignment;
  • the cache field on write and read, and unchanged cache data without such flags; definitions read from the cache keep the fingerprint of the client that fetched them, in any order, and version 1 definitions keep the fingerprint older releases give them;
  • the boolean, value, experiment and local evaluation corpora replayed through the client: 210 results match their corpus expectation exactly, events included; 44 undecided cases fall back with the reason local_outcomes.json gives (36 errors and parse errors, and 8 cases that need a complete property map); 8 cases set in another timezone are left to the evaluator's own replay.

Also run locally on Python 3.14: ruff format --check ., ruff check ., mypy with the baseline, make public_api_check (unchanged), python -W error -c "import posthog", the full suite, and 18 hand-made mutations of the change (for example a complete property map, no copy of an object value, no cache split, a dropped dependency check), each caught by the new tests.

📝 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

A patch changeset, so that this step is released on its own before the request change: a regression in the version 1 paths it touches then shows up without config version 2 flags in play, and releases that read the new cache field exist before any release writes it.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Written with Claude Code from a brief by the assignee, who has not reviewed the code yet.
  • chore(flags): add an evaluator for config version 2 flags, not yet called #1250's branch had main (8.x) merged into it first, with a merge commit, because this change edits the 8.x client. Its re-vendor to harness 1.36.0 is merged in the same way.
  • The record is built by passing a v4-shaped record through the existing v4 parser, so local and remote records cannot drift apart.

🤖 Generated with Claude Code

@andehen andehen self-assigned this Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks completed. No changes flagged for review.

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
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 133.67 KiB/op 133.67 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,345.53 count/op 1,345.53 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 106.04 µs/op 108.09 µs/op 1.9% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 37.74 MiB 38.02 MiB 0.7% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 40.19 MiB 40.52 MiB 0.8% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 40.19 MiB 40.52 MiB 0.8% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 106.25 µs/op 108.37 µs/op 2% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.5 MiB 40.67 MiB 0.4% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 185.76 KiB/op 185.75 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,337.57 count/op 1,337.55 count/op <0.1% fewer Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 148.54 µs/op 143.43 µs/op 3.4% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.39 MiB 41.69 MiB 0.7% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 47.18 MiB 47.51 MiB 0.7% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 47.18 MiB 47.51 MiB 0.7% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 140.65 µs/op 134.95 µs/op 4% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.59 MiB 51.79 MiB 0.4% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 138.74 KiB/op 138.74 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,390.42 count/op 1,390.45 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 122.57 µs/op 121.28 µs/op 1% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 37.65 MiB 37.94 MiB 0.8% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 37.83 MiB 38.14 MiB 0.8% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 37.83 MiB 38.14 MiB 0.8% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 120.56 µs/op 118.61 µs/op 1.6% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.5 MiB 40.5 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 190.92 KiB/op 190.92 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,382.36 count/op 1,382.38 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 150.99 µs/op 156.69 µs/op 3.8% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 41.15 MiB 41.53 MiB 0.9% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 41.61 MiB 42.03 MiB 1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 41.21 MiB 41.6 MiB 1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 143.28 µs/op 149.83 µs/op 4.6% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.54 MiB 41.87 MiB 0.8% more memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 76.9 µs/op 78.01 µs/op 1.4% more time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 78.72 µs/op 80.73 µs/op 2.6% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 75.76 µs/op 84.51 µs/op 11.5% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 75.89 µs/op 85.21 µs/op 12.3% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 225.43 KiB/op 229.39 KiB/op 1.8% more memory Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,279.42 count/op 2,330.43 count/op 2.2% more Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 82.25 µs/op 84.6 µs/op 2.9% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 82.25 µs/op 84.61 µs/op 2.9% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.21 MiB/op 1.8% more memory Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,413.69 count/op 22,914.75 count/op 2.2% more Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 778.37 µs/op 786.23 µs/op 1% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 778.34 µs/op 786.23 µs/op 1% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.63 MiB/op 22.01 MiB/op 1.8% more memory Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 223,655.44 count/op 228,656.5 count/op 2.2% more Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 7.66 ms/op 7.83 ms/op 2.2% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 7.66 ms/op 7.83 ms/op 2.2% more time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 43.84 MiB 43.81 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 44.17 MiB 44.12 MiB 0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 44.17 MiB 44.12 MiB 0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43.97 MiB 43.83 MiB 0.3% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 43 MiB 43.45 MiB 1.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 43.01 MiB 43.48 MiB 1.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 43.01 MiB 43.48 MiB 1.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 42.77 MiB 43.3 MiB 1.2% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 15.74 KiB/op 16.18 KiB/op 2.8% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 162.77 count/op 168.77 count/op 3.7% more Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 12.77 µs/op 13.03 µs/op 2.1% more time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 12.77 µs/op 13.03 µs/op 2.1% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 24.6 KiB/op 25.03 KiB/op 1.8% more memory Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 239.28 count/op 245.28 count/op 2.5% more Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 21.85 µs/op 22.22 µs/op 1.7% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 21.85 µs/op 22.22 µs/op 1.7% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 39.81 KiB/op 40.25 KiB/op 1.1% more memory Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 388.27 count/op 394.27 count/op 1.5% more Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 27.01 µs/op 27.66 µs/op 2.4% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 27.01 µs/op 27.65 µs/op 2.4% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 60.59 KiB/op 61.03 KiB/op 0.7% more memory Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 584.27 count/op 590.27 count/op 1% more Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 38.04 µs/op 38.62 µs/op 1.5% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 38.03 µs/op 38.62 µs/op 1.5% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 59.22 KiB/op 59.66 KiB/op 0.7% more memory Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 567.77 count/op 573.77 count/op 1.1% more Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 33.98 µs/op 34.24 µs/op 0.8% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 33.97 µs/op 34.24 µs/op 0.8% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.04 KiB/op 19.48 KiB/op 2.3% more memory Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 192.27 count/op 198.27 count/op 3.1% more Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 15.74 µs/op 16.29 µs/op 3.5% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 15.74 µs/op 16.29 µs/op 3.5% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 20.66 KiB/op 21.1 KiB/op 2.1% more memory Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 197.77 count/op 203.77 count/op 3% more Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 16.35 µs/op 17.06 µs/op 4.3% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 16.35 µs/op 17.06 µs/op 4.3% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 14.89 KiB/op 15.33 KiB/op 2.9% more memory Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 152.77 count/op 158.77 count/op 3.9% more Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 11.9 µs/op 12.18 µs/op 2.4% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 11.9 µs/op 12.17 µs/op 2.3% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 15.9 KiB/op 16.34 KiB/op 2.8% more memory Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 166.77 count/op 172.77 count/op 3.6% more Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 13.22 µs/op 13.59 µs/op 2.8% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 13.22 µs/op 13.59 µs/op 2.8% more time Not flagged

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.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared 83eb5affdac63d141b3a9ce29346ccc5a1c4ec82 → b146f8aba7dc269db0c6d774c3142982c56b1810.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

Local evaluation now evaluates a loaded definition whose `filters.version`
is not absent or 1 with the config version 2 evaluator. Version 1
definitions keep the version 1 evaluator. The definitions request is
unchanged, so no such definition arrives from the API yet.

- `_compute_flag_locally` dispatches on the config version. Each config
  version 2 document is parsed once, when definitions load.
- A local result is the record a `/flags?v=4` response gives, for single
  reads and `evaluate_flags()` snapshots, so the existing event step sends
  `$feature_flag_called` and, for a complete split, `$experiment_exposure`,
  with `locally_evaluated` true.
- An unsupported document or an undecided rule falls back to `/flags` per
  flag, or is unresolved with `only_evaluate_locally=True`. It never reads
  as false or the default.
- A version 1 flag that depends on a config version 2 flag goes to the
  server instead of reading the dependency as version 1.
- The external definition cache keeps version 1 definitions in `flags` and
  puts the others in a separate `flags_v2` field, left out when empty.
- Tests cover each read path, the fallbacks, the dependency path, both
  events, the cache field, and the boolean, value and experiment corpora
  replayed through the client.
- Hash the definitions with the version 1 definitions first, in the order
  the definition cache stores them. A client that loads them from the
  cache then gives them the same fingerprint as the client that fetched
  them, so it accepts the flag results that client cached. Version 1
  definitions keep their order, so their fingerprint is unchanged.
- Keep an integral `id` or `version`, such as 41.0, in the local record of
  a config version 2 flag, as the v4 reader keeps it.
- Build the number type tuple of the config version check once.
No behaviour change.

- Split the definitions by config version in one helper, used both to
  store them in the definition cache and to hash them in that order.
- Read a local record's `id` and `version` with the v4 reader's own
  integer rule.
- Read the parsed definitions from the snapshot directly, since every
  snapshot holds them.
- Shorten `_parse_definitions`, `_unresolved_reason`,
  `_with_cached_v2_flags` and the dependency format check.
- Say in the event helper's docstring that a local config version 2 flag
  passes its record.
- Tests: share the client that loads definitions from the cache, and drop
  an unused constant and a redundant check.
@andehen
andehen force-pushed the feat/flag-rules-v2-local-dispatch branch from e9c905d to ddfdf44 Compare November 23, 2026 14:00
…s-v2-local-dispatch

Brings in the contract files of posthog-sdk-test-harness 1.36.0 (contract
10.1.0) and the evaluator replay that the contract's local outcome mapping
drives.

The evaluator test no longer has `OMITTED`; it exports the cases a local
evaluator runs as `REPLAYED`. The client replay now takes those, which
include the 8 cases of `v2_local_evaluation.json`. Through the client, 7 of
them give their result and 1 falls back with `missing_context`, so the
pinned counts move from (203, 8, 35, 8) to (210, 8, 36, 8).
The contract of posthog-sdk-test-harness 1.36.0 has a whole definitions
feed that states the outcome a local reader gives each entry, and a
mapping from each evaluation case's expected result to its unresolved
reason.

- Each feed entry goes to the evaluator the contract names. A config
  version 2 entry gives the exact expected value, reason, rule and
  variant, and an unsupported entry is inconclusive with
  `unsupported_definition`.
- One read of the whole feed in each mode. In the default mode only the 3
  unsupported entries come from `/flags`. With `only_evaluate_locally`
  they are unresolved with `unsupported_definition`. Every other entry,
  version 1 included, is evaluated locally and keeps its result.
- These tests replace the invented mixed feed test.
- The client replay takes the unresolved reason of an undecided corpus
  case from `local_outcomes.json` instead of its own table.
…on 2 read

Review fixes for the config version dispatch.

- A single read of a locally evaluated config version 2 flag now sends the
  ID and version of its record in `$feature_flag_called` and
  `$experiment_exposure`, as a snapshot read already did. The single read
  took them from the raw definition instead, so a stored `41.0` was sent as
  `41.0` and a value that is not an integer was sent as it was stored,
  where the record reads `41.0` as `41` and leaves other values out, as the
  v4 reader does. Version 1 reads are unchanged.
- The tests' check that a read makes no `/flags` request now runs after the
  reads. The client catches the error of a failed request, so the error the
  mock raised never reached the test, and a read that fell back remotely
  could pass. Every existing use of the check still passes.
The definitions feed replay added the rule member only when a record
had a rule ID, so a record with a rule type or condition index but no
rule ID still matched an expectation without a rule. Read the three
rule fields as a set: all present or all absent, and compared to the
expectation's rule member when present.
@andehen
andehen marked this pull request as ready for review November 27, 2026 12:47
@andehen
andehen requested a review from a team as a code owner November 27, 2026 12:47
@andehen andehen added the stamphog Request stamphog review label Nov 27, 2026
@veria-ai

veria-ai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

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 ec31bca: 1 Should fix.

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

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.

Comment thread posthog/client.py Resolved
@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 27, 2026

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

Not approved — this change needs a human reviewer.

Add the stamphog label to request another review once you have addressed this.

The current code confirms @PostHog's unresolved publication-lock concern, so its comment-only review does not provide concern-free assurance. Address that thread and request a current-head review from a teammate or agent; Eli Reisman and Manoel Aranda Neto are suggested reviewers given their familiarity.

  • Author wrote 22% of the modified lines and has 8 merged PRs in these paths (familiarity MODERATE).
  • PostHog Review (Standard) reviewed the current head: 1 finding, posted on the PR.
  • posthog[bot] reviewed the current head.
  • @PostHog, posthog/client.py: The publication-lock performance concern remains unaddressed: _update_flag_state acquires the lock before assigning feature_flags, whose setter parses all version-2 definitions; snapshot readers require the same lock, and unchanged cache hydration still invokes that setter. Prepare parsed definitions outside the lock and publish them atomically before re-requesting. If the bot's finding is incorrect, explain why in its thread and resolve it.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 404L, 4F substantive, 1881L/6F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1d-complex (1881L, 6F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ ec31bca · reviewed head ec31bca

…n lock

Loading definitions parsed each config version 2 document in the
feature_flags setter, which runs under the publication lock, so every
concurrent flag read waited for the parse. Definitions fetched from the
API and definitions read from the definition cache provider are now
parsed first, and only the result is published, with the rest of the
flag state, under the lock. A read during a load keeps the definitions
published before, with the documents parsed from them.
Comment thread posthog/client.py Resolved
@andehen andehen added the stamphog Request stamphog review label Nov 27, 2026
@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 27, 2026

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

Not approved — this change needs a human reviewer.

Add the stamphog label to request another review once you have addressed this.

The bot's open security concern is still supported by the current code and must be fixed before re-requesting. The risky changes also lack current-head independent assurance; request review from a teammate or agent reviewer, with Eli Reisman or Manoel Aranda Neto suggested by familiarity.

  • Author wrote 18% of the modified lines and has 8 merged PRs in these paths (familiarity MODERATE).
  • @veria-ai on posthog/client.py: the unbounded-regex concern remains valid. The new dispatch reaches posthog/_feature_flags_v2.py:945 and then posthog/feature_flags.py:1342, which calls Python regex.search synchronously without a timeout. The translator rejects nested quantified groups but does not reject repeated overlapping alternatives; the value guard does not protect ordinary ASCII inputs ending in a non-newline mismatch. A configured susceptible rule can therefore allow attacker-controlled properties to monopolize an application worker. Use a bounded/non-backtracking matcher or defer unsafe patterns before local execution, add a regression test, and re-request review.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 441L, 4F substantive, 2008L/6F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1d-complex (2008L, 6F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ b146f8a · reviewed head b146f8a

@andehen andehen added the stamphog Request stamphog review label Nov 27, 2026

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

Not approved — escalated to a human reviewer.

Add the stamphog label to request another review once you have addressed this.

Risky contract, cache-format and event-write changes lack independent assurance on the current head; the older review and discussion summary do not supply it. Request a current-head review from a teammate or agent covering these areas before re-requesting, with Eli Reisman or Manoel Aranda Neto as suggested reviewers.

  • Author wrote 18% of the modified lines and has 8 merged PRs in these paths (familiarity MODERATE).
  • Risky territory without independent assurance: Public API contracts — posthog/client.py and posthog/feature_flag_evaluations.py now expose locally evaluated version 2 results through single and snapshot reads, including typed values and experiment attribution.; Data models — posthog/client.py changes the shared definition-cache representation by separating non-version-1 definitions into flags_v2 and recombining them on load.; Data ingestion or write paths — posthog/_feature_flags_v2.py and posthog/client.py construct local attribution records that feed feature-flag and experiment-exposure events, and cache local evaluation results.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 441L, 4F substantive, 2008L/6F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1d-complex (2008L, 6F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ b146f8a · reviewed head b146f8a

@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 27, 2026

This branch has not been deployed

No deployments
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.

1 participant