copilotz 1.0.0-rc.20 → 1.0.0-rc.21

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 (340) hide show
  1. package/CHANGELOG.md +417 -0
  2. package/README.md +295 -19
  3. package/hosts/orgs/mod.d.ts +4 -1
  4. package/hosts/orgs/mod.js +2 -2
  5. package/hosts/web/files.d.ts +6 -2
  6. package/hosts/web/files.js +36 -3
  7. package/hosts/web/node/mod.d.ts +1 -1
  8. package/hosts/web/node/mod.js +1 -1
  9. package/hosts/web/node/serve.d.ts +26 -0
  10. package/hosts/web/node/serve.js +23 -1
  11. package/hosts/web/routes.d.ts +28 -1
  12. package/hosts/web/routes.js +65 -10
  13. package/hosts/web/server.d.ts +5 -4
  14. package/hosts/web/server.js +1 -1
  15. package/package.json +2 -2
  16. package/plugins/access/accounts/collections/account.d.ts +2 -2
  17. package/plugins/access/contacts/actions/chat.d.ts +1 -0
  18. package/plugins/access/contacts/actions/chat.js +45 -0
  19. package/plugins/access/contacts/actions/forget.d.ts +1 -0
  20. package/plugins/access/contacts/actions/forget.js +56 -0
  21. package/plugins/access/contacts/actions/start.d.ts +1 -0
  22. package/plugins/access/contacts/actions/start.js +50 -0
  23. package/plugins/access/contacts/collections/contact.d.ts +81 -0
  24. package/plugins/access/contacts/collections/contact.js +58 -0
  25. package/plugins/access/contacts/contract.d.ts +10 -0
  26. package/plugins/access/contacts/contract.js +10 -0
  27. package/plugins/access/contacts/mod.d.ts +27 -0
  28. package/plugins/access/contacts/mod.js +49 -0
  29. package/plugins/access/contacts/views/contact.d.ts +8 -0
  30. package/plugins/access/contacts/views/contact.js +25 -0
  31. package/plugins/access/contacts/web.d.ts +7 -0
  32. package/plugins/access/contacts/web.js +11 -0
  33. package/plugins/access/directory/collections/credit-code.d.ts +1 -1
  34. package/plugins/access/directory/collections/invite.d.ts +1 -1
  35. package/plugins/access/identity/mod.js +8 -6
  36. package/plugins/access/mod.d.ts +7 -1
  37. package/plugins/access/mod.js +7 -2
  38. package/plugins/access/spaces/actions/agents.js +12 -6
  39. package/plugins/access/spaces/actions/create.js +10 -1
  40. package/plugins/access/spaces/actions/invitation.js +5 -2
  41. package/plugins/access/spaces/actions/keep.d.ts +1 -0
  42. package/plugins/access/spaces/actions/keep.js +55 -0
  43. package/plugins/access/spaces/actions/member.js +17 -4
  44. package/plugins/access/spaces/actions/rename.js +4 -0
  45. package/plugins/access/spaces/collections/member.d.ts +1 -1
  46. package/plugins/access/spaces/collections/request.d.ts +1 -1
  47. package/plugins/access/spaces/collections/space.d.ts +9 -0
  48. package/plugins/access/spaces/collections/space.js +2 -0
  49. package/plugins/access/spaces/contract.d.ts +4 -3
  50. package/plugins/access/spaces/contract.js +1 -1
  51. package/plugins/access/spaces/mod.d.ts +7 -1
  52. package/plugins/access/spaces/mod.js +8 -1
  53. package/plugins/access/spaces/resources/tools.d.ts +0 -5
  54. package/plugins/access/spaces/resources/tools.js +11 -1
  55. package/plugins/access/spaces/shared/agents.js +2 -2
  56. package/plugins/access/spaces/shared/can.d.ts +18 -3
  57. package/plugins/access/spaces/shared/can.js +32 -5
  58. package/plugins/access/spaces/shared/declared.d.ts +21 -0
  59. package/plugins/access/spaces/shared/declared.js +8 -0
  60. package/plugins/access/spaces/shared/labels.d.ts +7 -1
  61. package/plugins/access/spaces/shared/labels.js +56 -8
  62. package/plugins/access/spaces/shared/managers.js +2 -2
  63. package/plugins/access/spaces/shared/names.d.ts +9 -5
  64. package/plugins/access/spaces/shared/names.js +36 -19
  65. package/plugins/access/spaces/shared/reach.d.ts +20 -6
  66. package/plugins/access/spaces/shared/reach.js +62 -20
  67. package/plugins/access/spaces/shared/rules.d.ts +10 -5
  68. package/plugins/access/spaces/shared/rules.js +35 -10
  69. package/plugins/access/spaces/shared/seat.d.ts +3 -2
  70. package/plugins/access/spaces/shared/seat.js +9 -2
  71. package/plugins/access/spaces/views/where.d.ts +4 -2
  72. package/plugins/access/spaces/views/where.js +9 -0
  73. package/plugins/access/spaces/web.js +1 -1
  74. package/plugins/access/web.d.ts +1 -0
  75. package/plugins/access/web.js +1 -0
  76. package/plugins/agents/chat/actions/erase.d.ts +1 -0
  77. package/plugins/agents/chat/actions/erase.js +51 -0
  78. package/plugins/agents/chat/actions/open.d.ts +1 -0
  79. package/plugins/agents/chat/actions/open.js +49 -0
  80. package/plugins/agents/chat/actions/quiet.d.ts +1 -0
  81. package/plugins/agents/chat/actions/quiet.js +33 -0
  82. package/plugins/agents/chat/actions/say.d.ts +1 -0
  83. package/plugins/agents/chat/actions/say.js +82 -0
  84. package/plugins/agents/chat/actions/send.js +57 -19
  85. package/plugins/agents/chat/actions/shown.d.ts +1 -0
  86. package/plugins/agents/chat/actions/shown.js +35 -0
  87. package/plugins/agents/chat/collections/intent.d.ts +124 -0
  88. package/plugins/agents/chat/collections/intent.js +52 -0
  89. package/plugins/agents/chat/collections/message.d.ts +74 -1
  90. package/plugins/agents/chat/collections/message.js +23 -1
  91. package/plugins/agents/chat/collections/shown.d.ts +105 -0
  92. package/plugins/agents/chat/collections/shown.js +34 -0
  93. package/plugins/agents/chat/contract.d.ts +15 -7
  94. package/plugins/agents/chat/contract.js +7 -1
  95. package/plugins/agents/chat/mod.d.ts +20 -2
  96. package/plugins/agents/chat/mod.js +43 -4
  97. package/plugins/agents/chat/processors/failed.js +15 -5
  98. package/plugins/agents/chat/processors/turn.d.ts +1 -1
  99. package/plugins/agents/chat/processors/turn.js +95 -35
  100. package/plugins/agents/chat/shared/delivery.d.ts +16 -2
  101. package/plugins/agents/chat/shared/delivery.js +62 -7
  102. package/plugins/agents/chat/shared/send.d.ts +1 -3
  103. package/plugins/agents/chat/shared/send.js +2 -9
  104. package/plugins/agents/chat/shared/tools.d.ts +33 -0
  105. package/plugins/agents/chat/shared/tools.js +28 -0
  106. package/plugins/agents/chat/shared/types.d.ts +59 -1
  107. package/plugins/agents/chat/views/message.d.ts +4 -2
  108. package/plugins/agents/chat/views/message.js +22 -4
  109. package/plugins/agents/chat/views/prompt.js +38 -4
  110. package/plugins/agents/history/collections/outline.d.ts +1 -1
  111. package/plugins/agents/history/collections/summary.d.ts +1 -1
  112. package/plugins/agents/history/mod.js +2 -1
  113. package/plugins/agents/history/processors/erase.d.ts +3 -0
  114. package/plugins/agents/history/processors/erase.js +11 -0
  115. package/plugins/agents/memory/mod.js +2 -1
  116. package/plugins/agents/memory/processors/extract.d.ts +1 -1
  117. package/plugins/agents/memory/processors/extract.js +5 -0
  118. package/plugins/agents/memory/resources/tools.js +1 -1
  119. package/plugins/agents/memory/shared/settings.d.ts +7 -2
  120. package/plugins/agents/memory/shared/settings.js +9 -1
  121. package/plugins/agents/routines/collections/routine.d.ts +1 -1
  122. package/plugins/agents/routines/collections/run.d.ts +1 -1
  123. package/plugins/compute/apps/collections/app.d.ts +2 -2
  124. package/plugins/compute/cloud/resources/tools.js +3 -3
  125. package/plugins/compute/computers/collections/approval.d.ts +2 -2
  126. package/plugins/compute/computers/collections/grant.d.ts +1 -1
  127. package/plugins/compute/computers/collections/preview.d.ts +2 -2
  128. package/plugins/compute/computers/program/version.d.ts +1 -1
  129. package/plugins/compute/computers/program/version.js +1 -1
  130. package/plugins/compute/computers/resources/downloads.js +1 -1
  131. package/plugins/compute/computers/shared/wake.d.ts +1 -1
  132. package/plugins/compute/computers/shared/wake.js +17 -0
  133. package/plugins/interfaces/api/collections/key.d.ts +1 -1
  134. package/plugins/interfaces/api/shared/events.js +5 -4
  135. package/plugins/interfaces/maintenance/actions/read.d.ts +4 -0
  136. package/plugins/interfaces/maintenance/actions/read.js +76 -0
  137. package/plugins/interfaces/maintenance/actions/write.d.ts +4 -0
  138. package/plugins/interfaces/maintenance/actions/write.js +64 -0
  139. package/plugins/interfaces/maintenance/contract.d.ts +60 -0
  140. package/plugins/interfaces/maintenance/contract.js +2 -0
  141. package/plugins/interfaces/maintenance/mod.d.ts +52 -0
  142. package/plugins/interfaces/maintenance/mod.js +66 -0
  143. package/plugins/interfaces/maintenance/resources/routes.d.ts +71 -0
  144. package/plugins/interfaces/maintenance/resources/routes.js +457 -0
  145. package/plugins/interfaces/maintenance/shared/ops.d.ts +4 -0
  146. package/plugins/interfaces/maintenance/shared/ops.js +19 -0
  147. package/plugins/interfaces/mcp/actions/call.d.ts +11 -0
  148. package/plugins/interfaces/mcp/actions/call.js +304 -0
  149. package/plugins/interfaces/mcp/actions/prune.d.ts +1 -0
  150. package/plugins/interfaces/mcp/actions/prune.js +18 -0
  151. package/plugins/interfaces/mcp/collections/closed.d.ts +23 -0
  152. package/plugins/interfaces/mcp/collections/closed.js +17 -0
  153. package/plugins/interfaces/mcp/collections/rate.d.ts +27 -0
  154. package/plugins/interfaces/mcp/collections/rate.js +18 -0
  155. package/plugins/interfaces/mcp/collections/receipt.d.ts +46 -0
  156. package/plugins/interfaces/mcp/collections/receipt.js +46 -0
  157. package/plugins/interfaces/mcp/contract.d.ts +9 -0
  158. package/plugins/interfaces/mcp/contract.js +9 -0
  159. package/plugins/interfaces/mcp/mod.d.ts +84 -0
  160. package/plugins/interfaces/mcp/mod.js +122 -0
  161. package/plugins/interfaces/mcp/processors/closed.d.ts +13 -0
  162. package/plugins/interfaces/mcp/processors/closed.js +16 -0
  163. package/plugins/interfaces/mcp/resources/running.d.ts +21 -0
  164. package/plugins/interfaces/mcp/resources/running.js +36 -0
  165. package/plugins/interfaces/mcp/resources/server.d.ts +54 -0
  166. package/plugins/interfaces/mcp/resources/server.js +423 -0
  167. package/plugins/interfaces/mcp/resources/tools.d.ts +114 -0
  168. package/plugins/interfaces/mcp/resources/tools.js +324 -0
  169. package/plugins/interfaces/mcp/shared/caller.d.ts +45 -0
  170. package/plugins/interfaces/mcp/shared/caller.js +99 -0
  171. package/plugins/interfaces/mcp/shared/keys.d.ts +50 -0
  172. package/plugins/interfaces/mcp/shared/keys.js +53 -0
  173. package/plugins/interfaces/mcp/shared/settings.d.ts +187 -0
  174. package/plugins/interfaces/mcp/shared/settings.js +176 -0
  175. package/plugins/interfaces/mcp/views/offered.d.ts +20 -0
  176. package/plugins/interfaces/mcp/views/offered.js +20 -0
  177. package/plugins/interfaces/mod.d.ts +5 -1
  178. package/plugins/interfaces/mod.js +5 -1
  179. package/plugins/interfaces/terminal/actions/print.js +1 -1
  180. package/plugins/interfaces/terminal/actions/set.js +1 -1
  181. package/plugins/interfaces/terminal/collections/output.d.ts +1 -1
  182. package/plugins/interfaces/terminal/collections/shell.js +1 -1
  183. package/plugins/interfaces/voice/actions/block.d.ts +2 -0
  184. package/plugins/interfaces/voice/actions/block.js +74 -0
  185. package/plugins/interfaces/voice/actions/call.d.ts +19 -0
  186. package/plugins/interfaces/voice/actions/call.js +253 -0
  187. package/plugins/interfaces/voice/actions/hook.d.ts +5 -0
  188. package/plugins/interfaces/voice/actions/hook.js +225 -0
  189. package/plugins/interfaces/voice/actions/prune.d.ts +1 -0
  190. package/plugins/interfaces/voice/actions/prune.js +35 -0
  191. package/plugins/interfaces/voice/collections/blocked.d.ts +87 -0
  192. package/plugins/interfaces/voice/collections/blocked.js +34 -0
  193. package/plugins/interfaces/voice/collections/call.d.ts +279 -0
  194. package/plugins/interfaces/voice/collections/call.js +77 -0
  195. package/plugins/interfaces/voice/collections/phone.d.ts +74 -0
  196. package/plugins/interfaces/voice/collections/phone.js +26 -0
  197. package/plugins/interfaces/voice/contract.d.ts +14 -0
  198. package/plugins/interfaces/voice/contract.js +2 -0
  199. package/plugins/interfaces/voice/mod.d.ts +57 -0
  200. package/plugins/interfaces/voice/mod.js +118 -0
  201. package/plugins/interfaces/voice/processors/forget.d.ts +13 -0
  202. package/plugins/interfaces/voice/processors/forget.js +27 -0
  203. package/plugins/interfaces/voice/resources/relay.d.ts +77 -0
  204. package/plugins/interfaces/voice/resources/relay.js +428 -0
  205. package/plugins/interfaces/voice/resources/tools.d.ts +1 -0
  206. package/plugins/interfaces/voice/resources/tools.js +25 -0
  207. package/plugins/interfaces/voice/shared/caller.d.ts +27 -0
  208. package/plugins/interfaces/voice/shared/caller.js +38 -0
  209. package/plugins/interfaces/voice/shared/settings.d.ts +136 -0
  210. package/plugins/interfaces/voice/shared/settings.js +121 -0
  211. package/plugins/interfaces/voice/shared/signature.d.ts +11 -0
  212. package/plugins/interfaces/voice/shared/signature.js +34 -0
  213. package/plugins/interfaces/voice/views/call.d.ts +41 -0
  214. package/plugins/interfaces/voice/views/call.js +30 -0
  215. package/plugins/interfaces/voice/views/context.d.ts +9 -0
  216. package/plugins/interfaces/voice/views/context.js +14 -0
  217. package/plugins/interfaces/voice/views/message.d.ts +17 -0
  218. package/plugins/interfaces/voice/views/message.js +41 -0
  219. package/plugins/interfaces/whatsapp/actions/connect.js +3 -0
  220. package/plugins/interfaces/whatsapp/actions/disconnect.js +4 -1
  221. package/plugins/interfaces/whatsapp/actions/hook.js +27 -11
  222. package/plugins/interfaces/whatsapp/actions/set.d.ts +8 -0
  223. package/plugins/interfaces/whatsapp/actions/set.js +121 -0
  224. package/plugins/interfaces/whatsapp/collections/blocked.d.ts +29 -0
  225. package/plugins/interfaces/whatsapp/collections/blocked.js +22 -0
  226. package/plugins/interfaces/whatsapp/collections/code.d.ts +1 -1
  227. package/plugins/interfaces/whatsapp/collections/number.d.ts +90 -4
  228. package/plugins/interfaces/whatsapp/collections/number.js +36 -4
  229. package/plugins/interfaces/whatsapp/collections/phone.d.ts +13 -3
  230. package/plugins/interfaces/whatsapp/collections/phone.js +9 -3
  231. package/plugins/interfaces/whatsapp/collections/rate.d.ts +27 -0
  232. package/plugins/interfaces/whatsapp/collections/rate.js +13 -0
  233. package/plugins/interfaces/whatsapp/collections/sent.d.ts +24 -12
  234. package/plugins/interfaces/whatsapp/collections/sent.js +11 -7
  235. package/plugins/interfaces/whatsapp/collections/thread.d.ts +14 -6
  236. package/plugins/interfaces/whatsapp/collections/thread.js +7 -5
  237. package/plugins/interfaces/whatsapp/collections/typing.d.ts +70 -0
  238. package/plugins/interfaces/whatsapp/collections/typing.js +21 -0
  239. package/plugins/interfaces/whatsapp/contract.d.ts +27 -4
  240. package/plugins/interfaces/whatsapp/contract.js +34 -1
  241. package/plugins/interfaces/whatsapp/events/undelivered.d.ts +8 -0
  242. package/plugins/interfaces/whatsapp/events/undelivered.js +17 -0
  243. package/plugins/interfaces/whatsapp/mod.d.ts +28 -2
  244. package/plugins/interfaces/whatsapp/mod.js +65 -6
  245. package/plugins/interfaces/whatsapp/processors/forget.d.ts +6 -0
  246. package/plugins/interfaces/whatsapp/processors/forget.js +11 -0
  247. package/plugins/interfaces/whatsapp/processors/receive.d.ts +17 -3
  248. package/plugins/interfaces/whatsapp/processors/receive.js +264 -81
  249. package/plugins/interfaces/whatsapp/processors/reply.d.ts +25 -5
  250. package/plugins/interfaces/whatsapp/processors/reply.js +216 -20
  251. package/plugins/interfaces/whatsapp/processors/typing.d.ts +8 -0
  252. package/plugins/interfaces/whatsapp/processors/typing.js +25 -0
  253. package/plugins/interfaces/whatsapp/shared/customers.d.ts +13 -0
  254. package/plugins/interfaces/whatsapp/shared/customers.js +16 -0
  255. package/plugins/interfaces/whatsapp/shared/graph.d.ts +31 -6
  256. package/plugins/interfaces/whatsapp/shared/graph.js +117 -10
  257. package/plugins/interfaces/whatsapp/shared/media.d.ts +23 -0
  258. package/plugins/interfaces/whatsapp/shared/media.js +46 -0
  259. package/plugins/interfaces/whatsapp/shared/number.d.ts +19 -8
  260. package/plugins/interfaces/whatsapp/shared/number.js +64 -7
  261. package/plugins/interfaces/whatsapp/shared/parts.d.ts +94 -0
  262. package/plugins/interfaces/whatsapp/shared/parts.js +184 -0
  263. package/plugins/interfaces/whatsapp/shared/payload.d.ts +27 -1
  264. package/plugins/interfaces/whatsapp/shared/payload.js +36 -1
  265. package/plugins/interfaces/whatsapp/shared/settings.d.ts +49 -1
  266. package/plugins/interfaces/whatsapp/shared/settings.js +11 -0
  267. package/plugins/interfaces/whatsapp/views/message.d.ts +13 -0
  268. package/plugins/interfaces/whatsapp/views/message.js +13 -0
  269. package/plugins/interfaces/whatsapp/views/web/line.d.ts +24 -0
  270. package/plugins/interfaces/whatsapp/views/web/line.js +44 -0
  271. package/plugins/interfaces/whatsapp/views/web/space.d.ts +7 -5
  272. package/plugins/interfaces/whatsapp/views/web/space.js +17 -10
  273. package/plugins/interfaces/whatsapp/views/web/style.d.ts +1 -1
  274. package/plugins/interfaces/whatsapp/views/web/style.js +8 -0
  275. package/plugins/models/adapters/llm/anthropic.d.ts +1 -1
  276. package/plugins/models/adapters/llm/anthropic.js +28 -15
  277. package/plugins/models/adapters/llm/gemini.d.ts +1 -1
  278. package/plugins/models/adapters/llm/gemini.js +2 -1
  279. package/plugins/models/adapters/llm/openai-compatible.d.ts +1 -1
  280. package/plugins/models/adapters/llm/openai-compatible.js +2 -1
  281. package/plugins/models/adapters/llm/openai.d.ts +1 -1
  282. package/plugins/models/adapters/llm/openai.js +16 -4
  283. package/plugins/models/shared/types.d.ts +8 -0
  284. package/plugins/models/shared/types.js +10 -0
  285. package/runtime/actor.d.ts +6 -0
  286. package/runtime/actor.js +9 -4
  287. package/runtime/aggregate.js +8 -2
  288. package/runtime/blobs.d.ts +15 -12
  289. package/runtime/blobs.js +58 -11
  290. package/runtime/cache.d.ts +6 -0
  291. package/runtime/cache.js +19 -1
  292. package/runtime/define.d.ts +10 -0
  293. package/runtime/errors.d.ts +20 -2
  294. package/runtime/errors.js +7 -1
  295. package/runtime/executor.js +27 -11
  296. package/runtime/links.d.ts +31 -0
  297. package/runtime/links.js +60 -0
  298. package/runtime/maintain.d.ts +88 -0
  299. package/runtime/maintain.js +120 -0
  300. package/runtime/memory.js +7 -4
  301. package/runtime/mod.d.ts +6 -4
  302. package/runtime/mod.js +5 -2
  303. package/runtime/mongo.d.ts +13 -0
  304. package/runtime/mongo.js +39 -8
  305. package/runtime/observe.d.ts +2 -0
  306. package/runtime/observe.js +14 -10
  307. package/runtime/ops.d.ts +5 -0
  308. package/runtime/ops.js +10 -1
  309. package/runtime/owner.d.ts +18 -1
  310. package/runtime/owner.js +127 -61
  311. package/runtime/presence.d.ts +18 -0
  312. package/runtime/presence.js +25 -0
  313. package/runtime/registry.d.ts +5 -0
  314. package/runtime/registry.js +18 -0
  315. package/runtime/runtime.js +9 -3
  316. package/runtime/store.d.ts +48 -1
  317. package/runtime/wire.d.ts +1 -0
  318. package/runtime/wire.js +3 -1
  319. package/stores/blobs/files/mod.js +4 -2
  320. package/stores/blobs/gcs/mod.d.ts +3 -0
  321. package/stores/blobs/gcs/mod.js +6 -5
  322. package/stores/blobs/http/mod.d.ts +1 -1
  323. package/stores/blobs/http/mod.js +2 -1
  324. package/stores/latency/mod.js +5 -1
  325. package/stores/sql/postgres/maintenance.d.ts +24 -0
  326. package/stores/sql/postgres/maintenance.js +258 -0
  327. package/stores/sql/postgres/mod.d.ts +20 -4
  328. package/stores/sql/postgres/mod.js +363 -96
  329. package/stores/sql/postgres/update.js +3 -1
  330. package/stores/sql/shared/capped.d.ts +2 -0
  331. package/stores/sql/shared/capped.js +22 -0
  332. package/stores/sql/shared/rows.d.ts +14 -0
  333. package/stores/sql/shared/rows.js +29 -0
  334. package/stores/sql/shared/statement.d.ts +12 -0
  335. package/stores/sql/shared/statement.js +244 -0
  336. package/stores/sql/sqlite/apart.d.ts +11 -0
  337. package/stores/sql/sqlite/apart.js +219 -0
  338. package/stores/sql/sqlite/mod.d.ts +16 -2
  339. package/stores/sql/sqlite/mod.js +246 -51
  340. package/testing/mod.js +1 -1
