@capybara-hq/kernel 0.1.0-rc.121 → 0.1.0-rc.123

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 (230) hide show
  1. package/bootstrap.json +3 -3
  2. package/dist/background/finish.d.ts +5 -1
  3. package/dist/background/finish.js +29 -18
  4. package/dist/background/launch.d.ts +0 -5
  5. package/dist/background/launch.js +1 -6
  6. package/dist/background/managed.js +1 -4
  7. package/dist/background/poll.job.d.ts +3 -25
  8. package/dist/background/poll.job.js +20 -112
  9. package/dist/background/render.d.ts +2 -8
  10. package/dist/background/render.js +3 -17
  11. package/dist/background/stop.d.ts +15 -16
  12. package/dist/background/stop.js +39 -76
  13. package/dist/background/store.d.ts +17 -45
  14. package/dist/background/store.js +60 -94
  15. package/dist/background/tasks.service.d.ts +2 -5
  16. package/dist/background/tasks.service.js +9 -20
  17. package/dist/background/wake.js +3 -2
  18. package/dist/catalog/admin-catalog.controller.d.ts +2 -18
  19. package/dist/catalog/admin-catalog.controller.js +2 -28
  20. package/dist/catalog/catalog-content.d.ts +1 -5
  21. package/dist/catalog/catalog-items.d.ts +2 -6
  22. package/dist/catalog/catalog-items.js +5 -26
  23. package/dist/catalog/catalog-scope.service.d.ts +1 -121
  24. package/dist/catalog/catalog-scope.service.js +12 -552
  25. package/dist/catalog/catalog.service.d.ts +4 -15
  26. package/dist/catalog/catalog.service.js +7 -35
  27. package/dist/catalog/job-ids.d.ts +0 -18
  28. package/dist/catalog/job-ids.js +0 -20
  29. package/dist/catalog/marketplaces.service.d.ts +1 -1
  30. package/dist/catalog/org-catalog.service.js +6 -20
  31. package/dist/catalog/org-plugin-uploads.controller.d.ts +4 -19
  32. package/dist/catalog/org-plugin-uploads.controller.js +6 -34
  33. package/dist/catalog/org-plugin-uploads.d.ts +3 -10
  34. package/dist/catalog/org-plugin-uploads.js +4 -18
  35. package/dist/catalog/org-plugin-uploads.schemas.js +0 -1
  36. package/dist/catalog/owned-items.d.ts +6 -11
  37. package/dist/catalog/owned-items.js +10 -20
  38. package/dist/catalog/ownership.d.ts +3 -29
  39. package/dist/catalog/ownership.js +4 -54
  40. package/dist/catalog/platform-components.d.ts +5 -11
  41. package/dist/catalog/platform-components.js +8 -15
  42. package/dist/catalog/plugin-import.d.ts +3 -42
  43. package/dist/catalog/plugin-import.js +26 -195
  44. package/dist/catalog/plugin-install.js +1 -6
  45. package/dist/catalog/plugin-package.d.ts +1 -0
  46. package/dist/catalog/plugin-package.js +5 -0
  47. package/dist/catalog/plugin-upgrade.d.ts +3 -26
  48. package/dist/catalog/plugin-upgrade.js +20 -103
  49. package/dist/catalog/request-rules.js +4 -6
  50. package/dist/catalog/scope-sync.js +2 -4
  51. package/dist/catalog/scope.d.ts +2 -5
  52. package/dist/channels/jobs.d.ts +3 -3
  53. package/dist/channels/jobs.js +4 -3
  54. package/dist/channels/outbound.d.ts +1 -0
  55. package/dist/channels/outbound.js +32 -8
  56. package/dist/config/load.d.ts +0 -25
  57. package/dist/config/load.js +0 -57
  58. package/dist/config/schema.js +2 -3
  59. package/dist/conversations/attachments.service.js +3 -5
  60. package/dist/conversations/conversations.service.d.ts +3 -3
  61. package/dist/conversations/conversations.service.js +3 -3
  62. package/dist/conversations/job-org.d.ts +3 -0
  63. package/dist/conversations/job-org.js +11 -0
  64. package/dist/conversations/prompt-suggestions.d.ts +2 -7
  65. package/dist/conversations/prompt-suggestions.js +29 -27
  66. package/dist/conversations/titles.d.ts +3 -4
  67. package/dist/conversations/titles.js +19 -30
  68. package/dist/db/kernel-schema.d.ts +6 -16
  69. package/dist/db/pool.js +7 -2
  70. package/dist/devices/devices.controller.d.ts +0 -5
  71. package/dist/devices/devices.controller.js +2 -20
  72. package/dist/devices/devices.service.d.ts +1 -17
  73. package/dist/devices/devices.service.js +0 -22
  74. package/dist/errors.d.ts +1 -5
  75. package/dist/errors.js +1 -5
  76. package/dist/evals/judge.d.ts +3 -0
  77. package/dist/evals/judge.js +1 -1
  78. package/dist/evals/run-replay.d.ts +1 -1
  79. package/dist/evals/run-replay.js +26 -29
  80. package/dist/evals/step-replay.d.ts +3 -1
  81. package/dist/evals/step-replay.js +10 -1
  82. package/dist/evals/worker.d.ts +3 -1
  83. package/dist/evals/worker.js +53 -21
  84. package/dist/extensions/deployment-config-store.js +18 -14
  85. package/dist/extensions/effective-config.d.ts +1 -5
  86. package/dist/extensions/effective-config.js +1 -5
  87. package/dist/extensions/hooks.d.ts +3 -1
  88. package/dist/extensions/hooks.js +7 -2
  89. package/dist/extensions/installs.d.ts +2 -14
  90. package/dist/extensions/installs.js +10 -32
  91. package/dist/extensions/org-config.d.ts +3 -4
  92. package/dist/extensions/org-config.js +3 -4
  93. package/dist/extensions/plugin-guides.d.ts +0 -1
  94. package/dist/extensions/plugin-guides.js +0 -1
  95. package/dist/extensions/plugin-icons.d.ts +0 -1
  96. package/dist/extensions/plugin-icons.js +0 -3
  97. package/dist/files/blob-sweep.job.d.ts +2 -11
  98. package/dist/files/blob-sweep.job.js +7 -21
  99. package/dist/files/hash-job.d.ts +5 -15
  100. package/dist/files/hash-job.js +14 -33
  101. package/dist/files/library-jobs.module.d.ts +1 -6
  102. package/dist/files/library-jobs.module.js +3 -9
  103. package/dist/identity/api-tokens.js +54 -40
  104. package/dist/identity/auth.guard.js +2 -2
  105. package/dist/identity/sessions.js +7 -3
  106. package/dist/identity/settings.js +5 -21
  107. package/dist/inbox/inbox.service.d.ts +2 -3
  108. package/dist/inbox/org-notices.d.ts +0 -14
  109. package/dist/inbox/platform-inbox.service.d.ts +1 -8
  110. package/dist/inbox/platform-inbox.service.js +2 -6
  111. package/dist/inbox/platform-notices.d.ts +3 -3
  112. package/dist/index.d.ts +0 -4
  113. package/dist/index.js +0 -3
  114. package/dist/infra/database/database.module.js +2 -1
  115. package/dist/infra/database/org-scope.d.ts +5 -3
  116. package/dist/infra/database/org-scope.js +7 -1
  117. package/dist/infra/database/scoped-dialect.d.ts +3 -1
  118. package/dist/infra/database/scoped-dialect.js +17 -5
  119. package/dist/infra/events/kernel-events.d.ts +2 -0
  120. package/dist/infra/outbox/outbox.service.d.ts +2 -0
  121. package/dist/infra/outbox/outbox.service.js +10 -0
  122. package/dist/infra/outbox/registry.d.ts +0 -41
  123. package/dist/infra/outbox/registry.js +0 -37
  124. package/dist/models/embeddings.d.ts +0 -5
  125. package/dist/models/embeddings.js +28 -35
  126. package/dist/models/images.d.ts +3 -8
  127. package/dist/models/images.js +19 -37
  128. package/dist/models/purposes.d.ts +7 -2
  129. package/dist/models/purposes.js +16 -3
  130. package/dist/orgs/device-network-setting.d.ts +0 -44
  131. package/dist/orgs/device-network-setting.js +0 -83
  132. package/dist/orgs/invitations.service.js +34 -30
  133. package/dist/orgs/offboarding.d.ts +2 -2
  134. package/dist/orgs/offboarding.js +2 -2
  135. package/dist/orgs/orgs.service.d.ts +1 -1
  136. package/dist/orgs/orgs.service.js +19 -15
  137. package/dist/orgs/settings.service.d.ts +19 -14
  138. package/dist/orgs/settings.service.js +12 -37
  139. package/dist/platform/exclusions.js +0 -11
  140. package/dist/platform/ops/catalog-admin.ops.js +0 -8
  141. package/dist/platform/ops/devices.ops.js +0 -28
  142. package/dist/platform/ops/extensions-admin.ops.js +3 -3
  143. package/dist/platform/ops/inbox.ops.js +5 -22
  144. package/dist/platform/ops/org-plugins.ops.d.ts +1 -3
  145. package/dist/platform/ops/org-plugins.ops.js +11 -67
  146. package/dist/roles/worker.module.js +0 -9
  147. package/dist/runtime/hook-calls.d.ts +13 -6
  148. package/dist/runtime/hook-calls.js +26 -4
  149. package/dist/runtime/maintenance.d.ts +3 -12
  150. package/dist/runtime/maintenance.js +25 -55
  151. package/dist/runtime/model-call.d.ts +4 -2
  152. package/dist/runtime/model-call.js +59 -58
  153. package/dist/runtime/providers.d.ts +27 -1
  154. package/dist/runtime/providers.js +57 -0
  155. package/dist/runtime/run-jobs.d.ts +1 -1
  156. package/dist/runtime/run-processors.d.ts +3 -9
  157. package/dist/runtime/run-worker.d.ts +2 -0
  158. package/dist/runtime/run-worker.js +23 -8
  159. package/dist/runtime/subagents/deliveries.js +6 -1
  160. package/dist/runtime/subagents/dispatch.d.ts +3 -3
  161. package/dist/runtime/write-base.service.d.ts +2 -3
  162. package/dist/runtime/write-base.service.js +3 -5
  163. package/dist/sandbox/device-sandbox-settings.d.ts +38 -92
  164. package/dist/sandbox/device-sandbox-settings.js +19 -325
  165. package/dist/sandbox/jobs.d.ts +3 -14
  166. package/dist/sandbox/jobs.js +2 -20
  167. package/dist/sandbox/modes.d.ts +5 -45
  168. package/dist/sandbox/modes.js +3 -60
  169. package/dist/sandbox/operation-authority.js +0 -11
  170. package/dist/sandbox/operation-intents.d.ts +32 -9
  171. package/dist/sandbox/operation-intents.js +241 -32
  172. package/dist/sandbox/operation-intents.module.js +2 -1
  173. package/dist/sandbox/operation-reconcile.job.d.ts +9 -11
  174. package/dist/sandbox/operation-reconcile.job.js +119 -33
  175. package/dist/sandbox/operation-revoke.d.ts +20 -0
  176. package/dist/sandbox/operation-revoke.js +3 -0
  177. package/dist/sandbox/permission-ceiling-writes.d.ts +1 -2
  178. package/dist/sandbox/permission-ceiling-writes.js +3 -6
  179. package/dist/sandbox/permission-config-publication.d.ts +1 -28
  180. package/dist/sandbox/permission-config-publication.js +14 -211
  181. package/dist/sandbox/permission-facts.d.ts +4 -15
  182. package/dist/sandbox/permission-facts.js +7 -29
  183. package/dist/sandbox/permission-instance-writes.d.ts +1 -4
  184. package/dist/sandbox/permission-instance-writes.js +1 -55
  185. package/dist/sandbox/providers.d.ts +1 -6
  186. package/dist/sandbox/providers.js +1 -15
  187. package/dist/sandbox/review/classifier.js +12 -13
  188. package/dist/sandbox/sandbox.module.js +3 -6
  189. package/dist/sandbox/sandbox.service.d.ts +17 -123
  190. package/dist/sandbox/sandbox.service.js +239 -998
  191. package/dist/shares/liveness.d.ts +1 -2
  192. package/dist/shares/liveness.js +1 -2
  193. package/dist/shares/shares.module.d.ts +1 -2
  194. package/dist/shares/shares.module.js +2 -5
  195. package/dist/shares/shares.service.js +1 -2
  196. package/dist/tools/builtins/image/generate.js +7 -15
  197. package/dist/tools/builtins/sandbox/common.d.ts +0 -2
  198. package/dist/tools/builtins/sandbox/common.js +1 -27
  199. package/dist/tools/builtins/sandbox/task-list.js +10 -10
  200. package/dist/tools/builtins/sandbox/task-output.d.ts +3 -10
  201. package/dist/tools/builtins/sandbox/task-output.js +13 -83
  202. package/dist/tools/builtins/sandbox/task-stop.js +18 -22
  203. package/dist/tools/builtins/vision/read-image.js +9 -10
  204. package/dist/tools/builtins/web/extract.js +9 -10
  205. package/package.json +3 -3
  206. package/src/migrations/0206_retire_org_extension_uploads.sql +125 -0
  207. package/src/migrations/0207_confirm_device_instance_destruction.sql +25 -0
  208. package/src/migrations/0208_convert_stored_permission_settings.sql +189 -0
  209. package/dist/background/retire-legacy-tasks.job.d.ts +0 -57
  210. package/dist/background/retire-legacy-tasks.job.js +0 -149
  211. package/dist/catalog/owned-plugin-cleanup.d.ts +0 -47
  212. package/dist/catalog/owned-plugin-cleanup.js +0 -140
  213. package/dist/config/permission-migration.d.ts +0 -34
  214. package/dist/config/permission-migration.js +0 -115
  215. package/dist/devices/legacy-cleanup.job.d.ts +0 -34
  216. package/dist/devices/legacy-cleanup.job.js +0 -147
  217. package/dist/files/library-backfill.d.ts +0 -63
  218. package/dist/files/library-backfill.js +0 -143
  219. package/dist/infra/cli/permission-cutover.d.ts +0 -59
  220. package/dist/infra/cli/permission-cutover.js +0 -107
  221. package/dist/sandbox/cloud-permission-snapshot.d.ts +0 -159
  222. package/dist/sandbox/cloud-permission-snapshot.js +0 -430
  223. package/dist/sandbox/cloud-permission-snapshot.module.d.ts +0 -8
  224. package/dist/sandbox/cloud-permission-snapshot.module.js +0 -34
  225. package/dist/sandbox/legacy-retirement.d.ts +0 -284
  226. package/dist/sandbox/legacy-retirement.js +0 -1165
  227. package/dist/sandbox/legacy-retirement.module.d.ts +0 -8
  228. package/dist/sandbox/legacy-retirement.module.js +0 -25
  229. package/dist/shares/share-objects.d.ts +0 -47
  230. package/dist/shares/share-objects.js +0 -93
