{"record":{"id":"6af53d5ce77b5e0e","repo":"chatboxai/chatbox","slug":"could-not-allocate-a-resource-key-for-originalk","errorCode":null,"errorMessage":"Could not allocate a resource key for: ${originalKey}","messagePattern":"Could not allocate a resource key for: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/renderer/packages/backup/import-backup.ts","lineNumber":124,"sourceCode":"async function readStagedResource(storage: BackupStorage, plan: Pick<ResourceWritePlan, 'resource' | 'tempKey'>) {\n  const base64 = await storage.getBlob(plan.tempKey)\n  if (base64 === null) throw new Error(`Staged resource is missing: ${plan.resource.path}`)\n  return decodeStoredBlob(base64ToBytes(base64), plan.resource.encoding, plan.resource.mimeType)\n}\n\nasync function findAvailableCollisionKey(storage: BackupStorage, originalKey: string, reserved: Set<string>) {\n  for (let attempt = 0; attempt < 100; attempt++) {\n    let candidatePrefix = 'resource:imported'\n    for (const prefix of ['picture:', 'file:', 'link:', 'parseFile-', 'parseUrl-']) {\n      if (originalKey.startsWith(prefix)) {\n        candidatePrefix = `${prefix}imported`\n        break\n      }\n    }\n    const candidate = `${candidatePrefix}:${uuidv4()}`\n    if (!reserved.has(candidate) && (await storage.getBlob(candidate)) === null) return candidate\n  }\n  throw new Error(`Could not allocate a resource key for: ${originalKey}`)\n}\n\nasync function createResourcePlans(\n  manifest: BackupManifest,\n  stagedEntries: Map<string, StagedEntry>,\n  storage: BackupStorage\n) {\n  const plans: ResourceWritePlan[] = []\n  const resourceKeyMap = new Map<string, string>()\n  const reserved = new Set<string>()\n  for (const resource of manifest.resources) {\n    const staged = stagedEntries.get(resource.path)\n    if (!staged?.tempKey) throw new Error(`Resource was not staged: ${resource.path}`)\n    const restoredValue = await readStagedResource(storage, { resource, tempKey: staged.tempKey })\n    const targets: ResourceWritePlan['targets'] = []\n    for (const originalKey of resource.originalStorageKeys) {\n      const existingValue = await storage.getBlob(originalKey)\n      let targetKey = originalKey","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/chatboxai/chatbox/blob/81571269addb6bafb589a920b2883f1e1e084fd1/src/renderer/packages/backup/import-backup.ts#L106-L142","documentation":"Thrown by findAvailableCollisionKey after 100 attempts to mint a free resource key all failed - each random UUID candidate either was already in the reserved set or already existed in storage (getBlob !== null). With UUID v4 the probability is negligible, so hitting this almost always points to a faulty storage backend, not real key exhaustion. Propagates after rollback.","triggerScenarios":"100 consecutive candidates of the form '<prefix>imported:<uuid>' collide against the reserved set or storage.getBlob(candidate) !== null.","commonSituations":"A storage mock/stub whose getBlob always returns a non-null value; a corrupted storage layer; an adversarially seeded store. Practically never in production with correct UUID v4.","solutions":["Verify storage.getBlob returns null for genuinely absent keys (test the backend directly).","Increase the attempt budget in findAvailableCollisionKey if your storage legitimately cannot guarantee uniqueness.","Clear conflicting keys in the target namespace (picture:/file:/link:/parseFile-/parseUrl-) before import.","File a bug - real exhaustion is astronomically unlikely."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Sanity-check the storage backend before import: getBlob must return null for absent keys.\nconst probe = '__chatbox_collision_probe:' + uuidv4()\nif ((await storage.getBlob(probe)) !== null) {\n  throw new Error('Storage backend is faulty: getBlob returns non-null for absent keys')\n}","typeGuard":null,"tryCatchPattern":"try {\n  await importBackupArchive(file, options)\n} catch (error) {\n  if (error instanceof Error && error.message.startsWith('Could not allocate a resource key')) {\n    // Almost certainly a faulty storage backend; not real key exhaustion.\n    throw new Error('Storage backend failed key allocation; check getBlob correctness', { cause: error })\n  }\n  throw error\n}","preventionTips":["In storage mocks/stubs, make getBlob return null for unwritten keys.","Clear conflicting keys in picture:/file:/link:/parseFile-/parseUrl- namespaces before import.","Treat this error as a backend bug first and a capacity problem only second."],"tags":["backup","import","resource","key-allocation","unreachable","uuid"],"backgroundTag":null,"analyzedSha":"81571269addb6bafb589a920b2883f1e1e084fd1","analyzedAt":"2026-08-12T21:51:44.981Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}