openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
69 lines (68 loc) • 2.61 kB
JavaScript
import { i as resolveGatewayLogPaths, o as resolveGatewaySupervisorLogPaths } from "./restart-logs-zLeNdzsY.mjs";
import fs from "node:fs/promises";
import { readFileWindowFully } from "@openclaw/fs-safe/advanced";
//#region src/daemon/diagnostics.ts
const GATEWAY_OWNERSHIP_REFUSAL = /cannot verify Gateway ownership/i;
const GATEWAY_LOG_ERROR_PATTERNS = [
GATEWAY_OWNERSHIP_REFUSAL,
/\bENOSPC\b/i,
/no space left on device/i,
/refusing to bind gateway/i,
/gateway auth mode/i,
/gateway start blocked/i,
/gateway failed to start:/i,
/failed to bind gateway socket/i,
/tailscale .* requires/i
];
const GATEWAY_DIAGNOSTIC_LOG_TAIL_BYTES = 262144;
async function readTailWindow(e, t) {
const n = Math.min(t, GATEWAY_DIAGNOSTIC_LOG_TAIL_BYTES);
const r = t - n;
const i = Buffer.alloc(n);
return {
buffer: i,
bytesRead: await readFileWindowFully(e, i, r),
readStart: r
};
}
/** Reads complete lines from a bounded gateway log tail. */
async function readGatewayLogTailLines(e) {
const t = await fs.open(e, "r");
try {
const e = await t.stat();
if (!e.isFile() || e.size <= 0) return [];
let n = await readTailWindow(t, e.size);
if (n.bytesRead < n.buffer.length) {
const r = await t.stat();
if (!r.isFile() || r.size <= 0) return [];
if (r.size !== e.size) n = await readTailWindow(t, r.size);
}
const { buffer: r, bytesRead: i, readStart: a } = n;
let o = 0;
if (a > 0) {
const e = Buffer.alloc(1);
if ((await t.read(e, 0, 1, a - 1)).bytesRead !== 1 || e[0] !== 10) {
const e = r.subarray(0, i).indexOf(10);
if (e === -1) return [];
o = e + 1;
}
}
const s = r.subarray(o, i).toString("utf8").split(/\r?\n/u);
if (s.at(-1) === "") s.pop();
return s;
} finally {
await t.close();
}
}
/** Matching log evidence only; it is not bound to the current process generation. */
async function readLastGatewayErrorLine(e, t) {
const n = t?.platform ?? process.platform;
const r = n !== "darwin";
const { stdoutPath: i, stderrPath: a } = n === "darwin" ? resolveGatewaySupervisorLogPaths(e) : resolveGatewayLogPaths(e);
const o = r ? await readGatewayLogTailLines(a).catch(() => []) : [];
const s = [...await readGatewayLogTailLines(i).catch(() => []), ...o].map((e) => e.trim()).findLast((e) => GATEWAY_LOG_ERROR_PATTERNS.some((t) => t.test(e)));
if (s && GATEWAY_OWNERSHIP_REFUSAL.test(s)) return "Gateway ownership could not be verified. If the recorded holder has stopped, run `openclaw gateway recover-lock` to inspect it.";
return s ?? null;
}
//#endregion
export { readLastGatewayErrorLine as n, readGatewayLogTailLines as t };