@@ -13,38 +13,27 @@ import { sql } from "kysely";
13
13
  import { ApiError } from "../errors.js";
14
14
  import { ExtensionAdminOps } from "../extensions/admin-ops.js";
15
15
  import { ExtensionAdmin } from "../extensions/admin-view.js";
16
- import { PluginInstallLock } from "../extensions/locks.js";
17
16
  import { audit } from "../identity/audit.js";
18
17
  import { SuperAdminLock } from "../identity/super-admin.js";
19
- import { addOrgNotice, itemDisplayName, notifyItemShared } from "../inbox/org-notices.js";
18
+ import { notifyItemShared } from "../inbox/org-notices.js";
20
19
  import { KernelDb } from "../infra/database/kernel-db.js";
21
20
  import { TransactionHost } from "../infra/database/transaction-host.js";
22
21
  import { Transactional } from "../infra/database/transactional.decorator.js";
23
- import { OutboxService } from "../infra/outbox/outbox.service.js";
24
- import { KERNEL_LOGGER } from "../infra/tokens.js";
25
- import { OrgLock } from "../orgs/org-lock.js";
26
22
  import { assertNotPlatformDefault, themesPreview, } from "../themes/theme-usage.js";
27
23
  import { CatalogService } from "./catalog.service.js";
28
- import { assertOrgsExist, markVersionApplied } from "./catalog-items.js";
24
+ import { assertOrgsExist } from "./catalog-items.js";
29
25
  import { nameConflictError } from "./component-scope.service.js";
