Product requirements

Benchdog specification

This file is the source of truth for flag data and flag evaluation.

Status
Shipped
Owner
Joe Gaudet
Created
Updated
One-pager
None
Related
None

Summary

This file is the source of truth for flag data and flag evaluation. The TypeScript, Ruby and Python cores must agree with it. The shared vectors in vectors/ are the test of that agreement.

The spec version is in VERSION. Each core has a SPEC_VERSION constant, and a test checks it against that file. A change to this file that changes a result needs a new version, new vectors, and a change to all three cores in the same PR.

The words MUST, MUST NOT and MAY have the meanings in RFC 2119.

Glossary

1. Terms

  • Flag: a named switch. It has a type, a list of variations, and rules that select one variation for an evaluation context.
  • Variation: one possible value of a flag. Each variation has a key and a value.
  • Actor: the thing that a flag is evaluated for, for example a user or an account. An actor has an id (a string) and attributes (a JSON object).
  • Flag set: all flags and segments of one environment, as one JSON document.
  • Environment: a separate flag set for the same flag keys (for example development, staging, production). The evaluator sees one environment at a time.

Problem and context

Not recorded in the original plan.

Goals and non-goals

Not recorded in the original plan.

Customers

Not recorded in the original plan.

Internal stakeholders

Not recorded in the original plan.

Users and scenarios

Not recorded in the original plan.

Landscape and constraints

Not recorded in the original plan.

Proposed design

2. Flag set document

A flag set is a JSON object. schema/flagset.schema.json is its JSON Schema.

{
  "environment": "production",
  "flags": { "new-checkout": { "...": "a flag, section 3" } },
  "segments": { "pro-users": { "Equal": [{ "Property": "plan" }, "pro"] } }
}
  • flags maps each flag key to a flag (section 3). A flag key matches ^[a-z0-9][a-z0-9_.:-]*$.
  • segments maps a segment name to an expression (section 6). A segment is a named expression that a dashboard user builds. Expressions use it with the Segment function.

Groups (section 5.5) are not data. The host app registers them in code.

3. Flag

schema/flag.schema.json is the JSON Schema for one flag.

Field Type Default Meaning
key string required The flag key. It MUST be the same as the key in flags.
type string "boolean" boolean, string, number or object.
variations array see below The possible values: { "key": string, "value": any }.
offVariation string "off" for boolean flags The variation when the flag is off.
onVariation string "on" for boolean flags The variation when a gate is open.
enabled boolean true The kill switch. false serves offVariation to all.
gates object all gates closed The Flipper-style gates (section 5).
rules array [] Ordered targeting rules (section 5.7).
fallthrough serve { "variation": offVariation } What to serve when no gate or rule matches.
prerequisites array [] { "flag": string, "variation": string } items.
schedule array [] Scheduled changes (section 4).
tags array of strings [] Free tags. The tag client makes a flag visible to browsers.
owner string none The team, person or email that owns the flag. 1 to 200 characters.

A boolean flag MAY leave out variations, offVariation and onVariation. Its defaults are [{ "key": "off", "value": false }, { "key": "on", "value": true }], "off" and "on".

A flag of another type MUST have variations, offVariation and onVariation. Each variation value MUST have the flag's type: a JSON string, a JSON number, or a JSON object.

gates has these fields. A missing field is a closed gate.

Field Type Closed value
boolean boolean false
actors array of strings []
groups array of strings []
percentageOfActors number, 0 to 100 0
percentageOfTime number, 0 to 100 0
expression expression null

A serve is one of:

  • { "variation": "<key>" }: serve that variation.
  • { "rollout": [{ "variation": "<key>", "weight": <number> }, ...] }: serve one variation for each actor, by weight (section 7.2). The weights are percentages. Their sum MUST be 100.

4. Schedule

A schedule step is { "at": "<RFC 3339 time>", "set": { ... } }. set MAY contain enabled, gates and fallthrough.

Before an evaluation, the evaluator applies each step whose at is at or before the evaluation time, in order of at (earliest first; steps with the same at in array order):

  • enabled and fallthrough replace the flag's value.
  • gates is merged one field at a time: each field in the step replaces the same field of the flag's gates.

Thus a ramp from 5% to 25% to 100% is three steps that set gates.percentageOfActors. The evaluator applies the steps at read time, so a late worker cannot leave a flag in the wrong state.

