{"record":{"id":"fc8f7236ed5a16da","repo":"Hmbown/CodeWhale","slug":"parallel-max-max-items-items-per-call-vm","errorCode":null,"errorMessage":"parallel(): max ${MAX_ITEMS} items per call","messagePattern":"parallel\\(\\): max (.+?) items per call","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/workflow-js/src/vm.rs","lineNumber":1546,"sourceCode":"    const envelope = JSON.parse(await hostTask(JSON.stringify(opts)));\n    if (envelope.error !== undefined) {\n      const err = new Error(envelope.error);\n      // The host always names the kind; a missing one means an envelope this\n      // prelude did not produce, which is not a typed task failure.\n      err.kind = HOST_KINDS.indexOf(envelope.error_kind) !== -1\n        ? envelope.error_kind\n        : SCRIPT_KIND;\n      throw err;\n    }\n    return envelope.value;\n  };\n\n  globalThis.parallel = (thunks, opts) => {\n    if (!Array.isArray(thunks)) {\n      throw new TypeError(\"parallel(): expected an array of thunks\");\n    }\n    if (thunks.length > MAX_ITEMS) {\n      throw new Error(\"parallel(): max \" + MAX_ITEMS + \" items per call\");\n    }\n    const mode = slotMode(\"parallel\", opts);\n    const failFast = mode === \"fail-fast\";\n    const partial = mode === \"partial\";\n    const errors = [];\n    // Returns the slot value, or throws to reject the whole fan-out.\n    const onSlotError = (index, err) => {\n      const kind = taskErrorKind(err);\n      const message = taskErrorText(err);\n      stampKind(err, kind);\n      // Cancellation is the run's deadline in every mode, partial included.\n      if (kind === \"cancelled\") throw err;\n      if (partial) {\n        // Opt-in partial mode: every non-cancellation slot failure becomes a\n        // structured value the script can branch on. It never masquerades as\n        // a success -- `__taskError` is the whole point of the shape.\n        errors.push({ index: index, kind: kind, message: message });\n        hostLog(","sourceCodeStart":1528,"sourceCodeEnd":1564,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/workflow-js/src/vm.rs#L1528-L1564","documentation":"Thrown by the prelude's `globalThis.parallel` in vm.rs when the thunks array exceeds MAX_ITEMS (the PARALLEL_MAX_ITEMS constant baked into the prelude template). The cap bounds fan-out size so replayed runs stay within predictable resource limits.","triggerScenarios":"Calling `parallel(thunks)` where `thunks.length > MAX_ITEMS` (the limit value is printed in the error message); building the array dynamically from a large input collection without chunking.","commonSituations":"Mapping a large dataset (thousands of rows) into per-item thunks; unbounded fan-out over user-supplied lists; a config change that grew batch sizes past the cap.","solutions":["Chunk the work: split items into batches of at most MAX_ITEMS and run batches sequentially (or via pipeline).","Batch inside the host task instead: send the whole collection to one `task()` call and let the host process it.","Raise PARALLEL_MAX_ITEMS if your workload legitimately needs more (rebuild with a larger constant).","Pre-check `thunks.length` in-script and fail with a clearer domain-specific message."],"exampleFix":"// before\nconst results = await parallel(items.map(makeThunk));\n// after\nconst results = [];\nfor (let i = 0; i < items.length; i += MAX) {\n  results.push(...await parallel(items.slice(i, i + MAX).map(makeThunk)));\n}","handlingStrategy":"validation","validationCode":"function assertFanoutSize(thunks, MAX_ITEMS) {\n  if (thunks.length > MAX_ITEMS) {\n    throw new Error(`fan-out of ${thunks.length} exceeds parallel() cap of ${MAX_ITEMS}; chunk the input`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const results = await parallel(thunks);\n} catch (e) {\n  if (e.message.startsWith('parallel(): max ')) {\n    const max = Number(e.message.match(/max (\\d+)/)?.[1]);\n    console.error(`chunk input into batches of ${max} or move bulk work into a single host task`);\n  } else throw e;\n}","preventionTips":["Chunk large collections into batches within MAX_ITEMS.","Prefer one host task over per-item thunks for bulk data.","Cap fan-out at the source (paginate/limit inputs).","Read the cap from the error message rather than guessing the constant."],"tags":["workflow","limit-exceeded","fan-out"],"backgroundTag":"value-out-of-range","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}