Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/server/src/scheduledTasks/Schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,17 @@ describe("scheduled task schedule calculation", () => {
),
).toBe(false);
});

it.each([
["9:00", "09:00"],
["09:00", "9:00"],
["0:30", "00:30"],
])("treats %s and %s as the same fixed-time schedule", (before, after) => {
expect(
isSameSchedule(
{ type: "fixed_time", timeOfDay: before },
{ type: "fixed_time", timeOfDay: after },
),
).toBe(true);
});
});
12 changes: 10 additions & 2 deletions apps/server/src/scheduledTasks/Schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ export function isSameSchedule(a: ScheduledTaskSchedule, b: ScheduledTaskSchedul
if (a.type === "interval") {
return b.type === "interval" && a.everyMs === b.everyMs;
}
if (b.type !== "fixed_time") return false;
// The contract accepts padded and unpadded hours ("9:00" and "09:00"), so
// compare the parsed time — string equality would treat a format-only edit
// as a schedule change and recompute the pending run.
const aTime = parseTimeOfDay(a.timeOfDay);
const bTime = parseTimeOfDay(b.timeOfDay);
return (
b.type === "fixed_time" &&
a.timeOfDay === b.timeOfDay &&
aTime !== null &&
bTime !== null &&
aTime.hour === bTime.hour &&
aTime.minute === bTime.minute &&
weekdayKey(a.weekdays) === weekdayKey(b.weekdays)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import { expect, it } from "@effect/vitest";
import { ScheduledTaskUpsertInput } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as TestClock from "effect/testing/TestClock";

import { ThreadLaunchService } from "../orchestration-v2/ThreadLaunchService.ts";
import { ThreadManagementService } from "../orchestration-v2/ThreadManagementService.ts";
import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
import * as ScheduledTaskService from "./ScheduledTaskService.ts";

const decodeUpsertInput = Schema.decodeUnknownEffect(ScheduledTaskUpsertInput);

it.effect("preserves a due run when a save only pads the scheduled hour", () =>
Effect.gen(function* () {
const dueAt = DateTime.makeZonedUnsafe(
{ year: 2026, month: 7, day: 1, hour: 9, minute: 0, second: 0, millisecond: 0 },
{ timeZone: DateTime.zoneMakeLocal(), adjustForTimeZone: true },
);
yield* TestClock.setTime(DateTime.toEpochMillis(dueAt) - 1_000);

const dependencies = Layer.mergeAll(
NodeCrypto.layer,
Layer.mock(ThreadLaunchService)({}),
Layer.mock(ThreadManagementService)({}),
);
yield* Effect.gen(function* () {
const service = yield* ScheduledTaskService.ScheduledTaskService;
const input = yield* decodeUpsertInput({
commandId: "schedule-time-format",
title: "Morning review",
prompt: "Review the open pull requests.",
enabled: true,
schedule: { type: "fixed_time", timeOfDay: "9:00" },
projectId: "project-schedule-time-format",
workspaceStrategy: { type: "root" },
modelSelection: { instanceId: "codex", model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
creationSource: "mcp",
});
const created = yield* service.upsert(input);
const expectedDueAt = DateTime.formatIso(DateTime.toUtc(dueAt));
expect(created.task.nextRunAt).toBe(expectedDueAt);

// Cross the due time before the scheduler's first five-second tick.
yield* TestClock.setTime(DateTime.toEpochMillis(dueAt) + 1_000);
const update = yield* decodeUpsertInput({
...input,
id: created.task.id,
schedule: { type: "fixed_time", timeOfDay: "09:00" },
});
const updated = yield* service.upsert(update);
expect(updated.task.nextRunAt).toBe(expectedDueAt);
expect((yield* service.list()).tasks[0]?.nextRunAt).toBe(expectedDueAt);

const rescheduled = yield* service.upsert({
...update,
schedule: { type: "fixed_time", timeOfDay: "09:30" },
});
expect(rescheduled.task.nextRunAt).toBe(
DateTime.formatIso(DateTime.toUtc(DateTime.add(dueAt, { minutes: 30 }))),
);
}).pipe(Effect.provide(ScheduledTaskService.layer.pipe(Layer.provide(dependencies))));
}).pipe(Effect.provide(SqlitePersistenceMemory)),
);
Loading