Spec the SQL that resolves a user's feature flags

Vibe codingMedium25 min

Your task

Your Postgres app decides which features each user gets from 3 sources: their plan, grants to their org and overrides for that user. An agent will write the query, plus a thin TypeScript wrapper, that answers "which features does this user have?"

Write the spec you would hand the agent.

What you know:

  • features(key) lists every feature, and users(id, org_id, plan_id) holds each user's org and plan
  • plan_features(plan_id, feature_key) lists what each plan includes
  • org_grants(org_id, feature_key, enabled) and user_overrides(user_id, feature_key, enabled) can turn a feature on or off
  • the product rule: a user override beats an org grant, and an org grant beats the plan

Then, in a few sentences: what does "query the user's enabled features" get wrong in SQL?