Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(ai): enforce a key's model list at run time and guard capability and typed-in model keys (#16207)

Louai Boumediene1e44d04f2522
diff --git a/brain/knowledge/ai-intelligence/ai-providers.md b/brain/knowledge/ai-intelligence/ai-providers.md
index 49b83f4c431..3b7f4b27265 100644
--- a/brain/knowledge/ai-intelligence/ai-providers.md
+++ b/brain/knowledge/ai-intelligence/ai-providers.md
@@ -73,7 +73,8 @@ after a file without chat keys it seeds the chat list from the live flow list.
**Platform tier** — an admin-built, platform-owned list of up to 5 `{configId, modelId}` entries (main + ranked fallbacks, any keys), table `platform_model_tier`, API `/v1/platform-model-tiers`. A tier follows key scope: in a project it runs only the entries whose key serves that project, and it is listed there only when its main entry can run; see decision [000048](../decisions/000048-platform-tiers-follow-key-project-scope.md) (replaces 000044's grant). _Avoid_: "model tier" alone — that is the console **credits tier** (`ModelTier`, above).
### Gotchas
-- **A key a live platform tier uses cannot be deleted, and its `modelScope` cannot drop a tier's model.** `aiProviderService.delete` and `update` call `platformModelTierService.assertKeyCanBeDeleted` / `assertKeyScopeKeepsTiers` under a `platform` row `FOR NO KEY UPDATE` lock and answer 409 with the tier names. The same lock serialises every tier write, so a tier save can't race a key delete into a dangling entry.
+- **A key a live platform tier uses cannot be deleted, and its `modelScope` cannot drop a tier's model.** `aiProviderService.delete` and `update` call `platformModelTierService.assertKeyCanBeDeleted` / `assertKeyScopeKeepsTiers` under a `platform` row `FOR NO KEY UPDATE` lock and answer 409 with the tier names. The same lock serialises every tier write, so a tier save can't race a key delete into a dangling entry. The same update also refuses removing a typed-in model (Vertex, OpenAI-compatible, Cloudflare `config.models`) that a tier uses, even when `modelScope` is `all`, and limiting the projects of a key that a capability (web search, scraping, image) uses, or dropping the capability's `modelId` from its model list: capability keys must serve every project and keep their model (`assertCapabilitiesKeepKey`).
+- **A key's model list is enforced when a specific model runs, not only in pickers.** `/v1/ai/execute` checks `aiProviderService.assertModelAllowed` before queueing (the managed Activepieces key is skipped: it allows every model), and the worker's `resolveAiProvider` RPC sends `modelId` so the server checks it again against the key it resolves at run time (a model removed while the job waited fails then). `resolveNamedModelId` refuses any own-key model outside the list with no tier-id exemption: an own key never translates `smart`/`fast`, so a name that looks like a tier is just a customer model id. At platform scope, `rankRows` prefers keys open to every project; at project scope it still prefers the most specific key. Key edits and capability choices both take the `platform` row lock, and a capability save re-reads its key under that lock, so the two cannot interleave. One helper, `aiKeyScope.keyOffersModel`, decides whether a key still offers a model: its model scope, plus its typed-in `config.models` list when it has one. The tier and capability guards on a key edit both use it, and so does the locked re-check when a capability is saved. A key edit that removes a typed-in model a capability uses is refused like a model-scope change.
- **Deleting a platform tier needs a replacement, except the last live tier, and there is no usage count.** Refs to a deleted tier are never rewritten; they reach a live tier through `replacedBy` (flattened to one hop on delete). The last tier may always go with no replacement: without that exit, a platform's last tier and the key it uses could never be removed, because the key guard refuses keys a live tier uses. Tier delete is also deliberately **not** plan-gated (writes are), so a platform that downgrades can still clear its tiers and then remove its key. Its refs then point at a tier with `replacedBy: null`, and the resolver must fail them with a clear "tier removed" error. An exact "used by N flows" count needs a `LIKE` scan over every flow version's `trigger` text, plus agent jsonb and conversations, on the whole platform. We dropped it rather than pay that. If counts come back, add a denormalized `flow_version.modelTierIds` array + GIN index (the `connectionIds` pattern) instead of scanning.
- **The live-name unique index is `lower("name")`, which `EntitySchema` cannot express.** The entity declares it `synchronize: false` (so `check-migrations` skips it), which means test DBs built by `synchronize` don't have it. The case-insensitive check that tests rely on is the service's `assertNameIsFree`, which is race-free because it runs under the `platform` row lock; the index is the backstop.
- **`lockPlatform` takes `FOR NO KEY UPDATE`, not `FOR UPDATE`.** Every FK insert that references `platform` (projects, users, …) takes `FOR KEY SHARE` on the platform row, which `FOR UPDATE` blocks until commit. `FOR NO KEY UPDATE` serialises tier writes just the same without stalling those inserts.
diff --git a/brain/knowledge/engineering/ci-pr-review-hygiene.md b/brain/knowledge/engineering/ci-pr-review-hygiene.md
index 137583b7138..5dafd61710b 100644
--- a/brain/knowledge/engineering/ci-pr-review-hygiene.md
+++ b/brain/knowledge/engineering/ci-pr-review-hygiene.md
@@ -22,6 +22,7 @@ Which team gets asked to review comes entirely from `.github/CODEOWNERS` — the
Enforcement is the **`Codeowners review` repository ruleset** (active on the default branch), not classic branch protection: `require_code_owner_review: true` plus `required_approving_review_count: 1` and `required_review_thread_resolution: true`. Eight bypass actors are configured, which is why an owner-team request can look non-blocking on some PRs.
## Gotchas
+- **A clean merge of main can silently cancel your package version bump.** If main bumped the same package to the same version your branch did (seen 2026-10-08: both at core-execution `0.26.2`), `package.json` merges with no conflict. Your change then ships under a version main already published. After every main merge, compare each package your branch touches against `git show origin/main:<path>/package.json` and bump again if they match, then fix its `bun.lock` line.
- **When merging main, `rerere` can replay a stale `bun.lock` resolution.** "Staged 'bun.lock' using previous resolution" means git reused an old answer, which can carry workspace versions that no longer match any `package.json` (seen: core-utils `0.8.6` in the lockfile against `0.8.5` in the manifest). If the branch adds no dependencies, take main's `bun.lock` and edit only the branch's own workspace `version` lines. Expect `bun install` to then rewrite a few lines for pieces the branch never touched: piece PRs on `main` often bump a `package.json` without its `bun.lock` entry (seen 2026-10-05: grist, microsoft-todo, ticktick, zendesk). Those lines are main catching up, not your change; keep them, because the lock should match the manifests.
- **Any change to `bun.lock` runs every package's tests in `build-test`, all 700+ pieces included.** The `changes` job in `ci.yml` sets `all=true` when a changed file is outside `packages/`, `docs/`, `brain/`, `.agents/` and `.claude/` and is not a `*.md` file (so root Markdown alone does not count), and `tools/scripts/test-filters.ts` then adds every package with a `test` script **except `api`** (it lists with `--filter=!api`). API tests run in the separate `api-tests` matrix job (`api (ce)`, `api (cloud)`, `api (ee, unit, migrations)`), so look there for their results, not in `build-test`. Without that, a piece only runs when its own files or `pieces/framework` / `pieces/common` change; a `core-utils` change alone does not pull pieces in. Any workspace version bump edits `bun.lock`, so this hits most PRs that bump a package. A CPU-heavy piece test can then fail on the starved runner with no change of yours: an Odoo polling test that takes ~350ms locally passed vitest's 5s default in 3 of 3 runs, reruns included. Don't keep rerunning: give the heavy test an explicit timeout (third `it` argument). Even a test-only change needs that piece's patch bump (and its `bun.lock` line): `validate-publishable-packages` diffs the whole package folder against `origin/main`.
- **After the base PR of a stack merges, merging main into the next PR silently keeps the base PR's old lines.** The merge base predates the base PR, so code the base PR dropped during review (fields, routes, columns) looks like a branch addition and survives with no conflict. Check with `git diff --name-only origin/main`: every file must be one the stacked PR itself changed. If not, save the stacked PR's own patch first (`git diff --binary <base-PR tip it was built on> <head before the merge> > pr.patch`; without `--binary`, an image or other binary file is written as "Binary files differ" and `read-tree` deletes it with no way back), then `git read-tree -u --reset origin/main` and `git apply --3way pr.patch`, and commit. `read-tree` also deletes files that exist only on the branch (a file the base PR added, then dropped), which `git checkout origin/main -- .` would leave behind. Use `origin/main`, not `MERGE_HEAD`: a merge with no conflicts commits itself and `MERGE_HEAD` is gone. **The rebuild is not finished until `origin/main` is an ancestor.** `read-tree` + `apply` makes a commit whose *tree* is main + patch, but the merge base with main is still the old one, so GitHub's PR diff counts every main change since then as the PR's (seen: 731 files for a 43-file PR). Follow it with `git merge origin/main`; every conflict is the same change on both sides, so `git diff --name-only --diff-filter=U | xargs git checkout --ours --` is the answer, and `git diff --cached HEAD --stat` must print nothing before you commit (the tree does not change, only the ancestry). A branch cut from a stacked branch with `git checkout -b` inherits its **upstream**, so `git status -sb` reads `feat/pr-4...origin/feat/pr-3` and a plain `git push` would push PR 4's commits onto PR 3's remote; run `git branch --unset-upstream` right after cutting it (seen 2026-10-05 on `feat/platform-tiers-admin-ui`). Then retarget it (`gh pr edit <n> --base main`): a PR still pointed at the merged base branch shows every main change since then (seen: 1,285 files for a 28-file PR). No rebase or force push needed.
diff --git a/bun.lock b/bun.lock
index fd7e76265de..42ecdbfbf8b 100644
--- a/bun.lock
+++ b/bun.lock
@@ -121,7 +121,7 @@
},
"packages/core/execution": {
"name": "@activepieces/core-execution",
- "version": "0.26.2",
+ "version": "0.26.3",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
diff --git a/docs/install/reference/breaking-changes.mdx b/docs/install/reference/breaking-changes.mdx
index 7e8c08e4f88..bd81f75e547 100644
--- a/docs/install/reference/breaking-changes.mdx
+++ b/docs/install/reference/breaking-changes.mdx
@@ -8,6 +8,16 @@ icon: "hammer"
### What has changed?
+#### AI steps and agents stop running a model their key no longer allows
+
+An AI provider key can limit which models it offers. Until now, a flow AI step or a flow-step agent that already named a model kept running it after an admin removed that model from the key's list. From this release the step fails with "The model … isn't allowed for this AI provider key anymore". This is checked when the step is queued and again when it runs.
+
+Two key edits are now refused instead of silently breaking things: limiting the projects of a key that web search, web scraping or image generation uses for every project, and removing a typed-in model (Vertex, OpenAI-compatible, Cloudflare) that a platform tier uses. Platform-wide AI work, such as tool-search embeddings, now prefers a key that serves every project over one limited to a few.
+
+#### What you need to do
+
+If a flow step or agent fails with that message, pick a model the key allows, or add the model back to the key's list in AI Center. To limit a capability key's projects, choose a different key for that capability first.
+
#### Telegram Bot Get Chat and Get Chat Member fail on Telegram errors, and Get File no longer returns the bot token
Get Chat and Get Chat Member used to return Telegram's error response, such as `{ "ok": false, "description": "Bad Request: chat not found" }`, as a **successful** step. From Telegram Bot 0.8.0 the step fails with that error instead, so a wrong chat or user ID stops the flow where it happened.
diff --git a/packages/core/execution/package.json b/packages/core/execution/package.json
index b010c18879c..098e7e6bd95 100644
--- a/packages/core/execution/package.json
+++ b/packages/core/execution/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/core-execution",
- "version": "0.26.2",
+ "version": "0.26.3",
"type": "commonjs",
"main": "./dist/src/index.js",
"scripts": {
diff --git a/packages/core/execution/src/lib/workers/worker-contract.ts b/packages/core/execution/src/lib/workers/worker-contract.ts
index c76a223607c..2d7e536930b 100644
--- a/packages/core/execution/src/lib/workers/worker-contract.ts
+++ b/packages/core/execution/src/lib/workers/worker-contract.ts
@@ -454,6 +454,7 @@ export type ResolveAiProviderRequest = {
platformId: string
provider: AIProviderName
providerConfigId?: string
+ modelId?: string
}
export type ResolveAiProviderResponse = AiProviderCredentials & {
diff --git a/packages/server/api/src/app/ai/ai-execute-controller.ts b/packages/server/api/src/app/ai/ai-execute-controller.ts
index dae0921376c..6caa4afc3fe 100644
--- a/packages/server/api/src/app/ai/ai-execute-controller.ts
+++ b/packages/server/api/src/app/ai/ai-execute-controller.ts
@@ -1,4 +1,4 @@
-import { ActivepiecesError, AIProviderName, ApId, apId, ErrorCode, isNil } from '@activepieces/core-utils'
+import { ActivepiecesError, AIProviderName, ApId, apId, ErrorCode, isNil, spreadIfDefined } from '@activepieces/core-utils'
import { AiStepAction, AiStepFile, AiStepSchema, AiStepWebSearch, ExecuteAiJobData, LATEST_JOB_DATA_SCHEMA_VERSION, maxSocketHttpBufferSizeBytes, PrincipalType, WorkerJobType } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
@@ -11,6 +11,7 @@ import { assertCreditsAndAppSumoNotExceeded } from '../platform/billing-provider
import { aiExecution } from './ai-execution'
import { aiModelCandidates } from './ai-model-candidates'
import { aiModelResolution } from './ai-model-resolution'
+import { aiProviderService } from './ai-provider-service'
export const aiExecuteController: FastifyPluginAsyncZod = async (app) => {
const bodyLimit = maxSocketHttpBufferSizeBytes(system.getNumberOrThrow(AppSystemProp.MAX_FILE_SIZE_MB))
@@ -74,6 +75,15 @@ async function pickModel({ body, platformId, projectId, log }: {
const modelId = body.action === AiStepAction.GENERATE_IMAGE
? body.modelId
: aiModelResolution.resolveTierModelId({ provider: body.provider, modelId: body.modelId, log })
+ if (body.provider !== AIProviderName.ACTIVEPIECES) {
+ await aiProviderService(log).assertModelAllowed({
+ platformId,
+ provider: body.provider,
+ scope: { type: 'project', projectId },
+ modelId,
+ ...spreadIfDefined('configId', body.providerConfigId),
+ })
+ }
return { provider: body.provider, providerConfigId: body.providerConfigId, modelId }
}
diff --git a/packages/server/api/src/app/ai/ai-key-scope.ts b/packages/server/api/src/app/ai/ai-key-scope.ts
index 6a82a832ff9..7dd56942b13 100644
--- a/packages/server/api/src/app/ai/ai-key-scope.ts
+++ b/packages/server/api/src/app/ai/ai-key-scope.ts
@@ -1,4 +1,6 @@
+import { isNil } from '@activepieces/core-utils'
import { AiProviderModelScope } from '@activepieces/shared'
+import { z } from 'zod'
import { AIProviderSchema } from './ai-provider-entity'
export const aiKeyScope = {
@@ -18,8 +20,19 @@ export const aiKeyScope = {
scopeAllows({ modelScope, modelIds, modelId }: { modelScope: AiProviderModelScope, modelIds: string[], modelId: string }): boolean {
return modelScope !== 'selected' || modelIds.includes(modelId)
},
+ manualModelIdsOf({ config }: { config: unknown }): string[] | undefined {
+ const parsed = ManualModelsConfig.safeParse(config)
+ return parsed.success ? parsed.data.models.map((model) => model.modelId) : undefined
+ },
+ keyOffersModel({ key, modelId }: { key: Pick<AIProviderSchema, 'modelScope' | 'modelIds'> & { config: unknown }, modelId: string }): boolean {
+ const manualModelIds = aiKeyScope.manualModelIdsOf({ config: key.config })
+ return aiKeyScope.scopeAllows({ modelScope: key.modelScope, modelIds: key.modelIds, modelId })
+ && (isNil(manualModelIds) || manualModelIds.includes(modelId))
+ },
}
+const ManualModelsConfig = z.object({ models: z.array(z.object({ modelId: z.string() })) })
+
export type ProviderScope =
| { type: 'project', projectId: string }
| { type: 'platform' }
diff --git a/packages/server/api/src/app/ai/ai-provider-service.ts b/packages/server/api/src/app/ai/ai-provider-service.ts
index a5391120bb8..e5728418acc 100644
--- a/packages/server/api/src/app/ai/ai-provider-service.ts
+++ b/packages/server/api/src/app/ai/ai-provider-service.ts
@@ -1,6 +1,6 @@
import { ActivepiecesError, AiProviderKeyStatus, AIProviderName, apId, classifyProviderOutcome, ErrorCode, isNil, PlatformId, ProviderOutcomeSignal, spreadIfDefined, spreadIfNotUndefined, toProviderOutcomeSignal, tryCatch, unique } from '@activepieces/core-utils'
import { modelCatalog, modelTierCatalog } from '@activepieces/server-utils'
-import { ActivePiecesProviderAuthConfig, AI_PROVIDER_ENTITY_TYPES, AIProviderAuthConfig, AIProviderConfig, aiProviderCredentials, AIProviderModel, AIProviderModelType, AiProviderProjectScope, aiProviderUtils, AIProviderWithoutSensitiveData, CreateAIProviderRequest, GetProviderConfigResponse, ProjectAIProvider, UpdateAIProviderRequest } from '@activepieces/shared'
+import { ActivePiecesProviderAuthConfig, AI_PROVIDER_ENTITY_TYPES, AIProviderAuthConfig, AIProviderConfig, aiProviderCredentials, AIProviderModel, AIProviderModelType, AiProviderProjectScope, AiProviderToolConfig, aiProviderUtils, AIProviderWithoutSensitiveData, AiToolCapability, AiToolProvider, CreateAIProviderRequest, GetProviderConfigResponse, ProjectAIProvider, UpdateAIProviderRequest } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import cron from 'node-cron'
import { EntityManager, FindOptionsWhere } from 'typeorm'
@@ -15,6 +15,7 @@ import { platformService } from '../platform/platform.service'
import { aiKeyScope, ProviderScope } from './ai-key-scope'
import { AIProviderEntity, AIProviderSchema } from './ai-provider-entity'
import { aiProviderHealth } from './ai-provider-health'
+import { AiToolConfigEntity } from './ai-tool-config-entity'
import { platformModelTierService } from './platform-model-tier-service'
import { aiProviders } from './providers'
@@ -49,7 +50,7 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({
async listForProject({ platformId, projectId }: { platformId: PlatformId, projectId: string }): Promise<ProjectAIProvider[]> {
const rows = await listVisibleRows({ platformId, log })
const eligible = rows.filter((row) => aiKeyScope.rowAllowsScope({ row, scope: { type: 'project', projectId } }))
- const ranked = rankRows(eligible)
+ const ranked = rankRows({ rows: eligible, scope: { type: 'project', projectId } })
const chatRow = pickChatRow(ranked)
return unique(ranked.map((row) => row.provider)).map((provider) => {
const rows = ranked.filter((row) => row.provider === provider)
@@ -148,8 +149,10 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({
displayName: request.displayName,
}
- const changesModelScope = !isNil(request.modelScope) || !isNil(request.modelIds)
+ const changesModelScope = !isNil(request.modelScope) || !isNil(request.modelIds) || !isNil(aiKeyScope.manualModelIdsOf({ config: request.config }))
await transaction(async (manager) => {
+ await platformModelTierService.lockPlatform({ manager, platformId })
+ await assertCapabilitiesKeepKey({ manager, platformId, providerId, request })
if (changesModelScope) {
await platformModelTierService.assertKeyScopeKeepsTiers({
manager,
@@ -157,6 +160,7 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({
configId: providerId,
modelScope: request.modelScope,
modelIds: request.modelIds,
+ config: request.config,
})
}
if (request.enabledForChat === true) {
@@ -285,6 +289,18 @@ export const aiProviderService = (log: FastifyBaseLogger) => ({
})
}
},
+ async assertModelAllowed({ platformId, provider, scope, configId, modelId }: { platformId: PlatformId, provider: AIProviderName, scope: ProviderScope, configId?: string, modelId: string }): Promise<void> {
+ const aiProvider = await resolveRowForScope({ platformId, provider, scope, configId })
+ this.assertKeyAllowsModel({ key: aiProvider, modelId })
+ },
+ assertKeyAllowsModel({ key, modelId }: { key: Pick<AIProviderSchema, 'modelScope' | 'modelIds'>, modelId: string }): void {
+ if (!aiKeyScope.scopeAllows({ modelScope: key.modelScope, modelIds: key.modelIds, modelId })) {
+ throw new ActivepiecesError({
+ code: ErrorCode.VALIDATION,
+ params: { message: `The model "${modelId}" isn't allowed for this AI provider key anymore. Pick another model for this step.` },
+ })
+ }
+ },
async getConfigOrThrow({ platformId, provider, scope, configId }: { platformId: PlatformId, provider: AIProviderName, scope: ProviderScope, configId?: string }): Promise<GetProviderConfigResponse> {
const aiProvider = await resolveRowForScope({ platformId, provider, scope, configId })
const auth = await decryptRowAuth({ aiProvider, platformId, log })
@@ -306,15 +322,29 @@ async function shouldHideActivepiecesAiProvider({ platformId, log }: { platformI
return plan.embeddingEnabled
}
+
+const CAPABILITY_LABELS: Record<AiToolCapability, string> = {
+ [AiToolCapability.WEB_SEARCH]: 'web search',
+ [AiToolCapability.WEB_SCRAPING]: 'web scraping',
+ [AiToolCapability.IMAGE_GENERATION]: 'image generation',
+}
+
+const PLATFORM_SCOPE_PREFERENCE: Record<AiProviderProjectScope, number> = {
+ all: 0,
+ except: 1,
+ selected: 2,
+}
+
const PROJECT_SCOPE_SPECIFICITY: Record<AiProviderProjectScope, number> = {
selected: 0,
except: 1,
all: 2,
}
-function rankRows(rows: AIProviderSchema[]): AIProviderSchema[] {
+function rankRows({ rows, scope }: { rows: AIProviderSchema[], scope: ProviderScope }): AIProviderSchema[] {
+ const order = scope.type === 'platform' ? PLATFORM_SCOPE_PREFERENCE : PROJECT_SCOPE_SPECIFICITY
return [...rows].sort((a, b) => {
- const specificityDelta = PROJECT_SCOPE_SPECIFICITY[a.projectScope] - PROJECT_SCOPE_SPECIFICITY[b.projectScope]
+ const specificityDelta = order[a.projectScope] - order[b.projectScope]
if (specificityDelta !== 0) {
return specificityDelta
}
@@ -391,10 +421,40 @@ async function findRunKeyCandidates({ platformId, provider, configId, scope, log
), [])
}
+async function assertCapabilitiesKeepKey({ manager, platformId, providerId, request }: { manager: EntityManager, platformId: PlatformId, providerId: string, request: UpdateAIProviderRequest }): Promise<void> {
+ const changesScope = !isNil(request.projectScope) || !isNil(request.modelScope) || !isNil(request.modelIds) || !isNil(request.config)
+ if (!changesScope) {
+ return
+ }
+ const key = await manager.getRepository(AIProviderEntity).findOneByOrFail({ platformId, id: providerId })
+ const next = {
+ projectScope: request.projectScope ?? key.projectScope,
+ modelScope: request.modelScope ?? key.modelScope,
+ modelIds: request.modelIds ?? key.modelIds,
+ config: request.config,
+ }
+ const toolConfigs = await manager.getRepository(AiToolConfigEntity).findBy({ platformId, provider: AiToolProvider.AI_PROVIDER })
+ const broken = toolConfigs.flatMap((toolConfig) => {
+ const choice = AiProviderToolConfig.safeParse(toolConfig.config)
+ if (!choice.success || choice.data.aiProviderId !== providerId) {
+ return []
+ }
+ const losesProjects = next.projectScope !== 'all'
+ const losesModel = !isNil(choice.data.modelId) && !aiKeyScope.keyOffersModel({ key: next, modelId: choice.data.modelId })
+ return losesProjects || losesModel ? [CAPABILITY_LABELS[toolConfig.capability]] : []
+ })
+ if (broken.length > 0) {
+ throw new ActivepiecesError({
+ code: ErrorCode.VALIDATION,
+ params: { message: `This key powers ${broken.join(', ')} for every project. Pick another key there before limiting its projects or removing that model.` },
+ })
+ }
+}
+
async function findEligibleRow({ platformId, provider, scope }: { platformId: PlatformId, provider: AIProviderName, scope: ProviderScope }): Promise<AIProviderSchema | null> {
const rows = await aiProviderRepo().findBy({ platformId, provider })
const eligible = rows.filter((row) => aiKeyScope.rowAllowsScope({ row, scope }))
- return rankRows(eligible)[0] ?? null
+ return rankRows({ rows: eligible, scope })[0] ?? null
}
async function resolveEligibleRow({ platformId, provider, scope }: { platformId: PlatformId, provider: AIProviderName, scope: ProviderScope }): Promise<AIProviderSchema> {
diff --git a/packages/server/api/src/app/ai/ai-rpc-handlers.ts b/packages/server/api/src/app/ai/ai-rpc-handlers.ts
index 2ee2045cadb..50e8ab76da9 100644
--- a/packages/server/api/src/app/ai/ai-rpc-handlers.ts
+++ b/packages/server/api/src/app/ai/ai-rpc-handlers.ts
@@ -22,6 +22,9 @@ export const aiRpcHandlers = (log: FastifyBaseLogger) => ({
scope: { type: 'project', projectId: input.projectId },
...spreadIfDefined('configId', input.providerConfigId),
})
+ if (!isNil(input.modelId)) {
+ aiProviderService(log).assertKeyAllowsModel({ key: config, modelId: input.modelId })
+ }
return {
...config,
providerConfigId: config.configId,
diff --git a/packages/server/api/src/app/ai/ai-tool-config-service.ts b/packages/server/api/src/app/ai/ai-tool-config-service.ts
index 73a18d688bd..abfcc859910 100644
--- a/packages/server/api/src/app/ai/ai-tool-config-service.ts
+++ b/packages/server/api/src/app/ai/ai-tool-config-service.ts
@@ -2,10 +2,15 @@ import { ActivepiecesError, AIProviderName, apId, ErrorCode, isNil, PlatformId,
import { aiUtils } from '@activepieces/server-utils'
import { AIProviderModelType, AiProviderToolChoices, AiProviderToolConfig, AiToolAuthConfig, AiToolCapability, AiToolConfigWithoutSensitiveData, AiToolProvider, AiToolProviderConfig, CreateAiToolConfigRequest, GetEnabledAiToolsResponse, ResolvedAiTool, UpdateAiToolConfigRequest } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
+import { EntityManager } from 'typeorm'
import { repoFactory } from '../core/db/repo-factory'
+import { transaction } from '../core/db/transaction'
import { encryptUtils } from '../helper/encryption'
+import { aiKeyScope } from './ai-key-scope'
+import { AIProviderEntity } from './ai-provider-entity'
import { aiProviderService } from './ai-provider-service'
import { AiToolConfigEntity, AiToolConfigSchema } from './ai-tool-config-entity'
+import { platformModelTierService } from './platform-model-tier-service'
const aiToolConfigRepo = repoFactory<AiToolConfigSchema>(AiToolConfigEntity)
@@ -24,14 +29,18 @@ export const aiToolConfigService = (log: FastifyBaseLogger) => ({
throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'An API key is required for this service' } })
}
const encryptedAuth = await encryptUtils.encryptObject(request.auth ?? {})
- await aiToolConfigRepo().save({
- id: existing?.id ?? apId(),
+ await saveUnderPlatformLock({
platformId,
- capability: request.capability,
- provider: request.provider,
- auth: encryptedAuth,
- config: request.config ?? null,
- enabled: request.enabled ?? true,
+ choice: request.provider === AiToolProvider.AI_PROVIDER ? request.config : null,
+ write: (manager) => aiToolConfigRepo(manager).save({
+ id: existing?.id ?? apId(),
+ platformId,
+ capability: request.capability,
+ provider: request.provider,
+ auth: encryptedAuth,
+ config: request.config ?? null,
+ enabled: request.enabled ?? true,
+ }),
})
},
@@ -52,11 +61,15 @@ export const aiToolConfigService = (log: FastifyBaseLogger) => ({
throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'An API key is required for this service' } })
}
const encryptedAuth = !isNil(request.auth) ? await encryptUtils.encryptObject(request.auth) : undefined
- await aiToolConfigRepo().update(id, {
- ...spreadIfDefined('provider', request.provider),
- ...spreadIfDefined('auth', encryptedAuth),
- ...spreadIfDefined('config', request.config),
- ...spreadIfDefined('enabled', request.enabled),
+ await saveUnderPlatformLock({
+ platformId,
+ choice: provider === AiToolProvider.AI_PROVIDER ? request.config ?? config.config : null,
+ write: (manager) => aiToolConfigRepo(manager).update(id, {
+ ...spreadIfDefined('provider', request.provider),
+ ...spreadIfDefined('auth', encryptedAuth),
+ ...spreadIfDefined('config', request.config),
+ ...spreadIfDefined('enabled', request.enabled),
+ }),
})
},
@@ -97,12 +110,27 @@ export const aiToolConfigService = (log: FastifyBaseLogger) => ({
},
})
+async function saveUnderPlatformLock({ platformId, choice, write }: { platformId: PlatformId, choice: AiToolProviderConfig | null | undefined, write: (manager: EntityManager) => Promise<unknown> }): Promise<void> {
+ await transaction(async (manager) => {
+ await platformModelTierService.lockPlatform({ manager, platformId })
+ const parsed = AiProviderToolConfig.safeParse(choice)
+ if (parsed.success) {
+ const key = await manager.getRepository(AIProviderEntity).findOneBy({ platformId, id: parsed.data.aiProviderId })
+ const modelId = parsed.data.modelId
+ const dropsModel = !isNil(key) && !isNil(modelId) && !aiKeyScope.keyOffersModel({ key, modelId })
+ if (isNil(key) || key.projectScope !== 'all' || dropsModel) {
+ throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'This AI provider cannot be used for this capability' } })
+ }
+ }
+ await write(manager)
+ })
+}
+
async function assertProviderChoice({ platformId, capability, config, log }: { platformId: PlatformId, capability: AiToolCapability, config: AiToolProviderConfig | null | undefined, log: FastifyBaseLogger }): Promise<void> {
const choice = AiProviderToolConfig.safeParse(config)
const configs = choice.success ? await aiProviderService(log).listConfigs(platformId) : []
const aiProvider = choice.success ? configs.find((row) => row.id === choice.data.aiProviderId) : undefined
- const servesAllProjects = !isNil(aiProvider) && aiProvider.projectScope === 'all'
- if (choice.success && servesAllProjects && await providerCovers({ platformId, capability, aiProvider, modelId: choice.data.modelId, log })) {
+ if (choice.success && !isNil(aiProvider) && await providerCovers({ platformId, capability, aiProvider, modelId: choice.data.modelId, log })) {
return
}
throw new ActivepiecesError({ code: ErrorCode.VALIDATION, params: { message: 'This AI provider cannot be used for this capability' } })
diff --git a/packages/server/api/src/app/ai/platform-model-tier-service.ts b/packages/server/api/src/app/ai/platform-model-tier-service.ts
index c1fd4b81438..c30e719745c 100644
--- a/packages/server/api/src/app/ai/platform-model-tier-service.ts
+++ b/packages/server/api/src/app/ai/platform-model-tier-service.ts
@@ -188,7 +188,11 @@ export const platformModelTierService = {
}))
},
- async assertKeyScopeKeepsTiers({ manager, platformId, configId, modelScope, modelIds }: AssertKeyScopeParams): Promise<void> {
+ async lockPlatform({ manager, platformId }: { manager: EntityManager, platformId: PlatformId }): Promise<void> {
+ await lockPlatform({ manager, platformId })
+ },
+
+ async assertKeyScopeKeepsTiers({ manager, platformId, configId, modelScope, modelIds, config }: AssertKeyScopeParams): Promise<void> {
await lockPlatform({ manager, platformId })
const key = await aiProviderRepo(manager).findOneBy({ platformId, id: configId })
if (isNil(key)) {
@@ -196,7 +200,8 @@ export const platformModelTierService = {
}
const nextScope = { modelScope: modelScope ?? key.modelScope, modelIds: modelIds ?? key.modelIds }
const tiers = await findLiveTiersUsingKey({ manager, platformId, configId })
- const broken = tiers.filter((tier) => tier.entries.some((entry) => entry.configId === configId && !aiKeyScope.scopeAllows({ ...nextScope, modelId: entry.modelId })))
+ const keyKeeps = (modelId: string): boolean => aiKeyScope.keyOffersModel({ key: { ...nextScope, config }, modelId })
+ const broken = tiers.filter((tier) => tier.entries.some((entry) => entry.configId === configId && !keyKeeps(entry.modelId)))
if (broken.length > 0) {
throw keyInUseError({ tierNames: broken.map((tier) => tier.name) })
}
@@ -373,4 +378,5 @@ type AssertKeyScopeParams = {
configId: string
modelScope: AiProviderModelScope | undefined
modelIds: string[] | undefined
+ config: unknown
}
diff --git a/packages/server/api/src/app/ee/agent/agent-model-resolution.ts b/packages/server/api/src/app/ee/agent/agent-model-resolution.ts
index c6166d1b874..e5fb3ac9ec6 100644
--- a/packages/server/api/src/app/ee/agent/agent-model-resolution.ts
+++ b/packages/server/api/src/app/ee/agent/agent-model-resolution.ts
@@ -73,6 +73,12 @@ function publishedTierModelId({ tierId, surface, log }: { tierId: string, surfac
function resolveNamedModelId({ provider, modelName, surface, modelScope, modelIds, log }: { provider: AIProviderName, modelName: string, surface: ModelTierSurface, modelScope?: AiProviderModelScope, modelIds?: string[], log: FastifyBaseLogger }): string {
if (provider !== AIProviderName.ACTIVEPIECES) {
+ if (modelScope === 'selected' && !isNil(modelIds) && !modelIds.includes(modelName)) {
+ throw new ActivepiecesError({
+ code: ErrorCode.ENTITY_NOT_FOUND,
+ params: { entityId: provider, entityType: AI_PROVIDER_ENTITY_TYPES.provider },
+ }, `the model "${modelName}" is not allowed for this AI provider key anymore`)
+ }
return modelName
}
const requested = isTierId({ modelName }) ? publishedTierModelId({ tierId: modelName, surface, log }) : modelName
diff --git a/packages/server/api/test/integration/ce/ai-provider/ai-execute.test.ts b/packages/server/api/test/integration/ce/ai-provider/ai-execute.test.ts
index 109584f5cb2..3c10de4f240 100644
--- a/packages/server/api/test/integration/ce/ai-provider/ai-execute.test.ts
+++ b/packages/server/api/test/integration/ce/ai-provider/ai-execute.test.ts
@@ -5,6 +5,7 @@ import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
import { vi } from 'vitest'
import { generateMockToken } from '../../../helpers/auth'
+import { mockAndSaveAIProvider } from '../../../helpers/mocks'
import { createTestContext, TestContext } from '../../../helpers/test-context'
import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'
import { publishedTierReaders } from '../../../unit/app/ee/agent/model-tier-fixture'
@@ -36,7 +37,7 @@ afterEach(() => {
vi.restoreAllMocks()
})
-async function executeAiStep({ action, modelId }: { action: AiStepAction, modelId: string }) {
+async function executeAiStep({ action, modelId, provider = AIProviderName.ACTIVEPIECES, providerConfigId }: { action: AiStepAction, modelId: string, provider?: AIProviderName, providerConfigId?: string }) {
const engineToken = await generateMockToken({
type: PrincipalType.ENGINE,
id: apId(),
@@ -52,7 +53,8 @@ async function executeAiStep({ action, modelId }: { action: AiStepAction, modelI
flowId: apId(),
flowRunId: apId(),
waitpointId: apId(),
- provider: AIProviderName.ACTIVEPIECES,
+ provider,
+ ...(providerConfigId ? { providerConfigId } : {}),
modelId,
prompt: 'hello',
},
@@ -82,4 +84,15 @@ describe('POST /v1/ai/execute', () => {
modelId: 'smart',
})
})
+
+ it('refuses a specific model the key no longer allows, before anything is queued', async () => {
+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, modelScope: 'selected', modelIds: ['gpt-4o'] })
+
+ const refused = await executeAiStep({ action: AiStepAction.ASK_AI, provider: AIProviderName.OPENAI, providerConfigId: key.id, modelId: 'gpt-4o-mini' })
+ const allowed = await executeAiStep({ action: AiStepAction.ASK_AI, provider: AIProviderName.OPENAI, providerConfigId: key.id, modelId: 'gpt-4o' })
+
+ expect(refused.statusCode).toBe(StatusCodes.CONFLICT)
+ expect(allowed.statusCode).toBe(StatusCodes.OK)
+ expect(enqueue).toHaveBeenCalledTimes(1)
+ })
})
diff --git a/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts b/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts
index 727230e66ce..4a1b27e1bc4 100644
--- a/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts
+++ b/packages/server/api/test/integration/ce/ai-provider/ai-provider.test.ts
@@ -3,6 +3,7 @@ import { AIProviderModelType, DefaultProjectRole, PrincipalType, ProviderModelCo
import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
import { aiProviderService } from '../../../../src/app/ai/ai-provider-service'
+import { aiRpcHandlers } from '../../../../src/app/ai/ai-rpc-handlers'
import { generateMockToken } from '../../../helpers/auth'
import { db } from '../../../helpers/db'
import { createMockProject, mockAndSaveAIProvider } from '../../../helpers/mocks'
@@ -964,3 +965,112 @@ describe('AI Providers API', () => {
})
})
})
+
+describe('key scope hardening', () => {
+ it('prefers a key open to every project for platform-wide work, and the most specific key inside a project', async () => {
+ const open = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI })
+ const limited = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, projectScope: 'selected', projectIds: [ctx.project.id] })
+
+ const platformPick = await aiProviderService(app!.log).getConfigOrThrow({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, scope: { type: 'platform' } })
+ const projectPick = await aiProviderService(app!.log).getConfigOrThrow({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, scope: { type: 'project', projectId: ctx.project.id } })
+
+ expect(platformPick.configId).toBe(open.id)
+ expect(projectPick.configId).toBe(limited.id)
+ })
+
+ it('refuses at run time a model the key stopped allowing after the step was queued', async () => {
+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI, modelScope: 'selected', modelIds: ['gpt-4o'] })
+ const resolve = (modelId?: string) => aiRpcHandlers(app!.log).resolveAiProvider({ projectId: ctx.project.id, platformId: ctx.platform.id, provider: AIProviderName.OPENAI, providerConfigId: key.id, ...(modelId ? { modelId } : {}) })
+
+ await expect(resolve('gpt-4o-mini')).rejects.toMatchObject({ error: { code: 'VALIDATION' } })
+ expect((await resolve('gpt-4o')).providerConfigId).toBe(key.id)
+ expect((await resolve()).providerConfigId).toBe(key.id)
+ })
+
+ it('refuses to limit the projects of a key a capability uses for every project', async () => {
+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI })
+ await db.save('ai_tool_config', {
+ id: apId(),
+ created: new Date().toISOString(),
+ updated: new Date().toISOString(),
+ platformId: ctx.platform.id,
+ capability: 'WEB_SEARCH',
+ provider: 'ai_provider',
+ config: { aiProviderId: key.id },
+ auth: { iv: 'unused', data: 'unused' },
+ enabled: true,
+ })
+
+ const narrowed = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: key.displayName, projectScope: 'selected', projectIds: [ctx.project.id] })
+ const renamed = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: 'Renamed key' })
+
+ expect(narrowed.statusCode).toBe(StatusCodes.CONFLICT)
+ expect(narrowed.body).toContain('web search')
+ expect(renamed.statusCode).toBe(StatusCodes.OK)
+ })
+
+ it('refuses to drop the model a capability uses from its key', async () => {
+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.OPENAI })
+ await db.save('ai_tool_config', {
+ id: apId(),
+ created: new Date().toISOString(),
+ updated: new Date().toISOString(),
+ platformId: ctx.platform.id,
+ capability: 'IMAGE_GENERATION',
+ provider: 'ai_provider',
+ config: { aiProviderId: key.id, modelId: 'gpt-image-1' },
+ auth: { iv: 'unused', data: 'unused' },
+ enabled: true,
+ })
+
+ const dropping = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: key.displayName, modelScope: 'selected', modelIds: ['gpt-4o'] })
+ const keeping = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: key.displayName, modelScope: 'selected', modelIds: ['gpt-4o', 'gpt-image-1'] })
+
+ expect(dropping.statusCode).toBe(StatusCodes.CONFLICT)
+ expect(dropping.body).toContain('image generation')
+ expect(keeping.statusCode).toBe(StatusCodes.OK)
+ })
+
+ it('refuses to drop a typed-in model that a capability uses, even when the key allows all models', async () => {
+ const models = [
+ { modelId: 'chat-model', modelName: 'Chat', modelType: AIProviderModelType.TEXT },
+ { modelId: 'image-model', modelName: 'Image', modelType: AIProviderModelType.IMAGE },
+ ]
+ const config = { baseUrl: 'https://api.example.com/v1', apiKeyHeader: 'Authorization', models }
+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.CUSTOM, config })
+ await db.save('ai_tool_config', {
+ id: apId(),
+ created: new Date().toISOString(),
+ updated: new Date().toISOString(),
+ platformId: ctx.platform.id,
+ capability: 'IMAGE_GENERATION',
+ provider: 'ai_provider',
+ config: { aiProviderId: key.id, modelId: 'image-model' },
+ auth: { iv: 'unused', data: 'unused' },
+ enabled: true,
+ })
+
+ const dropping = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: key.displayName, config: { ...config, models: [models[0]] } })
+ const keeping = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: key.displayName, config: { ...config, models: [...models, { modelId: 'new-model', modelName: 'New', modelType: AIProviderModelType.TEXT }] } })
+
+ expect(dropping.statusCode).toBe(StatusCodes.CONFLICT)
+ expect(dropping.body).toContain('image generation')
+ expect(keeping.statusCode).toBe(StatusCodes.OK)
+ })
+
+ it('refuses to drop a typed-in model that a tier uses, even when the key allows all models', async () => {
+ const models = [
+ { modelId: 'kept-model', modelName: 'Kept', modelType: AIProviderModelType.TEXT },
+ { modelId: 'tier-model', modelName: 'Tier', modelType: AIProviderModelType.TEXT },
+ ]
+ const config = { baseUrl: 'https://api.example.com/v1', apiKeyHeader: 'Authorization', models }
+ const key = await mockAndSaveAIProvider({ platformId: ctx.platform.id, provider: AIProviderName.CUSTOM, config })
+ const tier = await ctx.post('/v1/platform-model-tiers', { name: 'Typed', emoji: '⚡', description: null, entries: [{ configId: key.id, modelId: 'tier-model' }] })
+ expect(tier.statusCode).toBe(StatusCodes.OK)
+
+ const dropped = await ctx.post(`/v1/ai-providers/${key.id}`, { displayName: key.displayName, config: { ...config, models: [models[0]] } })
+
+ expect(dropped.statusCode).toBe(StatusCodes.CONFLICT)
+ expect(dropped.body).toContain('Typed')
+ })
+})
diff --git a/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts
index 3fe43f92242..15a9f404042 100644
--- a/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts
+++ b/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts
@@ -264,6 +264,17 @@ describe('resolveNamedModelId', () => {
return error instanceof ActivepiecesError ? error.error : undefined
}
+ it('refuses an own-key model the key no longer allows, even when its name looks like a tier id', () => {
+ const refusal = (modelName: string) => {
+ const { error } = tryCatchSync(() => named({ provider: AIProviderName.OPENAI, modelName, modelScope: 'selected', modelIds: ['gpt-4o'] }))
+ return error instanceof ActivepiecesError ? error.error.code : undefined
+ }
+ expect(named({ provider: AIProviderName.OPENAI, modelName: 'gpt-4o', modelScope: 'selected', modelIds: ['gpt-4o'] })).toBe('gpt-4o')
+ expect(refusal('gpt-4o-mini')).toBe(ErrorCode.ENTITY_NOT_FOUND)
+ expect(refusal('smart')).toBe(ErrorCode.ENTITY_NOT_FOUND)
+ expect(named({ provider: AIProviderName.OPENAI, modelName: 'gpt-4o-mini' })).toBe('gpt-4o-mini')
+ })
+
it('lets a managed run name any model on the managed allow-list', () => {
for (const modelId of aiProviderUtils.managedChatModelIds()) {
expect(named({ provider: AIProviderName.ACTIVEPIECES, modelName: modelId }), modelId).toBe(modelId)
diff --git a/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts b/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts
index bc3dca3df66..f87e9d87c9e 100644
--- a/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts
@@ -100,6 +100,7 @@ async function resolveSpecificModel({ ctx, data }: { ctx: JobContext, data: Exec
projectId: data.projectId,
platformId: data.platformId,
provider: data.provider,
+ modelId: data.modelId,
...spreadIfDefined('providerConfigId', data.providerConfigId),
})
}