@rocicorp/zero 1.11.0-canary.1 → 1.11.0-canary.11

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 (628) hide show
  1. package/out/analyze-query/src/bin-analyze.js +9 -8
  2. package/out/analyze-query/src/bin-analyze.js.map +1 -1
  3. package/out/replicache/src/btree/node.d.ts.map +1 -1
  4. package/out/replicache/src/btree/node.js +3 -6
  5. package/out/replicache/src/btree/node.js.map +1 -1
  6. package/out/replicache/src/btree/read.d.ts +12 -1
  7. package/out/replicache/src/btree/read.d.ts.map +1 -1
  8. package/out/replicache/src/btree/read.js +24 -12
  9. package/out/replicache/src/btree/read.js.map +1 -1
  10. package/out/replicache/src/dag/gc.d.ts.map +1 -1
  11. package/out/replicache/src/dag/gc.js +8 -11
  12. package/out/replicache/src/dag/gc.js.map +1 -1
  13. package/out/replicache/src/dag/invalid-ref-count-error.d.ts +12 -0
  14. package/out/replicache/src/dag/invalid-ref-count-error.d.ts.map +1 -0
  15. package/out/replicache/src/dag/invalid-ref-count-error.js +19 -0
  16. package/out/replicache/src/dag/invalid-ref-count-error.js.map +1 -0
  17. package/out/replicache/src/dag/store-impl.d.ts +12 -2
  18. package/out/replicache/src/dag/store-impl.d.ts.map +1 -1
  19. package/out/replicache/src/dag/store-impl.js +22 -8
  20. package/out/replicache/src/dag/store-impl.js.map +1 -1
  21. package/out/replicache/src/db/rebase.d.ts +14 -2
  22. package/out/replicache/src/db/rebase.d.ts.map +1 -1
  23. package/out/replicache/src/db/rebase.js +27 -5
  24. package/out/replicache/src/db/rebase.js.map +1 -1
  25. package/out/replicache/src/db/write.js.map +1 -1
  26. package/out/replicache/src/kv/expo-sqlite/store.d.ts.map +1 -1
  27. package/out/replicache/src/kv/expo-sqlite/store.js +28 -5
  28. package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
  29. package/out/replicache/src/kv/idb-store-with-mem-fallback.d.ts +15 -5
  30. package/out/replicache/src/kv/idb-store-with-mem-fallback.d.ts.map +1 -1
  31. package/out/replicache/src/kv/idb-store-with-mem-fallback.js +25 -25
  32. package/out/replicache/src/kv/idb-store-with-mem-fallback.js.map +1 -1
  33. package/out/replicache/src/kv/idb-store.d.ts +9 -0
  34. package/out/replicache/src/kv/idb-store.d.ts.map +1 -1
  35. package/out/replicache/src/kv/idb-store.js +14 -2
  36. package/out/replicache/src/kv/idb-store.js.map +1 -1
  37. package/out/replicache/src/kv/mem-store.d.ts +1 -0
  38. package/out/replicache/src/kv/mem-store.d.ts.map +1 -1
  39. package/out/replicache/src/kv/mem-store.js +3 -0
  40. package/out/replicache/src/kv/mem-store.js.map +1 -1
  41. package/out/replicache/src/kv/op-sqlite/store.d.ts.map +1 -1
  42. package/out/replicache/src/kv/op-sqlite/store.js +1 -1
  43. package/out/replicache/src/kv/op-sqlite/store.js.map +1 -1
  44. package/out/replicache/src/kv/sqlite-store.d.ts +13 -1
  45. package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
  46. package/out/replicache/src/kv/sqlite-store.js +73 -6
  47. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  48. package/out/replicache/src/kv/store.d.ts +6 -0
  49. package/out/replicache/src/kv/store.d.ts.map +1 -1
  50. package/out/replicache/src/persist/collect-idb-databases.d.ts +10 -5
  51. package/out/replicache/src/persist/collect-idb-databases.d.ts.map +1 -1
  52. package/out/replicache/src/persist/collect-idb-databases.js +16 -9
  53. package/out/replicache/src/persist/collect-idb-databases.js.map +1 -1
  54. package/out/replicache/src/persist/persist.d.ts.map +1 -1
  55. package/out/replicache/src/persist/persist.js +13 -6
  56. package/out/replicache/src/persist/persist.js.map +1 -1
  57. package/out/replicache/src/persist/refresh.js +6 -2
  58. package/out/replicache/src/persist/refresh.js.map +1 -1
  59. package/out/replicache/src/replicache-impl.d.ts +14 -3
  60. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  61. package/out/replicache/src/replicache-impl.js +118 -11
  62. package/out/replicache/src/replicache-impl.js.map +1 -1
  63. package/out/replicache/src/replicache-options.d.ts +9 -0
  64. package/out/replicache/src/replicache-options.d.ts.map +1 -1
  65. package/out/replicache/src/transactions.d.ts.map +1 -1
  66. package/out/replicache/src/transactions.js +1 -1
  67. package/out/replicache/src/transactions.js.map +1 -1
  68. package/out/shared/src/arrays.d.ts +5 -0
  69. package/out/shared/src/arrays.d.ts.map +1 -1
  70. package/out/shared/src/arrays.js +8 -10
  71. package/out/shared/src/arrays.js.map +1 -1
  72. package/out/shared/src/error.d.ts +11 -0
  73. package/out/shared/src/error.d.ts.map +1 -1
  74. package/out/shared/src/error.js +35 -1
  75. package/out/shared/src/error.js.map +1 -1
  76. package/out/shared/src/iterables.d.ts +2 -0
  77. package/out/shared/src/iterables.d.ts.map +1 -1
  78. package/out/shared/src/iterables.js +18 -1
  79. package/out/shared/src/iterables.js.map +1 -1
  80. package/out/shared/src/map.d.ts +24 -2
  81. package/out/shared/src/map.d.ts.map +1 -1
  82. package/out/shared/src/map.js +18 -4
  83. package/out/shared/src/map.js.map +1 -1
  84. package/out/shared/src/ring-buffer.d.ts +8 -0
  85. package/out/shared/src/ring-buffer.d.ts.map +1 -1
  86. package/out/shared/src/ring-buffer.js +22 -1
  87. package/out/shared/src/ring-buffer.js.map +1 -1
  88. package/out/z2s/src/compiler.js +1 -1
  89. package/out/z2s/src/sql.d.ts.map +1 -1
  90. package/out/z2s/src/sql.js +2 -2
  91. package/out/z2s/src/sql.js.map +1 -1
  92. package/out/zero/package.js +3 -4
  93. package/out/zero/package.js.map +1 -1
  94. package/out/zero-cache/src/auth/write-authorizer.d.ts.map +1 -1
  95. package/out/zero-cache/src/auth/write-authorizer.js +1 -0
  96. package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
  97. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  98. package/out/zero-cache/src/config/normalize.js +25 -1
  99. package/out/zero-cache/src/config/normalize.js.map +1 -1
  100. package/out/zero-cache/src/config/zero-config.d.ts +48 -0
  101. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  102. package/out/zero-cache/src/config/zero-config.js +130 -5
  103. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  104. package/out/zero-cache/src/db/lite-tables.d.ts.map +1 -1
  105. package/out/zero-cache/src/db/lite-tables.js +3 -4
  106. package/out/zero-cache/src/db/lite-tables.js.map +1 -1
  107. package/out/zero-cache/src/observability/events.js +4 -1
  108. package/out/zero-cache/src/observability/events.js.map +1 -1
  109. package/out/zero-cache/src/observability/metrics.d.ts.map +1 -1
  110. package/out/zero-cache/src/observability/metrics.js +2 -7
  111. package/out/zero-cache/src/observability/metrics.js.map +1 -1
  112. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  113. package/out/zero-cache/src/server/change-streamer.js +27 -10
  114. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  115. package/out/zero-cache/src/server/inspector-delegate.d.ts +18 -2
  116. package/out/zero-cache/src/server/inspector-delegate.d.ts.map +1 -1
  117. package/out/zero-cache/src/server/inspector-delegate.js +22 -15
  118. package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
  119. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  120. package/out/zero-cache/src/server/main.js +8 -1
  121. package/out/zero-cache/src/server/main.js.map +1 -1
  122. package/out/zero-cache/src/server/mutator.js +2 -0
  123. package/out/zero-cache/src/server/mutator.js.map +1 -1
  124. package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
  125. package/out/zero-cache/src/server/reaper.js +2 -0
  126. package/out/zero-cache/src/server/reaper.js.map +1 -1
  127. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  128. package/out/zero-cache/src/server/replicator.js +12 -3
  129. package/out/zero-cache/src/server/replicator.js.map +1 -1
  130. package/out/zero-cache/src/server/runner/zero-dispatcher.d.ts.map +1 -1
  131. package/out/zero-cache/src/server/runner/zero-dispatcher.js +3 -0
  132. package/out/zero-cache/src/server/runner/zero-dispatcher.js.map +1 -1
  133. package/out/zero-cache/src/server/syncer-assigner.d.ts +14 -0
  134. package/out/zero-cache/src/server/syncer-assigner.d.ts.map +1 -0
  135. package/out/zero-cache/src/server/syncer-assigner.js +72 -0
  136. package/out/zero-cache/src/server/syncer-assigner.js.map +1 -0
  137. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  138. package/out/zero-cache/src/server/syncer.js +5 -1
  139. package/out/zero-cache/src/server/syncer.js.map +1 -1
  140. package/out/zero-cache/src/server/worker-dispatcher.d.ts.map +1 -1
  141. package/out/zero-cache/src/server/worker-dispatcher.js +21 -4
  142. package/out/zero-cache/src/server/worker-dispatcher.js.map +1 -1
  143. package/out/zero-cache/src/services/analyze.d.ts.map +1 -1
  144. package/out/zero-cache/src/services/analyze.js +11 -9
  145. package/out/zero-cache/src/services/analyze.js.map +1 -1
  146. package/out/zero-cache/src/services/change-source/common/backfill-manager.d.ts.map +1 -1
  147. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +15 -3
  148. package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
  149. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.d.ts.map +1 -1
  150. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +15 -3
  151. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
  152. package/out/zero-cache/src/services/change-source/common/replica-schema.d.ts.map +1 -1
  153. package/out/zero-cache/src/services/change-source/common/replica-schema.js +2 -39
  154. package/out/zero-cache/src/services/change-source/common/replica-schema.js.map +1 -1
  155. package/out/zero-cache/src/services/change-source/pg/backfill-resume.d.ts +156 -0
  156. package/out/zero-cache/src/services/change-source/pg/backfill-resume.d.ts.map +1 -0
  157. package/out/zero-cache/src/services/change-source/pg/backfill-resume.js +15 -0
  158. package/out/zero-cache/src/services/change-source/pg/backfill-resume.js.map +1 -0
  159. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts +25 -0
  160. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts.map +1 -0
  161. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +202 -0
  162. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -0
  163. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +14 -16
  164. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  165. package/out/zero-cache/src/services/change-source/pg/change-source.js +64 -143
  166. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  167. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +28 -2
  168. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  169. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +44 -21
  170. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  171. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.d.ts +16 -0
  172. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.d.ts.map +1 -0
  173. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.js +113 -0
  174. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.js.map +1 -0
  175. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts +21 -15
  176. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  177. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +86 -23
  178. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  179. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts.map +1 -1
  180. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js +68 -8
  181. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js.map +1 -1
  182. package/out/zero-cache/src/services/change-source/pg/schema/init.d.ts.map +1 -1
  183. package/out/zero-cache/src/services/change-source/pg/schema/init.js +32 -1
  184. package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
  185. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.d.ts +10 -0
  186. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.d.ts.map +1 -0
  187. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.js +6 -0
  188. package/out/zero-cache/src/services/change-source/pg/schema/replica-stage-enum.js.map +1 -0
  189. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +50 -27
  190. package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
  191. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +100 -34
  192. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  193. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts.map +1 -1
  194. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +58 -7
  195. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  196. package/out/zero-cache/src/services/change-streamer/broadcast.d.ts +8 -2
  197. package/out/zero-cache/src/services/change-streamer/broadcast.d.ts.map +1 -1
  198. package/out/zero-cache/src/services/change-streamer/broadcast.js +24 -4
  199. package/out/zero-cache/src/services/change-streamer/broadcast.js.map +1 -1
  200. package/out/zero-cache/src/services/change-streamer/change-log-codec.d.ts +19 -4
  201. package/out/zero-cache/src/services/change-streamer/change-log-codec.d.ts.map +1 -1
  202. package/out/zero-cache/src/services/change-streamer/change-log-codec.js +28 -8
  203. package/out/zero-cache/src/services/change-streamer/change-log-codec.js.map +1 -1
  204. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +5 -2
  205. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  206. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +21 -14
  207. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  208. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +11 -0
  209. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  210. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +97 -63
  211. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  212. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +12 -8
  213. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  214. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  215. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts +3 -3
  216. package/out/zero-cache/src/services/change-streamer/forwarder.d.ts.map +1 -1
  217. package/out/zero-cache/src/services/change-streamer/forwarder.js +36 -5
  218. package/out/zero-cache/src/services/change-streamer/forwarder.js.map +1 -1
  219. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts +1 -1
  220. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts.map +1 -1
  221. package/out/zero-cache/src/services/change-streamer/schema/tables.js +4 -4
  222. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  223. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.d.ts.map +1 -1
  224. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js +15 -3
  225. package/out/zero-cache/src/services/change-streamer/snapshot-reservations.js.map +1 -1
  226. package/out/zero-cache/src/services/change-streamer/snapshot.d.ts +21 -1
  227. package/out/zero-cache/src/services/change-streamer/snapshot.d.ts.map +1 -1
  228. package/out/zero-cache/src/services/change-streamer/snapshot.js +61 -21
  229. package/out/zero-cache/src/services/change-streamer/snapshot.js.map +1 -1
  230. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.d.ts +6 -1
  231. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.d.ts.map +1 -1
  232. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js +27 -9
  233. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js.map +1 -1
  234. package/out/zero-cache/src/services/change-streamer/storer.d.ts +2 -2
  235. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  236. package/out/zero-cache/src/services/change-streamer/storer.js +8 -5
  237. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  238. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +4 -1
  239. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  240. package/out/zero-cache/src/services/change-streamer/subscriber.js +42 -0
  241. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  242. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts +17 -1
  243. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts.map +1 -1
  244. package/out/zero-cache/src/services/change-streamer/upstream-acker.js +19 -3
  245. package/out/zero-cache/src/services/change-streamer/upstream-acker.js.map +1 -1
  246. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts.map +1 -1
  247. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js +1 -0
  248. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js.map +1 -1
  249. package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
  250. package/out/zero-cache/src/services/life-cycle.js +3 -1
  251. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  252. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  253. package/out/zero-cache/src/services/litestream/commands.js +3 -1
  254. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  255. package/out/zero-cache/src/services/litestream/config-v5.yml +3 -4
  256. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js +51 -24
  257. package/out/zero-cache/src/services/litestream/litestream-checkpointer.js.map +1 -1
  258. package/out/zero-cache/src/services/litestream/restore-progress.d.ts +24 -0
  259. package/out/zero-cache/src/services/litestream/restore-progress.d.ts.map +1 -0
  260. package/out/zero-cache/src/services/litestream/restore-progress.js +91 -0
  261. package/out/zero-cache/src/services/litestream/restore-progress.js.map +1 -0
  262. package/out/zero-cache/src/services/mutagen/mutagen.d.ts +5 -0
  263. package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
  264. package/out/zero-cache/src/services/mutagen/mutagen.js +7 -1
  265. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  266. package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
  267. package/out/zero-cache/src/services/mutagen/pusher.js +5 -6
  268. package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
  269. package/out/zero-cache/src/services/profz.d.ts +7 -0
  270. package/out/zero-cache/src/services/profz.d.ts.map +1 -0
  271. package/out/zero-cache/src/services/profz.js +105 -0
  272. package/out/zero-cache/src/services/profz.js.map +1 -0
  273. package/out/zero-cache/src/services/replicator/change-log-db.js.map +1 -1
  274. package/out/zero-cache/src/services/replicator/change-log-stream-writer.d.ts +5 -4
  275. package/out/zero-cache/src/services/replicator/change-log-stream-writer.d.ts.map +1 -1
  276. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js +20 -22
  277. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js.map +1 -1
  278. package/out/zero-cache/src/services/replicator/change-processor.js +1 -0
  279. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  280. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +1 -1
  281. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  282. package/out/zero-cache/src/services/replicator/incremental-sync.js +57 -17
  283. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  284. package/out/zero-cache/src/services/replicator/replication-status.d.ts +35 -1
  285. package/out/zero-cache/src/services/replicator/replication-status.d.ts.map +1 -1
  286. package/out/zero-cache/src/services/replicator/replication-status.js +96 -6
  287. package/out/zero-cache/src/services/replicator/replication-status.js.map +1 -1
  288. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  289. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts +6 -6
  290. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.d.ts.map +1 -1
  291. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js +19 -14
  292. package/out/zero-cache/src/services/replicator/sqlite-change-log-observability.js.map +1 -1
  293. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts +4 -4
  294. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts.map +1 -1
  295. package/out/zero-cache/src/services/replicator/write-worker-client.js +2 -2
  296. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  297. package/out/zero-cache/src/services/replicator/write-worker.js +10 -4
  298. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  299. package/out/zero-cache/src/services/run-ast.d.ts +12 -0
  300. package/out/zero-cache/src/services/run-ast.d.ts.map +1 -1
  301. package/out/zero-cache/src/services/run-ast.js +26 -14
  302. package/out/zero-cache/src/services/run-ast.js.map +1 -1
  303. package/out/zero-cache/src/services/runner.d.ts +2 -1
  304. package/out/zero-cache/src/services/runner.d.ts.map +1 -1
  305. package/out/zero-cache/src/services/runner.js +10 -2
  306. package/out/zero-cache/src/services/runner.js.map +1 -1
  307. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +1 -1
  308. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
  309. package/out/zero-cache/src/services/view-syncer/client-handler.js +36 -31
  310. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  311. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +45 -0
  312. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  313. package/out/zero-cache/src/services/view-syncer/cvr.js +133 -1
  314. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  315. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts +14 -1
  316. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts.map +1 -1
  317. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +30 -4
  318. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
  319. package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts +33 -0
  320. package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts.map +1 -0
  321. package/out/zero-cache/src/services/view-syncer/hydration-budget.js +62 -0
  322. package/out/zero-cache/src/services/view-syncer/hydration-budget.js.map +1 -0
  323. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts +51 -0
  324. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts.map +1 -0
  325. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js +78 -0
  326. package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js.map +1 -0
  327. package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts +1 -1
  328. package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts.map +1 -1
  329. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +2 -2
  330. package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
  331. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +14 -1
  332. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  333. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +64 -30
  334. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  335. package/out/zero-cache/src/services/view-syncer/query-covering.d.ts.map +1 -1
  336. package/out/zero-cache/src/services/view-syncer/query-covering.js +2 -6
  337. package/out/zero-cache/src/services/view-syncer/query-covering.js.map +1 -1
  338. package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.d.ts +95 -0
  339. package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.d.ts.map +1 -0
  340. package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.js +155 -0
  341. package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.js.map +1 -0
  342. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +48 -4
  343. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  344. package/out/zero-cache/src/services/view-syncer/snapshotter.js +125 -28
  345. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  346. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +11 -1
  347. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  348. package/out/zero-cache/src/services/view-syncer/view-syncer.js +512 -98
  349. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  350. package/out/zero-cache/src/types/processes.d.ts +39 -0
  351. package/out/zero-cache/src/types/processes.d.ts.map +1 -1
  352. package/out/zero-cache/src/types/processes.js +43 -4
  353. package/out/zero-cache/src/types/processes.js.map +1 -1
  354. package/out/zero-cache/src/types/profiler.d.ts +12 -0
  355. package/out/zero-cache/src/types/profiler.d.ts.map +1 -1
  356. package/out/zero-cache/src/types/profiler.js +46 -1
  357. package/out/zero-cache/src/types/profiler.js.map +1 -1
  358. package/out/zero-cache/src/types/row-key.d.ts.map +1 -1
  359. package/out/zero-cache/src/types/row-key.js +5 -5
  360. package/out/zero-cache/src/types/row-key.js.map +1 -1
  361. package/out/zero-cache/src/types/streams.d.ts +25 -9
  362. package/out/zero-cache/src/types/streams.d.ts.map +1 -1
  363. package/out/zero-cache/src/types/streams.js +118 -34
  364. package/out/zero-cache/src/types/streams.js.map +1 -1
  365. package/out/zero-cache/src/types/subscription.d.ts +4 -0
  366. package/out/zero-cache/src/types/subscription.d.ts.map +1 -1
  367. package/out/zero-cache/src/types/subscription.js +71 -0
  368. package/out/zero-cache/src/types/subscription.js.map +1 -1
  369. package/out/zero-cache/src/types/websocket-handoff.d.ts +1 -1
  370. package/out/zero-cache/src/types/websocket-handoff.d.ts.map +1 -1
  371. package/out/zero-cache/src/types/websocket-handoff.js +18 -9
  372. package/out/zero-cache/src/types/websocket-handoff.js.map +1 -1
  373. package/out/zero-cache/src/types/ws.d.ts.map +1 -1
  374. package/out/zero-cache/src/types/ws.js +23 -8
  375. package/out/zero-cache/src/types/ws.js.map +1 -1
  376. package/out/zero-cache/src/workers/connect-params.d.ts +1 -0
  377. package/out/zero-cache/src/workers/connect-params.d.ts.map +1 -1
  378. package/out/zero-cache/src/workers/connect-params.js.map +1 -1
  379. package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
  380. package/out/zero-cache/src/workers/syncer-ws-message-handler.js +21 -16
  381. package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
  382. package/out/zero-cache/src/workers/syncer.d.ts.map +1 -1
  383. package/out/zero-cache/src/workers/syncer.js +55 -4
  384. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  385. package/out/zero-client/src/client/connection-manager.d.ts +20 -0
  386. package/out/zero-client/src/client/connection-manager.d.ts.map +1 -1
  387. package/out/zero-client/src/client/connection-manager.js +77 -13
  388. package/out/zero-client/src/client/connection-manager.js.map +1 -1
  389. package/out/zero-client/src/client/connection-status-enum.d.ts +2 -0
  390. package/out/zero-client/src/client/connection-status-enum.d.ts.map +1 -1
  391. package/out/zero-client/src/client/connection-status-enum.js +3 -1
  392. package/out/zero-client/src/client/connection-status-enum.js.map +1 -1
  393. package/out/zero-client/src/client/connection.d.ts +6 -0
  394. package/out/zero-client/src/client/connection.d.ts.map +1 -1
  395. package/out/zero-client/src/client/connection.js +2 -1
  396. package/out/zero-client/src/client/connection.js.map +1 -1
  397. package/out/zero-client/src/client/context.d.ts +27 -2
  398. package/out/zero-client/src/client/context.d.ts.map +1 -1
  399. package/out/zero-client/src/client/context.js +112 -1
  400. package/out/zero-client/src/client/context.js.map +1 -1
  401. package/out/zero-client/src/client/custom.js +2 -2
  402. package/out/zero-client/src/client/custom.js.map +1 -1
  403. package/out/zero-client/src/client/error.js +1 -1
  404. package/out/zero-client/src/client/inspector/lazy-inspector.js +2 -2
  405. package/out/zero-client/src/client/inspector/lazy-inspector.js.map +1 -1
  406. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
  407. package/out/zero-client/src/client/ivm-branch.js +20 -3
  408. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  409. package/out/zero-client/src/client/mutator-proxy.d.ts.map +1 -1
  410. package/out/zero-client/src/client/mutator-proxy.js +2 -1
  411. package/out/zero-client/src/client/mutator-proxy.js.map +1 -1
  412. package/out/zero-client/src/client/options.d.ts +3 -0
  413. package/out/zero-client/src/client/options.d.ts.map +1 -1
  414. package/out/zero-client/src/client/options.js.map +1 -1
  415. package/out/zero-client/src/client/query-manager.d.ts.map +1 -1
  416. package/out/zero-client/src/client/query-manager.js +44 -11
  417. package/out/zero-client/src/client/query-manager.js.map +1 -1
  418. package/out/zero-client/src/client/version.js +1 -1
  419. package/out/zero-client/src/client/zero-rep.d.ts +1 -0
  420. package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
  421. package/out/zero-client/src/client/zero-rep.js +9 -6
  422. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  423. package/out/zero-client/src/client/zero.d.ts +18 -3
  424. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  425. package/out/zero-client/src/client/zero.js +120 -32
  426. package/out/zero-client/src/client/zero.js.map +1 -1
  427. package/out/zero-client/src/types/query-result.d.ts +2 -0
  428. package/out/zero-client/src/types/query-result.d.ts.map +1 -1
  429. package/out/zero-events/src/status.d.ts +58 -0
  430. package/out/zero-events/src/status.d.ts.map +1 -1
  431. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  432. package/out/zero-protocol/src/ast.js +13 -14
  433. package/out/zero-protocol/src/ast.js.map +1 -1
  434. package/out/zero-protocol/src/poke.d.ts +6 -6
  435. package/out/zero-protocol/src/query-hash.d.ts.map +1 -1
  436. package/out/zero-protocol/src/query-hash.js +2 -5
  437. package/out/zero-protocol/src/query-hash.js.map +1 -1
  438. package/out/zero-react/src/use-query.d.ts +5 -4
  439. package/out/zero-react/src/use-query.d.ts.map +1 -1
  440. package/out/zero-react/src/use-query.js +86 -9
  441. package/out/zero-react/src/use-query.js.map +1 -1
  442. package/out/zero-server/src/adapters/drizzle.d.ts +2 -1
  443. package/out/zero-server/src/adapters/drizzle.d.ts.map +1 -1
  444. package/out/zero-server/src/adapters/drizzle.js +12 -7
  445. package/out/zero-server/src/adapters/drizzle.js.map +1 -1
  446. package/out/zero-server/src/adapters/kysely.d.ts +2 -1
  447. package/out/zero-server/src/adapters/kysely.d.ts.map +1 -1
  448. package/out/zero-server/src/adapters/kysely.js +8 -2
  449. package/out/zero-server/src/adapters/kysely.js.map +1 -1
  450. package/out/zero-server/src/adapters/pg.d.ts +1 -0
  451. package/out/zero-server/src/adapters/pg.d.ts.map +1 -1
  452. package/out/zero-server/src/adapters/pg.js +8 -2
  453. package/out/zero-server/src/adapters/pg.js.map +1 -1
  454. package/out/zero-server/src/adapters/postgresjs.d.ts +1 -0
  455. package/out/zero-server/src/adapters/postgresjs.d.ts.map +1 -1
  456. package/out/zero-server/src/adapters/postgresjs.js +7 -1
  457. package/out/zero-server/src/adapters/postgresjs.js.map +1 -1
  458. package/out/zero-server/src/adapters/prisma.d.ts +3 -2
  459. package/out/zero-server/src/adapters/prisma.d.ts.map +1 -1
  460. package/out/zero-server/src/adapters/prisma.js +8 -2
  461. package/out/zero-server/src/adapters/prisma.js.map +1 -1
  462. package/out/zero-server/src/custom.d.ts +5 -1
  463. package/out/zero-server/src/custom.d.ts.map +1 -1
  464. package/out/zero-server/src/custom.js +6 -3
  465. package/out/zero-server/src/custom.js.map +1 -1
  466. package/out/zero-server/src/mod.d.ts +1 -1
  467. package/out/zero-server/src/mod.d.ts.map +1 -1
  468. package/out/zero-server/src/pg-query-executor.d.ts +2 -2
  469. package/out/zero-server/src/pg-query-executor.d.ts.map +1 -1
  470. package/out/zero-server/src/pg-query-executor.js +2 -2
  471. package/out/zero-server/src/pg-query-executor.js.map +1 -1
  472. package/out/zero-server/src/schema.d.ts +2 -2
  473. package/out/zero-server/src/schema.d.ts.map +1 -1
  474. package/out/zero-server/src/schema.js +2 -2
  475. package/out/zero-server/src/schema.js.map +1 -1
  476. package/out/zero-server/src/zql-database.d.ts +21 -1
  477. package/out/zero-server/src/zql-database.d.ts.map +1 -1
  478. package/out/zero-server/src/zql-database.js +33 -2
  479. package/out/zero-server/src/zql-database.js.map +1 -1
  480. package/out/zero-solid/src/solid-view.d.ts +17 -0
  481. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  482. package/out/zero-solid/src/solid-view.js +39 -1
  483. package/out/zero-solid/src/solid-view.js.map +1 -1
  484. package/out/zero-solid/src/use-query.js +5 -3
  485. package/out/zero-solid/src/use-query.js.map +1 -1
  486. package/out/zql/src/builder/builder.d.ts +19 -0
  487. package/out/zql/src/builder/builder.d.ts.map +1 -1
  488. package/out/zql/src/builder/builder.js +38 -12
  489. package/out/zql/src/builder/builder.js.map +1 -1
  490. package/out/zql/src/builder/correlated-predicate-pushdown.d.ts +43 -0
  491. package/out/zql/src/builder/correlated-predicate-pushdown.d.ts.map +1 -0
  492. package/out/zql/src/builder/correlated-predicate-pushdown.js +161 -0
  493. package/out/zql/src/builder/correlated-predicate-pushdown.js.map +1 -0
  494. package/out/zql/src/builder/debug-delegate.d.ts +6 -1
  495. package/out/zql/src/builder/debug-delegate.d.ts.map +1 -1
  496. package/out/zql/src/builder/debug-delegate.js +14 -2
  497. package/out/zql/src/builder/debug-delegate.js.map +1 -1
  498. package/out/zql/src/ivm/array-view.d.ts +29 -0
  499. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  500. package/out/zql/src/ivm/array-view.js +63 -12
  501. package/out/zql/src/ivm/array-view.js.map +1 -1
  502. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  503. package/out/zql/src/ivm/cap.js +10 -4
  504. package/out/zql/src/ivm/cap.js.map +1 -1
  505. package/out/zql/src/ivm/connection-index.d.ts +68 -0
  506. package/out/zql/src/ivm/connection-index.d.ts.map +1 -0
  507. package/out/zql/src/ivm/connection-index.js +149 -0
  508. package/out/zql/src/ivm/connection-index.js.map +1 -0
  509. package/out/zql/src/ivm/data.d.ts.map +1 -1
  510. package/out/zql/src/ivm/data.js +8 -3
  511. package/out/zql/src/ivm/data.js.map +1 -1
  512. package/out/zql/src/ivm/deferred-input.d.ts +44 -0
  513. package/out/zql/src/ivm/deferred-input.d.ts.map +1 -0
  514. package/out/zql/src/ivm/deferred-input.js +89 -0
  515. package/out/zql/src/ivm/deferred-input.js.map +1 -0
  516. package/out/zql/src/ivm/exists.d.ts +1 -1
  517. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  518. package/out/zql/src/ivm/exists.js +1 -1
  519. package/out/zql/src/ivm/exists.js.map +1 -1
  520. package/out/zql/src/ivm/fan-in.d.ts +2 -2
  521. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  522. package/out/zql/src/ivm/fan-in.js +6 -5
  523. package/out/zql/src/ivm/fan-in.js.map +1 -1
  524. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  525. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  526. package/out/zql/src/ivm/fan-out.js.map +1 -1
  527. package/out/zql/src/ivm/filter-operators.d.ts +6 -6
  528. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  529. package/out/zql/src/ivm/filter-operators.js +10 -8
  530. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  531. package/out/zql/src/ivm/filter.d.ts +2 -2
  532. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  533. package/out/zql/src/ivm/filter.js +7 -4
  534. package/out/zql/src/ivm/filter.js.map +1 -1
  535. package/out/zql/src/ivm/flipped-join.d.ts +6 -4
  536. package/out/zql/src/ivm/flipped-join.d.ts.map +1 -1
  537. package/out/zql/src/ivm/flipped-join.js +20 -33
  538. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  539. package/out/zql/src/ivm/join-utils.d.ts +10 -0
  540. package/out/zql/src/ivm/join-utils.d.ts.map +1 -1
  541. package/out/zql/src/ivm/join-utils.js +23 -1
  542. package/out/zql/src/ivm/join-utils.js.map +1 -1
  543. package/out/zql/src/ivm/join.d.ts +5 -1
  544. package/out/zql/src/ivm/join.d.ts.map +1 -1
  545. package/out/zql/src/ivm/join.js +91 -13
  546. package/out/zql/src/ivm/join.js.map +1 -1
  547. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts +1 -1
  548. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts.map +1 -1
  549. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +6 -4
  550. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js.map +1 -1
  551. package/out/zql/src/ivm/memory-source.d.ts +15 -3
  552. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  553. package/out/zql/src/ivm/memory-source.js +40 -19
  554. package/out/zql/src/ivm/memory-source.js.map +1 -1
  555. package/out/zql/src/ivm/take-gate.d.ts +35 -0
  556. package/out/zql/src/ivm/take-gate.d.ts.map +1 -0
  557. package/out/zql/src/ivm/take-gate.js +81 -0
  558. package/out/zql/src/ivm/take-gate.js.map +1 -0
  559. package/out/zql/src/ivm/take.d.ts +6 -1
  560. package/out/zql/src/ivm/take.d.ts.map +1 -1
  561. package/out/zql/src/ivm/take.js +104 -88
  562. package/out/zql/src/ivm/take.js.map +1 -1
  563. package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
  564. package/out/zql/src/ivm/union-fan-in.js +9 -7
  565. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  566. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  567. package/out/zql/src/ivm/view-apply-change.js +9 -4
  568. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  569. package/out/zql/src/ivm/view.d.ts +15 -0
  570. package/out/zql/src/ivm/view.d.ts.map +1 -1
  571. package/out/zql/src/mutate/custom.d.ts +24 -1
  572. package/out/zql/src/mutate/custom.d.ts.map +1 -1
  573. package/out/zql/src/mutate/custom.js.map +1 -1
  574. package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
  575. package/out/zql/src/mutate/mutator-registry.js +2 -2
  576. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  577. package/out/zql/src/planner/planner-builder.d.ts +12 -3
  578. package/out/zql/src/planner/planner-builder.d.ts.map +1 -1
  579. package/out/zql/src/planner/planner-builder.js +27 -17
  580. package/out/zql/src/planner/planner-builder.js.map +1 -1
  581. package/out/zql/src/planner/planner-connection.d.ts +15 -2
  582. package/out/zql/src/planner/planner-connection.d.ts.map +1 -1
  583. package/out/zql/src/planner/planner-connection.js +73 -8
  584. package/out/zql/src/planner/planner-connection.js.map +1 -1
  585. package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
  586. package/out/zql/src/planner/planner-debug.js +3 -8
  587. package/out/zql/src/planner/planner-debug.js.map +1 -1
  588. package/out/zql/src/planner/planner-source.d.ts +2 -2
  589. package/out/zql/src/planner/planner-source.d.ts.map +1 -1
  590. package/out/zql/src/planner/planner-source.js +2 -2
  591. package/out/zql/src/planner/planner-source.js.map +1 -1
  592. package/out/zql/src/query/query-delegate-base.d.ts +9 -1
  593. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  594. package/out/zql/src/query/query-delegate-base.js +129 -33
  595. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  596. package/out/zql/src/query/query-delegate.d.ts +50 -1
  597. package/out/zql/src/query/query-delegate.d.ts.map +1 -1
  598. package/out/zql/src/query/query-impl.d.ts +13 -11
  599. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  600. package/out/zql/src/query/query-impl.js +366 -60
  601. package/out/zql/src/query/query-impl.js.map +1 -1
  602. package/out/zql/src/query/query-transitions.d.ts +171 -0
  603. package/out/zql/src/query/query-transitions.d.ts.map +1 -0
  604. package/out/zql/src/query/query-transitions.js +204 -0
  605. package/out/zql/src/query/query-transitions.js.map +1 -0
  606. package/out/zql/src/query/query.d.ts +11 -2
  607. package/out/zql/src/query/query.d.ts.map +1 -1
  608. package/out/zql/src/query/runnable-query-impl.d.ts +1 -0
  609. package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
  610. package/out/zql/src/query/runnable-query-impl.js +13 -1
  611. package/out/zql/src/query/runnable-query-impl.js.map +1 -1
  612. package/out/zql/src/query/typed-view.d.ts +1 -1
  613. package/out/zql/src/query/typed-view.d.ts.map +1 -1
  614. package/out/zqlite/src/db.d.ts +9 -1
  615. package/out/zqlite/src/db.d.ts.map +1 -1
  616. package/out/zqlite/src/db.js +8 -2
  617. package/out/zqlite/src/db.js.map +1 -1
  618. package/out/zqlite/src/query-delegate.d.ts.map +1 -1
  619. package/out/zqlite/src/query-delegate.js +5 -6
  620. package/out/zqlite/src/query-delegate.js.map +1 -1
  621. package/out/zqlite/src/sqlite-stat-fanout.d.ts.map +1 -1
  622. package/out/zqlite/src/sqlite-stat-fanout.js +3 -5
  623. package/out/zqlite/src/sqlite-stat-fanout.js.map +1 -1
  624. package/out/zqlite/src/table-source.d.ts +15 -2
  625. package/out/zqlite/src/table-source.d.ts.map +1 -1
  626. package/out/zqlite/src/table-source.js +21 -10
  627. package/out/zqlite/src/table-source.js.map +1 -1
  628. package/package.json +3 -4
