mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:01:23 +00:00
fix: archive old transcript files on /new and /reset (#14949)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: 4724df7dea
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
@@ -28,6 +28,7 @@ import {
|
||||
} from "../protocol/index.js";
|
||||
import {
|
||||
archiveFileOnDisk,
|
||||
archiveSessionTranscripts,
|
||||
listSessionsFromStore,
|
||||
loadCombinedSessionStoreForGateway,
|
||||
loadSessionEntry,
|
||||
@@ -68,6 +69,25 @@ function migrateAndPruneSessionStoreKey(params: {
|
||||
return { target, primaryKey, entry: params.store[primaryKey] };
|
||||
}
|
||||
|
||||
function archiveSessionTranscriptsForSession(params: {
|
||||
sessionId: string | undefined;
|
||||
storePath: string;
|
||||
sessionFile?: string;
|
||||
agentId?: string;
|
||||
reason: "reset" | "deleted";
|
||||
}): string[] {
|
||||
if (!params.sessionId) {
|
||||
return [];
|
||||
}
|
||||
return archiveSessionTranscripts({
|
||||
sessionId: params.sessionId,
|
||||
storePath: params.storePath,
|
||||
sessionFile: params.sessionFile,
|
||||
agentId: params.agentId,
|
||||
reason: params.reason,
|
||||
});
|
||||
}
|
||||
|
||||
export const sessionsHandlers: GatewayRequestHandlers = {
|
||||
"sessions.list": ({ params, respond }) => {
|
||||
if (!validateSessionsListParams(params)) {
|
||||
@@ -259,9 +279,13 @@ export const sessionsHandlers: GatewayRequestHandlers = {
|
||||
const cfg = loadConfig();
|
||||
const target = resolveGatewaySessionStoreTarget({ cfg, key });
|
||||
const storePath = target.storePath;
|
||||
let oldSessionId: string | undefined;
|
||||
let oldSessionFile: string | undefined;
|
||||
const next = await updateSessionStore(storePath, (store) => {
|
||||
const { primaryKey } = migrateAndPruneSessionStoreKey({ cfg, key, store });
|
||||
const entry = store[primaryKey];
|
||||
oldSessionId = entry?.sessionId;
|
||||
oldSessionFile = entry?.sessionFile;
|
||||
const now = Date.now();
|
||||
const nextEntry: SessionEntry = {
|
||||
sessionId: randomUUID(),
|
||||
@@ -289,6 +313,14 @@ export const sessionsHandlers: GatewayRequestHandlers = {
|
||||
store[primaryKey] = nextEntry;
|
||||
return nextEntry;
|
||||
});
|
||||
// Archive old transcript so it doesn't accumulate on disk (#14869).
|
||||
archiveSessionTranscriptsForSession({
|
||||
sessionId: oldSessionId,
|
||||
storePath,
|
||||
sessionFile: oldSessionFile,
|
||||
agentId: target.agentId,
|
||||
reason: "reset",
|
||||
});
|
||||
respond(true, { ok: true, key: target.canonicalKey, entry: next }, undefined);
|
||||
},
|
||||
"sessions.delete": async ({ params, respond }) => {
|
||||
@@ -357,24 +389,15 @@ export const sessionsHandlers: GatewayRequestHandlers = {
|
||||
}
|
||||
});
|
||||
|
||||
const archived: string[] = [];
|
||||
if (deleteTranscript && sessionId) {
|
||||
for (const candidate of resolveSessionTranscriptCandidates(
|
||||
sessionId,
|
||||
storePath,
|
||||
entry?.sessionFile,
|
||||
target.agentId,
|
||||
)) {
|
||||
if (!fs.existsSync(candidate)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
archived.push(archiveFileOnDisk(candidate, "deleted"));
|
||||
} catch {
|
||||
// Best-effort.
|
||||
}
|
||||
}
|
||||
}
|
||||
const archived = deleteTranscript
|
||||
? archiveSessionTranscriptsForSession({
|
||||
sessionId,
|
||||
storePath,
|
||||
sessionFile: entry?.sessionFile,
|
||||
agentId: target.agentId,
|
||||
reason: "deleted",
|
||||
})
|
||||
: [];
|
||||
|
||||
respond(true, { ok: true, key: target.canonicalKey, deleted: existed, archived }, undefined);
|
||||
},
|
||||
|
||||
@@ -361,6 +361,8 @@ describe("gateway server sessions", () => {
|
||||
expect(reset.ok).toBe(true);
|
||||
expect(reset.payload?.key).toBe("agent:main:main");
|
||||
expect(reset.payload?.entry.sessionId).not.toBe("sess-main");
|
||||
const filesAfterReset = await fs.readdir(dir);
|
||||
expect(filesAfterReset.some((f) => f.startsWith("sess-main.jsonl.reset."))).toBe(true);
|
||||
|
||||
const badThinking = await rpcReq(ws, "sessions.patch", {
|
||||
key: "agent:main:main",
|
||||
|
||||
@@ -3,6 +3,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
||||
import {
|
||||
archiveSessionTranscripts,
|
||||
readFirstUserMessageFromTranscript,
|
||||
readLastMessagePreviewFromTranscript,
|
||||
readSessionMessages,
|
||||
@@ -553,3 +554,80 @@ describe("resolveSessionTranscriptCandidates safety", () => {
|
||||
expect(normalizedCandidates).toContain(expectedFallback);
|
||||
});
|
||||
});
|
||||
|
||||
describe("archiveSessionTranscripts", () => {
|
||||
let tmpDir: string;
|
||||
let storePath: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-archive-test-"));
|
||||
storePath = path.join(tmpDir, "sessions.json");
|
||||
vi.stubEnv("OPENCLAW_HOME", tmpDir);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test("archives existing transcript file and returns archived path", () => {
|
||||
const sessionId = "sess-archive-1";
|
||||
const transcriptPath = path.join(tmpDir, `${sessionId}.jsonl`);
|
||||
fs.writeFileSync(transcriptPath, '{"type":"session"}\n', "utf-8");
|
||||
|
||||
const archived = archiveSessionTranscripts({
|
||||
sessionId,
|
||||
storePath,
|
||||
reason: "reset",
|
||||
});
|
||||
|
||||
expect(archived).toHaveLength(1);
|
||||
expect(archived[0]).toContain(".reset.");
|
||||
expect(fs.existsSync(transcriptPath)).toBe(false);
|
||||
expect(fs.existsSync(archived[0])).toBe(true);
|
||||
});
|
||||
|
||||
test("archives transcript found via explicit sessionFile path", () => {
|
||||
const sessionId = "sess-archive-2";
|
||||
const customPath = path.join(tmpDir, "custom-transcript.jsonl");
|
||||
fs.writeFileSync(customPath, '{"type":"session"}\n', "utf-8");
|
||||
|
||||
const archived = archiveSessionTranscripts({
|
||||
sessionId,
|
||||
storePath: undefined,
|
||||
sessionFile: customPath,
|
||||
reason: "reset",
|
||||
});
|
||||
|
||||
expect(archived).toHaveLength(1);
|
||||
expect(fs.existsSync(customPath)).toBe(false);
|
||||
expect(fs.existsSync(archived[0])).toBe(true);
|
||||
});
|
||||
|
||||
test("returns empty array when no transcript files exist", () => {
|
||||
const archived = archiveSessionTranscripts({
|
||||
sessionId: "nonexistent-session",
|
||||
storePath,
|
||||
reason: "reset",
|
||||
});
|
||||
|
||||
expect(archived).toEqual([]);
|
||||
});
|
||||
|
||||
test("skips files that do not exist and archives only existing ones", () => {
|
||||
const sessionId = "sess-archive-3";
|
||||
const transcriptPath = path.join(tmpDir, `${sessionId}.jsonl`);
|
||||
fs.writeFileSync(transcriptPath, '{"type":"session"}\n', "utf-8");
|
||||
|
||||
const archived = archiveSessionTranscripts({
|
||||
sessionId,
|
||||
storePath,
|
||||
sessionFile: "/nonexistent/path/file.jsonl",
|
||||
reason: "deleted",
|
||||
});
|
||||
|
||||
expect(archived).toHaveLength(1);
|
||||
expect(archived[0]).toContain(".deleted.");
|
||||
expect(fs.existsSync(transcriptPath)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -102,13 +102,45 @@ export function resolveSessionTranscriptCandidates(
|
||||
return Array.from(new Set(candidates));
|
||||
}
|
||||
|
||||
export function archiveFileOnDisk(filePath: string, reason: string): string {
|
||||
export type ArchiveFileReason = "bak" | "reset" | "deleted";
|
||||
|
||||
export function archiveFileOnDisk(filePath: string, reason: ArchiveFileReason): string {
|
||||
const ts = new Date().toISOString().replaceAll(":", "-");
|
||||
const archived = `${filePath}.${reason}.${ts}`;
|
||||
fs.renameSync(filePath, archived);
|
||||
return archived;
|
||||
}
|
||||
|
||||
/**
|
||||
* Archives all transcript files for a given session.
|
||||
* Best-effort: silently skips files that don't exist or fail to rename.
|
||||
*/
|
||||
export function archiveSessionTranscripts(opts: {
|
||||
sessionId: string;
|
||||
storePath: string | undefined;
|
||||
sessionFile?: string;
|
||||
agentId?: string;
|
||||
reason: "reset" | "deleted";
|
||||
}): string[] {
|
||||
const archived: string[] = [];
|
||||
for (const candidate of resolveSessionTranscriptCandidates(
|
||||
opts.sessionId,
|
||||
opts.storePath,
|
||||
opts.sessionFile,
|
||||
opts.agentId,
|
||||
)) {
|
||||
if (!fs.existsSync(candidate)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
archived.push(archiveFileOnDisk(candidate, opts.reason));
|
||||
} catch {
|
||||
// Best-effort.
|
||||
}
|
||||
}
|
||||
return archived;
|
||||
}
|
||||
|
||||
function jsonUtf8Bytes(value: unknown): number {
|
||||
try {
|
||||
return Buffer.byteLength(JSON.stringify(value), "utf8");
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
|
||||
export {
|
||||
archiveFileOnDisk,
|
||||
archiveSessionTranscripts,
|
||||
capArrayByJsonBytes,
|
||||
readFirstUserMessageFromTranscript,
|
||||
readLastMessagePreviewFromTranscript,
|
||||
|
||||
Reference in New Issue
Block a user