MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / decide

Function decide

apps/cloud/src/engine/execution-rate-limit.ts:314–403  ·  view source on GitHub ↗
(organizationId: string)

Source from the content-addressed store, hash-verified

312 });
313
314 const decide = (organizationId: string): Effect.Effect<GateDecision> =>
315 Effect.suspend(() => {
316 const windowId = Math.floor(now() / windowMs);
317 return increment(organizationId, windowId).pipe(
318 Effect.timeoutOrElse({
319 duration: `${timeoutMs} millis`,
320 orElse: () => Effect.fail(new RateLimitCheckTimeoutError({ timeoutMs })),
321 }),
322 Effect.flatMap((count): Effect.Effect<GateDecision> => {
323 // Under the cap: no exemption lookup, no extra I/O.
324 if (count <= limit)
325 return Effect.as(
326 Effect.annotateCurrentSpan({
327 "rate_limit.count": count,
328 "rate_limit.blocked": false,
329 "rate_limit.check.failed_open": false,
330 }),
331 { blocked: false },
332 );
333 return Effect.gen(function* () {
334 yield* Effect.annotateCurrentSpan({
335 "rate_limit.count": count,
336 "rate_limit.check.failed_open": false,
337 });
338 if (yield* resolveExemption(organizationId)) {
339 yield* Effect.annotateCurrentSpan({
340 "rate_limit.blocked": false,
341 "rate_limit.exempt": true,
342 });
343 return { blocked: false } as const satisfies GateDecision;
344 }
345 // The only record that the backstop fired. A blocked execution is
346 // never usage-tracked (the gate short-circuits before the tracker)
347 // and deliberately not sent to Sentry — a backstop stopping
348 // runaway automation is expected, not exceptional — so without
349 // this line a blocked org is invisible outside a bug report, which
350 // is how the 2026-08-18 block went unnoticed until a customer
351 // sent a screenshot.
352 yield* Effect.sync(() => {
353 console.warn(
354 `[rate-limit] blocked execution for ${organizationId}: ${count} > ${limit} in window ${windowId}`,
355 );
356 });
357 yield* Effect.annotateCurrentSpan({
358 "rate_limit.blocked": true,
359 "rate_limit.exempt": false,
360 });
361 return {
362 blocked: true,
363 error: new ExecutionRateLimitExceededError({
364 organizationId,
365 message: RATE_LIMIT_BLOCKED_MESSAGE,
366 }),
367 } as const satisfies GateDecision;
368 });
369 }),
370 // FAIL OPEN: the backstop must never block executions because its
371 // counter is unreachable or slow.

Callers 1

makeExecutionRateLimiterFunction · 0.70

Calls 4

resolveExemptionFunction · 0.85
failOpenFunction · 0.85
warnMethod · 0.80
syncMethod · 0.65

Tested by

no test coverage detected