effect 4.1.5 → 4.1.6

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 (309) hide show
  1. package/ai-docs/package.json +2 -2
  2. package/dist/Channel.d.ts +104 -7
  3. package/dist/Channel.d.ts.map +1 -1
  4. package/dist/Channel.js +73 -4
  5. package/dist/Channel.js.map +1 -1
  6. package/dist/Codegen.d.ts +7 -2
  7. package/dist/Codegen.d.ts.map +1 -1
  8. package/dist/Codegen.js +7 -2
  9. package/dist/Codegen.js.map +1 -1
  10. package/dist/Effect.d.ts +139 -18
  11. package/dist/Effect.d.ts.map +1 -1
  12. package/dist/Effect.js +10 -2
  13. package/dist/Effect.js.map +1 -1
  14. package/dist/Equal.d.ts +4 -4
  15. package/dist/Equal.d.ts.map +1 -1
  16. package/dist/Equal.js +78 -65
  17. package/dist/Equal.js.map +1 -1
  18. package/dist/Hash.d.ts.map +1 -1
  19. package/dist/Hash.js +24 -17
  20. package/dist/Hash.js.map +1 -1
  21. package/dist/LayerRef.d.ts +1 -1
  22. package/dist/LayerRef.d.ts.map +1 -1
  23. package/dist/LayerRef.js +3 -3
  24. package/dist/LayerRef.js.map +1 -1
  25. package/dist/Logger.d.ts +37 -13
  26. package/dist/Logger.d.ts.map +1 -1
  27. package/dist/Logger.js +39 -10
  28. package/dist/Logger.js.map +1 -1
  29. package/dist/Match.d.ts +23 -23
  30. package/dist/Match.d.ts.map +1 -1
  31. package/dist/Match.js.map +1 -1
  32. package/dist/Order.js +1 -1
  33. package/dist/Order.js.map +1 -1
  34. package/dist/Schema.d.ts +41 -0
  35. package/dist/Schema.d.ts.map +1 -1
  36. package/dist/Schema.js +51 -390
  37. package/dist/Schema.js.map +1 -1
  38. package/dist/SchemaAST.d.ts.map +1 -1
  39. package/dist/SchemaAST.js +0 -3
  40. package/dist/SchemaAST.js.map +1 -1
  41. package/dist/ScopedCache.d.ts.map +1 -1
  42. package/dist/ScopedCache.js +6 -4
  43. package/dist/ScopedCache.js.map +1 -1
  44. package/dist/Stream.d.ts +128 -5
  45. package/dist/Stream.d.ts.map +1 -1
  46. package/dist/Stream.js +124 -29
  47. package/dist/Stream.js.map +1 -1
  48. package/dist/String.js +1 -1
  49. package/dist/String.js.map +1 -1
  50. package/dist/Tracer.d.ts +10 -0
  51. package/dist/Tracer.d.ts.map +1 -1
  52. package/dist/Tracer.js +2 -0
  53. package/dist/Tracer.js.map +1 -1
  54. package/dist/ai/McpSchema.d.ts +88 -88
  55. package/dist/ai/McpSchema.d.ts.map +1 -1
  56. package/dist/ai/McpServer.d.ts +152 -2
  57. package/dist/ai/McpServer.d.ts.map +1 -1
  58. package/dist/ai/McpServer.js +8 -2
  59. package/dist/ai/McpServer.js.map +1 -1
  60. package/dist/ai/Prompt.d.ts +3 -1
  61. package/dist/ai/Prompt.d.ts.map +1 -1
  62. package/dist/ai/Prompt.js +17 -3
  63. package/dist/ai/Prompt.js.map +1 -1
  64. package/dist/ai/Skill.d.ts +172 -0
  65. package/dist/ai/Skill.d.ts.map +1 -0
  66. package/dist/ai/Skill.js +209 -0
  67. package/dist/ai/Skill.js.map +1 -0
  68. package/dist/ai/index.d.ts +5 -0
  69. package/dist/ai/index.d.ts.map +1 -1
  70. package/dist/ai/index.js +5 -0
  71. package/dist/ai/index.js.map +1 -1
  72. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  73. package/dist/cluster/ClusterWorkflowEngine.js +155 -83
  74. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  75. package/dist/cluster/MessageStorage.d.ts +56 -0
  76. package/dist/cluster/MessageStorage.d.ts.map +1 -1
  77. package/dist/cluster/MessageStorage.js +18 -2
  78. package/dist/cluster/MessageStorage.js.map +1 -1
  79. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  80. package/dist/cluster/SqlMessageStorage.js +45 -22
  81. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  82. package/dist/cluster/internal/entityActivation.js +2 -0
  83. package/dist/cluster/internal/entityActivation.js.map +1 -1
  84. package/dist/cluster/internal/entityManager.js +8 -5
  85. package/dist/cluster/internal/entityManager.js.map +1 -1
  86. package/dist/encoding/Base64.js +5 -1
  87. package/dist/encoding/Base64.js.map +1 -1
  88. package/dist/encoding/Base64Url.d.ts.map +1 -1
  89. package/dist/encoding/Base64Url.js +6 -1
  90. package/dist/encoding/Base64Url.js.map +1 -1
  91. package/dist/encoding/Xml.d.ts +38 -9
  92. package/dist/encoding/Xml.d.ts.map +1 -1
  93. package/dist/encoding/Xml.js +63 -31
  94. package/dist/encoding/Xml.js.map +1 -1
  95. package/dist/http/FetchHttpClient.d.ts +2 -1
  96. package/dist/http/FetchHttpClient.d.ts.map +1 -1
  97. package/dist/http/FetchHttpClient.js +14 -10
  98. package/dist/http/FetchHttpClient.js.map +1 -1
  99. package/dist/http/Headers.d.ts +201 -3
  100. package/dist/http/Headers.d.ts.map +1 -1
  101. package/dist/http/Headers.js +205 -3
  102. package/dist/http/Headers.js.map +1 -1
  103. package/dist/http/HttpClient.d.ts.map +1 -1
  104. package/dist/http/HttpClient.js +11 -7
  105. package/dist/http/HttpClient.js.map +1 -1
  106. package/dist/http/HttpClientError.d.ts +1 -0
  107. package/dist/http/HttpClientError.d.ts.map +1 -1
  108. package/dist/http/HttpClientError.js +32 -9
  109. package/dist/http/HttpClientError.js.map +1 -1
  110. package/dist/http/HttpClientRequest.d.ts +46 -0
  111. package/dist/http/HttpClientRequest.d.ts.map +1 -1
  112. package/dist/http/HttpClientRequest.js +15 -0
  113. package/dist/http/HttpClientRequest.js.map +1 -1
  114. package/dist/http/HttpClientResponse.d.ts +132 -0
  115. package/dist/http/HttpClientResponse.d.ts.map +1 -1
  116. package/dist/http/HttpClientResponse.js +83 -0
  117. package/dist/http/HttpClientResponse.js.map +1 -1
  118. package/dist/http/HttpMiddleware.js +1 -1
  119. package/dist/http/HttpMiddleware.js.map +1 -1
  120. package/dist/http/HttpServerResponse.d.ts +118 -0
  121. package/dist/http/HttpServerResponse.d.ts.map +1 -1
  122. package/dist/http/HttpServerResponse.js +43 -0
  123. package/dist/http/HttpServerResponse.js.map +1 -1
  124. package/dist/http/Multipart.d.ts.map +1 -1
  125. package/dist/http/Multipart.js +0 -5
  126. package/dist/http/Multipart.js.map +1 -1
  127. package/dist/http/MultipartParser/internal/contentType.d.ts +6 -0
  128. package/dist/http/MultipartParser/internal/contentType.d.ts.map +1 -1
  129. package/dist/http/MultipartParser/internal/contentType.js +1 -1
  130. package/dist/http/MultipartParser/internal/contentType.js.map +1 -1
  131. package/dist/http/internal/clientPath.d.ts +2 -0
  132. package/dist/http/internal/clientPath.d.ts.map +1 -0
  133. package/dist/http/internal/clientPath.js +24 -0
  134. package/dist/http/internal/clientPath.js.map +1 -0
  135. package/dist/http/internal/redactedPath.js +2 -0
  136. package/dist/http/internal/redactedPath.js.map +1 -1
  137. package/dist/http/internal/tracing.js +5 -3
  138. package/dist/http/internal/tracing.js.map +1 -1
  139. package/dist/http-api/HttpApi.d.ts +2 -2
  140. package/dist/http-api/HttpApi.d.ts.map +1 -1
  141. package/dist/http-api/HttpApi.js +30 -7
  142. package/dist/http-api/HttpApi.js.map +1 -1
  143. package/dist/http-api/HttpApiBuilder.d.ts.map +1 -1
  144. package/dist/http-api/HttpApiBuilder.js +5 -2
  145. package/dist/http-api/HttpApiBuilder.js.map +1 -1
  146. package/dist/http-api/HttpApiClient.d.ts.map +1 -1
  147. package/dist/http-api/HttpApiClient.js +23 -6
  148. package/dist/http-api/HttpApiClient.js.map +1 -1
  149. package/dist/http-api/internal/redactedKeys.d.ts +2 -0
  150. package/dist/http-api/internal/redactedKeys.d.ts.map +1 -0
  151. package/dist/http-api/internal/redactedKeys.js +37 -0
  152. package/dist/http-api/internal/redactedKeys.js.map +1 -0
  153. package/dist/internal/base64.js +34 -2
  154. package/dist/internal/base64.js.map +1 -1
  155. package/dist/internal/dateTime.js +35 -17
  156. package/dist/internal/dateTime.js.map +1 -1
  157. package/dist/internal/effect.js +9 -1
  158. package/dist/internal/effect.js.map +1 -1
  159. package/dist/internal/equal.js +14 -13
  160. package/dist/internal/equal.js.map +1 -1
  161. package/dist/internal/hash.js +4 -1
  162. package/dist/internal/hash.js.map +1 -1
  163. package/dist/internal/matcher.js +17 -17
  164. package/dist/internal/matcher.js.map +1 -1
  165. package/dist/internal/schema/schemaCodegen.js +3 -2
  166. package/dist/internal/schema/schemaCodegen.js.map +1 -1
  167. package/dist/internal/schema/schemaCodegenBuiltIns.d.ts +2 -0
  168. package/dist/internal/schema/schemaCodegenBuiltIns.d.ts.map +1 -0
  169. package/dist/internal/schema/schemaCodegenBuiltIns.js +425 -0
  170. package/dist/internal/schema/schemaCodegenBuiltIns.js.map +1 -0
  171. package/dist/internal/schema/toEquivalence.js +3 -4
  172. package/dist/internal/schema/toEquivalence.js.map +1 -1
  173. package/dist/internal/version.js +1 -1
  174. package/dist/jose/JoseError.d.ts +51 -1
  175. package/dist/jose/JoseError.d.ts.map +1 -1
  176. package/dist/jose/JoseError.js +25 -18
  177. package/dist/jose/JoseError.js.map +1 -1
  178. package/dist/jose/internal/compact.js +10 -5
  179. package/dist/jose/internal/compact.js.map +1 -1
  180. package/dist/observability/Otlp.d.ts +19 -0
  181. package/dist/observability/Otlp.d.ts.map +1 -1
  182. package/dist/observability/Otlp.js +10 -0
  183. package/dist/observability/Otlp.js.map +1 -1
  184. package/dist/observability/OtlpExporter.d.ts +49 -13
  185. package/dist/observability/OtlpExporter.d.ts.map +1 -1
  186. package/dist/observability/OtlpExporter.js +135 -54
  187. package/dist/observability/OtlpExporter.js.map +1 -1
  188. package/dist/observability/OtlpLogger.d.ts +14 -0
  189. package/dist/observability/OtlpLogger.d.ts.map +1 -1
  190. package/dist/observability/OtlpLogger.js +10 -2
  191. package/dist/observability/OtlpLogger.js.map +1 -1
  192. package/dist/observability/OtlpMetrics.d.ts.map +1 -1
  193. package/dist/observability/OtlpMetrics.js +2 -0
  194. package/dist/observability/OtlpMetrics.js.map +1 -1
  195. package/dist/observability/OtlpTracer.d.ts +14 -0
  196. package/dist/observability/OtlpTracer.d.ts.map +1 -1
  197. package/dist/observability/OtlpTracer.js +10 -2
  198. package/dist/observability/OtlpTracer.js.map +1 -1
  199. package/dist/persistence/KeyValueStore.d.ts.map +1 -1
  200. package/dist/persistence/KeyValueStore.js +6 -4
  201. package/dist/persistence/KeyValueStore.js.map +1 -1
  202. package/dist/reactivity/Atom.d.ts.map +1 -1
  203. package/dist/reactivity/Atom.js +10 -9
  204. package/dist/reactivity/Atom.js.map +1 -1
  205. package/dist/rpc/RpcClient.d.ts.map +1 -1
  206. package/dist/rpc/RpcClient.js +32 -1
  207. package/dist/rpc/RpcClient.js.map +1 -1
  208. package/dist/rpc/RpcGroup.d.ts +1 -1
  209. package/dist/rpc/RpcGroup.d.ts.map +1 -1
  210. package/dist/rpc/RpcGroup.js +4 -0
  211. package/dist/rpc/RpcGroup.js.map +1 -1
  212. package/dist/sql/Migrator.d.ts.map +1 -1
  213. package/dist/sql/Migrator.js +2 -5
  214. package/dist/sql/Migrator.js.map +1 -1
  215. package/dist/sql/SqlClient.d.ts.map +1 -1
  216. package/dist/sql/SqlClient.js +5 -1
  217. package/dist/sql/SqlClient.js.map +1 -1
  218. package/dist/workflow/Activity.d.ts +18 -0
  219. package/dist/workflow/Activity.d.ts.map +1 -1
  220. package/dist/workflow/Activity.js +13 -4
  221. package/dist/workflow/Activity.js.map +1 -1
  222. package/dist/workflow/Workflow.d.ts +10 -9
  223. package/dist/workflow/Workflow.d.ts.map +1 -1
  224. package/dist/workflow/Workflow.js +33 -22
  225. package/dist/workflow/Workflow.js.map +1 -1
  226. package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
  227. package/dist/workflow/WorkflowEngine.js +26 -12
  228. package/dist/workflow/WorkflowEngine.js.map +1 -1
  229. package/dist/workflow/internal/retry.d.ts +2 -0
  230. package/dist/workflow/internal/retry.d.ts.map +1 -0
  231. package/dist/workflow/internal/retry.js +11 -0
  232. package/dist/workflow/internal/retry.js.map +1 -0
  233. package/dist/workflow/internal/scope.d.ts +2 -0
  234. package/dist/workflow/internal/scope.d.ts.map +1 -0
  235. package/dist/workflow/internal/scope.js +26 -0
  236. package/dist/workflow/internal/scope.js.map +1 -0
  237. package/package.json +3 -3
  238. package/src/Channel.ts +105 -8
  239. package/src/Codegen.ts +7 -2
  240. package/src/Effect.ts +224 -48
  241. package/src/Equal.ts +85 -79
  242. package/src/Hash.ts +30 -22
  243. package/src/LayerRef.ts +6 -4
  244. package/src/Logger.ts +68 -25
  245. package/src/Match.ts +78 -50
  246. package/src/Order.ts +1 -1
  247. package/src/Schema.ts +79 -296
  248. package/src/SchemaAST.ts +0 -1
  249. package/src/ScopedCache.ts +6 -4
  250. package/src/Stream.ts +167 -33
  251. package/src/String.ts +1 -1
  252. package/src/Tracer.ts +10 -0
  253. package/src/ai/McpServer.ts +161 -4
  254. package/src/ai/Prompt.ts +20 -3
  255. package/src/ai/Skill.ts +236 -0
  256. package/src/ai/index.ts +6 -0
  257. package/src/cluster/ClusterWorkflowEngine.ts +172 -96
  258. package/src/cluster/MessageStorage.ts +51 -2
  259. package/src/cluster/SqlMessageStorage.ts +81 -34
  260. package/src/cluster/internal/entityActivation.ts +6 -0
  261. package/src/cluster/internal/entityManager.ts +8 -4
  262. package/src/encoding/Base64.ts +5 -1
  263. package/src/encoding/Base64Url.ts +6 -1
  264. package/src/encoding/Xml.ts +96 -26
  265. package/src/http/FetchHttpClient.ts +8 -5
  266. package/src/http/Headers.ts +309 -3
  267. package/src/http/HttpClient.ts +19 -7
  268. package/src/http/HttpClientError.ts +42 -9
  269. package/src/http/HttpClientRequest.ts +51 -0
  270. package/src/http/HttpClientResponse.ts +156 -0
  271. package/src/http/HttpMiddleware.ts +1 -1
  272. package/src/http/HttpServerResponse.ts +126 -0
  273. package/src/http/Multipart.ts +0 -5
  274. package/src/http/MultipartParser/internal/contentType.ts +1 -1
  275. package/src/http/internal/clientPath.ts +39 -0
  276. package/src/http/internal/redactedPath.ts +4 -0
  277. package/src/http/internal/tracing.ts +6 -3
  278. package/src/http-api/HttpApi.ts +29 -8
  279. package/src/http-api/HttpApiBuilder.ts +7 -2
  280. package/src/http-api/HttpApiClient.ts +30 -7
  281. package/src/http-api/internal/redactedKeys.ts +40 -0
  282. package/src/internal/base64.ts +57 -2
  283. package/src/internal/dateTime.ts +43 -24
  284. package/src/internal/effect.ts +31 -102
  285. package/src/internal/equal.ts +37 -17
  286. package/src/internal/hash.ts +5 -1
  287. package/src/internal/matcher.ts +34 -113
  288. package/src/internal/schema/schemaCodegen.ts +5 -4
  289. package/src/internal/schema/schemaCodegenBuiltIns.ts +343 -0
  290. package/src/internal/schema/toEquivalence.ts +3 -5
  291. package/src/internal/version.ts +1 -1
  292. package/src/jose/JoseError.ts +34 -0
  293. package/src/jose/internal/compact.ts +14 -5
  294. package/src/observability/Otlp.ts +29 -0
  295. package/src/observability/OtlpExporter.ts +206 -75
  296. package/src/observability/OtlpLogger.ts +24 -2
  297. package/src/observability/OtlpMetrics.ts +2 -0
  298. package/src/observability/OtlpTracer.ts +24 -3
  299. package/src/persistence/KeyValueStore.ts +17 -14
  300. package/src/reactivity/Atom.ts +12 -18
  301. package/src/rpc/RpcClient.ts +26 -1
  302. package/src/rpc/RpcGroup.ts +4 -1
  303. package/src/sql/Migrator.ts +2 -11
  304. package/src/sql/SqlClient.ts +14 -8
  305. package/src/workflow/Activity.ts +28 -4
  306. package/src/workflow/Workflow.ts +68 -44
  307. package/src/workflow/WorkflowEngine.ts +38 -15
  308. package/src/workflow/internal/retry.ts +12 -0
  309. package/src/workflow/internal/scope.ts +36 -0
