@capybara-hq/kernel 0.1.0-rc.23 → 0.1.0-rc.25

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 (293) hide show
  1. package/bootstrap.json +25 -25
  2. package/dist/admin/admin.module.d.ts +2 -1
  3. package/dist/admin/admin.module.js +9 -3
  4. package/dist/admin/health.service.d.ts +2 -2
  5. package/dist/admin/health.service.js +6 -2
  6. package/dist/admin/outbox-dead.controller.d.ts +23 -0
  7. package/dist/admin/outbox-dead.controller.js +62 -0
  8. package/dist/admin/outbox-dead.schemas.d.ts +8 -0
  9. package/dist/admin/outbox-dead.schemas.js +18 -0
  10. package/dist/admin/outbox-dead.service.d.ts +66 -0
  11. package/dist/admin/outbox-dead.service.js +179 -0
  12. package/dist/admin/overview.service.d.ts +40 -5
  13. package/dist/admin/overview.service.js +41 -7
  14. package/dist/agents/agents.module.js +3 -0
  15. package/dist/agents/agents.service.d.ts +6 -1
  16. package/dist/agents/agents.service.js +11 -2
  17. package/dist/agents/from-template.js +1 -1
  18. package/dist/background/finish.d.ts +5 -2
  19. package/dist/background/finish.js +14 -3
  20. package/dist/background/launch.d.ts +2 -1
  21. package/dist/background/launch.js +5 -2
  22. package/dist/background/store.d.ts +8 -0
  23. package/dist/background/store.js +24 -0
  24. package/dist/catalog/admin-catalog.controller.d.ts +21 -2
  25. package/dist/catalog/admin-catalog.controller.js +45 -6
  26. package/dist/catalog/admin-catalog.schemas.d.ts +5 -0
  27. package/dist/catalog/admin-catalog.schemas.js +8 -0
  28. package/dist/catalog/catalog-import.module.js +0 -4
  29. package/dist/catalog/catalog-scope.service.d.ts +63 -26
  30. package/dist/catalog/catalog-scope.service.js +146 -71
  31. package/dist/catalog/catalog.module.d.ts +2 -1
  32. package/dist/catalog/catalog.module.js +9 -4
  33. package/dist/catalog/catalog.service.d.ts +11 -4
  34. package/dist/catalog/catalog.service.js +21 -5
  35. package/dist/catalog/cli.d.ts +4 -3
  36. package/dist/catalog/cli.js +8 -13
  37. package/dist/catalog/job-ids.d.ts +8 -20
  38. package/dist/catalog/job-ids.js +11 -16
  39. package/dist/catalog/marketplaces.service.d.ts +1 -1
  40. package/dist/catalog/org-plugin-uploads.d.ts +11 -4
  41. package/dist/catalog/org-plugin-uploads.js +21 -6
  42. package/dist/catalog/owned-items.d.ts +6 -13
  43. package/dist/catalog/owned-items.js +11 -29
  44. package/dist/catalog/owned-plugin-cleanup.d.ts +8 -17
  45. package/dist/catalog/owned-plugin-cleanup.js +12 -66
  46. package/dist/catalog/platform-components.d.ts +56 -34
  47. package/dist/catalog/platform-components.js +68 -78
  48. package/dist/catalog/platform-import-update.d.ts +50 -0
  49. package/dist/catalog/platform-import-update.js +116 -0
  50. package/dist/catalog/plugin-import.d.ts +64 -26
  51. package/dist/catalog/plugin-import.js +113 -77
  52. package/dist/catalog/plugin-upgrade.d.ts +22 -4
  53. package/dist/catalog/plugin-upgrade.js +26 -19
  54. package/dist/catalog/scope-jobs.d.ts +59 -0
  55. package/dist/catalog/scope-jobs.js +65 -0
  56. package/dist/catalog/scope-sync-workers.d.ts +22 -0
  57. package/dist/catalog/scope-sync-workers.js +58 -2
  58. package/dist/catalog/scope-sync.d.ts +40 -65
  59. package/dist/catalog/scope-sync.js +114 -122
  60. package/dist/channels/channels.module.d.ts +5 -2
  61. package/dist/channels/channels.module.js +9 -2
  62. package/dist/channels/deliver-job.d.ts +25 -0
  63. package/dist/channels/deliver-job.js +25 -0
  64. package/dist/channels/inbound.d.ts +1 -1
  65. package/dist/channels/inbound.js +4 -17
  66. package/dist/channels/jobs.d.ts +28 -18
  67. package/dist/channels/jobs.js +63 -26
  68. package/dist/channels/threads.d.ts +4 -3
  69. package/dist/channels/threads.js +4 -3
  70. package/dist/config/schema.d.ts +0 -3
  71. package/dist/config/schema.js +11 -20
  72. package/dist/conversations/agent-tree.d.ts +16 -0
  73. package/dist/conversations/agent-tree.js +36 -0
  74. package/dist/conversations/conversations.controller.js +4 -2
  75. package/dist/conversations/conversations.service.d.ts +61 -19
  76. package/dist/conversations/conversations.service.js +69 -43
  77. package/dist/conversations/event-log.d.ts +7 -10
  78. package/dist/conversations/event-log.js +24 -15
  79. package/dist/conversations/events.d.ts +21 -0
  80. package/dist/conversations/events.js +26 -2
  81. package/dist/conversations/queue.service.d.ts +8 -3
  82. package/dist/conversations/queue.service.js +37 -13
  83. package/dist/conversations/source.d.ts +10 -0
  84. package/dist/conversations/source.js +17 -0
  85. package/dist/conversations/titles.d.ts +21 -8
  86. package/dist/conversations/titles.js +42 -15
  87. package/dist/conversations/tree.d.ts +10 -0
  88. package/dist/conversations/tree.js +35 -0
  89. package/dist/db/kernel-schema.d.ts +35 -22
  90. package/dist/devices/devices.service.d.ts +23 -11
  91. package/dist/devices/devices.service.js +34 -26
  92. package/dist/errors.d.ts +11 -0
  93. package/dist/errors.js +11 -0
  94. package/dist/evals/run-replay.d.ts +13 -2
  95. package/dist/evals/run-replay.js +42 -9
  96. package/dist/evals/snapshot.d.ts +7 -1
  97. package/dist/evals/snapshot.js +2 -1
  98. package/dist/extensions/extensions.module.js +3 -0
  99. package/dist/extensions/hooks.d.ts +22 -3
  100. package/dist/extensions/hooks.js +15 -1
  101. package/dist/extensions/lifecycle-hooks.d.ts +99 -0
  102. package/dist/extensions/lifecycle-hooks.js +273 -0
  103. package/dist/extensions/lifecycle.d.ts +6 -1
  104. package/dist/extensions/lifecycle.js +9 -2
  105. package/dist/extensions/org-config.d.ts +13 -1
  106. package/dist/extensions/org-config.js +19 -2
  107. package/dist/extensions/permission-view.d.ts +9 -5
  108. package/dist/extensions/permission-view.js +9 -4
  109. package/dist/extensions/point-defaults.d.ts +28 -1
  110. package/dist/extensions/point-defaults.js +44 -13
  111. package/dist/extensions/usage-subscribers.d.ts +43 -0
  112. package/dist/extensions/usage-subscribers.js +108 -0
  113. package/dist/http/api.module.js +2 -0
  114. package/dist/inbox/inbox.service.d.ts +6 -20
  115. package/dist/inbox/inbox.service.js +0 -3
  116. package/dist/inbox/org-notices.d.ts +1 -1
  117. package/dist/inbox/platform-inbox.service.d.ts +3 -32
  118. package/dist/inbox/platform-inbox.service.js +5 -54
  119. package/dist/index.d.ts +1 -0
  120. package/dist/index.js +1 -0
  121. package/dist/infra/cli/kernel-cli.module.js +5 -0
  122. package/dist/infra/events/kernel-events.d.ts +9 -35
  123. package/dist/infra/outbox/outbox-relay.module.d.ts +6 -0
  124. package/dist/infra/outbox/outbox-relay.module.js +18 -0
  125. package/dist/infra/outbox/outbox.dispatcher.d.ts +103 -0
  126. package/dist/infra/outbox/outbox.dispatcher.js +310 -0
  127. package/dist/infra/outbox/outbox.module.d.ts +24 -0
  128. package/dist/infra/outbox/outbox.module.js +50 -0
  129. package/dist/infra/outbox/outbox.relay.d.ts +31 -0
  130. package/dist/infra/outbox/outbox.relay.js +76 -0
  131. package/dist/infra/outbox/outbox.service.d.ts +51 -0
  132. package/dist/infra/outbox/outbox.service.js +131 -0
  133. package/dist/infra/outbox/registry.d.ts +244 -0
  134. package/dist/infra/outbox/registry.js +234 -0
  135. package/dist/infra/queue/queue-names.d.ts +8 -0
  136. package/dist/infra/queue/queue-names.js +17 -0
  137. package/dist/kernel.module.js +3 -0
  138. package/dist/local-devices/device-policy.service.d.ts +4 -0
  139. package/dist/local-devices/device-policy.service.js +61 -3
  140. package/dist/local-devices/machines.d.ts +13 -5
  141. package/dist/local-devices/machines.js +45 -7
  142. package/dist/local-devices/policy-summary.d.ts +44 -1
  143. package/dist/local-devices/policy-summary.js +69 -0
  144. package/dist/models/embeddings.d.ts +9 -9
  145. package/dist/models/embeddings.js +11 -18
  146. package/dist/models/models.module.js +3 -0
  147. package/dist/orgs/directory-sync.js +1 -1
  148. package/dist/orgs/members.service.js +1 -1
  149. package/dist/orgs/offboarding.d.ts +50 -57
  150. package/dist/orgs/offboarding.js +117 -162
  151. package/dist/orgs/orgs.module.js +3 -0
  152. package/dist/orgs/orgs.service.d.ts +11 -5
  153. package/dist/orgs/orgs.service.js +22 -6
  154. package/dist/overrides.d.ts +23 -3
  155. package/dist/overrides.js +12 -2
  156. package/dist/platform/exclusions.js +5 -0
  157. package/dist/platform/ops/admin.ops.js +18 -2
  158. package/dist/platform/ops/catalog-admin.ops.d.ts +3 -1
  159. package/dist/platform/ops/catalog-admin.ops.js +32 -4
  160. package/dist/platform/ops/inbox.ops.js +4 -20
  161. package/dist/platform/ops/outbox.ops.d.ts +21 -0
  162. package/dist/platform/ops/outbox.ops.js +125 -0
  163. package/dist/platform/ops/platform.ops.d.ts +1 -0
  164. package/dist/platform/ops/platform.ops.js +6 -3
  165. package/dist/platform/ops/schedules.ops.js +40 -1
  166. package/dist/platform/platform-ops.module.d.ts +2 -1
  167. package/dist/platform/platform-ops.module.js +3 -0
  168. package/dist/platform/route-schemas.js +3 -0
  169. package/dist/roles/worker-role.d.ts +10 -5
  170. package/dist/roles/worker-role.js +19 -9
  171. package/dist/roles/worker.module.js +11 -8
  172. package/dist/runtime/context/build.d.ts +3 -0
  173. package/dist/runtime/context/build.js +15 -0
  174. package/dist/runtime/context/plugin-context.d.ts +20 -0
  175. package/dist/runtime/context/plugin-context.js +30 -0
  176. package/dist/runtime/langgraph-loop.js +17 -5
  177. package/dist/runtime/limits.d.ts +15 -0
  178. package/dist/runtime/limits.js +25 -0
  179. package/dist/runtime/loop.d.ts +16 -3
  180. package/dist/runtime/loop.js +2 -1
  181. package/dist/runtime/maintenance.d.ts +7 -3
  182. package/dist/runtime/maintenance.js +52 -12
  183. package/dist/runtime/model-call.d.ts +20 -17
  184. package/dist/runtime/model-call.js +15 -24
  185. package/dist/runtime/plugin-context.service.d.ts +69 -0
  186. package/dist/runtime/plugin-context.service.js +208 -0
  187. package/dist/runtime/queue-lost-jobs.d.ts +24 -0
  188. package/dist/runtime/queue-lost-jobs.js +51 -0
  189. package/dist/runtime/run-control.d.ts +47 -5
  190. package/dist/runtime/run-control.js +79 -12
  191. package/dist/runtime/run-finish.d.ts +39 -12
  192. package/dist/runtime/run-finish.js +97 -14
  193. package/dist/runtime/run-jobs.d.ts +95 -13
  194. package/dist/runtime/run-jobs.js +199 -28
  195. package/dist/runtime/run-orphan-check.job.d.ts +142 -0
  196. package/dist/runtime/run-orphan-check.job.js +339 -0
  197. package/dist/runtime/run-processors.d.ts +16 -9
  198. package/dist/runtime/run-processors.js +1 -1
  199. package/dist/runtime/run-queue.d.ts +75 -53
  200. package/dist/runtime/run-queue.js +189 -263
  201. package/dist/runtime/run-steps.d.ts +1 -2
  202. package/dist/runtime/run-steps.js +8 -11
  203. package/dist/runtime/run-worker.d.ts +26 -16
  204. package/dist/runtime/run-worker.js +132 -37
  205. package/dist/runtime/runtime.module.d.ts +2 -3
  206. package/dist/runtime/runtime.module.js +7 -6
  207. package/dist/runtime/step-log.d.ts +16 -4
  208. package/dist/runtime/step-log.js +26 -6
  209. package/dist/runtime/stream.controller.d.ts +2 -4
  210. package/dist/runtime/stream.controller.js +5 -11
  211. package/dist/runtime/subagents/child.js +1 -1
  212. package/dist/runtime/subagents/definition.d.ts +2 -1
  213. package/dist/runtime/subagents/definition.js +4 -2
  214. package/dist/runtime/subagents/deliveries.d.ts +6 -3
  215. package/dist/runtime/subagents/deliveries.js +16 -7
  216. package/dist/runtime/subagents/dispatch.d.ts +12 -12
  217. package/dist/runtime/subagents/dispatch.js +14 -38
  218. package/dist/runtime/subagents/messages.d.ts +5 -1
  219. package/dist/runtime/subagents/messages.js +11 -3
  220. package/dist/runtime/subagents/skill-fork.d.ts +3 -3
  221. package/dist/runtime/subagents/skill-fork.js +8 -5
  222. package/dist/runtime/subagents/subagents.service.js +1 -1
  223. package/dist/runtime/subagents/tree-jobs.d.ts +61 -0
  224. package/dist/runtime/subagents/tree-jobs.js +86 -0
  225. package/dist/runtime/usage-report-jobs.d.ts +23 -48
  226. package/dist/runtime/usage-report-jobs.js +67 -119
  227. package/dist/runtime/usage-report.d.ts +28 -77
  228. package/dist/runtime/usage-report.js +35 -213
  229. package/dist/runtime/waits.d.ts +48 -198
  230. package/dist/runtime/waits.js +103 -805
  231. package/dist/sandbox/jobs.d.ts +20 -10
  232. package/dist/sandbox/jobs.js +23 -17
  233. package/dist/sandbox/sandbox.service.d.ts +17 -4
  234. package/dist/sandbox/sandbox.service.js +55 -14
  235. package/dist/schedules/calendar.d.ts +134 -0
  236. package/dist/schedules/calendar.js +410 -0
  237. package/dist/schedules/fire.d.ts +26 -7
  238. package/dist/schedules/fire.js +91 -22
  239. package/dist/schedules/job-ids.d.ts +66 -17
  240. package/dist/schedules/job-ids.js +81 -13
  241. package/dist/schedules/run-tracker.d.ts +0 -5
  242. package/dist/schedules/run-tracker.js +2 -14
  243. package/dist/schedules/scan.job.d.ts +13 -32
  244. package/dist/schedules/scan.job.js +50 -164
  245. package/dist/schedules/schedule-math.d.ts +42 -0
  246. package/dist/schedules/schedule-math.js +52 -0
  247. package/dist/schedules/scheduled-tasks.service.d.ts +22 -6
  248. package/dist/schedules/scheduled-tasks.service.js +70 -14
  249. package/dist/schedules/schedules.controller.d.ts +5 -1
  250. package/dist/schedules/schedules.controller.js +15 -1
  251. package/dist/schedules/schedules.module.js +0 -4
  252. package/dist/schedules/schedules.schemas.d.ts +35 -0
  253. package/dist/schedules/schedules.schemas.js +27 -0
  254. package/dist/schedules/store.d.ts +5 -0
  255. package/dist/schedules/store.js +20 -0
  256. package/dist/skills/changes.controller.d.ts +1 -1
  257. package/dist/skills/changes.controller.js +1 -1
  258. package/dist/tools/approvals.service.d.ts +41 -11
  259. package/dist/tools/approvals.service.js +108 -14
  260. package/dist/tools/builtin-names.d.ts +5 -3
  261. package/dist/tools/builtin-names.js +12 -4
  262. package/dist/tools/builtins/builtin-tools.d.ts +1 -2
  263. package/dist/tools/builtins/builtin-tools.js +21 -17
  264. package/dist/tools/builtins/sandbox/bash.js +9 -3
  265. package/dist/tools/builtins/sandbox/common.d.ts +18 -1
  266. package/dist/tools/builtins/sandbox/common.js +14 -1
  267. package/dist/tools/builtins/sandbox/index.d.ts +2 -1
  268. package/dist/tools/builtins/sandbox/index.js +4 -1
  269. package/dist/tools/builtins/sandbox/task-list.d.ts +12 -0
  270. package/dist/tools/builtins/sandbox/task-list.js +130 -0
  271. package/dist/tools/builtins/sandbox/task-output.d.ts +8 -1
  272. package/dist/tools/builtins/sandbox/task-output.js +21 -3
  273. package/dist/tools/builtins/web/extract.d.ts +2 -4
  274. package/dist/tools/builtins/web/extract.js +2 -5
  275. package/dist/tools/pipeline.d.ts +12 -3
  276. package/dist/tools/pipeline.js +6 -1
  277. package/dist/tools/questions.service.d.ts +30 -10
  278. package/dist/tools/questions.service.js +41 -27
  279. package/dist/tools/registry.d.ts +12 -3
  280. package/dist/tools/registry.js +13 -3
  281. package/dist/tools/tools.module.js +4 -3
  282. package/package.json +3 -3
  283. package/src/migrations/0118_device_policy_ops.sql +6 -0
  284. package/src/migrations/0119_outbox.sql +22 -0
  285. package/src/migrations/0120_drop_usage_report_sweep.sql +7 -0
  286. package/src/migrations/0121_drop_platform_migration_runs.sql +12 -0
  287. package/src/migrations/0122_drop_scheduled_task_runs_rechecked_at.sql +5 -0
  288. package/src/migrations/0123_pgvector.sql +27 -0
  289. package/src/migrations/0124_conversation_source.sql +36 -0
  290. package/dist/catalog/platform-migration.d.ts +0 -115
  291. package/dist/catalog/platform-migration.js +0 -583
  292. package/dist/infra/queue/add-job-once.d.ts +0 -14
  293. package/dist/infra/queue/add-job-once.js +0 -16