5. Evaluation

5.1 Inputs

evaluate(flagSet, key, context, defaultValue, expectedType?, groups?)

  • context is { "actor"?: { "id": string, "attributes"?: object }, "now"?: string, "random"?: number, "memberOf"?: [string] }.
    • now is the evaluation time (RFC 3339). If it is missing, the core uses the clock.
    • random is a number in [0, 1) for the percentage-of-time gate. If it is missing, the core uses a random number generator. Vectors always give it.
    • memberOf is the names of the groups that the actor is a member of. Section 5.5.
  • groups maps a group name to a predicate on the actor. Section 5.5.
  • expectedType is the type that the caller asks for (boolean, string, number, object).

5.2 Result

{ "value": true, "variation": "on", "reason": "SPLIT", "gate": "percentageOfActors" }
Field Meaning
value The served value, or defaultValue on an error.
variation The served variation key, or null on an error.
reason An OpenFeature reason: STATIC, TARGETING_MATCH, SPLIT, DEFAULT, DISABLED, PREREQUISITE_FAILED or ERROR.
gate The step that decided: boolean, actor, group, expression, rule, percentageOfActors, percentageOfTime, fallthrough, disabled, prerequisite, or null on an error.
ruleId The rule id, only when gate is rule.
errorCode Only when reason is ERROR: FLAG_NOT_FOUND, TYPE_MISMATCH or INVALID_FLAG.

5.3 Order

The evaluator does these steps in this order. The first step that decides gives the result.

  1. Not found. If the flag set has no flag key: ERROR, FLAG_NOT_FOUND, defaultValue.
  2. Type. If expectedType is given and is not the flag's type: ERROR, TYPE_MISMATCH, defaultValue.
  3. Schedule. Apply the schedule (section 4).
  4. Kill switch. If enabled is false: serve offVariation, DISABLED, gate disabled.
  5. Prerequisites. For each prerequisite in order, evaluate the prerequisite flag with the same context and groups. If the flag does not exist, or its variation is not the required variation: serve offVariation, PREREQUISITE_FAILED, gate prerequisite. A prerequisite cycle (a flag that needs itself through any chain) also fails this step.
  6. Boolean gate. If gates.boolean is true: serve onVariation, STATIC, gate boolean.
  7. Actor gate. If there is an actor and gates.actors contains its id: serve onVariation, TARGETING_MATCH, gate actor.
  8. Group gate. If there is an actor and the actor is a member of a group in gates.groups (section 5.5): serve onVariation, TARGETING_MATCH, gate group.
  9. Expression gate. If gates.expression is not null and its result is truthy (section 6.1): serve onVariation, TARGETING_MATCH, gate expression.
  10. Rules. For each rule in order, if its expression result is truthy: serve the rule's serve (section 7), gate rule, with its ruleId. The reason is TARGETING_MATCH for a variation and SPLIT for a rollout.
  11. Percentage of actors. If there is an actor and bucket(key + actor.id) < percentageOfActors * 1000 (section 7.1): serve onVariation, SPLIT, gate percentageOfActors.
  12. Percentage of time. If random < percentageOfTime / 100: serve onVariation, SPLIT, gate percentageOfTime.
  13. Fallthrough. Serve fallthrough, gate fallthrough. The reason is DEFAULT for a variation and SPLIT for a rollout.

If a step decides on a variation key that the flag does not have, the result is ERROR, INVALID_FLAG, defaultValue. The JSON Schema cannot find this mistake, so a writer MUST check that each variation key in offVariation, onVariation, fallthrough and rules exists.

owner has no effect on evaluation.

Steps 6 to 12 are the Flipper gates. For a boolean flag, "a gate is open" in Flipper is the same as "the result is on" here.

5.4 Arithmetic

The comparisons in steps 11 and 12 and in section 7 use IEEE 754 double arithmetic, as Flipper does. Do not round. 33.333 * 1000 is 33333 in all three languages.

5.5 Groups

A group is a predicate that the host app registers in code, for example register("admins", actor => actor.admin). The flag set holds only group names. Vectors give groups as { "<name>": ["<actor id>", ...] }: the predicate is true when the list has the actor's id.

If the context has no memberOf, the actor is a member of a group when the group is registered and its predicate is true for the actor. An unregistered group name is false.

