@capybara-hq/kernel 0.1.0-rc.77 → 0.1.0-rc.79

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 (247) hide show
  1. package/bootstrap.json +6 -6
  2. package/dist/admin/outbox-dead.service.js +4 -1
  3. package/dist/agents/admin-agents.js +2 -0
  4. package/dist/agents/agent-lookup.js +4 -0
  5. package/dist/agents/agent-profile.service.js +3 -0
  6. package/dist/agents/agents.service.js +7 -0
  7. package/dist/agents/org-templates.js +1 -0
  8. package/dist/agents/pending-connector-requests.js +6 -0
  9. package/dist/agents/pending-connectors.service.js +2 -0
  10. package/dist/agents/readiness.service.d.ts +9 -3
  11. package/dist/agents/readiness.service.js +3 -3
  12. package/dist/agents/templates.js +1 -0
  13. package/dist/background/finish.js +3 -0
  14. package/dist/background/stop.js +2 -0
  15. package/dist/background/store.js +44 -0
  16. package/dist/background/tasks.service.js +3 -2
  17. package/dist/background/wake-rules.js +1 -0
  18. package/dist/catalog/admin-catalog.controller.d.ts +6 -0
  19. package/dist/catalog/admin-catalog.controller.js +13 -0
  20. package/dist/catalog/catalog-content.d.ts +9 -0
  21. package/dist/catalog/catalog-content.js +3 -1
  22. package/dist/catalog/catalog-items.js +4 -0
  23. package/dist/catalog/catalog-scope.service.js +4 -1
  24. package/dist/catalog/component-promotion.js +2 -0
  25. package/dist/catalog/component-scope.controller.d.ts +9 -15
  26. package/dist/catalog/component-scope.controller.js +14 -9
  27. package/dist/catalog/component-scope.service.js +5 -0
  28. package/dist/catalog/component-state.d.ts +4 -1
  29. package/dist/catalog/component-state.js +4 -2
  30. package/dist/catalog/marketplace-fetch.d.ts +1 -1
  31. package/dist/catalog/marketplace-fetch.js +1 -1
  32. package/dist/catalog/marketplaces.service.d.ts +6 -1
  33. package/dist/catalog/marketplaces.service.js +8 -3
  34. package/dist/catalog/org-catalog.controller.d.ts +9 -0
  35. package/dist/catalog/org-catalog.controller.js +18 -0
  36. package/dist/catalog/org-catalog.schemas.d.ts +2 -4
  37. package/dist/catalog/org-catalog.schemas.js +2 -3
  38. package/dist/catalog/org-catalog.service.d.ts +10 -1
  39. package/dist/catalog/org-catalog.service.js +34 -23
  40. package/dist/catalog/org-extension-switch.d.ts +3 -10
  41. package/dist/catalog/org-extension-switch.js +14 -60
  42. package/dist/catalog/org-extension.controller.d.ts +6 -7
  43. package/dist/catalog/org-extension.controller.js +4 -4
  44. package/dist/catalog/org-extension.schemas.d.ts +1 -2
  45. package/dist/catalog/org-extension.schemas.js +1 -3
  46. package/dist/catalog/owned-items.js +10 -1
  47. package/dist/catalog/ownership.js +2 -0
  48. package/dist/catalog/plugin-import.d.ts +3 -0
  49. package/dist/catalog/plugin-import.js +10 -1
  50. package/dist/catalog/plugin-install.d.ts +1 -4
  51. package/dist/catalog/plugin-install.js +11 -12
  52. package/dist/catalog/plugin-upgrade.js +4 -1
  53. package/dist/catalog/request-rules.js +6 -0
  54. package/dist/catalog/requests.service.js +4 -0
  55. package/dist/catalog/scope-orgs.js +1 -0
  56. package/dist/catalog/scope-sync.d.ts +8 -5
  57. package/dist/catalog/scope-sync.js +21 -23
  58. package/dist/catalog/template-description.d.ts +17 -0
  59. package/dist/catalog/template-description.js +18 -0
  60. package/dist/catalog/unshare.js +3 -0
  61. package/dist/channels/connections.service.js +1 -0
  62. package/dist/channels/inbound.js +1 -0
  63. package/dist/channels/jobs.js +1 -0
  64. package/dist/channels/outbound.js +6 -0
  65. package/dist/channels/supervisor.js +1 -0
  66. package/dist/channels/threads.js +1 -0
  67. package/dist/client/client-reports.service.js +1 -0
  68. package/dist/config/fields.js +20 -4
  69. package/dist/config/schema.d.ts +1 -1
  70. package/dist/config/schema.js +13 -13
  71. package/dist/conversations/access.js +1 -0
  72. package/dist/conversations/attachments.service.js +16 -4
  73. package/dist/conversations/conversations.service.js +17 -3
  74. package/dist/conversations/event-log.js +2 -0
  75. package/dist/conversations/events.d.ts +1 -0
  76. package/dist/conversations/events.js +3 -1
  77. package/dist/conversations/files.js +3 -2
  78. package/dist/conversations/permission-profiles.service.js +1 -0
  79. package/dist/conversations/prompt-suggestions.js +2 -0
  80. package/dist/conversations/queue.js +8 -0
  81. package/dist/conversations/queue.service.js +6 -2
  82. package/dist/conversations/source.js +2 -0
  83. package/dist/conversations/titles.js +10 -2
  84. package/dist/conversations/tree.js +6 -0
  85. package/dist/db/kernel-schema.d.ts +5 -1
  86. package/dist/devices/conversation-devices.js +3 -0
  87. package/dist/devices/device-projects.service.js +2 -0
  88. package/dist/devices/devices.service.js +5 -0
  89. package/dist/devices/legacy-cleanup.job.js +5 -2
  90. package/dist/dial-in/handlers.js +2 -0
  91. package/dist/dial-in/machines.js +11 -2
  92. package/dist/dial-in/targets.d.ts +0 -2
  93. package/dist/dial-in/targets.js +0 -10
  94. package/dist/errors.d.ts +0 -1
  95. package/dist/errors.js +0 -1
  96. package/dist/evals/budget.js +14 -1
  97. package/dist/evals/run-replay.js +4 -0
  98. package/dist/evals/runs.service.js +13 -7
  99. package/dist/evals/sets.service.js +23 -8
  100. package/dist/evals/trajectory.service.d.ts +1 -1
  101. package/dist/evals/trajectory.service.js +1 -0
  102. package/dist/evals/worker.js +5 -1
  103. package/dist/extensions/admin-ops.js +1 -0
  104. package/dist/extensions/admin-view.js +3 -1
  105. package/dist/extensions/bootstrap-packages.d.ts +12 -1
  106. package/dist/extensions/bootstrap-packages.js +49 -9
  107. package/dist/extensions/bootstrap.d.ts +17 -2
  108. package/dist/extensions/bootstrap.js +41 -9
  109. package/dist/extensions/config-json-schema.d.ts +2 -2
  110. package/dist/extensions/config-json-schema.js +1 -3
  111. package/dist/extensions/deployment-config.d.ts +5 -3
  112. package/dist/extensions/deployment-config.js +3 -3
  113. package/dist/extensions/effective-config.d.ts +8 -7
  114. package/dist/extensions/effective-config.js +8 -7
  115. package/dist/extensions/enabled.js +1 -0
  116. package/dist/extensions/host.d.ts +4 -3
  117. package/dist/extensions/host.js +11 -12
  118. package/dist/extensions/instance.d.ts +1 -1
  119. package/dist/extensions/instance.js +17 -2
  120. package/dist/extensions/org-config.d.ts +35 -97
  121. package/dist/extensions/org-config.js +48 -315
  122. package/dist/extensions/org-levels.d.ts +22 -9
  123. package/dist/extensions/org-levels.js +100 -20
  124. package/dist/extensions/org-overrides.d.ts +14 -15
  125. package/dist/extensions/org-overrides.js +20 -20
  126. package/dist/extensions/package.js +1 -1
  127. package/dist/extensions/permission-set.d.ts +1 -3
  128. package/dist/extensions/permission-set.js +3 -7
  129. package/dist/extensions/permission-view.d.ts +3 -1
  130. package/dist/extensions/permission-view.js +5 -4
  131. package/dist/extensions/plugin-icons.d.ts +80 -1
  132. package/dist/extensions/plugin-icons.js +211 -2
  133. package/dist/extensions/point-defaults.d.ts +17 -18
  134. package/dist/extensions/point-defaults.js +25 -28
  135. package/dist/git/gateway.js +1 -1
  136. package/dist/git/identity.d.ts +1 -1
  137. package/dist/git/identity.js +2 -2
  138. package/dist/identity/accounts.js +6 -0
  139. package/dist/identity/api-tokens.js +8 -0
  140. package/dist/infra/outbox/outbox.dispatcher.js +4 -1
  141. package/dist/infra/outbox/outbox.service.js +2 -0
  142. package/dist/net/public-address.d.ts +3 -3
  143. package/dist/net/public-address.js +3 -3
  144. package/dist/orgs/directory-sync.d.ts +9 -6
  145. package/dist/orgs/directory-sync.js +15 -12
  146. package/dist/orgs/invitations.service.js +2 -0
  147. package/dist/orgs/members.service.js +2 -0
  148. package/dist/orgs/offboarding.js +2 -0
  149. package/dist/packages/npm.d.ts +10 -2
  150. package/dist/packages/npm.js +38 -6
  151. package/dist/platform/exclusions.js +20 -0
  152. package/dist/platform/ops/admin-items.ops.js +1 -0
  153. package/dist/platform/ops/agents.ops.js +1 -0
  154. package/dist/platform/ops/connectors.ops.js +7 -1
  155. package/dist/platform/ops/extensions.ops.d.ts +6 -8
  156. package/dist/platform/ops/extensions.ops.js +18 -97
  157. package/dist/platform/ops/org-plugins.ops.d.ts +3 -6
  158. package/dist/platform/ops/org-plugins.ops.js +14 -65
  159. package/dist/platform/ops/outbox.ops.js +1 -0
  160. package/dist/platform/ops/person-inputs.d.ts +0 -4
  161. package/dist/platform/ops/person-inputs.js +0 -8
  162. package/dist/platform/ops/plugins.ops.d.ts +4 -1
  163. package/dist/platform/ops/plugins.ops.js +27 -34
  164. package/dist/platform/person-fields.d.ts +1 -1
  165. package/dist/platform/person-fields.js +1 -1
  166. package/dist/platform/sealed-reveals.js +2 -0
  167. package/dist/push/recipients.js +9 -2
  168. package/dist/runtime/permission-context.service.js +1 -0
  169. package/dist/runtime/plugin-context.service.js +1 -0
  170. package/dist/runtime/project/knowledge.service.js +1 -0
  171. package/dist/runtime/run-context.service.js +1 -0
  172. package/dist/runtime/run-deletion.js +4 -0
  173. package/dist/runtime/run-execution.js +2 -0
  174. package/dist/runtime/run-finish.js +3 -0
  175. package/dist/runtime/run-jobs.js +3 -0
  176. package/dist/runtime/run-orphan-check.job.js +7 -0
  177. package/dist/runtime/run-queue.js +21 -2
  178. package/dist/runtime/run-worker.js +5 -0
  179. package/dist/runtime/step-log.js +1 -0
  180. package/dist/runtime/subagents/child.js +2 -0
  181. package/dist/runtime/subagents/deliveries.js +3 -0
  182. package/dist/runtime/subagents/dispatch.js +5 -1
  183. package/dist/runtime/subagents/messages.js +2 -0
  184. package/dist/runtime/subagents/org-agents.d.ts +1 -1
  185. package/dist/runtime/subagents/org-agents.js +2 -1
  186. package/dist/runtime/usage-report-jobs.js +1 -0
  187. package/dist/runtime/usage-report.js +3 -0
  188. package/dist/runtime/waits.js +5 -0
  189. package/dist/sandbox/cloud-permission-snapshot.js +9 -3
  190. package/dist/sandbox/device-reports.js +3 -0
  191. package/dist/sandbox/legacy-retirement.js +5 -1
  192. package/dist/sandbox/managed-device-context.js +1 -0
  193. package/dist/sandbox/modes.d.ts +5 -2
  194. package/dist/sandbox/modes.js +8 -4
  195. package/dist/sandbox/network-grants.js +2 -0
  196. package/dist/sandbox/operation-intents.js +58 -0
  197. package/dist/sandbox/operation-reconcile.job.js +2 -0
  198. package/dist/sandbox/permission-authority-lock.js +12 -6
  199. package/dist/sandbox/permission-call-grants.js +6 -0
  200. package/dist/sandbox/permission-ceiling-writes.js +5 -0
  201. package/dist/sandbox/permission-config-publication.js +5 -0
  202. package/dist/sandbox/permission-instance-writes.js +4 -0
  203. package/dist/sandbox/permission-runtime-reports.js +10 -1
  204. package/dist/sandbox/permission-transitions.js +3 -0
  205. package/dist/sandbox/review/review.service.js +8 -5
  206. package/dist/sandbox/sandbox.service.d.ts +14 -0
  207. package/dist/sandbox/sandbox.service.js +195 -29
  208. package/dist/sandbox/unreachable.js +4 -1
  209. package/dist/schedules/fire.js +9 -3
  210. package/dist/schedules/pause.js +2 -0
  211. package/dist/schedules/run-tracker.js +16 -1
  212. package/dist/schedules/scheduled-tasks.service.js +4 -2
  213. package/dist/schedules/store.js +8 -0
  214. package/dist/schedules/unattended.js +4 -0
  215. package/dist/setup/setup-checks.service.d.ts +1 -10
  216. package/dist/setup/setup-checks.service.js +19 -29
  217. package/dist/shares/share-open.service.js +1 -0
  218. package/dist/shares/shares.service.js +2 -0
  219. package/dist/skills/agent-skills.service.d.ts +1 -1
  220. package/dist/skills/agent-skills.service.js +13 -2
  221. package/dist/skills/catalog.js +3 -0
  222. package/dist/skills/changes.service.js +10 -2
  223. package/dist/skills/conversation-catalog.js +1 -0
  224. package/dist/skills/skills.service.js +4 -0
  225. package/dist/tools/approvals.service.js +40 -0
  226. package/dist/tools/builtins/schedules.js +11 -6
  227. package/dist/tools/builtins/web/fetch.js +1 -1
  228. package/dist/tools/builtins/web/search.js +1 -1
  229. package/dist/tools/connector-account-policies.js +3 -0
  230. package/dist/tools/connectors.controller.d.ts +37 -9
  231. package/dist/tools/connectors.controller.js +47 -12
  232. package/dist/tools/connectors.schemas.d.ts +4 -0
  233. package/dist/tools/connectors.schemas.js +13 -1
  234. package/dist/tools/connectors.service.d.ts +17 -0
  235. package/dist/tools/connectors.service.js +36 -2
  236. package/dist/tools/drift.js +5 -0
  237. package/dist/tools/git-push-policy.js +3 -0
  238. package/dist/tools/oauth/discovery.d.ts +2 -2
  239. package/dist/tools/oauth/discovery.js +2 -2
  240. package/dist/tools/oauth/flow.js +1 -0
  241. package/dist/tools/outputs.js +2 -0
  242. package/dist/tools/permission-approval-jobs.js +3 -0
  243. package/dist/tools/pipeline.js +2 -0
  244. package/dist/tools/policy.js +3 -0
  245. package/dist/tools/questions.service.js +13 -0
  246. package/package.json +3 -3
  247. package/src/migrations/0181_connector_description.sql +3 -0
