brass-runtime 1.13.8 → 1.15.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.
Files changed (49) hide show
  1. package/README.md +6 -3
  2. package/dist/agent/cli/main.cjs +44 -43
  3. package/dist/agent/cli/main.js +5 -4
  4. package/dist/agent/cli/main.mjs +5 -4
  5. package/dist/agent/index.cjs +4 -3
  6. package/dist/agent/index.d.ts +1 -1
  7. package/dist/agent/index.js +3 -2
  8. package/dist/agent/index.mjs +3 -2
  9. package/dist/{chunk-3R7ZYRK2.mjs → chunk-3QMOKAS5.js} +9 -7
  10. package/dist/{chunk-ATHSSDUF.js → chunk-4NHES7VK.mjs} +113 -31
  11. package/dist/chunk-AR22SXML.js +1043 -0
  12. package/dist/chunk-BDF4AMWX.mjs +3773 -0
  13. package/dist/chunk-BDYEENHT.js +224 -0
  14. package/dist/chunk-BMH5AV44.js +3773 -0
  15. package/dist/chunk-ELOOF35R.mjs +131 -0
  16. package/dist/chunk-JFPU5GQI.mjs +1043 -0
  17. package/dist/{chunk-INZBKOHY.js → chunk-K6M7MDZ4.mjs} +9 -7
  18. package/dist/chunk-MS34J5LY.cjs +224 -0
  19. package/dist/{chunk-XNOTJSMZ.mjs → chunk-PPUXIH5R.js} +113 -31
  20. package/dist/chunk-R3R2FVLG.cjs +131 -0
  21. package/dist/{chunk-ZTDK2DLG.cjs → chunk-STVLQ3XD.cjs} +169 -87
  22. package/dist/chunk-TGIFUAK4.cjs +3773 -0
  23. package/dist/chunk-TO7IKXYT.js +131 -0
  24. package/dist/chunk-UMAZLXAB.mjs +224 -0
  25. package/dist/{chunk-XDINDYNA.cjs → chunk-VEZNF5GZ.cjs} +136 -134
  26. package/dist/chunk-XPZNXSVN.cjs +1043 -0
  27. package/dist/core/index.cjs +216 -0
  28. package/dist/core/index.d.ts +673 -0
  29. package/dist/core/index.js +216 -0
  30. package/dist/core/index.mjs +216 -0
  31. package/dist/{effect-ISvXPLgc.d.ts → effect-CMOQKX8y.d.ts} +202 -31
  32. package/dist/http/index.cjs +3177 -187
  33. package/dist/http/index.d.ts +1692 -9
  34. package/dist/http/index.js +3164 -174
  35. package/dist/http/index.mjs +3164 -174
  36. package/dist/index.cjs +936 -219
  37. package/dist/index.d.ts +313 -36
  38. package/dist/index.js +830 -113
  39. package/dist/index.mjs +830 -113
  40. package/dist/{stream-BvukHxCv.d.ts → stream-FQm9h4Mg.d.ts} +12 -4
  41. package/dist/tracing-DNT9jEbr.d.ts +106 -0
  42. package/package.json +11 -3
  43. package/wasm/pkg/brass_runtime_wasm_engine.d.ts +95 -16
  44. package/wasm/pkg/brass_runtime_wasm_engine.js +715 -15
  45. package/wasm/pkg/brass_runtime_wasm_engine_bg.wasm +0 -0
  46. package/wasm/pkg/brass_runtime_wasm_engine_bg.wasm.d.ts +78 -7
  47. package/dist/chunk-2P4PD6D7.cjs +0 -2557
  48. package/dist/chunk-7F2R7A2V.mjs +0 -2557
  49. package/dist/chunk-L6KKKM66.js +0 -2557
