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
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
  // -----------------------------------------------------------------------------
@@ -4378,11 +4353,12 @@ export function isPattern(regExp: globalThis.RegExp, annotations?: Schema.Annota
4378
4353
  const bodyOwners = new WeakMap<AST, AST>()
4379
4354
 
4380
4355
  function copy<A extends AST>(ast: A, changes: Partial<AST>): A {
4381
- // AST copies preserve the prototype and enumerable values, not property descriptors.
4382
- const out = Object.assign(Object.create(Object.getPrototypeOf(ast)), ast, changes) as A
4383
- if (Reflect.ownKeys(changes).every((key) => key === "context" || key === "encoding")) {
4384
- bodyOwners.set(out, getContextOwner(ast))
4385
- }
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))
4386
4362
  return out
4387
4363
  }
4388
4364
 
@@ -4392,14 +4368,14 @@ export function getContextOwner<A extends AST>(ast: A): A {
4392
4368
  if (existing !== undefined) return existing as A
4393
4369
  if (ast.encoding === undefined) return ast
4394
4370
  // A body representative must not expose or retain the node's own codec.
4395
- const owner = Object.assign(Object.create(Object.getPrototypeOf(ast)), ast, { encoding: undefined }) as A
4371
+ const owner = copy(ast, { encoding: undefined })
4396
4372
  bodyOwners.set(ast, owner)
4397
4373
  return owner
4398
4374
  }
4399
4375
 
4400
4376
  /** @internal */
4401
4377
  export function replaceEncoding<A extends AST>(ast: A, encoding: Encoding | undefined): A {
4402
- return ast.encoding === encoding ? ast : copy(ast, { encoding })
4378
+ return ast.encoding === encoding ? ast : copyBody(ast, { encoding })
4403
4379
  }
4404
4380
 
4405
4381
  /** @internal */
@@ -4411,7 +4387,7 @@ export function replaceContext<A extends AST>(ast: A, context: Context | undefin
4411
4387
  if (owner.context === context && owner.encoding === ast.encoding) {
4412
4388
  return owner
4413
4389
  }
4414
- return copy(ast, { context })
4390
+ return copyBody(ast, { context })
4415
4391
  }
4416
4392
 
4417
4393
  /** @internal */
@@ -4422,8 +4398,7 @@ export function getLastEncoding(ast: AST): AST {
4422
4398
  /** @internal */
4423
4399
  export function annotate<A extends AST>(ast: A, annotations: Schema.Annotations.Annotations): A {
4424
4400
  if (ast.checks) {
4425
- const last = ast.checks[ast.checks.length - 1]
4426
- return replaceChecks(ast, Arr.append(ast.checks.slice(0, -1), last.annotate(annotations)))
4401
+ return replaceChecks(ast, mapLast(ast.checks, (check) => check.annotate(annotations)))
4427
4402
  }
4428
4403
  return copy(ast, { annotations: { ...ast.annotations, ...annotations } })
4429
4404
  }
@@ -4450,11 +4425,17 @@ export function mapLink(link: Link, f: (ast: AST) => AST): Link {
4450
4425
  return to === link.to ? link : new Link(to, link.transformation)
4451
4426
  }
4452
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
+
4453
4437
  function updateLastLink(encoding: Encoding, f: (ast: AST) => AST): Encoding {
4454
- const links = encoding
4455
- const last = links[links.length - 1]
4456
- const out = mapLink(last, f)
4457
- return out === last ? encoding : Arr.append(encoding.slice(0, encoding.length - 1), out)
4438
+ return mapLast(encoding, (link) => mapLink(link, f))
4458
4439
  }
4459
4440
 
4460
4441
  /** @internal */
@@ -4541,30 +4522,39 @@ export function mapOrSame<A>(as: ReadonlyArray<A>, f: (a: A) => A): ReadonlyArra
4541
4522
  return out ?? as
4542
4523
  }
4543
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
+
4544
4545
  /** @internal */
4545
4546
  export function annotateKey<A extends AST>(ast: A, annotations: Schema.Annotations.Key<unknown>): A {
4546
- const context = ast.context ?
4547
- new Context(
4548
- ast.context.isOptional,
4549
- ast.context.isMutable,
4550
- ast.context.constructorDefault,
4551
- { ...ast.context.annotations, ...annotations }
4552
- ) :
4553
- new Context(false, false, undefined, annotations)
4554
- return replaceContext(ast, context)
4547
+ return updateContext(
4548
+ ast,
4549
+ undefined,
4550
+ undefined,
4551
+ undefined,
4552
+ ast.context ? { ...ast.context.annotations, ...annotations } : annotations
4553
+ )
4555
4554
  }
4556
4555
 
4557
4556
  /** @internal */
4558
- export const optionalKey: <A extends AST>(ast: A) => A = memoizeIdempotent(<A extends AST>(ast: A): A => {
4559
- const context = ast.context ?
4560
- ast.context.isOptional === false ?
4561
- new Context(true, ast.context.isMutable, ast.context.constructorDefault, ast.context.annotations) :
4562
- ast.context :
4563
- new Context(true, false)
4564
- return optionalKeyLastLink(replaceContext(ast, context))
4565
- })
4566
-
4567
- const optionalKeyLastLink = applyToLastLink(optionalKey)
4557
+ export const optionalKey: <A extends AST>(ast: A) => A = setContextFlag("isOptional")
4568
4558
 
4569
4559
  /** @internal */
