📐 Evaluation spec

Every Grimoire client (the .NET Grimoire.Consumer library, the PHP client in Pancake, and any future one) evaluates flags locally from the definitions the Consumer API serves. For the same flag, state and context, every client must return the same variant. The reference implementation is FlagEvaluator in Grimoire.Core.

Table of contents
  1. Inputs
  2. Algorithm
  3. Bucketing
  4. Conditions
  5. Test vectors

Inputs

  • Flag: key, salt, variants, defaultVariant.
  • State for the environment: enabled, offVariant, rules, fallthrough.
  • Context: a map of attribute name to string. Clients convert numbers and booleans to their JSON text (42, 1.5, true) and drop nulls and nested objects.

Algorithm

  1. Disabled. When enabled is false, serve offVariant, or defaultVariant if it is null. Reason OFF.
  2. Rules. Evaluate rules in order. The first rule whose conditions all match wins: serve its serve (below). Reason RULE_MATCH, with the zero-based rule index.
  3. Fallthrough. When no rule matches, serve fallthrough, or defaultVariant if it is null. Reason FALLTHROUGH.

Serving a serve block:

  • A fixed variant: serve it.
  • A rollout: read the bucketBy attribute (default userId) from the context.
    • Missing or empty: serve defaultVariant with reason MISSING_BUCKET_BY. Never pick randomly.
    • Otherwise compute the bucket and pick the variant (below).

Bucketing

1
bucket = murmur3_32(utf8("{flagKey}.{salt}.{bucketByValue}"), seed 0) mod 10000
  • murmur3_32 is MurmurHash3 x86 32-bit, read as an unsigned 32-bit integer.
  • That gives buckets 0–9999, a 0.01 % resolution.
  • Each rollout slice covers round(weight × 100) consecutive buckets, in the order listed. For example, [{a, 12.5}, {b, 87.5}] gives a buckets 0–1249 and b buckets 1250–9999.
  • A bucket past the last boundary (only possible through rounding) goes to the last slice.

The bucket depends only on the flag key, salt and attribute value. Changing the weights moves only the buckets at the boundary, so raising a rollout from 10 % to 20 % keeps the first 10 %.

Conditions

Operator Rule
in, equals values contains the attribute (exact match)
notIn values does not contain it
contains the attribute contains any value
startsWith the attribute starts with any value
endsWith the attribute ends with any value
matches any value, as a regular expression, matches the attribute (search, not full match)
semVerGte both parse as SemVer 2.0.0 and attribute ≥ values[0]
semVerLt both parse as SemVer and attribute < values[0]
  • Missing attribute: the condition is false, whatever the operator (including notIn).
  • Comparisons: ordinal (byte-wise on UTF-16/UTF-8 code units) and case-sensitive; no trimming, no normalisation.
  • SemVer: MAJOR.MINOR.PATCH, an optional leading v, pre-release identifiers compared per the spec (numeric < alphanumeric, numeric by value) and build metadata ignored. Anything else is not a version, and the condition is false.
  • Regular expressions: write portable patterns (anchors, classes, quantifiers, groups, alternation). They behave the same in .NET, PCRE and ECMAScript. A pattern that fails or times out never matches.
  • Unknown operators never match.

Test vectors

test-vectors.json holds:

  • hashes: murmur3_32 of UTF-8 strings (unsigned).
  • buckets: flagKey, salt, value → bucket.
  • flags: flag definitions with one environment state each.
  • cases: flag + context → expected variant, reason, matchedRuleIndex and bucket.

A client conforms when it reproduces every entry. The .NET reference runs them in FeatureFlagEvaluatorTests. They were generated by an independent Python implementation of this spec, whose Murmur3 matches the published reference values.