Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
Feature flags — implementation plan
Spec: .ai/specs/2026-10-28-feature-flags.md
Research: .ai/research/2026-10-28-feature-flags.md
Closest sibling: the OTEL exporter config (packages/logger/src/tracing.server.ts); for the hook, apps/erp/app/hooks/useFlags.tsx
Branch: feat/feature-flags (worktree ../carbon-feature-flags)
Progress
- Task 1: Create the branch worktree
- Task 2: Add the OpenFeature dependencies and exports
- Task 3: Add the two environment variables
- Task 4: Add the flag registry
- Task 5: Build the provider and its tests
- Task 6: Build the Redis flags store and its test
- Task 7: Start the provider in the ERP and MES entries
- Task 8: Send browser flags and extend
useFlags - Task 9: Put Settings → Companies behind
multi-company-management - Task 10: Put integration early access behind
integration-early-access - Task 11: Add the
flags-through-envcheck - Task 12: Pass the variables in deploy config and
ci/src/deploy.ts - Task 13: Update the docs and AGENTS files
- Task 14: Run every gate and verify in the browser
Changes during the build
- Task 6: the Redis store has no unit test.
test-mocks-own-modulerefusesvi.mock("./client"), and.claude/rules/testing.mdtreats glue as covered by typecheck. - Task 8:
no-duplicated-app-filerefuses auseFlags.tsxin both apps. The hook isuseFeatureFlags()in@carbon/react. The ERPuseFlagsreturns itsfeature; the MES has no hook of its own. - Task 12:
ci/src/deploy.tscaps the document at 32 KiB, because an ECS task definition is at most 64 KiB.cihas no tsconfig, so esbuild only checks that the file parses. - Task 14: no dev stack runs for this worktree, so no browser test ran. The MES production build loads, and its flags provider starts from a
data:endpoint.
Dependencies
Task 1 first. Task 2 before Tasks 4–6. Task 3 before Task 5. Tasks 5 and 6 before Task 7. Task 7 before Tasks 8–10. Tasks 11, 12 and 13 are independent of Tasks 8–10. Task 14 is last.
Task 1: Create the branch worktree
Depends on: none Files:
- Create:
../carbon-feature-flags(git worktree)
Steps:
- Run
git worktree add ../carbon-feature-flags -b feat/feature-flags mainfrom the main checkout. - Copy the spec, research and plan files into the same paths in the worktree.
- Run
pnpm installin the worktree.
Verify:
git -C ../carbon-feature-flags branch --show-current
# Expected: feat/feature-flags
Out of scope: the main checkout. Commit nothing there.
Task 2: Add the OpenFeature dependencies and exports
Depends on: 1 Files:
- Modify:
pnpm-workspace.yaml— add tocatalog::"@openfeature/core": 1.12.0,"@openfeature/server-sdk": 1.23.0,"@openfeature/flagd-core": 5.0.0. - Modify:
packages/env/package.json— add the 3 packages as"catalog:"dependencies. Add the exports"./flags": "./src/flags.ts"and"./flags.server": "./src/flags.server.ts". - Copy from (precedent): the
"./push.server"export in the same file.
Steps:
- Edit the two files.
- Run a bare
pnpm install. Never use--filter, because it prunes the lockfile. - Check
git diff --stat pnpm-lock.yaml. It must show added lines only for the 3 packages and their dependencies.
Verify:
pnpm --filter @carbon/env exec node -e "require.resolve('@openfeature/flagd-core')" && echo ok
# Expected: ok
Out of scope: any other package's dependencies.
Task 3: Add the two environment variables
Depends on: 1 Files:
- Modify:
packages/env/src/schema.ts— add afterSYSTEM_STATUS_URL:CARBON_FLAGS_ENDPOINT:{ group: "core", description: "URL of the feature flags document: https:, data: or file:", type: url }CARBON_FLAGS_HEADERS:{ group: "core", description: "Headers for CARBON_FLAGS_ENDPOINT, as k=v pairs separated by commas (OTEL format)", secret: true }
- Modify:
packages/env/src/index.ts— addexport const CARBON_FLAGS_ENDPOINT = getEnv("CARBON_FLAGS_ENDPOINT");and the same forCARBON_FLAGS_HEADERS, afterSYSTEM_STATUS_URL.
Verify:
pnpm --filter @carbon/env test
# Expected: all tests pass
Out of scope: getBrowserEnv(). Neither variable goes to the browser.
Task 4: Add the flag registry
Depends on: 2 Files:
- Create:
packages/env/src/flags.ts
Steps:
- Write the SPDX header with
pnpm --filter @carbon/checks license-headersafter the file exists. - Export
FLAGSwith the 2 v1 entries (spec section 10),as const. - Export the types
FlagKey,FlagValue<K>(boolean, string or number fromtype),BrowserFlagKey(keys withbrowser: true) andBrowserFlags(Partial<Record<BrowserFlagKey, …>>). - Export
flagDefault(key), which returnsFLAGS[key].default. - Import nothing from
.serverfiles and nothing fromnode:*. This file reaches the browser.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/env --concurrency=1
# Expected: Tasks: 1 successful
Task 5: Build the provider and its tests
Depends on: 2, 3, 4 Files:
- Create:
packages/env/src/flags.server.ts - Create:
packages/env/src/flags.server.test.ts - Copy from (precedent): the tested scratch provider
nodeflags/provider.tsin the session scratchpad. Rewrite it for the three layers below.
Steps:
- Export
parseHeaders(raw). Skip a pair with no=or a bad%sequence, log it, and never throw. - Export the type
FlagsStore:export type StoredFlags = { body: string; etag: string; fetchedAt: number }; export type FlagsStore = { readMeta(scope: string): Promise<Pick<StoredFlags, "etag" | "fetchedAt"> | null>; readBody(scope: string): Promise<string | null>; write(scope: string, value: Partial<StoredFlags> & { fetchedAt: number }): Promise<void>; tryLease(scope: string): Promise<{ status: "acquired"; release: () => Promise<void> } | { status: "held" } | { status: "unavailable" }>; }; - Export
CarbonFlagsProvider, an OpenFeatureProvider(runsOn = "server"). Implement the behavior list in spec section 3, steps 1–11, exactly. - Compute
scopeas the first 16 hex characters ofsha256(endpoint + "\n" + headers). - Use these constants: sync 10 s, source 60 s, jitter ± 20%, fetch timeout 5 s, boot wait 2 s, lease 10 s, body cap 1 MB.
- Add
tenancy(SINGLE_TENANT ? "single" : "multi") andedition(CarbonEdition) to every evaluation context. - Export
initFlags({ store, waitUntil }). It readsCARBON_FLAGS_ENDPOINTandCARBON_FLAGS_HEADERSfrom./index, builds the provider and callsOpenFeature.setProviderAndWait. A second call does nothing. - Export
flags.bool,flags.string,flags.number. Each takes(key, { companyId, userId? }), takes the default fromFLAGS, and never throws. - Export
evaluateBrowserFlags({ companyId, userId }). It returnsBrowserFlagsfor every key withbrowser: true. - Export
flagStatus(), which returns{ confirmedAt, lastError }. - Write one test per acceptance criterion of the provider in the spec. Use a local
node:httpserver and an in-memoryFlagsStore. Cover these cases:- unset endpoint
- company targeting, user targeting,
tenancyandedition - ETag and
304 - the header parse
- the 1 MB cap
- boot that waits at most 2 s with an empty store and does not wait with a filled store
- 5 providers on one store, with at most 1 source request
- a store write that reaches a second provider
- an unavailable store
- a
data:endpoint that never fetches again - one archived-flag warning per key
- Run the license-header fixer.
If vi.useFakeTimers blocks the local server, STOP and report — do not improvise.
Verify:
pnpm --filter @carbon/env test -- flags.server
# Expected: every flags.server test passes
Out of scope: Redis. This package must not import @carbon/kv.
Task 6: Build the Redis flags store and its test
Depends on: 5 Files:
- Create:
packages/kv/src/flags-store.ts - Create:
packages/kv/src/flags-store.test.ts - Modify:
packages/kv/src/index.ts— exportredisFlagsStore. - Copy from (precedent):
packages/kv/src/lease.tsandlease.test.ts.
Steps:
- Import the type
FlagsStorefrom@carbon/env/flags.server. - Implement it on the hash
flags:<scope>:readMetaisHMGET etag fetchedAt,readBodyisHGET body, andwriteis oneHSET. - Implement
tryLeasewithacquireLease("flags:<scope>:refresh", 10_000)andreleaseLease. - Test it with
ioredis-mock, the waylease.test.tsdoes.
Verify:
pnpm --filter @carbon/kv test -- flags-store
# Expected: every flags-store test passes
Task 7: Start the provider in the ERP and MES entries
Depends on: 5, 6 Files:
- Modify:
apps/erp/app/entry.server.tsx,apps/mes/app/entry.server.tsx
Steps:
- Import
initFlagsfrom@carbon/env/flags.serverandredisFlagsStorefrom@carbon/kv. - At module level, after the tracing setup, call
void initFlags({ store: redisFlagsStore, waitUntil }).waitUntilis already imported from@vercel/functions.
Verify:
pnpm exec turbo run typecheck --filter=erp --concurrency=1 && pnpm exec turbo run typecheck --filter=mes --concurrency=1
# Expected: both successful
Task 8: Send browser flags and extend useFlags
Depends on: 7 Files:
- Modify:
apps/erp/app/routes/x+/_layout.tsx,apps/mes/app/routes/x+/_layout.tsx— addflags: await evaluateBrowserFlags({ companyId, userId })to thedata({ … })return. - Modify:
apps/erp/app/hooks/useFlags.tsx— addfeature(key: BrowserFlagKey). It readsflagswithuseRouteData(path.to.authenticatedRoot)and falls back toflagDefault(key). - Create:
apps/mes/app/hooks/useFlags.tsx— the samefeature(key). Read the MES layout data the wayapps/mes/app/hooks/useUser.tsxdoes. - Copy from (precedent):
apps/erp/app/hooks/useFlags.tsx.
Verify:
pnpm exec turbo run typecheck --filter=erp --concurrency=1 && pnpm exec turbo run typecheck --filter=mes --concurrency=1
# Expected: both successful
Out of scope: shouldRevalidate. Do not change it.
Task 9: Put Settings → Companies behind multi-company-management
Depends on: 8 Files:
- Modify:
apps/erp/app/routes/x+/settings+/companies.tsx,companies.$id.tsx,companies.delete.$id.tsx - Modify:
apps/erp/app/modules/settings/ui/useSettingsSubmodules.tsx
Steps:
- In each loader and action, replace
if (!isInternalEmail(email))withif (!isInternalEmail(email) && !(await flags.bool("multi-company-management", { companyId, userId }))). - Get
companyIdanduserIdfrom therequirePermissionsresult that each route already has. - In
useSettingsSubmodules, hidepath.to.companiesonly when!isInternal && !feature("multi-company-management").
Verify:
pnpm exec turbo run typecheck --filter=erp --concurrency=1
# Expected: successful
Task 10: Put integration early access behind integration-early-access
Depends on: 7 Files:
- Modify:
packages/ee/src/plan.server.ts—hasIntegrationEarlyAccess({ companyId, userId })returnsflags.bool("integration-early-access", { companyId, userId }). DeleteisListedUserIdif nothing else calls it. - Modify:
apps/erp/app/modules/settings/settings.service.ts(isIntegrationInstallWithheld) andapps/erp/app/routes/x+/settings+/integrations.tsx— pass{ companyId, userId }andawaitthe result. - Modify:
packages/env/src/schema.ts,packages/env/src/index.ts— removeINTEGRATION_EARLY_ACCESS_USER_IDS.
Steps:
- Run
grep -rn "INTEGRATION_EARLY_ACCESS_USER_IDS\|hasIntegrationEarlyAccess\|isListedUserId" apps packagesfirst. Change every hit. - Keep
isIntegrationComingSoonandplan.test.tsunchanged.
Verify:
pnpm --filter @carbon/ee test && pnpm exec turbo run typecheck --filter=@carbon/ee --concurrency=1
# Expected: tests pass, typecheck successful
Task 11: Add the flags-through-env check
Depends on: 1 Files:
- Create:
packages/checks/src/conformance/flags-through-env.ts,flags-through-env.test.ts - Modify:
packages/checks/src/index.ts,packages/checks/src/run.ts— register the check. - Copy from (precedent):
packages/checks/src/conformance/no-integration-id-branching.tsand its test.
Steps:
- Flag any import of
@openfeature/in a TypeScript file outsidepackages/env/. - Name the fix in the message: "Read flags with
flags.*from@carbon/env/flags.server."
Verify:
pnpm --filter @carbon/checks test -- flags-through-env
# Expected: the test passes
Task 12: Pass the variables in deploy config and ci/src/deploy.ts
Depends on: 3 Files:
- Modify:
.env.example— 2 commented lines afterSYSTEM_STATUS_URL. - Modify:
contrib/deploying/simple-docker-caddy/docker-compose.prod.yml—CARBON_FLAGS_ENDPOINT: ${CARBON_FLAGS_ENDPOINT:-}and the same for the headers, in both services. - Modify:
sst.config.ts— forwardCARBON_FLAGS_ENDPOINTin both environment blocks, next toSYSTEM_STATUS_URL. - Modify:
ci/src/deploy.ts— addflags_endpointandflags_token(bothstring | null) toWorkspace.
Steps:
- In
ci/src/deploy.ts, before the$$env object, fetchflags_endpointwithAuthorization: Bearer <flags_token>whenflags_endpointis set. - Use a 10 s timeout and a 1 MB cap. On any failure, throw, so this workspace's deploy fails.
- Pass
CARBON_FLAGS_ENDPOINT: "data:application/json;base64," + base64(body)in the env object.
If the workspaces table has no flags_endpoint column, add the field to the type anyway. STOP and report that the CI database needs the 2 columns — do not create them.
Verify:
pnpm exec turbo run typecheck --filter=ci --concurrency=1
# Expected: successful
Task 13: Update the docs and AGENTS files
Depends on: 10 Files:
- Modify:
.claude/rules/environment-configuration.md— a "Feature flags" paragraph after "Maintenance and status". RemoveINTEGRATION_EARLY_ACCESS_USER_IDS. - Modify:
packages/env/AGENTS.md— the flags exports, the registry and the lifecycle. - Modify:
packages/kv/AGENTS.mdif it exists —redisFlagsStore. - Modify:
.claude/rules/onshape-panel.md,packages/ee/AGENTS.md— replaceINTEGRATION_EARLY_ACCESS_USER_IDSwith theintegration-early-accessflag.
Verify:
grep -rn "INTEGRATION_EARLY_ACCESS_USER_IDS" apps packages .claude --include=*.ts --include=*.tsx --include=*.md
# Expected: no output
Task 14: Run every gate and verify in the browser
Depends on: all Steps:
- Run
pnpm --filter @carbon/checks license-headers. - Run the typechecks one at a time:
@carbon/env,@carbon/kv,@carbon/ee,@carbon/checks,erp,mes,ci. - Run the tests:
@carbon/env,@carbon/kv,@carbon/ee,@carbon/checks. - Run
pnpm run lint. - Run
pnpm --filter @carbon/checks startand confirm the new check reports no violation. - If a dev stack runs, use
/testwith noCARBON_FLAGS_ENDPOINT. A user with a Carbon email opens Settings → Companies. Then set adata:endpoint that turnsmulti-company-managementon for the company, and confirm the page for a user without a Carbon email.
Verify:
pnpm run lint
# Expected: no errors
Out of scope: the console, the CSA dashboard and the byoc repo.