{"record":{"id":"df5ef4da0b6d81d1","repo":"Hmbown/CodeWhale","slug":"new-date-date-is-unavailable-in-workflow-scrip","errorCode":null,"errorMessage":"new Date()/Date() is unavailable in Workflow scripts: runs must be deterministic for record/replay","messagePattern":"new Date\\(\\)/Date\\(\\) is unavailable in Workflow scripts: runs must be deterministic for record/replay","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/workflow-js/src/vm.rs","lineNumber":1428,"sourceCode":"        \"implement\" | \"implementer\" | \"builder\" => Some(TaskRoleKind::Implementer),\n        _ => None,\n    }\n}\n\n/// The JS prelude injected before every script: determinism bans, the\n/// `task`/`parallel`/`pipeline`/`log`/`phase` stdlib (design §7), and the\n/// `budget` global.\nfn prelude() -> String {\n    PRELUDE_TEMPLATE.replace(\"__MAX_ITEMS__\", &PARALLEL_MAX_ITEMS.to_string())\n}\n\nconst PRELUDE_TEMPLATE: &str = r#\"\"use strict\";\n(() => {\n  const banned = (name) => () => {\n    throw new Error(name + \" is unavailable in Workflow scripts: runs must be deterministic for record/replay\");\n  };\n  const BannedDate = function Date() {\n    throw new Error(\"new Date()/Date() is unavailable in Workflow scripts: runs must be deterministic for record/replay\");\n  };\n  BannedDate.now = banned(\"Date.now()\");\n  BannedDate.parse = banned(\"Date.parse()\");\n  BannedDate.UTC = banned(\"Date.UTC()\");\n  globalThis.Date = BannedDate;\n  Math.random = banned(\"Math.random()\");\n\n  // Capture temporary host bindings into this closure, then strip them from\n  // globalThis so scripts only see the documented Workflow surface (#4129).\n  const hostTask = __workflow_task;\n  const hostLog = __workflow_log;\n  const hostEverySlotFailed = __workflow_every_slot_failed;\n  const hostSlotDropped = __workflow_slot_dropped;\n  const hostPhase = __workflow_phase;\n  const hostBudgetTotal = __workflow_budget_total;\n  const hostBudgetSpent = __workflow_budget_spent;\n  const hostBudgetRemaining = __workflow_budget_remaining;\n","sourceCodeStart":1410,"sourceCodeEnd":1446,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/workflow-js/src/vm.rs#L1410-L1446","documentation":"Thrown when a Workflow script invokes `new Date()` or calls `Date(...)` directly. The prelude replaces `globalThis.Date` with a `BannedDate` function whose body throws, because constructing a date from the wall clock would break record/replay determinism. `Date.now`/`parse`/`UTC` throw the sibling error [73].","triggerScenarios":"Executing `new Date()` with no arguments (or calling `Date()` as a function) anywhere in a workflow script body; any date computation that relies on the current time.","commonSituations":"Formatting 'now' for a log line or payload; measuring elapsed time inside a script with date arithmetic; copied-from-Node code that constructs Date objects.","solutions":["Construct dates only from deterministic inputs via host-provided values (e.g. a task that returns an ISO string).","Replace elapsed-time logic with values the host measures and passes in (started_at, duration).","If you only need a fixed timestamp, pass it as a workflow input/constant.","Use the host task envelope for any wall-clock-dependent computation."],"exampleFix":"// before\nconst stamp = new Date().toISOString();\n// after\nconst { stamp } = await task({ kind: \"now_iso\" });","handlingStrategy":"try-catch","validationCode":"if (/new\\s+Date\\s*\\(\\s*\\)/.test(scriptSource) || /(^|[^.\\w])Date\\s*\\(\\s*\\)/.test(scriptSource)) {\n  throw new Error('script constructs Date from wall clock; use host-provided timestamps');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await runWorkflow(script, input);\n} catch (e) {\n  if (e.message.startsWith('new Date()/Date() is unavailable')) {\n    console.error('replace new Date() with a host task that returns the current time');\n  } else throw e;\n}","preventionTips":["Construct Date only from deterministic string/epoch inputs supplied by the host.","Pass 'now' as a workflow input or host task result.","Lint for `new Date()` with no arguments in workflow sources.","Measure elapsed time on the host and pass durations in."],"tags":["workflow","determinism","sandbox"],"backgroundTag":"unsupported-operation","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"}