fix: recover gateway dashboard startup in stripped shells

This commit is contained in:
Peter Steinberger
2026-05-16 13:05:32 +01:00
parent 7c70954892
commit 03a7b19228
5 changed files with 164 additions and 11 deletions

View File

@@ -90,6 +90,10 @@ vi.mock("../../config/io.js", () => ({
})),
}));
vi.mock("../../config/mutate.js", () => ({
replaceConfigFile: replaceConfigFileMock,
}));
vi.mock("../../config/paths.js", () => ({
resolveGatewayPort: resolveGatewayPortMock,
resolveIsNixMode: resolveIsNixModeMock,
@@ -197,13 +201,13 @@ function readFirstInstallPlanArg(): Record<string, unknown> {
}
function readFirstConfigWriteParams(): {
nextConfig?: { gateway?: { auth?: { token?: string } } };
nextConfig?: { gateway?: { mode?: string; auth?: { token?: string } } };
} {
const [params] = replaceConfigFileMock.mock.calls[0] ?? [];
if (!params || typeof params !== "object") {
throw new Error("expected first config write params");
}
return params as { nextConfig?: { gateway?: { auth?: { token?: string } } } };
return params as { nextConfig?: { gateway?: { mode?: string; auth?: { token?: string } } } };
}
function readFirstNodeStartupTlsEnvironmentArg(): Record<string, unknown> {
@@ -255,12 +259,12 @@ describe("runDaemonInstall", () => {
actionState.emitted.length = 0;
actionState.failed.length = 0;
loadConfigMock.mockReturnValue({ gateway: { auth: { mode: "token" } } });
loadConfigMock.mockReturnValue({ gateway: { mode: "local", auth: { mode: "token" } } });
readConfigFileSnapshotMock.mockResolvedValue({
exists: false,
valid: true,
config: {},
sourceConfig: { gateway: { auth: { mode: "token" } } },
sourceConfig: { gateway: { mode: "local", auth: { mode: "token" } } },
});
resolveGatewayPortMock.mockReturnValue(18789);
resolveIsNixModeMock.mockReturnValue(false);
@@ -381,7 +385,7 @@ describe("runDaemonInstall", () => {
exists: true,
valid: true,
config: { gateway: { auth: { mode: "token" } } },
sourceConfig: { gateway: { auth: { mode: "token" } } },
sourceConfig: { gateway: { mode: "local", auth: { mode: "token" } } },
});
await runDaemonInstall({ json: true });
@@ -396,6 +400,63 @@ describe("runDaemonInstall", () => {
expect(actionState.warnings.join("\n")).toContain("Auto-generated");
});
it("persists local gateway mode when installing from config missing gateway.mode", async () => {
readConfigFileSnapshotMock
.mockResolvedValueOnce({
exists: true,
valid: true,
config: { gateway: { auth: { mode: "token", token: "durable-token" } } },
sourceConfig: { gateway: { auth: { mode: "token", token: "durable-token" } } },
})
.mockResolvedValue({
exists: true,
valid: true,
config: {
gateway: { mode: "local", auth: { mode: "token", token: "durable-token" } },
},
sourceConfig: {
gateway: { mode: "local", auth: { mode: "token", token: "durable-token" } },
},
});
resolveGatewayAuthMock.mockReturnValue({
mode: "token",
token: "durable-token",
password: undefined,
allowTailscale: false,
});
await runDaemonInstall({ json: true });
expect(actionState.failed).toStrictEqual([]);
expect(replaceConfigFileMock).toHaveBeenCalledTimes(1);
expect(readFirstConfigWriteParams().nextConfig?.gateway?.mode).toBe("local");
expect(actionState.warnings).toContain(
"No gateway.mode found. Set gateway.mode=local for managed gateway install.",
);
expectFields(readFirstInstallPlanArg().config as Record<string, unknown>, {
gateway: {
mode: "local",
auth: { mode: "token", token: "durable-token" },
},
});
});
it("does not persist gateway mode when runtime validation fails", async () => {
readConfigFileSnapshotMock.mockResolvedValue({
exists: true,
valid: true,
config: { gateway: { auth: { mode: "token", token: "durable-token" } } },
sourceConfig: { gateway: { auth: { mode: "token", token: "durable-token" } } },
});
isGatewayDaemonRuntimeMock.mockReturnValue(false);
await runDaemonInstall({ json: true, runtime: "bogus" });
expect(actionState.failed[0]?.message).toContain("Invalid --runtime");
expect(replaceConfigFileMock).not.toHaveBeenCalled();
expect(installDaemonServiceAndEmitMock).not.toHaveBeenCalled();
});
it("continues Linux install when service probe hits a non-fatal systemd bus failure", async () => {
service.isLoaded.mockRejectedValueOnce(
new Error("systemctl is-enabled unavailable: Failed to connect to bus"),

View File

@@ -8,6 +8,7 @@ import {
import { resolveGatewayInstallToken } from "../../commands/gateway-install-token.js";
import { resolveFutureConfigActionBlock } from "../../config/future-version-guard.js";
import { readConfigFileSnapshotForWrite } from "../../config/io.js";
import { replaceConfigFile } from "../../config/mutate.js";
import { resolveGatewayPort } from "../../config/paths.js";
import type { OpenClawConfig } from "../../config/types.js";
import { OPENCLAW_WRAPPER_ENV_KEY, resolveOpenClawWrapperPath } from "../../daemon/program-args.js";
@@ -82,7 +83,7 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
return;
}
const { snapshot: configSnapshot, writeOptions: configWriteOptions } =
let { snapshot: configSnapshot, writeOptions: configWriteOptions } =
await readConfigFileSnapshotForWrite();
const futureBlock = resolveFutureConfigActionBlock({
action: "install or rewrite the gateway service",
@@ -92,7 +93,7 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
fail(`Gateway install blocked: ${futureBlock.message}`, futureBlock.hints);
return;
}
const cfg = configSnapshot.valid ? configSnapshot.sourceConfig : configSnapshot.config;
let cfg = configSnapshot.valid ? configSnapshot.sourceConfig : configSnapshot.config;
const portOverride = parsePort(opts.port);
if (opts.port !== undefined && portOverride === null) {
fail(formatInvalidPortOption("--port"));
@@ -121,6 +122,35 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
return;
}
}
if (configSnapshot.valid && cfg.gateway?.mode === undefined) {
const baseConfig = configSnapshot.sourceConfig ?? configSnapshot.config;
await replaceConfigFile({
nextConfig: {
...baseConfig,
gateway: {
...baseConfig.gateway,
mode: "local",
},
},
snapshot: configSnapshot,
writeOptions: {
baseSnapshot: configSnapshot,
...configWriteOptions,
skipRuntimeSnapshotRefresh: true,
},
afterWrite: { mode: "auto" },
});
const refreshed = await readConfigFileSnapshotForWrite();
configSnapshot = refreshed.snapshot;
configWriteOptions = refreshed.writeOptions;
cfg = configSnapshot.valid ? configSnapshot.sourceConfig : configSnapshot.config;
const message = "No gateway.mode found. Set gateway.mode=local for managed gateway install.";
if (json) {
warnings.push(message);
} else {
defaultRuntime.log(message);
}
}
const service = resolveGatewayService();
let loaded = false;