@capybara-hq/kernel 0.1.0-rc.135 → 0.1.0-rc.137

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 (242) hide show
  1. package/dist/admin/admin.module.js +3 -0
  2. package/dist/admin/audit-log.service.d.ts +96 -12
  3. package/dist/admin/audit-log.service.js +508 -44
  4. package/dist/admin/audit-targets.d.ts +17 -0
  5. package/dist/admin/audit-targets.js +84 -0
  6. package/dist/admin/audit.controller.d.ts +12 -9
  7. package/dist/admin/audit.controller.js +29 -8
  8. package/dist/admin/audit.schemas.d.ts +20 -4
  9. package/dist/admin/audit.schemas.js +42 -17
  10. package/dist/admin/health.service.d.ts +2 -2
  11. package/dist/admin/health.service.js +6 -2
  12. package/dist/agents/templates.js +3 -10
  13. package/dist/app.d.ts +3 -1
  14. package/dist/app.js +1 -0
  15. package/dist/automations/automations.service.js +4 -5
  16. package/dist/catalog/catalog-content.d.ts +1 -1
  17. package/dist/catalog/catalog-content.js +17 -22
  18. package/dist/catalog/catalog-items.d.ts +5 -10
  19. package/dist/catalog/catalog-items.js +27 -51
  20. package/dist/catalog/catalog-scope.service.d.ts +4 -2
  21. package/dist/catalog/catalog-scope.service.js +105 -141
  22. package/dist/catalog/catalog.module.js +2 -0
  23. package/dist/catalog/catalog.service.d.ts +3 -3
  24. package/dist/catalog/catalog.service.js +33 -8
  25. package/dist/catalog/cli.js +17 -0
  26. package/dist/catalog/component-promotion.js +8 -4
  27. package/dist/catalog/component-scope.service.d.ts +3 -1
  28. package/dist/catalog/component-scope.service.js +101 -140
  29. package/dist/catalog/component-state.d.ts +10 -8
  30. package/dist/catalog/component-state.js +42 -30
  31. package/dist/catalog/dependency-edges.d.ts +25 -0
  32. package/dist/catalog/dependency-edges.js +125 -0
  33. package/dist/catalog/install-status.d.ts +2 -3
  34. package/dist/catalog/install-status.js +2 -3
  35. package/dist/catalog/marketplaces.service.d.ts +1 -1
  36. package/dist/catalog/org-catalog.service.d.ts +3 -1
  37. package/dist/catalog/org-catalog.service.js +33 -94
  38. package/dist/catalog/org-intent.d.ts +43 -0
  39. package/dist/catalog/org-intent.js +184 -0
  40. package/dist/catalog/owned-items.d.ts +1 -1
  41. package/dist/catalog/owned-items.js +9 -29
  42. package/dist/catalog/ownership.d.ts +2 -3
  43. package/dist/catalog/ownership.js +10 -28
  44. package/dist/catalog/platform-components.d.ts +3 -1
  45. package/dist/catalog/platform-components.js +9 -1
  46. package/dist/catalog/plugin-import.js +12 -16
  47. package/dist/catalog/plugin-install.d.ts +3 -4
  48. package/dist/catalog/plugin-install.js +25 -49
  49. package/dist/catalog/plugin-upgrade.d.ts +2 -2
  50. package/dist/catalog/plugin-upgrade.js +18 -34
  51. package/dist/catalog/request-rules.js +20 -8
  52. package/dist/catalog/scope-orgs.js +2 -7
  53. package/dist/catalog/scope-state.d.ts +6 -6
  54. package/dist/catalog/scope-state.js +24 -43
  55. package/dist/catalog/scope-sync.d.ts +0 -2
  56. package/dist/catalog/scope-sync.js +11 -39
  57. package/dist/catalog/scope-writer.d.ts +78 -0
  58. package/dist/catalog/scope-writer.js +181 -0
  59. package/dist/catalog/unshare.js +8 -41
  60. package/dist/channels/connections.service.js +6 -6
  61. package/dist/channels/supervisor.js +3 -7
  62. package/dist/client/client-access.service.js +2 -12
  63. package/dist/config/schema.d.ts +6 -0
  64. package/dist/config/schema.js +29 -1
  65. package/dist/conversations/access.d.ts +15 -7
  66. package/dist/conversations/access.js +59 -10
  67. package/dist/conversations/conversation-agent.d.ts +18 -0
  68. package/dist/conversations/conversation-agent.js +23 -0
  69. package/dist/conversations/conversation-ref.d.ts +3 -2
  70. package/dist/conversations/conversation-ref.js +6 -1
  71. package/dist/conversations/conversations.controller.d.ts +28 -5
  72. package/dist/conversations/conversations.controller.js +99 -10
  73. package/dist/conversations/conversations.module.js +10 -0
  74. package/dist/conversations/conversations.schemas.d.ts +6 -0
  75. package/dist/conversations/conversations.schemas.js +15 -0
  76. package/dist/conversations/conversations.service.d.ts +125 -21
  77. package/dist/conversations/conversations.service.js +260 -90
  78. package/dist/conversations/event-log.d.ts +9 -1
  79. package/dist/conversations/event-log.js +2 -0
  80. package/dist/conversations/events.d.ts +57 -0
  81. package/dist/conversations/events.js +73 -1
  82. package/dist/conversations/external/external-calls.d.ts +75 -0
  83. package/dist/conversations/external/external-calls.js +91 -0
  84. package/dist/conversations/external/external-calls.processor.d.ts +45 -0
  85. package/dist/conversations/external/external-calls.processor.js +144 -0
  86. package/dist/conversations/external/external-conversations.service.d.ts +266 -0
  87. package/dist/conversations/external/external-conversations.service.js +1138 -0
  88. package/dist/conversations/external/external.schemas.d.ts +63 -0
  89. package/dist/conversations/external/external.schemas.js +52 -0
  90. package/dist/conversations/external/token.d.ts +6 -0
  91. package/dist/conversations/external/token.js +6 -0
  92. package/dist/conversations/permission-profile-store.d.ts +4 -1
  93. package/dist/conversations/permission-profiles.controller.d.ts +33 -9
  94. package/dist/conversations/permission-profiles.controller.js +86 -6
  95. package/dist/conversations/prompt-suggestions.js +2 -1
  96. package/dist/conversations/queue.js +2 -1
  97. package/dist/conversations/titles.js +4 -3
  98. package/dist/ctx/create-ctx.d.ts +9 -2
  99. package/dist/ctx/create-ctx.js +47 -62
  100. package/dist/ctx/guard-db.d.ts +6 -0
  101. package/dist/ctx/guard-db.js +55 -0
  102. package/dist/db/extension-handoff-schema.d.ts +1 -1
  103. package/dist/db/extension-handoff-schema.js +48 -10
  104. package/dist/db/extension-schema-transfer.js +5 -2
  105. package/dist/db/kernel-schema.d.ts +42 -5
  106. package/dist/device-access/host-services.d.ts +2 -2
  107. package/dist/device-access/host-services.js +8 -8
  108. package/dist/device-access/person-handles.d.ts +16 -7
  109. package/dist/device-access/person-handles.js +14 -9
  110. package/dist/devices/conversation-devices.d.ts +5 -0
  111. package/dist/devices/conversation-devices.js +16 -0
  112. package/dist/erasure/org-catalog.js +2 -2
  113. package/dist/erasure/org-rules.js +5 -0
  114. package/dist/errors.d.ts +34 -0
  115. package/dist/errors.js +34 -0
  116. package/dist/evals/snapshot.js +3 -1
  117. package/dist/evals/trajectory.service.d.ts +2 -2
  118. package/dist/evals/trajectory.service.js +11 -7
  119. package/dist/extensions/admin-ops.d.ts +9 -5
  120. package/dist/extensions/admin-ops.js +52 -8
  121. package/dist/extensions/admin-view.js +1 -4
  122. package/dist/extensions/bootstrap.js +4 -1
  123. package/dist/extensions/cli.js +1 -0
  124. package/dist/extensions/effective-config.js +1 -4
  125. package/dist/extensions/enabled.d.ts +23 -29
  126. package/dist/extensions/enabled.js +141 -62
  127. package/dist/extensions/extensions.module.js +3 -0
  128. package/dist/extensions/host.d.ts +71 -45
  129. package/dist/extensions/host.js +454 -128
  130. package/dist/extensions/installs.d.ts +9 -0
  131. package/dist/extensions/installs.js +23 -0
  132. package/dist/extensions/isolated/admission.d.ts +10 -0
  133. package/dist/extensions/isolated/admission.js +206 -0
  134. package/dist/extensions/isolated/authorization.d.ts +37 -0
  135. package/dist/extensions/isolated/authorization.js +146 -0
  136. package/dist/extensions/isolated/broker.d.ts +15 -0
  137. package/dist/extensions/isolated/broker.js +452 -0
  138. package/dist/extensions/isolated/child-protocol.d.ts +84 -0
  139. package/dist/extensions/isolated/child-protocol.js +21 -0
  140. package/dist/extensions/isolated/child-registration.d.ts +22 -0
  141. package/dist/extensions/isolated/child-registration.js +152 -0
  142. package/dist/extensions/isolated/child-session.d.ts +6 -0
  143. package/dist/extensions/isolated/child-session.js +343 -0
  144. package/dist/extensions/isolated/child-streams.d.ts +4 -0
  145. package/dist/extensions/isolated/child-streams.js +142 -0
  146. package/dist/extensions/isolated/child.d.ts +1 -0
  147. package/dist/extensions/isolated/child.js +10 -0
  148. package/dist/extensions/isolated/configured-package.d.ts +15 -0
  149. package/dist/extensions/isolated/configured-package.js +73 -0
  150. package/dist/extensions/isolated/lanes.d.ts +100 -0
  151. package/dist/extensions/isolated/lanes.js +699 -0
  152. package/dist/extensions/isolated/postgres-gate.d.ts +14 -0
  153. package/dist/extensions/isolated/postgres-gate.js +255 -0
  154. package/dist/extensions/isolated/revision.d.ts +32 -0
  155. package/dist/extensions/isolated/revision.js +358 -0
  156. package/dist/extensions/isolated/rpc-frame.d.ts +50 -0
  157. package/dist/extensions/isolated/rpc-frame.js +193 -0
  158. package/dist/extensions/isolated/rpc.d.ts +66 -0
  159. package/dist/extensions/isolated/rpc.js +314 -0
  160. package/dist/extensions/isolated/sandbox.d.ts +35 -0
  161. package/dist/extensions/isolated/sandbox.js +372 -0
  162. package/dist/extensions/isolated/tier.d.ts +6 -0
  163. package/dist/extensions/isolated/tier.js +45 -0
  164. package/dist/extensions/ledger.d.ts +1 -1
  165. package/dist/extensions/lifecycle-topics.d.ts +1 -0
  166. package/dist/extensions/lifecycle-topics.js +1 -0
  167. package/dist/extensions/lifecycle.d.ts +3 -1
  168. package/dist/extensions/lifecycle.js +32 -5
  169. package/dist/extensions/locks.d.ts +2 -0
  170. package/dist/extensions/locks.js +7 -0
  171. package/dist/extensions/org-config.d.ts +17 -27
  172. package/dist/extensions/org-config.js +114 -129
  173. package/dist/extensions/permission-view.d.ts +3 -1
  174. package/dist/extensions/permission-view.js +4 -1
  175. package/dist/extensions/plugin-guides.d.ts +5 -10
  176. package/dist/extensions/plugin-guides.js +18 -37
  177. package/dist/extensions/plugin-icons.d.ts +3 -21
  178. package/dist/extensions/plugin-icons.js +9 -44
  179. package/dist/extensions/plugin-items.d.ts +10 -30
  180. package/dist/extensions/plugin-items.js +32 -73
  181. package/dist/extensions/point-defaults.d.ts +13 -21
  182. package/dist/extensions/point-defaults.js +32 -72
  183. package/dist/extensions/point-serving.d.ts +56 -0
  184. package/dist/extensions/point-serving.js +120 -0
  185. package/dist/extensions/source-intent.d.ts +14 -0
  186. package/dist/extensions/source-intent.js +77 -0
  187. package/dist/inbox/inbox-items.service.d.ts +11 -0
  188. package/dist/inbox/inbox-items.service.js +39 -0
  189. package/dist/index.d.ts +2 -1
  190. package/dist/index.js +2 -1
  191. package/dist/infra/database/database.module.js +6 -1
  192. package/dist/infra/database/kernel-db.d.ts +3 -1
  193. package/dist/infra/database/kernel-db.js +4 -0
  194. package/dist/infra/outbox/registry.d.ts +70 -0
  195. package/dist/infra/outbox/registry.js +44 -0
  196. package/dist/infra/paging/paged-read.d.ts +56 -0
  197. package/dist/infra/paging/paged-read.js +78 -0
  198. package/dist/infra/queue/queue-names.d.ts +1 -1
  199. package/dist/infra/queue/queue-names.js +1 -0
  200. package/dist/kernel.d.ts +3 -2
  201. package/dist/kernel.js +21 -2
  202. package/dist/kernel.module.js +4 -0
  203. package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
  204. package/dist/lifecycle/kernel-lifecycle.js +22 -12
  205. package/dist/lifecycle/tokens.d.ts +8 -0
  206. package/dist/lifecycle/tokens.js +2 -0
  207. package/dist/orgs/directory-sync.js +1 -4
  208. package/dist/platform/exclusions.js +10 -0
  209. package/dist/platform/ops/admin.ops.js +14 -3
  210. package/dist/platform/ops/conversations.ops.d.ts +8 -1
  211. package/dist/platform/ops/conversations.ops.js +52 -14
  212. package/dist/platform/ops/orgs.ops.js +13 -3
  213. package/dist/plugin-reads/plugin-reads.service.js +4 -1
  214. package/dist/push/recipients.js +5 -2
  215. package/dist/retention/worker.js +2 -1
  216. package/dist/roles/worker-role.d.ts +3 -1
  217. package/dist/roles/worker-role.js +8 -2
  218. package/dist/roles/worker.module.js +3 -0
  219. package/dist/runtime/context/build.js +8 -0
  220. package/dist/runtime/context/compaction.js +7 -0
  221. package/dist/runtime/idle-arm.js +3 -1
  222. package/dist/runtime/idle-compaction.js +2 -1
  223. package/dist/runtime/run-finish.js +2 -1
  224. package/dist/runtime/run-jobs.js +2 -1
  225. package/dist/runtime/run-queue.js +4 -2
  226. package/dist/runtime/run-worker.js +5 -2
  227. package/dist/runtime/stream.js +10 -0
  228. package/dist/runtime/usage-report.js +2 -1
  229. package/dist/runtime/waits.js +2 -1
  230. package/dist/setup/setup-checks.service.js +3 -2
  231. package/dist/skills/usable.js +41 -30
  232. package/dist/themes/theme-state.js +3 -3
  233. package/dist/tools/approvals.service.js +6 -2
  234. package/dist/tools/builtins/skill-load.js +3 -2
  235. package/dist/tools/registry.d.ts +1 -1
  236. package/dist/tools/registry.js +1 -1
  237. package/dist/tools/tool-watch.service.js +3 -1
  238. package/package.json +3 -3
  239. package/src/migrations/0227_external_conversations.sql +33 -0
  240. package/src/migrations/0228_extension_org_switches.sql +56 -0
  241. package/src/migrations/0229_catalog_dependency_edges.sql +17 -0
  242. package/src/migrations/0230_audit_filter_idx.sql +18 -0
