feat: run doctor after restart
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createClawdbotTools } from "./clawdbot-tools.js";
|
||||
@@ -10,6 +13,11 @@ describe("gateway tool", () => {
|
||||
it("schedules SIGUSR1 restart", async () => {
|
||||
vi.useFakeTimers();
|
||||
const kill = vi.spyOn(process, "kill").mockImplementation(() => true);
|
||||
const previousStateDir = process.env.CLAWDBOT_STATE_DIR;
|
||||
const stateDir = await fs.mkdtemp(
|
||||
path.join(os.tmpdir(), "clawdbot-test-"),
|
||||
);
|
||||
process.env.CLAWDBOT_STATE_DIR = stateDir;
|
||||
|
||||
try {
|
||||
const tool = createClawdbotTools({
|
||||
@@ -29,12 +37,27 @@ describe("gateway tool", () => {
|
||||
delayMs: 0,
|
||||
});
|
||||
|
||||
const sentinelPath = path.join(stateDir, "restart-sentinel.json");
|
||||
const raw = await fs.readFile(sentinelPath, "utf-8");
|
||||
const parsed = JSON.parse(raw) as {
|
||||
payload?: { kind?: string; doctorHint?: string | null };
|
||||
};
|
||||
expect(parsed.payload?.kind).toBe("restart");
|
||||
expect(parsed.payload?.doctorHint).toBe(
|
||||
"Run: clawdbot doctor --non-interactive",
|
||||
);
|
||||
|
||||
expect(kill).not.toHaveBeenCalled();
|
||||
await vi.runAllTimersAsync();
|
||||
expect(kill).toHaveBeenCalledWith(process.pid, "SIGUSR1");
|
||||
} finally {
|
||||
kill.mockRestore();
|
||||
vi.useRealTimers();
|
||||
if (previousStateDir === undefined) {
|
||||
delete process.env.CLAWDBOT_STATE_DIR;
|
||||
} else {
|
||||
process.env.CLAWDBOT_STATE_DIR = previousStateDir;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -2,6 +2,11 @@ import { Type } from "@sinclair/typebox";
|
||||
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
|
||||
import {
|
||||
DOCTOR_NONINTERACTIVE_HINT,
|
||||
type RestartSentinelPayload,
|
||||
writeRestartSentinel,
|
||||
} from "../../infra/restart-sentinel.js";
|
||||
import { type AnyAgentTool, jsonResult, readStringParam } from "./common.js";
|
||||
import { callGatewayTool } from "./gateway.js";
|
||||
|
||||
@@ -61,6 +66,10 @@ export function createGatewayTool(opts?: {
|
||||
"Gateway restart is disabled. Set commands.restart=true to enable.",
|
||||
);
|
||||
}
|
||||
const sessionKey =
|
||||
typeof params.sessionKey === "string" && params.sessionKey.trim()
|
||||
? params.sessionKey.trim()
|
||||
: opts?.agentSessionKey?.trim() || undefined;
|
||||
const delayMs =
|
||||
typeof params.delayMs === "number" && Number.isFinite(params.delayMs)
|
||||
? Math.floor(params.delayMs)
|
||||
@@ -69,6 +78,27 @@ export function createGatewayTool(opts?: {
|
||||
typeof params.reason === "string" && params.reason.trim()
|
||||
? params.reason.trim().slice(0, 200)
|
||||
: undefined;
|
||||
const note =
|
||||
typeof params.note === "string" && params.note.trim()
|
||||
? params.note.trim()
|
||||
: undefined;
|
||||
const payload: RestartSentinelPayload = {
|
||||
kind: "restart",
|
||||
status: "ok",
|
||||
ts: Date.now(),
|
||||
sessionKey,
|
||||
message: note ?? reason ?? null,
|
||||
doctorHint: DOCTOR_NONINTERACTIVE_HINT,
|
||||
stats: {
|
||||
mode: "gateway.restart",
|
||||
reason,
|
||||
},
|
||||
};
|
||||
try {
|
||||
await writeRestartSentinel(payload);
|
||||
} catch {
|
||||
// ignore: sentinel is best-effort
|
||||
}
|
||||
console.info(
|
||||
`gateway tool: restart requested (delayMs=${delayMs ?? "default"}, reason=${reason ?? "none"})`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user