import type { EventBus } from '@okikio/observables'; import type { StandardSchemaV1 } from '@standard-schema/spec'; import type { Context, Snapshot } from '@okikio/context'; import type { Encoded as EncodedFailure } from '@okikio/failure'; import type { Process } from '../types.ts'; /** Optional child-to-parent request/result contract used inside one active process request. */ export interface ProcessCallProtocol { /** Schema that validates child-to-parent reverse-call requests. */ readonly request: StandardSchemaV1; /** Response schema or payload associated with this process call. */ readonly response: StandardSchemaV1; } /** Schemas that define one framed child-process request protocol. */ export interface ProcessChannelProtocol { /** Schema that validates parent-to-child request payloads. */ readonly request: StandardSchemaV1; /** Response schema or payload associated with this process channel. */ readonly response: StandardSchemaV1; /** Optional schema for encoded declared failures returned by child requests. */ readonly failure?: StandardSchemaV1; /** Optional observation contract or callback for this process channel. */ readonly notice?: StandardSchemaV1; /** Make one correlated reverse request while this process channel remains active. */ readonly call?: ProcessCallProtocol; } /** Input accepted by `channel.protocol()`. */ export interface ProcessProtocolOptions extends ProcessChannelProtocol {} /** Correlation options for one process-channel request. */ export interface ProcessRequestOptions { /** Optional caller-supplied correlation ID for one process request. */ readonly id?: string; } /** Parent-side reverse-call implementation for one active process request. */ export type ProcessCallRun = ( request: CallRequest, ctx: Context, requestId: string, callId: string, ) => CallResponse | Promise; /** Parent-side observer for optional non-authoritative process notices. */ export type ProcessNoticeSink = (notice: Notice, ctx: Context, requestId: string) => void | Promise; /** Process channel lifecycle and correlated request observations. */ export type ProcessEventType = | Readonly<{ readonly type: 'opened'; readonly pid: number }> | Readonly<{ readonly type: 'request'; readonly id: string }> | Readonly<{ readonly type: 'notice'; readonly id: string }> | Readonly<{ readonly type: 'call'; readonly id: string; readonly callId: string }> | Readonly<{ readonly type: 'paused'; readonly id: string }> | Readonly<{ readonly type: 'resumed'; readonly id: string }> | Readonly<{ readonly type: 'result'; readonly id: string }> | Readonly<{ readonly type: 'failure'; readonly id: string; readonly failureId: string }> | Readonly<{ readonly type: 'fault'; readonly id?: string; readonly reason: unknown }> | Readonly<{ readonly type: 'cancelled'; readonly id: string }> | Readonly<{ readonly type: 'closing'; readonly reason?: unknown }> | Readonly<{ readonly type: 'closed' }>; /** Inputs accepted while opening one framed channel above an owned process. */ export interface ProcessChannelOptions { /** Exact schemas for requests, responses, notices, failures, and reverse calls. */ readonly protocol: ProcessChannelProtocol; /** Maximum UTF-8 bytes in one JSON frame, excluding the newline delimiter. */ readonly maximumFrameBytes?: number; /** Optional factory used when the caller does not supply a process request ID. */ readonly requestId?: () => string; /** Optional observation contract or callback for this process channel. */ readonly notice?: ProcessNoticeSink; /** Make one correlated reverse request while this process channel remains active. */ readonly call?: ProcessCallRun; } /** Owned parent-side framed request endpoint. The underlying Process keeps process lifetime authority. */ export interface ProcessChannel extends AsyncDisposable { /** Operating-system process ID used only for diagnostics and process ownership. */ readonly pid: number; /** Read-only local observations for request and channel lifecycle changes. */ readonly events: EventBus['events']; /** Send one correlated request and await its sole terminal response. */ request(ctx: Context, request: Request, options?: ProcessRequestOptions): Promise; /** Block the matching child request at its next cooperative checkpoint. */ pause(id: string): Promise; /** Release the matching paused checkpoint generation. */ resume(id: string): Promise; /** Stop new requests and close the framed protocol without taking process-lifecycle authority. */ close(reason?: unknown): Promise; } /** Controls available to one child-side active request. */ export interface ProcessRequestControl { /** Wait while the parent has paused this request and recheck cancellation before returning. */ checkpoint(): Promise; /** Publish one optional non-authoritative notice to the parent. */ notify(notice: Notice): Promise; /** Make one correlated reverse call to the parent and await its result. */ call(request: CallRequest): Promise; } /** Child-side request implementation. */ export type ProcessRequestRun = ( request: Request, ctx: Context, control: ProcessRequestControl, ) => Response | Promise; /** Inputs accepted while serving one framed process protocol. */ export interface ProcessServeOptions { /** Exact framed protocol contract accepted by the child server. */ readonly protocol: ProcessChannelProtocol; /** Readable protocol stream consumed by the child-side channel server. */ readonly input: ReadableStream; /** Protocol-only byte stream used for framed responses; diagnostics belong on stderr. */ readonly output: WritableStream; /** Child-side implementation for one correlated request. */ readonly run: ProcessRequestRun; /** Maximum UTF-8 bytes accepted in one framed protocol message. */ readonly maximumFrameBytes?: number; /** Optional factory used to create child-to-parent reverse-call IDs. */ readonly callId?: () => string; } /** Owned child-side process channel server. */ export interface ProcessServer extends AsyncDisposable { /** Terminal Promise for child-side protocol shutdown. */ readonly closed: Promise; /** Stop accepting frames and close the child-side protocol owner. */ close(reason?: unknown): Promise; } /** Parent-to-child request frame. */ export interface ProcessRequestFrame { /** Stable discriminant for this process request variant. */ readonly type: 'request'; /** Optional caller-supplied correlation ID for one process request. */ readonly id: string; /** Serializable execution-context snapshot restored by the receiving host. */ readonly context: Snapshot; /** Request payload validated before child-side execution begins. */ readonly request: Request; } /** Parent-to-child control frames. */ export type ProcessControlFrame = | Readonly<{ readonly type: 'cancel'; readonly id: string; readonly reason?: unknown }> | Readonly<{ readonly type: 'pause'; readonly id: string }> | Readonly<{ readonly type: 'resume'; readonly id: string }> | Readonly<{ readonly type: 'call-result'; readonly id: string; readonly callId: string; readonly response: CallResponse }> | Readonly<{ readonly type: 'call-fault'; readonly id: string; readonly callId: string; readonly fault: unknown }> | Readonly<{ readonly type: 'close'; readonly reason?: unknown }>; /** Child-to-parent terminal, notice, reverse-call, and close frames. */ export type ProcessResponseFrame = | Readonly<{ readonly type: 'result'; readonly id: string; readonly response: Response }> | Readonly<{ readonly type: 'failure'; readonly id: string; readonly failure: EncodedFailure }> | Readonly<{ readonly type: 'notice'; readonly id: string; readonly notice: Notice }> | Readonly<{ readonly type: 'call'; readonly id: string; readonly callId: string; readonly request: CallRequest }> | Readonly<{ readonly type: 'fault'; readonly id?: string; readonly fault: unknown }> | Readonly<{ readonly type: 'closed' }>; /** Process shape required by `channel.open()`. */ export type ProcessChannelSource = Process & Required>;