brass-runtime 1.11.0 → 1.12.0

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.
@@ -63,17 +63,64 @@ type RuntimeEvent = {
63
63
  message: string;
64
64
  fields?: Record<string, unknown>;
65
65
  };
66
+ type RuntimeEmitContext = {
67
+ fiberId?: number;
68
+ scopeId?: number;
69
+ traceId?: string;
70
+ spanId?: string;
71
+ };
66
72
  interface RuntimeHooks {
67
73
  emit(ev: RuntimeEvent, ctx: RuntimeEmitContext): void;
68
74
  }
69
- type RuntimeEmitContext = {
70
- fiberId?: number;
75
+ type RuntimeEventRecord = RuntimeEvent & RuntimeEmitContext & {
76
+ seq: number;
77
+ wallTs: number;
78
+ ts: number;
79
+ };
80
+
81
+ type FiberRunState = "Queued" | "Running" | "Suspended" | "Done";
82
+ type FiberInfo = {
83
+ fiberId: number;
84
+ parentFiberId?: number;
85
+ name?: string;
86
+ runState: FiberRunState;
87
+ status: "Running" | "Done" | "Interrupted";
88
+ createdAt: number;
89
+ lastActiveAt: number;
71
90
  scopeId?: number;
72
91
  traceId?: string;
73
92
  spanId?: string;
93
+ awaiting?: {
94
+ reason: string;
95
+ detail?: string;
96
+ };
97
+ lastEnd?: {
98
+ status: string;
99
+ error?: string;
100
+ };
101
+ };
102
+ type ScopeInfo = {
103
+ scopeId: number;
104
+ parentScopeId?: number;
105
+ ownerFiberId?: number;
106
+ openAt: number;
107
+ closedAt?: number;
108
+ finalizers: Array<{
109
+ id: number;
110
+ label?: string;
111
+ status: "added" | "running" | "done";
112
+ }>;
74
113
  };
114
+ declare class RuntimeRegistry implements RuntimeHooks {
115
+ fibers: Map<number, FiberInfo>;
116
+ scopes: Map<number, ScopeInfo>;
117
+ private seq;
118
+ private recent;
119
+ private recentCap;
120
+ emit(ev: RuntimeEvent, ctx: RuntimeEmitContext): void;
121
+ getRecentEvents(): RuntimeEventRecord[];
122
+ }
75
123
 
76
- type NodeCallback<A> = (err: Error | null, result: A) => void;
77
124
  /**
78
125
  * --- Runtime como objeto único (ZIO-style) ---
79
126
  * Un valor que representa "cómo" se ejecutan los efectos: scheduler + environment + hooks.
@@ -83,48 +130,41 @@ declare class Runtime<R> {
83
130
  readonly scheduler: Scheduler;
84
131
  readonly hooks: RuntimeHooks;
85
132
  readonly forkPolicy: {
86
- initChild(fiber: RuntimeFiber<any, any, any> & any, parent?: (RuntimeFiber<any, any, any> & any) | null): void;
133
+ initChild(fiber: RuntimeFiber<any, any, any> & any, parent?: (RuntimeFiber<any, any, any> & any) | null, scopeId?: number): void;
87
134
  };
135
+ registry?: RuntimeRegistry;
88
136
  constructor(args: {
89
137
  env: R;
90
138
  scheduler?: Scheduler;
91
139
  hooks?: RuntimeHooks;
92
140
  });
93
- fork<E, A>(effect: Async<R, E, A>): Fiber<E, A>;
94
- unsafeRunAsync<E, A>(effect: Async<R, E, A>, cb: (exit: Exit<E | Interrupted, A>) => void): void;
95
- toPromise<E, A>(effect: Async<R, E, A>): Promise<A>;
96
- log(level: "debug" | "info" | "warn" | "error", message: string, fields?: Record<string, unknown>): void;
97
- withHooks(hooks: RuntimeHooks): Runtime<R>;
141
+ /** Deriva un runtime con env extendido (estilo provide/locally) */
142
+ provide<R2>(env: R2): Runtime<R & R2>;
98
143
  emit(ev: RuntimeEvent): void;
144
+ /**
145
+ * ✅ CAMBIO: fork(effect, scopeId?) y pasa scopeId a forkPolicy
146
+ */
147
+ fork<E, A>(effect: Async<R, E, A>, scopeId?: number): Fiber<E, A>;
148
+ unsafeRunAsync<E, A>(effect: Async<R, E, A>, cb: (exit: Exit<E, A>) => void): void;
149
+ toPromise<E, A>(effect: Async<R, E, A>): Promise<A>;
150
+ unsafeRun<E, A>(effect: Async<R, E, A>): void;
151
+ delay<E, A>(ms: number, eff: Async<R, E, A>): Async<R, E, A>;
99
152
  static make<R>(env: R, scheduler?: Scheduler): Runtime<R>;
153
+ /** Convenience logger: emits a RuntimeEvent of type "log". */
154
+ log(level: "debug" | "info" | "warn" | "error", message: string, fields?: Record<string, unknown>): void;
100
155
  }
