{"record":{"id":"49db24ef4424c9e1","repo":"jackwener/OpenCLI","slug":"midjourney-reference-upload-fallback-lost-its-tran","errorCode":null,"errorMessage":"Midjourney reference upload fallback lost its transfer buffer","messagePattern":"Midjourney reference upload fallback lost its transfer buffer","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":1016,"sourceCode":"        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();\n    for (const item of window[key] || []) {\n      const binary = atob(item.chunks.join(''));\n      const bytes = new Uint8Array(binary.length);\n      for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);\n      transfer.items.add(new File([bytes], item.name, { type: item.mime }));\n    }\n    input.files = transfer.files;\n    const nativeEvent = new Event('change', { bubbles: true });\n    const propsKey = Object.keys(input).find((key) => key.startsWith('__reactProps$'));\n    if (propsKey && typeof input[propsKey]?.onChange === 'function') {\n      input[propsKey].onChange({ target: input, currentTarget: input, nativeEvent });\n    } else {","sourceCodeStart":998,"sourceCodeEnd":1034,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L998-L1034","documentation":"While streaming the file to the in-page transfer buffer in base64 chunks, each append is verified: the page-side code re-reads window[uploadKey][fileIndex] and pushes the chunk. If any append returns false — the buffer entry vanished or its chunks array is gone — this CommandExecutionError is thrown, since the partial upload would be corrupt.","triggerScenarios":"The page navigated or the execution context was destroyed mid-transfer, another script cleared window[uploadKey] between chunks, or the file entry was replaced with an object lacking a chunks array.","commonSituations":"Large files taking many chunks while the SPA re-renders or the user clicks something that reloads state, HMR/dev reloads clearing window state, or concurrent upload attempts overwriting the same key.","solutions":["Retry the upload; ensure no navigation or reload happens during the transfer","Avoid triggering other actions in the page while the upload is streaming","Use smaller images (fewer chunks, shorter transfer window) to reduce exposure","Update the CLI if the page environment changed how window state persists"],"exampleFix":"// before\nuploadPromise = uploadReferences(page, refs);\nawait page.reload(); // wipes window buffer mid-transfer\n// after\nawait uploadReferences(page, refs); // wait for transfer to finish first\nawait page.reload();","handlingStrategy":"retry","validationCode":"// before starting, ensure the buffer exists and will not be disturbed\nconst ready = await page.evaluate((k) => Array.isArray(window[k]) && window[k].length > 0, uploadKey);\nif (!ready) throw new Error('Transfer buffer missing before chunked upload');","typeGuard":"null","tryCatchPattern":"try {\n  await uploadLocalReference(page, localPath, descriptor);\n} catch (e) {\n  if (String(e.message).includes('lost its transfer buffer')) {\n    await page.reload();\n    await uploadLocalReference(page, localPath, descriptor); // restart transfer cleanly\n  } else throw e;\n}","preventionTips":["Keep uploads atomic: no clicks, reloads, or route changes while chunks stream","Prefer smaller images — fewer chunks means a smaller failure window","Restart the whole transfer on buffer loss; partial state cannot be resumed","Serialize uploads; concurrent attempts can clobber the shared window key"],"tags":["browser-automation","page-context","state-loss"],"backgroundTag":"browser-context-destroyed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}