📐 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
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 dropnulls and nested objects.
Algorithm
- Disabled. When
enabledis false, serveoffVariant, ordefaultVariantif it is null. ReasonOFF. - Rules. Evaluate
rulesin order. The first rule whose conditions all match wins: serve itsserve(below). ReasonRULE_MATCH, with the zero-based rule index. - Fallthrough. When no rule matches, serve
fallthrough, ordefaultVariantif it is null. ReasonFALLTHROUGH.
Serving a serve block:
- A fixed
variant: serve it. - A
rollout: read thebucketByattribute (defaultuserId) from the context.- Missing or empty: serve
defaultVariantwith reasonMISSING_BUCKET_BY. Never pick randomly. - Otherwise compute the bucket and pick the variant (below).
- Missing or empty: serve
Bucketing
1
bucket = murmur3_32(utf8("{flagKey}.{salt}.{bucketByValue}"), seed 0) mod 10000
murmur3_32is 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}]givesabuckets 0–1249 andbbuckets 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 leadingv, 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_32of UTF-8 strings (unsigned).buckets:flagKey,salt,value→bucket.flags: flag definitions with one environment state each.cases:flag+context→ expectedvariant,reason,matchedRuleIndexandbucket.
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.