Skip to content

Commit 2272300

Browse files
authored
fix(configure): persist changes before side effects (#125751)
Local mode is persisted before health and daemon actions, and failed actions no longer receive a bare success outro.
1 parent 9204ab8 commit 2272300

7 files changed

Lines changed: 225 additions & 41 deletions

‎src/commands/configure.channels.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ function expectUnknownChannelRemovalPrompt(unsafeChannel: string, label: string)
8484
`Delete ${label} configuration from ~/.openclaw/openclaw.json?`,
8585
);
8686
expect(note).toHaveBeenCalledWith(
87-
`${label} removed from config.\nNote: credentials/sessions on disk are unchanged.`,
88-
"Channel removed",
87+
`${label} selected for removal from config.\nNote: credentials/sessions on disk are unchanged.`,
88+
"Channel removal",
8989
);
9090
}
9191

@@ -143,8 +143,8 @@ describe("removeChannelConfigWizard", () => {
143143
);
144144
expect(next.channels).toEqual({ twitch: { token: "secret" } });
145145
expect(note).toHaveBeenCalledWith(
146-
"Telegram removed from config.\nNote: credentials/sessions on disk are unchanged.",
147-
"Channel removed",
146+
"Telegram selected for removal from config.\nNote: credentials/sessions on disk are unchanged.",
147+
"Channel removal",
148148
);
149149
});
150150

@@ -164,8 +164,8 @@ describe("removeChannelConfigWizard", () => {
164164
expect(confirmArg().message).toBe("Delete done configuration from ~/.openclaw/openclaw.json?");
165165
expect(next.channels).toEqual({ telegram: { token: "secret" } });
166166
expect(note).toHaveBeenCalledWith(
167-
"done removed from config.\nNote: credentials/sessions on disk are unchanged.",
168-
"Channel removed",
167+
"done selected for removal from config.\nNote: credentials/sessions on disk are unchanged.",
168+
"Channel removal",
169169
);
170170
});
171171

@@ -231,8 +231,8 @@ describe("removeChannelConfigWizard", () => {
231231
"Delete Telegram\\nBot configuration from ~/.openclaw/openclaw.json?",
232232
);
233233
expect(note).toHaveBeenCalledWith(
234-
"Telegram\\nBot removed from config.\nNote: credentials/sessions on disk are unchanged.",
235-
"Channel removed",
234+
"Telegram\\nBot selected for removal from config.\nNote: credentials/sessions on disk are unchanged.",
235+
"Channel removal",
236236
);
237237
});
238238

‎src/commands/configure.channels.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ export async function removeChannelConfigWizard(
130130
}
131131

132132
note(
133-
[`${label} removed from config.`, "Note: credentials/sessions on disk are unchanged."].join(
134-
"\n",
135-
),
136-
"Channel removed",
133+
[
134+
`${label} selected for removal from config.`,
135+
"Note: credentials/sessions on disk are unchanged.",
136+
].join("\n"),
137+
"Channel removal",
137138
);
138139
}
139140
}

‎src/commands/configure.daemon.test.ts‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@ describe("maybeInstallDaemon", () => {
121121
warnings: [],
122122
});
123123

