@capybara-hq/kernel 0.1.0-rc.124 → 0.1.0-rc.126

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 (40) hide show
  1. package/dist/agents/onboarding-checklist.d.ts +1 -0
  2. package/dist/agents/onboarding-checklist.js +14 -1
  3. package/dist/catalog/catalog.module.js +0 -3
  4. package/dist/catalog/org-catalog.service.d.ts +0 -2
  5. package/dist/catalog/org-catalog.service.js +0 -4
  6. package/dist/catalog/org-extension.controller.d.ts +5 -9
  7. package/dist/catalog/org-extension.controller.js +22 -22
  8. package/dist/catalog/org-extension.schemas.d.ts +0 -1
  9. package/dist/catalog/org-extension.schemas.js +0 -2
  10. package/dist/catalog/plugin-install.d.ts +4 -1
  11. package/dist/catalog/plugin-install.js +4 -1
  12. package/dist/conversations/events.d.ts +10 -154
  13. package/dist/conversations/events.js +8 -83
  14. package/dist/devices/conversation-devices.d.ts +1 -2
  15. package/dist/devices/devices.service.d.ts +8 -1
  16. package/dist/devices/devices.service.js +1 -1
  17. package/dist/errors.d.ts +5 -0
  18. package/dist/errors.js +5 -0
  19. package/dist/evals/recorded-permission-context.js +1 -3
  20. package/dist/evals/recorded-tools.d.ts +3 -4
  21. package/dist/evals/recorded-tools.js +6 -7
  22. package/dist/extensions/org-config.d.ts +35 -8
  23. package/dist/extensions/org-config.js +61 -14
  24. package/dist/extensions/plugin-items.d.ts +16 -0
  25. package/dist/extensions/plugin-items.js +35 -0
  26. package/dist/platform/operation.d.ts +0 -2
  27. package/dist/platform/ops/extensions.ops.d.ts +4 -6
  28. package/dist/platform/ops/extensions.ops.js +17 -34
  29. package/dist/runtime/context/build.d.ts +1 -1
  30. package/dist/runtime/context/build.js +1 -10
  31. package/dist/runtime/context/device-paths.d.ts +5 -82
  32. package/dist/runtime/context/device-paths.js +3 -174
  33. package/dist/runtime/context/permission-profile.d.ts +3 -13
  34. package/dist/runtime/context/permission-profile.js +9 -26
  35. package/dist/runtime/permission-context.service.js +3 -4
  36. package/dist/runtime/run-worker.js +6 -5
  37. package/package.json +1 -1
  38. package/src/migrations/0210_normalize_conversation_history.sql +1012 -0
  39. package/dist/catalog/org-extension-switch.d.ts +0 -41
  40. package/dist/catalog/org-extension-switch.js +0 -123
@@ -29,7 +29,7 @@ import { DeviceReports } from "../sandbox/device-reports.js";
29
29
  import { deviceSideLayer, hostGrantable } from "../sandbox/device-sandbox.js";
30
30
  import { oweDeviceDestroy, SandboxJobs } from "../sandbox/jobs.js";
31
31
  import { revokeLoopbackGrantsOfDevice } from "../sandbox/loopback-grant-records.js";
32
- import { coversHost } from "../sandbox/modes.js";
32
+ import { coversHost, } from "../sandbox/modes.js";
33
33
  import { assertAuthorityWriteHeld } from "../sandbox/permission-authority-lock.js";
34
34
  import { hostsNarrowed, needsOpenNetwork, PermissionCeilingWrites, } from "../sandbox/permission-ceiling-writes.js";
35
35
  import { ownNetwork, PermissionFacts } from "../sandbox/permission-facts.js";
