@ultrst-armada/krnl 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,10 +15,107 @@ declare class LifecycleManager {
15
15
  assertRunning(serviceName: string): void;
16
16
  }
17
17
 
18
+ /**
19
+ * @file EventTypes.ts
20
+ * @description Canonical event contracts for functionality and orchestration events.
21
+ */
22
+ type EventHandler<TPayload> = (payload: TPayload) => void;
23
+ interface EventSubscription {
24
+ readonly unsubscribe: () => void;
25
+ }
26
+ interface EventChannel<TPayload> {
27
+ readonly name: string;
28
+ subscribe(handler: EventHandler<TPayload>): EventSubscription;
29
+ on(handler: EventHandler<TPayload>): () => void;
30
+ }
31
+ interface EventDefinition<TPayload = unknown> {
32
+ readonly name: string;
33
+ readonly description?: string;
34
+ readonly payloadSchema?: Record<string, unknown>;
35
+ readonly marker?: TPayload;
36
+ }
37
+ interface FunctionalityLifecyclePayload {
38
+ readonly serviceId: string;
39
+ readonly functionalityId: string;
40
+ readonly functionalityName: string;
41
+ readonly correlationId?: string;
42
+ readonly timestamp: number;
43
+ readonly input?: unknown;
44
+ readonly output?: unknown;
45
+ readonly error?: string;
46
+ }
47
+ type FunctionalityLifecycleEvent = 'triggered' | 'completed' | 'failed';
48
+ interface OrchestrationLifecyclePayload {
49
+ readonly serviceId: string;
50
+ readonly orchestrationId: string;
51
+ readonly orchestrationName: string;
52
+ readonly correlationId: string;
53
+ readonly executionId?: string;
54
+ readonly timestamp: number;
55
+ readonly status?: string;
56
+ readonly output?: unknown;
57
+ readonly error?: string;
58
+ }
59
+
60
+ /**
61
+ * @file FunctionalityContract.ts
62
+ * @description Canonical functionality contracts and event surface.
63
+ */
64
+
65
+ type FunctionalityCustomEvents = Record<string, unknown>;
66
+ type FunctionalityEvents<TEvents extends FunctionalityCustomEvents = {}> = Omit<TEvents, 'triggered' | 'completed' | 'failed'> & {
67
+ triggered: FunctionalityLifecyclePayload;
68
+ completed: FunctionalityLifecyclePayload;
69
+ failed: FunctionalityLifecyclePayload;
70
+ };
71
+ interface FunctionalityExecutionContext<TState = Record<string, unknown>, TEvents extends FunctionalityCustomEvents = {}> {
72
+ readonly serviceId: string;
73
+ readonly functionalityId: string;
74
+ readonly correlationId?: string;
75
+ readonly state: Readonly<TState>;
76
+ readonly metadata?: Record<string, unknown>;
77
+ readonly emit: <K extends keyof FunctionalityEvents<TEvents> & string>(event: K, payload: FunctionalityEvents<TEvents>[K]) => void;
78
+ }
79
+ interface FunctionalityDefinition<TInput = unknown, TOutput = unknown, TState = Record<string, unknown>, TEvents extends FunctionalityCustomEvents = {}> {
80
+ readonly id?: string;
81
+ readonly name?: string;
82
+ readonly description?: string;
83
+ readonly events?: Record<keyof TEvents & string, EventDefinition<any>>;
84
+ readonly bubbleEvents?: boolean;
85
+ readonly execute: (input: TInput, context: FunctionalityExecutionContext<TState, TEvents>) => Promise<TOutput> | TOutput;
86
+ }
87
+ type FunctionalityEventNamespace<TEvents extends FunctionalityCustomEvents = {}> = {
88
+ readonly [K in keyof FunctionalityEvents<TEvents>]: EventChannel<FunctionalityEvents<TEvents>[K]>;
89
+ };
90
+
91
+ /**
92
+ * @file FunctionalityFacade.ts
93
+ * @description First-class functionality boundary with lifecycle and custom events.
94
+ */
95
+
96
+ declare class FunctionalityFacade<TInput = unknown, TOutput = unknown, TState = Record<string, unknown>, TEvents extends Record<string, unknown> = {}> {
97
+ readonly id: string;
98
+ readonly name: string;
99
+ readonly description: string;
100
+ readonly bubbleEvents: boolean;
101
+ readonly definition: FunctionalityDefinition<TInput, TOutput, TState, TEvents>;
102
+ private readonly eventBus;
103
+ readonly events: FunctionalityEventNamespace<TEvents>;
104
+ constructor(id: string, definition: FunctionalityDefinition<TInput, TOutput, TState, TEvents>, options?: {
105
+ bubbleEvents?: boolean;
106
+ });
107
+ getEventDefinitions(): Record<string, EventDefinition<any>>;
108
+ subscribe<K extends keyof FunctionalityEvents<TEvents> & string>(event: K, handler: (payload: FunctionalityEvents<TEvents>[K]) => void): () => void;
109
+ emit<K extends keyof FunctionalityEvents<TEvents> & string>(event: K, payload: FunctionalityEvents<TEvents>[K]): void;
110
+ execute(input: TInput, context: Omit<FunctionalityExecutionContext<TState, TEvents>, 'emit'>): Promise<TOutput>;
111
+ clearEventSubscriptions(): void;
112
+ }
113
+
18
114
  /**
19
115
  * @file ServiceContract.ts
20
116
  * @description Canonical service contracts for the KrnlTS Service Kernel.
21
- * Invariant: local and remote services share the same IService contract.
117
+ * Services are execution and namespace boundaries; application events belong to
118
+ * functionalities and orchestration lifecycle, never to the service itself.
22
119
  */
