Skip to content

Python Interop (Future)

Joe Curlee (w4ffl35) edited this page Feb 17, 2026 · 6 revisions

Python Interop (Future)

Status: future / post-v1.

Current v1 policy: python_ffi.* is not part of the supported language surface and is rejected at check/compile time.

Curlee is verification-first and capability-based. Python interoperability must not introduce ambient authority (file/network/system access) or non-determinism unless the user explicitly opts in.

Goals

  • Keep Python interop explicitly unsafe.
  • Keep Python interop capability-gated.
  • Preserve Curlee’s determinism and “no ambient authority” by default.
  • Enable a future “Curlee shield” workflow where Curlee verifies preconditions before delegating work.

Non-goals (MVP)

  • Full Python embedding or rich data marshaling.
  • Making Python calls available without an explicit opt-in.
  • Allowing Python interop to bypass verification.

Unsafe boundary

Python interop is a trust boundary: Python can do anything, and Curlee cannot generally verify or sandbox arbitrary Python.

Design rules:

  • Any call that may trigger Python execution must be within an explicit unsafe context.
  • Any python_ffi.* API is only callable from an unsafe context.

Capability-gated effects

Python execution is treated as an effect requiring an explicit capability token.

  • Capability name: python.ffi
  • Default: not granted

Without python.ffi, the runtime must refuse to execute any Python interop call.

Sandboxing (Linux, optional)

Granting python.ffi is a large escape hatch: it enables effects that Curlee cannot generally verify or reason about.

Curlee therefore supports an optional OS sandbox wrapper for the Python runner on Linux.

  • Capability name: python.sandbox
  • Default: not granted

When python.sandbox is granted, the VM should invoke the Python runner via an OS sandbox wrapper (initially: bubblewrap / bwrap) rather than executing the runner directly.

Rules:

  • Sandboxing is opt-in (explicit capability).
  • If sandboxing is requested but the wrapper cannot be executed, Curlee must fail deterministically (no silent fallback to an unsandboxed execution path).

python_ffi module boundary

Python interop lives behind a dedicated module boundary so it is easy to audit.

  • Module name: python_ffi
  • All APIs in this module are unsafe + effectful.
  • Imported usage should be clearly visible in source (no implicit magic).

Calling convention (initial)

Keep the initial surface minimal to avoid committing to a rich FFI ABI too early.

Planned MVP shape:

  • python_ffi.call(module, function, args...) -> String
    • arguments are marshaled as strings or integers initially
    • return values are strings or integers

Errors must be surfaced as Curlee diagnostics or runtime errors with span information where possible.

Curlee shield pattern

The intended safe workflow:

  1. Curlee function declares precise contracts (requires / ensures) over inputs.
  2. Curlee verifies these contracts.
  3. Only after verification succeeds, an unsafe block calls Python.
  4. Curlee validates/parses the Python result back into Curlee types.

Guideline: treat Python results as untrusted and validate invariants at the boundary.

Python execution strategy

Decision: use a subprocess runner, not in-process CPython embedding.

Rationale:

  • Isolation: a separate process contains crashes/memory corruption.
  • Operational simplicity: no libpython ABI/version coupling.
  • Capability story: the runner is invoked only when python.ffi is granted.

Tradeoffs:

  • Without OS sandboxing, granting python.ffi effectively opts into ambient authority and nondeterminism.
  • Curlee should not pretend Python execution is deterministic.

Decision (current): granting python.ffi is an explicit opt-in to nondeterminism. If Curlee later adopts a separate nondeterminism gate, this page must be updated.

Integration plan (minimal spike)

  1. Add a dedicated helper process (e.g. curlee_python_runner) invoked by the VM opcode.
  2. Define a tiny request/response protocol over stdin/stdout (JSON Lines) for module, function, and a minimal args encoding.
  1. Launch controls (best-effort isolation):
    • set PYTHONHASHSEED=0
    • run Python in isolated mode (-I) and (optionally) no-site (-S) if compatible
    • clear environment variables except an explicit allowlist
    • enforce a wall-time limit + output size limit
  2. Decide whether nondeterminism needs its own explicit CLI gate (separate from python.ffi) or whether python.ffi is sufficient.

Follow-up tasks

  • Implement the runner and wire it to the VM opcode.
  • Decide whether we split python.ffi into refined capabilities (e.g., python.ffi + fs:read, net:client, etc.).
  • Harden the Linux sandbox profile and decide whether to support an alternate backend (e.g. nsjail).
  • Expand marshaling once structured-data MVP exists.

Implementation status

  • Public language-level Python interop is intentionally de-scoped from v1.
  • Any future re-introduction must be issue-gated with explicit acceptance criteria for safety, determinism boundaries, diagnostics, and tests.

Clone this wiki locally