Skip to content

Commit 01a7974

Browse files
fix(scheduled-tasks): keep window openings in the future across fall-back, show unpadded times
Two Macroscope findings on the scheduled-task restrictions: - In a repeated fall-back hour, the restricted interval schedule added the interval with calendar arithmetic, which re-resolves the repeated hour to its first occurrence. The next run could land before the current time, so a window opening inside that hour kept the task due and rescheduled on every poll. The restricted path now adds elapsed time, and a window opening that resolves before the candidate steps forward on the candidate's own offset. - The contract accepts window times such as "9:00", but the web editor passed them unchanged to native time inputs, which only display zero-padded HH:mm, so the configured window showed as blank. The editor now pads them when it reads a task. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 3c9a91e commit 01a7974

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎apps/server/src/scheduledTasks/Schedule.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ describe("interval restrictions at dispatch and across DST", () => {
284284
expect(next && DateTime.toParts(next)).toMatchObject({ day: 9, hour: 2, minute: 10 });
285285
});
286286

287+
it("opens a window at the later 01:30 when a fall-back hour repeats", () => {
288+
// 2026-11-01 01:00-02:00 occurs twice in New York; 06:05Z is the
289+
// repeated 01:05 (EST), after the first 01:30 (EDT) has passed.
290+
const from = DateTime.makeZonedUnsafe("2026-11-01T06:05:00Z", {
291+
timeZone: "America/New_York",
292+
});
293+
expect(DateTime.toParts(from)).toMatchObject({ hour: 1, minute: 5 });
294+
const next = nextScheduledRunAt(
295+
{ type: "interval", everyMs: 15 * 60_000, window: { start: "01:30", end: "03:00" } },
296+
from,
297+
);
298+
expect(next).not.toBeNull();
299+
expect(next && DateTime.toEpochMillis(next)).toBe(DateTime.toEpochMillis(from) + 25 * 60_000);
300+
});
301+
287302
it("finds the next Sunday window even when it opens inside a spring-forward gap", () => {
288303
const from = zoned("America/New_York", { year: 2026, month: 3, day: 1, hour: 5, minute: 0 });
289304
const next = nextScheduledRunAt(

‎apps/server/src/scheduledTasks/Schedule.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ function nextRestrictedIntervalRun(
4040
schedule.weekdays && schedule.weekdays.length > 0 ? new Set(schedule.weekdays) : null;
4141
const windowStart = schedule.window ? minutesOfDay(schedule.window.start) : null;
4242
const windowEnd = schedule.window ? minutesOfDay(schedule.window.end) : null;
43-
let candidate = DateTime.add(from, { milliseconds: everyMs });
43+
// Elapsed-time arithmetic: a calendar add would re-resolve a repeated
44+
// fall-back hour to its first occurrence and step backwards.
45+
let candidate = DateTime.addDuration(from, everyMs);
4446
for (let hop = 0; hop < 16; hop += 1) {
4547
if (weekdays !== null && !weekdays.has(DateTime.toParts(candidate).weekDay)) {
4648
candidate = startOfNextDay(candidate);
@@ -55,12 +57,22 @@ function nextRestrictedIntervalRun(
5557
continue;
5658
}
5759
if (local >= windowStart) return candidate;
58-
const opensAt = DateTime.setParts(atMidnight(candidate), {
60+
const wallClockOpening = DateTime.setParts(atMidnight(candidate), {
5961
hour: Math.floor(windowStart / 60),
6062
minute: windowStart % 60,
6163
second: 0,
6264
millisecond: 0,
6365
});
66+
// In a fall-back overlap the wall-clock opening resolves to its earlier
67+
// occurrence, which can precede the candidate; step forward on the
68+
// candidate's own offset to the repeated opening instead.
69+
const { second, millisecond } = DateTime.toParts(candidate);
70+
const opensAt = DateTime.isGreaterThan(wallClockOpening, candidate)
71+
? wallClockOpening
72+
: DateTime.addDuration(
73+
candidate,
74+
(windowStart - local) * MINUTE_MS - second * 1000 - millisecond,
75+
);
6476
// A window opening inside a spring-forward gap does not exist that day.
6577
if (localMinutes(opensAt) === windowStart) return opensAt;
6678
candidate = startOfNextDay(candidate);

‎apps/web/src/components/settings/scheduledTasksSettings.logic.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,17 @@ describe("interval restrictions round-trip through the draft", () => {
196196
expect(scheduleFromDraft(draft)).toEqual(restrictedTask.schedule);
197197
});
198198

199+
it("pads a single-digit window hour so the time inputs can show it", () => {
200+
// The contract accepts "9:00" (for example from an MCP client), but a
201+
// native time input only displays zero-padded HH:mm values.
202+
const draft = taskToDraft({
203+
...restrictedTask,
204+
schedule: { type: "interval", everyMs: 1_800_000, window: { start: "9:00", end: "17:00" } },
205+
});
206+
expect(draft.windowStart).toBe("09:00");
207+
expect(draft.windowEnd).toBe("17:00");
208+
});
209+
199210
it("reads an unrestricted interval as windowless with no cap", () => {
200211
const draft = taskToDraft(legacyTask);
201212
expect(draft.intervalWeekdays.size).toBe(0);

‎apps/web/src/components/settings/scheduledTasksSettings.logic.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ export function runCapReached(task: ScheduledTask): boolean {
150150
return task.schedule.maxRuns !== undefined && task.runCount >= task.schedule.maxRuns;
151151
}
152152

153+
/** "9:00" -> "09:00": native time inputs only display zero-padded HH:mm. */
154+
function paddedTime(value: string): string {
155+
const [hours, minutes] = value.trim().split(":");
156+
return hours !== undefined && minutes !== undefined
157+
? `${hours.padStart(2, "0")}:${minutes}`
158+
: value;
159+
}
160+
153161
export function taskToDraft(task: ScheduledTask): DraftState {
154162
const schedule = task.schedule;
155163
const weekdays =
@@ -167,8 +175,10 @@ export function taskToDraft(task: ScheduledTask): DraftState {
167175
schedule.type === "interval" ? String(Math.max(1, schedule.everyMs / 60_000)) : "15",
168176
intervalWeekdays: new Set(schedule.type === "interval" ? (schedule.weekdays ?? []) : []),
169177
windowEnabled: schedule.type === "interval" && schedule.window !== undefined,
170-
windowStart: schedule.type === "interval" && schedule.window ? schedule.window.start : "09:00",
171-
windowEnd: schedule.type === "interval" && schedule.window ? schedule.window.end : "17:00",
178+
windowStart:
179+
schedule.type === "interval" && schedule.window ? paddedTime(schedule.window.start) : "09:00",
180+
windowEnd:
181+
schedule.type === "interval" && schedule.window ? paddedTime(schedule.window.end) : "17:00",
172182
maxRuns: schedule.maxRuns === undefined ? "" : String(schedule.maxRuns),
173183
timeOfDay: schedule.type === "fixed_time" ? schedule.timeOfDay : "09:00",
174184
weekdays,

0 commit comments

Comments
 (0)