@@ -10,8 +10,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
10
10
  import { EXTENSION_POINTS, parseExtensionPoint, } from "@capybara-hq/plugin-sdk";
11
11
  import { Inject, Injectable } from "@nestjs/common";
12
12
  import { assertItemInstallable, assertPluginSourcesInstallable, disabledPluginItems, pluginItemDisabled, } from "../catalog/request-rules.js";
13
- import { changedPaths, configFields, getPath, secretValues } from "../config/fields.js";
14
- import { createSecrets } from "../ctx/secrets.js";
15
13
  import { ApiError } from "../errors.js";
16
14
  import { audit } from "../identity/audit.js";
17
15
  import { afterCommit } from "../infra/database/after-commit.js";
@@ -19,10 +17,7 @@ import { KernelDb } from "../infra/database/kernel-db.js";
19
17
  import { TransactionHost } from "../infra/database/transaction-host.js";
20
18
  import { Transactional } from "../infra/database/transactional.decorator.js";
21
19
  import { KernelEvents } from "../infra/events/kernel-events.js";
22
- import { KERNEL_LOGGER, KEYRING } from "../infra/tokens.js";
23
20
  import { OrgLock } from "../orgs/org-lock.js";
24
- import { toIssues } from "./config-issues.js";
25
- import { configMessages, safeConfigFormSchema } from "./config-json-schema.js";
26
21
  import { EffectiveConfigs } from "./effective-config.js";
