effect 4.1.6 → 4.1.7

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 (173) hide show
  1. package/dist/Config.d.ts +1 -1
  2. package/dist/Config.d.ts.map +1 -1
  3. package/dist/Config.js +5 -3
  4. package/dist/Config.js.map +1 -1
  5. package/dist/Context.d.ts.map +1 -1
  6. package/dist/Context.js +79 -31
  7. package/dist/Context.js.map +1 -1
  8. package/dist/Cron.d.ts.map +1 -1
  9. package/dist/Cron.js +10 -9
  10. package/dist/Cron.js.map +1 -1
  11. package/dist/Equal.d.ts.map +1 -1
  12. package/dist/Equal.js +2 -3
  13. package/dist/Equal.js.map +1 -1
  14. package/dist/Formatter.js +4 -4
  15. package/dist/Formatter.js.map +1 -1
  16. package/dist/Hash.d.ts.map +1 -1
  17. package/dist/Hash.js +13 -27
  18. package/dist/Hash.js.map +1 -1
  19. package/dist/Metric.d.ts.map +1 -1
  20. package/dist/Metric.js +3 -4
  21. package/dist/Metric.js.map +1 -1
  22. package/dist/Pool.d.ts.map +1 -1
  23. package/dist/Pool.js +49 -21
  24. package/dist/Pool.js.map +1 -1
  25. package/dist/Predicate.d.ts.map +1 -1
  26. package/dist/Predicate.js +2 -1
  27. package/dist/Predicate.js.map +1 -1
  28. package/dist/Scheduler.d.ts.map +1 -1
  29. package/dist/Scheduler.js +15 -9
  30. package/dist/Scheduler.js.map +1 -1
  31. package/dist/SchemaAST.d.ts +2 -2
  32. package/dist/SchemaAST.d.ts.map +1 -1
  33. package/dist/SchemaAST.js +199 -95
  34. package/dist/SchemaAST.js.map +1 -1
  35. package/dist/SchemaParser.d.ts.map +1 -1
  36. package/dist/SchemaParser.js +29 -50
  37. package/dist/SchemaParser.js.map +1 -1
  38. package/dist/Tracer.d.ts.map +1 -1
  39. package/dist/Tracer.js +1 -4
  40. package/dist/Tracer.js.map +1 -1
  41. package/dist/ai/McpClient.d.ts +68 -4
  42. package/dist/ai/McpClient.d.ts.map +1 -1
  43. package/dist/ai/McpClient.js +100 -17
  44. package/dist/ai/McpClient.js.map +1 -1
  45. package/dist/ai/McpClientError.d.ts +46 -4
  46. package/dist/ai/McpClientError.d.ts.map +1 -1
  47. package/dist/ai/McpClientError.js +38 -3
  48. package/dist/ai/McpClientError.js.map +1 -1
  49. package/dist/ai/McpSchema.d.ts +88 -88
  50. package/dist/ai/McpSchema.d.ts.map +1 -1
  51. package/dist/ai/McpServer.d.ts +6 -5
  52. package/dist/ai/McpServer.d.ts.map +1 -1
  53. package/dist/ai/McpServer.js +95 -47
  54. package/dist/ai/McpServer.js.map +1 -1
  55. package/dist/ai/internal/mcpProtocol/v2024_11_05.js +9 -7
  56. package/dist/ai/internal/mcpProtocol/v2024_11_05.js.map +1 -1
  57. package/dist/ai/internal/mcpProtocol/v2025_03_26.js +12 -11
  58. package/dist/ai/internal/mcpProtocol/v2025_03_26.js.map +1 -1
  59. package/dist/ai/internal/mcpProtocol/v2025_06_18.js +18 -17
  60. package/dist/ai/internal/mcpProtocol/v2025_06_18.js.map +1 -1
  61. package/dist/ai/internal/mcpProtocol/v2025_11_25.js +19 -18
  62. package/dist/ai/internal/mcpProtocol/v2025_11_25.js.map +1 -1
  63. package/dist/ai/internal/mcpProtocol/v2026_07_28.js +1 -1
  64. package/dist/ai/internal/mcpProtocol/v2026_07_28.js.map +1 -1
  65. package/dist/ai/internal/mcpProtocol.js +11 -0
  66. package/dist/ai/internal/mcpProtocol.js.map +1 -1
  67. package/dist/ai/internal/mcpRuntime.js +29 -17
  68. package/dist/ai/internal/mcpRuntime.js.map +1 -1
  69. package/dist/ai/internal/mcpSchema/v2024_11_05.js +2 -1
  70. package/dist/ai/internal/mcpSchema/v2024_11_05.js.map +1 -1
  71. package/dist/ai/internal/mcpSchema/v2025_03_26.js +1 -8
  72. package/dist/ai/internal/mcpSchema/v2025_03_26.js.map +1 -1
  73. package/dist/ai/internal/mcpSchema/v2025_06_18.js +5 -9
  74. package/dist/ai/internal/mcpSchema/v2025_06_18.js.map +1 -1
  75. package/dist/ai/internal/mcpSchema/v2025_11_25.js +1 -8
  76. package/dist/ai/internal/mcpSchema/v2025_11_25.js.map +1 -1
  77. package/dist/ai/internal/mcpSchema/v2026_07_28.js +4 -3
  78. package/dist/ai/internal/mcpSchema/v2026_07_28.js.map +1 -1
  79. package/dist/cluster/ClusterCron.d.ts +5 -20
  80. package/dist/cluster/ClusterCron.d.ts.map +1 -1
  81. package/dist/cluster/ClusterCron.js +16 -4
  82. package/dist/cluster/ClusterCron.js.map +1 -1
  83. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  84. package/dist/cluster/SqlMessageStorage.js +3 -1
  85. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  86. package/dist/encoding/Hex.js +7 -5
  87. package/dist/encoding/Hex.js.map +1 -1
  88. package/dist/http/HttpEffect.d.ts.map +1 -1
  89. package/dist/http/HttpEffect.js +31 -2
  90. package/dist/http/HttpEffect.js.map +1 -1
  91. package/dist/http/HttpServerError.d.ts +1 -1
  92. package/dist/http/HttpServerError.js +1 -1
  93. package/dist/internal/core.js +60 -43
  94. package/dist/internal/core.js.map +1 -1
  95. package/dist/internal/effect.js +51 -51
  96. package/dist/internal/effect.js.map +1 -1
  97. package/dist/internal/equal.js +30 -0
  98. package/dist/internal/equal.js.map +1 -1
  99. package/dist/internal/fiberCachedKeys.d.ts +2 -0
  100. package/dist/internal/fiberCachedKeys.d.ts.map +1 -0
  101. package/dist/internal/fiberCachedKeys.js +48 -0
  102. package/dist/internal/fiberCachedKeys.js.map +1 -0
  103. package/dist/internal/hashMap.js +1 -1
  104. package/dist/internal/hashMap.js.map +1 -1
  105. package/dist/internal/predicate.d.ts +2 -0
  106. package/dist/internal/predicate.d.ts.map +1 -0
  107. package/dist/internal/predicate.js +10 -0
  108. package/dist/internal/predicate.js.map +1 -0
  109. package/dist/internal/references.js +8 -11
  110. package/dist/internal/references.js.map +1 -1
  111. package/dist/internal/version.js +1 -1
  112. package/dist/sql/Statement.d.ts +23 -0
  113. package/dist/sql/Statement.d.ts.map +1 -1
  114. package/dist/sql/Statement.js +30 -5
  115. package/dist/sql/Statement.js.map +1 -1
  116. package/dist/workers/Worker.d.ts +21 -0
  117. package/dist/workers/Worker.d.ts.map +1 -1
  118. package/dist/workers/Worker.js +23 -0
  119. package/dist/workers/Worker.js.map +1 -1
  120. package/package.json +1 -1
  121. package/src/Config.ts +6 -3
  122. package/src/Context.ts +108 -50
  123. package/src/Cron.ts +11 -10
  124. package/src/Equal.ts +10 -3
  125. package/src/Formatter.ts +4 -4
  126. package/src/Hash.ts +24 -31
  127. package/src/Metric.ts +4 -4
  128. package/src/Pool.ts +62 -33
  129. package/src/Predicate.ts +2 -5
  130. package/src/Scheduler.ts +27 -10
  131. package/src/SchemaAST.ts +231 -156
  132. package/src/SchemaParser.ts +40 -57
  133. package/src/Tracer.ts +1 -2
  134. package/src/ai/McpClient.ts +157 -15
  135. package/src/ai/McpClientError.ts +55 -4
  136. package/src/ai/McpServer.ts +157 -87
  137. package/src/ai/internal/mcpProtocol/v2024_11_05.ts +8 -13
  138. package/src/ai/internal/mcpProtocol/v2025_03_26.ts +13 -15
  139. package/src/ai/internal/mcpProtocol/v2025_06_18.ts +21 -23
  140. package/src/ai/internal/mcpProtocol/v2025_11_25.ts +22 -24
  141. package/src/ai/internal/mcpProtocol/v2026_07_28.ts +1 -1
  142. package/src/ai/internal/mcpProtocol.ts +15 -0
  143. package/src/ai/internal/mcpRuntime.ts +36 -13
  144. package/src/ai/internal/mcpSchema/v2024_11_05.ts +2 -1
  145. package/src/ai/internal/mcpSchema/v2025_03_26.ts +1 -9
  146. package/src/ai/internal/mcpSchema/v2025_06_18.ts +3 -10
  147. package/src/ai/internal/mcpSchema/v2025_11_25.ts +1 -9
  148. package/src/ai/internal/mcpSchema/v2026_07_28.ts +6 -8
  149. package/src/cluster/ClusterCron.ts +31 -20
  150. package/src/cluster/SqlMessageStorage.ts +7 -2
  151. package/src/encoding/Hex.ts +5 -5
  152. package/src/http/HttpEffect.ts +23 -3
  153. package/src/http/HttpServerError.ts +1 -1
  154. package/src/internal/core.ts +80 -44
  155. package/src/internal/effect.ts +84 -55
  156. package/src/internal/equal.ts +36 -0
  157. package/src/internal/fiberCachedKeys.ts +81 -0
  158. package/src/internal/hashMap.ts +1 -1
  159. package/src/internal/predicate.ts +10 -0
  160. package/src/internal/references.ts +21 -14
  161. package/src/internal/version.ts +1 -1
  162. package/src/sql/Statement.ts +48 -12
  163. package/src/workers/Worker.ts +25 -0
  164. package/dist/internal/hash.d.ts +0 -2
  165. package/dist/internal/hash.d.ts.map +0 -1
  166. package/dist/internal/hash.js +0 -14
  167. package/dist/internal/hash.js.map +0 -1
  168. package/dist/internal/metric.d.ts +0 -2
  169. package/dist/internal/metric.d.ts.map +0 -1
  170. package/dist/internal/metric.js +0 -3
  171. package/dist/internal/metric.js.map +0 -1
  172. package/src/internal/hash.ts +0 -15
  173. package/src/internal/metric.ts +0 -2
