@segmentstream/ontology 0.0.0-stage → 0.92.1

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 (483) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +2 -0
  3. package/README.md +1655 -2
  4. package/dist/action.d.ts +193 -0
  5. package/dist/action.js +41 -0
  6. package/dist/agent-chat.d.ts +44 -0
  7. package/dist/agent-chat.js +91 -0
  8. package/dist/agent-task-contract.d.ts +64 -0
  9. package/dist/agent-task-contract.js +15 -0
  10. package/dist/agent-task.d.ts +39 -0
  11. package/dist/agent-task.js +112 -0
  12. package/dist/analyze.d.ts +215 -0
  13. package/dist/analyze.js +2438 -0
  14. package/dist/app-feedback.d.ts +167 -0
  15. package/dist/app-feedback.js +176 -0
  16. package/dist/authoring-input.d.ts +3 -0
  17. package/dist/authoring-input.js +107 -0
  18. package/dist/authoring-names.d.ts +4 -0
  19. package/dist/authoring-names.js +13 -0
  20. package/dist/authoring-records.d.ts +35 -0
  21. package/dist/authoring-records.js +264 -0
  22. package/dist/authoring.d.ts +15 -0
  23. package/dist/authoring.js +331 -0
  24. package/dist/bin/check.d.ts +30 -0
  25. package/dist/bin/check.js +99 -0
  26. package/dist/bin/load.d.ts +48 -0
  27. package/dist/bin/load.js +139 -0
  28. package/dist/bin/main.d.ts +31 -0
  29. package/dist/bin/main.js +121 -0
  30. package/dist/bin/migrate.d.ts +57 -0
  31. package/dist/bin/migrate.js +170 -0
  32. package/dist/bin/output.d.ts +31 -0
  33. package/dist/bin/output.js +84 -0
  34. package/dist/bin.d.ts +2 -0
  35. package/dist/bin.js +31 -0
  36. package/dist/blob.d.ts +52 -0
  37. package/dist/blob.js +87 -0
  38. package/dist/channel-session.d.ts +16 -0
  39. package/dist/channel-session.js +16 -0
  40. package/dist/chat.d.ts +43 -0
  41. package/dist/chat.js +34 -0
  42. package/dist/confirmation.d.ts +46 -0
  43. package/dist/confirmation.js +41 -0
  44. package/dist/connection.d.ts +213 -0
  45. package/dist/connection.js +289 -0
  46. package/dist/deployment.d.ts +35 -0
  47. package/dist/deployment.js +76 -0
  48. package/dist/diagnostics.d.ts +27 -0
  49. package/dist/diagnostics.js +106 -0
  50. package/dist/emails.d.ts +70 -0
  51. package/dist/emails.js +221 -0
  52. package/dist/entity.d.ts +331 -0
  53. package/dist/entity.js +153 -0
  54. package/dist/errors.d.ts +154 -0
  55. package/dist/errors.js +109 -0
  56. package/dist/feedback-config.d.ts +24 -0
  57. package/dist/feedback-config.js +126 -0
  58. package/dist/files.d.ts +39 -0
  59. package/dist/files.js +4 -0
  60. package/dist/groups.d.ts +95 -0
  61. package/dist/groups.js +280 -0
  62. package/dist/index.d.ts +109 -0
  63. package/dist/index.js +70 -0
  64. package/dist/internal/diagnostics.js +594 -0
  65. package/dist/legacy.d.ts +160 -0
  66. package/dist/legacy.js +270 -0
  67. package/dist/manifest-compatibility.d.ts +8 -0
  68. package/dist/manifest-compatibility.js +87 -0
  69. package/dist/manifest.d.ts +3329 -0
  70. package/dist/manifest.js +757 -0
  71. package/dist/me.d.ts +46 -0
  72. package/dist/me.js +109 -0
  73. package/dist/metric-filters.d.ts +48 -0
  74. package/dist/metric-filters.js +252 -0
  75. package/dist/metric-routes.d.ts +132 -0
  76. package/dist/metric-routes.js +498 -0
  77. package/dist/metric.d.ts +172 -0
  78. package/dist/metric.js +132 -0
  79. package/dist/migrate/apply.d.ts +94 -0
  80. package/dist/migrate/apply.js +526 -0
  81. package/dist/migrate/errors.d.ts +46 -0
  82. package/dist/migrate/errors.js +38 -0
  83. package/dist/migrate/index.d.ts +10 -0
  84. package/dist/migrate/index.js +7 -0
  85. package/dist/migrate/model.d.ts +2311 -0
  86. package/dist/migrate/model.js +148 -0
  87. package/dist/migrate/plan.d.ts +51 -0
  88. package/dist/migrate/plan.js +1856 -0
  89. package/dist/migrate/preview.d.ts +13 -0
  90. package/dist/migrate/preview.js +65 -0
  91. package/dist/migrate/storage.d.ts +226 -0
  92. package/dist/migrate/storage.js +774 -0
  93. package/dist/model-metrics.d.ts +22 -0
  94. package/dist/model-metrics.js +545 -0
  95. package/dist/names.d.ts +49 -0
  96. package/dist/names.js +110 -0
  97. package/dist/navigation-artifact.d.ts +13 -0
  98. package/dist/navigation-artifact.js +11 -0
  99. package/dist/navigation-catalog.d.ts +55 -0
  100. package/dist/navigation-catalog.js +297 -0
  101. package/dist/nitro-navigation.d.ts +3 -0
  102. package/dist/nitro-navigation.js +64 -0
  103. package/dist/nitro-workflows.d.ts +108 -0
  104. package/dist/nitro-workflows.js +286 -0
  105. package/dist/nitro.d.ts +40 -0
  106. package/dist/nitro.js +50 -0
  107. package/dist/ontology.d.ts +41 -0
  108. package/dist/ontology.js +46 -0
  109. package/dist/permissions.d.ts +198 -0
  110. package/dist/permissions.js +272 -0
  111. package/dist/property.d.ts +188 -0
  112. package/dist/property.js +301 -0
  113. package/dist/query.d.ts +245 -0
  114. package/dist/query.js +615 -0
  115. package/dist/relationship.d.ts +99 -0
  116. package/dist/relationship.js +33 -0
  117. package/dist/render.d.ts +81 -0
  118. package/dist/render.js +512 -0
  119. package/dist/replication/baseline.d.ts +51 -0
  120. package/dist/replication/baseline.js +215 -0
  121. package/dist/replication/bigquery.d.ts +37 -0
  122. package/dist/replication/bigquery.js +211 -0
  123. package/dist/replication/coverage.d.ts +14 -0
  124. package/dist/replication/coverage.js +53 -0
  125. package/dist/replication/evolution.d.ts +25 -0
  126. package/dist/replication/evolution.js +184 -0
  127. package/dist/replication/identities.d.ts +6 -0
  128. package/dist/replication/identities.js +47 -0
  129. package/dist/replication/legacy-baseline.d.ts +27 -0
  130. package/dist/replication/legacy-baseline.js +109 -0
  131. package/dist/replication/legacy-store.d.ts +19 -0
  132. package/dist/replication/legacy-store.js +63 -0
  133. package/dist/replication/lineage.d.ts +36 -0
  134. package/dist/replication/lineage.js +72 -0
  135. package/dist/replication/migrate.d.ts +21 -0
  136. package/dist/replication/migrate.js +84 -0
  137. package/dist/replication/model.d.ts +52 -0
  138. package/dist/replication/model.js +99 -0
  139. package/dist/replication/scans.d.ts +34 -0
  140. package/dist/replication/scans.js +158 -0
  141. package/dist/replication/schema.d.ts +11 -0
  142. package/dist/replication/schema.js +226 -0
  143. package/dist/replication/shadow-schema.d.ts +4 -0
  144. package/dist/replication/shadow-schema.js +61 -0
  145. package/dist/replication/shadow.d.ts +127 -0
  146. package/dist/replication/shadow.js +614 -0
  147. package/dist/replication/source-replacement.d.ts +33 -0
  148. package/dist/replication/source-replacement.js +76 -0
  149. package/dist/replication/store.d.ts +36 -0
  150. package/dist/replication/store.js +382 -0
  151. package/dist/replication/transaction.d.ts +23 -0
  152. package/dist/replication/transaction.js +88 -0
  153. package/dist/role.d.ts +61 -0
  154. package/dist/role.js +76 -0
  155. package/dist/runtime/action-deadlocks.d.ts +21 -0
  156. package/dist/runtime/action-deadlocks.js +130 -0
  157. package/dist/runtime/action-transactions.d.ts +35 -0
  158. package/dist/runtime/action-transactions.js +54 -0
  159. package/dist/runtime/agent-counters.d.ts +26 -0
  160. package/dist/runtime/agent-counters.js +98 -0
  161. package/dist/runtime/agent-tasks.d.ts +63 -0
  162. package/dist/runtime/agent-tasks.js +120 -0
  163. package/dist/runtime/agents.d.ts +15 -0
  164. package/dist/runtime/agents.js +457 -0
  165. package/dist/runtime/batch-diagnostics.d.ts +22 -0
  166. package/dist/runtime/batch-diagnostics.js +89 -0
  167. package/dist/runtime/batch.d.ts +52 -0
  168. package/dist/runtime/batch.js +534 -0
  169. package/dist/runtime/channel-session-revocation.d.ts +3 -0
  170. package/dist/runtime/channel-session-revocation.js +5 -0
  171. package/dist/runtime/channel-sessions.d.ts +4 -0
  172. package/dist/runtime/channel-sessions.js +61 -0
  173. package/dist/runtime/confirmations.d.ts +29 -0
  174. package/dist/runtime/confirmations.js +57 -0
  175. package/dist/runtime/cursor.d.ts +23 -0
  176. package/dist/runtime/cursor.js +100 -0
  177. package/dist/runtime/deployment.d.ts +7 -0
  178. package/dist/runtime/deployment.js +4 -0
  179. package/dist/runtime/errors.d.ts +118 -0
  180. package/dist/runtime/errors.js +109 -0
  181. package/dist/runtime/federation-tables.d.ts +2 -0
  182. package/dist/runtime/federation-tables.js +20 -0
  183. package/dist/runtime/federation.d.ts +42 -0
  184. package/dist/runtime/federation.js +222 -0
  185. package/dist/runtime/files.d.ts +39 -0
  186. package/dist/runtime/files.js +44 -0
  187. package/dist/runtime/generated-actions.d.ts +37 -0
  188. package/dist/runtime/generated-actions.js +307 -0
  189. package/dist/runtime/history.d.ts +91 -0
  190. package/dist/runtime/history.js +77 -0
  191. package/dist/runtime/ids.d.ts +6 -0
  192. package/dist/runtime/ids.js +39 -0
  193. package/dist/runtime/member-tables.d.ts +8 -0
  194. package/dist/runtime/member-tables.js +33 -0
  195. package/dist/runtime/member-writes.d.ts +78 -0
  196. package/dist/runtime/member-writes.js +202 -0
  197. package/dist/runtime/metric-query.d.ts +19 -0
  198. package/dist/runtime/metric-query.js +411 -0
  199. package/dist/runtime/model.d.ts +67 -0
  200. package/dist/runtime/model.js +150 -0
  201. package/dist/runtime/new-user-hook.d.ts +6 -0
  202. package/dist/runtime/new-user-hook.js +37 -0
  203. package/dist/runtime/oauth.d.ts +9 -0
  204. package/dist/runtime/oauth.js +299 -0
  205. package/dist/runtime/pipeline.d.ts +155 -0
  206. package/dist/runtime/pipeline.js +597 -0
  207. package/dist/runtime/postgres-tls.d.ts +10 -0
  208. package/dist/runtime/postgres-tls.js +14 -0
  209. package/dist/runtime/principals.d.ts +302 -0
  210. package/dist/runtime/principals.js +1307 -0
  211. package/dist/runtime/reads.d.ts +55 -0
  212. package/dist/runtime/reads.js +313 -0
  213. package/dist/runtime/records.d.ts +42 -0
  214. package/dist/runtime/records.js +245 -0
  215. package/dist/runtime/resolve-agent-identity.d.ts +6 -0
  216. package/dist/runtime/resolve-agent-identity.js +181 -0
  217. package/dist/runtime/service-binding.d.ts +18 -0
  218. package/dist/runtime/service-binding.js +266 -0
  219. package/dist/runtime/sign-in-links.d.ts +68 -0
  220. package/dist/runtime/sign-in-links.js +147 -0
  221. package/dist/runtime/sql-runtime.d.ts +131 -0
  222. package/dist/runtime/sql-runtime.js +362 -0
  223. package/dist/runtime/statement.d.ts +18 -0
  224. package/dist/runtime/statement.js +31 -0
  225. package/dist/runtime/user-identities.d.ts +66 -0
  226. package/dist/runtime/user-identities.js +106 -0
  227. package/dist/runtime/user-identity-sql.d.ts +86 -0
  228. package/dist/runtime/user-identity-sql.js +297 -0
  229. package/dist/runtime/user-roles.d.ts +16 -0
  230. package/dist/runtime/user-roles.js +32 -0
  231. package/dist/runtime/validation.d.ts +16 -0
  232. package/dist/runtime/validation.js +26 -0
  233. package/dist/runtime/where.d.ts +57 -0
  234. package/dist/runtime/where.js +404 -0
  235. package/dist/runtime/workflow-admission.d.ts +49 -0
  236. package/dist/runtime/workflow-admission.js +92 -0
  237. package/dist/runtime/workflow-argument-codec.d.ts +10 -0
  238. package/dist/runtime/workflow-argument-codec.js +120 -0
  239. package/dist/runtime/workflow-arguments.d.ts +17 -0
  240. package/dist/runtime/workflow-arguments.js +99 -0
  241. package/dist/runtime/workflow-starts.d.ts +203 -0
  242. package/dist/runtime/workflow-starts.js +493 -0
  243. package/dist/runtime/workflow-users.d.ts +9 -0
  244. package/dist/runtime/workflow-users.js +17 -0
  245. package/dist/runtime/workload.d.ts +25 -0
  246. package/dist/runtime/workload.js +24 -0
  247. package/dist/runtime/writes.d.ts +108 -0
  248. package/dist/runtime/writes.js +491 -0
  249. package/dist/runtime-source.d.ts +22 -0
  250. package/dist/runtime-source.js +28 -0
  251. package/dist/runtime.d.ts +310 -0
  252. package/dist/runtime.js +31 -0
  253. package/dist/server.d.ts +23 -0
  254. package/dist/server.js +107 -0
  255. package/dist/service/agent/attachments.d.ts +33 -0
  256. package/dist/service/agent/attachments.js +228 -0
  257. package/dist/service/agent/authentication.d.ts +22 -0
  258. package/dist/service/agent/authentication.js +58 -0
  259. package/dist/service/agent/confirmations.d.ts +5 -0
  260. package/dist/service/agent/confirmations.js +73 -0
  261. package/dist/service/agent/conversation-routes.d.ts +14 -0
  262. package/dist/service/agent/conversation-routes.js +45 -0
  263. package/dist/service/agent/conversations.d.ts +40 -0
  264. package/dist/service/agent/conversations.js +69 -0
  265. package/dist/service/agent/files.d.ts +12 -0
  266. package/dist/service/agent/files.js +69 -0
  267. package/dist/service/agent/protocol.d.ts +90 -0
  268. package/dist/service/agent/protocol.js +114 -0
  269. package/dist/service/agent/server.d.ts +28 -0
  270. package/dist/service/agent/server.js +374 -0
  271. package/dist/service/agent/task-caller.d.ts +21 -0
  272. package/dist/service/agent/task-caller.js +35 -0
  273. package/dist/service/agent/task-start.d.ts +53 -0
  274. package/dist/service/agent/task-start.js +161 -0
  275. package/dist/service/agent/tasks.d.ts +40 -0
  276. package/dist/service/agent/tasks.js +155 -0
  277. package/dist/service/api.d.ts +50 -0
  278. package/dist/service/api.js +288 -0
  279. package/dist/service/authentication.d.ts +227 -0
  280. package/dist/service/authentication.js +767 -0
  281. package/dist/service/caller-context.d.ts +24 -0
  282. package/dist/service/caller-context.js +6 -0
  283. package/dist/service/catalog.d.ts +42 -0
  284. package/dist/service/catalog.js +66 -0
  285. package/dist/service/channel/provider.d.ts +10 -0
  286. package/dist/service/channel/provider.js +1 -0
  287. package/dist/service/channel/providers/telegram.d.ts +18 -0
  288. package/dist/service/channel/providers/telegram.js +108 -0
  289. package/dist/service/channel/sessions.d.ts +5 -0
  290. package/dist/service/channel/sessions.js +53 -0
  291. package/dist/service/channel/sign-in.d.ts +7 -0
  292. package/dist/service/channel/sign-in.js +78 -0
  293. package/dist/service/chat/archive.d.ts +64 -0
  294. package/dist/service/chat/archive.js +62 -0
  295. package/dist/service/chat/attribution.d.ts +26 -0
  296. package/dist/service/chat/attribution.js +28 -0
  297. package/dist/service/chat/delivery.d.ts +18 -0
  298. package/dist/service/chat/delivery.js +251 -0
  299. package/dist/service/chat/diagnostics.d.ts +2 -0
  300. package/dist/service/chat/diagnostics.js +35 -0
  301. package/dist/service/chat/envelope.d.ts +96 -0
  302. package/dist/service/chat/envelope.js +39 -0
  303. package/dist/service/chat/idempotency.d.ts +19 -0
  304. package/dist/service/chat/idempotency.js +37 -0
  305. package/dist/service/chat/providers/telegram.d.ts +36 -0
  306. package/dist/service/chat/providers/telegram.js +133 -0
  307. package/dist/service/chat/reads.d.ts +25 -0
  308. package/dist/service/chat/reads.js +75 -0
  309. package/dist/service/chat/resolve.d.ts +34 -0
  310. package/dist/service/chat/resolve.js +150 -0
  311. package/dist/service/chat/routes.d.ts +23 -0
  312. package/dist/service/chat/routes.js +75 -0
  313. package/dist/service/chat/schemas.d.ts +43 -0
  314. package/dist/service/chat/schemas.js +31 -0
  315. package/dist/service/config.d.ts +218 -0
  316. package/dist/service/config.js +265 -0
  317. package/dist/service/connection-stream.d.ts +10 -0
  318. package/dist/service/connection-stream.js +191 -0
  319. package/dist/service/connections.d.ts +60 -0
  320. package/dist/service/connections.js +271 -0
  321. package/dist/service/decisions.d.ts +35 -0
  322. package/dist/service/decisions.js +63 -0
  323. package/dist/service/descriptions.d.ts +8 -0
  324. package/dist/service/descriptions.js +20 -0
  325. package/dist/service/discovery-schemas.d.ts +92 -0
  326. package/dist/service/discovery-schemas.js +59 -0
  327. package/dist/service/discovery-search.d.ts +30 -0
  328. package/dist/service/discovery-search.js +95 -0
  329. package/dist/service/discovery.d.ts +37 -0
  330. package/dist/service/discovery.js +436 -0
  331. package/dist/service/docs.d.ts +9 -0
  332. package/dist/service/docs.js +197 -0
  333. package/dist/service/errors.d.ts +145 -0
  334. package/dist/service/errors.js +704 -0
  335. package/dist/service/federation-provider.d.ts +36 -0
  336. package/dist/service/federation-provider.js +110 -0
  337. package/dist/service/federation-server.d.ts +20 -0
  338. package/dist/service/federation-server.js +249 -0
  339. package/dist/service/federation-status.d.ts +40 -0
  340. package/dist/service/federation-status.js +151 -0
  341. package/dist/service/handlers.d.ts +18 -0
  342. package/dist/service/handlers.js +287 -0
  343. package/dist/service/mcp/confirmation.d.ts +68 -0
  344. package/dist/service/mcp/confirmation.js +178 -0
  345. package/dist/service/mcp/find-input.d.ts +12 -0
  346. package/dist/service/mcp/find-input.js +122 -0
  347. package/dist/service/mcp/input-schema.d.ts +6 -0
  348. package/dist/service/mcp/input-schema.js +126 -0
  349. package/dist/service/mcp/listing.d.ts +26 -0
  350. package/dist/service/mcp/listing.js +92 -0
  351. package/dist/service/mcp/metric-tools.d.ts +85 -0
  352. package/dist/service/mcp/metric-tools.js +258 -0
  353. package/dist/service/mcp/request-log.d.ts +5 -0
  354. package/dist/service/mcp/request-log.js +53 -0
  355. package/dist/service/mcp/runtime-policy.d.ts +27 -0
  356. package/dist/service/mcp/runtime-policy.js +62 -0
  357. package/dist/service/mcp/server.d.ts +52 -0
  358. package/dist/service/mcp/server.js +861 -0
  359. package/dist/service/mcp/tools.d.ts +99 -0
  360. package/dist/service/mcp/tools.js +938 -0
  361. package/dist/service/member-file-routes.d.ts +22 -0
  362. package/dist/service/member-file-routes.js +52 -0
  363. package/dist/service/member-files.d.ts +245 -0
  364. package/dist/service/member-files.js +258 -0
  365. package/dist/service/metric-query-platform.d.ts +77 -0
  366. package/dist/service/metric-query-platform.js +198 -0
  367. package/dist/service/metric-query-scopes.d.ts +43 -0
  368. package/dist/service/metric-query-scopes.js +147 -0
  369. package/dist/service/metric-query.d.ts +48 -0
  370. package/dist/service/metric-query.js +200 -0
  371. package/dist/service/name-suggestions.d.ts +3 -0
  372. package/dist/service/name-suggestions.js +38 -0
  373. package/dist/service/navigation-schemas.d.ts +80 -0
  374. package/dist/service/navigation-schemas.js +49 -0
  375. package/dist/service/navigation.d.ts +20 -0
  376. package/dist/service/navigation.js +165 -0
  377. package/dist/service/oauth/protocol.d.ts +70 -0
  378. package/dist/service/oauth/protocol.js +157 -0
  379. package/dist/service/oauth/refresh-replay.d.ts +2 -0
  380. package/dist/service/oauth/refresh-replay.js +20 -0
  381. package/dist/service/oauth/server.d.ts +28 -0
  382. package/dist/service/oauth/server.js +769 -0
  383. package/dist/service/operations.d.ts +170 -0
  384. package/dist/service/operations.js +392 -0
  385. package/dist/service/plan.d.ts +57 -0
  386. package/dist/service/plan.js +96 -0
  387. package/dist/service/rate-limit.d.ts +22 -0
  388. package/dist/service/rate-limit.js +52 -0
  389. package/dist/service/record-reads.d.ts +21 -0
  390. package/dist/service/record-reads.js +155 -0
  391. package/dist/service/replication.d.ts +5 -0
  392. package/dist/service/replication.js +39 -0
  393. package/dist/service/runtime-contract.d.ts +508 -0
  394. package/dist/service/runtime-contract.js +18 -0
  395. package/dist/service/runtime-identity.d.ts +71 -0
  396. package/dist/service/runtime-identity.js +122 -0
  397. package/dist/service/schemas.d.ts +961 -0
  398. package/dist/service/schemas.js +639 -0
  399. package/dist/service/service.d.ts +21 -0
  400. package/dist/service/service.js +526 -0
  401. package/dist/service/warehouse-platform.d.ts +17 -0
  402. package/dist/service/warehouse-platform.js +122 -0
  403. package/dist/service/warehouse-query.d.ts +24 -0
  404. package/dist/service/warehouse-query.js +280 -0
  405. package/dist/service/webhooks.d.ts +106 -0
  406. package/dist/service/webhooks.js +233 -0
  407. package/dist/service/workflow-admission-provider.d.ts +31 -0
  408. package/dist/service/workflow-admission-provider.js +205 -0
  409. package/dist/service/workflow-argument-serialization.d.ts +2 -0
  410. package/dist/service/workflow-argument-serialization.js +14 -0
  411. package/dist/service/workflow-callback.d.ts +22 -0
  412. package/dist/service/workflow-callback.js +75 -0
  413. package/dist/service/workflow-deployment.d.ts +8 -0
  414. package/dist/service/workflow-deployment.js +131 -0
  415. package/dist/service/workflow-diagnostics.d.ts +23 -0
  416. package/dist/service/workflow-diagnostics.js +263 -0
  417. package/dist/service/workflow-errors.d.ts +26 -0
  418. package/dist/service/workflow-errors.js +181 -0
  419. package/dist/service/workflow-logging.d.ts +2 -0
  420. package/dist/service/workflow-logging.js +50 -0
  421. package/dist/service/workflow-queue-fence.d.ts +6 -0
  422. package/dist/service/workflow-queue-fence.js +66 -0
  423. package/dist/service/workflows.d.ts +200 -0
  424. package/dist/service/workflows.js +1549 -0
  425. package/dist/sign-in.d.ts +58 -0
  426. package/dist/sign-in.js +57 -0
  427. package/dist/source.d.ts +30 -0
  428. package/dist/source.js +107 -0
  429. package/dist/start-recovery-contract.d.ts +49 -0
  430. package/dist/start-recovery-contract.js +26 -0
  431. package/dist/start-recovery.d.ts +27 -0
  432. package/dist/start-recovery.js +81 -0
  433. package/dist/storage/api.d.ts +4 -0
  434. package/dist/storage/api.js +40 -0
  435. package/dist/storage/attachments.d.ts +146 -0
  436. package/dist/storage/attachments.js +733 -0
  437. package/dist/storage/authorization.d.ts +38 -0
  438. package/dist/storage/authorization.js +83 -0
  439. package/dist/storage/file-access.d.ts +17 -0
  440. package/dist/storage/file-access.js +8 -0
  441. package/dist/storage/limits.d.ts +12 -0
  442. package/dist/storage/limits.js +17 -0
  443. package/dist/storage/maintenance-auth.d.ts +20 -0
  444. package/dist/storage/maintenance-auth.js +24 -0
  445. package/dist/storage/maintenance.d.ts +11 -0
  446. package/dist/storage/maintenance.js +67 -0
  447. package/dist/storage/neon.d.ts +4 -0
  448. package/dist/storage/neon.js +318 -0
  449. package/dist/storage/operator.d.ts +11 -0
  450. package/dist/storage/operator.js +30 -0
  451. package/dist/storage/provider.d.ts +93 -0
  452. package/dist/storage/provider.js +36 -0
  453. package/dist/storage/sessions.d.ts +184 -0
  454. package/dist/storage/sessions.js +688 -0
  455. package/dist/storage/tables.d.ts +54 -0
  456. package/dist/storage/tables.js +205 -0
  457. package/dist/storage/transfers.d.ts +16 -0
  458. package/dist/storage/transfers.js +153 -0
  459. package/dist/storage/writes.d.ts +7 -0
  460. package/dist/storage/writes.js +88 -0
  461. package/dist/telegram.d.ts +35 -0
  462. package/dist/telegram.js +173 -0
  463. package/dist/user-identity.d.ts +28 -0
  464. package/dist/user-identity.js +106 -0
  465. package/dist/user.d.ts +75 -0
  466. package/dist/user.js +111 -0
  467. package/dist/warehouse-backed.d.ts +6 -0
  468. package/dist/warehouse-backed.js +495 -0
  469. package/dist/webhook-token.d.ts +11 -0
  470. package/dist/webhook-token.js +18 -0
  471. package/dist/webhooks.d.ts +40 -0
  472. package/dist/webhooks.js +55 -0
  473. package/dist/wire.d.ts +10 -0
  474. package/dist/wire.js +35 -0
  475. package/dist/workflow-runtime.d.ts +116 -0
  476. package/dist/workflow-runtime.js +109 -0
  477. package/dist/workflow-topics.d.ts +28 -0
  478. package/dist/workflow-topics.js +28 -0
  479. package/dist/workflow.d.ts +284 -0
  480. package/dist/workflow.js +259 -0
  481. package/dist/workflows.d.ts +24 -0
  482. package/dist/workflows.js +18 -0
  483. package/package.json +127 -4