27
22
  import { alwaysOnExtensions, enabledExtensionsQuery, requiredExtensions } from "./enabled.js";
28
23
  import { ExtensionHost } from "./host.js";
@@ -31,10 +26,6 @@ import { fieldLevels } from "./org-levels.js";
31
26
  import { permissionGroups, } from "./permission-view.js";
32
27
  import { itemLocked, itemsVisible, offeredToOrg, pluginItemsOf, scopeListedFor, } from "./plugin-items.js";
33
28
  import { ExtensionAvailability } from "./unavailable.js";
34
- export { toIssues };
35
- /** 内核自己的 secrets 命名空间(plugin-sdk 把 `kernel` 列为保留插件名,插件不会与之相撞)。 */
36
- const SECRETS_NAMESPACE = "kernel";
37
- const secretKey = (plugin, path) => `plugin-config:${plugin}:${path}`;
38
29
  /** The extension points an extension provides, from its permission list's `provides` group. */
39
30
  export function providesOf(view) {
40
31
  return view.permissions.filter((g) => g.category === "provides").flatMap((g) => g.items);
@@ -54,95 +45,39 @@ function scopeFields(items, required) {
54
45
  switchable: !locked,
55
46
  };
56
47
  }
57
- const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
58
- /** 按点路径设值,缺失的中间对象自动创建;中间节点不是对象时放弃(交给 schema 校验报错)。 */
59
- function setDeep(obj, path, value) {
60
- const segs = path.split(".");
61
- let node = obj;
62
- for (const seg of segs.slice(0, -1)) {
63
- const next = node[seg];
64
- if (next === undefined)
65
- node[seg] = {};
66
- else if (!isObj(next))
67
- return;
68
- node = node[seg];
69
- }
70
- node[segs[segs.length - 1]] = value;
71
- }
72
- /** 按点路径删除键(路径不存在时无操作)。 */
73
- function deleteDeep(obj, path) {
74
- const segs = path.split(".");
75
- let node = obj;
76
- for (const seg of segs.slice(0, -1)) {
77
- if (!isObj(node))
78
- return;
79
- node = node[seg];
80
- }
81
- if (isObj(node))
82
- delete node[segs[segs.length - 1]];
83
- }
84
- /** 插件组织级 schema 中标记 secret 的叶子点路径(对象路径之外的 secret 已在插件加载时拒绝)。 */
85
- function secretPaths(schema) {
86
- return configFields(schema)
87
- .filter((f) => f.meta?.secret)
88
- .map((f) => f.path);
89
- }
90
48
  /**
91
- * An org's stored config of a plugin no longer matches its org schema (e.g. after a plugin upgrade):
92
- * a known state, not a read error. The message names the plugin, the org and the failing paths,
93
- * never the values.
94
- */
95
- export class OrgConfigInvalidError extends Error {
96
- plugin;
97
- orgId;
98
- constructor(plugin, orgId, detail) {
99
- super(`plugin ${plugin}: stored organization config for org ${orgId} is invalid: ${detail}`);
100
- this.plugin = plugin;
101
- this.orgId = orgId;
102
- this.name = "OrgConfigInvalidError";
103
- }
104
- }
105
- /**
106
- * Org-level extension config: enablement and the `config.org` config stored per org.
107
- * The non-secret part is stored in `capybara.extension_org_configs.config`; each secret field is
108
- * encrypted separately in `capybara.secrets` (namespace `kernel`, key `plugin-config:<plugin>:<path>`),
109
- * written in the same transaction as the config row.
49
+ * Org-level extension enablement (`capybara.extension_org_configs.enabled`) and the org's view of
50
+ * each extension. An org's config of an extension is its copy of the overridable deployment fields
51
+ * (`OrgOverrides`, `EffectiveConfigs`); the retired `config` column and the `plugin-config:` org
52
+ * secrets are neither read nor written (spec §5.4; dropped by a later contract migration).
110
53
  * No in-process state: every call reads PG, safe with multiple replicas.
111
54
  */
