@nylorun/runtime 0.13.0-beta → 0.15.0-beta

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 (309) hide show
  1. package/CHANGELOG.md +434 -0
  2. package/README.md +137 -41
  3. package/dist/adapters/execution/restate.d.ts +7 -2
  4. package/dist/adapters/execution/restate.js +40 -10
  5. package/dist/adapters/replication/pgoutput.d.ts +21 -0
  6. package/dist/adapters/replication/pgoutput.js +170 -0
  7. package/dist/adapters/streams/s2.js +37 -20
  8. package/dist/admin-openapi.json +570 -0
  9. package/dist/api/a2a/card.d.ts +15 -0
  10. package/dist/api/a2a/card.js +24 -0
  11. package/dist/api/a2a/endpoint.d.ts +8 -0
  12. package/dist/api/a2a/endpoint.js +81 -0
  13. package/dist/api/a2a/protocol.d.ts +144 -0
  14. package/dist/api/a2a/protocol.js +188 -0
  15. package/dist/api/a2a/routes.d.ts +19 -0
  16. package/dist/api/a2a/routes.js +289 -0
  17. package/dist/api/a2a/tasks.d.ts +50 -0
  18. package/dist/api/a2a/tasks.js +186 -0
  19. package/dist/api/ag-ui/endpoint.d.ts +8 -0
  20. package/dist/api/ag-ui/endpoint.js +126 -0
  21. package/dist/api/ag-ui/history.d.ts +7 -0
  22. package/dist/api/ag-ui/history.js +60 -0
  23. package/dist/api/ag-ui/routes.d.ts +32 -0
  24. package/dist/api/ag-ui/routes.js +305 -0
  25. package/dist/api/ag-ui/session-id.d.ts +1 -0
  26. package/dist/api/ag-ui/session-id.js +12 -0
  27. package/dist/api/ag-ui/sse.d.ts +9 -0
  28. package/dist/api/ag-ui/sse.js +7 -0
  29. package/dist/api/ag-ui/translate.d.ts +38 -0
  30. package/dist/api/ag-ui/translate.js +222 -0
  31. package/dist/api/components.d.ts +8065 -0
  32. package/dist/api/components.js +82 -0
  33. package/dist/api/gate/keys.d.ts +14 -0
  34. package/dist/api/gate/keys.js +59 -0
  35. package/dist/api/gate/routes.d.ts +34 -0
  36. package/dist/api/gate/routes.js +235 -0
  37. package/dist/api/http/app.d.ts +18 -0
  38. package/dist/api/http/app.js +58 -0
  39. package/dist/api/http/body.d.ts +4 -0
  40. package/dist/api/http/body.js +37 -0
  41. package/dist/api/http/define.d.ts +39 -0
  42. package/dist/api/http/define.js +122 -0
  43. package/dist/api/http/respond.d.ts +9 -0
  44. package/dist/api/http/respond.js +44 -0
  45. package/dist/api/http/routes/access.d.ts +24 -0
  46. package/dist/api/http/routes/access.js +266 -0
  47. package/dist/api/http/routes/actions.d.ts +8 -0
  48. package/dist/api/http/routes/actions.js +76 -0
  49. package/dist/api/http/routes/endpoints.d.ts +7 -0
  50. package/dist/api/http/routes/endpoints.js +100 -0
  51. package/dist/api/http/routes/sessions.d.ts +3 -0
  52. package/dist/api/http/routes/sessions.js +205 -0
  53. package/dist/api/http/routes/tenant.d.ts +11 -0
  54. package/dist/api/http/routes/tenant.js +244 -0
  55. package/dist/api/http/routes/vaults.d.ts +8 -0
  56. package/dist/api/http/routes/vaults.js +167 -0
  57. package/dist/api/openapi.d.ts +19 -0
  58. package/dist/api/openapi.js +137 -0
  59. package/dist/api/route.d.ts +12 -0
  60. package/dist/api/route.js +4 -0
  61. package/dist/core/bearer.d.ts +8 -0
  62. package/dist/core/bearer.js +11 -0
  63. package/dist/core/flow-host.d.ts +7 -15
  64. package/dist/core/flow-host.js +16 -41
  65. package/dist/core/sandbox-routes.d.ts +12 -7
  66. package/dist/core/sandbox-routes.js +24 -25
  67. package/dist/execution/memory.d.ts +8 -5
  68. package/dist/execution/memory.js +33 -13
  69. package/dist/execution/types.d.ts +38 -6
  70. package/dist/execution/types.js +12 -3
  71. package/dist/gates/contract.d.ts +51 -0
  72. package/dist/gates/contract.js +37 -0
  73. package/dist/gates/handler.d.ts +21 -0
  74. package/dist/gates/handler.js +55 -0
  75. package/dist/gates/http-client.d.ts +12 -0
  76. package/dist/gates/http-client.js +160 -0
  77. package/dist/gates/in-process.d.ts +32 -0
  78. package/dist/gates/in-process.js +42 -0
  79. package/dist/gates/inflight.d.ts +39 -0
  80. package/dist/gates/inflight.js +70 -0
  81. package/dist/gates/mcp-handler.d.ts +35 -0
  82. package/dist/gates/mcp-handler.js +142 -0
  83. package/dist/gates/meter.d.ts +24 -0
  84. package/dist/gates/meter.js +165 -0
  85. package/dist/gates/model-gate.d.ts +45 -0
  86. package/dist/gates/model-gate.js +1 -0
  87. package/dist/gates/tenant-vaults.d.ts +64 -0
  88. package/dist/gates/tenant-vaults.js +94 -0
  89. package/dist/gates/tool-calls.d.ts +51 -0
  90. package/dist/gates/tool-calls.js +64 -0
  91. package/dist/gates/tool-client.d.ts +12 -0
  92. package/dist/gates/tool-client.js +201 -0
  93. package/dist/gates/tool-contract.d.ts +116 -0
  94. package/dist/gates/tool-contract.js +64 -0
  95. package/dist/gates/tool-gate.d.ts +45 -0
  96. package/dist/gates/tool-gate.js +10 -0
  97. package/dist/host/admin-api.d.ts +23 -0
  98. package/dist/host/admin-api.js +74 -0
  99. package/dist/host/app.d.ts +34 -0
  100. package/dist/host/app.js +181 -0
  101. package/dist/host/config-for.d.ts +3 -1
  102. package/dist/host/config-for.js +3 -2
  103. package/dist/host/config.d.ts +11 -0
  104. package/dist/host/cors.d.ts +27 -0
  105. package/dist/host/cors.js +64 -0
  106. package/dist/host/create-host.d.ts +36 -8
  107. package/dist/host/create-host.js +161 -330
  108. package/dist/host/execution.d.ts +20 -18
  109. package/dist/host/execution.js +2 -2
  110. package/dist/host/gates.d.ts +58 -0
  111. package/dist/host/gates.js +127 -0
  112. package/dist/host/http.d.ts +20 -4
  113. package/dist/host/http.js +81 -23
  114. package/dist/host/index.d.ts +1 -1
  115. package/dist/host/main.js +189 -58
  116. package/dist/host/stack-config.d.ts +91 -19
  117. package/dist/host/stack-config.js +246 -17
  118. package/dist/infra/database.d.ts +2 -2
  119. package/dist/infra/execution.d.ts +5 -5
  120. package/dist/infra/execution.js +8 -6
  121. package/dist/infra/index.d.ts +1 -1
  122. package/dist/infra/workers.d.ts +17 -17
  123. package/dist/infra/workers.js +9 -9
  124. package/dist/keys/client.d.ts +9 -0
  125. package/dist/keys/client.js +70 -0
  126. package/dist/keys/contract.d.ts +24 -0
  127. package/dist/keys/contract.js +12 -0
  128. package/dist/keys/keys.d.ts +41 -0
  129. package/dist/keys/keys.js +50 -0
  130. package/dist/mcp/connect.d.ts +43 -3
  131. package/dist/mcp/connect.js +12 -5
  132. package/dist/mcp/pool.d.ts +39 -3
  133. package/dist/mcp/pool.js +74 -11
  134. package/dist/model/catalog.d.ts +2 -10
  135. package/dist/model/classify.d.ts +13 -0
  136. package/dist/model/classify.js +61 -0
  137. package/dist/model/models.d.ts +10 -1
  138. package/dist/model/models.js +14 -6
  139. package/dist/model/pi-model.d.ts +17 -2
  140. package/dist/model/pi-model.js +366 -222
  141. package/dist/openapi.json +25889 -0
  142. package/dist/record/append.d.ts +51 -0
  143. package/dist/record/append.js +20 -0
  144. package/dist/record/envelope.d.ts +19 -0
  145. package/dist/record/envelope.js +49 -0
  146. package/dist/record/index.d.ts +11 -0
  147. package/dist/record/index.js +11 -0
  148. package/dist/sandbox/manager.d.ts +13 -1
  149. package/dist/sandbox/manager.js +8 -11
  150. package/dist/sandbox/resolve.d.ts +33 -0
  151. package/dist/sandbox/resolve.js +81 -0
  152. package/dist/sandbox/session-sandbox.d.ts +11 -0
  153. package/dist/sandbox/session-sandbox.js +51 -0
  154. package/dist/sandbox/share.d.ts +14 -2
  155. package/dist/sandbox/share.js +33 -3
  156. package/dist/sandbox/tenant-config.d.ts +37 -0
  157. package/dist/sandbox/tenant-config.js +51 -0
  158. package/dist/sandbox/tools.d.ts +7 -9
  159. package/dist/sandbox/tools.js +11 -3
  160. package/dist/sandbox/types.d.ts +8 -2
  161. package/dist/store/postgres/connect.d.ts +25 -9
  162. package/dist/store/postgres/connect.js +27 -10
  163. package/dist/store/postgres/db.d.ts +16 -0
  164. package/dist/store/postgres/db.js +28 -0
  165. package/dist/store/postgres/drizzle/0000_foundation.sql +22 -0
  166. package/dist/store/postgres/drizzle/0001_baseline.sql +255 -0
  167. package/dist/store/postgres/drizzle/0002_stream_relay.sql +4 -0
  168. package/dist/store/postgres/drizzle/0003_tool_crossings.sql +9 -0
  169. package/dist/store/postgres/drizzle/meta/_journal.json +34 -0
  170. package/dist/store/postgres/migrate.d.ts +68 -0
  171. package/dist/store/postgres/migrate.js +183 -0
  172. package/dist/store/postgres/record-writer.d.ts +4 -0
  173. package/dist/store/postgres/record-writer.js +34 -0
  174. package/dist/store/postgres/record.d.ts +5 -0
  175. package/dist/store/postgres/record.js +60 -0
  176. package/dist/store/postgres/schema.d.ts +3096 -0
  177. package/dist/store/postgres/schema.js +363 -0
  178. package/dist/store/postgres/store.d.ts +2 -4
  179. package/dist/store/postgres/store.js +590 -434
  180. package/dist/store/postgres/tenant.d.ts +56 -0
  181. package/dist/store/postgres/tenant.js +154 -0
  182. package/dist/store/types.d.ts +183 -132
  183. package/dist/store/types.js +0 -1
  184. package/dist/streams/basin.d.ts +18 -1
  185. package/dist/streams/basin.js +34 -2
  186. package/dist/streams/control.d.ts +18 -0
  187. package/dist/streams/control.js +33 -0
  188. package/dist/streams/memory.d.ts +3 -1
  189. package/dist/streams/memory.js +14 -8
  190. package/dist/streams/relay/core.d.ts +35 -0
  191. package/dist/streams/relay/core.js +366 -0
  192. package/dist/streams/relay/types.d.ts +78 -0
  193. package/dist/streams/relay/types.js +8 -0
  194. package/dist/streams/types.d.ts +71 -36
  195. package/dist/streams/types.js +18 -53
  196. package/dist/tenant/access-policy.d.ts +30 -0
  197. package/dist/tenant/access-policy.js +52 -0
  198. package/dist/tenant/actions.d.ts +10 -149
  199. package/dist/tenant/actions.js +5 -167
  200. package/dist/tenant/advance.d.ts +7 -2
  201. package/dist/tenant/advance.js +125 -14
  202. package/dist/tenant/auth.d.ts +19 -17
  203. package/dist/tenant/auth.js +90 -93
  204. package/dist/tenant/browser.d.ts +15 -0
  205. package/dist/tenant/browser.js +39 -0
  206. package/dist/tenant/cause.d.ts +25 -0
  207. package/dist/tenant/cause.js +45 -0
  208. package/dist/tenant/commands.d.ts +18 -2
  209. package/dist/tenant/commands.js +111 -115
  210. package/dist/tenant/context.d.ts +101 -21
  211. package/dist/tenant/context.js +14 -10
  212. package/dist/tenant/delivery-token.d.ts +41 -0
  213. package/dist/tenant/delivery-token.js +104 -0
  214. package/dist/tenant/delivery.d.ts +62 -0
  215. package/dist/tenant/delivery.js +481 -0
  216. package/dist/tenant/effects.d.ts +20 -1
  217. package/dist/tenant/effects.js +159 -42
  218. package/dist/tenant/endpoints.d.ts +67 -0
  219. package/dist/tenant/endpoints.js +90 -0
  220. package/dist/tenant/ephemeral.d.ts +22 -7
  221. package/dist/tenant/ephemeral.js +83 -125
  222. package/dist/tenant/history.d.ts +64 -0
  223. package/dist/tenant/history.js +120 -0
  224. package/dist/tenant/http.d.ts +8 -9
  225. package/dist/tenant/http.js +13 -27
  226. package/dist/tenant/jwt.d.ts +16 -0
  227. package/dist/tenant/jwt.js +50 -0
  228. package/dist/tenant/model-setting.d.ts +2 -2
  229. package/dist/tenant/module.d.ts +17 -28
  230. package/dist/tenant/module.js +133 -279
  231. package/dist/tenant/outbound.d.ts +44 -0
  232. package/dist/tenant/outbound.js +127 -0
  233. package/dist/tenant/paths.d.ts +11 -16
  234. package/dist/tenant/paths.js +24 -68
  235. package/dist/tenant/pool.d.ts +1 -3
  236. package/dist/tenant/pool.js +1 -19
  237. package/dist/tenant/principals.d.ts +23 -11
  238. package/dist/tenant/principals.js +61 -49
  239. package/dist/tenant/reset.d.ts +0 -4
  240. package/dist/tenant/reset.js +0 -5
  241. package/dist/tenant/runtime.d.ts +28 -20
  242. package/dist/tenant/runtime.js +95 -64
  243. package/dist/tenant/scheduler.d.ts +10 -3
  244. package/dist/tenant/scheduler.js +9 -5
  245. package/dist/tenant/session-streams.d.ts +127 -0
  246. package/dist/tenant/session-streams.js +404 -0
  247. package/dist/tenant/sessions.d.ts +28 -4
  248. package/dist/tenant/sessions.js +118 -24
  249. package/dist/tenant/signing-keys.d.ts +55 -0
  250. package/dist/tenant/signing-keys.js +172 -0
  251. package/dist/tenant/slim.d.ts +8 -0
  252. package/dist/tenant/slim.js +21 -0
  253. package/dist/tenant/status.d.ts +3 -3
  254. package/dist/tenant/status.js +20 -13
  255. package/dist/tenant/store-pg.d.ts +9 -6
  256. package/dist/tenant/store-pg.js +60 -125
  257. package/dist/tenant/streams.d.ts +62 -85
  258. package/dist/tenant/streams.js +143 -226
  259. package/dist/tenant/subject-limits.d.ts +16 -0
  260. package/dist/tenant/subject-limits.js +45 -0
  261. package/dist/tenant/sweep.d.ts +1 -10
  262. package/dist/tenant/sweep.js +29 -69
  263. package/dist/tenant/tokens.d.ts +14 -0
  264. package/dist/tenant/tokens.js +180 -0
  265. package/dist/tenant/transcript.d.ts +48 -5
  266. package/dist/tenant/transcript.js +58 -1
  267. package/dist/tenant/types.d.ts +69 -98
  268. package/dist/tenant/types.js +4 -15
  269. package/dist/tenant/worker.d.ts +3 -1
  270. package/dist/tenant/worker.js +13 -7
  271. package/dist/vault/host-model.d.ts +31 -0
  272. package/dist/vault/host-model.js +57 -0
  273. package/dist/vault/service.d.ts +44 -17
  274. package/dist/vault/service.js +126 -121
  275. package/dist/version.d.ts +1 -1
  276. package/dist/version.js +1 -1
  277. package/package.json +25 -9
  278. package/dist/core/executors.d.ts +0 -44
  279. package/dist/core/executors.js +0 -95
  280. package/dist/store/memory.d.ts +0 -57
  281. package/dist/store/memory.js +0 -645
  282. package/dist/store/postgres/migrations/001_initial.d.ts +0 -27
  283. package/dist/store/postgres/migrations/001_initial.js +0 -196
  284. package/dist/store/postgres/migrations/002_session_owner.d.ts +0 -6
  285. package/dist/store/postgres/migrations/002_session_owner.js +0 -13
  286. package/dist/store/postgres/migrations/index.d.ts +0 -49
  287. package/dist/store/postgres/migrations/index.js +0 -76
  288. package/dist/store/postgres/names.d.ts +0 -11
  289. package/dist/store/postgres/names.js +0 -42
  290. package/dist/store/postgres/tenants.d.ts +0 -90
  291. package/dist/store/postgres/tenants.js +0 -214
  292. package/dist/streams/relay.d.ts +0 -37
  293. package/dist/streams/relay.js +0 -254
  294. package/dist/tenant/envelope.d.ts +0 -7
  295. package/dist/tenant/envelope.js +0 -22
  296. package/dist/tenant/live.d.ts +0 -92
  297. package/dist/tenant/live.js +0 -290
  298. package/dist/tenant/quarantine-error.d.ts +0 -6
  299. package/dist/tenant/quarantine-error.js +0 -9
  300. package/dist/tenant/quarantine.d.ts +0 -27
  301. package/dist/tenant/quarantine.js +0 -72
  302. package/dist/tenant/routes-tenant.d.ts +0 -12
  303. package/dist/tenant/routes-tenant.js +0 -137
  304. package/dist/tenant/routes.d.ts +0 -9
  305. package/dist/tenant/routes.js +0 -151
  306. package/dist/tenant/store-memory.d.ts +0 -16
  307. package/dist/tenant/store-memory.js +0 -111
  308. /package/dist/{store → record}/cursor.d.ts +0 -0
  309. /package/dist/{store → record}/cursor.js +0 -0
