assignment-user-matcher 1.16.1 → 1.22.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 (153) hide show
  1. package/README.md +290 -8
  2. package/dist/escalation/policy.d.ts +11 -0
  3. package/dist/escalation/policy.d.ts.map +1 -1
  4. package/dist/escalation/policy.js +22 -1
  5. package/dist/forecasting/backtest.d.ts +52 -0
  6. package/dist/forecasting/backtest.d.ts.map +1 -0
  7. package/dist/forecasting/backtest.js +195 -0
  8. package/dist/forecasting/calendar.d.ts +44 -0
  9. package/dist/forecasting/calendar.d.ts.map +1 -0
  10. package/dist/forecasting/calendar.js +85 -0
  11. package/dist/forecasting/cover.d.ts +69 -0
  12. package/dist/forecasting/cover.d.ts.map +1 -0
  13. package/dist/forecasting/cover.js +103 -0
  14. package/dist/forecasting/days.d.ts +17 -0
  15. package/dist/forecasting/days.d.ts.map +1 -0
  16. package/dist/forecasting/days.js +35 -0
  17. package/dist/forecasting/distributions.d.ts +31 -0
  18. package/dist/forecasting/distributions.d.ts.map +1 -0
  19. package/dist/forecasting/distributions.js +154 -0
  20. package/dist/forecasting/effects.d.ts +88 -0
  21. package/dist/forecasting/effects.d.ts.map +1 -0
  22. package/dist/forecasting/effects.js +303 -0
  23. package/dist/forecasting/erlang.d.ts +31 -0
  24. package/dist/forecasting/erlang.d.ts.map +1 -0
  25. package/dist/forecasting/erlang.js +86 -0
  26. package/dist/forecasting/forecast.d.ts +7 -0
  27. package/dist/forecasting/forecast.d.ts.map +1 -0
  28. package/dist/forecasting/forecast.js +221 -0
  29. package/dist/forecasting/index.d.ts +18 -0
  30. package/dist/forecasting/index.d.ts.map +1 -0
  31. package/dist/forecasting/index.js +54 -0
  32. package/dist/forecasting/model.d.ts +145 -0
  33. package/dist/forecasting/model.d.ts.map +1 -0
  34. package/dist/forecasting/model.js +583 -0
  35. package/dist/forecasting/nowcast.d.ts +26 -0
  36. package/dist/forecasting/nowcast.d.ts.map +1 -0
  37. package/dist/forecasting/nowcast.js +92 -0
  38. package/dist/forecasting/simulation.d.ts +91 -0
  39. package/dist/forecasting/simulation.d.ts.map +1 -0
  40. package/dist/forecasting/simulation.js +478 -0
  41. package/dist/forecasting/spikes.d.ts +6 -0
  42. package/dist/forecasting/spikes.d.ts.map +1 -0
  43. package/dist/forecasting/spikes.js +73 -0
  44. package/dist/forecasting/staffing.d.ts +64 -0
  45. package/dist/forecasting/staffing.d.ts.map +1 -0
  46. package/dist/forecasting/staffing.js +765 -0
  47. package/dist/forecasting/stats.d.ts +11 -0
  48. package/dist/forecasting/stats.d.ts.map +1 -0
  49. package/dist/forecasting/stats.js +45 -0
  50. package/dist/forecasting/types.d.ts +299 -0
  51. package/dist/forecasting/types.d.ts.map +1 -0
  52. package/dist/forecasting/types.js +8 -0
  53. package/dist/index.d.ts +3 -2
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +8 -1
  56. package/dist/learning/auto-weights.d.ts +1 -5
  57. package/dist/learning/auto-weights.d.ts.map +1 -1
  58. package/dist/learning/auto-weights.js +0 -3
  59. package/dist/lua/book-slot.lua +84 -0
  60. package/dist/lua/claim-queued.lua +47 -0
  61. package/dist/managers/DecisionTraceManager.d.ts +15 -0
  62. package/dist/managers/DecisionTraceManager.d.ts.map +1 -1
  63. package/dist/managers/DecisionTraceManager.js +117 -11
  64. package/dist/managers/LearningManager.d.ts +12 -0
  65. package/dist/managers/LearningManager.d.ts.map +1 -1
  66. package/dist/managers/LearningManager.js +21 -7
  67. package/dist/managers/ReliabilityManager.d.ts +1 -2
  68. package/dist/managers/ReliabilityManager.d.ts.map +1 -1
  69. package/dist/managers/ReliabilityManager.js +1 -1
  70. package/dist/managers/TelemetryManager.d.ts.map +1 -1
  71. package/dist/managers/TelemetryManager.js +9 -3
  72. package/dist/managers/WorkflowManager.d.ts.map +1 -1
  73. package/dist/managers/WorkflowManager.js +3 -4
  74. package/dist/matcher.class.d.ts +319 -4
  75. package/dist/matcher.class.d.ts.map +1 -1
  76. package/dist/matcher.class.js +1428 -102
  77. package/dist/queries/lane-stats.d.ts +51 -0
  78. package/dist/queries/lane-stats.d.ts.map +1 -0
  79. package/dist/queries/lane-stats.js +78 -0
  80. package/dist/schedule/recurrence.d.ts +11 -0
  81. package/dist/schedule/recurrence.d.ts.map +1 -1
  82. package/dist/schedule/recurrence.js +19 -2
  83. package/dist/schedule/slot.d.ts +60 -0
  84. package/dist/schedule/slot.d.ts.map +1 -0
  85. package/dist/schedule/slot.js +86 -0
  86. package/dist/scheduling/constraints/availability.d.ts +2 -14
  87. package/dist/scheduling/constraints/availability.d.ts.map +1 -1
  88. package/dist/scheduling/constraints/availability.js +14 -15
  89. package/dist/scheduling/constraints/employee-scope.d.ts.map +1 -1
  90. package/dist/scheduling/constraints/employee-scope.js +10 -2
  91. package/dist/scheduling/constraints/support.d.ts +4 -8
  92. package/dist/scheduling/constraints/support.d.ts.map +1 -1
  93. package/dist/scheduling/constraints/support.js +23 -2
  94. package/dist/scheduling/constraints/weekly-rest.d.ts +28 -0
  95. package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -1
  96. package/dist/scheduling/constraints/weekly-rest.js +108 -21
  97. package/dist/scheduling/engine/construction.d.ts.map +1 -1
  98. package/dist/scheduling/engine/construction.js +33 -4
  99. package/dist/scheduling/engine/objective.d.ts +0 -1
  100. package/dist/scheduling/engine/objective.d.ts.map +1 -1
  101. package/dist/scheduling/engine/objective.js +45 -3
  102. package/dist/scheduling/engine/search.d.ts +4 -0
  103. package/dist/scheduling/engine/search.d.ts.map +1 -1
  104. package/dist/scheduling/engine/search.js +2 -1
  105. package/dist/scheduling/engine/state.d.ts +14 -0
  106. package/dist/scheduling/engine/state.d.ts.map +1 -1
  107. package/dist/scheduling/engine/state.js +7 -0
  108. package/dist/scheduling/engine/timeline.d.ts +9 -1
  109. package/dist/scheduling/engine/timeline.d.ts.map +1 -1
  110. package/dist/scheduling/engine/timeline.js +52 -27
  111. package/dist/scheduling/engine/verdicts.d.ts +40 -1
  112. package/dist/scheduling/engine/verdicts.d.ts.map +1 -1
  113. package/dist/scheduling/engine/verdicts.js +177 -17
  114. package/dist/scheduling/index.d.ts +3 -2
  115. package/dist/scheduling/index.d.ts.map +1 -1
  116. package/dist/scheduling/index.js +7 -1
  117. package/dist/scheduling/model.d.ts +2 -3
  118. package/dist/scheduling/model.d.ts.map +1 -1
  119. package/dist/scheduling/model.js +30 -12
  120. package/dist/scheduling/operations.d.ts +174 -2
  121. package/dist/scheduling/operations.d.ts.map +1 -1
  122. package/dist/scheduling/operations.js +513 -15
  123. package/dist/scheduling/preference-report.d.ts +15 -0
  124. package/dist/scheduling/preference-report.d.ts.map +1 -0
  125. package/dist/scheduling/preference-report.js +111 -0
  126. package/dist/scheduling/preferences.d.ts +30 -0
  127. package/dist/scheduling/preferences.d.ts.map +1 -0
  128. package/dist/scheduling/preferences.js +85 -0
  129. package/dist/scheduling/resilience.d.ts +46 -0
  130. package/dist/scheduling/resilience.d.ts.map +1 -0
  131. package/dist/scheduling/resilience.js +45 -0
  132. package/dist/scheduling/scheduler.class.d.ts +12 -1
  133. package/dist/scheduling/scheduler.class.d.ts.map +1 -1
  134. package/dist/scheduling/scheduler.class.js +28 -4
  135. package/dist/scheduling/time.d.ts +28 -6
  136. package/dist/scheduling/time.d.ts.map +1 -1
  137. package/dist/scheduling/time.js +59 -17
  138. package/dist/scheduling/types.d.ts +107 -1
  139. package/dist/scheduling/types.d.ts.map +1 -1
  140. package/dist/scoring/fair-arbitration.d.ts.map +1 -1
  141. package/dist/scoring/fair-arbitration.js +2 -1
  142. package/dist/tracing/decision-trace.d.ts +5 -0
  143. package/dist/tracing/decision-trace.d.ts.map +1 -1
  144. package/dist/tracing/decision-trace.js +3 -0
  145. package/dist/types/matcher.d.ts +369 -2
  146. package/dist/types/matcher.d.ts.map +1 -1
  147. package/dist/updates/assignment-update.d.ts +93 -0
  148. package/dist/updates/assignment-update.d.ts.map +1 -0
  149. package/dist/updates/assignment-update.js +91 -0
  150. package/dist/utils/keys.d.ts +10 -0
  151. package/dist/utils/keys.d.ts.map +1 -1
  152. package/dist/utils/keys.js +43 -0
  153. package/package.json +15 -2
