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

Function decide

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

Source from the content-addressed store, hash-verified

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