Skip to content

Python Interop (Future)

Joe Curlee (w4ffl35) edited this page Jan 27, 2026 · 6 revisions

Python Interop (Future)

Status: planned. Any Python interoperability must remain explicitly unsafe and capability-gated.

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.

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; it should be explicitly opted into.

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.).
  • Add OS-level sandboxing on Linux behind explicit capabilities.
  • Expand marshaling once structured-data MVP exists.

Implementation status

  • A stub exists in the interop layer and refuses to run without python:ffi.
  • Tests should cover missing capability behavior.

Clone this wiki locally