@@ -1,4 +1,6 @@
1
+ import { failure, invalid } from "../../../runtime/errors.js";
1
2
  import { applyUpdate, seedOf } from "../../../runtime/mongo.js";
3
+ import { checkBlobId } from "../../../runtime/blobs.js";
2
4
  import { canonical } from "../../../runtime/registry.js";
3
5
  import { cosine, fenced, } from "../../../runtime/store.js";
4
6
  import { page, where } from "../shared/filter.js";
@@ -8,6 +10,10 @@ import { expression } from "./update.js";
8
10
  import { constant, forget, ident, others, prepare, remembered, TABLES, } from "./layout.js";
9
11
  import { postgresPool } from "./pool.js";
10
12
  import { dropTenant } from "./tenants.js";
13
+ import { statement } from "../shared/statement.js";
14
+ import { allowed, alone, connect, noRole, triggers } from "./maintenance.js";
15
+ import { connections } from "../shared/capped.js";
16
+ import { upTo, weight } from "../shared/rows.js";
11
17
  export { postgresPool } from "./pool.js";
12
18
  export { copyTenant, dropTenant, exportTenant } from "./tenants.js";
13
19
  const hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
@@ -34,6 +40,42 @@ const binder = () => {
34
40
  return { bind, params };
35
41
  };
