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.
Service function migration — the whole plan
Progress
- Phase 0 — Foundation (
refactor/service-functions) - Phase 1 — One gate for every caller (
fix/gate-every-caller, merged into this branch) - Phase 2 — Pipeline: the manifest from service functions
- Phase 3 — Pilot: the production job lifecycle (review gate before Phase 4)
- Phase 4 — Module waves
- Wave 1: production, purchasing, inventory, sales (agents, merged)
- Wave 2: items, quality, invoicing, accounting (agents, merged)
- Wave 3: resources, settings, users/account/people, shared/documents/workflows/integrations (merged)
- Publish only what routes use: 201 legacy tools that no route calls are unpublished
- Single-function tools: a codemod turns each route-called legacy tool into a leaf (below)
- Phase 5 — Dissolve
packages/server-functions(package deleted ind956b0be11)- The pattern: a function's body becomes a building block in
<module>.service.ts;settleorinvokePostingruns it with the function's own error rules (.claude/rules/module-files.md) - 39 functions ported:
close-job,assign-serial-numbers,preview-serial-unit-costs,recost-serial-unit,post-asset-transfer,preview-asset-capitalization,seed-company,finalize-supplier-quote,finalize-purchasing-rfq,post-inventory-count,post-nonconformance,apply-account-import,post-stock-transfer,import-csv,correct-stock-movement,sync,get-method,convert,kanban-replenish,update-purchased-prices,create-rental-invoices, and in one batch the posting cluster:post-sales-invoice,post-purchase-invoice,post-memo,post-payment,post-receipt,post-shipment,post-charge,post-reimbursement,post-rental-agreement,post-customer-contract,create-contract-invoices,post-lease,create-lease-invoices,activate-accounting,journal-legacy-documentsand the three revenue-run functions - Their live tests run from
apps/erp/test/database/ - Every function only the ERP, jobs and ee call is ported; the postings registry runs only ported bodies
- The seam for jobs and ee (steps 2 and 3):
@carbon/api/postings.server. The ERP registers 12 postings at start; jobs and ee callposting(name, handles, input) - The 10 functions of the MES closure are ported with Phase 6:
batch-operations,create,issue,post-inventory-adjustment,post-maintenance-event,post-picking,post-production-event,recalculate,reschedule,trigger-rework -
route-action-shapeflagsinvokePostingandinvokeCreatein a route. Theserver-fn-authorizes-callercheck and theserverFnspattern are deleted with the package - The shared
libhelpers are inshared.service.ts
- The pattern: a function's body becomes a building block in
- Phase 6 — MES calls the ERP's postings over signed HTTP calls (design approved 2026-10-17;
.ai/plans/2026-10-17-mes-calls-erp-postings.md). Verification against the local stack is still open there - Phase 7 — Clean up
- Module layout:
<module>.server.tsoperations,<module>.service.tsbuilding blocks, no barrels; flat names<module>_<function>;accountmerged intousers - Legacy pipeline deleted (dispatcher, registry, service-file parser, validator registry,
sql-effects, blocked/exposure lists, aliases) - Docs and rules rewritten; lessons added
- #1842 fixes: paged lists, flat inputs (
argsand single-object wrappers), stamped columns (stamp-matches-columnscheck) -
serviceContextstill built directly by two routes (kanban scan, agent chat) andcarbon.server.ts;assertIsPost/flash/bypassRlssweep not done - Changelog entry: draft kept, not written until the user asks
- Module layout:
Deviations from the approved plan, with the reason:
- Phase 0 step 1:
module-shape.tsneeds no change.loadModulesreads module folders only, so loose files inapps/erp/app/modules/pass. - Phase 0 step 12:
route-action-shapematches only patterns that are certainly writes (a.from(...)write, Kysely writes,serverFns). A route that calls two write functions is not detected: the check cannot tell a write function from a read by its name. It baselines 237 hits in 161 route files. replanAfterItemChangepermission:"system"→{ update: "parts" }. Six item routes call it as the user, so the gate would have refused every replan.- Phase 5 steps 2 and 3: jobs and ee get the postings, not the router. They call a posting's body (post an invoice, draft the recurring invoices), never the operation a route calls, which does more (sales rules, emails). The contract in
@carbon/api/postings.servertypes each input and the result its callers read, because neither package can see the ERP's types. - The gate checks the entry call only. A call from inside another service function's
runis not checked again, so"system"functions can be steps of user operations. useActionshows a default toast only for a service function's error ({ error: { category, message } }). Older{ success: false }results still flash from the action, and a second toast would double them.- Phase 1: the in-app agent calls no operations (docs and navigation tools only), so only the workflow dispatch changed. The changelog line waits for the release's entry.
- Phase 2: a refusal uses oRPC's own error body with
data.category, not a new{ error }envelope, so every v1 error has one shape. Input schemas come from the leaf's TypeScript type, like the legacy tools, not fromz.toJSONSchema. The generator does not drop legacy tools by itself: a module's migration removes@mcpfrom the primitives it replaces. - Phase 3: release is one operation for the job page, bulk release, planning and the API (
production.job.release), with every check inside; batch release likewise (production.batch.release). The Make to Asset and "nothing to make" rules moved intogetJobReleaseReadiness, so batch release now applies them too. The status route lost its release branch and a dead plan-and-schedule branch.planDraftJobstays unpublished: its checks depend on the planning drawer's location. - Phase 4: the waves moved the composed route actions (two or more writes) only. A route that makes one service call keeps it; its function stays a legacy tool until the codemod converts it.
- Phase 4: the codemod (
legacy-to-leaves.mjs, scratchpad) writes<module>.tools.server.ts. Each leaf has a zod input from the published schema, the same permission, a typed positional call, the auth stamping (stamp) and the result reader (envelope,paged,plain,envelopes,flag) inservice-tools.server.ts. An upsert with an id rule splits intocreateandupdate. Names are mechanical:getCustomersbecomessales_getCustomers. - Phase 4: the dispatcher's sales-rule gate is deleted. Every tool it checked is unpublished;
sales.quote.setStatusandsales.order.setStatushold the gate for every caller now. - Phase 4: an operation called through the API, MCP or a workflow gets the company-scoped client the dispatcher used (
scopedToCompany).
Context
MCP tools and the v1 API are generated from *.service.ts exports. The real logic of most operations lives in route actions. So a tool such as production_updateJobStatus does less than the Release button does. Callers cannot trust a tool to match the UI.
The fix: every operation is one defineServiceFn const. Routes, the HTTP API, MCP, workflows, jobs and MES call that same const. One permission gate runs for every caller. The API and MCP manifest comes from the consts and their TSDoc.
Scale, from .ai/research/2026-10-09-service-function-inventory.md (the work list for this plan):
| Count | |
|---|---|
| Service functions (ERP 1915, MES 145, ee 173) | 2233 |
| Route actions | 1062 |
| Actions that already make one service call | 676 |
| Actions with logic in the route (need a new function) | 334 (439 functions) |
Server functions in packages/server-functions |
about 50 |
Already built on refactor/service-functions (worktree .claude/worktrees/service-functions, merged with main at aaf1b4fa50): defineServiceFn, CarbonError, ctx.transaction, serviceContext, and 6 production functions with 12 routes that call them.
Decisions (locked by the user)
| Topic | Decision |
|---|---|
| Helper | defineServiceFn; oRPC is the transport |
| Context | actor, company, asCaller.supabase, asSystem.supabase, asSystem.pg, kv, auditUserId, error(), transaction() |
| Errors | return ctx.error(category, message, cause); the type system derives the error union; a returned error rolls back ctx.transaction |
| Schemas | z.compile; the output schema is optional |
| Docs | TSDoc summary = title, @remarks = description, @mcp / @mcp destructive = published |
| Gate | One permission check for every caller. Workflows are enforced from day one |
| Route action | Validator first (method, params, form), then auth, then one call, then return the result |
| Names | Rename outright to {module}_{entity}_{verb}; no aliases |
| Files | Drain gradually into {module}.server.ts, {module}.helpers.ts, {module}.models.ts; discard wip/production-merge-trial |
| Primitives | Unpublish each primitive that is unsafe alone |
| Home | Module code stays in apps/erp/app/modules. The shared helper lives in the modules folder. packages/server-functions dissolves into the modules |
| MES | MES calls ERP's API for operations it does not own |
| Idempotency | Spec only, no build |
| Legacy tools | Publish only what routes use. An operation, or a legacy function a route calls directly, is published. A function only an operation calls is not. Exceptions: the file-upload pair, and tools the workflow catalog names |
End state
// apps/erp/app/modules/production/production.server.ts
/**
* Cancel a job.
*
* @remarks
* Returns picked material, closes picking lists, then sets the job Cancelled.
*
* @mcp destructive
*/
export const cancelJob = defineServiceFn({
input: z.object({ jobId: z.string() }),
permission: { update: "production" },
async run(ctx, { jobId }) { /* ... */ }
});
// apps/erp/app/modules/production/index.server.ts — the module's public surface
export const production = { job: { cancel: cancelJob, release: releaseJobs } };
// route action
export async function action(args: ActionFunctionArgs) {
const input = await validator().request(args, { params: ["jobId"] });
const api = await carbon(args.request);
return api.production.job.cancel({ jobId: input.params.jobId });
}
- MCP tool
production_cancelJob, API pathPOST /api/v1/production/job/cancel. - The component uses
useAction; an error shows a toast by default. - Side effect of validator-first: an unauthenticated caller with a bad request gets 400 or 405, not a login redirect.
Phases
Each phase is its own PR from main, except where a phase says otherwise. Never push without approval.
Phase 0 — Foundation (on refactor/service-functions)
- Move
service-fn.server.tsandservice-context.server.tsfromapps/erp/app/services/toapps/erp/app/modules/. Updatepackages/checksmodule-shape.tsto allow these shared files. - Add
accessto the context: the caller's claims (session, OAuth, workflow owner) or key scopes (API key). Resolve it once per request. - Run the gate inside
defineServiceFn'scall, beforerun. ReusehasPermissionsfrompackages/server-functions/src/permissions.ts. - Give
{}one meaning everywhere: company membership."system"allows only thesystemactor. - Support an input-keyed permission (
{ by, rules }), copied fromdefineServerFn(define-server-fn.ts:24-41). - Make the transaction context a full
ServiceContext, so service functions compose insidectx.transaction. ItsasCaller.supabasethrows on use, because that client is outside the transaction. - Add
toResult(result): it turns aCarbonErrorinto{ category, message, status }. A returnedErrorinstance otherwise loses its fields to turbo-stream sanitising. - Add
validator(schema?).request(args, { method?, params? })topackages/form/src/server.ts:- Check the method first (default POST; throw 405).
- Check the required params (throw 400).
- Validate the form. Return
{ data, params, formData }or{ invalid }(fromvalidationError).
- Add
carbon(request)inapps/erp/app/modules/carbon.server.ts:- Call
requirePermissions(request, {}). Extend it to return the claims it already reads (auth.server.ts:348). - Build the context.
- Return a lazy proxy over the router. Each leaf returns
toResult(...).
- Call
- Add
apps/erp/app/modules/router.server.ts. It combines each module'sindex.server.ts. - Give
useAction(packages/query/src/useAction.ts) a defaultonErrorthat showserror.messageas a toast. - Add the conformance check
route-action-shape(packages/checks). An ERP route action fails it if it writes through a client, callsserverFns, or calls more than one write function. Baseline every current hit. A PR may only remove baseline entries. - Write the idempotency spec (
.ai/specs/2026-10-15-service-fn-idempotency.md, unlogged Postgres table). No code. - Copy this plan to
.ai/plans/2026-10-15-service-function-migration.md.
Phase 1 — One gate for every caller (own PR from main, ships first)
This is a security fix that does not wait for the refactor.
- In
apps/erp/app/routes/api+/v1+/lib/base.server.ts(gate, around line 94), checksessioncallers against their claims too. - Pass the workflow owner's claims into the dispatch context (
api+/inngest.ts:44-46). The engine already reads them inengine/owner.ts:58-76. - Do the same for the in-app agent.
- Remove the hand-written re-checks in
production.mcp.server.ts. - Add a changelog entry: workflows now fail at a step the owner has no permission for.
Phase 2 — Pipeline: the manifest from service functions (on refactor/service-functions)
- Add
scripts/lib/router-docs.ts. It uses the generator's ts-morphProject(response-schema.ts) to walkrouter.server.ts→ eachindex.server.tsobject → each leaf const. For each leaf it reads:- the summary line (title; the generator fails without one),
@remarks(description),@mcp/@mcp destructive(published, destructive).
- Build the runtime manifest entry from the live const: input JSON schema from
z.toJSONSchema(fn.input), permission fromfn.permission. - Classify each entry:
view= READ,create/update= WRITE,deleteordestructive= DESTRUCTIVE. - Merge both sources into
tool-metadata.json. The generator drops a legacy entry when its module's router publishes the replacement. - In
router.server.ts(v1), build one procedure per router leaf. Path is/{module}/{entity}/{verb}. ReusejsonSchemaInput,tracedandgate. - Map a returned
CarbonErrorto anORPCErrorwith itsERROR_STATUSstatus and the body{ error: { category, message } }. Fold the same shape incallOperation(call.server.ts) for MCP. - Extend the pre-commit trigger (
scripts/git-hooks/pre-commit:27) to*.server.tsandindex.server.ts. - Regenerate the docs (
pnpm --filter docs generate:api) and the digest (pnpm check:manifest).
Phase 3 — Pilot: the production job lifecycle (on refactor/service-functions)
- Add the 6 existing production functions to
production/index.server.ts, with@mcptags. - Convert their 12 routes to the end-state action. Move their components to
useAction. - Unpublish the primitives they replace. Remove
@mcpfrom functions such as the bare job status update. - Rename the 4 tool names in
packages/ee/src/workflows/catalog/actions.ts. Runscripts/check-workflow-catalog.ts. - Measure the result: lines per action, test count, sweep result, and a browser smoke test of release and cancel.
- Stop for review. The user approves the pilot before Phase 4 starts.
Phase 4 — Module waves (one PR per slice)
Use the per-module recipe below. Split a module into slices of about 30 routes, by entity, so each PR stays reviewable.
| Wave | Modules | Note |
|---|---|---|
| 1 | production (rest), purchasing, sales | most published tools, most logic in routes |
| 2 | inventory, items, quality, invoicing | |
| 3 | accounting, resources, people | |
| 4 | users, account, shared, documents, workflows | |
| 5 | settings | one settings.{page}.update(partial) per settings page, not one function per toggle (101 → about 15) |
| 6 | api+, auth, agent routes |
Rules that apply to every wave:
packages/eecode keeps its body and itsrequireEntitlementinpackages/ee(license boundary). The ERP service function wraps it. Files with.ee.in the name keep the infix.- Loaders: reads become service functions too (
list,get), because the API must cover what the app uses. Convert the 1:1 reads with a ts-morph codemod, written in the scratchpad: rewritefn(client, …)into adefineServiceFnwithctx.asCaller.supabase. - Client-safe functions: the 7 functions that client code imports stay in
{module}.service.ts. Every other function leaves it.
Phase 5 — Dissolve packages/server-functions
- Move each server function into its owning module's
.server.tsas adefineServiceFn. MapServerFnContext.dbtoctx.asSystem.pg. - Give the jobs package the router through an injection seam, like
setWorkflowDispatch(api+/inngest.ts). Jobs build asystemcontext. - Give
packages/eeits callers (post-payment,post-production-event) through its existingdepsinjection. - Move the helpers the functions share (
assertCompanyRecords,toJson,fetchAlluse) intomodules/shared. - Keep the 9 functions MES uses in the package until Phase 6 is done. Then move them and delete the package.
Phase 6 — MES calls ERP's API
- Design the MES→ERP auth branch on
/api/v1:- MES sends the user's Supabase access token as a Bearer token.
- MES forwards the signed console pin-in. ERP re-validates it with
resolveConsolePinIn(auth.server.ts). - ERP never accepts cookies on
/api/v1(CSRF). - This is an auth change: get user approval on the design before building.
- Generate a typed client in
packages/api. Print input and output types from the ERP consts with the generator's TypeChecker. MES cannot import ERP code. - Move the writes of the 57 MES route actions into ERP service functions under the owning module (
production.operation.issue, …). MES actions call them through the client. - Measure the issue round trip before and after.
- MES reads stay in
apps/mes/app/services. Shop-floor screens keep their direct reads.
Phase 7 — Clean up
- Delete the legacy pipeline:
- the textual parsing in
scripts/lib/service-metadata.ts registry.server.tsand the positional dispatch indispatch.server.ts- the
{mod}.mcp.server.tscompanions OPERATION_ALIASESand the emptied entries inmcp-blocked-tools.ts
- the textual parsing in
- Delete
serviceContext, thenassertIsPostandflashfrom migrated actions. DeletebypassRlsif nothing uses it. - Rewrite the docs:
.claude/rules/conventions-services.md.claude/rules/mcp-tools-reference.md- each module
AGENTS.md - the root
AGENTS.md(the module layout and the "one{module}.service.ts" rule) .ai/docs/module-conventions.md
- Add the lessons to
.ai/lessons.md.
The per-module recipe
- Read the module's rows in the inventory (sections 3 and 4). Review the proposed names, because the naming rules have known flaws.
- For each route action, write the service function in
{module}.server.ts:- input: the existing
.models.tsvalidator - permission: copied from the route's
requirePermissions - TSDoc with a summary line, plus
@remarksand@mcpwhere it is safe to publish
- input: the existing
- Move the action's logic into
run. Keep the behavior the same. List each deliberate difference in the PR. - Rewrite the action to the end-state shape: validator,
carbon, one call, return. - Move the component to
useAction. Put the success toast in the component. - Move each building block the function calls from
.service.tsto.server.ts. Move pure helpers to.helpers.ts. - Add the leaves to
index.server.ts. - Remove
@mcpfrom each primitive that is unsafe alone. - Update the tests:
- Test service functions with
createServiceContextand stub handles (production.server.test.tsshows the pattern). - Route tests mock
carbon.
- Test service functions with
- Remove the module's entries from the
route-action-shapebaseline. - Regenerate the manifest and the docs. Update the module
AGENTS.md. Add a changelog line that lists the renamed tools.
Critical files
apps/erp/app/services/service-fn.server.ts,service-context.server.ts→apps/erp/app/modules/packages/form/src/server.ts,packages/form/src/zod.ts(validator)packages/query/src/useAction.tspackages/auth/src/services/auth.server.ts(requirePermissionsreturns the claims)apps/erp/app/routes/api+/v1+/lib/{base,router,call,dispatch,registry,operations}.server.tsapps/erp/app/routes/api+/inngest.ts,packages/jobs/src/workflows/engine/owner.tsscripts/generate-mcp.ts,scripts/lib/{service-metadata,service-ast,response-schema}.tspackages/checks/src/run.ts,src/conformance/module-shape.tspackages/ee/src/workflows/catalog/actions.tspackages/server-functions/src/*(moved in Phase 5)
Verification (every PR)
- Run
pnpm exec turbo run typecheck --filter=erp --concurrency=1, and the same for each other package the PR touches. - Run
pnpm --filter erp testandpnpm --filter @carbon/checks test. - Run Biome on the changed files with
--vcs-use-ignore-file=false(worktree). - Run
npx tsx scripts/generate-mcp.ts, thenpnpm check:manifest. - Run
pnpm sweep:toolsfor the module against a seeded stack. - Run
pnpm --filter docs generate:apiand the docs typecheck. - Smoke-test the module's main flows in the browser, with the user's permission (
/test). - Live MCP smoke tests (Phases 1–3):
- Call a renamed tool through
call_toolwith an API key and with OAuth. - Confirm a 403 when the caller lacks the permission.
- Confirm a workflow fails at a step its owner lacks the permission for.
- Call a renamed tool through
Risks
- Repeated renames: tools change name each time a module ships. External clients must update per wave. Each wave's changelog lists the renames.
- Workflows: enforcing workflows from day one can stop a running customer automation. The Phase 1 changelog says so.
- Behavior drift: moving logic out of an action can change it. Known cases: a throw now returns
internal, and database messages become "Something went wrong". Each PR lists its differences. - MES: issue goes from in-process to HTTP, so it gains a network hop and depends on ERP. Phase 6 measures the issue round trip before and after.
- Merge conflicts on a long branch: keep slices small and merge each one fast.