Skip to content

Commit 0a70481

Browse files
authored
fix(onboard): preserve named-agent profiles during config resets (#127716)
1 parent 01cd0a4 commit 0a70481

2 files changed

Lines changed: 90 additions & 76 deletions

File tree

‎src/commands/onboard.test.ts‎

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
66
import type { ProviderAuthMethod, ProviderPlugin } from "../plugins/types.js";
77
import type { RuntimeEnv } from "../runtime.js";
88
import { resolveUserPath } from "../utils.js";
9+
import * as nonInteractiveApiKeys from "./onboard-non-interactive/api-keys.js";
910
import { setupWizardCommand } from "./onboard.js";
1011

1112
type ConfigSnapshotStub = {
@@ -261,12 +262,7 @@ describe("setupWizardCommand", () => {
261262
it("defaults --reset to config+creds+sessions scope", async () => {
262263
const runtime = makeRuntime();
263264

264-
await setupWizardCommand(
265-
{
266-
reset: true,
267-
},
268-
runtime,
269-
);
265+
await setupWizardCommand({ reset: true }, runtime);
270266

271267
expectResetCall({ scope: "config+creds+sessions", runtime });
272268
});
@@ -324,12 +320,7 @@ describe("setupWizardCommand", () => {
324320
},
325321
});
326322

327-
await setupWizardCommand(
328-
{
329-
reset: true,
330-
},
331-
runtime,
332-
);
323+
await setupWizardCommand({ reset: true }, runtime);
333324

334325
expect(mocks.handleReset).toHaveBeenCalledWith(
335326
"config+creds+sessions",
@@ -353,13 +344,7 @@ describe("setupWizardCommand", () => {
353344
},
354345
});
355346

356-
await setupWizardCommand(
357-
{
358-
reset: true,
359-
resetScope: "full",
360-
},
361-
runtime,
362-
);
347+
await setupWizardCommand({ reset: true, resetScope: "full" }, runtime);
363348

364349
expect(mocks.handleReset).toHaveBeenCalledWith(
365350
"full",
@@ -388,13 +373,7 @@ describe("setupWizardCommand", () => {
388373
} as unknown as OpenClawConfig,
389374
});
390375

391-
await setupWizardCommand(
392-
{
393-
reset: true,
394-
resetScope: "full",
395-
},
396-
runtime,
397-
);
376+
await setupWizardCommand({ reset: true, resetScope: "full" }, runtime);
398377

399378
expect(runtime.error).toHaveBeenCalledWith(
400379
"Configured workspace is invalid. Pass --workspace with the workspace to remove, or use a narrower --reset-scope.",
@@ -414,13 +393,7 @@ describe("setupWizardCommand", () => {
414393
readError: { code: "EACCES" },
415394
});
416395

417-
await setupWizardCommand(
418-
{
419-
reset: true,
420-
resetScope: "full",
421-
},
422-
runtime,
423-
);
396+
await setupWizardCommand({ reset: true, resetScope: "full" }, runtime);
424397

425398
expect(runtime.error).toHaveBeenCalledWith(
426399
"Cannot determine the configured workspace from an unreadable config. Pass --workspace with the workspace to remove, or use a narrower --reset-scope.",
@@ -437,13 +410,7 @@ describe("setupWizardCommand", () => {
437410
sourceConfig: { gateway: { port: 1 } },
438411
});
439412

440-
await setupWizardCommand(
441-
{
442-
reset: true,
443-
resetScope: "full",
444-
},
445-
runtime,
446-
);
413+
await setupWizardCommand({ reset: true, resetScope: "full" }, runtime);
447414

448415
expect(mocks.handleReset).toHaveBeenCalledWith(
449416
"full",
@@ -455,13 +422,7 @@ describe("setupWizardCommand", () => {
455422
it("accepts explicit --reset-scope full", async () => {
456423
const runtime = makeRuntime();
457424

458-
await setupWizardCommand(
459-
{
460-
reset: true,
461-
resetScope: "full",
462-
},
463-
runtime,
464-
);
425+
await setupWizardCommand({ reset: true, resetScope: "full" }, runtime);
465426

466427
expectResetCall({ scope: "full", runtime });
467428
});
@@ -490,12 +451,7 @@ describe("setupWizardCommand", () => {
490451
it("rejects --reset-scope without --reset", async () => {
491452
const runtime = makeRuntime();
492453

493-
await setupWizardCommand(
494-
{
495-
resetScope: "full",
496-
},
497-
runtime,
498-
);
454+
await setupWizardCommand({ resetScope: "full" }, runtime);
499455

500456
expect(runtime.error).toHaveBeenCalledWith(
501457
"--reset-scope requires --reset. Re-run with openclaw onboard --reset --reset-scope full.",
@@ -876,6 +832,56 @@ describe("setupWizardCommand", () => {
876832
expect(mocks.runNonInteractiveSetup).not.toHaveBeenCalled();
877833
});
878834

835+
it.each([
836+
{ agentName: "robby", agentId: "robby", scope: "config", reuseProfile: true },
837+
{ agentName: "Robby!", agentId: "robby", scope: "config", reuseProfile: true },
838+
{ agentName: undefined, agentId: "main", scope: "config", reuseProfile: true },
839+
{ agentName: "robby", agentId: "robby", scope: "config+creds+sessions", reuseProfile: false },
840+
{ agentName: "robby", agentId: "robby", scope: "full", reuseProfile: false },
841+
] as const)(
842+
"preflights $agentId provider profiles against reset scope $scope",
843+
async ({ agentName, agentId, scope, reuseProfile }) => {
844+
const runtime = makeRuntime();
845+
const workspaceDir = "/tmp/openclaw-reset-agent-workspace";
846+
const agentDirSuffix = path.join("agents", agentId, "agent");
847+
const resolveApiKey = vi
848+
.spyOn(nonInteractiveApiKeys, "resolveNonInteractiveApiKey")
849+
.mockImplementation(async (input) =>
850+
input.allowProfile && input.agentDir?.endsWith(agentDirSuffix)
851+
? { key: "fixture-agent-profile-key", source: "profile" }
852+
: null,
853+
);
854+
855+
try {
856+
await setupWizardCommand(
857+
{
858+
reset: true,
859+
resetScope: scope,
860+
nonInteractive: true,
861+
acceptRisk: true,
862+
agentName,
863+
workspace: workspaceDir,
864+
authChoice: "apiKey",
865+
tokenProvider: "anthropic",
866+
},
867+
runtime,
868+
);
869+
870+
expect(resolveApiKey).toHaveBeenCalledWith(
871+
expect.objectContaining({
872+
agentDir: expect.stringContaining(agentDirSuffix),
873+
workspaceDir,
874+
allowProfile: reuseProfile,
875+
}),
876+
);
877+
expect(mocks.handleReset).toHaveBeenCalledTimes(reuseProfile ? 1 : 0);
878+
expect(mocks.runNonInteractiveSetup).toHaveBeenCalledTimes(reuseProfile ? 1 : 0);
879+
} finally {
880+
resolveApiKey.mockRestore();
881+
}
882+
},
883+
);
884+
879885
it.each(localResetProviderCases)(
880886
"validates $providerId exactly once before reset and non-interactive setup",
881887
async ({ providerId, methodId }) => {
@@ -1115,28 +1121,21 @@ describe("setupWizardCommand", () => {
11151121
expect(mocks.runInteractiveSetup).not.toHaveBeenCalled();
11161122
});
11171123

1118-
it("rejects conflicting classic and non-interactive modes", async () => {
1119-
const runtime = makeRuntime();
1120-
1121-
await setupWizardCommand({ classic: true, nonInteractive: true, acceptRisk: true }, runtime);
1122-
1123-
expect(runtime.error).toHaveBeenCalledWith(
1124-
"--classic cannot be combined with --non-interactive. Remove --non-interactive to open the classic wizard, or remove --classic for automated setup.",
1125-
);
1126-
expect(runtime.exit).toHaveBeenCalledWith(1);
1127-
expect(mocks.runNonInteractiveSetup).not.toHaveBeenCalled();
1128-
expect(mocks.runInteractiveSetup).not.toHaveBeenCalled();
1129-
expect(mocks.runGuidedOnboarding).not.toHaveBeenCalled();
1130-
});
1131-
1132-
it("rejects conflicting TUI and non-interactive modes", async () => {
1124+
it.each([
1125+
{
1126+
opts: { classic: true },
1127+
error:
1128+
"--classic cannot be combined with --non-interactive. Remove --non-interactive to open the classic wizard, or remove --classic for automated setup.",
1129+
},
1130+
{
1131+
opts: { tui: true },
1132+
error:
1133+
"--tui cannot be combined with --non-interactive. Remove --tui for automation, or remove --non-interactive to open the terminal hatch.",
1134+
},
1135+
])("rejects conflicting $opts and non-interactive modes", async ({ opts, error }) => {
11331136
const runtime = makeRuntime();
1134-
1135-
await setupWizardCommand({ tui: true, nonInteractive: true, acceptRisk: true }, runtime);
1136-
1137-
expect(runtime.error).toHaveBeenCalledWith(
1138-
"--tui cannot be combined with --non-interactive. Remove --tui for automation, or remove --non-interactive to open the terminal hatch.",
1139-
);
1137+
await setupWizardCommand({ ...opts, nonInteractive: true, acceptRisk: true }, runtime);
1138+
expect(runtime.error).toHaveBeenCalledWith(error);
11401139
expect(runtime.exit).toHaveBeenCalledWith(1);
11411140
expect(mocks.runNonInteractiveSetup).not.toHaveBeenCalled();
11421141
expect(mocks.runInteractiveSetup).not.toHaveBeenCalled();

‎src/commands/onboard.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import { formatAuthChoiceChoicesForCli } from "./auth-choice-options.js";
3535
import { GENERIC_PROVIDER_AUTH_CHOICES } from "./auth-choice-options.static.js";
3636
import { isGatewayDaemonRuntime } from "./daemon-runtime.js";
37+
import { resolveOnboardingSetupTarget } from "./onboard-agent-target.js";
3738
import {
3839
applyCustomApiConfig,
3940
CustomApiError,
@@ -329,7 +330,16 @@ async function validateResetAuthChoice(params: {
329330
`Auth choice "${authChoice}" was not matched to provider "${params.opts.tokenProvider?.trim()}".`,
330331
);
331332
}
332-
if (params.opts.nonInteractive && authChoice === "custom-api-key") {
333+
if (!params.opts.nonInteractive || authChoice === "skip") {
334+
return true;
335+
}
336+
const target = resolveOnboardingSetupTarget(
337+
params.baseConfig,
338+
params.opts.agentName
339+
? { name: params.opts.agentName, workspaceDir: params.workspaceDir }
340+
: undefined,
341+
);
342+
if (authChoice === "custom-api-key") {
333343
try {
334344
const custom = parseNonInteractiveCustomApiFlags({
335345
baseUrl: params.opts.customBaseUrl,
@@ -351,6 +361,8 @@ async function validateResetAuthChoice(params: {
351361
flagName: "--custom-api-key",
352362
envVar: "CUSTOM_API_KEY",
353363
runtime: params.runtime,
364+
agentDir: target.agentDir,
365+
workspaceDir: params.workspaceDir,
354366
allowProfile: params.resetScope === "config",
355367
required: false,
356368
secretInputMode: params.opts.secretInputMode,
@@ -376,7 +388,7 @@ async function validateResetAuthChoice(params: {
376388
return rejectOption(params.runtime, message);
377389
}
378390
}
379-
if (params.opts.nonInteractive && authChoice !== "custom-api-key" && authChoice !== "skip") {
391+
if (authChoice !== "custom-api-key") {
380392
const runtimeProvider = providerAuthChoice
381393
? resolveProviderMatch(
382394
resolvePluginProviders({
@@ -412,12 +424,15 @@ async function validateResetAuthChoice(params: {
412424
baseConfig: params.baseConfig,
413425
opts: params.opts,
414426
runtime: params.runtime,
427+
agentDir: target.agentDir,
415428
workspaceDir: params.workspaceDir,
416429
resolveApiKey: async (input) =>
417430
await resolveNonInteractiveCredential({
418431
...input,
419432
cfg: params.baseConfig,
420433
runtime: params.runtime,
434+
agentDir: target.agentDir,
435+
workspaceDir: params.workspaceDir,
421436
allowProfile: input.allowProfile === false ? false : params.resetScope === "config",
422437
secretInputMode: params.opts.secretInputMode,
423438
}),

0 commit comments

Comments
 (0)