@@ -0,0 +1,76 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { Effect } from "effect";
3
+ import { ReplicationBaselineFailed } from "./baseline.js";
4
+ import { lockReplicationLifecycle } from "./transaction.js";
5
+ export const pendingSourceReplacement = (sql) => sql
6
+ .unsafe(`SELECT source_id::text,operation_id::text,expected,proof,
7
+ COALESCE(proof->>'published',attempts->0->'expected'->>'epoch') AS published
8
+ FROM _ontology_replication_source_replacements
9
+ WHERE state='pending' AND source_id=(SELECT source_id FROM _ontology_replication WHERE id)`)
10
+ .pipe(Effect.map((rows) => rows[0]));
11
+ /** Only the trusted product publication can justify replacing an otherwise healthy old source. */
12
+ export const prepareSourceReplacement = (sql, input) => sql.withTransaction(Effect.gen(function* () {
13
+ yield* lockReplicationLifecycle(sql);
14
+ const pending = yield* pendingSourceReplacement(sql);
15
+ if (pending) {
16
+ return pending;
17
+ }
18
+ const [state] = yield* sql.unsafe(`SELECT EXISTS(SELECT 1 FROM _ontology_replication r
19
+ WHERE r.id AND r.enabled AND r.source_id=$1::uuid
20
+ AND NOT EXISTS(SELECT 1 FROM _ontology_replication_targets WHERE generation=$2::uuid AND source_id=r.source_id)
21
+ AND NOT EXISTS(SELECT 1 FROM _ontology_replication_epochs
22
+ WHERE source_id=r.source_id AND (state='serving' OR installed))
23
+ AND NOT EXISTS(SELECT 1 FROM _ontology_replication_source_replacements WHERE source_id=r.source_id)) AS valid`, [input.sourceId, input.published]);
24
+ if (!state?.valid ||
25
+ input.observed.sourceId === input.sourceId ||
26
+ input.proof.published !== input.published ||
27
+ input.proof.sourceId !== input.observed.sourceId ||
28
+ input.proof.selectorIncarnation !== input.observed.incarnation ||
29
+ input.proof.workspaceId !== input.workspaceId ||
30
+ input.proof.projectId !== input.projectId) {
31
+ return yield* new ReplicationBaselineFailed({
32
+ message: "replication_source_replacement_not_proven",
33
+ });
34
+ }
35
+ const operation = randomUUID();
36
+ yield* sql.unsafe(`INSERT INTO _ontology_replication_source_replacements
37
+ (source_id,operation_id,expected,state,attempts,proof) VALUES($1::uuid,$2::uuid,$3::jsonb,'pending',
38
+ jsonb_build_array(jsonb_build_object('operation_id',$2::text,'expected',$3::jsonb)),$4::jsonb)`, [input.sourceId, operation, JSON.stringify(input.observed), JSON.stringify(input.proof)]);
39
+ yield* sql.unsafe(`UPDATE _ontology_replication SET destination_rebuilding=true,destination_repair_pending=true WHERE id`);
40
+ return {
41
+ source_id: input.sourceId,
42
+ operation_id: operation,
43
+ expected: input.observed,
44
+ proof: input.proof,
45
+ published: input.published,
46
+ };
47
+ }));
48
+ /** A confirmed no-op CAS may race an old-source handoff; retain its receipt before a new attempt. */
49
+ export const retrySourceReplacement = (sql, intent, observed) => sql.withTransaction(Effect.gen(function* () {
50
+ yield* lockReplicationLifecycle(sql);
51
+ if (observed.sourceId !== intent.expected.sourceId ||
52
+ observed.incarnation !== intent.expected.incarnation) {
53
+ return yield* new ReplicationBaselineFailed({
54
+ message: "replication_source_transition_changed",
55
+ });
56
+ }
57
+ const operation = randomUUID();
58
+ yield* sql.unsafe(`UPDATE _ontology_replication_source_replacements SET expected=$3::jsonb,
59
+ operation_id=$4::uuid,attempts=attempts || jsonb_build_array(jsonb_build_object(
60
+ 'operation_id',$4::text,'expected',$3::jsonb))
61
+ WHERE source_id=$1::uuid AND operation_id=$2::uuid AND state='pending'`, [intent.source_id, intent.operation_id, JSON.stringify(observed), operation]);
62
+ }));
63
+ /** Upgrade only an already admitted old journal; the provider evidence is read before this transaction. */
64
+ export const recordSourcePublicationProof = (sql, intent, proof) => sql.withTransaction(Effect.gen(function* () {
65
+ yield* lockReplicationLifecycle(sql);
66
+ if (proof.published !== intent.published ||
67
+ proof.sourceId !== intent.expected.sourceId ||
68
+ proof.selectorIncarnation !== intent.expected.incarnation) {
69
+ return yield* new ReplicationBaselineFailed({
70
+ message: "replication_source_replacement_not_proven",
71
+ });
72
+ }
73
+ yield* sql.unsafe(`UPDATE _ontology_replication_source_replacements SET proof=$3::jsonb
74
+ WHERE source_id=$1::uuid AND operation_id=$2::uuid AND state='pending' AND proof IS NULL
75
+ AND source_id=(SELECT source_id FROM _ontology_replication WHERE id AND enabled)`, [intent.source_id, intent.operation_id, JSON.stringify(proof)]);
76
+ }));
@@ -0,0 +1,36 @@
1
+ import { Effect } from "effect";
2
+ import type { SqlClient } from "effect/unstable/sql";
3
+ import { type ReplicationEvent, type ReplicationSelection, type ReplicationExclusion } from "./model.js";
4
+ import { advanceReplicationBaseline, ReplicationBaselineFailed, type ReplicationTarget } from "./baseline.js";
5
+ export type { ReplicationTarget } from "./baseline.js";
6
+ export declare const startReplication: (sql: SqlClient.SqlClient, input: {
7
+ readonly generation: string;
8
+ readonly exclusions?: ReadonlyArray<ReplicationExclusion>;
9
+ readonly selection?: ReadonlyArray<ReplicationSelection>;
10
+ readonly newSource?: boolean;
11
+ }) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError | ReplicationBaselineFailed | import("../index.js").CatalogInvalid | import("./legacy-baseline.js").ReplicationBaselineFailed, never>;
12
+ export declare const activeReplicationTarget: (sql: SqlClient.SqlClient) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError, never>;
13
+ export declare const pauseReplication: (sql: SqlClient.SqlClient) => Effect.Effect<void, import("effect/unstable/sql/SqlError").SqlError, never>;
14
+ export declare const replicationTarget: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError, never>;
15
+ export { advanceReplicationBaseline };
16
+ export { replicationInitiallyReady as legacyReplicationInitiallyReady } from "./legacy-store.js";
17
+ /** Retained history is recovery storage; frequent delivery reads only indexed unaccepted references. */
18
+ export declare const pendingReplicationEvents: (sql: SqlClient.SqlClient, target: ReplicationTarget, options?: {
19
+ readonly bulk?: boolean;
20
+ readonly through?: string;
21
+ }) => Effect.Effect<{
22
+ readonly historyId: string;
23
+ readonly event: ReplicationEvent;
24
+ }[], import("effect/unstable/sql/SqlError").SqlError, never>;
25
+ /** Acceptance comes only from Pub/Sub receipts; historical verification columns are not used. */
26
+ export declare const replicationStatus: (sql: SqlClient.SqlClient, target: ReplicationTarget) => Effect.Effect<{
27
+ total: string;
28
+ accepted: string;
29
+ }, import("effect/unstable/sql/SqlError").SqlError, never>;
30
+ /** Initialization is a fixed fence, so continuing writes cannot move the readiness goal. */
31
+ export declare const replicationInitiallyReady: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<boolean, import("effect/unstable/sql/SqlError").SqlError, never>;
32
+ export declare const advanceReplicationReplay: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<boolean, import("effect/unstable/sql/SqlError").SqlError, never>;
33
+ /** Static pre-upgrade histories have no new inserts: v4 writers atomically enqueue new history. */
34
+ export declare const advanceReplicationBackfill: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<boolean, import("effect/unstable/sql/SqlError").SqlError | import("../index.js").CatalogInvalid, never>;
35
+ export declare const acknowledgeReplication: (sql: SqlClient.SqlClient, generation: string, ids: ReadonlyArray<string>, version?: number) => Effect.Effect<void, import("effect/unstable/sql/SqlError").SqlError, never>;
36
+ export declare const replayReplication: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<void, import("effect/unstable/sql/SqlError").SqlError, never>;
@@ -0,0 +1,382 @@
1
+ import { refreshShadowCapturePolicy, shadowWireEvent } from "./shadow.js";
2
+ import { randomUUID } from "node:crypto";
3
+ import { Effect } from "effect";
4
+ import * as legacy from "./legacy-store.js";
5
+ import { createReplicationBaseline as createLegacyBaseline } from "./legacy-baseline.js";
6
+ import { lockReplicationLifecycle, shareReplicationLifecycle } from "./transaction.js";
7
+ import { canonicalJson } from "../manifest.js";
8
+ import { readCatalog } from "../migrate/apply.js";
9
+ import { historyTableName, quoteIdentifier as qi } from "../migrate/storage.js";
10
+ import { REPLICATION_BATCH_BYTES, REPLICATION_BATCH_SIZE, publishMessage, replicationEventId, resolveReplicationSelection, } from "./model.js";
11
+ import { createReplicationBaseline, advanceReplicationBaseline, ReplicationBaselineFailed, } from "./baseline.js";
12
+ import { effectiveReplicationExclusions, migratedReplicationFills } from "./migrate.js";
13
+ import { refreshReplicationProjection } from "./evolution.js";
14
+ import { REPLICATION_TARGET_FILLS, REPLICATION_TARGET_METADATA } from "./schema.js";
15
+ const replicationPolicyKey = (policy) => JSON.stringify(policy
16
+ .map((item) => ({
17
+ entityId: item.entityId,
18
+ disabled: item.disabled ?? false,
19
+ fields: [...new Set(item.fields ?? [])].sort(),
20
+ relationships: [...new Set(item.relationships ?? [])].sort(),
21
+ }))
22
+ .sort((a, b) => a.entityId.localeCompare(b.entityId)));
23
+ export const startReplication = (sql, input) => Effect.gen(function* () {
24
+ const manifest = yield* readCatalog({ sql });
25
+ if (!manifest) {
26
+ return yield* new ReplicationBaselineFailed({
27
+ message: "The ontology has not been migrated.",
28
+ });
29
+ }
30
+ const version = yield* protocol(sql);
31
+ const [state] = yield* sql.unsafe("SELECT * FROM _ontology_replication WHERE id");
32
+ const requested = yield* replicationTarget(sql, input.generation);
33
+ const previous = version >= 6 && requested ? ((yield* activeReplicationTarget(sql)) ?? requested) : requested;
34
+ const policy = input.exclusions ?? state?.exclusions ?? [];
35
+ const persisted = replicationPolicyKey(policy) === replicationPolicyKey(state?.exclusions ?? []);
36
+ const paused = version >= 6 && state?.enabled === false;
37
+ if (previous &&
38
+ !paused &&
39
+ persisted &&
40
+ previous.manifest.hash === manifest.hash &&
41
+ input.selection === undefined) {
42
+ return previous;
43
+ }
44
+ const selection = input.selection ??
45
+ (yield* Effect.try({
46
+ try: () => resolveReplicationSelection(manifest, persisted ? effectiveReplicationExclusions(manifest, policy) : policy),
47
+ catch: (cause) => new ReplicationBaselineFailed({ message: "Invalid replication exclusions.", cause }),
48
+ }));
49
+ if (previous &&
50
+ (paused ||
51
+ !persisted ||
52
+ previous.manifest.hash !== manifest.hash ||
53
+ canonicalJson(previous.selection) !== canonicalJson(selection))) {
54
+ if (version >= 5) {
55
+ return yield* sql.withTransaction(Effect.gen(function* () {
56
+ yield* lockReplicationLifecycle(sql);
57
+ const [capture] = yield* sql.unsafe("SELECT enabled FROM _ontology_replication WHERE id");
58
+ const current = version >= 6
59
+ ? (yield* activeReplicationTarget(sql))
60
+ : (yield* replicationTarget(sql, input.generation));
61
+ const catalog = yield* readCatalog({ sql });
62
+ if (catalog?.hash !== manifest.hash) {
63
+ return yield* new ReplicationBaselineFailed({
64
+ message: "The ontology changed while configuring replication; retry the current projection.",
65
+ });
66
+ }
67
+ const fills = yield* migratedReplicationFills(sql, current, manifest, selection);
68
+ yield* refreshReplicationProjection(sql, current, { ...current, manifest, selection, fills }, undefined, { resuming: version >= 6 && capture?.enabled === false });
69
+ yield* sql.unsafe("UPDATE _ontology_replication SET selection=$1::jsonb,exclusions=$2::jsonb,enabled=true WHERE id", [
70
+ JSON.stringify(Object.fromEntries(selection.map((item) => [item.entityId, item]))),
71
+ JSON.stringify(policy),
72
+ ]);
73
+ if (version >= 6) {
74
+ yield* sql.unsafe(`UPDATE _ontology_replication_epochs SET policy_installed=false
75
+ WHERE id=(SELECT active_generation FROM _ontology_replication WHERE id)`);
76
+ yield* refreshShadowCapturePolicy(sql);
77
+ return (yield* activeReplicationTarget(sql));
78
+ }
79
+ return (yield* replicationTarget(sql, input.generation));
80
+ }));
81
+ }
82
+ // Older source schemas cannot hold scoped scan cursors. An automatic generation copy is
83
+ // @legacy ontology.replication-older-source-protocols
84
+ // the compatibility path until the next ordinary ontology migration installs schema 5.
85
+ return yield* version >= 4
86
+ ? createReplicationBaseline(sql, {
87
+ ...input,
88
+ manifest,
89
+ selection,
90
+ generation: randomUUID(),
91
+ newSource: true,
92
+ })
93
+ : createLegacyBaseline(sql, {
94
+ ...input,
95
+ manifest,
96
+ selection,
97
+ generation: randomUUID(),
98
+ newSource: true,
99
+ });
100
+ }
101
+ return yield* version >= 4
102
+ ? createReplicationBaseline(sql, { ...input, manifest, selection })
103
+ : createLegacyBaseline(sql, { ...input, manifest, selection }).pipe(Effect.map((target) => target));
104
+ }).pipe(Effect.map((target) => target));
105
+ const targetFields = `generation::text, source_id::text, from_revision::text, manifest, selection,
106
+ protocol, baseline_complete, backfill_complete, initial_revision::text, replay_complete,
107
+ ${REPLICATION_TARGET_FILLS}, ${REPLICATION_TARGET_METADATA}, 4 AS delivery_version`;
108
+ const protocol = (sql) => sql
109
+ .unsafe("SELECT version FROM _ontology_replication WHERE id")
110
+ .pipe(Effect.map((rows) => rows[0]?.version ?? 3));
111
+ export const activeReplicationTarget = (sql) => Effect.gen(function* () {
112
+ const version = yield* protocol(sql);
113
+ const fields = version >= 4
114
+ ? targetFields
115
+ : "generation::text, source_id::text, from_revision::text, manifest, selection";
116
+ const rows = yield* sql.unsafe(`SELECT ${fields} FROM _ontology_replication_targets
117
+ WHERE generation = (SELECT active_generation FROM _ontology_replication WHERE id)`);
118
+ return rows[0];
119
+ });
120
+ export const pauseReplication = (sql) => sql.withTransaction(Effect.gen(function* () {
121
+ yield* lockReplicationLifecycle(sql);
122
+ yield* sql.unsafe("UPDATE _ontology_replication SET enabled = false WHERE id");
123
+ }));
124
+ export const replicationTarget = (sql, generation) => Effect.gen(function* () {
125
+ const version = yield* protocol(sql);
126
+ const fields = version >= 4
127
+ ? targetFields
128
+ : "generation::text, source_id::text, from_revision::text, manifest, selection";
129
+ const rows = yield* sql.unsafe(`SELECT ${fields} FROM _ontology_replication_targets WHERE generation = $1::uuid`, [generation]);
130
+ return rows[0];
131
+ });
132
+ export { advanceReplicationBaseline };
133
+ export { replicationInitiallyReady as legacyReplicationInitiallyReady } from "./legacy-store.js";
134
+ /** Retained history is recovery storage; frequent delivery reads only indexed unaccepted references. */
135
+ export const pendingReplicationEvents = (sql, target, options = {}) => Effect.gen(function* () {
136
+ if (target.delivery_version !== 4 &&
137
+ target.protocol !== 4) {
138
+ return yield* legacy.pendingReplicationEvents(sql, target);
139
+ }
140
+ const shadow = (target.schema_version ?? 0) >= 6;
141
+ const references = yield* sql.unsafe(shadow
142
+ ? `WITH event_refs AS MATERIALIZED (
143
+ SELECT event_id::text AS id, history_table, payload, revision, payload_bytes
144
+ FROM _ontology_replication_delivery d
145
+ WHERE generation = $1::uuid
146
+ AND suppressed_at IS NULL
147
+ AND (CASE WHEN $2::boolean THEN committed_at IS NULL AND segment_id IS NULL ELSE accepted_at IS NULL END)
148
+ AND ($3::bigint IS NULL OR revision <= $3::bigint)
149
+ AND ($2::boolean OR NOT EXISTS (SELECT 1 FROM _ontology_replication_epochs e
150
+ WHERE e.state='candidate' AND e.token IS NOT NULL AND d.revision>e.boundary))
151
+ ORDER BY revision, event_id LIMIT ${options.bulk ? 5000 : REPLICATION_BATCH_SIZE}
152
+ ), bounded AS (
153
+ SELECT *,sum(payload_bytes) OVER(ORDER BY revision,id) AS bytes FROM event_refs
154
+ ) SELECT id,history_table,payload FROM bounded
155
+ WHERE bytes<=${options.bulk ? 32_000_000 : REPLICATION_BATCH_BYTES}
156
+ ORDER BY revision,id`
157
+ : `SELECT event_id::text AS id,history_table,to_jsonb(d)->'payload' AS payload
158
+ FROM _ontology_replication_delivery d WHERE generation=$1::uuid AND accepted_at IS NULL
159
+ ORDER BY revision,event_id LIMIT ${REPLICATION_BATCH_SIZE}`, shadow
160
+ ? [target.generation, options.bulk ?? false, options.through ?? null]
161
+ : [target.generation]);
162
+ const groups = new Map();
163
+ for (const reference of references) {
164
+ if (reference.payload) {
165
+ continue;
166
+ }
167
+ const ids = groups.get(reference.history_table) ?? [];
168
+ ids.push(reference.id);
169
+ groups.set(reference.history_table, ids);
170
+ }
171
+ const events = new Map(references
172
+ .filter((reference) => reference.payload !== null)
173
+ .map((reference) => [reference.id, reference.payload]));
174
+ for (const [table, ids] of groups) {
175
+ const rows = yield* sql.unsafe(table === null
176
+ ? `SELECT id::text, event FROM _ontology_replication_baselines
177
+ WHERE generation = $1::uuid AND id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))`
178
+ : `SELECT id::text, replication AS event FROM ${qi(table)}
179
+ WHERE $1::uuid IS NOT NULL AND id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))`, [target.generation, JSON.stringify(ids)]);
180
+ for (const row of rows) {
181
+ events.set(row.id, row.event);
182
+ }
183
+ }
184
+ const [wire] = shadow
185
+ ? yield* sql.unsafe("SELECT capture_bindings,capture_versions FROM _ontology_replication_targets WHERE generation=$1::uuid", [target.generation])
186
+ : [];
187
+ const batchBytes = options.bulk ? 32_000_000 : REPLICATION_BATCH_BYTES;
188
+ const [policy] = shadow
189
+ ? yield* sql.unsafe("SELECT exclusions FROM _ontology_replication WHERE id")
190
+ : [];
191
+ const suppressed = [];
192
+ let size = 32;
193
+ const result = [];
194
+ for (const reference of references) {
195
+ const payload = events.get(reference.id);
196
+ if (!payload) {
197
+ return yield* Effect.die(new Error("A queued replication event is missing from retained history."));
198
+ }
199
+ let event = {
200
+ ...payload,
201
+ generation: target.generation,
202
+ event_id: replicationEventId(target.source_id, reference.id, target.generation),
203
+ };
204
+ if (wire?.capture_bindings) {
205
+ const bindings = wire.capture_versions?.find((version) => version.through === undefined || BigInt(event.revision) <= BigInt(version.through))?.bindings ?? wire.capture_bindings;
206
+ event = shadowWireEvent(event, bindings, target.selection);
207
+ }
208
+ const restriction = policy?.exclusions.find((item) => item.entityId === event.entity_id);
209
+ if (restriction?.disabled ||
210
+ (event.record_kind.startsWith("edge:") &&
211
+ restriction?.relationships?.includes(event.record_kind.slice(5)))) {
212
+ suppressed.push(reference.id);
213
+ continue;
214
+ }
215
+ if (restriction && event.record_kind === "entity" && !event.deleted) {
216
+ const fields = new Set([
217
+ ...(restriction.fields ?? []),
218
+ ...(restriction.relationships ?? []).map((name) => `${name}_id`),
219
+ ]);
220
+ event = {
221
+ ...event,
222
+ payload: JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(event.payload)).filter(([key]) => !fields.has(wire?.capture_bindings?.[event.entity_id]?.[key] ?? key)))),
223
+ };
224
+ }
225
+ const bytes = Buffer.byteLength(JSON.stringify(publishMessage(event))) + 1;
226
+ if (bytes + 32 > REPLICATION_BATCH_BYTES) {
227
+ return yield* Effect.die(new Error("A replication event exceeds the publish request limit."));
228
+ }
229
+ if (size + bytes > batchBytes) {
230
+ break;
231
+ }
232
+ size += bytes;
233
+ result.push({ historyId: reference.id, event });
234
+ }
235
+ if (suppressed.length) {
236
+ yield* sql.unsafe(`UPDATE _ontology_replication_delivery SET suppressed_at=now()
237
+ WHERE generation=$1::uuid AND event_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))`, [target.generation, JSON.stringify(suppressed)]);
238
+ }
239
+ return result;
240
+ });
241
+ /** Lost publish responses deliberately leave the same immutable IDs pending. */
242
+ const acknowledgeReplicationV4 = (sql, generation, ids) => sql.withTransaction(Effect.gen(function* () {
243
+ // Receipt foreign keys touch targets after delivery rows; drain before migration table locks.
244
+ yield* shareReplicationLifecycle(sql);
245
+ yield* sql.unsafe(`WITH accepted AS (
246
+ UPDATE _ontology_replication_delivery SET accepted_at = now()
247
+ WHERE generation = $1::uuid AND accepted_at IS NULL
248
+ AND event_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))
249
+ RETURNING event_id, accepted_at
250
+ ) INSERT INTO _ontology_replication_receipts (generation, event_id, accepted_at)
251
+ SELECT $1::uuid, event_id, accepted_at FROM accepted ON CONFLICT DO NOTHING`, [generation, JSON.stringify(ids)]);
252
+ }));
253
+ // @legacy ontology.replication-verification-remnants
254
+ /** Acceptance comes only from Pub/Sub receipts; historical verification columns are not used. */
255
+ export const replicationStatus = (sql, target) => target.delivery_version !== 4 &&
256
+ target.protocol !== 4
257
+ ? legacy.replicationStatus(sql, target)
258
+ : sql
259
+ .unsafe(`SELECT count(*)::text AS total, count(accepted_at)::text AS accepted
260
+ FROM _ontology_replication_delivery WHERE generation = $1::uuid`, [target.generation])
261
+ .pipe(Effect.map((rows) => rows[0]));
262
+ /** Initialization is a fixed fence, so continuing writes cannot move the readiness goal. */
263
+ export const replicationInitiallyReady = (sql, generation) => sql
264
+ .unsafe(`SELECT baseline_complete AND backfill_complete AND replay_complete AND NOT EXISTS (
265
+ SELECT 1 FROM _ontology_replication_delivery d WHERE d.generation = t.generation
266
+ AND d.accepted_at IS NULL AND d.revision <= t.initial_revision
267
+ ) AS ready FROM _ontology_replication_targets t WHERE generation = $1::uuid`, [generation])
268
+ .pipe(Effect.map((rows) => rows[0]?.ready ?? false));
269
+ /** Replay initialization is constant time; reset work is resumed in bounded pages. */
270
+ const replayReplicationV4 = (sql, generation) => sql.withTransaction(Effect.gen(function* () {
271
+ yield* lockReplicationLifecycle(sql);
272
+ yield* sql.unsafe(`UPDATE _ontology_replication_targets SET replay_complete = false, replay_cursor = NULL,
273
+ initial_revision = (SELECT CASE WHEN is_called THEN last_value ELSE 0 END FROM _ontology_replication_revision)
274
+ WHERE generation = $1::uuid`, [generation]);
275
+ }));
276
+ export const advanceReplicationReplay = (sql, generation) => sql.withTransaction(Effect.gen(function* () {
277
+ const [target] = yield* sql.unsafe(`SELECT replay_complete, backfill_complete,
278
+ COALESCE((to_jsonb(_ontology_replication_targets)->>'scans_complete')::boolean,true) AS scans_complete,
279
+ replay_cursor::text, initial_revision::text
280
+ FROM _ontology_replication_targets
281
+ WHERE generation = $1::uuid FOR NO KEY UPDATE`, [generation]);
282
+ if (!target || target.replay_complete) {
283
+ return true;
284
+ }
285
+ // A legacy import can add accepted rows behind any replay cursor. Wait until its full
286
+ // queue exists before resetting the fixed prefix, including when replay starts mid-import.
287
+ if (!target.backfill_complete || !target.scans_complete) {
288
+ return false;
289
+ }
290
+ const rows = yield* sql.unsafe(`SELECT event_id::text AS id, verified_at IS NOT NULL AS verified FROM _ontology_replication_delivery
291
+ WHERE generation = $1::uuid AND ($2::uuid IS NULL OR event_id > $2::uuid)
292
+ AND revision <= $3::bigint
293
+ ORDER BY event_id LIMIT 1000`, [generation, target.replay_cursor, target.initial_revision]);
294
+ const ids = JSON.stringify(rows.map((row) => row.id));
295
+ yield* sql.unsafe(`UPDATE _ontology_replication_delivery SET accepted_at = NULL, verified_at = NULL, next_verify_at = now()
296
+ WHERE generation = $1::uuid AND event_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))`, [generation, ids]);
297
+ yield* sql.unsafe(`DELETE FROM _ontology_replication_receipts WHERE generation = $1::uuid
298
+ AND event_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))`, [generation, ids]);
299
+ const complete = rows.length < 1000;
300
+ yield* sql.unsafe(`UPDATE _ontology_replication_targets SET replay_cursor = $2::uuid, replay_complete = $3,
301
+ verified_count = verified_count - $4::bigint WHERE generation = $1::uuid`, [
302
+ generation,
303
+ rows.at(-1)?.id ?? target.replay_cursor,
304
+ complete,
305
+ rows.filter((row) => row.verified).length,
306
+ ]);
307
+ return complete;
308
+ }));
309
+ /** Static pre-upgrade histories have no new inserts: v4 writers atomically enqueue new history. */
310
+ export const advanceReplicationBackfill = (sql, generation) => sql.withTransaction(Effect.gen(function* () {
311
+ yield* shareReplicationLifecycle(sql);
312
+ const [target] = yield* sql.unsafe(`SELECT ${targetFields}, backfill_cursor, backfill_revision::text
313
+ FROM _ontology_replication_targets WHERE generation = $1::uuid FOR NO KEY UPDATE`, [generation]);
314
+ if (!target || target.backfill_complete) {
315
+ return true;
316
+ }
317
+ const catalog = yield* readCatalog({ sql });
318
+ const tables = target.backfill_sources?.map((source) => source.table) ?? [
319
+ null,
320
+ ...target.selection.map((item) => {
321
+ const entity = (catalog ?? target.manifest).entities.find((entity) => entity.id === item.entityId) ??
322
+ target.manifest.entities.find((entity) => entity.id === item.entityId);
323
+ return historyTableName(entity.name);
324
+ }),
325
+ ];
326
+ const position = target.backfill_cursor.table ?? 0;
327
+ const table = tables[position];
328
+ if (target.backfill_sources?.[position]?.skip || position >= tables.length) {
329
+ const complete = position + 1 >= tables.length;
330
+ yield* sql.unsafe(`UPDATE _ontology_replication_targets SET backfill_cursor=$2::jsonb,backfill_complete=$3
331
+ WHERE generation=$1::uuid`, [generation, JSON.stringify({ table: position + 1 }), complete]);
332
+ return complete;
333
+ }
334
+ const cursor = target.backfill_cursor.id ?? null;
335
+ const rows = yield* sql.unsafe(table === null
336
+ ? `SELECT id::text FROM _ontology_replication_baselines WHERE generation = $1::uuid
337
+ AND ($2::uuid IS NULL OR id > $2::uuid) ORDER BY id LIMIT 1000`
338
+ : `SELECT id::text FROM ${qi(table)} WHERE replication IS NOT NULL
339
+ AND replication->>'source_id' = $3 AND (replication->>'revision')::bigint > $4::bigint
340
+ AND (replication->>'revision')::bigint <= $5::bigint
341
+ AND ($2::uuid IS NULL OR id > $2::uuid) AND $1::uuid IS NOT NULL ORDER BY id LIMIT 1000`, table === null
342
+ ? [generation, cursor]
343
+ : [generation, cursor, target.source_id, target.from_revision, target.backfill_revision]);
344
+ const ids = JSON.stringify(rows.map((row) => row.id));
345
+ const eventSelect = table === null
346
+ ? `SELECT id, event FROM _ontology_replication_baselines WHERE generation = $1::uuid`
347
+ : `SELECT id, replication AS event FROM ${qi(table)}`;
348
+ yield* sql.unsafe(`WITH inserted AS (
349
+ INSERT INTO _ontology_replication_delivery
350
+ (generation, event_id, history_table, revision, is_baseline, accepted_at, verified_at, next_verify_at)
351
+ SELECT $1::uuid, e.id, $3::text, (e.event->>'revision')::bigint, $4::boolean,
352
+ r.accepted_at, r.verified_at, now() FROM (${eventSelect}) e
353
+ LEFT JOIN _ontology_replication_receipts r ON r.generation = $1::uuid AND r.event_id = e.id
354
+ WHERE e.id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))
355
+ ON CONFLICT DO NOTHING RETURNING verified_at
356
+ ) UPDATE _ontology_replication_targets SET verified_count = verified_count +
357
+ (SELECT count(verified_at) FROM inserted) WHERE generation = $1::uuid`, [generation, ids, table, table === null]);
358
+ const finishedTable = rows.length < 1000;
359
+ const complete = finishedTable && position + 1 >= tables.length;
360
+ const next = finishedTable
361
+ ? { table: position + 1 }
362
+ : { table: position, id: rows.at(-1).id };
363
+ yield* sql.unsafe(`UPDATE _ontology_replication_targets SET backfill_cursor = $2::jsonb, backfill_complete = $3
364
+ WHERE generation = $1::uuid`, [generation, JSON.stringify(next), complete]);
365
+ return complete;
366
+ }));
367
+ export const acknowledgeReplication = (sql, generation, ids, version) => Effect.gen(function* () {
368
+ const current = version ?? (yield* protocol(sql));
369
+ yield* current >= 4
370
+ ? acknowledgeReplicationV4(sql, generation, ids)
371
+ : legacy.acknowledgeReplication(sql, generation, ids);
372
+ });
373
+ export const replayReplication = (sql, generation) => Effect.gen(function* () {
374
+ const current = yield* protocol(sql);
375
+ if (current >= 4) {
376
+ yield* replayReplicationV4(sql, generation);
377
+ yield* advanceReplicationReplay(sql, generation);
378
+ }
379
+ else {
380
+ yield* legacy.replayReplication(sql, generation);
381
+ }
382
+ });
@@ -0,0 +1,23 @@
1
+ import { Effect } from "effect";
2
+ import type { SqlClient } from "effect/unstable/sql";
3
+ import { SqlError } from "effect/unstable/sql/SqlError";
4
+ export declare const REPLICATION_LIFECYCLE_LOCK = "segmentstream.ontology.replication.lifecycle";
5
+ /** Legacy writers take the singleton first; lifecycle operations must keep that order. */
6
+ export declare const lockReplicationLifecycle: (sql: SqlClient.SqlClient) => Effect.Effect<void, SqlError, never>;
7
+ /** Acquire the relation before admission, like writers, even when the first page read is a target. */
8
+ export declare const shareReplicationLifecycle: (sql: SqlClient.SqlClient) => Effect.Effect<readonly object[], SqlError, never>;
9
+ /**
10
+ * Must run in the migration transaction, before catalog or business locks. Acquire the strongest
11
+ * relation lock up front: even ADD COLUMN IF NOT EXISTS needs it when the column already exists.
12
+ * Old writers read/lock the singleton before lifecycle admission, so waiting for DDL after taking
13
+ * lifecycle admission would prevent those readers from ever finishing.
14
+ *
15
+ * Older online replication pages can take shared lifecycle admission before the relation. Never
16
+ * wait for them while holding the relation: roll back just this acquisition savepoint, drain their
17
+ * lifecycle locks without any relation lock, then try again. No migration work is replayed.
18
+ * Bound contention to eight attempts; each blocking statement also retains the migration's SQL
19
+ * timeouts. Sustained old-worker traffic must fail the migration instead of holding its mutex forever.
20
+ */
21
+ export declare const lockReplicationSchema: (sql: SqlClient.SqlClient) => Effect.Effect<undefined, SqlError, never>;
22
+ /** Must run inside the write transaction, before any business row locks. */
23
+ export declare const beginReplicationWrite: (sql: SqlClient.SqlClient) => Effect.Effect<void, SqlError, never>;
@@ -0,0 +1,88 @@
1
+ import { Data, Effect } from "effect";
2
+ import { LockTimeoutError, SqlError } from "effect/unstable/sql/SqlError";
3
+ export const REPLICATION_LIFECYCLE_LOCK = "segmentstream.ontology.replication.lifecycle";
4
+ // @legacy ontology.replication-older-source-protocols
5
+ /** Legacy writers take the singleton first; lifecycle operations must keep that order. */
6
+ export const lockReplicationLifecycle = (sql) => Effect.gen(function* () {
7
+ yield* sql.unsafe("SELECT id FROM _ontology_replication WHERE id FOR UPDATE");
8
+ yield* sql.unsafe("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
9
+ REPLICATION_LIFECYCLE_LOCK,
10
+ ]);
11
+ });
12
+ const sharedLifecycleLock = (sql) => sql.unsafe("SELECT pg_advisory_xact_lock_shared(hashtextextended($1, 0))", [
13
+ REPLICATION_LIFECYCLE_LOCK,
14
+ ]);
15
+ /** Acquire the relation before admission, like writers, even when the first page read is a target. */
16
+ export const shareReplicationLifecycle = (sql) => sql
17
+ .unsafe("LOCK TABLE _ontology_replication IN ACCESS SHARE MODE")
18
+ .pipe(Effect.andThen(sharedLifecycleLock(sql)));
19
+ class RetryReplicationSchemaLock extends Data.TaggedError("RetryReplicationSchemaLock") {
20
+ }
21
+ const MAX_SCHEMA_LOCK_ATTEMPTS = 8;
22
+ /**
23
+ * Must run in the migration transaction, before catalog or business locks. Acquire the strongest
24
+ * relation lock up front: even ADD COLUMN IF NOT EXISTS needs it when the column already exists.
25
+ * Old writers read/lock the singleton before lifecycle admission, so waiting for DDL after taking
26
+ * lifecycle admission would prevent those readers from ever finishing.
27
+ *
28
+ * Older online replication pages can take shared lifecycle admission before the relation. Never
29
+ * wait for them while holding the relation: roll back just this acquisition savepoint, drain their
30
+ * lifecycle locks without any relation lock, then try again. No migration work is replayed.
31
+ * Bound contention to eight attempts; each blocking statement also retains the migration's SQL
32
+ * timeouts. Sustained old-worker traffic must fail the migration instead of holding its mutex forever.
33
+ */
34
+ export const lockReplicationSchema = (sql) => Effect.gen(function* () {
35
+ for (let attempt = 0; attempt < MAX_SCHEMA_LOCK_ATTEMPTS; attempt += 1) {
36
+ const acquired = yield* sql
37
+ .withTransaction(Effect.gen(function* () {
38
+ yield* sql.unsafe("LOCK TABLE _ontology_replication IN ACCESS EXCLUSIVE MODE");
39
+ // Keep the legacy singleton fence as well as the v4 lifecycle fence until commit.
40
+ yield* sql.unsafe("SELECT id FROM _ontology_replication WHERE id FOR UPDATE");
41
+ const [lock] = yield* sql.unsafe("SELECT pg_try_advisory_xact_lock(hashtextextended($1, 0)) AS acquired", [REPLICATION_LIFECYCLE_LOCK]);
42
+ if (!lock?.acquired) {
43
+ return yield* Effect.fail(new RetryReplicationSchemaLock());
44
+ }
45
+ }))
46
+ .pipe(Effect.as(true), Effect.catchTag("RetryReplicationSchemaLock", () => Effect.succeed(false)));
47
+ if (acquired) {
48
+ return;
49
+ }
50
+ if (attempt === MAX_SCHEMA_LOCK_ATTEMPTS - 1) {
51
+ break;
52
+ }
53
+ // Roll back the successful wait too, so the next attempt starts without either lock.
54
+ yield* sql
55
+ .withTransaction(sql
56
+ .unsafe("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
57
+ REPLICATION_LIFECYCLE_LOCK,
58
+ ])
59
+ .pipe(Effect.andThen(Effect.fail(new RetryReplicationSchemaLock()))))
60
+ .pipe(Effect.catchTag("RetryReplicationSchemaLock", () => Effect.void));
61
+ }
62
+ return yield* new SqlError({
63
+ reason: new LockTimeoutError({
64
+ message: "Replication schema admission remained contended after eight attempts.",
65
+ operation: "acquire replication schema locks",
66
+ cause: { attempts: MAX_SCHEMA_LOCK_ATTEMPTS, lock: REPLICATION_LIFECYCLE_LOCK },
67
+ }),
68
+ });
69
+ });
70
+ /** Must run inside the write transaction, before any business row locks. */
71
+ export const beginReplicationWrite = (sql) => Effect.gen(function* () {
72
+ const [schema] = yield* sql.unsafe("SELECT to_regclass('_ontology_replication') IS NOT NULL AS present");
73
+ if (!schema?.present) {
74
+ return;
75
+ }
76
+ // Shared admission permits independent writes while lifecycle changes drain admitted writers.
77
+ const [state] = yield* sql.unsafe("SELECT version FROM _ontology_replication WHERE id");
78
+ if ((state?.version ?? 0) < 4) {
79
+ // A new service may be deployed before its owner applies the protocol upgrade.
80
+ yield* sql.unsafe("SELECT id FROM _ontology_replication WHERE id FOR UPDATE");
81
+ yield* sql.unsafe("SELECT set_config('segmentstream.writer', '1', true)");
82
+ }
83
+ else {
84
+ // The version read already holds the relation lock; do not acquire it after admission.
85
+ yield* sharedLifecycleLock(sql);
86
+ yield* sql.unsafe("SELECT set_config('segmentstream.writer', '2', true)");
87
+ }
88
+ });