{"record":{"id":"691516e4962ff9ca","repo":"CherryHQ/cherry-studio","slug":"agent-data-directory-already-exists-agentdatapa","errorCode":null,"errorMessage":"Agent data directory already exists: ${agentDataPath}","messagePattern":"Agent data directory already exists: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/ai/agents/agentDataDirectory.ts","lineNumber":148,"sourceCode":"  await ensureAgentStorageDirectory(agentsDataRoot, agentDataPath)\n  await ensureAgentStorageDirectory(agentsDataRoot, path.join(agentDataPath, 'memory'))\n\n  if (options.createFiles !== false) {\n    for (const filename of AGENT_DATA_FILES) {\n      await ensureEmptyFile(path.join(agentDataPath, filename))\n    }\n  }\n\n  await assertAgentDataDirectory(agentsDataRoot, agentId)\n  return agentDataPath\n}\n\nexport async function createAgentDataDirectory(agentsDataRoot: string, agentId: string): Promise<string> {\n  const agentDataPath = agentDataDirectoryPath(agentsDataRoot, agentId)\n  await ensureAgentStorageDirectory(agentsDataRoot, agentsDataRoot)\n  await assertAgentStoragePath(agentsDataRoot, agentDataPath)\n  if (await lstatIfExists(agentDataPath)) {\n    throw new Error(`Agent data directory already exists: ${agentDataPath}`)\n  }\n\n  await mkdir(asAbsolutePath(agentDataPath))\n  try {\n    await ensureAgentDataDirectory(agentsDataRoot, agentId)\n    return agentDataPath\n  } catch (error) {\n    await removeAgentDataDirectory(agentsDataRoot, agentId).catch(() => undefined)\n    throw error\n  }\n}\n\nexport async function assertAgentDataDirectory(agentsDataRoot: string, agentId: string): Promise<string> {\n  const agentDataPath = agentDataDirectoryPath(agentsDataRoot, agentId)\n  await assertAgentStoragePath(agentsDataRoot, agentDataPath)\n\n  const rootStat = await lstatIfExists(agentDataPath)\n  if (!rootStat?.isDirectory || rootStat.isSymbolicLink) {","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/agents/agentDataDirectory.ts#L130-L166","documentation":"Thrown by createAgentDataDirectory when the target agent data directory already exists at the moment of creation. Unlike ensureAgentDataDirectory (which is idempotent and creates-if-missing), createAgentDataDirectory is strict: it is meant for first-time creation of a fresh agent data dir, so an existing path is treated as a collision (duplicate agentId or double creation).","triggerScenarios":"Calling createAgentDataDirectory(agentsDataRoot, agentId) when {agentsDataRoot}/{agentId} already exists on disk — e.g. retrying agent creation after a partial failure, reusing an agentId, or a collision in ID generation.","commonSituations":"Retrying an agent-creation flow that partially succeeded; importing an agent whose ID collides with an existing one; a previous create left the directory behind before failing; manually copied agent data directories.","solutions":["If you only need the directory to exist, call ensureAgentDataDirectory instead — it is idempotent and will not throw.","Check existence first with assertAgentDataDirectory and skip creation if it already validates.","Generate a fresh agentId for the new agent to avoid the collision.","If the existing directory is stale/orphaned, remove it via removeAgentDataDirectory first, then retry create."],"exampleFix":"// before: strict create throws on retry\nawait createAgentDataDirectory(root, agentId)\n\n// after: ensure is idempotent for the common case\nawait ensureAgentDataDirectory(root, agentId)\n// use createAgentDataDirectory only when you must assert a brand-new directory","handlingStrategy":"validation","validationCode":"const exists = await lstatIfExists(agentDataDirectoryPath(root, agentId))\nif (exists) {\n  // already provisioned — use the idempotent path\n  await ensureAgentDataDirectory(root, agentId)\n} else {\n  await createAgentDataDirectory(root, agentId)\n}","typeGuard":"function isDirectoryEntry(stat: import('node:fs').Stats): boolean {\n  return stat.isDirectory() && !stat.isSymbolicLink()\n}","tryCatchPattern":"try {\n  await createAgentDataDirectory(root, agentId)\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Agent data directory already exists')) {\n    await ensureAgentDataDirectory(root, agentId) // fall back to idempotent ensure\n  } else throw e\n}","preventionTips":["Prefer ensureAgentDataDirectory for idempotent provisioning.","Generate fresh agentIds to avoid collisions on retry.","Clean up stale dirs with removeAgentDataDirectory before recreating."],"tags":["filesystem","agents","state-conflict","duplicate"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}