112
55
  let OrgExtensionConfigs = class OrgExtensionConfigs {
113
56
  kdb;
114
57
  txHost;
115
- keyring;
116
58
  host;
117
59
  orgLock;
118
60
  installs;
119
61
  events;
120
- log;
121
62
  effective;
122
63
  availability;
123
- /** Test seam only: a deterministic interleaving point after the config rows are read, before the secrets. */
124
- afterConfigRowRead;
125
- constructor(kdb, txHost, keyring, host,
64
+ constructor(kdb, txHost, host,
126
65
  /** The org row lock (orgs/org-lock.ts): joins the transaction. */
127
66
  orgLock,
128
67
  /** Install records (any desired state): the ones not loaded are listed unavailable (T14). */
129
68
  installs,
130
69
  /** `extension.org-disabled` after a disable commits (channel connections drop at once). */
131
70
  events,
132
- /** Secret values handled (written or decrypted) are masked in the logs after the commit. */
133
- log,
134
71
  /** The effective deployment config of each org (spec A §6): read for `needsConfig`. */
135
72
  effective,
136
73
  /** Why an installed extension is not loaded (`status` / `reason` of the unavailable ones). */
137
74
  availability) {
138
75
  this.kdb = kdb;
139
76
  this.txHost = txHost;
140
- this.keyring = keyring;
141
77
  this.host = host;
142
78
  this.orgLock = orgLock;
143
79
  this.installs = installs;
144
80
  this.events = events;
145
- this.log = log;
146
81
  this.effective = effective;
147
82
  this.availability = availability;
148
83
  }
@@ -150,25 +85,17 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
150
85
  get db() {
151
86
  return this.kdb.db;
152
87
  }
153
- /** 把一份组织配置里的密钥值登记到日志打码(无密钥时无操作);调用方须在事务提交后调用。 */
154
- reportSecrets(found) {
155
- if (!found)
156
- return;
157
- const values = secretValues(found.schema, found.config);
158
- if (values.length > 0)
159
- this.log.addOrgSecrets(values);
160
- }
161
88
  /** After a write committed: a disabled extension's channel connections drop at once. */
162
89
  async afterPut(orgId, plugin, enabled) {
163
90
  if (!enabled)
164
91
  await this.events.emit("extension.org-disabled", { orgId, plugin });
165
92
  }
166
93
  /**
167
- * The state of every loaded extension the org sees, in load order (no row: `enabled` false,
168
- * `config` null), then the installed extensions not loaded right now (disabled on the deployment
169
- * and the like) as `available: false`, sorted by name. The rows and secrets are read in a
170
- * `requires_new` snapshot on a second pool connection: never call it inside a transaction that
171
- * holds a pool connection (unlike {@link get}, it does not join one).
94
+ * The state of every loaded extension the org sees, in load order (no row: `enabled` false),
95
+ * then the installed extensions not loaded right now (disabled on the deployment and the like)
96
+ * as `available: false`, sorted by name. The rows are read in a `requires_new` snapshot on a
97
+ * second pool connection: never call it inside a transaction that holds a pool connection
98
+ * (unlike {@link get}, it does not join one).
172
99
  */
173
100
  async list(orgId) {
174
101
  const installed = (await this.installs.list()).map((r) => ({
@@ -187,8 +114,8 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
187
114
  return this.withLayers(orgId, views);
188
115
  }
189
116
  /**
190
- * Fills `overrideState` and the platform half of `needsConfig` (spec A §6) for the views of
191
- * extensions with overridable fields (the others always run with the platform's config, which
117
+ * Fills `overrideState` and `needsConfig` (spec A §6: the effective config alone) for the views
118
+ * of extensions with overridable fields (the others always run with the platform's config, which
192
119
  * loaded valid). Reads the org's copy rows once and the effective config of each such extension.
193
120
  */
194
121
  async withLayers(orgId, views) {
@@ -203,14 +130,13 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
203
130
  .execute()).map((r) => r.plugin));
204
131
  for (const v of layered) {
205
132
  v.overrideState = copies.has(v.plugin) ? "copy" : "following";
206
- if (!(await this.effective.get(orgId, v.plugin)).ok)
207
- v.needsConfig = true;
133
+ v.needsConfig = !(await this.effective.get(orgId, v.plugin)).ok;
208
134
  }
209
135
  return views;
210
136
  }
211
137
  /**
212
- * `REPEATABLE READ` read-only: the config rows and the secrets come from one snapshot. It is
213
- * `requires_new`: it always takes a second pool connection, so never call `list` inside a
138
+ * `REPEATABLE READ` read-only: the enablement rows and the plugin items come from one snapshot.
139
+ * It is `requires_new`: it always takes a second pool connection, so never call `list` inside a
214
140
  * transaction that holds one (a locked transaction waiting on the pool can starve it; `get`
215
141
  * joins the caller's transaction instead).
216
142
  */
@@ -218,11 +144,9 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
218
144
  const t = this.db;
219
145
  const rows = await t
220
146
  .selectFrom("extension_org_configs")
221
- .select(["plugin", "enabled", "config"])
147
+ .select(["plugin", "enabled"])
222
148
  .where("org_id", "=", orgId)
223
149
  .execute();
224
- await this.afterConfigRowRead?.();
225
- const setKeys = await this.setSecretKeys(t, orgId);
226
150
  // Required bootstrap plugins and builtin plugins' extensions are listed enabled (no org
227
151
  // config row needed, spec §7; enabled.ts)
228
152
  const required = await requiredExtensions(t);
@@ -243,7 +167,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
243
167
  });
244
168
  const views = loaded
245
169
  .filter((m) => seen(m.name))
246
- .map((m) => this.view(m, rows.find((r) => r.plugin === m.name), setKeys, required.has(m.name), items.get(m.name), on.has(m.name)));
170
+ .map((m) => this.view(m, rows.find((r) => r.plugin === m.name), required.has(m.name), items.get(m.name), on.has(m.name)));
247
171
  const names = new Set(loaded.map((m) => m.name));
248
172
  const unavailable = installed
249
173
  .filter((i) => !names.has(i.name) && seen(i.name))
@@ -253,10 +177,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
253
177
  version: i.version,
254
178
  enabled: on.has(i.name) || (rows.find((r) => r.plugin === i.name)?.enabled ?? false),
255
179
  configurable: false,
256
- config: null,
257
- secrets: [],
258
180
  permissions: [],
259
- orgConfigSchema: null,
260
181
  available: false,
261
182
  // filled in by `list` from the replicas' reports
262
183
  status: "converging",
@@ -288,35 +209,26 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
288
209
  return items.length === 1 && only ? { kind: "item", itemId: only.id } : { kind: "direct" };
289
210
  }
