effect 4.1.5 → 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 (441) 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/Config.d.ts +1 -1
  11. package/dist/Config.d.ts.map +1 -1
  12. package/dist/Config.js +5 -3
  13. package/dist/Config.js.map +1 -1
  14. package/dist/Context.d.ts.map +1 -1
  15. package/dist/Context.js +79 -31
  16. package/dist/Context.js.map +1 -1
  17. package/dist/Cron.d.ts.map +1 -1
  18. package/dist/Cron.js +10 -9
  19. package/dist/Cron.js.map +1 -1
  20. package/dist/Effect.d.ts +139 -18
  21. package/dist/Effect.d.ts.map +1 -1
  22. package/dist/Effect.js +10 -2
  23. package/dist/Effect.js.map +1 -1
  24. package/dist/Equal.d.ts +4 -4
  25. package/dist/Equal.d.ts.map +1 -1
  26. package/dist/Equal.js +78 -66
  27. package/dist/Equal.js.map +1 -1
  28. package/dist/Formatter.js +4 -4
  29. package/dist/Formatter.js.map +1 -1
  30. package/dist/Hash.d.ts.map +1 -1
  31. package/dist/Hash.js +34 -41
  32. package/dist/Hash.js.map +1 -1
  33. package/dist/LayerRef.d.ts +1 -1
  34. package/dist/LayerRef.d.ts.map +1 -1
  35. package/dist/LayerRef.js +3 -3
  36. package/dist/LayerRef.js.map +1 -1
  37. package/dist/Logger.d.ts +37 -13
  38. package/dist/Logger.d.ts.map +1 -1
  39. package/dist/Logger.js +39 -10
  40. package/dist/Logger.js.map +1 -1
  41. package/dist/Match.d.ts +23 -23
  42. package/dist/Match.d.ts.map +1 -1
  43. package/dist/Match.js.map +1 -1
  44. package/dist/Metric.d.ts.map +1 -1
  45. package/dist/Metric.js +3 -4
  46. package/dist/Metric.js.map +1 -1
  47. package/dist/Order.js +1 -1
  48. package/dist/Order.js.map +1 -1
  49. package/dist/Pool.d.ts.map +1 -1
  50. package/dist/Pool.js +49 -21
  51. package/dist/Pool.js.map +1 -1
  52. package/dist/Predicate.d.ts.map +1 -1
  53. package/dist/Predicate.js +2 -1
  54. package/dist/Predicate.js.map +1 -1
  55. package/dist/Scheduler.d.ts.map +1 -1
  56. package/dist/Scheduler.js +15 -9
  57. package/dist/Scheduler.js.map +1 -1
  58. package/dist/Schema.d.ts +41 -0
  59. package/dist/Schema.d.ts.map +1 -1
  60. package/dist/Schema.js +51 -390
  61. package/dist/Schema.js.map +1 -1
  62. package/dist/SchemaAST.d.ts +2 -2
  63. package/dist/SchemaAST.d.ts.map +1 -1
  64. package/dist/SchemaAST.js +199 -98
  65. package/dist/SchemaAST.js.map +1 -1
  66. package/dist/SchemaParser.d.ts.map +1 -1
  67. package/dist/SchemaParser.js +29 -50
  68. package/dist/SchemaParser.js.map +1 -1
  69. package/dist/ScopedCache.d.ts.map +1 -1
  70. package/dist/ScopedCache.js +6 -4
  71. package/dist/ScopedCache.js.map +1 -1
  72. package/dist/Stream.d.ts +128 -5
  73. package/dist/Stream.d.ts.map +1 -1
  74. package/dist/Stream.js +124 -29
  75. package/dist/Stream.js.map +1 -1
  76. package/dist/String.js +1 -1
  77. package/dist/String.js.map +1 -1
  78. package/dist/Tracer.d.ts +10 -0
  79. package/dist/Tracer.d.ts.map +1 -1
  80. package/dist/Tracer.js +3 -4
  81. package/dist/Tracer.js.map +1 -1
  82. package/dist/ai/McpClient.d.ts +68 -4
  83. package/dist/ai/McpClient.d.ts.map +1 -1
  84. package/dist/ai/McpClient.js +100 -17
  85. package/dist/ai/McpClient.js.map +1 -1
  86. package/dist/ai/McpClientError.d.ts +46 -4
  87. package/dist/ai/McpClientError.d.ts.map +1 -1
  88. package/dist/ai/McpClientError.js +38 -3
  89. package/dist/ai/McpClientError.js.map +1 -1
  90. package/dist/ai/McpServer.d.ts +158 -7
  91. package/dist/ai/McpServer.d.ts.map +1 -1
  92. package/dist/ai/McpServer.js +103 -49
  93. package/dist/ai/McpServer.js.map +1 -1
  94. package/dist/ai/Prompt.d.ts +3 -1
  95. package/dist/ai/Prompt.d.ts.map +1 -1
  96. package/dist/ai/Prompt.js +17 -3
  97. package/dist/ai/Prompt.js.map +1 -1
  98. package/dist/ai/Skill.d.ts +172 -0
  99. package/dist/ai/Skill.d.ts.map +1 -0
  100. package/dist/ai/Skill.js +209 -0
  101. package/dist/ai/Skill.js.map +1 -0
  102. package/dist/ai/index.d.ts +5 -0
  103. package/dist/ai/index.d.ts.map +1 -1
  104. package/dist/ai/index.js +5 -0
  105. package/dist/ai/index.js.map +1 -1
  106. package/dist/ai/internal/mcpProtocol/v2024_11_05.js +9 -7
  107. package/dist/ai/internal/mcpProtocol/v2024_11_05.js.map +1 -1
  108. package/dist/ai/internal/mcpProtocol/v2025_03_26.js +12 -11
  109. package/dist/ai/internal/mcpProtocol/v2025_03_26.js.map +1 -1
  110. package/dist/ai/internal/mcpProtocol/v2025_06_18.js +18 -17
  111. package/dist/ai/internal/mcpProtocol/v2025_06_18.js.map +1 -1
  112. package/dist/ai/internal/mcpProtocol/v2025_11_25.js +19 -18
  113. package/dist/ai/internal/mcpProtocol/v2025_11_25.js.map +1 -1
  114. package/dist/ai/internal/mcpProtocol/v2026_07_28.js +1 -1
  115. package/dist/ai/internal/mcpProtocol/v2026_07_28.js.map +1 -1
  116. package/dist/ai/internal/mcpProtocol.js +11 -0
  117. package/dist/ai/internal/mcpProtocol.js.map +1 -1
  118. package/dist/ai/internal/mcpRuntime.js +29 -17
  119. package/dist/ai/internal/mcpRuntime.js.map +1 -1
  120. package/dist/ai/internal/mcpSchema/v2024_11_05.js +2 -1
  121. package/dist/ai/internal/mcpSchema/v2024_11_05.js.map +1 -1
  122. package/dist/ai/internal/mcpSchema/v2025_03_26.js +1 -8
  123. package/dist/ai/internal/mcpSchema/v2025_03_26.js.map +1 -1
  124. package/dist/ai/internal/mcpSchema/v2025_06_18.js +5 -9
  125. package/dist/ai/internal/mcpSchema/v2025_06_18.js.map +1 -1
  126. package/dist/ai/internal/mcpSchema/v2025_11_25.js +1 -8
  127. package/dist/ai/internal/mcpSchema/v2025_11_25.js.map +1 -1
  128. package/dist/ai/internal/mcpSchema/v2026_07_28.js +4 -3
  129. package/dist/ai/internal/mcpSchema/v2026_07_28.js.map +1 -1
  130. package/dist/cluster/ClusterCron.d.ts +5 -20
  131. package/dist/cluster/ClusterCron.d.ts.map +1 -1
  132. package/dist/cluster/ClusterCron.js +16 -4
  133. package/dist/cluster/ClusterCron.js.map +1 -1
  134. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  135. package/dist/cluster/ClusterWorkflowEngine.js +155 -83
  136. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  137. package/dist/cluster/MessageStorage.d.ts +56 -0
  138. package/dist/cluster/MessageStorage.d.ts.map +1 -1
  139. package/dist/cluster/MessageStorage.js +18 -2
  140. package/dist/cluster/MessageStorage.js.map +1 -1
  141. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  142. package/dist/cluster/SqlMessageStorage.js +48 -23
  143. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  144. package/dist/cluster/internal/entityActivation.js +2 -0
  145. package/dist/cluster/internal/entityActivation.js.map +1 -1
  146. package/dist/cluster/internal/entityManager.js +8 -5
  147. package/dist/cluster/internal/entityManager.js.map +1 -1
  148. package/dist/encoding/Base64.js +5 -1
  149. package/dist/encoding/Base64.js.map +1 -1
  150. package/dist/encoding/Base64Url.d.ts.map +1 -1
  151. package/dist/encoding/Base64Url.js +6 -1
  152. package/dist/encoding/Base64Url.js.map +1 -1
  153. package/dist/encoding/Hex.js +7 -5
  154. package/dist/encoding/Hex.js.map +1 -1
  155. package/dist/encoding/Xml.d.ts +38 -9
  156. package/dist/encoding/Xml.d.ts.map +1 -1
  157. package/dist/encoding/Xml.js +63 -31
  158. package/dist/encoding/Xml.js.map +1 -1
  159. package/dist/http/FetchHttpClient.d.ts +2 -1
  160. package/dist/http/FetchHttpClient.d.ts.map +1 -1
  161. package/dist/http/FetchHttpClient.js +14 -10
  162. package/dist/http/FetchHttpClient.js.map +1 -1
  163. package/dist/http/Headers.d.ts +201 -3
  164. package/dist/http/Headers.d.ts.map +1 -1
  165. package/dist/http/Headers.js +205 -3
  166. package/dist/http/Headers.js.map +1 -1
  167. package/dist/http/HttpClient.d.ts.map +1 -1
  168. package/dist/http/HttpClient.js +11 -7
  169. package/dist/http/HttpClient.js.map +1 -1
  170. package/dist/http/HttpClientError.d.ts +1 -0
  171. package/dist/http/HttpClientError.d.ts.map +1 -1
  172. package/dist/http/HttpClientError.js +32 -9
  173. package/dist/http/HttpClientError.js.map +1 -1
  174. package/dist/http/HttpClientRequest.d.ts +46 -0
  175. package/dist/http/HttpClientRequest.d.ts.map +1 -1
  176. package/dist/http/HttpClientRequest.js +15 -0
  177. package/dist/http/HttpClientRequest.js.map +1 -1
  178. package/dist/http/HttpClientResponse.d.ts +132 -0
  179. package/dist/http/HttpClientResponse.d.ts.map +1 -1
  180. package/dist/http/HttpClientResponse.js +83 -0
  181. package/dist/http/HttpClientResponse.js.map +1 -1
  182. package/dist/http/HttpEffect.d.ts.map +1 -1
  183. package/dist/http/HttpEffect.js +31 -2
  184. package/dist/http/HttpEffect.js.map +1 -1
  185. package/dist/http/HttpMiddleware.js +1 -1
  186. package/dist/http/HttpMiddleware.js.map +1 -1
  187. package/dist/http/HttpServerError.d.ts +1 -1
  188. package/dist/http/HttpServerError.js +1 -1
  189. package/dist/http/HttpServerResponse.d.ts +118 -0
  190. package/dist/http/HttpServerResponse.d.ts.map +1 -1
  191. package/dist/http/HttpServerResponse.js +43 -0
  192. package/dist/http/HttpServerResponse.js.map +1 -1
  193. package/dist/http/Multipart.d.ts.map +1 -1
  194. package/dist/http/Multipart.js +0 -5
  195. package/dist/http/Multipart.js.map +1 -1
  196. package/dist/http/MultipartParser/internal/contentType.d.ts +6 -0
  197. package/dist/http/MultipartParser/internal/contentType.d.ts.map +1 -1
  198. package/dist/http/MultipartParser/internal/contentType.js +1 -1
  199. package/dist/http/MultipartParser/internal/contentType.js.map +1 -1
  200. package/dist/http/internal/clientPath.d.ts +2 -0
  201. package/dist/http/internal/clientPath.d.ts.map +1 -0
  202. package/dist/http/internal/clientPath.js +24 -0
  203. package/dist/http/internal/clientPath.js.map +1 -0
  204. package/dist/http/internal/redactedPath.js +2 -0
  205. package/dist/http/internal/redactedPath.js.map +1 -1
  206. package/dist/http/internal/tracing.js +5 -3
  207. package/dist/http/internal/tracing.js.map +1 -1
  208. package/dist/http-api/HttpApi.d.ts +2 -2
  209. package/dist/http-api/HttpApi.d.ts.map +1 -1
  210. package/dist/http-api/HttpApi.js +30 -7
  211. package/dist/http-api/HttpApi.js.map +1 -1
  212. package/dist/http-api/HttpApiBuilder.d.ts.map +1 -1
  213. package/dist/http-api/HttpApiBuilder.js +5 -2
  214. package/dist/http-api/HttpApiBuilder.js.map +1 -1
  215. package/dist/http-api/HttpApiClient.d.ts.map +1 -1
  216. package/dist/http-api/HttpApiClient.js +23 -6
  217. package/dist/http-api/HttpApiClient.js.map +1 -1
  218. package/dist/http-api/internal/redactedKeys.d.ts +2 -0
  219. package/dist/http-api/internal/redactedKeys.d.ts.map +1 -0
  220. package/dist/http-api/internal/redactedKeys.js +37 -0
  221. package/dist/http-api/internal/redactedKeys.js.map +1 -0
  222. package/dist/internal/base64.js +34 -2
  223. package/dist/internal/base64.js.map +1 -1
  224. package/dist/internal/core.js +60 -43
  225. package/dist/internal/core.js.map +1 -1
  226. package/dist/internal/dateTime.js +35 -17
  227. package/dist/internal/dateTime.js.map +1 -1
  228. package/dist/internal/effect.js +60 -52
  229. package/dist/internal/effect.js.map +1 -1
  230. package/dist/internal/equal.js +44 -13
  231. package/dist/internal/equal.js.map +1 -1
  232. package/dist/internal/fiberCachedKeys.d.ts +2 -0
  233. package/dist/internal/fiberCachedKeys.d.ts.map +1 -0
  234. package/dist/internal/fiberCachedKeys.js +48 -0
  235. package/dist/internal/fiberCachedKeys.js.map +1 -0
  236. package/dist/internal/hashMap.js +1 -1
  237. package/dist/internal/hashMap.js.map +1 -1
  238. package/dist/internal/matcher.js +17 -17
  239. package/dist/internal/matcher.js.map +1 -1
  240. package/dist/internal/predicate.d.ts +2 -0
  241. package/dist/internal/predicate.d.ts.map +1 -0
  242. package/dist/internal/predicate.js +10 -0
  243. package/dist/internal/predicate.js.map +1 -0
  244. package/dist/internal/references.js +8 -11
  245. package/dist/internal/references.js.map +1 -1
  246. package/dist/internal/schema/schemaCodegen.js +3 -2
  247. package/dist/internal/schema/schemaCodegen.js.map +1 -1
  248. package/dist/internal/schema/schemaCodegenBuiltIns.d.ts +2 -0
  249. package/dist/internal/schema/schemaCodegenBuiltIns.d.ts.map +1 -0
  250. package/dist/internal/schema/schemaCodegenBuiltIns.js +425 -0
  251. package/dist/internal/schema/schemaCodegenBuiltIns.js.map +1 -0
  252. package/dist/internal/schema/toEquivalence.js +3 -4
  253. package/dist/internal/schema/toEquivalence.js.map +1 -1
  254. package/dist/internal/version.js +1 -1
  255. package/dist/jose/JoseError.d.ts +51 -1
  256. package/dist/jose/JoseError.d.ts.map +1 -1
  257. package/dist/jose/JoseError.js +25 -18
  258. package/dist/jose/JoseError.js.map +1 -1
  259. package/dist/jose/internal/compact.js +10 -5
  260. package/dist/jose/internal/compact.js.map +1 -1
  261. package/dist/observability/Otlp.d.ts +19 -0
  262. package/dist/observability/Otlp.d.ts.map +1 -1
  263. package/dist/observability/Otlp.js +10 -0
  264. package/dist/observability/Otlp.js.map +1 -1
  265. package/dist/observability/OtlpExporter.d.ts +49 -13
  266. package/dist/observability/OtlpExporter.d.ts.map +1 -1
  267. package/dist/observability/OtlpExporter.js +135 -54
  268. package/dist/observability/OtlpExporter.js.map +1 -1
  269. package/dist/observability/OtlpLogger.d.ts +14 -0
  270. package/dist/observability/OtlpLogger.d.ts.map +1 -1
  271. package/dist/observability/OtlpLogger.js +10 -2
  272. package/dist/observability/OtlpLogger.js.map +1 -1
  273. package/dist/observability/OtlpMetrics.d.ts.map +1 -1
  274. package/dist/observability/OtlpMetrics.js +2 -0
  275. package/dist/observability/OtlpMetrics.js.map +1 -1
  276. package/dist/observability/OtlpTracer.d.ts +14 -0
  277. package/dist/observability/OtlpTracer.d.ts.map +1 -1
  278. package/dist/observability/OtlpTracer.js +10 -2
  279. package/dist/observability/OtlpTracer.js.map +1 -1
  280. package/dist/persistence/KeyValueStore.d.ts.map +1 -1
  281. package/dist/persistence/KeyValueStore.js +6 -4
  282. package/dist/persistence/KeyValueStore.js.map +1 -1
  283. package/dist/reactivity/Atom.d.ts.map +1 -1
  284. package/dist/reactivity/Atom.js +10 -9
  285. package/dist/reactivity/Atom.js.map +1 -1
  286. package/dist/rpc/RpcClient.d.ts.map +1 -1
  287. package/dist/rpc/RpcClient.js +32 -1
  288. package/dist/rpc/RpcClient.js.map +1 -1
  289. package/dist/rpc/RpcGroup.d.ts +1 -1
  290. package/dist/rpc/RpcGroup.d.ts.map +1 -1
  291. package/dist/rpc/RpcGroup.js +4 -0
  292. package/dist/rpc/RpcGroup.js.map +1 -1
  293. package/dist/sql/Migrator.d.ts.map +1 -1
  294. package/dist/sql/Migrator.js +2 -5
  295. package/dist/sql/Migrator.js.map +1 -1
  296. package/dist/sql/SqlClient.d.ts.map +1 -1
  297. package/dist/sql/SqlClient.js +5 -1
  298. package/dist/sql/SqlClient.js.map +1 -1
  299. package/dist/sql/Statement.d.ts +23 -0
  300. package/dist/sql/Statement.d.ts.map +1 -1
  301. package/dist/sql/Statement.js +30 -5
  302. package/dist/sql/Statement.js.map +1 -1
  303. package/dist/workers/Worker.d.ts +21 -0
  304. package/dist/workers/Worker.d.ts.map +1 -1
  305. package/dist/workers/Worker.js +23 -0
  306. package/dist/workers/Worker.js.map +1 -1
  307. package/dist/workflow/Activity.d.ts +18 -0
  308. package/dist/workflow/Activity.d.ts.map +1 -1
  309. package/dist/workflow/Activity.js +13 -4
  310. package/dist/workflow/Activity.js.map +1 -1
  311. package/dist/workflow/Workflow.d.ts +10 -9
  312. package/dist/workflow/Workflow.d.ts.map +1 -1
  313. package/dist/workflow/Workflow.js +33 -22
  314. package/dist/workflow/Workflow.js.map +1 -1
  315. package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
  316. package/dist/workflow/WorkflowEngine.js +26 -12
  317. package/dist/workflow/WorkflowEngine.js.map +1 -1
  318. package/dist/workflow/internal/retry.d.ts +2 -0
  319. package/dist/workflow/internal/retry.d.ts.map +1 -0
  320. package/dist/workflow/internal/retry.js +11 -0
  321. package/dist/workflow/internal/retry.js.map +1 -0
  322. package/dist/workflow/internal/scope.d.ts +2 -0
  323. package/dist/workflow/internal/scope.d.ts.map +1 -0
  324. package/dist/workflow/internal/scope.js +26 -0
  325. package/dist/workflow/internal/scope.js.map +1 -0
  326. package/package.json +3 -3
  327. package/src/Channel.ts +105 -8
  328. package/src/Codegen.ts +7 -2
  329. package/src/Config.ts +6 -3
  330. package/src/Context.ts +108 -50
  331. package/src/Cron.ts +11 -10
  332. package/src/Effect.ts +224 -48
  333. package/src/Equal.ts +93 -80
  334. package/src/Formatter.ts +4 -4
  335. package/src/Hash.ts +49 -48
  336. package/src/LayerRef.ts +6 -4
  337. package/src/Logger.ts +68 -25
  338. package/src/Match.ts +78 -50
  339. package/src/Metric.ts +4 -4
  340. package/src/Order.ts +1 -1
  341. package/src/Pool.ts +62 -33
  342. package/src/Predicate.ts +2 -5
  343. package/src/Scheduler.ts +27 -10
  344. package/src/Schema.ts +79 -296
  345. package/src/SchemaAST.ts +231 -157
  346. package/src/SchemaParser.ts +40 -57
  347. package/src/ScopedCache.ts +6 -4
  348. package/src/Stream.ts +167 -33
  349. package/src/String.ts +1 -1
  350. package/src/Tracer.ts +11 -2
  351. package/src/ai/McpClient.ts +157 -15
  352. package/src/ai/McpClientError.ts +55 -4
  353. package/src/ai/McpServer.ts +318 -91
  354. package/src/ai/Prompt.ts +20 -3
  355. package/src/ai/Skill.ts +236 -0
  356. package/src/ai/index.ts +6 -0
  357. package/src/ai/internal/mcpProtocol/v2024_11_05.ts +8 -13
  358. package/src/ai/internal/mcpProtocol/v2025_03_26.ts +13 -15
  359. package/src/ai/internal/mcpProtocol/v2025_06_18.ts +21 -23
  360. package/src/ai/internal/mcpProtocol/v2025_11_25.ts +22 -24
  361. package/src/ai/internal/mcpProtocol/v2026_07_28.ts +1 -1
  362. package/src/ai/internal/mcpProtocol.ts +15 -0
  363. package/src/ai/internal/mcpRuntime.ts +36 -13
  364. package/src/ai/internal/mcpSchema/v2024_11_05.ts +2 -1
  365. package/src/ai/internal/mcpSchema/v2025_03_26.ts +1 -9
  366. package/src/ai/internal/mcpSchema/v2025_06_18.ts +3 -10
  367. package/src/ai/internal/mcpSchema/v2025_11_25.ts +1 -9
  368. package/src/ai/internal/mcpSchema/v2026_07_28.ts +6 -8
  369. package/src/cluster/ClusterCron.ts +31 -20
  370. package/src/cluster/ClusterWorkflowEngine.ts +172 -96
  371. package/src/cluster/MessageStorage.ts +51 -2
  372. package/src/cluster/SqlMessageStorage.ts +88 -36
  373. package/src/cluster/internal/entityActivation.ts +6 -0
  374. package/src/cluster/internal/entityManager.ts +8 -4
  375. package/src/encoding/Base64.ts +5 -1
  376. package/src/encoding/Base64Url.ts +6 -1
  377. package/src/encoding/Hex.ts +5 -5
  378. package/src/encoding/Xml.ts +96 -26
  379. package/src/http/FetchHttpClient.ts +8 -5
  380. package/src/http/Headers.ts +309 -3
  381. package/src/http/HttpClient.ts +19 -7
  382. package/src/http/HttpClientError.ts +42 -9
  383. package/src/http/HttpClientRequest.ts +51 -0
  384. package/src/http/HttpClientResponse.ts +156 -0
  385. package/src/http/HttpEffect.ts +23 -3
  386. package/src/http/HttpMiddleware.ts +1 -1
  387. package/src/http/HttpServerError.ts +1 -1
  388. package/src/http/HttpServerResponse.ts +126 -0
  389. package/src/http/Multipart.ts +0 -5
  390. package/src/http/MultipartParser/internal/contentType.ts +1 -1
  391. package/src/http/internal/clientPath.ts +39 -0
  392. package/src/http/internal/redactedPath.ts +4 -0
  393. package/src/http/internal/tracing.ts +6 -3
  394. package/src/http-api/HttpApi.ts +29 -8
  395. package/src/http-api/HttpApiBuilder.ts +7 -2
  396. package/src/http-api/HttpApiClient.ts +30 -7
  397. package/src/http-api/internal/redactedKeys.ts +40 -0
  398. package/src/internal/base64.ts +57 -2
  399. package/src/internal/core.ts +80 -44
  400. package/src/internal/dateTime.ts +43 -24
  401. package/src/internal/effect.ts +115 -157
  402. package/src/internal/equal.ts +73 -17
  403. package/src/internal/fiberCachedKeys.ts +81 -0
  404. package/src/internal/hashMap.ts +1 -1
  405. package/src/internal/matcher.ts +34 -113
  406. package/src/internal/predicate.ts +10 -0
  407. package/src/internal/references.ts +21 -14
  408. package/src/internal/schema/schemaCodegen.ts +5 -4
  409. package/src/internal/schema/schemaCodegenBuiltIns.ts +343 -0
  410. package/src/internal/schema/toEquivalence.ts +3 -5
  411. package/src/internal/version.ts +1 -1
  412. package/src/jose/JoseError.ts +34 -0
  413. package/src/jose/internal/compact.ts +14 -5
  414. package/src/observability/Otlp.ts +29 -0
  415. package/src/observability/OtlpExporter.ts +206 -75
  416. package/src/observability/OtlpLogger.ts +24 -2
  417. package/src/observability/OtlpMetrics.ts +2 -0
  418. package/src/observability/OtlpTracer.ts +24 -3
  419. package/src/persistence/KeyValueStore.ts +17 -14
  420. package/src/reactivity/Atom.ts +12 -18
  421. package/src/rpc/RpcClient.ts +26 -1
  422. package/src/rpc/RpcGroup.ts +4 -1
  423. package/src/sql/Migrator.ts +2 -11
  424. package/src/sql/SqlClient.ts +14 -8
  425. package/src/sql/Statement.ts +48 -12
  426. package/src/workers/Worker.ts +25 -0
  427. package/src/workflow/Activity.ts +28 -4
  428. package/src/workflow/Workflow.ts +68 -44
  429. package/src/workflow/WorkflowEngine.ts +38 -15
  430. package/src/workflow/internal/retry.ts +12 -0
  431. package/src/workflow/internal/scope.ts +36 -0
  432. package/dist/internal/hash.d.ts +0 -2
  433. package/dist/internal/hash.d.ts.map +0 -1
  434. package/dist/internal/hash.js +0 -11
  435. package/dist/internal/hash.js.map +0 -1
  436. package/dist/internal/metric.d.ts +0 -2
  437. package/dist/internal/metric.d.ts.map +0 -1
  438. package/dist/internal/metric.js +0 -3
  439. package/dist/internal/metric.js.map +0 -1
  440. package/src/internal/hash.ts +0 -11
  441. package/src/internal/metric.ts +0 -2
