Skip to content

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.

Merge remote-tracking branch 'origin/main' into naveenkash/capacity-replan-triggers

naveenkash15d9680c86cc
diff --cc .ai/lessons.md
index b26198213d,f854958267..1ac39c816e
--- a/.ai/lessons.md
+++ b/.ai/lessons.md
@@@ -4150,12 -4170,23 +4170,32 @@@ The write was then removed (nothing rea
**Applies to:** `crates/occt-bridge/src/*.cc` (any function that opens a BinXCAF document), `apps/assembler/src/actions/optimize.rs` (`run_ingest`).
+## A save that reconciles a whole set must be shown the whole set, whatever the page filters
+
+**Context:** The People board's day editor (`PeopleHoursModal` → `setPeopleDay`) saves a person's day: it updates the rows it is sent and DELETES the rows of the save's shift it is not sent (2026-10-28).
+
+**Problem:** The editor opened with the rows the page showed. The department filter hid rows at other stations, and the shift filter hid rows of other shifts, while the save was scoped to the person's own shift (or every shift, for a person with none). Saving deleted rows the user never saw, or inserted a second row for a station the person already held in another shift.
+
+**Rule:** A save that reconciles a set (deletes what it is not sent) gets that set from the same scope the server deletes by, never from what the page filtered. Pass the unfiltered rows next to the filtered ones and select the save's scope in one pure helper (`editorDayRows`, `peopleShared.ts`), with a test.
+
+**Applies to:** `apps/erp/app/modules/production/ui/Schedule/People/*` (Board and Matrix editors), `setPeopleDay` and any other "make these rows the whole day/week" write in `production.service.ts`.
+
+ ## A new table handler's generated migration fails locally with "must be owner"
+
+ **Context:** A new statement handler (`queue_pmi_inspection_plans`) was added to `event-system/handlers/` and `attachments.ts`. `pnpm db:migrate` synced it, then `authz migration` wrote the generated migration that ships it (2026-10-27).
+
+ **Problem:** The next `pnpm db:migrate` failed on that migration with `must be owner of function queue_pmi_inspection_plans`. The sync had created the function as `supabase_admin`. Every older handler is owned by `postgres`, the role the migration runs its `CREATE OR REPLACE` as. Production never syncs, so only a local database that synced first is affected.
+
+ **Rule:** If the generated migration of a new managed function fails locally with "must be owner", run `ALTER FUNCTION public.<name>() OWNER TO postgres;` as `supabase_admin`, then run `pnpm db:migrate` again. Do not edit the generated migration.
+
+ **Applies to:** `packages/database/src/event-system/handlers/*.sql`, `packages/database/src/event-system/functions/*.sql`, `pnpm --filter @carbon/database authz migration`.
+
+ ## A form validator is not an API contract
+
+ **Context:** `production_createJobOperation` answered a bare 500 "Failed to insert job operation" to an API caller (2026-10-27).
+
+ **Problem:** Operations moved from routes kept the route's input and error handling. The form sent a fresh client-made `id` for each new row, so the create input required one and an API caller had to invent it; a reused id failed the insert. The route's toast-only `ctx.error("internal", "Failed to …", error)` (448 of them) then hid the database's reason, so a duplicate key or a reference to nothing read as our failure.
+
+ **Rule:** A create input leaves the new row's `id` optional when the table has a default (`mcp-create-inputs.test.ts` fails otherwise). A database refusal the caller can fix is reported as itself: `defineServiceFn` maps an `internal` error whose cause is one (`databaseRefusal`), so don't swallow the cause. A yes/no field is a real boolean that also parses the form's `"true"` / `"false"` (`booleanField`, `shared.models.ts`), so the API publishes `boolean`. A rich-text (JSON) column takes `optionalTiptapDoc` / `requiredTiptapDoc`, never `z.string()` or `z.unknown()`: a raw string is stored as a JSON scalar the editor cannot read. A JSON value a form posts as text takes `jsonInput(schema)`. Never `z.preprocess` / `z.coerce` / `z.any()` bare in an operation's input: the manifest publishes those as "anything".
+
+ **Applies to:** every `defineServiceFn` in `apps/erp/app/modules/*/*.server.ts`, and any validator reused as an operation's input.
diff --cc apps/erp/app/modules/resources/AGENTS.md
index e133c21117,5764377f67..7f36f7dc78
--- a/apps/erp/app/modules/resources/AGENTS.md
+++ b/apps/erp/app/modules/resources/AGENTS.md
@@@ -68,9 -68,10 +68,10 @@@ Each operation is one `defineServiceFn
|---|---|---|
| `createAbility` / `deactivateAbility` | `x+/resources+/abilities.new.tsx`, `abilities.delete.$id.tsx` | The process must be the company's; turns `requiresAbility` on; the scheduler is told |
| `addEmployeeAbility` / `updateEmployeeAbility` / `removeEmployeeAbility` | `ability.$id.employee.new.tsx`, `person.$personId.ability.new.tsx`, `ability.$id.employee.$employeeAbilityId.tsx`, `ability.$id.employee.delete.$employeeAbilityId.tsx` | The ability must be the company's; a blank expiry comes from the recertification period; the scheduler is told |
-| `createLocation` / `updateLocation` | `locations.new.tsx`, `locations.$locationId.tsx` | An update needs `resources` create and update (the old route's gate and the row's policy) and drops the cached timezone |
-| `createProcess` / `updateProcess` | `processes.new.tsx`, `processes.$processId.tsx` | `resources` create; an update needs create, update and delete (it replaces the work centers on the system connection); `requiresAbility` creates the ability, and is rolled back when that fails; a change of it tells the scheduler |
-| `createWorkCenter` / `updateWorkCenter` / `reactivateWorkCenter` / `deactivateWorkCenter` / `deleteWorkCenter` | `work-centers.new.tsx`, `work-centers.$id.tsx`, `work-centers.activate.$id.tsx`, `work-centers.deactivate.$id.tsx`, `work-centers.delete.$id.tsx` | A create needs `resources` create and update; deactivate is a soft delete; delete refuses while anything references it (`getWorkCenterReferences` lists it for the modal); each tells the scheduler |
+| `createLocation` / `updateLocation` | `locations.new.tsx`, `locations.$locationId.tsx` | An update needs `resources` create and update (the old route's gate and the row's policy) and drops the cached timezone; a new timezone re-plans the location (kind `location`) |
- | `createProcess` / `updateProcess` | `processes.new.tsx`, `processes.$processId.tsx` | `resources` create; an update needs create, update and delete (it replaces the work centers on the system connection); `requiresAbility` creates the ability, and is rolled back when that fails; a change of it tells the scheduler; a name another process holds is a `conflict` (`findProcessNamed`), shown on the form's Name field |
- | `createWorkCenter` / `updateWorkCenter` / `reactivateWorkCenter` / `deactivateWorkCenter` | `work-centers.new.tsx`, `work-centers.$id.tsx`, `work-centers.activate.$id.tsx`, `work-centers.delete.$id.tsx` | A create needs `resources` create and update; deactivate is a soft delete; each tells the scheduler; `workCenterFormValidator` (the form's; `workCenterValidator` is the API's and does not read `operatingHours`) refuses Operating hours "Some Shifts" with no shift (it would save as every shift) |
++| `createProcess` / `updateProcess` | `processes.new.tsx`, `processes.$processId.tsx` | `resources` create; an update needs create, update and delete (it replaces the work centers on the system connection); `requiresAbility` creates the ability, and is rolled back when that fails; a change of it tells the scheduler; a name another process holds, deactivated ones included, is a `conflict` (`findProcessNamed`, `processNameTakenMessage`), shown on the form's Name field, and so is a duplicate the database refuses (23505) on create or update |
++| `createWorkCenter` / `updateWorkCenter` / `reactivateWorkCenter` / `deactivateWorkCenter` / `deleteWorkCenter` | `work-centers.new.tsx`, `work-centers.$id.tsx`, `work-centers.activate.$id.tsx`, `work-centers.deactivate.$id.tsx`, `work-centers.delete.$id.tsx` | A create needs `resources` create and update; deactivate is a soft delete; delete refuses while anything references it (`getWorkCenterReferences` lists it for the modal); each tells the scheduler; `workCenterFormValidator` (the form's; `workCenterValidator` is the API's and does not read `operatingHours`) refuses Operating hours "Some Shifts" with no shift (it would save as every shift) |
+ | `deleteProcess` / `getProcessReferences` | `processes.delete.$processId.tsx` | Refuses while anything references the process, including through its ability |
| `createMaintenanceSchedule` / `updateMaintenanceSchedule` | `scheduled-maintenance.new.tsx`, `scheduled-maintenance.$scheduleId.tsx` | Generates the schedule's dispatches now (`generate-maintenance`) |
| `createMaintenanceDispatch` / `updateMaintenanceDispatchField` / `setMaintenanceDispatchStatus` | `x+/maintenance+/new.tsx`, `update.tsx`, `$dispatchId.status.tsx` | `updateMaintenanceDispatchField` refuses a locked dispatch, takes a time as an ISO 8601 instant with an offset, and tells the scheduler about an offline dispatch's downtime; Completed posts the labor (`warning` when it fails); `setMaintenanceDispatchStatus` also reopens |
| `createMaintenanceEvent` / `updateMaintenanceEvent` / `deleteMaintenanceEvent` / `startMaintenanceEvent` / `stopMaintenanceEvent` | `$dispatchId.event.new.tsx`, `$dispatchId.event.$eventId.tsx`, `$dispatchId.event.$eventId.delete.tsx`, `$dispatchId.events.tsx` | Refused on a Completed or Cancelled dispatch; the timecard must be the dispatch's, its employee an active employee and its work center the company's (both are tagged on the labor journal); the labor is posted again (`warning` when it fails) |
diff --cc apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json
index 3ed35850a7,d34c4582a5..15220e751d
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-manifest.digest.json
@@@ -798,10 -798,10 +798,10 @@@
{"name":"production_getUnbatchedBatchableOperationCount","classification":"READ","schema":"31b84d65b2ac","response":"683e4ed05232","permission":"production:view","paginates":false},
{"name":"production_getUnscheduledJobs","classification":"READ","schema":"75b90f67d533","response":"eb77a38acf19","permission":"production:view","paginates":false},
{"name":"production_getWorkCenterRequiredAbilities","classification":"READ","schema":"75b90f67d533","response":"7418ea1fc0a7","permission":"production:view","paginates":false},
- {"name":"production_getWorkCenterReservationsRange","classification":"READ","schema":"c310a7ca038d","response":"a7e9bc09d2ab","permission":"production:view","paginates":false},
+ {"name":"production_getWorkCenterReservationsRange","classification":"READ","schema":"c310a7ca038d","response":"ae001677fab0","permission":"production:view","paginates":false},
{"name":"production_getWorkCenterShifts","classification":"READ","schema":"ef138c43c92c","response":"2b4835f80e6c","permission":"production:view","paginates":false},
- {"name":"production_insertAssemblyInstructionStep","classification":"WRITE","schema":"bf4512d170cc","response":"00404e686415","permission":"production:create","paginates":false},
- {"name":"production_insertJob","classification":"WRITE","schema":"f450754aacd3","response":"ad63dfd9d877","permission":"production:create","paginates":false},
+ {"name":"production_insertAssemblyInstructionStep","classification":"WRITE","schema":"0ae0a151262e","response":"00404e686415","permission":"production:create","paginates":false},
+ {"name":"production_insertJob","classification":"WRITE","schema":"3dec87ac6f73","response":"ad63dfd9d877","permission":"production:create","paginates":false},
{"name":"production_makeAssemblySubAssembly","classification":"WRITE","schema":"bd69d909a2e4","response":"00404e686415","permission":"production:update","paginates":false},
{"name":"production_mapAssemblyComponent","classification":"WRITE","schema":"4b0e8ad9fe70","response":"3189c67145d5","permission":"production:update","paginates":false},
{"name":"production_markPersonAbsent","classification":"WRITE","schema":"16a1cd418a2e","response":"3797626bd17e","permission":"production:update","paginates":false},
@@@ -839,10 -839,10 +839,10 @@@
{"name":"production_setJobOperationWorkInstruction","classification":"WRITE","schema":"5c977b058996","response":"3797626bd17e","permission":"production:update","paginates":false},
{"name":"production_setJobQuantity","classification":"WRITE","schema":"14db87743cc7","response":"0eb0c2be01ed","permission":"production:update","paginates":false},
{"name":"production_setJobSalesOrderLine","classification":"WRITE","schema":"9b91d34f8382","response":"0eb0c2be01ed","permission":"production:update","paginates":false},
- {"name":"production_setPeopleOvertime","classification":"WRITE","schema":"89851b04ba54","response":"393a0d83325c","permission":"production:update","paginates":false},
+ {"name":"production_setPeopleOvertime","classification":"WRITE","schema":"89851b04ba54","response":"c76bbc719a9e","permission":"production:update","paginates":false},
- {"name":"production_setPersonDay","classification":"WRITE","schema":"b2bcad277f44","response":"3d90cb5d0437","permission":"production:update","paginates":false},
+ {"name":"production_setPersonDay","classification":"WRITE","schema":"b610d345119b","response":"3d90cb5d0437","permission":"production:update","paginates":false},
{"name":"production_setPersonHours","classification":"WRITE","schema":"c99328071885","response":"3797626bd17e","permission":"production:update","paginates":false},
- {"name":"production_setProcedureFields","classification":"WRITE","schema":"e76f126b5b16","response":"0eb0c2be01ed","permission":"production:update","paginates":false},
+ {"name":"production_setProcedureFields","classification":"WRITE","schema":"9594e06855e0","response":"0eb0c2be01ed","permission":"production:update","paginates":false},
{"name":"production_syncAssemblyToOperation","classification":"DESTRUCTIVE","schema":"da43bcac38a0","response":"4348d3bad80b","permission":"production:update","paginates":false},
{"name":"production_syncJobOperationProcedure","classification":"WRITE","schema":"4db296880a62","response":"c407a9edb930","permission":"production:update","paginates":false},
{"name":"production_unassignPerson","classification":"DESTRUCTIVE","schema":"3797626bd17e","response":"3797626bd17e","permission":"production:update","paginates":false},