@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
@@ -63,6 +63,15 @@ export interface DesiredExtension {
63
63
  * The reviewed extension section of the plugin (the migrations path is taken only from here).
64
64
  */
65
65
  section: PluginExtensionSection;
66
+ marketplaceId?: string | null;
67
+ catalogEvidence?: {
68
+ marketplaceId: string | null;
69
+ npmName: string | null;
70
+ version: string;
71
+ source: string;
72
+ integrity: string | null;
73
+ proof: unknown;
74
+ };
66
75
  }
67
76
  /**
68
77
  * Where a package comes from (F-L): catalog / marketplace / app imports all record `catalog`, with
@@ -327,6 +327,11 @@ let ExtensionInstalls = class ExtensionInstalls {
327
327
  const rows = await this.db
328
328
  .selectFrom("extension_installs")
329
329
  .innerJoin("extension_packages", "extension_packages.sha256", "extension_installs.sha256")
330
+ .leftJoin("catalog_items", "catalog_items.id", "extension_installs.catalog_item_id")
331
+ .leftJoin("catalog_item_versions as revision", (join) => join
332
+ .onRef("revision.item_id", "=", "catalog_items.id")
333
+ .onRef("revision.extension_sha256", "=", "extension_installs.sha256")
334
+ .onRef("revision.version", "=", "extension_installs.version"))
330
335
  .select([
331
336
  "extension_installs.name",
332
337
  "extension_installs.sha256",
@@ -336,6 +341,12 @@ let ExtensionInstalls = class ExtensionInstalls {
336
341
  "extension_installs.migrated_sha",
337
342
  "extension_packages.section",
338
343
  "extension_packages.blob_store",
344
+ "catalog_items.marketplace_id",
345
+ "catalog_items.package_name",
346
+ "revision.version as catalog_version",
347
+ "revision.source as catalog_source",
348
+ "revision.integrity",
349
+ "revision.source_detail",
339
350
  ])
340
351
  .where("extension_installs.desired_state", "=", "enabled")
341
352
  .orderBy(sql `extension_installs.enabled_at ASC NULLS LAST`)
@@ -351,6 +362,18 @@ let ExtensionInstalls = class ExtensionInstalls {
351
362
  migratedSha: r.migrated_sha,
352
363
  blobStore: r.blob_store,
353
364
  section: r.section,
365
+ marketplaceId: r.marketplace_id,
366
+ ...(r.catalog_version !== null &&
367
+ r.catalog_source !== null && {
368
+ catalogEvidence: {
369
+ marketplaceId: r.marketplace_id,
370
+ npmName: r.package_name,
371
+ version: r.catalog_version,
372
+ source: r.catalog_source,
373
+ integrity: r.integrity,
374
+ proof: r.source_detail?.publisherProof,
375
+ },
376
+ }),
354
377
  }));
355
378
  }
356
379
  };
@@ -0,0 +1,10 @@
1
+ import type { PluginExtensionSection } from "@capybara-hq/plugin-sdk";
2
+ import { z } from "zod";
3
+ import type { OfficialNpmRevision } from "../../packages/official-publisher.js";
4
+ /** The proof is a kernel-written record of verification at import or upgrade. */
5
+ export declare function isTrustedPublisherRevision(proof: unknown, expected: OfficialNpmRevision): boolean;
6
+ /** Compile the deliberately bounded, executable-code-free subset used for kernel config gates. */
7
+ export declare function isolatedDeploymentSchema(input: unknown): z.ZodType;
8
+ /** The kernel must never compile executable or unbounded schema features from a child. */
9
+ export declare function assertIsolatedToolSchema(input: unknown): void;
10
+ export declare function assertIsolatedSection(section: PluginExtensionSection): void;
@@ -0,0 +1,206 @@
1
+ import { z } from "zod";
2
+ import { unreachableSecrets } from "../../config/fields.js";
3
+ const publisherProof = z.object({
4
+ checked: z.literal(true),
5
+ policyVersion: z.literal(1),
6
+ repository: z.literal("https://github.com/capybara-hq/plugins"),
7
+ workflow: z.literal(".github/workflows/release.yml"),
8
+ commit: z.string().regex(/^[a-f0-9]{40}$/),
9
+ evidenceDigest: z.string().regex(/^[a-f0-9]{64}$/),
10
+ revision: z.object({
11
+ name: z.string().regex(/^@capybara-hq\/plugin-[a-z0-9][a-z0-9-]*$/),
12
+ version: z.string(),
13
+ integrity: z.string().regex(/^sha512-[A-Za-z0-9+/]{86}==$/),
14
+ }),
15
+ });
16
+ /** The proof is a kernel-written record of verification at import or upgrade. */
17
+ export function isTrustedPublisherRevision(proof, expected) {
18
+ const result = publisherProof.safeParse(proof);
19
+ if (!result.success)
20
+ return false;
21
+ const { revision } = result.data;
22
+ return (revision.name === expected.name &&
23
+ revision.version === expected.version &&
24
+ revision.integrity === expected.integrity);
25
+ }
26
+ const object = (input) => {
27
+ if (typeof input !== "object" || input === null || Array.isArray(input))
28
+ return unsupported();
29
+ return input;
30
+ };
31
+ function unsupported() {
32
+ throw new Error("Unsupported isolated extension config schema");
33
+ }
34
+ const ANNOTATIONS = new Set([
35
+ "type",
36
+ "$schema",
37
+ "title",
38
+ "description",
39
+ "default",
40
+ "secret",
41
+ "platformOnly",
42
+ "x-secret",
43
+ "x-platform-only",
44
+ "x-hot",
45
+ "x-ui",
46
+ ]);
47
+ const KEYWORDS = {
48
+ object: ["properties", "required", "additionalProperties"],
49
+ array: ["items", "minItems", "maxItems"],
50
+ string: ["minLength", "maxLength", "format", "enum"],
51
+ number: ["minimum", "maximum"],
52
+ integer: ["minimum", "maximum"],
53
+ boolean: [],
54
+ null: [],
55
+ };
56
+ function limits(raw, min, max, count) {
57
+ const bounded = count ? z.number().int().min(0).max(1_000_000) : z.number();
58
+ return [raw[min], raw[max]].map((n) => {
59
+ if (n === undefined)
60
+ return n;
61
+ const parsed = bounded.safeParse(n);
62
+ return parsed.success ? parsed.data : unsupported();
63
+ });
64
+ }
65
+ function fields(raw, depth) {
66
+ const entries = Object.entries(raw.properties === undefined ? {} : object(raw.properties));
67
+ if (entries.length > 100)
68
+ return unsupported();
69
+ const required = z
70
+ .array(z.string())
71
+ .max(100)
72
+ .safeParse(raw.required ?? []);
73
+ if (!required.success || required.data.some((name) => !entries.some(([key]) => key === name))) {
74
+ return unsupported();
75
+ }
76
+ const shape = {};
77
+ for (const [name, child] of entries) {
78
+ if (["__proto__", "constructor", "prototype"].includes(name) || name.includes(".")) {
79
+ return unsupported();
80
+ }
81
+ const schema = compile(child, depth + 1);
82
+ shape[name] = required.data.includes(name) ? schema : schema.optional();
83
+ }
84
+ if (raw.additionalProperties === false)
85
+ return z.strictObject(shape);
86
+ if (raw.additionalProperties === undefined || raw.additionalProperties === true)
87
+ return z.object(shape);
88
+ return unsupported();
89
+ }
90
+ function compile(input, depth) {
91
+ if (depth > 12)
92
+ return unsupported();
93
+ const raw = object(input);
94
+ const type = raw.type;
95
+ if (typeof type !== "string" || !Object.hasOwn(KEYWORDS, type))
96
+ return unsupported();
97
+ const accepted = KEYWORDS[type] ?? [];
98
+ if (Object.keys(raw).some((key) => !ANNOTATIONS.has(key) && !accepted.includes(key))) {
99
+ return unsupported();
100
+ }
101
+ let schema;
102
+ switch (type) {
103
+ case "object":
104
+ schema = fields(raw, depth);
105
+ break;
106
+ case "array": {
107
+ const [min, max] = limits(raw, "minItems", "maxItems", true);
108
+ let array = z.array(compile(raw.items, depth + 1));
109
+ if (min !== undefined)
110
+ array = array.min(min);
111
+ if (max !== undefined)
112
+ array = array.max(max);
113
+ schema = array;
114
+ break;
115
+ }
116
+ case "string": {
117
+ if (raw.format !== undefined && raw.format !== "uri")
118
+ return unsupported();
119
+ const [min, max] = limits(raw, "minLength", "maxLength", true);
120
+ let string = z.string();
121
+ if (raw.format === "uri")
122
+ string = string.url();
123
+ if (min !== undefined)
124
+ string = string.min(min);
125
+ if (max !== undefined)
126
+ string = string.max(max);
127
+ const options = z.array(z.string()).min(1).max(100).safeParse(raw.enum);
128
+ if (raw.enum !== undefined && !options.success)
129
+ return unsupported();
130
+ schema = options.success ? string.pipe(z.enum(options.data)) : string;
131
+ break;
132
+ }
133
+ case "number":
134
+ case "integer": {
135
+ const [min, max] = limits(raw, "minimum", "maximum", false);
136
+ let number = type === "integer" ? z.number().int() : z.number();
137
+ if (min !== undefined)
138
+ number = number.min(min);
139
+ if (max !== undefined)
140
+ number = number.max(max);
141
+ schema = number;
142
+ break;
143
+ }
144
+ case "boolean":
145
+ schema = z.boolean();
146
+ break;
147
+ case "null":
148
+ schema = z.null();
149
+ break;
150
+ default:
151
+ return unsupported();
152
+ }
153
+ if (Object.hasOwn(raw, "default") && raw["x-secret"] !== true && raw.secret !== true) {
154
+ const baseline = schema.safeParse(raw.default);
155
+ if (!baseline.success)
156
+ return unsupported();
157
+ schema = schema.prefault(baseline.data);
158
+ }
159
+ return schema.meta({
160
+ ...(typeof raw.description === "string" && { description: raw.description }),
161
+ ...((raw["x-secret"] === true || raw.secret === true) && { secret: true }),
162
+ ...((raw["x-platform-only"] === true || raw.platformOnly === true) && { platformOnly: true }),
163
+ ...(typeof raw["x-ui"] === "object" && raw["x-ui"] !== null && { ui: raw["x-ui"] }),
164
+ });
165
+ }
166
+ /** Compile the deliberately bounded, executable-code-free subset used for kernel config gates. */
167
+ export function isolatedDeploymentSchema(input) {
168
+ if (object(input).type !== "object")
169
+ return unsupported();
170
+ const schema = compile(input, 0);
171
+ if (unreachableSecrets(schema).length > 0)
172
+ return unsupported();
173
+ return schema;
174
+ }
175
+ /** The kernel must never compile executable or unbounded schema features from a child. */
176
+ export function assertIsolatedToolSchema(input) {
177
+ try {
178
+ isolatedDeploymentSchema(input);
179
+ }
180
+ catch {
181
+ throw new Error("Unsupported isolated extension tool schema");
182
+ }
183
+ }
184
+ export function assertIsolatedSection(section) {
185
+ const { manifest, client } = section;
186
+ const permissions = manifest.permissions;
187
+ if (client !== undefined ||
188
+ permissions?.client ||
189
+ permissions?.secrets ||
190
+ manifest.provides.some((point) => point !== "trigger-source@1") ||
191
+ (manifest.requires ?? []).some((point) => point !== "trigger-source@1") ||
192
+ (manifest.optional ?? []).some((point) => point !== "trigger-source@1") ||
193
+ manifest.ui?.some((entry) => entry.kind !== "view") ||
194
+ permissions?.http ||
195
+ permissions?.dialIn ||
196
+ permissions?.deviceAccess ||
197
+ permissions?.policyHooks?.length) {
198
+ throw new Error("This extension declares a capability that requires trusted execution");
199
+ }
200
+ if (section.configSchemas?.deployment !== undefined) {
201
+ isolatedDeploymentSchema(section.configSchemas.deployment);
202
+ }
203
+ else if (manifest.config.deployment.length) {
204
+ throw new Error("Isolated extensions must include a declarative deployment config schema");
205
+ }
206
+ }
@@ -0,0 +1,45 @@
1
+ import type { KernelSchemaDb } from "../../db/kernel-schema.js";
2
+ import type { PermissionSet } from "../permission-set.js";
3
+ /** All broker grants are checked against one database snapshot, under the invocation's org scope. */
4
+ export declare function readIsolatedGrant(db: KernelSchemaDb, grant: {
5
+ plugin: string;
6
+ installedSha: string | null | undefined;
7
+ permissions: PermissionSet;
8
+ orgId: string | undefined;
9
+ }): Promise<boolean>;
10
+ export interface IsolatedInvocationGrant {
11
+ signal: AbortSignal;
12
+ release(): void;
13
+ }
14
+ export interface IsolatedRevocation {
15
+ plugin?: string;
16
+ orgId?: string;
17
+ }
18
+ interface CachedGrant {
19
+ abort: AbortController;
20
+ check: () => Promise<boolean>;
21
+ onRevoked: () => void;
22
+ pending?: Promise<boolean>;
23
+ checkedUntil: number;
24
+ calls: number;
25
+ }
26
+ /** Replica-local cache of authority checked against deployment and org state. */
27
+ export declare class IsolatedInvocationGrants {
28
+ private readonly instances;
29
+ private readonly rechecks;
30
+ private revalidating;
31
+ issue({ plugin, orgId }: IsolatedRevocation, check: CachedGrant["check"], onRevoked: CachedGrant["onRevoked"] | undefined, instance: AbortSignal): Promise<IsolatedInvocationGrant | undefined>;
32
+ revoke({ plugin, orgId }: IsolatedRevocation): void;
33
+ /**
34
+ * A committed catalog change can alter eligibility through dependency edges. Fence the old
35
+ * snapshot for every affected org grant, then close only the lanes that lose eligibility.
36
+ */
37
+ refreshOrgIntent(orgId?: string): Promise<void>;
38
+ /** Recheck retained grants, including idle lanes, when periodic reconciliation fires. */
39
+ revalidate(): Promise<void>;
40
+ private retained;
41
+ private runRechecks;
42
+ private revalidateRetained;
43
+ private validate;
44
+ }
45
+ export {};
@@ -0,0 +1,191 @@
1
+ import { snapshotCovers } from "../approvals.js";
2
+ import { enabledExtensionsQuery } from "../enabled.js";
3
+ /** All broker grants are checked against one database snapshot, under the invocation's org scope. */
4
+ export async function readIsolatedGrant(db, grant) {
5
+ const { plugin, installedSha, permissions, orgId } = grant;
6
+ if (installedSha === null)
7
+ return false;
8
+ let query = db
9
+ .selectFrom("extension_permission_snapshots")
10
+ .select("permissions")
11
+ .where("plugin", "=", plugin);
12
+ if (installedSha !== undefined) {
13
+ query = query.where(({ exists, selectFrom }) => exists(selectFrom("extension_installs")
14
+ .select("name")
15
+ .where("name", "=", plugin)
16
+ .where("sha256", "=", installedSha)
17
+ .where("desired_state", "=", "enabled")));
18
+ }
19
+ if (orgId !== undefined) {
20
+ const enabled = enabledExtensionsQuery(db, orgId, plugin).as("enabled");
21
+ query = query.where(({ exists, selectFrom }) => exists(selectFrom("orgs")
22
+ .innerJoin(enabled, (join) => join.on("enabled.plugin", "=", plugin))
23
+ .select("orgs.id")
24
+ .where("orgs.id", "=", orgId)
25
+ .where("orgs.deleted_at", "is", null)));
26
+ }
27
+ const snapshot = await query.executeTakeFirst();
28
+ return snapshotCovers(snapshot, permissions);
29
+ }
30
+ const MAX_GRANTS_PER_INSTANCE = 128;
31
+ const GRANT_TTL_MS = 60_000;
32
+ const REVALIDATION_BATCH = 8;
33
+ /** Replica-local cache of authority checked against deployment and org state. */
34
+ export class IsolatedInvocationGrants {
35
+ instances = new Map();
36
+ rechecks = new Map();
37
+ revalidating;
38
+ async issue({ plugin, orgId }, check, onRevoked, instance) {
39
+ if (instance.aborted)
40
+ return undefined;
41
+ let bucket = this.instances.get(instance);
42
+ if (!bucket) {
43
+ bucket = { plugin, orgs: new Map() };
44
+ const { orgs } = bucket;
45
+ this.instances.set(instance, bucket);
46
+ instance.addEventListener("abort", () => {
47
+ this.instances.delete(instance);
48
+ for (const grant of orgs.values())
49
+ grant.abort.abort();
50
+ orgs.clear();
51
+ }, { once: true });
52
+ }
53
+ const { orgs } = bucket;
54
+ let entry = orgs.get(orgId);
55
+ if (entry) {
56
+ // Move it to the back so an idle least recently used grant is the first eviction candidate.
57
+ orgs.delete(orgId);
58
+ }
59
+ else {
60
+ if (orgs.size >= MAX_GRANTS_PER_INSTANCE) {
61
+ for (const [key, idle] of orgs) {
62
+ if (idle.calls !== 0 || idle.pending)
63
+ continue;
64
+ orgs.delete(key);
65
+ break;
66
+ }
67
+ if (orgs.size >= MAX_GRANTS_PER_INSTANCE)
68
+ return undefined;
69
+ }
70
+ entry = {
71
+ abort: new AbortController(),
72
+ check,
73
+ onRevoked: onRevoked ?? (() => { }),
74
+ checkedUntil: 0,
75
+ calls: 0,
76
+ };
77
+ }
78
+ orgs.set(orgId, entry);
79
+ while (Date.now() >= entry.checkedUntil) {
80
+ if (!(await this.validate(orgs, orgId, entry)))
81
+ return undefined;
82
+ }
83
+ if (entry.abort.signal.aborted)
84
+ return undefined;
85
+ const call = new AbortController();
86
+ entry.calls += 1;
87
+ return {
88
+ signal: AbortSignal.any([entry.abort.signal, call.signal]),
89
+ release: () => {
90
+ if (call.signal.aborted)
91
+ return;
92
+ entry.calls -= 1;
93
+ call.abort();
94
+ },
95
+ };
96
+ }
97
+ revoke({ plugin, orgId }) {
98
+ for (const bucket of this.instances.values()) {
99
+ if (plugin !== undefined && plugin !== bucket.plugin)
100
+ continue;
101
+ for (const [key, entry] of bucket.orgs) {
102
+ if (orgId !== undefined && orgId !== key)
103
+ continue;
104
+ bucket.orgs.delete(key);
105
+ entry.abort.abort();
106
+ }
107
+ }
108
+ }
109
+ /**
110
+ * A committed catalog change can alter eligibility through dependency edges. Fence the old
111
+ * snapshot for every affected org grant, then close only the lanes that lose eligibility.
112
+ */
113
+ refreshOrgIntent(orgId) {
114
+ for (const grant of this.retained()) {
115
+ if (grant.orgId === undefined || (orgId !== undefined && orgId !== grant.orgId))
116
+ continue;
117
+ grant.entry.checkedUntil = 0;
118
+ delete grant.entry.pending;
119
+ this.rechecks.set(grant.entry, grant);
120
+ }
121
+ return this.runRechecks();
122
+ }
123
+ /** Recheck retained grants, including idle lanes, when periodic reconciliation fires. */
124
+ revalidate() {
125
+ for (const grant of this.retained()) {
126
+ if (grant.entry.checkedUntil > 0)
127
+ this.rechecks.set(grant.entry, grant);
128
+ }
129
+ return this.runRechecks();
130
+ }
131
+ *retained() {
132
+ for (const { orgs } of this.instances.values()) {
133
+ for (const [orgId, entry] of orgs)
134
+ yield { orgs, orgId, entry };
135
+ }
136
+ }
137
+ runRechecks() {
138
+ this.revalidating ??= this.revalidateRetained().finally(() => {
139
+ this.revalidating = undefined;
140
+ if (this.rechecks.size > 0)
141
+ return this.runRechecks();
142
+ });
143
+ return this.revalidating;
144
+ }
145
+ async revalidateRetained() {
146
+ while (this.rechecks.size > 0) {
147
+ const batch = [];
148
+ for (const { orgs, orgId, entry } of this.rechecks.values()) {
149
+ this.rechecks.delete(entry);
150
+ if (orgs.get(orgId) !== entry)
151
+ continue;
152
+ batch.push(this.validate(orgs, orgId, entry));
153
+ if (batch.length === REVALIDATION_BATCH)
154
+ break;
155
+ }
156
+ await Promise.all(batch);
157
+ }
158
+ }
159
+ validate(orgs, orgId, entry) {
160
+ if (entry.abort.signal.aborted || orgs.get(orgId) !== entry)
161
+ return Promise.resolve(false);
162
+ if (entry.pending)
163
+ return entry.pending;
164
+ this.rechecks.delete(entry);
165
+ const checkedUntil = Date.now() + GRANT_TTL_MS;
166
+ const pending = Promise.resolve()
167
+ .then(entry.check)
168
+ .catch(() => false)
169
+ .then((allowed) => {
170
+ if (entry.pending !== pending) {
171
+ // A catalog hint superseded this query. Share the next decision or reuse it if complete.
172
+ if (entry.abort.signal.aborted || orgs.get(orgId) !== entry)
173
+ return false;
174
+ return entry.checkedUntil > Date.now() || this.validate(orgs, orgId, entry);
175
+ }
176
+ delete entry.pending;
177
+ if (entry.abort.signal.aborted)
178
+ return false;
179
+ if (allowed) {
180
+ entry.checkedUntil = checkedUntil;
181
+ return true;
182
+ }
183
+ orgs.delete(orgId);
184
+ entry.abort.abort();
185
+ entry.onRevoked();
186
+ return false;
187
+ });
188
+ entry.pending = pending;
189
+ return pending;
190
+ }
191
+ }
@@ -0,0 +1,15 @@
1
+ import type { KernelExtensionContext } from "../../ctx/create-ctx.js";
2
+ import { type ExtensionEffects } from "../../ctx/durable-effects.service.js";
3
+ import type { ExtensionEffectKeys } from "../../ctx/durable-keys.js";
4
+ import type { BrokerCall } from "./lanes.js";
5
+ export interface BrokerOptions {
6
+ ctx: KernelExtensionContext;
7
+ plugin: string;
8
+ effects: () => ExtensionEffects;
9
+ keys: () => ExtensionEffectKeys;
10
+ current: () => boolean;
11
+ network: (url: URL, orgId?: string) => Promise<boolean>;
12
+ redactLog?: (message: string, config: unknown) => string;
13
+ }
14
+ /** The only route from an isolated process to kernel services. Authority comes from the live call. */
15
+ export declare function createIsolatedBroker(options: BrokerOptions): BrokerCall;