@@ -0,0 +1,273 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { OnWorkerEvent, Processor, WorkerHost } from "@nestjs/bullmq";
11
+ import { Inject, Injectable, Module } from "@nestjs/common";
12
+ import { sql } from "kysely";
13
+ import { KernelDb } from "../infra/database/kernel-db.js";
14
+ import { TransactionHost } from "../infra/database/transaction-host.js";
15
+ import { Transactional } from "../infra/database/transactional.decorator.js";
16
+ import { OutboxService } from "../infra/outbox/outbox.service.js";
17
+ import { QueueModule } from "../infra/queue/queue.module.js";
18
+ import { runJob } from "../infra/queue/run-job.js";
19
+ import { KERNEL_LOGGER } from "../infra/tokens.js";
20
+ import { ExtensionsModule } from "./extensions.module.js";
21
+ import { deepFreeze, errMsg } from "./hooks.js";
22
+ import { ExtensionHost } from "./host.js";
23
+ /** Queue of lifecycle hook deliveries (no `:` in the name). */
24
+ export const LIFECYCLE_QUEUE = "cap-kernel-lifecycle";
25
+ /** The job name of every delivery (registered in `infra/outbox/registry.ts`). */
26
+ export const LIFECYCLE_JOB = "lifecycle";
27
+ /** One handler call's time limit. */
28
+ export const LIFECYCLE_HOOK_TIMEOUT_MS = 5000;
29
+ /** Concurrent deliveries per worker replica. */
30
+ const LIFECYCLE_CONCURRENCY = 4;
31
+ /**
32
+ * The deterministic job id of one delivery: the mount, the plugin and the payload's ids (no `:`;
33
+ * kernel ids and plugin names carry none). Removing the same member, agent or org again while the
34
+ * first delivery is still owed is the same job.
35
+ */
36
+ export function lifecycleJobId(mount, plugin, payload) {
37
+ const ids = Object.entries(payload)
38
+ .sort(([a], [b]) => (a < b ? -1 : 1))
39
+ .map(([, v]) => String(v));
40
+ return ["lifecycle", mount.replaceAll(".", "-"), plugin, ...ids].join("-").replaceAll(":", "-");
41
+ }
42
+ /**
43
+ * Plugins that subscribe to a lifecycle mount according to deployment state: the approved
44
+ * permission snapshot declares the mount in `permissions.hooks`, and that approved version is the
45
+ * one the deployment runs: an enabled install of that version, or a replica status row (not
46
+ * `unloaded`) that loaded or still serves it (an upgrade pending approval keeps serving the
47
+ * approved version; a config-declared plugin has only replica rows). A snapshot approved for
48
+ * another version than the one installed or served does not count. Org enablement does not count
49
+ * either: cleanup concerns data the plugin already holds (memory plugin spec §6.3). Sorted by name.
50
+ */
51
+ export async function queryLifecycleSubscribers(db, mount) {
52
+ const rows = await db
53
+ .selectFrom("extension_permission_snapshots as s")
54
+ .select("s.plugin")
55
+ .distinct()
56
+ .where(sql `s.permissions -> 'permissions' -> 'hooks' ? ${mount}`)
57
+ .where((eb) => eb.or([
58
+ eb.exists(eb
59
+ .selectFrom("extension_installs as i")
60
+ .select(sql `1`.as("one"))
61
+ .whereRef("i.name", "=", "s.plugin")
62
+ .whereRef("i.version", "=", "s.version")
63
+ .where("i.desired_state", "=", "enabled")),
64
+ eb.exists(eb
65
+ .selectFrom("extension_replica_status as r")
66
+ .select(sql `1`.as("one"))
67
+ .whereRef("r.plugin", "=", "s.plugin")
68
+ .where("r.state", "<>", "unloaded")
69
+ .where((r) => r.or([
70
+ r("r.version", "=", r.ref("s.version")),
71
+ r("r.serving_version", "=", r.ref("s.version")),
72
+ ]))),
73
+ ]))
74
+ .orderBy("s.plugin")
75
+ .execute();
76
+ return rows.map((r) => r.plugin);
77
+ }
78
+ /**
79
+ * Durable delivery of the lifecycle hooks `org.member_removed`, `agent.deleted` and `org.deleted`
80
+ * (memory plugin spec §6.3), so extensions reliably learn to delete the data they hold:
81
+ * - **Owe** ({@link recordIn}): in the transaction that removes the member, the agent or the org,
82
+ * one outbox job per subscribing plugin ({@link queryLifecycleSubscribers}) on
83
+ * {@link LIFECYCLE_QUEUE}, under {@link lifecycleJobId}, with no `org_id` (it is the deletion's
84
+ * own cleanup work, transactional outbox spec §9); a rollback owes nothing. The outbox delivers
85
+ * it to BullMQ after the commit (registry: attempts and backoff).
86
+ * - **Deliver** ({@link process}, worker role): the plugin's handlers loaded on this replica are
87
+ * called in turn (5 s each). Not loaded on this replica -> the job fails and is retried (another
88
+ * replica or a later attempt). Loaded without a handler -> dropped with a warning. Handlers get
89
+ * the plugin's platform deployment config (`call.config`), never an org's copy, so an org whose
90
+ * effective config is not valid still gets its cleanup. The last failed attempt logs an error.
91
+ * Handlers must be idempotent (a delivery may repeat when a replica dies after the call).
92
+ */
93
+ let LifecycleHooks = class LifecycleHooks {
94
+ kdb;
95
+ txHost;
96
+ outbox;
97
+ host;
98
+ log;
99
+ constructor(kdb, txHost, outbox,
100
+ /** The loaded plugins' handlers (`hooks.handlers`) and whether a plugin is active here. */
101
+ host, log) {
102
+ this.kdb = kdb;
103
+ this.txHost = txHost;
104
+ this.outbox = outbox;
105
+ this.host = host;
106
+ this.log = log;
107
+ }
108
+ /**
109
+ * Owes one delivery per subscribing plugin in the caller's transaction (`mandatory`); returns
110
+ * the plugins.
111
+ */
112
+ async recordIn(mount, payload) {
113
+ const plugins = await queryLifecycleSubscribers(this.kdb.db, mount);
114
+ for (const plugin of plugins) {
115
+ const data = { mount, plugin, payload };
116
+ await this.outbox.add({
117
+ queue: LIFECYCLE_QUEUE,
118
+ name: LIFECYCLE_JOB,
119
+ jobId: lifecycleJobId(mount, plugin, payload),
120
+ data,
121
+ });
122
+ }
123
+ return plugins;
124
+ }
125
+ /** One delivery job (the consumer {@link LifecycleHooksProcessor} calls it). */
126
+ async process(data, stop) {
127
+ const fields = { mount: data.mount, plugin: data.plugin };
128
+ const handlers = this.host.hooks
129
+ .lifecycleHandlers(data.mount)
130
+ .filter((h) => h.plugin === data.plugin)
131
+ .map((h) => h.handler);
132
+ if (handlers.length === 0) {
133
+ if (this.host.current(data.plugin) === undefined) {
134
+ // another replica or a later attempt may have it loaded
135
+ throw new Error(`plugin ${data.plugin} is not loaded on this replica`);
136
+ }
137
+ this.log.warn("lifecycle hook delivery dropped: the plugin registered no handler", fields);
138
+ return;
139
+ }
140
+ const payload = deepFreeze(structuredClone(data.payload));
141
+ for (const h of handlers)
142
+ await this.call(h, payload, stop);
143
+ }
144
+ /** One handler call, aborted after {@link LIFECYCLE_HOOK_TIMEOUT_MS} or when the replica stops. */
145
+ async call(h, payload, stop) {
146
+ const ac = new AbortController();
147
+ const onStop = () => ac.abort(new Error("replica stopping"));
148
+ if (stop.aborted)
149
+ onStop();
150
+ else
151
+ stop.addEventListener("abort", onStop, { once: true });
152
+ let timer;
153
+ try {
154
+ await Promise.race([
155
+ Promise.resolve().then(() => h(payload, ac.signal)),
156
+ new Promise((_, reject) => {
157
+ timer = setTimeout(() => {
158
+ const err = new Error(`timed out after ${LIFECYCLE_HOOK_TIMEOUT_MS} ms`);
159
+ ac.abort(err);
160
+ reject(err);
161
+ }, LIFECYCLE_HOOK_TIMEOUT_MS);
162
+ // already aborted (the replica was stopping before the call): no abort event will fire
163
+ if (ac.signal.aborted)
164
+ reject(ac.signal.reason);
165
+ else
166
+ ac.signal.addEventListener("abort", () => reject(ac.signal.reason), { once: true });
167
+ }),
168
+ ]);
169
+ }
170
+ finally {
171
+ clearTimeout(timer);
172
+ stop.removeEventListener("abort", onStop);
173
+ }
174
+ }
175
+ /** A job failed (the worker's `failed` event): a retry warns, the last attempt logs an error. */
176
+ onFailed(j, err) {
177
+ if (!j)
178
+ return;
179
+ const fields = {
180
+ jobId: j.id,
181
+ mount: j.data.mount,
182
+ plugin: j.data.plugin,
183
+ attempt: j.attemptsMade,
184
+ error: errMsg(err),
185
+ };
186
+ if (j.attemptsMade >= (j.opts.attempts ?? 1)) {
187
+ this.log.error("lifecycle hook delivery failed on its last attempt; given up", fields);
188
+ }
189
+ else
190
+ this.log.warn("lifecycle hook delivery failed; retrying", fields);
191
+ }
192
+ };
193
+ __decorate([
194
+ Transactional({ propagation: "mandatory" })
195
+ ], LifecycleHooks.prototype, "recordIn", null);
196
+ LifecycleHooks = __decorate([
197
+ Injectable(),
198
+ __param(0, Inject(KernelDb)),
199
+ __param(1, Inject(TransactionHost)),
200
+ __param(2, Inject(OutboxService)),
201
+ __param(3, Inject(ExtensionHost)),
202
+ __param(4, Inject(KERNEL_LOGGER))
203
+ ], LifecycleHooks);
204
+ export { LifecycleHooks };
205
+ /**
206
+ * The consumer of {@link LIFECYCLE_QUEUE}: {@link LifecycleHooks.process}. Worker role only
207
+ * (`LifecycleHookWorkersModule`); built with `autorun: false`, started by the worker role after the
208
+ * extensions loaded.
209
+ */
210
+ let LifecycleHooksProcessor = class LifecycleHooksProcessor extends WorkerHost {
211
+ hooks;
212
+ log;
213
+ stopping = new AbortController();
214
+ constructor(hooks, log) {
215
+ super();
216
+ this.hooks = hooks;
217
+ this.log = log;
218
+ }
219
+ process(job) {
220
+ return runJob(LIFECYCLE_QUEUE, job, () => this.hooks.process(job.data, this.stopping.signal));
221
+ }
222
+ /** Starts consuming (worker role, after the extensions loaded). */
223
+ start() {
224
+ void this.worker.run().catch((err) => this.error(err));
225
+ }
226
+ /** Aborts the calls in flight and closes the worker. */
227
+ async close() {
228
+ this.stopping.abort();
229
+ await this.worker.close();
230
+ }
231
+ failed(job, err) {
232
+ this.hooks.onFailed(job, err);
233
+ }
234
+ error(err) {
235
+ this.log.error("lifecycle hook worker error", { err });
236
+ }
237
+ };
238
+ __decorate([
239
+ OnWorkerEvent("failed")
240
+ ], LifecycleHooksProcessor.prototype, "failed", null);
241
+ __decorate([
242
+ OnWorkerEvent("error")
243
+ ], LifecycleHooksProcessor.prototype, "error", null);
244
+ LifecycleHooksProcessor = __decorate([
245
+ Injectable(),
246
+ Processor(LIFECYCLE_QUEUE, { autorun: false, concurrency: LIFECYCLE_CONCURRENCY }),
247
+ __param(0, Inject(LifecycleHooks)),
248
+ __param(1, Inject(KERNEL_LOGGER))
249
+ ], LifecycleHooksProcessor);
250
+ export { LifecycleHooksProcessor };
251
+ const LIFECYCLE_QUEUE_MODULE = QueueModule.registerQueue(LIFECYCLE_QUEUE);
252
+ /** Lifecycle hook delivery: owing the jobs, and the queue the outbox adds them to, on every role. */
253
+ let LifecycleHooksModule = class LifecycleHooksModule {
254
+ };
255
+ LifecycleHooksModule = __decorate([
256
+ Module({
257
+ imports: [ExtensionsModule, LIFECYCLE_QUEUE_MODULE],
258
+ providers: [LifecycleHooks],
259
+ exports: [LifecycleHooks, LIFECYCLE_QUEUE_MODULE],
260
+ })
261
+ ], LifecycleHooksModule);
262
+ export { LifecycleHooksModule };
263
+ /** The lifecycle queue's consumer; imported only by the worker role (`WorkerModule`). */
264
+ let LifecycleHookWorkersModule = class LifecycleHookWorkersModule {
265
+ };
266
+ LifecycleHookWorkersModule = __decorate([
267
+ Module({
268
+ imports: [LifecycleHooksModule],
269
+ providers: [LifecycleHooksProcessor],
270
+ exports: [LifecycleHooksProcessor],
271
+ })
272
+ ], LifecycleHookWorkersModule);
273
+ export { LifecycleHookWorkersModule };
@@ -7,6 +7,7 @@ import { ExtensionInstalls } from "./installs.js";
7
7
  import { PLUGINS_CHANGED_TOPIC, PLUGINS_RECONCILE_JOB, PLUGINS_RECONCILE_TOPIC, STALE_RECONCILE_PERIODS } from "./lifecycle-topics.js";
