saasaloy-feature-flags
Runbook for the feature-flags module — typed flags with per-tenant overrides and percentage rollouts, resolved through an isolate cache over a published kv document with the database as the source of truth, plus maintenance mode and per-integration kill switches. Use when adding a flag key, toggling a flag, rolling one out to a share of users, overriding a flag for one tenant, closing the site for maintenance, switching off payments or AI or email, or working out why a toggle has not taken effect yet.
- License 许可
- MIT
- 所属仓库
- mimukit/saasaloy
- 最近更新
- 2026-11-13
- Stars
- ★ 4
- Forks
- 0
01 前言 Front Matter
- name
- saasaloy-feature-flags
- description
- Runbook for the feature-flags module — typed flags with per-tenant overrides and percentage rollouts, resolved through an isolate cache over a published kv document with the database as the source of truth, plus maintenance mode and per-integration kill switches. Use when adding a flag key, toggling a flag, rolling one out to a share of users, overriding a flag for one tenant, closing the site for maintenance, switching off payments or AI or email, or working out why a toggle has not taken effect yet.
03 SKILL.md 正文
feature-flags — typed flags, rollouts, maintenance mode and kill switches
feature-flags ships one package and eight files:
| File | What it holds |
|---|---|
packages/feature-flags/ |
the registry, the resolver, the isolate cache and the bucketing |
apps/api/src/routes/feature-flags.ts |
the admin API at /admin/flags; every route lists adminGate before validateValue, and every write runs through adminWrite |
apps/api/src/audit/feature-flags.ts |
the three audit actions of those writes |
apps/api/src/lib/flags.ts |
flagsFor(c), the database source, and sessionIsAdmin |
apps/api/src/middleware/maintenance.ts |
the 503 page middleware |
packages/db/src/schema/feature-flags.ts |
feature_flags and feature_flag_overrides |
packages/db/src/repositories/feature-flags.ts |
every query those tables need |
packages/validators/src/feature-flags.ts |
the write body schema, shared by the route and hc |
packages/ui/src/blocks/admin/feature-flags.tsx + apps/admin/src/routes/flags.tsx |
the Flags screen |
Where a value comes from
The database is the source of truth. kv is a published read cache, and the isolate cache in front of it is a plain Map.
A read walks three levels, cheapest first:
- the isolate's
Map, forFLAGS_ISOLATE_TTL_SECONDS(default 10), - the published kv document for the scope —
flags:v1:global, orflags:v1:t:<tenantId>, - the database, which also publishes the missing document.
A value then resolves in this order: the tenant override, the global row, the code default.
A reader never writes on a hit. The only write a read can do is publishing a document that was missing entirely, which happens once per scope per location (a colo reading inside the up-to-60-second window after a publish republishes the same content), not once per colo per request. Write-back on every read would scale KV writes with traffic, and the free plan allows 1,000 writes a day. Published this way, KV writes equal toggles.
Reading a flag
import { flagsFor } from "../lib/flags";
export const checkout = new Hono().post("/", async (c) => {
const flags = flagsFor(c);
if (await flags.flag("subscription.new-checkout", { subjectId: user.id, tenantId: org.id })) {
return newCheckout(c);
}
return oldCheckout(c);
});
subjectId and tenantId are both optional and both passed in by you. A missing tenantId resolves the global value, which is right for a single-tenant project. A missing subjectId makes a percentage flag resolve false — there is nothing to bucket, and guessing would make a retry of the same request answer differently.
Adding a flag
Two steps, and the first one needs a deploy:
// packages/feature-flags/src/flags/app.ts — your file, beside the module's system.ts
import { defineFlag } from "../define";
export function newCheckout() {
return defineFlag({
key: "subscription.new-checkout",
type: "percentage",
default: false,
description: "Route checkout through the new provider.",
});
}
// packages/feature-flags/src/index.ts
export const flags = defineFlags({
flags: [maintenanceFlag(), killPayments(), killAi(), killEmail(), newCheckout()],
});
flag()'s key set is inferred from that array, so a call with an unregistered key fails pnpm typecheck rather than resolving to a default at runtime. That is the trade: a new key needs a deploy, a new value does not.
Put your own flags in your own file. src/flags/system.ts belongs to the module and saasaloy update may rewrite it.
The 70 seconds
A toggle in the admin app reaches every location in about 70 seconds, and the two numbers that add up to it are:
| Layer | Delay | Why |
|---|---|---|
| Workers KV propagation | up to ~60 s | KV is eventually consistent between locations |
the isolate Map |
up to FLAGS_ISOLATE_TTL_SECONDS, default 10 s |
expiry with no invalidation — nothing can reach into another colo's isolate |
The isolate that served the toggle drops its own copy immediately, so you see the change on your next request. Everyone else waits.
Raise FLAGS_ISOLATE_TTL_SECONDS to spend fewer KV reads and wait longer; set it to 0 to skip that layer and pay a KV read per request. At the default a warm isolate costs about 8,640 reads a day, well inside the free plan's 100,000. A value that is not a whole number throws while the Worker initializes rather than falling back to 10.
The published document has no TTL. The admin routes republish on every change, so there is nothing an expiry would fix.
The admin screen reads the database, never the cache
GET /admin/flags queries the tables directly. That is deliberate: the published document is up to a minute behind, and showing an operator a stale copy of the switch they just moved is the failure the split exists to prevent. The cost is the other way round — the screen can be ahead of what a live request sees, for about a minute after a toggle.
Every write route writes the row, then republishes. The republish is queued with afterCommit, so it runs only after the row commits, and a kv document never shows a value whose write rolled back. It runs on waitUntil, so the write answers { ok: true } without waiting for kv, and a failed publish is logged at error rather than failing the write. A tenant write republishes the global document too, so the two documents a tenant request reads were built from the same database state.
Admin routes and their audit rows
The chained-route patch mounts apps/api/src/routes/feature-flags.ts at /admin/flags, so the admin app calls it as api.admin.flags. It sits under /admin/ so the admin module's gate test and audit coverage test see every route.
A project made before the move to /admin/flags keeps the old .route("/flags", featureFlags) link after saasaloy update, because update never reverses a patch the new revision dropped. The update note feature-flags/0001-admin-flags-mount tells you to delete that line. Until you do, both paths serve the same handlers, and the gate and coverage tests cannot see the /flags copy.
| Route | Audit action | Label |
|---|---|---|
GET /admin/flags |
none, a read | |
GET /admin/flags/:key |
none, a read | |
PUT /admin/flags/:key |
flag.value.set |
the flag key |
PUT /admin/flags/:key/tenants/:tenantId |
flag.tenant.set |
<key> · tenant <tenantId> |
DELETE /admin/flags/:key/tenants/:tenantId |
flag.tenant.clear |
<key> · tenant <tenantId> |
Each write runs through adminWrite from apps/api/src/lib/admin-write.ts. The area is flags, the target type is flag, and the target id is the flag key, so a filter by target finds the global value and every tenant override of one flag. A row records before and after for enabled and percentage. A first write has no before. Clearing an override that does not exist changes nothing and writes no row. The actions live in apps/api/src/audit/feature-flags.ts, which this module's plugin-array patch registers in apps/api/src/lib/audit-actions.ts. A new write route here needs an entry there, or the audit coverage test fails.
Percentage rollouts
Bucketing is FNV-1a 32-bit over "<flagKey>:<subjectId>", modulo 100. It is synchronous, has no dependencies, and is stable: one subject gets the same answer on every request, in every colo, for as long as the share does not move.
The flag key is in the hash, not just the subject. Two flags at 10% therefore reach different tenths of the audience — bucketing on the subject alone would make the second rollout a subset of the first, and the second experiment would measure the first.
Web Crypto was rejected for this. crypto.subtle.digest is async, so every flag() call would await a digest even for a boolean flag that never buckets.
Raising a share keeps everyone who was already in it: a subject's bucket does not move, so 10% → 20% adds subjects and removes none. Lowering it removes people who had the feature.
Maintenance mode
system.maintenance is an ordinary boolean flag. The middleware is not patched in anywhere, because which paths stay reachable is your decision:
// apps/api/src/index.ts
import { maintenance } from "./middleware/maintenance";
import { flagsFor, sessionIsAdmin } from "./lib/flags";
app.use(
maintenance({
flags: flagsFor,
isAdmin: sessionIsAdmin,
bypassPaths: ["/health", "/auth", "/admin/auth", "/admin/flags"],
})
);
Three ways past the page, checked in this order: the path is on the bypass list, the flag is off, or the caller is an admin. The path check runs first because it costs no flag read. sessionIsAdmin reads the admin session through getAdminSession, whatever x-auth-audience says, and passes any role in ADMIN_ROLES (admin and superadmin). A client session never counts, even one that belongs to a superadmin.
Put /health on the bypass list. A load balancer that gets a 503 may pull the Worker out of rotation. Put /admin/auth there too, or an admin cannot sign in on the admin app to turn maintenance back off, and /admin/flags, or they cannot reach the switch once signed in. /auth is the client sign-in; leave it on the list only if clients may sign in while the site is closed. A prefix matches whole segments, so /auth does not cover /admin/auth.
If the flag cannot be resolved at all, the request goes through. A store outage should not take an api down on the assumption that somebody meant to close it.
Kill switches
kill.<integration> keys drive assertEnabled:
import { KillSwitchError } from "@repo/feature-flags";
import { flagsFor } from "../lib/flags";
await flagsFor(c).assertEnabled("payments"); // throws KillSwitchError while kill.payments is off
Three ship: kill.payments, kill.ai, kill.email. All three default to on, because a kill switch is off-by-exception and a flag store that has never been published must not stop a project charging its customers. system.maintenance defaults off for the same reason read the other way.
assertEnabled takes the name without the prefix, and the name is typed: assertEnabled("paymnets") fails pnpm typecheck.
Settings
| Key | Default | What it does |
|---|---|---|
FLAGS_ISOLATE_TTL_SECONDS |
10 |
Seconds an isolate serves its cached document. 0 disables the layer. |
The kv settings apply too: KV_PROVIDER selects the store and KV_KEY_PREFIX prefixes the document keys. Two environments sharing one namespace need different prefixes, or they share their flags.
Troubleshooting
| Symptom | Cause |
|---|---|
| A toggle has not taken effect after 20 seconds | Normal. Wait ~70 s. Check again from a different location before you go looking for a bug. |
| Every flag reads as its code default | The document has never been published and the database has no rows. That is the correct state for a fresh install — toggle something. |
| A percentage flag is off for everyone | No subjectId is being passed. There is nothing to bucket, so it resolves false. |
| A tenant sees the global value | No override row exists for that tenant, or tenantId is not being passed to flag(). |
Flag "x.y" is not registered |
The key is not in the flags array in packages/feature-flags/src/index.ts. A new key needs a deploy. |
FLAGS_ISOLATE_TTL_SECONDS is "10s" at startup |
Whole seconds, no unit. Use 10. |
| The admin screen shows a value a live route does not | Expected for about a minute after a toggle. The screen reads the database; the route reads the published document. |
Upgrading from singular table names
This module used to name its tables feature_flag and feature_flag_override. It now names them feature_flags and feature_flag_overrides. A project that installed the old names upgrades them with one migration:
- Run
saasaloy update feature-flagsto take the new schema file. - Run
pnpm --filter @repo/db db:generate. - drizzle-kit asks, for each new table, whether it is created or renamed from an existing table. Pick the rename from the old name:
feature_flag→feature_flags, andfeature_flag_override→feature_flag_overrides. - Read the generated SQL before you apply it. It must rename the tables and the
feature_flag_override_key_tenant_idxindex (ALTER TABLE ... RENAME TO ...), and contain noDROP TABLEorCREATE TABLEfor a table that holds data. - If it drops a table, delete that migration file and run
pnpm --filter @repo/db db:generateagain. - Apply the migration with your driver's command, then open the admin Flags screen to confirm that it lists your flags.
What remove leaves behind
The feature_flags and feature_flag_overrides tables survive removal — run db:generate and read the drop migration before applying it. The published documents survive too: they have no TTL, so flags:v1:global and every flags:v1:t:<tenantId> stay in the namespace until wrangler kv key delete removes them.
If you wired maintenance() into apps/api/src/index.ts by hand, remove that line yourself. saasaloy remove does not undo an edit you made.
Wire-up
add feature-flags writes the block and the screen that renders it, so no page needs a manual import. The block lands at packages/ui/src/blocks/admin/feature-flags.tsx, the route at apps/admin/src/routes/flags.tsx, and a nav-entry patch adds Flags to NAV_AREAS in apps/admin/src/components/nav.ts.
- Run
pnpm db:generateand apply the migration with your driver's command, so the two flag tables exist. - Run
pnpm dev, sign in toadminas an admin, and open/flags. The router plugin regeneratesrouteTree.gen.tson the first start. - When the screen is missing from the nav, check that
NAV_AREASstill holds the Flags entry. A hand edit tonav.tscan drop it.
Next
Run saasaloy-admin to change the Flags screen or add a staff screen beside it.
04 同仓库更多 Skills
saasaloy-subscription
Runbook for the subscription capability — provider-agnostic subscriptions in packages/subscription, with a plan file, a projection table, a billable-subject file, plan entitlements and per-provider modules. Use when adding a plan, adding a plan feature or a numeric limit, gating an API route with requireFeature or requireWithinLimit, reading the 402 it returns, working out why a subject resolves to the free plan, wiring a checkout or portal route, reading a SubscriptionError, choosing or switching SUBSCRIPTION_PROVIDER, registering a webhook in the vendor's dashboard, writing a third subscription provider, or upgrading a project that still has `billing` installed.
saasaloy-notification
Runbook for the notification module, in-app notifications for apps/app. It owns the notifications and notification_opt_outs tables, notify() in apps/api/src/lib/notify.ts, the /notifications api routes, the bell in the app header, the sidebar badge, the /notifications page with Inbox and Preferences tabs, the notification config section, and the daily prune. Use when sending a notification from an api route, adding or changing a category, changing retention, debugging a badge or bell count that does not move, or removing the module.
saasaloy-cms
Runbook for the cms module, the in-house CMS behind apps/admin. Covers code-defined collections, the Tiptap editor, drafts, revisions and redirects, the publish-time renderer, the public blog on apps/web and its 60-second cache. Use when adding a collection or a field, rendering a stored body, reusing the editor, renderer or media field from another module, moving the blog to another path, re-rendering live posts after a renderer upgrade or a STORAGE_PUBLIC_URL change, or working out why a published post is not live yet.
saasaloy-beta
The beta module's own procedure.
helper
暂无简介
saasaloy-update
Runbook for an AI agent that updates this project to a newer Saasaloy CLI and newer module versions with saasaloy update. Use when the owner says "update saasaloy", "upgrade the CLI", "pull the latest modules", "update the base template", "work the merge plan", "clear the update notes", when saasaloy update or saasaloy outdated --check exits 3 or 2, when a merge plan or .saasaloy/merge-plan.md is handed to you, or when an adopted project takes its first update. Covers the CLI upgrade, the preview, --out, the file merges, the update notes and "Notes that may apply", --resolved and --done, a file deleted locally and --restore, a three-way or two-way base, an update that skips releases, --from, the verify commands, and the exit codes.