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,179 @@
1
+ /**
2
+ * Person timeline — the data structure the EU rule families are built on.
3
+ *
4
+ * Almost every working-time rule of consequence is a question about a *window*
5
+ * of one person's timeline rather than about a single assignment:
6
+ *
7
+ * - "≤48h averaged over any 4 months" → worked minutes in a window
8
+ * - "11h rest in every rolling 24h" → longest gap in a window
9
+ * - "35h continuous rest per 7 days" → longest gap in a window
10
+ * - "≤36 shifts ending after 02:00 / 16wks" → filtered count in a window
11
+ * - "max 5 consecutive nights" → run length over entries
12
+ * - "max 6 consecutive working days" → run length over day indices
13
+ *
14
+ * So they all reduce to a handful of primitives here, and a constraint that
15
+ * needs one of them costs O(log n + hits) rather than a scan of the roster.
16
+ *
17
+ * Two design notes that matter:
18
+ *
19
+ * 1. **Keyed by person, not by contract.** CJEU C-585/19 holds that where a
20
+ * worker has several contracts with the same employer, daily rest applies to
21
+ * them taken as a whole. Aggregating per employee record would understate
22
+ * hours and overstate rest for exactly the people most at risk.
23
+ * 2. **History is first-class.** Rest and weekly-rest windows straddle the
24
+ * period boundary, so a roster built without the previous period's tail is
25
+ * non-compliant on its first days by construction. Historical entries are
26
+ * seeded at negative period minutes and are immutable.
27
+ */
28
+ import type { MinuteRange } from '../time';
29
+ /** One occupied span on a person's timeline. */
30
+ export interface TimelineEntry extends MinuteRange {
31
+ /** Shift instance id, or a synthetic id for historical//absence spans. */
32
+ id: string;
33
+ /** Minutes that count as working time — may differ from the span for standby duties. */
34
+ workingMinutes: number;
35
+ /** Free-form classification tag used by sequence rules (e.g. a shift type). */
36
+ tag?: string;
37
+ /** Historical entries are immutable and never returned by the mutable views. */
38
+ historical?: boolean;
39
+ }
40
+ /**
41
+ * One person's ordered, non-overlapping-by-construction spans, with prefix sums
42
+ * for O(log n) window queries.
43
+ *
44
+ * Entries are kept sorted by start. The prefix array is rebuilt lazily, so a
45
+ * burst of assign/unassign calls during a search move costs one rebuild at the
46
+ * next query rather than one per mutation.
47
+ */
48
+ export declare class PersonTimeline {
49
+ private entries;
50
+ /** `prefix[i]` = total working minutes of entries `[0, i)`. */
51
+ private prefix;
52
+ /** `prefixMaxEnd[i]` = latest end among entries `[0, i)`; nondecreasing. */
53
+ private prefixMaxEnd;
54
+ private dirty;
55
+ constructor(history?: TimelineEntry[]);
56
+ /** Every entry, history first, ordered by start. */
57
+ all(): readonly TimelineEntry[];
58
+ /** Total working minutes currently on the timeline, history included. */
59
+ totalWorkingMinutes(): number;
60
+ add(entry: TimelineEntry): void;
61
+ remove(id: string): boolean;
62
+ has(id: string): boolean;
63
+ /**
64
+ * Working minutes falling inside `window`, counting partial overlaps
65
+ * proportionally. This is the primitive behind every rolling average.
66
+ *
67
+ * Costs a binary search plus one step per overlapping entry. A person's
68
+ * timeline holds tens of entries per period, so scanning the hits beats
69
+ * maintaining a minute-resolution index — a Fenwick tree over period
70
+ * minutes would be ~45k slots per person, 22M across a 500-person solve,
71
+ * to save a walk over a few dozen items.
72
+ */
73
+ workingMinutesIn(window: MinuteRange): number;
74
+ /**
75
+ * Worst case of "working minutes in any window of `windowMinutes`" that
76
+ * overlaps `bounds` — the quantity a rolling average such as "≤48h in any
77
+ * 4-month window" actually limits.
78
+ *
79
+ * Checking windows on a fixed calendar grid is the classic way to get this
80
+ * wrong: a person can sit under 48h in every Mon–Sun week while working 60h
81
+ * across a Thursday-to-Wednesday stretch. The maximum is always attained by
82
+ * a window whose left edge sits on an entry boundary, so those are the only
83
+ * candidates worth evaluating.
84
+ */
85
+ maxWorkingMinutesInAnyWindow(windowMinutes: number, bounds: MinuteRange): number;
86
+ /**
87
+ * Worst case of "longest continuous rest in any window of `windowMinutes`"
88
+ * overlapping `bounds` — what a weekly-rest rule requires a floor on.
89
+ *
90
+ * Returns `Infinity` when no candidate window applies, so an empty timeline
91
+ * never reads as a breach.
92
+ */
93
+ minLongestRestInAnyWindow(windowMinutes: number, bounds: MinuteRange): number;
94
+ /**
95
+ * Window left edges worth testing: every entry boundary that could put a
96
+ * window over `bounds`, plus the bounds themselves. Sliding a window until
97
+ * its edge meets a boundary never reduces the extreme being sought, so this
98
+ * finite set contains the answer.
99
+ */
100
+ private candidateWindowStarts;
101
+ /** Entries intersecting `window`, in order. */
102
+ entriesIn(window: MinuteRange): TimelineEntry[];
103
+ /**
104
+ * Run `fn` with `entry` temporarily on the timeline, then restore it.
105
+ *
106
+ * Constraints are asked "what would assigning this pair cost", and the pair
107
+ * is not on the timeline yet — so window rules have to measure a
108
+ * hypothetical. Adding and removing around the call beats copying the
109
+ * timeline, which would allocate on every candidate evaluation in the
110
+ * search's hot loop.
111
+ *
112
+ * Idempotent: when the entry is already present (re-explaining a committed
113
+ * roster) `fn` simply runs against the timeline as it stands.
114
+ */
115
+ withEntry<T>(entry: TimelineEntry, fn: (timeline: PersonTimeline) => T): T;
116
+ /**
117
+ * Every free stretch inside `window`, in minutes, including the stretches at
118
+ * the window edges. Weekly-rest averaging needs the individual gaps, not
119
+ * just the longest one.
120
+ */
121
+ restGapsIn(window: MinuteRange): number[];
122
+ /**
123
+ * Longest uninterrupted free stretch inside `window`, in minutes.
124
+ *
125
+ * Gaps at the window edges count: a person with no assignments in a 7-day
126
+ * window has 7 days of rest, and a weekly-rest rule must see that.
127
+ */
128
+ longestRestIn(window: MinuteRange): number;
129
+ /**
130
+ * Smallest gap between `range` and any existing entry, ignoring `excludeId`.
131
+ * Returns `Infinity` when the timeline is otherwise empty, and 0 when
132
+ * something overlaps — overlap is a different rule's business.
133
+ */
134
+ minGapAround(range: MinuteRange, excludeId?: string): number;
135
+ /** Number of entries in `window` satisfying `predicate`. */
136
+ countIn(window: MinuteRange, predicate?: (entry: TimelineEntry) => boolean): number;
137
+ /**
138
+ * Longest run of consecutive entries satisfying `predicate`, where
139
+ * "consecutive" means adjacent in time order with no non-matching entry
140
+ * between them. Backs `max consecutive night shifts`.
141
+ */
142
+ longestRun(predicate: (entry: TimelineEntry) => boolean): number;
143
+ /**
144
+ * Longest run of consecutive day indices on which the person has any entry.
145
+ * Backs `max consecutive working days`. `dayIndexOf` maps a period minute to
146
+ * a day so the caller's clock owns DST, not this structure.
147
+ */
148
+ longestConsecutiveDays(dayIndexOf: (minute: number) => number): number;
149
+ /**
150
+ * First index that may overlap a window starting at `windowStart`.
151
+ *
152
+ * Entries are sorted by start but may be *nested* — a long stand-by span can
153
+ * fully contain a later-starting shift — so walking back from the lower
154
+ * bound while only the immediately preceding entry reaches past the window
155
+ * start misses the outer span. The running maximum of entry ends is
156
+ * nondecreasing, so the first index whose prefix max-end exceeds the window
157
+ * start is exactly where iteration must begin, found by binary search.
158
+ */
159
+ private firstOverlapping;
160
+ private ensureClean;
161
+ private rebuild;
162
+ }
163
+ /**
164
+ * All timelines for a solve, keyed by person id.
165
+ *
166
+ * The engine owns mutation; constraints only read. Keeping the index here
167
+ * rather than on `SearchState` means a constraint never has to reconstruct a
168
+ * person's history to answer a window question.
169
+ */
170
+ export declare class TimelineIndex {
171
+ private readonly byPerson;
172
+ constructor(history?: Map<string, TimelineEntry[]>);
173
+ /** The person's timeline, created empty on first use. */
174
+ for(personId: string): PersonTimeline;
175
+ add(personId: string, entry: TimelineEntry): void;
176
+ remove(personId: string, entryId: string): boolean;
177
+ personIds(): string[];
178
+ }
179
+ //# sourceMappingURL=timeline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeline.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/timeline.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAG3C,gDAAgD;AAChD,MAAM,WAAW,aAAc,SAAQ,WAAW;IAC9C,0EAA0E;IAC1E,EAAE,EAAE,MAAM,CAAC;IACX,wFAAwF;IACxF,cAAc,EAAE,MAAM,CAAC;IACvB,+EAA+E;IAC/E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,gFAAgF;IAChF,UAAU,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;GAOG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,OAAO,CAAuB;IACtC,+DAA+D;IAC/D,OAAO,CAAC,MAAM,CAAiB;IAC/B,4EAA4E;IAC5E,OAAO,CAAC,YAAY,CAAyB;IAC7C,OAAO,CAAC,KAAK,CAAS;gBAEV,OAAO,GAAE,aAAa,EAAO;IAMzC,oDAAoD;IACpD,GAAG,IAAI,SAAS,aAAa,EAAE;IAK/B,yEAAyE;IACzE,mBAAmB,IAAI,MAAM;IAK7B,GAAG,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI;IAQ/B,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAQ3B,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAIxB;;;;;;;;;OASG;IACH,gBAAgB,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM;IAc7C;;;;;;;;;;OAUG;IACH,4BAA4B,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,MAAM;IAWhF;;;;;;OAMG;IACH,yBAAyB,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,MAAM;IAW7E;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IAU7B,+CAA+C;IAC/C,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,aAAa,EAAE;IAW/C;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE,cAAc,KAAK,CAAC,GAAG,CAAC;IAU1E;;;;OAIG;IACH,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,EAAE;IAezC;;;;;OAKG;IACH,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM;IAe1C;;;;OAIG;IACH,YAAY,CAAC,KAAK,EAAE,WAAW,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM;IAY5D,4DAA4D;IAC5D,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,GAAG,MAAM;IAKnF;;;;OAIG;IACH,UAAU,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,GAAG,MAAM;IAehE;;;;OAIG;IACH,sBAAsB,CAAC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM;IAiBtE;;;;;;;;;OASG;IACH,OAAO,CAAC,gBAAgB;IAWxB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,OAAO;CAWlB;AAED;;;;;;GAMG;AACH,qBAAa,aAAa;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqC;gBAElD,OAAO,GAAE,GAAG,CAAC,MAAM,EAAE,aAAa,EAAE,CAAa;IAM7D,yDAAyD;IACzD,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc;IASrC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,IAAI;IAIjD,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO;IAIlD,SAAS,IAAI,MAAM,EAAE;CAGxB"}
@@ -0,0 +1,416 @@
1
+ "use strict";
2
+ /**
3
+ * Person timeline — the data structure the EU rule families are built on.
4
+ *
5
+ * Almost every working-time rule of consequence is a question about a *window*
6
+ * of one person's timeline rather than about a single assignment:
7
+ *
8
+ * - "≤48h averaged over any 4 months" → worked minutes in a window
9
+ * - "11h rest in every rolling 24h" → longest gap in a window
10
+ * - "35h continuous rest per 7 days" → longest gap in a window
11
+ * - "≤36 shifts ending after 02:00 / 16wks" → filtered count in a window
12
+ * - "max 5 consecutive nights" → run length over entries
13
+ * - "max 6 consecutive working days" → run length over day indices
14
+ *
15
+ * So they all reduce to a handful of primitives here, and a constraint that
16
+ * needs one of them costs O(log n + hits) rather than a scan of the roster.
17
+ *
18
+ * Two design notes that matter:
19
+ *
20
+ * 1. **Keyed by person, not by contract.** CJEU C-585/19 holds that where a
21
+ * worker has several contracts with the same employer, daily rest applies to
22
+ * them taken as a whole. Aggregating per employee record would understate
23
+ * hours and overstate rest for exactly the people most at risk.
24
+ * 2. **History is first-class.** Rest and weekly-rest windows straddle the
25
+ * period boundary, so a roster built without the previous period's tail is
26
+ * non-compliant on its first days by construction. Historical entries are
27
+ * seeded at negative period minutes and are immutable.
28
+ */
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.TimelineIndex = exports.PersonTimeline = void 0;
31
+ const time_1 = require("../time");
32
+ /**
33
+ * One person's ordered, non-overlapping-by-construction spans, with prefix sums
34
+ * for O(log n) window queries.
35
+ *
36
+ * Entries are kept sorted by start. The prefix array is rebuilt lazily, so a
37
+ * burst of assign/unassign calls during a search move costs one rebuild at the
38
+ * next query rather than one per mutation.
39
+ */
40
+ class PersonTimeline {
41
+ constructor(history = []) {
42
+ this.entries = [];
43
+ /** `prefix[i]` = total working minutes of entries `[0, i)`. */
44
+ this.prefix = [0];
45
+ /** `prefixMaxEnd[i]` = latest end among entries `[0, i)`; nondecreasing. */
46
+ this.prefixMaxEnd = [-Infinity];
47
+ this.dirty = false;
48
+ for (const entry of history)
49
+ this.entries.push({ ...entry, historical: true });
50
+ this.entries.sort(byStart);
51
+ this.rebuild();
52
+ }
53
+ /** Every entry, history first, ordered by start. */
54
+ all() {
55
+ this.ensureClean();
56
+ return this.entries;
57
+ }
58
+ /** Total working minutes currently on the timeline, history included. */
59
+ totalWorkingMinutes() {
60
+ this.ensureClean();
61
+ return this.prefix[this.prefix.length - 1];
62
+ }
63
+ add(entry) {
64
+ // Insert in place; timelines are short (tens of entries per period), so a
65
+ // splice beats re-sorting the whole array.
66
+ const at = lowerBound(this.entries, entry.start);
67
+ this.entries.splice(at, 0, entry);
68
+ this.dirty = true;
69
+ }
70
+ remove(id) {
71
+ const index = this.entries.findIndex((e) => e.id === id && !e.historical);
72
+ if (index === -1)
73
+ return false;
74
+ this.entries.splice(index, 1);
75
+ this.dirty = true;
76
+ return true;
77
+ }
78
+ has(id) {
79
+ return this.entries.some((e) => e.id === id);
80
+ }
81
+ /**
82
+ * Working minutes falling inside `window`, counting partial overlaps
83
+ * proportionally. This is the primitive behind every rolling average.
84
+ *
85
+ * Costs a binary search plus one step per overlapping entry. A person's
86
+ * timeline holds tens of entries per period, so scanning the hits beats
87
+ * maintaining a minute-resolution index — a Fenwick tree over period
88
+ * minutes would be ~45k slots per person, 22M across a 500-person solve,
89
+ * to save a walk over a few dozen items.
90
+ */
91
+ workingMinutesIn(window) {
92
+ this.ensureClean();
93
+ if (window.end <= window.start || this.entries.length === 0)
94
+ return 0;
95
+ const first = this.firstOverlapping(window.start);
96
+ let total = 0;
97
+ for (let i = first; i < this.entries.length; i++) {
98
+ const entry = this.entries[i];
99
+ if (entry.start >= window.end)
100
+ break;
101
+ total += proratedWorkingMinutes(entry, window);
102
+ }
103
+ return total;
104
+ }
105
+ /**
106
+ * Worst case of "working minutes in any window of `windowMinutes`" that
107
+ * overlaps `bounds` — the quantity a rolling average such as "≤48h in any
108
+ * 4-month window" actually limits.
109
+ *
110
+ * Checking windows on a fixed calendar grid is the classic way to get this
111
+ * wrong: a person can sit under 48h in every Mon–Sun week while working 60h
112
+ * across a Thursday-to-Wednesday stretch. The maximum is always attained by
113
+ * a window whose left edge sits on an entry boundary, so those are the only
114
+ * candidates worth evaluating.
115
+ */
116
+ maxWorkingMinutesInAnyWindow(windowMinutes, bounds) {
117
+ this.ensureClean();
118
+ if (windowMinutes <= 0)
119
+ return 0;
120
+ let worst = 0;
121
+ for (const start of this.candidateWindowStarts(windowMinutes, bounds)) {
122
+ const total = this.workingMinutesIn({ start, end: start + windowMinutes });
123
+ if (total > worst)
124
+ worst = total;
125
+ }
126
+ return worst;
127
+ }
128
+ /**
129
+ * Worst case of "longest continuous rest in any window of `windowMinutes`"
130
+ * overlapping `bounds` — what a weekly-rest rule requires a floor on.
131
+ *
132
+ * Returns `Infinity` when no candidate window applies, so an empty timeline
133
+ * never reads as a breach.
134
+ */
135
+ minLongestRestInAnyWindow(windowMinutes, bounds) {
136
+ this.ensureClean();
137
+ if (windowMinutes <= 0)
138
+ return Infinity;
139
+ let worst = Infinity;
140
+ for (const start of this.candidateWindowStarts(windowMinutes, bounds)) {
141
+ const rest = this.longestRestIn({ start, end: start + windowMinutes });
142
+ if (rest < worst)
143
+ worst = rest;
144
+ }
145
+ return worst;
146
+ }
147
+ /**
148
+ * Window left edges worth testing: every entry boundary that could put a
149
+ * window over `bounds`, plus the bounds themselves. Sliding a window until
150
+ * its edge meets a boundary never reduces the extreme being sought, so this
151
+ * finite set contains the answer.
152
+ */
153
+ candidateWindowStarts(windowMinutes, bounds) {
154
+ const starts = new Set([bounds.start, bounds.end - windowMinutes]);
155
+ for (const entry of this.entries) {
156
+ if (entry.end < bounds.start - windowMinutes || entry.start > bounds.end + windowMinutes)
157
+ continue;
158
+ starts.add(entry.start);
159
+ starts.add(entry.end - windowMinutes);
160
+ }
161
+ return [...starts];
162
+ }
163
+ /** Entries intersecting `window`, in order. */
164
+ entriesIn(window) {
165
+ this.ensureClean();
166
+ const out = [];
167
+ for (let i = this.firstOverlapping(window.start); i < this.entries.length; i++) {
168
+ const entry = this.entries[i];
169
+ if (entry.start >= window.end)
170
+ break;
171
+ if (entry.end > window.start)
172
+ out.push(entry);
173
+ }
174
+ return out;
175
+ }
176
+ /**
177
+ * Run `fn` with `entry` temporarily on the timeline, then restore it.
178
+ *
179
+ * Constraints are asked "what would assigning this pair cost", and the pair
180
+ * is not on the timeline yet — so window rules have to measure a
181
+ * hypothetical. Adding and removing around the call beats copying the
182
+ * timeline, which would allocate on every candidate evaluation in the
183
+ * search's hot loop.
184
+ *
185
+ * Idempotent: when the entry is already present (re-explaining a committed
186
+ * roster) `fn` simply runs against the timeline as it stands.
187
+ */
188
+ withEntry(entry, fn) {
189
+ if (this.has(entry.id))
190
+ return fn(this);
191
+ this.add(entry);
192
+ try {
193
+ return fn(this);
194
+ }
195
+ finally {
196
+ this.remove(entry.id);
197
+ }
198
+ }
199
+ /**
200
+ * Every free stretch inside `window`, in minutes, including the stretches at
201
+ * the window edges. Weekly-rest averaging needs the individual gaps, not
202
+ * just the longest one.
203
+ */
204
+ restGapsIn(window) {
205
+ if (window.end <= window.start)
206
+ return [];
207
+ const busy = this.entriesIn(window);
208
+ if (busy.length === 0)
209
+ return [window.end - window.start];
210
+ const gaps = [];
211
+ let cursor = window.start;
212
+ for (const entry of busy) {
213
+ if (entry.start > cursor)
214
+ gaps.push(entry.start - cursor);
215
+ cursor = Math.max(cursor, entry.end);
216
+ }
217
+ if (window.end > cursor)
218
+ gaps.push(window.end - cursor);
219
+ return gaps;
220
+ }
221
+ /**
222
+ * Longest uninterrupted free stretch inside `window`, in minutes.
223
+ *
224
+ * Gaps at the window edges count: a person with no assignments in a 7-day
225
+ * window has 7 days of rest, and a weekly-rest rule must see that.
226
+ */
227
+ longestRestIn(window) {
228
+ if (window.end <= window.start)
229
+ return 0;
230
+ const busy = this.entriesIn(window);
231
+ if (busy.length === 0)
232
+ return window.end - window.start;
233
+ let longest = 0;
234
+ let cursor = window.start;
235
+ for (const entry of busy) {
236
+ if (entry.start > cursor)
237
+ longest = Math.max(longest, entry.start - cursor);
238
+ cursor = Math.max(cursor, entry.end);
239
+ }
240
+ if (window.end > cursor)
241
+ longest = Math.max(longest, window.end - cursor);
242
+ return longest;
243
+ }
244
+ /**
245
+ * Smallest gap between `range` and any existing entry, ignoring `excludeId`.
246
+ * Returns `Infinity` when the timeline is otherwise empty, and 0 when
247
+ * something overlaps — overlap is a different rule's business.
248
+ */
249
+ minGapAround(range, excludeId) {
250
+ this.ensureClean();
251
+ let smallest = Infinity;
252
+ for (const entry of this.entries) {
253
+ if (entry.id === excludeId)
254
+ continue;
255
+ if (entry.end <= range.start)
256
+ smallest = Math.min(smallest, range.start - entry.end);
257
+ else if (entry.start >= range.end)
258
+ smallest = Math.min(smallest, entry.start - range.end);
259
+ else
260
+ return 0;
261
+ }
262
+ return smallest;
263
+ }
264
+ /** Number of entries in `window` satisfying `predicate`. */
265
+ countIn(window, predicate) {
266
+ const hits = this.entriesIn(window);
267
+ return predicate ? hits.filter(predicate).length : hits.length;
268
+ }
269
+ /**
270
+ * Longest run of consecutive entries satisfying `predicate`, where
271
+ * "consecutive" means adjacent in time order with no non-matching entry
272
+ * between them. Backs `max consecutive night shifts`.
273
+ */
274
+ longestRun(predicate) {
275
+ this.ensureClean();
276
+ let longest = 0;
277
+ let current = 0;
278
+ for (const entry of this.entries) {
279
+ if (predicate(entry)) {
280
+ current++;
281
+ if (current > longest)
282
+ longest = current;
283
+ }
284
+ else {
285
+ current = 0;
286
+ }
287
+ }
288
+ return longest;
289
+ }
290
+ /**
291
+ * Longest run of consecutive day indices on which the person has any entry.
292
+ * Backs `max consecutive working days`. `dayIndexOf` maps a period minute to
293
+ * a day so the caller's clock owns DST, not this structure.
294
+ */
295
+ longestConsecutiveDays(dayIndexOf) {
296
+ this.ensureClean();
297
+ const days = new Set();
298
+ for (const entry of this.entries)
299
+ days.add(dayIndexOf(entry.start));
300
+ const sorted = [...days].sort((a, b) => a - b);
301
+ let longest = 0;
302
+ let current = 0;
303
+ let previous;
304
+ for (const day of sorted) {
305
+ current = previous !== undefined && day === previous + 1 ? current + 1 : 1;
306
+ if (current > longest)
307
+ longest = current;
308
+ previous = day;
309
+ }
310
+ return longest;
311
+ }
312
+ /**
313
+ * First index that may overlap a window starting at `windowStart`.
314
+ *
315
+ * Entries are sorted by start but may be *nested* — a long stand-by span can
316
+ * fully contain a later-starting shift — so walking back from the lower
317
+ * bound while only the immediately preceding entry reaches past the window
318
+ * start misses the outer span. The running maximum of entry ends is
319
+ * nondecreasing, so the first index whose prefix max-end exceeds the window
320
+ * start is exactly where iteration must begin, found by binary search.
321
+ */
322
+ firstOverlapping(windowStart) {
323
+ let low = 0;
324
+ let high = this.entries.length;
325
+ while (low < high) {
326
+ const mid = (low + high) >> 1;
327
+ if (this.prefixMaxEnd[mid + 1] > windowStart)
328
+ high = mid;
329
+ else
330
+ low = mid + 1;
331
+ }
332
+ return low;
333
+ }
334
+ ensureClean() {
335
+ if (this.dirty)
336
+ this.rebuild();
337
+ }
338
+ rebuild() {
339
+ this.prefix = new Array(this.entries.length + 1);
340
+ this.prefix[0] = 0;
341
+ this.prefixMaxEnd = new Array(this.entries.length + 1);
342
+ this.prefixMaxEnd[0] = -Infinity;
343
+ for (let i = 0; i < this.entries.length; i++) {
344
+ this.prefix[i + 1] = this.prefix[i] + this.entries[i].workingMinutes;
345
+ this.prefixMaxEnd[i + 1] = Math.max(this.prefixMaxEnd[i], this.entries[i].end);
346
+ }
347
+ this.dirty = false;
348
+ }
349
+ }
350
+ exports.PersonTimeline = PersonTimeline;
351
+ /**
352
+ * All timelines for a solve, keyed by person id.
353
+ *
354
+ * The engine owns mutation; constraints only read. Keeping the index here
355
+ * rather than on `SearchState` means a constraint never has to reconstruct a
356
+ * person's history to answer a window question.
357
+ */
358
+ class TimelineIndex {
359
+ constructor(history = new Map()) {
360
+ this.byPerson = new Map();
361
+ for (const [personId, entries] of history) {
362
+ this.byPerson.set(personId, new PersonTimeline(entries));
363
+ }
364
+ }
365
+ /** The person's timeline, created empty on first use. */
366
+ for(personId) {
367
+ let timeline = this.byPerson.get(personId);
368
+ if (!timeline) {
369
+ timeline = new PersonTimeline();
370
+ this.byPerson.set(personId, timeline);
371
+ }
372
+ return timeline;
373
+ }
374
+ add(personId, entry) {
375
+ this.for(personId).add(entry);
376
+ }
377
+ remove(personId, entryId) {
378
+ return this.for(personId).remove(entryId);
379
+ }
380
+ personIds() {
381
+ return [...this.byPerson.keys()];
382
+ }
383
+ }
384
+ exports.TimelineIndex = TimelineIndex;
385
+ function byStart(a, b) {
386
+ return a.start - b.start || a.end - b.end;
387
+ }
388
+ /** First index whose entry starts at or after `start`. */
389
+ function lowerBound(entries, start) {
390
+ let low = 0;
391
+ let high = entries.length;
392
+ while (low < high) {
393
+ const mid = (low + high) >> 1;
394
+ if (entries[mid].start < start)
395
+ low = mid + 1;
396
+ else
397
+ high = mid;
398
+ }
399
+ return low;
400
+ }
401
+ /**
402
+ * Working minutes of `entry` attributable to `window`.
403
+ *
404
+ * Standby duties can have `workingMinutes` below their span (a duty that counts
405
+ * at a fraction, or only for actual call-outs), so a partial overlap is scaled
406
+ * by the same fraction rather than clipped to elapsed time.
407
+ */
408
+ function proratedWorkingMinutes(entry, window) {
409
+ const span = entry.end - entry.start;
410
+ if (span <= 0)
411
+ return 0;
412
+ const inside = (0, time_1.overlapMinutes)(entry, window);
413
+ if (inside === span)
414
+ return entry.workingMinutes;
415
+ return (entry.workingMinutes * inside) / span;
416
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Verdict collection — the single place a roster is judged.
3
+ *
4
+ * Both the solver's result assembly and `checkCompliance` run through here.
5
+ * That is deliberate: an earlier version had the solver report only aggregate
6
+ * violations while the compliance check evaluated every pair, so a solve could
7
+ * announce "no hard violations" on a roster the compliance check then rejected.
8
+ * Two judgement paths always drift, and the one that drifts is the one nobody
9
+ * is reading at the time.
10
+ */
11
+ import type { AssignmentPair, ConstraintViolation, RuleVerdict, SearchState } from '../types';
12
+ /** Every rule's verdict on one pair, in registry order. */
13
+ export declare function verdictsFor(state: SearchState, pair: AssignmentPair): RuleVerdict[];
14
+ /** Breaches attributable to individual assignments in the committed roster. */
15
+ export declare function collectPairViolations(state: SearchState): ConstraintViolation[];
16
+ /** Breaches only a whole-roster view can see: coverage, quotas, team fairness. */
17
+ export declare function collectAggregateViolations(state: SearchState): ConstraintViolation[];
18
+ //# sourceMappingURL=verdicts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verdicts.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/verdicts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE9F,2DAA2D;AAC3D,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,cAAc,GAAG,WAAW,EAAE,CAoBnF;AAED,+EAA+E;AAC/E,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,WAAW,GAAG,mBAAmB,EAAE,CAqB/E;AAED,kFAAkF;AAClF,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,WAAW,GAAG,mBAAmB,EAAE,CASpF"}