@@ -37,9 +37,11 @@ import { findConnectorIn, lockOrgForActions, readCatalogAgentTemplates, readCata
37
37
  import { versionUsable } from "./catalog-items.js";
38
38
  import { agentDefinitionRefs, componentOn, connectorTemplateChoice, connectorTemplateState, connectorTemplatesOn, listedIn, loadComponents, loadScopeState, pluginFactsOf, referenceIds, switchOf, themeIdClashes, } from "./component-state.js";
39
39
  import { LIVE_INSTALL_STATUSES } from "./install-status.js";
40
+ import { DEPENDENCY_DEPTH_LIMIT } from "./org-intent.js";
40
41
  import { assertItemInstallable, disabledPluginItems, pluginItemDisabled, resolveRequestsByInstall, } from "./request-rules.js";
41
42
  import { visibleTo } from "./scope.js";
42
- import { ScopeNoLongerWanted, ScopeStillWanted, wantedByScope } from "./scope-state.js";
43
+ import { pluginEffectsWanted, ScopeNoLongerWanted, ScopeStillWanted, wantedByScope, } from "./scope-state.js";
44
+ import { ScopeWriter } from "./scope-writer.js";
43
45
  import { oweTemplateConnectors } from "./template-connector-jobs.js";
44
46
  import { maxSatisfying, satisfiesRange } from "./versions.js";
45
47
  /** The confirm hash of an uninstall preview: sha256 over its sorted JSON (the scheme of `diffHash`). */
@@ -56,20 +58,6 @@ export function uninstallPreviewHash(p) {
56
58
  }))
