Skip to content

Runtime Control & Objects

C# Preview 4 APIs freeze the runtime-control and object-reference surface that managed packages must implement for NNRP/1. The managed names below intentionally match the Rust metadata semantics while using C# casing and ReadOnlySpan<byte> for hot paths.

Import

csharp
using Nnrp.Core;
using Nnrp.Runtime;

NnrpRuntimeControl.Encode

Encodes one Preview 4 control metadata object.

ParameterTypeRequiredDescription
messageTypeMessageTypeYesOne of the Preview 4 control message types.
metadataRuntime control metadataYesMetadata object matching messageType.
tailReadOnlySpan<byte>NoExtension bytes, diagnostic bytes, progress body, or partial-result body declared by metadata.
Returns
byte[]
csharp
var payload = NnrpRuntimeControl.Encode(
    MessageType.Progress,
    new ProgressMetadata(
        OperationId: 42,
        ProgressSequence: 1,
        StageCode: 2,
        PercentX100: 2500,
        ObjectId: 0,
        BodyBytes: 0));

NnrpRuntimeControl.Decode

Decodes one control metadata payload.

ParameterTypeRequiredDescription
messageTypeMessageTypeYesMessage type that selected the metadata layout.
payloadReadOnlySpan<byte>YesMetadata bytes plus the declared tail.
Returns
DecodedRuntimeControlMetadata

NnrpRuntimeObject.Encode

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

ParameterTypeRequiredDescription
messageTypeMessageTypeYesOne of ObjectDeclare, ObjectRef, ObjectRelease, ObjectPatch, ObjectDelta, CacheReference, or CacheMiss.
metadataRuntime object metadataYesMetadata object matching messageType.
tailReadOnlySpan<byte>NoExtension bytes, diagnostics, or delta payload declared by metadata.
Returns
byte[]

NnrpRuntimeObject.Decode

Decodes one runtime object or cache metadata payload.

ParameterTypeRequiredDescription
messageTypeMessageTypeYesMessage type that selected the metadata layout.
payloadReadOnlySpan<byte>YesMetadata bytes plus the declared tail.
Returns
DecodedRuntimeObjectMetadata

NnrpWebSocketFrameCodec.Encode

NnrpWebSocketFrameCodec is exported by Nnrp.Transport.WebSocket. It builds the binary runtime frame carried by one WebSocket binary message; text messages are never accepted as NNRP data.

ParameterTypeRequiredDescription
headerRuntimeFrameHeaderYesHeader fields except metadata/body lengths; the helper derives both lengths from the buffers.
metadataReadOnlySpan<byte>NoMetadata payload.
bodyReadOnlySpan<byte>NoBody payload.
Returns
byte[]

NnrpWebSocketFrameCodec.Decode

Decodes one WebSocket binary frame into its header plus decoder-owned metadata and body copies.

ParameterTypeRequiredDescription
frameReadOnlySpan<byte>YesOne complete WebSocket binary message.
Returns
DecodedRuntimeFrame

NnrpWebSocketFrameCodec.DecodeBatch

Decodes concatenated binary frames from local buffers and conformance fixtures.

ParameterTypeRequiredDescription
batchReadOnlySpan<byte>YesConcatenated frames.
limitintNoMaximum decoded frames; 0 means no limit.
Returns
IReadOnlyList<DecodedRuntimeFrame>

DecodedRuntimeFrame exposes the following immutable projection:

PropertyTypeDescription
HeaderRuntimeFrameHeaderCaller-controlled common-header fields.
MetadataReadOnlyMemory<byte>Decoder-owned metadata copy.
BodyReadOnlyMemory<byte>Decoder-owned body copy.

The decoder owns the backing storage for both byte regions; neither property borrows the input buffer. Decode rejects truncated headers, metadata/body length mismatches, reserved header values, trailing bytes in a single-frame decode, and a decoded frame count above limit.

NnrpRuntimeEvent

NnrpRuntimeEvent is the immutable wire-event value carried by client and server event unions. Its public properties are exactly Header, Metadata, and Tail. The message type is available only as Header.MessageType; the event does not duplicate common-header or tail fields.

NnrpRuntimeEventMetadata is a closed Kind plus typed Get<T>() union. NnrpRuntimeEventTail is a closed Kind plus Match<TResult>(...) union with the following variants. Match requires one callback for every row, so application code cannot silently ignore a new tail shape.

