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(assembly): instruction steps follow a job operation's own steps instead of interleaving with them
aashud3412187f900
diff --git a/apps/erp/app/modules/production/AGENTS.md b/apps/erp/app/modules/production/AGENTS.mdindex eab3eeea72..f93a5babf4 100644--- a/apps/erp/app/modules/production/AGENTS.md+++ b/apps/erp/app/modules/production/AGENTS.md@@ -113,7 +113,7 @@ pnpm --filter @carbon/erp test- `getAssemblyUnits` / `upsertAssemblyUnit` / `deleteAssemblyUnit` — model-scoped "plan as one component" overrides (`assemblyUnit`: `componentNodeIds[]` + optional `itemId`), authored from the BOM tree- **Unit derivation**: the geometry planner itself auto-detects "detail swarms" — a populated PCB's hundreds of tiny components seated on a board — from pure geometry (`detect_swarm_units`, `crates/planner/src/pipeline2.rs`: a substantial host part + many tiny parts in narrowphase contact with it, each dwarfed by the host; fastener-named parts never join so screws keep their own animations). No BOM or LLM involved. The only TS-side unit source is the user-authored `assemblyUnit` rows ("plan as one component" from the BOM tree), which `loadPlanUnits` (`@carbon/jobs`) sends as `options.units`; the planner merges units for planning and expands them back to member leaves with a named `groups` entry (→ one titled step). Detected groups are ALSO materialized as `assemblyUnit` rows at step generation (`sourceGroupId` = the plan group id, e.g. `swarm:<hostNodeId>`; UNIQUE `(modelUploadId, sourceGroupId)`, upsert DO NOTHING so user renames/edits/deletions win within a plan — a regenerate re-materializes deleted ones) — the Components tab shows them like authored units with an "Auto" badge, and `loadPlanUnits` feeds them back to the planner as caller units so re-plans respect user edits. Flat multi-body STEP uploads (one PRODUCT, many solids, no assembly tree — the common Fusion/SolidWorks export shape) are split into per-solid components by the converter (`crates/occt-bridge` `read_step`), so they plan like proper assemblies instead of one un-plannable blob- `getFlattenedBomMaterials` — the item's engineering BOM flattened through Make subassemblies; feeds the assembly BOM tree and the per-step material picker-- `syncAssemblyInstructionToOperation` — Assembly → job-operation re-sync (Kysely txn): copies an instruction's steps (any status) into a `jobOperation` as `jobOperationStep` rows (typed fields mirror 1:1), links per-step BOM parts via `jobMaterialStep` (itemId resolved against the operation's own `jobMaterial`), refreshes slides on synced steps (delete + recreate: the instruction's 3D model leads as a model slide at sortOrder 0, then the step's authored `assemblyInstructionStepSlide` rows), syncs tools (ensures a `jobOperationTool` per distinct tool item — never deleted, quantity only ratchets up — and rebuilds `jobOperationToolStep` links on synced steps), and sets `jobOperation.assemblyInstructionId`. Re-runnable via the `assemblyInstructionStepId` provenance marker (update matched / insert new / delete stale; hand-authored NULL-marker steps untouched; ON DELETE SET NULL demotes to hand-authored). Route `x+/assembly+/$id.sync-bop.tsx` (action-only, `production_update`, guarded to unlocked jobs); UI = "Sync Assembly Steps" on an Assembly operation in `JobBillOfProcess` (`AssemblyStepsSyncModal`), mirroring the "Sync Procedure" flow. **Method/quote operations only store the `assemblyInstructionId` pointer** — they never materialize steps; jobs inherit steps at get-method time (`insertAssemblyDataForJobOperation`, reading straight from the instruction) and re-sync on demand here. The MES assembly view uses the marker for **step-aware 3D playback**: `getAssemblyPlaybackByOperationId` (apps/mes operations.service) loads the linked instruction's GLB/graph/steps and `AssemblyView` mounts the `@carbon/viewer` `AssemblyPlayer` pinned to the operator's current step+- `syncAssemblyInstructionToOperation` — Assembly → job-operation re-sync (Kysely txn): copies an instruction's steps (any status) into a `jobOperation` as `jobOperationStep` rows (typed fields mirror 1:1), links per-step BOM parts via `jobMaterialStep` (itemId resolved against the operation's own `jobMaterial`), refreshes slides on synced steps (delete + recreate: the instruction's 3D model leads as a model slide at sortOrder 0, then the step's authored `assemblyInstructionStepSlide` rows), syncs tools (ensures a `jobOperationTool` per distinct tool item — never deleted, quantity only ratchets up — and rebuilds `jobOperationToolStep` links on synced steps), and sets `jobOperation.assemblyInstructionId`. Synced steps are numbered AFTER the operation's own (procedure / hand-authored) steps — both sets start at 1, so keeping the instruction's `sortOrder` interleaved them; `insertAssemblyDataForJobOperation` in get-method does the same. Re-runnable via the `assemblyInstructionStepId` provenance marker (update matched / insert new / delete stale; hand-authored NULL-marker steps untouched; ON DELETE SET NULL demotes to hand-authored). Route `x+/assembly+/$id.sync-bop.tsx` (action-only, `production_update`, guarded to unlocked jobs); UI = "Sync Assembly Steps" on an Assembly operation in `JobBillOfProcess` (`AssemblyStepsSyncModal`), mirroring the "Sync Procedure" flow. **Method/quote operations only store the `assemblyInstructionId` pointer** — they never materialize steps; jobs inherit steps at get-method time (`insertAssemblyDataForJobOperation`, reading straight from the instruction) and re-sync on demand here. The MES assembly view uses the marker for **step-aware 3D playback**: `getAssemblyPlaybackByOperationId` (apps/mes operations.service) loads the linked instruction's GLB/graph/steps and `AssemblyView` mounts the `@carbon/viewer` `AssemblyPlayer` pinned to the operator's current step- `createJobDocumentUploadUrl` — MCP file upload (step 1): presigned URL for a job document (`job/{jobId}` folder); pair with `documents_insertUploadedDocument`. See `.claude/rules/mcp-tools-reference.md` → "File uploads"diff --git a/apps/erp/app/modules/production/production.service.ts b/apps/erp/app/modules/production/production.service.tsindex b61710b109..1cf3149154 100644--- a/apps/erp/app/modules/production/production.service.ts+++ b/apps/erp/app/modules/production/production.service.ts@@ -9378,6 +9378,17 @@ export async function syncAssemblyInstructionToOperation(existingSynced);+ // The operation's own steps number from 1 as well: the instruction's+ // steps follow them, or the two interleave.+ const syncedIds = new Set(existingSynced.map((step) => step.id));+ const firstSortOrder =+ Math.max(+ 0,+ ...existingSteps+ .filter((step) => !syncedIds.has(step.id))+ .map((step) => step.sortOrder ?? 0)+ ) + 1;+const now = new Date().toISOString();let created = 0;let updated = 0;@@ -9406,7 +9417,7 @@ export async function syncAssemblyInstructionToOperation(maxValue: source.maxValue,listValues: source.listValues,fileTypes: source.fileTypes,- sortOrder: source.sortOrder ?? index + 1+ sortOrder: firstSortOrder + index};const existingId = targetIdBySourceId.get(source.id);diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.tsindex d3531e3f09..d0526ef178 100644--- a/packages/database/supabase/functions/get-method/index.ts+++ b/packages/database/supabase/functions/get-method/index.ts@@ -8215,6 +8215,16 @@ async function insertAssemblyDataForJobOperation(toolsByStep.set(tool.stepId, list);}+ // The operation's own steps (its procedure's, inserted before this) number+ // from 1 as well: the instruction's steps follow them, or the two interleave.+ const ownSteps = await trx+ .selectFrom("jobOperationStep")+ .select((eb) => eb.fn.max("sortOrder").as("lastSortOrder"))+ .where("operationId", "=", operationId)+ .where("companyId", "=", companyId)+ .executeTakeFirst();+ const firstSortOrder = Number(ownSteps?.lastSortOrder ?? 0) + 1;+// Correlate inserted job steps back to their source steps via the provenance// column, not row order.const insertedSteps = await trx@@ -8231,7 +8241,7 @@ async function insertAssemblyDataForJobOperation(maxValue: source.maxValue,listValues: source.listValues,fileTypes: source.fileTypes,- sortOrder: source.sortOrder ?? index + 1,+ sortOrder: firstSortOrder + index,assemblyInstructionStepId: source.id,companyId,createdBy: userId,