← Back to Pull requests

Import host call #942

Closed
unknown closed this pull request without merging on Oct 11, 2026 22:09 UTC
View on GitHub
unknown unknown commented on Sep 26, 2026 17:59 UTC
Author

<!-- This is an auto-generated description by cubic. -->

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.
  • <sup>Written for commit b886bc28f508e3d236ddc9e301e131968cded224. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/pydantic/monty/pull/942?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/pydantic/monty/942?returnTo=https%3A%2F%2Fgithub.com%2Fpydantic%2Fmonty%2Fpull%2F942&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: rewitt94 Review duration: 21850m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #942"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%
    Repository