290
211
  /**
291
- * Writes enablement and / or config. The actor's role is re-read under the org row lock (needs
212
+ * Writes enablement. The actor's role is re-read under the org row lock (needs
292
213
  * `org.extensions.manage`).
293
- * - Extension not loaded -> `extension.not_found`; `config` given but the extension has no
294
- * `config.org` -> `extension.no_org_config`; `enabled: false` for a required bootstrap plugin
295
- * -> `extension.required` (it stays enabled in every org; its config can still be written).
296
- * - Non-secret fields are replaced as a whole by the given value; secret fields keep the old value
297
- * when omitted, `null` clears, any other value is the new value.
298
- * - The merged full config (or, without `config`, the existing config when the result is enabled)
299
- * does not satisfy the schema -> `extension.config_invalid`.
214
+ * - Extension not loaded -> `extension.not_found`; `enabled: false` for a required bootstrap
215
+ * plugin -> `extension.required` (it stays enabled in every org).
216
+ * - Enabling never checks a config: an extension whose effective config is not valid for the org
217
+ * is enabled and reported `needsConfig` (spec A §6: it gets no org-bound call until then).
300
218
  */
301
219
  async put(orgId, plugin, input,
302
220
  /** null: the kernel itself (the plugin scope sync installing a plugin), no role check. */
303
221
  actorAccountId, actorTokenId, opts = {}) {
304
222
  const manifest = this.manifest(plugin);
305
- const schema = manifest.config?.org;
306
- if (input.config !== undefined && !schema) {
307
- throw new ApiError("extension.no_org_config", `Extension ${plugin} has no organization config`);
308
- }
309
223
  const view = await this.write(orgId, plugin, manifest, input, actorAccountId, actorTokenId, opts);
310
224
  return (await this.withLayers(orgId, [view]))[0];
311
225
  }
