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.
Workflows Phase 2: Event Catalog — implementation plan
Spec / source: .ai/specs/2026-07-30-workflows-event-catalog.md
Branch: feat/automation
Progress
- Task 1: Grow
CatalogEventwithpermission+match - Task 2: Write the hand-written entity registry (
entities.ts) - Task 3: Write the hand-written moment declarations (
moments.ts) - Task 4: Write the pure
buildCatalogtransform + unit tests - Task 5: Write the generator script and commit the two generated files
- Task 6:
createEventCatalog()+ barrel exports - Task 7: Write
scripts/check-workflow-catalog.ts+ CI job - Task 8:
raiseMomentinpackages/lib+ the new Inngest event - Task 9: Raise sites — production + MES
- Task 10: Raise sites — sales (finalizeQuote, convertQuoteToOrder)
- Task 11: Raise sites — four post routes + post-transaction job
- Task 12: Lingui wiring (config include + turbo inputs + extract)
- Task 13: Deploy-time drift check in
packages/checks - Task 14: Docs — AGENTS.md, new rule file, spec changelog
- Task 15: Full verification sweep
Dependencies
Task 2, 3 need Task 1. Task 4 needs 2+3. Task 5 needs 4. Task 6 needs 5. Task 7 needs 5+6. Task 8 needs 3. Tasks 9–11 need 8 (they are independent of each other and of 5–7). Task 12 needs 5. Task 13 needs 6. Tasks 14–15 last.
One deviation from the spec to flag at review: the spec says CatalogEvent
gains label, permission, match. Labels live in a separate generated file
(labels.generated.ts) precisely so the runtime catalog never imports Lingui —
so CatalogEvent gains only permission and match, and labels are a parallel
Record<eventId, MessageDescriptor> the builder joins by id. A label field on
CatalogEvent would always be undefined at runtime; leaving it off is honest.
Global rules for every task:
pnpmonly, nevernpm. Never hand-edit*.generated.tsorpackages/database/src/types.ts/swagger-docs-schema.ts.- After any turbo run, check
git statusforpackages/database/src/types.ts,packages/database/src/swagger-docs-schema.ts,packages/database/supabase/functions/lib/types.tsand revert ride-along churn withgit checkout -- <file>. (These are already dirty on this branch from phase 1 — do not revert phase-1 changes, only NEW churn from your runs.) - If TS2589 ("excessively deep") appears in an unrelated erp file, suppress
with
// @ts-ignore(NOT@ts-expect-error) and keep the generated types shallow (explicitRecord<...>annotations, never inferred deep literals). - Comments: one-liners only, max 2 lines, only where non-obvious.
Task 1: Grow CatalogEvent with permission + match
Depends on: none Files:
- Modify:
packages/workflows/src/definition/catalog.ts— addEventMatchtype, two optional fields onCatalogEvent - Modify:
packages/workflows/src/index.ts— export the new type
Steps:
- In
packages/workflows/src/definition/catalog.ts, aboveCatalogEvent, add:/** How the phase-3 matcher recognises this event. Only the matcher reads it. */ export type EventMatch = | { table: string; operation: "INSERT" | "UPDATE" | "DELETE"; field?: string } | { moment: string }; - Change
CatalogEventto:
Both new fields optional soexport interface CatalogEvent { id: string; outputs: Record<string, ValueType>; /** Lowercase permission module the subscribing workflow's owner must hold. */ permission?: string; match?: EventMatch; }createFixtureCatalogand all 73 existing tests compile untouched. Do NOT edit the fixture data. - In
packages/workflows/src/index.ts, addEventMatchto the type exports from./definition/catalog.
Verify:
pnpm --filter @carbon/workflows test
# Expected: all existing tests pass (73 it() across 3 files)
pnpm --filter @carbon/workflows exec tsgo --noEmit
# Expected: exit 0
Out of scope: label on CatalogEvent (see deviation note), CatalogAction, CatalogOperation.
Task 2: Hand-written entity registry
Depends on: Task 1 Files:
- Create:
packages/workflows/src/catalog/entities.ts - Modify:
packages/workflows/package.json— add"@carbon/database": "workspace:*"todevDependencies(type-only usage; NOTdependencies)
Steps:
- Add the devDependency, then run
pnpm installfrom the repo root. - Create
packages/workflows/src/catalog/entities.ts. Shape:import type { ColumnOf, TableName } from "@carbon/database/audit.config"; /** One column a customer can watch. `ref` names a registry entity the FK points at. */ interface WatchedColumn { label: string; ref?: string; } interface EntityEntry<T extends TableName> { table: T; label: string; /** Lowercase permission module (existing family — never a new one). */ permission: string; /** Present => triggerable (generates created/deleted/changed events). Absent => reference-only. */ watch?: { [C in ColumnOf<T>]?: WatchedColumn }; } /** Identity helper so `watch` keys are checked against the entry's own table. */ const entity = <T extends TableName>(entry: EntityEntry<T>) => entry; export const WORKFLOW_ENTITY_REGISTRY = { purchaseOrder: entity({ table: "purchaseOrder", label: "Purchase order", permission: "purchasing", watch: { ... } }), ... } as const; export type RegistryEntityName = keyof typeof WORKFLOW_ENTITY_REGISTRY; - Fill in exactly these 10 triggerable entries (labels in parens; every
watchkey gets{ label: "<human label>" }, plusrefwhere noted):purchaseOrder(Purchase order,purchasing):status,supplierId(refsupplier),assignee(refuser),orderDate,purchaseOrderType,supplierReference,supplierLocationId,tagssalesOrder(Sales order,sales):status,customerId(refcustomer),assignee(refuser),salesPersonId(refuser),orderDate,locationId(reflocation),customerReference,completedDatejob(Job,production):status,assignee(refuser),dueDate,startDate,quantity,priority,deadlineType,scrapQuantityitem(Item,parts):active,revisionStatus,replenishmentSystem,itemTrackingType,defaultMethodType,assignee(refuser),name,unitOfMeasureCodereceipt(Receipt,inventory):status,supplierId(refsupplier),locationId(reflocation),assignee(refuser),postingDate,invoiced,sourceDocumentshipment(Shipment,inventory):status,customerId(refcustomer),locationId(reflocation),assignee(refuser),postingDate,trackingNumber,shippingMethodIdquote(Quote,sales):status,customerId(refcustomer),assignee(refuser),estimatorId(refuser),salesPersonId(refuser),expirationDate,dueDate,completedDatesupplier(Supplier,purchasing):supplierStatus,supplierTypeId,accountManagerId(refuser),assignee(refuser),name,currencyCode,taxPercentcustomer(Customer,sales):customerStatusId,customerTypeId,accountManagerId(refuser),assignee(refuser),name,currencyCode,salesContactId(NO ref — its FK iscustomerContact, notuser; corrected during execution)nonConformance(Issue,quality):status,priority,assignee(refuser),source,nonConformanceTypeId,dueDate,closeDate,locationId(reflocation),quantity(77 watched columns total — all verified to exist in the swagger schema.)
- Add the 5 reference-only entries (no
watch):user(User,users),jobOperation(Job operation,production),salesInvoice(Sales invoice,invoicing),purchaseInvoice(Purchase invoice,invoicing),location(Location,resources)
- Do NOT add a
writefield anywhere — that is phase 5.
Verify:
pnpm --filter @carbon/workflows exec tsgo --noEmit
# Expected: exit 0. Sanity: temporarily rename one watch key to "statusX" —
# must FAIL naming the key — then restore.
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0 (proves the type-only @carbon/database import doesn't break erp)
Out of scope: any runtime import of @carbon/database (type-only!), line-level entities, write allowlists.
Task 3: Hand-written moment declarations
Depends on: Task 1 Files:
- Create:
packages/workflows/src/catalog/moments.ts
Steps:
- Create the file with this shape (types must live here so
packages/libcan import them; keep zod out — plain types only):import type { ValueType } from "../definition/types"; const entity = (of: string): ValueType => ({ kind: "entity", of }); interface MomentDeclaration { /** Mandatory human label — the generator refuses a moment without one. */ label: string; permission: string; outputs: Record<string, ValueType>; } export const WORKFLOW_MOMENTS = { "production.jobReleased": { label: "A job is released", permission: "production", outputs: { job: entity("job"), releasedBy: entity("user") } }, ... } as const satisfies Record<string, MomentDeclaration>; export type MomentKey = keyof typeof WORKFLOW_MOMENTS; /** Entity outputs are passed as a type + id, never a row snapshot. */ export type MomentEntityRef = { id: string }; export type MomentPayload<K extends MomentKey> = { [O in keyof (typeof WORKFLOW_MOMENTS)[K]["outputs"]]: MomentEntityRef; }; - The 9 declarations (key → label → outputs; permission is the dot-prefix module
except
invoicing.*→invoicing):production.jobReleased— "A job is released" —job: entity("job"),releasedBy: entity("user")production.jobHeld— "A job is put on hold" —job: entity("job"),heldBy: entity("user")production.jobOperationCompleted— "A job operation is completed" —job: entity("job"),jobOperation: entity("jobOperation"),completedBy: entity("user")sales.quoteSent— "A quote is sent" —quote: entity("quote"),sentBy: entity("user")sales.quoteAccepted— "A quote is accepted" —quote: entity("quote"),salesOrder: entity("salesOrder")inventory.receiptPosted— "A receipt is posted" —receipt: entity("receipt"),postedBy: entity("user")inventory.shipmentPosted— "A shipment is posted" —shipment: entity("shipment"),postedBy: entity("user")invoicing.salesInvoicePosted— "A sales invoice is posted" —salesInvoice: entity("salesInvoice"),postedBy: entity("user")invoicing.purchaseInvoicePosted— "A purchase invoice is posted" —purchaseInvoice: entity("purchaseInvoice"),postedBy: entity("user")
Verify:
pnpm --filter @carbon/workflows exec tsgo --noEmit
# Expected: exit 0. Sanity: delete one `label` line — must fail to compile — restore.
Out of scope: the 7 deferred moments (job completed, PO issued, SO confirmed, quote lost, NC opened, NC closed, inspection failed) — phase 3.
Task 4: Pure buildCatalog transform + tests
Depends on: Tasks 2, 3 Files:
- Create:
packages/workflows/src/catalog/build.ts - Create:
packages/workflows/src/catalog/build.test.ts
Steps:
build.ts— a pure function; the swagger schema is INJECTED so this package never imports@carbon/databaseas a value:
(Use real value imports for the two hand-written inputs' types viaimport type { ValueType } from "../definition/types"; import type { EventMatch } from "../definition/catalog"; /** The slice of swagger-docs-schema.ts the builder reads. */ export interface SwaggerSchema { definitions: Record<string, { required?: string[]; properties: Record<string, { type?: string; format?: string; enum?: string[]; description?: string; items?: { type?: string; format?: string }; }>; }>; } export interface BuiltEvent { outputs: Record<string, ValueType>; permission: string; match: EventMatch; } export interface BuiltCatalog { events: Record<string, BuiltEvent>; /** English label text per event id — the generator wraps these in msg``. */ labels: Record<string, string>; entities: Record<string, Record<string, ValueType>>; } export function buildCatalog( registry: typeof import("./entities").WORKFLOW_ENTITY_REGISTRY, moments: typeof import("./moments").WORKFLOW_MOMENTS, schema: SwaggerSchema ): BuiltCatalogimport { WORKFLOW_ENTITY_REGISTRY } from "./entities"style typeof — either form is fine as long as no@carbon/databasevalue import appears.)- Record-event generation, per triggerable entity
Ewith registry keyk:${k}.created— outputs{ record: entity(k) }, match{ table: E.table, operation: "INSERT" }, labelA ${lower(E.label)} is created${k}.deleted— outputs{ record: entity(k) }, match{ table: E.table, operation: "DELETE" }, labelA ${lower(E.label)} is deleted- per watched column
c:${k}.${c}.changed— outputs{ record: entity(k), before: entity(k), after: entity(k) }, match{ table: E.table, operation: "UPDATE", field: c }, labelA ${lower(E.label)}'s ${watch[c].label} changeswherelower()lowercases only the first character. NO generic.updatedevent.
- Moment events: id = key, outputs passed through unchanged, permission from the
declaration, match
{ moment: key }, label from the declaration. Throw anErrornaming the moment if a moment output's entity name is not a registry key. - Entity properties, for EVERY registry entry (triggerable + reference-only),
from
schema.definitions[table].properties, with these rules:- Drop:
companyId,customFields,embedding,updatedAt,updatedBy. - FK columns (description contains
<fk table='X'): ifX, mapped through the registry's entries by table name, is a registry entity →entity(<registryKey>); else plainstring. An explicitwatch[c].refalso produces an entity ref — needed because composite FKs like(supplierId, companyId)carry no<fk>note — and buildCatalog THROWS if arefdisagrees with a present<fk>target, so a wrongrefcan never silently mislabel a property. - Labels use "An" before a vowel (
An item is created,An issue's status changes). format"date"or"timestamp with time zone"→{ kind: "primitive", of: "date" }.- swagger
type"integer"/"number"→ number;"boolean"→ boolean;"array"→{ kind: "list", of: { kind: "primitive", of: <mapped item type> } };"string"and anything else (incl.format: "jsonb") → string. - Throw an
Errornaming table+column if a registrywatchkey is missing from the schema definition (the script-side copy of check 3), and naming the table if a registry table has nodefinitionsentry.
- Drop:
build.test.ts— plain vitest, feed a small fakeSwaggerSchemaPLUS one test that imports the real hand-written inputs with a fake schema. Cover at minimum: event counts per entity (8 watched → 10 events), no.updatedid, the three match shapes, before/after outputs on.changed, moment pass-through, dropped columns absent, FK-to-registry → entity ref, FK-to-non-registry → string, date/array/jsonb mappings, throw on unknown moment-output entity, throw on watch column missing from schema.
Verify:
pnpm --filter @carbon/workflows test
# Expected: all pass, including new build.test.ts
Out of scope: file emission (Task 5), labels as msg (Task 5 emits those).
Task 5: Generator script + the two committed files
Depends on: Task 4 Files:
- Create:
scripts/generate-workflow-catalog.ts - Create (generated):
packages/workflows/src/catalog/events.generated.ts - Create (generated):
packages/workflows/src/catalog/labels.generated.ts - Modify:
package.json(root) — add script - Modify:
packages/workflows/package.json— add"@lingui/core": "catalog:"todependencies - Copy from (precedent):
scripts/generate-agent-kb.ts(header comment style, ROOT/cwd pattern)
Steps:
- Root
package.jsonscript:"generate:workflow-catalog": "tsx scripts/generate-workflow-catalog.ts && pnpm exec biome check --write packages/workflows/src/catalog/" - The script imports
buildCatalog+ both hand-written inputs frompackages/workflows/src/catalog/, imports the schema viaimport schema from "../packages/database/src/swagger-docs-schema"(relative path fromscripts/; it runs under tsx from the repo root), callsbuildCatalog, and writes two files, each headed by:// GENERATED FILE — do not edit. Run `pnpm run generate:workflow-catalog`. events.generated.tsmust import ONLY from../definition/catalogand../definition/types(types), and export with SHALLOW explicit annotations (TS2589 guard — never let TS infer a deep literal across 106 entries):
Emit keys sorted alphabetically (stable diffs).import type { EventMatch } from "../definition/catalog"; import type { ValueType } from "../definition/types"; export interface GeneratedEvent { outputs: Record<string, ValueType>; permission: string; match: EventMatch; } export const WORKFLOW_EVENTS: Record<string, GeneratedEvent> = { ... }; export const WORKFLOW_ENTITIES: Record<string, Record<string, ValueType>> = { ... };labels.generated.ts:
One literal msg`...` per event id (literal template so Lingui extract sees it), keys sorted.import type { MessageDescriptor } from "@lingui/core"; import { msg } from "@lingui/core/macro"; export const WORKFLOW_EVENT_LABELS: Record<string, MessageDescriptor> = { "customer.accountManagerId.changed": msg`A customer's account manager changes`, ... };- Run
pnpm run generate:workflow-catalog. Run it a second time and confirmgit statusshows no further change (idempotent).
Verify:
pnpm run generate:workflow-catalog && git status --porcelain packages/workflows/src/catalog/
# Expected (2nd run): no output for the two generated files
grep -c "changed\"\|created\"\|deleted\"\|jobReleased\|jobHeld\|OperationCompleted\|quoteSent\|quoteAccepted\|Posted" packages/workflows/src/catalog/events.generated.ts
node -e "const s=require('fs').readFileSync('packages/workflows/src/catalog/events.generated.ts','utf8'); console.log('events:', (s.match(/^ \"/gm)||[]).length)"
# Expected: 106 event entries; spot-check purchaseOrder.updated is ABSENT:
grep -n "purchaseOrder.updated" packages/workflows/src/catalog/events.generated.ts
# Expected: no matches (exit 1)
grep -n "@lingui" packages/workflows/src/catalog/events.generated.ts
# Expected: no matches (exit 1)
pnpm --filter @carbon/workflows exec tsgo --noEmit
# Expected: exit 0
Out of scope: touching scripts/generate-swagger-docs.ts or regenerating the swagger schema.
Task 6: createEventCatalog() + barrel
Depends on: Task 5 Files:
- Create:
packages/workflows/src/catalog/catalog.ts - Create:
packages/workflows/src/catalog/catalog.test.ts - Create:
packages/workflows/src/catalog/index.ts - Modify:
packages/workflows/src/index.ts— re-export the catalog barrel
Steps:
catalog.ts:import type { CatalogAction, CatalogOperation, WorkflowCatalog } from "../definition/catalog"; import { WORKFLOW_ENTITIES, WORKFLOW_EVENTS } from "./events.generated"; /** Phase 5 plugs real action/operation catalogs into these seams. */ export function createEventCatalog(options?: { getAction?: (id: string) => CatalogAction | undefined; getOperation?: (id: string) => CatalogOperation | undefined; }): WorkflowCataloggetEvent(id)returns{ id, ...WORKFLOW_EVENTS[id] }or undefined;getEntity(name)returns{ name, properties: WORKFLOW_ENTITIES[name] }or undefined;getAction/getOperationdelegate to the options or return undefined. BuildMaps once at module scope (precedent:audit.config.tsderived-index pattern).catalog/index.tsbarrel: exportcreateEventCatalog,WORKFLOW_EVENTS,WORKFLOW_ENTITIES,GeneratedEvent,WORKFLOW_ENTITY_REGISTRY,RegistryEntityName,WORKFLOW_MOMENTS,MomentKey,MomentPayload,MomentEntityRef,buildCatalog+ its types. Do NOT exportlabels.generatedfrom this barrel or fromsrc/index.ts— it must only be importable via the explicit deep path by Vite-built apps. Re-export the catalog barrel fromsrc/index.ts.catalog.test.ts(plain vitest — this doubles as proof the runtime catalog is importable without the Lingui macro plugin):createEventCatalog()satisfiesWorkflowCatalog;getAction/getOperationreturn undefined by default and delegate when supplied.getEvent("purchaseOrder.status.changed")has match{ table: "purchaseOrder", operation: "UPDATE", field: "status" }and outputsrecord/before/afterall{ kind: "entity", of: "purchaseOrder" }.getEvent("production.jobReleased")has match{ moment: "production.jobReleased" }.getEntity("purchaseOrder").properties.supplierIdequals{ kind: "entity", of: "supplier" }; properties lackcompanyId,customFields,updatedAt,updatedBy;orderTotalabsent.validateDefinition(imported from../definition/validate) with a real trigger onpurchaseOrder.status.changedand a condition referencingrecord.supplier.name→ no issues;record.supplier.notAColumn→UNKNOWN_VARIABLE.- Trigger listing two real events with different outputs exposes only the
intersection (reuse the pattern from
validate.test.ts).
- Confirm nothing under
src/definition/importssrc/catalog/:grep -rn "catalog/" packages/workflows/src/definition/must be empty.
Verify:
pnpm --filter @carbon/workflows test
# Expected: all pass — including the new catalog.test.ts importing events.generated under plain vitest
grep -rn "from \"../catalog" packages/workflows/src/definition/
# Expected: no matches
Out of scope: real getAction/getOperation implementations (phase 5).
Task 7: Build checks script + CI job
Depends on: Tasks 5, 6 Files:
- Create:
scripts/check-workflow-catalog.ts - Modify:
package.json(root) — add"check:workflow-catalog": "tsx scripts/check-workflow-catalog.ts" - Modify:
.github/workflows/check.yml— newcatalogjob - Copy from (precedent): the
linguijob incheck.yml(install/cache preamble, lines 93–118)
Steps:
- The script exits non-zero with a plain-English message on any failure. Checks:
- Check 1 — every moment is raised somewhere. Recursively scan
apps/andpackages/(skipnode_modules,dist,.turbo,*.test.*, andpackages/workflows/src/catalog/itself) for the literal substringraiseMoment("<key>"per declared key. Fail naming any unraised moment. (Raise-site typos are check 2 and already a compile error — the script doesn't duplicate it.) - Check 3 — every registry table + watched column exists. Import the
registry and the swagger schema; verify
definitions[table]and everywatchkey. Fail naming entity + column. - Check 4 — committed catalog is fresh. Rebuild via
buildCatalogin memory and deep-compare (node:assertdeepStrictEqual) against the importedWORKFLOW_EVENTS/WORKFLOW_ENTITIES, and compare the label-id sets againstWORKFLOW_EVENT_LABELS's keys — compare DATA, not file text, so formatting can't flap. On mismatch: "run pnpm run generate:workflow-catalog". NOTE: this script MAY importlabels.generated.tsonly if tsx tolerates the untransformedmsgmacro — it does NOT (macro throws on plain import). So readlabels.generated.tsas TEXT (fs.readFileSync) and extract its quoted keys with a regex instead of importing it. - Check 5 — moment declarations well-formed. Every moment has a non-empty
label; every moment output's entity name is a registry key. (buildCatalog already throws for the latter — catching that throw satisfies this.)
- Check 1 — every moment is raised somewhere. Recursively scan
check.yml: append acatalogjob — copy thelinguijob verbatim, rename tocatalog/Catalog, and replace the last step with- run: pnpm run check:workflow-catalog.- Prove the checks bite, then restore:
- Comment out one
raiseMoment(call site (after Task 9 lands; if running this task before Task 9, verify check 1 fails for ALL moments and note it) — expect non-zero naming the moment. - Add a fake watched column
zzzto the registry — expect check 3 failure naming it (andtsgofailure). - Hand-edit one output in
events.generated.ts— expect check 4 failure.
- Comment out one
Verify:
pnpm run check:workflow-catalog
# Expected (clean tree, after Task 9): exit 0
Out of scope: the deploy-time drift check (Task 13); running the checks against a live DB.
Task 8: raiseMoment + Inngest event registration
Depends on: Task 3 Files:
- Create:
packages/lib/src/workflows/raise-moment.ts - Modify:
packages/lib/src/events.ts— one new event - Modify:
packages/lib/package.json— add export"./workflows": "./src/workflows/raise-moment.ts"and"@carbon/workflows": "workspace:*"todependencies
Steps:
- In
packages/lib/src/events.ts, add toEvents:// Workflow moments — raised after a business action commits. Phase 3 consumes. "carbon/workflow-moment.raised": { data: { moment: string; companyId: string; /** auth.uid() of the actor; null for service-role / background writes. */ actorId: string | null; /** Output name -> entity id, per the moment's declaration. */ outputs: Record<string, { id: string }>; }; }; raise-moment.ts:
Ifimport type { MomentKey, MomentPayload } from "@carbon/workflows"; import { getLogger } from "@carbon/logger"; import { inngest } from "../inngest/client"; const log = getLogger("lib", "workflows"); /** * Announce a business moment after its write committed. Never throws into the * caller — losing a moment is a missed workflow; failing the caller is worse. */ export async function raiseMoment<K extends MomentKey>( key: K, payload: MomentPayload<K> & { companyId: string; actorId: string | null } ): Promise<void> { try { const { companyId, actorId, ...outputs } = payload; await inngest.send({ name: "carbon/workflow-moment.raised", data: { moment: key, companyId, actorId, outputs: outputs as Record<string, { id: string }> } }); } catch (err) { log.error("Failed to raise workflow moment", { key, err }); } }@carbon/loggerisn't already a dependency ofpackages/lib, use the import stylepackages/lib/src/inngest/client.tsuses (@carbon/logger/inngestproves the package is available) — plain@carbon/loggerwithgetLogger.pnpm installafter the package.json edits.- Check
packages/libhas no circular dep:@carbon/workflowsdepends only on@carbon/utils+zod, so lib → workflows is acyclic. If tsgo reports a cycle, STOP and report — do not improvise.
Verify:
pnpm --filter @carbon/lib exec tsgo --noEmit
# Expected: exit 0
pnpm --filter @carbon/workflows exec tsgo --noEmit
# Expected: exit 0
Out of scope: any consumer of carbon/workflow-moment.raised (phase 3); Inngest function registration.
Task 9: Raise sites — production (ERP) + MES
Depends on: Task 8 Files:
- Modify:
apps/erp/app/modules/production/production.service.ts—updateJobStatus(lines ~2345–2373) - Modify:
apps/erp/app/routes/x+/job+/$jobId.status.tsx— passcompanyId - Modify:
apps/mes/app/services/operations.service.ts—finishJobOperation(lines 151–199)
Steps:
updateJobStatuscurrently returns the un-awaited update builder and takes nocompanyId. Restructure:- Add
companyId: stringtoparams. - Before the update, read the prior status:
const prior = await client.from("job").select("status").eq("id", id).maybeSingle(); - Await the existing update into
const result = await client.from("job").update({...}).eq("id", id); - After it, raise only on a genuine transition:
if (!result.error && prior.data && prior.data.status !== status) { if (status === "Ready") { await raiseMoment("production.jobReleased", { job: { id }, releasedBy: { id: updatedBy }, companyId, actorId: updatedBy }); } else if (status === "Paused") { await raiseMoment("production.jobHeld", { job: { id }, heldBy: { id: updatedBy }, companyId, actorId: updatedBy }); } } return result; - Import:
import { raiseMoment } from "@carbon/lib/workflows";— safe in a.service.tsby the in-tree precedent (documents.service.tsimportstriggerfrom@carbon/jobs, which constructs the same Inngest client;sideEffects: falsetree-shakes it out of the browser bundle). If the erp client build breaks on a node built-in after this import, STOP and report — the fallback is moving the raise to the route, but that loses MCP callers and needs a user decision.
- Add
- Update the single caller
apps/erp/app/routes/x+/job+/$jobId.status.tsx(line ~125): addcompanyIdto theupdateJobStatuscall —companyIdis already in scope fromrequirePermissions(line 20). - Run
pnpm run generate:mcp; commit any regeneratedapps/erp/app/routes/api+/mcp+/lib/tool-metadata.jsonchurn caused by the signature change (MCP passes args by declared param names). finishJobOperation(MES): inside the existingif (!result.error) { ... }block, afterawait returnAllocatedRemaindersAtJobComplete(client, args);(line ~195), add:
Sameconst op = await client .from("jobOperation") .select("jobId") .eq("id", args.jobOperationId) .maybeSingle(); if (op.data?.jobId) { await raiseMoment("production.jobOperationCompleted", { job: { id: op.data.jobId }, jobOperation: { id: args.jobOperationId }, completedBy: { id: args.userId }, companyId: args.companyId, actorId: args.userId }); }@carbon/lib/workflowsimport.
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=mes
# Expected: exit 0 (package name is `erp`, NOT @carbon/erp — wrong name silently no-ops)
Out of scope: raising on In Progress/Completed/other statuses; the job-completed moment (deferred, phase 3).
Task 10: Raise sites — sales
Depends on: Task 8 Files:
- Modify:
apps/erp/app/modules/sales/sales.service.ts—finalizeQuote(line 1952),line 183)convertQuoteToOrder( - Modify:
apps/erp/app/routes/x+/quote+/$quoteId.finalize.tsx— passcompanyId
Steps:
finalizeQuote(client, quoteId, userId)currently returns the second update un-awaited and has nocompanyId. Restructure:- Signature →
finalizeQuote(client, quoteId, userId, companyId: string). - Await the trailing
quoteLineupdate intoconst lineUpdate = await .... - After it:
if (!lineUpdate.error) { await raiseMoment("sales.quoteSent", { quote: { id: quoteId }, sentBy: { id: userId }, companyId, actorId: userId }); } return lineUpdate;
- Signature →
- Update its single caller
apps/erp/app/routes/x+/quote+/$quoteId.finalize.tsx(line ~122) to passcompanyId(in scope at line 30). convertQuoteToOrdercurrently returns the un-awaitedclient.functions.invoke. Restructure:
No caller changes needed (both callers alreadyconst result = await client.functions.invoke<{ convertedId: string }>("convert", { body: { type: "quoteToSalesOrder", ...payload } }); if (!result.error && result.data?.convertedId) { await raiseMoment("sales.quoteAccepted", { quote: { id: payload.id }, salesOrder: { id: result.data.convertedId }, companyId: payload.companyId, actorId: payload.userId }); } return result;awaitand check.error).- Run
pnpm run generate:mcpagain (finalizeQuote signature changed); commit metadata churn.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
pnpm run check:workflow-catalog
# Expected: check 1 no longer lists sales.quoteSent / sales.quoteAccepted as unraised
Out of scope: the digital-quote route (api+/sales.digital-quote.$id.tsx) — it calls convertQuoteToOrder, so the service-level raise already covers it; quote-lost (deferred).
Task 11: Raise sites — posting moments
Depends on: Task 8 Files:
- Modify:
apps/erp/app/routes/x+/receipt+/$receiptId.post.tsx - Modify:
apps/erp/app/routes/x+/shipment+/$shipmentId.post.tsx - Modify:
apps/erp/app/routes/x+/sales-invoice+/$invoiceId.post.tsx - Modify:
apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.post.tsx - Modify:
packages/jobs/src/inngest/functions/tasks/post-transaction.ts
These writes happen inside Deno edge functions that cannot import app code, so
each real CALLER raises after the invoke returns cleanly. raiseMoment never
throws, so calling it inside the routes' try blocks cannot trigger their
status-rollback catch paths — but place calls AFTER the try/catch where noted
anyway, to keep the rollback paths obviously untouched.
Steps:
- Receipt route: immediately after the
postReceipt.errorguard closes (line ~232, still inside thetry), add:await raiseMoment("inventory.receiptPosted", { receipt: { id: receiptId }, postedBy: { id: userId }, companyId, actorId: userId }); - Shipment route: same pattern after the
postShipment.errorguard (line ~278):await raiseMoment("inventory.shipmentPosted", { shipment: { id: shipmentId }, postedBy: { id: userId }, companyId, actorId: userId }); - Sales-invoice route: at line ~100, AFTER the try/catch closes and BEFORE
const salesInvoice = await getSalesInvoice(serviceRole, invoiceId);:await raiseMoment("invoicing.salesInvoicePosted", { salesInvoice: { id: invoiceId }, postedBy: { id: userId }, companyId, actorId: userId }); - Purchase-invoice route: at line ~110, AFTER the try/catch closes (the
catchrolls status back to Draft — do not insert inside it):await raiseMoment("invoicing.purchaseInvoicePosted", { purchaseInvoice: { id: invoiceId }, postedBy: { id: userId }, companyId, actorId: userId }); post-transaction.ts(Inngest,retries: 3): add a SEPARATE step after the existingstep.run("post-transaction", ...)resolves (after line ~133), so a retry of the raise can never re-run the posting step:
Adjust to the file's actual payload field names if they differ — ifif (result.success) { await step.run("raise-moment", async () => { const base = { companyId: payload.companyId, actorId: payload.userId ?? null }; if (payload.type === "receipt") { await raiseMoment("inventory.receiptPosted", { receipt: { id: payload.documentId }, postedBy: { id: payload.userId }, ...base }); } else if (payload.type === "shipment") { await raiseMoment("inventory.shipmentPosted", { shipment: { id: payload.documentId }, postedBy: { id: payload.userId }, ...base }); } else if (payload.type === "purchase-invoice") { await raiseMoment("invoicing.purchaseInvoicePosted", { purchaseInvoice: { id: payload.documentId }, postedBy: { id: payload.userId }, ...base }); } }); }documentId/userIdare named differently, STOP and re-read the file rather than guessing. Import from@carbon/lib/workflows(jobs already depends on@carbon/lib).
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/jobs
# Expected: exit 0
pnpm run check:workflow-catalog
# Expected: exit 0 — all 9 moments now have at least one raise site
Out of scope: the edge functions themselves; dedupe logic (phase 1's run dedupe key handles double delivery in phase 3+).
Task 12: Lingui wiring
Depends on: Task 5 Files:
- Modify:
lingui.config.js— add"packages/workflows/src"to theerpcatalog'sincludearray (precedent:"packages/glossary/src"already there) - Modify:
turbo.json— add"packages/workflows/src/**"to//#lingui:compile'sinputs
Steps:
- Make both edits.
- Run
pnpm run lingui:extract. KNOWN QUIRK on this branch: extract dumps large stale.pochurn. Inspectgit diff --stat packages/locale/locales/en/erp.poand confirm the new catalog msgids are present:grep -c "A purchase order is created" packages/locale/locales/en/erp.po→ ≥1. If the churn is the known ~120k-line stale noise, revert everything excepten/erp.poadditions ONLY if the user has previously endorsed that split — otherwise leave the full extract result uncommitted and flag it in the final report for the user to decide.
Verify:
grep -c "msgid \"A purchase order's status changes\"" packages/locale/locales/en/erp.po
# Expected: 1
pnpm run lingui:compile
# Expected: exit 0
Out of scope: translating the new strings (the /translate flow is separate); mes catalog (labels are erp-only for now).
Task 13: Deploy-time drift check
Depends on: Task 6 Files:
- Create:
packages/checks/src/scripts/check-workflow-drift.ts - Modify:
packages/checks/package.json— add script"workflow-drift": "tsx src/scripts/check-workflow-drift.ts"and"@carbon/workflows": "workspace:*"todevDependencies - Copy from (precedent):
packages/checks/src/scripts/run-invariants.ts(DB connection + exit-code pattern)
Steps:
- The script (needs a live DB — NOT wired into
check.yml, NOT a floor gate, matching howinvariantsis handled):- Connect with
pgthe wayrun-invariants.tsdoes. - Query every active workflow:
SELECT w."id", w."companyId", w."activeVersionId", v."nodes" FROM "workflow" w JOIN "workflowVersion" v ON v."id" = w."activeVersionId" AND v."companyId" = w."companyId" WHERE w."active" = true - For each: extract event ids from trigger nodes
(
nodesJSONB → entries withtype === "trigger"→data.events), compare as a set againstSELECT "eventId" FROM "workflowTriggerEvent" WHERE "workflowId" = $1 AND "companyId" = $2, and check every id exists viacreateEventCatalog().getEvent(id). - Print each drifted workflow (id, companyId, missing/extra/unknown ids);
exit 1 if any, else print
workflow-drift: ok (N active workflows)exit 0.
- Connect with
- Do NOT run it against a live DB in this phase (no rebuilds, no migrations — there are zero active workflows anyway). Verification is typecheck + tests only.
Verify:
pnpm --filter @carbon/checks exec tsgo --noEmit
# Expected: exit 0
pnpm --filter @carbon/checks test
# Expected: existing tests pass
Out of scope: wiring into CI or FLOOR_GATES (needs DB); fixing any drift it might find later.
Task 14: Docs
Depends on: Tasks 1–13 Files:
- Modify:
packages/workflows/AGENTS.md - Create:
.claude/rules/workflow-event-catalog.md - Modify:
.ai/specs/2026-07-30-workflows-event-catalog.md— changelog entry + the CatalogEventlabeldeviation - Copy from (precedent):
.claude/rules/agent-knowledge-base.md(tone/shape for a generated-artifact rule)
Steps:
packages/workflows/AGENTS.md: add asrc/catalog/section to the Layout block and rules: regenerate withpnpm run generate:workflow-catalog, never hand-edit*.generated.ts; the label split exists becausemsgis a build-time macro (plain Node/vitest cannot importlabels.generated.ts);src/definition/must never importsrc/catalog/;@carbon/databaseis type-only (devDependency).- New rule
.claude/rules/workflow-event-catalog.mdwith frontmatterpaths: ["packages/workflows/src/catalog/**", "scripts/generate-workflow-catalog.ts", "scripts/check-workflow-catalog.ts"]covering: the two hand-written inputs, the generator, the two outputs, the five checks, how to add an entity/moment, and the raise-site rules (service function over route; after the committed write; raiseMoment never throws). - Spec changelog: implementation date + the
CatalogEvent.labeldeviation (labels live only inlabels.generated.ts; the interface carriespermission/matchonly).
Verify:
ls .claude/rules/workflow-event-catalog.md
# Expected: exists; content matches the implemented reality (spot-check paths)
Out of scope: product docs under docs/ (no user-facing surface exists yet); .codex/rules/ (install script copies it).
Task 15: Full verification sweep
Depends on: all Steps:
- Regenerate + idempotency:
pnpm run generate:workflow-catalogtwice → cleangit statuson the catalog dir. - Checks:
pnpm run check:workflow-catalog→ exit 0. Then the three bite-tests from Task 7 step 3 (break, observe failure, restore). - Tests:
pnpm --filter @carbon/workflows testandpnpm --filter @carbon/checks test. - Typecheck (scoped — whole-repo OOMs):
pnpm exec turbo run typecheck --filter=@carbon/workflows --filter=@carbon/lib --filter=@carbon/jobs --filter=@carbon/checks --filter=erp --filter=mesIf turbo cache masks a result, rerun the failing package withpnpm --filter <pkg> exec tsgo --noEmitdirectly. - Lint:
pnpm exec biome check packages/workflows packages/lib packages/checks scripts apps/erp/app/modules/production apps/erp/app/modules/sales apps/mes/app/services— fix error-severity only; leave pre-existing warnings. - Ride-along churn:
git status— revert NEW churn in the three generated@carbon/databasefiles (do not revert phase-1 edits already on the branch). - Report results with command output. Do NOT commit — the user commits on explicit ask only.
Verify: the commands above ARE the verification; expected outputs inline.
Out of scope: browser testing (no UI exists), database rebuilds, Inngest dev-UI verification (requires the local stack; offer it to the user instead of running it unprompted).