assignment-user-matcher 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +106 -1
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +19 -1
  5. package/dist/matcher.class.d.ts.map +1 -1
  6. package/dist/matcher.class.js +11 -0
  7. package/dist/scheduling/constraints/availability.d.ts +44 -0
  8. package/dist/scheduling/constraints/availability.d.ts.map +1 -0
  9. package/dist/scheduling/constraints/availability.js +142 -0
  10. package/dist/scheduling/constraints/consecutive.d.ts +25 -0
  11. package/dist/scheduling/constraints/consecutive.d.ts.map +1 -0
  12. package/dist/scheduling/constraints/consecutive.js +215 -0
  13. package/dist/scheduling/constraints/constraint.d.ts +35 -0
  14. package/dist/scheduling/constraints/constraint.d.ts.map +1 -0
  15. package/dist/scheduling/constraints/constraint.js +97 -0
  16. package/dist/scheduling/constraints/contract.d.ts +21 -0
  17. package/dist/scheduling/constraints/contract.d.ts.map +1 -0
  18. package/dist/scheduling/constraints/contract.js +106 -0
  19. package/dist/scheduling/constraints/cost.d.ts +18 -0
  20. package/dist/scheduling/constraints/cost.d.ts.map +1 -0
  21. package/dist/scheduling/constraints/cost.js +65 -0
  22. package/dist/scheduling/constraints/daily-rest.d.ts +22 -0
  23. package/dist/scheduling/constraints/daily-rest.d.ts.map +1 -0
  24. package/dist/scheduling/constraints/daily-rest.js +118 -0
  25. package/dist/scheduling/constraints/duty-quota.d.ts +20 -0
  26. package/dist/scheduling/constraints/duty-quota.d.ts.map +1 -0
  27. package/dist/scheduling/constraints/duty-quota.js +95 -0
  28. package/dist/scheduling/constraints/engagement-floor.d.ts +28 -0
  29. package/dist/scheduling/constraints/engagement-floor.d.ts.map +1 -0
  30. package/dist/scheduling/constraints/engagement-floor.js +79 -0
  31. package/dist/scheduling/constraints/fairness.d.ts +34 -0
  32. package/dist/scheduling/constraints/fairness.d.ts.map +1 -0
  33. package/dist/scheduling/constraints/fairness.js +149 -0
  34. package/dist/scheduling/constraints/group-composition.d.ts +19 -0
  35. package/dist/scheduling/constraints/group-composition.d.ts.map +1 -0
  36. package/dist/scheduling/constraints/group-composition.js +92 -0
  37. package/dist/scheduling/constraints/hour-budget.d.ts +13 -0
  38. package/dist/scheduling/constraints/hour-budget.d.ts.map +1 -0
  39. package/dist/scheduling/constraints/hour-budget.js +59 -0
  40. package/dist/scheduling/constraints/in-shift-breaks.d.ts +24 -0
  41. package/dist/scheduling/constraints/in-shift-breaks.d.ts.map +1 -0
  42. package/dist/scheduling/constraints/in-shift-breaks.js +97 -0
  43. package/dist/scheduling/constraints/intervals.d.ts +5 -0
  44. package/dist/scheduling/constraints/intervals.d.ts.map +1 -0
  45. package/dist/scheduling/constraints/intervals.js +17 -0
  46. package/dist/scheduling/constraints/max-shift-duration.d.ts +7 -0
  47. package/dist/scheduling/constraints/max-shift-duration.d.ts.map +1 -0
  48. package/dist/scheduling/constraints/max-shift-duration.js +38 -0
  49. package/dist/scheduling/constraints/min-rest.d.ts +22 -0
  50. package/dist/scheduling/constraints/min-rest.d.ts.map +1 -0
  51. package/dist/scheduling/constraints/min-rest.js +60 -0
  52. package/dist/scheduling/constraints/min-staffing.d.ts +14 -0
  53. package/dist/scheduling/constraints/min-staffing.d.ts.map +1 -0
  54. package/dist/scheduling/constraints/min-staffing.js +59 -0
  55. package/dist/scheduling/constraints/night-work.d.ts +24 -0
  56. package/dist/scheduling/constraints/night-work.d.ts.map +1 -0
  57. package/dist/scheduling/constraints/night-work.js +163 -0
  58. package/dist/scheduling/constraints/no-overlap.d.ts +7 -0
  59. package/dist/scheduling/constraints/no-overlap.d.ts.map +1 -0
  60. package/dist/scheduling/constraints/no-overlap.js +36 -0
  61. package/dist/scheduling/constraints/notice.d.ts +44 -0
  62. package/dist/scheduling/constraints/notice.d.ts.map +1 -0
  63. package/dist/scheduling/constraints/notice.js +140 -0
  64. package/dist/scheduling/constraints/one-shift-per-day.d.ts +7 -0
  65. package/dist/scheduling/constraints/one-shift-per-day.d.ts.map +1 -0
  66. package/dist/scheduling/constraints/one-shift-per-day.js +35 -0
  67. package/dist/scheduling/constraints/overtime.d.ts +47 -0
  68. package/dist/scheduling/constraints/overtime.d.ts.map +1 -0
  69. package/dist/scheduling/constraints/overtime.js +149 -0
  70. package/dist/scheduling/constraints/protections.d.ts +21 -0
  71. package/dist/scheduling/constraints/protections.d.ts.map +1 -0
  72. package/dist/scheduling/constraints/protections.js +93 -0
  73. package/dist/scheduling/constraints/qualification.d.ts +18 -0
  74. package/dist/scheduling/constraints/qualification.d.ts.map +1 -0
  75. package/dist/scheduling/constraints/qualification.js +78 -0
  76. package/dist/scheduling/constraints/rest-days.d.ts +30 -0
  77. package/dist/scheduling/constraints/rest-days.d.ts.map +1 -0
  78. package/dist/scheduling/constraints/rest-days.js +133 -0
  79. package/dist/scheduling/constraints/rolling-hours.d.ts +24 -0
  80. package/dist/scheduling/constraints/rolling-hours.d.ts.map +1 -0
  81. package/dist/scheduling/constraints/rolling-hours.js +147 -0
  82. package/dist/scheduling/constraints/start-interval.d.ts +20 -0
  83. package/dist/scheduling/constraints/start-interval.d.ts.map +1 -0
  84. package/dist/scheduling/constraints/start-interval.js +53 -0
  85. package/dist/scheduling/constraints/support.d.ts +48 -0
  86. package/dist/scheduling/constraints/support.d.ts.map +1 -0
  87. package/dist/scheduling/constraints/support.js +97 -0
  88. package/dist/scheduling/constraints/time-off.d.ts +7 -0
  89. package/dist/scheduling/constraints/time-off.d.ts.map +1 -0
  90. package/dist/scheduling/constraints/time-off.js +42 -0
  91. package/dist/scheduling/constraints/weekly-rest.d.ts +22 -0
  92. package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -0
  93. package/dist/scheduling/constraints/weekly-rest.js +92 -0
  94. package/dist/scheduling/cost.d.ts +56 -0
  95. package/dist/scheduling/cost.d.ts.map +1 -0
  96. package/dist/scheduling/cost.js +120 -0
  97. package/dist/scheduling/engine/construction.d.ts +22 -0
  98. package/dist/scheduling/engine/construction.d.ts.map +1 -0
  99. package/dist/scheduling/engine/construction.js +97 -0
  100. package/dist/scheduling/engine/explain.d.ts +12 -0
  101. package/dist/scheduling/engine/explain.d.ts.map +1 -0
  102. package/dist/scheduling/engine/explain.js +24 -0
  103. package/dist/scheduling/engine/objective.d.ts +73 -0
  104. package/dist/scheduling/engine/objective.d.ts.map +1 -0
  105. package/dist/scheduling/engine/objective.js +164 -0
  106. package/dist/scheduling/engine/prng.d.ts +5 -0
  107. package/dist/scheduling/engine/prng.d.ts.map +1 -0
  108. package/dist/scheduling/engine/prng.js +24 -0
  109. package/dist/scheduling/engine/propagation.d.ts +19 -0
  110. package/dist/scheduling/engine/propagation.d.ts.map +1 -0
  111. package/dist/scheduling/engine/propagation.js +40 -0
  112. package/dist/scheduling/engine/search.d.ts +50 -0
  113. package/dist/scheduling/engine/search.d.ts.map +1 -0
  114. package/dist/scheduling/engine/search.js +127 -0
  115. package/dist/scheduling/engine/state.d.ts +28 -0
  116. package/dist/scheduling/engine/state.d.ts.map +1 -0
  117. package/dist/scheduling/engine/state.js +96 -0
  118. package/dist/scheduling/engine/timeline.d.ts +179 -0
  119. package/dist/scheduling/engine/timeline.d.ts.map +1 -0
  120. package/dist/scheduling/engine/timeline.js +416 -0
  121. package/dist/scheduling/engine/verdicts.d.ts +18 -0
  122. package/dist/scheduling/engine/verdicts.d.ts.map +1 -0
  123. package/dist/scheduling/engine/verdicts.js +74 -0
  124. package/dist/scheduling/index.d.ts +10 -0
  125. package/dist/scheduling/index.d.ts.map +1 -0
  126. package/dist/scheduling/index.js +29 -0
  127. package/dist/scheduling/model.d.ts +53 -0
  128. package/dist/scheduling/model.d.ts.map +1 -0
  129. package/dist/scheduling/model.js +379 -0
  130. package/dist/scheduling/operations.d.ts +134 -0
  131. package/dist/scheduling/operations.d.ts.map +1 -0
  132. package/dist/scheduling/operations.js +376 -0
  133. package/dist/scheduling/provenance.d.ts +33 -0
  134. package/dist/scheduling/provenance.d.ts.map +1 -0
  135. package/dist/scheduling/provenance.js +70 -0
  136. package/dist/scheduling/rules.d.ts +23 -0
  137. package/dist/scheduling/rules.d.ts.map +1 -0
  138. package/dist/scheduling/rules.js +37 -0
  139. package/dist/scheduling/scheduler.class.d.ts +24 -0
  140. package/dist/scheduling/scheduler.class.d.ts.map +1 -0
  141. package/dist/scheduling/scheduler.class.js +150 -0
  142. package/dist/scheduling/time.d.ts +132 -0
  143. package/dist/scheduling/time.d.ts.map +1 -0
  144. package/dist/scheduling/time.js +256 -0
  145. package/dist/scheduling/types.d.ts +932 -0
  146. package/dist/scheduling/types.d.ts.map +1 -0
  147. package/dist/scheduling/types.js +19 -0
  148. package/dist/types/matcher.d.ts +15 -0
  149. package/dist/types/matcher.d.ts.map +1 -1
  150. package/package.json +4 -2
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Hour-budget — per-period worked-minutes bounds. Max is hard by default,
3
+ * min is soft by default (warn, don't block): an under-worked employee is a
4
+ * report, not a breach that should stall staffing.
5
+ *
6
+ * Durations are always true elapsed minutes — overnight shifts count forward
7
+ * into the next day, never `abs()`-wrapped like the legacy solver.
8
+ */
9
+ import type { ConstraintViolation, ModelContext, SchedulingConstraint, SearchState } from '../types';
10
+ export declare function hourBudget(): SchedulingConstraint;
11
+ /** Soft min-hours shortfalls of the current state. */
12
+ export declare function minHourViolations(ctx: ModelContext, state: SearchState, weight: number): ConstraintViolation[];
13
+ //# sourceMappingURL=hour-budget.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hour-budget.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/hour-budget.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAErG,wBAAgB,UAAU,IAAI,oBAAoB,CAwBjD;AAED,sDAAsD;AACtD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,mBAAmB,EAAE,CAgB9G"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ /**
3
+ * Hour-budget — per-period worked-minutes bounds. Max is hard by default,
4
+ * min is soft by default (warn, don't block): an under-worked employee is a
5
+ * report, not a breach that should stall staffing.
6
+ *
7
+ * Durations are always true elapsed minutes — overnight shifts count forward
8
+ * into the next day, never `abs()`-wrapped like the legacy solver.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.minHourViolations = exports.hourBudget = void 0;
12
+ function hourBudget() {
13
+ return {
14
+ id: 'hour-budget',
15
+ hardness: 'hard',
16
+ delta(state, pair) {
17
+ const employee = state.ctx.employeeById.get(pair.employeeId);
18
+ const inst = state.ctx.instanceById.get(pair.shiftInstanceId);
19
+ if (!employee || !inst)
20
+ return 0;
21
+ if (employee.maxHoursForPeriod === undefined)
22
+ return 0;
23
+ // Working minutes, not elapsed: `minutesByEmployee` accumulates
24
+ // working time, and the budget is documented as *worked* hours, so
25
+ // the candidate must contribute in the same unit — an unpaid break
26
+ // or a fractional duty must not be double-counted against it.
27
+ const current = state.minutesByEmployee.get(pair.employeeId) ?? 0;
28
+ return current + inst.workingMinutes > employee.maxHoursForPeriod * 60 ? 1 : 0;
29
+ },
30
+ explain(state, pair) {
31
+ const employee = state.ctx.employeeById.get(pair.employeeId);
32
+ if (this.delta(state, pair) > 0 && employee) {
33
+ return `assigning "${pair.shiftInstanceId}" pushes employee "${pair.employeeId}" past ${employee.maxHoursForPeriod}h for the period`;
34
+ }
35
+ return null;
36
+ },
37
+ };
38
+ }
39
+ exports.hourBudget = hourBudget;
40
+ /** Soft min-hours shortfalls of the current state. */
41
+ function minHourViolations(ctx, state, weight) {
42
+ const out = [];
43
+ for (const employee of ctx.employees) {
44
+ if (employee.minHoursForPeriod === undefined)
45
+ continue;
46
+ const worked = state.minutesByEmployee.get(employee.id) ?? 0;
47
+ const needed = employee.minHoursForPeriod * 60;
48
+ if (worked < needed) {
49
+ out.push({
50
+ constraintId: 'hour-budget',
51
+ severity: 'soft',
52
+ employeeId: employee.id,
53
+ message: `employee "${employee.id}" worked ${(worked / 60).toFixed(1)}h, below the ${employee.minHoursForPeriod}h minimum (weight ${weight})`,
54
+ });
55
+ }
56
+ }
57
+ return out;
58
+ }
59
+ exports.minHourViolations = minHourViolations;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * In-shift break entitlement — Directive 2003/88/EC Art 4.
3
+ *
4
+ * Art 4 fixes only the trigger — "where the working day is longer than six
5
+ * hours" — and leaves the duration to member states and collective agreements.
6
+ * The spread is wide: 10 minutes as an Italian floor, 15 in Spain and Belgium,
7
+ * 20 in France, 30 in Germany rising to 45 past nine hours, and a full one to
8
+ * two hours in Portugal, which splits the shift rather than pausing it.
9
+ *
10
+ * This engine assigns people to shifts rather than placing breaks inside them,
11
+ * so the rule is enforced against *shift design*: a template whose working span
12
+ * triggers an entitlement it does not carry is reported, once, rather than
13
+ * silently rostered. `ShiftTemplate.unpaidBreakMinutes` is how a template
14
+ * declares the break it provides.
15
+ *
16
+ * One trap worth encoding: C-107/19 holds that a break the worker must stay
17
+ * reachable through is working time *and* does not discharge the Art 4
18
+ * entitlement. An interruptible break therefore never satisfies the rule, no
19
+ * matter how long it is.
20
+ */
21
+ import type { BreakRule, SchedulingConstraint } from '../types';
22
+ export declare const BREAK_CITATION = "Directive 2003/88/EC Art 4";
23
+ export declare function inShiftBreaks(rules: BreakRule[]): SchedulingConstraint;
24
+ //# sourceMappingURL=in-shift-breaks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-shift-breaks.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/in-shift-breaks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAoC,oBAAoB,EAAiB,MAAM,UAAU,CAAC;AAGjH,eAAO,MAAM,cAAc,+BAA+B,CAAC;AAE3D,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,oBAAoB,CA2EtE"}
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ /**
3
+ * In-shift break entitlement — Directive 2003/88/EC Art 4.
4
+ *
5
+ * Art 4 fixes only the trigger — "where the working day is longer than six
6
+ * hours" — and leaves the duration to member states and collective agreements.
7
+ * The spread is wide: 10 minutes as an Italian floor, 15 in Spain and Belgium,
8
+ * 20 in France, 30 in Germany rising to 45 past nine hours, and a full one to
9
+ * two hours in Portugal, which splits the shift rather than pausing it.
10
+ *
11
+ * This engine assigns people to shifts rather than placing breaks inside them,
12
+ * so the rule is enforced against *shift design*: a template whose working span
13
+ * triggers an entitlement it does not carry is reported, once, rather than
14
+ * silently rostered. `ShiftTemplate.unpaidBreakMinutes` is how a template
15
+ * declares the break it provides.
16
+ *
17
+ * One trap worth encoding: C-107/19 holds that a break the worker must stay
18
+ * reachable through is working time *and* does not discharge the Art 4
19
+ * entitlement. An interruptible break therefore never satisfies the rule, no
20
+ * matter how long it is.
21
+ */
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.inShiftBreaks = exports.BREAK_CITATION = void 0;
24
+ const support_1 = require("./support");
25
+ exports.BREAK_CITATION = 'Directive 2003/88/EC Art 4';
26
+ function inShiftBreaks(rules) {
27
+ // Strictest applicable entitlement wins, so evaluate longest trigger first.
28
+ const ordered = [...rules].sort((a, b) => b.afterMinutes - a.afterMinutes);
29
+ const shortfall = (inst) => {
30
+ const applicable = ordered.find((r) => inst.workingMinutes > r.afterMinutes);
31
+ if (!applicable)
32
+ return null;
33
+ // An interruptible break provides nothing towards the entitlement
34
+ // (C-107/19). A `paid: true` entitlement is discharged only by declared
35
+ // paid break minutes — an unpaid break costs the worker wages, which is
36
+ // exactly what the paid entitlement exists to prevent.
37
+ const provided = applicable.interruptible
38
+ ? 0
39
+ : applicable.paid
40
+ ? inst.paidBreakMinutes
41
+ : inst.durationMinutes - inst.workingMinutes + inst.paidBreakMinutes;
42
+ if (provided >= applicable.minMinutes)
43
+ return null;
44
+ return { rule: applicable, provided };
45
+ };
46
+ const constraint = (0, support_1.fromVerdict)({
47
+ id: 'in-shift-breaks',
48
+ hardness: 'medium',
49
+ weight: 50,
50
+ citation: exports.BREAK_CITATION,
51
+ verdict(state, pair) {
52
+ const inst = (0, support_1.instanceOf)(state, pair);
53
+ if (!inst)
54
+ return (0, support_1.pass)('in-shift-breaks', 'unknown shift');
55
+ const missing = shortfall(inst);
56
+ if (!missing)
57
+ return (0, support_1.pass)('in-shift-breaks', 'break entitlement satisfied', { citation: exports.BREAK_CITATION });
58
+ const why = missing.rule.interruptible
59
+ ? 'an interruptible break is working time and does not discharge the entitlement (C-107/19)'
60
+ : `provides only ${(0, support_1.h)(missing.provided)}`;
61
+ return (0, support_1.fail)('in-shift-breaks', 'medium', `shift "${inst.id}" works ${(0, support_1.h)(inst.workingMinutes)} and owes a ${(0, support_1.h)(missing.rule.minMinutes)} break, but ${why}`, {
62
+ actual: missing.provided,
63
+ required: missing.rule.minMinutes,
64
+ unit: 'minutes',
65
+ citation: exports.BREAK_CITATION,
66
+ });
67
+ },
68
+ });
69
+ // The defect belongs to the template, not to whoever is rostered on it, so
70
+ // report it once per instance rather than once per assignee.
71
+ constraint.evaluate = (state) => {
72
+ const out = [];
73
+ for (const inst of state.ctx.instances) {
74
+ if ((state.assignments.get(inst.id)?.size ?? 0) === 0)
75
+ continue;
76
+ const missing = shortfall(inst);
77
+ if (!missing)
78
+ continue;
79
+ const why = missing.rule.interruptible
80
+ ? 'its break is interruptible, so it is working time and does not discharge the entitlement (C-107/19)'
81
+ : `provides only ${(0, support_1.h)(missing.provided)}`;
82
+ out.push({
83
+ constraintId: 'in-shift-breaks',
84
+ severity: 'medium',
85
+ shiftInstanceId: inst.id,
86
+ message: `shift "${inst.id}" works ${(0, support_1.h)(inst.workingMinutes)} and owes a ${(0, support_1.h)(missing.rule.minMinutes)} break after ${(0, support_1.h)(missing.rule.afterMinutes)}, but ${why}`,
87
+ actual: missing.provided,
88
+ required: missing.rule.minMinutes,
89
+ unit: 'minutes',
90
+ citation: exports.BREAK_CITATION,
91
+ });
92
+ }
93
+ return out;
94
+ };
95
+ return constraint;
96
+ }
97
+ exports.inShiftBreaks = inShiftBreaks;
@@ -0,0 +1,5 @@
1
+ /** Shared interval helpers for the built-in constraints. Minutes since period epoch, half-open [start, end). */
2
+ export declare function overlaps(aStart: number, aEnd: number, bStart: number, bEnd: number): boolean;
3
+ /** Minutes between the end of the earlier interval and the start of the later one; negative when they overlap. */
4
+ export declare function gapBetween(aStart: number, aEnd: number, bStart: number, bEnd: number): number;
5
+ //# sourceMappingURL=intervals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intervals.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/intervals.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAEhH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAE5F;AAED,kHAAkH;AAClH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAI7F"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ /** Shared interval helpers for the built-in constraints. Minutes since period epoch, half-open [start, end). */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.gapBetween = exports.overlaps = void 0;
5
+ function overlaps(aStart, aEnd, bStart, bEnd) {
6
+ return aStart < bEnd && bStart < aEnd;
7
+ }
8
+ exports.overlaps = overlaps;
9
+ /** Minutes between the end of the earlier interval and the start of the later one; negative when they overlap. */
10
+ function gapBetween(aStart, aEnd, bStart, bEnd) {
11
+ if (aEnd <= bStart)
12
+ return bStart - aEnd;
13
+ if (bEnd <= aStart)
14
+ return aStart - bEnd;
15
+ return -Math.min(aEnd, bEnd) + Math.max(aStart, bStart);
16
+ }
17
+ exports.gapBetween = gapBetween;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Max-shift-duration — a single shift never exceeds the employee's per-shift
3
+ * cap. Hard by default; prunes ineligible pairs before search.
4
+ */
5
+ import type { SchedulingConstraint } from '../types';
6
+ export declare function maxShiftDuration(): SchedulingConstraint;
7
+ //# sourceMappingURL=max-shift-duration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"max-shift-duration.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/max-shift-duration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAErD,wBAAgB,gBAAgB,IAAI,oBAAoB,CA4BvD"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /**
3
+ * Max-shift-duration — a single shift never exceeds the employee's per-shift
4
+ * cap. Hard by default; prunes ineligible pairs before search.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.maxShiftDuration = void 0;
8
+ function maxShiftDuration() {
9
+ const exceeds = (empMax, durationMinutes) => empMax !== undefined && durationMinutes > empMax;
10
+ return {
11
+ id: 'max-shift-duration',
12
+ hardness: 'hard',
13
+ prune(ctx, eligibility) {
14
+ for (const [employeeId, instances] of eligibility) {
15
+ const empMax = ctx.employeeById.get(employeeId)?.maxShiftDurationMinutes;
16
+ if (empMax === undefined)
17
+ continue;
18
+ for (const instanceId of [...instances]) {
19
+ const inst = ctx.instanceById.get(instanceId);
20
+ if (inst && exceeds(empMax, inst.durationMinutes))
21
+ instances.delete(instanceId);
22
+ }
23
+ }
24
+ },
25
+ delta(state, pair) {
26
+ const empMax = state.ctx.employeeById.get(pair.employeeId)?.maxShiftDurationMinutes;
27
+ const inst = state.ctx.instanceById.get(pair.shiftInstanceId);
28
+ return inst && exceeds(empMax, inst.durationMinutes) ? 1 : 0;
29
+ },
30
+ explain(state, pair) {
31
+ const empMax = state.ctx.employeeById.get(pair.employeeId)?.maxShiftDurationMinutes;
32
+ return this.delta(state, pair) > 0
33
+ ? `shift "${pair.shiftInstanceId}" exceeds employee "${pair.employeeId}" max shift duration of ${empMax} minutes`
34
+ : null;
35
+ },
36
+ };
37
+ }
38
+ exports.maxShiftDuration = maxShiftDuration;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Min-rest — a configurable minimum rest between the end of one assignment and
3
+ * the start of the next, across day boundaries. Hard by default.
4
+ *
5
+ * The default is 660 minutes (11h): the floor set by Article 3 of Directive
6
+ * 2003/88/EC. Member states may be stricter (ES and RO require 12h) and
7
+ * derogations may go lower (NL/DK/CZ 8h, FR 9h by collective agreement), so the
8
+ * value is always caller-supplied — the library ships the rule, never a
9
+ * jurisdiction's number.
10
+ *
11
+ * Scope note: this is a *general* inter-assignment rest rule, not a night-shift
12
+ * rule. It was called `rest-after-night` while it was a re-derivation of the
13
+ * legacy Python constraint, which compared `d_off > d` but then computed the
14
+ * gap from the *same* shift's start and end (so it never modeled rest between
15
+ * consecutive days) and hardcoded the shift name `"Night"`. Both bugs are gone;
16
+ * the name went with them.
17
+ */
18
+ import type { SchedulingConstraint } from '../types';
19
+ /** EU floor: Directive 2003/88/EC Article 3 — 11 consecutive hours per 24-hour period. */
20
+ export declare const DEFAULT_MIN_REST_MINUTES = 660;
21
+ export declare function minRest(minRestMinutes: number): SchedulingConstraint;
22
+ //# sourceMappingURL=min-rest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"min-rest.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/min-rest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAA+B,MAAM,UAAU,CAAC;AAElF,0FAA0F;AAC1F,eAAO,MAAM,wBAAwB,MAAM,CAAC;AAE5C,wBAAgB,OAAO,CAAC,cAAc,EAAE,MAAM,GAAG,oBAAoB,CAgCpE"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ /**
3
+ * Min-rest — a configurable minimum rest between the end of one assignment and
4
+ * the start of the next, across day boundaries. Hard by default.
5
+ *
6
+ * The default is 660 minutes (11h): the floor set by Article 3 of Directive
7
+ * 2003/88/EC. Member states may be stricter (ES and RO require 12h) and
8
+ * derogations may go lower (NL/DK/CZ 8h, FR 9h by collective agreement), so the
9
+ * value is always caller-supplied — the library ships the rule, never a
10
+ * jurisdiction's number.
11
+ *
12
+ * Scope note: this is a *general* inter-assignment rest rule, not a night-shift
13
+ * rule. It was called `rest-after-night` while it was a re-derivation of the
14
+ * legacy Python constraint, which compared `d_off > d` but then computed the
15
+ * gap from the *same* shift's start and end (so it never modeled rest between
16
+ * consecutive days) and hardcoded the shift name `"Night"`. Both bugs are gone;
17
+ * the name went with them.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.minRest = exports.DEFAULT_MIN_REST_MINUTES = void 0;
21
+ /** EU floor: Directive 2003/88/EC Article 3 — 11 consecutive hours per 24-hour period. */
22
+ exports.DEFAULT_MIN_REST_MINUTES = 660;
23
+ function minRest(minRestMinutes) {
24
+ const breach = (state, pair) => {
25
+ const inst = state.ctx.instanceById.get(pair.shiftInstanceId);
26
+ if (!inst)
27
+ return false;
28
+ const assigned = state.byEmployee.get(pair.employeeId);
29
+ if (!assigned)
30
+ return false;
31
+ for (const otherId of assigned) {
32
+ if (otherId === pair.shiftInstanceId)
33
+ continue;
34
+ const other = state.ctx.instanceById.get(otherId);
35
+ if (!other)
36
+ continue;
37
+ const gap = inst.startMinute >= other.endMinute
38
+ ? inst.startMinute - other.endMinute
39
+ : other.startMinute >= inst.endMinute
40
+ ? other.startMinute - inst.endMinute
41
+ : 0; // overlapping: no-overlap owns that, rest sees zero gap
42
+ if (gap < minRestMinutes)
43
+ return true;
44
+ }
45
+ return false;
46
+ };
47
+ return {
48
+ id: 'min-rest',
49
+ hardness: 'hard',
50
+ delta(state, pair) {
51
+ return breach(state, pair) ? 1 : 0;
52
+ },
53
+ explain(state, pair) {
54
+ return breach(state, pair)
55
+ ? `employee "${pair.employeeId}" would have less than ${(minRestMinutes / 60).toFixed(1)}h rest around "${pair.shiftInstanceId}"`
56
+ : null;
57
+ },
58
+ };
59
+ }
60
+ exports.minRest = minRest;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Min-staffing — every shift instance needs `minEmployees` assignees, plus
3
+ * per-tag minimums from `tagRequirements`. Hard by default.
4
+ *
5
+ * Assigning a pair can never *breach* a minimum, so `delta` is always 0; the
6
+ * rule's teeth are the per-instance shortfall evaluation in
7
+ * `staffingViolations` (used by the facade for the violation report) and the
8
+ * unfilled-slot penalty in the engine's objective.
9
+ */
10
+ import type { ConstraintViolation, ModelContext, SchedulingConstraint, SearchState, Severity } from '../types';
11
+ export declare function minStaffing(): SchedulingConstraint;
12
+ /** Per-instance staffing shortfalls of the current state — the reportable face of the rule. */
13
+ export declare function staffingViolations(ctx: ModelContext, state: SearchState, severity: Severity): ConstraintViolation[];
14
+ //# sourceMappingURL=min-staffing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"min-staffing.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/min-staffing.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,oBAAoB,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAE/G,wBAAgB,WAAW,IAAI,oBAAoB,CAOlD;AAED,+FAA+F;AAC/F,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,GAAG,mBAAmB,EAAE,CAkCnH"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ /**
3
+ * Min-staffing — every shift instance needs `minEmployees` assignees, plus
4
+ * per-tag minimums from `tagRequirements`. Hard by default.
5
+ *
6
+ * Assigning a pair can never *breach* a minimum, so `delta` is always 0; the
7
+ * rule's teeth are the per-instance shortfall evaluation in
8
+ * `staffingViolations` (used by the facade for the violation report) and the
9
+ * unfilled-slot penalty in the engine's objective.
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.staffingViolations = exports.minStaffing = void 0;
13
+ function minStaffing() {
14
+ return {
15
+ id: 'min-staffing',
16
+ hardness: 'hard',
17
+ delta: () => 0,
18
+ explain: () => null,
19
+ };
20
+ }
21
+ exports.minStaffing = minStaffing;
22
+ /** Per-instance staffing shortfalls of the current state — the reportable face of the rule. */
23
+ function staffingViolations(ctx, state, severity) {
24
+ const out = [];
25
+ for (const inst of ctx.instances) {
26
+ const assigned = state.assignments.get(inst.id);
27
+ const count = assigned ? assigned.size : 0;
28
+ if (count < inst.minEmployees) {
29
+ out.push({
30
+ constraintId: 'min-staffing',
31
+ severity,
32
+ shiftInstanceId: inst.id,
33
+ message: `shift "${inst.id}" needs ${inst.minEmployees} employees, has ${count}`,
34
+ actual: count,
35
+ required: inst.minEmployees,
36
+ unit: 'count',
37
+ });
38
+ }
39
+ for (const [tag, needed] of Object.entries(inst.tagRequirements)) {
40
+ let have = 0;
41
+ if (assigned) {
42
+ for (const employeeId of assigned) {
43
+ if (ctx.employeeTags.get(employeeId)?.has(tag))
44
+ have++;
45
+ }
46
+ }
47
+ if (have < needed) {
48
+ out.push({
49
+ constraintId: 'min-staffing',
50
+ severity,
51
+ shiftInstanceId: inst.id,
52
+ message: `shift "${inst.id}" needs ${needed} employees with tag "${tag}", has ${have}`,
53
+ });
54
+ }
55
+ }
56
+ }
57
+ return out;
58
+ }
59
+ exports.staffingViolations = staffingViolations;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Night work — Directive 2003/88/EC Art 2(3), 2(4) and 8.
3
+ *
4
+ * Art 8 has two limbs that behave differently and are routinely conflated:
5
+ *
6
+ * - **8(a)** caps a night worker's normal hours at an *average* of eight per
7
+ * 24h, over a reference period national law sets (the Directive fixes no
8
+ * ceiling on it);
9
+ * - **8(b)** caps night work involving *special hazards or heavy physical or
10
+ * mental strain* at eight hours in any 24h — **absolute, no averaging**.
11
+ *
12
+ * So the same eight-hour figure is a soft average for most workers and a hard
13
+ * per-day ceiling for some, selected per person by a `hazardousNight`
14
+ * protection. Getting that backwards under-protects exactly the people the
15
+ * provision exists for.
16
+ *
17
+ * The night band itself is national and, in Poland, employer-chosen: it ranges
18
+ * from 20:00–06:00 (BE) to 00:00–07:00 (IE), subject to the Art 2(3) floor of
19
+ * at least seven hours including 00:00–05:00.
20
+ */
21
+ import type { NightWorkRule, SchedulingConstraint } from '../types';
22
+ export declare const NIGHT_WORK_CITATION = "Directive 2003/88/EC Art 8";
23
+ export declare function nightWork(rule: NightWorkRule): SchedulingConstraint;
24
+ //# sourceMappingURL=night-work.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"night-work.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/night-work.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAe,oBAAoB,EAAe,MAAM,UAAU,CAAC;AAI9F,eAAO,MAAM,mBAAmB,+BAA+B,CAAC;AAEhE,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,GAAG,oBAAoB,CAmInE"}
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ /**
3
+ * Night work — Directive 2003/88/EC Art 2(3), 2(4) and 8.
4
+ *
5
+ * Art 8 has two limbs that behave differently and are routinely conflated:
6
+ *
7
+ * - **8(a)** caps a night worker's normal hours at an *average* of eight per
8
+ * 24h, over a reference period national law sets (the Directive fixes no
9
+ * ceiling on it);
10
+ * - **8(b)** caps night work involving *special hazards or heavy physical or
11
+ * mental strain* at eight hours in any 24h — **absolute, no averaging**.
12
+ *
13
+ * So the same eight-hour figure is a soft average for most workers and a hard
14
+ * per-day ceiling for some, selected per person by a `hazardousNight`
15
+ * protection. Getting that backwards under-protects exactly the people the
16
+ * provision exists for.
17
+ *
18
+ * The night band itself is national and, in Poland, employer-chosen: it ranges
19
+ * from 20:00–06:00 (BE) to 00:00–07:00 (IE), subject to the Art 2(3) floor of
20
+ * at least seven hours including 00:00–05:00.
21
+ */
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.nightWork = exports.NIGHT_WORK_CITATION = void 0;
24
+ const support_1 = require("./support");
25
+ exports.NIGHT_WORK_CITATION = 'Directive 2003/88/EC Art 8';
26
+ function nightWork(rule) {
27
+ return (0, support_1.fromVerdict)({
28
+ id: 'night-work',
29
+ hardness: 'hard',
30
+ weight: 1,
31
+ citation: exports.NIGHT_WORK_CITATION,
32
+ verdict(state, pair) {
33
+ const inst = (0, support_1.instanceOf)(state, pair);
34
+ if (!inst)
35
+ return (0, support_1.pass)('night-work', 'unknown shift');
36
+ const clock = state.ctx.clock;
37
+ const range = (0, support_1.rangeOf)(inst);
38
+ // Outright bans first: 94/33/EC bars adolescents from 00:00-04:00
39
+ // regardless of how the rest of the rule is configured.
40
+ for (const banned of rule.prohibitedRanges ?? []) {
41
+ const inside = clock.minutesInClockRange(range, banned);
42
+ if (inside > 0) {
43
+ return (0, support_1.fail)('night-work', 'hard', `employee "${pair.employeeId}" may not work ${banned.from}-${banned.to}; "${inst.id}" overlaps it by ${(0, support_1.h)(inside)}`, { actual: inside, required: 0, unit: 'minutes', citation: exports.NIGHT_WORK_CITATION });
44
+ }
45
+ }
46
+ const timelineForHazard = (0, support_1.timelineFor)(state, pair.employeeId);
47
+ const hazardProbe = {
48
+ ...range,
49
+ id: (0, support_1.entryIdOf)(pair),
50
+ workingMinutes: inst.workingMinutes,
51
+ tag: inst.shiftTypeTag,
52
+ };
53
+ // Art 8(b) is checked before the night-shift gate on purpose. The cap
54
+ // is on a hazardous night worker's total work in any 24 hours, so a
55
+ // day shift stacked onto the same rolling day breaches it just as a
56
+ // second night would — gating on "is this shift itself a night shift"
57
+ // would let exactly that combination through.
58
+ if (isHazardous(state, pair.employeeId, rule) && rule.maxShiftMinutes !== undefined) {
59
+ const breach = timelineForHazard.withEntry(hazardProbe, (t) => {
60
+ const worstDay = t.maxWorkingMinutesInAnyWindow(support_1.MINUTES_PER_DAY, {
61
+ start: range.start - support_1.MINUTES_PER_DAY,
62
+ end: range.end + support_1.MINUTES_PER_DAY,
63
+ });
64
+ return worstDay > rule.maxShiftMinutes ? worstDay : null;
65
+ });
66
+ if (breach !== null) {
67
+ return (0, support_1.fail)('night-work', 'hard', `employee "${pair.employeeId}" does hazardous night work and would reach ${(0, support_1.h)(breach)} in a 24h window, over the absolute ${(0, support_1.h)(rule.maxShiftMinutes)} cap`, {
68
+ actual: breach,
69
+ required: rule.maxShiftMinutes,
70
+ unit: 'minutes',
71
+ citation: 'Directive 2003/88/EC Art 8(b)',
72
+ });
73
+ }
74
+ }
75
+ if (!inst.isNightShift) {
76
+ return (0, support_1.pass)('night-work', `"${inst.id}" is not a night shift`, { citation: exports.NIGHT_WORK_CITATION });
77
+ }
78
+ if (rule.maxShiftMinutes !== undefined && inst.workingMinutes > rule.maxShiftMinutes) {
79
+ return (0, support_1.fail)('night-work', 'hard', `night shift "${inst.id}" is ${(0, support_1.h)(inst.workingMinutes)}, over the ${(0, support_1.h)(rule.maxShiftMinutes)} night limit`, {
80
+ actual: inst.workingMinutes,
81
+ required: rule.maxShiftMinutes,
82
+ unit: 'minutes',
83
+ citation: exports.NIGHT_WORK_CITATION,
84
+ });
85
+ }
86
+ const timeline = (0, support_1.timelineFor)(state, pair.employeeId);
87
+ const probe = {
88
+ ...range,
89
+ id: (0, support_1.entryIdOf)(pair),
90
+ workingMinutes: inst.workingMinutes,
91
+ tag: inst.shiftTypeTag,
92
+ };
93
+ return timeline.withEntry(probe, (t) => {
94
+ // Art 8(a): the averaged limit, for night workers generally.
95
+ if (rule.averageWindowDays !== undefined && rule.maxShiftMinutes !== undefined) {
96
+ const span = rule.averageWindowDays * support_1.MINUTES_PER_DAY;
97
+ const worst = t.maxWorkingMinutesInAnyWindow(span, {
98
+ start: range.start - span,
99
+ end: range.end + span,
100
+ });
101
+ const allowance = rule.maxShiftMinutes * rule.averageWindowDays;
102
+ if (worst > allowance) {
103
+ return (0, support_1.fail)('night-work', 'hard', `employee "${pair.employeeId}" would average over ${(0, support_1.h)(rule.maxShiftMinutes)} of night work per 24h across ${rule.averageWindowDays} days`, {
104
+ actual: worst,
105
+ required: allowance,
106
+ unit: 'minutes',
107
+ citation: 'Directive 2003/88/EC Art 8(a)',
108
+ });
109
+ }
110
+ }
111
+ for (const quota of rule.volumeQuotas ?? []) {
112
+ const span = quota.windowDays * support_1.MINUTES_PER_DAY;
113
+ const count = worstNightCount(state, t, quota, span, range);
114
+ if (count > quota.max) {
115
+ const label = quota.label ?? `${quota.max} night shifts / ${quota.windowDays}d`;
116
+ return (0, support_1.fail)('night-work', 'hard', `employee "${pair.employeeId}" would reach ${count} qualifying night shifts in a ${quota.windowDays}-day window, over the ${label} quota`, { actual: count, required: quota.max, unit: 'count', citation: exports.NIGHT_WORK_CITATION });
117
+ }
118
+ }
119
+ return (0, support_1.pass)('night-work', 'within every configured night-work limit', { citation: exports.NIGHT_WORK_CITATION });
120
+ });
121
+ },
122
+ });
123
+ }
124
+ exports.nightWork = nightWork;
125
+ /** Whether this person is subject to the Art 8(b) absolute cap. */
126
+ function isHazardous(state, employeeId, rule) {
127
+ if (rule.absoluteWhenHazardous === false)
128
+ return false;
129
+ const employee = state.ctx.employeeById.get(employeeId);
130
+ return (employee?.protections ?? []).some((p) => p.kind === 'hazardousNight');
131
+ }
132
+ /**
133
+ * Worst count of qualifying night shifts over any window of `span`.
134
+ *
135
+ * `endingAfter` narrows the count to shifts finishing past a wall-clock time —
136
+ * the Netherlands counts only those ending after 02:00 towards its 36-per-16-week
137
+ * quota, so a 22:00–01:00 shift does not consume the allowance.
138
+ */
139
+ function worstNightCount(state, timeline, quota, span, around) {
140
+ const clock = state.ctx.clock;
141
+ const qualifies = (entry) => {
142
+ const inst = findInstance(state, entry);
143
+ if (!inst?.isNightShift)
144
+ return false;
145
+ if (!quota.endingAfter)
146
+ return true;
147
+ const day = clock.dayIndexOfMinute(entry.end);
148
+ const threshold = clock.toPeriodMinutes(clock.dateAt(day), quota.endingAfter, 'nightWork.volumeQuotas.endingAfter');
149
+ return entry.end > threshold;
150
+ };
151
+ let worst = 0;
152
+ for (let start = around.start - span; start <= around.end; start += support_1.MINUTES_PER_DAY) {
153
+ const count = timeline.countIn({ start, end: start + span }, qualifies);
154
+ if (count > worst)
155
+ worst = count;
156
+ }
157
+ return worst;
158
+ }
159
+ /** Recover the shift instance behind a timeline entry, if it is one. */
160
+ function findInstance(state, entry) {
161
+ const instanceId = entry.id.includes('@@') ? entry.id.slice(entry.id.indexOf('@@') + 2) : entry.id;
162
+ return state.ctx.instanceById.get(instanceId);
163
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * No-overlap — an employee's assignments must never overlap in time, including
3
+ * across midnight for overnight shifts. Hard by default.
4
+ */
5
+ import type { SchedulingConstraint } from '../types';
6
+ export declare function noOverlap(): SchedulingConstraint;
7
+ //# sourceMappingURL=no-overlap.d.ts.map