Skip to content

Commit e131685

Browse files
committed
fix(config): cover service and gateway validation
1 parent 3f1ecaf commit e131685

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎src/cli/daemon-cli/lifecycle-action-preflight.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ afterEach(() => {
1111
});
1212

1313
async function withIsolatedLifecycleState(
14-
run: (params: { agentDir: string }) => Promise<void>,
14+
run: (params: { agentDir: string; configPath: string }) => Promise<void>,
1515
): Promise<void> {
1616
await withTestDir({ prefix: "openclaw-lifecycle-action-preflight-" }, async (root) => {
1717
const stateDir = path.join(root, "state");
@@ -23,7 +23,7 @@ async function withIsolatedLifecycleState(
2323
vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
2424
vi.stubEnv("OPENCLAW_CONFIG_PATH", configPath);
2525
resetConfigRuntimeState();
26-
await run({ agentDir });
26+
await run({ agentDir, configPath });
2727
await expect(fs.access(path.join(stateDir, "state", "openclaw.sqlite"))).rejects.toMatchObject({
2828
code: "ENOENT",
2929
});
@@ -65,4 +65,26 @@ describe("getServiceActionPreflightFailure", () => {
6565
});
6666
},
6767
);
68+
69+
it.each(["start", "restart"] as const)(
70+
"renders actionable invalid-config diagnostics before %s",
71+
async (action) => {
72+
await withIsolatedLifecycleState(async ({ configPath }) => {
73+
await fs.writeFile(
74+
configPath,
75+
'{\n meta: { lastTouchedVersion: "9999.1.1" },\n gateway: { mode: "nope" },\n}\n',
76+
);
77+
resetConfigRuntimeState();
78+
79+
const failure = await getServiceActionPreflightFailure(action);
80+
81+
expect(failure?.message).toContain(
82+
'openclaw.json:3 — gateway.mode: Invalid input (allowed: "local", "remote"), got: "nope"',
83+
);
84+
expect(failure?.message).toContain(
85+
"Config was last written by OpenClaw 9999.1.1, but you are running",
86+
);
87+
});
88+
},
89+
);
6890
});

‎src/cli/daemon-cli/lifecycle-action-preflight.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from "../../agents/auth-profiles/legacy-source-diagnostic.js";
55
import { readConfigFileSnapshot } from "../../config/config.js";
66
import { resolveFutureConfigActionBlock } from "../../config/future-version-guard.js";
7-
import { formatConfigIssueLines } from "../../config/issue-format.js";
7+
import { renderConfigValidationIssueLines } from "../../config/issue-location.js";
88
import { isPluginPackagingRuntimeOutputInvalidConfigSnapshot } from "../../config/recovery-policy.js";
99
import type { ConfigFileSnapshot } from "../../config/types.openclaw.js";
1010
import { collectCandidateAgentDirs } from "../../secrets/runtime-fast-path.js";
@@ -75,7 +75,7 @@ export async function getServiceActionPreflightFailure(
7575
if (snapshot.exists && !snapshot.valid) {
7676
const message =
7777
snapshot.issues.length > 0
78-
? formatConfigIssueLines(snapshot.issues, "", { normalizeRoot: true }).join("\n")
78+
? renderConfigValidationIssueLines(snapshot, "").join("\n")
7979
: "Unknown validation issue.";
8080
return {
8181
message,

‎src/gateway/server-startup-config-helpers.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type ReadConfigFileSnapshotWithPluginMetadataResult,
99
readConfigFileSnapshotWithPluginMetadata,
1010
} from "../config/io.js";
11-
import { formatConfigIssueLines } from "../config/issue-format.js";
11+
import { renderConfigValidationIssueLines } from "../config/issue-location.js";
1212
import {
1313
retainLegacyDefaultAgentId,
1414
tryGetLegacyDefaultAgentId,
@@ -56,7 +56,7 @@ export function assertValidGatewayStartupConfigSnapshot(
5656
}
5757
const issues =
5858
snapshot.issues.length > 0
59-
? formatConfigIssueLines(snapshot.issues, "", { normalizeRoot: true }).join("\n")
59+
? renderConfigValidationIssueLines(snapshot, "").join("\n")
6060
: "Unknown validation issue.";
6161
const recoveryHint =
6262
options.includeDoctorHint && isPluginPackagingRuntimeOutputInvalidConfigSnapshot(snapshot)

‎src/gateway/server-startup-config.recovery.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,32 @@ describe("gateway startup config validation", () => {
484484
);
485485
});
486486

487+
it("renders actionable diagnostics for invalid config written by a newer version", async () => {
488+
const rawConfig = {
489+
meta: { lastTouchedVersion: "9999.1.1" },
490+
gateway: { mode: "nope" },
491+
};
492+
const invalidSnapshot = buildInvalidConfigSnapshot({
493+
rawConfig,
494+
config: rawConfig as OpenClawConfig,
495+
issues: [
496+
{
497+
path: "gateway.mode",
498+
pathSegments: ["gateway", "mode"],
499+
message: 'Invalid input (allowed: "local", "remote")',
500+
},
501+
],
502+
});
503+
vi.mocked(configIo.readConfigFileSnapshot).mockResolvedValueOnce(invalidSnapshot);
504+
505+
await expectStartupRejects(
506+
new RegExp(
507+
'openclaw-startup-recovery\\.json:1 — gateway\\.mode: Invalid input \\(allowed: "local", "remote"\\), got: "nope".*Config was last written by OpenClaw 9999\\.1\\.1, but you are running',
508+
"s",
509+
),
510+
);
511+
});
512+
487513
it("does not suggest doctor repair for plugin packaging compiled-output failures", async () => {
488514
const rawConfig = pluginSlotRawConfig("local");
489515
const invalidSnapshot = buildInvalidConfigSnapshot({

0 commit comments

Comments
 (0)