@@ -1,4 +1,6 @@
1
1
  import { assert, unreachable } from "../../../../shared/src/asserts.js";
2
+ import { getOrInsertComputed } from "../../../../shared/src/map.js";
3
+ import { newArray } from "../../../../shared/src/arrays.js";
2
4
  import { must } from "../../../../shared/src/must.js";
3
5
  import { h64 } from "../../../../shared/src/hash.js";
4
6
  import { Internal, InvalidConnectionRequest, InvalidConnectionRequestBaseCookie, Rehome } from "../../../../zero-protocol/src/error-kind-enum.js";
@@ -10,12 +12,11 @@ import { manualSpan, startAsyncSpan, startSpan } from "../../../../otel/src/span
10
12
  import { ZERO_VERSION_COLUMN_NAME } from "../replicator/schema/constants.js";
11
13
  import { transformAndHashQuery } from "../../auth/read-authorizer.js";
12
14
  import { randInt } from "../../../../shared/src/rand.js";
13
- import { getOrCreateCounter, getOrCreateLatencyHistogram, getOrCreateNativeHistogram, getOrCreateUpDownCounter } from "../../observability/metrics.js";
15
+ import { getOrCreateCounter, getOrCreateLatencyHistogram, getOrCreateNativeHistogram, getOrCreateUpDownCounter, getOrCreateValueHistogram } from "../../observability/metrics.js";
14
16
  import { rowIDString } from "../../types/row-key.js";