@@ -0,0 +1,673 @@
1
+ import { A as Async, E as Exit, aa as Runtime } from '../effect-CMOQKX8y.js';
2
+ export { n as AbortablePromiseFinish, o as AbortablePromiseLabelStats, p as AbortablePromiseOptions, q as AbortablePromiseOutcome, r as AbortablePromiseStats, t as AsyncWithPromise, C as CancelToken, v as Canceler, w as Cause, b as Fiber, c as FiberId, d as FiberStatus, _ as Interrupted, a1 as None, a2 as NoopHooks, O as Option, R as RuntimeFiber, ad as RuntimeOptions, S as Scope, ak as ScopeId, al as Some, Z as ZIO, aq as abortablePromiseStats, ar as acquireRelease, as as async, at as asyncCatchAll, au as asyncFail, av as asyncFlatMap, aw as asyncFold, ax as asyncInterruptible, ay as asyncMap, az as asyncMapError, aA as asyncSucceed, aB as asyncSync, aC as asyncTotal, aD as catchAll, aE as end, aG as fail, aH as flatMap, aI as fork, aJ as fromPromiseAbortable, aK as getBenchmarkBudget, aL as getCurrentFiber, aP as linkAbortController, aR as makeCancelToken, aS as map, aT as mapAsync, aU as mapError, aV as mapTryAsync, aW as none, aX as orElseOptional, aY as resetAbortablePromiseStats, a_ as runtimeForCaller, b1 as setBenchmarkBudget, b2 as some, b3 as succeed, b4 as sync, b5 as toPromise, b6 as toPromiseByCaller, b7 as unit, b8 as unsafeGetCurrentRuntime, b9 as unsafeRunAsync, ba as unsafeRunFoldWithEnv, bb as withAsyncPromise, bc as withCurrentFiber, bd as withScope, be as withScopeAsync } from '../effect-CMOQKX8y.js';
3
+ export { C as CircuitBreaker, a as CircuitBreakerConfig, b as CircuitBreakerError, c as CircuitBreakerState, d as CircuitBreakerStats, S as Span, e as SpanContext, f as SpanEvent, g as SpanStatus, T as Tracer, h as TracerConfig, m as makeCircuitBreaker, i as makeTracer } from '../tracing-DNT9jEbr.js';
4
+
5
+ type TimeoutError = {
6
+ readonly _tag: "TimeoutError";
7
+ readonly ms: number;
8
+ };
9
+ /**
10
+ * Suspends the fiber for `ms` milliseconds. Cancellable via fiber interruption.
11
+ */
12
+ declare function sleep(ms: number): Async<unknown, never, void>;
13
+ /**
14
+ * Runs `effect` with a timeout of `ms` milliseconds.
15
+ * - If the effect completes before the timeout, returns its result.
16
+ * - If the timeout fires first, the effect is cancelled and a TimeoutError is returned.
17
+ *
18
+ * Works with ANY effect type (Async, FlatMap, Fold, etc.) by forking
19
+ * the effect into a child fiber and interrupting it on timeout.
20
+ */
21
+ declare function timeout<R, E, A>(effect: Async<R, E, A>, ms: number): Async<R, E | TimeoutError, A>;
22
+ type RetryPolicy = {
23
+ /** Maximum number of retry attempts (0 = no retries, just the initial attempt) */
24
+ readonly maxRetries: number;
25
+ /** Base delay in ms for exponential backoff */
26
+ readonly baseDelayMs: number;
27
+ /** Maximum delay cap in ms */
28
+ readonly maxDelayMs: number;
29
+ /** Total time budget in ms (optional). Retries stop if elapsed time exceeds this. */
30
+ readonly maxElapsedMs?: number;
31
+ /** Custom predicate: should this error trigger a retry? Default: always retry. */
32
+ readonly shouldRetry?: (error: unknown, attempt: number) => boolean;
33
+ /** Jitter strategy. Default: "full" (random 0..delay). "none" = no jitter. */
34
+ readonly jitter?: "full" | "none";
35
+ };
36
+ type RetryState = {
37
+ readonly attempt: number;
38
+ readonly elapsedMs: number;
39
+ readonly lastError: unknown;
40
+ };
41
+ /**
42
+ * Retries an effect according to the given policy.
43
+ * Uses exponential backoff with full jitter by default.
44
+ *
45
+ * ```ts
46
+ * const result = retry(
47
+ * fetchData(),
48
+ * { maxRetries: 3, baseDelayMs: 100, maxDelayMs: 5000 }
49
+ * );
50
+ * ```
51
+ */
52
+ declare function retry<R, E, A>(effect: Async<R, E, A>, policy: RetryPolicy): Async<R, E, A>;
53
+ /**
54
+ * Retry with a simple count (no backoff, immediate retry).
55
+ * Useful for flaky operations that just need a few attempts.
56
+ */
57
+ declare function retryN<R, E, A>(effect: Async<R, E, A>, n: number): Async<R, E, A>;
58
+ /**
59
+ * Retry with exponential backoff and full jitter.
60
+ * Convenience wrapper with sensible defaults.
61
+ */
62
+ declare function retryWithBackoff<R, E, A>(effect: Async<R, E, A>, opts?: {
63
+ maxRetries?: number;
64
+ baseDelayMs?: number;
65
+ maxDelayMs?: number;
66
+ maxElapsedMs?: number;
67
+ shouldRetry?: (error: unknown, attempt: number) => boolean;
68
+ }): Async<R, E, A>;
69
+
70
+ /**
71
+ * Acquires a resource, uses it, and guarantees release regardless of outcome.
72
+ *
73
+ * - `acquire` runs uninterruptibly (once started, it completes)
74
+ * - `use` runs with the acquired resource
75
+ * - `release` runs after `use` completes (success, failure, or interruption)
76
+ *
77
+ * ```ts
78
+ * const result = bracket(
79
+ * openConnection(), // acquire
80
+ * (conn) => queryDatabase(conn), // use
81
+ * (conn, exit) => conn.close() // release (always runs)
82
+ * );
83
+ * ```
84
+ */
85
+ declare function bracket<R, E, A, B>(acquire: Async<R, E, A>, use: (resource: A) => Async<R, E, B>, release: (resource: A, exit: Exit<E, B>) => Async<R, any, void>): Async<R, E, B>;
86
+ /**
87
+ * Runs `effect` and then runs `finalizer` regardless of the outcome.
88
+ * The finalizer receives the exit value for inspection.
89
+ *
90
+ * ```ts
91
+ * const result = ensuring(
92
+ * doWork(),
93
+ * (exit) => logCompletion(exit)
94
+ * );
95
+ * ```
96
+ */
97
+ declare function ensuring<R, E, A>(effect: Async<R, E, A>, finalizer: (exit: Exit<E, A>) => Async<R, any, void>): Async<R, E, A>;
98
+ /**
99
+ * A Managed resource describes how to acquire and release a resource.
100
+ * It can be used multiple times — each `use` call acquires a fresh instance.
101
+ *
102
+ * ```ts
103
+ * const dbPool = managed(
104
+ * createPool({ max: 10 }),
105
+ * (pool) => pool.close()
106
+ * );
107
+ *
108
+ * // Use it:
109
+ * const result = useManaged(dbPool, (pool) => pool.query("SELECT 1"));
110
+ * ```
111
+ */
112
+ type Managed<R, E, A> = {
113
+ readonly _tag: "Managed";
114
+ readonly acquire: Async<R, E, A>;
115
+ readonly release: (resource: A, exit: Exit<any, any>) => Async<R, any, void>;
116
+ };
117
+ /**
118
+ * Creates a Managed resource descriptor.
119
+ */
120
+ declare function managed<R, E, A>(acquire: Async<R, E, A>, release: (resource: A, exit?: Exit<any, any>) => Async<R, any, void>): Managed<R, E, A>;
121
+ /**
122
+ * Uses a Managed resource: acquires, runs the body, and releases.
123
+ */
124
+ declare function useManaged<R, E, A, B>(m: Managed<R, E, A>, body: (resource: A) => Async<R, E, B>): Async<R, E, B>;
125
+ /**
126
+ * Combines multiple Managed resources. All are acquired in order,
127
+ * and released in reverse order (LIFO).
128
+ *
129
+ * ```ts
130
+ * const resources = managedAll([dbPool, cacheConn, fileHandle]);
131
+ * const result = useManaged(resources, ([db, cache, file]) => ...);
132
+ * ```
133
+ */
134
+ declare function managedAll<R, E, Resources extends readonly any[]>(manageds: {
135
+ [K in keyof Resources]: Managed<R, E, Resources[K]>;
136
+ }): Managed<R, E, Resources>;
137
+
138
+ type Semaphore = {
139
+ /** Current number of available permits. */
140
+ readonly available: () => number;
141
+ /** Total number of permits (capacity). */
142
+ readonly capacity: number;
143
+ /** Number of effects waiting for a permit. */
144
+ readonly waiting: () => number;
145
+ /** Acquire a permit, run the effect, and release the permit. */
146
+ readonly withPermit: <R, E, A>(effect: Async<R, E, A>) => Async<R, E, A>;
147
+ /** Acquire a permit (manual). Must call release() when done. */
148
+ readonly acquire: () => Async<unknown, never, void>;
149
+ /** Release a permit (manual). */
150
+ readonly release: () => void;
151
+ };
152
+ type SemaphoreStats = {
153
+ readonly capacity: number;
154
+ readonly available: number;
155
+ readonly waiting: number;
156
+ readonly acquired: number;
157
+ readonly released: number;
158
+ };
159
+ /**
160
+ * Creates a counting semaphore with `n` permits.
161
+ *
162
+ * ```ts
163
+ * const sem = makeSemaphore(5); // max 5 concurrent
164
+ *
165
+ * // Automatic acquire/release:
166
+ * const result = await run(sem.withPermit(fetchData()));
167
+ *
168
+ * // Manual acquire/release:
169
+ * await run(sem.acquire());
170
+ * try { ... } finally { sem.release(); }
171
+ * ```
172
+ */
173
+ declare function makeSemaphore(n: number): Semaphore;
174
+
175
+ type Ref<A> = {
176
+ /** Get the current value. */
177
+ readonly get: () => Async<unknown, never, A>;
178
+ /** Set a new value. */
179
+ readonly set: (value: A) => Async<unknown, never, void>;
180
+ /** Modify the value with a function and return the new value. */
181
+ readonly update: (f: (current: A) => A) => Async<unknown, never, A>;
182
+ /** Modify the value and return a derived result. */
183
+ readonly modify: <B>(f: (current: A) => [B, A]) => Async<unknown, never, B>;
184
+ /** Get the current value synchronously (for non-effect contexts). */
185
+ readonly unsafeGet: () => A;
186
+ };
187
+ /**
188
+ * Creates a mutable reference with an initial value.
189
+ *
190
+ * ```ts
191
+ * const counter = makeRef(0);
192
+ *
193
+ * // In effects:
194
+ * await run(counter.update(n => n + 1));
195
+ * const value = await run(counter.get());
196
+ *
197
+ * // Synchronous access (outside effects):
198
+ * counter.unsafeGet();
199
+ * ```
200
+ */
201
+ declare function makeRef<A>(initial: A): Ref<A>;
202
+ /**
203
+ * Creates a derived Ref that applies a lens to a parent Ref.
204
+ *
205
+ * ```ts
206
+ * const state = makeRef({ count: 0, name: "test" });
207
+ * const countRef = derivedRef(state, s => s.count, (s, c) => ({ ...s, count: c }));
208
+ * ```
209
+ */
210
+ declare function derivedRef<A, B>(parent: Ref<A>, get: (a: A) => B, set: (a: A, b: B) => A): Ref<B>;
211
+
212
+ type ScheduleDecision = {
213
+ readonly continue: boolean;
214
+ readonly delayMs: number;
215
+ };
216
+ /**
217
+ * A Schedule<I, O> takes an input I (typically the error or output of an effect)
218
+ * and decides whether to continue and with what delay.
219
+ */
220
+ type Schedule<I, O> = {
221
+ readonly _tag: "Schedule";
222
+ /** Initial state */
223
+ readonly initial: () => any;
224
+ /** Given current state and input, produce a decision and next state */
225
+ readonly step: (state: any, input: I) => [ScheduleDecision, any, O];
226
+ };
227
+ /** Retry/repeat up to N times with no delay. */
228
+ declare function recurs(n: number): Schedule<unknown, number>;
229
+ /** Fixed delay between each retry/repeat. */
230
+ declare function fixed(delayMs: number): Schedule<unknown, number>;
231
+ /** Exponential backoff: delay doubles each time, capped at maxDelayMs. */
232
+ declare function exponential(baseMs: number, maxMs?: number): Schedule<unknown, number>;
233
+ /** Exponential backoff with full jitter (random in [0, delay]). */
234
+ declare function jittered(baseMs: number, maxMs?: number): Schedule<unknown, number>;
235
+ /** Stop after a total elapsed time. */
236
+ declare function elapsed(maxMs: number): Schedule<unknown, number>;
237
+ /** Only continue while a predicate holds on the input. */
238
+ declare function whileInput<I>(pred: (input: I) => boolean): Schedule<I, I>;
239
+ /** Limit a schedule to N repetitions. */
240
+ declare function take<I, O>(schedule: Schedule<I, O>, n: number): Schedule<I, O>;
241
+ /** Compose two schedules: use the first, then switch to the second. */
242
+ declare function andThen<I, O1, O2>(first: Schedule<I, O1>, second: Schedule<I, O2>): Schedule<I, O1 | O2>;
243
+ /** Run both schedules and continue while BOTH say continue. Use max delay. */
244
+ declare function intersect<I, O1, O2>(left: Schedule<I, O1>, right: Schedule<I, O2>): Schedule<I, [O1, O2]>;
245
+ /** Run both schedules and continue while EITHER says continue. Use min delay. */
246
+ declare function union<I, O1, O2>(left: Schedule<I, O1>, right: Schedule<I, O2>): Schedule<I, [O1, O2]>;
247
+ /**
248
+ * Retry an effect according to a schedule.
249
+ * The schedule receives the error as input on each failure.
250
+ */
251
+ declare function retryWithSchedule<R, E, A, O>(effect: Async<R, E, A>, schedule: Schedule<E, O>): Async<R, E, A>;
252
+ /**
253
+ * Repeat an effect according to a schedule.
254
+ * The schedule receives the success value as input on each iteration.
255
+ * Returns the last successful value.
256
+ */
257
+ declare function repeatWithSchedule<R, E, A, O>(effect: Async<R, E, A>, schedule: Schedule<A, O>): Async<R, E, A>;
258
+
259
+ type ShutdownConfig = {
260
+ /** Max time to wait for in-flight work to complete. Default: 30000ms */
261
+ readonly timeoutMs?: number;
262
+ /** Called when shutdown starts. */
263
+ readonly onStart?: () => void;
264
+ /** Called when shutdown completes. */
265
+ readonly onComplete?: (stats: ShutdownStats) => void;
266
+ /** Called if shutdown times out. */
267
+ readonly onTimeout?: (stats: ShutdownStats) => void;
268
+ };
269
+ type ShutdownStats = {
270
+ readonly startedAt: number;
271
+ readonly completedAt: number;
272
+ readonly elapsedMs: number;
273
+ readonly timedOut: boolean;
274
+ };
275
+ /**
276
+ * Performs a graceful shutdown of the runtime.
277
+ *
278
+ * 1. Signals the scheduler to stop accepting new work
279
+ * 2. Waits for in-flight fibers to complete (up to timeoutMs)
280
+ * 3. Calls the runtime's shutdown hook
281
+ * 4. Reports stats
282
+ *
283
+ * ```ts
284
+ * await gracefulShutdown(runtime, {
285
+ * timeoutMs: 5000,
286
+ * onStart: () => console.log("Shutting down..."),
287
+ * onComplete: (stats) => console.log(`Done in ${stats.elapsedMs}ms`),
288
+ * });
289
+ * ```
290
+ */
291
+ declare function gracefulShutdown<R>(runtime: Runtime<R>, config?: ShutdownConfig): Promise<ShutdownStats>;
292
+ /**
293
+ * Registers process signal handlers for graceful shutdown.
294
+ * Handles SIGTERM and SIGINT (Ctrl+C).
295
+ *
296
+ * ```ts
297
+ * registerShutdownHooks(runtime, {
298
+ * timeoutMs: 10000,
299
+ * onComplete: () => process.exit(0),
300
+ * onTimeout: () => process.exit(1),
301
+ * });
302
+ * ```
303
+ */
304
+ declare function registerShutdownHooks<R>(runtime: Runtime<R>, config?: ShutdownConfig): () => void;
305
+
306
+ type TestRuntimeOptions = {
307
+ /** If true, effects run synchronously where possible. Default: true */
308
+ readonly synchronous?: boolean;
309
+ };
310
+ /**
311
+ * Creates a test runtime that provides controlled execution.
312
+ *
313
+ * ```ts
314
+ * const { runtime, run, runExit } = makeTestRuntime();
315
+ *
316
+ * const result = await run(myEffect);
317
+ * const exit = await runExit(myEffect); // get full Exit (success or failure)
318
+ * ```
319
+ */
320
+ declare function makeTestRuntime<R = {}>(env?: R, options?: TestRuntimeOptions): {
321
+ runtime: Runtime<R>;
322
+ run: <E, A>(effect: Async<R, E, A>) => Promise<A>;
323
+ runExit: <E, A>(effect: Async<R, E, A>) => Promise<Exit<E, A>>;
324
+ };
325
+ /**
326
+ * Asserts that an effect succeeds with a specific value.
327
+ *
328
+ * ```ts
329
+ * await assertSucceeds(myEffect, 42);
330
+ * ```
331
+ */
332
+ declare function assertSucceeds<R, E, A>(effect: Async<R, E, A>, expected: A, runtime?: Runtime<R>): Promise<void>;
333
+ /**
334
+ * Asserts that an effect fails with a specific error.
335
+ *
336
+ * ```ts
337
+ * await assertFails(myEffect, "not found");
338
+ * ```
339
+ */
340
+ declare function assertFails<R, E, A>(effect: Async<R, E, A>, expectedError: E, runtime?: Runtime<R>): Promise<void>;
341
+ /**
342
+ * Asserts that an effect fails with an error matching a predicate.
343
+ *
344
+ * ```ts
345
+ * await assertFailsWith(myEffect, (e) => e._tag === "NotFound");
346
+ * ```
347
+ */
348
+ declare function assertFailsWith<R, E, A>(effect: Async<R, E, A>, predicate: (error: E) => boolean, runtime?: Runtime<R>): Promise<void>;
349
+ /**
350
+ * Asserts that an effect completes within a time limit.
351
+ *
352
+ * ```ts
353
+ * await assertCompletesWithin(myEffect, 100); // must finish in 100ms
354
+ * ```
355
+ */
356
+ declare function assertCompletesWithin<R, E, A>(effect: Async<R, E, A>, maxMs: number, runtime?: Runtime<R>): Promise<A>;
357
+ /**
358
+ * Creates an effect that fails on the first N calls, then succeeds.
359
+ * Useful for testing retry logic.
360
+ *
361
+ * ```ts
362
+ * const flaky = flakyEffect(3, "success!", "temporary error");
363
+ * // Fails 3 times, then returns "success!"
364
+ * ```
365
+ */
366
+ declare function flakyEffect<E, A>(failCount: number, successValue: A, errorValue: E): Async<unknown, E, A>;
367
+ /**
368
+ * Creates an effect that takes a specific amount of time to complete.
369
+ * Useful for testing timeouts and concurrency.
370
+ *
371
+ * ```ts
372
+ * const slow = delayedEffect(100, "done"); // completes after 100ms
373
+ * ```
374
+ */
375
+ declare function delayedEffect<A>(ms: number, value: A): Async<unknown, never, A>;
376
+ /**
377
+ * Creates an effect that never completes (hangs forever).
378
+ * Useful for testing timeouts and interruption.
379
+ */
380
+ declare function neverEffect<A = never>(): Async<unknown, never, A>;
381
+
382
+ /**
383
+ * A Layer describes how to build a service.
384
+ *
385
+ * - RIn: dependencies required to build this service
386
+ * - E: possible failure during construction
387
+ * - ROut: the service produced
388
+ */
389
+ type Layer<RIn, E, ROut> = {
390
+ readonly _tag: "Layer";
391
+ readonly build: (deps: RIn) => Async<unknown, E, {
392
+ service: ROut;
393
+ release: () => Async<unknown, never, void>;
394
+ }>;
395
+ };
396
+ /**
397
+ * Creates a Layer from an acquire/release pair.
398
+ *
399
+ * ```ts
400
+ * const DbLayer = layer(
401
+ * () => createPool({ max: 10 }),
402
+ * (pool) => pool.close()
403
+ * );
404
+ * ```
405
+ */
406
+ declare function layer<ROut, E = never>(acquire: () => Async<unknown, E, ROut>, release?: (service: ROut) => Async<unknown, never, void>): Layer<unknown, E, ROut>;
407
+ /**
408
+ * Creates a Layer that depends on another service.
409
+ *
410
+ * ```ts
411
+ * const RepoLayer = layerFrom<DbPool>()(
412
+ * (pool) => createRepo(pool),
413
+ * (repo) => repo.close()
414
+ * );
415
+ * ```
416
+ */
417
+ declare function layerFrom<RIn>(): <ROut, E = never>(acquire: (deps: RIn) => Async<unknown, E, ROut>, release?: (service: ROut) => Async<unknown, never, void>) => Layer<RIn, E, ROut>;
418
+ /**
419
+ * Creates a Layer from a pure value (no lifecycle).
420
+ *
421
+ * ```ts
422
+ * const ConfigLayer = layerSucceed({ port: 3000, host: "localhost" });
423
+ * ```
424
+ */
425
+ declare function layerSucceed<ROut>(value: ROut): Layer<unknown, never, ROut>;
426
+ /**
427
+ * Creates a Layer that always fails.
428
+ */
429
+ declare function layerFail<E>(error: E): Layer<unknown, E, never>;
430
+ /**
431
+ * Compose two layers: the output of `from` feeds into `to`.
432
+ *
433
+ * ```ts
434
+ * const AppLayer = compose(DbLayer, RepoLayer);
435
+ * // DbLayer produces DbPool → RepoLayer consumes DbPool → produces Repo
436
+ * ```
437
+ */
438
+ declare function compose<R1, E1, Mid, E2, ROut>(from: Layer<R1, E1, Mid>, to: Layer<Mid, E2, ROut>): Layer<R1, E1 | E2, ROut>;
439
+ /**
440
+ * Merge two independent layers into one that produces both services.
441
+ *
442
+ * ```ts
443
+ * const AppLayer = merge(DbLayer, CacheLayer);
444
+ * // Produces { db: DbPool, cache: CacheClient }
445
+ * ```
446
+ */
447
+ declare function merge<R1, E1, A, R2, E2, B>(left: Layer<R1, E1, A>, right: Layer<R2, E2, B>): Layer<R1 & R2, E1 | E2, A & B>;
448
+ /**
449
+ * Map the output of a layer.
450
+ */
451
+ declare function mapLayer<RIn, E, A, B>(l: Layer<RIn, E, A>, f: (a: A) => B): Layer<RIn, E, B>;
452
+ /**
453
+ * Builds a layer, runs an effect with the produced service, and releases.
454
+ *
455
+ * ```ts
456
+ * const result = await run(
457
+ * provideLayer(AppLayer, (services) => services.db.query("SELECT 1"))
458
+ * );
459
+ * ```
460
+ */
461
+ declare function provideLayer<RIn, E, ROut, E2, A>(l: Layer<RIn, E, ROut>, use: (service: ROut) => Async<unknown, E2, A>, deps?: RIn): Async<unknown, E | E2, A>;
462
+
463
+ type WorkerPoolConfig = {
464
+ /** Number of worker threads. Default: number of CPUs - 1 */
465
+ readonly size?: number;
466
+ /** Max queued tasks. Tasks beyond this are rejected. Default: 1000 */
467
+ readonly maxQueue?: number;
468
+ /** Task timeout in ms. Default: 30000 */
469
+ readonly taskTimeoutMs?: number;
470
+ };
471
+ type WorkerPoolError = {
472
+ readonly _tag: "WorkerPoolFull";
473
+ readonly queued: number;
474
+ } | {
475
+ readonly _tag: "WorkerTaskTimeout";
476
+ readonly ms: number;
477
+ } | {
478
+ readonly _tag: "WorkerTaskError";
479
+ readonly message: string;
480
+ } | {
481
+ readonly _tag: "WorkerPoolClosed";
482
+ };
483
+ type WorkerPool = {
484
+ /** Execute a function in a worker thread. */
485
+ readonly execute: <A>(fn: () => A) => Async<unknown, WorkerPoolError, A>;
486
+ /** Execute a serializable task (function source + args). */
487
+ readonly run: <A>(taskSource: string, args?: any[]) => Async<unknown, WorkerPoolError, A>;
488
+ /** Current pool stats. */
489
+ readonly stats: () => WorkerPoolStats;
490
+ /** Shutdown the pool (terminate all workers). */
491
+ readonly shutdown: () => Promise<void>;
492
+ };
493
+ type WorkerPoolStats = {
494
+ readonly size: number;
495
+ readonly busy: number;
496
+ readonly idle: number;
497
+ readonly queued: number;
498
+ readonly completed: number;
499
+ readonly failed: number;
500
+ readonly timedOut: number;
501
+ };
502
+ /**
503
+ * Creates a worker pool for CPU-intensive tasks.
504
+ *
505
+ * NOTE: This is a simplified implementation that uses setTimeout to simulate
506
+ * async execution. For real worker_threads support, the pool would need to
507
+ * spawn actual Worker instances. This provides the API contract and can be
508
+ * upgraded to real threads when needed.
509
+ *
510
+ * ```ts
511
+ * const pool = makeWorkerPool({ size: 4 });
512
+ *
513
+ * const result = await run(pool.execute(() => heavyComputation()));
514
+ *
515
+ * await pool.shutdown();
516
+ * ```
517
+ */
518
+ declare function makeWorkerPool(config?: WorkerPoolConfig): WorkerPool;
519
+
520
+ type MetricType = "counter" | "gauge" | "histogram";
521
+ type MetricValue = {
522
+ readonly name: string;
523
+ readonly type: MetricType;
524
+ readonly value: number;
525
+ readonly labels: Record<string, string>;
526
+ readonly timestamp: number;
527
+ };
528
+ type HistogramBuckets = {
529
+ readonly boundaries: readonly number[];
530
+ counts: number[];
531
+ sum: number;
532
+ count: number;
533
+ min: number;
534
+ max: number;
535
+ };
536
+ type MetricsRegistry = {
537
+ /** Create or get a counter. */
538
+ readonly counter: (name: string, labels?: Record<string, string>) => Counter;
539
+ /** Create or get a gauge. */
540
+ readonly gauge: (name: string, labels?: Record<string, string>) => Gauge;
541
+ /** Create or get a histogram. */
542
+ readonly histogram: (name: string, boundaries?: number[], labels?: Record<string, string>) => Histogram;
543
+ /** Get all current metric values. */
544
+ readonly snapshot: () => MetricSnapshot;
545
+ /** Reset all metrics. */
546
+ readonly reset: () => void;
547
+ };
548
+ type Counter = {
549
+ readonly increment: (n?: number) => void;
550
+ readonly value: () => number;
551
+ };
552
+ type Gauge = {
553
+ readonly set: (value: number) => void;
554
+ readonly increment: (n?: number) => void;
555
+ readonly decrement: (n?: number) => void;
556
+ readonly value: () => number;
557
+ };
558
+ type Histogram = {
559
+ readonly observe: (value: number) => void;
560
+ readonly buckets: () => HistogramBuckets;
561
+ readonly percentile: (p: number) => number;
562
+ };
563
+ type MetricSnapshot = {
564
+ readonly counters: Array<{
565
+ name: string;
566
+ labels: Record<string, string>;
567
+ value: number;
568
+ }>;
569
+ readonly gauges: Array<{
570
+ name: string;
571
+ labels: Record<string, string>;
572
+ value: number;
573
+ }>;
574
+ readonly histograms: Array<{
575
+ name: string;
576
+ labels: Record<string, string>;
577
+ buckets: HistogramBuckets;
578
+ }>;
579
+ };
580
+ /**
581
+ * Creates a metrics registry.
582
+ *
583
+ * ```ts
584
+ * const metrics = makeMetrics();
585
+ *
586
+ * const requestCount = metrics.counter("http_requests_total", { method: "GET" });
587
+ * requestCount.increment();
588
+ *
589
+ * const latency = metrics.histogram("http_request_duration_ms");
590
+ * latency.observe(42.5);
591
+ *
592
+ * const activeConns = metrics.gauge("active_connections");
593
+ * activeConns.set(10);
594
+ *
595
+ * console.log(metrics.snapshot());
596
+ * ```
597
+ */
598
+ declare function makeMetrics(): MetricsRegistry;
599
+
600
+ /**
601
+ * Base type for tagged errors. All errors should extend this pattern:
602
+ *
603
+ * ```ts
604
+ * type NetworkError = { _tag: "NetworkError"; url: string; status: number };
605
+ * type TimeoutError = { _tag: "TimeoutError"; ms: number };
606
+ * type AppError = NetworkError | TimeoutError;
607
+ * ```
608
+ */
609
+ type TaggedError = {
610
+ readonly _tag: string;
611
+ };
612
+ /**
613
+ * Catches a specific error by its `_tag` field and handles it.
614
+ * Other errors pass through unchanged.
615
+ *
616
+ * ```ts
617
+ * const result = catchTag(effect, "NetworkError", (e) => fallbackValue);
618
+ * // e is narrowed to NetworkError
619
+ * ```
620
+ */
621
+ declare function catchTag<R, E extends TaggedError, A, Tag extends E["_tag"], B>(effect: Async<R, E, A>, tag: Tag, handler: (error: Extract<E, {
622
+ _tag: Tag;
623
+ }>) => Async<R, Exclude<E, {
624
+ _tag: Tag;
625
+ }>, A | B>): Async<R, Exclude<E, {
626
+ _tag: Tag;
627
+ }>, A | B>;
628
+ /**
629
+ * Catches multiple error tags with a single handler map.
630
+ *
631
+ * ```ts
632
+ * const result = catchTags(effect, {
633
+ * NetworkError: (e) => asyncSucceed(defaultValue),
634
+ * TimeoutError: (e) => retry(effect),
635
+ * });
636
+ * ```
637
+ */
638
+ declare function catchTags<R, E extends TaggedError, A, Handlers extends Partial<{
639
+ [K in E["_tag"]]: (error: Extract<E, {
640
+ _tag: K;
641
+ }>) => Async<R, any, any>;
642
+ }>>(effect: Async<R, E, A>, handlers: Handlers): Async<R, Exclude<E, {
643
+ _tag: keyof Handlers & string;
644
+ }>, A>;
645
+ /**
646
+ * Maps the error channel of an effect.
647
+ *
648
+ * ```ts
649
+ * const result = mapError(effect, (e) => ({ _tag: "Wrapped", cause: e }));
650
+ * ```
651
+ */
652
+ declare function mapError<R, E, E2, A>(effect: Async<R, E, A>, f: (error: E) => E2): Async<R, E2, A>;
653
+ /**
654
+ * Wraps any error with a tag, making it part of a discriminated union.
655
+ *
656
+ * ```ts
657
+ * const typed = tagError(fetchData(), "NetworkError", (e) => ({ url, cause: e }));
658
+ * // Error type becomes { _tag: "NetworkError"; url: string; cause: unknown }
659
+ * ```
660
+ */
661
+ declare function tagError<R, E, A, Tag extends string, Fields extends Record<string, unknown>>(effect: Async<R, E, A>, tag: Tag, enrich?: (error: E) => Fields): Async<R, {
662
+ _tag: Tag;
663
+ } & Fields, A>;
664
+ /**
665
+ * If the effect fails, run the fallback instead.
666
+ *
667
+ * ```ts
668
+ * const result = orElse(primary(), () => fallback());
669
+ * ```
670
+ */
671
+ declare function orElse<R, E, A, R2, E2, B>(effect: Async<R, E, A>, fallback: (error: E) => Async<R2, E2, B>): Async<R & R2, E2, A | B>;
672
+
673
+ export { Async, type Counter, Exit, type Gauge, type Histogram, type HistogramBuckets, type Layer, type Managed as ManagedResource, type MetricSnapshot, type MetricType, type MetricValue, type MetricsRegistry, type Ref, type RetryPolicy, type RetryState, Runtime, type Schedule, type ScheduleDecision, type Semaphore, type SemaphoreStats, type ShutdownConfig, type ShutdownStats, type TaggedError, type TestRuntimeOptions, type TimeoutError, type WorkerPool, type WorkerPoolConfig, type WorkerPoolError, type WorkerPoolStats, andThen as andThenSchedule, assertCompletesWithin, assertFails, assertFailsWith, assertSucceeds, bracket, catchTag, catchTags, compose as composeLayer, delayedEffect, derivedRef, elapsed, ensuring, exponential, fixed, flakyEffect, gracefulShutdown, intersect, jittered, layer, layerFail, layerFrom, layerSucceed, makeMetrics, makeRef, makeSemaphore, makeTestRuntime, makeWorkerPool, managed, managedAll, mapError as mapErrorTyped, mapLayer, merge as mergeLayer, neverEffect, orElse, provideLayer, recurs, registerShutdownHooks, repeatWithSchedule, retry, retryN, retryWithBackoff, retryWithSchedule, sleep, tagError, take as takeSchedule, timeout, union, useManaged, whileInput };