Skip to content

Runtime Control & Objects ​

Python Preview 4 APIs expose the same runtime-control and object-reference wire model as the Rust core. SDK implementations must keep these names and field spellings stable for NNRP/1.

Use this page when you need cancellation, priority changes, progress streaming, partial results, runtime object references, cache references, or WebSocket binary frame helpers.

Import ​

python
from nnrp import MessageType
from nnrp.runtime import (
    decode_runtime_control_metadata,
    decode_runtime_object_metadata,
    decode_websocket_binary_frame,
    decode_websocket_binary_frame_batch,
    encode_runtime_control_metadata,
    encode_runtime_object_metadata,
    encode_websocket_binary_frame,
)
from nnrp.runtime.types import (
    ProgressMetadata,
    RuntimeFrameHeader,
)

encode_runtime_control_metadata ​

Encodes one Preview 4 control metadata object.

ParameterTypeRequiredDescription
message_typeMessageTypeYesOne of CANCEL, ABORT, PRIORITY_UPDATE, DEADLINE, EXPIRE_AT, SUPERSEDE, BUDGET_UPDATE, PROGRESS, PARTIAL_RESULT, BACKPRESSURE, CREDIT_UPDATE, CAPABILITY_NEGOTIATION, DEGRADE_PROFILE, ROUTE_HINT, EXECUTION_HINT, TRACE_CONTEXT, RESULT_DROP_REASON, ERROR_RECOVERABLE, or RETRY_AFTER.
metadataRuntime control metadataYesMetadata object matching message_type.
tailbytesNoExtension bytes, diagnostic bytes, progress body, or partial-result body required by metadata.
Returns
bytes
python
payload = encode_runtime_control_metadata(
    MessageType.PROGRESS,
    ProgressMetadata(operation_id=42, progress_sequence=1, stage_code=2, percent_x100=2500, object_id=0, body_bytes=0),
)

decode_runtime_control_metadata ​

Decodes one Preview 4 control metadata payload.

ParameterTypeRequiredDescription
message_typeMessageTypeYesMessage type that selected the metadata layout.
payloadbytesYesMetadata bytes plus the declared tail.
Returns
DecodedRuntimeControlMetadata with metadata and tail

encode_runtime_object_metadata ​

Encodes object, object-reference, object-delta, cache-reference, and cache-miss metadata.

ParameterTypeRequiredDescription
message_typeMessageTypeYesOne of OBJECT_DECLARE, OBJECT_REF, OBJECT_RELEASE, OBJECT_PATCH, OBJECT_DELTA, CACHE_REFERENCE, or CACHE_MISS.
metadataRuntime object metadataYesMetadata object matching message_type.
tailbytesNoExtension bytes, diagnostics, or delta payload declared by metadata.
Returns
bytes

decode_runtime_object_metadata ​

Decodes one runtime object or cache metadata payload.

ParameterTypeRequiredDescription
message_typeMessageTypeYesMessage type that selected the metadata layout.
payloadbytesYesMetadata bytes plus the declared tail.
Returns
DecodedRuntimeObjectMetadata with metadata and tail

High-Level Runtime Frame Contract ​

The codec functions above are advanced protocol helpers. Normal applications send Preview4 frames through the named methods on NativeRuntimeSession or NativeRuntimeServerSession; they do not encode payloads, select message types, or call control() themselves.

The SDK validates each metadata/message pairing, encodes the complete payload, and performs one coarse native call behind those named methods. That role-neutral frame-send primitive is an internal binding boundary and is not part of the public session API. Protocol extensions add a typed metadata model and a named SDK method instead of exposing raw frame construction to applications.

NativeRuntimeFrameEvent ​

Runtime-frame polling returns a decoded NativeRuntimeFrameEvent, not a raw control code and byte buffer. Its frozen fields are type, message_type, metadata, body, diagnostic, metadata_body, delta, connection, session, operation, frame_id, and native_diagnostic. Fields that do not apply to a message contain b"".

type uses the kebab-case names from the JavaScript runtime event table. metadata is the matching typed metadata object. Object patch and delta events split their tail into metadata_body and delta; all other declared tails are exposed as body or diagnostic. Bindings copy and release the native owned payload before returning the event.

