Skip to content

Commit 903a009

Browse files
committed
fix: treat future lastRunMs as never-run in isTaskDue (clock skew)
1 parent db81eeb commit 903a009

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎src/auto-reply/heartbeat.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
22
import {
33
DEFAULT_HEARTBEAT_ACK_MAX_CHARS,
44
isHeartbeatContentEffectivelyEmpty,
5+
isTaskDue,
56
parseHeartbeatTasks,
67
stripHeartbeatToken,
78
} from "./heartbeat.js";
@@ -282,3 +283,24 @@ interval: should-not-bleed
282283
]);
283284
});
284285
});
286+
287+
describe("isTaskDue", () => {
288+
it("returns true when task has never run", () => {
289+
expect(isTaskDue(undefined, "30m", Date.now())).toBe(true);
290+
});
291+
292+
it("returns true when interval has elapsed", () => {
293+
const now = 1_000_000;
294+
expect(isTaskDue(now - 31 * 60 * 1000, "30m", now)).toBe(true);
295+
});
296+
297+
it("returns false when interval has not elapsed", () => {
298+
const now = 1_000_000;
299+
expect(isTaskDue(now - 10 * 60 * 1000, "30m", now)).toBe(false);
300+
});
301+
302+
it("returns true when lastRunMs is in the future (clock skew)", () => {
303+
const now = 1_000_000;
304+
expect(isTaskDue(now + 5 * 60 * 1000, "30m", now)).toBe(true);
305+
});
306+
});

‎src/auto-reply/heartbeat.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ export function parseHeartbeatTasks(content: string): HeartbeatTask[] {
285285
* Check if a task is due based on its interval and last run time.
286286
*/
287287
export function isTaskDue(lastRunMs: number | undefined, interval: string, nowMs: number): boolean {
288-
if (lastRunMs === undefined) {
289-
return true; // Never run, always due
288+
if (lastRunMs === undefined || lastRunMs > nowMs) {
289+
return true; // Never run, or clock moved backward — treat as due
290290
}
291291

292292
try {

0 commit comments

Comments
 (0)