package/src/SchemaAST.ts CHANGED
@@ -717,6 +717,14 @@ abstract class ASTNodeImpl implements ASTNode {
717
717
  }
718
718
  }
719
719
 
720
+ function flipChecks<A extends Declaration | Arrays | Objects | Union>(ast: A): A {
721
+ return ast.checks === ast.encodingChecks ? ast : copy(ast, {
722
+ checks: ast.encodingChecks,
723
+ encodingChecks: ast.checks,
724
+ encoding: undefined
725
+ })
726
+ }
727
+
720
728
  type DeclarationRun = (
721
729
  typeParameters: ReadonlyArray<AST>
722
730
  ) => (input: unknown, self: Declaration, options: ParseOptions) => Effect.Effect<any, SchemaIssue.Issue>
@@ -823,26 +831,19 @@ export const Declaration: new(
823
831
  }
824
832
  }
825
833
  }
826
- private _rebuild(
827
- recur: (ast: AST) => AST,
828
- checks: Checks | undefined,
829
- encodingChecks: Checks | undefined,
830
- run: DeclarationRun,
831
- encodingRun: DeclarationRun | undefined
832
- ) {
833
- const tps = mapOrSame(this.typeParameters, recur)
834
- return tps === this.typeParameters && checks === this.checks && encodingChecks === this.encodingChecks &&
835
- run === this.run && encodingRun === this.encodingRun ?
836
- this :
837
- new Declaration(tps, run, this.annotations, checks, undefined, this.context, encodingChecks, encodingRun)
838
- }
839
834
  /** @internal */
