@capybara-hq/kernel 0.1.0-rc.142 → 0.1.0-rc.144

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 (238) hide show
  1. package/dist/admin/admin-agents.controller.d.ts +3 -5
  2. package/dist/admin/admin-agents.controller.js +1 -1
  3. package/dist/admin/admin-connectors.controller.d.ts +3 -5
  4. package/dist/admin/admin-connectors.controller.js +1 -1
  5. package/dist/admin/admin-items.schemas.d.ts +6 -6
  6. package/dist/admin/admin-items.schemas.js +5 -9
  7. package/dist/admin/admin.controller.d.ts +18 -12
  8. package/dist/admin/admin.controller.js +29 -15
  9. package/dist/admin/admin.schemas.d.ts +12 -3
  10. package/dist/admin/admin.schemas.js +17 -6
  11. package/dist/agents/admin-agents.d.ts +10 -8
  12. package/dist/agents/admin-agents.js +13 -9
  13. package/dist/agents/agents.module.js +3 -0
  14. package/dist/agents/agents.service.js +1 -1
  15. package/dist/agents/org-templates.d.ts +3 -1
  16. package/dist/agents/org-templates.js +7 -3
  17. package/dist/agents/pending-connector-requests.d.ts +10 -4
  18. package/dist/agents/pending-connector-requests.js +29 -7
  19. package/dist/automations/automations.controller.d.ts +9 -5
  20. package/dist/automations/automations.controller.js +21 -4
  21. package/dist/automations/automations.schemas.d.ts +19 -0
  22. package/dist/automations/automations.schemas.js +9 -0
  23. package/dist/automations/automations.service.d.ts +25 -8
  24. package/dist/automations/automations.service.js +40 -34
  25. package/dist/automations/events/acceptance.d.ts +6 -1
  26. package/dist/automations/events/acceptance.js +30 -11
  27. package/dist/automations/store.d.ts +17 -0
  28. package/dist/automations/store.js +35 -0
  29. package/dist/catalog/admin-catalog.controller.d.ts +64 -2
  30. package/dist/catalog/admin-catalog.controller.js +17 -0
  31. package/dist/catalog/catalog-import.module.js +3 -0
  32. package/dist/catalog/catalog-scope.service.d.ts +3 -1
  33. package/dist/catalog/catalog-scope.service.js +11 -7
  34. package/dist/catalog/catalog.module.js +3 -0
  35. package/dist/catalog/catalog.service.d.ts +3 -1
  36. package/dist/catalog/catalog.service.js +8 -4
  37. package/dist/catalog/cli.js +2 -2
  38. package/dist/catalog/component-promotion.d.ts +3 -1
  39. package/dist/catalog/component-promotion.js +7 -3
  40. package/dist/catalog/component-scope.service.d.ts +3 -1
  41. package/dist/catalog/component-scope.service.js +7 -3
  42. package/dist/catalog/enable-requests.d.ts +10 -8
  43. package/dist/catalog/enable-requests.js +40 -21
  44. package/dist/catalog/marketplaces.service.d.ts +1 -1
  45. package/dist/catalog/owned-items.d.ts +3 -1
  46. package/dist/catalog/owned-items.js +13 -9
  47. package/dist/catalog/platform-components.d.ts +6 -1
  48. package/dist/catalog/platform-components.js +9 -3
  49. package/dist/catalog/plugin-connectors.d.ts +8 -4
  50. package/dist/catalog/plugin-connectors.js +12 -6
  51. package/dist/catalog/plugin-import.d.ts +3 -1
  52. package/dist/catalog/plugin-import.js +14 -5
  53. package/dist/catalog/plugin-install.d.ts +3 -1
  54. package/dist/catalog/plugin-install.js +8 -4
  55. package/dist/catalog/plugin-package.js +2 -1
  56. package/dist/catalog/plugin-upgrade.d.ts +3 -1
  57. package/dist/catalog/plugin-upgrade.js +8 -4
  58. package/dist/catalog/request-hints.d.ts +19 -0
  59. package/dist/catalog/request-hints.js +34 -0
  60. package/dist/catalog/request-rules.d.ts +9 -5
  61. package/dist/catalog/request-rules.js +16 -9
  62. package/dist/catalog/requests.service.d.ts +3 -1
  63. package/dist/catalog/requests.service.js +28 -9
  64. package/dist/catalog/scope-orgs.d.ts +2 -1
  65. package/dist/catalog/scope-orgs.js +2 -2
  66. package/dist/catalog/scope-sync.d.ts +3 -1
  67. package/dist/catalog/scope-sync.js +12 -2
  68. package/dist/catalog/scope-writer.d.ts +6 -3
  69. package/dist/catalog/scope-writer.js +28 -10
  70. package/dist/catalog/unshare.d.ts +3 -2
  71. package/dist/catalog/unshare.js +4 -4
  72. package/dist/client/client-access.service.js +29 -1
  73. package/dist/config/schema.d.ts +12 -0
  74. package/dist/config/schema.js +56 -0
  75. package/dist/ctx/create-ctx.d.ts +2 -19
  76. package/dist/ctx/create-ctx.js +3 -93
  77. package/dist/ctx/durable-relay.d.ts +2 -5
  78. package/dist/ctx/durable-relay.js +56 -29
  79. package/dist/db/extension-handoff-schema.js +5 -0
  80. package/dist/db/kernel-schema.d.ts +74 -0
  81. package/dist/db/migrator.js +36 -0
  82. package/dist/erasure/account-batches.js +2 -0
  83. package/dist/erasure/account-residual.js +1 -0
  84. package/dist/erasure/org-catalog.js +8 -3
  85. package/dist/erasure/org-rules.js +1 -0
  86. package/dist/evals/evals.schemas.d.ts +15 -5
  87. package/dist/evals/evals.schemas.js +17 -10
  88. package/dist/evals/runs.controller.d.ts +4 -12
  89. package/dist/evals/runs.controller.js +14 -11
  90. package/dist/evals/runs.service.d.ts +29 -25
  91. package/dist/evals/runs.service.js +73 -61
  92. package/dist/evals/trajectory.controller.d.ts +10 -6
  93. package/dist/evals/trajectory.controller.js +11 -4
  94. package/dist/evals/trajectory.service.d.ts +17 -9
  95. package/dist/evals/trajectory.service.js +52 -35
  96. package/dist/extensions/admin-ops.d.ts +6 -1
  97. package/dist/extensions/admin-ops.js +21 -7
  98. package/dist/extensions/blobs.d.ts +2 -0
  99. package/dist/extensions/blobs.js +11 -0
  100. package/dist/extensions/deployment-config.d.ts +6 -1
  101. package/dist/extensions/deployment-config.js +13 -2
  102. package/dist/extensions/enabled.d.ts +5 -0
  103. package/dist/extensions/extensions.module.js +3 -0
  104. package/dist/extensions/grant-epoch.d.ts +108 -0
  105. package/dist/extensions/grant-epoch.js +214 -0
  106. package/dist/extensions/host.d.ts +29 -9
  107. package/dist/extensions/host.js +148 -42
  108. package/dist/extensions/instance.d.ts +3 -3
  109. package/dist/extensions/instance.js +1 -1
  110. package/dist/extensions/isolated/authorization.d.ts +54 -2
  111. package/dist/extensions/isolated/authorization.js +148 -35
  112. package/dist/extensions/isolated/child-session.js +0 -1
  113. package/dist/extensions/isolated/revision.js +1 -1
  114. package/dist/extensions/org-config.d.ts +3 -1
  115. package/dist/extensions/org-config.js +13 -4
  116. package/dist/extensions/org-overrides.d.ts +6 -1
  117. package/dist/extensions/org-overrides.js +13 -2
  118. package/dist/extensions/package-digest.job.d.ts +34 -0
  119. package/dist/extensions/package-digest.job.js +121 -0
  120. package/dist/extensions/package.js +10 -6
  121. package/dist/extensions/permission-set.d.ts +14 -78
  122. package/dist/extensions/permission-set.js +17 -143
  123. package/dist/extensions/permission-view.d.ts +13 -1
  124. package/dist/extensions/permission-view.js +20 -0
  125. package/dist/extensions/registry.js +4 -1
  126. package/dist/extensions/resolve.d.ts +7 -8
  127. package/dist/extensions/resolve.js +7 -8
  128. package/dist/extensions/revision-approvals.d.ts +167 -0
  129. package/dist/extensions/revision-approvals.js +353 -0
  130. package/dist/extensions/revision-grants.d.ts +197 -0
  131. package/dist/extensions/revision-grants.js +584 -0
  132. package/dist/extensions/state.module.js +12 -1
  133. package/dist/identity/accounts.d.ts +10 -4
  134. package/dist/identity/accounts.js +18 -5
  135. package/dist/inbox/inbox-items.service.d.ts +6 -1
  136. package/dist/inbox/inbox-items.service.js +29 -10
  137. package/dist/inbox/inbox.module.js +3 -0
  138. package/dist/inbox/inbox.service.d.ts +3 -1
  139. package/dist/inbox/inbox.service.js +9 -2
  140. package/dist/inbox/org-notices.d.ts +4 -3
  141. package/dist/inbox/org-notices.js +9 -6
  142. package/dist/inbox/platform-inbox.service.d.ts +3 -1
  143. package/dist/inbox/platform-inbox.service.js +11 -3
  144. package/dist/inbox/platform-notices.d.ts +3 -2
  145. package/dist/inbox/platform-notices.js +10 -4
  146. package/dist/infra/config/kernel-config.service.js +4 -1
  147. package/dist/infra/database/extension-effect-key-lock.js +7 -0
  148. package/dist/infra/paging/paged-read.d.ts +34 -4
  149. package/dist/infra/paging/paged-read.js +38 -1
  150. package/dist/invalidation/audiences.d.ts +32 -0
  151. package/dist/invalidation/audiences.js +43 -0
  152. package/dist/invalidation/hints.d.ts +6 -0
  153. package/dist/invalidation/hints.js +7 -1
  154. package/dist/invalidation/invalidation-hints.d.ts +12 -1
  155. package/dist/invalidation/invalidation-hints.js +46 -5
  156. package/dist/invalidation/invalidation-hints.module.d.ts +2 -0
  157. package/dist/invalidation/invalidation-hints.module.js +4 -1
  158. package/dist/invalidation/me-stream.d.ts +1 -11
  159. package/dist/invalidation/me-stream.js +12 -41
  160. package/dist/invalidation/org-intent-bridge.d.ts +18 -0
  161. package/dist/invalidation/org-intent-bridge.js +46 -0
  162. package/dist/models/enablement.d.ts +3 -1
  163. package/dist/models/enablement.js +25 -5
  164. package/dist/orgs/invitations.controller.d.ts +4 -7
  165. package/dist/orgs/invitations.controller.js +8 -5
  166. package/dist/orgs/invitations.service.d.ts +17 -3
  167. package/dist/orgs/invitations.service.js +35 -13
  168. package/dist/orgs/members.service.d.ts +35 -0
  169. package/dist/orgs/members.service.js +52 -3
  170. package/dist/orgs/offboarding.d.ts +3 -1
  171. package/dist/orgs/offboarding.js +7 -3
  172. package/dist/orgs/orgs.controller.d.ts +11 -4
  173. package/dist/orgs/orgs.controller.js +25 -5
  174. package/dist/orgs/orgs.module.js +3 -0
  175. package/dist/orgs/orgs.schemas.d.ts +14 -0
  176. package/dist/orgs/orgs.schemas.js +19 -2
  177. package/dist/orgs/orgs.service.d.ts +24 -2
  178. package/dist/orgs/orgs.service.js +47 -4
  179. package/dist/orgs/security-settings.service.d.ts +14 -6
  180. package/dist/orgs/security-settings.service.js +18 -13
  181. package/dist/orgs/settings.controller.d.ts +7 -4
  182. package/dist/platform/exclusions.js +15 -0
  183. package/dist/platform/ops/accounts.ops.js +7 -16
  184. package/dist/platform/ops/admin-items.ops.js +7 -7
  185. package/dist/platform/ops/automations.ops.js +6 -4
  186. package/dist/platform/ops/evals.ops.js +30 -15
  187. package/dist/platform/ops/org-settings.ops.js +2 -2
  188. package/dist/platform/ops/orgs.ops.js +44 -27
  189. package/dist/push/delivery.d.ts +3 -1
  190. package/dist/push/delivery.js +7 -3
  191. package/dist/push/push-processor.js +8 -1
  192. package/dist/roles/worker.module.js +3 -0
  193. package/dist/runtime/step-log.d.ts +1 -1
  194. package/dist/runtime/step-log.js +5 -4
  195. package/dist/runtime/stream-limit.d.ts +13 -0
  196. package/dist/runtime/stream-limit.js +40 -0
  197. package/dist/runtime/stream.controller.d.ts +2 -0
  198. package/dist/runtime/stream.controller.js +2 -0
  199. package/dist/runtime/stream.d.ts +1 -0
  200. package/dist/runtime/stream.js +15 -6
  201. package/dist/sandbox/permission-authority.module.js +6 -1
  202. package/dist/sandbox/permission-transitions.d.ts +3 -1
  203. package/dist/sandbox/permission-transitions.js +17 -2
  204. package/dist/setup/kernel-checks.d.ts +3 -3
  205. package/dist/setup/kernel-checks.js +4 -4
  206. package/dist/setup/setup-check-registry.d.ts +16 -5
  207. package/dist/setup/setup-check-registry.js +11 -3
  208. package/dist/setup/setup-checks.service.d.ts +1 -2
  209. package/dist/setup/setup-checks.service.js +7 -6
  210. package/dist/skills/agent-skills.service.d.ts +6 -1
  211. package/dist/skills/agent-skills.service.js +12 -3
  212. package/dist/skills/changes.service.d.ts +6 -1
  213. package/dist/skills/changes.service.js +11 -3
  214. package/dist/skills/conversation-catalog.d.ts +3 -0
  215. package/dist/skills/conversation-catalog.js +2 -0
  216. package/dist/skills/skills.module.js +3 -0
  217. package/dist/skills/skills.service.d.ts +3 -1
  218. package/dist/skills/skills.service.js +8 -4
  219. package/dist/tools/admin-connectors.d.ts +10 -8
  220. package/dist/tools/admin-connectors.js +12 -10
  221. package/dist/tools/approvals.service.d.ts +12 -1
  222. package/dist/tools/approvals.service.js +36 -9
  223. package/dist/tools/builtins/automations.d.ts +1 -1
  224. package/dist/tools/builtins/automations.js +1 -1
  225. package/dist/tools/connectors.service.d.ts +3 -1
  226. package/dist/tools/connectors.service.js +29 -7
  227. package/dist/tools/tools.module.js +3 -0
  228. package/dist/ui/admin-ui.controller.d.ts +3 -1
  229. package/dist/ui/admin-ui.controller.js +3 -1
  230. package/dist/ui/ui-stream.d.ts +2 -0
  231. package/dist/ui/ui-stream.js +20 -6
  232. package/dist/ui/views.controller.d.ts +4 -2
  233. package/dist/ui/views.controller.js +4 -2
  234. package/package.json +3 -3
  235. package/src/migrations/0234_extension_grant_epoch.sql +141 -0
  236. package/src/migrations/0235_extension_revision_grants.sql +166 -0
  237. package/src/migrations/0236_paged_table_idx.sql +33 -0
  238. package/src/migrations/0237_extension_handoff_health.sql +18 -0