NativeRuntimeEvent.to_runtime_frame() returns a NativeRuntimeFrameEvent for Preview4 runtime frames and None for submit/result/lifecycle events. Role events are session-scoped in the Rust ABI, so NativeRuntimeSession.poll_runtime_frames() and iter_runtime_frames() skip non-runtime events and return the decoded type directly. The client connection does not expose a connection-wide event pump because it cannot preserve unambiguous ownership when several sessions are open.

encode_websocket_binary_frame ​

Builds the binary frame used by the WebSocket transport.

ParameterTypeRequiredDescription
headerRuntimeFrameHeaderYesHeader fields except meta_len and body_len; the helper derives both lengths from the buffers.
metadatabytesNoMetadata payload.
bodybytesNoBody payload.
Returns
bytes

decode_websocket_binary_frame ​

Splits one WebSocket binary frame into header, metadata slice, and body slice.

ParameterTypeRequiredDescription
framebytesYesOne complete WebSocket binary message.
Returns
DecodedRuntimeFrame

decode_websocket_binary_frame_batch ​

Decodes concatenated binary frames from local buffers and conformance fixtures.

ParameterTypeRequiredDescription
batchbytesYesConcatenated frames.
limitintNoMaximum decoded frames; 0 means no limit.
Returns
list[DecodedRuntimeFrame]

Runtime Control Metadata ​

All integer fields are unsigned unless the field name says otherwise.

ControlRequestMetadata ​

FieldTypeDescription
operation_idintOperation affected by CANCEL or ABORT.
control_sequenceintMonotonic control sequence in the operation scope.
reason_codeintApplication reason code.
source_roleRuntimeRoleSender role.
flagsintBit flags.
diagnostic_bytesintDeclared diagnostic tail length.

SchedulingMetadata ​

FieldTypeDescription
operation_idintOperation to reschedule.
control_sequenceintMonotonic control sequence.
priority_classintAbsolute priority class.
priority_deltaintSigned priority delta.
deadline_unix_msintUnix deadline in milliseconds.
flagsintBit flags.

Control Metadata Field Map ​

TypeMessage typesFrozen fields
SupersedeMetadataSUPERSEDEold_operation_id, new_operation_id, control_sequence, drop_reason_code, flags, diagnostic_bytes
BudgetMetadataBUDGET_UPDATEoperation_id, compute_budget_units, memory_budget_bytes, bandwidth_budget_bytes, token_budget, flags
ProgressMetadataPROGRESSoperation_id, progress_sequence, stage_code, percent_x100, object_id, body_bytes
PartialResultMetadataPARTIAL_RESULToperation_id, result_sequence, object_id, delta_sequence, body_bytes, flags
PressureMetadataBACKPRESSURE, CREDIT_UPDATEscope_id, credit_window, pressure_level, pressure_reason, retry_after_ms, flags
CapabilityMetadataCAPABILITY_NEGOTIATION, DEGRADE_PROFILEprofile_id, capability_count, cost_model_id, preference_rank, limit_bytes, limit_units, body_bytes, flags
RouteHintMetadataROUTE_HINT, EXECUTION_HINToperation_id, route_id, executor_class, affinity_class, deadline_unix_ms, body_bytes, flags
TraceContextMetadataTRACE_CONTEXTtrace_id, span_id, parent_span_id, stage_code, flags, body_bytes
ResultDropReasonMetadataRESULT_DROP_REASONoperation_id, result_sequence, drop_reason_code, source_role, flags, diagnostic_bytes
RecoverableErrorMetadataERROR_RECOVERABLEerror_code, error_scope, recovery_action, source_role, flags, retry_after_ms, related_session_id, related_frame_id, related_view_id, diagnostic_bytes
RetryAfterMetadataRETRY_AFTERscope_id, control_sequence, retry_after_ms, jitter_ms, reason_code, source_role, flags, diagnostic_bytes

Runtime Object Metadata ​

TypeMessage typesFrozen fields
ObjectDescriptorMetadataOBJECT_DECLAREobject_id, object_kind, producer_role, consumer_role, session_id, byte_size, compute_cost_units, memory_location_hint, ownership_hint, lifetime_hint_ms, metadata_bytes
ObjectReferenceMetadataOBJECT_REFobject_id, operation_id, object_version, offset, length, flags, metadata_bytes
ObjectReleaseMetadataOBJECT_RELEASEobject_id, operation_id, release_reason, source_role, flags, diagnostic_bytes
ObjectDeltaMetadataOBJECT_PATCH, OBJECT_DELTAobject_id, delta_sequence, region_offset, region_bytes, delta_bytes, flags, metadata_bytes
CacheReferenceMetadataCACHE_REFERENCEcache_namespace, cache_key_hi, cache_key_lo, profile_id, reuse_scope, lease_id, producer_trace_id, expiration_hint_ms, metadata_bytes, flags
CacheMissMetadataCACHE_MISScache_namespace, cache_key_hi, cache_key_lo, miss_reason, profile_id, diagnostic_bytes

