@capybara-hq/kernel 0.1.0-rc.137 → 0.1.0-rc.139
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.
- package/bootstrap.json +31 -31
- package/dist/admin/extensions.controller.d.ts +5 -2
- package/dist/admin/extensions.controller.js +10 -1
- package/dist/admin/extensions.schemas.d.ts +4 -0
- package/dist/admin/extensions.schemas.js +9 -0
- package/dist/admin/health.service.d.ts +1 -1
- package/dist/admin/health.service.js +3 -3
- package/dist/catalog/catalog-content.d.ts +5 -7
- package/dist/catalog/catalog-items.js +1 -1
- package/dist/catalog/catalog-scope.service.js +5 -5
- package/dist/catalog/catalog.module.js +5 -4
- package/dist/catalog/catalog.service.d.ts +7 -1
- package/dist/catalog/catalog.service.js +19 -2
- package/dist/catalog/effect-work.d.ts +27 -0
- package/dist/catalog/effect-work.js +151 -0
- package/dist/catalog/jobs.d.ts +4 -1
- package/dist/catalog/jobs.js +14 -4
- package/dist/catalog/org-catalog.service.d.ts +2 -0
- package/dist/catalog/org-catalog.service.js +68 -12
- package/dist/catalog/org-intent.d.ts +4 -2
- package/dist/catalog/org-intent.js +25 -4
- package/dist/catalog/owned-items.d.ts +5 -1
- package/dist/catalog/owned-items.js +18 -3
- package/dist/catalog/plugin-import.d.ts +4 -2
- package/dist/catalog/plugin-import.js +10 -5
- package/dist/catalog/plugin-install.d.ts +17 -51
- package/dist/catalog/plugin-install.js +209 -235
- package/dist/catalog/plugin-upgrade.d.ts +8 -3
- package/dist/catalog/plugin-upgrade.js +63 -34
- package/dist/catalog/scope-sync-workers.d.ts +7 -11
- package/dist/catalog/scope-sync-workers.js +10 -26
- package/dist/catalog/scope-sync.d.ts +19 -114
- package/dist/catalog/scope-sync.js +220 -239
- package/dist/catalog/scope-writer.d.ts +10 -1
- package/dist/catalog/scope-writer.js +62 -4
- package/dist/catalog/template-connector-ensure.js +1 -1
- package/dist/catalog/unshare.js +1 -2
- package/dist/config/schema.d.ts +1 -1
- package/dist/config/schema.js +7 -4
- package/dist/db/kernel-schema.d.ts +18 -17
- package/dist/erasure/org-catalog.js +2 -2
- package/dist/erasure/org-rules.js +2 -2
- package/dist/extensions/admin-ops.d.ts +1 -1
- package/dist/extensions/admin-ops.js +1 -1
- package/dist/extensions/admin-view.d.ts +4 -1
- package/dist/extensions/admin-view.js +27 -11
- package/dist/extensions/bootstrap.d.ts +1 -1
- package/dist/extensions/bootstrap.js +2 -3
- package/dist/extensions/enabled.d.ts +159 -0
- package/dist/extensions/enabled.js +7 -0
- package/dist/extensions/host.d.ts +1 -0
- package/dist/extensions/host.js +5 -2
- package/dist/extensions/isolated/authorization.d.ts +8 -0
- package/dist/extensions/isolated/authorization.js +56 -11
- package/dist/extensions/isolated/broker.js +30 -11
- package/dist/extensions/isolated/engine-binds.d.ts +9 -0
- package/dist/extensions/isolated/engine-binds.js +82 -0
- package/dist/extensions/isolated/engine-policy.d.ts +85 -0
- package/dist/extensions/isolated/engine-policy.js +75 -0
- package/dist/extensions/isolated/engine-proxy-main.d.ts +1 -0
- package/dist/extensions/isolated/engine-proxy-main.js +20 -0
- package/dist/extensions/isolated/engine-proxy.d.ts +7 -0
- package/dist/extensions/isolated/engine-proxy.js +408 -0
- package/dist/extensions/isolated/engine-vaults.d.ts +12 -0
- package/dist/extensions/isolated/engine-vaults.js +93 -0
- package/dist/extensions/isolated/lanes.d.ts +11 -3
- package/dist/extensions/isolated/lanes.js +43 -16
- package/dist/extensions/isolated/postgres-gate.d.ts +1 -0
- package/dist/extensions/isolated/postgres-gate.js +6 -6
- package/dist/extensions/isolated/revision.d.ts +1 -1
- package/dist/extensions/isolated/revision.js +3 -2
- package/dist/extensions/isolated/sandbox.d.ts +10 -6
- package/dist/extensions/isolated/sandbox.js +101 -117
- package/dist/extensions/lifecycle-topics.d.ts +1 -0
- package/dist/extensions/lifecycle-topics.js +1 -0
- package/dist/extensions/lifecycle.js +23 -2
- package/dist/extensions/permission-view.d.ts +6 -1
- package/dist/inbox/platform-inbox.service.js +16 -27
- package/dist/infra/events/kernel-events.d.ts +4 -0
- package/dist/infra/outbox/registry.d.ts +3 -36
- package/dist/infra/outbox/registry.js +4 -49
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -1
- package/dist/orgs/org-lock.d.ts +3 -1
- package/dist/orgs/org-lock.js +3 -1
- package/dist/orgs/orgs.service.d.ts +3 -1
- package/dist/orgs/orgs.service.js +15 -8
- package/dist/overrides.d.ts +0 -3
- package/dist/overrides.js +0 -2
- package/dist/platform/ops/extensions-admin.ops.js +20 -3
- package/dist/platform/ops/org-plugins.ops.js +5 -2
- package/dist/platform/ops/plugins.ops.js +11 -10
- package/dist/tools/registry.d.ts +1 -1
- package/dist/tools/registry.js +6 -2
- package/package.json +2 -2
- package/src/migrations/0231_plugin_effect_work.sql +171 -0
- package/src/migrations/0232_contract_plugin_effect_schema.sql +49 -0
- package/dist/catalog/install-status.d.ts +0 -5
- package/dist/catalog/install-status.js +0 -5
- package/dist/catalog/scope-jobs.d.ts +0 -59
- package/dist/catalog/scope-jobs.js +0 -78
|
@@ -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';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
SET LOCAL lock_timeout = '5s';
|
|
2
|
+
SET LOCAL capybara.scope = 'platform';
|
|
3
|
+
|
|
4
|
+
-- Hold the checked tables through commit so a writer cannot race the contract checks.
|
|
5
|
+
LOCK TABLE extension_org_configs IN ACCESS EXCLUSIVE MODE;
|
|
6
|
+
LOCK TABLE outbox, plugin_install_components IN SHARE MODE;
|
|
7
|
+
|
|
8
|
+
-- capybara:allow-destructive the contract needs conditional assertions with operator-facing errors before either drop
|
|
9
|
+
DO $$
|
|
10
|
+
DECLARE
|
|
11
|
+
switches_cutover timestamptz;
|
|
12
|
+
effects_cutover timestamptz;
|
|
13
|
+
BEGIN
|
|
14
|
+
SELECT applied_at INTO STRICT switches_cutover FROM _migrations
|
|
15
|
+
WHERE name = '0228_extension_org_switches.sql';
|
|
16
|
+
SELECT applied_at INTO STRICT effects_cutover FROM _migrations
|
|
17
|
+
WHERE name = '0231_plugin_effect_work.sql';
|
|
18
|
+
|
|
19
|
+
IF EXISTS (SELECT 1 FROM extension_org_configs WHERE updated_at >= switches_cutover) THEN
|
|
20
|
+
RAISE EXCEPTION '0232 blocked: extension_org_configs was written after 0228'
|
|
21
|
+
USING HINT = 'Stop legacy writers and reconcile these switches before retrying. See the plugin schema contract upgrade guide.';
|
|
22
|
+
END IF;
|
|
23
|
+
|
|
24
|
+
-- 0231 clears these artifacts in a same-init upgrade. Between releases they detect old writers.
|
|
25
|
+
IF EXISTS (SELECT 1 FROM outbox WHERE queue = 'cap-kernel-plugin-scope') THEN
|
|
26
|
+
RAISE EXCEPTION '0232 blocked: cap-kernel-plugin-scope work remains in the outbox'
|
|
27
|
+
USING HINT = 'Stop legacy writers and reconcile the remaining work before retrying.';
|
|
28
|
+
END IF;
|
|
29
|
+
|
|
30
|
+
IF EXISTS (SELECT 1 FROM plugin_install_components WHERE kind = 'extension') THEN
|
|
31
|
+
RAISE EXCEPTION '0232 blocked: plugin_install_components still has legacy extension rows'
|
|
32
|
+
USING HINT = 'Stop legacy writers and reconcile the remaining extension ownership before retrying.';
|
|
33
|
+
END IF;
|
|
34
|
+
|
|
35
|
+
IF to_regclass('capybara.plugin_scope_sync_failures') IS NOT NULL THEN
|
|
36
|
+
LOCK TABLE plugin_scope_sync_failures IN ACCESS EXCLUSIVE MODE;
|
|
37
|
+
IF EXISTS (SELECT 1 FROM plugin_scope_sync_failures WHERE failed_at >= effects_cutover) THEN
|
|
38
|
+
RAISE EXCEPTION '0232 blocked: plugin_scope_sync_failures was written after 0231'
|
|
39
|
+
USING HINT = 'Stop legacy writers and reconcile the unconverted failures before retrying.';
|
|
40
|
+
END IF;
|
|
41
|
+
END IF;
|
|
42
|
+
END
|
|
43
|
+
$$;
|
|
44
|
+
|
|
45
|
+
-- capybara:allow-destructive 0228 converted eligible switches and the guard rejects later legacy writes; keep row metadata and policy
|
|
46
|
+
ALTER TABLE extension_org_configs DROP COLUMN IF EXISTS enabled;
|
|
47
|
+
|
|
48
|
+
-- capybara:allow-destructive 0231 converted failure state to the effects ledger; the guard rejects failure snapshots written after the conversion
|
|
49
|
+
DROP TABLE IF EXISTS plugin_scope_sync_failures;
|
|
@@ -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
|
-
}
|