Skip to content

Commit 5c729a4

Browse files
committed
refactor(webapp): move the mollifier-globally-enabled check behind a DI hook
The previous commit added a perf short-circuit at the call site that read `env.TRIGGER_MOLLIFIER_ENABLED` directly. That broke three mollifier integration tests in CI: the tests inject a custom `evaluateGate` via the existing DI seam expecting the buffer-write branch to be reached, but CI has no `.env` (the `apps/webapp/.env` symlink target is absent), the Zod default `"0"` wins, the call site short-circuits to `null` before the injected gate runs, and `buffer.accepted` stays empty. Make the global-enabled check itself injectable: - New constructor opt `isMollifierGloballyEnabled?: () => boolean`, defaulting to `() => env.TRIGGER_MOLLIFIER_ENABLED === "1"`. Each DI hook now represents one decision (gate, buffer, global-enabled), so a test that wants the buffer-write branch reached can inject `isMollifierGloballyEnabled: () => true` alongside its custom gate. - Call site now reads `this.isMollifierGloballyEnabled()` instead of `env.TRIGGER_MOLLIFIER_ENABLED` directly. In production, with no DI override, the default closure resolves `env` exactly once per call just as before — same perf win when the flag is off. - All six mollifier DI injection sites in triggerTask.test.ts now also pass `isMollifierGloballyEnabled: () => true` so the tests' DI surface matches the new contract regardless of CI env state.
1 parent 5255c47 commit 5c729a4

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

apps/webapp/app/runEngine/services/triggerTask.server.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ export class RunEngineTriggerTaskService {
7373
private readonly metadataMaximumSize: number;
7474
// Mollifier hooks are DI'd so tests can drive the call-site's mollify branch
7575
// deterministically (stub the gate to return mollify, inject a real or fake
76-
// buffer). In production both default to the live module-level singletons.
76+
// buffer, force the global-enabled predicate to true so the call site
77+
// doesn't short-circuit on an unset env). In production all three default
78+
// to the live module-level singletons + env read.
7779
private readonly evaluateGate: MollifierEvaluateGate;
7880
private readonly getMollifierBuffer: MollifierGetBuffer;
81+
private readonly isMollifierGloballyEnabled: () => boolean;
7982

8083
constructor(opts: {
8184
prisma: PrismaClientOrTransaction;
@@ -90,6 +93,7 @@ export class RunEngineTriggerTaskService {
9093
triggerRacepointSystem?: TriggerRacepointSystem;
9194
evaluateGate?: MollifierEvaluateGate;
9295
getMollifierBuffer?: MollifierGetBuffer;
96+
isMollifierGloballyEnabled?: () => boolean;
9397
}) {
9498
this.prisma = opts.prisma;
9599
this.engine = opts.engine;
@@ -103,6 +107,8 @@ export class RunEngineTriggerTaskService {
103107
this.triggerRacepointSystem = opts.triggerRacepointSystem ?? new NoopTriggerRacepointSystem();
104108
this.evaluateGate = opts.evaluateGate ?? defaultEvaluateGate;
105109
this.getMollifierBuffer = opts.getMollifierBuffer ?? defaultGetMollifierBuffer;
110+
this.isMollifierGloballyEnabled =
111+
opts.isMollifierGloballyEnabled ?? (() => env.TRIGGER_MOLLIFIER_ENABLED === "1");
106112
}
107113

108114
public async call({
@@ -342,17 +348,19 @@ export class RunEngineTriggerTaskService {
342348
// GateInputs allocation, the deps spread inside `evaluateGate`, and
343349
// the `mollifier.decisions{outcome=pass_through}` OTel increment on
344350
// every trigger — `triggerTask` is the highest-throughput code path
345-
// in the system. When the flag is on, behaviour is unchanged.
346-
const mollifierOutcome: GateOutcome | null =
347-
env.TRIGGER_MOLLIFIER_ENABLED === "1"
348-
? await this.evaluateGate({
349-
envId: environment.id,
350-
orgId: environment.organizationId,
351-
taskId,
352-
orgFeatureFlags:
353-
(environment.organization.featureFlags as Record<string, unknown> | null) ?? null,
354-
})
355-
: null;
351+
// in the system. The check goes through a DI'd predicate so unit
352+
// tests that inject a custom `evaluateGate` can also override the
353+
// gate-on check (the default reads `env.TRIGGER_MOLLIFIER_ENABLED`,
354+
// which is "0" in CI where no .env file is present).
355+
const mollifierOutcome: GateOutcome | null = this.isMollifierGloballyEnabled()
356+
? await this.evaluateGate({
357+
envId: environment.id,
358+
orgId: environment.organizationId,
359+
taskId,
360+
orgFeatureFlags:
361+
(environment.organization.featureFlags as Record<string, unknown> | null) ?? null,
362+
})
363+
: null;
356364

357365
try {
358366
return await this.traceEventConcern.traceRun(

apps/webapp/test/engine/triggerTask.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ describe("RunEngineTriggerTaskService", () => {
12461246
metadataMaximumSize: 1024 * 1024,
12471247
evaluateGate: evaluateGateSpy,
12481248
getMollifierBuffer: () => buffer as never,
1249+
isMollifierGloballyEnabled: () => true,
12491250
});
12501251

12511252
await expect(
@@ -1309,6 +1310,7 @@ describe("RunEngineTriggerTaskService", () => {
13091310
metadataMaximumSize: 1024 * 1024,
13101311
evaluateGate: async () => ({ action: "mollify", decision: trippedDecision }),
13111312
getMollifierBuffer: () => buffer as never,
1313+
isMollifierGloballyEnabled: () => true,
13121314
});
13131315

13141316
const result = await triggerTaskService.call({
@@ -1376,6 +1378,7 @@ describe("RunEngineTriggerTaskService", () => {
13761378
metadataMaximumSize: 1024 * 1024,
13771379
evaluateGate: async () => ({ action: "pass_through" }),
13781380
getMollifierBuffer: getBufferSpy,
1381+
isMollifierGloballyEnabled: () => true,
13791382
});
13801383

13811384
const result = await triggerTaskService.call({
@@ -1475,6 +1478,7 @@ describe("RunEngineTriggerTaskService", () => {
14751478
},
14761479
}),
14771480
getMollifierBuffer: () => buffer as never,
1481+
isMollifierGloballyEnabled: () => true,
14781482
});
14791483

14801484
await expect(
@@ -1581,6 +1585,7 @@ describe("RunEngineTriggerTaskService", () => {
15811585
metadataMaximumSize: 1024 * 1024,
15821586
evaluateGate: evaluateGateSpy,
15831587
getMollifierBuffer: () => buffer as never,
1588+
isMollifierGloballyEnabled: () => true,
15841589
});
15851590

15861591
const cached = await mollifierService.call({
@@ -1709,6 +1714,7 @@ describe("RunEngineTriggerTaskService", () => {
17091714
},
17101715
}),
17111716
getMollifierBuffer: () => buffer as never,
1717+
isMollifierGloballyEnabled: () => true,
17121718
});
17131719

17141720
const debounced = await mollifierService.call({

0 commit comments

Comments
 (0)