@@ -18,7 +18,7 @@
18
18
  * `minMinutes` and both are checked.
19
19
  */
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.weeklyRest = exports.WEEKLY_REST_CITATION = void 0;
21
+ exports.worstQualifyingRest = exports.qualifyingRestAt = exports.weeklyRest = exports.WEEKLY_REST_CITATION = void 0;
22
22
  const support_1 = require("./support");
23
23
  exports.WEEKLY_REST_CITATION = 'Directive 2003/88/EC Art 5';
24
24
  function weeklyRest(rule) {
@@ -53,7 +53,13 @@ function weeklyRest(rule) {
53
53
  // they would have had from each short window.
54
54
  const windows = averagingMinutes / windowMinutes;
55
55
  const required = rule.minMinutes * windows;
56
- const worstAveraged = totalQualifyingRest(t, averagingMinutes, bounds, rule.minMinutes);
56
+ // Every averaging window that contains the shift, exactly.
57
+ // Probing a sample of window starts let two checks see
58
+ // different windows: construction added a shift that broke
59
+ // a window it never probed, and the final judgement of an
60
+ // earlier shift in that window then reported a breach the
61
+ // solve had created itself.
62
+ const worstAveraged = worstQualifyingRest(t, averagingMinutes, range.start - averagingMinutes + 1, range.end - 1, rule.minMinutes);
57
63
  if (worstAveraged < required) {
58
64
  return (0, support_1.fail)('weekly-rest', 'hard', `employee "${pair.employeeId}" would average ${(0, support_1.h)(worstAveraged / windows)} weekly rest over ${rule.averageOverDays} days, below the ${(0, support_1.h)(rule.minMinutes)} average`, { actual: worstAveraged, required, unit: 'minutes', citation: exports.WEEKLY_REST_CITATION });
59
65
  }
@@ -69,35 +75,116 @@ function weeklyRest(rule) {
69
75
  }
70
76
  exports.weeklyRest = weeklyRest;
71
77
  /**
72
- * Total rest, counting only stretches long enough to qualify, in the worst
73
- * averaging window.
78
+ * Rest that counts towards an averaged weekly-rest requirement in the window
79
+ * `[start, start + averagingMinutes)`: each free stretch contributes whole
80
+ * multiples of `minPer`, so two 30h gaps are worth nothing against a 48h
81
+ * requirement and one 100h gap is worth 96h.
82
+ */
83
+ function qualifyingRestAt(timeline, start, averagingMinutes, minPer) {
84
+ let total = 0;
85
+ for (const gap of timeline.restGapsIn({ start, end: start + averagingMinutes })) {
86
+ if (gap >= minPer)
87
+ total += minPer * Math.floor(gap / minPer);
88
+ }
89
+ return total;
90
+ }
91
+ exports.qualifyingRestAt = qualifyingRestAt;
92
+ /**
93
+ * The least qualifying rest in any window starting at an integer minute in
94
+ * `[lo, hi]` — exactly, not over a sample of starts.
74
95
  *
75
- * Averaging counts *rest periods*, not idle minutes: eight scattered hours are
76
- * not half a weekly rest. Only unbroken stretches of at least `minPer` count,
77
- * and each contributes at most one qualifying period's worth.
96
+ * The quantity is a step function of the window start, and its minima are
97
+ * not only where an edge meets a shift boundary: they also sit where the
98
+ * leading or trailing gap crosses a multiple of `minPer`. So the starts are
99
+ * split into segments on which the window's structure is fixed — the same
100
+ * shifts intersect it, and the same edges are free — using every boundary at
101
+ * which that can change. Inside a segment the value is
102
+ *
103
+ * inner + m·⌊(a − s)/m⌋ [leading gap] + m·⌊(s + A − b)/m⌋ [trailing gap]
104
+ *
105
+ * which is falling in `s` with only a leading gap (least at the segment's
106
+ * end), rising with only a trailing one (least at its start), and with both
107
+ * dips one step below `m·⌊C/m⌋` exactly where `(a − s) mod m` exceeds
108
+ * `C mod m`, `C = a + A − b` — the first such start is the only other
109
+ * candidate. Each candidate is then measured directly.
78
110
  */
79
- function totalQualifyingRest(timeline, averagingMinutes, bounds, minPer) {
80
- // Window starts anchored on entry boundaries, never a day grid (a grid
81
- // silently misses straddling windows — see CLAUDE.md). The worst (least
82
- // rest) windows are the ones aligning their edges with work, so probe
83
- // every start where a window edge touches an entry boundary.
84
- const starts = new Set([bounds.start, bounds.end]);
85
- for (const entry of timeline.entriesIn({ start: bounds.start, end: bounds.end + averagingMinutes })) {
86
- for (const s of [entry.start, entry.end, entry.start - averagingMinutes + 1, entry.end - averagingMinutes]) {
87
- if (s >= bounds.start && s <= bounds.end)
88
- starts.add(s);
111
+ function worstQualifyingRest(timeline, averagingMinutes, lo, hi, minPer) {
112
+ if (hi < lo)
113
+ return Infinity;
114
+ const A = averagingMinutes;
115
+ const entries = timeline.entriesIn({ start: lo, end: hi + A });
116
+ const cuts = new Set([lo, hi + 1]);
117
+ for (const e of entries) {
118
+ for (const at of [e.start, e.end, e.start - A + 1, e.end - A + 1]) {
119
+ if (at > lo && at <= hi)
120
+ cuts.add(at);
89
121
  }
90
122
  }
123
+ const sorted = [...cuts].sort((x, y) => x - y);
91
124
  let worst = Infinity;
92
- for (const start of starts) {
93
- const window = { start, end: start + averagingMinutes };
125
+ // `qualifyingRestAt` over the entries already in hand, without building
126
+ // the gap list: this runs for every candidate the search judges.
127
+ const measure = (s) => {
128
+ const end = s + A;
94
129
  let total = 0;
95
- for (const gap of timeline.restGapsIn(window)) {
130
+ let cursor = s;
131
+ for (const e of entries) {
132
+ if (e.start >= end)
133
+ break;
134
+ if (e.end <= s)
135
+ continue;
136
+ if (e.start > cursor) {
137
+ const gap = e.start - cursor;
138
+ if (gap >= minPer)
139
+ total += minPer * Math.floor(gap / minPer);
140
+ }
141
+ if (e.end > cursor)
142
+ cursor = e.end;
143
+ }
144
+ if (end > cursor) {
145
+ const gap = end - cursor;
96
146
  if (gap >= minPer)
97
147
  total += minPer * Math.floor(gap / minPer);
98
148
  }
99
149
  if (total < worst)
100
150
  worst = total;
151
+ };
152
+ for (let i = 0; i + 1 < sorted.length; i++) {
153
+ const p = sorted[i];
154
+ const q = sorted[i + 1] - 1;
155
+ measure(p);
156
+ if (q !== p)
157
+ measure(q);
158
+ if (q - p < 2 || !(minPer > 0))
159
+ continue;
160
+ // Both edges free throughout the segment? Then the dip, if any.
161
+ let a = Infinity;
162
+ let b = -Infinity;
163
+ for (const e of entries) {
164
+ if (e.start >= p + A)
165
+ break;
166
+ if (e.end <= p)
167
+ continue;
168
+ if (e.start < a)
169
+ a = e.start;
170
+ if (e.end > b)
171
+ b = e.end;
172
+ }
173
+ if (a === Infinity)
174
+ continue;
175
+ if (!(a > q && p + A > b))
176
+ continue;
177
+ const C = a + A - b;
178
+ const r = ((C % minPer) + minPer) % minPer;
179
+ if (r >= minPer - 1)
180
+ continue;
181
+ // u = a − s runs from a − q up to a − p; the first u with residue r + 1.
182
+ const uLo = a - q;
183
+ const target = r + 1;
184
+ const u = uLo + ((((target - uLo) % minPer) + minPer) % minPer);
185
+ if (u <= a - p)
186
+ measure(a - u);
101
187
  }
102
- return worst === Infinity ? 0 : worst;
188
+ return worst;
103
189
  }
190
+ exports.worstQualifyingRest = worstQualifyingRest;
@@ -1 +1 @@
1
- {"version":3,"file":"construction.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/construction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAGrD,wEAAwE;AACxE,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAKtH;AAmFD,gGAAgG;AAChG,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,YAAY,EAAE,WAAW,EAAE,iBAAiB,GAAG,MAAM,EAAE,CAS7F;AAwDD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAC1B,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,IAAI,EAAE,MAAM,MAAM,GACnB,IAAI,CA0CN;AAKD;;;;GAIG;AACH,wBAAgB,UAAU,CACtB,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,IAAI,EAAE,MAAM,MAAM,EAClB,WAAW,CAAC,EAAE,MAAM,EAAE,GACvB,IAAI,CA6BN"}
1
+ {"version":3,"file":"construction.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/construction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAIrD,wEAAwE;AACxE,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAuBtH;AA8FD,gGAAgG;AAChG,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,YAAY,EAAE,WAAW,EAAE,iBAAiB,GAAG,MAAM,EAAE,CAS7F;AAwDD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAC1B,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,IAAI,EAAE,MAAM,MAAM,GACnB,IAAI,CA0CN;AAKD;;;;GAIG;AACH,wBAAgB,UAAU,CACtB,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,IAAI,EAAE,MAAM,MAAM,EAClB,WAAW,CAAC,EAAE,MAAM,EAAE,GACvB,IAAI,CA6BN"}
@@ -20,15 +20,44 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.greedyFill = exports.fillToContract = exports.constructionOrder = exports.hardCompliant = void 0;
21
21
  const state_1 = require("./state");
22
22
  const support_1 = require("../constraints/support");
23
+ const verdicts_1 = require("./verdicts");
23
24
  /** Whether assigning the pair keeps every hard constraint satisfied. */
24
25
  function hardCompliant(ctx, state, employeeId, instanceId) {
25
- for (const c of ctx.constraints) {
26
- if (c.hardness === 'hard' && c.delta(state, { employeeId, shiftInstanceId: instanceId }) > 0)
27
- return false;
26
+ const pair = { employeeId, shiftInstanceId: instanceId };
27
+ // Rules are asked cheapest first, which cannot change a yes/no over all of them.
28
+ // A solve with a custom rule judges afresh every time: a custom rule could
29
+ // read any part of the state, so no counter can say when its answer went stale.
30
+ if (state.ctx !== ctx || !allBuiltIn(ctx))
31
+ return !(0, verdicts_1.anyHardDelta)(ctx, state, pair);
32
+ // The crew-reading rules are asked live. The rest read only the person's
33
+ // timeline and records, which move exactly when the person's counter does,
34
+ // so their answer is remembered until then — a shift nobody can fill is
35
+ // otherwise re-judged against the whole team on every pass of the search.
36
+ if ((0, verdicts_1.anyHardDeltaIn)(ctx, state, pair, 'crew'))
37
+ return false;
38
+ const person = state.personVersion.get(ctx.personIdOf.get(employeeId) ?? employeeId) ?? 0;
39
+ let byEmployee = state.hardCache.get(instanceId);
40
+ if (!byEmployee) {
41
+ byEmployee = new Map();
42
+ state.hardCache.set(instanceId, byEmployee);
28
43
  }
29
- return true;
44
+ const cached = byEmployee.get(employeeId);
45
+ if (cached && cached.person === person)
46
+ return cached.ok;
47
+ const ok = !(0, verdicts_1.anyHardDeltaIn)(ctx, state, pair, 'person');
48
+ byEmployee.set(employeeId, { person, ok });
49
+ return ok;
30
50
  }
31
51
  exports.hardCompliant = hardCompliant;
52
+ const builtInOnly = new WeakMap();
53
+ function allBuiltIn(ctx) {
54
+ let answer = builtInOnly.get(ctx);
55
+ if (answer === undefined) {
56
+ answer = ctx.constraints.every((c) => support_1.builtInConstraints.has(c));
57
+ builtInOnly.set(ctx, answer);
58
+ }
59
+ return answer;
60
+ }
32
61
  function rankCandidate(ctx, state, instanceId, employeeId, objective, rand) {
33
62
  const reasons = [];
34
63
  let rank = 0;
@@ -57,7 +57,6 @@ export declare function minHoursShortfalls(ctx: ModelContext, state: SearchState
57
57
  * instead of being silently ignored.
58
58
  */
59
59
  export declare function breachTotals(ctx: ModelContext, state: SearchState): LexScore;
60
- /** Backwards-compatible soft-breach total, for callers that want a single figure. */
61
60
  export declare function softBreaches(ctx: ModelContext, state: SearchState): number;
62
61
  /** The full three-level score. Lower is better at every level. */
63
62
  export declare function scoreLex(ctx: ModelContext, state: SearchState, objective: 'standard' | 'balanced', minHoursWeight: number, fairnessRules?: FairnessRule[]): LexScore;
@@ -1 +1 @@
1
- {"version":3,"file":"objective.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/objective.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAKxE,eAAO,MAAM,eAAe,QAAS,CAAC;AACtC,eAAO,MAAM,oBAAoB,OAAQ,CAAC;AAC1C,eAAO,MAAM,gBAAgB,MAAM,CAAC;AACpC,6EAA6E;AAC7E,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC,2DAA2D;AAC3D,MAAM,WAAW,QAAQ;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,MAAM,CAI9D;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,OAAO,CAE1D;AAED,gEAAgE;AAChE,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAI9F;AAED,6EAA6E;AAC7E,wBAAgB,WAAW,CAAC,GAAG,EAAE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EAAE,YAAY,EACjB,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,cAAc,EAAE,MAAM,EACtB,aAAa,CAAC,EAAE,YAAY,EAAE,GAC/B,QAAQ,CA6BV;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CACjB,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,cAAc,EAAE,MAAM,GACvB,MAAM,CAGR"}
@@ -27,6 +27,8 @@
27
27
  */
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.score = exports.scoreLex = exports.softBreaches = exports.breachTotals = exports.minHoursShortfalls = exports.hoursVariance = exports.unfilledSlots = exports.tagShortage = exports.slotShortage = exports.isBetter = exports.compareScores = exports.DEFAULT_SOFT_WEIGHT = exports.MIN_HOURS_WEIGHT = exports.TAG_SHORTFALL_WEIGHT = exports.UNFILLED_WEIGHT = void 0;
30
+ const support_1 = require("../constraints/support");
31
+ const verdicts_1 = require("./verdicts");
30
32
  const fairness_1 = require("../constraints/fairness");
31
33
  const availability_1 = require("../constraints/availability");
32
34
  const contract_hours_1 = require("../contract-hours");
@@ -133,10 +135,20 @@ function breachTotals(ctx, state) {
133
135
  const scored = ctx.constraints.filter((c) => c.id !== 'min-staffing' && c.id !== 'fairness');
134
136
  if (scored.length === 0)
135
137
  return totals;
138
+ // The search scores the whole roster after every move, and most people's
139
+ // schedules did not move. Where every rule is a built-in and the state
140
+ // counts mutations per person, a pair's person-scoped deltas are reused
141
+ // until that person changes; crew-reading rules are always asked. The
142
+ // sums below run over the same pairs and rules in the same order either
143
+ // way, so the totals are the same numbers to the last bit.
144
+ const memo = deltaMemoFor(ctx, state, scored);
136
145
  for (const [instanceId, employees] of state.assignments) {
137
146
  for (const employeeId of employees) {
138
- for (const c of scored) {
139
- const d = c.delta(state, { employeeId, shiftInstanceId: instanceId });
147
+ const pair = { employeeId, shiftInstanceId: instanceId };
148
+ const cached = memo?.lookup(pair);
149
+ for (let i = 0; i < scored.length; i++) {
150
+ const c = scored[i];
151
+ const d = cached && !memo.crew[i] ? cached[i] : c.delta(state, pair);
140
152
  if (d > 0)
141
153
  totals[c.hardness] += d * (c.weight ?? exports.DEFAULT_SOFT_WEIGHT);
142
154
  }
@@ -145,7 +157,37 @@ function breachTotals(ctx, state) {
145
157
  return totals;
146
158
  }
147
159
  exports.breachTotals = breachTotals;
148
- /** Backwards-compatible soft-breach total, for callers that want a single figure. */
160
+ const memos = new WeakMap();
161
+ function deltaMemoFor(ctx, state, scored) {
162
+ const versions = state.personVersion;
163
+ if (!versions || state.ctx !== ctx || !ctx.constraints.every((c) => support_1.builtInConstraints.has(c)))
164
+ return undefined;
165
+ const existing = memos.get(state);
166
+ if (existing && existing.scored.length === scored.length && existing.scored.every((c, i) => c === scored[i])) {
167
+ return existing.memo;
168
+ }
169
+ const crew = scored.map((c) => (0, verdicts_1.readsCrew)(c));
170
+ const byInstance = new Map();
171
+ const memo = {
172
+ crew,
173
+ lookup(pair) {
174
+ const person = versions.get(ctx.personIdOf.get(pair.employeeId) ?? pair.employeeId) ?? 0;
175
+ let byEmployee = byInstance.get(pair.shiftInstanceId);
176
+ if (!byEmployee) {
177
+ byEmployee = new Map();
178
+ byInstance.set(pair.shiftInstanceId, byEmployee);
179
+ }
180
+ const hit = byEmployee.get(pair.employeeId);
181
+ if (hit && hit.person === person)
182
+ return hit.deltas;
183
+ const deltas = scored.map((c, i) => (crew[i] ? 0 : c.delta(state, pair)));
184
+ byEmployee.set(pair.employeeId, { person, deltas });
185
+ return deltas;
186
+ },
187
+ };
188
+ memos.set(state, { scored, memo });
189
+ return memo;
190
+ }
149
191
  function softBreaches(ctx, state) {
150
192
  return breachTotals(ctx, state).soft;
151
193
  }
@@ -36,6 +36,10 @@ export interface SearchOptions {
36
36
  historyLength?: number;
37
37
  /** Called whenever a new best is found, for anytime reporting. */
38
38
  onImprovement?: (state: InternalState, score: LexScore) => void;
39
+ /** After every round, with the best state so far and the variants judged. */
40
+ onRound?: (best: InternalState, evaluatedVariants: number) => void;
41
+ /** Asked before every round; `true` ends the search with the best so far. */
42
+ shouldStop?: () => boolean;
39
43
  }
40
44
  export interface SearchOutcome {
41
45
  best: InternalState;
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,EAAgE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC1G,OAAO,EAAmC,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAG9E,MAAM,WAAW,aAAa;IAC1B,SAAS,EAAE,UAAU,GAAG,UAAU,CAAC;IACnC,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,MAAM,MAAM,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,iEAAiE;IACjE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,kEAAkE;IAClE,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,KAAK,IAAI,CAAC;CACnE;AAED,MAAM,WAAW,aAAa;IAC1B,IAAI,EAAE,aAAa,CAAC;IACpB,iBAAiB,EAAE,MAAM,CAAC;CAC7B;AAiCD;;;;;GAKG;AACH,wBAAgB,cAAc,CAC1B,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,aAAa,GACtC,aAAa,CA4Ef"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,EAAgE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC1G,OAAO,EAAmC,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAG9E,MAAM,WAAW,aAAa;IAC1B,SAAS,EAAE,UAAU,GAAG,UAAU,CAAC;IACnC,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,MAAM,MAAM,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,iEAAiE;IACjE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,kEAAkE;IAClE,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,KAAK,IAAI,CAAC;IAChE,6EAA6E;IAC7E,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,KAAK,IAAI,CAAC;IACnE,6EAA6E;IAC7E,UAAU,CAAC,EAAE,MAAM,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,aAAa;IAC1B,IAAI,EAAE,aAAa,CAAC;IACpB,iBAAiB,EAAE,MAAM,CAAC;CAC7B;AAiCD;;;;;GAKG;AACH,wBAAgB,cAAc,CAC1B,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,OAAO,EAAE,aAAa,EACtB,gBAAgB,EAAE,MAAM,aAAa,GACtC,aAAa,CA6Ef"}
@@ -71,7 +71,7 @@ function improveWithLns(ctx, state, propagation, options, createEmptyState) {
71
71
  const historyLength = Math.max(1, options.historyLength ?? DEFAULT_HISTORY_LENGTH);
72
72
  const history = new Array(historyLength).fill(workingScore);
73
73
  let iteration = 0;
74
- while (now() < deadline && iteration < (options.maxIterations ?? Infinity)) {
74
+ while (now() < deadline && iteration < (options.maxIterations ?? Infinity) && !options.shouldStop?.()) {
75
75
  const total = (0, state_1.assignedPairs)(working).length;
76
76
  if (total === 0)
77
77
  break;
@@ -121,6 +121,7 @@ function improveWithLns(ctx, state, propagation, options, createEmptyState) {
121
121
  }
122
122
  history[iteration % historyLength] = workingScore;
123
123
  iteration++;
124
+ options.onRound?.(best, evaluatedVariants);
124
125
  }
125
126
  return { best, evaluatedVariants };
126
127
  }
@@ -9,6 +9,20 @@ import type { ModelContext, SearchState } from '../types';
9
9
  export interface InternalState extends SearchState {
10
10
  /** Per-pair selection reasons recorded by construction/repair, surfaced in the result. */
11
11
  reasons: Map<string, string[]>;
12
+ /**
13
+ * A mutation counter per person, bumped by every `assign`/`unassign` of any
14
+ * of their records: it moves exactly when their timeline, shifts or hours do.
15
+ */
16
+ personVersion: Map<string, number>;
17
+ /**
18
+ * The person-scoped half of `hardCompliant`'s answer, by instance then
19
+ * employee, with the person counter it was computed at. The crew-reading
20
+ * rules are cheap and asked live.
21
+ */
22
+ hardCache: Map<string, Map<string, {
23
+ person: number;
24
+ ok: boolean;
25
+ }>>;
12
26
  }
13
27
  export declare function createState(ctx: ModelContext): InternalState;
14
28
  export declare function pairKey(employeeId: string, instanceId: string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/state.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAG1D,MAAM,WAAW,aAAc,SAAQ,WAAW;IAC9C,0FAA0F;IAC1F,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CA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EAAE,YAAY,GAAG,aAAa,CAwB5D;AAED,wBAAgB,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAEtE;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,CAwB5G;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAa3F;AAMD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAE9E;AAED,yFAAyF;AACzF,wBAAgB,aAAa,CAAC,KAAK,EAAE,aAAa,GAAG,KAAK,CAAC;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAQrG"}
@@ -28,6 +28,8 @@ function createState(ctx) {
28
28
  // boundary from the very first evaluation.
29
29
  timelines: new timeline_1.TimelineIndex(ctx.history),
30
30
  reasons: new Map(),
31
+ personVersion: new Map(),
32
+ hardCache: new Map(),
31
33
  isAssigned(employeeId, instanceId) {
32
34
  return assignments.get(instanceId)?.has(employeeId) ?? false;
33
35
  },
@@ -52,6 +54,7 @@ function assign(state, employeeId, instanceId, reasons) {
52
54
  // classification can put below the elapsed span.
53
55
  state.minutesByEmployee.set(employeeId, (state.minutesByEmployee.get(employeeId) ?? 0) + inst.workingMinutes);
54
56
  const personId = state.ctx.personIdOf.get(employeeId) ?? employeeId;
57
+ touch(state, personId);
55
58
  state.timelines.add(personId, {
56
59
  id: timelineEntryId(employeeId, instanceId),
57
60
  start: inst.startMinute,
@@ -73,10 +76,14 @@ function unassign(state, employeeId, instanceId) {
73
76
  state.byEmployee.get(employeeId)?.delete(instanceId);
74
77
  state.minutesByEmployee.set(employeeId, (state.minutesByEmployee.get(employeeId) ?? 0) - inst.workingMinutes);
75
78
  const personId = state.ctx.personIdOf.get(employeeId) ?? employeeId;
79
+ touch(state, personId);
76
80
  state.timelines.remove(personId, timelineEntryId(employeeId, instanceId));
77
81
  state.reasons.delete(pairKey(employeeId, instanceId));
78
82
  }
79
83
  exports.unassign = unassign;
84
+ function touch(state, personId) {
85
+ state.personVersion.set(personId, (state.personVersion.get(personId) ?? 0) + 1);
86
+ }
80
87
  /**
81
88
  * Timeline entries are keyed by employee *and* instance, not by instance alone:
82
89
  * two contracts of the same person share a timeline, so an instance id on its
@@ -73,6 +73,8 @@ export declare class PersonTimeline {
73
73
  * to save a walk over a few dozen items.
74
74
  */
75
75
  workingMinutesIn(window: MinuteRange): number;
76
+ /** `workingMinutesIn` without the window object, for the probes below; assumes a clean index. */
77
+ private workingMinutesBetween;
76
78
  /**
77
79
  * Worst case of "working minutes in any window of `windowMinutes`" that
78
80
  * overlaps `bounds` — the quantity a rolling average such as "≤48h in any
@@ -98,8 +100,12 @@ export declare class PersonTimeline {
98
100
  * window over `bounds`, plus the bounds themselves. Sliding a window until
99
101
  * its edge meets a boundary never reduces the extreme being sought, so this
100
102
  * finite set contains the answer.
103
+ *
104
+ * Visited rather than collected: both callers take a max or a min, which a
105
+ * repeated edge cannot change, and these probes run for every candidate the
106
+ * search judges, so the set and array they used to build were pure garbage.
101
107
  */
102
- private candidateWindowStarts;
108
+ private forEachCandidateWindowStart;
103
109
  /** Entries intersecting `window`, in order. */
104
110
  entriesIn(window: MinuteRange): TimelineEntry[];
105
111
  /**
@@ -128,6 +134,8 @@ export declare class PersonTimeline {
128
134
  * window has 7 days of rest, and a weekly-rest rule must see that.
129
135
  */
130
136
  longestRestIn(window: MinuteRange): number;
137
+ /** `longestRestIn` without the window object or the intersecting-entries array; assumes a clean index. */
138
+ private longestRestBetween;
131
139
  /**
132
140
  * Smallest gap between `range` and any existing entry, ignoring `excludeId`.
133
141
  * Returns `Infinity` when the timeline is otherwise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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"}
@@ -28,7 +28,6 @@
28
28
  */
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.TimelineIndex = exports.PersonTimeline = void 0;
31
- const time_1 = require("../time");
32
31
  /**
33
32
  * One person's ordered, non-overlapping-by-construction spans, with prefix sums
34
33
  * for O(log n) window queries.
@@ -90,15 +89,19 @@ class PersonTimeline {
90
89
  */
91
90
  workingMinutesIn(window) {
92
91
  this.ensureClean();
93
- if (window.end <= window.start || this.entries.length === 0)
92
+ return this.workingMinutesBetween(window.start, window.end);
93
+ }
94
+ /** `workingMinutesIn` without the window object, for the probes below; assumes a clean index. */
95
+ workingMinutesBetween(start, end) {
96
+ if (end <= start || this.entries.length === 0)
94
97
  return 0;
95
- const first = this.firstOverlapping(window.start);
98
+ const first = this.firstOverlapping(start);
96
99
  let total = 0;
97
100
  for (let i = first; i < this.entries.length; i++) {
98
101
  const entry = this.entries[i];
99
- if (entry.start >= window.end)
102
+ if (entry.start >= end)
100
103
  break;
101
- total += proratedWorkingMinutes(entry, window);
104
+ total += proratedWorkingMinutesBetween(entry, start, end);
102
105
  }
103
106
  return total;
104
107
  }
@@ -118,11 +121,12 @@ class PersonTimeline {
118
121
  if (windowMinutes <= 0)
119
122
  return 0;
120
123
  let worst = 0;
121
- for (const start of this.candidateWindowStarts(windowMinutes, bounds)) {
122
- const total = this.workingMinutesIn({ start, end: start + windowMinutes });
124
+ const probe = (start) => {
125
+ const total = this.workingMinutesBetween(start, start + windowMinutes);
123
126
  if (total > worst)
124
127
  worst = total;
125
- }
128
+ };
129
+ this.forEachCandidateWindowStart(windowMinutes, bounds, probe);
126
130
  return worst;
127
131
  }
128
132
  /**
@@ -137,11 +141,12 @@ class PersonTimeline {
137
141
  if (windowMinutes <= 0)
138
142
  return Infinity;
139
143
  let worst = Infinity;
140
- for (const start of this.candidateWindowStarts(windowMinutes, bounds)) {
141
- const rest = this.longestRestIn({ start, end: start + windowMinutes });
144
+ const probe = (start) => {
145
+ const rest = this.longestRestBetween(start, start + windowMinutes);
142
146
  if (rest < worst)
143
147
  worst = rest;
144
- }
148
+ };
149
+ this.forEachCandidateWindowStart(windowMinutes, bounds, probe);
145
150
  return worst;
146
151
  }
147
152
  /**
@@ -149,16 +154,25 @@ class PersonTimeline {
149
154
  * window over `bounds`, plus the bounds themselves. Sliding a window until
150
155
  * its edge meets a boundary never reduces the extreme being sought, so this
151
156
  * finite set contains the answer.
157
+ *
158
+ * Visited rather than collected: both callers take a max or a min, which a
159
+ * repeated edge cannot change, and these probes run for every candidate the
160
+ * search judges, so the set and array they used to build were pure garbage.
152
161
  */
153
- candidateWindowStarts(windowMinutes, bounds) {
154
- const starts = new Set([bounds.start, bounds.end - windowMinutes]);
162
+ forEachCandidateWindowStart(windowMinutes, bounds, visit) {
163
+ visit(bounds.start);
164
+ visit(bounds.end - windowMinutes);
165
+ const lowest = bounds.start - windowMinutes;
166
+ const highest = bounds.end + windowMinutes;
155
167
  for (const entry of this.entries) {
156
- if (entry.end < bounds.start - windowMinutes || entry.start > bounds.end + windowMinutes)
168
+ // Sorted by start, so nothing after this can qualify either.
169
+ if (entry.start > highest)
170
+ break;
171
+ if (entry.end < lowest)
157
172
  continue;
158
- starts.add(entry.start);
159
- starts.add(entry.end - windowMinutes);
173
+ visit(entry.start);
174
+ visit(entry.end - windowMinutes);
160
175
  }
161
- return [...starts];
162
176
  }
163
177
  /** Entries intersecting `window`, in order. */
164
178
  entriesIn(window) {
@@ -225,20 +239,31 @@ class PersonTimeline {
225
239
  * window has 7 days of rest, and a weekly-rest rule must see that.
226
240
  */
227
241
  longestRestIn(window) {
228
- if (window.end <= window.start)
242
+ this.ensureClean();
243
+ return this.longestRestBetween(window.start, window.end);
244
+ }
245
+ /** `longestRestIn` without the window object or the intersecting-entries array; assumes a clean index. */
246
+ longestRestBetween(start, end) {
247
+ if (end <= start)
229
248
  return 0;
230
- const busy = this.entriesIn(window);
231
- if (busy.length === 0)
232
- return window.end - window.start;
233
249
  let longest = 0;
234
- let cursor = window.start;
235
- for (const entry of busy) {
250
+ let cursor = start;
251
+ let any = false;
252
+ for (let i = this.firstOverlapping(start); i < this.entries.length; i++) {
253
+ const entry = this.entries[i];
254
+ if (entry.start >= end)
255
+ break;
256
+ if (entry.end <= start)
257
+ continue;
258
+ any = true;
236
259
  if (entry.start > cursor)
237
260
  longest = Math.max(longest, entry.start - cursor);
238
261
  cursor = Math.max(cursor, entry.end);
239
262
  }
240
- if (window.end > cursor)
241
- longest = Math.max(longest, window.end - cursor);
263
+ if (!any)
264
+ return end - start;
265
+ if (end > cursor)
266
+ longest = Math.max(longest, end - cursor);
242
267
  return longest;
243
268
  }
244
269
  /**
@@ -438,11 +463,11 @@ function lowerBound(entries, start) {
438
463
  * at a fraction, or only for actual call-outs), so a partial overlap is scaled
439
464
  * by the same fraction rather than clipped to elapsed time.
440
465
  */
441
- function proratedWorkingMinutes(entry, window) {
466
+ function proratedWorkingMinutesBetween(entry, start, end) {
442
467
  const span = entry.end - entry.start;
443
468
  if (span <= 0)
444
469
  return 0;
445
- const inside = (0, time_1.overlapMinutes)(entry, window);
470
+ const inside = Math.max(0, Math.min(entry.end, end) - Math.max(entry.start, start));
446
471
  if (inside === span)
447
472
  return entry.workingMinutes;
448
473
  return (entry.workingMinutes * inside) / span;