refactor: reset sqlite session state

This commit is contained in:
Peter Steinberger
2026-05-08 11:09:26 +01:00
parent aa2c72140d
commit d49e0fecdf
5 changed files with 21 additions and 19 deletions

View File

@@ -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,
});

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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,
}));

View File

@@ -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),
]);
}