mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-20 13:21:26 +00:00
refactor: reset sqlite session state
This commit is contained in:
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}));
|
||||
|
||||
@@ -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),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user