57
59
  .digest("hex");
58
60
  }
59
- /** Upserts the org's switch row of an item in the caller's transaction (the org lock is held). */
60
- export async function upsertOrgSwitch(db, orgId, itemId, enabled, actor) {
61
- await db
62
- .insertInto("catalog_item_org_switches")
63
- .values({ item_id: itemId, org_id: orgId, enabled, updated_by: actor })
64
- .onConflict((oc) => oc.columns(["item_id", "org_id"]).doUpdateSet({
65
- enabled,
66
- updated_by: actor,
67
- updated_at: sql `now()`,
68
- }))
69
- .execute();
70
- }
71
- /** How deep plugin dependencies may nest (the root plugin's dependencies are level 1). */
72
- export const DEPENDENCY_DEPTH_LIMIT = 10;
73
61
  /** A failed install step, carried up through the dependency installs to the root install. */
74
62
  export class InstallFailure extends Error {
75
63
  step;
@@ -277,6 +265,7 @@ let PluginInstaller = class PluginInstaller {
277
265
  registry;
278
266
  skills;
279
267
  orgConfigs;
268
+ scopeWriter;
280
269
  config;
281
270
  connectors;
282
271
  log;
@@ -286,7 +275,7 @@ let PluginInstaller = class PluginInstaller {
286
275
  /** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
287
276
  orgLock, storage,
288
277
  /** The loaded extensions and connectors of this replica (the extension registry). */
289
- registry, skills, orgConfigs, config,
278
+ registry, skills, orgConfigs, scopeWriter, config,
290
279
  /** Org connectors: installs create, upgrade and remove them. */
291
280
  connectors, log,
292
281
  /** Owes the template connector ensure of an install's commit (`markInstalled`). */
@@ -297,6 +286,7 @@ let PluginInstaller = class PluginInstaller {
297
286
  this.registry = registry;
298
287
  this.skills = skills;
299
288
  this.orgConfigs = orgConfigs;
289
+ this.scopeWriter = scopeWriter;
300
290
  this.config = config;
301
291
  this.connectors = connectors;
302
292
  this.log = log;
@@ -390,7 +380,7 @@ let PluginInstaller = class PluginInstaller {
390
380
  const listed = listedIn(st, c.id, orgId);
391
381
  const sw = switchOf(st, c.id, orgId);
392
382
  if (componentOn(c, p, listed, sw) ||
393
- !componentOn(c, p && { ...p, installed: true }, listed, sw)) {
383
+ !componentOn(c, p && { ...p, usable: true }, listed, sw)) {
394
384
  continue;
395
385
  }
396
386
  const clash = await themeIdClashes(this.db, orgId, c, c.name);
@@ -865,6 +855,10 @@ let PluginInstaller = class PluginInstaller {
865
855
  throw alreadyInstalled();
866
856
  throw e;
867
857
  }
858
+ // The connector and extension setup reads the committed bundle choice.
859
+ if (ctx.switchOn === true && ctx.parent === undefined) {
860
+ await this.scopeWriter.setOrgSwitch({ kind: "plugin", id: itemId }, orgId, true, actor);
861
+ }
868
862
  }
869
863
  /** The install `installed` and its `plugin.org_installed` audit, under the org row lock (its own transaction). */
870
864
  async markInstalled(orgId, actor, installId, plan, done, ctx, opts) {
@@ -892,28 +886,10 @@ let PluginInstaller = class PluginInstaller {
892
886
  throw new ApiError("plugin.install_not_found", "The install record was cleaned up as stale while the install was running");
893
887
  }
894
888
  await resolveRequestsByInstall(t, orgId, itemId, actor, opts);
895
- // The org switch is written with the install, in one transaction under the org lock
896
- if (ctx.switchOn === true && ctx.parent === undefined) {
897
- await upsertOrgSwitch(t, orgId, itemId, true, actor);
898
- }
899
889
  // The org's choice of connector templates is written with the install too: the chosen ones on,
900
890
  // the ones left out off (they stay available on the connectors page)
901
891
  for (const sw of plan.templateSwitches) {
902
- await t
903
- .insertInto("component_org_switches")
904
- .values({
905
- kind: "connector_template",
906
- component_id: sw.componentId,
907
- org_id: orgId,
908
- enabled: sw.to,
909
- updated_by: actor,
910
- })
911
- .onConflict((oc) => oc.columns(["kind", "component_id", "org_id"]).doUpdateSet({
912
- enabled: sw.to,
913
- updated_by: actor,
914
- updated_at: sql `now()`,
915
- }))
916
- .execute();
892
+ await this.scopeWriter.setOrgSwitch({ kind: "connector_template", id: sw.componentId }, orgId, sw.to, actor);
917
893
  await audit(t, {
918
894
  actorAccountId: actor,
919
895
  ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
@@ -1157,9 +1133,7 @@ let PluginInstaller = class PluginInstaller {
1157
1133
  .where("id", "=", installId)
1158
1134
  .where("org_id", "=", orgId)
1159
1135
  .executeTakeFirst();
1160
- if (row !== undefined &&
1161
- ((await wantedByScope(this.db, orgId, row.item_id)) ||
1162
- (await this.heir(orgId, new Set([installId]), "plugin", installId)) !== undefined)) {
1136
+ if (row !== undefined && (await pluginEffectsWanted(this.db, orgId, row.item_id))) {
1163
1137
  throw new ScopeStillWanted(orgId, row.item_id);
1164
1138
  }
1165
1139
  }
@@ -1195,6 +1169,10 @@ let PluginInstaller = class PluginInstaller {
1195
1169
  // a scope sync uninstall follows a scope change or disable that was guarded already)
1196
1170
  if (via !== "scope")
1197
1171
  await this.assertKeepsPlatformDefault(orgId, installId);
1172
+ // Reconciliation below must see the new intent when it considers the remaining roots.
1173
+ if (itemId !== undefined) {
1174
+ await this.scopeWriter.setOrgSwitch({ kind: "plugin", id: itemId }, orgId, false, actor);
1175
+ }
1198
1176
  const outcome = await this.uninstallIn(orgId, actor, installId, {
1199
1177
  report,
1200
1178
  removing: new Set(),
@@ -1207,9 +1185,6 @@ let PluginInstaller = class PluginInstaller {
1207
1185
  if (outcome === "kept") {
1208
1186
  report.kept.push({ kind: "plugin", ref: installId, reason: "used_by_other_install" });
1209
1187
  }
1210
- if (itemId !== undefined && outcome === "removed") {
1211
- await upsertOrgSwitch(this.db, orgId, itemId, false, actor);
1212
- }
1213
1188
  afterCommit(async () => {
1214
1189
  for (const r of report.removed) {
1215
1190
  if (r.kind === "extension")
@@ -1914,7 +1889,7 @@ let PluginInstaller = class PluginInstaller {
1914
1889
  }
1915
1890
  for (const kind of ["skill", "agent_template"]) {
1916
1891
  const platform = await loadComponents(t, kind, { itemId });
1917
- const st = await loadScopeState(t, kind, platform);
1892
+ const st = await loadScopeState(t, kind, platform, orgId);
1918
1893
  const off = platform.filter((c) => c.scopeMode === "follow" &&
1919
1894
  componentOn(c, pluginFactsOf(st, c.itemId, orgId), listedIn(st, c.id, orgId), switchOf(st, c.id, orgId)));
1920
1895
  const refs = await referenceIds(t, orgId, kind, off.map((c) => c.id));
@@ -1926,7 +1901,7 @@ let PluginInstaller = class PluginInstaller {
1926
1901
  }
1927
1902
  }
1928
1903
  const themes = await loadComponents(t, "theme", { itemId });
1929
- const tst = await loadScopeState(t, "theme", themes);
1904
+ const tst = await loadScopeState(t, "theme", themes, orgId);
1930
1905
  for (const c of themes) {
1931
1906
  if (c.scopeMode === "follow" &&
1932
1907
  componentOn(c, pluginFactsOf(tst, c.itemId, orgId), listedIn(tst, c.id, orgId), switchOf(tst, c.id, orgId))) {
@@ -2021,11 +1996,12 @@ PluginInstaller = __decorate([
2021
1996
  __param(3, Inject(ExtensionRegistry)),
2022
1997
  __param(4, Inject(SkillsService)),
2023
1998
  __param(5, Inject(OrgExtensionConfigs)),
2024
- __param(6, Inject(KernelConfigService)),
2025
- __param(7, Inject(ConnectorsService)),
2026
- __param(8, Inject(KERNEL_LOGGER)),
2027
- __param(9, Inject(OutboxService)),
2028
- __param(10, Inject(TransactionHost))
1999
+ __param(6, Inject(ScopeWriter)),
2000
+ __param(7, Inject(KernelConfigService)),
2001
+ __param(8, Inject(ConnectorsService)),
2002
+ __param(9, Inject(KERNEL_LOGGER)),
2003
+ __param(10, Inject(OutboxService)),
2004
+ __param(11, Inject(TransactionHost))
2029
2005
  ], PluginInstaller);
2030
2006
  export { PluginInstaller };
2031
2007
  /**
@@ -413,8 +413,8 @@ export declare class PlatformPluginUpdater {
413
413
  private themesLost;
414
414
  /**
415
415
  * {@link PlatformUpdatePreview.scopeInstalls}: the live orgs without a live install (`onOrgs`
416
- * are those with one) whose scope wants the plugin on, as the scope sync decides it
417
- * (`wantedOn`). None for a builtin plugin (on everywhere without an install) or one not usable.
416
+ * are those with one) that owe setup under the scope worker's intent predicate.
417
+ * Builtin or unavailable plugins have no org setup plan.
418
418
  */
419
419
  private scopeInstallsOf;
420
420
  /** The orgs where the plugin is on: a live install (builtin: every live org). */
@@ -45,15 +45,16 @@ import { dirDigest, getPath, readCatalogAgentTemplates, readCatalogTemplates, re
45
45
  import { markVersionApplied, versionUsable } from "./catalog-items.js";
46
46
  import { agentsUsing, clashingOrgSkills, clashingPlatformSkills, componentOn, connectorsMadeFrom, connectorTemplatesOn, listedIn, loadComponents, loadScopeState, pluginFactsOf, switchOf, themeIdClashes, } from "./component-state.js";
47
47
  import { credentialKey } from "./credential-key.js";
48
+ import { refreshCatalogDependencyEdges } from "./dependency-edges.js";
48
49
  import { LIVE_INSTALL_STATUSES } from "./install-status.js";
49
50
  import { CatalogItemLock } from "./locks.js";
50
51
  import { requireMarketplacePublisher } from "./marketplace-fetch.js";
52
+ import { orgPluginEffectIntent } from "./org-intent.js";
51
53
  import { isUsable } from "./ownership.js";
52
54
  import { PlanStale, PlatformComponents } from "./platform-components.js";
53
55
  import { catalogSkillNames, PluginInstaller } from "./plugin-install.js";
54
56
  import { assertDeclarativeUpload } from "./plugin-package.js";
55
57
  import { OFFICIAL_MARKETPLACE_ID, officialPublisherProofMode } from "./publisher-policy.js";
56
- import { wantedOn } from "./scope.js";
57
58
  import { PluginScopeSync } from "./scope-sync.js";
58
59
  import { oweTemplateConnectors } from "./template-connector-jobs.js";
59
60
  import { compareVersions, satisfiesRange } from "./versions.js";
@@ -438,22 +439,15 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
438
439
  // An owner update was authorized at its start: the put-back is not checked again
439
440
  const back = opsOwner && { ...opsOwner, rollback: true };
440
441
  try {
441
- if (ext.sha256 === null) {
442
- // Removed by this update: the previous package reinstalled (as it was)
443
- await this.ops.install(checked.name, ext.previous, actor, opts.actorTokenId, {
444
- catalogItemId: itemId,
442
+ if (ext.previous !== null) {
443
+ const { source, sha256, catalog_item_id } = ext.previous;
444
+ await this.ops.install(checked.name, sha256, actor, opts.actorTokenId, {
445
+ ...(source === "catalog" && catalog_item_id && { catalogItemId: catalog_item_id }),
446
+ ...(source === "bootstrap" && { bootstrap: true }),
445
447
  ...(back && { owner: back }),
446
448
  });
447
- if (removedEnabled)
448
- await this.ops.enable(checked.name, actor, opts.actorTokenId, back);
449
- }
450
- else if (ext.previous !== null) {
451
- await this.ops.install(checked.name, ext.previous, actor, opts.actorTokenId, {
452
- ...(back && { owner: back }),
453
- });
454
- // The install left the enabled extension disabled (a held single-provider point): the
455
- // previous version, which did not provide it, runs again
456
- if (leftDisabled !== undefined) {
449
+ // Restore the serving state after a removal or a single-provider conflict.
450
+ if (removedEnabled || leftDisabled !== undefined) {
457
451
  await this.ops.enable(checked.name, actor, opts.actorTokenId, back);
458
452
  }
459
453
  }
@@ -683,6 +677,7 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
683
677
  .execute();
684
678
  await markVersionApplied(t, itemId, diff.to.version);
685
679
  }
680
+ await refreshCatalogDependencyEdges(t);
686
681
  // The orgs using a shared org-owned plugin are told, one notice per plugin (a platform-owned
687
682
  // plugin tells nobody); a plugin never synced before has no version to come from
688
683
  if (diff.from !== null) {
@@ -1414,7 +1409,7 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
1414
1409
  const want = to.manifest.extension;
1415
1410
  const row = await this.db
1416
1411
  .selectFrom("extension_installs")
1417
- .select(["sha256", "version"])
1412
+ .select(["sha256", "version", "source", "catalog_item_id"])
1418
1413
  .where("name", "=", name)
1419
1414
  .executeTakeFirst();
1420
1415
  // Only an extension declared in the startup config runs without an install record; one this
@@ -1423,7 +1418,7 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
1423
1418
  const loaded = declared ? this.registry.manifests().find((m) => m.name === name) : undefined;
1424
1419
  if (!want) {
1425
1420
  if (allowRemove && from?.manifest.extension && row && !declared) {
1426
- return { from: row.version, to: null, sha256: null, previous: row.sha256 };
1421
+ return { from: row.version, to: null, sha256: null, previous: row };
1427
1422
  }
1428
1423
  if (from?.manifest.extension && (row || declared)) {
1429
1424
  throw targetInvalid(`Version ${to.version} of plugin ${name} has no extension, but the deployment runs it; uninstall the extension first`, [{ reason: "extension_removed", version: to.version }]);
@@ -1448,7 +1443,7 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
1448
1443
  from: row?.version ?? null,
1449
1444
  to: want.manifest.version,
1450
1445
  sha256: to.extension_sha256,
1451
- previous: row?.sha256 ?? null,
1446
+ previous: row ?? null,
1452
1447
  };
1453
1448
  }
1454
1449
  /**
@@ -1579,29 +1574,20 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
1579
1574
  }
1580
1575
  /**
1581
1576
  * {@link PlatformUpdatePreview.scopeInstalls}: the live orgs without a live install (`onOrgs`
1582
- * are those with one) whose scope wants the plugin on, as the scope sync decides it
1583
- * (`wantedOn`). None for a builtin plugin (on everywhere without an install) or one not usable.
1577
+ * are those with one) that owe setup under the scope worker's intent predicate.
1578
+ * Builtin or unavailable plugins have no org setup plan.
1584
1579
  */
1585
1580
  async scopeInstallsOf(item, onOrgs) {
1586
1581
  if (item.builtin || !isUsable(item))
1587
1582
  return [];
1588
- const [orgs, listed, switches, failures] = await Promise.all([
1583
+ const [orgs, failures] = await Promise.all([
1589
1584
  this.db
1590
1585
  .selectFrom("orgs")
1591
1586
  .select(["id", "name"])
1592
1587
  .where("deleted_at", "is", null)
1588
+ .where(({ ref }) => orgPluginEffectIntent(this.db, ref("orgs.id"), item.id))
1593
1589
  .orderBy("id")
1594
1590
  .execute(),
1595
- this.db
1596
- .selectFrom("catalog_item_scope_orgs")
1597
- .select("org_id")
1598
- .where("item_id", "=", item.id)
1599
- .execute(),
1600
- this.db
1601
- .selectFrom("catalog_item_org_switches")
1602
- .select(["org_id", "enabled"])
1603
- .where("item_id", "=", item.id)
1604
- .execute(),
1605
1591
  this.db
1606
1592
  .selectFrom("plugin_scope_sync_failures")
1607
1593
  .select(["org_id", "message"])
@@ -1610,10 +1596,8 @@ let PlatformPluginUpdater = class PlatformPluginUpdater {
1610
1596
  ]);
1611
1597
  const failed = new Map(failures.map((r) => [r.org_id, r.message]));
1612
1598
  const on = new Set(onOrgs);
1613
- const inList = new Set(listed.map((r) => r.org_id));
1614
- const sw = new Map(switches.map((r) => [r.org_id, r.enabled]));
1615
1599
  return orgs
1616
- .filter((o) => !on.has(o.id) && wantedOn(item, inList.has(o.id), sw.get(o.id) ?? null))
1600
+ .filter((o) => !on.has(o.id))
1617
1601
  .map((o) => {
1618
1602
  const lastFailure = failed.get(o.id);
1619
1603
  return {
@@ -1,5 +1,6 @@
1
1
  import { sql } from "kysely";
2
2
  import { ApiError } from "../errors.js";
3
+ import { extensionSourcesQuery } from "../extensions/plugin-items.js";
3
4
  import { audit } from "../identity/audit.js";
4
5
  import { isUsable } from "./ownership.js";
5
6
  import { visibleTo } from "./scope.js";
@@ -95,19 +96,30 @@ export function pluginItemDisabled(names) {
95
96
  */
96
97
  export async function assertPluginSourcesInstallable(t, orgId, plugin) {
97
98
  const rows = await t
98
- .selectFrom("extension_catalog_sources as s")
99
- .innerJoin("catalog_items as i", "i.id", "s.item_id")
100
- .select(["i.id", "i.disabled", "i.scope", "i.builtin"])
101
- .where("s.plugin", "=", plugin)
102
- .where("i.deleted_at", "is", null)
99
+ .selectFrom(extensionSourcesQuery(t, plugin).as("source"))
100
+ .innerJoin("catalog_items as i", "i.id", "source.item_id")
101
+ .select([
102
+ "i.id",
103
+ "i.disabled",
104
+ "i.scope",
105
+ "i.builtin",
106
+ "i.deleted_at",
107
+ "i.owner_org_id",
108
+ "i.shareable",
109
+ ])
103
110
  .orderBy("i.id")
104
111
  .forShare("i")
105
112
  .execute();
106
- if (rows.some((r) => r.disabled))
113
+ const live = rows.filter((row) => row.deleted_at === null);
114
+ if (live.some((row) => row.disabled))
107
115
  throw itemDisabled();
108
- for (const r of rows)
109
- if (!(await offeredTo(t, orgId, r)))
116
+ if (rows.length > 0 && live.length === 0)
117
+ throw notOffered();
118
+ for (const row of live) {
119
+ if ((row.owner_org_id !== null && row.owner_org_id !== orgId && !row.shareable) ||
120
+ !(await offeredTo(t, orgId, row)))
110
121
  throw notOffered();
122
+ }
111
123
  }
112
124
  /**
113
125
  * Cancels catalog requests still pending in the caller's transaction (`db` is its handle; when the
@@ -2,6 +2,7 @@ import { sql } from "kysely";
2
2
  import { audit } from "../identity/audit.js";
3
3
  import { notifyItemShared } from "../inbox/org-notices.js";
4
4
  import { assertShareable, shareGate } from "./ownership.js";
5
+ import { addNamedPluginOrg } from "./scope-writer.js";
5
6
  /**
6
7
  * Adds one org to a `named` item's list in the caller's transaction (the caller holds the item row
7
8
  * `FOR UPDATE`): audits `catalog.scope_changed` the way a scope change
@@ -25,13 +26,7 @@ export async function addScopeOrg(db, actor, item, orgId, opts = {}) {
25
26
  return false;
26
27
  assertShareable(item);
27
28
  }
28
- const added = await db
29
- .insertInto("catalog_item_scope_orgs")
30
- .values({ item_id: item.id, org_id: orgId })
31
- .onConflict((oc) => oc.doNothing())
32
- .returning("org_id")
33
- .executeTakeFirst();
34
- if (added === undefined)
29
+ if (!(await addNamedPluginOrg(db, item.id, orgId)))
35
30
  return false;
36
31
  await db
37
32
  .updateTable("catalog_items")
@@ -1,10 +1,10 @@
1
1
  import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
- /**
3
- * Whether the item's own scope wants it on in the org (`wantedOn` over the item's scope, builtin
4
- * and disabled flags, the org's list membership and its switch), read through `db` (the caller's
5
- * transaction under the org row lock, or the pool). A deleted or missing item is not wanted.
6
- */
2
+ /** Whether the item's own scope wants it on in the org, independent of dependencies. */
7
3
  export declare function wantedByScope(db: KernelSchemaDb, orgId: string, itemId: string): Promise<boolean>;
4
+ /** The committed plugin intent, including any authorized dependency root. */
5
+ export declare function pluginUsable(db: KernelSchemaDb, orgId: string, itemId: string): Promise<boolean>;
6
+ /** Direct intent still owes an effect attempt when a blocked dependency denies execution. */
7
+ export declare function pluginEffectsWanted(db: KernelSchemaDb, orgId: string, itemId: string): Promise<boolean>;
8
8
  /**
9
9
  * A scope sync install (`via: "scope"`) found, under the org row lock, that the item is no longer
10
10
  * wanted in the org (a person switched it off, or the scope changed, after the sync's decision):
@@ -32,4 +32,4 @@ export declare class ScopeStillWanted extends Error {
32
32
  * orgs, through the share gate). A hidden `named` item outside its list is left out, so nothing
33
33
  * reveals it to the org.
34
34
  */
35
- export declare function itemsSeenBy(db: KernelSchemaDb, orgId: string, itemIds: readonly string[]): Promise<Set<string>>;
35
+ export declare function itemsSeenBy(db: KernelSchemaDb, orgId: string, itemIds: readonly string[], view?: "catalog" | "offered"): Promise<Set<string>>;
@@ -1,32 +1,24 @@
1
- import { requestable, visibleTo, wantedOn } from "./scope.js";
2
- /**
3
- * Whether the item's own scope wants it on in the org (`wantedOn` over the item's scope, builtin
4
- * and disabled flags, the org's list membership and its switch), read through `db` (the caller's
5
- * transaction under the org row lock, or the pool). A deleted or missing item is not wanted.
6
- */
1
+ import { orgPluginDirectIntent, orgPluginEffectIntent, orgSees, orgUsable } from "./org-intent.js";
2
+ /** Whether the item's own scope wants it on in the org, independent of dependencies. */
7
3
  export async function wantedByScope(db, orgId, itemId) {
8
- const item = await db
9
- .selectFrom("catalog_items")
10
- .select(["scope", "builtin", "disabled"])
11
- .where("id", "=", itemId)
12
- .where("deleted_at", "is", null)
13
- .executeTakeFirst();
14
- if (!item)
15
- return false;
16
- // one after the other: `db` may be a transaction's single connection
17
- const listed = await db
18
- .selectFrom("catalog_item_scope_orgs")
19
- .select("item_id")
20
- .where("item_id", "=", itemId)
21
- .where("org_id", "=", orgId)
4
+ const item = await orgPluginDirectIntent(db, orgId)
5
+ .where("item.id", "=", itemId)
22
6
  .executeTakeFirst();
23
- const sw = await db
24
- .selectFrom("catalog_item_org_switches")
25
- .select("enabled")
26
- .where("item_id", "=", itemId)
27
- .where("org_id", "=", orgId)
28
- .executeTakeFirst();
29
- return wantedOn(item, listed !== undefined, sw?.enabled ?? null);
7
+ return item?.directIntent === true;
8
+ }
9
+ /** The committed plugin intent, including any authorized dependency root. */
10
+ export async function pluginUsable(db, orgId, itemId) {
11
+ const row = await db
12
+ .selectNoFrom(orgUsable(db, orgId, itemId).as("usable"))
13
+ .executeTakeFirstOrThrow();
14
+ return row.usable;
15
+ }
16
+ /** Direct intent still owes an effect attempt when a blocked dependency denies execution. */
17
+ export async function pluginEffectsWanted(db, orgId, itemId) {
18
+ const row = await db
19
+ .selectNoFrom(orgPluginEffectIntent(db, orgId, itemId).as("wanted"))
20
+ .executeTakeFirstOrThrow();
21
+ return row.wanted;
30
22
  }
31
23
  /**
32
24
  * A scope sync install (`via: "scope"`) found, under the org row lock, that the item is no longer
@@ -65,23 +57,12 @@ export class ScopeStillWanted extends Error {
65
57
  * orgs, through the share gate). A hidden `named` item outside its list is left out, so nothing
66
58
  * reveals it to the org.
67
59
  */
68
- export async function itemsSeenBy(db, orgId, itemIds) {
60
+ export async function itemsSeenBy(db, orgId, itemIds, view = "catalog") {
69
61
  if (itemIds.length === 0)
70
62
  return new Set();
71
- const items = await db
72
- .selectFrom("catalog_items")
73
- .select(["id", "scope", "builtin", "shown_to_others", "owner_org_id", "shareable"])
74
- .where("id", "in", [...itemIds])
75
- .where("deleted_at", "is", null)
63
+ const items = await orgSees(db, orgId, true, view)
64
+ .select("item.id")
65
+ .where("item.id", "in", [...itemIds])
76
66
  .execute();
77
- const listed = new Set((await db
78
- .selectFrom("catalog_item_scope_orgs")
79
- .select("item_id")
80
- .where("org_id", "=", orgId)
81
- .where("item_id", "in", [...itemIds])
82
- .execute()).map((r) => r.item_id));
83
- return new Set(items
84
- .filter((i) => visibleTo({ scope: i.builtin ? "all" : i.scope }, listed.has(i.id)) ||
85
- requestable(i, listed.has(i.id)))
86
- .map((i) => i.id));
67
+ return new Set(items.map((item) => item.id));
87
68
  }
@@ -131,8 +131,6 @@ export declare class PluginScopeSync {
131
131
  */
132
132
  private syncRecorded;
133
133
  private syncLocked;
134
- /** Whether a live install of the org depends on this install (spec §5 rule 9). */
135
- dependedOn(orgId: string, installId: string): Promise<boolean>;
136
134
  /** The items of the dependency installs this install (of `orgId`) created. */
137
135
  private createdDependencies;
138
136
  /**
@@ -22,9 +22,8 @@ import { KERNEL_LOGGER } from "../infra/tokens.js";
22
22
  import { LIVE_INSTALL_STATUSES } from "./install-status.js";
23
23
  import { isUsable } from "./ownership.js";
24
24
  import { PluginInstaller } from "./plugin-install.js";
25
- import { wantedOn } from "./scope.js";
26
25
  import { oweScopeItem, PLUGIN_SCOPE_QUEUE, SCOPE_CHUNK_JOB, SCOPE_ORG_CHUNK, SCOPE_ORG_CHUNK_SIZE, SCOPE_ORG_JOB, scopeChunkJobId, scopeOrgJobId, } from "./scope-jobs.js";
27
- import { ScopeNoLongerWanted, ScopeStillWanted } from "./scope-state.js";
26
+ import { pluginEffectsWanted, ScopeNoLongerWanted, ScopeStillWanted, wantedByScope, } from "./scope-state.js";
28
27
  import { TemplateConnectorEnsurer } from "./template-connector-ensure.js";
29
28
  /**
30
29
  * The plugin scope sync (plugin and component management spec §5, phase 1): converges every org
@@ -281,29 +280,14 @@ let PluginScopeSync = class PluginScopeSync {
281
280
  .executeTakeFirst();
282
281
  if (!item || !org || item.builtin)
283
282
  return "unchanged";
284
- const [listed, sw, live] = await Promise.all([
285
- this.db
286
- .selectFrom("catalog_item_scope_orgs")
287
- .select("item_id")
288
- .where("item_id", "=", itemId)
289
- .where("org_id", "=", orgId)
290
- .executeTakeFirst(),
291
- this.db
292
- .selectFrom("catalog_item_org_switches")
293
- .select("enabled")
294
- .where("item_id", "=", itemId)
295
- .where("org_id", "=", orgId)
296
- .executeTakeFirst(),
297
- this.db
298
- .selectFrom("plugin_installs")
299
- .select(["id", "status"])
300
- .where("org_id", "=", orgId)
301
- .where("item_id", "=", itemId)
302
- .where("status", "in", [...LIVE_INSTALL_STATUSES])
303
- .executeTakeFirst(),
304
- ]);
305
- const wanted = wantedOn(item, listed !== undefined, sw?.enabled ?? null) ||
306
- (live !== undefined && (await this.dependedOn(orgId, live.id)));
283
+ const live = await this.db
284
+ .selectFrom("plugin_installs")
285
+ .select(["id", "status"])
286
+ .where("org_id", "=", orgId)
287
+ .where("item_id", "=", itemId)
288
+ .where("status", "in", [...LIVE_INSTALL_STATUSES])
289
+ .executeTakeFirst();
290
+ const wanted = await pluginEffectsWanted(this.db, orgId, itemId);
307
291
  if (wanted) {
308
292
  if (live) {
309
293
  // A platform update may have added dependencies: installed here (rule 9, Task 7)
@@ -323,7 +307,8 @@ let PluginScopeSync = class PluginScopeSync {
323
307
  }
324
308
  return "unchanged";
325
309
  }
326
- if (!isUsable(item))
310
+ // A dependency-only install is owned and recorded by the enabling root's effects.
311
+ if (!isUsable(item) || !(await wantedByScope(this.db, orgId, itemId)))
327
312
  return "unchanged";
328
313
  // The platform's version (one per deployment, Task 7); the latest import before the first sync
329
314
  const version = item.platform_synced_version ?? item.latest_version;
@@ -370,19 +355,6 @@ let PluginScopeSync = class PluginScopeSync {
370
355
  }
371
356
  return "removed";
372
357
  }
373
- /** Whether a live install of the org depends on this install (spec §5 rule 9). */
374
- async dependedOn(orgId, installId) {
375
- const row = await this.db
376
- .selectFrom("plugin_install_components as c")
377
- .innerJoin("plugin_installs as a", "a.id", "c.install_id")
378
- .select("a.id")
379
- .where("a.org_id", "=", orgId)
380
- .where("a.status", "in", [...LIVE_INSTALL_STATUSES])
381
- .where("c.kind", "=", "plugin")
382
- .where("c.ref", "=", installId)
383
- .executeTakeFirst();
384
- return row !== undefined;
385
- }
386
358
  /** The items of the dependency installs this install (of `orgId`) created. */
387
359
  async createdDependencies(orgId, installId) {
388
360
  // `ref` is text: the child install ids are read first, then their items