different-ai/openwork · error · InterpreterRuntimeError

Math. expects number arguments.

Error message

Math.${name} expects number arguments.

What it means

Once the method name passes the whitelist check, CodeMode validates that every argument is a number. Non-numeric arguments (strings, NaN-producing values from upstream, undefined) are rejected with this error instead of being silently coerced, keeping sandbox math strict and predictable.

Solutions

  1. Coerce inputs explicitly with Number(x) before passing them to Math methods.
  2. Filter the array before spreading: nums.filter(n => typeof n === "number").
  3. Guard optional/missing values with a default before the call.

Example fix

// before
const m = Math.max(...values)
// after
const m = Math.max(...values.filter((n) => typeof n === "number"))
Defensive patterns

Strategy: type-guard

Validate before calling

const nums = args.filter((a) => typeof a === "number")
if (nums.length !== args.length) throw new TypeError("Math arguments must all be numbers")

Type guard

const isNum = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v)

Try / catch

try { return Math.max(...values) } catch (e) { if (/expects number arguments/.test(String(e))) return Math.max(...values.filter(isNum)); throw e }

Prevention

When it happens

Trigger: Calling any supported Math method with a non-number argument: Math.pow("2", 3), Math.max(...arr) where arr contains undefined, Math.log2(values[0]) where the array slot is missing.

Common situations: Spreading arrays that may contain holes or undefined entries (Math.max(...sparse)); passing numeric strings from JSON input without Number() conversion; downstream functions returning undefined on edge cases.

Related errors


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

Appendix: source

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

  "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)
    case "trunc":
      return Math.trunc(a)

View on GitHub (pinned to 2b7df46e8a)