156
+ /** Create a runtime from `env` and fork the given effect. */
157
+ declare function fork<R, E, A>(effect: Async<R, E, A>, env?: R): Fiber<E, A>;
158
+ /** Run an effect with `env` and invoke `cb` with the final Exit. */
159
+ declare function unsafeRunAsync<R, E, A>(effect: Async<R, E, A>, env: R | undefined, cb: (exit: Exit<E, A>) => void): void;
160
+ /** Run an effect with `env` and return a Promise of its success value. */
161
+ declare function toPromise<R, E, A>(effect: Async<R, E, A>, env?: R): Promise<A>;
101
162
  /**
102
- * ---------------------------------------------------------------------------
103
- * Helpers existentes (los dejo igual) + wrappers que usan Runtime por debajo
104
- * ---------------------------------------------------------------------------
163
+ * Create an Async from an abortable Promise.
164
+ * Type params are ordered as `<E, A, R = unknown>` to match call-sites.
105
165
  */
106
- declare function from<A>(f: (cb: NodeCallback<A>) => void): Async<{}, Error, A>;
107
- declare function from<A>(thunk: () => Promise<A>): Async<{}, Error, A>;
108
- type BrassError = {
109
- _tag: "Abort";
110
- } | {
111
- _tag: "PromiseRejected";
112
- reason: unknown;
113
- };
114
- /**
115
- * --- Wrappers legacy (mantienen tu API actual) ---
116
- * Internamente crean un Runtime ad-hoc con env + scheduler.
117
- */
118
- declare function fork<R, E, A>(effect: Async<R, E, A>, env: R, scheduler?: Scheduler): Fiber<E, A>;
119
- declare function unsafeRunAsync<R, E, A>(effect: Async<R, E, A>, env: R, cb: (exit: Exit<E | Interrupted, A>) => void): void;
120
- declare function toPromise<R, E, A>(eff: Async<R, E, A>, env: R): Promise<A>;
121
- declare function fromPromise<R, E, A>(thunk: (env: R) => Promise<A>, onError: (e: unknown) => E): Async<R, E, A>;
122
- declare function fromCallback<A>(f: (cb: NodeCallback<A>) => void): Async<{}, Error, A>;
123
- declare function tryPromiseAbortable<A>(thunk: (signal: AbortSignal) => Promise<A>): Async<unknown, BrassError, A>;
124
- declare function tryPromiseAbortable<R, A>(thunk: (env: R, signal: AbortSignal) => Promise<A>): Async<R, BrassError, A>;
125
- declare function fromPromiseAbortable<E, A>(thunk: (signal: AbortSignal) => Promise<A>, onError: (e: unknown) => E): Async<unknown, E, A>;
126
- declare function fromPromiseAbortable<R, E, A>(thunk: (env: R, signal: AbortSignal) => Promise<A>, onError: (e: unknown) => E): Async<R, E, A>;
127
- declare function getCurrentRuntime<R>(): Runtime<R>;
166
+ declare function fromPromiseAbortable<E, A, R = unknown>(make: (signal: AbortSignal, env: R) => Promise<A>, onReject: (u: unknown) => E): Async<R, E, A>;
167
+ declare function unsafeRunFoldWithEnv<R, E, A>(eff: Async<R, E, A>, env: R, onFailure: (cause: Cause<E>) => void, onSuccess: (value: A) => void): void;
128
168
 