840
- recur(recur: (ast: AST) => AST) {
841
- return this._rebuild(recur, this.checks, this.encodingChecks, this.run, this.encodingRun)
835
+ recur(recur: (ast: AST) => AST): Declaration {
836
+ const typeParameters = mapOrSame(this.typeParameters, recur)
837
+ return typeParameters === this.typeParameters ? this : copy(this, { typeParameters, encoding: undefined })
842
838
  }
843
839
  /** @internal */
844
- flip(recur: (ast: AST) => AST) {
845
- return this._rebuild(recur, this.encodingChecks, this.checks, this.encodingRun ?? this.run, this.run)
840
+ flip(recur: (ast: AST) => AST): Declaration {
841
+ const out = flipChecks(this.recur(recur))
842
+ return this.run === this.encodingRun ? out : copy(out, {
843
+ run: this.encodingRun ?? this.run,
844
+ encodingRun: this.run,
845
+ encoding: undefined
846
+ })
846
847
  }
847
848
  /** @internal */
848
849
  getExpected(): string {
@@ -2486,30 +2487,26 @@ export const Arrays: new(
2486
2487
  }
2487
2488
  }
2488
2489
  }
2489
- private _rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
2490
+ /** @internal */
2491
+ recur(recur: (ast: AST) => AST): Arrays {
2490
2492
  const elements = mapOrSame(this.elements, recur)
2491
2493
  const rest = mapOrSame(this.rest, recur)
2492
- return elements === this.elements && rest === this.rest && checks === this.checks &&
2493
- encodingChecks === this.encodingChecks ?
2494
+ return elements === this.elements && rest === this.rest ?
2494
2495
  this :
2495
2496
  new Arrays(
2496
2497
  this.isMutable,
2497
2498
  elements,
2498
2499
  rest,
2499
2500
  this.annotations,
2500
- checks,
2501
+ this.checks,
2501
2502
  undefined,
2502
2503
  this.context,
2503
- encodingChecks
2504
+ this.encodingChecks
2504
2505
  )
2505
2506
  }
2506
2507
  /** @internal */
2507
- recur(recur: (ast: AST) => AST) {
2508
- return this._rebuild(recur, this.checks, this.encodingChecks)
2509
- }
2510
- /** @internal */
2511
- flip(recur: (ast: AST) => AST) {
2512
- return this._rebuild(recur, this.encodingChecks, this.checks)
2508
+ flip(recur: (ast: AST) => AST): Arrays {
2509
+ return flipChecks(this.recur(recur))
2513
2510
  }
2514
2511
  /** @internal */
2515
2512
  getExpected(): string {
@@ -3181,12 +3178,8 @@ export const Objects: new(
3181
3178
  return InternalParser.succeed(out)
3182
3179
  }
3183
3180
  }
3184
- private _rebuild(
3185
- recur: (ast: AST) => AST,
3186
- recurParameter: (ast: AST) => AST,
3187
- checks: Checks | undefined,
3188
- encodingChecks: Checks | undefined
3189
- ): Objects {
3181
+ /** @internal */
3182
+ recur(recur: (ast: AST) => AST, recurParameter: (ast: AST) => AST = recur): Objects {
3190
3183
  const props = mapOrSame(this.propertySignatures, (ps) => {
3191
3184
  const t = recur(ps.type)
3192
3185
  return t === ps.type ? ps : new PropertySignature(ps.name, t)
@@ -3200,26 +3193,13 @@ export const Objects: new(
3200
3193
  : new IndexSignature(p, t)
3201
3194
  })
3202
3195
 
3203
- return props === this.propertySignatures && indexes === this.indexSignatures && checks === this.checks &&
3204
- encodingChecks === this.encodingChecks
3205
- ? this
3206
- : new Objects(
3207
- props,
3208
- indexes,
3209
- this.annotations,
3210
- checks,
3211
- undefined,
3212
- this.context,
3213
- encodingChecks
3214
- )
3215
- }
3216
- /** @internal */
3217
- flip(recur: (ast: AST) => AST): AST {
3218
- return this._rebuild(recur, recur, this.encodingChecks, this.checks)
3196
+ return props === this.propertySignatures && indexes === this.indexSignatures ?
3197
+ this :
3198
+ copy(this, { propertySignatures: props, indexSignatures: indexes, encoding: undefined })
3219
3199
  }
3220
3200
  /** @internal */
3221
- recur(recur: (ast: AST) => AST, recurParameter: (ast: AST) => AST = recur): AST {
3222
- return this._rebuild(recur, recurParameter, this.checks, this.encodingChecks)
3201
+ flip(recur: (ast: AST) => AST): Objects {
3202
+ return flipChecks(this.recur(recur))
3223
3203
  }
3224
3204
  /** @internal */
3225
3205
  getExpected(): string {
@@ -3814,23 +3794,14 @@ export const Union: new<A extends AST = AST>(
3814
3794
  return parseUnionCandidates(ast, parser, candidates, input, options)
3815
3795
  }
3816
3796
  }
3817
- private _rebuild(
3818
- recur: (ast: AST) => AST,
3819
- checks: Checks | undefined,
3820
- encodingChecks: Checks | undefined
3821
- ): Union<AST> {
3822
- const types = mapOrSame(this.types, recur)
3823
- return types === this.types && checks === this.checks && encodingChecks === this.encodingChecks ?
3824
- this :
3825
- new Union(types, this.options, this.annotations, checks, undefined, this.context, encodingChecks)
3826
- }
3827
3797
  /** @internal */
3828
3798
  recur(recur: (ast: AST) => AST): Union<AST> {
3829
- return this._rebuild(recur, this.checks, this.encodingChecks)
3799
+ const types = mapOrSame(this.types, recur)
3800
+ return types === this.types ? this : copy<Union<AST>>(this, { types, encoding: undefined })
3830
3801
  }
3831
3802
  /** @internal */
3832
3803
  flip(recur: (ast: AST) => AST): Union<AST> {
3833
- return this._rebuild(recur, this.encodingChecks, this.checks)
3804
+ return flipChecks(this.recur(recur))
3834
3805
  }
3835
3806
  /** @internal */
3836
3807
  matchPart(s: string, options: ParseOptions): LiteralValue | undefined {
@@ -4065,8 +4036,7 @@ export const Suspend: new(
4065
4036
  throw new Error("Cannot add checks to Suspend")
4066
4037
  }
4067
4038
  super(annotations, undefined, encoding, context)
4068
- let ast: AST
4069
- this.thunk = () => ast ??= thunk()
4039
+ this.thunk = memoizeThunk(thunk)
4070
4040
  }
4071
4041
  /** @internal */
4072
4042
  getParser(compile: SchemaParser.Compiler): SchemaParser.Parser {
@@ -4089,6 +4059,11 @@ export const Suspend: new(
4089
4059
  }
4090
4060
  }
4091
4061
 
4062
+ function memoizeThunk(thunk: Suspend["thunk"]): Suspend["thunk"] {
4063
+ let ast: AST
4064
+ return () => ast ??= thunk()
4065
+ }
4066
+
4092
4067
  // -----------------------------------------------------------------------------
4093
4068
  // Checks
4094
4069
  // -----------------------------------------------------------------------------
@@ -4288,7 +4263,6 @@ export function isFinite(annotations?: Schema.Annotations.Filter) {
4288
4263
  payload: null
4289
4264
  },
4290
4265
  toJsonSchema: () => ({ type: "number" }),
4291
- toCode: () => ({ runtime: "Schema.isFinite()" }),
4292
4266
  arbitraryConstraint: {
4293
4267
  number: "finite"
4294
4268
  },
@@ -4379,11 +4353,12 @@ export function isPattern(regExp: globalThis.RegExp, annotations?: Schema.Annota
4379
4353
  const bodyOwners = new WeakMap<AST, AST>()
4380
4354
 
4381
4355
  function copy<A extends AST>(ast: A, changes: Partial<AST>): A {
4382
- // AST copies preserve the prototype and enumerable values, not property descriptors.
4383
- const out = Object.assign(Object.create(Object.getPrototypeOf(ast)), ast, changes) as A
4384
- if (Reflect.ownKeys(changes).every((key) => key === "context" || key === "encoding")) {
4385
- bodyOwners.set(out, getContextOwner(ast))
4386
- }
4356
+ return Object.assign(Object.create(Object.getPrototypeOf(ast)), ast, changes) as A
4357
+ }
4358
+
4359
+ function copyBody<A extends AST>(ast: A, changes: Pick<Partial<AST>, "context" | "encoding">): A {
4360
+ const out = copy(ast, changes)
4361
+ bodyOwners.set(out, getContextOwner(ast))
4387
4362
  return out
4388
4363
  }
4389
4364
 
@@ -4393,14 +4368,14 @@ export function getContextOwner<A extends AST>(ast: A): A {
4393
4368
  if (existing !== undefined) return existing as A
4394
4369
  if (ast.encoding === undefined) return ast
4395
4370
  // A body representative must not expose or retain the node's own codec.
4396
- const owner = Object.assign(Object.create(Object.getPrototypeOf(ast)), ast, { encoding: undefined }) as A
4371
+ const owner = copy(ast, { encoding: undefined })
4397
4372
  bodyOwners.set(ast, owner)
4398
4373
  return owner
4399
4374
  }
4400
4375
 
4401
4376
  /** @internal */
4402
4377
  export function replaceEncoding<A extends AST>(ast: A, encoding: Encoding | undefined): A {
4403
- return ast.encoding === encoding ? ast : copy(ast, { encoding })
4378
+ return ast.encoding === encoding ? ast : copyBody(ast, { encoding })
4404
4379
  }
4405
4380
 
4406
4381
  /** @internal */
@@ -4412,7 +4387,7 @@ export function replaceContext<A extends AST>(ast: A, context: Context | undefin
4412
4387
  if (owner.context === context && owner.encoding === ast.encoding) {
4413
4388
  return owner
4414
4389
  }
4415
- return copy(ast, { context })
4390
+ return copyBody(ast, { context })
4416
4391
  }
4417
4392
 
4418
4393
  /** @internal */
@@ -4423,8 +4398,7 @@ export function getLastEncoding(ast: AST): AST {
4423
4398
  /** @internal */
4424
4399
  export function annotate<A extends AST>(ast: A, annotations: Schema.Annotations.Annotations): A {
4425
4400
  if (ast.checks) {
4426
- const last = ast.checks[ast.checks.length - 1]
4427
- return replaceChecks(ast, Arr.append(ast.checks.slice(0, -1), last.annotate(annotations)))
4401
+ return replaceChecks(ast, mapLast(ast.checks, (check) => check.annotate(annotations)))
4428
4402
  }
4429
4403
  return copy(ast, { annotations: { ...ast.annotations, ...annotations } })
4430
4404
  }
@@ -4451,11 +4425,17 @@ export function mapLink(link: Link, f: (ast: AST) => AST): Link {
4451
4425
  return to === link.to ? link : new Link(to, link.transformation)
4452
4426
  }
4453
4427
 
4428
+ function mapLast<A>(items: readonly [A, ...Array<A>], f: (item: A) => A): readonly [A, ...Array<A>] {
4429
+ const last = items[items.length - 1]
4430
+ const out = f(last)
4431
+ if (last === out) return items
4432
+ const copy: [A, ...Array<A>] = [...items]
4433
+ copy[copy.length - 1] = out
4434
+ return copy
4435
+ }
4436
+
4454
4437
  function updateLastLink(encoding: Encoding, f: (ast: AST) => AST): Encoding {
4455
- const links = encoding
4456
- const last = links[links.length - 1]
4457
- const out = mapLink(last, f)
4458
- return out === last ? encoding : Arr.append(encoding.slice(0, encoding.length - 1), out)
4438
+ return mapLast(encoding, (link) => mapLink(link, f))
4459
4439
  }
4460
4440
 
4461
4441
  /** @internal */
@@ -4542,30 +4522,39 @@ export function mapOrSame<A>(as: ReadonlyArray<A>, f: (a: A) => A): ReadonlyArra
4542
4522
  return out ?? as
4543
4523
  }
4544
4524
 
4525
+ function updateContext<A extends AST>(
4526
+ ast: A,
4527
+ optional = isOptional(ast),
4528
+ mutable = isMutable(ast),
4529
+ constructorDefault = ast.context?.constructorDefault,
4530
+ annotations = ast.context?.annotations
4531
+ ): A {
4532
+ return replaceContext(ast, new Context(optional, mutable, constructorDefault, annotations))
4533
+ }
4534
+
4535
+ function setContextFlag(key: "isOptional" | "isMutable") {
4536
+ const optional = key === "isOptional" ? true : undefined
4537
+ const mutable = key === "isMutable" ? true : undefined
4538
+ const set: <A extends AST>(ast: A) => A = memoizeIdempotent(<A extends AST>(ast: A): A =>
4539
+ last(ast.context?.[key] ? ast : updateContext(ast, optional, mutable))
4540
+ )
4541
+ const last = applyToLastLink(set)
4542
+ return set
4543
+ }
4544
+
4545
4545
  /** @internal */
4546
4546
  export function annotateKey<A extends AST>(ast: A, annotations: Schema.Annotations.Key<unknown>): A {
4547
- const context = ast.context ?
4548
- new Context(
4549
- ast.context.isOptional,
4550
- ast.context.isMutable,
4551
- ast.context.constructorDefault,
4552
- { ...ast.context.annotations, ...annotations }
4553
- ) :
4554
- new Context(false, false, undefined, annotations)
4555
- return replaceContext(ast, context)
4547
+ return updateContext(
4548
+ ast,
4549
+ undefined,
4550
+ undefined,
4551
+ undefined,
4552
+ ast.context ? { ...ast.context.annotations, ...annotations } : annotations
4553
+ )
4556
4554
  }
4557
4555
 
4558
4556
  /** @internal */
4559
- export const optionalKey: <A extends AST>(ast: A) => A = memoizeIdempotent(<A extends AST>(ast: A): A => {
4560
- const context = ast.context ?
4561
- ast.context.isOptional === false ?
4562
- new Context(true, ast.context.isMutable, ast.context.constructorDefault, ast.context.annotations) :
4563
- ast.context :
4564
- new Context(true, false)
4565
- return optionalKeyLastLink(replaceContext(ast, context))
4566
- })
4567
-
4568
- const optionalKeyLastLink = applyToLastLink(optionalKey)
4557
+ export const optionalKey: <A extends AST>(ast: A) => A = setContextFlag("isOptional")
4569
4558
 
4570
4559
  /** @internal */
4571
4560
  export const optional = memoize(<A extends AST>(ast: A): Union<A | Undefined> =>
@@ -4573,26 +4562,14 @@ export const optional = memoize(<A extends AST>(ast: A): Union<A | Undefined> =>
4573
4562
  )
4574
4563
 
4575
4564
  /** @internal */
4576
- export const mutableKey = memoizeIdempotent(<A extends AST>(ast: A): A => {
4577
- const context = ast.context ?
4578
- ast.context.isMutable === false ?
4579
- new Context(ast.context.isOptional, true, ast.context.constructorDefault, ast.context.annotations) :
4580
- ast.context :
4581
- new Context(false, true)
4582
- return mutableKeyLastLink(replaceContext(ast, context))
4583
- })
4584
-
4585
- const mutableKeyLastLink = applyToLastLink(mutableKey)
4565
+ export const mutableKey = setContextFlag("isMutable")
4586
4566
 
4587
4567
  /** @internal */
4588
4568
  export function withConstructorDefault<A extends AST>(
4589
4569
  ast: A,
4590
4570
  defaultValue: Effect.Effect<unknown, SchemaIssue.Issue>
4591
4571
  ): A {
4592
- const context = ast.context ?
4593
- new Context(ast.context.isOptional, ast.context.isMutable, defaultValue, ast.context.annotations) :
4594
- new Context(false, false, defaultValue)
4595
- return replaceContext(ast, context)
4572
+ return updateContext(ast, undefined, undefined, defaultValue)
4596
4573
  }
4597
4574
 
4598
4575
  /**
@@ -4712,17 +4689,64 @@ function extractStructuralChecks(checks: Checks): Checks | undefined {
4712
4689
  return Arr.isArrayNonEmpty(out) ? out : undefined
4713
4690
  }
4714
4691
 
4715
- function canPreserveEncodingChecks(ast: AST): boolean {
4716
- let preserve = true
4692
+ // Suspensions are leaves in this walk. Only resolving a suspension may call its thunk.
4693
+ function everyNode(root: AST, predicate: (ast: AST) => boolean, proven?: WeakSet<AST>): boolean {
4694
+ const nodes = new Set([root])
4695
+ for (const ast of nodes) {
4696
+ if (proven?.has(ast)) continue
4697
+ if (!predicate(ast)) return false
4698
+ if (!isSuspend(ast) && "recur" in ast) ast.recur(visit)
4699
+ }
4700
+ if (proven) {
4701
+ for (const ast of nodes) {
4702
+ proven.add(ast)
4703
+ }
4704
+ }
4705
+ return true
4706
+
4717
4707
  function visit(child: AST): AST {
4718
- preserve = preserve && !child.encoding && !isSuspend(child)
4719
- if (preserve && "recur" in child) child.recur(visit)
4708
+ nodes.add(child)
4720
4709
  return child
4721
4710
  }
4722
- if ("recur" in ast) ast.recur(visit)
4723
- return preserve
4724
4711
  }
4725
4712
 
4713
+ const preservesShape = new WeakSet<AST>()
4714
+
4715
+ function projectChecks(ast: AST, checks: Checks): Checks | undefined {
4716
+ const structural = isArrays(ast) || isObjects(ast) || isDeclaration(ast) && ast.typeParameters.length > 0
4717
+ if (
4718
+ structural && checks.every(isStructuralCheck) ||
4719
+ everyNode(ast, (node) => !node.encoding && !isSuspend(node), preservesShape)
4720
+ ) {
4721
+ return checks
4722
+ }
4723
+ if (structural) return extractStructuralChecks(checks)
4724
+ }
4725
+
4726
+ function hasDirectionalParser(ast: AST): boolean {
4727
+ return isDeclaration(ast) && ast.encodingRun !== undefined && ast.encodingRun !== ast.run
4728
+ }
4729
+
4730
+ function canReuse(root: AST, edge: Suspend["thunk"]): boolean {
4731
+ function same(ast: AST): boolean {
4732
+ if (isSuspend(ast)) return ast.thunk === edge
4733
+ return !hasDirectionalParser(ast) &&
4734
+ !("encodingChecks" in ast && ast.checks && projectChecks(ast, ast.checks) !== ast.checks)
4735
+ }
4736
+ return everyNode(root, same)
4737
+ }
4738
+
4739
+ const types = new WeakMap<AST, AST>()
4740
+ const reversed = new WeakMap<AST, AST>()
4741
+ const encoded = new WeakSet<AST>()
4742
+ const reprojected = new WeakSet<AST>()
4743
+ const flippedFrom = new WeakMap<AST, AST>()
4744
+ interface FlippedSuspension {
4745
+ readonly source: Suspend
4746
+ project(ast: Suspend): Suspend
4747
+ }
4748
+ const flippedThunks = new WeakMap<Suspend["thunk"], FlippedSuspension>()
4749
+
4726
4750
  /**
4727
4751
  * Strips all encoding transformations from an AST, returning the decoded
4728
4752
  * (type-level) representation.
@@ -4749,27 +4773,52 @@ function canPreserveEncodingChecks(ast: AST): boolean {
4749
4773
  * @category transforming
4750
4774
  * @since 4.0.0
4751
4775
  */
4752
- export const toType = memoizeIdempotent(<A extends AST>(ast: A): A => {
4776
+ export const toType = <A extends AST>(ast: A): A => flippedFrom.has(ast) ? toFlippedType(ast) : stripEncoding(ast)
4777
+
4778
+ const toFlippedType = memoize(<A extends AST>(ast: A): A => {
4779
+ const source = flippedFrom.get(ast)
4780
+ // The public encoded projection is idempotent, including when requested as toType(flip(ast)).
4781
+ if (source && encoded.has(source)) return source as A
4782
+ const out = stripEncoding(ast)
4783
+ if (source) encoded.add(out)
4784
+ return out
4785
+ })
4786
+
4787
+ function stripEncoding<A extends AST>(ast: A): A {
4788
+ const cached = types.get(ast)
4789
+ if (cached) return cached as A
4790
+ const source = flippedFrom.get(ast)
4791
+ const wasProjected = source && types.get(source) === source
4792
+ const out = source && reversed.get(source) || projectType(ast)
4793
+ if (wasProjected) reversed.set(source, out)
4794
+ types.set(ast, out)
4795
+ // Projected nodes map to themselves, so the cache also identifies completed projections.
4796
+ if (out !== ast) types.set(out, out)
4797
+ return out as A
4798
+ }
4799
+
4800
+ function projectOwner<A extends AST>(ast: A, project: (ast: A) => AST): A | undefined {
4753
4801
  const owner = getContextOwner(ast)
4754
4802
  if (owner !== ast) {
4755
- const type = toType(owner)
4756
- return type === owner && ast.encoding === undefined ? ast : replaceContext(type, ast.context)
4803
+ const out = project(owner)
4804
+ return out === owner && !ast.encoding ? ast : replaceContext(out, ast.context) as A
4757
4805
  }
4758
- const type = ("recur" in ast ? ast.recur(toType) : ast) as A
4806
+ }
4807
+
4808
+ function projectType<A extends AST>(ast: A): A {
4809
+ const owned = projectOwner(ast, stripEncoding)
4810
+ if (owned) return owned
4811
+ if (isSuspend(ast)) return (flippedThunks.get(ast.thunk)?.project(ast) ?? ast.recur(stripEncoding)) as A
4812
+ const type = ("recur" in ast ? ast.recur(stripEncoding) : ast) as A
4759
4813
  if ("encodingChecks" in type && type.encodingChecks) {
4760
- const checks = canPreserveEncodingChecks(ast)
4761
- ? type.encodingChecks
4762
- : isArrays(type) || isObjects(type) || isDeclaration(type) && type.typeParameters.length > 0
4763
- ? extractStructuralChecks(type.encodingChecks)
4764
- : undefined
4765
4814
  return copy(type, {
4766
4815
  encodingChecks: undefined,
4767
- // Match parsing order and preserve annotation precedence for decoded checks.
4768
- checks: combineChecks(checks, type.checks)
4816
+ checks: combineChecks(projectChecks(ast, type.encodingChecks), type.checks)
4769
4817
  })
4770
4818
  }
4771
- return type
4772
- })
4819
+ // A flipped declaration and its encoded projection need distinct reverse parsers.
4820
+ return type === ast && hasDirectionalParser(type) && flippedFrom.has(ast) ? copy(type, {}) : type
4821
+ }
4773
4822
 
4774
4823
  /**
4775
4824
  * Returns the encoded (wire-format) AST by flipping and then stripping
@@ -4798,26 +4847,17 @@ export const toType = memoizeIdempotent(<A extends AST>(ast: A): A => {
4798
4847
  * @category transforming
4799
4848
  * @since 4.0.0
4800
4849
  */
4801
- export const toEncoded = memoizeIdempotent((ast: AST): AST => {
4802
- return toType(flip(ast))
4803
- })
4850
+ export const toEncoded = (ast: AST): AST => toType(flip(ast))
4804
4851
 
4805
4852
  function flipEncoding(ast: AST, encoding: Encoding): AST {
4806
- const links = encoding
4807
- const len = links.length
4808
- const last = links[len - 1]
4809
- const ls: Arr.NonEmptyArray<Link> = [
4810
- new Link(flip(replaceEncoding(ast, undefined)), links[0].transformation.flip())
4811
- ]
4812
- for (let i = 1; i < len; i++) {
4813
- ls.unshift(new Link(flip(links[i - 1].to), links[i].transformation.flip()))
4814
- }
4815
- const to = flip(last.to)
4816
- if (to.encoding) {
4817
- return replaceEncoding(to, [...to.encoding, ...ls])
4818
- } else {
4819
- return replaceEncoding(to, ls)
4820
- }
4853
+ let from = replaceEncoding(ast, undefined)
4854
+ const reversed = encoding.map((link) => {
4855
+ const out = new Link(flip(from), link.transformation.flip())
4856
+ from = link.to
4857
+ return out
4858
+ }).reverse() as Arr.NonEmptyArray<Link>
4859
+ const to = flip(from)
4860
+ return replaceEncoding(to, to.encoding ? [...to.encoding, ...reversed] : reversed)
4821
4861
  }
4822
4862
 
4823
4863
  /**
@@ -4839,16 +4879,50 @@ function flipEncoding(ast: AST, encoding: Encoding): AST {
4839
4879
  * @since 4.0.0
4840
4880
  */
4841
4881
  export const flip = memoize((ast: AST): AST => {
4882
+ const out = flipAST(ast)
4883
+ if (out !== ast) flippedFrom.set(out, ast)
4884
+ return out
4885
+ })
4886
+
4887
+ function flipAST(ast: AST): AST {
4842
4888
  if (ast.encoding) {
4843
4889
  return flipEncoding(ast, ast.encoding)
4844
4890
  }
4845
- const owner = getContextOwner(ast)
4846
- if (owner !== ast) {
4847
- const flipped = flip(owner)
4848
- return flipped === owner ? ast : replaceContext(flipped, ast.context)
4891
+ const owned = projectOwner(ast, flip)
4892
+ if (owned) return owned
4893
+ if (isSuspend(ast)) {
4894
+ const previous = flippedThunks.get(ast.thunk)
4895
+ if (previous) return copy(ast, { thunk: previous.source.thunk })
4896
+ const flipped = ast.recur(flip)
4897
+ // Register the projection here so decoding alone does not pull in flip.
4898
+ flippedThunks.set(flipped.thunk, {
4899
+ source: ast,
4900
+ project(node) {
4901
+ const thunk = memoizeThunk(() => reverseBody(ast.thunk(), thunk))
4902
+ return copy(node, { thunk })
4903
+ }
4904
+ })
4905
+ return flipped
4849
4906
  }
4850
- return "flip" in ast ? ast.flip(flip) : "recur" in ast ? ast.recur(flip) : ast
4851
- })
4907
+ return "flip" in ast ? ast.flip(flip) : ast
4908
+ }
4909
+
4910
+ function reverseBody(body: AST, edge: Suspend["thunk"]): AST {
4911
+ if (types.get(body) !== body) return stripEncoding(flip(body))
4912
+ const existing = reversed.get(body)
4913
+ if (existing) return existing
4914
+ // A projected body can close its own cycle if all suspended edges lead back here
4915
+ // and reversing it preserves the checks and declaration callbacks.
4916
+ if (canReuse(body, edge)) {
4917
+ reversed.set(body, body)
4918
+ return body
4919
+ }
4920
+ const out = stripEncoding(flip(body))
4921
+ // The first reversal can discard checks. Further reversals can reuse the pair.
4922
+ if (reprojected.has(body) && !reversed.has(out)) reversed.set(out, body)
4923
+ reprojected.add(out)
4924
+ return out
4925
+ }
4852
4926
 
4853
4927
  function fromConst<const T>(
4854
4928
  ast: AST,