{"record":{"id":"47d51b3128a96a5e","repo":"jackwener/OpenCLI","slug":"midjourney-reference-upload-fallback-could-not-ini","errorCode":null,"errorMessage":"Midjourney reference upload fallback could not initialize its transfer buffer","messagePattern":"Midjourney reference upload fallback could not initialize its transfer buffer","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":1004,"sourceCode":"  const uploadKey = `opencli_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n  await page.evaluate((key) => {\n    window[key] = [];\n    return true;\n  }, uploadKey);\n  try {\n    for (const localPath of localPaths) {\n      const descriptor = {\n        name: path.basename(localPath),\n        mime: IMAGE_EXTENSIONS.get(path.extname(localPath).toLowerCase()),\n      };\n      const fileIndex = unwrapEvaluateResult(await page.evaluate((key, item) => {\n        const files = window[key];\n        if (!Array.isArray(files)) return -1;\n        files.push({ ...item, chunks: [] });\n        return files.length - 1;\n      }, uploadKey, descriptor));\n      if (!Number.isInteger(fileIndex) || fileIndex < 0) {\n        throw new CommandExecutionError('Midjourney reference upload fallback could not initialize its transfer buffer');\n      }\n      const base64 = (await fs.readFile(localPath)).toString('base64');\n      const chunkSize = 96 * 1024;\n      for (let offset = 0; offset < base64.length; offset += chunkSize) {\n        const chunk = base64.slice(offset, offset + chunkSize);\n        const appended = unwrapEvaluateResult(await page.evaluate((key, index, value) => {\n          const file = window[key]?.[index];\n          if (!file || !Array.isArray(file.chunks)) return false;\n          file.chunks.push(value);\n          return true;\n        }, uploadKey, fileIndex, chunk));\n        if (!appended) throw new CommandExecutionError('Midjourney reference upload fallback lost its transfer buffer');\n      }\n    }\n    const result = unwrapEvaluateResult(await page.evaluate((key) => {\n    const input = document.querySelector('[data-opencli-image-input=\"1\"]');\n    if (!(input instanceof HTMLInputElement)) return { ok: false, reason: 'image file input not found' };\n    const transfer = new DataTransfer();","sourceCodeStart":986,"sourceCodeEnd":1022,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L986-L1022","documentation":"For local files, the upload fallback initializes an in-page transfer buffer by pushing a file descriptor into a window-scoped array via page.evaluate. If the returned index is not a valid non-negative integer — meaning the buffer could not be created in the page context — this CommandExecutionError is thrown before any file data is transferred.","triggerScenarios":"The page.evaluate returning undefined/null due to a page navigation or context destruction mid-call, the evaluation being blocked by CSP or a page error, or the window-keyed array being reset between calls.","commonSituations":"The Midjourney tab navigated/reloaded while uploads were being prepared, an SPA route change destroyed the execution context, or an extension/script wiped the injected window state.","solutions":["Re-run the upload on a freshly loaded, stable page (avoid navigation during upload)","Retry — transient context destruction during SPA re-renders often resolves on a second attempt","Verify no browser extensions or page scripts interfere with injected window state","Update the CLI in case the page's CSP or structure changed"],"exampleFix":"// before\nawait page.click('nav a'); // triggers SPA navigation mid-upload\nawait uploadReferences(page, refs);\n// after\nawait uploadReferences(page, refs); // complete upload before any navigation\nawait page.click('nav a');","handlingStrategy":"retry","validationCode":"const ok = await page.evaluate((k) => Array.isArray(window[k]), uploadKey);\nif (!ok) throw new Error('Page context lost buffer key; reload before uploading');","typeGuard":"null","tryCatchPattern":"let lastErr;\nfor (let attempt = 0; attempt < 2; attempt++) {\n  try { await uploadReferences(page, refs); return; }\n  catch (e) {\n    if (!String(e.message).includes('transfer buffer')) throw e;\n    lastErr = e;\n    await page.reload();\n  }\n}\nthrow lastErr;","preventionTips":["Never navigate or reload the page during an upload","Upload on a freshly loaded, idle page","Avoid extensions/scripts that clear injected window state","Wrap uploads in a bounded retry with a page reload between attempts"],"tags":["browser-automation","page-context","injection"],"backgroundTag":"browser-context-destroyed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}