# 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 & framing - 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. ## Versioning Every request and response carries a `protocol_version`. - `protocol_version` is 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"`. ## Determinism constraints 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. ## Request schema (v1) All requests have the following top-level shape: ```json { "protocol_version": 1, "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 be `1`. - `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 if `op == "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): - `args` elements 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_request` error. ### `handshake` operation (v1) `handshake` is a minimal request used to validate that the runner can parse requests and that the `protocol_version` is supported. ```json { "protocol_version": 1, "id": "t1", "op": "handshake" } ``` ### `echo` operation (v1) `echo` is a deterministic operation used by tests to validate request/response plumbing. ```json { "protocol_version": 1, "id": "t2", "op": "echo", "echo": {"value": "hi"} } ``` For v1, `echo.value` must be a string. ## Response schema (v1) All responses have this top-level shape: ```json { "protocol_version": 1, "id": "", "ok": true, "result": { "value": "3.0", "type": "string" } } ``` Fields: - `protocol_version` (int, required): must be `1`. - `id` (string, required): must equal the request `id`. - `ok` (bool, required): success vs failure. ### Success response 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. ### Error response When `ok == false`, the runner must include: ```json { "protocol_version": 1, "id": "", "ok": false, "error": { "kind": "python_exception", "message": "ValueError: bad value", "details": { "py_type": "ValueError", "py_message": "bad value", "traceback": [ {"file": "", "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.details` should include `{ "limit": {"wall_ms": ...}, "observed": {"wall_ms": ...} }` when available. - For `resource_limit`, `error.details` should include which limit triggered, e.g. `{ "limit": {"stdout_bytes": 65536} }`. ## Diagnostics derivation (VM side) 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.kind` is `python_exception`, the VM may attach a note that includes: - `py_type` and `py_message` - a truncated traceback summary (if present) - If the runner exits without a valid response, the VM must synthesize `runner_crash` and 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.”