36
42
  const same = (a, b) => canonical(a) === canonical(b);
43
+ /** Statements a cursor can hold, so a read stops at its limit. */
44
+ const CURSORS = new Set(["SELECT", "WITH", "VALUES", "TABLE"]);
45
+ const sharedSchema = () => Object.assign(new Error("This store shares its schema with other tenants: SQL could reach theirs."), { code: "shared_schema" });
46
+ /**
47
+ * A database's refusal of maintenance SQL, said so: `read_only` (a write in
48
+ * a read), `timeout` (it ran too long), else `sql`, with its SQLSTATE.
49
+ */
50
+ const refused = (error) => {
51
+ const state = error?.code;
52
+ if (typeof state !== "string" || !/^[0-9A-Z]{5}$/.test(state))
53
+ return error;
54
+ const code = state === "25006" ? "read_only" : state === "57014" ? "timeout" : "sql";
55
+ return Object.assign(new Error(error instanceof Error ? error.message : String(error)), {
56
+ code,
57
+ state,
58
+ });
59
+ };
60
+ /** A reading that stops at the rows it fetched: nothing it did is kept. */
61
+ const READ = Symbol("read");
62
+ const triggered = () => failure("forbidden", "The store's tables have a trigger, which the store never makes: someone else did, so " +
63
+ "maintenance SQL doesn't run (and what made one is rolled back).");
64
+ const stateChanged = () => failure("forbidden", "The statement changed the store's own state (meta: its fencing epoch or Processors' " +
65
+ "positions): it was rolled back.");
66
+ /**
67
+ * A document (a jsonb expression) without the private fields at these dot
68
+ * paths, as the log keeps it. As in JavaScript, a path through an array
69
+ * reaches nothing (`#-` would take its keys for indexes).
70
+ */
71
+ const unlogged = (doc, fields, bind) => {
72
+ const path = (keys) => `ARRAY[${keys.map((k) => `${bind(k)}::text`).join(", ")}]::text[]`;
73
+ return fields.reduce((at, field) => {
74
+ const keys = field.split(".");
75
+ return `(${at} #- CASE WHEN jsonb_typeof(${doc} #> ${path(keys.slice(0, -1))}) = 'object' ` +
76
+ `THEN ${path(keys)} ELSE '{}'::text[] END)`;
77
+ }, doc);
78
+ };
37
79
  /** A short hash, the same for the same text: names and fingerprints for what a store makes. */
