From 73b06f77ab3a7fa3d8255e4ee655bf07ec6136bd Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 8 May 2026 11:09:26 +0100 Subject: [PATCH] refactor: reset sqlite session state --- src/commands/agents.commands.delete.ts | 6 +----- src/commands/onboard-helpers.test.ts | 14 ++++++++++++++ src/commands/onboard-helpers.ts | 8 +++++--- src/gateway/server-methods/agents-mutate.test.ts | 5 ----- src/gateway/server-methods/agents.ts | 7 +------ 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/commands/agents.commands.delete.ts b/src/commands/agents.commands.delete.ts index fd6271b1121..f2c6a5a2c0b 100644 --- a/src/commands/agents.commands.delete.ts +++ b/src/commands/agents.commands.delete.ts @@ -3,7 +3,7 @@ import { resolveAgentDir, resolveAgentWorkspaceDir } from "../agents/agent-scope import { formatCliCommand } from "../cli/command-format.js"; import { replaceConfigFile } from "../config/config.js"; import { logConfigUpdated } from "../config/logging.js"; -import { purgeAgentSessionRows, resolveSessionTranscriptsDirForAgent } from "../config/sessions.js"; +import { purgeAgentSessionRows } from "../config/sessions.js"; import { callGateway, isGatewayTransportError } from "../gateway/call.js"; import { DEFAULT_AGENT_ID, normalizeAgentId } from "../routing/session-key.js"; import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js"; @@ -106,7 +106,6 @@ export async function agentsDeleteCommand( const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId); const agentDir = resolveAgentDir(cfg, agentId); - const sessionsDir = resolveSessionTranscriptsDirForAgent(agentId); const result = pruneAgentConfig(cfg, agentId); const gatewayResult = await maybeDeleteAgentThroughGateway({ @@ -124,7 +123,6 @@ export async function agentsDeleteCommand( workspaceRetainedReason: workspaceRetained ? "shared" : undefined, workspaceSharedWith: workspaceRetained ? workspaceSharedWith : undefined, agentDir, - sessionsDir, removedBindings: gatewayResult.removedBindings, removedAllow: result.removedAllow, transport: "gateway", @@ -159,7 +157,6 @@ export async function agentsDeleteCommand( await moveToTrash(workspaceDir, quietRuntime); } await moveToTrash(agentDir, quietRuntime); - await moveToTrash(sessionsDir, quietRuntime); if (opts.json) { writeRuntimeJson(runtime, { @@ -169,7 +166,6 @@ export async function agentsDeleteCommand( workspaceRetainedReason: workspaceRetained ? "shared" : undefined, workspaceSharedWith: workspaceRetained ? workspaceSharedWith : undefined, agentDir, - sessionsDir, removedBindings: result.removedBindings, removedAllow: result.removedAllow, }); diff --git a/src/commands/onboard-helpers.test.ts b/src/commands/onboard-helpers.test.ts index 1e793f049e2..82ff25235e7 100644 --- a/src/commands/onboard-helpers.test.ts +++ b/src/commands/onboard-helpers.test.ts @@ -56,14 +56,25 @@ describe("handleReset", () => { const profileConfigPath = path.join(profileStateDir, "openclaw.json"); const profileCredentialsDir = path.join(profileStateDir, "credentials"); const profileSessionsDir = path.join(profileStateDir, "agents", "main", "sessions"); + const profileAgentDb = path.join( + profileStateDir, + "agents", + "main", + "agent", + "openclaw-agent.sqlite", + ); const workspaceDir = path.join(profileStateDir, "workspace"); const defaultCredentialsDir = path.join(defaultStateDir, "credentials"); fs.mkdirSync(profileCredentialsDir, { recursive: true }); fs.mkdirSync(profileSessionsDir, { recursive: true }); + fs.mkdirSync(path.dirname(profileAgentDb), { recursive: true }); fs.mkdirSync(workspaceDir, { recursive: true }); fs.mkdirSync(defaultCredentialsDir, { recursive: true }); fs.writeFileSync(profileConfigPath, "{}\n"); + fs.writeFileSync(profileAgentDb, ""); + fs.writeFileSync(`${profileAgentDb}-wal`, ""); + fs.writeFileSync(`${profileAgentDb}-shm`, ""); vi.stubEnv("HOME", homeDir); vi.stubEnv("OPENCLAW_HOME", homeDir); @@ -80,6 +91,9 @@ describe("handleReset", () => { profileConfigPath, profileCredentialsDir, profileSessionsDir, + profileAgentDb, + `${profileAgentDb}-wal`, + `${profileAgentDb}-shm`, workspaceDir, ]); expect(trashedPaths).not.toContain(defaultCredentialsDir); diff --git a/src/commands/onboard-helpers.ts b/src/commands/onboard-helpers.ts index c2d6309e3aa..bc911fcabd2 100644 --- a/src/commands/onboard-helpers.ts +++ b/src/commands/onboard-helpers.ts @@ -4,8 +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 { resolveConfigPath } from "../config/paths.js"; -import { resolveSessionTranscriptsDirForAgent } from "../config/sessions/paths.js"; +import { resolveConfigPath, resolveStateDir } from "../config/paths.js"; import type { OptionalBootstrapFileName } from "../config/types.agent-defaults.js"; import type { OpenClawConfig } from "../config/types.openclaw.js"; import { resolveControlUiLinks } from "../gateway/control-ui-links.js"; @@ -23,6 +22,7 @@ import { normalizeOptionalString } from "../shared/string-coerce.js"; import { stylePromptTitle } from "../terminal/prompt-style.js"; import { resolveConfigDir, shortenHomeInString, shortenHomePath, sleep } from "../utils.js"; import { VERSION } from "../version.js"; +import { listAgentSessionStatePaths } from "./cleanup-utils.js"; import type { NodeManagerChoice, OnboardMode, ResetScope } from "./onboard-types.js"; export { randomToken } from "./random-token.js"; @@ -212,7 +212,9 @@ export async function handleReset(scope: ResetScope, workspaceDir: string, runti return; } await moveToTrash(path.join(resolveConfigDir(), "credentials"), runtime); - await moveToTrash(resolveSessionTranscriptsDirForAgent(), runtime); + for (const target of await listAgentSessionStatePaths(resolveStateDir())) { + await moveToTrash(target, runtime); + } if (scope === "full") { await moveToTrash(workspaceDir, runtime); } diff --git a/src/gateway/server-methods/agents-mutate.test.ts b/src/gateway/server-methods/agents-mutate.test.ts index 324639ba0ff..8afa54f2a89 100644 --- a/src/gateway/server-methods/agents-mutate.test.ts +++ b/src/gateway/server-methods/agents-mutate.test.ts @@ -22,7 +22,6 @@ const mocks = vi.hoisted(() => ({ isWorkspaceSetupCompleted: vi.fn(async () => false), resolveAgentDir: vi.fn((_cfg?: unknown, _agentId?: string) => "/agents/test-agent"), resolveAgentWorkspaceDir: vi.fn((_cfg?: unknown, _agentId?: string) => "/workspace/test-agent"), - resolveSessionTranscriptsDirForAgent: vi.fn((_agentId?: string) => "/transcripts/test-agent"), listAgentsForGateway: vi.fn(() => ({ defaultId: "main", mainKey: "agent:main:main", @@ -98,10 +97,6 @@ vi.mock("../../agents/workspace.js", async () => { }; }); -vi.mock("../../config/sessions/paths.js", () => ({ - resolveSessionTranscriptsDirForAgent: mocks.resolveSessionTranscriptsDirForAgent, -})); - vi.mock("../../plugin-sdk/browser-maintenance.js", () => ({ movePathToTrash: mocks.movePathToTrash, })); diff --git a/src/gateway/server-methods/agents.ts b/src/gateway/server-methods/agents.ts index 7f168e02197..780043bccab 100644 --- a/src/gateway/server-methods/agents.ts +++ b/src/gateway/server-methods/agents.ts @@ -27,10 +27,7 @@ import { pruneAgentConfig, } from "../../commands/agents.config.js"; import { replaceConfigFile } from "../../config/config.js"; -import { - purgeAgentSessionRows, - resolveSessionTranscriptsDirForAgent, -} from "../../config/sessions.js"; +import { purgeAgentSessionRows } from "../../config/sessions.js"; import type { IdentityConfig } from "../../config/types.base.js"; import type { OpenClawConfig } from "../../config/types.openclaw.js"; import { root, FsSafeError, type ReadResult } from "../../infra/fs-safe.js"; @@ -636,7 +633,6 @@ export const agentsHandlers: GatewayRequestHandlers = { const deleteFiles = typeof params.deleteFiles === "boolean" ? params.deleteFiles : true; const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId); const agentDir = resolveAgentDir(cfg, agentId); - const sessionsDir = resolveSessionTranscriptsDirForAgent(agentId); const result = pruneAgentConfig(cfg, agentId); await replaceConfigFile({ @@ -653,7 +649,6 @@ export const agentsHandlers: GatewayRequestHandlers = { await Promise.all([ ...(deleteWorkspace ? [moveToTrashBestEffort(workspaceDir)] : []), moveToTrashBestEffort(agentDir), - moveToTrashBestEffort(sessionsDir), ]); }