@file:Suppress("unused") package dev.shibasis.reaktor.proposal.data // DESIGN SPECIMEN, not shipped API. Compileable without Reaktor dependencies. // Ref/Revision stand in for canonical semantic-kernel IDs, never a second registry. // Collections supplied to definitions must be immutable snapshots in production. @JvmInline value class Ref(val value: String) @JvmInline value class Revision(val value: String) @JvmInline value class InvocationId(val value: String) @JvmInline value class IdempotencyKey(val value: String) @JvmInline value class Cursor(val opaque: String) data class Schema(val id: Ref, val revision: Revision) data class Version(val domain: Ref, val epoch: String, val position: String) data class EntityRef(val dataset: Ref, val tenant: Ref, val key: String) data class AuthorityRef(val issuer: Ref, val grant: Ref) data class TraceContext(val traceId: String, val parentSpanId: String?) data class CallContext( val invocation: InvocationId, val authority: AuthorityRef, val tenant: Ref, val remainingBudgetMillis: Long, val idempotency: IdempotencyKey? = null, val causation: InvocationId? = null, val trace: TraceContext? = null, val consistency: ReadConsistency = ReadConsistency.Eventual, ) // AuthorityRef is resolved and validated by the receiving runtime, never trusted on sight. // remainingBudgetMillis is decremented at hops; it is not a cross-host monotonic timestamp. sealed interface ReadConsistency { data object Eventual : ReadConsistency data class AtLeast(val versions: Set) : ReadConsistency data class BoundedStaleness(val maxMillis: Long) : ReadConsistency data class Snapshot(val token: Ref) : ReadConsistency data class Linearizable(val domain: Ref) : ReadConsistency } enum class EffectKind { READ, WRITE, PUBLISH, EXTERNAL, COMPUTE } data class Effect(val kind: EffectKind, val resource: Ref) data class Requirement(val profile: Ref, val revision: Revision) data class Operation( val id: Ref, val revision: Revision, val input: Schema, val output: Schema, val error: Schema, val effects: Set, val requirements: Set, ) enum class EffectStatus { NOT_STARTED, APPLIED, PARTIALLY_APPLIED, UNKNOWN } enum class FailureKind { UNAVAILABLE, DEADLINE, CANCELLED, UNAUTHORIZED, UNSUPPORTED, CONFLICT, RESOURCE_EXHAUSTED, INVALID_CONTRACT, INTERNAL } data class Failure( val kind: FailureKind, val effect: EffectStatus, val diagnostic: Ref?, val retryAfterMillis: Long? = null, ) // Success means the operation's declared postcondition, not every downstream projection. sealed interface Outcome { data class Ok(val value: O) : Outcome data class Rejected(val error: E, val effect: EffectStatus) : Outcome data class Failed(val failure: Failure) : Outcome } // Six composable callable forms. Stream upload is an explicit additional convenience. fun interface SyncCall { fun call(input: I, context: CallContext): Outcome } fun interface AsyncCall { suspend fun call(input: I, context: CallContext): Outcome } sealed interface Received { data class Items(val values: List, val resume: Cursor?) : Received data object End : Received } interface Receiver { // Pull grants at most maxItems. Byte/window limits belong to the stream profile too. suspend fun receive(maxItems: Int): Outcome, E> suspend fun close(): Outcome } interface Sender { // Suspends for capacity. Acceptance here is not a durable application commit. suspend fun send(value: T): Outcome suspend fun finish(): Outcome // half-close; receiver may remain open } interface DuplexSession : Sender, Receiver fun interface StreamCall { suspend fun open(input: I, resume: Cursor?, context: CallContext): Outcome, E> } fun interface DuplexCall { suspend fun open(input: Open, context: CallContext): Outcome, E> } interface UploadSession : Sender { suspend fun result(): Outcome // available after finish; cancellation can be uncertain suspend fun cancel(): Outcome } fun interface UploadCall { suspend fun open(input: Open, context: CallContext): Outcome, E> } data class Event( val id: Ref, val schema: Schema, val payload: T, val causation: InvocationId?, val orderingKey: String?, val source: Version?, ) enum class PublicationStage { VOLATILE_ACCEPTED, DURABLY_ACCEPTED } data class PublicationReceipt(val event: Ref, val stage: PublicationStage, val logPosition: Version?) fun interface Publisher { suspend fun publish(event: Event, context: CallContext): Outcome } data class Delivery(val event: Event, val token: Ref, val attempt: Int) sealed interface Disposition { data object Acknowledge : Disposition data class Retry(val delayMillis: Long) : Disposition data class DeadLetter(val reason: Ref) : Disposition } interface Subscription : Receiver, E> { suspend fun settle(token: Ref, disposition: Disposition): Outcome } fun interface Subscriber { suspend fun open(subscription: Ref, resume: Cursor?, context: CallContext): Outcome, E> } enum class JobPhase { ACCEPTED, RUNNING, WAITING, SUCCEEDED, FAILED, CANCELLED } data class JobUpdate( val job: Ref, val phase: JobPhase, val cursor: Cursor, val outcome: Outcome?, ) enum class CancelDisposition { REQUESTED, ALREADY_TERMINAL, UNSUPPORTED } interface Job { val id: Ref suspend fun inspect(context: CallContext): Outcome, E> suspend fun watch(resume: Cursor?, context: CallContext): Outcome, E>, E> suspend fun cancel(context: CallContext): Outcome } interface Jobs { suspend fun open(id: Ref, context: CallContext): Outcome, E> } fun interface DurableCall { suspend fun submit(input: I, context: CallContext): Outcome, E> } // Callable C is retained by the descriptor and resolution result; callers cannot pair // a string endpoint with arbitrary request/response types. Generated factories build // these descriptors from typed schemas and interaction protocols. interface Contract { val id: Ref val revision: Revision val protocol: Ref val requirements: Set } data class UnarySync(val operation: Operation) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/unary-sync/v1") override val requirements get() = operation.requirements } data class UnaryAsync(val operation: Operation) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/unary-async/v1") override val requirements get() = operation.requirements } enum class StreamOrdering { ORDERED, UNORDERED } enum class OverflowPolicy { BACKPRESSURE, DROP_OLDEST, DROP_NEWEST, FAIL } data class StreamProfile( val ordering: StreamOrdering, val maxFrameBytes: Int, val maxBufferedBytes: Long, val overflow: OverflowPolicy, val replayProfile: Requirement?, // null means no supported resumption ) data class ServerStream( val operation: Operation, val stream: StreamProfile, ) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/server-stream/v1") override val requirements get() = operation.requirements } data class Duplex( val operation: Operation, val incoming: Schema, val inputStream: StreamProfile, val outputStream: StreamProfile, ) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/duplex/v1") override val requirements get() = operation.requirements } data class Upload( val operation: Operation, val incoming: Schema, val stream: StreamProfile, ) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/upload/v1") override val requirements get() = operation.requirements } data class Publication( val operation: Operation, val delivery: Requirement, ) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/publication/v1") override val requirements get() = operation.requirements + delivery } data class SubscriptionContract( override val id: Ref, override val revision: Revision, val event: Schema, val error: Schema, val delivery: Requirement, val additionalRequirements: Set, ) : Contract> { override val requirements get() = additionalRequirements + delivery override val protocol = Ref("reaktor.protocol/subscription/v1") } data class DurableOperation( val operation: Operation, val execution: Requirement, ) : Contract> { override val id get() = operation.id override val revision get() = operation.revision override val protocol = Ref("reaktor.protocol/durable-job/v1") override val requirements get() = operation.requirements + execution } // These are semantic protocol descriptors. Implementations must validate profile limits, // schema compatibility and target behavior; a protocol name is not evidence of support. data class BindingChoice(val provider: Ref, val deployment: Ref, val revision: Revision) data class Qualification(val contract: Ref, val binding: BindingChoice, val evidence: Ref) sealed interface BindingResult { data class Bound(val call: C, val qualification: Qualification) : BindingResult data class Unsupported(val missing: Set, val reason: Ref) : BindingResult } interface Binder { fun bind(contract: Contract, choice: BindingChoice): BindingResult } interface SyncInterceptor { fun intercept(input: I, context: CallContext, next: SyncCall): Outcome } interface AsyncInterceptor { suspend fun intercept(input: I, context: CallContext, next: AsyncCall): Outcome } // FFI binding descriptors own ABI/ownership; none is imposed on HTTP or local Kotlin. enum class ExecutionPolicy { DIRECT, SERIAL_ACTOR, CONCURRENT, THREAD_AFFINE } enum class BufferLifetime { BORROWED_UNTIL_RETURN, OWNED, RETAINED_UNTIL_COMPLETION } data class NativeHandle(val runtime: Ref, val generation: Long, val objectId: Long) data class FfiBinding( val operation: Ref, val runtime: Ref, val abi: Revision, val execution: ExecutionPolicy, val lifetime: BufferLifetime, val requiredThread: Ref?, val reentrant: Boolean, ) // Data families are an open vocabulary in the graph; these are baseline profile names. enum class StoreFamily { RELATIONAL, KEY_VALUE, GRAPH, COLUMNAR, TIME_SERIES, OBJECT_FILE, EVENT_LOG } data class StoreBinding( val logicalResource: Ref, val physicalLocator: String, val provider: Ref, val family: StoreFamily, val profiles: Set, val qualification: Ref, ) data class Dataset(val id: Ref, val schema: Schema) interface Predicate // typed serializable expression IR; native predicates need provider requirements interface Projection interface Ordering // must specify stable tie-break, null semantics and collation interface Patch // typed field operations; SQL JSON and Mongo translations are qualified interface Query { val id: Ref val result: Schema val requirements: Set } data class Page(val values: List, val next: Cursor?, val observed: Set) interface QueryReader { suspend fun read(query: Query, cursor: Cursor?, limit: Int, context: CallContext): Outcome, E> } interface DocumentDb { val dataset: Dataset suspend fun get(id: EntityRef, context: CallContext): Outcome suspend fun find(where: Predicate, order: Ordering, cursor: Cursor?, limit: Int, context: CallContext): Outcome, E> suspend fun put(id: EntityRef, value: T, expected: Version?, context: CallContext): Outcome suspend fun patch(id: EntityRef, change: Patch, expected: Version, context: CallContext): Outcome suspend fun remove(id: EntityRef, expected: Version, context: CallContext): Outcome } // expected=null is explicitly unconditional, not implicit create-if-absent. // DocumentDb is a relational/JSON facet by default; it is not Mongo's API in disguise. enum class Distance { L2, COSINE, INNER_PRODUCT } enum class SearchAccuracy { EXACT, APPROXIMATE } data class EmbeddingSpace(val model: Ref, val revision: Revision, val dimensions: Int, val distance: Distance) data class VectorRequest( val space: EmbeddingSpace, val vector: List, val filter: Predicate?, val limit: Int, val accuracy: SearchAccuracy, ) data class VectorHit(val entity: EntityRef, val distance: Double) interface VectorIndex { suspend fun nearest(request: VectorRequest, context: CallContext): Outcome>, E> } // Distances use the requested metric's documented orientation; inner product distance // is negative dot product. Scores from unrelated models/spaces are not interchangeable. sealed interface TextExpression { data class Terms(val text: String) : TextExpression data class Phrase(val text: String) : TextExpression data class All(val clauses: List) : TextExpression data class AnyOf(val clauses: List) : TextExpression } data class TextRequest( val expression: TextExpression, val fields: Set, val filter: Predicate?, val analyzer: Ref, val ranking: Ref, val limit: Int, ) data class TextHit(val entity: EntityRef, val score: Double) interface SearchIndex { suspend fun search(request: TextRequest, cursor: Cursor?, context: CallContext): Outcome>, E> } // Index writes normally come from authoritative data via a declared derivation. // Explicit index management/bulk load and provider-native features are separate facets. interface CapabilityKey { val profile: Requirement } interface Capabilities { fun resolve(key: CapabilityKey): BindingResult } // Plans are pure descriptions; executors are the effect boundary. Hashes include graph // revision, schemas, bindings, authority scope and relevant observed preconditions. data class PlanRef(val id: Ref, val fingerprint: String) data class PlanFinding(val subject: Ref, val reason: Ref, val blocksExecution: Boolean) data class PlanResult(val plan: PlanRef?, val findings: List) interface DataPlanner { fun plan(query: Query<*>, bindings: List): PlanResult }