15
17
  import "../replicator/schema/replication-state.js";
16
18
  import { parseSignature } from "./row-set-signature.js";
17
19
  import { ResetPipelinesSignal } from "./snapshotter.js";
18
- import "./pipeline-driver.js";
19
20
  import { Subscription } from "../../types/subscription.js";
20
21
  import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
21
22
  import { ttlClockAsNumber, ttlClockFromNumber } from "./ttl-clock.js";
@@ -25,8 +26,10 @@ import { isAuthErrorBody } from "../../auth/auth.js";
25
26
  import { ClientHandler, startPoke } from "./client-handler.js";
26
27
  import { tracer } from "./tracer.js";
27
28
  import { CVRStore, ClientNotFoundError } from "./cvr-store.js";
28
- import { CVRConfigDrivenUpdater, CVRQueryDrivenUpdater, nextEvictionTime } from "./cvr.js";
29
+ import { CVRConfigDrivenUpdater, CVRQueryDrivenUpdater, classifyQueriesForHydration, nextEvictionTime } from "./cvr.js";
29
30
  import { E2EServingLagTracker } from "./e2e-serving-lag.js";
31
+ import { HydrationBudget } from "./hydration-budget.js";
32
+ import { HydrationCircuitBreaker } from "./hydration-circuit-breaker.js";
30
33
  import { handleInspect } from "./inspect-handler.js";
