fix(planning): name a reserve that fails jointly and refuse alternative dates that break one (#376)
Two findings of the independent verification. An obligation whose reserve holds for each occurrence alone but not for all together ended in the generic joint-date conflict. When no single occurrence fails and an obligation IF_THEN rule carries a reserve, the conflict path now searches the obligations once more with the obligation reserves ignored, on its own context and stopping at the first solution. Placements are only added along the obligation search, so a solution found there breaks at least one reserve: the conflict names the first occurrence whose reserve does not hold in it. Without such a solution the conflict is reported as before, and requests without a reserve-carrying obligation take the same path with the same steps. The alternative-date analysis accepted a move that left an earned day in place while its reserve no longer held. A date is now ineligible when an activation of a reserve-carrying rule is no longer satisfied. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0192He3CzBZP96zgTG9hCgLw
This commit is contained in:
1 parent
2fe9a02f72
commit
9c06516090
2 files changed
+184
-32
No files matched your search
@@ -4722,10 +4722,11 @@ function conflictExplanation(
|
||||
};
|
||||
}
|
||||
|
||||
function obligationConflict(
|
||||
function* obligationConflict(
|
||||
requirements: readonly Requirement[],
|
||||
context: SearchContext,
|
||||
): PlanningConflictOutcome {
|
||||
jointReserve?: PlanningSteps<PlanningConflictOutcome | undefined>,
|
||||
): PlanningSteps<PlanningConflictOutcome> {
|
||||
const ruleIds = [...new Set(requirements.map((requirement) => requirement.rule.id))];
|
||||
const generatedTypes = new Set(
|
||||
context.rules
|
||||
@@ -4781,7 +4782,9 @@ function obligationConflict(
|
||||
),
|
||||
],
|
||||
};
|
||||
const reserveConflict = obligationReserveConflict(requirements, context);
|
||||
const reserveConflict =
|
||||
obligationReserveConflict(requirements, context) ??
|
||||
(jointReserve ? yield* jointReserve : undefined);
|
||||
if (reserveConflict) return reserveConflict;
|
||||
if (generatedCapacityBottleneck)
|
||||
return {
|
||||
@@ -4836,27 +4839,73 @@ function obligationReserveConflict(
|
||||
return reserveRemaining(rule, occurrence, state, context);
|
||||
};
|
||||
if (patterns.some((pattern) => remainingWith(pattern) >= rule.reserveDays!)) continue;
|
||||
const remaining = remainingWith(
|
||||
patterns.find((pattern) => open(pattern).length === requiredPlacements)!,
|
||||
return reserveConflictOutcome(
|
||||
rule,
|
||||
occurrence,
|
||||
remainingWith(patterns.find((pattern) => open(pattern).length === requiredPlacements)!),
|
||||
);
|
||||
return {
|
||||
kind: 'PlanningConflict',
|
||||
conflictingRuleIds: [rule.id],
|
||||
reasons: [
|
||||
{
|
||||
...reason(
|
||||
'if-then-reserve',
|
||||
`IF_THEN rule "${rule.name}" needs a reserve of ${rule.reserveDays} ${rule.placeType.toUpperCase()} days on ${occurrence}, but only ${remaining} would remain.`,
|
||||
[rule.id],
|
||||
),
|
||||
reserve: { occurrence, remaining, required: rule.reserveDays },
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obligations that fail only together because of their reserves: search them once more with the
|
||||
* obligation reserves at zero, stopping at the first solution. A solution keeping every reserve
|
||||
* would have been found with them, so this one breaks one; report the first, in rule priority then
|
||||
* occurrence order. Undefined when the obligations fail even without their reserves.
|
||||
*/
|
||||
function* jointReserveConflict(
|
||||
requirements: readonly Requirement[],
|
||||
state: State,
|
||||
context: SearchContext,
|
||||
): PlanningSteps<PlanningConflictOutcome | undefined> {
|
||||
const reserved = (rule: NormalizedRule): rule is NormalizedCalendarRule =>
|
||||
rule.type === 'if_then' && rule.force === 'obligation' && !!rule.reserveDays;
|
||||
const rules = context.rules.map((rule) =>
|
||||
reserved(rule) ? { ...rule, reserveDays: 0 } : rule,
|
||||
);
|
||||
// Every cache and memo is keyed on the context, so a context of its own inherits no verdict
|
||||
// reached with the reserves; it keeps the deadline and retains no incumbent.
|
||||
const relaxed: SearchContext = { ...context, rules, retain: undefined };
|
||||
const solution = yield* satisfyObligations(
|
||||
requirements.map((requirement) => ({
|
||||
...requirement,
|
||||
rule: rules[context.rules.indexOf(requirement.rule)] as NormalizedCalendarRule,
|
||||
})),
|
||||
state,
|
||||
relaxed,
|
||||
);
|
||||
if (!solution) return undefined;
|
||||
for (const rule of context.rules.filter(reserved))
|
||||
for (const occurrence of occurrences(rule, context)) {
|
||||
const remaining = reserveRemaining(rule, occurrence, solution, context);
|
||||
if (remaining < rule.reserveDays!)
|
||||
return reserveConflictOutcome(rule, occurrence, remaining);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function reserveConflictOutcome(
|
||||
rule: NormalizedCalendarRule,
|
||||
occurrence: string,
|
||||
remaining: number,
|
||||
): PlanningConflictOutcome {
|
||||
return {
|
||||
kind: 'PlanningConflict',
|
||||
conflictingRuleIds: [rule.id],
|
||||
reasons: [
|
||||
{
|
||||
...reason(
|
||||
'if-then-reserve',
|
||||
`IF_THEN rule "${rule.name}" needs a reserve of ${rule.reserveDays} ${rule.placeType.toUpperCase()} days on ${occurrence}, but only ${remaining} would remain.`,
|
||||
[rule.id],
|
||||
),
|
||||
reserve: { occurrence, remaining, required: rule.reserveDays! },
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
export type PlanningCheckpoint = (plan: PlanOutcome, state?: PlanningStateSnapshot) => void;
|
||||
export interface PlanningArtifacts {
|
||||
explanation?: PlanningExplanation;
|
||||
@@ -5324,8 +5373,18 @@ function* calculatePlanningSteps(
|
||||
obligationSolutionLimit,
|
||||
);
|
||||
if (!obligationSolutions.length) {
|
||||
const conflict = yield* obligationConflict(
|
||||
obligationRequirements,
|
||||
context,
|
||||
normalized.rules.some(
|
||||
(rule) =>
|
||||
rule.type === 'if_then' && rule.force === 'obligation' && rule.reserveDays,
|
||||
)
|
||||
? jointReserveConflict(jointRequirements, state, context)
|
||||
: undefined,
|
||||
);
|
||||
stages.push('obligations');
|
||||
return finish(obligationConflict(obligationRequirements, context));
|
||||
return finish(conflict);
|
||||
}
|
||||
const preferenceRules = normalized.rules.filter(
|
||||
(item) => item.enabled && item.force === 'preference',
|
||||
@@ -5396,7 +5455,7 @@ function* calculatePlanningSteps(
|
||||
}
|
||||
// As when every preview failed, several failing solutions report the obligation conflict.
|
||||
if (rankedSolutions.length > 1)
|
||||
return finish(obligationConflict(obligationRequirements, context));
|
||||
return finish(yield* obligationConflict(obligationRequirements, context));
|
||||
return finish({
|
||||
kind: 'PlanningConflict',
|
||||
conflictingRuleIds: state.earnedActivations.map((activation) => activation.ruleId),
|
||||
@@ -5799,6 +5858,9 @@ export function analyzeCounterfactualInternal(
|
||||
);
|
||||
const horizon = normalized.horizon ?? plan.horizon;
|
||||
const context = counterfactualContext(normalized, horizon);
|
||||
const reserveRules = context.rules.filter(
|
||||
(rule): rule is NormalizedCalendarRule => rule.type === 'if_then' && !!rule.reserveDays,
|
||||
);
|
||||
const finalState: State = finalStateSnapshot
|
||||
? stateFromPlanningSnapshot(finalStateSnapshot)
|
||||
: (() => {
|
||||
@@ -5941,17 +6003,28 @@ export function analyzeCounterfactualInternal(
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// The pattern check above cannot see the reserve, which depends on the date's whole plan.
|
||||
const reserveBroken = selected.ruleIds.some((id) => {
|
||||
const rule = context.rules.find((item) => item.id === id);
|
||||
return (
|
||||
rule?.type === 'if_then' &&
|
||||
!!rule.reserveDays &&
|
||||
!occurrences(rule, context).some((occurrence) =>
|
||||
selectedOccurrencePattern(rule, occurrence, finalized, context)?.includes(date),
|
||||
)
|
||||
);
|
||||
});
|
||||
// The checks above cannot see a reserve, which depends on the date's whole plan: the moved
|
||||
// placement must keep its own occurrence, and no earned day may lose its reserve.
|
||||
const reserveBroken = reserveRules.some(
|
||||
(rule) =>
|
||||
(selected.ruleIds.includes(rule.id) &&
|
||||
!occurrences(rule, context).some((occurrence) =>
|
||||
selectedOccurrencePattern(rule, occurrence, finalized, context)?.includes(
|
||||
date,
|
||||
),
|
||||
)) ||
|
||||
finalized.earnedActivations.some(
|
||||
(activation) =>
|
||||
activation.ruleId === rule.id &&
|
||||
!occurrenceSatisfied(
|
||||
rule,
|
||||
activation.occurrence,
|
||||
[activation.dates],
|
||||
finalized,
|
||||
context,
|
||||
),
|
||||
),
|
||||
);
|
||||
if (reserveBroken) {
|
||||
results.push({
|
||||
date,
|
||||
|
||||
@@ -1347,6 +1347,66 @@ describe('Samedi Malin next to another public holiday', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
// Each Saturday keeps 8 - 2 = 6 alone; with 29-30 April and 7 and 10 May placed, 8 May keeps 8 - 4 = 4.
|
||||
['both obligation Saturdays', [], 8, 4],
|
||||
// 1 May's 29-30 April and 7 to 14 May all precede the end of 8 May's period: 9 - 2 - 6 = 1.
|
||||
[
|
||||
'the leave other obligations force on 7 and 10 to 14 May',
|
||||
[
|
||||
obligation('may-7', '2027-05-07', '2027-05-07'),
|
||||
obligation('may-10-14', '2027-05-10', '2027-05-14', 5),
|
||||
],
|
||||
9,
|
||||
1,
|
||||
],
|
||||
] as const)(
|
||||
'names the reserve of 8 May when it fails only with %s placed',
|
||||
async (_, others, quantity, remaining) => {
|
||||
const outcome = await planInProcess(
|
||||
request({
|
||||
balances: [cp(quantity)],
|
||||
rules: [...others, { ...reserved, force: 'obligation' }],
|
||||
}),
|
||||
);
|
||||
expect(outcome).toMatchObject({
|
||||
kind: 'PlanningConflict',
|
||||
conflictingRuleIds: ['samedi-malin'],
|
||||
reasons: [
|
||||
{
|
||||
code: 'if-then-reserve',
|
||||
ruleIds: ['samedi-malin'],
|
||||
reserve: { occurrence: '2027-05-08', remaining, required: 5 },
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it('keeps the reason of an obligation conflict the reserve takes no part in', async () => {
|
||||
// CP and RTT cannot both take 12 May, while 20 CP keep the reserve of both Saturdays.
|
||||
const conflict = (reserveDays: number) =>
|
||||
planInProcess(
|
||||
request({
|
||||
balances: [cp(20), { ...cp(1, 'rtt'), type: 'rtt' }],
|
||||
rules: [
|
||||
{ ...samediMalin, force: 'obligation', reserveDays },
|
||||
obligation('cp-12', '2027-05-12', '2027-05-12'),
|
||||
{
|
||||
...obligation('rtt-12', '2027-05-12', '2027-05-12'),
|
||||
placeType: 'rtt',
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
const expected = {
|
||||
kind: 'PlanningConflict',
|
||||
reasons: [{ code: 'joint-date-conflict' }],
|
||||
};
|
||||
expect(await conflict(0)).toMatchObject(expected);
|
||||
expect(await conflict(5)).toMatchObject(expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[5, []],
|
||||
[6, ['2027-05-01', '2027-05-08']],
|
||||
@@ -1424,6 +1484,25 @@ describe('Samedi Malin next to another public holiday', () => {
|
||||
expect(reasonOn('2027-05-11')).toBe('rule-invalid');
|
||||
});
|
||||
|
||||
it('refuses moving other leave to a date that un-earns the day of 8 May', async () => {
|
||||
const calculation = createInProcessCalculation(
|
||||
request({ rules: [reserved, presets[0]] }),
|
||||
);
|
||||
const result = isPlan(await calculation.outcome);
|
||||
const kept = result.placements.find(({ date }) => date === '2027-05-12')!;
|
||||
expect(kept.ruleIds).toEqual([presets[0].id]);
|
||||
const analysis = await calculation.analyzeCounterfactual(kept);
|
||||
if (analysis.kind !== 'Counterfactual') throw new Error(`Got ${analysis.kind}`);
|
||||
const resultOn = (day: string) => analysis.results.find(({ date }) => date === day);
|
||||
// Before the end of 8 May's period, the day leaves 9 - 5 = 4 of the reserve.
|
||||
for (const day of ['2027-04-26', '2027-05-05'])
|
||||
expect(resultOn(day)).toMatchObject({
|
||||
status: 'ineligible',
|
||||
reason: { code: 'rule-invalid' },
|
||||
});
|
||||
expect(resultOn('2027-05-21')?.status).toBe('eligible');
|
||||
});
|
||||
|
||||
it.each([
|
||||
[9, ['2027-05-01', '2027-05-08']],
|
||||
[4, []],
|
||||
|
||||
Reference in new issue
Block a user