The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
refactor(usage-limit): stock counters hold what is held, keyed by scope (#27866)
Etienne09a60835f14c
diff --git a/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-entitlement-sync.service.ts b/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-entitlement-sync.service.tsindex 7e0afecc108..47eb36bb1a1 100644--- a/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-entitlement-sync.service.ts+++ b/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-entitlement-sync.service.ts@@ -9,6 +9,7 @@ import { BILLING_ENTITLEMENT_STATE_LOCK_OPTIONS } from 'src/engine/core-modules/import { buildBillingEntitlementStateLockKey } from 'src/engine/core-modules/billing/utils/build-billing-entitlement-state-lock-key.util';import { buildBillingEntitlementsFromLookupKeys } from 'src/engine/core-modules/billing/utils/build-billing-entitlements-from-lookup-keys.util';import { UsageLimitQuotaService } from 'src/engine/core-modules/usage-limit/services/usage-limit-quota.service';+import { UsageLimitStockService } from 'src/engine/core-modules/usage-limit/services/usage-limit-stock.service';import { RowLevelPermissionPredicateGroupService } from 'src/engine/metadata-modules/row-level-permission-predicate/services/row-level-permission-predicate-group.service';import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';@@ -30,6 +31,7 @@ export class BillingEntitlementSyncService {private readonly billingEntitlementRepository: WorkspaceScopedRepository<BillingEntitlementEntity>,private readonly rowLevelPermissionPredicateGroupService: RowLevelPermissionPredicateGroupService,private readonly usageLimitQuotaService: UsageLimitQuotaService,+ private readonly usageLimitStockService: UsageLimitStockService,private readonly cacheLockService: CacheLockService,private readonly workspaceCacheService: WorkspaceCacheService,) {}@@ -82,6 +84,9 @@ export class BillingEntitlementSyncService {await this.usageLimitQuotaService.dropIntraWorkspaceLimitCounters(workspaceId,);+ await this.usageLimitStockService.dropIntraWorkspaceLimitCounters(+ workspaceId,+ );}await this.billingEntitlementRepository.upsert(diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.tsindex b17dd5b58ce..5403e9617d5 100644--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts@@ -711,7 +711,7 @@ export class ConfigVariables {@ConfigVariablesMetadata({group: ConfigVariablesGroup.STORAGE_CONFIG,description:- 'Maximum bytes of file storage a single workspace may hold, all its files counted. An upload that would cross it is refused. No limit when unset',+ 'Maximum bytes of file storage a single workspace may hold, all its files counted. No limit when unset',type: ConfigVariableType.NUMBER,})@CastToPositiveNumber()@@ -721,7 +721,7 @@ export class ConfigVariables {@ConfigVariablesMetadata({group: ConfigVariablesGroup.STORAGE_CONFIG,description:- 'Maximum number of records a single workspace may hold across its objects, system objects other than messages and calendar events left out, soft-deleted records included. A write that would cross it is refused. No limit when unset',+ 'Maximum number of records a single workspace may hold across its objects, system objects other than messages and calendar events left out, soft-deleted records included. No limit when unset',type: ConfigVariableType.NUMBER,})@CastToPositiveNumber()diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/consume-quota-counters-script.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/consume-quota-counters-script.constant.tsdeleted file mode 100644index 3386cb839ac..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/consume-quota-counters-script.constant.ts+++ /dev/null@@ -1,21 +0,0 @@-import { type CacheScript } from 'src/engine/core-modules/cache-storage/types/cache-script.type';--export const CONSUME_QUOTA_COUNTERS_SCRIPT: CacheScript = {- name: 'usage-limit:consume-quota-counters',- source: `-local costs = cjson.decode(ARGV[1])-local result = {}--for i = 1, #KEYS do- if redis.call('EXISTS', KEYS[i]) == 1 then- result[2 * i - 1] = 1- result[2 * i] = redis.call('DECRBY', KEYS[i], costs[i])- else- result[2 * i - 1] = 0- result[2 * i] = 0- end-end--return result-`,-};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/release-stock-counters-script.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/release-stock-counters-script.constant.tsdeleted file mode 100644index fe9cf497613..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/release-stock-counters-script.constant.ts+++ /dev/null@@ -1,21 +0,0 @@-import { type CacheScript } from 'src/engine/core-modules/cache-storage/types/cache-script.type';--export const RELEASE_STOCK_COUNTERS_SCRIPT: CacheScript = {- name: 'usage-limit:release-stock-counters',- source: `-local amounts = cjson.decode(ARGV[1])-local caps = cjson.decode(ARGV[2])--for i = 1, #KEYS do- if redis.call('EXISTS', KEYS[i]) == 1 then- local remaining = redis.call('INCRBY', KEYS[i], amounts[i])-- if remaining > caps[i] then- redis.call('SET', KEYS[i], caps[i], 'KEEPTTL')- end- end-end--return 1-`,-};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/warm-and-consume-quota-counters-script.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/warm-and-consume-counters-script.constant.tssimilarity index 71%rename from packages/twenty-server/src/engine/core-modules/usage-limit/constants/warm-and-consume-quota-counters-script.constant.tsrename to packages/twenty-server/src/engine/core-modules/usage-limit/constants/warm-and-consume-counters-script.constant.tsindex 5a1da4acd73..83abaf9d8d6 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/warm-and-consume-quota-counters-script.constant.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/warm-and-consume-counters-script.constant.ts@@ -1,7 +1,7 @@import { type CacheScript } from 'src/engine/core-modules/cache-storage/types/cache-script.type';-export const WARM_AND_CONSUME_QUOTA_COUNTERS_SCRIPT: CacheScript = {- name: 'usage-limit:warm-and-consume-quota-counters',+export const WARM_AND_CONSUME_COUNTERS_SCRIPT: CacheScript = {+ name: 'usage-limit:warm-and-consume-counters',source: `local amounts = cjson.decode(ARGV[1])local warmValues = cjson.decode(ARGV[2])@@ -15,6 +15,11 @@ for i = 1, #KEYS doif redis.call('EXISTS', KEYS[i]) == 1 thenresult[i] = redis.call('INCRBY', KEYS[i], amounts[i])++ if result[i] < 0 then+ redis.call('SET', KEYS[i], 0, 'KEEPTTL')+ result[i] = 0+ endelseresult[i] = falseenddiff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-quota.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-quota.service.tsindex c0a82bc35de..1082f9816b2 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-quota.service.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-quota.service.ts@@ -10,7 +10,7 @@ import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/typimport { MetricsService } from 'src/engine/core-modules/metrics/metrics.service';import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.type';import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';-import { WARM_AND_CONSUME_QUOTA_COUNTERS_SCRIPT } from 'src/engine/core-modules/usage-limit/constants/warm-and-consume-quota-counters-script.constant';+import { WARM_AND_CONSUME_COUNTERS_SCRIPT } from 'src/engine/core-modules/usage-limit/constants/warm-and-consume-counters-script.constant';import { UsageLimitException } from 'src/engine/core-modules/usage-limit/exceptions/usage-limit.exception';import { CreditAllowanceProvider } from 'src/engine/core-modules/usage-limit/interfaces/credit-allowance-provider.service';import { UsageLimitEntitlementService } from 'src/engine/core-modules/usage-limit/services/usage-limit-entitlement.service';@@ -814,7 +814,7 @@ export class UsageLimitQuotaService implements OnModuleInit {}const results = await this.cacheStorage.runScript<unknown[]>({- script: WARM_AND_CONSUME_QUOTA_COUNTERS_SCRIPT,+ script: WARM_AND_CONSUME_COUNTERS_SCRIPT,keys: counters.map((counter) => counter.key),args: buildConsumeScriptArguments(entries),});diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.tsindex aa1d6e53d30..c11b9f1c895 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts@@ -1,52 +1,45 @@import { Injectable, Logger } from '@nestjs/common';+import uniqBy from 'lodash.uniqby';import { isDefined } from 'twenty-shared/utils';-import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';-import {- CacheLockException,- CacheLockExceptionCode,-} from 'src/engine/core-modules/cache-lock/exceptions/cache-lock.exception';import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';-import { type CacheScript } from 'src/engine/core-modules/cache-storage/types/cache-script.type';import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';import { MetricsService } from 'src/engine/core-modules/metrics/metrics.service';import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.type';import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';-import { CONSUME_QUOTA_COUNTERS_SCRIPT } from 'src/engine/core-modules/usage-limit/constants/consume-quota-counters-script.constant';-import { RELEASE_STOCK_COUNTERS_SCRIPT } from 'src/engine/core-modules/usage-limit/constants/release-stock-counters-script.constant';+import { WARM_AND_CONSUME_COUNTERS_SCRIPT } from 'src/engine/core-modules/usage-limit/constants/warm-and-consume-counters-script.constant';import {UsageLimitException,UsageLimitExceptionCode,} from 'src/engine/core-modules/usage-limit/exceptions/usage-limit.exception';import { UsageLimitEntitlementService } from 'src/engine/core-modules/usage-limit/services/usage-limit-entitlement.service';import { type ComputeUsedStock } from 'src/engine/core-modules/usage-limit/types/compute-used-stock.type';-import { type SpenderType } from 'src/engine/core-modules/usage-limit/types/spender-type.type';+import { type FlatUsageLimit } from 'src/engine/core-modules/usage-limit/types/flat-usage-limit.type';import { type StockCost } from 'src/engine/core-modules/usage-limit/types/stock-cost.type';import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';import { type StockLimitDefault } from 'src/engine/core-modules/usage-limit/types/stock-limit-default.type';import { type StockResourceType } from 'src/engine/core-modules/usage-limit/types/stock-resource-type.type';-import { buildStockCounterKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util';+import { buildConsumeScriptArguments } from 'src/engine/core-modules/usage-limit/utils/build-consume-script-arguments.util';+import { buildIntraWorkspaceStockCounterKeys } from 'src/engine/core-modules/usage-limit/utils/build-intra-workspace-stock-counter-keys.util';+import { buildReleaseScriptArguments } from 'src/engine/core-modules/usage-limit/utils/build-release-script-arguments.util';+import { buildStockCounter } from 'src/engine/core-modules/usage-limit/utils/build-stock-counter.util';import { buildStockCounters } from 'src/engine/core-modules/usage-limit/utils/build-stock-counters.util';-import { buildStockDefaultCounterKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-default-counter-key.util';import { buildStockExhaustedScope } from 'src/engine/core-modules/usage-limit/utils/build-stock-exhausted-scope.util';-import { buildStockWarmedEntries } from 'src/engine/core-modules/usage-limit/utils/build-stock-warmed-entries.util';import { buildStockScopeKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-scope-key.util';-import { buildStockWarmLockKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-warm-lock-key.util';-import { findExhaustedStockCounter } from 'src/engine/core-modules/usage-limit/utils/find-exhausted-stock-counter.util';+import { findSuppressedUsageLimitDefaults } from 'src/engine/core-modules/usage-limit/utils/find-suppressed-usage-limit-defaults.util';import { findUsageLimitDefinition } from 'src/engine/core-modules/usage-limit/utils/find-usage-limit-definition.util';import { getStockExhaustedUserFriendlyMessage } from 'src/engine/core-modules/usage-limit/utils/get-stock-exhausted-user-friendly-message.util';+import { isLimitExhausted } from 'src/engine/core-modules/usage-limit/utils/is-limit-exhausted.util';import { isStockLimit } from 'src/engine/core-modules/usage-limit/utils/is-stock-limit.util';import { isStockResourceType } from 'src/engine/core-modules/usage-limit/utils/is-stock-resource-type.util';import { type UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';import { type UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';-import { type UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';import { type UsageSpenders } from 'src/engine/core-modules/usage/types/usage-spenders.type';import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';const STOCK_COUNTER_TTL_MS = 60 * 60 * 1000;-const STOCK_WARM_LOCK_OPTIONS = { ms: 50, maxRetries: 20, ttl: 10_000 };type StockArgs = {workspaceId: string;@@ -63,7 +56,6 @@ export class UsageLimitStockService {@InjectCacheStorage(CacheStorageNamespace.EngineUsageLimit)private readonly cacheStorage: CacheStorageService,private readonly workspaceCacheService: WorkspaceCacheService,- private readonly cacheLockService: CacheLockService,private readonly usageLimitEntitlementService: UsageLimitEntitlementService,private readonly metricsService: MetricsService,private readonly twentyConfigService: TwentyConfigService,@@ -78,35 +70,39 @@ export class UsageLimitStockService {computeUsedStock: ComputeUsedStock;}): Promise<void> {try {- const counters = await this.buildCounters(args);+ const counters = await this.buildChargedCounters({ ...args, cost });if (counters.length === 0) {return;}- const remainings = await this.readRemainings({- workspaceId: args.workspaceId,+ const heldValueByKey = await this.warmHeldValues({counters,computeUsedStock,});- const exhausted = findExhaustedStockCounter({- counters,- remainings,- cost,- });-- if (isDefined(exhausted)) {- throw new UsageLimitException(- `${exhausted.counter.resourceType} limit reached for this workspace`,- UsageLimitExceptionCode.STOCK_EXHAUSTED,- {- userFriendlyMessage: getStockExhaustedUserFriendlyMessage(- exhausted.counter.resourceType,- ),- exhaustedScope: buildStockExhaustedScope(exhausted),- },- );+ for (const counter of counters) {+ const held = heldValueByKey.get(counter.key);++ if (+ isDefined(held) &&+ isLimitExhausted({+ consumed: held,+ cost: cost[counter.unit] ?? 0,+ limitValue: counter.limitValue,+ })+ ) {+ throw new UsageLimitException(+ `${counter.resourceType} limit reached for this workspace`,+ UsageLimitExceptionCode.STOCK_EXHAUSTED,+ {+ userFriendlyMessage: getStockExhaustedUserFriendlyMessage(+ counter.resourceType,+ ),+ exhaustedScope: buildStockExhaustedScope({ counter, held }),+ },+ );+ }}} catch (error) {this.admitOnFailure({ error, workspaceId: args.workspaceId });@@ -120,10 +116,10 @@ export class UsageLimitStockService {await this.applyStockDelta({...args,cost,- script: CONSUME_QUOTA_COUNTERS_SCRIPT,- buildArgs: (counters) => [- JSON.stringify(counters.map((counter) => cost[counter.unit] ?? 0)),- ],+ buildArguments: (amounts) =>+ buildConsumeScriptArguments(+ amounts.map((amount) => ({ amount, warm: null })),+ ),});}@@ -134,141 +130,95 @@ export class UsageLimitStockService {await this.applyStockDelta({...args,cost,- script: RELEASE_STOCK_COUNTERS_SCRIPT,- buildArgs: (counters) => [- JSON.stringify(counters.map((counter) => cost[counter.unit] ?? 0)),- JSON.stringify(counters.map((counter) => counter.limitValue)),- ],+ buildArguments: buildReleaseScriptArguments,});}- async dropStockCounters({+ async dropLimitCounter({workspaceId,- resourceType,- operationType,- spenderType,- spenderId,- unit,- limitValue,+ usageLimit,}: {workspaceId: string;- resourceType: UsageResourceType;- operationType: UsageOperationType;- spenderType: SpenderType;- spenderId: string | null;- unit: UsageUnit;- limitValue: number;- }) {- const keys = [- buildStockCounterKey({- workspaceId,- resourceType,- operationType,- spenderType,- spenderId,- unit,- limitValue,- }),- ];-- if (isStockResourceType(resourceType)) {- keys.push(- ...this.buildStockLimitDefaults({ resourceType, operationType })- .filter(- (stockLimitDefault) =>- stockLimitDefault.spenderType === spenderType,- )- .map((stockLimitDefault) =>- buildStockDefaultCounterKey({- workspaceId,- resourceType,- operationType,- spenderType,- unit: stockLimitDefault.unit,- limitValue: stockLimitDefault.limitValue,- }),+ usageLimit: FlatUsageLimit;+ }): Promise<void> {+ if (+ !isStockLimit(usageLimit) ||+ findSuppressedUsageLimitDefaults(usageLimit).some((usageLimitDefault) =>+ isDefined(+ this.twentyConfigService.get(+ usageLimitDefault.limitValueConfigVariable,),- );+ ),+ )+ ) {+ return;}- await this.dropCounterKeys({ workspaceId, keys });+ await this.cacheStorage.del(+ buildStockCounter({ workspaceId, limit: usageLimit }).key,+ );+ }++ async dropIntraWorkspaceLimitCounters(workspaceId: string): Promise<void> {+ const { usageLimits } = await this.workspaceCacheService.getOrRecompute(+ workspaceId,+ ['usageLimits'],+ );++ await this.cacheStorage.mdel(+ buildIntraWorkspaceStockCounterKeys({+ workspaceId,+ limits: Object.values(usageLimits.byResourceType).flatMap(+ (limits) => limits ?? [],+ ),+ }),+ );}async invalidateStock(args: StockArgs): Promise<void> {try {const counters = await this.buildCounters(args);- if (counters.length === 0) {- return;- }-- await this.dropCounterKeys({- workspaceId: args.workspaceId,- keys: counters.map((counter) => counter.key),- });+ await this.cacheStorage.mdel(counters.map((counter) => counter.key));} catch (error) {this.recordDegradation({ error, workspaceId: args.workspaceId });}}- private async dropCounterKeys({- workspaceId,- keys,- }: {- workspaceId: string;- keys: string[];- }): Promise<void> {- try {- await this.cacheLockService.withLock(- () => this.cacheStorage.mdel(keys),- buildStockWarmLockKey(workspaceId),- STOCK_WARM_LOCK_OPTIONS,- );- } catch (error) {- if (- !(error instanceof CacheLockException) ||- error.code !== CacheLockExceptionCode.LOCK_ACQUISITION_TIMEOUT- ) {- throw error;- }-- this.logger.warn(- `Dropping stock counters ${keys.join(', ')} without the warm lock: ${error.message}`,- );-- await this.cacheStorage.mdel(keys);- }- }-private async applyStockDelta({cost,- script,- buildArgs,+ buildArguments,...args}: StockArgs & {cost: StockCost;- script: CacheScript;- buildArgs: (counters: StockCounter[]) => string[];+ buildArguments: (amounts: number[]) => string[];}): Promise<void> {try {- const counters = (await this.buildCounters(args)).filter(- (counter) => (cost[counter.unit] ?? 0) > 0,+ const counters = uniqBy(+ await this.buildChargedCounters({ ...args, cost }),+ (counter) => counter.key,);- if (counters.length === 0) {- return;- }-- await this.cacheStorage.runScript({- script,- keys: counters.map((counter) => counter.key),- args: buildArgs(counters),+ await this.runWarmAndConsumeScript({+ counters,+ args: buildArguments(+ counters.map((counter) => cost[counter.unit] ?? 0),+ ),});} catch (error) {this.recordDegradation({ error, workspaceId: args.workspaceId });}}+ private async buildChargedCounters({+ cost,+ ...args+ }: StockArgs & { cost: StockCost }): Promise<StockCounter[]> {+ return (await this.buildCounters(args)).filter(+ (counter) => (cost[counter.unit] ?? 0) > 0,+ );+ }+private async buildCounters({workspaceId,resourceType,@@ -340,92 +290,101 @@ export class UsageLimitStockService {});}- private async readRemainings({- workspaceId,+ private async warmHeldValues({counters,computeUsedStock,}: {- workspaceId: string;counters: StockCounter[];computeUsedStock: ComputeUsedStock;- }): Promise<(number | null)[]> {- const remainings = await this.cacheStorage.mget<number>(- counters.map((counter) => counter.key),+ }): Promise<Map<string, number | null>> {+ const uniqueCounters = uniqBy(counters, (counter) => counter.key);++ const storedValues = await this.cacheStorage.mget<number>(+ uniqueCounters.map((counter) => counter.key),);- if (remainings.every(isDefined)) {- return remainings;- }+ const coldCounters = uniqueCounters.filter(+ (_, index) => !isDefined(storedValues[index]),+ );++ const warmValues = await this.fetchWarmValues({+ counters: coldCounters,+ computeUsedStock,+ });++ const warmedValues = await this.runWarmAndConsumeScript({+ counters: coldCounters,+ args: buildConsumeScriptArguments(+ warmValues.map((warmValue) => ({+ amount: 0,+ warm: isDefined(warmValue)+ ? { value: warmValue, ttl: STOCK_COUNTER_TTL_MS }+ : null,+ })),+ ),+ });- return this.warmColdCounters({ workspaceId, counters, computeUsedStock });+ const warmedValueByKey = new Map(+ coldCounters.map((counter, index) => [counter.key, warmedValues[index]]),+ );++ return new Map(+ uniqueCounters.map((counter, index) => [+ counter.key,+ storedValues[index] ?? warmedValueByKey.get(counter.key) ?? null,+ ]),+ );}- private async warmColdCounters({- workspaceId,+ private async fetchWarmValues({counters,computeUsedStock,}: {- workspaceId: string;counters: StockCounter[];computeUsedStock: ComputeUsedStock;- }): Promise<(number | null)[]> {- return this.cacheLockService.withLock(- async () => {- const remainings = await this.cacheStorage.mget<number>(- counters.map((counter) => counter.key),- );-- if (remainings.every(isDefined)) {- return remainings;- }-- const coldCounters = counters.filter(- (_, index) => !isDefined(remainings[index]),- );-- const scopeKeys = [- ...new Map(- coldCounters.map((counter) => [- buildStockScopeKey(counter),- counter,- ]),- ).entries(),- ];-- const usedByScope = new Map(- await Promise.all(- scopeKeys.map(- async ([scopeKey, counter]) =>- [- scopeKey,- await computeUsedStock({- spenderType: counter.spenderType,- spenderId: counter.spenderId,- }),- ] as const,- ),- ),- );-- const warmedEntries = buildStockWarmedEntries({- coldCounters,- usedByScope,- ttl: STOCK_COUNTER_TTL_MS,- });+ }): Promise<(number | undefined)[]> {+ const usedStockReadsByScope = new Map<+ string,+ ReturnType<ComputeUsedStock>+ >();++ return Promise.all(+ counters.map(async (counter) => {+ const scopeKey = buildStockScopeKey(counter);++ const usedStockRead =+ usedStockReadsByScope.get(scopeKey) ??+ computeUsedStock({+ spenderType: counter.spenderType,+ spenderId: counter.spenderId,+ });++ usedStockReadsByScope.set(scopeKey, usedStockRead);++ return (await usedStockRead)[counter.unit];+ }),+ );+ }- await this.cacheStorage.mset(warmedEntries);+ private async runWarmAndConsumeScript({+ counters,+ args,+ }: {+ counters: StockCounter[];+ args: string[];+ }): Promise<(number | null)[]> {+ if (counters.length === 0) {+ return [];+ }- const warmedValueByKey = new Map(- warmedEntries.map((entry) => [entry.key, entry.value]),- );+ const results = await this.cacheStorage.runScript<unknown[]>({+ script: WARM_AND_CONSUME_COUNTERS_SCRIPT,+ keys: counters.map((counter) => counter.key),+ args,+ });- return counters.map(- (counter, index) =>- remainings[index] ?? warmedValueByKey.get(counter.key) ?? null,- );- },- buildStockWarmLockKey(workspaceId),- STOCK_WARM_LOCK_OPTIONS,+ return results.map((result) =>+ typeof result === 'number' ? result : null,);}diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.tsindex e299989e481..ca922a30116 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit.service.ts@@ -172,7 +172,6 @@ export class UsageLimitService {);if (- isStockLimit(usageLimit) ||usageLimit.isInstanceOverride !== isOperator ||!isEqual(authorizedScope, scope)) {@@ -300,14 +299,9 @@ export class UsageLimitService {private async dropCounter(usageLimit: UsageLimitEntity): Promise<void> {if (isStockLimit(usageLimit)) {- return this.usageLimitStockService.dropStockCounters({+ return this.usageLimitStockService.dropLimitCounter({workspaceId: usageLimit.workspaceId,- resourceType: usageLimit.resourceType,- operationType: usageLimit.operationType,- spenderType: usageLimit.spenderType,- spenderId: usageLimit.spenderId,- unit: usageLimit.unit,- limitValue: usageLimit.limitValue,+ usageLimit,});}diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.module.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.module.tsindex 52511ae850d..3960dde637d 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.module.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/usage-limit.module.ts@@ -3,7 +3,6 @@ import { DiscoveryModule } from '@nestjs/core';import { TypeOrmModule } from '@nestjs/typeorm';import { ApiKeyEntity } from 'src/engine/core-modules/api-key/api-key.entity';-import { CacheLockModule } from 'src/engine/core-modules/cache-lock/cache-lock.module';import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';import { ThrottlerModule } from 'src/engine/core-modules/throttler/throttler.module';import { UsageLimitQuotaService } from 'src/engine/core-modules/usage-limit/services/usage-limit-quota.service';@@ -41,7 +40,6 @@ import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cacheWorkspaceCacheModule,PermissionsModule,DiscoveryModule,- CacheLockModule,MetricsModule,ThrottlerModule,UsageAnalyticsModule,diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-intra-workspace-stock-counter-keys.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-intra-workspace-stock-counter-keys.util.spec.tsnew file mode 100644index 00000000000..06aa818395e--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-intra-workspace-stock-counter-keys.util.spec.ts@@ -0,0 +1,62 @@+import { type FlatUsageLimit } from 'src/engine/core-modules/usage-limit/types/flat-usage-limit.type';+import { buildIntraWorkspaceStockCounterKeys } from 'src/engine/core-modules/usage-limit/utils/build-intra-workspace-stock-counter-keys.util';+import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';+import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';+import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';++const buildLimit = (overrides: Partial<FlatUsageLimit>): FlatUsageLimit => ({+ id: 'limit-1',+ resourceType: UsageResourceType.STORAGE,+ operationType: UsageOperationType.STORAGE_FILE,+ spenderType: 'application',+ spenderId: 'application-1',+ limitKind: 'stock',+ periodCount: 1,+ periodUnit: 'lifetime',+ unit: UsageUnit.BYTE,+ limitValue: 1_000,+ burstValue: null,+ isInstanceOverride: false,+ ...overrides,+});++describe('buildIntraWorkspaceStockCounterKeys', () => {+ it('keys every intra-workspace stock limit the entitlement turns enforced', () => {+ expect(+ buildIntraWorkspaceStockCounterKeys({+ workspaceId: 'workspace-1',+ limits: [+ buildLimit({}),+ buildLimit({+ id: 'limit-2',+ spenderId: 'application-2',+ unit: UsageUnit.FILE,+ }),+ ],+ }),+ ).toEqual([+ '{workspace-1}:stock-held:STORAGE:STORAGE_FILE:application:application-1:BYTE',+ '{workspace-1}:stock-held:STORAGE:STORAGE_FILE:application:application-2:FILE',+ ]);+ });++ it('leaves workspace-scoped stock limits, operator overrides and quotas alone', () => {+ expect(+ buildIntraWorkspaceStockCounterKeys({+ workspaceId: 'workspace-1',+ limits: [+ buildLimit({ spenderType: 'workspace', spenderId: '' }),+ buildLimit({ id: 'limit-2', isInstanceOverride: true }),+ buildLimit({+ id: 'limit-3',+ resourceType: UsageResourceType.AI,+ operationType: UsageOperationType.ALL,+ limitKind: 'quota',+ periodUnit: 'month',+ unit: UsageUnit.CREDIT,+ }),+ ],+ }),+ ).toEqual([]);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counter-key.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counter-key.util.spec.tsindex c69b889317e..874110d7362 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counter-key.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counter-key.util.spec.ts@@ -3,44 +3,19 @@ import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-opimport { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';-const WORKSPACE_ID = 'workspace-1';--const buildKey = (- overrides: Partial<Parameters<typeof buildStockCounterKey>[0]> = {},-) =>- buildStockCounterKey({- workspaceId: WORKSPACE_ID,- resourceType: UsageResourceType.STORAGE,- operationType: UsageOperationType.STORAGE_FILE,- spenderType: 'workspace',- spenderId: null,- unit: UsageUnit.BYTE,- limitValue: 1000,- ...overrides,- });-describe('buildStockCounterKey', () => {- it('hashes on the workspace so every counter of a workspace shares a slot', () => {- expect(buildKey()).toBe(- `{${WORKSPACE_ID}}:stock:STORAGE:STORAGE_FILE:workspace:-:BYTE:1000`,- );+ it('keys the held stock by its scope alone, under the workspace hash tag', () => {+ expect(+ buildStockCounterKey({+ workspaceId: 'workspace-1',+ counter: {+ resourceType: UsageResourceType.STORAGE,+ operationType: UsageOperationType.STORAGE_FILE,+ spenderType: 'workspace',+ spenderId: null,+ unit: UsageUnit.BYTE,+ },+ }),+ ).toBe('{workspace-1}:stock-held:STORAGE:STORAGE_FILE:workspace:-:BYTE');});-- it('names an absent spender rather than leaving the segment empty', () => {- expect(buildKey({ spenderType: 'application', spenderId: 'app-1' })).toBe(- `{${WORKSPACE_ID}}:stock:STORAGE:STORAGE_FILE:application:app-1:BYTE:1000`,- );- });-- it.each([- ['operationType', { operationType: UsageOperationType.CALL_RECORDING }],- ['unit', { unit: UsageUnit.FILE }],- ['spenderId', { spenderId: 'app-1' }],- ['limitValue', { limitValue: 2000 }],- ])(- 'gives two limits differing only by %s their own counter',- (_, differs) => {- expect(buildKey(differs)).not.toBe(buildKey());- },- );});diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counters.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counters.util.spec.tsindex 0e53dca2f57..17dfaa4f6eb 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counters.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-counters.util.spec.ts@@ -85,7 +85,7 @@ describe('buildStockCounters', () => {});expect(counters.map((counter) => counter.isDefault)).toEqual([false, true]);- expect(new Set(counters.map((counter) => counter.key)).size).toBe(2);+ expect(new Set(counters.map((counter) => counter.key)).size).toBe(1);});it.each([@@ -116,22 +116,6 @@ describe('buildStockCounters', () => {).toEqual([expect.objectContaining({ isDefault: true })]);});- it('keys a stored limit by its value so an edited limit warms afresh', () => {- const [before] = build({ limits: [buildLimit({ limitValue: 500 })] });- const [after] = build({ limits: [buildLimit({ limitValue: 2_000 })] });-- expect(before.key).not.toBe(after.key);- });-- it('keys the default by its value so a changed setting warms afresh', () => {- const [before] = build({ stockLimitDefaults: [buildDefault()] });- const [after] = build({- stockLimitDefaults: [buildDefault({ limitValue: 2_000 })],- });-- expect(before.key).not.toBe(after.key);- });-it('skips a default whose spender type the call does not carry', () => {expect(build({diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-exhausted-scope.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-exhausted-scope.util.spec.tsnew file mode 100644index 00000000000..73d461d9ada--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-exhausted-scope.util.spec.ts@@ -0,0 +1,24 @@+import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';+import { buildStockExhaustedScope } from 'src/engine/core-modules/usage-limit/utils/build-stock-exhausted-scope.util';+import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';+import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';+import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';++const COUNTER: StockCounter = {+ key: '{workspace-1}:stock-held:STORAGE:STORAGE_FILE:workspace:-:FILE',+ isDefault: false,+ limitValue: 10,+ unit: UsageUnit.FILE,+ resourceType: UsageResourceType.STORAGE,+ operationType: UsageOperationType.STORAGE_FILE,+ spenderType: 'workspace',+ spenderId: null,+};++describe('buildStockExhaustedScope', () => {+ it('reports what the held stock leaves under the cap', () => {+ expect(buildStockExhaustedScope({ counter: COUNTER, held: 7 })).toEqual(+ expect.objectContaining({ limitValue: 10, remaining: 3 }),+ );+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-warmed-entries.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-warmed-entries.util.spec.tsdeleted file mode 100644index 809b82026a0..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-stock-warmed-entries.util.spec.ts+++ /dev/null@@ -1,160 +0,0 @@-import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';-import { buildStockScopeKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-scope-key.util';-import { buildStockWarmedEntries } from 'src/engine/core-modules/usage-limit/utils/build-stock-warmed-entries.util';-import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';-import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';-import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';--const TTL = 1_000;--const buildCounter = (overrides: Partial<StockCounter> = {}): StockCounter => ({- key: 'stock:BYTE',- isDefault: false,- limitValue: 1_000,- unit: UsageUnit.BYTE,- resourceType: UsageResourceType.STORAGE,- operationType: UsageOperationType.STORAGE_FILE,- spenderType: 'workspace',- spenderId: null,- ...overrides,-});--describe('buildStockWarmedEntries', () => {- it('warms a counter to what the limit leaves over what is held', () => {- const counter = buildCounter();-- expect(- buildStockWarmedEntries({- coldCounters: [counter],- usedByScope: new Map([- [- buildStockScopeKey(counter),- { [UsageUnit.BYTE]: 400, [UsageUnit.FILE]: 2 },- ],- ]),- ttl: TTL,- }),- ).toEqual([{ key: 'stock:BYTE', value: 600, ttl: TTL }]);- });-- it('reads each counter against its own unit', () => {- const counter = buildCounter({- key: 'stock:FILE',- unit: UsageUnit.FILE,- limitValue: 5,- });-- expect(- buildStockWarmedEntries({- coldCounters: [counter],- usedByScope: new Map([- [- buildStockScopeKey(counter),- { [UsageUnit.BYTE]: 400, [UsageUnit.FILE]: 2 },- ],- ]),- ttl: TTL,- }),- ).toEqual([{ key: 'stock:FILE', value: 3, ttl: TTL }]);- });-- it('warms counters of different units on the same scope from one read', () => {- const byteCounter = buildCounter();- const fileCounter = buildCounter({- key: 'stock:FILE',- unit: UsageUnit.FILE,- limitValue: 5,- });-- expect(- buildStockWarmedEntries({- coldCounters: [byteCounter, fileCounter],- usedByScope: new Map([- [- buildStockScopeKey(byteCounter),- { [UsageUnit.BYTE]: 400, [UsageUnit.FILE]: 2 },- ],- ]),- ttl: TTL,- }),- ).toEqual([- { key: 'stock:BYTE', value: 600, ttl: TTL },- { key: 'stock:FILE', value: 3, ttl: TTL },- ]);- });-- it('leaves a counter cold when its unit was not counted', () => {- const counter = buildCounter({- key: 'stock:FILE',- unit: UsageUnit.FILE,- limitValue: 5,- });-- expect(- buildStockWarmedEntries({- coldCounters: [counter],- usedByScope: new Map([- [buildStockScopeKey(counter), { [UsageUnit.BYTE]: 400 }],- ]),- ttl: TTL,- }),- ).toEqual([]);- });-- it('warms a lowered limit into the red rather than clamping it', () => {- const counter = buildCounter({ limitValue: 500 });-- expect(- buildStockWarmedEntries({- coldCounters: [counter],- usedByScope: new Map([- [- buildStockScopeKey(counter),- { [UsageUnit.BYTE]: 900, [UsageUnit.FILE]: 4 },- ],- ]),- ttl: TTL,- }),- ).toEqual([{ key: 'stock:BYTE', value: -400, ttl: TTL }]);- });-- it('skips a counter whose scope was never counted', () => {- expect(- buildStockWarmedEntries({- coldCounters: [buildCounter()],- usedByScope: new Map(),- ttl: TTL,- }),- ).toEqual([]);- });-- it('warms each spender from its own scope', () => {- const workspaceCounter = buildCounter();- const applicationCounter = buildCounter({- key: 'stock:application:BYTE',- spenderType: 'application',- spenderId: 'application-1',- limitValue: 800,- });-- expect(- buildStockWarmedEntries({- coldCounters: [workspaceCounter, applicationCounter],- usedByScope: new Map([- [- buildStockScopeKey(workspaceCounter),- { [UsageUnit.BYTE]: 400, [UsageUnit.FILE]: 2 },- ],- [- buildStockScopeKey(applicationCounter),- { [UsageUnit.BYTE]: 100, [UsageUnit.FILE]: 1 },- ],- ]),- ttl: TTL,- }),- ).toEqual([- { key: 'stock:BYTE', value: 600, ttl: TTL },- { key: 'stock:application:BYTE', value: 700, ttl: TTL },- ]);- });-});diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/find-exhausted-stock-counter.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/find-exhausted-stock-counter.util.spec.tsdeleted file mode 100644index fa3e565c0ad..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/find-exhausted-stock-counter.util.spec.ts+++ /dev/null@@ -1,87 +0,0 @@-import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';-import { findExhaustedStockCounter } from 'src/engine/core-modules/usage-limit/utils/find-exhausted-stock-counter.util';-import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';-import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';-import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';--const buildCounter = (overrides: Partial<StockCounter> = {}): StockCounter => ({- key: 'stock:BYTE',- isDefault: false,- limitValue: 1_000,- unit: UsageUnit.BYTE,- resourceType: UsageResourceType.STORAGE,- operationType: UsageOperationType.STORAGE_FILE,- spenderType: 'workspace',- spenderId: null,- ...overrides,-});--describe('findExhaustedStockCounter', () => {- it('admits a cost that exactly consumes the remainder', () => {- expect(- findExhaustedStockCounter({- counters: [buildCounter()],- remainings: [250],- cost: { [UsageUnit.BYTE]: 250 },- }),- ).toBeNull();- });-- it('reports the counter a cost overruns, with what was left', () => {- const counter = buildCounter();-- expect(- findExhaustedStockCounter({- counters: [counter],- remainings: [250],- cost: { [UsageUnit.BYTE]: 251 },- }),- ).toEqual({ counter, remaining: 250 });- });-- it('admits a counter the cost does not touch', () => {- expect(- findExhaustedStockCounter({- counters: [buildCounter({ unit: UsageUnit.FILE })],- remainings: [0],- cost: { [UsageUnit.BYTE]: 400 },- }),- ).toBeNull();- });-- it('admits a counter that could not be read', () => {- expect(- findExhaustedStockCounter({- counters: [buildCounter()],- remainings: [null],- cost: { [UsageUnit.BYTE]: 400 },- }),- ).toBeNull();- });-- it('refuses on a counter already in the red', () => {- expect(- findExhaustedStockCounter({- counters: [buildCounter()],- remainings: [-10],- cost: { [UsageUnit.BYTE]: 1 },- }),- ).toMatchObject({ remaining: -10 });- });-- it('keeps looking past a counter the cost fits in', () => {- const fileCounter = buildCounter({- key: 'stock:FILE',- unit: UsageUnit.FILE,- limitValue: 5,- });-- expect(- findExhaustedStockCounter({- counters: [buildCounter(), fileCounter],- remainings: [500, 0],- cost: { [UsageUnit.BYTE]: 400, [UsageUnit.FILE]: 1 },- }),- ).toEqual({ counter: fileCounter, remaining: 0 });- });-});diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-intra-workspace-stock-counter-keys.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-intra-workspace-stock-counter-keys.util.tsnew file mode 100644index 00000000000..ec9c82ad117--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-intra-workspace-stock-counter-keys.util.ts@@ -0,0 +1,25 @@+import { type FlatUsageLimit } from 'src/engine/core-modules/usage-limit/types/flat-usage-limit.type';+import { buildStockCounter } from 'src/engine/core-modules/usage-limit/utils/build-stock-counter.util';+import { findEnforceableLimits } from 'src/engine/core-modules/usage-limit/utils/find-enforceable-limits.util';+import { isStockLimit } from 'src/engine/core-modules/usage-limit/utils/is-stock-limit.util';++export const buildIntraWorkspaceStockCounterKeys = ({+ workspaceId,+ limits,+}: {+ workspaceId: string;+ limits: FlatUsageLimit[];+}): string[] => {+ const stockLimits = limits.filter(isStockLimit);++ const limitIdsEnforcedWithoutEntitlement = new Set(+ findEnforceableLimits({+ limits: stockLimits,+ isIntraWorkspaceLimitEntitled: false,+ }).map((limit) => limit.id),+ );++ return stockLimits+ .filter((limit) => !limitIdsEnforcedWithoutEntitlement.has(limit.id))+ .map((limit) => buildStockCounter({ workspaceId, limit }).key);+};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-release-script-arguments.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-release-script-arguments.util.tsnew file mode 100644index 00000000000..1ce271c8ab0--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-release-script-arguments.util.ts@@ -0,0 +1,13 @@+import { buildConsumeScriptArguments } from 'src/engine/core-modules/usage-limit/utils/build-consume-script-arguments.util';++export const buildReleaseScriptArguments = (amounts: number[]): string[] => {+ const [, warmValues, warmTtls] = buildConsumeScriptArguments(+ amounts.map((amount) => ({ amount, warm: null })),+ );++ return [+ JSON.stringify(amounts.map((amount) => -amount)),+ warmValues,+ warmTtls,+ ];+};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util.tsindex be16cbc1748..b125946a98a 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util.ts@@ -1,30 +1,17 @@import { isNonEmptyString } from '@sniptt/guards';-import { type SpenderType } from 'src/engine/core-modules/usage-limit/types/spender-type.type';-import { type UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';-import { type UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';-import { type UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';+import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';const ABSENT = '-';-// The counter holds `limitValue - used`, so it only means anything for the value-// it was warmed against. Keying on that value retires the counter as soon as the-// limit changes, instead of leaving a remaining that outlives its limit.export const buildStockCounterKey = ({workspaceId,- resourceType,- operationType,- spenderType,- spenderId,- unit,- limitValue,+ counter,}: {workspaceId: string;- resourceType: UsageResourceType;- operationType: UsageOperationType;- spenderType: SpenderType;- spenderId?: string | null;- unit: UsageUnit;- limitValue: number;+ counter: Pick<+ StockCounter,+ 'resourceType' | 'operationType' | 'spenderType' | 'spenderId' | 'unit'+ >;}): string =>- `{${workspaceId}}:stock:${resourceType}:${operationType}:${spenderType}:${isNonEmptyString(spenderId) ? spenderId : ABSENT}:${unit}:${limitValue}`;+ `{${workspaceId}}:stock-held:${counter.resourceType}:${counter.operationType}:${counter.spenderType}:${isNonEmptyString(counter.spenderId) ? counter.spenderId : ABSENT}:${counter.unit}`;diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter.util.tsindex 2cf98b57af3..558a75ce2a4 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter.util.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-counter.util.ts@@ -9,21 +9,16 @@ export const buildStockCounter = ({}: {workspaceId: string;limit: FlatStockLimit;-}): StockCounter => ({- key: buildStockCounterKey({- workspaceId,+}): StockCounter => {+ const counter: Omit<StockCounter, 'key'> = {+ isDefault: false,+ limitValue: limit.limitValue,+ unit: limit.unit,resourceType: limit.resourceType,operationType: limit.operationType,spenderType: limit.spenderType,- spenderId: limit.spenderId,- unit: limit.unit,- limitValue: limit.limitValue,- }),- isDefault: false,- limitValue: limit.limitValue,- unit: limit.unit,- resourceType: limit.resourceType,- operationType: limit.operationType,- spenderType: limit.spenderType,- spenderId: normalizeSpenderId(limit.spenderId),-});+ spenderId: normalizeSpenderId(limit.spenderId),+ };++ return { ...counter, key: buildStockCounterKey({ workspaceId, counter }) };+};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter-key.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter-key.util.tsdeleted file mode 100644index 8a0ffef0ecb..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter-key.util.ts+++ /dev/null@@ -1,22 +0,0 @@-import { type SpenderType } from 'src/engine/core-modules/usage-limit/types/spender-type.type';-import { buildStockCounterKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util';-import { type UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';-import { type UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';-import { type UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';--export const buildStockDefaultCounterKey = ({- workspaceId,- resourceType,- operationType,- spenderType,- unit,- limitValue,-}: {- workspaceId: string;- resourceType: UsageResourceType;- operationType: UsageOperationType;- spenderType: SpenderType;- unit: UsageUnit;- limitValue: number;-}): string =>- `${buildStockCounterKey({ workspaceId, resourceType, operationType, spenderType, unit, limitValue })}:default`;diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter.util.tsindex de0727cd342..25ba6f231a6 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter.util.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-default-counter.util.ts@@ -1,7 +1,7 @@import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';import { type StockLimitDefault } from 'src/engine/core-modules/usage-limit/types/stock-limit-default.type';import { type StockResourceType } from 'src/engine/core-modules/usage-limit/types/stock-resource-type.type';-import { buildStockDefaultCounterKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-default-counter-key.util';+import { buildStockCounterKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-counter-key.util';import { type UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';export const buildStockDefaultCounter = ({@@ -14,20 +14,16 @@ export const buildStockDefaultCounter = ({resourceType: StockResourceType;operationType: UsageOperationType;stockLimitDefault: StockLimitDefault;-}): StockCounter => ({- key: buildStockDefaultCounterKey({- workspaceId,+}): StockCounter => {+ const counter: Omit<StockCounter, 'key'> = {+ isDefault: true,+ limitValue: stockLimitDefault.limitValue,+ unit: stockLimitDefault.unit,resourceType,operationType,spenderType: stockLimitDefault.spenderType,- unit: stockLimitDefault.unit,- limitValue: stockLimitDefault.limitValue,- }),- isDefault: true,- limitValue: stockLimitDefault.limitValue,- unit: stockLimitDefault.unit,- resourceType,- operationType,- spenderType: stockLimitDefault.spenderType,- spenderId: null,-});+ spenderId: null,+ };++ return { ...counter, key: buildStockCounterKey({ workspaceId, counter }) };+};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-exhausted-scope.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-exhausted-scope.util.tsindex a01dfa045ba..ce30a0849d5 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-exhausted-scope.util.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-exhausted-scope.util.ts@@ -3,10 +3,10 @@ import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stoexport const buildStockExhaustedScope = ({counter,- remaining,+ held,}: {counter: StockCounter;- remaining: number;+ held: number;}): ExhaustedScope => ({resourceType: counter.resourceType,limitKind: 'stock',@@ -16,7 +16,7 @@ export const buildStockExhaustedScope = ({operationType: counter.operationType,unit: counter.unit,limitValue: counter.limitValue,- remaining,+ remaining: counter.limitValue - held,periodCount: null,periodUnit: null,retryAfterMs: 0,diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-warm-lock-key.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-warm-lock-key.util.tsdeleted file mode 100644index b192b04b147..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-warm-lock-key.util.ts+++ /dev/null@@ -1,2 +0,0 @@-export const buildStockWarmLockKey = (workspaceId: string): string =>- `usage-stock-warm:${workspaceId}`;diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-warmed-entries.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-warmed-entries.util.tsdeleted file mode 100644index aea88f02044..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-stock-warmed-entries.util.ts+++ /dev/null@@ -1,22 +0,0 @@-import { isDefined } from 'twenty-shared/utils';--import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';-import { buildStockScopeKey } from 'src/engine/core-modules/usage-limit/utils/build-stock-scope-key.util';-import { type UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';--export const buildStockWarmedEntries = ({- coldCounters,- usedByScope,- ttl,-}: {- coldCounters: StockCounter[];- usedByScope: Map<string, Partial<Record<UsageUnit, number>>>;- ttl: number;-}): { key: string; value: number; ttl: number }[] =>- coldCounters.flatMap((counter) => {- const used = usedByScope.get(buildStockScopeKey(counter))?.[counter.unit];-- return isDefined(used)- ? [{ key: counter.key, value: counter.limitValue - used, ttl }]- : [];- });diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/find-exhausted-stock-counter.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/find-exhausted-stock-counter.util.tsdeleted file mode 100644index 4ffecabf595..00000000000--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/find-exhausted-stock-counter.util.ts+++ /dev/null@@ -1,24 +0,0 @@-import { isDefined } from 'twenty-shared/utils';--import { type StockCost } from 'src/engine/core-modules/usage-limit/types/stock-cost.type';-import { type StockCounter } from 'src/engine/core-modules/usage-limit/types/stock-counter.type';--export const findExhaustedStockCounter = ({- counters,- remainings,- cost,-}: {- counters: StockCounter[];- remainings: (number | null)[];- cost: StockCost;-}): { counter: StockCounter; remaining: number } | null => {- for (const [index, counter] of counters.entries()) {- const remaining = remainings[index];-- if (isDefined(remaining) && remaining < (cost[counter.unit] ?? 0)) {- return { counter, remaining };- }- }-- return null;-};diff --git a/packages/twenty-server/test/integration/usage-limit/record-stock-limit.integration-spec.ts b/packages/twenty-server/test/integration/usage-limit/record-stock-limit.integration-spec.tsindex b659e6e5c9e..8236c25f790 100644--- a/packages/twenty-server/test/integration/usage-limit/record-stock-limit.integration-spec.ts+++ b/packages/twenty-server/test/integration/usage-limit/record-stock-limit.integration-spec.ts@@ -130,7 +130,7 @@ describe('Record stock limit', () => {};const dropStockCounter = () =>- dropKeys(`*{${SEED_APPLE_WORKSPACE_ID}}:stock:RECORD:*`);+ dropKeys(`*{${SEED_APPLE_WORKSPACE_ID}}:stock-held:RECORD:*`);const refreshUsageLimitsCache = () =>getAppProviderByClassName<WorkspaceCacheService>(diff --git a/packages/twenty-server/test/integration/usage-limit/storage-stock-limit.integration-spec.ts b/packages/twenty-server/test/integration/usage-limit/storage-stock-limit.integration-spec.tsnew file mode 100644index 00000000000..cc55cf8cafb--- /dev/null+++ b/packages/twenty-server/test/integration/usage-limit/storage-stock-limit.integration-spec.ts@@ -0,0 +1,163 @@+import { createFileUploadMutation } from 'test/integration/graphql/utils/upload-file-with-direct-upload.util';+import { makeMetadataApiRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';+import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';+import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';++import { createClient } from 'redis';+import { FileFolder } from 'twenty-shared/types';+import { type Repository } from 'typeorm';+import { v4 } from 'uuid';++import { type FileStorageService } from 'src/engine/core-modules/file-storage/services/file-storage.service';+import { UsageLimitEntity } from 'src/engine/core-modules/usage-limit/usage-limit.entity';+import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';+import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';+import { UsageUnit } from 'src/engine/core-modules/usage/enums/usage-unit.enum';+import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';+import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';++const FILE_FOLDER = FileFolder.Workflow;++describe('Storage stock limit', () => {+ let usageLimitRepository: Repository<UsageLimitEntity>;+ let redis: Awaited<ReturnType<typeof createClient>>;+ let fileStorageService: FileStorageService;+ let applicationUniversalIdentifier: string;+ const writtenResourcePaths = new Set<string>();++ const refreshUsageLimitsCache = () =>+ getAppProviderByClassName<WorkspaceCacheService>(+ 'WorkspaceCacheService',+ ).invalidateAndRecompute(SEED_APPLE_WORKSPACE_ID, ['usageLimits']);++ const dropStockCounters = async () => {+ const keys = await redis.keys(+ `*{${SEED_APPLE_WORKSPACE_ID}}:stock-held:STORAGE:*`,+ );++ if (keys.length > 0) {+ await redis.del(keys);+ }+ };++ const deleteStorageStockLimits = async () => {+ await usageLimitRepository.delete({+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ resourceType: UsageResourceType.STORAGE,+ });+ await refreshUsageLimitsCache();+ };++ const writeFile = ({+ resourcePath,+ content,+ }: {+ resourcePath: string;+ content: string;+ }) => {+ writtenResourcePaths.add(resourcePath);++ return fileStorageService.writeFile({+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ applicationUniversalIdentifier,+ fileFolder: FILE_FOLDER,+ resourcePath,+ sourceFile: content,+ settings: { isTemporaryFile: false, toDelete: false },+ });+ };++ const deleteFile = (resourcePath: string) =>+ fileStorageService.deleteFile({+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ applicationUniversalIdentifier,+ fileFolder: FILE_FOLDER,+ resourcePath,+ });++ const createFileUpload = (size: number) =>+ makeMetadataApiRequest({+ query: createFileUploadMutation,+ variables: { filename: 'stock-limit.txt', size, fileFolder: 'Workflow' },+ });++ const expectRefused = (response: { body: { errors?: unknown[] } }) => {+ expect(+ (response.body.errors?.[0] as { extensions?: { subCode?: string } })+ ?.extensions?.subCode,+ ).toBe('STOCK_EXHAUSTED');+ };++ beforeAll(async () => {+ usageLimitRepository =+ getCoreRepository<UsageLimitEntity>(UsageLimitEntity);+ redis = await createClient({ url: process.env.REDIS_URL }).connect();+ fileStorageService =+ getAppProviderByClassName<FileStorageService>('FileStorageService');++ const [workspaceCustomApplication]: { universalIdentifier: string }[] =+ await global.testDataSource.query(+ `SELECT application."universalIdentifier"+ FROM core."workspace" workspace+ JOIN core."application" application+ ON application.id = workspace."workspaceCustomApplicationId"+ WHERE workspace.id = $1`,+ [SEED_APPLE_WORKSPACE_ID],+ );++ applicationUniversalIdentifier =+ workspaceCustomApplication.universalIdentifier;++ await deleteStorageStockLimits();+ });++ beforeEach(async () => {+ await dropStockCounters();+ });++ afterEach(async () => {+ await deleteStorageStockLimits();++ for (const resourcePath of writtenResourcePaths) {+ await deleteFile(resourcePath);+ }++ writtenResourcePaths.clear();++ await dropStockCounters();+ });++ afterAll(async () => {+ await redis.quit();+ });++ it('admits an overwrite that grows a file while over the file cap', async () => {+ const resourcePath = `stock-limit/${v4()}.txt`;++ await writeFile({ resourcePath, content: 'a' });+ await writeFile({+ resourcePath: `stock-limit/${v4()}.txt`,+ content: 'a',+ });+ await usageLimitRepository.save({+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ resourceType: UsageResourceType.STORAGE,+ operationType: UsageOperationType.STORAGE_FILE,+ spenderType: 'workspace',+ spenderId: '',+ limitKind: 'stock',+ periodCount: 1,+ periodUnit: 'lifetime',+ unit: UsageUnit.FILE,+ limitValue: 1,+ burstValue: null,+ });+ await refreshUsageLimitsCache();++ expectRefused(await createFileUpload(1));++ await expect(+ writeFile({ resourcePath, content: 'a longer content' }),+ ).resolves.toBeDefined();+ });+});