fix(doctor): canonicalize gateway service entrypoint paths (#43882)

Merged via squash.

Prepared head SHA: 9f530d2a86b5d30822d4419db7cffae6a560ddca
Co-authored-by: ngutman <1540134+ngutman@users.noreply.github.com>
Co-authored-by: ngutman <1540134+ngutman@users.noreply.github.com>
Reviewed-by: @ngutman
This commit is contained in:
Nimrod Gutman
2026-03-12 12:39:22 +02:00
committed by GitHub
parent 783a0d540f
commit 4f620bebe5
4 changed files with 186 additions and 18 deletions

View File

@@ -2,6 +2,22 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { withEnvAsync } from "../test-utils/env.js";
const fsMocks = vi.hoisted(() => ({
realpath: vi.fn(),
}));
vi.mock("node:fs/promises", async () => {
const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
return {
...actual,
default: {
...actual,
realpath: fsMocks.realpath,
},
realpath: fsMocks.realpath,
};
});
const mocks = vi.hoisted(() => ({
readCommand: vi.fn(),
install: vi.fn(),
@@ -137,6 +153,7 @@ function setupGatewayTokenRepairScenario() {
describe("maybeRepairGatewayServiceConfig", () => {
beforeEach(() => {
vi.clearAllMocks();
fsMocks.realpath.mockImplementation(async (value: string) => value);
mocks.resolveGatewayAuthTokenForService.mockImplementation(async (cfg: OpenClawConfig, env) => {
const configToken =
typeof cfg.gateway?.auth?.token === "string" ? cfg.gateway.auth.token.trim() : undefined;
@@ -218,6 +235,121 @@ describe("maybeRepairGatewayServiceConfig", () => {
});
});
it("does not flag entrypoint mismatch when symlink and realpath match", async () => {
mocks.readCommand.mockResolvedValue({
programArguments: [
"/usr/bin/node",
"/Users/test/Library/pnpm/global/5/node_modules/openclaw/dist/index.js",
"gateway",
"--port",
"18789",
],
environment: {},
});
mocks.auditGatewayServiceConfig.mockResolvedValue({
ok: true,
issues: [],
});
mocks.buildGatewayInstallPlan.mockResolvedValue({
programArguments: [
"/usr/bin/node",
"/Users/test/Library/pnpm/global/5/node_modules/.pnpm/openclaw@2026.3.12/node_modules/openclaw/dist/index.js",
"gateway",
"--port",
"18789",
],
environment: {},
});
fsMocks.realpath.mockImplementation(async (value: string) => {
if (value.includes("/global/5/node_modules/openclaw/")) {
return value.replace(
"/global/5/node_modules/openclaw/",
"/global/5/node_modules/.pnpm/openclaw@2026.3.12/node_modules/openclaw/",
);
}
return value;
});
await runRepair({ gateway: {} });
expect(mocks.note).not.toHaveBeenCalledWith(
expect.stringContaining("Gateway service entrypoint does not match the current install."),
"Gateway service config",
);
expect(mocks.install).not.toHaveBeenCalled();
});
it("does not flag entrypoint mismatch when realpath fails but normalized absolute paths match", async () => {
mocks.readCommand.mockResolvedValue({
programArguments: [
"/usr/bin/node",
"/opt/openclaw/../openclaw/dist/index.js",
"gateway",
"--port",
"18789",
],
environment: {},
});
mocks.auditGatewayServiceConfig.mockResolvedValue({
ok: true,
issues: [],
});
mocks.buildGatewayInstallPlan.mockResolvedValue({
programArguments: [
"/usr/bin/node",
"/opt/openclaw/dist/index.js",
"gateway",
"--port",
"18789",
],
environment: {},
});
fsMocks.realpath.mockRejectedValue(new Error("no realpath"));
await runRepair({ gateway: {} });
expect(mocks.note).not.toHaveBeenCalledWith(
expect.stringContaining("Gateway service entrypoint does not match the current install."),
"Gateway service config",
);
expect(mocks.install).not.toHaveBeenCalled();
});
it("still flags entrypoint mismatch when canonicalized paths differ", async () => {
mocks.readCommand.mockResolvedValue({
programArguments: [
"/usr/bin/node",
"/Users/test/.nvm/versions/node/v22.0.0/lib/node_modules/openclaw/dist/index.js",
"gateway",
"--port",
"18789",
],
environment: {},
});
mocks.auditGatewayServiceConfig.mockResolvedValue({
ok: true,
issues: [],
});
mocks.buildGatewayInstallPlan.mockResolvedValue({
programArguments: [
"/usr/bin/node",
"/Users/test/Library/pnpm/global/5/node_modules/openclaw/dist/index.js",
"gateway",
"--port",
"18789",
],
environment: {},
});
await runRepair({ gateway: {} });
expect(mocks.note).toHaveBeenCalledWith(
expect.stringContaining("Gateway service entrypoint does not match the current install."),
"Gateway service config",
);
expect(mocks.install).toHaveBeenCalledTimes(1);
});
it("treats SecretRef-managed gateway token as non-persisted service state", async () => {
mocks.readCommand.mockResolvedValue({
programArguments: gatewayProgramArguments,

View File

@@ -54,8 +54,13 @@ function findGatewayEntrypoint(programArguments?: string[]): string | null {
return programArguments[gatewayIndex - 1] ?? null;
}
function normalizeExecutablePath(value: string): string {
return path.resolve(value);
async function normalizeExecutablePath(value: string): Promise<string> {
const resolvedPath = path.resolve(value);
try {
return await fs.realpath(resolvedPath);
} catch {
return resolvedPath;
}
}
function extractDetailPath(detail: string, prefix: string): string | null {
@@ -269,10 +274,16 @@ export async function maybeRepairGatewayServiceConfig(
});
const expectedEntrypoint = findGatewayEntrypoint(programArguments);
const currentEntrypoint = findGatewayEntrypoint(command.programArguments);
const normalizedExpectedEntrypoint = expectedEntrypoint
? await normalizeExecutablePath(expectedEntrypoint)
: null;
const normalizedCurrentEntrypoint = currentEntrypoint
? await normalizeExecutablePath(currentEntrypoint)
: null;
if (
expectedEntrypoint &&
currentEntrypoint &&
normalizeExecutablePath(expectedEntrypoint) !== normalizeExecutablePath(currentEntrypoint)
normalizedExpectedEntrypoint &&
normalizedCurrentEntrypoint &&
normalizedExpectedEntrypoint !== normalizedCurrentEntrypoint
) {
audit.issues.push({
code: SERVICE_AUDIT_CODES.gatewayEntrypointMismatch,