Skip to content

The open alternative to Salesforce, designed for AI.

feat(usage-limit): gate workflow node runs and logic function executions (#26283)

Etiennebf102e43cd07
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
index 1c438bfcb49..d41fa88c92c 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
@@ -2009,6 +2009,7 @@ enum FeatureFlagKey {
IS_INITIAL_OBJECT_VIEW_ENABLED
IS_WEBHOOK_RATE_LIMIT_ENABLED
IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED
+ IS_EXECUTION_QUOTA_ENABLED
}
type WorkspaceUrls {
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.ts b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
index feca18b3563..f664028e39f 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
@@ -1615,7 +1615,7 @@ export interface FeatureFlag {
__typename: 'FeatureFlag'
}
-export type FeatureFlagKey = 'IS_ASYNC_CSV_EXPORT_ENABLED' | 'IS_UNIQUE_INDEXES_ENABLED' | 'IS_CONFIGURABLE_SEARCH_FIELDS_ENABLED' | 'IS_JSON_FILTER_ENABLED' | 'IS_MESSAGE_CAMPAIGN_ENABLED' | 'IS_JUNCTION_RELATIONS_ENABLED' | 'IS_REST_METADATA_API_NEW_FORMAT_DIRECT' | 'IS_LOGIC_FUNCTION_PREBUILT_MODE_ENABLED' | 'IS_WORKFLOW_CORE_INDEX_PAGE_ENABLED' | 'IS_MESSAGE_CALENDAR_TARGET_READ_ENABLED' | 'IS_RECORD_SHARING_ENABLED' | 'IS_INITIAL_OBJECT_VIEW_ENABLED' | 'IS_WEBHOOK_RATE_LIMIT_ENABLED' | 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED'
+export type FeatureFlagKey = 'IS_ASYNC_CSV_EXPORT_ENABLED' | 'IS_UNIQUE_INDEXES_ENABLED' | 'IS_CONFIGURABLE_SEARCH_FIELDS_ENABLED' | 'IS_JSON_FILTER_ENABLED' | 'IS_MESSAGE_CAMPAIGN_ENABLED' | 'IS_JUNCTION_RELATIONS_ENABLED' | 'IS_REST_METADATA_API_NEW_FORMAT_DIRECT' | 'IS_LOGIC_FUNCTION_PREBUILT_MODE_ENABLED' | 'IS_WORKFLOW_CORE_INDEX_PAGE_ENABLED' | 'IS_MESSAGE_CALENDAR_TARGET_READ_ENABLED' | 'IS_RECORD_SHARING_ENABLED' | 'IS_INITIAL_OBJECT_VIEW_ENABLED' | 'IS_WEBHOOK_RATE_LIMIT_ENABLED' | 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED' | 'IS_EXECUTION_QUOTA_ENABLED'
export interface WorkspaceUrls {
customUrl?: Scalars['String']
@@ -10936,7 +10936,8 @@ export const enumFeatureFlagKey = {
IS_RECORD_SHARING_ENABLED: 'IS_RECORD_SHARING_ENABLED' as const,
IS_INITIAL_OBJECT_VIEW_ENABLED: 'IS_INITIAL_OBJECT_VIEW_ENABLED' as const,
IS_WEBHOOK_RATE_LIMIT_ENABLED: 'IS_WEBHOOK_RATE_LIMIT_ENABLED' as const,
- IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED: 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED' as const
+ IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED: 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED' as const,
+ IS_EXECUTION_QUOTA_ENABLED: 'IS_EXECUTION_QUOTA_ENABLED' as const
}
export const enumIdentityProviderType = {
diff --git a/packages/twenty-docs/developers/extend/apps/logic/credits.mdx b/packages/twenty-docs/developers/extend/apps/logic/credits.mdx
index 5dab47b705d..fdd24039bbf 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/credits.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/credits.mdx
@@ -126,7 +126,7 @@ Notes:
## Checking before you spend
-A workspace can run out of credits. Your app is not stopped when that happens, but some of the platform's own work is. `runAgent` returns an error instead of running, and the other AI entry points refuse outright. So an app that does its expensive work first and calls the platform at the end can fail late, on a call it did not write.
+A workspace can run out of credits. When that happens your function is refused before your code starts, and the platform's own work stops too: `runAgent` returns an error instead of running, and the other AI entry points refuse outright. Uninstall and disconnect hooks are the exception and always run, so an app can still be removed cleanly. An execution that does get through can still fail late, on a platform call it did not write, if the workspace runs out mid-run.
Ask first:
@@ -147,7 +147,7 @@ Notes:
- It answers whether the workspace may spend, not how much it has left. Your app does not see the balance or the plan.
- It fails open. If the check times out or the server is unreachable it reports credits available, so an infrastructure hiccup does not stop your app. Treat it as a hint worth honouring, not as an authorization decision.
- It is a snapshot. Credits can run out between the check and the work.
-- The gate is not applied uniformly. Workflow runs are not credit-gated, and neither is every outbound email path, so a `false` here tells you the workspace is out of credits rather than predicting exactly which calls will fail.
+- The gate is not applied uniformly. Not every outbound email path is credit-gated, so a `false` here tells you the workspace is out of credits rather than predicting exactly which calls will fail.
## Telling people what it costs
diff --git a/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx b/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx
index a754a9cbbec..87faefb1937 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx
@@ -178,7 +178,7 @@ The status code must be a valid HTTP status code (between 100 and 599). Response
#### Platform error responses
-Beyond your handler's own responses, the platform answers route calls directly in some situations: `404` when the route or function does not exist, `403` when the application is stopped, `429` when the execution rate limit is hit, and `422` when the application's production `dependencies` are too large to install — see [dependencies size limits](/developers/extend/apps/getting-started/project-structure#dependencies).
+Beyond your handler's own responses, the platform answers route calls directly in some situations: `404` when the route or function does not exist, `403` when the application is stopped, `402` when the workspace has exhausted its credit allowance or has no active subscription, `409` when a storage limit is reached, `429` when an execution rate limit or a usage limit is hit, and `422` when the application's production `dependencies` are too large to install — see [dependencies size limits](/developers/extend/apps/getting-started/project-structure#dependencies).
#### Server route trigger
@@ -339,6 +339,8 @@ The resolver example above already shows the GitHub HMAC-SHA256 flow — adapt t
<Note>
When the resolver returns a dispatch object, the route responds `202 { queued: true }` and the target runs on the worker queue — the caller never observes the target's latency, result, or failures (those are recorded in execution logs). This keeps sender redeliveries from amplifying processing slowdowns, which is what you want for webhook ingestion.
+The resolver runs in the owner workspace and spends its credits, so the route answers `402` when that workspace has exhausted its allowance or has no active subscription, and `429` when a usage limit is hit. Senders that retry on `5xx` will not retry these, which is what you want: the refusal will not clear on the next attempt.
+
When the caller must read the response body on the same request (challenge handshakes, interactive acknowledgements), return a `Response` from the **resolver** instead. The platform echoes it synchronously and skips the queue; its headers go through the same allow-list as HTTP route responses. Keep the resolver fast — some providers (e.g. Slack) time out in a few seconds. Because the resolver is reachable as a public endpoint, protect it with rate limiting at your edge.
</Note>
diff --git a/packages/twenty-front/src/generated-admin/graphql.ts b/packages/twenty-front/src/generated-admin/graphql.ts
index 43728b134d3..122b6c65fc2 100644
--- a/packages/twenty-front/src/generated-admin/graphql.ts
+++ b/packages/twenty-front/src/generated-admin/graphql.ts
@@ -427,6 +427,7 @@ export enum FeatureFlagKey {
IS_ASYNC_CSV_EXPORT_ENABLED = 'IS_ASYNC_CSV_EXPORT_ENABLED',
IS_CONFIGURABLE_SEARCH_FIELDS_ENABLED = 'IS_CONFIGURABLE_SEARCH_FIELDS_ENABLED',
IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED = 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED',
+ IS_EXECUTION_QUOTA_ENABLED = 'IS_EXECUTION_QUOTA_ENABLED',
IS_INITIAL_OBJECT_VIEW_ENABLED = 'IS_INITIAL_OBJECT_VIEW_ENABLED',
IS_JSON_FILTER_ENABLED = 'IS_JSON_FILTER_ENABLED',
IS_JUNCTION_RELATIONS_ENABLED = 'IS_JUNCTION_RELATIONS_ENABLED',
diff --git a/packages/twenty-front/src/generated-metadata/graphql.ts b/packages/twenty-front/src/generated-metadata/graphql.ts
index dd8522fe06b..48127d6bc31 100644
--- a/packages/twenty-front/src/generated-metadata/graphql.ts
+++ b/packages/twenty-front/src/generated-metadata/graphql.ts
@@ -2107,6 +2107,7 @@ export enum FeatureFlagKey {
IS_ASYNC_CSV_EXPORT_ENABLED = 'IS_ASYNC_CSV_EXPORT_ENABLED',
IS_CONFIGURABLE_SEARCH_FIELDS_ENABLED = 'IS_CONFIGURABLE_SEARCH_FIELDS_ENABLED',
IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED = 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED',
+ IS_EXECUTION_QUOTA_ENABLED = 'IS_EXECUTION_QUOTA_ENABLED',
IS_INITIAL_OBJECT_VIEW_ENABLED = 'IS_INITIAL_OBJECT_VIEW_ENABLED',
IS_JSON_FILTER_ENABLED = 'IS_JSON_FILTER_ENABLED',
IS_JUNCTION_RELATIONS_ENABLED = 'IS_JUNCTION_RELATIONS_ENABLED',
diff --git a/packages/twenty-front/src/modules/settings/admin-panel/constants/SettingsAdminFeatureFlagMetadata.ts b/packages/twenty-front/src/modules/settings/admin-panel/constants/SettingsAdminFeatureFlagMetadata.ts
index 0cab8238a17..449b1ca3ef1 100644
--- a/packages/twenty-front/src/modules/settings/admin-panel/constants/SettingsAdminFeatureFlagMetadata.ts
+++ b/packages/twenty-front/src/modules/settings/admin-panel/constants/SettingsAdminFeatureFlagMetadata.ts
@@ -58,4 +58,8 @@ export const SETTINGS_ADMIN_FEATURE_FLAG_METADATA: Partial<
label: msg`Deferred workspace migration actions`,
description: msg`Run the slow parts of data model changes in the background after they are saved.`,
},
+ [FeatureFlagKey.IS_EXECUTION_QUOTA_ENABLED]: {
+ label: msg`Execution quotas`,
+ description: msg`Enforce usage quotas on workflow node runs and logic function executions.`,
+ },
};
diff --git a/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitSpenderGroups.ts b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitSpenderGroups.ts
index b005f79f839..17ed0dff0ff 100644
--- a/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitSpenderGroups.ts
+++ b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitSpenderGroups.ts
@@ -2,6 +2,7 @@ import { type MessageDescriptor } from '@lingui/core';
import { msg } from '@lingui/core/macro';
import {
IconApps,
+ IconBrandTypescript,
type IconComponent,
IconKey,
IconSettings,
@@ -36,6 +37,12 @@ export const USAGE_LIMIT_SPENDER_GROUPS: UsageLimitSpenderGroup[] = [
Icon: IconApps,
spenderType: 'application',
},
+ {
+ id: 'logicFunction',
+ label: msg`Logic function`,
+ Icon: IconBrandTypescript,
+ spenderType: 'logicFunction',
+ },
{
id: 'apiKey',
label: msg`API key`,
diff --git a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/getUsageLimitSpenderGroups.test.ts b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/getUsageLimitSpenderGroups.test.ts
index 2634e409176..2294bcee67b 100644
--- a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/getUsageLimitSpenderGroups.test.ts
+++ b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/getUsageLimitSpenderGroups.test.ts
@@ -23,6 +23,15 @@ describe('getUsageLimitSpenderGroups', () => {
expect(groups.map((group) => group.id)).toEqual(['workspace', 'apiKey']);
});
+ it('offers the logic function group when the resource accepts it', () => {
+ const groups = getUsageLimitSpenderGroups(['workspace', 'logicFunction']);
+
+ expect(groups.map((group) => group.id)).toEqual([
+ 'workspace',
+ 'logicFunction',
+ ]);
+ });
+
it('returns nothing when no spender is allowed', () => {
expect(getUsageLimitSpenderGroups([])).toEqual([]);
});
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-uninstall.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-uninstall.service.ts
index 1c7744f4b58..8628966e4e7 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-uninstall.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-uninstall.service.ts
@@ -210,6 +210,7 @@ export class ApplicationUninstallService {
logicFunctionId: application.uninstallLogicFunctionId,
workspaceId,
payload,
+ shouldEnforceUsageLimits: false,
...(isDefined(workspaceDeletionRequestTimestamp)
? { workspaceDeletionRequestTimestamp }
: {}),
diff --git a/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-lifecycle-hook.service.ts b/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-lifecycle-hook.service.ts
index 473118f9d22..635f102d025 100644
--- a/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-lifecycle-hook.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-lifecycle-hook.service.ts
@@ -118,6 +118,7 @@ export class ConnectionProviderLifecycleHookService {
logicFunctionId,
workspaceId,
payload: this.buildPayload({ provider, connectedAccountId }),
+ shouldEnforceUsageLimits: false,
});
if (isDefined(executionResult.error)) {
diff --git a/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/is-usage-refused-error.util.spec.ts b/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/is-usage-refused-error.util.spec.ts
new file mode 100644
index 00000000000..11f8ceeeeba
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/billing/utils/__tests__/is-usage-refused-error.util.spec.ts
@@ -0,0 +1,72 @@
+/* @license Enterprise */
+
+import {
+ BillingException,
+ BillingExceptionCode,
+} from 'src/engine/core-modules/billing/billing.exception';
+import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
+import {
+ UsageLimitException,
+ UsageLimitExceptionCode,
+} from 'src/engine/core-modules/usage-limit/exceptions/usage-limit.exception';
+
+describe('isUsageRefusedError', () => {
+ it('recognizes an exhausted credit allowance', () => {
+ expect(
+ isUsageRefusedError(
+ new BillingException(
+ 'Credits exhausted',
+ BillingExceptionCode.BILLING_CREDITS_EXHAUSTED,
+ ),
+ ),
+ ).toBe(true);
+ });
+
+ it('recognizes an exhausted quota', () => {
+ expect(
+ isUsageRefusedError(
+ new UsageLimitException(
+ 'Usage limit reached for workspace',
+ UsageLimitExceptionCode.QUOTA_EXHAUSTED,
+ ),
+ ),
+ ).toBe(true);
+ });
+
+ it('recognizes an inactive subscription', () => {
+ expect(
+ isUsageRefusedError(
+ new BillingException(
+ 'Workspace has no active subscription',
+ BillingExceptionCode.BILLING_SUBSCRIPTION_INACTIVE,
+ ),
+ ),
+ ).toBe(true);
+ });
+
+ it('recognizes an exhausted stock', () => {
+ expect(
+ isUsageRefusedError(
+ new UsageLimitException(
+ 'This workspace has reached its storage limit',
+ UsageLimitExceptionCode.STOCK_EXHAUSTED,
+ ),
+ ),
+ ).toBe(true);
+ });
+
+ it('leaves a rate limit to the caller, because waiting does help there', () => {
+ expect(
+ isUsageRefusedError(
+ new UsageLimitException(
+ 'Rate limited',
+ UsageLimitExceptionCode.RATE_LIMITED,
+ ),
+ ),
+ ).toBe(false);
+ });
+
+ it('ignores an unrelated failure', () => {
+ expect(isUsageRefusedError(new Error('socket closed'))).toBe(false);
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/billing/utils/is-usage-refused-error.util.ts b/packages/twenty-server/src/engine/core-modules/billing/utils/is-usage-refused-error.util.ts
new file mode 100644
index 00000000000..11d67e6190b
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/billing/utils/is-usage-refused-error.util.ts
@@ -0,0 +1,28 @@
+/* @license Enterprise */
+
+import {
+ BillingException,
+ BillingExceptionCode,
+} from 'src/engine/core-modules/billing/billing.exception';
+import {
+ UsageLimitException,
+ UsageLimitExceptionCode,
+} from 'src/engine/core-modules/usage-limit/exceptions/usage-limit.exception';
+
+const BILLING_REFUSAL_EXCEPTION_CODES = [
+ BillingExceptionCode.BILLING_CREDITS_EXHAUSTED,
+ BillingExceptionCode.BILLING_SUBSCRIPTION_INACTIVE,
+];
+
+const USAGE_LIMIT_REFUSAL_EXCEPTION_CODES = [
+ UsageLimitExceptionCode.QUOTA_EXHAUSTED,
+ UsageLimitExceptionCode.STOCK_EXHAUSTED,
+];
+
+export const isUsageRefusedError = (
+ error: unknown,
+): error is BillingException | UsageLimitException =>
+ (error instanceof BillingException &&
+ BILLING_REFUSAL_EXCEPTION_CODES.includes(error.code)) ||
+ (error instanceof UsageLimitException &&
+ USAGE_LIMIT_REFUSAL_EXCEPTION_CODES.includes(error.code));
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts
index 8178d328d57..d24a4bdd214 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts
@@ -133,6 +133,7 @@ export class LogicFunctionExecutorService {
executionMode,
workspaceDeletionRequestTimestamp,
retry = { retryCount: 0, maxRetries: 0 },
+ shouldEnforceUsageLimits = true,
}: {
logicFunctionId: string;
workspaceId: string;
@@ -142,6 +143,7 @@ export class LogicFunctionExecutorService {
executionMode?: LogicFunctionExecutionMode;
workspaceDeletionRequestTimestamp?: string;
retry?: LogicFunctionRetryContext;
+ shouldEnforceUsageLimits?: boolean;
}): Promise<LogicFunctionExecuteResult> {
const { flatApplication, flatLogicFunction, applicationVariableMaps } =
await this.getFlatEntitiesOrThrow({
@@ -155,6 +157,14 @@ export class LogicFunctionExecutorService {
await this.throttleExecution(workspaceId);
+ if (shouldEnforceUsageLimits) {
+ await this.assertExecutionAllowed({
+ workspaceId,
+ flatApplication,
+ flatLogicFunction,
+ });
+ }
+
const envVariables = await this.getExecutionEnvVariables({
workspaceId,
flatApplication,
@@ -273,6 +283,40 @@ export class LogicFunctionExecutorService {
}
}
+ private async assertExecutionAllowed({
+ workspaceId,
+ flatApplication,
+ flatLogicFunction,
+ }: {
+ workspaceId: string;
+ flatApplication: FlatApplication;
+ flatLogicFunction: FlatLogicFunction;
+ }): Promise<void> {
+ if (isBillingExemptApplication(flatApplication.universalIdentifier)) {
+ return;
+ }
+
+ const isExecutionQuotaEnabled =
+ await this.featureFlagService.isFeatureEnabled(
+ FeatureFlagKey.IS_EXECUTION_QUOTA_ENABLED,
+ workspaceId,
+ );
+
+ if (!isExecutionQuotaEnabled) {
+ return;
+ }
+
+ await this.billingUsageService.assertUsageAllowed({
+ workspaceId,
+ resourceType: UsageResourceType.LOGIC_FUNCTION,
+ operationType: UsageOperationType.CODE_EXECUTION,
+ spenders: {
+ logicFunctionId: flatLogicFunction.id,
+ applicationId: flatApplication.id,
+ },
+ });
+ }
+
private async throttleExecution(workspaceId: string) {
try {
await this.throttlerService.tokenBucketThrottleOrThrow(
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts
index 426437f8d81..30743f07a8d 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts
@@ -5,6 +5,10 @@ import {
import { type LogicFunctionExecutorService } from 'src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service';
import { LogicFunctionJobRunnerService } from 'src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service';
+import {
+ UsageLimitException,
+ UsageLimitExceptionCode,
+} from 'src/engine/core-modules/usage-limit/exceptions/usage-limit.exception';
import {
LogicFunctionException,
LogicFunctionExceptionCode,
@@ -93,6 +97,26 @@ describe('LogicFunctionJobRunnerService', () => {
expect(persistRetryCount).not.toHaveBeenCalled();
});
+ it('does not fail the job when usage was refused', async () => {
+ const execute = jest
+ .fn()
+ .mockRejectedValue(
+ new UsageLimitException(
+ 'Usage limit reached for workspace',
+ UsageLimitExceptionCode.QUOTA_EXHAUSTED,
+ ),
+ );
+ const { service, persistRetryCount } = buildService(execute);
+
+ await service.run({
+ logicFunctionPayload,
+ retryLimit: 3,
+ persistRetryCount,
+ });
+
+ expect(persistRetryCount).not.toHaveBeenCalled();
+ });
+
it('does not fail the job when the application is stopped', async () => {
const execute = jest
.fn()
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts
index 3494b0f503a..b6a48b5b0c3 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts
@@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { RetryableLogicFunctionError } from 'twenty-shared/logic-function';
+import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
import { LogicFunctionExecutorService } from 'src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service';
import { LOGIC_FUNCTION_APPLICATION_RETRY_LIMIT } from 'src/engine/core-modules/logic-function/logic-function-trigger/constants/logic-function-application-retry-limit.constant';
import { isRetryableLogicFunctionExecutionError } from 'src/engine/core-modules/logic-function/logic-function-trigger/utils/is-retryable-logic-function-execution-error.util';
@@ -76,6 +77,14 @@ export class LogicFunctionJobRunnerService {
return;
}
+ if (isUsageRefusedError(error)) {
+ this.logger.warn(
+ `Skipping function ${logicFunctionPayload.logicFunctionId} (workspace ${logicFunctionPayload.workspaceId}): ${error.message}`,
+ );
+
+ return;
+ }
+
if (
error instanceof LogicFunctionException &&
error.code ===
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/route/route-trigger.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/route/route-trigger.service.ts
index 4fab7dddc9c..e46c1f66744 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/route/route-trigger.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/route/route-trigger.service.ts
@@ -10,6 +10,7 @@ import { HTTPMethod } from 'twenty-shared/types';
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
import { AuthException } from 'src/engine/core-modules/auth/auth.exception';
+import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
import { AccessTokenService } from 'src/engine/core-modules/auth/token/services/access-token.service';
import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.service';
@@ -325,7 +326,10 @@ export class RouteTriggerService {
userWorkspaceId,
});
} catch (error) {
- if (error instanceof RouteTriggerException) {
+ if (
+ error instanceof RouteTriggerException ||
+ isUsageRefusedError(error)
+ ) {
throw error;
}
diff --git a/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.controller.ts b/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.controller.ts
index 84a027bfd4e..d71699238b9 100644
--- a/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.controller.ts
+++ b/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.controller.ts
@@ -12,15 +12,21 @@ import {
import { Request, Response } from 'express';
import { ApiPath } from 'twenty-shared/types';
+import { BillingRestApiExceptionFilter } from 'src/engine/core-modules/billing/filters/billing-api-exception.filter';
import { sendRouteTriggerResponse } from 'src/engine/core-modules/logic-function/logic-function-trigger/triggers/route/utils/route-trigger-response.util';
import { ServerRouteTriggerRestApiExceptionFilter } from 'src/engine/core-modules/server-route-trigger/exceptions/server-route-trigger-rest-api-exception-filter';
import { ServerRouteTriggerService } from 'src/engine/core-modules/server-route-trigger/server-route-trigger.service';
+import { UsageLimitRestApiExceptionFilter } from 'src/engine/core-modules/usage-limit/filters/usage-limit-rest-api-exception.filter';
import { NoPermissionGuard } from 'src/engine/guards/no-permission.guard';
import { PublicEndpointGuard } from 'src/engine/guards/public-endpoint.guard';
@Controller(`${ApiPath.Webhooks}/server`)
@UseGuards(PublicEndpointGuard, NoPermissionGuard)
-@UseFilters(ServerRouteTriggerRestApiExceptionFilter)
+@UseFilters(
+ ServerRouteTriggerRestApiExceptionFilter,
+ UsageLimitRestApiExceptionFilter,
+ BillingRestApiExceptionFilter,
+)
export class ServerRouteTriggerController {
constructor(
private readonly serverRouteTriggerService: ServerRouteTriggerService,
diff --git a/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.service.ts b/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.service.ts
index 36d6a7db2b7..74b3be2bbb0 100644
--- a/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/server-route-trigger/server-route-trigger.service.ts
@@ -4,6 +4,7 @@ import { Request } from 'express';
import { isLogicFunctionHttpResponse } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
+import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
import {
LogicFunctionExecutionException,
LogicFunctionExecutionExceptionCode,
@@ -251,6 +252,10 @@ export class ServerRouteTriggerService {
payload,
});
} catch (error) {
+ if (isUsageRefusedError(error)) {
+ throw error;
+ }
+
this.logger.error(
`Server logic function ${logicFunction.id} failed in workspace ${workspaceId}: ${error instanceof Error ? error.message : String(error)}`,
error instanceof Error ? error.stack : undefined,
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts
index b20f0d79bb3..1eb1dc984ec 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts
@@ -48,7 +48,13 @@ export const USAGE_LIMIT_DEFINITIONS = {
allowedMeters: ['creditsUsedMicro', 'quantity'],
},
},
- [UsageResourceType.WORKFLOW]: {},
+ [UsageResourceType.WORKFLOW]: {
+ quota: {
+ allowedOperationTypes: [UsageOperationType.WORKFLOW_EXECUTION],
+ allowedSpenderTypes: ['workspace', 'application'],
+ allowedMeters: ['creditsUsedMicro', 'quantity'],
+ },
+ },
[UsageResourceType.APP]: {},
[UsageResourceType.STORAGE]: {
stock: {
@@ -65,7 +71,13 @@ export const USAGE_LIMIT_DEFINITIONS = {
],
},
},
- [UsageResourceType.LOGIC_FUNCTION]: {},
+ [UsageResourceType.LOGIC_FUNCTION]: {
+ quota: {
+ allowedOperationTypes: [UsageOperationType.CODE_EXECUTION],
+ allowedSpenderTypes: ['workspace', 'application', 'logicFunction'],
+ allowedMeters: ['creditsUsedMicro', 'quantity'],
+ },
+ },
[UsageResourceType.EMAIL]: {
speed: {
allowedOperationTypes: [
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-quota-definitions.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-quota-definitions.util.spec.ts
index b11ccd6f722..94586644739 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-quota-definitions.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-quota-definitions.util.spec.ts
@@ -8,7 +8,11 @@ describe('buildQuotaDefinitions', () => {
resourceType,
limitKind,
]),
- ).toEqual([[UsageResourceType.AI, 'quota']]);
+ ).toEqual([
+ [UsageResourceType.AI, 'quota'],
+ [UsageResourceType.WORKFLOW, 'quota'],
+ [UsageResourceType.LOGIC_FUNCTION, 'quota'],
+ ]);
});
it('carries the allow lists a form needs to offer a scope', () => {
diff --git a/packages/twenty-server/src/engine/metadata-modules/route-trigger/route-trigger.controller.ts b/packages/twenty-server/src/engine/metadata-modules/route-trigger/route-trigger.controller.ts
index 0d4a795020f..a32c68fb20d 100644
--- a/packages/twenty-server/src/engine/metadata-modules/route-trigger/route-trigger.controller.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/route-trigger/route-trigger.controller.ts
@@ -14,6 +14,8 @@ import {
import { Request, Response } from 'express';
import { ApiPath, HTTPMethod } from 'twenty-shared/types';
+import { BillingRestApiExceptionFilter } from 'src/engine/core-modules/billing/filters/billing-api-exception.filter';
+import { UsageLimitRestApiExceptionFilter } from 'src/engine/core-modules/usage-limit/filters/usage-limit-rest-api-exception.filter';
import { NoPermissionGuard } from 'src/engine/guards/no-permission.guard';
import { PublicEndpointGuard } from 'src/engine/guards/public-endpoint.guard';
import { RouteTriggerRestApiExceptionFilter } from 'src/engine/core-modules/logic-function/logic-function-trigger/triggers/route/exceptions/route-trigger-rest-api-exception-filter';
@@ -22,7 +24,11 @@ import { sendRouteTriggerResponse } from 'src/engine/core-modules/logic-function
@Controller(ApiPath.RouteTrigger)
@UseGuards(PublicEndpointGuard, NoPermissionGuard)
-@UseFilters(RouteTriggerRestApiExceptionFilter)
+@UseFilters(
+ RouteTriggerRestApiExceptionFilter,
+ UsageLimitRestApiExceptionFilter,
+ BillingRestApiExceptionFilter,
+)
export class RouteTriggerController {
constructor(private readonly routeTriggerService: RouteTriggerService) {}
diff --git a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/core/utils/seed-feature-flags.util.ts b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/core/utils/seed-feature-flags.util.ts
index 20b75c82c26..e48e42f739d 100644
--- a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/core/utils/seed-feature-flags.util.ts
+++ b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/core/utils/seed-feature-flags.util.ts
@@ -10,6 +10,7 @@ const DEFAULT_SEEDED_FEATURE_FLAGS: Partial<Record<FeatureFlagKey, boolean>> = {
[FeatureFlagKey.IS_MESSAGE_CAMPAIGN_ENABLED]: true,
[FeatureFlagKey.IS_RECORD_SHARING_ENABLED]: false,
[FeatureFlagKey.IS_INITIAL_OBJECT_VIEW_ENABLED]: false,
+ [FeatureFlagKey.IS_EXECUTION_QUOTA_ENABLED]: true,
};
type SeedFeatureFlagsArgs = {
diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/constant/default-feature-flags.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/constant/default-feature-flags.ts
index a6e31f106e9..54b0714eb97 100644
--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/constant/default-feature-flags.ts
+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/constant/default-feature-flags.ts
@@ -2,4 +2,5 @@ import { FeatureFlagKey } from 'twenty-shared/types';
export const DEFAULT_FEATURE_FLAGS = [
FeatureFlagKey.IS_REST_METADATA_API_NEW_FORMAT_DIRECT,
+ FeatureFlagKey.IS_EXECUTION_QUOTA_ENABLED,
] as const satisfies FeatureFlagKey[];
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-output.type.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-output.type.ts
index 4d909ed6239..7ac2b760639 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-output.type.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-output.type.ts
@@ -7,4 +7,5 @@ export type WorkflowActionOutput = {
shouldRemainRunning?: boolean;
shouldSkipStepExecution?: boolean;
shouldFailSafely?: boolean;
+ isUsageRefused?: boolean;
};
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/is-user-facing-workflow-executor-error.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/is-user-facing-workflow-executor-error.util.ts
index ade65a25789..116eae9656e 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/is-user-facing-workflow-executor-error.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/is-user-facing-workflow-executor-error.util.ts
@@ -1,11 +1,4 @@
-import {
- BillingException,
- BillingExceptionCode,
-} from 'src/engine/core-modules/billing/billing.exception';
-import {
- UsageLimitException,
- UsageLimitExceptionCode,
-} from 'src/engine/core-modules/usage-limit/exceptions/usage-limit.exception';
+import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
import {
AiException,
AiExceptionCode,
@@ -21,15 +14,6 @@ const USER_FACING_STEP_EXECUTOR_EXCEPTION_CODES = [
WorkflowStepExecutorExceptionCode.STEP_NOT_FOUND,
];
-const USER_FACING_BILLING_EXCEPTION_CODES = [
- BillingExceptionCode.BILLING_CREDITS_EXHAUSTED,
- BillingExceptionCode.BILLING_SUBSCRIPTION_INACTIVE,
-];
-
-const USER_FACING_USAGE_LIMIT_EXCEPTION_CODES = [
- UsageLimitExceptionCode.QUOTA_EXHAUSTED,
-];
-
// A step asking for a model the instance does not serve, or asking it something
// it cannot answer, is a configuration the author has to change: no retry will
// produce a model, and reporting it as a system error buries real ones.
@@ -47,12 +31,8 @@ export const isUserFacingWorkflowExecutorError = (error: unknown): boolean => {
return USER_FACING_STEP_EXECUTOR_EXCEPTION_CODES.includes(error.code);
}
- if (error instanceof BillingException) {
- return USER_FACING_BILLING_EXCEPTION_CODES.includes(error.code);
- }
-
- if (error instanceof UsageLimitException) {
- return USER_FACING_USAGE_LIMIT_EXCEPTION_CODES.includes(error.code);
+ if (isUsageRefusedError(error)) {
+ return true;
}
if (error instanceof AiException) {
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
index 0a057a3107b..7bb4d79c484 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
@@ -1,6 +1,7 @@
import { WorkflowCoreSyncService } from 'src/engine/core-modules/workflow/services/workflow-core-sync.service';
import { Injectable } from '@nestjs/common';
+import { FeatureFlagKey } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import {
getWorkflowRunContext,
@@ -10,7 +11,9 @@ import {
} from 'twenty-shared/workflow';
import { BillingUsageService } from 'src/engine/core-modules/billing/services/billing-usage.service';
+import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
+import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
@@ -52,6 +55,11 @@ import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runne
const MAX_EXECUTED_STEPS_COUNT = 20;
+type WorkflowBillingSpenders = {
+ workflowId: string;
+ applicationId: string;
+};
+
@Injectable()
export class WorkflowExecutorWorkspaceService {
constructor(
@@ -60,6 +68,7 @@ export class WorkflowExecutorWorkspaceService {
private readonly usageRecorderService: UsageRecorderService,
private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,
private readonly billingUsageService: BillingUsageService,
+ private readonly featureFlagService: FeatureFlagService,
private readonly exceptionHandlerService: ExceptionHandlerService,
private readonly metricsService: MetricsService,
@InjectMessageQueue(MessageQueue.workflowQueue)
@@ -135,6 +144,11 @@ export class WorkflowExecutorWorkspaceService {
);
}
+ const billingSpenders = {
+ workflowId: workflow.workspaceWorkflowId ?? workflow.id,
+ applicationId: workflow.applicationId,
+ };
+
let actionOutput: WorkflowActionOutput;
if (
@@ -151,6 +165,7 @@ export class WorkflowExecutorWorkspaceService {
stepInfos,
workflowRunId,
workspaceId,
+ billingSpenders,
});
if (isDefined(actionOutput.error) && !actionOutput.isUserError) {
@@ -173,7 +188,7 @@ export class WorkflowExecutorWorkspaceService {
}
}
- if (isDefined(actionOutput.error)) {
+ if (isDefined(actionOutput.error) && !actionOutput.isUsageRefused) {
const enclosingIterator = findEnclosingIteratorWithContinueOnFailure({
failedStepId: stepId,
steps,
@@ -218,9 +233,7 @@ export class WorkflowExecutorWorkspaceService {
!actionOutput.shouldFailSafely &&
!actionOutput.shouldSkipStepExecution
) {
- const billingWorkflowId = workflow.workspaceWorkflowId ?? workflow.id;
-
- await this.sendWorkflowNodeRunEvent(workspaceId, billingWorkflowId);
+ await this.sendWorkflowNodeRunEvent(workspaceId, billingSpenders);
}
const { shouldProcessNextSteps } = await this.processStepExecutionResult({
@@ -355,15 +368,54 @@ export class WorkflowExecutorWorkspaceService {
});
}
+ private async getNodeRunRefusal({
+ workspaceId,
+ billingSpenders,
+ }: {
+ workspaceId: string;
+ billingSpenders: WorkflowBillingSpenders;
+ }): Promise<WorkflowActionOutput | undefined> {
+ const isExecutionQuotaEnabled =
+ await this.featureFlagService.isFeatureEnabled(
+ FeatureFlagKey.IS_EXECUTION_QUOTA_ENABLED,
+ workspaceId,
+ );
+
+ if (!isExecutionQuotaEnabled) {
+ return undefined;
+ }
+
+ try {
+ await this.billingUsageService.assertUsageAllowed({
+ workspaceId,
+ resourceType: UsageResourceType.WORKFLOW,
+ operationType: UsageOperationType.WORKFLOW_EXECUTION,
+ spenders: billingSpenders,
+ });
+
+ return undefined;
+ } catch (error) {
+ if (!isUsageRefusedError(error)) {
+ throw error;
+ }
+
+ return {
+ error: error.message,
+ isUserError: true,
+ isUsageRefused: true,
+ };
+ }
+ }
+
private async sendWorkflowNodeRunEvent(
workspaceId: string,
- workflowId: string,
+ billingSpenders: WorkflowBillingSpenders,
) {
await this.billingUsageService.consumeUsageQuota({
workspaceId,
resourceType: UsageResourceType.WORKFLOW,
operationType: UsageOperationType.WORKFLOW_EXECUTION,
- spenders: { workflowId },
+ spenders: billingSpenders,
cost: { creditsUsedMicro: 100, quantity: 1 },
});
@@ -374,8 +426,8 @@ export class WorkflowExecutorWorkspaceService {
creditsUsedMicro: 100,
quantity: 1,
unit: UsageUnit.INVOCATION,
- resourceId: workflowId,
- spenders: { workflowId },
+ resourceId: billingSpenders.workflowId,
+ spenders: billingSpenders,
},
]);
}
@@ -454,17 +506,15 @@ export class WorkflowExecutorWorkspaceService {
stepInfos,
workflowRunId,
workspaceId,
+ billingSpenders,
}: {
step: WorkflowAction;
steps: WorkflowAction[];
stepInfos: WorkflowRunStepInfos;
workflowRunId: string;
workspaceId: string;
+ billingSpenders: WorkflowBillingSpenders;
}) {
- // Credit-cap enforcement lives at the AI entry points (chat resolver,
- // executeAgent, generate-text controller, title generation). Cheap
- // workflow steps (DB CRUD, branching, actions) are not gated here so a
- // chat-driven cap exhaustion does not block non-AI automations.
const stepId = step.id;
const workflowAction = this.workflowActionFactory.get(step.type);
@@ -480,6 +530,15 @@ export class WorkflowExecutorWorkspaceService {
});
try {
+ const nodeRunRefusal = await this.getNodeRunRefusal({
+ workspaceId,
+ billingSpenders,
+ });
+
+ if (isDefined(nodeRunRefusal)) {
+ return nodeRunRefusal;
+ }
+
return await workflowAction.execute({
currentStepId: stepId,
steps,
diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts
index b5374459271..a8bfded827e 100644
--- a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts
@@ -361,11 +361,17 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
await waitForRun(await runFixture(fixture), 'COMPLETED');
+ const [workspace] = await global.testDataSource.query(
+ `SELECT "workspaceCustomApplicationId" FROM core.workspace WHERE id = $1`,
+ [workspaceId],
+ );
+
expect(consume).toHaveBeenCalledWith(
expect.objectContaining({
workspaceId,
spenders: {
workflowId: fixture.workflowId ?? fixture.coreWorkflowId,
+ applicationId: workspace.workspaceCustomApplicationId,
},
}),
);
@@ -945,7 +951,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
await waitForRun(runId, 'COMPLETED');
});
- it('preserves the subscription check and execution when billing enforcement is disabled', async () => {
+ it('fails the run when the subscription is inactive', async () => {
const fixture = await createFixture({ mirrorless: true });
const subscriptionCheck = jest
.spyOn(
@@ -954,12 +960,37 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
)
.mockResolvedValue('WORKSPACE_SUSPENDED');
const runId = await runFixture(fixture);
- const run = await waitForRun(runId, 'COMPLETED');
+ const run = await waitForRun(runId, 'FAILED');
expect(subscriptionCheck).toHaveBeenCalledWith(workspaceId);
expect(run.coreWorkflowVersionId).toBe(fixture.coreWorkflowVersionId);
expect(run.state.flow.steps).toEqual(fixture.steps);
});
+ it('fails the run when the subscription is inactive even if the step continues on failure', async () => {
+ const step: WorkflowEmptyAction = {
+ ...emptyStep(),
+ settings: {
+ ...settings,
+ errorHandlingOptions: {
+ retryOnFailure: { value: 0 },
+ continueOnFailure: { value: true },
+ },
+ },
+ };
+ const fixture = await createFixture({ mirrorless: true, steps: [step] });
+
+ jest
+ .spyOn(
+ global.workflowTestServices.billing,
+ 'getSubscriptionInactiveReason',
+ )
+ .mockResolvedValue('WORKSPACE_SUSPENDED');
+
+ const run = await waitForRun(await runFixture(fixture), 'FAILED');
+
+ expect(run.state.stepInfos[step.id].status).toBe('FAILED');
+ });
+
it('edits, activates and executes through both APIs across flag ON / OFF / ON', async () => {
const flags = global.workflowTestServices.flags;
const featureFlag = FeatureFlagKey.IS_WORKFLOW_CORE_INDEX_PAGE_ENABLED;
diff --git a/packages/twenty-shared/src/types/FeatureFlagKey.ts b/packages/twenty-shared/src/types/FeatureFlagKey.ts
index dc088ebf59f..0e70d399aca 100644
--- a/packages/twenty-shared/src/types/FeatureFlagKey.ts
+++ b/packages/twenty-shared/src/types/FeatureFlagKey.ts
@@ -13,4 +13,5 @@ export enum FeatureFlagKey {
IS_INITIAL_OBJECT_VIEW_ENABLED = 'IS_INITIAL_OBJECT_VIEW_ENABLED',
IS_WEBHOOK_RATE_LIMIT_ENABLED = 'IS_WEBHOOK_RATE_LIMIT_ENABLED',
IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED = 'IS_DEFERRED_WORKSPACE_MIGRATION_ACTIONS_ENABLED',
+ IS_EXECUTION_QUOTA_ENABLED = 'IS_EXECUTION_QUOTA_ENABLED',
}