brass-runtime 1.12.1 → 1.13.2

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.
@@ -0,0 +1,797 @@
1
+ type Canceler = () => void;
2
+ type CancelToken = {
3
+ /** true si ya fue cancelado */
4
+ readonly isCancelled: () => boolean;
5
+ /**
6
+ * Registra un callback que se ejecuta cuando se cancela.
7
+ * Si ya estaba cancelado, lo ejecuta inmediatamente.
8
+ * Devuelve un "unsubscribe" para desregistrar.
9
+ */
10
+ readonly onCancel: (f: Canceler) => Canceler;
11
+ };
12
+ /** Implementación simple de CancelToken */
13
+ declare function makeCancelToken(): CancelToken & {
14
+ cancel: Canceler;
15
+ };
16
+ /**
17
+ * Helper: conecta un AbortController a un CancelToken.
18
+ * Devuelve una función para desenganchar (unsubscribe).
19
+ */
20
+ declare function linkAbortController(token: CancelToken, ac: AbortController): Canceler;
21
+
22
+ type RuntimeCapabilities = {
23
+ wasmAvailable: boolean;
24
+ wasmFiberEngine: boolean;
25
+ wasmRingBuffer: boolean;
26
+ wasmScheduler: boolean;
27
+ wasmFiberRegistry: boolean;
28
+ wasmStreamChunks: boolean;
29
+ };
30
+ declare function runtimeCapabilities(): RuntimeCapabilities;
31
+
32
+ declare const enum PushStatus {
33
+ Ok = 0,
34
+ Grew = 1,
35
+ Dropped = 2
36
+ }
37
+ declare class RingBuffer<T> {
38
+ private buf;
39
+ private head;
40
+ private tail;
41
+ private size_;
42
+ private readonly maxCap;
43
+ constructor(initialCapacity?: number, maxCapacity?: number);
44
+ get length(): number;
45
+ get capacity(): number;
46
+ isEmpty(): boolean;
47
+ push(value: T): PushStatus;
48
+ shift(): T | undefined;
49
+ clear(): void;
50
+ private grow;
51
+ }
52
+
53
+ type EngineKind = "js" | "wasm";
54
+ type EngineStats<T> = {
55
+ engine: EngineKind;
56
+ data: T;
57
+ fallbackUsed: boolean;
58
+ };
59
+ type EngineSelectionMode = "auto" | EngineKind;
60
+ type EngineSelection<T> = EngineStats<T> & {
61
+ requested: EngineSelectionMode;
62
+ };
63
+ declare function engineStats<T>(engine: EngineKind, data: T, fallbackUsed?: boolean): EngineStats<T>;
64
+ declare function selectedEngineStats<T>(requested: EngineSelectionMode, engine: EngineKind, data: T): EngineSelection<T>;
65
+
66
+ type RingBufferStatsData = {
67
+ len: number;
68
+ capacity: number;
69
+ pushes: number;
70
+ shifts: number;
71
+ clears: number;
72
+ dropped: number;
73
+ };
74
+ type RingLike<T> = {
75
+ readonly length: number;
76
+ readonly capacity: number;
77
+ readonly engine: "js" | "wasm";
78
+ readonly fallbackUsed: boolean;
79
+ isEmpty(): boolean;
80
+ push(value: T): PushStatus;
81
+ shift(): T | undefined;
82
+ clear(): void;
83
+ stats(): EngineStats<RingBufferStatsData>;
84
+ };
85
+ type RingBufferEngine = "auto" | "js" | "wasm";
86
+ type RingBufferOptions = {
87
+ engine?: RingBufferEngine;
88
+ };
89
+ declare function makeBoundedRingBuffer<T>(initialCapacity: number, maxCapacity?: number, options?: RingBufferOptions): RingLike<T>;
90
+
91
+ type Task = () => void;
92
+ type SchedulerEngine = "auto" | "js" | "wasm";
93
+ type SchedulerStatsData = {
94
+ len: number;
95
+ capacity?: number;
96
+ phase?: string;
97
+ scheduledFlushes?: number;
98
+ completedFlushes?: number;
99
+ enqueuedTasks?: number;
100
+ executedTasks?: number;
101
+ droppedTasks?: number;
102
+ yieldedByBudget?: number;
103
+ };
104
+ type SchedulerStats = EngineStats<SchedulerStatsData>;
105
+ type SchedulerOptions = RingBufferOptions & {
106
+ engine?: SchedulerEngine;
107
+ initialCapacity?: number;
108
+ maxCapacity?: number;
109
+ flushBudget?: number;
110
+ microThreshold?: number;
111
+ };
112
+ declare class Scheduler {
113
+ private readonly engine;
114
+ private readonly js?;
115
+ private readonly wasm?;
116
+ private readonly flushBudget;
117
+ private readonly microThreshold;
118
+ private readonly fallbackUsed;
119
+ private readonly boundFlush;
120
+ constructor(options?: SchedulerOptions);
121
+ schedule(task: Task, tag?: string): void;
122
+ stats(): SchedulerStats;
123
+ private scheduleWasm;
124
+ private scheduleJs;
125
+ private requestFlush;
126
+ private flush;
127
+ private flushWasm;
128
+ private flushJs;
129
+ }
130
+ declare const globalScheduler: Scheduler;
131
+
132
+ type RuntimeEvent = {
133
+ type: "fiber.start";
134
+ fiberId: number;
135
+ parentFiberId?: number;
136
+ scopeId?: number;
137
+ name?: string;
138
+ } | {
139
+ type: "fiber.end";
140
+ fiberId: number;
141
+ status: "success" | "failure" | "interrupted";
142
+ error?: unknown;
143
+ } | {
144
+ type: "fiber.suspend";
145
+ fiberId: number;
146
+ reason?: string;
147
+ } | {
148
+ type: "fiber.resume";
149
+ fiberId: number;
150
+ } | {
151
+ type: "scope.open";
152
+ scopeId: number;
153
+ parentScopeId?: number;
154
+ } | {
155
+ type: "scope.close";
156
+ scopeId: number;
157
+ status: "success" | "failure" | "interrupted";
158
+ error?: unknown;
159
+ } | {
160
+ type: "log";
161
+ level: "debug" | "info" | "warn" | "error";
162
+ message: string;
163
+ fields?: Record<string, unknown>;
164
+ };
165
+ type RuntimeEmitContext = {
166
+ fiberId?: number;
167
+ scopeId?: number;
168
+ traceId?: string;
169
+ spanId?: string;
170
+ };
171
+ interface RuntimeHooks {
172
+ emit(ev: RuntimeEvent, ctx: RuntimeEmitContext): void;
173
+ }
174
+ type RuntimeEventRecord = RuntimeEvent & RuntimeEmitContext & {
175
+ seq: number;
176
+ wallTs: number;
177
+ ts: number;
178
+ /**
179
+ * Convenience fields for generic event consumers. They are present for
180
+ * log events and absent for fiber/scope events, but keeping them optional
181
+ * lets subscribers inspect records without narrowing the RuntimeEvent
182
+ * union first.
183
+ */
184
+ level?: "debug" | "info" | "warn" | "error";
185
+ message?: string;
186
+ fields?: Record<string, unknown>;
187
+ };
188
+
189
+ type FiberRunState = "Queued" | "Running" | "Suspended" | "Done";
190
+ type FiberInfo = {
191
+ fiberId: number;
192
+ parentFiberId?: number;
193
+ name?: string;
194
+ runState: FiberRunState;
195
+ status: "Running" | "Done" | "Interrupted";
196
+ createdAt: number;
197
+ lastActiveAt: number;
198
+ scopeId?: number;
199
+ traceId?: string;
200
+ spanId?: string;
201
+ awaiting?: {
202
+ reason: string;
203
+ detail?: string;
204
+ };
205
+ lastEnd?: {
206
+ status: string;
207
+ error?: string;
208
+ };
209
+ };
210
+ type ScopeInfo = {
211
+ scopeId: number;
212
+ parentScopeId?: number;
213
+ ownerFiberId?: number;
214
+ openAt: number;
215
+ closedAt?: number;
216
+ finalizers: Array<{
217
+ id: number;
218
+ label?: string;
219
+ status: "added" | "running" | "done";
220
+ }>;
221
+ };
222
+ declare class RuntimeRegistry implements RuntimeHooks {
223
+ fibers: Map<number, FiberInfo>;
224
+ scopes: Map<number, ScopeInfo>;
225
+ private seq;
226
+ private recent;
227
+ private recentCap;
228
+ emit(ev: RuntimeEvent, ctx: RuntimeEmitContext): void;
229
+ getRecentEvents(): RuntimeEventRecord[];
230
+ }
231
+
232
+ type HostActionKind = "http" | "db" | "queue" | "custom";
233
+ type HttpHostAction = {
234
+ readonly kind: "http";
235
+ readonly actionId?: string;
236
+ readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
237
+ readonly target: string;
238
+ readonly path?: string;
239
+ readonly headers?: Record<string, string>;
240
+ readonly body?: Uint8Array;
241
+ readonly timeoutMs?: number;
242
+ readonly responseLimitBytes?: number;
243
+ readonly idempotencyKey?: string;
244
+ };
245
+ type DbHostAction = {
246
+ readonly kind: "db";
247
+ readonly actionId?: string;
248
+ readonly operation: "get" | "put" | "query" | "delete";
249
+ readonly target: string;
250
+ readonly payload: Uint8Array;
251
+ readonly timeoutMs?: number;
252
+ readonly idempotencyKey?: string;
253
+ };
254
+ type QueueHostAction = {
255
+ readonly kind: "queue";
256
+ readonly actionId?: string;
257
+ readonly target: string;
258
+ readonly payload: Uint8Array;
259
+ readonly timeoutMs?: number;
260
+ readonly idempotencyKey?: string;
261
+ };
262
+ type CustomHostAction = {
263
+ readonly kind: "custom";
264
+ readonly actionId?: string;
265
+ readonly target: string;
266
+ readonly payload?: Uint8Array;
267
+ readonly timeoutMs?: number;
268
+ readonly idempotencyKey?: string;
269
+ };
270
+ type HostAction = HttpHostAction | DbHostAction | QueueHostAction | CustomHostAction;
271
+ type HostActionResult<A = unknown> = {
272
+ readonly kind: "ok";
273
+ readonly actionId?: string;
274
+ readonly value: A;
275
+ readonly metadata?: Record<string, unknown>;
276
+ } | {
277
+ readonly kind: "error";
278
+ readonly actionId?: string;
279
+ readonly error: unknown;
280
+ readonly metadata?: Record<string, unknown>;
281
+ };
282
+ type HostExecutionContext<R = unknown> = {
283
+ readonly fiberId: number;
284
+ readonly env: R;
285
+ readonly signal: AbortSignal;
286
+ readonly deadlineAt?: number;
287
+ };
288
+ interface HostExecutor<R = unknown> {
289
+ execute(action: HostAction, context: HostExecutionContext<R>): Promise<HostActionResult>;
290
+ }
291
+ declare const DefaultHostExecutor: HostExecutor<any>;
292
+
293
+ type NodeId = number;
294
+ type RefId = number;
295
+ type FiberId$1 = number;
296
+ type OpcodeNode = {
297
+ readonly tag: "Succeed";
298
+ readonly valueRef: RefId;
299
+ } | {
300
+ readonly tag: "Fail";
301
+ readonly errorRef: RefId;
302
+ } | {
303
+ readonly tag: "Sync";
304
+ readonly fnRef: RefId;
305
+ } | {
306
+ readonly tag: "Async";
307
+ readonly registerRef: RefId;
308
+ } | {
309
+ readonly tag: "FlatMap";
310
+ readonly first: NodeId;
311
+ readonly fnRef: RefId;
312
+ } | {
313
+ readonly tag: "Fold";
314
+ readonly first: NodeId;
315
+ readonly onFailureRef: RefId;
316
+ readonly onSuccessRef: RefId;
317
+ } | {
318
+ readonly tag: "Fork";
319
+ readonly effectRef: RefId;
320
+ readonly scopeId?: number;
321
+ } | {
322
+ readonly tag: "HostAction";
323
+ readonly actionRef: RefId;
324
+ readonly decodeRef?: RefId;
325
+ };
326
+ type OpcodeProgram = {
327
+ readonly version: 1;
328
+ readonly root: NodeId;
329
+ readonly nodes: OpcodeNode[];
330
+ };
331
+ type ProgramPatch = {
332
+ readonly root: NodeId;
333
+ readonly nodes: OpcodeNode[];
334
+ };
335
+ type SyncRef<R = unknown> = (env: R) => unknown;
336
+ type AsyncRegisterRef<R = unknown> = (env: R, cb: (exit: unknown) => void) => void | (() => void);
337
+ type FlatMapRef<R = unknown> = (value: unknown) => Async<R, unknown, unknown>;
338
+ type FoldFailureRef<R = unknown> = (error: unknown) => Async<R, unknown, unknown>;
339
+ type FoldSuccessRef<R = unknown> = (value: unknown) => Async<R, unknown, unknown>;
340
+ type DecodeRef = (result: HostActionResult) => unknown;
341
+ declare class HostRegistry {
342
+ private nextRef;
343
+ private readonly refs;
344
+ register<T>(value: T): RefId;
345
+ get<T>(ref: RefId): T;
346
+ set(ref: RefId, value: unknown): void;
347
+ delete(ref: RefId): void;
348
+ clear(): void;
349
+ size(): number;
350
+ }
351
+ type CompiledProgram = {
352
+ readonly program: OpcodeProgram;
353
+ readonly registry: HostRegistry;
354
+ };
355
+ declare class ProgramBuilder {
356
+ private readonly nodes;
357
+ private readonly registry;
358
+ compile(effect: Async<unknown, unknown, unknown>): CompiledProgram;
359
+ append(effect: Async<unknown, unknown, unknown>): ProgramPatch;
360
+ private add;
361
+ private visit;
362
+ }
363
+
364
+ type FiberEngineKind = "js" | "wasm" | "wasm-reference";
365
+ type RuntimeEngineMode = FiberEngineKind | "auto";
366
+ type FiberEngineStats = {
367
+ readonly engine: string;
368
+ readonly startedFibers: number;
369
+ readonly runningFibers: number;
370
+ readonly suspendedFibers: number;
371
+ readonly queuedFibers: number;
372
+ readonly completedFibers: number;
373
+ readonly failedFibers: number;
374
+ readonly interruptedFibers: number;
375
+ readonly pendingHostEffects: number;
376
+ readonly hostRegistryRefs?: number;
377
+ readonly wasm?: unknown;
378
+ readonly fiberRegistry?: unknown;
379
+ };
380
+ interface FiberEngine<R = unknown> {
381
+ readonly kind: FiberEngineKind;
382
+ fork<E, A>(effect: Async<R, E, A>, scopeId?: number): Fiber<E, A> & {
383
+ schedule?: (tag?: string) => void;
384
+ };
385
+ stats(): FiberEngineStats;
386
+ shutdown?(): Promise<void> | void;
387
+ }
388
+ type WasmEngineRuntime<R = unknown> = {
389
+ readonly env: R;
390
+ readonly hostExecutor: HostExecutor<R>;
391
+ readonly scheduler: {
392
+ schedule(task: () => void, label?: string): void;
393
+ };
394
+ readonly hooks: {
395
+ emit(ev: any, ctx: any): void;
396
+ };
397
+ fork<E, A>(effect: Async<R, E, A>, scopeId?: number): Fiber<E, A>;
398
+ };
399
+ type EngineEvent = {
400
+ readonly kind: "Continue";
401
+ readonly fiberId: FiberId$1;
402
+ } | {
403
+ readonly kind: "Done";
404
+ readonly fiberId: FiberId$1;
405
+ readonly valueRef: RefId;
406
+ } | {
407
+ readonly kind: "Failed";
408
+ readonly fiberId: FiberId$1;
409
+ readonly errorRef: RefId;
410
+ } | {
411
+ readonly kind: "Interrupted";
412
+ readonly fiberId: FiberId$1;
413
+ readonly reasonRef: RefId;
414
+ } | {
415
+ readonly kind: "InvokeSync";
416
+ readonly fiberId: FiberId$1;
417
+ readonly fnRef: RefId;
418
+ } | {
419
+ readonly kind: "InvokeAsync";
420
+ readonly fiberId: FiberId$1;
421
+ readonly registerRef: RefId;
422
+ } | {
423
+ readonly kind: "InvokeFlatMap";
424
+ readonly fiberId: FiberId$1;
425
+ readonly fnRef: RefId;
426
+ readonly valueRef: RefId;
427
+ } | {
428
+ readonly kind: "InvokeFoldFailure";
429
+ readonly fiberId: FiberId$1;
430
+ readonly fnRef: RefId;
431
+ readonly errorRef: RefId;
432
+ } | {
433
+ readonly kind: "InvokeFoldSuccess";
434
+ readonly fiberId: FiberId$1;
435
+ readonly fnRef: RefId;
436
+ readonly valueRef: RefId;
437
+ } | {
438
+ readonly kind: "InvokeFork";
439
+ readonly fiberId: FiberId$1;
440
+ readonly effectRef: RefId;
441
+ readonly scopeId?: number;
442
+ } | {
443
+ readonly kind: "InvokeHostAction";
444
+ readonly fiberId: FiberId$1;
445
+ readonly actionRef: RefId;
446
+ readonly decodeRef?: RefId;
447
+ };
448
+ interface WasmBridge {
449
+ readonly kind: "wasm" | "wasm-reference";
450
+ createFiber(program: OpcodeProgram): FiberId$1;
451
+ poll(fiberId: FiberId$1): EngineEvent;
452
+ provideValue(fiberId: FiberId$1, valueRef: RefId): EngineEvent;
453
+ provideError(fiberId: FiberId$1, errorRef: RefId): EngineEvent;
454
+ provideEffect(fiberId: FiberId$1, root: NodeId, nodes: OpcodeNode[]): EngineEvent;
455
+ interrupt(fiberId: FiberId$1, reasonRef: RefId): EngineEvent;
456
+ dropFiber(fiberId: FiberId$1): void;
457
+ stats(): unknown;
458
+ }
459
+ type Joiner<E, A> = (exit: Exit<E, A>) => void;
460
+
461
+ type WasmFiberEngineOptions = {
462
+ readonly bridge?: WasmBridge;
463
+ readonly modulePath?: string;
464
+ readonly reference?: boolean;
465
+ };
466
+ declare class WasmFiberEngine<R> implements FiberEngine<R> {
467
+ private readonly runtime;
468
+ readonly kind: FiberEngineKind;
469
+ private readonly bridge;
470
+ private startedFibers;
471
+ private runningFibers;
472
+ private suspendedFibers;
473
+ private completedFibers;
474
+ private failedFibers;
475
+ private interruptedFibers;
476
+ private pendingHostEffects;
477
+ private readonly states;
478
+ private readonly fiberRegistry?;
479
+ constructor(runtime: WasmEngineRuntime<R> & any, options?: WasmFiberEngineOptions);
480
+ fork<E, A>(effect: Async<R, E, A>, scopeId?: number): Fiber<E, A> & {
481
+ schedule?: (tag?: string) => void;
482
+ };
483
+ stats(): FiberEngineStats;
484
+ shutdown(): Promise<void>;
485
+ private scheduleWakeup;
486
+ private drainWakeups;
487
+ private driveById;
488
+ private drive;
489
+ private scheduleAsync;
490
+ private scheduleHostAction;
491
+ private resumeWithExit;
492
+ private resumeWithValue;
493
+ private resumeWithError;
494
+ private interruptById;
495
+ private interruptState;
496
+ private markSuspended;
497
+ private markRunning;
498
+ private completeSuccess;
499
+ private completeFailure;
500
+ private completeDie;
501
+ private completeInterrupted;
502
+ private cleanupState;
503
+ }
504
+
505
+ declare const NoopHooks: RuntimeHooks;
506
+ /**
507
+ * --- Runtime como objeto único (ZIO-style) ---
508
+ * Un valor que representa "cómo" se ejecutan los efectos: scheduler + environment + hooks.
509
+ */
510
+ type RuntimeOptions<R> = {
511
+ env: R;
512
+ scheduler?: Scheduler;
513
+ hooks?: RuntimeHooks;
514
+ /**
515
+ * Selects the fiber interpreter used by fork().
516
+ *
517
+ * - js: existing TypeScript RuntimeFiber interpreter.
518
+ * - wasm: wasm-pack backed interpreter from wasm/pkg.
519
+ * - wasm-reference: JS reference bridge with the same host/engine protocol.
520
+ * - auto: try wasm first and fall back to js when the wasm package is not loadable.
521
+ */
522
+ engine?: RuntimeEngineMode;
523
+ /** Executor used by HostAction opcodes when running on the WASM engine. */
524
+ hostExecutor?: HostExecutor<R>;
525
+ /** Optional low-level WASM bridge options, mostly for tests and local experiments. */
526
+ wasm?: WasmFiberEngineOptions;
527
+ };
528
+ declare class Runtime<R> {
529
+ readonly env: R;
530
+ readonly scheduler: Scheduler;
531
+ readonly hooks: RuntimeHooks;
532
+ readonly hostExecutor: HostExecutor<R>;
533
+ readonly engineMode: RuntimeEngineMode;
534
+ readonly wasmOptions?: WasmFiberEngineOptions;
535
+ readonly fiberEngine: FiberEngine<R>;
536
+ readonly fallbackUsed: boolean;
537
+ readonly forkPolicy: {
538
+ initChild(fiber: RuntimeFiber<any, any, any> & any, parent?: (RuntimeFiber<any, any, any> & any) | null, scopeId?: number): void;
539
+ };
540
+ registry?: RuntimeRegistry;
541
+ constructor(args: RuntimeOptions<R>);
542
+ private makeFiberEngine;
543
+ /** Returns true when the runtime has real hooks (not the no-op singleton). */
544
+ hasActiveHooks(): boolean;
545
+ /** Deriva un runtime con env extendido (estilo provide/locally) */
546
+ provide<R2>(env: R2): Runtime<R & R2>;
547
+ emit(ev: RuntimeEvent): void;
548
+ /**
549
+ * ✅ CAMBIO: fork(effect, scopeId?) y pasa scopeId a forkPolicy
550
+ */
551
+ fork<E, A>(effect: Async<R, E, A>, scopeId?: number): Fiber<E, A>;
552
+ stats(): EngineStats<ReturnType<FiberEngine<R>["stats"]>>;
553
+ capabilities(): RuntimeCapabilities;
554
+ shutdown(): Promise<void> | void;
555
+ unsafeRunAsync<E, A>(effect: Async<R, E, A>, cb: (exit: Exit<E, A>) => void): void;
556
+ toPromise<E, A>(effect: Async<R, E, A>): Promise<A>;
557
+ unsafeRun<E, A>(effect: Async<R, E, A>): void;
558
+ delay<E, A>(ms: number, eff: Async<R, E, A>): Async<R, E, A>;
559
+ static make<R>(env: R, scheduler?: Scheduler): Runtime<R>;
560
+ static makeWithEngine<R>(env: R, engine: RuntimeEngineMode, options?: Omit<RuntimeOptions<R>, "env" | "engine">): Runtime<R>;
561
+ /** Convenience logger: emits a RuntimeEvent of type "log". */
562
+ log(level: "debug" | "info" | "warn" | "error", message: string, fields?: Record<string, unknown>): void;
563
+ }
564
+ /** Create a runtime from `env` and fork the given effect. */
565
+ declare function fork<R, E, A>(effect: Async<R, E, A>, env?: R): Fiber<E, A>;
566
+ /** Run an effect with `env` and invoke `cb` with the final Exit. */
567
+ declare function unsafeRunAsync<R, E, A>(effect: Async<R, E, A>, env: R | undefined, cb: (exit: Exit<E, A>) => void): void;
568
+ /** Run an effect with `env` and return a Promise of its success value. */
569
+ declare function toPromise<R, E, A>(effect: Async<R, E, A>, env?: R): Promise<A>;
570
+ /**
571
+ * Create an Async from an abortable Promise.
572
+ * Type params are ordered as `<E, A, R = unknown>` to match call-sites.
573
+ */
574
+ declare function fromPromiseAbortable<E, A, R = unknown>(make: (signal: AbortSignal, env: R) => Promise<A>, onReject: (u: unknown) => E): Async<R, E, A>;
575
+ declare function unsafeRunFoldWithEnv<R, E, A>(eff: Async<R, E, A>, env: R, onFailure: (cause: Cause<E>) => void, onSuccess: (value: A) => void): void;
576
+
577
+ type JSONValue = null | boolean | number | string | JSONValue[] | {
578
+ [k: string]: JSONValue;
579
+ };
580
+ type ContextNode = {
581
+ parent: ContextNode | null;
582
+ patch: Record<string, JSONValue>;
583
+ };
584
+ type TraceContext = {
585
+ traceId: string;
586
+ spanId: string;
587
+ parentSpanId?: string;
588
+ sampled?: boolean;
589
+ };
590
+ type FiberContext = {
591
+ log: ContextNode;
592
+ trace: TraceContext | null;
593
+ };
594
+
595
+ type FiberId = number;
596
+ type FiberStatus = "Running" | "Done" | "Interrupted";
597
+ type Interrupted = {
598
+ readonly _tag: "Interrupt";
599
+ };
600
+ type Fiber<E, A> = {
601
+ readonly id: FiberId;
602
+ readonly status: () => FiberStatus;
603
+ readonly join: (cb: (exit: Exit<E, A>) => void) => void;
604
+ readonly interrupt: () => void;
605
+ readonly addFinalizer: (f: (exit: Exit<E, A>) => void) => void;
606
+ };
607
+ declare function setBenchmarkBudget(budget: number | undefined): void;
608
+ declare function getBenchmarkBudget(): number | undefined;
609
+ declare class RuntimeFiber<R, E, A> implements Fiber<E, A> {
610
+ readonly id: FiberId;
611
+ readonly runtime: Runtime<R>;
612
+ private closing;
613
+ private finishing;
614
+ private runState;
615
+ private interrupted;
616
+ private result;
617
+ private readonly joiners;
618
+ private current;
619
+ private readonly stack;
620
+ private readonly fiberFinalizers;
621
+ private finalizersDrained;
622
+ fiberContext: FiberContext;
623
+ name?: string;
624
+ scopeId?: number;
625
+ /**
626
+ * Cached closure for the scheduler callback — avoids creating a new
627
+ * closure on every `schedule()` call. The tag parameter used by the
628
+ * scheduler is only part of the label string, not the callback logic,
629
+ * so a single cached closure is sufficient.
630
+ */
631
+ private readonly boundStep;
632
+ constructor(runtime: Runtime<R>, effect: Async<R, E, A>);
633
+ private get env();
634
+ private get scheduler();
635
+ private emit;
636
+ addFinalizer(f: (exit: Exit<E, A>) => void): void;
637
+ status(): FiberStatus;
638
+ join(cb: (exit: Exit<E, A>) => void): void;
639
+ interrupt(): void;
640
+ schedule(tag?: string): void;
641
+ private runFinalizersOnce;
642
+ private notify;
643
+ private onSuccess;
644
+ private onFailure;
645
+ private budget;
646
+ private step;
647
+ }
648
+ declare function getCurrentFiber(): RuntimeFiber<any, any, any> | null;
649
+ /**
650
+ * Unsafe (but convenient) access to the runtime that is currently executing.
651
+ * Throws if called outside of a running fiber.
652
+ */
653
+ declare function unsafeGetCurrentRuntime<R>(): Runtime<R>;
654
+ declare function withCurrentFiber<T>(fiber: RuntimeFiber<any, any, any>, f: () => T): T;
655
+
656
+ type ScopeId = number;
657
+ type CloseOptions = {
658
+ awaitChildren?: boolean;
659
+ };
660
+ declare class Scope<R> {
661
+ private readonly runtime;
662
+ private readonly parentScopeId?;
663
+ readonly id: ScopeId;
664
+ private closed;
665
+ private readonly children;
666
+ private readonly subScopes;
667
+ private readonly finalizers;
668
+ constructor(runtime: Runtime<R>, parentScopeId?: ScopeId | undefined);
669
+ /** registra un finalizer (LIFO) */
670
+ addFinalizer(f: (exit: Exit<any, any>) => Async<R, any, any>): void;
671
+ /** crea un sub scope (mismo runtime) */
672
+ subScope(): Scope<R>;
673
+ /** ✅ fork en este scope */
674
+ fork<E, A>(eff: Async<R, E, A>): Fiber<E, A>;
675
+ /** close fire-and-forget (no bloquea) */
676
+ close(exit?: Exit<any, any>): void;
677
+ /** Emit the scope.close event if hooks are active. */
678
+ private emitCloseEvent;
679
+ /**
680
+ * Build an effect that executes finalizers in LIFO order.
681
+ *
682
+ * Optimization over the original: instead of wrapping every finalizer in
683
+ * `asyncFold(fin(exit), () => unit(), () => unit())` which creates 3 effect
684
+ * nodes per finalizer (Fold + 2 Succeed), we use a single Sync thunk per
685
+ * finalizer that catches errors inline. When the finalizer returns a
686
+ * Succeed effect (like `unit()`), the Sync thunk completes without creating
687
+ * additional effect nodes.
688
+ */
689
+ private buildFinalizerEffect;
690
+ closeAsync(exit?: Exit<any, any>, opts?: CloseOptions): Async<R, any, void>;
691
+ }
692
+ declare function withScopeAsync<R, E, A>(runtime: Runtime<R>, f: (scope: Scope<R>) => Async<R, E, A>): Async<R, E, A>;
693
+
694
+ declare function withScope<R>(runtime: Runtime<R>, f: (scope: Scope<R>) => void): Async<R, never, void>;
695
+ declare function withScope<R, E, A>(runtime: Runtime<R>, f: (scope: Scope<R>) => Async<R, E, A>): Async<R, E, A>;
696
+
697
+ type Async<R, E, A> = {
698
+ readonly _tag: "Succeed";
699
+ readonly value: A;
700
+ } | {
701
+ readonly _tag: "Fail";
702
+ readonly error: E;
703
+ } | {
704
+ readonly _tag: "Sync";
705
+ readonly thunk: (env: R) => A;
706
+ } | {
707
+ readonly _tag: "Async";
708
+ readonly register: (env: R, cb: (exit: Exit<E, A>) => void) => void | (() => void);
709
+ } | {
710
+ readonly _tag: "FlatMap";
711
+ readonly first: Async<R, E, any>;
712
+ readonly andThen: (a: any) => Async<R, E, A>;
713
+ } | {
714
+ readonly _tag: "Fold";
715
+ readonly first: Async<R, E, any>;
716
+ readonly onFailure: (e: any) => Async<R, E, A>;
717
+ readonly onSuccess: (a: any) => Async<R, E, A>;
718
+ } | {
719
+ readonly _tag: "Fork";
720
+ readonly effect: Async<R, E, any>;
721
+ readonly scopeId?: number;
722
+ };
723
+ declare const Async: {
724
+ succeed: <R, E, A>(value: A) => Async<R, E, A>;
725
+ fail: <R, E, A = never>(error: E) => Async<R, E, A>;
726
+ sync: <R, E, A>(thunk: (env: R) => A) => Async<R, E, A>;
727
+ async: <R, E, A>(register: (env: R, cb: (exit: Exit<E, A>) => void) => void | (() => void)) => Async<R, E, A>;
728
+ };
729
+ declare function asyncFold<R, E, A, B>(fa: Async<R, E, A>, onFailure: (e: E) => Async<R, E, B>, onSuccess: (a: A) => Async<R, E, B>): Async<R, E, B>;
730
+ declare function asyncCatchAll<R, E, A, R2, E2, B>(fa: Async<R, E, A>, handler: (e: E) => Async<R2, E2, B>): Async<R & R2, E2, A | B>;
731
+ declare function asyncMapError<R, E, E2, A>(fa: Async<R, E, A>, f: (e: E) => E2): Async<R, E2, A>;
732
+ declare const unit: <R>() => Async<R, never, void>;
733
+ declare const asyncSucceed: <A>(value: A) => Async<unknown, never, A>;
734
+ declare const asyncFail: <E>(error: E) => Async<unknown, E, never>;
735
+ declare const asyncSync: <R, A>(thunk: (env: R) => A) => Async<R, unknown, A>;
736
+ declare const asyncTotal: <A>(thunk: () => A) => Async<unknown, unknown, A>;
737
+ declare const async: <R, E, A>(register: (env: R, cb: (exit: Exit<E, A>) => void) => void | Canceler) => Async<R, E, A>;
738
+ declare function asyncMap<R, E, A, B>(fa: Async<R, E, A>, f: (a: A) => B): Async<R, E, B>;
739
+ declare function asyncFlatMap<R, E, A, B>(fa: Async<R, E, A>, f: (a: A) => Async<R, E, B>): Async<R, E, B>;
740
+ declare function acquireRelease<R, E, A>(acquire: Async<R, E, A>, release: (res: A, exit: Exit<E, any>) => Async<R, any, any>, scope: Scope<R>): Async<R, E, A>;
741
+ declare function asyncInterruptible<R, E, A>(register: (env: R, cb: (exit: Exit<E, A>) => void) => void | Canceler): Async<R, E, A>;
742
+ type AsyncWithPromise<R, E, A> = Async<R, E, A> & {
743
+ toPromise: (env: R) => Promise<A>;
744
+ unsafeRunPromise: () => Promise<A>;
745
+ };
746
+ declare const withAsyncPromise: <R, E, A>(run: (eff: Async<R, E, A>, env: R) => Promise<A>) => (eff: Async<R, E, A>) => AsyncWithPromise<R, E, A>;
747
+ declare const mapAsync: <R, E, A, B>(fa: Async<R, E, A>, f: (a: A) => B) => Async<R, E, B>;
748
+ declare const mapTryAsync: <R, E, A, B>(fa: Async<R, E, A>, f: (a: A) => B) => Async<R, E, B>;
749
+
750
+ type None = {
751
+ readonly _tag: "None";
752
+ };
753
+ type Some<A> = {
754
+ readonly _tag: "Some";
755
+ readonly value: A;
756
+ };
757
+ type Option<A> = None | Some<A>;
758
+ declare const none: Option<never>;
759
+ declare const some: <A>(value: A) => Option<A>;
760
+
761
+ type Cause<E> = {
762
+ readonly _tag: "Fail";
763
+ readonly error: E;
764
+ } | {
765
+ readonly _tag: "Interrupt";
766
+ } | {
767
+ readonly _tag: "Die";
768
+ readonly defect: unknown;
769
+ };
770
+ declare const Cause: {
771
+ fail: <E>(error: E) => Cause<E>;
772
+ interrupt: <E = never>() => Cause<E>;
773
+ die: <E = never>(defect: unknown) => Cause<E>;
774
+ };
775
+ type Exit<E, A> = {
776
+ _tag: "Success";
777
+ value: A;
778
+ } | {
779
+ _tag: "Failure";
780
+ cause: Cause<E>;
781
+ };
782
+ declare const Exit: {
783
+ succeed: <E = never, A = never>(value: A) => Exit<E, A>;
784
+ failCause: <E = never, A = never>(cause: Cause<E>) => Exit<E, A>;
785
+ };
786
+ type ZIO<R, E, A> = Async<R, E, A>;
787
+ declare const succeed: <A>(value: A) => ZIO<unknown, never, A>;
788
+ declare const fail: <E>(error: E) => ZIO<unknown, E, never>;
789
+ declare const sync: <R, A>(thunk: (env: R) => A) => ZIO<R, unknown, A>;
790
+ declare const map: <R, E, A, B>(fa: ZIO<R, E, A>, f: (a: A) => B) => Async<R, E, B>;
791
+ declare const flatMap: <R, E, A, R2, E2, B>(fa: ZIO<R, E, A>, f: (a: A) => ZIO<R2, E2, B>) => ZIO<R & R2, E | E2, B>;
792
+ declare const mapError: <R, E, E2, A>(fa: ZIO<R, E, A>, f: (e: E) => E2) => any;
793
+ declare const catchAll: <R, E, A, R2, E2, B>(fa: ZIO<R, E, A>, handler: (e: E) => ZIO<R2, E2, B>) => ZIO<R & R2, E2, A | B>;
794
+ declare function orElseOptional<R, E, A, R2, A2>(fa: ZIO<R, Option<E>, A>, that: () => ZIO<R2, Option<E>, A2>): ZIO<R & R2, Option<E>, A | A2>;
795
+ declare const end: <E>() => ZIO<unknown, Option<E>, never>;
796
+
797
+ export { type QueueHostAction as $, Async as A, type FoldFailureRef as B, type CancelToken as C, type DbHostAction as D, Exit as E, type FiberEngine as F, type FoldSuccessRef as G, type HostAction as H, type HostActionKind as I, type HostActionResult as J, type HostExecutionContext as K, type HostExecutor as L, HostRegistry as M, type NodeId as N, type Option as O, type HttpHostAction as P, type Interrupted as Q, RuntimeFiber as R, Scope as S, type Joiner as T, type None as U, NoopHooks as V, type WasmEngineRuntime as W, ProgramBuilder as X, type ProgramPatch as Y, type ZIO as Z, PushStatus as _, type AsyncWithPromise as a, withScopeAsync as a$, RingBuffer as a0, type RingBufferEngine as a1, type RingBufferStatsData as a2, Runtime as a3, type RuntimeCapabilities as a4, type RuntimeEngineMode as a5, type RuntimeOptions as a6, Scheduler as a7, type SchedulerEngine as a8, type SchedulerOptions as a9, fromPromiseAbortable as aA, getBenchmarkBudget as aB, getCurrentFiber as aC, globalScheduler as aD, linkAbortController as aE, makeBoundedRingBuffer as aF, makeCancelToken as aG, map as aH, mapAsync as aI, mapError as aJ, mapTryAsync as aK, none as aL, orElseOptional as aM, runtimeCapabilities as aN, selectedEngineStats as aO, setBenchmarkBudget as aP, some as aQ, succeed as aR, sync as aS, toPromise as aT, unit as aU, unsafeGetCurrentRuntime as aV, unsafeRunAsync as aW, unsafeRunFoldWithEnv as aX, withAsyncPromise as aY, withCurrentFiber as aZ, withScope as a_, type SchedulerStats as aa, type SchedulerStatsData as ab, type ScopeId as ac, type Some as ad, type SyncRef as ae, type Task as af, WasmFiberEngine as ag, type WasmFiberEngineOptions as ah, acquireRelease as ai, async as aj, asyncCatchAll as ak, asyncFail as al, asyncFlatMap as am, asyncFold as an, asyncInterruptible as ao, asyncMap as ap, asyncMapError as aq, asyncSucceed as ar, asyncSync as as, asyncTotal as at, catchAll as au, end as av, engineStats as aw, fail as ax, flatMap as ay, fork as az, type FiberEngineStats as b, type Fiber as c, type FiberId as d, type FiberStatus as e, type RuntimeEvent as f, type WasmBridge as g, type OpcodeProgram as h, type FiberId$1 as i, type EngineEvent as j, type RefId as k, type OpcodeNode as l, type RingBufferOptions as m, type EngineStats as n, type AsyncRegisterRef as o, type RingLike as p, type Canceler as q, Cause as r, type CustomHostAction as s, type DecodeRef as t, DefaultHostExecutor as u, type EngineKind as v, type EngineSelection as w, type EngineSelectionMode as x, type FiberEngineKind as y, type FlatMapRef as z };