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.
PR #1294 (Workflows) — review changelist
Branch: feat/automation @ 5e180ddfe · PR: https://github.com/crbnos/carbon/pull/1294
Scope reviewed: full branch diff vs main (416 files, +93,619 / −2,632) — engine, matcher,
scheduler, retention, actions, migrations, routes, service layer, plus a spot-read of the builder UI.
You are picking this up as a work list. Every item below was verified against the checked-out branch, not inferred from the diff alone. Three findings from the first pass were checked and discarded as false positives — they are listed at the bottom so you don't re-raise them.
Baseline: pnpm --filter @carbon/workflows --filter @carbon/jobs test is green on this branch
(641 tests). None of the defects below are caught by existing coverage. Assume a fix is not
proven until you have a test that fails before it and passes after.
Ground rules for this work
- Read
AGENTS.md,packages/jobs/AGENTS.md,packages/workflows/AGENTS.mdandapps/erp/app/modules/workflows/AGENTS.mdbefore starting. The.claude/rules/workflow-*.mdfiles are the subsystem reference. - Carbon is multi-tenant. Every query needs
companyIdscoping; every workflow action must use the owner-scoped, run-tagged client (getOwnerClient) — a privileged or untagged write escapes the owner's permissions and blinds the matcher's origin filter and loop guards. pnpmonly, nevernpm. Scoped typecheck only — a whole-repo typecheck OOMs.- Don't hand-edit generated DB types (
packages/database/src/types.ts,swagger-docs-schema.ts,supabase/functions/lib/types.ts). Ifpnpm installdirties them with ordering churn,git checkout --them. - Migrations are immutable once applied. For item P1-7, check whether the migration has shipped anywhere before editing it; if it has, write a follow-up migration instead.
Verification commands
pnpm --filter @carbon/workflows test
pnpm --filter @carbon/jobs test
pnpm exec turbo run typecheck --filter=@carbon/jobs
pnpm exec turbo run typecheck --filter=erp # note: the package is "erp", not "@carbon/erp"
pnpm run lint
P0 — blocking
P0-1 · The walk executes a node before its ancestors have run
Files: packages/jobs/src/workflows/engine/walk.ts,
packages/workflows/src/definition/variables.ts (resolveRef)
walk.ts is a plain BFS frontier with an executed set and no in-degree tracking. When two
paths into the same node have different lengths, the shorter one wins and the node runs early;
the later arrival is then silently discarded by alreadyExecuted.
Reproduced by execution. Graph t→b→d and t→c1→c2→d walks as:
["b", "c1", "d", "c2"] // d runs BEFORE c2
The symmetric diamond (t→b→d, t→c→d) is correct — ["b","c","d"] — and is the only case
walk.test.ts covers, which is why this is green.
The validator compounds it: resolveRef only requires the referenced node to be a graph
ancestor (reverse-reachable), so wiring d's input to c2.record passes validation and then
resolves to nothing at run time, with no error surfaced. The failure message for the opposite
case even claims ordering is guaranteed: "This uses a value from a step that does not always
run before it."
Do one of:
- (a) Fix the engine — track in-degree over live incoming edges (edges whose source handle was actually taken) and hold a node in the frontier until every live predecessor has settled. This is the correct fix but needs care around condition branches that are never taken, or the join deadlocks.
- (b) Fix the validator (cheaper, ships sooner) — change
resolveRefto require dominance rather than ancestry: the referenced node must lie on every path from every trigger to the referencing node. That rejects at publish time exactly what the engine can't honour. Note this narrows what customers can wire, so confirm the product call.
Regression test (drop into packages/jobs/src/workflows/engine/walk.test.ts):
it("does not execute a join before a longer branch has arrived", () => {
const def = definition(
[trigger("t"), action("b"), action("c1"), action("c2"), action("d")],
[
edge("t", DEFAULT_HANDLE, "b"),
edge("t", DEFAULT_HANDLE, "c1"),
edge("b", DEFAULT_HANDLE, "d"),
edge("c1", DEFAULT_HANDLE, "c2"),
edge("c2", DEFAULT_HANDLE, "d")
]
);
expect(drain(createWalkState(def), def)).toEqual(["b", "c1", "c2", "d"]);
});
(definition, trigger, action, edge and drain already exist in that file.)
P0-2 · Retention pass 4 never advances; step payloads leak
File: packages/jobs/src/inngest/functions/scheduled/workflow-run-retention.ts:180
drop-step-detail calls selectTerminalRuns(db, cutoffDays(30), BATCH) with no ORDER BY and
no predicate excluding runs whose steps are already deleted. It re-picks the same ~500 rows every
night and deletes zero. Passes 2 and 3 self-advance (pass 2 deletes the rows; pass 3 filters
compactedAt is null); this pass does not. Any company with more than 500 terminal runs in the
30–90 day window keeps full workflowStepRun payloads until the 90-day header purge catches up.
Do: add a progress marker mirroring compactedAt (e.g. detailDroppedAt on workflowRun),
or gate the select on WHERE EXISTS (SELECT 1 FROM "workflowStepRun" s WHERE s."runId" = r."id").
Add an ORDER BY so batching is deterministic.
P0-3 · Unbounded Promise.all exhausts the connection pool
File: workflow-run-retention.ts:136
One UPDATE per step row across up to 200 runs, all fanned out at once, against
getJobDatabaseClient(5) — a pool of 5 with connectionTimeoutMillis: 10_000
(packages/database/supabase/functions/lib/postgres/index.ts:75). A few thousand queued promises
start throwing connection timeouts and fail the whole Inngest step.
Do: one set-based statement (UPDATE … FROM (VALUES …)), or move the truncation into SQL
(see P1-6), which removes this loop entirely.
P0-4 · Autosave silently loses edits
Files: apps/erp/app/modules/workflows/ui/Builder/Autosave.tsx:60,
apps/erp/app/modules/workflows/ui/Builder/store.ts:253
rebaseline() snapshots the current store state, not the payload that was submitted:
rebaseline: () => {
const { nodes, edges } = get(); // ← current, not what was saved
set({ baseline: snapshot(nodes, edges) });
}
Sequence: edit A → debounce submits payload A → user edits B while in flight → A's response
arrives → rebaseline() baselines A+B → next debounce compares against A+B, finds no diff, and
skips the save. Edit B is gone on reload.
Do: thread the submitted definition through — rebaseline(submittedDefinition) — and baseline
that. Also consider that savedRef is a single boolean and can't distinguish overlapping
in-flight saves.
P0-5 · Deleting a workflow orphans its eventSystemSubscription rows
File: apps/erp/app/routes/x+/workflows+/delete.$id.tsx
syncWorkflowTriggers is the only reconciler of eventSystemSubscription, and it has exactly two
callers — publishWorkflowVersion and setWorkflowActive
(apps/erp/app/modules/workflows/workflows.server.ts:136,168). The delete path calls neither.
The FK cascade removes workflowTriggerEvent, but the handlerType = 'WORKFLOW' subscription row
survives active. dispatch_event_batch() then keeps row_to_json-ing every row of that table
for that company and enqueuing to pgmq, forever, for a workflow that no longer exists.
Do: call syncAndWake(companyId, workflowId) (or syncWorkflowSubscriptions) after a
successful delete.
P0-6 · Company-wide subscription reconcile has no lock
File: packages/workflows/src/sync.ts:203
reconcileWorkflowSubscriptions(trx, companyId) reads all of the company's
workflowTriggerEvent rows and delete/inserts eventSystemSubscription — but it runs inside a
per-workflow transaction. Two concurrent publishes or toggles in the same company each compute
desired against pre-commit state; one can delete a subscription the other still needs, and event
delivery silently stops for that table. The delete/insert loop also iterates existing rows in
non-deterministic order, which is a two-session deadlock shape.
Do: take a company-scoped advisory lock as the first statement inside the
syncWorkflowTriggers transaction:
await sql`SELECT pg_advisory_xact_lock(hashtext(${companyId}))`.execute(trx);
P0-7 · Global Tooltip change with app-wide blast radius
File: packages/react/src/Tooltip.tsx:112-120
<TooltipPrimitive.Portal container={typeof document !== "undefined" ? document.body : undefined}>
<TooltipPrimitive.Positioner className="z-[100]" style={{ zIndex: 9999 }}>
The inline style always beats the class, so z-[100] is dead code, and every tooltip in ERP and
MES now paints above modals, drawers, sheets and the command palette. The portal container change
is also app-wide. This is a workflow-builder stacking fix pushed into a shared primitive.
Do: revert the shared component. Fix the stacking inside the builder — raise the canvas popover's own stacking context, or pass an opt-in prop for the one call site that needs it.
P1 — scale (before this sees real volume)
P1-1 · Add two missing indexes
-- Global runs list: apps/erp/app/routes/x+/workflows+/runs.tsx filters companyId only
-- and sorts createdAt DESC. No existing index leads with (companyId, createdAt) —
-- workflowRun_companyId_workflowId_idx puts workflowId in the middle, so this is a
-- scan + sort of the company's entire 90-day run history on every page.
CREATE INDEX "workflowRun_companyId_createdAt_idx"
ON "workflowRun" ("companyId", "createdAt" DESC);
-- hasActiveRun (packages/jobs/src/workflows/scheduler.ts) runs once per due workflow
-- per scheduler wake and currently scans every historical run for that workflow.
CREATE INDEX "workflowRun_active_idx"
ON "workflowRun" ("companyId", "workflowId")
WHERE "status" IN ('Queued', 'Running');
P1-2 · Drop two dead indexes
Verified no consumer exists for either; both are pure write cost on the two highest-insert tables.
workflowRun_purge_idx ("status","completedAt")— the retention job filters onCOALESCE("completedAt","createdAt"), which is whatworkflowRun_retention_idxis for.workflowStepRun_companyId_idx ("companyId")— every query on that table also filtersrunId(covered byworkflowStepRun_runId_idx) orid(covered by the PK).
P1-3 · dispatchDue is a ~600-round-trip loop
packages/jobs/src/workflows/scheduler.ts:139 — up to 200 due workflows, each doing a claimDue
UPDATE + a hasActiveRun SELECT + an insert, inside one Inngest step on a pool of 1.
Do: collapse into a single CTE / UPDATE … RETURNING. Consider FOR UPDATE SKIP LOCKED on
the claim so more than one wake can drain in parallel.
P1-4 · reap-stale-runs is a ~1,500-round-trip loop
workflow-run-retention.ts:83 — per stale run: failInterruptedSteps + failCrashedRun (itself
a SELECT then an UPDATE). Do: two set-based UPDATEs.
P1-5 · Exact count on the run log
apps/erp/app/modules/workflows/workflows.service.ts:225 uses { count: "exact" }, and
runs.tsx consumes count for pagination — so every page load runs a full RLS-filtered
COUNT(*) over a table that grows with event volume. (Line 19, on workflow, is fine — tens of
rows.) Do: estimated count or keyset pagination.
P1-6 · Move JSONB compaction into SQL
The compaction pass reads every step's input/output/detail into Node, truncates in JS via
compactForLog (packages/jobs/src/workflows/retention.ts), and writes each row back. Port the
truncation to a compact_jsonb() plpgsql function driven by one
UPDATE "workflowStepRun" SET … WHERE "runId" = ANY($1). Also resolves P0-3.
P1-7 · Migration rewrites every userPermission row
packages/database/supabase/migrations/20260730142317_workflows-foundation.sql:24 — the
UPDATE "userPermission" SET "permissions" = "permissions" || … has no WHERE clause. Every
row in every tenant is rewritten, doubling the table until vacuum and holding locks across the
whole table.
Do: add WHERE NOT ("permissions" ? 'workflows_view'). Check first whether this migration
has already been applied anywhere — if it has, leave it alone and ship a corrected follow-up.
P1-8 · getWorkflowRunSteps is unbounded
workflows.service.ts:253, called from runs.$runId.tsx:43 with no pagination. A batched node
with 500 items returns 500+ rows each carrying input, output and detail JSONB, all shipped
to the browser.
P1-9 · Lookup does select("*") and persists whole rows
packages/jobs/src/workflows/actions/search.ts:49. Full rows ride inline on each entity value and
land in workflowStepRun.output, retained 30 days — a payload-size and data-minimisation problem
for tables like supplier, user, salesOrder. Do: select id plus the columns the
catalog's entity properties actually declare.
P1-10 · checkWorkflowVersionLock runs on every autosave and reads the whole definition
apps/erp/app/modules/workflows/workflows.server.ts:41 — two round trips roughly once per second
while editing, one of which (getWorkflowVersion) selects the entire nodes/edges JSONB purely
to check a lock. Do: one query —
select 1 from workflowVersion v join workflow w on … where v.id = w.activeVersionId.
P2 — robustness
P2-1 Redact
detail.packages/jobs/src/workflows/engine/ledger.ts:150writes it raw whileinput/outputgo throughredactForLog. Condition diagnostics carry resolved values.P2-2 Clamp
durationMs.ledger.ts:143lacks theMath.max(0, …)thatfinishRunhas, so it can go negative.P2-3 Fix the sequence collision. The trigger row hardcodes
sequence: 0(execute.ts:501) and the first node also reads0(execute.ts:540,state.sequencestarts at 0). Run detail orders by sequence, so the first two rows are ambiguously ordered. Start the walk at 1.P2-4 (product decision) Decide what a test run leaves behind.
engine/manual.tsusescreateMemoryLedger, so an owner can create records, update rows and fire outbound webhooks with zeroworkflowRun/workflowStepRunhistory. The side effects are intentional; the total absence of an audit trail probably isn't. Minimum: write a run header flagged as a test.P2-5 Move the permission gate before the privileged read.
apps/erp/app/routes/x+/workflow+/$id.test-run.tsx:247fetches the trigger record with service role (bypassing RLS) beforewalkWorkflowchecks the owner's moduleviewpermission. No leak today; the ordering is fragile. Hardening.P2-6
actorIdis dead payload. Parsed ininngest/functions/workflows/moment.ts:11andinngest/functions/events/workflow.ts:12, never read;MatchInput(packages/jobs/src/workflows/types.ts) has no such field. Related: origin is decided purely by the presence of the run tag, so every service-role / background / edge-function write classifies asPerson. Either useactorIdto classify properly, or drop the field.P2-7 SSRF guard gaps in
packages/jobs/src/workflows/actions/url-guard.ts:100.64.0.0/10(CGNAT) and198.18.0.0/15(benchmark) are not blocked, and URLs with embedded credentials are accepted. The DNS-rebinding TOCTOU (resolve, thenfetchre-resolves) is inherent — either pin the resolved IP or document it as accepted.P2-8 Re-validate
fieldat run time inrunSearch. Column names are checked at publish (packages/workflows/src/definition/nodes.ts:543) but the executor passesfieldstraight into.eq()/.ilike(). Defence in depth against validator/catalog drift.P2-9 Add optimistic concurrency to save.
$id.save.tsxhas none, and the canvas is explicitly designed as shared (canvasStateis per workflow, not per user), so two editors on one draft silently last-write-wins.P2-10 Make workflow delete async. The cascade (
workflowVersion → workflowRun → workflowStepRun) can be hundreds of thousands of rows inside the HTTP request transaction. Soft-delete + background purge.P2-11 Scope the two canvas workarounds that leaked app-wide:
apps/erp/app/components/Selectors/UserSelect/useUserSelect.ts:377setsuseOutsideClick({ immediate: true })unconditionally, changing close-on-press behaviour for every user select in the app.apps/erp/app/components/Selectors/UserSelect/components/TreeSelect.tsx:31swallowsonWheel/onTouchMovefor every UserSelect, including ones in drawers that need the scroll.
Gate both on being inside the canvas (prop or context).
P2-12 Remove the debug dump.
apps/erp/app/routes/x+/workflow+/$id.save.tsx:65logs full user definitions to server logs viaconsole.error(…, JSON.stringify(…, null, 2)).P2-13 Cache the Kysely client.
packages/jobs/src/db.ts:10builds a new instance on every call — once per engine step, per matcher run, per queue pass. The pool is already cached.P2-14 Cache the owner client per run.
execute.ts:106(contextFor) mints a JWT and a Supabase client for every node and again insideresolveBatchItems; a 100-item batch signs ~101 JWTs.P2-15 Parallelise batched items.
runBatchedNodeawaits onestep.runper item serially; durability justifies discrete steps, not serial ones.
P3 — cleanup
- P3-1
RunLedger.records()returns[]for the durable ledger (execute.ts:90) — an interface method meaningless for one of its two implementors. Split the interface, or letexecuteManualWorkflowRunown the memory ledger's accessor. - P3-2 Three overlapping id→route resolvers now exist:
getEntityPath(apps/erp/app/utils/entity.ts, id-prefix based),getWorkflowRecordPath(apps/erp/app/utils/path.ts:2210, entity-name based) and the pre-existinggetItemDetailPath. Collapse to one. - P3-3 Wire
packages/checks'workflow-eventsscript into CI, or drop it in favour of theworkflow-trigger-event-drift.sqlinvariant..github/workflows/check.yml:145only runscheck:workflow-catalog. - P3-4 Loop-guard depth resets to 0 when the causing run has been purged
(
packages/jobs/src/workflows/matcher.ts,causingRunfallback), so the chain cap isn't strictly enforceable. Low risk — worth a comment if not a fix. - P3-5 Measure the realtime cost.
workflowRunandworkflowStepRunare both in thesupabase_realtimepublication, and each step is an INSERT + an UPDATE, per item in a batch.
Docs
- D-1 Move the ten implemented specs from
.ai/specs/to.ai/specs/implemented/per.ai/specs/AGENTS.md:2026-07-30-workflows-{engine,event-catalog,foundation,matcher},2026-07-31-workflows-{builder-canvas,builder-ux-overhaul,node-configuration,run-history,scheduling},2026-08-03-workflow-variable-ux. (Only2026-07-30-workflows-catalogs.mdwas moved.) - D-2
packages/jobs/AGENTS.md:70documents anensureSchedulerChain()export that does not exist anywhere in the codebase — that line is the only occurrence of the identifier. The real mechanism istrigger("workflow-scheduler-wake", { bookedFor: null })insidesyncAndWake(apps/erp/app/modules/workflows/workflows.server.ts:79). Fix or delete the line. - D-3
packages/checks/AGENTS.mdhas zero mention of workflows — add theworkflow-eventsscript and theworkflow-trigger-event-drift.sqlinvariant. - D-4
packages/lib/AGENTS.mdhas zero mention of workflows — addsrc/workflows/raise-moment.tsand its contract (never throws into the caller;momentIddoubles as the Inngest event id and the matcher'ssourceEventId). - D-5 (optional)
packages/database/AGENTS.mdhas no seed section at all. This PR restructuredseed-dev.tsinto a ten-tierseed-dev/directory and deletedseed-change-orders.tsplus itsdb:seed:change-ordersscript.
Checked and found fine — do not re-raise
- Permission and plan gating is consistent across all 14 workflow routes
(
requirePermissions+requirePlan({ feature: "WORKFLOWS" })). - RLS policies use the
(SELECT get_companies_with_employee_permission(...))initplan-cached form. workflowLastRunissecurity_invoker, itsDISTINCT ONkey supports qual pushdown, and the supporting index exists.- Tenancy in
runUpdateActionis sound —existsInCompanyis checked on the target and on every entity-typed value before the write. claimStep/claimRunidempotency is genuinely atomic (unique-constraintON CONFLICT DO NOTHING; conditionalQueued → Runningupdate).- Webhook secret-header scrubbing is applied in both the log redactor and the response excerpt.
scanDue's partial indexworkflow_due_idxmatches its predicate exactly.- Schedule arithmetic is correct: wall-clock recomputation via
@internationalized/date(not instant addition, so DST-safe), monthly skips rather than clamps, and per-workflow jitter spread. - No
console.log/debugger/TODO/.only/.skipin added lines (the oneconsole.erroris P2-12). - No new file pushed past ~800 lines; the new code is well modularised.
Discarded as false positives
- "
delete.$id.tsxis missingassertIsPost." Only 1 of 10 delete routes in the repo uses it — omitting it is the convention. Not an inconsistency. - "Remaining JS
Dateusage violates the date rule." It doesn't.packages/checks/src/conformance/no-local-timezone.tsbans exactly four idioms (getLocalTimeZone(), UTC day-slicing, date-parts-of-now,setHours(0,0,0,0)) and explicitly states "Full-instantnew Date().toISOString()stays allowed — timestamps are timezone-free.".claude/rules/date-handling.mdcarries a matching narrow exception for instant arithmetic.Date.parse(completedAt),new Date(startedAt).getTime()and the scheduler's instant comparisons all fall inside it. (P2-2, the negativedurationMs, survives on its own merits — it is an inconsistency withfinishRun, not a date-rule issue.) - "Compute retention cutoffs in SQL." Same reasoning —
cutoffDaysis permitted instant arithmetic. Marginal at best.