@capybara-hq/kernel 0.1.0-rc.136 → 0.1.0-rc.138

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 (203) hide show
  1. package/dist/admin/audit-log.service.d.ts +96 -12
  2. package/dist/admin/audit-log.service.js +508 -44
  3. package/dist/admin/audit-targets.d.ts +17 -0
  4. package/dist/admin/audit-targets.js +84 -0
  5. package/dist/admin/audit.controller.d.ts +12 -9
  6. package/dist/admin/audit.controller.js +29 -8
  7. package/dist/admin/audit.schemas.d.ts +20 -4
  8. package/dist/admin/audit.schemas.js +42 -17
  9. package/dist/admin/extensions.controller.d.ts +5 -2
  10. package/dist/admin/extensions.controller.js +10 -1
  11. package/dist/admin/extensions.schemas.d.ts +4 -0
  12. package/dist/admin/extensions.schemas.js +9 -0
  13. package/dist/admin/health.service.d.ts +1 -1
  14. package/dist/admin/health.service.js +3 -3
  15. package/dist/agents/templates.js +3 -10
  16. package/dist/app.d.ts +3 -1
  17. package/dist/app.js +1 -0
  18. package/dist/automations/automations.service.js +4 -5
  19. package/dist/catalog/catalog-content.d.ts +6 -8
  20. package/dist/catalog/catalog-content.js +17 -22
  21. package/dist/catalog/catalog-items.d.ts +5 -10
  22. package/dist/catalog/catalog-items.js +27 -51
  23. package/dist/catalog/catalog-scope.service.d.ts +4 -2
  24. package/dist/catalog/catalog-scope.service.js +109 -145
  25. package/dist/catalog/catalog.module.js +7 -4
  26. package/dist/catalog/catalog.service.d.ts +10 -4
  27. package/dist/catalog/catalog.service.js +52 -10
  28. package/dist/catalog/cli.js +17 -0
  29. package/dist/catalog/component-promotion.js +8 -4
  30. package/dist/catalog/component-scope.service.d.ts +3 -1
  31. package/dist/catalog/component-scope.service.js +101 -140
  32. package/dist/catalog/component-state.d.ts +10 -8
  33. package/dist/catalog/component-state.js +42 -30
  34. package/dist/catalog/dependency-edges.d.ts +25 -0
  35. package/dist/catalog/dependency-edges.js +125 -0
  36. package/dist/catalog/effect-work.d.ts +27 -0
  37. package/dist/catalog/effect-work.js +151 -0
  38. package/dist/catalog/jobs.d.ts +4 -1
  39. package/dist/catalog/jobs.js +14 -4
  40. package/dist/catalog/org-catalog.service.d.ts +5 -1
  41. package/dist/catalog/org-catalog.service.js +101 -106
  42. package/dist/catalog/org-intent.d.ts +45 -0
  43. package/dist/catalog/org-intent.js +205 -0
  44. package/dist/catalog/owned-items.d.ts +6 -2
  45. package/dist/catalog/owned-items.js +27 -32
  46. package/dist/catalog/ownership.d.ts +2 -3
  47. package/dist/catalog/ownership.js +10 -28
  48. package/dist/catalog/platform-components.d.ts +3 -1
  49. package/dist/catalog/platform-components.js +9 -1
  50. package/dist/catalog/plugin-import.d.ts +4 -2
  51. package/dist/catalog/plugin-import.js +22 -21
  52. package/dist/catalog/plugin-install.d.ts +19 -54
  53. package/dist/catalog/plugin-install.js +221 -271
  54. package/dist/catalog/plugin-upgrade.d.ts +10 -5
  55. package/dist/catalog/plugin-upgrade.js +80 -67
  56. package/dist/catalog/request-rules.js +20 -8
  57. package/dist/catalog/scope-orgs.js +2 -7
  58. package/dist/catalog/scope-state.d.ts +6 -6
  59. package/dist/catalog/scope-state.js +24 -43
  60. package/dist/catalog/scope-sync-workers.d.ts +7 -11
  61. package/dist/catalog/scope-sync-workers.js +10 -26
  62. package/dist/catalog/scope-sync.d.ts +19 -116
  63. package/dist/catalog/scope-sync.js +228 -275
  64. package/dist/catalog/scope-writer.d.ts +87 -0
  65. package/dist/catalog/scope-writer.js +239 -0
  66. package/dist/catalog/template-connector-ensure.js +1 -1
  67. package/dist/catalog/unshare.js +9 -43
  68. package/dist/channels/connections.service.js +6 -6
  69. package/dist/channels/supervisor.js +3 -7
  70. package/dist/client/client-access.service.js +2 -12
  71. package/dist/config/schema.d.ts +6 -0
  72. package/dist/config/schema.js +32 -1
  73. package/dist/ctx/create-ctx.d.ts +2 -2
  74. package/dist/ctx/create-ctx.js +2 -60
  75. package/dist/ctx/guard-db.d.ts +6 -0
  76. package/dist/ctx/guard-db.js +55 -0
  77. package/dist/db/extension-handoff-schema.d.ts +1 -1
  78. package/dist/db/extension-handoff-schema.js +48 -10
  79. package/dist/db/extension-schema-transfer.js +5 -2
  80. package/dist/db/kernel-schema.d.ts +32 -6
  81. package/dist/device-access/host-services.d.ts +2 -2
  82. package/dist/device-access/host-services.js +5 -6
  83. package/dist/erasure/org-catalog.js +2 -2
  84. package/dist/erasure/org-rules.js +7 -0
  85. package/dist/extensions/admin-ops.d.ts +10 -6
  86. package/dist/extensions/admin-ops.js +53 -9
  87. package/dist/extensions/admin-view.d.ts +4 -1
  88. package/dist/extensions/admin-view.js +27 -14
  89. package/dist/extensions/bootstrap.d.ts +1 -1
  90. package/dist/extensions/bootstrap.js +6 -4
  91. package/dist/extensions/cli.js +1 -0
  92. package/dist/extensions/effective-config.js +1 -4
  93. package/dist/extensions/enabled.d.ts +183 -29
  94. package/dist/extensions/enabled.js +148 -62
  95. package/dist/extensions/host.d.ts +65 -45
  96. package/dist/extensions/host.js +446 -128
  97. package/dist/extensions/installs.d.ts +9 -0
  98. package/dist/extensions/installs.js +23 -0
  99. package/dist/extensions/isolated/admission.d.ts +10 -0
  100. package/dist/extensions/isolated/admission.js +206 -0
  101. package/dist/extensions/isolated/authorization.d.ts +45 -0
  102. package/dist/extensions/isolated/authorization.js +191 -0
  103. package/dist/extensions/isolated/broker.d.ts +15 -0
  104. package/dist/extensions/isolated/broker.js +471 -0
  105. package/dist/extensions/isolated/child-protocol.d.ts +84 -0
  106. package/dist/extensions/isolated/child-protocol.js +21 -0
  107. package/dist/extensions/isolated/child-registration.d.ts +22 -0
  108. package/dist/extensions/isolated/child-registration.js +152 -0
  109. package/dist/extensions/isolated/child-session.d.ts +6 -0
  110. package/dist/extensions/isolated/child-session.js +343 -0
  111. package/dist/extensions/isolated/child-streams.d.ts +4 -0
  112. package/dist/extensions/isolated/child-streams.js +142 -0
  113. package/dist/extensions/isolated/child.d.ts +1 -0
  114. package/dist/extensions/isolated/child.js +10 -0
  115. package/dist/extensions/isolated/configured-package.d.ts +15 -0
  116. package/dist/extensions/isolated/configured-package.js +73 -0
  117. package/dist/extensions/isolated/engine-binds.d.ts +9 -0
  118. package/dist/extensions/isolated/engine-binds.js +82 -0
  119. package/dist/extensions/isolated/engine-policy.d.ts +85 -0
  120. package/dist/extensions/isolated/engine-policy.js +75 -0
  121. package/dist/extensions/isolated/engine-proxy-main.d.ts +1 -0
  122. package/dist/extensions/isolated/engine-proxy-main.js +20 -0
  123. package/dist/extensions/isolated/engine-proxy.d.ts +7 -0
  124. package/dist/extensions/isolated/engine-proxy.js +408 -0
  125. package/dist/extensions/isolated/engine-vaults.d.ts +12 -0
  126. package/dist/extensions/isolated/engine-vaults.js +93 -0
  127. package/dist/extensions/isolated/lanes.d.ts +108 -0
  128. package/dist/extensions/isolated/lanes.js +726 -0
  129. package/dist/extensions/isolated/postgres-gate.d.ts +15 -0
  130. package/dist/extensions/isolated/postgres-gate.js +255 -0
  131. package/dist/extensions/isolated/revision.d.ts +32 -0
  132. package/dist/extensions/isolated/revision.js +359 -0
  133. package/dist/extensions/isolated/rpc-frame.d.ts +50 -0
  134. package/dist/extensions/isolated/rpc-frame.js +193 -0
  135. package/dist/extensions/isolated/rpc.d.ts +66 -0
  136. package/dist/extensions/isolated/rpc.js +314 -0
  137. package/dist/extensions/isolated/sandbox.d.ts +39 -0
  138. package/dist/extensions/isolated/sandbox.js +356 -0
  139. package/dist/extensions/isolated/tier.d.ts +6 -0
  140. package/dist/extensions/isolated/tier.js +45 -0
  141. package/dist/extensions/ledger.d.ts +1 -1
  142. package/dist/extensions/lifecycle-topics.d.ts +2 -0
  143. package/dist/extensions/lifecycle-topics.js +2 -0
  144. package/dist/extensions/lifecycle.d.ts +3 -1
  145. package/dist/extensions/lifecycle.js +53 -5
  146. package/dist/extensions/locks.d.ts +2 -0
  147. package/dist/extensions/locks.js +7 -0
  148. package/dist/extensions/org-config.d.ts +17 -27
  149. package/dist/extensions/org-config.js +114 -129
  150. package/dist/extensions/permission-view.d.ts +6 -1
  151. package/dist/extensions/plugin-guides.d.ts +5 -10
  152. package/dist/extensions/plugin-guides.js +18 -37
  153. package/dist/extensions/plugin-icons.d.ts +3 -21
  154. package/dist/extensions/plugin-icons.js +9 -44
  155. package/dist/extensions/plugin-items.d.ts +10 -30
  156. package/dist/extensions/plugin-items.js +32 -73
  157. package/dist/extensions/source-intent.d.ts +14 -0
  158. package/dist/extensions/source-intent.js +77 -0
  159. package/dist/inbox/platform-inbox.service.js +16 -27
  160. package/dist/index.d.ts +2 -1
  161. package/dist/index.js +2 -1
  162. package/dist/infra/database/database.module.js +6 -1
  163. package/dist/infra/database/kernel-db.d.ts +3 -1
  164. package/dist/infra/database/kernel-db.js +4 -0
  165. package/dist/infra/events/kernel-events.d.ts +4 -0
  166. package/dist/infra/outbox/registry.d.ts +3 -36
  167. package/dist/infra/outbox/registry.js +4 -49
  168. package/dist/infra/paging/paged-read.d.ts +56 -0
  169. package/dist/infra/paging/paged-read.js +78 -0
  170. package/dist/infra/queue/queue-names.d.ts +1 -1
  171. package/dist/infra/queue/queue-names.js +1 -1
  172. package/dist/kernel.d.ts +3 -2
  173. package/dist/kernel.js +21 -2
  174. package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
  175. package/dist/lifecycle/kernel-lifecycle.js +22 -12
  176. package/dist/lifecycle/tokens.d.ts +8 -0
  177. package/dist/lifecycle/tokens.js +2 -0
  178. package/dist/orgs/directory-sync.js +1 -4
  179. package/dist/orgs/org-lock.d.ts +3 -1
  180. package/dist/orgs/org-lock.js +3 -1
  181. package/dist/orgs/orgs.service.d.ts +3 -1
  182. package/dist/orgs/orgs.service.js +15 -8
  183. package/dist/overrides.d.ts +0 -3
  184. package/dist/overrides.js +0 -2
  185. package/dist/platform/ops/admin.ops.js +14 -3
  186. package/dist/platform/ops/extensions-admin.ops.js +20 -3
  187. package/dist/platform/ops/org-plugins.ops.js +5 -2
  188. package/dist/platform/ops/orgs.ops.js +13 -3
  189. package/dist/platform/ops/plugins.ops.js +11 -10
  190. package/dist/setup/setup-checks.service.js +3 -2
  191. package/dist/skills/usable.js +41 -30
  192. package/dist/themes/theme-state.js +3 -3
  193. package/dist/tools/registry.d.ts +2 -2
  194. package/dist/tools/registry.js +7 -3
  195. package/package.json +2 -2
  196. package/src/migrations/0228_extension_org_switches.sql +56 -0
  197. package/src/migrations/0229_catalog_dependency_edges.sql +17 -0
  198. package/src/migrations/0230_audit_filter_idx.sql +18 -0
  199. package/src/migrations/0231_plugin_effect_work.sql +171 -0
  200. package/dist/catalog/install-status.d.ts +0 -6
  201. package/dist/catalog/install-status.js +0 -6
  202. package/dist/catalog/scope-jobs.d.ts +0 -59
  203. package/dist/catalog/scope-jobs.js +0 -78