NnrpRuntimeEventTailKindActive values passed to Match
NoneNo value
BodyOne owned ReadOnlyMemory<byte> body
DiagnosticOne owned ReadOnlyMemory<byte> diagnostic
MetadataBodyAndDeltaIndependent owned metadataBody and delta values

The event and tail types do not expose flattened Body, Diagnostic, CapabilityEntries, HintBody, TraceAttributes, ObjectMetadata, Delta, or CacheMetadata properties. Callers first discriminate Metadata.Kind and Tail.Kind, then use Metadata.Get<T>() and Tail.Match.

The event never exposes a raw native buffer. Native-owned data is copied or retained behind an explicit lifetime guard before the event reaches application code.

NnrpPreview4CapabilityTokens

NnrpPreview4CapabilityTokens is the C# projection of the frozen Preview4 capability and transport catalog. The constants retain the exact protocol strings; SDK code must not derive or rename them.

Constant groupC# constantsFrozen values
ControlControlCancelAbort, ControlSupersede, ControlPriorityUpdate, ControlDeadlineExpire, ControlProgressPartial, ControlCreditBackpressure, ControlCapabilityCosts, ControlRouteExecutionHint, ControlTraceContext, ControlResultDropReason, ControlDegradeProfile, ControlBudgetUpdate, ControlRecoverableErrorcontrol.cancel_abort, control.supersede, control.priority_update, control.deadline_expire, control.progress_partial, control.credit_backpressure, control.capability_costs, control.route_execution_hint, control.trace_context, control.result_drop_reason, control.degrade_profile, control.budget_update, control.recoverable_error
Runtime object and cacheObjectLifecycle, ObjectDelta, ObjectCost, ObjectOwnership, CacheReferenceobject.lifecycle, object.delta, object.cost, object.ownership, cache.reference
TransportTransportTcp, TransportQuic, TransportIpc, TransportWebSockettcp, quic, ipc, websocket

The class also exposes read-only Control, RuntimeObjectAndCache, Transports, and AllCapabilities collections. AllCapabilities excludes transport names because transport availability is reported separately from protocol capability claims.

Runtime Control Metadata

TypeMessage typesFrozen properties
ControlRequestMetadataCancel, AbortOperationId, ControlSequence, ReasonCode, SourceRole, Flags, DiagnosticBytes
SchedulingMetadataPriorityUpdate, Deadline, ExpireAtOperationId, ControlSequence, PriorityClass, PriorityDelta, DeadlineUnixMs, Flags
SupersedeMetadataSupersedeOldOperationId, NewOperationId, ControlSequence, DropReasonCode, Flags, DiagnosticBytes
BudgetMetadataBudgetUpdateOperationId, ComputeBudgetUnits, MemoryBudgetBytes, BandwidthBudgetBytes, TokenBudget, Flags
ProgressMetadataProgressOperationId, ProgressSequence, StageCode, PercentX100, ObjectId, BodyBytes
PartialResultMetadataPartialResultOperationId, ResultSequence, ObjectId, DeltaSequence, BodyBytes, Flags
PressureMetadataBackpressure, CreditUpdateScopeId, CreditWindow, PressureLevel, PressureReason, RetryAfterMs, Flags
CapabilityMetadataCapabilityNegotiation, DegradeProfileProfileId, CapabilityCount, CostModelId, PreferenceRank, LimitBytes, LimitUnits, BodyBytes, Flags
RouteHintMetadataRouteHint, ExecutionHintOperationId, RouteId, ExecutorClass, AffinityClass, DeadlineUnixMs, BodyBytes, Flags
TraceContextMetadataTraceContextTraceId, SpanId, ParentSpanId, StageCode, Flags, BodyBytes
ResultDropReasonMetadataResultDropReasonOperationId, ResultSequence, DropReasonCode, SourceRole, Flags, DiagnosticBytes
RecoverableErrorMetadataErrorRecoverableErrorCode, ErrorScope, RecoveryAction, SourceRole, Flags, RetryAfterMs, RelatedSessionId, RelatedFrameId, RelatedViewId, DiagnosticBytes
RetryAfterMetadataRetryAfterScopeId, ControlSequence, RetryAfterMs, JitterMs, ReasonCode, SourceRole, Flags, DiagnosticBytes

SupersedeMetadata.DropReasonCode and ResultDropReasonMetadata.DropReasonCode are NnrpResultDropReasonCode, not raw ushort values. Encoders and decoders reject the reserved 0x000a..0x7fff range; 0x8000..0xffff remains available for private extensions as frozen by the runtime-control value registry.