23
120
 
24
121
  type ProtocolType = 'http' | 'https' | 'ws' | 'wss' | 'udp' | 'p2p' | 'signaling' | 'memory';
@@ -70,7 +167,7 @@ interface ServiceCapability {
70
167
  protocols: ProtocolType[];
71
168
  metadata?: Record<string, unknown>;
72
169
  }
73
- interface ServiceNamespace<TLibraries extends Record<string, unknown> = Record<string, unknown>, TFunctionalities extends Record<string, unknown> = Record<string, unknown>, TOrchestrations extends Record<string, unknown> = Record<string, unknown>> {
170
+ interface ServiceNamespace<TLibraries extends Record<string, unknown> = Record<string, unknown>, TFunctionalities extends Record<string, FunctionalityFacade<any, any, any, any>> = Record<string, FunctionalityFacade<any, any, any, any>>, TOrchestrations extends Record<string, unknown> = Record<string, unknown>> {
74
171
  readonly serviceId: string;
75
172
  readonly serviceName: string;
76
173
  readonly version: string;
@@ -89,7 +186,9 @@ interface EndpointDescriptor {
89
186
  channel?: string;
90
187
  capabilities: string[];
91
188
  }
92
- interface ServiceDefinition<TState extends Record<string, unknown> = Record<string, unknown>, TEvents extends Record<string, unknown> = Record<string, unknown>> {
189
+ interface ServiceDefinition<TState extends Record<string, unknown> = Record<string, unknown>,
190
+ /** @deprecated Service-owned events are no longer part of the KrnlTS event model. */
191
+ TLegacyEvents extends Record<string, unknown> = Record<string, unknown>> {
93
192
  id: string;
94
193
  name: string;
95
194
  version: string;
@@ -97,26 +196,25 @@ interface ServiceDefinition<TState extends Record<string, unknown> = Record<stri
97
196
  outputs?: Record<string, Record<string, unknown>>;
98
197
  operations: Record<string, OperationContract<any, any>>;
99
198
  state?: StateContract<TState>;
100
- events?: Record<keyof TEvents & string, EventContract<any>>;
101
199
  lifecycle?: LifecycleContract;
102
200
  dependencies?: DependencyContract[];
103
201
  capabilities?: ServiceCapability[];
104
202
  protocols?: ProtocolType[];
105
203
  }
106
- interface IService<TState extends Record<string, unknown> = Record<string, unknown>, TEvents extends Record<string, unknown> = Record<string, unknown>> {
204
+ interface IService<TState extends Record<string, unknown> = Record<string, unknown>,
205
+ /** @deprecated Service-owned events are no longer part of the KrnlTS event model. */
206
+ TLegacyEvents extends Record<string, unknown> = Record<string, unknown>> {
107
207
  readonly id: string;
108
208
  readonly name: string;
109
209
  readonly version: string;
110
210
  call<TIn = unknown, TOut = unknown>(op: string, input?: TIn, ctx?: OperationContext): Promise<TOut>;
111
211
  getState(): Readonly<TState>;
112
212
  setState(mutation: Partial<TState>): void;
113
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
114
- emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
115
213
  getStatus(): ServiceStatus;
116
214
  start(): Promise<void>;
117
215
  stop(): Promise<void>;
118
216
  dispose(): Promise<void>;
119
- getDefinition(): ServiceDefinition<TState, TEvents>;
217
+ getDefinition(): ServiceDefinition<TState, TLegacyEvents>;
120
218
  }
121
219
 
122
220
  /**
@@ -130,7 +228,6 @@ declare class ServiceInstance<TState extends Record<string, unknown> = Record<st
130
228
  readonly name: string;
131
229
  readonly version: string;
132
230
  private stateStore?;
133
- private readonly eventBus;
134
231
  private readonly lifecycle;
135
232
  private initialized;
136
233
  private disposed;
@@ -143,8 +240,6 @@ declare class ServiceInstance<TState extends Record<string, unknown> = Record<st
143
240
  call<TIn = unknown, TOut = unknown>(op: string, input?: TIn, ctx?: OperationContext): Promise<TOut>;
144
241
  getState(): Readonly<TState>;
145
242
  setState(mutation: Partial<TState>): void;
146
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
147
- emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
148
243
  }
149
244
 
150
245
  /**
@@ -296,6 +391,7 @@ interface OrchestrationDefinition<TInput = unknown, TOutput = unknown> {
296
391
  readonly version: string;
297
392
  readonly description?: string;
298
393
  readonly scope: 'functionality' | 'service';
394
+ readonly bubbleEvents?: boolean;
299
395
  readonly dependencies: readonly string[];
300
396
  readonly defaultFrontend?: OrchestrationFrontendReference;
301
397
  readonly timeoutMs?: number;
@@ -334,6 +430,7 @@ interface OrchestrationDescriptor {
334
430
  readonly timeoutMs?: number;
335
431
  readonly concurrency?: number;
336
432
  readonly scope?: 'functionality' | 'service';
433
+ readonly bubbleEvents?: boolean;
337
434
  }
338
435
  interface OrchestrationContext {
339
436
  readonly correlationId: string;
@@ -386,6 +483,27 @@ interface DAGValidationResult {
386
483
  */
387
484
  declare function validateOrchestrationDAG(steps: readonly OrchestrationStep[]): DAGValidationResult;
388
485
 
486
+ /**
487
+ * @file OrchestrationEventFacade.ts
488
+ * @description Default lifecycle event surface for functionality orchestrations.
489
+ */
490
+
491
+ interface OrchestrationEventNamespace {
492
+ readonly triggered: EventChannel<OrchestrationLifecyclePayload>;
493
+ readonly completed: EventChannel<OrchestrationLifecyclePayload>;
494
+ readonly failed: EventChannel<OrchestrationLifecyclePayload>;
495
+ }
496
+ declare class OrchestrationEventFacade {
497
+ private readonly eventBus;
498
+ readonly events: OrchestrationEventNamespace;
499
+ constructor();
500
+ get triggered(): EventChannel<OrchestrationLifecyclePayload>;
501
+ get completed(): EventChannel<OrchestrationLifecyclePayload>;
502
+ get failed(): EventChannel<OrchestrationLifecyclePayload>;
503
+ emit(event: 'triggered' | 'completed' | 'failed', payload: OrchestrationLifecyclePayload): void;
504
+ clear(): void;
505
+ }
506
+
389
507
  interface FunctionalityFlowStep {
390
508
  readonly id: string;
391
509
  readonly type: 'functionality' | 'functionalityOrchestration';
@@ -428,6 +546,8 @@ declare class FunctionalityOrchestration {
428
546
  readonly description: string;
429
547
  readonly timeoutMs?: number;
430
548
  readonly scope: 'functionality';
549
+ readonly bubbleEvents: boolean;
550
+ readonly events: OrchestrationEventFacade;
431
551
  private readonly steps;
432
552
  private readonly flows;
433
553
  constructor(descriptor: OrchestrationDescriptor);
@@ -691,7 +811,7 @@ declare class ServiceFacade<TState extends Record<string, unknown> = Record<stri
691
811
  readonly name: string;
692
812
  readonly version: string;
693
813
  readonly description: string;
694
- readonly functionalities: Record<string, unknown>;
814
+ readonly functionalities: Record<string, FunctionalityFacade<any, any, any, any>>;
695
815
  readonly functionalityOrchestrations: Record<string, FunctionalityOrchestration>;
696
816
  readonly libraries: TLibraries;
697
817
  readonly dependencies: readonly DependencyContract[];
@@ -740,17 +860,18 @@ declare class ServiceFacade<TState extends Record<string, unknown> = Record<stri
740
860
  }): Promise<any>;
741
861
  getState(): Readonly<TState>;
742
862
  setState(mutation: Partial<TState>): void;
743
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
744
- emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
745
- registerFunctionality(name: string, functionality: unknown): this;
863
+ getFunctionality(name: string): FunctionalityFacade<any, any, any, any>;
864
+ registerFunctionality(name: string, functionality: unknown, options?: {
865
+ bubbleEvents?: boolean;
866
+ }): this;
746
867
  registerFunctionalityOrchestration(name: string, orchestration: FunctionalityOrchestration): this;
747
868
  /** @deprecated Use registerFunctionalityOrchestration */
748
869
  registerOrchestration(name: string, orchestration: FunctionalityOrchestration): this;
749
870
  registerLibrary<TLib = unknown>(name: string, library: TLib): this;
750
871
  getLibrary<TLib = unknown>(name: string): TLib;
751
- get namespace(): ServiceNamespace<TLibraries, Record<string, unknown>, Record<string, FunctionalityOrchestration>>;
752
- getNamespace(): ServiceNamespace<TLibraries, Record<string, unknown>, Record<string, FunctionalityOrchestration>>;
753
- getServiceFacadeMetadata(): ServiceNamespace<TLibraries, Record<string, unknown>, Record<string, FunctionalityOrchestration>>;
872
+ get namespace(): ServiceNamespace<TLibraries, Record<string, FunctionalityFacade<any, any, any, any>>, Record<string, FunctionalityOrchestration>>;
873
+ getNamespace(): ServiceNamespace<TLibraries, Record<string, FunctionalityFacade<any, any, any, any>>, Record<string, FunctionalityOrchestration>>;
874
+ getServiceFacadeMetadata(): ServiceNamespace<TLibraries, Record<string, FunctionalityFacade<any, any, any, any>>, Record<string, FunctionalityOrchestration>>;
754
875
  }
755
876
  declare function createServiceFacade<TState extends Record<string, unknown> = Record<string, unknown>, TEvents extends Record<string, unknown> = Record<string, unknown>, TLibraries extends Record<string, unknown> = Record<string, unknown>>(config: ServiceFacadeConfig<TState, TEvents, TLibraries>): ServiceFacade<TState, TEvents, TLibraries>;
756
877
 
@@ -946,15 +1067,31 @@ declare class ServiceStateStore<TState extends Record<string, unknown> = Record<
946
1067
  }
947
1068
 
948
1069
  /**
949
- * @file ServiceEventBus.ts
950
- * @description In-memory strongly-typed event bus for services.
1070
+ * @file EventBus.ts
1071
+ * @description Generic in-process event bus used by functionality and orchestration event surfaces.
951
1072
  */
952
- declare class ServiceEventBus<TEvents extends Record<string, unknown> = Record<string, unknown>> {
953
- private handlers;
954
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
1073
+
1074
+ declare class EventBus<TEvents extends Record<string, unknown> = Record<string, unknown>> {
1075
+ private readonly handlers;
1076
+ subscribe<K extends keyof TEvents & string>(event: K, handler: EventHandler<TEvents[K]>): EventSubscription;
1077
+ on<K extends keyof TEvents & string>(event: K, handler: EventHandler<TEvents[K]>): () => void;
955
1078
  emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
956
1079
  clear(): void;
957
1080
  }
1081
+ declare function createEventChannels<TEvents extends Record<string, unknown>>(bus: EventBus<TEvents>, names: readonly (keyof TEvents & string)[]): {
1082
+ [K in keyof TEvents]: EventChannel<TEvents[K]>;
1083
+ };
1084
+
1085
+ /**
1086
+ * @file ServiceEventBus.ts
1087
+ * @description Backward-compatible generic event bus.
1088
+ *
1089
+ * KrnlTS no longer defines service-owned application events. First-class
1090
+ * application events are owned by functionalities and orchestration lifecycle.
1091
+ */
1092
+
1093
+ declare class ServiceEventBus<TEvents extends Record<string, unknown> = Record<string, unknown>> extends EventBus<TEvents> {
1094
+ }
958
1095
 
959
1096
  /**
960
1097
  * @file ProtocolResolver.ts
@@ -966,4 +1103,4 @@ declare class ProtocolResolver {
966
1103
  static getSupportedProtocols(definition: ServiceDefinition): ProtocolType[];
967
1104
  }
968
1105
 
969
- export { type BackendCommand, type Connection, type DAGValidationResult, type DefaultFrontendReference, type DependencyContract, type EndpointDescriptor, type EventContract, type ExecutionContext, type FunctionalityFlow, type FunctionalityFlowStep, FunctionalityOrchestration, type FunctionalityStepOptions, HttpProtocolAdapter, type IService, type IServiceFacade, type JobStatus, type KrnlJobDefinition, type KrnlJobHandle, type KrnlJobOptions, KrnlPackageReconciler, KrnlQueue, type KrnlQueueOptions, type KrnlWorkerInfo, KrnlWorkerPool, type KrnlWorkerState, type LifecycleContract, LifecycleManager, MemoryTransport, type NormalizedResult, type OperationContext, type OperationContract, type OrchestrationContext, type OrchestrationDefinition, type OrchestrationDescriptor, type OrchestrationFrontendReference, type OrchestrationResult, type OrchestrationStatus, type OrchestrationStep, P2PProtocolAdapter, type PeerRecord, type ProtocolAdapter, type ProtocolCapabilities, type ProtocolEnvelope, ProtocolResolver, type ProtocolType, type ReconcileResult, type RegisteredServiceRecord, type SagaCompensationHandler, type ServiceCapability, type ServiceDefinition, ServiceDiscovery, ServiceEventBus, ServiceFacade, type ServiceFacadeConfig, ServiceInstance, type ServiceNamespace, ServiceProxy, ServiceRegistry, ServiceRuntime, ServiceStateStore, type ServiceStatus, type StateContract, type StepCompensation, type StepCondition, type StepDefinition, type StepExecutionResult, type StepExecutionStatus, type StepRetryPolicy, type StepStatus, type StepTimeoutPolicy, type StepType, UdpProtocolAdapter, WebSocketProtocolAdapter, WorkerExecutionBoundary, createService, createServiceDefinition, createServiceFacade, createServiceProxy, validateOrchestrationDAG, validateServiceDefinition };
1106
+ export { type BackendCommand, type Connection, type DAGValidationResult, type DefaultFrontendReference, type DependencyContract, type EndpointDescriptor, EventBus, type EventChannel, type EventContract, type EventDefinition, type EventHandler, type EventSubscription, type ExecutionContext, type FunctionalityCustomEvents, type FunctionalityDefinition, type FunctionalityEventNamespace, type FunctionalityEvents, type FunctionalityExecutionContext, FunctionalityFacade, type FunctionalityFlow, type FunctionalityFlowStep, type FunctionalityLifecycleEvent, type FunctionalityLifecyclePayload, FunctionalityOrchestration, type FunctionalityStepOptions, HttpProtocolAdapter, type IService, type IServiceFacade, type JobStatus, type KrnlJobDefinition, type KrnlJobHandle, type KrnlJobOptions, KrnlPackageReconciler, KrnlQueue, type KrnlQueueOptions, type KrnlWorkerInfo, KrnlWorkerPool, type KrnlWorkerState, type LifecycleContract, LifecycleManager, MemoryTransport, type NormalizedResult, type OperationContext, type OperationContract, type OrchestrationContext, type OrchestrationDefinition, type OrchestrationDescriptor, OrchestrationEventFacade, type OrchestrationEventNamespace, type OrchestrationFrontendReference, type OrchestrationLifecyclePayload, type OrchestrationResult, type OrchestrationStatus, type OrchestrationStep, P2PProtocolAdapter, type PeerRecord, type ProtocolAdapter, type ProtocolCapabilities, type ProtocolEnvelope, ProtocolResolver, type ProtocolType, type ReconcileResult, type RegisteredServiceRecord, type SagaCompensationHandler, type ServiceCapability, type ServiceDefinition, ServiceDiscovery, ServiceEventBus, ServiceFacade, type ServiceFacadeConfig, ServiceInstance, type ServiceNamespace, ServiceProxy, ServiceRegistry, ServiceRuntime, ServiceStateStore, type ServiceStatus, type StateContract, type StepCompensation, type StepCondition, type StepDefinition, type StepExecutionResult, type StepExecutionStatus, type StepRetryPolicy, type StepStatus, type StepTimeoutPolicy, type StepType, UdpProtocolAdapter, WebSocketProtocolAdapter, WorkerExecutionBoundary, createEventChannels, createService, createServiceDefinition, createServiceFacade, createServiceProxy, validateOrchestrationDAG, validateServiceDefinition };
@@ -15,10 +15,107 @@ declare class LifecycleManager {
15
15
  assertRunning(serviceName: string): void;
16
16
  }
17
17
 
18
+ /**
19
+ * @file EventTypes.ts
20
+ * @description Canonical event contracts for functionality and orchestration events.
21
+ */
22
+ type EventHandler<TPayload> = (payload: TPayload) => void;
23
+ interface EventSubscription {
24
+ readonly unsubscribe: () => void;
25
+ }
26
+ interface EventChannel<TPayload> {
27
+ readonly name: string;
28
+ subscribe(handler: EventHandler<TPayload>): EventSubscription;
29
+ on(handler: EventHandler<TPayload>): () => void;
30
+ }
31
+ interface EventDefinition<TPayload = unknown> {
32
+ readonly name: string;
33
+ readonly description?: string;
34
+ readonly payloadSchema?: Record<string, unknown>;
35
+ readonly marker?: TPayload;
36
+ }
37
+ interface FunctionalityLifecyclePayload {
38
+ readonly serviceId: string;
39
+ readonly functionalityId: string;
40
+ readonly functionalityName: string;
41
+ readonly correlationId?: string;
42
+ readonly timestamp: number;
43
+ readonly input?: unknown;
44
+ readonly output?: unknown;
45
+ readonly error?: string;
46
+ }
47
+ type FunctionalityLifecycleEvent = 'triggered' | 'completed' | 'failed';
48
+ interface OrchestrationLifecyclePayload {
49
+ readonly serviceId: string;
50
+ readonly orchestrationId: string;
51
+ readonly orchestrationName: string;
52
+ readonly correlationId: string;
53
+ readonly executionId?: string;
54
+ readonly timestamp: number;
55
+ readonly status?: string;
56
+ readonly output?: unknown;
57
+ readonly error?: string;
58
+ }
59
+
60
+ /**
61
+ * @file FunctionalityContract.ts
62
+ * @description Canonical functionality contracts and event surface.
63
+ */
64
+
65
+ type FunctionalityCustomEvents = Record<string, unknown>;
66
+ type FunctionalityEvents<TEvents extends FunctionalityCustomEvents = {}> = Omit<TEvents, 'triggered' | 'completed' | 'failed'> & {
67
+ triggered: FunctionalityLifecyclePayload;
68
+ completed: FunctionalityLifecyclePayload;
69
+ failed: FunctionalityLifecyclePayload;
70
+ };
71
+ interface FunctionalityExecutionContext<TState = Record<string, unknown>, TEvents extends FunctionalityCustomEvents = {}> {
72
+ readonly serviceId: string;
73
+ readonly functionalityId: string;
74
+ readonly correlationId?: string;
75
+ readonly state: Readonly<TState>;
76
+ readonly metadata?: Record<string, unknown>;
77
+ readonly emit: <K extends keyof FunctionalityEvents<TEvents> & string>(event: K, payload: FunctionalityEvents<TEvents>[K]) => void;
78
+ }
79
+ interface FunctionalityDefinition<TInput = unknown, TOutput = unknown, TState = Record<string, unknown>, TEvents extends FunctionalityCustomEvents = {}> {
80
+ readonly id?: string;
81
+ readonly name?: string;
82
+ readonly description?: string;
83
+ readonly events?: Record<keyof TEvents & string, EventDefinition<any>>;
84
+ readonly bubbleEvents?: boolean;
85
+ readonly execute: (input: TInput, context: FunctionalityExecutionContext<TState, TEvents>) => Promise<TOutput> | TOutput;
86
+ }
87
+ type FunctionalityEventNamespace<TEvents extends FunctionalityCustomEvents = {}> = {
88
+ readonly [K in keyof FunctionalityEvents<TEvents>]: EventChannel<FunctionalityEvents<TEvents>[K]>;
89
+ };
90
+
91
+ /**
92
+ * @file FunctionalityFacade.ts
93
+ * @description First-class functionality boundary with lifecycle and custom events.
94
+ */
95
+
96
+ declare class FunctionalityFacade<TInput = unknown, TOutput = unknown, TState = Record<string, unknown>, TEvents extends Record<string, unknown> = {}> {
97
+ readonly id: string;
98
+ readonly name: string;
99
+ readonly description: string;
100
+ readonly bubbleEvents: boolean;
101
+ readonly definition: FunctionalityDefinition<TInput, TOutput, TState, TEvents>;
102
+ private readonly eventBus;
103
+ readonly events: FunctionalityEventNamespace<TEvents>;
104
+ constructor(id: string, definition: FunctionalityDefinition<TInput, TOutput, TState, TEvents>, options?: {
105
+ bubbleEvents?: boolean;
106
+ });
107
+ getEventDefinitions(): Record<string, EventDefinition<any>>;
108
+ subscribe<K extends keyof FunctionalityEvents<TEvents> & string>(event: K, handler: (payload: FunctionalityEvents<TEvents>[K]) => void): () => void;
109
+ emit<K extends keyof FunctionalityEvents<TEvents> & string>(event: K, payload: FunctionalityEvents<TEvents>[K]): void;
110
+ execute(input: TInput, context: Omit<FunctionalityExecutionContext<TState, TEvents>, 'emit'>): Promise<TOutput>;
111
+ clearEventSubscriptions(): void;
112
+ }
113
+
18
114
  /**
19
115
  * @file ServiceContract.ts
20
116
  * @description Canonical service contracts for the KrnlTS Service Kernel.
21
- * Invariant: local and remote services share the same IService contract.
117
+ * Services are execution and namespace boundaries; application events belong to
118
+ * functionalities and orchestration lifecycle, never to the service itself.
22
119
  */
23
120
 
24
121
  type ProtocolType = 'http' | 'https' | 'ws' | 'wss' | 'udp' | 'p2p' | 'signaling' | 'memory';
@@ -70,7 +167,7 @@ interface ServiceCapability {
70
167
  protocols: ProtocolType[];
71
168
  metadata?: Record<string, unknown>;
72
169
  }
73
- interface ServiceNamespace<TLibraries extends Record<string, unknown> = Record<string, unknown>, TFunctionalities extends Record<string, unknown> = Record<string, unknown>, TOrchestrations extends Record<string, unknown> = Record<string, unknown>> {
170
+ interface ServiceNamespace<TLibraries extends Record<string, unknown> = Record<string, unknown>, TFunctionalities extends Record<string, FunctionalityFacade<any, any, any, any>> = Record<string, FunctionalityFacade<any, any, any, any>>, TOrchestrations extends Record<string, unknown> = Record<string, unknown>> {
74
171
  readonly serviceId: string;
75
172
  readonly serviceName: string;
76
173
  readonly version: string;
@@ -89,7 +186,9 @@ interface EndpointDescriptor {
89
186
  channel?: string;
90
187
  capabilities: string[];
91
188
  }
92
- interface ServiceDefinition<TState extends Record<string, unknown> = Record<string, unknown>, TEvents extends Record<string, unknown> = Record<string, unknown>> {
189
+ interface ServiceDefinition<TState extends Record<string, unknown> = Record<string, unknown>,
190
+ /** @deprecated Service-owned events are no longer part of the KrnlTS event model. */
191
+ TLegacyEvents extends Record<string, unknown> = Record<string, unknown>> {
93
192
  id: string;
94
193
  name: string;
95
194
  version: string;
@@ -97,26 +196,25 @@ interface ServiceDefinition<TState extends Record<string, unknown> = Record<stri
97
196
  outputs?: Record<string, Record<string, unknown>>;
98
197
  operations: Record<string, OperationContract<any, any>>;
99
198
  state?: StateContract<TState>;
100
- events?: Record<keyof TEvents & string, EventContract<any>>;
101
199
  lifecycle?: LifecycleContract;
102
200
  dependencies?: DependencyContract[];
103
201
  capabilities?: ServiceCapability[];
104
202
  protocols?: ProtocolType[];
105
203
  }
106
- interface IService<TState extends Record<string, unknown> = Record<string, unknown>, TEvents extends Record<string, unknown> = Record<string, unknown>> {
204
+ interface IService<TState extends Record<string, unknown> = Record<string, unknown>,
205
+ /** @deprecated Service-owned events are no longer part of the KrnlTS event model. */
206
+ TLegacyEvents extends Record<string, unknown> = Record<string, unknown>> {
107
207
  readonly id: string;
108
208
  readonly name: string;
109
209
  readonly version: string;
110
210
  call<TIn = unknown, TOut = unknown>(op: string, input?: TIn, ctx?: OperationContext): Promise<TOut>;
111
211
  getState(): Readonly<TState>;
112
212
  setState(mutation: Partial<TState>): void;
113
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
114
- emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
115
213
  getStatus(): ServiceStatus;
116
214
  start(): Promise<void>;
117
215
  stop(): Promise<void>;
118
216
  dispose(): Promise<void>;
119
- getDefinition(): ServiceDefinition<TState, TEvents>;
217
+ getDefinition(): ServiceDefinition<TState, TLegacyEvents>;
120
218
  }
121
219
 
122
220
  /**
@@ -130,7 +228,6 @@ declare class ServiceInstance<TState extends Record<string, unknown> = Record<st
130
228
  readonly name: string;
131
229
  readonly version: string;
132
230
  private stateStore?;
133
- private readonly eventBus;
134
231
  private readonly lifecycle;
135
232
  private initialized;
136
233
  private disposed;
@@ -143,8 +240,6 @@ declare class ServiceInstance<TState extends Record<string, unknown> = Record<st
143
240
  call<TIn = unknown, TOut = unknown>(op: string, input?: TIn, ctx?: OperationContext): Promise<TOut>;
144
241
  getState(): Readonly<TState>;
145
242
  setState(mutation: Partial<TState>): void;
146
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
147
- emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
148
243
  }
149
244
 
150
245
  /**
@@ -296,6 +391,7 @@ interface OrchestrationDefinition<TInput = unknown, TOutput = unknown> {
296
391
  readonly version: string;
297
392
  readonly description?: string;
298
393
  readonly scope: 'functionality' | 'service';
394
+ readonly bubbleEvents?: boolean;
299
395
  readonly dependencies: readonly string[];
300
396
  readonly defaultFrontend?: OrchestrationFrontendReference;
301
397
  readonly timeoutMs?: number;
@@ -334,6 +430,7 @@ interface OrchestrationDescriptor {
334
430
  readonly timeoutMs?: number;
335
431
  readonly concurrency?: number;
336
432
  readonly scope?: 'functionality' | 'service';
433
+ readonly bubbleEvents?: boolean;
337
434
  }
338
435
  interface OrchestrationContext {
339
436
  readonly correlationId: string;
@@ -386,6 +483,27 @@ interface DAGValidationResult {
386
483
  */
387
484
  declare function validateOrchestrationDAG(steps: readonly OrchestrationStep[]): DAGValidationResult;
388
485
 
486
+ /**
487
+ * @file OrchestrationEventFacade.ts
488
+ * @description Default lifecycle event surface for functionality orchestrations.
489
+ */
490
+
491
+ interface OrchestrationEventNamespace {
492
+ readonly triggered: EventChannel<OrchestrationLifecyclePayload>;
493
+ readonly completed: EventChannel<OrchestrationLifecyclePayload>;
494
+ readonly failed: EventChannel<OrchestrationLifecyclePayload>;
495
+ }
496
+ declare class OrchestrationEventFacade {
497
+ private readonly eventBus;
498
+ readonly events: OrchestrationEventNamespace;
499
+ constructor();
500
+ get triggered(): EventChannel<OrchestrationLifecyclePayload>;
501
+ get completed(): EventChannel<OrchestrationLifecyclePayload>;
502
+ get failed(): EventChannel<OrchestrationLifecyclePayload>;
503
+ emit(event: 'triggered' | 'completed' | 'failed', payload: OrchestrationLifecyclePayload): void;
504
+ clear(): void;
505
+ }
506
+
389
507
  interface FunctionalityFlowStep {
390
508
  readonly id: string;
391
509
  readonly type: 'functionality' | 'functionalityOrchestration';
@@ -428,6 +546,8 @@ declare class FunctionalityOrchestration {
428
546
  readonly description: string;
429
547
  readonly timeoutMs?: number;
430
548
  readonly scope: 'functionality';
549
+ readonly bubbleEvents: boolean;
550
+ readonly events: OrchestrationEventFacade;
431
551
  private readonly steps;
432
552
  private readonly flows;
433
553
  constructor(descriptor: OrchestrationDescriptor);
@@ -691,7 +811,7 @@ declare class ServiceFacade<TState extends Record<string, unknown> = Record<stri
691
811
  readonly name: string;
692
812
  readonly version: string;
693
813
  readonly description: string;
694
- readonly functionalities: Record<string, unknown>;
814
+ readonly functionalities: Record<string, FunctionalityFacade<any, any, any, any>>;
695
815
  readonly functionalityOrchestrations: Record<string, FunctionalityOrchestration>;
696
816
  readonly libraries: TLibraries;
697
817
  readonly dependencies: readonly DependencyContract[];
@@ -740,17 +860,18 @@ declare class ServiceFacade<TState extends Record<string, unknown> = Record<stri
740
860
  }): Promise<any>;
741
861
  getState(): Readonly<TState>;
742
862
  setState(mutation: Partial<TState>): void;
743
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
744
- emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
745
- registerFunctionality(name: string, functionality: unknown): this;
863
+ getFunctionality(name: string): FunctionalityFacade<any, any, any, any>;
864
+ registerFunctionality(name: string, functionality: unknown, options?: {
865
+ bubbleEvents?: boolean;
866
+ }): this;
746
867
  registerFunctionalityOrchestration(name: string, orchestration: FunctionalityOrchestration): this;
747
868
  /** @deprecated Use registerFunctionalityOrchestration */
748
869
  registerOrchestration(name: string, orchestration: FunctionalityOrchestration): this;
749
870
  registerLibrary<TLib = unknown>(name: string, library: TLib): this;
750
871
  getLibrary<TLib = unknown>(name: string): TLib;
751
- get namespace(): ServiceNamespace<TLibraries, Record<string, unknown>, Record<string, FunctionalityOrchestration>>;
752
- getNamespace(): ServiceNamespace<TLibraries, Record<string, unknown>, Record<string, FunctionalityOrchestration>>;
753
- getServiceFacadeMetadata(): ServiceNamespace<TLibraries, Record<string, unknown>, Record<string, FunctionalityOrchestration>>;
872
+ get namespace(): ServiceNamespace<TLibraries, Record<string, FunctionalityFacade<any, any, any, any>>, Record<string, FunctionalityOrchestration>>;
873
+ getNamespace(): ServiceNamespace<TLibraries, Record<string, FunctionalityFacade<any, any, any, any>>, Record<string, FunctionalityOrchestration>>;
874
+ getServiceFacadeMetadata(): ServiceNamespace<TLibraries, Record<string, FunctionalityFacade<any, any, any, any>>, Record<string, FunctionalityOrchestration>>;
754
875
  }
755
876
  declare function createServiceFacade<TState extends Record<string, unknown> = Record<string, unknown>, TEvents extends Record<string, unknown> = Record<string, unknown>, TLibraries extends Record<string, unknown> = Record<string, unknown>>(config: ServiceFacadeConfig<TState, TEvents, TLibraries>): ServiceFacade<TState, TEvents, TLibraries>;
756
877
 
@@ -946,15 +1067,31 @@ declare class ServiceStateStore<TState extends Record<string, unknown> = Record<
946
1067
  }
947
1068
 
948
1069
  /**
949
- * @file ServiceEventBus.ts
950
- * @description In-memory strongly-typed event bus for services.
1070
+ * @file EventBus.ts
1071
+ * @description Generic in-process event bus used by functionality and orchestration event surfaces.
951
1072
  */
952
- declare class ServiceEventBus<TEvents extends Record<string, unknown> = Record<string, unknown>> {
953
- private handlers;
954
- on<K extends keyof TEvents & string>(event: K, handler: (payload: TEvents[K]) => void): () => void;
1073
+
1074
+ declare class EventBus<TEvents extends Record<string, unknown> = Record<string, unknown>> {
1075
+ private readonly handlers;
1076
+ subscribe<K extends keyof TEvents & string>(event: K, handler: EventHandler<TEvents[K]>): EventSubscription;
1077
+ on<K extends keyof TEvents & string>(event: K, handler: EventHandler<TEvents[K]>): () => void;
955
1078
  emit<K extends keyof TEvents & string>(event: K, payload: TEvents[K]): void;
956
1079
  clear(): void;
957
1080
  }
1081
+ declare function createEventChannels<TEvents extends Record<string, unknown>>(bus: EventBus<TEvents>, names: readonly (keyof TEvents & string)[]): {
1082
+ [K in keyof TEvents]: EventChannel<TEvents[K]>;
1083
+ };
1084
+
1085
+ /**
1086
+ * @file ServiceEventBus.ts
1087
+ * @description Backward-compatible generic event bus.
1088
+ *
1089
+ * KrnlTS no longer defines service-owned application events. First-class
1090
+ * application events are owned by functionalities and orchestration lifecycle.
1091
+ */
1092
+
1093
+ declare class ServiceEventBus<TEvents extends Record<string, unknown> = Record<string, unknown>> extends EventBus<TEvents> {
1094
+ }
958
1095
 
959
1096
  /**
960
1097
  * @file ProtocolResolver.ts
@@ -966,4 +1103,4 @@ declare class ProtocolResolver {
966
1103
  static getSupportedProtocols(definition: ServiceDefinition): ProtocolType[];
967
1104
  }
968
1105
 
969
- export { type BackendCommand, type Connection, type DAGValidationResult, type DefaultFrontendReference, type DependencyContract, type EndpointDescriptor, type EventContract, type ExecutionContext, type FunctionalityFlow, type FunctionalityFlowStep, FunctionalityOrchestration, type FunctionalityStepOptions, HttpProtocolAdapter, type IService, type IServiceFacade, type JobStatus, type KrnlJobDefinition, type KrnlJobHandle, type KrnlJobOptions, KrnlPackageReconciler, KrnlQueue, type KrnlQueueOptions, type KrnlWorkerInfo, KrnlWorkerPool, type KrnlWorkerState, type LifecycleContract, LifecycleManager, MemoryTransport, type NormalizedResult, type OperationContext, type OperationContract, type OrchestrationContext, type OrchestrationDefinition, type OrchestrationDescriptor, type OrchestrationFrontendReference, type OrchestrationResult, type OrchestrationStatus, type OrchestrationStep, P2PProtocolAdapter, type PeerRecord, type ProtocolAdapter, type ProtocolCapabilities, type ProtocolEnvelope, ProtocolResolver, type ProtocolType, type ReconcileResult, type RegisteredServiceRecord, type SagaCompensationHandler, type ServiceCapability, type ServiceDefinition, ServiceDiscovery, ServiceEventBus, ServiceFacade, type ServiceFacadeConfig, ServiceInstance, type ServiceNamespace, ServiceProxy, ServiceRegistry, ServiceRuntime, ServiceStateStore, type ServiceStatus, type StateContract, type StepCompensation, type StepCondition, type StepDefinition, type StepExecutionResult, type StepExecutionStatus, type StepRetryPolicy, type StepStatus, type StepTimeoutPolicy, type StepType, UdpProtocolAdapter, WebSocketProtocolAdapter, WorkerExecutionBoundary, createService, createServiceDefinition, createServiceFacade, createServiceProxy, validateOrchestrationDAG, validateServiceDefinition };
1106
+ export { type BackendCommand, type Connection, type DAGValidationResult, type DefaultFrontendReference, type DependencyContract, type EndpointDescriptor, EventBus, type EventChannel, type EventContract, type EventDefinition, type EventHandler, type EventSubscription, type ExecutionContext, type FunctionalityCustomEvents, type FunctionalityDefinition, type FunctionalityEventNamespace, type FunctionalityEvents, type FunctionalityExecutionContext, FunctionalityFacade, type FunctionalityFlow, type FunctionalityFlowStep, type FunctionalityLifecycleEvent, type FunctionalityLifecyclePayload, FunctionalityOrchestration, type FunctionalityStepOptions, HttpProtocolAdapter, type IService, type IServiceFacade, type JobStatus, type KrnlJobDefinition, type KrnlJobHandle, type KrnlJobOptions, KrnlPackageReconciler, KrnlQueue, type KrnlQueueOptions, type KrnlWorkerInfo, KrnlWorkerPool, type KrnlWorkerState, type LifecycleContract, LifecycleManager, MemoryTransport, type NormalizedResult, type OperationContext, type OperationContract, type OrchestrationContext, type OrchestrationDefinition, type OrchestrationDescriptor, OrchestrationEventFacade, type OrchestrationEventNamespace, type OrchestrationFrontendReference, type OrchestrationLifecyclePayload, type OrchestrationResult, type OrchestrationStatus, type OrchestrationStep, P2PProtocolAdapter, type PeerRecord, type ProtocolAdapter, type ProtocolCapabilities, type ProtocolEnvelope, ProtocolResolver, type ProtocolType, type ReconcileResult, type RegisteredServiceRecord, type SagaCompensationHandler, type ServiceCapability, type ServiceDefinition, ServiceDiscovery, ServiceEventBus, ServiceFacade, type ServiceFacadeConfig, ServiceInstance, type ServiceNamespace, ServiceProxy, ServiceRegistry, ServiceRuntime, ServiceStateStore, type ServiceStatus, type StateContract, type StepCompensation, type StepCondition, type StepDefinition, type StepExecutionResult, type StepExecutionStatus, type StepRetryPolicy, type StepStatus, type StepTimeoutPolicy, type StepType, UdpProtocolAdapter, WebSocketProtocolAdapter, WorkerExecutionBoundary, createEventChannels, createService, createServiceDefinition, createServiceFacade, createServiceProxy, validateOrchestrationDAG, validateServiceDefinition };