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:
Marcus Castro
2026-02-13 16:55:16 -03:00
committed by GitHub
parent c8b198ab51
commit 31537c669a
8 changed files with 211 additions and 19 deletions

View File

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