Import host call

Merged
SA
2mo ago
+6783-681

Summary by cubic

Sandbox code can now import host-provided modules: importing a module the sandbox lacks suspends as the import OS call and binds the host's answer. A not_found answer raises ModuleNotFoundError naming the first missing component of a dotted import, and a future is refused.

Bindings

  • external_modules (Python) / externalModules (JS) on the checkout methods names the modules a session may import, each an ExternalModule pairing the module's value with its optional .pyi stub. The value is a mapping (any mapping in Python) whose public callables become host functions named <module>.<attr>, a ClassInstance, a ClassType (which crosses as the type it wraps), or a zero-argument callable (async under AsyncMonty/JS) returning one of those, run once per session at the module's first need.
  • ExternalModule is immutable: it copies and validates its attrs at construction and carries an id; the entries mapping stays live, read at each first import. Host modules cross as real sandbox module objects, so type(tools) is module and attribute and call errors raise in the sandbox, and a module exports back with the id it imported with. Passing a module as a value is refused on both sides.
  • Entries are validated when a feed first imports them: a CPython module entry is refused, any other entry raises TypeError, and a stub name that is not an identifier or that the sandbox or its type checker already provides (now including hashlib, posixpath, and genericpath) is refused. Names and dict keys must be plain str identifiers (keywords refused, non-XID names in JS), an own undefined entry is an invalid entry rather than a missing module, and the wire repr quotes a module's name. A throwing getter surfaces at the import, and private names find nothing. modules on a ClassInstance entry and an entry nested inside itself are refused, as is a factory returning either. In JS a module entry must be an own property, and a plain value's own then is never run: it is boxed so calling it fails as not callable instead of being awaited.
  • Host module names are plain identifiers; a dotted import walks the host's answer one component at a time through a new LoadSubmodule opcode, capped at 32 components (SyntaxError). ExternalModule.modules nests submodules; a missing component raises ModuleNotFoundError naming the full path, and from a.b import keeps it on the frame. Repeated imports move to the end of the type-check prelude so the latest alias binds, and below a built-in module only a module is a submodule, so import math.pi raises ModuleNotFoundError.

Type checking and protocol

  • A type-checked feed raises the import_stubs OS call before it is checked, once per host module it imports that the session has not asked about; an ask naming a module twice is refused. The worker keeps the stubs it receives, carries them in the dump, and adopts the feed's cwd on arrival so a typing rejection after the ask keeps it. Checks resolve the imports from per-module stubs laid out as /<module>.pyi (a package for dotted paths); each committed import is re-injected once into later checks, and a reused worker never resolves a previous session's stubs. Diagnostics in the injected prelude are dropped, so a committed import x # type: ignore re-emits without its comment. The JS binding refuses keywords and sandbox module names without needing a stub; reserved names split into the sandbox's own modules, refused as host module names, and the checker-only ones, refused only as stub names.
  • Host-backed class instances are now callable, suspending __call__ on their uuid; the Python and JS wrappers answer with TypeError: 'X' object is not callable. PROTOCOL_VERSION bumps to 6 — a version 5 parent would answer neither new OS call — while DUMP_VERSION stays at 13: LoadAttrImport keeps its one-operand shape with the from ... import module name carried on the call frame. A TypingError answering anything other than a feed that type-checks is a protocol violation.