fix(restart): deduplicate reason line in restart sentinel message

When gateway.restart is triggered with a reason but no separate note,
the payload sets both message and stats.reason to the same text.
formatRestartSentinelMessage() then emits both the message line and a
redundant 'Reason: <same text>' line, doubling the restart reason in
the notification delivered to the agent session.

Skip the 'Reason:' line when stats.reason matches the already-emitted
message text. Add regression tests for both duplicate and distinct
reason scenarios.
This commit is contained in:
velamints2
2026-03-03 03:22:09 +08:00
committed by Peter Steinberger
parent d76ddd61ec
commit 61be533ad4
2 changed files with 31 additions and 1 deletions

View File

@@ -116,3 +116,33 @@ describe("restart sentinel", () => {
expect(textA).not.toContain('"ts"');
});
});
describe("restart sentinel message dedup", () => {
it("omits duplicate Reason: line when stats.reason matches message", () => {
const payload = {
kind: "restart" as const,
status: "ok" as const,
ts: Date.now(),
message: "Applying config changes",
stats: { mode: "gateway.restart", reason: "Applying config changes" },
};
const result = formatRestartSentinelMessage(payload);
// The message text should appear exactly once, not duplicated as "Reason: ..."
const occurrences = result.split("Applying config changes").length - 1;
expect(occurrences).toBe(1);
expect(result).not.toContain("Reason:");
});
it("keeps Reason: line when stats.reason differs from message", () => {
const payload = {
kind: "restart" as const,
status: "ok" as const,
ts: Date.now(),
message: "Restart requested by /restart",
stats: { mode: "gateway.restart", reason: "/restart" },
};
const result = formatRestartSentinelMessage(payload);
expect(result).toContain("Restart requested by /restart");
expect(result).toContain("Reason: /restart");
});
});

View File

@@ -118,7 +118,7 @@ export function formatRestartSentinelMessage(payload: RestartSentinelPayload): s
lines.push(message);
}
const reason = payload.stats?.reason?.trim();
if (reason) {
if (reason && reason !== message) {
lines.push(`Reason: ${reason}`);
}
if (payload.doctorHint?.trim()) {