@@ -27,8 +27,8 @@ import { ComponentPromotion, notOwnedError, PROMOTION_ATTEMPTS, PromotionStale,
27
27
  import { agentsUsing, COMPONENT_KINDS, componentLocked, componentOffered, componentOn, componentPluginOn, connectorsMadeFrom, listedIn, loadComponents, loadScopeState, pluginFactsOf, skillNameClashes, switchOf, themeIdClashes, } from "./component-state.js";
28
28
  import { assertOwnerListed, assertShareable, assertShareableTarget, newlySharedWith, ownerOf, ownersOf, } from "./ownership.js";
29
29
  import { PluginInstaller } from "./plugin-install.js";
30
+ import { assertScopePreview, COMPONENT_SCOPE_TABLE, ScopeWriter } from "./scope-writer.js";
30
31
  const notFound = () => new ApiError("component.not_found", "Component not found");
31
- const staleError = () => new ApiError("plugin.scope_stale", "What this change affects has changed since the preview; preview it again and confirm the new hash");
32
32
  const builtinError = () => new ApiError("plugin.builtin", "This component belongs to a built-in plugin: it is on in every organization and its scope cannot be changed or disabled");
33
33
  /** The org action that manages a kind of component (the org admin's). */
34
34
  const ACTION = {
@@ -123,12 +123,13 @@ function switchHash(orgId, c, preview) {
123
123
  let ComponentScopeService = class ComponentScopeService {
124
124
  kdb;
125
125
  adminLock;
126
+ scopeWriter;
126
127
  orgLock;
127
128
  promotion;
128
129
  txHost;
129
130
  guides;
130
131
  installer;
131
- constructor(kdb, adminLock,
132
+ constructor(kdb, adminLock, scopeWriter,
132
133
  /** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
133
134
  orgLock,
134
135
  /** Promote on share (ruling P3-3): an org row shown or associated becomes a platform row. */
@@ -139,6 +140,7 @@ let ComponentScopeService = class ComponentScopeService {
139
140
  installer) {
140
141
  this.kdb = kdb;
141
142
  this.adminLock = adminLock;
143
+ this.scopeWriter = scopeWriter;
142
144
  this.orgLock = orgLock;
143
145
  this.promotion = promotion;
144
146
  this.txHost = txHost;
@@ -253,7 +255,7 @@ let ComponentScopeService = class ComponentScopeService {
253
255
  const uiTheme = await platformDefaultThemeId(this.db);
254
256
  for (const kind of filter.kind !== undefined ? [filter.kind] : COMPONENT_KINDS) {
255
257
  const comps = await loadComponents(this.db, kind, filter.itemId !== undefined ? { itemId: filter.itemId } : {});
256
- const st = await loadScopeState(this.db, kind, comps);
258
+ const st = await loadScopeState(this.db, kind, comps, orgId);
257
259
  const linked = await this.linkedCopies(orgId, kind);
258
260
  const owners = await ownersOf(this.db, comps.flatMap((c) => (c.ownerOrgId === null ? [] : [c.ownerOrgId])));
259
261
  const guides = kind === "connector_template"
@@ -434,10 +436,7 @@ let ComponentScopeService = class ComponentScopeService {
434
436
  assertOwnerListed(gateRow(v), t);
435
437
  assertShareable(gateRow(v));
436
438
  const p = await this.compute(v, t, { hashId: id, listed });
437
- if (p.scopeHash !== scopeHash)
438
- throw staleError();
439
- if (p.conflicts.length > 0)
440
- throw nameConflictError(p.conflicts, kind);
439
+ assertScopePreview({ kind: "scope", hash: scopeHash }, p, (conflicts) => nameConflictError(conflicts, kind));
441
440
  const promotable = kind;
442
441
  const written = await this.withPromotion(promotable, id, (prepared) => this.writeScope(actor, kind, prepared.platformId, target, scopeHash, opts, {
443
442
  hashId: id,
@@ -493,7 +492,7 @@ let ComponentScopeService = class ComponentScopeService {
493
492
  const [c] = await loadComponents(this.db, kind, { ids: [id], forUpdate: true });
494
493
  if (!c)
495
494
  throw notFound();
496
- const st = await loadScopeState(this.db, kind, [c]);
495
+ const st = await loadScopeState(this.db, kind, [c], orgId);
497
496
  const p = pluginFactsOf(st, c.itemId, orgId);
498
497
  const listed = listedIn(st, c.id, orgId);
499
498
  const sw = switchOf(st, c.id, orgId);
@@ -565,21 +564,7 @@ let ComponentScopeService = class ComponentScopeService {
565
564
  }
566
565
  }
567
566
  if (sw !== body.enabled) {
568
- await this.db
569
- .insertInto("component_org_switches")
570
- .values({
571
- kind,
572
- component_id: c.id,
573
- org_id: orgId,
574
- enabled: body.enabled,
575
- updated_by: actor,
576
- })
577
- .onConflict((oc) => oc.columns(["kind", "component_id", "org_id"]).doUpdateSet({
578
- enabled: body.enabled,
579
- updated_by: actor,
580
- updated_at: sql `now()`,
581
- }))
582
- .execute();
567
+ await this.scopeWriter.setOrgSwitch({ kind, id: c.id }, orgId, body.enabled, actor);
583
568
  await audit(this.db, {
584
569
  actorAccountId: actor,
585
570
  ...(actorTokenId !== undefined && { actorTokenId }),
@@ -628,7 +613,7 @@ let ComponentScopeService = class ComponentScopeService {
628
613
  const [c] = await loadComponents(this.db, kind, { ids: [id] });
629
614
  if (!c)
630
615
  throw notFound();
631
- const st = await loadScopeState(this.db, kind, [c]);
616
+ const st = await loadScopeState(this.db, kind, [c], orgId);
632
617
  const p = pluginFactsOf(st, c.itemId, orgId);
633
618
  const listed = listedIn(st, c.id, orgId);
634
619
  const sw = switchOf(st, c.id, orgId);
@@ -659,108 +644,95 @@ let ComponentScopeService = class ComponentScopeService {
659
644
  * `x.promote`: the org row to promote first, in this transaction. Returns the id written.
660
645
  */
661
646
  async writeScope(actor, kind, id, target, hash, opts, x) {
662
- await this.adminLock.lockAndAuthorize(actor);
663
- if (x.promote)
664
- id = await this.promotion.promote(x.promote, actor);
665
- const c = await this.component(kind, id, true);
666
- const t = await this.targetOf(c, target);
667
- if (kind === "theme")
668
- await this.assertKeepsPlatformDefault(c, t);
669
- const from = await this.listed(kind, id);
670
- // Its own scope: the owner stays listed (P3-17), widening goes through the share gate (P3-16);
671
- // following the plugin again takes the plugin's scope, which its own writer gates
672
- if (t.scopeMode === "own") {
673
- assertOwnerListed(gateRow(c), t);
674
- assertShareableTarget(gateRow(c), await this.effectiveScope(c, from), t);
675
- }
676
- const p = await this.compute(c, t, { hashId: x.hashId });
677
- if (p.scopeHash !== hash)
678
- throw staleError();
679
- if (p.conflicts.length > 0)
680
- throw nameConflictError(p.conflicts, kind);
681
- if (c.scopeMode === t.scopeMode && c.scope === t.scope && from.join() === t.orgs.join()) {
682
- return id;
683
- }
684
- const before = await this.effectiveScope(c, from);
685
- await this.db
686
- .updateTable(TABLE[kind])
687
- .set({
688
- scope_mode: t.scopeMode,
689
- scope: t.scope,
690
- updated_at: sql `now()`,
691
- })
692
- .where("id", "=", id)
693
- .execute();
694
- // org-filter: super admin's scope change (adminLock above) replaces the platform component's
695
- // whole named list, which spans orgs by design
696
- await this.db
697
- .deleteFrom("component_scope_orgs")
698
- .where("kind", "=", kind)
699
- .where("component_id", "=", id)
700
- .execute();
701
- if (t.scopeMode === "own" && t.orgs.length > 0) {
702
- await this.db
703
- .insertInto("component_scope_orgs")
704
- .values(t.orgs.map((org_id) => ({ kind, component_id: id, org_id })))
705
- .execute();
706
- }
707
- // An own scope that newly shows an org-owned component tells its owner (P3-22); not a theme:
708
- // the `item_shared` notice names plugins, skills and agent templates only
709
- if (t.scopeMode === "own" && kind !== "connector_template" && kind !== "theme") {
710
- const shared = newlySharedWith(before, t);
711
- if (shared === "all_orgs" || shared.length > 0) {
712
- await notifyItemShared(this.db, c.ownerOrgId, { kind, id }, c.name, shared);
647
+ const written = await this.scopeWriter.platformChange(actor, { kind: "scope", hash }, async () => {
648
+ if (x.promote)
649
+ id = await this.promotion.promote(x.promote, actor);
650
+ const c = await this.component(kind, id, true);
651
+ const t = await this.targetOf(c, target);
652
+ if (kind === "theme")
653
+ await this.assertKeepsPlatformDefault(c, t);
654
+ const from = await this.listed(kind, id);
655
+ // Its own scope: the owner stays listed (P3-17), widening goes through the share gate (P3-16);
656
+ // following the plugin again takes the plugin's scope, which its own writer gates
657
+ if (t.scopeMode === "own") {
658
+ assertOwnerListed(gateRow(c), t);
659
+ assertShareableTarget(gateRow(c), await this.effectiveScope(c, from), t);
713
660
  }
714
- }
715
- await audit(this.db, {
716
- actorAccountId: actor,
717
- ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
718
- action: "component.scope_changed",
719
- target: id,
720
- detail: {
721
- kind,
722
- name: c.name,
723
- plugin: c.plugin,
724
- from: { scopeMode: c.scopeMode, scope: c.scope, orgs: from },
725
- to: { scopeMode: t.scopeMode, scope: t.scope, orgs: t.orgs },
726
- ...opts.auditDetail,
727
- },
661
+ const p = await this.compute(c, t, { hashId: x.hashId });
662
+ return {
663
+ preview: p,
664
+ conflict: (conflicts) => nameConflictError(conflicts, kind),
665
+ apply: async () => {
666
+ if (c.scopeMode === t.scopeMode &&
667
+ c.scope === t.scope &&
668
+ from.join() === t.orgs.join()) {
669
+ return id;
670
+ }
671
+ const before = await this.effectiveScope(c, from);
672
+ await this.scopeWriter.replaceScope({
673
+ kind,
674
+ id,
675
+ scopeMode: t.scopeMode,
676
+ scope: t.scope,
677
+ orgs: t.orgs,
678
+ });
679
+ // An own scope that newly shows an org-owned component tells its owner (P3-22); not a theme:
680
+ // the `item_shared` notice names plugins, skills and agent templates only
681
+ if (t.scopeMode === "own" && kind !== "connector_template" && kind !== "theme") {
682
+ const shared = newlySharedWith(before, t);
683
+ if (shared === "all_orgs" || shared.length > 0) {
684
+ await notifyItemShared(this.db, c.ownerOrgId, { kind, id }, c.name, shared);
685
+ }
686
+ }
687
+ await audit(this.db, {
688
+ actorAccountId: actor,
689
+ ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
690
+ action: "component.scope_changed",
691
+ target: id,
692
+ detail: {
693
+ kind,
694
+ name: c.name,
695
+ plugin: c.plugin,
696
+ from: { scopeMode: c.scopeMode, scope: c.scope, orgs: from },
697
+ to: { scopeMode: t.scopeMode, scope: t.scope, orgs: t.orgs },
698
+ ...opts.auditDetail,
699
+ },
700
+ });
701
+ return id;
702
+ },
703
+ };
728
704
  });
729
- return id;
705
+ return written ?? id;
730
706
  }
731
707
  async writeDisabled(actor, kind, id, disabled, hash, opts) {
732
- await this.adminLock.lockAndAuthorize(actor);
733
- const c = await this.component(kind, id, true);
734
- if (c.disabled === disabled)
735
- return;
736
- const t = await this.targetOf(c, { disabled });
737
- if (kind === "theme")
738
- await this.assertKeepsPlatformDefault(c, t);
739
- const p = await this.compute(c, t);
740
- if (disabled && p.scopeHash !== hash)
741
- throw staleError();
742
- if (p.conflicts.length > 0)
743
- throw nameConflictError(p.conflicts, kind);
744
- await this.db
745
- .updateTable(TABLE[kind])
746
- .set({
747
- disabled,
748
- updated_at: sql `now()`,
749
- })
750
- .where("id", "=", id)
751
- .execute();
752
- await audit(this.db, {
753
- actorAccountId: actor,
754
- ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
755
- action: "component.scope_changed",
756
- target: id,
757
- detail: {
758
- kind,
759
- name: c.name,
760
- plugin: c.plugin,
761
- disabled: { from: c.disabled, to: disabled },
762
- ...opts.auditDetail,
763
- },
708
+ await this.scopeWriter.platformChange(actor, disabled ? { kind: "disable", hash } : { kind: "enable" }, async () => {
709
+ const c = await this.component(kind, id, true);
710
+ if (c.disabled === disabled)
711
+ return;
712
+ const t = await this.targetOf(c, { disabled });
713
+ if (kind === "theme")
714
+ await this.assertKeepsPlatformDefault(c, t);
715
+ const p = await this.compute(c, t);
716
+ return {
717
+ preview: p,
718
+ conflict: (conflicts) => nameConflictError(conflicts, kind),
719
+ apply: async () => {
720
+ await this.scopeWriter.setDisabled({ kind, id }, disabled);
721
+ await audit(this.db, {
722
+ actorAccountId: actor,
723
+ ...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
724
+ action: "component.scope_changed",
725
+ target: id,
726
+ detail: {
727
+ kind,
728
+ name: c.name,
729
+ plugin: c.plugin,
730
+ disabled: { from: c.disabled, to: disabled },
731
+ ...opts.auditDetail,
732
+ },
733
+ });
734
+ },
735
+ };
764
736
  });
765
737
  }
766
738
  /** `promote`: the org row to promote first, in this transaction. Returns the id written. */
@@ -777,7 +749,7 @@ let ComponentScopeService = class ComponentScopeService {
777
749
  if (c.shownToOthers === shown)
778
750
  return id;
779
751
  await this.db
780
- .updateTable(TABLE[kind])
752
+ .updateTable(COMPONENT_SCOPE_TABLE[kind])
781
753
  .set({ shown_to_others: shown, updated_at: sql `now()` })
782
754
  .where("id", "=", id)
783
755
  .execute();
@@ -1081,12 +1053,6 @@ let ComponentScopeService = class ComponentScopeService {
1081
1053
  __decorate([
1082
1054
  Transactional()
1083
1055
  ], ComponentScopeService.prototype, "setSwitch", null);
1084
- __decorate([
1085
- Transactional()
1086
- ], ComponentScopeService.prototype, "writeScope", null);
1087
- __decorate([
1088
- Transactional()
1089
- ], ComponentScopeService.prototype, "writeDisabled", null);
1090
1056
  __decorate([
1091
1057
  Transactional()
1092
1058
  ], ComponentScopeService.prototype, "writeShown", null);
@@ -1094,11 +1060,12 @@ ComponentScopeService = __decorate([
1094
1060
  Injectable(),
1095
1061
  __param(0, Inject(KernelDb)),
1096
1062
  __param(1, Inject(SuperAdminLock)),
1097
- __param(2, Inject(OrgLock)),
1098
- __param(3, Inject(ComponentPromotion)),
1099
- __param(4, Inject(TransactionHost)),
1100
- __param(5, Inject(PluginGuides)),
1101
- __param(6, Inject(PluginInstaller))
1063
+ __param(2, Inject(ScopeWriter)),
1064
+ __param(3, Inject(OrgLock)),
1065
+ __param(4, Inject(ComponentPromotion)),
1066
+ __param(5, Inject(TransactionHost)),
1067
+ __param(6, Inject(PluginGuides)),
1068
+ __param(7, Inject(PluginInstaller))
1102
1069
  ], ComponentScopeService);
1103
1070
  export { ComponentScopeService };
1104
1071
  /** Whether `c` is the platform default theme `uiTheme` (locked on in every org, themes spec §7). */
@@ -1143,9 +1110,3 @@ const gateRow = (c) => ({
1143
1110
  shareable: c.shareable,
1144
1111
  name: c.name,
1145
1112
  });
1146
- const TABLE = {
1147
- skill: "platform_skills",
1148
- agent_template: "platform_agent_templates",
1149
- connector_template: "platform_connector_templates",
1150
- theme: "platform_themes",
1151
- };
@@ -19,7 +19,7 @@ export interface ComponentScopeFacts {
19
19
  }
20
20
  /**
21
21
  * The component's plugin as one org sees it: `live` false for a deleted plugin; `listed` the org on
22
- * the plugin's `named` list; `installed` a live install of the plugin in the org.
22
+ * the plugin's `named` list; `usable` the effective plugin intent in the org.
23
23
  */
24
24
  export interface PluginFacts {
25
25
  live: boolean;
@@ -27,19 +27,20 @@ export interface PluginFacts {
27
27
  builtin: boolean;
28
28
  scope: PluginScope;
29
29
  listed: boolean;
30
- installed: boolean;
30
+ usable: boolean;
31
+ admitted?: boolean;
31
32
  }
32
33
  /**
33
34
  * Whether the component is on in the org (`listed`: the org on the component's own `named` list;
34
35
  * `sw`: the org's component switch):
35
36
  * - never when the component or its plugin is disabled, or its plugin is deleted;
36
- * - `follow`: its plugin is on in the org (a live install, or builtin) and the switch is not off
37
+ * - `follow`: its plugin is usable in the org and the switch is not off
37
38
  * (a `defaultOff` template of a plugin that is not builtin: the switch is on);
38
39
  * - `own`: `wantedOn` over the component's own scope, the list and the switch.
39
40
  */
40
41
  export declare function componentOn(c: ComponentScopeFacts, p: PluginFacts | null, listed: boolean, sw: boolean | null): boolean;
41
42
  /**
42
- * Whether the plugin a component follows is on in the org (a live install there, or builtin); a
43
+ * Whether the plugin a component follows is usable in the org; a
43
44
  * component with its own scope does not depend on it (true). A `follow` component comes on and off
44
45
  * with its plugin (spec §5 rule 5), so its own switch only matters while this holds.
45
46
  */
@@ -104,7 +105,8 @@ export interface ScopeState {
104
105
  disabled: boolean;
105
106
  }>;
106
107
  pluginListed: Map<string, Set<string>>;
107
- installed: Map<string, Set<string>>;
108
+ usable: Map<string, Set<string>>;
109
+ admitted: Map<string, Set<string>>;
108
110
  listed: Map<string, Set<string>>;
109
111
  switches: Map<string, Map<string, boolean>>;
110
112
  }
@@ -112,10 +114,10 @@ export interface ScopeState {
112
114
  export declare function loadScopeState(db: KernelSchemaDb, kind: ComponentKind, comps: readonly {
113
115
  id: string;
114
116
  itemId: string | null;
115
- }[]): Promise<ScopeState>;
117
+ }[], orgId?: string): Promise<ScopeState>;
116
118
  /**
117
119
  * Whether a catalog item is builtin: marked so and owned by the platform. An org-owned item marked
118
- * builtin (never so in practice) is an ordinary item: never on without an install, never locked,
120
+ * builtin (never so in practice) is an ordinary item with regular scope and switches,
119
121
  * and its skills can be turned off (skills default-on spec §3).
120
122
  */
121
123
  export declare const platformBuiltin: (item: {
@@ -235,7 +237,7 @@ export declare function connectorTemplateChoice(db: KernelSchemaDb, orgId: strin
235
237
  export declare function connectorsMadeFrom(db: KernelSchemaDb, orgId: string, id: string): Promise<number>;
236
238
  /**
237
239
  * Whether platform connector template `id` is on in the org (`on`), offered to it but off
238
- * (`off`: its plugin not installed for a `follow` template, or switched off there), or neither
240
+ * (`off`: its plugin off for a `follow` template, or the template switched off there), or neither
239
241
  * (`absent`: missing, deleted, without a plugin, stopped or not offered). One more connector from a
240
242
  * template needs `on` (ruling P4-T6-8), read before and again under the org lock.
241
243
  */
@@ -1,4 +1,4 @@
1
- import { LIVE_INSTALL_STATUSES } from "./install-status.js";
1
+ import { orgCatalogAdmits, orgUsable } from "./org-intent.js";
2
2
  import { visibleTo, wantedOn } from "./scope.js";
3
3
  import { templateDescription } from "./template-description.js";
4
4
  /** The kinds the scope, switch, ownership and preview flows (and the component routes) walk. */
@@ -20,7 +20,7 @@ function stopped(c, p) {
20
20
  * Whether the component is on in the org (`listed`: the org on the component's own `named` list;
21
21
  * `sw`: the org's component switch):
22
22
  * - never when the component or its plugin is disabled, or its plugin is deleted;
23
- * - `follow`: its plugin is on in the org (a live install, or builtin) and the switch is not off
23
+ * - `follow`: its plugin is usable in the org and the switch is not off
24
24
  * (a `defaultOff` template of a plugin that is not builtin: the switch is on);
25
25
  * - `own`: `wantedOn` over the component's own scope, the list and the switch.
26
26
  */
@@ -34,14 +34,14 @@ export function componentOn(c, p, listed, sw) {
34
34
  return wantedOn({ scope: c.scope, disabled: false, builtin: false }, listed, sw);
35
35
  }
36
36
  /**
37
- * Whether the plugin a component follows is on in the org (a live install there, or builtin); a
37
+ * Whether the plugin a component follows is usable in the org; a
38
38
  * component with its own scope does not depend on it (true). A `follow` component comes on and off
39
39
  * with its plugin (spec §5 rule 5), so its own switch only matters while this holds.
40
40
  */
41
41
  export function componentPluginOn(c, p) {
42
42
  if (c.scopeMode === "own")
43
43
  return true;
44
- return p !== null && p.live && (p.installed || p.builtin);
44
+ return p?.live === true && p.usable;
45
45
  }
46
46
  /**
47
47
  * Whether the component is offered to the org: on, or an org admin could turn it on there (spec
@@ -53,7 +53,9 @@ export function componentOffered(c, p, listed) {
53
53
  if (stopped(c, p))
54
54
  return false;
55
55
  if (c.scopeMode === "follow") {
56
- return p !== null && (p.builtin || visibleTo({ scope: p.scope }, p.listed));
56
+ return (p !== null &&
57
+ p.admitted !== false &&
58
+ (p.usable || p.builtin || visibleTo({ scope: p.scope }, p.listed)));
57
59
  }
58
60
  return visibleTo({ scope: c.scope }, listed);
59
61
  }
@@ -180,15 +182,16 @@ const groupBy = (rows, key, value) => {
180
182
  return out;
181
183
  };
182
184
  /** Reads the {@link ScopeState} of `comps` (all of one kind), one query after the other. */
183
- // org-filter: the scope state of platform components and their plugins for every org (named lists,
184
- // installs, switches keyed by platform ids); callers pick their own org out of it
185
- export async function loadScopeState(db, kind, comps) {
185
+ // org-filter: the scope state of platform components and their plugins for every org (named lists and
186
+ // switches keyed by platform IDs); org readers supply their org
187
+ export async function loadScopeState(db, kind, comps, orgId) {
186
188
  const itemIds = [...new Set(comps.map((c) => c.itemId).filter((x) => x !== null))];
187
189
  const ids = comps.map((c) => c.id);
188
190
  const state = {
189
191
  plugins: new Map(),
190
192
  pluginListed: new Map(),
191
- installed: new Map(),
193
+ usable: new Map(),
194
+ admitted: new Map(),
192
195
  listed: new Map(),
193
196
  switches: new Map(),
194
197
  };
@@ -212,13 +215,21 @@ export async function loadScopeState(db, kind, comps) {
212
215
  .where("item_id", "in", itemIds)
213
216
  .execute();
214
217
  state.pluginListed = groupBy(listed, (r) => r.item_id, (r) => r.org_id);
215
- const installs = await db
216
- .selectFrom("plugin_installs")
217
- .select(["item_id", "org_id"])
218
- .where("item_id", "in", itemIds)
219
- .where("status", "in", [...LIVE_INSTALL_STATUSES])
220
- .execute();
221
- state.installed = groupBy(installs, (r) => r.item_id, (r) => r.org_id);
218
+ const intentQuery = db
219
+ .selectFrom("catalog_items as candidate")
220
+ .crossJoin("orgs as org")
221
+ .select(["candidate.id as item_id", "org.id as org_id"])
222
+ .select(({ ref }) => [
223
+ orgUsable(db, ref("org.id"), ref("candidate.id")).as("usable"),
224
+ orgCatalogAdmits(db, ref("org.id"), ref("candidate.id")).as("admitted"),
225
+ ])
226
+ .where("candidate.id", "in", itemIds)
227
+ .where("org.deleted_at", "is", null);
228
+ const facts = await (orgId === undefined
229
+ ? intentQuery
230
+ : intentQuery.where("org.id", "=", orgId)).execute();
231
+ state.usable = groupBy(facts.filter((row) => row.usable), (row) => row.item_id, (row) => row.org_id);
232
+ state.admitted = groupBy(facts.filter((row) => row.admitted), (row) => row.item_id, (row) => row.org_id);
222
233
  }
223
234
  if (ids.length > 0) {
224
235
  const listed = await db
@@ -244,7 +255,7 @@ export async function loadScopeState(db, kind, comps) {
244
255
  }
245
256
  /**
246
257
  * Whether a catalog item is builtin: marked so and owned by the platform. An org-owned item marked
247
- * builtin (never so in practice) is an ordinary item: never on without an install, never locked,
258
+ * builtin (never so in practice) is an ordinary item with regular scope and switches,
248
259
  * and its skills can be turned off (skills default-on spec §3).
249
260
  */
250
261
  export const platformBuiltin = (item) => item.builtin === true && item.owner_org_id === null;
@@ -260,7 +271,7 @@ export function pluginFactsOf(st, itemId, orgId) {
260
271
  builtin: false,
261
272
  scope: "optional",
262
273
  listed: false,
263
- installed: false,
274
+ usable: false,
264
275
  };
265
276
  return {
266
277
  live: true,
@@ -268,7 +279,8 @@ export function pluginFactsOf(st, itemId, orgId) {
268
279
  builtin: p.builtin,
269
280
  scope: p.scope,
270
281
  listed: st.pluginListed.get(itemId)?.has(orgId) ?? false,
271
- installed: st.installed.get(itemId)?.has(orgId) ?? false,
282
+ usable: st.usable.get(itemId)?.has(orgId) ?? false,
283
+ admitted: st.admitted.get(itemId)?.has(orgId) ?? false,
272
284
  };
273
285
  }
274
286
  /** The org's switch of a component from a {@link ScopeState}. */
@@ -406,7 +418,7 @@ export async function offeredNamed(db, orgId, kind, name) {
406
418
  const comps = await loadComponents(db, kind, { name });
407
419
  if (comps.length === 0)
408
420
  return [];
409
- const st = await loadScopeState(db, kind, comps);
421
+ const st = await loadScopeState(db, kind, comps, orgId);
410
422
  return comps.filter((c) => componentOffered(c, pluginFactsOf(st, c.itemId, orgId), listedIn(st, c.id, orgId)));
411
423
  }
412
424
  /** The other platform skills named `name` that are on in the org (rule 10). */
@@ -414,7 +426,7 @@ export async function clashingPlatformSkills(db, orgId, component, name) {
414
426
  const others = (await loadComponents(db, "skill", { name })).filter((c) => c.id !== component.id);
415
427
  if (others.length === 0)
416
428
  return [];
417
- const st = await loadScopeState(db, "skill", others);
429
+ const st = await loadScopeState(db, "skill", others, orgId);
418
430
  return others.filter((c) => componentOn(c, pluginFactsOf(st, c.itemId, orgId), listedIn(st, c.id, orgId), switchOf(st, c.id, orgId)));
419
431
  }
420
432
  /**
@@ -439,7 +451,7 @@ export async function themeIdClashes(db, orgId, self, name) {
439
451
  const others = (await loadComponents(db, "theme", { name })).filter((c) => c.id !== self?.id);
440
452
  if (others.length === 0)
441
453
  return [];
442
- const st = await loadScopeState(db, "theme", others);
454
+ const st = await loadScopeState(db, "theme", others, orgId);
443
455
  return others.filter((c) => componentOn(c, pluginFactsOf(st, c.itemId, orgId), listedIn(st, c.id, orgId), switchOf(st, c.id, orgId)));
444
456
  }
445
457
  /** A connector template's id from its manifest path (`./connectors/<id>.json`). */
@@ -455,12 +467,12 @@ export async function connectorTemplatesOn(db, orgId, itemId, ids, defaultOff) {
455
467
  if (ids.length === 0)
456
468
  return new Set();
457
469
  const comps = await loadComponents(db, "connector_template", { itemId });
458
- const st = await loadScopeState(db, "connector_template", comps);
470
+ const st = await loadScopeState(db, "connector_template", comps, orgId);
459
471
  const off = new Set(comps
460
472
  .filter((c) => {
461
473
  const p = pluginFactsOf(st, c.itemId, orgId);
462
474
  const shipped = defaultOff?.get(c.name);
463
- return !componentOn(shipped === undefined ? c : { ...c, defaultOff: shipped }, p && { ...p, installed: true }, listedIn(st, c.id, orgId), switchOf(st, c.id, orgId));
475
+ return !componentOn(shipped === undefined ? c : { ...c, defaultOff: shipped }, p && { ...p, usable: true }, listedIn(st, c.id, orgId), switchOf(st, c.id, orgId));
464
476
  })
465
477
  .map((c) => c.name));
466
478
  return new Set(ids.filter((id) => !off.has(id)));
@@ -474,19 +486,19 @@ export async function connectorTemplatesOn(db, orgId, itemId, ids, defaultOff) {
474
486
  */
475
487
  export async function connectorTemplateChoice(db, orgId, itemId, chosen) {
476
488
  const comps = await loadComponents(db, "connector_template", { itemId });
477
- const st = await loadScopeState(db, "connector_template", comps);
489
+ const st = await loadScopeState(db, "connector_template", comps, orgId);
478
490
  const on = new Set();
479
491
  const switches = [];
480
492
  const refused = [];
481
493
  for (const c of comps) {
482
494
  const p = pluginFactsOf(st, c.itemId, orgId);
483
- const installed = p && { ...p, installed: true };
495
+ const proposed = p && { ...p, usable: true };
484
496
  const listed = listedIn(st, c.id, orgId);
485
497
  const sw = switchOf(st, c.id, orgId);
486
- const now = componentOn(c, installed, listed, sw);
498
+ const now = componentOn(c, proposed, listed, sw);
487
499
  const want = chosen.has(c.name);
488
500
  if (want) {
489
- if (!componentOn(c, installed, listed, true)) {
501
+ if (!componentOn(c, proposed, listed, true)) {
490
502
  refused.push(c.name);
491
503
  continue;
492
504
  }
@@ -515,7 +527,7 @@ export async function connectorsMadeFrom(db, orgId, id) {
515
527
  }
516
528
  /**
517
529
  * Whether platform connector template `id` is on in the org (`on`), offered to it but off
518
- * (`off`: its plugin not installed for a `follow` template, or switched off there), or neither
530
+ * (`off`: its plugin off for a `follow` template, or the template switched off there), or neither
519
531
  * (`absent`: missing, deleted, without a plugin, stopped or not offered). One more connector from a
520
532
  * template needs `on` (ruling P4-T6-8), read before and again under the org lock.
521
533
  */
@@ -523,7 +535,7 @@ export async function connectorTemplateState(db, orgId, id) {
523
535
  const [c] = await loadComponents(db, "connector_template", { ids: [id] });
524
536
  if (!c || c.itemId === null)
525
537
  return "absent";
526
- const st = await loadScopeState(db, "connector_template", [c]);
538
+ const st = await loadScopeState(db, "connector_template", [c], orgId);
527
539
  const p = pluginFactsOf(st, c.itemId, orgId);
528
540
  const listed = listedIn(st, c.id, orgId);
529
541
  if (componentOn(c, p, listed, switchOf(st, c.id, orgId)))
@@ -0,0 +1,25 @@
1
+ import type { PluginManifest } from "@capybara-hq/plugin-sdk";
2
+ import { type Insertable } from "kysely";
3
+ import type { CatalogDependencyEdgesTable, KernelSchemaDb } from "../db/kernel-schema.js";
4
+ interface SelectedVersion {
5
+ id: string;
6
+ name: string;
7
+ version: string;
8
+ manifest: PluginManifest;
9
+ }
10
+ interface TemplateDependency {
11
+ id: string;
12
+ itemId: string;
13
+ plugin: string;
14
+ range: string;
15
+ }
16
+ type Edge = Insertable<CatalogDependencyEdgesTable>;
17
+ /** Resolve catalog SemVer once when the selected versions change. Missing edges are explicit. */
18
+ export declare function resolveCatalogEdges(versions: readonly SelectedVersion[], templates: readonly TemplateDependency[]): Edge[];
19
+ /** Remove the retired source's edges and make incoming requirements unresolved. */
20
+ export declare function retireCatalogDependencyEdges(db: KernelSchemaDb, itemId: string): Promise<void>;
21
+ /** Complete the cutover once, including after an interruption before the projection committed. */
22
+ export declare function initializeCatalogDependencyEdges(db: KernelSchemaDb): Promise<void>;
23
+ /** Serialize publication in the caller's transaction without taking locks on other catalog items. */
24
+ export declare function refreshCatalogDependencyEdges(db: KernelSchemaDb): Promise<void>;
25
+ export {};