(organizationId: string)
| 230 | }); |
| 231 | |
| 232 | const decide = (organizationId: string): Effect.Effect<GateDecision> => |
| 233 | Effect.suspend(() => { |
| 234 | const windowId = Math.floor(now() / windowMs); |
| 235 | return increment(organizationId, windowId).pipe( |
| 236 | Effect.timeoutOrElse({ |
| 237 | duration: `${timeoutMs} millis`, |
| 238 | orElse: () => Effect.fail(new RateLimitCheckTimeoutError({ timeoutMs })), |
| 239 | }), |
| 240 | Effect.flatMap((count): Effect.Effect<GateDecision> => { |
| 241 | // Under the cap: no exemption lookup, no extra I/O. |
| 242 | if (count <= limit) return Effect.succeed({ blocked: false }); |
| 243 | return Effect.gen(function* () { |
| 244 | if (yield* resolveExemption(organizationId)) { |
| 245 | return { blocked: false } as const satisfies GateDecision; |
| 246 | } |
| 247 | // The only record that the backstop fired. A blocked execution is |
| 248 | // never usage-tracked (the gate short-circuits before the tracker) |
| 249 | // and deliberately not sent to Sentry — a backstop stopping |
| 250 | // runaway automation is expected, not exceptional — so without |
| 251 | // this line a blocked org is invisible outside a bug report, which |
| 252 | // is how the 2026-08-18 block went unnoticed until a customer |
| 253 | // sent a screenshot. |
| 254 | yield* Effect.sync(() => { |
| 255 | console.warn( |
| 256 | `[rate-limit] blocked execution for ${organizationId}: ${count} > ${limit} in window ${windowId}`, |
| 257 | ); |
| 258 | }); |
| 259 | return { |
| 260 | blocked: true, |
| 261 | error: new ExecutionRateLimitExceededError({ |
| 262 | organizationId, |
| 263 | message: RATE_LIMIT_BLOCKED_MESSAGE, |
| 264 | }), |
| 265 | } as const satisfies GateDecision; |
| 266 | }); |
| 267 | }), |
| 268 | // FAIL OPEN: the backstop must never block executions because its |
| 269 | // counter is unreachable or slow. |
| 270 | Effect.catch((error: unknown) => |
| 271 | Effect.gen(function* () { |
| 272 | yield* Effect.sync(() => { |
| 273 | console.warn("[rate-limit] execution rate limit check failed open:", error); |
| 274 | }); |
| 275 | yield* captureCauseEffect(error); |
| 276 | return { blocked: false } as const satisfies GateDecision; |
| 277 | }), |
| 278 | ), |
| 279 | ); |
| 280 | }); |
| 281 | |
| 282 | return { |
| 283 | decorate: (organizationId, engine) => withPreExecutionGate(engine, decide(organizationId)), |
no test coverage detected