ADR-0011: Feature flags are evaluated server-side, per plan, and a flag grants the deviation
- Status: Accepted
- Date: 2026-08-13
- Deciders: Cardsite platform team (workstream
001-renaissance, audit 02-D4)
Context
feature_flags shipped complete and consumed by nothing. Migration 000022
created the table with per-plan targeting (enabled plus a comma-separated
plan_ids); the repository had eight methods; five admin routes did CRUD; an
admin page created and toggled flags; a typed client wrapped all of it; and a
public endpoint, GET /api/v1/feature-flags/evaluate, returned the enabled
keys. Nothing in 80,000 lines of Go and 239 web source files read a flag to
change behaviour (audit 02-D4, .archives/removal-safe/AUDITS-081226.md).
Three properties of that state matter, because they decide the shape of the fix rather than merely motivating it:
- The evaluate endpoint could not evaluate. It answered
ListEnabled— every enabled flag, to every caller, with no audience — and the handler loadedplan_idsfrom the database only to discard it. "Enabled for the Pro plan" and "enabled for everyone" were the same answer. The targeting the admin form has offered since 000022 has therefore never worked, and no client could have gated anything on the response even if one had tried:plan_idsis not in it. - It was unauthenticated, so a flag rolled out to one plan was announced to the internet by key. A flag key is usually the name of an unreleased feature.
- The plan-resolution chain that exists does not resolve a plan.
PlanLimitServicereadsusers.plan_settings— the denormalised copy of a plan's quotas — and never touchesusers.plan_id. There was noGetPlanIDanywhere in the repository layer. So "which plan is this user on" had to be built, not reused.
This is the third shipped-but-dead subsystem on record (AGENTS.md non-negotiable #7 names the first two). The ROADMAP calls wiring it the highest-value-per-line change available, because everything phase 02 goes on to do — capabilities, block gating, admin-editable entitlements — needs one place that turns a principal into a set of grants.
Decision
A feature flag is evaluated on the server, for the signed-in user, against the plan they are on. The client receives keys, never rows. And a flag grants a deviation from the default — never the default itself.
Four parts:
FeatureFlagService.EnabledFor(ctx, userID)is the one evaluation helper. It resolves the caller's plan (UserRepository.GetPlanID, new) and asks for the flags targeted at it (FeatureFlagRepository.ListEnabledForPlan, new): every enabled flag whoseplan_idsis empty, plus every enabled flag naming that plan. Matching is in SQL, on whole entries —"77"is not plan 7 — with each entry trimmed of spaces, tabs and newlines ("3, 7"is what an operator types;"3,\r\n7"is what a paste produces), and an empty entry matching nobody.plan_id = 0— the no-plan default every install starts in — is a targetable audience, not a null. On the write side the admin API refusesplan_idsit could never match, because the alternative is a flag that looks targeted and reaches no one.It fails closed, which is the opposite of
PlanLimitService's posture, deliberately. A quota check that cannot read the plan lets the write through, because refusing a paying user over a database blip is worse than one row over the limit. A flag is a grant: an evaluation that fails returns no keys and an error, and the endpoint answers 500 rather than 200 with an empty array — "off" is a decision, "unknown" is not, and a client must be able to tell them apart.The evaluate endpoint is authenticated, at the same path and with the same response shape, registered inside the
/api/v1group. Under the scope gate (ADR-0002) the prefix stays closed to token principals: filing it under theaccountscope would hand the list of enabled flag keys to every token already holdingaccount:readforGET /userintrospection — a silent widening of a granted scope, of exactly the kind this decision is closing. It opens to tokens when an integration has a reason to read it, which is a smaller change to make later than a leak is to undo.This is a security fix shipped in place, and it should be read as one. AGENTS.md §3 sends breaking API changes to
/api/v2, and this is not the ADR-0008 situation, where the published contract already described the new behaviour and the code was the thing that had drifted: here the contract said the opposite, in three places (security: []in the OpenAPI spec, "the caller may be anonymous" indocs/API.md, and a "Public (no auth)" row inapps/api/AGENTS.md), and all three are edited by this change. What justifies editing them is not that the change is small but that the documented behaviour leaked: unauthenticated, the endpoint published the key of every enabled flag — usually the name of an unreleased feature — to anyone who asked, and could not honour the targeting that would have limited it. A/api/v2twin would have left that endpoint standing for callers who never arrive. It had none: zero consumers, in either tree, at the time of the change. The admin CRUD is untouched.The client declares the keys it understands, in
apps/web/src/lib/featureFlags.ts, and consumes them throughuseFeatureFlag(key)— false while loading, false on failure. Flags are data, but the set of keys the app can act on is code: a key the app never reads gates nothing, and a key the app reads under a name no flag has evaluates false forever, silently. That is 02-D4 one level down, and it is what the gate below prevents.
The polarity rule
A flag must grant the deviation, never the default. Because the hook answers false while loading and false on failure, the flag-off path is the one every user gets during boot, on a slow network, and whenever the endpoint is down. If "off" meant hide a live surface, every one of those states would be a regression; because "off" means behave exactly as an install with no flags does, none of them are.
The first consumer is chosen to demonstrate it: the one-time notice on /links
explaining that the list narrowed to short links (phase 01 task 1) renders
unless links_type_filter_notice_retired is enabled. The notice has no expiry
and the ROADMAP already records that it must be retired; the flag is the
operator's switch for retiring it for everyone without a deploy, and with no
flag row the page behaves precisely as it does today.
What is not decided here
Capabilities (decision 005, phase 02 task 2) are a separate mechanism with a separate registry. Flags are operational rollout; capabilities are what a plan includes. They will share the enforcement shape, not the table.
A server-side single-flag check.
IsEnabled(ctx, userID, key)is the obvious sibling ofEnabledForand is deliberately not written: it would have no caller until write-path enforcement lands (task 3), and shipping an exported helper wired to nothing is the finding this ADR closes.Caching. The plan's risk row proposes resolving at session load and caching with the existing
Cacher. Not done here, and the honest reason is the kill switch, not the cost: a TTL cache silently defers a disable, and an operator who turns a flag off and watches nothing happen for a minute has been given a worse tool than no cache. The cost is not the argument — an evaluation is two round trips (the plan, then the flags) and the second is a sequential scan by construction, since the predicate splits a text column and no index can serve it. That is irrelevant at this table's size and would stop being irrelevant long before it became the reason to cache. When caching is added it must be invalidated on write, not merely expire.One consequence to state plainly rather than leave implied: the plan's test list names "the no-Redis path" as a required Go test. With no cache there is no such path — that item is void, not satisfied, and it comes back with the cache.
Consequences
- The gating primitive exists and has exactly one caller, which is checked by a gate rather than by memory.
GET /api/v1/feature-flags/evaluatenow requires authentication. No known consumer breaks — it had none — and the OpenAPI entry, previouslysecurity: []with an undocumented response, now documents both. The generator that produces those entries (apps/api/scripts/gen_openapi_paths.py) no longer classifies the path as public, or the next regeneration would restoresecurity: []and quietly undo this.plan_idsis validated on write. It is free TEXT matched against an integer column, so a plan name — which is what the admin form's own placeholder used to suggest — stored cleanly, listed back to the operator as targeting, and reached nobody. The admin API answers 400 for a non-numeric entry and canonicalises the rest; the form says "numbers" and shows the refusal. A flag that silently reaches nobody is this same finding pointed at the operator instead of the user.FeatureFlagRepository.ListEnabledis gone, superseded in place byListEnabledForPlan.GetByKeyremains uncalled; it is the primitive a server-side single-flag check will use, and it predates this change.- New CI gate —
scripts/check-entitlement-parity.mjs, innpm run verifyand thedesign-systemjob. It fails when auseFeatureFlagcall site passes anything but a key from the registry, when a registered key has no consumer, and when a call site's answer is discarded — the last one because counting call sites is not the same as counting gates, and only the second is the property worth having. A flag that gates nothing cannot be committed. The plan (.station/jobs/001-renaissance/plans/02-entitlements.md) names two further rules for this script — capability referenced with no registry entry, capability registered with no enforcement call site — which land with task 2, in the same file. - An operator can now reach the Feature Flags page from the admin nav. It has been routed but unlisted since it shipped, which was survivable while flags did nothing.
Alternatives considered
- Keep the endpoint public and evaluate anonymously. Rejected: with no audience there is nothing to evaluate, so it would have preserved the defect under a new name, and it leaks unreleased feature names.
- Add a second, authenticated endpoint beside the public one. Rejected: two paths answering one question, one of them known-wrong, is the "one canonical place per concept" principle inverted.
- Evaluate in the client from the admin list. Rejected outright: it would ship every flag's targeting to every browser and make the gate advisory.
- Fail open on a database error. Rejected: it grants every flag to everyone at exactly the moment the system is least able to notice.
Source: docs/decisions/0011-feature-flags-are-evaluated-server-side-per-plan.md