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

Function decide

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

Source from the content-addressed store, hash-verified

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

Callers 1

makeExecutionRateLimiterFunction · 0.70

Calls 5

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

Tested by

no test coverage detected