Skip to content

The open alternative to Salesforce, designed for AI.

fix(usage-limit): key stock counters by their limit value (#26275)

Etienne6e1db2931564
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.ts
index 19a74b1ccd7..929c615b9d0 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
@@ -16,7 +16,6 @@ import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.
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 { STOCK_METERS } from 'src/engine/core-modules/usage-limit/constants/usage-meters.constant';
import {
UsageLimitException,
UsageLimitExceptionCode,
@@ -27,6 +26,7 @@ import { type SpenderType } from 'src/engine/core-modules/usage-limit/types/spen
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 StockMeter } from 'src/engine/core-modules/usage-limit/types/stock-meter.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 { buildStockCounters } from 'src/engine/core-modules/usage-limit/utils/build-stock-counters.util';
@@ -148,14 +148,18 @@ export class UsageLimitStockService {
operationType,
spenderType,
spenderId,
+ meter,
+ limitValue,
}: {
workspaceId: string;
resourceType: UsageResourceType;
operationType: UsageOperationType;
spenderType: SpenderType;
- spenderId: string;
+ spenderId: string | null;
+ meter: StockMeter;
+ limitValue: number;
}) {
- const keys = STOCK_METERS.map((meter) =>
+ const keys = [
buildStockCounterKey({
workspaceId,
resourceType,
@@ -163,8 +167,9 @@ export class UsageLimitStockService {
spenderType,
spenderId,
meter,
+ limitValue,
}),
- );
+ ];
if (isStockResourceType(resourceType)) {
keys.push(
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.ts
index a3cda6b8cad..188edda6529 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
@@ -19,6 +19,7 @@ import { type SpenderType } from 'src/engine/core-modules/usage-limit/types/spen
import { UsageLimitEntity } from 'src/engine/core-modules/usage-limit/usage-limit.entity';
import { buildUsageLimitScope } from 'src/engine/core-modules/usage-limit/utils/build-usage-limit-scope.util';
import { isIntraWorkspaceScoped } from 'src/engine/core-modules/usage-limit/utils/is-intra-workspace-scoped.util';
+import { isStockLimit } from 'src/engine/core-modules/usage-limit/utils/is-stock-limit.util';
import { validateUsageLimitAgainstDefinition } from 'src/engine/core-modules/usage-limit/utils/validate-usage-limit-against-definition.util';
import { validateUsageLimitAgainstKindRule } from 'src/engine/core-modules/usage-limit/utils/validate-usage-limit-against-kind-rule.util';
import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
@@ -135,7 +136,7 @@ export class UsageLimitService {
{ where: { id: usageLimit.id } },
);
- // counters are keyed by scope, so a scope change leaves two of them to rewarm
+ // counters are keyed by scope and value, so an edit leaves two of them behind
await this.dropCounter(usageLimit);
await this.dropCounter(updatedUsageLimit);
@@ -241,13 +242,15 @@ export class UsageLimitService {
}
private async dropCounter(usageLimit: UsageLimitEntity): Promise<void> {
- if (usageLimit.limitKind === 'stock') {
+ if (isStockLimit(usageLimit)) {
return this.usageLimitStockService.dropStockCounters({
workspaceId: usageLimit.workspaceId,
resourceType: usageLimit.resourceType,
operationType: usageLimit.operationType,
spenderType: usageLimit.spenderType,
spenderId: usageLimit.spenderId,
+ meter: usageLimit.meter,
+ limitValue: usageLimit.limitValue,
});
}
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.ts
index 24f02766c3d..57668ddd508 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
@@ -14,19 +14,20 @@ const buildKey = (
spenderType: 'workspace',
spenderId: null,
meter: 'bytes',
+ 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:-:bytes`,
+ `{${WORKSPACE_ID}}:stock:STORAGE:STORAGE_FILE:workspace:-:bytes:1000`,
);
});
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:bytes`,
+ `{${WORKSPACE_ID}}:stock:STORAGE:STORAGE_FILE:application:app-1:bytes:1000`,
);
});
@@ -34,6 +35,7 @@ describe('buildStockCounterKey', () => {
['operationType', { operationType: UsageOperationType.CALL_RECORDING }],
['meter', { meter: 'quantity' as const }],
['spenderId', { spenderId: 'app-1' }],
+ ['limitValue', { limitValue: 2000 }],
])(
'gives two limits differing only by %s their own counter',
(_, differs) => {
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.ts
index e7c99cb83f9..f88d8aff9f2 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
@@ -114,6 +114,13 @@ 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({
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.ts
index 12f5e627063..8ce7e1726ca 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
@@ -7,6 +7,9 @@ import { type UsageResourceType } from 'src/engine/core-modules/usage/enums/usag
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,
@@ -14,6 +17,7 @@ export const buildStockCounterKey = ({
spenderType,
spenderId,
meter,
+ limitValue,
}: {
workspaceId: string;
resourceType: UsageResourceType;
@@ -21,5 +25,6 @@ export const buildStockCounterKey = ({
spenderType: SpenderType;
spenderId?: string | null;
meter: StockMeter;
+ limitValue: number;
}): string =>
- `{${workspaceId}}:stock:${resourceType}:${operationType}:${spenderType}:${isNonEmptyString(spenderId) ? spenderId : ABSENT}:${meter}`;
+ `{${workspaceId}}:stock:${resourceType}:${operationType}:${spenderType}:${isNonEmptyString(spenderId) ? spenderId : ABSENT}:${meter}:${limitValue}`;
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.ts
index cff7d5586f5..daabe358d1a 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
@@ -17,6 +17,7 @@ export const buildStockCounter = ({
spenderType: limit.spenderType,
spenderId: limit.spenderId,
meter: limit.meter,
+ limitValue: limit.limitValue,
}),
isDefault: false,
limitValue: limit.limitValue,
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.ts
index 180b3cede75..c27ff6da002 100644
--- 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.ts
@@ -19,4 +19,4 @@ export const buildStockDefaultCounterKey = ({
meter: StockMeter;
limitValue: number;
}): string =>
- `${buildStockCounterKey({ workspaceId, resourceType, operationType, spenderType, meter })}:default:${limitValue}`;
+ `${buildStockCounterKey({ workspaceId, resourceType, operationType, spenderType, meter, limitValue })}:default`;