30
26
  import { agentsUsing, COMPONENT_KINDS, componentOffered, componentOn, listedIn, loadComponents, loadScopeState, pluginFactsOf, skillNameClashes, switchOf, themeIdClashes, } from "./component-state.js";
31
27
  import { LIVE_INSTALL_STATUSES } from "./install-status.js";
32
- import { oweOwnedPluginCleanup } from "./job-ids.js";
33
- import { CatalogItemLock } from "./locks.js";
34
- import { assertNotAwaiting, assertOwnerListed, assertShareable, assertShareableTarget, itemRemovedError, newlySharedWith, ownerOf, ownersOf, PLATFORM_OWNER, packageOwner, pendingDigest, releaseExtensionClaim, } from "./ownership.js";
35
- import { PlanStale, PlatformComponents } from "./platform-components.js";
28
+ import { assertOwnerListed, assertShareable, assertShareableTarget, newlySharedWith, } from "./ownership.js";
36
29
  import { PluginImport } from "./plugin-import.js";
37
30
  import { PluginInstaller } from "./plugin-install.js";
38
- import { PlatformPluginUpdater } from "./plugin-upgrade.js";
39
- import { cancelPendingRequests } from "./request-rules.js";
40
31
  import { wantedOn } from "./scope.js";
41
32
  import { addScopeOrg } from "./scope-orgs.js";
