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.
Batch Materials & Output Lots
Last tested: 2026-09-16 (feat/batch-materials-and-output-lots — re-verified on a clean crbn up stack)
Routes: ERP /x/resources/processes/$processId (Produced item rule), /x/production/batches/$batchId (Merge output lots); MES /x/operation/$operationId (batch materials annotation + Batch Number column)
Edge fns: issue (trackedEntitiesToBatch, jobOperationBatchOutput, mergeTrackedEntities), batch-operations (complete with trackedEntityId/batchNumber member fields)
Strategy
Same split as job-operation-batching.md: UI (agent-browser) for the process
rule row, the MES annotation, and the completion modal; edge fn curl +
SQL for every mutation proof. All logic proofs are DB-level.
Prerequisites / seeding
On top of the batching playbook's seeding gotchas:
- Produced items and the input item need
itemTrackingType='Batch';jobMakeMethod.requiresBatchTracking=trueper member. - Each member needs a WIP
trackedEntity(status='Reserved', quantity 0,attributes: {"Job Make Method": <jmmId>, "Job": <jobId>}) — outputs finalize into it, and the shared pick books consumption against it. - The input lot needs an
itemLedgerPositive Adjmt.row (bin resolution). itemCostrows are mandatory for every seeded item whencompanySettings.accountingEnabled—calculateCOGSthrows "no result" without them (first observed failure mode).- enum values:
methodType='Pull from Inventory',operationType='Process',processType='Process'.
Steps
1. producedItem compatibility rule
- SQL:
update process set "batchRules"='{"producedItem":"must"}'. - Edge fn
batch-operationscreatewith ops producing DIFFERENT items →These operations can't share a batch — the producedItem must match…. - Same produced item →
{success, readableId: BAT…}. - UI:
/x/resources/processes/$id→ Compatibility rules card shows a "Produced item" row; a storedmustrenders as "Require Match".
2. Shared pick (trackedEntitiesToBatch)
POST /functions/v1/issue{type:"trackedEntitiesToBatch", batchId, itemId, children:[{trackedEntityId: <lot>, quantity: <sum>}]}.- Verify:
jobMaterial.quantityIssuedper member = its OWN estimated quantity (4000/2500 from a 6500 pick); a partial draw creates a split child (Splitactivity) consumed by member 1, the remainder consumed whole by member 2; eachConsumeactivity outputs that member's WIP entity;itemLedgernets to zero for the lot moves + per-memberJob Consumption. - Over-pick →
Pick of N exceeds the batch's remaining requirement of M.
3. Completion with output lots
- Seed a batch-tagged
productionEvent, thenbatch-operationscompletewith member rows carryingtrackedEntityId(the WIP entity) +batchNumber. - Verify: WIP entities →
Availablewith the entered readableId + produced quantity; sliced events ∝ operationQuantity; ops Done; batch Completed; response carriesoutputTrackedEntityIds.
4. Output idempotency
- Re-invoke
issuejobOperationBatchOutputon an Available entity →{success:true, created:false}, still exactly ONEProduceactivity.
5. Lot merge
issue{type:"mergeTrackedEntities", trackedEntityIds:[A,B], readableId?}→ ONE new entity (Σ quantity, earliest parent expiry;readableIdinherits the FIRST parent's when omitted — the edge fn orders parents by the CALLER's list, so[WIP1,WIP2]yields WIP1's number and the reverse yields WIP2's),Mergeactivity (inputs = parents at their quantities, output = merged), net-zeroBatch Mergeledger rows, parentsConsumed.- MES prompt: completing via
/x/batch/$batchId/completewith ≥2 same-item outputs returns{merge:{count}}instead of redirecting; the prompt renders fromJobOperation(NOT the completion modal — completing unmounts it). - The merge action posts
intent=mergeand no ids; the route re-derives them from the batch's membership. Negative test: posttrackedEntityIds=<two unrelated Available same-item lots>and confirm those lots are untouched (before the fix they would have been merged). - ERP drawer shows "Merge output lots" only for a Completed batch with ≥2 Available same-item outputs (absent once merged).
6. MES batch materials UI
/x/operation/$memberOpIdin batch mode: the materials row showsBatch: <summed required>under required andBatch: <summed issued>under issued (fromgetBatchMaterialTotals).- Complete modal (icon button with the package-check icon in Controls — click
via
document.querySelectorAll('button')index hunting, aria-labels are empty on the icon buttons): table columns Job / Quantity / Scrap / Batch Number (batch-number inputs pre-filled from each member's WIP entity readableId). - The pick modal's quantity in batch mode defaults to the BATCH remaining
(6,500), not the member's share — if it shows the member's number the
batchRemainingQuantitywiring has regressed and the pro-rata split will under-serve every member.
Selector Notes
- MES Controls icon buttons have NO aria-labels; the big start/stop is the
size-24button, the complete/ellipsis buttons flank it. Click by index via eval, never by ref. - Login
Continueis overlay-blocked foragent-browser click— useform.requestSubmit(button)via eval.
Stack setup (crbn)
pnpm exec crbn status # port assignment + container health
pnpm exec crbn up --all # compose services + both dev servers
pnpm exec crbn reload edge-runtime # REQUIRED after editing functions/** — see below
crbn up rewrites .env.local with this worktree's own slug URLs
(https://{erp,mes,api}.<branch-slug>.dev) — never reuse another worktree's URLs.
The edge runtime caches compiled isolates. The functions tree is live-mounted,
so docker exec <edge> grep … shows your edit, but the running isolate still
serves the OLD module — a fix appears not to work while the file is provably
correct. crbn reload edge-runtime is the fix; allow ~30-60s before the first
call succeeds (an early call returns "An invalid response was received from the
upstream server").
Seeding gotchas found the hard way
- Seed the jobs at a location that HAS
storageUnitrows (all bins live at Manufacturing Plantloc_GYZ…, none at Headquarters) and give the input lot'sitemLedgerrow astorageUnitId— otherwiseget_available_tracked_entitiesreturns nothing and the pick modal says "Batch number is not available". - That RPC's argument order is
(item, company, location)— easy to transpose. - The Scan field takes the entity id (what a barcode encodes), not the human
readableId; a valid scan auto-submits the pick. - Resetting a batch for a re-run must also zero
jobOperation.quantityComplete, or the completion modal pre-fills 0, reads every row as "not in this run", and disables submit.
Common Failures
{"message":"no result"}on the pick → missingitemCostrow (accounting on).duplicate key … accountingPeriodon the FIRST pick of a month → fixed in code (period pre-resolved before the batch transaction); if seen, the edge isolate is stale —crbn reload edge-runtime.- Merged lot comes back with
readableId: null→ stale isolate (the builder inherits the first parent's number). Reload and retry. {}empty response with 200 → look atdocker logs carbon-carbon-edge-runtime-1— the error is server-side.