@capybara-hq/kernel 0.1.0-rc.36 → 0.1.0-rc.38

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 (351) hide show
  1. package/bootstrap.json +5 -5
  2. package/dist/admin/admin.module.d.ts +3 -2
  3. package/dist/admin/admin.module.js +9 -5
  4. package/dist/admin/audit-log.service.d.ts +51 -0
  5. package/dist/admin/{platform-audit.service.js → audit-log.service.js} +20 -12
  6. package/dist/admin/audit.controller.d.ts +18 -6
  7. package/dist/admin/audit.controller.js +36 -6
  8. package/dist/admin/audit.schemas.d.ts +9 -1
  9. package/dist/admin/audit.schemas.js +22 -2
  10. package/dist/admin/org-network.controller.d.ts +30 -0
  11. package/dist/admin/org-network.controller.js +77 -0
  12. package/dist/admin/org-network.schemas.d.ts +16 -0
  13. package/dist/admin/org-network.schemas.js +27 -0
  14. package/dist/admin/org-network.service.d.ts +41 -0
  15. package/dist/admin/org-network.service.js +101 -0
  16. package/dist/admin/outbox-dead.controller.d.ts +4 -6
  17. package/dist/admin/outbox-dead.controller.js +9 -6
  18. package/dist/admin/outbox-dead.schemas.d.ts +16 -0
  19. package/dist/admin/outbox-dead.schemas.js +25 -1
  20. package/dist/admin/outbox-dead.service.d.ts +16 -7
  21. package/dist/admin/outbox-dead.service.js +34 -10
  22. package/dist/admin/overview.service.d.ts +3 -3
  23. package/dist/admin/overview.service.js +0 -1
  24. package/dist/agents/agent-lookup.js +2 -10
  25. package/dist/agents/agents.service.js +9 -20
  26. package/dist/agents/definition.d.ts +3 -51
  27. package/dist/agents/definition.js +4 -254
  28. package/dist/agents/org-templates.js +2 -6
  29. package/dist/agents/readiness.controller.d.ts +4 -12
  30. package/dist/agents/readiness.controller.js +4 -12
  31. package/dist/agents/readiness.service.d.ts +23 -11
  32. package/dist/agents/readiness.service.js +31 -14
  33. package/dist/agents/templates.d.ts +0 -6
  34. package/dist/agents/templates.js +8 -34
  35. package/dist/background/store.d.ts +2 -0
  36. package/dist/background/store.js +3 -1
  37. package/dist/background/wake-rules.js +2 -1
  38. package/dist/catalog/admin-catalog.controller.d.ts +1 -2
  39. package/dist/catalog/admin-catalog.controller.js +1 -2
  40. package/dist/catalog/appearance.service.js +1 -3
  41. package/dist/catalog/catalog-content.d.ts +1 -1
  42. package/dist/catalog/catalog-content.js +1 -4
  43. package/dist/catalog/catalog-items.js +3 -3
  44. package/dist/catalog/catalog-scope.service.d.ts +10 -28
  45. package/dist/catalog/catalog-scope.service.js +22 -73
  46. package/dist/catalog/cli.d.ts +0 -11
  47. package/dist/catalog/cli.js +0 -20
  48. package/dist/catalog/component-scope.service.js +0 -2
  49. package/dist/catalog/component-state.js +2 -2
  50. package/dist/catalog/install-status.d.ts +6 -0
  51. package/dist/catalog/install-status.js +6 -0
  52. package/dist/catalog/job-ids.d.ts +0 -4
  53. package/dist/catalog/job-ids.js +0 -4
  54. package/dist/catalog/marketplaces.service.d.ts +1 -1
  55. package/dist/catalog/org-catalog.service.d.ts +2 -0
  56. package/dist/catalog/org-catalog.service.js +8 -3
  57. package/dist/catalog/org-extension-switch.d.ts +7 -2
  58. package/dist/catalog/org-extension-switch.js +56 -9
  59. package/dist/catalog/org-plugin-uploads.d.ts +6 -15
  60. package/dist/catalog/org-plugin-uploads.js +9 -27
  61. package/dist/catalog/owned-items.js +1 -3
  62. package/dist/catalog/platform-import-update.js +1 -1
  63. package/dist/catalog/plugin-import.d.ts +15 -13
  64. package/dist/catalog/plugin-import.js +21 -30
  65. package/dist/catalog/plugin-install.d.ts +3 -43
  66. package/dist/catalog/plugin-install.js +8 -150
  67. package/dist/catalog/plugin-package.d.ts +5 -10
  68. package/dist/catalog/plugin-package.js +12 -33
  69. package/dist/catalog/plugin-upgrade.js +5 -4
  70. package/dist/catalog/scope-jobs.d.ts +34 -34
  71. package/dist/catalog/scope-jobs.js +50 -37
  72. package/dist/catalog/scope-orgs.d.ts +1 -1
  73. package/dist/catalog/scope-orgs.js +1 -2
  74. package/dist/catalog/scope-sync-workers.d.ts +4 -3
  75. package/dist/catalog/scope-sync-workers.js +18 -6
  76. package/dist/catalog/scope-sync.d.ts +56 -59
  77. package/dist/catalog/scope-sync.js +96 -166
  78. package/dist/catalog/unshare.d.ts +3 -4
  79. package/dist/catalog/unshare.js +5 -8
  80. package/dist/channels/job-ids.d.ts +2 -6
  81. package/dist/channels/job-ids.js +2 -6
  82. package/dist/channels/outbound.d.ts +1 -1
  83. package/dist/channels/outbound.js +1 -1
  84. package/dist/channels/supervisor.d.ts +2 -3
  85. package/dist/channels/supervisor.js +2 -3
  86. package/dist/client/client-access.service.js +1 -1
  87. package/dist/config/env.js +19 -1
  88. package/dist/config/load.d.ts +0 -6
  89. package/dist/config/load.js +8 -27
  90. package/dist/config/schema.d.ts +54 -47
  91. package/dist/config/schema.js +198 -42
  92. package/dist/conversations/attachments.service.d.ts +10 -3
  93. package/dist/conversations/attachments.service.js +49 -16
  94. package/dist/conversations/conversations.controller.d.ts +0 -39
  95. package/dist/conversations/conversations.controller.js +0 -39
  96. package/dist/conversations/event-log.js +4 -1
  97. package/dist/conversations/events.d.ts +71 -46
  98. package/dist/conversations/events.js +124 -112
  99. package/dist/conversations/media-types.d.ts +25 -0
  100. package/dist/conversations/media-types.js +38 -0
  101. package/dist/conversations/queue.d.ts +3 -1
  102. package/dist/conversations/queue.service.js +9 -2
  103. package/dist/conversations/titles.js +6 -0
  104. package/dist/ctx/create-ctx.d.ts +8 -1
  105. package/dist/ctx/create-ctx.js +13 -3
  106. package/dist/ctx/net.d.ts +50 -0
  107. package/dist/ctx/net.js +67 -0
  108. package/dist/ctx/secrets.d.ts +24 -7
  109. package/dist/ctx/secrets.js +97 -30
  110. package/dist/db/kernel-schema.d.ts +27 -6
  111. package/dist/devices/devices.service.js +2 -1
  112. package/dist/errors.d.ts +10 -2
  113. package/dist/errors.js +10 -2
  114. package/dist/evals/reconstruct.d.ts +4 -17
  115. package/dist/evals/reconstruct.js +13 -53
  116. package/dist/evals/recorded-tools.d.ts +0 -27
  117. package/dist/evals/recorded-tools.js +2 -92
  118. package/dist/evals/run-replay.d.ts +1 -6
  119. package/dist/evals/run-replay.js +10 -17
  120. package/dist/evals/sets.service.d.ts +17 -5
  121. package/dist/evals/sets.service.js +33 -12
  122. package/dist/evals/snapshot.d.ts +1 -2
  123. package/dist/evals/snapshot.js +4 -17
  124. package/dist/extensions/admin-ops.d.ts +6 -1
  125. package/dist/extensions/admin-ops.js +17 -4
  126. package/dist/extensions/admin-view.d.ts +7 -4
  127. package/dist/extensions/admin-view.js +27 -21
  128. package/dist/extensions/bootstrap.js +0 -1
  129. package/dist/extensions/cli.js +0 -1
  130. package/dist/extensions/extensions.module.js +3 -0
  131. package/dist/extensions/host.d.ts +9 -4
  132. package/dist/extensions/host.js +14 -8
  133. package/dist/extensions/installs.d.ts +1 -3
  134. package/dist/extensions/instance.d.ts +0 -2
  135. package/dist/extensions/instance.js +0 -3
  136. package/dist/extensions/org-config.d.ts +4 -4
  137. package/dist/extensions/org-config.js +13 -22
  138. package/dist/extensions/package.d.ts +6 -6
  139. package/dist/extensions/package.js +3 -7
  140. package/dist/extensions/permission-view.d.ts +2 -4
  141. package/dist/extensions/plugin-items.d.ts +21 -1
  142. package/dist/extensions/plugin-items.js +28 -0
  143. package/dist/extensions/sync.js +5 -10
  144. package/dist/extensions/unavailable.js +0 -1
  145. package/dist/http/api.module.js +4 -1
  146. package/dist/identity/audit.d.ts +0 -12
  147. package/dist/identity/audit.js +0 -27
  148. package/dist/identity/auth.controller.js +5 -9
  149. package/dist/identity/cookies.d.ts +4 -9
  150. package/dist/identity/cookies.js +8 -32
  151. package/dist/identity/initial-passwords.d.ts +1 -1
  152. package/dist/identity/initial-passwords.js +1 -2
  153. package/dist/identity/sessions.d.ts +0 -6
  154. package/dist/identity/sessions.js +0 -18
  155. package/dist/inbox/platform-inbox.service.d.ts +33 -1
  156. package/dist/inbox/platform-inbox.service.js +56 -6
  157. package/dist/index.d.ts +4 -2
  158. package/dist/index.js +4 -2
  159. package/dist/infra/cli/master-key-rewrap.d.ts +15 -0
  160. package/dist/infra/cli/master-key-rewrap.js +24 -0
  161. package/dist/infra/config/kernel-config.service.js +0 -17
  162. package/dist/infra/database/transaction-host.d.ts +32 -1
  163. package/dist/infra/database/transaction-host.js +30 -4
  164. package/dist/infra/database/xact-lock.d.ts +16 -0
  165. package/dist/infra/database/xact-lock.js +28 -1
  166. package/dist/infra/health/health.controller.js +8 -0
  167. package/dist/infra/health/process-fault.d.ts +16 -0
  168. package/dist/infra/health/process-fault.js +49 -0
  169. package/dist/infra/lifecycle/process-guards.d.ts +8 -2
  170. package/dist/infra/lifecycle/process-guards.js +12 -3
  171. package/dist/infra/lifecycle/shutdown-signals.d.ts +10 -1
  172. package/dist/infra/lifecycle/shutdown-signals.js +28 -13
  173. package/dist/infra/outbox/outbox.service.d.ts +12 -9
  174. package/dist/infra/outbox/outbox.service.js +22 -26
  175. package/dist/infra/outbox/registry.d.ts +15 -7
  176. package/dist/infra/outbox/registry.js +39 -23
  177. package/dist/lifecycle/kernel-lifecycle.js +1 -7
  178. package/dist/models/enablement.d.ts +5 -6
  179. package/dist/models/enablement.js +7 -14
  180. package/dist/net/public-address.d.ts +21 -1
  181. package/dist/net/public-address.js +56 -10
  182. package/dist/net/safe-fetch.d.ts +4 -1
  183. package/dist/net/safe-fetch.js +15 -4
  184. package/dist/orgs/directory-sync.js +2 -0
  185. package/dist/orgs/members.service.js +5 -0
  186. package/dist/orgs/network-allowances.service.d.ts +79 -0
  187. package/dist/orgs/network-allowances.service.js +371 -0
  188. package/dist/orgs/offboarding.d.ts +3 -1
  189. package/dist/orgs/offboarding.js +9 -2
  190. package/dist/orgs/org-lock.d.ts +28 -2
  191. package/dist/orgs/org-lock.js +59 -4
  192. package/dist/orgs/orgs.controller.d.ts +11 -2
  193. package/dist/orgs/orgs.controller.js +19 -3
  194. package/dist/orgs/orgs.module.d.ts +3 -2
  195. package/dist/orgs/orgs.module.js +6 -2
  196. package/dist/orgs/orgs.schemas.d.ts +12 -0
  197. package/dist/orgs/orgs.schemas.js +5 -0
  198. package/dist/orgs/orgs.service.js +6 -1
  199. package/dist/orgs/settings.controller.d.ts +3 -2
  200. package/dist/orgs/settings.service.d.ts +59 -14
  201. package/dist/orgs/settings.service.js +102 -20
  202. package/dist/overrides.d.ts +7 -7
  203. package/dist/overrides.js +4 -4
  204. package/dist/packages/tgz.d.ts +0 -2
  205. package/dist/packages/tgz.js +0 -2
  206. package/dist/platform/exclusions.js +0 -5
  207. package/dist/platform/operation.js +1 -1
  208. package/dist/platform/ops/admin.ops.d.ts +2 -2
  209. package/dist/platform/ops/admin.ops.js +9 -6
  210. package/dist/platform/ops/agents.ops.js +1 -1
  211. package/dist/platform/ops/connectors.ops.js +46 -1
  212. package/dist/platform/ops/evals.ops.js +1 -64
  213. package/dist/platform/ops/extensions-admin.ops.js +1 -1
  214. package/dist/platform/ops/inbox.ops.js +16 -3
  215. package/dist/platform/ops/org-plugins.ops.js +1 -1
  216. package/dist/platform/ops/orgs.ops.d.ts +11 -3
  217. package/dist/platform/ops/orgs.ops.js +139 -5
  218. package/dist/platform/ops/outbox.ops.js +5 -5
  219. package/dist/platform/route-schemas.js +3 -0
  220. package/dist/roles/worker-role.d.ts +2 -3
  221. package/dist/roles/worker-role.js +3 -13
  222. package/dist/runtime/checkpointer.d.ts +3 -0
  223. package/dist/runtime/checkpointer.js +7 -4
  224. package/dist/runtime/context/build.d.ts +56 -18
  225. package/dist/runtime/context/build.js +143 -50
  226. package/dist/runtime/context/compaction.d.ts +1 -3
  227. package/dist/runtime/context/compaction.js +42 -8
  228. package/dist/runtime/context/run-context.js +1 -2
  229. package/dist/runtime/context/skills.d.ts +27 -56
  230. package/dist/runtime/context/skills.js +42 -114
  231. package/dist/runtime/context/tool-changes.d.ts +3 -3
  232. package/dist/runtime/context/tool-changes.js +6 -16
  233. package/dist/runtime/hook-calls.d.ts +6 -6
  234. package/dist/runtime/hook-calls.js +7 -7
  235. package/dist/runtime/langgraph-loop.js +9 -3
  236. package/dist/runtime/maintenance.d.ts +3 -11
  237. package/dist/runtime/maintenance.js +5 -20
  238. package/dist/runtime/model-call.d.ts +5 -0
  239. package/dist/runtime/model-call.js +48 -36
  240. package/dist/runtime/providers.d.ts +33 -35
  241. package/dist/runtime/providers.js +64 -109
  242. package/dist/runtime/run-context.service.js +7 -22
  243. package/dist/runtime/run-deletion.d.ts +6 -0
  244. package/dist/runtime/run-deletion.js +39 -16
  245. package/dist/runtime/run-execution.d.ts +56 -3
  246. package/dist/runtime/run-execution.js +81 -11
  247. package/dist/runtime/run-finish.js +2 -1
  248. package/dist/runtime/run-liveness.d.ts +11 -0
  249. package/dist/runtime/run-liveness.js +19 -0
  250. package/dist/runtime/run-orphan-check.job.js +2 -1
  251. package/dist/runtime/run-queue.js +8 -16
  252. package/dist/runtime/run-worker.d.ts +2 -2
  253. package/dist/runtime/run-worker.js +8 -6
  254. package/dist/runtime/step-log.d.ts +5 -0
  255. package/dist/runtime/step-log.js +9 -4
  256. package/dist/runtime/subagents/definition.d.ts +16 -20
  257. package/dist/runtime/subagents/definition.js +27 -27
  258. package/dist/runtime/subagents/dispatch.d.ts +6 -2
  259. package/dist/runtime/subagents/dispatch.js +17 -3
  260. package/dist/runtime/subagents/org-agents.js +1 -2
  261. package/dist/runtime/subagents/preamble.d.ts +3 -2
  262. package/dist/runtime/subagents/preamble.js +3 -2
  263. package/dist/runtime/subagents/render.d.ts +1 -2
  264. package/dist/runtime/subagents/render.js +1 -2
  265. package/dist/runtime/subagents/skill-fork.d.ts +2 -4
  266. package/dist/runtime/subagents/template-skills.d.ts +47 -0
  267. package/dist/runtime/subagents/template-skills.js +76 -0
  268. package/dist/runtime/waits.js +3 -1
  269. package/dist/schedules/effective-settings.d.ts +9 -3
  270. package/dist/schedules/effective-settings.js +5 -4
  271. package/dist/schedules/fire.js +11 -0
  272. package/dist/schedules/scheduled-tasks.service.js +5 -0
  273. package/dist/schedules/unattended.d.ts +2 -0
  274. package/dist/schedules/unattended.js +6 -1
  275. package/dist/skills/agent-skills.service.js +1 -2
  276. package/dist/skills/catalog.d.ts +1 -3
  277. package/dist/skills/catalog.js +1 -3
  278. package/dist/skills/conversation-catalog.d.ts +1 -2
  279. package/dist/skills/conversation-catalog.js +1 -2
  280. package/dist/skills/usable.js +2 -3
  281. package/dist/storage/storage.service.js +9 -1
  282. package/dist/tools/approvals.service.js +2 -4
  283. package/dist/tools/builtin-names.d.ts +2 -9
  284. package/dist/tools/builtin-names.js +4 -13
  285. package/dist/tools/builtins/sandbox/bash.js +3 -3
  286. package/dist/tools/builtins/sandbox/common.d.ts +7 -13
  287. package/dist/tools/builtins/sandbox/common.js +8 -115
  288. package/dist/tools/builtins/sandbox/fetch-attachment.d.ts +10 -0
  289. package/dist/tools/builtins/sandbox/fetch-attachment.js +119 -0
  290. package/dist/tools/builtins/sandbox/glob.js +1 -3
  291. package/dist/tools/builtins/sandbox/grep.js +3 -5
  292. package/dist/tools/builtins/sandbox/index.d.ts +8 -5
  293. package/dist/tools/builtins/sandbox/index.js +10 -5
  294. package/dist/tools/builtins/sandbox/read.js +3 -2
  295. package/dist/tools/builtins/tool-search.d.ts +4 -7
  296. package/dist/tools/builtins/tool-search.js +6 -10
  297. package/dist/tools/connector-account-policies.d.ts +30 -3
  298. package/dist/tools/connector-account-policies.js +54 -7
  299. package/dist/tools/connector-content.js +1 -0
  300. package/dist/tools/connectors.controller.d.ts +24 -11
  301. package/dist/tools/connectors.controller.js +35 -6
  302. package/dist/tools/connectors.schemas.d.ts +14 -1
  303. package/dist/tools/connectors.schemas.js +14 -2
  304. package/dist/tools/connectors.service.d.ts +22 -0
  305. package/dist/tools/connectors.service.js +65 -6
  306. package/dist/tools/drift.d.ts +6 -2
  307. package/dist/tools/drift.js +10 -5
  308. package/dist/tools/git-push-policy.d.ts +4 -1
  309. package/dist/tools/git-push-policy.js +3 -1
  310. package/dist/tools/info.d.ts +1 -7
  311. package/dist/tools/info.js +5 -18
  312. package/dist/tools/job-ids.d.ts +0 -4
  313. package/dist/tools/job-ids.js +1 -6
  314. package/dist/tools/pipeline.d.ts +4 -10
  315. package/dist/tools/pipeline.js +17 -62
  316. package/dist/tools/policy.d.ts +6 -2
  317. package/dist/tools/policy.js +14 -12
  318. package/dist/tools/registry.js +1 -5
  319. package/dist/ui/views.d.ts +5 -5
  320. package/dist/ui/views.js +10 -16
  321. package/package.json +3 -3
  322. package/src/migrations/0137_audit_org_idx.sql +5 -0
  323. package/src/migrations/0138_plugin_install_status_no_upgrading.sql +18 -0
  324. package/src/migrations/0139_retire_legacy_forms.sql +183 -0
  325. package/src/migrations/0140_retire_history_forms.sql +148 -0
  326. package/src/migrations/0141_install_ledger_promoted_rows.sql +36 -0
  327. package/src/migrations/0142_backfill_event_fields.sql +74 -0
  328. package/src/migrations/0143_backfill_model_call_fields.sql +105 -0
  329. package/src/migrations/0144_retire_frozen_definition_keys.sql +163 -0
  330. package/src/migrations/0145_org_network_allowances.sql +12 -0
  331. package/dist/admin/platform-audit.service.d.ts +0 -41
  332. package/dist/conversations/job-ids.d.ts +0 -6
  333. package/dist/conversations/job-ids.js +0 -6
  334. package/dist/extensions/job-ids.d.ts +0 -6
  335. package/dist/extensions/job-ids.js +0 -6
  336. package/dist/infra/queue/retired-schedulers.d.ts +0 -7
  337. package/dist/infra/queue/retired-schedulers.js +0 -9
  338. package/dist/orgs/job-ids.d.ts +0 -8
  339. package/dist/orgs/job-ids.js +0 -8
  340. package/dist/runtime/context/legacy-text.d.ts +0 -22
  341. package/dist/runtime/context/legacy-text.js +0 -37
  342. package/dist/runtime/job-ids.d.ts +0 -10
  343. package/dist/runtime/job-ids.js +0 -10
  344. package/dist/runtime/retired-schedulers.d.ts +0 -7
  345. package/dist/runtime/retired-schedulers.js +0 -31
  346. package/dist/storage/job-ids.d.ts +0 -6
  347. package/dist/storage/job-ids.js +0 -6
  348. package/dist/tools/legacy-data.d.ts +0 -34
  349. package/dist/tools/legacy-data.js +0 -76
  350. package/dist/tools/legacy-keys.d.ts +0 -51
  351. package/dist/tools/legacy-keys.js +0 -114