package/src/Equal.ts CHANGED
@@ -12,7 +12,8 @@
12
12
  */
13
13
  import type { Equivalence } from "./Equivalence.ts"
14
14
  import * as Hash from "./Hash.ts"
15
- import { bufferBytes, byReferenceInstances, getAllObjectKeys, isArrayBufferLike, viewBytes } from "./internal/equal.ts"
15
+ import { byReferenceInstances, getAllObjectKeys, objectKind, toBytes } from "./internal/equal.ts"
16
+ import { addBackEdge, backEdges, hashCache } from "./internal/hash.ts"
16
17
  import { hasProperty } from "./Predicate.ts"
17
18
 
18
19
  /**
@@ -133,14 +134,14 @@ export interface Equal extends Hash.Hash {
133
134
  * Functions without an `Equal` implementation compare by reference. Circular
134
135
  * structures use coinductive equality.
135
136
  *
136
- * Hash values are checked first as a fast-path rejection. The function also
137
- * supports dual data-last usage: call it with one argument to get a curried
138
- * predicate.
137
+ * Custom `Hash` implementations and already-cached structural hashes are
138
+ * checked first as a fast-path rejection. The function also supports dual
139
+ * data-last usage: call it with one argument to get a curried predicate.
139
140
  *
140
141
  * **Gotchas**
141
142
  *
142
143
  * - Object-pair results are cached in a WeakMap. **Objects must not be
143
- * be mutated after their first comparison.**
144
+ * mutated after their first comparison.**
144
145
  * - Map and Set entries are matched within groups of equal hashes, so they
145
146
  * are O(n) for well-distributed hashes and O(n²) when every hash collides.
146
147
  *
@@ -184,114 +185,121 @@ export function equals(): any {
184
185
  }
185
186
 
186
187
  function compareBoth(self: unknown, that: unknown): boolean {
187
- if (self === that) return true
188
- if (self == null || that == null) return false
188
+ if (self == null || that == null) return self === that
189
189
  const selfType = typeof self
190
- if (selfType !== typeof that) {
191
- return false
190
+ if (selfType !== "object" && selfType !== "function") {
191
+ return self === that || (self !== self && that !== that)
192
192
  }
193
- // Special case for NaN: NaN should be considered equal to NaN
194
- if (selfType === "number" && self !== self && that !== that) {
193
+ if (self === that) {
194
+ // A shared reference can close a cycle on one side; hashing records any back-edge.
195
+ if (pathLeft.length > 0 && !hashCache.has(self)) Hash.hash(self)
195
196
  return true
196
197
  }
197
- if (selfType !== "object" && selfType !== "function") {
198
- return false
199
- }
200
-
201
- if (byReferenceInstances.has(self) || byReferenceInstances.has(that)) {
198
+ if (selfType !== typeof that || byReferenceInstances.has(self) || byReferenceInstances.has(that)) {
202
199
  return false
203
200
  }
204
-
205
201
  return compareObjects(self, that)
206
202
  }
207
203
 
208
204
  function compareObjects(self: object, that: object): boolean {
209
205
  const depth = pathLeft.length
210
- // A repeated pair closes a cycle under coinductive equality.
206
+ const prev = seen.get(self)
207
+ if (prev === that) return true
208
+ let known: WeakMap<object, boolean> | undefined
209
+ if (!depth || prev !== undefined) {
210
+ known = results.get(self)
211
+ const cached = known?.get(that)
212
+ if (cached !== undefined) return cached
213
+ }
214
+ let revisited = false
211
215
  for (let i = depth; i-- > 0;) {
212
- if (pathLeft[i] === self && pathRight[i] === that) return true
216
+ const sameLeft = pathLeft[i] === self
217
+ if (sameLeft || pathRight[i] === that) {
218
+ addBackEdge()
219
+ // A repeated pair closes a cycle under coinductive equality.
220
+ if (sameLeft && pathRight[i] === that) return true
221
+ revisited = true
222
+ }
213
223
  }
214
- if (depth) return compareOnPath(self, that)
215
- // Only outermost results are independent of path assumptions.
216
- let known = results.get(self)
217
- if (!known) results.set(self, known = new WeakMap())
218
- let result = known.get(that)
219
- if (result === undefined) known.set(that, result = compareOnPath(self, that))
220
- return result
221
- }
222
-
223
- function compareOnPath(self: object, that: object): boolean {
224
+ // Reject mismatched cycle lengths instead of unrolling both loops.
225
+ if (revisited && Hash.hash(self) !== Hash.hash(that)) return false
226
+ const seenBack = backEdges
224
227
  pathLeft.push(self)
225
228
  pathRight.push(that)
229
+ let result: boolean
226
230
  try {
227
- return compareStructure(self, that)
231
+ result = compareStructure(self, that)
228
232
  } finally {
229
233
  pathLeft.pop()
230
234
  pathRight.pop()
235
+ if (!depth) seen.clear()
236
+ }
237
+ // Cyclic hashes depend on entry point; verify hash agreement.
238
+ if (seenBack !== backEdges) return result && Hash.hash(self) === Hash.hash(that)
239
+ // Only cache acyclic results so nested lookups never skip a back-edge.
240
+ if (depth && prev === undefined) {
241
+ if (result) seen.set(self, that)
242
+ } else {
243
+ if (!known) results.set(self, known = new WeakMap())
244
+ known.set(that, result)
231
245
  }
246
+ return result
232
247
  }
233
248
 
234
249
  // The pairs on the current comparison's path (the coinductive assumptions).
235
250
  const pathLeft: Array<object> = []
236
251
  const pathRight: Array<object> = []
252
+ const seen = new Map<object, object>()
237
253
  const results = new WeakMap<object, WeakMap<object, boolean>>()
238
254
 
239
255
  function compareStructure(self: object, that: object): boolean {
240
- if (Hash.hash(self) !== Hash.hash(that)) {
241
- return false
242
- } else if (self instanceof Date) {
243
- if (!(that instanceof Date)) return false
244
- const selfTime = self.getTime()
245
- const thatTime = that.getTime()
246
- return selfTime === thatTime || (Number.isNaN(selfTime) && Number.isNaN(thatTime))
247
- } else if (self instanceof RegExp) {
248
- return that instanceof RegExp && self.toString() === that.toString()
256
+ const selfHash = hashCache.get(self)
257
+ if (selfHash !== undefined && selfHash !== (hashCache.get(that) ?? selfHash)) return false
258
+ const kind = objectKind(self)
259
+ if (kind === 1 || kind === 2) {
260
+ if (objectKind(that) !== kind) return false
261
+ if (kind === 2) return self.toString() === that.toString()
262
+ if (Hash.hash(self) !== Hash.hash(that)) return false
263
+ const a = (self as Date).getTime()
264
+ const b = (that as Date).getTime()
265
+ return a === b || (a !== a && b !== b)
249
266
  }
250
267
  const bothEquals = isEqual(self)
251
268
  if (bothEquals !== isEqual(that) || (typeof self === "function" && !bothEquals)) {
252
269
  return false
253
- } else if (bothEquals) {
270
+ }
271
+ if ((bothEquals || Hash.isHash(self) || Hash.isHash(that)) && Hash.hash(self) !== Hash.hash(that)) {
272
+ return false
273
+ }
274
+ if (bothEquals) {
254
275
  return (self as Equal)[symbol](that as Equal)
255
- } else if (Array.isArray(self)) {
256
- if (!Array.isArray(that) || self.length !== that.length) {
257
- return false
258
- }
259
- return compareArrays(self, that)
260
- } else if (isArrayBufferLike(self)) {
261
- if (!isArrayBufferLike(that) || (self instanceof ArrayBuffer) !== (that instanceof ArrayBuffer)) {
262
- return false
263
- }
264
- const selfBytes = bufferBytes(self)
265
- const thatBytes = bufferBytes(that)
276
+ }
277
+ if (kind !== objectKind(that)) {
278
+ return false
279
+ }
280
+ if (kind >= 7) {
281
+ const selfBytes = toBytes(self as ArrayBufferLike | DataView, kind)
282
+ const thatBytes = toBytes(that as ArrayBufferLike | DataView, kind)
266
283
  return selfBytes !== undefined && thatBytes !== undefined && compareTypedArrays(selfBytes, thatBytes)
267
- } else if (ArrayBuffer.isView(self)) {
268
- const selfIsDataView = self instanceof DataView
269
- if (
270
- !ArrayBuffer.isView(that) ||
271
- self.byteLength !== that.byteLength ||
272
- selfIsDataView !== (that instanceof DataView)
273
- ) {
274
- return false
275
- }
276
- if (selfIsDataView) {
277
- return compareTypedArrays(viewBytes(self), viewBytes(that as DataView))
278
- }
279
- return compareTypedArrays(self as Uint8Array, that as Uint8Array)
280
- } else if (self instanceof Map) {
281
- if (!(that instanceof Map) || self.size !== that.size) {
282
- return false
283
- }
284
- return compareHashed(self, that, entryHash, equalEntries)
285
- } else if (self instanceof Set) {
286
- if (!(that instanceof Set) || self.size !== that.size) {
287
- return false
288
- }
289
- return compareHashed(self, that, Hash.hash, compareBoth)
284
+ } else if (kind === 5) {
285
+ return compareArrays(self as Array<unknown>, that as Array<unknown>)
286
+ } else if (kind === 6) {
287
+ return (self as ArrayBufferView).byteLength === (that as ArrayBufferView).byteLength &&
288
+ compareTypedArrays(self as Uint8Array, that as Uint8Array)
289
+ } else if (kind === 3) {
290
+ return (self as Map<unknown, unknown>).size === (that as Map<unknown, unknown>).size &&
291
+ compareHashed(self as Map<unknown, unknown>, that as Map<unknown, unknown>, entryHash, equalEntries)
292
+ } else if (kind === 4) {
293
+ return (self as Set<unknown>).size === (that as Set<unknown>).size &&
294
+ compareHashed(self as Set<unknown>, that as Set<unknown>, Hash.hash, compareBoth)
290
295
  }
291
296
  return compareRecords(self as any, that as any)
292
297
  }
293
298
 
294
299
  function compareArrays(self: Array<unknown>, that: Array<unknown>): boolean {
300
+ if (self.length !== that.length) {
301
+ return false
302
+ }
295
303
  for (let i = 0; i < self.length; i++) {
296
304
  if (!compareBoth(self[i], that[i])) {
297
305
  return false
@@ -319,17 +327,15 @@ function compareRecords(
319
327
  ): boolean {
320
328
  const selfKeys = getAllObjectKeys(self)
321
329
  const thatKeys = getAllObjectKeys(that)
322
-
323
- if (selfKeys.size !== thatKeys.size) {
330
+ const thatIsSet = thatKeys instanceof Set
331
+ if ((selfKeys instanceof Set ? selfKeys.size : selfKeys.length) !== (thatIsSet ? thatKeys.size : thatKeys.length)) {
324
332
  return false
325
333
  }
326
-
327
334
  for (const key of selfKeys) {
328
- if (!(thatKeys.has(key)) || !compareBoth(self[key], that[key])) {
335
+ if (!(thatIsSet ? thatKeys.has(key) : Object.hasOwn(that, key)) || !compareBoth(self[key], that[key])) {
329
336
  return false
330
337
  }
331
338
  }
332
-
333
339
  return true
334
340
  }
335
341
 
package/src/Hash.ts CHANGED
@@ -11,8 +11,8 @@
11
11
  * @since 2.0.0
12
12
  */