38
80
  const hash = (text) => {
39
81
  let h = 2166136261;
@@ -61,6 +103,27 @@ export async function postgresStore(options) {
61
103
  const T = constant(tenant);
62
104
  const writer = pool.writes;
63
105
  const committed = pool.reads;
106
+ /** Maintenance connections open at once, at most so many. */
107
+ const capped = connections(options.maintenance?.connections ?? 2);
108
+ /**
109
+ * Maintenance SQL's settings, for its transaction only: strings read as the
110
+ * statement check reads them, the views show this tenant, unqualified names
111
+ * are the schema's (then `public`'s, then the catalog's, and a temporary
112
+ * object's last: none stands in for a catalog), and a statement runs at
113
+ * most `timeout` ms.
114
+ */
115
+ const scoped = (timeout) => `pg_catalog.set_config('standard_conforming_strings', 'on', true), ` +
116
+ `pg_catalog.set_config('copilotz.tenant', ${T}, true), ` +
117
+ `pg_catalog.set_config('search_path', ` +
118
+ `${constant(`${ident(schema)}, public, pg_catalog, pg_temp`)}, true)` +
119
+ (timeout !== undefined && Number.isFinite(timeout) && timeout > 0
120
+ ? `, pg_catalog.set_config('statement_timeout', ${constant(String(Math.ceil(timeout)))}, true)`
121
+ : "");
122
+ /**
123
+ * What the store's checks after a statement read by: the catalog, then a
124
+ * temporary object last, whatever the statement set (in its transaction).
125
+ */
126
+ const settled = "SELECT pg_catalog.set_config('search_path', 'pg_catalog, pg_temp', true)";
64
127
  let layout;
65
128
  try {
66
129
  layout = await prepare(pool, schema);
@@ -121,7 +184,15 @@ export async function postgresStore(options) {
121
184
  const gate = (alone, work) => new Promise((resolve, reject) => {
122
185
  waiting.push({
123
186
  alone,
124
- start: () => void work().then(resolve, reject).finally(() => {
187
+ // Work that throws before it returns a promise fails like one that rejects.
188
+ start: () => void (() => {
189
+ try {
190
+ return work();
191
+ }
192
+ catch (error) {
193
+ return Promise.reject(error);
194
+ }
195
+ })().then(resolve, reject).finally(() => {
125
196
  running--;
126
197
  if (alone)
127
198
  solo = false;
@@ -242,7 +313,7 @@ export async function postgresStore(options) {
242
313
  }));
243
314
  },
244
315
  async blob(id) {
245
- const [row] = await run(db, `SELECT data, type FROM ${t("blobs")} WHERE tenant = ${T} AND id = $1`, [id]);
316
+ const [row] = await run(db, `SELECT data, type FROM ${t("blobs")} WHERE tenant = ${T} AND id = $1`, [checkBlobId(id)]);
246
317
  if (!row)
247
318
  return null;
248
319
  return {
@@ -256,7 +327,7 @@ export async function postgresStore(options) {
256
327
  `type = ANY($2::text[]) ORDER BY seq`, [id, types])).map(event),
257
328
  };
258
329
  };
259
- const transaction = (tx, emitted, held) => {
330
+ const transaction = (tx, emitted, held, maintenance) => {
260
331
  /** The ids that went in. With `ifAbsent`, existing ones are left as they are. */
261
332
  const insert = async (collection, docs, ifAbsent = false) => {
262
333
  if (!docs.length)
@@ -440,9 +511,9 @@ export async function postgresStore(options) {
440
511
  await run(tx, `DELETE FROM ${t("edges")} WHERE tenant = ${T} AND "from" = $1 AND type = $2 AND ` +
441
512
  `"to" = $3`, [edge.from, edge.type, edge.to]);
442
513
  },
443
- putBlob(value) {
514
+ async putBlob(value) {
515
+ checkBlobId(value.id);
444
516
  held.blobs.push(value);
445
- return Promise.resolve();
446
517
  },
447
518
  async putVector(value) {
448
519
  await run(tx, `INSERT INTO ${t("vectors")} (tenant, collection, node, field, embedding) VALUES (${T}, ` +
@@ -458,6 +529,52 @@ export async function postgresStore(options) {
458
529
  emit(value) {
459
530
  emitted.push({ ...value, id: value.id ?? crypto.randomUUID() });
460
531
  },
532
+ /**
533
+ * Maintenance SQL, as it is (runtime/maintain.ts), in a maintenance
534
+ * transaction: alone, so no other write holds numbers for events it
535
+ * might add to the log. The rows it changed are what this
536
+ * transaction's table statistics moved by, in every table. A statement
537
+ * that changes the store's own state (`meta`: the fencing epoch,
538
+ * Processors' positions) is refused, and its transaction rolled back.
539
+ */
540
+ async execute(sql, params = [], options = {}) {
541
+ if (!maintenance)
542
+ throw invalid("SQL runs only in a maintenance transaction.");
543
+ const { text } = statement(sql, "postgres", "write");
544
+ await flush();
545
+ // Every name the checks read by is qualified: the statement may have made temporary
546
+ // objects named as catalogs, and set the search path, in this transaction.
547
+ const moved = `(SELECT coalesce(pg_catalog.sum(n_tup_ins + n_tup_upd + ` +
548
+ `n_tup_del), 0) FROM pg_catalog.pg_stat_xact_user_tables)::pg_catalog.int8`;
549
+ const state = `(SELECT coalesce(pg_catalog.jsonb_object_agg(key, value), ` +
550
+ `'{}'::pg_catalog.jsonb) FROM ${t("meta")} WHERE tenant = ${T})::pg_catalog.text`;
551
+ const [before] = await run(tx, `SELECT ${scoped(options.timeout)}, ${moved} AS n, ${state} AS state, ` +
552
+ `${triggers(schema)} AS triggers`);
553
+ if (before.triggers)
554
+ throw triggered();
555
+ let rows;
556
+ try {
557
+ rows = await run(tx, text, params);
558
+ }
559
+ catch (error) {
560
+ throw refused(error);
561
+ }
562
+ await run(tx, settled);
563
+ const [after] = await run(tx, `SELECT ${moved} AS n, ${state} AS state, ${triggers(schema)} AS triggers, (SELECT ` +
564
+ `coalesce(pg_catalog.max(seq), 0) FROM ${t("events")} WHERE tenant = ` +
565
+ `${T}) AS seq`);
566
+ if (after.state !== before.state)
567
+ throw stateChanged();
568
+ if (after.triggers)
569
+ throw triggered();
570
+ // What the role holds is as it was: no large object, setting or grant came of it.
571
+ if (pool.server)
572
+ await allowed(counted(tx), schema);
573
+ // Events it wrote into the log keep their numbers: this commit's go after them.
574
+ if (last !== undefined)
575
+ last = Math.max(last, Number(after.seq));
576
+ return { rows, changed: Number(after.n) - Number(before.n) };
577
+ },
461
578
  };
462
579
  };
463
580
  /** Events as VALUES rows (id, type, data, cause, actor, at, i), parameters bound. */
@@ -610,6 +727,13 @@ export async function postgresStore(options) {
610
727
  * transaction).
611
728
  */
612
729
  const command = (owner, c) => {
730
+ try {
731
+ for (const blob of c.blobs)
732
+ checkBlobId(blob.id);
733
+ }
734
+ catch (error) {
735
+ return Promise.reject(error);
736
+ }
613
737
  const writes = c.steps.flatMap((step, i) => "write" in step ? [{ ...step, i }] : []);
614
738
  const touched = new Map();
615
739
  for (const { write: op } of writes) {
@@ -638,7 +762,7 @@ export async function postgresStore(options) {
638
762
  const sources = [];
639
763
  const seeds = [];
640
764
  const inserted = [];
641
- for (const [n, { write: op, record, i }] of writes.entries()) {
765
+ for (const [n, { write: op, record, private: hidden = [], i }] of writes.entries()) {
642
766
  let changes = "";
643
767
  let seed;
644
768
  switch (op.op) {
@@ -720,12 +844,13 @@ export async function postgresStore(options) {
720
844
  `jsonb_object_keys(${of}) AS k(k) WHERE ${condition})`;
721
845
  const changed = keys("after", "before -> k IS DISTINCT FROM after -> k");
722
846
  const removed = keys("before", "NOT after ? k");
847
+ const [after, before] = [unlogged("after", hidden, bind), unlogged("before", hidden, bind)];
723
848
  sources.push(`SELECT ${i} AS grp, row_number() OVER (ORDER BY ord) AS ord, ` +
724
849
  `gen_random_uuid()::text AS id, ${bind(op.collection)}::text || CASE WHEN before ` +
725
850
  `IS NULL THEN '.created' WHEN after IS NULL THEN '.deleted' ELSE '.updated' END ` +
726
- `AS type, CASE WHEN before IS NULL THEN jsonb_build_object('_id', id, 'doc', after) ` +
727
- `WHEN after IS NULL THEN jsonb_build_object('_id', id, 'doc', before) ELSE ` +
728
- `jsonb_build_object('_id', id, 'doc', after, 'changed', ${changed}, 'removed', ` +
851
+ `AS type, CASE WHEN before IS NULL THEN jsonb_build_object('_id', id, 'doc', ${after}) ` +
852
+ `WHEN after IS NULL THEN jsonb_build_object('_id', id, 'doc', ${before}) ELSE ` +
853
+ `jsonb_build_object('_id', id, 'doc', ${after}, 'changed', ${changed}, 'removed', ` +
729
854
  `${removed}) END AS data, ${bind(c.cause ?? null)}::text AS cause, ` +
730
855
  `${bind(c.actor ? JSON.stringify(c.actor) : null)}::jsonb AS actor FROM changes${n}`);
731
856
  }
@@ -790,6 +915,73 @@ export async function postgresStore(options) {
790
915
  return { results, events, changes };
791
916
  });
792
917
  };
918
+ /**
919
+ * Refuses maintenance SQL where it could reach what isn't this tenant's: a
920
+ * schema other tenants share; on a server, a role that holds anything
921
+ * beyond maintenance's grants; in PGlite, another schema's tables
922
+ * (maintenance.ts). `db` is the connection it runs on.
923
+ */
924
+ const reachable = async (db) => {
925
+ if (pool.server)
926
+ return await allowed(db, schema);
927
+ if (await others(counted(committed), schema, tenant))
928
+ throw sharedSchema();
929
+ await alone(db, schema);
930
+ };
931
+ /**
932
+ * On a server, a connection of its own for one maintenance statement, as
933
+ * the maintenance role, closed after, within the cap (maintenance.ts).
934
+ */
935
+ const dedicated = async (work) => {
936
+ // A schema other tenants share comes first: no role would make SQL there theirs alone.
937
+ if (await others(counted(committed), schema, tenant))
938
+ throw sharedSchema();
939
+ if (!options.maintenance)
940
+ throw noRole();
941
+ const url = options.maintenance.url;
942
+ return await capped(async () => {
943
+ const db = await connect(url);
944
+ try {
945
+ return await work(counted(db));
946
+ }
947
+ finally {
948
+ await db.end();
949
+ }
950
+ });
951
+ };
952
+ /**
953
+ * PGlite has one session, the store's: after maintenance SQL, it forgets
954
+ * whatever that left on it (settings, session locks, prepared statements).
955
+ */
956
+ const discard = () => run(writer, "DISCARD ALL").then(() => { }, () => { });
957
+ /**
958
+ * A maintenance transaction on a server: alone, on a connection of its
959
+ * own (closed after), fenced like any commit (the epoch, locked for share),
960
+ * with its events, numbered as the store numbers them, in the same commit.
961
+ */
962
+ const maintained = (owner, work) => dedicated(async (db) => {
963
+ let inserting = [];
964
+ try {
965
+ await reachable(db);
966
+ await db.query("BEGIN");
967
+ const [fence] = (await db.query(`SELECT value FROM ${t("meta")} WHERE tenant = ${T} AND key = 'epoch' FOR SHARE`)).rows;
968
+ if (Number(fence?.value) !== owner)
969
+ throw fenced();
970
+ const emitted = [];
971
+ const held = { docs: [], blobs: [] };
972
+ const scope = transaction(db, emitted, held, true);
973
+ const result = await work(scope);
974
+ inserting = [...held.docs];
975
+ await scope.flush();
976
+ const events = await record(db, unique(emitted), {});
977
+ await db.query("COMMIT");
978
+ return { result, events };
979
+ }
980
+ catch (error) {
981
+ await db.query("ROLLBACK").catch(() => { });
982
+ throw await conflict(error, inserting);
983
+ }
984
+ });
793
985
  return {
794
986
  ...reader(committed),
795
987
  command,
@@ -806,89 +998,99 @@ export async function postgresStore(options) {
806
998
  const rows = await run(committed, `SELECT key, value FROM ${t("meta")} WHERE tenant = ${T} AND key LIKE 'position:%'`);
807
999
  return Object.fromEntries(rows.map((row) => [String(row.key).slice("position:".length), Number(row.value)]));
808
1000
  },
809
- transaction: (owner, work, options = {}) => gate(false, async () => {
810
- const positions = options.positions ?? {};
811
- // The database transaction opens with the command's first statement. A
812
- // command that only records events (an action's completion, say) needs
813
- // none: its events go in as one fenced statement, atomic on its own.
814
- const emitted = [];
815
- let open;
816
- const begin = () => {
817
- if (closed)
818
- throw new Error("This store is closed.");
819
- if (!open) {
820
- const ready = Promise.withResolvers();
821
- const done = Promise.withResolvers();
822
- done.promise.catch(() => { });
823
- const settled = writer.transaction(async (tx) => {
824
- // Unparameterized, so Ominipg sends it together with BEGIN. FOR SHARE
825
- // waits for a newer owner's claim to settle, then sees it.
826
- const [fence] = await run(tx, `SELECT value FROM ${t("meta")} WHERE tenant = ${T} AND key = 'epoch' FOR SHARE`);
1001
+ transaction: (owner, work, options = {}) => options.maintenance && pool.server
1002
+ ? gate(true, () => maintained(owner, work))
1003
+ : gate(options.maintenance ?? false, async () => {
1004
+ const positions = options.positions ?? {};
1005
+ if (options.maintenance)
1006
+ await reachable(counted(committed));
1007
+ // The database transaction opens with the command's first statement. A
1008
+ // command that only records events (an action's completion, say) needs
1009
+ // none: its events go in as one fenced statement, atomic on its own.
1010
+ const emitted = [];
1011
+ let open;
1012
+ const begin = () => {
1013
+ if (closed)
1014
+ throw new Error("This store is closed.");
1015
+ if (!open) {
1016
+ const ready = Promise.withResolvers();
1017
+ const done = Promise.withResolvers();
1018
+ done.promise.catch(() => { });
1019
+ const settled = writer.transaction(async (tx) => {
1020
+ // Unparameterized, so Ominipg sends it together with BEGIN. FOR SHARE
1021
+ // waits for a newer owner's claim to settle, then sees it.
1022
+ const [fence] = await run(tx, `SELECT value FROM ${t("meta")} WHERE tenant = ${T} AND key = 'epoch' FOR SHARE`);
1023
+ if (Number(fence?.value) !== owner)
1024
+ throw fenced();
1025
+ ready.resolve(tx);
1026
+ await done.promise;
1027
+ });
1028
+ settled.catch((error) => ready.reject(error));
1029
+ ready.promise.catch(() => { });
1030
+ open = { tx: ready.promise, done, settled };
1031
+ }
1032
+ return open.tx;
1033
+ };
1034
+ const lazy = {
1035
+ query: async (sql, params) => (await begin()).query(sql, params),
1036
+ };
1037
+ const held = { docs: [], blobs: [] };
1038
+ // What the last statement inserts, to name a duplicate _id if it fails.
1039
+ let inserting = [];
1040
+ try {
1041
+ const scope = transaction(lazy, emitted, held, options.maintenance ?? false);
1042
+ const result = await work(scope);
1043
+ const events = unique(emitted);
1044
+ const writes = events.length || Object.keys(positions).length ||
1045
+ held.docs.length || held.blobs.length;
1046
+ inserting = held.docs;
1047
+ if (!open) {
1048
+ if (writes)
1049
+ return { result, events: await append(owner, events, positions, held) };
1050
+ // Nothing to write: still tell a fenced owner so.
1051
+ const [fence] = await run(writer, `SELECT value FROM ${t("meta")} WHERE tenant = ${T} AND key = 'epoch'`);
827
1052
  if (Number(fence?.value) !== owner)
828
1053
  throw fenced();
829
- ready.resolve(tx);
830
- await done.promise;
831
- });
832
- settled.catch((error) => ready.reject(error));
833
- ready.promise.catch(() => { });
834
- open = { tx: ready.promise, done, settled };
835
- }
836
- return open.tx;
837
- };
838
- const lazy = { query: async (sql, params) => (await begin()).query(sql, params) };
839
- const held = { docs: [], blobs: [] };
840
- // What the last statement inserts, to name a duplicate _id if it fails.
841
- let inserting = [];
842
- try {
843
- const scope = transaction(lazy, emitted, held);
844
- const result = await work(scope);
845
- const events = unique(emitted);
846
- const writes = events.length || Object.keys(positions).length ||
847
- held.docs.length || held.blobs.length;
848
- inserting = held.docs;
849
- if (!open) {
850
- if (writes)
851
- return { result, events: await append(owner, events, positions, held) };
852
- // Nothing to write: still tell a fenced owner so.
853
- const [fence] = await run(writer, `SELECT value FROM ${t("meta")} WHERE tenant = ${T} AND key = 'epoch'`);
854
- if (Number(fence?.value) !== owner)
855
- throw fenced();
856
- return { result, events: [] };
857
- }
858
- const tx = await open.tx;
859
- const finish = tx.finish;
860
- if (finish && writes) {
861
- // Ominipg 0.11.2+: the last statement and COMMIT go in one round trip.
862
- // It must carry no parameters, so its values are written as literals.
863
- const at = new Date().toISOString();
864
- const sql = final(events, positions, at, held);
865
- statements++;
866
- trace?.(sql);
867
- const { rows } = await finish.call(tx, sql);
1054
+ return { result, events: [] };
1055
+ }
1056
+ const tx = await open.tx;
1057
+ const finish = tx.finish;
1058
+ if (finish && writes) {
1059
+ // Ominipg 0.11.2+: the last statement and COMMIT go in one round trip.
1060
+ // It must carry no parameters, so its values are written as literals.
1061
+ const at = new Date().toISOString();
1062
+ const sql = final(events, positions, at, held);
1063
+ statements++;
1064
+ trace?.(sql);
1065
+ const { rows } = await finish.call(tx, sql);
1066
+ open.done.resolve();
1067
+ await open.settled;
1068
+ return {
1069
+ result,
1070
+ events: events.length
1071
+ ? committedEvents(events, at, rows)
1072
+ : [],
1073
+ };
1074
+ }
1075
+ await scope.flush();
1076
+ const recorded = await record(tx, events, positions);
868
1077
  open.done.resolve();
869
1078
  await open.settled;
870
- return {
871
- result,
872
- events: events.length
873
- ? committedEvents(events, at, rows)
874
- : [],
875
- };
1079
+ statements++; // COMMIT
1080
+ return { result, events: recorded };
876
1081
  }
877
- await scope.flush();
878
- const recorded = await record(tx, events, positions);
879
- open.done.resolve();
880
- await open.settled;
881
- statements++; // COMMIT
882
- return { result, events: recorded };
883
- }
884
- catch (error) {
885
- if (open) {
886
- open.done.reject(error);
887
- await open.settled.catch(() => { });
1082
+ catch (error) {
1083
+ if (open) {
1084
+ open.done.reject(error);
1085
+ await open.settled.catch(() => { });
1086
+ }
1087
+ throw await conflict(error, inserting);
888
1088
  }
889
- throw await conflict(error, inserting);
890
- }
891
- }),
1089
+ finally {
1090
+ if (options.maintenance)
1091
+ await discard();
1092
+ }
1093
+ }),
892
1094
  async index(collections) {
893
1095
  const wanted = new Map();
894
1096
  for (const [collection, specs] of Object.entries(collections)) {
@@ -973,17 +1175,82 @@ export async function postgresStore(options) {
973
1175
  }
974
1176
  known.set(key, signature);
975
1177
  },
976
- async inspect(sql) {
977
- // Any SQL can reach the tables, and so other tenants' rows: only in a schema of its own.
978
- if (await others(counted(committed), schema, tenant)) {
979
- throw Object.assign(new Error("This store shares its schema with other tenants: it can't be inspected."), { code: "shared_schema" });
1178
+ /**
1179
+ * One read-only statement: on a server on a connection of its own, as
1180
+ * the maintenance role, in a read-only transaction, closed after; in
1181
+ * PGlite on the store's session, which forgets it after. Always rolled
1182
+ * back. A query a cursor holds is fetched until its limit, or until its
1183
+ * rows pass `bytes` (the one that passes them included).
1184
+ */
1185
+ async inspect(sql, params = [], asked = {}) {
1186
+ const { text, keyword } = statement(sql, "postgres", "read");
1187
+ const { limit, bytes } = asked;
1188
+ const read = async (db) => {
1189
+ await db.query(`SELECT ${scoped(asked.timeout)}`);
1190
+ if ((limit === undefined && bytes === undefined) || !CURSORS.has(keyword)) {
1191
+ return upTo((await db.query(text, params)).rows, limit, bytes);
1192
+ }
1193
+ // A cursor fetches only what's asked for, however many rows the query has.
1194
+ await db.query(`DECLARE copilotz_inspect NO SCROLL CURSOR FOR ${text}`, params);
1195
+ const rows = [];
1196
+ let used = 0;
1197
+ for (;;) {
1198
+ // One row first, then as many as the bytes left look to hold (rows weigh alike, mostly).
1199
+ const room = bytes === undefined
1200
+ ? 100
1201
+ : !rows.length
1202
+ ? 1
1203
+ : Math.max(1, Math.floor((bytes - used) / (used / rows.length)));
1204
+ const left = Math.min(100, room, limit === undefined ? 100 : Math.floor(limit) - rows.length);
1205
+ if (left <= 0)
1206
+ return rows;
1207
+ const batch = (await db.query(`FETCH FORWARD ${left} FROM copilotz_inspect`)).rows;
1208
+ for (const row of batch) {
1209
+ used += weight(row);
1210
+ // A row past the bytes isn't kept: the read says it stopped there.
1211
+ if (bytes !== undefined && used > bytes) {
1212
+ return Object.assign(rows, { cut: true });
1213
+ }
1214
+ rows.push(row);
1215
+ }
1216
+ if (batch.length < left)
1217
+ return rows;
1218
+ }
1219
+ };
1220
+ try {
1221
+ if (pool.server) {
1222
+ return await dedicated(async (db) => {
1223
+ try {
1224
+ await reachable(db);
1225
+ await db.query("BEGIN READ ONLY");
1226
+ return await read(db);
1227
+ }
1228
+ finally {
1229
+ await db.query("ROLLBACK").catch(() => { });
1230
+ }
1231
+ });
1232
+ }
1233
+ await reachable(counted(committed));
1234
+ let rows = [];
1235
+ try {
1236
+ await committed.transaction(async (tx) => {
1237
+ rows = await read(tx);
1238
+ // Rolled back: whatever it set (a setting, for the session) goes with it.
1239
+ throw READ;
1240
+ }, { readOnly: true });
1241
+ }
1242
+ catch (error) {
1243
+ if (error !== READ)
1244
+ throw error;
1245
+ }
1246
+ finally {
1247
+ await discard();
1248
+ }
1249
+ return rows;
1250
+ }
1251
+ catch (error) {
1252
+ throw refused(error);
980
1253
  }
981
- return await committed.transaction(async (tx) => {
982
- // The views show this tenant, and unqualified names are the schema's.
983
- await tx.query(`SELECT set_config('copilotz.tenant', ${T}, true), set_config('search_path', ` +
984
- `${constant(`${ident(schema)}, public`)}, true)`);
985
- return (await tx.query(sql)).rows;
986
- }, { readOnly: true });
987
1254
  },
988
1255
  roundTrips: () => statements,
989
1256
  close,
@@ -1,3 +1,4 @@
1
+ import { isFieldName } from "../../../runtime/mongo.js";
1
2
  /**
2
3
  * The SQL for an update, when it only touches top-level fields with `$set`,
3
4
  * `$unset`, `$inc`, `$push`, `$addToSet` and `$pull` (applied in that order,
@@ -15,7 +16,8 @@ export function expression(update) {
15
16
  if (ops.some((op) => !supported.includes(op)))
16
17
  return undefined;
17
18
  const all = supported.flatMap(fields);
18
- if (all.some((field) => field.includes(".") || field.startsWith("$") || field === "_id")) {
19
+ // Paths, `_id`, and names no write may give (the reference refuses those) go to the reference.
20
+ if (all.some((field) => field.includes(".") || !isFieldName(field) || field === "_id")) {
19
21
  return undefined;
20
22
  }
21
23
  if (Object.values(u.$inc ?? {}).some((by) => typeof by !== "number"))
@@ -0,0 +1,2 @@
1
+ /** Runs work within `max` at once; `busy` past that. */
2
+ export declare function connections(max?: number): <R>(work: () => Promise<R>) => Promise<R>;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Maintenance statements at once: each holds a connection of its own (a
3
+ * PostgreSQL session, a SQLite process), so a store runs at most so many,
4
+ * and refuses another past that (`busy`) rather than queue it.
5
+ */
6
+ import { failure } from "../../../runtime/errors.js";
7
+ /** Runs work within `max` at once; `busy` past that. */
8
+ export function connections(max = 2) {
9
+ let open = 0;
10
+ return async (work) => {
11
+ if (open >= max) {
12
+ throw failure("busy", `Maintenance is running ${open} statement${open === 1 ? "" : "s"} already: try again.`);
13
+ }
14
+ open++;
15
+ try {
16
+ return await work();
17
+ }
18
+ finally {
19
+ open--;
20
+ }
21
+ };
22
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * What a maintenance read's rows weigh, as JSON: bytes as base64, big
3
+ * integers as text (as the maintenance endpoints answer them). A read stops
4
+ * fetching before a row would take it past the bytes it may answer with.
5
+ */
6
+ /** About how many bytes of JSON `row` comes to. */
7
+ export declare const weight: (row: unknown) => number;
8
+ /**
9
+ * At most `limit` rows, and only those within `bytes`: `cut` when a row
10
+ * past them was left out (more may follow).
11
+ */
12
+ export declare function upTo<T>(rows: T[], limit?: number, bytes?: number): T[] & {
13
+ cut?: true;
14
+ };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * What a maintenance read's rows weigh, as JSON: bytes as base64, big
3
+ * integers as text (as the maintenance endpoints answer them). A read stops
4
+ * fetching before a row would take it past the bytes it may answer with.
5
+ */
6
+ const encoder = new TextEncoder();
7
+ const plain = (_key, value) => typeof value === "bigint"
8
+ ? value.toString()
9
+ : value instanceof Uint8Array
10
+ ? "x".repeat(Math.ceil(value.length / 3) * 4)
11
+ : value;
12
+ /** About how many bytes of JSON `row` comes to. */
13
+ export const weight = (row) => encoder.encode(JSON.stringify(row ?? null, plain)).length + 1;
14
+ /**
15
+ * At most `limit` rows, and only those within `bytes`: `cut` when a row
16
+ * past them was left out (more may follow).
17
+ */
18
+ export function upTo(rows, limit, bytes) {
19
+ const kept = limit === undefined ? rows : rows.slice(0, limit);
20
+ if (bytes === undefined)
21
+ return kept;
22
+ let used = 0;
23
+ for (const [i, row] of kept.entries()) {
24
+ used += weight(row);
25
+ if (used > bytes)
26
+ return Object.assign(kept.slice(0, i), { cut: true });
27
+ }
28
+ return kept;
29
+ }