From 8e40bdba902c1a4836a3636f49a96b7657b439cf Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Sat, 25 Apr 2026 12:24:47 +0530 Subject: [PATCH] fix(cli): scope dev reset to active profile --- src/commands/onboard-helpers.test.ts | 41 ++++++++++++++++++++++++++++ src/commands/onboard-helpers.ts | 8 +++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/commands/onboard-helpers.test.ts b/src/commands/onboard-helpers.test.ts index b66c48b2a85..fb07427e830 100644 --- a/src/commands/onboard-helpers.test.ts +++ b/src/commands/onboard-helpers.test.ts @@ -1,7 +1,10 @@ +import * as fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; +import type { RuntimeEnv } from "../runtime.js"; import { + handleReset, normalizeGatewayTokenInput, openUrl, probeGatewayReachable, @@ -45,6 +48,44 @@ afterEach(() => { vi.unstubAllEnvs(); }); +describe("handleReset", () => { + it("uses active profile paths for destructive reset targets", async () => { + const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-reset-profile-")); + const profileStateDir = path.join(homeDir, ".openclaw-work"); + const defaultStateDir = path.join(homeDir, ".openclaw"); + const profileConfigPath = path.join(profileStateDir, "openclaw.json"); + const profileCredentialsDir = path.join(profileStateDir, "credentials"); + const profileSessionsDir = path.join(profileStateDir, "agents", "main", "sessions"); + const workspaceDir = path.join(profileStateDir, "workspace"); + const defaultCredentialsDir = path.join(defaultStateDir, "credentials"); + + fs.mkdirSync(profileCredentialsDir, { recursive: true }); + fs.mkdirSync(profileSessionsDir, { recursive: true }); + fs.mkdirSync(workspaceDir, { recursive: true }); + fs.mkdirSync(defaultCredentialsDir, { recursive: true }); + fs.writeFileSync(profileConfigPath, "{}\n"); + + vi.stubEnv("HOME", homeDir); + vi.stubEnv("OPENCLAW_HOME", homeDir); + vi.stubEnv("OPENCLAW_PROFILE", "work"); + vi.stubEnv("OPENCLAW_STATE_DIR", profileStateDir); + vi.stubEnv("OPENCLAW_CONFIG_PATH", profileConfigPath); + + const runtime = { log: vi.fn() } as unknown as RuntimeEnv; + + await handleReset("full", workspaceDir, runtime); + + const trashedPaths = mocks.runCommandWithTimeout.mock.calls.map(([argv]) => argv[1]); + expect(trashedPaths).toEqual([ + profileConfigPath, + profileCredentialsDir, + profileSessionsDir, + workspaceDir, + ]); + expect(trashedPaths).not.toContain(defaultCredentialsDir); + }); +}); + describe("openUrl", () => { it("passes OAuth URLs to Windows FileProtocolHandler without cmd parsing", async () => { vi.stubEnv("VITEST", ""); diff --git a/src/commands/onboard-helpers.ts b/src/commands/onboard-helpers.ts index b2fdff3be95..1b69d59faec 100644 --- a/src/commands/onboard-helpers.ts +++ b/src/commands/onboard-helpers.ts @@ -4,7 +4,7 @@ import { inspect } from "node:util"; import { cancel, isCancel } from "@clack/prompts"; import { DEFAULT_AGENT_WORKSPACE_DIR, ensureAgentWorkspace } from "../agents/workspace.js"; import { resolveAgentModelPrimaryValue } from "../config/model-input.js"; -import { CONFIG_PATH } from "../config/paths.js"; +import { resolveConfigPath } from "../config/paths.js"; import { resolveSessionTranscriptsDirForAgent } from "../config/sessions/paths.js"; import type { OpenClawConfig } from "../config/types.openclaw.js"; import { resolveControlUiLinks } from "../gateway/control-ui-links.js"; @@ -21,7 +21,7 @@ import { runCommandWithTimeout } from "../process/exec.js"; import type { RuntimeEnv } from "../runtime.js"; import { normalizeOptionalString } from "../shared/string-coerce.js"; import { stylePromptTitle } from "../terminal/prompt-style.js"; -import { CONFIG_DIR, shortenHomeInString, shortenHomePath, sleep } from "../utils.js"; +import { resolveConfigDir, shortenHomeInString, shortenHomePath, sleep } from "../utils.js"; import { VERSION } from "../version.js"; import type { NodeManagerChoice, OnboardMode, ResetScope } from "./onboard-types.js"; export { randomToken } from "./random-token.js"; @@ -205,11 +205,11 @@ export async function moveToTrash(pathname: string, runtime: RuntimeEnv): Promis } export async function handleReset(scope: ResetScope, workspaceDir: string, runtime: RuntimeEnv) { - await moveToTrash(CONFIG_PATH, runtime); + await moveToTrash(resolveConfigPath(), runtime); if (scope === "config") { return; } - await moveToTrash(path.join(CONFIG_DIR, "credentials"), runtime); + await moveToTrash(path.join(resolveConfigDir(), "credentials"), runtime); await moveToTrash(resolveSessionTranscriptsDirForAgent(), runtime); if (scope === "full") { await moveToTrash(workspaceDir, runtime);