If the context has memberOf, the actor is a member of a group when memberOf contains the group name. The evaluator does not call the predicates, and it does not check that the group is registered. An empty memberOf matches no group. The group gate still needs an actor. Flipper has no memberOf, so its vectors have "flipper": false.

A host can compute memberOf once for each request, for example with the TypeScript client's groupsFor(actor), and give it to each evaluation. This is faster when the predicates are slow.

5.6 Actor properties

Expressions read actor properties with Property. The properties are the actor's attributes, plus flipper_id, which is always the actor's id (Flipper compatibility). With no actor, every property is null.

5.7 Rules

A rule is { "id": string, "expression": expression, "serve": serve }. Rule ids MUST be unique in a flag.

6. Expressions

Expressions use the Flipper expression JSON format. An expression is one of:

  • A literal: a JSON string, number, boolean or null.
  • A function call: an object with exactly one key, the function name. Its value is the argument list. A value that is not an array is one argument.
{
  "All": [
    { "Equal": [{ "Property": "plan" }, "pro"] },
    { "GreaterThan": [{ "Property": "seats" }, 5] }
  ]
}

6.1 Values

An expression result is null, a boolean, a number, a string, or a time (a point in time, held as seconds since the Unix epoch, UTC). A time is a separate type. It is never equal to a number.

A result is truthy when it is not null and not false. Thus 0 and "" are truthy, as in Ruby.

6.2 Functions

Function Arguments Result
All any number true if each argument is truthy (true for no arguments), else false.
Any any number true if one argument is truthy, else false (false for no arguments).
Equal 2 Section 6.3.
NotEqual 2 The opposite of Equal.
GreaterThan 2 Section 6.4.
GreaterThanOrEqualTo 2 Section 6.4.
LessThan 2 Section 6.4.
LessThanOrEqualTo 2 Section 6.4.
Property 1: a name The actor property (section 5.6), or null.
Boolean 1 true for true, 1, "true" and "1"; else false.
Number 1 A number as it is. A string that is a decimal number (-?\d+(\.\d+)?) as that number. Else null.
String 1 Section 6.5.
Now none The evaluation time, as a time.
Time 1 A number as seconds since the epoch, or an RFC 3339 string, as a time. Else null.
PercentageOfActors 2: text, percentage bucket(key + String(text)) < percentage * 1000, where key is the flag key. false if percentage is not a number.
Segment 1: a segment name The result of that segment's expression, or null if there is no such segment or a cycle. Benchdog extension.

A call to an unknown function, or with the wrong number of arguments, gives null. The JSON Schema refuses these, so a valid flag set does not have them.

A writer MUST refuse an All or Any with no arguments, as Flipper does: an empty All matches every actor, which is almost always a mistake. The JSON Schema refuses it. The evaluator still gives the results in the table, for flag sets from other sources.

6.3 Equality

Equal(a, b) is true when:

  • both are null; or
  • both are booleans, strings or times, and they are the same; or
  • both are numbers, and they are the same number (1 equals 1.0).

Else it is false. A string is never equal to a number.

6.4 Order

GreaterThan and the other order functions compare two numbers, two strings (by Unicode code point), or two times. For any other pair, the result is false.

6.5 String

String(x) gives:

  • "" for null;
  • "true" or "false" for a boolean;
  • for a number: the integer digits if the number is an integer (5, not 5.0), else the shortest decimal that gives the same double (0.1, 33.333);
  • the string as it is;
  • for a time: the RFC 3339 form in UTC with seconds and a Z (2026-01-01T00:00:00Z).

7. Buckets

7.1 The bucket function

bucket(text) = crc32(utf8(text)) mod 100000

crc32 is the CRC-32 of zlib (ISO 3309, polynomial 0xEDB88320), as in Ruby Zlib.crc32, Python zlib.crc32 and Flipper. The result is an integer from 0 to 99 999.

This is Flipper's function. Thus a percentage rollout in Flipper gives the flag to the same actors in Benchdog.

7.2 Rollouts

A rollout picks a variation for an actor:

  1. If there is no actor, serve offVariation, reason DEFAULT. The gate stays fallthrough or rule.
  2. b = bucket(key + ":" + salt + actor.id). The salt is variant for the fallthrough and the rule id for a rule.
  3. Walk the rollout items in order and add weight * 1000 to a sum. Serve the first item where b < sum. If no item matches (a float effect), serve the last item.

8. Audit chain