@@ -2,6 +2,7 @@ import { type InboxPost, type LocalizedName, type LocalizedText } from "@capybar
2
2
  import type { StoredInboxAction } from "../db/kernel-schema.js";
3
3
  import { KernelDb } from "../infra/database/kernel-db.js";
4
4
  import { TransactionHost } from "../infra/database/transaction-host.js";
5
+ import { type HintPublisher } from "../invalidation/audiences.js";
5
6
  import { PushOwed } from "../push/push-owed.js";
6
7
  /** Who posts an item: the kernel, or an extension (by plugin name). */
7
8
  export type InboxSource = {
@@ -60,7 +61,11 @@ export declare class InboxItemsService {
60
61
  private readonly kdb;
61
62
  readonly txHost: TransactionHost;
62
63
  private readonly push;
63
- constructor(kdb: KernelDb, txHost: TransactionHost, push: Pick<PushOwed, "oweInboxItem">);
64
+ /** Every write publishes `inbox` to the item's person after the commit. */
65
+ private readonly hints;
66
+ constructor(kdb: KernelDb, txHost: TransactionHost, push: Pick<PushOwed, "oweInboxItem">,
67
+ /** Every write publishes `inbox` to the item's person after the commit. */
68
+ hints: HintPublisher);
64
69
  /**
65
70
  * Posts `p` for `source` and owes its push fanout in the same transaction. The SDK's
66
71
  * `validateInboxPost` checks the fields (a plugin item links only to `/p/<plugin>/…`; a kernel
@@ -16,6 +16,9 @@ import { isExtensionEnabled } from "../extensions/enabled.js";
16
16
  import { KernelDb } from "../infra/database/kernel-db.js";
17
17
  import { TransactionHost } from "../infra/database/transaction-host.js";
18
18
  import { Transactional } from "../infra/database/transactional.decorator.js";
19
+ import { publishAccountInbox } from "../invalidation/audiences.js";
20
+ import { InvalidationHints } from "../invalidation/invalidation-hints.js";
21
+ import { InvalidationHintsModule } from "../invalidation/invalidation-hints.module.js";
19
22
  import { PushOwed, PushOwedModule } from "../push/push-owed.js";
20
23
  import { pluginDisplayNames } from "../tools/display-names.js";
21
24
  import { INBOX_ITEMS_BACKEND } from "./token.js";
@@ -48,10 +51,14 @@ let InboxItemsService = class InboxItemsService {
48
51
  kdb;
49
52
  txHost;
50
53
  push;
51
- constructor(kdb, txHost, push) {
54
+ hints;
55
+ constructor(kdb, txHost, push,
56
+ /** Every write publishes `inbox` to the item's person after the commit. */
57
+ hints) {
52
58
  this.kdb = kdb;
53
59
  this.txHost = txHost;
54
60
  this.push = push;
61
+ this.hints = hints;
55
62
  }
56
63
  /**
57
64
  * Posts `p` for `source` and owes its push fanout in the same transaction. The SDK's
@@ -118,6 +125,7 @@ let InboxItemsService = class InboxItemsService {
118
125
  // the conflicting item may close between the insert and the read: then the insert runs again
119
126
  for (let attempt = 0; attempt < 2; attempt++) {
120
127
  if ((await insert()) !== undefined) {
128
+ publishAccountInbox(this.hints, [p.accountId]);
121
129
  if (!opts.resolved)
122
130
  await this.push.oweInboxItem(id, p.orgId);
123
131
  return { itemId: id };
@@ -178,7 +186,7 @@ let InboxItemsService = class InboxItemsService {
178
186
  }
179
187
  /** Resolves open posts of this exact plugin, recipient and post key. */
180
188
  async resolveForPostKey(plugin, orgId, accountId, postKey) {
181
- await this.kdb.db
189
+ const r = await this.kdb.db
182
190
  .updateTable("inbox_items")
183
191
  .set({ state: "resolved", resolved_at: sql `now()` })
184
192
  .where("org_id", "=", orgId)
@@ -187,7 +195,9 @@ let InboxItemsService = class InboxItemsService {
187
195
  .where("plugin", "=", plugin)
188
196
  .where("dedupe_key", "=", postKey)
189
197
  .where("state", "=", "open")
190
- .execute();
198
+ .executeTakeFirst();
199
+ if (r.numUpdatedRows > 0n)
200
+ publishAccountInbox(this.hints, [accountId]);
191
201
  }
192
202
  /** The open items of (org, account, source, plugin) with `dedupe`, closed as `expired`. */
193
203
  sameKey(orgId, accountId, source, dedupe) {
@@ -228,7 +238,7 @@ let InboxItemsService = class InboxItemsService {
228
238
  * scope.
229
239
  */
230
240
  async resolveKernelItem(orgId, accountId, dedupeKey) {
231
- await this.kdb.db
241
+ const r = await this.kdb.db
232
242
  .updateTable("inbox_items")
233
243
  .set({ state: "resolved", resolved_at: sql `now()` })
234
244
  .where("org_id", "=", orgId)
@@ -237,14 +247,16 @@ let InboxItemsService = class InboxItemsService {
237
247
  .where("plugin", "is", null)
238
248
  .where("dedupe_key", "=", dedupeKey)
239
249
  .where("state", "=", "open")
240
- .execute();
250
+ .executeTakeFirst();
251
+ if (r.numUpdatedRows > 0n)
252
+ publishAccountInbox(this.hints, [accountId]);
241
253
  }
242
254
  /**
243
255
  * Resolves every open kernel item of `accountId` in `orgId` whose dedupe key starts with
244
256
  * `prefix` (the things they asked about are gone). The caller runs it in the org's scope.
245
257
  */
246
258
  async resolveKernelItems(orgId, accountId, prefix) {
247
- await this.kdb.db
259
+ const r = await this.kdb.db
248
260
  .updateTable("inbox_items")
249
261
  .set({ state: "resolved", resolved_at: sql `now()` })
250
262
  .where("org_id", "=", orgId)
@@ -253,7 +265,9 @@ let InboxItemsService = class InboxItemsService {
253
265
  .where("plugin", "is", null)
254
266
  .where(sql `starts_with(dedupe_key, ${prefix})`)
255
267
  .where("state", "=", "open")
256
- .execute();
268
+ .executeTakeFirst();
269
+ if (r.numUpdatedRows > 0n)
270
+ publishAccountInbox(this.hints, [accountId]);
257
271
  }
258
272
  /** Marks an open item resolved; whether it was open. */
259
273
  async close(orgId, itemId) {
@@ -263,8 +277,12 @@ let InboxItemsService = class InboxItemsService {
263
277
  .where("org_id", "=", orgId)
264
278
  .where("id", "=", itemId)
265
279
  .where("state", "=", "open")
280
+ .returning("account_id")
266
281
  .executeTakeFirst();
267
- return r.numUpdatedRows > 0n;
282
+ if (r === undefined)
283
+ return false;
284
+ publishAccountInbox(this.hints, [r.account_id]);
285
+ return true;
268
286
  }
269
287
  /** The open, unexpired item `itemId` of `accountId` in `orgId`, or `inbox.item_not_found`. */
270
288
  async openOf(orgId, accountId, itemId) {
@@ -375,7 +393,8 @@ InboxItemsService = __decorate([
375
393
  Injectable(),
376
394
  __param(0, Inject(KernelDb)),
377
395
  __param(1, Inject(TransactionHost)),
378
- __param(2, Inject(PushOwed))
396
+ __param(2, Inject(PushOwed)),
397
+ __param(3, Inject(InvalidationHints))
379
398
  ], InboxItemsService);
380
399
  export { InboxItemsService };
381
400
  /**
@@ -387,7 +406,7 @@ let InboxItemsModule = class InboxItemsModule {
387
406
  };
388
407
  InboxItemsModule = __decorate([
389
408
  Module({
390
- imports: [PushOwedModule],
409
+ imports: [PushOwedModule, InvalidationHintsModule],
391
410
  providers: [InboxItemsService, { provide: INBOX_ITEMS_BACKEND, useExisting: InboxItemsService }],
392
411
  exports: [InboxItemsService, INBOX_ITEMS_BACKEND],
393
412
  })
@@ -7,6 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  import { Module } from "@nestjs/common";
8
8
  import { AutomationsModule } from "../automations/automations.module.js";
9
9
  import { CatalogModule } from "../catalog/catalog.module.js";
10
+ import { InvalidationHintsModule } from "../invalidation/invalidation-hints.module.js";
10
11
  import { SkillsModule } from "../skills/skills.module.js";
11
12
  import { ToolsModule } from "../tools/tools.module.js";
12
13
  import { UiModule } from "../ui/ui.module.js";
@@ -24,6 +25,8 @@ let InboxModule = class InboxModule {
24
25
  InboxModule = __decorate([
25
26
  Module({
26
27
  imports: [
28
+ // the notice dismissals publish invalidation hints after their commit
29
+ InvalidationHintsModule,
27
30
  ToolsModule,
28
31
  SkillsModule,
29
32
  CatalogModule,
@@ -2,6 +2,7 @@ import { type AutomationAttention, AutomationsService } from "../automations/aut
2
2
  import { AcceptanceService, type AutomationOfferView } from "../automations/events/acceptance.js";
3
3
  import { CatalogRequestsService, type CatalogRequestView } from "../catalog/requests.service.js";
4
4
  import { KernelDb } from "../infra/database/kernel-db.js";
5
+ import { type HintPublisher } from "../invalidation/audiences.js";
5
6
  import { SkillChangesService, type SkillChangeView } from "../skills/changes.service.js";
6
7
  import { ApprovalsService, type ApprovalView } from "../tools/approvals.service.js";
7
8
  import { InboxItemsService, type PersonalInboxItemView } from "./inbox-items.service.js";
@@ -109,7 +110,8 @@ export declare class InboxService {
109
110
  private readonly automations;
110
111
  private readonly items;
111
112
  private readonly acceptance;
112
- constructor(approvals: Pick<ApprovalsService, "list">, skillChanges: Pick<SkillChangesService, "listPending">, kdb: KernelDb, catalogRequests: Pick<CatalogRequestsService, "listForOrgAdmin" | "listDecidedForRequester">, automations: Pick<AutomationsService, "attentionFor">, items: Pick<InboxItemsService, "listOpen">, acceptance: Pick<AcceptanceService, "openOffers">);
113
+ private readonly hints;
114
+ constructor(approvals: Pick<ApprovalsService, "list">, skillChanges: Pick<SkillChangesService, "listPending">, kdb: KernelDb, catalogRequests: Pick<CatalogRequestsService, "listForOrgAdmin" | "listDecidedForRequester">, automations: Pick<AutomationsService, "attentionFor">, items: Pick<InboxItemsService, "listOpen">, acceptance: Pick<AcceptanceService, "openOffers">, hints: HintPublisher);
113
115
  list(orgId: string, accountId: string): Promise<InboxView>;
114
116
  /** The org's open notices when the account is an org admin (else none), newest first. */
115
117
  private notices;
@@ -14,6 +14,8 @@ import { AcceptanceService, OFFERS_LIST_MAX, } from "../automations/events/accep
14
14
  import { CATALOG_REQUESTS_LIST_MAX, CatalogRequestsService, } from "../catalog/requests.service.js";
15
15
  import { ApiError } from "../errors.js";
16
16
  import { KernelDb } from "../infra/database/kernel-db.js";
17
+ import { publishOrgAdminsInbox } from "../invalidation/audiences.js";
18
+ import { InvalidationHints } from "../invalidation/invalidation-hints.js";
17
19
  import { roleAtLeast } from "../orgs/roles.js";
18
20
  import { SKILL_CHANGES_LIST_MAX, SkillChangesService, } from "../skills/changes.service.js";
19
21
  import { APPROVALS_LIST_MAX, ApprovalsService, } from "../tools/approvals.service.js";
@@ -31,7 +33,8 @@ let InboxService = class InboxService {
31
33
  automations;
32
34
  items;
33
35
  acceptance;
34
- constructor(approvals, skillChanges, kdb, catalogRequests, automations, items, acceptance) {
36
+ hints;
37
+ constructor(approvals, skillChanges, kdb, catalogRequests, automations, items, acceptance, hints) {
35
38
  this.approvals = approvals;
36
39
  this.skillChanges = skillChanges;
37
40
  this.kdb = kdb;
@@ -39,6 +42,7 @@ let InboxService = class InboxService {
39
42
  this.automations = automations;
40
43
  this.items = items;
41
44
  this.acceptance = acceptance;
45
+ this.hints = hints;
42
46
  }
43
47
  async list(orgId, accountId) {
44
48
  const [approvals, changes, requests, decided, notices, attention, allItems, offers] = await Promise.all([
@@ -152,6 +156,8 @@ let InboxService = class InboxService {
152
156
  .executeTakeFirst();
153
157
  if (r.numUpdatedRows === 0n)
154
158
  throw notFound;
159
+ // dismissed for every admin of the org: their inboxes refetch
160
+ publishOrgAdminsInbox(this.hints, [orgId]);
155
161
  }
156
162
  async isAdmin(orgId, accountId) {
157
163
  const m = await this.kdb.db
@@ -171,7 +177,8 @@ InboxService = __decorate([
171
177
  __param(3, Inject(CatalogRequestsService)),
172
178
  __param(4, Inject(AutomationsService)),
173
179
  __param(5, Inject(InboxItemsService)),
174
- __param(6, Inject(AcceptanceService))
180
+ __param(6, Inject(AcceptanceService)),
181
+ __param(7, Inject(InvalidationHints))
175
182
  ], InboxService);
176
183
  export { InboxService };
177
184
  const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
@@ -1,5 +1,6 @@
1
1
  import { type OwnedRef, type Owner } from "../catalog/ownership.js";
2
2
  import type { KernelSchemaDb, OrgNoticeKind } from "../db/kernel-schema.js";
3
+ import { type HintPublisher } from "../invalidation/audiences.js";
3
4
  export type { OrgNoticeKind };
4
5
  /** The `detail` of each notice kind. */
5
6
  export interface OrgNoticeDetails {
@@ -31,18 +32,18 @@ type LocalizedName = {
31
32
  /** A catalog item's display name for an upload notice (null without one, or for an unknown id). */
32
33
  export declare function itemDisplayName(db: KernelSchemaDb, itemId: string): Promise<LocalizedName>;
33
34
  /** Writes an org notice in the caller's transaction. */
34
- export declare function addOrgNotice<K extends keyof OrgNoticeDetails>(db: KernelSchemaDb, orgId: string, kind: K, detail: OrgNoticeDetails[K]): Promise<void>;
35
+ export declare function addOrgNotice<K extends keyof OrgNoticeDetails>(db: KernelSchemaDb, hints: HintPublisher, orgId: string, kind: K, detail: OrgNoticeDetails[K]): Promise<void>;
35
36
  /**
36
37
  * Tells the owner org that its item was shared (`item_shared`): with each org of `orgs` (the owner
37
38
  * itself is skipped), or with `all_orgs`. A platform-owned item writes nothing.
38
39
  */
39
- export declare function notifyItemShared(db: KernelSchemaDb, ownerOrgId: string | null, ref: OwnedRef, name: string, orgs: readonly string[] | "all_orgs"): Promise<void>;
40
+ export declare function notifyItemShared(db: KernelSchemaDb, hints: HintPublisher, ownerOrgId: string | null, ref: OwnedRef, name: string, orgs: readonly string[] | "all_orgs"): Promise<void>;
40
41
  /**
41
42
  * Tells every live org the org-owned item is on in, except its owner (P3-18, {@link orgsUsing}),
42
43
  * that it changed version (`shared_item_updated`); returns how many orgs were told. A platform-owned
43
44
  * or missing item tells nobody.
44
45
  */
45
- export declare function notifySharedUpdate(db: KernelSchemaDb, ref: OwnedRef, change: {
46
+ export declare function notifySharedUpdate(db: KernelSchemaDb, hints: HintPublisher, ref: OwnedRef, change: {
46
47
  name: string;
47
48
  from: string;
48
49
  to: string;
@@ -4,6 +4,7 @@
4
4
  import { randomUUID } from "node:crypto";
5
5
  import { orgsUsing, ownerOf, ownersOf } from "../catalog/ownership.js";
6
6
  import { withOrgScope } from "../infra/database/org-scope.js";
7
+ import { publishOrgAdminsInbox } from "../invalidation/audiences.js";
7
8
  /** An owned ref's display name: a plugin's (skills and agent templates have none). */
8
9
  const refDisplayName = (db, ref) => ref.kind === "plugin" ? itemDisplayName(db, ref.id) : Promise.resolve(null);
9
10
  /** A catalog item's display name for an upload notice (null without one, or for an unknown id). */
@@ -16,11 +17,13 @@ export async function itemDisplayName(db, itemId) {
16
17
  return row?.display_name ?? null;
17
18
  }
18
19
  /** Writes an org notice in the caller's transaction. */
19
- export async function addOrgNotice(db, orgId, kind, detail) {
20
+ export async function addOrgNotice(db, hints, orgId, kind, detail) {
20
21
  await db
21
22
  .insertInto("org_notices")
22
23
  .values({ id: randomUUID(), org_id: orgId, kind, detail: JSON.stringify(detail) })
23
24
  .execute();
25
+ // the org's admins see it in their inbox (account invalidation stream spec §4)
26
+ publishOrgAdminsInbox(hints, [orgId]);
24
27
  }
25
28
  const OWNER_TABLE = {
26
29
  plugin: "catalog_items",
@@ -31,12 +34,12 @@ const OWNER_TABLE = {
31
34
  * Tells the owner org that its item was shared (`item_shared`): with each org of `orgs` (the owner
32
35
  * itself is skipped), or with `all_orgs`. A platform-owned item writes nothing.
33
36
  */
34
- export async function notifyItemShared(db, ownerOrgId, ref, name, orgs) {
37
+ export async function notifyItemShared(db, hints, ownerOrgId, ref, name, orgs) {
35
38
  if (ownerOrgId === null)
36
39
  return;
37
40
  const displayName = await refDisplayName(db, ref);
38
41
  if (orgs === "all_orgs") {
39
- await addOrgNotice(db, ownerOrgId, "item_shared", {
42
+ await addOrgNotice(db, hints, ownerOrgId, "item_shared", {
40
43
  ref,
41
44
  name,
42
45
  displayName,
@@ -54,7 +57,7 @@ export async function notifyItemShared(db, ownerOrgId, ref, name, orgs) {
54
57
  .execute();
55
58
  const names = new Map(rows.map((r) => [r.id, r.name]));
56
59
  for (const id of targets) {
57
- await addOrgNotice(db, ownerOrgId, "item_shared", {
60
+ await addOrgNotice(db, hints, ownerOrgId, "item_shared", {
58
61
  ref,
59
62
  name,
60
63
  displayName,
@@ -67,7 +70,7 @@ export async function notifyItemShared(db, ownerOrgId, ref, name, orgs) {
67
70
  * that it changed version (`shared_item_updated`); returns how many orgs were told. A platform-owned
68
71
  * or missing item tells nobody.
69
72
  */
70
- export async function notifySharedUpdate(db, ref, change) {
73
+ export async function notifySharedUpdate(db, hints, ref, change) {
71
74
  const row = await db
72
75
  .selectFrom(OWNER_TABLE[ref.kind])
73
76
  .select("owner_org_id")
@@ -79,7 +82,7 @@ export async function notifySharedUpdate(db, ref, change) {
79
82
  const using = await orgsUsing(db, ref, { except: row.owner_org_id });
80
83
  const displayName = await refDisplayName(db, ref);
81
84
  for (const o of using) {
82
- await withOrgScope(o.id, () => addOrgNotice(db, o.id, "shared_item_updated", {
85
+ await withOrgScope(o.id, () => addOrgNotice(db, hints, o.id, "shared_item_updated", {
83
86
  ref,
84
87
  name: change.name,
85
88
  displayName,
@@ -3,6 +3,7 @@ import { type CatalogItemView, CatalogService } from "../catalog/catalog.service
3
3
  import { CatalogRequestsService, type CatalogRequestView } from "../catalog/requests.service.js";
4
4
  import type { ScopeSyncOp } from "../db/kernel-schema.js";
5
5
  import { KernelDb } from "../infra/database/kernel-db.js";
6
+ import type { HintPublisher } from "../invalidation/audiences.js";
6
7
  import type { PlatformNoticeDetails, PlatformNoticeKind } from "./platform-notices.js";
7
8
  /** The most items one platform inbox read returns. */
8
9
  export declare const PLATFORM_INBOX_MAX_ITEMS = 100;
@@ -131,7 +132,8 @@ export declare class PlatformInboxService {
131
132
  private readonly requests;
132
133
  private readonly catalog;
133
134
  private readonly kdb;
134
- constructor(requests: Pick<CatalogRequestsService, "listDeployment" | "countPendingDeployment">, catalog: Pick<CatalogService, "list">, kdb: KernelDb);
135
+ private readonly hints;
136
+ constructor(requests: Pick<CatalogRequestsService, "listDeployment" | "countPendingDeployment">, catalog: Pick<CatalogService, "list">, kdb: KernelDb, hints: HintPublisher);
135
137
  list(): Promise<PlatformInboxView>;
136
138
  /**
137
139
  * How many items wait (the sidebar's count): the same sources, counted where they can be (the
@@ -13,6 +13,8 @@ import { CatalogService } from "../catalog/catalog.service.js";
13
13
  import { CatalogRequestsService } from "../catalog/requests.service.js";
14
14
  import { ApiError } from "../errors.js";
15
15
  import { KernelDb } from "../infra/database/kernel-db.js";
16
+ import { PLATFORM_ADMINS_TOPIC } from "../invalidation/hints.js";
17
+ import { InvalidationHints } from "../invalidation/invalidation-hints.js";
16
18
  /** The most items one platform inbox read returns. */
17
19
  export const PLATFORM_INBOX_MAX_ITEMS = 100;
18
20
  /** The most open scope sync failures one read takes. */
@@ -36,10 +38,12 @@ let PlatformInboxService = class PlatformInboxService {
36
38
  requests;
37
39
  catalog;
38
40
  kdb;
39
- constructor(requests, catalog, kdb) {
41
+ hints;
42
+ constructor(requests, catalog, kdb, hints) {
40
43
  this.requests = requests;
41
44
  this.catalog = catalog;
42
45
  this.kdb = kdb;
46
+ this.hints = hints;
43
47
  }
44
48
  async list() {
45
49
  const [requests, requestCount, pending, failures, failureCount, disabled, disabledCount, notices, noticeCount,] = await Promise.all([
@@ -172,8 +176,11 @@ let PlatformInboxService = class PlatformInboxService {
172
176
  .where("id", "=", id)
173
177
  .where("dismissed_at", "is", null)
174
178
  .executeTakeFirst();
175
- if (r.numUpdatedRows > 0n)
179
+ if (r.numUpdatedRows > 0n) {
180
+ // dismissed for every super admin: their platform inboxes refetch
181
+ this.hints.publishAfterCommit([PLATFORM_ADMINS_TOPIC], { kind: "platform-inbox" });
176
182
  return;
183
+ }
177
184
  const exists = await this.kdb.db
178
185
  .selectFrom("platform_notices")
179
186
  .select("id")
@@ -257,7 +264,8 @@ PlatformInboxService = __decorate([
257
264
  Injectable(),
258
265
  __param(0, Inject(CatalogRequestsService)),
259
266
  __param(1, Inject(CatalogService)),
260
- __param(2, Inject(KernelDb))
267
+ __param(2, Inject(KernelDb)),
268
+ __param(3, Inject(InvalidationHints))
261
269
  ], PlatformInboxService);
262
270
  export { PlatformInboxService };
263
271
  /**
@@ -1,5 +1,6 @@
1
1
  import type { OwnedRef } from "../catalog/ownership.js";
2
2
  import type { KernelSchemaDb } from "../db/kernel-schema.js";
3
+ import type { HintPublisher } from "../invalidation/audiences.js";
3
4
  export type PlatformNoticeKind = "ownership_transferred" | "push_auth_failed";
4
5
  /** The `detail` of each platform notice kind. */
5
6
  export interface PlatformNoticeDetails {
@@ -23,11 +24,11 @@ export interface PlatformNoticeDetails {
23
24
  plugin: string;
24
25
  };
25
26
  }
26
- export declare function addPlatformNotice<K extends PlatformNoticeKind>(db: KernelSchemaDb, kind: K, detail: PlatformNoticeDetails[K]): Promise<void>;
27
+ export declare function addPlatformNotice<K extends PlatformNoticeKind>(db: KernelSchemaDb, hints: HintPublisher, kind: K, detail: PlatformNoticeDetails[K]): Promise<void>;
27
28
  /**
28
29
  * Tells the super admins that `plugin`'s push credentials were refused, unless a notice of that
29
30
  * kind naming the plugin is still open. The partial unique index
30
31
  * `platform_notices_push_auth_open_idx` (migration 0173) keeps it at one open notice per plugin
31
32
  * even when deliveries on several replicas fail at once.
32
33
  */
33
- export declare function addPushAuthNotice(db: KernelSchemaDb, plugin: string): Promise<void>;
34
+ export declare function addPushAuthNotice(db: KernelSchemaDb, hints: HintPublisher, plugin: string): Promise<void>;
@@ -2,11 +2,13 @@
2
2
  // are told, until any one of them dismisses it. Written in the caller's transaction.
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { sql } from "kysely";
5
- export async function addPlatformNotice(db, kind, detail) {
5
+ import { PLATFORM_ADMINS_TOPIC } from "../invalidation/hints.js";
6
+ export async function addPlatformNotice(db, hints, kind, detail) {
6
7
  await db
7
8
  .insertInto("platform_notices")
8
9
  .values({ id: randomUUID(), kind, detail: JSON.stringify(detail) })
9
10
  .execute();
11
+ hints.publishAfterCommit([PLATFORM_ADMINS_TOPIC], { kind: "platform-inbox" });
10
12
  }
11
13
  /**
12
14
  * Tells the super admins that `plugin`'s push credentials were refused, unless a notice of that
@@ -14,9 +16,9 @@ export async function addPlatformNotice(db, kind, detail) {
14
16
  * `platform_notices_push_auth_open_idx` (migration 0173) keeps it at one open notice per plugin
15
17
  * even when deliveries on several replicas fail at once.
16
18
  */
17
- export async function addPushAuthNotice(db, plugin) {
19
+ export async function addPushAuthNotice(db, hints, plugin) {
18
20
  const detail = { plugin };
19
- await db
21
+ const inserted = await db
20
22
  .insertInto("platform_notices")
21
23
  .values({ id: randomUUID(), kind: "push_auth_failed", detail: JSON.stringify(detail) })
22
24
  .onConflict((oc) => oc
@@ -25,5 +27,9 @@ export async function addPushAuthNotice(db, plugin) {
25
27
  .where("kind", "=", sql.lit("push_auth_failed"))
26
28
  .where("dismissed_at", "is", null)
27
29
  .doNothing())
28
- .execute();
30
+ .returning("id")
31
+ .executeTakeFirst();
32
+ if (inserted !== undefined) {
33
+ hints.publishAfterCommit([PLATFORM_ADMINS_TOPIC], { kind: "platform-inbox" });
34
+ }
29
35
  }
@@ -25,8 +25,11 @@ const HOT_APPLIERS = {
25
25
  "log.level": (k, v) => k.log.setLevel(String(v)),
26
26
  // The runtime reads the concurrency cap through the config service on every quota request.
27
27
  "runtime.orgConcurrency": () => { },
28
- // MeStreams reads the per-account stream cap at every stream open.
28
+ // SSE stream families read their per-account cap when admitting a new connection.
29
29
  "runtime.meStreamMaxPerAccount": () => { },
30
+ "runtime.orgUiStreamMaxPerAccount": () => { },
31
+ "runtime.platformUiStreamMaxPerAccount": () => { },
32
+ "runtime.conversationStreamMaxPerAccount": () => { },
30
33
  // Eval org concurrency is read the same way at every quota request.
31
34
  "evals.orgConcurrency": () => { },
32
35
  // The git gateway reads tools.git.enabled through the config service on every request.
@@ -36,6 +36,13 @@ export async function handoffKeysInUse(db, keyIds, identity) {
36
36
  where pg_catalog.left(n.nspname, ${prefix.length}) = ${prefix}
37
37
  and c.relname = ${EXTENSION_EFFECT_HANDOFF}
38
38
  `.execute(db);
39
+ const { rows: expected } = await sql `
40
+ select schema_name from capybara.extension_handoff_tables
41
+ `.execute(db);
42
+ const present = new Set(tables.map(({ schema }) => schema));
43
+ // A temporarily missing table may still hold envelopes when it is restored.
44
+ if (expected.some(({ schema_name }) => !present.has(schema_name)))
45
+ return new Set(keyIds);
39
46
  const used = new Set();
40
47
  for (const { schema, oid, ordinary, owner, login } of tables) {
41
48
  const plugin = schema.slice(prefix.length).replaceAll("_", "-");
@@ -5,7 +5,8 @@ import type { KernelSchemaDb } from "../../db/kernel-schema.js";
5
5
  * Page-number reads on a snapshot (table pagination spec §2.3): a paged route takes `page`,
6
6
  * `size`, its filters and an optional `asOf`, and answers {@link Paged}. Shared by every route a
7
7
  * DataTable pages on the server; the snapshot key (`asOf`) is the route's own (an append-only
8
- * table uses its id, a sortable table its `(sort column, id)`).
8
+ * table uses its id, a sortable table its `(sort column, id)`). A table whose rows are edited or
9
+ * deleted in place reads without a snapshot and answers {@link Page} (no `asOf`, no `newer`).
9
10
  */
10
11
  /** The page sizes a table offers (the Pagination select). */
11
12
  export declare const PAGE_SIZES: readonly [20, 50, 100];
@@ -13,22 +14,38 @@ export declare const PAGE_SIZES: readonly [20, 50, 100];
13
14
  export declare const PAGED_TOTAL_CAP = 10000;
14
15
  /** `newer` stops counting here. */
15
16
  export declare const PAGED_NEWER_CAP = 1000;
16
- /** One page of a paged route (spec §2.3). */
17
- export interface Paged<T> {
17
+ /**
18
+ * One page of a paged route without a snapshot (spec §2.3): a table whose rows are edited or
19
+ * deleted in place (accounts, agents, connectors, an eval run's results) reads by offset alone,
20
+ * and a removed row shifts later pages by one. Such a route has no `asOf` query field and its
21
+ * answer no `asOf` / `newer`: a client reads them as optional.
22
+ */
23
+ export interface Page<T> {
18
24
  items: T[];
19
25
  /** The page read, 1-based. */
20
26
  page: number;
21
27
  /** Rows per page: 20, 50 or 100. */
22
28
  size: number;
23
- /** The matching rows under the snapshot, at most {@link PAGED_TOTAL_CAP}. */
29
+ /** The matching rows (under the snapshot, when there is one), at most {@link PAGED_TOTAL_CAP}. */
24
30
  total: number;
25
31
  /** More rows match than `total` says. */
26
32
  totalCapped: boolean;
33
+ }
34
+ /** One page of a paged route on a snapshot (spec §2.3). */
35
+ export interface Paged<T> extends Page<T> {
27
36
  /** The snapshot key: pass it back to read the other pages of the same snapshot. */
28
37
  asOf: string;
29
38
  /** Rows above `asOf` matching the filters, at most {@link PAGED_NEWER_CAP}; 0 without `asOf`. */
30
39
  newer: number;
31
40
  }
41
+ /** A {@link Page} from its rows, the query's `page` / `size` and a {@link cappedCount}. */
42
+ export declare function pageOf<T>(items: T[], q: {
43
+ page: number;
44
+ size: number;
45
+ }, counted: {
46
+ count: number;
47
+ capped: boolean;
48
+ }): Page<T>;
32
49
  /**
33
50
  * The query fields every paged route takes (spec §2.3), to `extend` a route's filters with. `asOf`
34
51
  * is the route's own (its snapshot key format), e.g. {@link bigserialAsOf}.
@@ -39,6 +56,19 @@ export declare const PageQueryFields: {
39
56
  };
40
57
  /** An `asOf` that is a bigserial id (a decimal string), for append-only tables keyed by id. */
41
58
  export declare function bigserialAsOf(what: string): z.ZodOptional<z.ZodString>;
59
+ /**
60
+ * An `asOf` that is a point in time (ISO 8601), for tables whose snapshot is not a row: rows that
61
+ * move up when used (conversations by last activity), or rows keyed by a time and a random id
62
+ * (eval runs). The first read takes it from {@link snapshotNow}; a page then lists the rows whose
63
+ * sort time is at or before it.
64
+ */
65
+ export declare function timeAsOf(): z.ZodOptional<z.ZodISODateTime>;
66
+ /**
67
+ * The database's time now as a {@link timeAsOf} snapshot key: ISO 8601 in UTC with microseconds,
68
+ * as precise as a `timestamptz`, so a key compared back in SQL (`<= $1::timestamptz`) loses
69
+ * nothing. The database's clock, as the `now()` the rows were written with.
70
+ */
71
+ export declare function snapshotNow(db: KernelSchemaDb): Promise<string>;
42
72
  /** The last page a size may read: pages past the counting cap are refused. */
43
73
  export declare const lastReadablePage: (size: number) => number;
44
74
  /** Refuses a page past {@link lastReadablePage} (400 `validation_failed`, spec §2.3). */
@@ -5,7 +5,8 @@ import { ApiError } from "../../errors.js";
5
5
  * Page-number reads on a snapshot (table pagination spec §2.3): a paged route takes `page`,
6
6
  * `size`, its filters and an optional `asOf`, and answers {@link Paged}. Shared by every route a
7
7
  * DataTable pages on the server; the snapshot key (`asOf`) is the route's own (an append-only
8
- * table uses its id, a sortable table its `(sort column, id)`).
8
+ * table uses its id, a sortable table its `(sort column, id)`). A table whose rows are edited or
9
+ * deleted in place reads without a snapshot and answers {@link Page} (no `asOf`, no `newer`).
9
10
  */
10
11
  /** The page sizes a table offers (the Pagination select). */
11
12
  export const PAGE_SIZES = [20, 50, 100];
@@ -13,6 +14,16 @@ export const PAGE_SIZES = [20, 50, 100];
13
14
  export const PAGED_TOTAL_CAP = 10_000;
14
15
  /** `newer` stops counting here. */
15
16
  export const PAGED_NEWER_CAP = 1_000;
17
+ /** A {@link Page} from its rows, the query's `page` / `size` and a {@link cappedCount}. */
18
+ export function pageOf(items, q, counted) {
19
+ return {
20
+ items,
21
+ page: q.page,
22
+ size: q.size,
23
+ total: counted.count,
24
+ totalCapped: counted.capped,
25
+ };
26
+ }
16
27
  /**
17
28
  * The query fields every paged route takes (spec §2.3), to `extend` a route's filters with. `asOf`
18
29
  * is the route's own (its snapshot key format), e.g. {@link bigserialAsOf}.
@@ -44,6 +55,32 @@ export function bigserialAsOf(what) {
44
55
  .optional()
45
56
  .describe(`The snapshot: the asOf an earlier page returned (${what}); absent: a new snapshot of the newest rows.`));
46
57
  }
58
+ /**
59
+ * An `asOf` that is a point in time (ISO 8601), for tables whose snapshot is not a row: rows that
60
+ * move up when used (conversations by last activity), or rows keyed by a time and a random id
61
+ * (eval runs). The first read takes it from {@link snapshotNow}; a page then lists the rows whose
62
+ * sort time is at or before it.
63
+ */
64
+ export function timeAsOf() {
65
+ return z.iso
66
+ .datetime({ offset: true })
67
+ .optional()
68
+ .describe("The snapshot: the asOf an earlier page returned (an ISO 8601 time); absent: a new snapshot.");
69
+ }
70
+ /**
71
+ * The database's time now as a {@link timeAsOf} snapshot key: ISO 8601 in UTC with microseconds,
72
+ * as precise as a `timestamptz`, so a key compared back in SQL (`<= $1::timestamptz`) loses
73
+ * nothing. The database's clock, as the `now()` the rows were written with.
74
+ */
75
+ export async function snapshotNow(db) {
76
+ const r = await sql `
77
+ SELECT to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS now
78
+ `.execute(db);
79
+ const now = r.rows[0]?.now;
80
+ if (now === undefined)
81
+ throw new Error("SELECT now() returned no row");
82
+ return now;
83
+ }
47
84
  /** The last page a size may read: pages past the counting cap are refused. */
48
85
  export const lastReadablePage = (size) => Math.ceil(PAGED_TOTAL_CAP / size);
49
86
  /** Refuses a page past {@link lastReadablePage} (400 `validation_failed`, spec §2.3). */
@@ -0,0 +1,32 @@
1
+ import type { OrgRole } from "../orgs/roles.js";
2
+ import { type InvalidationTopic } from "./hints.js";
3
+ import type { InvalidationHints } from "./invalidation-hints.js";
4
+ /**
5
+ * What a writer needs to publish hints (account invalidation stream spec §4): the after-commit
6
+ * publish only. Plain database functions (the request settlement, org notices) take it as a
7
+ * parameter from the Nest service that calls them, as `oweEffectWork` takes the outbox.
8
+ */
9
+ export type HintPublisher = Pick<InvalidationHints, "publishAfterCommit">;
10
+ /**
11
+ * The role topic that reaches every member at or above `role`. A guest has no role topic: an
12
+ * entry addressed to guests and up reaches members and up (guests converge on freshness).
13
+ */
14
+ export declare function roleTopicFor(orgId: string, role: OrgRole): InvalidationTopic;
15
+ /** The `inbox` audience of an approval: its `requested_for` person, else its approver role. */
16
+ export declare function approvalTopics(a: {
17
+ orgId: string;
18
+ requestedFor: string | null;
19
+ approverRole: OrgRole | null;
20
+ }): InvalidationTopic[];
21
+ /** Publishes `capabilities` to the member streams of each org in `orgIds` (one call). */
22
+ export declare function publishCapabilities(hints: HintPublisher, orgIds: Iterable<string>): void;
23
+ /**
24
+ * Publishes `capabilities` to every member stream of every org (`platform-caps`): a
25
+ * deployment-wide change whose writer computes no affected-org set. Kind only, so nothing leaks;
26
+ * such writes are rare, and the web coalesces refetches.
27
+ */
28
+ export declare function publishPlatformCapabilities(hints: HintPublisher): void;
29
+ /** Publishes `inbox` to the streams of each account in `accountIds` (one call). */
30
+ export declare function publishAccountInbox(hints: HintPublisher, accountIds: Iterable<string>): void;
31
+ /** Publishes `inbox` to the admins and owners of each org in `orgIds` (one call). */
32
+ export declare function publishOrgAdminsInbox(hints: HintPublisher, orgIds: Iterable<string>): void;