Runtime Object Metadata

TypeMessage typesFrozen properties
ObjectDescriptorMetadataObjectDeclareObjectId, ObjectKind, ProducerRole, ConsumerRole, SessionId, ByteSize, ComputeCostUnits, MemoryLocationHint, OwnershipHint, LifetimeHintMs, MetadataBytes
ObjectReferenceMetadataObjectRefObjectId, OperationId, ObjectVersion, Offset, Length, Flags, MetadataBytes
ObjectReleaseMetadataObjectReleaseObjectId, OperationId, ReleaseReason, SourceRole, Flags, DiagnosticBytes
ObjectDeltaMetadataObjectPatch, ObjectDeltaObjectId, DeltaSequence, RegionOffset, RegionBytes, DeltaBytes, Flags, MetadataBytes
CacheReferenceMetadataCacheReferenceCacheNamespace, CacheKeyHi, CacheKeyLo, ProfileId, ReuseScope, LeaseId, ProducerTraceId, ExpirationHintMs, MetadataBytes, Flags
CacheMissMetadataCacheMissCacheNamespace, CacheKeyHi, CacheKeyLo, MissReason, ProfileId, DiagnosticBytes

CacheNamespace is uint; both cache-key words are ulong. CachePutMetadata, CacheAckMetadata, CacheInvalidateMetadata, and ObjectReferenceBlock use the same widths and names.

Local Cache Lease State

NnrpCacheLease is the validated C# value for a granted lease. It is not a wire payload or a native handle.

C# propertyTypeProtocol field
ObjectIdNnrpCacheObjectIdobject_id
ObjectVersionulongobject_version
LeaseIdulonglease_id
OwnerScopeCacheLeaseOwnerScopeowner_scope
OwnerIdulongowner_id
GrantedAtMillisecondsulonggranted_at_ms
TtlMillisecondsuintttl_ms

NnrpCacheObjectId contains CacheNamespace: uint, CacheKeyHigh: ulong, CacheKeyLow: ulong, and ObjectKind: CacheObjectKind. CacheLeaseOwnerScope is Connection = 0, Session = 1, or Operation = 2. Use ExpiresAtMilliseconds, TryValidateLiveAt, and TryValidateVersion for local validation.

NnrpCacheObjectVersion binds that complete object identity to ObjectVersion, SchemaId, and SchemaVersion. NnrpCacheLeaseResult is the closed result returned by native cache operations:

C# propertyType
ObjectIdNnrpCacheObjectId
OutcomeNnrpCacheLeaseOutcome
LeaseNnrpCacheLease?
ObjectVersionNnrpCacheObjectVersion?
Diagnosticstring?

NnrpCacheLeaseOutcome is Valid, Expired, Renewed, Released, or Missing. A present lease or object-version value must carry the same complete object identity as ObjectId.

CachePolicyOptions

CachePolicyOptions is the local opt-in policy described by the runtime-control profile. It never performs a lookup or serializes a cache frame by itself.

C# propertyTypeDefault
Enabledboolfalse
ReuseScopeCacheReuseScope?null
ExpirationHintMillisecondsulong0
InvalidationReasonCachePolicyInvalidationReasonExplicit

CachePolicyInvalidationReason has Explicit, DependencyInvalidated, LeaseExpired, VersionMismatch, and SchemaMismatch. Enabling the policy requires ReuseScope; disabling it requires ReuseScope == null and ExpirationHintMilliseconds == 0.

Connection And Session Lifecycle

NnrpConnectionLifecycle is the application-facing Preview4 lifecycle model. It starts in Open, keeps sessions ordered by session id, and exposes immutable session snapshots through Sessions, TryGetSession, and Snapshot. TryCloseConnection moves the connection and every installed session to Closed.

NnrpSessionLifecycle exposes the frozen session snapshot fields: SessionId, State, ProfileId, PriorityClass, SchemaId, SchemaVersion, MaxInFlightOperations, RouteScopeId, LastOperationId, and SessionErrorCode. AcceptsSessionScopedMessages and AcceptsNewOperations are derived C# convenience properties, not additional wire fields.

The transition methods are TryApplySessionOpenAck, TryBeginSessionClose, TryApplySessionCloseAck, and TryValidateFlowUpdate. A rejected close acknowledgement restores the session's established Open or Resumed state. Unknown close-status values return an NnrpProtocolFailure; Try methods do not translate malformed peer state into language exceptions.