@@ -179,23 +179,27 @@ export declare const OUTBOX_JOBS: {
179
179
  };
180
180
  readonly "scope-item": {
181
181
  readonly queue: "cap-kernel-plugin-scope";
182
- readonly idempotent: "the sync converges each (item, org) from the scope, switches and installs it reads under the org's lock, and the installer re-checks under the org row lock, so a second run changes nothing";
182
+ readonly idempotent: "the fan-out owes the same chunk job ids again (`<item job id>-c<n>`), which the outbox and BullMQ dedupe, and each chunk converges from what it reads";
183
183
  readonly processedIn: "packages/kernel/src/catalog/scope-sync-workers.ts";
184
184
  readonly opts: {
185
- readonly attempts: 1;
185
+ readonly attempts: 7;
186
+ readonly backoff: {
187
+ readonly type: "exponential";
188
+ readonly delay: 5000;
189
+ };
186
190
  readonly removeOnComplete: true;
187
191
  readonly removeOnFail: true;
188
192
  };
189
193
  };
190
- readonly "scope-item-backstop": {
194
+ readonly "scope-item-chunk": {
191
195
  readonly queue: "cap-kernel-plugin-scope";
192
- readonly idempotent: "the sync converges each (item, org) from what it reads, as a scope-item job does, so a run after the immediate job changes nothing";
196
+ readonly idempotent: "the sync converges each (item, org) from the scope, switches and installs it reads under the org's lock, and the installer re-checks under the org row lock, so a second run changes nothing";
193
197
  readonly processedIn: "packages/kernel/src/catalog/scope-sync-workers.ts";
194
198
  readonly opts: {
195
- readonly attempts: 5;
199
+ readonly attempts: 7;
196
200
  readonly backoff: {
197
201
  readonly type: "exponential";
198
- readonly delay: 60000;
202
+ readonly delay: 5000;
199
203
  };
200
204
  readonly removeOnComplete: true;
201
205
  readonly removeOnFail: true;
@@ -206,7 +210,11 @@ export declare const OUTBOX_JOBS: {
206
210
  readonly idempotent: "the sync converges each (item, org) of the org from what it reads, so a second run changes nothing";
207
211
  readonly processedIn: "packages/kernel/src/catalog/scope-sync-workers.ts";
208
212
  readonly opts: {
209
- readonly attempts: 1;
213
+ readonly attempts: 7;
214
+ readonly backoff: {
215
+ readonly type: "exponential";
216
+ readonly delay: 5000;
217
+ };
210
218
  readonly removeOnComplete: true;
211
219
  readonly removeOnFail: true;
212
220
  };
@@ -1,3 +1,21 @@
1
+ /**
2
+ * The plugin scope queue's one retry policy (`scope-item`, `scope-item-chunk`, `scope-org`): 7
3
+ * attempts, 5 s doubling, so the retries wait 5 + 10 + 20 + 40 + 80 + 160 = 315 s (about five
4
+ * minutes) before the last attempt. That is the window a replica has to load an extension its
5
+ * deploy enabled: the enable's `kernel.plugins.changed` message reconciles every replica at once,
6
+ * and if a replica misses it, its periodic reconcile does within
7
+ * `extensionHost.reconcileIntervalSeconds` (default 60 s). An interval longer than the window (it
8
+ * is not capped: tests set an hour to keep the periodic reconcile out of the way) leaves such a
9
+ * replica's chunk to its last attempt, which records `catalog.not_deployed` for the pair until the
10
+ * next sync of the item.
11
+ * Removed once done, so the next add of the same id is not swallowed.
12
+ */
13
+ const SCOPE_JOB_OPTS = {
14
+ attempts: 7,
15
+ backoff: { type: "exponential", delay: 5_000 },
16
+ removeOnComplete: true,
17
+ removeOnFail: true,
18
+ };
1
19
  /**
2
20
  * Every job name added through the outbox. The task that introduces a job name adds its entry; a
3
21
  * name without one does not type-check as an `OutboxJobName` (`add` does not check it at runtime).
@@ -157,40 +175,38 @@ export const OUTBOX_JOBS = {
157
175
  removeOnFail: true,
158
176
  },
159
177
  },
160
- // `catalog/scope-sync.ts` SCOPE_ITEM_JOB: owed by every transaction that changes an item's
161
- // scope, disabled flag, list, owner or platform version, under `scopeItemJobId` (one per scope
162
- // version), and by a sync deferred on a replica that had not loaded the extension yet, under
163
- // `scopeRetryJobId` with `runAt` (spec §8.1)
178
+ // `catalog/scope-jobs.ts` SCOPE_ITEM_JOB: owed by every transaction that changes what an item's
179
+ // sync converges to (its scope, disabled flag, list, owner or platform version, an import's
180
+ // commit deploying no extension) and by the deploy of its extension (enable, a new version of an
181
+ // enabled one, an approval), under `scopeItemJobId` (one per owing transaction, spec §8.1)
164
182
  "scope-item": {
165
183
  queue: "cap-kernel-plugin-scope",
166
- idempotent: "the sync converges each (item, org) from the scope, switches and installs it reads under the org's lock, and the installer re-checks under the org row lock, so a second run changes nothing",
184
+ idempotent: "the fan-out owes the same chunk job ids again (`<item job id>-c<n>`), which the outbox and BullMQ dedupe, and each chunk converges from what it reads",
167
185
  processedIn: "packages/kernel/src/catalog/scope-sync-workers.ts",
168
- // one attempt (a pair's failure is recorded, not retried); removed once done, so the next
169
- // add of the same id is not swallowed
170
- opts: { attempts: 1, removeOnComplete: true, removeOnFail: true },
186
+ // the plugin scope queue's one retry policy (a PG or Redis blip); removed once done, so the
187
+ // next add of the same id is not swallowed
188
+ opts: SCOPE_JOB_OPTS,
171
189
  },
172
- // `catalog/scope-jobs.ts` SCOPE_BACKSTOP_JOB: owed by a confirmation's commit (an import, a
173
- // permissions confirm, an org upload's first version) with `runAt` 5 min on, under
174
- // `scopeBackstopJobId`, so a crash before the post-deploy add still converges (ruling R24)
175
- "scope-item-backstop": {
190
+ // `catalog/scope-jobs.ts` SCOPE_CHUNK_JOB: owed by a scope-item job, one per chunk of orgs, under
191
+ // `scopeChunkJobId`
192
+ "scope-item-chunk": {
176
193
  queue: "cap-kernel-plugin-scope",
177
- idempotent: "the sync converges each (item, org) from what it reads, as a scope-item job does, so a run after the immediate job changes nothing",
194
+ idempotent: "the sync converges each (item, org) from the scope, switches and installs it reads under the org's lock, and the installer re-checks under the org row lock, so a second run changes nothing",
178
195
  processedIn: "packages/kernel/src/catalog/scope-sync-workers.ts",
179
- // retried while the item's extension is not enabled on the deployment yet (the job throws):
180
- // 1, 2, 4, 8 min; removed once done
181
- opts: {
182
- attempts: 5,
183
- backoff: { type: "exponential", delay: 60_000 },
184
- removeOnComplete: true,
185
- removeOnFail: true,
186
- },
196
+ // retried while the item's extension is enabled but not loaded on the replica (the job throws
197
+ // after its other pairs, P3-38): 5 s doubling, about five minutes in all; the last attempt
198
+ // installs anyway and records what fails. A pair's own failure is recorded and does not fail
199
+ // the job; a retry of a deferred chunk attempts every pair again, a failed one included
200
+ // (idempotent: it converges from what it reads)
201
+ opts: SCOPE_JOB_OPTS,
187
202
  },
188
- // `catalog/scope-sync.ts` SCOPE_ORG_JOB: owed by an org's creation, under `scopeOrgJobId`
203
+ // `catalog/scope-jobs.ts` SCOPE_ORG_JOB: owed by an org's creation, under `scopeOrgJobId`
189
204
  "scope-org": {
190
205
  queue: "cap-kernel-plugin-scope",
191
206
  idempotent: "the sync converges each (item, org) of the org from what it reads, so a second run changes nothing",
192
207
  processedIn: "packages/kernel/src/catalog/scope-sync-workers.ts",
193
- opts: { attempts: 1, removeOnComplete: true, removeOnFail: true },
208
+ // as a chunk job: retried while one of its items' extensions is not loaded on the replica
209
+ opts: SCOPE_JOB_OPTS,
194
210
  },
195
211
  // `catalog/job-ids.ts` OWNED_PLUGIN_CLEANUP_JOB: owed by the removal of an org-owned plugin
196
212
  // (and by a confirmation that took out the extension of an item removed meanwhile), under
@@ -38,7 +38,6 @@ import { GatewayRole } from "../roles/gateway-role.js";
38
38
  import { WorkerRole } from "../roles/worker-role.js";
39
39
  import { UsageReporter } from "../runtime/usage-report.js";
40
40
  import { sandboxConfigWarnings } from "../sandbox/sandbox.service.js";
41
- import { warnLegacyToolKeys } from "../tools/legacy-data.js";
42
41
  import { KERNEL_APP_OPTIONS } from "./tokens.js";
43
42
  /**
44
43
  * The stop phase budgets. Each phase waits `min(its budget, what is left of the total)`, where the
@@ -204,10 +203,6 @@ let KernelLifecycle = class KernelLifecycle {
204
203
  // In flight for the stop: aborted when the shutdown begins, which then waits for its session to end
205
204
  await this.shutdown.trackInFlight(migrate(config.db.url, { schema: KERNEL_SCHEMA, dir: KERNEL_MIGRATIONS_DIR }, { connectTimeoutMs: config.db.connectTimeoutMs, signal: this.shutdown.signal }));
206
205
  this.checkpoint();
207
- // Rows still holding a release-candidate tool key that migration 0063 could not map: one
208
- // read-only count, a WARN when there are any (tool naming spec §8.2); never blocks the start
209
- await warnLegacyToolKeys(this.kdb.db, this.log).catch((e) => this.log.warn("tools.legacy_key_count_failed", { error: errMsg(e) }));
210
- this.checkpoint();
211
206
  // official 清单来源按配置同步(幂等;每个副本启动时都做)
212
207
  await this.marketplaces.syncOfficialFromConfig();
213
208
  // The bootstrap plugins (spec §7), once per manifest across replicas (the Redis lock
@@ -255,8 +250,7 @@ let KernelLifecycle = class KernelLifecycle {
255
250
  // start gate: their timers are armed at application bootstrap, but no tick runs before the
256
251
  // worker role opens the start gate here; the runOnStart jobs run then), the gateway role's
257
252
  // supervisor and webhook inbound
258
- if (worker)
259
- await worker.start(() => this.checkpoint());
253
+ worker?.start();
260
254
  if (gateway)
261
255
  await gateway.start(() => this.checkpoint());
262
256
  // Routes stay mounted and are resolved live: 503 extension.unavailable after an extension is revoked, back after it reloads (C17)
@@ -64,13 +64,12 @@ export declare class ModelEnablement {
64
64
  private disabledKeys;
65
65
  private rowsOf;
66
66
  /**
67
- * The extensions `orgId` explicitly turned off: a row of the org's own switch
68
- * (`extension_org_configs.enabled = false`, what the org's extension or plugin switch writes when
69
- * an org admin turns it off), never a required bootstrap plugin. An extension with no row for the
70
- * org is not in the set (it keeps working as before: spec A §8 with B's no-breakage anti-goal).
71
- * Read from PG on every call (one small query; the switch has its own writers, nothing to evict).
67
+ * The extensions enabled in `orgId` ({@link enabledExtensionsQuery}: the org's own switch on, or
68
+ * a required bootstrap plugin): a model provider is on in an org only then, like every other
69
+ * extension. Read from PG on every call (one small query; the switch has its own writers, nothing
70
+ * to evict).
72
71
  */
73
- switchedOff(orgId: string): Promise<Set<string>>;
72
+ enabledIn(orgId: string): Promise<Set<string>>;
74
73
  /** Whether neither the platform nor `orgId` disabled the model (the lists only; see the class). */
75
74
  isEnabled(orgId: string, provider: string, model: string): Promise<boolean>;
76
75
  /**
@@ -9,6 +9,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
9
  };
10
10
  import { Inject, Injectable, Module } from "@nestjs/common";
11
11
  import { ApiError } from "../errors.js";
12
+ import { enabledExtensionsQuery } from "../extensions/enabled.js";
12
13
  import { ExtensionRegistry, ExtensionRegistryModule } from "../extensions/registry.js";
13
14
  import { audit } from "../identity/audit.js";
14
15
  import { SuperAdminLock, SuperAdminLockModule } from "../identity/super-admin.js";
@@ -73,21 +74,13 @@ let ModelEnablement = class ModelEnablement {
73
74
  return q.execute();
74
75
  }
75
76
  /**
76
- * The extensions `orgId` explicitly turned off: a row of the org's own switch
77
- * (`extension_org_configs.enabled = false`, what the org's extension or plugin switch writes when
78
- * an org admin turns it off), never a required bootstrap plugin. An extension with no row for the
79
- * org is not in the set (it keeps working as before: spec A §8 with B's no-breakage anti-goal).
80
- * Read from PG on every call (one small query; the switch has its own writers, nothing to evict).
77
+ * The extensions enabled in `orgId` ({@link enabledExtensionsQuery}: the org's own switch on, or
78
+ * a required bootstrap plugin): a model provider is on in an org only then, like every other
79
+ * extension. Read from PG on every call (one small query; the switch has its own writers, nothing
80
+ * to evict).
81
81
  */
82
- async switchedOff(orgId) {
83
- const rows = await this.db
84
- .selectFrom("extension_org_configs as eoc")
85
- .leftJoin("extension_installs as ri", (j) => j.onRef("ri.name", "=", "eoc.plugin").on("ri.required", "=", true))
86
- .select("eoc.plugin")
87
- .where("eoc.org_id", "=", orgId)
88
- .where("eoc.enabled", "=", false)
89
- .where("ri.name", "is", null)
90
- .execute();
82
+ async enabledIn(orgId) {
83
+ const rows = await enabledExtensionsQuery(this.db, orgId).execute();
91
84
  return new Set(rows.map((r) => r.plugin));
92
85
  }
93
86
  /** Whether neither the platform nor `orgId` disabled the model (the lists only; see the class). */
@@ -1,7 +1,12 @@
1
1
  /** Whether connecting to this (resolved) IP is forbidden: private, loopback, reserved, or unparseable. */
2
2
  export declare function isForbiddenAddress(ip: string): boolean;
3
- /** Link-local (incl. cloud metadata 169.254.169.254) or unparseable: forbidden whatever the exemptions. */
3
+ /**
4
+ * Link-local (incl. cloud metadata 169.254.169.254), another cloud metadata endpoint
5
+ * (`100.100.100.200`, `fd00:ec2::254`) or unparseable: forbidden whatever the exemptions.
6
+ */
4
7
  export declare function isLinkLocalAddress(ip: string): boolean;
8
+ /** Loopback (`127.0.0.0/8`, `::1`) or unspecified (`0.0.0.0/8`, `::`), embedded forms included. */
9
+ export declare function isLoopbackOrUnspecifiedAddress(ip: string): boolean;
5
10
  /** A hostname normalized for comparison: lower case, no IPv6 brackets, no trailing dots. */
6
11
  export declare function normalizeHost(host: string): string;
7
12
  /** `localhost`, `127.0.0.0/8` or `::1` (as written in a URL). */
@@ -30,6 +35,15 @@ export type NetPolicy = {
30
35
  url: string;
31
36
  noProxy: string[];
32
37
  };
38
+ /**
39
+ * Set for an extension's request (`ctx.net.fetch`): loopback and unspecified addresses are
40
+ * refused whatever the exemptions (unless `allowInsecureLoopback`), `*.domain` entries in
41
+ * `allowPrivateHosts` match subdomains (never the apex), and a refused private address says
42
+ * `privateHint` (where such an endpoint is allowed) instead of the marketplace hint.
43
+ */
44
+ extension?: {
45
+ privateHint: string;
46
+ };
33
47
  };
34
48
  export type LookupFn = (host: string) => Promise<{
35
49
  address: string;
@@ -60,6 +74,12 @@ export declare function parseCidr(s: string): [string, number, "ipv4" | "ipv6"]
60
74
  export declare function privateHostEntryProblem(entry: string): string | null;
61
75
  /** A valid `marketplaces.allowPrivateHosts` entry (see {@link privateHostEntryProblem}). */
62
76
  export declare function isPrivateHostEntry(entry: string): boolean;
77
+ /**
78
+ * Whether a host entry of `policy.allowPrivateHosts` (a hostname, a `*.domain` wildcard, an IP or a
79
+ * CIDR) names `host`, ignoring trusted origins: such entries exempt https requests only, so a
80
+ * refused plain-http request to that host can point at an `http://host:port` origin entry.
81
+ */
82
+ export declare function hostEntryExempts(host: string, policy: NetPolicy): boolean;
63
83
  /**
64
84
  * Resolves `host` (an IP literal is taken as is) and returns every address, in resolution order,
65
85
  * after checking that each is public or exempt by `policy`. `origin` is the request URL's origin:
@@ -15,8 +15,12 @@ import { BlockList, isIP } from "node:net";
15
15
  * the embedded IPv4 address (proxy.mjs falls through to the IPv6 table, where they are not blocked);
16
16
  * a host behind a SIIT translator could otherwise reach a private IPv4 address.
17
17
  *
18
- * Link-local addresses (`169.254.0.0/16`, including cloud metadata, and `fe80::/10`) stay forbidden
19
- * whatever the exemptions: no internal mirror lives there.
18
+ * Link-local addresses (`169.254.0.0/16`, including cloud metadata, and `fe80::/10`) and the cloud
19
+ * metadata endpoints outside link-local (AWS IPv6 `fd00:ec2::254`, Alibaba Cloud `100.100.100.200`)
20
+ * stay forbidden whatever the exemptions: no internal mirror lives there.
21
+ *
22
+ * Extension requests (`ctx.net`, {@link NetPolicy.extension}) also refuse loopback and unspecified
23
+ * addresses whatever the exemptions (the kernel's own host), unless `allowInsecureLoopback` (tests).
20
24
  */
21
25
  const BLOCKED_V4 = [
22
26
  ["0.0.0.0", 8], // unspecified / this network
@@ -51,9 +55,18 @@ for (const [a, p] of BLOCKED_V4)
51
55
  blocked.addSubnet(a, p, "ipv4");
52
56
  for (const [a, p] of BLOCKED_V6)
53
57
  blocked.addSubnet(a, p, "ipv6");
54
- const linkLocal = new BlockList();
55
- linkLocal.addSubnet("169.254.0.0", 16, "ipv4");
56
- linkLocal.addSubnet("fe80::", 10, "ipv6");
58
+ /** Never allowed, whatever the exemptions: link-local and the cloud metadata endpoints. */
59
+ const neverAllowed = new BlockList();
60
+ neverAllowed.addSubnet("169.254.0.0", 16, "ipv4");
61
+ neverAllowed.addSubnet("fe80::", 10, "ipv6");
62
+ neverAllowed.addAddress("100.100.100.200", "ipv4"); // Alibaba Cloud metadata (inside CGNAT)
63
+ neverAllowed.addAddress("fd00:ec2::254", "ipv6"); // AWS IMDS over IPv6 (inside ULA)
64
+ /** Loopback and unspecified: never allowed for extension requests (see the file header). */
65
+ const loopbackOrUnspecified = new BlockList();
66
+ loopbackOrUnspecified.addSubnet("127.0.0.0", 8, "ipv4");
67
+ loopbackOrUnspecified.addSubnet("0.0.0.0", 8, "ipv4");
68
+ loopbackOrUnspecified.addAddress("::1", "ipv6");
69
+ loopbackOrUnspecified.addAddress("::", "ipv6");
57
70
  /** An IPv6 address as its 8 16-bit groups; null when it cannot be parsed. */
58
71
  function v6Groups(ip) {
59
72
  let s = ip.toLowerCase();
@@ -118,9 +131,16 @@ function inTable(ip, table) {
118
131
  export function isForbiddenAddress(ip) {
119
132
  return inTable(ip, blocked);
120
133
  }
121
- /** Link-local (incl. cloud metadata 169.254.169.254) or unparseable: forbidden whatever the exemptions. */
134
+ /**
135
+ * Link-local (incl. cloud metadata 169.254.169.254), another cloud metadata endpoint
136
+ * (`100.100.100.200`, `fd00:ec2::254`) or unparseable: forbidden whatever the exemptions.
137
+ */
122
138
  export function isLinkLocalAddress(ip) {
123
- return inTable(ip, linkLocal);
139
+ return inTable(ip, neverAllowed);
140
+ }
141
+ /** Loopback (`127.0.0.0/8`, `::1`) or unspecified (`0.0.0.0/8`, `::`), embedded forms included. */
142
+ export function isLoopbackOrUnspecifiedAddress(ip) {
143
+ return inTable(ip, loopbackOrUnspecified);
124
144
  }
125
145
  /** A hostname normalized for comparison: lower case, no IPv6 brackets, no trailing dots. */
126
146
  export function normalizeHost(host) {
@@ -201,6 +221,14 @@ function exemptions(host, policy, origin) {
201
221
  cidrs.addAddress("::1", "ipv6");
202
222
  }
203
223
  for (const entry of policy.allowPrivateHosts) {
224
+ // a `*.domain` wildcard (extension requests): a name under the domain, never the apex
225
+ if (entry.startsWith("*.")) {
226
+ const base = normalizeHost(entry.slice(2));
227
+ if (policy.extension && !literal && HOSTNAME.test(base) && host.endsWith(`.${base}`)) {
228
+ all = true;
229
+ }
230
+ continue;
231
+ }
204
232
  if (!isPrivateHostEntry(entry))
205
233
  continue;
206
234
  const cidr = parseCidr(entry);
@@ -248,6 +276,15 @@ function allowedBy(cidrs, ip) {
248
276
  }
249
277
  return false;
250
278
  }
279
+ /**
280
+ * Whether a host entry of `policy.allowPrivateHosts` (a hostname, a `*.domain` wildcard, an IP or a
281
+ * CIDR) names `host`, ignoring trusted origins: such entries exempt https requests only, so a
282
+ * refused plain-http request to that host can point at an `http://host:port` origin entry.
283
+ */
284
+ export function hostEntryExempts(host, policy) {
285
+ const { all, cidrs } = exemptions(host, { ...policy, allowInsecureLoopback: false }, undefined);
286
+ return all || (isIP(host) !== 0 && allowedBy(cidrs, host));
287
+ }
251
288
  /**
252
289
  * Resolves `host` (an IP literal is taken as is) and returns every address, in resolution order,
253
290
  * after checking that each is public or exempt by `policy`. `origin` is the request URL's origin:
@@ -272,11 +309,20 @@ export async function resolvePublic(host, policy, lookup = defaultLookup, origin
272
309
  throw new NetDeniedError("net.dns_failed", h, `Cannot resolve ${h}.`);
273
310
  }
274
311
  const ex = exemptions(h, policy, origin);
275
- const refused = (a) => isForbiddenAddress(a) && (isLinkLocalAddress(a) || (!ex.all && !allowedBy(ex.cidrs, a)));
312
+ // extension requests never reach the kernel's own host, whatever the exemptions
313
+ const ownHost = (a) => policy.extension !== undefined &&
314
+ !policy.allowInsecureLoopback &&
315
+ isLoopbackOrUnspecifiedAddress(a);
316
+ const refused = (a) => isForbiddenAddress(a) &&
317
+ (isLinkLocalAddress(a) || ownHost(a) || (!ex.all && !allowedBy(ex.cidrs, a)));
276
318
  if (addrs.some(refused)) {
319
+ const hint = policy.extension?.privateHint ??
320
+ "add the hostname to marketplaces.allowPrivateHosts to allow an internal host";
277
321
  throw new NetDeniedError("net.private_address", h, addrs.some(isLinkLocalAddress)
278
- ? `Access to ${h} is refused: it resolves to a link-local address, which is never allowed.`
279
- : `Access to ${h} is refused: it resolves to a private or reserved address (add the hostname to marketplaces.allowPrivateHosts to allow an internal host).`);
322
+ ? `Access to ${h} is refused: it resolves to a link-local or cloud metadata address, which is never allowed.`
323
+ : addrs.some(ownHost)
324
+ ? `Access to ${h} is refused: it resolves to a loopback address (the kernel's own host), which is never allowed.`
325
+ : `Access to ${h} is refused: it resolves to a private or reserved address (${hint}).`);
280
326
  }
281
327
  return addrs;
282
328
  }
@@ -12,7 +12,8 @@ import { type LookupFn, type NetPolicy } from "./public-address.js";
12
12
  * `noProxy` tunnels through the proxy (`proxy.ts`) instead: the host is not resolved locally
13
13
  * (the proxy resolves it and owns SSRF protection for the resolved address), but an IP-literal
14
14
  * host is still checked against the private ranges and `allowPrivateHosts`;
15
- * - redirects are never followed: a 3xx (other than 304) is {@link NetDeniedError} `net.redirect`;
15
+ * - redirects are never followed: a 3xx (other than 304) is {@link NetDeniedError} `net.redirect`,
16
+ * or, with `redirects: "return"`, returned to the caller as is (its body unread);
16
17
  * - with `maxBytes`, a declared or streamed body over the limit is {@link NetBodyTooLargeError}.
17
18
  */
18
19
  /** The response body is larger than `maxBytes`. */
@@ -32,6 +33,8 @@ export declare function safeFetch(url: string, init: RequestInit & {
32
33
  }, policy: NetPolicy, opts?: {
33
34
  lookup?: LookupFn;
34
35
  maxBytes?: number;
36
+ /** A 3xx response (other than 304): `refuse` (default) rejects with `net.redirect`, `return` returns it. */
37
+ redirects?: "refuse" | "return";
35
38
  /** Tests only: wraps the pinned dispatcher (defaults to {@link pinnedAgent}). */
36
39
  dispatcherFor?: (ips: string[]) => Agent;
37
40
  /** Tests only: builds the proxy dispatcher (defaults to {@link proxyAgent}). */
@@ -1,7 +1,7 @@
1
1
  import { isIP } from "node:net";
2
2
  import { Agent, fetch as undiciFetch } from "undici";
3
3
  import { bypassesProxy, proxyAgent } from "./proxy.js";
4
- import { isLoopbackHost, NetDeniedError, normalizeHost, resolvePublic, } from "./public-address.js";
4
+ import { hostEntryExempts, isLoopbackHost, NetDeniedError, normalizeHost, resolvePublic, } from "./public-address.js";
5
5
  /**
6
6
  * `fetch` for URLs that come from a marketplace or an admin (spec §5.1):
7
7
  * - https only; plain http only for a loopback host under `allowInsecureLoopback` (testing), or
@@ -14,7 +14,8 @@ import { isLoopbackHost, NetDeniedError, normalizeHost, resolvePublic, } from ".
14
14
  * `noProxy` tunnels through the proxy (`proxy.ts`) instead: the host is not resolved locally
15
15
  * (the proxy resolves it and owns SSRF protection for the resolved address), but an IP-literal
16
16
  * host is still checked against the private ranges and `allowPrivateHosts`;
17
- * - redirects are never followed: a 3xx (other than 304) is {@link NetDeniedError} `net.redirect`;
17
+ * - redirects are never followed: a 3xx (other than 304) is {@link NetDeniedError} `net.redirect`,
18
+ * or, with `redirects: "return"`, returned to the caller as is (its body unread);
18
19
  * - with `maxBytes`, a declared or streamed body over the limit is {@link NetBodyTooLargeError}.
19
20
  */
20
21
  /** The response body is larger than `maxBytes`. */
@@ -83,7 +84,14 @@ export async function safeFetch(url, init, policy, opts = {}) {
83
84
  ((policy.allowInsecureLoopback && isLoopbackHost(host)) ||
84
85
  policy.trustedOrigins.some((o) => originOf(o) === target.origin));
85
86
  if (target.protocol !== "https:" && !insecureOk) {
86
- throw new NetDeniedError("net.insecure_url", host, `The URL must use https (${host}).`);
87
+ // an extension request to a host the org allows only as a host entry (https only): name the
88
+ // origin entry that would allow plain http
89
+ const hint = policy.extension !== undefined &&
90
+ target.protocol === "http:" &&
91
+ hostEntryExempts(host, policy)
92
+ ? ` ${host} is allowed for the organization over https only; to allow plain http, ask a super admin to allow the origin ${target.origin}.`
93
+ : "";
94
+ throw new NetDeniedError("net.insecure_url", host, `The URL must use https (${host}).${hint}`);
87
95
  }
88
96
  const proxy = policy.proxy;
89
97
  const proxied = proxy !== undefined &&
@@ -126,7 +134,10 @@ export async function safeFetch(url, init, policy, opts = {}) {
126
134
  }
127
135
  // close() waits for the in-flight request (the body still being read) before it closes
128
136
  void agent.close().catch(() => { });
129
- if (res.status >= 300 && res.status < 400 && res.status !== 304) {
137
+ if (res.status >= 300 &&
138
+ res.status < 400 &&
139
+ res.status !== 304 &&
140
+ (opts.redirects ?? "refuse") === "refuse") {
130
141
  await res.body?.cancel().catch(() => { });
131
142
  throw new NetDeniedError("net.redirect", host, `${host} responded with a redirect (${res.status}); redirects are not followed.`, res.status);
132
143
  }
@@ -599,6 +599,8 @@ let DirectorySync = class DirectorySync {
599
599
  }
600
600
  }
601
601
  async offboardIn(orgId, source, accountId) {
602
+ // offboarding: the org admission lock first (`Offboarding.offboard`)
603
+ await this.orgLock.lockForRemoval(orgId);
602
604
  if (!(await this.orgLock.tryLock(orgId)))
603
605
  return undefined;
604
606
  const row = await this.db
@@ -143,6 +143,10 @@ let MembersService = class MembersService {
143
143
  await this.removeIn(orgId, targetId, actorAccountId, actorTokenId);
144
144
  }
145
145
  async removeIn(orgId, targetId, actorAccountId, actorTokenId) {
146
+ // an unauthorized call is refused before it holds off the org's run writes
147
+ await this.orgLock.precheck(orgId, actorAccountId, "org.members.manage");
148
+ // offboarding: the org admission lock first (`Offboarding.offboard`)
149
+ await this.orgLock.lockForRemoval(orgId);
146
150
  const { role: actorRole } = await this.orgLock.lockAndAuthorize(orgId, actorAccountId, "org.members.manage");
147
151
  const before = await this.requireMember(orgId, targetId);
148
152
  await rejectAnonymousGuest(this.db.db, targetId);
@@ -167,6 +171,7 @@ let MembersService = class MembersService {
167
171
  await this.leaveIn(orgId, accountId, actorTokenId);
168
172
  }
169
173
  async leaveIn(orgId, accountId, actorTokenId) {
174
+ await this.orgLock.lockForRemoval(orgId);
170
175
  await this.orgLock.lock(orgId);
171
176
  const before = await this.requireMember(orgId, accountId);
172
177
  const actor = {
@@ -0,0 +1,79 @@
1
+ import type { WriteActor } from "../identity/write-actor.js";
2
+ import { KernelDb } from "../infra/database/kernel-db.js";
3
+ import { TransactionHost } from "../infra/database/transaction-host.js";
4
+ import { OrgLock } from "./org-lock.js";
5
+ /**
6
+ * Org network allowances (plugin safe fetch spec): the internal endpoints a super admin lets one
7
+ * org's extension requests (`ctx.net.fetch` made for that org) reach although they resolve to
8
+ * private or loopback addresses. Stored in `capybara.org_network_allowances`, read from PG on every
9
+ * call (no in-process cache: a removal takes effect on the next request on every replica).
10
+ *
11
+ * An entry is one of:
12
+ * - a hostname or a `*.domain` wildcard (https, any port; the domain has at least two labels and is
13
+ * not a known public suffix);
14
+ * - an IP address or a CIDR (IP-literal URL hosts only, within `MIN_CIDR_PREFIX`);
15
+ * - an origin `http(s)://host[:port]`: exactly that scheme, host and port, plain http included.
16
+ * Link-local and cloud metadata addresses (`100.100.100.200`, `fd00:ec2::254`), loopback and
17
+ * unspecified addresses are never allowed (`localhost` names included), nor IPv6 forms that embed an IPv4 address, so entries naming them are refused.
18
+ */
19
+ /** The most entries one org may hold. */
20
+ export declare const ORG_NETWORK_ALLOWANCES_MAX = 200;
21
+ /**
22
+ * Why an allowance entry is invalid (English, shown to the super admin), or null when it is valid
23
+ * (see the file header for the accepted forms). Reuses `privateHostEntryProblem` for hosts, IPs and
24
+ * CIDRs; refuses link-local, loopback and unspecified addresses (also as `localhost` names, numeric
25
+ * hosts and inside origins), IPv6 forms that embed IPv4, wildcards over a public suffix, and
26
+ * origins with a path, query, fragment or credentials.
27
+ */
28
+ export declare function allowanceEntryProblem(entry: string): string | null;
29
+ /**
30
+ * The stored form of a valid entry: hosts lower case without brackets or trailing dots, IPv6
31
+ * addresses and CIDRs in canonical spelling, origins as scheme, host and port (default ports
32
+ * dropped), so one endpoint is stored once.
33
+ */
34
+ export declare function normalizeAllowanceEntry(entry: string): string;
35
+ export type OrgNetworkAllowance = {
36
+ id: string;
37
+ entry: string;
38
+ createdAt: Date;
39
+ createdBy: string | null;
40
+ };
41
+ /** What an org's allowances exempt, in the shape of `NetPolicy` (`net/public-address.ts`). */
42
+ export type OrgNetworkPolicy = {
43
+ allowPrivateHosts: string[];
44
+ trustedOrigins: string[];
45
+ };
46
+ export declare class OrgNetworkAllowances {
47
+ private readonly db;
48
+ readonly txHost: TransactionHost;
49
+ private readonly orgLock;
50
+ constructor(db: KernelDb, txHost: TransactionHost, orgLock: OrgLock);
51
+ /** The org's entries, oldest first. */
52
+ list(orgId: string): Promise<OrgNetworkAllowance[]>;
53
+ /**
54
+ * Adds an entry (super admin only; the route enforces it). Refuses an invalid entry with
55
+ * `org.network_allowance_invalid` and the reason; an entry already listed is returned as is,
56
+ * without a second audit event. `actor.tokenId` (a personal access token) and `auditDetail` (e.g.
57
+ * `viaAgent`) go into the audit event; the caller's detail cannot override `entry`.
58
+ */
59
+ add(orgId: string, entry: string, actor: WriteActor, auditDetail?: Record<string, unknown>): Promise<OrgNetworkAllowance>;
60
+ /**
61
+ * Removes the org's entry `id`; false (and no audit event) when the org has no such entry. The
62
+ * actor and `auditDetail` are recorded as {@link add}'s.
63
+ */
64
+ remove(orgId: string, id: string, actor: WriteActor, auditDetail?: Record<string, unknown>): Promise<boolean>;
65
+ /** An allowance audit event in the org; the caller's detail first, so `entry` wins. */
66
+ private audit;
67
+ /**
68
+ * The exemptions of an org-bound request: origins become `trustedOrigins`, hosts, wildcards, IPs
69
+ * and CIDRs `allowPrivateHosts`. A deleted (or unknown) org allows nothing.
70
+ */
71
+ policyFor(orgId: string): Promise<OrgNetworkPolicy>;
72
+ }
73
+ /**
74
+ * {@link OrgNetworkAllowances} alone (its only dependencies are the global database providers), so
75
+ * the extension host (`ctx.net`) can read the allowances without importing the orgs domain, which
76
+ * itself imports the extensions domain.
77
+ */
78
+ export declare class OrgNetworkAllowancesModule {
79
+ }