@@ -61,6 +61,12 @@ const routingNameKey = (method: string): "name" | "uri" | undefined => {
61
61
  }
62
62
  }
63
63
 
64
+ /**
65
+ * @internal
66
+ */
67
+ export const jsonRpcError = (status: number, error: unknown, id: string | number | null = null) =>
68
+ HttpServerResponse.jsonUnsafe({ jsonrpc: "2.0", id, error }, { status })
69
+
64
70
  const headerMismatch = (message: string): HttpProtocolSelection => ({
65
71
  _tag: "Rejected",
66
72
  status: 400,
@@ -97,11 +103,11 @@ type HttpProtocolSelection =
97
103
  | {
98
104
  readonly _tag: "Rejected"
99
105
  readonly status: 400 | 404
100
- readonly error?: {
106
+ readonly error: {
101
107
  readonly code: number
102
108
  readonly message: string
103
109
  readonly data?: unknown
104
- } | undefined
110
+ }
105
111
  }
106
112
 
107
113
  /**
@@ -241,7 +247,7 @@ export const make = Effect.fnUntraced(function*(
241
247
  const sessionTerminationListeners = new Set<(binding: RequestBinding) => Effect.Effect<void>>()
242
248
  const unsupportedProtocolVersion = (requested: string) => ({
243
249
  code: PublicMcpSchema.UNSUPPORTED_PROTOCOL_VERSION_ERROR_CODE,
244
- message: `Unsupported protocol version '${requested}'`,
250
+ message: `Unsupported protocol version '${requested}'; supported versions: ${protocolVersions.join(", ")}`,
245
251
  data: { supported: protocolVersions, requested }
246
252
  })
247
253
  const selectHttpSession = (headers: Headers.Headers, isInitialize: boolean): HttpProtocolSelection => {
@@ -249,7 +255,15 @@ export const make = Effect.fnUntraced(function*(
249
255
  const sessionId = headers[MCP_SESSION_ID_HEADER]
250
256
  const binding = sessionId === undefined ? undefined : stateful?.resolveSessionId(sessionId)
251
257
  if (sessionId !== undefined && binding === undefined) {
252
- return { _tag: "Rejected", status: 404 }
258
+ return {
259
+ _tag: "Rejected",
260
+ status: 404,
261
+ error: {
262
+ code: PublicMcpSchema.INVALID_REQUEST_ERROR_CODE,
263
+ message:
264
+ "Unknown or expired MCP-Session-Id; send initialize without an MCP-Session-Id header to start a new session"
265
+ }
266
+ }
253
267
  }
254
268
  if (!isInitialize && protocolVersion !== undefined) {
255
269
  if (!protocolVersions.includes(protocolVersion)) {
@@ -266,6 +280,8 @@ export const make = Effect.fnUntraced(function*(
266
280
  }
267
281
  return { _tag: "Accepted", binding, protocol: binding?.protocol }
268
282
  }
283
+ const isStatelessVersion = (headers: Headers.Headers) =>
284
+ statelessProtocol !== undefined && headers[MCP_PROTOCOL_VERSION_HEADER] === statelessProtocol.protocolVersion
269
285
  const selectHttpProtocol = (headers: Headers.Headers, input: unknown): HttpProtocolSelection => {
270
286
  const protocolVersion = headers[MCP_PROTOCOL_VERSION_HEADER]
271
287
  const inputRecord = asRecord(input)
@@ -395,18 +411,17 @@ export const make = Effect.fnUntraced(function*(
395
411
  _tag: "Rejected",
396
412
  response: error === undefined
397
413
  ? HttpServerResponse.empty({ status })
398
- : HttpServerResponse.jsonUnsafe({ jsonrpc: "2.0", id, error }, { status })
414
+ : jsonRpcError(status, error, id)
399
415
  })
400
416
  if (Result.isFailure(parsed)) {
401
417
  const version = headers[MCP_PROTOCOL_VERSION_HEADER]
402
418
  if (version !== undefined && !protocolVersions.includes(version)) {
403
419
  return reject(400, unsupportedProtocolVersion(version))
404
420
  }
405
- const admission = selectHttpProtocol(headers, undefined)
406
421
  // Session and protocol-header errors take precedence over parse errors.
407
- return admission._tag === "Rejected" &&
408
- (admission.error === undefined || admission.error.code === PublicMcpSchema.HEADER_MISMATCH_ERROR_CODE)
409
- ? reject(admission.status, admission.error)
422
+ const selection = isStatelessVersion(headers) ? undefined : selectHttpSession(headers, false)
423
+ return selection?._tag === "Rejected"
424
+ ? reject(selection.status, selection.error)
410
425
  : reject(200, new PublicMcpSchema.ParseError({ message: "Parse error" }))
411
426
  }
412
427
  const input = parsed.success
@@ -414,14 +429,22 @@ export const make = Effect.fnUntraced(function*(
414
429
  if (input.length === 0) {
415
430
  return reject(400, new PublicMcpSchema.InvalidRequest({ message: "Invalid Request" }))
416
431
  }
417
- const admission = selectHttpProtocol(headers, input)
432
+ const admission = selectHttpSession(headers, false)
433
+ if (admission._tag === "Rejected") {
434
+ return reject(admission.status, admission.error)
435
+ }
418
436
  if (
419
- admission._tag === "Rejected" ||
437
+ isStatelessVersion(headers) ||
420
438
  input.some(hasRequestProtocolVersion) ||
421
439
  input.some((message) => Predicate.hasProperty(message, "method") && message.method === "initialize") ||
422
440
  admission.binding?.protocol.runtime.transport.jsonRpc.acceptsBatches !== true
423
441
  ) {
424
- return reject(400)
442
+ return reject(
443
+ 400,
444
+ new PublicMcpSchema.InvalidRequest({
445
+ message: "JSON-RPC batches are not supported; send each message in its own POST request"
446
+ })
447
+ )
425
448
  }
426
449
  return {
427
450
  _tag: "Accepted",
@@ -492,7 +515,7 @@ export const make = Effect.fnUntraced(function*(
492
515
  }
493
516
  const selection = selectHttpSession(headers, false)
494
517
  if (selection._tag === "Rejected") {
495
- return Effect.succeed(HttpServerResponse.empty({ status: selection.status }))
518
+ return Effect.succeed(jsonRpcError(selection.status, selection.error))
496
519
  }
497
520
  const binding = selection.binding!
498
521
  stateful!.terminateSession(sessionId)
@@ -408,7 +408,8 @@ export const GetPromptResult = Schema.Struct({
408
408
  */
409
409
  export const ListToolsResult = Schema.Struct({
410
410
  ...PaginatedResult.fields,
411
- tools: Schema.Array(Tool)
411
+ // Already JSON-encoded by the protocol adapter.
412
+ tools: Schema.Any
412
413
  })
413
414
 
414
415
  /**
@@ -113,14 +113,6 @@ export const GetPromptResult = Schema.Struct({
113
113
  messages: Schema.Array(PromptMessage)
114
114
  })
115
115
 
116
- /**
117
- * @internal
118
- */
119
- export const ListToolsResult = Schema.Struct({
120
- ...Previous.PaginatedResult.fields,
121
- tools: Schema.Array(Tool)
122
- })
123
-
124
116
  /**
125
117
  * @internal
126
118
  */
@@ -172,7 +164,7 @@ export class GetPrompt extends Rpc.make("prompts/get", {
172
164
  * @internal
173
165
  */
174
166
  export class ListTools extends Rpc.make("tools/list", {
175
- success: ListToolsResult,
167
+ success: Previous.ListToolsResult,
176
168
  error: Previous.McpError,
177
169
  payload: Schema.UndefinedOr(Previous.PaginatedRequest)
178
170
  }) {}
@@ -356,14 +356,6 @@ export const GetPromptResult = Schema.Struct({
356
356
  messages: Schema.Array(PromptMessage)
357
357
  })
358
358
 
359
- /**
360
- * @internal
361
- */
362
- export const ListToolsResult = Schema.Struct({
363
- ...Previous.PaginatedResult.fields,
364
- tools: Schema.Array(Tool)
365
- })
366
-
367
359
  /**
368
360
  * @internal
369
361
  */
@@ -417,7 +409,7 @@ export class GetPrompt extends Rpc.make("prompts/get", {
417
409
  * @internal
418
410
  */
419
411
  export class ListTools extends Rpc.make("tools/list", {
420
- success: ListToolsResult,
412
+ success: Previous.ListToolsResult,
421
413
  error: Previous.McpError,
422
414
  payload: Schema.UndefinedOr(Previous.PaginatedRequest)
423
415
  }) {}
@@ -479,7 +471,8 @@ const RequestedSchema = Schema.Struct({
479
471
  type: Schema.Literal("object"),
480
472
  properties: Schema.Record(
481
473
  Schema.String,
482
- Schema.Union([StringSchema, NumberSchema, BooleanSchema, EnumSchema])
474
+ // EnumSchema first: StringSchema also matches an enum and would drop its values.
475
+ Schema.Union([EnumSchema, StringSchema, NumberSchema, BooleanSchema])
483
476
  ),
484
477
  required: optional(Schema.Array(Schema.String))
485
478
  })
@@ -538,14 +538,6 @@ export const GetPromptResult = Schema.Struct({
538
538
  messages: Schema.Array(PromptMessage)
539
539
  })
540
540
 
541
- /**
542
- * @internal
543
- */
544
- export const ListToolsResult = Schema.Struct({
545
- ...Previous.PaginatedResult.fields,
546
- tools: Schema.Array(Tool)
547
- })
548
-
549
541
  /**
550
542
  * @internal
551
543
  */
@@ -590,7 +582,7 @@ export class GetPrompt extends Rpc.make("prompts/get", {
590
582
  * @internal
591
583
  */
592
584
  export class ListTools extends Rpc.make("tools/list", {
593
- success: ListToolsResult,
585
+ success: Previous.ListToolsResult,
594
586
  error: Previous.McpError,
595
587
  payload: Schema.UndefinedOr(Previous.PaginatedRequest)
596
588
  }) {}
@@ -627,14 +627,12 @@ export class GetPrompt extends Rpc.make("prompts/get", {
627
627
  /**
628
628
  * @internal
629
629
  */
630
- export const ListToolsResult = Schema.StructWithRest(
631
- Schema.Struct({
632
- ...PaginatedResult,
633
- ...CacheableResult,
634
- tools: Schema.Array(Tool)
635
- }),
636
- [UnknownObject]
637
- )
630
+ export const ListToolsResult = Schema.Struct({
631
+ ...PaginatedResult,
632
+ ...CacheableResult,
633
+ // Already JSON-encoded by the protocol adapter; a rest signature would re-walk them.
634
+ tools: Schema.Any
635
+ })
638
636
  /**
639
637
  * @internal
640
638
  */
@@ -15,12 +15,13 @@
15
15
  * @stability unstable
16
16
  * @since 4.0.0
17
17
  */
18
+ import * as Cause from "../Cause.ts"
18
19
  import * as Context from "../Context.ts"
19
20
  import * as Cron from "../Cron.ts"
20
21
  import * as DateTime from "../DateTime.ts"
21
22
  import * as Duration from "../Duration.ts"
22
23
  import * as Effect from "../Effect.ts"
23
- import * as Exit from "../Exit.ts"
24
+ import * as Fiber from "../Fiber.ts"
24
25
  import * as Layer from "../Layer.ts"
25
26
  import * as PrimaryKey from "../PrimaryKey.ts"
26
27
  import * as Rpc from "../rpc/Rpc.ts"
@@ -79,6 +80,10 @@ export class CronSchedule extends Context.Service<CronSchedule, {
79
80
  * scheduling step and optional controls for shard group, next-run calculation,
80
81
  * and skipping stale scheduled runs.
81
82
  *
83
+ * A run that fails or dies is logged and not retried, and the next run is
84
+ * scheduled as usual. To retry typed failures, apply `Effect.retry` inside
85
+ * `execute` with your own stopping policy.
86
+ *
82
87
  * @stability unstable
83
88
  * @category constructors
84
89
  * @since 4.0.0
@@ -103,9 +108,7 @@ export const make = <E, R>(options: {
103
108
  * current time.
104
109
  *
105
110
  * When enabled, all runs are processed one at a time by a single entity, so
106
- * missed runs are caught up in order without overlapping. A run that fails
107
- * is retried in place and blocks later runs until it succeeds or is older
108
- * than `skipIfOlderThan`.
111
+ * missed runs are caught up in order without overlapping.
109
112
  */
110
113
  readonly calculateNextRunFromPrevious?: boolean | undefined
111
114
 
@@ -168,16 +171,23 @@ export const make = <E, R>(options: {
168
171
  { shardGroup: options.shardGroup }
169
172
  )
170
173
 
171
- const execute = Effect.fnUntraced(function*(dateTime: DateTime.Utc) {
172
- const now = yield* DateTime.now
173
- if (DateTime.isLessThan(dateTime, DateTime.subtractDuration(now, cronSchedule.skipIfOlderThan))) {
174
- return
175
- }
176
- return yield* Effect.provideContext(
177
- options.execute,
178
- Context.add(scheduleContext, CurrentScheduledTime, dateTime)
179
- )
180
- }, Effect.orDie)
174
+ const execute = Effect.fnUntraced(
175
+ function*(dateTime: DateTime.Utc) {
176
+ const now = yield* DateTime.now
177
+ if (DateTime.isLessThan(dateTime, DateTime.subtractDuration(now, cronSchedule.skipIfOlderThan))) {
178
+ return
179
+ }
180
+ // the run gets its own fiber and scope, so its child fibers stop and its
181
+ // finalizers see its exit before the next run is scheduled
182
+ const fiber = yield* Effect.forkChild(
183
+ Effect.provideContext(options.execute, Context.add(scheduleContext, CurrentScheduledTime, dateTime)),
184
+ { uninterruptible: "inherit" }
185
+ )
186
+ return yield* Fiber.join(fiber).pipe(Effect.onInterrupt(() => Fiber.interrupt(fiber)))
187
+ },
188
+ Effect.scoped,
189
+ Effect.orDie
190
+ )
181
191
 
182
192
  const EntityLayer = CronEntity.toLayer(Effect.gen(function*() {
183
193
  const address = yield* Entity.CurrentAddress
@@ -193,12 +203,13 @@ export const make = <E, R>(options: {
193
203
  }
194
204
  processedRun = true
195
205
  return Effect.onExitPrimitive(
196
- execute(dateTime),
197
- (exit) =>
198
- Effect.andThen(
199
- Exit.isFailure(exit) ? Effect.logWarning(exit.cause) : Effect.void,
200
- fromPrevious ? schedule(nextRun(dateTime)) : Effect.flatMap(DateTime.now, (now) => schedule(nextRun(now)))
201
- ),
206
+ execute(dateTime).pipe(
207
+ Effect.tapCause(Effect.logWarning),
208
+ // interrupted runs are left for the entity manager to replay
209
+ Effect.catchCauseIf((cause) => !Cause.hasInterrupts(cause), () => Effect.void)
210
+ ),
211
+ () =>
212
+ fromPrevious ? schedule(nextRun(dateTime)) : Effect.flatMap(DateTime.now, (now) => schedule(nextRun(now))),
202
213
  true
203
214
  ).pipe(
204
215
  Effect.annotateLogs({
@@ -784,8 +784,13 @@ export const makeEncodedStorage: (options?: {
784
784
  const update = reply._tag === "Chunk" ?
785
785
  sql`UPDATE ${messagesTableSql} SET last_reply_id = ${reply.id} WHERE id = ${reply.requestId}` :
786
786
  sql`UPDATE ${messagesTableSql} SET processed = ${sqlTrue}, last_reply_id = ${reply.id} WHERE request_id = ${reply.requestId}`
787
- return update.unprepared.pipe(
788
- Effect.andThen(sql`INSERT INTO ${repliesTableSql} ${sql.insert(row)}`),
787
+ return lockRequest(reply.requestId).pipe(
788
+ Effect.flatMap((request) =>
789
+ // the request was deleted, so there is nothing left to reply to
790
+ request.length === 0 ? Effect.void : update.unprepared.pipe(
791
+ Effect.andThen(sql`INSERT INTO ${repliesTableSql} ${sql.insert(row)}`)
792
+ )
793
+ ),
789
794
  sql.withTransaction
790
795
  )
791
796
  }).pipe(
@@ -284,10 +284,10 @@ const hexEncodeUint8Array = (bytes: Uint8Array): string => {
284
284
  return result
285
285
  }
286
286
 
287
- const hexCodes = new Uint8Array(256).fill(255)
288
- for (let i = 0; i < 16; i++) {
289
- hexCodes["0123456789abcdef".charCodeAt(i)] = i
290
- hexCodes["0123456789ABCDEF".charCodeAt(i)] = i
291
- }
287
+ // Maps each byte to its hex digit value, or 255 when it is not a hex digit.
288
+ const hexCodes = Uint8Array.from({ length: 256 }, (_, charCode) => {
289
+ const digit = "0123456789abcdef".indexOf(String.fromCharCode(charCode).toLowerCase())
290
+ return digit === -1 ? 255 : digit
291
+ })
292
292
 
293
293
  const hexCode = (charCode: number): number => charCode < 256 ? hexCodes[charCode] : 255
@@ -9,7 +9,7 @@
9
9
  * @stability unstable
10
10
  * @since 4.0.0
11
11
  */
12
- import type * as Cause from "../Cause.ts"
12
+ import * as Cause from "../Cause.ts"
13
13
  import * as Context from "../Context.ts"
14
14
  import * as Effect from "../Effect.ts"
15
15
  import * as Exit from "../Exit.ts"
@@ -17,6 +17,7 @@ import * as Fiber from "../Fiber.ts"
17
17
  import { constVoid, dual } from "../Function.ts"
18
18
  import { contA, contE } from "../internal/core.ts"
19
19
  import { effectIsExit, fiberEnterUninterruptibleUnsafe, reportCauseUnsafe } from "../internal/effect.ts"
20
+ import { isSchemaError } from "../internal/schemaError.ts"
20
21
  import * as Layer from "../Layer.ts"
21
22
  import * as Scope from "../Scope.ts"
22
23
  import * as Stream from "../Stream.ts"
@@ -26,6 +27,7 @@ import { type HttpMiddleware, isTracerDisabledUnsafe, tracer } from "./HttpMiddl
26
27
  import { causeResponse, ClientAbort, HttpServerError, InternalError } from "./HttpServerError.ts"
27
28
  import { HttpServerRequest } from "./HttpServerRequest.ts"
28
29
  import * as Request from "./HttpServerRequest.ts"
30
+ import { isRespondable } from "./HttpServerRespondable.ts"
29
31
  import type { HttpServerResponse } from "./HttpServerResponse.ts"
30
32
  import * as Response from "./HttpServerResponse.ts"
31
33
  import * as requestState from "./internal/requestState.ts"
@@ -63,7 +65,7 @@ export const toHandled = <E, R, EH, RH>(
63
65
  const request = frame.request
64
66
  return Effect.flatMapEager(causeResponse(cause), ([response, cause]) => {
65
67
  const fiber = Fiber.getCurrent()!
66
- reportCauseUnsafe(fiber, cause)
68
+ reportServerCauseUnsafe(fiber, cause)
67
69
  const handler = frame.preResponseHandler
68
70
  const cont = cause.reasons.length === 0 ? Effect.succeed(response) : Effect.failCause(cause)
69
71
  if (handler === undefined) {
@@ -122,7 +124,7 @@ export const toHandled = <E, R, EH, RH>(
122
124
  responded :
123
125
  Effect.matchCauseEffect(tracer(middleware(responded)), {
124
126
  onFailure(cause): Effect.Effect<void, EH, RH> {
125
- reportCauseUnsafe(Fiber.getCurrent()!, cause)
127
+ reportServerCauseUnsafe(Fiber.getCurrent()!, cause)
126
128
  if (handledSymbol in request) return Effect.void
127
129
  return Effect.matchCauseEffectEager(causeResponse(cause), {
128
130
  onFailure(_) {
@@ -227,6 +229,24 @@ export const toHandled = <E, R, EH, RH>(
227
229
 
228
230
  const handledSymbol = Symbol.for("effect/http/HttpEffect/handled")
229
231
 
232
+ // Client errors that `HttpServerRespondable.toResponseOrElse` answers with 400
233
+ // or 404. They are not reported.
234
+ const isClientError = (u: unknown) => !isRespondable(u) && (isSchemaError(u) || Cause.isNoSuchElementError(u))
235
+
236
+ // Both reporting paths can see the same cause. Reusing its filtered cause lets
237
+ // reporters skip the second report.
238
+ const reportableCauses = new WeakMap<Cause.Cause<unknown>, Cause.Cause<unknown>>()
239
+
240
+ const reportServerCauseUnsafe = (fiber: Fiber.Fiber<unknown, unknown>, cause: Cause.Cause<unknown>) => {
241
+ let reportable = reportableCauses.get(cause)
242
+ if (reportable === undefined) {
243
+ const reasons = cause.reasons.filter((reason) => reason._tag !== "Fail" || !isClientError(reason.error))
244
+ reportable = reasons.length === cause.reasons.length ? cause : Cause.fromReasons(reasons)
245
+ reportableCauses.set(cause, reportable)
246
+ }
247
+ reportCauseUnsafe(fiber, reportable)
248
+ }
249
+
230
250
  /**
231
251
  * Disables automatic closing for an HTTP request scope.
232
252
  *
@@ -289,7 +289,7 @@ const formatRequestMessage = (reason: string, description: string | undefined, i
289
289
 
290
290
  /**
291
291
  * Converts a failed handler cause into the HTTP response that should be sent and
292
- * the cause that should be reported.
292
+ * the cause to propagate.
293
293
  *
294
294
  * **Details**
295
295
  *