Skip to content

Commit 96e27c6

Browse files
authored
Fix config patch restart-required notices
Merges the Clownfish-repaired contributor branch for #83041. Clownfish merge preflight cleared security/comments/review and accepted pnpm check:changed; the remaining cron shard failure is present on current main.
1 parent 90cf265 commit 96e27c6

5 files changed

Lines changed: 152 additions & 13 deletions

File tree

‎src/gateway/server-methods/config-write-flow.ts‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,40 @@ function queueSharedGatewayAuthGenerationRefresh(
132132
});
133133
}
134134

135-
function shouldScheduleDirectConfigRestart(params: {
135+
function isNoopConfigReloadPlan(plan: ReturnType<typeof buildGatewayReloadPlan>): boolean {
136+
return (
137+
!plan.restartGateway &&
138+
plan.hotReasons.length === 0 &&
139+
!plan.reloadHooks &&
140+
!plan.restartGmailWatcher &&
141+
!plan.restartCron &&
142+
!plan.restartHeartbeat &&
143+
!plan.restartHealthMonitor &&
144+
!plan.reloadPlugins &&
145+
!plan.disposeMcpRuntimes &&
146+
plan.restartChannels.size === 0
147+
);
148+
}
149+
150+
function resolveConfigRestartRequirement(params: {
136151
changedPaths: string[];
137152
nextConfig: OpenClawConfig;
138-
}): boolean {
153+
}): { requiresRestart: boolean; scheduleDirectRestart: boolean } {
139154
const reloadSettings = resolveGatewayReloadSettings(params.nextConfig);
155+
const plan = buildGatewayReloadPlan(params.changedPaths);
156+
if (isNoopConfigReloadPlan(plan)) {
157+
return { requiresRestart: false, scheduleDirectRestart: false };
158+
}
140159
if (reloadSettings.mode === "off") {
141-
return true;
160+
return { requiresRestart: true, scheduleDirectRestart: true };
142161
}
143-
// Hybrid mode lets hot-reload own non-gateway restarts; only paths the reload
144-
// plan marks as gateway-owned get a direct process restart here.
145-
const plan = buildGatewayReloadPlan(params.changedPaths);
146-
if (reloadSettings.mode === "hot" && plan.restartGateway) {
147-
return true;
162+
if (reloadSettings.mode === "restart") {
163+
return { requiresRestart: true, scheduleDirectRestart: false };
164+
}
165+
if (plan.restartGateway) {
166+
return { requiresRestart: true, scheduleDirectRestart: reloadSettings.mode === "hot" };
148167
}
149-
return false;
168+
return { requiresRestart: false, scheduleDirectRestart: false };
150169
}
151170

152171
function resolveConfigRestartRequest(params: unknown): {
@@ -182,6 +201,7 @@ function buildConfigRestartSentinelPayload(params: {
182201
kind: RestartSentinelPayload["kind"];
183202
mode: string;
184203
configPath: string;
204+
requiresRestart: boolean;
185205
sessionKey: string | undefined;
186206
deliveryContext: ReturnType<typeof extractDeliveryInfo>["deliveryContext"];
187207
threadId: ReturnType<typeof extractDeliveryInfo>["threadId"];
@@ -199,6 +219,7 @@ function buildConfigRestartSentinelPayload(params: {
199219
stats: {
200220
mode: params.mode,
201221
root: params.configPath,
222+
requiresRestart: params.requiresRestart,
202223
},
203224
};
204225
}
@@ -260,20 +281,22 @@ export async function resolveGatewayConfigRestartWriteResult(params: {
260281
}> {
261282
const { sessionKey, note, restartDelayMs, deliveryContext, threadId } =
262283
resolveConfigRestartRequest(params.requestParams);
284+
const restartRequirement = resolveConfigRestartRequirement({
285+
changedPaths: params.changedPaths,
286+
nextConfig: params.nextConfig,
287+
});
263288
const payload = buildConfigRestartSentinelPayload({
264289
kind: params.kind,
265290
mode: params.mode,
266291
configPath: params.configPath,
292+
requiresRestart: restartRequirement.requiresRestart,
267293
sessionKey,
268294
deliveryContext,
269295
threadId,
270296
note,
271297
});
272298
const sentinelPersisted = await tryWriteRestartSentinelPayload(payload);
273-
const restart = shouldScheduleDirectConfigRestart({
274-
changedPaths: params.changedPaths,
275-
nextConfig: params.nextConfig,
276-
})
299+
const restart = restartRequirement.scheduleDirectRestart
277300
? scheduleGatewaySigusr1Restart({
278301
delayMs: restartDelayMs,
279302
reason: params.mode,

‎src/gateway/server-methods/config.shared-auth.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,33 @@ describe("config shared auth disconnects", () => {
353353
await runConfigPatch({ gateway: { port: 19001 } });
354354

355355
expect(scheduleGatewaySigusr1RestartMock).toHaveBeenCalledTimes(1);
356+
const payload = restartSentinelMocks.writeRestartSentinel.mock.calls.at(-1)?.[0];
357+
expect(payload?.stats?.requiresRestart).toBe(true);
358+
});
359+
360+
it("marks hot-reloaded config.patch writes as not restart required", async () => {
361+
const prevConfig: OpenClawConfig = {
362+
gateway: {
363+
channelHealthCheckMinutes: 10,
364+
},
365+
};
366+
readConfigFileSnapshotForWriteMock.mockResolvedValue(createConfigWriteSnapshot(prevConfig));
367+
368+
const { options } = createConfigHandlerHarness({
369+
method: "config.patch",
370+
params: {
371+
baseHash: "base-hash",
372+
raw: JSON.stringify({ gateway: { channelHealthCheckMinutes: 15 } }),
373+
restartDelayMs: 1_000,
374+
},
375+
});
376+
377+
await configHandlers["config.patch"](options);
378+
await flushConfigHandlerMicrotasks();
379+
380+
expect(scheduleGatewaySigusr1RestartMock).not.toHaveBeenCalled();
381+
const payload = restartSentinelMocks.writeRestartSentinel.mock.calls.at(-1)?.[0];
382+
expect(payload?.stats?.requiresRestart).toBe(false);
356383
});
357384

358385
it("does not add an agent continuation from generic control-plane sessionKey params", async () => {

‎src/gateway/server-restart-sentinel.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ describe("scheduleRestartSentinelWake", () => {
426426
mocks.finalizeUpdateRestartSentinelRunningVersion.mockReset();
427427
mocks.finalizeUpdateRestartSentinelRunningVersion.mockResolvedValue(null);
428428
mocks.clearRestartSentinel.mockClear();
429+
mocks.formatRestartSentinelMessage.mockClear();
430+
mocks.summarizeRestartSentinel.mockClear();
429431
mocks.injectTimestamp.mockClear();
430432
mocks.timestampOptsFromConfig.mockClear();
431433
mocks.recordInboundSessionAndDispatchReply.mockReset();
@@ -456,6 +458,8 @@ describe("scheduleRestartSentinelWake", () => {
456458
});
457459
expect(mocks.ackDelivery).toHaveBeenCalledWith("queue-1");
458460
expect(mocks.failDelivery).not.toHaveBeenCalled();
461+
expect(mocks.formatRestartSentinelMessage).toHaveBeenCalledWith(expect.anything());
462+
expect(mocks.summarizeRestartSentinel).toHaveBeenCalledWith(expect.anything());
459463
expect(mockCallArg(mocks.enqueueSystemEvent)).toBe("restart message");
460464
expectNthSystemEventFields(0, {
461465
sessionKey: "agent:main:main",

‎src/infra/restart-sentinel.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,36 @@ describe("restart sentinel", () => {
194194
});
195195
});
196196

197+
it("keeps old config restart sentinels readable without restart-required stats", async () => {
198+
await withRestartSentinelStateDir(async () => {
199+
const filePath = path.join(process.env.OPENCLAW_STATE_DIR ?? "", "restart-sentinel.json");
200+
const payload = {
201+
kind: "config-patch" as const,
202+
status: "ok" as const,
203+
ts: Date.now(),
204+
message: "Config updated successfully",
205+
stats: { mode: "config.patch" },
206+
};
207+
await fs.mkdir(path.dirname(filePath), { recursive: true });
208+
await fs.writeFile(filePath, JSON.stringify({ version: 1, payload }, null, 2), "utf-8");
209+
210+
const read = await readRestartSentinel();
211+
212+
expect(read?.payload).toEqual(payload);
213+
if (!read) {
214+
throw new Error("Expected old restart sentinel to be readable");
215+
}
216+
expect(summarizeRestartSentinel(read.payload)).toBe(
217+
"Gateway restart config-patch ok (config.patch)",
218+
);
219+
expect(formatRestartSentinelMessage(read.payload)).toBe(
220+
["Gateway restart config-patch ok (config.patch)", "Config updated successfully"].join(
221+
"\n",
222+
),
223+
);
224+
});
225+
});
226+
197227
it("formatRestartSentinelMessage uses custom message when present", () => {
198228
const payload = {
199229
kind: "config-apply" as const,
@@ -242,6 +272,48 @@ describe("restart sentinel", () => {
242272
expect(result).toContain("Gateway restart");
243273
});
244274

275+
it("formats config write success notices as restart required when marked", () => {
276+
const payload = {
277+
kind: "config-patch" as const,
278+
status: "ok" as const,
279+
ts: Date.now(),
280+
message: "Run restart-gateway.ps1 to apply config changes.",
281+
doctorHint: "Run openclaw doctor --non-interactive",
282+
stats: { mode: "config.patch", requiresRestart: true },
283+
};
284+
285+
expect(formatRestartSentinelMessage(payload)).toBe(
286+
[
287+
"Gateway restart required (config.patch)",
288+
"Run restart-gateway.ps1 to apply config changes.",
289+
"Run openclaw doctor --non-interactive",
290+
].join("\n"),
291+
);
292+
expect(summarizeRestartSentinel(payload)).toBe("Gateway restart required (config.patch)");
293+
294+
expect(
295+
summarizeRestartSentinel({
296+
kind: "config-apply",
297+
status: "ok",
298+
ts: Date.now(),
299+
stats: { mode: "config.apply", requiresRestart: true },
300+
}),
301+
).toBe("Gateway restart required (config.apply)");
302+
});
303+
304+
it("does not mark hot-reloaded config patch notices as restart required", () => {
305+
const payload = {
306+
kind: "config-patch" as const,
307+
status: "ok" as const,
308+
ts: Date.now(),
309+
stats: { mode: "config.patch", requiresRestart: false },
310+
};
311+
312+
expect(summarizeRestartSentinel(payload)).toBe(
313+
"Gateway restart config-patch ok (config.patch)",
314+
);
315+
});
316+
245317
it("formats summary, distinct reason, and doctor hint together", () => {
246318
const payload = {
247319
kind: "config-patch" as const,

‎src/infra/restart-sentinel.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type RestartSentinelStep = {
3333
export type RestartSentinelStats = {
3434
mode?: string;
3535
root?: string;
36+
requiresRestart?: boolean;
3637
handoffId?: string;
3738
before?: Record<string, unknown> | null;
3839
after?: Record<string, unknown> | null;
@@ -340,10 +341,22 @@ export function formatRestartSentinelMessage(payload: RestartSentinelPayload): s
340341
return lines.join("\n");
341342
}
342343

344+
function isRestartRequiredConfigWriteSentinel(payload: RestartSentinelPayload): boolean {
345+
return (
346+
(payload.kind === "config-apply" || payload.kind === "config-patch") &&
347+
payload.status === "ok" &&
348+
payload.stats?.requiresRestart === true
349+
);
350+
}
351+
343352
export function summarizeRestartSentinel(payload: RestartSentinelPayload): string {
344353
if (payload.kind === "config-auto-recovery") {
345354
return "Gateway auto-recovery";
346355
}
356+
if (isRestartRequiredConfigWriteSentinel(payload)) {
357+
const mode = payload.stats?.mode ? ` (${payload.stats.mode})` : "";
358+
return `Gateway restart required${mode}`.trim();
359+
}
347360
const kind = payload.kind;
348361
const status = payload.status;
349362
const mode = payload.stats?.mode ? ` (${payload.stats.mode})` : "";

0 commit comments

Comments
 (0)