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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(production): address review on strict backflush
naveenkash21909deca2b1
diff --git a/.ai/runs/2026-10-14-strict-backflush.md b/.ai/runs/2026-10-14-strict-backflush.mdindex 42a52742c9..74597b3842 100644--- a/.ai/runs/2026-10-14-strict-backflush.md+++ b/.ai/runs/2026-10-14-strict-backflush.md@@ -1,4 +1,4 @@-# Feature run: Strict backflush (CARBON-113)+# Feature run: Strict backflush- Date: 2026-10-14- Mode: fully-autonomousdiff --git a/.ai/specs/2026-10-14-strict-backflush.md b/.ai/specs/2026-10-14-strict-backflush.mdindex 5443b65ec9..072b6d8178 100644--- a/.ai/specs/2026-10-14-strict-backflush.md+++ b/.ai/specs/2026-10-14-strict-backflush.md@@ -3,7 +3,6 @@> Status: implemented> Author: naveenkash> Date: 2026-10-14-> Ticket: CARBON-113 — Backflush rules## TLDR@@ -161,7 +160,7 @@ No new table. No RLS change.## Changelog-- 2026-10-14: Created from the CARBON-113 design discussion.+- 2026-10-14: Created from the backflush rules design discussion.- 2026-10-15: Long lists. A 20-line BoM produced a 912-character toast that ran off a small screen and faded before it could be read. Every strict refusal now names the first entries and counts the rest (`summarizeList`: 5 part names, 3 shortfalls; the SQL caps at 3 too). The Release dialog keeps the full list in a scroll box, and a collapsed BoM or job BoM row shows a red "No storage unit" badge.- 2026-10-15: The activation error names only the parts, not the locations (user request).- 2026-10-15: Browser test. Fixed: a location with a work center but no storage unit blocked activation for good; the release list named a part twice; the Release dialog did not warn before submit; the job BoM line showed "Default Storage Unit" and "Optional"; a refusal named only the first short material (TypeScript and SQL).diff --git a/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx b/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsxindex e65e7e70ae..22c0889bba 100644--- a/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx+++ b/apps/erp/app/modules/production/ui/Jobs/JobHeader.tsx@@ -689,6 +689,7 @@ export function JobStartModal({"id, description, methodType, itemType, storageUnitId, item(itemTrackingType, readableIdWithRevision)").eq("jobId", job.id!)+ .eq("companyId", job.companyId!).is("storageUnitId", null): Promise.resolve({ data: [] })]);diff --git a/apps/erp/app/modules/settings/settings.service.ts b/apps/erp/app/modules/settings/settings.service.tsindex 1b29c6194b..d7feb527ff 100644--- a/apps/erp/app/modules/settings/settings.service.ts+++ b/apps/erp/app/modules/settings/settings.service.ts@@ -1258,33 +1258,33 @@ export async function getActiveMethodsMissingPullFrom(error: Error | null;}> {try {- const [lines, locations] = await Promise.all([- db- .selectFrom("methodMaterial")- .innerJoin("makeMethod", (join) =>- join- .onRef("makeMethod.id", "=", "methodMaterial.makeMethodId")- .on("makeMethod.companyId", "=", companyId)- .on("makeMethod.status", "=", "Active")- )- .innerJoin("item as component", "component.id", "methodMaterial.itemId")- .innerJoin("item as parent", "parent.id", "makeMethod.itemId")- .where("methodMaterial.companyId", "=", companyId)- .where("methodMaterial.methodType", "!=", "Make to Order")- .where("methodMaterial.itemType", "in", [...PULLED_ITEM_TYPES])- .select([- "makeMethod.id as makeMethodId",- "parent.id as itemId",- "parent.type as itemType",- "parent.readableIdWithRevision as readableId",- "methodMaterial.methodType",- "methodMaterial.itemType as lineItemType",- "methodMaterial.storageUnitIds",- "component.itemTrackingType"- ])- .execute(),- getPullFromLocations(db, companyId)- ]);+ // One query at a time: run together, each would take its own pooled+ // connection, and opening one costs more than these reads do.+ const lines = await db+ .selectFrom("methodMaterial")+ .innerJoin("makeMethod", (join) =>+ join+ .onRef("makeMethod.id", "=", "methodMaterial.makeMethodId")+ .on("makeMethod.companyId", "=", companyId)+ .on("makeMethod.status", "=", "Active")+ )+ .innerJoin("item as component", "component.id", "methodMaterial.itemId")+ .innerJoin("item as parent", "parent.id", "makeMethod.itemId")+ .where("methodMaterial.companyId", "=", companyId)+ .where("methodMaterial.methodType", "!=", "Make to Order")+ .where("methodMaterial.itemType", "in", [...PULLED_ITEM_TYPES])+ .select([+ "makeMethod.id as makeMethodId",+ "parent.id as itemId",+ "parent.type as itemType",+ "parent.readableIdWithRevision as readableId",+ "methodMaterial.methodType",+ "methodMaterial.itemType as lineItemType",+ "methodMaterial.storageUnitIds",+ "component.itemTrackingType"+ ])+ .execute();+ const locations = await getPullFromLocations(db, companyId);const locationIds = locations.map((location) => location.id);const methods = new Map<diff --git a/packages/database/supabase/migrations/20261015151804_strict-backflush.sql b/packages/database/supabase/migrations/20261015151804_strict-backflush.sqlindex 6647027253..6c1f2b137b 100644--- a/packages/database/supabase/migrations/20261015151804_strict-backflush.sql+++ b/packages/database/supabase/migrations/20261015151804_strict-backflush.sql@@ -1,4 +1,4 @@--- Strict backflush (.ai/specs/2026-10-14-strict-backflush.md, CARBON-113).+-- Strict backflush (.ai/specs/2026-10-14-strict-backflush.md).---- companySettings.requireBackflushStorageUnit turns it on (off by default).-- backflush_job_materials is copied from its live definitiondiff --git a/packages/server-functions/src/lib/strict-backflush.ts b/packages/server-functions/src/lib/strict-backflush.tsindex 64462a6efd..c881a39b3e 100644--- a/packages/server-functions/src/lib/strict-backflush.ts+++ b/packages/server-functions/src/lib/strict-backflush.ts@@ -10,6 +10,7 @@import type { Database } from "@carbon/database";import type { KyselyDatabase as DB } from "@carbon/database/client";+import { inOrder } from "@carbon/database/rows";import {binShortfall,getPullFromLocations,@@ -116,14 +117,15 @@ export async function binCoverProblem(if (binShortfall({ onHand, alreadyDrawn, quantity }) === 0) return null;- const [item, storageUnit] = await Promise.all([- readItemName(db, itemId, companyId),- db- .selectFrom("storageUnit")- .where("id", "=", storageUnitId)- .where("companyId", "=", companyId)- .select("name")- .executeTakeFirst()+ const [item, storageUnit] = await inOrder([+ () => readItemName(db, itemId, companyId),+ () =>+ db+ .selectFrom("storageUnit")+ .where("id", "=", storageUnitId)+ .where("companyId", "=", companyId)+ .select("name")+ .executeTakeFirst()]);const have = round(Math.max(onHand - alreadyDrawn, 0));return `Not enough ${item} in ${storageUnit?.name ?? storageUnitId}: need ${round(quantity)}, have ${have}`;@@ -140,24 +142,25 @@ export async function assertMethodHasPullFrom(): Promise<void> {const { makeMethodId, companyId } = args;- const [lines, locations] = await Promise.all([- db- .selectFrom("methodMaterial")- .innerJoin("item", "item.id", "methodMaterial.itemId")- .where("methodMaterial.makeMethodId", "=", makeMethodId)- .where("methodMaterial.companyId", "=", companyId)- .select([- "methodMaterial.methodType",- "methodMaterial.itemType",- "methodMaterial.storageUnitIds",- "methodMaterial.order",- "item.id as itemId",- "item.itemTrackingType",- "item.readableIdWithRevision"- ])- .orderBy("methodMaterial.order")- .execute(),- getPullFromLocations(db, companyId)+ const [lines, locations] = await inOrder([+ () =>+ db+ .selectFrom("methodMaterial")+ .innerJoin("item", "item.id", "methodMaterial.itemId")+ .where("methodMaterial.makeMethodId", "=", makeMethodId)+ .where("methodMaterial.companyId", "=", companyId)+ .select([+ "methodMaterial.methodType",+ "methodMaterial.itemType",+ "methodMaterial.storageUnitIds",+ "methodMaterial.order",+ "item.id as itemId",+ "item.itemTrackingType",+ "item.readableIdWithRevision"+ ])+ .orderBy("methodMaterial.order")+ .execute(),+ () => getPullFromLocations(db, companyId)]);const locationIds = locations.map((location) => location.id);