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.

fix(scheduling): never reopen finished operations on a location regen (#1536)

Brad Barbin25e2b4c3ab39
diff --git a/.github/workflows/supabase.yml b/.github/workflows/supabase.yml
index 2681d048ee..d5dbb7a59d 100644
--- a/.github/workflows/supabase.yml
+++ b/.github/workflows/supabase.yml
@@ -22,10 +22,10 @@ jobs:
SUPABASE_AUTH_EXTERNAL_AZURE_REDIRECT_URI: ${{ secrets.SUPABASE_AUTH_EXTERNAL_AZURE_REDIRECT_URI }}
steps:
- - uses: actions/checkout@v4
+ - uses: actions/checkout@v5
- uses: pnpm/action-setup@v4
- - uses: actions/setup-node@v4
+ - uses: actions/setup-node@v5
with:
node-version: "22"
cache: pnpm
diff --git a/packages/ee/src/planning/scheduling/master-data-provider.ts b/packages/ee/src/planning/scheduling/master-data-provider.ts
index 471f0e97c6..12acc86b43 100644
--- a/packages/ee/src/planning/scheduling/master-data-provider.ts
+++ b/packages/ee/src/planning/scheduling/master-data-provider.ts
@@ -263,6 +263,7 @@ export class KyselyMasterDataProvider implements MasterDataProvider {
.leftJoin("location", "location.id", "job.locationId")
.select([
"job.id",
+ "job.status",
"job.dueDate",
"job.deadlineType",
"job.locationId",
diff --git a/packages/ee/src/planning/scheduling/scheduling-engine.ts b/packages/ee/src/planning/scheduling/scheduling-engine.ts
index 19bfa0911b..ecdab5aca0 100644
--- a/packages/ee/src/planning/scheduling/scheduling-engine.ts
+++ b/packages/ee/src/planning/scheduling/scheduling-engine.ts
@@ -69,6 +69,7 @@ import type {
SchedulingOptions,
SchedulingResult
} from "./types.ts";
+import { capacityHoldingJobStatuses } from "./types.ts";
import {
applyWorkCenterSelections,
type FiniteSchedulingContext,
@@ -469,15 +470,40 @@ export class SchedulingEngine {
}
});
- // Update operations with no dependencies to Ready status (outside the
- // rebuild txn so the advisory lock is held only for the delete/insert).
- for (const [opId, deps] of allDependencies) {
- if (deps.size === 0) {
- await this.db
- .updateTable("jobOperation")
- .set({ status: "Ready" })
- .where("id", "=", opId)
- .execute();
+ // Unblock dependency-free operations to Ready (outside the rebuild txn so
+ // the advisory lock is held only for the delete/insert). Two guards keep
+ // this from RE-OPENING work that is already finished or in flight:
+ //
+ // 1. Only OPEN jobs. A terminal job (Completed/Closed/Cancelled) or a
+ // pre-release Draft/Planned job is never re-opened by a regen. Open
+ // work is `capacityHoldingJobStatuses`; the batch loader already
+ // filters to these, so this is defense-in-depth for any direct caller.
+ // 2. Only resettable operation statuses. Never overwrite an operation
+ // that is Done/Canceled (finished) or In Progress/Paused (running) —
+ // only Ready/Waiting/Todo become Ready. Without this, a first op that
+ // an operator already completed was flipped back to Ready, and because
+ // `is_last_job_operation` requires EVERY op Done, the finished job
+ // could then never auto-complete (it sat stuck as open work).
+ const jobIsOpen =
+ this.job?.status != null &&
+ (capacityHoldingJobStatuses as readonly string[]).includes(
+ this.job.status
+ );
+ if (jobIsOpen) {
+ for (const [opId, deps] of allDependencies) {
+ if (deps.size === 0) {
+ await this.db
+ .updateTable("jobOperation")
+ .set({ status: "Ready" })
+ .where("id", "=", opId)
+ .where("status", "not in", [
+ "Done",
+ "Canceled",
+ "In Progress",
+ "Paused"
+ ])
+ .execute();
+ }
}
}
}
diff --git a/packages/ee/src/planning/scheduling/types.ts b/packages/ee/src/planning/scheduling/types.ts
index 5c2084a4d6..22bc7e9fb7 100644
--- a/packages/ee/src/planning/scheduling/types.ts
+++ b/packages/ee/src/planning/scheduling/types.ts
@@ -88,6 +88,11 @@ export type Operation = Omit<
export type Job = {
id?: string;
+ /**
+ * Job header status. Governs whether a scheduling run may (re)set this job's
+ * operation statuses — only `capacityHoldingJobStatuses` jobs are open work.
+ */
+ status?: Database["public"]["Enums"]["jobStatus"];
dueDate?: string | null;
deadlineType?: DeadlineType;
locationId?: string;