@@ -4,7 +4,8 @@
4
4
  */
5
5
  import type { SandboxNetworkPreset } from "@nylorun/core/define";
6
6
  export type SandboxBackendName = "virtual";
7
- export type SandboxIsolation = "process";
7
+ /** What separates a sandbox from its host: the Runtime process, or a container boundary. */
8
+ export type SandboxIsolation = "process" | "container";
8
9
  export interface SandboxProbe {
9
10
  readonly name: SandboxBackendName;
10
11
  readonly available: boolean;
@@ -24,7 +25,8 @@ export interface ResolvedNetwork {
24
25
  export interface SandboxSpec {
25
26
  /** Stable backend name for this sandbox; unique per Runtime scope and session. */
26
27
  readonly key: string;
27
- readonly image: string;
28
+ /** OCI image reference; undefined means the backend's default. */
29
+ readonly image?: string;
28
30
  readonly cpus: number;
29
31
  readonly memoryMiB: number;
30
32
  readonly network: ResolvedNetwork;
@@ -43,6 +45,10 @@ export interface ExecResult {
43
45
  readonly timedOut: boolean;
44
46
  }
45
47
  export interface SandboxHandle {
48
+ /** The directory tools run in and resolve relative paths against. Default: `/workspace`. */
49
+ readonly workspace?: string;
50
+ /** The backend had to create the sandbox afresh, so files from an earlier run are gone. */
51
+ readonly created?: boolean;
46
52
  exec(request: ExecRequest, signal: AbortSignal): Promise<ExecResult>;
47
53
  /** Returns undefined when the file does not exist. */
48
54
  readFile(path: string): Promise<string | undefined>;
@@ -9,17 +9,33 @@ export interface PostgresClientOptions {
9
9
  connectTimeoutSeconds?: number;
10
10
  /** `application_name` on every connection. Default `nylorun-runtime`. */
11
11
  applicationName?: string;
12
+ /**
13
+ * `statement_timeout`: a statement running or waiting for a lock longer fails. Default
14
+ * 60 s; 0 turns it off. Migrations lift it (`lockMigrations` in `migrate.ts`).
15
+ */
16
+ statementTimeoutMs?: number;
17
+ /**
18
+ * `idle_in_transaction_session_timeout`: a transaction left idle this long is ended and its
19
+ * connection closed, releasing its locks. Default 60 s; 0 turns it off.
20
+ */
21
+ idleInTransactionTimeoutMs?: number;
12
22
  }
13
23
  /**
14
- * Opens a connection pool for Session Stores and the Tenant catalog. One pool
15
- * serves every Tenant schema of a Host; stores never end it, so the caller
16
- * calls `client.end()` at shutdown. This module and its siblings are the only
17
- * code that imports the Postgres driver (seam rule 3).
24
+ * Opens a connection pool on the Tenant's database, for its Session Store and the Tenant
25
+ * bootstrap (`tenant.ts`). Stores never end it, so the caller calls `client.end()` at
26
+ * shutdown. This module and its siblings are the only code that imports the Postgres driver
27
+ * and Drizzle (seam rule 3, `scripts/check-boundaries.mjs`); Drizzle runs on this pool
28
+ * (`db.ts`).
18
29
  *
19
- * Statements are not prepared (`prepare: false`). Every statement names its
20
- * Tenant's schema, so the same query is a different statement per Tenant, and
21
- * named prepared statements would pile up on every pooled connection with the
22
- * number of Tenants a Host serves. Unnamed statements still go in one round
23
- * trip; the queries are simple enough that re-planning them costs little.
30
+ * Statements are prepared: one database holds one Tenant in fixed schemas, so every statement
31
+ * is the same for the whole pool (session-store.md §2).
32
+ *
33
+ * The pool serves every request of the Host, so a statement or transaction that hangs must
34
+ * not hold a connection forever: both are bounded by default.
24
35
  */
25
36
  export declare function createPostgresClient(url: string, options?: PostgresClientOptions): PostgresClient;
37
+ /**
38
+ * Throws, naming the setting, when Postgres cannot run the stream relay: logical decoding
39
+ * needs `wal_level = logical` (a restart) and a role allowed to replicate.
40
+ */
41
+ export declare function assertLogicalReplication(sql: Sql): Promise<void>;
@@ -1,25 +1,42 @@
1
1
  import postgres, {} from "postgres";
2
2
  /**
3
- * Opens a connection pool for Session Stores and the Tenant catalog. One pool
4
- * serves every Tenant schema of a Host; stores never end it, so the caller
5
- * calls `client.end()` at shutdown. This module and its siblings are the only
6
- * code that imports the Postgres driver (seam rule 3).
3
+ * Opens a connection pool on the Tenant's database, for its Session Store and the Tenant
4
+ * bootstrap (`tenant.ts`). Stores never end it, so the caller calls `client.end()` at
5
+ * shutdown. This module and its siblings are the only code that imports the Postgres driver
6
+ * and Drizzle (seam rule 3, `scripts/check-boundaries.mjs`); Drizzle runs on this pool
7
+ * (`db.ts`).
7
8
  *
8
- * Statements are not prepared (`prepare: false`). Every statement names its
9
- * Tenant's schema, so the same query is a different statement per Tenant, and
10
- * named prepared statements would pile up on every pooled connection with the
11
- * number of Tenants a Host serves. Unnamed statements still go in one round
12
- * trip; the queries are simple enough that re-planning them costs little.
9
+ * Statements are prepared: one database holds one Tenant in fixed schemas, so every statement
10
+ * is the same for the whole pool (session-store.md §2).
11
+ *
12
+ * The pool serves every request of the Host, so a statement or transaction that hangs must
13
+ * not hold a connection forever: both are bounded by default.
13
14
  */
14
15
  export function createPostgresClient(url, options = {}) {
15
16
  return postgres(url, {
16
17
  max: options.max ?? 10,
17
18
  idle_timeout: options.idleTimeoutSeconds ?? 30,
18
19
  connect_timeout: options.connectTimeoutSeconds ?? 10,
19
- prepare: false,
20
20
  onnotice: () => { },
21
21
  connection: {
22
22
  application_name: options.applicationName ?? "nylorun-runtime",
23
+ statement_timeout: options.statementTimeoutMs ?? 60_000,
24
+ idle_in_transaction_session_timeout: options.idleInTransactionTimeoutMs ?? 60_000,
23
25
  },
24
26
  });
25
27
  }
28
+ /**
29
+ * Throws, naming the setting, when Postgres cannot run the stream relay: logical decoding
30
+ * needs `wal_level = logical` (a restart) and a role allowed to replicate.
31
+ */
32
+ export async function assertLogicalReplication(sql) {
33
+ const [row] = await sql `
34
+ SELECT current_setting('wal_level') AS wal_level,
35
+ (SELECT rolreplication OR rolsuper FROM pg_roles WHERE rolname = current_user) AS replicates`;
36
+ if (row?.wal_level !== "logical")
37
+ throw new Error(`Postgres has wal_level = ${row?.wal_level ?? "unknown"}; the stream relay needs logical replication. ` +
38
+ "Set wal_level = logical and restart Postgres (`nylorun start` does this for the local stack; " +
39
+ "on a managed Postgres, turn on its logical replication option). See DEPLOYMENT.md.");
40
+ if (!row.replicates)
41
+ throw new Error("The Runtime's Postgres role cannot replicate: grant it REPLICATION (ALTER ROLE … REPLICATION). See DEPLOYMENT.md.");
42
+ }
@@ -0,0 +1,16 @@
1
+ import { type PostgresJsDatabase, type PostgresJsQueryResultHKT } from "drizzle-orm/postgres-js";
2
+ import type { PgDatabase } from "drizzle-orm/pg-core";
3
+ import type { Sql } from "postgres";
4
+ export type Database = PostgresJsDatabase<Record<string, never>>;
5
+ /** A database or one of its transactions: anything a query can run on. */
6
+ export type Queryable = PgDatabase<PostgresJsQueryResultHKT, Record<string, never>>;
7
+ /** A transaction of `Database` (`db.transaction`). */
8
+ export type Transaction = Parameters<Parameters<Database["transaction"]>[0]>[0];
9
+ /** The Drizzle database on `sql`. */
10
+ export declare function database(sql: Sql): Database;
11
+ /**
12
+ * The driver's error behind a Drizzle query error. Drizzle wraps every failed query in a
13
+ * `DrizzleQueryError` whose message holds the query and its parameters (secrets among them);
14
+ * the store surfaces the postgres.js error with its SQLSTATE `code`, as callers expect.
15
+ */
16
+ export declare function driverError(error: unknown): unknown;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Drizzle on the Tenant database's pool (session-store.md §3): `drizzle-orm/postgres-js` over
3
+ * the postgres.js pool `connect.ts` opens, with the snake_case column names `schema.ts`
4
+ * defines. One Drizzle instance per pool.
5
+ *
6
+ * Drizzle hands timestamps to its own column types: on the pool it is given, postgres.js stops
7
+ * parsing date and time values, so a raw query on that pool reads a `timestamptz` as text.
8
+ */
9
+ import { DrizzleQueryError } from "drizzle-orm";
10
+ import { drizzle, } from "drizzle-orm/postgres-js";
11
+ const databases = new WeakMap();
12
+ /** The Drizzle database on `sql`. */
13
+ export function database(sql) {
14
+ let db = databases.get(sql);
15
+ if (!db) {
16
+ db = drizzle({ client: sql, casing: "snake_case" });
17
+ databases.set(sql, db);
18
+ }
19
+ return db;
20
+ }
21
+ /**
22
+ * The driver's error behind a Drizzle query error. Drizzle wraps every failed query in a
23
+ * `DrizzleQueryError` whose message holds the query and its parameters (secrets among them);
24
+ * the store surfaces the postgres.js error with its SQLSTATE `code`, as callers expect.
25
+ */
26
+ export function driverError(error) {
27
+ return error instanceof DrizzleQueryError && error.cause !== undefined ? error.cause : error;
28
+ }
@@ -0,0 +1,22 @@
1
+ -- Custom SQL (drizzle-kit does not model it): the two schemas and `nylorun.doc(body)`, which
2
+ -- the generated columns of the baseline (0001) read. Its snapshot declares the schemas, so
3
+ -- the baseline does not create them again.
4
+ --
5
+ -- doc(body) casts a `json` body to `jsonb` for the generated columns. `jsonb` rejects the
6
+ -- escapes \u0000 and unpaired surrogates, which `json` keeps verbatim: they are replaced with
7
+ -- \ufffd first (the stored body is untouched). A body with no \u escape takes the plain cast.
8
+ CREATE SCHEMA IF NOT EXISTS nylorun;
9
+ --> statement-breakpoint
10
+ CREATE SCHEMA nylorun_streams;
11
+ --> statement-breakpoint
12
+ CREATE FUNCTION nylorun.doc(body json) RETURNS jsonb
13
+ LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
14
+ SELECT CASE WHEN strpos(body::text, '\u') = 0 THEN body::jsonb
15
+ ELSE regexp_replace(
16
+ body::text,
17
+ '(?<!\\)((?:\\\\)*)\\u(0000|[dD][89a-fA-F][0-9a-fA-F]{2})',
18
+ '\1\\ufffd',
19
+ 'g'
20
+ )::jsonb
21
+ END
22
+ $$;
@@ -0,0 +1,255 @@
1
+ CREATE TABLE "nylorun"."actions" (
2
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
3
+ "body" json NOT NULL,
4
+ "session_id" text GENERATED ALWAYS AS (nylorun.doc(body)->>'sessionId') STORED,
5
+ "turn_id" text GENERATED ALWAYS AS (nylorun.doc(body)->>'turnId') STORED,
6
+ "agent_id" text GENERATED ALWAYS AS (nylorun.doc(body)->>'agentId') STORED,
7
+ "status" text GENERATED ALWAYS AS (nylorun.doc(body)->>'status') STORED,
8
+ "kind" text GENERATED ALWAYS AS (nylorun.doc(body)->>'kind') STORED,
9
+ "deadline_at" text GENERATED ALWAYS AS (nylorun.doc(body)->>'deadlineAt') STORED
10
+ );
11
+ --> statement-breakpoint
12
+ CREATE TABLE "nylorun"."commands" (
13
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
14
+ "body" json NOT NULL
15
+ );
16
+ --> statement-breakpoint
17
+ CREATE TABLE "nylorun"."definitions" (
18
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
19
+ "body" json NOT NULL
20
+ );
21
+ --> statement-breakpoint
22
+ CREATE TABLE "nylorun"."effects" (
23
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
24
+ "body" json NOT NULL,
25
+ "session_id" text GENERATED ALWAYS AS (nylorun.doc(body)->'request'->>'sessionId') STORED,
26
+ "turn_id" text GENERATED ALWAYS AS (nylorun.doc(body)->'request'->>'turnId') STORED,
27
+ "kind" text GENERATED ALWAYS AS (nylorun.doc(body)->'request'->>'kind') STORED,
28
+ "status" text GENERATED ALWAYS AS (nylorun.doc(body)->>'status') STORED
29
+ );
30
+ --> statement-breakpoint
31
+ CREATE TABLE "nylorun"."endpoints" (
32
+ "agent_id" text COLLATE "C" PRIMARY KEY NOT NULL,
33
+ "url" text NOT NULL,
34
+ "implementation_version" text NOT NULL,
35
+ "manifest_hash" text,
36
+ "timeout_ms" integer NOT NULL,
37
+ "max_concurrent" integer NOT NULL,
38
+ "principal_id" text,
39
+ "last_delivery_at" text,
40
+ "last_success_at" text,
41
+ "last_error_code" text,
42
+ "last_error_message" text,
43
+ "consecutive_failures" integer DEFAULT 0 NOT NULL,
44
+ "served_implementation_version" text,
45
+ "served_manifest_hash" text,
46
+ "created_at" text NOT NULL,
47
+ "updated_at" text NOT NULL
48
+ );
49
+ --> statement-breakpoint
50
+ CREATE TABLE "nylorun"."links" (
51
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
52
+ "body" json NOT NULL,
53
+ "workflow_session_id" text GENERATED ALWAYS AS (nylorun.doc(body)->>'workflowSessionId') STORED
54
+ );
55
+ --> statement-breakpoint
56
+ CREATE TABLE "nylorun"."model_budgets" (
57
+ "scope" text NOT NULL,
58
+ "scope_id" text COLLATE "C" NOT NULL,
59
+ "period" text,
60
+ "limit_usd" double precision,
61
+ "limit_tokens" bigint,
62
+ "updated_at" text NOT NULL,
63
+ CONSTRAINT "model_budgets_pkey" PRIMARY KEY("scope","scope_id")
64
+ );
65
+ --> statement-breakpoint
66
+ CREATE TABLE "nylorun"."model_usage" (
67
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
68
+ "effect_key" text COLLATE "C" NOT NULL,
69
+ "session_id" text COLLATE "C" NOT NULL,
70
+ "turn_id" text COLLATE "C" NOT NULL,
71
+ "agent_id" text COLLATE "C" NOT NULL,
72
+ "provider" text,
73
+ "model" text,
74
+ "input_tokens" integer NOT NULL,
75
+ "output_tokens" integer NOT NULL,
76
+ "total_tokens" integer NOT NULL,
77
+ "cached_tokens" integer NOT NULL,
78
+ "cache_write_tokens" integer NOT NULL,
79
+ "reasoning_tokens" integer NOT NULL,
80
+ "cost_usd" double precision NOT NULL,
81
+ "duplicate" boolean NOT NULL,
82
+ "created_at" text NOT NULL
83
+ );
84
+ --> statement-breakpoint
85
+ CREATE TABLE "nylorun"."principals" (
86
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
87
+ "role" text NOT NULL,
88
+ "token_hash" text NOT NULL,
89
+ "idempotency_key" text,
90
+ "created_at" text NOT NULL,
91
+ CONSTRAINT "principals_token_hash_key" UNIQUE("token_hash")
92
+ );
93
+ --> statement-breakpoint
94
+ CREATE TABLE "nylorun"."publishable_keys" (
95
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
96
+ "key" text COLLATE "C" NOT NULL,
97
+ "name" text NOT NULL,
98
+ "origins_json" text NOT NULL,
99
+ "created_at" text NOT NULL,
100
+ "revoked_at" text,
101
+ CONSTRAINT "publishable_keys_key_key" UNIQUE("key"),
102
+ CONSTRAINT "publishable_keys_name_key" UNIQUE("name")
103
+ );
104
+ --> statement-breakpoint
105
+ CREATE TABLE "nylorun_streams"."relay_slots" (
106
+ "slot_name" text PRIMARY KEY NOT NULL,
107
+ "reconcile_pending" boolean NOT NULL
108
+ );
109
+ --> statement-breakpoint
110
+ CREATE TABLE "nylorun"."sandboxes" (
111
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
112
+ "body" json NOT NULL
113
+ );
114
+ --> statement-breakpoint
115
+ CREATE TABLE "nylorun_streams"."session_events" (
116
+ "session_id" text COLLATE "C" NOT NULL,
117
+ "seq" bigint NOT NULL,
118
+ "generation" integer NOT NULL,
119
+ "type" text NOT NULL,
120
+ "body" json NOT NULL,
121
+ "committed_at" timestamp with time zone DEFAULT now() NOT NULL,
122
+ CONSTRAINT "session_events_pkey" PRIMARY KEY("session_id","seq")
123
+ );
124
+ --> statement-breakpoint
125
+ CREATE TABLE "nylorun_streams"."session_log_heads" (
126
+ "session_id" text COLLATE "C" PRIMARY KEY NOT NULL,
127
+ "generation" integer NOT NULL,
128
+ "head" bigint DEFAULT 0 NOT NULL
129
+ );
130
+ --> statement-breakpoint
131
+ CREATE TABLE "nylorun"."sessions" (
132
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
133
+ "body" json NOT NULL,
134
+ "status" text GENERATED ALWAYS AS (nylorun.doc(body)->>'status') STORED,
135
+ "agent_id" text GENERATED ALWAYS AS (nylorun.doc(body)->>'agentId') STORED,
136
+ "owner" text,
137
+ "epoch" bigint DEFAULT 0 NOT NULL,
138
+ "owner_expires_at" timestamp with time zone,
139
+ "owner_user_id" text GENERATED ALWAYS AS (nylorun.doc(body)->>'ownerUserId') STORED
140
+ );
141
+ --> statement-breakpoint
142
+ CREATE TABLE "nylorun"."signing_keys" (
143
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
144
+ "state" text NOT NULL,
145
+ "alg" text NOT NULL,
146
+ "public_jwk" text NOT NULL,
147
+ "kek_id" text NOT NULL,
148
+ "nonce" "bytea" NOT NULL,
149
+ "ciphertext" "bytea" NOT NULL,
150
+ "wrapped_dek" "bytea" NOT NULL,
151
+ "created_at" text NOT NULL,
152
+ "activated_at" text,
153
+ "retired_at" text,
154
+ "revoked_at" text,
155
+ CONSTRAINT "signing_keys_state_check" CHECK (state IN ('standby', 'current', 'previous', 'revoked')),
156
+ CONSTRAINT "signing_keys_alg_check" CHECK (alg = 'ES256')
157
+ );
158
+ --> statement-breakpoint
159
+ CREATE TABLE "nylorun"."subject_epochs" (
160
+ "subject" text COLLATE "C" PRIMARY KEY NOT NULL,
161
+ "epoch" bigint NOT NULL,
162
+ "revoked_at" text NOT NULL
163
+ );
164
+ --> statement-breakpoint
165
+ CREATE TABLE "nylorun"."subject_usage" (
166
+ "subject" text COLLATE "C" PRIMARY KEY NOT NULL,
167
+ "turn_tokens" double precision NOT NULL,
168
+ "refilled_at" text NOT NULL
169
+ );
170
+ --> statement-breakpoint
171
+ CREATE TABLE "nylorun"."tenant" (
172
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
173
+ "name" text NOT NULL,
174
+ "created_at" text NOT NULL,
175
+ "updated_at" text NOT NULL,
176
+ "schema_version" integer NOT NULL,
177
+ "singleton" boolean DEFAULT true NOT NULL,
178
+ "basin_generation" integer DEFAULT 0 NOT NULL,
179
+ "retired_generations" integer[] DEFAULT '{}' NOT NULL,
180
+ CONSTRAINT "tenant_singleton_key" UNIQUE("singleton"),
181
+ CONSTRAINT "tenant_singleton_check" CHECK (singleton)
182
+ );
183
+ --> statement-breakpoint
184
+ CREATE TABLE "nylorun"."tenant_settings" (
185
+ "key" text PRIMARY KEY NOT NULL,
186
+ "value" text NOT NULL
187
+ );
188
+ --> statement-breakpoint
189
+ CREATE TABLE "nylorun"."vault_audit" (
190
+ "ord" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "nylorun"."vault_audit_ord_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
191
+ "id" text NOT NULL,
192
+ "at" text NOT NULL,
193
+ "actor" text NOT NULL,
194
+ "action" text NOT NULL,
195
+ "vault_id" text,
196
+ "credential_id" text,
197
+ "session_id" text,
198
+ "target" text,
199
+ "outcome" text NOT NULL,
200
+ CONSTRAINT "vault_audit_id_key" UNIQUE("id")
201
+ );
202
+ --> statement-breakpoint
203
+ CREATE TABLE "nylorun"."vault_credentials" (
204
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
205
+ "vault_id" text NOT NULL,
206
+ "name" text NOT NULL,
207
+ "type" text NOT NULL,
208
+ "binding_json" text NOT NULL,
209
+ "expires_at" text,
210
+ "created_at" text COLLATE "C" NOT NULL,
211
+ "rotated_at" text,
212
+ "kek_id" text NOT NULL,
213
+ "nonce" "bytea" NOT NULL,
214
+ "ciphertext" "bytea" NOT NULL,
215
+ "wrapped_dek" "bytea" NOT NULL
216
+ );
217
+ --> statement-breakpoint
218
+ CREATE TABLE "nylorun"."vault_idempotency" (
219
+ "id" text PRIMARY KEY NOT NULL,
220
+ "body_hash" text NOT NULL,
221
+ "response" text NOT NULL
222
+ );
223
+ --> statement-breakpoint
224
+ CREATE TABLE "nylorun"."vaults" (
225
+ "id" text COLLATE "C" PRIMARY KEY NOT NULL,
226
+ "name" text NOT NULL,
227
+ "owner_user_id" text NOT NULL,
228
+ "metadata_json" text,
229
+ "created_at" text COLLATE "C" NOT NULL,
230
+ "scope" text DEFAULT 'user' NOT NULL,
231
+ CONSTRAINT "vaults_scope_check" CHECK (scope IN ('user', 'host'))
232
+ );
233
+ --> statement-breakpoint
234
+ ALTER TABLE "nylorun"."vault_credentials" ADD CONSTRAINT "vault_credentials_vault_id_fkey" FOREIGN KEY ("vault_id") REFERENCES "nylorun"."vaults"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
235
+ CREATE INDEX "actions_session" ON "nylorun"."actions" USING btree ("session_id","turn_id","status");--> statement-breakpoint
236
+ CREATE INDEX "actions_agent" ON "nylorun"."actions" USING btree ("agent_id","status");--> statement-breakpoint
237
+ CREATE INDEX "actions_status" ON "nylorun"."actions" USING btree ("status","kind");--> statement-breakpoint
238
+ CREATE INDEX "actions_deadline" ON "nylorun"."actions" USING btree ("status","deadline_at");--> statement-breakpoint
239
+ CREATE INDEX "effects_session" ON "nylorun"."effects" USING btree ("session_id","turn_id","status");--> statement-breakpoint
240
+ CREATE INDEX "effects_status" ON "nylorun"."effects" USING btree ("status","kind");--> statement-breakpoint
241
+ CREATE INDEX "links_workflow" ON "nylorun"."links" USING btree ("workflow_session_id");--> statement-breakpoint
242
+ CREATE INDEX "model_usage_effect" ON "nylorun"."model_usage" USING btree ("effect_key");--> statement-breakpoint
243
+ CREATE INDEX "model_usage_agent" ON "nylorun"."model_usage" USING btree ("agent_id","created_at");--> statement-breakpoint
244
+ CREATE INDEX "model_usage_turn" ON "nylorun"."model_usage" USING btree ("turn_id");--> statement-breakpoint
245
+ CREATE INDEX "model_usage_created" ON "nylorun"."model_usage" USING btree ("created_at");--> statement-breakpoint
246
+ CREATE INDEX "sessions_status" ON "nylorun"."sessions" USING btree ("status","owner_expires_at");--> statement-breakpoint
247
+ CREATE INDEX "sessions_agent" ON "nylorun"."sessions" USING btree ("agent_id");--> statement-breakpoint
248
+ CREATE INDEX "sessions_owner_user" ON "nylorun"."sessions" USING btree ("owner_user_id");--> statement-breakpoint
249
+ CREATE UNIQUE INDEX "signing_keys_one_standby" ON "nylorun"."signing_keys" USING btree ("state") WHERE state = 'standby';--> statement-breakpoint
250
+ CREATE UNIQUE INDEX "signing_keys_one_current" ON "nylorun"."signing_keys" USING btree ("state") WHERE state = 'current';--> statement-breakpoint
251
+ CREATE UNIQUE INDEX "signing_keys_one_previous" ON "nylorun"."signing_keys" USING btree ("state") WHERE state = 'previous';--> statement-breakpoint
252
+ CREATE INDEX "vault_audit_vault" ON "nylorun"."vault_audit" USING btree ("vault_id","ord");--> statement-breakpoint
253
+ CREATE INDEX "vault_credentials_vault" ON "nylorun"."vault_credentials" USING btree ("vault_id","created_at","id");--> statement-breakpoint
254
+ CREATE INDEX "vaults_owner" ON "nylorun"."vaults" USING btree ("owner_user_id","created_at","id");--> statement-breakpoint
255
+ CREATE UNIQUE INDEX "vaults_one_host" ON "nylorun"."vaults" USING btree ("scope") WHERE scope = 'host';
@@ -0,0 +1,4 @@
1
+ -- Custom SQL (drizzle-kit does not model publications): the publication the stream relay
2
+ -- subscribes to (Durable Streams §7). Only inserts: the record is append-only.
3
+ CREATE PUBLICATION nylorun_stream_relay
4
+ FOR TABLE nylorun_streams.session_events WITH (publish = 'insert');
@@ -0,0 +1,9 @@
1
+ CREATE TABLE "nylorun"."tool_crossings" (
2
+ "key" text COLLATE "C" PRIMARY KEY NOT NULL,
3
+ "hash" text NOT NULL,
4
+ "started_at" text COLLATE "C" NOT NULL,
5
+ "settled_at" text COLLATE "C",
6
+ "answer" json
7
+ );
8
+ --> statement-breakpoint
9
+ CREATE INDEX "tool_crossings_settled" ON "nylorun"."tool_crossings" USING btree ("settled_at");
@@ -0,0 +1,34 @@
1
+ {
2
+ "version": "7",
3
+ "dialect": "postgresql",
4
+ "entries": [
5
+ {
6
+ "idx": 0,
7
+ "version": "7",
8
+ "when": 1790943952307,
9
+ "tag": "0000_foundation",
10
+ "breakpoints": true
11
+ },
12
+ {
13
+ "idx": 1,
14
+ "version": "7",
15
+ "when": 1790943980707,
16
+ "tag": "0001_baseline",
17
+ "breakpoints": true
18
+ },
19
+ {
20
+ "idx": 2,
21
+ "version": "7",
22
+ "when": 1790943989041,
23
+ "tag": "0002_stream_relay",
24
+ "breakpoints": true
25
+ },
26
+ {
27
+ "idx": 3,
28
+ "version": "7",
29
+ "when": 1790956444115,
30
+ "tag": "0003_tool_crossings",
31
+ "breakpoints": true
32
+ }
33
+ ]
34
+ }
@@ -0,0 +1,68 @@
1
+ import type { Sql } from "postgres";
2
+ import { type Queryable, type Transaction } from "./db.js";
3
+ /** Where the migrations are, next to this module (`src/` in development, `dist/` built). */
4
+ export declare const MIGRATIONS_FOLDER: string;
5
+ /** The journal of applied migrations: `nylorun.__drizzle_migrations`. */
6
+ export declare const MIGRATIONS_SCHEMA = "nylorun";
7
+ export declare const MIGRATIONS_TABLE = "__drizzle_migrations";
8
+ /**
9
+ * What schemas were called when one database held several Tenants (`"tenant_<id>"`). A
10
+ * database that still has one was written by an older Runtime and is refused.
11
+ */
12
+ export declare const OLD_TENANT_SCHEMA_PREFIX = "tenant_";
13
+ export interface Migration {
14
+ /** The journal tag, `0001_baseline`. */
15
+ tag: string;
16
+ /** SHA-256 of the SQL file, as Drizzle records it. */
17
+ hash: string;
18
+ /** The journal's `when` (ms), recorded as `created_at`. */
19
+ when: number;
20
+ /** The file's statements, split at `--> statement-breakpoint`. */
21
+ statements: readonly string[];
22
+ }
23
+ /** The migrations this Runtime ships, in order. */
24
+ export declare function shippedMigrations(): readonly Migration[];
25
+ /** The schema version this Runtime writes and expects: the number of shipped migrations. */
26
+ export declare function expectedSchemaVersion(): number;
27
+ /**
28
+ * Lifts `statement_timeout` for the transaction and takes the database's migration lock
29
+ * until it ends. Waiting for the lock, and the migrations done under it, may outlast the
30
+ * pool's statement timeout; a migration that timed out would fail the Host's readiness.
31
+ */
32
+ export declare function lockMigrations(tx: Transaction): Promise<void>;
33
+ /**
34
+ * Throws `database-layout-old` when the database was written by an older Runtime: one that
35
+ * kept several Tenants in one database (`tenant_<id>` schemas, a record keyed by Tenant), or a
36
+ * pre-release build of one Tenant per database whose hand-written migrations recorded
37
+ * themselves in `schema_version` tables.
38
+ */
39
+ export declare function assertCurrentLayout(q: Queryable): Promise<void>;
40
+ /**
41
+ * The database's schema version: the number of migrations its journal records, or undefined
42
+ * when it has no journal (a database never migrated).
43
+ */
44
+ export declare function readSchemaVersion(q: Queryable): Promise<number | undefined>;
45
+ /**
46
+ * Applies every migration of `migrations` the journal does not record, in `tx`. Take
47
+ * `lockMigrations` first (`migrateDatabase` does). Throws `schema-too-new` when the journal
48
+ * records a migration `migrations` does not have, and `migration-failed` when Postgres
49
+ * rejects a migration.
50
+ */
51
+ export declare function applyMigrations(tx: Transaction, migrations?: readonly Migration[]): Promise<{
52
+ from: number;
53
+ to: number;
54
+ }>;
55
+ /**
56
+ * Refuses an old layout and applies the missing migrations, in one transaction under the
57
+ * migration lock. For tests and tools; the Host migrates through `openTenantDatabase`.
58
+ */
59
+ export declare function migrateDatabase(pool: Sql, migrations?: readonly Migration[]): Promise<{
60
+ from: number;
61
+ to: number;
62
+ }>;
63
+ /**
64
+ * Whether Postgres rejected a statement (a SQLSTATE), rather than the connection or the
65
+ * server failing (classes 08, 53, 57, and the driver's own connection errors). Only the
66
+ * first says something about the Tenant's database.
67
+ */
68
+ export declare function isStatementError(error: unknown): boolean;