package/dist/errors.d.ts CHANGED
@@ -141,6 +141,11 @@ export declare const ERROR_CODES: {
141
141
  readonly "extension.package_too_large": 413;
142
142
  readonly "extension.config_declared": 409;
143
143
  readonly "extension.required": 409;
144
+ /**
145
+ * Switching on an extension whose plugin is not installed in the org (details
146
+ * `[{ itemId, plugin }]`): the plugin's switch on the Plugins page installs it first.
147
+ */
148
+ readonly "extension.plugin_not_installed": 409;
144
149
  readonly "extension.name_conflict": 409;
145
150
  readonly "extension.approval_stale": 409;
146
151
  readonly "extension.not_pending": 409;
package/dist/errors.js CHANGED
@@ -141,6 +141,11 @@ export const ERROR_CODES = {
141
141
  "extension.package_too_large": 413,
142
142
  "extension.config_declared": 409,
143
143
  "extension.required": 409,
144
+ /**
145
+ * Switching on an extension whose plugin is not installed in the org (details
146
+ * `[{ itemId, plugin }]`): the plugin's switch on the Plugins page installs it first.
147
+ */
148
+ "extension.plugin_not_installed": 409,
144
149
  "extension.name_conflict": 409,
145
150
  "extension.approval_stale": 409,
146
151
  "extension.not_pending": 409,
@@ -6,9 +6,7 @@ const invalid = (message) => new ApiError("eval.snapshot_invalid", message);
6
6
  /** Whether the recording holds any v1 permission marker (event, managed path or metadata). */
7
7
  export function hasPermissionMarkers(events) {
8
8
  return events.some((e) => e.type === "permission_context" ||
9
- (e.type === "device_paths" &&
10
- e.payload.permissionContextVersion !==
11
- undefined) ||
9
+ e.type === "device_paths" ||
12
10
  (e.type === "model_call" &&
13
11
  e.payload.request.permissionContext !== undefined));
14
12
  }
@@ -24,13 +24,12 @@ export interface RecordedCall {
24
24
  /** 原 `tool_call_started` 的定义指纹(没有开始事件的调用为空串)。 */
25
25
  specHash: string;
26
26
  /**
27
- * The payload (roots, and the sandbox level when recorded) of the latest `device_paths` event
27
+ * The payload of the latest `device_paths` event
28
28
  * the original run had logged by this call's step (environments spec §13.1, ruling R22): set on
29
29
  * every recorded call from the step where an event was logged onward. A replay that matches the
30
30
  * call writes the event when the eval conversation's latest one differs, so a candidate whose
31
- * first sandbox call lands in another step still gets the line, once. A managed v1 payload
32
- * (`permissionContextVersion: 1`) is kept whole (permission context replay spec B4); older ones
33
- * keep their roots-and-level shape and change semantics.
31
+ * first sandbox call lands in another step still gets the line, once. The managed payload is
32
+ * copied whole (permission context replay spec B4).
34
33
  */
35
34
  devicePaths?: DevicePathsPayload;
36
35
  /**
@@ -1,6 +1,6 @@
1
- import { ReviewRecordSchema } from "../conversations/events.js";
1
+ import { ManagedDevicePathsV1Schema, ReviewRecordSchema, } from "../conversations/events.js";
2
2
  import { ApiError } from "../errors.js";
3
- import { devicePathsChangeFromPayload, devicePathsLevel, } from "../runtime/context/device-paths.js";
3
+ import { devicePathsChangeFromPayload, } from "../runtime/context/device-paths.js";
4
4
  import { assistantIdemKey } from "../runtime/model-call.js";
5
5
  import { MATCH_IGNORED_FIELDS } from "../tools/builtin-names.js";
6
6
  import { RUN_STOPPED_TEXT } from "../tools/pipeline.js";
@@ -58,11 +58,10 @@ export function recordedCalls(s) {
58
58
  for (; next < own.length && own[next].seq < end; next++) {
59
59
  const e = own[next];
60
60
  if (e.type === "device_paths" && e.seq > a) {
61
- const p = e.payload;
62
- current =
63
- p.permissionContextVersion === 1
64
- ? structuredClone(p)
65
- : { roots: p.roots, ...devicePathsLevel(p) };
61
+ const parsed = ManagedDevicePathsV1Schema.safeParse(e.payload);
62
+ if (!parsed.success)
63
+ throw new ApiError("eval.snapshot_invalid", `Invalid recorded device_paths at seq ${e.seq}`);
64
+ current = parsed.data;
66
65
  }
67
66
  }
68
67
  if (current !== undefined)
@@ -1,5 +1,6 @@
1
1
  import type { PluginScope } from "../catalog/scope.js";
2
2
  import type { KernelSchemaDb } from "../db/kernel-schema.js";
3
+ import { ApiError } from "../errors.js";
3
4
  import { KernelDb } from "../infra/database/kernel-db.js";
4
5
  import { TransactionHost } from "../infra/database/transaction-host.js";
5
6
  import { KernelEvents } from "../infra/events/kernel-events.js";
@@ -52,8 +53,17 @@ export interface OrgExtensionView {
52
53
  itemId: string;
53
54
  scope: PluginScope;
54
55
  } | null;
55
- /** The org admin can switch it (the plugin's switch): never for an `all` / builtin plugin. */
56
+ /** The org admin can switch it: never for a required one or a builtin plugin's. */
56
57
  switchable: boolean;
58
+ /**
59
+ * Whether the plugin that brings the extension is installed in the org: `installed` /
60
+ * `not_installed` for an extension with a plugin path (one catalog plugin, not builtin, not
61
+ * required), null without one (required, config-declared, several sources, builtin). The org's
62
+ * Extensions and Models pages list an extension unless it is `not_installed` and off
63
+ * ({@link listedInOrg}); its switch starts or stops the extension only, and switching on needs
64
+ * the plugin installed (`extension.plugin_not_installed`; extension switch spec 2026-12-02).
65
+ */
66
+ pluginInstall: "installed" | "not_installed" | null;
57
67
  /**
58
68
  * The two-level deployment config (spec A §5.3, §5.4): `following` the platform, a `copy` of the
59
69
  * overridable fields, or `none` (the extension has no overridable field, or is unavailable).
@@ -65,6 +75,13 @@ export interface OrgExtensionView {
65
75
  */
66
76
  needsConfig: boolean;
67
77
  }
78
+ /**
79
+ * Whether the org's lists of its extensions (the Extensions page, the Models page's model services,
80
+ * `extensions.list`) show the extension: everything except one whose plugin is not installed in
81
+ * the org and that is off (the Plugins page installs it; one left on shows, so it can be stopped).
82
+ * The web applies the same rule (`apps/web/src/lib/extensions.ts`).
83
+ */
84
+ export declare function listedInOrg(view: Pick<OrgExtensionView, "pluginInstall" | "enabled">): boolean;
68
85
  /** The extension points an extension provides, from its permission list's `provides` group. */
69
86
  export declare function providesOf(view: Pick<OrgExtensionView, "permissions">): string[];
70
87
  /** The input of `put`: enablement only. */
@@ -161,18 +178,19 @@ export declare class OrgExtensionConfigs {
161
178
  */
162
179
  private listSnapshot;
163
180
  /**
164
- * How an `enabled` write of this extension is handled: `direct` through {@link put} (a required
165
- * bootstrap plugin keeps `extension.required`; an extension without a live item is config-declared;
166
- * several live items keep the direct write, the strictest source rules applying), `locked` (a live
167
- * item is builtin and the write switches off: `plugin.scope_locked`), or `item` (the single plugin
168
- * whose org switch it is).
181
+ * How an `enabled` write of this extension in the org is handled, read before the write (the
182
+ * Platform tool's precheck; {@link put} repeats every rule under the org row lock): `direct`, a
183
+ * write of the extension's own switch (a required bootstrap plugin keeps `extension.required`;
184
+ * the plugin stays installed either way, extension switch spec 2026-12-02 §3.1), `locked` (a live
185
+ * item is builtin and the write switches off: `plugin.scope_locked`), or `not_installed` (switching
186
+ * on an extension whose plugin is not installed in the org: `extension.plugin_not_installed`).
169
187
  */
170
- switchPlan(plugin: string, enabled: boolean): Promise<{
188
+ switchPlan(orgId: string, plugin: string, enabled: boolean): Promise<{
171
189
  kind: "direct";
172
190
  } | {
173
191
  kind: "locked";
174
192
  } | {
175
- kind: "item";
193
+ kind: "not_installed";
176
194
  itemId: string;
177
195
  }>;
178
196
  /**
@@ -180,6 +198,10 @@ export declare class OrgExtensionConfigs {
180
198
  * `org.extensions.manage`).
181
199
  * - Extension not loaded -> `extension.not_found`; `enabled: false` for a required bootstrap
182
200
  * plugin -> `extension.required` (it stays enabled in every org).
201
+ * - Switches the extension only, never its plugin (extension switch spec 2026-12-02 §3.1): with a
202
+ * plugin path ({@link pluginPathItem}) switching on needs the plugin installed in the org
203
+ * (`extension.plugin_not_installed`; the install itself passes `viaItem`), and switching off an
204
+ * extension that is off already while its plugin is not installed writes nothing.
183
205
  * - Enabling never checks a config: an extension whose effective config is not valid for the org
184
206
  * is enabled and reported `needsConfig` (spec A §6: it gets no org-bound call until then).
185
207
  */
@@ -234,3 +256,8 @@ export declare class OrgExtensionConfigs {
234
256
  * `db` may be a transaction.
235
257
  */
236
258
  export declare function extensionVisibleTo(db: KernelSchemaDb, orgId: string, plugin: string): Promise<boolean>;
259
+ /**
260
+ * `extension.plugin_not_installed`: switching on an extension whose plugin is not installed in the
261
+ * org. Names the plugin (details `[{ itemId, plugin }]`). `db` may be a transaction.
262
+ */
263
+ export declare function pluginNotInstalledError(db: KernelSchemaDb, extension: string, itemId: string): Promise<ApiError>;
@@ -27,8 +27,17 @@ import { ExtensionHost } from "./host.js";
27
27
  import { ExtensionInstalls } from "./installs.js";
28
28
  import { fieldLevels } from "./org-levels.js";
29
29
  import { permissionGroups, } from "./permission-view.js";
30
- import { itemLocked, itemsVisible, offeredToOrg, pluginItemsOf, scopeListedFor, } from "./plugin-items.js";
30
+ import { itemLocked, itemsVisible, liveInstalledItems, offeredToOrg, pluginInstallOf, pluginItemsOf, pluginPathItem, scopeListedFor, } from "./plugin-items.js";
31
31
  import { ExtensionAvailability } from "./unavailable.js";
32
+ /**
33
+ * Whether the org's lists of its extensions (the Extensions page, the Models page's model services,
34
+ * `extensions.list`) show the extension: everything except one whose plugin is not installed in
35
+ * the org and that is off (the Plugins page installs it; one left on shows, so it can be stopped).
36
+ * The web applies the same rule (`apps/web/src/lib/extensions.ts`).
37
+ */
38
+ export function listedInOrg(view) {
39
+ return view.pluginInstall !== "not_installed" || view.enabled;
40
+ }
32
41
  /** The extension points an extension provides, from its permission list's `provides` group. */
33
42
  export function providesOf(view) {
34
43
  return view.permissions.filter((g) => g.category === "provides").flatMap((g) => g.items);
@@ -165,6 +174,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
165
174
  // An extension the org is not offered (a `named` plugin without it, or one off here whose
166
175
  // item the super admin disabled) is absent: the rule readiness shares
167
176
  const listed = await scopeListedFor(t, orgId);
177
+ const live = await liveInstalledItems(t, orgId);
168
178
  const seen = (name) => offeredToOrg({
169
179
  required: required.has(name),
170
180
  enabled: on.has(name) || (rows.find((r) => r.plugin === name)?.enabled ?? false),
@@ -173,7 +183,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
173
183
  });
174
184
  const views = loaded
175
185
  .filter((m) => seen(m.name))
176
- .map((m) => this.view(m, rows.find((r) => r.plugin === m.name), required.has(m.name), items.get(m.name), on.has(m.name)));
186
+ .map((m) => this.view(m, rows.find((r) => r.plugin === m.name), required.has(m.name), items.get(m.name), on.has(m.name), live));
177
187
  const names = new Set(loaded.map((m) => m.name));
178
188
  const unavailable = installed
179
189
  .filter((i) => !names.has(i.name) && seen(i.name))
@@ -190,35 +200,46 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
190
200
  reason: "loading",
191
201
  required: required.has(i.name),
192
202
  ...scopeFields(items.get(i.name), required.has(i.name)),
203
+ pluginInstall: pluginInstallOf(items.get(i.name), required.has(i.name), live),
193
204
  overrideState: "none",
194
205
  needsConfig: false,
195
206
  }));
196
207
  return [...views, ...unavailable];
197
208
  }
198
209
  /**
199
- * How an `enabled` write of this extension is handled: `direct` through {@link put} (a required
200
- * bootstrap plugin keeps `extension.required`; an extension without a live item is config-declared;
201
- * several live items keep the direct write, the strictest source rules applying), `locked` (a live
202
- * item is builtin and the write switches off: `plugin.scope_locked`), or `item` (the single plugin
203
- * whose org switch it is).
210
+ * How an `enabled` write of this extension in the org is handled, read before the write (the
211
+ * Platform tool's precheck; {@link put} repeats every rule under the org row lock): `direct`, a
212
+ * write of the extension's own switch (a required bootstrap plugin keeps `extension.required`;
213
+ * the plugin stays installed either way, extension switch spec 2026-12-02 §3.1), `locked` (a live
214
+ * item is builtin and the write switches off: `plugin.scope_locked`), or `not_installed` (switching
215
+ * on an extension whose plugin is not installed in the org: `extension.plugin_not_installed`).
204
216
  */
205
- async switchPlan(plugin, enabled) {
217
+ async switchPlan(orgId, plugin, enabled) {
206
218
  const t = this.db;
207
- if ((await requiredExtensions(t)).has(plugin))
219
+ const required = (await requiredExtensions(t)).has(plugin);
220
+ if (required)
208
221
  return { kind: "direct" };
209
222
  const items = (await pluginItemsOf(t, [plugin])).get(plugin);
210
223
  if (!items)
211
224
  return { kind: "direct" };
212
225
  if (!enabled && items.some(itemLocked))
213
226
  return { kind: "locked" };
214
- const [only] = items;
215
- return items.length === 1 && only ? { kind: "item", itemId: only.id } : { kind: "direct" };
227
+ const itemId = pluginPathItem(items, required);
228
+ if (!enabled || itemId === null)
229
+ return { kind: "direct" };
230
+ return (await liveInstalledItems(t, orgId)).has(itemId)
231
+ ? { kind: "direct" }
232
+ : { kind: "not_installed", itemId };
216
233
  }
217
234
  /**
218
235
  * Writes enablement. The actor's role is re-read under the org row lock (needs
219
236
  * `org.extensions.manage`).
220
237
  * - Extension not loaded -> `extension.not_found`; `enabled: false` for a required bootstrap
221
238
  * plugin -> `extension.required` (it stays enabled in every org).
239
+ * - Switches the extension only, never its plugin (extension switch spec 2026-12-02 §3.1): with a
240
+ * plugin path ({@link pluginPathItem}) switching on needs the plugin installed in the org
241
+ * (`extension.plugin_not_installed`; the install itself passes `viaItem`), and switching off an
242
+ * extension that is off already while its plugin is not installed writes nothing.
222
243
  * - Enabling never checks a config: an extension whose effective config is not valid for the org
223
244
  * is enabled and reported `needsConfig` (spec A §6: it gets no org-bound call until then).
224
245
  */
@@ -246,9 +267,19 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
246
267
  throw requiredError(plugin);
247
268
  if (on && input.enabled === false)
248
269
  throw scopeLockedError();
270
+ // The org's live installs matter only to an extension with a plugin path
271
+ const pathItem = pluginPathItem(item, required);
272
+ const live = pathItem === null ? new Set() : await liveInstalledItems(t, orgId);
249
273
  if (opts.ifDisabled && (row?.enabled === true || on)) {
250
274
  opts.ifDisabled.skipped = true;
251
- return this.view(manifest, row, required, item, on);
275
+ return this.view(manifest, row, required, item, on, live);
276
+ }
277
+ // Not the install itself (`viaItem`): a plugin path needs the plugin installed here
278
+ if (opts.viaItem === undefined && !on && pathItem !== null && !live.has(pathItem)) {
279
+ if (input.enabled === true)
280
+ throw await pluginNotInstalledError(t, plugin, pathItem);
281
+ if (row?.enabled !== true)
282
+ return this.view(manifest, row, required, item, on, live);
252
283
  }
253
284
  // An always-on extension is enabled whatever the row said (it may predate `required` / builtin)
254
285
  const enabled = on || (input.enabled ?? row?.enabled ?? false);
@@ -282,7 +313,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
282
313
  ...opts.auditDetail,
283
314
  },
284
315
  });