312
226
  async write(orgId, plugin, manifest, input, actorAccountId, actorTokenId, opts) {
313
- const schema = manifest.config?.org;
314
- let seen;
315
227
  const t = this.db;
316
228
  await this.orgLock.lockFor(orgId, actorAccountId, "org.extensions.manage");
317
229
  const row = await t
318
230
  .selectFrom("extension_org_configs")
319
- .select(["enabled", "config"])
231
+ .select(["enabled"])
320
232
  .where("plugin", "=", plugin)
321
233
  .where("org_id", "=", orgId)
322
234
  .executeTakeFirst();
@@ -330,87 +242,16 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
330
242
  throw scopeLockedError();
331
243
  if (opts.ifDisabled && (row?.enabled === true || on)) {
332
244
  opts.ifDisabled.skipped = true;
333
- return this.view(manifest, row, await this.setSecretKeys(t, orgId), required, item, on);
245
+ return this.view(manifest, row, required, item, on);
334
246
  }
335
247
  // An always-on extension is enabled whatever the row said (it may predate `required` / builtin)
336
248
  const enabled = on || (input.enabled ?? row?.enabled ?? false);
337
- // validateOnly: the rules for (newly) enabling and disabling belong to the write that follows
338
- if (!opts.validateOnly) {
339
- await this.assertSwitchRules(t, orgId, manifest, input, row, on, opts);
340
- }
341
- let plain = row?.config ?? null;
342
- let changed = [];
343
- if (schema) {
344
- const secrets = createSecrets(t, this.keyring, SECRETS_NAMESPACE);
345
- const paths = secretPaths(schema);
346
- const before = structuredClone(plain ?? {});
347
- for (const p of paths) {
348
- const v = await secrets.get(orgId, secretKey(plugin, p));
349
- if (v !== null)
350
- setDeep(before, p, JSON.parse(v));
351
- }
352
- let next;
353
- if (input.config !== undefined) {
354
- next = structuredClone(input.config);
355
- for (const p of paths) {
356
- const v = getPath(input.config, p);
357
- if (v === null)
358
- deleteDeep(next, p);
359
- else if (v === undefined) {
360
- const old = getPath(before, p);
361
- if (old !== undefined)
362
- setDeep(next, p, old);
363
- }
364
- }
365
- }
366
- else {
367
- next = before;
368
- }
369
- if (input.config !== undefined || enabled) {
370
- const r = schema.safeParse(next);
371
- if (!r.success) {
372
- throw new ApiError("extension.config_invalid", `Invalid organization config for extension ${plugin}`, toIssues(r.error, configMessages(this.formSchema(plugin, manifest.version, schema))));
373
- }
374
- next = r.data;
375
- }
376
- seen = { schema, config: next };
377
- if (!opts.validateOnly && input.config !== undefined) {
378
- changed = changedPaths(schema, before, next);
379
- plain = structuredClone(next);
380
- for (const p of paths) {
381
- const v = getPath(next, p);
382
- deleteDeep(plain, p);
383
- if (v === undefined)
384
- await secrets.delete(orgId, secretKey(plugin, p));
385
- else
386
- await secrets.set(orgId, secretKey(plugin, p), JSON.stringify(v));
387
- }
388
- }
389
- }
390
- // Also without an org config schema: nothing is written
391
- if (opts.validateOnly) {
392
- return this.view(manifest, row, await this.setSecretKeys(t, orgId), required, item, on);
393
- }
394
- // 当前 schema 中已不存在的密钥路径:同一事务内清掉遗留的密文行
395
- const live = new Set((schema ? secretPaths(schema) : []).map((p) => secretKey(plugin, p)));
396
- const prefix = secretKey(plugin, "");
397
- const cleanup = createSecrets(t, this.keyring, SECRETS_NAMESPACE);
398
- for (const key of await this.setSecretKeys(t, orgId)) {
399
- if (key.startsWith(prefix) && !live.has(key))
400
- await cleanup.delete(orgId, key);
401
- }
249
+ await this.assertSwitchRules(t, orgId, manifest, input, row, on, opts);
402
250
  await t
403
251
  .insertInto("extension_org_configs")
404
- .values({
405
- plugin,
406
- org_id: orgId,
407
- enabled,
408
- config: plain === null ? null : JSON.stringify(plain),
409
- updated_by: actorAccountId,
410
- })
252
+ .values({ plugin, org_id: orgId, enabled, updated_by: actorAccountId })
411
253
  .onConflict((oc) => oc.columns(["plugin", "org_id"]).doUpdateSet({
412
254
  enabled,
413
- config: plain === null ? null : JSON.stringify(plain),
414
255
  updated_by: actorAccountId,
415
256
  updated_at: new Date(),
416
257
  }))
@@ -423,23 +264,14 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
423
264
  target: plugin,
424
265
  detail: {
425
266
  enabled,
426
- changedPaths: changed,
427
267
  ...(opts.viaItem !== undefined && { catalogItemId: opts.viaItem }),
428
268
  ...opts.auditDetail,
429
269
  },
430
270
  });
431
- // 在同一事务里读回,返回值就是刚提交的状态
432
- const saved = await t
433
- .selectFrom("extension_org_configs")
434
- .select(["plugin", "enabled", "config"])
435
- .where("plugin", "=", plugin)
436
- .where("org_id", "=", orgId)
437
- .executeTakeFirst();
438
- const view = this.view(manifest, saved, await this.setSecretKeys(t, orgId), required, item, on);
271
+ const view = this.view(manifest, { enabled }, required, item, on);
439
272
  // Only once the root transaction committed (an outer caller's too): a rolled-back write
440
- // registers no secrets and disconnects nothing
273
+ // disconnects nothing
441
274
  afterCommit(async () => {
442
- this.reportSecrets(seen);
443
275
  await this.afterPut(orgId, plugin, view.enabled);
444
276
  });
445
277
  return view;
@@ -470,70 +302,24 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
470
302
  }
471
303
  }
472
304
  /**
473
- * 读取并解密某插件在该组织下的完整配置(供运行时按组织构造插件实例)。
474
- * An always-on extension (a required bootstrap plugin, or its plugin is builtin: `enabled.ts`) is
475
- * enabled without a row (its stored config, or `{}`, is validated).
476
- * 无记录 → undefined;插件无 `config.org` 或已停用(`enabled=false`,不校验、不读密钥)时 `config` 为 null。
477
- * Inside a caller's transaction it joins that transaction (its connection and isolation, and it
478
- * sees the caller's uncommitted writes): the config row and its secrets are consistent there when
479
- * the caller holds the org row, as every writer of org configs and their secrets locks it first.
480
- * On its own it reads them in one `REPEATABLE READ` read-only snapshot.
481
- * A stored config that no longer matches the schema (e.g. after a plugin upgrade) throws
482
- * {@link OrgConfigInvalidError} (plugin, org ID and the failing paths, no values); other errors are
483
- * read errors.
305
+ * Whether `plugin` is enabled in the org: an always-on extension (a required bootstrap plugin, or
306
+ * its plugin is builtin: `enabled.ts`) is enabled without a row; no row otherwise -> undefined.
307
+ * Inside a caller's transaction it reads in that transaction (it sees the caller's uncommitted
308
+ * writes). The extension's config for the org is its effective config ({@link EffectiveConfigs}).
484
309
  */
