different-ai/openwork · error · InterpreterRuntimeError
The right-hand side of 'instanceof' must be a constructor Co
Error message
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.
What it means
When the interpreter evaluates a binary `instanceof` expression, the right-hand side must resolve to one of a fixed allowlist of constructors that CodeMode models: Error types, Date, RegExp, Map, Set, URL, URLSearchParams, Array, Object, Promise (plus Number/String/Boolean coercion functions, which are statically false). If the RHS is any other value — undefined, a user class, a function, a sandbox value the interpreter doesn't recognize — it throws this InterpreterRuntimeError instead of silently misbehaving.
Source
Thrown at packages/codemode/src/interpreter/runtime.ts:330
case "Set":
return lhs instanceof SandboxSet
case "URL":
return lhs instanceof SandboxURL
case "URLSearchParams":
return lhs instanceof SandboxURLSearchParams
case "Array":
return Array.isArray(lhs)
case "Object":
return lhs !== null && (typeof lhs === "object" || typeofValue(lhs) === "function")
}
}
if (rhs instanceof PromiseNamespace) return lhs instanceof SandboxPromise
// Number/String/Boolean wrap primitives in JS; no boxed values exist in CodeMode, so
// `x instanceof Number` is always false - exactly what it is for primitives in JS.
if (rhs instanceof CoercionFunction && (rhs.name === "Number" || rhs.name === "String" || rhs.name === "Boolean")) {
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
}View on GitHub (pinned to 2b7df46e8a)
Solutions
- Rewrite the check to use the constructor's own discriminant instead of instanceof, e.g. `err.name === "TypeError"` or `err.message.includes(...)` for error narrowing.
- Restrict instanceof to the supported constructors listed in the message (Error, TypeError, Date, RegExp, Map, Set, URL, URLSearchParams, Array, Object, Promise).
- Use `Array.isArray(x)` / `typeof x` checks instead of `x instanceof Array/Object` where possible.
- Register/extend the sandbox with the needed class only if you control the codemode interpreter's constructor namespace.
Example fix
// before
if (err instanceof MyCustomError) { ... }
// after
if (err instanceof Error && err.name === "MyCustomError") { ... } Defensive patterns
Strategy: type-guard
Validate before calling
const KNOWN = ["Error","TypeError","RangeError","Date","RegExp","Map","Set","URL","URLSearchParams","Array","Object","Promise"]
const usesUnknownInstanceof = (script: string): boolean =>
KNOWN.some((k) => new RegExp(`instanceof\\s+${k}\\b`).test(script))
? false
: /instanceof\s+[A-Za-z_$]/.test(script) Type guard
const isKnownConstructor = (v: unknown): boolean => v === Error || v === TypeError || v === Date || v === RegExp || v === Map || v === Set || v === URL || v === URLSearchParams || v === Array || v === Object || v === Promise
Try / catch
try {
await program(script)
} catch (e) {
if (e instanceof Error && e.message.includes("right-hand side of 'instanceof'")) {
// rewrite the script: replace custom-class instanceof with err.name / property checks
} else throw e
} Prevention
- Ban `instanceof` with user-defined classes in scripts; lint generated code for it.
- Narrow errors via err.name or err.message instead of custom error classes.
- Prefer Array.isArray and typeof over instanceof Array/Object.
- Keep a whitelist test that runs representative scripts containing each supported constructor.
When it happens
Trigger: Evaluating `x instanceof SomeClass` where SomeClass is a user-defined class or an unknown/undefined identifier inside a CodeMode script; e.g. `err instanceof MyCustomError` or `v instanceof Foo` where Foo isn't one of the supported constructors.
Common situations: Porting Node.js code with custom error classes into CodeMode scripts, typos in constructor names, or LLM-generated code using application-specific classes the sandbox doesn't provide.
Related errors
- path_escape
- Invalid AST node while reading ${context}.
- Expected '${key}' to be an array.
- Expected '${key}' to be a string.
- Expected '${key}' to be a boolean.
AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01).
Data as JSON: /api/errors/1b0b41a951fe4f0a.
Report an issue: GitHub.