Local Cache Lease State ​

CacheLeaseDescriptor is the validated Python value for a granted lease. It is not a wire payload or a native handle.

Python fieldTypeProtocol field
identityCacheObjectIdentityobject_id
object_versionint (u64)object_version
lease_idint (u64)lease_id
owner_scopeCacheLeaseOwnerScopeowner_scope
owner_idint (u64)owner_id
granted_at_msint (u64)granted_at_ms
ttl_msint (u32)ttl_ms

CacheObjectIdentity contains cache_namespace: int (u32), cache_key_hi: int (u64), cache_key_lo: int (u64), and object_kind: CacheObjectKind (u32). CacheLeaseOwnerScope is CONNECTION = 0, SESSION = 1, or OPERATION = 2. Use expires_at_ms, is_expired, and validate_version for local validation.

CachePolicyOptions ​

CachePolicyOptions is a local opt-in value and never performs an implicit lookup or emits a frame.

Python fieldTypeDefault
enabledboolFalse
reuse_scope`CacheReuseScopeNone`
expiration_hint_msint (u64)0
invalidation_reasonCacheInvalidationReasonEXPLICIT

CacheInvalidationReason has EXPLICIT, DEPENDENCY_INVALIDATED, LEASE_EXPIRED, VERSION_MISMATCH, and SCHEMA_MISMATCH. Enabling requires reuse_scope; disabling requires reuse_scope is None and expiration_hint_ms == 0.

Runtime Enums ​

RuntimeObjectKind ​

UNSPECIFIED, TENSOR, TOKEN_BLOCK, IMAGE_TILE, FEATURE_MAP, TOOL_RESULT, TRACE_SEGMENT, OPAQUE_BYTES, DOCUMENT_CHUNK, AUDIO_CHUNK, VIDEO_CHUNK, ROUTE_PLAN, CACHE_MANIFEST.

RuntimeRole ​

UNSPECIFIED, CLIENT, SERVER, RUNTIME, SUBAGENT, TOOL, SCHEDULER, CONFORMANCE_RUNNER.

Object and cache enums ​

EnumMembers
MemoryLocationHintUNSPECIFIED, HOST_MEMORY, DEVICE_MEMORY, SHARED_MEMORY, REMOTE_MEMORY, MMAP_FILE, OBJECT_STORE
OwnershipHintUNSPECIFIED, PRODUCER_OWNED, CONSUMER_OWNED, SESSION_OWNED, BORROWED, TRANSFER_ON_REF, RELEASE_ON_DROP
ObjectReleaseReasonCOMPLETED, CANCELLED, EXPIRED, REPLACED, INVALIDATED, OWNER_CLOSED, LEASE_EXPIRED, CONFORMANCE_INJECTION
CacheReuseScopeOPERATION, SESSION, CONNECTION, GLOBAL, TENANT, PROFILE
CacheMissReasonUNKNOWN, NOT_FOUND, EXPIRED, INVALIDATED, SCHEMA_MISMATCH, PRODUCER_UNAVAILABLE, LEASE_REQUIRED, PERMISSION_DENIED

RuntimeFrameHeader ​

RuntimeFrameHeader is the lossless caller-supplied projection of the NNRP common header used by the binary-frame helpers. It contains every wire field that is neither a protocol constant nor derived from the supplied metadata and body buffers. It does not contain native handle or session generation state.

FieldTypeDescription
message_typeMessageTypeFrame message type.
flagsHeaderFlagsHeader flags.
session_idintSession id.
frame_idintFrame id.
view_idintLogical lane or view id.
route_idintRoute or scheduling id.
trace_idintEnd-to-end trace id.
version_majorintProtocol major; defaults to the current NNRP/1 value.
wire_formatWireFormatWire format; defaults to CURRENT.

The codec writes the fixed NNRP magic and 40-byte header length, and derives meta_len and body_len from the supplied buffers. Decode returns the same nine caller-controlled fields.

NNRP Documentation