Skip to content

Commit b427c6c

Browse files
committed
fix(gateway): recover running health monitors after clock rollback
1 parent 79b4bad commit b427c6c

2 files changed

Lines changed: 107 additions & 4 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import type { ChannelAccountSnapshot } from "../channels/plugins/types.public.js";
3+
import { startChannelHealthMonitor } from "./channel-health-monitor.js";
4+
import type { ChannelManager } from "./server-channels.js";
5+
6+
const STARTED_AT = 1_000_000;
7+
const CHECK_INTERVAL_MS = 1_000;
8+
9+
function createChannelManager(running: boolean) {
10+
const account: ChannelAccountSnapshot = {
11+
accountId: "default",
12+
running,
13+
connected: running,
14+
enabled: true,
15+
configured: true,
16+
};
17+
const snapshot = {
18+
channels: { discord: account },
19+
channelAccounts: { discord: { default: account } },
20+
};
21+
const manager: ChannelManager = {
22+
getRuntimeSnapshot: vi.fn(() => snapshot),
23+
getPluginCommandCatalogAccounts: vi.fn(() => new Map()),
24+
startChannels: vi.fn(async () => {}),
25+
startChannel: vi.fn(async () => {}),
26+
stopChannel: vi.fn(async () => {}),
27+
setAutostartSuppression: vi.fn(),
28+
getAutostartSuppression: vi.fn(() => null),
29+
recoverAutostartSuppression: vi.fn(async () => false),
30+
setAmbientAutostartSuppressedChannelIds: vi.fn(),
31+
isAmbientAutostartSuppressed: vi.fn(() => false),
32+
markChannelLoggedOut: vi.fn(),
33+
isHealthMonitorEnabled: vi.fn(() => true),
34+
isManuallyStopped: vi.fn(() => false),
35+
isAutoRestartScheduled: vi.fn(() => false),
36+
resetRestartAttempts: vi.fn(),
37+
};
38+
return { account, manager };
39+
}
40+
41+
describe("channel-health-monitor clock rollback", () => {
42+
beforeEach(() => {
43+
vi.useFakeTimers();
44+
vi.setSystemTime(STARTED_AT);
45+
});
46+
47+
afterEach(() => {
48+
vi.useRealTimers();
49+
});
50+
51+
it("does not trap an existing monitor in startup grace after clock rollback", async () => {
52+
const { manager } = createChannelManager(false);
53+
const monitor = startChannelHealthMonitor({
54+
channelManager: manager,
55+
checkIntervalMs: CHECK_INTERVAL_MS,
56+
timing: { monitorStartupGraceMs: CHECK_INTERVAL_MS },
57+
});
58+
59+
vi.setSystemTime(STARTED_AT - 60_000);
60+
await vi.advanceTimersByTimeAsync(CHECK_INTERVAL_MS);
61+
62+
expect(manager.startChannel).toHaveBeenCalledWith("discord", "default");
63+
monitor.stop();
64+
});
65+
66+
it.each([
67+
{ name: "restart cooldown", maxRestartsPerHour: 10 },
68+
{ name: "hourly restart budget", maxRestartsPerHour: 1 },
69+
])("discards future $name from an existing monitor", async ({ maxRestartsPerHour }) => {
70+
const { account, manager } = createChannelManager(true);
71+
const monitor = startChannelHealthMonitor({
72+
channelManager: manager,
73+
checkIntervalMs: CHECK_INTERVAL_MS,
74+
cooldownCycles: 0,
75+
maxRestartsPerHour,
76+
timing: { monitorStartupGraceMs: 0 },
77+
});
78+
79+
await vi.advanceTimersByTimeAsync(5 * CHECK_INTERVAL_MS);
80+
account.running = false;
81+
account.connected = false;
82+
await vi.advanceTimersByTimeAsync(CHECK_INTERVAL_MS);
83+
expect(manager.startChannel).toHaveBeenCalledTimes(1);
84+
85+
vi.setSystemTime(STARTED_AT + 3 * CHECK_INTERVAL_MS);
86+
await vi.advanceTimersByTimeAsync(CHECK_INTERVAL_MS);
87+
88+
expect(manager.startChannel).toHaveBeenCalledTimes(2);
89+
monitor.stop();
90+
});
91+
});

‎src/gateway/channel-health-monitor.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
1+
import {
2+
isFutureDateTimestampMs,
3+
resolveTimerTimeoutMs,
4+
} from "@openclaw/normalization-core/number-coercion";
25
// Gateway channel health monitor.
36
// Periodically evaluates channel account health and restarts stale runtimes.
47
import type { ChannelId } from "../channels/plugins/types.public.js";
@@ -90,13 +93,18 @@ export function startChannelHealthMonitor(deps: ChannelHealthMonitorDeps): Chann
9093
const rKey = (channelId: string, accountId: string) => `${channelId}:${accountId}`;
9194

9295
function pruneOldRestarts(record: RestartRecord, now: number) {
93-
record.restartsThisHour = record.restartsThisHour.filter((r) => now - r.at < ONE_HOUR_MS);
96+
record.restartsThisHour = record.restartsThisHour.filter(
97+
(r) => !isFutureDateTimestampMs(r.at, { nowMs: now }) && now - r.at < ONE_HOUR_MS,
98+
);
9499
}
95100

96101
async function runCheckWork() {
97102
try {
98103
const now = Date.now();
99-
if (now - startedAt < timing.monitorStartupGraceMs) {
104+
if (
105+
!isFutureDateTimestampMs(startedAt, { nowMs: now }) &&
106+
now - startedAt < timing.monitorStartupGraceMs
107+
) {
100108
return;
101109
}
102110

@@ -188,7 +196,11 @@ export function startChannelHealthMonitor(deps: ChannelHealthMonitorDeps): Chann
188196
const continuingPendingRestart =
189197
pendingRestartState && record.pendingContinuationUsed !== true;
190198

191-
if (!continuingPendingRestart && now - record.lastRestartAt <= cooldownMs) {
199+
if (
200+
!continuingPendingRestart &&
201+
!isFutureDateTimestampMs(record.lastRestartAt, { nowMs: now }) &&
202+
now - record.lastRestartAt <= cooldownMs
203+
) {
192204
continue;
193205
}
194206

0 commit comments

Comments
 (0)