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

Function decide

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

Source from the content-addressed store, hash-verified

151 };
152
153 const decide = (organizationId: string): Effect.Effect<GateDecision> =>
154 Effect.suspend(() => {
155 const nowMs = Date.now();
156 const cached = cache.get(organizationId);
157 if (cached && cached.expiresAtMs > nowMs) {
158 return Effect.succeed(toDecision(organizationId, cached.allowed));
159 }
160 return checkBalance(organizationId).pipe(
161 Effect.timeoutOrElse({
162 duration: `${timeoutMs} millis`,
163 orElse: () => Effect.fail(new GateCheckTimeoutError({ timeoutMs })),
164 }),
165 Effect.map(({ allowed }) => {
166 writeCache(organizationId, allowed, nowMs);
167 return toDecision(organizationId, allowed);
168 }),
169 // FAIL OPEN: Autumn errors, timeouts, and missing customers/features
170 // must never block executions. Reported like `trackExecution` so a
171 // billing outage still pages; the error outcome is never cached.
172 Effect.catch((error: unknown) =>
173 Effect.gen(function* () {
174 yield* Effect.sync(() => {
175 console.warn("[billing] execution balance check failed open:", error);
176 });
177 yield* captureCauseEffect(error);
178 return { blocked: false } as const satisfies GateDecision;
179 }),
180 ),
181 );
182 });
183
184 return {
185 /** 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