Skip to content

Latest commit

 

History

History
102 lines (84 loc) · 6.08 KB

FOREIGN_RUNTIME_V1.md

File metadata and controls

102 lines (84 loc) · 6.08 KB

Native / Process Runtime v1

All adapters share ContractAdapter validation, read-only input semantics, negotiated formats, stream/epoch checks, block-boundary HOT revisions, bounded EOS and lifecycle failures. Serial and DAG workers select the manifest's runtime kind. Trusted ZIP installation, content hashes, version activation, reference protection and rollback apply to all three kinds. Neither native loading nor process separation is an OS permission sandbox.

Native C ABI

The public header is obliviousmic/sdk/native_v1.h (also shipped in the wheel). runtime.kind = native, entrypoint = gain.dll (or a local .so/.dylib). Exports use cdecl: obmic_abi_version, obmic_create, obmic_call, obmic_destroy. The host verifies ABI 1 and struct_size. Config is the UTF-8 manifest, whose describe reply must match exactly. The plugin owns one opaque handle; the host owns request/response and float32 buffers. No pointer may be retained after a call; no party frees another runtime's allocation. Replies report their actual byte and PCM lengths. Nonzero error status, invalid headers/descriptors or negotiated output violations fail the node. destroy releases the handle; explicit library release allows Windows package removal/reinstallation. Untrusted in-process native memory access can crash the host; run such code as an external worker instead.

The example gain DSP preserves frame counts, format, rational media position, stream, epoch, sequence and flags. Compile it using a C compiler, for example:

python -m pip install ziglang   # optional developer tool, not a runtime dependency
python -m ziglang cc -shared -O2 examples/modules/gain-native/gain.c -o TMPS/gain.dll
python tools/build_adapter_examples.py --library TMPS/gain.dll --output TMPS/examples

Install either generated ZIP in the module library. The module defaults to gain 1; gain is HOT. Native packages must match the host OS/architecture.

Process worker

runtime.kind = process, default worker = python, entrypoint = runtime:Gain. The host launches the packaged module in a standalone Python worker. No module import occurs in the host. Alternatively worker = external names a packaged executable in entrypoint and optional args (array, no shell). Other languages implement this protocol. Python's reference worker redirects module stdout to stderr so normal prints cannot corrupt control frames.

stdin/stdout use a four-byte unsigned big-endian length followed by UTF-8 JSON, maximum 1 MiB per frame. Every request/reply contains protocol: 1, consecutive sequence, method and args / result (or error). First request hello sends {protocol, shm, samples}; attach to the named shared memory. It contains two float32 slots, each samples long: slot 0 host input, slot 1 worker output. One request is in flight; writing its reply publishes the output slot. Output buffers are copied before release/reuse, so packets retained by downstream nodes remain valid. The host owns unlink; workers only close their attachment. Lifetime handling follows Python shared memory documentation.

The request args are an array encoded by adapters/wire.py. Audio packets carry {$:"audio", offset, frames, format, media_start, stream_id, epoch_id, sequence, flags}; offsets/counts are in samples, not bytes. Formats use $:"format"; rational seconds use {$:"fraction",n,d}. Bundles use {$:"bundle",ports:{...}}. Metadata, controls, contexts, latency and flush results use their SDK class name as $, with recursively encoded dataclass fields. User mappings containing $ are escaped as {$:"map",items:[[key,value],...]}. PCM never passes through JSON.

Supported methods are describe/prepare/start/process/update_parameters/report_latency/ flush/reset/stop/dispose and ping. The host serializes lifecycle calls. The transport deadline (default 2 s, configurable 0.05–10 s) also bounds a blocked stdin write; queues and PCM slots are fixed capacity (max_samples, default 1,048,576). Idle workers receive ping every 2 s. Exit, timeout, wrong protocol/sequence or method failure kills the worker. Dispose joins IPC threads and closes/unlinks shared memory. Graph faults use existing SafeGraph/LKG handling. Restart means an explicitly reprepared graph with a new worker/epoch, rather than replaying a stateful stream.

Foreign runtime v1 accepts serializable prepare capabilities. Callable host capabilities and action handlers belong in a separate Python control module; they are rejected rather than serialized or granted unrestricted host APIs. Model modules stay in their distribution repository and use PythonAdapter as before.

Verification and opt-in performance probe

tests/contract/test_foreign.py builds the real C library and checks adapter equality, HOT/EOS/reset, ownership, typed wire packets, installation/restart, serial/DAG execution, worker exit/hang, heartbeat and malformed protocol cleanup. Native tests require a C compiler (or ziglang); they explicitly skip if none is available.

python tools/benchmark_adapters.py --bundles TMPS/examples --output TMPS/probe.json

This bounded synthetic probe reports p50/p95/p99/max and RTF for 10 ms blocks; DAG timings include profiling overhead. It is not a neural inference or device benchmark.

Public failure codes

By default the Python worker sends only the exception class, never its message or provider response body. Modules may explicitly raise SDK PublicModuleError(code) with a stable lowercase identifier ([a-z][a-z0-9_]{0,95}). The optional error_code response field preserves that identifier across IPC and graph exception wrappers. Control jobs retain the existing exception-class error and add error_code when available. WebUI can translate known codes without logging keys, prompts or URLs. Modules must use declared, content-independent codes; never construct a code from user input or a provider body. Existing workers that omit the field keep their previous failure behavior. Failed workers are still stopped, without replaying a billable or stateful request automatically.