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

Function decide

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

Source from the content-addressed store, hash-verified

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