129
169
  type JSONValue = null | boolean | number | string | JSONValue[] | {
130
170
  [k: string]: JSONValue;
@@ -145,17 +185,16 @@ type FiberContext = {
145
185
  };
146
186
 
147
187
  type FiberId = number;
188
+ type FiberStatus = "Running" | "Done" | "Interrupted";
148
189
  type Interrupted = {
149
- readonly _tag: "Interrupted";
190
+ readonly _tag: "Interrupt";
150
191
  };
151
- type FiberStatus = "Running" | "Done" | "Interrupted";
152
- type StepDecision = "Continue" | "Suspend" | "Done";
153
192
  type Fiber<E, A> = {
154
193
  readonly id: FiberId;
155
194
  readonly status: () => FiberStatus;
156
- readonly join: (cb: (exit: Exit<E | Interrupted, A>) => void) => void;
195
+ readonly join: (cb: (exit: Exit<E, A>) => void) => void;
157
196
  readonly interrupt: () => void;
158
- readonly addFinalizer: (f: (exit: Exit<E | Interrupted, A>) => void) => void;
197
+ readonly addFinalizer: (f: (exit: Exit<E, A>) => void) => void;
159
198
  };
160
199
  declare class RuntimeFiber<R, E, A> implements Fiber<E, A> {
161
200
  readonly id: FiberId;
@@ -176,18 +215,25 @@ declare class RuntimeFiber<R, E, A> implements Fiber<E, A> {
176
215
  constructor(runtime: Runtime<R>, effect: Async<R, E, A>);
177
216
  private get env();
178
217
  private get scheduler();
179
- addFinalizer(f: (exit: Exit<E | Interrupted, A>) => void): void;
218
+ private emit;
219
+ addFinalizer(f: (exit: Exit<E, A>) => void): void;
180
220
  status(): FiberStatus;
181
- join(cb: (exit: Exit<E | Interrupted, A>) => void): void;
221
+ join(cb: (exit: Exit<E, A>) => void): void;
182
222
  interrupt(): void;
183
223
  schedule(tag?: string): void;
184
224
  private runFinalizersOnce;
185
225
  private notify;
186
226
  private onSuccess;
187
227
  private onFailure;
188
- step(): StepDecision;
228
+ private budget;
229
+ private step;
189
230
  }
190
231
  declare function getCurrentFiber(): RuntimeFiber<any, any, any> | null;
232
+ /**
233
+ * Unsafe (but convenient) access to the runtime that is currently executing.
234
+ * Throws if called outside of a running fiber.
235
+ */
236
+ declare function unsafeGetCurrentRuntime<R>(): Runtime<R>;
191
237
  declare function withCurrentFiber<T>(fiber: RuntimeFiber<any, any, any>, f: () => T): T;
192
238
 
193
239
  type ScopeId = number;
@@ -196,55 +242,59 @@ type CloseOptions = {
196
242
  };
197
243
  declare class Scope<R> {
198
244
  private readonly runtime;
245
+ private readonly parentScopeId?;
199
246
  readonly id: ScopeId;
200
247
  private closed;
201
248
  private readonly children;
202
249
  private readonly subScopes;
203
250
  private readonly finalizers;
204
- constructor(runtime: Runtime<R>);
205
- /** Acceso al env del runtime (conserva tu modelo actual) */
206
- private get env();
251
+ constructor(runtime: Runtime<R>, parentScopeId?: ScopeId | undefined);
207
252
  /** registra un finalizer (LIFO) */
208
253
  addFinalizer(f: (exit: Exit<any, any>) => Async<R, any, any>): void;
209
254
  /** crea un sub scope (mismo runtime) */
210
255
  subScope(): Scope<R>;
211
- /** fork en este scope */
212
- fork<E, A>(eff: Async<R, E, A>): Fiber<E | Interrupted, A>;
256
+ /** fork en este scope */
257
+ fork<E, A>(eff: Async<R, E, A>): Fiber<E, A>;
213
258
  /** close fire-and-forget (no bloquea) */
214
259
  close(exit?: Exit<any, any>): void;
215
- closeAsync(exit?: Exit<any, any>, options?: CloseOptions): Async<R, never, void>;
260
+ closeAsync(exit?: Exit<any, any>, opts?: CloseOptions): Async<R, any, void>;
216
261
  }
217
- /**
218
- * Ejecuta una función dentro de un scope estructurado (sync).
219
- * NOTA: En sync no tenés env real; por eso recibimos Runtime explícito.
220
- */
221
- declare function withScope<R, A>(runtime: Runtime<R>, body: (scope: Scope<R>) => A): A;
222
- /**
223
- * Versión async: crea scope, corre el efecto, cierra el scope con el Exit.
224
- */
225
- declare function withScopeAsync<R, E, A>(runtime: Runtime<R>, use: (scope: Scope<R>) => Async<R, E, A>): Async<R, E | Interrupted, A>;
262
+ declare function withScopeAsync<R, E, A>(runtime: Runtime<R>, f: (scope: Scope<R>) => Async<R, E, A>): Async<R, E, A>;
263
+
264
+ declare function withScope<R>(runtime: Runtime<R>, f: (scope: Scope<R>) => void): Async<R, never, void>;
265
+ declare function withScope<R, E, A>(runtime: Runtime<R>, f: (scope: Scope<R>) => Async<R, E, A>): Async<R, E, A>;
226
266
 
227
267
  type Async<R, E, A> = {
228
- _tag: "Succeed";
229
- value: A;
268
+ readonly _tag: "Succeed";
269
+ readonly value: A;
230
270
  } | {
231
- _tag: "Fail";
232
- error: E;
271
+ readonly _tag: "Fail";
272
+ readonly error: E;
273
+ } | {
274
+ readonly _tag: "Sync";
275
+ readonly thunk: (env: R) => A;
233
276
  } | {
234
- _tag: "Sync";
235
- thunk: (env: R) => A;
277
+ readonly _tag: "Async";
278
+ readonly register: (env: R, cb: (exit: Exit<E, A>) => void) => void | (() => void);
236
279
  } | {
237
- _tag: "Async";
238
- register: (env: R, cb: (exit: Exit<E, A>) => void) => void | Canceler;
280
+ readonly _tag: "FlatMap";
281
+ readonly first: Async<R, E, any>;
282
+ readonly andThen: (a: any) => Async<R, E, A>;
239
283
  } | {
240
- _tag: "FlatMap";
241
- first: Async<R, E, any>;
242
- andThen: (a: any) => Async<R, E, A>;
284
+ readonly _tag: "Fold";
285
+ readonly first: Async<R, E, any>;
286
+ readonly onFailure: (e: any) => Async<R, E, A>;
287
+ readonly onSuccess: (a: any) => Async<R, E, A>;
243
288
  } | {
244
- _tag: "Fold";
245
- first: Async<R, E, any>;
246
- onFailure: (e: any) => Async<R, E, A>;
247
- onSuccess: (a: any) => Async<R, E, A>;
289
+ readonly _tag: "Fork";
290
+ readonly effect: Async<R, E, any>;
291
+ readonly scopeId?: number;
292
+ };
293
+ declare const Async: {
294
+ succeed: <R, E, A>(value: A) => Async<R, E, A>;
295
+ fail: <R, E, A = never>(error: E) => Async<R, E, A>;
296
+ sync: <R, E, A>(thunk: (env: R) => A) => Async<R, E, A>;
297
+ async: <R, E, A>(register: (env: R, cb: (exit: Exit<E, A>) => void) => void | (() => void)) => Async<R, E, A>;
248
298
  };
249
299
  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>;
250
300
  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>;
@@ -278,12 +328,30 @@ type Option<A> = None | Some<A>;
278
328
  declare const none: Option<never>;
279
329
  declare const some: <A>(value: A) => Option<A>;
280
330
 
331
+ type Cause<E> = {
332
+ readonly _tag: "Fail";
333
+ readonly error: E;
334
+ } | {
335
+ readonly _tag: "Interrupt";
336
+ } | {
337
+ readonly _tag: "Die";
338
+ readonly defect: unknown;
339
+ };
340
+ declare const Cause: {
341
+ fail: <E>(error: E) => Cause<E>;
342
+ interrupt: <E = never>() => Cause<E>;
343
+ die: <E = never>(defect: unknown) => Cause<E>;
344
+ };
281
345
  type Exit<E, A> = {
282
- readonly _tag: "Success";
283
- readonly value: A;
346
+ _tag: "Success";
347
+ value: A;
284
348
  } | {
285
- readonly _tag: "Failure";
286
- readonly error: E;
349
+ _tag: "Failure";
350
+ cause: Cause<E>;
351
+ };
352
+ declare const Exit: {
353
+ succeed: <E = never, A = never>(value: A) => Exit<E, A>;
354
+ failCause: <E = never, A = never>(cause: Cause<E>) => Exit<E, A>;
287
355
  };
288
356
  type ZIO<R, E, A> = Async<R, E, A>;
289
357
  declare const succeed: <A>(value: A) => ZIO<unknown, never, A>;
@@ -327,12 +395,19 @@ type Scoped<R, E, A> = {
327
395
  readonly acquire: ZIO<R, E, ZStream<R, E, A>>;
328
396
  readonly release: (exit: Exit<any, any>) => Async<R, any, void>;
329
397
  };
330
- type ZStream<R, E, A> = Empty<R, E, A> | Emit<R, E, A> | Concat<R, E, A> | FromPull<R, E, A> | Flatten<R, E, A> | Merge<R, E, A> | Scoped<R, E, A>;
398
+ type Managed<R, E, A> = {
399
+ readonly _tag: "Managed";
400
+ readonly acquire: ZIO<R, E, {
401
+ stream: ZStream<R, E, A>;
402
+ release: (exit: Exit<any, any>) => Async<R, any, void>;
403
+ }>;
404
+ };
405
+ type ZStream<R, E, A> = Empty<R, E, A> | Emit<R, E, A> | Concat<R, E, A> | FromPull<R, E, A> | Flatten<R, E, A> | Merge<R, E, A> | Scoped<R, E, A> | Managed<R, E, A>;
331
406
  type Normalize<E> = (u: unknown) => E;
332
407
  declare const widenOpt: <E1, E2>(opt: Option<E1>) => Option<E1 | E2>;
333
408
  declare const fromPull: <R, E, A>(pull: ZIO<R, Option<E>, [A, ZStream<R, E, A>]>) => ZStream<R, E, A>;
334
409
  declare const unwrapScoped: <R, E, A>(acquire: ZIO<R, E, ZStream<R, E, A>>, release: (exit: Exit<any, any>) => Async<R, any, void>) => ZStream<R, E, A>;
335
- declare const managedStream: <R, E, A>(acquire: Async<R, E, {
410
+ declare const managedStream: <R, E, A>(acquire: ZIO<R, E, {
336
411
  stream: ZStream<R, E, A>;
337
412
  release: (exit: Exit<any, any>) => Async<R, any, void>;
338
413
  }>) => ZStream<R, E, A>;
@@ -352,4 +427,4 @@ declare function fromArray<A>(values: readonly A[]): ZStream<unknown, never, A>;
352
427
  declare function collectStream<R, E, A>(stream: ZStream<R, E, A>): ZIO<R, E, A[]>;
353
428
  declare function streamFromReadableStream<E>(body: ReadableStream<Uint8Array> | null | undefined, normalizeError: Normalize<E>): ZStream<unknown, E, Uint8Array>;
354
429
 
355
- export { type FiberId as $, type Async as A, mapTryAsync as B, type Some as C, none as D, type Exit as E, type Fiber as F, some as G, type Canceler as H, type Interrupted as I, type CancelToken as J, makeCancelToken as K, linkAbortController as L, from as M, type None as N, type Option as O, type BrassError as P, fork as Q, Runtime as R, Scope as S, unsafeRunAsync as T, toPromise as U, fromPromise as V, fromCallback as W, tryPromiseAbortable as X, fromPromiseAbortable as Y, type ZStream as Z, getCurrentRuntime as _, type ZIO as a, type FiberStatus as a0, RuntimeFiber as a1, getCurrentFiber as a2, withCurrentFiber as a3, type ScopeId as a4, withScope as a5, withScopeAsync as a6, type Task as a7, Scheduler as a8, globalScheduler as a9, streamFromReadableStream as aA, type Empty as aa, type Emit as ab, type Concat as ac, type Flatten as ad, type FromPull as ae, type Merge as af, type Scoped as ag, type Normalize as ah, widenOpt as ai, fromPull as aj, unwrapScoped as ak, managedStream as al, mergeStream as am, emptyStream as an, emitStream as ao, concatStream as ap, flattenStream as aq, merge as ar, uncons as as, assertNever as at, mapStream as au, rangeStream as av, zip as aw, foreachStream as ax, fromArray as ay, collectStream as az, sync as b, flatMap as c, mapError as d, catchAll as e, fail as f, end as g, asyncFold as h, asyncCatchAll as i, asyncMapError as j, asyncSucceed as k, asyncFail as l, map as m, asyncSync as n, orElseOptional as o, asyncTotal as p, async as q, asyncMap as r, succeed as s, asyncFlatMap as t, unit as u, acquireRelease as v, asyncInterruptible as w, type AsyncWithPromise as x, withAsyncPromise as y, mapAsync as z };
430
+ export { unsafeGetCurrentRuntime as $, Async as A, mapTryAsync as B, Cause as C, type Some as D, Exit as E, type Fiber as F, none as G, some as H, type Canceler as I, type CancelToken as J, makeCancelToken as K, linkAbortController as L, fork as M, type None as N, type Option as O, unsafeRunAsync as P, toPromise as Q, Runtime as R, Scope as S, fromPromiseAbortable as T, unsafeRunFoldWithEnv as U, type FiberId as V, type FiberStatus as W, type Interrupted as X, RuntimeFiber as Y, type ZStream as Z, getCurrentFiber as _, type ZIO as a, withCurrentFiber as a0, type ScopeId as a1, withScopeAsync as a2, withScope as a3, type Task as a4, Scheduler as a5, globalScheduler as a6, type Empty as a7, type Emit as a8, type Concat as a9, type Flatten as aa, type FromPull as ab, type Merge as ac, type Scoped as ad, type Managed as ae, type Normalize as af, widenOpt as ag, fromPull as ah, unwrapScoped as ai, managedStream as aj, mergeStream as ak, emptyStream as al, emitStream as am, concatStream as an, flattenStream as ao, merge as ap, uncons as aq, assertNever as ar, mapStream as as, rangeStream as at, zip as au, foreachStream as av, fromArray as aw, collectStream as ax, streamFromReadableStream as ay, sync as b, flatMap as c, mapError as d, catchAll as e, fail as f, end as g, asyncFold as h, asyncCatchAll as i, asyncMapError as j, asyncSucceed as k, asyncFail as l, map as m, asyncSync as n, orElseOptional as o, asyncTotal as p, async as q, asyncMap as r, succeed as s, asyncFlatMap as t, unit as u, acquireRelease as v, asyncInterruptible as w, type AsyncWithPromise as x, withAsyncPromise as y, mapAsync as z };