8
8
  import { PLUGINS_MIGRATED_TOPIC } from "./migrate-lock.js";
9
9
  import { ReplicaStatusStore } from "./sources.js";
10
+ import { UsageSubscribers } from "./usage-subscribers.js";
10
11
  export { PLUGINS_CHANGED_TOPIC, PLUGINS_MIGRATED_TOPIC, PLUGINS_RECONCILE_JOB, PLUGINS_RECONCILE_TOPIC, STALE_RECONCILE_PERIODS, };
11
12
  /**
12
13
  * 每副本一个:把本副本的插件加载状态收敛到 PG 期望状态(C16)。
@@ -24,6 +25,8 @@ export declare class ExtensionLifecycle {
24
25
  private readonly installs;
25
26
  private readonly config;
26
27
  private readonly log;
28
+ /** "Does any plugin subscribe to usage.report": its cached answer is cleared on PLUGINS_CHANGED. */
29
+ private readonly usageSubscribers;
27
30
  private running;
28
31
  private queued;
29
32
  private unsubs;
@@ -32,7 +35,9 @@ export declare class ExtensionLifecycle {
32
35
  private stopped;
33
36
  constructor(host: ExtensionHost, bus: Pick<ExtensionContext["bus"], "publish" | "subscribe">, status: ReplicaStatusStore,
34
37
  /** Every install record's name (any desired state): prunes status rows this replica no longer knows. */
35
- installs: ExtensionInstalls, config: KernelConfigService, log: KernelLogger);
38
+ installs: ExtensionInstalls, config: KernelConfigService, log: KernelLogger,
39
+ /** "Does any plugin subscribe to usage.report": its cached answer is cleared on PLUGINS_CHANGED. */
40
+ usageSubscribers: Pick<UsageSubscribers, "invalidate">);
36
41
  /** 订阅三个主题(订阅失败只记 error:周期任务的消息同样收不到时,下次启动再来)。 */