Each change to a flag set writes one audit entry. The entries make a hash chain, so a change to an old entry is visible. schema/audit-entry.schema.json is the JSON Schema.

{
  "seq": 1,
  "at": "2026-12-01T12:00:00Z",
  "environment": "production",
  "flag": "new-checkout",
  "action": "flag.update",
  "actor": { "id": "user:42", "name": "Joe" },
  "source": "dashboard",
  "reason": "Ramp to 25%",
  "before": { "gates": { "percentageOfActors": 5 } },
  "after": { "gates": { "percentageOfActors": 25 } },
  "prevHash": "<64 hex digits>",
  "hash": "<64 hex digits>"
}
  • seq starts at 1 and adds 1 for each entry.
  • prevHash is the hash of the entry before. For the first entry, it is 64 zeros.
  • hash is the lowercase hex SHA-256 of the UTF-8 bytes of the canonical JSON of the entry without its hash field.
  • Canonical JSON is RFC 8785 (JCS): no white space, object keys sorted by UTF-16 code units, strings with the minimum escapes, and numbers in the shortest form (an integer has no .0).
  • action is one of flag.create, flag.update, flag.delete, flag.promote, flag.kill, segment.create, segment.update, segment.delete, approval.request, approval.approve, approval.reject, schedule.apply.
  • source is one of dashboard, api, cli, code, worker, external.

To verify a chain, check each entry in order. For entry number n (from 1), do these checks in this order, and stop at the first failure:

  1. seq MUST be n. Else the error is SEQ_GAP.
  2. prevHash MUST be the hash of entry n - 1, or 64 zeros for entry 1. Else the error is PREV_HASH_MISMATCH.
  3. The hash of the entry, computed again, MUST be its hash. Else the error is HASH_MISMATCH.

The result is { "valid": true }, or { "valid": false, "seq": <the entry's seq>, "error": "<code>" }. An empty chain is valid.

9. Client payload

The client payload is the result for one actor, for the flags with the tag client. The host evaluates it on the server. schema/client-payload.schema.json is the JSON Schema.

{
  "environment": "production",
  "generatedAt": "2026-12-01T12:00:00Z",
  "flags": { "new-checkout": { "value": true, "variation": "on" } }
}

User interface and prior art

Not recorded in the original plan.

Requirements

10. Vectors

Each core MUST pass every file in vectors/:

  • vectors/bucket.json: inputs and outputs of the bucket function.
  • vectors/eval/*.json: a flag set, groups, and cases. Each case has a flag key, a context, a default value, an optional expected type, and the expected result (section 5.2). A file with "schemaValid": false holds a flag set that the schema refuses. The evaluator MUST still pass its cases.
  • vectors/audit.json: audit entries with their canonical JSON and hashes, and chains with the expected result of a verification.

tools/flipper-oracle.rb computes the Flipper-compatible expected results with the real Flipper gem. Thus the vectors test Benchdog against Flipper, not against itself.

Data model and API changes

Not recorded in the original plan.

Security and permissions

Not recorded in the original plan.

Observability and success metrics

Not recorded in the original plan.

Rollout and migration

Not recorded in the original plan.

Work items

Not recorded in the original plan.

Future work and research

Not recorded in the original plan.

Alternatives considered

Not recorded in the original plan.

Risks

Not recorded in the original plan.

Open questions

Not recorded in the original plan.

Interview record

Each question of the interview with its answer and its source. A fact from the code or a document, a question that the author did not ask, and an assumption are rows too.

QuestionAnswerSource
Where does the text of this page come from?From the Markdown file spec/SPEC.md at commit beba50d. The text of each section is the text of the Markdown file. A section that the Markdown file does not have says so.Document: spec/SPEC.md

Changelog

DateAuthorChange
2026-12-01Joe GaudetCommit ef2e4d9: Release 0.1: the TypeScript slice (#1 to #15) (#16)
2026-12-04Joe Gaudet (agent)Converted from spec/SPEC.md to this page. The text is the text of the Markdown file.
2026-12-04Joe Gaudet (agent)Status Shipped: spec/VERSION is 1, and the TypeScript, Ruby and Python cores that follow it are on npm, RubyGems and PyPI (README.md). The Markdown file has no status line.

The prd skill of skillsaw wrote this page. The HTML in this file is the source of truth. The diagrams are the Mermaid sources in it.