different-ai/openwork · error · InterpreterRuntimeError

String.${name} expects argument ${index + 1} to be a number.

Error message

String.${name} expects argument ${index + 1} to be a number.

What it means

This sandboxed String method dispatcher validates that numeric arguments (indexes, counts, positions) are actual JavaScript numbers before calling the host String method. The library throws this instead of letting a non-number reach String.prototype methods so errors stay inside the interpreter runtime with the offending AST node attached. Argument index+1 in the message is 1-based to match how developers count method arguments.

Source

Thrown at packages/codemode/src/interpreter/runtime.ts:346

    return false
  }
  throw new InterpreterRuntimeError(
    "The right-hand side of 'instanceof' must be a constructor CodeMode knows: Error (or a specific error type like TypeError), Date, RegExp, Map, Set, URL, URLSearchParams, Array, Object, or Promise.",
    node,
  )
}

const invokeStringMethod = (value: string, name: string, args: Array<unknown>, node: AstNode): unknown => {
  const str = (index: number): string => {
    const arg = args[index]
    if (typeof arg !== "string")
      throw new InterpreterRuntimeError(`String.${name} expects argument ${index + 1} to be a string.`, node)
    return arg
  }
  const num = (index: number): number => {
    const arg = args[index]
    if (typeof arg !== "number")
      throw new InterpreterRuntimeError(`String.${name} expects argument ${index + 1} to be a number.`, node)
    return arg
  }
  const optNum = (index: number): number | undefined => (args[index] === undefined ? undefined : num(index))
  const optStr = (index: number): string | undefined => (args[index] === undefined ? undefined : str(index))

  let result: unknown
  switch (name) {
    case "toLowerCase":
      result = value.toLowerCase()
      break
    case "toUpperCase":
      result = value.toUpperCase()
      break
    case "trim":
      result = value.trim()
      break
    // trimLeft/trimRight are the legacy aliases of trimStart/trimEnd, kept because models write them.
    case "trimStart":

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Convert the argument with Number(), parseInt/parseFloat, or unary + before passing it
  2. Check argument order so the numeric parameter receives the numeric value
  3. Ensure the value is not undefined for a required numeric argument (optional slots use optNum and tolerate undefined)
  4. Use typeof x === 'number' guards on dynamically sourced values

Example fix

// before
"hello sandbox".slice("0", 5)
// after
"hello sandbox".slice(Number(offset), 5) // offset coerced/validated as number
Defensive patterns

Strategy: type-guard

Validate before calling

function assertNum(v, name) { if (typeof v !== "number") throw new TypeError(`${name} must be a number, got ${typeof v}`); return v }
assertNum(index, "index"); s.slice(index, end)

Type guard

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

Try / catch

try { r = s.slice(i, j) } catch (e) { if (String(e.message).includes("to be a number")) { i = Number(i); j = Number(j); r = s.slice(i, j) } else throw e }

Prevention

When it happens

Trigger: Calling a sandboxed String method whose numeric argument is a string, undefined non-optional, boolean, or object: e.g. String('abc').slice('0', 2), String('abc').indexOf(1), String('x').repeat('3'). Also happens when optNum forwards a defined non-number value.

Common situations: Porting JS written where values came from JSON with string-typed numbers ('3' instead of 3); forgetting parseInt on user input; passing null where a number was expected; mixing up argument order so a string lands in a numeric slot.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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