124-
await maybeInstallDaemon({
124+
const outcome = await maybeInstallDaemon({
125125
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
126126
port: 18789,
127127
});
128128

129+
expect(outcome).toBe("failed");
129130
expect(note).toHaveBeenCalledWith(
130131
"Gateway service install failed: Gateway install blocked: gateway.auth.token SecretRef is configured but unresolved (boom). Fix gateway auth config/token input and rerun configure.",
131132
"Gateway",
@@ -139,12 +140,10 @@ describe("maybeInstallDaemon", () => {
139140
new Error("systemctl is-enabled unavailable: Failed to connect to bus"),
140141
);
141142

142-
await expect(
143-
maybeInstallDaemon({
144-
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
145-
port: 18789,
146-
}),
147-
).resolves.toBeUndefined();
143+
await maybeInstallDaemon({
144+
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
145+
port: 18789,
146+
});
148147

149148
expect(serviceInstall).toHaveBeenCalledTimes(1);
150149
});
@@ -169,12 +168,10 @@ describe("maybeInstallDaemon", () => {
169168
new Error("systemctl --user unavailable: Failed to connect to bus: No medium found"),
170169
);
171170

172-
await expect(
173-
maybeInstallDaemon({
174-
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
175-
port: 18789,
176-
}),
177-
).resolves.toBeUndefined();
171+
await maybeInstallDaemon({
172+
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
173+
port: 18789,
174+
});
178175

179176
expect(serviceInstall).toHaveBeenCalledTimes(1);
180177
});

‎src/commands/configure.daemon.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import { resolveGatewayInstallToken } from "./gateway-install-token.js";
1717
import { guardCancel } from "./onboard-helpers.js";
1818
import { ensureSystemdUserLingerInteractive } from "./systemd-linger.js";
1919

20+
export type DaemonSetupOutcome = "succeeded" | "failed" | "skipped";
21+
2022
/** Prompt to install, reinstall, restart, or skip the local Gateway service. */
2123
export async function maybeInstallDaemon(params: {
2224
runtime: RuntimeEnv;
2325
port: number;
2426
daemonRuntime?: GatewayDaemonRuntime;
25-
}) {
27+
}): Promise<DaemonSetupOutcome> {
2628
const service = resolveGatewayService();
2729
let loaded;
2830
try {
@@ -67,7 +69,7 @@ export async function maybeInstallDaemon(params: {
6769
shouldInstall = false;
6870
}
6971
if (action === "skip") {
70-
return;
72+
return "skipped";
7173
}
7274
if (action === "reinstall") {
7375
await withProgress(
@@ -149,7 +151,7 @@ export async function maybeInstallDaemon(params: {
149151
if (installError) {
150152
note("Gateway service install failed: ".concat(installError), "Gateway");
151153
note(gatewayInstallErrorHint(), "Gateway");
152-
return;
154+
return "failed";
153155
}
154156
shouldCheckLinger = true;
155157
}
@@ -166,4 +168,5 @@ export async function maybeInstallDaemon(params: {
166168
requireConfirm: true,
167169
});
168170
}
171+
return "succeeded";
169172
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Configure wizard persistence tests protect config writes before local side effects.
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import type { OpenClawConfig } from "../config/types.openclaw.js";
4+
5+
const mocks = vi.hoisted(() => ({
6+
intro: vi.fn(),
7+
outro: vi.fn(),
8+
select: vi.fn(),
9+
text: vi.fn(),
10+
note: vi.fn(),
11+
readConfigFileSnapshotForWrite: vi.fn(),
12+
writeWizardConfigFile: vi.fn(),
13+
probeGatewayReachable: vi.fn(),
14+
waitForGatewayReachable: vi.fn(),
15+
healthCommand: vi.fn(),
16+
maybeInstallDaemon: vi.fn(),
17+
}));
18+
19+
vi.mock("./configure.shared.js", () => ({
20+
CONFIGURE_SECTION_OPTIONS: [
21+
{ value: "daemon", label: "Daemon", hint: "Manage the background service" },
22+
{ value: "health", label: "Health check", hint: "Run gateway checks" },
23+
],
24+
confirm: vi.fn(),
25+
intro: mocks.intro,
26+
outro: mocks.outro,
27+
select: mocks.select,
28+
text: mocks.text,
29+
}));
30+
31+
vi.mock("../config/config.js", () => ({
32+
readConfigFileSnapshotForWrite: mocks.readConfigFileSnapshotForWrite,
33+
resolveGatewayPort: () => 18789,
34+
}));
35+
36+
vi.mock("../config/logging.js", () => ({ logConfigUpdated: vi.fn() }));
37+
vi.mock("../../packages/terminal-core/src/note.js", () => ({ note: mocks.note }));
38+
vi.mock("../wizard/clack-prompter.js", () => ({ createClackPrompter: () => ({}) }));
39+
vi.mock("../wizard/setup.shared.js", () => ({
40+
writeWizardConfigFile: mocks.writeWizardConfigFile,
41+
}));
42+
vi.mock("../wizard/setup.secret-input.js", () => ({
43+
resolveSetupSecretInputString: vi.fn(async () => undefined),
44+
}));
45+
46+
vi.mock("./onboard-helpers.js", () => ({
47+
DEFAULT_WORKSPACE: "/tmp/openclaw-workspace",
48+
applyWizardMetadata: (config: OpenClawConfig) => config,
49+
guardCancel: (value: unknown) => value,
50+
probeGatewayReachable: mocks.probeGatewayReachable,
51+
resolveAdvertisedControlUiLinks: vi.fn(async () => ({
52+
httpUrl: "http://127.0.0.1:18789/",
53+
wsUrl: "ws://127.0.0.1:18789",
54+
})),
55+
resolveLocalControlUiProbeLinks: vi.fn(() => ({
56+
httpUrl: "http://127.0.0.1:18789/",
57+
wsUrl: "ws://127.0.0.1:18789",
58+
})),
59+
summarizeExistingConfig: vi.fn(() => "Gateway: remote"),
60+
waitForGatewayReachable: mocks.waitForGatewayReachable,
61+
}));
62+
63+
vi.mock("./onboard-agent-target.js", () => ({
64+
ensureOnboardingAgentWorkspace: vi.fn(),
65+
resolveOnboardingAgentTarget: () => ({
66+
agentId: "main",
67+
agentDir: "/tmp/openclaw-agent",
68+
workspaceDir: "/tmp/openclaw-workspace",
69+
}),
70+
}));
71+
72+
vi.mock("../plugins/install-record-commit.js", () => ({
73+
commitConfigWithPendingPluginInstalls: vi.fn(),
74+
}));
75+
vi.mock("../plugins/plugin-registry.js", () => ({ resolvePluginContributionOwners: vi.fn() }));
76+
vi.mock("./configure.channels.js", () => ({ removeChannelConfigWizard: vi.fn() }));
77+
vi.mock("./configure.daemon.js", () => ({ maybeInstallDaemon: mocks.maybeInstallDaemon }));
78+
vi.mock("./configure.gateway-auth.js", () => ({ promptAuthConfig: vi.fn() }));
79+
vi.mock("./configure.gateway.js", () => ({ promptGatewayConfig: vi.fn() }));
80+
vi.mock("./health.js", () => ({ healthCommand: mocks.healthCommand }));
81+
vi.mock("./onboard-channels.js", () => ({ setupChannels: vi.fn() }));
82+
vi.mock("./onboard-remote.js", () => ({ promptRemoteGatewayConfig: vi.fn() }));
83+
vi.mock("./onboard-skills.js", () => ({ setupSkills: vi.fn() }));
84+
85+
import { runConfigureWizard } from "./configure.wizard.js";
86+
87+
const runtime = {
88+
log: vi.fn(),
89+
error: vi.fn(),
90+
exit: vi.fn(),
91+
};
92+
93+
describe("configure wizard persistence before local side effects", () => {
94+
beforeEach(() => {
95+
vi.clearAllMocks();
96+
mocks.readConfigFileSnapshotForWrite.mockResolvedValue({
97+
snapshot: {
98+
exists: true,
99+
valid: true,
100+
config: { gateway: { mode: "remote" } },
101+
issues: [],
102+
},
103+
writeOptions: {
104+
expectedConfigPath: "/tmp/openclaw.json",
105+
ownedConfigPathForWrite: "/tmp/openclaw.json",
106+
},
107+
});
108+
mocks.probeGatewayReachable.mockResolvedValue({ ok: false });
109+
mocks.text.mockResolvedValue("18789");
110+
});
111+
112+
it.each([
113+
["health", "succeeded"],
114+
["daemon", "succeeded"],
115+
["health", "failed"],
116+
["daemon", "failed"],
117+
] as const)("persists Local before %s reports %s", async (section, outcome) => {
118+
const choices = ["local", section, "__continue"];
119+
const events: string[] = [];
120+
const writes: OpenClawConfig[] = [];
121+
mocks.select.mockImplementation(async () => choices.shift());
122+
mocks.writeWizardConfigFile.mockImplementation(async (config: OpenClawConfig) => {
123+
events.push("commit");
124+
writes.push(config);
125+
return config;
126+
});
127+
if (section === "health") {
128+
mocks.waitForGatewayReachable.mockImplementationOnce(async () => {
129+
events.push("health");
130+
return { ok: outcome === "succeeded" };
131+
});
132+
} else {
133+
mocks.maybeInstallDaemon.mockImplementationOnce(async () => {
134+
events.push("daemon");
135+
return outcome;
136+
});
137+
}
138+
139+
await runConfigureWizard({ command: "configure" }, runtime);
140+
141+
expect(writes).toHaveLength(1);
142+
expect(writes[0]?.gateway?.mode).toBe("local");
143+
expect(events).toEqual(["commit", section]);
144+
if (outcome === "failed") {
145+
expect(mocks.outro).toHaveBeenLastCalledWith(
146+
`Configuration updated, but ${section === "health" ? "health check" : "daemon setup"} failed.`,
147+
);
148+
expect(mocks.outro).not.toHaveBeenCalledWith("Configuration updated.");
149+
}
150+
});
151+
});

‎src/commands/configure.wizard.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ describe("runConfigureWizard", () => {
388388
});
389389
vi.mocked(maybeInstallDaemon).mockImplementationOnce(async () => {
390390
events.push("daemon");
391+
return "succeeded";
391392
});
392393

393394
await runConfigureWizard(

0 commit comments

Comments
 (0)