@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
@@ -0,0 +1,17 @@
1
+ -- A deployment projection of the selected catalog versions. The kernel populates it with
2
+ -- its catalog SemVer resolver before starting the new roles.
3
+ CREATE TABLE IF NOT EXISTS catalog_dependency_edges (
4
+ item_id uuid NOT NULL,
5
+ item_version text NOT NULL,
6
+ dependency_name text NOT NULL,
7
+ -- Empty for a declared dependency, otherwise the stable connector template ID.
8
+ template_id text NOT NULL DEFAULT '',
9
+ dependency_item_id uuid,
10
+ dependency_version text,
11
+ PRIMARY KEY (item_id, dependency_name, template_id),
12
+ CHECK ((dependency_item_id IS NULL) = (dependency_version IS NULL))
13
+ );
14
+
15
+ CREATE INDEX IF NOT EXISTS catalog_dependency_edges_reverse_idx
16
+ ON catalog_dependency_edges (dependency_item_id, dependency_version, item_id)
17
+ WHERE dependency_item_id IS NOT NULL;
@@ -0,0 +1,18 @@
1
+ -- The audit log's actor filter and actor options (table pagination spec §3): one org's (or, with
2
+ -- org_id IS NULL, the platform's) events by one account, newest first, and the distinct accounts
3
+ -- and tokens that acted, read by a skip scan. The indexes are not partial on org_id, so the same
4
+ -- index serves `org_id = $1` and `org_id IS NULL`. The time filter needs no new index: 0003's
5
+ -- audit_events_org_at_idx (org_id, at DESC) is not partial either and serves both scopes.
6
+ -- Plain CREATE INDEX: the migrator runs each file in a transaction.
7
+ CREATE INDEX IF NOT EXISTS audit_events_actor_idx
8
+ ON audit_events (org_id, actor_account_id, id DESC);
9
+
10
+ -- Events done with an access token (a small share of the log): the token filter and the token
11
+ -- options.
12
+ CREATE INDEX IF NOT EXISTS audit_events_token_idx
13
+ ON audit_events (org_id, actor_token_id, id DESC) WHERE actor_token_id IS NOT NULL;
14
+
15
+ -- The object filter (q): a target id matched exactly, or against the ids a name semi-join finds,
16
+ -- within the scope, newest first.
17
+ CREATE INDEX IF NOT EXISTS audit_events_target_idx
18
+ ON audit_events (org_id, target, id DESC);
@@ -0,0 +1,171 @@
1
+ SET LOCAL lock_timeout = '5s';
2
+
3
+ -- One generation per plugin and organization. The outbox uses the generation in each job ID.
4
+ CREATE TABLE IF NOT EXISTS plugin_effect_work (
5
+ item_id uuid NOT NULL REFERENCES catalog_items(id) ON DELETE RESTRICT,
6
+ org_id text NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
7
+ generation bigint NOT NULL CHECK (generation > 0),
8
+ state text NOT NULL CHECK (state IN ('pending', 'settled')),
9
+ generation_started_at timestamptz NOT NULL,
10
+ updated_at timestamptz NOT NULL,
11
+ PRIMARY KEY (item_id, org_id)
12
+ );
13
+
14
+ CREATE INDEX IF NOT EXISTS plugin_effect_work_org_idx ON plugin_effect_work (org_id, item_id);
15
+ CREATE INDEX IF NOT EXISTS plugin_effect_work_pending_idx
16
+ ON plugin_effect_work (updated_at, item_id, org_id) WHERE state = 'pending';
17
+
18
+ DROP POLICY IF EXISTS org_scope ON plugin_effect_work;
19
+ CREATE POLICY org_scope ON plugin_effect_work
20
+ USING (rls_admits(org_id)) WITH CHECK (rls_admits(org_id));
21
+ ALTER TABLE plugin_effect_work ENABLE ROW LEVEL SECURITY, FORCE ROW LEVEL SECURITY;
22
+
23
+ -- Keep applied ownership when old attempts left multiple records for the same pair.
24
+ CREATE TEMP TABLE IF NOT EXISTS effect_install_survivors ON COMMIT DROP AS
25
+ SELECT id, first_value(id) OVER (
26
+ PARTITION BY org_id, item_id
27
+ ORDER BY (status IN ('installed', 'on')) DESC, (status = 'uninstalling') DESC, installed_at DESC, id
28
+ ) AS keep_id
29
+ FROM plugin_installs;
30
+
31
+ CREATE TEMP TABLE IF NOT EXISTS legacy_effect_attempts ON COMMIT DROP AS
32
+ SELECT item_id, org_id, op AS operation, code, failed_at AS attempted_at
33
+ FROM plugin_scope_sync_failures
34
+ UNION ALL
35
+ SELECT item_id, org_id,
36
+ CASE WHEN status = 'uninstalling' THEN 'uninstall' ELSE 'install' END,
37
+ COALESCE(error->>'code', 'plugin.interrupted'), installed_at
38
+ FROM plugin_installs WHERE status IN ('installing', 'uninstalling', 'failed');
39
+
40
+ -- capybara:allow-destructive Bundle effects follow intent; extension ownership entries are retired and extension choices already live in component_org_switches.
41
+ DELETE FROM plugin_install_components WHERE kind = 'extension';
42
+
43
+ INSERT INTO plugin_install_components
44
+ (install_id, kind, ref, created, skill_version, template_id, dropped)
45
+ SELECT owner.keep_id, component.kind,
46
+ COALESCE(dependency.keep_id::text, component.ref),
47
+ bool_or(component.created), max(component.skill_version), min(component.template_id),
48
+ bool_and(component.dropped)
49
+ FROM plugin_install_components AS component
50
+ JOIN effect_install_survivors AS owner ON owner.id = component.install_id
51
+ LEFT JOIN effect_install_survivors AS dependency
52
+ ON component.kind = 'plugin' AND dependency.id::text = component.ref
53
+ WHERE owner.keep_id <> owner.id OR dependency.keep_id <> dependency.id
54
+ GROUP BY owner.keep_id, component.kind, COALESCE(dependency.keep_id::text, component.ref)
55
+ ON CONFLICT (install_id, kind, ref) DO UPDATE
56
+ SET created = plugin_install_components.created OR excluded.created,
57
+ skill_version = COALESCE(plugin_install_components.skill_version, excluded.skill_version),
58
+ template_id = COALESCE(plugin_install_components.template_id, excluded.template_id),
59
+ dropped = plugin_install_components.dropped AND excluded.dropped;
60
+
61
+ -- capybara:allow-destructive Duplicate dependency references were re-pointed to their surviving install above.
62
+ DELETE FROM plugin_install_components AS c USING effect_install_survivors AS m
63
+ WHERE c.kind = 'plugin' AND c.ref = m.id::text AND m.keep_id <> m.id;
64
+
65
+ -- capybara:allow-destructive Resource ownership was merged into the surviving install; the duplicate attempt rows are superseded.
66
+ DELETE FROM plugin_installs AS p USING effect_install_survivors AS m
67
+ WHERE p.id = m.id AND m.keep_id <> m.id;
68
+
69
+ -- capybara:allow-destructive Replace the legacy status check with the effect-ledger invariant below.
70
+ ALTER TABLE plugin_installs DROP CONSTRAINT IF EXISTS plugin_installs_status_check;
71
+ ALTER TABLE plugin_installs ALTER COLUMN version DROP NOT NULL;
72
+
73
+ WITH failures AS (
74
+ SELECT DISTINCT ON (item_id, org_id) item_id, org_id, operation, code, attempted_at
75
+ FROM legacy_effect_attempts
76
+ ORDER BY item_id, org_id, attempted_at DESC, operation, code
77
+ )
78
+ UPDATE plugin_installs AS p
79
+ SET status = 'install_failed',
80
+ version = CASE WHEN p.status IN ('installed', 'on', 'uninstalling') THEN p.version ELSE NULL END,
81
+ error = jsonb_build_object(
82
+ 'generation', '0', 'operation', f.operation, 'code', f.code,
83
+ 'message', 'Plugin setup was interrupted or failed before this upgrade.',
84
+ 'attemptedAt', to_char(f.attempted_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')
85
+ )
86
+ FROM failures AS f
87
+ WHERE p.item_id = f.item_id AND p.org_id = f.org_id
88
+ AND p.status IN ('installed', 'installing', 'uninstalling', 'failed')
89
+ AND (p.status <> 'installed' OR p.installed_at <= f.attempted_at);
90
+
91
+ UPDATE plugin_installs SET status = 'on', error = NULL WHERE status = 'installed';
92
+
93
+ INSERT INTO plugin_installs (org_id, item_id, id, version, status, installed_by, installed_at, error)
94
+ SELECT org_id, item_id, gen_random_uuid(), NULL, 'install_failed', NULL, failed_at,
95
+ jsonb_build_object(
96
+ 'generation', '0', 'operation', op, 'code', code,
97
+ 'message', 'Plugin setup failed before this upgrade.',
98
+ 'attemptedAt', to_char(failed_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')
99
+ )
100
+ FROM plugin_scope_sync_failures AS f
101
+ WHERE NOT EXISTS (
102
+ SELECT 1 FROM plugin_installs AS p WHERE p.org_id = f.org_id AND p.item_id = f.item_id
103
+ );
104
+
105
+ -- capybara:allow-destructive Replace the partial live-install index with uniqueness for every effect outcome.
106
+ DROP INDEX IF EXISTS app_installs_live_uq;
107
+ CREATE UNIQUE INDEX IF NOT EXISTS plugin_installs_pair_uq ON plugin_installs (org_id, item_id);
108
+ ALTER TABLE plugin_installs ADD CONSTRAINT plugin_installs_status_check CHECK (
109
+ (status = 'on' AND version IS NOT NULL AND error IS NULL)
110
+ OR (status = 'install_failed' AND error IS NOT NULL)
111
+ );
112
+
113
+ -- capybara:allow-destructive Pure content with no recorded or referenced org effects needs no applied install ledger; intent and catalog versions remain.
114
+ DELETE FROM plugin_installs AS p USING catalog_item_versions AS v
115
+ WHERE p.status = 'on' AND v.item_id = p.item_id AND v.version = p.version
116
+ AND v.manifest->'extension' IS NULL
117
+ AND CASE WHEN jsonb_typeof(v.manifest->'connectorTemplates') = 'array'
118
+ THEN jsonb_array_length(v.manifest->'connectorTemplates') = 0 ELSE true END
119
+ AND NOT EXISTS (
120
+ SELECT FROM plugin_install_components AS c
121
+ WHERE c.install_id = p.id OR (c.kind = 'plugin' AND c.ref = p.id::text)
122
+ );
123
+
124
+ -- Re-owe from current intent and ownership. This includes jobs the outbox already handed to
125
+ -- the old Redis queue before the maintenance window. The new queue never consumes their payloads.
126
+ WITH candidates(item_id, org_id) AS (
127
+ SELECT item.id, org.id
128
+ FROM catalog_items AS item CROSS JOIN orgs AS org
129
+ WHERE item.scope = 'all' AND item.deleted_at IS NULL AND NOT item.builtin
130
+ AND (item.owner_org_id IS NULL OR item.owner_org_id = org.id OR item.shareable)
131
+ UNION
132
+ SELECT item_id, org_id FROM catalog_item_scope_orgs
133
+ UNION
134
+ SELECT item_id, org_id FROM catalog_item_org_switches
135
+ UNION
136
+ SELECT t.catalog_item_id, choice.org_id
137
+ FROM platform_connector_templates AS t
138
+ JOIN component_org_switches AS choice
139
+ ON choice.kind = 'connector_template' AND choice.component_id = t.id
140
+ UNION
141
+ SELECT t.catalog_item_id, named.org_id
142
+ FROM platform_connector_templates AS t
143
+ JOIN component_scope_orgs AS named
144
+ ON named.kind = 'connector_template' AND named.component_id = t.id
145
+ UNION
146
+ SELECT t.catalog_item_id, org.id
147
+ FROM platform_connector_templates AS t CROSS JOIN orgs AS org
148
+ WHERE t.scope_mode = 'own' AND t.scope = 'all' AND t.deleted_at IS NULL
149
+ AND (t.owner_org_id IS NULL OR t.owner_org_id = org.id OR t.shareable)
150
+ UNION
151
+ SELECT item_id, org_id FROM plugin_installs
152
+ ), owed AS (
153
+ INSERT INTO plugin_effect_work (item_id, org_id, generation, state, generation_started_at, updated_at)
154
+ SELECT candidate.item_id, candidate.org_id, 1, 'pending',
155
+ statement_timestamp(), statement_timestamp()
156
+ FROM candidates AS candidate
157
+ JOIN orgs AS org ON org.id = candidate.org_id AND org.deleted_at IS NULL
158
+ JOIN catalog_items AS item ON item.id = candidate.item_id AND NOT item.builtin
159
+ ON CONFLICT (item_id, org_id) DO NOTHING
160
+ RETURNING item_id, org_id, generation
161
+ )
162
+ INSERT INTO outbox (queue, job_name, job_id, data, org_id)
163
+ SELECT 'cap-kernel-plugin-effects', 'plugin-effect',
164
+ item_id || '-' || org_id || '-' || generation,
165
+ jsonb_build_object('kind', 'chunk', 'itemId', item_id, 'orgIds', jsonb_build_array(org_id)),
166
+ org_id
167
+ FROM owed
168
+ ON CONFLICT (queue, job_id) WHERE dead_at IS NULL DO NOTHING;
169
+
170
+ -- capybara:allow-destructive Legacy jobs are superseded by current intent and ownership above; the new worker cannot consume the legacy payload.
171
+ DELETE FROM outbox WHERE queue = 'cap-kernel-plugin-scope';
@@ -1,6 +0,0 @@
1
- /**
2
- * The `plugin_installs.status` values of a live install: the plugin is on in the org (or about to
3
- * be). `failed` and `uninstalling` are not live. Every query that asks "is this plugin installed
4
- * in the org" uses this one set.
5
- */
6
- export declare const LIVE_INSTALL_STATUSES: readonly ["installing", "installed"];
@@ -1,6 +0,0 @@
1
- /**
2
- * The `plugin_installs.status` values of a live install: the plugin is on in the org (or about to
3
- * be). `failed` and `uninstalling` are not live. Every query that asks "is this plugin installed
4
- * in the org" uses this one set.
5
- */
6
- export const LIVE_INSTALL_STATUSES = ["installing", "installed"];
@@ -1,59 +0,0 @@
1
- import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
- import type { OutboxService } from "../infra/outbox/outbox.service.js";
3
- /**
4
- * The plugin scope sync's jobs as the outbox owes them (transactional outbox spec §8.1): a leaf
5
- * module (type imports only), so the import confirm (`PluginImport`, below the scope service), the
6
- * extension host's deploy (`ExtensionAdminOps`, `ExtensionAdmin`) and the CLI owe them without the
7
- * sync. `scope-sync.ts` (the consumer side) re-exports these names.
8
- */
9
- /** The scope sync queue (no `:` in the name). */
10
- export declare const PLUGIN_SCOPE_QUEUE = "cap-kernel-plugin-scope";
11
- /** The job fanning one item out to every org: one {@link SCOPE_CHUNK_JOB} per org chunk. */
12
- export declare const SCOPE_ITEM_JOB = "scope-item";
13
- /** The job converging one item in one chunk of orgs (at most {@link SCOPE_ORG_CHUNK} of them). */
14
- export declare const SCOPE_CHUNK_JOB = "scope-item-chunk";
15
- /** The job converging every item in one org. */
16
- export declare const SCOPE_ORG_JOB = "scope-org";
17
- /** How many orgs one chunk job converges. */
18
- export declare const SCOPE_ORG_CHUNK = 100;
19
- /**
20
- * Optional provider (a test seam): the chunk size instead of {@link SCOPE_ORG_CHUNK}, so a test
21
- * with a few orgs fans out to several chunks.
22
- */
23
- export declare const SCOPE_ORG_CHUNK_SIZE: unique symbol;
24
- /**
25
- * One item job per owing transaction (`xact`: its PostgreSQL transaction id, so two owes in one
26
- * transaction are one job, and a later transaction's job is never swallowed by an earlier one
27
- * still running; no `:`: item ids are UUIDs).
28
- */
29
- export declare const scopeItemJobId: (itemId: string, xact: string) => string;
30
- /** Chunk `n` of an item job (deterministic: a redelivered item job adds the same chunks). */
31
- export declare const scopeChunkJobId: (itemJobId: string, n: number) => string;
32
- /** One org job at a time per org. */
33
- export declare const scopeOrgJobId: (orgId: string) => string;
34
- /** The job's data. */
35
- export type ScopeJob = {
36
- kind: "item";
37
- itemId: string;
38
- } | {
39
- kind: "chunk";
40
- itemId: string;
41
- orgIds: string[];
42
- } | {
43
- kind: "org";
44
- orgId: string;
45
- };
46
- /**
47
- * Owes the item's scope sync in the transaction in context, one job per transaction (see
48
- * {@link scopeItemJobId}). A missing item owes nothing.
49
- */
50
- export declare function oweScopeItem(outbox: Pick<OutboxService, "add">, db: KernelSchemaDb, itemId: string): Promise<void>;
51
- /**
52
- * Owes the scope sync of the live catalog item of an extension (its plugin, by name; at most one
53
- * live item has a name) in the transaction that deploys it: the extension became enabled, a new
54
- * version of an enabled one was installed, or its permissions were approved while enabled (or
55
- * config-declared). The sync then installs it where its scope wants it on; a replica that has not
56
- * loaded the extension yet fails the chunk job, and BullMQ retries it. A builtin item (on
57
- * everywhere through its required extension) owes nothing.
58
- */
59
- export declare function oweDeployedExtensionSync(outbox: Pick<OutboxService, "add">, db: KernelSchemaDb, extensionName: string): Promise<void>;
@@ -1,78 +0,0 @@
1
- import { sql } from "kysely";
2
- /**
3
- * The plugin scope sync's jobs as the outbox owes them (transactional outbox spec §8.1): a leaf
4
- * module (type imports only), so the import confirm (`PluginImport`, below the scope service), the
5
- * extension host's deploy (`ExtensionAdminOps`, `ExtensionAdmin`) and the CLI owe them without the
6
- * sync. `scope-sync.ts` (the consumer side) re-exports these names.
7
- */
8
- /** The scope sync queue (no `:` in the name). */
9
- export const PLUGIN_SCOPE_QUEUE = "cap-kernel-plugin-scope";
10
- /** The job fanning one item out to every org: one {@link SCOPE_CHUNK_JOB} per org chunk. */
11
- export const SCOPE_ITEM_JOB = "scope-item";
12
- /** The job converging one item in one chunk of orgs (at most {@link SCOPE_ORG_CHUNK} of them). */
13
- export const SCOPE_CHUNK_JOB = "scope-item-chunk";
14
- /** The job converging every item in one org. */
15
- export const SCOPE_ORG_JOB = "scope-org";
16
- /** How many orgs one chunk job converges. */
17
- export const SCOPE_ORG_CHUNK = 100;
18
- /**
19
- * Optional provider (a test seam): the chunk size instead of {@link SCOPE_ORG_CHUNK}, so a test
20
- * with a few orgs fans out to several chunks.
21
- */
22
- export const SCOPE_ORG_CHUNK_SIZE = Symbol("SCOPE_ORG_CHUNK_SIZE");
23
- /**
24
- * One item job per owing transaction (`xact`: its PostgreSQL transaction id, so two owes in one
25
- * transaction are one job, and a later transaction's job is never swallowed by an earlier one
26
- * still running; no `:`: item ids are UUIDs).
27
- */
28
- export const scopeItemJobId = (itemId, xact) => `scope-item-${itemId}-x${xact}`;
29
- /** Chunk `n` of an item job (deterministic: a redelivered item job adds the same chunks). */
30
- export const scopeChunkJobId = (itemJobId, n) => `${itemJobId}-c${n}`;
31
- /** One org job at a time per org. */
32
- export const scopeOrgJobId = (orgId) => `scope-org-${orgId}`;
33
- /** The id of the transaction in context (assigned now if it has none yet). */
34
- async function currentXact(db) {
35
- const r = await sql `SELECT pg_current_xact_id()::text AS xact`.execute(db);
36
- const xact = r.rows[0]?.xact;
37
- if (xact === undefined)
38
- throw new Error("no transaction id");
39
- return xact;
40
- }
41
- /**
42
- * Owes the item's scope sync in the transaction in context, one job per transaction (see
43
- * {@link scopeItemJobId}). A missing item owes nothing.
44
- */
45
- export async function oweScopeItem(outbox, db, itemId) {
46
- const row = await db
47
- .selectFrom("catalog_items")
48
- .select("id")
49
- .where("id", "=", itemId)
50
- .executeTakeFirst();
51
- if (!row)
52
- return;
53
- await outbox.add({
54
- queue: PLUGIN_SCOPE_QUEUE,
55
- name: SCOPE_ITEM_JOB,
56
- jobId: scopeItemJobId(itemId, await currentXact(db)),
57
- data: { kind: "item", itemId },
58
- });
59
- }
60
- /**
61
- * Owes the scope sync of the live catalog item of an extension (its plugin, by name; at most one
62
- * live item has a name) in the transaction that deploys it: the extension became enabled, a new
63
- * version of an enabled one was installed, or its permissions were approved while enabled (or
64
- * config-declared). The sync then installs it where its scope wants it on; a replica that has not
65
- * loaded the extension yet fails the chunk job, and BullMQ retries it. A builtin item (on
66
- * everywhere through its required extension) owes nothing.
67
- */
68
- export async function oweDeployedExtensionSync(outbox, db, extensionName) {
69
- const item = await db
70
- .selectFrom("catalog_items")
71
- .select("id")
72
- .where("name", "=", extensionName)
73
- .where("deleted_at", "is", null)
74
- .where("builtin", "=", false)
75
- .executeTakeFirst();
76
- if (item)
77
- await oweScopeItem(outbox, db, item.id);
78
- }