31
34
  import { QueryCoveringIndex } from "./query-covering.js";
32
35
  import { resolver } from "@rocicorp/resolver";
@@ -61,6 +64,7 @@ var ViewSyncerService = class {
61
64
  #drainCoordinator;
62
65
  #keepaliveMs;
63
66
  #slowHydrateThreshold;
67
+ #hydrationCircuitBreaker;
64
68
  #lastConnectTime = Date.now();
65
69
  /**
66
70
  * The TTL clock is used to determine the time at which queries are considered
@@ -88,14 +92,37 @@ var ViewSyncerService = class {
88
92
  #lock = new Lock();
89
93
  #cvrStore;
90
94
  #stopped = resolver();
95
+ /**
96
+ * Set when {@link #cleanup} begins. Lock tasks that were in flight when the
97
+ * view-syncer was stopped may still complete after the timers have been
98
+ * cleared; this flag prevents them from scheduling new timers, which would
99
+ * otherwise outlive the service (and retain everything it references).
100
+ */
101
+ #shuttingDown = false;
91
102
  #initialized = resolver();
92
103
  #cvr;
93
- #pipelinesSynced = false;
104
+ /**
105
+ * Indicates whether the query pipelines have completed initial catch-up and
106
+ * hydration with the CVR snapshot, and are ready for steady-state operation.
107
+ *
108
+ * - When `false`: The syncer is in its initial catch-up phase. The replica
109
+ * advances without diffs until it reaches `cvr.version.stateVersion`,
110
+ * after which `#maybeHydratePipelines` hydrates queries.
111
+ * - When `true`: Pipelines are fully populated and in steady-state; replica
112
+ * changes advance incrementally via `#advancePipelines`, and client query
113
+ * updates are processed via `#syncQueryPipelineSet`.
114
+ *
115
+ * Resets to `false` if `#advancePipelines` returns a `ResetPipelinesSignal`
116
+ * and pipelines must be reset and rehydrated, or if a reloaded CVR is ahead
117
+ * of the pipelines (see `#resetPipelinesIfBehindCVR`).
118
+ */
119
+ #pipelinesHydrated = false;
94
120
  #servedVersion = null;
95
121
  #e2eServingLagTracker = new E2EServingLagTracker();
96
122
  #expiredQueriesTimer = 0;
97
123
  #authMaintenanceTimer = 0;
98
124
  #setTimeout;
125
+ #now;
99
126
  #customQueryTransformer;
100
127
  #queryReplacements = /* @__PURE__ */ new Map();
101
128
  #activeClients = getOrCreateUpDownCounter("sync", "active-clients", "Number of active sync clients");
@@ -122,10 +149,64 @@ var ViewSyncerService = class {
122
149
  #pipelineResets = getOrCreateCounter("sync", "pipeline-resets", "Number of pipeline resets");
123
150
  #rowSetSignatureDrifts = getOrCreateCounter("sync", "query.row-set-signature-drifts", "Number of times re-hydration of an unchanged query produced a different row-set signature than what is stored in the CVR (forcing a configVersion bump and full re-execution). Expected to be near-zero in steady state; persistent non-zero values indicate non-deterministic query execution (e.g. Cap operator picking different N-row subsets).");
124
151
  #sameHashRehydrationVersionBumps = getOrCreateCounter("sync", "query.same-hash-rehydrations-forced-bump", "Number of times query-set reconciliation forced a configVersion bump for already-gotten same-transformation-hash query rehydration because trackQueries would not otherwise bump. Expected to be near-zero; non-zero values indicate pipeline/CVR row-set drift reached query-set reconciliation.");
152
+ #hydrationBudgetExhaustions = getOrCreateCounter("sync", "hydration_budget_exhaustions", {
153
+ description: "Number of hydration passes that exhaust their budget.",
154
+ unit: "{pass}"
155
+ });
156
+ #hydrationBudgetEvictions = getOrCreateCounter("sync", "hydration_budget_evictions", {
157
+ description: "Number of inactive queries removed after hydration budget exhaustion.",
158
+ unit: "{query}"
159
+ });
160
+ #hydrationBudgetElapsed = getOrCreateValueHistogram("sync", "hydration_budget_elapsed", {
161
+ description: "Elapsed milliseconds when optional query hydration stopped.",
162
+ unit: "ms",
163
+ bucketBoundaries: [
164
+ 1,
165
+ 2,
166
+ 5,
167
+ 10,
168
+ 20,
169
+ 50,
170
+ 100,
171
+ 200,
172
+ 500,
173
+ 1e3,
174
+ 5e3
175
+ ]
176
+ });
177
+ #hydrationBudgetOvershoot = getOrCreateValueHistogram("sync", "hydration_budget_overshoot", {
178
+ description: "Milliseconds elapsed beyond the configured hydration budget.",
179
+ unit: "ms",
180
+ bucketBoundaries: [
181
+ 0,
182
+ 1,
183
+ 2,
184
+ 5,
185
+ 10,
186
+ 20,
187
+ 50,
188
+ 100,
189
+ 200,
190
+ 500,
191
+ 1e3
192
+ ]
193
+ });
194
+ #queryEvictions = getOrCreateCounter("sync", "query_evictions", {
195
+ description: "Number of queries evicted from the CVR ahead of their removal by the client, grouped by reason. Inactive queries are evicted by ttl and hydration-budget; hydration-timeout and hydration-circuit-breaker evict active queries as well.",
196
+ unit: "{query}"
197
+ });
198
+ #hydrationTimeouts = getOrCreateCounter("sync", "hydration_timeouts", {
199
+ description: "Number of query hydrations aborted for exceeding the query hydration timeout.",
200
+ unit: "{query}"
201
+ });
202
+ #hydrationCircuitBreakerRejections = getOrCreateCounter("sync", "hydration_circuit_breaker_rejections", {
203
+ description: "Number of queries rejected without hydration because their hydration circuit breaker was open.",
204
+ unit: "{query}"
205
+ });
125
206
  #inspectorDelegate;
126
207
  #config;
127
208
  #runPriorityOp;