Schema Registry

NnrpSchemaDescriptorHeader is the 32-byte C# projection of the frozen schema descriptor header. It exposes schema identity and version, profile assignment, flags, supported protocol-version range, body size, dependency count, default stream semantics, and schema hash. Use TryParse and TryWrite for validated wire access.

NnrpSchemaRegistry stores descriptors by (SchemaId, SchemaVersion). WithStandardProfiles installs the standard bindings; TryInstall, TryGet, TryInvalidate, and TryValidateDescriptorBinding implement the public registry lifecycle. SchemaRegistryAction reports whether an install was new, already present, updated, or invalidated, while SchemaErrorCode carries protocol-defined failures.

Native Object Delta Metadata Copies

NnrpNativeRuntimeObjects owns the Rust-backed metadata buffer helpers. Both methods return an NnrpNativeObjectMetadataBuffer that must be disposed.

MethodParametersPayload layout
AcquireObjectPatchMetadataCopyObjectDeltaMetadata metadata, byte[] metadataTail, byte[] deltaObjectPatch metadata + metadata tail + delta bytes
AcquireObjectDeltaMetadataCopyObjectDeltaMetadata metadata, byte[] metadataTail, byte[] deltaObjectDelta metadata + metadata tail + delta bytes

Both helpers validate MetadataBytes and DeltaBytes before acquiring the native-owned copy.

Runtime Enums

EnumMembers
NnrpOperationState : byteAccepted = 0, Running = 1, Partial = 2, WaitingTool = 3, Superseded = 4, Cancelled = 5, Failed = 6, Completed = 7
NnrpResultTerminalState : byteSuccess = 0, Cancelled = 1, Dropped = 2, Error = 3
NnrpResultDropReasonCode : ushortNone = 0, DeadlineExpired = 1, Superseded = 2, PeerCancelled = 3, Backpressure = 4, CapabilityMismatch = 5, BudgetExceeded = 6, ObjectInvalidated = 7, TransportClosed = 8, ConformanceInjection = 9
RuntimeObjectKindUnspecified, Tensor, TokenBlock, ImageTile, FeatureMap, ToolResult, TraceSegment, OpaqueBytes, DocumentChunk, AudioChunk, VideoChunk, RoutePlan, CacheManifest
RuntimeRoleUnspecified, Client, Server, Runtime, Subagent, Tool, Scheduler, ConformanceRunner
MemoryLocationHintUnspecified, HostMemory, DeviceMemory, SharedMemory, RemoteMemory, MmapFile, ObjectStore
OwnershipHintUnspecified, ProducerOwned, ConsumerOwned, SessionOwned, Borrowed, TransferOnRef, ReleaseOnDrop
ObjectReleaseReasonCompleted, Cancelled, Expired, Replaced, Invalidated, OwnerClosed, LeaseExpired, ConformanceInjection
CacheReuseScopeOperation, Session, Connection, Global, Tenant, Profile
CacheMissReasonUnknown, NotFound, Expired, Invalidated, SchemaMismatch, ProducerUnavailable, LeaseRequired, PermissionDenied

NnrpOperationState and NnrpResultTerminalState mirror the canonical Rust OperationState and ResultTerminalState registries exactly. The terminal mapping is Completed -> Success, Cancelled -> Cancelled, Superseded -> Dropped, and Failed -> Error; non-terminal operation states have no terminal result state.

RuntimeFrameHeader

RuntimeFrameHeader is exported by Nnrp.Core as an immutable record struct. It is the role-neutral header projection used by runtime events and NnrpWebSocketFrameCodec; it is not a second protocol header version. It contains every caller-controlled common-header field and does not contain native handle or session generation state.

PropertyTypeDescription
MessageTypeMessageTypeFrame message type.
FlagsHeaderFlagsHeader flags.
SessionIduintSession id.
FrameIduintFrame id.
ViewIdushortLogical lane or view id.
RouteIdushortRoute or scheduling id.
TraceIdulongEnd-to-end trace id.
VersionMajorbyteProtocol major; defaults to NnrpHeader.CurrentVersionMajor.
WireFormatbyteWire format; defaults to NnrpHeader.CurrentWireFormat.

MessageType is required; the remaining values use their protocol zero/current defaults. The codec writes the fixed magic and 40-byte header length, and derives metadata and body lengths from the provided buffers. Decode returns the same nine caller-controlled fields.

NNRP Documentation