fix: address the release-prep review of #680

Claude history merges, never overwriting, into a project directory a deleted case of the new name left behind, instead of staying orphaned under the old key.
The free-target pre-check also refuses a dangling symlink (lstat), so stopSessions no longer ends sessions before a 409 EXISTS.
The rename dialog says stopped sessions are closed (their tabs go away) and, after a failed rename, how many were already closed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-10-24 14:43:13 +02:00
1 parent ac6d4aa2db
commit d1e0e0aea6
10 files changed
+185 -39

No files matched your search

+37 -21
View File
@@ -131,34 +131,50 @@ export function claudeProjectDirName(path: string): string {
/**
* Move Claude Code's history for the old folder to the new one, so `--resume` and the past-sessions list
* still find the conversations. Leaves it alone when the new path already has history (nothing is merged
* or overwritten) or when there is none. Returns whether anything moved.
* still find the conversations. Returns whether anything moved.
*
* The new path can already have history: Claude Code never removes a project directory, and deleting a
* case does not either, so a case deleted earlier under the new name leaves one behind. The old history is
* then merged into it entry by entry (conversations are UUID-named, so they do not collide), never
* overwriting: an entry both directories have (a `memory/` folder, say) stays at the old path. A merge
* only goes between two real directories, never through a symlink.
*/
export async function moveClaudeHistory(oldPath: string, newPath: string, configDir?: string): Promise<boolean> {
const projects = claudeProjectsDir(configDir);
const from = join(projects, claudeProjectDirName(oldPath));
const to = join(projects, claudeProjectDirName(newPath));
if (from === to) return false;
if (
!(await fs.lstat(from).then(
() => true,
() => false
))
)
return false;
if (
await fs.lstat(to).then(
() => true,
() => false
)
)
return false;
try {
await fs.rename(from, to);
return true;
} catch {
return false; // history is a convenience; the folder move already succeeded
const fromStat = await fs.lstat(from).catch(() => null);
if (!fromStat) return false;
const toStat = await fs.lstat(to).catch(() => null);
if (!toStat) {
try {
await fs.rename(from, to);
return true;
} catch {
return false; // history is a convenience; the folder move already succeeded
}
}
if (!fromStat.isDirectory() || !toStat.isDirectory()) return false;
let moved = false;
for (const entry of await fs.readdir(from).catch(() => [] as string[])) {
const target = join(to, entry);
if (
await fs.lstat(target).then(
() => true,
() => false
)
)
continue;
try {
await fs.rename(join(from, entry), target);
moved = true;
} catch {
/* left at the old path, like an entry both have */
}
}
await fs.rmdir(from).catch(() => {}); // succeeds only once nothing was left behind
return moved;
}
/** Replace `from` by `to` in a list, keeping its position (the case order). */
+2 -2
View File
@@ -1346,8 +1346,8 @@
[/^Case renamed to "(.+)"$/, (_m, name) => `案例已重命名为“${name}”`],
[/^New name for "(.+)"$/, (_m, name) => `“${name}”的新名称`],
[
/^These sessions have the folder open: (.+)\. They will be stopped \(nothing is restarted\) before the folder is renamed\.$/,
(_m, list) => `这些会话正在使用该文件夹:${list}。重命名前将停止它们(不会重新启动)。`,
/^These sessions have the folder open: (.+)\. They will be closed \(their tabs go away and nothing is restarted\) before the folder is renamed\.$/,
(_m, list) => `这些会话正在使用该文件夹:${list}。重命名前将关闭它们(其标签页会消失,不会重新启动)。`,
],
[/^Failed to (.+)$/, (_m, action) => `操作失败:${action}`],
[/^Will create: (.+)$/, (_m, path) => `将创建:${path}`],
+7 -2
View File
@@ -5067,12 +5067,17 @@ Object.assign(CodemanApp.prototype, {
if (data.requiresStop) {
const list = (data.sessions || []).map((s) => `${s.name}`).join(', ');
state.stop = true;
this._renameCaseNotice(`These sessions have the folder open: ${list}. They will be stopped (nothing is restarted) before the folder is renamed.`);
this._renameCaseNotice(`These sessions have the folder open: ${list}. They will be closed (their tabs go away and nothing is restarted) before the folder is renamed.`);
if (submit) submit.textContent = 'Stop sessions and rename';
return;
}
if (!data.success) {
this._renameCaseNotice(data.error || 'Failed to rename the case.');
// A rename that failed after closing sessions says so: their tabs are gone either way.
const closed = Number(data.stoppedSessions) || 0;
const message = data.error || 'Failed to rename the case.';
this._renameCaseNotice(
closed > 0 ? `${message} (${closed} session${closed === 1 ? ' was' : 's were'} already closed.)` : message
);
return;
}
this.closeRenameCase();
+14 -2
View File
@@ -1819,8 +1819,20 @@ export function registerCaseRoutes(
const moveFolder = !isLinked || body.renameFolder === true;
const newPath = isLinked ? (moveFolder ? siblingPath(oldPath, newName) : oldPath) : join(casesBase, newName);
if (moveFolder && newPath !== oldPath && (await probePath(newPath, { pastCap: true })) !== 'absent') {
return fail(409, ApiErrorCode.ALREADY_EXISTS, `A folder named ${newPath} already exists`);
if (moveFolder && newPath !== oldPath) {
// probePath follows symlinks, so a dangling one reads as absent; moveCaseFolder would still refuse it
// (it checks with lstat), but only after the sessions below were stopped. The probe answered for this
// very path, so the lstat after it is not a first touch of an unreachable mount.
const target = await probePath(newPath, { pastCap: true });
const danglingLink =
target === 'absent' &&
(await fs.lstat(newPath).then(
() => true,
() => false
));
if (target !== 'absent' || danglingLink) {
return fail(409, ApiErrorCode.ALREADY_EXISTS, `A folder named ${newPath} already exists`);
}
}
// Sessions with the folder open. Only a move disturbs them; an alias-only rename leaves them running.