128
- constructor(config, lc, shard, taskID, clientGroupID, cvrDb, pipelineDriver, versionChanges, drainCoordinator, slowHydrateThreshold, inspectorDelegate, connContextManager, customQueryTransformer, runPriorityOp, keepaliveMs = DEFAULT_KEEPALIVE_MS, setTimeoutFn = setTimeout.bind(globalThis)) {
209
+ constructor(config, lc, shard, taskID, clientGroupID, cvrDb, pipelineDriver, versionChanges, drainCoordinator, slowHydrateThreshold, inspectorDelegate, connContextManager, customQueryTransformer, runPriorityOp, keepaliveMs = DEFAULT_KEEPALIVE_MS, setTimeoutFn = setTimeout.bind(globalThis), now = performance.now.bind(performance)) {
129
210
  this.#config = config;
130
211
  this.id = clientGroupID;
131
212
  this.connContextManager = connContextManager;
@@ -140,6 +221,8 @@ var ViewSyncerService = class {
140
221
  this.#customQueryTransformer = customQueryTransformer;
141
222
  this.#cvrStore = new CVRStore(lc, cvrDb, shard, taskID, clientGroupID, () => this.#stateChanges.cancel());
142
223
  this.#setTimeout = setTimeoutFn;
224
+ this.#now = now;
225
+ this.#hydrationCircuitBreaker = new HydrationCircuitBreaker(config.viewSyncerQueryHydrationTimeoutMs ?? 0, void 0, now);
143
226
  this.#runPriorityOp = runPriorityOp;
144
227
  this.keepalive();
145
228
  }
@@ -166,11 +249,13 @@ var ViewSyncerService = class {
166
249
  this.#stateChanges.cancel();
167
250
  return;
168
251
  }
252
+ let reloaded = false;
169
253
  if (!this.#cvr) {
170
254
  this.#lc.debug?.("loading cvr");
171
255
  this.#cvr = await this.#runPriorityOp(lc, "loading cvr", () => this.#cvrStore.load(lc, this.#lastConnectTime));
172
256
  this.#ttlClock = this.#cvr.ttlClock;
173
257
  this.#ttlClockBase = Date.now();
258
+ reloaded = true;
174
259
  } else {
175
260
  const now = Date.now();
176
261
  this.#cvr = {
@@ -179,6 +264,10 @@ var ViewSyncerService = class {
179
264
  };
180
265
  }
181
266
  try {
267
+ if (reloaded && this.#resetPipelinesIfBehindCVR(lc, this.#cvr)) {
268
+ const connCtx = this.connContextManager.getBackgroundConnectionContext();
269
+ if (connCtx) await this.#maybeHydratePipelines(lc, this.#cvr, connCtx);
270
+ }
182
271
  await fn(lc, this.#cvr);
183
272
  } catch (e) {
184
273
  this.#cvr = void 0;
@@ -188,11 +277,47 @@ var ViewSyncerService = class {
188
277
  }
189
278
  });
190
279
  }
280
+ /**
281
+ * The CVR is reloaded after an error clears the cached copy. By then another
282
+ * view-syncer may have taken over the client group and flushed the CVR at a
283
+ * version ahead of this instance's hydrated pipelines (e.g. its replica was
284
+ * further ahead). The pipelines can no longer be diffed against the CVR, so
285
+ * reset them and let `#maybeHydratePipelines` rehydrate once the replica has
286
+ * caught up to the CVR.
287
+ *
288
+ * A CVR behind the pipelines is expected (advancements that do not change
289
+ * the CVR are not flushed) and is handled by the normal update path.
290
+ *
291
+ * Returns whether the pipelines were reset.
292
+ *
293
+ * Must be called from within the #lock.
294
+ */
295
+ #resetPipelinesIfBehindCVR(lc, cvr) {
296
+ if (!this.#pipelinesHydrated) return false;
297
+ const pipelineVersion = this.#pipelines.currentVersion();
298
+ if (pipelineVersion >= cvr.version.stateVersion) return false;
299
+ lc.info?.(`resetting pipelines: pipelines@${pipelineVersion} are behind reloaded cvr@${versionString(cvr.version)}`);
300
+ this.#pipelineResets.add(1, { reason: "behind-cvr" });
301
+ this.#pipelinesHydrated = false;
302
+ this.connContextManager.setSharedRetransformReady(false);
303
+ this.#pipelines.reset(must(cvr.clientSchema, "cvr.clientSchema missing after initialization"));
304
+ return true;
305
+ }
191
306
  readyState() {
192
- return Promise.race([this.#initialized.promise, this.#drainCoordinator.draining]);
307
+ return new Promise((resolve, reject) => {
308
+ const unsubscribe = this.#drainCoordinator.onDraining(() => resolve("draining"));
309
+ this.#initialized.promise.then((state) => {
310
+ unsubscribe();
311
+ resolve(state);
312
+ }, (err) => {
313
+ unsubscribe();
314
+ reject(err);
315
+ });
316
+ });
193
317
  }
194
318
  async run() {
195
319
  try {
320
+ this.#scheduleShutdown(this.#keepaliveMs);
196
321
  if (await this.readyState() === "draining") {
197
322
  this.#lc.debug?.(`draining view-syncer ${this.id} before running`);
198
323
  this.stop();
@@ -213,26 +338,29 @@ var ViewSyncerService = class {
213
338
  lc.info?.(`resetting CVR: ${message}`);
214
339
  throw new ClientNotFoundError(message);
215
340
  }
216
- if (this.#pipelinesSynced) {
341
+ let previousQueries;
342
+ if (this.#pipelinesHydrated) {
217
343
  const result = await this.#advancePipelines(lc, cvr);
218
344
  if (result === "success") return;
219
345
  lc.info?.(`resetting pipelines: ${result.message}`);
220
346
  this.#pipelineResets.add(1, { reason: result.reason });
347
+ switch (result.reason) {
348
+ case "advancement-timeout":
349
+ case "scalar-subquery":
350
+ case "truncation":
351
+ case "schema-change":
352
+ if (!Object.values(cvr.queries).some((q) => q.type === "client")) previousQueries = new Map(this.#pipelines.queries());
353
+ break;
354
+ case "permissions-change": break;
355
+ default: unreachable(result.reason);
356
+ }
221
357
  this.#pipelines.reset(clientSchema);
222
- this.#pipelinesSynced = false;
358
+ this.#pipelinesHydrated = false;
223
359
  this.connContextManager.setSharedRetransformReady(false);
224
360
  }
225
- const version = this.#pipelines.advanceWithoutDiff();
226
- const cvrVer = versionString(cvr.version);
227
- if (version < cvr.version.stateVersion) {
228
- lc.debug?.(`replica@${version} is behind cvr@${cvrVer}`);
229
- return;
230
- }
231
- lc.info?.(`init pipelines@${version} (cvr@${cvrVer})`);
232
- const driftedQueryIDs = await this.#hydrateUnchangedQueries(lc, cvr);
233
- await this.#syncQueryPipelineSet(lc, cvr, "missing", void 0, driftedQueryIDs);
234
- this.#pipelinesSynced = true;
235
- this.connContextManager.setSharedRetransformReady(true);
361
+ const backgroundConnCtx = this.connContextManager.getBackgroundConnectionContext();
362
+ if (backgroundConnCtx) await this.#maybeHydratePipelines(lc, cvr, backgroundConnCtx, previousQueries);
363
+ else lc.info?.("No validated background connection; deferring pipeline init");
236
364
  });
237
365
  }
238
366
  if (this.#drainCoordinator.shouldDrain()) this.#drainCoordinator.drainNextIn(this.#totalHydrationTimeMs());
@@ -246,11 +374,66 @@ var ViewSyncerService = class {
246
374
  this.#stopped.resolve();
247
375
  }
248
376
  }
377
+ /**
378
+ * Ensures that query pipelines are hydrated and ready for steady-state
379
+ * operation.
380
+ *
381
+ * Preconditions:
382
+ * 1. Must be called from within the `#lock`.
383
+ * 2. The caller must provide an active, validated `ConnectionContext` for
384
+ * evaluating custom query transforms and permissions.
385
+ *
386
+ * Readiness prerequisites:
387
+ * - The underlying `QueryPipelineDriver` must be initialized with the schema
388
+ * (i.e. `this.#pipelines.initialized()` is true).
389
+ * - The SQLite replica must have caught up to at least the CVR's
390
+ * `stateVersion` (`version >= cvr.version.stateVersion`).
391
+ *
392
+ * If either prerequisite is not met, this method returns early as a no-op,
393
+ * waiting for the replica to catch up or the driver to initialize.
394
+ *
395
+ * Once ready:
396
+ * - Hydrates unchanged queries from the CVR snapshot without full row diffing.
397
+ * - Syncs missing, errored, or drifted queries via `#syncQueryPipelineSet`.
398
+ * - Sets `#pipelinesHydrated = true` and enables shared retransformations.
399
+ */
400
+ async #maybeHydratePipelines(lc, cvr, connCtx, previousQueries) {
401
+ if (!this.#pipelines.initialized()) return;
402
+ let version;
403
+ try {
404
+ version = this.#pipelines.advanceWithoutDiff();
405
+ } catch (e) {
406
+ if (!(e instanceof ResetPipelinesSignal)) throw e;
407
+ lc.info?.(`resetting pipelines: ${e.message}`);
408
+ this.#pipelineResets.add(1, { reason: e.reason });
409
+ this.#pipelines.reset(must(cvr.clientSchema, "cvr.clientSchema missing after initialization"));
410
+ version = this.#pipelines.currentVersion();
411
+ }
412
+ const cvrVer = versionString(cvr.version);
413
+ if (version < cvr.version.stateVersion) {
414
+ lc.debug?.(`replica@${version} is behind cvr@${cvrVer}`);
415
+ return;
416
+ }
417
+ lc.info?.(`init pipelines@${version} (cvr@${cvrVer})`);
418
+ const hydrationBudget = new HydrationBudget(this.#config.viewSyncerHydrationBudgetMs ?? 0, this.#now);
419
+ const hydrationPassStats = {
420
+ activeHydratedQueries: 0,
421
+ inactiveHydratedQueries: 0
422
+ };
423
+ const driftedQueryIDs = await this.#hydrateUnchangedQueries(lc, cvr, connCtx, hydrationPassStats, hydrationBudget, previousQueries);
424
+ await this.#syncQueryPipelineSet(lc, cvr, "missing", connCtx, driftedQueryIDs, hydrationBudget, hydrationPassStats, previousQueries);
425
+ this.#pipelinesHydrated = true;
426
+ this.connContextManager.setSharedRetransformReady(true);
427
+ }
249
428
  #removeExpiredQueries = async (lc, cvr) => {
250
429
  if (hasExpiredQueries(cvr)) {
251
430
  lc = lc.withContext("method", "#removeExpiredQueries");
252
431
  lc.debug?.("Queries have expired");
253
- if (this.#pipelinesSynced) await this.#syncQueryPipelineSet(lc, cvr, "missing", void 0);
432
+ if (this.#pipelinesHydrated) {
433
+ const connCtx = this.connContextManager.getBackgroundConnectionContext();
434
+ if (connCtx) await this.#syncQueryPipelineSet(lc, cvr, "missing", connCtx);
435
+ else lc.info?.("No validated background connection to remove expired queries; deferring");
436
+ }
254
437
  }
255
438
  this.#scheduleExpireEviction(lc, cvr);
256
439
  };
@@ -332,7 +515,14 @@ var ViewSyncerService = class {
332
515
  return true;
333
516
  }
334
517
  #shutdownTimer = null;
518
+ #stopShutdownTimer() {
519
+ if (this.#shutdownTimer !== null) {
520
+ clearTimeout(this.#shutdownTimer);
521
+ this.#shutdownTimer = null;
522
+ }
523
+ }
335
524
  #scheduleShutdown(delayMs = 0) {
525
+ if (this.#shuttingDown) return;
336
526
  this.#shutdownTimer ??= this.#setTimeout(() => {
337
527
  this.#shutdownTimer = null;
338
528
  this.#runInLockWithCVR(() => {}).catch((e) => this.#stateChanges.fail(e));
@@ -379,6 +569,7 @@ var ViewSyncerService = class {
379
569
  */
380
570
  #scheduleAuthMaintenance(lc) {
381
571
  this.#stopAuthMaintenanceTimer();
572
+ if (this.#shuttingDown) return;
382
573
  const plan = this.connContextManager.planMaintenance();
383
574
  if (plan.earliestDeadlineAt === void 0) {
384
575
  lc.debug?.("No auth maintenance wakeup scheduled");
@@ -466,7 +657,7 @@ var ViewSyncerService = class {
466
657
  }
467
658
  lc.debug?.("Auth changed, re-validating and re-transforming queries");
468
659
  const connCtx = this.connContextManager.mustGetConnectionContext(selector);
469
- if (!this.#pipelinesSynced) {
660
+ if (!this.#pipelinesHydrated) {
470
661
  if (!await this.#validateConnection(connCtx)) return;
471
662
  }
472
663
  return await this.#handleConfigUpdate(lc, clientID, {}, cvr, "all", void 0, connCtx);
@@ -495,6 +686,7 @@ var ViewSyncerService = class {
495
686
  }
496
687
  #startTTLClockInterval(lc) {
497
688
  this.#stopTTLClockInterval();
689
+ if (this.#shuttingDown) return;
498
690
  this.#ttlClockInterval = this.#setTimeout(() => {
499
691
  this.#updateTTLClockInCVRWithoutLock(lc);
500
692
  this.#startTTLClockInterval(lc);
@@ -523,12 +715,13 @@ var ViewSyncerService = class {
523
715
  if (cmpVersions(cvr.version, this.#cvr.version) < 0) {
524
716
  const newCVR = this.#cvr;
525
717
  await startAsyncSpan(tracer, "vs.#updateCVRConfig.pokeClients", async () => {
526
- const pokers = startPoke(this.#getClients(cvr.version), newCVR.version);
718
+ const pokers = startPoke(lc, this.#getClients(cvr.version), newCVR.version);
527
719
  for (const patch of patches) await pokers.addPatch(patch);
528
720
  await pokers.end(newCVR.version);
529
721
  });
530
722
  }
531
- if (this.#pipelinesSynced) await this.#syncQueryPipelineSet(lc, this.#cvr, customQueryTransformMode, connCtx);
723
+ if (!this.#pipelinesHydrated) await this.#maybeHydratePipelines(lc, this.#cvr, connCtx);
724
+ else await this.#syncQueryPipelineSet(lc, this.#cvr, customQueryTransformMode, connCtx);
532
725
  return this.#cvr;
533
726
  }
534
727
  /**
@@ -624,6 +817,7 @@ var ViewSyncerService = class {
624
817
  #scheduleExpireEviction(lc, cvr) {
625
818
  const { ttlClock } = cvr;
626
819
  this.#stopExpireTimer();
820
+ if (this.#shuttingDown) return;
627
821
  const next = nextEvictionTime(cvr);
628
822
  if (next === void 0) {
629
823
  lc.debug?.("no inactive queries with ttl");
@@ -651,7 +845,7 @@ var ViewSyncerService = class {
651
845
  *
652
846
  * This must be called from within the #lock.
653
847
  */
654
- async #hydrateUnchangedQueries(lc, cvr) {
848
+ async #hydrateUnchangedQueries(lc, cvr, connCtx, hydrationPassStats, hydrationBudget, previousQueries) {
655
849
  assert(this.#pipelines.initialized(), "pipelines must be initialized");
656
850
  const dbVersion = this.#pipelines.currentVersion();
657
851
  const cvrVersion = cvr.version;
@@ -660,33 +854,34 @@ var ViewSyncerService = class {
660
854
  return /* @__PURE__ */ new Set();
661
855
  }
662
856
  const gotQueries = Object.entries(cvr.queries).filter(([_, state]) => state.transformationHash !== void 0);
857
+ const { required: requiredGotQueries } = classifyQueriesForHydration(gotQueries.map(([, query]) => query));
663
858
  const customQueries = /* @__PURE__ */ new Map();
664
859
  const otherQueries = [];
665
- let inactivatedCount = 0;
666
- for (const [, query] of gotQueries) {
667
- if (query.type !== "internal" && Object.values(query.clientState).every(({ inactivatedAt }) => inactivatedAt !== void 0)) {
668
- inactivatedCount++;
669
- continue;
670
- }
671
- if (query.type === "custom") customQueries.set(query.id, query);
672
- else otherQueries.push(query);
673
- }
860
+ const inactivatedCount = gotQueries.length - requiredGotQueries.length;
674
861
  const transformedQueries = [];
862
+ for (const query of requiredGotQueries) if (query.type === "custom") {
863
+ const previous = previousQueries?.get(query.id);
864
+ if (previous && previous.transformationHash === query.transformationHash) transformedQueries.push({
865
+ id: query.id,
866
+ transformationHash: previous.transformationHash,
867
+ transformedAst: previous.originalAst ?? previous.transformedAst
868
+ });
869
+ else customQueries.set(query.id, query);
870
+ } else otherQueries.push(query);
675
871
  let customErrorCount = 0;
676
872
  let customHashMismatchCount = 0;
677
873
  let otherHashMismatchCount = 0;
678
874
  if (customQueries.size > 0 && !this.#customQueryTransformer) lc.warn?.("Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache.");
679
- const backgroundContext = this.connContextManager.mustGetBackgroundConnectionContext();
680
875
  const customQueryTransformer = this.#customQueryTransformer;
681
876
  if (customQueryTransformer && customQueries.size > 0) {
682
- const transformedCustomQueries = await this.#runPriorityOp(lc, "#hydrateUnchangedQueries transforming custom queries", () => customQueryTransformer.transform(backgroundContext, customQueries.values()));
683
- if (transformedCustomQueries.kind === "success" && !transformedCustomQueries.cached) this.connContextManager.validateConnection(backgroundContext, backgroundContext.revision, transformedCustomQueries.validation);
877
+ const transformedCustomQueries = await hydrationBudget.excluding(() => this.#runPriorityOp(lc, "#hydrateUnchangedQueries transforming custom queries", () => customQueryTransformer.transform(connCtx, customQueries.values())));
878
+ if (transformedCustomQueries.kind === "success" && !transformedCustomQueries.cached) this.connContextManager.validateConnection(connCtx, connCtx.revision, transformedCustomQueries.validation);
684
879
  if (Array.isArray(transformedCustomQueries.result)) for (const q of transformedCustomQueries.result) if ("error" in q) customErrorCount++;
685
880
  else if (q.transformationHash !== customQueries.get(q.id)?.transformationHash) customHashMismatchCount++;
686
881
  else transformedQueries.push(q);
687
882
  }
688
883
  for (const q of otherQueries) {
689
- const transformed = transformAndHashQuery(lc, q.id, q.ast, must(this.#pipelines.currentPermissions()).permissions ?? { tables: {} }, backgroundContext.auth?.type === "jwt" ? backgroundContext.auth : void 0, q.type === "internal");
884
+ const transformed = transformAndHashQuery(lc, q.id, q.ast, must(this.#pipelines.currentPermissions()).permissions ?? { tables: {} }, connCtx.auth?.type === "jwt" ? connCtx.auth : void 0, q.type === "internal");
690
885
  if (transformed.transformationHash === q.transformationHash) transformedQueries.push(transformed);
691
886
  else otherHashMismatchCount++;
692
887
  }
@@ -699,6 +894,10 @@ var ViewSyncerService = class {
699
894
  for (const { id: queryID, transformationHash, transformedAst } of transformedQueries) {
700
895
  const query = cvr.queries[queryID];
701
896
  const queryName = query.type === "custom" ? query.name : void 0;
897
+ if (query.type !== "internal" && this.#hydrationCircuitBreaker.isOpen(transformationHash)) {
898
+ lc.info?.(`skipping hydration of ${queryID}: hydration circuit breaker open`);
899
+ continue;
900
+ }
702
901
  const covered = queryCoveringIndex ? this.#findQueryCoverageShadowHit(queryCoveringIndex, queryID, transformationHash, transformedAst, queryName) : void 0;
703
902
  totalHydratedQueries++;
704
903
  if (covered) {
@@ -707,19 +906,35 @@ var ViewSyncerService = class {
707
906
  }
708
907
  const timer = new TimeSliceTimer(lc);
709
908
  let count = 0;
909
+ let timedOut = false;
710
910
  await startAsyncSpan(tracer, "vs.#hydrateUnchangedQueries.addQuery", async (span) => {
711
911
  span.setAttribute("queryHash", queryID);
712
912
  span.setAttribute("transformationHash", transformationHash);
713
913
  span.setAttribute("table", transformedAst.table);
714
914
  if (queryName !== void 0) span.setAttribute("queryName", queryName);
715
- for (const change of this.#pipelines.addQuery(transformationHash, queryID, transformedAst, await timer.start(), queryName, "unchanged-query-rehydrate")) if (change === "yield") await timer.yieldProcess("yield in hydrateUnchangedQueries");
716
- else count++;
915
+ for (const change of this.#pipelines.addQuery(transformationHash, queryID, transformedAst, await timer.start(), queryName, "unchanged-query-rehydrate")) if (change === "yield") {
916
+ if (query.type !== "internal" && this.#hydrationCircuitBreaker.exceeded(timer.totalElapsed())) {
917
+ timedOut = true;
918
+ await timer.yieldProcess("yield in hydrateUnchangedQueries");
919
+ break;
920
+ }
921
+ await timer.yieldProcess("yield in hydrateUnchangedQueries");
922
+ } else count++;
717
923
  });
718
924
  const elapsed = timer.totalElapsed();
925
+ if (timedOut) {
926
+ this.#recordHydrationTimeout(lc, {
927
+ id: queryID,
928
+ transformationHash,
929
+ name: queryName
930
+ }, elapsed, count);
931
+ continue;
932
+ }
933
+ hydrationPassStats.activeHydratedQueries++;
719
934
  this.#hydrations.add(1);
720
935
  this.#hydrationTime.recordMs(elapsed);
721
- this.#addQueryMaterializationServerMetric(transformationHash, elapsed);
722
- this.#inspectorDelegate.addQuery(transformationHash, transformedAst);
936
+ this.#addQueryMaterializationServerMetric(queryID, elapsed);
937
+ this.#inspectorDelegate.addQuery(queryID, transformedAst);
723
938
  lc.debug?.(`hydrated ${count} rows for ${queryID} (${elapsed} ms)`);
724
939
  let drifted = false;
725
940
  const storedSigHex = cvr.queries[queryID]?.rowSetSignature;
@@ -780,11 +995,7 @@ var ViewSyncerService = class {
780
995
  return;
781
996
  }
782
997
  const appErrorGroups = /* @__PURE__ */ new Map();
783
- for (const err of errorOrErrors) for (const clientId of getAffectedClientIDs([err.id])) {
784
- const group = appErrorGroups.get(clientId) ?? [];
785
- group.push(err);
786
- appErrorGroups.set(clientId, group);
787
- }
998
+ for (const err of errorOrErrors) for (const clientId of getAffectedClientIDs([err.id])) getOrInsertComputed(appErrorGroups, clientId, newArray).push(err);
788
999
  for (const [clientId, errors] of appErrorGroups) this.#clients.get(clientId)?.sendQueryTransformApplicationErrors(errors);
789
1000
  }
790
1001
  #addQueryMaterializationServerMetric(queryID, elapsed) {
@@ -820,7 +1031,10 @@ var ViewSyncerService = class {
820
1031
  *
821
1032
  * This must be called from within the #lock.
822
1033
  */
823
- #syncQueryPipelineSet(lc, cvr, customQueryTransformMode, connCtx, driftedQueryIDs = /* @__PURE__ */ new Set()) {
1034
+ #syncQueryPipelineSet(lc, cvr, customQueryTransformMode, connCtx, driftedQueryIDs = /* @__PURE__ */ new Set(), hydrationBudget = new HydrationBudget(this.#config.viewSyncerHydrationBudgetMs ?? 0, this.#now), hydrationPassStats = {
1035
+ activeHydratedQueries: 0,
1036
+ inactiveHydratedQueries: 0
1037
+ }, previousQueries) {
824
1038
  return startAsyncSpan(tracer, "vs.#syncQueryPipelineSet", async (span) => {
825
1039
  const start = performance.now();
826
1040
  span.setAttribute("clientGroupID", this.id);
@@ -829,38 +1043,45 @@ var ViewSyncerService = class {
829
1043
  const now = Date.now();
830
1044
  const ttlClock = this.#getTTLClock(now);
831
1045
  const cvrQueryEntires = Object.entries(cvr.queries);
832
- const customQueries = /* @__PURE__ */ new Map();
833
- const otherQueries = [];
834
1046
  const transformedQueries = [];
835
- const resolvedConnCtx = connCtx ?? this.connContextManager.mustGetBackgroundConnectionContext();
836
- for (const [id, query] of cvrQueryEntires) if (query.type === "custom") {
837
- assert(id === query.id, "custom query id mismatch");
838
- customQueries.set(id, query);
839
- } else otherQueries.push({
840
- id,
841
- query
842
- });
843
- for (const { id, query: origQuery } of otherQueries) {
844
- assert(id === origQuery.id, "query id mismatch");
845
- const transformed = transformAndHashQuery(lc, origQuery.id, origQuery.ast, must(this.#pipelines.currentPermissions()).permissions ?? { tables: {} }, resolvedConnCtx.auth?.type === "jwt" ? resolvedConnCtx.auth : void 0, origQuery.type === "internal");
846
- transformedQueries.push({
847
- id,
848
- origQuery,
849
- transformed
850
- });
851
- }
852
- if (customQueries.size > 0 && !this.#customQueryTransformer) lc.warn?.("Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache.");
853
- let erroredQueryIDs;
854
- const customQueriesToTransform = customQueryTransformMode === "all" ? [...customQueries.values()] : customQueryTransformMode && [...customQueries.values()].filter((q) => !this.#pipelines.queries().has(q.id));
1047
+ const naturallyExpiredQueryIDs = new Set(cvrQueryEntires.map(([, query]) => query).filter((query) => expired(ttlClock, query)).map((query) => query.id));
1048
+ if (naturallyExpiredQueryIDs.size > 0) this.#queryEvictions.add(naturallyExpiredQueryIDs.size, { reason: "ttl" });
1049
+ const hydrationCandidates = cvrQueryEntires.map(([, query]) => query).filter((query) => !naturallyExpiredQueryIDs.has(query.id));
1050
+ const { required, optional } = classifyQueriesForHydration(hydrationCandidates);
1051
+ const requiredQueryIDs = new Set(required.map((query) => query.id));
1052
+ const optionalToHydrate = hydrationBudget.limitMs === 0 ? optional : optional.filter((query) => query.patchVersion !== void 0);
1053
+ if (!this.#customQueryTransformer && hydrationCandidates.some((query) => query.type === "custom")) lc.warn?.("Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache.");
855
1054
  const customQueryTransformer = this.#customQueryTransformer;
856
- if (customQueryTransformer && customQueriesToTransform.length > 0) {
1055
+ const erroredQueryIDs = [];
1056
+ let transformedCustomQueryCount = 0;
1057
+ const transformOtherQueries = (queries) => {
1058
+ for (const origQuery of queries) {
1059
+ if (origQuery.type === "custom") continue;
1060
+ const transformed = transformAndHashQuery(lc, origQuery.id, origQuery.ast, must(this.#pipelines.currentPermissions()).permissions ?? { tables: {} }, connCtx.auth?.type === "jwt" ? connCtx.auth : void 0, origQuery.type === "internal");
1061
+ transformedQueries.push({
1062
+ id: origQuery.id,
1063
+ origQuery,
1064
+ transformed
1065
+ });
1066
+ }
1067
+ };
1068
+ const shouldTransformCustomQuery = (query) => {
1069
+ if (customQueryTransformMode === "all") return true;
1070
+ if (this.#pipelines.queries().has(query.id)) return false;
1071
+ const previous = previousQueries?.get(query.id);
1072
+ if (previous && previous.transformationHash === query.transformationHash) return false;
1073
+ return true;
1074
+ };
1075
+ const transformCustomQueries = async (queries) => {
1076
+ if (!customQueryTransformer || queries.length === 0) return;
1077
+ transformedCustomQueryCount += queries.length;
857
1078
  const transformStart = performance.now();
858
1079
  let transformedCustomQueries;
859
1080
  try {
860
- transformedCustomQueries = await this.#runPriorityOp(lc, "#syncQueryPipelineSet transforming custom queries", () => customQueryTransformer.transform(resolvedConnCtx, customQueriesToTransform));
1081
+ transformedCustomQueries = await hydrationBudget.excluding(() => this.#runPriorityOp(lc, "#syncQueryPipelineSet transforming custom queries", () => customQueryTransformer.transform(connCtx, queries)));
861
1082
  if (transformedCustomQueries.kind === "failed") throw new ProtocolErrorWithLevel(transformedCustomQueries.result, "warn");
862
1083
  else {
863
- if (!transformedCustomQueries.cached) this.connContextManager.validateConnection(resolvedConnCtx, resolvedConnCtx.revision, transformedCustomQueries.validation);
1084
+ if (!transformedCustomQueries.cached) this.connContextManager.validateConnection(connCtx, connCtx.revision, transformedCustomQueries.validation);
864
1085
  this.#queryTransformations.add(1, { result: "success" });
865
1086
  }
866
1087
  } catch (e) {
@@ -871,8 +1092,9 @@ var ViewSyncerService = class {
871
1092
  this.#queryTransformationTime.recordMs(transformDuration);
872
1093
  }
873
1094
  const successfullyTransformedCustomQueries = /* @__PURE__ */ new Map();
874
- erroredQueryIDs = this.#processTransformedCustomQueries(lc, transformedCustomQueries.result, (q) => {
875
- const origQuery = customQueries.get(q.id);
1095
+ const customQueryMap = new Map(queries.map((query) => [query.id, query]));
1096
+ erroredQueryIDs.push(...this.#processTransformedCustomQueries(lc, transformedCustomQueries.result, (q) => {
1097
+ const origQuery = customQueryMap.get(q.id);
876
1098
  if (origQuery) {
877
1099
  successfullyTransformedCustomQueries.set(q.id, q);
878
1100
  transformedQueries.push({
@@ -881,7 +1103,7 @@ var ViewSyncerService = class {
881
1103
  transformed: q
882
1104
  });
883
1105
  }
884
- }, customQueries);
1106
+ }, customQueryMap));
885
1107
  for (const [queryID, newTransform] of successfullyTransformedCustomQueries) {
886
1108
  const existingTransformHash = cvr.queries[queryID]?.transformationHash;
887
1109
  if (existingTransformHash) {
@@ -894,21 +1116,55 @@ var ViewSyncerService = class {
894
1116
  } else this.#queryTransformationNoOps.add(1);
895
1117
  }
896
1118
  }
1119
+ };
1120
+ const queriesToTransform = [...required, ...optionalToHydrate];
1121
+ transformOtherQueries(queriesToTransform);
1122
+ if (customQueryTransformMode === "missing" && previousQueries) {
1123
+ for (const query of queriesToTransform) if (query.type === "custom" && !this.#pipelines.queries().has(query.id)) {
1124
+ const previous = previousQueries.get(query.id);
1125
+ if (previous && previous.transformationHash === query.transformationHash) {
1126
+ this.#queryTransformationNoOps.add(1);
1127
+ transformedQueries.push({
1128
+ id: query.id,
1129
+ origQuery: query,
1130
+ transformed: {
1131
+ id: query.id,
1132
+ transformationHash: previous.transformationHash,
1133
+ transformedAst: previous.originalAst ?? previous.transformedAst
1134
+ }
1135
+ });
1136
+ }
1137
+ }
897
1138
  }
898
- const removeQueriesQueryIds = new Set([...Object.values(cvr.queries).filter((q) => expired(ttlClock, q)).map((q) => q.id), ...erroredQueryIDs || []]);
1139
+ await transformCustomQueries(queriesToTransform.filter((query) => query.type === "custom" && shouldTransformCustomQuery(query)));
1140
+ const circuitBrokenQueries = transformedQueries.filter(({ id, origQuery, transformed }) => origQuery.type !== "internal" && this.#pipelines.queries().get(id)?.transformationHash !== transformed.transformationHash && this.#hydrationCircuitBreaker.isOpen(transformed.transformationHash)).map(({ id, origQuery, transformed }) => ({
1141
+ id,
1142
+ transformationHash: transformed.transformationHash,
1143
+ name: origQuery.type === "custom" ? origQuery.name : void 0
1144
+ }));
1145
+ if (circuitBrokenQueries.length > 0) this.#rejectCircuitBrokenQueries(lc, cvr, circuitBrokenQueries);
1146
+ const removeQueriesQueryIds = new Set([
1147
+ ...naturallyExpiredQueryIDs,
1148
+ ...erroredQueryIDs,
1149
+ ...circuitBrokenQueries.map(({ id }) => id)
1150
+ ]);
899
1151
  const addQueries = transformedQueries.map(({ id, origQuery, transformed }) => ({
900
1152
  id,
901
1153
  ast: transformed.transformedAst,
902
1154
  transformationHash: transformed.transformationHash,
903
1155
  name: origQuery.type === "custom" ? origQuery.name : void 0
904
1156
  })).filter((q) => !removeQueriesQueryIds.has(q.id) && this.#pipelines.queries().get(q.id)?.transformationHash !== q.transformationHash);
905
- lc.info?.(`syncQueryPipelineSet: ${cvrQueryEntires.length} CVR queries, ${customQueriesToTransform.length} custom re-transformed, ${erroredQueryIDs?.length ?? 0} errored, ${removeQueriesQueryIds.size} to remove, ${addQueries.length} to add`);
1157
+ const hydrationOrder = new Map([...required, ...optional].map((query, index) => [query.id, index]));
1158
+ addQueries.sort((a, b) => must(hydrationOrder.get(a.id)) - must(hydrationOrder.get(b.id)));
1159
+ const requiredAddQueries = addQueries.filter((query) => requiredQueryIDs.has(query.id));
1160
+ const optionalAddQueries = addQueries.filter((query) => !requiredQueryIDs.has(query.id));
1161
+ lc.info?.(`syncQueryPipelineSet: ${cvrQueryEntires.length} CVR queries, ${transformedCustomQueryCount} custom re-transformed, ${erroredQueryIDs.length} errored, ${circuitBrokenQueries.length} circuit-broken, ${removeQueriesQueryIds.size} to remove, ${addQueries.length} to add`);
906
1162
  for (const q of addQueries) {
907
1163
  const orig = cvr.queries[q.id];
908
1164
  lc.debug?.("ViewSyncer adding query", q.ast, "transformed from", orig.type === "custom" ? orig.name : orig.ast);
909
1165
  }
910
1166
  if (addQueries.length > 0 || removeQueriesQueryIds.size > 0) {
911
- await this.#addAndRemoveQueries(lc, cvr, addQueries, Array.from(removeQueriesQueryIds, (id) => ({ id })), driftedQueryIDs);
1167
+ await this.#addAndRemoveQueries(lc, cvr, requiredAddQueries, optionalAddQueries, Array.from(removeQueriesQueryIds, (id) => ({ id })), hydrationBudget, hydrationPassStats, driftedQueryIDs);
912
1168
  if (addQueries.length > 0) this.#viewSyncerHydration.recordMs(performance.now() - start);
913
1169
  } else await this.#catchupClients(lc, cvr);
914
1170
  });
@@ -941,8 +1197,93 @@ var ViewSyncerService = class {
941
1197
  record.count++;
942
1198
  if (record.count >= THRASH_THRESHOLD) this.#lc.warn?.(`Query thrashing detected for query ${queryID}. ${record.count} replacements in 60s. This may indicate clients with different auth contexts connecting to the same client group.`);
943
1199
  }
944
- #addAndRemoveQueries(lc, cvr, addQueries, removeQueries, driftedQueryIDs = /* @__PURE__ */ new Set()) {
1200
+ /**
1201
+ * Records that hydrating `query` was aborted for exceeding the query
1202
+ * hydration timeout, and opens its circuit breaker.
1203
+ */
1204
+ #recordHydrationTimeout(lc, query, elapsedMs, rowCount) {
1205
+ this.#hydrationCircuitBreaker.trip(query.transformationHash);
1206
+ this.#hydrationTimeouts.add(1);
1207
+ lc.warn?.("Query hydration aborted for exceeding the hydration timeout", {
1208
+ clientGroupID: this.id,
1209
+ queryHash: query.id,
1210
+ transformationHash: query.transformationHash,
1211
+ ...query.name !== void 0 && { queryName: query.name },
1212
+ hydrationTimeoutMs: this.#hydrationCircuitBreaker.timeoutMs,
1213
+ hydrationElapsedMs: elapsedMs,
1214
+ ...rowCount !== void 0 && { hydrationRowCount: rowCount },
1215
+ circuitBreakerOpenMs: this.#hydrationCircuitBreaker.openMs
1216
+ });
1217
+ }
1218
+ /** Records that `query` was rejected without hydration by its open breaker. */
1219
+ #recordCircuitBreakerRejection(lc, query) {
1220
+ this.#hydrationCircuitBreakerRejections.add(1);
1221
+ lc.warn?.("Query rejected by its open hydration circuit breaker", {
1222
+ clientGroupID: this.id,
1223
+ queryHash: query.id,
1224
+ transformationHash: query.transformationHash,
1225
+ ...query.name !== void 0 && { queryName: query.name },
1226
+ hydrationTimeoutMs: this.#hydrationCircuitBreaker.timeoutMs,
1227
+ circuitBreakerOpenMs: this.#hydrationCircuitBreaker.openMs
1228
+ });
1229
+ }
1230
+ /**
1231
+ * Handles queries whose hydration circuit breaker is open: they are counted,
1232
+ * logged, and errored to the affected clients. The caller removes them from
1233
+ * the CVR.
1234
+ */
1235
+ #rejectCircuitBrokenQueries(lc, cvr, queries) {
1236
+ for (const query of queries) this.#recordCircuitBreakerRejection(lc, query);
1237
+ this.#queryEvictions.add(queries.length, { reason: "hydration-circuit-breaker" });
1238
+ this.#sendHydrationTimeoutErrors(cvr, queries);
1239
+ }
1240
+ /**
1241
+ * Sends a per-query error to every client that desires one of `queries`.
1242
+ * The error goes out as a `transformError` application error, which every
1243
+ * client understands as "this query errored" without affecting the
1244
+ * connection or the client's other queries.
1245
+ */
1246
+ #sendHydrationTimeoutErrors(cvr, queries) {
1247
+ const timeoutMs = this.#hydrationCircuitBreaker.timeoutMs;
1248
+ const errorsByClient = /* @__PURE__ */ new Map();
1249
+ for (const { id, name } of queries) {
1250
+ const query = cvr.queries[id];
1251
+ if (query === void 0 || query.type === "internal") continue;
1252
+ const error = {
1253
+ error: "app",
1254
+ id,
1255
+ name: name ?? (query.type === "custom" ? query.name : "legacy"),
1256
+ message: `Query hydration exceeded the ${timeoutMs}ms limit (ZERO_VIEW_SYNCER_QUERY_HYDRATION_TIMEOUT_MS) and was aborted`,
1257
+ details: {
1258
+ kind: "HydrationTimeout",
1259
+ timeoutMs
1260
+ }
1261
+ };
1262
+ for (const clientID of Object.keys(query.clientState)) getOrInsertComputed(errorsByClient, clientID, newArray).push(error);
1263
+ }
1264
+ for (const [clientID, errors] of errorsByClient) this.#clients.get(clientID)?.sendQueryTransformApplicationErrors(errors);
1265
+ }
1266
+ #recordHydrationBudgetExhaustion(lc, hydrationBudget, activeHydratedQueries, inactiveHydratedQueries, inactiveEvictedQueryIDs) {
1267
+ const elapsedMs = hydrationBudget.exhaustedAtMs;
1268
+ if (elapsedMs === void 0) return;
1269
+ this.#hydrationBudgetExhaustions.add(1);
1270
+ this.#hydrationBudgetEvictions.add(inactiveEvictedQueryIDs.length);
1271
+ this.#hydrationBudgetElapsed.record(elapsedMs);
1272
+ this.#hydrationBudgetOvershoot.record(Math.max(0, elapsedMs - hydrationBudget.limitMs));
1273
+ if (inactiveEvictedQueryIDs.length > 0) this.#queryEvictions.add(inactiveEvictedQueryIDs.length, { reason: "hydration-budget" });
1274
+ lc.info?.("view-syncer hydration budget exhausted", {
1275
+ clientGroupID: this.id,
1276
+ hydrationBudgetMs: hydrationBudget.limitMs,
1277
+ hydrationElapsedMs: elapsedMs,
1278
+ activeHydratedQueries,
1279
+ inactiveHydratedQueries,
1280
+ inactiveEvictedQueries: inactiveEvictedQueryIDs.length,
1281
+ firstEvictedQueryHash: inactiveEvictedQueryIDs[0] ?? null
1282
+ });
1283
+ }
1284
+ #addAndRemoveQueries(lc, cvr, requiredQueries, optionalQueries, removeQueries, hydrationBudget, hydrationPassStats, driftedQueryIDs = /* @__PURE__ */ new Set()) {
945
1285
  return startAsyncSpan(tracer, "vs.#addAndRemoveQueries", async () => {
1286
+ const addQueries = [...requiredQueries, ...optionalQueries];
946
1287
  assert(addQueries.length > 0 || removeQueries.length > 0, "Must have queries to add or remove");
947
1288
  const start = performance.now();
948
1289
  const stateVersion = this.#pipelines.currentVersion();
@@ -959,7 +1300,9 @@ var ViewSyncerService = class {
959
1300
  updater.ensureNewVersion();
960
1301
  }
961
1302
  const { queryPatches, newVersion } = updater.trackQueries(lc, addQueries, removeQueries);
962
- const pokers = startPoke(this.#getClients(), newVersion);
1303
+ if (hydrationBudget.limitMs !== 0 && optionalQueries.length > 0) assert(cmpVersions(cvr.version, newVersion) < 0, "Optional hydration requires a final poke version before row processing so a removal is actually poked");
1304
+ const clients = this.#getClients();
1305
+ const pokers = startPoke(lc, clients, newVersion);
963
1306
  for (const patch of queryPatches) await pokers.addPatch(patch);
964
1307
  for (const q of removeQueries) {
965
1308
  this.#pipelines.removeQuery(q.id);
@@ -975,22 +1318,54 @@ var ViewSyncerService = class {
975
1318
  let totalHydratedQueries = 0;
976
1319
  let coveredHydratedQueries = 0;
977
1320
  let firstCoveredQuery;
1321
+ const hydratedQueryIDs = [];
1322
+ const budgetEvictedQueryIDs = [];
1323
+ const timedOutQueries = [];
1324
+ const rejectedQueries = [];
1325
+ const circuitBreaker = this.#hydrationCircuitBreaker;
978
1326
  const self = this;
979
1327
  await yieldProcess(lc);
980
- function* generateRowChanges(slowHydrateThreshold) {
981
- for (const q of addQueries) {
1328
+ function* hydrateQueries(queries, optional, slowHydrateThreshold) {
1329
+ for (let i = 0; i < queries.length; i++) {
1330
+ if (optional && hydrationBudget.exhausted()) {
1331
+ budgetEvictedQueryIDs.push(...queries.slice(i).map((query) => query.id));
1332
+ return;
1333
+ }
1334
+ const q = must(queries[i]);
982
1335
  let queryLC = lc.withContext("hash", q.id).withContext("queryHash", q.id).withContext("transformationHash", q.transformationHash);
983
1336
  if (q.name !== void 0) queryLC = queryLC.withContext("queryName", q.name);
984
1337
  queryLC.debug?.(`adding pipeline for query`, q.ast);
1338
+ const breakable = cvr.queries[q.id]?.type !== "internal";
1339
+ if (breakable && circuitBreaker.isOpen(q.transformationHash)) {
1340
+ rejectedQueries.push(q);
1341
+ self.#recordCircuitBreakerRejection(queryLC, q);
1342
+ continue;
1343
+ }
985
1344
  const covered = queryCoveringIndex ? self.#findQueryCoverageShadowHit(queryCoveringIndex, q.id, q.transformationHash, q.ast, q.name) : void 0;
986
1345
  totalHydratedQueries++;
987
1346
  if (covered) {
988
1347
  coveredHydratedQueries++;
989
1348
  firstCoveredQuery ??= covered;
990
1349
  }
991
- yield* pipelines.addQuery(q.transformationHash, q.id, q.ast, timer.startWithoutYielding(), q.name, "query-set-sync");
1350
+ let timedOut = false;
1351
+ for (const change of pipelines.addQuery(q.transformationHash, q.id, q.ast, timer.startWithoutYielding(), q.name, "query-set-sync")) {
1352
+ if (change === "yield" && breakable && circuitBreaker.exceeded(timer.totalElapsed())) {
1353
+ timedOut = true;
1354
+ yield change;
1355
+ break;
1356
+ }
1357
+ yield change;
1358
+ }
992
1359
  const elapsed = timer.stop();
993
1360
  totalProcessTime += elapsed;
1361
+ if (timedOut) {
1362
+ timedOutQueries.push(q);
1363
+ self.#recordHydrationTimeout(queryLC, q, elapsed);
1364
+ continue;
1365
+ }
1366
+ hydratedQueryIDs.push(q.id);
1367
+ if (optional) hydrationPassStats.inactiveHydratedQueries++;
1368
+ else hydrationPassStats.activeHydratedQueries++;
994
1369
  self.#addQueryMaterializationServerMetric(q.id, elapsed);
995
1370
  self.#inspectorDelegate.addQuery(q.id, q.ast);
996
1371
  queryCoveringIndex?.add(q.id, {
@@ -1004,18 +1379,43 @@ var ViewSyncerService = class {
1004
1379
  transformationHash: q.transformationHash,
1005
1380
  ...q.name !== void 0 && { name: q.name }
1006
1381
  });
1382
+ hydrations.add(1);
1383
+ hydrationTime.recordMs(elapsed);
1007
1384
  }
1008
- hydrations.add(1);
1009
- hydrationTime.recordMs(totalProcessTime);
1385
+ }
1386
+ function* generateRowChanges(slowHydrateThreshold) {
1387
+ yield* hydrateQueries(requiredQueries, false, slowHydrateThreshold);
1388
+ yield* hydrateQueries(optionalQueries, true, slowHydrateThreshold);
1010
1389
  }
1011
1390
  await this.#processChanges(lc, timer, generateRowChanges(this.#slowHydrateThreshold), updater, pokers);
1391
+ const abortedQueries = [...timedOutQueries, ...rejectedQueries];
1392
+ if (abortedQueries.length > 0) {
1393
+ const abortedQueryIDs = abortedQueries.map(({ id }) => id);
1394
+ for (const patch of await updater.abortExecutedQueries(lc, abortedQueryIDs)) await pokers.addPatch(patch);
1395
+ for (const queryID of abortedQueryIDs) {
1396
+ this.#pipelines.removeQuery(queryID);
1397
+ this.#inspectorDelegate.removeQuery(queryID);
1398
+ this.#queryReplacements.delete(queryID);
1399
+ }
1400
+ if (timedOutQueries.length > 0) this.#queryEvictions.add(timedOutQueries.length, { reason: "hydration-timeout" });
1401
+ if (rejectedQueries.length > 0) this.#queryEvictions.add(rejectedQueries.length, { reason: "hydration-circuit-breaker" });
1402
+ this.#sendHydrationTimeoutErrors(cvr, abortedQueries);
1403
+ }
1404
+ for (const patch of updater.removeTrackedQueries(budgetEvictedQueryIDs)) await pokers.addPatch(patch);
1405
+ for (const queryID of budgetEvictedQueryIDs) {
1406
+ this.#pipelines.removeQuery(queryID);
1407
+ this.#inspectorDelegate.removeQuery(queryID);
1408
+ this.#queryReplacements.delete(queryID);
1409
+ }
1410
+ this.#recordHydrationBudgetExhaustion(lc, hydrationBudget, hydrationPassStats.activeHydratedQueries, hydrationPassStats.inactiveHydratedQueries, budgetEvictedQueryIDs);
1012
1411
  this.#logQueryCoverageShadowSummary(lc, "add", totalHydratedQueries, coveredHydratedQueries, firstCoveredQuery);
1013
1412
  await startAsyncSpan(tracer, "vs.#syncQueryPipelineSet.deleteUnreferencedRows", async () => {
1014
1413
  for (const patch of await updater.deleteUnreferencedRows(lc)) await pokers.addPatch(patch);
1015
1414
  });
1016
1415
  this.#cvr = await this.#flushUpdater(lc, updater);
1416
+ if (budgetEvictedQueryIDs.length > 0) this.#scheduleExpireEviction(lc, this.#cvr);
1017
1417
  const finalVersion = this.#cvr.version;
1018
- await this.#catchupClients(lc, cvr, finalVersion, addQueries.map((q) => q.id), pokers);
1418
+ await this.#catchupClients(lc, cvr, finalVersion, hydratedQueryIDs, pokers);
1019
1419
  await startAsyncSpan(tracer, "vs.#syncQueryPipelineSet.pokeEnd", () => pokers.end(finalVersion));
1020
1420
  this.#markVersionServed(stateVersion);
1021
1421
  const wallTime = performance.now() - start;
@@ -1044,7 +1444,7 @@ var ViewSyncerService = class {
1044
1444
  return startAsyncSpan(tracer, "vs.#catchupClients", async (span) => {
1045
1445
  current ??= cvr.version;
1046
1446
  const clients = this.#getClients();
1047
- const pokers = usePokers ?? startPoke(clients, cvr.version);
1447
+ const pokers = usePokers ?? startPoke(lc, clients, cvr.version);
1048
1448
  span.setAttribute("numClients", clients.length);
1049
1449
  const catchupFrom = clients.map((c) => c.version()).reduce((a, b) => cmpVersions(a, b) < 0 ? a : b, cvr.version);
1050
1450
  const rowPatches = this.#cvrStore.catchupRowPatches(lc, catchupFrom, cvr, current, excludeQueryHashes);
@@ -1156,7 +1556,9 @@ var ViewSyncerService = class {
1156
1556
  *
1157
1557
  * Must be called from within the #lock.
1158
1558
  *
1159
- * Returns false if the advancement failed due to a schema change.
1559
+ * Returns 'success' if changes were successfully processed and poked,
1560
+ * or a `ResetPipelinesSignal` if advancement aborted (e.g. schema change,
1561
+ * timeout) and pipelines need to be reset and rehydrated.
1160
1562
  */
1161
1563
  #advancePipelines(lc, cvr) {
1162
1564
  return startAsyncSpan(tracer, "vs.#advancePipelines", async (span) => {
@@ -1164,20 +1566,27 @@ var ViewSyncerService = class {
1164
1566
  assert(this.#pipelines.initialized(), "pipelines must be initialized (advancePipelines");
1165
1567
  const start = performance.now();
1166
1568
  const timer = new TimeSliceTimer(lc);
1167
- const { version, numChanges, changes } = this.#pipelines.advance(timer);
1168
- lc = lc.withContext("newVersion", version);
1169
- const updater = new CVRQueryDrivenUpdater(this.#cvrStore, cvr, version, this.#pipelines.replicaVersion, (queryID) => this.#pipelines.rowSetSignature(queryID));
1170
- const pokers = startPoke(this.#getClients(cvr.version), updater.updatedVersion());
1171
- lc.debug?.(`applying ${numChanges} to advance to ${version}`);
1569
+ let pokers;
1570
+ let updater;
1571
+ let version;
1572
+ let numChanges = 0;
1172
1573
  try {
1173
- await this.#processChanges(lc, await timer.start(), changes, updater, pokers);
1574
+ const advancement = this.#pipelines.advance(timer);
1575
+ version = advancement.version;
1576
+ numChanges = advancement.numChanges;
1577
+ lc = lc.withContext("newVersion", version);
1578
+ updater = new CVRQueryDrivenUpdater(this.#cvrStore, cvr, version, this.#pipelines.replicaVersion, (queryID) => this.#pipelines.rowSetSignature(queryID));
1579
+ pokers = startPoke(lc, this.#getClients(cvr.version), updater.updatedVersion());
1580
+ lc.debug?.(`applying ${numChanges} to advance to ${version}`);
1581
+ await this.#processChanges(lc, await timer.start(), advancement.changes, updater, pokers);
1174
1582
  } catch (e) {
1175
1583
  if (e instanceof ResetPipelinesSignal) {
1176
- await pokers.cancel();
1584
+ await pokers?.cancel();
1177
1585
  return e;
1178
1586
  }
1179
1587
  throw e;
1180
1588
  }
1589
+ assert(updater && pokers && version !== void 0, "advancement state missing");
1181
1590
  this.#cvr = await this.#flushUpdater(lc, updater);
1182
1591
  const finalVersion = this.#cvr.version;
1183
1592
  await startAsyncSpan(tracer, "vs.#advancePipelines.pokeEnd", () => pokers.end(finalVersion));
@@ -1198,7 +1607,7 @@ var ViewSyncerService = class {
1198
1607
  clientID,
1199
1608
  wsID: client.wsID
1200
1609
  });
1201
- return handleInspect(lc, body, cvr, client, this.#inspectorDelegate, this.id, this.#cvrStore, this.#config, connCtx);
1610
+ return handleInspect(lc, body, cvr, client, this.#inspectorDelegate, this.id, this, this.#cvrStore, this.#config, connCtx);
1202
1611
  };
1203
1612
  async #runBackgroundRetransform(lc) {
1204
1613
  const attemptRetransform = async (connCtx) => {
@@ -1285,15 +1694,18 @@ var ViewSyncerService = class {
1285
1694
  return this.#stopped.promise;
1286
1695
  }
1287
1696
  async #cleanup(err) {
1697
+ this.#shuttingDown = true;
1288
1698
  this.connContextManager.setSharedRetransformReady(false);
1289
1699
  this.#stopTTLClockInterval();
1290
1700
  this.#stopExpireTimer();
1291
1701
  this.#stopAuthMaintenanceTimer();
1702
+ this.#stopShutdownTimer();
1292
1703
  this.#customQueryTransformer?.destroy();
1293
1704
  for (const client of this.#clients.values()) if (err) client.fail(err);
1294
1705
  else client.close(`closed clientGroupID=${this.id}`);
1295
1706
  await this.#lock.withLock(() => {});
1296
1707
  this.#pipelines.destroy();
1708
+ this.#inspectorDelegate.clearAuthenticated(this.id, this);
1297
1709
  }
1298
1710
  /**
1299
1711
  * Test helper: Manually mark initialization as complete.
@@ -1329,8 +1741,10 @@ function isTransformFailedError(error) {
1329
1741
  return error.errorBody.kind === "TransformFailed" && !isAuthErrorBody(error.errorBody);
1330
1742
  }
1331
1743
  /**
1332
- * A query must be expired for all clients in order to be considered
1333
- * expired.
1744
+ * Whether a query's TTL has elapsed. A query must be expired for all clients
1745
+ * in order to be considered expired.
1746
+ *
1747
+ * Exported for testing.
1334
1748
  */
1335
1749
  function expired(ttlClock, q) {
1336
1750
  if (q.type === "internal") return false;