485
310
  async get(orgId, plugin) {
486
- const manifest = this.manifest(plugin);
487
- const result = await this.getSnapshot(orgId, plugin, manifest);
488
- // the read is done (its own snapshot has ended, or it ran in the caller's transaction); a
489
- // non-null config means the plugin has an org schema and the config was decrypted
490
- const schema = manifest.config?.org;
491
- if (schema && result?.config != null)
492
- this.reportSecrets({ schema, config: result.config });
493
- return result;
494
- }
495
- /**
496
- * On its own, `REPEATABLE READ` read-only: the config row and the secrets come from one snapshot.
497
- * Inside a caller's transaction it reads in that transaction instead (`required`; consistent when
498
- * the caller holds the org row, see {@link get}): a caller holding a
499
- * connection never waits for a second one from the pool (the device provider check runs inside
500
- * locked transactions; concurrent runs would starve the pool).
501
- */
502
- async getSnapshot(orgId, plugin, manifest) {
311
+ this.manifest(plugin);
503
312
  const t = this.db;
504
313
  const row = await t
505
314
  .selectFrom("extension_org_configs")
506
- .select(["enabled", "config"])
315
+ .select(["enabled"])
507
316
  .where("plugin", "=", plugin)
508
317
  .where("org_id", "=", orgId)
509
318
  .executeTakeFirst();
510
- await this.afterConfigRowRead?.();
511
319
  const on = (await alwaysOnExtensions(t)).has(plugin);
512
320
  if (!row && !on)
513
321
  return undefined;
514
- const enabled = on || row?.enabled === true;
515
- const schema = manifest.config?.org;
516
- // 已停用的插件不会被实例化:不校验(可能是升级前存下的旧配置),也不解密密钥
517
- if (!schema || !enabled)
518
- return { enabled, config: null };
519
- return this.decode(t, orgId, plugin, schema, row?.config ?? {});
520
- }
521
- async decode(t, orgId, plugin, schema, stored) {
522
- const full = structuredClone(stored);
523
- const secrets = createSecrets(t, this.keyring, SECRETS_NAMESPACE);
524
- for (const p of secretPaths(schema)) {
525
- const v = await secrets.get(orgId, secretKey(plugin, p));
526
- if (v !== null)
527
- setDeep(full, p, JSON.parse(v));
528
- }
529
- const r = schema.safeParse(full);
530
- if (!r.success) {
531
- const detail = toIssues(r.error)
532
- .map((i) => `${i.path || "<root>"}: ${i.message}`)
533
- .join("; ");
534
- throw new OrgConfigInvalidError(plugin, orgId, detail);
535
- }
536
- return { enabled: true, config: r.data };
322
+ return { enabled: on || row?.enabled === true };
537
323
  }
538
324
  /**
539
325
  * 该组织里已启用、且所 requires 的按组织启用的能力只剩这个插件提供的插件(停用它会让它们失去依赖)。
@@ -587,7 +373,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
587
373
  orgId,
588
374
  action: "org.plugin_config.updated",
589
375
  target: plugin,
590
- detail: { enabled: false, changedPaths: [], reason },
376
+ detail: { enabled: false, reason },
591
377
  });
592
378
  return true;
593
379
  }
@@ -660,57 +446,25 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
660
446
  throw new ApiError("extension.not_found", `Extension ${plugin} is not installed`);
661
447
  return set;
662
448
  }
663
- view(m, row, setKeys, required, items,
449
+ view(m, row, required, items,
664
450
  /** Always on in every org (enabled.ts): required, or its plugin is builtin. */
665
451
  alwaysOn) {
666
- const schema = m.config?.org;
667
- const paths = schema ? secretPaths(schema) : [];
668
- let config = null;
669
- if (schema && row?.config) {
670
- config = structuredClone(row.config);
671
- for (const p of paths)
672
- deleteDeep(config, p);
673
- }
452
+ const configurable = [...fieldLevels(m.config?.deployment).values()].includes("override");
674
453
  return {
675
454
  plugin: m.name,
676
455
  version: m.version,
677
456
  enabled: alwaysOn || (row?.enabled ?? false),
678
- configurable: schema !== undefined,
679
- config,
680
- secrets: paths.map((p) => ({ path: p, set: setKeys.has(secretKey(m.name, p)) })),
457
+ configurable,
681
458
  permissions: permissionGroups(this.permissionsOf(m.name)),
682
- orgConfigSchema: schema ? this.formSchema(m.name, m.version, schema) : null,
683
459
  available: true,
684
460
  status: "enabled",
685
461
  required,
686
462
  ...scopeFields(items, required),
687
- overrideState: [...fieldLevels(m.config?.deployment).values()].includes("override")
688
- ? "following"
689
- : "none",
690
- needsConfig: schema !== undefined &&
691
- orgConfigIncomplete(schema, config, (p) => setKeys.has(secretKey(m.name, p))),
463
+ // filled in by `withLayers` (the copy rows and the effective config)
464
+ overrideState: configurable ? "following" : "none",
465
+ needsConfig: false,
692
466
  };
693
467
  }
694
- /**
695
- * The org schema's form JSON Schema, a copy (`x-ui` stripped when its hints are invalid; the host
696
- * warns at load). Never throws: a hint conversion that throws serves the plain schema, or null
697
- * (one warning per schema on this replica), so one hand-made package cannot fail the org's list.
698
- */
699
- formSchema(plugin, version, schema) {
700
- const r = safeConfigFormSchema(schema, "org", (err, fallback) => this.log.warn("the org config form schema could not be built with its UI hints; hints dropped", { plugin, version, schema: fallback, err }));
701
- return r ? structuredClone(r.schema) : null;
702
- }
703
- /** 该组织在 `kernel` 命名空间下已设置的插件配置密钥键(不解密)。 */
704
- async setSecretKeys(db, orgId) {
705
- const rows = await db
706
- .selectFrom("secrets")
707
- .select("key")
708
- .where("namespace", "=", SECRETS_NAMESPACE)
709
- .where("org_id", "=", orgId)
710
- .where("key", "like", "plugin-config:%")
711
- .execute();
712
- return new Set(rows.map((r) => r.key));
713
- }
714
468
  };
