import type { MachineStep } from "../executor/types"; import type { InstanceMessage, PackStateUpdatePayload, StateUpdatePayload } from "./messages"; /** * Scope identity for externalized state handlers. */ export type ExternalScope = | { kind: "pack"; id: string; packName: string; rootInstanceId: string; } | { kind: "node"; id: string; instanceId: string; nodeId: string; rootInstanceId: string; }; /** * Event emitted when an instance message mutates state. * This is the normalized unit for syncing externalized state. */ export interface ExternalStateMutationEvent { /** Scope this mutation targets */ scope: ExternalScope; /** Current local state at dispatch time */ state: S; /** Partial patch requested by the runtime */ patch: Record; /** Original instance message payload */ payload: StateUpdatePayload | PackStateUpdatePayload; /** Full instance message */ message: InstanceMessage; /** Step number where this mutation was observed */ stepNumber: number; /** Monotonic sequence within this machine runtime */ sequence: number; } /** * Options for applying externalized state back into runtime. */ export interface ExternalSetStateOptions { /** patch = shallow merge, replace = replace full state */ mode?: "patch" | "replace"; /** Optional external revision/version for conflict handling */ revision?: number; /** Source attribution for diagnostics */ source?: "external" | "optimistic"; } /** * Context passed to externalized state handlers. * Works identically for pack and node scopes. */ export interface ExternalStateHandlerContext { scope: ExternalScope; getState: () => S; setState: (next: Partial | S, options?: ExternalSetStateOptions) => void; onMutation: (cb: (state: S, event: ExternalStateMutationEvent) => void | Promise) => void; /** Alias for onMutation for ergonomic parity with prior proposal */ onInstanceMessage: ( cb: (state: S, event: ExternalStateMutationEvent) => void | Promise ) => void; onStep: ( cb: ( step: MachineStep, events: ExternalStateMutationEvent[], ) => void | Promise, ) => void; onHydrate: (cb: () => void | Promise) => void; } /** * External state handler. Return cleanup to unsubscribe listeners. */ export type ExternalStateHandler = ( ctx: ExternalStateHandlerContext, ) => void | (() => void); /** * Externalization config for state ownership. */ export interface ExternalizeStateConfig { state: ExternalStateHandler; } /** * Runtime interface attached to Machine for dispatching externalized state events. */ export interface ExternalizeRuntime { syncRegistrations: () => void; consumeInstanceMessage: ( message: InstanceMessage, stepNumber: number, ) => ExternalStateMutationEvent | undefined; notifyStep: ( step: MachineStep, events: ExternalStateMutationEvent[], ) => void; dispose: () => void; }