assignment-user-matcher 1.12.0 → 1.14.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.
- package/README.md +78 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/managers/WorkflowManager.d.ts +24 -0
- package/dist/managers/WorkflowManager.d.ts.map +1 -1
- package/dist/managers/WorkflowManager.js +77 -0
- package/dist/matcher.class.d.ts +52 -3
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +157 -2
- package/dist/scheduling/constraints/constraint.d.ts.map +1 -1
- package/dist/scheduling/constraints/constraint.js +7 -1
- package/dist/scheduling/constraints/contract.d.ts +1 -0
- package/dist/scheduling/constraints/contract.d.ts.map +1 -1
- package/dist/scheduling/constraints/contract.js +61 -16
- package/dist/scheduling/constraints/fairness.js +1 -1
- package/dist/scheduling/constraints/overtime.d.ts.map +1 -1
- package/dist/scheduling/constraints/overtime.js +4 -2
- package/dist/scheduling/constraints/site-eligibility.d.ts +11 -0
- package/dist/scheduling/constraints/site-eligibility.d.ts.map +1 -0
- package/dist/scheduling/constraints/site-eligibility.js +52 -0
- package/dist/scheduling/constraints/site-home.d.ts +14 -0
- package/dist/scheduling/constraints/site-home.d.ts.map +1 -0
- package/dist/scheduling/constraints/site-home.js +43 -0
- package/dist/scheduling/constraints/site-travel-gap.d.ts +19 -0
- package/dist/scheduling/constraints/site-travel-gap.d.ts.map +1 -0
- package/dist/scheduling/constraints/site-travel-gap.js +88 -0
- package/dist/scheduling/contract-hours.d.ts +42 -0
- package/dist/scheduling/contract-hours.d.ts.map +1 -0
- package/dist/scheduling/contract-hours.js +104 -0
- package/dist/scheduling/engine/objective.d.ts.map +1 -1
- package/dist/scheduling/engine/objective.js +5 -0
- package/dist/scheduling/engine/state.d.ts.map +1 -1
- package/dist/scheduling/engine/state.js +1 -0
- package/dist/scheduling/engine/timeline.d.ts +2 -0
- package/dist/scheduling/engine/timeline.d.ts.map +1 -1
- package/dist/scheduling/index.d.ts +4 -1
- package/dist/scheduling/index.d.ts.map +1 -1
- package/dist/scheduling/index.js +14 -1
- package/dist/scheduling/model.d.ts +11 -0
- package/dist/scheduling/model.d.ts.map +1 -1
- package/dist/scheduling/model.js +81 -1
- package/dist/scheduling/operations.d.ts +23 -1
- package/dist/scheduling/operations.d.ts.map +1 -1
- package/dist/scheduling/operations.js +85 -4
- package/dist/scheduling/provenance.d.ts +2 -2
- package/dist/scheduling/provenance.d.ts.map +1 -1
- package/dist/scheduling/provenance.js +15 -1
- package/dist/scheduling/scheduler.class.d.ts.map +1 -1
- package/dist/scheduling/scheduler.class.js +4 -1
- package/dist/scheduling/sites.d.ts +35 -0
- package/dist/scheduling/sites.d.ts.map +1 -0
- package/dist/scheduling/sites.js +112 -0
- package/dist/scheduling/types.d.ts +120 -1
- package/dist/scheduling/types.d.ts.map +1 -1
- package/dist/types/matcher.d.ts +49 -1
- package/dist/types/matcher.d.ts.map +1 -1
- package/dist/utils/keys.d.ts +2 -0
- package/dist/utils/keys.d.ts.map +1 -1
- package/dist/utils/keys.js +8 -0
- package/package.json +13 -1
|
@@ -28,7 +28,17 @@ const verdicts_1 = require("./engine/verdicts");
|
|
|
28
28
|
const scheduler_class_1 = require("./scheduler.class");
|
|
29
29
|
const availability_1 = require("./constraints/availability");
|
|
30
30
|
const cost_1 = require("./cost");
|
|
31
|
+
const contract_hours_1 = require("./contract-hours");
|
|
32
|
+
const sites_1 = require("./sites");
|
|
33
|
+
const rest_days_1 = require("./constraints/rest-days");
|
|
34
|
+
const notice_1 = require("./constraints/notice");
|
|
35
|
+
const protections_1 = require("./constraints/protections");
|
|
36
|
+
const overtime_1 = require("./constraints/overtime");
|
|
31
37
|
const time_1 = require("./time");
|
|
38
|
+
/** Soft-ranking weight for travel minutes in `rankCandidates`. */
|
|
39
|
+
const TRAVEL_MINUTE_WEIGHT = 1;
|
|
40
|
+
/** Soft-ranking weight per hour a candidate would be pushed over their contracted period total. */
|
|
41
|
+
const CONTRACT_HOUR_WEIGHT = 10;
|
|
32
42
|
/**
|
|
33
43
|
* Validate an externally-produced or hand-edited roster.
|
|
34
44
|
*
|
|
@@ -70,11 +80,21 @@ function checkCompliance(input, roster) {
|
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
violations.push(...(0, verdicts_1.collectPairViolations)(state), ...(0, verdicts_1.collectAggregateViolations)(state));
|
|
83
|
+
// The same ledger pass the solver runs at result assembly — one judgement
|
|
84
|
+
// path, so `checkCompliance` can never disagree with `solveSchedule`
|
|
85
|
+
// about what a roster owes.
|
|
86
|
+
const ledger = [
|
|
87
|
+
...(0, rest_days_1.compensatoryRestLedger)(state, ctx.rules.restDays),
|
|
88
|
+
...(0, notice_1.cancellationLedger)(state, ctx.rules.notice),
|
|
89
|
+
...(0, protections_1.protectionLedger)(state),
|
|
90
|
+
...(0, overtime_1.overtimeLedger)(state),
|
|
91
|
+
];
|
|
73
92
|
return {
|
|
74
93
|
compliant: violations.every((v) => v.severity !== 'hard'),
|
|
75
94
|
violations,
|
|
76
95
|
verdicts,
|
|
77
|
-
ledger
|
|
96
|
+
ledger,
|
|
97
|
+
contractHours: (0, contract_hours_1.contractHoursSummary)(state),
|
|
78
98
|
};
|
|
79
99
|
}
|
|
80
100
|
exports.checkCompliance = checkCompliance;
|
|
@@ -180,6 +200,22 @@ function rankCandidates(input, shiftInstanceId, roster, disruption) {
|
|
|
180
200
|
const costCents = (0, cost_1.marginalCostCents)(inst, employee, state.minutesByEmployee.get(employee.id) ?? 0, ctx.rules.engagement);
|
|
181
201
|
const fairnessDebt = meanExtra - (extraLoad.get(employee.id) ?? 0);
|
|
182
202
|
const preference = (0, availability_1.preferenceScore)(ctx.clock, employee.availability, inst);
|
|
203
|
+
const originSiteId = originSiteFor(state, employee.id, inst);
|
|
204
|
+
const travelMinutes = originSiteId !== undefined && inst.siteId !== undefined
|
|
205
|
+
? (0, sites_1.travelMinutesBetween)(ctx.siteIndex, originSiteId, inst.siteId)
|
|
206
|
+
: undefined;
|
|
207
|
+
const distanceKm = travelMinutes === undefined && originSiteId !== undefined && inst.siteId !== undefined
|
|
208
|
+
? (0, sites_1.distanceKmBetween)(ctx.siteIndex, originSiteId, inst.siteId)
|
|
209
|
+
: undefined;
|
|
210
|
+
const isHomeSite = employee.homeSiteId !== undefined && employee.homeSiteId === inst.siteId;
|
|
211
|
+
// Contract headroom: a person still short of their contracted hours is
|
|
212
|
+
// a better call than one this shift would push past them — declared
|
|
213
|
+
// terms and realised counts only, nothing behavioural.
|
|
214
|
+
const contracted = ctx.contractedPeriodMinutes.get(employee.id);
|
|
215
|
+
const contractDeltaMinutes = contracted === undefined
|
|
216
|
+
? undefined
|
|
217
|
+
: (state.minutesByEmployee.get(employee.id) ?? 0) + inst.workingMinutes - contracted;
|
|
218
|
+
const contractPenalty = contractDeltaMinutes === undefined ? 0 : Math.max(0, contractDeltaMinutes) / 60;
|
|
183
219
|
candidates.push({
|
|
184
220
|
employeeId: employee.id,
|
|
185
221
|
shiftInstanceId,
|
|
@@ -188,26 +224,71 @@ function rankCandidates(input, shiftInstanceId, roster, disruption) {
|
|
|
188
224
|
blockers,
|
|
189
225
|
marginalCostCents: costCents,
|
|
190
226
|
fairnessDebt,
|
|
227
|
+
...(contractDeltaMinutes !== undefined ? { contractDeltaMinutes } : {}),
|
|
228
|
+
originSiteId,
|
|
229
|
+
travelMinutes,
|
|
230
|
+
distanceKm,
|
|
231
|
+
isHomeSite,
|
|
191
232
|
// Ineligible candidates sort to the bottom but stay visible.
|
|
192
|
-
rank: eligible
|
|
193
|
-
|
|
233
|
+
rank: eligible
|
|
234
|
+
? costCents / 100 -
|
|
235
|
+
fairnessDebt * 10 +
|
|
236
|
+
preference * 5 +
|
|
237
|
+
(travelMinutes ?? 0) * TRAVEL_MINUTE_WEIGHT +
|
|
238
|
+
contractPenalty * CONTRACT_HOUR_WEIGHT
|
|
239
|
+
: Number.MAX_SAFE_INTEGER,
|
|
240
|
+
rationale: rationaleFor(eligible, blockers, costCents, fairnessDebt, preference, travelMinutes, distanceKm, originSiteId, isHomeSite, contractDeltaMinutes),
|
|
194
241
|
});
|
|
195
242
|
}
|
|
196
243
|
return candidates.sort((a, b) => a.rank - b.rank || (a.employeeId < b.employeeId ? -1 : 1));
|
|
197
244
|
}
|
|
198
245
|
exports.rankCandidates = rankCandidates;
|
|
199
|
-
function rationaleFor(eligible, blockers, costCents, fairnessDebt, preference) {
|
|
246
|
+
function rationaleFor(eligible, blockers, costCents, fairnessDebt, preference, travelMinutes, distanceKm, originSiteId, isHomeSite, contractDeltaMinutes) {
|
|
200
247
|
if (!eligible)
|
|
201
248
|
return blockers.map((b) => b.message).join('; ') || 'not eligible';
|
|
202
249
|
const parts = [`€${(costCents / 100).toFixed(2)} marginal cost`];
|
|
203
250
|
if (fairnessDebt > 0)
|
|
204
251
|
parts.push(`${fairnessDebt.toFixed(1)} fewer extra shifts than average`);
|
|
252
|
+
if (contractDeltaMinutes !== undefined) {
|
|
253
|
+
parts.push(contractDeltaMinutes > 0
|
|
254
|
+
? `${(contractDeltaMinutes / 60).toFixed(1)}h over contract with this shift`
|
|
255
|
+
: `${(-contractDeltaMinutes / 60).toFixed(1)}h under contract with this shift`);
|
|
256
|
+
}
|
|
205
257
|
if (preference < 0)
|
|
206
258
|
parts.push('prefers this window');
|
|
207
259
|
if (preference > 0)
|
|
208
260
|
parts.push('would rather avoid this window');
|
|
261
|
+
if (isHomeSite) {
|
|
262
|
+
parts.push('home site');
|
|
263
|
+
}
|
|
264
|
+
else if (travelMinutes !== undefined && originSiteId !== undefined) {
|
|
265
|
+
parts.push(`${travelMinutes} min from ${originSiteId}`);
|
|
266
|
+
}
|
|
267
|
+
else if (distanceKm !== undefined && originSiteId !== undefined) {
|
|
268
|
+
parts.push(`${distanceKm.toFixed(1)} km from ${originSiteId}`);
|
|
269
|
+
}
|
|
209
270
|
return parts.join(' · ');
|
|
210
271
|
}
|
|
272
|
+
/** Site the candidate would travel from: nearest preceding same-day assignment, then home site. */
|
|
273
|
+
function originSiteFor(state, employeeId, inst) {
|
|
274
|
+
const assigned = state.byEmployee.get(employeeId);
|
|
275
|
+
const homeSite = state.ctx.employeeById.get(employeeId)?.homeSiteId;
|
|
276
|
+
if (!assigned)
|
|
277
|
+
return homeSite;
|
|
278
|
+
let best;
|
|
279
|
+
for (const instanceId of assigned) {
|
|
280
|
+
const other = state.ctx.instanceById.get(instanceId);
|
|
281
|
+
if (!other || other.date !== inst.date || !other.siteId)
|
|
282
|
+
continue;
|
|
283
|
+
if (other.endMinute <= inst.startMinute) {
|
|
284
|
+
const gap = inst.startMinute - other.endMinute;
|
|
285
|
+
if (!best || gap < best.gap) {
|
|
286
|
+
best = { siteId: other.siteId, gap };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return best?.siteId ?? homeSite;
|
|
291
|
+
}
|
|
211
292
|
/** Shifts each person already holds, as the basis for fairness debt. */
|
|
212
293
|
function extraShiftCounts(state) {
|
|
213
294
|
const counts = new Map();
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* commitment: it exists to detect that the rules changed between two runs, and
|
|
12
12
|
* to key an audit record. It is deliberately dependency-free.
|
|
13
13
|
*/
|
|
14
|
-
import type { ConstraintOptions, ObjectiveWeights, WorkingTimeRules } from './types';
|
|
14
|
+
import type { ConstraintOptions, ObjectiveWeights, Site, WorkingTimeRules } from './types';
|
|
15
15
|
/**
|
|
16
16
|
* A stable digest of the rule set, constraint options and objective weights.
|
|
17
17
|
*
|
|
@@ -19,7 +19,7 @@ import type { ConstraintOptions, ObjectiveWeights, WorkingTimeRules } from './ty
|
|
|
19
19
|
* in property order produce the same digest — otherwise a round-trip through
|
|
20
20
|
* JSON would look like a rule change.
|
|
21
21
|
*/
|
|
22
|
-
export declare function hashRules(rules: WorkingTimeRules | undefined, options: ConstraintOptions | undefined, objectives?: ObjectiveWeights): string;
|
|
22
|
+
export declare function hashRules(rules: WorkingTimeRules | undefined, options: ConstraintOptions | undefined, objectives?: ObjectiveWeights, sites?: Site[], travelSpeedKmh?: number): string;
|
|
23
23
|
/** JSON with object keys sorted recursively. */
|
|
24
24
|
export declare function stableStringify(value: unknown): string;
|
|
25
25
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provenance.d.ts","sourceRoot":"","sources":["../../src/scheduling/provenance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"provenance.d.ts","sourceRoot":"","sources":["../../src/scheduling/provenance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,IAAI,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAgB,SAAS,CACrB,KAAK,EAAE,gBAAgB,GAAG,SAAS,EACnC,OAAO,EAAE,iBAAiB,GAAG,SAAS,EACtC,UAAU,CAAC,EAAE,gBAAgB,EAC7B,KAAK,CAAC,EAAE,IAAI,EAAE,EACd,cAAc,CAAC,EAAE,MAAM,GACxB,MAAM,CAgBR;AAaD,gDAAgD;AAChD,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAOtD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAY7C"}
|
|
@@ -21,7 +21,7 @@ exports.fnv1a64 = exports.stableStringify = exports.hashRules = void 0;
|
|
|
21
21
|
* in property order produce the same digest — otherwise a round-trip through
|
|
22
22
|
* JSON would look like a rule change.
|
|
23
23
|
*/
|
|
24
|
-
function hashRules(rules, options, objectives) {
|
|
24
|
+
function hashRules(rules, options, objectives, sites, travelSpeedKmh) {
|
|
25
25
|
const payload = stableStringify({
|
|
26
26
|
rules: rules ?? null,
|
|
27
27
|
// Only the parts of `constraints` that change outcomes: custom rules are
|
|
@@ -31,10 +31,24 @@ function hashRules(rules, options, objectives) {
|
|
|
31
31
|
overrides: options?.overrides ?? null,
|
|
32
32
|
custom: (options?.custom ?? []).map((c) => ({ id: c.id, hardness: c.hardness, weight: c.weight ?? null })),
|
|
33
33
|
objectives: objectives ?? null,
|
|
34
|
+
// Omit these keys entirely when absent so existing inputs without site data
|
|
35
|
+
// keep the same rulesHash they had before this feature existed.
|
|
36
|
+
...(sites ? { sites: sites.map(siteDigest) } : {}),
|
|
37
|
+
...(travelSpeedKmh !== undefined ? { travelSpeedKmh } : {}),
|
|
34
38
|
});
|
|
35
39
|
return fnv1a64(payload);
|
|
36
40
|
}
|
|
37
41
|
exports.hashRules = hashRules;
|
|
42
|
+
function siteDigest(site) {
|
|
43
|
+
return {
|
|
44
|
+
id: site.id,
|
|
45
|
+
lat: site.lat ?? null,
|
|
46
|
+
lng: site.lng ?? null,
|
|
47
|
+
travelMinutesTo: site.travelMinutesTo
|
|
48
|
+
? Object.fromEntries(Object.entries(site.travelMinutesTo).sort(([a], [b]) => (a < b ? -1 : 1)))
|
|
49
|
+
: null,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
38
52
|
/** JSON with object keys sorted recursively. */
|
|
39
53
|
function stableStringify(value) {
|
|
40
54
|
if (value === null || typeof value !== 'object')
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduler.class.d.ts","sourceRoot":"","sources":["../../src/scheduling/scheduler.class.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAoC,aAAa,EAAE,cAAc,EAAuB,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"scheduler.class.d.ts","sourceRoot":"","sources":["../../src/scheduling/scheduler.class.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAoC,aAAa,EAAE,cAAc,EAAuB,MAAM,SAAS,CAAC;AAsBpH,eAAO,MAAM,cAAc,MAAM,CAAC;AAElC,qBAAa,cAAc;IACvB,qGAAqG;IACrG,KAAK,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc;IA+C3C,qFAAqF;IACrF,OAAO,CAAC,QAAQ;CA8EnB;AAUD,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc,CAElE"}
|
|
@@ -28,6 +28,7 @@ const notice_1 = require("./constraints/notice");
|
|
|
28
28
|
const protections_1 = require("./constraints/protections");
|
|
29
29
|
const overtime_1 = require("./constraints/overtime");
|
|
30
30
|
const cost_1 = require("./cost");
|
|
31
|
+
const contract_hours_1 = require("./contract-hours");
|
|
31
32
|
const prng_1 = require("./engine/prng");
|
|
32
33
|
const verdicts_1 = require("./engine/verdicts");
|
|
33
34
|
const provenance_1 = require("./provenance");
|
|
@@ -110,6 +111,7 @@ class ShiftScheduler {
|
|
|
110
111
|
...(0, overtime_1.overtimeLedger)(state),
|
|
111
112
|
];
|
|
112
113
|
const cost = (0, cost_1.rosterCost)(state);
|
|
114
|
+
const contractHours = (0, contract_hours_1.contractHoursSummary)(state);
|
|
113
115
|
return {
|
|
114
116
|
status,
|
|
115
117
|
assignments,
|
|
@@ -122,7 +124,7 @@ class ShiftScheduler {
|
|
|
122
124
|
provenance: {
|
|
123
125
|
engineVersion: exports.ENGINE_VERSION,
|
|
124
126
|
seed,
|
|
125
|
-
rulesHash: (0, provenance_1.hashRules)(input.rules, input.constraints, input.objectives),
|
|
127
|
+
rulesHash: (0, provenance_1.hashRules)(input.rules, input.constraints, input.objectives, input.sites, input.travelSpeedKmh),
|
|
126
128
|
dutyClassificationNotes: dutyNotes(ctx),
|
|
127
129
|
// Nothing in this module consumes a behavioural or predictive
|
|
128
130
|
// per-worker signal, so the claim is structural rather than a
|
|
@@ -131,6 +133,7 @@ class ShiftScheduler {
|
|
|
131
133
|
},
|
|
132
134
|
...(ledger.length ? { ledger } : {}),
|
|
133
135
|
...(cost ? { cost } : {}),
|
|
136
|
+
...(contractHours.length ? { contractHours } : {}),
|
|
134
137
|
};
|
|
135
138
|
}
|
|
136
139
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Site registry and travel-time resolution.
|
|
3
|
+
*
|
|
4
|
+
* Multi-site rosters need two numbers that the caller owns: where a site is
|
|
5
|
+
* (coordinates) and how long it takes to move between sites (a travel-time
|
|
6
|
+
* matrix or a fallback speed). The engine never calls an external routing
|
|
7
|
+
* service and never invents default values — it only does pure arithmetic on
|
|
8
|
+
* caller-supplied data.
|
|
9
|
+
*/
|
|
10
|
+
import type { Site, SiteIndex } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* Build a validated site index from caller input.
|
|
13
|
+
*
|
|
14
|
+
* Throws `ScheduleValidationError` for duplicate ids, malformed coordinates,
|
|
15
|
+
* negative matrix entries, or a non-positive fallback speed. Site ids that are
|
|
16
|
+
* referenced by templates or employees but not declared here are tolerated;
|
|
17
|
+
* they simply have no coordinates or matrix entries.
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildSiteIndex(sites: Site[] | undefined, travelSpeedKmh?: number): SiteIndex;
|
|
20
|
+
/**
|
|
21
|
+
* Travel time between two sites, in minutes.
|
|
22
|
+
*
|
|
23
|
+
* Returns 0 for the same site, undefined when either site is unknown or no
|
|
24
|
+
* travel data exists for the ordered pair.
|
|
25
|
+
*/
|
|
26
|
+
export declare function travelMinutesBetween(index: SiteIndex, from?: string, to?: string): number | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Straight-line distance between two sites, in kilometres.
|
|
29
|
+
*
|
|
30
|
+
* Returns 0 for same site, undefined when either site is unknown or lacks
|
|
31
|
+
* coordinates. Used for display and host-side re-sorting when minutes are not
|
|
32
|
+
* available.
|
|
33
|
+
*/
|
|
34
|
+
export declare function distanceKmBetween(index: SiteIndex, from?: string, to?: string): number | undefined;
|
|
35
|
+
//# sourceMappingURL=sites.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sites.d.ts","sourceRoot":"","sources":["../../src/scheduling/sites.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAI/C;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,SAAS,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CA2C5F;AAsBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIrG;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAalG"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Site registry and travel-time resolution.
|
|
4
|
+
*
|
|
5
|
+
* Multi-site rosters need two numbers that the caller owns: where a site is
|
|
6
|
+
* (coordinates) and how long it takes to move between sites (a travel-time
|
|
7
|
+
* matrix or a fallback speed). The engine never calls an external routing
|
|
8
|
+
* service and never invents default values — it only does pure arithmetic on
|
|
9
|
+
* caller-supplied data.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.distanceKmBetween = exports.travelMinutesBetween = exports.buildSiteIndex = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const geo_1 = require("../utils/geo");
|
|
15
|
+
/**
|
|
16
|
+
* Build a validated site index from caller input.
|
|
17
|
+
*
|
|
18
|
+
* Throws `ScheduleValidationError` for duplicate ids, malformed coordinates,
|
|
19
|
+
* negative matrix entries, or a non-positive fallback speed. Site ids that are
|
|
20
|
+
* referenced by templates or employees but not declared here are tolerated;
|
|
21
|
+
* they simply have no coordinates or matrix entries.
|
|
22
|
+
*/
|
|
23
|
+
function buildSiteIndex(sites, travelSpeedKmh) {
|
|
24
|
+
if (travelSpeedKmh !== undefined && (!Number.isFinite(travelSpeedKmh) || travelSpeedKmh <= 0)) {
|
|
25
|
+
throw new types_1.ScheduleValidationError(`travelSpeedKmh must be a positive finite number: ${travelSpeedKmh}`);
|
|
26
|
+
}
|
|
27
|
+
const byId = new Map();
|
|
28
|
+
for (const site of sites ?? []) {
|
|
29
|
+
if (!site.id)
|
|
30
|
+
throw new types_1.ScheduleValidationError('Site is missing `id`');
|
|
31
|
+
if (byId.has(site.id))
|
|
32
|
+
throw new types_1.ScheduleValidationError(`Duplicate site id "${site.id}"`);
|
|
33
|
+
if (site.lat !== undefined || site.lng !== undefined) {
|
|
34
|
+
if (!(0, geo_1.hasValidCoordinates)(site.lat, site.lng)) {
|
|
35
|
+
throw new types_1.ScheduleValidationError(`Site "${site.id}" has invalid coordinates: lat=${site.lat}, lng=${site.lng}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (site.travelMinutesTo) {
|
|
39
|
+
for (const [target, minutes] of Object.entries(site.travelMinutesTo)) {
|
|
40
|
+
if (!Number.isFinite(minutes) || minutes < 0) {
|
|
41
|
+
throw new types_1.ScheduleValidationError(`Site "${site.id}".travelMinutesTo["${target}"] has invalid minutes: ${minutes}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
byId.set(site.id, site);
|
|
46
|
+
}
|
|
47
|
+
const resolvedTravelMinutes = new Map();
|
|
48
|
+
const siteList = [...byId.values()];
|
|
49
|
+
for (const from of siteList) {
|
|
50
|
+
const row = new Map();
|
|
51
|
+
resolvedTravelMinutes.set(from.id, row);
|
|
52
|
+
for (const to of siteList) {
|
|
53
|
+
const minutes = resolvePairMinutes(from, to, travelSpeedKmh);
|
|
54
|
+
if (minutes !== undefined)
|
|
55
|
+
row.set(to.id, minutes);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { byId, travelSpeedKmh, resolvedTravelMinutes };
|
|
59
|
+
}
|
|
60
|
+
exports.buildSiteIndex = buildSiteIndex;
|
|
61
|
+
function resolvePairMinutes(from, to, travelSpeedKmh) {
|
|
62
|
+
if (from.id === to.id)
|
|
63
|
+
return 0;
|
|
64
|
+
const matrix = from.travelMinutesTo?.[to.id];
|
|
65
|
+
if (matrix !== undefined)
|
|
66
|
+
return matrix;
|
|
67
|
+
if (from.lat !== undefined &&
|
|
68
|
+
from.lng !== undefined &&
|
|
69
|
+
to.lat !== undefined &&
|
|
70
|
+
to.lng !== undefined &&
|
|
71
|
+
travelSpeedKmh !== undefined) {
|
|
72
|
+
const km = (0, geo_1.haversineDistanceKm)(from.lat, from.lng, to.lat, to.lng);
|
|
73
|
+
return Math.round((km / travelSpeedKmh) * 60);
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Travel time between two sites, in minutes.
|
|
79
|
+
*
|
|
80
|
+
* Returns 0 for the same site, undefined when either site is unknown or no
|
|
81
|
+
* travel data exists for the ordered pair.
|
|
82
|
+
*/
|
|
83
|
+
function travelMinutesBetween(index, from, to) {
|
|
84
|
+
if (from === to && from !== undefined)
|
|
85
|
+
return 0;
|
|
86
|
+
if (from === undefined || to === undefined)
|
|
87
|
+
return undefined;
|
|
88
|
+
return index.resolvedTravelMinutes.get(from)?.get(to);
|
|
89
|
+
}
|
|
90
|
+
exports.travelMinutesBetween = travelMinutesBetween;
|
|
91
|
+
/**
|
|
92
|
+
* Straight-line distance between two sites, in kilometres.
|
|
93
|
+
*
|
|
94
|
+
* Returns 0 for same site, undefined when either site is unknown or lacks
|
|
95
|
+
* coordinates. Used for display and host-side re-sorting when minutes are not
|
|
96
|
+
* available.
|
|
97
|
+
*/
|
|
98
|
+
function distanceKmBetween(index, from, to) {
|
|
99
|
+
if (from === to && from !== undefined)
|
|
100
|
+
return 0;
|
|
101
|
+
if (from === undefined || to === undefined)
|
|
102
|
+
return undefined;
|
|
103
|
+
const fromSite = index.byId.get(from);
|
|
104
|
+
const toSite = index.byId.get(to);
|
|
105
|
+
if (!fromSite || !toSite)
|
|
106
|
+
return undefined;
|
|
107
|
+
if (fromSite.lat !== undefined && fromSite.lng !== undefined && toSite.lat !== undefined && toSite.lng !== undefined) {
|
|
108
|
+
return (0, geo_1.haversineDistanceKm)(fromSite.lat, fromSite.lng, toSite.lat, toSite.lng);
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
exports.distanceKmBetween = distanceKmBetween;
|
|
@@ -18,6 +18,10 @@ export interface Employee {
|
|
|
18
18
|
tags: string[];
|
|
19
19
|
/** ISO dates (YYYY-MM-DD) or explicit shift instances the employee must not work. */
|
|
20
20
|
timeOff: TimeOffEntry[];
|
|
21
|
+
/** Site the employee is normally based at. Used as a soft preference by the solver and for origin inference in suggestions. */
|
|
22
|
+
homeSiteId?: string;
|
|
23
|
+
/** Hard allow-list of sites this employee may work at. When set, shifts at unlisted sites are ineligible. */
|
|
24
|
+
siteIds?: string[];
|
|
21
25
|
/** Hard upper bound of worked **hours** over the whole period. */
|
|
22
26
|
maxHoursForPeriod?: number;
|
|
23
27
|
/** Soft lower bound of worked **hours** over the whole period (warn, don't block). */
|
|
@@ -103,8 +107,21 @@ export interface AvailabilityRule {
|
|
|
103
107
|
/** Contract shape — hours-based or day-count. */
|
|
104
108
|
export interface EmployeeContract {
|
|
105
109
|
kind: 'hours' | 'days';
|
|
106
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* Contracted minutes per week. Sets the person's overtime baseline, their
|
|
112
|
+
* pro-rata fairness share and — through `rules.contract` and the
|
|
113
|
+
* contract-hours objective — the period total the solver plans them
|
|
114
|
+
* towards. Takes precedence over `fte`.
|
|
115
|
+
*/
|
|
107
116
|
weeklyMinutes?: number;
|
|
117
|
+
/**
|
|
118
|
+
* Fraction of a full-time week, `0 < fte <= 1` (0.5 is half-time). Resolved
|
|
119
|
+
* to weekly minutes against `rules.contract.fullTimeWeeklyMinutes`, falling
|
|
120
|
+
* back to `rules.overtime.ordinaryPerWeekMinutes`; with neither the engine
|
|
121
|
+
* rejects the input rather than guess a working week. Ignored when
|
|
122
|
+
* `weeklyMinutes` is set.
|
|
123
|
+
*/
|
|
124
|
+
fte?: number;
|
|
108
125
|
/** Day-count contracts: maximum working days in the period (e.g. FR forfait jours). */
|
|
109
126
|
maxDaysInPeriod?: number;
|
|
110
127
|
/** Hard bounds over the period, in minutes. */
|
|
@@ -156,6 +173,22 @@ export interface TimeOffEntry {
|
|
|
156
173
|
/** Optional shift-instance id (`<templateId>@<date>`); when set only that instance is blocked. */
|
|
157
174
|
shiftInstanceId?: string;
|
|
158
175
|
}
|
|
176
|
+
/** A physical site, with optional coordinates and an asymmetric travel-time matrix. */
|
|
177
|
+
export interface Site {
|
|
178
|
+
id: string;
|
|
179
|
+
lat?: number;
|
|
180
|
+
lng?: number;
|
|
181
|
+
/** Travel time from this site to another site, in minutes. Takes precedence over haversine estimates. */
|
|
182
|
+
travelMinutesTo?: Record<string, number>;
|
|
183
|
+
}
|
|
184
|
+
/** Indexed view of `ScheduleInput.sites` used by constraints and ranking. */
|
|
185
|
+
export interface SiteIndex {
|
|
186
|
+
byId: Map<string, Site>;
|
|
187
|
+
/** Caller-supplied fallback speed for deriving minutes from haversine kilometres. */
|
|
188
|
+
travelSpeedKmh?: number;
|
|
189
|
+
/** Precomputed travel minutes for every ordered site pair with resolvable data. */
|
|
190
|
+
resolvedTravelMinutes: Map<string, Map<string, number>>;
|
|
191
|
+
}
|
|
159
192
|
/**
|
|
160
193
|
* The labour-law layer, as plain typed configuration.
|
|
161
194
|
*
|
|
@@ -176,6 +209,8 @@ export interface WorkingTimeRules {
|
|
|
176
209
|
workingTime?: WorkingTimeLimits;
|
|
177
210
|
/** The ordinary-vs-overtime split and its caps. */
|
|
178
211
|
overtime?: OvertimeRule;
|
|
212
|
+
/** Contracted hours: the full-time week `fte` is a fraction of, and how far a plan may stray from a person's contract. */
|
|
213
|
+
contract?: ContractHoursRule;
|
|
179
214
|
/** Rolling volume caps on particular duty types, matched on `shiftTypeTag`. */
|
|
180
215
|
dutyQuotas?: DutyQuota[];
|
|
181
216
|
consecutive?: ConsecutiveRule;
|
|
@@ -317,6 +352,42 @@ export interface OvertimeRule {
|
|
|
317
352
|
/** Legal source echoed into verdicts and violations. */
|
|
318
353
|
citation?: string;
|
|
319
354
|
}
|
|
355
|
+
/**
|
|
356
|
+
* Contracted hours as a planning fact.
|
|
357
|
+
*
|
|
358
|
+
* A contracted week already sets a person's overtime baseline (`overtime`) and
|
|
359
|
+
* pro-rata fairness share (`fairness[].proRataByContract`). This family makes
|
|
360
|
+
* it steer the roster itself. The contracted *period* total is
|
|
361
|
+
* `weeklyMinutes × periodDays / 7`; the solver's soft objective pulls every
|
|
362
|
+
* person with a known contract towards that total (weight
|
|
363
|
+
* `objectives.contractHoursWeight`), so a half-time worker is planned half the
|
|
364
|
+
* hours of a full-timer rather than filled to the statutory ceiling.
|
|
365
|
+
*
|
|
366
|
+
* The two bounds are optional because whether over-contract time is a breach
|
|
367
|
+
* or merely overtime is the caller's regime: a workspace whose `overtime` rule
|
|
368
|
+
* governs the surplus omits `maxOverMinutes`; one whose part-timers must not
|
|
369
|
+
* exceed their agreed hours sets it to 0.
|
|
370
|
+
*/
|
|
371
|
+
export interface ContractHoursRule {
|
|
372
|
+
/**
|
|
373
|
+
* The full-time week that `contract.fte` is a fraction of, in minutes.
|
|
374
|
+
* Falls back to `overtime.ordinaryPerWeekMinutes`. Required (one or the
|
|
375
|
+
* other) for any employee with an `fte`.
|
|
376
|
+
*/
|
|
377
|
+
fullTimeWeeklyMinutes?: number;
|
|
378
|
+
/**
|
|
379
|
+
* Hard cap: minutes a person may be planned *over* their contracted period
|
|
380
|
+
* total. `0` means never over contract. Omit for no hard cap.
|
|
381
|
+
*/
|
|
382
|
+
maxOverMinutes?: number;
|
|
383
|
+
/**
|
|
384
|
+
* Soft report: a shortfall larger than this against the contracted period
|
|
385
|
+
* total is surfaced as a violation. Omit to leave shortfalls to the
|
|
386
|
+
* objective and the result summary.
|
|
387
|
+
*/
|
|
388
|
+
maxUnderMinutes?: number;
|
|
389
|
+
citation?: string;
|
|
390
|
+
}
|
|
320
391
|
/**
|
|
321
392
|
* A rolling volume cap on one duty type — "at most N hours (or N occurrences)
|
|
322
393
|
* of stand-by in any 28 days". Matched on `ShiftTemplate.shiftTypeTag`.
|
|
@@ -600,6 +671,10 @@ export interface ScheduleInput {
|
|
|
600
671
|
timeBudgetMs?: number;
|
|
601
672
|
/** The labour-law layer. Omit for a plain feasibility solve. */
|
|
602
673
|
rules?: WorkingTimeRules;
|
|
674
|
+
/** Site registry. Required for multi-site travel-gap, distance-aware ranking and home-site preferences. */
|
|
675
|
+
sites?: Site[];
|
|
676
|
+
/** Fallback speed (km/h) used to derive travel minutes from haversine distance when a matrix entry is absent. */
|
|
677
|
+
travelSpeedKmh?: number;
|
|
603
678
|
/** Public holidays and closures, as ISO dates. */
|
|
604
679
|
calendar?: {
|
|
605
680
|
publicHolidays?: string[];
|
|
@@ -648,6 +723,8 @@ export interface HistoricalAssignment {
|
|
|
648
723
|
/** Working minutes, if they differ from the elapsed span. */
|
|
649
724
|
workingMinutes?: number;
|
|
650
725
|
shiftTypeTag?: string;
|
|
726
|
+
/** Site where the historical duty took place, so cross-site travel gaps can be enforced at the period boundary. */
|
|
727
|
+
siteId?: string;
|
|
651
728
|
id?: string;
|
|
652
729
|
}
|
|
653
730
|
/** One rostered (employee, shift occurrence) pair. */
|
|
@@ -728,6 +805,8 @@ export interface ScheduleResult {
|
|
|
728
805
|
totalCents: number;
|
|
729
806
|
byEmployee: Record<string, number>;
|
|
730
807
|
};
|
|
808
|
+
/** Planned against contracted hours, for every employee whose contracted week resolves. */
|
|
809
|
+
contractHours?: ContractHoursSummary[];
|
|
731
810
|
}
|
|
732
811
|
/** Identifying stamp for a solve. */
|
|
733
812
|
export interface ScheduleProvenance {
|
|
@@ -761,6 +840,33 @@ export interface ObjectiveWeights {
|
|
|
761
840
|
* summary still use the cost model either way.
|
|
762
841
|
*/
|
|
763
842
|
costWeightPerEuro?: number;
|
|
843
|
+
/**
|
|
844
|
+
* Soft-score points per assignment away from an employee's `homeSiteId`.
|
|
845
|
+
* When set positive, the solver prefers assigning people to their home site.
|
|
846
|
+
* The ranking function does **not** consume behavioural or predictive signals;
|
|
847
|
+
* this is declared data, kept outside AI Act Annex III point 4(b).
|
|
848
|
+
*/
|
|
849
|
+
homeSiteWeight?: number;
|
|
850
|
+
/**
|
|
851
|
+
* Soft-score points per *hour* a person is planned away from their
|
|
852
|
+
* contracted period total (see `ContractHoursRule`). Applies only to
|
|
853
|
+
* employees whose contracted week resolves. Defaults to 1; `0` switches the
|
|
854
|
+
* term off.
|
|
855
|
+
*/
|
|
856
|
+
contractHoursWeight?: number;
|
|
857
|
+
}
|
|
858
|
+
/** One person's planned hours set against their contract. */
|
|
859
|
+
export interface ContractHoursSummary {
|
|
860
|
+
employeeId: string;
|
|
861
|
+
/** The contracted week, explicit or resolved from `fte`. */
|
|
862
|
+
weeklyMinutes: number;
|
|
863
|
+
/** As supplied, or derived from `weeklyMinutes` when a full-time week is known. */
|
|
864
|
+
fte?: number;
|
|
865
|
+
/** The contracted week pro-rated to the period. */
|
|
866
|
+
contractedMinutes: number;
|
|
867
|
+
plannedMinutes: number;
|
|
868
|
+
/** `plannedMinutes − contractedMinutes`; negative is a shortfall. */
|
|
869
|
+
deltaMinutes: number;
|
|
764
870
|
}
|
|
765
871
|
/** A dated obligation created by an assignment. */
|
|
766
872
|
export interface LedgerEntry {
|
|
@@ -831,6 +937,8 @@ export interface ModelContext {
|
|
|
831
937
|
employeeTags: Map<string, Set<string>>;
|
|
832
938
|
instances: ShiftInstance[];
|
|
833
939
|
instanceById: Map<string, ShiftInstance>;
|
|
940
|
+
/** Site registry built from `ScheduleInput.sites`. */
|
|
941
|
+
siteIndex: SiteIndex;
|
|
834
942
|
/** Minutes in [0, periodDays*1440) the employee is blocked by time-off. */
|
|
835
943
|
employeeBlockedIntervals: Map<string, Array<{
|
|
836
944
|
start: number;
|
|
@@ -844,6 +952,17 @@ export interface ModelContext {
|
|
|
844
952
|
/** Rules after merging the global set with each person's overrides. */
|
|
845
953
|
rules: WorkingTimeRules;
|
|
846
954
|
rulesByEmployee: Map<string, WorkingTimeRules>;
|
|
955
|
+
/**
|
|
956
|
+
* Each employee's contracted week in minutes — explicit `weeklyMinutes` or
|
|
957
|
+
* `fte` resolved against the full-time week — for those who have one. Every
|
|
958
|
+
* rule that reads a contracted week reads it here, so `fte` and
|
|
959
|
+
* `weeklyMinutes` can never disagree between overtime and fairness.
|
|
960
|
+
*/
|
|
961
|
+
contractedWeeklyMinutes: Map<string, number>;
|
|
962
|
+
/** The contracted week pro-rated to the period, for the same employees. */
|
|
963
|
+
contractedPeriodMinutes: Map<string, number>;
|
|
964
|
+
/** Resolved `objectives.contractHoursWeight`. */
|
|
965
|
+
contractHoursWeight: number;
|
|
847
966
|
/** Employee id → the natural person it belongs to (CJEU C-585/19). */
|
|
848
967
|
personIdOf: Map<string, string>;
|
|
849
968
|
/** Person id → the employee records that share it. */
|