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

Function decide

apps/cloud/src/engine/execution-gate.ts:149–176  ·  view source on GitHub ↗
(organizationId: string)

Source from the content-addressed store, hash-verified

147 };
148
149 const decide = (organizationId: string): Effect.Effect<GateDecision> =>
150 Effect.suspend(() => {
151 const nowMs = Date.now();
152 const cached = cache.get(organizationId);
153 if (cached && cached.expiresAtMs > nowMs) {
154 return Effect.succeed(toDecision(organizationId, cached.allowed));
155 }
156 return checkBalance(organizationId).pipe(
157 Effect.timeoutOrElse({
158 duration: `${timeoutMs} millis`,
159 orElse: () => Effect.fail(new GateCheckTimeoutError({ timeoutMs })),
160 }),
161 Effect.map(({ allowed }) => {
162 writeCache(organizationId, allowed, nowMs);
163 return toDecision(organizationId, allowed);
164 }),
165 // FAIL OPEN: Autumn errors, timeouts, and missing customers/features
166 // must never block executions. Reported like `trackExecution` so a
167 // billing outage still pages; the error outcome is never cached.
168 Effect.catch((error: unknown) =>
169 Effect.sync((): GateDecision => {
170 console.warn("[billing] execution balance check failed open:", error);
171 Sentry.captureException(error);
172 return { blocked: false };
173 }),
174 ),
175 );
176 });
177
178 return {
179 /** Wrap an engine so the balance gate runs before each new execution. */

Callers 1

makeExecutionLimitGateFunction · 0.70

Calls 5

toDecisionFunction · 0.85
syncMethod · 0.80
warnMethod · 0.80
writeCacheFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected