@capybara-hq/kernel 0.1.0-rc.3 → 0.1.0-rc.5
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.
- package/bootstrap.json +61 -5
- package/dist/admin/admin.controller.d.ts +47 -16
- package/dist/admin/admin.controller.js +44 -19
- package/dist/admin/extensions.controller.d.ts +24 -3
- package/dist/admin/extensions.controller.js +48 -8
- package/dist/admin/setup-lock.d.ts +1 -1
- package/dist/admin/setup-lock.js +1 -1
- package/dist/admin/setup.controller.d.ts +1 -1
- package/dist/admin/setup.controller.js +1 -1
- package/dist/admin/setup.service.d.ts +4 -4
- package/dist/admin/setup.service.js +8 -8
- package/dist/agents/agent-lookup.d.ts +19 -0
- package/dist/agents/agent-lookup.js +21 -0
- package/dist/agents/agent-profile.service.d.ts +29 -0
- package/dist/agents/agent-profile.service.js +117 -0
- package/dist/agents/agents.controller.d.ts +168 -3
- package/dist/agents/agents.controller.js +201 -12
- package/dist/agents/agents.module.d.ts +4 -1
- package/dist/agents/agents.module.js +36 -6
- package/dist/agents/agents.service.d.ts +64 -27
- package/dist/agents/agents.service.js +199 -74
- package/dist/agents/definition.d.ts +53 -0
- package/dist/agents/definition.js +55 -0
- package/dist/agents/from-template.d.ts +106 -0
- package/dist/agents/from-template.js +649 -0
- package/dist/agents/from-template.service.d.ts +60 -0
- package/dist/agents/from-template.service.js +161 -0
- package/dist/agents/instruction-files.d.ts +60 -0
- package/dist/agents/instruction-files.js +111 -0
- package/dist/agents/models.controller.d.ts +9 -3
- package/dist/agents/models.controller.js +19 -4
- package/dist/agents/org-templates.d.ts +176 -0
- package/dist/agents/org-templates.js +827 -0
- package/dist/agents/pending-connector-requests.d.ts +77 -0
- package/dist/agents/pending-connector-requests.js +215 -0
- package/dist/agents/pending-connectors.controller.d.ts +39 -0
- package/dist/agents/pending-connectors.controller.js +91 -0
- package/dist/agents/pending-connectors.service.d.ts +62 -0
- package/dist/agents/pending-connectors.service.js +282 -0
- package/dist/agents/profile.d.ts +96 -0
- package/dist/agents/profile.js +51 -0
- package/dist/agents/readiness.controller.d.ts +26 -0
- package/dist/agents/readiness.controller.js +56 -0
- package/dist/agents/templates.controller.d.ts +86 -5
- package/dist/agents/templates.controller.js +171 -7
- package/dist/agents/templates.d.ts +127 -23
- package/dist/agents/templates.js +476 -53
- package/dist/background/background.controller.d.ts +38 -0
- package/dist/background/background.controller.js +87 -0
- package/dist/background/background.module.d.ts +10 -0
- package/dist/background/background.module.js +39 -0
- package/dist/background/finish.d.ts +44 -0
- package/dist/background/finish.js +94 -0
- package/dist/background/launch-lock.d.ts +15 -0
- package/dist/background/launch-lock.js +40 -0
- package/dist/background/launch.d.ts +36 -0
- package/dist/background/launch.js +70 -0
- package/dist/background/poll.job.d.ts +56 -0
- package/dist/background/poll.job.js +190 -0
- package/dist/background/render.d.ts +23 -0
- package/dist/background/render.js +48 -0
- package/dist/background/session.d.ts +48 -0
- package/dist/background/session.js +79 -0
- package/dist/background/status.d.ts +35 -0
- package/dist/background/status.js +57 -0
- package/dist/background/stop.d.ts +68 -0
- package/dist/background/stop.js +243 -0
- package/dist/background/store.d.ts +162 -0
- package/dist/background/store.js +357 -0
- package/dist/background/store.module.d.ts +6 -0
- package/dist/background/store.module.js +22 -0
- package/dist/background/tasks.service.d.ts +77 -0
- package/dist/background/tasks.service.js +211 -0
- package/dist/background/wake-rules.d.ts +66 -0
- package/dist/background/wake-rules.js +113 -0
- package/dist/background/wake.d.ts +30 -0
- package/dist/background/wake.js +63 -0
- package/dist/catalog/admin-catalog.controller.d.ts +129 -30
- package/dist/catalog/admin-catalog.controller.js +213 -42
- package/dist/catalog/catalog-content.d.ts +58 -32
- package/dist/catalog/catalog-content.js +34 -8
- package/dist/catalog/catalog-import.module.d.ts +7 -0
- package/dist/catalog/catalog-import.module.js +60 -0
- package/dist/catalog/catalog-items.d.ts +111 -7
- package/dist/catalog/catalog-items.js +279 -13
- package/dist/catalog/catalog-scope.service.d.ts +260 -0
- package/dist/catalog/catalog-scope.service.js +1116 -0
- package/dist/catalog/catalog.module.d.ts +7 -9
- package/dist/catalog/catalog.module.js +41 -31
- package/dist/catalog/catalog.service.d.ts +101 -29
- package/dist/catalog/catalog.service.js +104 -106
- package/dist/catalog/claude/agents.js +2 -0
- package/dist/catalog/cli.d.ts +11 -1
- package/dist/catalog/cli.js +26 -1
- package/dist/catalog/component-promotion.d.ts +96 -0
- package/dist/catalog/component-promotion.js +395 -0
- package/dist/catalog/component-scope.controller.d.ts +152 -0
- package/dist/catalog/component-scope.controller.js +287 -0
- package/dist/catalog/component-scope.service.d.ts +303 -0
- package/dist/catalog/component-scope.service.js +936 -0
- package/dist/catalog/component-state.d.ts +174 -0
- package/dist/catalog/component-state.js +437 -0
- package/dist/catalog/job-ids.d.ts +30 -0
- package/dist/catalog/job-ids.js +25 -0
- package/dist/catalog/marketplace-fetch.d.ts +1 -1
- package/dist/catalog/marketplaces.controller.d.ts +22 -5
- package/dist/catalog/marketplaces.controller.js +56 -7
- package/dist/catalog/marketplaces.service.d.ts +54 -14
- package/dist/catalog/marketplaces.service.js +71 -22
- package/dist/catalog/org-catalog.controller.d.ts +125 -34
- package/dist/catalog/org-catalog.controller.js +181 -65
- package/dist/catalog/org-catalog.service.d.ts +65 -33
- package/dist/catalog/org-catalog.service.js +324 -157
- package/dist/catalog/org-extension-switch.d.ts +41 -0
- package/dist/catalog/org-extension-switch.js +115 -0
- package/dist/catalog/org-extension.controller.d.ts +119 -0
- package/dist/catalog/org-extension.controller.js +266 -0
- package/dist/catalog/org-plugin-uploads.controller.d.ts +98 -0
- package/dist/catalog/org-plugin-uploads.controller.js +213 -0
- package/dist/catalog/org-plugin-uploads.d.ts +63 -0
- package/dist/catalog/org-plugin-uploads.js +116 -0
- package/dist/catalog/owned-items.d.ts +184 -0
- package/dist/catalog/owned-items.js +789 -0
- package/dist/catalog/owned-plugin-cleanup.d.ts +56 -0
- package/dist/catalog/owned-plugin-cleanup.js +194 -0
- package/dist/catalog/ownership.d.ts +172 -0
- package/dist/catalog/ownership.js +326 -0
- package/dist/catalog/platform-components.d.ts +221 -0
- package/dist/catalog/platform-components.js +761 -0
- package/dist/catalog/platform-migration.d.ts +115 -0
- package/dist/catalog/platform-migration.js +582 -0
- package/dist/catalog/plugin-import.d.ts +178 -34
- package/dist/catalog/plugin-import.js +623 -83
- package/dist/catalog/plugin-install.d.ts +185 -41
- package/dist/catalog/plugin-install.js +758 -126
- package/dist/catalog/plugin-upgrade.d.ts +260 -225
- package/dist/catalog/plugin-upgrade.js +1148 -1137
- package/dist/catalog/request-rules.d.ts +65 -0
- package/dist/catalog/request-rules.js +217 -0
- package/dist/catalog/requests.service.d.ts +193 -118
- package/dist/catalog/requests.service.js +526 -343
- package/dist/catalog/reserved-names.d.ts +8 -0
- package/dist/catalog/reserved-names.js +42 -0
- package/dist/catalog/scope-orgs.d.ts +21 -0
- package/dist/catalog/scope-orgs.js +67 -0
- package/dist/catalog/scope-state.d.ts +35 -0
- package/dist/catalog/scope-state.js +87 -0
- package/dist/catalog/scope-sync-workers.d.ts +7 -0
- package/dist/catalog/scope-sync-workers.js +29 -0
- package/dist/catalog/scope-sync.d.ts +176 -0
- package/dist/catalog/scope-sync.js +576 -0
- package/dist/catalog/scope.d.ts +42 -0
- package/dist/catalog/scope.js +42 -0
- package/dist/catalog/unshare.d.ts +22 -0
- package/dist/catalog/unshare.js +118 -0
- package/dist/channels/actions.js +1 -0
- package/dist/channels/cards.js +21 -3
- package/dist/channels/channels.module.js +2 -0
- package/dist/channels/connections.service.d.ts +16 -1
- package/dist/channels/connections.service.js +38 -4
- package/dist/channels/inbound.d.ts +9 -1
- package/dist/channels/inbound.js +67 -5
- package/dist/channels/phrases.d.ts +35 -0
- package/dist/channels/phrases.js +29 -0
- package/dist/channels/threads.d.ts +5 -0
- package/dist/channels/threads.js +8 -0
- package/dist/config/docs.js +3 -1
- package/dist/config/schema.d.ts +24 -2
- package/dist/config/schema.js +145 -19
- package/dist/conversations/access.d.ts +6 -4
- package/dist/conversations/access.js +29 -5
- package/dist/conversations/agent-tree.d.ts +46 -0
- package/dist/conversations/agent-tree.js +92 -0
- package/dist/conversations/attachments.service.d.ts +30 -0
- package/dist/conversations/attachments.service.js +130 -5
- package/dist/conversations/conversations.controller.d.ts +79 -23
- package/dist/conversations/conversations.controller.js +141 -50
- package/dist/conversations/conversations.module.d.ts +1 -1
- package/dist/conversations/conversations.module.js +10 -7
- package/dist/conversations/conversations.service.d.ts +108 -9
- package/dist/conversations/conversations.service.js +237 -30
- package/dist/conversations/environment-status.d.ts +24 -0
- package/dist/conversations/environment-status.js +71 -0
- package/dist/conversations/event-log.d.ts +21 -6
- package/dist/conversations/event-log.js +70 -1
- package/dist/conversations/events.d.ts +140 -0
- package/dist/conversations/events.js +142 -13
- package/dist/conversations/queue.d.ts +3 -1
- package/dist/conversations/queue.js +13 -2
- package/dist/conversations/queue.service.d.ts +1 -1
- package/dist/conversations/queue.service.js +8 -1
- package/dist/conversations/titles.d.ts +12 -3
- package/dist/conversations/titles.js +36 -8
- package/dist/conversations/tree.d.ts +17 -0
- package/dist/conversations/tree.js +42 -0
- package/dist/ctx/create-ctx.d.ts +5 -0
- package/dist/ctx/create-ctx.js +15 -5
- package/dist/db/kernel-schema.d.ts +420 -20
- package/dist/db/pool.d.ts +5 -0
- package/dist/db/pool.js +3 -0
- package/dist/environments/access.d.ts +41 -0
- package/dist/environments/access.js +77 -0
- package/dist/environments/conversation-environments.d.ts +61 -0
- package/dist/environments/conversation-environments.js +170 -0
- package/dist/environments/dto.d.ts +25 -0
- package/dist/environments/dto.js +12 -0
- package/dist/environments/environments.controller.d.ts +33 -0
- package/dist/environments/environments.controller.js +110 -0
- package/dist/environments/environments.module.d.ts +8 -0
- package/dist/environments/environments.module.js +27 -0
- package/dist/environments/environments.service.d.ts +153 -0
- package/dist/environments/environments.service.js +541 -0
- package/dist/environments/legacy-cleanup.job.d.ts +34 -0
- package/dist/environments/legacy-cleanup.job.js +141 -0
- package/dist/errors.d.ts +90 -2
- package/dist/errors.js +90 -2
- package/dist/evals/reconstruct.d.ts +18 -9
- package/dist/evals/reconstruct.js +26 -7
- package/dist/evals/run-replay.d.ts +7 -2
- package/dist/evals/run-replay.js +42 -7
- package/dist/evals/step-replay.d.ts +2 -5
- package/dist/evals/step-replay.js +13 -7
- package/dist/evals/trajectory.js +1 -1
- package/dist/evals/trajectory.service.d.ts +8 -3
- package/dist/evals/trajectory.service.js +26 -14
- package/dist/evals/worker.d.ts +5 -5
- package/dist/evals/worker.js +9 -3
- package/dist/extensions/admin-ops.d.ts +36 -10
- package/dist/extensions/admin-ops.js +67 -42
- package/dist/extensions/admin-view.d.ts +39 -10
- package/dist/extensions/admin-view.js +111 -20
- package/dist/extensions/blobs.js +1 -0
- package/dist/extensions/bootstrap.d.ts +17 -4
- package/dist/extensions/bootstrap.js +70 -9
- package/dist/extensions/cli.d.ts +12 -0
- package/dist/extensions/cli.js +11 -2
- package/dist/extensions/config-issues.d.ts +13 -1
- package/dist/extensions/config-issues.js +22 -5
- package/dist/extensions/config-json-schema.d.ts +43 -7
- package/dist/extensions/config-json-schema.js +124 -58
- package/dist/extensions/deployment-config-store.d.ts +6 -0
- package/dist/extensions/deployment-config.d.ts +78 -8
- package/dist/extensions/deployment-config.js +172 -34
- package/dist/extensions/display-name.d.ts +22 -0
- package/dist/extensions/display-name.js +77 -0
- package/dist/extensions/effective-config.d.ts +127 -0
- package/dist/extensions/effective-config.js +368 -0
- package/dist/extensions/extensions.module.js +14 -2
- package/dist/extensions/hooks.d.ts +37 -4
- package/dist/extensions/hooks.js +82 -16
- package/dist/extensions/host.d.ts +45 -2
- package/dist/extensions/host.js +112 -16
- package/dist/extensions/installs.d.ts +20 -8
- package/dist/extensions/installs.js +37 -15
- package/dist/extensions/org-config.d.ts +81 -6
- package/dist/extensions/org-config.js +195 -44
- package/dist/extensions/org-levels.d.ts +37 -0
- package/dist/extensions/org-levels.js +71 -0
- package/dist/extensions/org-overrides.d.ts +151 -0
- package/dist/extensions/org-overrides.js +524 -0
- package/dist/extensions/package.d.ts +14 -1
- package/dist/extensions/package.js +51 -2
- package/dist/extensions/permission-view.d.ts +19 -4
- package/dist/extensions/permission-view.js +4 -4
- package/dist/extensions/plugin-items.d.ts +21 -0
- package/dist/extensions/plugin-items.js +42 -0
- package/dist/extensions/point-defaults.controller.d.ts +34 -0
- package/dist/extensions/point-defaults.controller.js +71 -0
- package/dist/extensions/point-defaults.d.ts +71 -0
- package/dist/extensions/point-defaults.js +234 -0
- package/dist/extensions/registry.d.ts +1 -1
- package/dist/extensions/registry.js +2 -0
- package/dist/extensions/resolve.d.ts +5 -1
- package/dist/extensions/resolve.js +15 -2
- package/dist/files/previewers.d.ts +8 -1
- package/dist/files/previewers.js +35 -6
- package/dist/http/api.module.js +31 -3
- package/dist/http/app.d.ts +11 -2
- package/dist/http/app.js +31 -5
- package/dist/http/nest-logger.d.ts +6 -5
- package/dist/http/nest-logger.js +24 -10
- package/dist/identity/accounts.d.ts +29 -29
- package/dist/identity/accounts.js +47 -47
- package/dist/identity/audit.d.ts +2 -1
- package/dist/identity/auth.controller.js +9 -5
- package/dist/identity/auth.guard.d.ts +13 -5
- package/dist/identity/auth.guard.js +26 -9
- package/dist/identity/cookies.d.ts +20 -5
- package/dist/identity/cookies.js +44 -6
- package/dist/identity/identity.module.d.ts +1 -1
- package/dist/identity/identity.module.js +4 -4
- package/dist/identity/me.controller.d.ts +5 -1
- package/dist/identity/me.controller.js +12 -3
- package/dist/identity/principal.d.ts +3 -3
- package/dist/identity/principal.js +3 -3
- package/dist/identity/sessions.d.ts +12 -0
- package/dist/identity/sessions.js +45 -0
- package/dist/identity/settings.d.ts +22 -11
- package/dist/identity/settings.js +53 -18
- package/dist/identity/{enterprise-admin.d.ts → super-admin.d.ts} +5 -5
- package/dist/identity/{enterprise-admin.js → super-admin.js} +24 -24
- package/dist/inbox/inbox.module.d.ts +4 -1
- package/dist/inbox/inbox.module.js +7 -3
- package/dist/inbox/inbox.service.d.ts +62 -3
- package/dist/inbox/inbox.service.js +74 -4
- package/dist/inbox/org-notices.d.ts +63 -0
- package/dist/inbox/org-notices.js +91 -0
- package/dist/inbox/platform-inbox.controller.d.ts +23 -0
- package/dist/inbox/platform-inbox.controller.js +58 -0
- package/dist/inbox/platform-inbox.service.d.ts +151 -0
- package/dist/inbox/platform-inbox.service.js +263 -0
- package/dist/inbox/platform-notices.d.ts +19 -0
- package/dist/inbox/platform-notices.js +9 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/infra/cli/kernel-cli.module.js +9 -1
- package/dist/infra/config/kernel-config.service.js +27 -1
- package/dist/infra/database/database.module.js +1 -0
- package/dist/infra/database/xact-lock.d.ts +1 -1
- package/dist/infra/database/xact-lock.js +1 -1
- package/dist/infra/events/kernel-events.d.ts +19 -2
- package/dist/infra/lifecycle/process-guards.js +4 -0
- package/dist/infra/logging/logging.module.d.ts +4 -2
- package/dist/infra/logging/logging.module.js +24 -6
- package/dist/infra/queue/add-job-once.d.ts +14 -0
- package/dist/infra/queue/add-job-once.js +16 -0
- package/dist/infra/queue/redis-connection.d.ts +6 -0
- package/dist/infra/queue/redis-connection.js +7 -0
- package/dist/infra/redis/redis.module.js +6 -2
- package/dist/infra/tokens.d.ts +6 -1
- package/dist/infra/tokens.js +6 -1
- package/dist/kernel.module.js +6 -0
- package/dist/lifecycle/kernel-lifecycle.d.ts +1 -1
- package/dist/lifecycle/kernel-lifecycle.js +4 -4
- package/dist/log.d.ts +13 -1
- package/dist/log.js +93 -17
- package/dist/models/prices.d.ts +5 -5
- package/dist/models/prices.js +4 -4
- package/dist/models/purposes.controller.d.ts +6 -4
- package/dist/models/purposes.controller.js +13 -7
- package/dist/models/purposes.d.ts +43 -9
- package/dist/models/purposes.js +48 -18
- package/dist/orgs/directory-sync.d.ts +14 -5
- package/dist/orgs/directory-sync.js +41 -17
- package/dist/orgs/offboarding.d.ts +12 -8
- package/dist/orgs/offboarding.js +54 -33
- package/dist/orgs/org-lock.d.ts +5 -0
- package/dist/orgs/org-lock.js +13 -0
- package/dist/orgs/org.guard.d.ts +10 -0
- package/dist/orgs/org.guard.js +43 -32
- package/dist/orgs/orgs.module.js +3 -0
- package/dist/orgs/orgs.service.d.ts +20 -7
- package/dist/orgs/orgs.service.js +32 -12
- package/dist/orgs/roles.d.ts +1 -1
- package/dist/orgs/roles.js +5 -1
- package/dist/orgs/settings.controller.d.ts +1 -1
- package/dist/orgs/settings.service.d.ts +20 -3
- package/dist/orgs/settings.service.js +19 -4
- package/dist/overrides.d.ts +3 -0
- package/dist/overrides.js +2 -0
- package/dist/platform/actor.d.ts +48 -0
- package/dist/platform/actor.js +100 -0
- package/dist/platform/agent-docs.d.ts +18 -0
- package/dist/platform/agent-docs.js +59 -0
- package/dist/platform/availability.d.ts +14 -0
- package/dist/platform/availability.js +43 -0
- package/dist/platform/errors.d.ts +11 -0
- package/dist/platform/errors.js +26 -0
- package/dist/platform/exclusions.d.ts +12 -0
- package/dist/platform/exclusions.js +1387 -0
- package/dist/platform/method.d.ts +6 -0
- package/dist/platform/method.js +15 -0
- package/dist/platform/operation.d.ts +92 -0
- package/dist/platform/operation.js +35 -0
- package/dist/platform/ops/agent-templates.ops.d.ts +14 -0
- package/dist/platform/ops/agent-templates.ops.js +92 -0
- package/dist/platform/ops/agents.ops.d.ts +19 -0
- package/dist/platform/ops/agents.ops.js +294 -0
- package/dist/platform/ops/connectors.ops.d.ts +15 -0
- package/dist/platform/ops/connectors.ops.js +126 -0
- package/dist/platform/ops/errors.d.ts +11 -0
- package/dist/platform/ops/errors.js +22 -0
- package/dist/platform/ops/extensions.ops.d.ts +19 -0
- package/dist/platform/ops/extensions.ops.js +116 -0
- package/dist/platform/ops/marketplaces.ops.d.ts +16 -0
- package/dist/platform/ops/marketplaces.ops.js +157 -0
- package/dist/platform/ops/paging.d.ts +14 -0
- package/dist/platform/ops/paging.js +20 -0
- package/dist/platform/ops/platform.ops.d.ts +23 -0
- package/dist/platform/ops/platform.ops.js +158 -0
- package/dist/platform/ops/plugins.ops.d.ts +33 -0
- package/dist/platform/ops/plugins.ops.js +377 -0
- package/dist/platform/ops/requests.ops.d.ts +14 -0
- package/dist/platform/ops/requests.ops.js +153 -0
- package/dist/platform/ops/role.d.ts +4 -0
- package/dist/platform/ops/role.js +10 -0
- package/dist/platform/ops/skills.ops.d.ts +13 -0
- package/dist/platform/ops/skills.ops.js +98 -0
- package/dist/platform/ops/summary.d.ts +10 -0
- package/dist/platform/ops/summary.js +18 -0
- package/dist/platform/platform-ops.module.d.ts +7 -0
- package/dist/platform/platform-ops.module.js +55 -0
- package/dist/platform/platform.module.d.ts +8 -0
- package/dist/platform/platform.module.js +25 -0
- package/dist/platform/redact-orgs.d.ts +7 -0
- package/dist/platform/redact-orgs.js +52 -0
- package/dist/platform/registry.d.ts +30 -0
- package/dist/platform/registry.js +111 -0
- package/dist/roles/worker-role.d.ts +6 -4
- package/dist/roles/worker-role.js +14 -7
- package/dist/roles/worker.module.d.ts +1 -1
- package/dist/roles/worker.module.js +15 -1
- package/dist/runtime/context/build.d.ts +11 -7
- package/dist/runtime/context/build.js +46 -19
- package/dist/runtime/context/claude-tools.d.ts +3 -1
- package/dist/runtime/context/claude-tools.js +8 -6
- package/dist/runtime/context/compaction.js +13 -0
- package/dist/runtime/context/reminders.d.ts +2 -0
- package/dist/runtime/context/reminders.js +2 -0
- package/dist/runtime/context/skills.js +9 -2
- package/dist/runtime/context/tool-changes.d.ts +27 -0
- package/dist/runtime/context/tool-changes.js +60 -0
- package/dist/runtime/hook-calls.js +2 -0
- package/dist/runtime/langgraph-loop.d.ts +3 -1
- package/dist/runtime/langgraph-loop.js +36 -17
- package/dist/runtime/loop.d.ts +34 -5
- package/dist/runtime/maintenance.d.ts +9 -4
- package/dist/runtime/maintenance.js +35 -8
- package/dist/runtime/model-call.d.ts +46 -8
- package/dist/runtime/model-call.js +72 -21
- package/dist/runtime/providers.d.ts +6 -0
- package/dist/runtime/providers.js +8 -0
- package/dist/runtime/run-control.d.ts +11 -1
- package/dist/runtime/run-control.js +12 -0
- package/dist/runtime/run-drafts.d.ts +85 -0
- package/dist/runtime/run-drafts.js +230 -0
- package/dist/runtime/run-jobs.d.ts +3 -5
- package/dist/runtime/run-jobs.js +3 -7
- package/dist/runtime/run-queue.d.ts +59 -2
- package/dist/runtime/run-queue.js +311 -38
- package/dist/runtime/run-worker.d.ts +11 -5
- package/dist/runtime/run-worker.js +69 -33
- package/dist/runtime/runtime.module.js +11 -3
- package/dist/runtime/step-log.d.ts +15 -4
- package/dist/runtime/step-log.js +24 -7
- package/dist/runtime/stream.controller.d.ts +23 -1
- package/dist/runtime/stream.controller.js +60 -3
- package/dist/runtime/stream.d.ts +4 -1
- package/dist/runtime/stream.js +27 -3
- package/dist/runtime/subagents/child.d.ts +55 -7
- package/dist/runtime/subagents/child.js +105 -7
- package/dist/runtime/subagents/definition.d.ts +12 -3
- package/dist/runtime/subagents/definition.js +44 -28
- package/dist/runtime/subagents/deliveries.d.ts +71 -0
- package/dist/runtime/subagents/deliveries.js +228 -0
- package/dist/runtime/subagents/dispatch.d.ts +115 -26
- package/dist/runtime/subagents/dispatch.js +212 -141
- package/dist/runtime/subagents/limits.d.ts +24 -5
- package/dist/runtime/subagents/limits.js +40 -6
- package/dist/runtime/subagents/messages.d.ts +95 -0
- package/dist/runtime/subagents/messages.js +328 -0
- package/dist/runtime/subagents/preamble.d.ts +4 -4
- package/dist/runtime/subagents/preamble.js +9 -7
- package/dist/runtime/subagents/progress.d.ts +8 -6
- package/dist/runtime/subagents/progress.js +11 -6
- package/dist/runtime/subagents/render.d.ts +30 -0
- package/dist/runtime/subagents/render.js +53 -0
- package/dist/runtime/subagents/roster.d.ts +24 -7
- package/dist/runtime/subagents/roster.js +47 -11
- package/dist/runtime/subagents/skill-fork.d.ts +3 -2
- package/dist/runtime/subagents/skill-fork.js +8 -4
- package/dist/runtime/subagents/status.d.ts +14 -0
- package/dist/runtime/subagents/status.js +23 -0
- package/dist/runtime/subagents/subagents.service.d.ts +7 -4
- package/dist/runtime/subagents/subagents.service.js +11 -6
- package/dist/runtime/usage-report.d.ts +2 -1
- package/dist/runtime/usage-report.js +19 -13
- package/dist/runtime/waits.d.ts +145 -36
- package/dist/runtime/waits.js +423 -109
- package/dist/sandbox/jobs.d.ts +40 -13
- package/dist/sandbox/jobs.js +57 -20
- package/dist/sandbox/leases.d.ts +37 -54
- package/dist/sandbox/leases.js +71 -115
- package/dist/sandbox/providers.d.ts +2 -2
- package/dist/sandbox/providers.js +1 -1
- package/dist/sandbox/sandbox.module.d.ts +3 -2
- package/dist/sandbox/sandbox.module.js +10 -4
- package/dist/sandbox/sandbox.service.d.ts +282 -129
- package/dist/sandbox/sandbox.service.js +736 -381
- package/dist/skills/catalog.d.ts +3 -2
- package/dist/skills/catalog.js +21 -43
- package/dist/skills/changes.controller.d.ts +7 -0
- package/dist/skills/changes.controller.js +19 -1
- package/dist/skills/invoke.js +8 -3
- package/dist/skills/package.d.ts +20 -1
- package/dist/skills/package.js +28 -1
- package/dist/skills/skills.controller.d.ts +39 -4
- package/dist/skills/skills.controller.js +88 -10
- package/dist/skills/skills.module.js +10 -1
- package/dist/skills/skills.service.d.ts +106 -26
- package/dist/skills/skills.service.js +421 -75
- package/dist/skills/usable.d.ts +81 -0
- package/dist/skills/usable.js +379 -0
- package/dist/tools/approvals.service.d.ts +20 -2
- package/dist/tools/approvals.service.js +25 -4
- package/dist/tools/builtin-names.d.ts +41 -7
- package/dist/tools/builtin-names.js +59 -7
- package/dist/tools/builtins/agent.d.ts +23 -21
- package/dist/tools/builtins/agent.js +62 -55
- package/dist/tools/builtins/builtin-tools.d.ts +19 -1
- package/dist/tools/builtins/builtin-tools.js +59 -2
- package/dist/tools/builtins/handback.d.ts +28 -0
- package/dist/tools/builtins/handback.js +53 -0
- package/dist/tools/builtins/platform.d.ts +34 -0
- package/dist/tools/builtins/platform.js +362 -0
- package/dist/tools/builtins/sandbox/bash.d.ts +1 -1
- package/dist/tools/builtins/sandbox/bash.js +241 -4
- package/dist/tools/builtins/sandbox/common.d.ts +69 -27
- package/dist/tools/builtins/sandbox/common.js +87 -48
- package/dist/tools/builtins/sandbox/deliver.js +1 -1
- package/dist/tools/builtins/sandbox/edit.js +2 -2
- package/dist/tools/builtins/sandbox/glob.js +2 -2
- package/dist/tools/builtins/sandbox/grep.js +2 -4
- package/dist/tools/builtins/sandbox/index.d.ts +3 -1
- package/dist/tools/builtins/sandbox/index.js +7 -1
- package/dist/tools/builtins/sandbox/read.js +1 -1
- package/dist/tools/builtins/sandbox/task-output.d.ts +74 -0
- package/dist/tools/builtins/sandbox/task-output.js +329 -0
- package/dist/tools/builtins/sandbox/task-stop.d.ts +10 -0
- package/dist/tools/builtins/sandbox/task-stop.js +113 -0
- package/dist/tools/builtins/sandbox/write.js +2 -2
- package/dist/tools/builtins/send-message.d.ts +56 -0
- package/dist/tools/builtins/send-message.js +100 -0
- package/dist/tools/builtins/skill-load.d.ts +22 -14
- package/dist/tools/builtins/skill-load.js +86 -57
- package/dist/tools/builtins/skill-write.js +1 -1
- package/dist/tools/builtins/web/errors.d.ts +8 -0
- package/dist/tools/builtins/web/errors.js +53 -0
- package/dist/tools/builtins/web/extract.d.ts +62 -0
- package/dist/tools/builtins/web/extract.js +271 -0
- package/dist/tools/builtins/web/fetch.d.ts +55 -0
- package/dist/tools/builtins/web/fetch.js +175 -0
- package/dist/tools/builtins/web/index.d.ts +13 -0
- package/dist/tools/builtins/web/index.js +12 -0
- package/dist/tools/builtins/web/page-cache.d.ts +25 -0
- package/dist/tools/builtins/web/page-cache.js +77 -0
- package/dist/tools/builtins/web/search.d.ts +25 -0
- package/dist/tools/builtins/web/search.js +210 -0
- package/dist/tools/builtins/web/url.d.ts +17 -0
- package/dist/tools/builtins/web/url.js +43 -0
- package/dist/tools/connectors.controller.d.ts +10 -0
- package/dist/tools/connectors.controller.js +12 -0
- package/dist/tools/connectors.service.d.ts +46 -5
- package/dist/tools/connectors.service.js +94 -15
- package/dist/tools/extension-tools.controller.d.ts +14 -0
- package/dist/tools/extension-tools.controller.js +37 -0
- package/dist/tools/info.d.ts +42 -1
- package/dist/tools/info.js +64 -1
- package/dist/tools/oauth/flow-paths.d.ts +1 -1
- package/dist/tools/oauth/flow-paths.js +1 -1
- package/dist/tools/org-tool-switches.d.ts +74 -0
- package/dist/tools/org-tool-switches.js +184 -0
- package/dist/tools/pipeline.d.ts +46 -40
- package/dist/tools/pipeline.js +202 -364
- package/dist/tools/policy.d.ts +1 -1
- package/dist/tools/policy.js +36 -16
- package/dist/tools/registry.d.ts +120 -8
- package/dist/tools/registry.js +112 -19
- package/dist/tools/tools.module.js +24 -2
- package/dist/tools/validate.js +1 -1
- package/dist/ui/views.d.ts +2 -1
- package/dist/ui/views.js +17 -3
- package/package.json +3 -3
- package/src/migrations/0064_account_last_org.sql +7 -0
- package/src/migrations/0065_conversation_archive.sql +8 -0
- package/src/migrations/0066_extension_display_name.sql +4 -0
- package/src/migrations/0067_agent_profiles.sql +20 -0
- package/src/migrations/0068_agent_avatar_uploads.sql +7 -0
- package/src/migrations/0069_attachments_agent_idx.sql +3 -0
- package/src/migrations/0070_background_tasks.sql +37 -0
- package/src/migrations/0071_background_tasks_reserve.sql +6 -0
- package/src/migrations/0072_background_tasks_stopping.sql +5 -0
- package/src/migrations/0073_org_extension_point_defaults.sql +11 -0
- package/src/migrations/0074_subagent_tree.sql +50 -0
- package/src/migrations/0075_plugin_scope.sql +61 -0
- package/src/migrations/0076_skills_created_by_nullable.sql +6 -0
- package/src/migrations/0077_scope_requests.sql +52 -0
- package/src/migrations/0078_scope_grant_switches.sql +18 -0
- package/src/migrations/0079_scope_live_install_switches.sql +19 -0
- package/src/migrations/0080_rename_enterprise_admin.sql +18 -0
- package/src/migrations/0081_platform_components.sql +129 -0
- package/src/migrations/0082_platform_migration.sql +33 -0
- package/src/migrations/0083_plugin_scope_sync_failures.sql +15 -0
- package/src/migrations/0084_org_owned_items.sql +85 -0
- package/src/migrations/0085_agent_templates_phase4.sql +45 -0
- package/src/migrations/0086_org_layering.sql +30 -0
- package/src/migrations/0087_approval_summary.sql +6 -0
- package/src/migrations/0088_environments.sql +80 -0
- package/dist/catalog/upgrade-targets.d.ts +0 -56
- package/dist/catalog/upgrade-targets.js +0 -90
- package/dist/conversations/sandbox-status.d.ts +0 -24
- package/dist/conversations/sandbox-status.js +0 -52
- package/dist/extensions/org-config.controller.d.ts +0 -24
- package/dist/extensions/org-config.controller.js +0 -62
|
@@ -7,37 +7,60 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
// The platform plugin update (plugin and component management spec §7, plugin scope phase 2,
|
|
11
|
+
// Task 7): a platform plugin has one version for the deployment, so an update is previewed and
|
|
12
|
+
// confirmed once by the super admin, per component. The per-org upgrade routes are retired (ruling
|
|
13
|
+
// F2).
|
|
14
|
+
import { createHash } from "node:crypto";
|
|
15
|
+
import { SDK_VERSION } from "@capybara-hq/plugin-sdk";
|
|
11
16
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
17
|
import { sql } from "kysely";
|
|
13
|
-
import { putTemplateVersionIn } from "../agents/templates.js";
|
|
14
18
|
import { ApiError } from "../errors.js";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
19
|
+
import { ExtensionAdminOps } from "../extensions/admin-ops.js";
|
|
20
|
+
import { ExtensionBlobs } from "../extensions/blobs.js";
|
|
21
|
+
import { permissionSetOfPackage, readExtensionPackage } from "../extensions/package.js";
|
|
22
|
+
import { blockingKeys, diffPermissions } from "../extensions/permission-set.js";
|
|
17
23
|
import { ExtensionRegistry } from "../extensions/registry.js";
|
|
18
24
|
import { audit } from "../identity/audit.js";
|
|
25
|
+
import { SuperAdminLock } from "../identity/super-admin.js";
|
|
26
|
+
import { addOrgNotice, itemDisplayName, notifySharedUpdate } from "../inbox/org-notices.js";
|
|
27
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
28
|
+
import { afterCommit } from "../infra/database/after-commit.js";
|
|
19
29
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
20
30
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
21
31
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
32
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
22
33
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
23
34
|
import { OrgLock } from "../orgs/org-lock.js";
|
|
24
35
|
import { packagePath } from "../packages/tgz.js";
|
|
25
|
-
import { SkillsService, skillConflictError } from "../skills/skills.service.js";
|
|
26
36
|
import { StorageService } from "../storage/storage.service.js";
|
|
37
|
+
import { canonicalJson } from "../tools/canonical-json.js";
|
|
27
38
|
import { ConnectorsService } from "../tools/connectors.service.js";
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
39
|
+
import { dirDigest, getPath, readCatalogAgentTemplates, readCatalogTemplates, setPath, skillFilesDigest, templatePluginProblem, } from "./catalog-content.js";
|
|
40
|
+
import { markVersionApplied, versionUsable } from "./catalog-items.js";
|
|
41
|
+
import { agentsUsing, clashingOrgSkills, clashingPlatformSkills, componentOn, connectorTemplatesOn, listedIn, loadScopeState, pluginFactsOf, switchOf, } from "./component-state.js";
|
|
42
|
+
import { CatalogItemLock } from "./locks.js";
|
|
43
|
+
import { itemPendingError, itemRemovedError } from "./ownership.js";
|
|
44
|
+
import { PlanStale, PlatformComponents } from "./platform-components.js";
|
|
45
|
+
import { catalogSkillNames, PluginInstaller } from "./plugin-install.js";
|
|
46
|
+
import { PluginScopeSync } from "./scope-sync.js";
|
|
47
|
+
import { compareVersions, satisfiesRange } from "./versions.js";
|
|
34
48
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
35
|
-
const installNotFound = () => new ApiError("plugin.install_not_found", "Plugin install not found");
|
|
36
|
-
const inProgress = () => new ApiError("plugin.install_in_progress", "This plugin install is being installed, upgraded or uninstalled; try again when it finishes");
|
|
37
|
-
const stale = () => new ApiError("plugin.upgrade_stale", "The upgrade preview is out of date (the catalog version or this install's components changed); preview the upgrade again");
|
|
38
|
-
const targetInvalid = (message, details) => new ApiError("plugin.upgrade_target_invalid", message, details);
|
|
39
|
-
const fail = (path, message) => new ApiError("validation_failed", "Request validation failed", [{ path, message }]);
|
|
40
49
|
const sha256 = (s) => createHash("sha256").update(s).digest("hex");
|
|
50
|
+
const itemNotFound = () => new ApiError("catalog.item_not_found", "Catalog item not found");
|
|
51
|
+
const stale = () => new ApiError("plugin.upgrade_stale", "The update preview is out of date (the catalog version, the platform components, a pin or a connector changed); preview the update again");
|
|
52
|
+
/** The pending version a super admin confirms was replaced by another upload (P3-11). */
|
|
53
|
+
const pendingStale = (name) => new ApiError("extension.approval_stale", `The pending upload of ${name} changed since it was reviewed; review it again`);
|
|
54
|
+
const targetInvalid = (message, details) => new ApiError("plugin.upgrade_target_invalid", message, details);
|
|
55
|
+
/** A version an org uploaded that was never applied or confirmed, or was rejected (P3-37, P3-52). */
|
|
56
|
+
const notApplied = (name, version) => targetInvalid(`Version ${version} of plugin ${name} was uploaded by an organization and never applied or confirmed (or was rejected); the generic update cannot apply it`, [{ reason: "org_version_not_applied", version }]);
|
|
57
|
+
const KIND_ORDER = {
|
|
58
|
+
skill: 0,
|
|
59
|
+
agent_template: 1,
|
|
60
|
+
connector_template: 2,
|
|
61
|
+
};
|
|
62
|
+
/** How long one update holds its item (renewed while it runs). */
|
|
63
|
+
const UPDATE_LOCK_TTL_MS = 60_000;
|
|
41
64
|
/**
|
|
42
65
|
* What a credential of `auth` looks like: two auths with the same key take the same stored
|
|
43
66
|
* credential (static: the type family, ownership and header names; OAuth: any change re-enters).
|
|
@@ -58,725 +81,646 @@ function credentialKey(auth) {
|
|
|
58
81
|
}
|
|
59
82
|
}
|
|
60
83
|
/** The digest of all files of a catalog version. */
|
|
61
|
-
const filesDigest = (row) =>
|
|
62
|
-
.map((f) => `${f.path}\0${f.sha256}\n`)
|
|
63
|
-
.sort()
|
|
64
|
-
.join(""));
|
|
65
|
-
/** The digest of the files under `dir/` of a catalog version (paths relative to it). */
|
|
66
|
-
function dirDigest(row, dir) {
|
|
67
|
-
const prefix = `${dir}/`;
|
|
68
|
-
return sha256(row.files
|
|
69
|
-
.filter((f) => f.path.startsWith(prefix))
|
|
70
|
-
.map((f) => `${f.path.slice(prefix.length)}\0${f.sha256}\n`)
|
|
71
|
-
.sort()
|
|
72
|
-
.join(""));
|
|
73
|
-
}
|
|
84
|
+
const filesDigest = (row) => skillFilesDigest(row.files.map((f) => ({ path: f.path, sha256: f.sha256 })));
|
|
74
85
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* `
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
86
|
+
* The platform plugin update (spec §7; ruling F2 retires the per-org upgrades):
|
|
87
|
+
* - **preview** diffs the platform's current components of the plugin (the version recorded in
|
|
88
|
+
* `catalog_items.platform_synced_version`, components updated on their own included) against a
|
|
89
|
+
* catalog version: each skill (by name, by its files' digest), agent template (by name, canonical
|
|
90
|
+
* definition) and connector template (by id, canonical file) is `added`, `removed`, `same`, or
|
|
91
|
+
* `upgrade` / `downgrade` (the target's direction from the component's catalog version). It marks
|
|
92
|
+
* components updated on their own, components with their own scope and the orgs whose linked
|
|
93
|
+
* copy is pinned; lists removed components agents still use (they stay, spec §7.3), the org
|
|
94
|
+
* connectors made from changed templates (all orgs, ruling E2; `credentialsReset` when the
|
|
95
|
+
* credential shape changed) and the added templates that will need an org's input, the
|
|
96
|
+
* deployment's extension move, and a `diffHash`. Refused: same version, a disabled or deleted
|
|
97
|
+
* item, a target without the extension the deployment runs for it or moving a config-declared
|
|
98
|
+
* one, a template moving to another connector, a plugin another platform plugin depends on
|
|
99
|
+
* outside its range, and an added skill whose name is taken where the plugin is on (rule 10).
|
|
100
|
+
* - **update** runs under a Redis lock per item (another update -> `plugin.install_in_progress`):
|
|
101
|
+
* the extension first (`ExtensionAdminOps.install`, enabled when the update installs it; added
|
|
102
|
+
* permissions wait for the admin's approval as for any extension upgrade), then one transaction
|
|
103
|
+
* under the item name's lock that recomputes the diff (`plugin.upgrade_stale` on a difference)
|
|
104
|
+
* and writes the platform versions (`PlatformComponents.syncFromVersion`, which records
|
|
105
|
+
* `platform_synced_version`), the pinned suggestions of unchanged skills, the removals (a
|
|
106
|
+
* component no agent uses is soft-deleted, so unpinned linked copies go with it; pinned copies
|
|
107
|
+
* keep their own version; a removed connector template leaves its connectors as the orgs' own),
|
|
108
|
+
* clears the pins of linked copies, fans changed templates out to every connector made from them
|
|
109
|
+
* (`applyTemplateIn`, under each org's row lock, credentials cleared only for a changed
|
|
110
|
+
* credential shape), moves every org install to the version and audits
|
|
111
|
+
* `plugin.platform_updated`; the scope sync of the item and its dependencies is enqueued after
|
|
112
|
+
* the commit (orgs get new dependencies by rule 9). A failure rolls the transaction back and
|
|
113
|
+
* puts the extension back where it was (audit `plugin.platform_update_failed`, 502
|
|
114
|
+
* `plugin.upgrade_failed`); files copied to `deployment/platform/` stay unreferenced.
|
|
115
|
+
* Storage is never read or written inside the transaction: the target's contents (and the
|
|
116
|
+
* extension package, for `needsApproval`) are read and the platform files copied
|
|
117
|
+
* (`PlatformComponents.prepareSync`) after the Redis lock is taken and before the transaction,
|
|
118
|
+
* which reuses them and writes rows only (`applySyncIn`). The confirm-time diff treats the
|
|
119
|
+
* extension this update moved as expected (the install record must point at that package).
|
|
120
|
+
* Recovery: a process that dies between the extension step and the transaction leaves the
|
|
121
|
+
* deployment on the target's extension with the platform components unchanged; a new preview
|
|
122
|
+
* then shows the extension unchanged (`extension: null`) and the components still to move, and
|
|
123
|
+
* confirming it completes the update (or an update back to the old version moves it back).
|
|
124
|
+
* No in-process state.
|
|
98
125
|
*/
|
|
99
|
-
let
|
|
100
|
-
installer;
|
|
126
|
+
let PlatformPluginUpdater = class PlatformPluginUpdater {
|
|
101
127
|
kdb;
|
|
128
|
+
txHost;
|
|
129
|
+
adminLock;
|
|
130
|
+
itemLock;
|
|
102
131
|
orgLock;
|
|
132
|
+
locks;
|
|
103
133
|
storage;
|
|
104
134
|
registry;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
135
|
+
ops;
|
|
136
|
+
blobs;
|
|
137
|
+
config;
|
|
138
|
+
platform;
|
|
139
|
+
installer;
|
|
108
140
|
connectors;
|
|
141
|
+
sync;
|
|
109
142
|
log;
|
|
110
|
-
txHost
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
orgLock, storage, registry, skills, orgConfigs, requests,
|
|
116
|
-
/** Org connectors: installs create, upgrade and remove them. */
|
|
117
|
-
connectors, log, txHost) {
|
|
118
|
-
this.installer = installer;
|
|
143
|
+
constructor(kdb, txHost, adminLock, itemLock, orgLock, locks, storage, registry, ops,
|
|
144
|
+
/** Reads the target's extension package (its permissions, for `needsApproval`). */
|
|
145
|
+
blobs, config, platform,
|
|
146
|
+
/** Template dependencies (the deployed connector kinds). */
|
|
147
|
+
installer, connectors, sync, log) {
|
|
119
148
|
this.kdb = kdb;
|
|
149
|
+
this.txHost = txHost;
|
|
150
|
+
this.adminLock = adminLock;
|
|
151
|
+
this.itemLock = itemLock;
|
|
120
152
|
this.orgLock = orgLock;
|
|
153
|
+
this.locks = locks;
|
|
121
154
|
this.storage = storage;
|
|
122
155
|
this.registry = registry;
|
|
123
|
-
this.
|
|
124
|
-
this.
|
|
125
|
-
this.
|
|
156
|
+
this.ops = ops;
|
|
157
|
+
this.blobs = blobs;
|
|
158
|
+
this.config = config;
|
|
159
|
+
this.platform = platform;
|
|
160
|
+
this.installer = installer;
|
|
126
161
|
this.connectors = connectors;
|
|
162
|
+
this.sync = sync;
|
|
127
163
|
this.log = log;
|
|
128
|
-
this.txHost = txHost;
|
|
129
164
|
}
|
|
130
|
-
/** The kernel-schema handle of the current context (the transaction, else the pool). */
|
|
131
165
|
get db() {
|
|
132
166
|
return this.kdb.db;
|
|
133
167
|
}
|
|
134
|
-
/** The
|
|
135
|
-
async preview(
|
|
136
|
-
await this.
|
|
137
|
-
const diff = await this.diff(orgId, actor, installId, version, null);
|
|
138
|
-
await this.authorizeActions(orgId, actor, diff.actions);
|
|
139
|
-
return {
|
|
140
|
-
from: diff.from.version,
|
|
141
|
-
to: diff.to.version,
|
|
142
|
-
diffHash: diff.diffHash,
|
|
143
|
-
components: diff.components,
|
|
144
|
-
inputs: diff.inputs,
|
|
145
|
-
templates: diff.templates.flatMap((c) => c.target !== undefined &&
|
|
146
|
-
(diff.inputs.connectors.includes(c.id) || diff.inputs.credentialsChanged.includes(c.id))
|
|
147
|
-
? [orgTemplateView(c.id, c.target)]
|
|
148
|
-
: []),
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
/** Re-checks the actor's org actions under the org row lock (a transaction of its own). */
|
|
152
|
-
async authorizeActions(orgId, actor, actions) {
|
|
153
|
-
await lockOrgForActions(this.orgLock, orgId, actor, actions);
|
|
168
|
+
/** The update preview (see the class comment); writes nothing. */
|
|
169
|
+
async preview(itemId, version) {
|
|
170
|
+
return previewOf(await this.diff(itemId, version));
|
|
154
171
|
}
|
|
155
172
|
/**
|
|
156
|
-
* The
|
|
157
|
-
*
|
|
158
|
-
*
|
|
173
|
+
* The update preview of a new version of an org's own plugin (ruling P3-12): the item must be
|
|
174
|
+
* owned by `orgId` (else 404, as for any item the org cannot see), the target carries no
|
|
175
|
+
* extension (an extension-bearing version waits for the super admin, plan Task 6), and a target
|
|
176
|
+
* dropping the deployment's extension is allowed (P3-12'). `staged` is the target read from the
|
|
177
|
+
* org's staging (pre-flight F3); without it the imported version `version` is read. The route
|
|
178
|
+
* authorizes the org admin; writes nothing.
|
|
159
179
|
*/
|
|
160
|
-
async
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
throw
|
|
165
|
-
|
|
180
|
+
async previewForOwner(orgId, itemId, version, staged) {
|
|
181
|
+
// An internal invariant, not reachable from input: the only caller (OrgPluginUploads) passes
|
|
182
|
+
// the staged target together with its own version (PluginImport.stagedTarget)
|
|
183
|
+
if (staged !== undefined && staged.version !== version) {
|
|
184
|
+
throw new Error(`internal: previewForOwner got the staged target of version ${staged.version} for version ${version}`);
|
|
185
|
+
}
|
|
186
|
+
const preview = previewOf(await this.diff(itemId, version, {
|
|
187
|
+
owner: { orgId },
|
|
188
|
+
...(staged !== undefined && { target: staged }),
|
|
189
|
+
}));
|
|
190
|
+
return { ...preview, orgNames: await this.orgNamesOf(preview) };
|
|
166
191
|
}
|
|
167
192
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
193
|
+
* Locks, in sorted order and before the item (ruling P3-50), every live org the update may
|
|
194
|
+
* write in: the orgs with connectors made from the plugin's connector templates and the item's
|
|
195
|
+
* owner org. `acting`: the owner org an owner update acts for, locked with its admin re-checked
|
|
196
|
+
* (`catalog.upload`; deleted -> 404 `org.not_found`). Returns the live orgs locked; another
|
|
197
|
+
* deleted one is skipped.
|
|
171
198
|
*/
|
|
172
|
-
async
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
done.push(s);
|
|
197
|
-
unjournaled = s;
|
|
198
|
-
const r = await this.db
|
|
199
|
-
.updateTable("plugin_installs")
|
|
200
|
-
.set({
|
|
201
|
-
upgrade: sql `jsonb_set(upgrade, '{done}', (upgrade->'done') || ${JSON.stringify([s])}::jsonb)`,
|
|
202
|
-
})
|
|
203
|
-
.where("id", "=", installId)
|
|
204
|
-
.where(sql `upgrade->>'id'`, "=", upgradeId)
|
|
205
|
-
.executeTakeFirst();
|
|
206
|
-
if (r.numUpdatedRows === 0n)
|
|
207
|
-
throw cleanedUp();
|
|
208
|
-
unjournaled = undefined;
|
|
209
|
-
};
|
|
210
|
-
// Records a component in the ledger and the journal, in the transaction of the context
|
|
211
|
-
const record = (c) => this.recordIn(installId, journal, c);
|
|
212
|
-
let step = "dependencies";
|
|
213
|
-
const disabled = [];
|
|
214
|
-
try {
|
|
215
|
-
for (const dep of diff.dependencies.filter((x) => x.change === "added")) {
|
|
216
|
-
const resolved = dep.resolved;
|
|
217
|
-
const rec = (c) => record(c);
|
|
218
|
-
if ((await this.installer.recordDependency(orgId, resolved, rec)) !== undefined)
|
|
219
|
-
continue;
|
|
220
|
-
if (!dep.plan) {
|
|
221
|
-
throw new ApiError("catalog.dependency_unresolved", `Dependency ${dep.name}@${resolved.range} cannot be resolved: its install in this organization was removed meanwhile; try again`, [{ dependency: dep.name, range: resolved.range }]);
|
|
222
|
-
}
|
|
223
|
-
let child;
|
|
224
|
-
try {
|
|
225
|
-
child = await this.installer.execute(orgId, actor, dep.plan, opts, {
|
|
226
|
-
actions: requiredActions(dep.plan.manifest),
|
|
227
|
-
approvedBy: itemId,
|
|
228
|
-
parent: installId,
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
catch (e) {
|
|
232
|
-
if (e instanceof InstallFailure) {
|
|
233
|
-
throw new InstallFailure(e.step, e.cause, [dep.name, ...e.dependency], e.rolledBack);
|
|
234
|
-
}
|
|
235
|
-
if (e instanceof ApiError &&
|
|
236
|
-
e.code === "plugin.already_installed" &&
|
|
237
|
-
(await this.installer.recordDependency(orgId, resolved, rec)) !== undefined) {
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
throw new InstallFailure("dependencies", e, [dep.name], true);
|
|
241
|
-
}
|
|
242
|
-
await this.installer.recordDependency(orgId, resolved, rec, child);
|
|
243
|
-
}
|
|
244
|
-
step = "extension";
|
|
245
|
-
if (diff.extension?.change === "added") {
|
|
246
|
-
await this.installer.enableExtension(orgId, diff.extension.name, {
|
|
247
|
-
enabled: true,
|
|
248
|
-
...(input.extensionConfig !== undefined && { config: input.extensionConfig }),
|
|
249
|
-
}, actor, opts.actorTokenId, { viaItem: itemId }, record);
|
|
250
|
-
}
|
|
251
|
-
step = "skills";
|
|
252
|
-
for (const s of diff.skills) {
|
|
253
|
-
if (s.change !== "added" && s.change !== "changed")
|
|
254
|
-
continue;
|
|
255
|
-
const files = await readCatalogFiles(this.storage, diff.to.object_prefix, diff.to.files, s.dir);
|
|
256
|
-
const r = await this.skills.createVersionFromFiles(orgId, actor, { ...skillOf(files), files }, {
|
|
257
|
-
catalog: {
|
|
258
|
-
itemId,
|
|
259
|
-
version: diff.to.version,
|
|
260
|
-
format: diff.to.source_format,
|
|
261
|
-
suggestPinned: s.suggestPinned === true,
|
|
262
|
-
},
|
|
263
|
-
...opts,
|
|
264
|
-
});
|
|
265
|
-
await this.journalSkill(r, record, journal);
|
|
266
|
-
}
|
|
267
|
-
step = "agent_templates";
|
|
268
|
-
for (const a of diff.agentTemplates) {
|
|
269
|
-
if (a.change !== "added" && a.change !== "changed")
|
|
270
|
-
continue;
|
|
271
|
-
await this.upgradeAgentTemplate(orgId, diff, a.template, record, journal);
|
|
199
|
+
async lockUpdateOrgs(itemId, acting) {
|
|
200
|
+
const item = await this.db
|
|
201
|
+
.selectFrom("catalog_items")
|
|
202
|
+
.select("owner_org_id")
|
|
203
|
+
.where("id", "=", itemId)
|
|
204
|
+
.executeTakeFirst();
|
|
205
|
+
const made = await this.db
|
|
206
|
+
.selectFrom("connectors as c")
|
|
207
|
+
.innerJoin("platform_connector_templates as t", "t.id", "c.platform_template_id")
|
|
208
|
+
.select("c.org_id")
|
|
209
|
+
.distinct()
|
|
210
|
+
.where("t.catalog_item_id", "=", itemId)
|
|
211
|
+
.where("c.deleted_at", "is", null)
|
|
212
|
+
.execute();
|
|
213
|
+
const ids = new Set(made.map((r) => r.org_id));
|
|
214
|
+
if (item?.owner_org_id)
|
|
215
|
+
ids.add(item.owner_org_id);
|
|
216
|
+
if (acting !== undefined)
|
|
217
|
+
ids.add(acting.orgId);
|
|
218
|
+
const live = new Set();
|
|
219
|
+
for (const orgId of [...ids].sort()) {
|
|
220
|
+
if (orgId === acting?.orgId) {
|
|
221
|
+
await this.orgLock.lockAndAuthorize(orgId, acting.actor, "catalog.upload");
|
|
222
|
+
live.add(orgId);
|
|
272
223
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const r = await this.connectors.create(orgId, actor, ci, opts.actorTokenId);
|
|
276
|
-
await this.recordStep(installId, journal, {
|
|
277
|
-
kind: "connector",
|
|
278
|
-
ref: r.connector.id,
|
|
279
|
-
created: true,
|
|
280
|
-
skillVersion: null,
|
|
281
|
-
templateId,
|
|
282
|
-
});
|
|
224
|
+
else if (await this.orgLock.tryLock(orgId)) {
|
|
225
|
+
live.add(orgId);
|
|
283
226
|
}
|
|
284
|
-
const prepared = [];
|
|
285
|
-
for (const tpl of diff.templates) {
|
|
286
|
-
const update = diff.updates.get(tpl.id);
|
|
287
|
-
if (!update || !tpl.connector)
|
|
288
|
-
continue;
|
|
289
|
-
try {
|
|
290
|
-
prepared.push({
|
|
291
|
-
connectorId: tpl.connector.id,
|
|
292
|
-
p: await this.connectors.prepareTemplateUpdate(actor, tpl.connector.connector, update),
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
catch (e) {
|
|
296
|
-
throw under(e, `connectors.${tpl.id}`);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
// Finish, in one transaction: changed connectors, removals, the version switch
|
|
300
|
-
await this.finish(orgId, actor, installId, upgradeId, diff, prepared, disabled, opts, () => {
|
|
301
|
-
step = "finish";
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
catch (e) {
|
|
305
|
-
throw await this.failed(orgId, actor, installId, upgradeId, diff, { done, unjournaled }, step, e, opts);
|
|
306
227
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
228
|
+
return live;
|
|
229
|
+
}
|
|
230
|
+
/** The names of the orgs a preview mentions, by id (an owner's preview, see `orgNames`). */
|
|
231
|
+
async orgNamesOf(p) {
|
|
232
|
+
const ids = new Set([
|
|
233
|
+
...(p.extension?.orgs ?? []),
|
|
234
|
+
...p.connectorOrgs.map((c) => c.orgId),
|
|
235
|
+
...p.components.flatMap((c) => [...c.pinnedOrgs, ...(c.kept ?? []).map((k) => k.orgId)]),
|
|
236
|
+
]);
|
|
237
|
+
if (ids.size === 0)
|
|
238
|
+
return {};
|
|
239
|
+
const rows = await this.db
|
|
240
|
+
.selectFrom("orgs")
|
|
241
|
+
.select(["id", "name"])
|
|
242
|
+
.where("id", "in", [...ids])
|
|
243
|
+
.where("deleted_at", "is", null)
|
|
244
|
+
.execute();
|
|
245
|
+
return Object.fromEntries(rows.map((r) => [r.id, r.name]));
|
|
313
246
|
}
|
|
314
247
|
/**
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
* the
|
|
320
|
-
*
|
|
248
|
+
* Applies a new version of an org's own plugin for its owner (ruling P3-12, see
|
|
249
|
+
* {@link previewForOwner}): the actor must be an admin of the owner org (`catalog.upload`,
|
|
250
|
+
* re-checked under the org row lock in the transaction), then the platform update as
|
|
251
|
+
* {@link update} does, also removing the extension when the target drops it. In the update's
|
|
252
|
+
* transaction the orgs using the shared plugin are told (`shared_item_updated`, one notice per
|
|
253
|
+
* plugin) and a pending version is cleared (pre-flight F7: the new version replaces it).
|
|
321
254
|
*/
|
|
322
|
-
async
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
255
|
+
async updateAsOwner(orgId, actor, itemId, version, diffHash, opts = {}) {
|
|
256
|
+
if (!UUID_RE.test(itemId))
|
|
257
|
+
throw itemNotFound();
|
|
258
|
+
await this.authorizeOwner(orgId, actor);
|
|
259
|
+
return this.locked(itemId, () => this.updateLocked(actor, itemId, { version, diffHash }, opts, { orgId }));
|
|
260
|
+
}
|
|
261
|
+
/** The actor is an admin of the owner org (a read of the membership under the org row lock). */
|
|
262
|
+
async authorizeOwner(orgId, actor) {
|
|
263
|
+
await this.orgLock.lockAndAuthorize(orgId, actor, "catalog.upload");
|
|
264
|
+
}
|
|
265
|
+
/** Runs `f` holding the item's update lock (another update -> `plugin.install_in_progress`). */
|
|
266
|
+
async locked(itemId, f) {
|
|
267
|
+
const handle = await this.locks.acquire(`plugin-update-${itemId}`, {
|
|
268
|
+
ttlMs: UPDATE_LOCK_TTL_MS,
|
|
269
|
+
});
|
|
270
|
+
if (!handle) {
|
|
271
|
+
throw new ApiError("plugin.install_in_progress", "This plugin is being updated; try again when that update finishes");
|
|
334
272
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
rolledBack = false;
|
|
338
|
-
this.log.warn("plugin upgrade rollback failed", {
|
|
339
|
-
orgId,
|
|
340
|
-
installId,
|
|
341
|
-
error: u instanceof Error ? u.message : String(u),
|
|
342
|
-
});
|
|
273
|
+
try {
|
|
274
|
+
return await f();
|
|
343
275
|
}
|
|
344
|
-
|
|
345
|
-
|
|
276
|
+
finally {
|
|
277
|
+
await handle.release();
|
|
346
278
|
}
|
|
347
|
-
const dependency = failure && failure.dependency.length > 0 ? failure.dependency : [];
|
|
348
|
-
const dep = dependency.length > 0 ? ` in dependency ${dependency.join(" > ")}` : "";
|
|
349
|
-
return new ApiError("plugin.upgrade_failed", rolledBack
|
|
350
|
-
? `Upgrading the plugin failed at step ${at}${dep} (${code}); this organization's components are back at version ${diff.from.version}`
|
|
351
|
-
: `Upgrading the plugin failed at step ${at}${dep} (${code}) and rolling back failed; it is undone after catalog.staleInstallMinutes`, [
|
|
352
|
-
{
|
|
353
|
-
step: at,
|
|
354
|
-
code,
|
|
355
|
-
...(dependency.length > 0 && { dependency: dependency.join(" > ") }),
|
|
356
|
-
...(cause instanceof ApiError && { message: cause.message }),
|
|
357
|
-
...(cause instanceof ApiError &&
|
|
358
|
-
cause.details !== undefined && { causeDetails: cause.details }),
|
|
359
|
-
rolledBack,
|
|
360
|
-
},
|
|
361
|
-
]);
|
|
362
279
|
}
|
|
363
280
|
/**
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
281
|
+
* The super admin's confirmation of an org upload's pending new version (rulings P3-10, P3-21,
|
|
282
|
+
* pre-flight F8): the update to `version` as {@link update} does it (the super admin's, the
|
|
283
|
+
* extension installed or upgraded on the deployment), with the version allowed although it is
|
|
284
|
+
* pending. Its transaction re-reads the item under the row lock: removed meanwhile -> 409
|
|
285
|
+
* `catalog.item_removed`, the pending version replaced -> 409 `extension.approval_stale` (both
|
|
286
|
+
* undo the extension step); then the pending state is cleared and the owner told
|
|
287
|
+
* (`upload_confirmed`). The caller checked the digest of the version's package.
|
|
367
288
|
*/
|
|
368
|
-
async
|
|
369
|
-
if (!UUID_RE.test(
|
|
370
|
-
throw
|
|
371
|
-
|
|
372
|
-
for (const r of removed) {
|
|
373
|
-
if (r.kind === "extension")
|
|
374
|
-
await this.orgConfigs.afterDisable(orgId, r.ref);
|
|
375
|
-
}
|
|
289
|
+
async updateToPending(actor, itemId, version, opts = {}) {
|
|
290
|
+
if (!UUID_RE.test(itemId))
|
|
291
|
+
throw itemNotFound();
|
|
292
|
+
return this.locked(itemId, () => this.updateToPendingLocked(actor, itemId, version, opts));
|
|
376
293
|
}
|
|
377
|
-
/**
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
.where("id", "=", installId)
|
|
386
|
-
.where("org_id", "=", orgId)
|
|
387
|
-
.forUpdate()
|
|
388
|
-
.executeTakeFirst();
|
|
389
|
-
if (!row || row.status === "failed")
|
|
390
|
-
throw installNotFound();
|
|
391
|
-
if (row.status !== "installed")
|
|
392
|
-
throw inProgress();
|
|
393
|
-
if (row.version !== diff.from.version ||
|
|
394
|
-
ledgerHash(await ledgerOf(t, installId)) !== diff.ledgerHash) {
|
|
395
|
-
throw stale();
|
|
396
|
-
}
|
|
397
|
-
await assertItemInstallable(t, orgId, itemId);
|
|
398
|
-
await t
|
|
399
|
-
.updateTable("plugin_installs")
|
|
400
|
-
.set({
|
|
401
|
-
status: "upgrading",
|
|
402
|
-
upgrade: sql `jsonb_build_object('id', ${upgradeId}::text, 'from', ${diff.from.version}::text, 'to', ${diff.to.version}::text, 'startedAt', now(), 'done', '[]'::jsonb)`,
|
|
403
|
-
})
|
|
404
|
-
.where("id", "=", installId)
|
|
405
|
-
.execute();
|
|
294
|
+
/**
|
|
295
|
+
* {@link updateToPending} for a caller already holding the item's update lock
|
|
296
|
+
* ({@link withItemLock}): the super admin's confirmation runs all its steps under it (ruling
|
|
297
|
+
* P3-35).
|
|
298
|
+
*/
|
|
299
|
+
async updateToPendingLocked(actor, itemId, version, opts = {}) {
|
|
300
|
+
const d = await this.diff(itemId, version, { confirmPending: version });
|
|
301
|
+
return this.updateLocked(actor, itemId, { version, diffHash: d.diffHash }, opts, undefined, version);
|
|
406
302
|
}
|
|
407
|
-
/**
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
303
|
+
/**
|
|
304
|
+
* Runs `f` holding the item's update lock (the one every platform update of the item takes;
|
|
305
|
+
* another holder -> 409 `plugin.install_in_progress`): the super admin's confirmation of an org
|
|
306
|
+
* upload runs every step under it (ruling P3-35).
|
|
307
|
+
*/
|
|
308
|
+
withItemLock(itemId, f) {
|
|
309
|
+
if (!UUID_RE.test(itemId))
|
|
310
|
+
throw itemNotFound();
|
|
311
|
+
return this.locked(itemId, f);
|
|
415
312
|
}
|
|
416
313
|
/**
|
|
417
|
-
*
|
|
418
|
-
*
|
|
314
|
+
* Runs an update confirmed with the preview's `diffHash` (see the class comment). A version an
|
|
315
|
+
* org upload waits with, or an item whose first version waits, -> 409 `catalog.item_pending`
|
|
316
|
+
* (confirmed in 待确认 instead, pre-flight F8).
|
|
419
317
|
*/
|
|
420
|
-
async
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
const r = await putTemplateVersionIn(t, {
|
|
425
|
-
orgId,
|
|
426
|
-
plugin: diff.name,
|
|
427
|
-
catalogItemId: itemId,
|
|
428
|
-
catalogVersion: diff.to.version,
|
|
429
|
-
sourceFormat: diff.to.source_format,
|
|
430
|
-
}, template);
|
|
431
|
-
if (r.created) {
|
|
432
|
-
await record({ kind: "agent_template", ref: r.id, created: true, skillVersion: null });
|
|
433
|
-
return;
|
|
434
|
-
}
|
|
435
|
-
if (r.changed)
|
|
436
|
-
await journal({ op: "template_version", ref: r.id, version: r.version });
|
|
437
|
-
await record({
|
|
438
|
-
kind: "agent_template",
|
|
439
|
-
ref: r.id,
|
|
440
|
-
created: false,
|
|
441
|
-
skillVersion: r.changed ? r.version : null,
|
|
442
|
-
});
|
|
318
|
+
async update(actor, itemId, input, opts = {}) {
|
|
319
|
+
if (!UUID_RE.test(itemId))
|
|
320
|
+
throw itemNotFound();
|
|
321
|
+
return this.locked(itemId, () => this.updateLocked(actor, itemId, input, opts));
|
|
443
322
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
await this.
|
|
323
|
+
async updateLocked(actor, itemId, input, opts, owner, confirmPending) {
|
|
324
|
+
// Storage is read and written here, before any transaction (see the class comment)
|
|
325
|
+
const checked = await this.diff(itemId, input.version, {
|
|
326
|
+
...(owner && { owner }),
|
|
327
|
+
...(confirmPending !== undefined && { confirmPending }),
|
|
328
|
+
});
|
|
329
|
+
if (checked.diffHash !== input.diffHash)
|
|
330
|
+
throw stale();
|
|
331
|
+
const prepared = await this.platform.prepareSync(itemId, input.version);
|
|
332
|
+
const ext = checked.extension;
|
|
333
|
+
// The extension admin re-checks the actor: a super admin, or for an owner update the owner
|
|
334
|
+
// org's admin (and audits it with `ownerOrgId`)
|
|
335
|
+
const opsOwner = owner && { orgId: owner.orgId };
|
|
336
|
+
let step = "extension";
|
|
337
|
+
let extensionMoved = false;
|
|
338
|
+
let enabledHere = false;
|
|
339
|
+
/** A removal: the extension was enabled before it (put back enabled on failure). */
|
|
340
|
+
let removedEnabled = false;
|
|
341
|
+
try {
|
|
342
|
+
if (ext?.sha256 === null) {
|
|
343
|
+
// The target drops the extension (P3-12'): disabled (drained), then uninstalled
|
|
344
|
+
const row = await this.db
|
|
345
|
+
.selectFrom("extension_installs")
|
|
346
|
+
.select("desired_state")
|
|
347
|
+
.where("name", "=", checked.name)
|
|
348
|
+
.executeTakeFirst();
|
|
349
|
+
removedEnabled = row?.desired_state === "enabled";
|
|
350
|
+
await this.ops.disable(checked.name, actor, opts.actorTokenId, opsOwner);
|
|
351
|
+
extensionMoved = true;
|
|
352
|
+
await this.ops.uninstall(checked.name, actor, opts.actorTokenId, opsOwner);
|
|
353
|
+
}
|
|
354
|
+
else if (ext) {
|
|
355
|
+
await this.ops.install(checked.name, ext.sha256, actor, opts.actorTokenId, {
|
|
356
|
+
catalogItemId: itemId,
|
|
357
|
+
...(opsOwner && { owner: opsOwner }),
|
|
358
|
+
});
|
|
359
|
+
extensionMoved = true;
|
|
360
|
+
if (ext.previous === null) {
|
|
361
|
+
await this.ops.enable(checked.name, actor, opts.actorTokenId, opsOwner);
|
|
362
|
+
enabledHere = true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
step = "components";
|
|
366
|
+
const diff = await this.commit(actor, itemId, input, opts, { contents: checked.contents, extension: ext }, prepared, owner, confirmPending);
|
|
367
|
+
return {
|
|
368
|
+
itemId,
|
|
369
|
+
version: diff.to.version,
|
|
370
|
+
components: diff.components,
|
|
371
|
+
extension: extensionView(diff.extension),
|
|
372
|
+
connectorOrgs: diff.connectorOrgs,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
catch (e) {
|
|
376
|
+
let rolledBack = true;
|
|
377
|
+
if (extensionMoved && ext) {
|
|
378
|
+
// An owner update was authorized at its start: the put-back is not checked again
|
|
379
|
+
const back = opsOwner && { ...opsOwner, rollback: true };
|
|
380
|
+
try {
|
|
381
|
+
if (ext.sha256 === null) {
|
|
382
|
+
// Removed by this update: the previous package reinstalled (as it was)
|
|
383
|
+
await this.ops.install(checked.name, ext.previous, actor, opts.actorTokenId, {
|
|
384
|
+
catalogItemId: itemId,
|
|
385
|
+
...(back && { owner: back }),
|
|
386
|
+
});
|
|
387
|
+
if (removedEnabled)
|
|
388
|
+
await this.ops.enable(checked.name, actor, opts.actorTokenId, back);
|
|
389
|
+
}
|
|
390
|
+
else if (ext.previous !== null) {
|
|
391
|
+
await this.ops.install(checked.name, ext.previous, actor, opts.actorTokenId, {
|
|
392
|
+
...(back && { owner: back }),
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
// Installed by this update: an uninstall needs it disabled first
|
|
397
|
+
if (enabledHere)
|
|
398
|
+
await this.ops.disable(checked.name, actor, opts.actorTokenId, back);
|
|
399
|
+
await this.ops.uninstall(checked.name, actor, opts.actorTokenId, back);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
catch (u) {
|
|
403
|
+
rolledBack = false;
|
|
404
|
+
this.log.error("putting a plugin's extension back after a failed update failed", {
|
|
405
|
+
itemId,
|
|
406
|
+
plugin: checked.name,
|
|
407
|
+
err: u,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const code = e instanceof ApiError ? e.code : "internal";
|
|
412
|
+
await this.auditFailure(actor, checked, { step, code, rolledBack }, opts);
|
|
413
|
+
// A refusal found under the lock (stale, disabled meanwhile, a conflict, a downgrade the
|
|
414
|
+
// extension's migrations refuse): returned as it is
|
|
415
|
+
if (e instanceof ApiError && e.status < 500 && rolledBack)
|
|
416
|
+
throw e;
|
|
417
|
+
throw new ApiError("plugin.upgrade_failed", rolledBack
|
|
418
|
+
? `Updating the plugin failed at step ${step} (${code}); the platform is back at version ${checked.from ?? "(none)"}`
|
|
419
|
+
: `Updating the plugin failed at step ${step} (${code}) and putting its extension back failed; check the extension in the admin console`, [
|
|
420
|
+
{
|
|
421
|
+
step,
|
|
422
|
+
code,
|
|
423
|
+
...(e instanceof ApiError && { message: e.message }),
|
|
424
|
+
...(e instanceof ApiError && e.details !== undefined && { causeDetails: e.details }),
|
|
425
|
+
rolledBack,
|
|
426
|
+
},
|
|
427
|
+
]);
|
|
428
|
+
}
|
|
447
429
|
}
|
|
448
|
-
/**
|
|
449
|
-
async
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
.
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
skill_version: c.skillVersion,
|
|
458
|
-
template_id: c.templateId ?? null,
|
|
459
|
-
})
|
|
460
|
-
.onConflict((oc) => oc.columns(["install_id", "kind", "ref"]).doNothing())
|
|
461
|
-
.returning("ref")
|
|
462
|
-
.executeTakeFirst();
|
|
463
|
-
if (c.created)
|
|
464
|
-
await journal({ op: "created", kind: c.kind, ref: c.ref });
|
|
465
|
-
else if (inserted)
|
|
466
|
-
await journal({ op: "recorded", kind: c.kind, ref: c.ref });
|
|
430
|
+
/** `plugin.platform_update_failed`, in a transaction of its own. */
|
|
431
|
+
async auditFailure(actor, diff, f, opts) {
|
|
432
|
+
await audit(this.db, {
|
|
433
|
+
actorAccountId: actor,
|
|
434
|
+
...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
|
|
435
|
+
action: "plugin.platform_update_failed",
|
|
436
|
+
target: diff.itemId,
|
|
437
|
+
detail: { name: diff.name, from: diff.from, to: diff.to.version, ...f },
|
|
438
|
+
});
|
|
467
439
|
}
|
|
468
440
|
/**
|
|
469
|
-
* The
|
|
470
|
-
* the
|
|
471
|
-
* once the connectors are applied.
|
|
441
|
+
* The update's transaction (see the class comment): the super admin re-checked, the diff
|
|
442
|
+
* recomputed under the item name's lock, then every write. Returns the diff it applied.
|
|
472
443
|
*/
|
|
473
|
-
async
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
.
|
|
444
|
+
async commit(actor, itemId, input, opts, pre, prepared, owner, confirmPending) {
|
|
445
|
+
if (owner === undefined)
|
|
446
|
+
await this.adminLock.lockAndAuthorize(actor);
|
|
447
|
+
// The global lock order (ruling P3-50): the orgs (sorted; the acting owner org's admin
|
|
448
|
+
// re-checked at its place), then the item, then its components; an org deletion locks its org
|
|
449
|
+
// row, then the items it owns
|
|
450
|
+
const liveOrgs = await this.lockUpdateOrgs(itemId, owner && { orgId: owner.orgId, actor });
|
|
451
|
+
const named = await this.db
|
|
452
|
+
.selectFrom("catalog_items")
|
|
453
|
+
.select("name")
|
|
454
|
+
.where("id", "=", itemId)
|
|
482
455
|
.executeTakeFirst();
|
|
483
|
-
if (
|
|
484
|
-
throw
|
|
485
|
-
await
|
|
486
|
-
|
|
487
|
-
|
|
456
|
+
if (!named)
|
|
457
|
+
throw itemNotFound();
|
|
458
|
+
await this.itemLock.lock(named.name);
|
|
459
|
+
let pendingOwner = null;
|
|
460
|
+
if (confirmPending !== undefined) {
|
|
461
|
+
// The super admin's confirmation (P3-21): still pending with the version they reviewed
|
|
462
|
+
const row = await this.db
|
|
463
|
+
.selectFrom("catalog_items")
|
|
464
|
+
.select(["deleted_at", "pending_version", "owner_org_id"])
|
|
465
|
+
.where("id", "=", itemId)
|
|
466
|
+
.forUpdate()
|
|
467
|
+
.executeTakeFirstOrThrow();
|
|
468
|
+
if (row.deleted_at !== null)
|
|
469
|
+
throw itemRemovedError(named.name);
|
|
470
|
+
if (row.pending_version !== confirmPending)
|
|
471
|
+
throw pendingStale(named.name);
|
|
472
|
+
pendingOwner = row.owner_org_id;
|
|
488
473
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
throw new ApiError("catalog.dependency_unresolved", `Dependency ${dep.name}@${dep.range} cannot be resolved: its install in this organization changed meanwhile; preview the upgrade again`, [{ dependency: dep.name, range: dep.range }]);
|
|
501
|
-
}
|
|
474
|
+
const diff = await this.diff(itemId, input.version, {
|
|
475
|
+
pre,
|
|
476
|
+
...(owner && { owner }),
|
|
477
|
+
...(confirmPending !== undefined && { confirmPending }),
|
|
478
|
+
});
|
|
479
|
+
if (diff.diffHash !== input.diffHash)
|
|
480
|
+
throw stale();
|
|
481
|
+
const t = this.db;
|
|
482
|
+
// The platform versions (files copied before the transaction; rows only here)
|
|
483
|
+
try {
|
|
484
|
+
await this.platform.applySyncIn(prepared);
|
|
502
485
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
486
|
+
catch (e) {
|
|
487
|
+
if (e instanceof PlanStale)
|
|
488
|
+
throw stale();
|
|
489
|
+
throw e;
|
|
490
|
+
}
|
|
491
|
+
for (const [id, pinned] of diff.suggest) {
|
|
509
492
|
await t
|
|
510
|
-
.updateTable("
|
|
511
|
-
.set({ suggest_pinned:
|
|
512
|
-
.from("
|
|
493
|
+
.updateTable("platform_skill_versions as v")
|
|
494
|
+
.set({ suggest_pinned: pinned })
|
|
495
|
+
.from("platform_skills as s")
|
|
513
496
|
.whereRef("s.id", "=", "v.skill_id")
|
|
514
497
|
.whereRef("s.current_version", "=", "v.version")
|
|
515
|
-
.where("s.id", "=",
|
|
516
|
-
.
|
|
517
|
-
|
|
518
|
-
|
|
498
|
+
.where("s.id", "=", id)
|
|
499
|
+
.execute();
|
|
500
|
+
}
|
|
501
|
+
// Removals (spec §7.3): kept while agents use it; otherwise soft-deleted
|
|
502
|
+
const now = sql `now()`;
|
|
503
|
+
for (const c of diff.components) {
|
|
504
|
+
if (c.change !== "removed" || c.id === null || c.kept !== undefined)
|
|
505
|
+
continue;
|
|
506
|
+
const table = c.kind === "skill"
|
|
507
|
+
? "platform_skills"
|
|
508
|
+
: c.kind === "agent_template"
|
|
509
|
+
? "platform_agent_templates"
|
|
510
|
+
: "platform_connector_templates";
|
|
511
|
+
await t
|
|
512
|
+
.updateTable(table)
|
|
513
|
+
.set({ deleted_at: now, updated_at: now })
|
|
514
|
+
.where("id", "=", c.id)
|
|
519
515
|
.execute();
|
|
520
516
|
}
|
|
517
|
+
// Pins of linked copies: moved to the plugin's version (spec §7.1)
|
|
518
|
+
for (const c of diff.components) {
|
|
519
|
+
if (c.change === "removed" || c.id === null || c.pinnedOrgs.length === 0)
|
|
520
|
+
continue;
|
|
521
|
+
if (c.kind === "skill") {
|
|
522
|
+
await t
|
|
523
|
+
.updateTable("skills")
|
|
524
|
+
.set({ pinned_version: null, updated_at: now })
|
|
525
|
+
.where("platform_skill_id", "=", c.id)
|
|
526
|
+
.where("org_id", "in", c.pinnedOrgs)
|
|
527
|
+
.where("pinned_version", "is not", null)
|
|
528
|
+
.execute();
|
|
529
|
+
}
|
|
530
|
+
else if (c.kind === "agent_template") {
|
|
531
|
+
await t
|
|
532
|
+
.updateTable("agent_templates")
|
|
533
|
+
.set({ pinned_version: null, updated_at: now })
|
|
534
|
+
.where("platform_template_id", "=", c.id)
|
|
535
|
+
.where("org_id", "in", c.pinnedOrgs)
|
|
536
|
+
.where("pinned_version", "is not", null)
|
|
537
|
+
.execute();
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
// Connectors made from changed templates, in every org (ruling E2)
|
|
541
|
+
const orgs = [...new Set(diff.connectors.map((c) => c.orgId))].sort();
|
|
542
|
+
for (const orgId of orgs) {
|
|
543
|
+
if (!liveOrgs.has(orgId)) {
|
|
544
|
+
// Locked before the item (P3-50): an org not locked then is deleted (its connectors go
|
|
545
|
+
// with it), or its connector was made since, which changes what the update does
|
|
546
|
+
const live = await t
|
|
547
|
+
.selectFrom("orgs")
|
|
548
|
+
.select("id")
|
|
549
|
+
.where("id", "=", orgId)
|
|
550
|
+
.where("deleted_at", "is", null)
|
|
551
|
+
.executeTakeFirst();
|
|
552
|
+
if (live === undefined)
|
|
553
|
+
continue;
|
|
554
|
+
throw stale();
|
|
555
|
+
}
|
|
556
|
+
for (const c of diff.connectors.filter((x) => x.orgId === orgId)) {
|
|
557
|
+
const prepared = await this.prepare(actor, itemId, diff.to.version, c);
|
|
558
|
+
await this.connectors.applyTemplateIn(orgId, c.id, actor, prepared, opts.actorTokenId);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
// One version for the deployment: every live org install of the plugin moves with it (one
|
|
562
|
+
// still installing too, and a legacy `upgrading` row; an install marked installed later
|
|
563
|
+
// records the platform version current at its commit, PluginInstaller.markInstalled)
|
|
521
564
|
await t
|
|
522
565
|
.updateTable("plugin_installs")
|
|
523
|
-
.set({ version: diff.to.version,
|
|
524
|
-
.where("
|
|
566
|
+
.set({ version: diff.to.version, error: null })
|
|
567
|
+
.where("item_id", "=", itemId)
|
|
568
|
+
.where("status", "in", ["installing", "installed", "upgrading"])
|
|
525
569
|
.execute();
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
570
|
+
if (owner !== undefined || confirmPending !== undefined) {
|
|
571
|
+
// An org's version becomes its latest once applied or confirmed (ruling P3-34)
|
|
572
|
+
await t
|
|
573
|
+
.updateTable("catalog_items")
|
|
574
|
+
.set({ latest_version: diff.to.version })
|
|
575
|
+
.where("id", "=", itemId)
|
|
576
|
+
.execute();
|
|
577
|
+
await markVersionApplied(t, itemId, diff.to.version);
|
|
578
|
+
// An owner's new version replaces a version waiting for the super admin (pre-flight F7):
|
|
579
|
+
// their confirmation of it is then refused (409 extension.not_pending); the super admin's
|
|
580
|
+
// confirmation applied it
|
|
581
|
+
await t
|
|
582
|
+
.updateTable("catalog_items")
|
|
583
|
+
.set({ pending_version: null, pending_by: null, pending_at: null })
|
|
584
|
+
.where("id", "=", itemId)
|
|
585
|
+
.where("pending_version", "is not", null)
|
|
586
|
+
.execute();
|
|
587
|
+
}
|
|
588
|
+
if (confirmPending !== undefined && pendingOwner !== null) {
|
|
589
|
+
await addOrgNotice(t, pendingOwner, "upload_confirmed", {
|
|
534
590
|
itemId,
|
|
535
591
|
name: diff.name,
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
async undoFailed(orgId, actor, installId, upgradeId, diff, steps, f, opts) {
|
|
546
|
-
const { at, code } = f;
|
|
547
|
-
const t = this.db;
|
|
548
|
-
await this.orgLock.lock(orgId);
|
|
549
|
-
const row = await t
|
|
550
|
-
.selectFrom("plugin_installs")
|
|
551
|
-
.select(sql `upgrade->>'id'`.as("upgradeId"))
|
|
552
|
-
.where("id", "=", installId)
|
|
553
|
-
.forUpdate()
|
|
554
|
-
.executeTakeFirst();
|
|
555
|
-
const ours = row?.upgradeId === upgradeId;
|
|
556
|
-
// Cleaned up meanwhile: only the step whose journal write found the journal gone is ours to
|
|
557
|
-
// undo. When that step ran in the same transaction as its journal write (the extension
|
|
558
|
-
// enable, the ledger rows), the transaction rolled back with the failed write, so undoing
|
|
559
|
-
// it is a no-op (a never-enabled extension, a never-inserted row); undoing is still safe.
|
|
560
|
-
const undo = ours ? steps.done : steps.unjournaled ? [steps.unjournaled] : [];
|
|
561
|
-
const out = await this.installer.undoUpgradeIn(orgId, actor, installId, undo, "plugin_upgrade_rollback");
|
|
562
|
-
await t
|
|
563
|
-
.updateTable("plugin_installs")
|
|
564
|
-
.set({ status: "installed", upgrade: null, error: JSON.stringify({ step: at, code }) })
|
|
565
|
-
.where("id", "=", installId)
|
|
566
|
-
.where(sql `upgrade->>'id'`, "=", upgradeId)
|
|
567
|
-
.execute();
|
|
592
|
+
displayName: await itemDisplayName(t, itemId),
|
|
593
|
+
version: diff.to.version,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
// The orgs using a shared org-owned plugin are told, one notice per plugin (a platform-owned
|
|
597
|
+
// plugin tells nobody); a plugin never synced before has no version to come from
|
|
598
|
+
if (diff.from !== null) {
|
|
599
|
+
await notifySharedUpdate(t, { kind: "plugin", id: itemId }, { name: diff.name, from: diff.from, to: diff.to.version });
|
|
600
|
+
}
|
|
568
601
|
await audit(t, {
|
|
569
602
|
actorAccountId: actor,
|
|
570
603
|
...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
target: installId,
|
|
604
|
+
action: "plugin.platform_updated",
|
|
605
|
+
target: itemId,
|
|
574
606
|
detail: {
|
|
575
|
-
itemId: diff.itemId,
|
|
576
607
|
name: diff.name,
|
|
577
|
-
from: diff.from
|
|
608
|
+
from: diff.from,
|
|
578
609
|
to: diff.to.version,
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
610
|
+
...(owner !== undefined && { ownerOrgId: owner.orgId }),
|
|
611
|
+
components: diff.components.map((c) => ({
|
|
612
|
+
kind: c.kind,
|
|
613
|
+
name: c.name,
|
|
614
|
+
change: c.change,
|
|
615
|
+
...(c.kept !== undefined && { kept: true }),
|
|
616
|
+
...(c.pinnedOrgs.length > 0 && c.change !== "removed" && { unpinned: c.pinnedOrgs }),
|
|
617
|
+
})),
|
|
618
|
+
...(diff.extension && { extension: extensionView(diff.extension) }),
|
|
619
|
+
connectors: diff.connectors.map((c) => ({
|
|
620
|
+
orgId: c.orgId,
|
|
621
|
+
id: c.id,
|
|
622
|
+
credentialsReset: c.credentialsReset,
|
|
623
|
+
})),
|
|
624
|
+
...opts.auditDetail,
|
|
583
625
|
},
|
|
584
626
|
});
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
return [];
|
|
599
|
-
await this.orgLock.lock(orgId);
|
|
600
|
-
const row = await t
|
|
601
|
-
.selectFrom("plugin_installs as a")
|
|
602
|
-
.innerJoin("catalog_items as i", "i.id", "a.item_id")
|
|
603
|
-
.select([
|
|
604
|
-
"a.status",
|
|
605
|
-
"a.upgrade",
|
|
606
|
-
"a.item_id",
|
|
607
|
-
"i.name",
|
|
608
|
-
sql `coalesce((a.upgrade->>'startedAt')::timestamptz < now() - make_interval(mins => ${staleMinutes}), false)`.as("stale"),
|
|
609
|
-
])
|
|
610
|
-
.where("a.id", "=", installId)
|
|
611
|
-
.where("a.org_id", "=", orgId)
|
|
612
|
-
.forUpdate("a")
|
|
613
|
-
.executeTakeFirst();
|
|
614
|
-
if (row?.status !== "upgrading" || !row.stale)
|
|
615
|
-
return [];
|
|
616
|
-
const steps = journalEntries(row.upgrade);
|
|
617
|
-
const out = await this.installer.undoUpgradeIn(orgId, actor, installId, steps, "plugin_upgrade_cleanup");
|
|
618
|
-
await t
|
|
619
|
-
.updateTable("plugin_installs")
|
|
620
|
-
.set({
|
|
621
|
-
status: "installed",
|
|
622
|
-
upgrade: null,
|
|
623
|
-
error: JSON.stringify({ step: "interrupted", code: "internal" }),
|
|
624
|
-
})
|
|
625
|
-
.where("id", "=", installId)
|
|
626
|
-
.execute();
|
|
627
|
-
const j = row.upgrade;
|
|
628
|
-
await audit(t, {
|
|
629
|
-
actorAccountId: actor,
|
|
630
|
-
orgId,
|
|
631
|
-
action: "plugin.org_upgrade_failed",
|
|
632
|
-
target: installId,
|
|
633
|
-
detail: {
|
|
634
|
-
itemId: row.item_id,
|
|
635
|
-
name: row.name,
|
|
636
|
-
from: j?.from ?? null,
|
|
637
|
-
to: j?.to ?? null,
|
|
638
|
-
step: "interrupted",
|
|
639
|
-
code: "internal",
|
|
640
|
-
rolledBack: steps,
|
|
641
|
-
},
|
|
627
|
+
const enqueue = [itemId, ...diff.dependencies];
|
|
628
|
+
afterCommit(async () => {
|
|
629
|
+
for (const id of enqueue) {
|
|
630
|
+
try {
|
|
631
|
+
await this.sync.enqueueItem(id);
|
|
632
|
+
}
|
|
633
|
+
catch (err) {
|
|
634
|
+
this.log.warn("enqueueing the scope sync after a platform update failed", {
|
|
635
|
+
itemId: id,
|
|
636
|
+
err,
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
}
|
|
642
640
|
});
|
|
643
|
-
return
|
|
641
|
+
return diff;
|
|
644
642
|
}
|
|
645
643
|
/**
|
|
646
|
-
* The
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
* ledger marked `dropped` ("no longer in the plugin", still this install's), every other removed
|
|
650
|
-
* component leaves the ledger; a dropped component the target brings back is un-dropped. A dependency
|
|
651
|
-
* install is uninstalled by the same rules (another install using it takes it over). Skills ->
|
|
652
|
-
* connectors -> extension -> dependencies.
|
|
644
|
+
* The template update of one connector: the new endpoint preset with the org's values at the
|
|
645
|
+
* editable paths, the auth preset and default policies; credentials cleared (no new ones: they
|
|
646
|
+
* are the org's) when the credential shape changed.
|
|
653
647
|
*/
|
|
654
|
-
async
|
|
655
|
-
const
|
|
656
|
-
const
|
|
657
|
-
const
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
.where("install_id", "=", diff.installId)
|
|
662
|
-
.where("kind", "=", kind)
|
|
663
|
-
.where("ref", "=", ref);
|
|
664
|
-
const dropRow = (kind, ref) => t
|
|
665
|
-
.deleteFrom("plugin_install_components")
|
|
666
|
-
.where("install_id", "=", diff.installId)
|
|
667
|
-
.where("kind", "=", kind)
|
|
668
|
-
.where("ref", "=", ref)
|
|
669
|
-
.execute();
|
|
670
|
-
const removed = [
|
|
671
|
-
...diff.skills.flatMap((x) => (x.change === "removed" && x.ledger ? [x.ledger] : [])),
|
|
672
|
-
...diff.agentTemplates.flatMap((x) => (x.change === "removed" && x.ledger ? [x.ledger] : [])),
|
|
673
|
-
...diff.templates.flatMap((x) => (x.change === "removed" && x.ledger ? [x.ledger] : [])),
|
|
674
|
-
...(diff.extension?.change === "removed"
|
|
675
|
-
? diff.ledger.filter((l) => l.kind === "extension" && l.ref === diff.extension?.name)
|
|
676
|
-
: []),
|
|
677
|
-
];
|
|
678
|
-
const agentRefs = await this.installer.agentRefs(orgId);
|
|
679
|
-
const self = new Set([diff.installId]);
|
|
680
|
-
for (const l of removed) {
|
|
681
|
-
if (!l.created) {
|
|
682
|
-
await dropRow(l.kind, l.ref);
|
|
683
|
-
keep(l.kind, l.ref, "pre_existing");
|
|
684
|
-
continue;
|
|
685
|
-
}
|
|
686
|
-
const outcome = await this.installer.decideRemovalIn(orgId, actor, { kind: l.kind, ref: l.ref, created: true, skillVersion: null }, self, agentRefs, reason);
|
|
687
|
-
if (outcome === "used_by_agents" ||
|
|
688
|
-
outcome === "used_by_connectors" ||
|
|
689
|
-
outcome === "used_by_plugins") {
|
|
690
|
-
// Still used: stays this install's, marked dropped, so an uninstall (or a later upgrade
|
|
691
|
-
// bringing it back) still finds it
|
|
692
|
-
await row(l.kind, l.ref).set({ dropped: true }).execute();
|
|
693
|
-
keep(l.kind, l.ref, outcome);
|
|
694
|
-
continue;
|
|
695
|
-
}
|
|
696
|
-
await dropRow(l.kind, l.ref);
|
|
697
|
-
if (outcome === "removed") {
|
|
698
|
-
report.removed.push({ kind: l.kind, ref: l.ref });
|
|
699
|
-
if (l.kind === "extension")
|
|
700
|
-
disabled.push(l.ref);
|
|
701
|
-
}
|
|
702
|
-
else if (outcome !== "gone")
|
|
703
|
-
keep(l.kind, l.ref, outcome);
|
|
704
|
-
}
|
|
705
|
-
// Components back in the plugin (dropped by an earlier upgrade, brought again by the target)
|
|
706
|
-
const back = [
|
|
707
|
-
...diff.skills.flatMap((x) => x.change !== "removed" && x.ledger?.dropped
|
|
708
|
-
? [{ kind: "skill", ref: x.ledger.ref }]
|
|
709
|
-
: []),
|
|
710
|
-
...diff.agentTemplates.flatMap((x) => x.change !== "removed" && x.ledger?.dropped
|
|
711
|
-
? [{ kind: "agent_template", ref: x.ledger.ref }]
|
|
712
|
-
: []),
|
|
713
|
-
...diff.templates.flatMap((x) => x.change !== "removed" && x.ledger?.dropped
|
|
714
|
-
? [{ kind: "connector", ref: x.ledger.ref }]
|
|
715
|
-
: []),
|
|
716
|
-
...(diff.extension && diff.extension.change !== "removed"
|
|
717
|
-
? diff.ledger
|
|
718
|
-
.filter((l) => l.kind === "extension" && l.ref === diff.extension?.name && l.dropped)
|
|
719
|
-
.map((l) => ({ kind: "extension", ref: l.ref }))
|
|
720
|
-
: []),
|
|
721
|
-
];
|
|
722
|
-
for (const b of back)
|
|
723
|
-
await row(b.kind, b.ref).set({ dropped: false }).execute();
|
|
724
|
-
for (const dep of diff.dependencies) {
|
|
725
|
-
if (dep.change !== "removed" || !dep.child)
|
|
726
|
-
continue;
|
|
727
|
-
const ref = dep.child.installId;
|
|
728
|
-
await dropRow("plugin", ref);
|
|
729
|
-
if (!dep.child.ledger.created) {
|
|
730
|
-
keep("plugin", ref, "pre_existing");
|
|
731
|
-
continue;
|
|
732
|
-
}
|
|
733
|
-
const sub = { removed: [], kept: [] };
|
|
734
|
-
const outcome = await this.installer.uninstallIn(orgId, actor, ref, {
|
|
735
|
-
report: sub,
|
|
736
|
-
removing: new Set(),
|
|
737
|
-
from: diff.installId,
|
|
738
|
-
authorize: false,
|
|
739
|
-
reason,
|
|
740
|
-
opts: {},
|
|
741
|
-
});
|
|
742
|
-
for (const r of sub.removed)
|
|
743
|
-
if (r.kind === "extension")
|
|
744
|
-
disabled.push(r.ref);
|
|
745
|
-
if (outcome === "removed")
|
|
746
|
-
report.removed.push({ kind: "plugin", ref });
|
|
747
|
-
else if (outcome === "kept")
|
|
748
|
-
keep("plugin", ref, "used_by_other_install");
|
|
648
|
+
async prepare(actor, itemId, version, c) {
|
|
649
|
+
const template = c.file.template;
|
|
650
|
+
const endpoint = structuredClone(template.endpoint);
|
|
651
|
+
for (const e of template.editableEndpoint ?? []) {
|
|
652
|
+
const v = getPath(c.endpoint, e);
|
|
653
|
+
if (v !== undefined)
|
|
654
|
+
setPath(endpoint, e, v);
|
|
749
655
|
}
|
|
750
|
-
|
|
656
|
+
const input = {
|
|
657
|
+
endpoint,
|
|
658
|
+
auth: template.auth,
|
|
659
|
+
template: {
|
|
660
|
+
itemId,
|
|
661
|
+
version,
|
|
662
|
+
templateId: c.templateId,
|
|
663
|
+
...(template.defaultToolPolicies !== undefined && {
|
|
664
|
+
defaultToolPolicies: template.defaultToolPolicies,
|
|
665
|
+
}),
|
|
666
|
+
},
|
|
667
|
+
resetCredentials: false,
|
|
668
|
+
};
|
|
669
|
+
// Checked without credentials (nothing outbound); a changed shape then clears the old ones
|
|
670
|
+
const prepared = await this.connectors.prepareTemplateUpdate(actor, c.connector, input);
|
|
671
|
+
return c.credentialsReset
|
|
672
|
+
? { ...prepared, input: { ...prepared.input, resetCredentials: true } }
|
|
673
|
+
: prepared;
|
|
751
674
|
}
|
|
752
675
|
/**
|
|
753
|
-
* Computes an
|
|
754
|
-
*
|
|
676
|
+
* Computes an update (see the class comment). `target`: the target read from an org's staging
|
|
677
|
+
* instead of `catalog_item_versions` (pre-flight F3). `owner`: an owner update (P3-12): the item
|
|
678
|
+
* must be the org's, an extension-bearing target is refused, and a target dropping the
|
|
679
|
+
* extension removes it.
|
|
755
680
|
*/
|
|
756
|
-
async diff(
|
|
681
|
+
async diff(itemId, version, o = {}) {
|
|
757
682
|
const db = this.db;
|
|
758
|
-
if (!UUID_RE.test(
|
|
759
|
-
throw
|
|
760
|
-
const
|
|
761
|
-
.selectFrom("
|
|
762
|
-
.
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
683
|
+
if (!UUID_RE.test(itemId))
|
|
684
|
+
throw itemNotFound();
|
|
685
|
+
const item = await db
|
|
686
|
+
.selectFrom("catalog_items")
|
|
687
|
+
.select([
|
|
688
|
+
"id",
|
|
689
|
+
"name",
|
|
690
|
+
"disabled",
|
|
691
|
+
"builtin",
|
|
692
|
+
"deleted_at",
|
|
693
|
+
"platform_synced_version",
|
|
694
|
+
"owner_org_id",
|
|
695
|
+
"awaiting_confirmation",
|
|
696
|
+
"pending_version",
|
|
697
|
+
"latest_version",
|
|
698
|
+
])
|
|
699
|
+
.where("id", "=", itemId)
|
|
766
700
|
.executeTakeFirst();
|
|
767
|
-
if (!
|
|
768
|
-
throw
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
701
|
+
if (!item || item.deleted_at !== null)
|
|
702
|
+
throw itemNotFound();
|
|
703
|
+
// A version waiting for the super admin applies only through their confirmation (P3-9, F8)
|
|
704
|
+
if (o.confirmPending !== version) {
|
|
705
|
+
if (item.awaiting_confirmation)
|
|
706
|
+
throw itemPendingError(item.name);
|
|
707
|
+
if (item.pending_version !== null && item.pending_version === version) {
|
|
708
|
+
throw itemPendingError(item.name, version);
|
|
709
|
+
}
|
|
710
|
+
// The super admin's generic update of an org's plugin reaches only its applied version
|
|
711
|
+
// (`latest_version`, ruling P3-34/P3-37): one the org uploaded that was never applied or
|
|
712
|
+
// confirmed (a rejected or failed one) is not theirs to apply
|
|
713
|
+
if (o.owner === undefined && item.owner_org_id !== null && version !== item.latest_version) {
|
|
714
|
+
throw notApplied(item.name, version);
|
|
715
|
+
}
|
|
775
716
|
}
|
|
776
|
-
if (
|
|
777
|
-
throw
|
|
717
|
+
if (o.owner !== undefined && item.owner_org_id !== o.owner.orgId)
|
|
718
|
+
throw itemNotFound();
|
|
719
|
+
if (item.disabled) {
|
|
720
|
+
throw new ApiError("catalog.item_disabled", "This plugin is disabled on the deployment; enable it before updating");
|
|
778
721
|
}
|
|
779
|
-
const
|
|
722
|
+
const name = item.name;
|
|
723
|
+
const versions = (await db
|
|
780
724
|
.selectFrom("catalog_item_versions")
|
|
781
725
|
.select([
|
|
782
726
|
"version",
|
|
@@ -784,561 +728,628 @@ let PluginUpgrader = class PluginUpgrader {
|
|
|
784
728
|
"object_prefix",
|
|
785
729
|
"files",
|
|
786
730
|
"extension_sha256",
|
|
787
|
-
"
|
|
788
|
-
"
|
|
731
|
+
"applied_at",
|
|
732
|
+
"rejected_at",
|
|
789
733
|
])
|
|
790
734
|
.where("item_id", "=", itemId)
|
|
791
|
-
.execute();
|
|
792
|
-
const
|
|
793
|
-
|
|
794
|
-
if (!fromRow) {
|
|
795
|
-
throw new ApiError("catalog.item_not_found", `The installed version ${install.version} is no longer in the enterprise catalog`);
|
|
796
|
-
}
|
|
797
|
-
if (!toRow)
|
|
735
|
+
.execute());
|
|
736
|
+
const to = o.target ?? versions.find((v) => v.version === version);
|
|
737
|
+
if (!to)
|
|
798
738
|
throw new ApiError("catalog.item_not_found", `Version ${version} of this item not found`);
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
739
|
+
// Whoever owns the item now (a transfer keeps the version rows), the generic update never
|
|
740
|
+
// reaches a version that was rejected or never applied (ruling P3-52)
|
|
741
|
+
const row = versions.find((v) => v.version === version);
|
|
742
|
+
if (o.owner === undefined && o.confirmPending !== version && row && !versionUsable(row)) {
|
|
743
|
+
throw notApplied(item.name, version);
|
|
744
|
+
}
|
|
745
|
+
if (o.owner !== undefined && to.manifest.extension) {
|
|
746
|
+
// An org's version with an extension waits for the super admin's confirmation (plan Task 6)
|
|
747
|
+
throw new ApiError("plugin.extension_upload_pending", `Version ${to.version} of plugin ${item.name} has an extension; it applies once a super admin confirms it`);
|
|
748
|
+
}
|
|
749
|
+
const from = item.platform_synced_version;
|
|
750
|
+
const fromRow = versions.find((v) => v.version === from);
|
|
751
|
+
if (from === to.version) {
|
|
752
|
+
// Confirming a preview whose update already ran (a concurrent confirm won): stale
|
|
753
|
+
if (o.pre)
|
|
804
754
|
throw stale();
|
|
805
|
-
throw targetInvalid(`
|
|
806
|
-
{ reason: "same_version", version: from
|
|
755
|
+
throw targetInvalid(`The platform already runs version ${from} of plugin ${name}`, [
|
|
756
|
+
{ reason: "same_version", version: from },
|
|
807
757
|
]);
|
|
808
758
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
759
|
+
const target = await this.extensionTarget(name, fromRow, to, o.owner !== undefined);
|
|
760
|
+
let extension;
|
|
761
|
+
if (o.pre) {
|
|
762
|
+
// The confirm's transaction: the extension this update moved is expected in place (a
|
|
763
|
+
// removed one gone)
|
|
764
|
+
if (o.pre.extension) {
|
|
765
|
+
const row = await db
|
|
766
|
+
.selectFrom("extension_installs")
|
|
767
|
+
.select("sha256")
|
|
768
|
+
.where("name", "=", name)
|
|
769
|
+
.executeTakeFirst();
|
|
770
|
+
if ((row?.sha256 ?? null) !== o.pre.extension.sha256)
|
|
771
|
+
throw stale();
|
|
772
|
+
extension = o.pre.extension;
|
|
823
773
|
}
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
.innerJoin("plugin_installs as a", "a.id", "c.install_id")
|
|
829
|
-
.innerJoin("catalog_item_versions as v", (j) => j.onRef("v.item_id", "=", "a.item_id").onRef("v.version", "=", "a.version"))
|
|
830
|
-
.innerJoin("catalog_items as i", "i.id", "a.item_id")
|
|
831
|
-
.select(["i.name", "v.manifest"])
|
|
832
|
-
.where("c.kind", "=", "plugin")
|
|
833
|
-
.where("c.ref", "=", installId)
|
|
834
|
-
.where("a.org_id", "=", orgId)
|
|
835
|
-
.where("a.status", "<>", "failed")
|
|
836
|
-
.execute();
|
|
837
|
-
for (const p of parents) {
|
|
838
|
-
const range = p.manifest.dependencies?.find((x) => x.name === name)?.version;
|
|
839
|
-
if (range !== undefined && !satisfiesRange(to.version, range)) {
|
|
840
|
-
throw targetInvalid(`Plugin ${p.name} installed in this organization depends on ${name}@${range}, which version ${to.version} does not satisfy`, [{ reason: "dependent_range", dependent: p.name, range, version: to.version }]);
|
|
774
|
+
else {
|
|
775
|
+
if (target !== null)
|
|
776
|
+
throw stale();
|
|
777
|
+
extension = null;
|
|
841
778
|
}
|
|
842
779
|
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
const change = !from.manifest.extension
|
|
850
|
-
? "added"
|
|
851
|
-
: !to.manifest.extension
|
|
852
|
-
? "removed"
|
|
853
|
-
: from.extension_sha256 !== to.extension_sha256 ||
|
|
854
|
-
from.manifest.extension.manifest.version !== to.manifest.extension.manifest.version
|
|
855
|
-
? "changed"
|
|
856
|
-
: "unchanged";
|
|
857
|
-
extension = { change, name };
|
|
858
|
-
if (change === "added") {
|
|
859
|
-
await this.installer.planExtension(orgId, to.manifest);
|
|
860
|
-
extensionConfig = !(await isExtensionEnabled(db, orgId, name));
|
|
861
|
-
}
|
|
862
|
-
components.push({
|
|
863
|
-
kind: "extension",
|
|
864
|
-
ref: name,
|
|
865
|
-
change,
|
|
866
|
-
...(change === "changed" && {
|
|
867
|
-
detail: `${from.manifest.extension?.manifest.version} -> ${to.manifest.extension?.manifest.version}`,
|
|
868
|
-
}),
|
|
869
|
-
});
|
|
780
|
+
else {
|
|
781
|
+
extension = target && {
|
|
782
|
+
...target,
|
|
783
|
+
needsApproval: target.sha256 !== null && (await this.needsApproval(name, target.sha256)),
|
|
784
|
+
orgs: [],
|
|
785
|
+
};
|
|
870
786
|
}
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
const
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
});
|
|
787
|
+
const contents = o.pre?.contents ?? (await this.readContents(to));
|
|
788
|
+
const dependencies = await this.checkDependents(name, to);
|
|
789
|
+
// The platform's components of the plugin and the target's
|
|
790
|
+
const live = await this.liveComponents(itemId);
|
|
791
|
+
const components = [];
|
|
792
|
+
const suggest = new Map();
|
|
793
|
+
const direction = (fromVersion) => fromVersion === null || compareVersions(to.version, fromVersion) >= 0
|
|
794
|
+
? "upgrade"
|
|
795
|
+
: "downgrade";
|
|
796
|
+
const added = (kind, n) => ({
|
|
797
|
+
kind,
|
|
798
|
+
id: null,
|
|
799
|
+
name: n,
|
|
800
|
+
change: "added",
|
|
801
|
+
from: null,
|
|
802
|
+
to: to.version,
|
|
803
|
+
updatedOnOwn: false,
|
|
804
|
+
ownScope: false,
|
|
805
|
+
pinnedOrgs: [],
|
|
806
|
+
});
|
|
807
|
+
// Skills (by name, by their files' digest)
|
|
808
|
+
const toSkills = contents.skillNames.map((n, i) => {
|
|
809
|
+
const entry = (to.manifest.skills ?? [])[i];
|
|
810
|
+
return {
|
|
811
|
+
name: n,
|
|
812
|
+
digest: dirDigest(to, packagePath(entry?.path)),
|
|
813
|
+
pinned: entry?.pinned === true,
|
|
814
|
+
};
|
|
815
|
+
});
|
|
816
|
+
for (const s of toSkills) {
|
|
817
|
+
const l = live.skills.find((x) => x.name === s.name);
|
|
818
|
+
if (!l) {
|
|
819
|
+
components.push(added("skill", s.name));
|
|
905
820
|
continue;
|
|
906
821
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
822
|
+
const same = skillFilesDigest(l.manifest.files) === s.digest;
|
|
823
|
+
if (same && l.suggestPinned !== s.pinned)
|
|
824
|
+
suggest.set(l.id, s.pinned);
|
|
825
|
+
components.push({
|
|
826
|
+
kind: "skill",
|
|
827
|
+
id: l.id,
|
|
828
|
+
name: s.name,
|
|
829
|
+
change: same ? "same" : direction(l.catalogVersion),
|
|
830
|
+
from: l.catalogVersion,
|
|
831
|
+
to: to.version,
|
|
832
|
+
updatedOnOwn: l.updatedOnOwn,
|
|
833
|
+
ownScope: l.ownScope,
|
|
834
|
+
pinnedOrgs: [],
|
|
917
835
|
});
|
|
918
836
|
}
|
|
919
|
-
for (const
|
|
920
|
-
if (
|
|
837
|
+
for (const l of live.skills) {
|
|
838
|
+
if (toSkills.some((s) => s.name === l.name))
|
|
921
839
|
continue;
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
840
|
+
components.push({
|
|
841
|
+
kind: "skill",
|
|
842
|
+
id: l.id,
|
|
843
|
+
name: l.name,
|
|
925
844
|
change: "removed",
|
|
926
|
-
|
|
927
|
-
|
|
845
|
+
from: l.catalogVersion,
|
|
846
|
+
to: null,
|
|
847
|
+
updatedOnOwn: l.updatedOnOwn,
|
|
848
|
+
ownScope: l.ownScope,
|
|
849
|
+
pinnedOrgs: [],
|
|
928
850
|
});
|
|
929
851
|
}
|
|
930
|
-
|
|
852
|
+
// Agent templates (by name, canonical definition)
|
|
853
|
+
const toAgents = contents.agents;
|
|
854
|
+
for (const a of toAgents) {
|
|
855
|
+
const l = live.agentTemplates.find((x) => x.name === a.template.name);
|
|
856
|
+
if (!l) {
|
|
857
|
+
components.push(added("agent_template", a.template.name));
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
const same = canonicalJson(l.definition) === canonicalJson(a.template);
|
|
931
861
|
components.push({
|
|
932
|
-
kind: "
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
862
|
+
kind: "agent_template",
|
|
863
|
+
id: l.id,
|
|
864
|
+
name: l.name,
|
|
865
|
+
change: same ? "same" : direction(l.catalogVersion),
|
|
866
|
+
from: l.catalogVersion,
|
|
867
|
+
to: to.version,
|
|
868
|
+
updatedOnOwn: l.updatedOnOwn,
|
|
869
|
+
ownScope: l.ownScope,
|
|
870
|
+
pinnedOrgs: [],
|
|
939
871
|
});
|
|
940
872
|
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
const toNames = await catalogSkillNames(this.storage, to, to.manifest);
|
|
944
|
-
const dirOf = (m, i) => packagePath((m.skills ?? [])[i]?.path);
|
|
945
|
-
const fromSkills = new Map(fromNames.map((n, i) => [n, dirOf(from.manifest, i)]));
|
|
946
|
-
const toSkills = new Map(toNames.map((n, i) => [n, dirOf(to.manifest, i)]));
|
|
947
|
-
const toPinned = new Set(toNames.filter((_, i) => (to.manifest.skills ?? [])[i]?.pinned === true));
|
|
948
|
-
const skillRefs = ledger.filter((l) => l.kind === "skill").map((l) => l.ref);
|
|
949
|
-
const liveSkills = skillRefs.length === 0
|
|
950
|
-
? []
|
|
951
|
-
: await db
|
|
952
|
-
.selectFrom("skills")
|
|
953
|
-
.select(["id", "name"])
|
|
954
|
-
.where("id", "in", skillRefs)
|
|
955
|
-
.where("org_id", "=", orgId)
|
|
956
|
-
.where("deleted_at", "is", null)
|
|
957
|
-
.execute();
|
|
958
|
-
const ledgerOfSkill = (n) => {
|
|
959
|
-
const s = liveSkills.find((x) => x.name === n);
|
|
960
|
-
return s && ledger.find((l) => l.kind === "skill" && l.ref === s.id);
|
|
961
|
-
};
|
|
962
|
-
const skills = [];
|
|
963
|
-
for (const [n, dir] of toSkills) {
|
|
964
|
-
const before = fromSkills.get(n);
|
|
965
|
-
const l = ledgerOfSkill(n);
|
|
966
|
-
const change = before === undefined
|
|
967
|
-
? "added"
|
|
968
|
-
: dirDigest(from, before) === dirDigest(to, dir)
|
|
969
|
-
? "unchanged"
|
|
970
|
-
: "changed";
|
|
971
|
-
skills.push({
|
|
972
|
-
name: n,
|
|
973
|
-
change,
|
|
974
|
-
dir,
|
|
975
|
-
suggestPinned: toPinned.has(n),
|
|
976
|
-
...(l && { ledger: l }),
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
for (const n of fromSkills.keys()) {
|
|
980
|
-
if (toSkills.has(n))
|
|
981
|
-
continue;
|
|
982
|
-
const l = ledgerOfSkill(n);
|
|
983
|
-
skills.push({ name: n, change: "removed", ...(l && { ledger: l }) });
|
|
984
|
-
}
|
|
985
|
-
for (const s of skills)
|
|
986
|
-
components.push({ kind: "skill", ref: s.name, change: s.change });
|
|
987
|
-
// The same skill conflict check as an install (spec §9.2)
|
|
988
|
-
const conflicts = await planSkillConflicts(db, orgId, {
|
|
989
|
-
itemId,
|
|
990
|
-
name,
|
|
991
|
-
version: to.version,
|
|
992
|
-
skillNames: toNames,
|
|
993
|
-
dependencies: dependencies
|
|
994
|
-
.filter((x) => x.change === "added")
|
|
995
|
-
.map((x) => ({ plan: x.plan ?? null })),
|
|
996
|
-
});
|
|
997
|
-
if (conflicts.length > 0)
|
|
998
|
-
throw skillConflictError(conflicts);
|
|
999
|
-
// Agent templates (by name; phase C)
|
|
1000
|
-
const fromAgents = await readCatalogAgentTemplates(this.storage, from);
|
|
1001
|
-
const toAgents = await readCatalogAgentTemplates(this.storage, to);
|
|
1002
|
-
const templateRefs = ledger.filter((l) => l.kind === "agent_template").map((l) => l.ref);
|
|
1003
|
-
const liveTemplates = templateRefs.length === 0
|
|
1004
|
-
? []
|
|
1005
|
-
: await db
|
|
1006
|
-
.selectFrom("agent_templates")
|
|
1007
|
-
.select(["id", "name"])
|
|
1008
|
-
.where("id", "in", templateRefs)
|
|
1009
|
-
.where("org_id", "=", orgId)
|
|
1010
|
-
.where("deleted_at", "is", null)
|
|
1011
|
-
.execute();
|
|
1012
|
-
const ledgerOfTemplate = (n) => {
|
|
1013
|
-
const r = liveTemplates.find((x) => x.name === n);
|
|
1014
|
-
return r && ledger.find((l) => l.kind === "agent_template" && l.ref === r.id);
|
|
1015
|
-
};
|
|
1016
|
-
const agentTemplates = [];
|
|
1017
|
-
for (const a of toAgents) {
|
|
1018
|
-
const n = a.template.name;
|
|
1019
|
-
const before = fromAgents.find((x) => x.template.name === n);
|
|
1020
|
-
const l = ledgerOfTemplate(n);
|
|
1021
|
-
const change = before === undefined
|
|
1022
|
-
? "added"
|
|
1023
|
-
: canonicalJson(before.template) === canonicalJson(a.template)
|
|
1024
|
-
? "unchanged"
|
|
1025
|
-
: "changed";
|
|
1026
|
-
agentTemplates.push({ name: n, change, template: a.template, ...(l && { ledger: l }) });
|
|
1027
|
-
}
|
|
1028
|
-
for (const a of fromAgents) {
|
|
1029
|
-
const n = a.template.name;
|
|
1030
|
-
if (toAgents.some((x) => x.template.name === n))
|
|
873
|
+
for (const l of live.agentTemplates) {
|
|
874
|
+
if (toAgents.some((a) => a.template.name === l.name))
|
|
1031
875
|
continue;
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
876
|
+
components.push({
|
|
877
|
+
kind: "agent_template",
|
|
878
|
+
id: l.id,
|
|
879
|
+
name: l.name,
|
|
880
|
+
change: "removed",
|
|
881
|
+
from: l.catalogVersion,
|
|
882
|
+
to: null,
|
|
883
|
+
updatedOnOwn: l.updatedOnOwn,
|
|
884
|
+
ownScope: l.ownScope,
|
|
885
|
+
pinnedOrgs: [],
|
|
886
|
+
});
|
|
1037
887
|
}
|
|
1038
|
-
// Connector templates (by id)
|
|
1039
|
-
const
|
|
1040
|
-
const toTemplates = await readCatalogTemplates(this.storage, to);
|
|
1041
|
-
// The install's connectors by template; a dropped one (kept only for the agents using it) counts
|
|
1042
|
-
// only while its connector is live, so a template the target brings back maps onto it
|
|
1043
|
-
const connectorRows = ledger.filter((l) => l.kind === "connector" && l.template_id !== null);
|
|
1044
|
-
const liveConnectors = connectorRows.length === 0
|
|
1045
|
-
? []
|
|
1046
|
-
: await db
|
|
1047
|
-
.selectFrom("connectors")
|
|
1048
|
-
.select(["id", "connector", "endpoint", "auth"])
|
|
1049
|
-
.where("id", "in", connectorRows.map((l) => l.ref))
|
|
1050
|
-
.where("org_id", "=", orgId)
|
|
1051
|
-
.where("deleted_at", "is", null)
|
|
1052
|
-
.execute();
|
|
1053
|
-
const connectorOf = (templateId) => {
|
|
1054
|
-
const rows = connectorRows.filter((x) => x.template_id === templateId);
|
|
1055
|
-
const l = rows.find((x) => !x.dropped) ??
|
|
1056
|
-
rows.find((x) => liveConnectors.some((c) => c.id === x.ref));
|
|
1057
|
-
const c = l && liveConnectors.find((x) => x.id === l.ref);
|
|
1058
|
-
return {
|
|
1059
|
-
...(l && { ledger: l }),
|
|
1060
|
-
...(c && {
|
|
1061
|
-
connector: {
|
|
1062
|
-
id: c.id,
|
|
1063
|
-
connector: c.connector,
|
|
1064
|
-
endpoint: c.endpoint,
|
|
1065
|
-
auth: c.auth,
|
|
1066
|
-
},
|
|
1067
|
-
}),
|
|
1068
|
-
};
|
|
1069
|
-
};
|
|
888
|
+
// Connector templates (by id, canonical file) and the connectors made from them
|
|
889
|
+
const toTemplates = contents.templates;
|
|
1070
890
|
const deps = this.installer.templateDeps();
|
|
1071
|
-
const
|
|
891
|
+
const connectors = [];
|
|
1072
892
|
for (const ct of toTemplates) {
|
|
1073
|
-
const
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
if (!before && mapped.ledger?.dropped && mapped.connector) {
|
|
1077
|
-
// Back in the plugin (an earlier upgrade dropped it, its connector was kept): the kept
|
|
1078
|
-
// connector is updated to the template, not created again (its slug would be taken)
|
|
893
|
+
const l = live.templates.find((x) => x.name === ct.id);
|
|
894
|
+
if (!l) {
|
|
895
|
+
const problem = templatePluginProblem(deps, ct.file.template);
|
|
1079
896
|
if (problem !== undefined)
|
|
1080
897
|
throw new ApiError("catalog.not_deployed", problem);
|
|
1081
|
-
|
|
1082
|
-
throw targetInvalid(`Connector template ${ct.id} comes back for another connector (${ct.file.template.plugin} "${ct.file.template.connector}"); uninstall the plugin and install the new version instead`, [
|
|
1083
|
-
{
|
|
1084
|
-
reason: "template_connector",
|
|
1085
|
-
template: ct.id,
|
|
1086
|
-
plugin: ct.file.template.plugin,
|
|
1087
|
-
connector: ct.file.template.connector,
|
|
1088
|
-
},
|
|
1089
|
-
]);
|
|
1090
|
-
}
|
|
1091
|
-
const credentialsChanged = credentialKey(mapped.connector.auth) !==
|
|
1092
|
-
credentialKey(ct.file.template.auth);
|
|
1093
|
-
templates.push({
|
|
1094
|
-
id: ct.id,
|
|
1095
|
-
change: "changed",
|
|
1096
|
-
target: ct.file,
|
|
1097
|
-
...mapped,
|
|
1098
|
-
credentialsChanged,
|
|
1099
|
-
});
|
|
898
|
+
components.push(added("connector_template", ct.id));
|
|
1100
899
|
continue;
|
|
1101
900
|
}
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
901
|
+
const same = canonicalJson(l.file) === canonicalJson(ct.file);
|
|
902
|
+
components.push({
|
|
903
|
+
kind: "connector_template",
|
|
904
|
+
id: l.id,
|
|
905
|
+
name: ct.id,
|
|
906
|
+
change: same ? "same" : direction(l.version),
|
|
907
|
+
from: l.version,
|
|
908
|
+
to: to.version,
|
|
909
|
+
updatedOnOwn: false,
|
|
910
|
+
ownScope: l.ownScope,
|
|
911
|
+
pinnedOrgs: [],
|
|
912
|
+
});
|
|
913
|
+
if (same)
|
|
1106
914
|
continue;
|
|
915
|
+
const before = l.file.template;
|
|
916
|
+
const after = ct.file.template;
|
|
917
|
+
if (before.plugin !== after.plugin || before.connector !== after.connector) {
|
|
918
|
+
throw targetInvalid(`Connector template ${ct.id} moves to another connector (${after.plugin} "${after.connector}"); remove the plugin and import the new version as a new plugin instead`, [
|
|
919
|
+
{
|
|
920
|
+
reason: "template_connector",
|
|
921
|
+
template: ct.id,
|
|
922
|
+
plugin: after.plugin,
|
|
923
|
+
connector: after.connector,
|
|
924
|
+
},
|
|
925
|
+
]);
|
|
1107
926
|
}
|
|
1108
|
-
const
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
]);
|
|
1120
|
-
}
|
|
1121
|
-
if (problem !== undefined && mapped.connector) {
|
|
927
|
+
const made = await db
|
|
928
|
+
.selectFrom("connectors")
|
|
929
|
+
.select(["id", "org_id", "name", "connector", "endpoint", "auth"])
|
|
930
|
+
.where("platform_template_id", "=", l.id)
|
|
931
|
+
.where("deleted_at", "is", null)
|
|
932
|
+
.orderBy("org_id")
|
|
933
|
+
.orderBy("id")
|
|
934
|
+
.execute();
|
|
935
|
+
if (made.length > 0) {
|
|
936
|
+
const problem = templatePluginProblem(deps, after);
|
|
937
|
+
if (problem !== undefined)
|
|
1122
938
|
throw new ApiError("catalog.not_deployed", problem);
|
|
1123
|
-
}
|
|
1124
939
|
}
|
|
1125
|
-
const
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
940
|
+
for (const c of made) {
|
|
941
|
+
connectors.push({
|
|
942
|
+
orgId: c.org_id,
|
|
943
|
+
id: c.id,
|
|
944
|
+
name: c.name,
|
|
945
|
+
connector: c.connector,
|
|
946
|
+
endpoint: c.endpoint,
|
|
947
|
+
templateId: ct.id,
|
|
948
|
+
file: ct.file,
|
|
949
|
+
credentialsReset: credentialKey(c.auth) !==
|
|
950
|
+
credentialKey(after.auth),
|
|
951
|
+
});
|
|
952
|
+
}
|
|
1130
953
|
}
|
|
1131
|
-
for (const
|
|
1132
|
-
if (toTemplates.some((x) => x.id ===
|
|
954
|
+
for (const l of live.templates) {
|
|
955
|
+
if (toTemplates.some((x) => x.id === l.name))
|
|
1133
956
|
continue;
|
|
1134
|
-
templates.push({
|
|
1135
|
-
id: ct.id,
|
|
1136
|
-
change: "removed",
|
|
1137
|
-
...connectorOf(ct.id),
|
|
1138
|
-
credentialsChanged: false,
|
|
1139
|
-
});
|
|
1140
|
-
}
|
|
1141
|
-
for (const c of templates) {
|
|
1142
957
|
components.push({
|
|
1143
|
-
kind: "
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
958
|
+
kind: "connector_template",
|
|
959
|
+
id: l.id,
|
|
960
|
+
name: l.name,
|
|
961
|
+
change: "removed",
|
|
962
|
+
from: l.version,
|
|
963
|
+
to: null,
|
|
964
|
+
updatedOnOwn: false,
|
|
965
|
+
ownScope: l.ownScope,
|
|
966
|
+
pinnedOrgs: [],
|
|
1150
967
|
});
|
|
1151
968
|
}
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
for (const
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
969
|
+
// The orgs where the plugin is on (a live install; builtin: every org)
|
|
970
|
+
const onOrgs = await this.pluginOrgs(itemId, item.builtin);
|
|
971
|
+
if (extension)
|
|
972
|
+
extension = { ...extension, orgs: onOrgs };
|
|
973
|
+
// Rule 10: an added skill must not take a name already used where the plugin is on
|
|
974
|
+
const conflicts = [];
|
|
975
|
+
for (const c of components) {
|
|
976
|
+
if (c.kind !== "skill" || c.change !== "added")
|
|
977
|
+
continue;
|
|
978
|
+
for (const orgId of onOrgs) {
|
|
979
|
+
const ids = [
|
|
980
|
+
...(await clashingOrgSkills(db, orgId, { id: "", itemId }, c.name)),
|
|
981
|
+
...(await clashingPlatformSkills(db, orgId, { id: "" }, c.name)).map((x) => x.id),
|
|
982
|
+
];
|
|
983
|
+
if (ids.length > 0)
|
|
984
|
+
conflicts.push({ skill: c.name, orgId, conflicts: ids.sort() });
|
|
1168
985
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
986
|
+
}
|
|
987
|
+
if (conflicts.length > 0) {
|
|
988
|
+
throw new ApiError("plugin.skill_conflict", `The update adds skills whose names organizations already use where the plugin is on (${[...new Set(conflicts.map((c) => c.skill))].join(", ")}); remove or rename those skills first`, conflicts);
|
|
989
|
+
}
|
|
990
|
+
// Pinned copies (in orgs where the component is on) and removed components agents use
|
|
991
|
+
for (const kind of ["skill", "agent_template"]) {
|
|
992
|
+
const mine = components.filter((c) => c.kind === kind && c.id !== null);
|
|
993
|
+
if (mine.length === 0)
|
|
994
|
+
continue;
|
|
995
|
+
const pins = kind === "skill"
|
|
996
|
+
? await db
|
|
997
|
+
.selectFrom("skills")
|
|
998
|
+
.select(["org_id", "platform_skill_id as platform_id"])
|
|
999
|
+
.where("platform_skill_id", "in", mine.map((c) => c.id))
|
|
1000
|
+
.where("pinned_version", "is not", null)
|
|
1001
|
+
.where("deleted_at", "is", null)
|
|
1002
|
+
.execute()
|
|
1003
|
+
: await db
|
|
1004
|
+
.selectFrom("agent_templates")
|
|
1005
|
+
.select(["org_id", "platform_template_id as platform_id"])
|
|
1006
|
+
.where("platform_template_id", "in", mine.map((c) => c.id))
|
|
1007
|
+
.where("pinned_version", "is not", null)
|
|
1008
|
+
.where("deleted_at", "is", null)
|
|
1009
|
+
.execute();
|
|
1010
|
+
const facts = mine.map((c) => {
|
|
1011
|
+
const l = (kind === "skill" ? live.skills : live.agentTemplates).find((x) => x.id === c.id);
|
|
1012
|
+
return {
|
|
1013
|
+
id: c.id,
|
|
1014
|
+
itemId,
|
|
1015
|
+
scopeMode: l?.scopeMode ?? "follow",
|
|
1016
|
+
scope: l?.scope ?? "optional",
|
|
1017
|
+
disabled: l?.disabled ?? false,
|
|
1018
|
+
};
|
|
1019
|
+
});
|
|
1020
|
+
const st = await loadScopeState(db, kind, facts);
|
|
1021
|
+
for (const c of mine) {
|
|
1022
|
+
const f = facts.find((x) => x.id === c.id);
|
|
1023
|
+
if (!f)
|
|
1172
1024
|
continue;
|
|
1173
|
-
|
|
1174
|
-
|
|
1025
|
+
const orgsPinned = [
|
|
1026
|
+
...new Set(pins.filter((p) => p.platform_id === c.id).map((p) => p.org_id)),
|
|
1027
|
+
];
|
|
1028
|
+
c.pinnedOrgs = orgsPinned
|
|
1029
|
+
.filter((orgId) => componentOn(f, pluginFactsOf(st, itemId, orgId), listedIn(st, f.id, orgId), switchOf(st, f.id, orgId)))
|
|
1030
|
+
.sort();
|
|
1031
|
+
if (c.change !== "removed")
|
|
1175
1032
|
continue;
|
|
1176
|
-
|
|
1033
|
+
const kept = [];
|
|
1034
|
+
for (const orgId of await this.liveOrgs()) {
|
|
1035
|
+
const agents = await agentsUsing(db, orgId, kind, [c.id]);
|
|
1036
|
+
if (agents.length > 0) {
|
|
1037
|
+
kept.push({ orgId, agents: agents.map((a) => ({ id: a.id, name: a.name })) });
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
if (kept.length > 0)
|
|
1041
|
+
c.kept = kept;
|
|
1177
1042
|
}
|
|
1178
1043
|
}
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
.
|
|
1193
|
-
.
|
|
1194
|
-
.
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1044
|
+
// The org connectors: changed templates' connectors, added templates needing input
|
|
1045
|
+
const addedTemplates = components
|
|
1046
|
+
.filter((c) => c.kind === "connector_template" && c.change === "added")
|
|
1047
|
+
.map((c) => c.name);
|
|
1048
|
+
const byOrg = new Map();
|
|
1049
|
+
const entry = (orgId) => {
|
|
1050
|
+
const e = byOrg.get(orgId) ?? { orgId, connectors: [], needsConfig: [] };
|
|
1051
|
+
byOrg.set(orgId, e);
|
|
1052
|
+
return e;
|
|
1053
|
+
};
|
|
1054
|
+
for (const c of connectors) {
|
|
1055
|
+
entry(c.orgId).connectors.push({
|
|
1056
|
+
id: c.id,
|
|
1057
|
+
name: c.name,
|
|
1058
|
+
templateId: c.templateId,
|
|
1059
|
+
credentialsReset: c.credentialsReset,
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
if (addedTemplates.length > 0) {
|
|
1063
|
+
for (const orgId of onOrgs) {
|
|
1064
|
+
const on = await connectorTemplatesOn(db, orgId, itemId, addedTemplates);
|
|
1065
|
+
if (on.size > 0)
|
|
1066
|
+
entry(orgId).needsConfig.push(...[...on].sort());
|
|
1067
|
+
}
|
|
1198
1068
|
}
|
|
1199
|
-
const
|
|
1069
|
+
const connectorOrgs = [...byOrg.values()]
|
|
1070
|
+
.map((e) => ({
|
|
1071
|
+
...e,
|
|
1072
|
+
connectors: e.connectors.sort((a, b) => `${a.templateId}\0${a.id}`.localeCompare(`${b.templateId}\0${b.id}`)),
|
|
1073
|
+
}))
|
|
1074
|
+
.sort((a, b) => a.orgId.localeCompare(b.orgId));
|
|
1075
|
+
components.sort((a, b) => KIND_ORDER[a.kind] - KIND_ORDER[b.kind] || a.name.localeCompare(b.name));
|
|
1200
1076
|
const diffHash = sha256(canonicalJson({
|
|
1201
|
-
|
|
1202
|
-
from
|
|
1077
|
+
itemId,
|
|
1078
|
+
from,
|
|
1203
1079
|
to: to.version,
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1080
|
+
manifest: to.manifest,
|
|
1081
|
+
files: filesDigest(to),
|
|
1082
|
+
// `needsApproval` is shown only: it does not change what the confirm does
|
|
1083
|
+
extension: extension && {
|
|
1084
|
+
from: extension.from,
|
|
1085
|
+
to: extension.to,
|
|
1086
|
+
orgs: extension.orgs,
|
|
1087
|
+
sha256: extension.sha256,
|
|
1088
|
+
},
|
|
1207
1089
|
components,
|
|
1208
|
-
|
|
1090
|
+
connectors: connectors.map((c) => ({
|
|
1091
|
+
id: c.id,
|
|
1092
|
+
endpoint: c.endpoint,
|
|
1093
|
+
reset: c.credentialsReset,
|
|
1094
|
+
})),
|
|
1095
|
+
connectorOrgs,
|
|
1209
1096
|
}));
|
|
1210
1097
|
return {
|
|
1211
|
-
installId,
|
|
1212
1098
|
itemId,
|
|
1213
1099
|
name,
|
|
1214
1100
|
from,
|
|
1215
1101
|
to,
|
|
1216
|
-
|
|
1102
|
+
components,
|
|
1217
1103
|
extension,
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1104
|
+
connectors,
|
|
1105
|
+
connectorOrgs,
|
|
1106
|
+
suggest,
|
|
1107
|
+
contents,
|
|
1221
1108
|
dependencies,
|
|
1222
|
-
components,
|
|
1223
|
-
inputs,
|
|
1224
1109
|
diffHash,
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1112
|
+
/** The target's skill names, agent templates and connector templates, from object storage. */
|
|
1113
|
+
async readContents(to) {
|
|
1114
|
+
return {
|
|
1115
|
+
skillNames: await catalogSkillNames(this.storage, to, to.manifest),
|
|
1116
|
+
agents: await readCatalogAgentTemplates(this.storage, to),
|
|
1117
|
+
templates: await readCatalogTemplates(this.storage, to),
|
|
1229
1118
|
};
|
|
1230
1119
|
}
|
|
1231
1120
|
/**
|
|
1232
|
-
*
|
|
1233
|
-
*
|
|
1234
|
-
* the credentials it needs changed (then the input must give what the template needs).
|
|
1121
|
+
* Whether the target's extension package asks for permissions the approved snapshot does not
|
|
1122
|
+
* cover (no snapshot, or a blocking addition): it then loads only once approved (§7.4).
|
|
1235
1123
|
*/
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
const
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1124
|
+
async needsApproval(name, sha) {
|
|
1125
|
+
if (this.config.get().extensionPermissions.autoApprove)
|
|
1126
|
+
return false;
|
|
1127
|
+
const pkg = await readExtensionPackage(await this.blobs.get(sha), {
|
|
1128
|
+
maxBytes: this.config.get().packages.maxBytes,
|
|
1129
|
+
hostSdkVersion: SDK_VERSION,
|
|
1130
|
+
});
|
|
1131
|
+
const snap = await this.db
|
|
1132
|
+
.selectFrom("extension_permission_snapshots")
|
|
1133
|
+
.select("permissions")
|
|
1134
|
+
.where("plugin", "=", name)
|
|
1135
|
+
.executeTakeFirst();
|
|
1136
|
+
if (!snap)
|
|
1137
|
+
return true;
|
|
1138
|
+
const { added } = diffPermissions(snap.permissions, permissionSetOfPackage(pkg));
|
|
1139
|
+
return blockingKeys(added).length > 0;
|
|
1140
|
+
}
|
|
1141
|
+
/**
|
|
1142
|
+
* The deployment's extension step: none when the target carries the extension the deployment
|
|
1143
|
+
* runs; a version without the extension the deployment runs for the plugin is refused, except
|
|
1144
|
+
* for an owner update (`allowRemove`, ruling P3-12': removing code runs none), which removes an
|
|
1145
|
+
* installed one; a move of a config-declared extension is refused.
|
|
1146
|
+
*/
|
|
1147
|
+
async extensionTarget(name, from, to, allowRemove = false) {
|
|
1148
|
+
const want = to.manifest.extension;
|
|
1149
|
+
const row = await this.db
|
|
1150
|
+
.selectFrom("extension_installs")
|
|
1151
|
+
.select(["sha256", "version"])
|
|
1152
|
+
.where("name", "=", name)
|
|
1153
|
+
.executeTakeFirst();
|
|
1154
|
+
// Only an extension declared in the startup config runs without an install record; one this
|
|
1155
|
+
// replica still has loaded after its uninstall (until its reconcile) does not count
|
|
1156
|
+
const declared = this.registry.declared().get(name) === "config";
|
|
1157
|
+
const loaded = declared ? this.registry.manifests().find((m) => m.name === name) : undefined;
|
|
1158
|
+
if (!want) {
|
|
1159
|
+
if (allowRemove && from?.manifest.extension && row && !declared) {
|
|
1160
|
+
return { from: row.version, to: null, sha256: null, previous: row.sha256 };
|
|
1246
1161
|
}
|
|
1162
|
+
if (from?.manifest.extension && (row || declared)) {
|
|
1163
|
+
throw targetInvalid(`Version ${to.version} of plugin ${name} has no extension, but the deployment runs it; uninstall the extension first`, [{ reason: "extension_removed", version: to.version }]);
|
|
1164
|
+
}
|
|
1165
|
+
return null;
|
|
1247
1166
|
}
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
if (v !== undefined)
|
|
1252
|
-
setPath(endpoint, e, v);
|
|
1167
|
+
if (row) {
|
|
1168
|
+
if (row.sha256 === to.extension_sha256)
|
|
1169
|
+
return null;
|
|
1253
1170
|
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
}
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
}
|
|
1264
|
-
if (!oauth && given?.client !== undefined) {
|
|
1265
|
-
throw fail(`${path}.client`, "This connector does not use OAuth; provide credential instead");
|
|
1266
|
-
}
|
|
1171
|
+
else if (declared) {
|
|
1172
|
+
if (loaded?.version === want.manifest.version)
|
|
1173
|
+
return null;
|
|
1174
|
+
throw targetInvalid(`The extension of plugin ${name} is declared in the startup config at version ${loaded?.version ?? "(not loaded)"}; it cannot be moved to ${want.manifest.version} by an update`, [{ reason: "config_extension", deploymentVersion: loaded?.version ?? null }]);
|
|
1175
|
+
}
|
|
1176
|
+
if (to.extension_sha256 === null) {
|
|
1177
|
+
throw targetInvalid(`Version ${to.version} of plugin ${name} has no extension package`, [
|
|
1178
|
+
{ reason: "extension_package", version: to.version },
|
|
1179
|
+
]);
|
|
1267
1180
|
}
|
|
1268
|
-
const credential = oauth ? given?.client : given?.credential;
|
|
1269
1181
|
return {
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
version,
|
|
1275
|
-
templateId: c.id,
|
|
1276
|
-
...(template.defaultToolPolicies !== undefined && {
|
|
1277
|
-
defaultToolPolicies: template.defaultToolPolicies,
|
|
1278
|
-
}),
|
|
1279
|
-
},
|
|
1280
|
-
resetCredentials: c.credentialsChanged,
|
|
1281
|
-
...(c.credentialsChanged && credential !== undefined && { credential }),
|
|
1182
|
+
from: row?.version ?? null,
|
|
1183
|
+
to: want.manifest.version,
|
|
1184
|
+
sha256: to.extension_sha256,
|
|
1185
|
+
previous: row?.sha256 ?? null,
|
|
1282
1186
|
};
|
|
1283
1187
|
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Other platform plugins depending on this one must accept the target (their platform version's
|
|
1190
|
+
* dependency range); returns the catalog items of the target's own dependencies (their scope
|
|
1191
|
+
* sync is enqueued after the update: rule 9).
|
|
1192
|
+
*/
|
|
1193
|
+
async checkDependents(name, to) {
|
|
1194
|
+
const rows = await this.db
|
|
1195
|
+
.selectFrom("catalog_items as i")
|
|
1196
|
+
.innerJoin("catalog_item_versions as v", (j) => j
|
|
1197
|
+
.onRef("v.item_id", "=", "i.id")
|
|
1198
|
+
.on(sql `v.version`, "=", sql `coalesce(i.platform_synced_version, i.latest_version)`))
|
|
1199
|
+
.select(["i.id", "i.name", "v.manifest"])
|
|
1200
|
+
.where("i.deleted_at", "is", null)
|
|
1201
|
+
.execute();
|
|
1202
|
+
for (const r of rows) {
|
|
1203
|
+
if (r.name === name)
|
|
1204
|
+
continue;
|
|
1205
|
+
const range = r.manifest.dependencies?.find((d) => d.name === name)?.version;
|
|
1206
|
+
if (range !== undefined && !satisfiesRange(to.version, range)) {
|
|
1207
|
+
throw targetInvalid(`Plugin ${r.name} depends on ${name}@${range}, which version ${to.version} does not satisfy`, [{ reason: "dependent_range", dependent: r.name, range, version: to.version }]);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
const wanted = new Set((to.manifest.dependencies ?? []).map((d) => d.name));
|
|
1211
|
+
return rows.filter((r) => wanted.has(r.name)).map((r) => r.id);
|
|
1212
|
+
}
|
|
1213
|
+
/** The plugin's live platform components with their current versions. */
|
|
1214
|
+
async liveComponents(itemId) {
|
|
1215
|
+
const db = this.db;
|
|
1216
|
+
const skills = (await db
|
|
1217
|
+
.selectFrom("platform_skills as p")
|
|
1218
|
+
.innerJoin("platform_skill_versions as v", (j) => j.onRef("v.skill_id", "=", "p.id").onRef("v.version", "=", "p.current_version"))
|
|
1219
|
+
.select([
|
|
1220
|
+
"p.id",
|
|
1221
|
+
"p.name",
|
|
1222
|
+
"p.scope_mode",
|
|
1223
|
+
"p.scope",
|
|
1224
|
+
"p.disabled",
|
|
1225
|
+
"v.manifest",
|
|
1226
|
+
"v.catalog_version",
|
|
1227
|
+
"v.updated_on_own",
|
|
1228
|
+
"v.suggest_pinned",
|
|
1229
|
+
])
|
|
1230
|
+
.where("p.catalog_item_id", "=", itemId)
|
|
1231
|
+
.where("p.deleted_at", "is", null)
|
|
1232
|
+
.execute()).map((r) => ({
|
|
1233
|
+
id: r.id,
|
|
1234
|
+
name: r.name,
|
|
1235
|
+
scopeMode: r.scope_mode,
|
|
1236
|
+
scope: r.scope,
|
|
1237
|
+
disabled: r.disabled,
|
|
1238
|
+
manifest: r.manifest,
|
|
1239
|
+
catalogVersion: r.catalog_version,
|
|
1240
|
+
updatedOnOwn: r.updated_on_own,
|
|
1241
|
+
ownScope: r.scope_mode === "own",
|
|
1242
|
+
suggestPinned: r.suggest_pinned,
|
|
1243
|
+
}));
|
|
1244
|
+
const agentTemplates = (await db
|
|
1245
|
+
.selectFrom("platform_agent_templates as p")
|
|
1246
|
+
.innerJoin("platform_agent_template_versions as v", (j) => j.onRef("v.template_id", "=", "p.id").onRef("v.version", "=", "p.current_version"))
|
|
1247
|
+
.select([
|
|
1248
|
+
"p.id",
|
|
1249
|
+
"p.name",
|
|
1250
|
+
"p.scope_mode",
|
|
1251
|
+
"p.scope",
|
|
1252
|
+
"p.disabled",
|
|
1253
|
+
"v.definition",
|
|
1254
|
+
"v.catalog_version",
|
|
1255
|
+
"v.updated_on_own",
|
|
1256
|
+
])
|
|
1257
|
+
.where("p.catalog_item_id", "=", itemId)
|
|
1258
|
+
.where("p.deleted_at", "is", null)
|
|
1259
|
+
.execute()).map((r) => ({
|
|
1260
|
+
id: r.id,
|
|
1261
|
+
name: r.name,
|
|
1262
|
+
scopeMode: r.scope_mode,
|
|
1263
|
+
scope: r.scope,
|
|
1264
|
+
disabled: r.disabled,
|
|
1265
|
+
definition: r.definition,
|
|
1266
|
+
catalogVersion: r.catalog_version,
|
|
1267
|
+
updatedOnOwn: r.updated_on_own,
|
|
1268
|
+
ownScope: r.scope_mode === "own",
|
|
1269
|
+
}));
|
|
1270
|
+
const templates = (await db
|
|
1271
|
+
.selectFrom("platform_connector_templates")
|
|
1272
|
+
.select(["id", "template_id", "current_version", "file", "scope_mode"])
|
|
1273
|
+
.where("catalog_item_id", "=", itemId)
|
|
1274
|
+
.where("deleted_at", "is", null)
|
|
1275
|
+
.execute()).map((r) => ({
|
|
1276
|
+
id: r.id,
|
|
1277
|
+
name: r.template_id,
|
|
1278
|
+
version: r.current_version,
|
|
1279
|
+
file: r.file,
|
|
1280
|
+
ownScope: r.scope_mode === "own",
|
|
1281
|
+
}));
|
|
1282
|
+
return { skills, agentTemplates, templates };
|
|
1283
|
+
}
|
|
1284
|
+
/** The orgs where the plugin is on: a live install (builtin: every live org). */
|
|
1285
|
+
async pluginOrgs(itemId, builtin) {
|
|
1286
|
+
if (builtin)
|
|
1287
|
+
return this.liveOrgs();
|
|
1288
|
+
const rows = await this.db
|
|
1289
|
+
.selectFrom("plugin_installs as a")
|
|
1290
|
+
.innerJoin("orgs as o", "o.id", "a.org_id")
|
|
1291
|
+
.select("a.org_id")
|
|
1292
|
+
.distinct()
|
|
1293
|
+
.where("a.item_id", "=", itemId)
|
|
1294
|
+
.where("a.status", "in", ["installing", "installed", "upgrading"])
|
|
1295
|
+
.where("o.deleted_at", "is", null)
|
|
1296
|
+
.orderBy("a.org_id")
|
|
1297
|
+
.execute();
|
|
1298
|
+
return rows.map((r) => r.org_id);
|
|
1299
|
+
}
|
|
1300
|
+
async liveOrgs() {
|
|
1301
|
+
const rows = await this.db
|
|
1302
|
+
.selectFrom("orgs")
|
|
1303
|
+
.select("id")
|
|
1304
|
+
.where("deleted_at", "is", null)
|
|
1305
|
+
.orderBy("id")
|
|
1306
|
+
.execute();
|
|
1307
|
+
return rows.map((r) => r.id);
|
|
1308
|
+
}
|
|
1284
1309
|
};
|
|
1285
1310
|
__decorate([
|
|
1286
1311
|
Transactional()
|
|
1287
|
-
],
|
|
1312
|
+
], PlatformPluginUpdater.prototype, "authorizeOwner", null);
|
|
1288
1313
|
__decorate([
|
|
1289
1314
|
Transactional({ propagation: "requires_new" })
|
|
1290
|
-
],
|
|
1315
|
+
], PlatformPluginUpdater.prototype, "auditFailure", null);
|
|
1291
1316
|
__decorate([
|
|
1292
1317
|
Transactional({ propagation: "requires_new" })
|
|
1293
|
-
],
|
|
1294
|
-
__decorate([
|
|
1295
|
-
Transactional({ propagation: "requires_new" })
|
|
1296
|
-
], PluginUpgrader.prototype, "upgradeAgentTemplate", null);
|
|
1297
|
-
__decorate([
|
|
1298
|
-
Transactional({ propagation: "requires_new" })
|
|
1299
|
-
], PluginUpgrader.prototype, "recordStep", null);
|
|
1300
|
-
__decorate([
|
|
1301
|
-
Transactional({ propagation: "requires_new" })
|
|
1302
|
-
], PluginUpgrader.prototype, "finish", null);
|
|
1303
|
-
__decorate([
|
|
1304
|
-
Transactional({ propagation: "requires_new" })
|
|
1305
|
-
], PluginUpgrader.prototype, "undoFailed", null);
|
|
1306
|
-
__decorate([
|
|
1307
|
-
Transactional({ propagation: "requires_new" })
|
|
1308
|
-
], PluginUpgrader.prototype, "undoInterrupted", null);
|
|
1309
|
-
__decorate([
|
|
1310
|
-
Transactional({ propagation: "mandatory" })
|
|
1311
|
-
], PluginUpgrader.prototype, "removeIn", null);
|
|
1312
|
-
PluginUpgrader = __decorate([
|
|
1318
|
+
], PlatformPluginUpdater.prototype, "commit", null);
|
|
1319
|
+
PlatformPluginUpdater = __decorate([
|
|
1313
1320
|
Injectable(),
|
|
1314
|
-
__param(0, Inject(
|
|
1315
|
-
__param(1, Inject(
|
|
1316
|
-
__param(2, Inject(
|
|
1317
|
-
__param(3, Inject(
|
|
1318
|
-
__param(4, Inject(
|
|
1319
|
-
__param(5, Inject(
|
|
1320
|
-
__param(6, Inject(
|
|
1321
|
-
__param(7, Inject(
|
|
1322
|
-
__param(8, Inject(
|
|
1323
|
-
__param(9, Inject(
|
|
1324
|
-
__param(10, Inject(
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
.
|
|
1336
|
-
.
|
|
1321
|
+
__param(0, Inject(KernelDb)),
|
|
1322
|
+
__param(1, Inject(TransactionHost)),
|
|
1323
|
+
__param(2, Inject(SuperAdminLock)),
|
|
1324
|
+
__param(3, Inject(CatalogItemLock)),
|
|
1325
|
+
__param(4, Inject(OrgLock)),
|
|
1326
|
+
__param(5, Inject(LockService)),
|
|
1327
|
+
__param(6, Inject(StorageService)),
|
|
1328
|
+
__param(7, Inject(ExtensionRegistry)),
|
|
1329
|
+
__param(8, Inject(ExtensionAdminOps)),
|
|
1330
|
+
__param(9, Inject(ExtensionBlobs)),
|
|
1331
|
+
__param(10, Inject(KernelConfigService)),
|
|
1332
|
+
__param(11, Inject(PlatformComponents)),
|
|
1333
|
+
__param(12, Inject(PluginInstaller)),
|
|
1334
|
+
__param(13, Inject(ConnectorsService)),
|
|
1335
|
+
__param(14, Inject(PluginScopeSync)),
|
|
1336
|
+
__param(15, Inject(KERNEL_LOGGER))
|
|
1337
|
+
], PlatformPluginUpdater);
|
|
1338
|
+
export { PlatformPluginUpdater };
|
|
1339
|
+
/** The preview of a diff. */
|
|
1340
|
+
function previewOf(d) {
|
|
1341
|
+
return {
|
|
1342
|
+
itemId: d.itemId,
|
|
1343
|
+
name: d.name,
|
|
1344
|
+
from: d.from,
|
|
1345
|
+
to: d.to.version,
|
|
1346
|
+
components: d.components,
|
|
1347
|
+
extension: extensionView(d.extension),
|
|
1348
|
+
connectorOrgs: d.connectorOrgs,
|
|
1349
|
+
diffHash: d.diffHash,
|
|
1350
|
+
};
|
|
1337
1351
|
}
|
|
1338
|
-
/**
|
|
1339
|
-
function
|
|
1340
|
-
return
|
|
1341
|
-
.map((r) => `${r.kind}\0${r.ref}\0${r.created}\0${r.skill_version}\0${r.template_id}\0${r.dropped}\n`)
|
|
1342
|
-
.sort()
|
|
1343
|
-
.join(""));
|
|
1352
|
+
/** The extension step as the preview and the result show it. */
|
|
1353
|
+
function extensionView(e) {
|
|
1354
|
+
return e && { from: e.from, to: e.to, orgs: e.orgs, needsApproval: e.needsApproval };
|
|
1344
1355
|
}
|