@capybara-hq/kernel 0.1.0-rc.36 → 0.1.0-rc.37

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/bootstrap.json +5 -5
  2. package/dist/admin/admin.module.d.ts +3 -3
  3. package/dist/admin/admin.module.js +6 -6
  4. package/dist/admin/audit-log.service.d.ts +51 -0
  5. package/dist/admin/{platform-audit.service.js → audit-log.service.js} +20 -12
  6. package/dist/admin/audit.controller.d.ts +18 -6
  7. package/dist/admin/audit.controller.js +36 -6
  8. package/dist/admin/audit.schemas.d.ts +9 -1
  9. package/dist/admin/audit.schemas.js +22 -2
  10. package/dist/admin/outbox-dead.controller.d.ts +4 -6
  11. package/dist/admin/outbox-dead.controller.js +9 -6
  12. package/dist/admin/outbox-dead.schemas.d.ts +16 -0
  13. package/dist/admin/outbox-dead.schemas.js +25 -1
  14. package/dist/admin/outbox-dead.service.d.ts +16 -7
  15. package/dist/admin/outbox-dead.service.js +34 -10
  16. package/dist/admin/overview.service.d.ts +3 -3
  17. package/dist/admin/overview.service.js +0 -1
  18. package/dist/agents/agent-lookup.js +2 -10
  19. package/dist/agents/agents.service.js +9 -20
  20. package/dist/agents/definition.d.ts +3 -51
  21. package/dist/agents/definition.js +4 -254
  22. package/dist/agents/org-templates.js +2 -6
  23. package/dist/agents/readiness.controller.d.ts +4 -12
  24. package/dist/agents/readiness.controller.js +4 -12
  25. package/dist/agents/readiness.service.d.ts +23 -11
  26. package/dist/agents/readiness.service.js +31 -14
  27. package/dist/agents/templates.d.ts +0 -6
  28. package/dist/agents/templates.js +8 -34
  29. package/dist/background/store.d.ts +2 -0
  30. package/dist/background/store.js +3 -1
  31. package/dist/background/wake-rules.js +2 -1
  32. package/dist/catalog/admin-catalog.controller.d.ts +1 -2
  33. package/dist/catalog/admin-catalog.controller.js +1 -2
  34. package/dist/catalog/appearance.service.js +1 -3
  35. package/dist/catalog/catalog-content.d.ts +1 -1
  36. package/dist/catalog/catalog-content.js +1 -4
  37. package/dist/catalog/catalog-items.js +3 -3
  38. package/dist/catalog/catalog-scope.service.d.ts +10 -28
  39. package/dist/catalog/catalog-scope.service.js +22 -73
  40. package/dist/catalog/cli.d.ts +0 -11
  41. package/dist/catalog/cli.js +0 -20
  42. package/dist/catalog/component-scope.service.js +0 -2
  43. package/dist/catalog/component-state.js +2 -2
  44. package/dist/catalog/install-status.d.ts +6 -0
  45. package/dist/catalog/install-status.js +6 -0
  46. package/dist/catalog/job-ids.d.ts +0 -4
  47. package/dist/catalog/job-ids.js +0 -4
  48. package/dist/catalog/marketplaces.service.d.ts +1 -1
  49. package/dist/catalog/org-catalog.service.d.ts +2 -0
  50. package/dist/catalog/org-catalog.service.js +8 -3
  51. package/dist/catalog/org-extension-switch.d.ts +7 -2
  52. package/dist/catalog/org-extension-switch.js +56 -9
  53. package/dist/catalog/org-plugin-uploads.d.ts +6 -15
  54. package/dist/catalog/org-plugin-uploads.js +9 -27
  55. package/dist/catalog/owned-items.js +1 -3
  56. package/dist/catalog/platform-import-update.js +1 -1
  57. package/dist/catalog/plugin-import.d.ts +15 -13
  58. package/dist/catalog/plugin-import.js +21 -30
  59. package/dist/catalog/plugin-install.d.ts +3 -43
  60. package/dist/catalog/plugin-install.js +8 -150
  61. package/dist/catalog/plugin-package.d.ts +5 -10
  62. package/dist/catalog/plugin-package.js +12 -33
  63. package/dist/catalog/plugin-upgrade.js +5 -4
  64. package/dist/catalog/scope-jobs.d.ts +34 -34
  65. package/dist/catalog/scope-jobs.js +50 -37
  66. package/dist/catalog/scope-orgs.d.ts +1 -1
  67. package/dist/catalog/scope-orgs.js +1 -2
  68. package/dist/catalog/scope-sync-workers.d.ts +4 -3
  69. package/dist/catalog/scope-sync-workers.js +18 -6
  70. package/dist/catalog/scope-sync.d.ts +56 -59
  71. package/dist/catalog/scope-sync.js +96 -166
  72. package/dist/catalog/unshare.d.ts +3 -4
  73. package/dist/catalog/unshare.js +5 -8
  74. package/dist/channels/job-ids.d.ts +2 -6
  75. package/dist/channels/job-ids.js +2 -6
  76. package/dist/channels/outbound.d.ts +1 -1
  77. package/dist/channels/outbound.js +1 -1
  78. package/dist/channels/supervisor.d.ts +2 -3
  79. package/dist/channels/supervisor.js +2 -3
  80. package/dist/client/client-access.service.js +1 -1
  81. package/dist/config/env.js +19 -1
  82. package/dist/config/load.d.ts +0 -6
  83. package/dist/config/load.js +8 -27
  84. package/dist/config/schema.d.ts +53 -47
  85. package/dist/config/schema.js +190 -40
  86. package/dist/conversations/attachments.service.d.ts +10 -3
  87. package/dist/conversations/attachments.service.js +49 -16
  88. package/dist/conversations/conversations.controller.d.ts +0 -39
  89. package/dist/conversations/conversations.controller.js +0 -39
  90. package/dist/conversations/events.d.ts +71 -46
  91. package/dist/conversations/events.js +124 -112
  92. package/dist/conversations/media-types.d.ts +25 -0
  93. package/dist/conversations/media-types.js +38 -0
  94. package/dist/conversations/queue.d.ts +3 -1
  95. package/dist/conversations/queue.service.js +9 -2
  96. package/dist/conversations/titles.js +6 -0
  97. package/dist/ctx/create-ctx.js +1 -2
  98. package/dist/ctx/secrets.d.ts +24 -7
  99. package/dist/ctx/secrets.js +97 -30
  100. package/dist/db/kernel-schema.d.ts +17 -6
  101. package/dist/errors.d.ts +5 -2
  102. package/dist/errors.js +5 -2
  103. package/dist/evals/reconstruct.d.ts +4 -17
  104. package/dist/evals/reconstruct.js +13 -53
  105. package/dist/evals/recorded-tools.d.ts +0 -27
  106. package/dist/evals/recorded-tools.js +2 -92
  107. package/dist/evals/run-replay.d.ts +1 -6
  108. package/dist/evals/run-replay.js +10 -17
  109. package/dist/evals/sets.service.d.ts +17 -5
  110. package/dist/evals/sets.service.js +33 -12
  111. package/dist/evals/snapshot.d.ts +1 -2
  112. package/dist/evals/snapshot.js +4 -17
  113. package/dist/extensions/admin-ops.d.ts +6 -1
  114. package/dist/extensions/admin-ops.js +17 -4
  115. package/dist/extensions/admin-view.d.ts +7 -4
  116. package/dist/extensions/admin-view.js +27 -21
  117. package/dist/extensions/bootstrap.js +0 -1
  118. package/dist/extensions/cli.js +0 -1
  119. package/dist/extensions/host.d.ts +2 -3
  120. package/dist/extensions/host.js +1 -5
  121. package/dist/extensions/installs.d.ts +1 -3
  122. package/dist/extensions/instance.d.ts +0 -2
  123. package/dist/extensions/instance.js +0 -3
  124. package/dist/extensions/org-config.d.ts +4 -4
  125. package/dist/extensions/org-config.js +13 -22
  126. package/dist/extensions/package.d.ts +6 -6
  127. package/dist/extensions/package.js +3 -7
  128. package/dist/extensions/permission-view.d.ts +2 -4
  129. package/dist/extensions/plugin-items.d.ts +21 -1
  130. package/dist/extensions/plugin-items.js +28 -0
  131. package/dist/extensions/sync.js +5 -10
  132. package/dist/extensions/unavailable.js +0 -1
  133. package/dist/http/api.module.js +2 -1
  134. package/dist/identity/audit.d.ts +0 -12
  135. package/dist/identity/audit.js +0 -27
  136. package/dist/identity/auth.controller.js +5 -9
  137. package/dist/identity/cookies.d.ts +4 -9
  138. package/dist/identity/cookies.js +8 -32
  139. package/dist/identity/initial-passwords.d.ts +1 -1
  140. package/dist/identity/initial-passwords.js +1 -2
  141. package/dist/identity/sessions.d.ts +0 -6
  142. package/dist/identity/sessions.js +0 -18
  143. package/dist/inbox/platform-inbox.service.d.ts +33 -1
  144. package/dist/inbox/platform-inbox.service.js +56 -6
  145. package/dist/index.d.ts +4 -2
  146. package/dist/index.js +4 -2
  147. package/dist/infra/cli/master-key-rewrap.d.ts +15 -0
  148. package/dist/infra/cli/master-key-rewrap.js +24 -0
  149. package/dist/infra/config/kernel-config.service.js +0 -17
  150. package/dist/infra/health/health.controller.js +8 -0
  151. package/dist/infra/health/process-fault.d.ts +16 -0
  152. package/dist/infra/health/process-fault.js +49 -0
  153. package/dist/infra/lifecycle/process-guards.d.ts +8 -2
  154. package/dist/infra/lifecycle/process-guards.js +12 -3
  155. package/dist/infra/lifecycle/shutdown-signals.d.ts +10 -1
  156. package/dist/infra/lifecycle/shutdown-signals.js +28 -13
  157. package/dist/infra/outbox/outbox.service.d.ts +12 -9
  158. package/dist/infra/outbox/outbox.service.js +22 -26
  159. package/dist/infra/outbox/registry.d.ts +15 -7
  160. package/dist/infra/outbox/registry.js +39 -23
  161. package/dist/lifecycle/kernel-lifecycle.js +1 -7
  162. package/dist/models/enablement.d.ts +5 -6
  163. package/dist/models/enablement.js +7 -14
  164. package/dist/orgs/orgs.module.d.ts +2 -2
  165. package/dist/orgs/orgs.module.js +2 -2
  166. package/dist/orgs/settings.controller.d.ts +3 -2
  167. package/dist/orgs/settings.service.d.ts +59 -14
  168. package/dist/orgs/settings.service.js +102 -20
  169. package/dist/overrides.d.ts +3 -7
  170. package/dist/overrides.js +2 -4
  171. package/dist/packages/tgz.d.ts +0 -2
  172. package/dist/packages/tgz.js +0 -2
  173. package/dist/platform/exclusions.js +0 -5
  174. package/dist/platform/operation.js +1 -1
  175. package/dist/platform/ops/admin.ops.d.ts +2 -2
  176. package/dist/platform/ops/admin.ops.js +9 -6
  177. package/dist/platform/ops/agents.ops.js +1 -1
  178. package/dist/platform/ops/connectors.ops.js +46 -1
  179. package/dist/platform/ops/evals.ops.js +1 -64
  180. package/dist/platform/ops/extensions-admin.ops.js +1 -1
  181. package/dist/platform/ops/inbox.ops.js +16 -3
  182. package/dist/platform/ops/org-plugins.ops.js +1 -1
  183. package/dist/platform/ops/orgs.ops.d.ts +5 -3
  184. package/dist/platform/ops/orgs.ops.js +19 -4
  185. package/dist/platform/ops/outbox.ops.js +5 -5
  186. package/dist/roles/worker-role.d.ts +2 -3
  187. package/dist/roles/worker-role.js +3 -13
  188. package/dist/runtime/context/build.d.ts +56 -18
  189. package/dist/runtime/context/build.js +143 -50
  190. package/dist/runtime/context/compaction.d.ts +1 -3
  191. package/dist/runtime/context/compaction.js +42 -8
  192. package/dist/runtime/context/run-context.js +1 -2
  193. package/dist/runtime/context/skills.d.ts +27 -56
  194. package/dist/runtime/context/skills.js +42 -114
  195. package/dist/runtime/context/tool-changes.d.ts +3 -3
  196. package/dist/runtime/context/tool-changes.js +6 -16
  197. package/dist/runtime/hook-calls.d.ts +6 -6
  198. package/dist/runtime/hook-calls.js +7 -7
  199. package/dist/runtime/langgraph-loop.js +1 -0
  200. package/dist/runtime/maintenance.d.ts +3 -11
  201. package/dist/runtime/maintenance.js +5 -20
  202. package/dist/runtime/model-call.d.ts +5 -0
  203. package/dist/runtime/model-call.js +48 -36
  204. package/dist/runtime/providers.d.ts +33 -35
  205. package/dist/runtime/providers.js +64 -109
  206. package/dist/runtime/run-context.service.js +7 -22
  207. package/dist/runtime/run-deletion.d.ts +6 -0
  208. package/dist/runtime/run-deletion.js +39 -16
  209. package/dist/runtime/run-execution.js +4 -3
  210. package/dist/runtime/run-finish.js +2 -1
  211. package/dist/runtime/run-liveness.d.ts +11 -0
  212. package/dist/runtime/run-liveness.js +19 -0
  213. package/dist/runtime/run-orphan-check.job.js +2 -1
  214. package/dist/runtime/run-queue.js +8 -16
  215. package/dist/runtime/run-worker.d.ts +2 -2
  216. package/dist/runtime/run-worker.js +8 -6
  217. package/dist/runtime/subagents/definition.d.ts +16 -20
  218. package/dist/runtime/subagents/definition.js +27 -27
  219. package/dist/runtime/subagents/dispatch.d.ts +6 -2
  220. package/dist/runtime/subagents/dispatch.js +17 -3
  221. package/dist/runtime/subagents/org-agents.js +1 -2
  222. package/dist/runtime/subagents/preamble.d.ts +3 -2
  223. package/dist/runtime/subagents/preamble.js +3 -2
  224. package/dist/runtime/subagents/render.d.ts +1 -2
  225. package/dist/runtime/subagents/render.js +1 -2
  226. package/dist/runtime/subagents/skill-fork.d.ts +2 -4
  227. package/dist/runtime/subagents/template-skills.d.ts +47 -0
  228. package/dist/runtime/subagents/template-skills.js +76 -0
  229. package/dist/schedules/effective-settings.d.ts +9 -3
  230. package/dist/schedules/effective-settings.js +5 -4
  231. package/dist/schedules/fire.js +11 -0
  232. package/dist/schedules/scheduled-tasks.service.js +5 -0
  233. package/dist/schedules/unattended.d.ts +2 -0
  234. package/dist/schedules/unattended.js +6 -1
  235. package/dist/skills/agent-skills.service.js +1 -2
  236. package/dist/skills/catalog.d.ts +1 -3
  237. package/dist/skills/catalog.js +1 -3
  238. package/dist/skills/conversation-catalog.d.ts +1 -2
  239. package/dist/skills/conversation-catalog.js +1 -2
  240. package/dist/skills/usable.js +2 -3
  241. package/dist/storage/storage.service.js +9 -1
  242. package/dist/tools/approvals.service.js +2 -4
  243. package/dist/tools/builtin-names.d.ts +2 -9
  244. package/dist/tools/builtin-names.js +4 -13
  245. package/dist/tools/builtins/sandbox/bash.js +3 -3
  246. package/dist/tools/builtins/sandbox/common.d.ts +7 -13
  247. package/dist/tools/builtins/sandbox/common.js +8 -115
  248. package/dist/tools/builtins/sandbox/fetch-attachment.d.ts +10 -0
  249. package/dist/tools/builtins/sandbox/fetch-attachment.js +119 -0
  250. package/dist/tools/builtins/sandbox/glob.js +1 -3
  251. package/dist/tools/builtins/sandbox/grep.js +3 -5
  252. package/dist/tools/builtins/sandbox/index.d.ts +8 -5
  253. package/dist/tools/builtins/sandbox/index.js +10 -5
  254. package/dist/tools/builtins/sandbox/read.js +3 -2
  255. package/dist/tools/builtins/tool-search.d.ts +4 -7
  256. package/dist/tools/builtins/tool-search.js +6 -10
  257. package/dist/tools/connector-account-policies.d.ts +30 -3
  258. package/dist/tools/connector-account-policies.js +54 -7
  259. package/dist/tools/connectors.controller.d.ts +24 -11
  260. package/dist/tools/connectors.controller.js +35 -6
  261. package/dist/tools/connectors.schemas.d.ts +14 -1
  262. package/dist/tools/connectors.schemas.js +14 -2
  263. package/dist/tools/connectors.service.d.ts +22 -0
  264. package/dist/tools/connectors.service.js +56 -0
  265. package/dist/tools/drift.d.ts +1 -1
  266. package/dist/tools/drift.js +1 -1
  267. package/dist/tools/git-push-policy.d.ts +4 -1
  268. package/dist/tools/git-push-policy.js +3 -1
  269. package/dist/tools/info.d.ts +1 -7
  270. package/dist/tools/info.js +5 -18
  271. package/dist/tools/job-ids.d.ts +0 -4
  272. package/dist/tools/job-ids.js +1 -6
  273. package/dist/tools/pipeline.d.ts +4 -10
  274. package/dist/tools/pipeline.js +16 -62
  275. package/dist/tools/policy.d.ts +6 -2
  276. package/dist/tools/policy.js +14 -12
  277. package/dist/tools/registry.js +1 -5
  278. package/dist/ui/views.d.ts +5 -5
  279. package/dist/ui/views.js +10 -16
  280. package/package.json +3 -3
  281. package/src/migrations/0137_audit_org_idx.sql +5 -0
  282. package/src/migrations/0138_plugin_install_status_no_upgrading.sql +18 -0
  283. package/src/migrations/0139_retire_legacy_forms.sql +183 -0
  284. package/src/migrations/0140_retire_history_forms.sql +148 -0
  285. package/src/migrations/0141_install_ledger_promoted_rows.sql +36 -0
  286. package/src/migrations/0142_backfill_event_fields.sql +74 -0
  287. package/src/migrations/0143_backfill_model_call_fields.sql +105 -0
  288. package/src/migrations/0144_retire_frozen_definition_keys.sql +163 -0
  289. package/dist/admin/platform-audit.service.d.ts +0 -41
  290. package/dist/conversations/job-ids.d.ts +0 -6
  291. package/dist/conversations/job-ids.js +0 -6
  292. package/dist/extensions/job-ids.d.ts +0 -6
  293. package/dist/extensions/job-ids.js +0 -6
  294. package/dist/infra/queue/retired-schedulers.d.ts +0 -7
  295. package/dist/infra/queue/retired-schedulers.js +0 -9
  296. package/dist/orgs/job-ids.d.ts +0 -8
  297. package/dist/orgs/job-ids.js +0 -8
  298. package/dist/runtime/context/legacy-text.d.ts +0 -22
  299. package/dist/runtime/context/legacy-text.js +0 -37
  300. package/dist/runtime/job-ids.d.ts +0 -10
  301. package/dist/runtime/job-ids.js +0 -10
  302. package/dist/runtime/retired-schedulers.d.ts +0 -7
  303. package/dist/runtime/retired-schedulers.js +0 -31
  304. package/dist/storage/job-ids.d.ts +0 -6
  305. package/dist/storage/job-ids.js +0 -6
  306. package/dist/tools/legacy-data.d.ts +0 -34
  307. package/dist/tools/legacy-data.js +0 -76
  308. package/dist/tools/legacy-keys.d.ts +0 -51
  309. package/dist/tools/legacy-keys.js +0 -114