4570
4560
  export const optional = memoize(<A extends AST>(ast: A): Union<A | Undefined> =>
@@ -4572,26 +4562,14 @@ export const optional = memoize(<A extends AST>(ast: A): Union<A | Undefined> =>
4572
4562
  )
4573
4563
 
4574
4564
  /** @internal */
4575
- export const mutableKey = memoizeIdempotent(<A extends AST>(ast: A): A => {
4576
- const context = ast.context ?
4577
- ast.context.isMutable === false ?
4578
- new Context(ast.context.isOptional, true, ast.context.constructorDefault, ast.context.annotations) :
4579
- ast.context :
4580
- new Context(false, true)
4581
- return mutableKeyLastLink(replaceContext(ast, context))
4582
- })
4583
-
4584
- const mutableKeyLastLink = applyToLastLink(mutableKey)
4565
+ export const mutableKey = setContextFlag("isMutable")
4585
4566
 
4586
4567
  /** @internal */
4587
4568
  export function withConstructorDefault<A extends AST>(
4588
4569
  ast: A,
4589
4570
  defaultValue: Effect.Effect<unknown, SchemaIssue.Issue>
4590
4571
  ): A {
4591
- const context = ast.context ?
4592
- new Context(ast.context.isOptional, ast.context.isMutable, defaultValue, ast.context.annotations) :
4593
- new Context(false, false, defaultValue)
4594
- return replaceContext(ast, context)
4572
+ return updateContext(ast, undefined, undefined, defaultValue)
4595
4573
  }
4596
4574
 
4597
4575
  /**
@@ -4711,17 +4689,64 @@ function extractStructuralChecks(checks: Checks): Checks | undefined {
4711
4689
  return Arr.isArrayNonEmpty(out) ? out : undefined
4712
4690
  }
4713
4691
 
4714
- function canPreserveEncodingChecks(ast: AST): boolean {
4715
- 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
+
4716
4707
  function visit(child: AST): AST {
4717
- preserve = preserve && !child.encoding && !isSuspend(child)
4718
- if (preserve && "recur" in child) child.recur(visit)
4708
+ nodes.add(child)
4719
4709
  return child
4720
4710
  }
4721
- if ("recur" in ast) ast.recur(visit)
4722
- return preserve
4723
4711
  }
4724
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
+
4725
4750
  /**
4726
4751
  * Strips all encoding transformations from an AST, returning the decoded
4727
4752
  * (type-level) representation.
@@ -4748,27 +4773,52 @@ function canPreserveEncodingChecks(ast: AST): boolean {
4748
4773
  * @category transforming
4749
4774
  * @since 4.0.0
4750
4775
  */
4751
- 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 {
4752
4801
  const owner = getContextOwner(ast)
4753
4802
  if (owner !== ast) {
4754
- const type = toType(owner)
4755
- 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
4756
4805
  }
4757
- 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
4758
4813
  if ("encodingChecks" in type && type.encodingChecks) {
4759
- const checks = canPreserveEncodingChecks(ast)
4760
- ? type.encodingChecks
4761
- : isArrays(type) || isObjects(type) || isDeclaration(type) && type.typeParameters.length > 0
4762
- ? extractStructuralChecks(type.encodingChecks)
4763
- : undefined
4764
4814
  return copy(type, {
4765
4815
  encodingChecks: undefined,
4766
- // Match parsing order and preserve annotation precedence for decoded checks.
4767
- checks: combineChecks(checks, type.checks)
4816
+ checks: combineChecks(projectChecks(ast, type.encodingChecks), type.checks)
4768
4817
  })
4769
4818
  }
4770
- return type
4771
- })
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
+ }
4772
4822
 
4773
4823
  /**
4774
4824
  * Returns the encoded (wire-format) AST by flipping and then stripping
@@ -4797,26 +4847,17 @@ export const toType = memoizeIdempotent(<A extends AST>(ast: A): A => {
4797
4847
  * @category transforming
4798
4848
  * @since 4.0.0
4799
4849
  */
4800
- export const toEncoded = memoizeIdempotent((ast: AST): AST => {
4801
- return toType(flip(ast))
4802
- })
4850
+ export const toEncoded = (ast: AST): AST => toType(flip(ast))
4803
4851
 
4804
4852
  function flipEncoding(ast: AST, encoding: Encoding): AST {
4805
- const links = encoding
4806
- const len = links.length
4807
- const last = links[len - 1]
4808
- const ls: Arr.NonEmptyArray<Link> = [
4809
- new Link(flip(replaceEncoding(ast, undefined)), links[0].transformation.flip())
4810
- ]
4811
- for (let i = 1; i < len; i++) {
4812
- ls.unshift(new Link(flip(links[i - 1].to), links[i].transformation.flip()))
4813
- }
4814
- const to = flip(last.to)
4815
- if (to.encoding) {
4816
- return replaceEncoding(to, [...to.encoding, ...ls])
4817
- } else {
4818
- return replaceEncoding(to, ls)
4819
- }
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)
4820
4861
  }
4821
4862
 
4822
4863
  /**
@@ -4838,16 +4879,50 @@ function flipEncoding(ast: AST, encoding: Encoding): AST {
4838
4879
  * @since 4.0.0
4839
4880
  */
4840
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 {
4841
4888
  if (ast.encoding) {
4842
4889
  return flipEncoding(ast, ast.encoding)
4843
4890
  }
4844
- const owner = getContextOwner(ast)
4845
- if (owner !== ast) {
4846
- const flipped = flip(owner)
4847
- 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
4848
4906
  }
4849
- return "flip" in ast ? ast.flip(flip) : "recur" in ast ? ast.recur(flip) : ast
4850
- })
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
+ }
4851
4926
 
4852
4927
  function fromConst<const T>(
4853
4928
  ast: AST,