Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

refactor(task-copy): 落点以 mkdir 占用并逐个位置回退

Dash24163cee319d
diff --git a/apps/desktop/src/main/task-migration/__tests__/service.test.ts b/apps/desktop/src/main/task-migration/__tests__/service.test.ts
index 491eb9d0a3..88f58c9e6a 100644
--- a/apps/desktop/src/main/task-migration/__tests__/service.test.ts
+++ b/apps/desktop/src/main/task-migration/__tests__/service.test.ts
@@ -927,25 +927,32 @@ describe('resumable cross-computer copy', () => {
await start();
expect(await copiedDir()).toBe(path.join(state.root, 'app 2'));
});
- it('falls back to the project folder name when the mirrored parent cannot be created', async () => {
- await moveSource('Code', 'app');
- const blocked = path.join(state.root, 'Code');
- const mkdir = fs.mkdir.bind(fs);
- const spy = vi
- .spyOn(fs, 'mkdir')
- .mockImplementation(((dir, options) =>
- dir === blocked
- ? Promise.reject(Object.assign(new Error('not a directory'), { code: 'ENOTDIR' }))
- : mkdir(dir, options)) as typeof fs.mkdir);
- try {
- await start();
- const dir = await copiedDir();
- expect(dir).toBe(path.join(state.root, 'app'));
- expect(state.recentProjects).toEqual([dir]);
- } finally {
- spy.mockRestore();
- }
- });
+ it.each([
+ ['parent', ['Code']],
+ // E.g. a Windows reserved name such as CON: the parent exists, the folder itself is refused.
+ ['folder', ['Code', 'app 2']],
+ ])(
+ 'falls back to the project folder name when the mirrored %s cannot be created',
+ async (_kind, blockedPath) => {
+ await moveSource('Code', 'app');
+ const blocked = path.join(state.root, ...blockedPath);
+ const mkdir = fs.mkdir.bind(fs);
+ const spy = vi
+ .spyOn(fs, 'mkdir')
+ .mockImplementation(((dir, options) =>
+ dir === blocked
+ ? Promise.reject(Object.assign(new Error('refused'), { code: 'EINVAL' }))
+ : mkdir(dir, options)) as typeof fs.mkdir);
+ try {
+ await start();
+ const dir = await copiedDir();
+ expect(dir).toBe(path.join(state.root, 'app'));
+ expect(state.recentProjects).toEqual([dir]);
+ } finally {
+ spy.mockRestore();
+ }
+ },
+ );
it('copies a chat task into the dialogue workspace without adding a project', async () => {
state.rows.get('A')!.get('fork')!.workspaceKind = 'dialogue';
await start();
diff --git a/apps/desktop/src/main/task-migration/service.ts b/apps/desktop/src/main/task-migration/service.ts
index 025b3b03a8..6c143deb57 100644
--- a/apps/desktop/src/main/task-migration/service.ts
+++ b/apps/desktop/src/main/task-migration/service.ts
@@ -339,58 +339,91 @@ const folderName = (name: unknown): name is string =>
name !== '..' &&
!/[\\/\0]/.test(name) &&
(process.platform !== 'win32' || !/[<>:"|?*\x00-\x1f]|[. ]$/.test(name));
+interface Placement {
+ parent: string;
+ name: string;
+ project?: true;
+ /** Makes `parent` usable; rejects when this place cannot hold the copy. */
+ prepare(): Promise<unknown>;
+}
/**
- * Parent folder and name for a copy without a chosen project. A new project mirrors the source
- * path under home; a path this computer cannot create falls back to the source folder name alone,
- * then to an app-managed folder.
+ * Places a copy may land, in order. A chosen project or the dialogue workspace is the only place.
+ * A new project mirrors the source path under home, then tries the source folder name alone,
+ * then an app-managed folder, which is also where copies from older sources land.
*/
-async function defaultDestination(
+function placements(
scope: Scope,
- id: string,
+ request: { id: string; targetProject: string | null },
destination: WorkspaceBundle['destination'],
-): Promise<{ parent: string; name: string; project?: true }> {
+): Placement[] {
+ const managedName = `cindy-${request.id.slice(0, 8)}-${randomUUID()}`;
+ if (request.targetProject)
+ return [{ parent: request.targetProject, name: managedName, prepare: async () => {} }];
if (destination?.kind === 'dialogue') {
const { directory, isCustomized } = readDialogueWorkspaceSettings();
const parent = path.join(directory, dialogueWorkspaceDayKey(Date.now()));
- // A custom root is never recreated: an unmounted volume may leave a writable mount point.
- await fs
- .mkdir(parent, { recursive: !isCustomized })
- .catch(async (error: NodeJS.ErrnoException) => {
- if (error.code !== 'EEXIST' || !(await fs.stat(parent)).isDirectory()) throw error;
- });
- return { parent, name: id };
- }
- if (destination?.kind === 'project' && Array.isArray(destination.path)) {
- const segments = destination.path;
- const candidates = [segments.length <= 32 ? segments : [], segments.slice(-1)].filter(
- (candidate) => candidate.length && candidate.every(folderName),
- );
- for (const candidate of candidates) {
- const parent = path.join(app.getPath('home'), ...candidate.slice(0, -1));
- // E.g. a parent that is a file here, or a name this file system rejects.
- const created = await fs.mkdir(parent, { recursive: true }).then(
- () => true,
- () => false,
- );
- if (created) return { parent, name: candidate[candidate.length - 1], project: true };
- }
+ return [
+ {
+ parent,
+ name: request.id,
+ // A custom root is never recreated: an unmounted volume may leave a writable mount point.
+ prepare: () =>
+ fs
+ .mkdir(parent, { recursive: !isCustomized })
+ .catch(async (error: NodeJS.ErrnoException) => {
+ if (error.code !== 'EEXIST' || !(await fs.stat(parent)).isDirectory()) throw error;
+ }),
+ },
+ ];
}
- // Older sources, or no creatable project path: an app-managed folder, as before.
- const parent = path.join(scope.root, 'projects');
- await fs.mkdir(parent, { recursive: true, mode: 0o700 });
- return { parent, name: `cindy-${id.slice(0, 8)}-${randomUUID()}` };
+ const managed = path.join(scope.root, 'projects');
+ const fallback: Placement = {
+ parent: managed,
+ name: managedName,
+ prepare: () => fs.mkdir(managed, { recursive: true, mode: 0o700 }),
+ };
+ if (destination?.kind !== 'project' || !Array.isArray(destination.path)) return [fallback];
+ const segments = destination.path;
+ return [
+ ...[segments.length <= 32 ? segments : [], segments.slice(-1)]
+ .filter((candidate) => candidate.length && candidate.every(folderName))
+ .map((candidate): Placement => {
+ const parent = path.join(app.getPath('home'), ...candidate.slice(0, -1));
+ return {
+ parent,
+ name: candidate[candidate.length - 1],
+ project: true,
+ prepare: () => fs.mkdir(parent, { recursive: true }),
+ };
+ }),
+ fallback,
+ ];
}
-/** Never reuses an existing entry: `name`, then `name 2`, `name 3`… */
-async function unusedPath(parent: string, name: string): Promise<string> {
- for (let n = 1; n <= 100; n++) {
- const candidate = path.join(parent, n === 1 ? name : `${name} ${n}`);
- const taken = await fs.lstat(candidate).then(
- () => true,
- () => false,
- );
- if (!taken) return candidate;
+/**
+ * Creates the copy folder at the first place that accepts it. `mkdir` is the reservation: an
+ * existing entry is never reused (`name`, then `name 2`…), so concurrent copies cannot share one.
+ * Any other failure, such as a parent that is a file or a name the file system rejects, moves on.
+ */
+async function createWorkingDir(places: Placement[]): Promise<{ dir: string; place: Placement }> {
+ let failure: unknown = new Error('MIGRATION_TARGET_UNKNOWN');
+ for (const place of places) {
+ try {
+ await place.prepare();
+ for (let n = 1; n <= 100; n++) {
+ const dir = path.join(place.parent, n === 1 ? place.name : `${place.name} ${n}`);
+ try {
+ await fs.mkdir(dir);
+ return { dir, place };
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
+ }
+ }
+ throw new Error('MIGRATION_TARGET_UNKNOWN');
+ } catch (error) {
+ failure = error;
+ }
}
- throw new Error('MIGRATION_TARGET_UNKNOWN');
+ throw failure;
}
const workspaces = (workspace: WorkspaceBundle) => [
workspace,
@@ -693,7 +726,8 @@ async function preflight(
async function checkTargetResources(
scope: Scope,
targetProject: string | null,
- destination: string,
+ /** Where the files land; null before the target knows (the receive step checks it again). */
+ destination: string | null,
resources: MigrationResources,
) {
if (targetProject && !(await projects(scope)).includes(targetProject))
@@ -702,10 +736,15 @@ async function checkTargetResources(
await fs.mkdir(scope.root, { recursive: true });
await assertDiskCapacity([
{ path: scope.root, bytes: resources.transferBytes * 2 + resources.contextBytes * 2 },
- {
- path: destination,
- bytes: resources.unpackedBytes + resources.repositoryBytes * 3 + resources.entries * 4096,
- },
+ ...(destination
+ ? [
+ {
+ path: destination,
+ bytes:
+ resources.unpackedBytes + resources.repositoryBytes * 3 + resources.entries * 4096,
+ },
+ ]
+ : []),
{
path: app.getPath('temp'),
bytes: Math.min(resources.transferBytes, FILE_PEER_MAX_BYTES) * 2,
@@ -1048,45 +1087,25 @@ async function receive(
)
)
throw new Error('MIGRATION_INVALID_MANIFEST');
- const target = request.targetProject
- ? {
- parent: request.targetProject,
- name: `cindy-${request.id.slice(0, 8)}-${randomUUID()}`,
- }
- : await defaultDestination(scope, request.id, workspace.destination);
- scope.assertCurrent();
- await checkTargetResources(scope, request.targetProject, target.parent, {
- ...(transcriptFiles.length
- ? { transcriptBytes: transcriptFiles.reduce((sum, file) => sum + file.size, 0) }
- : {}),
- transferBytes: transferFiles(request.files).reduce((sum, file) => sum + file.size, 0),
- unpackedBytes: snapshots.reduce((sum, entry) => sum + entry.unpackedBytes, 0),
- contextBytes: Math.max(
- workspace.contextBytes ?? request.files.session.size,
- request.files.session.size,
- ),
- manifestBytes: request.files.manifest.size,
- repositoryBytes: [request.files, ...(request.files.additionalWorkspaces ?? [])].reduce(
- (sum, entry) => sum + (entry.repository?.size ?? 0),
- 0,
- ),
- entries: snapshots.reduce(
- (sum, entry) => sum + Object.keys(entry.archive.files ?? {}).length,
- 0,
- ),
- });
// Each failed attempt owns only its new directory. It never replaces a user's existing folder.
- // A retry reuses this copy's own still-empty folder rather than leaving `name 2` behind.
- const reusable =
- previous?.workingDir !== undefined &&
- path.dirname(previous.workingDir) === target.parent &&
+ // The record names a folder only after this copy created it, so a retry may reuse its own
+ // still-empty folder rather than leaving `name 2` behind. (A process exit between creating
+ // and recording leaves at most one empty folder.)
+ const places = placements(scope, request, workspace.destination);
+ const own = previous
+ ? places.find((place) => place.parent === path.dirname(previous.workingDir))
+ : undefined;
+ const reused =
+ own &&
+ previous &&
(await fs.readdir(previous.workingDir).then(
(entries) => entries.length === 0,
() => false,
- ));
- const workingDir = reusable
- ? previous.workingDir
- : await unusedPath(target.parent, target.name);
+ ))
+ ? { dir: previous.workingDir, place: own }
+ : null;
+ const { dir: workingDir, place } = reused ?? (await createWorkingDir(places));
+ scope.assertCurrent();
const retainedWorkingDirs = previous
? [
...new Set([
@@ -1104,11 +1123,30 @@ async function receive(
sourceSessionId: request.sourceSessionId,
stage: 'receiving',
workingDir,
- ...(target.project ? { newProject: true } : {}),
+ ...(place.project ? { newProject: true } : {}),
...(retainedWorkingDirs.length ? { retainedWorkingDirs } : {}),
};
scope.save(record);
- if (!reusable) await fs.mkdir(workingDir);
+ await checkTargetResources(scope, request.targetProject, path.dirname(workingDir), {
+ ...(transcriptFiles.length
+ ? { transcriptBytes: transcriptFiles.reduce((sum, file) => sum + file.size, 0) }
+ : {}),
+ transferBytes: transferFiles(request.files).reduce((sum, file) => sum + file.size, 0),
+ unpackedBytes: snapshots.reduce((sum, entry) => sum + entry.unpackedBytes, 0),
+ contextBytes: Math.max(
+ workspace.contextBytes ?? request.files.session.size,
+ request.files.session.size,
+ ),
+ manifestBytes: request.files.manifest.size,
+ repositoryBytes: [request.files, ...(request.files.additionalWorkspaces ?? [])].reduce(
+ (sum, entry) => sum + (entry.repository?.size ?? 0),
+ 0,
+ ),
+ entries: snapshots.reduce(
+ (sum, entry) => sum + Object.keys(entry.archive.files ?? {}).length,
+ 0,
+ ),
+ });
const targetDirs = [workingDir];
// Worker folders stay app-managed unless the user chose a project for the copy.
const workerParent = request.targetProject ?? path.join(scope.root, 'projects');
@@ -1296,7 +1334,8 @@ export async function requestTaskMigration(raw: unknown): Promise<TaskMigrationV
await checkTargetResources(
scope,
request.targetProject,
- request.targetProject ?? scope.root,
+ // Without a chosen project the landing folder depends on the task the manifest describes.
+ request.targetProject,
request.resources,
).catch(logTargetFailure('preflight', null));
return view(scope, null);
diff --git a/docs/product-rules/task-device-migration.md b/docs/product-rules/task-device-migration.md
index abb6eb81f8..03156d14f0 100644
--- a/docs/product-rules/task-device-migration.md
+++ b/docs/product-rules/task-device-migration.md
@@ -29,6 +29,8 @@
源项目不在用户目录下或路径在目标系统无法创建时,只用项目文件夹名放在用户目录下。
Worker 的独立目录在未选项目时放在 Cindy 管理的 `task-copies/projects` 下,不进用户目录。
旧版源端不提供任务类型、或文件夹名本身无法创建时,目标仍放在 `task-copies/projects` 下。
+ 目标以 `mkdir` 直接占用文件夹(并发复制不会选到同一个),任一位置创建失败都依次尝试下一个。
+ 未选项目时,复制前的预检不估算落点所在磁盘,接收端在解包前按实际落点检查空间。
3. 复用 peer 直传及 OSS 回退、冷却。按实时磁盘和可用内存预算准备、发送、接收及解包,
不设任务总大小上限;大文件按现有单对象传输容量分段,校验大小及 SHA-256 后解包。
目标声明 `externalTranscripts` 时,32 MiB 以上的原生会话记录(Codex rollout、Claude、Pi,含 Worker)