42
33
  import { PluginScopeSync } from "./scope-sync.js";
43
34
  import { narrowComponentOnUnshare, narrowItemOnUnshare } from "./unshare.js";
44
35
  const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
45
36
  const notFound = () => new ApiError("catalog.item_not_found", "Catalog item not found");
46
- const notPending = (name) => new ApiError("extension.not_pending", `Plugin ${name} has no upload waiting for confirmation`);
47
- const pendingStale = (name) => new ApiError("extension.approval_stale", `The pending upload of ${name} changed since it was reviewed; review it again`);
48
37
  const builtinError = () => new ApiError("plugin.builtin", "This plugin is built in: it is on in every organization and its scope cannot be changed or disabled");
49
38
  const staleError = () => new ApiError("plugin.scope_stale", "What this change affects has changed since the preview; preview it again and confirm the new hash");
50
39
  /** The hash of a preview: sha256 over its sorted JSON, the target included. */
@@ -99,44 +88,20 @@ let CatalogScopeService = class CatalogScopeService {
99
88
  adminLock;
100
89
  catalog;
101
90
  pluginImport;
102
- platform;
103
- itemLock;
104
91
  installer;
105
92
  extensions;
106
93
  ops;
107
94
  sync;
108
- updater;
109
- orgLock;
110
- installLock;
111
- outbox;
112
- log;
113
95
  txHost;
114
- constructor(kdb, adminLock, catalog, pluginImport,
115
- /** A confirmed first version's platform components (written in the confirming commit). */
116
- platform,
117
- /** The import's item name lock, taken by the confirming commit before the item row. */
118
- itemLock, installer, extensions, ops, sync, updater,
119
- /** The owner org's lock: an org admin withdrawing its pending upload (ruling P3-10). */
120
- orgLock,
121
- /** The per-name install lock a reject / withdraw releases the name claim under (M1). */
122
- installLock,
123
- /** Owes the scope syncs and the removed org plugin's cleanup (P3-21, pre-flight F9). */
124
- outbox, log, txHost) {
96
+ constructor(kdb, adminLock, catalog, pluginImport, installer, extensions, ops, sync, txHost) {
125
97
  this.kdb = kdb;
126
98
  this.adminLock = adminLock;
127
99
  this.catalog = catalog;
128
100
  this.pluginImport = pluginImport;
129
- this.platform = platform;
130
- this.itemLock = itemLock;
131
101
  this.installer = installer;
132
102
  this.extensions = extensions;
133
103
  this.ops = ops;
134
104
  this.sync = sync;
135
- this.updater = updater;
136
- this.orgLock = orgLock;
137
- this.installLock = installLock;
138
- this.outbox = outbox;
139
- this.log = log;
140
105
  this.txHost = txHost;
141
106
  }
142
107
  get db() {
@@ -158,32 +123,20 @@ let CatalogScopeService = class CatalogScopeService {
158
123
  * store from the import). Nothing waiting (a covered snapshot a person approved already, an item
159
124
  * without an extension) -> 409 `extension.not_pending`. The item's scope sync is owed by the
160
125
  * approval of an enabled extension, or by the enable.
161
- *
162
- * An org-owned item is confirmed only while an upload of it waits (ruling P3-10, see
163
- * {@link confirmOrgUpload}); otherwise -> 409 `extension.not_pending`.
164
126
  */
165
127
  async confirmPermissions(actor, itemId, permissionsHash, opts = {}) {
166
128
  if (!UUID_RE.test(itemId))
167
129
  throw notFound();
168
130
  const item = await this.db
169
131
  .selectFrom("catalog_items")
170
- .select([
171
- "id",
172
- "name",
173
- "latest_version",
174
- "builtin",
175
- "owner_org_id",
176
- "awaiting_confirmation",
177
- "pending_version",
178
- "scope",
179
- ])
132
+ .select(["id", "name", "latest_version", "builtin", "owner_org_id", "scope"])
180
133
  .where("id", "=", itemId)
181
134
  .where("deleted_at", "is", null)
182
135
  .executeTakeFirst();
183
136
  if (!item)
184
137
  throw notFound();
185
138
  if (item.owner_org_id !== null) {
186
- return this.confirmOrgUpload(actor, { id: item.id, name: item.name }, permissionsHash, opts);
139
+ throw new ApiError("extension.not_pending", `Plugin ${item.name} has no extension to confirm`);
187
140
  }
188
141
  const version = await this.db
189
142
  .selectFrom("catalog_item_versions")
@@ -227,466 +180,6 @@ let CatalogScopeService = class CatalogScopeService {
227
180
  await this.ops.enableUnlessOccupied(item.name, actor, opts.actorTokenId);
228
181
  return (await this.catalog.get(itemId)).item;
229
182
  }
230
- /**
231
- * The super admin's confirmation of an org upload's pending version (rulings P3-10, P3-11,
232
- * P3-21): `permissionsHash` must be the digest of that version's package ({@link pendingDigest};
233
- * another -> 409 `extension.approval_stale`, also for a version a later upload replaced); no
234
- * pending version (withdrawn, rejected, replaced by one without an extension) -> 409
235
- * `extension.not_pending`.
236
- *
237
- * A first version: its platform components planned and their files copied (keyed by content),
238
- * its extension installed, its permissions approved, enabled, then (one transaction) the item
239
- * stops waiting, its platform component rows are written and the owner is told
240
- * (`upload_confirmed`) and the scope sync is owed in that commit. Each step re-reads
241
- * the item under its row lock: removed meanwhile -> 409 `catalog.item_removed`, the pending
242
- * version replaced -> `extension.approval_stale`; the extension this confirmation installed is
243
- * then taken out again. A new version: the platform update to it
244
- * ({@link PlatformPluginUpdater.updateToPending}, which installs or upgrades the extension,
245
- * clears the pending state, tells the owner and the orgs using a shared plugin), then its
246
- * permissions approved.
247
- */
248
- async confirmOrgUpload(actor, item, permissionsHash, opts) {
249
- // Every step under the item's update lock (ruling P3-35): a second confirmation waits its
250
- // turn out as 409 plugin.install_in_progress and never undoes the first one's install
251
- await this.updater.withItemLock(item.id, () => this.confirmLocked(actor, item.id, item.name, permissionsHash, opts));
252
- return (await this.catalog.get(item.id)).item;
253
- }
254
- /**
255
- * {@link confirmOrgUpload} under the item lock.
256
- */
257
- async confirmLocked(actor, itemId, name, permissionsHash, opts) {
258
- // Read again under the lock (the caller's read was outside it)
259
- const item = await this.db
260
- .selectFrom("catalog_items")
261
- .select([
262
- "deleted_at",
263
- "owner_org_id",
264
- "awaiting_confirmation",
265
- "pending_version",
266
- "pending_by",
267
- "pending_at",
268
- "package_name",
269
- "platform_synced_version",
270
- ])
271
- .where("id", "=", itemId)
272
- .executeTakeFirst();
273
- if (!item || item.deleted_at !== null || item.owner_org_id === null) {
274
- throw itemRemovedError(name);
275
- }
276
- const version = item.pending_version;
277
- if (version === null)
278
- throw notPending(name);
279
- const row = await this.db
280
- .selectFrom("catalog_item_versions")
281
- .select("extension_sha256")
282
- .where("item_id", "=", itemId)
283
- .where("version", "=", version)
284
- .executeTakeFirst();
285
- const sha = row?.extension_sha256 ?? null;
286
- if (sha === null)
287
- throw notPending(name);
288
- const reviewHash = await this.extensions.packageReviewHash(sha);
289
- if (reviewHash === null || pendingDigest(sha, reviewHash) !== permissionsHash) {
290
- throw pendingStale(name);
291
- }
292
- // An install of this name that is not this item's (left by an earlier item, of another
293
- // package or of the same package name under another owner): the org's code never inherits its
294
- // deployment config, secrets or data (rulings P3-36, P3-51). This item's own install, or one
295
- // its owner's removed item of the same package left, is upgraded in place.
296
- const install = await this.db
297
- .selectFrom("extension_installs as x")
298
- .innerJoin("extension_packages as p", "p.sha256", "x.sha256")
299
- .leftJoin("catalog_items as ci", "ci.id", "x.catalog_item_id")
300
- .select(["x.sha256", "x.catalog_item_id", "p.package_name", "ci.owner_org_id"])
301
- .where("x.name", "=", name)
302
- .executeTakeFirst();
303
- const own = install === undefined ||
304
- (install.package_name === item.package_name &&
305
- (install.catalog_item_id === itemId ||
306
- (install.catalog_item_id !== null && install.owner_org_id === item.owner_org_id)));
307
- if (install !== undefined && !own) {
308
- // who holds it: the install's own item's owner, else the package's (none: the platform)
309
- const holderOrg = install.catalog_item_id === null ? null : install.owner_org_id;
310
- const holder = install.catalog_item_id !== null
311
- ? ownerOf(holderOrg, await ownersOf(this.db, holderOrg === null ? [] : [holderOrg]))
312
- : install.package_name !== item.package_name
313
- ? await packageOwner(this.db, install.package_name, name)
314
- : PLATFORM_OWNER;
315
- throw new ApiError("extension.name_conflict", install.package_name !== item.package_name
316
- ? `Extension ${name} is installed from package ${install.package_name}; the upload's package ${item.package_name ?? "(none)"} cannot take it over`
317
- : `Extension ${name} is installed for another plugin of package ${install.package_name}; this upload cannot take it over`, [{ owner: holder }]);
318
- }
319
- if (!item.awaiting_confirmation) {
320
- const pending = {
321
- version,
322
- by: item.pending_by,
323
- at: item.pending_at,
324
- };
325
- // A new version: applied (unless an earlier confirmation applied it and stopped before the
326
- // approval), then its permissions approved; an approval that fails puts the pending state
327
- // back, so the version shows in 待确认 again (ruling P3-37)
328
- try {
329
- if (item.platform_synced_version !== version) {
330
- await this.updater.updateToPendingLocked(actor, itemId, version, opts);
331
- }
332
- else {
333
- await this.clearPending(itemId, version);
334
- }
335
- try {
336
- await this.approvePackage(actor, name, sha, reviewHash, opts);
337
- }
338
- catch (e) {
339
- await this.restorePending(itemId, pending);
340
- throw e;
341
- }
342
- }
343
- catch (e) {
344
- throw await this.removalOr(e, itemId, name);
345
- }
346
- return;
347
- }
348
- await this.stillPending(itemId, name, version);
349
- let installedHere = false;
350
- try {
351
- // Planned and copied before any transaction; the rows are written by the confirming commit
352
- const components = await this.platform.prepareSync(itemId, version, { confirming: true });
353
- if (install?.sha256 !== sha) {
354
- await this.ops.install(name, sha, actor, opts.actorTokenId, {
355
- catalogItemId: itemId,
356
- liveItem: true,
357
- });
358
- installedHere = install === undefined;
359
- }
360
- await this.stillPending(itemId, name, version);
361
- await this.approvePackage(actor, name, sha, reviewHash, opts);
362
- await this.stillPending(itemId, name, version);
363
- // An occupied single-provider point leaves it installed disabled (the admin view says why)
364
- await this.ops.enableUnlessOccupied(name, actor, opts.actorTokenId);
365
- await this.finishFirstVersion(actor, itemId, version, components, opts);
366
- }
367
- catch (e) {
368
- // Taken out only when this confirmation did not win: the item gone or still waiting
369
- if (installedHere && !(await this.confirmed(itemId))) {
370
- await this.takeOut(itemId, name, actor, opts);
371
- }
372
- throw await this.removalOr(e, itemId, name);
373
- }
374
- }
375
- /** The item is live and its first version confirmed (no longer awaiting). */
376
- async confirmed(itemId) {
377
- const row = await this.db
378
- .selectFrom("catalog_items")
379
- .select(["deleted_at", "awaiting_confirmation"])
380
- .where("id", "=", itemId)
381
- .executeTakeFirst();
382
- return row !== undefined && row.deleted_at === null && !row.awaiting_confirmation;
383
- }
384
- /**
385
- * The error a failed confirmation step answers with: 409 `catalog.item_removed` when the item is
386
- * gone or soft-deleted by now, whichever step failed (the removal's cleanup job may take the
387
- * extension out under the confirmation, so a step answers `extension.not_found`), the original
388
- * error logged; else (or when the item cannot be read) the original error.
389
- */
390
- async removalOr(e, itemId, name) {
391
- if (e instanceof ApiError && e.code === "catalog.item_removed")
392
- return e;
393
- let removed;
394
- try {
395
- const row = await this.db
396
- .selectFrom("catalog_items")
397
- .select("deleted_at")
398
- .where("id", "=", itemId)
399
- .executeTakeFirst();
400
- removed = row === undefined || row.deleted_at !== null;
401
- }
402
- catch (err) {
403
- this.log.warn("cannot read the item of a failed confirmation", { itemId, err });
404
- return e;
405
- }
406
- if (!removed)
407
- return e;
408
- if (e instanceof ApiError) {
409
- this.log.info("a confirmation step failed after its item was removed", {
410
- itemId,
411
- name,
412
- code: e.code,
413
- });
414
- }
415
- else {
416
- this.log.warn("a confirmation step failed after its item was removed", {
417
- itemId,
418
- name,
419
- err: e,
420
- });
421
- }
422
- return itemRemovedError(name);
423
- }
424
- /** Clears the pending state of a version an earlier confirmation applied already. */
425
- async clearPending(itemId, version) {
426
- await this.db
427
- .updateTable("catalog_items")
428
- .set({ pending_version: null, pending_by: null, pending_at: null })
429
- .where("id", "=", itemId)
430
- .where("pending_version", "=", version)
431
- .execute();
432
- }
433
- /**
434
- * Puts a confirmed new version's pending state back after its approval failed (ruling P3-37):
435
- * the version applied, its permissions still waiting, 待确认 shows it again. Not when another
436
- * upload is pending meanwhile.
437
- */
438
- async restorePending(itemId, p) {
439
- await this.db
440
- .updateTable("catalog_items")
441
- .set({ pending_version: p.version, pending_by: p.by, pending_at: p.at })
442
- .where("id", "=", itemId)
443
- .where("deleted_at", "is", null)
444
- .where("pending_version", "is", null)
445
- .execute();
446
- }
447
- /** Approves the permissions of the installed package `sha` (already covered: nothing to do). */
448
- async approvePackage(actor, name, sha, reviewHash, opts) {
449
- try {
450
- await this.extensions.approve(actor, name, { sha256: sha, reviewHash }, opts);
451
- }
452
- catch (e) {
453
- if (!(e instanceof ApiError && e.code === "extension.not_pending"))
454
- throw e;
455
- }
456
- }
457
- /**
458
- * An extension a confirmation installed whose item was removed or replaced meanwhile is taken
459
- * out again (disabled, uninstalled; P3-21), and the name claim released when the item is gone
460
- * (ruling P3-37), in one transaction ({@link takeOutIn}). Inline (a failure is logged): when the
461
- * take-out fails, a removed item's cleanup job (`owned-plugin-cleanup-<itemId>`, idempotent) is
462
- * owed instead, so the job retries it (pre-flight F9). That owe is the last resort: when it
463
- * fails too (PG unavailable) nothing retries the take-out, and it is logged as an error.
464
- */
465
- async takeOut(itemId, name, actor, opts) {
466
- try {
467
- await this.takeOutIn(itemId, name, actor, opts);
468
- return;
469
- }
470
- catch (err) {
471
- this.log.error("taking out the extension of a removed org upload failed", { name, err });
472
- }
473
- try {
474
- await this.oweCleanupIfGone(itemId);
475
- }
476
- catch (err) {
477
- this.log.error("owing the owned plugin cleanup failed; the take-out is not retried", {
478
- itemId,
479
- err,
480
- });
481
- }
482
- }
483
- /**
484
- * {@link takeOut}'s transaction, in the global lock order (ruling P3-50): the super admin lock,
485
- * the item row, then the extension's install lock (taken by the disable): disabled,
486
- * uninstalled, and the name claim released when the item is gone.
487
- */
488
- async takeOutIn(itemId, name, actor, opts) {
489
- await this.adminLock.lockAndAuthorize(actor);
490
- const row = await this.db
491
- .selectFrom("catalog_items")
492
- .select(["deleted_at", "package_name"])
493
- .where("id", "=", itemId)
494
- .forUpdate()
495
- .executeTakeFirst();
496
- await this.ops.disable(name, actor, opts.actorTokenId);
497
- await this.ops.uninstall(name, actor, opts.actorTokenId);
498
- if (row === undefined || row.deleted_at !== null) {
499
- await releaseExtensionClaim(this.db, name, row?.package_name ?? null);
500
- }
501
- }
502
- /**
503
- * Owes the cleanup of a removed item; a live item owes nothing (a removal committing later owes
504
- * its own).
505
- */
506
- async oweCleanupIfGone(itemId) {
507
- const gone = await this.db
508
- .selectFrom("catalog_items")
509
- .select("id")
510
- .where("id", "=", itemId)
511
- .where("deleted_at", "is not", null)
512
- .executeTakeFirst();
513
- if (gone !== undefined)
514
- await oweOwnedPluginCleanup(this.outbox, itemId);
515
- }
516
- /** The item is still live and pending with `version` (row lock), else 409 (P3-21). */
517
- async stillPending(itemId, name, version) {
518
- await this.pendingRow(itemId, name, version);
519
- }
520
- /** {@link stillPending} in the caller's transaction; returns the owner org. */
521
- async pendingRow(itemId, name, version) {
522
- const row = await this.db
523
- .selectFrom("catalog_items")
524
- .select(["deleted_at", "pending_version", "owner_org_id"])
525
- .where("id", "=", itemId)
526
- .forUpdate()
527
- .executeTakeFirst();
528
- if (!row || row.deleted_at !== null || row.owner_org_id === null) {
529
- throw itemRemovedError(name);
530
- }
531
- if (row.pending_version !== version)
532
- throw pendingStale(name);
533
- return row.owner_org_id;
534
- }
535
- /**
536
- * A confirmed first version: the item stops waiting, its platform component rows are written
537
- * from `components` (a component changed since they were planned -> 409
538
- * `extension.approval_stale`: confirm again), the owner is told (`upload_confirmed`). The
539
- * import's item name lock before the item row, as an import's commit takes them (ruling P3-50).
540
- */
541
- async finishFirstVersion(actor, itemId, version, components, opts) {
542
- await this.adminLock.lockAndAuthorize(actor);
543
- const named = await this.db
544
- .selectFrom("catalog_items")
545
- .select("name")
546
- .where("id", "=", itemId)
547
- .executeTakeFirstOrThrow();
548
- await this.itemLock.lock(named.name);
549
- const owner = await this.pendingRow(itemId, named.name, version);
550
- await this.db
551
- .updateTable("catalog_items")
552
- .set({
553
- awaiting_confirmation: false,
554
- pending_version: null,
555
- pending_by: null,
556
- pending_at: null,
557
- // the confirmed version is the item's latest (ruling P3-34)
558
- latest_version: version,
559
- updated_at: sql `now()`,
560
- })
561
- .where("id", "=", itemId)
562
- .execute();
563
- await markVersionApplied(this.db, itemId, version);
564
- try {
565
- await this.platform.applySyncIn(components);
566
- }
567
- catch (e) {
568
- if (e instanceof PlanStale)
569
- throw pendingStale(named.name);
570
- throw e;
571
- }
572
- await addOrgNotice(this.db, owner, "upload_confirmed", {
573
- itemId,
574
- name: named.name,
575
- displayName: await itemDisplayName(this.db, itemId),
576
- version,
577
- });
578
- await audit(this.db, {
579
- actorAccountId: actor,
580
- ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
581
- action: "catalog.upload_confirmed",
582
- target: itemId,
583
- detail: { name: named.name, version, ownerOrgId: owner },
584
- });
585
- // The item became usable: its commit owes the scope sync (the extension was enabled before,
586
- // so a replica that has not loaded it yet retries the chunk job)
587
- await this.sync.enqueueItem(itemId);
588
- }
589
- /**
590
- * The super admin rejects an org upload's pending version (ruling P3-10, `reason` shown to the
591
- * owner in `upload_rejected`): a first version is removed (soft delete, its requests
592
- * cancelled), a new version's pending state cleared (the version stays imported, never applied);
593
- * the extension name claim is released when nothing is installed under it (pre-flight F10).
594
- * Nothing pending, or a platform-owned item (pre-flight F11) -> 409 `extension.not_pending`.
595
- */
596
- async reject(actor, itemId, reason, opts = {}) {
597
- if (!UUID_RE.test(itemId))
598
- throw notFound();
599
- await this.dropPending(actor, itemId, { kind: "reject", reason }, opts);
600
- }
601
- /**
602
- * The owner org withdraws its pending upload (ruling P3-10): as {@link reject}, without a
603
- * notice. Another org's or a platform item -> 404; nothing pending -> 409
604
- * `extension.not_pending`.
605
- */
606
- async withdraw(orgId, actor, itemId, opts = {}) {
607
- if (!UUID_RE.test(itemId))
608
- throw notFound();
609
- await this.dropPending(actor, itemId, { kind: "withdraw", orgId }, opts);
610
- }
611
- async dropPending(actor, itemId, how, opts) {
612
- if (how.kind === "reject")
613
- await this.adminLock.lockAndAuthorize(actor);
614
- else
615
- await this.orgLock.lockAndAuthorize(how.orgId, actor, "catalog.upload");
616
- const t = this.db;
617
- const item = await t
618
- .selectFrom("catalog_items")
619
- .select([
620
- "id",
621
- "name",
622
- "owner_org_id",
623
- "awaiting_confirmation",
624
- "pending_version",
625
- "package_name",
626
- ])
627
- .where("id", "=", itemId)
628
- .where("deleted_at", "is", null)
629
- .forUpdate()
630
- .executeTakeFirst();
631
- if (!item)
632
- throw notFound();
633
- if (how.kind === "withdraw" && item.owner_org_id !== how.orgId)
634
- throw notFound();
635
- if (item.owner_org_id === null || item.pending_version === null)
636
- throw notPending(item.name);
637
- const now = sql `now()`;
638
- await t
639
- .updateTable("catalog_items")
640
- .set({
641
- pending_version: null,
642
- pending_by: null,
643
- pending_at: null,
644
- updated_at: now,
645
- // A first version is removed: the name is free again (P3-10)
646
- ...(item.awaiting_confirmation && { deleted_at: now }),
647
- })
648
- .where("id", "=", itemId)
649
- .execute();
650
- if (item.awaiting_confirmation) {
651
- await cancelPendingRequests(t, { itemId }, actor, "item_removed", opts);
652
- }
653
- if (how.kind === "reject") {
654
- // never usable afterwards, whoever owns the item later (ruling P3-52)
655
- await t
656
- .updateTable("catalog_item_versions")
657
- .set({ rejected_at: now })
658
- .where("item_id", "=", itemId)
659
- .where("version", "=", item.pending_version)
660
- .execute();
661
- }
662
- // Under the per-name install lock, as CatalogService.removeIn: a confirm's install still
663
- // committing is seen, so its claim is never released under it
664
- await this.installLock.lock(item.name);
665
- await releaseExtensionClaim(t, item.name, item.package_name);
666
- if (how.kind === "reject") {
667
- await addOrgNotice(t, item.owner_org_id, "upload_rejected", {
668
- itemId,
669
- name: item.name,
670
- displayName: await itemDisplayName(t, itemId),
671
- version: item.pending_version,
672
- reason: how.reason,
673
- });
674
- }
675
- await audit(t, {
676
- actorAccountId: actor,
677
- ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
678
- action: how.kind === "reject" ? "catalog.upload_rejected" : "catalog.upload_withdrawn",
679
- target: itemId,
680
- detail: {
681
- name: item.name,
682
- version: item.pending_version,
683
- ownerOrgId: item.owner_org_id,
684
- removed: item.awaiting_confirmation,
685
- ...(how.kind === "reject" && { reason: how.reason }),
686
- ...opts.auditDetail,
687
- },
688
- });
689
- }
690
183
  /** The preview of a scope change or a disable (nothing written). */
691
184
  async preview(itemId, target) {
692
185
  const item = await this.item(itemId, false);
@@ -725,15 +218,13 @@ let CatalogScopeService = class CatalogScopeService {
725
218
  // A builtin item is `all`: the flag is irrelevant there but harmless, so it is not refused
726
219
  const item = await this.db
727
220
  .selectFrom("catalog_items")
728
- .select(["name", "shown_to_others", "owner_org_id", "shareable", "awaiting_confirmation"])
221
+ .select(["name", "shown_to_others", "owner_org_id", "shareable"])
729
222
  .where("id", "=", itemId)
730
223
  .where("deleted_at", "is", null)
731
224
  .forUpdate()
732
225
  .executeTakeFirst();
733
226
  if (!item)
734
227
  throw notFound();
735
- // A first version waiting for the super admin is nobody's to show yet (P3-9)
736
- assertNotAwaiting(item);
737
228
  // Showing goes through the share gate (P3-16); hiding is always allowed
738
229
  if (shown)
739
230
  assertShareable(item);
@@ -755,7 +246,6 @@ let CatalogScopeService = class CatalogScopeService {
755
246
  async writeScope(actor, itemId, target, hash, opts) {
756
247
  await this.adminLock.lockAndAuthorize(actor);
757
248
  const item = await this.item(itemId, true);
758
- assertNotAwaiting(item);
759
249
  const t = await this.targetOf(item, target);
760
250
  const from = await this.listed(itemId);
761
251
  // The owner stays listed (P3-17); widening goes through the share gate (P3-16)
@@ -896,7 +386,6 @@ let CatalogScopeService = class CatalogScopeService {
896
386
  */
897
387
  async addOrg(actor, itemId, orgId, opts = {}) {
898
388
  const item = await this.item(itemId, true);
899
- assertNotAwaiting(item);
900
389
  const changed = await addScopeOrg(this.db, actor, item, orgId, opts);
901
390
  if (changed)
902
391
  await this.enqueue(itemId);
@@ -937,7 +426,6 @@ let CatalogScopeService = class CatalogScopeService {
937
426
  "latest_version",
938
427
  "owner_org_id",
939
428
  "shareable",
940
- "awaiting_confirmation",
941
429
  ])
942
430
  .where("id", "=", itemId)
943
431
  .where("deleted_at", "is", null);
@@ -1198,27 +686,6 @@ let CatalogScopeService = class CatalogScopeService {
1198
686
  }
1199
687
  }
1200
688
  };
1201
- __decorate([
1202
- Transactional()
1203
- ], CatalogScopeService.prototype, "clearPending", null);
1204
- __decorate([
1205
- Transactional()
1206
- ], CatalogScopeService.prototype, "restorePending", null);
1207
- __decorate([
1208
- Transactional()
1209
- ], CatalogScopeService.prototype, "takeOutIn", null);
1210
- __decorate([
1211
- Transactional()
1212
- ], CatalogScopeService.prototype, "oweCleanupIfGone", null);
1213
- __decorate([
1214
- Transactional()
1215
- ], CatalogScopeService.prototype, "stillPending", null);
1216
- __decorate([
1217
- Transactional()
1218
- ], CatalogScopeService.prototype, "finishFirstVersion", null);
1219
- __decorate([
1220
- Transactional()
1221
- ], CatalogScopeService.prototype, "dropPending", null);
1222
689
  __decorate([
1223
690
  Transactional()
1224
691
  ], CatalogScopeService.prototype, "writeShown", null);
@@ -1249,17 +716,10 @@ CatalogScopeService = __decorate([
1249
716
  __param(1, Inject(SuperAdminLock)),
1250
717
  __param(2, Inject(CatalogService)),
1251
718
  __param(3, Inject(PluginImport)),
1252
- __param(4, Inject(PlatformComponents)),
1253
- __param(5, Inject(CatalogItemLock)),
1254
- __param(6, Inject(PluginInstaller)),
1255
- __param(7, Inject(ExtensionAdmin)),
1256
- __param(8, Inject(ExtensionAdminOps)),
1257
- __param(9, Inject(PluginScopeSync)),
1258
- __param(10, Inject(PlatformPluginUpdater)),
1259
- __param(11, Inject(OrgLock)),
1260
- __param(12, Inject(PluginInstallLock)),
1261
- __param(13, Inject(OutboxService)),
1262
- __param(14, Inject(KERNEL_LOGGER)),
1263
- __param(15, Inject(TransactionHost))
719
+ __param(4, Inject(PluginInstaller)),
720
+ __param(5, Inject(ExtensionAdmin)),
721
+ __param(6, Inject(ExtensionAdminOps)),
722
+ __param(7, Inject(PluginScopeSync)),
723
+ __param(8, Inject(TransactionHost))
1264
724
  ], CatalogScopeService);
1265
725
  export { CatalogScopeService };