different-ai/openwork · error · InterpreterRuntimeError

Math.${name} is not available in CodeMode.

Error message

Math.${name} is not available in CodeMode.

What it means

CodeMode's Math object exposes only a whitelisted set of static methods (stored in the mathMethods Set). If the invoked method name is not in that set, the library throws this error before evaluating any arguments. This pre-check guards the whitelist so unsupported Math APIs fail fast with a clear message.

Source

Thrown at packages/codemode/src/stdlib/math.ts:23

  "min",
  "abs",
  "floor",
  "ceil",
  "round",
  "trunc",
  "sign",
  "sqrt",
  "cbrt",
  "pow",
  "hypot",
  "log",
  "log2",
  "log10",
  "exp",
])

export const invokeMathMethod = (name: string, args: Array<unknown>, node: AstNode): number => {
  if (!mathMethods.has(name)) throw new InterpreterRuntimeError(`Math.${name} is not available in CodeMode.`, node)
  const nums = args.map((arg) => {
    if (typeof arg !== "number") throw new InterpreterRuntimeError(`Math.${name} expects number arguments.`, node)
    return arg
  })
  const [a = Number.NaN, b = Number.NaN] = nums
  switch (name) {
    case "max":
      return Math.max(...nums)
    case "min":
      return Math.min(...nums)
    case "abs":
      return Math.abs(a)
    case "floor":
      return Math.floor(a)
    case "ceil":
      return Math.ceil(a)
    case "round":
      return Math.round(a)

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Use only whitelisted Math methods; check the mathMethods whitelist in packages/codemode/src/stdlib/math.ts for the supported list.
  2. Replace Math.random() with a seeded PRNG implemented in sandbox JS if randomness is needed deterministically.
  3. Implement unsupported helpers (e.g. clamp) inline: const clamp = (x, lo, hi) => Math.min(hi, Math.max(lo, x)).

Example fix

// before
const r = Math.random()
// after
const r = (seed = 1) => (seed * 16807) % 2147483647 / 2147483647
Defensive patterns

Strategy: validation

Validate before calling

const allowedMath = new Set(["abs","ceil","floor","round","sqrt","pow","min","max","log","log2","log10","exp"])
if (!allowedMath.has(fnName)) throw new Error(`Math.${fnName} is not whitelisted in CodeMode`)

Type guard

const isMathMethod = (n: string): n is "abs" | "ceil" | "floor" | "round" | "sqrt" | "pow" | "min" | "max" | "log" | "log2" | "log10" | "exp" => allowedMath.has(n)

Try / catch

try { return Math[fnName](x) } catch (e) { if (/Math\..+ is not available in CodeMode/.test(String(e))) return inlineImplementation(fnName, x); throw e }

Prevention

When it happens

Trigger: Calling Math.random(), Math.clamp(), Math.hypot(), Math.sign(), or any method missing from the whitelist (e.g. abs, ceil, floor, round, sqrt, pow, min, max, log, log2, log10, exp are supported); also dynamic dispatch Math[fn]() with an unexpected fn.

Common situations: Porting code that uses Math.random() — CodeMode forbids it for determinism; using newer Math methods like Math.clamp or Math.fsum from other engines; typos like Math.sqtr.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/a0e4af60b8d478c5. Report an issue: GitHub.