285
- const view = this.view(manifest, { enabled }, required, item, on);
316
+ const view = this.view(manifest, { enabled }, required, item, on, live);
286
317
  // Only once the root transaction committed (an outer caller's too): a rolled-back write
287
318
  // disconnects nothing
288
319
  afterCommit(async () => {
@@ -463,7 +494,9 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
463
494
  }
464
495
  view(m, row, required, items,
465
496
  /** Always on in every org (enabled.ts): required, or its plugin is builtin. */
466
- alwaysOn) {
497
+ alwaysOn,
498
+ /** The items with a live install in the org ({@link liveInstalledItems}). */
499
+ liveItems) {
467
500
  const configurable = [...fieldLevels(m.config?.deployment).values()].includes("override");
468
501
  return {
469
502
  plugin: m.name,
@@ -475,6 +508,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
475
508
  status: "enabled",
476
509
  required,
477
510
  ...scopeFields(items, required),
511
+ pluginInstall: pluginInstallOf(items, required, liveItems),
478
512
  // filled in by `withLayers` (the copy rows and the effective config)
479
513
  overrideState: configurable ? "following" : "none",
480
514
  needsConfig: false,
@@ -530,4 +564,17 @@ export async function extensionVisibleTo(db, orgId, plugin) {
530
564
  return itemsVisible(items, listed);
531
565
  }
532
566
  const scopeLockedError = () => new ApiError("plugin.scope_locked", "This plugin is built in: it is on for every organization and cannot be switched off here");
567
+ /**
568
+ * `extension.plugin_not_installed`: switching on an extension whose plugin is not installed in the
569
+ * org. Names the plugin (details `[{ itemId, plugin }]`). `db` may be a transaction.
570
+ */
571
+ export async function pluginNotInstalledError(db, extension, itemId) {
572
+ const item = await db
573
+ .selectFrom("catalog_items")
574
+ .select("name")
575
+ .where("id", "=", itemId)
576
+ .executeTakeFirst();
577
+ const name = item?.name ?? extension;
578
+ return new ApiError("extension.plugin_not_installed", `Extension ${extension} comes with the plugin ${name}, which is not installed in this organization: enable the plugin ${name} on the Plugins page first`, [{ itemId, plugin: name }]);
579
+ }
533
580
  const requiredError = (plugin) => new ApiError("extension.required", `Extension ${plugin} is required by this deployment (a bootstrap plugin): it is enabled in every organization and cannot be disabled`);
@@ -19,6 +19,22 @@ export declare function pluginItemsOf(db: KernelSchemaDb, names: readonly string
19
19
  * one, layering spec §4): its switch cannot go off.
20
20
  */
21
21
  export declare const itemLocked: (i: ExtensionPluginItem) => boolean;
22
+ /**
23
+ * The catalog item whose org install brings the extension into an org, or null when the
24
+ * extension has no such plugin path: a required bootstrap plugin, a config-declared extension (no
25
+ * live item), one several live items bring, and one whose item is builtin (on in every org without
26
+ * an install). With a plugin path the extension's org switch needs that plugin installed in the
27
+ * org (extension switch spec 2026-12-02 §3.1).
28
+ */
29
+ export declare function pluginPathItem(items: readonly ExtensionPluginItem[] | undefined, required: boolean): string | null;
30
+ /**
31
+ * Whether the extension's plugin is installed in the org, as the org's lists read it:
32
+ * `installed` / `not_installed` for an extension with a plugin path ({@link pluginPathItem}), null
33
+ * without one. `liveItems`: the items with a live install in the org ({@link liveInstalledItems}).
34
+ */
35
+ export declare function pluginInstallOf(items: readonly ExtensionPluginItem[] | undefined, required: boolean, liveItems: ReadonlySet<string>): "installed" | "not_installed" | null;
36
+ /** The ids of the catalog items with a live install (installing or installed) in the org. */
37
+ export declare function liveInstalledItems(db: KernelSchemaDb, orgId: string): Promise<Set<string>>;
22
38
  /**
23
39
  * Whether an org may see an extension with these live catalog items (`listed`: the items whose
24
40
  * `named` list has the org): one visible item is enough; without an item (config-declared, or its
@@ -1,3 +1,4 @@
1
+ import { LIVE_INSTALL_STATUSES } from "../catalog/install-status.js";
1
2
  import { visibleTo } from "../catalog/scope.js";
2
3
  /**
3
4
  * The live catalog items of each extension in `names`, the one recorded on its install
@@ -41,6 +42,40 @@ export async function pluginItemsOf(db, names) {
41
42
  * one, layering spec §4): its switch cannot go off.
42
43
  */
43
44
  export const itemLocked = (i) => i.builtin;
45
+ /**
46
+ * The catalog item whose org install brings the extension into an org, or null when the
47
+ * extension has no such plugin path: a required bootstrap plugin, a config-declared extension (no
48
+ * live item), one several live items bring, and one whose item is builtin (on in every org without
49
+ * an install). With a plugin path the extension's org switch needs that plugin installed in the
50
+ * org (extension switch spec 2026-12-02 §3.1).
51
+ */
52
+ export function pluginPathItem(items, required) {
53
+ if (required || items === undefined || items.length !== 1)
54
+ return null;
55
+ const [only] = items;
56
+ return only === undefined || itemLocked(only) ? null : only.id;
57
+ }
58
+ /**
59
+ * Whether the extension's plugin is installed in the org, as the org's lists read it:
60
+ * `installed` / `not_installed` for an extension with a plugin path ({@link pluginPathItem}), null
61
+ * without one. `liveItems`: the items with a live install in the org ({@link liveInstalledItems}).
62
+ */
63
+ export function pluginInstallOf(items, required, liveItems) {
64
+ const item = pluginPathItem(items, required);
65
+ if (item === null)
66
+ return null;
67
+ return liveItems.has(item) ? "installed" : "not_installed";
68
+ }
69
+ /** The ids of the catalog items with a live install (installing or installed) in the org. */
70
+ export async function liveInstalledItems(db, orgId) {
71
+ const rows = await db
72
+ .selectFrom("plugin_installs")
73
+ .select("item_id")
74
+ .where("org_id", "=", orgId)
75
+ .where("status", "in", [...LIVE_INSTALL_STATUSES])
76
+ .execute();
77
+ return new Set(rows.map((r) => r.item_id));
78
+ }
44
79
  /**
45
80
  * Whether an org may see an extension with these live catalog items (`listed`: the items whose
46
81
  * `named` list has the org): one visible item is enough; without an item (config-declared, or its
@@ -86,8 +86,6 @@ export interface OperationError {
86
86
  * `marketplaces.add` duplicate check is tool policy; the web accepts a duplicate source),
87
87
  * `agent.visibility_web_only`,
88
88
  * `catalog.org_owned` (`plugins.set-shown` leaves an org's own plugin to that org),
89
- * `extension.switch_via_plugin` (`extensions.set-enabled` leaves a catalog plugin's extension to
90
- * `plugins.set-enabled`),
91
89
  * `account.self` / `account.no_change` (disabling your own account, a status that is set already),
92
90
  * and `platform.secret_record_web_only` (a secret record the card cannot ask for; ruling R1).
93
91
  */
@@ -1,5 +1,4 @@
1
1
  import { type OnModuleInit } from "@nestjs/common";
2
- import { OrgExtensionSwitch } from "../../catalog/org-extension-switch.js";
3
2
  import { ExtensionDisplayNames } from "../../extensions/display-name.js";
4
3
  import { OrgExtensionConfigs } from "../../extensions/org-config.js";
5
4
  import { OrgOverrides, type OrgOverrideView } from "../../extensions/org-overrides.js";
@@ -29,12 +28,11 @@ export declare class ExtensionsOperations implements OnModuleInit {
29
28
  private readonly switches;
30
29
  private readonly pointDefaults;
31
30
  private readonly overrides;
32
- private readonly extSwitch;
33
- constructor(registry: PlatformOperations, configs: OrgExtensionConfigs, displayNames: ExtensionDisplayNames, kdb: KernelDb, switches: OrgToolSwitches, pointDefaults: ExtensionPointDefaults, overrides: OrgOverrides, extSwitch: OrgExtensionSwitch);
31
+ constructor(registry: PlatformOperations, configs: OrgExtensionConfigs, displayNames: ExtensionDisplayNames, kdb: KernelDb, switches: OrgToolSwitches, pointDefaults: ExtensionPointDefaults, overrides: OrgOverrides);
34
32
  /**
35
- * Why `extensions.set-enabled` refuses the switch before writing, or null: an extension of one
36
- * catalog plugin is switched through its plugin; a required or builtin one cannot go off. The
37
- * write itself repeats every rule ({@link OrgExtensionSwitch.put}).
33
+ * Why `extensions.set-enabled` refuses the switch before writing, or null: switching on needs the
34
+ * extension's plugin installed in the org; a required or builtin one cannot go off. The write
35
+ * itself repeats every rule ({@link OrgExtensionConfigs.put}).
38
36
  */
39
37
  private switchRefusal;
40
38
  /** The org view of one extension, or `extension.not_found` (not installed or not offered). */
@@ -7,14 +7,13 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __param = (this && this.__param) || function (paramIndex, decorator) {
8
8
  return function (target, key) { decorator(target, key, paramIndex); }
9
9
  };
10
- import { EXTENSION_POINTS, parseExtensionPoint, ToolError } from "@capybara-hq/plugin-sdk";
10
+ import { EXTENSION_POINTS, parseExtensionPoint } from "@capybara-hq/plugin-sdk";
11
11
  import { Inject, Injectable } from "@nestjs/common";
12
12
  import { z } from "zod";
13
13
  import { OrgExtensionParams, OrgExtensionPut, OrgExtensionToolParams, OrgOverridesPut, OrgOverridesTake, OrgToolSwitchPut, } from "../../catalog/org-extension.schemas.js";
14
- import { OrgExtensionSwitch } from "../../catalog/org-extension-switch.js";
15
14
  import { ApiError } from "../../errors.js";
16
15
  import { ExtensionDisplayNames } from "../../extensions/display-name.js";
17
- import { OrgExtensionConfigs, providesOf, } from "../../extensions/org-config.js";
16
+ import { listedInOrg, OrgExtensionConfigs, pluginNotInstalledError, providesOf, } from "../../extensions/org-config.js";
18
17
  import { OrgOverrides } from "../../extensions/org-overrides.js";
19
18
  import { ExtensionPointDefaults } from "../../extensions/point-defaults.js";
20
19
  import { ExtensionPointDefaultBody, ExtensionPointParams, } from "../../extensions/point-defaults.schemas.js";
@@ -36,11 +35,6 @@ const Extension = OrgExtensionParams.shape.extension;
36
35
  const Tool = OrgExtensionToolParams.shape.tool;
37
36
  const SwitchConfirm = OrgToolSwitchPut.shape.confirm.unwrap();
38
37
  const Force = OrgExtensionPut.shape.force;
39
- /**
40
- * Why `extensions.set-enabled` does not switch an extension that ships in one catalog plugin: that
41
- * switch is the plugin's (with its uninstall preview), `plugins.set-enabled`.
42
- */
43
- const viaPlugin = (extension, itemId) => `Extension ${extension} ships in a catalog plugin, whose switch it is: switch the plugin with plugins.set-enabled (itemId ${itemId}; switching off takes the confirm value of plugins.disable-preview).`;
44
38
  /** The next step of an extension that needs its org config. */
45
39
  const NEEDS_ORG_CONFIG = {
46
40
  orgAdmin: "Read what is missing with extensions.org-config, then fill in the org's own copy of the overridable fields with extensions.save-org-config; the person enters secrets in a secure input request.",
@@ -99,8 +93,7 @@ let ExtensionsOperations = class ExtensionsOperations {
99
93
  switches;
100
94
  pointDefaults;
101
95
  overrides;
102
- extSwitch;
103
- constructor(registry, configs, displayNames, kdb, switches, pointDefaults, overrides, extSwitch) {
96
+ constructor(registry, configs, displayNames, kdb, switches, pointDefaults, overrides) {
104
97
  this.registry = registry;
105
98
  this.configs = configs;
106
99
  this.displayNames = displayNames;
@@ -108,18 +101,18 @@ let ExtensionsOperations = class ExtensionsOperations {
108
101
  this.switches = switches;
109
102
  this.pointDefaults = pointDefaults;
110
103
  this.overrides = overrides;
111
- this.extSwitch = extSwitch;
112
104
  }
113
105
  /**
114
- * Why `extensions.set-enabled` refuses the switch before writing, or null: an extension of one
115
- * catalog plugin is switched through its plugin; a required or builtin one cannot go off. The
116
- * write itself repeats every rule ({@link OrgExtensionSwitch.put}).
106
+ * Why `extensions.set-enabled` refuses the switch before writing, or null: switching on needs the
107
+ * extension's plugin installed in the org; a required or builtin one cannot go off. The write
108
+ * itself repeats every rule ({@link OrgExtensionConfigs.put}).
117
109
  */
118
110
  async switchRefusal(orgId, extension, enabled) {
119
111
  const v = await this.viewOf(orgId, extension);
120
- const plan = await this.configs.switchPlan(extension, enabled);
121
- if (plan.kind === "item") {
122
- return { code: "extension.switch_via_plugin", message: viaPlugin(extension, plan.itemId) };
112
+ const plan = await this.configs.switchPlan(orgId, extension, enabled);
113
+ if (plan.kind === "not_installed") {
114
+ const e = await pluginNotInstalledError(this.kdb.db, extension, plan.itemId);
115
+ return { code: e.code, message: `${e.message} (plugins.set-enabled, itemId ${plan.itemId})` };
123
116
  }
124
117
  if (!enabled && v.required) {
125
118
  return {
@@ -151,7 +144,7 @@ let ExtensionsOperations = class ExtensionsOperations {
151
144
  this.registry.register([
152
145
  defineOperation({
153
146
  id: "extensions.list",
154
- description: "List the extensions installed on the platform as this org sees them, one compact row each: the state here (on, off, needs_config: its config for this org is not valid, unavailable: it cannot run on the platform), why an unavailable one cannot be used (reason) and your next step (next), whether it is available on the platform, the extension points it provides and the plugin that brings it. State only: an org admin reads the config with extensions.org-config and sets the org's copy with extensions.save-org-config. Pages by 50: pass the returned next as params.cursor.",
147
+ description: "List this org's extensions, as its Extensions page does (an extension whose plugin is not installed in the org is left out until plugins.set-enabled installs it), one compact row each: the state here (on, off, needs_config: its config for this org is not valid, unavailable: it cannot run on the platform), why an unavailable one cannot be used (reason) and your next step (next), whether it is available on the platform, the extension points it provides and the plugin that brings it. State only: an org admin reads the config with extensions.org-config and sets the org's copy with extensions.save-org-config. Pages by 50: pass the returned next as params.cursor.",
155
148
  input: z.object({ cursor: Cursor.optional() }).strict(),
156
149
  example: {},
157
150
  authz: { orgAction: "org.extensions.read" },
@@ -164,7 +157,7 @@ let ExtensionsOperations = class ExtensionsOperations {
164
157
  roleFor(this.kdb.db, ctx),
165
158
  ]);
166
159
  const names = await this.displayNames.many(views.map((v) => v.plugin));
167
- return pageOf(views.map((v) => {
160
+ return pageOf(views.filter(listedInOrg).map((v) => {
168
161
  const name = localized(names.get(v.plugin), v.plugin).en;
169
162
  return {
170
163
  id: v.plugin,
@@ -184,13 +177,11 @@ let ExtensionsOperations = class ExtensionsOperations {
184
177
  }),
185
178
  defineOperation({
186
179
  id: "extensions.set-enabled",
187
- description: "Switch an extension that is not a catalog plugin's on or off in this org (org admins): one declared in the deployment config, or one several catalog plugins bring. An extension shipped in one catalog plugin is switched with plugins.set-enabled; extensions.list shows its catalogItemId. Switching on never waits for config: an extension with no valid config for the org is switched on anyway and the result's configNeeded says how to fill in the org's copy (extensions.save-org-config). Switching off is refused while other enabled extensions depend on it, unless force is true (they lose what they depend on).",
180
+ description: "Start or stop one extension in this org (org admins). It switches the extension only: its plugin stays installed and its config is kept, and switching it on again needs no install. The extension's plugin must be installed in the org (plugins.set-enabled installs and uninstalls plugins; extensions.list shows the catalogItemId). Switching on never waits for config: an extension with no valid config for the org is switched on anyway and the result's configNeeded says how to fill in the org's copy (extensions.save-org-config). Switching off is refused while other enabled extensions depend on it, unless force is true (they lose what they depend on).",
188
181
  input: z.object({ extension: Extension, enabled: z.boolean(), force: Force }).strict(),
189
182
  example: { extension: "web-search-tavily", enabled: true },
190
183
  authz: { orgAction: "org.extensions.manage" },
191
184
  mirrors: ["PUT /api/v1/orgs/:orgId/extensions/:extension"],
192
- // the confirm of a catalog plugin's switch-off: that switch is plugins.set-enabled's
193
- omits: ["confirm"],
194
185
  confirm: "org",
195
186
  errors: [
196
187
  {
@@ -198,8 +189,8 @@ let ExtensionsOperations = class ExtensionsOperations {
198
189
  next: "The extension is not running or not visible to this org; check the name with extensions.list.",
199
190
  },
200
191
  {
201
- code: "extension.switch_via_plugin",
202
- next: "Switch its plugin with plugins.set-enabled (the itemId is extensions.list's catalogItemId).",
192
+ code: "extension.plugin_not_installed",
193
+ next: "Its plugin is not installed in this org: install it with plugins.set-enabled (the itemId is in the message and extensions.list's catalogItemId), which starts the extension too.",
203
194
  },
204
195
  {
205
196
  code: "extension.required",
@@ -255,14 +246,7 @@ let ExtensionsOperations = class ExtensionsOperations {
255
246
  : { zh: `在本组织停用扩展「${x.zh}」`, en: `Disable extension "${x.en}" in the org` };
256
247
  },
257
248
  run: async (input, ctx) => {
258
- const refusal = await this.switchRefusal(ctx.orgId, input.extension, input.enabled);
259
- if (refusal?.code === "extension.switch_via_plugin")
260
- throw new ToolError(refusal.message);
261
- const r = await this.extSwitch.put(ctx.orgId, ctx.actorAccountId, input.extension, { enabled: input.enabled, ...(input.force === true && { force: true }) }, undefined, viaAgent("extensions.set-enabled", ctx));
262
- // unreachable: only a catalog plugin's switch-off answers a preview, refused above
263
- if (r.kind === "preview")
264
- throw new Error("extensions.set-enabled: unexpected preview");
265
- const v = r.view;
249
+ const v = await this.configs.put(ctx.orgId, input.extension, { enabled: input.enabled, ...(input.force === true && { force: true }) }, ctx.actorAccountId, undefined, { auditDetail: { reason: "org_switch", ...viaAgent("extensions.set-enabled", ctx) } });
266
250
  return {
267
251
  extension: v.plugin,
268
252
  state: stateOf(v),
@@ -664,7 +648,6 @@ ExtensionsOperations = __decorate([
664
648
  __param(3, Inject(KernelDb)),
665
649
  __param(4, Inject(OrgToolSwitches)),
666
650
  __param(5, Inject(ExtensionPointDefaults)),
667
- __param(6, Inject(OrgOverrides)),
668
- __param(7, Inject(OrgExtensionSwitch))
651
+ __param(6, Inject(OrgOverrides))
669
652
  ], ExtensionsOperations);
670
653
  export { ExtensionsOperations };
@@ -382,7 +382,7 @@ export declare function summaryText(summary: string, framing: {
382
382
  * `user_message` / `assistant_message`(content 原样,含 opaque;aborted 的也回放;媒体引用按
383
383
  * {@link resolveMedia} 换成 base64 或文字占位)/ `reminder`
384
384
  * (`role: "system"`,放在它所在的位置)/ `tools_changed` (`role: "system"`, the text of {@link toolsChangedText}, at its position) /
385
- * `device_paths` (`role: "system"` at its position, `devicePathsText`; covered by the
385
+ * `device_paths` (`role: "system"` at its position, `managedDevicePathsText`; covered by the
386
386
  * summary: the latest one follows the summary) /
387
387
  * `run_context` (`role: "system"` at its position, `runContextText`; covered by the summary: the
388
388
  * current context follows the summary whole; a user message's sender line by `senderLines`) /
@@ -4,7 +4,6 @@ import { renderBackgroundFinished } from "../../background/render.js";
4
4
  import { isAbsorbable, refFileId, } from "../../conversations/events.js";
5
5
  import { catalogText, isAlwaysLoaded } from "../../tools/builtins/tool-search.js";
6
6
  import { renderSubagentDelivery } from "../subagents/render.js";
7
- import { devicePathsAt, devicePathsText } from "./device-paths.js";
8
7
  import { OUTPUT_TRUNCATED_TEXT } from "./output-truncated.js";
9
8
  import { permissionContextHistory } from "./permission-profile.js";
10
9
  import { pluginContextsAfterSummary, pluginContextText } from "./plugin-context.js";
@@ -607,7 +606,7 @@ export function summaryText(summary, framing) {
607
606
  * `user_message` / `assistant_message`(content 原样,含 opaque;aborted 的也回放;媒体引用按
608
607
  * {@link resolveMedia} 换成 base64 或文字占位)/ `reminder`
609
608
  * (`role: "system"`,放在它所在的位置)/ `tools_changed` (`role: "system"`, the text of {@link toolsChangedText}, at its position) /
610
- * `device_paths` (`role: "system"` at its position, `devicePathsText`; covered by the
609
+ * `device_paths` (`role: "system"` at its position, `managedDevicePathsText`; covered by the
611
610
  * summary: the latest one follows the summary) /
612
611
  * `run_context` (`role: "system"` at its position, `runContextText`; covered by the summary: the
613
612
  * current context follows the summary whole; a user message's sender line by `senderLines`) /
@@ -895,12 +894,6 @@ export function buildModelRequest(input) {
895
894
  content: runContextFullText(currentRunContext(events, after)),
896
895
  });
897
896
  }
898
- // environments spec §13.1: the instance's directories told before the boundary follow the
899
- // summary again (the model keeps using the real paths)
900
- const paths = devicePathsAt(events, after);
901
- if (paths && paths.payload.permissionContextVersion !== 1) {
902
- messages.push({ role: "system", content: devicePathsText(paths.payload) });
903
- }
904
897
  for (const contribution of permissionHistory.restored) {
905
898
  messages.push({ role: "system", content: contribution.text });
906
899
  }
@@ -1006,8 +999,6 @@ export function buildModelRequest(input) {
1006
999
  const contribution = permissionHistory.ordered.get(e.seq);
1007
1000
  if (contribution)
1008
1001
  messages.push({ role: "system", content: contribution.text });
1009
- else if (e.type === "device_paths")
1010
- messages.push({ role: "system", content: devicePathsText(e.payload) });
1011
1002
  break;
1012
1003
  }
1013
1004
  // a context@1 provider's text (memory spec §6.1); empty text (nothing any more) renders nothing