* fix: defer gateway restart until all replies are sent Fixes a race condition where gateway config changes (e.g., enabling plugins via iMessage) trigger an immediate SIGUSR1 restart, killing the iMessage RPC connection before replies are delivered. Both restart paths (config watcher and RPC-triggered) now defer until all queued operations, pending replies, and embedded agent runs complete (polling every 500ms, 30s timeout). A shared emitGatewayRestart() guard prevents double SIGUSR1 when both paths fire simultaneously. Key changes: - Dispatcher registry tracks active reply dispatchers globally - markComplete() called in finally block for guaranteed cleanup - Pre-restart deferral hook registered at gateway startup - Centralized extractDeliveryInfo() for session key parsing - Post-restart sentinel messages delivered directly (not via agent) - config-patch distinguished from config-apply in sentinel kind Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: single-source gateway restart authorization --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Peter Steinberger <steipete@gmail.com>
106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import {
|
|
consumeRestartSentinel,
|
|
formatRestartSentinelMessage,
|
|
readRestartSentinel,
|
|
resolveRestartSentinelPath,
|
|
trimLogTail,
|
|
writeRestartSentinel,
|
|
} from "./restart-sentinel.js";
|
|
|
|
describe("restart sentinel", () => {
|
|
let prevStateDir: string | undefined;
|
|
let tempDir: string;
|
|
|
|
beforeEach(async () => {
|
|
prevStateDir = process.env.OPENCLAW_STATE_DIR;
|
|
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sentinel-"));
|
|
process.env.OPENCLAW_STATE_DIR = tempDir;
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (prevStateDir) {
|
|
process.env.OPENCLAW_STATE_DIR = prevStateDir;
|
|
} else {
|
|
delete process.env.OPENCLAW_STATE_DIR;
|
|
}
|
|
await fs.rm(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it("writes and consumes a sentinel", async () => {
|
|
const payload = {
|
|
kind: "update" as const,
|
|
status: "ok" as const,
|
|
ts: Date.now(),
|
|
sessionKey: "agent:main:whatsapp:dm:+15555550123",
|
|
stats: { mode: "git" },
|
|
};
|
|
const filePath = await writeRestartSentinel(payload);
|
|
expect(filePath).toBe(resolveRestartSentinelPath());
|
|
|
|
const read = await readRestartSentinel();
|
|
expect(read?.payload.kind).toBe("update");
|
|
|
|
const consumed = await consumeRestartSentinel();
|
|
expect(consumed?.payload.sessionKey).toBe(payload.sessionKey);
|
|
|
|
const empty = await readRestartSentinel();
|
|
expect(empty).toBeNull();
|
|
});
|
|
|
|
it("drops invalid sentinel payloads", async () => {
|
|
const filePath = resolveRestartSentinelPath();
|
|
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
await fs.writeFile(filePath, "not-json", "utf-8");
|
|
|
|
const read = await readRestartSentinel();
|
|
expect(read).toBeNull();
|
|
|
|
await expect(fs.stat(filePath)).rejects.toThrow();
|
|
});
|
|
|
|
it("formatRestartSentinelMessage uses custom message when present", () => {
|
|
const payload = {
|
|
kind: "config-apply" as const,
|
|
status: "ok" as const,
|
|
ts: Date.now(),
|
|
message: "Config updated successfully",
|
|
};
|
|
expect(formatRestartSentinelMessage(payload)).toBe("Config updated successfully");
|
|
});
|
|
|
|
it("formatRestartSentinelMessage falls back to summary when no message", () => {
|
|
const payload = {
|
|
kind: "update" as const,
|
|
status: "ok" as const,
|
|
ts: Date.now(),
|
|
stats: { mode: "git" },
|
|
};
|
|
const result = formatRestartSentinelMessage(payload);
|
|
expect(result).toContain("Gateway restart");
|
|
expect(result).toContain("update");
|
|
expect(result).toContain("ok");
|
|
});
|
|
|
|
it("formatRestartSentinelMessage falls back to summary for blank message", () => {
|
|
const payload = {
|
|
kind: "restart" as const,
|
|
status: "ok" as const,
|
|
ts: Date.now(),
|
|
message: " ",
|
|
};
|
|
const result = formatRestartSentinelMessage(payload);
|
|
expect(result).toContain("Gateway restart");
|
|
});
|
|
|
|
it("trims log tails", () => {
|
|
const text = "a".repeat(9000);
|
|
const trimmed = trimLogTail(text, 8000);
|
|
expect(trimmed?.length).toBeLessThanOrEqual(8001);
|
|
expect(trimmed?.startsWith("…")).toBe(true);
|
|
});
|
|
});
|