Repository navigation
Python Runner Protocol
This page specifies the stable request/response protocol between the Curlee VM and the Python runner process (e.g., curlee_python_runner).
Goals:
- Small, stable, testable wire format.
- Deterministic and reproducible outputs suitable for golden tests.
- Clear error surface so the VM can convert runner failures into user-facing Curlee diagnostics.
Non-goals:
- Rich, general-purpose Python↔Curlee ABI (keep MVP minimal).
- Streaming/bidirectional channels beyond request/response.
- Transport: the VM starts the runner as a subprocess.
- Request stream: runner reads from stdin.
- Response stream: runner writes to stdout.
- Debug logging: runner writes to stderr only (never stdout).
Message framing:
- JSON Lines (JSONL): one JSON object per line, UTF-8 encoded.
- Each line must end with
\n. - No additional bytes may be written to stdout other than JSONL messages.
Size limits:
- The VM must cap request size and response size (implementation-defined).
- The runner must fail with a structured error if the request is too large.
Every request and response carries a protocol_version.
-
protocol_versionis an integer. - This document defines protocol version 1.
- If the runner receives an unsupported version, it must return an error response with
error.kind = "protocol_version_unsupported".
The runner is part of Curlee’s determinism story, so it must minimize nondeterminism.
Hard requirements (protocol-level):
- Responses must be purely derived from the request + runner code + Python code executed.
- Runner must not include timestamps, PIDs, random IDs, or hostnames in stdout responses.
- Runner must not include environment-dependent paths in stdout responses unless explicitly requested and marked as such.
Serialization requirements (for stable golden tests):
- Runner should emit canonical JSON on stdout: compact formatting, and object keys in lexicographic order.
- The VM should parse responses as JSON (not depend on formatting), but golden tests should prefer canonical output.
All requests have the following top-level shape:
{
"protocol_version": 1,
"id": "<opaque request id>",
"op": "call",
"limits": {
"wall_ms": 250,
"cpu_ms": 250,
"stdout_bytes": 65536,
"stderr_bytes": 65536
},
"call": {
"module": "math",
"function": "sqrt",
"args": ["9"],
"kwargs": {}
}
}Fields:
-
protocol_version(int, required): must be1. -
id(string, required): opaque identifier chosen by the VM; echoed in the response. -
op(string, required): operation name.-
"call": execute a Python function call. -
"handshake": protocol smoke-test (no-op), useful for CI. -
"echo": deterministic echo, useful for protocol tests.
-
-
limits(object, optional): best-effort limits the runner should enforce.-
wall_ms(int, optional): wall-clock timeout. -
cpu_ms(int, optional): CPU time limit (best-effort; may be unsupported on some platforms). -
stdout_bytes(int, optional): max bytes the VM will accept on stdout. -
stderr_bytes(int, optional): max bytes the VM will accept on stderr.
-
-
call(object, required ifop == "call"): describes the Python call.-
module(string, required): Python module to import. -
function(string, required): attribute on the imported module. -
args(array, optional): positional arguments. -
kwargs(object, optional): keyword arguments.
-
Argument encoding (MVP):
-
argselements are JSON values. - For MVP, the VM should send only strings/ints/bools and arrays of those.
- The runner must reject unsupported argument shapes with a structured
invalid_requesterror.
handshake is a minimal request used to validate that the runner can parse requests and that the
protocol_version is supported.
{
"protocol_version": 1,
"id": "t1",
"op": "handshake"
}echo is a deterministic operation used by tests to validate request/response plumbing.
{
"protocol_version": 1,
"id": "t2",
"op": "echo",
"echo": {"value": "hi"}
}For v1, echo.value must be a string.
All responses have this top-level shape:
{
"protocol_version": 1,
"id": "<opaque request id>",
"ok": true,
"result": {
"value": "3.0",
"type": "string"
}
}Fields:
-
protocol_version(int, required): must be1. -
id(string, required): must equal the requestid. -
ok(bool, required): success vs failure.
When ok == true, the runner must include:
-
result(object, required):-
type(string, required): one of"string","int","bool". -
value(string|int|bool, required): the returned value.
-
Notes:
- The runner must not write a Python traceback to stdout on success.
When ok == false, the runner must include:
{
"protocol_version": 1,
"id": "<opaque request id>",
"ok": false,
"error": {
"kind": "python_exception",
"message": "ValueError: bad value",
"details": {
"py_type": "ValueError",
"py_message": "bad value",
"traceback": [
{"file": "<redacted>", "line": 12, "function": "f"}
]
},
"retryable": false
}
}Error fields:
-
error.kind(string, required): machine-readable category. -
error.message(string, required): single-line summary suitable for end-user display. -
error.details(object, optional): structured details for diagnostics. -
error.retryable(bool, optional): whether retrying is plausible.
Standard error.kind values (v1):
-
invalid_request: schema violation or unsupported argument types. -
protocol_version_unsupported: unknown protocol version. -
timeout: wall-clock timeout exceeded. -
resource_limit: exceeded a provided limit (stdout/stderr bytes, etc). -
import_error: module import failed. -
attribute_error: function lookup failed. -
python_exception: Python raised an exception during execution. -
runner_crash: runner caught an internal exception or hit an unrecoverable state.
Timeout/limit signaling:
- For
timeout,error.detailsshould include{ "limit": {"wall_ms": ...}, "observed": {"wall_ms": ...} }when available. - For
resource_limit,error.detailsshould include which limit triggered, e.g.{ "limit": {"stdout_bytes": 65536} }.
The VM must turn a runner error response into a Curlee diagnostic anchored at the call site span for the python_ffi.* operation.
Guidelines:
- The primary diagnostic message should use
error.message. - The diagnostic “kind” should be derived from
error.kind(stable mapping). - If
error.kindispython_exception, the VM may attach a note that includes:-
py_typeandpy_message - a truncated traceback summary (if present)
-
- If the runner exits without a valid response, the VM must synthesize
runner_crashand include a hint to inspect stderr output.
User-facing hints (examples):
-
import_error: “Check the module name and that it’s installed in the runner environment.” -
attribute_error: “Check the function name and its spelling.” -
timeout: “Increase the python runner timeout or simplify the call.” -
invalid_request: “This call shape is not supported by the current python interop MVP.”