715
469
  __decorate([
716
470
  Transactional({
@@ -722,13 +476,6 @@ __decorate([
722
476
  __decorate([
723
477
  Transactional()
724
478
  ], OrgExtensionConfigs.prototype, "write", null);
725
- __decorate([
726
- Transactional({
727
- propagation: "required",
728
- isolationLevel: "repeatable read",
729
- accessMode: "read only",
730
- })
731
- ], OrgExtensionConfigs.prototype, "getSnapshot", null);
732
479
  __decorate([
733
480
  Transactional({ propagation: "mandatory" })
734
481
  ], OrgExtensionConfigs.prototype, "enabledDependents", null);
@@ -739,14 +486,12 @@ OrgExtensionConfigs = __decorate([
739
486
  Injectable(),
740
487
  __param(0, Inject(KernelDb)),
741
488
  __param(1, Inject(TransactionHost)),
742
- __param(2, Inject(KEYRING)),
743
- __param(3, Inject(ExtensionHost)),
744
- __param(4, Inject(OrgLock)),
745
- __param(5, Inject(ExtensionInstalls)),
746
- __param(6, Inject(KernelEvents)),
747
- __param(7, Inject(KERNEL_LOGGER)),
748
- __param(8, Inject(EffectiveConfigs)),
749
- __param(9, Inject(ExtensionAvailability))
489
+ __param(2, Inject(ExtensionHost)),
490
+ __param(3, Inject(OrgLock)),
491
+ __param(4, Inject(ExtensionInstalls)),
492
+ __param(5, Inject(KernelEvents)),
493
+ __param(6, Inject(EffectiveConfigs)),
494
+ __param(7, Inject(ExtensionAvailability))
750
495
  ], OrgExtensionConfigs);
751
496
  export { OrgExtensionConfigs };
752
497
  /**
@@ -768,17 +513,5 @@ export async function extensionVisibleTo(db, orgId, plugin) {
768
513
  .execute()).map((r) => r.item_id));
769
514
  return itemsVisible(items, listed);
770
515
  }
771
- /**
772
- * The stored org-only config does not satisfy the org schema (a required field missing): what the
773
- * org form reports before any edit. A set secret is not decrypted here; issues on it or below it
774
- * are ignored (it was validated when written).
775
- */
776
- function orgConfigIncomplete(schema, plain, secretSet) {
777
- const r = schema.safeParse(plain ?? {});
778
- if (r.success)
779
- return false;
780
- const secrets = secretPaths(schema).filter(secretSet);
781
- return toIssues(r.error).some((i) => !secrets.some((p) => i.path === p || i.path.startsWith(`${p}.`)));
782
- }
783
516
  const scopeLockedError = () => new ApiError("plugin.scope_locked", "This plugin is built in: it is on for every organization and cannot be switched off here");
784
517
  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`);
@@ -1,23 +1,36 @@
1
1
  import { type JsonSchema } from "@capybara-hq/plugin-sdk";
2
2
  import { z } from "zod";
3
3
  /**
4
- * The org level of a deployment config field (spec A §5.1): `hidden` (platform only, orgs never
5
- * see it; a field without `orgAccess`), `readonly` (orgs see it, cannot change it) or `override`
6
- * (the platform sets the default, an org may bring its own value). Levels sit on top-level keys of
7
- * the deployment schema only (§5.2); a nested field has its top-level key's level.
4
+ * The org level of a deployment config field (unified config spec §5.1): `override` (the default:
5
+ * the platform sets the default value, an org may save its own copy) or `platform` (marked
6
+ * `platformOnly`: set by the platform only, never shown to an org). Levels sit on top-level keys of
7
+ * the deployment schema only (spec A §5.2); a nested field has its top-level key's level. A
8
+ * `platformOnly` root makes every key `platform`. The SDK refuses a mark below a top-level key,
9
+ * but a hand-made package may carry one: such a mark fails closed, making its whole top-level key
10
+ * `platform` (an org never gets a field the plugin meant for the platform alone).
11
+ */
12
+ export type FieldLevel = "platform" | "override";
13
+ /**
14
+ * The deployment schema's object: the schema itself, or the input object of a pipe or transform
15
+ * root (`z.object({…}).transform(…)`), as zod's JSON Schema of the input shows its properties, so
16
+ * a loaded replica and one reading the static form agree. undefined for anything else.
17
+ */
18
+ export declare function deploymentObject(schema: z.ZodTypeAny | undefined): z.ZodObject | undefined;
19
+ /**
20
+ * The top-level keys of a `ZodObject` deployment schema (or the input object of a pipe or
21
+ * transform root, {@link deploymentObject}) with their level; empty otherwise.
8
22
  */
9
- export type FieldLevel = "hidden" | "readonly" | "override";
10
- /** The top-level keys of a `ZodObject` deployment schema with their level; empty otherwise. */
11
23
  export declare function fieldLevels(schema: z.ZodTypeAny | undefined): Map<string, FieldLevel>;
12
24
  /**
13
- * The same levels read from a package's static form schema (`x-org` on top-level properties), for
14
- * an install record whose module this replica has not loaded.
25
+ * The same levels read from a package's static form schema (`x-platform-only` on the root, a
26
+ * top-level property or, failing closed, anywhere below one), for an install record whose module
27
+ * this replica has not loaded.
15
28
  */
16
29
  export declare function jsonSchemaFieldLevels(schema: JsonSchema | undefined): Map<string, FieldLevel>;
17
30
  /**
18
31
  * The platform's relaxed schema (§5.4a): a plain object rebuilt from the shape with every
19
32
  * `override` key optional, so the platform may leave overridable fields empty (each org then
20
- * brings its own); hidden and read-only fields stay as declared. Object-level refinements
33
+ * brings its own); platform-only fields stay as declared. Object-level refinements
21
34
  * (`.refine` / `.superRefine`) are not carried over: they may read the absent fields, and zod
22
35
  * refuses `.extend()` on a refined object. The identity when there is no `override` key. Parse
23
36
  * through {@link parseLoadGate}, which uses this only when an overridable field is missing.