Hmbown/CodeWhale · error · Error

new Date()/Date() is unavailable in Workflow scripts: runs…

Error message

new Date()/Date() is unavailable in Workflow scripts: runs must be deterministic for record/replay

What it means

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].

Solutions

  1. Construct dates only from deterministic inputs via host-provided values (e.g. a task that returns an ISO string).
  2. Replace elapsed-time logic with values the host measures and passes in (started_at, duration).
  3. If you only need a fixed timestamp, pass it as a workflow input/constant.
  4. Use the host task envelope for any wall-clock-dependent computation.

Example fix

// before
const stamp = new Date().toISOString();
// after
const { stamp } = await task({ kind: "now_iso" });
Defensive patterns

Strategy: try-catch

Validate before calling

if (/new\s+Date\s*\(\s*\)/.test(scriptSource) || /(^|[^.\w])Date\s*\(\s*\)/.test(scriptSource)) {
  throw new Error('script constructs Date from wall clock; use host-provided timestamps');
}

Try / catch

try {
  await runWorkflow(script, input);
} catch (e) {
  if (e.message.startsWith('new Date()/Date() is unavailable')) {
    console.error('replace new Date() with a host task that returns the current time');
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/df5ef4da0b6d81d1. Report an issue: GitHub.

Appendix: source

Thrown at crates/workflow-js/src/vm.rs:1428

        "implement" | "implementer" | "builder" => Some(TaskRoleKind::Implementer),
        _ => None,
    }
}

/// The JS prelude injected before every script: determinism bans, the
/// `task`/`parallel`/`pipeline`/`log`/`phase` stdlib (design §7), and the
/// `budget` global.
fn prelude() -> String {
    PRELUDE_TEMPLATE.replace("__MAX_ITEMS__", &PARALLEL_MAX_ITEMS.to_string())
}

const PRELUDE_TEMPLATE: &str = r#""use strict";
(() => {
  const banned = (name) => () => {
    throw new Error(name + " is unavailable in Workflow scripts: runs must be deterministic for record/replay");
  };
  const BannedDate = function Date() {
    throw new Error("new Date()/Date() is unavailable in Workflow scripts: runs must be deterministic for record/replay");
  };
  BannedDate.now = banned("Date.now()");
  BannedDate.parse = banned("Date.parse()");
  BannedDate.UTC = banned("Date.UTC()");
  globalThis.Date = BannedDate;
  Math.random = banned("Math.random()");

  // Capture temporary host bindings into this closure, then strip them from
  // globalThis so scripts only see the documented Workflow surface (#4129).
  const hostTask = __workflow_task;
  const hostLog = __workflow_log;
  const hostEverySlotFailed = __workflow_every_slot_failed;
  const hostSlotDropped = __workflow_slot_dropped;
  const hostPhase = __workflow_phase;
  const hostBudgetTotal = __workflow_budget_total;
  const hostBudgetSpent = __workflow_budget_spent;
  const hostBudgetRemaining = __workflow_budget_remaining;

View on GitHub (pinned to 73e0f67d83)