37
42
  start(): Promise<void>;
38
43
  /**
@@ -17,6 +17,7 @@ import { ExtensionInstalls } from "./installs.js";
17
17
  import { PLUGIN_DEPLOYMENT_CONFIG_TOPIC, PLUGINS_CHANGED_TOPIC, PLUGINS_RECONCILE_JOB, PLUGINS_RECONCILE_TOPIC, STALE_RECONCILE_PERIODS, } from "./lifecycle-topics.js";
18
18
  import { PLUGINS_MIGRATED_TOPIC } from "./migrate-lock.js";
19
19
  import { ReplicaStatusStore } from "./sources.js";
20
+ import { UsageSubscribers } from "./usage-subscribers.js";
20
21
  export { PLUGINS_CHANGED_TOPIC, PLUGINS_MIGRATED_TOPIC, PLUGINS_RECONCILE_JOB, PLUGINS_RECONCILE_TOPIC, STALE_RECONCILE_PERIODS, };
21
22
  /** 心跳超过这么多个周期的行直接删除(副本早已退出;读的一方本来就不计入)。 */
22
23
  const PRUNE_RECONCILE_PERIODS = 10;
@@ -35,6 +36,7 @@ let ExtensionLifecycle = class ExtensionLifecycle {
35
36
  installs;
36
37
  config;
37
38
  log;
39
+ usageSubscribers;
38
40
  running;
39
41
  queued;
40
42
  unsubs = [];
@@ -43,13 +45,16 @@ let ExtensionLifecycle = class ExtensionLifecycle {
43
45
  stopped = false;
44
46
  constructor(host, bus, status,
45
47
  /** Every install record's name (any desired state): prunes status rows this replica no longer knows. */
46
- installs, config, log) {
48
+ installs, config, log,
49
+ /** "Does any plugin subscribe to usage.report": its cached answer is cleared on PLUGINS_CHANGED. */
50
+ usageSubscribers) {
47
51
  this.host = host;
48
52
  this.bus = bus;
49
53
  this.status = status;
50
54
  this.installs = installs;
51
55
  this.config = config;
52
56
  this.log = log;
57
+ this.usageSubscribers = usageSubscribers;
53
58
  }
54
59
  /** 订阅三个主题(订阅失败只记 error:周期任务的消息同样收不到时,下次启动再来)。 */
55
60
  async start() {
@@ -65,6 +70,7 @@ let ExtensionLifecycle = class ExtensionLifecycle {
65
70
  await on(PLUGINS_CHANGED_TOPIC, (p) => {
66
71
  // 安装 / 卸载改变了「部署已知」的插件名:路由闸门不等 TTL
67
72
  this.host.invalidateKnownNames();
73
+ this.usageSubscribers.invalidate();
68
74
  this.trigger(`changed:${pluginOf(p)}`);
69
75
  });
70
76
  await on(PLUGINS_RECONCILE_TOPIC, () => this.trigger("periodic"));
@@ -176,7 +182,8 @@ ExtensionLifecycle = __decorate([
176
182
  __param(2, Inject(ReplicaStatusStore)),
177
183
  __param(3, Inject(ExtensionInstalls)),
178
184
  __param(4, Inject(KernelConfigService)),
179
- __param(5, Inject(KERNEL_LOGGER))
185
+ __param(5, Inject(KERNEL_LOGGER)),
186
+ __param(6, Inject(UsageSubscribers))
180
187
  ], ExtensionLifecycle);
181
188
  export { ExtensionLifecycle };
182
189
  const pluginOf = (p) => typeof p === "object" && p !== null && typeof p.plugin === "string"
@@ -110,6 +110,16 @@ export interface OrgExtensionPutOptions {
110
110
  */
111
111
  validateOnly?: boolean;
112
112
  }
113
+ /**
114
+ * An org's stored config of a plugin no longer matches its org schema (e.g. after a plugin upgrade):
115
+ * a known state, not a read error. The message names the plugin, the org and the failing paths,
116
+ * never the values.
117
+ */
118
+ export declare class OrgConfigInvalidError extends Error {
119
+ readonly plugin: string;
120
+ readonly orgId: string;
121
+ constructor(plugin: string, orgId: string, detail: string);
122
+ }
113
123
  /**
114
124
  * Org-level extension config: enablement and the `config.org` config stored per org.
115
125
  * The non-secret part is stored in `capybara.extension_org_configs.config`; each secret field is
@@ -210,7 +220,9 @@ export declare class OrgExtensionConfigs {
210
220
  * A required bootstrap plugin is enabled without a row (its stored config, or `{}`, is validated).
211
221
  * 无记录 → undefined;插件无 `config.org` 或已停用(`enabled=false`,不校验、不读密钥)时 `config` 为 null。
212
222
  * 配置行与密钥在同一个 `REPEATABLE READ` 只读事务里读取。
213
- * 已存配置不再满足 schema(如插件升级后)→ 抛 Error,信息含插件名、组织 ID 与出错路径(不含值)。
223
+ * A stored config that no longer matches the schema (e.g. after a plugin upgrade) throws
224
+ * {@link OrgConfigInvalidError} (plugin, org ID and the failing paths, no values); other errors are
225
+ * read errors.
214
226
  */
215
227
  get(orgId: string, plugin: string): Promise<{
216
228
  enabled: boolean;
@@ -88,6 +88,21 @@ function secretPaths(schema) {
88
88
  .filter((f) => f.meta?.secret)
89
89
  .map((f) => f.path);
90
90
  }
91
+ /**
92
+ * An org's stored config of a plugin no longer matches its org schema (e.g. after a plugin upgrade):
93
+ * a known state, not a read error. The message names the plugin, the org and the failing paths,
94
+ * never the values.
95
+ */
96
+ export class OrgConfigInvalidError extends Error {
97
+ plugin;
98
+ orgId;
99
+ constructor(plugin, orgId, detail) {
100
+ super(`plugin ${plugin}: stored organization config for org ${orgId} is invalid: ${detail}`);
101
+ this.plugin = plugin;
102
+ this.orgId = orgId;
103
+ this.name = "OrgConfigInvalidError";
104
+ }
105
+ }
91
106
  /**
92
107
  * Org-level extension config: enablement and the `config.org` config stored per org.
93
108
  * The non-secret part is stored in `capybara.extension_org_configs.config`; each secret field is
@@ -444,7 +459,9 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
444
459
  * A required bootstrap plugin is enabled without a row (its stored config, or `{}`, is validated).
445
460
  * 无记录 → undefined;插件无 `config.org` 或已停用(`enabled=false`,不校验、不读密钥)时 `config` 为 null。
446
461
  * 配置行与密钥在同一个 `REPEATABLE READ` 只读事务里读取。
447
- * 已存配置不再满足 schema(如插件升级后)→ 抛 Error,信息含插件名、组织 ID 与出错路径(不含值)。
462
+ * A stored config that no longer matches the schema (e.g. after a plugin upgrade) throws
463
+ * {@link OrgConfigInvalidError} (plugin, org ID and the failing paths, no values); other errors are
464
+ * read errors.
448
465
  */
449
466
  async get(orgId, plugin) {
450
467
  const manifest = this.manifest(plugin);
@@ -488,7 +505,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
488
505
  const detail = toIssues(r.error)
489
506
  .map((i) => `${i.path || "<root>"}: ${i.message}`)
490
507
  .join("; ");
491
- throw new Error(`plugin ${plugin}: stored organization config for org ${orgId} is invalid: ${detail}`);
508
+ throw new OrgConfigInvalidError(plugin, orgId, detail);
492
509
  }
493
510
  return { enabled: true, config: r.data };
494
511
  }
@@ -30,7 +30,7 @@ export interface PermissionGroup {
30
30
  * ({@link SENSITIVE_EXTENSION_POINTS}; the code is the extension point name without `@<major>`, with
31
31
  * `-` replaced by `_`).
32
32
  */
33
- export type SensitiveReason = "secrets" | "public_routes" | "tools" | "hooks" | "ui_embed" | "client_code" | "ui_component" | "channel" | "directory" | "identity_source" | "sandbox" | "object_store" | "migration_warnings";
33
+ export type SensitiveReason = "secrets" | "public_routes" | "tools" | "hooks" | "ui_embed" | "client_code" | "ui_component" | "channel" | "directory" | "identity_source" | "sandbox" | "object_store" | "context" | "migration_warnings";
34
34
  /** 权限清单里的一项(分组里的一项,或 `added` 里的一个权限键)。 */
35
35
  export interface PermissionItem {
36
36
  value: string;
@@ -39,10 +39,14 @@ export interface PermissionItem {
39
39
  reason?: SensitiveReason;
40
40
  }
41
41
  /**
42
- * 敏感的能力(D10 + 计划扩展,F-AI):`sandbox@2` 在部署里执行任意命令、`object-store@1` 保管全部附件与插件包(D10);
43
- * `channel@1` 收到组织在 IM 里的对话内容(Ruling A33)、`directory@1` 能把人加进组织(并为其建账号、建身份)、
44
- * `identity-source@1` 决定谁能登录成哪个账号。插件提供其中任何一个时,「提供」一组标为敏感,
45
- * 后台插件列表的 `sensitive` 为真,须超级管理员批准。只影响展示,不改权限快照的键。
42
+ * Sensitive capabilities (D10 + planned extensions, F-AI): `sandbox@2` runs arbitrary commands in
43
+ * the deployment, `object-store@1` holds every attachment and plugin package (D10); `channel@1`
44
+ * receives the org's IM conversations (Ruling A33), `directory@1` can add people to orgs (creating
45
+ * their accounts and identities), `identity-source@1` decides who can sign in as which account;
46
+ * `context@1` adds text to the model's context in conversations of orgs that enable it (memory
47
+ * plugin spec §6.1). A plugin providing any of them has its "provides" group marked sensitive and
48
+ * `sensitive` true in the admin plugin list, for the super admin to approve. Display only: the keys
49
+ * of the permission snapshot do not change.
46
50
  */
47
51
  export declare const SENSITIVE_EXTENSION_POINTS: readonly string[];
48
52
  /**
@@ -18,10 +18,14 @@ export function reviewHash(set, warnings = []) {
18
18
  return h.digest("hex").slice(0, 16);
19
19
  }
20
20
  /**
21
- * 敏感的能力(D10 + 计划扩展,F-AI):`sandbox@2` 在部署里执行任意命令、`object-store@1` 保管全部附件与插件包(D10);
22
- * `channel@1` 收到组织在 IM 里的对话内容(Ruling A33)、`directory@1` 能把人加进组织(并为其建账号、建身份)、
23
- * `identity-source@1` 决定谁能登录成哪个账号。插件提供其中任何一个时,「提供」一组标为敏感,
24
- * 后台插件列表的 `sensitive` 为真,须超级管理员批准。只影响展示,不改权限快照的键。
21
+ * Sensitive capabilities (D10 + planned extensions, F-AI): `sandbox@2` runs arbitrary commands in
22
+ * the deployment, `object-store@1` holds every attachment and plugin package (D10); `channel@1`
23
+ * receives the org's IM conversations (Ruling A33), `directory@1` can add people to orgs (creating
24
+ * their accounts and identities), `identity-source@1` decides who can sign in as which account;
25
+ * `context@1` adds text to the model's context in conversations of orgs that enable it (memory
26
+ * plugin spec §6.1). A plugin providing any of them has its "provides" group marked sensitive and
27
+ * `sensitive` true in the admin plugin list, for the super admin to approve. Display only: the keys
28
+ * of the permission snapshot do not change.
25
29
  */
26
30
  export const SENSITIVE_EXTENSION_POINTS = [
27
31
  "channel@1",
@@ -29,6 +33,7 @@ export const SENSITIVE_EXTENSION_POINTS = [
29
33
  "identity-source@1",
30
34
  "sandbox@2",
31
35
  "object-store@1",
36
+ "context@1",
32
37
  ];
33
38
  /** The one item of the "client" group (client extensions spec §5.4). */
34
39
  const CLIENT_ITEM = "client code";
@@ -18,6 +18,16 @@ export type PointResolution<T> = {
18
18
  ok: false;
19
19
  reason: "none" | "ambiguous";
20
20
  };
21
+ /**
22
+ * One provider of a multi-provider extension point for an org ({@link ExtensionPointDefaults.providersFor}),
23
+ * with its org config and its effective deployment config for the org, both to pass back to it.
24
+ */
25
+ export interface PointProvider<T> {
26
+ extension: string;
27
+ impl: T;
28
+ orgConfig: unknown;
29
+ config: unknown;
30
+ }
21
31
  /** One `org-default` extension point of an org (the admin view). */
22
32
  export interface OrgPointDefaultView {
23
33
  point: string;
@@ -50,6 +60,19 @@ export declare class ExtensionPointDefaults {
50
60
  * point has no `selection`.
51
61
  */
52
62
  resolve<T>(orgId: string, point: string): Promise<PointResolution<T>>;
63
+ /**
64
+ * Every provider of `point` (the name without the major, e.g. `context`) that could serve
65
+ * `orgId` now, in extension-name order: the same candidates {@link resolve} chooses from (loaded
66
+ * here, enabled in the org, valid org and effective config, `configured()` not false), for
67
+ * points whose every enabled provider is used (no `selection`). `failed`: the providers enabled
68
+ * in the org whose status could not be read (reading a config or `configured()` threw, e.g. a
69
+ * transient database error); they are neither serving nor known to be off. An invalid stored
70
+ * org config ({@link OrgConfigInvalidError}) is not a read error: that provider is off.
71
+ */
72
+ providersFor<T>(orgId: string, point: string): Promise<{
73
+ providers: PointProvider<T>[];
74
+ failed: Set<string>;
75
+ }>;
53
76
  /** Every `org-default` point with its candidates, stored default and effective provider. */
54
77
  list(orgId: string): Promise<OrgPointDefaultView[]>;
55
78
  /** Whether each `org-default` point has a provider for the org now (no extension names). */
@@ -65,7 +88,11 @@ export declare class ExtensionPointDefaults {
65
88
  setDefault(orgId: string, point: string, extension: string, actorId: string,
66
89
  /** Merged into the audit detail (a write made through the Platform tool). */
67
90
  auditDetail?: Record<string, unknown>): Promise<void>;
68
- /** The candidates of `point` in `orgId`, by extension name. */
91
+ /**
92
+ * The candidates of `point` in `orgId`, by extension name, and the enabled providers whose status
93
+ * could not be read (a read or `configured()` threw: excluded, logged). A provider whose stored
94
+ * org config or effective config is invalid is excluded but not `failed`: it is known to be off.
95
+ */
69
96
  private candidates;
70
97
  private choose;
71
98
  /** The org's stored defaults, point → extension. */
@@ -17,7 +17,7 @@ import { Transactional } from "../infra/database/transactional.decorator.js";
17
17
  import { KERNEL_LOGGER } from "../infra/tokens.js";
18
18
  import { EffectiveConfigs } from "./effective-config.js";
19
19
  import { enabledExtensionsQuery, isExtensionEnabled } from "./enabled.js";
20
- import { OrgExtensionConfigs } from "./org-config.js";
20
+ import { OrgConfigInvalidError, OrgExtensionConfigs } from "./org-config.js";
21
21
  import { ExtensionRegistry } from "./registry.js";
22
22
  /** The extension points whose provider each org chooses (`selection: "org-default"`). */
23
23
  function selectionPoints() {
@@ -58,7 +58,7 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
58
58
  if (!hasSelection(point)) {
59
59
  throw new Error(`extension point ${point} has no org-default selection`);
60
60
  }
61
- const candidates = await this.candidates(orgId, point);
61
+ const { candidates } = await this.candidates(orgId, point);
62
62
  const chosen = await this.choose(orgId, point, candidates);
63
63
  if (!chosen.ok)
64
64
  return chosen;
@@ -71,12 +71,33 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
71
71
  config: c.config,
72
72
  };
73
73
  }
74
+ /**
75
+ * Every provider of `point` (the name without the major, e.g. `context`) that could serve
76
+ * `orgId` now, in extension-name order: the same candidates {@link resolve} chooses from (loaded
77
+ * here, enabled in the org, valid org and effective config, `configured()` not false), for
78
+ * points whose every enabled provider is used (no `selection`). `failed`: the providers enabled
79
+ * in the org whose status could not be read (reading a config or `configured()` threw, e.g. a
80
+ * transient database error); they are neither serving nor known to be off. An invalid stored
81
+ * org config ({@link OrgConfigInvalidError}) is not a read error: that provider is off.
82
+ */
83
+ async providersFor(orgId, point) {
84
+ const { candidates, failed } = await this.candidates(orgId, point);
85
+ return {
86
+ providers: candidates.map((c) => ({
87
+ extension: c.extension,
88
+ impl: c.impl,
89
+ orgConfig: c.orgConfig,
90
+ config: c.config,
91
+ })),
92
+ failed,
93
+ };
94
+ }
74
95
  /** Every `org-default` point with its candidates, stored default and effective provider. */
75
96
  async list(orgId) {
76
97
  const stored = await this.storedDefaults(orgId);
77
98
  const out = [];
78
99
  for (const point of selectionPoints()) {
79
- const candidates = await this.candidates(orgId, point);
100
+ const { candidates } = await this.candidates(orgId, point);
80
101
  const chosen = pick(candidates, stored.get(point));
81
102
  out.push({
82
103
  point,
@@ -128,7 +149,11 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
128
149
  detail: { point, extension, ...auditDetail },
129
150
  });
130
151
  }
131
- /** The candidates of `point` in `orgId`, by extension name. */
152
+ /**
153
+ * The candidates of `point` in `orgId`, by extension name, and the enabled providers whose status
154
+ * could not be read (a read or `configured()` threw: excluded, logged). A provider whose stored
155
+ * org config or effective config is invalid is excluded but not `failed`: it is known to be off.
156
+ */
132
157
  async candidates(orgId, point) {
133
158
  const db = this.kdb.db;
134
159
  const enabled = new Set((await db
@@ -141,18 +166,21 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
141
166
  providers.set(e.plugin, e.impl);
142
167
  }
143
168
  const out = [];
169
+ const failed = new Set();
144
170
  for (const [extension, impl] of providers) {
145
171
  let cfg;
146
172
  try {
147
173
  cfg = await this.orgConfigs.get(orgId, extension);
148
174
  }
149
175
  catch (err) {
150
- this.log.warn("org default provider: provider excluded, its org config is invalid", {
151
- orgId,
152
- point,
153
- extension,
154
- err,
155
- });
176
+ // a stored org config that no longer validates is a known state: the provider is off
177
+ // (as for an invalid effective config below); only a read that failed is `failed`
178
+ const invalid = err instanceof OrgConfigInvalidError;
179
+ this.log.warn(invalid
180
+ ? "extension point provider excluded, its stored org config is invalid"
181
+ : "extension point provider excluded, cannot read its org config", { orgId, point, extension, err });
182
+ if (!invalid)
183
+ failed.add(extension);
156
184
  continue;
157
185
  }
158
186
  if (!cfg?.enabled)
@@ -163,12 +191,13 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
163
191
  effective = await this.effective.get(orgId, extension);
164
192
  }
165
193
  catch (err) {
166
- this.log.warn("org default provider: provider excluded, cannot read its effective config", {
194
+ this.log.warn("extension point provider excluded, cannot read its effective config", {
167
195
  orgId,
168
196
  point,
169
197
  extension,
170
198
  err,
171
199
  });
200
+ failed.add(extension);
172
201
  continue;
173
202
  }
174
203
  if (!effective.ok)
@@ -181,12 +210,13 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
181
210
  ok = configured.call(impl, cfg.config, effective.config) !== false;
182
211
  }
183
212
  catch (err) {
184
- this.log.warn("org default provider: provider excluded, configured() threw", {
213
+ this.log.warn("extension point provider excluded, configured() threw", {
185
214
  orgId,
186
215
  point,
187
216
  extension,
188
217
  err,
189
218
  });
219
+ failed.add(extension);
190
220
  continue;
191
221
  }
192
222
  if (!ok)
@@ -194,7 +224,8 @@ let ExtensionPointDefaults = class ExtensionPointDefaults {
194
224
  }
195
225
  out.push({ extension, impl, orgConfig: cfg.config, config: effective.config });
196
226
  }
197
- return out.sort((a, b) => (a.extension < b.extension ? -1 : a.extension > b.extension ? 1 : 0));
227
+ out.sort((a, b) => (a.extension < b.extension ? -1 : a.extension > b.extension ? 1 : 0));
228
+ return { candidates: out, failed };
198
229
  }
199
230
  async choose(orgId, point, candidates) {
200
231
  if (candidates.length < 2)