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. NativeRuntimeConnection.poll_runtime_frames() and iter_runtime_frames() skip non-runtime events and return the decoded type directly.

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_key_hi, cache_key_lo, profile_id, reuse_scope, lease_id, producer_trace_id, expiration_hint_ms, metadata_bytes, flags
CacheMissMetadataCACHE_MISScache_key_hi, cache_key_lo, miss_reason, profile_id, diagnostic_bytes

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

FieldTypeDescription
message_typeMessageTypeFrame message type.
flagsHeaderFlagsHeader flags.
session_idintSession id.
generationintSession generation.
frame_idintFrame id.

NNRP Documentation