{"record":{"id":"586d0cd2ac1e3bea","repo":"pydantic/monty","slug":"cannot-instantiate-host-class-this-getname","errorCode":null,"errorMessage":"cannot instantiate host class '${this.getName()}'","messagePattern":"cannot instantiate host class '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/monty-js/ts/classInstance.ts","lineNumber":325,"sourceCode":"\n  /** Routes `__call__` (construction) to [`construct`](ClassType.construct);\n   *  every other name is a static-method call gated by `allowedMethods`. */\n  override callMethod(name: string, args: unknown[], kwargs: Record<string, unknown>): unknown {\n    if (name === '__call__') {\n      return this.construct(args, kwargs)\n    }\n    return super.callMethod(name, args, kwargs)\n  }\n\n  /**\n   * Constructs an instance for the sandbox, checking the `init` policy —\n   * a purely host-side gate that never crosses the wire. JS constructors\n   * have no keyword arguments, so a non-empty `kwargs` is appended as a\n   * final options bag, matching [`ClassInstance.callMethod`].\n   */\n  construct(args: unknown[], kwargs: Record<string, unknown>): ClassInstance {\n    if (this.options.init !== true) {\n      throw new TypeError(`cannot instantiate host class '${this.getName()}'`)\n    }\n    const callArgs = Object.keys(kwargs).length > 0 ? [...args, kwargs] : args\n    return this.instanceWrapper(new this.classType(...(callArgs as never[])))\n  }\n\n  /** Wraps a constructed instance with the `instance*` policies. The instance\n   *  carries this wrapper as its `classType`, so its class keeps this\n   *  wrapper's `id`, `name` and eager class attrs; a constructor that returns\n   *  an object of another class gets that class's default wrapper instead.\n   *  Override to customize how constructed instances are exposed. */\n  instanceWrapper(instance: object): ClassInstance {\n    const { instanceEagerAttrs, instanceLazyAttrs, instanceAllowedMethods, convertValue } = this.options\n    const ownClass = classOf(instance) === this.classType\n    return new ClassInstance(instance, {\n      eagerAttrs: instanceEagerAttrs,\n      lazyAttrs: instanceLazyAttrs,\n      allowedMethods: instanceAllowedMethods,\n      convertValue,","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/pydantic/monty/blob/adc986b362e3961f407868cb118a99fe831b9e61/crates/monty-js/ts/classInstance.ts#L307-L343","documentation":"ClassType never lets sandbox code instantiate a host class by default; construction is allowed only when the wrapper was created with `init: true`. This TypeError is raised in ClassType.construct (reached via __call__ when sandbox code calls the class) whenever the init gate is not exactly true — it is a purely host-side policy check applied on every construction request.","triggerScenarios":"Sandbox code runs `Point(1, 2)` (a `__call__` on the class wrapper) while the host registered the class with `new ClassType(Point)` or `new ClassType(Point, { init: false })` — i.e. init omitted or explicitly false.","commonSituations":"Forgetting init: true when granting a class you intend sandbox code to construct; a policy change tightening defaults; sandbox code evolved to construct a class the host only meant to expose for static methods/constants.","solutions":["Create the wrapper with `new ClassType(Point, { init: true })` to allow construction.","If the class should not be constructible, remove or guard the `Point(...)` call in the sandbox code.","Grant instance-level access a different way: construct the instance host-side and pass it as a ClassInstance input.","Pair init: true with instance* policies (instanceEagerAttrs, instanceLazyAttrs, instanceAllowedMethods) to control what constructed instances expose."],"exampleFix":"// before\ninputs: { Point: new ClassType(Point) } // sandbox: Point(1, 2) → TypeError\n// after\ninputs: { Point: new ClassType(Point, { init: true, instanceEagerAttrs: 'all' }) }","handlingStrategy":"try-catch","validationCode":"function canInstantiate(type: ClassType): boolean {\n  if (type.options.init !== true) {\n    throw new TypeError(`enable init: true on the ClassType for '${type.getName()}' before sandbox use`)\n  }\n  return true\n}","typeGuard":"function allowsInit(type: ClassType): boolean {\n  return type.options.init === true\n}","tryCatchPattern":"try {\n  await session.feedRun('p = Point(1, 2)', { inputs: { Point } })\n} catch (e) {\n  if (e instanceof TypeError && /cannot instantiate host class/.test(e.message)) {\n    const name = /'([^']+)'/.exec(e.message)?.[1]\n    throw new TypeError(`set init: true on the ClassType wrapper for '${name}'`)\n  } else throw e\n}","preventionTips":["Every ClassType you expect sandbox code to call must be created with init: true","Default is no construction — audit wrapper configs when sandbox code grows a constructor call","Pair init: true with instance* policies to keep constructed instances tightly scoped","init is host-side only; it is not part of the wire contract, so re-check it when reusing wrappers across sessions"],"tags":["permissions","policy","sandbox","configuration"],"backgroundTag":"feature-not-enabled","analyzedSha":"adc986b362e3961f407868cb118a99fe831b9e61","analyzedAt":"2026-09-13T19:19:18.698Z","contentChangedAt":"2026-09-13T19:19:18.698Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}