@@ -33,6 +33,7 @@ import { ConnectorsService } from "../tools/connectors.service.js";
33
33
  import { findConnectorIn, lockOrgForActions, readCatalogAgentTemplates, readCatalogFiles, readCatalogTemplates, requiredActions, templateConnectorInput, templatePluginProblem, } from "./catalog-content.js";
34
34
  import { versionUsable } from "./catalog-items.js";
35
35
  import { agentDefinitionRefs, componentOn, connectorTemplateState, connectorTemplatesOn, listedIn, loadComponents, loadScopeState, pluginFactsOf, referenceIds, switchOf, themeIdClashes, } from "./component-state.js";
36
+ import { LIVE_INSTALL_STATUSES } from "./install-status.js";
36
37
  import { assertItemInstallable, disabledPluginItems, pluginItemDisabled, resolveRequestsByInstall, } from "./request-rules.js";
37
38
  import { visibleTo } from "./scope.js";
38
39
  import { ScopeNoLongerWanted, ScopeStillWanted, wantedByScope } from "./scope-state.js";
@@ -288,8 +289,7 @@ export function under(e, at) {
288
289
  * `plugin.install_failed` is thrown. Uninstall removes only components with created=true that
289
290
  * nothing else uses, and reports what it kept and why. No in-process state; the org row lock
290
291
  * serializes writes within one org. There is no per-org upgrade: a platform plugin is updated
291
- * once for the deployment (`PlatformPluginUpdater`, plugin-upgrade.ts, ruling F2); an install left
292
- * `upgrading` by an earlier per-org upgrade is undone from its journal by the next uninstall. The
292
+ * once for the deployment (`PlatformPluginUpdater`, plugin-upgrade.ts, ruling F2). The
293
293
  * methods without `private` are internal to the catalog, not an API.
294
294
  */
295
295
  let PluginInstaller = class PluginInstaller {
@@ -1069,13 +1069,6 @@ let PluginInstaller = class PluginInstaller {
1069
1069
  }
1070
1070
  if (c.kind === "plugin")
1071
1071
  return false;
1072
- // A row the org promoted is the org's own shared item (ruling P3-47): an install never
1073
- // removes or reverts it (P3-46 keeps plugin content from being promoted; this guards rows
1074
- // recorded before that or by a legacy install)
1075
- if ((c.kind === "agent_template" || c.kind === "skill") &&
1076
- (await promotedOrgRow(t, orgId, c.kind, c.ref))) {
1077
- return false;
1078
- }
1079
1072
  if (c.kind === "agent_template") {
1080
1073
  if (c.created)
1081
1074
  return softDeleteTemplateIn(t, orgId, c.ref);
@@ -1229,7 +1222,7 @@ let PluginInstaller = class PluginInstaller {
1229
1222
  .innerJoin("plugin_installs as a", "a.id", "c.install_id")
1230
1223
  .select("a.id")
1231
1224
  .where("a.org_id", "=", orgId)
1232
- .where("a.status", "in", ["installing", "installed", "upgrading"])
1225
+ .where("a.status", "in", [...LIVE_INSTALL_STATUSES])
1233
1226
  .where("c.kind", "=", kind)
1234
1227
  .where("c.ref", "=", ref);
1235
1228
  if (removing.size > 0)
@@ -1289,8 +1282,6 @@ let PluginInstaller = class PluginInstaller {
1289
1282
  "a.status",
1290
1283
  "i.name",
1291
1284
  sql `a.installed_at < now() - make_interval(mins => ${staleMinutes})`.as("stale"),
1292
- sql `coalesce((a.upgrade->>'startedAt')::timestamptz < now() - make_interval(mins => ${staleMinutes}), false)`.as("upgradeStale"),
1293
- "a.upgrade",
1294
1285
  ])
1295
1286
  .where("a.id", "=", installId)
1296
1287
  .where("a.org_id", "=", orgId)
@@ -1311,26 +1302,16 @@ let PluginInstaller = class PluginInstaller {
1311
1302
  await this.handOver(user, "plugin", installId);
1312
1303
  return "kept";
1313
1304
  }
1314
- if (row.status === "uninstalling" ||
1315
- (row.status === "installing" && !row.stale) ||
1316
- (row.status === "upgrading" && !row.upgradeStale)) {
1305
+ if (row.status === "uninstalling" || (row.status === "installing" && !row.stale)) {
1317
1306
  throw new ApiError("plugin.install_in_progress", `This plugin install is still in progress; it can be cleaned up after ${staleMinutes} minutes`);
1318
1307
  }
1319
- if (row.status === "upgrading") {
1320
- // An upgrade interrupted midway (a replica crashed): undone first, back at the old version
1321
- const undone = await this.undoUpgradeIn(orgId, actor, installId, journalEntries(row.upgrade), "plugin_upgrade_cleanup");
1322
- for (const x of undone)
1323
- addRemoved(x);
1324
- }
1325
1308
  removing.add(installId);
1326
1309
  // failed (already rolled back) and a stale installing (a replica crashed midway) are cleaned up
1327
1310
  // as a rollback
1328
- const rollbackMode = row.status !== "installed" && row.status !== "upgrading";
1329
- // A dependency install removed by a failed install's (or a crashed upgrade's) rollback removes
1330
- // what it created, like the rolled-back install itself: it is a rollback, not a switch-off
1331
- const connectorRollback = rollbackMode ||
1332
- ctx.reason === "plugin_install_rollback" ||
1333
- ctx.reason === "plugin_upgrade_cleanup";
1311
+ const rollbackMode = row.status !== "installed";
1312
+ // A dependency install removed by a failed install's rollback removes what it created, like
1313
+ // the rolled-back install itself: it is a rollback, not a switch-off
1314
+ const connectorRollback = rollbackMode || ctx.reason === "plugin_install_rollback";
1334
1315
  const comps = await t
1335
1316
  .selectFrom("plugin_install_components")
1336
1317
  .select(["kind", "ref", "created", "skill_version"])
@@ -1536,65 +1517,6 @@ let PluginInstaller = class PluginInstaller {
1536
1517
  deploymentPending: false,
1537
1518
  }));
1538
1519
  }
1539
- /**
1540
- * Undoes the steps of an upgrade in the caller's transaction (the caller holds the org row lock),
1541
- * in reverse order: components it created are removed (a dependency install by the uninstall
1542
- * rules, an extension another enabled extension depends on is kept), the ledger rows it added
1543
- * are deleted, and versions it added to skills are reverted (`revertVersionIn` reverts exactly
1544
- * one version, and an upgrade adds at most one per skill). Returns the components removed (an
1545
- * extension among them needs `afterDisable` once committed). Used when an upgrade fails and when
1546
- * an interrupted one is cleaned up.
1547
- */
1548
- async undoUpgradeIn(orgId, actor, installId, steps, reason) {
1549
- const removed = [];
1550
- const dropRow = (kind, ref) => this.db
1551
- .deleteFrom("plugin_install_components")
1552
- .where("install_id", "=", installId)
1553
- .where("kind", "=", kind)
1554
- .where("ref", "=", ref)
1555
- .execute();
1556
- for (const s of [...steps].reverse()) {
1557
- if (s.op === "skill_version") {
1558
- await this.skills.revertVersionIn(orgId, s.ref, s.version, actor, reason);
1559
- continue;
1560
- }
1561
- if (s.op === "template_version") {
1562
- await revertTemplateVersionIn(this.db, orgId, s.ref, s.version);
1563
- continue;
1564
- }
1565
- if (s.op === "recorded") {
1566
- await dropRow(s.kind, s.ref);
1567
- continue;
1568
- }
1569
- if (s.kind === "plugin") {
1570
- const sub = { removed: [], kept: [] };
1571
- await dropRow(s.kind, s.ref);
1572
- const outcome = await this.uninstallIn(orgId, actor, s.ref, {
1573
- report: sub,
1574
- removing: new Set([installId]),
1575
- from: installId,
1576
- authorize: false,
1577
- reason,
1578
- opts: {},
1579
- });
1580
- removed.push(...sub.removed.filter((r) => r.kind === "extension"));
1581
- if (outcome === "removed")
1582
- removed.push({ kind: "plugin", ref: s.ref });
1583
- continue;
1584
- }
1585
- if (s.kind === "extension" &&
1586
- (await this.orgConfigs.enabledDependents(orgId, s.ref)).length > 0) {
1587
- // Another enabled extension needs it: kept, and stays this install's component
1588
- continue;
1589
- }
1590
- const c = { kind: s.kind, ref: s.ref, created: true, skillVersion: null };
1591
- if (await this.removeComponent(orgId, actor, c, reason)) {
1592
- removed.push({ kind: s.kind, ref: s.ref });
1593
- }
1594
- await dropRow(s.kind, s.ref);
1595
- }
1596
- return removed;
1597
- }
1598
1520
  /**
1599
1521
  * Installs the dependencies an installed plugin's version declares and the org lacks (rule 9),
1600
1522
  * as the kernel (actor null): a platform update can add one. Each is recorded on the install
@@ -2094,9 +2016,6 @@ __decorate([
2094
2016
  __decorate([
2095
2017
  Transactional({ propagation: "mandatory" })
2096
2018
  ], PluginInstaller.prototype, "decideRemovalIn", null);
2097
- __decorate([
2098
- Transactional({ propagation: "mandatory" })
2099
- ], PluginInstaller.prototype, "undoUpgradeIn", null);
2100
2019
  __decorate([
2101
2020
  Transactional({ propagation: "requires_new" })
2102
2021
  ], PluginInstaller.prototype, "recordConnector", null);
@@ -2169,34 +2088,6 @@ export async function planSkillConflicts(db, orgId, plan) {
2169
2088
  await visit(plan);
2170
2089
  return out;
2171
2090
  }
2172
- const COMPONENT_KINDS = new Set([
2173
- "extension",
2174
- "connector",
2175
- "skill",
2176
- "plugin",
2177
- "agent_template",
2178
- ]);
2179
- /** The journaled steps of `plugin_installs.upgrade` (malformed entries are skipped). */
2180
- export function journalEntries(upgrade) {
2181
- const done = upgrade?.done;
2182
- if (!Array.isArray(done))
2183
- return [];
2184
- const out = [];
2185
- for (const e of done) {
2186
- if (typeof e !== "object" || e === null || typeof e.ref !== "string")
2187
- continue;
2188
- if ((e.op === "skill_version" || e.op === "template_version") &&
2189
- typeof e.version === "number") {
2190
- out.push({ op: e.op, ref: e.ref, version: e.version });
2191
- }
2192
- else if ((e.op === "created" || e.op === "recorded") &&
2193
- typeof e.kind === "string" &&
2194
- COMPONENT_KINDS.has(e.kind)) {
2195
- out.push({ op: e.op, kind: e.kind, ref: e.ref });
2196
- }
2197
- }
2198
- return out;
2199
- }
2200
2091
  /** The org actions a plan needs: the union over the plugins it installs (spec §10). */
2201
2092
  export function planActions(plan) {
2202
2093
  const out = new Set(requiredActions(plan.manifest));
@@ -2226,36 +2117,3 @@ function orderByRequires(set) {
2226
2117
  }
2227
2118
  return placed;
2228
2119
  }
2229
- /**
2230
- * Whether org row `id` (a skill or an agent template of the org) is linked without a pin to a live
2231
- * platform row the org promoted when sharing it (ruling P3-3): owned by the org and from no plugin
2232
- * (`catalog_item_id` null). A component of the org's own uploaded plugin is a plugin's, so its
2233
- * install copies are removed and reverted as any plugin's.
2234
- */
2235
- async function promotedOrgRow(db, orgId, kind, id) {
2236
- const row = kind === "skill"
2237
- ? await db
2238
- .selectFrom("skills as s")
2239
- .innerJoin("platform_skills as p", "p.id", "s.platform_skill_id")
2240
- .select("s.id")
2241
- .where("s.id", "=", id)
2242
- .where("s.org_id", "=", orgId)
2243
- .where("s.pinned_version", "is", null)
2244
- .where("p.owner_org_id", "=", orgId)
2245
- // promoted, not an org plugin's component (which has its plugin, P3-2)
2246
- .where("p.catalog_item_id", "is", null)
2247
- .where("p.deleted_at", "is", null)
2248
- .executeTakeFirst()
2249
- : await db
2250
- .selectFrom("agent_templates as a")
2251
- .innerJoin("platform_agent_templates as p", "p.id", "a.platform_template_id")
2252
- .select("a.id")
2253
- .where("a.id", "=", id)
2254
- .where("a.org_id", "=", orgId)
2255
- .where("a.pinned_version", "is", null)
2256
- .where("p.owner_org_id", "=", orgId)
2257
- .where("p.catalog_item_id", "is", null)
2258
- .where("p.deleted_at", "is", null)
2259
- .executeTakeFirst();
2260
- return row !== undefined;
2261
- }
@@ -1,7 +1,7 @@
1
1
  import { type AgentTemplate, type ConnectorTemplateFile, type ParsedTheme, type PluginManifest } from "@capybara-hq/plugin-sdk";
2
2
  import { ApiError } from "../errors.js";
3
- import { LEGACY_FORMAT_MESSAGE, type PackageTgzLimits, packagePath } from "../packages/tgz.js";
4
- export { LEGACY_FORMAT_MESSAGE, packagePath };
3
+ import { type PackageTgzLimits, packagePath } from "../packages/tgz.js";
4
+ export { packagePath };
5
5
  /**
6
6
  * Reads a plugin tgz (spec §4.1): `package/.capybara-plugin/plugin.json` plus the skills, connector
7
7
  * templates and extension it describes. The tgz is untrusted input and is unpacked by the extension
@@ -9,10 +9,6 @@ export { LEGACY_FORMAT_MESSAGE, packagePath };
9
9
  * count, regular files only, safe paths under `package/`). No code from the package runs here; the extension's module graph is
10
10
  * checked by `readExtensionPackage`. The checks after unpacking live in {@link readPluginFiles},
11
11
  * shared with the git reader (`git-source.ts`), which reads the tree from a commit instead.
12
- *
13
- * A tgz in the pre-0.1.0 format (a `capybara` section in package.json, or `app.yaml`, and no
14
- * plugin.json) is rejected first, with {@link LEGACY_FORMAT_MESSAGE}: there is no reader for the
15
- * old formats.
16
12
  */
17
13
  /**
18
14
  * Codes of {@link PluginPackageError}: `theme.invalid` / `theme.unreadable` when the package's only
@@ -20,7 +16,7 @@ export { LEGACY_FORMAT_MESSAGE, packagePath };
20
16
  * `client.undeclared` for an `extension.client` section whose manifest lacks
21
17
  * `permissions.client: true` (client extensions spec §5.4: client code is always reviewed).
22
18
  */
23
- export type PluginPackageErrorCode = "catalog.invalid_manifest" | "catalog.legacy_format" | "theme.invalid" | "theme.unreadable" | "client.undeclared";
19
+ export type PluginPackageErrorCode = "catalog.invalid_manifest" | "theme.invalid" | "theme.unreadable" | "client.undeclared";
24
20
  /** The problems of one theme of a refused plugin (English; `unreadable:` lines for readability). */
25
21
  export interface PluginThemeProblem {
26
22
  /** The theme id (the file's base name). */
@@ -137,9 +133,8 @@ export declare const PLUGIN_ICON_MAX_BYTES: number;
137
133
  export declare function iconProblem(path: string, data: Uint8Array): string | undefined;
138
134
  /**
139
135
  * Reads and validates a plugin tgz (see the file header). Invalid -> {@link PluginPackageError}:
140
- * `catalog.legacy_format` for the pre-0.1.0 format, otherwise `catalog.invalid_manifest` listing
141
- * every problem it can find, each naming the path it is about (unpacking and manifest parse
142
- * problems stop at the first stage that fails).
136
+ * `catalog.invalid_manifest` listing every problem it can find, each naming the path it is about
137
+ * (unpacking and manifest parse problems stop at the first stage that fails).
143
138
  */
144
139
  export declare function readPluginPackage(bytes: Uint8Array, opts: ReadPluginPackageOptions): Promise<PluginPackage>;
145
140
  /** Options of {@link readPluginFiles}. */
@@ -1,8 +1,8 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { AGENT_TEMPLATE_LIMITS, CATALOG_ITEM_NAME_RE, CONNECTOR_TEMPLATE_LIMITS, hostSdkProblem, PLUGIN_MANIFEST_FILE, PLUGIN_MANIFEST_LIMITS, parseAgentTemplate, parseConnectorTemplateFile, parsePluginManifest, parseSkillMd, parseThemeFile, SKILL_ENTRY_FILE, setupGuideDocPath, setupGuideDocProblem, THEME_LIMITS, themeIdOf, validatePluginPackageJsonFor, validateSetupGuide, validateThemeAssets, } from "@capybara-hq/plugin-sdk";
3
3
  import { ApiError } from "../errors.js";
4
- import { HOST_ZOD_VERSION, LEGACY_FORMAT_MESSAGE, PackageTgzError, packagePath, parsePackageJsonFile, unpackPackageTgz, } from "../packages/tgz.js";
5
- export { LEGACY_FORMAT_MESSAGE, packagePath };
4
+ import { HOST_ZOD_VERSION, PackageTgzError, packagePath, parsePackageJsonFile, unpackPackageTgz, } from "../packages/tgz.js";
5
+ export { packagePath };
6
6
  /** The plugin package is invalid; `issues` lists the problems (English, shown to the admin). */
7
7
  export class PluginPackageError extends Error {
8
8
  code;
@@ -10,13 +10,11 @@ export class PluginPackageError extends Error {
10
10
  /** The failing themes of a `theme.*` code. */
11
11
  themes;
12
12
  constructor(code, issues, themes) {
13
- super(code === "catalog.legacy_format"
14
- ? (issues[0] ?? LEGACY_FORMAT_MESSAGE)
15
- : code === "client.undeclared"
16
- ? (issues[0] ?? code)
17
- : code === "theme.invalid" || code === "theme.unreadable"
18
- ? `Theme ${themes?.[0]?.theme ?? "?"} ${code === "theme.invalid" ? "is invalid" : "is not readable"}: ${issues[0] ?? "unknown problem"}`
19
- : `Invalid plugin package: ${issues[0] ?? "unknown problem"}`);
13
+ super(code === "client.undeclared"
14
+ ? (issues[0] ?? code)
15
+ : code === "theme.invalid" || code === "theme.unreadable"
16
+ ? `Theme ${themes?.[0]?.theme ?? "?"} ${code === "theme.invalid" ? "is invalid" : "is not readable"}: ${issues[0] ?? "unknown problem"}`
17
+ : `Invalid plugin package: ${issues[0] ?? "unknown problem"}`);
20
18
  this.name = "PluginPackageError";
21
19
  this.code = code;
22
20
  this.issues = issues;
@@ -105,7 +103,6 @@ export function iconProblem(path, data) {
105
103
  return `${path} is not a UTF-8 SVG document`;
106
104
  return /<svg[\s>/]/i.test(text) ? undefined : `${path} is not an SVG document`;
107
105
  }
108
- /** package.json parsed, or undefined when it is not valid (only used to name a legacy package). */
109
106
  /**
110
107
  * Client code the extension manifest does not declare (an `extension.client` section without
111
108
  * `permissions.client: true`), read from the raw plugin.json before it is parsed, so the refusal
@@ -127,16 +124,6 @@ function clientUndeclared(text) {
127
124
  return undefined;
128
125
  return `${PLUGIN_MANIFEST_FILE}: extension.client ships client code, but the extension manifest does not declare permissions.client: true; client code must be declared so it is reviewed (capybara-plugin pack writes both)`;
129
126
  }
130
- const parsedOrUndefined = (data) => {
131
- try {
132
- return parsePackageJsonFile({ data });
133
- }
134
- catch (e) {
135
- if (e instanceof PackageTgzError)
136
- return undefined;
137
- throw e;
138
- }
139
- };
140
127
  const invalid = (issues) => {
141
128
  throw new PluginPackageError("catalog.invalid_manifest", issues);
142
129
  };
@@ -173,9 +160,8 @@ function hostSdkIssues(manifest, hostSdkVersion) {
173
160
  }
174
161
  /**
175
162
  * Reads and validates a plugin tgz (see the file header). Invalid -> {@link PluginPackageError}:
176
- * `catalog.legacy_format` for the pre-0.1.0 format, otherwise `catalog.invalid_manifest` listing
177
- * every problem it can find, each naming the path it is about (unpacking and manifest parse
178
- * problems stop at the first stage that fails).
163
+ * `catalog.invalid_manifest` listing every problem it can find, each naming the path it is about
164
+ * (unpacking and manifest parse problems stop at the first stage that fails).
179
165
  */
180
166
  export async function readPluginPackage(bytes, opts) {
181
167
  const unpacked = await step(() => unpackPackageTgz(bytes, opts));
@@ -225,20 +211,13 @@ function fallbackManifest(files, opts) {
225
211
  export function readPluginFiles(files, opts) {
226
212
  const pj = files.get("package.json");
227
213
  const manifestBytes = files.get(PLUGIN_MANIFEST_FILE);
228
- if (manifestBytes === undefined) {
229
- // The old formats are recognised only to say so; nothing in them is read. app.yaml is checked
230
- // before package.json is parsed, so an old bundle with a broken package.json is still legacy.
231
- const pkg = pj && parsedOrUndefined(pj);
232
- if (files.has("app.yaml") || (isObj(pkg) && pkg.capybara !== undefined)) {
233
- const who = isObj(pkg) && typeof pkg.name === "string" ? `Package ${pkg.name}` : "The package";
234
- throw new PluginPackageError("catalog.legacy_format", [`${who} ${LEGACY_FORMAT_MESSAGE}`]);
235
- }
236
- }
237
214
  const fallback = manifestBytes === undefined ? fallbackManifest(files, opts) : undefined;
238
215
  if (manifestBytes === undefined && fallback === undefined) {
239
216
  if (pj)
240
217
  stepSync(() => parsePackageJsonFile({ data: pj }));
241
- return invalid([`${PLUGIN_MANIFEST_FILE} not found in the package (the plugin manifest)`]);
218
+ return invalid([
219
+ `${PLUGIN_MANIFEST_FILE} not found in the package: a plugin package carries its manifest at that path (build it with \`capybara-plugin pack\`)`,
220
+ ]);
242
221
  }
243
222
  const packageJson = pj === undefined && !opts.requirePackageJson
244
223
  ? undefined
@@ -40,6 +40,7 @@ import { ConnectorsService } from "../tools/connectors.service.js";
40
40
  import { dirDigest, getPath, readCatalogAgentTemplates, readCatalogTemplates, readCatalogThemes, setPath, skillFilesDigest, templatePluginProblem, themeAssetsDigest, } from "./catalog-content.js";
41
41
  import { markVersionApplied, versionUsable } from "./catalog-items.js";
42
42
  import { agentsUsing, clashingOrgSkills, clashingPlatformSkills, componentOn, connectorsMadeFrom, connectorTemplatesOn, listedIn, loadComponents, loadScopeState, pluginFactsOf, switchOf, themeIdClashes, } from "./component-state.js";
43
+ import { LIVE_INSTALL_STATUSES } from "./install-status.js";
43
44
  import { CatalogItemLock } from "./locks.js";
44
45
  import { itemPendingError, itemRemovedError } from "./ownership.js";
45
46
  import { PlanStale, PlatformComponents } from "./platform-components.js";
@@ -603,13 +604,13 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
603
604
  }
604
605
  }
605
606
  // One version for the deployment: every live org install of the plugin moves with it (one
606
- // still installing too, and a legacy `upgrading` row; an install marked installed later
607
- // records the platform version current at its commit, PluginInstaller.markInstalled)
607
+ // still installing too; an install marked installed later records the platform version
608
+ // current at its commit, PluginInstaller.markInstalled)
608
609
  await t
609
610
  .updateTable("plugin_installs")
610
611
  .set({ version: diff.to.version, error: null })
611
612
  .where("item_id", "=", itemId)
612
- .where("status", "in", ["installing", "installed", "upgrading"])
613
+ .where("status", "in", [...LIVE_INSTALL_STATUSES])
613
614
  .execute();
614
615
  if (owner !== undefined || confirmPending !== undefined) {
615
616
  // An org's version becomes its latest once applied or confirmed (ruling P3-34)
@@ -1462,7 +1463,7 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
1462
1463
  .select("a.org_id")
1463
1464
  .distinct()
1464
1465
  .where("a.item_id", "=", itemId)
1465
- .where("a.status", "in", ["installing", "installed", "upgrading"])
1466
+ .where("a.status", "in", [...LIVE_INSTALL_STATUSES])
1466
1467
  .where("o.deleted_at", "is", null)
1467
1468
  .orderBy("a.org_id")
1468
1469
  .execute();
@@ -2,58 +2,58 @@ import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
2
  import type { OutboxService } from "../infra/outbox/outbox.service.js";
3
3
  /**
4
4
  * The plugin scope sync's jobs as the outbox owes them (transactional outbox spec §8.1): a leaf
5
- * module (type imports only), so the import confirm (`PluginImport`, below the scope service) and
6
- * the CLI owe them without the sync. `scope-sync.ts` (the consumer side) re-exports these names.
5
+ * module (type imports only), so the import confirm (`PluginImport`, below the scope service), the
6
+ * extension host's deploy (`ExtensionAdminOps`, `ExtensionAdmin`) and the CLI owe them without the
7
+ * sync. `scope-sync.ts` (the consumer side) re-exports these names.
7
8
  */
8
9
  /** The scope sync queue (no `:` in the name). */
9
10
  export declare const PLUGIN_SCOPE_QUEUE = "cap-kernel-plugin-scope";
10
- /** The job converging one item in every org. */
11
+ /** The job fanning one item out to every org: one {@link SCOPE_CHUNK_JOB} per org chunk. */
11
12
  export declare const SCOPE_ITEM_JOB = "scope-item";
13
+ /** The job converging one item in one chunk of orgs (at most {@link SCOPE_ORG_CHUNK} of them). */
14
+ export declare const SCOPE_CHUNK_JOB = "scope-item-chunk";
12
15
  /** The job converging every item in one org. */
13
16
  export declare const SCOPE_ORG_JOB = "scope-org";
17
+ /** How many orgs one chunk job converges. */
18
+ export declare const SCOPE_ORG_CHUNK = 100;
14
19
  /**
15
- * The delayed backstop of an item job owed by a confirmation's commit (ruling R24): it converges
16
- * the item as {@link SCOPE_ITEM_JOB} does once the extension is deployed, and fails (BullMQ
17
- * retries it with its backoff) while the extension is not enabled on the deployment yet.
20
+ * Optional provider (a test seam): the chunk size instead of {@link SCOPE_ORG_CHUNK}, so a test
21
+ * with a few orgs fans out to several chunks.
18
22
  */
19
- export declare const SCOPE_BACKSTOP_JOB = "scope-item-backstop";
20
- /** How long after the confirmation's commit the backstop runs (the deploy has finished by then). */
21
- export declare const SCOPE_BACKSTOP_DELAY_MS: number;
23
+ export declare const SCOPE_ORG_CHUNK_SIZE: unique symbol;
22
24
  /**
23
- * Optional provider (a test seam): the backstop's delay (ms) instead of
24
- * {@link SCOPE_BACKSTOP_DELAY_MS}, so a long test file never has a backstop fire in a later test.
25
+ * One item job per owing transaction (`xact`: its PostgreSQL transaction id, so two owes in one
26
+ * transaction are one job, and a later transaction's job is never swallowed by an earlier one
27
+ * still running; no `:`: item ids are UUIDs).
25
28
  */
26
- export declare const SCOPE_BACKSTOP_DELAY: unique symbol;
27
- /** One item job per scope version (no `:`: item ids are UUIDs). */
28
- export declare const scopeItemJobId: (itemId: string, version: number) => string;
29
- /** The backstop of the item job of a scope version (distinct from the immediate one). */
30
- export declare const scopeBackstopJobId: (itemId: string, version: number) => string;
29
+ export declare const scopeItemJobId: (itemId: string, xact: string) => string;
30
+ /** Chunk `n` of an item job (deterministic: a redelivered item job adds the same chunks). */
31
+ export declare const scopeChunkJobId: (itemJobId: string, n: number) => string;
31
32
  /** One org job at a time per org. */
32
33
  export declare const scopeOrgJobId: (orgId: string) => string;
33
- /**
34
- * The delayed retry `n` of an item job whose extension this replica had not loaded yet (ruling
35
- * P3-38); deterministic, so replicas deferring the same item add it once.
36
- */
37
- export declare const scopeRetryJobId: (itemId: string, version: number, n: number) => string;
38
- /**
39
- * The job's data. `retry`: the item job's delayed retry number (P3-38). `backstop`: a
40
- * {@link SCOPE_BACKSTOP_JOB}.
41
- */
34
+ /** The job's data. */
42
35
  export type ScopeJob = {
43
36
  kind: "item";
44
37
  itemId: string;
45
- retry?: number;
46
- backstop?: true;
38
+ } | {
39
+ kind: "chunk";
40
+ itemId: string;
41
+ orgIds: string[];
47
42
  } | {
48
43
  kind: "org";
49
44
  orgId: string;
50
45
  };
51
46
  /**
52
- * Owes the item's scope sync in the transaction in context, once per scope version (read in that
53
- * transaction, so after its own bump); `backstop` owes the delayed backstop instead, due
54
- * `backstopDelayMs` later (default {@link SCOPE_BACKSTOP_DELAY_MS}). A missing item owes nothing.
47
+ * Owes the item's scope sync in the transaction in context, one job per transaction (see
48
+ * {@link scopeItemJobId}). A missing item owes nothing.
49
+ */
50
+ export declare function oweScopeItem(outbox: Pick<OutboxService, "add">, db: KernelSchemaDb, itemId: string): Promise<void>;
51
+ /**
52
+ * Owes the scope sync of the live catalog item of an extension (its plugin, by name; at most one
53
+ * live item has a name) in the transaction that deploys it: the extension became enabled, a new
54
+ * version of an enabled one was installed, or its permissions were approved while enabled (or
55
+ * config-declared). The sync then installs it where its scope wants it on; a replica that has not
56
+ * loaded the extension yet fails the chunk job, and BullMQ retries it. A builtin item (on
57
+ * everywhere through its required extension) owes nothing.
55
58
  */
56
- export declare function oweScopeItem(outbox: Pick<OutboxService, "add">, db: KernelSchemaDb, itemId: string, opts?: {
57
- backstop?: boolean;
58
- backstopDelayMs?: number;
59
- }): Promise<void>;
59
+ export declare function oweDeployedExtensionSync(outbox: Pick<OutboxService, "add">, db: KernelSchemaDb, extensionName: string): Promise<void>;
@@ -1,65 +1,78 @@
1
+ import { sql } from "kysely";
1
2
  /**
2
3
  * The plugin scope sync's jobs as the outbox owes them (transactional outbox spec §8.1): a leaf
3
- * module (type imports only), so the import confirm (`PluginImport`, below the scope service) and
4
- * the CLI owe them without the sync. `scope-sync.ts` (the consumer side) re-exports these names.
4
+ * module (type imports only), so the import confirm (`PluginImport`, below the scope service), the
5
+ * extension host's deploy (`ExtensionAdminOps`, `ExtensionAdmin`) and the CLI owe them without the
6
+ * sync. `scope-sync.ts` (the consumer side) re-exports these names.
5
7
  */
6
8
  /** The scope sync queue (no `:` in the name). */
7
9
  export const PLUGIN_SCOPE_QUEUE = "cap-kernel-plugin-scope";
8
- /** The job converging one item in every org. */
10
+ /** The job fanning one item out to every org: one {@link SCOPE_CHUNK_JOB} per org chunk. */
9
11
  export const SCOPE_ITEM_JOB = "scope-item";
12
+ /** The job converging one item in one chunk of orgs (at most {@link SCOPE_ORG_CHUNK} of them). */
13
+ export const SCOPE_CHUNK_JOB = "scope-item-chunk";
10
14
  /** The job converging every item in one org. */
11
15
  export const SCOPE_ORG_JOB = "scope-org";
16
+ /** How many orgs one chunk job converges. */
17
+ export const SCOPE_ORG_CHUNK = 100;
12
18
  /**
13
- * The delayed backstop of an item job owed by a confirmation's commit (ruling R24): it converges
14
- * the item as {@link SCOPE_ITEM_JOB} does once the extension is deployed, and fails (BullMQ
15
- * retries it with its backoff) while the extension is not enabled on the deployment yet.
19
+ * Optional provider (a test seam): the chunk size instead of {@link SCOPE_ORG_CHUNK}, so a test
20
+ * with a few orgs fans out to several chunks.
16
21
  */
17
- export const SCOPE_BACKSTOP_JOB = "scope-item-backstop";
18
- /** How long after the confirmation's commit the backstop runs (the deploy has finished by then). */
19
- export const SCOPE_BACKSTOP_DELAY_MS = 5 * 60_000;
22
+ export const SCOPE_ORG_CHUNK_SIZE = Symbol("SCOPE_ORG_CHUNK_SIZE");
20
23
  /**
21
- * Optional provider (a test seam): the backstop's delay (ms) instead of
22
- * {@link SCOPE_BACKSTOP_DELAY_MS}, so a long test file never has a backstop fire in a later test.
24
+ * One item job per owing transaction (`xact`: its PostgreSQL transaction id, so two owes in one
25
+ * transaction are one job, and a later transaction's job is never swallowed by an earlier one
26
+ * still running; no `:`: item ids are UUIDs).
23
27
  */
24
- export const SCOPE_BACKSTOP_DELAY = Symbol("SCOPE_BACKSTOP_DELAY");
25
- /** One item job per scope version (no `:`: item ids are UUIDs). */
26
- export const scopeItemJobId = (itemId, version) => `scope-item-${itemId}-${version}`;
27
- /** The backstop of the item job of a scope version (distinct from the immediate one). */
28
- export const scopeBackstopJobId = (itemId, version) => `${scopeItemJobId(itemId, version)}-backstop`;
28
+ export const scopeItemJobId = (itemId, xact) => `scope-item-${itemId}-x${xact}`;
29
+ /** Chunk `n` of an item job (deterministic: a redelivered item job adds the same chunks). */
30
+ export const scopeChunkJobId = (itemJobId, n) => `${itemJobId}-c${n}`;
29
31
  /** One org job at a time per org. */
30
32
  export const scopeOrgJobId = (orgId) => `scope-org-${orgId}`;
33
+ /** The id of the transaction in context (assigned now if it has none yet). */
34
+ async function currentXact(db) {
35
+ const r = await sql `SELECT pg_current_xact_id()::text AS xact`.execute(db);
36
+ const xact = r.rows[0]?.xact;
37
+ if (xact === undefined)
38
+ throw new Error("no transaction id");
39
+ return xact;
40
+ }
31
41
  /**
32
- * The delayed retry `n` of an item job whose extension this replica had not loaded yet (ruling
33
- * P3-38); deterministic, so replicas deferring the same item add it once.
34
- */
35
- export const scopeRetryJobId = (itemId, version, n) => `scope-item-${itemId}-${version}-r${n}`;
36
- /**
37
- * Owes the item's scope sync in the transaction in context, once per scope version (read in that
38
- * transaction, so after its own bump); `backstop` owes the delayed backstop instead, due
39
- * `backstopDelayMs` later (default {@link SCOPE_BACKSTOP_DELAY_MS}). A missing item owes nothing.
42
+ * Owes the item's scope sync in the transaction in context, one job per transaction (see
43
+ * {@link scopeItemJobId}). A missing item owes nothing.
40
44
  */
41
- export async function oweScopeItem(outbox, db, itemId, opts = {}) {
45
+ export async function oweScopeItem(outbox, db, itemId) {
42
46
  const row = await db
43
47
  .selectFrom("catalog_items")
44
- .select("scope_version")
48
+ .select("id")
45
49
  .where("id", "=", itemId)
46
50
  .executeTakeFirst();
47
51
  if (!row)
48
52
  return;
49
- if (opts.backstop === true) {
50
- await outbox.add({
51
- queue: PLUGIN_SCOPE_QUEUE,
52
- name: SCOPE_BACKSTOP_JOB,
53
- jobId: scopeBackstopJobId(itemId, row.scope_version),
54
- data: { kind: "item", itemId, backstop: true },
55
- runAt: new Date(Date.now() + (opts.backstopDelayMs ?? SCOPE_BACKSTOP_DELAY_MS)),
56
- });
57
- return;
58
- }
59
53
  await outbox.add({
60
54
  queue: PLUGIN_SCOPE_QUEUE,
61
55
  name: SCOPE_ITEM_JOB,
62
- jobId: scopeItemJobId(itemId, row.scope_version),
56
+ jobId: scopeItemJobId(itemId, await currentXact(db)),
63
57
  data: { kind: "item", itemId },
64
58
  });
65
59
  }
60
+ /**
61
+ * Owes the scope sync of the live catalog item of an extension (its plugin, by name; at most one
62
+ * live item has a name) in the transaction that deploys it: the extension became enabled, a new
63
+ * version of an enabled one was installed, or its permissions were approved while enabled (or
64
+ * config-declared). The sync then installs it where its scope wants it on; a replica that has not
65
+ * loaded the extension yet fails the chunk job, and BullMQ retries it. A builtin item (on
66
+ * everywhere through its required extension) owes nothing.
67
+ */
68
+ export async function oweDeployedExtensionSync(outbox, db, extensionName) {
69
+ const item = await db
70
+ .selectFrom("catalog_items")
71
+ .select("id")
72
+ .where("name", "=", extensionName)
73
+ .where("deleted_at", "is", null)
74
+ .where("builtin", "=", false)
75
+ .executeTakeFirst();
76
+ if (item)
77
+ await oweScopeItem(outbox, db, item.id);
78
+ }
@@ -1,7 +1,7 @@
1
1
  import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
2
  /**
3
3
  * Adds one org to a `named` item's list in the caller's transaction (the caller holds the item row
4
- * `FOR UPDATE`): bumps `scope_version` and audits `catalog.scope_changed` the way a scope change
4
+ * `FOR UPDATE`): audits `catalog.scope_changed` the way a scope change
5
5
  * does (`from` / `to` with the sorted lists; `requestId` when a request asked for it). Nothing is
6
6
  * written when the item is not `named` (or builtin) or the org is listed already. Returns whether
7
7
  * the list changed; the caller enqueues the item's scope sync after the commit. Adding an org other
@@ -4,7 +4,7 @@ import { notifyItemShared } from "../inbox/org-notices.js";
4
4
  import { assertShareable, shareGate } from "./ownership.js";
5
5
  /**
6
6
  * Adds one org to a `named` item's list in the caller's transaction (the caller holds the item row
7
- * `FOR UPDATE`): bumps `scope_version` and audits `catalog.scope_changed` the way a scope change
7
+ * `FOR UPDATE`): audits `catalog.scope_changed` the way a scope change
8
8
  * does (`from` / `to` with the sorted lists; `requestId` when a request asked for it). Nothing is
9
9
  * written when the item is not `named` (or builtin) or the org is listed already. Returns whether
10
10
  * the list changed; the caller enqueues the item's scope sync after the commit. Adding an org other
@@ -36,7 +36,6 @@ export async function addScopeOrg(db, actor, item, orgId, opts = {}) {
36
36
  await db
37
37
  .updateTable("catalog_items")
38
38
  .set({
39
- scope_version: sql `scope_version + 1`,
40
39
  updated_at: sql `now()`,
41
40
  })
42
41
  .where("id", "=", item.id)