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
Plan: Run the scheduling engine in-process (Node)
Spec: .ai/specs/2026-08-19-schedule-in-process-node.md
Date: 2026-08-19
Status: DONE — all gates green (typecheck erp/jobs/database, full erp build,
155 Deno scheduling tests, jobs+database vitest).
Deviations discovered during execution (all resolved)
production.service.tsis client-bundled, so it CANNOT statically import a.servermodule or the engine (the fullerpbuild caught this via thereact-router:dot-serverplugin). Fix: it dynamic-imports@carbon/database/scheduling+@carbon/database/clientinside the two functions and uses a lazy local Kysely pool (getSchedulingDb, pool size 5) — the same tacticnotifyScheduleInputsChangedalready uses withawait import("@carbon/jobs"). It uses the already-passedclientfor the engine's reads (any same-company employee client can read the master data) instead ofgetCarbonServiceRole, so it needs no.serverauth import. The route/jobs call sites keep STATIC imports (React Router strips their server-only action code from the client bundle; jobs isn't client code).- Importing the engine into the Node typecheck surfaced ~34 latent
noUncheckedIndexedAccessissues in vendored Deno engine files (Deno's config has the check off) + 2 Deno-driver type errors. Fixed: (a) minimal!/guards on provably-in-bounds accesses (behavior-preserving; all 155 Deno tests still pass), (b) decoupled the engine'sDBtype from the Deno-onlydatabase.ts/driver.tsby addingexport type DB = KyselyDatabasetopostgres/index.tsand repointing 5import type { DB }sites there — so the Deno postgres driver never enters any Node typecheck. - The edge function is KEPT as a thin wrapper (not deleted) — one orchestration, compatibility preserved.
Task 1 — Make logging.ts dual-runtime
File: packages/database/supabase/functions/lib/logging.ts
- Replace the unguarded
Deno.env.get("LOG_LEVEL")with a runtime-guarded read that falls back toprocess.env.LOG_LEVELin Node (mirror thetypeof Deno !== "undefined"guard already used inpostgres/index.ts). - Verify: no other
Deno.reference remains outside a guard in this file.
Task 2 — Declare @logtape deps on packages/database
File: packages/database/package.json
- Add
@logtape/logtapeand@logtape/redaction(catalog:) todependencies. -
pnpm installsucceeds; both resolve underpackages/database/node_modules.
Task 3 — Extract runLocationSchedule
New file: packages/database/supabase/functions/lib/scheduling/run-schedule.ts
- Move
deadlineRank,asMs, the job query, the sort, batch build, expedite branch, and the sequential engine loop out of the edgeserve()handler. - Signature:
runLocationSchedule(opts: { db: Kysely<DB>; client: SupabaseClient<Database>; locationId: string; companyId: string; userId: string; expediteJobId?: string }): Promise<LocationScheduleResult | ExpediteResult>. - Export result types. Use only Node+Deno-safe imports (engine, provider,
priority-calculator, types,
@internationalized/dateif needed — NOTnpm:zod, NOT deno std, NOT../supabase.ts, NOT../response.ts). -
tsc/tsgo sees the new file typecheck under@carbon/database.
Task 4 — Edge function delegates to the shared orchestration
File: packages/database/supabase/functions/schedule/index.ts
- Keep
serve, CORS preflight,payloadValidator(npm zod),requirePermissions,jsonResponse/errorResponse, and the module-scope pool/db. - Replace the inline orchestration with a single
runLocationSchedule({ db, client, locationId, companyId, userId, expediteJobId })call; return its result viajsonResponse. - Behavior identical (same response JSON).
Task 5 — Node bridge export
File: packages/database/src/scheduling.ts
- Re-export
runLocationScheduleand its result types fromrun-schedule.ts. -
@carbon/database/schedulingexposesrunLocationSchedule.
Task 6 — Repoint ERP call sites
apps/erp/app/routes/api+/schedule.ts— callrunLocationSchedule({ db: getDatabaseClient(), client: getCarbonServiceRole(), … }); surface{ success, error }.apps/erp/app/modules/production/production.service.tsrecalculateJobOperationDependencies— after resolvinglocationId, callrunLocationSchedulewithgetDatabaseClient()+getCarbonServiceRole()instead ofclient.functions.invoke("schedule", …).apps/erp/app/routes/x+/job+/$jobId.status.tsx— replace theserviceRole.functions.invoke("schedule", …)inside thePromise.allwith arunLocationSchedule(...)call (keep the parallelcreateinvoke as-is).apps/erp/app/routes/api+/kanban.$id.tsx— same swap inside itsPromise.all.- Each keeps existing error handling / flash.
Task 7 — Repoint jobs call sites
packages/jobs/src/inngest/functions/tasks/recalculate.tsrecalculateJobMakeMethodRequirements— swap invoke →runLocationSchedulewithgetJobDatabaseClient()+getCarbonServiceRole().packages/jobs/src/inngest/functions/scheduled/schedule-inputs-changed.tswaveregen-${locationId}step — swap invoke →runLocationSchedule; keep theLocationRegenResultshape and the newly-late notification logic.- Wave still returns
{ jobsScheduled, conflictsDetected, newlyLate }.
Task 8 — Regenerate button UX
File: apps/erp/app/modules/production/ui/Schedule/ForecastHeader.tsx
- On fetcher completion, if
!successshow an error toast; keep the loading state. (Empty board no longer implies failure.) - Manual check.
Task 9 — Docs sync
- Update
apps/erp/app/modules/production/AGENTS.mdand.claude/rules/scheduling-data-structures.mdwhere they state scheduling runs "via thescheduleedge function" — note the app/jobs now run it in-process viarunLocationSchedule(@carbon/database/scheduling); the edge function remains as a thin wrapper.
Verification (gate)
-
pnpm exec turbo run typecheck --filter=@carbon/database -
pnpm exec turbo run typecheck --filter=erp -
pnpm exec turbo run typecheck --filter=@carbon/jobs -
pnpm --filter erp build(orturbo build --filter=erp) succeeds — no Node lib in the client bundle,.tsdeep-imports resolve. -
pnpm run testfor affected packages (scheduling unit tests unaffected).