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

Function decide

apps/cloud/src/engine/execution-gate.ts:149–178  ·  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.gen(function* () {
170 yield* Effect.sync(() => {
171 console.warn("[billing] execution balance check failed open:", error);
172 });
173 yield* captureCauseEffect(error);
174 return { blocked: false } as const satisfies GateDecision;
175 }),
176 ),
177 );
178 });
179
180 return {
181 /** Wrap an engine so the balance gate runs before each new execution. */

Callers 1

makeExecutionLimitGateFunction · 0.70

Calls 6

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

Tested by

no test coverage detected