13
13
  import { dual } from "./Function.ts"
14
- import { bufferBytes, byReferenceInstances, getAllObjectKeys, isArrayBufferLike, viewBytes } from "./internal/equal.ts"
15
- import { addBackEdge, backEdges } from "./internal/hash.ts"
14
+ import { byReferenceInstances, getAllObjectKeys, objectKind, toBytes } from "./internal/equal.ts"
15
+ import { addBackEdge, backEdges, hashCache } from "./internal/hash.ts"
16
16
  import { hasProperty } from "./Predicate.ts"
17
17
 
18
18
  /**
@@ -117,13 +117,15 @@ export const hash: <A>(self: A) => number = <A>(self: A) => {
117
117
  case "object": {
118
118
  if (self === null) {
119
119
  break
120
- } else if (self instanceof Date) {
121
- if (Number.isNaN(self.getTime())) {
120
+ }
121
+ const kind = objectKind(self)
122
+ if (kind === 1) {
123
+ if (Number.isNaN((self as any as Date).getTime())) {
122
124
  return string("Invalid Date")
123
125
  }
124
- return string(self.toISOString())
125
- } else if (self instanceof RegExp) {
126
- return string(self.toString())
126
+ return string((self as any as Date).toISOString())
127
+ } else if (kind === 2) {
128
+ return string((self as any as RegExp).toString())
127
129
  } else {
128
130
  if (byReferenceInstances.has(self)) {
129
131
  return random(self)
@@ -144,17 +146,15 @@ export const hash: <A>(self: A) => number = <A>(self: A) => {
144
146
  h = (self as Hash)[symbol]()
145
147
  } else if (typeof self === "function") {
146
148
  h = random(self)
147
- } else if (isArrayBufferLike(self)) {
148
- const bytes = bufferBytes(self)
149
- h = bytes === undefined ? random(self) : array(bytes)
150
- } else if (self instanceof DataView) {
151
- h = array(viewBytes(self))
152
- } else if (Array.isArray(self) || ArrayBuffer.isView(self)) {
153
- h = array(self as any)
154
- } else if (self instanceof Map) {
155
- h = hashMap(self)
156
- } else if (self instanceof Set) {
157
- h = hashSet(self)
149
+ } else if (kind >= 7) {
150
+ const bytes = toBytes(self as any, kind)
151
+ h = bytes === undefined ? random(self) : hashIndexed(bytes)
152
+ } else if (kind >= 5) {
153
+ h = hashIndexed(self as any)
154
+ } else if (kind === 3) {
155
+ h = hashMap(self as any)
156
+ } else if (kind === 4) {
157
+ h = hashSet(self as any)
158
158
  } else {
159
159
  h = structure(self)
160
160
  }
@@ -203,10 +203,11 @@ export const hash: <A>(self: A) => number = <A>(self: A) => {
203
203
  * @since 2.0.0
204
204
  */
205
205
  export const random: <A extends object>(self: A) => number = (self) => {
206
- if (!randomHashCache.has(self)) {
207
- randomHashCache.set(self, optimize((Math.random() * 0x100000000) | 0))
206
+ let h = randomHashCache.get(self)
207
+ if (h === undefined) {
208
+ randomHashCache.set(self, h = optimize((Math.random() * 0x100000000) | 0))
208
209
  }
209
- return randomHashCache.get(self)!
210
+ return h
210
211
  }
211
212
 
212
213
  // 32-bit MurmurHash3 finalizer.
@@ -580,6 +581,14 @@ export const array = <A>(arr: Iterable<A>): number => {
580
581
  return optimize(h)
581
582
  }
582
583
 
584
+ const hashIndexed = (arr: ArrayLike<unknown>): number => {
585
+ let h = 6151
586
+ for (let i = 0; i < arr.length; i++) {
587
+ h = combine(h, hash(arr[i]))
588
+ }
589
+ return optimize(h)
590
+ }
591
+
583
592
  const hashMap: <K, V>(map: Iterable<readonly [K, V]>) => number = unordered(
584
593
  string("Map"),
585
594
  ([k, v]) => combine(hash(k), hash(v))
@@ -588,5 +597,4 @@ const setSeed = string("Set")
588
597
  const hashSet: <A>(set: Iterable<A>) => number = unordered(setSeed, (element) => combine(setSeed, hash(element)))
589
598
 
590
599
  const randomHashCache = new WeakMap<any, number>()
591
- const hashCache = new WeakMap<object, number>()
592
600
  const visitedObjects = new WeakSet<object>()
package/src/LayerRef.ts CHANGED
@@ -16,7 +16,7 @@ import { getStackTraceLimit, setStackTraceLimit } from "./internal/stackTraceLim
16
16
  import * as Layer from "./Layer.ts"
17
17
  import * as RcRef from "./RcRef.ts"
18
18
  import type * as Schedule from "./Schedule.ts"
19
- import * as Scope from "./Scope.ts"
19
+ import type * as Scope from "./Scope.ts"
20
20
  import type { Mutable } from "./Types.ts"
21
21
 
22
22
  const TypeId = "~effect/LayerRef"
@@ -157,11 +157,13 @@ export const make = Effect.fnUntraced(
157
157
  Scope.Scope | R | SR
158
158
  > {
159
159
  const context = yield* Effect.context<never>()
160
- const memoMap = Layer.CurrentMemoMap.forkOrCreate(context)
161
160
 
162
161
  const rcRef = yield* RcRef.make({
163
- acquire: Effect.contextWith((_: Context.Context<Scope.Scope>) =>
164
- Layer.buildWithMemoMap(layer, memoMap, Context.get(_, Scope.Scope))
162
+ // Fork the creation-time memo map per acquisition so rebuilds don't reuse
163
+ // a live borrow (`Layer.build` would inherit the borrower's memo map).
164
+ acquire: Effect.flatMap(
165
+ Effect.scope,
166
+ (scope) => Layer.buildWithMemoMap(layer, Layer.CurrentMemoMap.forkOrCreate(context), scope)
165
167
  ),
166
168
  idleTimeToLive: options?.idleTimeToLive
167
169
  })
package/src/Logger.ts CHANGED
@@ -7,6 +7,11 @@
7
7
  * logger references, console routing helpers, built-in formatters, batching,
8
8
  * file logging, and layers for installing loggers.
9
9
  *
10
+ * When a log effect (`Effect.log`, `Effect.logWithLevel`, or a level-specific
11
+ * variant) invokes a logger, it does so synchronously in the logging fiber
12
+ * before the log effect completes. A logger that defers work says so in its own
13
+ * documentation, as `batched` and `toFile` do.
14
+ *
10
15
  * @stability stable
11
16
  * @since 2.0.0
12
17
  */
@@ -37,9 +42,9 @@ const TypeId = "~effect/Logger"
37
42
  *
38
43
  * **Details**
39
44
  *
40
- * The runtime calls `log` with the message, level, cause, fiber, and timestamp
41
- * for each log event. Use `Logger.layer` to install one or more loggers for an
42
- * effect.
45
+ * The runtime calls `log` synchronously, in the fiber that logs, with the
46
+ * message, level, cause, fiber, and timestamp for each log event. Use
47
+ * `Logger.layer` to install one or more loggers for an effect.
43
48
  *
44
49
  * **Example** (Creating custom loggers)
45
50
  *
@@ -198,6 +203,10 @@ export const LogToStderr: Context.Reference<boolean> = effect.LogToStderr
198
203
  * Use when an existing logger's output should be transformed without recreating the
199
204
  * logging logic.
200
205
  *
206
+ * **Details**
207
+ *
208
+ * The returned logger calls `self` and then `f` inside the same `log` call.
209
+ *
201
210
  * **Example** (Transforming logger output)
202
211
  *
203
212
  * ```ts import.meta.vitest
@@ -233,6 +242,10 @@ export const map = dual<
233
242
  * Use when an existing logger's output should be transformed without recreating the
234
243
  * logging logic.
235
244
  *
245
+ * **Details**
246
+ *
247
+ * The returned logger calls `self` and then `f` inside the same `log` call.
248
+ *
236
249
  * **Example** (Transforming logger output)
237
250
  *
238
251
  * ```ts import.meta.vitest
@@ -270,6 +283,10 @@ export const map = dual<
270
283
  * Use when an existing logger's output should be transformed without recreating the
271
284
  * logging logic.
272
285
  *
286
+ * **Details**
287
+ *
288
+ * The returned logger calls `self` and then `f` inside the same `log` call.
289
+ *
273
290
  * **Example** (Transforming logger output)
274
291
  *
275
292
  * ```ts import.meta.vitest
@@ -308,6 +325,10 @@ export const map = dual<
308
325
  * Use when a logger's string or object output should be routed to `console.log` for
309
326
  * development or debugging.
310
327
  *
328
+ * **Details**
329
+ *
330
+ * Writes to the console inside the `log` call.
331
+ *
311
332
  * **Example** (Writing logger output with console.log)
312
333
  *
313
334
  * ```ts import.meta.vitest
@@ -349,6 +370,10 @@ export const withConsoleLog = <Message, Output>(
349
370
  * Use when logger output should be routed to `console.error`, such as error logs that
350
371
  * should appear on stderr instead of stdout.
351
372
  *
373
+ * **Details**
374
+ *
375
+ * Writes to the console inside the `log` call.
376
+ *
352
377
  * **Example** (Writing logger output with console.error)
353
378
  *
354
379
  * ```ts import.meta.vitest
@@ -387,8 +412,9 @@ export const withConsoleError = <Message, Output>(
387
412
  *
388
413
  * **Details**
389
414
  *
390
- * Will use the appropriate console method (i.e. `console.log`, `console.error`,
391
- * etc.) based upon the current `LogLevel`.
415
+ * Writes to the console inside the `log` call. Will use the appropriate console
416
+ * method (i.e. `console.log`, `console.error`, etc.) based upon the current
417
+ * `LogLevel`.
392
418
  *
393
419
  * `Debug` uses `console.debug`, `Info` uses `console.info`, `Trace` uses
394
420
  * `console.trace`, `Warn` uses `console.warn`, `Error` and `Fatal` use
@@ -519,6 +545,12 @@ const format = (
519
545
  *
520
546
  * The log function receives an options object containing the message, log level,
521
547
  * cause, fiber information, and timestamp, and should return the desired output.
548
+ * `log` is called synchronously in the fiber that logs, before the log effect
549
+ * completes.
550
+ *
551
+ * **Gotchas**
552
+ *
553
+ * If `log` throws, the log effect dies with the thrown value as a defect.
522
554
  *
523
555
  * **Example** (Creating loggers from functions)
524
556
  *
@@ -747,9 +779,11 @@ export const formatJson = map(formatStructured, Formatter.formatJson)
747
779
  *
748
780
  * **Details**
749
781
  *
750
- * The returned effect starts a scoped background process that periodically
751
- * passes buffered outputs to `flush`. When the scope closes, the background
752
- * process is interrupted and any remaining buffered entries are flushed.
782
+ * The returned logger formats each event with `self` and buffers the output
783
+ * during the log call. A background fiber passes pending entries to `flush`
784
+ * once per window, skipping empty windows. When the scope closes, the
785
+ * background fiber is interrupted and the scope finalizer flushes any remaining
786
+ * entries.
753
787
  *
754
788
  * **Example** (Batching logger output)
755
789
  *
@@ -785,9 +819,11 @@ export const batched = dual<
785
819
  *
786
820
  * **Details**
787
821
  *
788
- * The returned effect starts a scoped background process that periodically
789
- * passes buffered outputs to `flush`. When the scope closes, the background
790
- * process is interrupted and any remaining buffered entries are flushed.
822
+ * The returned logger formats each event with `self` and buffers the output
823
+ * during the log call. A background fiber passes pending entries to `flush`
824
+ * once per window, skipping empty windows. When the scope closes, the
825
+ * background fiber is interrupted and the scope finalizer flushes any remaining
826
+ * entries.
791
827
  *
792
828
  * **Example** (Batching logger output)
793
829
  *
@@ -830,9 +866,11 @@ export const batched = dual<
830
866
  *
831
867
  * **Details**
832
868
  *
833
- * The returned effect starts a scoped background process that periodically
834
- * passes buffered outputs to `flush`. When the scope closes, the background
835
- * process is interrupted and any remaining buffered entries are flushed.
869
+ * The returned logger formats each event with `self` and buffers the output
870
+ * during the log call. A background fiber passes pending entries to `flush`
871
+ * once per window, skipping empty windows. When the scope closes, the
872
+ * background fiber is interrupted and the scope finalizer flushes any remaining
873
+ * entries.
836
874
  *
837
875
  * **Example** (Batching logger output)
838
876
  *
@@ -1136,8 +1174,10 @@ export const consoleJson: Logger<unknown, void> = withConsoleLog(formatJson)
1136
1174
  * **Details**
1137
1175
  *
1138
1176
  * This logger integrates logging with distributed tracing by recording
1139
- * all log messages as events on the current trace span, making them visible
1140
- * in tracing tools like OpenTelemetry, Jaeger, or Zipkin.
1177
+ * all log messages as events on the current local span, making them visible
1178
+ * in tracing tools like OpenTelemetry, Jaeger, or Zipkin. A log made with no
1179
+ * current span, or while the nearest parent span is an `ExternalSpan`, is not
1180
+ * recorded by this logger.
1141
1181
  *
1142
1182
  * This logger is included in the default set of loggers for all Effect programs,
1143
1183
  * so log messages automatically appear as span events unless you override the
@@ -1221,9 +1261,10 @@ export const layer = <
1221
1261
  *
1222
1262
  * **Details**
1223
1263
  *
1224
- * The returned effect requires `FileSystem` and `Scope`. The file logger batches
1225
- * string output, writes each batch to the specified path, and flushes remaining
1226
- * entries when the scope closes.
1264
+ * The returned effect requires `FileSystem` and `Scope`. The file logger is
1265
+ * built on `batched`, so each log call formats and buffers its string output.
1266
+ * Each flushed batch is written to the specified path, and remaining entries
1267
+ * are flushed when the scope closes.
1227
1268
  *
1228
1269
  * **Example** (Writing JSON logs to a file)
1229
1270
  *
@@ -1287,9 +1328,10 @@ export const toFile = dual<
1287
1328
  *
1288
1329
  * **Details**
1289
1330
  *
1290
- * The returned effect requires `FileSystem` and `Scope`. The file logger batches
1291
- * string output, writes each batch to the specified path, and flushes remaining
1292
- * entries when the scope closes.
1331
+ * The returned effect requires `FileSystem` and `Scope`. The file logger is
1332
+ * built on `batched`, so each log call formats and buffers its string output.
1333
+ * Each flushed batch is written to the specified path, and remaining entries
1334
+ * are flushed when the scope closes.
1293
1335
  *
1294
1336
  * **Example** (Writing JSON logs to a file)
1295
1337
  *
@@ -1362,9 +1404,10 @@ export const toFile = dual<
1362
1404
  *
1363
1405
  * **Details**
1364
1406
  *
1365
- * The returned effect requires `FileSystem` and `Scope`. The file logger batches
1366
- * string output, writes each batch to the specified path, and flushes remaining
1367
- * entries when the scope closes.
1407
+ * The returned effect requires `FileSystem` and `Scope`. The file logger is
1408
+ * built on `batched`, so each log call formats and buffers its string output.
1409
+ * Each flushed batch is written to the specified path, and remaining entries
1410
+ * are flushed when the scope closes.
1368
1411
  *
1369
1412
  * **Example** (Writing JSON logs to a file)
1370
1413
  *