@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.
- package/dist/admin/audit-log.service.d.ts +96 -12
- package/dist/admin/audit-log.service.js +508 -44
- package/dist/admin/audit-targets.d.ts +17 -0
- package/dist/admin/audit-targets.js +84 -0
- package/dist/admin/audit.controller.d.ts +12 -9
- package/dist/admin/audit.controller.js +29 -8
- package/dist/admin/audit.schemas.d.ts +20 -4
- package/dist/admin/audit.schemas.js +42 -17
- 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/agents/templates.js +3 -10
- package/dist/app.d.ts +3 -1
- package/dist/app.js +1 -0
- package/dist/automations/automations.service.js +4 -5
- package/dist/catalog/catalog-content.d.ts +6 -8
- package/dist/catalog/catalog-content.js +17 -22
- package/dist/catalog/catalog-items.d.ts +5 -10
- package/dist/catalog/catalog-items.js +27 -51
- package/dist/catalog/catalog-scope.service.d.ts +4 -2
- package/dist/catalog/catalog-scope.service.js +109 -145
- package/dist/catalog/catalog.module.js +7 -4
- package/dist/catalog/catalog.service.d.ts +10 -4
- package/dist/catalog/catalog.service.js +52 -10
- package/dist/catalog/cli.js +17 -0
- package/dist/catalog/component-promotion.js +8 -4
- package/dist/catalog/component-scope.service.d.ts +3 -1
- package/dist/catalog/component-scope.service.js +101 -140
- package/dist/catalog/component-state.d.ts +10 -8
- package/dist/catalog/component-state.js +42 -30
- package/dist/catalog/dependency-edges.d.ts +25 -0
- package/dist/catalog/dependency-edges.js +125 -0
- 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 +5 -1
- package/dist/catalog/org-catalog.service.js +101 -106
- package/dist/catalog/org-intent.d.ts +45 -0
- package/dist/catalog/org-intent.js +205 -0
- package/dist/catalog/owned-items.d.ts +6 -2
- package/dist/catalog/owned-items.js +27 -32
- package/dist/catalog/ownership.d.ts +2 -3
- package/dist/catalog/ownership.js +10 -28
- package/dist/catalog/platform-components.d.ts +3 -1
- package/dist/catalog/platform-components.js +9 -1
- package/dist/catalog/plugin-import.d.ts +4 -2
- package/dist/catalog/plugin-import.js +22 -21
- package/dist/catalog/plugin-install.d.ts +19 -54
- package/dist/catalog/plugin-install.js +221 -271
- package/dist/catalog/plugin-upgrade.d.ts +10 -5
- package/dist/catalog/plugin-upgrade.js +80 -67
- package/dist/catalog/request-rules.js +20 -8
- package/dist/catalog/scope-orgs.js +2 -7
- package/dist/catalog/scope-state.d.ts +6 -6
- package/dist/catalog/scope-state.js +24 -43
- 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 -116
- package/dist/catalog/scope-sync.js +228 -275
- package/dist/catalog/scope-writer.d.ts +87 -0
- package/dist/catalog/scope-writer.js +239 -0
- package/dist/catalog/template-connector-ensure.js +1 -1
- package/dist/catalog/unshare.js +9 -43
- package/dist/channels/connections.service.js +6 -6
- package/dist/channels/supervisor.js +3 -7
- package/dist/client/client-access.service.js +2 -12
- package/dist/config/schema.d.ts +6 -0
- package/dist/config/schema.js +32 -1
- package/dist/ctx/create-ctx.d.ts +2 -2
- package/dist/ctx/create-ctx.js +2 -60
- package/dist/ctx/guard-db.d.ts +6 -0
- package/dist/ctx/guard-db.js +55 -0
- package/dist/db/extension-handoff-schema.d.ts +1 -1
- package/dist/db/extension-handoff-schema.js +48 -10
- package/dist/db/extension-schema-transfer.js +5 -2
- package/dist/db/kernel-schema.d.ts +32 -6
- package/dist/device-access/host-services.d.ts +2 -2
- package/dist/device-access/host-services.js +5 -6
- package/dist/erasure/org-catalog.js +2 -2
- package/dist/erasure/org-rules.js +7 -0
- package/dist/extensions/admin-ops.d.ts +10 -6
- package/dist/extensions/admin-ops.js +53 -9
- package/dist/extensions/admin-view.d.ts +4 -1
- package/dist/extensions/admin-view.js +27 -14
- package/dist/extensions/bootstrap.d.ts +1 -1
- package/dist/extensions/bootstrap.js +6 -4
- package/dist/extensions/cli.js +1 -0
- package/dist/extensions/effective-config.js +1 -4
- package/dist/extensions/enabled.d.ts +183 -29
- package/dist/extensions/enabled.js +148 -62
- package/dist/extensions/host.d.ts +65 -45
- package/dist/extensions/host.js +446 -128
- package/dist/extensions/installs.d.ts +9 -0
- package/dist/extensions/installs.js +23 -0
- package/dist/extensions/isolated/admission.d.ts +10 -0
- package/dist/extensions/isolated/admission.js +206 -0
- package/dist/extensions/isolated/authorization.d.ts +45 -0
- package/dist/extensions/isolated/authorization.js +191 -0
- package/dist/extensions/isolated/broker.d.ts +15 -0
- package/dist/extensions/isolated/broker.js +471 -0
- package/dist/extensions/isolated/child-protocol.d.ts +84 -0
- package/dist/extensions/isolated/child-protocol.js +21 -0
- package/dist/extensions/isolated/child-registration.d.ts +22 -0
- package/dist/extensions/isolated/child-registration.js +152 -0
- package/dist/extensions/isolated/child-session.d.ts +6 -0
- package/dist/extensions/isolated/child-session.js +343 -0
- package/dist/extensions/isolated/child-streams.d.ts +4 -0
- package/dist/extensions/isolated/child-streams.js +142 -0
- package/dist/extensions/isolated/child.d.ts +1 -0
- package/dist/extensions/isolated/child.js +10 -0
- package/dist/extensions/isolated/configured-package.d.ts +15 -0
- package/dist/extensions/isolated/configured-package.js +73 -0
- 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 +108 -0
- package/dist/extensions/isolated/lanes.js +726 -0
- package/dist/extensions/isolated/postgres-gate.d.ts +15 -0
- package/dist/extensions/isolated/postgres-gate.js +255 -0
- package/dist/extensions/isolated/revision.d.ts +32 -0
- package/dist/extensions/isolated/revision.js +359 -0
- package/dist/extensions/isolated/rpc-frame.d.ts +50 -0
- package/dist/extensions/isolated/rpc-frame.js +193 -0
- package/dist/extensions/isolated/rpc.d.ts +66 -0
- package/dist/extensions/isolated/rpc.js +314 -0
- package/dist/extensions/isolated/sandbox.d.ts +39 -0
- package/dist/extensions/isolated/sandbox.js +356 -0
- package/dist/extensions/isolated/tier.d.ts +6 -0
- package/dist/extensions/isolated/tier.js +45 -0
- package/dist/extensions/ledger.d.ts +1 -1
- package/dist/extensions/lifecycle-topics.d.ts +2 -0
- package/dist/extensions/lifecycle-topics.js +2 -0
- package/dist/extensions/lifecycle.d.ts +3 -1
- package/dist/extensions/lifecycle.js +53 -5
- package/dist/extensions/locks.d.ts +2 -0
- package/dist/extensions/locks.js +7 -0
- package/dist/extensions/org-config.d.ts +17 -27
- package/dist/extensions/org-config.js +114 -129
- package/dist/extensions/permission-view.d.ts +6 -1
- package/dist/extensions/plugin-guides.d.ts +5 -10
- package/dist/extensions/plugin-guides.js +18 -37
- package/dist/extensions/plugin-icons.d.ts +3 -21
- package/dist/extensions/plugin-icons.js +9 -44
- package/dist/extensions/plugin-items.d.ts +10 -30
- package/dist/extensions/plugin-items.js +32 -73
- package/dist/extensions/source-intent.d.ts +14 -0
- package/dist/extensions/source-intent.js +77 -0
- package/dist/inbox/platform-inbox.service.js +16 -27
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/infra/database/database.module.js +6 -1
- package/dist/infra/database/kernel-db.d.ts +3 -1
- package/dist/infra/database/kernel-db.js +4 -0
- 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/paging/paged-read.d.ts +56 -0
- package/dist/infra/paging/paged-read.js +78 -0
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -1
- package/dist/kernel.d.ts +3 -2
- package/dist/kernel.js +21 -2
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +22 -12
- package/dist/lifecycle/tokens.d.ts +8 -0
- package/dist/lifecycle/tokens.js +2 -0
- package/dist/orgs/directory-sync.js +1 -4
- 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/admin.ops.js +14 -3
- package/dist/platform/ops/extensions-admin.ops.js +20 -3
- package/dist/platform/ops/org-plugins.ops.js +5 -2
- package/dist/platform/ops/orgs.ops.js +13 -3
- package/dist/platform/ops/plugins.ops.js +11 -10
- package/dist/setup/setup-checks.service.js +3 -2
- package/dist/skills/usable.js +41 -30
- package/dist/themes/theme-state.js +3 -3
- package/dist/tools/registry.d.ts +2 -2
- package/dist/tools/registry.js +7 -3
- package/package.json +2 -2
- package/src/migrations/0228_extension_org_switches.sql +56 -0
- package/src/migrations/0229_catalog_dependency_edges.sql +17 -0
- package/src/migrations/0230_audit_filter_idx.sql +18 -0
- package/src/migrations/0231_plugin_effect_work.sql +171 -0
- package/dist/catalog/install-status.d.ts +0 -6
- package/dist/catalog/install-status.js +0 -6
- package/dist/catalog/scope-jobs.d.ts +0 -59
- package/dist/catalog/scope-jobs.js +0 -78
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import { satisfiesRange } from "./versions.js";
|
|
3
|
+
/** Resolve catalog SemVer once when the selected versions change. Missing edges are explicit. */
|
|
4
|
+
export function resolveCatalogEdges(versions, templates) {
|
|
5
|
+
const byName = new Map(versions.map((version) => [version.name, version]));
|
|
6
|
+
const byId = new Map(versions.map((version) => [version.id, version]));
|
|
7
|
+
const edges = [];
|
|
8
|
+
const add = (source, name, range, templateId) => {
|
|
9
|
+
const target = byName.get(name);
|
|
10
|
+
const resolved = target !== undefined && satisfiesRange(target.version, range) ? target : null;
|
|
11
|
+
edges.push({
|
|
12
|
+
item_id: source.id,
|
|
13
|
+
item_version: source.version,
|
|
14
|
+
dependency_name: name,
|
|
15
|
+
template_id: templateId,
|
|
16
|
+
dependency_item_id: resolved?.id ?? null,
|
|
17
|
+
dependency_version: resolved?.version ?? null,
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
for (const source of versions) {
|
|
21
|
+
for (const dependency of source.manifest.dependencies ?? []) {
|
|
22
|
+
add(source, dependency.name, dependency.version, "");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
for (const template of templates) {
|
|
26
|
+
const source = byId.get(template.itemId);
|
|
27
|
+
if (source === undefined ||
|
|
28
|
+
template.plugin === source.name ||
|
|
29
|
+
!byName.has(template.plugin) ||
|
|
30
|
+
source.manifest.dependencies?.some((dependency) => dependency.name === template.plugin))
|
|
31
|
+
continue;
|
|
32
|
+
add(source, template.plugin, template.range, template.id);
|
|
33
|
+
}
|
|
34
|
+
return edges;
|
|
35
|
+
}
|
|
36
|
+
const initializedKey = "catalogDependencyEdgesInitialized";
|
|
37
|
+
async function lockCatalogDependencyEdges(db) {
|
|
38
|
+
await sql `LOCK TABLE capybara.catalog_dependency_edges IN SHARE ROW EXCLUSIVE MODE`.execute(db);
|
|
39
|
+
}
|
|
40
|
+
/** Remove the retired source's edges and make incoming requirements unresolved. */
|
|
41
|
+
export async function retireCatalogDependencyEdges(db, itemId) {
|
|
42
|
+
await lockCatalogDependencyEdges(db);
|
|
43
|
+
await db.deleteFrom("catalog_dependency_edges").where("item_id", "=", itemId).execute();
|
|
44
|
+
await db
|
|
45
|
+
.updateTable("catalog_dependency_edges")
|
|
46
|
+
.set({ dependency_item_id: null, dependency_version: null })
|
|
47
|
+
.where("dependency_item_id", "=", itemId)
|
|
48
|
+
.execute();
|
|
49
|
+
}
|
|
50
|
+
/** Complete the cutover once, including after an interruption before the projection committed. */
|
|
51
|
+
export async function initializeCatalogDependencyEdges(db) {
|
|
52
|
+
const initialized = () => db
|
|
53
|
+
.selectFrom("deployment_settings")
|
|
54
|
+
.select("key")
|
|
55
|
+
.where("key", "=", initializedKey)
|
|
56
|
+
.executeTakeFirst();
|
|
57
|
+
if (await initialized())
|
|
58
|
+
return;
|
|
59
|
+
await lockCatalogDependencyEdges(db);
|
|
60
|
+
if (await initialized())
|
|
61
|
+
return;
|
|
62
|
+
await writeCatalogDependencyEdges(db);
|
|
63
|
+
await db
|
|
64
|
+
.insertInto("deployment_settings")
|
|
65
|
+
.values({ key: initializedKey, value: "true", updated_by: null })
|
|
66
|
+
.execute();
|
|
67
|
+
}
|
|
68
|
+
/** Serialize publication in the caller's transaction without taking locks on other catalog items. */
|
|
69
|
+
export async function refreshCatalogDependencyEdges(db) {
|
|
70
|
+
await lockCatalogDependencyEdges(db);
|
|
71
|
+
await writeCatalogDependencyEdges(db);
|
|
72
|
+
}
|
|
73
|
+
async function writeCatalogDependencyEdges(db) {
|
|
74
|
+
const versions = await db
|
|
75
|
+
.selectFrom("catalog_items as item")
|
|
76
|
+
.innerJoin("catalog_item_versions as version", (join) => join
|
|
77
|
+
.onRef("version.item_id", "=", "item.id")
|
|
78
|
+
.on("version.version", "=", sql `coalesce(item.platform_synced_version, item.latest_version)`))
|
|
79
|
+
.select(["item.id", "item.name", "version.version", "version.manifest"])
|
|
80
|
+
.where("item.deleted_at", "is", null)
|
|
81
|
+
.execute();
|
|
82
|
+
const templates = await db
|
|
83
|
+
.selectFrom("platform_connector_templates")
|
|
84
|
+
.select(["id", "catalog_item_id", "file"])
|
|
85
|
+
.where("deleted_at", "is", null)
|
|
86
|
+
.execute();
|
|
87
|
+
const edges = resolveCatalogEdges(versions.map((version) => ({ ...version, manifest: version.manifest })), templates.flatMap((row) => {
|
|
88
|
+
const template = row.file.template;
|
|
89
|
+
if (template === undefined)
|
|
90
|
+
return [];
|
|
91
|
+
return [
|
|
92
|
+
{
|
|
93
|
+
id: row.id,
|
|
94
|
+
itemId: row.catalog_item_id,
|
|
95
|
+
plugin: template.plugin,
|
|
96
|
+
range: template.pluginVersion ?? "*",
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
}));
|
|
100
|
+
await sql `
|
|
101
|
+
WITH next_edges AS (
|
|
102
|
+
SELECT * FROM jsonb_to_recordset(${JSON.stringify(edges)}::jsonb) AS edge(
|
|
103
|
+
item_id uuid, item_version text, dependency_name text, template_id text,
|
|
104
|
+
dependency_item_id uuid, dependency_version text
|
|
105
|
+
)
|
|
106
|
+
), removed AS (
|
|
107
|
+
DELETE FROM capybara.catalog_dependency_edges AS old
|
|
108
|
+
WHERE NOT EXISTS (
|
|
109
|
+
SELECT 1 FROM next_edges AS edge
|
|
110
|
+
WHERE (edge.item_id, edge.dependency_name, edge.template_id)
|
|
111
|
+
= (old.item_id, old.dependency_name, old.template_id)
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
INSERT INTO capybara.catalog_dependency_edges AS old
|
|
115
|
+
(item_id, item_version, dependency_name, template_id, dependency_item_id, dependency_version)
|
|
116
|
+
SELECT item_id, item_version, dependency_name, template_id, dependency_item_id, dependency_version
|
|
117
|
+
FROM next_edges
|
|
118
|
+
ON CONFLICT (item_id, dependency_name, template_id) DO UPDATE
|
|
119
|
+
SET item_version = excluded.item_version,
|
|
120
|
+
dependency_item_id = excluded.dependency_item_id,
|
|
121
|
+
dependency_version = excluded.dependency_version
|
|
122
|
+
WHERE (old.item_version, old.dependency_item_id, old.dependency_version) IS DISTINCT FROM
|
|
123
|
+
(excluded.item_version, excluded.dependency_item_id, excluded.dependency_version)
|
|
124
|
+
`.execute(db);
|
|
125
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
2
|
+
import type { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
3
|
+
export declare const PLUGIN_EFFECT_QUEUE = "cap-kernel-plugin-effects";
|
|
4
|
+
export declare const PLUGIN_EFFECT_JOB = "plugin-effect";
|
|
5
|
+
export declare const EFFECT_CANDIDATES_LOCK = "capybara.plugin-effect-candidates";
|
|
6
|
+
export type EffectJob = {
|
|
7
|
+
kind: "chunk";
|
|
8
|
+
itemId: string;
|
|
9
|
+
orgIds: [string];
|
|
10
|
+
};
|
|
11
|
+
type Outbox = Pick<OutboxService, "addMany">;
|
|
12
|
+
export declare function effectGeneration(jobId: string, { itemId, orgIds }: EffectJob): string;
|
|
13
|
+
type EffectMutation = {
|
|
14
|
+
itemIds: readonly string[];
|
|
15
|
+
previousOrgs?: readonly string[];
|
|
16
|
+
orgId?: string;
|
|
17
|
+
includeDependents?: boolean;
|
|
18
|
+
} | {
|
|
19
|
+
orgId: string;
|
|
20
|
+
};
|
|
21
|
+
/** Selects sparse intent or ownership first. Only an all-scope root needs the org census. */
|
|
22
|
+
export declare function oweEffectWork(outbox: Outbox, db: KernelSchemaDb, mutation: EffectMutation): Promise<void>;
|
|
23
|
+
/** Re-owes the same generation if a worker did not settle it. */
|
|
24
|
+
export declare function recoverEffectWork(outbox: Outbox, db: KernelSchemaDb): Promise<void>;
|
|
25
|
+
/** Resolve a deployed extension's live catalog item before owing its affected pairs. */
|
|
26
|
+
export declare function oweDeployedExtensionSync(outbox: Outbox, db: KernelSchemaDb, extensionName: string): Promise<void>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
3
|
+
export const PLUGIN_EFFECT_QUEUE = "cap-kernel-plugin-effects";
|
|
4
|
+
export const PLUGIN_EFFECT_JOB = "plugin-effect";
|
|
5
|
+
export const EFFECT_CANDIDATES_LOCK = "capybara.plugin-effect-candidates";
|
|
6
|
+
const pairPrefix = (itemId, orgId) => `${itemId}-${orgId}-`;
|
|
7
|
+
export function effectGeneration(jobId, { itemId, orgIds }) {
|
|
8
|
+
const orgId = orgIds[0];
|
|
9
|
+
const prefix = pairPrefix(itemId, orgId);
|
|
10
|
+
const generation = jobId.slice(prefix.length);
|
|
11
|
+
if (orgIds.length !== 1 ||
|
|
12
|
+
!orgId ||
|
|
13
|
+
!jobId.startsWith(prefix) ||
|
|
14
|
+
jobId.includes(":") ||
|
|
15
|
+
!/^[1-9][0-9]{0,18}$/.test(generation) ||
|
|
16
|
+
BigInt(generation) > 9223372036854775807n) {
|
|
17
|
+
throw new Error("invalid plugin effect job id");
|
|
18
|
+
}
|
|
19
|
+
return generation;
|
|
20
|
+
}
|
|
21
|
+
async function owePairs(outbox, pairs) {
|
|
22
|
+
await outbox.addMany(pairs.map(({ item_id: itemId, org_id: orgId, generation }) => ({
|
|
23
|
+
queue: PLUGIN_EFFECT_QUEUE,
|
|
24
|
+
name: PLUGIN_EFFECT_JOB,
|
|
25
|
+
jobId: `${pairPrefix(itemId, orgId)}${generation}`,
|
|
26
|
+
data: { kind: "chunk", itemId, orgIds: [orgId] },
|
|
27
|
+
orgId,
|
|
28
|
+
})));
|
|
29
|
+
}
|
|
30
|
+
/** Selects sparse intent or ownership first. Only an all-scope root needs the org census. */
|
|
31
|
+
export async function oweEffectWork(outbox, db, mutation) {
|
|
32
|
+
const itemIds = "itemIds" in mutation ? [...mutation.itemIds] : null;
|
|
33
|
+
if (itemIds?.length === 0)
|
|
34
|
+
return;
|
|
35
|
+
const previousOrgs = "itemIds" in mutation ? [...(mutation.previousOrgs ?? [])] : [];
|
|
36
|
+
const orgId = mutation.orgId ?? null;
|
|
37
|
+
const includeDependents = !("itemIds" in mutation) || mutation.includeDependents !== false;
|
|
38
|
+
// org-filter: deployment-wide effect candidates derived from a single committed intent graph.
|
|
39
|
+
await asPlatform("Select affected plugin effect pairs", async () => {
|
|
40
|
+
const { rows } = await sql `
|
|
41
|
+
WITH RECURSIVE affected(id) AS (
|
|
42
|
+
SELECT id FROM capybara.catalog_items
|
|
43
|
+
WHERE (${itemIds}::uuid[] IS NULL OR id = ANY(${itemIds}::uuid[]))
|
|
44
|
+
UNION
|
|
45
|
+
SELECT edge.item_id
|
|
46
|
+
FROM capybara.catalog_dependency_edges AS edge
|
|
47
|
+
JOIN affected ON affected.id = edge.dependency_item_id
|
|
48
|
+
WHERE ${includeDependents}::boolean
|
|
49
|
+
), subjects AS (
|
|
50
|
+
SELECT item.id, item.scope, item.owner_org_id, item.shareable, item.deleted_at
|
|
51
|
+
FROM affected
|
|
52
|
+
JOIN capybara.catalog_items AS item USING (id)
|
|
53
|
+
WHERE NOT item.builtin
|
|
54
|
+
), candidates(item_id, org_id) AS (
|
|
55
|
+
SELECT subjects.id, org.id
|
|
56
|
+
FROM subjects CROSS JOIN capybara.orgs AS org
|
|
57
|
+
WHERE subjects.scope = 'all' AND subjects.deleted_at IS NULL
|
|
58
|
+
AND (subjects.owner_org_id IS NULL OR subjects.owner_org_id = org.id OR subjects.shareable)
|
|
59
|
+
AND (${orgId}::text IS NULL OR org.id = ${orgId})
|
|
60
|
+
UNION
|
|
61
|
+
SELECT named.item_id, named.org_id
|
|
62
|
+
FROM capybara.catalog_item_scope_orgs AS named
|
|
63
|
+
JOIN subjects ON subjects.id = named.item_id
|
|
64
|
+
UNION
|
|
65
|
+
SELECT choice.item_id, choice.org_id
|
|
66
|
+
FROM capybara.catalog_item_org_switches AS choice
|
|
67
|
+
JOIN subjects ON subjects.id = choice.item_id
|
|
68
|
+
UNION
|
|
69
|
+
SELECT template.catalog_item_id, org.id
|
|
70
|
+
FROM capybara.platform_connector_templates AS template
|
|
71
|
+
JOIN subjects ON subjects.id = template.catalog_item_id
|
|
72
|
+
CROSS JOIN capybara.orgs AS org
|
|
73
|
+
WHERE template.scope_mode = 'own' AND template.scope = 'all'
|
|
74
|
+
AND template.deleted_at IS NULL AND NOT template.disabled
|
|
75
|
+
AND (template.owner_org_id IS NULL OR template.owner_org_id = org.id OR template.shareable)
|
|
76
|
+
AND (${orgId}::text IS NULL OR org.id = ${orgId})
|
|
77
|
+
UNION
|
|
78
|
+
SELECT template.catalog_item_id, named.org_id
|
|
79
|
+
FROM capybara.platform_connector_templates AS template
|
|
80
|
+
JOIN subjects ON subjects.id = template.catalog_item_id
|
|
81
|
+
JOIN capybara.component_scope_orgs AS named
|
|
82
|
+
ON named.kind = 'connector_template' AND named.component_id = template.id
|
|
83
|
+
UNION
|
|
84
|
+
SELECT template.catalog_item_id, choice.org_id
|
|
85
|
+
FROM capybara.platform_connector_templates AS template
|
|
86
|
+
JOIN subjects ON subjects.id = template.catalog_item_id
|
|
87
|
+
JOIN capybara.component_org_switches AS choice
|
|
88
|
+
ON choice.kind = 'connector_template' AND choice.component_id = template.id
|
|
89
|
+
UNION
|
|
90
|
+
SELECT inst.item_id, inst.org_id
|
|
91
|
+
FROM capybara.plugin_installs AS inst
|
|
92
|
+
JOIN subjects ON subjects.id = inst.item_id
|
|
93
|
+
UNION
|
|
94
|
+
SELECT work.item_id, work.org_id
|
|
95
|
+
FROM capybara.plugin_effect_work AS work
|
|
96
|
+
JOIN subjects ON subjects.id = work.item_id
|
|
97
|
+
WHERE work.state = 'pending'
|
|
98
|
+
UNION
|
|
99
|
+
SELECT subjects.id, old.org_id
|
|
100
|
+
FROM subjects CROSS JOIN unnest(${previousOrgs}::text[]) AS old(org_id)
|
|
101
|
+
)
|
|
102
|
+
INSERT INTO capybara.plugin_effect_work (item_id, org_id, generation, state, generation_started_at, updated_at)
|
|
103
|
+
SELECT candidate.item_id, candidate.org_id, 1, 'pending',
|
|
104
|
+
statement_timestamp(), statement_timestamp()
|
|
105
|
+
FROM candidates AS candidate
|
|
106
|
+
JOIN capybara.orgs AS org ON org.id = candidate.org_id AND org.deleted_at IS NULL
|
|
107
|
+
WHERE ${orgId}::text IS NULL OR candidate.org_id = ${orgId}
|
|
108
|
+
ORDER BY candidate.item_id, candidate.org_id
|
|
109
|
+
ON CONFLICT (item_id, org_id) DO UPDATE
|
|
110
|
+
SET generation = plugin_effect_work.generation + 1,
|
|
111
|
+
state = 'pending',
|
|
112
|
+
generation_started_at = EXCLUDED.generation_started_at,
|
|
113
|
+
updated_at = EXCLUDED.updated_at
|
|
114
|
+
-- Multiple writes in the same transaction owe its final state once.
|
|
115
|
+
WHERE plugin_effect_work.xmin <> pg_current_xact_id()::xid
|
|
116
|
+
RETURNING item_id, org_id, generation
|
|
117
|
+
`.execute(db);
|
|
118
|
+
await owePairs(outbox, rows);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/** Re-owes the same generation if a worker did not settle it. */
|
|
122
|
+
export async function recoverEffectWork(outbox, db) {
|
|
123
|
+
// org-filter: deployment-wide stale effect pairs; the job itself enters the pair's org scope.
|
|
124
|
+
await asPlatform("Recover stale plugin effect pairs", async () => {
|
|
125
|
+
const { rows } = await sql `
|
|
126
|
+
WITH stale AS (
|
|
127
|
+
SELECT item_id, org_id FROM capybara.plugin_effect_work
|
|
128
|
+
WHERE state = 'pending' AND updated_at < statement_timestamp() - INTERVAL '2 minutes'
|
|
129
|
+
ORDER BY updated_at, item_id, org_id
|
|
130
|
+
LIMIT 100
|
|
131
|
+
FOR UPDATE SKIP LOCKED
|
|
132
|
+
)
|
|
133
|
+
UPDATE capybara.plugin_effect_work AS work SET updated_at = statement_timestamp()
|
|
134
|
+
FROM stale WHERE work.item_id = stale.item_id AND work.org_id = stale.org_id
|
|
135
|
+
RETURNING work.item_id, work.org_id, work.generation
|
|
136
|
+
`.execute(db);
|
|
137
|
+
await owePairs(outbox, rows);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** Resolve a deployed extension's live catalog item before owing its affected pairs. */
|
|
141
|
+
export async function oweDeployedExtensionSync(outbox, db, extensionName) {
|
|
142
|
+
const item = await db
|
|
143
|
+
.selectFrom("catalog_items")
|
|
144
|
+
.select("id")
|
|
145
|
+
.where("name", "=", extensionName)
|
|
146
|
+
.where("deleted_at", "is", null)
|
|
147
|
+
.where("builtin", "=", false)
|
|
148
|
+
.executeTakeFirst();
|
|
149
|
+
if (item)
|
|
150
|
+
await oweEffectWork(outbox, db, { itemIds: [item.id] });
|
|
151
|
+
}
|
package/dist/catalog/jobs.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { LockService } from "../infra/lock/lock.service.js";
|
|
|
3
3
|
import type { KernelLogger } from "../log.js";
|
|
4
4
|
import { MarketplacesService } from "./marketplaces.service.js";
|
|
5
5
|
import { PluginImport } from "./plugin-import.js";
|
|
6
|
+
import { PluginScopeSync } from "./scope-sync.js";
|
|
6
7
|
import { TemplateConnectorEnsurer } from "./template-connector-ensure.js";
|
|
7
8
|
/**
|
|
8
9
|
* The catalog's periodic jobs, each run once per tick across replicas: the marketplace refresh
|
|
@@ -15,10 +16,11 @@ export declare class CatalogJobs {
|
|
|
15
16
|
private readonly marketplaces;
|
|
16
17
|
private readonly imports;
|
|
17
18
|
private readonly templateConnectors;
|
|
19
|
+
private readonly pluginEffects;
|
|
18
20
|
readonly config: KernelConfigService;
|
|
19
21
|
readonly lockService: LockService;
|
|
20
22
|
readonly log: KernelLogger;
|
|
21
|
-
constructor(marketplaces: MarketplacesService, imports: PluginImport, templateConnectors: TemplateConnectorEnsurer, config: KernelConfigService, lockService: LockService, log: KernelLogger);
|
|
23
|
+
constructor(marketplaces: MarketplacesService, imports: PluginImport, templateConnectors: TemplateConnectorEnsurer, pluginEffects: PluginScopeSync, config: KernelConfigService, lockService: LockService, log: KernelLogger);
|
|
22
24
|
/**
|
|
23
25
|
* Refreshes every enabled marketplace in turn (overall deadline
|
|
24
26
|
* {@link MARKETPLACES_REFRESH_DEADLINE_MS}; the job's signal aborts it: the stop, or the run
|
|
@@ -31,6 +33,7 @@ export declare class CatalogJobs {
|
|
|
31
33
|
* each connector is created in its own (`TemplateConnectorEnsurer`).
|
|
32
34
|
*/
|
|
33
35
|
sweepTemplateConnectors(stop?: AbortSignal): Promise<void>;
|
|
36
|
+
recoverPluginEffects(): Promise<void>;
|
|
34
37
|
/** Deletes expired staging rows and their objects. */
|
|
35
38
|
cleanupStaging(): Promise<void>;
|
|
36
39
|
}
|
package/dist/catalog/jobs.js
CHANGED
|
@@ -16,6 +16,7 @@ import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
|
16
16
|
import { MARKETPLACES_REFRESH_JOB, PLUGIN_STAGING_CLEANUP_JOB, TEMPLATE_CONNECTORS_SWEEP_BATCH, TEMPLATE_CONNECTORS_SWEEP_JOB, TEMPLATE_CONNECTORS_SWEEP_PERIOD_MS, } from "./job-ids.js";
|
|
17
17
|
import { MARKETPLACES_REFRESH_DEADLINE_MS, MarketplacesService } from "./marketplaces.service.js";
|
|
18
18
|
import { PluginImport } from "./plugin-import.js";
|
|
19
|
+
import { PluginScopeSync } from "./scope-sync.js";
|
|
19
20
|
import { TemplateConnectorEnsurer } from "./template-connector-ensure.js";
|
|
20
21
|
/**
|
|
21
22
|
* The catalog's periodic jobs, each run once per tick across replicas: the marketplace refresh
|
|
@@ -28,13 +29,15 @@ let CatalogJobs = class CatalogJobs {
|
|
|
28
29
|
marketplaces;
|
|
29
30
|
imports;
|
|
30
31
|
templateConnectors;
|
|
32
|
+
pluginEffects;
|
|
31
33
|
config;
|
|
32
34
|
lockService;
|
|
33
35
|
log;
|
|
34
|
-
constructor(marketplaces, imports, templateConnectors, config, lockService, log) {
|
|
36
|
+
constructor(marketplaces, imports, templateConnectors, pluginEffects, config, lockService, log) {
|
|
35
37
|
this.marketplaces = marketplaces;
|
|
36
38
|
this.imports = imports;
|
|
37
39
|
this.templateConnectors = templateConnectors;
|
|
40
|
+
this.pluginEffects = pluginEffects;
|
|
38
41
|
this.config = config;
|
|
39
42
|
this.lockService = lockService;
|
|
40
43
|
this.log = log;
|
|
@@ -61,6 +64,9 @@ let CatalogJobs = class CatalogJobs {
|
|
|
61
64
|
if (r.created > 0)
|
|
62
65
|
this.log.info("template connector sweep created connectors", r);
|
|
63
66
|
}
|
|
67
|
+
async recoverPluginEffects() {
|
|
68
|
+
await this.pluginEffects.recover();
|
|
69
|
+
}
|
|
64
70
|
/** Deletes expired staging rows and their objects. */
|
|
65
71
|
async cleanupStaging() {
|
|
66
72
|
const n = await this.imports.cleanupExpired();
|
|
@@ -76,6 +82,9 @@ __decorate([
|
|
|
76
82
|
__decorate([
|
|
77
83
|
LockedInterval(TEMPLATE_CONNECTORS_SWEEP_JOB, () => TEMPLATE_CONNECTORS_SWEEP_PERIOD_MS)
|
|
78
84
|
], CatalogJobs.prototype, "sweepTemplateConnectors", null);
|
|
85
|
+
__decorate([
|
|
86
|
+
LockedInterval("plugin-effect-recovery", () => 10_000)
|
|
87
|
+
], CatalogJobs.prototype, "recoverPluginEffects", null);
|
|
79
88
|
__decorate([
|
|
80
89
|
LockedCron("0 * * * *", { name: PLUGIN_STAGING_CLEANUP_JOB })
|
|
81
90
|
], CatalogJobs.prototype, "cleanupStaging", null);
|
|
@@ -84,8 +93,9 @@ CatalogJobs = __decorate([
|
|
|
84
93
|
__param(0, Inject(MarketplacesService)),
|
|
85
94
|
__param(1, Inject(PluginImport)),
|
|
86
95
|
__param(2, Inject(TemplateConnectorEnsurer)),
|
|
87
|
-
__param(3, Inject(
|
|
88
|
-
__param(4, Inject(
|
|
89
|
-
__param(5, Inject(
|
|
96
|
+
__param(3, Inject(PluginScopeSync)),
|
|
97
|
+
__param(4, Inject(KernelConfigService)),
|
|
98
|
+
__param(5, Inject(LockService)),
|
|
99
|
+
__param(6, Inject(KERNEL_LOGGER))
|
|
90
100
|
], CatalogJobs);
|
|
91
101
|
export { CatalogJobs };
|
|
@@ -9,6 +9,7 @@ import { OrgLock } from "../orgs/org-lock.js";
|
|
|
9
9
|
import { StorageService } from "../storage/storage.service.js";
|
|
10
10
|
import { type CatalogIcon, type OrgCatalogDetail, type OrgCatalogItem, type OrgInstallInput, type OrgInstallResult } from "./catalog-content.js";
|
|
11
11
|
import { PluginInstaller, type UninstallPreview, type UninstallReport } from "./plugin-install.js";
|
|
12
|
+
import { ScopeWriter } from "./scope-writer.js";
|
|
12
13
|
/** The result of a switch: the item, or (switching off without `confirm`) the uninstall preview. */
|
|
13
14
|
export type SwitchResult = {
|
|
14
15
|
kind: "done";
|
|
@@ -36,6 +37,7 @@ export declare class OrgCatalogService {
|
|
|
36
37
|
private readonly storage;
|
|
37
38
|
private readonly registry;
|
|
38
39
|
private readonly pluginInstaller;
|
|
40
|
+
private readonly scopeWriter;
|
|
39
41
|
private readonly log;
|
|
40
42
|
readonly txHost: TransactionHost;
|
|
41
43
|
/** Why a plugin's extension cannot run (the `unavailable` state's reason). */
|
|
@@ -45,7 +47,7 @@ export declare class OrgCatalogService {
|
|
|
45
47
|
private readonly icons;
|
|
46
48
|
constructor(kdb: KernelDb,
|
|
47
49
|
/** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
|
|
48
|
-
orgLock: OrgLock, storage: StorageService, registry: ExtensionRegistry, pluginInstaller: PluginInstaller, log: KernelLogger, txHost: TransactionHost,
|
|
50
|
+
orgLock: OrgLock, storage: StorageService, registry: ExtensionRegistry, pluginInstaller: PluginInstaller, scopeWriter: ScopeWriter, log: KernelLogger, txHost: TransactionHost,
|
|
49
51
|
/** Why a plugin's extension cannot run (the `unavailable` state's reason). */
|
|
50
52
|
availability: ExtensionAvailability,
|
|
51
53
|
/** The effective config of an extension for the org: a wanted plugin whose config is not valid needs input. */
|
|
@@ -117,6 +119,8 @@ export declare class OrgCatalogService {
|
|
|
117
119
|
uninstall(orgId: string, actor: string, installId: string, actorTokenId?: string,
|
|
118
120
|
/** Merged into the audit detail (a write made through the Platform tool). */
|
|
119
121
|
auditDetail?: Record<string, unknown>): Promise<UninstallReport>;
|
|
122
|
+
/** The switch-off impact even when there are no applied org resources. */
|
|
123
|
+
previewContentOff(orgId: string, itemId: string): Promise<UninstallPreview | null>;
|
|
120
124
|
private itemOf;
|
|
121
125
|
/**
|
|
122
126
|
* Writes the org's switch row of the item under the org row lock. The item row is locked FOR
|